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. 23*40510e8eSJosef 'Jeff' Sipek * Copyright (c) 2011, 2016 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] 285f7a8e6dSDan 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 825f7a8e6dSDan McDonald extern int spa_asize_inflation; 835f7a8e6dSDan 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 272*40510e8eSJosef 'Jeff' Sipek /* 273*40510e8eSJosef 'Jeff' Sipek * We have to release the fsid syncronously or we risk that a subsequent 274*40510e8eSJosef 'Jeff' Sipek * mount of the same dataset will fail to unique_insert the fsid. This 275*40510e8eSJosef 'Jeff' Sipek * failure would manifest itself as the fsid of this dataset changing 276*40510e8eSJosef 'Jeff' Sipek * between mounts which makes NFS clients quite unhappy. 277*40510e8eSJosef 'Jeff' Sipek */ 278fa9e4066Sahrens static void 279*40510e8eSJosef 'Jeff' Sipek dsl_dataset_evict_sync(void *dbu) 280fa9e4066Sahrens { 281bc9014e6SJustin Gibbs dsl_dataset_t *ds = dbu; 282fa9e4066Sahrens 2833b2aab18SMatthew Ahrens ASSERT(ds->ds_owner == NULL); 284fa9e4066Sahrens 28591ebeef5Sahrens unique_remove(ds->ds_fsid_guid); 286*40510e8eSJosef 'Jeff' Sipek } 287*40510e8eSJosef 'Jeff' Sipek 288*40510e8eSJosef 'Jeff' Sipek static void 289*40510e8eSJosef 'Jeff' Sipek dsl_dataset_evict_async(void *dbu) 290*40510e8eSJosef 'Jeff' Sipek { 291*40510e8eSJosef 'Jeff' Sipek dsl_dataset_t *ds = dbu; 292*40510e8eSJosef 'Jeff' Sipek 293*40510e8eSJosef 'Jeff' Sipek ASSERT(ds->ds_owner == NULL); 294*40510e8eSJosef 'Jeff' Sipek 295*40510e8eSJosef 'Jeff' Sipek ds->ds_dbuf = NULL; 296fa9e4066Sahrens 297503ad85cSMatthew Ahrens if (ds->ds_objset != NULL) 298503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 299fa9e4066Sahrens 300fa9e4066Sahrens if (ds->ds_prev) { 3013b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 302fa9e4066Sahrens ds->ds_prev = NULL; 303fa9e4066Sahrens } 304fa9e4066Sahrens 305cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 306bc9014e6SJustin Gibbs if (ds->ds_deadlist.dl_os != NULL) 307cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 308745cd3c5Smaybee if (ds->ds_dir) 309bc9014e6SJustin Gibbs dsl_dir_async_rele(ds->ds_dir, ds); 310fa9e4066Sahrens 31191ebeef5Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 312fa9e4066Sahrens 31303bad06fSJustin Gibbs list_destroy(&ds->ds_prop_cbs); 3145ad82045Snd mutex_destroy(&ds->ds_lock); 31591ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 316d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 3173b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 318c166b69dSPaul Dagnelie rrw_destroy(&ds->ds_bp_rwlock); 3195ad82045Snd 320fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 321fa9e4066Sahrens } 322fa9e4066Sahrens 3233b2aab18SMatthew Ahrens int 324fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 325fa9e4066Sahrens { 326fa9e4066Sahrens dsl_dataset_phys_t *headphys; 327fa9e4066Sahrens int err; 328fa9e4066Sahrens dmu_buf_t *headdbuf; 329fa9e4066Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 330fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 331fa9e4066Sahrens 332fa9e4066Sahrens if (ds->ds_snapname[0]) 333ea8dc4b6Seschrock return (0); 334c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) 335ea8dc4b6Seschrock return (0); 336fa9e4066Sahrens 337c1379625SJustin T. Gibbs err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, 338ea8dc4b6Seschrock FTAG, &headdbuf); 3393b2aab18SMatthew Ahrens if (err != 0) 340ea8dc4b6Seschrock return (err); 341fa9e4066Sahrens headphys = headdbuf->db_data; 342fa9e4066Sahrens err = zap_value_search(dp->dp_meta_objset, 343e7437265Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 344ea8dc4b6Seschrock dmu_buf_rele(headdbuf, FTAG); 345ea8dc4b6Seschrock return (err); 346fa9e4066Sahrens } 347fa9e4066Sahrens 3483b2aab18SMatthew Ahrens int 349745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 350ab04eb8eStimh { 351745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 352c1379625SJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 353ab04eb8eStimh matchtype_t mt; 354ab04eb8eStimh int err; 355ab04eb8eStimh 356c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 357ab04eb8eStimh mt = MT_FIRST; 358ab04eb8eStimh else 359ab04eb8eStimh mt = MT_EXACT; 360ab04eb8eStimh 361745cd3c5Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 362ab04eb8eStimh value, mt, NULL, 0, NULL); 363ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 364745cd3c5Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 365ab04eb8eStimh return (err); 366ab04eb8eStimh } 367ab04eb8eStimh 3683b2aab18SMatthew Ahrens int 369a2afb611SJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx, 370a2afb611SJerry Jelinek boolean_t adj_cnt) 371ab04eb8eStimh { 372745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 373c1379625SJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 374ab04eb8eStimh matchtype_t mt; 375ab04eb8eStimh int err; 376ab04eb8eStimh 37771eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 37871eb0538SChris Kirby 379c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 380ab04eb8eStimh mt = MT_FIRST; 381ab04eb8eStimh else 382ab04eb8eStimh mt = MT_EXACT; 383ab04eb8eStimh 384745cd3c5Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 385ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 386745cd3c5Smaybee err = zap_remove(mos, snapobj, name, tx); 387a2afb611SJerry Jelinek 388a2afb611SJerry Jelinek if (err == 0 && adj_cnt) 389a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, -1, 390a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 391a2afb611SJerry Jelinek 392ab04eb8eStimh return (err); 393ab04eb8eStimh } 394ab04eb8eStimh 395e57a022bSJustin T. Gibbs boolean_t 396e57a022bSJustin T. Gibbs dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag) 397e57a022bSJustin T. Gibbs { 3989d47dec0SJustin T. Gibbs dmu_buf_t *dbuf = ds->ds_dbuf; 3999d47dec0SJustin T. Gibbs boolean_t result = B_FALSE; 4009d47dec0SJustin T. Gibbs 4019d47dec0SJustin T. Gibbs if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset, 4029d47dec0SJustin T. Gibbs ds->ds_object, DMU_BONUS_BLKID, tag)) { 4039d47dec0SJustin T. Gibbs 4049d47dec0SJustin T. Gibbs if (ds == dmu_buf_get_user(dbuf)) 4059d47dec0SJustin T. Gibbs result = B_TRUE; 4069d47dec0SJustin T. Gibbs else 4079d47dec0SJustin T. Gibbs dmu_buf_rele(dbuf, tag); 4089d47dec0SJustin T. Gibbs } 4099d47dec0SJustin T. Gibbs 4109d47dec0SJustin T. Gibbs return (result); 411e57a022bSJustin T. Gibbs } 412e57a022bSJustin T. Gibbs 4133b2aab18SMatthew Ahrens int 4143b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 415745cd3c5Smaybee dsl_dataset_t **dsp) 416fa9e4066Sahrens { 417fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 418fa9e4066Sahrens dmu_buf_t *dbuf; 419fa9e4066Sahrens dsl_dataset_t *ds; 420ea8dc4b6Seschrock int err; 421a7f53a56SChris Kirby dmu_object_info_t doi; 422fa9e4066Sahrens 4233b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 424fa9e4066Sahrens 425ea8dc4b6Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 4263b2aab18SMatthew Ahrens if (err != 0) 427ea8dc4b6Seschrock return (err); 428a7f53a56SChris Kirby 429a7f53a56SChris Kirby /* Make sure dsobj has the correct object type. */ 430a7f53a56SChris Kirby dmu_object_info_from_db(dbuf, &doi); 4312acef22dSMatthew Ahrens if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) { 432b287be1bSWill Andrews dmu_buf_rele(dbuf, tag); 433be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 434b287be1bSWill Andrews } 435a7f53a56SChris Kirby 436fa9e4066Sahrens ds = dmu_buf_get_user(dbuf); 437fa9e4066Sahrens if (ds == NULL) { 438d5285caeSGeorge Wilson dsl_dataset_t *winner = NULL; 439fa9e4066Sahrens 440fa9e4066Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 441fa9e4066Sahrens ds->ds_dbuf = dbuf; 442fa9e4066Sahrens ds->ds_object = dsobj; 443bc9014e6SJustin Gibbs ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0; 444fa9e4066Sahrens 4455ad82045Snd mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 44691ebeef5Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 4474e3c9f44SBill Pijewski mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL); 448c166b69dSPaul Dagnelie rrw_init(&ds->ds_bp_rwlock, B_FALSE); 4493b2aab18SMatthew Ahrens refcount_create(&ds->ds_longholds); 4505ad82045Snd 451cde58dbcSMatthew Ahrens bplist_create(&ds->ds_pending_deadlist); 452cde58dbcSMatthew Ahrens dsl_deadlist_open(&ds->ds_deadlist, 453c1379625SJustin T. Gibbs mos, dsl_dataset_phys(ds)->ds_deadlist_obj); 454cde58dbcSMatthew Ahrens 4554e3c9f44SBill Pijewski list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t), 4564e3c9f44SBill Pijewski offsetof(dmu_sendarg_t, dsa_link)); 4574e3c9f44SBill Pijewski 45803bad06fSJustin Gibbs list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t), 45903bad06fSJustin Gibbs offsetof(dsl_prop_cb_record_t, cbr_ds_node)); 46003bad06fSJustin Gibbs 461b5152584SMatthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 462ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 463ca0cc391SMatthew Ahrens if (!(spa_feature_table[f].fi_flags & 464ca0cc391SMatthew Ahrens ZFEATURE_FLAG_PER_DATASET)) 465ca0cc391SMatthew Ahrens continue; 466ca0cc391SMatthew Ahrens err = zap_contains(mos, dsobj, 467ca0cc391SMatthew Ahrens spa_feature_table[f].fi_guid); 468ca0cc391SMatthew Ahrens if (err == 0) { 469ca0cc391SMatthew Ahrens ds->ds_feature_inuse[f] = B_TRUE; 470ca0cc391SMatthew Ahrens } else { 471ca0cc391SMatthew Ahrens ASSERT3U(err, ==, ENOENT); 472ca0cc391SMatthew Ahrens err = 0; 473ca0cc391SMatthew Ahrens } 474e1f3c208SJustin T. Gibbs } 475b5152584SMatthew Ahrens } 476b5152584SMatthew Ahrens 477ca0cc391SMatthew Ahrens err = dsl_dir_hold_obj(dp, 478ca0cc391SMatthew Ahrens dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir); 4793b2aab18SMatthew Ahrens if (err != 0) { 4805ad82045Snd mutex_destroy(&ds->ds_lock); 48191ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 482d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 4833b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 484cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 485cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 486ea8dc4b6Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 487ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 488ea8dc4b6Seschrock return (err); 489ea8dc4b6Seschrock } 490fa9e4066Sahrens 491bc9014e6SJustin Gibbs if (!ds->ds_is_snapshot) { 492fa9e4066Sahrens ds->ds_snapname[0] = '\0'; 493c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 4943b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, 495c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, 496745cd3c5Smaybee ds, &ds->ds_prev); 497fa9e4066Sahrens } 49878f17100SMatthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 49978f17100SMatthew Ahrens int zaperr = zap_lookup(mos, ds->ds_object, 50078f17100SMatthew Ahrens DS_FIELD_BOOKMARK_NAMES, 50178f17100SMatthew Ahrens sizeof (ds->ds_bookmarks), 1, 50278f17100SMatthew Ahrens &ds->ds_bookmarks); 50378f17100SMatthew Ahrens if (zaperr != ENOENT) 50478f17100SMatthew Ahrens VERIFY0(zaperr); 50578f17100SMatthew Ahrens } 506842727c2SChris Kirby } else { 507842727c2SChris Kirby if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 508842727c2SChris Kirby err = dsl_dataset_get_snapname(ds); 509c1379625SJustin T. Gibbs if (err == 0 && 510c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj != 0) { 511842727c2SChris Kirby err = zap_count( 512842727c2SChris Kirby ds->ds_dir->dd_pool->dp_meta_objset, 513c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj, 514842727c2SChris Kirby &ds->ds_userrefs); 515842727c2SChris Kirby } 516fa9e4066Sahrens } 517fa9e4066Sahrens 518bc9014e6SJustin Gibbs if (err == 0 && !ds->ds_is_snapshot) { 5193b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 5203b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 5213b2aab18SMatthew Ahrens &ds->ds_reserved); 522cb625fb5Sck if (err == 0) { 5233b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 5243b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 5253b2aab18SMatthew Ahrens &ds->ds_quota); 526cb625fb5Sck } 527cb625fb5Sck } else { 528cb625fb5Sck ds->ds_reserved = ds->ds_quota = 0; 529cb625fb5Sck } 530cb625fb5Sck 531*40510e8eSJosef 'Jeff' Sipek dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync, 532*40510e8eSJosef 'Jeff' Sipek dsl_dataset_evict_async, &ds->ds_dbuf); 533bc9014e6SJustin Gibbs if (err == 0) 534bc9014e6SJustin Gibbs winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu); 535bc9014e6SJustin Gibbs 536bc9014e6SJustin Gibbs if (err != 0 || winner != NULL) { 537cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 538cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 539745cd3c5Smaybee if (ds->ds_prev) 5403b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 5413b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 5425ad82045Snd mutex_destroy(&ds->ds_lock); 54391ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 544d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 5453b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 546fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 5473b2aab18SMatthew Ahrens if (err != 0) { 548ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 549ea8dc4b6Seschrock return (err); 550ea8dc4b6Seschrock } 551fa9e4066Sahrens ds = winner; 552fa9e4066Sahrens } else { 55391ebeef5Sahrens ds->ds_fsid_guid = 554c1379625SJustin T. Gibbs unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid); 555*40510e8eSJosef 'Jeff' Sipek if (ds->ds_fsid_guid != 556*40510e8eSJosef 'Jeff' Sipek dsl_dataset_phys(ds)->ds_fsid_guid) { 557*40510e8eSJosef 'Jeff' Sipek zfs_dbgmsg("ds_fsid_guid changed from " 558*40510e8eSJosef 'Jeff' Sipek "%llx to %llx for pool %s dataset id %llu", 559*40510e8eSJosef 'Jeff' Sipek (long long) 560*40510e8eSJosef 'Jeff' Sipek dsl_dataset_phys(ds)->ds_fsid_guid, 561*40510e8eSJosef 'Jeff' Sipek (long long)ds->ds_fsid_guid, 562*40510e8eSJosef 'Jeff' Sipek spa_name(dp->dp_spa), 563*40510e8eSJosef 'Jeff' Sipek dsobj); 564*40510e8eSJosef 'Jeff' Sipek } 565fa9e4066Sahrens } 566fa9e4066Sahrens } 567fa9e4066Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 568c1379625SJustin T. Gibbs ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data); 569c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 || 570afc6333aSahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 57184db2a68Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 572ea8dc4b6Seschrock *dsp = ds; 573ea8dc4b6Seschrock return (0); 574fa9e4066Sahrens } 575fa9e4066Sahrens 576745cd3c5Smaybee int 5773b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name, 578503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 579fa9e4066Sahrens { 580fa9e4066Sahrens dsl_dir_t *dd; 581745cd3c5Smaybee const char *snapname; 582fa9e4066Sahrens uint64_t obj; 583fa9e4066Sahrens int err = 0; 584a2cdcdd2SPaul Dagnelie dsl_dataset_t *ds; 585fa9e4066Sahrens 5863b2aab18SMatthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname); 5873b2aab18SMatthew Ahrens if (err != 0) 588ea8dc4b6Seschrock return (err); 589fa9e4066Sahrens 5903b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 591c1379625SJustin T. Gibbs obj = dsl_dir_phys(dd)->dd_head_dataset_obj; 5923b2aab18SMatthew Ahrens if (obj != 0) 593a2cdcdd2SPaul Dagnelie err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 594745cd3c5Smaybee else 595be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 596fa9e4066Sahrens 597745cd3c5Smaybee /* we may be looking for a snapshot */ 598745cd3c5Smaybee if (err == 0 && snapname != NULL) { 599a2cdcdd2SPaul Dagnelie dsl_dataset_t *snap_ds; 600fa9e4066Sahrens 601745cd3c5Smaybee if (*snapname++ != '@') { 602a2cdcdd2SPaul Dagnelie dsl_dataset_rele(ds, tag); 6033b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 604be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 605fa9e4066Sahrens } 606fa9e4066Sahrens 607745cd3c5Smaybee dprintf("looking for snapshot '%s'\n", snapname); 608a2cdcdd2SPaul Dagnelie err = dsl_dataset_snap_lookup(ds, snapname, &obj); 609745cd3c5Smaybee if (err == 0) 610a2cdcdd2SPaul Dagnelie err = dsl_dataset_hold_obj(dp, obj, tag, &snap_ds); 611a2cdcdd2SPaul Dagnelie dsl_dataset_rele(ds, tag); 612745cd3c5Smaybee 6133b2aab18SMatthew Ahrens if (err == 0) { 614a2cdcdd2SPaul Dagnelie mutex_enter(&snap_ds->ds_lock); 615a2cdcdd2SPaul Dagnelie if (snap_ds->ds_snapname[0] == 0) 616a2cdcdd2SPaul Dagnelie (void) strlcpy(snap_ds->ds_snapname, snapname, 617a2cdcdd2SPaul Dagnelie sizeof (snap_ds->ds_snapname)); 618a2cdcdd2SPaul Dagnelie mutex_exit(&snap_ds->ds_lock); 619a2cdcdd2SPaul Dagnelie ds = snap_ds; 620fa9e4066Sahrens } 621fa9e4066Sahrens } 622a2cdcdd2SPaul Dagnelie if (err == 0) 623a2cdcdd2SPaul Dagnelie *dsp = ds; 6243b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 625fa9e4066Sahrens return (err); 626fa9e4066Sahrens } 627fa9e4066Sahrens 628fa9e4066Sahrens int 6293b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, 6303b2aab18SMatthew Ahrens void *tag, dsl_dataset_t **dsp) 6313b2aab18SMatthew Ahrens { 6323b2aab18SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 6333b2aab18SMatthew Ahrens if (err != 0) 6343b2aab18SMatthew Ahrens return (err); 6353b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 6363b2aab18SMatthew Ahrens dsl_dataset_rele(*dsp, tag); 6373b2aab18SMatthew Ahrens *dsp = NULL; 638be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 6393b2aab18SMatthew Ahrens } 6403b2aab18SMatthew Ahrens return (0); 6413b2aab18SMatthew Ahrens } 6423b2aab18SMatthew Ahrens 6433b2aab18SMatthew Ahrens int 6443b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name, 645503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 646fa9e4066Sahrens { 6473b2aab18SMatthew Ahrens int err = dsl_dataset_hold(dp, name, tag, dsp); 6483b2aab18SMatthew Ahrens if (err != 0) 649745cd3c5Smaybee return (err); 6503b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 651503ad85cSMatthew Ahrens dsl_dataset_rele(*dsp, tag); 652be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 653745cd3c5Smaybee } 654745cd3c5Smaybee return (0); 655fa9e4066Sahrens } 656fa9e4066Sahrens 6573b2aab18SMatthew Ahrens /* 6583b2aab18SMatthew Ahrens * See the comment above dsl_pool_hold() for details. In summary, a long 6593b2aab18SMatthew Ahrens * hold is used to prevent destruction of a dataset while the pool hold 6603b2aab18SMatthew Ahrens * is dropped, allowing other concurrent operations (e.g. spa_sync()). 6613b2aab18SMatthew Ahrens * 6623b2aab18SMatthew Ahrens * The dataset and pool must be held when this function is called. After it 6633b2aab18SMatthew Ahrens * is called, the pool hold may be released while the dataset is still held 6643b2aab18SMatthew Ahrens * and accessed. 6653b2aab18SMatthew Ahrens */ 6663b2aab18SMatthew Ahrens void 6673b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag) 6683b2aab18SMatthew Ahrens { 6693b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 6703b2aab18SMatthew Ahrens (void) refcount_add(&ds->ds_longholds, tag); 6713b2aab18SMatthew Ahrens } 6723b2aab18SMatthew Ahrens 6733b2aab18SMatthew Ahrens void 6743b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag) 6753b2aab18SMatthew Ahrens { 6763b2aab18SMatthew Ahrens (void) refcount_remove(&ds->ds_longholds, tag); 6773b2aab18SMatthew Ahrens } 6783b2aab18SMatthew Ahrens 6793b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */ 6803b2aab18SMatthew Ahrens boolean_t 6813b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds) 6823b2aab18SMatthew Ahrens { 6833b2aab18SMatthew Ahrens return (!refcount_is_zero(&ds->ds_longholds)); 6843b2aab18SMatthew Ahrens } 6853b2aab18SMatthew Ahrens 686fa9e4066Sahrens void 687fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 688fa9e4066Sahrens { 689fa9e4066Sahrens if (ds == NULL) { 690fa9e4066Sahrens (void) strcpy(name, "mos"); 691fa9e4066Sahrens } else { 692fa9e4066Sahrens dsl_dir_name(ds->ds_dir, name); 6933b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 694fa9e4066Sahrens if (ds->ds_snapname[0]) { 6959adfa60dSMatthew Ahrens VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN), 6969adfa60dSMatthew Ahrens <, ZFS_MAX_DATASET_NAME_LEN); 697745cd3c5Smaybee /* 698745cd3c5Smaybee * We use a "recursive" mutex so that we 699745cd3c5Smaybee * can call dprintf_ds() with ds_lock held. 700745cd3c5Smaybee */ 701fa9e4066Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 702fa9e4066Sahrens mutex_enter(&ds->ds_lock); 7039adfa60dSMatthew Ahrens VERIFY3U(strlcat(name, ds->ds_snapname, 7049adfa60dSMatthew Ahrens ZFS_MAX_DATASET_NAME_LEN), <, 7059adfa60dSMatthew Ahrens ZFS_MAX_DATASET_NAME_LEN); 706fa9e4066Sahrens mutex_exit(&ds->ds_lock); 707fa9e4066Sahrens } else { 7089adfa60dSMatthew Ahrens VERIFY3U(strlcat(name, ds->ds_snapname, 7099adfa60dSMatthew Ahrens ZFS_MAX_DATASET_NAME_LEN), <, 7109adfa60dSMatthew Ahrens ZFS_MAX_DATASET_NAME_LEN); 711fa9e4066Sahrens } 712fa9e4066Sahrens } 713fa9e4066Sahrens } 714fa9e4066Sahrens } 715fa9e4066Sahrens 7169adfa60dSMatthew Ahrens int 7179adfa60dSMatthew Ahrens dsl_dataset_namelen(dsl_dataset_t *ds) 7189adfa60dSMatthew Ahrens { 7199adfa60dSMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 7209adfa60dSMatthew Ahrens mutex_enter(&ds->ds_lock); 7219adfa60dSMatthew Ahrens int len = dsl_dir_namelen(ds->ds_dir) + 1 + strlen(ds->ds_snapname); 7229adfa60dSMatthew Ahrens mutex_exit(&ds->ds_lock); 7239adfa60dSMatthew Ahrens return (len); 7249adfa60dSMatthew Ahrens } 7259adfa60dSMatthew Ahrens 7263cb34c60Sahrens void 727745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 7283cb34c60Sahrens { 7293b2aab18SMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, tag); 730745cd3c5Smaybee } 731745cd3c5Smaybee 732745cd3c5Smaybee void 733503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 734745cd3c5Smaybee { 735d808a4fcSJustin T. Gibbs ASSERT3P(ds->ds_owner, ==, tag); 736d808a4fcSJustin T. Gibbs ASSERT(ds->ds_dbuf != NULL); 737745cd3c5Smaybee 7383cb34c60Sahrens mutex_enter(&ds->ds_lock); 739745cd3c5Smaybee ds->ds_owner = NULL; 7403cb34c60Sahrens mutex_exit(&ds->ds_lock); 7413b2aab18SMatthew Ahrens dsl_dataset_long_rele(ds, tag); 742d808a4fcSJustin T. Gibbs dsl_dataset_rele(ds, tag); 7433cb34c60Sahrens } 7443cb34c60Sahrens 7453cb34c60Sahrens boolean_t 7463b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag) 7473cb34c60Sahrens { 748745cd3c5Smaybee boolean_t gotit = FALSE; 749745cd3c5Smaybee 7509c3fd121SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 7513cb34c60Sahrens mutex_enter(&ds->ds_lock); 7523b2aab18SMatthew Ahrens if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) { 753503ad85cSMatthew Ahrens ds->ds_owner = tag; 7543b2aab18SMatthew Ahrens dsl_dataset_long_hold(ds, tag); 755745cd3c5Smaybee gotit = TRUE; 7563cb34c60Sahrens } 7573cb34c60Sahrens mutex_exit(&ds->ds_lock); 758745cd3c5Smaybee return (gotit); 759745cd3c5Smaybee } 760745cd3c5Smaybee 7619c3fd121SMatthew Ahrens boolean_t 7629c3fd121SMatthew Ahrens dsl_dataset_has_owner(dsl_dataset_t *ds) 7639c3fd121SMatthew Ahrens { 7649c3fd121SMatthew Ahrens boolean_t rv; 7659c3fd121SMatthew Ahrens mutex_enter(&ds->ds_lock); 7669c3fd121SMatthew Ahrens rv = (ds->ds_owner != NULL); 7679c3fd121SMatthew Ahrens mutex_exit(&ds->ds_lock); 7689c3fd121SMatthew Ahrens return (rv); 7699c3fd121SMatthew Ahrens } 7709c3fd121SMatthew Ahrens 771ca0cc391SMatthew Ahrens static void 772ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx) 773ca0cc391SMatthew Ahrens { 774ca0cc391SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 775ca0cc391SMatthew Ahrens objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 776ca0cc391SMatthew Ahrens uint64_t zero = 0; 777ca0cc391SMatthew Ahrens 778ca0cc391SMatthew Ahrens VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET); 779ca0cc391SMatthew Ahrens 780ca0cc391SMatthew Ahrens spa_feature_incr(spa, f, tx); 781ca0cc391SMatthew Ahrens dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx); 782ca0cc391SMatthew Ahrens 783ca0cc391SMatthew Ahrens VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid, 784ca0cc391SMatthew Ahrens sizeof (zero), 1, &zero, tx)); 785ca0cc391SMatthew Ahrens } 786ca0cc391SMatthew Ahrens 787ca0cc391SMatthew Ahrens void 788ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx) 789ca0cc391SMatthew Ahrens { 790ca0cc391SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 791ca0cc391SMatthew Ahrens objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 792ca0cc391SMatthew Ahrens 793ca0cc391SMatthew Ahrens VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET); 794ca0cc391SMatthew Ahrens 795ca0cc391SMatthew Ahrens VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx)); 796ca0cc391SMatthew Ahrens spa_feature_decr(spa, f, tx); 797ca0cc391SMatthew Ahrens } 798ca0cc391SMatthew Ahrens 7991d452cf5Sahrens uint64_t 800088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 801ab04eb8eStimh uint64_t flags, dmu_tx_t *tx) 802fa9e4066Sahrens { 8033cb34c60Sahrens dsl_pool_t *dp = dd->dd_pool; 804fa9e4066Sahrens dmu_buf_t *dbuf; 805fa9e4066Sahrens dsl_dataset_phys_t *dsphys; 8063cb34c60Sahrens uint64_t dsobj; 807fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 808fa9e4066Sahrens 809088f3894Sahrens if (origin == NULL) 810088f3894Sahrens origin = dp->dp_origin_snap; 811088f3894Sahrens 8123cb34c60Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 813c1379625SJustin T. Gibbs ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0); 814fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 815c1379625SJustin T. Gibbs ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0); 816fa9e4066Sahrens 8171649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 8181649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 8193b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 820fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 821fa9e4066Sahrens dsphys = dbuf->db_data; 822745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 823fa9e4066Sahrens dsphys->ds_dir_obj = dd->dd_object; 824ab04eb8eStimh dsphys->ds_flags = flags; 825fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 826fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 827fa9e4066Sahrens sizeof (dsphys->ds_guid)); 828fa9e4066Sahrens dsphys->ds_snapnames_zapobj = 829ab04eb8eStimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 830ab04eb8eStimh DMU_OT_NONE, 0, tx); 831fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 832088f3894Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 833a9799022Sck 834cde58dbcSMatthew Ahrens if (origin == NULL) { 835cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx); 836cde58dbcSMatthew Ahrens } else { 8373b2aab18SMatthew Ahrens dsl_dataset_t *ohds; /* head of the origin snapshot */ 838cde58dbcSMatthew Ahrens 8393cb34c60Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 840fa9e4066Sahrens dsphys->ds_prev_snap_txg = 841c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_creation_txg; 842ad135b5dSChristopher Siden dsphys->ds_referenced_bytes = 843c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_referenced_bytes; 844fa9e4066Sahrens dsphys->ds_compressed_bytes = 845c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_compressed_bytes; 846fa9e4066Sahrens dsphys->ds_uncompressed_bytes = 847c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_uncompressed_bytes; 848c166b69dSPaul Dagnelie rrw_enter(&origin->ds_bp_rwlock, RW_READER, FTAG); 849c1379625SJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp; 850c166b69dSPaul Dagnelie rrw_exit(&origin->ds_bp_rwlock, FTAG); 85142fcb65eSMatthew Ahrens 85242fcb65eSMatthew Ahrens /* 85342fcb65eSMatthew Ahrens * Inherit flags that describe the dataset's contents 85442fcb65eSMatthew Ahrens * (INCONSISTENT) or properties (Case Insensitive). 85542fcb65eSMatthew Ahrens */ 856c1379625SJustin T. Gibbs dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags & 85742fcb65eSMatthew Ahrens (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET); 858fa9e4066Sahrens 859ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 860ca0cc391SMatthew Ahrens if (origin->ds_feature_inuse[f]) 861ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(dsobj, f, tx); 862ca0cc391SMatthew Ahrens } 863b5152584SMatthew Ahrens 8643cb34c60Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 865c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_num_children++; 866fa9e4066Sahrens 8673b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 868c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj, 869c1379625SJustin T. Gibbs FTAG, &ohds)); 870cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist, 871cde58dbcSMatthew Ahrens dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx); 872cde58dbcSMatthew Ahrens dsl_dataset_rele(ohds, FTAG); 873cde58dbcSMatthew Ahrens 874088f3894Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 875c1379625SJustin T. Gibbs if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) { 876c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj = 877088f3894Sahrens zap_create(mos, 878088f3894Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 879088f3894Sahrens } 8803b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 881c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj, 882c1379625SJustin T. Gibbs dsobj, tx)); 883088f3894Sahrens } 884088f3894Sahrens 885fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 886c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object; 887cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 888c1379625SJustin T. Gibbs if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) { 889cde58dbcSMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 890c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones = 891cde58dbcSMatthew Ahrens zap_create(mos, 892cde58dbcSMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 893cde58dbcSMatthew Ahrens } 8943b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 895c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones, 896c1379625SJustin T. Gibbs dsobj, tx)); 897cde58dbcSMatthew Ahrens } 898fa9e4066Sahrens } 899ab04eb8eStimh 900ab04eb8eStimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 901ab04eb8eStimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 902ab04eb8eStimh 903ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 904fa9e4066Sahrens 905fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 906c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj; 9073cb34c60Sahrens 9083cb34c60Sahrens return (dsobj); 9093cb34c60Sahrens } 9103cb34c60Sahrens 9113b2aab18SMatthew Ahrens static void 9123b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx) 9133b2aab18SMatthew Ahrens { 9143b2aab18SMatthew Ahrens objset_t *os; 9153b2aab18SMatthew Ahrens 9163b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os)); 9173b2aab18SMatthew Ahrens bzero(&os->os_zil_header, sizeof (os->os_zil_header)); 9183b2aab18SMatthew Ahrens dsl_dataset_dirty(ds, tx); 9193b2aab18SMatthew Ahrens } 9203b2aab18SMatthew Ahrens 9213cb34c60Sahrens uint64_t 922ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 923ab04eb8eStimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 9243cb34c60Sahrens { 9253cb34c60Sahrens dsl_pool_t *dp = pdd->dd_pool; 9263cb34c60Sahrens uint64_t dsobj, ddobj; 9273cb34c60Sahrens dsl_dir_t *dd; 9283cb34c60Sahrens 9293b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 9303cb34c60Sahrens ASSERT(lastname[0] != '@'); 9313cb34c60Sahrens 932088f3894Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 9333b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd)); 9343cb34c60Sahrens 9353b2aab18SMatthew Ahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, 9363b2aab18SMatthew Ahrens flags & ~DS_CREATE_FLAG_NODIRTY, tx); 9373cb34c60Sahrens 9383cb34c60Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 9393cb34c60Sahrens 940a2afb611SJerry Jelinek /* 941a2afb611SJerry Jelinek * Since we're creating a new node we know it's a leaf, so we can 942a2afb611SJerry Jelinek * initialize the counts if the limit feature is active. 943a2afb611SJerry Jelinek */ 944a2afb611SJerry Jelinek if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { 945a2afb611SJerry Jelinek uint64_t cnt = 0; 946a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 947a2afb611SJerry Jelinek 948a2afb611SJerry Jelinek dsl_dir_zapify(dd, tx); 949a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 950a2afb611SJerry Jelinek sizeof (cnt), 1, &cnt, tx)); 951a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 952a2afb611SJerry Jelinek sizeof (cnt), 1, &cnt, tx)); 953a2afb611SJerry Jelinek } 954a2afb611SJerry Jelinek 9553b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 956fa9e4066Sahrens 957feaa74e4SMark Maybee /* 958feaa74e4SMark Maybee * If we are creating a clone, make sure we zero out any stale 959feaa74e4SMark Maybee * data from the origin snapshots zil header. 960feaa74e4SMark Maybee */ 9613b2aab18SMatthew Ahrens if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) { 962feaa74e4SMark Maybee dsl_dataset_t *ds; 963feaa74e4SMark Maybee 9643b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 9653b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 966feaa74e4SMark Maybee dsl_dataset_rele(ds, FTAG); 967feaa74e4SMark Maybee } 968feaa74e4SMark Maybee 9691d452cf5Sahrens return (dsobj); 970fa9e4066Sahrens } 971fa9e4066Sahrens 9721d452cf5Sahrens /* 9733b2aab18SMatthew Ahrens * The unique space in the head dataset can be calculated by subtracting 9743b2aab18SMatthew Ahrens * the space used in the most recent snapshot, that is still being used 9753b2aab18SMatthew Ahrens * in this file system, from the space currently in use. To figure out 9763b2aab18SMatthew Ahrens * the space in the most recent snapshot still in use, we need to take 9773b2aab18SMatthew Ahrens * the total space used in the snapshot and subtract out the space that 9783b2aab18SMatthew Ahrens * has been freed up since the snapshot was taken. 9791d452cf5Sahrens */ 9803b2aab18SMatthew Ahrens void 9813b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 9821d452cf5Sahrens { 9833b2aab18SMatthew Ahrens uint64_t mrs_used; 9843b2aab18SMatthew Ahrens uint64_t dlused, dlcomp, dluncomp; 9851d452cf5Sahrens 986bc9014e6SJustin Gibbs ASSERT(!ds->ds_is_snapshot); 9871d452cf5Sahrens 988c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) 989c1379625SJustin T. Gibbs mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes; 9903b2aab18SMatthew Ahrens else 9913b2aab18SMatthew Ahrens mrs_used = 0; 992842727c2SChris Kirby 9933b2aab18SMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp); 994fa9e4066Sahrens 9953b2aab18SMatthew Ahrens ASSERT3U(dlused, <=, mrs_used); 996c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes = 997c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused); 99819b94df9SMatthew Ahrens 9993b2aab18SMatthew Ahrens if (spa_version(ds->ds_dir->dd_pool->dp_spa) >= 10003b2aab18SMatthew Ahrens SPA_VERSION_UNIQUE_ACCURATE) 1001c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1002fa9e4066Sahrens } 1003fa9e4066Sahrens 10043b2aab18SMatthew Ahrens void 10053b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, 10063b2aab18SMatthew Ahrens dmu_tx_t *tx) 1007842727c2SChris Kirby { 10083b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 10093b2aab18SMatthew Ahrens uint64_t count; 10103b2aab18SMatthew Ahrens int err; 10113b2aab18SMatthew Ahrens 1012c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2); 1013c1379625SJustin T. Gibbs err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 1014c1379625SJustin T. Gibbs obj, tx); 10153b2aab18SMatthew Ahrens /* 10163b2aab18SMatthew Ahrens * The err should not be ENOENT, but a bug in a previous version 10173b2aab18SMatthew Ahrens * of the code could cause upgrade_clones_cb() to not set 10183b2aab18SMatthew Ahrens * ds_next_snap_obj when it should, leading to a missing entry. 10193b2aab18SMatthew Ahrens * If we knew that the pool was created after 10203b2aab18SMatthew Ahrens * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 10213b2aab18SMatthew Ahrens * ENOENT. However, at least we can check that we don't have 10223b2aab18SMatthew Ahrens * too many entries in the next_clones_obj even after failing to 10233b2aab18SMatthew Ahrens * remove this one. 10243b2aab18SMatthew Ahrens */ 10253b2aab18SMatthew Ahrens if (err != ENOENT) 10263b2aab18SMatthew Ahrens VERIFY0(err); 1027c1379625SJustin T. Gibbs ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 10283b2aab18SMatthew Ahrens &count)); 1029c1379625SJustin T. Gibbs ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2); 10303b2aab18SMatthew Ahrens } 1031842727c2SChris Kirby 1032842727c2SChris Kirby 10333b2aab18SMatthew Ahrens blkptr_t * 10343b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds) 10353b2aab18SMatthew Ahrens { 1036c1379625SJustin T. Gibbs return (&dsl_dataset_phys(ds)->ds_bp); 1037842727c2SChris Kirby } 1038842727c2SChris Kirby 10393b2aab18SMatthew Ahrens spa_t * 10403b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 10413b2aab18SMatthew Ahrens { 10423b2aab18SMatthew Ahrens return (ds->ds_dir->dd_pool->dp_spa); 1043842727c2SChris Kirby } 1044842727c2SChris Kirby 10453b2aab18SMatthew Ahrens void 10463b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1047fa9e4066Sahrens { 10483b2aab18SMatthew Ahrens dsl_pool_t *dp; 1049842727c2SChris Kirby 10503b2aab18SMatthew Ahrens if (ds == NULL) /* this is the meta-objset */ 10513b2aab18SMatthew Ahrens return; 10521d452cf5Sahrens 10533b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 1054fa9e4066Sahrens 1055c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) 10563b2aab18SMatthew Ahrens panic("dirtying snapshot!"); 1057fa9e4066Sahrens 10583b2aab18SMatthew Ahrens dp = ds->ds_dir->dd_pool; 1059ce636f8bSMatthew Ahrens 10603b2aab18SMatthew Ahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) { 10613b2aab18SMatthew Ahrens /* up the hold count until we can be written out */ 10623b2aab18SMatthew Ahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 10633b2aab18SMatthew Ahrens } 10643b2aab18SMatthew Ahrens } 1065fa9e4066Sahrens 10662e2c1355SMatthew Ahrens boolean_t 10672e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds) 10682e2c1355SMatthew Ahrens { 10692e2c1355SMatthew Ahrens for (int t = 0; t < TXG_SIZE; t++) { 10702e2c1355SMatthew Ahrens if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets, 10712e2c1355SMatthew Ahrens ds, t)) 10722e2c1355SMatthew Ahrens return (B_TRUE); 10732e2c1355SMatthew Ahrens } 10742e2c1355SMatthew Ahrens return (B_FALSE); 10752e2c1355SMatthew Ahrens } 10762e2c1355SMatthew Ahrens 1077fa9e4066Sahrens static int 10783b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 1079fa9e4066Sahrens { 10803b2aab18SMatthew Ahrens uint64_t asize; 1081fa9e4066Sahrens 10823b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 108388b7b0f2SMatthew Ahrens return (0); 1084fa9e4066Sahrens 1085e1930233Sbonwick /* 10863b2aab18SMatthew Ahrens * If there's an fs-only reservation, any blocks that might become 10873b2aab18SMatthew Ahrens * owned by the snapshot dataset must be accommodated by space 10883b2aab18SMatthew Ahrens * outside of the reservation. 1089e1930233Sbonwick */ 10903b2aab18SMatthew Ahrens ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds)); 1091c1379625SJustin T. Gibbs asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved); 10923b2aab18SMatthew Ahrens if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 1093be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 1094e1930233Sbonwick 10953cb34c60Sahrens /* 10963b2aab18SMatthew Ahrens * Propagate any reserved space for this snapshot to other 10973b2aab18SMatthew Ahrens * snapshot checks in this sync group. 10983cb34c60Sahrens */ 10993b2aab18SMatthew Ahrens if (asize > 0) 11003b2aab18SMatthew Ahrens dsl_dir_willuse_space(ds->ds_dir, asize, tx); 11013cb34c60Sahrens 1102e1930233Sbonwick return (0); 1103e1930233Sbonwick } 1104e1930233Sbonwick 11053b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg { 11063b2aab18SMatthew Ahrens nvlist_t *ddsa_snaps; 11073b2aab18SMatthew Ahrens nvlist_t *ddsa_props; 11083b2aab18SMatthew Ahrens nvlist_t *ddsa_errors; 1109a2afb611SJerry Jelinek cred_t *ddsa_cr; 11103b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t; 1111842727c2SChris Kirby 11123cb34c60Sahrens int 11133b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname, 1114a2afb611SJerry Jelinek dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr) 11151d452cf5Sahrens { 11163b2aab18SMatthew Ahrens int error; 11173b2aab18SMatthew Ahrens uint64_t value; 1118fa9e4066Sahrens 11193b2aab18SMatthew Ahrens ds->ds_trysnap_txg = tx->tx_txg; 1120745cd3c5Smaybee 11213b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 1122842727c2SChris Kirby return (0); 1123fa9e4066Sahrens 1124fa9e4066Sahrens /* 11253b2aab18SMatthew Ahrens * We don't allow multiple snapshots of the same txg. If there 11263b2aab18SMatthew Ahrens * is already one, try again. 1127fa9e4066Sahrens */ 1128c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) 1129be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 1130fa9e4066Sahrens 1131fa9e4066Sahrens /* 11323b2aab18SMatthew Ahrens * Check for conflicting snapshot name. 1133fa9e4066Sahrens */ 11343b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(ds, snapname, &value); 11353b2aab18SMatthew Ahrens if (error == 0) 1136be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 11373b2aab18SMatthew Ahrens if (error != ENOENT) 11383b2aab18SMatthew Ahrens return (error); 1139842727c2SChris Kirby 1140ca48f36fSKeith M Wesolowski /* 1141ca48f36fSKeith M Wesolowski * We don't allow taking snapshots of inconsistent datasets, such as 1142ca48f36fSKeith M Wesolowski * those into which we are currently receiving. However, if we are 1143ca48f36fSKeith M Wesolowski * creating this snapshot as part of a receive, this check will be 1144ca48f36fSKeith M Wesolowski * executed atomically with respect to the completion of the receive 1145ca48f36fSKeith M Wesolowski * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this 1146ca48f36fSKeith M Wesolowski * case we ignore this, knowing it will be fixed up for us shortly in 1147ca48f36fSKeith M Wesolowski * dmu_recv_end_sync(). 1148ca48f36fSKeith M Wesolowski */ 1149ca48f36fSKeith M Wesolowski if (!recv && DS_IS_INCONSISTENT(ds)) 1150ca48f36fSKeith M Wesolowski return (SET_ERROR(EBUSY)); 1151ca48f36fSKeith M Wesolowski 1152a2afb611SJerry Jelinek /* 1153a2afb611SJerry Jelinek * Skip the check for temporary snapshots or if we have already checked 1154a2afb611SJerry Jelinek * the counts in dsl_dataset_snapshot_check. This means we really only 1155a2afb611SJerry Jelinek * check the count here when we're receiving a stream. 1156a2afb611SJerry Jelinek */ 1157a2afb611SJerry Jelinek if (cnt != 0 && cr != NULL) { 1158a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1159a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr); 1160a2afb611SJerry Jelinek if (error != 0) 1161a2afb611SJerry Jelinek return (error); 1162a2afb611SJerry Jelinek } 1163a2afb611SJerry Jelinek 11643b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_reserve_space(ds, tx); 11653b2aab18SMatthew Ahrens if (error != 0) 11663b2aab18SMatthew Ahrens return (error); 1167842727c2SChris Kirby 11681d452cf5Sahrens return (0); 11691d452cf5Sahrens } 11701d452cf5Sahrens 11713b2aab18SMatthew Ahrens static int 11723b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx) 1173745cd3c5Smaybee { 11743b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 11753b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 11763b2aab18SMatthew Ahrens nvpair_t *pair; 11773b2aab18SMatthew Ahrens int rv = 0; 11783b2aab18SMatthew Ahrens 1179a2afb611SJerry Jelinek /* 1180a2afb611SJerry Jelinek * Pre-compute how many total new snapshots will be created for each 1181a2afb611SJerry Jelinek * level in the tree and below. This is needed for validating the 1182a2afb611SJerry Jelinek * snapshot limit when either taking a recursive snapshot or when 1183a2afb611SJerry Jelinek * taking multiple snapshots. 1184a2afb611SJerry Jelinek * 1185a2afb611SJerry Jelinek * The problem is that the counts are not actually adjusted when 1186a2afb611SJerry Jelinek * we are checking, only when we finally sync. For a single snapshot, 1187a2afb611SJerry Jelinek * this is easy, the count will increase by 1 at each node up the tree, 1188a2afb611SJerry Jelinek * but its more complicated for the recursive/multiple snapshot case. 1189a2afb611SJerry Jelinek * 1190a2afb611SJerry Jelinek * The dsl_fs_ss_limit_check function does recursively check the count 1191a2afb611SJerry Jelinek * at each level up the tree but since it is validating each snapshot 1192a2afb611SJerry Jelinek * independently we need to be sure that we are validating the complete 1193a2afb611SJerry Jelinek * count for the entire set of snapshots. We do this by rolling up the 1194a2afb611SJerry Jelinek * counts for each component of the name into an nvlist and then 1195a2afb611SJerry Jelinek * checking each of those cases with the aggregated count. 1196a2afb611SJerry Jelinek * 1197a2afb611SJerry Jelinek * This approach properly handles not only the recursive snapshot 1198a2afb611SJerry Jelinek * case (where we get all of those on the ddsa_snaps list) but also 1199a2afb611SJerry Jelinek * the sibling case (e.g. snapshot a/b and a/c so that we will also 1200a2afb611SJerry Jelinek * validate the limit on 'a' using a count of 2). 1201a2afb611SJerry Jelinek * 1202a2afb611SJerry Jelinek * We validate the snapshot names in the third loop and only report 1203a2afb611SJerry Jelinek * name errors once. 1204a2afb611SJerry Jelinek */ 1205a2afb611SJerry Jelinek if (dmu_tx_is_syncing(tx)) { 1206a2afb611SJerry Jelinek nvlist_t *cnt_track = NULL; 1207a2afb611SJerry Jelinek cnt_track = fnvlist_alloc(); 1208a2afb611SJerry Jelinek 1209a2afb611SJerry Jelinek /* Rollup aggregated counts into the cnt_track list */ 1210a2afb611SJerry Jelinek for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 1211a2afb611SJerry Jelinek pair != NULL; 1212a2afb611SJerry Jelinek pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 1213a2afb611SJerry Jelinek char *pdelim; 1214a2afb611SJerry Jelinek uint64_t val; 1215a2afb611SJerry Jelinek char nm[MAXPATHLEN]; 1216a2afb611SJerry Jelinek 1217a2afb611SJerry Jelinek (void) strlcpy(nm, nvpair_name(pair), sizeof (nm)); 1218a2afb611SJerry Jelinek pdelim = strchr(nm, '@'); 1219a2afb611SJerry Jelinek if (pdelim == NULL) 1220a2afb611SJerry Jelinek continue; 1221a2afb611SJerry Jelinek *pdelim = '\0'; 1222a2afb611SJerry Jelinek 1223a2afb611SJerry Jelinek do { 1224a2afb611SJerry Jelinek if (nvlist_lookup_uint64(cnt_track, nm, 1225a2afb611SJerry Jelinek &val) == 0) { 1226a2afb611SJerry Jelinek /* update existing entry */ 1227a2afb611SJerry Jelinek fnvlist_add_uint64(cnt_track, nm, 1228a2afb611SJerry Jelinek val + 1); 1229a2afb611SJerry Jelinek } else { 1230a2afb611SJerry Jelinek /* add to list */ 1231a2afb611SJerry Jelinek fnvlist_add_uint64(cnt_track, nm, 1); 1232a2afb611SJerry Jelinek } 1233a2afb611SJerry Jelinek 1234a2afb611SJerry Jelinek pdelim = strrchr(nm, '/'); 1235a2afb611SJerry Jelinek if (pdelim != NULL) 1236a2afb611SJerry Jelinek *pdelim = '\0'; 1237a2afb611SJerry Jelinek } while (pdelim != NULL); 1238a2afb611SJerry Jelinek } 1239a2afb611SJerry Jelinek 1240a2afb611SJerry Jelinek /* Check aggregated counts at each level */ 1241a2afb611SJerry Jelinek for (pair = nvlist_next_nvpair(cnt_track, NULL); 1242a2afb611SJerry Jelinek pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) { 1243a2afb611SJerry Jelinek int error = 0; 1244a2afb611SJerry Jelinek char *name; 1245a2afb611SJerry Jelinek uint64_t cnt = 0; 1246a2afb611SJerry Jelinek dsl_dataset_t *ds; 1247a2afb611SJerry Jelinek 1248a2afb611SJerry Jelinek name = nvpair_name(pair); 1249a2afb611SJerry Jelinek cnt = fnvpair_value_uint64(pair); 1250a2afb611SJerry Jelinek ASSERT(cnt > 0); 1251a2afb611SJerry Jelinek 1252a2afb611SJerry Jelinek error = dsl_dataset_hold(dp, name, FTAG, &ds); 1253a2afb611SJerry Jelinek if (error == 0) { 1254a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1255a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, 1256a2afb611SJerry Jelinek ddsa->ddsa_cr); 1257a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 1258a2afb611SJerry Jelinek } 1259a2afb611SJerry Jelinek 1260a2afb611SJerry Jelinek if (error != 0) { 1261a2afb611SJerry Jelinek if (ddsa->ddsa_errors != NULL) 1262a2afb611SJerry Jelinek fnvlist_add_int32(ddsa->ddsa_errors, 1263a2afb611SJerry Jelinek name, error); 1264a2afb611SJerry Jelinek rv = error; 1265a2afb611SJerry Jelinek /* only report one error for this check */ 1266a2afb611SJerry Jelinek break; 1267a2afb611SJerry Jelinek } 1268a2afb611SJerry Jelinek } 1269a2afb611SJerry Jelinek nvlist_free(cnt_track); 1270a2afb611SJerry Jelinek } 1271a2afb611SJerry Jelinek 12723b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 12733b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 12743b2aab18SMatthew Ahrens int error = 0; 12753b2aab18SMatthew Ahrens dsl_dataset_t *ds; 12763b2aab18SMatthew Ahrens char *name, *atp; 12779adfa60dSMatthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN]; 12783b2aab18SMatthew Ahrens 12793b2aab18SMatthew Ahrens name = nvpair_name(pair); 12809adfa60dSMatthew Ahrens if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN) 1281be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 12823b2aab18SMatthew Ahrens if (error == 0) { 12833b2aab18SMatthew Ahrens atp = strchr(name, '@'); 12843b2aab18SMatthew Ahrens if (atp == NULL) 1285be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 12863b2aab18SMatthew Ahrens if (error == 0) 12873b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 12883b2aab18SMatthew Ahrens } 12893b2aab18SMatthew Ahrens if (error == 0) 12903b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 12913b2aab18SMatthew Ahrens if (error == 0) { 1292a2afb611SJerry Jelinek /* passing 0/NULL skips dsl_fs_ss_limit_check */ 12933b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_check_impl(ds, 1294a2afb611SJerry Jelinek atp + 1, tx, B_FALSE, 0, NULL); 12953b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12963b2aab18SMatthew Ahrens } 1297745cd3c5Smaybee 12983b2aab18SMatthew Ahrens if (error != 0) { 12993b2aab18SMatthew Ahrens if (ddsa->ddsa_errors != NULL) { 13003b2aab18SMatthew Ahrens fnvlist_add_int32(ddsa->ddsa_errors, 13013b2aab18SMatthew Ahrens name, error); 13023b2aab18SMatthew Ahrens } 13033b2aab18SMatthew Ahrens rv = error; 13043b2aab18SMatthew Ahrens } 13053b2aab18SMatthew Ahrens } 1306a2afb611SJerry Jelinek 13073b2aab18SMatthew Ahrens return (rv); 1308745cd3c5Smaybee } 1309745cd3c5Smaybee 13103b2aab18SMatthew Ahrens void 13113b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname, 13123b2aab18SMatthew Ahrens dmu_tx_t *tx) 1313745cd3c5Smaybee { 13143b2aab18SMatthew Ahrens static zil_header_t zero_zil; 1315745cd3c5Smaybee 13163b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 13173b2aab18SMatthew Ahrens dmu_buf_t *dbuf; 13183b2aab18SMatthew Ahrens dsl_dataset_phys_t *dsphys; 13193b2aab18SMatthew Ahrens uint64_t dsobj, crtxg; 13203b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 13213b2aab18SMatthew Ahrens objset_t *os; 1322745cd3c5Smaybee 13233b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1324c33e334fSMatthew Ahrens 1325c33e334fSMatthew Ahrens /* 13263b2aab18SMatthew Ahrens * If we are on an old pool, the zil must not be active, in which 13273b2aab18SMatthew Ahrens * case it will be zeroed. Usually zil_suspend() accomplishes this. 1328c33e334fSMatthew Ahrens */ 13293b2aab18SMatthew Ahrens ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP || 13303b2aab18SMatthew Ahrens dmu_objset_from_ds(ds, &os) != 0 || 13313b2aab18SMatthew Ahrens bcmp(&os->os_phys->os_zil_header, &zero_zil, 13323b2aab18SMatthew Ahrens sizeof (zero_zil)) == 0); 1333c33e334fSMatthew Ahrens 1334a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx); 1335cde58dbcSMatthew Ahrens 1336cde58dbcSMatthew Ahrens /* 13373b2aab18SMatthew Ahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 1338088f3894Sahrens */ 1339088f3894Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 1340088f3894Sahrens crtxg = 1; 1341088f3894Sahrens else 1342088f3894Sahrens crtxg = tx->tx_txg; 1343088f3894Sahrens 13441649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 13451649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 13463b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1347fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 1348fa9e4066Sahrens dsphys = dbuf->db_data; 1349745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 13501d452cf5Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1351fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 1352fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1353fa9e4066Sahrens sizeof (dsphys->ds_guid)); 1354c1379625SJustin T. Gibbs dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 1355c1379625SJustin T. Gibbs dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 1356fa9e4066Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1357fa9e4066Sahrens dsphys->ds_num_children = 1; 1358fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 1359088f3894Sahrens dsphys->ds_creation_txg = crtxg; 1360c1379625SJustin T. Gibbs dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj; 1361c1379625SJustin T. Gibbs dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes; 1362c1379625SJustin T. Gibbs dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes; 1363c1379625SJustin T. Gibbs dsphys->ds_uncompressed_bytes = 1364c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes; 1365c1379625SJustin T. Gibbs dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags; 1366c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 1367c1379625SJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp; 1368c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 1369ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 1370fa9e4066Sahrens 1371ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 1372ca0cc391SMatthew Ahrens if (ds->ds_feature_inuse[f]) 1373ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(dsobj, f, tx); 1374ca0cc391SMatthew Ahrens } 1375b5152584SMatthew Ahrens 1376c1379625SJustin T. Gibbs ASSERT3U(ds->ds_prev != 0, ==, 1377c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj != 0); 13781d452cf5Sahrens if (ds->ds_prev) { 1379088f3894Sahrens uint64_t next_clones_obj = 1380c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj; 1381c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1382fa9e4066Sahrens ds->ds_object || 1383c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1); 1384c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1385c1379625SJustin T. Gibbs ds->ds_object) { 13861d452cf5Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1387c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==, 1388c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_creation_txg); 1389c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj; 1390088f3894Sahrens } else if (next_clones_obj != 0) { 13913b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(ds->ds_prev, 1392c33e334fSMatthew Ahrens dsphys->ds_next_snap_obj, tx); 13933b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 1394088f3894Sahrens next_clones_obj, dsobj, tx)); 1395fa9e4066Sahrens } 1396fa9e4066Sahrens } 1397fa9e4066Sahrens 1398a9799022Sck /* 1399a9799022Sck * If we have a reference-reservation on this dataset, we will 1400a9799022Sck * need to increase the amount of refreservation being charged 1401a9799022Sck * since our unique space is going to zero. 1402a9799022Sck */ 1403a9799022Sck if (ds->ds_reserved) { 14043f9d6ad7SLin Ling int64_t delta; 14053f9d6ad7SLin Ling ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 1406c1379625SJustin T. Gibbs delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, 1407c1379625SJustin T. Gibbs ds->ds_reserved); 140874e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 14093f9d6ad7SLin Ling delta, 0, 0, tx); 1410a9799022Sck } 1411a9799022Sck 1412fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1413c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj = 1414c1379625SJustin T. Gibbs dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX, 1415c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, tx); 1416cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 1417c1379625SJustin T. Gibbs dsl_deadlist_open(&ds->ds_deadlist, mos, 1418c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj); 1419cde58dbcSMatthew Ahrens dsl_deadlist_add_key(&ds->ds_deadlist, 1420c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, tx); 1421cde58dbcSMatthew Ahrens 1422c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg); 1423c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj; 1424c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg; 1425c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes = 0; 1426a9799022Sck if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1427c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1428fa9e4066Sahrens 1429c1379625SJustin T. Gibbs VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj, 14303b2aab18SMatthew Ahrens snapname, 8, 1, &dsobj, tx)); 1431fa9e4066Sahrens 1432fa9e4066Sahrens if (ds->ds_prev) 14333b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 14343b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1435c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev)); 1436ecd6cf80Smarks 14373f9d6ad7SLin Ling dsl_scan_ds_snapshotted(ds, tx); 1438088f3894Sahrens 143971eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 144071eb0538SChris Kirby 14414445fffbSMatthew Ahrens spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, ""); 1442fa9e4066Sahrens } 1443fa9e4066Sahrens 14443b2aab18SMatthew Ahrens static void 14453b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx) 1446fa9e4066Sahrens { 14473b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 14483b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 14493b2aab18SMatthew Ahrens nvpair_t *pair; 145091ebeef5Sahrens 14513b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 14523b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 14533b2aab18SMatthew Ahrens dsl_dataset_t *ds; 14543b2aab18SMatthew Ahrens char *name, *atp; 14559adfa60dSMatthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN]; 14563b2aab18SMatthew Ahrens 14573b2aab18SMatthew Ahrens name = nvpair_name(pair); 14583b2aab18SMatthew Ahrens atp = strchr(name, '@'); 14593b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 14603b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds)); 14613b2aab18SMatthew Ahrens 14623b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx); 14633b2aab18SMatthew Ahrens if (ddsa->ddsa_props != NULL) { 14643b2aab18SMatthew Ahrens dsl_props_set_sync_impl(ds->ds_prev, 14653b2aab18SMatthew Ahrens ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx); 14663b2aab18SMatthew Ahrens } 14673b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14683b2aab18SMatthew Ahrens } 1469fa9e4066Sahrens } 1470fa9e4066Sahrens 14713b2aab18SMatthew Ahrens /* 14723b2aab18SMatthew Ahrens * The snapshots must all be in the same pool. 14733b2aab18SMatthew Ahrens * All-or-nothing: if there are any failures, nothing will be modified. 14743b2aab18SMatthew Ahrens */ 14753b2aab18SMatthew Ahrens int 14763b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors) 147719b94df9SMatthew Ahrens { 14783b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t ddsa; 14793b2aab18SMatthew Ahrens nvpair_t *pair; 14803b2aab18SMatthew Ahrens boolean_t needsuspend; 14813b2aab18SMatthew Ahrens int error; 14823b2aab18SMatthew Ahrens spa_t *spa; 14833b2aab18SMatthew Ahrens char *firstname; 14843b2aab18SMatthew Ahrens nvlist_t *suspended = NULL; 148519b94df9SMatthew Ahrens 14863b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, NULL); 14873b2aab18SMatthew Ahrens if (pair == NULL) 14883b2aab18SMatthew Ahrens return (0); 14893b2aab18SMatthew Ahrens firstname = nvpair_name(pair); 14903b2aab18SMatthew Ahrens 14913b2aab18SMatthew Ahrens error = spa_open(firstname, &spa, FTAG); 14923b2aab18SMatthew Ahrens if (error != 0) 14933b2aab18SMatthew Ahrens return (error); 14943b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 14953b2aab18SMatthew Ahrens spa_close(spa, FTAG); 14963b2aab18SMatthew Ahrens 14973b2aab18SMatthew Ahrens if (needsuspend) { 14983b2aab18SMatthew Ahrens suspended = fnvlist_alloc(); 14993b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 15003b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 15019adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN]; 15023b2aab18SMatthew Ahrens char *snapname = nvpair_name(pair); 15033b2aab18SMatthew Ahrens char *atp; 15043b2aab18SMatthew Ahrens void *cookie; 15053b2aab18SMatthew Ahrens 15063b2aab18SMatthew Ahrens atp = strchr(snapname, '@'); 15073b2aab18SMatthew Ahrens if (atp == NULL) { 1508be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 15093b2aab18SMatthew Ahrens break; 15103b2aab18SMatthew Ahrens } 15113b2aab18SMatthew Ahrens (void) strlcpy(fsname, snapname, atp - snapname + 1); 15123b2aab18SMatthew Ahrens 15133b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 15143b2aab18SMatthew Ahrens if (error != 0) 15153b2aab18SMatthew Ahrens break; 15163b2aab18SMatthew Ahrens fnvlist_add_uint64(suspended, fsname, 15173b2aab18SMatthew Ahrens (uintptr_t)cookie); 15183b2aab18SMatthew Ahrens } 15193b2aab18SMatthew Ahrens } 15203b2aab18SMatthew Ahrens 15213b2aab18SMatthew Ahrens ddsa.ddsa_snaps = snaps; 15223b2aab18SMatthew Ahrens ddsa.ddsa_props = props; 15233b2aab18SMatthew Ahrens ddsa.ddsa_errors = errors; 1524a2afb611SJerry Jelinek ddsa.ddsa_cr = CRED(); 15253b2aab18SMatthew Ahrens 15263b2aab18SMatthew Ahrens if (error == 0) { 15273b2aab18SMatthew Ahrens error = dsl_sync_task(firstname, dsl_dataset_snapshot_check, 15283b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync, &ddsa, 15297d46dc6cSMatthew Ahrens fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL); 15303b2aab18SMatthew Ahrens } 15313b2aab18SMatthew Ahrens 15323b2aab18SMatthew Ahrens if (suspended != NULL) { 15333b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL; 15343b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(suspended, pair)) { 15353b2aab18SMatthew Ahrens zil_resume((void *)(uintptr_t) 15363b2aab18SMatthew Ahrens fnvpair_value_uint64(pair)); 15373b2aab18SMatthew Ahrens } 15383b2aab18SMatthew Ahrens fnvlist_free(suspended); 15393b2aab18SMatthew Ahrens } 15403b2aab18SMatthew Ahrens 15413b2aab18SMatthew Ahrens return (error); 15423b2aab18SMatthew Ahrens } 15433b2aab18SMatthew Ahrens 15443b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg { 15453b2aab18SMatthew Ahrens const char *ddsta_fsname; 15463b2aab18SMatthew Ahrens const char *ddsta_snapname; 15473b2aab18SMatthew Ahrens minor_t ddsta_cleanup_minor; 15483b2aab18SMatthew Ahrens const char *ddsta_htag; 15493b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t; 15503b2aab18SMatthew Ahrens 15513b2aab18SMatthew Ahrens static int 15523b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx) 15533b2aab18SMatthew Ahrens { 15543b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 15553b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15563b2aab18SMatthew Ahrens dsl_dataset_t *ds; 15573b2aab18SMatthew Ahrens int error; 15583b2aab18SMatthew Ahrens 15593b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds); 15603b2aab18SMatthew Ahrens if (error != 0) 15613b2aab18SMatthew Ahrens return (error); 15623b2aab18SMatthew Ahrens 1563a2afb611SJerry Jelinek /* NULL cred means no limit check for tmp snapshot */ 1564ca48f36fSKeith M Wesolowski error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, 1565a2afb611SJerry Jelinek tx, B_FALSE, 0, NULL); 15663b2aab18SMatthew Ahrens if (error != 0) { 15673b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15683b2aab18SMatthew Ahrens return (error); 15693b2aab18SMatthew Ahrens } 15703b2aab18SMatthew Ahrens 15713b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) { 15723b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1573be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 15743b2aab18SMatthew Ahrens } 15753b2aab18SMatthew Ahrens error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag, 15763b2aab18SMatthew Ahrens B_TRUE, tx); 15773b2aab18SMatthew Ahrens if (error != 0) { 15783b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15793b2aab18SMatthew Ahrens return (error); 15803b2aab18SMatthew Ahrens } 15813b2aab18SMatthew Ahrens 15823b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15833b2aab18SMatthew Ahrens return (0); 15843b2aab18SMatthew Ahrens } 15853b2aab18SMatthew Ahrens 15863b2aab18SMatthew Ahrens static void 15873b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx) 15883b2aab18SMatthew Ahrens { 15893b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 15903b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15913b2aab18SMatthew Ahrens dsl_dataset_t *ds; 15923b2aab18SMatthew Ahrens 15933b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds)); 15943b2aab18SMatthew Ahrens 15953b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx); 15963b2aab18SMatthew Ahrens dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag, 15973b2aab18SMatthew Ahrens ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx); 15983b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx); 15993b2aab18SMatthew Ahrens 16003b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 16013b2aab18SMatthew Ahrens } 16023b2aab18SMatthew Ahrens 16033b2aab18SMatthew Ahrens int 16043b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname, 16053b2aab18SMatthew Ahrens minor_t cleanup_minor, const char *htag) 16063b2aab18SMatthew Ahrens { 16073b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t ddsta; 16083b2aab18SMatthew Ahrens int error; 16093b2aab18SMatthew Ahrens spa_t *spa; 16103b2aab18SMatthew Ahrens boolean_t needsuspend; 16113b2aab18SMatthew Ahrens void *cookie; 16123b2aab18SMatthew Ahrens 16133b2aab18SMatthew Ahrens ddsta.ddsta_fsname = fsname; 16143b2aab18SMatthew Ahrens ddsta.ddsta_snapname = snapname; 16153b2aab18SMatthew Ahrens ddsta.ddsta_cleanup_minor = cleanup_minor; 16163b2aab18SMatthew Ahrens ddsta.ddsta_htag = htag; 16173b2aab18SMatthew Ahrens 16183b2aab18SMatthew Ahrens error = spa_open(fsname, &spa, FTAG); 16193b2aab18SMatthew Ahrens if (error != 0) 16203b2aab18SMatthew Ahrens return (error); 16213b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 16223b2aab18SMatthew Ahrens spa_close(spa, FTAG); 16233b2aab18SMatthew Ahrens 16243b2aab18SMatthew Ahrens if (needsuspend) { 16253b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 16263b2aab18SMatthew Ahrens if (error != 0) 16273b2aab18SMatthew Ahrens return (error); 16283b2aab18SMatthew Ahrens } 16293b2aab18SMatthew Ahrens 16303b2aab18SMatthew Ahrens error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check, 16317d46dc6cSMatthew Ahrens dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED); 16323b2aab18SMatthew Ahrens 16333b2aab18SMatthew Ahrens if (needsuspend) 16343b2aab18SMatthew Ahrens zil_resume(cookie); 16353b2aab18SMatthew Ahrens return (error); 16363b2aab18SMatthew Ahrens } 16373b2aab18SMatthew Ahrens 16383b2aab18SMatthew Ahrens 16393b2aab18SMatthew Ahrens void 16403b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 16413b2aab18SMatthew Ahrens { 16423b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 16433b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 1644c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0); 16453b2aab18SMatthew Ahrens 16463b2aab18SMatthew Ahrens /* 16473b2aab18SMatthew Ahrens * in case we had to change ds_fsid_guid when we opened it, 16483b2aab18SMatthew Ahrens * sync it out now. 16493b2aab18SMatthew Ahrens */ 16503b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1651c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid; 16523b2aab18SMatthew Ahrens 16539c3fd121SMatthew Ahrens if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) { 16549c3fd121SMatthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 16559c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1, 16569c3fd121SMatthew Ahrens &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx)); 16579c3fd121SMatthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 16589c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1, 16599c3fd121SMatthew Ahrens &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx)); 16609c3fd121SMatthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 16619c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1, 16629c3fd121SMatthew Ahrens &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx)); 16639c3fd121SMatthew Ahrens ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0; 16649c3fd121SMatthew Ahrens ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0; 16659c3fd121SMatthew Ahrens ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0; 16669c3fd121SMatthew Ahrens } 16679c3fd121SMatthew Ahrens 16683b2aab18SMatthew Ahrens dmu_objset_sync(ds->ds_objset, zio, tx); 1669b5152584SMatthew Ahrens 1670ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 1671ca0cc391SMatthew Ahrens if (ds->ds_feature_activation_needed[f]) { 1672ca0cc391SMatthew Ahrens if (ds->ds_feature_inuse[f]) 1673ca0cc391SMatthew Ahrens continue; 1674ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(ds->ds_object, f, tx); 1675ca0cc391SMatthew Ahrens ds->ds_feature_inuse[f] = B_TRUE; 1676ca0cc391SMatthew Ahrens } 1677b5152584SMatthew Ahrens } 16783b2aab18SMatthew Ahrens } 16793b2aab18SMatthew Ahrens 16803b2aab18SMatthew Ahrens static void 16813b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv) 16823b2aab18SMatthew Ahrens { 16833b2aab18SMatthew Ahrens uint64_t count = 0; 16843b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 16853b2aab18SMatthew Ahrens zap_cursor_t zc; 16863b2aab18SMatthew Ahrens zap_attribute_t za; 16873b2aab18SMatthew Ahrens nvlist_t *propval = fnvlist_alloc(); 16883b2aab18SMatthew Ahrens nvlist_t *val = fnvlist_alloc(); 16893b2aab18SMatthew Ahrens 16903b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 169119b94df9SMatthew Ahrens 169219b94df9SMatthew Ahrens /* 16933b2aab18SMatthew Ahrens * There may be missing entries in ds_next_clones_obj 169419b94df9SMatthew Ahrens * due to a bug in a previous version of the code. 169519b94df9SMatthew Ahrens * Only trust it if it has the right number of entries. 169619b94df9SMatthew Ahrens */ 1697c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) { 1698c1379625SJustin T. Gibbs VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 169919b94df9SMatthew Ahrens &count)); 170019b94df9SMatthew Ahrens } 1701c1379625SJustin T. Gibbs if (count != dsl_dataset_phys(ds)->ds_num_children - 1) 170219b94df9SMatthew Ahrens goto fail; 1703c1379625SJustin T. Gibbs for (zap_cursor_init(&zc, mos, 1704c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj); 170519b94df9SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 170619b94df9SMatthew Ahrens zap_cursor_advance(&zc)) { 170719b94df9SMatthew Ahrens dsl_dataset_t *clone; 17089adfa60dSMatthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN]; 17093b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 17103b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &clone)); 171119b94df9SMatthew Ahrens dsl_dir_name(clone->ds_dir, buf); 17123b2aab18SMatthew Ahrens fnvlist_add_boolean(val, buf); 171319b94df9SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 171419b94df9SMatthew Ahrens } 171519b94df9SMatthew Ahrens zap_cursor_fini(&zc); 17163b2aab18SMatthew Ahrens fnvlist_add_nvlist(propval, ZPROP_VALUE, val); 17173b2aab18SMatthew Ahrens fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval); 171819b94df9SMatthew Ahrens fail: 171919b94df9SMatthew Ahrens nvlist_free(val); 172019b94df9SMatthew Ahrens nvlist_free(propval); 172119b94df9SMatthew Ahrens } 172219b94df9SMatthew Ahrens 17239c3fd121SMatthew Ahrens static void 17249c3fd121SMatthew Ahrens get_receive_resume_stats(dsl_dataset_t *ds, nvlist_t *nv) 17259c3fd121SMatthew Ahrens { 17269c3fd121SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 17279c3fd121SMatthew Ahrens 17289c3fd121SMatthew Ahrens if (dsl_dataset_has_resume_receive_state(ds)) { 17299c3fd121SMatthew Ahrens char *str; 17309c3fd121SMatthew Ahrens void *packed; 17319c3fd121SMatthew Ahrens uint8_t *compressed; 17329c3fd121SMatthew Ahrens uint64_t val; 17339c3fd121SMatthew Ahrens nvlist_t *token_nv = fnvlist_alloc(); 17349c3fd121SMatthew Ahrens size_t packed_size, compressed_size; 17359c3fd121SMatthew Ahrens 17369c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17379c3fd121SMatthew Ahrens DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) { 17389c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "fromguid", val); 17399c3fd121SMatthew Ahrens } 17409c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17419c3fd121SMatthew Ahrens DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) { 17429c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "object", val); 17439c3fd121SMatthew Ahrens } 17449c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17459c3fd121SMatthew Ahrens DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) { 17469c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "offset", val); 17479c3fd121SMatthew Ahrens } 17489c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17499c3fd121SMatthew Ahrens DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) { 17509c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "bytes", val); 17519c3fd121SMatthew Ahrens } 17529c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17539c3fd121SMatthew Ahrens DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) { 17549c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "toguid", val); 17559c3fd121SMatthew Ahrens } 17569c3fd121SMatthew Ahrens char buf[256]; 17579c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17589c3fd121SMatthew Ahrens DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) { 17599c3fd121SMatthew Ahrens fnvlist_add_string(token_nv, "toname", buf); 17609c3fd121SMatthew Ahrens } 17619c3fd121SMatthew Ahrens if (zap_contains(dp->dp_meta_objset, ds->ds_object, 17629c3fd121SMatthew Ahrens DS_FIELD_RESUME_EMBEDOK) == 0) { 17639c3fd121SMatthew Ahrens fnvlist_add_boolean(token_nv, "embedok"); 17649c3fd121SMatthew Ahrens } 17659c3fd121SMatthew Ahrens packed = fnvlist_pack(token_nv, &packed_size); 17669c3fd121SMatthew Ahrens fnvlist_free(token_nv); 17679c3fd121SMatthew Ahrens compressed = kmem_alloc(packed_size, KM_SLEEP); 17689c3fd121SMatthew Ahrens 17699c3fd121SMatthew Ahrens compressed_size = gzip_compress(packed, compressed, 17709c3fd121SMatthew Ahrens packed_size, packed_size, 6); 17719c3fd121SMatthew Ahrens 17729c3fd121SMatthew Ahrens zio_cksum_t cksum; 17739c3fd121SMatthew Ahrens fletcher_4_native(compressed, compressed_size, NULL, &cksum); 17749c3fd121SMatthew Ahrens 17759c3fd121SMatthew Ahrens str = kmem_alloc(compressed_size * 2 + 1, KM_SLEEP); 17769c3fd121SMatthew Ahrens for (int i = 0; i < compressed_size; i++) { 17779c3fd121SMatthew Ahrens (void) sprintf(str + i * 2, "%02x", compressed[i]); 17789c3fd121SMatthew Ahrens } 17799c3fd121SMatthew Ahrens str[compressed_size * 2] = '\0'; 17809c3fd121SMatthew Ahrens char *propval = kmem_asprintf("%u-%llx-%llx-%s", 17819c3fd121SMatthew Ahrens ZFS_SEND_RESUME_TOKEN_VERSION, 17829c3fd121SMatthew Ahrens (longlong_t)cksum.zc_word[0], 17839c3fd121SMatthew Ahrens (longlong_t)packed_size, str); 17849c3fd121SMatthew Ahrens dsl_prop_nvlist_add_string(nv, 17859c3fd121SMatthew Ahrens ZFS_PROP_RECEIVE_RESUME_TOKEN, propval); 17869c3fd121SMatthew Ahrens kmem_free(packed, packed_size); 17879c3fd121SMatthew Ahrens kmem_free(str, compressed_size * 2 + 1); 17889c3fd121SMatthew Ahrens kmem_free(compressed, packed_size); 17899c3fd121SMatthew Ahrens strfree(propval); 17909c3fd121SMatthew Ahrens } 17919c3fd121SMatthew Ahrens } 17929c3fd121SMatthew Ahrens 1793fa9e4066Sahrens void 1794a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1795fa9e4066Sahrens { 17963b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1797187d6ac0SMatt Ahrens uint64_t refd, avail, uobjs, aobjs, ratio; 1798a9799022Sck 17993b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 18003b2aab18SMatthew Ahrens 1801c1379625SJustin T. Gibbs ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 : 1802c1379625SJustin T. Gibbs (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 / 1803c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes); 18044445fffbSMatthew Ahrens 18054445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio); 180677372cb0SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED, 1807c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes); 18084445fffbSMatthew Ahrens 1809bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 18104445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio); 18114445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 1812c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 18134445fffbSMatthew Ahrens get_clones_stat(ds, nv); 18144445fffbSMatthew Ahrens } else { 1815b461c746SMatthew Ahrens if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) { 18169adfa60dSMatthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN]; 1817b461c746SMatthew Ahrens dsl_dataset_name(ds->ds_prev, buf); 1818b461c746SMatthew Ahrens dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf); 1819b461c746SMatthew Ahrens } 1820b461c746SMatthew Ahrens 18214445fffbSMatthew Ahrens dsl_dir_stats(ds->ds_dir, nv); 18224445fffbSMatthew Ahrens } 1823fa9e4066Sahrens 1824a9799022Sck dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 1825a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 1826a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 1827a9799022Sck 1828a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 1829c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_time); 1830a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 1831c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_txg); 1832a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 1833a9799022Sck ds->ds_quota); 1834a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 1835a9799022Sck ds->ds_reserved); 1836c5904d13Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 1837c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_guid); 18381d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 1839c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 18401d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 18411d713200SEric Schrock ds->ds_object); 184292241e0bSTom Erickson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 184392241e0bSTom Erickson ds->ds_userrefs); 1844842727c2SChris Kirby dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 1845842727c2SChris Kirby DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 1846fa9e4066Sahrens 1847c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 184819b94df9SMatthew Ahrens uint64_t written, comp, uncomp; 184919b94df9SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 185019b94df9SMatthew Ahrens dsl_dataset_t *prev; 185119b94df9SMatthew Ahrens 185219b94df9SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, 1853c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 185419b94df9SMatthew Ahrens if (err == 0) { 185519b94df9SMatthew Ahrens err = dsl_dataset_space_written(prev, ds, &written, 185619b94df9SMatthew Ahrens &comp, &uncomp); 185719b94df9SMatthew Ahrens dsl_dataset_rele(prev, FTAG); 185819b94df9SMatthew Ahrens if (err == 0) { 185919b94df9SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN, 186019b94df9SMatthew Ahrens written); 186119b94df9SMatthew Ahrens } 186219b94df9SMatthew Ahrens } 186319b94df9SMatthew Ahrens } 18649c3fd121SMatthew Ahrens 18659c3fd121SMatthew Ahrens if (!dsl_dataset_is_snapshot(ds)) { 18669c3fd121SMatthew Ahrens /* 18679c3fd121SMatthew Ahrens * A failed "newfs" (e.g. full) resumable receive leaves 18689c3fd121SMatthew Ahrens * the stats set on this dataset. Check here for the prop. 18699c3fd121SMatthew Ahrens */ 18709c3fd121SMatthew Ahrens get_receive_resume_stats(ds, nv); 18719c3fd121SMatthew Ahrens 18729c3fd121SMatthew Ahrens /* 18739c3fd121SMatthew Ahrens * A failed incremental resumable receive leaves the 18749c3fd121SMatthew Ahrens * stats set on our child named "%recv". Check the child 18759c3fd121SMatthew Ahrens * for the prop. 18769c3fd121SMatthew Ahrens */ 18779adfa60dSMatthew Ahrens /* 6 extra bytes for /%recv */ 18789adfa60dSMatthew Ahrens char recvname[ZFS_MAX_DATASET_NAME_LEN + 6]; 18799c3fd121SMatthew Ahrens dsl_dataset_t *recv_ds; 18809c3fd121SMatthew Ahrens dsl_dataset_name(ds, recvname); 18819adfa60dSMatthew Ahrens if (strlcat(recvname, "/", sizeof (recvname)) < 18829adfa60dSMatthew Ahrens sizeof (recvname) && 18839adfa60dSMatthew Ahrens strlcat(recvname, recv_clone_name, sizeof (recvname)) < 18849adfa60dSMatthew Ahrens sizeof (recvname) && 18859adfa60dSMatthew Ahrens dsl_dataset_hold(dp, recvname, FTAG, &recv_ds) == 0) { 18869c3fd121SMatthew Ahrens get_receive_resume_stats(recv_ds, nv); 18879c3fd121SMatthew Ahrens dsl_dataset_rele(recv_ds, FTAG); 18889c3fd121SMatthew Ahrens } 18899c3fd121SMatthew Ahrens } 1890fa9e4066Sahrens } 1891fa9e4066Sahrens 1892a2eea2e1Sahrens void 1893a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1894a2eea2e1Sahrens { 18953b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 18963b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 18973b2aab18SMatthew Ahrens 1898c1379625SJustin T. Gibbs stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg; 1899c1379625SJustin T. Gibbs stat->dds_inconsistent = 1900c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT; 1901c1379625SJustin T. Gibbs stat->dds_guid = dsl_dataset_phys(ds)->ds_guid; 19024445fffbSMatthew Ahrens stat->dds_origin[0] = '\0'; 1903bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 1904a2eea2e1Sahrens stat->dds_is_snapshot = B_TRUE; 1905c1379625SJustin T. Gibbs stat->dds_num_clones = 1906c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_num_children - 1; 1907ebedde84SEric Taylor } else { 1908ebedde84SEric Taylor stat->dds_is_snapshot = B_FALSE; 1909ebedde84SEric Taylor stat->dds_num_clones = 0; 1910a2eea2e1Sahrens 19114445fffbSMatthew Ahrens if (dsl_dir_is_clone(ds->ds_dir)) { 19124445fffbSMatthew Ahrens dsl_dataset_t *ods; 1913a2eea2e1Sahrens 19143b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1915c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_origin_obj, 1916c1379625SJustin T. Gibbs FTAG, &ods)); 19174445fffbSMatthew Ahrens dsl_dataset_name(ods, stat->dds_origin); 19183b2aab18SMatthew Ahrens dsl_dataset_rele(ods, FTAG); 19194445fffbSMatthew Ahrens } 1920a2eea2e1Sahrens } 1921a2eea2e1Sahrens } 1922a2eea2e1Sahrens 1923a2eea2e1Sahrens uint64_t 1924a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 1925a2eea2e1Sahrens { 192691ebeef5Sahrens return (ds->ds_fsid_guid); 1927a2eea2e1Sahrens } 1928a2eea2e1Sahrens 1929a2eea2e1Sahrens void 1930a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds, 1931a2eea2e1Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 1932a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1933fa9e4066Sahrens { 1934c1379625SJustin T. Gibbs *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes; 1935a2eea2e1Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 1936c1379625SJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) 1937c1379625SJustin T. Gibbs *availbytesp += 1938c1379625SJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes; 1939a9799022Sck if (ds->ds_quota != 0) { 1940a9799022Sck /* 1941a9799022Sck * Adjust available bytes according to refquota 1942a9799022Sck */ 1943a9799022Sck if (*refdbytesp < ds->ds_quota) 1944a9799022Sck *availbytesp = MIN(*availbytesp, 1945a9799022Sck ds->ds_quota - *refdbytesp); 1946a9799022Sck else 1947a9799022Sck *availbytesp = 0; 1948a9799022Sck } 1949c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 1950c1379625SJustin T. Gibbs *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp); 1951c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 1952a2eea2e1Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1953fa9e4066Sahrens } 1954fa9e4066Sahrens 1955f18faf3fSek boolean_t 195634f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap) 1957f18faf3fSek { 1958f18faf3fSek dsl_pool_t *dp = ds->ds_dir->dd_pool; 1959c166b69dSPaul Dagnelie uint64_t birth; 1960f18faf3fSek 19613b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 196234f2f8cfSMatthew Ahrens if (snap == NULL) 1963f18faf3fSek return (B_FALSE); 1964c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 1965c166b69dSPaul Dagnelie birth = dsl_dataset_get_blkptr(ds)->blk_birth; 1966c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 1967c166b69dSPaul Dagnelie if (birth > dsl_dataset_phys(snap)->ds_creation_txg) { 196834f2f8cfSMatthew Ahrens objset_t *os, *os_snap; 19696e0cbcaaSMatthew Ahrens /* 19706e0cbcaaSMatthew Ahrens * It may be that only the ZIL differs, because it was 19716e0cbcaaSMatthew Ahrens * reset in the head. Don't count that as being 19726e0cbcaaSMatthew Ahrens * modified. 19736e0cbcaaSMatthew Ahrens */ 19746e0cbcaaSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) != 0) 19756e0cbcaaSMatthew Ahrens return (B_TRUE); 197634f2f8cfSMatthew Ahrens if (dmu_objset_from_ds(snap, &os_snap) != 0) 19776e0cbcaaSMatthew Ahrens return (B_TRUE); 19786e0cbcaaSMatthew Ahrens return (bcmp(&os->os_phys->os_meta_dnode, 197934f2f8cfSMatthew Ahrens &os_snap->os_phys->os_meta_dnode, 19806e0cbcaaSMatthew Ahrens sizeof (os->os_phys->os_meta_dnode)) != 0); 19816e0cbcaaSMatthew Ahrens } 1982f18faf3fSek return (B_FALSE); 1983f18faf3fSek } 1984f18faf3fSek 19853b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg { 19863b2aab18SMatthew Ahrens const char *ddrsa_fsname; 19873b2aab18SMatthew Ahrens const char *ddrsa_oldsnapname; 19883b2aab18SMatthew Ahrens const char *ddrsa_newsnapname; 19893b2aab18SMatthew Ahrens boolean_t ddrsa_recursive; 19903b2aab18SMatthew Ahrens dmu_tx_t *ddrsa_tx; 19913b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t; 19923b2aab18SMatthew Ahrens 19931d452cf5Sahrens /* ARGSUSED */ 1994fa9e4066Sahrens static int 19953b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp, 19963b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1997fa9e4066Sahrens { 19983b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 19993b2aab18SMatthew Ahrens int error; 2000fa9e4066Sahrens uint64_t val; 2001fa9e4066Sahrens 20023b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 20033b2aab18SMatthew Ahrens if (error != 0) { 20043b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 20053b2aab18SMatthew Ahrens return (error == ENOENT ? 0 : error); 20063b2aab18SMatthew Ahrens } 20071d452cf5Sahrens 20083b2aab18SMatthew Ahrens /* new name should not exist */ 20093b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val); 20103b2aab18SMatthew Ahrens if (error == 0) 2011be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 20123b2aab18SMatthew Ahrens else if (error == ENOENT) 20133b2aab18SMatthew Ahrens error = 0; 2014cdf5b4caSmmusante 2015cdf5b4caSmmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 20163b2aab18SMatthew Ahrens if (dsl_dir_namelen(hds->ds_dir) + 1 + 20179adfa60dSMatthew Ahrens strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN) 2018be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 2019cdf5b4caSmmusante 20203b2aab18SMatthew Ahrens return (error); 20211d452cf5Sahrens } 2022fa9e4066Sahrens 20233b2aab18SMatthew Ahrens static int 20243b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx) 20251d452cf5Sahrens { 20263b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 20273b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 20281d452cf5Sahrens dsl_dataset_t *hds; 20293b2aab18SMatthew Ahrens int error; 2030fa9e4066Sahrens 20313b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds); 20323b2aab18SMatthew Ahrens if (error != 0) 20333b2aab18SMatthew Ahrens return (error); 2034fa9e4066Sahrens 20353b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 20363b2aab18SMatthew Ahrens error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 20373b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl, ddrsa, 20383b2aab18SMatthew Ahrens DS_FIND_CHILDREN); 20393b2aab18SMatthew Ahrens } else { 20403b2aab18SMatthew Ahrens error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa); 20413b2aab18SMatthew Ahrens } 2042745cd3c5Smaybee dsl_dataset_rele(hds, FTAG); 20433b2aab18SMatthew Ahrens return (error); 2044fa9e4066Sahrens } 2045fa9e4066Sahrens 2046cdf5b4caSmmusante static int 20473b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp, 20483b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 2049cdf5b4caSmmusante { 20503b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 20513b2aab18SMatthew Ahrens dsl_dataset_t *ds; 20523b2aab18SMatthew Ahrens uint64_t val; 20533b2aab18SMatthew Ahrens dmu_tx_t *tx = ddrsa->ddrsa_tx; 20543b2aab18SMatthew Ahrens int error; 2055ecd6cf80Smarks 20563b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 20573b2aab18SMatthew Ahrens ASSERT(error == 0 || error == ENOENT); 20583b2aab18SMatthew Ahrens if (error == ENOENT) { 20593b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 20603b2aab18SMatthew Ahrens return (0); 2061ecd6cf80Smarks } 2062ecd6cf80Smarks 20633b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds)); 20643b2aab18SMatthew Ahrens 20653b2aab18SMatthew Ahrens /* log before we change the name */ 20663b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "rename", tx, 20673b2aab18SMatthew Ahrens "-> @%s", ddrsa->ddrsa_newsnapname); 2068cdf5b4caSmmusante 2069a2afb611SJerry Jelinek VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx, 2070a2afb611SJerry Jelinek B_FALSE)); 20713b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 20723b2aab18SMatthew Ahrens (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname); 20733b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2074c1379625SJustin T. Gibbs VERIFY0(zap_add(dp->dp_meta_objset, 2075c1379625SJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj, 20763b2aab18SMatthew Ahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx)); 2077cdf5b4caSmmusante 20783b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2079cdf5b4caSmmusante return (0); 2080cdf5b4caSmmusante } 2081cdf5b4caSmmusante 20823b2aab18SMatthew Ahrens static void 20833b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx) 2084cdf5b4caSmmusante { 20853b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 20863b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 20873b2aab18SMatthew Ahrens dsl_dataset_t *hds; 2088cdf5b4caSmmusante 20893b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds)); 20903b2aab18SMatthew Ahrens ddrsa->ddrsa_tx = tx; 20913b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 20923b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 20933b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl, ddrsa, 20943b2aab18SMatthew Ahrens DS_FIND_CHILDREN)); 20953b2aab18SMatthew Ahrens } else { 20963b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa)); 2097cdf5b4caSmmusante } 20983b2aab18SMatthew Ahrens dsl_dataset_rele(hds, FTAG); 2099cdf5b4caSmmusante } 2100cdf5b4caSmmusante 21013b2aab18SMatthew Ahrens int 21023b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname, 21033b2aab18SMatthew Ahrens const char *oldsnapname, const char *newsnapname, boolean_t recursive) 21043a5a36beSmmusante { 21053b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t ddrsa; 21063a5a36beSmmusante 21073b2aab18SMatthew Ahrens ddrsa.ddrsa_fsname = fsname; 21083b2aab18SMatthew Ahrens ddrsa.ddrsa_oldsnapname = oldsnapname; 21093b2aab18SMatthew Ahrens ddrsa.ddrsa_newsnapname = newsnapname; 21103b2aab18SMatthew Ahrens ddrsa.ddrsa_recursive = recursive; 21113a5a36beSmmusante 21123b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check, 21137d46dc6cSMatthew Ahrens dsl_dataset_rename_snapshot_sync, &ddrsa, 21147d46dc6cSMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED)); 21153a5a36beSmmusante } 21163a5a36beSmmusante 211791948b51SKeith M Wesolowski /* 211891948b51SKeith M Wesolowski * If we're doing an ownership handoff, we need to make sure that there is 211991948b51SKeith M Wesolowski * only one long hold on the dataset. We're not allowed to change anything here 212091948b51SKeith M Wesolowski * so we don't permanently release the long hold or regular hold here. We want 212191948b51SKeith M Wesolowski * to do this only when syncing to avoid the dataset unexpectedly going away 212291948b51SKeith M Wesolowski * when we release the long hold. 212391948b51SKeith M Wesolowski */ 212491948b51SKeith M Wesolowski static int 212591948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx) 212691948b51SKeith M Wesolowski { 212791948b51SKeith M Wesolowski boolean_t held; 212891948b51SKeith M Wesolowski 212991948b51SKeith M Wesolowski if (!dmu_tx_is_syncing(tx)) 213091948b51SKeith M Wesolowski return (0); 213191948b51SKeith M Wesolowski 213291948b51SKeith M Wesolowski if (owner != NULL) { 213391948b51SKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, owner); 213491948b51SKeith M Wesolowski dsl_dataset_long_rele(ds, owner); 213591948b51SKeith M Wesolowski } 213691948b51SKeith M Wesolowski 213791948b51SKeith M Wesolowski held = dsl_dataset_long_held(ds); 213891948b51SKeith M Wesolowski 213991948b51SKeith M Wesolowski if (owner != NULL) 214091948b51SKeith M Wesolowski dsl_dataset_long_hold(ds, owner); 214191948b51SKeith M Wesolowski 214291948b51SKeith M Wesolowski if (held) 214391948b51SKeith M Wesolowski return (SET_ERROR(EBUSY)); 214491948b51SKeith M Wesolowski 214591948b51SKeith M Wesolowski return (0); 214691948b51SKeith M Wesolowski } 214791948b51SKeith M Wesolowski 214891948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg { 214991948b51SKeith M Wesolowski const char *ddra_fsname; 215091948b51SKeith M Wesolowski void *ddra_owner; 2151a7027df1SMatthew Ahrens nvlist_t *ddra_result; 215291948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t; 215391948b51SKeith M Wesolowski 21543b2aab18SMatthew Ahrens static int 21553b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx) 2156fa9e4066Sahrens { 215791948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 21583b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 21591d452cf5Sahrens dsl_dataset_t *ds; 21603b2aab18SMatthew Ahrens int64_t unused_refres_delta; 21613b2aab18SMatthew Ahrens int error; 2162fa9e4066Sahrens 216391948b51SKeith M Wesolowski error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds); 21643b2aab18SMatthew Ahrens if (error != 0) 21653b2aab18SMatthew Ahrens return (error); 2166370c1af0SSanjeev Bagewadi 21673b2aab18SMatthew Ahrens /* must not be a snapshot */ 2168bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 21693b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2170be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 21713b2aab18SMatthew Ahrens } 21723a5a36beSmmusante 21733b2aab18SMatthew Ahrens /* must have a most recent snapshot */ 2174c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) { 21753b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2176be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 21773b2aab18SMatthew Ahrens } 21783a5a36beSmmusante 217978f17100SMatthew Ahrens /* must not have any bookmarks after the most recent snapshot */ 218078f17100SMatthew Ahrens nvlist_t *proprequest = fnvlist_alloc(); 218178f17100SMatthew Ahrens fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG)); 218278f17100SMatthew Ahrens nvlist_t *bookmarks = fnvlist_alloc(); 218378f17100SMatthew Ahrens error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks); 218478f17100SMatthew Ahrens fnvlist_free(proprequest); 218578f17100SMatthew Ahrens if (error != 0) 218678f17100SMatthew Ahrens return (error); 218778f17100SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL); 218878f17100SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) { 218978f17100SMatthew Ahrens nvlist_t *valuenv = 219078f17100SMatthew Ahrens fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair), 219178f17100SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CREATETXG)); 219278f17100SMatthew Ahrens uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value"); 2193c1379625SJustin T. Gibbs if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) { 219478f17100SMatthew Ahrens fnvlist_free(bookmarks); 219578f17100SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 219678f17100SMatthew Ahrens return (SET_ERROR(EEXIST)); 219778f17100SMatthew Ahrens } 219878f17100SMatthew Ahrens } 219978f17100SMatthew Ahrens fnvlist_free(bookmarks); 220078f17100SMatthew Ahrens 220191948b51SKeith M Wesolowski error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx); 220291948b51SKeith M Wesolowski if (error != 0) { 22033b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 220491948b51SKeith M Wesolowski return (error); 22053b2aab18SMatthew Ahrens } 22063b2aab18SMatthew Ahrens 22073b2aab18SMatthew Ahrens /* 22083b2aab18SMatthew Ahrens * Check if the snap we are rolling back to uses more than 22093b2aab18SMatthew Ahrens * the refquota. 22103b2aab18SMatthew Ahrens */ 22113b2aab18SMatthew Ahrens if (ds->ds_quota != 0 && 2212c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) { 22133b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2214be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 2215fa9e4066Sahrens } 2216370c1af0SSanjeev Bagewadi 22173b2aab18SMatthew Ahrens /* 22183b2aab18SMatthew Ahrens * When we do the clone swap, we will temporarily use more space 22193b2aab18SMatthew Ahrens * due to the refreservation (the head will no longer have any 22203b2aab18SMatthew Ahrens * unique space, so the entire amount of the refreservation will need 22213b2aab18SMatthew Ahrens * to be free). We will immediately destroy the clone, freeing 22223b2aab18SMatthew Ahrens * this space, but the freeing happens over many txg's. 22233b2aab18SMatthew Ahrens */ 22243b2aab18SMatthew Ahrens unused_refres_delta = (int64_t)MIN(ds->ds_reserved, 2225c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 22263b2aab18SMatthew Ahrens 22273b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 22283b2aab18SMatthew Ahrens unused_refres_delta > 22293b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) { 22303b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2231be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 2232fa9e4066Sahrens } 2233fa9e4066Sahrens 22343b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 22353b2aab18SMatthew Ahrens return (0); 22363b2aab18SMatthew Ahrens } 22371d452cf5Sahrens 22383b2aab18SMatthew Ahrens static void 22393b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) 22403b2aab18SMatthew Ahrens { 224191948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 22423b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 22433b2aab18SMatthew Ahrens dsl_dataset_t *ds, *clone; 22443b2aab18SMatthew Ahrens uint64_t cloneobj; 22459adfa60dSMatthew Ahrens char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 22461d452cf5Sahrens 224791948b51SKeith M Wesolowski VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds)); 22481d452cf5Sahrens 2249a7027df1SMatthew Ahrens dsl_dataset_name(ds->ds_prev, namebuf); 2250a7027df1SMatthew Ahrens fnvlist_add_string(ddra->ddra_result, "target", namebuf); 2251a7027df1SMatthew Ahrens 22523b2aab18SMatthew Ahrens cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback", 22533b2aab18SMatthew Ahrens ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx); 22541d452cf5Sahrens 22553b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone)); 22561d452cf5Sahrens 22573b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(clone, ds, tx); 22583b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 22593b2aab18SMatthew Ahrens 22603b2aab18SMatthew Ahrens dsl_destroy_head_sync_impl(clone, tx); 22613b2aab18SMatthew Ahrens 22623b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 22633b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 22643b2aab18SMatthew Ahrens } 22653b2aab18SMatthew Ahrens 226691948b51SKeith M Wesolowski /* 2267a7027df1SMatthew Ahrens * Rolls back the given filesystem or volume to the most recent snapshot. 2268a7027df1SMatthew Ahrens * The name of the most recent snapshot will be returned under key "target" 2269a7027df1SMatthew Ahrens * in the result nvlist. 227091948b51SKeith M Wesolowski * 2271a7027df1SMatthew Ahrens * If owner != NULL: 227291948b51SKeith M Wesolowski * - The existing dataset MUST be owned by the specified owner at entry 227391948b51SKeith M Wesolowski * - Upon return, dataset will still be held by the same owner, whether we 227491948b51SKeith M Wesolowski * succeed or not. 227591948b51SKeith M Wesolowski * 227691948b51SKeith M Wesolowski * This mode is required any time the existing filesystem is mounted. See 227791948b51SKeith M Wesolowski * notes above zfs_suspend_fs() for further details. 227891948b51SKeith M Wesolowski */ 22793b2aab18SMatthew Ahrens int 2280a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result) 22813b2aab18SMatthew Ahrens { 228291948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t ddra; 228391948b51SKeith M Wesolowski 228491948b51SKeith M Wesolowski ddra.ddra_fsname = fsname; 228591948b51SKeith M Wesolowski ddra.ddra_owner = owner; 2286a7027df1SMatthew Ahrens ddra.ddra_result = result; 228791948b51SKeith M Wesolowski 22883b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rollback_check, 22897d46dc6cSMatthew Ahrens dsl_dataset_rollback_sync, &ddra, 22907d46dc6cSMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED)); 2291fa9e4066Sahrens } 229299653d4eSeschrock 2293088f3894Sahrens struct promotenode { 2294745cd3c5Smaybee list_node_t link; 2295745cd3c5Smaybee dsl_dataset_t *ds; 2296745cd3c5Smaybee }; 2297745cd3c5Smaybee 22983b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg { 22993b2aab18SMatthew Ahrens const char *ddpa_clonename; 23003b2aab18SMatthew Ahrens dsl_dataset_t *ddpa_clone; 230174e7dc98SMatthew Ahrens list_t shared_snaps, origin_snaps, clone_snaps; 23023b2aab18SMatthew Ahrens dsl_dataset_t *origin_origin; /* origin of the origin */ 230374e7dc98SMatthew Ahrens uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 2304681d9761SEric Taylor char *err_ds; 2305a2afb611SJerry Jelinek cred_t *cr; 23063b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t; 23071d452cf5Sahrens 230874e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 23093b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, 23103b2aab18SMatthew Ahrens void *tag); 23113b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag); 231274e7dc98SMatthew Ahrens 231399653d4eSeschrock static int 23143b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx) 231599653d4eSeschrock { 23163b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 23173b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 23183b2aab18SMatthew Ahrens dsl_dataset_t *hds; 23193b2aab18SMatthew Ahrens struct promotenode *snap; 23203b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 2321745cd3c5Smaybee int err; 2322cde58dbcSMatthew Ahrens uint64_t unused; 2323a2afb611SJerry Jelinek uint64_t ss_mv_cnt; 2324cb5842f8SAndriy Gapon size_t max_snap_len; 23251d452cf5Sahrens 23263b2aab18SMatthew Ahrens err = promote_hold(ddpa, dp, FTAG); 23273b2aab18SMatthew Ahrens if (err != 0) 23283b2aab18SMatthew Ahrens return (err); 232999653d4eSeschrock 23303b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 2331cb5842f8SAndriy Gapon max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1; 23321d452cf5Sahrens 2333c1379625SJustin T. Gibbs if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) { 23343b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2335be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 23363b2aab18SMatthew Ahrens } 23373b2aab18SMatthew Ahrens 23383b2aab18SMatthew Ahrens /* 23393b2aab18SMatthew Ahrens * Compute and check the amount of space to transfer. Since this is 23403b2aab18SMatthew Ahrens * so expensive, don't do the preliminary check. 23413b2aab18SMatthew Ahrens */ 23423b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 23433b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 23443b2aab18SMatthew Ahrens return (0); 23453b2aab18SMatthew Ahrens } 23463b2aab18SMatthew Ahrens 23473b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 23483b2aab18SMatthew Ahrens origin_ds = snap->ds; 234999653d4eSeschrock 23503cb34c60Sahrens /* compute origin's new unique space */ 23513b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 2352c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 2353c1379625SJustin T. Gibbs origin_ds->ds_object); 2354cde58dbcSMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 2355c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX, 23563b2aab18SMatthew Ahrens &ddpa->unique, &unused, &unused); 235799653d4eSeschrock 2358745cd3c5Smaybee /* 2359745cd3c5Smaybee * Walk the snapshots that we are moving 2360745cd3c5Smaybee * 236174e7dc98SMatthew Ahrens * Compute space to transfer. Consider the incremental changes 23623b2aab18SMatthew Ahrens * to used by each snapshot: 236374e7dc98SMatthew Ahrens * (my used) = (prev's used) + (blocks born) - (blocks killed) 236474e7dc98SMatthew Ahrens * So each snapshot gave birth to: 236574e7dc98SMatthew Ahrens * (blocks born) = (my used) - (prev's used) + (blocks killed) 2366745cd3c5Smaybee * So a sequence would look like: 236774e7dc98SMatthew Ahrens * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 2368745cd3c5Smaybee * Which simplifies to: 236974e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + k1 + k0 2370745cd3c5Smaybee * Note however, if we stop before we reach the ORIGIN we get: 237174e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + kM - uM-1 2372745cd3c5Smaybee */ 2373a2afb611SJerry Jelinek ss_mv_cnt = 0; 2374c1379625SJustin T. Gibbs ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes; 2375c1379625SJustin T. Gibbs ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes; 2376c1379625SJustin T. Gibbs ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes; 23773b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 23783b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 237999653d4eSeschrock uint64_t val, dlused, dlcomp, dluncomp; 2380745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 238199653d4eSeschrock 2382a2afb611SJerry Jelinek ss_mv_cnt++; 2383a2afb611SJerry Jelinek 23843b2aab18SMatthew Ahrens /* 23853b2aab18SMatthew Ahrens * If there are long holds, we won't be able to evict 23863b2aab18SMatthew Ahrens * the objset. 23873b2aab18SMatthew Ahrens */ 23883b2aab18SMatthew Ahrens if (dsl_dataset_long_held(ds)) { 2389be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 23903b2aab18SMatthew Ahrens goto out; 23913b2aab18SMatthew Ahrens } 23923b2aab18SMatthew Ahrens 239399653d4eSeschrock /* Check that the snapshot name does not conflict */ 23943b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 2395cb5842f8SAndriy Gapon if (strlen(ds->ds_snapname) >= max_snap_len) { 2396cb5842f8SAndriy Gapon err = SET_ERROR(ENAMETOOLONG); 2397cb5842f8SAndriy Gapon goto out; 2398cb5842f8SAndriy Gapon } 2399745cd3c5Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 2400681d9761SEric Taylor if (err == 0) { 24013b2aab18SMatthew Ahrens (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname); 2402be6fd75aSMatthew Ahrens err = SET_ERROR(EEXIST); 2403681d9761SEric Taylor goto out; 2404681d9761SEric Taylor } 2405745cd3c5Smaybee if (err != ENOENT) 2406681d9761SEric Taylor goto out; 240799653d4eSeschrock 2408745cd3c5Smaybee /* The very first snapshot does not have a deadlist */ 2409c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0) 241074e7dc98SMatthew Ahrens continue; 241174e7dc98SMatthew Ahrens 2412cde58dbcSMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, 2413cde58dbcSMatthew Ahrens &dlused, &dlcomp, &dluncomp); 24143b2aab18SMatthew Ahrens ddpa->used += dlused; 24153b2aab18SMatthew Ahrens ddpa->comp += dlcomp; 24163b2aab18SMatthew Ahrens ddpa->uncomp += dluncomp; 241774e7dc98SMatthew Ahrens } 2418745cd3c5Smaybee 2419745cd3c5Smaybee /* 2420745cd3c5Smaybee * If we are a clone of a clone then we never reached ORIGIN, 2421745cd3c5Smaybee * so we need to subtract out the clone origin's used space. 2422745cd3c5Smaybee */ 24233b2aab18SMatthew Ahrens if (ddpa->origin_origin) { 2424c1379625SJustin T. Gibbs ddpa->used -= 2425c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes; 2426c1379625SJustin T. Gibbs ddpa->comp -= 2427c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes; 24283b2aab18SMatthew Ahrens ddpa->uncomp -= 2429c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)-> 2430c1379625SJustin T. Gibbs ds_uncompressed_bytes; 243199653d4eSeschrock } 243299653d4eSeschrock 2433a2afb611SJerry Jelinek /* Check that there is enough space and limit headroom here */ 243474e7dc98SMatthew Ahrens err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 2435a2afb611SJerry Jelinek 0, ss_mv_cnt, ddpa->used, ddpa->cr); 24363b2aab18SMatthew Ahrens if (err != 0) 24373b2aab18SMatthew Ahrens goto out; 243874e7dc98SMatthew Ahrens 243974e7dc98SMatthew Ahrens /* 244074e7dc98SMatthew Ahrens * Compute the amounts of space that will be used by snapshots 244174e7dc98SMatthew Ahrens * after the promotion (for both origin and clone). For each, 244274e7dc98SMatthew Ahrens * it is the amount of space that will be on all of their 244374e7dc98SMatthew Ahrens * deadlists (that was not born before their new origin). 244474e7dc98SMatthew Ahrens */ 2445c1379625SJustin T. Gibbs if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) { 244674e7dc98SMatthew Ahrens uint64_t space; 244774e7dc98SMatthew Ahrens 244874e7dc98SMatthew Ahrens /* 244974e7dc98SMatthew Ahrens * Note, typically this will not be a clone of a clone, 24503f9d6ad7SLin Ling * so dd_origin_txg will be < TXG_INITIAL, so 2451cde58dbcSMatthew Ahrens * these snaplist_space() -> dsl_deadlist_space_range() 245274e7dc98SMatthew Ahrens * calls will be fast because they do not have to 245374e7dc98SMatthew Ahrens * iterate over all bps. 245474e7dc98SMatthew Ahrens */ 24553b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 24563b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->shared_snaps, 24573b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap); 24583b2aab18SMatthew Ahrens if (err != 0) 24593b2aab18SMatthew Ahrens goto out; 246074e7dc98SMatthew Ahrens 24613b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->clone_snaps, 24623f9d6ad7SLin Ling snap->ds->ds_dir->dd_origin_txg, &space); 24633b2aab18SMatthew Ahrens if (err != 0) 24643b2aab18SMatthew Ahrens goto out; 24653b2aab18SMatthew Ahrens ddpa->cloneusedsnap += space; 246674e7dc98SMatthew Ahrens } 2467c1379625SJustin T. Gibbs if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags & 2468c1379625SJustin T. Gibbs DD_FLAG_USED_BREAKDOWN) { 24693b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->origin_snaps, 2470c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg, 2471c1379625SJustin T. Gibbs &ddpa->originusedsnap); 24723b2aab18SMatthew Ahrens if (err != 0) 24733b2aab18SMatthew Ahrens goto out; 2474745cd3c5Smaybee } 24751d452cf5Sahrens 2476681d9761SEric Taylor out: 24773b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2478681d9761SEric Taylor return (err); 24791d452cf5Sahrens } 248099653d4eSeschrock 24811d452cf5Sahrens static void 24823b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx) 24831d452cf5Sahrens { 24843b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 24853b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 24863b2aab18SMatthew Ahrens dsl_dataset_t *hds; 24873b2aab18SMatthew Ahrens struct promotenode *snap; 24883b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 24893b2aab18SMatthew Ahrens dsl_dataset_t *origin_head; 24903b2aab18SMatthew Ahrens dsl_dir_t *dd; 24913cb34c60Sahrens dsl_dir_t *odd = NULL; 2492088f3894Sahrens uint64_t oldnext_obj; 249374e7dc98SMatthew Ahrens int64_t delta; 24941d452cf5Sahrens 24953b2aab18SMatthew Ahrens VERIFY0(promote_hold(ddpa, dp, FTAG)); 24963b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 24973b2aab18SMatthew Ahrens 2498c1379625SJustin T. Gibbs ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE); 24991d452cf5Sahrens 25003b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 25013b2aab18SMatthew Ahrens origin_ds = snap->ds; 25023b2aab18SMatthew Ahrens dd = hds->ds_dir; 25033b2aab18SMatthew Ahrens 25043b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 250574e7dc98SMatthew Ahrens origin_head = snap->ds; 250674e7dc98SMatthew Ahrens 25070b69c2f0Sahrens /* 25083cb34c60Sahrens * We need to explicitly open odd, since origin_ds's dd will be 25090b69c2f0Sahrens * changing. 25100b69c2f0Sahrens */ 25113b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object, 25123cb34c60Sahrens NULL, FTAG, &odd)); 251399653d4eSeschrock 2514745cd3c5Smaybee /* change origin's next snap */ 2515745cd3c5Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2516c1379625SJustin T. Gibbs oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj; 25173b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 2518c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 2519c1379625SJustin T. Gibbs origin_ds->ds_object); 2520c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object; 2521745cd3c5Smaybee 2522088f3894Sahrens /* change the origin's next clone */ 2523c1379625SJustin T. Gibbs if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) { 25243b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(origin_ds, 25253b2aab18SMatthew Ahrens snap->ds->ds_object, tx); 25263b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2527c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_clones_obj, 2528088f3894Sahrens oldnext_obj, tx)); 2529088f3894Sahrens } 2530088f3894Sahrens 2531745cd3c5Smaybee /* change origin */ 2532745cd3c5Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 2533c1379625SJustin T. Gibbs ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object); 2534c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj; 25353f9d6ad7SLin Ling dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg; 2536745cd3c5Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 2537c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object; 25383f9d6ad7SLin Ling origin_head->ds_dir->dd_origin_txg = 2539c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg; 2540745cd3c5Smaybee 2541cde58dbcSMatthew Ahrens /* change dd_clone entries */ 2542cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 25433b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2544c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx)); 25453b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2546c1379625SJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 2547cde58dbcSMatthew Ahrens hds->ds_object, tx)); 2548cde58dbcSMatthew Ahrens 25493b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2550c1379625SJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 2551cde58dbcSMatthew Ahrens origin_head->ds_object, tx)); 2552c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_clones == 0) { 2553c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones = 2554c1379625SJustin T. Gibbs zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES, 2555c1379625SJustin T. Gibbs DMU_OT_NONE, 0, tx); 2556cde58dbcSMatthew Ahrens } 25573b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2558c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx)); 2559cde58dbcSMatthew Ahrens } 2560cde58dbcSMatthew Ahrens 256199653d4eSeschrock /* move snapshots to this dir */ 25623b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 25633b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 2564745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 256599653d4eSeschrock 25663b2aab18SMatthew Ahrens /* 25673b2aab18SMatthew Ahrens * Property callbacks are registered to a particular 25683b2aab18SMatthew Ahrens * dsl_dir. Since ours is changing, evict the objset 25693b2aab18SMatthew Ahrens * so that they will be unregistered from the old dsl_dir. 25703b2aab18SMatthew Ahrens */ 2571503ad85cSMatthew Ahrens if (ds->ds_objset) { 2572503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 2573503ad85cSMatthew Ahrens ds->ds_objset = NULL; 25743baa08fcSek } 25753b2aab18SMatthew Ahrens 257699653d4eSeschrock /* move snap name entry */ 25773b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 25783b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_snap_remove(origin_head, 2579a2afb611SJerry Jelinek ds->ds_snapname, tx, B_TRUE)); 25803b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, 2581c1379625SJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname, 258299653d4eSeschrock 8, 1, &ds->ds_object, tx)); 2583a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(hds->ds_dir, 1, 2584a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 2585cde58dbcSMatthew Ahrens 258699653d4eSeschrock /* change containing dsl_dir */ 258799653d4eSeschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 2588c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object); 2589c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object; 25903cb34c60Sahrens ASSERT3P(ds->ds_dir, ==, odd); 25913b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 25923b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object, 259399653d4eSeschrock NULL, ds, &ds->ds_dir)); 259499653d4eSeschrock 2595cde58dbcSMatthew Ahrens /* move any clone references */ 2596c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj && 2597cde58dbcSMatthew Ahrens spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2598cde58dbcSMatthew Ahrens zap_cursor_t zc; 2599cde58dbcSMatthew Ahrens zap_attribute_t za; 2600cde58dbcSMatthew Ahrens 26013b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 2602c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj); 26033b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 26043b2aab18SMatthew Ahrens zap_cursor_advance(&zc)) { 26053b2aab18SMatthew Ahrens dsl_dataset_t *cnds; 26063b2aab18SMatthew Ahrens uint64_t o; 2607a9799022Sck 26083b2aab18SMatthew Ahrens if (za.za_first_integer == oldnext_obj) { 26093b2aab18SMatthew Ahrens /* 26103b2aab18SMatthew Ahrens * We've already moved the 26113b2aab18SMatthew Ahrens * origin's reference. 26123b2aab18SMatthew Ahrens */ 26133b2aab18SMatthew Ahrens continue; 26143b2aab18SMatthew Ahrens } 2615a9799022Sck 26163b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 26173b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &cnds)); 2618c1379625SJustin T. Gibbs o = dsl_dir_phys(cnds->ds_dir)-> 2619c1379625SJustin T. Gibbs dd_head_dataset_obj; 2620a9799022Sck 26213b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2622c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, o, tx)); 26233b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2624c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, o, tx)); 26253b2aab18SMatthew Ahrens dsl_dataset_rele(cnds, FTAG); 26263b2aab18SMatthew Ahrens } 26273b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 26283b2aab18SMatthew Ahrens } 26299082849eSck 26303b2aab18SMatthew Ahrens ASSERT(!dsl_prop_hascb(ds)); 2631a9799022Sck } 2632a9799022Sck 2633a9799022Sck /* 26343b2aab18SMatthew Ahrens * Change space accounting. 26353b2aab18SMatthew Ahrens * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 26363b2aab18SMatthew Ahrens * both be valid, or both be 0 (resulting in delta == 0). This 26373b2aab18SMatthew Ahrens * is true for each of {clone,origin} independently. 2638a9799022Sck */ 2639a9799022Sck 26403b2aab18SMatthew Ahrens delta = ddpa->cloneusedsnap - 2641c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]; 26423b2aab18SMatthew Ahrens ASSERT3S(delta, >=, 0); 26433b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, delta); 26443b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 26453b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_HEAD, 26463b2aab18SMatthew Ahrens ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx); 26473b2aab18SMatthew Ahrens 26483b2aab18SMatthew Ahrens delta = ddpa->originusedsnap - 2649c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP]; 26503b2aab18SMatthew Ahrens ASSERT3S(delta, <=, 0); 26513b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, -delta); 26523b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 26533b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_HEAD, 26543b2aab18SMatthew Ahrens -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx); 26553b2aab18SMatthew Ahrens 2656c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique; 26573b2aab18SMatthew Ahrens 26583b2aab18SMatthew Ahrens /* log history record */ 26593b2aab18SMatthew Ahrens spa_history_log_internal_ds(hds, "promote", tx, ""); 26603b2aab18SMatthew Ahrens 26613b2aab18SMatthew Ahrens dsl_dir_rele(odd, FTAG); 26623b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2663a9799022Sck } 2664a9799022Sck 26653b2aab18SMatthew Ahrens /* 26663b2aab18SMatthew Ahrens * Make a list of dsl_dataset_t's for the snapshots between first_obj 26673b2aab18SMatthew Ahrens * (exclusive) and last_obj (inclusive). The list will be in reverse 26683b2aab18SMatthew Ahrens * order (last_obj will be the list_head()). If first_obj == 0, do all 26693b2aab18SMatthew Ahrens * snapshots back to this dataset's origin. 26703b2aab18SMatthew Ahrens */ 2671a9799022Sck static int 26723b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp, 26733b2aab18SMatthew Ahrens uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag) 2674a9799022Sck { 26753b2aab18SMatthew Ahrens uint64_t obj = last_obj; 2676a9799022Sck 26773b2aab18SMatthew Ahrens list_create(l, sizeof (struct promotenode), 26783b2aab18SMatthew Ahrens offsetof(struct promotenode, link)); 2679a9799022Sck 26803b2aab18SMatthew Ahrens while (obj != first_obj) { 26813b2aab18SMatthew Ahrens dsl_dataset_t *ds; 26823b2aab18SMatthew Ahrens struct promotenode *snap; 26833b2aab18SMatthew Ahrens int err; 268492241e0bSTom Erickson 26853b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 26863b2aab18SMatthew Ahrens ASSERT(err != ENOENT); 26873b2aab18SMatthew Ahrens if (err != 0) 26883b2aab18SMatthew Ahrens return (err); 2689a9799022Sck 26903b2aab18SMatthew Ahrens if (first_obj == 0) 2691c1379625SJustin T. Gibbs first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj; 26923b2aab18SMatthew Ahrens 26933b2aab18SMatthew Ahrens snap = kmem_alloc(sizeof (*snap), KM_SLEEP); 26943b2aab18SMatthew Ahrens snap->ds = ds; 26953b2aab18SMatthew Ahrens list_insert_tail(l, snap); 2696c1379625SJustin T. Gibbs obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 26973b2aab18SMatthew Ahrens } 2698a9799022Sck 2699a9799022Sck return (0); 2700a9799022Sck } 2701a9799022Sck 27023b2aab18SMatthew Ahrens static int 27033b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 2704a9799022Sck { 27053b2aab18SMatthew Ahrens struct promotenode *snap; 2706a9799022Sck 27073b2aab18SMatthew Ahrens *spacep = 0; 27083b2aab18SMatthew Ahrens for (snap = list_head(l); snap; snap = list_next(l, snap)) { 27093b2aab18SMatthew Ahrens uint64_t used, comp, uncomp; 27103b2aab18SMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 27113b2aab18SMatthew Ahrens mintxg, UINT64_MAX, &used, &comp, &uncomp); 27123b2aab18SMatthew Ahrens *spacep += used; 271392241e0bSTom Erickson } 27143b2aab18SMatthew Ahrens return (0); 2715a9799022Sck } 2716a9799022Sck 27173b2aab18SMatthew Ahrens static void 27183b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag) 2719a9799022Sck { 27203b2aab18SMatthew Ahrens struct promotenode *snap; 272192241e0bSTom Erickson 27223b2aab18SMatthew Ahrens if (l == NULL || !list_link_active(&l->list_head)) 27233b2aab18SMatthew Ahrens return; 2724a9799022Sck 27253b2aab18SMatthew Ahrens while ((snap = list_tail(l)) != NULL) { 27263b2aab18SMatthew Ahrens list_remove(l, snap); 27273b2aab18SMatthew Ahrens dsl_dataset_rele(snap->ds, tag); 27283b2aab18SMatthew Ahrens kmem_free(snap, sizeof (*snap)); 27293b2aab18SMatthew Ahrens } 27303b2aab18SMatthew Ahrens list_destroy(l); 2731a9799022Sck } 2732a9799022Sck 2733a9799022Sck static int 27343b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag) 2735a9799022Sck { 27363b2aab18SMatthew Ahrens int error; 27373b2aab18SMatthew Ahrens dsl_dir_t *dd; 27383b2aab18SMatthew Ahrens struct promotenode *snap; 2739a9799022Sck 27403b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag, 27413b2aab18SMatthew Ahrens &ddpa->ddpa_clone); 27423b2aab18SMatthew Ahrens if (error != 0) 27433b2aab18SMatthew Ahrens return (error); 27443b2aab18SMatthew Ahrens dd = ddpa->ddpa_clone->ds_dir; 2745a9799022Sck 2746bc9014e6SJustin Gibbs if (ddpa->ddpa_clone->ds_is_snapshot || 27473b2aab18SMatthew Ahrens !dsl_dir_is_clone(dd)) { 27483b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 2749be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 27503b2aab18SMatthew Ahrens } 2751a9799022Sck 2752c1379625SJustin T. Gibbs error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj, 27533b2aab18SMatthew Ahrens &ddpa->shared_snaps, tag); 27543b2aab18SMatthew Ahrens if (error != 0) 27553b2aab18SMatthew Ahrens goto out; 2756a9799022Sck 27573b2aab18SMatthew Ahrens error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object, 27583b2aab18SMatthew Ahrens &ddpa->clone_snaps, tag); 27593b2aab18SMatthew Ahrens if (error != 0) 27603b2aab18SMatthew Ahrens goto out; 2761a9799022Sck 27623b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 2763c1379625SJustin T. Gibbs ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj); 2764c1379625SJustin T. Gibbs error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj, 2765c1379625SJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj, 27663b2aab18SMatthew Ahrens &ddpa->origin_snaps, tag); 27673b2aab18SMatthew Ahrens if (error != 0) 27683b2aab18SMatthew Ahrens goto out; 2769379c004dSEric Schrock 2770c1379625SJustin T. Gibbs if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) { 27713b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 2772c1379625SJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj, 27733b2aab18SMatthew Ahrens tag, &ddpa->origin_origin); 27743b2aab18SMatthew Ahrens if (error != 0) 27753b2aab18SMatthew Ahrens goto out; 2776379c004dSEric Schrock } 27773b2aab18SMatthew Ahrens out: 27783b2aab18SMatthew Ahrens if (error != 0) 27793b2aab18SMatthew Ahrens promote_rele(ddpa, tag); 27803b2aab18SMatthew Ahrens return (error); 2781a9799022Sck } 2782a9799022Sck 2783a9799022Sck static void 27843b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag) 2785a9799022Sck { 27863b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->shared_snaps, tag); 27873b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->clone_snaps, tag); 27883b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->origin_snaps, tag); 27893b2aab18SMatthew Ahrens if (ddpa->origin_origin != NULL) 27903b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->origin_origin, tag); 27913b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 27923b2aab18SMatthew Ahrens } 279302c8f3f0SMatthew Ahrens 27943b2aab18SMatthew Ahrens /* 27953b2aab18SMatthew Ahrens * Promote a clone. 27963b2aab18SMatthew Ahrens * 27973b2aab18SMatthew Ahrens * If it fails due to a conflicting snapshot name, "conflsnap" will be filled 27989adfa60dSMatthew Ahrens * in with the name. (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.) 27993b2aab18SMatthew Ahrens */ 28003b2aab18SMatthew Ahrens int 28013b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap) 28023b2aab18SMatthew Ahrens { 28033b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t ddpa = { 0 }; 28043b2aab18SMatthew Ahrens uint64_t numsnaps; 28053b2aab18SMatthew Ahrens int error; 28063b2aab18SMatthew Ahrens objset_t *os; 280792241e0bSTom Erickson 28083b2aab18SMatthew Ahrens /* 28093b2aab18SMatthew Ahrens * We will modify space proportional to the number of 28103b2aab18SMatthew Ahrens * snapshots. Compute numsnaps. 28113b2aab18SMatthew Ahrens */ 28123b2aab18SMatthew Ahrens error = dmu_objset_hold(name, FTAG, &os); 28133b2aab18SMatthew Ahrens if (error != 0) 28143b2aab18SMatthew Ahrens return (error); 28153b2aab18SMatthew Ahrens error = zap_count(dmu_objset_pool(os)->dp_meta_objset, 2816c1379625SJustin T. Gibbs dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj, 2817c1379625SJustin T. Gibbs &numsnaps); 28183b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 28193b2aab18SMatthew Ahrens if (error != 0) 28203b2aab18SMatthew Ahrens return (error); 282102c8f3f0SMatthew Ahrens 28223b2aab18SMatthew Ahrens ddpa.ddpa_clonename = name; 28233b2aab18SMatthew Ahrens ddpa.err_ds = conflsnap; 2824a2afb611SJerry Jelinek ddpa.cr = CRED(); 282502c8f3f0SMatthew Ahrens 28263b2aab18SMatthew Ahrens return (dsl_sync_task(name, dsl_dataset_promote_check, 28277d46dc6cSMatthew Ahrens dsl_dataset_promote_sync, &ddpa, 28287d46dc6cSMatthew Ahrens 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED)); 2829a9799022Sck } 2830a9799022Sck 2831a9799022Sck int 28323b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone, 283391948b51SKeith M Wesolowski dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx) 2834a9799022Sck { 28355f7a8e6dSDan McDonald /* 28365f7a8e6dSDan McDonald * "slack" factor for received datasets with refquota set on them. 28375f7a8e6dSDan McDonald * See the bottom of this function for details on its use. 28385f7a8e6dSDan McDonald */ 28395f7a8e6dSDan McDonald uint64_t refquota_slack = DMU_MAX_ACCESS * spa_asize_inflation; 28403b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2841a9799022Sck 28423b2aab18SMatthew Ahrens /* they should both be heads */ 2843bc9014e6SJustin Gibbs if (clone->ds_is_snapshot || 2844bc9014e6SJustin Gibbs origin_head->ds_is_snapshot) 2845be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 284692241e0bSTom Erickson 284734f2f8cfSMatthew Ahrens /* if we are not forcing, the branch point should be just before them */ 284834f2f8cfSMatthew Ahrens if (!force && clone->ds_prev != origin_head->ds_prev) 2849be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2850a9799022Sck 28513b2aab18SMatthew Ahrens /* clone should be the clone (unless they are unrelated) */ 28523b2aab18SMatthew Ahrens if (clone->ds_prev != NULL && 28533b2aab18SMatthew Ahrens clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap && 285434f2f8cfSMatthew Ahrens origin_head->ds_dir != clone->ds_prev->ds_dir) 2855be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 285692241e0bSTom Erickson 28573b2aab18SMatthew Ahrens /* the clone should be a child of the origin */ 28583b2aab18SMatthew Ahrens if (clone->ds_dir->dd_parent != origin_head->ds_dir) 2859be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2860842727c2SChris Kirby 28613b2aab18SMatthew Ahrens /* origin_head shouldn't be modified unless 'force' */ 286234f2f8cfSMatthew Ahrens if (!force && 286334f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev)) 2864be6fd75aSMatthew Ahrens return (SET_ERROR(ETXTBSY)); 2865c99e4bdcSChris Kirby 28663b2aab18SMatthew Ahrens /* origin_head should have no long holds (e.g. is not mounted) */ 286791948b51SKeith M Wesolowski if (dsl_dataset_handoff_check(origin_head, owner, tx)) 2868be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 28693b2aab18SMatthew Ahrens 28703b2aab18SMatthew Ahrens /* check amount of any unconsumed refreservation */ 28713b2aab18SMatthew Ahrens unused_refres_delta = 28723b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2873c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) - 28743b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2875c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 28763b2aab18SMatthew Ahrens 28773b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 28783b2aab18SMatthew Ahrens unused_refres_delta > 28793b2aab18SMatthew Ahrens dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE)) 2880be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 28813b2aab18SMatthew Ahrens 28825f7a8e6dSDan McDonald /* 28835f7a8e6dSDan McDonald * The clone can't be too much over the head's refquota. 28845f7a8e6dSDan McDonald * 28855f7a8e6dSDan McDonald * To ensure that the entire refquota can be used, we allow one 28865f7a8e6dSDan McDonald * transaction to exceed the the refquota. Therefore, this check 28875f7a8e6dSDan McDonald * needs to also allow for the space referenced to be more than the 28885f7a8e6dSDan McDonald * refquota. The maximum amount of space that one transaction can use 28895f7a8e6dSDan McDonald * on disk is DMU_MAX_ACCESS * spa_asize_inflation. Allowing this 28905f7a8e6dSDan McDonald * overage ensures that we are able to receive a filesystem that 28915f7a8e6dSDan McDonald * exceeds the refquota on the source system. 28925f7a8e6dSDan McDonald * 28935f7a8e6dSDan McDonald * So that overage is the refquota_slack we use below. 28945f7a8e6dSDan McDonald */ 28953b2aab18SMatthew Ahrens if (origin_head->ds_quota != 0 && 2896c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes > 28975f7a8e6dSDan McDonald origin_head->ds_quota + refquota_slack) 2898be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 2899c99e4bdcSChris Kirby 29003b2aab18SMatthew Ahrens return (0); 2901c99e4bdcSChris Kirby } 2902c99e4bdcSChris Kirby 2903a7f53a56SChris Kirby void 29043b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone, 29053b2aab18SMatthew Ahrens dsl_dataset_t *origin_head, dmu_tx_t *tx) 2906a7f53a56SChris Kirby { 29073b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 29083b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2909a7f53a56SChris Kirby 29103b2aab18SMatthew Ahrens ASSERT(clone->ds_reserved == 0); 29115f7a8e6dSDan McDonald /* 29125f7a8e6dSDan McDonald * NOTE: On DEBUG kernels there could be a race between this and 29135f7a8e6dSDan McDonald * the check function if spa_asize_inflation is adjusted... 29145f7a8e6dSDan McDonald */ 29153b2aab18SMatthew Ahrens ASSERT(origin_head->ds_quota == 0 || 29165f7a8e6dSDan McDonald dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota + 29175f7a8e6dSDan McDonald DMU_MAX_ACCESS * spa_asize_inflation); 291834f2f8cfSMatthew Ahrens ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev); 2919842727c2SChris Kirby 2920ca0cc391SMatthew Ahrens /* 2921ca0cc391SMatthew Ahrens * Swap per-dataset feature flags. 2922ca0cc391SMatthew Ahrens */ 2923ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 2924ca0cc391SMatthew Ahrens if (!(spa_feature_table[f].fi_flags & 2925ca0cc391SMatthew Ahrens ZFEATURE_FLAG_PER_DATASET)) { 2926ca0cc391SMatthew Ahrens ASSERT(!clone->ds_feature_inuse[f]); 2927ca0cc391SMatthew Ahrens ASSERT(!origin_head->ds_feature_inuse[f]); 2928ca0cc391SMatthew Ahrens continue; 2929ca0cc391SMatthew Ahrens } 2930ca0cc391SMatthew Ahrens 2931ca0cc391SMatthew Ahrens boolean_t clone_inuse = clone->ds_feature_inuse[f]; 2932ca0cc391SMatthew Ahrens boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f]; 2933ca0cc391SMatthew Ahrens 2934ca0cc391SMatthew Ahrens if (clone_inuse) { 2935ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(clone->ds_object, f, tx); 2936ca0cc391SMatthew Ahrens clone->ds_feature_inuse[f] = B_FALSE; 2937ca0cc391SMatthew Ahrens } 2938ca0cc391SMatthew Ahrens if (origin_head_inuse) { 2939ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(origin_head->ds_object, 2940ca0cc391SMatthew Ahrens f, tx); 2941ca0cc391SMatthew Ahrens origin_head->ds_feature_inuse[f] = B_FALSE; 2942ca0cc391SMatthew Ahrens } 2943ca0cc391SMatthew Ahrens if (clone_inuse) { 2944ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(origin_head->ds_object, 2945ca0cc391SMatthew Ahrens f, tx); 2946ca0cc391SMatthew Ahrens origin_head->ds_feature_inuse[f] = B_TRUE; 2947ca0cc391SMatthew Ahrens } 2948ca0cc391SMatthew Ahrens if (origin_head_inuse) { 2949ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(clone->ds_object, f, tx); 2950ca0cc391SMatthew Ahrens clone->ds_feature_inuse[f] = B_TRUE; 2951ca0cc391SMatthew Ahrens } 2952ca0cc391SMatthew Ahrens } 2953ca0cc391SMatthew Ahrens 29543b2aab18SMatthew Ahrens dmu_buf_will_dirty(clone->ds_dbuf, tx); 29553b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin_head->ds_dbuf, tx); 2956842727c2SChris Kirby 29573b2aab18SMatthew Ahrens if (clone->ds_objset != NULL) { 29583b2aab18SMatthew Ahrens dmu_objset_evict(clone->ds_objset); 29593b2aab18SMatthew Ahrens clone->ds_objset = NULL; 29603b2aab18SMatthew Ahrens } 2961842727c2SChris Kirby 29623b2aab18SMatthew Ahrens if (origin_head->ds_objset != NULL) { 29633b2aab18SMatthew Ahrens dmu_objset_evict(origin_head->ds_objset); 29643b2aab18SMatthew Ahrens origin_head->ds_objset = NULL; 2965842727c2SChris Kirby } 2966842727c2SChris Kirby 29673b2aab18SMatthew Ahrens unused_refres_delta = 29683b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2969c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) - 29703b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2971c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 29723b2aab18SMatthew Ahrens 29733b2aab18SMatthew Ahrens /* 29743b2aab18SMatthew Ahrens * Reset origin's unique bytes, if it exists. 29753b2aab18SMatthew Ahrens */ 29763b2aab18SMatthew Ahrens if (clone->ds_prev) { 29773b2aab18SMatthew Ahrens dsl_dataset_t *origin = clone->ds_prev; 29783b2aab18SMatthew Ahrens uint64_t comp, uncomp; 29793b2aab18SMatthew Ahrens 29803b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 29813b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 2982c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX, 2983c1379625SJustin T. Gibbs &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp); 29843b2aab18SMatthew Ahrens } 29853b2aab18SMatthew Ahrens 29863b2aab18SMatthew Ahrens /* swap blkptrs */ 29873b2aab18SMatthew Ahrens { 2988c166b69dSPaul Dagnelie rrw_enter(&clone->ds_bp_rwlock, RW_WRITER, FTAG); 2989c166b69dSPaul Dagnelie rrw_enter(&origin_head->ds_bp_rwlock, RW_WRITER, FTAG); 29903b2aab18SMatthew Ahrens blkptr_t tmp; 2991c1379625SJustin T. Gibbs tmp = dsl_dataset_phys(origin_head)->ds_bp; 2992c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_bp = 2993c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp; 2994c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp = tmp; 2995c166b69dSPaul Dagnelie rrw_exit(&origin_head->ds_bp_rwlock, FTAG); 2996c166b69dSPaul Dagnelie rrw_exit(&clone->ds_bp_rwlock, FTAG); 29973b2aab18SMatthew Ahrens } 29983b2aab18SMatthew Ahrens 29993b2aab18SMatthew Ahrens /* set dd_*_bytes */ 30003b2aab18SMatthew Ahrens { 30013b2aab18SMatthew Ahrens int64_t dused, dcomp, duncomp; 30023b2aab18SMatthew Ahrens uint64_t cdl_used, cdl_comp, cdl_uncomp; 30033b2aab18SMatthew Ahrens uint64_t odl_used, odl_comp, odl_uncomp; 30043b2aab18SMatthew Ahrens 3005c1379625SJustin T. Gibbs ASSERT3U(dsl_dir_phys(clone->ds_dir)-> 30063b2aab18SMatthew Ahrens dd_used_breakdown[DD_USED_SNAP], ==, 0); 30073b2aab18SMatthew Ahrens 30083b2aab18SMatthew Ahrens dsl_deadlist_space(&clone->ds_deadlist, 30093b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 30103b2aab18SMatthew Ahrens dsl_deadlist_space(&origin_head->ds_deadlist, 30113b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 301215508ac0SChris Kirby 3013c1379625SJustin T. Gibbs dused = dsl_dataset_phys(clone)->ds_referenced_bytes + 3014c1379625SJustin T. Gibbs cdl_used - 3015c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_referenced_bytes + 3016c1379625SJustin T. Gibbs odl_used); 3017c1379625SJustin T. Gibbs dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes + 3018c1379625SJustin T. Gibbs cdl_comp - 3019c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_compressed_bytes + 3020c1379625SJustin T. Gibbs odl_comp); 3021c1379625SJustin T. Gibbs duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes + 30223b2aab18SMatthew Ahrens cdl_uncomp - 3023c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes + 3024c1379625SJustin T. Gibbs odl_uncomp); 3025842727c2SChris Kirby 30263b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD, 30273b2aab18SMatthew Ahrens dused, dcomp, duncomp, tx); 30283b2aab18SMatthew Ahrens dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD, 30293b2aab18SMatthew Ahrens -dused, -dcomp, -duncomp, tx); 3030842727c2SChris Kirby 3031842727c2SChris Kirby /* 30323b2aab18SMatthew Ahrens * The difference in the space used by snapshots is the 30333b2aab18SMatthew Ahrens * difference in snapshot space due to the head's 30343b2aab18SMatthew Ahrens * deadlist (since that's the only thing that's 30353b2aab18SMatthew Ahrens * changing that affects the snapused). 3036842727c2SChris Kirby */ 30373b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 30383b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 30393b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 30403b2aab18SMatthew Ahrens dsl_deadlist_space_range(&origin_head->ds_deadlist, 30413b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 30423b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 30433b2aab18SMatthew Ahrens dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used, 30443b2aab18SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 3045842727c2SChris Kirby } 3046842727c2SChris Kirby 30473b2aab18SMatthew Ahrens /* swap ds_*_bytes */ 3048c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes, 3049c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes); 3050c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes, 3051c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_compressed_bytes); 3052c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes, 3053c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_uncompressed_bytes); 3054c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes, 3055c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 3056842727c2SChris Kirby 30573b2aab18SMatthew Ahrens /* apply any parent delta for change in unconsumed refreservation */ 30583b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV, 30593b2aab18SMatthew Ahrens unused_refres_delta, 0, 0, tx); 3060ca45db41SChris Kirby 30613b2aab18SMatthew Ahrens /* 30623b2aab18SMatthew Ahrens * Swap deadlists. 30633b2aab18SMatthew Ahrens */ 30643b2aab18SMatthew Ahrens dsl_deadlist_close(&clone->ds_deadlist); 30653b2aab18SMatthew Ahrens dsl_deadlist_close(&origin_head->ds_deadlist); 3066c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj, 3067c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj); 30683b2aab18SMatthew Ahrens dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset, 3069c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj); 30703b2aab18SMatthew Ahrens dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset, 3071c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_deadlist_obj); 3072842727c2SChris Kirby 30733b2aab18SMatthew Ahrens dsl_scan_ds_clone_swapped(origin_head, clone, tx); 3074842727c2SChris Kirby 30753b2aab18SMatthew Ahrens spa_history_log_internal_ds(clone, "clone swap", tx, 30763b2aab18SMatthew Ahrens "parent=%s", origin_head->ds_dir->dd_myname); 3077842727c2SChris Kirby } 3078842727c2SChris Kirby 30793b2aab18SMatthew Ahrens /* 30803b2aab18SMatthew Ahrens * Given a pool name and a dataset object number in that pool, 30813b2aab18SMatthew Ahrens * return the name of that dataset. 30823b2aab18SMatthew Ahrens */ 3083a7f53a56SChris Kirby int 30843b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 3085a7f53a56SChris Kirby { 30863b2aab18SMatthew Ahrens dsl_pool_t *dp; 30873b2aab18SMatthew Ahrens dsl_dataset_t *ds; 3088a7f53a56SChris Kirby int error; 3089a7f53a56SChris Kirby 30903b2aab18SMatthew Ahrens error = dsl_pool_hold(pname, FTAG, &dp); 30913b2aab18SMatthew Ahrens if (error != 0) 30923b2aab18SMatthew Ahrens return (error); 30933b2aab18SMatthew Ahrens 30943b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 30953b2aab18SMatthew Ahrens if (error == 0) { 30963b2aab18SMatthew Ahrens dsl_dataset_name(ds, buf); 30973b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 30983b2aab18SMatthew Ahrens } 30993b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 3100a7f53a56SChris Kirby 3101a7f53a56SChris Kirby return (error); 3102a7f53a56SChris Kirby } 3103a7f53a56SChris Kirby 3104842727c2SChris Kirby int 31053b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 31063b2aab18SMatthew Ahrens uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 3107842727c2SChris Kirby { 31083b2aab18SMatthew Ahrens int error = 0; 3109842727c2SChris Kirby 31103b2aab18SMatthew Ahrens ASSERT3S(asize, >, 0); 3111842727c2SChris Kirby 31123b2aab18SMatthew Ahrens /* 31133b2aab18SMatthew Ahrens * *ref_rsrv is the portion of asize that will come from any 31143b2aab18SMatthew Ahrens * unconsumed refreservation space. 31153b2aab18SMatthew Ahrens */ 31163b2aab18SMatthew Ahrens *ref_rsrv = 0; 3117842727c2SChris Kirby 31183b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 31193b2aab18SMatthew Ahrens /* 31203b2aab18SMatthew Ahrens * Make a space adjustment for reserved bytes. 31213b2aab18SMatthew Ahrens */ 3122c1379625SJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) { 31233b2aab18SMatthew Ahrens ASSERT3U(*used, >=, 3124c1379625SJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 3125c1379625SJustin T. Gibbs *used -= 3126c1379625SJustin T. Gibbs (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 31273b2aab18SMatthew Ahrens *ref_rsrv = 31283b2aab18SMatthew Ahrens asize - MIN(asize, parent_delta(ds, asize + inflight)); 3129842727c2SChris Kirby } 3130842727c2SChris Kirby 31313b2aab18SMatthew Ahrens if (!check_quota || ds->ds_quota == 0) { 31323b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 31333b2aab18SMatthew Ahrens return (0); 3134842727c2SChris Kirby } 31353b2aab18SMatthew Ahrens /* 31363b2aab18SMatthew Ahrens * If they are requesting more space, and our current estimate 31373b2aab18SMatthew Ahrens * is over quota, they get to try again unless the actual 31383b2aab18SMatthew Ahrens * on-disk is over quota and there are no pending changes (which 31393b2aab18SMatthew Ahrens * may free up space for us). 31403b2aab18SMatthew Ahrens */ 3141c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >= 3142c1379625SJustin T. Gibbs ds->ds_quota) { 31433b2aab18SMatthew Ahrens if (inflight > 0 || 3144c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota) 3145be6fd75aSMatthew Ahrens error = SET_ERROR(ERESTART); 31463b2aab18SMatthew Ahrens else 3147be6fd75aSMatthew Ahrens error = SET_ERROR(EDQUOT); 3148842727c2SChris Kirby } 31493b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3150842727c2SChris Kirby 3151842727c2SChris Kirby return (error); 3152842727c2SChris Kirby } 3153842727c2SChris Kirby 31543b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg { 31553b2aab18SMatthew Ahrens const char *ddsqra_name; 31563b2aab18SMatthew Ahrens zprop_source_t ddsqra_source; 31573b2aab18SMatthew Ahrens uint64_t ddsqra_value; 31583b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t; 3159842727c2SChris Kirby 31603b2aab18SMatthew Ahrens 31613b2aab18SMatthew Ahrens /* ARGSUSED */ 3162842727c2SChris Kirby static int 31633b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx) 3164842727c2SChris Kirby { 31653b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 31663b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 31673b2aab18SMatthew Ahrens dsl_dataset_t *ds; 3168842727c2SChris Kirby int error; 31693b2aab18SMatthew Ahrens uint64_t newval; 3170842727c2SChris Kirby 31713b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA) 3172be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 3173842727c2SChris Kirby 31743b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 31753b2aab18SMatthew Ahrens if (error != 0) 31763b2aab18SMatthew Ahrens return (error); 31773b2aab18SMatthew Ahrens 3178bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 31793b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3180be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3181842727c2SChris Kirby } 3182842727c2SChris Kirby 31833b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 31843b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 31853b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 31863b2aab18SMatthew Ahrens if (error != 0) { 31873b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3188842727c2SChris Kirby return (error); 3189842727c2SChris Kirby } 3190842727c2SChris Kirby 31913b2aab18SMatthew Ahrens if (newval == 0) { 31923b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 31933b2aab18SMatthew Ahrens return (0); 31943b2aab18SMatthew Ahrens } 3195842727c2SChris Kirby 3196c1379625SJustin T. Gibbs if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes || 31973b2aab18SMatthew Ahrens newval < ds->ds_reserved) { 31983b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3199be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 32003b2aab18SMatthew Ahrens } 32013b2aab18SMatthew Ahrens 32023b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3203842727c2SChris Kirby return (0); 3204842727c2SChris Kirby } 3205842727c2SChris Kirby 32063b2aab18SMatthew Ahrens static void 32073b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx) 3208842727c2SChris Kirby { 32093b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 32103b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 32113b2aab18SMatthew Ahrens dsl_dataset_t *ds; 32123b2aab18SMatthew Ahrens uint64_t newval; 3213842727c2SChris Kirby 32143b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 3215842727c2SChris Kirby 32163b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, 32173b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 32183b2aab18SMatthew Ahrens ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 32193b2aab18SMatthew Ahrens &ddsqra->ddsqra_value, tx); 3220842727c2SChris Kirby 32213b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 32223b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval)); 3223842727c2SChris Kirby 32243b2aab18SMatthew Ahrens if (ds->ds_quota != newval) { 32253b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 32263b2aab18SMatthew Ahrens ds->ds_quota = newval; 3227842727c2SChris Kirby } 32283b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3229842727c2SChris Kirby } 3230842727c2SChris Kirby 32313b2aab18SMatthew Ahrens int 32323b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source, 32333b2aab18SMatthew Ahrens uint64_t refquota) 3234842727c2SChris Kirby { 32353b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 3236842727c2SChris Kirby 32373b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 32383b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 32393b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refquota; 32403b2aab18SMatthew Ahrens 32413b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check, 32427d46dc6cSMatthew Ahrens dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); 3243842727c2SChris Kirby } 3244842727c2SChris Kirby 3245842727c2SChris Kirby static int 32463b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx) 3247842727c2SChris Kirby { 32483b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 32493b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 3250842727c2SChris Kirby dsl_dataset_t *ds; 3251842727c2SChris Kirby int error; 32523b2aab18SMatthew Ahrens uint64_t newval, unique; 3253d7747cbcSChris Kirby 32543b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION) 3255be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 3256842727c2SChris Kirby 32573b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 32583b2aab18SMatthew Ahrens if (error != 0) 3259842727c2SChris Kirby return (error); 3260842727c2SChris Kirby 3261bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 32623b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3263be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3264842727c2SChris Kirby } 3265842727c2SChris Kirby 32663b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 32673b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 32683b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 32693b2aab18SMatthew Ahrens if (error != 0) { 32703b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3271842727c2SChris Kirby return (error); 3272842727c2SChris Kirby } 3273842727c2SChris Kirby 32743b2aab18SMatthew Ahrens /* 32753b2aab18SMatthew Ahrens * If we are doing the preliminary check in open context, the 32763b2aab18SMatthew Ahrens * space estimates may be inaccurate. 32773b2aab18SMatthew Ahrens */ 32783b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 32793b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 32803b2aab18SMatthew Ahrens return (0); 3281842727c2SChris Kirby } 3282842727c2SChris Kirby 32833b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 32843b2aab18SMatthew Ahrens if (!DS_UNIQUE_IS_ACCURATE(ds)) 32853b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(ds); 3286c1379625SJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes; 32873b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3288842727c2SChris Kirby 32893b2aab18SMatthew Ahrens if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) { 32903b2aab18SMatthew Ahrens uint64_t delta = MAX(unique, newval) - 32913b2aab18SMatthew Ahrens MAX(unique, ds->ds_reserved); 3292842727c2SChris Kirby 32933b2aab18SMatthew Ahrens if (delta > 32943b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) || 32953b2aab18SMatthew Ahrens (ds->ds_quota > 0 && newval > ds->ds_quota)) { 32963b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3297be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 32983b2aab18SMatthew Ahrens } 3299842727c2SChris Kirby } 3300842727c2SChris Kirby 33013b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 33023b2aab18SMatthew Ahrens return (0); 3303842727c2SChris Kirby } 3304842727c2SChris Kirby 33053b2aab18SMatthew Ahrens void 33063b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, 33073b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value, dmu_tx_t *tx) 3308ca45db41SChris Kirby { 33093b2aab18SMatthew Ahrens uint64_t newval; 33103b2aab18SMatthew Ahrens uint64_t unique; 33113b2aab18SMatthew Ahrens int64_t delta; 3312ca45db41SChris Kirby 33133b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 33143b2aab18SMatthew Ahrens source, sizeof (value), 1, &value, tx); 3315ca45db41SChris Kirby 33163b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 33173b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval)); 3318a7f53a56SChris Kirby 33193b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 33203b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 33213b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 33223b2aab18SMatthew Ahrens ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 3323c1379625SJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes; 33243b2aab18SMatthew Ahrens delta = MAX(0, (int64_t)(newval - unique)) - 33253b2aab18SMatthew Ahrens MAX(0, (int64_t)(ds->ds_reserved - unique)); 33263b2aab18SMatthew Ahrens ds->ds_reserved = newval; 33273b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3328a7f53a56SChris Kirby 33293b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 33303b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 3331ca45db41SChris Kirby } 3332ca45db41SChris Kirby 33333b2aab18SMatthew Ahrens static void 33343b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx) 3335842727c2SChris Kirby { 33363b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 33373b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 3338842727c2SChris Kirby dsl_dataset_t *ds; 3339842727c2SChris Kirby 33403b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 33413b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(ds, 33423b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx); 3343842727c2SChris Kirby dsl_dataset_rele(ds, FTAG); 3344842727c2SChris Kirby } 3345503ad85cSMatthew Ahrens 3346503ad85cSMatthew Ahrens int 33473b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source, 33483b2aab18SMatthew Ahrens uint64_t refreservation) 3349503ad85cSMatthew Ahrens { 33503b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 3351503ad85cSMatthew Ahrens 33523b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 33533b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 33543b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refreservation; 33553b2aab18SMatthew Ahrens 33563b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check, 33577d46dc6cSMatthew Ahrens dsl_dataset_set_refreservation_sync, &ddsqra, 33587d46dc6cSMatthew Ahrens 0, ZFS_SPACE_CHECK_NONE)); 3359503ad85cSMatthew Ahrens } 336019b94df9SMatthew Ahrens 336119b94df9SMatthew Ahrens /* 336219b94df9SMatthew Ahrens * Return (in *usedp) the amount of space written in new that is not 336319b94df9SMatthew Ahrens * present in oldsnap. New may be a snapshot or the head. Old must be 336419b94df9SMatthew Ahrens * a snapshot before new, in new's filesystem (or its origin). If not then 336519b94df9SMatthew Ahrens * fail and return EINVAL. 336619b94df9SMatthew Ahrens * 336719b94df9SMatthew Ahrens * The written space is calculated by considering two components: First, we 336819b94df9SMatthew Ahrens * ignore any freed space, and calculate the written as new's used space 336919b94df9SMatthew Ahrens * minus old's used space. Next, we add in the amount of space that was freed 337019b94df9SMatthew Ahrens * between the two snapshots, thus reducing new's used space relative to old's. 337119b94df9SMatthew Ahrens * Specifically, this is the space that was born before old->ds_creation_txg, 337219b94df9SMatthew Ahrens * and freed before new (ie. on new's deadlist or a previous deadlist). 337319b94df9SMatthew Ahrens * 337419b94df9SMatthew Ahrens * space freed [---------------------] 337519b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O------ 337619b94df9SMatthew Ahrens * oldsnap new 337719b94df9SMatthew Ahrens */ 337819b94df9SMatthew Ahrens int 337919b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new, 338019b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 338119b94df9SMatthew Ahrens { 338219b94df9SMatthew Ahrens int err = 0; 338319b94df9SMatthew Ahrens uint64_t snapobj; 338419b94df9SMatthew Ahrens dsl_pool_t *dp = new->ds_dir->dd_pool; 338519b94df9SMatthew Ahrens 33863b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 33873b2aab18SMatthew Ahrens 338819b94df9SMatthew Ahrens *usedp = 0; 3389c1379625SJustin T. Gibbs *usedp += dsl_dataset_phys(new)->ds_referenced_bytes; 3390c1379625SJustin T. Gibbs *usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes; 339119b94df9SMatthew Ahrens 339219b94df9SMatthew Ahrens *compp = 0; 3393c1379625SJustin T. Gibbs *compp += dsl_dataset_phys(new)->ds_compressed_bytes; 3394c1379625SJustin T. Gibbs *compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes; 339519b94df9SMatthew Ahrens 339619b94df9SMatthew Ahrens *uncompp = 0; 3397c1379625SJustin T. Gibbs *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes; 3398c1379625SJustin T. Gibbs *uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes; 339919b94df9SMatthew Ahrens 340019b94df9SMatthew Ahrens snapobj = new->ds_object; 340119b94df9SMatthew Ahrens while (snapobj != oldsnap->ds_object) { 340219b94df9SMatthew Ahrens dsl_dataset_t *snap; 340319b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 340419b94df9SMatthew Ahrens 3405ad135b5dSChristopher Siden if (snapobj == new->ds_object) { 3406ad135b5dSChristopher Siden snap = new; 3407ad135b5dSChristopher Siden } else { 3408ad135b5dSChristopher Siden err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap); 3409ad135b5dSChristopher Siden if (err != 0) 3410ad135b5dSChristopher Siden break; 3411ad135b5dSChristopher Siden } 341219b94df9SMatthew Ahrens 3413c1379625SJustin T. Gibbs if (dsl_dataset_phys(snap)->ds_prev_snap_txg == 3414c1379625SJustin T. Gibbs dsl_dataset_phys(oldsnap)->ds_creation_txg) { 341519b94df9SMatthew Ahrens /* 341619b94df9SMatthew Ahrens * The blocks in the deadlist can not be born after 341719b94df9SMatthew Ahrens * ds_prev_snap_txg, so get the whole deadlist space, 341819b94df9SMatthew Ahrens * which is more efficient (especially for old-format 341919b94df9SMatthew Ahrens * deadlists). Unfortunately the deadlist code 342019b94df9SMatthew Ahrens * doesn't have enough information to make this 342119b94df9SMatthew Ahrens * optimization itself. 342219b94df9SMatthew Ahrens */ 342319b94df9SMatthew Ahrens dsl_deadlist_space(&snap->ds_deadlist, 342419b94df9SMatthew Ahrens &used, &comp, &uncomp); 342519b94df9SMatthew Ahrens } else { 342619b94df9SMatthew Ahrens dsl_deadlist_space_range(&snap->ds_deadlist, 3427c1379625SJustin T. Gibbs 0, dsl_dataset_phys(oldsnap)->ds_creation_txg, 342819b94df9SMatthew Ahrens &used, &comp, &uncomp); 342919b94df9SMatthew Ahrens } 343019b94df9SMatthew Ahrens *usedp += used; 343119b94df9SMatthew Ahrens *compp += comp; 343219b94df9SMatthew Ahrens *uncompp += uncomp; 343319b94df9SMatthew Ahrens 343419b94df9SMatthew Ahrens /* 343519b94df9SMatthew Ahrens * If we get to the beginning of the chain of snapshots 343619b94df9SMatthew Ahrens * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap 343719b94df9SMatthew Ahrens * was not a snapshot of/before new. 343819b94df9SMatthew Ahrens */ 3439c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj; 3440ad135b5dSChristopher Siden if (snap != new) 3441ad135b5dSChristopher Siden dsl_dataset_rele(snap, FTAG); 344219b94df9SMatthew Ahrens if (snapobj == 0) { 3443be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 344419b94df9SMatthew Ahrens break; 344519b94df9SMatthew Ahrens } 344619b94df9SMatthew Ahrens 344719b94df9SMatthew Ahrens } 344819b94df9SMatthew Ahrens return (err); 344919b94df9SMatthew Ahrens } 345019b94df9SMatthew Ahrens 345119b94df9SMatthew Ahrens /* 345219b94df9SMatthew Ahrens * Return (in *usedp) the amount of space that will be reclaimed if firstsnap, 345319b94df9SMatthew Ahrens * lastsnap, and all snapshots in between are deleted. 345419b94df9SMatthew Ahrens * 345519b94df9SMatthew Ahrens * blocks that would be freed [---------------------------] 345619b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O--------O 345719b94df9SMatthew Ahrens * firstsnap lastsnap 345819b94df9SMatthew Ahrens * 345919b94df9SMatthew Ahrens * This is the set of blocks that were born after the snap before firstsnap, 346019b94df9SMatthew Ahrens * (birth > firstsnap->prev_snap_txg) and died before the snap after the 346119b94df9SMatthew Ahrens * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist). 346219b94df9SMatthew Ahrens * We calculate this by iterating over the relevant deadlists (from the snap 346319b94df9SMatthew Ahrens * after lastsnap, backward to the snap after firstsnap), summing up the 346419b94df9SMatthew Ahrens * space on the deadlist that was born after the snap before firstsnap. 346519b94df9SMatthew Ahrens */ 346619b94df9SMatthew Ahrens int 346719b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap, 346819b94df9SMatthew Ahrens dsl_dataset_t *lastsnap, 346919b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 347019b94df9SMatthew Ahrens { 347119b94df9SMatthew Ahrens int err = 0; 347219b94df9SMatthew Ahrens uint64_t snapobj; 347319b94df9SMatthew Ahrens dsl_pool_t *dp = firstsnap->ds_dir->dd_pool; 347419b94df9SMatthew Ahrens 3475bc9014e6SJustin Gibbs ASSERT(firstsnap->ds_is_snapshot); 3476bc9014e6SJustin Gibbs ASSERT(lastsnap->ds_is_snapshot); 347719b94df9SMatthew Ahrens 347819b94df9SMatthew Ahrens /* 347919b94df9SMatthew Ahrens * Check that the snapshots are in the same dsl_dir, and firstsnap 348019b94df9SMatthew Ahrens * is before lastsnap. 348119b94df9SMatthew Ahrens */ 348219b94df9SMatthew Ahrens if (firstsnap->ds_dir != lastsnap->ds_dir || 3483c1379625SJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_creation_txg > 3484c1379625SJustin T. Gibbs dsl_dataset_phys(lastsnap)->ds_creation_txg) 3485be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 348619b94df9SMatthew Ahrens 348719b94df9SMatthew Ahrens *usedp = *compp = *uncompp = 0; 348819b94df9SMatthew Ahrens 3489c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj; 349019b94df9SMatthew Ahrens while (snapobj != firstsnap->ds_object) { 349119b94df9SMatthew Ahrens dsl_dataset_t *ds; 349219b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 349319b94df9SMatthew Ahrens 349419b94df9SMatthew Ahrens err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds); 349519b94df9SMatthew Ahrens if (err != 0) 349619b94df9SMatthew Ahrens break; 349719b94df9SMatthew Ahrens 349819b94df9SMatthew Ahrens dsl_deadlist_space_range(&ds->ds_deadlist, 3499c1379625SJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX, 350019b94df9SMatthew Ahrens &used, &comp, &uncomp); 350119b94df9SMatthew Ahrens *usedp += used; 350219b94df9SMatthew Ahrens *compp += comp; 350319b94df9SMatthew Ahrens *uncompp += uncomp; 350419b94df9SMatthew Ahrens 3505c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 350619b94df9SMatthew Ahrens ASSERT3U(snapobj, !=, 0); 350719b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 350819b94df9SMatthew Ahrens } 350919b94df9SMatthew Ahrens return (err); 351019b94df9SMatthew Ahrens } 35113b2aab18SMatthew Ahrens 35123b2aab18SMatthew Ahrens /* 35133b2aab18SMatthew Ahrens * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline. 35143b2aab18SMatthew Ahrens * For example, they could both be snapshots of the same filesystem, and 35153b2aab18SMatthew Ahrens * 'earlier' is before 'later'. Or 'earlier' could be the origin of 35163b2aab18SMatthew Ahrens * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's 35173b2aab18SMatthew Ahrens * filesystem. Or 'earlier' could be the origin's origin. 351878f17100SMatthew Ahrens * 351978f17100SMatthew Ahrens * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg. 35203b2aab18SMatthew Ahrens */ 35213b2aab18SMatthew Ahrens boolean_t 352278f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier, 35239a686fbcSPaul Dagnelie uint64_t earlier_txg) 35243b2aab18SMatthew Ahrens { 35253b2aab18SMatthew Ahrens dsl_pool_t *dp = later->ds_dir->dd_pool; 35263b2aab18SMatthew Ahrens int error; 35273b2aab18SMatthew Ahrens boolean_t ret; 35283b2aab18SMatthew Ahrens 35293b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 3530bc9014e6SJustin Gibbs ASSERT(earlier->ds_is_snapshot || earlier_txg != 0); 353178f17100SMatthew Ahrens 353278f17100SMatthew Ahrens if (earlier_txg == 0) 3533c1379625SJustin T. Gibbs earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg; 35343b2aab18SMatthew Ahrens 3535bc9014e6SJustin Gibbs if (later->ds_is_snapshot && 3536c1379625SJustin T. Gibbs earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg) 35373b2aab18SMatthew Ahrens return (B_FALSE); 35383b2aab18SMatthew Ahrens 35393b2aab18SMatthew Ahrens if (later->ds_dir == earlier->ds_dir) 35403b2aab18SMatthew Ahrens return (B_TRUE); 35413b2aab18SMatthew Ahrens if (!dsl_dir_is_clone(later->ds_dir)) 35423b2aab18SMatthew Ahrens return (B_FALSE); 35433b2aab18SMatthew Ahrens 3544c1379625SJustin T. Gibbs if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object) 35453b2aab18SMatthew Ahrens return (B_TRUE); 35463b2aab18SMatthew Ahrens dsl_dataset_t *origin; 35473b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 3548c1379625SJustin T. Gibbs dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin); 35493b2aab18SMatthew Ahrens if (error != 0) 35503b2aab18SMatthew Ahrens return (B_FALSE); 355178f17100SMatthew Ahrens ret = dsl_dataset_is_before(origin, earlier, earlier_txg); 35523b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 35533b2aab18SMatthew Ahrens return (ret); 35543b2aab18SMatthew Ahrens } 35552acef22dSMatthew Ahrens 35562acef22dSMatthew Ahrens void 35572acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx) 35582acef22dSMatthew Ahrens { 35592acef22dSMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 35602acef22dSMatthew Ahrens dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx); 35612acef22dSMatthew Ahrens } 35629c3fd121SMatthew Ahrens 35639c3fd121SMatthew Ahrens boolean_t 35649c3fd121SMatthew Ahrens dsl_dataset_is_zapified(dsl_dataset_t *ds) 35659c3fd121SMatthew Ahrens { 35669c3fd121SMatthew Ahrens dmu_object_info_t doi; 35679c3fd121SMatthew Ahrens 35689c3fd121SMatthew Ahrens dmu_object_info_from_db(ds->ds_dbuf, &doi); 35699c3fd121SMatthew Ahrens return (doi.doi_type == DMU_OTN_ZAP_METADATA); 35709c3fd121SMatthew Ahrens } 35719c3fd121SMatthew Ahrens 35729c3fd121SMatthew Ahrens boolean_t 35739c3fd121SMatthew Ahrens dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds) 35749c3fd121SMatthew Ahrens { 35759c3fd121SMatthew Ahrens return (dsl_dataset_is_zapified(ds) && 35769c3fd121SMatthew Ahrens zap_contains(ds->ds_dir->dd_pool->dp_meta_objset, 35779c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0); 35789c3fd121SMatthew Ahrens } 3579