fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * Common Development and Distribution License (the "License").
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * or http://www.opensolaris.org/os/licensing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
5afc78aa05e746cd98000659cff9f89d9995b3feChris Kirby * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * Copyright (c) 2014, Joyent, Inc. All rights reserved.
03d1795fa6f720eafbee821ad37f4343c391cfe4Alexander Stetsenko * Copyright (c) 2014 RackTop Systems.
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu_objset.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dsl_dataset.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dsl_dir.h>
99653d4ee642c6528e88224f12409a5f23060994eschrock#include <sys/dsl_prop.h>
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens#include <sys/dsl_synctask.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu_traverse.h>
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski#include <sys/dmu_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/dmu_tx.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/arc.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zio.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zap.h>
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden#include <sys/zfeature.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/unique.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zfs_context.h>
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante#include <sys/zfs_ioctl.h>
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks#include <sys/spa.h>
088f389458728c464569a5506b58070254fa4f7dahrens#include <sys/zfs_znode.h>
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby#include <sys/zfs_onexit.h>
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby#include <sys/zvol.h>
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling#include <sys/dsl_scan.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <sys/dsl_deadlist.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_destroy.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_userhold.h>
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens#include <sys/dsl_bookmark.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/dmu_send.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/zio_checksum.h>
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens#include <sys/zio_compress.h>
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens#include <zfs_fletcher.h>
e19302335c33c8c6e0b0b5e426fc1f6352c84b5dbonwick
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens/*
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * The SPA supports block sizes up to 16MB. However, very large blocks
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * can have an impact on i/o latency (e.g. tying up a spinning disk for
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * ~300ms), and also potentially on the memory allocator. Therefore,
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * we do not allow the recordsize to be set larger than zfs_max_recordsize
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * (default 1MB). Larger blocks can be created by changing this tunable,
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * and pools with larger blocks can always be imported and used, regardless
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * of this setting.
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens */
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrensint zfs_max_recordsize = 1 * 1024 * 1024;
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#define SWITCH64(x, y) \
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens { \
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t __tmp = (x); \
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens (x) = (y); \
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens (y) = __tmp; \
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
55434c770c89aa1b84474f2559a106803511aba0ek#define DS_REF_MAX (1ULL << 62)
fa9e4066f08beec538e775443c5be79dd423fcabahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbsextern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonaldextern int spa_asize_inflation;
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gaponstatic zil_header_t zero_zil;
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon
a9799022bd90b13722204e80112efaa5bf573099ck/*
a9799022bd90b13722204e80112efaa5bf573099ck * Figure out how much of this delta should be propogated to the dsl_dir
a9799022bd90b13722204e80112efaa5bf573099ck * layer. If there's a refreservation, that space has already been
a9799022bd90b13722204e80112efaa5bf573099ck * partially accounted for in our ancestors.
a9799022bd90b13722204e80112efaa5bf573099ck */
a9799022bd90b13722204e80112efaa5bf573099ckstatic int64_t
a9799022bd90b13722204e80112efaa5bf573099ckparent_delta(dsl_dataset_t *ds, int64_t delta)
a9799022bd90b13722204e80112efaa5bf573099ck{
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys_t *ds_phys;
a9799022bd90b13722204e80112efaa5bf573099ck uint64_t old_bytes, new_bytes;
a9799022bd90b13722204e80112efaa5bf573099ck
a9799022bd90b13722204e80112efaa5bf573099ck if (ds->ds_reserved == 0)
a9799022bd90b13722204e80112efaa5bf573099ck return (delta);
a9799022bd90b13722204e80112efaa5bf573099ck
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ds_phys = dsl_dataset_phys(ds);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
a9799022bd90b13722204e80112efaa5bf573099ck
a9799022bd90b13722204e80112efaa5bf573099ck ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
a9799022bd90b13722204e80112efaa5bf573099ck return (new_bytes - old_bytes);
a9799022bd90b13722204e80112efaa5bf573099ck}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickdsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens int compressed = BP_GET_PSIZE(bp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens int uncompressed = BP_GET_UCSIZE(bp);
a9799022bd90b13722204e80112efaa5bf573099ck int64_t delta;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling dprintf_bp(bp, "ds=%p", ds);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(dmu_tx_is_syncing(tx));
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* It could have been compressed away to nothing */
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (BP_IS_HOLE(bp))
fa9e4066f08beec538e775443c5be79dd423fcabahrens return;
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (ds == NULL) {
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool,
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens used, compressed, uncompressed);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon ASSERT3U(bp->blk_birth, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
b62969f868a827f0823a084bc0af9c7d8b76c659Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&ds->ds_lock);
a9799022bd90b13722204e80112efaa5bf573099ck delta = parent_delta(ds, used);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes += used;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes += used;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] =
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens B_TRUE;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens spa_feature_t f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (f != SPA_FEATURE_NONE)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ds->ds_feature_activation_needed[f] = B_TRUE;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&ds->ds_lock);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens compressed, uncompressed, tx);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used - delta,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
cdb0ab79ea1af7b8fc339a04d4bf7426dc77ec4emaybeeint
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwickdsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick boolean_t async)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman int compressed = BP_GET_PSIZE(bp);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman int uncompressed = BP_GET_UCSIZE(bp);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (BP_IS_HOLE(bp))
cdb0ab79ea1af7b8fc339a04d4bf7426dc77ec4emaybee return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(dmu_tx_is_syncing(tx));
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick ASSERT(bp->blk_birth <= tx->tx_txg);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (ds == NULL) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool,
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens -used, -compressed, -uncompressed);
cdb0ab79ea1af7b8fc339a04d4bf7426dc77ec4emaybee return (used);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs ASSERT(!ds->ds_is_snapshot);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
a9799022bd90b13722204e80112efaa5bf573099ck int64_t delta;
c717a56157ae0e6fca6a1e3689ae1edc385716a3maybee
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&ds->ds_lock);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
a9799022bd90b13722204e80112efaa5bf573099ck !DS_UNIQUE_IS_ACCURATE(ds));
a9799022bd90b13722204e80112efaa5bf573099ck delta = parent_delta(ds, -used);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes -= used;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&ds->ds_lock);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
a9799022bd90b13722204e80112efaa5bf573099ck delta, -compressed, -uncompressed, tx);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, -used - delta,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens } else {
fa9e4066f08beec538e775443c5be79dd423fcabahrens dprintf_bp(bp, "putting on dead list: %s", "");
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (async) {
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick /*
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * We are here as part of zio's write done callback,
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * which means we're a zio interrupt thread. We can't
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * call dsl_deadlist_insert() now because it may block
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * waiting for I/O. Instead, put bp on the deferred
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick * queue and let dsl_pool_sync() finish the job.
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bplist_append(&ds->ds_pending_deadlist, bp);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick } else {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick }
a4611ede45b06d3d765fa70e98f9c5222d824853ahrens ASSERT3U(ds->ds_prev->ds_object, ==,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
a4611ede45b06d3d765fa70e98f9c5222d824853ahrens ds->ds_object && bp->blk_birth >
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
a4611ede45b06d3d765fa70e98f9c5222d824853ahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
a4611ede45b06d3d765fa70e98f9c5222d824853ahrens mutex_enter(&ds->ds_prev->ds_lock);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
a4611ede45b06d3d765fa70e98f9c5222d824853ahrens mutex_exit(&ds->ds_prev->ds_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&ds->ds_lock);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&ds->ds_lock);
cdb0ab79ea1af7b8fc339a04d4bf7426dc77ec4emaybee
cdb0ab79ea1af7b8fc339a04d4bf7426dc77ec4emaybee return (used);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockuint64_t
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockdsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens uint64_t trysnap = 0;
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (ds == NULL)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The snapshot creation could fail, but that would cause an
fa9e4066f08beec538e775443c5be79dd423fcabahrens * incorrect FALSE return, which would only result in an
fa9e4066f08beec538e775443c5be79dd423fcabahrens * overestimation of the amount of space that an operation would
fa9e4066f08beec538e775443c5be79dd423fcabahrens * consume, which is OK.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * There's also a small window where we could miss a pending
fa9e4066f08beec538e775443c5be79dd423fcabahrens * snapshot, because we could set the sync task in the quiescing
fa9e4066f08beec538e775443c5be79dd423fcabahrens * phase. So this should only be used as a guess.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens if (ds->ds_trysnap_txg >
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens trysnap = ds->ds_trysnap_txg;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs return (MAX(dsl_dataset_phys(ds)->ds_prev_snap_txg, trysnap));
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock}
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
3d6926289465757c3da780cea696825b0d730283Sanjeev Bagewadiboolean_t
c7cd242109c82107ec2e50013369e92be9d77702George Wilsondsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
c7cd242109c82107ec2e50013369e92be9d77702George Wilson uint64_t blk_birth)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock{
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman if (blk_birth <= dsl_dataset_prev_snap_txg(ds) ||
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman (bp != NULL && BP_IS_HOLE(bp)))
c7cd242109c82107ec2e50013369e92be9d77702George Wilson return (B_FALSE);
c7cd242109c82107ec2e50013369e92be9d77702George Wilson
837b568b3a2559f8c9b9403f95104271a85d129eGeorge Wilson ddt_prefetch(dsl_dataset_get_spa(ds), bp);
c7cd242109c82107ec2e50013369e92be9d77702George Wilson
c7cd242109c82107ec2e50013369e92be9d77702George Wilson return (B_TRUE);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek/*
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek * We have to release the fsid syncronously or we risk that a subsequent
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek * mount of the same dataset will fail to unique_insert the fsid. This
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek * failure would manifest itself as the fsid of this dataset changing
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek * between mounts which makes NFS clients quite unhappy.
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek */
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipekdsl_dataset_evict_sync(void *dbu)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs dsl_dataset_t *ds = dbu;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(ds->ds_owner == NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens unique_remove(ds->ds_fsid_guid);
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek}
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipekstatic void
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipekdsl_dataset_evict_async(void *dbu)
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek{
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek dsl_dataset_t *ds = dbu;
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek ASSERT(ds->ds_owner == NULL);
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek ds->ds_dbuf = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens if (ds->ds_objset != NULL)
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens dmu_objset_evict(ds->ds_objset);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (ds->ds_prev) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds->ds_prev, ds);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ds->ds_prev = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist);
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (ds->ds_deadlist.dl_os != NULL)
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee if (ds->ds_dir)
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs dsl_dir_async_rele(ds->ds_dir, ds);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens ASSERT(!list_link_active(&ds->ds_synced_link));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
03bad06fbb261fd4a7151a70dfeff2f5041cce1fJustin Gibbs list_destroy(&ds->ds_prop_cbs);
5ad820458efd0fdb914baff9c1447c22b819fa23nd mutex_destroy(&ds->ds_lock);
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens mutex_destroy(&ds->ds_opening_lock);
d2b3cbbd7f3a37bc7c01b526d3eb312acd070423Jorgen Lundman mutex_destroy(&ds->ds_sendstream_lock);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens refcount_destroy(&ds->ds_longholds);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_destroy(&ds->ds_bp_rwlock);
5ad820458efd0fdb914baff9c1447c22b819fa23nd
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_free(ds, sizeof (dsl_dataset_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
fa9e4066f08beec538e775443c5be79dd423fcabahrensdsl_dataset_get_snapname(dsl_dataset_t *ds)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_dataset_phys_t *headphys;
fa9e4066f08beec538e775443c5be79dd423fcabahrens int err;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_buf_t *headdbuf;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
fa9e4066f08beec538e775443c5be79dd423fcabahrens objset_t *mos = dp->dp_meta_objset;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (ds->ds_snapname[0])
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (0);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock FTAG, &headdbuf);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (err);
fa9e4066f08beec538e775443c5be79dd423fcabahrens headphys = headdbuf->db_data;
fa9e4066f08beec538e775443c5be79dd423fcabahrens err = zap_value_search(dp->dp_meta_objset,
e7437265dc2a4920c197ed4337665539d358b22cahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dmu_buf_rele(headdbuf, FTAG);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (err);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybeedsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh{
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh matchtype_t mt;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh int err;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh mt = MT_FIRST;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh else
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh mt = MT_EXACT;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1,
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh value, mt, NULL, 0, NULL);
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh if (err == ENOTSUP && mt == MT_FIRST)
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee err = zap_lookup(mos, snapobj, name, 8, 1, value);
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh return (err);
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh}
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinekdsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek boolean_t adj_cnt)
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh{
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh matchtype_t mt;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh int err;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh
71eb05381846ad14a2087631474e832d0f316654Chris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir);
71eb05381846ad14a2087631474e832d0f316654Chris Kirby
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh mt = MT_FIRST;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh else
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh mt = MT_EXACT;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee err = zap_remove_norm(mos, snapobj, name, mt, tx);
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh if (err == ENOTSUP && mt == MT_FIRST)
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee err = zap_remove(mos, snapobj, name, tx);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (err == 0 && adj_cnt)
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, -1,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh return (err);
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh}
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh
e57a022b8f718889ffa92adbde47a8f08abcdb25Justin T. Gibbsboolean_t
e57a022b8f718889ffa92adbde47a8f08abcdb25Justin T. Gibbsdsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag)
e57a022b8f718889ffa92adbde47a8f08abcdb25Justin T. Gibbs{
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs dmu_buf_t *dbuf = ds->ds_dbuf;
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs boolean_t result = B_FALSE;
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs ds->ds_object, DMU_BONUS_BLKID, tag)) {
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs if (ds == dmu_buf_get_user(dbuf))
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs result = B_TRUE;
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs else
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs dmu_buf_rele(dbuf, tag);
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs }
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs
9d47dec0481d8cd53b2c1053c96bfa3f78357d6aJustin T. Gibbs return (result);
e57a022b8f718889ffa92adbde47a8f08abcdb25Justin T. Gibbs}
e57a022b8f718889ffa92adbde47a8f08abcdb25Justin T. Gibbs
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee dsl_dataset_t **dsp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens objset_t *mos = dp->dp_meta_objset;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_buf_t *dbuf;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_dataset_t *ds;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock int err;
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby dmu_object_info_t doi;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dsl_pool_config_held(dp));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (err);
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby /* Make sure dsobj has the correct object type. */
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby dmu_object_info_from_db(dbuf, &doi);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
b287be1ba86043996f49b1cc34c80cc620f9b841Will Andrews dmu_buf_rele(dbuf, tag);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
b287be1ba86043996f49b1cc34c80cc620f9b841Will Andrews }
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby
fa9e4066f08beec538e775443c5be79dd423fcabahrens ds = dmu_buf_get_user(dbuf);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (ds == NULL) {
d5285cae913f4e01ffa0e6693a6d8ef1fbea30baGeorge Wilson dsl_dataset_t *winner = NULL;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ds->ds_dbuf = dbuf;
fa9e4066f08beec538e775443c5be79dd423fcabahrens ds->ds_object = dsobj;
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
5ad820458efd0fdb914baff9c1447c22b819fa23nd mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_init(&ds->ds_bp_rwlock, B_FALSE);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens refcount_create(&ds->ds_longholds);
5ad820458efd0fdb914baff9c1447c22b819fa23nd
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bplist_create(&ds->ds_pending_deadlist);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_open(&ds->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski offsetof(dmu_sendarg_t, dsa_link));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski
03bad06fbb261fd4a7151a70dfeff2f5041cce1fJustin Gibbs list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t),
03bad06fbb261fd4a7151a70dfeff2f5041cce1fJustin Gibbs offsetof(dsl_prop_cb_record_t, cbr_ds_node));
03bad06fbb261fd4a7151a70dfeff2f5041cce1fJustin Gibbs
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (!(spa_feature_table[f].fi_flags &
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens ZFEATURE_FLAG_PER_DATASET))
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens continue;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens err = zap_contains(mos, dsobj,
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens spa_feature_table[f].fi_guid);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (err == 0) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens ds->ds_feature_inuse[f] = B_TRUE;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens } else {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens ASSERT3U(err, ==, ENOENT);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens err = 0;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
e1f3c208abda53769a6531bb1141618555273460Justin T. Gibbs }
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens }
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens err = dsl_dir_hold_obj(dp,
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
5ad820458efd0fdb914baff9c1447c22b819fa23nd mutex_destroy(&ds->ds_lock);
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens mutex_destroy(&ds->ds_opening_lock);
d2b3cbbd7f3a37bc7c01b526d3eb312acd070423Jorgen Lundman mutex_destroy(&ds->ds_sendstream_lock);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens refcount_destroy(&ds->ds_longholds);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock kmem_free(ds, sizeof (dsl_dataset_t));
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dmu_buf_rele(dbuf, tag);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (err);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (!ds->ds_is_snapshot) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens ds->ds_snapname[0] = '\0';
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj,
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee ds, &ds->ds_prev);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens int zaperr = zap_lookup(mos, ds->ds_object,
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens DS_FIELD_BOOKMARK_NAMES,
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens sizeof (ds->ds_bookmarks), 1,
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens &ds->ds_bookmarks);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (zaperr != ENOENT)
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens VERIFY0(zaperr);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby } else {
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby err = dsl_dataset_get_snapname(ds);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (err == 0 &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby err = zap_count(
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby ds->ds_dir->dd_pool->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj,
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby &ds->ds_userrefs);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (err == 0 && !ds->ds_is_snapshot) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_prop_get_int_ds(ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &ds->ds_reserved);
cb625fb575ce78c4928c387e0d3bff0c5be9dc07ck if (err == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_prop_get_int_ds(ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA),
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &ds->ds_quota);
cb625fb575ce78c4928c387e0d3bff0c5be9dc07ck }
cb625fb575ce78c4928c387e0d3bff0c5be9dc07ck } else {
cb625fb575ce78c4928c387e0d3bff0c5be9dc07ck ds->ds_reserved = ds->ds_quota = 0;
cb625fb575ce78c4928c387e0d3bff0c5be9dc07ck }
cb625fb575ce78c4928c387e0d3bff0c5be9dc07ck
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync,
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek dsl_dataset_evict_async, &ds->ds_dbuf);
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (err == 0)
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (err != 0 || winner != NULL) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee if (ds->ds_prev)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds->ds_prev, ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(ds->ds_dir, ds);
5ad820458efd0fdb914baff9c1447c22b819fa23nd mutex_destroy(&ds->ds_lock);
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens mutex_destroy(&ds->ds_opening_lock);
d2b3cbbd7f3a37bc7c01b526d3eb312acd070423Jorgen Lundman mutex_destroy(&ds->ds_sendstream_lock);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens refcount_destroy(&ds->ds_longholds);
fa9e4066f08beec538e775443c5be79dd423fcabahrens kmem_free(ds, sizeof (dsl_dataset_t));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dmu_buf_rele(dbuf, tag);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (err);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock }
fa9e4066f08beec538e775443c5be79dd423fcabahrens ds = winner;
fa9e4066f08beec538e775443c5be79dd423fcabahrens } else {
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens ds->ds_fsid_guid =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek if (ds->ds_fsid_guid !=
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek dsl_dataset_phys(ds)->ds_fsid_guid) {
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek zfs_dbgmsg("ds_fsid_guid changed from "
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek "%llx to %llx for pool %s dataset id %llu",
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek (long long)
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek dsl_dataset_phys(ds)->ds_fsid_guid,
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek (long long)ds->ds_fsid_guid,
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek spa_name(dp->dp_spa),
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek dsobj);
40510e8eba18690b9a9843b26393725eeb0f1dacJosef 'Jeff' Sipek }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT3P(ds->ds_dbuf, ==, dbuf);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
afc6333a176879f0ea62345548438d6a5168b0c4ahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
84db2a68825c1a672f664432101f6f0b443679e3ahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock *dsp = ds;
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybeeint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_hold(dsl_pool_t *dp, const char *name,
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens void *tag, dsl_dataset_t **dsp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_dir_t *dd;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee const char *snapname;
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t obj;
fa9e4066f08beec538e775443c5be79dd423fcabahrens int err = 0;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dsl_dataset_t *ds;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock return (err);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dsl_pool_config_held(dp));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (obj != 0)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee else
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens err = SET_ERROR(ENOENT);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee /* we may be looking for a snapshot */
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee if (err == 0 && snapname != NULL) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dsl_dataset_t *snap_ds;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee if (*snapname++ != '@') {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dsl_dataset_rele(ds, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(dd, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOENT));
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee dprintf("looking for snapshot '%s'\n", snapname);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dsl_dataset_snap_lookup(ds, snapname, &obj);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee if (err == 0)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dsl_dataset_hold_obj(dp, obj, tag, &snap_ds);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dsl_dataset_rele(ds, tag);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err == 0) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_enter(&snap_ds->ds_lock);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (snap_ds->ds_snapname[0] == 0)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie (void) strlcpy(snap_ds->ds_snapname, snapname,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie sizeof (snap_ds->ds_snapname));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_exit(&snap_ds->ds_lock);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ds = snap_ds;
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err == 0)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie *dsp = ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(dd, FTAG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (err);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens void *tag, dsl_dataset_t **dsp)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(*dsp, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *dsp = NULL;
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EBUSY));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_own(dsl_pool_t *dp, const char *name,
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens void *tag, dsl_dataset_t **dsp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int err = dsl_dataset_hold(dp, name, tag, dsp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) {
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens dsl_dataset_rele(*dsp, tag);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EBUSY));
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee }
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * See the comment above dsl_pool_hold() for details. In summary, a long
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * hold is used to prevent destruction of a dataset while the pool hold
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * is dropped, allowing other concurrent operations (e.g. spa_sync()).
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The dataset and pool must be held when this function is called. After it
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * is called, the pool hold may be released while the dataset is still held
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * and accessed.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) refcount_add(&ds->ds_longholds, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) refcount_remove(&ds->ds_longholds, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/* Return B_TRUE if there are any long holds on this dataset. */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensboolean_t
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_long_held(dsl_dataset_t *ds)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (!refcount_is_zero(&ds->ds_longholds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensdsl_dataset_name(dsl_dataset_t *ds, char *name)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (ds == NULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens (void) strcpy(name, "mos");
fa9e4066f08beec538e775443c5be79dd423fcabahrens } else {
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_dir_name(ds->ds_dir, name);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds));
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (ds->ds_snapname[0]) {
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN),
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens <, ZFS_MAX_DATASET_NAME_LEN);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee /*
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee * We use a "recursive" mutex so that we
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee * can call dprintf_ds() with ds_lock held.
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee */
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (!MUTEX_HELD(&ds->ds_lock)) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&ds->ds_lock);
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens VERIFY3U(strlcat(name, ds->ds_snapname,
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens ZFS_MAX_DATASET_NAME_LEN), <,
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens ZFS_MAX_DATASET_NAME_LEN);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&ds->ds_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens } else {
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens VERIFY3U(strlcat(name, ds->ds_snapname,
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens ZFS_MAX_DATASET_NAME_LEN), <,
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens ZFS_MAX_DATASET_NAME_LEN);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrensint
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrensdsl_dataset_namelen(dsl_dataset_t *ds)
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens{
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds));
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens mutex_enter(&ds->ds_lock);
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens int len = dsl_dir_namelen(ds->ds_dir) + 1 + strlen(ds->ds_snapname);
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens mutex_exit(&ds->ds_lock);
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens return (len);
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens}
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensvoid
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybeedsl_dataset_rele(dsl_dataset_t *ds, void *tag)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_rele(ds->ds_dbuf, tag);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee}
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybeevoid
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrensdsl_dataset_disown(dsl_dataset_t *ds, void *tag)
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee{
d808a4fc6ac40e878a28e96f1ad7dd2ec439bfbfJustin T. Gibbs ASSERT3P(ds->ds_owner, ==, tag);
d808a4fc6ac40e878a28e96f1ad7dd2ec439bfbfJustin T. Gibbs ASSERT(ds->ds_dbuf != NULL);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens mutex_enter(&ds->ds_lock);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee ds->ds_owner = NULL;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens mutex_exit(&ds->ds_lock);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_long_rele(ds, tag);
d808a4fc6ac40e878a28e96f1ad7dd2ec439bfbfJustin T. Gibbs dsl_dataset_rele(ds, tag);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens}
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensboolean_t
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens{
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee boolean_t gotit = FALSE;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens mutex_enter(&ds->ds_lock);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens ds->ds_owner = tag;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_long_hold(ds, tag);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee gotit = TRUE;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens }
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens mutex_exit(&ds->ds_lock);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee return (gotit);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee}
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensboolean_t
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensdsl_dataset_has_owner(dsl_dataset_t *ds)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens{
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens boolean_t rv;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens mutex_enter(&ds->ds_lock);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens rv = (ds->ds_owner != NULL);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens mutex_exit(&ds->ds_lock);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (rv);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens}
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrensstatic void
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrensdsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens{
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens uint64_t zero = 0;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens spa_feature_incr(spa, f, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens sizeof (zero), 1, &zero, tx));
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens}
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrensvoid
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrensdsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens{
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens spa_feature_decr(spa, f, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens}
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrensuint64_t
088f389458728c464569a5506b58070254fa4f7dahrensdsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh uint64_t flags, dmu_tx_t *tx)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens dsl_pool_t *dp = dd->dd_pool;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_buf_t *dbuf;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsl_dataset_phys_t *dsphys;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens uint64_t dsobj;
fa9e4066f08beec538e775443c5be79dd423fcabahrens objset_t *mos = dp->dp_meta_objset;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
088f389458728c464569a5506b58070254fa4f7dahrens if (origin == NULL)
088f389458728c464569a5506b58070254fa4f7dahrens origin = dp->dp_origin_snap;
088f389458728c464569a5506b58070254fa4f7dahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens ASSERT(dmu_tx_is_syncing(tx));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
1649cd4b1641110b549d9f70a902cafc2007bd77tabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1649cd4b1641110b549d9f70a902cafc2007bd77tabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_buf_will_dirty(dbuf, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys = dbuf->db_data;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee bzero(dsphys, sizeof (dsl_dataset_phys_t));
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys->ds_dir_obj = dd->dd_object;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh dsphys->ds_flags = flags;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys->ds_fsid_guid = unique_create();
fa9e4066f08beec538e775443c5be79dd423fcabahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
fa9e4066f08beec538e775443c5be79dd423fcabahrens sizeof (dsphys->ds_guid));
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys->ds_snapnames_zapobj =
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh DMU_OT_NONE, 0, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys->ds_creation_time = gethrestime_sec();
088f389458728c464569a5506b58070254fa4f7dahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
a9799022bd90b13722204e80112efaa5bf573099ck
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (origin == NULL) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ohds; /* head of the origin snapshot */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens dsphys->ds_prev_snap_obj = origin->ds_object;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys->ds_prev_snap_txg =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin)->ds_creation_txg;
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden dsphys->ds_referenced_bytes =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin)->ds_referenced_bytes;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys->ds_compressed_bytes =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin)->ds_compressed_bytes;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys->ds_uncompressed_bytes =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin)->ds_uncompressed_bytes;
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&origin->ds_bp_rwlock, RW_READER, FTAG);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&origin->ds_bp_rwlock, FTAG);
42fcb65ea4f2c6f8cc5a3c6142a486cb49871fd2Matthew Ahrens
42fcb65ea4f2c6f8cc5a3c6142a486cb49871fd2Matthew Ahrens /*
42fcb65ea4f2c6f8cc5a3c6142a486cb49871fd2Matthew Ahrens * Inherit flags that describe the dataset's contents
42fcb65ea4f2c6f8cc5a3c6142a486cb49871fd2Matthew Ahrens * (INCONSISTENT) or properties (Case Insensitive).
42fcb65ea4f2c6f8cc5a3c6142a486cb49871fd2Matthew Ahrens */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
42fcb65ea4f2c6f8cc5a3c6142a486cb49871fd2Matthew Ahrens (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (origin->ds_feature_inuse[f])
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dsl_dataset_activate_feature(dsobj, f, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens dmu_buf_will_dirty(origin->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin)->ds_num_children++;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs FTAG, &ohds));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_dataset_rele(ohds, FTAG);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
088f389458728c464569a5506b58070254fa4f7dahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj =
088f389458728c464569a5506b58070254fa4f7dahrens zap_create(mos,
088f389458728c464569a5506b58070254fa4f7dahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
088f389458728c464569a5506b58070254fa4f7dahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add_int(mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsobj, tx));
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_buf_will_dirty(dd->dd_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones =
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_create(mos,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add_int(mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsobj, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dmu_buf_rele(dbuf, FTAG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_buf_will_dirty(dd->dd_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (dsobj);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens}
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *os;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os));
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon if (bcmp(&os->os_zil_header, &zero_zil, sizeof (zero_zil)) != 0) {
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon dsl_pool_t *dp = ds->ds_dir->dd_pool;
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon zio_t *zio;
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon bzero(&os->os_zil_header, sizeof (os->os_zil_header));
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon dsl_dataset_sync(ds, zio, tx);
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon VERIFY0(zio_wait(zio));
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon /* dsl_dataset_sync_done will drop this reference. */
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon dmu_buf_add_ref(ds->ds_dbuf, ds);
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon dsl_dataset_sync_done(ds, tx);
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensuint64_t
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timhdsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens{
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens dsl_pool_t *dp = pdd->dd_pool;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens uint64_t dsobj, ddobj;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens dsl_dir_t *dd;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dmu_tx_is_syncing(tx));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens ASSERT(lastname[0] != '@');
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
088f389458728c464569a5506b58070254fa4f7dahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsobj = dsl_dataset_create_sync_dd(dd, origin,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens flags & ~DS_CREATE_FLAG_NODIRTY, tx);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens dsl_deleg_set_create_perms(dd, tx, cr);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /*
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * Since we're creating a new node we know it's a leaf, so we can
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * initialize the counts if the limit feature is active.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek uint64_t cnt = 0;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek dsl_dir_zapify(dd, tx);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek sizeof (cnt), 1, &cnt, tx));
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek sizeof (cnt), 1, &cnt, tx));
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek }
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(dd, FTAG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
feaa74e41c407fe56e66a47e097c2842d4f65b9fMark Maybee /*
feaa74e41c407fe56e66a47e097c2842d4f65b9fMark Maybee * If we are creating a clone, make sure we zero out any stale
feaa74e41c407fe56e66a47e097c2842d4f65b9fMark Maybee * data from the origin snapshots zil header.
feaa74e41c407fe56e66a47e097c2842d4f65b9fMark Maybee */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
feaa74e41c407fe56e66a47e097c2842d4f65b9fMark Maybee dsl_dataset_t *ds;
feaa74e41c407fe56e66a47e097c2842d4f65b9fMark Maybee
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_zero_zil(ds, tx);
feaa74e41c407fe56e66a47e097c2842d4f65b9fMark Maybee dsl_dataset_rele(ds, FTAG);
feaa74e41c407fe56e66a47e097c2842d4f65b9fMark Maybee }
feaa74e41c407fe56e66a47e097c2842d4f65b9fMark Maybee
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens return (dsobj);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The unique space in the head dataset can be calculated by subtracting
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the space used in the most recent snapshot, that is still being used
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * in this file system, from the space currently in use. To figure out
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the space in the most recent snapshot still in use, we need to take
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the total space used in the snapshot and subtract out the space that
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * has been freed up since the snapshot was taken.
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t mrs_used;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t dlused, dlcomp, dluncomp;
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs ASSERT(!ds->ds_is_snapshot);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens else
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mrs_used = 0;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(dlused, <=, mrs_used);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens SPA_VERSION_UNIQUE_ACCURATE)
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_tx_t *tx)
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t count;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int err;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs obj, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The err should not be ENOENT, but a bug in a previous version
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * of the code could cause upgrade_clones_cb() to not set
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * ds_next_snap_obj when it should, leading to a missing entry.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If we knew that the pool was created after
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * ENOENT. However, at least we can check that we don't have
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * too many entries in the next_clones_obj even after failing to
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * remove this one.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != ENOENT)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(err);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &count));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensblkptr_t *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_get_blkptr(dsl_dataset_t *ds)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs return (&dsl_dataset_phys(ds)->ds_bp);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby}
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensspa_t *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_get_spa(dsl_dataset_t *ds)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (ds->ds_dir->dd_pool->dp_spa);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby}
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds == NULL) /* this is the meta-objset */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return;
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(ds->ds_objset != NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens panic("dirtying snapshot!");
fa9e4066f08beec538e775443c5be79dd423fcabahrens
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon /* Must not dirty a dataset in the same txg where it got snapshotted. */
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon ASSERT3U(tx->tx_txg, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
ce636f8b38e8c9ff484e880d9abb27251a882860Matthew Ahrens
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon dp = ds->ds_dir->dd_pool;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* up the hold count until we can be written out */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_add_ref(ds->ds_dbuf, ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
2e2c135528b3edfe9aaf67d1f004dc0202fa1a54Matthew Ahrensboolean_t
2e2c135528b3edfe9aaf67d1f004dc0202fa1a54Matthew Ahrensdsl_dataset_is_dirty(dsl_dataset_t *ds)
2e2c135528b3edfe9aaf67d1f004dc0202fa1a54Matthew Ahrens{
2e2c135528b3edfe9aaf67d1f004dc0202fa1a54Matthew Ahrens for (int t = 0; t < TXG_SIZE; t++) {
2e2c135528b3edfe9aaf67d1f004dc0202fa1a54Matthew Ahrens if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
2e2c135528b3edfe9aaf67d1f004dc0202fa1a54Matthew Ahrens ds, t))
2e2c135528b3edfe9aaf67d1f004dc0202fa1a54Matthew Ahrens return (B_TRUE);
2e2c135528b3edfe9aaf67d1f004dc0202fa1a54Matthew Ahrens }
2e2c135528b3edfe9aaf67d1f004dc0202fa1a54Matthew Ahrens return (B_FALSE);
2e2c135528b3edfe9aaf67d1f004dc0202fa1a54Matthew Ahrens}
2e2c135528b3edfe9aaf67d1f004dc0202fa1a54Matthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t asize;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!dmu_tx_is_syncing(tx))
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e19302335c33c8c6e0b0b5e426fc1f6352c84b5dbonwick /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If there's an fs-only reservation, any blocks that might become
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * owned by the snapshot dataset must be accommodated by space
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * outside of the reservation.
e19302335c33c8c6e0b0b5e426fc1f6352c84b5dbonwick */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOSPC));
e19302335c33c8c6e0b0b5e426fc1f6352c84b5dbonwick
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Propagate any reserved space for this snapshot to other
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * snapshot checks in this sync group.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (asize > 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_willuse_space(ds->ds_dir, asize, tx);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
e19302335c33c8c6e0b0b5e426fc1f6352c84b5dbonwick return (0);
e19302335c33c8c6e0b0b5e426fc1f6352c84b5dbonwick}
e19302335c33c8c6e0b0b5e426fc1f6352c84b5dbonwick
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenstypedef struct dsl_dataset_snapshot_arg {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *ddsa_snaps;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *ddsa_props;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *ddsa_errors;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek cred_t *ddsa_cr;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens} dsl_dataset_snapshot_arg_t;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t value;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_trysnap_txg = tx->tx_txg;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!dmu_tx_is_syncing(tx))
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * We don't allow multiple snapshots of the same txg. If there
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * is already one, try again.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EAGAIN));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Check for conflicting snapshot name.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_snap_lookup(ds, snapname, &value);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EEXIST));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != ENOENT)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski /*
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski * We don't allow taking snapshots of inconsistent datasets, such as
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski * those into which we are currently receiving. However, if we are
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski * creating this snapshot as part of a receive, this check will be
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski * executed atomically with respect to the completion of the receive
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski * case we ignore this, knowing it will be fixed up for us shortly in
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski * dmu_recv_end_sync().
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski */
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski if (!recv && DS_IS_INCONSISTENT(ds))
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski return (SET_ERROR(EBUSY));
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /*
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * Skip the check for temporary snapshots or if we have already checked
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * the counts in dsl_dataset_snapshot_check. This means we really only
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * check the count here when we're receiving a stream.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (cnt != 0 && cr != NULL) {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (error != 0)
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek return (error);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek }
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_snapshot_reserve_space(ds, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens return (0);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens}
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_t *pair;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int rv = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /*
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * Pre-compute how many total new snapshots will be created for each
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * level in the tree and below. This is needed for validating the
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * snapshot limit when either taking a recursive snapshot or when
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * taking multiple snapshots.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek *
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * The problem is that the counts are not actually adjusted when
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * we are checking, only when we finally sync. For a single snapshot,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * this is easy, the count will increase by 1 at each node up the tree,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * but its more complicated for the recursive/multiple snapshot case.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek *
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * The dsl_fs_ss_limit_check function does recursively check the count
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * at each level up the tree but since it is validating each snapshot
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * independently we need to be sure that we are validating the complete
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * count for the entire set of snapshots. We do this by rolling up the
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * counts for each component of the name into an nvlist and then
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * checking each of those cases with the aggregated count.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek *
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * This approach properly handles not only the recursive snapshot
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * case (where we get all of those on the ddsa_snaps list) but also
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * the sibling case (e.g. snapshot a/b and a/c so that we will also
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * validate the limit on 'a' using a count of 2).
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek *
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * We validate the snapshot names in the third loop and only report
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * name errors once.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (dmu_tx_is_syncing(tx)) {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek nvlist_t *cnt_track = NULL;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek cnt_track = fnvlist_alloc();
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /* Rollup aggregated counts into the cnt_track list */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek pair != NULL;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek char *pdelim;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek uint64_t val;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek char nm[MAXPATHLEN];
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek (void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek pdelim = strchr(nm, '@');
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (pdelim == NULL)
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek continue;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek *pdelim = '\0';
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek do {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (nvlist_lookup_uint64(cnt_track, nm,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek &val) == 0) {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /* update existing entry */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek fnvlist_add_uint64(cnt_track, nm,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek val + 1);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek } else {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /* add to list */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek fnvlist_add_uint64(cnt_track, nm, 1);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek }
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek pdelim = strrchr(nm, '/');
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (pdelim != NULL)
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek *pdelim = '\0';
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek } while (pdelim != NULL);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek }
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /* Check aggregated counts at each level */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek for (pair = nvlist_next_nvpair(cnt_track, NULL);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek int error = 0;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek char *name;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek uint64_t cnt = 0;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek dsl_dataset_t *ds;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek name = nvpair_name(pair);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek cnt = fnvpair_value_uint64(pair);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek ASSERT(cnt > 0);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek error = dsl_dataset_hold(dp, name, FTAG, &ds);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (error == 0) {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek ddsa->ddsa_cr);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek dsl_dataset_rele(ds, FTAG);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek }
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (error != 0) {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (ddsa->ddsa_errors != NULL)
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek fnvlist_add_int32(ddsa->ddsa_errors,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek name, error);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek rv = error;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /* only report one error for this check */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek break;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek }
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek }
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek nvlist_free(cnt_track);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek }
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens char *name, *atp;
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN];
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens name = nvpair_name(pair);
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens error = SET_ERROR(ENAMETOOLONG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens atp = strchr(name, '@');
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (atp == NULL)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens error = SET_ERROR(EINVAL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) strlcpy(dsname, name, atp - name + 1);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /* passing 0/NULL skips dsl_fs_ss_limit_check */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_snapshot_check_impl(ds,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek atp + 1, tx, B_FALSE, 0, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ddsa->ddsa_errors != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_int32(ddsa->ddsa_errors,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens name, error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens rv = error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (rv);
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee}
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_tx_t *tx)
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_t *dbuf;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_phys_t *dsphys;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t dsobj, crtxg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos = dp->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *os;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
c33e334fd3eb2b3d91c4b9667d7a465b6924e8d3Matthew Ahrens
c33e334fd3eb2b3d91c4b9667d7a465b6924e8d3Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If we are on an old pool, the zil must not be active, in which
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * case it will be zeroed. Usually zil_suspend() accomplishes this.
c33e334fd3eb2b3d91c4b9667d7a465b6924e8d3Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_objset_from_ds(ds, &os) != 0 ||
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens bcmp(&os->os_phys->os_zil_header, &zero_zil,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens sizeof (zero_zil)) == 0);
c33e334fd3eb2b3d91c4b9667d7a465b6924e8d3Matthew Ahrens
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon /* Should not snapshot a dirty dataset. */
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon ASSERT(!txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon ds, tx->tx_txg));
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The origin's ds_creation_txg has to be < TXG_INITIAL
088f389458728c464569a5506b58070254fa4f7dahrens */
088f389458728c464569a5506b58070254fa4f7dahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
088f389458728c464569a5506b58070254fa4f7dahrens crtxg = 1;
088f389458728c464569a5506b58070254fa4f7dahrens else
088f389458728c464569a5506b58070254fa4f7dahrens crtxg = tx->tx_txg;
088f389458728c464569a5506b58070254fa4f7dahrens
1649cd4b1641110b549d9f70a902cafc2007bd77tabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1649cd4b1641110b549d9f70a902cafc2007bd77tabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_buf_will_dirty(dbuf, tx);
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys = dbuf->db_data;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee bzero(dsphys, sizeof (dsl_dataset_phys_t));
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys->ds_fsid_guid = unique_create();
fa9e4066f08beec538e775443c5be79dd423fcabahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
fa9e4066f08beec538e775443c5be79dd423fcabahrens sizeof (dsphys->ds_guid));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys->ds_next_snap_obj = ds->ds_object;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys->ds_num_children = 1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens dsphys->ds_creation_time = gethrestime_sec();
088f389458728c464569a5506b58070254fa4f7dahrens dsphys->ds_creation_txg = crtxg;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsphys->ds_uncompressed_bytes =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock dmu_buf_rele(dbuf, FTAG);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (ds->ds_feature_inuse[f])
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dsl_dataset_activate_feature(dsobj, f, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(ds->ds_prev != 0, ==,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens if (ds->ds_prev) {
088f389458728c464569a5506b58070254fa4f7dahrens uint64_t next_clones_obj =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
fa9e4066f08beec538e775443c5be79dd423fcabahrens ds->ds_object ||
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ds->ds_object) {
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
088f389458728c464569a5506b58070254fa4f7dahrens } else if (next_clones_obj != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_remove_from_next_clones(ds->ds_prev,
c33e334fd3eb2b3d91c4b9667d7a465b6924e8d3Matthew Ahrens dsphys->ds_next_snap_obj, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add_int(mos,
088f389458728c464569a5506b58070254fa4f7dahrens next_clones_obj, dsobj, tx));
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
a9799022bd90b13722204e80112efaa5bf573099ck /*
a9799022bd90b13722204e80112efaa5bf573099ck * If we have a reference-reservation on this dataset, we will
a9799022bd90b13722204e80112efaa5bf573099ck * need to increase the amount of refreservation being charged
a9799022bd90b13722204e80112efaa5bf573099ck * since our unique space is going to zero.
a9799022bd90b13722204e80112efaa5bf573099ck */
a9799022bd90b13722204e80112efaa5bf573099ck if (ds->ds_reserved) {
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling int64_t delta;
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ds->ds_reserved);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling delta, 0, 0, tx);
a9799022bd90b13722204e80112efaa5bf573099ck }
a9799022bd90b13722204e80112efaa5bf573099ck
fa9e4066f08beec538e775443c5be79dd423fcabahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_deadlist_open(&ds->ds_deadlist, mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_add_key(&ds->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes = 0;
a9799022bd90b13722204e80112efaa5bf573099ck if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snapname, 8, 1, &dsobj, tx));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (ds->ds_prev)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds->ds_prev, ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling dsl_scan_ds_snapshotted(ds, tx);
088f389458728c464569a5506b58070254fa4f7dahrens
71eb05381846ad14a2087631474e832d0f316654Chris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir);
71eb05381846ad14a2087631474e832d0f316654Chris Kirby
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_t *pair;
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens char *name, *atp;
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char dsname[ZFS_MAX_DATASET_NAME_LEN];
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens name = nvpair_name(pair);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens atp = strchr(name, '@');
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) strlcpy(dsname, name, atp - name + 1);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ddsa->ddsa_props != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_props_set_sync_impl(ds->ds_prev,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The snapshots must all be in the same pool.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * All-or-nothing: if there are any failures, nothing will be modified.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_arg_t ddsa;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_t *pair;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens boolean_t needsuspend;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_t *spa;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens char *firstname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *suspended = NULL;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair = nvlist_next_nvpair(snaps, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (pair == NULL)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens firstname = nvpair_name(pair);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = spa_open(firstname, &spa, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_close(spa, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (needsuspend) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens suspended = fnvlist_alloc();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) {
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN];
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens char *snapname = nvpair_name(pair);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens char *atp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens void *cookie;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens atp = strchr(snapname, '@');
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (atp == NULL) {
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens error = SET_ERROR(EINVAL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens break;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) strlcpy(fsname, snapname, atp - snapname + 1);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = zil_suspend(fsname, &cookie);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens break;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_uint64(suspended, fsname,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (uintptr_t)cookie);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsa.ddsa_snaps = snaps;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsa.ddsa_props = props;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsa.ddsa_errors = errors;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek ddsa.ddsa_cr = CRED();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_sync, &ddsa,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (suspended != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pair = nvlist_next_nvpair(suspended, pair)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zil_resume((void *)(uintptr_t)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvpair_value_uint64(pair));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_free(suspended);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenstypedef struct dsl_dataset_snapshot_tmp_arg {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *ddsta_fsname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *ddsta_snapname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens minor_t ddsta_cleanup_minor;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *ddsta_htag;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens} dsl_dataset_snapshot_tmp_arg_t;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /* NULL cred means no limit check for tmp snapshot */
ca48f36f20f6098ceb19d5b084b6b3d4b8eca9faKeith M Wesolowski error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek tx, B_FALSE, 0, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOTSUP));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens B_TRUE, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens minor_t cleanup_minor, const char *htag)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_tmp_arg_t ddsta;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_t *spa;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens boolean_t needsuspend;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens void *cookie;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsta.ddsta_fsname = fsname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsta.ddsta_snapname = snapname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsta.ddsta_cleanup_minor = cleanup_minor;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsta.ddsta_htag = htag;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = spa_open(fsname, &spa, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_close(spa, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (needsuspend) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = zil_suspend(fsname, &cookie);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (needsuspend)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zil_resume(cookie);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dmu_tx_is_syncing(tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(ds->ds_objset != NULL);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * in case we had to change ds_fsid_guid when we opened it,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * sync it out now.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_objset_sync(ds->ds_objset, zio, tx);
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (ds->ds_feature_activation_needed[f]) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (ds->ds_feature_inuse[f])
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens continue;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dsl_dataset_activate_feature(ds->ds_object, f, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens ds->ds_feature_inuse[f] = B_TRUE;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gaponstatic int
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapondeadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon{
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon dsl_deadlist_t *dl = arg;
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon dsl_deadlist_insert(dl, bp, tx);
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon return (0);
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon}
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gaponvoid
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapondsl_dataset_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx)
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon{
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon objset_t *os = ds->ds_objset;
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon bplist_iterate(&ds->ds_pending_deadlist,
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon deadlist_enqueue_cb, &ds->ds_deadlist, tx);
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon ASSERT(!dmu_objset_is_dirty(os, dmu_tx_get_txg(tx)));
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon dmu_buf_rele(ds->ds_dbuf, ds);
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon}
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensget_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t count = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_t zc;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_attribute_t za;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *propval = fnvlist_alloc();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *val = fnvlist_alloc();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * There may be missing entries in ds_next_clones_obj
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * due to a bug in a previous version of the code.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Only trust it if it has the right number of entries.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens &count));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (count != dsl_dataset_phys(ds)->ds_num_children - 1)
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens goto fail;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs for (zap_cursor_init(&zc, mos,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens zap_cursor_advance(&zc)) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_dataset_t *clone;
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN];
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens za.za_first_integer, FTAG, &clone));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_dir_name(clone->ds_dir, buf);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_boolean(val, buf);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_dataset_rele(clone, FTAG);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens zap_cursor_fini(&zc);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrensfail:
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens nvlist_free(val);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens nvlist_free(propval);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens}
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensstatic void
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensget_receive_resume_stats(dsl_dataset_t *ds, nvlist_t *nv)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens{
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (dsl_dataset_has_resume_receive_state(ds)) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens char *str;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens void *packed;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint8_t *compressed;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t val;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens nvlist_t *token_nv = fnvlist_alloc();
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens size_t packed_size, compressed_size;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_add_uint64(token_nv, "fromguid", val);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_add_uint64(token_nv, "object", val);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_add_uint64(token_nv, "offset", val);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_add_uint64(token_nv, "bytes", val);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_add_uint64(token_nv, "toguid", val);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens char buf[256];
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_add_string(token_nv, "toname", buf);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (zap_contains(dp->dp_meta_objset, ds->ds_object,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel DS_FIELD_RESUME_LARGEBLOCK) == 0) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel fnvlist_add_boolean(token_nv, "largeblockok");
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (zap_contains(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_EMBEDOK) == 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_add_boolean(token_nv, "embedok");
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (zap_contains(dp->dp_meta_objset, ds->ds_object,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel DS_FIELD_RESUME_COMPRESSOK) == 0) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel fnvlist_add_boolean(token_nv, "compressok");
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens packed = fnvlist_pack(token_nv, &packed_size);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_free(token_nv);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens compressed = kmem_alloc(packed_size, KM_SLEEP);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens compressed_size = gzip_compress(packed, compressed,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens packed_size, packed_size, 6);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens zio_cksum_t cksum;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fletcher_4_native(compressed, compressed_size, NULL, &cksum);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens str = kmem_alloc(compressed_size * 2 + 1, KM_SLEEP);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens for (int i = 0; i < compressed_size; i++) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) sprintf(str + i * 2, "%02x", compressed[i]);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens str[compressed_size * 2] = '\0';
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens char *propval = kmem_asprintf("%u-%llx-%llx-%s",
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ZFS_SEND_RESUME_TOKEN_VERSION,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (longlong_t)cksum.zc_word[0],
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (longlong_t)packed_size, str);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_prop_nvlist_add_string(nv,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ZFS_PROP_RECEIVE_RESUME_TOKEN, propval);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens kmem_free(packed, packed_size);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens kmem_free(str, compressed_size * 2 + 1);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens kmem_free(compressed, packed_size);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens strfree(propval);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens}
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrensdsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
187d6ac08adc31ea6868bde0cfbbb288826254e8Matt Ahrens uint64_t refd, avail, uobjs, aobjs, ratio;
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dsl_pool_config_held(dp));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
77372cb0f35e8d3615ca2e16044f033397e88e21Matthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (ds->ds_is_snapshot) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens get_clones_stat(ds, nv);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens } else {
b461c7460e5e77cf65f00151162e654220c6e2fbMatthew Ahrens if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN];
b461c7460e5e77cf65f00151162e654220c6e2fbMatthew Ahrens dsl_dataset_name(ds->ds_prev, buf);
b461c7460e5e77cf65f00151162e654220c6e2fbMatthew Ahrens dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf);
b461c7460e5e77cf65f00151162e654220c6e2fbMatthew Ahrens }
b461c7460e5e77cf65f00151162e654220c6e2fbMatthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens dsl_dir_stats(ds->ds_dir, nv);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
a9799022bd90b13722204e80112efaa5bf573099ck dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
a9799022bd90b13722204e80112efaa5bf573099ck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
a9799022bd90b13722204e80112efaa5bf573099ck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
a9799022bd90b13722204e80112efaa5bf573099ck
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_time);
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_txg);
a9799022bd90b13722204e80112efaa5bf573099ck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
a9799022bd90b13722204e80112efaa5bf573099ck ds->ds_quota);
a9799022bd90b13722204e80112efaa5bf573099ck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
a9799022bd90b13722204e80112efaa5bf573099ck ds->ds_reserved);
c5904d138f3bdf0762dbf452a43d5a5c387ea6a8eschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_guid);
1d7132005da8d75994a6ad204e6ec05ef5ffaa4bEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes);
1d7132005da8d75994a6ad204e6ec05ef5ffaa4bEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
1d7132005da8d75994a6ad204e6ec05ef5ffaa4bEric Schrock ds->ds_object);
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson ds->ds_userrefs);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens uint64_t written, comp, uncomp;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_dataset_t *prev;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens int err = dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (err == 0) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens err = dsl_dataset_space_written(prev, ds, &written,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens &comp, &uncomp);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_dataset_rele(prev, FTAG);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (err == 0) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens written);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (!dsl_dataset_is_snapshot(ds)) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * A failed "newfs" (e.g. full) resumable receive leaves
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * the stats set on this dataset. Check here for the prop.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens get_receive_resume_stats(ds, nv);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * A failed incremental resumable receive leaves the
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * stats set on our child named "%recv". Check the child
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * for the prop.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens /* 6 extra bytes for /%recv */
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_t *recv_ds;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_name(ds, recvname);
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens if (strlcat(recvname, "/", sizeof (recvname)) <
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens sizeof (recvname) &&
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens strlcat(recvname, recv_clone_name, sizeof (recvname)) <
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens sizeof (recvname) &&
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens dsl_dataset_hold(dp, recvname, FTAG, &recv_ds) == 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens get_receive_resume_stats(recv_ds, nv);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_rele(recv_ds, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrensvoid
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrensdsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dsl_pool_config_held(dp));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs stat->dds_inconsistent =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs stat->dds_guid = dsl_dataset_phys(ds)->ds_guid;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens stat->dds_origin[0] = '\0';
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (ds->ds_is_snapshot) {
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens stat->dds_is_snapshot = B_TRUE;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs stat->dds_num_clones =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_num_children - 1;
ebedde844eddf46049b2403f974b50bcba47356eEric Taylor } else {
ebedde844eddf46049b2403f974b50bcba47356eEric Taylor stat->dds_is_snapshot = B_FALSE;
ebedde844eddf46049b2403f974b50bcba47356eEric Taylor stat->dds_num_clones = 0;
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (dsl_dir_is_clone(ds->ds_dir)) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens dsl_dataset_t *ods;
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_origin_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs FTAG, &ods));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens dsl_dataset_name(ods, stat->dds_origin);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ods, FTAG);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens }
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens}
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrensuint64_t
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrensdsl_dataset_fsid_guid(dsl_dataset_t *ds)
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens{
91ebeef555ce7f899b6270a3c2df47b51f7ad59aahrens return (ds->ds_fsid_guid);
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens}
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrensvoid
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrensdsl_dataset_space(dsl_dataset_t *ds,
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens uint64_t *refdbytesp, uint64_t *availbytesp,
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens uint64_t *usedobjsp, uint64_t *availobjsp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs *availbytesp +=
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
a9799022bd90b13722204e80112efaa5bf573099ck if (ds->ds_quota != 0) {
a9799022bd90b13722204e80112efaa5bf573099ck /*
a9799022bd90b13722204e80112efaa5bf573099ck * Adjust available bytes according to refquota
a9799022bd90b13722204e80112efaa5bf573099ck */
a9799022bd90b13722204e80112efaa5bf573099ck if (*refdbytesp < ds->ds_quota)
a9799022bd90b13722204e80112efaa5bf573099ck *availbytesp = MIN(*availbytesp,
a9799022bd90b13722204e80112efaa5bf573099ck ds->ds_quota - *refdbytesp);
a9799022bd90b13722204e80112efaa5bf573099ck else
a9799022bd90b13722204e80112efaa5bf573099ck *availbytesp = 0;
a9799022bd90b13722204e80112efaa5bf573099ck }
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG);
a2eea2e101e6a163a537dcc6d4e3c4da2a0ea5b2ahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp;
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f18faf3f3e5def85fdfff681617d227703ace2adekboolean_t
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrensdsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
f18faf3f3e5def85fdfff681617d227703ace2adek{
f18faf3f3e5def85fdfff681617d227703ace2adek dsl_pool_t *dp = ds->ds_dir->dd_pool;
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie uint64_t birth;
f18faf3f3e5def85fdfff681617d227703ace2adek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dsl_pool_config_held(dp));
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (snap == NULL)
f18faf3f3e5def85fdfff681617d227703ace2adek return (B_FALSE);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie birth = dsl_dataset_get_blkptr(ds)->blk_birth;
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie if (birth > dsl_dataset_phys(snap)->ds_creation_txg) {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens objset_t *os, *os_snap;
6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1Matthew Ahrens /*
6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1Matthew Ahrens * It may be that only the ZIL differs, because it was
6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1Matthew Ahrens * reset in the head. Don't count that as being
6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1Matthew Ahrens * modified.
6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1Matthew Ahrens */
6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1Matthew Ahrens if (dmu_objset_from_ds(ds, &os) != 0)
6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1Matthew Ahrens return (B_TRUE);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (dmu_objset_from_ds(snap, &os_snap) != 0)
6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1Matthew Ahrens return (B_TRUE);
6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1Matthew Ahrens return (bcmp(&os->os_phys->os_meta_dnode,
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens &os_snap->os_phys->os_meta_dnode,
6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1Matthew Ahrens sizeof (os->os_phys->os_meta_dnode)) != 0);
6e0cbcaa0c6f2bc34634a4cc17b099f9ecef03d1Matthew Ahrens }
f18faf3f3e5def85fdfff681617d227703ace2adek return (B_FALSE);
f18faf3f3e5def85fdfff681617d227703ace2adek}
f18faf3f3e5def85fdfff681617d227703ace2adek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenstypedef struct dsl_dataset_rename_snapshot_arg {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *ddrsa_fsname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *ddrsa_oldsnapname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *ddrsa_newsnapname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens boolean_t ddrsa_recursive;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_tx_t *ddrsa_tx;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens} dsl_dataset_rename_snapshot_arg_t;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens/* ARGSUSED */
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *hds, void *arg)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
fa9e4066f08beec538e775443c5be79dd423fcabahrens uint64_t val;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* ignore nonexistent snapshots */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error == ENOENT ? 0 : error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* new name should not exist */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens error = SET_ERROR(EEXIST);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens else if (error == ENOENT)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = 0;
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (dsl_dir_namelen(hds->ds_dir) + 1 +
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens error = SET_ERROR(ENAMETOOLONG);
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens dsl_dataset_t *hds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ddrsa->ddrsa_recursive) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rename_snapshot_check_impl, ddrsa,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens DS_FIND_CHILDREN);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee dsl_dataset_rele(hds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusantestatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *hds, void *arg)
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t val;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_tx_t *tx = ddrsa->ddrsa_tx;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(error == 0 || error == ENOENT);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == ENOENT) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* ignore nonexistent snapshots */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks }
ecd6cf800b63704be73fb264c3f5b6e0dafc068dmarks
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* log before we change the name */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(ds, "rename", tx,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens "-> @%s", ddrsa->ddrsa_newsnapname);
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek B_FALSE));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mutex_enter(&ds->ds_lock);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mutex_exit(&ds->ds_lock);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs VERIFY0(zap_add(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx));
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante return (0);
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante}
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *hds;
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddrsa->ddrsa_tx = tx;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ddrsa->ddrsa_recursive) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rename_snapshot_sync_impl, ddrsa,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens DS_FIND_CHILDREN));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(hds, FTAG);
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante}
cdf5b4ca0fa5ca7622b06bcb271be9e8a8245fecmmusante
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_rename_snapshot(const char *fsname,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *oldsnapname, const char *newsnapname, boolean_t recursive)
3a5a36bed7d37f89dd29cedbff57558e30629f6emmusante{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rename_snapshot_arg_t ddrsa;
3a5a36bed7d37f89dd29cedbff57558e30629f6emmusante
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddrsa.ddrsa_fsname = fsname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddrsa.ddrsa_oldsnapname = oldsnapname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddrsa.ddrsa_newsnapname = newsnapname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddrsa.ddrsa_recursive = recursive;
3a5a36bed7d37f89dd29cedbff57558e30629f6emmusante
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens dsl_dataset_rename_snapshot_sync, &ddrsa,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED));
3a5a36bed7d37f89dd29cedbff57558e30629f6emmusante}
3a5a36bed7d37f89dd29cedbff57558e30629f6emmusante
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski/*
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski * If we're doing an ownership handoff, we need to make sure that there is
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski * only one long hold on the dataset. We're not allowed to change anything here
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski * so we don't permanently release the long hold or regular hold here. We want
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski * to do this only when syncing to avoid the dataset unexpectedly going away
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski * when we release the long hold.
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski */
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowskistatic int
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowskidsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski{
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski boolean_t held;
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski if (!dmu_tx_is_syncing(tx))
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski return (0);
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski if (owner != NULL) {
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, owner);
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski dsl_dataset_long_rele(ds, owner);
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski }
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski held = dsl_dataset_long_held(ds);
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski if (owner != NULL)
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski dsl_dataset_long_hold(ds, owner);
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski if (held)
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski return (SET_ERROR(EBUSY));
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski return (0);
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski}
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowskitypedef struct dsl_dataset_rollback_arg {
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski const char *ddra_fsname;
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski void *ddra_owner;
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens nvlist_t *ddra_result;
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski} dsl_dataset_rollback_arg_t;
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int64_t unused_refres_delta;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
370c1af0b499c5bb606379abee9b560d4554f217Sanjeev Bagewadi
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* must not be a snapshot */
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (ds->ds_is_snapshot) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3a5a36bed7d37f89dd29cedbff57558e30629f6emmusante
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* must have a most recent snapshot */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3a5a36bed7d37f89dd29cedbff57558e30629f6emmusante
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon /*
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon * No rollback to a snapshot created in the current txg, because
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon * the rollback may dirty the dataset and create blocks that are
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon * not reachable from the rootbp while having a birth txg that
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon * falls into the snapshot's range.
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon */
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon if (dmu_tx_is_syncing(tx) &&
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) {
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon dsl_dataset_rele(ds, FTAG);
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon return (SET_ERROR(EAGAIN));
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon }
bfaed0b91e57062c38bc16b4f89db3c8f0052a9bAndriy Gapon
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens /* must not have any bookmarks after the most recent snapshot */
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens nvlist_t *proprequest = fnvlist_alloc();
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens nvlist_t *bookmarks = fnvlist_alloc();
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens fnvlist_free(proprequest);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (error != 0)
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (error);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens nvlist_t *valuenv =
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens zfs_prop_to_name(ZFS_PROP_CREATETXG));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens fnvlist_free(bookmarks);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens dsl_dataset_rele(ds, FTAG);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (SET_ERROR(EEXIST));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens }
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens }
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens fnvlist_free(bookmarks);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski if (error != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Check if the snap we are rolling back to uses more than
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the refquota.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds->ds_quota != 0 &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EDQUOT));
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
370c1af0b499c5bb606379abee9b560d4554f217Sanjeev Bagewadi
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * When we do the clone swap, we will temporarily use more space
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * due to the refreservation (the head will no longer have any
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * unique space, so the entire amount of the refreservation will need
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * to be free). We will immediately destroy the clone, freeing
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * this space, but the freeing happens over many txg's.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (unused_refres_delta > 0 &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens unused_refres_delta >
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOSPC));
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds, *clone;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t cloneobj;
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens dsl_dataset_name(ds->ds_prev, namebuf);
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens fnvlist_add_string(ddra->ddra_result, "target", namebuf);
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_zero_zil(ds, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_head_sync_impl(clone, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(clone, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski/*
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens * Rolls back the given filesystem or volume to the most recent snapshot.
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens * The name of the most recent snapshot will be returned under key "target"
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens * in the result nvlist.
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski *
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens * If owner != NULL:
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski * - The existing dataset MUST be owned by the specified owner at entry
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski * - Upon return, dataset will still be held by the same owner, whether we
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski * succeed or not.
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski *
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski * This mode is required any time the existing filesystem is mounted. See
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski * notes above zfs_suspend_fs() for further details.
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrensdsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski dsl_dataset_rollback_arg_t ddra;
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski ddra.ddra_fsname = fsname;
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski ddra.ddra_owner = owner;
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens ddra.ddra_result = result;
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens dsl_dataset_rollback_sync, &ddra,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED));
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
99653d4ee642c6528e88224f12409a5f23060994eschrock
088f389458728c464569a5506b58070254fa4f7dahrensstruct promotenode {
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee list_node_t link;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee dsl_dataset_t *ds;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee};
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenstypedef struct dsl_dataset_promote_arg {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *ddpa_clonename;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ddpa_clone;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens list_t shared_snaps, origin_snaps, clone_snaps;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *origin_origin; /* origin of the origin */
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
681d9761e8516a7dc5ab6589e2dfe717777e1123Eric Taylor char *err_ds;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek cred_t *cr;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens} dsl_dataset_promote_arg_t;
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrensstatic int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens void *tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
99653d4ee642c6528e88224f12409a5f23060994eschrockstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
99653d4ee642c6528e88224f12409a5f23060994eschrock{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *hds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct promotenode *snap;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *origin_ds;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee int err;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens uint64_t unused;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek uint64_t ss_mv_cnt;
cb5842f8b0caaad0ed53535bd77042e933fdbafeAndriy Gapon size_t max_snap_len;
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = promote_hold(ddpa, dp, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
99653d4ee642c6528e88224f12409a5f23060994eschrock
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens hds = ddpa->ddpa_clone;
cb5842f8b0caaad0ed53535bd77042e933fdbafeAndriy Gapon max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens promote_rele(ddpa, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EXDEV));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Compute and check the amount of space to transfer. Since this is
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * so expensive, don't do the preliminary check.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!dmu_tx_is_syncing(tx)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens promote_rele(ddpa, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap = list_head(&ddpa->shared_snaps);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens origin_ds = snap->ds;
99653d4ee642c6528e88224f12409a5f23060994eschrock
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* compute origin's new unique space */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap = list_tail(&ddpa->clone_snaps);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs origin_ds->ds_object);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &ddpa->unique, &unused, &unused);
99653d4ee642c6528e88224f12409a5f23060994eschrock
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee /*
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee * Walk the snapshots that we are moving
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee *
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * Compute space to transfer. Consider the incremental changes
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * to used by each snapshot:
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * (my used) = (prev's used) + (blocks born) - (blocks killed)
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * So each snapshot gave birth to:
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * (blocks born) = (my used) - (prev's used) + (blocks killed)
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee * So a sequence would look like:
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee * Which simplifies to:
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * uN + kN + kN-1 + ... + k1 + k0
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee * Note however, if we stop before we reach the ORIGIN we get:
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * uN + kN + kN-1 + ... + kM - uM-1
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek ss_mv_cnt = 0;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) {
99653d4ee642c6528e88224f12409a5f23060994eschrock uint64_t val, dlused, dlcomp, dluncomp;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee dsl_dataset_t *ds = snap->ds;
99653d4ee642c6528e88224f12409a5f23060994eschrock
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek ss_mv_cnt++;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If there are long holds, we won't be able to evict
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the objset.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (dsl_dataset_long_held(ds)) {
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens err = SET_ERROR(EBUSY);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens goto out;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
99653d4ee642c6528e88224f12409a5f23060994eschrock /* Check that the snapshot name does not conflict */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds));
cb5842f8b0caaad0ed53535bd77042e933fdbafeAndriy Gapon if (strlen(ds->ds_snapname) >= max_snap_len) {
cb5842f8b0caaad0ed53535bd77042e933fdbafeAndriy Gapon err = SET_ERROR(ENAMETOOLONG);
cb5842f8b0caaad0ed53535bd77042e933fdbafeAndriy Gapon goto out;
cb5842f8b0caaad0ed53535bd77042e933fdbafeAndriy Gapon }
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
681d9761e8516a7dc5ab6589e2dfe717777e1123Eric Taylor if (err == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens err = SET_ERROR(EEXIST);
681d9761e8516a7dc5ab6589e2dfe717777e1123Eric Taylor goto out;
681d9761e8516a7dc5ab6589e2dfe717777e1123Eric Taylor }
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee if (err != ENOENT)
681d9761e8516a7dc5ab6589e2dfe717777e1123Eric Taylor goto out;
99653d4ee642c6528e88224f12409a5f23060994eschrock
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee /* The very first snapshot does not have a deadlist */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens continue;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens &dlused, &dlcomp, &dluncomp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddpa->used += dlused;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddpa->comp += dlcomp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddpa->uncomp += dluncomp;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens }
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee /*
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee * If we are a clone of a clone then we never reached ORIGIN,
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee * so we need to subtract out the clone origin's used space.
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ddpa->origin_origin) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ddpa->used -=
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ddpa->comp -=
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddpa->uncomp -=
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ds_uncompressed_bytes;
99653d4ee642c6528e88224f12409a5f23060994eschrock }
99653d4ee642c6528e88224f12409a5f23060994eschrock
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /* Check that there is enough space and limit headroom here */
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek 0, ss_mv_cnt, ddpa->used, ddpa->cr);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens goto out;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens /*
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * Compute the amounts of space that will be used by snapshots
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * after the promotion (for both origin and clone). For each,
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * it is the amount of space that will be on all of their
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * deadlists (that was not born before their new origin).
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens uint64_t space;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens /*
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * Note, typically this will not be a clone of a clone,
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling * so dd_origin_txg will be < TXG_INITIAL, so
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens * these snaplist_space() -> dsl_deadlist_space_range()
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * calls will be fast because they do not have to
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens * iterate over all bps.
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap = list_head(&ddpa->origin_snaps);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = snaplist_space(&ddpa->shared_snaps,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens goto out;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = snaplist_space(&ddpa->clone_snaps,
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling snap->ds->ds_dir->dd_origin_txg, &space);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens goto out;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddpa->cloneusedsnap += space;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens }
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs DD_FLAG_USED_BREAKDOWN) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = snaplist_space(&ddpa->origin_snaps,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs &ddpa->originusedsnap);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens goto out;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee }
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
681d9761e8516a7dc5ab6589e2dfe717777e1123Eric Taylorout:
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens promote_rele(ddpa, FTAG);
681d9761e8516a7dc5ab6589e2dfe717777e1123Eric Taylor return (err);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens}
99653d4ee642c6528e88224f12409a5f23060994eschrock
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *hds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct promotenode *snap;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *origin_ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *origin_head;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_t *dd;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens dsl_dir_t *odd = NULL;
088f389458728c464569a5506b58070254fa4f7dahrens uint64_t oldnext_obj;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens int64_t delta;
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(promote_hold(ddpa, dp, FTAG));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens hds = ddpa->ddpa_clone;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
1d452cf5123cb6ac0a013a4dbd4dcceeb0da314dahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap = list_head(&ddpa->shared_snaps);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens origin_ds = snap->ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dd = hds->ds_dir;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap = list_head(&ddpa->origin_snaps);
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens origin_head = snap->ds;
74e7dc986c89efca1f2e4451c7a572e05e4a6e4fMatthew Ahrens
0b69c2f001a429251e2d38f25aca860396551214ahrens /*
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * We need to explicitly open odd, since origin_ds's dd will be
0b69c2f001a429251e2d38f25aca860396551214ahrens * changing.
0b69c2f001a429251e2d38f25aca860396551214ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens NULL, FTAG, &odd));
99653d4ee642c6528e88224f12409a5f23060994eschrock
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee /* change origin's next snap */
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap = list_tail(&ddpa->clone_snaps);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs origin_ds->ds_object);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
088f389458728c464569a5506b58070254fa4f7dahrens /* change the origin's next clone */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_remove_from_next_clones(origin_ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap->ds->ds_object, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
088f389458728c464569a5506b58070254fa4f7dahrens oldnext_obj, tx));
088f389458728c464569a5506b58070254fa4f7dahrens }
088f389458728c464569a5506b58070254fa4f7dahrens
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee /* change origin */
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee dmu_buf_will_dirty(dd->dd_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee dmu_buf_will_dirty(odd->dd_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling origin_head->ds_dir->dd_origin_txg =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg;
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /* change dd_clone entries */
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens hds->ds_object, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens origin_head->ds_object, tx));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dir_phys(dd)->dd_clones == 0) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(dd)->dd_clones =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs DMU_OT_NONE, 0, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens }
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
99653d4ee642c6528e88224f12409a5f23060994eschrock /* move snapshots to this dir */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) {
745cd3c5371d020efae7a911c58c526aa1fd0dbamaybee dsl_dataset_t *ds = snap->ds;
99653d4ee642c6528e88224f12409a5f23060994eschrock
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Property callbacks are registered to a particular
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * dsl_dir. Since ours is changing, evict the objset
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * so that they will be unregistered from the old dsl_dir.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens if (ds->ds_objset) {
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens dmu_objset_evict(ds->ds_objset);
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens ds->ds_objset = NULL;
3baa08fc5b6bea08a475b0cfe3ad161d74c5864bek }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
99653d4ee642c6528e88224f12409a5f23060994eschrock /* move snap name entry */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_snap_remove(origin_head,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek ds->ds_snapname, tx, B_TRUE));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
99653d4ee642c6528e88224f12409a5f23060994eschrock 8, 1, &ds->ds_object, tx));
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek dsl_fs_ss_count_adjust(hds->ds_dir, 1,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx);
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
99653d4ee642c6528e88224f12409a5f23060994eschrock /* change containing dsl_dir */
99653d4ee642c6528e88224f12409a5f23060994eschrock dmu_buf_will_dirty(ds->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens ASSERT3P(ds->ds_dir, ==, odd);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(ds->ds_dir, ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
99653d4ee642c6528e88224f12409a5f23060994eschrock NULL, ds, &ds->ds_dir));
99653d4ee642c6528e88224f12409a5f23060994eschrock
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens /* move any clone references */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_cursor_t zc;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens zap_attribute_t za;
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_retrieve(&zc, &za) == 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_advance(&zc)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *cnds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t o;
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (za.za_first_integer == oldnext_obj) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * We've already moved the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * origin's reference.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens continue;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens za.za_first_integer, FTAG, &cnds));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs o = dsl_dir_phys(cnds->ds_dir)->
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dd_head_dataset_obj;
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, o, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, o, tx));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(cnds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zap_cursor_fini(&zc);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
9082849ec1ce8be7d4eff196c8f4a3a9f75dc65dck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(!dsl_prop_hascb(ds));
a9799022bd90b13722204e80112efaa5bf573099ck }
a9799022bd90b13722204e80112efaa5bf573099ck
a9799022bd90b13722204e80112efaa5bf573099ck /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Change space accounting.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * both be valid, or both be 0 (resulting in delta == 0). This
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * is true for each of {clone,origin} independently.
a9799022bd90b13722204e80112efaa5bf573099ck */
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens delta = ddpa->cloneusedsnap -
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3S(delta, >=, 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(ddpa->used, >=, delta);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(dd, DD_USED_HEAD,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens delta = ddpa->originusedsnap -
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3S(delta, <=, 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(ddpa->used, >=, -delta);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(odd, DD_USED_HEAD,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* log history record */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(hds, "promote", tx, "");
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(odd, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens promote_rele(ddpa, FTAG);
a9799022bd90b13722204e80112efaa5bf573099ck}
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Make a list of dsl_dataset_t's for the snapshots between first_obj
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * (exclusive) and last_obj (inclusive). The list will be in reverse
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * order (last_obj will be the list_head()). If first_obj == 0, do all
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * snapshots back to this dataset's origin.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
a9799022bd90b13722204e80112efaa5bf573099ckstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenssnaplist_make(dsl_pool_t *dp,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
a9799022bd90b13722204e80112efaa5bf573099ck{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t obj = last_obj;
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens list_create(l, sizeof (struct promotenode),
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens offsetof(struct promotenode, link));
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens while (obj != first_obj) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct promotenode *snap;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int err;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(err != ENOENT);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (first_obj == 0)
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap->ds = ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens list_insert_tail(l, snap);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
a9799022bd90b13722204e80112efaa5bf573099ck
a9799022bd90b13722204e80112efaa5bf573099ck return (0);
a9799022bd90b13722204e80112efaa5bf573099ck}
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenssnaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
a9799022bd90b13722204e80112efaa5bf573099ck{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct promotenode *snap;
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *spacep = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens for (snap = list_head(l); snap; snap = list_next(l, snap)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t used, comp, uncomp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mintxg, UINT64_MAX, &used, &comp, &uncomp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *spacep += used;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
a9799022bd90b13722204e80112efaa5bf573099ck}
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenssnaplist_destroy(list_t *l, void *tag)
a9799022bd90b13722204e80112efaa5bf573099ck{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct promotenode *snap;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (l == NULL || !list_link_active(&l->list_head))
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return;
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens while ((snap = list_tail(l)) != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens list_remove(l, snap);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(snap->ds, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens kmem_free(snap, sizeof (*snap));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens list_destroy(l);
a9799022bd90b13722204e80112efaa5bf573099ck}
a9799022bd90b13722204e80112efaa5bf573099ck
a9799022bd90b13722204e80112efaa5bf573099ckstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenspromote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
a9799022bd90b13722204e80112efaa5bf573099ck{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_t *dd;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct promotenode *snap;
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &ddpa->ddpa_clone);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dd = ddpa->ddpa_clone->ds_dir;
a9799022bd90b13722204e80112efaa5bf573099ck
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (ddpa->ddpa_clone->ds_is_snapshot ||
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens !dsl_dir_is_clone(dd)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
a9799022bd90b13722204e80112efaa5bf573099ck
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &ddpa->shared_snaps, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens goto out;
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &ddpa->clone_snaps, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens goto out;
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snap = list_head(&ddpa->shared_snaps);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &ddpa->origin_snaps, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens goto out;
379c004d1f26b343f034bba8a350290691d00d38Eric Schrock
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens tag, &ddpa->origin_origin);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens goto out;
379c004d1f26b343f034bba8a350290691d00d38Eric Schrock }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensout:
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens promote_rele(ddpa, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
a9799022bd90b13722204e80112efaa5bf573099ck}
a9799022bd90b13722204e80112efaa5bf573099ck
a9799022bd90b13722204e80112efaa5bf573099ckstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenspromote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
a9799022bd90b13722204e80112efaa5bf573099ck{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snaplist_destroy(&ddpa->shared_snaps, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snaplist_destroy(&ddpa->clone_snaps, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens snaplist_destroy(&ddpa->origin_snaps, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ddpa->origin_origin != NULL)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ddpa->origin_origin, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
02c8f3f039188ad827739e040dd0e6f6de8fd943Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Promote a clone.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens * in with the name. (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_promote(const char *name, char *conflsnap)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_promote_arg_t ddpa = { 0 };
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t numsnaps;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *os;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * We will modify space proportional to the number of
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * snapshots. Compute numsnaps.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dmu_objset_hold(name, FTAG, &os);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs &numsnaps);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_objset_rele(os, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
02c8f3f039188ad827739e040dd0e6f6de8fd943Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddpa.ddpa_clonename = name;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddpa.err_ds = conflsnap;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek ddpa.cr = CRED();
02c8f3f039188ad827739e040dd0e6f6de8fd943Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (dsl_sync_task(name, dsl_dataset_promote_check,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens dsl_dataset_promote_sync, &ddpa,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
a9799022bd90b13722204e80112efaa5bf573099ck}
a9799022bd90b13722204e80112efaa5bf573099ck
a9799022bd90b13722204e80112efaa5bf573099ckint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
a9799022bd90b13722204e80112efaa5bf573099ck{
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald /*
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * "slack" factor for received datasets with refquota set on them.
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * See the bottom of this function for details on its use.
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald */
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald uint64_t refquota_slack = DMU_MAX_ACCESS * spa_asize_inflation;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int64_t unused_refres_delta;
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* they should both be heads */
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (clone->ds_is_snapshot ||
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs origin_head->ds_is_snapshot)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens /* if we are not forcing, the branch point should be just before them */
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (!force && clone->ds_prev != origin_head->ds_prev)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
a9799022bd90b13722204e80112efaa5bf573099ck
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* clone should be the clone (unless they are unrelated) */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (clone->ds_prev != NULL &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens origin_head->ds_dir != clone->ds_prev->ds_dir)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* the clone should be a child of the origin */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (clone->ds_dir->dd_parent != origin_head->ds_dir)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* origin_head shouldn't be modified unless 'force' */
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (!force &&
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ETXTBSY));
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* origin_head should have no long holds (e.g. is not mounted) */
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski if (dsl_dataset_handoff_check(origin_head, owner, tx))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EBUSY));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* check amount of any unconsumed refreservation */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens unused_refres_delta =
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (int64_t)MIN(origin_head->ds_reserved,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) -
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (int64_t)MIN(origin_head->ds_reserved,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (unused_refres_delta > 0 &&
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens unused_refres_delta >
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOSPC));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald /*
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * The clone can't be too much over the head's refquota.
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald *
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * To ensure that the entire refquota can be used, we allow one
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * transaction to exceed the the refquota. Therefore, this check
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * needs to also allow for the space referenced to be more than the
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * refquota. The maximum amount of space that one transaction can use
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * on disk is DMU_MAX_ACCESS * spa_asize_inflation. Allowing this
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * overage ensures that we are able to receive a filesystem that
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * exceeds the refquota on the source system.
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald *
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * So that overage is the refquota_slack we use below.
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (origin_head->ds_quota != 0 &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes >
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald origin_head->ds_quota + refquota_slack)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EDQUOT));
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby}
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirbyvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *origin_head, dmu_tx_t *tx)
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int64_t unused_refres_delta;
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(clone->ds_reserved == 0);
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald /*
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * NOTE: On DEBUG kernels there could be a race between this and
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald * the check function if spa_asize_inflation is adjusted...
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(origin_head->ds_quota == 0 ||
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota +
5f7a8e6d750cb070a3347f045201c6206caee6aaDan McDonald DMU_MAX_ACCESS * spa_asize_inflation);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens /*
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens * Swap per-dataset feature flags.
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens */
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (!(spa_feature_table[f].fi_flags &
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens ZFEATURE_FLAG_PER_DATASET)) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens ASSERT(!clone->ds_feature_inuse[f]);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens ASSERT(!origin_head->ds_feature_inuse[f]);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens continue;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens boolean_t clone_inuse = clone->ds_feature_inuse[f];
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f];
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (clone_inuse) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens clone->ds_feature_inuse[f] = B_FALSE;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (origin_head_inuse) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dsl_dataset_deactivate_feature(origin_head->ds_object,
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens f, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens origin_head->ds_feature_inuse[f] = B_FALSE;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (clone_inuse) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dsl_dataset_activate_feature(origin_head->ds_object,
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens f, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens origin_head->ds_feature_inuse[f] = B_TRUE;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (origin_head_inuse) {
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens dsl_dataset_activate_feature(clone->ds_object, f, tx);
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens clone->ds_feature_inuse[f] = B_TRUE;
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens }
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(clone->ds_dbuf, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (clone->ds_objset != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_objset_evict(clone->ds_objset);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens clone->ds_objset = NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (origin_head->ds_objset != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_objset_evict(origin_head->ds_objset);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens origin_head->ds_objset = NULL;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens unused_refres_delta =
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (int64_t)MIN(origin_head->ds_reserved,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) -
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (int64_t)MIN(origin_head->ds_reserved,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Reset origin's unique bytes, if it exists.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (clone->ds_prev) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *origin = clone->ds_prev;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t comp, uncomp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(origin->ds_dbuf, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* swap blkptrs */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens {
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&clone->ds_bp_rwlock, RW_WRITER, FTAG);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&origin_head->ds_bp_rwlock, RW_WRITER, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens blkptr_t tmp;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs tmp = dsl_dataset_phys(origin_head)->ds_bp;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_bp =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp = tmp;
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&origin_head->ds_bp_rwlock, FTAG);
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&clone->ds_bp_rwlock, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* set dd_*_bytes */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int64_t dused, dcomp, duncomp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t cdl_used, cdl_comp, cdl_uncomp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t odl_used, odl_comp, odl_uncomp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT3U(dsl_dir_phys(clone->ds_dir)->
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dd_used_breakdown[DD_USED_SNAP], ==, 0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_space(&clone->ds_deadlist,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_space(&origin_head->ds_deadlist,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &odl_used, &odl_comp, &odl_uncomp);
15508ac067f2fb55a439711838b971c02d42316fChris Kirby
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs cdl_used -
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs odl_used);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs cdl_comp -
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs odl_comp);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens cdl_uncomp -
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs odl_uncomp);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dused, dcomp, duncomp, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens -dused, -dcomp, -duncomp, tx);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The difference in the space used by snapshots is the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * difference in snapshot space due to the head's
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * deadlist (since that's the only thing that's
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * changing that affects the snapused).
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_space_range(&origin_head->ds_deadlist,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &odl_used, &odl_comp, &odl_uncomp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* swap ds_*_bytes */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(clone)->ds_compressed_bytes);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(clone)->ds_uncompressed_bytes);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* apply any parent delta for change in unconsumed refreservation */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens unused_refres_delta, 0, 0, tx);
ca45db4129beff691dc46576c328149443788af2Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Swap deadlists.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_close(&clone->ds_deadlist);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_close(&origin_head->ds_deadlist);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_deadlist_obj);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_scan_ds_clone_swapped(origin_head, clone, tx);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(clone, "clone swap", tx,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens "parent=%s", origin_head->ds_dir->dd_myname);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby}
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Given a pool name and a dataset object number in that pool,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * return the name of that dataset.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirbyint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby int error;
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_pool_hold(pname, FTAG, &dp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_name(ds, buf);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_rele(dp, FTAG);
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby return (error);
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby}
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby
842727c2f41f01b380de4f5e787d905702870f23Chris Kirbyint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error = 0;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3S(asize, >, 0);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * *ref_rsrv is the portion of asize that will come from any
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * unconsumed refreservation space.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *ref_rsrv = 0;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mutex_enter(&ds->ds_lock);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Make a space adjustment for reserved bytes.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(*used, >=,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs *used -=
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *ref_rsrv =
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens asize - MIN(asize, parent_delta(ds, asize + inflight));
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!check_quota || ds->ds_quota == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mutex_exit(&ds->ds_lock);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If they are requesting more space, and our current estimate
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * is over quota, they get to try again unless the actual
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * on-disk is over quota and there are no pending changes (which
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * may free up space for us).
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ds->ds_quota) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (inflight > 0 ||
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens error = SET_ERROR(ERESTART);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens else
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens error = SET_ERROR(EDQUOT);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mutex_exit(&ds->ds_lock);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby return (error);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby}
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenstypedef struct dsl_dataset_set_qr_arg {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *ddsqra_name;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zprop_source_t ddsqra_source;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t ddsqra_value;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens} dsl_dataset_set_qr_arg_t;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/* ARGSUSED */
842727c2f41f01b380de4f5e787d905702870f23Chris Kirbystatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t newval;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOTSUP));
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (ds->ds_is_snapshot) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_prop_predict(ds->ds_dir,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA),
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby return (error);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (newval == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens newval < ds->ds_reserved) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOSPC));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby return (0);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby}
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t newval;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_prop_set_sync_impl(ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA),
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &ddsqra->ddsqra_value, tx);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ds->ds_quota != newval) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_quota = newval;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby}
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t refquota)
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_set_qr_arg_t ddsqra;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsqra.ddsqra_name = dsname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsqra.ddsqra_source = source;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsqra.ddsqra_value = refquota;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby}
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
842727c2f41f01b380de4f5e787d905702870f23Chris Kirbystatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby dsl_dataset_t *ds;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t newval, unique;
d7747cbcf0e2da91e8d0e3dfd6d3ac45da469773Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOTSUP));
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby return (error);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (ds->ds_is_snapshot) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_prop_predict(ds->ds_dir,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby return (error);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If we are doing the preliminary check in open context, the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * space estimates may be inaccurate.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!dmu_tx_is_syncing(tx)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mutex_enter(&ds->ds_lock);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!DS_UNIQUE_IS_ACCURATE(ds))
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_recalc_head_uniq(ds);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mutex_exit(&ds->ds_lock);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t delta = MAX(unique, newval) -
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens MAX(unique, ds->ds_reserved);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (delta >
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (ds->ds_quota > 0 && newval > ds->ds_quota)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOSPC));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby }
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby}
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensvoid
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zprop_source_t source, uint64_t value, dmu_tx_t *tx)
ca45db4129beff691dc46576c328149443788af2Chris Kirby{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t newval;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t unique;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int64_t delta;
ca45db4129beff691dc46576c328149443788af2Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens source, sizeof (value), 1, &value, tx);
ca45db4129beff691dc46576c328149443788af2Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mutex_enter(&ds->ds_dir->dd_lock);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mutex_enter(&ds->ds_lock);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens delta = MAX(0, (int64_t)(newval - unique)) -
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens MAX(0, (int64_t)(ds->ds_reserved - unique));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ds->ds_reserved = newval;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mutex_exit(&ds->ds_lock);
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens mutex_exit(&ds->ds_dir->dd_lock);
ca45db4129beff691dc46576c328149443788af2Chris Kirby}
ca45db4129beff691dc46576c328149443788af2Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby dsl_dataset_t *ds;
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_set_refreservation_sync_impl(ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby dsl_dataset_rele(ds, FTAG);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby}
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t refreservation)
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_set_qr_arg_t ddsqra;
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsqra.ddsqra_name = dsname;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsqra.ddsqra_source = source;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ddsqra.ddsqra_value = refreservation;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens dsl_dataset_set_refreservation_sync, &ddsqra,
7d46dc6ca63a6f3f0d51aa655bfcf10cf2405a9eMatthew Ahrens 0, ZFS_SPACE_CHECK_NONE));
503ad85c168c7992ccc310af845a581cff3c72b5Matthew Ahrens}
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens/*
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Return (in *usedp) the amount of space written in new that is not
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * present in oldsnap. New may be a snapshot or the head. Old must be
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * a snapshot before new, in new's filesystem (or its origin). If not then
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * fail and return EINVAL.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * The written space is calculated by considering two components: First, we
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * ignore any freed space, and calculate the written as new's used space
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * minus old's used space. Next, we add in the amount of space that was freed
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * between the two snapshots, thus reducing new's used space relative to old's.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Specifically, this is the space that was born before old->ds_creation_txg,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * and freed before new (ie. on new's deadlist or a previous deadlist).
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * space freed [---------------------]
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * snapshots ---O-------O--------O-------O------
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * oldsnap new
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens */
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrensint
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrensdsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens{
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens int err = 0;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens uint64_t snapobj;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_pool_t *dp = new->ds_dir->dd_pool;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dsl_pool_config_held(dp));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *usedp = 0;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs *usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs *usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *compp = 0;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs *compp += dsl_dataset_phys(new)->ds_compressed_bytes;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs *compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *uncompp = 0;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs *uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens snapobj = new->ds_object;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens while (snapobj != oldsnap->ds_object) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_dataset_t *snap;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens uint64_t used, comp, uncomp;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden if (snapobj == new->ds_object) {
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden snap = new;
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden } else {
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden if (err != 0)
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden break;
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(snap)->ds_prev_snap_txg ==
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(oldsnap)->ds_creation_txg) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /*
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * The blocks in the deadlist can not be born after
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * ds_prev_snap_txg, so get the whole deadlist space,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * which is more efficient (especially for old-format
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * deadlists). Unfortunately the deadlist code
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * doesn't have enough information to make this
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * optimization itself.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens */
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_deadlist_space(&snap->ds_deadlist,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens &used, &comp, &uncomp);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens } else {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_deadlist_space_range(&snap->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs 0, dsl_dataset_phys(oldsnap)->ds_creation_txg,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens &used, &comp, &uncomp);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *usedp += used;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *compp += comp;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *uncompp += uncomp;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /*
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * If we get to the beginning of the chain of snapshots
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * was not a snapshot of/before new.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden if (snap != new)
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden dsl_dataset_rele(snap, FTAG);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (snapobj == 0) {
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens err = SET_ERROR(EINVAL);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens break;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens return (err);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens}
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens/*
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * lastsnap, and all snapshots in between are deleted.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * blocks that would be freed [---------------------------]
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * snapshots ---O-------O--------O-------O--------O
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * firstsnap lastsnap
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * This is the set of blocks that were born after the snap before firstsnap,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * (birth > firstsnap->prev_snap_txg) and died before the snap after the
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * We calculate this by iterating over the relevant deadlists (from the snap
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * after lastsnap, backward to the snap after firstsnap), summing up the
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * space on the deadlist that was born after the snap before firstsnap.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens */
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrensint
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrensdsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_dataset_t *lastsnap,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens{
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens int err = 0;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens uint64_t snapobj;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs ASSERT(firstsnap->ds_is_snapshot);
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs ASSERT(lastsnap->ds_is_snapshot);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /*
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Check that the snapshots are in the same dsl_dir, and firstsnap
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * is before lastsnap.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens */
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (firstsnap->ds_dir != lastsnap->ds_dir ||
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_creation_txg >
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(lastsnap)->ds_creation_txg)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *usedp = *compp = *uncompp = 0;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens while (snapobj != firstsnap->ds_object) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_dataset_t *ds;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens uint64_t used, comp, uncomp;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (err != 0)
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens break;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_deadlist_space_range(&ds->ds_deadlist,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens &used, &comp, &uncomp);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *usedp += used;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *compp += comp;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens *uncompp += uncomp;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens ASSERT3U(snapobj, !=, 0);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_dataset_rele(ds, FTAG);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens return (err);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * For example, they could both be snapshots of the same filesystem, and
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * 'earlier' is before 'later'. Or 'earlier' could be the origin of
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * filesystem. Or 'earlier' could be the origin's origin.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensboolean_t
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrensdsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
9a686fbc186e8e2a64e9a5094d44c7d6fa0ea167Paul Dagnelie uint64_t earlier_txg)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = later->ds_dir->dd_pool;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens boolean_t ret;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dsl_pool_config_held(dp));
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (earlier_txg == 0)
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (later->ds_is_snapshot &&
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (B_FALSE);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (later->ds_dir == earlier->ds_dir)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (B_TRUE);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!dsl_dir_is_clone(later->ds_dir))
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (B_FALSE);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (B_TRUE);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *origin;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold_obj(dp,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (B_FALSE);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(origin, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (ret);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensvoid
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrensdsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens{
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
2acef22db7808606888f8f92715629ff3ba555b9Matthew Ahrens}
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensboolean_t
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensdsl_dataset_is_zapified(dsl_dataset_t *ds)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens{
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dmu_object_info_t doi;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dmu_object_info_from_db(ds->ds_dbuf, &doi);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (doi.doi_type == DMU_OTN_ZAP_METADATA);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens}
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensboolean_t
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensdsl_dataset_has_resume_receive_state(dsl_dataset_t *ds)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens{
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (dsl_dataset_is_zapified(ds) &&
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens zap_contains(ds->ds_dir->dd_pool->dp_meta_objset,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens}