1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy * CDDL HEADER START
3eda14cbcSMatt Macy *
4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy *
8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy * See the License for the specific language governing permissions
11eda14cbcSMatt Macy * and limitations under the License.
12eda14cbcSMatt Macy *
13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy *
19eda14cbcSMatt Macy * CDDL HEADER END
20eda14cbcSMatt Macy */
21eda14cbcSMatt Macy
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25eda14cbcSMatt Macy * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26eda14cbcSMatt Macy * Copyright (c) 2014 RackTop Systems.
27eda14cbcSMatt Macy * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28eda14cbcSMatt Macy * Copyright (c) 2016 Actifio, Inc. All rights reserved.
29eda14cbcSMatt Macy * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved.
30eda14cbcSMatt Macy * Copyright 2017 Nexenta Systems, Inc.
31eda14cbcSMatt Macy * Copyright (c) 2019, Klara Inc.
32eda14cbcSMatt Macy * Copyright (c) 2019, Allan Jude
33eda14cbcSMatt Macy * Copyright (c) 2020 The FreeBSD Foundation [1]
34eda14cbcSMatt Macy *
35eda14cbcSMatt Macy * [1] Portions of this software were developed by Allan Jude
36eda14cbcSMatt Macy * under sponsorship from the FreeBSD Foundation.
37eda14cbcSMatt Macy */
38eda14cbcSMatt Macy
39eda14cbcSMatt Macy #include <sys/dmu_objset.h>
40eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
41eda14cbcSMatt Macy #include <sys/dsl_dir.h>
42eda14cbcSMatt Macy #include <sys/dsl_prop.h>
43eda14cbcSMatt Macy #include <sys/dsl_synctask.h>
44eda14cbcSMatt Macy #include <sys/dmu_traverse.h>
45eda14cbcSMatt Macy #include <sys/dmu_impl.h>
46eda14cbcSMatt Macy #include <sys/dmu_tx.h>
47eda14cbcSMatt Macy #include <sys/arc.h>
48eda14cbcSMatt Macy #include <sys/zio.h>
49eda14cbcSMatt Macy #include <sys/zap.h>
50eda14cbcSMatt Macy #include <sys/zfeature.h>
51eda14cbcSMatt Macy #include <sys/unique.h>
52eda14cbcSMatt Macy #include <sys/zfs_context.h>
53eda14cbcSMatt Macy #include <sys/zfs_ioctl.h>
54eda14cbcSMatt Macy #include <sys/spa.h>
55eda14cbcSMatt Macy #include <sys/spa_impl.h>
56eda14cbcSMatt Macy #include <sys/vdev.h>
57eda14cbcSMatt Macy #include <sys/zfs_znode.h>
58eda14cbcSMatt Macy #include <sys/zfs_onexit.h>
59eda14cbcSMatt Macy #include <sys/zvol.h>
60eda14cbcSMatt Macy #include <sys/dsl_scan.h>
61eda14cbcSMatt Macy #include <sys/dsl_deadlist.h>
62eda14cbcSMatt Macy #include <sys/dsl_destroy.h>
63eda14cbcSMatt Macy #include <sys/dsl_userhold.h>
64eda14cbcSMatt Macy #include <sys/dsl_bookmark.h>
65eda14cbcSMatt Macy #include <sys/policy.h>
66eda14cbcSMatt Macy #include <sys/dmu_send.h>
67eda14cbcSMatt Macy #include <sys/dmu_recv.h>
68eda14cbcSMatt Macy #include <sys/zio_compress.h>
69eda14cbcSMatt Macy #include <zfs_fletcher.h>
70eda14cbcSMatt Macy #include <sys/zio_checksum.h>
71eda14cbcSMatt Macy
72eda14cbcSMatt Macy /*
73eda14cbcSMatt Macy * The SPA supports block sizes up to 16MB. However, very large blocks
74eda14cbcSMatt Macy * can have an impact on i/o latency (e.g. tying up a spinning disk for
75eda14cbcSMatt Macy * ~300ms), and also potentially on the memory allocator. Therefore,
76716fd348SMartin Matuska * we did not allow the recordsize to be set larger than zfs_max_recordsize
77716fd348SMartin Matuska * (former default: 1MB). Larger blocks could be created by changing this
78716fd348SMartin Matuska * tunable, and pools with larger blocks could always be imported and used,
79716fd348SMartin Matuska * regardless of this setting.
80716fd348SMartin Matuska *
81716fd348SMartin Matuska * We do, however, still limit it by default to 1M on x86_32, because Linux's
82716fd348SMartin Matuska * 3/1 memory split doesn't leave much room for 16M chunks.
83eda14cbcSMatt Macy */
84716fd348SMartin Matuska #ifdef _ILP32
85be181ee2SMartin Matuska uint_t zfs_max_recordsize = 1 * 1024 * 1024;
86716fd348SMartin Matuska #else
87be181ee2SMartin Matuska uint_t zfs_max_recordsize = 16 * 1024 * 1024;
88716fd348SMartin Matuska #endif
89e92ffd9bSMartin Matuska static int zfs_allow_redacted_dataset_mount = 0;
90eda14cbcSMatt Macy
9108aba0aeSMartin Matuska int zfs_snapshot_history_enabled = 1;
9208aba0aeSMartin Matuska
93eda14cbcSMatt Macy #define SWITCH64(x, y) \
94eda14cbcSMatt Macy { \
95eda14cbcSMatt Macy uint64_t __tmp = (x); \
96eda14cbcSMatt Macy (x) = (y); \
97eda14cbcSMatt Macy (y) = __tmp; \
98eda14cbcSMatt Macy }
99eda14cbcSMatt Macy
100eda14cbcSMatt Macy #define DS_REF_MAX (1ULL << 62)
101eda14cbcSMatt Macy
102eda14cbcSMatt Macy static void dsl_dataset_set_remap_deadlist_object(dsl_dataset_t *ds,
103eda14cbcSMatt Macy uint64_t obj, dmu_tx_t *tx);
104eda14cbcSMatt Macy static void dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t *ds,
105eda14cbcSMatt Macy dmu_tx_t *tx);
106eda14cbcSMatt Macy
107eda14cbcSMatt Macy static void unload_zfeature(dsl_dataset_t *ds, spa_feature_t f);
108eda14cbcSMatt Macy
109be181ee2SMartin Matuska extern uint_t spa_asize_inflation;
110eda14cbcSMatt Macy
111eda14cbcSMatt Macy static zil_header_t zero_zil;
112eda14cbcSMatt Macy
113eda14cbcSMatt Macy /*
114eda14cbcSMatt Macy * Figure out how much of this delta should be propagated to the dsl_dir
115eda14cbcSMatt Macy * layer. If there's a refreservation, that space has already been
116eda14cbcSMatt Macy * partially accounted for in our ancestors.
117eda14cbcSMatt Macy */
118eda14cbcSMatt Macy static int64_t
parent_delta(dsl_dataset_t * ds,int64_t delta)119eda14cbcSMatt Macy parent_delta(dsl_dataset_t *ds, int64_t delta)
120eda14cbcSMatt Macy {
121eda14cbcSMatt Macy dsl_dataset_phys_t *ds_phys;
122eda14cbcSMatt Macy uint64_t old_bytes, new_bytes;
123eda14cbcSMatt Macy
124eda14cbcSMatt Macy if (ds->ds_reserved == 0)
125eda14cbcSMatt Macy return (delta);
126eda14cbcSMatt Macy
127eda14cbcSMatt Macy ds_phys = dsl_dataset_phys(ds);
128eda14cbcSMatt Macy old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
129eda14cbcSMatt Macy new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
130eda14cbcSMatt Macy
131eda14cbcSMatt Macy ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
132eda14cbcSMatt Macy return (new_bytes - old_bytes);
133eda14cbcSMatt Macy }
134eda14cbcSMatt Macy
135eda14cbcSMatt Macy void
dsl_dataset_block_born(dsl_dataset_t * ds,const blkptr_t * bp,dmu_tx_t * tx)136eda14cbcSMatt Macy dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
137eda14cbcSMatt Macy {
138eda14cbcSMatt Macy spa_t *spa = dmu_tx_pool(tx)->dp_spa;
139eda14cbcSMatt Macy int used = bp_get_dsize_sync(spa, bp);
140eda14cbcSMatt Macy int compressed = BP_GET_PSIZE(bp);
141eda14cbcSMatt Macy int uncompressed = BP_GET_UCSIZE(bp);
142eda14cbcSMatt Macy int64_t delta;
143c7046f76SMartin Matuska spa_feature_t f;
144eda14cbcSMatt Macy
145eda14cbcSMatt Macy dprintf_bp(bp, "ds=%p", ds);
146eda14cbcSMatt Macy
147eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx));
148eda14cbcSMatt Macy /* It could have been compressed away to nothing */
149eda14cbcSMatt Macy if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp))
150eda14cbcSMatt Macy return;
151eda14cbcSMatt Macy ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
152eda14cbcSMatt Macy ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
153eda14cbcSMatt Macy if (ds == NULL) {
154eda14cbcSMatt Macy dsl_pool_mos_diduse_space(tx->tx_pool,
155eda14cbcSMatt Macy used, compressed, uncompressed);
156eda14cbcSMatt Macy return;
157eda14cbcSMatt Macy }
158eda14cbcSMatt Macy
159783d3ff6SMartin Matuska ASSERT3U(BP_GET_LOGICAL_BIRTH(bp), >,
160783d3ff6SMartin Matuska dsl_dataset_phys(ds)->ds_prev_snap_txg);
161eda14cbcSMatt Macy dmu_buf_will_dirty(ds->ds_dbuf, tx);
162eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
163eda14cbcSMatt Macy delta = parent_delta(ds, used);
164eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_referenced_bytes += used;
165eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
166eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
167eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_unique_bytes += used;
168eda14cbcSMatt Macy
169c7046f76SMartin Matuska if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
170c7046f76SMartin Matuska ds->ds_feature_activation[SPA_FEATURE_LARGE_BLOCKS] =
171c7046f76SMartin Matuska (void *)B_TRUE;
172c7046f76SMartin Matuska }
173c7046f76SMartin Matuska
174c7046f76SMartin Matuska
175c7046f76SMartin Matuska f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp));
176c7046f76SMartin Matuska if (f != SPA_FEATURE_NONE) {
177c7046f76SMartin Matuska ASSERT3S(spa_feature_table[f].fi_type, ==,
178c7046f76SMartin Matuska ZFEATURE_TYPE_BOOLEAN);
179c7046f76SMartin Matuska ds->ds_feature_activation[f] = (void *)B_TRUE;
180c7046f76SMartin Matuska }
181c7046f76SMartin Matuska
182c7046f76SMartin Matuska f = zio_compress_to_feature(BP_GET_COMPRESS(bp));
183c7046f76SMartin Matuska if (f != SPA_FEATURE_NONE) {
184c7046f76SMartin Matuska ASSERT3S(spa_feature_table[f].fi_type, ==,
185c7046f76SMartin Matuska ZFEATURE_TYPE_BOOLEAN);
186c7046f76SMartin Matuska ds->ds_feature_activation[f] = (void *)B_TRUE;
187c7046f76SMartin Matuska }
188eda14cbcSMatt Macy
189eda14cbcSMatt Macy /*
190eda14cbcSMatt Macy * Track block for livelist, but ignore embedded blocks because
191eda14cbcSMatt Macy * they do not need to be freed.
192eda14cbcSMatt Macy */
193eda14cbcSMatt Macy if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) &&
194783d3ff6SMartin Matuska BP_GET_LOGICAL_BIRTH(bp) > ds->ds_dir->dd_origin_txg &&
195eda14cbcSMatt Macy !(BP_IS_EMBEDDED(bp))) {
196eda14cbcSMatt Macy ASSERT(dsl_dir_is_clone(ds->ds_dir));
197eda14cbcSMatt Macy ASSERT(spa_feature_is_enabled(spa,
198eda14cbcSMatt Macy SPA_FEATURE_LIVELIST));
199eda14cbcSMatt Macy bplist_append(&ds->ds_dir->dd_pending_allocs, bp);
200eda14cbcSMatt Macy }
201eda14cbcSMatt Macy
202eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
2033f9d360cSMartin Matuska dsl_dir_diduse_transfer_space(ds->ds_dir, delta,
2043f9d360cSMartin Matuska compressed, uncompressed, used,
205eda14cbcSMatt Macy DD_USED_REFRSRV, DD_USED_HEAD, tx);
206eda14cbcSMatt Macy }
207eda14cbcSMatt Macy
208eda14cbcSMatt Macy /*
209eda14cbcSMatt Macy * Called when the specified segment has been remapped, and is thus no
210eda14cbcSMatt Macy * longer referenced in the head dataset. The vdev must be indirect.
211eda14cbcSMatt Macy *
212eda14cbcSMatt Macy * If the segment is referenced by a snapshot, put it on the remap deadlist.
213eda14cbcSMatt Macy * Otherwise, add this segment to the obsolete spacemap.
214eda14cbcSMatt Macy */
215eda14cbcSMatt Macy void
dsl_dataset_block_remapped(dsl_dataset_t * ds,uint64_t vdev,uint64_t offset,uint64_t size,uint64_t birth,dmu_tx_t * tx)216eda14cbcSMatt Macy dsl_dataset_block_remapped(dsl_dataset_t *ds, uint64_t vdev, uint64_t offset,
217eda14cbcSMatt Macy uint64_t size, uint64_t birth, dmu_tx_t *tx)
218eda14cbcSMatt Macy {
219eda14cbcSMatt Macy spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
220eda14cbcSMatt Macy
221eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx));
222eda14cbcSMatt Macy ASSERT(birth <= tx->tx_txg);
223eda14cbcSMatt Macy ASSERT(!ds->ds_is_snapshot);
224eda14cbcSMatt Macy
225eda14cbcSMatt Macy if (birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
226eda14cbcSMatt Macy spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx);
227eda14cbcSMatt Macy } else {
228eda14cbcSMatt Macy blkptr_t fakebp;
229eda14cbcSMatt Macy dva_t *dva = &fakebp.blk_dva[0];
230eda14cbcSMatt Macy
231eda14cbcSMatt Macy ASSERT(ds != NULL);
232eda14cbcSMatt Macy
233eda14cbcSMatt Macy mutex_enter(&ds->ds_remap_deadlist_lock);
234eda14cbcSMatt Macy if (!dsl_dataset_remap_deadlist_exists(ds)) {
235eda14cbcSMatt Macy dsl_dataset_create_remap_deadlist(ds, tx);
236eda14cbcSMatt Macy }
237eda14cbcSMatt Macy mutex_exit(&ds->ds_remap_deadlist_lock);
238eda14cbcSMatt Macy
239eda14cbcSMatt Macy BP_ZERO(&fakebp);
240783d3ff6SMartin Matuska BP_SET_LOGICAL_BIRTH(&fakebp, birth);
241eda14cbcSMatt Macy DVA_SET_VDEV(dva, vdev);
242eda14cbcSMatt Macy DVA_SET_OFFSET(dva, offset);
243eda14cbcSMatt Macy DVA_SET_ASIZE(dva, size);
244eda14cbcSMatt Macy dsl_deadlist_insert(&ds->ds_remap_deadlist, &fakebp, B_FALSE,
245eda14cbcSMatt Macy tx);
246eda14cbcSMatt Macy }
247eda14cbcSMatt Macy }
248eda14cbcSMatt Macy
249eda14cbcSMatt Macy int
dsl_dataset_block_kill(dsl_dataset_t * ds,const blkptr_t * bp,dmu_tx_t * tx,boolean_t async)250eda14cbcSMatt Macy dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
251eda14cbcSMatt Macy boolean_t async)
252eda14cbcSMatt Macy {
253eda14cbcSMatt Macy spa_t *spa = dmu_tx_pool(tx)->dp_spa;
254eda14cbcSMatt Macy
255eda14cbcSMatt Macy int used = bp_get_dsize_sync(spa, bp);
256eda14cbcSMatt Macy int compressed = BP_GET_PSIZE(bp);
257eda14cbcSMatt Macy int uncompressed = BP_GET_UCSIZE(bp);
258eda14cbcSMatt Macy
259eda14cbcSMatt Macy if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp))
260eda14cbcSMatt Macy return (0);
261eda14cbcSMatt Macy
262eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx));
263783d3ff6SMartin Matuska ASSERT(BP_GET_LOGICAL_BIRTH(bp) <= tx->tx_txg);
264eda14cbcSMatt Macy
265eda14cbcSMatt Macy if (ds == NULL) {
266eda14cbcSMatt Macy dsl_free(tx->tx_pool, tx->tx_txg, bp);
267eda14cbcSMatt Macy dsl_pool_mos_diduse_space(tx->tx_pool,
268eda14cbcSMatt Macy -used, -compressed, -uncompressed);
269eda14cbcSMatt Macy return (used);
270eda14cbcSMatt Macy }
271eda14cbcSMatt Macy ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
272eda14cbcSMatt Macy
273eda14cbcSMatt Macy ASSERT(!ds->ds_is_snapshot);
274eda14cbcSMatt Macy dmu_buf_will_dirty(ds->ds_dbuf, tx);
275eda14cbcSMatt Macy
276eda14cbcSMatt Macy /*
277eda14cbcSMatt Macy * Track block for livelist, but ignore embedded blocks because
278eda14cbcSMatt Macy * they do not need to be freed.
279eda14cbcSMatt Macy */
280eda14cbcSMatt Macy if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) &&
281783d3ff6SMartin Matuska BP_GET_LOGICAL_BIRTH(bp) > ds->ds_dir->dd_origin_txg &&
282eda14cbcSMatt Macy !(BP_IS_EMBEDDED(bp))) {
283eda14cbcSMatt Macy ASSERT(dsl_dir_is_clone(ds->ds_dir));
284eda14cbcSMatt Macy ASSERT(spa_feature_is_enabled(spa,
285eda14cbcSMatt Macy SPA_FEATURE_LIVELIST));
286eda14cbcSMatt Macy bplist_append(&ds->ds_dir->dd_pending_frees, bp);
287eda14cbcSMatt Macy }
288eda14cbcSMatt Macy
289783d3ff6SMartin Matuska if (BP_GET_LOGICAL_BIRTH(bp) > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
290eda14cbcSMatt Macy int64_t delta;
291eda14cbcSMatt Macy
29233b8c039SMartin Matuska dprintf_bp(bp, "freeing ds=%llu", (u_longlong_t)ds->ds_object);
293eda14cbcSMatt Macy dsl_free(tx->tx_pool, tx->tx_txg, bp);
294eda14cbcSMatt Macy
295eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
296eda14cbcSMatt Macy ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
297eda14cbcSMatt Macy !DS_UNIQUE_IS_ACCURATE(ds));
298eda14cbcSMatt Macy delta = parent_delta(ds, -used);
299eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_unique_bytes -= used;
300eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
3013f9d360cSMartin Matuska dsl_dir_diduse_transfer_space(ds->ds_dir,
3023f9d360cSMartin Matuska delta, -compressed, -uncompressed, -used,
303eda14cbcSMatt Macy DD_USED_REFRSRV, DD_USED_HEAD, tx);
304eda14cbcSMatt Macy } else {
305eda14cbcSMatt Macy dprintf_bp(bp, "putting on dead list: %s", "");
306eda14cbcSMatt Macy if (async) {
307eda14cbcSMatt Macy /*
308eda14cbcSMatt Macy * We are here as part of zio's write done callback,
309eda14cbcSMatt Macy * which means we're a zio interrupt thread. We can't
310eda14cbcSMatt Macy * call dsl_deadlist_insert() now because it may block
311eda14cbcSMatt Macy * waiting for I/O. Instead, put bp on the deferred
312eda14cbcSMatt Macy * queue and let dsl_pool_sync() finish the job.
313eda14cbcSMatt Macy */
314eda14cbcSMatt Macy bplist_append(&ds->ds_pending_deadlist, bp);
315eda14cbcSMatt Macy } else {
316eda14cbcSMatt Macy dsl_deadlist_insert(&ds->ds_deadlist, bp, B_FALSE, tx);
317eda14cbcSMatt Macy }
318eda14cbcSMatt Macy ASSERT3U(ds->ds_prev->ds_object, ==,
319eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj);
320eda14cbcSMatt Macy ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
321783d3ff6SMartin Matuska /* if (logical birth > prev prev snap txg) prev unique += bs */
322eda14cbcSMatt Macy if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
323783d3ff6SMartin Matuska ds->ds_object && BP_GET_LOGICAL_BIRTH(bp) >
324eda14cbcSMatt Macy dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
325eda14cbcSMatt Macy dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
326eda14cbcSMatt Macy mutex_enter(&ds->ds_prev->ds_lock);
327eda14cbcSMatt Macy dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
328eda14cbcSMatt Macy mutex_exit(&ds->ds_prev->ds_lock);
329eda14cbcSMatt Macy }
330783d3ff6SMartin Matuska if (BP_GET_LOGICAL_BIRTH(bp) > ds->ds_dir->dd_origin_txg) {
331eda14cbcSMatt Macy dsl_dir_transfer_space(ds->ds_dir, used,
332eda14cbcSMatt Macy DD_USED_HEAD, DD_USED_SNAP, tx);
333eda14cbcSMatt Macy }
334eda14cbcSMatt Macy }
335eda14cbcSMatt Macy
336eda14cbcSMatt Macy dsl_bookmark_block_killed(ds, bp, tx);
337eda14cbcSMatt Macy
338eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
339eda14cbcSMatt Macy ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
340eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
341eda14cbcSMatt Macy ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
342eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
343eda14cbcSMatt Macy ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
344eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
345eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
346eda14cbcSMatt Macy
347eda14cbcSMatt Macy return (used);
348eda14cbcSMatt Macy }
349eda14cbcSMatt Macy
350eda14cbcSMatt Macy struct feature_type_uint64_array_arg {
351eda14cbcSMatt Macy uint64_t length;
352eda14cbcSMatt Macy uint64_t *array;
353eda14cbcSMatt Macy };
354eda14cbcSMatt Macy
355eda14cbcSMatt Macy static void
unload_zfeature(dsl_dataset_t * ds,spa_feature_t f)356eda14cbcSMatt Macy unload_zfeature(dsl_dataset_t *ds, spa_feature_t f)
357eda14cbcSMatt Macy {
358eda14cbcSMatt Macy switch (spa_feature_table[f].fi_type) {
359eda14cbcSMatt Macy case ZFEATURE_TYPE_BOOLEAN:
360eda14cbcSMatt Macy break;
361eda14cbcSMatt Macy case ZFEATURE_TYPE_UINT64_ARRAY:
362eda14cbcSMatt Macy {
363eda14cbcSMatt Macy struct feature_type_uint64_array_arg *ftuaa = ds->ds_feature[f];
364eda14cbcSMatt Macy kmem_free(ftuaa->array, ftuaa->length * sizeof (uint64_t));
365eda14cbcSMatt Macy kmem_free(ftuaa, sizeof (*ftuaa));
366eda14cbcSMatt Macy break;
367eda14cbcSMatt Macy }
368eda14cbcSMatt Macy default:
369eda14cbcSMatt Macy panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
370eda14cbcSMatt Macy }
371eda14cbcSMatt Macy }
372eda14cbcSMatt Macy
373eda14cbcSMatt Macy static int
load_zfeature(objset_t * mos,dsl_dataset_t * ds,spa_feature_t f)374eda14cbcSMatt Macy load_zfeature(objset_t *mos, dsl_dataset_t *ds, spa_feature_t f)
375eda14cbcSMatt Macy {
376eda14cbcSMatt Macy int err = 0;
377eda14cbcSMatt Macy switch (spa_feature_table[f].fi_type) {
378eda14cbcSMatt Macy case ZFEATURE_TYPE_BOOLEAN:
379eda14cbcSMatt Macy err = zap_contains(mos, ds->ds_object,
380eda14cbcSMatt Macy spa_feature_table[f].fi_guid);
381eda14cbcSMatt Macy if (err == 0) {
382eda14cbcSMatt Macy ds->ds_feature[f] = (void *)B_TRUE;
383eda14cbcSMatt Macy } else {
384eda14cbcSMatt Macy ASSERT3U(err, ==, ENOENT);
385eda14cbcSMatt Macy err = 0;
386eda14cbcSMatt Macy }
387eda14cbcSMatt Macy break;
388eda14cbcSMatt Macy case ZFEATURE_TYPE_UINT64_ARRAY:
389eda14cbcSMatt Macy {
390eda14cbcSMatt Macy uint64_t int_size, num_int;
391eda14cbcSMatt Macy uint64_t *data;
392eda14cbcSMatt Macy err = zap_length(mos, ds->ds_object,
393eda14cbcSMatt Macy spa_feature_table[f].fi_guid, &int_size, &num_int);
394eda14cbcSMatt Macy if (err != 0) {
395eda14cbcSMatt Macy ASSERT3U(err, ==, ENOENT);
396eda14cbcSMatt Macy err = 0;
397eda14cbcSMatt Macy break;
398eda14cbcSMatt Macy }
399eda14cbcSMatt Macy ASSERT3U(int_size, ==, sizeof (uint64_t));
400eda14cbcSMatt Macy data = kmem_alloc(int_size * num_int, KM_SLEEP);
401eda14cbcSMatt Macy VERIFY0(zap_lookup(mos, ds->ds_object,
402eda14cbcSMatt Macy spa_feature_table[f].fi_guid, int_size, num_int, data));
403eda14cbcSMatt Macy struct feature_type_uint64_array_arg *ftuaa =
404eda14cbcSMatt Macy kmem_alloc(sizeof (*ftuaa), KM_SLEEP);
405eda14cbcSMatt Macy ftuaa->length = num_int;
406eda14cbcSMatt Macy ftuaa->array = data;
407eda14cbcSMatt Macy ds->ds_feature[f] = ftuaa;
408eda14cbcSMatt Macy break;
409eda14cbcSMatt Macy }
410eda14cbcSMatt Macy default:
411eda14cbcSMatt Macy panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
412eda14cbcSMatt Macy }
413eda14cbcSMatt Macy return (err);
414eda14cbcSMatt Macy }
415eda14cbcSMatt Macy
416eda14cbcSMatt Macy /*
417eda14cbcSMatt Macy * We have to release the fsid synchronously or we risk that a subsequent
418eda14cbcSMatt Macy * mount of the same dataset will fail to unique_insert the fsid. This
419eda14cbcSMatt Macy * failure would manifest itself as the fsid of this dataset changing
420eda14cbcSMatt Macy * between mounts which makes NFS clients quite unhappy.
421eda14cbcSMatt Macy */
422eda14cbcSMatt Macy static void
dsl_dataset_evict_sync(void * dbu)423eda14cbcSMatt Macy dsl_dataset_evict_sync(void *dbu)
424eda14cbcSMatt Macy {
425eda14cbcSMatt Macy dsl_dataset_t *ds = dbu;
426eda14cbcSMatt Macy
427eda14cbcSMatt Macy ASSERT(ds->ds_owner == NULL);
428eda14cbcSMatt Macy
429eda14cbcSMatt Macy unique_remove(ds->ds_fsid_guid);
430eda14cbcSMatt Macy }
431eda14cbcSMatt Macy
432eda14cbcSMatt Macy static void
dsl_dataset_evict_async(void * dbu)433eda14cbcSMatt Macy dsl_dataset_evict_async(void *dbu)
434eda14cbcSMatt Macy {
435eda14cbcSMatt Macy dsl_dataset_t *ds = dbu;
436eda14cbcSMatt Macy
437eda14cbcSMatt Macy ASSERT(ds->ds_owner == NULL);
438eda14cbcSMatt Macy
439eda14cbcSMatt Macy ds->ds_dbuf = NULL;
440eda14cbcSMatt Macy
441eda14cbcSMatt Macy if (ds->ds_objset != NULL)
442eda14cbcSMatt Macy dmu_objset_evict(ds->ds_objset);
443eda14cbcSMatt Macy
444eda14cbcSMatt Macy if (ds->ds_prev) {
445eda14cbcSMatt Macy dsl_dataset_rele(ds->ds_prev, ds);
446eda14cbcSMatt Macy ds->ds_prev = NULL;
447eda14cbcSMatt Macy }
448eda14cbcSMatt Macy
449eda14cbcSMatt Macy dsl_bookmark_fini_ds(ds);
450eda14cbcSMatt Macy
451eda14cbcSMatt Macy bplist_destroy(&ds->ds_pending_deadlist);
452eda14cbcSMatt Macy if (dsl_deadlist_is_open(&ds->ds_deadlist))
453eda14cbcSMatt Macy dsl_deadlist_close(&ds->ds_deadlist);
454eda14cbcSMatt Macy if (dsl_deadlist_is_open(&ds->ds_remap_deadlist))
455eda14cbcSMatt Macy dsl_deadlist_close(&ds->ds_remap_deadlist);
456eda14cbcSMatt Macy if (ds->ds_dir)
457eda14cbcSMatt Macy dsl_dir_async_rele(ds->ds_dir, ds);
458eda14cbcSMatt Macy
459eda14cbcSMatt Macy ASSERT(!list_link_active(&ds->ds_synced_link));
460eda14cbcSMatt Macy
461eda14cbcSMatt Macy for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
462eda14cbcSMatt Macy if (dsl_dataset_feature_is_active(ds, f))
463eda14cbcSMatt Macy unload_zfeature(ds, f);
464eda14cbcSMatt Macy }
465eda14cbcSMatt Macy
466eda14cbcSMatt Macy list_destroy(&ds->ds_prop_cbs);
467eda14cbcSMatt Macy mutex_destroy(&ds->ds_lock);
468eda14cbcSMatt Macy mutex_destroy(&ds->ds_opening_lock);
469eda14cbcSMatt Macy mutex_destroy(&ds->ds_sendstream_lock);
470eda14cbcSMatt Macy mutex_destroy(&ds->ds_remap_deadlist_lock);
471eda14cbcSMatt Macy zfs_refcount_destroy(&ds->ds_longholds);
472eda14cbcSMatt Macy rrw_destroy(&ds->ds_bp_rwlock);
473eda14cbcSMatt Macy
474eda14cbcSMatt Macy kmem_free(ds, sizeof (dsl_dataset_t));
475eda14cbcSMatt Macy }
476eda14cbcSMatt Macy
477eda14cbcSMatt Macy int
dsl_dataset_get_snapname(dsl_dataset_t * ds)478eda14cbcSMatt Macy dsl_dataset_get_snapname(dsl_dataset_t *ds)
479eda14cbcSMatt Macy {
480eda14cbcSMatt Macy dsl_dataset_phys_t *headphys;
481eda14cbcSMatt Macy int err;
482eda14cbcSMatt Macy dmu_buf_t *headdbuf;
483eda14cbcSMatt Macy dsl_pool_t *dp = ds->ds_dir->dd_pool;
484eda14cbcSMatt Macy objset_t *mos = dp->dp_meta_objset;
485eda14cbcSMatt Macy
486eda14cbcSMatt Macy if (ds->ds_snapname[0])
487eda14cbcSMatt Macy return (0);
488eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
489eda14cbcSMatt Macy return (0);
490eda14cbcSMatt Macy
491eda14cbcSMatt Macy err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
492eda14cbcSMatt Macy FTAG, &headdbuf);
493eda14cbcSMatt Macy if (err != 0)
494eda14cbcSMatt Macy return (err);
495eda14cbcSMatt Macy headphys = headdbuf->db_data;
496eda14cbcSMatt Macy err = zap_value_search(dp->dp_meta_objset,
4977a7741afSMartin Matuska headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname,
4987a7741afSMartin Matuska sizeof (ds->ds_snapname));
499eda14cbcSMatt Macy if (err != 0 && zfs_recover == B_TRUE) {
500eda14cbcSMatt Macy err = 0;
501eda14cbcSMatt Macy (void) snprintf(ds->ds_snapname, sizeof (ds->ds_snapname),
502eda14cbcSMatt Macy "SNAPOBJ=%llu-ERR=%d",
503eda14cbcSMatt Macy (unsigned long long)ds->ds_object, err);
504eda14cbcSMatt Macy }
505eda14cbcSMatt Macy dmu_buf_rele(headdbuf, FTAG);
506eda14cbcSMatt Macy return (err);
507eda14cbcSMatt Macy }
508eda14cbcSMatt Macy
509eda14cbcSMatt Macy int
dsl_dataset_snap_lookup(dsl_dataset_t * ds,const char * name,uint64_t * value)510eda14cbcSMatt Macy dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
511eda14cbcSMatt Macy {
512eda14cbcSMatt Macy objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
513eda14cbcSMatt Macy uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
514eda14cbcSMatt Macy matchtype_t mt = 0;
515eda14cbcSMatt Macy int err;
516eda14cbcSMatt Macy
517eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
518eda14cbcSMatt Macy mt = MT_NORMALIZE;
519eda14cbcSMatt Macy
520eda14cbcSMatt Macy err = zap_lookup_norm(mos, snapobj, name, 8, 1,
521eda14cbcSMatt Macy value, mt, NULL, 0, NULL);
522eda14cbcSMatt Macy if (err == ENOTSUP && (mt & MT_NORMALIZE))
523eda14cbcSMatt Macy err = zap_lookup(mos, snapobj, name, 8, 1, value);
524eda14cbcSMatt Macy return (err);
525eda14cbcSMatt Macy }
526eda14cbcSMatt Macy
527eda14cbcSMatt Macy int
dsl_dataset_snap_remove(dsl_dataset_t * ds,const char * name,dmu_tx_t * tx,boolean_t adj_cnt)528eda14cbcSMatt Macy dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
529eda14cbcSMatt Macy boolean_t adj_cnt)
530eda14cbcSMatt Macy {
531eda14cbcSMatt Macy objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
532eda14cbcSMatt Macy uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
533eda14cbcSMatt Macy matchtype_t mt = 0;
534eda14cbcSMatt Macy int err;
535eda14cbcSMatt Macy
536271171e0SMartin Matuska dsl_dir_snap_cmtime_update(ds->ds_dir, tx);
537eda14cbcSMatt Macy
538eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
539eda14cbcSMatt Macy mt = MT_NORMALIZE;
540eda14cbcSMatt Macy
541eda14cbcSMatt Macy err = zap_remove_norm(mos, snapobj, name, mt, tx);
542eda14cbcSMatt Macy if (err == ENOTSUP && (mt & MT_NORMALIZE))
543eda14cbcSMatt Macy err = zap_remove(mos, snapobj, name, tx);
544eda14cbcSMatt Macy
545eda14cbcSMatt Macy if (err == 0 && adj_cnt)
546eda14cbcSMatt Macy dsl_fs_ss_count_adjust(ds->ds_dir, -1,
547eda14cbcSMatt Macy DD_FIELD_SNAPSHOT_COUNT, tx);
548eda14cbcSMatt Macy
549eda14cbcSMatt Macy return (err);
550eda14cbcSMatt Macy }
551eda14cbcSMatt Macy
552eda14cbcSMatt Macy boolean_t
dsl_dataset_try_add_ref(dsl_pool_t * dp,dsl_dataset_t * ds,const void * tag)553a0b956f5SMartin Matuska dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, const void *tag)
554eda14cbcSMatt Macy {
555eda14cbcSMatt Macy dmu_buf_t *dbuf = ds->ds_dbuf;
556eda14cbcSMatt Macy boolean_t result = B_FALSE;
557eda14cbcSMatt Macy
558eda14cbcSMatt Macy if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
559eda14cbcSMatt Macy ds->ds_object, DMU_BONUS_BLKID, tag)) {
560eda14cbcSMatt Macy
561eda14cbcSMatt Macy if (ds == dmu_buf_get_user(dbuf))
562eda14cbcSMatt Macy result = B_TRUE;
563eda14cbcSMatt Macy else
564eda14cbcSMatt Macy dmu_buf_rele(dbuf, tag);
565eda14cbcSMatt Macy }
566eda14cbcSMatt Macy
567eda14cbcSMatt Macy return (result);
568eda14cbcSMatt Macy }
569eda14cbcSMatt Macy
570eda14cbcSMatt Macy int
dsl_dataset_hold_obj(dsl_pool_t * dp,uint64_t dsobj,const void * tag,dsl_dataset_t ** dsp)571a0b956f5SMartin Matuska dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, const void *tag,
572eda14cbcSMatt Macy dsl_dataset_t **dsp)
573eda14cbcSMatt Macy {
574eda14cbcSMatt Macy objset_t *mos = dp->dp_meta_objset;
575eda14cbcSMatt Macy dmu_buf_t *dbuf;
576eda14cbcSMatt Macy dsl_dataset_t *ds;
577eda14cbcSMatt Macy int err;
578eda14cbcSMatt Macy dmu_object_info_t doi;
579eda14cbcSMatt Macy
580eda14cbcSMatt Macy ASSERT(dsl_pool_config_held(dp));
581eda14cbcSMatt Macy
582eda14cbcSMatt Macy err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
583eda14cbcSMatt Macy if (err != 0)
584eda14cbcSMatt Macy return (err);
585eda14cbcSMatt Macy
586eda14cbcSMatt Macy /* Make sure dsobj has the correct object type. */
587eda14cbcSMatt Macy dmu_object_info_from_db(dbuf, &doi);
588eda14cbcSMatt Macy if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
589eda14cbcSMatt Macy dmu_buf_rele(dbuf, tag);
590eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
591eda14cbcSMatt Macy }
592eda14cbcSMatt Macy
593eda14cbcSMatt Macy ds = dmu_buf_get_user(dbuf);
594eda14cbcSMatt Macy if (ds == NULL) {
595eda14cbcSMatt Macy dsl_dataset_t *winner = NULL;
596eda14cbcSMatt Macy
597eda14cbcSMatt Macy ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
598eda14cbcSMatt Macy ds->ds_dbuf = dbuf;
599eda14cbcSMatt Macy ds->ds_object = dsobj;
600eda14cbcSMatt Macy ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
601eda14cbcSMatt Macy list_link_init(&ds->ds_synced_link);
602eda14cbcSMatt Macy
603eda14cbcSMatt Macy err = dsl_dir_hold_obj(dp, dsl_dataset_phys(ds)->ds_dir_obj,
604eda14cbcSMatt Macy NULL, ds, &ds->ds_dir);
605eda14cbcSMatt Macy if (err != 0) {
606eda14cbcSMatt Macy kmem_free(ds, sizeof (dsl_dataset_t));
607eda14cbcSMatt Macy dmu_buf_rele(dbuf, tag);
608eda14cbcSMatt Macy return (err);
609eda14cbcSMatt Macy }
610eda14cbcSMatt Macy
611eda14cbcSMatt Macy mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
612eda14cbcSMatt Macy mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
613eda14cbcSMatt Macy mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
614eda14cbcSMatt Macy mutex_init(&ds->ds_remap_deadlist_lock,
615eda14cbcSMatt Macy NULL, MUTEX_DEFAULT, NULL);
616eda14cbcSMatt Macy rrw_init(&ds->ds_bp_rwlock, B_FALSE);
617eda14cbcSMatt Macy zfs_refcount_create(&ds->ds_longholds);
618eda14cbcSMatt Macy
619eda14cbcSMatt Macy bplist_create(&ds->ds_pending_deadlist);
620eda14cbcSMatt Macy
621eda14cbcSMatt Macy list_create(&ds->ds_sendstreams, sizeof (dmu_sendstatus_t),
622eda14cbcSMatt Macy offsetof(dmu_sendstatus_t, dss_link));
623eda14cbcSMatt Macy
624eda14cbcSMatt Macy list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t),
625eda14cbcSMatt Macy offsetof(dsl_prop_cb_record_t, cbr_ds_node));
626eda14cbcSMatt Macy
627eda14cbcSMatt Macy if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
628eda14cbcSMatt Macy spa_feature_t f;
629eda14cbcSMatt Macy
630eda14cbcSMatt Macy for (f = 0; f < SPA_FEATURES; f++) {
631eda14cbcSMatt Macy if (!(spa_feature_table[f].fi_flags &
632eda14cbcSMatt Macy ZFEATURE_FLAG_PER_DATASET))
633eda14cbcSMatt Macy continue;
634eda14cbcSMatt Macy err = load_zfeature(mos, ds, f);
635eda14cbcSMatt Macy }
636eda14cbcSMatt Macy }
637eda14cbcSMatt Macy
638eda14cbcSMatt Macy if (!ds->ds_is_snapshot) {
639eda14cbcSMatt Macy ds->ds_snapname[0] = '\0';
640eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
641eda14cbcSMatt Macy err = dsl_dataset_hold_obj(dp,
642eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj,
643eda14cbcSMatt Macy ds, &ds->ds_prev);
644eda14cbcSMatt Macy }
645be181ee2SMartin Matuska if (err != 0)
646be181ee2SMartin Matuska goto after_dsl_bookmark_fini;
647eda14cbcSMatt Macy err = dsl_bookmark_init_ds(ds);
648eda14cbcSMatt Macy } else {
649eda14cbcSMatt Macy if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
650eda14cbcSMatt Macy err = dsl_dataset_get_snapname(ds);
651eda14cbcSMatt Macy if (err == 0 &&
652eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
653eda14cbcSMatt Macy err = zap_count(
654eda14cbcSMatt Macy ds->ds_dir->dd_pool->dp_meta_objset,
655eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_userrefs_obj,
656eda14cbcSMatt Macy &ds->ds_userrefs);
657eda14cbcSMatt Macy }
658eda14cbcSMatt Macy }
659eda14cbcSMatt Macy
660eda14cbcSMatt Macy if (err == 0 && !ds->ds_is_snapshot) {
661eda14cbcSMatt Macy err = dsl_prop_get_int_ds(ds,
662eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
663eda14cbcSMatt Macy &ds->ds_reserved);
664eda14cbcSMatt Macy if (err == 0) {
665eda14cbcSMatt Macy err = dsl_prop_get_int_ds(ds,
666eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REFQUOTA),
667eda14cbcSMatt Macy &ds->ds_quota);
668eda14cbcSMatt Macy }
669eda14cbcSMatt Macy } else {
670eda14cbcSMatt Macy ds->ds_reserved = ds->ds_quota = 0;
671eda14cbcSMatt Macy }
672eda14cbcSMatt Macy
673eda14cbcSMatt Macy if (err == 0 && ds->ds_dir->dd_crypto_obj != 0 &&
674eda14cbcSMatt Macy ds->ds_is_snapshot &&
675eda14cbcSMatt Macy zap_contains(mos, dsobj, DS_FIELD_IVSET_GUID) != 0) {
676eda14cbcSMatt Macy dp->dp_spa->spa_errata =
677eda14cbcSMatt Macy ZPOOL_ERRATA_ZOL_8308_ENCRYPTION;
678eda14cbcSMatt Macy }
679eda14cbcSMatt Macy
680eda14cbcSMatt Macy dsl_deadlist_open(&ds->ds_deadlist,
681eda14cbcSMatt Macy mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
682eda14cbcSMatt Macy uint64_t remap_deadlist_obj =
683eda14cbcSMatt Macy dsl_dataset_get_remap_deadlist_object(ds);
684eda14cbcSMatt Macy if (remap_deadlist_obj != 0) {
685eda14cbcSMatt Macy dsl_deadlist_open(&ds->ds_remap_deadlist, mos,
686eda14cbcSMatt Macy remap_deadlist_obj);
687eda14cbcSMatt Macy }
688eda14cbcSMatt Macy
689eda14cbcSMatt Macy dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync,
690eda14cbcSMatt Macy dsl_dataset_evict_async, &ds->ds_dbuf);
691eda14cbcSMatt Macy if (err == 0)
692eda14cbcSMatt Macy winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
693eda14cbcSMatt Macy
694eda14cbcSMatt Macy if (err != 0 || winner != NULL) {
695eda14cbcSMatt Macy dsl_deadlist_close(&ds->ds_deadlist);
696eda14cbcSMatt Macy if (dsl_deadlist_is_open(&ds->ds_remap_deadlist))
697eda14cbcSMatt Macy dsl_deadlist_close(&ds->ds_remap_deadlist);
698eda14cbcSMatt Macy dsl_bookmark_fini_ds(ds);
699be181ee2SMartin Matuska after_dsl_bookmark_fini:
700eda14cbcSMatt Macy if (ds->ds_prev)
701eda14cbcSMatt Macy dsl_dataset_rele(ds->ds_prev, ds);
702eda14cbcSMatt Macy dsl_dir_rele(ds->ds_dir, ds);
703eda14cbcSMatt Macy for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
704eda14cbcSMatt Macy if (dsl_dataset_feature_is_active(ds, f))
705eda14cbcSMatt Macy unload_zfeature(ds, f);
706eda14cbcSMatt Macy }
707eda14cbcSMatt Macy
708eda14cbcSMatt Macy list_destroy(&ds->ds_prop_cbs);
709eda14cbcSMatt Macy list_destroy(&ds->ds_sendstreams);
710be181ee2SMartin Matuska bplist_destroy(&ds->ds_pending_deadlist);
711eda14cbcSMatt Macy mutex_destroy(&ds->ds_lock);
712eda14cbcSMatt Macy mutex_destroy(&ds->ds_opening_lock);
713eda14cbcSMatt Macy mutex_destroy(&ds->ds_sendstream_lock);
714eda14cbcSMatt Macy mutex_destroy(&ds->ds_remap_deadlist_lock);
715eda14cbcSMatt Macy zfs_refcount_destroy(&ds->ds_longholds);
716eda14cbcSMatt Macy rrw_destroy(&ds->ds_bp_rwlock);
717eda14cbcSMatt Macy kmem_free(ds, sizeof (dsl_dataset_t));
718eda14cbcSMatt Macy if (err != 0) {
719eda14cbcSMatt Macy dmu_buf_rele(dbuf, tag);
720eda14cbcSMatt Macy return (err);
721eda14cbcSMatt Macy }
722eda14cbcSMatt Macy ds = winner;
723eda14cbcSMatt Macy } else {
724eda14cbcSMatt Macy ds->ds_fsid_guid =
725eda14cbcSMatt Macy unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
726eda14cbcSMatt Macy if (ds->ds_fsid_guid !=
727eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_fsid_guid) {
728eda14cbcSMatt Macy zfs_dbgmsg("ds_fsid_guid changed from "
729eda14cbcSMatt Macy "%llx to %llx for pool %s dataset id %llu",
730eda14cbcSMatt Macy (long long)
731eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_fsid_guid,
732eda14cbcSMatt Macy (long long)ds->ds_fsid_guid,
733eda14cbcSMatt Macy spa_name(dp->dp_spa),
73433b8c039SMartin Matuska (u_longlong_t)dsobj);
735eda14cbcSMatt Macy }
736eda14cbcSMatt Macy }
737eda14cbcSMatt Macy }
738eda14cbcSMatt Macy
739eda14cbcSMatt Macy ASSERT3P(ds->ds_dbuf, ==, dbuf);
740eda14cbcSMatt Macy ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
741eda14cbcSMatt Macy ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
742eda14cbcSMatt Macy spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
743eda14cbcSMatt Macy dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
744eda14cbcSMatt Macy *dsp = ds;
745eda14cbcSMatt Macy
746eda14cbcSMatt Macy return (0);
747eda14cbcSMatt Macy }
748eda14cbcSMatt Macy
749eda14cbcSMatt Macy int
dsl_dataset_create_key_mapping(dsl_dataset_t * ds)750eda14cbcSMatt Macy dsl_dataset_create_key_mapping(dsl_dataset_t *ds)
751eda14cbcSMatt Macy {
752eda14cbcSMatt Macy dsl_dir_t *dd = ds->ds_dir;
753eda14cbcSMatt Macy
754eda14cbcSMatt Macy if (dd->dd_crypto_obj == 0)
755eda14cbcSMatt Macy return (0);
756eda14cbcSMatt Macy
757eda14cbcSMatt Macy return (spa_keystore_create_mapping(dd->dd_pool->dp_spa,
758eda14cbcSMatt Macy ds, ds, &ds->ds_key_mapping));
759eda14cbcSMatt Macy }
760eda14cbcSMatt Macy
761eda14cbcSMatt Macy int
dsl_dataset_hold_obj_flags(dsl_pool_t * dp,uint64_t dsobj,ds_hold_flags_t flags,const void * tag,dsl_dataset_t ** dsp)762eda14cbcSMatt Macy dsl_dataset_hold_obj_flags(dsl_pool_t *dp, uint64_t dsobj,
763a0b956f5SMartin Matuska ds_hold_flags_t flags, const void *tag, dsl_dataset_t **dsp)
764eda14cbcSMatt Macy {
765eda14cbcSMatt Macy int err;
766eda14cbcSMatt Macy
767eda14cbcSMatt Macy err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
768eda14cbcSMatt Macy if (err != 0)
769eda14cbcSMatt Macy return (err);
770eda14cbcSMatt Macy
771eda14cbcSMatt Macy ASSERT3P(*dsp, !=, NULL);
772eda14cbcSMatt Macy
773eda14cbcSMatt Macy if (flags & DS_HOLD_FLAG_DECRYPT) {
774eda14cbcSMatt Macy err = dsl_dataset_create_key_mapping(*dsp);
775eda14cbcSMatt Macy if (err != 0)
776eda14cbcSMatt Macy dsl_dataset_rele(*dsp, tag);
777eda14cbcSMatt Macy }
778eda14cbcSMatt Macy
779eda14cbcSMatt Macy return (err);
780eda14cbcSMatt Macy }
781eda14cbcSMatt Macy
782eda14cbcSMatt Macy int
dsl_dataset_hold_flags(dsl_pool_t * dp,const char * name,ds_hold_flags_t flags,const void * tag,dsl_dataset_t ** dsp)783eda14cbcSMatt Macy dsl_dataset_hold_flags(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
784a0b956f5SMartin Matuska const void *tag, dsl_dataset_t **dsp)
785eda14cbcSMatt Macy {
786eda14cbcSMatt Macy dsl_dir_t *dd;
787eda14cbcSMatt Macy const char *snapname;
788eda14cbcSMatt Macy uint64_t obj;
789eda14cbcSMatt Macy int err = 0;
790eda14cbcSMatt Macy dsl_dataset_t *ds;
791eda14cbcSMatt Macy
792eda14cbcSMatt Macy err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
793eda14cbcSMatt Macy if (err != 0)
794eda14cbcSMatt Macy return (err);
795eda14cbcSMatt Macy
796eda14cbcSMatt Macy ASSERT(dsl_pool_config_held(dp));
797eda14cbcSMatt Macy obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
798eda14cbcSMatt Macy if (obj != 0)
799eda14cbcSMatt Macy err = dsl_dataset_hold_obj_flags(dp, obj, flags, tag, &ds);
800eda14cbcSMatt Macy else
801eda14cbcSMatt Macy err = SET_ERROR(ENOENT);
802eda14cbcSMatt Macy
803eda14cbcSMatt Macy /* we may be looking for a snapshot */
804eda14cbcSMatt Macy if (err == 0 && snapname != NULL) {
805eda14cbcSMatt Macy dsl_dataset_t *snap_ds;
806eda14cbcSMatt Macy
807eda14cbcSMatt Macy if (*snapname++ != '@') {
808eda14cbcSMatt Macy dsl_dataset_rele_flags(ds, flags, tag);
809eda14cbcSMatt Macy dsl_dir_rele(dd, FTAG);
810eda14cbcSMatt Macy return (SET_ERROR(ENOENT));
811eda14cbcSMatt Macy }
812eda14cbcSMatt Macy
813eda14cbcSMatt Macy dprintf("looking for snapshot '%s'\n", snapname);
814eda14cbcSMatt Macy err = dsl_dataset_snap_lookup(ds, snapname, &obj);
815eda14cbcSMatt Macy if (err == 0) {
816eda14cbcSMatt Macy err = dsl_dataset_hold_obj_flags(dp, obj, flags, tag,
817eda14cbcSMatt Macy &snap_ds);
818eda14cbcSMatt Macy }
819eda14cbcSMatt Macy dsl_dataset_rele_flags(ds, flags, tag);
820eda14cbcSMatt Macy
821eda14cbcSMatt Macy if (err == 0) {
822eda14cbcSMatt Macy mutex_enter(&snap_ds->ds_lock);
823eda14cbcSMatt Macy if (snap_ds->ds_snapname[0] == 0)
824eda14cbcSMatt Macy (void) strlcpy(snap_ds->ds_snapname, snapname,
825eda14cbcSMatt Macy sizeof (snap_ds->ds_snapname));
826eda14cbcSMatt Macy mutex_exit(&snap_ds->ds_lock);
827eda14cbcSMatt Macy ds = snap_ds;
828eda14cbcSMatt Macy }
829eda14cbcSMatt Macy }
830eda14cbcSMatt Macy if (err == 0)
831eda14cbcSMatt Macy *dsp = ds;
832eda14cbcSMatt Macy dsl_dir_rele(dd, FTAG);
833eda14cbcSMatt Macy return (err);
834eda14cbcSMatt Macy }
835eda14cbcSMatt Macy
836eda14cbcSMatt Macy int
dsl_dataset_hold(dsl_pool_t * dp,const char * name,const void * tag,dsl_dataset_t ** dsp)837a0b956f5SMartin Matuska dsl_dataset_hold(dsl_pool_t *dp, const char *name, const void *tag,
838eda14cbcSMatt Macy dsl_dataset_t **dsp)
839eda14cbcSMatt Macy {
840eda14cbcSMatt Macy return (dsl_dataset_hold_flags(dp, name, 0, tag, dsp));
841eda14cbcSMatt Macy }
842eda14cbcSMatt Macy
843eda14cbcSMatt Macy static int
dsl_dataset_own_obj_impl(dsl_pool_t * dp,uint64_t dsobj,ds_hold_flags_t flags,const void * tag,boolean_t override,dsl_dataset_t ** dsp)844eda14cbcSMatt Macy dsl_dataset_own_obj_impl(dsl_pool_t *dp, uint64_t dsobj, ds_hold_flags_t flags,
845a0b956f5SMartin Matuska const void *tag, boolean_t override, dsl_dataset_t **dsp)
846eda14cbcSMatt Macy {
847eda14cbcSMatt Macy int err = dsl_dataset_hold_obj_flags(dp, dsobj, flags, tag, dsp);
848eda14cbcSMatt Macy if (err != 0)
849eda14cbcSMatt Macy return (err);
850eda14cbcSMatt Macy if (!dsl_dataset_tryown(*dsp, tag, override)) {
851eda14cbcSMatt Macy dsl_dataset_rele_flags(*dsp, flags, tag);
852eda14cbcSMatt Macy *dsp = NULL;
853eda14cbcSMatt Macy return (SET_ERROR(EBUSY));
854eda14cbcSMatt Macy }
855eda14cbcSMatt Macy return (0);
856eda14cbcSMatt Macy }
857eda14cbcSMatt Macy
858eda14cbcSMatt Macy
859eda14cbcSMatt Macy int
dsl_dataset_own_obj(dsl_pool_t * dp,uint64_t dsobj,ds_hold_flags_t flags,const void * tag,dsl_dataset_t ** dsp)860eda14cbcSMatt Macy dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, ds_hold_flags_t flags,
861a0b956f5SMartin Matuska const void *tag, dsl_dataset_t **dsp)
862eda14cbcSMatt Macy {
863eda14cbcSMatt Macy return (dsl_dataset_own_obj_impl(dp, dsobj, flags, tag, B_FALSE, dsp));
864eda14cbcSMatt Macy }
865eda14cbcSMatt Macy
866eda14cbcSMatt Macy int
dsl_dataset_own_obj_force(dsl_pool_t * dp,uint64_t dsobj,ds_hold_flags_t flags,const void * tag,dsl_dataset_t ** dsp)867eda14cbcSMatt Macy dsl_dataset_own_obj_force(dsl_pool_t *dp, uint64_t dsobj,
868a0b956f5SMartin Matuska ds_hold_flags_t flags, const void *tag, dsl_dataset_t **dsp)
869eda14cbcSMatt Macy {
870eda14cbcSMatt Macy return (dsl_dataset_own_obj_impl(dp, dsobj, flags, tag, B_TRUE, dsp));
871eda14cbcSMatt Macy }
872eda14cbcSMatt Macy
873eda14cbcSMatt Macy static int
dsl_dataset_own_impl(dsl_pool_t * dp,const char * name,ds_hold_flags_t flags,const void * tag,boolean_t override,dsl_dataset_t ** dsp)874eda14cbcSMatt Macy dsl_dataset_own_impl(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
875a0b956f5SMartin Matuska const void *tag, boolean_t override, dsl_dataset_t **dsp)
876eda14cbcSMatt Macy {
877eda14cbcSMatt Macy int err = dsl_dataset_hold_flags(dp, name, flags, tag, dsp);
878eda14cbcSMatt Macy if (err != 0)
879eda14cbcSMatt Macy return (err);
880eda14cbcSMatt Macy if (!dsl_dataset_tryown(*dsp, tag, override)) {
881eda14cbcSMatt Macy dsl_dataset_rele_flags(*dsp, flags, tag);
882eda14cbcSMatt Macy return (SET_ERROR(EBUSY));
883eda14cbcSMatt Macy }
884eda14cbcSMatt Macy return (0);
885eda14cbcSMatt Macy }
886eda14cbcSMatt Macy
887eda14cbcSMatt Macy int
dsl_dataset_own_force(dsl_pool_t * dp,const char * name,ds_hold_flags_t flags,const void * tag,dsl_dataset_t ** dsp)888eda14cbcSMatt Macy dsl_dataset_own_force(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
889a0b956f5SMartin Matuska const void *tag, dsl_dataset_t **dsp)
890eda14cbcSMatt Macy {
891eda14cbcSMatt Macy return (dsl_dataset_own_impl(dp, name, flags, tag, B_TRUE, dsp));
892eda14cbcSMatt Macy }
893eda14cbcSMatt Macy
894eda14cbcSMatt Macy int
dsl_dataset_own(dsl_pool_t * dp,const char * name,ds_hold_flags_t flags,const void * tag,dsl_dataset_t ** dsp)895eda14cbcSMatt Macy dsl_dataset_own(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
896a0b956f5SMartin Matuska const void *tag, dsl_dataset_t **dsp)
897eda14cbcSMatt Macy {
898eda14cbcSMatt Macy return (dsl_dataset_own_impl(dp, name, flags, tag, B_FALSE, dsp));
899eda14cbcSMatt Macy }
900eda14cbcSMatt Macy
901eda14cbcSMatt Macy /*
902eda14cbcSMatt Macy * See the comment above dsl_pool_hold() for details. In summary, a long
903eda14cbcSMatt Macy * hold is used to prevent destruction of a dataset while the pool hold
904eda14cbcSMatt Macy * is dropped, allowing other concurrent operations (e.g. spa_sync()).
905eda14cbcSMatt Macy *
906eda14cbcSMatt Macy * The dataset and pool must be held when this function is called. After it
907eda14cbcSMatt Macy * is called, the pool hold may be released while the dataset is still held
908eda14cbcSMatt Macy * and accessed.
909eda14cbcSMatt Macy */
910eda14cbcSMatt Macy void
dsl_dataset_long_hold(dsl_dataset_t * ds,const void * tag)911e92ffd9bSMartin Matuska dsl_dataset_long_hold(dsl_dataset_t *ds, const void *tag)
912eda14cbcSMatt Macy {
913eda14cbcSMatt Macy ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
914eda14cbcSMatt Macy (void) zfs_refcount_add(&ds->ds_longholds, tag);
915eda14cbcSMatt Macy }
916eda14cbcSMatt Macy
917eda14cbcSMatt Macy void
dsl_dataset_long_rele(dsl_dataset_t * ds,const void * tag)918e92ffd9bSMartin Matuska dsl_dataset_long_rele(dsl_dataset_t *ds, const void *tag)
919eda14cbcSMatt Macy {
920eda14cbcSMatt Macy (void) zfs_refcount_remove(&ds->ds_longholds, tag);
921eda14cbcSMatt Macy }
922eda14cbcSMatt Macy
923eda14cbcSMatt Macy /* Return B_TRUE if there are any long holds on this dataset. */
924eda14cbcSMatt Macy boolean_t
dsl_dataset_long_held(dsl_dataset_t * ds)925eda14cbcSMatt Macy dsl_dataset_long_held(dsl_dataset_t *ds)
926eda14cbcSMatt Macy {
927eda14cbcSMatt Macy return (!zfs_refcount_is_zero(&ds->ds_longholds));
928eda14cbcSMatt Macy }
929eda14cbcSMatt Macy
930eda14cbcSMatt Macy void
dsl_dataset_name(dsl_dataset_t * ds,char * name)931eda14cbcSMatt Macy dsl_dataset_name(dsl_dataset_t *ds, char *name)
932eda14cbcSMatt Macy {
933eda14cbcSMatt Macy if (ds == NULL) {
934eda14cbcSMatt Macy (void) strlcpy(name, "mos", ZFS_MAX_DATASET_NAME_LEN);
935eda14cbcSMatt Macy } else {
936eda14cbcSMatt Macy dsl_dir_name(ds->ds_dir, name);
937eda14cbcSMatt Macy VERIFY0(dsl_dataset_get_snapname(ds));
938eda14cbcSMatt Macy if (ds->ds_snapname[0]) {
939eda14cbcSMatt Macy VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN),
940eda14cbcSMatt Macy <, ZFS_MAX_DATASET_NAME_LEN);
941eda14cbcSMatt Macy /*
942eda14cbcSMatt Macy * We use a "recursive" mutex so that we
943eda14cbcSMatt Macy * can call dprintf_ds() with ds_lock held.
944eda14cbcSMatt Macy */
945eda14cbcSMatt Macy if (!MUTEX_HELD(&ds->ds_lock)) {
946eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
947eda14cbcSMatt Macy VERIFY3U(strlcat(name, ds->ds_snapname,
948eda14cbcSMatt Macy ZFS_MAX_DATASET_NAME_LEN), <,
949eda14cbcSMatt Macy ZFS_MAX_DATASET_NAME_LEN);
950eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
951eda14cbcSMatt Macy } else {
952eda14cbcSMatt Macy VERIFY3U(strlcat(name, ds->ds_snapname,
953eda14cbcSMatt Macy ZFS_MAX_DATASET_NAME_LEN), <,
954eda14cbcSMatt Macy ZFS_MAX_DATASET_NAME_LEN);
955eda14cbcSMatt Macy }
956eda14cbcSMatt Macy }
957eda14cbcSMatt Macy }
958eda14cbcSMatt Macy }
959eda14cbcSMatt Macy
960eda14cbcSMatt Macy int
dsl_dataset_namelen(dsl_dataset_t * ds)961eda14cbcSMatt Macy dsl_dataset_namelen(dsl_dataset_t *ds)
962eda14cbcSMatt Macy {
963eda14cbcSMatt Macy VERIFY0(dsl_dataset_get_snapname(ds));
964eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
965eda14cbcSMatt Macy int len = strlen(ds->ds_snapname);
966eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
967eda14cbcSMatt Macy /* add '@' if ds is a snap */
968eda14cbcSMatt Macy if (len > 0)
969eda14cbcSMatt Macy len++;
970eda14cbcSMatt Macy len += dsl_dir_namelen(ds->ds_dir);
971eda14cbcSMatt Macy return (len);
972eda14cbcSMatt Macy }
973eda14cbcSMatt Macy
974eda14cbcSMatt Macy void
dsl_dataset_rele(dsl_dataset_t * ds,const void * tag)975a0b956f5SMartin Matuska dsl_dataset_rele(dsl_dataset_t *ds, const void *tag)
976eda14cbcSMatt Macy {
977eda14cbcSMatt Macy dmu_buf_rele(ds->ds_dbuf, tag);
978eda14cbcSMatt Macy }
979eda14cbcSMatt Macy
980eda14cbcSMatt Macy void
dsl_dataset_remove_key_mapping(dsl_dataset_t * ds)981eda14cbcSMatt Macy dsl_dataset_remove_key_mapping(dsl_dataset_t *ds)
982eda14cbcSMatt Macy {
983eda14cbcSMatt Macy dsl_dir_t *dd = ds->ds_dir;
984eda14cbcSMatt Macy
985eda14cbcSMatt Macy if (dd == NULL || dd->dd_crypto_obj == 0)
986eda14cbcSMatt Macy return;
987eda14cbcSMatt Macy
988eda14cbcSMatt Macy (void) spa_keystore_remove_mapping(dd->dd_pool->dp_spa,
989eda14cbcSMatt Macy ds->ds_object, ds);
990eda14cbcSMatt Macy }
991eda14cbcSMatt Macy
992eda14cbcSMatt Macy void
dsl_dataset_rele_flags(dsl_dataset_t * ds,ds_hold_flags_t flags,const void * tag)993a0b956f5SMartin Matuska dsl_dataset_rele_flags(dsl_dataset_t *ds, ds_hold_flags_t flags,
994a0b956f5SMartin Matuska const void *tag)
995eda14cbcSMatt Macy {
996eda14cbcSMatt Macy if (flags & DS_HOLD_FLAG_DECRYPT)
997eda14cbcSMatt Macy dsl_dataset_remove_key_mapping(ds);
998eda14cbcSMatt Macy
999eda14cbcSMatt Macy dsl_dataset_rele(ds, tag);
1000eda14cbcSMatt Macy }
1001eda14cbcSMatt Macy
1002eda14cbcSMatt Macy void
dsl_dataset_disown(dsl_dataset_t * ds,ds_hold_flags_t flags,const void * tag)1003a0b956f5SMartin Matuska dsl_dataset_disown(dsl_dataset_t *ds, ds_hold_flags_t flags, const void *tag)
1004eda14cbcSMatt Macy {
1005eda14cbcSMatt Macy ASSERT3P(ds->ds_owner, ==, tag);
1006eda14cbcSMatt Macy ASSERT(ds->ds_dbuf != NULL);
1007eda14cbcSMatt Macy
1008eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
1009eda14cbcSMatt Macy ds->ds_owner = NULL;
1010eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
1011eda14cbcSMatt Macy dsl_dataset_long_rele(ds, tag);
1012eda14cbcSMatt Macy dsl_dataset_rele_flags(ds, flags, tag);
1013eda14cbcSMatt Macy }
1014eda14cbcSMatt Macy
1015eda14cbcSMatt Macy boolean_t
dsl_dataset_tryown(dsl_dataset_t * ds,const void * tag,boolean_t override)1016a0b956f5SMartin Matuska dsl_dataset_tryown(dsl_dataset_t *ds, const void *tag, boolean_t override)
1017eda14cbcSMatt Macy {
1018eda14cbcSMatt Macy boolean_t gotit = FALSE;
1019eda14cbcSMatt Macy
1020eda14cbcSMatt Macy ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1021eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
1022eda14cbcSMatt Macy if (ds->ds_owner == NULL && (override || !(DS_IS_INCONSISTENT(ds) ||
1023eda14cbcSMatt Macy (dsl_dataset_feature_is_active(ds,
1024eda14cbcSMatt Macy SPA_FEATURE_REDACTED_DATASETS) &&
1025eda14cbcSMatt Macy !zfs_allow_redacted_dataset_mount)))) {
1026eda14cbcSMatt Macy ds->ds_owner = tag;
1027eda14cbcSMatt Macy dsl_dataset_long_hold(ds, tag);
1028eda14cbcSMatt Macy gotit = TRUE;
1029eda14cbcSMatt Macy }
1030eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
1031eda14cbcSMatt Macy return (gotit);
1032eda14cbcSMatt Macy }
1033eda14cbcSMatt Macy
1034eda14cbcSMatt Macy boolean_t
dsl_dataset_has_owner(dsl_dataset_t * ds)1035eda14cbcSMatt Macy dsl_dataset_has_owner(dsl_dataset_t *ds)
1036eda14cbcSMatt Macy {
1037eda14cbcSMatt Macy boolean_t rv;
1038eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
1039eda14cbcSMatt Macy rv = (ds->ds_owner != NULL);
1040eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
1041eda14cbcSMatt Macy return (rv);
1042eda14cbcSMatt Macy }
1043eda14cbcSMatt Macy
10442a58b312SMartin Matuska static boolean_t
zfeature_active(spa_feature_t f,void * arg)1045eda14cbcSMatt Macy zfeature_active(spa_feature_t f, void *arg)
1046eda14cbcSMatt Macy {
1047eda14cbcSMatt Macy switch (spa_feature_table[f].fi_type) {
1048eda14cbcSMatt Macy case ZFEATURE_TYPE_BOOLEAN: {
1049eda14cbcSMatt Macy boolean_t val = (boolean_t)(uintptr_t)arg;
1050eda14cbcSMatt Macy ASSERT(val == B_FALSE || val == B_TRUE);
1051eda14cbcSMatt Macy return (val);
1052eda14cbcSMatt Macy }
1053eda14cbcSMatt Macy case ZFEATURE_TYPE_UINT64_ARRAY:
1054eda14cbcSMatt Macy /*
1055eda14cbcSMatt Macy * In this case, arg is a uint64_t array. The feature is active
1056eda14cbcSMatt Macy * if the array is non-null.
1057eda14cbcSMatt Macy */
1058eda14cbcSMatt Macy return (arg != NULL);
1059eda14cbcSMatt Macy default:
1060eda14cbcSMatt Macy panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
1061eda14cbcSMatt Macy return (B_FALSE);
1062eda14cbcSMatt Macy }
1063eda14cbcSMatt Macy }
1064eda14cbcSMatt Macy
1065eda14cbcSMatt Macy boolean_t
dsl_dataset_feature_is_active(dsl_dataset_t * ds,spa_feature_t f)1066eda14cbcSMatt Macy dsl_dataset_feature_is_active(dsl_dataset_t *ds, spa_feature_t f)
1067eda14cbcSMatt Macy {
1068eda14cbcSMatt Macy return (zfeature_active(f, ds->ds_feature[f]));
1069eda14cbcSMatt Macy }
1070eda14cbcSMatt Macy
1071eda14cbcSMatt Macy /*
1072eda14cbcSMatt Macy * The buffers passed out by this function are references to internal buffers;
1073eda14cbcSMatt Macy * they should not be freed by callers of this function, and they should not be
1074eda14cbcSMatt Macy * used after the dataset has been released.
1075eda14cbcSMatt Macy */
1076eda14cbcSMatt Macy boolean_t
dsl_dataset_get_uint64_array_feature(dsl_dataset_t * ds,spa_feature_t f,uint64_t * outlength,uint64_t ** outp)1077eda14cbcSMatt Macy dsl_dataset_get_uint64_array_feature(dsl_dataset_t *ds, spa_feature_t f,
1078eda14cbcSMatt Macy uint64_t *outlength, uint64_t **outp)
1079eda14cbcSMatt Macy {
1080eda14cbcSMatt Macy VERIFY(spa_feature_table[f].fi_type & ZFEATURE_TYPE_UINT64_ARRAY);
1081eda14cbcSMatt Macy if (!dsl_dataset_feature_is_active(ds, f)) {
1082eda14cbcSMatt Macy return (B_FALSE);
1083eda14cbcSMatt Macy }
1084eda14cbcSMatt Macy struct feature_type_uint64_array_arg *ftuaa = ds->ds_feature[f];
1085eda14cbcSMatt Macy *outp = ftuaa->array;
1086eda14cbcSMatt Macy *outlength = ftuaa->length;
1087eda14cbcSMatt Macy return (B_TRUE);
1088eda14cbcSMatt Macy }
1089eda14cbcSMatt Macy
1090eda14cbcSMatt Macy void
dsl_dataset_activate_feature(uint64_t dsobj,spa_feature_t f,void * arg,dmu_tx_t * tx)1091eda14cbcSMatt Macy dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, void *arg,
1092eda14cbcSMatt Macy dmu_tx_t *tx)
1093eda14cbcSMatt Macy {
1094eda14cbcSMatt Macy spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1095eda14cbcSMatt Macy objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
1096eda14cbcSMatt Macy uint64_t zero = 0;
1097eda14cbcSMatt Macy
1098eda14cbcSMatt Macy VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
1099eda14cbcSMatt Macy
1100eda14cbcSMatt Macy spa_feature_incr(spa, f, tx);
1101eda14cbcSMatt Macy dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
1102eda14cbcSMatt Macy
1103eda14cbcSMatt Macy switch (spa_feature_table[f].fi_type) {
1104eda14cbcSMatt Macy case ZFEATURE_TYPE_BOOLEAN:
1105eda14cbcSMatt Macy ASSERT3S((boolean_t)(uintptr_t)arg, ==, B_TRUE);
1106eda14cbcSMatt Macy VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
1107eda14cbcSMatt Macy sizeof (zero), 1, &zero, tx));
1108eda14cbcSMatt Macy break;
1109eda14cbcSMatt Macy case ZFEATURE_TYPE_UINT64_ARRAY:
1110eda14cbcSMatt Macy {
1111eda14cbcSMatt Macy struct feature_type_uint64_array_arg *ftuaa = arg;
1112eda14cbcSMatt Macy VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
1113eda14cbcSMatt Macy sizeof (uint64_t), ftuaa->length, ftuaa->array, tx));
1114eda14cbcSMatt Macy break;
1115eda14cbcSMatt Macy }
1116eda14cbcSMatt Macy default:
1117eda14cbcSMatt Macy panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
1118eda14cbcSMatt Macy }
1119eda14cbcSMatt Macy }
1120eda14cbcSMatt Macy
1121eda14cbcSMatt Macy static void
dsl_dataset_deactivate_feature_impl(dsl_dataset_t * ds,spa_feature_t f,dmu_tx_t * tx)1122eda14cbcSMatt Macy dsl_dataset_deactivate_feature_impl(dsl_dataset_t *ds, spa_feature_t f,
1123eda14cbcSMatt Macy dmu_tx_t *tx)
1124eda14cbcSMatt Macy {
1125eda14cbcSMatt Macy spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1126eda14cbcSMatt Macy objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
1127eda14cbcSMatt Macy uint64_t dsobj = ds->ds_object;
1128eda14cbcSMatt Macy
1129eda14cbcSMatt Macy VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
1130eda14cbcSMatt Macy
1131eda14cbcSMatt Macy VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
1132eda14cbcSMatt Macy spa_feature_decr(spa, f, tx);
1133eda14cbcSMatt Macy ds->ds_feature[f] = NULL;
1134eda14cbcSMatt Macy }
1135eda14cbcSMatt Macy
1136eda14cbcSMatt Macy void
dsl_dataset_deactivate_feature(dsl_dataset_t * ds,spa_feature_t f,dmu_tx_t * tx)1137eda14cbcSMatt Macy dsl_dataset_deactivate_feature(dsl_dataset_t *ds, spa_feature_t f, dmu_tx_t *tx)
1138eda14cbcSMatt Macy {
1139eda14cbcSMatt Macy unload_zfeature(ds, f);
1140eda14cbcSMatt Macy dsl_dataset_deactivate_feature_impl(ds, f, tx);
1141eda14cbcSMatt Macy }
1142eda14cbcSMatt Macy
1143eda14cbcSMatt Macy uint64_t
dsl_dataset_create_sync_dd(dsl_dir_t * dd,dsl_dataset_t * origin,dsl_crypto_params_t * dcp,uint64_t flags,dmu_tx_t * tx)1144eda14cbcSMatt Macy dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
1145eda14cbcSMatt Macy dsl_crypto_params_t *dcp, uint64_t flags, dmu_tx_t *tx)
1146eda14cbcSMatt Macy {
1147eda14cbcSMatt Macy dsl_pool_t *dp = dd->dd_pool;
1148eda14cbcSMatt Macy dmu_buf_t *dbuf;
1149eda14cbcSMatt Macy dsl_dataset_phys_t *dsphys;
1150eda14cbcSMatt Macy uint64_t dsobj;
1151eda14cbcSMatt Macy objset_t *mos = dp->dp_meta_objset;
1152eda14cbcSMatt Macy
1153eda14cbcSMatt Macy if (origin == NULL)
1154eda14cbcSMatt Macy origin = dp->dp_origin_snap;
1155eda14cbcSMatt Macy
1156eda14cbcSMatt Macy ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
1157eda14cbcSMatt Macy ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
1158eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx));
1159eda14cbcSMatt Macy ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1160eda14cbcSMatt Macy
1161eda14cbcSMatt Macy dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1162eda14cbcSMatt Macy DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1163eda14cbcSMatt Macy VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1164eda14cbcSMatt Macy dmu_buf_will_dirty(dbuf, tx);
1165eda14cbcSMatt Macy dsphys = dbuf->db_data;
1166da5137abSMartin Matuska memset(dsphys, 0, sizeof (dsl_dataset_phys_t));
1167eda14cbcSMatt Macy dsphys->ds_dir_obj = dd->dd_object;
1168eda14cbcSMatt Macy dsphys->ds_flags = flags;
1169eda14cbcSMatt Macy dsphys->ds_fsid_guid = unique_create();
1170eda14cbcSMatt Macy (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1171eda14cbcSMatt Macy sizeof (dsphys->ds_guid));
1172eda14cbcSMatt Macy dsphys->ds_snapnames_zapobj =
1173eda14cbcSMatt Macy zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
1174eda14cbcSMatt Macy DMU_OT_NONE, 0, tx);
1175eda14cbcSMatt Macy dsphys->ds_creation_time = gethrestime_sec();
1176eda14cbcSMatt Macy dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
1177eda14cbcSMatt Macy
1178eda14cbcSMatt Macy if (origin == NULL) {
1179eda14cbcSMatt Macy dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
1180eda14cbcSMatt Macy } else {
1181eda14cbcSMatt Macy dsl_dataset_t *ohds; /* head of the origin snapshot */
1182eda14cbcSMatt Macy
1183eda14cbcSMatt Macy dsphys->ds_prev_snap_obj = origin->ds_object;
1184eda14cbcSMatt Macy dsphys->ds_prev_snap_txg =
1185eda14cbcSMatt Macy dsl_dataset_phys(origin)->ds_creation_txg;
1186eda14cbcSMatt Macy dsphys->ds_referenced_bytes =
1187eda14cbcSMatt Macy dsl_dataset_phys(origin)->ds_referenced_bytes;
1188eda14cbcSMatt Macy dsphys->ds_compressed_bytes =
1189eda14cbcSMatt Macy dsl_dataset_phys(origin)->ds_compressed_bytes;
1190eda14cbcSMatt Macy dsphys->ds_uncompressed_bytes =
1191eda14cbcSMatt Macy dsl_dataset_phys(origin)->ds_uncompressed_bytes;
1192eda14cbcSMatt Macy rrw_enter(&origin->ds_bp_rwlock, RW_READER, FTAG);
1193eda14cbcSMatt Macy dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
1194eda14cbcSMatt Macy rrw_exit(&origin->ds_bp_rwlock, FTAG);
1195eda14cbcSMatt Macy
1196eda14cbcSMatt Macy /*
1197eda14cbcSMatt Macy * Inherit flags that describe the dataset's contents
1198eda14cbcSMatt Macy * (INCONSISTENT) or properties (Case Insensitive).
1199eda14cbcSMatt Macy */
1200eda14cbcSMatt Macy dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
1201eda14cbcSMatt Macy (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
1202eda14cbcSMatt Macy
1203eda14cbcSMatt Macy for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1204eda14cbcSMatt Macy if (zfeature_active(f, origin->ds_feature[f])) {
1205eda14cbcSMatt Macy dsl_dataset_activate_feature(dsobj, f,
1206eda14cbcSMatt Macy origin->ds_feature[f], tx);
1207eda14cbcSMatt Macy }
1208eda14cbcSMatt Macy }
1209eda14cbcSMatt Macy
1210eda14cbcSMatt Macy dmu_buf_will_dirty(origin->ds_dbuf, tx);
1211eda14cbcSMatt Macy dsl_dataset_phys(origin)->ds_num_children++;
1212eda14cbcSMatt Macy
1213eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj(dp,
1214eda14cbcSMatt Macy dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
1215eda14cbcSMatt Macy FTAG, &ohds));
1216eda14cbcSMatt Macy dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
1217eda14cbcSMatt Macy dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
1218eda14cbcSMatt Macy dsl_dataset_rele(ohds, FTAG);
1219eda14cbcSMatt Macy
1220eda14cbcSMatt Macy if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
1221eda14cbcSMatt Macy if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
1222eda14cbcSMatt Macy dsl_dataset_phys(origin)->ds_next_clones_obj =
1223eda14cbcSMatt Macy zap_create(mos,
1224eda14cbcSMatt Macy DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
1225eda14cbcSMatt Macy }
1226eda14cbcSMatt Macy VERIFY0(zap_add_int(mos,
1227eda14cbcSMatt Macy dsl_dataset_phys(origin)->ds_next_clones_obj,
1228eda14cbcSMatt Macy dsobj, tx));
1229eda14cbcSMatt Macy }
1230eda14cbcSMatt Macy
1231eda14cbcSMatt Macy dmu_buf_will_dirty(dd->dd_dbuf, tx);
1232eda14cbcSMatt Macy dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
1233eda14cbcSMatt Macy if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
1234eda14cbcSMatt Macy if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
1235eda14cbcSMatt Macy dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
1236eda14cbcSMatt Macy dsl_dir_phys(origin->ds_dir)->dd_clones =
1237eda14cbcSMatt Macy zap_create(mos,
1238eda14cbcSMatt Macy DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
1239eda14cbcSMatt Macy }
1240eda14cbcSMatt Macy VERIFY0(zap_add_int(mos,
1241eda14cbcSMatt Macy dsl_dir_phys(origin->ds_dir)->dd_clones,
1242eda14cbcSMatt Macy dsobj, tx));
1243eda14cbcSMatt Macy }
1244eda14cbcSMatt Macy }
1245eda14cbcSMatt Macy
1246eda14cbcSMatt Macy /* handle encryption */
1247eda14cbcSMatt Macy dsl_dataset_create_crypt_sync(dsobj, dd, origin, dcp, tx);
1248eda14cbcSMatt Macy
1249eda14cbcSMatt Macy if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1250eda14cbcSMatt Macy dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1251eda14cbcSMatt Macy
1252eda14cbcSMatt Macy dmu_buf_rele(dbuf, FTAG);
1253eda14cbcSMatt Macy
1254eda14cbcSMatt Macy dmu_buf_will_dirty(dd->dd_dbuf, tx);
1255eda14cbcSMatt Macy dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
1256eda14cbcSMatt Macy
1257eda14cbcSMatt Macy return (dsobj);
1258eda14cbcSMatt Macy }
1259eda14cbcSMatt Macy
1260eda14cbcSMatt Macy static void
dsl_dataset_zero_zil(dsl_dataset_t * ds,dmu_tx_t * tx)1261eda14cbcSMatt Macy dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
1262eda14cbcSMatt Macy {
1263eda14cbcSMatt Macy objset_t *os;
1264eda14cbcSMatt Macy
1265eda14cbcSMatt Macy VERIFY0(dmu_objset_from_ds(ds, &os));
1266da5137abSMartin Matuska if (memcmp(&os->os_zil_header, &zero_zil, sizeof (zero_zil)) != 0) {
1267eda14cbcSMatt Macy dsl_pool_t *dp = ds->ds_dir->dd_pool;
1268eda14cbcSMatt Macy zio_t *zio;
1269eda14cbcSMatt Macy
1270da5137abSMartin Matuska memset(&os->os_zil_header, 0, sizeof (os->os_zil_header));
1271eda14cbcSMatt Macy if (os->os_encrypted)
1272eda14cbcSMatt Macy os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;
1273eda14cbcSMatt Macy
1274eda14cbcSMatt Macy zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1275eda14cbcSMatt Macy dsl_dataset_sync(ds, zio, tx);
1276eda14cbcSMatt Macy VERIFY0(zio_wait(zio));
1277eda14cbcSMatt Macy dsl_dataset_sync_done(ds, tx);
1278eda14cbcSMatt Macy }
1279eda14cbcSMatt Macy }
1280eda14cbcSMatt Macy
1281eda14cbcSMatt Macy uint64_t
dsl_dataset_create_sync(dsl_dir_t * pdd,const char * lastname,dsl_dataset_t * origin,uint64_t flags,cred_t * cr,dsl_crypto_params_t * dcp,dmu_tx_t * tx)1282eda14cbcSMatt Macy dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
1283eda14cbcSMatt Macy dsl_dataset_t *origin, uint64_t flags, cred_t *cr,
1284eda14cbcSMatt Macy dsl_crypto_params_t *dcp, dmu_tx_t *tx)
1285eda14cbcSMatt Macy {
1286eda14cbcSMatt Macy dsl_pool_t *dp = pdd->dd_pool;
1287eda14cbcSMatt Macy uint64_t dsobj, ddobj;
1288eda14cbcSMatt Macy dsl_dir_t *dd;
1289eda14cbcSMatt Macy
1290eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx));
1291eda14cbcSMatt Macy ASSERT(lastname[0] != '@');
1292eda14cbcSMatt Macy /*
1293eda14cbcSMatt Macy * Filesystems will eventually have their origin set to dp_origin_snap,
1294eda14cbcSMatt Macy * but that's taken care of in dsl_dataset_create_sync_dd. When
1295eda14cbcSMatt Macy * creating a filesystem, this function is called with origin equal to
1296eda14cbcSMatt Macy * NULL.
1297eda14cbcSMatt Macy */
1298eda14cbcSMatt Macy if (origin != NULL)
1299eda14cbcSMatt Macy ASSERT3P(origin, !=, dp->dp_origin_snap);
1300eda14cbcSMatt Macy
1301eda14cbcSMatt Macy ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
1302eda14cbcSMatt Macy VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
1303eda14cbcSMatt Macy
1304eda14cbcSMatt Macy dsobj = dsl_dataset_create_sync_dd(dd, origin, dcp,
1305eda14cbcSMatt Macy flags & ~DS_CREATE_FLAG_NODIRTY, tx);
1306eda14cbcSMatt Macy
1307eda14cbcSMatt Macy dsl_deleg_set_create_perms(dd, tx, cr);
1308eda14cbcSMatt Macy
1309eda14cbcSMatt Macy /*
1310eda14cbcSMatt Macy * If we are creating a clone and the livelist feature is enabled,
1311eda14cbcSMatt Macy * add the entry DD_FIELD_LIVELIST to ZAP.
1312eda14cbcSMatt Macy */
1313eda14cbcSMatt Macy if (origin != NULL &&
1314eda14cbcSMatt Macy spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LIVELIST)) {
1315eda14cbcSMatt Macy objset_t *mos = dd->dd_pool->dp_meta_objset;
1316eda14cbcSMatt Macy dsl_dir_zapify(dd, tx);
1317eda14cbcSMatt Macy uint64_t obj = dsl_deadlist_alloc(mos, tx);
1318eda14cbcSMatt Macy VERIFY0(zap_add(mos, dd->dd_object, DD_FIELD_LIVELIST,
1319eda14cbcSMatt Macy sizeof (uint64_t), 1, &obj, tx));
1320eda14cbcSMatt Macy spa_feature_incr(dp->dp_spa, SPA_FEATURE_LIVELIST, tx);
1321eda14cbcSMatt Macy }
1322eda14cbcSMatt Macy
1323eda14cbcSMatt Macy /*
1324eda14cbcSMatt Macy * Since we're creating a new node we know it's a leaf, so we can
1325eda14cbcSMatt Macy * initialize the counts if the limit feature is active.
1326eda14cbcSMatt Macy */
1327eda14cbcSMatt Macy if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
1328eda14cbcSMatt Macy uint64_t cnt = 0;
1329eda14cbcSMatt Macy objset_t *os = dd->dd_pool->dp_meta_objset;
1330eda14cbcSMatt Macy
1331eda14cbcSMatt Macy dsl_dir_zapify(dd, tx);
1332eda14cbcSMatt Macy VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
1333eda14cbcSMatt Macy sizeof (cnt), 1, &cnt, tx));
1334eda14cbcSMatt Macy VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
1335eda14cbcSMatt Macy sizeof (cnt), 1, &cnt, tx));
1336eda14cbcSMatt Macy }
1337eda14cbcSMatt Macy
1338eda14cbcSMatt Macy dsl_dir_rele(dd, FTAG);
1339eda14cbcSMatt Macy
1340eda14cbcSMatt Macy /*
1341eda14cbcSMatt Macy * If we are creating a clone, make sure we zero out any stale
1342eda14cbcSMatt Macy * data from the origin snapshots zil header.
1343eda14cbcSMatt Macy */
1344eda14cbcSMatt Macy if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
1345eda14cbcSMatt Macy dsl_dataset_t *ds;
1346eda14cbcSMatt Macy
1347eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1348eda14cbcSMatt Macy dsl_dataset_zero_zil(ds, tx);
1349eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
1350eda14cbcSMatt Macy }
1351eda14cbcSMatt Macy
1352eda14cbcSMatt Macy return (dsobj);
1353eda14cbcSMatt Macy }
1354eda14cbcSMatt Macy
1355eda14cbcSMatt Macy /*
1356eda14cbcSMatt Macy * The unique space in the head dataset can be calculated by subtracting
1357eda14cbcSMatt Macy * the space used in the most recent snapshot, that is still being used
1358eda14cbcSMatt Macy * in this file system, from the space currently in use. To figure out
1359eda14cbcSMatt Macy * the space in the most recent snapshot still in use, we need to take
1360eda14cbcSMatt Macy * the total space used in the snapshot and subtract out the space that
1361eda14cbcSMatt Macy * has been freed up since the snapshot was taken.
1362eda14cbcSMatt Macy */
1363eda14cbcSMatt Macy void
dsl_dataset_recalc_head_uniq(dsl_dataset_t * ds)1364eda14cbcSMatt Macy dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
1365eda14cbcSMatt Macy {
1366eda14cbcSMatt Macy uint64_t mrs_used;
1367eda14cbcSMatt Macy uint64_t dlused, dlcomp, dluncomp;
1368eda14cbcSMatt Macy
1369eda14cbcSMatt Macy ASSERT(!ds->ds_is_snapshot);
1370eda14cbcSMatt Macy
1371eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
1372eda14cbcSMatt Macy mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
1373eda14cbcSMatt Macy else
1374eda14cbcSMatt Macy mrs_used = 0;
1375eda14cbcSMatt Macy
1376eda14cbcSMatt Macy dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
1377eda14cbcSMatt Macy
1378eda14cbcSMatt Macy ASSERT3U(dlused, <=, mrs_used);
1379eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_unique_bytes =
1380eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
1381eda14cbcSMatt Macy
1382eda14cbcSMatt Macy if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
1383eda14cbcSMatt Macy SPA_VERSION_UNIQUE_ACCURATE)
1384eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1385eda14cbcSMatt Macy }
1386eda14cbcSMatt Macy
1387eda14cbcSMatt Macy void
dsl_dataset_remove_from_next_clones(dsl_dataset_t * ds,uint64_t obj,dmu_tx_t * tx)1388eda14cbcSMatt Macy dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
1389eda14cbcSMatt Macy dmu_tx_t *tx)
1390eda14cbcSMatt Macy {
1391eda14cbcSMatt Macy objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1392eda14cbcSMatt Macy uint64_t count __maybe_unused;
1393eda14cbcSMatt Macy int err;
1394eda14cbcSMatt Macy
1395eda14cbcSMatt Macy ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
1396eda14cbcSMatt Macy err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1397eda14cbcSMatt Macy obj, tx);
1398eda14cbcSMatt Macy /*
1399eda14cbcSMatt Macy * The err should not be ENOENT, but a bug in a previous version
1400eda14cbcSMatt Macy * of the code could cause upgrade_clones_cb() to not set
1401eda14cbcSMatt Macy * ds_next_snap_obj when it should, leading to a missing entry.
1402eda14cbcSMatt Macy * If we knew that the pool was created after
1403eda14cbcSMatt Macy * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
1404eda14cbcSMatt Macy * ENOENT. However, at least we can check that we don't have
1405eda14cbcSMatt Macy * too many entries in the next_clones_obj even after failing to
1406eda14cbcSMatt Macy * remove this one.
1407eda14cbcSMatt Macy */
1408eda14cbcSMatt Macy if (err != ENOENT)
1409eda14cbcSMatt Macy VERIFY0(err);
1410eda14cbcSMatt Macy ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1411eda14cbcSMatt Macy &count));
1412eda14cbcSMatt Macy ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
1413eda14cbcSMatt Macy }
1414eda14cbcSMatt Macy
1415eda14cbcSMatt Macy
1416eda14cbcSMatt Macy blkptr_t *
dsl_dataset_get_blkptr(dsl_dataset_t * ds)1417eda14cbcSMatt Macy dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1418eda14cbcSMatt Macy {
1419eda14cbcSMatt Macy return (&dsl_dataset_phys(ds)->ds_bp);
1420eda14cbcSMatt Macy }
1421eda14cbcSMatt Macy
1422eda14cbcSMatt Macy spa_t *
dsl_dataset_get_spa(dsl_dataset_t * ds)1423eda14cbcSMatt Macy dsl_dataset_get_spa(dsl_dataset_t *ds)
1424eda14cbcSMatt Macy {
1425eda14cbcSMatt Macy return (ds->ds_dir->dd_pool->dp_spa);
1426eda14cbcSMatt Macy }
1427eda14cbcSMatt Macy
1428eda14cbcSMatt Macy void
dsl_dataset_dirty(dsl_dataset_t * ds,dmu_tx_t * tx)1429eda14cbcSMatt Macy dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1430eda14cbcSMatt Macy {
1431eda14cbcSMatt Macy dsl_pool_t *dp;
1432eda14cbcSMatt Macy
1433eda14cbcSMatt Macy if (ds == NULL) /* this is the meta-objset */
1434eda14cbcSMatt Macy return;
1435eda14cbcSMatt Macy
1436eda14cbcSMatt Macy ASSERT(ds->ds_objset != NULL);
1437eda14cbcSMatt Macy
1438eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
1439eda14cbcSMatt Macy panic("dirtying snapshot!");
1440eda14cbcSMatt Macy
1441eda14cbcSMatt Macy /* Must not dirty a dataset in the same txg where it got snapshotted. */
1442eda14cbcSMatt Macy ASSERT3U(tx->tx_txg, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
1443eda14cbcSMatt Macy
1444eda14cbcSMatt Macy dp = ds->ds_dir->dd_pool;
1445eda14cbcSMatt Macy if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1446eda14cbcSMatt Macy objset_t *os = ds->ds_objset;
1447eda14cbcSMatt Macy
1448eda14cbcSMatt Macy /* up the hold count until we can be written out */
1449eda14cbcSMatt Macy dmu_buf_add_ref(ds->ds_dbuf, ds);
1450eda14cbcSMatt Macy
1451eda14cbcSMatt Macy /* if this dataset is encrypted, grab a reference to the DCK */
1452eda14cbcSMatt Macy if (ds->ds_dir->dd_crypto_obj != 0 &&
1453eda14cbcSMatt Macy !os->os_raw_receive &&
1454eda14cbcSMatt Macy !os->os_next_write_raw[tx->tx_txg & TXG_MASK]) {
1455eda14cbcSMatt Macy ASSERT3P(ds->ds_key_mapping, !=, NULL);
1456eda14cbcSMatt Macy key_mapping_add_ref(ds->ds_key_mapping, ds);
1457eda14cbcSMatt Macy }
1458eda14cbcSMatt Macy }
1459eda14cbcSMatt Macy }
1460eda14cbcSMatt Macy
1461eda14cbcSMatt Macy static int
dsl_dataset_snapshot_reserve_space(dsl_dataset_t * ds,dmu_tx_t * tx)1462eda14cbcSMatt Macy dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1463eda14cbcSMatt Macy {
1464eda14cbcSMatt Macy uint64_t asize;
1465eda14cbcSMatt Macy
1466eda14cbcSMatt Macy if (!dmu_tx_is_syncing(tx))
1467eda14cbcSMatt Macy return (0);
1468eda14cbcSMatt Macy
1469eda14cbcSMatt Macy /*
1470eda14cbcSMatt Macy * If there's an fs-only reservation, any blocks that might become
1471eda14cbcSMatt Macy * owned by the snapshot dataset must be accommodated by space
1472eda14cbcSMatt Macy * outside of the reservation.
1473eda14cbcSMatt Macy */
1474eda14cbcSMatt Macy ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
1475eda14cbcSMatt Macy asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
1476eda14cbcSMatt Macy if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
1477eda14cbcSMatt Macy return (SET_ERROR(ENOSPC));
1478eda14cbcSMatt Macy
1479eda14cbcSMatt Macy /*
1480eda14cbcSMatt Macy * Propagate any reserved space for this snapshot to other
1481eda14cbcSMatt Macy * snapshot checks in this sync group.
1482eda14cbcSMatt Macy */
1483eda14cbcSMatt Macy if (asize > 0)
1484eda14cbcSMatt Macy dsl_dir_willuse_space(ds->ds_dir, asize, tx);
1485eda14cbcSMatt Macy
1486eda14cbcSMatt Macy return (0);
1487eda14cbcSMatt Macy }
1488eda14cbcSMatt Macy
1489eda14cbcSMatt Macy int
dsl_dataset_snapshot_check_impl(dsl_dataset_t * ds,const char * snapname,dmu_tx_t * tx,boolean_t recv,uint64_t cnt,cred_t * cr,proc_t * proc)1490eda14cbcSMatt Macy dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1491eda14cbcSMatt Macy dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr, proc_t *proc)
1492eda14cbcSMatt Macy {
1493eda14cbcSMatt Macy int error;
1494eda14cbcSMatt Macy uint64_t value;
1495eda14cbcSMatt Macy
1496eda14cbcSMatt Macy ds->ds_trysnap_txg = tx->tx_txg;
1497eda14cbcSMatt Macy
1498eda14cbcSMatt Macy if (!dmu_tx_is_syncing(tx))
1499eda14cbcSMatt Macy return (0);
1500eda14cbcSMatt Macy
1501eda14cbcSMatt Macy /*
1502eda14cbcSMatt Macy * We don't allow multiple snapshots of the same txg. If there
1503eda14cbcSMatt Macy * is already one, try again.
1504eda14cbcSMatt Macy */
1505eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
1506eda14cbcSMatt Macy return (SET_ERROR(EAGAIN));
1507eda14cbcSMatt Macy
1508eda14cbcSMatt Macy /*
1509eda14cbcSMatt Macy * Check for conflicting snapshot name.
1510eda14cbcSMatt Macy */
1511eda14cbcSMatt Macy error = dsl_dataset_snap_lookup(ds, snapname, &value);
1512eda14cbcSMatt Macy if (error == 0)
1513eda14cbcSMatt Macy return (SET_ERROR(EEXIST));
1514eda14cbcSMatt Macy if (error != ENOENT)
1515eda14cbcSMatt Macy return (error);
1516eda14cbcSMatt Macy
1517eda14cbcSMatt Macy /*
1518eda14cbcSMatt Macy * We don't allow taking snapshots of inconsistent datasets, such as
1519eda14cbcSMatt Macy * those into which we are currently receiving. However, if we are
1520eda14cbcSMatt Macy * creating this snapshot as part of a receive, this check will be
1521eda14cbcSMatt Macy * executed atomically with respect to the completion of the receive
1522eda14cbcSMatt Macy * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1523eda14cbcSMatt Macy * case we ignore this, knowing it will be fixed up for us shortly in
1524eda14cbcSMatt Macy * dmu_recv_end_sync().
1525eda14cbcSMatt Macy */
1526eda14cbcSMatt Macy if (!recv && DS_IS_INCONSISTENT(ds))
1527eda14cbcSMatt Macy return (SET_ERROR(EBUSY));
1528eda14cbcSMatt Macy
1529eda14cbcSMatt Macy /*
1530eda14cbcSMatt Macy * Skip the check for temporary snapshots or if we have already checked
1531eda14cbcSMatt Macy * the counts in dsl_dataset_snapshot_check. This means we really only
1532eda14cbcSMatt Macy * check the count here when we're receiving a stream.
1533eda14cbcSMatt Macy */
1534eda14cbcSMatt Macy if (cnt != 0 && cr != NULL) {
1535eda14cbcSMatt Macy error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1536eda14cbcSMatt Macy ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr, proc);
1537eda14cbcSMatt Macy if (error != 0)
1538eda14cbcSMatt Macy return (error);
1539eda14cbcSMatt Macy }
1540eda14cbcSMatt Macy
1541eda14cbcSMatt Macy error = dsl_dataset_snapshot_reserve_space(ds, tx);
1542eda14cbcSMatt Macy if (error != 0)
1543eda14cbcSMatt Macy return (error);
1544eda14cbcSMatt Macy
1545eda14cbcSMatt Macy return (0);
1546eda14cbcSMatt Macy }
1547eda14cbcSMatt Macy
1548eda14cbcSMatt Macy int
dsl_dataset_snapshot_check(void * arg,dmu_tx_t * tx)1549eda14cbcSMatt Macy dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1550eda14cbcSMatt Macy {
1551eda14cbcSMatt Macy dsl_dataset_snapshot_arg_t *ddsa = arg;
1552eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
1553eda14cbcSMatt Macy nvpair_t *pair;
1554eda14cbcSMatt Macy int rv = 0;
1555eda14cbcSMatt Macy
1556eda14cbcSMatt Macy /*
1557eda14cbcSMatt Macy * Pre-compute how many total new snapshots will be created for each
1558eda14cbcSMatt Macy * level in the tree and below. This is needed for validating the
1559eda14cbcSMatt Macy * snapshot limit when either taking a recursive snapshot or when
1560eda14cbcSMatt Macy * taking multiple snapshots.
1561eda14cbcSMatt Macy *
1562eda14cbcSMatt Macy * The problem is that the counts are not actually adjusted when
1563eda14cbcSMatt Macy * we are checking, only when we finally sync. For a single snapshot,
1564eda14cbcSMatt Macy * this is easy, the count will increase by 1 at each node up the tree,
1565eda14cbcSMatt Macy * but its more complicated for the recursive/multiple snapshot case.
1566eda14cbcSMatt Macy *
1567eda14cbcSMatt Macy * The dsl_fs_ss_limit_check function does recursively check the count
1568eda14cbcSMatt Macy * at each level up the tree but since it is validating each snapshot
1569eda14cbcSMatt Macy * independently we need to be sure that we are validating the complete
1570eda14cbcSMatt Macy * count for the entire set of snapshots. We do this by rolling up the
1571eda14cbcSMatt Macy * counts for each component of the name into an nvlist and then
1572eda14cbcSMatt Macy * checking each of those cases with the aggregated count.
1573eda14cbcSMatt Macy *
1574eda14cbcSMatt Macy * This approach properly handles not only the recursive snapshot
1575eda14cbcSMatt Macy * case (where we get all of those on the ddsa_snaps list) but also
1576eda14cbcSMatt Macy * the sibling case (e.g. snapshot a/b and a/c so that we will also
1577eda14cbcSMatt Macy * validate the limit on 'a' using a count of 2).
1578eda14cbcSMatt Macy *
1579eda14cbcSMatt Macy * We validate the snapshot names in the third loop and only report
1580eda14cbcSMatt Macy * name errors once.
1581eda14cbcSMatt Macy */
1582eda14cbcSMatt Macy if (dmu_tx_is_syncing(tx)) {
1583eda14cbcSMatt Macy char *nm;
1584eda14cbcSMatt Macy nvlist_t *cnt_track = NULL;
1585eda14cbcSMatt Macy cnt_track = fnvlist_alloc();
1586eda14cbcSMatt Macy
1587eda14cbcSMatt Macy nm = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1588eda14cbcSMatt Macy
1589eda14cbcSMatt Macy /* Rollup aggregated counts into the cnt_track list */
1590eda14cbcSMatt Macy for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1591eda14cbcSMatt Macy pair != NULL;
1592eda14cbcSMatt Macy pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1593eda14cbcSMatt Macy char *pdelim;
1594eda14cbcSMatt Macy uint64_t val;
1595eda14cbcSMatt Macy
1596eda14cbcSMatt Macy (void) strlcpy(nm, nvpair_name(pair), MAXPATHLEN);
1597eda14cbcSMatt Macy pdelim = strchr(nm, '@');
1598eda14cbcSMatt Macy if (pdelim == NULL)
1599eda14cbcSMatt Macy continue;
1600eda14cbcSMatt Macy *pdelim = '\0';
1601eda14cbcSMatt Macy
1602eda14cbcSMatt Macy do {
1603eda14cbcSMatt Macy if (nvlist_lookup_uint64(cnt_track, nm,
1604eda14cbcSMatt Macy &val) == 0) {
1605eda14cbcSMatt Macy /* update existing entry */
1606eda14cbcSMatt Macy fnvlist_add_uint64(cnt_track, nm,
1607eda14cbcSMatt Macy val + 1);
1608eda14cbcSMatt Macy } else {
1609eda14cbcSMatt Macy /* add to list */
1610eda14cbcSMatt Macy fnvlist_add_uint64(cnt_track, nm, 1);
1611eda14cbcSMatt Macy }
1612eda14cbcSMatt Macy
1613eda14cbcSMatt Macy pdelim = strrchr(nm, '/');
1614eda14cbcSMatt Macy if (pdelim != NULL)
1615eda14cbcSMatt Macy *pdelim = '\0';
1616eda14cbcSMatt Macy } while (pdelim != NULL);
1617eda14cbcSMatt Macy }
1618eda14cbcSMatt Macy
1619eda14cbcSMatt Macy kmem_free(nm, MAXPATHLEN);
1620eda14cbcSMatt Macy
1621eda14cbcSMatt Macy /* Check aggregated counts at each level */
1622eda14cbcSMatt Macy for (pair = nvlist_next_nvpair(cnt_track, NULL);
1623eda14cbcSMatt Macy pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1624eda14cbcSMatt Macy int error = 0;
16252a58b312SMartin Matuska const char *name;
1626eda14cbcSMatt Macy uint64_t cnt = 0;
1627eda14cbcSMatt Macy dsl_dataset_t *ds;
1628eda14cbcSMatt Macy
1629eda14cbcSMatt Macy name = nvpair_name(pair);
1630eda14cbcSMatt Macy cnt = fnvpair_value_uint64(pair);
1631eda14cbcSMatt Macy ASSERT(cnt > 0);
1632eda14cbcSMatt Macy
1633eda14cbcSMatt Macy error = dsl_dataset_hold(dp, name, FTAG, &ds);
1634eda14cbcSMatt Macy if (error == 0) {
1635eda14cbcSMatt Macy error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1636eda14cbcSMatt Macy ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1637eda14cbcSMatt Macy ddsa->ddsa_cr, ddsa->ddsa_proc);
1638eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
1639eda14cbcSMatt Macy }
1640eda14cbcSMatt Macy
1641eda14cbcSMatt Macy if (error != 0) {
1642eda14cbcSMatt Macy if (ddsa->ddsa_errors != NULL)
1643eda14cbcSMatt Macy fnvlist_add_int32(ddsa->ddsa_errors,
1644eda14cbcSMatt Macy name, error);
1645eda14cbcSMatt Macy rv = error;
1646eda14cbcSMatt Macy /* only report one error for this check */
1647eda14cbcSMatt Macy break;
1648eda14cbcSMatt Macy }
1649eda14cbcSMatt Macy }
1650eda14cbcSMatt Macy nvlist_free(cnt_track);
1651eda14cbcSMatt Macy }
1652eda14cbcSMatt Macy
1653eda14cbcSMatt Macy for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1654eda14cbcSMatt Macy pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1655eda14cbcSMatt Macy int error = 0;
1656eda14cbcSMatt Macy dsl_dataset_t *ds;
16572a58b312SMartin Matuska const char *name, *atp = NULL;
1658eda14cbcSMatt Macy char dsname[ZFS_MAX_DATASET_NAME_LEN];
1659eda14cbcSMatt Macy
1660eda14cbcSMatt Macy name = nvpair_name(pair);
1661eda14cbcSMatt Macy if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN)
1662eda14cbcSMatt Macy error = SET_ERROR(ENAMETOOLONG);
1663eda14cbcSMatt Macy if (error == 0) {
1664eda14cbcSMatt Macy atp = strchr(name, '@');
1665eda14cbcSMatt Macy if (atp == NULL)
1666eda14cbcSMatt Macy error = SET_ERROR(EINVAL);
1667eda14cbcSMatt Macy if (error == 0)
1668eda14cbcSMatt Macy (void) strlcpy(dsname, name, atp - name + 1);
1669eda14cbcSMatt Macy }
1670eda14cbcSMatt Macy if (error == 0)
1671eda14cbcSMatt Macy error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1672eda14cbcSMatt Macy if (error == 0) {
1673eda14cbcSMatt Macy /* passing 0/NULL skips dsl_fs_ss_limit_check */
1674eda14cbcSMatt Macy error = dsl_dataset_snapshot_check_impl(ds,
1675eda14cbcSMatt Macy atp + 1, tx, B_FALSE, 0, NULL, NULL);
1676eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
1677eda14cbcSMatt Macy }
1678eda14cbcSMatt Macy
1679eda14cbcSMatt Macy if (error != 0) {
1680eda14cbcSMatt Macy if (ddsa->ddsa_errors != NULL) {
1681eda14cbcSMatt Macy fnvlist_add_int32(ddsa->ddsa_errors,
1682eda14cbcSMatt Macy name, error);
1683eda14cbcSMatt Macy }
1684eda14cbcSMatt Macy rv = error;
1685eda14cbcSMatt Macy }
1686eda14cbcSMatt Macy }
1687eda14cbcSMatt Macy
1688eda14cbcSMatt Macy return (rv);
1689eda14cbcSMatt Macy }
1690eda14cbcSMatt Macy
1691eda14cbcSMatt Macy void
dsl_dataset_snapshot_sync_impl(dsl_dataset_t * ds,const char * snapname,dmu_tx_t * tx)1692eda14cbcSMatt Macy dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1693eda14cbcSMatt Macy dmu_tx_t *tx)
1694eda14cbcSMatt Macy {
1695eda14cbcSMatt Macy dsl_pool_t *dp = ds->ds_dir->dd_pool;
1696eda14cbcSMatt Macy dmu_buf_t *dbuf;
1697eda14cbcSMatt Macy dsl_dataset_phys_t *dsphys;
1698eda14cbcSMatt Macy uint64_t dsobj, crtxg;
1699eda14cbcSMatt Macy objset_t *mos = dp->dp_meta_objset;
1700eda14cbcSMatt Macy objset_t *os __maybe_unused;
1701eda14cbcSMatt Macy
1702eda14cbcSMatt Macy ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1703eda14cbcSMatt Macy
1704eda14cbcSMatt Macy /*
1705eda14cbcSMatt Macy * If we are on an old pool, the zil must not be active, in which
1706eda14cbcSMatt Macy * case it will be zeroed. Usually zil_suspend() accomplishes this.
1707eda14cbcSMatt Macy */
1708eda14cbcSMatt Macy ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1709eda14cbcSMatt Macy dmu_objset_from_ds(ds, &os) != 0 ||
1710da5137abSMartin Matuska memcmp(&os->os_phys->os_zil_header, &zero_zil,
1711eda14cbcSMatt Macy sizeof (zero_zil)) == 0);
1712eda14cbcSMatt Macy
1713eda14cbcSMatt Macy /* Should not snapshot a dirty dataset. */
1714eda14cbcSMatt Macy ASSERT(!txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
1715eda14cbcSMatt Macy ds, tx->tx_txg));
1716eda14cbcSMatt Macy
1717eda14cbcSMatt Macy dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1718eda14cbcSMatt Macy
1719eda14cbcSMatt Macy /*
1720eda14cbcSMatt Macy * The origin's ds_creation_txg has to be < TXG_INITIAL
1721eda14cbcSMatt Macy */
1722eda14cbcSMatt Macy if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1723eda14cbcSMatt Macy crtxg = 1;
1724eda14cbcSMatt Macy else
1725eda14cbcSMatt Macy crtxg = tx->tx_txg;
1726eda14cbcSMatt Macy
1727eda14cbcSMatt Macy dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1728eda14cbcSMatt Macy DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1729eda14cbcSMatt Macy VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1730eda14cbcSMatt Macy dmu_buf_will_dirty(dbuf, tx);
1731eda14cbcSMatt Macy dsphys = dbuf->db_data;
1732da5137abSMartin Matuska memset(dsphys, 0, sizeof (dsl_dataset_phys_t));
1733eda14cbcSMatt Macy dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1734eda14cbcSMatt Macy dsphys->ds_fsid_guid = unique_create();
1735eda14cbcSMatt Macy (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1736eda14cbcSMatt Macy sizeof (dsphys->ds_guid));
1737eda14cbcSMatt Macy dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1738eda14cbcSMatt Macy dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1739eda14cbcSMatt Macy dsphys->ds_next_snap_obj = ds->ds_object;
1740eda14cbcSMatt Macy dsphys->ds_num_children = 1;
1741eda14cbcSMatt Macy dsphys->ds_creation_time = gethrestime_sec();
1742eda14cbcSMatt Macy dsphys->ds_creation_txg = crtxg;
1743eda14cbcSMatt Macy dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
1744eda14cbcSMatt Macy dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
1745eda14cbcSMatt Macy dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
1746eda14cbcSMatt Macy dsphys->ds_uncompressed_bytes =
1747eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1748eda14cbcSMatt Macy dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
1749eda14cbcSMatt Macy rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1750eda14cbcSMatt Macy dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
1751eda14cbcSMatt Macy rrw_exit(&ds->ds_bp_rwlock, FTAG);
1752eda14cbcSMatt Macy dmu_buf_rele(dbuf, FTAG);
1753eda14cbcSMatt Macy
1754eda14cbcSMatt Macy for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1755eda14cbcSMatt Macy if (zfeature_active(f, ds->ds_feature[f])) {
1756eda14cbcSMatt Macy dsl_dataset_activate_feature(dsobj, f,
1757eda14cbcSMatt Macy ds->ds_feature[f], tx);
1758eda14cbcSMatt Macy }
1759eda14cbcSMatt Macy }
1760eda14cbcSMatt Macy
1761eda14cbcSMatt Macy ASSERT3U(ds->ds_prev != 0, ==,
1762eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
1763eda14cbcSMatt Macy if (ds->ds_prev) {
1764eda14cbcSMatt Macy uint64_t next_clones_obj =
1765eda14cbcSMatt Macy dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
1766eda14cbcSMatt Macy ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1767eda14cbcSMatt Macy ds->ds_object ||
1768eda14cbcSMatt Macy dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
1769eda14cbcSMatt Macy if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1770eda14cbcSMatt Macy ds->ds_object) {
1771eda14cbcSMatt Macy dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1772eda14cbcSMatt Macy ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
1773eda14cbcSMatt Macy dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
1774eda14cbcSMatt Macy dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
1775eda14cbcSMatt Macy } else if (next_clones_obj != 0) {
1776eda14cbcSMatt Macy dsl_dataset_remove_from_next_clones(ds->ds_prev,
1777eda14cbcSMatt Macy dsphys->ds_next_snap_obj, tx);
1778eda14cbcSMatt Macy VERIFY0(zap_add_int(mos,
1779eda14cbcSMatt Macy next_clones_obj, dsobj, tx));
1780eda14cbcSMatt Macy }
1781eda14cbcSMatt Macy }
1782eda14cbcSMatt Macy
1783eda14cbcSMatt Macy /*
1784eda14cbcSMatt Macy * If we have a reference-reservation on this dataset, we will
1785eda14cbcSMatt Macy * need to increase the amount of refreservation being charged
1786eda14cbcSMatt Macy * since our unique space is going to zero.
1787eda14cbcSMatt Macy */
1788eda14cbcSMatt Macy if (ds->ds_reserved) {
1789eda14cbcSMatt Macy int64_t delta;
1790eda14cbcSMatt Macy ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1791eda14cbcSMatt Macy delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
1792eda14cbcSMatt Macy ds->ds_reserved);
1793eda14cbcSMatt Macy dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1794eda14cbcSMatt Macy delta, 0, 0, tx);
1795eda14cbcSMatt Macy }
1796eda14cbcSMatt Macy
1797eda14cbcSMatt Macy dmu_buf_will_dirty(ds->ds_dbuf, tx);
1798eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_deadlist_obj =
1799eda14cbcSMatt Macy dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
1800eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
1801eda14cbcSMatt Macy dsl_deadlist_close(&ds->ds_deadlist);
1802eda14cbcSMatt Macy dsl_deadlist_open(&ds->ds_deadlist, mos,
1803eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_deadlist_obj);
1804eda14cbcSMatt Macy dsl_deadlist_add_key(&ds->ds_deadlist,
1805eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
1806eda14cbcSMatt Macy dsl_bookmark_snapshotted(ds, tx);
1807eda14cbcSMatt Macy
1808eda14cbcSMatt Macy if (dsl_dataset_remap_deadlist_exists(ds)) {
1809eda14cbcSMatt Macy uint64_t remap_deadlist_obj =
1810eda14cbcSMatt Macy dsl_dataset_get_remap_deadlist_object(ds);
1811eda14cbcSMatt Macy /*
1812eda14cbcSMatt Macy * Move the remap_deadlist to the snapshot. The head
1813eda14cbcSMatt Macy * will create a new remap deadlist on demand, from
1814eda14cbcSMatt Macy * dsl_dataset_block_remapped().
1815eda14cbcSMatt Macy */
1816eda14cbcSMatt Macy dsl_dataset_unset_remap_deadlist_object(ds, tx);
1817eda14cbcSMatt Macy dsl_deadlist_close(&ds->ds_remap_deadlist);
1818eda14cbcSMatt Macy
1819eda14cbcSMatt Macy dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
1820eda14cbcSMatt Macy VERIFY0(zap_add(mos, dsobj, DS_FIELD_REMAP_DEADLIST,
1821eda14cbcSMatt Macy sizeof (remap_deadlist_obj), 1, &remap_deadlist_obj, tx));
1822eda14cbcSMatt Macy }
1823eda14cbcSMatt Macy
1824eda14cbcSMatt Macy /*
1825eda14cbcSMatt Macy * Create a ivset guid for this snapshot if the dataset is
1826eda14cbcSMatt Macy * encrypted. This may be overridden by a raw receive. A
1827eda14cbcSMatt Macy * previous implementation of this code did not have this
1828eda14cbcSMatt Macy * field as part of the on-disk format for ZFS encryption
1829eda14cbcSMatt Macy * (see errata #4). As part of the remediation for this
1830eda14cbcSMatt Macy * issue, we ask the user to enable the bookmark_v2 feature
1831eda14cbcSMatt Macy * which is now a dependency of the encryption feature. We
1832eda14cbcSMatt Macy * use this as a heuristic to determine when the user has
1833eda14cbcSMatt Macy * elected to correct any datasets created with the old code.
1834eda14cbcSMatt Macy * As a result, we only do this step if the bookmark_v2
1835eda14cbcSMatt Macy * feature is enabled, which limits the number of states a
1836eda14cbcSMatt Macy * given pool / dataset can be in with regards to terms of
1837eda14cbcSMatt Macy * correcting the issue.
1838eda14cbcSMatt Macy */
1839eda14cbcSMatt Macy if (ds->ds_dir->dd_crypto_obj != 0 &&
1840eda14cbcSMatt Macy spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARK_V2)) {
1841eda14cbcSMatt Macy uint64_t ivset_guid = unique_create();
1842eda14cbcSMatt Macy
1843eda14cbcSMatt Macy dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
1844eda14cbcSMatt Macy VERIFY0(zap_add(mos, dsobj, DS_FIELD_IVSET_GUID,
1845eda14cbcSMatt Macy sizeof (ivset_guid), 1, &ivset_guid, tx));
1846eda14cbcSMatt Macy }
1847eda14cbcSMatt Macy
1848eda14cbcSMatt Macy ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
1849eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
1850eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
1851eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_unique_bytes = 0;
1852eda14cbcSMatt Macy
1853eda14cbcSMatt Macy if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1854eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1855eda14cbcSMatt Macy
1856eda14cbcSMatt Macy VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1857eda14cbcSMatt Macy snapname, 8, 1, &dsobj, tx));
1858eda14cbcSMatt Macy
1859eda14cbcSMatt Macy if (ds->ds_prev)
1860eda14cbcSMatt Macy dsl_dataset_rele(ds->ds_prev, ds);
1861eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj(dp,
1862eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
1863eda14cbcSMatt Macy
1864eda14cbcSMatt Macy dsl_scan_ds_snapshotted(ds, tx);
1865eda14cbcSMatt Macy
1866271171e0SMartin Matuska dsl_dir_snap_cmtime_update(ds->ds_dir, tx);
1867eda14cbcSMatt Macy
186808aba0aeSMartin Matuska if (zfs_snapshot_history_enabled)
1869eda14cbcSMatt Macy spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, " ");
1870eda14cbcSMatt Macy }
1871eda14cbcSMatt Macy
1872eda14cbcSMatt Macy void
dsl_dataset_snapshot_sync(void * arg,dmu_tx_t * tx)1873eda14cbcSMatt Macy dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1874eda14cbcSMatt Macy {
1875eda14cbcSMatt Macy dsl_dataset_snapshot_arg_t *ddsa = arg;
1876eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
1877eda14cbcSMatt Macy nvpair_t *pair;
1878eda14cbcSMatt Macy
1879eda14cbcSMatt Macy for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1880eda14cbcSMatt Macy pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1881eda14cbcSMatt Macy dsl_dataset_t *ds;
18822a58b312SMartin Matuska const char *name, *atp;
1883eda14cbcSMatt Macy char dsname[ZFS_MAX_DATASET_NAME_LEN];
1884eda14cbcSMatt Macy
1885eda14cbcSMatt Macy name = nvpair_name(pair);
1886eda14cbcSMatt Macy atp = strchr(name, '@');
1887eda14cbcSMatt Macy (void) strlcpy(dsname, name, atp - name + 1);
1888eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1889eda14cbcSMatt Macy
1890eda14cbcSMatt Macy dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1891eda14cbcSMatt Macy if (ddsa->ddsa_props != NULL) {
1892eda14cbcSMatt Macy dsl_props_set_sync_impl(ds->ds_prev,
1893eda14cbcSMatt Macy ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1894eda14cbcSMatt Macy }
1895eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
1896eda14cbcSMatt Macy }
1897eda14cbcSMatt Macy }
1898eda14cbcSMatt Macy
1899eda14cbcSMatt Macy /*
1900eda14cbcSMatt Macy * The snapshots must all be in the same pool.
1901eda14cbcSMatt Macy * All-or-nothing: if there are any failures, nothing will be modified.
1902eda14cbcSMatt Macy */
1903eda14cbcSMatt Macy int
dsl_dataset_snapshot(nvlist_t * snaps,nvlist_t * props,nvlist_t * errors)1904eda14cbcSMatt Macy dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1905eda14cbcSMatt Macy {
1906eda14cbcSMatt Macy dsl_dataset_snapshot_arg_t ddsa;
1907eda14cbcSMatt Macy nvpair_t *pair;
1908eda14cbcSMatt Macy boolean_t needsuspend;
1909eda14cbcSMatt Macy int error;
1910eda14cbcSMatt Macy spa_t *spa;
19112a58b312SMartin Matuska const char *firstname;
1912eda14cbcSMatt Macy nvlist_t *suspended = NULL;
1913eda14cbcSMatt Macy
1914eda14cbcSMatt Macy pair = nvlist_next_nvpair(snaps, NULL);
1915eda14cbcSMatt Macy if (pair == NULL)
1916eda14cbcSMatt Macy return (0);
1917eda14cbcSMatt Macy firstname = nvpair_name(pair);
1918eda14cbcSMatt Macy
1919eda14cbcSMatt Macy error = spa_open(firstname, &spa, FTAG);
1920eda14cbcSMatt Macy if (error != 0)
1921eda14cbcSMatt Macy return (error);
1922eda14cbcSMatt Macy needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1923eda14cbcSMatt Macy spa_close(spa, FTAG);
1924eda14cbcSMatt Macy
1925eda14cbcSMatt Macy if (needsuspend) {
1926eda14cbcSMatt Macy suspended = fnvlist_alloc();
1927eda14cbcSMatt Macy for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1928eda14cbcSMatt Macy pair = nvlist_next_nvpair(snaps, pair)) {
1929eda14cbcSMatt Macy char fsname[ZFS_MAX_DATASET_NAME_LEN];
19302a58b312SMartin Matuska const char *snapname = nvpair_name(pair);
19312a58b312SMartin Matuska const char *atp;
1932eda14cbcSMatt Macy void *cookie;
1933eda14cbcSMatt Macy
1934eda14cbcSMatt Macy atp = strchr(snapname, '@');
1935eda14cbcSMatt Macy if (atp == NULL) {
1936eda14cbcSMatt Macy error = SET_ERROR(EINVAL);
1937eda14cbcSMatt Macy break;
1938eda14cbcSMatt Macy }
1939eda14cbcSMatt Macy (void) strlcpy(fsname, snapname, atp - snapname + 1);
1940eda14cbcSMatt Macy
1941eda14cbcSMatt Macy error = zil_suspend(fsname, &cookie);
1942eda14cbcSMatt Macy if (error != 0)
1943eda14cbcSMatt Macy break;
1944eda14cbcSMatt Macy fnvlist_add_uint64(suspended, fsname,
1945eda14cbcSMatt Macy (uintptr_t)cookie);
1946eda14cbcSMatt Macy }
1947eda14cbcSMatt Macy }
1948eda14cbcSMatt Macy
1949eda14cbcSMatt Macy ddsa.ddsa_snaps = snaps;
1950eda14cbcSMatt Macy ddsa.ddsa_props = props;
1951eda14cbcSMatt Macy ddsa.ddsa_errors = errors;
1952eda14cbcSMatt Macy ddsa.ddsa_cr = CRED();
1953eda14cbcSMatt Macy ddsa.ddsa_proc = curproc;
1954eda14cbcSMatt Macy
1955eda14cbcSMatt Macy if (error == 0) {
1956eda14cbcSMatt Macy error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1957eda14cbcSMatt Macy dsl_dataset_snapshot_sync, &ddsa,
1958eda14cbcSMatt Macy fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
1959eda14cbcSMatt Macy }
1960eda14cbcSMatt Macy
1961eda14cbcSMatt Macy if (suspended != NULL) {
1962eda14cbcSMatt Macy for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1963eda14cbcSMatt Macy pair = nvlist_next_nvpair(suspended, pair)) {
1964eda14cbcSMatt Macy zil_resume((void *)(uintptr_t)
1965eda14cbcSMatt Macy fnvpair_value_uint64(pair));
1966eda14cbcSMatt Macy }
1967eda14cbcSMatt Macy fnvlist_free(suspended);
1968eda14cbcSMatt Macy }
1969eda14cbcSMatt Macy
1970eda14cbcSMatt Macy if (error == 0) {
1971eda14cbcSMatt Macy for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1972eda14cbcSMatt Macy pair = nvlist_next_nvpair(snaps, pair)) {
1973eda14cbcSMatt Macy zvol_create_minor(nvpair_name(pair));
1974eda14cbcSMatt Macy }
1975eda14cbcSMatt Macy }
1976eda14cbcSMatt Macy
1977eda14cbcSMatt Macy return (error);
1978eda14cbcSMatt Macy }
1979eda14cbcSMatt Macy
1980eda14cbcSMatt Macy typedef struct dsl_dataset_snapshot_tmp_arg {
1981eda14cbcSMatt Macy const char *ddsta_fsname;
1982eda14cbcSMatt Macy const char *ddsta_snapname;
1983eda14cbcSMatt Macy minor_t ddsta_cleanup_minor;
1984eda14cbcSMatt Macy const char *ddsta_htag;
1985eda14cbcSMatt Macy } dsl_dataset_snapshot_tmp_arg_t;
1986eda14cbcSMatt Macy
1987eda14cbcSMatt Macy static int
dsl_dataset_snapshot_tmp_check(void * arg,dmu_tx_t * tx)1988eda14cbcSMatt Macy dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1989eda14cbcSMatt Macy {
1990eda14cbcSMatt Macy dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1991eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
1992eda14cbcSMatt Macy dsl_dataset_t *ds;
1993eda14cbcSMatt Macy int error;
1994eda14cbcSMatt Macy
1995eda14cbcSMatt Macy error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1996eda14cbcSMatt Macy if (error != 0)
1997eda14cbcSMatt Macy return (error);
1998eda14cbcSMatt Macy
1999eda14cbcSMatt Macy /* NULL cred means no limit check for tmp snapshot */
2000eda14cbcSMatt Macy error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
2001eda14cbcSMatt Macy tx, B_FALSE, 0, NULL, NULL);
2002eda14cbcSMatt Macy if (error != 0) {
2003eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
2004eda14cbcSMatt Macy return (error);
2005eda14cbcSMatt Macy }
2006eda14cbcSMatt Macy
2007eda14cbcSMatt Macy if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
2008eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
2009eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP));
2010eda14cbcSMatt Macy }
2011eda14cbcSMatt Macy error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
2012eda14cbcSMatt Macy B_TRUE, tx);
2013eda14cbcSMatt Macy if (error != 0) {
2014eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
2015eda14cbcSMatt Macy return (error);
2016eda14cbcSMatt Macy }
2017eda14cbcSMatt Macy
2018eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
2019eda14cbcSMatt Macy return (0);
2020eda14cbcSMatt Macy }
2021eda14cbcSMatt Macy
2022eda14cbcSMatt Macy static void
dsl_dataset_snapshot_tmp_sync(void * arg,dmu_tx_t * tx)2023eda14cbcSMatt Macy dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
2024eda14cbcSMatt Macy {
2025eda14cbcSMatt Macy dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
2026eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
2027eda14cbcSMatt Macy dsl_dataset_t *ds = NULL;
2028eda14cbcSMatt Macy
2029eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
2030eda14cbcSMatt Macy
2031eda14cbcSMatt Macy dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
2032eda14cbcSMatt Macy dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
2033eda14cbcSMatt Macy ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
2034eda14cbcSMatt Macy dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
2035eda14cbcSMatt Macy
2036eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
2037eda14cbcSMatt Macy }
2038eda14cbcSMatt Macy
2039eda14cbcSMatt Macy int
dsl_dataset_snapshot_tmp(const char * fsname,const char * snapname,minor_t cleanup_minor,const char * htag)2040eda14cbcSMatt Macy dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
2041eda14cbcSMatt Macy minor_t cleanup_minor, const char *htag)
2042eda14cbcSMatt Macy {
2043eda14cbcSMatt Macy dsl_dataset_snapshot_tmp_arg_t ddsta;
2044eda14cbcSMatt Macy int error;
2045eda14cbcSMatt Macy spa_t *spa;
2046eda14cbcSMatt Macy boolean_t needsuspend;
2047eda14cbcSMatt Macy void *cookie;
2048eda14cbcSMatt Macy
2049eda14cbcSMatt Macy ddsta.ddsta_fsname = fsname;
2050eda14cbcSMatt Macy ddsta.ddsta_snapname = snapname;
2051eda14cbcSMatt Macy ddsta.ddsta_cleanup_minor = cleanup_minor;
2052eda14cbcSMatt Macy ddsta.ddsta_htag = htag;
2053eda14cbcSMatt Macy
2054eda14cbcSMatt Macy error = spa_open(fsname, &spa, FTAG);
2055eda14cbcSMatt Macy if (error != 0)
2056eda14cbcSMatt Macy return (error);
2057eda14cbcSMatt Macy needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
2058eda14cbcSMatt Macy spa_close(spa, FTAG);
2059eda14cbcSMatt Macy
2060eda14cbcSMatt Macy if (needsuspend) {
2061eda14cbcSMatt Macy error = zil_suspend(fsname, &cookie);
2062eda14cbcSMatt Macy if (error != 0)
2063eda14cbcSMatt Macy return (error);
2064eda14cbcSMatt Macy }
2065eda14cbcSMatt Macy
2066eda14cbcSMatt Macy error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
2067eda14cbcSMatt Macy dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
2068eda14cbcSMatt Macy
2069eda14cbcSMatt Macy if (needsuspend)
2070eda14cbcSMatt Macy zil_resume(cookie);
2071eda14cbcSMatt Macy return (error);
2072eda14cbcSMatt Macy }
2073eda14cbcSMatt Macy
207414c2e0a0SMartin Matuska /* Nonblocking dataset sync. Assumes dataset:objset is always 1:1 */
2075eda14cbcSMatt Macy void
dsl_dataset_sync(dsl_dataset_t * ds,zio_t * rio,dmu_tx_t * tx)207614c2e0a0SMartin Matuska dsl_dataset_sync(dsl_dataset_t *ds, zio_t *rio, dmu_tx_t *tx)
2077eda14cbcSMatt Macy {
2078eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx));
2079eda14cbcSMatt Macy ASSERT(ds->ds_objset != NULL);
2080eda14cbcSMatt Macy ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
2081eda14cbcSMatt Macy
2082eda14cbcSMatt Macy /*
2083eda14cbcSMatt Macy * in case we had to change ds_fsid_guid when we opened it,
2084eda14cbcSMatt Macy * sync it out now.
2085eda14cbcSMatt Macy */
2086eda14cbcSMatt Macy dmu_buf_will_dirty(ds->ds_dbuf, tx);
2087eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
2088eda14cbcSMatt Macy
2089eda14cbcSMatt Macy if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) {
2090eda14cbcSMatt Macy VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
2091eda14cbcSMatt Macy ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1,
2092eda14cbcSMatt Macy &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx));
2093eda14cbcSMatt Macy VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
2094eda14cbcSMatt Macy ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1,
2095eda14cbcSMatt Macy &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx));
2096eda14cbcSMatt Macy VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
2097eda14cbcSMatt Macy ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1,
2098eda14cbcSMatt Macy &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx));
2099eda14cbcSMatt Macy ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0;
2100eda14cbcSMatt Macy ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0;
2101eda14cbcSMatt Macy ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0;
2102eda14cbcSMatt Macy }
2103eda14cbcSMatt Macy
210414c2e0a0SMartin Matuska dmu_objset_sync(ds->ds_objset, rio, tx);
2105eda14cbcSMatt Macy }
2106eda14cbcSMatt Macy
2107eda14cbcSMatt Macy /*
2108eda14cbcSMatt Macy * Check if the percentage of blocks shared between the clone and the
2109eda14cbcSMatt Macy * snapshot (as opposed to those that are clone only) is below a certain
2110eda14cbcSMatt Macy * threshold
2111eda14cbcSMatt Macy */
2112eda14cbcSMatt Macy static boolean_t
dsl_livelist_should_disable(dsl_dataset_t * ds)2113eda14cbcSMatt Macy dsl_livelist_should_disable(dsl_dataset_t *ds)
2114eda14cbcSMatt Macy {
2115eda14cbcSMatt Macy uint64_t used, referenced;
2116eda14cbcSMatt Macy int percent_shared;
2117eda14cbcSMatt Macy
2118eda14cbcSMatt Macy used = dsl_dir_get_usedds(ds->ds_dir);
2119eda14cbcSMatt Macy referenced = dsl_get_referenced(ds);
2120eda14cbcSMatt Macy if (referenced == 0)
2121eda14cbcSMatt Macy return (B_FALSE);
2122eda14cbcSMatt Macy percent_shared = (100 * (referenced - used)) / referenced;
2123eda14cbcSMatt Macy if (percent_shared <= zfs_livelist_min_percent_shared)
2124eda14cbcSMatt Macy return (B_TRUE);
2125eda14cbcSMatt Macy return (B_FALSE);
2126eda14cbcSMatt Macy }
2127eda14cbcSMatt Macy
2128eda14cbcSMatt Macy /*
2129eda14cbcSMatt Macy * Check if it is possible to combine two livelist entries into one.
2130eda14cbcSMatt Macy * This is the case if the combined number of 'live' blkptrs (ALLOCs that
2131eda14cbcSMatt Macy * don't have a matching FREE) is under the maximum sublist size.
2132eda14cbcSMatt Macy * We check this by subtracting twice the total number of frees from the total
2133eda14cbcSMatt Macy * number of blkptrs. FREEs are counted twice because each FREE blkptr
2134eda14cbcSMatt Macy * will cancel out an ALLOC blkptr when the livelist is processed.
2135eda14cbcSMatt Macy */
2136eda14cbcSMatt Macy static boolean_t
dsl_livelist_should_condense(dsl_deadlist_entry_t * first,dsl_deadlist_entry_t * next)2137eda14cbcSMatt Macy dsl_livelist_should_condense(dsl_deadlist_entry_t *first,
2138eda14cbcSMatt Macy dsl_deadlist_entry_t *next)
2139eda14cbcSMatt Macy {
2140eda14cbcSMatt Macy uint64_t total_free = first->dle_bpobj.bpo_phys->bpo_num_freed +
2141eda14cbcSMatt Macy next->dle_bpobj.bpo_phys->bpo_num_freed;
2142eda14cbcSMatt Macy uint64_t total_entries = first->dle_bpobj.bpo_phys->bpo_num_blkptrs +
2143eda14cbcSMatt Macy next->dle_bpobj.bpo_phys->bpo_num_blkptrs;
2144eda14cbcSMatt Macy if ((total_entries - (2 * total_free)) < zfs_livelist_max_entries)
2145eda14cbcSMatt Macy return (B_TRUE);
2146eda14cbcSMatt Macy return (B_FALSE);
2147eda14cbcSMatt Macy }
2148eda14cbcSMatt Macy
2149eda14cbcSMatt Macy typedef struct try_condense_arg {
2150eda14cbcSMatt Macy spa_t *spa;
2151eda14cbcSMatt Macy dsl_dataset_t *ds;
2152eda14cbcSMatt Macy } try_condense_arg_t;
2153eda14cbcSMatt Macy
2154eda14cbcSMatt Macy /*
2155eda14cbcSMatt Macy * Iterate over the livelist entries, searching for a pair to condense.
2156eda14cbcSMatt Macy * A nonzero return value means stop, 0 means keep looking.
2157eda14cbcSMatt Macy */
2158eda14cbcSMatt Macy static int
dsl_livelist_try_condense(void * arg,dsl_deadlist_entry_t * first)2159eda14cbcSMatt Macy dsl_livelist_try_condense(void *arg, dsl_deadlist_entry_t *first)
2160eda14cbcSMatt Macy {
2161eda14cbcSMatt Macy try_condense_arg_t *tca = arg;
2162eda14cbcSMatt Macy spa_t *spa = tca->spa;
2163eda14cbcSMatt Macy dsl_dataset_t *ds = tca->ds;
2164eda14cbcSMatt Macy dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist;
2165eda14cbcSMatt Macy dsl_deadlist_entry_t *next;
2166eda14cbcSMatt Macy
2167eda14cbcSMatt Macy /* The condense thread has not yet been created at import */
2168eda14cbcSMatt Macy if (spa->spa_livelist_condense_zthr == NULL)
2169eda14cbcSMatt Macy return (1);
2170eda14cbcSMatt Macy
2171eda14cbcSMatt Macy /* A condense is already in progress */
2172eda14cbcSMatt Macy if (spa->spa_to_condense.ds != NULL)
2173eda14cbcSMatt Macy return (1);
2174eda14cbcSMatt Macy
2175eda14cbcSMatt Macy next = AVL_NEXT(&ll->dl_tree, &first->dle_node);
2176eda14cbcSMatt Macy /* The livelist has only one entry - don't condense it */
2177eda14cbcSMatt Macy if (next == NULL)
2178eda14cbcSMatt Macy return (1);
2179eda14cbcSMatt Macy
2180eda14cbcSMatt Macy /* Next is the newest entry - don't condense it */
2181eda14cbcSMatt Macy if (AVL_NEXT(&ll->dl_tree, &next->dle_node) == NULL)
2182eda14cbcSMatt Macy return (1);
2183eda14cbcSMatt Macy
2184eda14cbcSMatt Macy /* This pair is not ready to condense but keep looking */
2185eda14cbcSMatt Macy if (!dsl_livelist_should_condense(first, next))
2186eda14cbcSMatt Macy return (0);
2187eda14cbcSMatt Macy
2188eda14cbcSMatt Macy /*
2189eda14cbcSMatt Macy * Add a ref to prevent the dataset from being evicted while
2190eda14cbcSMatt Macy * the condense zthr or synctask are running. Ref will be
2191eda14cbcSMatt Macy * released at the end of the condense synctask
2192eda14cbcSMatt Macy */
2193eda14cbcSMatt Macy dmu_buf_add_ref(ds->ds_dbuf, spa);
2194eda14cbcSMatt Macy
2195eda14cbcSMatt Macy spa->spa_to_condense.ds = ds;
2196eda14cbcSMatt Macy spa->spa_to_condense.first = first;
2197eda14cbcSMatt Macy spa->spa_to_condense.next = next;
2198eda14cbcSMatt Macy spa->spa_to_condense.syncing = B_FALSE;
2199eda14cbcSMatt Macy spa->spa_to_condense.cancelled = B_FALSE;
2200eda14cbcSMatt Macy
2201eda14cbcSMatt Macy zthr_wakeup(spa->spa_livelist_condense_zthr);
2202eda14cbcSMatt Macy return (1);
2203eda14cbcSMatt Macy }
2204eda14cbcSMatt Macy
2205eda14cbcSMatt Macy static void
dsl_flush_pending_livelist(dsl_dataset_t * ds,dmu_tx_t * tx)2206eda14cbcSMatt Macy dsl_flush_pending_livelist(dsl_dataset_t *ds, dmu_tx_t *tx)
2207eda14cbcSMatt Macy {
2208eda14cbcSMatt Macy dsl_dir_t *dd = ds->ds_dir;
2209eda14cbcSMatt Macy spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
2210eda14cbcSMatt Macy dsl_deadlist_entry_t *last = dsl_deadlist_last(&dd->dd_livelist);
2211eda14cbcSMatt Macy
2212eda14cbcSMatt Macy /* Check if we need to add a new sub-livelist */
2213eda14cbcSMatt Macy if (last == NULL) {
2214eda14cbcSMatt Macy /* The livelist is empty */
2215eda14cbcSMatt Macy dsl_deadlist_add_key(&dd->dd_livelist,
2216eda14cbcSMatt Macy tx->tx_txg - 1, tx);
2217eda14cbcSMatt Macy } else if (spa_sync_pass(spa) == 1) {
2218eda14cbcSMatt Macy /*
2219eda14cbcSMatt Macy * Check if the newest entry is full. If it is, make a new one.
2220eda14cbcSMatt Macy * We only do this once per sync because we could overfill a
2221eda14cbcSMatt Macy * sublist in one sync pass and don't want to add another entry
2222eda14cbcSMatt Macy * for a txg that is already represented. This ensures that
2223eda14cbcSMatt Macy * blkptrs born in the same txg are stored in the same sublist.
2224eda14cbcSMatt Macy */
2225eda14cbcSMatt Macy bpobj_t bpobj = last->dle_bpobj;
2226eda14cbcSMatt Macy uint64_t all = bpobj.bpo_phys->bpo_num_blkptrs;
2227eda14cbcSMatt Macy uint64_t free = bpobj.bpo_phys->bpo_num_freed;
2228eda14cbcSMatt Macy uint64_t alloc = all - free;
2229eda14cbcSMatt Macy if (alloc > zfs_livelist_max_entries) {
2230eda14cbcSMatt Macy dsl_deadlist_add_key(&dd->dd_livelist,
2231eda14cbcSMatt Macy tx->tx_txg - 1, tx);
2232eda14cbcSMatt Macy }
2233eda14cbcSMatt Macy }
2234eda14cbcSMatt Macy
2235eda14cbcSMatt Macy /* Insert each entry into the on-disk livelist */
2236eda14cbcSMatt Macy bplist_iterate(&dd->dd_pending_allocs,
2237eda14cbcSMatt Macy dsl_deadlist_insert_alloc_cb, &dd->dd_livelist, tx);
2238eda14cbcSMatt Macy bplist_iterate(&dd->dd_pending_frees,
2239eda14cbcSMatt Macy dsl_deadlist_insert_free_cb, &dd->dd_livelist, tx);
2240eda14cbcSMatt Macy
2241eda14cbcSMatt Macy /* Attempt to condense every pair of adjacent entries */
2242eda14cbcSMatt Macy try_condense_arg_t arg = {
2243eda14cbcSMatt Macy .spa = spa,
2244eda14cbcSMatt Macy .ds = ds
2245eda14cbcSMatt Macy };
2246eda14cbcSMatt Macy dsl_deadlist_iterate(&dd->dd_livelist, dsl_livelist_try_condense,
2247eda14cbcSMatt Macy &arg);
2248eda14cbcSMatt Macy }
2249eda14cbcSMatt Macy
2250eda14cbcSMatt Macy void
dsl_dataset_sync_done(dsl_dataset_t * ds,dmu_tx_t * tx)2251eda14cbcSMatt Macy dsl_dataset_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx)
2252eda14cbcSMatt Macy {
2253eda14cbcSMatt Macy objset_t *os = ds->ds_objset;
2254eda14cbcSMatt Macy
2255eda14cbcSMatt Macy bplist_iterate(&ds->ds_pending_deadlist,
2256eda14cbcSMatt Macy dsl_deadlist_insert_alloc_cb, &ds->ds_deadlist, tx);
2257eda14cbcSMatt Macy
2258eda14cbcSMatt Macy if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist)) {
2259eda14cbcSMatt Macy dsl_flush_pending_livelist(ds, tx);
2260eda14cbcSMatt Macy if (dsl_livelist_should_disable(ds)) {
2261eda14cbcSMatt Macy dsl_dir_remove_livelist(ds->ds_dir, tx, B_TRUE);
2262eda14cbcSMatt Macy }
2263eda14cbcSMatt Macy }
2264eda14cbcSMatt Macy
2265eda14cbcSMatt Macy dsl_bookmark_sync_done(ds, tx);
2266eda14cbcSMatt Macy
22673ff01b23SMartin Matuska multilist_destroy(&os->os_synced_dnodes);
2268eda14cbcSMatt Macy
2269eda14cbcSMatt Macy if (os->os_encrypted)
2270eda14cbcSMatt Macy os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_FALSE;
2271eda14cbcSMatt Macy else
2272eda14cbcSMatt Macy ASSERT0(os->os_next_write_raw[tx->tx_txg & TXG_MASK]);
2273eda14cbcSMatt Macy
2274c9539b89SMartin Matuska for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2275c9539b89SMartin Matuska if (zfeature_active(f,
2276c9539b89SMartin Matuska ds->ds_feature_activation[f])) {
2277c9539b89SMartin Matuska if (zfeature_active(f, ds->ds_feature[f]))
2278c9539b89SMartin Matuska continue;
2279c9539b89SMartin Matuska dsl_dataset_activate_feature(ds->ds_object, f,
2280c9539b89SMartin Matuska ds->ds_feature_activation[f], tx);
2281c9539b89SMartin Matuska ds->ds_feature[f] = ds->ds_feature_activation[f];
2282c9539b89SMartin Matuska }
2283c9539b89SMartin Matuska }
22842a58b312SMartin Matuska
22852a58b312SMartin Matuska ASSERT(!dmu_objset_is_dirty(os, dmu_tx_get_txg(tx)));
2286eda14cbcSMatt Macy }
2287eda14cbcSMatt Macy
2288eda14cbcSMatt Macy int
get_clones_stat_impl(dsl_dataset_t * ds,nvlist_t * val)2289eda14cbcSMatt Macy get_clones_stat_impl(dsl_dataset_t *ds, nvlist_t *val)
2290eda14cbcSMatt Macy {
2291eda14cbcSMatt Macy uint64_t count = 0;
2292eda14cbcSMatt Macy objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
2293eda14cbcSMatt Macy zap_cursor_t zc;
22947a7741afSMartin Matuska zap_attribute_t *za;
2295eda14cbcSMatt Macy
2296eda14cbcSMatt Macy ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
2297eda14cbcSMatt Macy
2298eda14cbcSMatt Macy /*
2299eda14cbcSMatt Macy * There may be missing entries in ds_next_clones_obj
2300eda14cbcSMatt Macy * due to a bug in a previous version of the code.
2301eda14cbcSMatt Macy * Only trust it if it has the right number of entries.
2302eda14cbcSMatt Macy */
2303eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
2304eda14cbcSMatt Macy VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
2305eda14cbcSMatt Macy &count));
2306eda14cbcSMatt Macy }
2307eda14cbcSMatt Macy if (count != dsl_dataset_phys(ds)->ds_num_children - 1) {
2308eda14cbcSMatt Macy return (SET_ERROR(ENOENT));
2309eda14cbcSMatt Macy }
23107a7741afSMartin Matuska
23117a7741afSMartin Matuska za = zap_attribute_alloc();
2312eda14cbcSMatt Macy for (zap_cursor_init(&zc, mos,
2313eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_next_clones_obj);
23147a7741afSMartin Matuska zap_cursor_retrieve(&zc, za) == 0;
2315eda14cbcSMatt Macy zap_cursor_advance(&zc)) {
2316eda14cbcSMatt Macy dsl_dataset_t *clone;
2317eda14cbcSMatt Macy char buf[ZFS_MAX_DATASET_NAME_LEN];
2318eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
23197a7741afSMartin Matuska za->za_first_integer, FTAG, &clone));
2320eda14cbcSMatt Macy dsl_dir_name(clone->ds_dir, buf);
2321eda14cbcSMatt Macy fnvlist_add_boolean(val, buf);
2322eda14cbcSMatt Macy dsl_dataset_rele(clone, FTAG);
2323eda14cbcSMatt Macy }
2324eda14cbcSMatt Macy zap_cursor_fini(&zc);
23257a7741afSMartin Matuska zap_attribute_free(za);
2326eda14cbcSMatt Macy return (0);
2327eda14cbcSMatt Macy }
2328eda14cbcSMatt Macy
2329eda14cbcSMatt Macy void
get_clones_stat(dsl_dataset_t * ds,nvlist_t * nv)2330eda14cbcSMatt Macy get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
2331eda14cbcSMatt Macy {
2332eda14cbcSMatt Macy nvlist_t *propval = fnvlist_alloc();
2333184c1b94SMartin Matuska nvlist_t *val = fnvlist_alloc();
2334eda14cbcSMatt Macy
2335eda14cbcSMatt Macy if (get_clones_stat_impl(ds, val) == 0) {
2336eda14cbcSMatt Macy fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
2337eda14cbcSMatt Macy fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES),
2338eda14cbcSMatt Macy propval);
2339eda14cbcSMatt Macy }
2340eda14cbcSMatt Macy
2341eda14cbcSMatt Macy nvlist_free(val);
2342eda14cbcSMatt Macy nvlist_free(propval);
2343eda14cbcSMatt Macy }
2344eda14cbcSMatt Macy
2345c03c5b1cSMartin Matuska static char *
get_receive_resume_token_impl(dsl_dataset_t * ds)2346c03c5b1cSMartin Matuska get_receive_resume_token_impl(dsl_dataset_t *ds)
2347eda14cbcSMatt Macy {
2348c03c5b1cSMartin Matuska if (!dsl_dataset_has_resume_receive_state(ds))
2349c03c5b1cSMartin Matuska return (NULL);
2350eda14cbcSMatt Macy
2351c03c5b1cSMartin Matuska dsl_pool_t *dp = ds->ds_dir->dd_pool;
2352eda14cbcSMatt Macy char *str;
2353eda14cbcSMatt Macy void *packed;
2354eda14cbcSMatt Macy uint8_t *compressed;
2355eda14cbcSMatt Macy uint64_t val;
2356eda14cbcSMatt Macy nvlist_t *token_nv = fnvlist_alloc();
2357eda14cbcSMatt Macy size_t packed_size, compressed_size;
2358eda14cbcSMatt Macy
2359eda14cbcSMatt Macy if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2360eda14cbcSMatt Macy DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) {
2361eda14cbcSMatt Macy fnvlist_add_uint64(token_nv, "fromguid", val);
2362eda14cbcSMatt Macy }
2363eda14cbcSMatt Macy if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2364eda14cbcSMatt Macy DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) {
2365eda14cbcSMatt Macy fnvlist_add_uint64(token_nv, "object", val);
2366eda14cbcSMatt Macy }
2367eda14cbcSMatt Macy if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2368eda14cbcSMatt Macy DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) {
2369eda14cbcSMatt Macy fnvlist_add_uint64(token_nv, "offset", val);
2370eda14cbcSMatt Macy }
2371eda14cbcSMatt Macy if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2372eda14cbcSMatt Macy DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) {
2373eda14cbcSMatt Macy fnvlist_add_uint64(token_nv, "bytes", val);
2374eda14cbcSMatt Macy }
2375eda14cbcSMatt Macy if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2376eda14cbcSMatt Macy DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) {
2377eda14cbcSMatt Macy fnvlist_add_uint64(token_nv, "toguid", val);
2378eda14cbcSMatt Macy }
2379eda14cbcSMatt Macy char buf[MAXNAMELEN];
2380eda14cbcSMatt Macy if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2381eda14cbcSMatt Macy DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) {
2382eda14cbcSMatt Macy fnvlist_add_string(token_nv, "toname", buf);
2383eda14cbcSMatt Macy }
2384eda14cbcSMatt Macy if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2385eda14cbcSMatt Macy DS_FIELD_RESUME_LARGEBLOCK) == 0) {
2386eda14cbcSMatt Macy fnvlist_add_boolean(token_nv, "largeblockok");
2387eda14cbcSMatt Macy }
2388eda14cbcSMatt Macy if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2389eda14cbcSMatt Macy DS_FIELD_RESUME_EMBEDOK) == 0) {
2390eda14cbcSMatt Macy fnvlist_add_boolean(token_nv, "embedok");
2391eda14cbcSMatt Macy }
2392eda14cbcSMatt Macy if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2393eda14cbcSMatt Macy DS_FIELD_RESUME_COMPRESSOK) == 0) {
2394eda14cbcSMatt Macy fnvlist_add_boolean(token_nv, "compressok");
2395eda14cbcSMatt Macy }
2396eda14cbcSMatt Macy if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2397eda14cbcSMatt Macy DS_FIELD_RESUME_RAWOK) == 0) {
2398eda14cbcSMatt Macy fnvlist_add_boolean(token_nv, "rawok");
2399eda14cbcSMatt Macy }
2400eda14cbcSMatt Macy if (dsl_dataset_feature_is_active(ds,
2401eda14cbcSMatt Macy SPA_FEATURE_REDACTED_DATASETS)) {
2402c03c5b1cSMartin Matuska uint64_t num_redact_snaps = 0;
2403c03c5b1cSMartin Matuska uint64_t *redact_snaps = NULL;
2404c03c5b1cSMartin Matuska VERIFY3B(dsl_dataset_get_uint64_array_feature(ds,
2405eda14cbcSMatt Macy SPA_FEATURE_REDACTED_DATASETS, &num_redact_snaps,
2406c03c5b1cSMartin Matuska &redact_snaps), ==, B_TRUE);
2407eda14cbcSMatt Macy fnvlist_add_uint64_array(token_nv, "redact_snaps",
2408eda14cbcSMatt Macy redact_snaps, num_redact_snaps);
2409eda14cbcSMatt Macy }
2410eda14cbcSMatt Macy if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2411eda14cbcSMatt Macy DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS) == 0) {
2412c03c5b1cSMartin Matuska uint64_t num_redact_snaps = 0, int_size = 0;
2413c03c5b1cSMartin Matuska uint64_t *redact_snaps = NULL;
2414eda14cbcSMatt Macy VERIFY0(zap_length(dp->dp_meta_objset, ds->ds_object,
2415eda14cbcSMatt Macy DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS, &int_size,
2416eda14cbcSMatt Macy &num_redact_snaps));
2417eda14cbcSMatt Macy ASSERT3U(int_size, ==, sizeof (uint64_t));
2418eda14cbcSMatt Macy
2419eda14cbcSMatt Macy redact_snaps = kmem_alloc(int_size * num_redact_snaps,
2420eda14cbcSMatt Macy KM_SLEEP);
2421eda14cbcSMatt Macy VERIFY0(zap_lookup(dp->dp_meta_objset, ds->ds_object,
2422eda14cbcSMatt Macy DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS, int_size,
2423eda14cbcSMatt Macy num_redact_snaps, redact_snaps));
2424eda14cbcSMatt Macy fnvlist_add_uint64_array(token_nv, "book_redact_snaps",
2425eda14cbcSMatt Macy redact_snaps, num_redact_snaps);
2426eda14cbcSMatt Macy kmem_free(redact_snaps, int_size * num_redact_snaps);
2427eda14cbcSMatt Macy }
2428eda14cbcSMatt Macy packed = fnvlist_pack(token_nv, &packed_size);
2429eda14cbcSMatt Macy fnvlist_free(token_nv);
2430eda14cbcSMatt Macy compressed = kmem_alloc(packed_size, KM_SLEEP);
2431eda14cbcSMatt Macy
2432e2df9bb4SMartin Matuska /* Call compress function directly to avoid hole detection. */
2433e2df9bb4SMartin Matuska abd_t pabd, cabd;
2434e2df9bb4SMartin Matuska abd_get_from_buf_struct(&pabd, packed, packed_size);
2435e2df9bb4SMartin Matuska abd_get_from_buf_struct(&cabd, compressed, packed_size);
2436e2df9bb4SMartin Matuska compressed_size = zfs_gzip_compress(&pabd, &cabd,
2437eda14cbcSMatt Macy packed_size, packed_size, 6);
2438e2df9bb4SMartin Matuska abd_free(&cabd);
2439e2df9bb4SMartin Matuska abd_free(&pabd);
2440eda14cbcSMatt Macy
2441eda14cbcSMatt Macy zio_cksum_t cksum;
2442eda14cbcSMatt Macy fletcher_4_native_varsize(compressed, compressed_size, &cksum);
2443eda14cbcSMatt Macy
2444eda14cbcSMatt Macy size_t alloc_size = compressed_size * 2 + 1;
2445eda14cbcSMatt Macy str = kmem_alloc(alloc_size, KM_SLEEP);
2446eda14cbcSMatt Macy for (int i = 0; i < compressed_size; i++) {
2447eda14cbcSMatt Macy size_t offset = i * 2;
2448eda14cbcSMatt Macy (void) snprintf(str + offset, alloc_size - offset,
2449eda14cbcSMatt Macy "%02x", compressed[i]);
2450eda14cbcSMatt Macy }
2451eda14cbcSMatt Macy str[compressed_size * 2] = '\0';
2452eda14cbcSMatt Macy char *propval = kmem_asprintf("%u-%llx-%llx-%s",
2453eda14cbcSMatt Macy ZFS_SEND_RESUME_TOKEN_VERSION,
2454eda14cbcSMatt Macy (longlong_t)cksum.zc_word[0],
2455eda14cbcSMatt Macy (longlong_t)packed_size, str);
2456eda14cbcSMatt Macy kmem_free(packed, packed_size);
2457eda14cbcSMatt Macy kmem_free(str, alloc_size);
2458eda14cbcSMatt Macy kmem_free(compressed, packed_size);
2459eda14cbcSMatt Macy return (propval);
2460eda14cbcSMatt Macy }
2461eda14cbcSMatt Macy
2462eda14cbcSMatt Macy /*
2463c03c5b1cSMartin Matuska * Returns a string that represents the receive resume state token. It should
2464c03c5b1cSMartin Matuska * be freed with strfree(). NULL is returned if no resume state is present.
2465eda14cbcSMatt Macy */
2466eda14cbcSMatt Macy char *
get_receive_resume_token(dsl_dataset_t * ds)2467c03c5b1cSMartin Matuska get_receive_resume_token(dsl_dataset_t *ds)
2468eda14cbcSMatt Macy {
2469c03c5b1cSMartin Matuska /*
2470c03c5b1cSMartin Matuska * A failed "newfs" (e.g. full) resumable receive leaves
2471c03c5b1cSMartin Matuska * the stats set on this dataset. Check here for the prop.
2472c03c5b1cSMartin Matuska */
2473c03c5b1cSMartin Matuska char *token = get_receive_resume_token_impl(ds);
2474c03c5b1cSMartin Matuska if (token != NULL)
2475c03c5b1cSMartin Matuska return (token);
2476c03c5b1cSMartin Matuska /*
2477c03c5b1cSMartin Matuska * A failed incremental resumable receive leaves the
2478c03c5b1cSMartin Matuska * stats set on our child named "%recv". Check the child
2479c03c5b1cSMartin Matuska * for the prop.
2480c03c5b1cSMartin Matuska */
2481c03c5b1cSMartin Matuska /* 6 extra bytes for /%recv */
2482c03c5b1cSMartin Matuska char name[ZFS_MAX_DATASET_NAME_LEN + 6];
2483eda14cbcSMatt Macy dsl_dataset_t *recv_ds;
2484c03c5b1cSMartin Matuska dsl_dataset_name(ds, name);
2485c03c5b1cSMartin Matuska if (strlcat(name, "/", sizeof (name)) < sizeof (name) &&
2486c03c5b1cSMartin Matuska strlcat(name, recv_clone_name, sizeof (name)) < sizeof (name) &&
2487c03c5b1cSMartin Matuska dsl_dataset_hold(ds->ds_dir->dd_pool, name, FTAG, &recv_ds) == 0) {
2488c03c5b1cSMartin Matuska token = get_receive_resume_token_impl(recv_ds);
2489eda14cbcSMatt Macy dsl_dataset_rele(recv_ds, FTAG);
2490eda14cbcSMatt Macy }
2491c03c5b1cSMartin Matuska return (token);
2492eda14cbcSMatt Macy }
2493eda14cbcSMatt Macy
2494eda14cbcSMatt Macy uint64_t
dsl_get_refratio(dsl_dataset_t * ds)2495eda14cbcSMatt Macy dsl_get_refratio(dsl_dataset_t *ds)
2496eda14cbcSMatt Macy {
2497eda14cbcSMatt Macy uint64_t ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
2498eda14cbcSMatt Macy (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
2499eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_compressed_bytes);
2500eda14cbcSMatt Macy return (ratio);
2501eda14cbcSMatt Macy }
2502eda14cbcSMatt Macy
2503eda14cbcSMatt Macy uint64_t
dsl_get_logicalreferenced(dsl_dataset_t * ds)2504eda14cbcSMatt Macy dsl_get_logicalreferenced(dsl_dataset_t *ds)
2505eda14cbcSMatt Macy {
2506eda14cbcSMatt Macy return (dsl_dataset_phys(ds)->ds_uncompressed_bytes);
2507eda14cbcSMatt Macy }
2508eda14cbcSMatt Macy
2509eda14cbcSMatt Macy uint64_t
dsl_get_compressratio(dsl_dataset_t * ds)2510eda14cbcSMatt Macy dsl_get_compressratio(dsl_dataset_t *ds)
2511eda14cbcSMatt Macy {
2512eda14cbcSMatt Macy if (ds->ds_is_snapshot) {
2513eda14cbcSMatt Macy return (dsl_get_refratio(ds));
2514eda14cbcSMatt Macy } else {
2515eda14cbcSMatt Macy dsl_dir_t *dd = ds->ds_dir;
2516eda14cbcSMatt Macy mutex_enter(&dd->dd_lock);
2517eda14cbcSMatt Macy uint64_t val = dsl_dir_get_compressratio(dd);
2518eda14cbcSMatt Macy mutex_exit(&dd->dd_lock);
2519eda14cbcSMatt Macy return (val);
2520eda14cbcSMatt Macy }
2521eda14cbcSMatt Macy }
2522eda14cbcSMatt Macy
2523eda14cbcSMatt Macy uint64_t
dsl_get_used(dsl_dataset_t * ds)2524eda14cbcSMatt Macy dsl_get_used(dsl_dataset_t *ds)
2525eda14cbcSMatt Macy {
2526eda14cbcSMatt Macy if (ds->ds_is_snapshot) {
2527eda14cbcSMatt Macy return (dsl_dataset_phys(ds)->ds_unique_bytes);
2528eda14cbcSMatt Macy } else {
2529eda14cbcSMatt Macy dsl_dir_t *dd = ds->ds_dir;
2530eda14cbcSMatt Macy mutex_enter(&dd->dd_lock);
2531eda14cbcSMatt Macy uint64_t val = dsl_dir_get_used(dd);
2532eda14cbcSMatt Macy mutex_exit(&dd->dd_lock);
2533eda14cbcSMatt Macy return (val);
2534eda14cbcSMatt Macy }
2535eda14cbcSMatt Macy }
2536eda14cbcSMatt Macy
2537eda14cbcSMatt Macy uint64_t
dsl_get_creation(dsl_dataset_t * ds)2538eda14cbcSMatt Macy dsl_get_creation(dsl_dataset_t *ds)
2539eda14cbcSMatt Macy {
2540eda14cbcSMatt Macy return (dsl_dataset_phys(ds)->ds_creation_time);
2541eda14cbcSMatt Macy }
2542eda14cbcSMatt Macy
2543eda14cbcSMatt Macy uint64_t
dsl_get_creationtxg(dsl_dataset_t * ds)2544eda14cbcSMatt Macy dsl_get_creationtxg(dsl_dataset_t *ds)
2545eda14cbcSMatt Macy {
2546eda14cbcSMatt Macy return (dsl_dataset_phys(ds)->ds_creation_txg);
2547eda14cbcSMatt Macy }
2548eda14cbcSMatt Macy
2549eda14cbcSMatt Macy uint64_t
dsl_get_refquota(dsl_dataset_t * ds)2550eda14cbcSMatt Macy dsl_get_refquota(dsl_dataset_t *ds)
2551eda14cbcSMatt Macy {
2552eda14cbcSMatt Macy return (ds->ds_quota);
2553eda14cbcSMatt Macy }
2554eda14cbcSMatt Macy
2555eda14cbcSMatt Macy uint64_t
dsl_get_refreservation(dsl_dataset_t * ds)2556eda14cbcSMatt Macy dsl_get_refreservation(dsl_dataset_t *ds)
2557eda14cbcSMatt Macy {
2558eda14cbcSMatt Macy return (ds->ds_reserved);
2559eda14cbcSMatt Macy }
2560eda14cbcSMatt Macy
2561eda14cbcSMatt Macy uint64_t
dsl_get_guid(dsl_dataset_t * ds)2562eda14cbcSMatt Macy dsl_get_guid(dsl_dataset_t *ds)
2563eda14cbcSMatt Macy {
2564eda14cbcSMatt Macy return (dsl_dataset_phys(ds)->ds_guid);
2565eda14cbcSMatt Macy }
2566eda14cbcSMatt Macy
2567eda14cbcSMatt Macy uint64_t
dsl_get_unique(dsl_dataset_t * ds)2568eda14cbcSMatt Macy dsl_get_unique(dsl_dataset_t *ds)
2569eda14cbcSMatt Macy {
2570eda14cbcSMatt Macy return (dsl_dataset_phys(ds)->ds_unique_bytes);
2571eda14cbcSMatt Macy }
2572eda14cbcSMatt Macy
2573eda14cbcSMatt Macy uint64_t
dsl_get_objsetid(dsl_dataset_t * ds)2574eda14cbcSMatt Macy dsl_get_objsetid(dsl_dataset_t *ds)
2575eda14cbcSMatt Macy {
2576eda14cbcSMatt Macy return (ds->ds_object);
2577eda14cbcSMatt Macy }
2578eda14cbcSMatt Macy
2579eda14cbcSMatt Macy uint64_t
dsl_get_userrefs(dsl_dataset_t * ds)2580eda14cbcSMatt Macy dsl_get_userrefs(dsl_dataset_t *ds)
2581eda14cbcSMatt Macy {
2582eda14cbcSMatt Macy return (ds->ds_userrefs);
2583eda14cbcSMatt Macy }
2584eda14cbcSMatt Macy
2585eda14cbcSMatt Macy uint64_t
dsl_get_defer_destroy(dsl_dataset_t * ds)2586eda14cbcSMatt Macy dsl_get_defer_destroy(dsl_dataset_t *ds)
2587eda14cbcSMatt Macy {
2588eda14cbcSMatt Macy return (DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
2589eda14cbcSMatt Macy }
2590eda14cbcSMatt Macy
2591eda14cbcSMatt Macy uint64_t
dsl_get_referenced(dsl_dataset_t * ds)2592eda14cbcSMatt Macy dsl_get_referenced(dsl_dataset_t *ds)
2593eda14cbcSMatt Macy {
2594eda14cbcSMatt Macy return (dsl_dataset_phys(ds)->ds_referenced_bytes);
2595eda14cbcSMatt Macy }
2596eda14cbcSMatt Macy
2597eda14cbcSMatt Macy uint64_t
dsl_get_numclones(dsl_dataset_t * ds)2598eda14cbcSMatt Macy dsl_get_numclones(dsl_dataset_t *ds)
2599eda14cbcSMatt Macy {
2600eda14cbcSMatt Macy ASSERT(ds->ds_is_snapshot);
2601eda14cbcSMatt Macy return (dsl_dataset_phys(ds)->ds_num_children - 1);
2602eda14cbcSMatt Macy }
2603eda14cbcSMatt Macy
2604eda14cbcSMatt Macy uint64_t
dsl_get_inconsistent(dsl_dataset_t * ds)2605eda14cbcSMatt Macy dsl_get_inconsistent(dsl_dataset_t *ds)
2606eda14cbcSMatt Macy {
2607eda14cbcSMatt Macy return ((dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT) ?
2608eda14cbcSMatt Macy 1 : 0);
2609eda14cbcSMatt Macy }
2610eda14cbcSMatt Macy
2611eda14cbcSMatt Macy uint64_t
dsl_get_redacted(dsl_dataset_t * ds)2612eda14cbcSMatt Macy dsl_get_redacted(dsl_dataset_t *ds)
2613eda14cbcSMatt Macy {
2614eda14cbcSMatt Macy return (dsl_dataset_feature_is_active(ds,
2615eda14cbcSMatt Macy SPA_FEATURE_REDACTED_DATASETS));
2616eda14cbcSMatt Macy }
2617eda14cbcSMatt Macy
2618eda14cbcSMatt Macy uint64_t
dsl_get_available(dsl_dataset_t * ds)2619eda14cbcSMatt Macy dsl_get_available(dsl_dataset_t *ds)
2620eda14cbcSMatt Macy {
2621eda14cbcSMatt Macy uint64_t refdbytes = dsl_get_referenced(ds);
2622eda14cbcSMatt Macy uint64_t availbytes = dsl_dir_space_available(ds->ds_dir,
2623eda14cbcSMatt Macy NULL, 0, TRUE);
2624eda14cbcSMatt Macy if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
2625eda14cbcSMatt Macy availbytes +=
2626eda14cbcSMatt Macy ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
2627eda14cbcSMatt Macy }
2628eda14cbcSMatt Macy if (ds->ds_quota != 0) {
2629eda14cbcSMatt Macy /*
2630eda14cbcSMatt Macy * Adjust available bytes according to refquota
2631eda14cbcSMatt Macy */
2632eda14cbcSMatt Macy if (refdbytes < ds->ds_quota) {
2633eda14cbcSMatt Macy availbytes = MIN(availbytes,
2634eda14cbcSMatt Macy ds->ds_quota - refdbytes);
2635eda14cbcSMatt Macy } else {
2636eda14cbcSMatt Macy availbytes = 0;
2637eda14cbcSMatt Macy }
2638eda14cbcSMatt Macy }
2639eda14cbcSMatt Macy return (availbytes);
2640eda14cbcSMatt Macy }
2641eda14cbcSMatt Macy
2642eda14cbcSMatt Macy int
dsl_get_written(dsl_dataset_t * ds,uint64_t * written)2643eda14cbcSMatt Macy dsl_get_written(dsl_dataset_t *ds, uint64_t *written)
2644eda14cbcSMatt Macy {
2645eda14cbcSMatt Macy dsl_pool_t *dp = ds->ds_dir->dd_pool;
2646eda14cbcSMatt Macy dsl_dataset_t *prev;
2647eda14cbcSMatt Macy int err = dsl_dataset_hold_obj(dp,
2648eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
2649eda14cbcSMatt Macy if (err == 0) {
2650eda14cbcSMatt Macy uint64_t comp, uncomp;
2651eda14cbcSMatt Macy err = dsl_dataset_space_written(prev, ds, written,
2652eda14cbcSMatt Macy &comp, &uncomp);
2653eda14cbcSMatt Macy dsl_dataset_rele(prev, FTAG);
2654eda14cbcSMatt Macy }
2655eda14cbcSMatt Macy return (err);
2656eda14cbcSMatt Macy }
2657eda14cbcSMatt Macy
2658eda14cbcSMatt Macy /*
2659eda14cbcSMatt Macy * 'snap' should be a buffer of size ZFS_MAX_DATASET_NAME_LEN.
2660eda14cbcSMatt Macy */
2661eda14cbcSMatt Macy int
dsl_get_prev_snap(dsl_dataset_t * ds,char * snap)2662eda14cbcSMatt Macy dsl_get_prev_snap(dsl_dataset_t *ds, char *snap)
2663eda14cbcSMatt Macy {
2664eda14cbcSMatt Macy dsl_pool_t *dp = ds->ds_dir->dd_pool;
2665eda14cbcSMatt Macy if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
2666eda14cbcSMatt Macy dsl_dataset_name(ds->ds_prev, snap);
2667eda14cbcSMatt Macy return (0);
2668eda14cbcSMatt Macy } else {
2669eda14cbcSMatt Macy return (SET_ERROR(ENOENT));
2670eda14cbcSMatt Macy }
2671eda14cbcSMatt Macy }
2672eda14cbcSMatt Macy
2673eda14cbcSMatt Macy void
dsl_get_redact_snaps(dsl_dataset_t * ds,nvlist_t * propval)2674eda14cbcSMatt Macy dsl_get_redact_snaps(dsl_dataset_t *ds, nvlist_t *propval)
2675eda14cbcSMatt Macy {
2676eda14cbcSMatt Macy uint64_t nsnaps;
2677eda14cbcSMatt Macy uint64_t *snaps;
2678eda14cbcSMatt Macy if (dsl_dataset_get_uint64_array_feature(ds,
2679eda14cbcSMatt Macy SPA_FEATURE_REDACTED_DATASETS, &nsnaps, &snaps)) {
2680eda14cbcSMatt Macy fnvlist_add_uint64_array(propval, ZPROP_VALUE, snaps,
2681eda14cbcSMatt Macy nsnaps);
2682eda14cbcSMatt Macy }
2683eda14cbcSMatt Macy }
2684eda14cbcSMatt Macy
2685eda14cbcSMatt Macy /*
2686eda14cbcSMatt Macy * Returns the mountpoint property and source for the given dataset in the value
2687eda14cbcSMatt Macy * and source buffers. The value buffer must be at least as large as MAXPATHLEN
2688eda14cbcSMatt Macy * and the source buffer as least as large a ZFS_MAX_DATASET_NAME_LEN.
2689eda14cbcSMatt Macy * Returns 0 on success and an error on failure.
2690eda14cbcSMatt Macy */
2691eda14cbcSMatt Macy int
dsl_get_mountpoint(dsl_dataset_t * ds,const char * dsname,char * value,char * source)2692eda14cbcSMatt Macy dsl_get_mountpoint(dsl_dataset_t *ds, const char *dsname, char *value,
2693eda14cbcSMatt Macy char *source)
2694eda14cbcSMatt Macy {
2695eda14cbcSMatt Macy int error;
2696eda14cbcSMatt Macy dsl_pool_t *dp = ds->ds_dir->dd_pool;
2697eda14cbcSMatt Macy
2698eda14cbcSMatt Macy /* Retrieve the mountpoint value stored in the zap object */
2699eda14cbcSMatt Macy error = dsl_prop_get_ds(ds, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 1,
2700eda14cbcSMatt Macy ZAP_MAXVALUELEN, value, source);
2701eda14cbcSMatt Macy if (error != 0) {
2702eda14cbcSMatt Macy return (error);
2703eda14cbcSMatt Macy }
2704eda14cbcSMatt Macy
2705eda14cbcSMatt Macy /*
2706eda14cbcSMatt Macy * Process the dsname and source to find the full mountpoint string.
2707eda14cbcSMatt Macy * Can be skipped for 'legacy' or 'none'.
2708eda14cbcSMatt Macy */
2709eda14cbcSMatt Macy if (value[0] == '/') {
2710eda14cbcSMatt Macy char *buf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
2711eda14cbcSMatt Macy char *root = buf;
2712eda14cbcSMatt Macy const char *relpath;
2713eda14cbcSMatt Macy
2714eda14cbcSMatt Macy /*
2715eda14cbcSMatt Macy * If we inherit the mountpoint, even from a dataset
2716eda14cbcSMatt Macy * with a received value, the source will be the path of
2717eda14cbcSMatt Macy * the dataset we inherit from. If source is
2718eda14cbcSMatt Macy * ZPROP_SOURCE_VAL_RECVD, the received value is not
2719eda14cbcSMatt Macy * inherited.
2720eda14cbcSMatt Macy */
2721eda14cbcSMatt Macy if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2722eda14cbcSMatt Macy relpath = "";
2723eda14cbcSMatt Macy } else {
2724eda14cbcSMatt Macy ASSERT0(strncmp(dsname, source, strlen(source)));
2725eda14cbcSMatt Macy relpath = dsname + strlen(source);
2726eda14cbcSMatt Macy if (relpath[0] == '/')
2727eda14cbcSMatt Macy relpath++;
2728eda14cbcSMatt Macy }
2729eda14cbcSMatt Macy
2730eda14cbcSMatt Macy spa_altroot(dp->dp_spa, root, ZAP_MAXVALUELEN);
2731eda14cbcSMatt Macy
2732eda14cbcSMatt Macy /*
2733eda14cbcSMatt Macy * Special case an alternate root of '/'. This will
2734eda14cbcSMatt Macy * avoid having multiple leading slashes in the
2735eda14cbcSMatt Macy * mountpoint path.
2736eda14cbcSMatt Macy */
2737eda14cbcSMatt Macy if (strcmp(root, "/") == 0)
2738eda14cbcSMatt Macy root++;
2739eda14cbcSMatt Macy
2740eda14cbcSMatt Macy /*
2741eda14cbcSMatt Macy * If the mountpoint is '/' then skip over this
2742eda14cbcSMatt Macy * if we are obtaining either an alternate root or
2743eda14cbcSMatt Macy * an inherited mountpoint.
2744eda14cbcSMatt Macy */
2745eda14cbcSMatt Macy char *mnt = value;
2746eda14cbcSMatt Macy if (value[1] == '\0' && (root[0] != '\0' ||
2747eda14cbcSMatt Macy relpath[0] != '\0'))
2748eda14cbcSMatt Macy mnt = value + 1;
2749eda14cbcSMatt Macy
2750be181ee2SMartin Matuska mnt = kmem_strdup(mnt);
2751be181ee2SMartin Matuska
2752eda14cbcSMatt Macy if (relpath[0] == '\0') {
2753eda14cbcSMatt Macy (void) snprintf(value, ZAP_MAXVALUELEN, "%s%s",
2754eda14cbcSMatt Macy root, mnt);
2755eda14cbcSMatt Macy } else {
2756eda14cbcSMatt Macy (void) snprintf(value, ZAP_MAXVALUELEN, "%s%s%s%s",
2757eda14cbcSMatt Macy root, mnt, relpath[0] == '@' ? "" : "/",
2758eda14cbcSMatt Macy relpath);
2759eda14cbcSMatt Macy }
2760eda14cbcSMatt Macy kmem_free(buf, ZAP_MAXVALUELEN);
2761be181ee2SMartin Matuska kmem_strfree(mnt);
2762eda14cbcSMatt Macy }
2763eda14cbcSMatt Macy
2764eda14cbcSMatt Macy return (0);
2765eda14cbcSMatt Macy }
2766eda14cbcSMatt Macy
2767eda14cbcSMatt Macy void
dsl_dataset_stats(dsl_dataset_t * ds,nvlist_t * nv)2768eda14cbcSMatt Macy dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
2769eda14cbcSMatt Macy {
2770c03c5b1cSMartin Matuska dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool;
2771eda14cbcSMatt Macy
2772eda14cbcSMatt Macy ASSERT(dsl_pool_config_held(dp));
2773eda14cbcSMatt Macy
2774eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO,
2775eda14cbcSMatt Macy dsl_get_refratio(ds));
2776eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
2777eda14cbcSMatt Macy dsl_get_logicalreferenced(ds));
2778eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
2779eda14cbcSMatt Macy dsl_get_compressratio(ds));
2780eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
2781eda14cbcSMatt Macy dsl_get_used(ds));
2782eda14cbcSMatt Macy
2783eda14cbcSMatt Macy if (ds->ds_is_snapshot) {
2784eda14cbcSMatt Macy get_clones_stat(ds, nv);
2785eda14cbcSMatt Macy } else {
2786eda14cbcSMatt Macy char buf[ZFS_MAX_DATASET_NAME_LEN];
2787eda14cbcSMatt Macy if (dsl_get_prev_snap(ds, buf) == 0)
2788eda14cbcSMatt Macy dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP,
2789eda14cbcSMatt Macy buf);
2790eda14cbcSMatt Macy dsl_dir_stats(ds->ds_dir, nv);
2791eda14cbcSMatt Macy }
2792eda14cbcSMatt Macy
2793eda14cbcSMatt Macy nvlist_t *propval = fnvlist_alloc();
2794eda14cbcSMatt Macy dsl_get_redact_snaps(ds, propval);
2795eda14cbcSMatt Macy fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS),
2796eda14cbcSMatt Macy propval);
2797eda14cbcSMatt Macy nvlist_free(propval);
2798eda14cbcSMatt Macy
2799eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE,
2800eda14cbcSMatt Macy dsl_get_available(ds));
2801eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED,
2802eda14cbcSMatt Macy dsl_get_referenced(ds));
2803eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
2804eda14cbcSMatt Macy dsl_get_creation(ds));
2805eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
2806eda14cbcSMatt Macy dsl_get_creationtxg(ds));
2807eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
2808eda14cbcSMatt Macy dsl_get_refquota(ds));
2809eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
2810eda14cbcSMatt Macy dsl_get_refreservation(ds));
2811eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
2812eda14cbcSMatt Macy dsl_get_guid(ds));
2813eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
2814eda14cbcSMatt Macy dsl_get_unique(ds));
2815eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
2816eda14cbcSMatt Macy dsl_get_objsetid(ds));
2817eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
2818eda14cbcSMatt Macy dsl_get_userrefs(ds));
2819eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
2820eda14cbcSMatt Macy dsl_get_defer_destroy(ds));
2821271171e0SMartin Matuska dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOTS_CHANGED,
2822271171e0SMartin Matuska dsl_dir_snap_cmtime(ds->ds_dir).tv_sec);
2823eda14cbcSMatt Macy dsl_dataset_crypt_stats(ds, nv);
2824eda14cbcSMatt Macy
2825eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
2826eda14cbcSMatt Macy uint64_t written;
2827eda14cbcSMatt Macy if (dsl_get_written(ds, &written) == 0) {
2828eda14cbcSMatt Macy dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
2829eda14cbcSMatt Macy written);
2830eda14cbcSMatt Macy }
2831eda14cbcSMatt Macy }
2832eda14cbcSMatt Macy
2833eda14cbcSMatt Macy if (!dsl_dataset_is_snapshot(ds)) {
2834c03c5b1cSMartin Matuska char *token = get_receive_resume_token(ds);
2835c03c5b1cSMartin Matuska if (token != NULL) {
2836c03c5b1cSMartin Matuska dsl_prop_nvlist_add_string(nv,
2837c03c5b1cSMartin Matuska ZFS_PROP_RECEIVE_RESUME_TOKEN, token);
2838c03c5b1cSMartin Matuska kmem_strfree(token);
2839eda14cbcSMatt Macy }
2840eda14cbcSMatt Macy }
2841eda14cbcSMatt Macy }
2842eda14cbcSMatt Macy
2843eda14cbcSMatt Macy void
dsl_dataset_fast_stat(dsl_dataset_t * ds,dmu_objset_stats_t * stat)2844eda14cbcSMatt Macy dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2845eda14cbcSMatt Macy {
2846eda14cbcSMatt Macy dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool;
2847eda14cbcSMatt Macy ASSERT(dsl_pool_config_held(dp));
2848eda14cbcSMatt Macy
2849eda14cbcSMatt Macy stat->dds_creation_txg = dsl_get_creationtxg(ds);
2850eda14cbcSMatt Macy stat->dds_inconsistent = dsl_get_inconsistent(ds);
2851eda14cbcSMatt Macy stat->dds_guid = dsl_get_guid(ds);
2852eda14cbcSMatt Macy stat->dds_redacted = dsl_get_redacted(ds);
2853eda14cbcSMatt Macy stat->dds_origin[0] = '\0';
2854eda14cbcSMatt Macy if (ds->ds_is_snapshot) {
2855eda14cbcSMatt Macy stat->dds_is_snapshot = B_TRUE;
2856eda14cbcSMatt Macy stat->dds_num_clones = dsl_get_numclones(ds);
2857eda14cbcSMatt Macy } else {
2858eda14cbcSMatt Macy stat->dds_is_snapshot = B_FALSE;
2859eda14cbcSMatt Macy stat->dds_num_clones = 0;
2860eda14cbcSMatt Macy
2861eda14cbcSMatt Macy if (dsl_dir_is_clone(ds->ds_dir)) {
2862eda14cbcSMatt Macy dsl_dir_get_origin(ds->ds_dir, stat->dds_origin);
2863eda14cbcSMatt Macy }
2864eda14cbcSMatt Macy }
2865eda14cbcSMatt Macy }
2866eda14cbcSMatt Macy
2867eda14cbcSMatt Macy uint64_t
dsl_dataset_fsid_guid(dsl_dataset_t * ds)2868eda14cbcSMatt Macy dsl_dataset_fsid_guid(dsl_dataset_t *ds)
2869eda14cbcSMatt Macy {
2870eda14cbcSMatt Macy return (ds->ds_fsid_guid);
2871eda14cbcSMatt Macy }
2872eda14cbcSMatt Macy
2873eda14cbcSMatt Macy void
dsl_dataset_space(dsl_dataset_t * ds,uint64_t * refdbytesp,uint64_t * availbytesp,uint64_t * usedobjsp,uint64_t * availobjsp)2874eda14cbcSMatt Macy dsl_dataset_space(dsl_dataset_t *ds,
2875eda14cbcSMatt Macy uint64_t *refdbytesp, uint64_t *availbytesp,
2876eda14cbcSMatt Macy uint64_t *usedobjsp, uint64_t *availobjsp)
2877eda14cbcSMatt Macy {
2878eda14cbcSMatt Macy *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
2879eda14cbcSMatt Macy *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
2880eda14cbcSMatt Macy if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
2881eda14cbcSMatt Macy *availbytesp +=
2882eda14cbcSMatt Macy ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
2883eda14cbcSMatt Macy if (ds->ds_quota != 0) {
2884eda14cbcSMatt Macy /*
2885eda14cbcSMatt Macy * Adjust available bytes according to refquota
2886eda14cbcSMatt Macy */
2887eda14cbcSMatt Macy if (*refdbytesp < ds->ds_quota)
2888eda14cbcSMatt Macy *availbytesp = MIN(*availbytesp,
2889eda14cbcSMatt Macy ds->ds_quota - *refdbytesp);
2890eda14cbcSMatt Macy else
2891eda14cbcSMatt Macy *availbytesp = 0;
2892eda14cbcSMatt Macy }
2893eda14cbcSMatt Macy rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2894eda14cbcSMatt Macy *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
2895eda14cbcSMatt Macy rrw_exit(&ds->ds_bp_rwlock, FTAG);
2896eda14cbcSMatt Macy *availobjsp = DN_MAX_OBJECT - *usedobjsp;
2897eda14cbcSMatt Macy }
2898eda14cbcSMatt Macy
2899eda14cbcSMatt Macy boolean_t
dsl_dataset_modified_since_snap(dsl_dataset_t * ds,dsl_dataset_t * snap)2900eda14cbcSMatt Macy dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
2901eda14cbcSMatt Macy {
2902eda14cbcSMatt Macy dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool;
2903eda14cbcSMatt Macy uint64_t birth;
2904eda14cbcSMatt Macy
2905eda14cbcSMatt Macy ASSERT(dsl_pool_config_held(dp));
2906eda14cbcSMatt Macy if (snap == NULL)
2907eda14cbcSMatt Macy return (B_FALSE);
2908eda14cbcSMatt Macy rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2909783d3ff6SMartin Matuska birth = BP_GET_LOGICAL_BIRTH(dsl_dataset_get_blkptr(ds));
2910eda14cbcSMatt Macy rrw_exit(&ds->ds_bp_rwlock, FTAG);
2911eda14cbcSMatt Macy if (birth > dsl_dataset_phys(snap)->ds_creation_txg) {
2912eda14cbcSMatt Macy objset_t *os, *os_snap;
2913eda14cbcSMatt Macy /*
2914eda14cbcSMatt Macy * It may be that only the ZIL differs, because it was
2915eda14cbcSMatt Macy * reset in the head. Don't count that as being
2916eda14cbcSMatt Macy * modified.
2917eda14cbcSMatt Macy */
2918eda14cbcSMatt Macy if (dmu_objset_from_ds(ds, &os) != 0)
2919eda14cbcSMatt Macy return (B_TRUE);
2920eda14cbcSMatt Macy if (dmu_objset_from_ds(snap, &os_snap) != 0)
2921eda14cbcSMatt Macy return (B_TRUE);
2922da5137abSMartin Matuska return (memcmp(&os->os_phys->os_meta_dnode,
2923eda14cbcSMatt Macy &os_snap->os_phys->os_meta_dnode,
2924eda14cbcSMatt Macy sizeof (os->os_phys->os_meta_dnode)) != 0);
2925eda14cbcSMatt Macy }
2926eda14cbcSMatt Macy return (B_FALSE);
2927eda14cbcSMatt Macy }
2928eda14cbcSMatt Macy
2929eda14cbcSMatt Macy static int
dsl_dataset_rename_snapshot_check_impl(dsl_pool_t * dp,dsl_dataset_t * hds,void * arg)2930eda14cbcSMatt Macy dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
2931eda14cbcSMatt Macy dsl_dataset_t *hds, void *arg)
2932eda14cbcSMatt Macy {
2933e92ffd9bSMartin Matuska (void) dp;
2934eda14cbcSMatt Macy dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2935eda14cbcSMatt Macy int error;
2936eda14cbcSMatt Macy uint64_t val;
2937eda14cbcSMatt Macy
2938eda14cbcSMatt Macy error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
2939eda14cbcSMatt Macy if (error != 0) {
2940eda14cbcSMatt Macy /* ignore nonexistent snapshots */
2941eda14cbcSMatt Macy return (error == ENOENT ? 0 : error);
2942eda14cbcSMatt Macy }
2943eda14cbcSMatt Macy
2944eda14cbcSMatt Macy /* new name should not exist */
2945eda14cbcSMatt Macy error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
2946eda14cbcSMatt Macy if (error == 0)
2947eda14cbcSMatt Macy error = SET_ERROR(EEXIST);
2948eda14cbcSMatt Macy else if (error == ENOENT)
2949eda14cbcSMatt Macy error = 0;
2950eda14cbcSMatt Macy
2951eda14cbcSMatt Macy /* dataset name + 1 for the "@" + the new snapshot name must fit */
2952eda14cbcSMatt Macy if (dsl_dir_namelen(hds->ds_dir) + 1 +
2953eda14cbcSMatt Macy strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN)
2954eda14cbcSMatt Macy error = SET_ERROR(ENAMETOOLONG);
2955eda14cbcSMatt Macy
2956eda14cbcSMatt Macy return (error);
2957eda14cbcSMatt Macy }
2958eda14cbcSMatt Macy
2959c7046f76SMartin Matuska int
dsl_dataset_rename_snapshot_check(void * arg,dmu_tx_t * tx)2960eda14cbcSMatt Macy dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
2961eda14cbcSMatt Macy {
2962eda14cbcSMatt Macy dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2963eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
2964eda14cbcSMatt Macy dsl_dataset_t *hds;
2965eda14cbcSMatt Macy int error;
2966eda14cbcSMatt Macy
2967eda14cbcSMatt Macy error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
2968eda14cbcSMatt Macy if (error != 0)
2969eda14cbcSMatt Macy return (error);
2970eda14cbcSMatt Macy
2971eda14cbcSMatt Macy if (ddrsa->ddrsa_recursive) {
2972eda14cbcSMatt Macy error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
2973eda14cbcSMatt Macy dsl_dataset_rename_snapshot_check_impl, ddrsa,
2974eda14cbcSMatt Macy DS_FIND_CHILDREN);
2975eda14cbcSMatt Macy } else {
2976eda14cbcSMatt Macy error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
2977eda14cbcSMatt Macy }
2978eda14cbcSMatt Macy dsl_dataset_rele(hds, FTAG);
2979eda14cbcSMatt Macy return (error);
2980eda14cbcSMatt Macy }
2981eda14cbcSMatt Macy
2982eda14cbcSMatt Macy static int
dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t * dp,dsl_dataset_t * hds,void * arg)2983eda14cbcSMatt Macy dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
2984eda14cbcSMatt Macy dsl_dataset_t *hds, void *arg)
2985eda14cbcSMatt Macy {
2986eda14cbcSMatt Macy dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2987eda14cbcSMatt Macy dsl_dataset_t *ds;
2988eda14cbcSMatt Macy uint64_t val;
2989eda14cbcSMatt Macy dmu_tx_t *tx = ddrsa->ddrsa_tx;
2990*87bf66d4SMartin Matuska char *oldname, *newname;
2991eda14cbcSMatt Macy int error;
2992eda14cbcSMatt Macy
2993eda14cbcSMatt Macy error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
2994eda14cbcSMatt Macy ASSERT(error == 0 || error == ENOENT);
2995eda14cbcSMatt Macy if (error == ENOENT) {
2996eda14cbcSMatt Macy /* ignore nonexistent snapshots */
2997eda14cbcSMatt Macy return (0);
2998eda14cbcSMatt Macy }
2999eda14cbcSMatt Macy
3000eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
3001eda14cbcSMatt Macy
3002eda14cbcSMatt Macy /* log before we change the name */
3003eda14cbcSMatt Macy spa_history_log_internal_ds(ds, "rename", tx,
3004eda14cbcSMatt Macy "-> @%s", ddrsa->ddrsa_newsnapname);
3005eda14cbcSMatt Macy
3006eda14cbcSMatt Macy VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
3007eda14cbcSMatt Macy B_FALSE));
3008eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
3009eda14cbcSMatt Macy (void) strlcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname,
3010eda14cbcSMatt Macy sizeof (ds->ds_snapname));
3011eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
3012eda14cbcSMatt Macy VERIFY0(zap_add(dp->dp_meta_objset,
3013eda14cbcSMatt Macy dsl_dataset_phys(hds)->ds_snapnames_zapobj,
3014eda14cbcSMatt Macy ds->ds_snapname, 8, 1, &ds->ds_object, tx));
3015*87bf66d4SMartin Matuska
3016*87bf66d4SMartin Matuska oldname = kmem_asprintf("%s@%s", ddrsa->ddrsa_fsname,
3017*87bf66d4SMartin Matuska ddrsa->ddrsa_oldsnapname);
3018*87bf66d4SMartin Matuska newname = kmem_asprintf("%s@%s", ddrsa->ddrsa_fsname,
3019*87bf66d4SMartin Matuska ddrsa->ddrsa_newsnapname);
3020*87bf66d4SMartin Matuska zvol_rename_minors(dp->dp_spa, oldname, newname, B_TRUE);
3021*87bf66d4SMartin Matuska kmem_strfree(oldname);
3022*87bf66d4SMartin Matuska kmem_strfree(newname);
3023eda14cbcSMatt Macy
3024eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3025eda14cbcSMatt Macy return (0);
3026eda14cbcSMatt Macy }
3027eda14cbcSMatt Macy
3028c7046f76SMartin Matuska void
dsl_dataset_rename_snapshot_sync(void * arg,dmu_tx_t * tx)3029eda14cbcSMatt Macy dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
3030eda14cbcSMatt Macy {
3031eda14cbcSMatt Macy dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
3032eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
3033eda14cbcSMatt Macy dsl_dataset_t *hds = NULL;
3034eda14cbcSMatt Macy
3035eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
3036eda14cbcSMatt Macy ddrsa->ddrsa_tx = tx;
3037eda14cbcSMatt Macy if (ddrsa->ddrsa_recursive) {
3038eda14cbcSMatt Macy VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
3039eda14cbcSMatt Macy dsl_dataset_rename_snapshot_sync_impl, ddrsa,
3040eda14cbcSMatt Macy DS_FIND_CHILDREN));
3041eda14cbcSMatt Macy } else {
3042eda14cbcSMatt Macy VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
3043eda14cbcSMatt Macy }
3044eda14cbcSMatt Macy dsl_dataset_rele(hds, FTAG);
3045eda14cbcSMatt Macy }
3046eda14cbcSMatt Macy
3047eda14cbcSMatt Macy int
dsl_dataset_rename_snapshot(const char * fsname,const char * oldsnapname,const char * newsnapname,boolean_t recursive)3048eda14cbcSMatt Macy dsl_dataset_rename_snapshot(const char *fsname,
3049eda14cbcSMatt Macy const char *oldsnapname, const char *newsnapname, boolean_t recursive)
3050eda14cbcSMatt Macy {
3051eda14cbcSMatt Macy dsl_dataset_rename_snapshot_arg_t ddrsa;
3052eda14cbcSMatt Macy
3053eda14cbcSMatt Macy ddrsa.ddrsa_fsname = fsname;
3054eda14cbcSMatt Macy ddrsa.ddrsa_oldsnapname = oldsnapname;
3055eda14cbcSMatt Macy ddrsa.ddrsa_newsnapname = newsnapname;
3056eda14cbcSMatt Macy ddrsa.ddrsa_recursive = recursive;
3057eda14cbcSMatt Macy
3058eda14cbcSMatt Macy return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
3059eda14cbcSMatt Macy dsl_dataset_rename_snapshot_sync, &ddrsa,
3060eda14cbcSMatt Macy 1, ZFS_SPACE_CHECK_RESERVED));
3061eda14cbcSMatt Macy }
3062eda14cbcSMatt Macy
3063eda14cbcSMatt Macy /*
3064eda14cbcSMatt Macy * If we're doing an ownership handoff, we need to make sure that there is
3065eda14cbcSMatt Macy * only one long hold on the dataset. We're not allowed to change anything here
3066eda14cbcSMatt Macy * so we don't permanently release the long hold or regular hold here. We want
3067eda14cbcSMatt Macy * to do this only when syncing to avoid the dataset unexpectedly going away
3068eda14cbcSMatt Macy * when we release the long hold.
3069eda14cbcSMatt Macy */
3070eda14cbcSMatt Macy static int
dsl_dataset_handoff_check(dsl_dataset_t * ds,void * owner,dmu_tx_t * tx)3071eda14cbcSMatt Macy dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
3072eda14cbcSMatt Macy {
3073eda14cbcSMatt Macy boolean_t held = B_FALSE;
3074eda14cbcSMatt Macy
3075eda14cbcSMatt Macy if (!dmu_tx_is_syncing(tx))
3076eda14cbcSMatt Macy return (0);
3077eda14cbcSMatt Macy
3078eda14cbcSMatt Macy dsl_dir_t *dd = ds->ds_dir;
3079eda14cbcSMatt Macy mutex_enter(&dd->dd_activity_lock);
3080eda14cbcSMatt Macy uint64_t holds = zfs_refcount_count(&ds->ds_longholds) -
3081eda14cbcSMatt Macy (owner != NULL ? 1 : 0);
3082eda14cbcSMatt Macy /*
3083eda14cbcSMatt Macy * The value of dd_activity_waiters can chance as soon as we drop the
3084eda14cbcSMatt Macy * lock, but we're fine with that; new waiters coming in or old
3085eda14cbcSMatt Macy * waiters leaving doesn't cause problems, since we're going to cancel
3086eda14cbcSMatt Macy * waiters later anyway. The goal of this check is to verify that no
3087eda14cbcSMatt Macy * non-waiters have long-holds, and all new long-holds will be
3088eda14cbcSMatt Macy * prevented because we're holding the pool config as writer.
3089eda14cbcSMatt Macy */
3090eda14cbcSMatt Macy if (holds != dd->dd_activity_waiters)
3091eda14cbcSMatt Macy held = B_TRUE;
3092eda14cbcSMatt Macy mutex_exit(&dd->dd_activity_lock);
3093eda14cbcSMatt Macy
3094eda14cbcSMatt Macy if (held)
3095eda14cbcSMatt Macy return (SET_ERROR(EBUSY));
3096eda14cbcSMatt Macy
3097eda14cbcSMatt Macy return (0);
3098eda14cbcSMatt Macy }
3099eda14cbcSMatt Macy
3100eda14cbcSMatt Macy int
dsl_dataset_rollback_check(void * arg,dmu_tx_t * tx)3101eda14cbcSMatt Macy dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
3102eda14cbcSMatt Macy {
3103eda14cbcSMatt Macy dsl_dataset_rollback_arg_t *ddra = arg;
3104eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
3105eda14cbcSMatt Macy dsl_dataset_t *ds;
3106eda14cbcSMatt Macy int64_t unused_refres_delta;
3107eda14cbcSMatt Macy int error;
3108eda14cbcSMatt Macy
3109eda14cbcSMatt Macy error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
3110eda14cbcSMatt Macy if (error != 0)
3111eda14cbcSMatt Macy return (error);
3112eda14cbcSMatt Macy
3113eda14cbcSMatt Macy /* must not be a snapshot */
3114eda14cbcSMatt Macy if (ds->ds_is_snapshot) {
3115eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3116eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
3117eda14cbcSMatt Macy }
3118eda14cbcSMatt Macy
3119eda14cbcSMatt Macy /* must have a most recent snapshot */
3120eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
3121eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3122eda14cbcSMatt Macy return (SET_ERROR(ESRCH));
3123eda14cbcSMatt Macy }
3124eda14cbcSMatt Macy
3125eda14cbcSMatt Macy /*
3126eda14cbcSMatt Macy * No rollback to a snapshot created in the current txg, because
3127eda14cbcSMatt Macy * the rollback may dirty the dataset and create blocks that are
3128eda14cbcSMatt Macy * not reachable from the rootbp while having a birth txg that
3129eda14cbcSMatt Macy * falls into the snapshot's range.
3130eda14cbcSMatt Macy */
3131eda14cbcSMatt Macy if (dmu_tx_is_syncing(tx) &&
3132eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) {
3133eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3134eda14cbcSMatt Macy return (SET_ERROR(EAGAIN));
3135eda14cbcSMatt Macy }
3136eda14cbcSMatt Macy
3137eda14cbcSMatt Macy /*
3138eda14cbcSMatt Macy * If the expected target snapshot is specified, then check that
3139eda14cbcSMatt Macy * the latest snapshot is it.
3140eda14cbcSMatt Macy */
3141eda14cbcSMatt Macy if (ddra->ddra_tosnap != NULL) {
3142eda14cbcSMatt Macy dsl_dataset_t *snapds;
3143eda14cbcSMatt Macy
3144eda14cbcSMatt Macy /* Check if the target snapshot exists at all. */
3145eda14cbcSMatt Macy error = dsl_dataset_hold(dp, ddra->ddra_tosnap, FTAG, &snapds);
3146eda14cbcSMatt Macy if (error != 0) {
3147eda14cbcSMatt Macy /*
3148eda14cbcSMatt Macy * ESRCH is used to signal that the target snapshot does
3149eda14cbcSMatt Macy * not exist, while ENOENT is used to report that
3150eda14cbcSMatt Macy * the rolled back dataset does not exist.
3151eda14cbcSMatt Macy * ESRCH is also used to cover other cases where the
3152eda14cbcSMatt Macy * target snapshot is not related to the dataset being
3153eda14cbcSMatt Macy * rolled back such as being in a different pool.
3154eda14cbcSMatt Macy */
3155eda14cbcSMatt Macy if (error == ENOENT || error == EXDEV)
3156eda14cbcSMatt Macy error = SET_ERROR(ESRCH);
3157eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3158eda14cbcSMatt Macy return (error);
3159eda14cbcSMatt Macy }
3160eda14cbcSMatt Macy ASSERT(snapds->ds_is_snapshot);
3161eda14cbcSMatt Macy
3162eda14cbcSMatt Macy /* Check if the snapshot is the latest snapshot indeed. */
3163eda14cbcSMatt Macy if (snapds != ds->ds_prev) {
3164eda14cbcSMatt Macy /*
3165eda14cbcSMatt Macy * Distinguish between the case where the only problem
3166eda14cbcSMatt Macy * is intervening snapshots (EEXIST) vs the snapshot
3167eda14cbcSMatt Macy * not being a valid target for rollback (ESRCH).
3168eda14cbcSMatt Macy */
3169eda14cbcSMatt Macy if (snapds->ds_dir == ds->ds_dir ||
3170eda14cbcSMatt Macy (dsl_dir_is_clone(ds->ds_dir) &&
3171eda14cbcSMatt Macy dsl_dir_phys(ds->ds_dir)->dd_origin_obj ==
3172eda14cbcSMatt Macy snapds->ds_object)) {
3173eda14cbcSMatt Macy error = SET_ERROR(EEXIST);
3174eda14cbcSMatt Macy } else {
3175eda14cbcSMatt Macy error = SET_ERROR(ESRCH);
3176eda14cbcSMatt Macy }
3177eda14cbcSMatt Macy dsl_dataset_rele(snapds, FTAG);
3178eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3179eda14cbcSMatt Macy return (error);
3180eda14cbcSMatt Macy }
3181eda14cbcSMatt Macy dsl_dataset_rele(snapds, FTAG);
3182eda14cbcSMatt Macy }
3183eda14cbcSMatt Macy
3184eda14cbcSMatt Macy /* must not have any bookmarks after the most recent snapshot */
3185eda14cbcSMatt Macy if (dsl_bookmark_latest_txg(ds) >
3186eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_txg) {
3187eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3188eda14cbcSMatt Macy return (SET_ERROR(EEXIST));
3189eda14cbcSMatt Macy }
3190eda14cbcSMatt Macy
3191eda14cbcSMatt Macy error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
3192eda14cbcSMatt Macy if (error != 0) {
3193eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3194eda14cbcSMatt Macy return (error);
3195eda14cbcSMatt Macy }
3196eda14cbcSMatt Macy
3197eda14cbcSMatt Macy /*
3198eda14cbcSMatt Macy * Check if the snap we are rolling back to uses more than
3199eda14cbcSMatt Macy * the refquota.
3200eda14cbcSMatt Macy */
3201eda14cbcSMatt Macy if (ds->ds_quota != 0 &&
3202eda14cbcSMatt Macy dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
3203eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3204eda14cbcSMatt Macy return (SET_ERROR(EDQUOT));
3205eda14cbcSMatt Macy }
3206eda14cbcSMatt Macy
3207eda14cbcSMatt Macy /*
3208eda14cbcSMatt Macy * When we do the clone swap, we will temporarily use more space
3209eda14cbcSMatt Macy * due to the refreservation (the head will no longer have any
3210eda14cbcSMatt Macy * unique space, so the entire amount of the refreservation will need
3211eda14cbcSMatt Macy * to be free). We will immediately destroy the clone, freeing
3212eda14cbcSMatt Macy * this space, but the freeing happens over many txg's.
3213eda14cbcSMatt Macy */
3214eda14cbcSMatt Macy unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
3215eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_unique_bytes);
3216eda14cbcSMatt Macy
3217eda14cbcSMatt Macy if (unused_refres_delta > 0 &&
3218eda14cbcSMatt Macy unused_refres_delta >
3219eda14cbcSMatt Macy dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
3220eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3221eda14cbcSMatt Macy return (SET_ERROR(ENOSPC));
3222eda14cbcSMatt Macy }
3223eda14cbcSMatt Macy
3224eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3225eda14cbcSMatt Macy return (0);
3226eda14cbcSMatt Macy }
3227eda14cbcSMatt Macy
3228eda14cbcSMatt Macy void
dsl_dataset_rollback_sync(void * arg,dmu_tx_t * tx)3229eda14cbcSMatt Macy dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
3230eda14cbcSMatt Macy {
3231eda14cbcSMatt Macy dsl_dataset_rollback_arg_t *ddra = arg;
3232eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
3233eda14cbcSMatt Macy dsl_dataset_t *ds, *clone;
3234eda14cbcSMatt Macy uint64_t cloneobj;
3235eda14cbcSMatt Macy char namebuf[ZFS_MAX_DATASET_NAME_LEN];
3236eda14cbcSMatt Macy
3237eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
3238eda14cbcSMatt Macy
3239eda14cbcSMatt Macy dsl_dataset_name(ds->ds_prev, namebuf);
3240eda14cbcSMatt Macy fnvlist_add_string(ddra->ddra_result, "target", namebuf);
3241eda14cbcSMatt Macy
3242eda14cbcSMatt Macy cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
3243eda14cbcSMatt Macy ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, NULL, tx);
3244eda14cbcSMatt Macy
3245eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
3246eda14cbcSMatt Macy
3247eda14cbcSMatt Macy dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
3248eda14cbcSMatt Macy dsl_dataset_zero_zil(ds, tx);
3249eda14cbcSMatt Macy
3250eda14cbcSMatt Macy dsl_destroy_head_sync_impl(clone, tx);
3251eda14cbcSMatt Macy
3252eda14cbcSMatt Macy dsl_dataset_rele(clone, FTAG);
3253eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
3254eda14cbcSMatt Macy }
3255eda14cbcSMatt Macy
3256eda14cbcSMatt Macy /*
3257eda14cbcSMatt Macy * Rolls back the given filesystem or volume to the most recent snapshot.
3258eda14cbcSMatt Macy * The name of the most recent snapshot will be returned under key "target"
3259eda14cbcSMatt Macy * in the result nvlist.
3260eda14cbcSMatt Macy *
3261eda14cbcSMatt Macy * If owner != NULL:
3262eda14cbcSMatt Macy * - The existing dataset MUST be owned by the specified owner at entry
3263eda14cbcSMatt Macy * - Upon return, dataset will still be held by the same owner, whether we
3264eda14cbcSMatt Macy * succeed or not.
3265eda14cbcSMatt Macy *
3266eda14cbcSMatt Macy * This mode is required any time the existing filesystem is mounted. See
3267eda14cbcSMatt Macy * notes above zfs_suspend_fs() for further details.
3268eda14cbcSMatt Macy */
3269eda14cbcSMatt Macy int
dsl_dataset_rollback(const char * fsname,const char * tosnap,void * owner,nvlist_t * result)3270eda14cbcSMatt Macy dsl_dataset_rollback(const char *fsname, const char *tosnap, void *owner,
3271eda14cbcSMatt Macy nvlist_t *result)
3272eda14cbcSMatt Macy {
3273eda14cbcSMatt Macy dsl_dataset_rollback_arg_t ddra;
3274eda14cbcSMatt Macy
3275eda14cbcSMatt Macy ddra.ddra_fsname = fsname;
3276eda14cbcSMatt Macy ddra.ddra_tosnap = tosnap;
3277eda14cbcSMatt Macy ddra.ddra_owner = owner;
3278eda14cbcSMatt Macy ddra.ddra_result = result;
3279eda14cbcSMatt Macy
3280eda14cbcSMatt Macy return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
3281eda14cbcSMatt Macy dsl_dataset_rollback_sync, &ddra,
3282eda14cbcSMatt Macy 1, ZFS_SPACE_CHECK_RESERVED));
3283eda14cbcSMatt Macy }
3284eda14cbcSMatt Macy
3285eda14cbcSMatt Macy struct promotenode {
3286eda14cbcSMatt Macy list_node_t link;
3287eda14cbcSMatt Macy dsl_dataset_t *ds;
3288eda14cbcSMatt Macy };
3289eda14cbcSMatt Macy
3290eda14cbcSMatt Macy static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
3291eda14cbcSMatt Macy static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
3292a0b956f5SMartin Matuska const void *tag);
3293a0b956f5SMartin Matuska static void promote_rele(dsl_dataset_promote_arg_t *ddpa, const void *tag);
3294eda14cbcSMatt Macy
3295eda14cbcSMatt Macy int
dsl_dataset_promote_check(void * arg,dmu_tx_t * tx)3296eda14cbcSMatt Macy dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
3297eda14cbcSMatt Macy {
3298eda14cbcSMatt Macy dsl_dataset_promote_arg_t *ddpa = arg;
3299eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
3300eda14cbcSMatt Macy dsl_dataset_t *hds;
3301eda14cbcSMatt Macy struct promotenode *snap;
3302eda14cbcSMatt Macy int err;
3303eda14cbcSMatt Macy uint64_t unused;
3304eda14cbcSMatt Macy uint64_t ss_mv_cnt;
3305eda14cbcSMatt Macy size_t max_snap_len;
3306eda14cbcSMatt Macy boolean_t conflicting_snaps;
3307eda14cbcSMatt Macy
3308eda14cbcSMatt Macy err = promote_hold(ddpa, dp, FTAG);
3309eda14cbcSMatt Macy if (err != 0)
3310eda14cbcSMatt Macy return (err);
3311eda14cbcSMatt Macy
3312eda14cbcSMatt Macy hds = ddpa->ddpa_clone;
3313eda14cbcSMatt Macy max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
3314eda14cbcSMatt Macy
3315eda14cbcSMatt Macy if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
3316eda14cbcSMatt Macy promote_rele(ddpa, FTAG);
3317eda14cbcSMatt Macy return (SET_ERROR(EXDEV));
3318eda14cbcSMatt Macy }
3319eda14cbcSMatt Macy
3320eda14cbcSMatt Macy snap = list_head(&ddpa->shared_snaps);
3321eda14cbcSMatt Macy if (snap == NULL) {
3322eda14cbcSMatt Macy err = SET_ERROR(ENOENT);
3323eda14cbcSMatt Macy goto out;
3324eda14cbcSMatt Macy }
3325be181ee2SMartin Matuska dsl_dataset_t *const origin_ds = snap->ds;
3326eda14cbcSMatt Macy
3327eda14cbcSMatt Macy /*
3328eda14cbcSMatt Macy * Encrypted clones share a DSL Crypto Key with their origin's dsl dir.
3329eda14cbcSMatt Macy * When doing a promote we must make sure the encryption root for
3330eda14cbcSMatt Macy * both the target and the target's origin does not change to avoid
3331eda14cbcSMatt Macy * needing to rewrap encryption keys
3332eda14cbcSMatt Macy */
3333eda14cbcSMatt Macy err = dsl_dataset_promote_crypt_check(hds->ds_dir, origin_ds->ds_dir);
3334eda14cbcSMatt Macy if (err != 0)
3335eda14cbcSMatt Macy goto out;
3336eda14cbcSMatt Macy
3337eda14cbcSMatt Macy /*
3338eda14cbcSMatt Macy * Compute and check the amount of space to transfer. Since this is
3339eda14cbcSMatt Macy * so expensive, don't do the preliminary check.
3340eda14cbcSMatt Macy */
3341eda14cbcSMatt Macy if (!dmu_tx_is_syncing(tx)) {
3342eda14cbcSMatt Macy promote_rele(ddpa, FTAG);
3343eda14cbcSMatt Macy return (0);
3344eda14cbcSMatt Macy }
3345eda14cbcSMatt Macy
3346eda14cbcSMatt Macy /* compute origin's new unique space */
3347eda14cbcSMatt Macy snap = list_tail(&ddpa->clone_snaps);
3348eda14cbcSMatt Macy ASSERT(snap != NULL);
3349eda14cbcSMatt Macy ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
3350eda14cbcSMatt Macy origin_ds->ds_object);
3351eda14cbcSMatt Macy dsl_deadlist_space_range(&snap->ds->ds_deadlist,
3352eda14cbcSMatt Macy dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
3353eda14cbcSMatt Macy &ddpa->unique, &unused, &unused);
3354eda14cbcSMatt Macy
3355eda14cbcSMatt Macy /*
3356eda14cbcSMatt Macy * Walk the snapshots that we are moving
3357eda14cbcSMatt Macy *
3358eda14cbcSMatt Macy * Compute space to transfer. Consider the incremental changes
3359eda14cbcSMatt Macy * to used by each snapshot:
3360eda14cbcSMatt Macy * (my used) = (prev's used) + (blocks born) - (blocks killed)
3361eda14cbcSMatt Macy * So each snapshot gave birth to:
3362eda14cbcSMatt Macy * (blocks born) = (my used) - (prev's used) + (blocks killed)
3363eda14cbcSMatt Macy * So a sequence would look like:
3364eda14cbcSMatt Macy * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
3365eda14cbcSMatt Macy * Which simplifies to:
3366eda14cbcSMatt Macy * uN + kN + kN-1 + ... + k1 + k0
3367eda14cbcSMatt Macy * Note however, if we stop before we reach the ORIGIN we get:
3368eda14cbcSMatt Macy * uN + kN + kN-1 + ... + kM - uM-1
3369eda14cbcSMatt Macy */
3370eda14cbcSMatt Macy conflicting_snaps = B_FALSE;
3371eda14cbcSMatt Macy ss_mv_cnt = 0;
3372eda14cbcSMatt Macy ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
3373eda14cbcSMatt Macy ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
3374eda14cbcSMatt Macy ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
3375eda14cbcSMatt Macy for (snap = list_head(&ddpa->shared_snaps); snap;
3376eda14cbcSMatt Macy snap = list_next(&ddpa->shared_snaps, snap)) {
3377eda14cbcSMatt Macy uint64_t val, dlused, dlcomp, dluncomp;
3378eda14cbcSMatt Macy dsl_dataset_t *ds = snap->ds;
3379eda14cbcSMatt Macy
3380eda14cbcSMatt Macy ss_mv_cnt++;
3381eda14cbcSMatt Macy
3382eda14cbcSMatt Macy /*
3383eda14cbcSMatt Macy * If there are long holds, we won't be able to evict
3384eda14cbcSMatt Macy * the objset.
3385eda14cbcSMatt Macy */
3386eda14cbcSMatt Macy if (dsl_dataset_long_held(ds)) {
3387eda14cbcSMatt Macy err = SET_ERROR(EBUSY);
3388eda14cbcSMatt Macy goto out;
3389eda14cbcSMatt Macy }
3390eda14cbcSMatt Macy
3391eda14cbcSMatt Macy /* Check that the snapshot name does not conflict */
3392eda14cbcSMatt Macy VERIFY0(dsl_dataset_get_snapname(ds));
3393eda14cbcSMatt Macy if (strlen(ds->ds_snapname) >= max_snap_len) {
3394eda14cbcSMatt Macy err = SET_ERROR(ENAMETOOLONG);
3395eda14cbcSMatt Macy goto out;
3396eda14cbcSMatt Macy }
3397eda14cbcSMatt Macy err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
3398eda14cbcSMatt Macy if (err == 0) {
3399eda14cbcSMatt Macy fnvlist_add_boolean(ddpa->err_ds,
3400eda14cbcSMatt Macy snap->ds->ds_snapname);
3401eda14cbcSMatt Macy conflicting_snaps = B_TRUE;
3402eda14cbcSMatt Macy } else if (err != ENOENT) {
3403eda14cbcSMatt Macy goto out;
3404eda14cbcSMatt Macy }
3405eda14cbcSMatt Macy
3406eda14cbcSMatt Macy /* The very first snapshot does not have a deadlist */
3407eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
3408eda14cbcSMatt Macy continue;
3409eda14cbcSMatt Macy
3410eda14cbcSMatt Macy dsl_deadlist_space(&ds->ds_deadlist,
3411eda14cbcSMatt Macy &dlused, &dlcomp, &dluncomp);
3412eda14cbcSMatt Macy ddpa->used += dlused;
3413eda14cbcSMatt Macy ddpa->comp += dlcomp;
3414eda14cbcSMatt Macy ddpa->uncomp += dluncomp;
3415eda14cbcSMatt Macy }
3416eda14cbcSMatt Macy
3417eda14cbcSMatt Macy /*
3418eda14cbcSMatt Macy * Check that bookmarks that are being transferred don't have
3419eda14cbcSMatt Macy * name conflicts.
3420eda14cbcSMatt Macy */
3421be181ee2SMartin Matuska for (dsl_bookmark_node_t *dbn = avl_first(&origin_ds->ds_bookmarks);
3422eda14cbcSMatt Macy dbn != NULL && dbn->dbn_phys.zbm_creation_txg <=
3423eda14cbcSMatt Macy dsl_dataset_phys(origin_ds)->ds_creation_txg;
3424be181ee2SMartin Matuska dbn = AVL_NEXT(&origin_ds->ds_bookmarks, dbn)) {
3425eda14cbcSMatt Macy if (strlen(dbn->dbn_name) >= max_snap_len) {
3426eda14cbcSMatt Macy err = SET_ERROR(ENAMETOOLONG);
3427eda14cbcSMatt Macy goto out;
3428eda14cbcSMatt Macy }
3429eda14cbcSMatt Macy zfs_bookmark_phys_t bm;
3430eda14cbcSMatt Macy err = dsl_bookmark_lookup_impl(ddpa->ddpa_clone,
3431eda14cbcSMatt Macy dbn->dbn_name, &bm);
3432eda14cbcSMatt Macy
3433eda14cbcSMatt Macy if (err == 0) {
3434eda14cbcSMatt Macy fnvlist_add_boolean(ddpa->err_ds, dbn->dbn_name);
3435eda14cbcSMatt Macy conflicting_snaps = B_TRUE;
3436eda14cbcSMatt Macy } else if (err == ESRCH) {
3437eda14cbcSMatt Macy err = 0;
3438dbd5678dSMartin Matuska }
3439dbd5678dSMartin Matuska if (err != 0) {
3440eda14cbcSMatt Macy goto out;
3441eda14cbcSMatt Macy }
3442eda14cbcSMatt Macy }
3443eda14cbcSMatt Macy
3444eda14cbcSMatt Macy /*
3445eda14cbcSMatt Macy * In order to return the full list of conflicting snapshots, we check
3446eda14cbcSMatt Macy * whether there was a conflict after traversing all of them.
3447eda14cbcSMatt Macy */
3448eda14cbcSMatt Macy if (conflicting_snaps) {
3449eda14cbcSMatt Macy err = SET_ERROR(EEXIST);
3450eda14cbcSMatt Macy goto out;
3451eda14cbcSMatt Macy }
3452eda14cbcSMatt Macy
3453eda14cbcSMatt Macy /*
3454eda14cbcSMatt Macy * If we are a clone of a clone then we never reached ORIGIN,
3455eda14cbcSMatt Macy * so we need to subtract out the clone origin's used space.
3456eda14cbcSMatt Macy */
3457eda14cbcSMatt Macy if (ddpa->origin_origin) {
3458eda14cbcSMatt Macy ddpa->used -=
3459eda14cbcSMatt Macy dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
3460eda14cbcSMatt Macy ddpa->comp -=
3461eda14cbcSMatt Macy dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
3462eda14cbcSMatt Macy ddpa->uncomp -=
3463eda14cbcSMatt Macy dsl_dataset_phys(ddpa->origin_origin)->
3464eda14cbcSMatt Macy ds_uncompressed_bytes;
3465eda14cbcSMatt Macy }
3466eda14cbcSMatt Macy
3467eda14cbcSMatt Macy /* Check that there is enough space and limit headroom here */
3468eda14cbcSMatt Macy err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
3469eda14cbcSMatt Macy 0, ss_mv_cnt, ddpa->used, ddpa->cr, ddpa->proc);
3470eda14cbcSMatt Macy if (err != 0)
3471eda14cbcSMatt Macy goto out;
3472eda14cbcSMatt Macy
3473eda14cbcSMatt Macy /*
3474eda14cbcSMatt Macy * Compute the amounts of space that will be used by snapshots
3475eda14cbcSMatt Macy * after the promotion (for both origin and clone). For each,
3476eda14cbcSMatt Macy * it is the amount of space that will be on all of their
3477eda14cbcSMatt Macy * deadlists (that was not born before their new origin).
3478eda14cbcSMatt Macy */
3479eda14cbcSMatt Macy if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
3480eda14cbcSMatt Macy uint64_t space;
3481eda14cbcSMatt Macy
3482eda14cbcSMatt Macy /*
3483eda14cbcSMatt Macy * Note, typically this will not be a clone of a clone,
3484eda14cbcSMatt Macy * so dd_origin_txg will be < TXG_INITIAL, so
3485eda14cbcSMatt Macy * these snaplist_space() -> dsl_deadlist_space_range()
3486eda14cbcSMatt Macy * calls will be fast because they do not have to
3487eda14cbcSMatt Macy * iterate over all bps.
3488eda14cbcSMatt Macy */
3489eda14cbcSMatt Macy snap = list_head(&ddpa->origin_snaps);
3490eda14cbcSMatt Macy if (snap == NULL) {
3491eda14cbcSMatt Macy err = SET_ERROR(ENOENT);
3492eda14cbcSMatt Macy goto out;
3493eda14cbcSMatt Macy }
3494eda14cbcSMatt Macy err = snaplist_space(&ddpa->shared_snaps,
3495eda14cbcSMatt Macy snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
3496eda14cbcSMatt Macy if (err != 0)
3497eda14cbcSMatt Macy goto out;
3498eda14cbcSMatt Macy
3499eda14cbcSMatt Macy err = snaplist_space(&ddpa->clone_snaps,
3500eda14cbcSMatt Macy snap->ds->ds_dir->dd_origin_txg, &space);
3501eda14cbcSMatt Macy if (err != 0)
3502eda14cbcSMatt Macy goto out;
3503eda14cbcSMatt Macy ddpa->cloneusedsnap += space;
3504eda14cbcSMatt Macy }
3505eda14cbcSMatt Macy if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
3506eda14cbcSMatt Macy DD_FLAG_USED_BREAKDOWN) {
3507eda14cbcSMatt Macy err = snaplist_space(&ddpa->origin_snaps,
3508eda14cbcSMatt Macy dsl_dataset_phys(origin_ds)->ds_creation_txg,
3509eda14cbcSMatt Macy &ddpa->originusedsnap);
3510eda14cbcSMatt Macy if (err != 0)
3511eda14cbcSMatt Macy goto out;
3512eda14cbcSMatt Macy }
3513eda14cbcSMatt Macy
3514eda14cbcSMatt Macy out:
3515eda14cbcSMatt Macy promote_rele(ddpa, FTAG);
3516eda14cbcSMatt Macy return (err);
3517eda14cbcSMatt Macy }
3518eda14cbcSMatt Macy
3519eda14cbcSMatt Macy void
dsl_dataset_promote_sync(void * arg,dmu_tx_t * tx)3520eda14cbcSMatt Macy dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
3521eda14cbcSMatt Macy {
3522eda14cbcSMatt Macy dsl_dataset_promote_arg_t *ddpa = arg;
3523eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
3524eda14cbcSMatt Macy dsl_dataset_t *hds;
3525eda14cbcSMatt Macy struct promotenode *snap;
3526eda14cbcSMatt Macy dsl_dataset_t *origin_ds;
3527eda14cbcSMatt Macy dsl_dataset_t *origin_head;
3528eda14cbcSMatt Macy dsl_dir_t *dd;
3529eda14cbcSMatt Macy dsl_dir_t *odd = NULL;
3530eda14cbcSMatt Macy uint64_t oldnext_obj;
3531eda14cbcSMatt Macy int64_t delta;
3532eda14cbcSMatt Macy
3533eda14cbcSMatt Macy ASSERT(nvlist_empty(ddpa->err_ds));
3534eda14cbcSMatt Macy
3535eda14cbcSMatt Macy VERIFY0(promote_hold(ddpa, dp, FTAG));
3536eda14cbcSMatt Macy hds = ddpa->ddpa_clone;
3537eda14cbcSMatt Macy
3538eda14cbcSMatt Macy ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
3539eda14cbcSMatt Macy
3540eda14cbcSMatt Macy snap = list_head(&ddpa->shared_snaps);
3541eda14cbcSMatt Macy origin_ds = snap->ds;
3542eda14cbcSMatt Macy dd = hds->ds_dir;
3543eda14cbcSMatt Macy
3544eda14cbcSMatt Macy snap = list_head(&ddpa->origin_snaps);
3545eda14cbcSMatt Macy origin_head = snap->ds;
3546eda14cbcSMatt Macy
3547eda14cbcSMatt Macy /*
3548eda14cbcSMatt Macy * We need to explicitly open odd, since origin_ds's dd will be
3549eda14cbcSMatt Macy * changing.
3550eda14cbcSMatt Macy */
3551eda14cbcSMatt Macy VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
3552eda14cbcSMatt Macy NULL, FTAG, &odd));
3553eda14cbcSMatt Macy
3554eda14cbcSMatt Macy dsl_dataset_promote_crypt_sync(hds->ds_dir, odd, tx);
3555eda14cbcSMatt Macy
3556eda14cbcSMatt Macy /* change origin's next snap */
3557eda14cbcSMatt Macy dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
3558eda14cbcSMatt Macy oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
3559eda14cbcSMatt Macy snap = list_tail(&ddpa->clone_snaps);
3560eda14cbcSMatt Macy ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
3561eda14cbcSMatt Macy origin_ds->ds_object);
3562eda14cbcSMatt Macy dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
3563eda14cbcSMatt Macy
3564eda14cbcSMatt Macy /* change the origin's next clone */
3565eda14cbcSMatt Macy if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
3566eda14cbcSMatt Macy dsl_dataset_remove_from_next_clones(origin_ds,
3567eda14cbcSMatt Macy snap->ds->ds_object, tx);
3568eda14cbcSMatt Macy VERIFY0(zap_add_int(dp->dp_meta_objset,
3569eda14cbcSMatt Macy dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
3570eda14cbcSMatt Macy oldnext_obj, tx));
3571eda14cbcSMatt Macy }
3572eda14cbcSMatt Macy
3573eda14cbcSMatt Macy /* change origin */
3574eda14cbcSMatt Macy dmu_buf_will_dirty(dd->dd_dbuf, tx);
3575eda14cbcSMatt Macy ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
3576eda14cbcSMatt Macy dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
3577eda14cbcSMatt Macy dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
3578eda14cbcSMatt Macy dmu_buf_will_dirty(odd->dd_dbuf, tx);
3579eda14cbcSMatt Macy dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
3580eda14cbcSMatt Macy origin_head->ds_dir->dd_origin_txg =
3581eda14cbcSMatt Macy dsl_dataset_phys(origin_ds)->ds_creation_txg;
3582eda14cbcSMatt Macy
3583eda14cbcSMatt Macy /* change dd_clone entries */
3584eda14cbcSMatt Macy if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
3585eda14cbcSMatt Macy VERIFY0(zap_remove_int(dp->dp_meta_objset,
3586eda14cbcSMatt Macy dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
3587eda14cbcSMatt Macy VERIFY0(zap_add_int(dp->dp_meta_objset,
3588eda14cbcSMatt Macy dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
3589eda14cbcSMatt Macy hds->ds_object, tx));
3590eda14cbcSMatt Macy
3591eda14cbcSMatt Macy VERIFY0(zap_remove_int(dp->dp_meta_objset,
3592eda14cbcSMatt Macy dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
3593eda14cbcSMatt Macy origin_head->ds_object, tx));
3594eda14cbcSMatt Macy if (dsl_dir_phys(dd)->dd_clones == 0) {
3595eda14cbcSMatt Macy dsl_dir_phys(dd)->dd_clones =
3596eda14cbcSMatt Macy zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
3597eda14cbcSMatt Macy DMU_OT_NONE, 0, tx);
3598eda14cbcSMatt Macy }
3599eda14cbcSMatt Macy VERIFY0(zap_add_int(dp->dp_meta_objset,
3600eda14cbcSMatt Macy dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
3601eda14cbcSMatt Macy }
3602eda14cbcSMatt Macy
3603eda14cbcSMatt Macy /*
3604eda14cbcSMatt Macy * Move bookmarks to this dir.
3605eda14cbcSMatt Macy */
3606eda14cbcSMatt Macy dsl_bookmark_node_t *dbn_next;
3607eda14cbcSMatt Macy for (dsl_bookmark_node_t *dbn = avl_first(&origin_head->ds_bookmarks);
3608eda14cbcSMatt Macy dbn != NULL && dbn->dbn_phys.zbm_creation_txg <=
3609eda14cbcSMatt Macy dsl_dataset_phys(origin_ds)->ds_creation_txg;
3610eda14cbcSMatt Macy dbn = dbn_next) {
3611eda14cbcSMatt Macy dbn_next = AVL_NEXT(&origin_head->ds_bookmarks, dbn);
3612eda14cbcSMatt Macy
3613eda14cbcSMatt Macy avl_remove(&origin_head->ds_bookmarks, dbn);
3614eda14cbcSMatt Macy VERIFY0(zap_remove(dp->dp_meta_objset,
3615eda14cbcSMatt Macy origin_head->ds_bookmarks_obj, dbn->dbn_name, tx));
3616eda14cbcSMatt Macy
3617eda14cbcSMatt Macy dsl_bookmark_node_add(hds, dbn, tx);
3618eda14cbcSMatt Macy }
3619eda14cbcSMatt Macy
3620eda14cbcSMatt Macy dsl_bookmark_next_changed(hds, origin_ds, tx);
3621eda14cbcSMatt Macy
3622eda14cbcSMatt Macy /* move snapshots to this dir */
3623eda14cbcSMatt Macy for (snap = list_head(&ddpa->shared_snaps); snap;
3624eda14cbcSMatt Macy snap = list_next(&ddpa->shared_snaps, snap)) {
3625eda14cbcSMatt Macy dsl_dataset_t *ds = snap->ds;
3626eda14cbcSMatt Macy
3627eda14cbcSMatt Macy /*
3628eda14cbcSMatt Macy * Property callbacks are registered to a particular
3629eda14cbcSMatt Macy * dsl_dir. Since ours is changing, evict the objset
3630eda14cbcSMatt Macy * so that they will be unregistered from the old dsl_dir.
3631eda14cbcSMatt Macy */
3632eda14cbcSMatt Macy if (ds->ds_objset) {
3633eda14cbcSMatt Macy dmu_objset_evict(ds->ds_objset);
3634eda14cbcSMatt Macy ds->ds_objset = NULL;
3635eda14cbcSMatt Macy }
3636eda14cbcSMatt Macy
3637eda14cbcSMatt Macy /* move snap name entry */
3638eda14cbcSMatt Macy VERIFY0(dsl_dataset_get_snapname(ds));
3639eda14cbcSMatt Macy VERIFY0(dsl_dataset_snap_remove(origin_head,
3640eda14cbcSMatt Macy ds->ds_snapname, tx, B_TRUE));
3641eda14cbcSMatt Macy VERIFY0(zap_add(dp->dp_meta_objset,
3642eda14cbcSMatt Macy dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
3643eda14cbcSMatt Macy 8, 1, &ds->ds_object, tx));
3644eda14cbcSMatt Macy dsl_fs_ss_count_adjust(hds->ds_dir, 1,
3645eda14cbcSMatt Macy DD_FIELD_SNAPSHOT_COUNT, tx);
3646eda14cbcSMatt Macy
3647eda14cbcSMatt Macy /* change containing dsl_dir */
3648eda14cbcSMatt Macy dmu_buf_will_dirty(ds->ds_dbuf, tx);
3649eda14cbcSMatt Macy ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
3650eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
3651eda14cbcSMatt Macy ASSERT3P(ds->ds_dir, ==, odd);
3652eda14cbcSMatt Macy dsl_dir_rele(ds->ds_dir, ds);
3653eda14cbcSMatt Macy VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
3654eda14cbcSMatt Macy NULL, ds, &ds->ds_dir));
3655eda14cbcSMatt Macy
3656eda14cbcSMatt Macy /* move any clone references */
3657eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
3658eda14cbcSMatt Macy spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
3659eda14cbcSMatt Macy zap_cursor_t zc;
36607a7741afSMartin Matuska zap_attribute_t *za = zap_attribute_alloc();
3661eda14cbcSMatt Macy
3662eda14cbcSMatt Macy for (zap_cursor_init(&zc, dp->dp_meta_objset,
3663eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_next_clones_obj);
36647a7741afSMartin Matuska zap_cursor_retrieve(&zc, za) == 0;
3665eda14cbcSMatt Macy zap_cursor_advance(&zc)) {
3666eda14cbcSMatt Macy dsl_dataset_t *cnds;
3667eda14cbcSMatt Macy uint64_t o;
3668eda14cbcSMatt Macy
36697a7741afSMartin Matuska if (za->za_first_integer == oldnext_obj) {
3670eda14cbcSMatt Macy /*
3671eda14cbcSMatt Macy * We've already moved the
3672eda14cbcSMatt Macy * origin's reference.
3673eda14cbcSMatt Macy */
3674eda14cbcSMatt Macy continue;
3675eda14cbcSMatt Macy }
3676eda14cbcSMatt Macy
3677eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold_obj(dp,
36787a7741afSMartin Matuska za->za_first_integer, FTAG, &cnds));
3679eda14cbcSMatt Macy o = dsl_dir_phys(cnds->ds_dir)->
3680eda14cbcSMatt Macy dd_head_dataset_obj;
3681eda14cbcSMatt Macy
3682eda14cbcSMatt Macy VERIFY0(zap_remove_int(dp->dp_meta_objset,
3683eda14cbcSMatt Macy dsl_dir_phys(odd)->dd_clones, o, tx));
3684eda14cbcSMatt Macy VERIFY0(zap_add_int(dp->dp_meta_objset,
3685eda14cbcSMatt Macy dsl_dir_phys(dd)->dd_clones, o, tx));
3686eda14cbcSMatt Macy dsl_dataset_rele(cnds, FTAG);
3687eda14cbcSMatt Macy }
3688eda14cbcSMatt Macy zap_cursor_fini(&zc);
36897a7741afSMartin Matuska zap_attribute_free(za);
3690eda14cbcSMatt Macy }
3691eda14cbcSMatt Macy
3692eda14cbcSMatt Macy ASSERT(!dsl_prop_hascb(ds));
3693eda14cbcSMatt Macy }
3694eda14cbcSMatt Macy
3695eda14cbcSMatt Macy /*
3696eda14cbcSMatt Macy * Change space accounting.
3697eda14cbcSMatt Macy * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
3698eda14cbcSMatt Macy * both be valid, or both be 0 (resulting in delta == 0). This
3699eda14cbcSMatt Macy * is true for each of {clone,origin} independently.
3700eda14cbcSMatt Macy */
3701eda14cbcSMatt Macy
3702eda14cbcSMatt Macy delta = ddpa->cloneusedsnap -
3703eda14cbcSMatt Macy dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
3704eda14cbcSMatt Macy ASSERT3S(delta, >=, 0);
3705eda14cbcSMatt Macy ASSERT3U(ddpa->used, >=, delta);
3706eda14cbcSMatt Macy dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
3707eda14cbcSMatt Macy dsl_dir_diduse_space(dd, DD_USED_HEAD,
3708eda14cbcSMatt Macy ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
3709eda14cbcSMatt Macy
3710eda14cbcSMatt Macy delta = ddpa->originusedsnap -
3711eda14cbcSMatt Macy dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
3712eda14cbcSMatt Macy ASSERT3S(delta, <=, 0);
3713eda14cbcSMatt Macy ASSERT3U(ddpa->used, >=, -delta);
3714eda14cbcSMatt Macy dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
3715eda14cbcSMatt Macy dsl_dir_diduse_space(odd, DD_USED_HEAD,
3716eda14cbcSMatt Macy -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
3717eda14cbcSMatt Macy
3718eda14cbcSMatt Macy dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
3719eda14cbcSMatt Macy
3720eda14cbcSMatt Macy /*
3721eda14cbcSMatt Macy * Since livelists are specific to a clone's origin txg, they
3722eda14cbcSMatt Macy * are no longer accurate. Destroy the livelist from the clone being
3723eda14cbcSMatt Macy * promoted. If the origin dataset is a clone, destroy its livelist
3724eda14cbcSMatt Macy * as well.
3725eda14cbcSMatt Macy */
3726eda14cbcSMatt Macy dsl_dir_remove_livelist(dd, tx, B_TRUE);
3727eda14cbcSMatt Macy dsl_dir_remove_livelist(odd, tx, B_TRUE);
3728eda14cbcSMatt Macy
3729eda14cbcSMatt Macy /* log history record */
3730eda14cbcSMatt Macy spa_history_log_internal_ds(hds, "promote", tx, " ");
3731eda14cbcSMatt Macy
3732eda14cbcSMatt Macy dsl_dir_rele(odd, FTAG);
3733716fd348SMartin Matuska
3734716fd348SMartin Matuska /*
373575e1fea6SMartin Matuska * Transfer common error blocks from old head to new head, before
373675e1fea6SMartin Matuska * calling promote_rele() on ddpa since we need to dereference
373775e1fea6SMartin Matuska * origin_head and hds.
3738716fd348SMartin Matuska */
3739716fd348SMartin Matuska if (spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_HEAD_ERRLOG)) {
3740716fd348SMartin Matuska uint64_t old_head = origin_head->ds_object;
3741716fd348SMartin Matuska uint64_t new_head = hds->ds_object;
3742716fd348SMartin Matuska spa_swap_errlog(dp->dp_spa, new_head, old_head, tx);
3743716fd348SMartin Matuska }
374475e1fea6SMartin Matuska
374575e1fea6SMartin Matuska promote_rele(ddpa, FTAG);
3746eda14cbcSMatt Macy }
3747eda14cbcSMatt Macy
3748eda14cbcSMatt Macy /*
3749eda14cbcSMatt Macy * Make a list of dsl_dataset_t's for the snapshots between first_obj
3750eda14cbcSMatt Macy * (exclusive) and last_obj (inclusive). The list will be in reverse
3751eda14cbcSMatt Macy * order (last_obj will be the list_head()). If first_obj == 0, do all
3752eda14cbcSMatt Macy * snapshots back to this dataset's origin.
3753eda14cbcSMatt Macy */
3754eda14cbcSMatt Macy static int
snaplist_make(dsl_pool_t * dp,uint64_t first_obj,uint64_t last_obj,list_t * l,const void * tag)3755eda14cbcSMatt Macy snaplist_make(dsl_pool_t *dp,
3756a0b956f5SMartin Matuska uint64_t first_obj, uint64_t last_obj, list_t *l, const void *tag)
3757eda14cbcSMatt Macy {
3758eda14cbcSMatt Macy uint64_t obj = last_obj;
3759eda14cbcSMatt Macy
3760eda14cbcSMatt Macy list_create(l, sizeof (struct promotenode),
3761eda14cbcSMatt Macy offsetof(struct promotenode, link));
3762eda14cbcSMatt Macy
3763eda14cbcSMatt Macy while (obj != first_obj) {
3764eda14cbcSMatt Macy dsl_dataset_t *ds;
3765eda14cbcSMatt Macy struct promotenode *snap;
3766eda14cbcSMatt Macy int err;
3767eda14cbcSMatt Macy
3768eda14cbcSMatt Macy err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
3769eda14cbcSMatt Macy ASSERT(err != ENOENT);
3770eda14cbcSMatt Macy if (err != 0)
3771eda14cbcSMatt Macy return (err);
3772eda14cbcSMatt Macy
3773eda14cbcSMatt Macy if (first_obj == 0)
3774eda14cbcSMatt Macy first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
3775eda14cbcSMatt Macy
3776eda14cbcSMatt Macy snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
3777eda14cbcSMatt Macy snap->ds = ds;
3778eda14cbcSMatt Macy list_insert_tail(l, snap);
3779eda14cbcSMatt Macy obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
3780eda14cbcSMatt Macy }
3781eda14cbcSMatt Macy
3782eda14cbcSMatt Macy return (0);
3783eda14cbcSMatt Macy }
3784eda14cbcSMatt Macy
3785eda14cbcSMatt Macy static int
snaplist_space(list_t * l,uint64_t mintxg,uint64_t * spacep)3786eda14cbcSMatt Macy snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
3787eda14cbcSMatt Macy {
3788eda14cbcSMatt Macy struct promotenode *snap;
3789eda14cbcSMatt Macy
3790eda14cbcSMatt Macy *spacep = 0;
3791eda14cbcSMatt Macy for (snap = list_head(l); snap; snap = list_next(l, snap)) {
3792eda14cbcSMatt Macy uint64_t used, comp, uncomp;
3793eda14cbcSMatt Macy dsl_deadlist_space_range(&snap->ds->ds_deadlist,
3794eda14cbcSMatt Macy mintxg, UINT64_MAX, &used, &comp, &uncomp);
3795eda14cbcSMatt Macy *spacep += used;
3796eda14cbcSMatt Macy }
3797eda14cbcSMatt Macy return (0);
3798eda14cbcSMatt Macy }
3799eda14cbcSMatt Macy
3800eda14cbcSMatt Macy static void
snaplist_destroy(list_t * l,const void * tag)3801a0b956f5SMartin Matuska snaplist_destroy(list_t *l, const void *tag)
3802eda14cbcSMatt Macy {
3803eda14cbcSMatt Macy struct promotenode *snap;
3804eda14cbcSMatt Macy
3805eda14cbcSMatt Macy if (l == NULL || !list_link_active(&l->list_head))
3806eda14cbcSMatt Macy return;
3807eda14cbcSMatt Macy
38084e8d558cSMartin Matuska while ((snap = list_remove_tail(l)) != NULL) {
3809eda14cbcSMatt Macy dsl_dataset_rele(snap->ds, tag);
3810eda14cbcSMatt Macy kmem_free(snap, sizeof (*snap));
3811eda14cbcSMatt Macy }
3812eda14cbcSMatt Macy list_destroy(l);
3813eda14cbcSMatt Macy }
3814eda14cbcSMatt Macy
3815eda14cbcSMatt Macy static int
promote_hold(dsl_dataset_promote_arg_t * ddpa,dsl_pool_t * dp,const void * tag)3816a0b956f5SMartin Matuska promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, const void *tag)
3817eda14cbcSMatt Macy {
3818eda14cbcSMatt Macy int error;
3819eda14cbcSMatt Macy dsl_dir_t *dd;
3820eda14cbcSMatt Macy struct promotenode *snap;
3821eda14cbcSMatt Macy
3822eda14cbcSMatt Macy error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
3823eda14cbcSMatt Macy &ddpa->ddpa_clone);
3824eda14cbcSMatt Macy if (error != 0)
3825eda14cbcSMatt Macy return (error);
3826eda14cbcSMatt Macy dd = ddpa->ddpa_clone->ds_dir;
3827eda14cbcSMatt Macy
3828eda14cbcSMatt Macy if (ddpa->ddpa_clone->ds_is_snapshot ||
3829eda14cbcSMatt Macy !dsl_dir_is_clone(dd)) {
3830eda14cbcSMatt Macy dsl_dataset_rele(ddpa->ddpa_clone, tag);
3831eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
3832eda14cbcSMatt Macy }
3833eda14cbcSMatt Macy
3834eda14cbcSMatt Macy error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
3835eda14cbcSMatt Macy &ddpa->shared_snaps, tag);
3836eda14cbcSMatt Macy if (error != 0)
3837eda14cbcSMatt Macy goto out;
3838eda14cbcSMatt Macy
3839eda14cbcSMatt Macy error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
3840eda14cbcSMatt Macy &ddpa->clone_snaps, tag);
3841eda14cbcSMatt Macy if (error != 0)
3842eda14cbcSMatt Macy goto out;
3843eda14cbcSMatt Macy
3844eda14cbcSMatt Macy snap = list_head(&ddpa->shared_snaps);
3845eda14cbcSMatt Macy ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
3846eda14cbcSMatt Macy error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
3847eda14cbcSMatt Macy dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
3848eda14cbcSMatt Macy &ddpa->origin_snaps, tag);
3849eda14cbcSMatt Macy if (error != 0)
3850eda14cbcSMatt Macy goto out;
3851eda14cbcSMatt Macy
3852eda14cbcSMatt Macy if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
3853eda14cbcSMatt Macy error = dsl_dataset_hold_obj(dp,
3854eda14cbcSMatt Macy dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
3855eda14cbcSMatt Macy tag, &ddpa->origin_origin);
3856eda14cbcSMatt Macy if (error != 0)
3857eda14cbcSMatt Macy goto out;
3858eda14cbcSMatt Macy }
3859eda14cbcSMatt Macy out:
3860eda14cbcSMatt Macy if (error != 0)
3861eda14cbcSMatt Macy promote_rele(ddpa, tag);
3862eda14cbcSMatt Macy return (error);
3863eda14cbcSMatt Macy }
3864eda14cbcSMatt Macy
3865eda14cbcSMatt Macy static void
promote_rele(dsl_dataset_promote_arg_t * ddpa,const void * tag)3866a0b956f5SMartin Matuska promote_rele(dsl_dataset_promote_arg_t *ddpa, const void *tag)
3867eda14cbcSMatt Macy {
3868eda14cbcSMatt Macy snaplist_destroy(&ddpa->shared_snaps, tag);
3869eda14cbcSMatt Macy snaplist_destroy(&ddpa->clone_snaps, tag);
3870eda14cbcSMatt Macy snaplist_destroy(&ddpa->origin_snaps, tag);
3871eda14cbcSMatt Macy if (ddpa->origin_origin != NULL)
3872eda14cbcSMatt Macy dsl_dataset_rele(ddpa->origin_origin, tag);
3873eda14cbcSMatt Macy dsl_dataset_rele(ddpa->ddpa_clone, tag);
3874eda14cbcSMatt Macy }
3875eda14cbcSMatt Macy
3876eda14cbcSMatt Macy /*
3877eda14cbcSMatt Macy * Promote a clone.
3878eda14cbcSMatt Macy *
3879eda14cbcSMatt Macy * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
3880eda14cbcSMatt Macy * in with the name. (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.)
3881eda14cbcSMatt Macy */
3882eda14cbcSMatt Macy int
dsl_dataset_promote(const char * name,char * conflsnap)3883eda14cbcSMatt Macy dsl_dataset_promote(const char *name, char *conflsnap)
3884eda14cbcSMatt Macy {
3885eda14cbcSMatt Macy dsl_dataset_promote_arg_t ddpa = { 0 };
3886eda14cbcSMatt Macy uint64_t numsnaps;
3887eda14cbcSMatt Macy int error;
3888eda14cbcSMatt Macy nvpair_t *snap_pair;
3889eda14cbcSMatt Macy objset_t *os;
3890eda14cbcSMatt Macy
3891eda14cbcSMatt Macy /*
3892eda14cbcSMatt Macy * We will modify space proportional to the number of
3893eda14cbcSMatt Macy * snapshots. Compute numsnaps.
3894eda14cbcSMatt Macy */
3895eda14cbcSMatt Macy error = dmu_objset_hold(name, FTAG, &os);
3896eda14cbcSMatt Macy if (error != 0)
3897eda14cbcSMatt Macy return (error);
3898eda14cbcSMatt Macy error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
3899eda14cbcSMatt Macy dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
3900eda14cbcSMatt Macy &numsnaps);
3901eda14cbcSMatt Macy dmu_objset_rele(os, FTAG);
3902eda14cbcSMatt Macy if (error != 0)
3903eda14cbcSMatt Macy return (error);
3904eda14cbcSMatt Macy
3905eda14cbcSMatt Macy ddpa.ddpa_clonename = name;
3906eda14cbcSMatt Macy ddpa.err_ds = fnvlist_alloc();
3907eda14cbcSMatt Macy ddpa.cr = CRED();
3908eda14cbcSMatt Macy ddpa.proc = curproc;
3909eda14cbcSMatt Macy
3910eda14cbcSMatt Macy error = dsl_sync_task(name, dsl_dataset_promote_check,
3911eda14cbcSMatt Macy dsl_dataset_promote_sync, &ddpa,
3912eda14cbcSMatt Macy 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED);
3913eda14cbcSMatt Macy
3914eda14cbcSMatt Macy /*
3915eda14cbcSMatt Macy * Return the first conflicting snapshot found.
3916eda14cbcSMatt Macy */
3917eda14cbcSMatt Macy snap_pair = nvlist_next_nvpair(ddpa.err_ds, NULL);
3918eda14cbcSMatt Macy if (snap_pair != NULL && conflsnap != NULL)
3919eda14cbcSMatt Macy (void) strlcpy(conflsnap, nvpair_name(snap_pair),
3920eda14cbcSMatt Macy ZFS_MAX_DATASET_NAME_LEN);
3921eda14cbcSMatt Macy
3922eda14cbcSMatt Macy fnvlist_free(ddpa.err_ds);
3923eda14cbcSMatt Macy return (error);
3924eda14cbcSMatt Macy }
3925eda14cbcSMatt Macy
3926eda14cbcSMatt Macy int
dsl_dataset_clone_swap_check_impl(dsl_dataset_t * clone,dsl_dataset_t * origin_head,boolean_t force,void * owner,dmu_tx_t * tx)3927eda14cbcSMatt Macy dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
3928eda14cbcSMatt Macy dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
3929eda14cbcSMatt Macy {
3930eda14cbcSMatt Macy /*
3931eda14cbcSMatt Macy * "slack" factor for received datasets with refquota set on them.
3932eda14cbcSMatt Macy * See the bottom of this function for details on its use.
3933eda14cbcSMatt Macy */
3934eda14cbcSMatt Macy uint64_t refquota_slack = (uint64_t)DMU_MAX_ACCESS *
3935eda14cbcSMatt Macy spa_asize_inflation;
3936eda14cbcSMatt Macy int64_t unused_refres_delta;
3937eda14cbcSMatt Macy
3938eda14cbcSMatt Macy /* they should both be heads */
3939eda14cbcSMatt Macy if (clone->ds_is_snapshot ||
3940eda14cbcSMatt Macy origin_head->ds_is_snapshot)
3941eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
3942eda14cbcSMatt Macy
3943eda14cbcSMatt Macy /* if we are not forcing, the branch point should be just before them */
3944eda14cbcSMatt Macy if (!force && clone->ds_prev != origin_head->ds_prev)
3945eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
3946eda14cbcSMatt Macy
3947eda14cbcSMatt Macy /* clone should be the clone (unless they are unrelated) */
3948eda14cbcSMatt Macy if (clone->ds_prev != NULL &&
3949eda14cbcSMatt Macy clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
3950eda14cbcSMatt Macy origin_head->ds_dir != clone->ds_prev->ds_dir)
3951eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
3952eda14cbcSMatt Macy
3953eda14cbcSMatt Macy /* the clone should be a child of the origin */
3954eda14cbcSMatt Macy if (clone->ds_dir->dd_parent != origin_head->ds_dir)
3955eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
3956eda14cbcSMatt Macy
3957eda14cbcSMatt Macy /* origin_head shouldn't be modified unless 'force' */
3958eda14cbcSMatt Macy if (!force &&
3959eda14cbcSMatt Macy dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
3960eda14cbcSMatt Macy return (SET_ERROR(ETXTBSY));
3961eda14cbcSMatt Macy
3962eda14cbcSMatt Macy /* origin_head should have no long holds (e.g. is not mounted) */
3963eda14cbcSMatt Macy if (dsl_dataset_handoff_check(origin_head, owner, tx))
3964eda14cbcSMatt Macy return (SET_ERROR(EBUSY));
3965eda14cbcSMatt Macy
3966eda14cbcSMatt Macy /* check amount of any unconsumed refreservation */
3967eda14cbcSMatt Macy unused_refres_delta =
3968eda14cbcSMatt Macy (int64_t)MIN(origin_head->ds_reserved,
3969eda14cbcSMatt Macy dsl_dataset_phys(origin_head)->ds_unique_bytes) -
3970eda14cbcSMatt Macy (int64_t)MIN(origin_head->ds_reserved,
3971eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_unique_bytes);
3972eda14cbcSMatt Macy
3973eda14cbcSMatt Macy if (unused_refres_delta > 0 &&
3974eda14cbcSMatt Macy unused_refres_delta >
3975eda14cbcSMatt Macy dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
3976eda14cbcSMatt Macy return (SET_ERROR(ENOSPC));
3977eda14cbcSMatt Macy
3978eda14cbcSMatt Macy /*
3979eda14cbcSMatt Macy * The clone can't be too much over the head's refquota.
3980eda14cbcSMatt Macy *
3981eda14cbcSMatt Macy * To ensure that the entire refquota can be used, we allow one
3982eda14cbcSMatt Macy * transaction to exceed the refquota. Therefore, this check
3983eda14cbcSMatt Macy * needs to also allow for the space referenced to be more than the
3984eda14cbcSMatt Macy * refquota. The maximum amount of space that one transaction can use
3985eda14cbcSMatt Macy * on disk is DMU_MAX_ACCESS * spa_asize_inflation. Allowing this
3986eda14cbcSMatt Macy * overage ensures that we are able to receive a filesystem that
3987eda14cbcSMatt Macy * exceeds the refquota on the source system.
3988eda14cbcSMatt Macy *
3989eda14cbcSMatt Macy * So that overage is the refquota_slack we use below.
3990eda14cbcSMatt Macy */
3991eda14cbcSMatt Macy if (origin_head->ds_quota != 0 &&
3992eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_referenced_bytes >
3993eda14cbcSMatt Macy origin_head->ds_quota + refquota_slack)
3994eda14cbcSMatt Macy return (SET_ERROR(EDQUOT));
3995eda14cbcSMatt Macy
3996eda14cbcSMatt Macy return (0);
3997eda14cbcSMatt Macy }
3998eda14cbcSMatt Macy
3999eda14cbcSMatt Macy static void
dsl_dataset_swap_remap_deadlists(dsl_dataset_t * clone,dsl_dataset_t * origin,dmu_tx_t * tx)4000eda14cbcSMatt Macy dsl_dataset_swap_remap_deadlists(dsl_dataset_t *clone,
4001eda14cbcSMatt Macy dsl_dataset_t *origin, dmu_tx_t *tx)
4002eda14cbcSMatt Macy {
4003eda14cbcSMatt Macy uint64_t clone_remap_dl_obj, origin_remap_dl_obj;
4004eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
4005eda14cbcSMatt Macy
4006eda14cbcSMatt Macy ASSERT(dsl_pool_sync_context(dp));
4007eda14cbcSMatt Macy
4008eda14cbcSMatt Macy clone_remap_dl_obj = dsl_dataset_get_remap_deadlist_object(clone);
4009eda14cbcSMatt Macy origin_remap_dl_obj = dsl_dataset_get_remap_deadlist_object(origin);
4010eda14cbcSMatt Macy
4011eda14cbcSMatt Macy if (clone_remap_dl_obj != 0) {
4012eda14cbcSMatt Macy dsl_deadlist_close(&clone->ds_remap_deadlist);
4013eda14cbcSMatt Macy dsl_dataset_unset_remap_deadlist_object(clone, tx);
4014eda14cbcSMatt Macy }
4015eda14cbcSMatt Macy if (origin_remap_dl_obj != 0) {
4016eda14cbcSMatt Macy dsl_deadlist_close(&origin->ds_remap_deadlist);
4017eda14cbcSMatt Macy dsl_dataset_unset_remap_deadlist_object(origin, tx);
4018eda14cbcSMatt Macy }
4019eda14cbcSMatt Macy
4020eda14cbcSMatt Macy if (clone_remap_dl_obj != 0) {
4021eda14cbcSMatt Macy dsl_dataset_set_remap_deadlist_object(origin,
4022eda14cbcSMatt Macy clone_remap_dl_obj, tx);
4023eda14cbcSMatt Macy dsl_deadlist_open(&origin->ds_remap_deadlist,
4024eda14cbcSMatt Macy dp->dp_meta_objset, clone_remap_dl_obj);
4025eda14cbcSMatt Macy }
4026eda14cbcSMatt Macy if (origin_remap_dl_obj != 0) {
4027eda14cbcSMatt Macy dsl_dataset_set_remap_deadlist_object(clone,
4028eda14cbcSMatt Macy origin_remap_dl_obj, tx);
4029eda14cbcSMatt Macy dsl_deadlist_open(&clone->ds_remap_deadlist,
4030eda14cbcSMatt Macy dp->dp_meta_objset, origin_remap_dl_obj);
4031eda14cbcSMatt Macy }
4032eda14cbcSMatt Macy }
4033eda14cbcSMatt Macy
4034eda14cbcSMatt Macy void
dsl_dataset_clone_swap_sync_impl(dsl_dataset_t * clone,dsl_dataset_t * origin_head,dmu_tx_t * tx)4035eda14cbcSMatt Macy dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
4036eda14cbcSMatt Macy dsl_dataset_t *origin_head, dmu_tx_t *tx)
4037eda14cbcSMatt Macy {
4038eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
4039eda14cbcSMatt Macy int64_t unused_refres_delta;
4040eda14cbcSMatt Macy
4041eda14cbcSMatt Macy ASSERT(clone->ds_reserved == 0);
4042eda14cbcSMatt Macy /*
4043eda14cbcSMatt Macy * NOTE: On DEBUG kernels there could be a race between this and
4044eda14cbcSMatt Macy * the check function if spa_asize_inflation is adjusted...
4045eda14cbcSMatt Macy */
4046eda14cbcSMatt Macy ASSERT(origin_head->ds_quota == 0 ||
4047eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota +
4048eda14cbcSMatt Macy DMU_MAX_ACCESS * spa_asize_inflation);
4049eda14cbcSMatt Macy ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
4050eda14cbcSMatt Macy
4051eda14cbcSMatt Macy dsl_dir_cancel_waiters(origin_head->ds_dir);
4052eda14cbcSMatt Macy
4053eda14cbcSMatt Macy /*
4054eda14cbcSMatt Macy * Swap per-dataset feature flags.
4055eda14cbcSMatt Macy */
4056eda14cbcSMatt Macy for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
4057eda14cbcSMatt Macy if (!(spa_feature_table[f].fi_flags &
4058eda14cbcSMatt Macy ZFEATURE_FLAG_PER_DATASET)) {
4059eda14cbcSMatt Macy ASSERT(!dsl_dataset_feature_is_active(clone, f));
4060eda14cbcSMatt Macy ASSERT(!dsl_dataset_feature_is_active(origin_head, f));
4061eda14cbcSMatt Macy continue;
4062eda14cbcSMatt Macy }
4063eda14cbcSMatt Macy
4064eda14cbcSMatt Macy boolean_t clone_inuse = dsl_dataset_feature_is_active(clone, f);
4065eda14cbcSMatt Macy void *clone_feature = clone->ds_feature[f];
4066eda14cbcSMatt Macy boolean_t origin_head_inuse =
4067eda14cbcSMatt Macy dsl_dataset_feature_is_active(origin_head, f);
4068eda14cbcSMatt Macy void *origin_head_feature = origin_head->ds_feature[f];
4069eda14cbcSMatt Macy
4070eda14cbcSMatt Macy if (clone_inuse)
4071eda14cbcSMatt Macy dsl_dataset_deactivate_feature_impl(clone, f, tx);
4072eda14cbcSMatt Macy if (origin_head_inuse)
4073eda14cbcSMatt Macy dsl_dataset_deactivate_feature_impl(origin_head, f, tx);
4074eda14cbcSMatt Macy
4075eda14cbcSMatt Macy if (clone_inuse) {
4076eda14cbcSMatt Macy dsl_dataset_activate_feature(origin_head->ds_object, f,
4077eda14cbcSMatt Macy clone_feature, tx);
4078eda14cbcSMatt Macy origin_head->ds_feature[f] = clone_feature;
4079eda14cbcSMatt Macy }
4080eda14cbcSMatt Macy if (origin_head_inuse) {
4081eda14cbcSMatt Macy dsl_dataset_activate_feature(clone->ds_object, f,
4082eda14cbcSMatt Macy origin_head_feature, tx);
4083eda14cbcSMatt Macy clone->ds_feature[f] = origin_head_feature;
4084eda14cbcSMatt Macy }
4085eda14cbcSMatt Macy }
4086eda14cbcSMatt Macy
4087eda14cbcSMatt Macy dmu_buf_will_dirty(clone->ds_dbuf, tx);
4088eda14cbcSMatt Macy dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
4089eda14cbcSMatt Macy
4090eda14cbcSMatt Macy if (clone->ds_objset != NULL) {
4091eda14cbcSMatt Macy dmu_objset_evict(clone->ds_objset);
4092eda14cbcSMatt Macy clone->ds_objset = NULL;
4093eda14cbcSMatt Macy }
4094eda14cbcSMatt Macy
4095eda14cbcSMatt Macy if (origin_head->ds_objset != NULL) {
4096eda14cbcSMatt Macy dmu_objset_evict(origin_head->ds_objset);
4097eda14cbcSMatt Macy origin_head->ds_objset = NULL;
4098eda14cbcSMatt Macy }
4099eda14cbcSMatt Macy
4100eda14cbcSMatt Macy unused_refres_delta =
4101eda14cbcSMatt Macy (int64_t)MIN(origin_head->ds_reserved,
4102eda14cbcSMatt Macy dsl_dataset_phys(origin_head)->ds_unique_bytes) -
4103eda14cbcSMatt Macy (int64_t)MIN(origin_head->ds_reserved,
4104eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_unique_bytes);
4105eda14cbcSMatt Macy
4106eda14cbcSMatt Macy /*
4107eda14cbcSMatt Macy * Reset origin's unique bytes.
4108eda14cbcSMatt Macy */
4109eda14cbcSMatt Macy {
4110eda14cbcSMatt Macy dsl_dataset_t *origin = clone->ds_prev;
4111eda14cbcSMatt Macy uint64_t comp, uncomp;
4112eda14cbcSMatt Macy
4113eda14cbcSMatt Macy dmu_buf_will_dirty(origin->ds_dbuf, tx);
4114eda14cbcSMatt Macy dsl_deadlist_space_range(&clone->ds_deadlist,
4115eda14cbcSMatt Macy dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
4116eda14cbcSMatt Macy &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
4117eda14cbcSMatt Macy }
4118eda14cbcSMatt Macy
4119eda14cbcSMatt Macy /* swap blkptrs */
4120eda14cbcSMatt Macy {
4121eda14cbcSMatt Macy rrw_enter(&clone->ds_bp_rwlock, RW_WRITER, FTAG);
4122eda14cbcSMatt Macy rrw_enter(&origin_head->ds_bp_rwlock, RW_WRITER, FTAG);
4123eda14cbcSMatt Macy blkptr_t tmp;
4124eda14cbcSMatt Macy tmp = dsl_dataset_phys(origin_head)->ds_bp;
4125eda14cbcSMatt Macy dsl_dataset_phys(origin_head)->ds_bp =
4126eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_bp;
4127eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_bp = tmp;
4128eda14cbcSMatt Macy rrw_exit(&origin_head->ds_bp_rwlock, FTAG);
4129eda14cbcSMatt Macy rrw_exit(&clone->ds_bp_rwlock, FTAG);
4130eda14cbcSMatt Macy }
4131eda14cbcSMatt Macy
4132eda14cbcSMatt Macy /* set dd_*_bytes */
4133eda14cbcSMatt Macy {
4134eda14cbcSMatt Macy int64_t dused, dcomp, duncomp;
4135eda14cbcSMatt Macy uint64_t cdl_used, cdl_comp, cdl_uncomp;
4136eda14cbcSMatt Macy uint64_t odl_used, odl_comp, odl_uncomp;
4137eda14cbcSMatt Macy
4138eda14cbcSMatt Macy ASSERT3U(dsl_dir_phys(clone->ds_dir)->
4139eda14cbcSMatt Macy dd_used_breakdown[DD_USED_SNAP], ==, 0);
4140eda14cbcSMatt Macy
4141eda14cbcSMatt Macy dsl_deadlist_space(&clone->ds_deadlist,
4142eda14cbcSMatt Macy &cdl_used, &cdl_comp, &cdl_uncomp);
4143eda14cbcSMatt Macy dsl_deadlist_space(&origin_head->ds_deadlist,
4144eda14cbcSMatt Macy &odl_used, &odl_comp, &odl_uncomp);
4145eda14cbcSMatt Macy
4146eda14cbcSMatt Macy dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
4147eda14cbcSMatt Macy cdl_used -
4148eda14cbcSMatt Macy (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
4149eda14cbcSMatt Macy odl_used);
4150eda14cbcSMatt Macy dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
4151eda14cbcSMatt Macy cdl_comp -
4152eda14cbcSMatt Macy (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
4153eda14cbcSMatt Macy odl_comp);
4154eda14cbcSMatt Macy duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
4155eda14cbcSMatt Macy cdl_uncomp -
4156eda14cbcSMatt Macy (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
4157eda14cbcSMatt Macy odl_uncomp);
4158eda14cbcSMatt Macy
4159eda14cbcSMatt Macy dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
4160eda14cbcSMatt Macy dused, dcomp, duncomp, tx);
4161eda14cbcSMatt Macy dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
4162eda14cbcSMatt Macy -dused, -dcomp, -duncomp, tx);
4163eda14cbcSMatt Macy
4164eda14cbcSMatt Macy /*
4165eda14cbcSMatt Macy * The difference in the space used by snapshots is the
4166eda14cbcSMatt Macy * difference in snapshot space due to the head's
4167eda14cbcSMatt Macy * deadlist (since that's the only thing that's
4168eda14cbcSMatt Macy * changing that affects the snapused).
4169eda14cbcSMatt Macy */
4170eda14cbcSMatt Macy dsl_deadlist_space_range(&clone->ds_deadlist,
4171eda14cbcSMatt Macy origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
4172eda14cbcSMatt Macy &cdl_used, &cdl_comp, &cdl_uncomp);
4173eda14cbcSMatt Macy dsl_deadlist_space_range(&origin_head->ds_deadlist,
4174eda14cbcSMatt Macy origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
4175eda14cbcSMatt Macy &odl_used, &odl_comp, &odl_uncomp);
4176eda14cbcSMatt Macy dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
4177eda14cbcSMatt Macy DD_USED_HEAD, DD_USED_SNAP, tx);
4178eda14cbcSMatt Macy }
4179eda14cbcSMatt Macy
4180eda14cbcSMatt Macy /* swap ds_*_bytes */
4181eda14cbcSMatt Macy SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
4182eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_referenced_bytes);
4183eda14cbcSMatt Macy SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
4184eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_compressed_bytes);
4185eda14cbcSMatt Macy SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
4186eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_uncompressed_bytes);
4187eda14cbcSMatt Macy SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
4188eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_unique_bytes);
4189eda14cbcSMatt Macy
4190eda14cbcSMatt Macy /* apply any parent delta for change in unconsumed refreservation */
4191eda14cbcSMatt Macy dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
4192eda14cbcSMatt Macy unused_refres_delta, 0, 0, tx);
4193eda14cbcSMatt Macy
4194eda14cbcSMatt Macy /*
4195eda14cbcSMatt Macy * Swap deadlists.
4196eda14cbcSMatt Macy */
4197eda14cbcSMatt Macy dsl_deadlist_close(&clone->ds_deadlist);
4198eda14cbcSMatt Macy dsl_deadlist_close(&origin_head->ds_deadlist);
4199eda14cbcSMatt Macy SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
4200eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_deadlist_obj);
4201eda14cbcSMatt Macy dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
4202eda14cbcSMatt Macy dsl_dataset_phys(clone)->ds_deadlist_obj);
4203eda14cbcSMatt Macy dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
4204eda14cbcSMatt Macy dsl_dataset_phys(origin_head)->ds_deadlist_obj);
4205eda14cbcSMatt Macy dsl_dataset_swap_remap_deadlists(clone, origin_head, tx);
4206eda14cbcSMatt Macy
4207eda14cbcSMatt Macy /*
4208eda14cbcSMatt Macy * If there is a bookmark at the origin, its "next dataset" is
4209eda14cbcSMatt Macy * changing, so we need to reset its FBN.
4210eda14cbcSMatt Macy */
4211eda14cbcSMatt Macy dsl_bookmark_next_changed(origin_head, origin_head->ds_prev, tx);
4212eda14cbcSMatt Macy
4213eda14cbcSMatt Macy dsl_scan_ds_clone_swapped(origin_head, clone, tx);
4214eda14cbcSMatt Macy
4215eda14cbcSMatt Macy /*
4216eda14cbcSMatt Macy * Destroy any livelists associated with the clone or the origin,
4217eda14cbcSMatt Macy * since after the swap the corresponding livelists are no longer
4218eda14cbcSMatt Macy * valid.
4219eda14cbcSMatt Macy */
4220eda14cbcSMatt Macy dsl_dir_remove_livelist(clone->ds_dir, tx, B_TRUE);
4221eda14cbcSMatt Macy dsl_dir_remove_livelist(origin_head->ds_dir, tx, B_TRUE);
4222eda14cbcSMatt Macy
4223eda14cbcSMatt Macy spa_history_log_internal_ds(clone, "clone swap", tx,
4224eda14cbcSMatt Macy "parent=%s", origin_head->ds_dir->dd_myname);
4225eda14cbcSMatt Macy }
4226eda14cbcSMatt Macy
4227eda14cbcSMatt Macy /*
4228eda14cbcSMatt Macy * Given a pool name and a dataset object number in that pool,
4229eda14cbcSMatt Macy * return the name of that dataset.
4230eda14cbcSMatt Macy */
4231eda14cbcSMatt Macy int
dsl_dsobj_to_dsname(char * pname,uint64_t obj,char * buf)4232eda14cbcSMatt Macy dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
4233eda14cbcSMatt Macy {
4234eda14cbcSMatt Macy dsl_pool_t *dp;
4235eda14cbcSMatt Macy dsl_dataset_t *ds;
4236eda14cbcSMatt Macy int error;
4237eda14cbcSMatt Macy
4238eda14cbcSMatt Macy error = dsl_pool_hold(pname, FTAG, &dp);
4239eda14cbcSMatt Macy if (error != 0)
4240eda14cbcSMatt Macy return (error);
4241eda14cbcSMatt Macy
4242eda14cbcSMatt Macy error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
4243eda14cbcSMatt Macy if (error == 0) {
4244eda14cbcSMatt Macy dsl_dataset_name(ds, buf);
4245eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4246eda14cbcSMatt Macy }
4247eda14cbcSMatt Macy dsl_pool_rele(dp, FTAG);
4248eda14cbcSMatt Macy
4249eda14cbcSMatt Macy return (error);
4250eda14cbcSMatt Macy }
4251eda14cbcSMatt Macy
4252eda14cbcSMatt Macy int
dsl_dataset_check_quota(dsl_dataset_t * ds,boolean_t check_quota,uint64_t asize,uint64_t inflight,uint64_t * used,uint64_t * ref_rsrv)4253eda14cbcSMatt Macy dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
4254eda14cbcSMatt Macy uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
4255eda14cbcSMatt Macy {
4256eda14cbcSMatt Macy int error = 0;
4257eda14cbcSMatt Macy
4258eda14cbcSMatt Macy ASSERT3S(asize, >, 0);
4259eda14cbcSMatt Macy
4260eda14cbcSMatt Macy /*
4261eda14cbcSMatt Macy * *ref_rsrv is the portion of asize that will come from any
4262eda14cbcSMatt Macy * unconsumed refreservation space.
4263eda14cbcSMatt Macy */
4264eda14cbcSMatt Macy *ref_rsrv = 0;
4265eda14cbcSMatt Macy
4266eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
4267eda14cbcSMatt Macy /*
4268eda14cbcSMatt Macy * Make a space adjustment for reserved bytes.
4269eda14cbcSMatt Macy */
4270eda14cbcSMatt Macy if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
4271eda14cbcSMatt Macy ASSERT3U(*used, >=,
4272eda14cbcSMatt Macy ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
4273eda14cbcSMatt Macy *used -=
4274eda14cbcSMatt Macy (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
4275eda14cbcSMatt Macy *ref_rsrv =
4276eda14cbcSMatt Macy asize - MIN(asize, parent_delta(ds, asize + inflight));
4277eda14cbcSMatt Macy }
4278eda14cbcSMatt Macy
4279eda14cbcSMatt Macy if (!check_quota || ds->ds_quota == 0) {
4280eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
4281eda14cbcSMatt Macy return (0);
4282eda14cbcSMatt Macy }
4283eda14cbcSMatt Macy /*
4284eda14cbcSMatt Macy * If they are requesting more space, and our current estimate
4285eda14cbcSMatt Macy * is over quota, they get to try again unless the actual
4286eda14cbcSMatt Macy * on-disk is over quota and there are no pending changes (which
4287eda14cbcSMatt Macy * may free up space for us).
4288eda14cbcSMatt Macy */
4289eda14cbcSMatt Macy if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
4290eda14cbcSMatt Macy ds->ds_quota) {
4291eda14cbcSMatt Macy if (inflight > 0 ||
4292eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
4293eda14cbcSMatt Macy error = SET_ERROR(ERESTART);
4294eda14cbcSMatt Macy else
4295eda14cbcSMatt Macy error = SET_ERROR(EDQUOT);
4296eda14cbcSMatt Macy }
4297eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
4298eda14cbcSMatt Macy
4299eda14cbcSMatt Macy return (error);
4300eda14cbcSMatt Macy }
4301eda14cbcSMatt Macy
4302eda14cbcSMatt Macy typedef struct dsl_dataset_set_qr_arg {
4303eda14cbcSMatt Macy const char *ddsqra_name;
4304eda14cbcSMatt Macy zprop_source_t ddsqra_source;
4305eda14cbcSMatt Macy uint64_t ddsqra_value;
4306eda14cbcSMatt Macy } dsl_dataset_set_qr_arg_t;
4307eda14cbcSMatt Macy
4308eda14cbcSMatt Macy
4309eda14cbcSMatt Macy static int
dsl_dataset_set_refquota_check(void * arg,dmu_tx_t * tx)4310eda14cbcSMatt Macy dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
4311eda14cbcSMatt Macy {
4312eda14cbcSMatt Macy dsl_dataset_set_qr_arg_t *ddsqra = arg;
4313eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
4314eda14cbcSMatt Macy dsl_dataset_t *ds;
4315eda14cbcSMatt Macy int error;
4316eda14cbcSMatt Macy uint64_t newval;
4317eda14cbcSMatt Macy
4318eda14cbcSMatt Macy if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
4319eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP));
4320eda14cbcSMatt Macy
4321eda14cbcSMatt Macy error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
4322eda14cbcSMatt Macy if (error != 0)
4323eda14cbcSMatt Macy return (error);
4324eda14cbcSMatt Macy
4325eda14cbcSMatt Macy if (ds->ds_is_snapshot) {
4326eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4327eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
4328eda14cbcSMatt Macy }
4329eda14cbcSMatt Macy
4330eda14cbcSMatt Macy error = dsl_prop_predict(ds->ds_dir,
4331eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4332eda14cbcSMatt Macy ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
4333eda14cbcSMatt Macy if (error != 0) {
4334eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4335eda14cbcSMatt Macy return (error);
4336eda14cbcSMatt Macy }
4337eda14cbcSMatt Macy
4338eda14cbcSMatt Macy if (newval == 0) {
4339eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4340eda14cbcSMatt Macy return (0);
4341eda14cbcSMatt Macy }
4342eda14cbcSMatt Macy
4343eda14cbcSMatt Macy if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
4344eda14cbcSMatt Macy newval < ds->ds_reserved) {
4345eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4346eda14cbcSMatt Macy return (SET_ERROR(ENOSPC));
4347eda14cbcSMatt Macy }
4348eda14cbcSMatt Macy
4349eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4350eda14cbcSMatt Macy return (0);
4351eda14cbcSMatt Macy }
4352eda14cbcSMatt Macy
4353eda14cbcSMatt Macy static void
dsl_dataset_set_refquota_sync(void * arg,dmu_tx_t * tx)4354eda14cbcSMatt Macy dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
4355eda14cbcSMatt Macy {
4356eda14cbcSMatt Macy dsl_dataset_set_qr_arg_t *ddsqra = arg;
4357eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
4358eda14cbcSMatt Macy dsl_dataset_t *ds = NULL;
4359eda14cbcSMatt Macy uint64_t newval;
4360eda14cbcSMatt Macy
4361eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
4362eda14cbcSMatt Macy
4363eda14cbcSMatt Macy dsl_prop_set_sync_impl(ds,
4364eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4365eda14cbcSMatt Macy ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
4366eda14cbcSMatt Macy &ddsqra->ddsqra_value, tx);
4367eda14cbcSMatt Macy
4368eda14cbcSMatt Macy VERIFY0(dsl_prop_get_int_ds(ds,
4369eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
4370eda14cbcSMatt Macy
4371eda14cbcSMatt Macy if (ds->ds_quota != newval) {
4372eda14cbcSMatt Macy dmu_buf_will_dirty(ds->ds_dbuf, tx);
4373eda14cbcSMatt Macy ds->ds_quota = newval;
4374eda14cbcSMatt Macy }
4375eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4376eda14cbcSMatt Macy }
4377eda14cbcSMatt Macy
4378eda14cbcSMatt Macy int
dsl_dataset_set_refquota(const char * dsname,zprop_source_t source,uint64_t refquota)4379eda14cbcSMatt Macy dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
4380eda14cbcSMatt Macy uint64_t refquota)
4381eda14cbcSMatt Macy {
4382eda14cbcSMatt Macy dsl_dataset_set_qr_arg_t ddsqra;
4383eda14cbcSMatt Macy
4384eda14cbcSMatt Macy ddsqra.ddsqra_name = dsname;
4385eda14cbcSMatt Macy ddsqra.ddsqra_source = source;
4386eda14cbcSMatt Macy ddsqra.ddsqra_value = refquota;
4387eda14cbcSMatt Macy
4388eda14cbcSMatt Macy return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
4389eda14cbcSMatt Macy dsl_dataset_set_refquota_sync, &ddsqra, 0,
4390eda14cbcSMatt Macy ZFS_SPACE_CHECK_EXTRA_RESERVED));
4391eda14cbcSMatt Macy }
4392eda14cbcSMatt Macy
4393eda14cbcSMatt Macy static int
dsl_dataset_set_refreservation_check(void * arg,dmu_tx_t * tx)4394eda14cbcSMatt Macy dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
4395eda14cbcSMatt Macy {
4396eda14cbcSMatt Macy dsl_dataset_set_qr_arg_t *ddsqra = arg;
4397eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
4398eda14cbcSMatt Macy dsl_dataset_t *ds;
4399eda14cbcSMatt Macy int error;
4400eda14cbcSMatt Macy uint64_t newval, unique;
4401eda14cbcSMatt Macy
4402eda14cbcSMatt Macy if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
4403eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP));
4404eda14cbcSMatt Macy
4405eda14cbcSMatt Macy error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
4406eda14cbcSMatt Macy if (error != 0)
4407eda14cbcSMatt Macy return (error);
4408eda14cbcSMatt Macy
4409eda14cbcSMatt Macy if (ds->ds_is_snapshot) {
4410eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4411eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
4412eda14cbcSMatt Macy }
4413eda14cbcSMatt Macy
4414eda14cbcSMatt Macy error = dsl_prop_predict(ds->ds_dir,
4415eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4416eda14cbcSMatt Macy ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
4417eda14cbcSMatt Macy if (error != 0) {
4418eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4419eda14cbcSMatt Macy return (error);
4420eda14cbcSMatt Macy }
4421eda14cbcSMatt Macy
4422eda14cbcSMatt Macy /*
4423eda14cbcSMatt Macy * If we are doing the preliminary check in open context, the
4424eda14cbcSMatt Macy * space estimates may be inaccurate.
4425eda14cbcSMatt Macy */
4426eda14cbcSMatt Macy if (!dmu_tx_is_syncing(tx)) {
4427eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4428eda14cbcSMatt Macy return (0);
4429eda14cbcSMatt Macy }
4430eda14cbcSMatt Macy
4431eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
4432eda14cbcSMatt Macy if (!DS_UNIQUE_IS_ACCURATE(ds))
4433eda14cbcSMatt Macy dsl_dataset_recalc_head_uniq(ds);
4434eda14cbcSMatt Macy unique = dsl_dataset_phys(ds)->ds_unique_bytes;
4435eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
4436eda14cbcSMatt Macy
4437eda14cbcSMatt Macy if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
4438eda14cbcSMatt Macy uint64_t delta = MAX(unique, newval) -
4439eda14cbcSMatt Macy MAX(unique, ds->ds_reserved);
4440eda14cbcSMatt Macy
4441eda14cbcSMatt Macy if (delta >
4442eda14cbcSMatt Macy dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
4443eda14cbcSMatt Macy (ds->ds_quota > 0 && newval > ds->ds_quota)) {
4444eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4445eda14cbcSMatt Macy return (SET_ERROR(ENOSPC));
4446eda14cbcSMatt Macy }
4447eda14cbcSMatt Macy }
4448eda14cbcSMatt Macy
4449eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4450eda14cbcSMatt Macy return (0);
4451eda14cbcSMatt Macy }
4452eda14cbcSMatt Macy
4453eda14cbcSMatt Macy void
dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t * ds,zprop_source_t source,uint64_t value,dmu_tx_t * tx)4454eda14cbcSMatt Macy dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
4455eda14cbcSMatt Macy zprop_source_t source, uint64_t value, dmu_tx_t *tx)
4456eda14cbcSMatt Macy {
4457eda14cbcSMatt Macy uint64_t newval;
4458eda14cbcSMatt Macy uint64_t unique;
4459eda14cbcSMatt Macy int64_t delta;
4460eda14cbcSMatt Macy
4461eda14cbcSMatt Macy dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4462eda14cbcSMatt Macy source, sizeof (value), 1, &value, tx);
4463eda14cbcSMatt Macy
4464eda14cbcSMatt Macy VERIFY0(dsl_prop_get_int_ds(ds,
4465eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
4466eda14cbcSMatt Macy
4467eda14cbcSMatt Macy dmu_buf_will_dirty(ds->ds_dbuf, tx);
4468eda14cbcSMatt Macy mutex_enter(&ds->ds_dir->dd_lock);
4469eda14cbcSMatt Macy mutex_enter(&ds->ds_lock);
4470eda14cbcSMatt Macy ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
4471eda14cbcSMatt Macy unique = dsl_dataset_phys(ds)->ds_unique_bytes;
4472eda14cbcSMatt Macy delta = MAX(0, (int64_t)(newval - unique)) -
4473eda14cbcSMatt Macy MAX(0, (int64_t)(ds->ds_reserved - unique));
4474eda14cbcSMatt Macy ds->ds_reserved = newval;
4475eda14cbcSMatt Macy mutex_exit(&ds->ds_lock);
4476eda14cbcSMatt Macy
4477eda14cbcSMatt Macy dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
4478eda14cbcSMatt Macy mutex_exit(&ds->ds_dir->dd_lock);
4479eda14cbcSMatt Macy }
4480eda14cbcSMatt Macy
4481eda14cbcSMatt Macy static void
dsl_dataset_set_refreservation_sync(void * arg,dmu_tx_t * tx)4482eda14cbcSMatt Macy dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
4483eda14cbcSMatt Macy {
4484eda14cbcSMatt Macy dsl_dataset_set_qr_arg_t *ddsqra = arg;
4485eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
4486eda14cbcSMatt Macy dsl_dataset_t *ds = NULL;
4487eda14cbcSMatt Macy
4488eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
4489eda14cbcSMatt Macy dsl_dataset_set_refreservation_sync_impl(ds,
4490eda14cbcSMatt Macy ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
4491eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4492eda14cbcSMatt Macy }
4493eda14cbcSMatt Macy
4494eda14cbcSMatt Macy int
dsl_dataset_set_refreservation(const char * dsname,zprop_source_t source,uint64_t refreservation)4495eda14cbcSMatt Macy dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
4496eda14cbcSMatt Macy uint64_t refreservation)
4497eda14cbcSMatt Macy {
4498eda14cbcSMatt Macy dsl_dataset_set_qr_arg_t ddsqra;
4499eda14cbcSMatt Macy
4500eda14cbcSMatt Macy ddsqra.ddsqra_name = dsname;
4501eda14cbcSMatt Macy ddsqra.ddsqra_source = source;
4502eda14cbcSMatt Macy ddsqra.ddsqra_value = refreservation;
4503eda14cbcSMatt Macy
4504eda14cbcSMatt Macy return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
4505eda14cbcSMatt Macy dsl_dataset_set_refreservation_sync, &ddsqra, 0,
4506eda14cbcSMatt Macy ZFS_SPACE_CHECK_EXTRA_RESERVED));
4507eda14cbcSMatt Macy }
4508eda14cbcSMatt Macy
4509eda14cbcSMatt Macy typedef struct dsl_dataset_set_compression_arg {
4510eda14cbcSMatt Macy const char *ddsca_name;
4511eda14cbcSMatt Macy zprop_source_t ddsca_source;
4512eda14cbcSMatt Macy uint64_t ddsca_value;
4513eda14cbcSMatt Macy } dsl_dataset_set_compression_arg_t;
4514eda14cbcSMatt Macy
4515eda14cbcSMatt Macy static int
dsl_dataset_set_compression_check(void * arg,dmu_tx_t * tx)4516eda14cbcSMatt Macy dsl_dataset_set_compression_check(void *arg, dmu_tx_t *tx)
4517eda14cbcSMatt Macy {
4518eda14cbcSMatt Macy dsl_dataset_set_compression_arg_t *ddsca = arg;
4519eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
4520eda14cbcSMatt Macy
4521eda14cbcSMatt Macy uint64_t compval = ZIO_COMPRESS_ALGO(ddsca->ddsca_value);
4522eda14cbcSMatt Macy spa_feature_t f = zio_compress_to_feature(compval);
4523eda14cbcSMatt Macy
4524eda14cbcSMatt Macy if (f == SPA_FEATURE_NONE)
4525eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
4526eda14cbcSMatt Macy
4527eda14cbcSMatt Macy if (!spa_feature_is_enabled(dp->dp_spa, f))
4528eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP));
4529eda14cbcSMatt Macy
4530eda14cbcSMatt Macy return (0);
4531eda14cbcSMatt Macy }
4532eda14cbcSMatt Macy
4533eda14cbcSMatt Macy static void
dsl_dataset_set_compression_sync(void * arg,dmu_tx_t * tx)4534eda14cbcSMatt Macy dsl_dataset_set_compression_sync(void *arg, dmu_tx_t *tx)
4535eda14cbcSMatt Macy {
4536eda14cbcSMatt Macy dsl_dataset_set_compression_arg_t *ddsca = arg;
4537eda14cbcSMatt Macy dsl_pool_t *dp = dmu_tx_pool(tx);
4538eda14cbcSMatt Macy dsl_dataset_t *ds = NULL;
4539eda14cbcSMatt Macy
4540eda14cbcSMatt Macy uint64_t compval = ZIO_COMPRESS_ALGO(ddsca->ddsca_value);
4541eda14cbcSMatt Macy spa_feature_t f = zio_compress_to_feature(compval);
4542c7046f76SMartin Matuska ASSERT3S(f, !=, SPA_FEATURE_NONE);
4543eda14cbcSMatt Macy ASSERT3S(spa_feature_table[f].fi_type, ==, ZFEATURE_TYPE_BOOLEAN);
4544eda14cbcSMatt Macy
4545eda14cbcSMatt Macy VERIFY0(dsl_dataset_hold(dp, ddsca->ddsca_name, FTAG, &ds));
4546eda14cbcSMatt Macy if (zfeature_active(f, ds->ds_feature[f]) != B_TRUE) {
4547eda14cbcSMatt Macy ds->ds_feature_activation[f] = (void *)B_TRUE;
4548eda14cbcSMatt Macy dsl_dataset_activate_feature(ds->ds_object, f,
4549eda14cbcSMatt Macy ds->ds_feature_activation[f], tx);
4550eda14cbcSMatt Macy ds->ds_feature[f] = ds->ds_feature_activation[f];
4551eda14cbcSMatt Macy }
4552eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4553eda14cbcSMatt Macy }
4554eda14cbcSMatt Macy
4555eda14cbcSMatt Macy int
dsl_dataset_set_compression(const char * dsname,zprop_source_t source,uint64_t compression)4556eda14cbcSMatt Macy dsl_dataset_set_compression(const char *dsname, zprop_source_t source,
4557eda14cbcSMatt Macy uint64_t compression)
4558eda14cbcSMatt Macy {
4559eda14cbcSMatt Macy dsl_dataset_set_compression_arg_t ddsca;
4560eda14cbcSMatt Macy
4561eda14cbcSMatt Macy /*
4562eda14cbcSMatt Macy * The sync task is only required for zstd in order to activate
4563eda14cbcSMatt Macy * the feature flag when the property is first set.
4564eda14cbcSMatt Macy */
4565eda14cbcSMatt Macy if (ZIO_COMPRESS_ALGO(compression) != ZIO_COMPRESS_ZSTD)
4566eda14cbcSMatt Macy return (0);
4567eda14cbcSMatt Macy
4568eda14cbcSMatt Macy ddsca.ddsca_name = dsname;
4569eda14cbcSMatt Macy ddsca.ddsca_source = source;
4570eda14cbcSMatt Macy ddsca.ddsca_value = compression;
4571eda14cbcSMatt Macy
4572eda14cbcSMatt Macy return (dsl_sync_task(dsname, dsl_dataset_set_compression_check,
4573eda14cbcSMatt Macy dsl_dataset_set_compression_sync, &ddsca, 0,
4574eda14cbcSMatt Macy ZFS_SPACE_CHECK_EXTRA_RESERVED));
4575eda14cbcSMatt Macy }
4576eda14cbcSMatt Macy
4577eda14cbcSMatt Macy /*
4578eda14cbcSMatt Macy * Return (in *usedp) the amount of space referenced by "new" that was not
4579eda14cbcSMatt Macy * referenced at the time the bookmark corresponds to. "New" may be a
4580eda14cbcSMatt Macy * snapshot or a head. The bookmark must be before new, in
4581eda14cbcSMatt Macy * new's filesystem (or its origin) -- caller verifies this.
4582eda14cbcSMatt Macy *
4583eda14cbcSMatt Macy * The written space is calculated by considering two components: First, we
4584eda14cbcSMatt Macy * ignore any freed space, and calculate the written as new's used space
4585eda14cbcSMatt Macy * minus old's used space. Next, we add in the amount of space that was freed
4586eda14cbcSMatt Macy * between the two time points, thus reducing new's used space relative to
4587eda14cbcSMatt Macy * old's. Specifically, this is the space that was born before
4588eda14cbcSMatt Macy * zbm_creation_txg, and freed before new (ie. on new's deadlist or a
4589eda14cbcSMatt Macy * previous deadlist).
4590eda14cbcSMatt Macy *
4591eda14cbcSMatt Macy * space freed [---------------------]
4592eda14cbcSMatt Macy * snapshots ---O-------O--------O-------O------
4593eda14cbcSMatt Macy * bookmark new
4594eda14cbcSMatt Macy *
4595eda14cbcSMatt Macy * Note, the bookmark's zbm_*_bytes_refd must be valid, but if the HAS_FBN
4596eda14cbcSMatt Macy * flag is not set, we will calculate the freed_before_next based on the
4597eda14cbcSMatt Macy * next snapshot's deadlist, rather than using zbm_*_freed_before_next_snap.
4598eda14cbcSMatt Macy */
4599eda14cbcSMatt Macy static int
dsl_dataset_space_written_impl(zfs_bookmark_phys_t * bmp,dsl_dataset_t * new,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)4600eda14cbcSMatt Macy dsl_dataset_space_written_impl(zfs_bookmark_phys_t *bmp,
4601eda14cbcSMatt Macy dsl_dataset_t *new, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4602eda14cbcSMatt Macy {
4603eda14cbcSMatt Macy int err = 0;
4604eda14cbcSMatt Macy dsl_pool_t *dp = new->ds_dir->dd_pool;
4605eda14cbcSMatt Macy
4606eda14cbcSMatt Macy ASSERT(dsl_pool_config_held(dp));
4607eda14cbcSMatt Macy if (dsl_dataset_is_snapshot(new)) {
4608eda14cbcSMatt Macy ASSERT3U(bmp->zbm_creation_txg, <,
4609eda14cbcSMatt Macy dsl_dataset_phys(new)->ds_creation_txg);
4610eda14cbcSMatt Macy }
4611eda14cbcSMatt Macy
4612eda14cbcSMatt Macy *usedp = 0;
4613eda14cbcSMatt Macy *usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
4614eda14cbcSMatt Macy *usedp -= bmp->zbm_referenced_bytes_refd;
4615eda14cbcSMatt Macy
4616eda14cbcSMatt Macy *compp = 0;
4617eda14cbcSMatt Macy *compp += dsl_dataset_phys(new)->ds_compressed_bytes;
4618eda14cbcSMatt Macy *compp -= bmp->zbm_compressed_bytes_refd;
4619eda14cbcSMatt Macy
4620eda14cbcSMatt Macy *uncompp = 0;
4621eda14cbcSMatt Macy *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
4622eda14cbcSMatt Macy *uncompp -= bmp->zbm_uncompressed_bytes_refd;
4623eda14cbcSMatt Macy
4624eda14cbcSMatt Macy dsl_dataset_t *snap = new;
4625eda14cbcSMatt Macy
4626eda14cbcSMatt Macy while (dsl_dataset_phys(snap)->ds_prev_snap_txg >
4627eda14cbcSMatt Macy bmp->zbm_creation_txg) {
4628eda14cbcSMatt Macy uint64_t used, comp, uncomp;
4629eda14cbcSMatt Macy
4630eda14cbcSMatt Macy dsl_deadlist_space_range(&snap->ds_deadlist,
4631eda14cbcSMatt Macy 0, bmp->zbm_creation_txg,
4632eda14cbcSMatt Macy &used, &comp, &uncomp);
4633eda14cbcSMatt Macy *usedp += used;
4634eda14cbcSMatt Macy *compp += comp;
4635eda14cbcSMatt Macy *uncompp += uncomp;
4636eda14cbcSMatt Macy
4637eda14cbcSMatt Macy uint64_t snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
4638eda14cbcSMatt Macy if (snap != new)
4639eda14cbcSMatt Macy dsl_dataset_rele(snap, FTAG);
4640eda14cbcSMatt Macy err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
4641eda14cbcSMatt Macy if (err != 0)
4642eda14cbcSMatt Macy break;
4643eda14cbcSMatt Macy }
4644eda14cbcSMatt Macy
4645eda14cbcSMatt Macy /*
4646eda14cbcSMatt Macy * We might not have the FBN if we are calculating written from
4647eda14cbcSMatt Macy * a snapshot (because we didn't know the correct "next" snapshot
4648eda14cbcSMatt Macy * until now).
4649eda14cbcSMatt Macy */
4650eda14cbcSMatt Macy if (bmp->zbm_flags & ZBM_FLAG_HAS_FBN) {
4651eda14cbcSMatt Macy *usedp += bmp->zbm_referenced_freed_before_next_snap;
4652eda14cbcSMatt Macy *compp += bmp->zbm_compressed_freed_before_next_snap;
4653eda14cbcSMatt Macy *uncompp += bmp->zbm_uncompressed_freed_before_next_snap;
4654eda14cbcSMatt Macy } else {
4655eda14cbcSMatt Macy ASSERT3U(dsl_dataset_phys(snap)->ds_prev_snap_txg, ==,
4656eda14cbcSMatt Macy bmp->zbm_creation_txg);
4657eda14cbcSMatt Macy uint64_t used, comp, uncomp;
4658eda14cbcSMatt Macy dsl_deadlist_space(&snap->ds_deadlist, &used, &comp, &uncomp);
4659eda14cbcSMatt Macy *usedp += used;
4660eda14cbcSMatt Macy *compp += comp;
4661eda14cbcSMatt Macy *uncompp += uncomp;
4662eda14cbcSMatt Macy }
4663eda14cbcSMatt Macy if (snap != new)
4664eda14cbcSMatt Macy dsl_dataset_rele(snap, FTAG);
4665eda14cbcSMatt Macy return (err);
4666eda14cbcSMatt Macy }
4667eda14cbcSMatt Macy
4668eda14cbcSMatt Macy /*
4669eda14cbcSMatt Macy * Return (in *usedp) the amount of space written in new that was not
4670eda14cbcSMatt Macy * present at the time the bookmark corresponds to. New may be a
4671eda14cbcSMatt Macy * snapshot or the head. Old must be a bookmark before new, in
4672eda14cbcSMatt Macy * new's filesystem (or its origin) -- caller verifies this.
4673eda14cbcSMatt Macy */
4674eda14cbcSMatt Macy int
dsl_dataset_space_written_bookmark(zfs_bookmark_phys_t * bmp,dsl_dataset_t * new,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)4675eda14cbcSMatt Macy dsl_dataset_space_written_bookmark(zfs_bookmark_phys_t *bmp,
4676eda14cbcSMatt Macy dsl_dataset_t *new, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4677eda14cbcSMatt Macy {
4678eda14cbcSMatt Macy if (!(bmp->zbm_flags & ZBM_FLAG_HAS_FBN))
4679eda14cbcSMatt Macy return (SET_ERROR(ENOTSUP));
4680eda14cbcSMatt Macy return (dsl_dataset_space_written_impl(bmp, new,
4681eda14cbcSMatt Macy usedp, compp, uncompp));
4682eda14cbcSMatt Macy }
4683eda14cbcSMatt Macy
4684eda14cbcSMatt Macy /*
4685eda14cbcSMatt Macy * Return (in *usedp) the amount of space written in new that is not
4686eda14cbcSMatt Macy * present in oldsnap. New may be a snapshot or the head. Old must be
4687eda14cbcSMatt Macy * a snapshot before new, in new's filesystem (or its origin). If not then
4688eda14cbcSMatt Macy * fail and return EINVAL.
4689eda14cbcSMatt Macy */
4690eda14cbcSMatt Macy int
dsl_dataset_space_written(dsl_dataset_t * oldsnap,dsl_dataset_t * new,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)4691eda14cbcSMatt Macy dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
4692eda14cbcSMatt Macy uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4693eda14cbcSMatt Macy {
4694eda14cbcSMatt Macy if (!dsl_dataset_is_before(new, oldsnap, 0))
4695eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
4696eda14cbcSMatt Macy
4697eda14cbcSMatt Macy zfs_bookmark_phys_t zbm = { 0 };
4698eda14cbcSMatt Macy dsl_dataset_phys_t *dsp = dsl_dataset_phys(oldsnap);
4699eda14cbcSMatt Macy zbm.zbm_guid = dsp->ds_guid;
4700eda14cbcSMatt Macy zbm.zbm_creation_txg = dsp->ds_creation_txg;
4701eda14cbcSMatt Macy zbm.zbm_creation_time = dsp->ds_creation_time;
4702eda14cbcSMatt Macy zbm.zbm_referenced_bytes_refd = dsp->ds_referenced_bytes;
4703eda14cbcSMatt Macy zbm.zbm_compressed_bytes_refd = dsp->ds_compressed_bytes;
4704eda14cbcSMatt Macy zbm.zbm_uncompressed_bytes_refd = dsp->ds_uncompressed_bytes;
4705eda14cbcSMatt Macy
4706eda14cbcSMatt Macy /*
4707eda14cbcSMatt Macy * If oldsnap is the origin (or origin's origin, ...) of new,
4708eda14cbcSMatt Macy * we can't easily calculate the effective FBN. Therefore,
4709eda14cbcSMatt Macy * we do not set ZBM_FLAG_HAS_FBN, so that the _impl will calculate
4710eda14cbcSMatt Macy * it relative to the correct "next": the next snapshot towards "new",
4711eda14cbcSMatt Macy * rather than the next snapshot in oldsnap's dsl_dir.
4712eda14cbcSMatt Macy */
4713eda14cbcSMatt Macy return (dsl_dataset_space_written_impl(&zbm, new,
4714eda14cbcSMatt Macy usedp, compp, uncompp));
4715eda14cbcSMatt Macy }
4716eda14cbcSMatt Macy
4717eda14cbcSMatt Macy /*
4718eda14cbcSMatt Macy * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
4719eda14cbcSMatt Macy * lastsnap, and all snapshots in between are deleted.
4720eda14cbcSMatt Macy *
4721eda14cbcSMatt Macy * blocks that would be freed [---------------------------]
4722eda14cbcSMatt Macy * snapshots ---O-------O--------O-------O--------O
4723eda14cbcSMatt Macy * firstsnap lastsnap
4724eda14cbcSMatt Macy *
4725eda14cbcSMatt Macy * This is the set of blocks that were born after the snap before firstsnap,
4726eda14cbcSMatt Macy * (birth > firstsnap->prev_snap_txg) and died before the snap after the
4727eda14cbcSMatt Macy * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
4728eda14cbcSMatt Macy * We calculate this by iterating over the relevant deadlists (from the snap
4729eda14cbcSMatt Macy * after lastsnap, backward to the snap after firstsnap), summing up the
4730eda14cbcSMatt Macy * space on the deadlist that was born after the snap before firstsnap.
4731eda14cbcSMatt Macy */
4732eda14cbcSMatt Macy int
dsl_dataset_space_wouldfree(dsl_dataset_t * firstsnap,dsl_dataset_t * lastsnap,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)4733eda14cbcSMatt Macy dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
4734eda14cbcSMatt Macy dsl_dataset_t *lastsnap,
4735eda14cbcSMatt Macy uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4736eda14cbcSMatt Macy {
4737eda14cbcSMatt Macy int err = 0;
4738eda14cbcSMatt Macy uint64_t snapobj;
4739eda14cbcSMatt Macy dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
4740eda14cbcSMatt Macy
4741eda14cbcSMatt Macy ASSERT(firstsnap->ds_is_snapshot);
4742eda14cbcSMatt Macy ASSERT(lastsnap->ds_is_snapshot);
4743eda14cbcSMatt Macy
4744eda14cbcSMatt Macy /*
4745eda14cbcSMatt Macy * Check that the snapshots are in the same dsl_dir, and firstsnap
4746eda14cbcSMatt Macy * is before lastsnap.
4747eda14cbcSMatt Macy */
4748eda14cbcSMatt Macy if (firstsnap->ds_dir != lastsnap->ds_dir ||
4749eda14cbcSMatt Macy dsl_dataset_phys(firstsnap)->ds_creation_txg >
4750eda14cbcSMatt Macy dsl_dataset_phys(lastsnap)->ds_creation_txg)
4751eda14cbcSMatt Macy return (SET_ERROR(EINVAL));
4752eda14cbcSMatt Macy
4753eda14cbcSMatt Macy *usedp = *compp = *uncompp = 0;
4754eda14cbcSMatt Macy
4755eda14cbcSMatt Macy snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
4756eda14cbcSMatt Macy while (snapobj != firstsnap->ds_object) {
4757eda14cbcSMatt Macy dsl_dataset_t *ds;
4758eda14cbcSMatt Macy uint64_t used, comp, uncomp;
4759eda14cbcSMatt Macy
4760eda14cbcSMatt Macy err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
4761eda14cbcSMatt Macy if (err != 0)
4762eda14cbcSMatt Macy break;
4763eda14cbcSMatt Macy
4764eda14cbcSMatt Macy dsl_deadlist_space_range(&ds->ds_deadlist,
4765eda14cbcSMatt Macy dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
4766eda14cbcSMatt Macy &used, &comp, &uncomp);
4767eda14cbcSMatt Macy *usedp += used;
4768eda14cbcSMatt Macy *compp += comp;
4769eda14cbcSMatt Macy *uncompp += uncomp;
4770eda14cbcSMatt Macy
4771eda14cbcSMatt Macy snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
4772eda14cbcSMatt Macy ASSERT3U(snapobj, !=, 0);
4773eda14cbcSMatt Macy dsl_dataset_rele(ds, FTAG);
4774eda14cbcSMatt Macy }
4775eda14cbcSMatt Macy return (err);
4776eda14cbcSMatt Macy }
4777eda14cbcSMatt Macy
4778eda14cbcSMatt Macy /*
4779eda14cbcSMatt Macy * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
4780eda14cbcSMatt Macy * For example, they could both be snapshots of the same filesystem, and
4781eda14cbcSMatt Macy * 'earlier' is before 'later'. Or 'earlier' could be the origin of
4782eda14cbcSMatt Macy * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
4783eda14cbcSMatt Macy * filesystem. Or 'earlier' could be the origin's origin.
4784eda14cbcSMatt Macy *
4785eda14cbcSMatt Macy * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
4786eda14cbcSMatt Macy */
4787eda14cbcSMatt Macy boolean_t
dsl_dataset_is_before(dsl_dataset_t * later,dsl_dataset_t * earlier,uint64_t earlier_txg)4788eda14cbcSMatt Macy dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
4789eda14cbcSMatt Macy uint64_t earlier_txg)
4790eda14cbcSMatt Macy {
4791eda14cbcSMatt Macy dsl_pool_t *dp = later->ds_dir->dd_pool;
4792eda14cbcSMatt Macy int error;
4793eda14cbcSMatt Macy boolean_t ret;
4794eda14cbcSMatt Macy
4795eda14cbcSMatt Macy ASSERT(dsl_pool_config_held(dp));
4796eda14cbcSMatt Macy ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
4797eda14cbcSMatt Macy
4798eda14cbcSMatt Macy if (earlier_txg == 0)
4799eda14cbcSMatt Macy earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
4800eda14cbcSMatt Macy
4801eda14cbcSMatt Macy if (later->ds_is_snapshot &&
4802eda14cbcSMatt Macy earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
4803eda14cbcSMatt Macy return (B_FALSE);
4804eda14cbcSMatt Macy
4805eda14cbcSMatt Macy if (later->ds_dir == earlier->ds_dir)
4806eda14cbcSMatt Macy return (B_TRUE);
4807eda14cbcSMatt Macy
4808eda14cbcSMatt Macy /*
4809eda14cbcSMatt Macy * We check dd_origin_obj explicitly here rather than using
4810eda14cbcSMatt Macy * dsl_dir_is_clone() so that we will return TRUE if "earlier"
4811eda14cbcSMatt Macy * is $ORIGIN@$ORIGIN. dsl_dataset_space_written() depends on
4812eda14cbcSMatt Macy * this behavior.
4813eda14cbcSMatt Macy */
4814eda14cbcSMatt Macy if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == 0)
4815eda14cbcSMatt Macy return (B_FALSE);
4816eda14cbcSMatt Macy
4817eda14cbcSMatt Macy dsl_dataset_t *origin;
4818eda14cbcSMatt Macy error = dsl_dataset_hold_obj(dp,
4819eda14cbcSMatt Macy dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
4820eda14cbcSMatt Macy if (error != 0)
4821eda14cbcSMatt Macy return (B_FALSE);
4822eda14cbcSMatt Macy if (dsl_dataset_phys(origin)->ds_creation_txg == earlier_txg &&
4823eda14cbcSMatt Macy origin->ds_dir == earlier->ds_dir) {
4824eda14cbcSMatt Macy dsl_dataset_rele(origin, FTAG);
4825eda14cbcSMatt Macy return (B_TRUE);
4826eda14cbcSMatt Macy }
4827eda14cbcSMatt Macy ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
4828eda14cbcSMatt Macy dsl_dataset_rele(origin, FTAG);
4829eda14cbcSMatt Macy return (ret);
4830eda14cbcSMatt Macy }
4831eda14cbcSMatt Macy
4832eda14cbcSMatt Macy void
dsl_dataset_zapify(dsl_dataset_t * ds,dmu_tx_t * tx)4833eda14cbcSMatt Macy dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
4834eda14cbcSMatt Macy {
4835eda14cbcSMatt Macy objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
4836eda14cbcSMatt Macy dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
4837eda14cbcSMatt Macy }
4838eda14cbcSMatt Macy
4839eda14cbcSMatt Macy boolean_t
dsl_dataset_is_zapified(dsl_dataset_t * ds)4840eda14cbcSMatt Macy dsl_dataset_is_zapified(dsl_dataset_t *ds)
4841eda14cbcSMatt Macy {
4842eda14cbcSMatt Macy dmu_object_info_t doi;
4843eda14cbcSMatt Macy
4844eda14cbcSMatt Macy dmu_object_info_from_db(ds->ds_dbuf, &doi);
4845eda14cbcSMatt Macy return (doi.doi_type == DMU_OTN_ZAP_METADATA);
4846eda14cbcSMatt Macy }
4847eda14cbcSMatt Macy
4848eda14cbcSMatt Macy boolean_t
dsl_dataset_has_resume_receive_state(dsl_dataset_t * ds)4849eda14cbcSMatt Macy dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds)
4850eda14cbcSMatt Macy {
4851eda14cbcSMatt Macy return (dsl_dataset_is_zapified(ds) &&
4852eda14cbcSMatt Macy zap_contains(ds->ds_dir->dd_pool->dp_meta_objset,
4853eda14cbcSMatt Macy ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0);
4854eda14cbcSMatt Macy }
4855eda14cbcSMatt Macy
4856eda14cbcSMatt Macy uint64_t
dsl_dataset_get_remap_deadlist_object(dsl_dataset_t * ds)4857eda14cbcSMatt Macy dsl_dataset_get_remap_deadlist_object(dsl_dataset_t *ds)
4858eda14cbcSMatt Macy {
4859eda14cbcSMatt Macy uint64_t remap_deadlist_obj;
4860eda14cbcSMatt Macy int err;
4861eda14cbcSMatt Macy
4862eda14cbcSMatt Macy if (!dsl_dataset_is_zapified(ds))
4863eda14cbcSMatt Macy return (0);
4864eda14cbcSMatt Macy
4865eda14cbcSMatt Macy err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, ds->ds_object,
4866eda14cbcSMatt Macy DS_FIELD_REMAP_DEADLIST, sizeof (remap_deadlist_obj), 1,
4867eda14cbcSMatt Macy &remap_deadlist_obj);
4868eda14cbcSMatt Macy
4869eda14cbcSMatt Macy if (err != 0) {
4870eda14cbcSMatt Macy VERIFY3S(err, ==, ENOENT);
4871eda14cbcSMatt Macy return (0);
4872eda14cbcSMatt Macy }
4873eda14cbcSMatt Macy
4874eda14cbcSMatt Macy ASSERT(remap_deadlist_obj != 0);
4875eda14cbcSMatt Macy return (remap_deadlist_obj);
4876eda14cbcSMatt Macy }
4877eda14cbcSMatt Macy
4878eda14cbcSMatt Macy boolean_t
dsl_dataset_remap_deadlist_exists(dsl_dataset_t * ds)4879eda14cbcSMatt Macy dsl_dataset_remap_deadlist_exists(dsl_dataset_t *ds)
4880eda14cbcSMatt Macy {
4881eda14cbcSMatt Macy EQUIV(dsl_deadlist_is_open(&ds->ds_remap_deadlist),
4882eda14cbcSMatt Macy dsl_dataset_get_remap_deadlist_object(ds) != 0);
4883eda14cbcSMatt Macy return (dsl_deadlist_is_open(&ds->ds_remap_deadlist));
4884eda14cbcSMatt Macy }
4885eda14cbcSMatt Macy
4886eda14cbcSMatt Macy static void
dsl_dataset_set_remap_deadlist_object(dsl_dataset_t * ds,uint64_t obj,dmu_tx_t * tx)4887eda14cbcSMatt Macy dsl_dataset_set_remap_deadlist_object(dsl_dataset_t *ds, uint64_t obj,
4888eda14cbcSMatt Macy dmu_tx_t *tx)
4889eda14cbcSMatt Macy {
4890eda14cbcSMatt Macy ASSERT(obj != 0);
4891eda14cbcSMatt Macy dsl_dataset_zapify(ds, tx);
4892eda14cbcSMatt Macy VERIFY0(zap_add(ds->ds_dir->dd_pool->dp_meta_objset, ds->ds_object,
4893eda14cbcSMatt Macy DS_FIELD_REMAP_DEADLIST, sizeof (obj), 1, &obj, tx));
4894eda14cbcSMatt Macy }
4895eda14cbcSMatt Macy
4896eda14cbcSMatt Macy static void
dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t * ds,dmu_tx_t * tx)4897eda14cbcSMatt Macy dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t *ds, dmu_tx_t *tx)
4898eda14cbcSMatt Macy {
4899eda14cbcSMatt Macy VERIFY0(zap_remove(ds->ds_dir->dd_pool->dp_meta_objset,
4900eda14cbcSMatt Macy ds->ds_object, DS_FIELD_REMAP_DEADLIST, tx));
4901eda14cbcSMatt Macy }
4902eda14cbcSMatt Macy
4903eda14cbcSMatt Macy void
dsl_dataset_destroy_remap_deadlist(dsl_dataset_t * ds,dmu_tx_t * tx)4904eda14cbcSMatt Macy dsl_dataset_destroy_remap_deadlist(dsl_dataset_t *ds, dmu_tx_t *tx)
4905eda14cbcSMatt Macy {
4906eda14cbcSMatt Macy uint64_t remap_deadlist_object;
4907eda14cbcSMatt Macy spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
4908eda14cbcSMatt Macy
4909eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx));
4910eda14cbcSMatt Macy ASSERT(dsl_dataset_remap_deadlist_exists(ds));
4911eda14cbcSMatt Macy
4912eda14cbcSMatt Macy remap_deadlist_object = ds->ds_remap_deadlist.dl_object;
4913eda14cbcSMatt Macy dsl_deadlist_close(&ds->ds_remap_deadlist);
4914eda14cbcSMatt Macy dsl_deadlist_free(spa_meta_objset(spa), remap_deadlist_object, tx);
4915eda14cbcSMatt Macy dsl_dataset_unset_remap_deadlist_object(ds, tx);
4916eda14cbcSMatt Macy spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
4917eda14cbcSMatt Macy }
4918eda14cbcSMatt Macy
4919eda14cbcSMatt Macy void
dsl_dataset_create_remap_deadlist(dsl_dataset_t * ds,dmu_tx_t * tx)4920eda14cbcSMatt Macy dsl_dataset_create_remap_deadlist(dsl_dataset_t *ds, dmu_tx_t *tx)
4921eda14cbcSMatt Macy {
4922eda14cbcSMatt Macy uint64_t remap_deadlist_obj;
4923eda14cbcSMatt Macy spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
4924eda14cbcSMatt Macy
4925eda14cbcSMatt Macy ASSERT(dmu_tx_is_syncing(tx));
4926eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&ds->ds_remap_deadlist_lock));
4927eda14cbcSMatt Macy /*
4928eda14cbcSMatt Macy * Currently we only create remap deadlists when there are indirect
4929eda14cbcSMatt Macy * vdevs with referenced mappings.
4930eda14cbcSMatt Macy */
4931eda14cbcSMatt Macy ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
4932eda14cbcSMatt Macy
4933eda14cbcSMatt Macy remap_deadlist_obj = dsl_deadlist_clone(
4934eda14cbcSMatt Macy &ds->ds_deadlist, UINT64_MAX,
4935eda14cbcSMatt Macy dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
4936eda14cbcSMatt Macy dsl_dataset_set_remap_deadlist_object(ds,
4937eda14cbcSMatt Macy remap_deadlist_obj, tx);
4938eda14cbcSMatt Macy dsl_deadlist_open(&ds->ds_remap_deadlist, spa_meta_objset(spa),
4939eda14cbcSMatt Macy remap_deadlist_obj);
4940eda14cbcSMatt Macy spa_feature_incr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
4941eda14cbcSMatt Macy }
4942eda14cbcSMatt Macy
4943eda14cbcSMatt Macy void
dsl_dataset_activate_redaction(dsl_dataset_t * ds,uint64_t * redact_snaps,uint64_t num_redact_snaps,dmu_tx_t * tx)4944eda14cbcSMatt Macy dsl_dataset_activate_redaction(dsl_dataset_t *ds, uint64_t *redact_snaps,
4945eda14cbcSMatt Macy uint64_t num_redact_snaps, dmu_tx_t *tx)
4946eda14cbcSMatt Macy {
4947eda14cbcSMatt Macy uint64_t dsobj = ds->ds_object;
4948eda14cbcSMatt Macy struct feature_type_uint64_array_arg *ftuaa =
4949eda14cbcSMatt Macy kmem_zalloc(sizeof (*ftuaa), KM_SLEEP);
4950eda14cbcSMatt Macy ftuaa->length = (int64_t)num_redact_snaps;
4951eda14cbcSMatt Macy if (num_redact_snaps > 0) {
4952eda14cbcSMatt Macy ftuaa->array = kmem_alloc(num_redact_snaps * sizeof (uint64_t),
4953eda14cbcSMatt Macy KM_SLEEP);
4954da5137abSMartin Matuska memcpy(ftuaa->array, redact_snaps, num_redact_snaps *
4955eda14cbcSMatt Macy sizeof (uint64_t));
4956eda14cbcSMatt Macy }
4957eda14cbcSMatt Macy dsl_dataset_activate_feature(dsobj, SPA_FEATURE_REDACTED_DATASETS,
4958eda14cbcSMatt Macy ftuaa, tx);
4959eda14cbcSMatt Macy ds->ds_feature[SPA_FEATURE_REDACTED_DATASETS] = ftuaa;
4960eda14cbcSMatt Macy }
4961eda14cbcSMatt Macy
4962716fd348SMartin Matuska /*
4963716fd348SMartin Matuska * Find and return (in *oldest_dsobj) the oldest snapshot of the dsobj
4964716fd348SMartin Matuska * dataset whose birth time is >= min_txg.
4965716fd348SMartin Matuska */
4966716fd348SMartin Matuska int
dsl_dataset_oldest_snapshot(spa_t * spa,uint64_t head_ds,uint64_t min_txg,uint64_t * oldest_dsobj)4967716fd348SMartin Matuska dsl_dataset_oldest_snapshot(spa_t *spa, uint64_t head_ds, uint64_t min_txg,
4968716fd348SMartin Matuska uint64_t *oldest_dsobj)
4969716fd348SMartin Matuska {
4970716fd348SMartin Matuska dsl_dataset_t *ds;
4971716fd348SMartin Matuska dsl_pool_t *dp = spa->spa_dsl_pool;
4972716fd348SMartin Matuska
4973716fd348SMartin Matuska int error = dsl_dataset_hold_obj(dp, head_ds, FTAG, &ds);
4974716fd348SMartin Matuska if (error != 0)
4975716fd348SMartin Matuska return (error);
4976716fd348SMartin Matuska
4977716fd348SMartin Matuska uint64_t prev_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
4978716fd348SMartin Matuska uint64_t prev_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
4979716fd348SMartin Matuska
4980716fd348SMartin Matuska while (prev_obj != 0 && min_txg < prev_obj_txg) {
4981716fd348SMartin Matuska dsl_dataset_rele(ds, FTAG);
4982716fd348SMartin Matuska if ((error = dsl_dataset_hold_obj(dp, prev_obj,
4983716fd348SMartin Matuska FTAG, &ds)) != 0)
4984716fd348SMartin Matuska return (error);
4985716fd348SMartin Matuska prev_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
4986716fd348SMartin Matuska prev_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
4987716fd348SMartin Matuska }
4988716fd348SMartin Matuska *oldest_dsobj = ds->ds_object;
4989716fd348SMartin Matuska dsl_dataset_rele(ds, FTAG);
4990716fd348SMartin Matuska return (0);
4991716fd348SMartin Matuska }
4992716fd348SMartin Matuska
4993be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, max_recordsize, UINT, ZMOD_RW,
4994eda14cbcSMatt Macy "Max allowed record size");
4995eda14cbcSMatt Macy
4996eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, allow_redacted_dataset_mount, INT, ZMOD_RW,
4997eda14cbcSMatt Macy "Allow mounting of redacted datasets");
4998eda14cbcSMatt Macy
499908aba0aeSMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, snapshot_history_enabled, INT, ZMOD_RW,
500008aba0aeSMartin Matuska "Include snapshot events in pool history/events");
500108aba0aeSMartin Matuska
5002eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_hold);
5003eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_hold_flags);
5004eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_hold_obj);
5005eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_hold_obj_flags);
5006eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_own);
5007eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_own_obj);
5008eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_name);
5009eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_rele);
5010eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_rele_flags);
5011eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_disown);
5012eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_tryown);
5013eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_create_sync);
5014eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_create_sync_dd);
5015eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_snapshot_check);
5016eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_snapshot_sync);
5017eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_promote);
5018eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_user_hold);
5019eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_user_release);
5020eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_get_holds);
5021eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_get_blkptr);
5022eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_get_spa);
5023eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_modified_since_snap);
5024eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_space_written);
5025eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_space_wouldfree);
5026eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_sync);
5027eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_block_born);
5028eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_block_kill);
5029eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_dirty);
5030eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_stats);
5031eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_fast_stat);
5032eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_space);
5033eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_fsid_guid);
5034eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dsobj_to_dsname);
5035eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_check_quota);
5036eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl);
5037eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl);
5038