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