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 */ 211c17160aSKevin Crowe 22fa9e4066Sahrens /* 235afc78aaSChris Kirby * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24*000cce6bSBrad Lewis * Copyright (c) 2011, 2017 by Delphix. All rights reserved. 25a2afb611SJerry Jelinek * Copyright (c) 2014, Joyent, Inc. All rights reserved. 2603d1795fSAlexander Stetsenko * Copyright (c) 2014 RackTop Systems. 27bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 28c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 295f7a8e6dSDan McDonald * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved. 301c17160aSKevin Crowe * Copyright 2017 Nexenta Systems, Inc. 31fa9e4066Sahrens */ 32fa9e4066Sahrens 33fa9e4066Sahrens #include <sys/dmu_objset.h> 34fa9e4066Sahrens #include <sys/dsl_dataset.h> 35fa9e4066Sahrens #include <sys/dsl_dir.h> 3699653d4eSeschrock #include <sys/dsl_prop.h> 371d452cf5Sahrens #include <sys/dsl_synctask.h> 38fa9e4066Sahrens #include <sys/dmu_traverse.h> 394e3c9f44SBill Pijewski #include <sys/dmu_impl.h> 40fa9e4066Sahrens #include <sys/dmu_tx.h> 41fa9e4066Sahrens #include <sys/arc.h> 42fa9e4066Sahrens #include <sys/zio.h> 43fa9e4066Sahrens #include <sys/zap.h> 44ad135b5dSChristopher Siden #include <sys/zfeature.h> 45fa9e4066Sahrens #include <sys/unique.h> 46fa9e4066Sahrens #include <sys/zfs_context.h> 47cdf5b4caSmmusante #include <sys/zfs_ioctl.h> 48ecd6cf80Smarks #include <sys/spa.h> 49088f3894Sahrens #include <sys/zfs_znode.h> 50c99e4bdcSChris Kirby #include <sys/zfs_onexit.h> 51842727c2SChris Kirby #include <sys/zvol.h> 523f9d6ad7SLin Ling #include <sys/dsl_scan.h> 53cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h> 543b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 553b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h> 5678f17100SMatthew Ahrens #include <sys/dsl_bookmark.h> 5745818ee1SMatthew Ahrens #include <sys/dmu_send.h> 5845818ee1SMatthew Ahrens #include <sys/zio_checksum.h> 599c3fd121SMatthew Ahrens #include <sys/zio_compress.h> 609c3fd121SMatthew Ahrens #include <zfs_fletcher.h> 61e1930233Sbonwick 62b5152584SMatthew Ahrens /* 63b5152584SMatthew Ahrens * The SPA supports block sizes up to 16MB. However, very large blocks 64b5152584SMatthew Ahrens * can have an impact on i/o latency (e.g. tying up a spinning disk for 65b5152584SMatthew Ahrens * ~300ms), and also potentially on the memory allocator. Therefore, 66b5152584SMatthew Ahrens * we do not allow the recordsize to be set larger than zfs_max_recordsize 67b5152584SMatthew Ahrens * (default 1MB). Larger blocks can be created by changing this tunable, 68b5152584SMatthew Ahrens * and pools with larger blocks can always be imported and used, regardless 69b5152584SMatthew Ahrens * of this setting. 70b5152584SMatthew Ahrens */ 71b5152584SMatthew Ahrens int zfs_max_recordsize = 1 * 1024 * 1024; 72b5152584SMatthew Ahrens 73cde58dbcSMatthew Ahrens #define SWITCH64(x, y) \ 74cde58dbcSMatthew Ahrens { \ 75cde58dbcSMatthew Ahrens uint64_t __tmp = (x); \ 76cde58dbcSMatthew Ahrens (x) = (y); \ 77cde58dbcSMatthew Ahrens (y) = __tmp; \ 78cde58dbcSMatthew Ahrens } 79cde58dbcSMatthew Ahrens 8055434c77Sek #define DS_REF_MAX (1ULL << 62) 81fa9e4066Sahrens 82c1379625SJustin T. Gibbs extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds); 83c1379625SJustin T. Gibbs 845f7a8e6dSDan McDonald extern int spa_asize_inflation; 855f7a8e6dSDan McDonald 86bfaed0b9SAndriy Gapon static zil_header_t zero_zil; 87bfaed0b9SAndriy Gapon 88a9799022Sck /* 89a9799022Sck * Figure out how much of this delta should be propogated to the dsl_dir 90a9799022Sck * layer. If there's a refreservation, that space has already been 91a9799022Sck * partially accounted for in our ancestors. 92a9799022Sck */ 93a9799022Sck static int64_t 94a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta) 95a9799022Sck { 96c1379625SJustin T. Gibbs dsl_dataset_phys_t *ds_phys; 97a9799022Sck uint64_t old_bytes, new_bytes; 98a9799022Sck 99a9799022Sck if (ds->ds_reserved == 0) 100a9799022Sck return (delta); 101a9799022Sck 102c1379625SJustin T. Gibbs ds_phys = dsl_dataset_phys(ds); 103c1379625SJustin T. Gibbs old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved); 104c1379625SJustin T. Gibbs new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 105a9799022Sck 106a9799022Sck ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 107a9799022Sck return (new_bytes - old_bytes); 108a9799022Sck } 109fa9e4066Sahrens 110fa9e4066Sahrens void 111b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx) 112fa9e4066Sahrens { 113b24ab676SJeff Bonwick int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 114fa9e4066Sahrens int compressed = BP_GET_PSIZE(bp); 115fa9e4066Sahrens int uncompressed = BP_GET_UCSIZE(bp); 116a9799022Sck int64_t delta; 117fa9e4066Sahrens 1183f9d6ad7SLin Ling dprintf_bp(bp, "ds=%p", ds); 119fa9e4066Sahrens 120fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 121fa9e4066Sahrens /* It could have been compressed away to nothing */ 122fa9e4066Sahrens if (BP_IS_HOLE(bp)) 123fa9e4066Sahrens return; 124fa9e4066Sahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 125ad135b5dSChristopher Siden ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp))); 126fa9e4066Sahrens if (ds == NULL) { 127ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 128ce636f8bSMatthew Ahrens used, compressed, uncompressed); 129fa9e4066Sahrens return; 130fa9e4066Sahrens } 1313f9d6ad7SLin Ling 132bfaed0b9SAndriy Gapon ASSERT3U(bp->blk_birth, >, dsl_dataset_phys(ds)->ds_prev_snap_txg); 133b62969f8SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 134fa9e4066Sahrens mutex_enter(&ds->ds_lock); 135a9799022Sck delta = parent_delta(ds, used); 136c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes += used; 137c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes += compressed; 138c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed; 139c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes += used; 14045818ee1SMatthew Ahrens 141ca0cc391SMatthew Ahrens if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) { 142ca0cc391SMatthew Ahrens ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] = 143ca0cc391SMatthew Ahrens B_TRUE; 144ca0cc391SMatthew Ahrens } 14545818ee1SMatthew Ahrens 14645818ee1SMatthew Ahrens spa_feature_t f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp)); 14745818ee1SMatthew Ahrens if (f != SPA_FEATURE_NONE) 14845818ee1SMatthew Ahrens ds->ds_feature_activation_needed[f] = B_TRUE; 14945818ee1SMatthew Ahrens 150fa9e4066Sahrens mutex_exit(&ds->ds_lock); 15174e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta, 15274e7dc98SMatthew Ahrens compressed, uncompressed, tx); 15374e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used - delta, 15474e7dc98SMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx); 155fa9e4066Sahrens } 156fa9e4066Sahrens 157cdb0ab79Smaybee int 158b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx, 159b24ab676SJeff Bonwick boolean_t async) 160fa9e4066Sahrens { 16143466aaeSMax Grossman int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 16243466aaeSMax Grossman int compressed = BP_GET_PSIZE(bp); 16343466aaeSMax Grossman int uncompressed = BP_GET_UCSIZE(bp); 16443466aaeSMax Grossman 165fa9e4066Sahrens if (BP_IS_HOLE(bp)) 166cdb0ab79Smaybee return (0); 167fa9e4066Sahrens 168b24ab676SJeff Bonwick ASSERT(dmu_tx_is_syncing(tx)); 169b24ab676SJeff Bonwick ASSERT(bp->blk_birth <= tx->tx_txg); 170b24ab676SJeff Bonwick 171fa9e4066Sahrens if (ds == NULL) { 172b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 173ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 174ce636f8bSMatthew Ahrens -used, -compressed, -uncompressed); 175cdb0ab79Smaybee return (used); 176fa9e4066Sahrens } 177fa9e4066Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 178fa9e4066Sahrens 179bc9014e6SJustin Gibbs ASSERT(!ds->ds_is_snapshot); 180fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 181fa9e4066Sahrens 182c1379625SJustin T. Gibbs if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) { 183a9799022Sck int64_t delta; 184c717a561Smaybee 1853f9d6ad7SLin Ling dprintf_bp(bp, "freeing ds=%llu", ds->ds_object); 186b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 187fa9e4066Sahrens 188fa9e4066Sahrens mutex_enter(&ds->ds_lock); 189c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used || 190a9799022Sck !DS_UNIQUE_IS_ACCURATE(ds)); 191a9799022Sck delta = parent_delta(ds, -used); 192c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes -= used; 193fa9e4066Sahrens mutex_exit(&ds->ds_lock); 19474e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 195a9799022Sck delta, -compressed, -uncompressed, tx); 19674e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, -used - delta, 19774e7dc98SMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx); 198fa9e4066Sahrens } else { 199fa9e4066Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 200b24ab676SJeff Bonwick if (async) { 201b24ab676SJeff Bonwick /* 202b24ab676SJeff Bonwick * We are here as part of zio's write done callback, 203b24ab676SJeff Bonwick * which means we're a zio interrupt thread. We can't 204cde58dbcSMatthew Ahrens * call dsl_deadlist_insert() now because it may block 205b24ab676SJeff Bonwick * waiting for I/O. Instead, put bp on the deferred 206b24ab676SJeff Bonwick * queue and let dsl_pool_sync() finish the job. 207b24ab676SJeff Bonwick */ 208cde58dbcSMatthew Ahrens bplist_append(&ds->ds_pending_deadlist, bp); 209b24ab676SJeff Bonwick } else { 210cde58dbcSMatthew Ahrens dsl_deadlist_insert(&ds->ds_deadlist, bp, tx); 211b24ab676SJeff Bonwick } 212a4611edeSahrens ASSERT3U(ds->ds_prev->ds_object, ==, 213c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj); 214c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0); 215fa9e4066Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 216c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 217a4611edeSahrens ds->ds_object && bp->blk_birth > 218c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) { 219a4611edeSahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 220a4611edeSahrens mutex_enter(&ds->ds_prev->ds_lock); 221c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used; 222a4611edeSahrens mutex_exit(&ds->ds_prev->ds_lock); 223fa9e4066Sahrens } 2243f9d6ad7SLin Ling if (bp->blk_birth > ds->ds_dir->dd_origin_txg) { 22574e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used, 22674e7dc98SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 22774e7dc98SMatthew Ahrens } 228fa9e4066Sahrens } 229fa9e4066Sahrens mutex_enter(&ds->ds_lock); 230c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used); 231c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes -= used; 232c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed); 233c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed; 234c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed); 235c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed; 236fa9e4066Sahrens mutex_exit(&ds->ds_lock); 237cdb0ab79Smaybee 238cdb0ab79Smaybee return (used); 239fa9e4066Sahrens } 240fa9e4066Sahrens 24140510e8eSJosef 'Jeff' Sipek /* 24240510e8eSJosef 'Jeff' Sipek * We have to release the fsid syncronously or we risk that a subsequent 24340510e8eSJosef 'Jeff' Sipek * mount of the same dataset will fail to unique_insert the fsid. This 24440510e8eSJosef 'Jeff' Sipek * failure would manifest itself as the fsid of this dataset changing 24540510e8eSJosef 'Jeff' Sipek * between mounts which makes NFS clients quite unhappy. 24640510e8eSJosef 'Jeff' Sipek */ 247fa9e4066Sahrens static void 24840510e8eSJosef 'Jeff' Sipek dsl_dataset_evict_sync(void *dbu) 249fa9e4066Sahrens { 250bc9014e6SJustin Gibbs dsl_dataset_t *ds = dbu; 251fa9e4066Sahrens 2523b2aab18SMatthew Ahrens ASSERT(ds->ds_owner == NULL); 253fa9e4066Sahrens 25491ebeef5Sahrens unique_remove(ds->ds_fsid_guid); 25540510e8eSJosef 'Jeff' Sipek } 25640510e8eSJosef 'Jeff' Sipek 25740510e8eSJosef 'Jeff' Sipek static void 25840510e8eSJosef 'Jeff' Sipek dsl_dataset_evict_async(void *dbu) 25940510e8eSJosef 'Jeff' Sipek { 26040510e8eSJosef 'Jeff' Sipek dsl_dataset_t *ds = dbu; 26140510e8eSJosef 'Jeff' Sipek 26240510e8eSJosef 'Jeff' Sipek ASSERT(ds->ds_owner == NULL); 26340510e8eSJosef 'Jeff' Sipek 26440510e8eSJosef 'Jeff' Sipek ds->ds_dbuf = NULL; 265fa9e4066Sahrens 266503ad85cSMatthew Ahrens if (ds->ds_objset != NULL) 267503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 268fa9e4066Sahrens 269fa9e4066Sahrens if (ds->ds_prev) { 2703b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 271fa9e4066Sahrens ds->ds_prev = NULL; 272fa9e4066Sahrens } 273fa9e4066Sahrens 274cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 275bc9014e6SJustin Gibbs if (ds->ds_deadlist.dl_os != NULL) 276cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 277745cd3c5Smaybee if (ds->ds_dir) 278bc9014e6SJustin Gibbs dsl_dir_async_rele(ds->ds_dir, ds); 279fa9e4066Sahrens 28091ebeef5Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 281fa9e4066Sahrens 28203bad06fSJustin Gibbs list_destroy(&ds->ds_prop_cbs); 2835ad82045Snd mutex_destroy(&ds->ds_lock); 28491ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 285d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 2863b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 287c166b69dSPaul Dagnelie rrw_destroy(&ds->ds_bp_rwlock); 2885ad82045Snd 289fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 290fa9e4066Sahrens } 291fa9e4066Sahrens 2923b2aab18SMatthew Ahrens int 293fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 294fa9e4066Sahrens { 295fa9e4066Sahrens dsl_dataset_phys_t *headphys; 296fa9e4066Sahrens int err; 297fa9e4066Sahrens dmu_buf_t *headdbuf; 298fa9e4066Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 299fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 300fa9e4066Sahrens 301fa9e4066Sahrens if (ds->ds_snapname[0]) 302ea8dc4b6Seschrock return (0); 303c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) 304ea8dc4b6Seschrock return (0); 305fa9e4066Sahrens 306c1379625SJustin T. Gibbs err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, 307ea8dc4b6Seschrock FTAG, &headdbuf); 3083b2aab18SMatthew Ahrens if (err != 0) 309ea8dc4b6Seschrock return (err); 310fa9e4066Sahrens headphys = headdbuf->db_data; 311fa9e4066Sahrens err = zap_value_search(dp->dp_meta_objset, 312e7437265Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 313ea8dc4b6Seschrock dmu_buf_rele(headdbuf, FTAG); 314ea8dc4b6Seschrock return (err); 315fa9e4066Sahrens } 316fa9e4066Sahrens 3173b2aab18SMatthew Ahrens int 318745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 319ab04eb8eStimh { 320745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 321c1379625SJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 3221c17160aSKevin Crowe matchtype_t mt = 0; 323ab04eb8eStimh int err; 324ab04eb8eStimh 325c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 3261c17160aSKevin Crowe mt = MT_NORMALIZE; 327ab04eb8eStimh 328745cd3c5Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 329ab04eb8eStimh value, mt, NULL, 0, NULL); 3301c17160aSKevin Crowe if (err == ENOTSUP && (mt & MT_NORMALIZE)) 331745cd3c5Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 332ab04eb8eStimh return (err); 333ab04eb8eStimh } 334ab04eb8eStimh 3353b2aab18SMatthew Ahrens int 336a2afb611SJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx, 337a2afb611SJerry Jelinek boolean_t adj_cnt) 338ab04eb8eStimh { 339745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 340c1379625SJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 3411c17160aSKevin Crowe matchtype_t mt = 0; 342ab04eb8eStimh int err; 343ab04eb8eStimh 34471eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 34571eb0538SChris Kirby 346c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 3471c17160aSKevin Crowe mt = MT_NORMALIZE; 348ab04eb8eStimh 349745cd3c5Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 3501c17160aSKevin Crowe if (err == ENOTSUP && (mt & MT_NORMALIZE)) 351745cd3c5Smaybee err = zap_remove(mos, snapobj, name, tx); 352a2afb611SJerry Jelinek 353a2afb611SJerry Jelinek if (err == 0 && adj_cnt) 354a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, -1, 355a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 356a2afb611SJerry Jelinek 357ab04eb8eStimh return (err); 358ab04eb8eStimh } 359ab04eb8eStimh 360e57a022bSJustin T. Gibbs boolean_t 361e57a022bSJustin T. Gibbs dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag) 362e57a022bSJustin T. Gibbs { 3639d47dec0SJustin T. Gibbs dmu_buf_t *dbuf = ds->ds_dbuf; 3649d47dec0SJustin T. Gibbs boolean_t result = B_FALSE; 3659d47dec0SJustin T. Gibbs 3669d47dec0SJustin T. Gibbs if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset, 3679d47dec0SJustin T. Gibbs ds->ds_object, DMU_BONUS_BLKID, tag)) { 3689d47dec0SJustin T. Gibbs 3699d47dec0SJustin T. Gibbs if (ds == dmu_buf_get_user(dbuf)) 3709d47dec0SJustin T. Gibbs result = B_TRUE; 3719d47dec0SJustin T. Gibbs else 3729d47dec0SJustin T. Gibbs dmu_buf_rele(dbuf, tag); 3739d47dec0SJustin T. Gibbs } 3749d47dec0SJustin T. Gibbs 3759d47dec0SJustin T. Gibbs return (result); 376e57a022bSJustin T. Gibbs } 377e57a022bSJustin T. Gibbs 3783b2aab18SMatthew Ahrens int 3793b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 380745cd3c5Smaybee dsl_dataset_t **dsp) 381fa9e4066Sahrens { 382fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 383fa9e4066Sahrens dmu_buf_t *dbuf; 384fa9e4066Sahrens dsl_dataset_t *ds; 385ea8dc4b6Seschrock int err; 386a7f53a56SChris Kirby dmu_object_info_t doi; 387fa9e4066Sahrens 3883b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 389fa9e4066Sahrens 390ea8dc4b6Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 3913b2aab18SMatthew Ahrens if (err != 0) 392ea8dc4b6Seschrock return (err); 393a7f53a56SChris Kirby 394a7f53a56SChris Kirby /* Make sure dsobj has the correct object type. */ 395a7f53a56SChris Kirby dmu_object_info_from_db(dbuf, &doi); 3962acef22dSMatthew Ahrens if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) { 397b287be1bSWill Andrews dmu_buf_rele(dbuf, tag); 398be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 399b287be1bSWill Andrews } 400a7f53a56SChris Kirby 401fa9e4066Sahrens ds = dmu_buf_get_user(dbuf); 402fa9e4066Sahrens if (ds == NULL) { 403d5285caeSGeorge Wilson dsl_dataset_t *winner = NULL; 404fa9e4066Sahrens 405fa9e4066Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 406fa9e4066Sahrens ds->ds_dbuf = dbuf; 407fa9e4066Sahrens ds->ds_object = dsobj; 408bc9014e6SJustin Gibbs ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0; 409fa9e4066Sahrens 4105ad82045Snd mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 41191ebeef5Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 4124e3c9f44SBill Pijewski mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL); 413c166b69dSPaul Dagnelie rrw_init(&ds->ds_bp_rwlock, B_FALSE); 4143b2aab18SMatthew Ahrens refcount_create(&ds->ds_longholds); 4155ad82045Snd 416cde58dbcSMatthew Ahrens bplist_create(&ds->ds_pending_deadlist); 417cde58dbcSMatthew Ahrens dsl_deadlist_open(&ds->ds_deadlist, 418c1379625SJustin T. Gibbs mos, dsl_dataset_phys(ds)->ds_deadlist_obj); 419cde58dbcSMatthew Ahrens 4204e3c9f44SBill Pijewski list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t), 4214e3c9f44SBill Pijewski offsetof(dmu_sendarg_t, dsa_link)); 4224e3c9f44SBill Pijewski 42303bad06fSJustin Gibbs list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t), 42403bad06fSJustin Gibbs offsetof(dsl_prop_cb_record_t, cbr_ds_node)); 42503bad06fSJustin Gibbs 426b5152584SMatthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 427ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 428ca0cc391SMatthew Ahrens if (!(spa_feature_table[f].fi_flags & 429ca0cc391SMatthew Ahrens ZFEATURE_FLAG_PER_DATASET)) 430ca0cc391SMatthew Ahrens continue; 431ca0cc391SMatthew Ahrens err = zap_contains(mos, dsobj, 432ca0cc391SMatthew Ahrens spa_feature_table[f].fi_guid); 433ca0cc391SMatthew Ahrens if (err == 0) { 434ca0cc391SMatthew Ahrens ds->ds_feature_inuse[f] = B_TRUE; 435ca0cc391SMatthew Ahrens } else { 436ca0cc391SMatthew Ahrens ASSERT3U(err, ==, ENOENT); 437ca0cc391SMatthew Ahrens err = 0; 438ca0cc391SMatthew Ahrens } 439e1f3c208SJustin T. Gibbs } 440b5152584SMatthew Ahrens } 441b5152584SMatthew Ahrens 442ca0cc391SMatthew Ahrens err = dsl_dir_hold_obj(dp, 443ca0cc391SMatthew Ahrens dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir); 4443b2aab18SMatthew Ahrens if (err != 0) { 4455ad82045Snd mutex_destroy(&ds->ds_lock); 44691ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 447d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 4483b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 449cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 450cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 451ea8dc4b6Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 452ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 453ea8dc4b6Seschrock return (err); 454ea8dc4b6Seschrock } 455fa9e4066Sahrens 456bc9014e6SJustin Gibbs if (!ds->ds_is_snapshot) { 457fa9e4066Sahrens ds->ds_snapname[0] = '\0'; 458c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 4593b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, 460c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, 461745cd3c5Smaybee ds, &ds->ds_prev); 462fa9e4066Sahrens } 46378f17100SMatthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 46478f17100SMatthew Ahrens int zaperr = zap_lookup(mos, ds->ds_object, 46578f17100SMatthew Ahrens DS_FIELD_BOOKMARK_NAMES, 46678f17100SMatthew Ahrens sizeof (ds->ds_bookmarks), 1, 46778f17100SMatthew Ahrens &ds->ds_bookmarks); 46878f17100SMatthew Ahrens if (zaperr != ENOENT) 46978f17100SMatthew Ahrens VERIFY0(zaperr); 47078f17100SMatthew Ahrens } 471842727c2SChris Kirby } else { 472842727c2SChris Kirby if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 473842727c2SChris Kirby err = dsl_dataset_get_snapname(ds); 474c1379625SJustin T. Gibbs if (err == 0 && 475c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj != 0) { 476842727c2SChris Kirby err = zap_count( 477842727c2SChris Kirby ds->ds_dir->dd_pool->dp_meta_objset, 478c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj, 479842727c2SChris Kirby &ds->ds_userrefs); 480842727c2SChris Kirby } 481fa9e4066Sahrens } 482fa9e4066Sahrens 483bc9014e6SJustin Gibbs if (err == 0 && !ds->ds_is_snapshot) { 4843b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 4853b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 4863b2aab18SMatthew Ahrens &ds->ds_reserved); 487cb625fb5Sck if (err == 0) { 4883b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 4893b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 4903b2aab18SMatthew Ahrens &ds->ds_quota); 491cb625fb5Sck } 492cb625fb5Sck } else { 493cb625fb5Sck ds->ds_reserved = ds->ds_quota = 0; 494cb625fb5Sck } 495cb625fb5Sck 49640510e8eSJosef 'Jeff' Sipek dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync, 49740510e8eSJosef 'Jeff' Sipek dsl_dataset_evict_async, &ds->ds_dbuf); 498bc9014e6SJustin Gibbs if (err == 0) 499bc9014e6SJustin Gibbs winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu); 500bc9014e6SJustin Gibbs 501bc9014e6SJustin Gibbs if (err != 0 || winner != NULL) { 502cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 503cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 504745cd3c5Smaybee if (ds->ds_prev) 5053b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 5063b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 5075ad82045Snd mutex_destroy(&ds->ds_lock); 50891ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 509d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 5103b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 511fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 5123b2aab18SMatthew Ahrens if (err != 0) { 513ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 514ea8dc4b6Seschrock return (err); 515ea8dc4b6Seschrock } 516fa9e4066Sahrens ds = winner; 517fa9e4066Sahrens } else { 51891ebeef5Sahrens ds->ds_fsid_guid = 519c1379625SJustin T. Gibbs unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid); 52040510e8eSJosef 'Jeff' Sipek if (ds->ds_fsid_guid != 52140510e8eSJosef 'Jeff' Sipek dsl_dataset_phys(ds)->ds_fsid_guid) { 52240510e8eSJosef 'Jeff' Sipek zfs_dbgmsg("ds_fsid_guid changed from " 52340510e8eSJosef 'Jeff' Sipek "%llx to %llx for pool %s dataset id %llu", 52440510e8eSJosef 'Jeff' Sipek (long long) 52540510e8eSJosef 'Jeff' Sipek dsl_dataset_phys(ds)->ds_fsid_guid, 52640510e8eSJosef 'Jeff' Sipek (long long)ds->ds_fsid_guid, 52740510e8eSJosef 'Jeff' Sipek spa_name(dp->dp_spa), 52840510e8eSJosef 'Jeff' Sipek dsobj); 52940510e8eSJosef 'Jeff' Sipek } 530fa9e4066Sahrens } 531fa9e4066Sahrens } 532fa9e4066Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 533c1379625SJustin T. Gibbs ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data); 534c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 || 535afc6333aSahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 53684db2a68Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 537ea8dc4b6Seschrock *dsp = ds; 538ea8dc4b6Seschrock return (0); 539fa9e4066Sahrens } 540fa9e4066Sahrens 541745cd3c5Smaybee int 5423b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name, 543503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 544fa9e4066Sahrens { 545fa9e4066Sahrens dsl_dir_t *dd; 546745cd3c5Smaybee const char *snapname; 547fa9e4066Sahrens uint64_t obj; 548fa9e4066Sahrens int err = 0; 549a2cdcdd2SPaul Dagnelie dsl_dataset_t *ds; 550fa9e4066Sahrens 5513b2aab18SMatthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname); 5523b2aab18SMatthew Ahrens if (err != 0) 553ea8dc4b6Seschrock return (err); 554fa9e4066Sahrens 5553b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 556c1379625SJustin T. Gibbs obj = dsl_dir_phys(dd)->dd_head_dataset_obj; 5573b2aab18SMatthew Ahrens if (obj != 0) 558a2cdcdd2SPaul Dagnelie err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 559745cd3c5Smaybee else 560be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 561fa9e4066Sahrens 562745cd3c5Smaybee /* we may be looking for a snapshot */ 563745cd3c5Smaybee if (err == 0 && snapname != NULL) { 564a2cdcdd2SPaul Dagnelie dsl_dataset_t *snap_ds; 565fa9e4066Sahrens 566745cd3c5Smaybee if (*snapname++ != '@') { 567a2cdcdd2SPaul Dagnelie dsl_dataset_rele(ds, tag); 5683b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 569be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 570fa9e4066Sahrens } 571fa9e4066Sahrens 572745cd3c5Smaybee dprintf("looking for snapshot '%s'\n", snapname); 573a2cdcdd2SPaul Dagnelie err = dsl_dataset_snap_lookup(ds, snapname, &obj); 574745cd3c5Smaybee if (err == 0) 575a2cdcdd2SPaul Dagnelie err = dsl_dataset_hold_obj(dp, obj, tag, &snap_ds); 576a2cdcdd2SPaul Dagnelie dsl_dataset_rele(ds, tag); 577745cd3c5Smaybee 5783b2aab18SMatthew Ahrens if (err == 0) { 579a2cdcdd2SPaul Dagnelie mutex_enter(&snap_ds->ds_lock); 580a2cdcdd2SPaul Dagnelie if (snap_ds->ds_snapname[0] == 0) 581a2cdcdd2SPaul Dagnelie (void) strlcpy(snap_ds->ds_snapname, snapname, 582a2cdcdd2SPaul Dagnelie sizeof (snap_ds->ds_snapname)); 583a2cdcdd2SPaul Dagnelie mutex_exit(&snap_ds->ds_lock); 584a2cdcdd2SPaul Dagnelie ds = snap_ds; 585fa9e4066Sahrens } 586fa9e4066Sahrens } 587a2cdcdd2SPaul Dagnelie if (err == 0) 588a2cdcdd2SPaul Dagnelie *dsp = ds; 5893b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 590fa9e4066Sahrens return (err); 591fa9e4066Sahrens } 592fa9e4066Sahrens 593fa9e4066Sahrens int 5943b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, 5953b2aab18SMatthew Ahrens void *tag, dsl_dataset_t **dsp) 5963b2aab18SMatthew Ahrens { 5973b2aab18SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 5983b2aab18SMatthew Ahrens if (err != 0) 5993b2aab18SMatthew Ahrens return (err); 6003b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 6013b2aab18SMatthew Ahrens dsl_dataset_rele(*dsp, tag); 6023b2aab18SMatthew Ahrens *dsp = NULL; 603be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 6043b2aab18SMatthew Ahrens } 6053b2aab18SMatthew Ahrens return (0); 6063b2aab18SMatthew Ahrens } 6073b2aab18SMatthew Ahrens 6083b2aab18SMatthew Ahrens int 6093b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name, 610503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 611fa9e4066Sahrens { 6123b2aab18SMatthew Ahrens int err = dsl_dataset_hold(dp, name, tag, dsp); 6133b2aab18SMatthew Ahrens if (err != 0) 614745cd3c5Smaybee return (err); 6153b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 616503ad85cSMatthew Ahrens dsl_dataset_rele(*dsp, tag); 617be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 618745cd3c5Smaybee } 619745cd3c5Smaybee return (0); 620fa9e4066Sahrens } 621fa9e4066Sahrens 6223b2aab18SMatthew Ahrens /* 6233b2aab18SMatthew Ahrens * See the comment above dsl_pool_hold() for details. In summary, a long 6243b2aab18SMatthew Ahrens * hold is used to prevent destruction of a dataset while the pool hold 6253b2aab18SMatthew Ahrens * is dropped, allowing other concurrent operations (e.g. spa_sync()). 6263b2aab18SMatthew Ahrens * 6273b2aab18SMatthew Ahrens * The dataset and pool must be held when this function is called. After it 6283b2aab18SMatthew Ahrens * is called, the pool hold may be released while the dataset is still held 6293b2aab18SMatthew Ahrens * and accessed. 6303b2aab18SMatthew Ahrens */ 6313b2aab18SMatthew Ahrens void 6323b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag) 6333b2aab18SMatthew Ahrens { 6343b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 6353b2aab18SMatthew Ahrens (void) refcount_add(&ds->ds_longholds, tag); 6363b2aab18SMatthew Ahrens } 6373b2aab18SMatthew Ahrens 6383b2aab18SMatthew Ahrens void 6393b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag) 6403b2aab18SMatthew Ahrens { 6413b2aab18SMatthew Ahrens (void) refcount_remove(&ds->ds_longholds, tag); 6423b2aab18SMatthew Ahrens } 6433b2aab18SMatthew Ahrens 6443b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */ 6453b2aab18SMatthew Ahrens boolean_t 6463b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds) 6473b2aab18SMatthew Ahrens { 6483b2aab18SMatthew Ahrens return (!refcount_is_zero(&ds->ds_longholds)); 6493b2aab18SMatthew Ahrens } 6503b2aab18SMatthew Ahrens 651fa9e4066Sahrens void 652fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 653fa9e4066Sahrens { 654fa9e4066Sahrens if (ds == NULL) { 655fa9e4066Sahrens (void) strcpy(name, "mos"); 656fa9e4066Sahrens } else { 657fa9e4066Sahrens dsl_dir_name(ds->ds_dir, name); 6583b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 659fa9e4066Sahrens if (ds->ds_snapname[0]) { 6609adfa60dSMatthew Ahrens VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN), 6619adfa60dSMatthew Ahrens <, ZFS_MAX_DATASET_NAME_LEN); 662745cd3c5Smaybee /* 663745cd3c5Smaybee * We use a "recursive" mutex so that we 664745cd3c5Smaybee * can call dprintf_ds() with ds_lock held. 665745cd3c5Smaybee */ 666fa9e4066Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 667fa9e4066Sahrens mutex_enter(&ds->ds_lock); 6689adfa60dSMatthew Ahrens VERIFY3U(strlcat(name, ds->ds_snapname, 6699adfa60dSMatthew Ahrens ZFS_MAX_DATASET_NAME_LEN), <, 6709adfa60dSMatthew Ahrens ZFS_MAX_DATASET_NAME_LEN); 671fa9e4066Sahrens mutex_exit(&ds->ds_lock); 672fa9e4066Sahrens } else { 6739adfa60dSMatthew Ahrens VERIFY3U(strlcat(name, ds->ds_snapname, 6749adfa60dSMatthew Ahrens ZFS_MAX_DATASET_NAME_LEN), <, 6759adfa60dSMatthew Ahrens ZFS_MAX_DATASET_NAME_LEN); 676fa9e4066Sahrens } 677fa9e4066Sahrens } 678fa9e4066Sahrens } 679fa9e4066Sahrens } 680fa9e4066Sahrens 6819adfa60dSMatthew Ahrens int 6829adfa60dSMatthew Ahrens dsl_dataset_namelen(dsl_dataset_t *ds) 6839adfa60dSMatthew Ahrens { 6849adfa60dSMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 6859adfa60dSMatthew Ahrens mutex_enter(&ds->ds_lock); 6869adfa60dSMatthew Ahrens int len = dsl_dir_namelen(ds->ds_dir) + 1 + strlen(ds->ds_snapname); 6879adfa60dSMatthew Ahrens mutex_exit(&ds->ds_lock); 6889adfa60dSMatthew Ahrens return (len); 6899adfa60dSMatthew Ahrens } 6909adfa60dSMatthew Ahrens 6913cb34c60Sahrens void 692745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6933cb34c60Sahrens { 6943b2aab18SMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, tag); 695745cd3c5Smaybee } 696745cd3c5Smaybee 697745cd3c5Smaybee void 698503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 699745cd3c5Smaybee { 700d808a4fcSJustin T. Gibbs ASSERT3P(ds->ds_owner, ==, tag); 701d808a4fcSJustin T. Gibbs ASSERT(ds->ds_dbuf != NULL); 702745cd3c5Smaybee 7033cb34c60Sahrens mutex_enter(&ds->ds_lock); 704745cd3c5Smaybee ds->ds_owner = NULL; 7053cb34c60Sahrens mutex_exit(&ds->ds_lock); 7063b2aab18SMatthew Ahrens dsl_dataset_long_rele(ds, tag); 707d808a4fcSJustin T. Gibbs dsl_dataset_rele(ds, tag); 7083cb34c60Sahrens } 7093cb34c60Sahrens 7103cb34c60Sahrens boolean_t 7113b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag) 7123cb34c60Sahrens { 713745cd3c5Smaybee boolean_t gotit = FALSE; 714745cd3c5Smaybee 7159c3fd121SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 7163cb34c60Sahrens mutex_enter(&ds->ds_lock); 7173b2aab18SMatthew Ahrens if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) { 718503ad85cSMatthew Ahrens ds->ds_owner = tag; 7193b2aab18SMatthew Ahrens dsl_dataset_long_hold(ds, tag); 720745cd3c5Smaybee gotit = TRUE; 7213cb34c60Sahrens } 7223cb34c60Sahrens mutex_exit(&ds->ds_lock); 723745cd3c5Smaybee return (gotit); 724745cd3c5Smaybee } 725745cd3c5Smaybee 7269c3fd121SMatthew Ahrens boolean_t 7279c3fd121SMatthew Ahrens dsl_dataset_has_owner(dsl_dataset_t *ds) 7289c3fd121SMatthew Ahrens { 7299c3fd121SMatthew Ahrens boolean_t rv; 7309c3fd121SMatthew Ahrens mutex_enter(&ds->ds_lock); 7319c3fd121SMatthew Ahrens rv = (ds->ds_owner != NULL); 7329c3fd121SMatthew Ahrens mutex_exit(&ds->ds_lock); 7339c3fd121SMatthew Ahrens return (rv); 7349c3fd121SMatthew Ahrens } 7359c3fd121SMatthew Ahrens 736ca0cc391SMatthew Ahrens static void 737ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx) 738ca0cc391SMatthew Ahrens { 739ca0cc391SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 740ca0cc391SMatthew Ahrens objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 741ca0cc391SMatthew Ahrens uint64_t zero = 0; 742ca0cc391SMatthew Ahrens 743ca0cc391SMatthew Ahrens VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET); 744ca0cc391SMatthew Ahrens 745ca0cc391SMatthew Ahrens spa_feature_incr(spa, f, tx); 746ca0cc391SMatthew Ahrens dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx); 747ca0cc391SMatthew Ahrens 748ca0cc391SMatthew Ahrens VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid, 749ca0cc391SMatthew Ahrens sizeof (zero), 1, &zero, tx)); 750ca0cc391SMatthew Ahrens } 751ca0cc391SMatthew Ahrens 752ca0cc391SMatthew Ahrens void 753ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx) 754ca0cc391SMatthew Ahrens { 755ca0cc391SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 756ca0cc391SMatthew Ahrens objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 757ca0cc391SMatthew Ahrens 758ca0cc391SMatthew Ahrens VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET); 759ca0cc391SMatthew Ahrens 760ca0cc391SMatthew Ahrens VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx)); 761ca0cc391SMatthew Ahrens spa_feature_decr(spa, f, tx); 762ca0cc391SMatthew Ahrens } 763ca0cc391SMatthew Ahrens 7641d452cf5Sahrens uint64_t 765088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 766ab04eb8eStimh uint64_t flags, dmu_tx_t *tx) 767fa9e4066Sahrens { 7683cb34c60Sahrens dsl_pool_t *dp = dd->dd_pool; 769fa9e4066Sahrens dmu_buf_t *dbuf; 770fa9e4066Sahrens dsl_dataset_phys_t *dsphys; 7713cb34c60Sahrens uint64_t dsobj; 772fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 773fa9e4066Sahrens 774088f3894Sahrens if (origin == NULL) 775088f3894Sahrens origin = dp->dp_origin_snap; 776088f3894Sahrens 7773cb34c60Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 778c1379625SJustin T. Gibbs ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0); 779fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 780c1379625SJustin T. Gibbs ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0); 781fa9e4066Sahrens 7821649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 7831649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 7843b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 785fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 786fa9e4066Sahrens dsphys = dbuf->db_data; 787745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 788fa9e4066Sahrens dsphys->ds_dir_obj = dd->dd_object; 789ab04eb8eStimh dsphys->ds_flags = flags; 790fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 791fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 792fa9e4066Sahrens sizeof (dsphys->ds_guid)); 793fa9e4066Sahrens dsphys->ds_snapnames_zapobj = 794ab04eb8eStimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 795ab04eb8eStimh DMU_OT_NONE, 0, tx); 796fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 797088f3894Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 798a9799022Sck 799cde58dbcSMatthew Ahrens if (origin == NULL) { 800cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx); 801cde58dbcSMatthew Ahrens } else { 8023b2aab18SMatthew Ahrens dsl_dataset_t *ohds; /* head of the origin snapshot */ 803cde58dbcSMatthew Ahrens 8043cb34c60Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 805fa9e4066Sahrens dsphys->ds_prev_snap_txg = 806c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_creation_txg; 807ad135b5dSChristopher Siden dsphys->ds_referenced_bytes = 808c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_referenced_bytes; 809fa9e4066Sahrens dsphys->ds_compressed_bytes = 810c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_compressed_bytes; 811fa9e4066Sahrens dsphys->ds_uncompressed_bytes = 812c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_uncompressed_bytes; 813c166b69dSPaul Dagnelie rrw_enter(&origin->ds_bp_rwlock, RW_READER, FTAG); 814c1379625SJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp; 815c166b69dSPaul Dagnelie rrw_exit(&origin->ds_bp_rwlock, FTAG); 81642fcb65eSMatthew Ahrens 81742fcb65eSMatthew Ahrens /* 81842fcb65eSMatthew Ahrens * Inherit flags that describe the dataset's contents 81942fcb65eSMatthew Ahrens * (INCONSISTENT) or properties (Case Insensitive). 82042fcb65eSMatthew Ahrens */ 821c1379625SJustin T. Gibbs dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags & 82242fcb65eSMatthew Ahrens (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET); 823fa9e4066Sahrens 824ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 825ca0cc391SMatthew Ahrens if (origin->ds_feature_inuse[f]) 826ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(dsobj, f, tx); 827ca0cc391SMatthew Ahrens } 828b5152584SMatthew Ahrens 8293cb34c60Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 830c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_num_children++; 831fa9e4066Sahrens 8323b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 833c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj, 834c1379625SJustin T. Gibbs FTAG, &ohds)); 835cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist, 836cde58dbcSMatthew Ahrens dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx); 837cde58dbcSMatthew Ahrens dsl_dataset_rele(ohds, FTAG); 838cde58dbcSMatthew Ahrens 839088f3894Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 840c1379625SJustin T. Gibbs if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) { 841c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj = 842088f3894Sahrens zap_create(mos, 843088f3894Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 844088f3894Sahrens } 8453b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 846c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj, 847c1379625SJustin T. Gibbs dsobj, tx)); 848088f3894Sahrens } 849088f3894Sahrens 850fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 851c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object; 852cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 853c1379625SJustin T. Gibbs if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) { 854cde58dbcSMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 855c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones = 856cde58dbcSMatthew Ahrens zap_create(mos, 857cde58dbcSMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 858cde58dbcSMatthew Ahrens } 8593b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 860c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones, 861c1379625SJustin T. Gibbs dsobj, tx)); 862cde58dbcSMatthew Ahrens } 863fa9e4066Sahrens } 864ab04eb8eStimh 865ab04eb8eStimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 866ab04eb8eStimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 867ab04eb8eStimh 868ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 869fa9e4066Sahrens 870fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 871c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj; 8723cb34c60Sahrens 8733cb34c60Sahrens return (dsobj); 8743cb34c60Sahrens } 8753cb34c60Sahrens 8763b2aab18SMatthew Ahrens static void 8773b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx) 8783b2aab18SMatthew Ahrens { 8793b2aab18SMatthew Ahrens objset_t *os; 8803b2aab18SMatthew Ahrens 8813b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os)); 882bfaed0b9SAndriy Gapon if (bcmp(&os->os_zil_header, &zero_zil, sizeof (zero_zil)) != 0) { 883bfaed0b9SAndriy Gapon dsl_pool_t *dp = ds->ds_dir->dd_pool; 884bfaed0b9SAndriy Gapon zio_t *zio; 885bfaed0b9SAndriy Gapon 886bfaed0b9SAndriy Gapon bzero(&os->os_zil_header, sizeof (os->os_zil_header)); 887bfaed0b9SAndriy Gapon 888bfaed0b9SAndriy Gapon zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 889bfaed0b9SAndriy Gapon dsl_dataset_sync(ds, zio, tx); 890bfaed0b9SAndriy Gapon VERIFY0(zio_wait(zio)); 891bfaed0b9SAndriy Gapon 892bfaed0b9SAndriy Gapon /* dsl_dataset_sync_done will drop this reference. */ 893bfaed0b9SAndriy Gapon dmu_buf_add_ref(ds->ds_dbuf, ds); 894bfaed0b9SAndriy Gapon dsl_dataset_sync_done(ds, tx); 895bfaed0b9SAndriy Gapon } 8963b2aab18SMatthew Ahrens } 8973b2aab18SMatthew Ahrens 8983cb34c60Sahrens uint64_t 899ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 900ab04eb8eStimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 9013cb34c60Sahrens { 9023cb34c60Sahrens dsl_pool_t *dp = pdd->dd_pool; 9033cb34c60Sahrens uint64_t dsobj, ddobj; 9043cb34c60Sahrens dsl_dir_t *dd; 9053cb34c60Sahrens 9063b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 9073cb34c60Sahrens ASSERT(lastname[0] != '@'); 9083cb34c60Sahrens 909088f3894Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 9103b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd)); 9113cb34c60Sahrens 9123b2aab18SMatthew Ahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, 9133b2aab18SMatthew Ahrens flags & ~DS_CREATE_FLAG_NODIRTY, tx); 9143cb34c60Sahrens 9153cb34c60Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 9163cb34c60Sahrens 917a2afb611SJerry Jelinek /* 918a2afb611SJerry Jelinek * Since we're creating a new node we know it's a leaf, so we can 919a2afb611SJerry Jelinek * initialize the counts if the limit feature is active. 920a2afb611SJerry Jelinek */ 921a2afb611SJerry Jelinek if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { 922a2afb611SJerry Jelinek uint64_t cnt = 0; 923a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 924a2afb611SJerry Jelinek 925a2afb611SJerry Jelinek dsl_dir_zapify(dd, tx); 926a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 927a2afb611SJerry Jelinek sizeof (cnt), 1, &cnt, tx)); 928a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 929a2afb611SJerry Jelinek sizeof (cnt), 1, &cnt, tx)); 930a2afb611SJerry Jelinek } 931a2afb611SJerry Jelinek 9323b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 933fa9e4066Sahrens 934feaa74e4SMark Maybee /* 935feaa74e4SMark Maybee * If we are creating a clone, make sure we zero out any stale 936feaa74e4SMark Maybee * data from the origin snapshots zil header. 937feaa74e4SMark Maybee */ 9383b2aab18SMatthew Ahrens if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) { 939feaa74e4SMark Maybee dsl_dataset_t *ds; 940feaa74e4SMark Maybee 9413b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 9423b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 943feaa74e4SMark Maybee dsl_dataset_rele(ds, FTAG); 944feaa74e4SMark Maybee } 945feaa74e4SMark Maybee 9461d452cf5Sahrens return (dsobj); 947fa9e4066Sahrens } 948fa9e4066Sahrens 9491d452cf5Sahrens /* 9503b2aab18SMatthew Ahrens * The unique space in the head dataset can be calculated by subtracting 9513b2aab18SMatthew Ahrens * the space used in the most recent snapshot, that is still being used 9523b2aab18SMatthew Ahrens * in this file system, from the space currently in use. To figure out 9533b2aab18SMatthew Ahrens * the space in the most recent snapshot still in use, we need to take 9543b2aab18SMatthew Ahrens * the total space used in the snapshot and subtract out the space that 9553b2aab18SMatthew Ahrens * has been freed up since the snapshot was taken. 9561d452cf5Sahrens */ 9573b2aab18SMatthew Ahrens void 9583b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 9591d452cf5Sahrens { 9603b2aab18SMatthew Ahrens uint64_t mrs_used; 9613b2aab18SMatthew Ahrens uint64_t dlused, dlcomp, dluncomp; 9621d452cf5Sahrens 963bc9014e6SJustin Gibbs ASSERT(!ds->ds_is_snapshot); 9641d452cf5Sahrens 965c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) 966c1379625SJustin T. Gibbs mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes; 9673b2aab18SMatthew Ahrens else 9683b2aab18SMatthew Ahrens mrs_used = 0; 969842727c2SChris Kirby 9703b2aab18SMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp); 971fa9e4066Sahrens 9723b2aab18SMatthew Ahrens ASSERT3U(dlused, <=, mrs_used); 973c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes = 974c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused); 97519b94df9SMatthew Ahrens 9763b2aab18SMatthew Ahrens if (spa_version(ds->ds_dir->dd_pool->dp_spa) >= 9773b2aab18SMatthew Ahrens SPA_VERSION_UNIQUE_ACCURATE) 978c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 979fa9e4066Sahrens } 980fa9e4066Sahrens 9813b2aab18SMatthew Ahrens void 9823b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, 9833b2aab18SMatthew Ahrens dmu_tx_t *tx) 984842727c2SChris Kirby { 9853b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 9863b2aab18SMatthew Ahrens uint64_t count; 9873b2aab18SMatthew Ahrens int err; 9883b2aab18SMatthew Ahrens 989c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2); 990c1379625SJustin T. Gibbs err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 991c1379625SJustin T. Gibbs obj, tx); 9923b2aab18SMatthew Ahrens /* 9933b2aab18SMatthew Ahrens * The err should not be ENOENT, but a bug in a previous version 9943b2aab18SMatthew Ahrens * of the code could cause upgrade_clones_cb() to not set 9953b2aab18SMatthew Ahrens * ds_next_snap_obj when it should, leading to a missing entry. 9963b2aab18SMatthew Ahrens * If we knew that the pool was created after 9973b2aab18SMatthew Ahrens * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 9983b2aab18SMatthew Ahrens * ENOENT. However, at least we can check that we don't have 9993b2aab18SMatthew Ahrens * too many entries in the next_clones_obj even after failing to 10003b2aab18SMatthew Ahrens * remove this one. 10013b2aab18SMatthew Ahrens */ 10023b2aab18SMatthew Ahrens if (err != ENOENT) 10033b2aab18SMatthew Ahrens VERIFY0(err); 1004c1379625SJustin T. Gibbs ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 10053b2aab18SMatthew Ahrens &count)); 1006c1379625SJustin T. Gibbs ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2); 10073b2aab18SMatthew Ahrens } 1008842727c2SChris Kirby 1009842727c2SChris Kirby 10103b2aab18SMatthew Ahrens blkptr_t * 10113b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds) 10123b2aab18SMatthew Ahrens { 1013c1379625SJustin T. Gibbs return (&dsl_dataset_phys(ds)->ds_bp); 1014842727c2SChris Kirby } 1015842727c2SChris Kirby 10163b2aab18SMatthew Ahrens spa_t * 10173b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 10183b2aab18SMatthew Ahrens { 10193b2aab18SMatthew Ahrens return (ds->ds_dir->dd_pool->dp_spa); 1020842727c2SChris Kirby } 1021842727c2SChris Kirby 10223b2aab18SMatthew Ahrens void 10233b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1024fa9e4066Sahrens { 10253b2aab18SMatthew Ahrens dsl_pool_t *dp; 1026842727c2SChris Kirby 10273b2aab18SMatthew Ahrens if (ds == NULL) /* this is the meta-objset */ 10283b2aab18SMatthew Ahrens return; 10291d452cf5Sahrens 10303b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 1031fa9e4066Sahrens 1032c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) 10333b2aab18SMatthew Ahrens panic("dirtying snapshot!"); 1034fa9e4066Sahrens 1035bfaed0b9SAndriy Gapon /* Must not dirty a dataset in the same txg where it got snapshotted. */ 1036bfaed0b9SAndriy Gapon ASSERT3U(tx->tx_txg, >, dsl_dataset_phys(ds)->ds_prev_snap_txg); 1037ce636f8bSMatthew Ahrens 1038bfaed0b9SAndriy Gapon dp = ds->ds_dir->dd_pool; 10393b2aab18SMatthew Ahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) { 10403b2aab18SMatthew Ahrens /* up the hold count until we can be written out */ 10413b2aab18SMatthew Ahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 10423b2aab18SMatthew Ahrens } 10433b2aab18SMatthew Ahrens } 1044fa9e4066Sahrens 10452e2c1355SMatthew Ahrens boolean_t 10462e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds) 10472e2c1355SMatthew Ahrens { 10482e2c1355SMatthew Ahrens for (int t = 0; t < TXG_SIZE; t++) { 10492e2c1355SMatthew Ahrens if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets, 10502e2c1355SMatthew Ahrens ds, t)) 10512e2c1355SMatthew Ahrens return (B_TRUE); 10522e2c1355SMatthew Ahrens } 10532e2c1355SMatthew Ahrens return (B_FALSE); 10542e2c1355SMatthew Ahrens } 10552e2c1355SMatthew Ahrens 1056fa9e4066Sahrens static int 10573b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 1058fa9e4066Sahrens { 10593b2aab18SMatthew Ahrens uint64_t asize; 1060fa9e4066Sahrens 10613b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 106288b7b0f2SMatthew Ahrens return (0); 1063fa9e4066Sahrens 1064e1930233Sbonwick /* 10653b2aab18SMatthew Ahrens * If there's an fs-only reservation, any blocks that might become 10663b2aab18SMatthew Ahrens * owned by the snapshot dataset must be accommodated by space 10673b2aab18SMatthew Ahrens * outside of the reservation. 1068e1930233Sbonwick */ 10693b2aab18SMatthew Ahrens ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds)); 1070c1379625SJustin T. Gibbs asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved); 10713b2aab18SMatthew Ahrens if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 1072be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 1073e1930233Sbonwick 10743cb34c60Sahrens /* 10753b2aab18SMatthew Ahrens * Propagate any reserved space for this snapshot to other 10763b2aab18SMatthew Ahrens * snapshot checks in this sync group. 10773cb34c60Sahrens */ 10783b2aab18SMatthew Ahrens if (asize > 0) 10793b2aab18SMatthew Ahrens dsl_dir_willuse_space(ds->ds_dir, asize, tx); 10803cb34c60Sahrens 1081e1930233Sbonwick return (0); 1082e1930233Sbonwick } 1083e1930233Sbonwick 10843b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg { 10853b2aab18SMatthew Ahrens nvlist_t *ddsa_snaps; 10863b2aab18SMatthew Ahrens nvlist_t *ddsa_props; 10873b2aab18SMatthew Ahrens nvlist_t *ddsa_errors; 1088a2afb611SJerry Jelinek cred_t *ddsa_cr; 10893b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t; 1090842727c2SChris Kirby 10913cb34c60Sahrens int 10923b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname, 1093a2afb611SJerry Jelinek dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr) 10941d452cf5Sahrens { 10953b2aab18SMatthew Ahrens int error; 10963b2aab18SMatthew Ahrens uint64_t value; 1097fa9e4066Sahrens 10983b2aab18SMatthew Ahrens ds->ds_trysnap_txg = tx->tx_txg; 1099745cd3c5Smaybee 11003b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 1101842727c2SChris Kirby return (0); 1102fa9e4066Sahrens 1103fa9e4066Sahrens /* 11043b2aab18SMatthew Ahrens * We don't allow multiple snapshots of the same txg. If there 11053b2aab18SMatthew Ahrens * is already one, try again. 1106fa9e4066Sahrens */ 1107c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) 1108be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 1109fa9e4066Sahrens 1110fa9e4066Sahrens /* 11113b2aab18SMatthew Ahrens * Check for conflicting snapshot name. 1112fa9e4066Sahrens */ 11133b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(ds, snapname, &value); 11143b2aab18SMatthew Ahrens if (error == 0) 1115be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 11163b2aab18SMatthew Ahrens if (error != ENOENT) 11173b2aab18SMatthew Ahrens return (error); 1118842727c2SChris Kirby 1119ca48f36fSKeith M Wesolowski /* 1120ca48f36fSKeith M Wesolowski * We don't allow taking snapshots of inconsistent datasets, such as 1121ca48f36fSKeith M Wesolowski * those into which we are currently receiving. However, if we are 1122ca48f36fSKeith M Wesolowski * creating this snapshot as part of a receive, this check will be 1123ca48f36fSKeith M Wesolowski * executed atomically with respect to the completion of the receive 1124ca48f36fSKeith M Wesolowski * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this 1125ca48f36fSKeith M Wesolowski * case we ignore this, knowing it will be fixed up for us shortly in 1126ca48f36fSKeith M Wesolowski * dmu_recv_end_sync(). 1127ca48f36fSKeith M Wesolowski */ 1128ca48f36fSKeith M Wesolowski if (!recv && DS_IS_INCONSISTENT(ds)) 1129ca48f36fSKeith M Wesolowski return (SET_ERROR(EBUSY)); 1130ca48f36fSKeith M Wesolowski 1131a2afb611SJerry Jelinek /* 1132a2afb611SJerry Jelinek * Skip the check for temporary snapshots or if we have already checked 1133a2afb611SJerry Jelinek * the counts in dsl_dataset_snapshot_check. This means we really only 1134a2afb611SJerry Jelinek * check the count here when we're receiving a stream. 1135a2afb611SJerry Jelinek */ 1136a2afb611SJerry Jelinek if (cnt != 0 && cr != NULL) { 1137a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1138a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr); 1139a2afb611SJerry Jelinek if (error != 0) 1140a2afb611SJerry Jelinek return (error); 1141a2afb611SJerry Jelinek } 1142a2afb611SJerry Jelinek 11433b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_reserve_space(ds, tx); 11443b2aab18SMatthew Ahrens if (error != 0) 11453b2aab18SMatthew Ahrens return (error); 1146842727c2SChris Kirby 11471d452cf5Sahrens return (0); 11481d452cf5Sahrens } 11491d452cf5Sahrens 11503b2aab18SMatthew Ahrens static int 11513b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx) 1152745cd3c5Smaybee { 11533b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 11543b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 11553b2aab18SMatthew Ahrens nvpair_t *pair; 11563b2aab18SMatthew Ahrens int rv = 0; 11573b2aab18SMatthew Ahrens 1158a2afb611SJerry Jelinek /* 1159a2afb611SJerry Jelinek * Pre-compute how many total new snapshots will be created for each 1160a2afb611SJerry Jelinek * level in the tree and below. This is needed for validating the 1161a2afb611SJerry Jelinek * snapshot limit when either taking a recursive snapshot or when 1162a2afb611SJerry Jelinek * taking multiple snapshots. 1163a2afb611SJerry Jelinek * 1164a2afb611SJerry Jelinek * The problem is that the counts are not actually adjusted when 1165a2afb611SJerry Jelinek * we are checking, only when we finally sync. For a single snapshot, 1166a2afb611SJerry Jelinek * this is easy, the count will increase by 1 at each node up the tree, 1167a2afb611SJerry Jelinek * but its more complicated for the recursive/multiple snapshot case. 1168a2afb611SJerry Jelinek * 1169a2afb611SJerry Jelinek * The dsl_fs_ss_limit_check function does recursively check the count 1170a2afb611SJerry Jelinek * at each level up the tree but since it is validating each snapshot 1171a2afb611SJerry Jelinek * independently we need to be sure that we are validating the complete 1172a2afb611SJerry Jelinek * count for the entire set of snapshots. We do this by rolling up the 1173a2afb611SJerry Jelinek * counts for each component of the name into an nvlist and then 1174a2afb611SJerry Jelinek * checking each of those cases with the aggregated count. 1175a2afb611SJerry Jelinek * 1176a2afb611SJerry Jelinek * This approach properly handles not only the recursive snapshot 1177a2afb611SJerry Jelinek * case (where we get all of those on the ddsa_snaps list) but also 1178a2afb611SJerry Jelinek * the sibling case (e.g. snapshot a/b and a/c so that we will also 1179a2afb611SJerry Jelinek * validate the limit on 'a' using a count of 2). 1180a2afb611SJerry Jelinek * 1181a2afb611SJerry Jelinek * We validate the snapshot names in the third loop and only report 1182a2afb611SJerry Jelinek * name errors once. 1183a2afb611SJerry Jelinek */ 1184a2afb611SJerry Jelinek if (dmu_tx_is_syncing(tx)) { 1185a2afb611SJerry Jelinek nvlist_t *cnt_track = NULL; 1186a2afb611SJerry Jelinek cnt_track = fnvlist_alloc(); 1187a2afb611SJerry Jelinek 1188a2afb611SJerry Jelinek /* Rollup aggregated counts into the cnt_track list */ 1189a2afb611SJerry Jelinek for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 1190a2afb611SJerry Jelinek pair != NULL; 1191a2afb611SJerry Jelinek pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 1192a2afb611SJerry Jelinek char *pdelim; 1193a2afb611SJerry Jelinek uint64_t val; 1194a2afb611SJerry Jelinek char nm[MAXPATHLEN]; 1195a2afb611SJerry Jelinek 1196a2afb611SJerry Jelinek (void) strlcpy(nm, nvpair_name(pair), sizeof (nm)); 1197a2afb611SJerry Jelinek pdelim = strchr(nm, '@'); 1198a2afb611SJerry Jelinek if (pdelim == NULL) 1199a2afb611SJerry Jelinek continue; 1200a2afb611SJerry Jelinek *pdelim = '\0'; 1201a2afb611SJerry Jelinek 1202a2afb611SJerry Jelinek do { 1203a2afb611SJerry Jelinek if (nvlist_lookup_uint64(cnt_track, nm, 1204a2afb611SJerry Jelinek &val) == 0) { 1205a2afb611SJerry Jelinek /* update existing entry */ 1206a2afb611SJerry Jelinek fnvlist_add_uint64(cnt_track, nm, 1207a2afb611SJerry Jelinek val + 1); 1208a2afb611SJerry Jelinek } else { 1209a2afb611SJerry Jelinek /* add to list */ 1210a2afb611SJerry Jelinek fnvlist_add_uint64(cnt_track, nm, 1); 1211a2afb611SJerry Jelinek } 1212a2afb611SJerry Jelinek 1213a2afb611SJerry Jelinek pdelim = strrchr(nm, '/'); 1214a2afb611SJerry Jelinek if (pdelim != NULL) 1215a2afb611SJerry Jelinek *pdelim = '\0'; 1216a2afb611SJerry Jelinek } while (pdelim != NULL); 1217a2afb611SJerry Jelinek } 1218a2afb611SJerry Jelinek 1219a2afb611SJerry Jelinek /* Check aggregated counts at each level */ 1220a2afb611SJerry Jelinek for (pair = nvlist_next_nvpair(cnt_track, NULL); 1221a2afb611SJerry Jelinek pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) { 1222a2afb611SJerry Jelinek int error = 0; 1223a2afb611SJerry Jelinek char *name; 1224a2afb611SJerry Jelinek uint64_t cnt = 0; 1225a2afb611SJerry Jelinek dsl_dataset_t *ds; 1226a2afb611SJerry Jelinek 1227a2afb611SJerry Jelinek name = nvpair_name(pair); 1228a2afb611SJerry Jelinek cnt = fnvpair_value_uint64(pair); 1229a2afb611SJerry Jelinek ASSERT(cnt > 0); 1230a2afb611SJerry Jelinek 1231a2afb611SJerry Jelinek error = dsl_dataset_hold(dp, name, FTAG, &ds); 1232a2afb611SJerry Jelinek if (error == 0) { 1233a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1234a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, 1235a2afb611SJerry Jelinek ddsa->ddsa_cr); 1236a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 1237a2afb611SJerry Jelinek } 1238a2afb611SJerry Jelinek 1239a2afb611SJerry Jelinek if (error != 0) { 1240a2afb611SJerry Jelinek if (ddsa->ddsa_errors != NULL) 1241a2afb611SJerry Jelinek fnvlist_add_int32(ddsa->ddsa_errors, 1242a2afb611SJerry Jelinek name, error); 1243a2afb611SJerry Jelinek rv = error; 1244a2afb611SJerry Jelinek /* only report one error for this check */ 1245a2afb611SJerry Jelinek break; 1246a2afb611SJerry Jelinek } 1247a2afb611SJerry Jelinek } 1248a2afb611SJerry Jelinek nvlist_free(cnt_track); 1249a2afb611SJerry Jelinek } 1250a2afb611SJerry Jelinek 12513b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 12523b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 12533b2aab18SMatthew Ahrens int error = 0; 12543b2aab18SMatthew Ahrens dsl_dataset_t *ds; 12553b2aab18SMatthew Ahrens char *name, *atp; 12569adfa60dSMatthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN]; 12573b2aab18SMatthew Ahrens 12583b2aab18SMatthew Ahrens name = nvpair_name(pair); 12599adfa60dSMatthew Ahrens if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN) 1260be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 12613b2aab18SMatthew Ahrens if (error == 0) { 12623b2aab18SMatthew Ahrens atp = strchr(name, '@'); 12633b2aab18SMatthew Ahrens if (atp == NULL) 1264be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 12653b2aab18SMatthew Ahrens if (error == 0) 12663b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 12673b2aab18SMatthew Ahrens } 12683b2aab18SMatthew Ahrens if (error == 0) 12693b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 12703b2aab18SMatthew Ahrens if (error == 0) { 1271a2afb611SJerry Jelinek /* passing 0/NULL skips dsl_fs_ss_limit_check */ 12723b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_check_impl(ds, 1273a2afb611SJerry Jelinek atp + 1, tx, B_FALSE, 0, NULL); 12743b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12753b2aab18SMatthew Ahrens } 1276745cd3c5Smaybee 12773b2aab18SMatthew Ahrens if (error != 0) { 12783b2aab18SMatthew Ahrens if (ddsa->ddsa_errors != NULL) { 12793b2aab18SMatthew Ahrens fnvlist_add_int32(ddsa->ddsa_errors, 12803b2aab18SMatthew Ahrens name, error); 12813b2aab18SMatthew Ahrens } 12823b2aab18SMatthew Ahrens rv = error; 12833b2aab18SMatthew Ahrens } 12843b2aab18SMatthew Ahrens } 1285a2afb611SJerry Jelinek 12863b2aab18SMatthew Ahrens return (rv); 1287745cd3c5Smaybee } 1288745cd3c5Smaybee 12893b2aab18SMatthew Ahrens void 12903b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname, 12913b2aab18SMatthew Ahrens dmu_tx_t *tx) 1292745cd3c5Smaybee { 12933b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 12943b2aab18SMatthew Ahrens dmu_buf_t *dbuf; 12953b2aab18SMatthew Ahrens dsl_dataset_phys_t *dsphys; 12963b2aab18SMatthew Ahrens uint64_t dsobj, crtxg; 12973b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 12983b2aab18SMatthew Ahrens objset_t *os; 1299745cd3c5Smaybee 13003b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1301c33e334fSMatthew Ahrens 1302c33e334fSMatthew Ahrens /* 13033b2aab18SMatthew Ahrens * If we are on an old pool, the zil must not be active, in which 13043b2aab18SMatthew Ahrens * case it will be zeroed. Usually zil_suspend() accomplishes this. 1305c33e334fSMatthew Ahrens */ 13063b2aab18SMatthew Ahrens ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP || 13073b2aab18SMatthew Ahrens dmu_objset_from_ds(ds, &os) != 0 || 13083b2aab18SMatthew Ahrens bcmp(&os->os_phys->os_zil_header, &zero_zil, 13093b2aab18SMatthew Ahrens sizeof (zero_zil)) == 0); 1310c33e334fSMatthew Ahrens 1311bfaed0b9SAndriy Gapon /* Should not snapshot a dirty dataset. */ 1312bfaed0b9SAndriy Gapon ASSERT(!txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets, 1313bfaed0b9SAndriy Gapon ds, tx->tx_txg)); 1314bfaed0b9SAndriy Gapon 1315a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx); 1316cde58dbcSMatthew Ahrens 1317cde58dbcSMatthew Ahrens /* 13183b2aab18SMatthew Ahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 1319088f3894Sahrens */ 1320088f3894Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 1321088f3894Sahrens crtxg = 1; 1322088f3894Sahrens else 1323088f3894Sahrens crtxg = tx->tx_txg; 1324088f3894Sahrens 13251649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 13261649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 13273b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1328fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 1329fa9e4066Sahrens dsphys = dbuf->db_data; 1330745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 13311d452cf5Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1332fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 1333fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1334fa9e4066Sahrens sizeof (dsphys->ds_guid)); 1335c1379625SJustin T. Gibbs dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 1336c1379625SJustin T. Gibbs dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 1337fa9e4066Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1338fa9e4066Sahrens dsphys->ds_num_children = 1; 1339fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 1340088f3894Sahrens dsphys->ds_creation_txg = crtxg; 1341c1379625SJustin T. Gibbs dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj; 1342c1379625SJustin T. Gibbs dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes; 1343c1379625SJustin T. Gibbs dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes; 1344c1379625SJustin T. Gibbs dsphys->ds_uncompressed_bytes = 1345c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes; 1346c1379625SJustin T. Gibbs dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags; 1347c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 1348c1379625SJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp; 1349c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 1350ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 1351fa9e4066Sahrens 1352ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 1353ca0cc391SMatthew Ahrens if (ds->ds_feature_inuse[f]) 1354ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(dsobj, f, tx); 1355ca0cc391SMatthew Ahrens } 1356b5152584SMatthew Ahrens 1357c1379625SJustin T. Gibbs ASSERT3U(ds->ds_prev != 0, ==, 1358c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj != 0); 13591d452cf5Sahrens if (ds->ds_prev) { 1360088f3894Sahrens uint64_t next_clones_obj = 1361c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj; 1362c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1363fa9e4066Sahrens ds->ds_object || 1364c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1); 1365c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1366c1379625SJustin T. Gibbs ds->ds_object) { 13671d452cf5Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1368c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==, 1369c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_creation_txg); 1370c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj; 1371088f3894Sahrens } else if (next_clones_obj != 0) { 13723b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(ds->ds_prev, 1373c33e334fSMatthew Ahrens dsphys->ds_next_snap_obj, tx); 13743b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 1375088f3894Sahrens next_clones_obj, dsobj, tx)); 1376fa9e4066Sahrens } 1377fa9e4066Sahrens } 1378fa9e4066Sahrens 1379a9799022Sck /* 1380a9799022Sck * If we have a reference-reservation on this dataset, we will 1381a9799022Sck * need to increase the amount of refreservation being charged 1382a9799022Sck * since our unique space is going to zero. 1383a9799022Sck */ 1384a9799022Sck if (ds->ds_reserved) { 13853f9d6ad7SLin Ling int64_t delta; 13863f9d6ad7SLin Ling ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 1387c1379625SJustin T. Gibbs delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, 1388c1379625SJustin T. Gibbs ds->ds_reserved); 138974e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 13903f9d6ad7SLin Ling delta, 0, 0, tx); 1391a9799022Sck } 1392a9799022Sck 1393fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1394c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj = 1395c1379625SJustin T. Gibbs dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX, 1396c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, tx); 1397cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 1398c1379625SJustin T. Gibbs dsl_deadlist_open(&ds->ds_deadlist, mos, 1399c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj); 1400cde58dbcSMatthew Ahrens dsl_deadlist_add_key(&ds->ds_deadlist, 1401c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, tx); 1402cde58dbcSMatthew Ahrens 1403c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg); 1404c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj; 1405c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg; 1406c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes = 0; 1407a9799022Sck if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1408c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1409fa9e4066Sahrens 1410c1379625SJustin T. Gibbs VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj, 14113b2aab18SMatthew Ahrens snapname, 8, 1, &dsobj, tx)); 1412fa9e4066Sahrens 1413fa9e4066Sahrens if (ds->ds_prev) 14143b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 14153b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1416c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev)); 1417ecd6cf80Smarks 14183f9d6ad7SLin Ling dsl_scan_ds_snapshotted(ds, tx); 1419088f3894Sahrens 142071eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 142171eb0538SChris Kirby 14224445fffbSMatthew Ahrens spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, ""); 1423fa9e4066Sahrens } 1424fa9e4066Sahrens 14253b2aab18SMatthew Ahrens static void 14263b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx) 1427fa9e4066Sahrens { 14283b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 14293b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 14303b2aab18SMatthew Ahrens nvpair_t *pair; 143191ebeef5Sahrens 14323b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 14333b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 14343b2aab18SMatthew Ahrens dsl_dataset_t *ds; 14353b2aab18SMatthew Ahrens char *name, *atp; 14369adfa60dSMatthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN]; 14373b2aab18SMatthew Ahrens 14383b2aab18SMatthew Ahrens name = nvpair_name(pair); 14393b2aab18SMatthew Ahrens atp = strchr(name, '@'); 14403b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 14413b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds)); 14423b2aab18SMatthew Ahrens 14433b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx); 14443b2aab18SMatthew Ahrens if (ddsa->ddsa_props != NULL) { 14453b2aab18SMatthew Ahrens dsl_props_set_sync_impl(ds->ds_prev, 14463b2aab18SMatthew Ahrens ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx); 14473b2aab18SMatthew Ahrens } 14483b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14493b2aab18SMatthew Ahrens } 1450fa9e4066Sahrens } 1451fa9e4066Sahrens 14523b2aab18SMatthew Ahrens /* 14533b2aab18SMatthew Ahrens * The snapshots must all be in the same pool. 14543b2aab18SMatthew Ahrens * All-or-nothing: if there are any failures, nothing will be modified. 14553b2aab18SMatthew Ahrens */ 14563b2aab18SMatthew Ahrens int 14573b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors) 145819b94df9SMatthew Ahrens { 14593b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t ddsa; 14603b2aab18SMatthew Ahrens nvpair_t *pair; 14613b2aab18SMatthew Ahrens boolean_t needsuspend; 14623b2aab18SMatthew Ahrens int error; 14633b2aab18SMatthew Ahrens spa_t *spa; 14643b2aab18SMatthew Ahrens char *firstname; 14653b2aab18SMatthew Ahrens nvlist_t *suspended = NULL; 146619b94df9SMatthew Ahrens 14673b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, NULL); 14683b2aab18SMatthew Ahrens if (pair == NULL) 14693b2aab18SMatthew Ahrens return (0); 14703b2aab18SMatthew Ahrens firstname = nvpair_name(pair); 14713b2aab18SMatthew Ahrens 14723b2aab18SMatthew Ahrens error = spa_open(firstname, &spa, FTAG); 14733b2aab18SMatthew Ahrens if (error != 0) 14743b2aab18SMatthew Ahrens return (error); 14753b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 14763b2aab18SMatthew Ahrens spa_close(spa, FTAG); 14773b2aab18SMatthew Ahrens 14783b2aab18SMatthew Ahrens if (needsuspend) { 14793b2aab18SMatthew Ahrens suspended = fnvlist_alloc(); 14803b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 14813b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 14829adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN]; 14833b2aab18SMatthew Ahrens char *snapname = nvpair_name(pair); 14843b2aab18SMatthew Ahrens char *atp; 14853b2aab18SMatthew Ahrens void *cookie; 14863b2aab18SMatthew Ahrens 14873b2aab18SMatthew Ahrens atp = strchr(snapname, '@'); 14883b2aab18SMatthew Ahrens if (atp == NULL) { 1489be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 14903b2aab18SMatthew Ahrens break; 14913b2aab18SMatthew Ahrens } 14923b2aab18SMatthew Ahrens (void) strlcpy(fsname, snapname, atp - snapname + 1); 14933b2aab18SMatthew Ahrens 14943b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 14953b2aab18SMatthew Ahrens if (error != 0) 14963b2aab18SMatthew Ahrens break; 14973b2aab18SMatthew Ahrens fnvlist_add_uint64(suspended, fsname, 14983b2aab18SMatthew Ahrens (uintptr_t)cookie); 14993b2aab18SMatthew Ahrens } 15003b2aab18SMatthew Ahrens } 15013b2aab18SMatthew Ahrens 15023b2aab18SMatthew Ahrens ddsa.ddsa_snaps = snaps; 15033b2aab18SMatthew Ahrens ddsa.ddsa_props = props; 15043b2aab18SMatthew Ahrens ddsa.ddsa_errors = errors; 1505a2afb611SJerry Jelinek ddsa.ddsa_cr = CRED(); 15063b2aab18SMatthew Ahrens 15073b2aab18SMatthew Ahrens if (error == 0) { 15083b2aab18SMatthew Ahrens error = dsl_sync_task(firstname, dsl_dataset_snapshot_check, 15093b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync, &ddsa, 15107d46dc6cSMatthew Ahrens fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL); 15113b2aab18SMatthew Ahrens } 15123b2aab18SMatthew Ahrens 15133b2aab18SMatthew Ahrens if (suspended != NULL) { 15143b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL; 15153b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(suspended, pair)) { 15163b2aab18SMatthew Ahrens zil_resume((void *)(uintptr_t) 15173b2aab18SMatthew Ahrens fnvpair_value_uint64(pair)); 15183b2aab18SMatthew Ahrens } 15193b2aab18SMatthew Ahrens fnvlist_free(suspended); 15203b2aab18SMatthew Ahrens } 15213b2aab18SMatthew Ahrens 15223b2aab18SMatthew Ahrens return (error); 15233b2aab18SMatthew Ahrens } 15243b2aab18SMatthew Ahrens 15253b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg { 15263b2aab18SMatthew Ahrens const char *ddsta_fsname; 15273b2aab18SMatthew Ahrens const char *ddsta_snapname; 15283b2aab18SMatthew Ahrens minor_t ddsta_cleanup_minor; 15293b2aab18SMatthew Ahrens const char *ddsta_htag; 15303b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t; 15313b2aab18SMatthew Ahrens 15323b2aab18SMatthew Ahrens static int 15333b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx) 15343b2aab18SMatthew Ahrens { 15353b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 15363b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15373b2aab18SMatthew Ahrens dsl_dataset_t *ds; 15383b2aab18SMatthew Ahrens int error; 15393b2aab18SMatthew Ahrens 15403b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds); 15413b2aab18SMatthew Ahrens if (error != 0) 15423b2aab18SMatthew Ahrens return (error); 15433b2aab18SMatthew Ahrens 1544a2afb611SJerry Jelinek /* NULL cred means no limit check for tmp snapshot */ 1545ca48f36fSKeith M Wesolowski error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, 1546a2afb611SJerry Jelinek tx, B_FALSE, 0, NULL); 15473b2aab18SMatthew Ahrens if (error != 0) { 15483b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15493b2aab18SMatthew Ahrens return (error); 15503b2aab18SMatthew Ahrens } 15513b2aab18SMatthew Ahrens 15523b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) { 15533b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1554be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 15553b2aab18SMatthew Ahrens } 15563b2aab18SMatthew Ahrens error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag, 15573b2aab18SMatthew Ahrens B_TRUE, tx); 15583b2aab18SMatthew Ahrens if (error != 0) { 15593b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15603b2aab18SMatthew Ahrens return (error); 15613b2aab18SMatthew Ahrens } 15623b2aab18SMatthew Ahrens 15633b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15643b2aab18SMatthew Ahrens return (0); 15653b2aab18SMatthew Ahrens } 15663b2aab18SMatthew Ahrens 15673b2aab18SMatthew Ahrens static void 15683b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx) 15693b2aab18SMatthew Ahrens { 15703b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 15713b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15723b2aab18SMatthew Ahrens dsl_dataset_t *ds; 15733b2aab18SMatthew Ahrens 15743b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds)); 15753b2aab18SMatthew Ahrens 15763b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx); 15773b2aab18SMatthew Ahrens dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag, 15783b2aab18SMatthew Ahrens ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx); 15793b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx); 15803b2aab18SMatthew Ahrens 15813b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15823b2aab18SMatthew Ahrens } 15833b2aab18SMatthew Ahrens 15843b2aab18SMatthew Ahrens int 15853b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname, 15863b2aab18SMatthew Ahrens minor_t cleanup_minor, const char *htag) 15873b2aab18SMatthew Ahrens { 15883b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t ddsta; 15893b2aab18SMatthew Ahrens int error; 15903b2aab18SMatthew Ahrens spa_t *spa; 15913b2aab18SMatthew Ahrens boolean_t needsuspend; 15923b2aab18SMatthew Ahrens void *cookie; 15933b2aab18SMatthew Ahrens 15943b2aab18SMatthew Ahrens ddsta.ddsta_fsname = fsname; 15953b2aab18SMatthew Ahrens ddsta.ddsta_snapname = snapname; 15963b2aab18SMatthew Ahrens ddsta.ddsta_cleanup_minor = cleanup_minor; 15973b2aab18SMatthew Ahrens ddsta.ddsta_htag = htag; 15983b2aab18SMatthew Ahrens 15993b2aab18SMatthew Ahrens error = spa_open(fsname, &spa, FTAG); 16003b2aab18SMatthew Ahrens if (error != 0) 16013b2aab18SMatthew Ahrens return (error); 16023b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 16033b2aab18SMatthew Ahrens spa_close(spa, FTAG); 16043b2aab18SMatthew Ahrens 16053b2aab18SMatthew Ahrens if (needsuspend) { 16063b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 16073b2aab18SMatthew Ahrens if (error != 0) 16083b2aab18SMatthew Ahrens return (error); 16093b2aab18SMatthew Ahrens } 16103b2aab18SMatthew Ahrens 16113b2aab18SMatthew Ahrens error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check, 16127d46dc6cSMatthew Ahrens dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED); 16133b2aab18SMatthew Ahrens 16143b2aab18SMatthew Ahrens if (needsuspend) 16153b2aab18SMatthew Ahrens zil_resume(cookie); 16163b2aab18SMatthew Ahrens return (error); 16173b2aab18SMatthew Ahrens } 16183b2aab18SMatthew Ahrens 16193b2aab18SMatthew Ahrens void 16203b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 16213b2aab18SMatthew Ahrens { 16223b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 16233b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 1624c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0); 16253b2aab18SMatthew Ahrens 16263b2aab18SMatthew Ahrens /* 16273b2aab18SMatthew Ahrens * in case we had to change ds_fsid_guid when we opened it, 16283b2aab18SMatthew Ahrens * sync it out now. 16293b2aab18SMatthew Ahrens */ 16303b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1631c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid; 16323b2aab18SMatthew Ahrens 16339c3fd121SMatthew Ahrens if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) { 16349c3fd121SMatthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 16359c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1, 16369c3fd121SMatthew Ahrens &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx)); 16379c3fd121SMatthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 16389c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1, 16399c3fd121SMatthew Ahrens &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx)); 16409c3fd121SMatthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 16419c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1, 16429c3fd121SMatthew Ahrens &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx)); 16439c3fd121SMatthew Ahrens ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0; 16449c3fd121SMatthew Ahrens ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0; 16459c3fd121SMatthew Ahrens ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0; 16469c3fd121SMatthew Ahrens } 16479c3fd121SMatthew Ahrens 16483b2aab18SMatthew Ahrens dmu_objset_sync(ds->ds_objset, zio, tx); 1649b5152584SMatthew Ahrens 1650ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 1651ca0cc391SMatthew Ahrens if (ds->ds_feature_activation_needed[f]) { 1652ca0cc391SMatthew Ahrens if (ds->ds_feature_inuse[f]) 1653ca0cc391SMatthew Ahrens continue; 1654ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(ds->ds_object, f, tx); 1655ca0cc391SMatthew Ahrens ds->ds_feature_inuse[f] = B_TRUE; 1656ca0cc391SMatthew Ahrens } 1657b5152584SMatthew Ahrens } 16583b2aab18SMatthew Ahrens } 16593b2aab18SMatthew Ahrens 1660bfaed0b9SAndriy Gapon static int 1661bfaed0b9SAndriy Gapon deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1662bfaed0b9SAndriy Gapon { 1663bfaed0b9SAndriy Gapon dsl_deadlist_t *dl = arg; 1664bfaed0b9SAndriy Gapon dsl_deadlist_insert(dl, bp, tx); 1665bfaed0b9SAndriy Gapon return (0); 1666bfaed0b9SAndriy Gapon } 1667bfaed0b9SAndriy Gapon 1668bfaed0b9SAndriy Gapon void 1669bfaed0b9SAndriy Gapon dsl_dataset_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx) 1670bfaed0b9SAndriy Gapon { 1671bfaed0b9SAndriy Gapon objset_t *os = ds->ds_objset; 1672bfaed0b9SAndriy Gapon 1673bfaed0b9SAndriy Gapon bplist_iterate(&ds->ds_pending_deadlist, 1674bfaed0b9SAndriy Gapon deadlist_enqueue_cb, &ds->ds_deadlist, tx); 1675bfaed0b9SAndriy Gapon 167694c2d0ebSMatthew Ahrens if (os->os_synced_dnodes != NULL) { 167794c2d0ebSMatthew Ahrens multilist_destroy(os->os_synced_dnodes); 167894c2d0ebSMatthew Ahrens os->os_synced_dnodes = NULL; 167994c2d0ebSMatthew Ahrens } 168094c2d0ebSMatthew Ahrens 1681bfaed0b9SAndriy Gapon ASSERT(!dmu_objset_is_dirty(os, dmu_tx_get_txg(tx))); 1682bfaed0b9SAndriy Gapon 1683bfaed0b9SAndriy Gapon dmu_buf_rele(ds->ds_dbuf, ds); 1684bfaed0b9SAndriy Gapon } 1685bfaed0b9SAndriy Gapon 1686dfc11533SChris Williamson int 1687dfc11533SChris Williamson get_clones_stat_impl(dsl_dataset_t *ds, nvlist_t *val) 16883b2aab18SMatthew Ahrens { 16893b2aab18SMatthew Ahrens uint64_t count = 0; 16903b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 16913b2aab18SMatthew Ahrens zap_cursor_t zc; 16923b2aab18SMatthew Ahrens zap_attribute_t za; 16933b2aab18SMatthew Ahrens 16943b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 169519b94df9SMatthew Ahrens 169619b94df9SMatthew Ahrens /* 16973b2aab18SMatthew Ahrens * There may be missing entries in ds_next_clones_obj 169819b94df9SMatthew Ahrens * due to a bug in a previous version of the code. 169919b94df9SMatthew Ahrens * Only trust it if it has the right number of entries. 170019b94df9SMatthew Ahrens */ 1701c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) { 1702c1379625SJustin T. Gibbs VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 170319b94df9SMatthew Ahrens &count)); 170419b94df9SMatthew Ahrens } 1705dfc11533SChris Williamson if (count != dsl_dataset_phys(ds)->ds_num_children - 1) { 1706dfc11533SChris Williamson return (ENOENT); 1707dfc11533SChris Williamson } 1708c1379625SJustin T. Gibbs for (zap_cursor_init(&zc, mos, 1709c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj); 171019b94df9SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 171119b94df9SMatthew Ahrens zap_cursor_advance(&zc)) { 171219b94df9SMatthew Ahrens dsl_dataset_t *clone; 17139adfa60dSMatthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN]; 17143b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 17153b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &clone)); 171619b94df9SMatthew Ahrens dsl_dir_name(clone->ds_dir, buf); 17173b2aab18SMatthew Ahrens fnvlist_add_boolean(val, buf); 171819b94df9SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 171919b94df9SMatthew Ahrens } 172019b94df9SMatthew Ahrens zap_cursor_fini(&zc); 1721dfc11533SChris Williamson return (0); 172219b94df9SMatthew Ahrens } 172319b94df9SMatthew Ahrens 1724dfc11533SChris Williamson void 1725dfc11533SChris Williamson get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv) 1726dfc11533SChris Williamson { 1727dfc11533SChris Williamson nvlist_t *propval = fnvlist_alloc(); 1728dfc11533SChris Williamson nvlist_t *val; 1729dfc11533SChris Williamson 1730dfc11533SChris Williamson /* 1731dfc11533SChris Williamson * We use nvlist_alloc() instead of fnvlist_alloc() because the 1732dfc11533SChris Williamson * latter would allocate the list with NV_UNIQUE_NAME flag. 1733dfc11533SChris Williamson * As a result, every time a clone name is appended to the list 1734dfc11533SChris Williamson * it would be (linearly) searched for for a duplicate name. 1735dfc11533SChris Williamson * We already know that all clone names must be unique and we 1736dfc11533SChris Williamson * want avoid the quadratic complexity of double-checking that 1737dfc11533SChris Williamson * because we can have a large number of clones. 1738dfc11533SChris Williamson */ 1739dfc11533SChris Williamson VERIFY0(nvlist_alloc(&val, 0, KM_SLEEP)); 1740dfc11533SChris Williamson 1741dfc11533SChris Williamson if (get_clones_stat_impl(ds, val) == 0) { 1742dfc11533SChris Williamson fnvlist_add_nvlist(propval, ZPROP_VALUE, val); 1743dfc11533SChris Williamson fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), 1744dfc11533SChris Williamson propval); 1745dfc11533SChris Williamson } 17467d3000f7SPavel Zakharov 17477d3000f7SPavel Zakharov nvlist_free(val); 17487d3000f7SPavel Zakharov nvlist_free(propval); 1749dfc11533SChris Williamson } 1750dfc11533SChris Williamson 1751dfc11533SChris Williamson /* 1752dfc11533SChris Williamson * Returns a string that represents the receive resume stats token. It should 1753dfc11533SChris Williamson * be freed with strfree(). 1754dfc11533SChris Williamson */ 1755dfc11533SChris Williamson char * 1756dfc11533SChris Williamson get_receive_resume_stats_impl(dsl_dataset_t *ds) 17579c3fd121SMatthew Ahrens { 17589c3fd121SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 17599c3fd121SMatthew Ahrens 17609c3fd121SMatthew Ahrens if (dsl_dataset_has_resume_receive_state(ds)) { 17619c3fd121SMatthew Ahrens char *str; 17629c3fd121SMatthew Ahrens void *packed; 17639c3fd121SMatthew Ahrens uint8_t *compressed; 17649c3fd121SMatthew Ahrens uint64_t val; 17659c3fd121SMatthew Ahrens nvlist_t *token_nv = fnvlist_alloc(); 17669c3fd121SMatthew Ahrens size_t packed_size, compressed_size; 17679c3fd121SMatthew Ahrens 17689c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17699c3fd121SMatthew Ahrens DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) { 17709c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "fromguid", val); 17719c3fd121SMatthew Ahrens } 17729c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17739c3fd121SMatthew Ahrens DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) { 17749c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "object", val); 17759c3fd121SMatthew Ahrens } 17769c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17779c3fd121SMatthew Ahrens DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) { 17789c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "offset", val); 17799c3fd121SMatthew Ahrens } 17809c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17819c3fd121SMatthew Ahrens DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) { 17829c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "bytes", val); 17839c3fd121SMatthew Ahrens } 17849c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17859c3fd121SMatthew Ahrens DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) { 17869c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "toguid", val); 17879c3fd121SMatthew Ahrens } 17889c3fd121SMatthew Ahrens char buf[256]; 17899c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17909c3fd121SMatthew Ahrens DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) { 17919c3fd121SMatthew Ahrens fnvlist_add_string(token_nv, "toname", buf); 17929c3fd121SMatthew Ahrens } 17935602294fSDan Kimmel if (zap_contains(dp->dp_meta_objset, ds->ds_object, 17945602294fSDan Kimmel DS_FIELD_RESUME_LARGEBLOCK) == 0) { 17955602294fSDan Kimmel fnvlist_add_boolean(token_nv, "largeblockok"); 17965602294fSDan Kimmel } 17979c3fd121SMatthew Ahrens if (zap_contains(dp->dp_meta_objset, ds->ds_object, 17989c3fd121SMatthew Ahrens DS_FIELD_RESUME_EMBEDOK) == 0) { 17999c3fd121SMatthew Ahrens fnvlist_add_boolean(token_nv, "embedok"); 18009c3fd121SMatthew Ahrens } 18015602294fSDan Kimmel if (zap_contains(dp->dp_meta_objset, ds->ds_object, 18025602294fSDan Kimmel DS_FIELD_RESUME_COMPRESSOK) == 0) { 18035602294fSDan Kimmel fnvlist_add_boolean(token_nv, "compressok"); 18045602294fSDan Kimmel } 18059c3fd121SMatthew Ahrens packed = fnvlist_pack(token_nv, &packed_size); 18069c3fd121SMatthew Ahrens fnvlist_free(token_nv); 18079c3fd121SMatthew Ahrens compressed = kmem_alloc(packed_size, KM_SLEEP); 18089c3fd121SMatthew Ahrens 18099c3fd121SMatthew Ahrens compressed_size = gzip_compress(packed, compressed, 18109c3fd121SMatthew Ahrens packed_size, packed_size, 6); 18119c3fd121SMatthew Ahrens 18129c3fd121SMatthew Ahrens zio_cksum_t cksum; 18139c3fd121SMatthew Ahrens fletcher_4_native(compressed, compressed_size, NULL, &cksum); 18149c3fd121SMatthew Ahrens 18159c3fd121SMatthew Ahrens str = kmem_alloc(compressed_size * 2 + 1, KM_SLEEP); 18169c3fd121SMatthew Ahrens for (int i = 0; i < compressed_size; i++) { 18179c3fd121SMatthew Ahrens (void) sprintf(str + i * 2, "%02x", compressed[i]); 18189c3fd121SMatthew Ahrens } 18199c3fd121SMatthew Ahrens str[compressed_size * 2] = '\0'; 18209c3fd121SMatthew Ahrens char *propval = kmem_asprintf("%u-%llx-%llx-%s", 18219c3fd121SMatthew Ahrens ZFS_SEND_RESUME_TOKEN_VERSION, 18229c3fd121SMatthew Ahrens (longlong_t)cksum.zc_word[0], 18239c3fd121SMatthew Ahrens (longlong_t)packed_size, str); 18249c3fd121SMatthew Ahrens kmem_free(packed, packed_size); 18259c3fd121SMatthew Ahrens kmem_free(str, compressed_size * 2 + 1); 18269c3fd121SMatthew Ahrens kmem_free(compressed, packed_size); 1827dfc11533SChris Williamson return (propval); 1828dfc11533SChris Williamson } 1829dfc11533SChris Williamson return (strdup("")); 1830dfc11533SChris Williamson } 1831dfc11533SChris Williamson 1832dfc11533SChris Williamson /* 1833dfc11533SChris Williamson * Returns a string that represents the receive resume stats token of the 1834dfc11533SChris Williamson * dataset's child. It should be freed with strfree(). 1835dfc11533SChris Williamson */ 1836dfc11533SChris Williamson char * 1837dfc11533SChris Williamson get_child_receive_stats(dsl_dataset_t *ds) 1838dfc11533SChris Williamson { 1839dfc11533SChris Williamson char recvname[ZFS_MAX_DATASET_NAME_LEN + 6]; 1840dfc11533SChris Williamson dsl_dataset_t *recv_ds; 1841dfc11533SChris Williamson dsl_dataset_name(ds, recvname); 1842dfc11533SChris Williamson if (strlcat(recvname, "/", sizeof (recvname)) < 1843dfc11533SChris Williamson sizeof (recvname) && 1844dfc11533SChris Williamson strlcat(recvname, recv_clone_name, sizeof (recvname)) < 1845dfc11533SChris Williamson sizeof (recvname) && 1846dfc11533SChris Williamson dsl_dataset_hold(ds->ds_dir->dd_pool, recvname, FTAG, 1847dfc11533SChris Williamson &recv_ds) == 0) { 1848dfc11533SChris Williamson char *propval = get_receive_resume_stats_impl(recv_ds); 1849dfc11533SChris Williamson dsl_dataset_rele(recv_ds, FTAG); 1850dfc11533SChris Williamson return (propval); 1851dfc11533SChris Williamson } 1852dfc11533SChris Williamson return (strdup("")); 1853dfc11533SChris Williamson } 1854dfc11533SChris Williamson 1855dfc11533SChris Williamson static void 1856dfc11533SChris Williamson get_receive_resume_stats(dsl_dataset_t *ds, nvlist_t *nv) 1857dfc11533SChris Williamson { 1858dfc11533SChris Williamson char *propval = get_receive_resume_stats_impl(ds); 1859dfc11533SChris Williamson if (strcmp(propval, "") != 0) { 1860dfc11533SChris Williamson dsl_prop_nvlist_add_string(nv, 1861dfc11533SChris Williamson ZFS_PROP_RECEIVE_RESUME_TOKEN, propval); 1862dfc11533SChris Williamson } else { 1863dfc11533SChris Williamson char *childval = get_child_receive_stats(ds); 1864dfc11533SChris Williamson if (strcmp(childval, "") != 0) { 1865dfc11533SChris Williamson dsl_prop_nvlist_add_string(nv, 1866dfc11533SChris Williamson ZFS_PROP_RECEIVE_RESUME_TOKEN, childval); 1867dfc11533SChris Williamson } 1868dfc11533SChris Williamson strfree(childval); 18699c3fd121SMatthew Ahrens } 1870dfc11533SChris Williamson strfree(propval); 1871dfc11533SChris Williamson } 1872dfc11533SChris Williamson 1873dfc11533SChris Williamson uint64_t 1874dfc11533SChris Williamson dsl_get_refratio(dsl_dataset_t *ds) 1875dfc11533SChris Williamson { 1876dfc11533SChris Williamson uint64_t ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 : 1877dfc11533SChris Williamson (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 / 1878dfc11533SChris Williamson dsl_dataset_phys(ds)->ds_compressed_bytes); 1879dfc11533SChris Williamson return (ratio); 1880dfc11533SChris Williamson } 1881dfc11533SChris Williamson 1882dfc11533SChris Williamson uint64_t 1883dfc11533SChris Williamson dsl_get_logicalreferenced(dsl_dataset_t *ds) 1884dfc11533SChris Williamson { 1885dfc11533SChris Williamson return (dsl_dataset_phys(ds)->ds_uncompressed_bytes); 1886dfc11533SChris Williamson } 1887dfc11533SChris Williamson 1888dfc11533SChris Williamson uint64_t 1889dfc11533SChris Williamson dsl_get_compressratio(dsl_dataset_t *ds) 1890dfc11533SChris Williamson { 1891dfc11533SChris Williamson if (ds->ds_is_snapshot) { 1892dfc11533SChris Williamson return (dsl_get_refratio(ds)); 1893dfc11533SChris Williamson } else { 1894dfc11533SChris Williamson dsl_dir_t *dd = ds->ds_dir; 1895dfc11533SChris Williamson mutex_enter(&dd->dd_lock); 1896dfc11533SChris Williamson uint64_t val = dsl_dir_get_compressratio(dd); 1897dfc11533SChris Williamson mutex_exit(&dd->dd_lock); 1898dfc11533SChris Williamson return (val); 1899dfc11533SChris Williamson } 1900dfc11533SChris Williamson } 1901dfc11533SChris Williamson 1902dfc11533SChris Williamson uint64_t 1903dfc11533SChris Williamson dsl_get_used(dsl_dataset_t *ds) 1904dfc11533SChris Williamson { 1905dfc11533SChris Williamson if (ds->ds_is_snapshot) { 1906dfc11533SChris Williamson return (dsl_dataset_phys(ds)->ds_unique_bytes); 1907dfc11533SChris Williamson } else { 1908dfc11533SChris Williamson dsl_dir_t *dd = ds->ds_dir; 1909dfc11533SChris Williamson mutex_enter(&dd->dd_lock); 1910dfc11533SChris Williamson uint64_t val = dsl_dir_get_used(dd); 1911dfc11533SChris Williamson mutex_exit(&dd->dd_lock); 1912dfc11533SChris Williamson return (val); 1913dfc11533SChris Williamson } 1914dfc11533SChris Williamson } 1915dfc11533SChris Williamson 1916dfc11533SChris Williamson uint64_t 1917dfc11533SChris Williamson dsl_get_creation(dsl_dataset_t *ds) 1918dfc11533SChris Williamson { 1919dfc11533SChris Williamson return (dsl_dataset_phys(ds)->ds_creation_time); 1920dfc11533SChris Williamson } 1921dfc11533SChris Williamson 1922dfc11533SChris Williamson uint64_t 1923dfc11533SChris Williamson dsl_get_creationtxg(dsl_dataset_t *ds) 1924dfc11533SChris Williamson { 1925dfc11533SChris Williamson return (dsl_dataset_phys(ds)->ds_creation_txg); 1926dfc11533SChris Williamson } 1927dfc11533SChris Williamson 1928dfc11533SChris Williamson uint64_t 1929dfc11533SChris Williamson dsl_get_refquota(dsl_dataset_t *ds) 1930dfc11533SChris Williamson { 1931dfc11533SChris Williamson return (ds->ds_quota); 1932dfc11533SChris Williamson } 1933dfc11533SChris Williamson 1934dfc11533SChris Williamson uint64_t 1935dfc11533SChris Williamson dsl_get_refreservation(dsl_dataset_t *ds) 1936dfc11533SChris Williamson { 1937dfc11533SChris Williamson return (ds->ds_reserved); 1938dfc11533SChris Williamson } 1939dfc11533SChris Williamson 1940dfc11533SChris Williamson uint64_t 1941dfc11533SChris Williamson dsl_get_guid(dsl_dataset_t *ds) 1942dfc11533SChris Williamson { 1943dfc11533SChris Williamson return (dsl_dataset_phys(ds)->ds_guid); 1944dfc11533SChris Williamson } 1945dfc11533SChris Williamson 1946dfc11533SChris Williamson uint64_t 1947dfc11533SChris Williamson dsl_get_unique(dsl_dataset_t *ds) 1948dfc11533SChris Williamson { 1949dfc11533SChris Williamson return (dsl_dataset_phys(ds)->ds_unique_bytes); 1950dfc11533SChris Williamson } 1951dfc11533SChris Williamson 1952dfc11533SChris Williamson uint64_t 1953dfc11533SChris Williamson dsl_get_objsetid(dsl_dataset_t *ds) 1954dfc11533SChris Williamson { 1955dfc11533SChris Williamson return (ds->ds_object); 1956dfc11533SChris Williamson } 1957dfc11533SChris Williamson 1958dfc11533SChris Williamson uint64_t 1959dfc11533SChris Williamson dsl_get_userrefs(dsl_dataset_t *ds) 1960dfc11533SChris Williamson { 1961dfc11533SChris Williamson return (ds->ds_userrefs); 1962dfc11533SChris Williamson } 1963dfc11533SChris Williamson 1964dfc11533SChris Williamson uint64_t 1965dfc11533SChris Williamson dsl_get_defer_destroy(dsl_dataset_t *ds) 1966dfc11533SChris Williamson { 1967dfc11533SChris Williamson return (DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 1968dfc11533SChris Williamson } 1969dfc11533SChris Williamson 1970dfc11533SChris Williamson uint64_t 1971dfc11533SChris Williamson dsl_get_referenced(dsl_dataset_t *ds) 1972dfc11533SChris Williamson { 1973dfc11533SChris Williamson return (dsl_dataset_phys(ds)->ds_referenced_bytes); 1974dfc11533SChris Williamson } 1975dfc11533SChris Williamson 1976dfc11533SChris Williamson uint64_t 1977dfc11533SChris Williamson dsl_get_numclones(dsl_dataset_t *ds) 1978dfc11533SChris Williamson { 1979dfc11533SChris Williamson ASSERT(ds->ds_is_snapshot); 1980dfc11533SChris Williamson return (dsl_dataset_phys(ds)->ds_num_children - 1); 1981dfc11533SChris Williamson } 1982dfc11533SChris Williamson 1983dfc11533SChris Williamson uint64_t 1984dfc11533SChris Williamson dsl_get_inconsistent(dsl_dataset_t *ds) 1985dfc11533SChris Williamson { 1986dfc11533SChris Williamson return ((dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT) ? 1987dfc11533SChris Williamson 1 : 0); 1988dfc11533SChris Williamson } 1989dfc11533SChris Williamson 1990dfc11533SChris Williamson uint64_t 1991dfc11533SChris Williamson dsl_get_available(dsl_dataset_t *ds) 1992dfc11533SChris Williamson { 1993dfc11533SChris Williamson uint64_t refdbytes = dsl_get_referenced(ds); 1994dfc11533SChris Williamson uint64_t availbytes = dsl_dir_space_available(ds->ds_dir, 1995dfc11533SChris Williamson NULL, 0, TRUE); 1996dfc11533SChris Williamson if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) { 1997dfc11533SChris Williamson availbytes += 1998dfc11533SChris Williamson ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes; 1999dfc11533SChris Williamson } 2000dfc11533SChris Williamson if (ds->ds_quota != 0) { 2001dfc11533SChris Williamson /* 2002dfc11533SChris Williamson * Adjust available bytes according to refquota 2003dfc11533SChris Williamson */ 2004dfc11533SChris Williamson if (refdbytes < ds->ds_quota) { 2005dfc11533SChris Williamson availbytes = MIN(availbytes, 2006dfc11533SChris Williamson ds->ds_quota - refdbytes); 2007dfc11533SChris Williamson } else { 2008dfc11533SChris Williamson availbytes = 0; 2009dfc11533SChris Williamson } 2010dfc11533SChris Williamson } 2011dfc11533SChris Williamson return (availbytes); 2012dfc11533SChris Williamson } 2013dfc11533SChris Williamson 2014dfc11533SChris Williamson int 2015dfc11533SChris Williamson dsl_get_written(dsl_dataset_t *ds, uint64_t *written) 2016dfc11533SChris Williamson { 2017dfc11533SChris Williamson dsl_pool_t *dp = ds->ds_dir->dd_pool; 2018dfc11533SChris Williamson dsl_dataset_t *prev; 2019dfc11533SChris Williamson int err = dsl_dataset_hold_obj(dp, 2020dfc11533SChris Williamson dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 2021dfc11533SChris Williamson if (err == 0) { 2022dfc11533SChris Williamson uint64_t comp, uncomp; 2023dfc11533SChris Williamson err = dsl_dataset_space_written(prev, ds, written, 2024dfc11533SChris Williamson &comp, &uncomp); 2025dfc11533SChris Williamson dsl_dataset_rele(prev, FTAG); 2026dfc11533SChris Williamson } 2027dfc11533SChris Williamson return (err); 2028dfc11533SChris Williamson } 2029dfc11533SChris Williamson 2030dfc11533SChris Williamson /* 2031dfc11533SChris Williamson * 'snap' should be a buffer of size ZFS_MAX_DATASET_NAME_LEN. 2032dfc11533SChris Williamson */ 2033dfc11533SChris Williamson int 2034dfc11533SChris Williamson dsl_get_prev_snap(dsl_dataset_t *ds, char *snap) 2035dfc11533SChris Williamson { 2036dfc11533SChris Williamson dsl_pool_t *dp = ds->ds_dir->dd_pool; 2037dfc11533SChris Williamson if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) { 2038dfc11533SChris Williamson dsl_dataset_name(ds->ds_prev, snap); 2039dfc11533SChris Williamson return (0); 2040dfc11533SChris Williamson } else { 2041dfc11533SChris Williamson return (ENOENT); 2042dfc11533SChris Williamson } 2043dfc11533SChris Williamson } 2044dfc11533SChris Williamson 2045dfc11533SChris Williamson /* 2046dfc11533SChris Williamson * Returns the mountpoint property and source for the given dataset in the value 2047dfc11533SChris Williamson * and source buffers. The value buffer must be at least as large as MAXPATHLEN 2048dfc11533SChris Williamson * and the source buffer as least as large a ZFS_MAX_DATASET_NAME_LEN. 2049dfc11533SChris Williamson * Returns 0 on success and an error on failure. 2050dfc11533SChris Williamson */ 2051dfc11533SChris Williamson int 2052dfc11533SChris Williamson dsl_get_mountpoint(dsl_dataset_t *ds, const char *dsname, char *value, 2053dfc11533SChris Williamson char *source) 2054dfc11533SChris Williamson { 2055dfc11533SChris Williamson int error; 2056dfc11533SChris Williamson dsl_pool_t *dp = ds->ds_dir->dd_pool; 2057dfc11533SChris Williamson 2058dfc11533SChris Williamson /* Retrieve the mountpoint value stored in the zap opbject */ 2059dfc11533SChris Williamson error = dsl_prop_get_ds(ds, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 1, 2060dfc11533SChris Williamson ZAP_MAXVALUELEN, value, source); 2061dfc11533SChris Williamson if (error != 0) { 2062dfc11533SChris Williamson return (error); 2063dfc11533SChris Williamson } 2064dfc11533SChris Williamson 2065dfc11533SChris Williamson /* Process the dsname and source to find the full mountpoint string */ 2066dfc11533SChris Williamson if (value[0] == '/') { 2067dfc11533SChris Williamson char *buf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP); 2068dfc11533SChris Williamson char *root = buf; 2069dfc11533SChris Williamson const char *relpath; 2070dfc11533SChris Williamson 2071dfc11533SChris Williamson /* 2072dfc11533SChris Williamson * If we inherit the mountpoint, even from a dataset 2073dfc11533SChris Williamson * with a received value, the source will be the path of 2074dfc11533SChris Williamson * the dataset we inherit from. If source is 2075dfc11533SChris Williamson * ZPROP_SOURCE_VAL_RECVD, the received value is not 2076dfc11533SChris Williamson * inherited. 2077dfc11533SChris Williamson */ 2078dfc11533SChris Williamson if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { 2079dfc11533SChris Williamson relpath = ""; 2080dfc11533SChris Williamson } else { 2081dfc11533SChris Williamson ASSERT0(strncmp(dsname, source, strlen(source))); 2082dfc11533SChris Williamson relpath = dsname + strlen(source); 2083dfc11533SChris Williamson if (relpath[0] == '/') 2084dfc11533SChris Williamson relpath++; 2085dfc11533SChris Williamson } 2086dfc11533SChris Williamson 2087dfc11533SChris Williamson spa_altroot(dp->dp_spa, root, ZAP_MAXVALUELEN); 2088dfc11533SChris Williamson 2089dfc11533SChris Williamson /* 2090dfc11533SChris Williamson * Special case an alternate root of '/'. This will 2091dfc11533SChris Williamson * avoid having multiple leading slashes in the 2092dfc11533SChris Williamson * mountpoint path. 2093dfc11533SChris Williamson */ 2094dfc11533SChris Williamson if (strcmp(root, "/") == 0) 2095dfc11533SChris Williamson root++; 2096dfc11533SChris Williamson 2097dfc11533SChris Williamson /* 2098dfc11533SChris Williamson * If the mountpoint is '/' then skip over this 2099dfc11533SChris Williamson * if we are obtaining either an alternate root or 2100dfc11533SChris Williamson * an inherited mountpoint. 2101dfc11533SChris Williamson */ 2102dfc11533SChris Williamson char *mnt = value; 2103dfc11533SChris Williamson if (value[1] == '\0' && (root[0] != '\0' || 2104dfc11533SChris Williamson relpath[0] != '\0')) 2105dfc11533SChris Williamson mnt = value + 1; 2106dfc11533SChris Williamson 2107dfc11533SChris Williamson if (relpath[0] == '\0') { 2108dfc11533SChris Williamson (void) snprintf(value, ZAP_MAXVALUELEN, "%s%s", 2109dfc11533SChris Williamson root, mnt); 2110dfc11533SChris Williamson } else { 2111dfc11533SChris Williamson (void) snprintf(value, ZAP_MAXVALUELEN, "%s%s%s%s", 2112dfc11533SChris Williamson root, mnt, relpath[0] == '@' ? "" : "/", 2113dfc11533SChris Williamson relpath); 2114dfc11533SChris Williamson } 2115dfc11533SChris Williamson kmem_free(buf, ZAP_MAXVALUELEN); 2116dfc11533SChris Williamson } else { 2117dfc11533SChris Williamson /* 'legacy' or 'none' */ 2118dfc11533SChris Williamson (void) snprintf(value, ZAP_MAXVALUELEN, "%s", value); 2119dfc11533SChris Williamson } 2120dfc11533SChris Williamson return (0); 21219c3fd121SMatthew Ahrens } 21229c3fd121SMatthew Ahrens 2123fa9e4066Sahrens void 2124a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 2125fa9e4066Sahrens { 21263b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 2127a9799022Sck 21283b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 21293b2aab18SMatthew Ahrens 2130dfc11533SChris Williamson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, 2131dfc11533SChris Williamson dsl_get_refratio(ds)); 213277372cb0SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED, 2133dfc11533SChris Williamson dsl_get_logicalreferenced(ds)); 2134dfc11533SChris Williamson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 2135dfc11533SChris Williamson dsl_get_compressratio(ds)); 2136dfc11533SChris Williamson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 2137dfc11533SChris Williamson dsl_get_used(ds)); 21384445fffbSMatthew Ahrens 2139bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 21404445fffbSMatthew Ahrens get_clones_stat(ds, nv); 21414445fffbSMatthew Ahrens } else { 2142dfc11533SChris Williamson char buf[ZFS_MAX_DATASET_NAME_LEN]; 2143dfc11533SChris Williamson if (dsl_get_prev_snap(ds, buf) == 0) 2144dfc11533SChris Williamson dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, 2145dfc11533SChris Williamson buf); 21464445fffbSMatthew Ahrens dsl_dir_stats(ds->ds_dir, nv); 21474445fffbSMatthew Ahrens } 2148fa9e4066Sahrens 2149dfc11533SChris Williamson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, 2150dfc11533SChris Williamson dsl_get_available(ds)); 2151dfc11533SChris Williamson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, 2152dfc11533SChris Williamson dsl_get_referenced(ds)); 2153a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 2154dfc11533SChris Williamson dsl_get_creation(ds)); 2155a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 2156dfc11533SChris Williamson dsl_get_creationtxg(ds)); 2157a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 2158dfc11533SChris Williamson dsl_get_refquota(ds)); 2159a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 2160dfc11533SChris Williamson dsl_get_refreservation(ds)); 2161c5904d13Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 2162dfc11533SChris Williamson dsl_get_guid(ds)); 21631d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 2164dfc11533SChris Williamson dsl_get_unique(ds)); 21651d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 2166dfc11533SChris Williamson dsl_get_objsetid(ds)); 216792241e0bSTom Erickson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 2168dfc11533SChris Williamson dsl_get_userrefs(ds)); 2169842727c2SChris Kirby dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 2170dfc11533SChris Williamson dsl_get_defer_destroy(ds)); 2171fa9e4066Sahrens 2172c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 2173dfc11533SChris Williamson uint64_t written; 2174dfc11533SChris Williamson if (dsl_get_written(ds, &written) == 0) { 2175dfc11533SChris Williamson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN, 2176dfc11533SChris Williamson written); 217719b94df9SMatthew Ahrens } 217819b94df9SMatthew Ahrens } 21799c3fd121SMatthew Ahrens 21809c3fd121SMatthew Ahrens if (!dsl_dataset_is_snapshot(ds)) { 21819c3fd121SMatthew Ahrens /* 21829c3fd121SMatthew Ahrens * A failed "newfs" (e.g. full) resumable receive leaves 21839c3fd121SMatthew Ahrens * the stats set on this dataset. Check here for the prop. 21849c3fd121SMatthew Ahrens */ 21859c3fd121SMatthew Ahrens get_receive_resume_stats(ds, nv); 21869c3fd121SMatthew Ahrens 21879c3fd121SMatthew Ahrens /* 21889c3fd121SMatthew Ahrens * A failed incremental resumable receive leaves the 21899c3fd121SMatthew Ahrens * stats set on our child named "%recv". Check the child 21909c3fd121SMatthew Ahrens * for the prop. 21919c3fd121SMatthew Ahrens */ 21929adfa60dSMatthew Ahrens /* 6 extra bytes for /%recv */ 21939adfa60dSMatthew Ahrens char recvname[ZFS_MAX_DATASET_NAME_LEN + 6]; 21949c3fd121SMatthew Ahrens dsl_dataset_t *recv_ds; 21959c3fd121SMatthew Ahrens dsl_dataset_name(ds, recvname); 21969adfa60dSMatthew Ahrens if (strlcat(recvname, "/", sizeof (recvname)) < 21979adfa60dSMatthew Ahrens sizeof (recvname) && 21989adfa60dSMatthew Ahrens strlcat(recvname, recv_clone_name, sizeof (recvname)) < 21999adfa60dSMatthew Ahrens sizeof (recvname) && 22009adfa60dSMatthew Ahrens dsl_dataset_hold(dp, recvname, FTAG, &recv_ds) == 0) { 22019c3fd121SMatthew Ahrens get_receive_resume_stats(recv_ds, nv); 22029c3fd121SMatthew Ahrens dsl_dataset_rele(recv_ds, FTAG); 22039c3fd121SMatthew Ahrens } 22049c3fd121SMatthew Ahrens } 2205fa9e4066Sahrens } 2206fa9e4066Sahrens 2207a2eea2e1Sahrens void 2208a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 2209a2eea2e1Sahrens { 22103b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 22113b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 22123b2aab18SMatthew Ahrens 2213dfc11533SChris Williamson stat->dds_creation_txg = dsl_get_creationtxg(ds); 2214dfc11533SChris Williamson stat->dds_inconsistent = dsl_get_inconsistent(ds); 2215dfc11533SChris Williamson stat->dds_guid = dsl_get_guid(ds); 22164445fffbSMatthew Ahrens stat->dds_origin[0] = '\0'; 2217bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 2218a2eea2e1Sahrens stat->dds_is_snapshot = B_TRUE; 2219dfc11533SChris Williamson stat->dds_num_clones = dsl_get_numclones(ds); 2220ebedde84SEric Taylor } else { 2221ebedde84SEric Taylor stat->dds_is_snapshot = B_FALSE; 2222ebedde84SEric Taylor stat->dds_num_clones = 0; 2223a2eea2e1Sahrens 22244445fffbSMatthew Ahrens if (dsl_dir_is_clone(ds->ds_dir)) { 2225dfc11533SChris Williamson dsl_dir_get_origin(ds->ds_dir, stat->dds_origin); 22264445fffbSMatthew Ahrens } 2227a2eea2e1Sahrens } 2228a2eea2e1Sahrens } 2229a2eea2e1Sahrens 2230a2eea2e1Sahrens uint64_t 2231a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 2232a2eea2e1Sahrens { 223391ebeef5Sahrens return (ds->ds_fsid_guid); 2234a2eea2e1Sahrens } 2235a2eea2e1Sahrens 2236a2eea2e1Sahrens void 2237a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds, 2238a2eea2e1Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 2239a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 2240fa9e4066Sahrens { 2241c1379625SJustin T. Gibbs *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes; 2242a2eea2e1Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 2243c1379625SJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) 2244c1379625SJustin T. Gibbs *availbytesp += 2245c1379625SJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes; 2246a9799022Sck if (ds->ds_quota != 0) { 2247a9799022Sck /* 2248a9799022Sck * Adjust available bytes according to refquota 2249a9799022Sck */ 2250a9799022Sck if (*refdbytesp < ds->ds_quota) 2251a9799022Sck *availbytesp = MIN(*availbytesp, 2252a9799022Sck ds->ds_quota - *refdbytesp); 2253a9799022Sck else 2254a9799022Sck *availbytesp = 0; 2255a9799022Sck } 2256c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 2257c1379625SJustin T. Gibbs *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp); 2258c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 2259a2eea2e1Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 2260fa9e4066Sahrens } 2261fa9e4066Sahrens 2262f18faf3fSek boolean_t 226334f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap) 2264f18faf3fSek { 2265f18faf3fSek dsl_pool_t *dp = ds->ds_dir->dd_pool; 2266c166b69dSPaul Dagnelie uint64_t birth; 2267f18faf3fSek 22683b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 226934f2f8cfSMatthew Ahrens if (snap == NULL) 2270f18faf3fSek return (B_FALSE); 2271c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 2272c166b69dSPaul Dagnelie birth = dsl_dataset_get_blkptr(ds)->blk_birth; 2273c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 2274c166b69dSPaul Dagnelie if (birth > dsl_dataset_phys(snap)->ds_creation_txg) { 227534f2f8cfSMatthew Ahrens objset_t *os, *os_snap; 22766e0cbcaaSMatthew Ahrens /* 22776e0cbcaaSMatthew Ahrens * It may be that only the ZIL differs, because it was 22786e0cbcaaSMatthew Ahrens * reset in the head. Don't count that as being 22796e0cbcaaSMatthew Ahrens * modified. 22806e0cbcaaSMatthew Ahrens */ 22816e0cbcaaSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) != 0) 22826e0cbcaaSMatthew Ahrens return (B_TRUE); 228334f2f8cfSMatthew Ahrens if (dmu_objset_from_ds(snap, &os_snap) != 0) 22846e0cbcaaSMatthew Ahrens return (B_TRUE); 22856e0cbcaaSMatthew Ahrens return (bcmp(&os->os_phys->os_meta_dnode, 228634f2f8cfSMatthew Ahrens &os_snap->os_phys->os_meta_dnode, 22876e0cbcaaSMatthew Ahrens sizeof (os->os_phys->os_meta_dnode)) != 0); 22886e0cbcaaSMatthew Ahrens } 2289f18faf3fSek return (B_FALSE); 2290f18faf3fSek } 2291f18faf3fSek 22923b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg { 22933b2aab18SMatthew Ahrens const char *ddrsa_fsname; 22943b2aab18SMatthew Ahrens const char *ddrsa_oldsnapname; 22953b2aab18SMatthew Ahrens const char *ddrsa_newsnapname; 22963b2aab18SMatthew Ahrens boolean_t ddrsa_recursive; 22973b2aab18SMatthew Ahrens dmu_tx_t *ddrsa_tx; 22983b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t; 22993b2aab18SMatthew Ahrens 23001d452cf5Sahrens /* ARGSUSED */ 2301fa9e4066Sahrens static int 23023b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp, 23033b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 2304fa9e4066Sahrens { 23053b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 23063b2aab18SMatthew Ahrens int error; 2307fa9e4066Sahrens uint64_t val; 2308fa9e4066Sahrens 23093b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 23103b2aab18SMatthew Ahrens if (error != 0) { 23113b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 23123b2aab18SMatthew Ahrens return (error == ENOENT ? 0 : error); 23133b2aab18SMatthew Ahrens } 23141d452cf5Sahrens 23153b2aab18SMatthew Ahrens /* new name should not exist */ 23163b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val); 23173b2aab18SMatthew Ahrens if (error == 0) 2318be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 23193b2aab18SMatthew Ahrens else if (error == ENOENT) 23203b2aab18SMatthew Ahrens error = 0; 2321cdf5b4caSmmusante 2322cdf5b4caSmmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 23233b2aab18SMatthew Ahrens if (dsl_dir_namelen(hds->ds_dir) + 1 + 23249adfa60dSMatthew Ahrens strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN) 2325be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 2326cdf5b4caSmmusante 23273b2aab18SMatthew Ahrens return (error); 23281d452cf5Sahrens } 2329fa9e4066Sahrens 23303b2aab18SMatthew Ahrens static int 23313b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx) 23321d452cf5Sahrens { 23333b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 23343b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 23351d452cf5Sahrens dsl_dataset_t *hds; 23363b2aab18SMatthew Ahrens int error; 2337fa9e4066Sahrens 23383b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds); 23393b2aab18SMatthew Ahrens if (error != 0) 23403b2aab18SMatthew Ahrens return (error); 2341fa9e4066Sahrens 23423b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 23433b2aab18SMatthew Ahrens error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 23443b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl, ddrsa, 23453b2aab18SMatthew Ahrens DS_FIND_CHILDREN); 23463b2aab18SMatthew Ahrens } else { 23473b2aab18SMatthew Ahrens error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa); 23483b2aab18SMatthew Ahrens } 2349745cd3c5Smaybee dsl_dataset_rele(hds, FTAG); 23503b2aab18SMatthew Ahrens return (error); 2351fa9e4066Sahrens } 2352fa9e4066Sahrens 2353cdf5b4caSmmusante static int 23543b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp, 23553b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 2356cdf5b4caSmmusante { 23573b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 23583b2aab18SMatthew Ahrens dsl_dataset_t *ds; 23593b2aab18SMatthew Ahrens uint64_t val; 23603b2aab18SMatthew Ahrens dmu_tx_t *tx = ddrsa->ddrsa_tx; 23613b2aab18SMatthew Ahrens int error; 2362ecd6cf80Smarks 23633b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 23643b2aab18SMatthew Ahrens ASSERT(error == 0 || error == ENOENT); 23653b2aab18SMatthew Ahrens if (error == ENOENT) { 23663b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 23673b2aab18SMatthew Ahrens return (0); 2368ecd6cf80Smarks } 2369ecd6cf80Smarks 23703b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds)); 23713b2aab18SMatthew Ahrens 23723b2aab18SMatthew Ahrens /* log before we change the name */ 23733b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "rename", tx, 23743b2aab18SMatthew Ahrens "-> @%s", ddrsa->ddrsa_newsnapname); 2375cdf5b4caSmmusante 2376a2afb611SJerry Jelinek VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx, 2377a2afb611SJerry Jelinek B_FALSE)); 23783b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 23793b2aab18SMatthew Ahrens (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname); 23803b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2381c1379625SJustin T. Gibbs VERIFY0(zap_add(dp->dp_meta_objset, 2382c1379625SJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj, 23833b2aab18SMatthew Ahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx)); 2384cdf5b4caSmmusante 23853b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2386cdf5b4caSmmusante return (0); 2387cdf5b4caSmmusante } 2388cdf5b4caSmmusante 23893b2aab18SMatthew Ahrens static void 23903b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx) 2391cdf5b4caSmmusante { 23923b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 23933b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 23943b2aab18SMatthew Ahrens dsl_dataset_t *hds; 2395cdf5b4caSmmusante 23963b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds)); 23973b2aab18SMatthew Ahrens ddrsa->ddrsa_tx = tx; 23983b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 23993b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 24003b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl, ddrsa, 24013b2aab18SMatthew Ahrens DS_FIND_CHILDREN)); 24023b2aab18SMatthew Ahrens } else { 24033b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa)); 2404cdf5b4caSmmusante } 24053b2aab18SMatthew Ahrens dsl_dataset_rele(hds, FTAG); 2406cdf5b4caSmmusante } 2407cdf5b4caSmmusante 24083b2aab18SMatthew Ahrens int 24093b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname, 24103b2aab18SMatthew Ahrens const char *oldsnapname, const char *newsnapname, boolean_t recursive) 24113a5a36beSmmusante { 24123b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t ddrsa; 24133a5a36beSmmusante 24143b2aab18SMatthew Ahrens ddrsa.ddrsa_fsname = fsname; 24153b2aab18SMatthew Ahrens ddrsa.ddrsa_oldsnapname = oldsnapname; 24163b2aab18SMatthew Ahrens ddrsa.ddrsa_newsnapname = newsnapname; 24173b2aab18SMatthew Ahrens ddrsa.ddrsa_recursive = recursive; 24183a5a36beSmmusante 24193b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check, 24207d46dc6cSMatthew Ahrens dsl_dataset_rename_snapshot_sync, &ddrsa, 24217d46dc6cSMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED)); 24223a5a36beSmmusante } 24233a5a36beSmmusante 242491948b51SKeith M Wesolowski /* 242591948b51SKeith M Wesolowski * If we're doing an ownership handoff, we need to make sure that there is 242691948b51SKeith M Wesolowski * only one long hold on the dataset. We're not allowed to change anything here 242791948b51SKeith M Wesolowski * so we don't permanently release the long hold or regular hold here. We want 242891948b51SKeith M Wesolowski * to do this only when syncing to avoid the dataset unexpectedly going away 242991948b51SKeith M Wesolowski * when we release the long hold. 243091948b51SKeith M Wesolowski */ 243191948b51SKeith M Wesolowski static int 243291948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx) 243391948b51SKeith M Wesolowski { 243491948b51SKeith M Wesolowski boolean_t held; 243591948b51SKeith M Wesolowski 243691948b51SKeith M Wesolowski if (!dmu_tx_is_syncing(tx)) 243791948b51SKeith M Wesolowski return (0); 243891948b51SKeith M Wesolowski 243991948b51SKeith M Wesolowski if (owner != NULL) { 244091948b51SKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, owner); 244191948b51SKeith M Wesolowski dsl_dataset_long_rele(ds, owner); 244291948b51SKeith M Wesolowski } 244391948b51SKeith M Wesolowski 244491948b51SKeith M Wesolowski held = dsl_dataset_long_held(ds); 244591948b51SKeith M Wesolowski 244691948b51SKeith M Wesolowski if (owner != NULL) 244791948b51SKeith M Wesolowski dsl_dataset_long_hold(ds, owner); 244891948b51SKeith M Wesolowski 244991948b51SKeith M Wesolowski if (held) 245091948b51SKeith M Wesolowski return (SET_ERROR(EBUSY)); 245191948b51SKeith M Wesolowski 245291948b51SKeith M Wesolowski return (0); 245391948b51SKeith M Wesolowski } 245491948b51SKeith M Wesolowski 2455*000cce6bSBrad Lewis int 24563b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx) 2457fa9e4066Sahrens { 245891948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 24593b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 24601d452cf5Sahrens dsl_dataset_t *ds; 24613b2aab18SMatthew Ahrens int64_t unused_refres_delta; 24623b2aab18SMatthew Ahrens int error; 2463fa9e4066Sahrens 246491948b51SKeith M Wesolowski error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds); 24653b2aab18SMatthew Ahrens if (error != 0) 24663b2aab18SMatthew Ahrens return (error); 2467370c1af0SSanjeev Bagewadi 24683b2aab18SMatthew Ahrens /* must not be a snapshot */ 2469bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 24703b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2471be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 24723b2aab18SMatthew Ahrens } 24733a5a36beSmmusante 24743b2aab18SMatthew Ahrens /* must have a most recent snapshot */ 2475c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) { 24763b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2477be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 24783b2aab18SMatthew Ahrens } 24793a5a36beSmmusante 2480bfaed0b9SAndriy Gapon /* 2481bfaed0b9SAndriy Gapon * No rollback to a snapshot created in the current txg, because 2482bfaed0b9SAndriy Gapon * the rollback may dirty the dataset and create blocks that are 2483bfaed0b9SAndriy Gapon * not reachable from the rootbp while having a birth txg that 2484bfaed0b9SAndriy Gapon * falls into the snapshot's range. 2485bfaed0b9SAndriy Gapon */ 2486bfaed0b9SAndriy Gapon if (dmu_tx_is_syncing(tx) && 2487bfaed0b9SAndriy Gapon dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) { 2488bfaed0b9SAndriy Gapon dsl_dataset_rele(ds, FTAG); 2489bfaed0b9SAndriy Gapon return (SET_ERROR(EAGAIN)); 2490bfaed0b9SAndriy Gapon } 2491bfaed0b9SAndriy Gapon 249277b17137SAndriy Gapon /* 249377b17137SAndriy Gapon * If the expected target snapshot is specified, then check that 249477b17137SAndriy Gapon * the latest snapshot is it. 249577b17137SAndriy Gapon */ 249677b17137SAndriy Gapon if (ddra->ddra_tosnap != NULL) { 249777b17137SAndriy Gapon char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 249877b17137SAndriy Gapon 249977b17137SAndriy Gapon dsl_dataset_name(ds->ds_prev, namebuf); 250077b17137SAndriy Gapon if (strcmp(namebuf, ddra->ddra_tosnap) != 0) 250177b17137SAndriy Gapon return (SET_ERROR(EXDEV)); 250277b17137SAndriy Gapon } 250377b17137SAndriy Gapon 250478f17100SMatthew Ahrens /* must not have any bookmarks after the most recent snapshot */ 250578f17100SMatthew Ahrens nvlist_t *proprequest = fnvlist_alloc(); 250678f17100SMatthew Ahrens fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG)); 250778f17100SMatthew Ahrens nvlist_t *bookmarks = fnvlist_alloc(); 250878f17100SMatthew Ahrens error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks); 250978f17100SMatthew Ahrens fnvlist_free(proprequest); 251078f17100SMatthew Ahrens if (error != 0) 251178f17100SMatthew Ahrens return (error); 251278f17100SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL); 251378f17100SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) { 251478f17100SMatthew Ahrens nvlist_t *valuenv = 251578f17100SMatthew Ahrens fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair), 251678f17100SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CREATETXG)); 251778f17100SMatthew Ahrens uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value"); 2518c1379625SJustin T. Gibbs if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) { 251978f17100SMatthew Ahrens fnvlist_free(bookmarks); 252078f17100SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 252178f17100SMatthew Ahrens return (SET_ERROR(EEXIST)); 252278f17100SMatthew Ahrens } 252378f17100SMatthew Ahrens } 252478f17100SMatthew Ahrens fnvlist_free(bookmarks); 252578f17100SMatthew Ahrens 252691948b51SKeith M Wesolowski error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx); 252791948b51SKeith M Wesolowski if (error != 0) { 25283b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 252991948b51SKeith M Wesolowski return (error); 25303b2aab18SMatthew Ahrens } 25313b2aab18SMatthew Ahrens 25323b2aab18SMatthew Ahrens /* 25333b2aab18SMatthew Ahrens * Check if the snap we are rolling back to uses more than 25343b2aab18SMatthew Ahrens * the refquota. 25353b2aab18SMatthew Ahrens */ 25363b2aab18SMatthew Ahrens if (ds->ds_quota != 0 && 2537c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) { 25383b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2539be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 2540fa9e4066Sahrens } 2541370c1af0SSanjeev Bagewadi 25423b2aab18SMatthew Ahrens /* 25433b2aab18SMatthew Ahrens * When we do the clone swap, we will temporarily use more space 25443b2aab18SMatthew Ahrens * due to the refreservation (the head will no longer have any 25453b2aab18SMatthew Ahrens * unique space, so the entire amount of the refreservation will need 25463b2aab18SMatthew Ahrens * to be free). We will immediately destroy the clone, freeing 25473b2aab18SMatthew Ahrens * this space, but the freeing happens over many txg's. 25483b2aab18SMatthew Ahrens */ 25493b2aab18SMatthew Ahrens unused_refres_delta = (int64_t)MIN(ds->ds_reserved, 2550c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 25513b2aab18SMatthew Ahrens 25523b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 25533b2aab18SMatthew Ahrens unused_refres_delta > 25543b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) { 25553b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2556be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 2557fa9e4066Sahrens } 2558fa9e4066Sahrens 25593b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 25603b2aab18SMatthew Ahrens return (0); 25613b2aab18SMatthew Ahrens } 25621d452cf5Sahrens 2563*000cce6bSBrad Lewis void 25643b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) 25653b2aab18SMatthew Ahrens { 256691948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 25673b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 25683b2aab18SMatthew Ahrens dsl_dataset_t *ds, *clone; 25693b2aab18SMatthew Ahrens uint64_t cloneobj; 25709adfa60dSMatthew Ahrens char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 25711d452cf5Sahrens 257291948b51SKeith M Wesolowski VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds)); 25731d452cf5Sahrens 2574a7027df1SMatthew Ahrens dsl_dataset_name(ds->ds_prev, namebuf); 2575a7027df1SMatthew Ahrens fnvlist_add_string(ddra->ddra_result, "target", namebuf); 2576a7027df1SMatthew Ahrens 25773b2aab18SMatthew Ahrens cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback", 25783b2aab18SMatthew Ahrens ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx); 25791d452cf5Sahrens 25803b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone)); 25811d452cf5Sahrens 25823b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(clone, ds, tx); 25833b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 25843b2aab18SMatthew Ahrens 25853b2aab18SMatthew Ahrens dsl_destroy_head_sync_impl(clone, tx); 25863b2aab18SMatthew Ahrens 25873b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 25883b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 25893b2aab18SMatthew Ahrens } 25903b2aab18SMatthew Ahrens 259191948b51SKeith M Wesolowski /* 2592a7027df1SMatthew Ahrens * Rolls back the given filesystem or volume to the most recent snapshot. 2593a7027df1SMatthew Ahrens * The name of the most recent snapshot will be returned under key "target" 2594a7027df1SMatthew Ahrens * in the result nvlist. 259591948b51SKeith M Wesolowski * 2596a7027df1SMatthew Ahrens * If owner != NULL: 259791948b51SKeith M Wesolowski * - The existing dataset MUST be owned by the specified owner at entry 259891948b51SKeith M Wesolowski * - Upon return, dataset will still be held by the same owner, whether we 259991948b51SKeith M Wesolowski * succeed or not. 260091948b51SKeith M Wesolowski * 260191948b51SKeith M Wesolowski * This mode is required any time the existing filesystem is mounted. See 260291948b51SKeith M Wesolowski * notes above zfs_suspend_fs() for further details. 260391948b51SKeith M Wesolowski */ 26043b2aab18SMatthew Ahrens int 260577b17137SAndriy Gapon dsl_dataset_rollback(const char *fsname, const char *tosnap, void *owner, 260677b17137SAndriy Gapon nvlist_t *result) 26073b2aab18SMatthew Ahrens { 260891948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t ddra; 260991948b51SKeith M Wesolowski 261091948b51SKeith M Wesolowski ddra.ddra_fsname = fsname; 261177b17137SAndriy Gapon ddra.ddra_tosnap = tosnap; 261291948b51SKeith M Wesolowski ddra.ddra_owner = owner; 2613a7027df1SMatthew Ahrens ddra.ddra_result = result; 261491948b51SKeith M Wesolowski 26153b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rollback_check, 26167d46dc6cSMatthew Ahrens dsl_dataset_rollback_sync, &ddra, 26177d46dc6cSMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED)); 2618fa9e4066Sahrens } 261999653d4eSeschrock 2620088f3894Sahrens struct promotenode { 2621745cd3c5Smaybee list_node_t link; 2622745cd3c5Smaybee dsl_dataset_t *ds; 2623745cd3c5Smaybee }; 2624745cd3c5Smaybee 262574e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 26263b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, 26273b2aab18SMatthew Ahrens void *tag); 26283b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag); 262974e7dc98SMatthew Ahrens 2630dfc11533SChris Williamson int 26313b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx) 263299653d4eSeschrock { 26333b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 26343b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 26353b2aab18SMatthew Ahrens dsl_dataset_t *hds; 26363b2aab18SMatthew Ahrens struct promotenode *snap; 26373b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 2638745cd3c5Smaybee int err; 2639cde58dbcSMatthew Ahrens uint64_t unused; 2640a2afb611SJerry Jelinek uint64_t ss_mv_cnt; 2641cb5842f8SAndriy Gapon size_t max_snap_len; 2642dfc11533SChris Williamson boolean_t conflicting_snaps; 26431d452cf5Sahrens 26443b2aab18SMatthew Ahrens err = promote_hold(ddpa, dp, FTAG); 26453b2aab18SMatthew Ahrens if (err != 0) 26463b2aab18SMatthew Ahrens return (err); 264799653d4eSeschrock 26483b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 2649dfc11533SChris Williamson snap = list_head(&ddpa->shared_snaps); 2650dfc11533SChris Williamson origin_ds = snap->ds; 2651cb5842f8SAndriy Gapon max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1; 26521d452cf5Sahrens 2653dfc11533SChris Williamson snap = list_head(&ddpa->origin_snaps); 2654dfc11533SChris Williamson 2655c1379625SJustin T. Gibbs if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) { 26563b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2657be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 26583b2aab18SMatthew Ahrens } 26593b2aab18SMatthew Ahrens 26603b2aab18SMatthew Ahrens /* 26613b2aab18SMatthew Ahrens * Compute and check the amount of space to transfer. Since this is 26623b2aab18SMatthew Ahrens * so expensive, don't do the preliminary check. 26633b2aab18SMatthew Ahrens */ 26643b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 26653b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 26663b2aab18SMatthew Ahrens return (0); 26673b2aab18SMatthew Ahrens } 26683b2aab18SMatthew Ahrens 26693cb34c60Sahrens /* compute origin's new unique space */ 26703b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 2671c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 2672c1379625SJustin T. Gibbs origin_ds->ds_object); 2673cde58dbcSMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 2674c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX, 26753b2aab18SMatthew Ahrens &ddpa->unique, &unused, &unused); 267699653d4eSeschrock 2677745cd3c5Smaybee /* 2678745cd3c5Smaybee * Walk the snapshots that we are moving 2679745cd3c5Smaybee * 268074e7dc98SMatthew Ahrens * Compute space to transfer. Consider the incremental changes 26813b2aab18SMatthew Ahrens * to used by each snapshot: 268274e7dc98SMatthew Ahrens * (my used) = (prev's used) + (blocks born) - (blocks killed) 268374e7dc98SMatthew Ahrens * So each snapshot gave birth to: 268474e7dc98SMatthew Ahrens * (blocks born) = (my used) - (prev's used) + (blocks killed) 2685745cd3c5Smaybee * So a sequence would look like: 268674e7dc98SMatthew Ahrens * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 2687745cd3c5Smaybee * Which simplifies to: 268874e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + k1 + k0 2689745cd3c5Smaybee * Note however, if we stop before we reach the ORIGIN we get: 269074e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + kM - uM-1 2691745cd3c5Smaybee */ 2692dfc11533SChris Williamson conflicting_snaps = B_FALSE; 2693a2afb611SJerry Jelinek ss_mv_cnt = 0; 2694c1379625SJustin T. Gibbs ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes; 2695c1379625SJustin T. Gibbs ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes; 2696c1379625SJustin T. Gibbs ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes; 26973b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 26983b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 269999653d4eSeschrock uint64_t val, dlused, dlcomp, dluncomp; 2700745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 270199653d4eSeschrock 2702a2afb611SJerry Jelinek ss_mv_cnt++; 2703a2afb611SJerry Jelinek 27043b2aab18SMatthew Ahrens /* 27053b2aab18SMatthew Ahrens * If there are long holds, we won't be able to evict 27063b2aab18SMatthew Ahrens * the objset. 27073b2aab18SMatthew Ahrens */ 27083b2aab18SMatthew Ahrens if (dsl_dataset_long_held(ds)) { 2709be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 27103b2aab18SMatthew Ahrens goto out; 27113b2aab18SMatthew Ahrens } 27123b2aab18SMatthew Ahrens 271399653d4eSeschrock /* Check that the snapshot name does not conflict */ 27143b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 2715cb5842f8SAndriy Gapon if (strlen(ds->ds_snapname) >= max_snap_len) { 2716cb5842f8SAndriy Gapon err = SET_ERROR(ENAMETOOLONG); 2717cb5842f8SAndriy Gapon goto out; 2718cb5842f8SAndriy Gapon } 2719745cd3c5Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 2720681d9761SEric Taylor if (err == 0) { 2721dfc11533SChris Williamson fnvlist_add_boolean(ddpa->err_ds, 2722dfc11533SChris Williamson snap->ds->ds_snapname); 2723dfc11533SChris Williamson conflicting_snaps = B_TRUE; 2724dfc11533SChris Williamson } else if (err != ENOENT) { 2725681d9761SEric Taylor goto out; 2726681d9761SEric Taylor } 272799653d4eSeschrock 2728745cd3c5Smaybee /* The very first snapshot does not have a deadlist */ 2729c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0) 273074e7dc98SMatthew Ahrens continue; 273174e7dc98SMatthew Ahrens 2732cde58dbcSMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, 2733cde58dbcSMatthew Ahrens &dlused, &dlcomp, &dluncomp); 27343b2aab18SMatthew Ahrens ddpa->used += dlused; 27353b2aab18SMatthew Ahrens ddpa->comp += dlcomp; 27363b2aab18SMatthew Ahrens ddpa->uncomp += dluncomp; 273774e7dc98SMatthew Ahrens } 2738745cd3c5Smaybee 2739dfc11533SChris Williamson /* 2740dfc11533SChris Williamson * In order to return the full list of conflicting snapshots, we check 2741dfc11533SChris Williamson * whether there was a conflict after traversing all of them. 2742dfc11533SChris Williamson */ 2743dfc11533SChris Williamson if (conflicting_snaps) { 2744dfc11533SChris Williamson err = SET_ERROR(EEXIST); 2745dfc11533SChris Williamson goto out; 2746dfc11533SChris Williamson } 2747dfc11533SChris Williamson 2748745cd3c5Smaybee /* 2749745cd3c5Smaybee * If we are a clone of a clone then we never reached ORIGIN, 2750745cd3c5Smaybee * so we need to subtract out the clone origin's used space. 2751745cd3c5Smaybee */ 27523b2aab18SMatthew Ahrens if (ddpa->origin_origin) { 2753c1379625SJustin T. Gibbs ddpa->used -= 2754c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes; 2755c1379625SJustin T. Gibbs ddpa->comp -= 2756c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes; 27573b2aab18SMatthew Ahrens ddpa->uncomp -= 2758c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)-> 2759c1379625SJustin T. Gibbs ds_uncompressed_bytes; 276099653d4eSeschrock } 276199653d4eSeschrock 2762a2afb611SJerry Jelinek /* Check that there is enough space and limit headroom here */ 276374e7dc98SMatthew Ahrens err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 2764a2afb611SJerry Jelinek 0, ss_mv_cnt, ddpa->used, ddpa->cr); 27653b2aab18SMatthew Ahrens if (err != 0) 27663b2aab18SMatthew Ahrens goto out; 276774e7dc98SMatthew Ahrens 276874e7dc98SMatthew Ahrens /* 276974e7dc98SMatthew Ahrens * Compute the amounts of space that will be used by snapshots 277074e7dc98SMatthew Ahrens * after the promotion (for both origin and clone). For each, 277174e7dc98SMatthew Ahrens * it is the amount of space that will be on all of their 277274e7dc98SMatthew Ahrens * deadlists (that was not born before their new origin). 277374e7dc98SMatthew Ahrens */ 2774c1379625SJustin T. Gibbs if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) { 277574e7dc98SMatthew Ahrens uint64_t space; 277674e7dc98SMatthew Ahrens 277774e7dc98SMatthew Ahrens /* 277874e7dc98SMatthew Ahrens * Note, typically this will not be a clone of a clone, 27793f9d6ad7SLin Ling * so dd_origin_txg will be < TXG_INITIAL, so 2780cde58dbcSMatthew Ahrens * these snaplist_space() -> dsl_deadlist_space_range() 278174e7dc98SMatthew Ahrens * calls will be fast because they do not have to 278274e7dc98SMatthew Ahrens * iterate over all bps. 278374e7dc98SMatthew Ahrens */ 27843b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 27853b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->shared_snaps, 27863b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap); 27873b2aab18SMatthew Ahrens if (err != 0) 27883b2aab18SMatthew Ahrens goto out; 278974e7dc98SMatthew Ahrens 27903b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->clone_snaps, 27913f9d6ad7SLin Ling snap->ds->ds_dir->dd_origin_txg, &space); 27923b2aab18SMatthew Ahrens if (err != 0) 27933b2aab18SMatthew Ahrens goto out; 27943b2aab18SMatthew Ahrens ddpa->cloneusedsnap += space; 279574e7dc98SMatthew Ahrens } 2796c1379625SJustin T. Gibbs if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags & 2797c1379625SJustin T. Gibbs DD_FLAG_USED_BREAKDOWN) { 27983b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->origin_snaps, 2799c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg, 2800c1379625SJustin T. Gibbs &ddpa->originusedsnap); 28013b2aab18SMatthew Ahrens if (err != 0) 28023b2aab18SMatthew Ahrens goto out; 2803745cd3c5Smaybee } 28041d452cf5Sahrens 2805681d9761SEric Taylor out: 28063b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2807681d9761SEric Taylor return (err); 28081d452cf5Sahrens } 280999653d4eSeschrock 2810dfc11533SChris Williamson void 28113b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx) 28121d452cf5Sahrens { 28133b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 28143b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 28153b2aab18SMatthew Ahrens dsl_dataset_t *hds; 28163b2aab18SMatthew Ahrens struct promotenode *snap; 28173b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 28183b2aab18SMatthew Ahrens dsl_dataset_t *origin_head; 28193b2aab18SMatthew Ahrens dsl_dir_t *dd; 28203cb34c60Sahrens dsl_dir_t *odd = NULL; 2821088f3894Sahrens uint64_t oldnext_obj; 282274e7dc98SMatthew Ahrens int64_t delta; 28231d452cf5Sahrens 28243b2aab18SMatthew Ahrens VERIFY0(promote_hold(ddpa, dp, FTAG)); 28253b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 28263b2aab18SMatthew Ahrens 2827c1379625SJustin T. Gibbs ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE); 28281d452cf5Sahrens 28293b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 28303b2aab18SMatthew Ahrens origin_ds = snap->ds; 28313b2aab18SMatthew Ahrens dd = hds->ds_dir; 28323b2aab18SMatthew Ahrens 28333b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 283474e7dc98SMatthew Ahrens origin_head = snap->ds; 283574e7dc98SMatthew Ahrens 28360b69c2f0Sahrens /* 28373cb34c60Sahrens * We need to explicitly open odd, since origin_ds's dd will be 28380b69c2f0Sahrens * changing. 28390b69c2f0Sahrens */ 28403b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object, 28413cb34c60Sahrens NULL, FTAG, &odd)); 284299653d4eSeschrock 2843745cd3c5Smaybee /* change origin's next snap */ 2844745cd3c5Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2845c1379625SJustin T. Gibbs oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj; 28463b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 2847c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 2848c1379625SJustin T. Gibbs origin_ds->ds_object); 2849c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object; 2850745cd3c5Smaybee 2851088f3894Sahrens /* change the origin's next clone */ 2852c1379625SJustin T. Gibbs if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) { 28533b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(origin_ds, 28543b2aab18SMatthew Ahrens snap->ds->ds_object, tx); 28553b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2856c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_clones_obj, 2857088f3894Sahrens oldnext_obj, tx)); 2858088f3894Sahrens } 2859088f3894Sahrens 2860745cd3c5Smaybee /* change origin */ 2861745cd3c5Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 2862c1379625SJustin T. Gibbs ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object); 2863c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj; 28643f9d6ad7SLin Ling dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg; 2865745cd3c5Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 2866c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object; 28673f9d6ad7SLin Ling origin_head->ds_dir->dd_origin_txg = 2868c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg; 2869745cd3c5Smaybee 2870cde58dbcSMatthew Ahrens /* change dd_clone entries */ 2871cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 28723b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2873c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx)); 28743b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2875c1379625SJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 2876cde58dbcSMatthew Ahrens hds->ds_object, tx)); 2877cde58dbcSMatthew Ahrens 28783b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2879c1379625SJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 2880cde58dbcSMatthew Ahrens origin_head->ds_object, tx)); 2881c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_clones == 0) { 2882c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones = 2883c1379625SJustin T. Gibbs zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES, 2884c1379625SJustin T. Gibbs DMU_OT_NONE, 0, tx); 2885cde58dbcSMatthew Ahrens } 28863b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2887c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx)); 2888cde58dbcSMatthew Ahrens } 2889cde58dbcSMatthew Ahrens 289099653d4eSeschrock /* move snapshots to this dir */ 28913b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 28923b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 2893745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 289499653d4eSeschrock 28953b2aab18SMatthew Ahrens /* 28963b2aab18SMatthew Ahrens * Property callbacks are registered to a particular 28973b2aab18SMatthew Ahrens * dsl_dir. Since ours is changing, evict the objset 28983b2aab18SMatthew Ahrens * so that they will be unregistered from the old dsl_dir. 28993b2aab18SMatthew Ahrens */ 2900503ad85cSMatthew Ahrens if (ds->ds_objset) { 2901503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 2902503ad85cSMatthew Ahrens ds->ds_objset = NULL; 29033baa08fcSek } 29043b2aab18SMatthew Ahrens 290599653d4eSeschrock /* move snap name entry */ 29063b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 29073b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_snap_remove(origin_head, 2908a2afb611SJerry Jelinek ds->ds_snapname, tx, B_TRUE)); 29093b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, 2910c1379625SJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname, 291199653d4eSeschrock 8, 1, &ds->ds_object, tx)); 2912a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(hds->ds_dir, 1, 2913a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 2914cde58dbcSMatthew Ahrens 291599653d4eSeschrock /* change containing dsl_dir */ 291699653d4eSeschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 2917c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object); 2918c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object; 29193cb34c60Sahrens ASSERT3P(ds->ds_dir, ==, odd); 29203b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 29213b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object, 292299653d4eSeschrock NULL, ds, &ds->ds_dir)); 292399653d4eSeschrock 2924cde58dbcSMatthew Ahrens /* move any clone references */ 2925c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj && 2926cde58dbcSMatthew Ahrens spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2927cde58dbcSMatthew Ahrens zap_cursor_t zc; 2928cde58dbcSMatthew Ahrens zap_attribute_t za; 2929cde58dbcSMatthew Ahrens 29303b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 2931c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj); 29323b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 29333b2aab18SMatthew Ahrens zap_cursor_advance(&zc)) { 29343b2aab18SMatthew Ahrens dsl_dataset_t *cnds; 29353b2aab18SMatthew Ahrens uint64_t o; 2936a9799022Sck 29373b2aab18SMatthew Ahrens if (za.za_first_integer == oldnext_obj) { 29383b2aab18SMatthew Ahrens /* 29393b2aab18SMatthew Ahrens * We've already moved the 29403b2aab18SMatthew Ahrens * origin's reference. 29413b2aab18SMatthew Ahrens */ 29423b2aab18SMatthew Ahrens continue; 29433b2aab18SMatthew Ahrens } 2944a9799022Sck 29453b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 29463b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &cnds)); 2947c1379625SJustin T. Gibbs o = dsl_dir_phys(cnds->ds_dir)-> 2948c1379625SJustin T. Gibbs dd_head_dataset_obj; 2949a9799022Sck 29503b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2951c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, o, tx)); 29523b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2953c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, o, tx)); 29543b2aab18SMatthew Ahrens dsl_dataset_rele(cnds, FTAG); 29553b2aab18SMatthew Ahrens } 29563b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 29573b2aab18SMatthew Ahrens } 29589082849eSck 29593b2aab18SMatthew Ahrens ASSERT(!dsl_prop_hascb(ds)); 2960a9799022Sck } 2961a9799022Sck 2962a9799022Sck /* 29633b2aab18SMatthew Ahrens * Change space accounting. 29643b2aab18SMatthew Ahrens * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 29653b2aab18SMatthew Ahrens * both be valid, or both be 0 (resulting in delta == 0). This 29663b2aab18SMatthew Ahrens * is true for each of {clone,origin} independently. 2967a9799022Sck */ 2968a9799022Sck 29693b2aab18SMatthew Ahrens delta = ddpa->cloneusedsnap - 2970c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]; 29713b2aab18SMatthew Ahrens ASSERT3S(delta, >=, 0); 29723b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, delta); 29733b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 29743b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_HEAD, 29753b2aab18SMatthew Ahrens ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx); 29763b2aab18SMatthew Ahrens 29773b2aab18SMatthew Ahrens delta = ddpa->originusedsnap - 2978c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP]; 29793b2aab18SMatthew Ahrens ASSERT3S(delta, <=, 0); 29803b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, -delta); 29813b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 29823b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_HEAD, 29833b2aab18SMatthew Ahrens -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx); 29843b2aab18SMatthew Ahrens 2985c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique; 29863b2aab18SMatthew Ahrens 29873b2aab18SMatthew Ahrens /* log history record */ 29883b2aab18SMatthew Ahrens spa_history_log_internal_ds(hds, "promote", tx, ""); 29893b2aab18SMatthew Ahrens 29903b2aab18SMatthew Ahrens dsl_dir_rele(odd, FTAG); 29913b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2992a9799022Sck } 2993a9799022Sck 29943b2aab18SMatthew Ahrens /* 29953b2aab18SMatthew Ahrens * Make a list of dsl_dataset_t's for the snapshots between first_obj 29963b2aab18SMatthew Ahrens * (exclusive) and last_obj (inclusive). The list will be in reverse 29973b2aab18SMatthew Ahrens * order (last_obj will be the list_head()). If first_obj == 0, do all 29983b2aab18SMatthew Ahrens * snapshots back to this dataset's origin. 29993b2aab18SMatthew Ahrens */ 3000a9799022Sck static int 30013b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp, 30023b2aab18SMatthew Ahrens uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag) 3003a9799022Sck { 30043b2aab18SMatthew Ahrens uint64_t obj = last_obj; 3005a9799022Sck 30063b2aab18SMatthew Ahrens list_create(l, sizeof (struct promotenode), 30073b2aab18SMatthew Ahrens offsetof(struct promotenode, link)); 3008a9799022Sck 30093b2aab18SMatthew Ahrens while (obj != first_obj) { 30103b2aab18SMatthew Ahrens dsl_dataset_t *ds; 30113b2aab18SMatthew Ahrens struct promotenode *snap; 30123b2aab18SMatthew Ahrens int err; 301392241e0bSTom Erickson 30143b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 30153b2aab18SMatthew Ahrens ASSERT(err != ENOENT); 30163b2aab18SMatthew Ahrens if (err != 0) 30173b2aab18SMatthew Ahrens return (err); 3018a9799022Sck 30193b2aab18SMatthew Ahrens if (first_obj == 0) 3020c1379625SJustin T. Gibbs first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj; 30213b2aab18SMatthew Ahrens 30223b2aab18SMatthew Ahrens snap = kmem_alloc(sizeof (*snap), KM_SLEEP); 30233b2aab18SMatthew Ahrens snap->ds = ds; 30243b2aab18SMatthew Ahrens list_insert_tail(l, snap); 3025c1379625SJustin T. Gibbs obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 30263b2aab18SMatthew Ahrens } 3027a9799022Sck 3028a9799022Sck return (0); 3029a9799022Sck } 3030a9799022Sck 30313b2aab18SMatthew Ahrens static int 30323b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 3033a9799022Sck { 30343b2aab18SMatthew Ahrens struct promotenode *snap; 3035a9799022Sck 30363b2aab18SMatthew Ahrens *spacep = 0; 30373b2aab18SMatthew Ahrens for (snap = list_head(l); snap; snap = list_next(l, snap)) { 30383b2aab18SMatthew Ahrens uint64_t used, comp, uncomp; 30393b2aab18SMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 30403b2aab18SMatthew Ahrens mintxg, UINT64_MAX, &used, &comp, &uncomp); 30413b2aab18SMatthew Ahrens *spacep += used; 304292241e0bSTom Erickson } 30433b2aab18SMatthew Ahrens return (0); 3044a9799022Sck } 3045a9799022Sck 30463b2aab18SMatthew Ahrens static void 30473b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag) 3048a9799022Sck { 30493b2aab18SMatthew Ahrens struct promotenode *snap; 305092241e0bSTom Erickson 30513b2aab18SMatthew Ahrens if (l == NULL || !list_link_active(&l->list_head)) 30523b2aab18SMatthew Ahrens return; 3053a9799022Sck 30543b2aab18SMatthew Ahrens while ((snap = list_tail(l)) != NULL) { 30553b2aab18SMatthew Ahrens list_remove(l, snap); 30563b2aab18SMatthew Ahrens dsl_dataset_rele(snap->ds, tag); 30573b2aab18SMatthew Ahrens kmem_free(snap, sizeof (*snap)); 30583b2aab18SMatthew Ahrens } 30593b2aab18SMatthew Ahrens list_destroy(l); 3060a9799022Sck } 3061a9799022Sck 3062a9799022Sck static int 30633b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag) 3064a9799022Sck { 30653b2aab18SMatthew Ahrens int error; 30663b2aab18SMatthew Ahrens dsl_dir_t *dd; 30673b2aab18SMatthew Ahrens struct promotenode *snap; 3068a9799022Sck 30693b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag, 30703b2aab18SMatthew Ahrens &ddpa->ddpa_clone); 30713b2aab18SMatthew Ahrens if (error != 0) 30723b2aab18SMatthew Ahrens return (error); 30733b2aab18SMatthew Ahrens dd = ddpa->ddpa_clone->ds_dir; 3074a9799022Sck 3075bc9014e6SJustin Gibbs if (ddpa->ddpa_clone->ds_is_snapshot || 30763b2aab18SMatthew Ahrens !dsl_dir_is_clone(dd)) { 30773b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 3078be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 30793b2aab18SMatthew Ahrens } 3080a9799022Sck 3081c1379625SJustin T. Gibbs error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj, 30823b2aab18SMatthew Ahrens &ddpa->shared_snaps, tag); 30833b2aab18SMatthew Ahrens if (error != 0) 30843b2aab18SMatthew Ahrens goto out; 3085a9799022Sck 30863b2aab18SMatthew Ahrens error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object, 30873b2aab18SMatthew Ahrens &ddpa->clone_snaps, tag); 30883b2aab18SMatthew Ahrens if (error != 0) 30893b2aab18SMatthew Ahrens goto out; 3090a9799022Sck 30913b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 3092c1379625SJustin T. Gibbs ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj); 3093c1379625SJustin T. Gibbs error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj, 3094c1379625SJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj, 30953b2aab18SMatthew Ahrens &ddpa->origin_snaps, tag); 30963b2aab18SMatthew Ahrens if (error != 0) 30973b2aab18SMatthew Ahrens goto out; 3098379c004dSEric Schrock 3099c1379625SJustin T. Gibbs if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) { 31003b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 3101c1379625SJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj, 31023b2aab18SMatthew Ahrens tag, &ddpa->origin_origin); 31033b2aab18SMatthew Ahrens if (error != 0) 31043b2aab18SMatthew Ahrens goto out; 3105379c004dSEric Schrock } 31063b2aab18SMatthew Ahrens out: 31073b2aab18SMatthew Ahrens if (error != 0) 31083b2aab18SMatthew Ahrens promote_rele(ddpa, tag); 31093b2aab18SMatthew Ahrens return (error); 3110a9799022Sck } 3111a9799022Sck 3112a9799022Sck static void 31133b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag) 3114a9799022Sck { 31153b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->shared_snaps, tag); 31163b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->clone_snaps, tag); 31173b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->origin_snaps, tag); 31183b2aab18SMatthew Ahrens if (ddpa->origin_origin != NULL) 31193b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->origin_origin, tag); 31203b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 31213b2aab18SMatthew Ahrens } 312202c8f3f0SMatthew Ahrens 31233b2aab18SMatthew Ahrens /* 31243b2aab18SMatthew Ahrens * Promote a clone. 31253b2aab18SMatthew Ahrens * 31263b2aab18SMatthew Ahrens * If it fails due to a conflicting snapshot name, "conflsnap" will be filled 31279adfa60dSMatthew Ahrens * in with the name. (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.) 31283b2aab18SMatthew Ahrens */ 31293b2aab18SMatthew Ahrens int 31303b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap) 31313b2aab18SMatthew Ahrens { 31323b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t ddpa = { 0 }; 31333b2aab18SMatthew Ahrens uint64_t numsnaps; 31343b2aab18SMatthew Ahrens int error; 3135dfc11533SChris Williamson nvpair_t *snap_pair; 31363b2aab18SMatthew Ahrens objset_t *os; 313792241e0bSTom Erickson 31383b2aab18SMatthew Ahrens /* 31393b2aab18SMatthew Ahrens * We will modify space proportional to the number of 31403b2aab18SMatthew Ahrens * snapshots. Compute numsnaps. 31413b2aab18SMatthew Ahrens */ 31423b2aab18SMatthew Ahrens error = dmu_objset_hold(name, FTAG, &os); 31433b2aab18SMatthew Ahrens if (error != 0) 31443b2aab18SMatthew Ahrens return (error); 31453b2aab18SMatthew Ahrens error = zap_count(dmu_objset_pool(os)->dp_meta_objset, 3146c1379625SJustin T. Gibbs dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj, 3147c1379625SJustin T. Gibbs &numsnaps); 31483b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 31493b2aab18SMatthew Ahrens if (error != 0) 31503b2aab18SMatthew Ahrens return (error); 315102c8f3f0SMatthew Ahrens 31523b2aab18SMatthew Ahrens ddpa.ddpa_clonename = name; 3153dfc11533SChris Williamson ddpa.err_ds = fnvlist_alloc(); 3154a2afb611SJerry Jelinek ddpa.cr = CRED(); 315502c8f3f0SMatthew Ahrens 3156dfc11533SChris Williamson error = dsl_sync_task(name, dsl_dataset_promote_check, 31577d46dc6cSMatthew Ahrens dsl_dataset_promote_sync, &ddpa, 3158dfc11533SChris Williamson 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED); 3159dfc11533SChris Williamson 3160dfc11533SChris Williamson /* 3161dfc11533SChris Williamson * Return the first conflicting snapshot found. 3162dfc11533SChris Williamson */ 3163dfc11533SChris Williamson snap_pair = nvlist_next_nvpair(ddpa.err_ds, NULL); 3164dfc11533SChris Williamson if (snap_pair != NULL && conflsnap != NULL) 3165dfc11533SChris Williamson (void) strcpy(conflsnap, nvpair_name(snap_pair)); 3166dfc11533SChris Williamson 3167dfc11533SChris Williamson fnvlist_free(ddpa.err_ds); 3168dfc11533SChris Williamson return (error); 3169a9799022Sck } 3170a9799022Sck 3171a9799022Sck int 31723b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone, 317391948b51SKeith M Wesolowski dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx) 3174a9799022Sck { 31755f7a8e6dSDan McDonald /* 31765f7a8e6dSDan McDonald * "slack" factor for received datasets with refquota set on them. 31775f7a8e6dSDan McDonald * See the bottom of this function for details on its use. 31785f7a8e6dSDan McDonald */ 31795f7a8e6dSDan McDonald uint64_t refquota_slack = DMU_MAX_ACCESS * spa_asize_inflation; 31803b2aab18SMatthew Ahrens int64_t unused_refres_delta; 3181a9799022Sck 31823b2aab18SMatthew Ahrens /* they should both be heads */ 3183bc9014e6SJustin Gibbs if (clone->ds_is_snapshot || 3184bc9014e6SJustin Gibbs origin_head->ds_is_snapshot) 3185be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 318692241e0bSTom Erickson 318734f2f8cfSMatthew Ahrens /* if we are not forcing, the branch point should be just before them */ 318834f2f8cfSMatthew Ahrens if (!force && clone->ds_prev != origin_head->ds_prev) 3189be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3190a9799022Sck 31913b2aab18SMatthew Ahrens /* clone should be the clone (unless they are unrelated) */ 31923b2aab18SMatthew Ahrens if (clone->ds_prev != NULL && 31933b2aab18SMatthew Ahrens clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap && 319434f2f8cfSMatthew Ahrens origin_head->ds_dir != clone->ds_prev->ds_dir) 3195be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 319692241e0bSTom Erickson 31973b2aab18SMatthew Ahrens /* the clone should be a child of the origin */ 31983b2aab18SMatthew Ahrens if (clone->ds_dir->dd_parent != origin_head->ds_dir) 3199be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3200842727c2SChris Kirby 32013b2aab18SMatthew Ahrens /* origin_head shouldn't be modified unless 'force' */ 320234f2f8cfSMatthew Ahrens if (!force && 320334f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev)) 3204be6fd75aSMatthew Ahrens return (SET_ERROR(ETXTBSY)); 3205c99e4bdcSChris Kirby 32063b2aab18SMatthew Ahrens /* origin_head should have no long holds (e.g. is not mounted) */ 320791948b51SKeith M Wesolowski if (dsl_dataset_handoff_check(origin_head, owner, tx)) 3208be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 32093b2aab18SMatthew Ahrens 32103b2aab18SMatthew Ahrens /* check amount of any unconsumed refreservation */ 32113b2aab18SMatthew Ahrens unused_refres_delta = 32123b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 3213c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) - 32143b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 3215c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 32163b2aab18SMatthew Ahrens 32173b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 32183b2aab18SMatthew Ahrens unused_refres_delta > 32193b2aab18SMatthew Ahrens dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE)) 3220be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 32213b2aab18SMatthew Ahrens 32225f7a8e6dSDan McDonald /* 32235f7a8e6dSDan McDonald * The clone can't be too much over the head's refquota. 32245f7a8e6dSDan McDonald * 32255f7a8e6dSDan McDonald * To ensure that the entire refquota can be used, we allow one 32265f7a8e6dSDan McDonald * transaction to exceed the the refquota. Therefore, this check 32275f7a8e6dSDan McDonald * needs to also allow for the space referenced to be more than the 32285f7a8e6dSDan McDonald * refquota. The maximum amount of space that one transaction can use 32295f7a8e6dSDan McDonald * on disk is DMU_MAX_ACCESS * spa_asize_inflation. Allowing this 32305f7a8e6dSDan McDonald * overage ensures that we are able to receive a filesystem that 32315f7a8e6dSDan McDonald * exceeds the refquota on the source system. 32325f7a8e6dSDan McDonald * 32335f7a8e6dSDan McDonald * So that overage is the refquota_slack we use below. 32345f7a8e6dSDan McDonald */ 32353b2aab18SMatthew Ahrens if (origin_head->ds_quota != 0 && 3236c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes > 32375f7a8e6dSDan McDonald origin_head->ds_quota + refquota_slack) 3238be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 3239c99e4bdcSChris Kirby 32403b2aab18SMatthew Ahrens return (0); 3241c99e4bdcSChris Kirby } 3242c99e4bdcSChris Kirby 3243a7f53a56SChris Kirby void 32443b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone, 32453b2aab18SMatthew Ahrens dsl_dataset_t *origin_head, dmu_tx_t *tx) 3246a7f53a56SChris Kirby { 32473b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 32483b2aab18SMatthew Ahrens int64_t unused_refres_delta; 3249a7f53a56SChris Kirby 32503b2aab18SMatthew Ahrens ASSERT(clone->ds_reserved == 0); 32515f7a8e6dSDan McDonald /* 32525f7a8e6dSDan McDonald * NOTE: On DEBUG kernels there could be a race between this and 32535f7a8e6dSDan McDonald * the check function if spa_asize_inflation is adjusted... 32545f7a8e6dSDan McDonald */ 32553b2aab18SMatthew Ahrens ASSERT(origin_head->ds_quota == 0 || 32565f7a8e6dSDan McDonald dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota + 32575f7a8e6dSDan McDonald DMU_MAX_ACCESS * spa_asize_inflation); 325834f2f8cfSMatthew Ahrens ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev); 3259842727c2SChris Kirby 3260ca0cc391SMatthew Ahrens /* 3261ca0cc391SMatthew Ahrens * Swap per-dataset feature flags. 3262ca0cc391SMatthew Ahrens */ 3263ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 3264ca0cc391SMatthew Ahrens if (!(spa_feature_table[f].fi_flags & 3265ca0cc391SMatthew Ahrens ZFEATURE_FLAG_PER_DATASET)) { 3266ca0cc391SMatthew Ahrens ASSERT(!clone->ds_feature_inuse[f]); 3267ca0cc391SMatthew Ahrens ASSERT(!origin_head->ds_feature_inuse[f]); 3268ca0cc391SMatthew Ahrens continue; 3269ca0cc391SMatthew Ahrens } 3270ca0cc391SMatthew Ahrens 3271ca0cc391SMatthew Ahrens boolean_t clone_inuse = clone->ds_feature_inuse[f]; 3272ca0cc391SMatthew Ahrens boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f]; 3273ca0cc391SMatthew Ahrens 3274ca0cc391SMatthew Ahrens if (clone_inuse) { 3275ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(clone->ds_object, f, tx); 3276ca0cc391SMatthew Ahrens clone->ds_feature_inuse[f] = B_FALSE; 3277ca0cc391SMatthew Ahrens } 3278ca0cc391SMatthew Ahrens if (origin_head_inuse) { 3279ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(origin_head->ds_object, 3280ca0cc391SMatthew Ahrens f, tx); 3281ca0cc391SMatthew Ahrens origin_head->ds_feature_inuse[f] = B_FALSE; 3282ca0cc391SMatthew Ahrens } 3283ca0cc391SMatthew Ahrens if (clone_inuse) { 3284ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(origin_head->ds_object, 3285ca0cc391SMatthew Ahrens f, tx); 3286ca0cc391SMatthew Ahrens origin_head->ds_feature_inuse[f] = B_TRUE; 3287ca0cc391SMatthew Ahrens } 3288ca0cc391SMatthew Ahrens if (origin_head_inuse) { 3289ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(clone->ds_object, f, tx); 3290ca0cc391SMatthew Ahrens clone->ds_feature_inuse[f] = B_TRUE; 3291ca0cc391SMatthew Ahrens } 3292ca0cc391SMatthew Ahrens } 3293ca0cc391SMatthew Ahrens 32943b2aab18SMatthew Ahrens dmu_buf_will_dirty(clone->ds_dbuf, tx); 32953b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin_head->ds_dbuf, tx); 3296842727c2SChris Kirby 32973b2aab18SMatthew Ahrens if (clone->ds_objset != NULL) { 32983b2aab18SMatthew Ahrens dmu_objset_evict(clone->ds_objset); 32993b2aab18SMatthew Ahrens clone->ds_objset = NULL; 33003b2aab18SMatthew Ahrens } 3301842727c2SChris Kirby 33023b2aab18SMatthew Ahrens if (origin_head->ds_objset != NULL) { 33033b2aab18SMatthew Ahrens dmu_objset_evict(origin_head->ds_objset); 33043b2aab18SMatthew Ahrens origin_head->ds_objset = NULL; 3305842727c2SChris Kirby } 3306842727c2SChris Kirby 33073b2aab18SMatthew Ahrens unused_refres_delta = 33083b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 3309c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) - 33103b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 3311c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 33123b2aab18SMatthew Ahrens 33133b2aab18SMatthew Ahrens /* 33143b2aab18SMatthew Ahrens * Reset origin's unique bytes, if it exists. 33153b2aab18SMatthew Ahrens */ 33163b2aab18SMatthew Ahrens if (clone->ds_prev) { 33173b2aab18SMatthew Ahrens dsl_dataset_t *origin = clone->ds_prev; 33183b2aab18SMatthew Ahrens uint64_t comp, uncomp; 33193b2aab18SMatthew Ahrens 33203b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 33213b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 3322c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX, 3323c1379625SJustin T. Gibbs &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp); 33243b2aab18SMatthew Ahrens } 33253b2aab18SMatthew Ahrens 33263b2aab18SMatthew Ahrens /* swap blkptrs */ 33273b2aab18SMatthew Ahrens { 3328c166b69dSPaul Dagnelie rrw_enter(&clone->ds_bp_rwlock, RW_WRITER, FTAG); 3329c166b69dSPaul Dagnelie rrw_enter(&origin_head->ds_bp_rwlock, RW_WRITER, FTAG); 33303b2aab18SMatthew Ahrens blkptr_t tmp; 3331c1379625SJustin T. Gibbs tmp = dsl_dataset_phys(origin_head)->ds_bp; 3332c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_bp = 3333c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp; 3334c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp = tmp; 3335c166b69dSPaul Dagnelie rrw_exit(&origin_head->ds_bp_rwlock, FTAG); 3336c166b69dSPaul Dagnelie rrw_exit(&clone->ds_bp_rwlock, FTAG); 33373b2aab18SMatthew Ahrens } 33383b2aab18SMatthew Ahrens 33393b2aab18SMatthew Ahrens /* set dd_*_bytes */ 33403b2aab18SMatthew Ahrens { 33413b2aab18SMatthew Ahrens int64_t dused, dcomp, duncomp; 33423b2aab18SMatthew Ahrens uint64_t cdl_used, cdl_comp, cdl_uncomp; 33433b2aab18SMatthew Ahrens uint64_t odl_used, odl_comp, odl_uncomp; 33443b2aab18SMatthew Ahrens 3345c1379625SJustin T. Gibbs ASSERT3U(dsl_dir_phys(clone->ds_dir)-> 33463b2aab18SMatthew Ahrens dd_used_breakdown[DD_USED_SNAP], ==, 0); 33473b2aab18SMatthew Ahrens 33483b2aab18SMatthew Ahrens dsl_deadlist_space(&clone->ds_deadlist, 33493b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 33503b2aab18SMatthew Ahrens dsl_deadlist_space(&origin_head->ds_deadlist, 33513b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 335215508ac0SChris Kirby 3353c1379625SJustin T. Gibbs dused = dsl_dataset_phys(clone)->ds_referenced_bytes + 3354c1379625SJustin T. Gibbs cdl_used - 3355c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_referenced_bytes + 3356c1379625SJustin T. Gibbs odl_used); 3357c1379625SJustin T. Gibbs dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes + 3358c1379625SJustin T. Gibbs cdl_comp - 3359c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_compressed_bytes + 3360c1379625SJustin T. Gibbs odl_comp); 3361c1379625SJustin T. Gibbs duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes + 33623b2aab18SMatthew Ahrens cdl_uncomp - 3363c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes + 3364c1379625SJustin T. Gibbs odl_uncomp); 3365842727c2SChris Kirby 33663b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD, 33673b2aab18SMatthew Ahrens dused, dcomp, duncomp, tx); 33683b2aab18SMatthew Ahrens dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD, 33693b2aab18SMatthew Ahrens -dused, -dcomp, -duncomp, tx); 3370842727c2SChris Kirby 3371842727c2SChris Kirby /* 33723b2aab18SMatthew Ahrens * The difference in the space used by snapshots is the 33733b2aab18SMatthew Ahrens * difference in snapshot space due to the head's 33743b2aab18SMatthew Ahrens * deadlist (since that's the only thing that's 33753b2aab18SMatthew Ahrens * changing that affects the snapused). 3376842727c2SChris Kirby */ 33773b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 33783b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 33793b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 33803b2aab18SMatthew Ahrens dsl_deadlist_space_range(&origin_head->ds_deadlist, 33813b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 33823b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 33833b2aab18SMatthew Ahrens dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used, 33843b2aab18SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 3385842727c2SChris Kirby } 3386842727c2SChris Kirby 33873b2aab18SMatthew Ahrens /* swap ds_*_bytes */ 3388c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes, 3389c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes); 3390c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes, 3391c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_compressed_bytes); 3392c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes, 3393c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_uncompressed_bytes); 3394c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes, 3395c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 3396842727c2SChris Kirby 33973b2aab18SMatthew Ahrens /* apply any parent delta for change in unconsumed refreservation */ 33983b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV, 33993b2aab18SMatthew Ahrens unused_refres_delta, 0, 0, tx); 3400ca45db41SChris Kirby 34013b2aab18SMatthew Ahrens /* 34023b2aab18SMatthew Ahrens * Swap deadlists. 34033b2aab18SMatthew Ahrens */ 34043b2aab18SMatthew Ahrens dsl_deadlist_close(&clone->ds_deadlist); 34053b2aab18SMatthew Ahrens dsl_deadlist_close(&origin_head->ds_deadlist); 3406c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj, 3407c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj); 34083b2aab18SMatthew Ahrens dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset, 3409c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj); 34103b2aab18SMatthew Ahrens dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset, 3411c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_deadlist_obj); 3412842727c2SChris Kirby 34133b2aab18SMatthew Ahrens dsl_scan_ds_clone_swapped(origin_head, clone, tx); 3414842727c2SChris Kirby 34153b2aab18SMatthew Ahrens spa_history_log_internal_ds(clone, "clone swap", tx, 34163b2aab18SMatthew Ahrens "parent=%s", origin_head->ds_dir->dd_myname); 3417842727c2SChris Kirby } 3418842727c2SChris Kirby 34193b2aab18SMatthew Ahrens /* 34203b2aab18SMatthew Ahrens * Given a pool name and a dataset object number in that pool, 34213b2aab18SMatthew Ahrens * return the name of that dataset. 34223b2aab18SMatthew Ahrens */ 3423a7f53a56SChris Kirby int 34243b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 3425a7f53a56SChris Kirby { 34263b2aab18SMatthew Ahrens dsl_pool_t *dp; 34273b2aab18SMatthew Ahrens dsl_dataset_t *ds; 3428a7f53a56SChris Kirby int error; 3429a7f53a56SChris Kirby 34303b2aab18SMatthew Ahrens error = dsl_pool_hold(pname, FTAG, &dp); 34313b2aab18SMatthew Ahrens if (error != 0) 34323b2aab18SMatthew Ahrens return (error); 34333b2aab18SMatthew Ahrens 34343b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 34353b2aab18SMatthew Ahrens if (error == 0) { 34363b2aab18SMatthew Ahrens dsl_dataset_name(ds, buf); 34373b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 34383b2aab18SMatthew Ahrens } 34393b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 3440a7f53a56SChris Kirby 3441a7f53a56SChris Kirby return (error); 3442a7f53a56SChris Kirby } 3443a7f53a56SChris Kirby 3444842727c2SChris Kirby int 34453b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 34463b2aab18SMatthew Ahrens uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 3447842727c2SChris Kirby { 34483b2aab18SMatthew Ahrens int error = 0; 3449842727c2SChris Kirby 34503b2aab18SMatthew Ahrens ASSERT3S(asize, >, 0); 3451842727c2SChris Kirby 34523b2aab18SMatthew Ahrens /* 34533b2aab18SMatthew Ahrens * *ref_rsrv is the portion of asize that will come from any 34543b2aab18SMatthew Ahrens * unconsumed refreservation space. 34553b2aab18SMatthew Ahrens */ 34563b2aab18SMatthew Ahrens *ref_rsrv = 0; 3457842727c2SChris Kirby 34583b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 34593b2aab18SMatthew Ahrens /* 34603b2aab18SMatthew Ahrens * Make a space adjustment for reserved bytes. 34613b2aab18SMatthew Ahrens */ 3462c1379625SJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) { 34633b2aab18SMatthew Ahrens ASSERT3U(*used, >=, 3464c1379625SJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 3465c1379625SJustin T. Gibbs *used -= 3466c1379625SJustin T. Gibbs (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 34673b2aab18SMatthew Ahrens *ref_rsrv = 34683b2aab18SMatthew Ahrens asize - MIN(asize, parent_delta(ds, asize + inflight)); 3469842727c2SChris Kirby } 3470842727c2SChris Kirby 34713b2aab18SMatthew Ahrens if (!check_quota || ds->ds_quota == 0) { 34723b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 34733b2aab18SMatthew Ahrens return (0); 3474842727c2SChris Kirby } 34753b2aab18SMatthew Ahrens /* 34763b2aab18SMatthew Ahrens * If they are requesting more space, and our current estimate 34773b2aab18SMatthew Ahrens * is over quota, they get to try again unless the actual 34783b2aab18SMatthew Ahrens * on-disk is over quota and there are no pending changes (which 34793b2aab18SMatthew Ahrens * may free up space for us). 34803b2aab18SMatthew Ahrens */ 3481c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >= 3482c1379625SJustin T. Gibbs ds->ds_quota) { 34833b2aab18SMatthew Ahrens if (inflight > 0 || 3484c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota) 3485be6fd75aSMatthew Ahrens error = SET_ERROR(ERESTART); 34863b2aab18SMatthew Ahrens else 3487be6fd75aSMatthew Ahrens error = SET_ERROR(EDQUOT); 3488842727c2SChris Kirby } 34893b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3490842727c2SChris Kirby 3491842727c2SChris Kirby return (error); 3492842727c2SChris Kirby } 3493842727c2SChris Kirby 34943b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg { 34953b2aab18SMatthew Ahrens const char *ddsqra_name; 34963b2aab18SMatthew Ahrens zprop_source_t ddsqra_source; 34973b2aab18SMatthew Ahrens uint64_t ddsqra_value; 34983b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t; 3499842727c2SChris Kirby 35003b2aab18SMatthew Ahrens 35013b2aab18SMatthew Ahrens /* ARGSUSED */ 3502842727c2SChris Kirby static int 35033b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx) 3504842727c2SChris Kirby { 35053b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 35063b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 35073b2aab18SMatthew Ahrens dsl_dataset_t *ds; 3508842727c2SChris Kirby int error; 35093b2aab18SMatthew Ahrens uint64_t newval; 3510842727c2SChris Kirby 35113b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA) 3512be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 3513842727c2SChris Kirby 35143b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 35153b2aab18SMatthew Ahrens if (error != 0) 35163b2aab18SMatthew Ahrens return (error); 35173b2aab18SMatthew Ahrens 3518bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 35193b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3520be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3521842727c2SChris Kirby } 3522842727c2SChris Kirby 35233b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 35243b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 35253b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 35263b2aab18SMatthew Ahrens if (error != 0) { 35273b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3528842727c2SChris Kirby return (error); 3529842727c2SChris Kirby } 3530842727c2SChris Kirby 35313b2aab18SMatthew Ahrens if (newval == 0) { 35323b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 35333b2aab18SMatthew Ahrens return (0); 35343b2aab18SMatthew Ahrens } 3535842727c2SChris Kirby 3536c1379625SJustin T. Gibbs if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes || 35373b2aab18SMatthew Ahrens newval < ds->ds_reserved) { 35383b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3539be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 35403b2aab18SMatthew Ahrens } 35413b2aab18SMatthew Ahrens 35423b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3543842727c2SChris Kirby return (0); 3544842727c2SChris Kirby } 3545842727c2SChris Kirby 35463b2aab18SMatthew Ahrens static void 35473b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx) 3548842727c2SChris Kirby { 35493b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 35503b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 35513b2aab18SMatthew Ahrens dsl_dataset_t *ds; 35523b2aab18SMatthew Ahrens uint64_t newval; 3553842727c2SChris Kirby 35543b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 3555842727c2SChris Kirby 35563b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, 35573b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 35583b2aab18SMatthew Ahrens ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 35593b2aab18SMatthew Ahrens &ddsqra->ddsqra_value, tx); 3560842727c2SChris Kirby 35613b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 35623b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval)); 3563842727c2SChris Kirby 35643b2aab18SMatthew Ahrens if (ds->ds_quota != newval) { 35653b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 35663b2aab18SMatthew Ahrens ds->ds_quota = newval; 3567842727c2SChris Kirby } 35683b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3569842727c2SChris Kirby } 3570842727c2SChris Kirby 35713b2aab18SMatthew Ahrens int 35723b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source, 35733b2aab18SMatthew Ahrens uint64_t refquota) 3574842727c2SChris Kirby { 35753b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 3576842727c2SChris Kirby 35773b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 35783b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 35793b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refquota; 35803b2aab18SMatthew Ahrens 35813b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check, 35827d46dc6cSMatthew Ahrens dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); 3583842727c2SChris Kirby } 3584842727c2SChris Kirby 3585842727c2SChris Kirby static int 35863b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx) 3587842727c2SChris Kirby { 35883b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 35893b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 3590842727c2SChris Kirby dsl_dataset_t *ds; 3591842727c2SChris Kirby int error; 35923b2aab18SMatthew Ahrens uint64_t newval, unique; 3593d7747cbcSChris Kirby 35943b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION) 3595be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 3596842727c2SChris Kirby 35973b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 35983b2aab18SMatthew Ahrens if (error != 0) 3599842727c2SChris Kirby return (error); 3600842727c2SChris Kirby 3601bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 36023b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3603be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3604842727c2SChris Kirby } 3605842727c2SChris Kirby 36063b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 36073b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 36083b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 36093b2aab18SMatthew Ahrens if (error != 0) { 36103b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3611842727c2SChris Kirby return (error); 3612842727c2SChris Kirby } 3613842727c2SChris Kirby 36143b2aab18SMatthew Ahrens /* 36153b2aab18SMatthew Ahrens * If we are doing the preliminary check in open context, the 36163b2aab18SMatthew Ahrens * space estimates may be inaccurate. 36173b2aab18SMatthew Ahrens */ 36183b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 36193b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 36203b2aab18SMatthew Ahrens return (0); 3621842727c2SChris Kirby } 3622842727c2SChris Kirby 36233b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 36243b2aab18SMatthew Ahrens if (!DS_UNIQUE_IS_ACCURATE(ds)) 36253b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(ds); 3626c1379625SJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes; 36273b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3628842727c2SChris Kirby 36293b2aab18SMatthew Ahrens if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) { 36303b2aab18SMatthew Ahrens uint64_t delta = MAX(unique, newval) - 36313b2aab18SMatthew Ahrens MAX(unique, ds->ds_reserved); 3632842727c2SChris Kirby 36333b2aab18SMatthew Ahrens if (delta > 36343b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) || 36353b2aab18SMatthew Ahrens (ds->ds_quota > 0 && newval > ds->ds_quota)) { 36363b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3637be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 36383b2aab18SMatthew Ahrens } 3639842727c2SChris Kirby } 3640842727c2SChris Kirby 36413b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 36423b2aab18SMatthew Ahrens return (0); 3643842727c2SChris Kirby } 3644842727c2SChris Kirby 36453b2aab18SMatthew Ahrens void 36463b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, 36473b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value, dmu_tx_t *tx) 3648ca45db41SChris Kirby { 36493b2aab18SMatthew Ahrens uint64_t newval; 36503b2aab18SMatthew Ahrens uint64_t unique; 36513b2aab18SMatthew Ahrens int64_t delta; 3652ca45db41SChris Kirby 36533b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 36543b2aab18SMatthew Ahrens source, sizeof (value), 1, &value, tx); 3655ca45db41SChris Kirby 36563b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 36573b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval)); 3658a7f53a56SChris Kirby 36593b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 36603b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 36613b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 36623b2aab18SMatthew Ahrens ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 3663c1379625SJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes; 36643b2aab18SMatthew Ahrens delta = MAX(0, (int64_t)(newval - unique)) - 36653b2aab18SMatthew Ahrens MAX(0, (int64_t)(ds->ds_reserved - unique)); 36663b2aab18SMatthew Ahrens ds->ds_reserved = newval; 36673b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3668a7f53a56SChris Kirby 36693b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 36703b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 3671ca45db41SChris Kirby } 3672ca45db41SChris Kirby 36733b2aab18SMatthew Ahrens static void 36743b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx) 3675842727c2SChris Kirby { 36763b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 36773b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 3678842727c2SChris Kirby dsl_dataset_t *ds; 3679842727c2SChris Kirby 36803b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 36813b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(ds, 36823b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx); 3683842727c2SChris Kirby dsl_dataset_rele(ds, FTAG); 3684842727c2SChris Kirby } 3685503ad85cSMatthew Ahrens 3686503ad85cSMatthew Ahrens int 36873b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source, 36883b2aab18SMatthew Ahrens uint64_t refreservation) 3689503ad85cSMatthew Ahrens { 36903b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 3691503ad85cSMatthew Ahrens 36923b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 36933b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 36943b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refreservation; 36953b2aab18SMatthew Ahrens 36963b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check, 36977d46dc6cSMatthew Ahrens dsl_dataset_set_refreservation_sync, &ddsqra, 36987d46dc6cSMatthew Ahrens 0, ZFS_SPACE_CHECK_NONE)); 3699503ad85cSMatthew Ahrens } 370019b94df9SMatthew Ahrens 370119b94df9SMatthew Ahrens /* 370219b94df9SMatthew Ahrens * Return (in *usedp) the amount of space written in new that is not 370319b94df9SMatthew Ahrens * present in oldsnap. New may be a snapshot or the head. Old must be 370419b94df9SMatthew Ahrens * a snapshot before new, in new's filesystem (or its origin). If not then 370519b94df9SMatthew Ahrens * fail and return EINVAL. 370619b94df9SMatthew Ahrens * 370719b94df9SMatthew Ahrens * The written space is calculated by considering two components: First, we 370819b94df9SMatthew Ahrens * ignore any freed space, and calculate the written as new's used space 370919b94df9SMatthew Ahrens * minus old's used space. Next, we add in the amount of space that was freed 371019b94df9SMatthew Ahrens * between the two snapshots, thus reducing new's used space relative to old's. 371119b94df9SMatthew Ahrens * Specifically, this is the space that was born before old->ds_creation_txg, 371219b94df9SMatthew Ahrens * and freed before new (ie. on new's deadlist or a previous deadlist). 371319b94df9SMatthew Ahrens * 371419b94df9SMatthew Ahrens * space freed [---------------------] 371519b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O------ 371619b94df9SMatthew Ahrens * oldsnap new 371719b94df9SMatthew Ahrens */ 371819b94df9SMatthew Ahrens int 371919b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new, 372019b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 372119b94df9SMatthew Ahrens { 372219b94df9SMatthew Ahrens int err = 0; 372319b94df9SMatthew Ahrens uint64_t snapobj; 372419b94df9SMatthew Ahrens dsl_pool_t *dp = new->ds_dir->dd_pool; 372519b94df9SMatthew Ahrens 37263b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 37273b2aab18SMatthew Ahrens 372819b94df9SMatthew Ahrens *usedp = 0; 3729c1379625SJustin T. Gibbs *usedp += dsl_dataset_phys(new)->ds_referenced_bytes; 3730c1379625SJustin T. Gibbs *usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes; 373119b94df9SMatthew Ahrens 373219b94df9SMatthew Ahrens *compp = 0; 3733c1379625SJustin T. Gibbs *compp += dsl_dataset_phys(new)->ds_compressed_bytes; 3734c1379625SJustin T. Gibbs *compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes; 373519b94df9SMatthew Ahrens 373619b94df9SMatthew Ahrens *uncompp = 0; 3737c1379625SJustin T. Gibbs *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes; 3738c1379625SJustin T. Gibbs *uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes; 373919b94df9SMatthew Ahrens 374019b94df9SMatthew Ahrens snapobj = new->ds_object; 374119b94df9SMatthew Ahrens while (snapobj != oldsnap->ds_object) { 374219b94df9SMatthew Ahrens dsl_dataset_t *snap; 374319b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 374419b94df9SMatthew Ahrens 3745ad135b5dSChristopher Siden if (snapobj == new->ds_object) { 3746ad135b5dSChristopher Siden snap = new; 3747ad135b5dSChristopher Siden } else { 3748ad135b5dSChristopher Siden err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap); 3749ad135b5dSChristopher Siden if (err != 0) 3750ad135b5dSChristopher Siden break; 3751ad135b5dSChristopher Siden } 375219b94df9SMatthew Ahrens 3753c1379625SJustin T. Gibbs if (dsl_dataset_phys(snap)->ds_prev_snap_txg == 3754c1379625SJustin T. Gibbs dsl_dataset_phys(oldsnap)->ds_creation_txg) { 375519b94df9SMatthew Ahrens /* 375619b94df9SMatthew Ahrens * The blocks in the deadlist can not be born after 375719b94df9SMatthew Ahrens * ds_prev_snap_txg, so get the whole deadlist space, 375819b94df9SMatthew Ahrens * which is more efficient (especially for old-format 375919b94df9SMatthew Ahrens * deadlists). Unfortunately the deadlist code 376019b94df9SMatthew Ahrens * doesn't have enough information to make this 376119b94df9SMatthew Ahrens * optimization itself. 376219b94df9SMatthew Ahrens */ 376319b94df9SMatthew Ahrens dsl_deadlist_space(&snap->ds_deadlist, 376419b94df9SMatthew Ahrens &used, &comp, &uncomp); 376519b94df9SMatthew Ahrens } else { 376619b94df9SMatthew Ahrens dsl_deadlist_space_range(&snap->ds_deadlist, 3767c1379625SJustin T. Gibbs 0, dsl_dataset_phys(oldsnap)->ds_creation_txg, 376819b94df9SMatthew Ahrens &used, &comp, &uncomp); 376919b94df9SMatthew Ahrens } 377019b94df9SMatthew Ahrens *usedp += used; 377119b94df9SMatthew Ahrens *compp += comp; 377219b94df9SMatthew Ahrens *uncompp += uncomp; 377319b94df9SMatthew Ahrens 377419b94df9SMatthew Ahrens /* 377519b94df9SMatthew Ahrens * If we get to the beginning of the chain of snapshots 377619b94df9SMatthew Ahrens * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap 377719b94df9SMatthew Ahrens * was not a snapshot of/before new. 377819b94df9SMatthew Ahrens */ 3779c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj; 3780ad135b5dSChristopher Siden if (snap != new) 3781ad135b5dSChristopher Siden dsl_dataset_rele(snap, FTAG); 378219b94df9SMatthew Ahrens if (snapobj == 0) { 3783be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 378419b94df9SMatthew Ahrens break; 378519b94df9SMatthew Ahrens } 378619b94df9SMatthew Ahrens 378719b94df9SMatthew Ahrens } 378819b94df9SMatthew Ahrens return (err); 378919b94df9SMatthew Ahrens } 379019b94df9SMatthew Ahrens 379119b94df9SMatthew Ahrens /* 379219b94df9SMatthew Ahrens * Return (in *usedp) the amount of space that will be reclaimed if firstsnap, 379319b94df9SMatthew Ahrens * lastsnap, and all snapshots in between are deleted. 379419b94df9SMatthew Ahrens * 379519b94df9SMatthew Ahrens * blocks that would be freed [---------------------------] 379619b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O--------O 379719b94df9SMatthew Ahrens * firstsnap lastsnap 379819b94df9SMatthew Ahrens * 379919b94df9SMatthew Ahrens * This is the set of blocks that were born after the snap before firstsnap, 380019b94df9SMatthew Ahrens * (birth > firstsnap->prev_snap_txg) and died before the snap after the 380119b94df9SMatthew Ahrens * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist). 380219b94df9SMatthew Ahrens * We calculate this by iterating over the relevant deadlists (from the snap 380319b94df9SMatthew Ahrens * after lastsnap, backward to the snap after firstsnap), summing up the 380419b94df9SMatthew Ahrens * space on the deadlist that was born after the snap before firstsnap. 380519b94df9SMatthew Ahrens */ 380619b94df9SMatthew Ahrens int 380719b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap, 380819b94df9SMatthew Ahrens dsl_dataset_t *lastsnap, 380919b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 381019b94df9SMatthew Ahrens { 381119b94df9SMatthew Ahrens int err = 0; 381219b94df9SMatthew Ahrens uint64_t snapobj; 381319b94df9SMatthew Ahrens dsl_pool_t *dp = firstsnap->ds_dir->dd_pool; 381419b94df9SMatthew Ahrens 3815bc9014e6SJustin Gibbs ASSERT(firstsnap->ds_is_snapshot); 3816bc9014e6SJustin Gibbs ASSERT(lastsnap->ds_is_snapshot); 381719b94df9SMatthew Ahrens 381819b94df9SMatthew Ahrens /* 381919b94df9SMatthew Ahrens * Check that the snapshots are in the same dsl_dir, and firstsnap 382019b94df9SMatthew Ahrens * is before lastsnap. 382119b94df9SMatthew Ahrens */ 382219b94df9SMatthew Ahrens if (firstsnap->ds_dir != lastsnap->ds_dir || 3823c1379625SJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_creation_txg > 3824c1379625SJustin T. Gibbs dsl_dataset_phys(lastsnap)->ds_creation_txg) 3825be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 382619b94df9SMatthew Ahrens 382719b94df9SMatthew Ahrens *usedp = *compp = *uncompp = 0; 382819b94df9SMatthew Ahrens 3829c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj; 383019b94df9SMatthew Ahrens while (snapobj != firstsnap->ds_object) { 383119b94df9SMatthew Ahrens dsl_dataset_t *ds; 383219b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 383319b94df9SMatthew Ahrens 383419b94df9SMatthew Ahrens err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds); 383519b94df9SMatthew Ahrens if (err != 0) 383619b94df9SMatthew Ahrens break; 383719b94df9SMatthew Ahrens 383819b94df9SMatthew Ahrens dsl_deadlist_space_range(&ds->ds_deadlist, 3839c1379625SJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX, 384019b94df9SMatthew Ahrens &used, &comp, &uncomp); 384119b94df9SMatthew Ahrens *usedp += used; 384219b94df9SMatthew Ahrens *compp += comp; 384319b94df9SMatthew Ahrens *uncompp += uncomp; 384419b94df9SMatthew Ahrens 3845c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 384619b94df9SMatthew Ahrens ASSERT3U(snapobj, !=, 0); 384719b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 384819b94df9SMatthew Ahrens } 384919b94df9SMatthew Ahrens return (err); 385019b94df9SMatthew Ahrens } 38513b2aab18SMatthew Ahrens 38523b2aab18SMatthew Ahrens /* 38533b2aab18SMatthew Ahrens * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline. 38543b2aab18SMatthew Ahrens * For example, they could both be snapshots of the same filesystem, and 38553b2aab18SMatthew Ahrens * 'earlier' is before 'later'. Or 'earlier' could be the origin of 38563b2aab18SMatthew Ahrens * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's 38573b2aab18SMatthew Ahrens * filesystem. Or 'earlier' could be the origin's origin. 385878f17100SMatthew Ahrens * 385978f17100SMatthew Ahrens * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg. 38603b2aab18SMatthew Ahrens */ 38613b2aab18SMatthew Ahrens boolean_t 386278f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier, 38639a686fbcSPaul Dagnelie uint64_t earlier_txg) 38643b2aab18SMatthew Ahrens { 38653b2aab18SMatthew Ahrens dsl_pool_t *dp = later->ds_dir->dd_pool; 38663b2aab18SMatthew Ahrens int error; 38673b2aab18SMatthew Ahrens boolean_t ret; 38683b2aab18SMatthew Ahrens 38693b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 3870bc9014e6SJustin Gibbs ASSERT(earlier->ds_is_snapshot || earlier_txg != 0); 387178f17100SMatthew Ahrens 387278f17100SMatthew Ahrens if (earlier_txg == 0) 3873c1379625SJustin T. Gibbs earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg; 38743b2aab18SMatthew Ahrens 3875bc9014e6SJustin Gibbs if (later->ds_is_snapshot && 3876c1379625SJustin T. Gibbs earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg) 38773b2aab18SMatthew Ahrens return (B_FALSE); 38783b2aab18SMatthew Ahrens 38793b2aab18SMatthew Ahrens if (later->ds_dir == earlier->ds_dir) 38803b2aab18SMatthew Ahrens return (B_TRUE); 38813b2aab18SMatthew Ahrens if (!dsl_dir_is_clone(later->ds_dir)) 38823b2aab18SMatthew Ahrens return (B_FALSE); 38833b2aab18SMatthew Ahrens 3884c1379625SJustin T. Gibbs if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object) 38853b2aab18SMatthew Ahrens return (B_TRUE); 38863b2aab18SMatthew Ahrens dsl_dataset_t *origin; 38873b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 3888c1379625SJustin T. Gibbs dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin); 38893b2aab18SMatthew Ahrens if (error != 0) 38903b2aab18SMatthew Ahrens return (B_FALSE); 389178f17100SMatthew Ahrens ret = dsl_dataset_is_before(origin, earlier, earlier_txg); 38923b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 38933b2aab18SMatthew Ahrens return (ret); 38943b2aab18SMatthew Ahrens } 38952acef22dSMatthew Ahrens 38962acef22dSMatthew Ahrens void 38972acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx) 38982acef22dSMatthew Ahrens { 38992acef22dSMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 39002acef22dSMatthew Ahrens dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx); 39012acef22dSMatthew Ahrens } 39029c3fd121SMatthew Ahrens 39039c3fd121SMatthew Ahrens boolean_t 39049c3fd121SMatthew Ahrens dsl_dataset_is_zapified(dsl_dataset_t *ds) 39059c3fd121SMatthew Ahrens { 39069c3fd121SMatthew Ahrens dmu_object_info_t doi; 39079c3fd121SMatthew Ahrens 39089c3fd121SMatthew Ahrens dmu_object_info_from_db(ds->ds_dbuf, &doi); 39099c3fd121SMatthew Ahrens return (doi.doi_type == DMU_OTN_ZAP_METADATA); 39109c3fd121SMatthew Ahrens } 39119c3fd121SMatthew Ahrens 39129c3fd121SMatthew Ahrens boolean_t 39139c3fd121SMatthew Ahrens dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds) 39149c3fd121SMatthew Ahrens { 39159c3fd121SMatthew Ahrens return (dsl_dataset_is_zapified(ds) && 39169c3fd121SMatthew Ahrens zap_contains(ds->ds_dir->dd_pool->dp_meta_objset, 39179c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0); 39189c3fd121SMatthew Ahrens } 3919