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