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