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.
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.
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]
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]
35 * [1] Portions of this software were developed by Allan Jude
36 * under sponsorship from the FreeBSD Foundation.
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>
50 #include <sys/zfeature.h>
51 #include <sys/unique.h>
52 #include <sys/zfs_context.h>
53 #include <sys/zfs_ioctl.h>
55 #include <sys/spa_impl.h>
57 #include <sys/zfs_znode.h>
58 #include <sys/zfs_onexit.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>
73 * The SPA supports block sizes up to 16MB. However, very large blocks
74 * can have an impact on i/o latency (e.g. tying up a spinning disk for
75 * ~300ms), and also potentially on the memory allocator. Therefore,
76 * we did not allow the recordsize to be set larger than zfs_max_recordsize
77 * (former default: 1MB). Larger blocks could be created by changing this
78 * tunable, and pools with larger blocks could always be imported and used,
79 * regardless of this setting.
81 * We do, however, still limit it by default to 1M on x86_32, because Linux's
82 * 3/1 memory split doesn't leave much room for 16M chunks.
85 uint_t zfs_max_recordsize
= 1 * 1024 * 1024;
87 uint_t zfs_max_recordsize
= 16 * 1024 * 1024;
89 static int zfs_allow_redacted_dataset_mount
= 0;
91 int zfs_snapshot_history_enabled
= 1;
93 #define SWITCH64(x, y) \
95 uint64_t __tmp = (x); \
100 #define DS_REF_MAX (1ULL << 62)
102 static void dsl_dataset_set_remap_deadlist_object(dsl_dataset_t
*ds
,
103 uint64_t obj
, dmu_tx_t
*tx
);
104 static void dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t
*ds
,
107 static void unload_zfeature(dsl_dataset_t
*ds
, spa_feature_t f
);
109 extern uint_t spa_asize_inflation
;
111 static zil_header_t zero_zil
;
114 * Figure out how much of this delta should be propagated to the dsl_dir
115 * layer. If there's a refreservation, that space has already been
116 * partially accounted for in our ancestors.
119 parent_delta(dsl_dataset_t
*ds
, int64_t delta
)
121 dsl_dataset_phys_t
*ds_phys
;
122 uint64_t old_bytes
, new_bytes
;
124 if (ds
->ds_reserved
== 0)
127 ds_phys
= dsl_dataset_phys(ds
);
128 old_bytes
= MAX(ds_phys
->ds_unique_bytes
, ds
->ds_reserved
);
129 new_bytes
= MAX(ds_phys
->ds_unique_bytes
+ delta
, ds
->ds_reserved
);
131 ASSERT3U(ABS((int64_t)(new_bytes
- old_bytes
)), <=, ABS(delta
));
132 return (new_bytes
- old_bytes
);
136 dsl_dataset_block_born(dsl_dataset_t
*ds
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
138 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
139 int used
= bp_get_dsize_sync(spa
, bp
);
140 int compressed
= BP_GET_PSIZE(bp
);
141 int uncompressed
= BP_GET_UCSIZE(bp
);
145 dprintf_bp(bp
, "ds=%p", ds
);
147 ASSERT(dmu_tx_is_syncing(tx
));
148 /* It could have been compressed away to nothing */
149 if (BP_IS_HOLE(bp
) || BP_IS_REDACTED(bp
))
151 ASSERT(BP_GET_TYPE(bp
) != DMU_OT_NONE
);
152 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp
)));
154 dsl_pool_mos_diduse_space(tx
->tx_pool
,
155 used
, compressed
, uncompressed
);
159 ASSERT3U(bp
->blk_birth
, >, dsl_dataset_phys(ds
)->ds_prev_snap_txg
);
160 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
161 mutex_enter(&ds
->ds_lock
);
162 delta
= parent_delta(ds
, used
);
163 dsl_dataset_phys(ds
)->ds_referenced_bytes
+= used
;
164 dsl_dataset_phys(ds
)->ds_compressed_bytes
+= compressed
;
165 dsl_dataset_phys(ds
)->ds_uncompressed_bytes
+= uncompressed
;
166 dsl_dataset_phys(ds
)->ds_unique_bytes
+= used
;
168 if (BP_GET_LSIZE(bp
) > SPA_OLD_MAXBLOCKSIZE
) {
169 ds
->ds_feature_activation
[SPA_FEATURE_LARGE_BLOCKS
] =
174 f
= zio_checksum_to_feature(BP_GET_CHECKSUM(bp
));
175 if (f
!= SPA_FEATURE_NONE
) {
176 ASSERT3S(spa_feature_table
[f
].fi_type
, ==,
177 ZFEATURE_TYPE_BOOLEAN
);
178 ds
->ds_feature_activation
[f
] = (void *)B_TRUE
;
181 f
= zio_compress_to_feature(BP_GET_COMPRESS(bp
));
182 if (f
!= SPA_FEATURE_NONE
) {
183 ASSERT3S(spa_feature_table
[f
].fi_type
, ==,
184 ZFEATURE_TYPE_BOOLEAN
);
185 ds
->ds_feature_activation
[f
] = (void *)B_TRUE
;
189 * Track block for livelist, but ignore embedded blocks because
190 * they do not need to be freed.
192 if (dsl_deadlist_is_open(&ds
->ds_dir
->dd_livelist
) &&
193 bp
->blk_birth
> ds
->ds_dir
->dd_origin_txg
&&
194 !(BP_IS_EMBEDDED(bp
))) {
195 ASSERT(dsl_dir_is_clone(ds
->ds_dir
));
196 ASSERT(spa_feature_is_enabled(spa
,
197 SPA_FEATURE_LIVELIST
));
198 bplist_append(&ds
->ds_dir
->dd_pending_allocs
, bp
);
201 mutex_exit(&ds
->ds_lock
);
202 dsl_dir_diduse_transfer_space(ds
->ds_dir
, delta
,
203 compressed
, uncompressed
, used
,
204 DD_USED_REFRSRV
, DD_USED_HEAD
, tx
);
208 * Called when the specified segment has been remapped, and is thus no
209 * longer referenced in the head dataset. The vdev must be indirect.
211 * If the segment is referenced by a snapshot, put it on the remap deadlist.
212 * Otherwise, add this segment to the obsolete spacemap.
215 dsl_dataset_block_remapped(dsl_dataset_t
*ds
, uint64_t vdev
, uint64_t offset
,
216 uint64_t size
, uint64_t birth
, dmu_tx_t
*tx
)
218 spa_t
*spa
= ds
->ds_dir
->dd_pool
->dp_spa
;
220 ASSERT(dmu_tx_is_syncing(tx
));
221 ASSERT(birth
<= tx
->tx_txg
);
222 ASSERT(!ds
->ds_is_snapshot
);
224 if (birth
> dsl_dataset_phys(ds
)->ds_prev_snap_txg
) {
225 spa_vdev_indirect_mark_obsolete(spa
, vdev
, offset
, size
, tx
);
228 dva_t
*dva
= &fakebp
.blk_dva
[0];
232 mutex_enter(&ds
->ds_remap_deadlist_lock
);
233 if (!dsl_dataset_remap_deadlist_exists(ds
)) {
234 dsl_dataset_create_remap_deadlist(ds
, tx
);
236 mutex_exit(&ds
->ds_remap_deadlist_lock
);
239 fakebp
.blk_birth
= birth
;
240 DVA_SET_VDEV(dva
, vdev
);
241 DVA_SET_OFFSET(dva
, offset
);
242 DVA_SET_ASIZE(dva
, size
);
243 dsl_deadlist_insert(&ds
->ds_remap_deadlist
, &fakebp
, B_FALSE
,
249 dsl_dataset_block_kill(dsl_dataset_t
*ds
, const blkptr_t
*bp
, dmu_tx_t
*tx
,
252 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
254 int used
= bp_get_dsize_sync(spa
, bp
);
255 int compressed
= BP_GET_PSIZE(bp
);
256 int uncompressed
= BP_GET_UCSIZE(bp
);
258 if (BP_IS_HOLE(bp
) || BP_IS_REDACTED(bp
))
261 ASSERT(dmu_tx_is_syncing(tx
));
262 ASSERT(bp
->blk_birth
<= tx
->tx_txg
);
265 dsl_free(tx
->tx_pool
, tx
->tx_txg
, bp
);
266 dsl_pool_mos_diduse_space(tx
->tx_pool
,
267 -used
, -compressed
, -uncompressed
);
270 ASSERT3P(tx
->tx_pool
, ==, ds
->ds_dir
->dd_pool
);
272 ASSERT(!ds
->ds_is_snapshot
);
273 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
276 * Track block for livelist, but ignore embedded blocks because
277 * they do not need to be freed.
279 if (dsl_deadlist_is_open(&ds
->ds_dir
->dd_livelist
) &&
280 bp
->blk_birth
> ds
->ds_dir
->dd_origin_txg
&&
281 !(BP_IS_EMBEDDED(bp
))) {
282 ASSERT(dsl_dir_is_clone(ds
->ds_dir
));
283 ASSERT(spa_feature_is_enabled(spa
,
284 SPA_FEATURE_LIVELIST
));
285 bplist_append(&ds
->ds_dir
->dd_pending_frees
, bp
);
288 if (bp
->blk_birth
> dsl_dataset_phys(ds
)->ds_prev_snap_txg
) {
291 dprintf_bp(bp
, "freeing ds=%llu", (u_longlong_t
)ds
->ds_object
);
292 dsl_free(tx
->tx_pool
, tx
->tx_txg
, bp
);
294 mutex_enter(&ds
->ds_lock
);
295 ASSERT(dsl_dataset_phys(ds
)->ds_unique_bytes
>= used
||
296 !DS_UNIQUE_IS_ACCURATE(ds
));
297 delta
= parent_delta(ds
, -used
);
298 dsl_dataset_phys(ds
)->ds_unique_bytes
-= used
;
299 mutex_exit(&ds
->ds_lock
);
300 dsl_dir_diduse_transfer_space(ds
->ds_dir
,
301 delta
, -compressed
, -uncompressed
, -used
,
302 DD_USED_REFRSRV
, DD_USED_HEAD
, tx
);
304 dprintf_bp(bp
, "putting on dead list: %s", "");
307 * We are here as part of zio's write done callback,
308 * which means we're a zio interrupt thread. We can't
309 * call dsl_deadlist_insert() now because it may block
310 * waiting for I/O. Instead, put bp on the deferred
311 * queue and let dsl_pool_sync() finish the job.
313 bplist_append(&ds
->ds_pending_deadlist
, bp
);
315 dsl_deadlist_insert(&ds
->ds_deadlist
, bp
, B_FALSE
, tx
);
317 ASSERT3U(ds
->ds_prev
->ds_object
, ==,
318 dsl_dataset_phys(ds
)->ds_prev_snap_obj
);
319 ASSERT(dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
> 0);
320 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
321 if (dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
==
322 ds
->ds_object
&& bp
->blk_birth
>
323 dsl_dataset_phys(ds
->ds_prev
)->ds_prev_snap_txg
) {
324 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
325 mutex_enter(&ds
->ds_prev
->ds_lock
);
326 dsl_dataset_phys(ds
->ds_prev
)->ds_unique_bytes
+= used
;
327 mutex_exit(&ds
->ds_prev
->ds_lock
);
329 if (bp
->blk_birth
> ds
->ds_dir
->dd_origin_txg
) {
330 dsl_dir_transfer_space(ds
->ds_dir
, used
,
331 DD_USED_HEAD
, DD_USED_SNAP
, tx
);
335 dsl_bookmark_block_killed(ds
, bp
, tx
);
337 mutex_enter(&ds
->ds_lock
);
338 ASSERT3U(dsl_dataset_phys(ds
)->ds_referenced_bytes
, >=, used
);
339 dsl_dataset_phys(ds
)->ds_referenced_bytes
-= used
;
340 ASSERT3U(dsl_dataset_phys(ds
)->ds_compressed_bytes
, >=, compressed
);
341 dsl_dataset_phys(ds
)->ds_compressed_bytes
-= compressed
;
342 ASSERT3U(dsl_dataset_phys(ds
)->ds_uncompressed_bytes
, >=, uncompressed
);
343 dsl_dataset_phys(ds
)->ds_uncompressed_bytes
-= uncompressed
;
344 mutex_exit(&ds
->ds_lock
);
349 struct feature_type_uint64_array_arg
{
355 unload_zfeature(dsl_dataset_t
*ds
, spa_feature_t f
)
357 switch (spa_feature_table
[f
].fi_type
) {
358 case ZFEATURE_TYPE_BOOLEAN
:
360 case ZFEATURE_TYPE_UINT64_ARRAY
:
362 struct feature_type_uint64_array_arg
*ftuaa
= ds
->ds_feature
[f
];
363 kmem_free(ftuaa
->array
, ftuaa
->length
* sizeof (uint64_t));
364 kmem_free(ftuaa
, sizeof (*ftuaa
));
368 panic("Invalid zfeature type %d", spa_feature_table
[f
].fi_type
);
373 load_zfeature(objset_t
*mos
, dsl_dataset_t
*ds
, spa_feature_t f
)
376 switch (spa_feature_table
[f
].fi_type
) {
377 case ZFEATURE_TYPE_BOOLEAN
:
378 err
= zap_contains(mos
, ds
->ds_object
,
379 spa_feature_table
[f
].fi_guid
);
381 ds
->ds_feature
[f
] = (void *)B_TRUE
;
383 ASSERT3U(err
, ==, ENOENT
);
387 case ZFEATURE_TYPE_UINT64_ARRAY
:
389 uint64_t int_size
, num_int
;
391 err
= zap_length(mos
, ds
->ds_object
,
392 spa_feature_table
[f
].fi_guid
, &int_size
, &num_int
);
394 ASSERT3U(err
, ==, ENOENT
);
398 ASSERT3U(int_size
, ==, sizeof (uint64_t));
399 data
= kmem_alloc(int_size
* num_int
, KM_SLEEP
);
400 VERIFY0(zap_lookup(mos
, ds
->ds_object
,
401 spa_feature_table
[f
].fi_guid
, int_size
, num_int
, data
));
402 struct feature_type_uint64_array_arg
*ftuaa
=
403 kmem_alloc(sizeof (*ftuaa
), KM_SLEEP
);
404 ftuaa
->length
= num_int
;
406 ds
->ds_feature
[f
] = ftuaa
;
410 panic("Invalid zfeature type %d", spa_feature_table
[f
].fi_type
);
416 * We have to release the fsid synchronously or we risk that a subsequent
417 * mount of the same dataset will fail to unique_insert the fsid. This
418 * failure would manifest itself as the fsid of this dataset changing
419 * between mounts which makes NFS clients quite unhappy.
422 dsl_dataset_evict_sync(void *dbu
)
424 dsl_dataset_t
*ds
= dbu
;
426 ASSERT(ds
->ds_owner
== NULL
);
428 unique_remove(ds
->ds_fsid_guid
);
432 dsl_dataset_evict_async(void *dbu
)
434 dsl_dataset_t
*ds
= dbu
;
436 ASSERT(ds
->ds_owner
== NULL
);
440 if (ds
->ds_objset
!= NULL
)
441 dmu_objset_evict(ds
->ds_objset
);
444 dsl_dataset_rele(ds
->ds_prev
, ds
);
448 dsl_bookmark_fini_ds(ds
);
450 bplist_destroy(&ds
->ds_pending_deadlist
);
451 if (dsl_deadlist_is_open(&ds
->ds_deadlist
))
452 dsl_deadlist_close(&ds
->ds_deadlist
);
453 if (dsl_deadlist_is_open(&ds
->ds_remap_deadlist
))
454 dsl_deadlist_close(&ds
->ds_remap_deadlist
);
456 dsl_dir_async_rele(ds
->ds_dir
, ds
);
458 ASSERT(!list_link_active(&ds
->ds_synced_link
));
460 for (spa_feature_t f
= 0; f
< SPA_FEATURES
; f
++) {
461 if (dsl_dataset_feature_is_active(ds
, f
))
462 unload_zfeature(ds
, f
);
465 list_destroy(&ds
->ds_prop_cbs
);
466 mutex_destroy(&ds
->ds_lock
);
467 mutex_destroy(&ds
->ds_opening_lock
);
468 mutex_destroy(&ds
->ds_sendstream_lock
);
469 mutex_destroy(&ds
->ds_remap_deadlist_lock
);
470 zfs_refcount_destroy(&ds
->ds_longholds
);
471 rrw_destroy(&ds
->ds_bp_rwlock
);
473 kmem_free(ds
, sizeof (dsl_dataset_t
));
477 dsl_dataset_get_snapname(dsl_dataset_t
*ds
)
479 dsl_dataset_phys_t
*headphys
;
482 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
483 objset_t
*mos
= dp
->dp_meta_objset
;
485 if (ds
->ds_snapname
[0])
487 if (dsl_dataset_phys(ds
)->ds_next_snap_obj
== 0)
490 err
= dmu_bonus_hold(mos
, dsl_dir_phys(ds
->ds_dir
)->dd_head_dataset_obj
,
494 headphys
= headdbuf
->db_data
;
495 err
= zap_value_search(dp
->dp_meta_objset
,
496 headphys
->ds_snapnames_zapobj
, ds
->ds_object
, 0, ds
->ds_snapname
);
497 if (err
!= 0 && zfs_recover
== B_TRUE
) {
499 (void) snprintf(ds
->ds_snapname
, sizeof (ds
->ds_snapname
),
500 "SNAPOBJ=%llu-ERR=%d",
501 (unsigned long long)ds
->ds_object
, err
);
503 dmu_buf_rele(headdbuf
, FTAG
);
508 dsl_dataset_snap_lookup(dsl_dataset_t
*ds
, const char *name
, uint64_t *value
)
510 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
511 uint64_t snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
515 if (dsl_dataset_phys(ds
)->ds_flags
& DS_FLAG_CI_DATASET
)
518 err
= zap_lookup_norm(mos
, snapobj
, name
, 8, 1,
519 value
, mt
, NULL
, 0, NULL
);
520 if (err
== ENOTSUP
&& (mt
& MT_NORMALIZE
))
521 err
= zap_lookup(mos
, snapobj
, name
, 8, 1, value
);
526 dsl_dataset_snap_remove(dsl_dataset_t
*ds
, const char *name
, dmu_tx_t
*tx
,
529 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
530 uint64_t snapobj
= dsl_dataset_phys(ds
)->ds_snapnames_zapobj
;
534 dsl_dir_snap_cmtime_update(ds
->ds_dir
, tx
);
536 if (dsl_dataset_phys(ds
)->ds_flags
& DS_FLAG_CI_DATASET
)
539 err
= zap_remove_norm(mos
, snapobj
, name
, mt
, tx
);
540 if (err
== ENOTSUP
&& (mt
& MT_NORMALIZE
))
541 err
= zap_remove(mos
, snapobj
, name
, tx
);
543 if (err
== 0 && adj_cnt
)
544 dsl_fs_ss_count_adjust(ds
->ds_dir
, -1,
545 DD_FIELD_SNAPSHOT_COUNT
, tx
);
551 dsl_dataset_try_add_ref(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, const void *tag
)
553 dmu_buf_t
*dbuf
= ds
->ds_dbuf
;
554 boolean_t result
= B_FALSE
;
556 if (dbuf
!= NULL
&& dmu_buf_try_add_ref(dbuf
, dp
->dp_meta_objset
,
557 ds
->ds_object
, DMU_BONUS_BLKID
, tag
)) {
559 if (ds
== dmu_buf_get_user(dbuf
))
562 dmu_buf_rele(dbuf
, tag
);
569 dsl_dataset_hold_obj(dsl_pool_t
*dp
, uint64_t dsobj
, const void *tag
,
572 objset_t
*mos
= dp
->dp_meta_objset
;
576 dmu_object_info_t doi
;
578 ASSERT(dsl_pool_config_held(dp
));
580 err
= dmu_bonus_hold(mos
, dsobj
, tag
, &dbuf
);
584 /* Make sure dsobj has the correct object type. */
585 dmu_object_info_from_db(dbuf
, &doi
);
586 if (doi
.doi_bonus_type
!= DMU_OT_DSL_DATASET
) {
587 dmu_buf_rele(dbuf
, tag
);
588 return (SET_ERROR(EINVAL
));
591 ds
= dmu_buf_get_user(dbuf
);
593 dsl_dataset_t
*winner
= NULL
;
595 ds
= kmem_zalloc(sizeof (dsl_dataset_t
), KM_SLEEP
);
597 ds
->ds_object
= dsobj
;
598 ds
->ds_is_snapshot
= dsl_dataset_phys(ds
)->ds_num_children
!= 0;
599 list_link_init(&ds
->ds_synced_link
);
601 err
= dsl_dir_hold_obj(dp
, dsl_dataset_phys(ds
)->ds_dir_obj
,
602 NULL
, ds
, &ds
->ds_dir
);
604 kmem_free(ds
, sizeof (dsl_dataset_t
));
605 dmu_buf_rele(dbuf
, tag
);
609 mutex_init(&ds
->ds_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
610 mutex_init(&ds
->ds_opening_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
611 mutex_init(&ds
->ds_sendstream_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
612 mutex_init(&ds
->ds_remap_deadlist_lock
,
613 NULL
, MUTEX_DEFAULT
, NULL
);
614 rrw_init(&ds
->ds_bp_rwlock
, B_FALSE
);
615 zfs_refcount_create(&ds
->ds_longholds
);
617 bplist_create(&ds
->ds_pending_deadlist
);
619 list_create(&ds
->ds_sendstreams
, sizeof (dmu_sendstatus_t
),
620 offsetof(dmu_sendstatus_t
, dss_link
));
622 list_create(&ds
->ds_prop_cbs
, sizeof (dsl_prop_cb_record_t
),
623 offsetof(dsl_prop_cb_record_t
, cbr_ds_node
));
625 if (doi
.doi_type
== DMU_OTN_ZAP_METADATA
) {
628 for (f
= 0; f
< SPA_FEATURES
; f
++) {
629 if (!(spa_feature_table
[f
].fi_flags
&
630 ZFEATURE_FLAG_PER_DATASET
))
632 err
= load_zfeature(mos
, ds
, f
);
636 if (!ds
->ds_is_snapshot
) {
637 ds
->ds_snapname
[0] = '\0';
638 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
639 err
= dsl_dataset_hold_obj(dp
,
640 dsl_dataset_phys(ds
)->ds_prev_snap_obj
,
644 goto after_dsl_bookmark_fini
;
645 err
= dsl_bookmark_init_ds(ds
);
647 if (zfs_flags
& ZFS_DEBUG_SNAPNAMES
)
648 err
= dsl_dataset_get_snapname(ds
);
650 dsl_dataset_phys(ds
)->ds_userrefs_obj
!= 0) {
652 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
653 dsl_dataset_phys(ds
)->ds_userrefs_obj
,
658 if (err
== 0 && !ds
->ds_is_snapshot
) {
659 err
= dsl_prop_get_int_ds(ds
,
660 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
663 err
= dsl_prop_get_int_ds(ds
,
664 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
668 ds
->ds_reserved
= ds
->ds_quota
= 0;
671 if (err
== 0 && ds
->ds_dir
->dd_crypto_obj
!= 0 &&
672 ds
->ds_is_snapshot
&&
673 zap_contains(mos
, dsobj
, DS_FIELD_IVSET_GUID
) != 0) {
674 dp
->dp_spa
->spa_errata
=
675 ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
;
678 dsl_deadlist_open(&ds
->ds_deadlist
,
679 mos
, dsl_dataset_phys(ds
)->ds_deadlist_obj
);
680 uint64_t remap_deadlist_obj
=
681 dsl_dataset_get_remap_deadlist_object(ds
);
682 if (remap_deadlist_obj
!= 0) {
683 dsl_deadlist_open(&ds
->ds_remap_deadlist
, mos
,
687 dmu_buf_init_user(&ds
->ds_dbu
, dsl_dataset_evict_sync
,
688 dsl_dataset_evict_async
, &ds
->ds_dbuf
);
690 winner
= dmu_buf_set_user_ie(dbuf
, &ds
->ds_dbu
);
692 if (err
!= 0 || winner
!= NULL
) {
693 dsl_deadlist_close(&ds
->ds_deadlist
);
694 if (dsl_deadlist_is_open(&ds
->ds_remap_deadlist
))
695 dsl_deadlist_close(&ds
->ds_remap_deadlist
);
696 dsl_bookmark_fini_ds(ds
);
697 after_dsl_bookmark_fini
:
699 dsl_dataset_rele(ds
->ds_prev
, ds
);
700 dsl_dir_rele(ds
->ds_dir
, ds
);
701 for (spa_feature_t f
= 0; f
< SPA_FEATURES
; f
++) {
702 if (dsl_dataset_feature_is_active(ds
, f
))
703 unload_zfeature(ds
, f
);
706 list_destroy(&ds
->ds_prop_cbs
);
707 list_destroy(&ds
->ds_sendstreams
);
708 bplist_destroy(&ds
->ds_pending_deadlist
);
709 mutex_destroy(&ds
->ds_lock
);
710 mutex_destroy(&ds
->ds_opening_lock
);
711 mutex_destroy(&ds
->ds_sendstream_lock
);
712 mutex_destroy(&ds
->ds_remap_deadlist_lock
);
713 zfs_refcount_destroy(&ds
->ds_longholds
);
714 rrw_destroy(&ds
->ds_bp_rwlock
);
715 kmem_free(ds
, sizeof (dsl_dataset_t
));
717 dmu_buf_rele(dbuf
, tag
);
723 unique_insert(dsl_dataset_phys(ds
)->ds_fsid_guid
);
724 if (ds
->ds_fsid_guid
!=
725 dsl_dataset_phys(ds
)->ds_fsid_guid
) {
726 zfs_dbgmsg("ds_fsid_guid changed from "
727 "%llx to %llx for pool %s dataset id %llu",
729 dsl_dataset_phys(ds
)->ds_fsid_guid
,
730 (long long)ds
->ds_fsid_guid
,
731 spa_name(dp
->dp_spa
),
732 (u_longlong_t
)dsobj
);
737 ASSERT3P(ds
->ds_dbuf
, ==, dbuf
);
738 ASSERT3P(dsl_dataset_phys(ds
), ==, dbuf
->db_data
);
739 ASSERT(dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0 ||
740 spa_version(dp
->dp_spa
) < SPA_VERSION_ORIGIN
||
741 dp
->dp_origin_snap
== NULL
|| ds
== dp
->dp_origin_snap
);
748 dsl_dataset_create_key_mapping(dsl_dataset_t
*ds
)
750 dsl_dir_t
*dd
= ds
->ds_dir
;
752 if (dd
->dd_crypto_obj
== 0)
755 return (spa_keystore_create_mapping(dd
->dd_pool
->dp_spa
,
756 ds
, ds
, &ds
->ds_key_mapping
));
760 dsl_dataset_hold_obj_flags(dsl_pool_t
*dp
, uint64_t dsobj
,
761 ds_hold_flags_t flags
, const void *tag
, dsl_dataset_t
**dsp
)
765 err
= dsl_dataset_hold_obj(dp
, dsobj
, tag
, dsp
);
769 ASSERT3P(*dsp
, !=, NULL
);
771 if (flags
& DS_HOLD_FLAG_DECRYPT
) {
772 err
= dsl_dataset_create_key_mapping(*dsp
);
774 dsl_dataset_rele(*dsp
, tag
);
781 dsl_dataset_hold_flags(dsl_pool_t
*dp
, const char *name
, ds_hold_flags_t flags
,
782 const void *tag
, dsl_dataset_t
**dsp
)
785 const char *snapname
;
790 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, &snapname
);
794 ASSERT(dsl_pool_config_held(dp
));
795 obj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
;
797 err
= dsl_dataset_hold_obj_flags(dp
, obj
, flags
, tag
, &ds
);
799 err
= SET_ERROR(ENOENT
);
801 /* we may be looking for a snapshot */
802 if (err
== 0 && snapname
!= NULL
) {
803 dsl_dataset_t
*snap_ds
;
805 if (*snapname
++ != '@') {
806 dsl_dataset_rele_flags(ds
, flags
, tag
);
807 dsl_dir_rele(dd
, FTAG
);
808 return (SET_ERROR(ENOENT
));
811 dprintf("looking for snapshot '%s'\n", snapname
);
812 err
= dsl_dataset_snap_lookup(ds
, snapname
, &obj
);
814 err
= dsl_dataset_hold_obj_flags(dp
, obj
, flags
, tag
,
817 dsl_dataset_rele_flags(ds
, flags
, tag
);
820 mutex_enter(&snap_ds
->ds_lock
);
821 if (snap_ds
->ds_snapname
[0] == 0)
822 (void) strlcpy(snap_ds
->ds_snapname
, snapname
,
823 sizeof (snap_ds
->ds_snapname
));
824 mutex_exit(&snap_ds
->ds_lock
);
830 dsl_dir_rele(dd
, FTAG
);
835 dsl_dataset_hold(dsl_pool_t
*dp
, const char *name
, const void *tag
,
838 return (dsl_dataset_hold_flags(dp
, name
, 0, tag
, dsp
));
842 dsl_dataset_own_obj_impl(dsl_pool_t
*dp
, uint64_t dsobj
, ds_hold_flags_t flags
,
843 const void *tag
, boolean_t override
, dsl_dataset_t
**dsp
)
845 int err
= dsl_dataset_hold_obj_flags(dp
, dsobj
, flags
, tag
, dsp
);
848 if (!dsl_dataset_tryown(*dsp
, tag
, override
)) {
849 dsl_dataset_rele_flags(*dsp
, flags
, tag
);
851 return (SET_ERROR(EBUSY
));
858 dsl_dataset_own_obj(dsl_pool_t
*dp
, uint64_t dsobj
, ds_hold_flags_t flags
,
859 const void *tag
, dsl_dataset_t
**dsp
)
861 return (dsl_dataset_own_obj_impl(dp
, dsobj
, flags
, tag
, B_FALSE
, dsp
));
865 dsl_dataset_own_obj_force(dsl_pool_t
*dp
, uint64_t dsobj
,
866 ds_hold_flags_t flags
, const void *tag
, dsl_dataset_t
**dsp
)
868 return (dsl_dataset_own_obj_impl(dp
, dsobj
, flags
, tag
, B_TRUE
, dsp
));
872 dsl_dataset_own_impl(dsl_pool_t
*dp
, const char *name
, ds_hold_flags_t flags
,
873 const void *tag
, boolean_t override
, dsl_dataset_t
**dsp
)
875 int err
= dsl_dataset_hold_flags(dp
, name
, flags
, tag
, dsp
);
878 if (!dsl_dataset_tryown(*dsp
, tag
, override
)) {
879 dsl_dataset_rele_flags(*dsp
, flags
, tag
);
880 return (SET_ERROR(EBUSY
));
886 dsl_dataset_own_force(dsl_pool_t
*dp
, const char *name
, ds_hold_flags_t flags
,
887 const void *tag
, dsl_dataset_t
**dsp
)
889 return (dsl_dataset_own_impl(dp
, name
, flags
, tag
, B_TRUE
, dsp
));
893 dsl_dataset_own(dsl_pool_t
*dp
, const char *name
, ds_hold_flags_t flags
,
894 const void *tag
, dsl_dataset_t
**dsp
)
896 return (dsl_dataset_own_impl(dp
, name
, flags
, tag
, B_FALSE
, dsp
));
900 * See the comment above dsl_pool_hold() for details. In summary, a long
901 * hold is used to prevent destruction of a dataset while the pool hold
902 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
904 * The dataset and pool must be held when this function is called. After it
905 * is called, the pool hold may be released while the dataset is still held
909 dsl_dataset_long_hold(dsl_dataset_t
*ds
, const void *tag
)
911 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
912 (void) zfs_refcount_add(&ds
->ds_longholds
, tag
);
916 dsl_dataset_long_rele(dsl_dataset_t
*ds
, const void *tag
)
918 (void) zfs_refcount_remove(&ds
->ds_longholds
, tag
);
921 /* Return B_TRUE if there are any long holds on this dataset. */
923 dsl_dataset_long_held(dsl_dataset_t
*ds
)
925 return (!zfs_refcount_is_zero(&ds
->ds_longholds
));
929 dsl_dataset_name(dsl_dataset_t
*ds
, char *name
)
932 (void) strlcpy(name
, "mos", ZFS_MAX_DATASET_NAME_LEN
);
934 dsl_dir_name(ds
->ds_dir
, name
);
935 VERIFY0(dsl_dataset_get_snapname(ds
));
936 if (ds
->ds_snapname
[0]) {
937 VERIFY3U(strlcat(name
, "@", ZFS_MAX_DATASET_NAME_LEN
),
938 <, ZFS_MAX_DATASET_NAME_LEN
);
940 * We use a "recursive" mutex so that we
941 * can call dprintf_ds() with ds_lock held.
943 if (!MUTEX_HELD(&ds
->ds_lock
)) {
944 mutex_enter(&ds
->ds_lock
);
945 VERIFY3U(strlcat(name
, ds
->ds_snapname
,
946 ZFS_MAX_DATASET_NAME_LEN
), <,
947 ZFS_MAX_DATASET_NAME_LEN
);
948 mutex_exit(&ds
->ds_lock
);
950 VERIFY3U(strlcat(name
, ds
->ds_snapname
,
951 ZFS_MAX_DATASET_NAME_LEN
), <,
952 ZFS_MAX_DATASET_NAME_LEN
);
959 dsl_dataset_namelen(dsl_dataset_t
*ds
)
961 VERIFY0(dsl_dataset_get_snapname(ds
));
962 mutex_enter(&ds
->ds_lock
);
963 int len
= strlen(ds
->ds_snapname
);
964 mutex_exit(&ds
->ds_lock
);
965 /* add '@' if ds is a snap */
968 len
+= dsl_dir_namelen(ds
->ds_dir
);
973 dsl_dataset_rele(dsl_dataset_t
*ds
, const void *tag
)
975 dmu_buf_rele(ds
->ds_dbuf
, tag
);
979 dsl_dataset_remove_key_mapping(dsl_dataset_t
*ds
)
981 dsl_dir_t
*dd
= ds
->ds_dir
;
983 if (dd
== NULL
|| dd
->dd_crypto_obj
== 0)
986 (void) spa_keystore_remove_mapping(dd
->dd_pool
->dp_spa
,
991 dsl_dataset_rele_flags(dsl_dataset_t
*ds
, ds_hold_flags_t flags
,
994 if (flags
& DS_HOLD_FLAG_DECRYPT
)
995 dsl_dataset_remove_key_mapping(ds
);
997 dsl_dataset_rele(ds
, tag
);
1001 dsl_dataset_disown(dsl_dataset_t
*ds
, ds_hold_flags_t flags
, const void *tag
)
1003 ASSERT3P(ds
->ds_owner
, ==, tag
);
1004 ASSERT(ds
->ds_dbuf
!= NULL
);
1006 mutex_enter(&ds
->ds_lock
);
1007 ds
->ds_owner
= NULL
;
1008 mutex_exit(&ds
->ds_lock
);
1009 dsl_dataset_long_rele(ds
, tag
);
1010 dsl_dataset_rele_flags(ds
, flags
, tag
);
1014 dsl_dataset_tryown(dsl_dataset_t
*ds
, const void *tag
, boolean_t override
)
1016 boolean_t gotit
= FALSE
;
1018 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1019 mutex_enter(&ds
->ds_lock
);
1020 if (ds
->ds_owner
== NULL
&& (override
|| !(DS_IS_INCONSISTENT(ds
) ||
1021 (dsl_dataset_feature_is_active(ds
,
1022 SPA_FEATURE_REDACTED_DATASETS
) &&
1023 !zfs_allow_redacted_dataset_mount
)))) {
1025 dsl_dataset_long_hold(ds
, tag
);
1028 mutex_exit(&ds
->ds_lock
);
1033 dsl_dataset_has_owner(dsl_dataset_t
*ds
)
1036 mutex_enter(&ds
->ds_lock
);
1037 rv
= (ds
->ds_owner
!= NULL
);
1038 mutex_exit(&ds
->ds_lock
);
1043 zfeature_active(spa_feature_t f
, void *arg
)
1045 switch (spa_feature_table
[f
].fi_type
) {
1046 case ZFEATURE_TYPE_BOOLEAN
: {
1047 boolean_t val
= (boolean_t
)(uintptr_t)arg
;
1048 ASSERT(val
== B_FALSE
|| val
== B_TRUE
);
1051 case ZFEATURE_TYPE_UINT64_ARRAY
:
1053 * In this case, arg is a uint64_t array. The feature is active
1054 * if the array is non-null.
1056 return (arg
!= NULL
);
1058 panic("Invalid zfeature type %d", spa_feature_table
[f
].fi_type
);
1064 dsl_dataset_feature_is_active(dsl_dataset_t
*ds
, spa_feature_t f
)
1066 return (zfeature_active(f
, ds
->ds_feature
[f
]));
1070 * The buffers passed out by this function are references to internal buffers;
1071 * they should not be freed by callers of this function, and they should not be
1072 * used after the dataset has been released.
1075 dsl_dataset_get_uint64_array_feature(dsl_dataset_t
*ds
, spa_feature_t f
,
1076 uint64_t *outlength
, uint64_t **outp
)
1078 VERIFY(spa_feature_table
[f
].fi_type
& ZFEATURE_TYPE_UINT64_ARRAY
);
1079 if (!dsl_dataset_feature_is_active(ds
, f
)) {
1082 struct feature_type_uint64_array_arg
*ftuaa
= ds
->ds_feature
[f
];
1083 *outp
= ftuaa
->array
;
1084 *outlength
= ftuaa
->length
;
1089 dsl_dataset_activate_feature(uint64_t dsobj
, spa_feature_t f
, void *arg
,
1092 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
1093 objset_t
*mos
= dmu_tx_pool(tx
)->dp_meta_objset
;
1096 VERIFY(spa_feature_table
[f
].fi_flags
& ZFEATURE_FLAG_PER_DATASET
);
1098 spa_feature_incr(spa
, f
, tx
);
1099 dmu_object_zapify(mos
, dsobj
, DMU_OT_DSL_DATASET
, tx
);
1101 switch (spa_feature_table
[f
].fi_type
) {
1102 case ZFEATURE_TYPE_BOOLEAN
:
1103 ASSERT3S((boolean_t
)(uintptr_t)arg
, ==, B_TRUE
);
1104 VERIFY0(zap_add(mos
, dsobj
, spa_feature_table
[f
].fi_guid
,
1105 sizeof (zero
), 1, &zero
, tx
));
1107 case ZFEATURE_TYPE_UINT64_ARRAY
:
1109 struct feature_type_uint64_array_arg
*ftuaa
= arg
;
1110 VERIFY0(zap_add(mos
, dsobj
, spa_feature_table
[f
].fi_guid
,
1111 sizeof (uint64_t), ftuaa
->length
, ftuaa
->array
, tx
));
1115 panic("Invalid zfeature type %d", spa_feature_table
[f
].fi_type
);
1120 dsl_dataset_deactivate_feature_impl(dsl_dataset_t
*ds
, spa_feature_t f
,
1123 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
1124 objset_t
*mos
= dmu_tx_pool(tx
)->dp_meta_objset
;
1125 uint64_t dsobj
= ds
->ds_object
;
1127 VERIFY(spa_feature_table
[f
].fi_flags
& ZFEATURE_FLAG_PER_DATASET
);
1129 VERIFY0(zap_remove(mos
, dsobj
, spa_feature_table
[f
].fi_guid
, tx
));
1130 spa_feature_decr(spa
, f
, tx
);
1131 ds
->ds_feature
[f
] = NULL
;
1135 dsl_dataset_deactivate_feature(dsl_dataset_t
*ds
, spa_feature_t f
, dmu_tx_t
*tx
)
1137 unload_zfeature(ds
, f
);
1138 dsl_dataset_deactivate_feature_impl(ds
, f
, tx
);
1142 dsl_dataset_create_sync_dd(dsl_dir_t
*dd
, dsl_dataset_t
*origin
,
1143 dsl_crypto_params_t
*dcp
, uint64_t flags
, dmu_tx_t
*tx
)
1145 dsl_pool_t
*dp
= dd
->dd_pool
;
1147 dsl_dataset_phys_t
*dsphys
;
1149 objset_t
*mos
= dp
->dp_meta_objset
;
1152 origin
= dp
->dp_origin_snap
;
1154 ASSERT(origin
== NULL
|| origin
->ds_dir
->dd_pool
== dp
);
1155 ASSERT(origin
== NULL
|| dsl_dataset_phys(origin
)->ds_num_children
> 0);
1156 ASSERT(dmu_tx_is_syncing(tx
));
1157 ASSERT(dsl_dir_phys(dd
)->dd_head_dataset_obj
== 0);
1159 dsobj
= dmu_object_alloc(mos
, DMU_OT_DSL_DATASET
, 0,
1160 DMU_OT_DSL_DATASET
, sizeof (dsl_dataset_phys_t
), tx
);
1161 VERIFY0(dmu_bonus_hold(mos
, dsobj
, FTAG
, &dbuf
));
1162 dmu_buf_will_dirty(dbuf
, tx
);
1163 dsphys
= dbuf
->db_data
;
1164 memset(dsphys
, 0, sizeof (dsl_dataset_phys_t
));
1165 dsphys
->ds_dir_obj
= dd
->dd_object
;
1166 dsphys
->ds_flags
= flags
;
1167 dsphys
->ds_fsid_guid
= unique_create();
1168 (void) random_get_pseudo_bytes((void*)&dsphys
->ds_guid
,
1169 sizeof (dsphys
->ds_guid
));
1170 dsphys
->ds_snapnames_zapobj
=
1171 zap_create_norm(mos
, U8_TEXTPREP_TOUPPER
, DMU_OT_DSL_DS_SNAP_MAP
,
1172 DMU_OT_NONE
, 0, tx
);
1173 dsphys
->ds_creation_time
= gethrestime_sec();
1174 dsphys
->ds_creation_txg
= tx
->tx_txg
== TXG_INITIAL
? 1 : tx
->tx_txg
;
1176 if (origin
== NULL
) {
1177 dsphys
->ds_deadlist_obj
= dsl_deadlist_alloc(mos
, tx
);
1179 dsl_dataset_t
*ohds
; /* head of the origin snapshot */
1181 dsphys
->ds_prev_snap_obj
= origin
->ds_object
;
1182 dsphys
->ds_prev_snap_txg
=
1183 dsl_dataset_phys(origin
)->ds_creation_txg
;
1184 dsphys
->ds_referenced_bytes
=
1185 dsl_dataset_phys(origin
)->ds_referenced_bytes
;
1186 dsphys
->ds_compressed_bytes
=
1187 dsl_dataset_phys(origin
)->ds_compressed_bytes
;
1188 dsphys
->ds_uncompressed_bytes
=
1189 dsl_dataset_phys(origin
)->ds_uncompressed_bytes
;
1190 rrw_enter(&origin
->ds_bp_rwlock
, RW_READER
, FTAG
);
1191 dsphys
->ds_bp
= dsl_dataset_phys(origin
)->ds_bp
;
1192 rrw_exit(&origin
->ds_bp_rwlock
, FTAG
);
1195 * Inherit flags that describe the dataset's contents
1196 * (INCONSISTENT) or properties (Case Insensitive).
1198 dsphys
->ds_flags
|= dsl_dataset_phys(origin
)->ds_flags
&
1199 (DS_FLAG_INCONSISTENT
| DS_FLAG_CI_DATASET
);
1201 for (spa_feature_t f
= 0; f
< SPA_FEATURES
; f
++) {
1202 if (zfeature_active(f
, origin
->ds_feature
[f
])) {
1203 dsl_dataset_activate_feature(dsobj
, f
,
1204 origin
->ds_feature
[f
], tx
);
1208 dmu_buf_will_dirty(origin
->ds_dbuf
, tx
);
1209 dsl_dataset_phys(origin
)->ds_num_children
++;
1211 VERIFY0(dsl_dataset_hold_obj(dp
,
1212 dsl_dir_phys(origin
->ds_dir
)->dd_head_dataset_obj
,
1214 dsphys
->ds_deadlist_obj
= dsl_deadlist_clone(&ohds
->ds_deadlist
,
1215 dsphys
->ds_prev_snap_txg
, dsphys
->ds_prev_snap_obj
, tx
);
1216 dsl_dataset_rele(ohds
, FTAG
);
1218 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_NEXT_CLONES
) {
1219 if (dsl_dataset_phys(origin
)->ds_next_clones_obj
== 0) {
1220 dsl_dataset_phys(origin
)->ds_next_clones_obj
=
1222 DMU_OT_NEXT_CLONES
, DMU_OT_NONE
, 0, tx
);
1224 VERIFY0(zap_add_int(mos
,
1225 dsl_dataset_phys(origin
)->ds_next_clones_obj
,
1229 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1230 dsl_dir_phys(dd
)->dd_origin_obj
= origin
->ds_object
;
1231 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
1232 if (dsl_dir_phys(origin
->ds_dir
)->dd_clones
== 0) {
1233 dmu_buf_will_dirty(origin
->ds_dir
->dd_dbuf
, tx
);
1234 dsl_dir_phys(origin
->ds_dir
)->dd_clones
=
1236 DMU_OT_DSL_CLONES
, DMU_OT_NONE
, 0, tx
);
1238 VERIFY0(zap_add_int(mos
,
1239 dsl_dir_phys(origin
->ds_dir
)->dd_clones
,
1244 /* handle encryption */
1245 dsl_dataset_create_crypt_sync(dsobj
, dd
, origin
, dcp
, tx
);
1247 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_UNIQUE_ACCURATE
)
1248 dsphys
->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
1250 dmu_buf_rele(dbuf
, FTAG
);
1252 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1253 dsl_dir_phys(dd
)->dd_head_dataset_obj
= dsobj
;
1259 dsl_dataset_zero_zil(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
1263 VERIFY0(dmu_objset_from_ds(ds
, &os
));
1264 if (memcmp(&os
->os_zil_header
, &zero_zil
, sizeof (zero_zil
)) != 0) {
1265 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1268 memset(&os
->os_zil_header
, 0, sizeof (os
->os_zil_header
));
1269 if (os
->os_encrypted
)
1270 os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
] = B_TRUE
;
1272 zio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
1273 dsl_dataset_sync(ds
, zio
, tx
);
1274 VERIFY0(zio_wait(zio
));
1276 /* dsl_dataset_sync_done will drop this reference. */
1277 dmu_buf_add_ref(ds
->ds_dbuf
, ds
);
1278 dsl_dataset_sync_done(ds
, tx
);
1283 dsl_dataset_create_sync(dsl_dir_t
*pdd
, const char *lastname
,
1284 dsl_dataset_t
*origin
, uint64_t flags
, cred_t
*cr
,
1285 dsl_crypto_params_t
*dcp
, dmu_tx_t
*tx
)
1287 dsl_pool_t
*dp
= pdd
->dd_pool
;
1288 uint64_t dsobj
, ddobj
;
1291 ASSERT(dmu_tx_is_syncing(tx
));
1292 ASSERT(lastname
[0] != '@');
1294 * Filesystems will eventually have their origin set to dp_origin_snap,
1295 * but that's taken care of in dsl_dataset_create_sync_dd. When
1296 * creating a filesystem, this function is called with origin equal to
1300 ASSERT3P(origin
, !=, dp
->dp_origin_snap
);
1302 ddobj
= dsl_dir_create_sync(dp
, pdd
, lastname
, tx
);
1303 VERIFY0(dsl_dir_hold_obj(dp
, ddobj
, lastname
, FTAG
, &dd
));
1305 dsobj
= dsl_dataset_create_sync_dd(dd
, origin
, dcp
,
1306 flags
& ~DS_CREATE_FLAG_NODIRTY
, tx
);
1308 dsl_deleg_set_create_perms(dd
, tx
, cr
);
1311 * If we are creating a clone and the livelist feature is enabled,
1312 * add the entry DD_FIELD_LIVELIST to ZAP.
1314 if (origin
!= NULL
&&
1315 spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LIVELIST
)) {
1316 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
1317 dsl_dir_zapify(dd
, tx
);
1318 uint64_t obj
= dsl_deadlist_alloc(mos
, tx
);
1319 VERIFY0(zap_add(mos
, dd
->dd_object
, DD_FIELD_LIVELIST
,
1320 sizeof (uint64_t), 1, &obj
, tx
));
1321 spa_feature_incr(dp
->dp_spa
, SPA_FEATURE_LIVELIST
, tx
);
1325 * Since we're creating a new node we know it's a leaf, so we can
1326 * initialize the counts if the limit feature is active.
1328 if (spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_FS_SS_LIMIT
)) {
1330 objset_t
*os
= dd
->dd_pool
->dp_meta_objset
;
1332 dsl_dir_zapify(dd
, tx
);
1333 VERIFY0(zap_add(os
, dd
->dd_object
, DD_FIELD_FILESYSTEM_COUNT
,
1334 sizeof (cnt
), 1, &cnt
, tx
));
1335 VERIFY0(zap_add(os
, dd
->dd_object
, DD_FIELD_SNAPSHOT_COUNT
,
1336 sizeof (cnt
), 1, &cnt
, tx
));
1339 dsl_dir_rele(dd
, FTAG
);
1342 * If we are creating a clone, make sure we zero out any stale
1343 * data from the origin snapshots zil header.
1345 if (origin
!= NULL
&& !(flags
& DS_CREATE_FLAG_NODIRTY
)) {
1348 VERIFY0(dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
1349 dsl_dataset_zero_zil(ds
, tx
);
1350 dsl_dataset_rele(ds
, FTAG
);
1357 * The unique space in the head dataset can be calculated by subtracting
1358 * the space used in the most recent snapshot, that is still being used
1359 * in this file system, from the space currently in use. To figure out
1360 * the space in the most recent snapshot still in use, we need to take
1361 * the total space used in the snapshot and subtract out the space that
1362 * has been freed up since the snapshot was taken.
1365 dsl_dataset_recalc_head_uniq(dsl_dataset_t
*ds
)
1368 uint64_t dlused
, dlcomp
, dluncomp
;
1370 ASSERT(!ds
->ds_is_snapshot
);
1372 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0)
1373 mrs_used
= dsl_dataset_phys(ds
->ds_prev
)->ds_referenced_bytes
;
1377 dsl_deadlist_space(&ds
->ds_deadlist
, &dlused
, &dlcomp
, &dluncomp
);
1379 ASSERT3U(dlused
, <=, mrs_used
);
1380 dsl_dataset_phys(ds
)->ds_unique_bytes
=
1381 dsl_dataset_phys(ds
)->ds_referenced_bytes
- (mrs_used
- dlused
);
1383 if (spa_version(ds
->ds_dir
->dd_pool
->dp_spa
) >=
1384 SPA_VERSION_UNIQUE_ACCURATE
)
1385 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
1389 dsl_dataset_remove_from_next_clones(dsl_dataset_t
*ds
, uint64_t obj
,
1392 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
1393 uint64_t count __maybe_unused
;
1396 ASSERT(dsl_dataset_phys(ds
)->ds_num_children
>= 2);
1397 err
= zap_remove_int(mos
, dsl_dataset_phys(ds
)->ds_next_clones_obj
,
1400 * The err should not be ENOENT, but a bug in a previous version
1401 * of the code could cause upgrade_clones_cb() to not set
1402 * ds_next_snap_obj when it should, leading to a missing entry.
1403 * If we knew that the pool was created after
1404 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
1405 * ENOENT. However, at least we can check that we don't have
1406 * too many entries in the next_clones_obj even after failing to
1411 ASSERT0(zap_count(mos
, dsl_dataset_phys(ds
)->ds_next_clones_obj
,
1413 ASSERT3U(count
, <=, dsl_dataset_phys(ds
)->ds_num_children
- 2);
1418 dsl_dataset_get_blkptr(dsl_dataset_t
*ds
)
1420 return (&dsl_dataset_phys(ds
)->ds_bp
);
1424 dsl_dataset_get_spa(dsl_dataset_t
*ds
)
1426 return (ds
->ds_dir
->dd_pool
->dp_spa
);
1430 dsl_dataset_dirty(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
1434 if (ds
== NULL
) /* this is the meta-objset */
1437 ASSERT(ds
->ds_objset
!= NULL
);
1439 if (dsl_dataset_phys(ds
)->ds_next_snap_obj
!= 0)
1440 panic("dirtying snapshot!");
1442 /* Must not dirty a dataset in the same txg where it got snapshotted. */
1443 ASSERT3U(tx
->tx_txg
, >, dsl_dataset_phys(ds
)->ds_prev_snap_txg
);
1445 dp
= ds
->ds_dir
->dd_pool
;
1446 if (txg_list_add(&dp
->dp_dirty_datasets
, ds
, tx
->tx_txg
)) {
1447 objset_t
*os
= ds
->ds_objset
;
1449 /* up the hold count until we can be written out */
1450 dmu_buf_add_ref(ds
->ds_dbuf
, ds
);
1452 /* if this dataset is encrypted, grab a reference to the DCK */
1453 if (ds
->ds_dir
->dd_crypto_obj
!= 0 &&
1454 !os
->os_raw_receive
&&
1455 !os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
]) {
1456 ASSERT3P(ds
->ds_key_mapping
, !=, NULL
);
1457 key_mapping_add_ref(ds
->ds_key_mapping
, ds
);
1463 dsl_dataset_snapshot_reserve_space(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
1467 if (!dmu_tx_is_syncing(tx
))
1471 * If there's an fs-only reservation, any blocks that might become
1472 * owned by the snapshot dataset must be accommodated by space
1473 * outside of the reservation.
1475 ASSERT(ds
->ds_reserved
== 0 || DS_UNIQUE_IS_ACCURATE(ds
));
1476 asize
= MIN(dsl_dataset_phys(ds
)->ds_unique_bytes
, ds
->ds_reserved
);
1477 if (asize
> dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
))
1478 return (SET_ERROR(ENOSPC
));
1481 * Propagate any reserved space for this snapshot to other
1482 * snapshot checks in this sync group.
1485 dsl_dir_willuse_space(ds
->ds_dir
, asize
, tx
);
1491 dsl_dataset_snapshot_check_impl(dsl_dataset_t
*ds
, const char *snapname
,
1492 dmu_tx_t
*tx
, boolean_t recv
, uint64_t cnt
, cred_t
*cr
, proc_t
*proc
)
1497 ds
->ds_trysnap_txg
= tx
->tx_txg
;
1499 if (!dmu_tx_is_syncing(tx
))
1503 * We don't allow multiple snapshots of the same txg. If there
1504 * is already one, try again.
1506 if (dsl_dataset_phys(ds
)->ds_prev_snap_txg
>= tx
->tx_txg
)
1507 return (SET_ERROR(EAGAIN
));
1510 * Check for conflicting snapshot name.
1512 error
= dsl_dataset_snap_lookup(ds
, snapname
, &value
);
1514 return (SET_ERROR(EEXIST
));
1515 if (error
!= ENOENT
)
1519 * We don't allow taking snapshots of inconsistent datasets, such as
1520 * those into which we are currently receiving. However, if we are
1521 * creating this snapshot as part of a receive, this check will be
1522 * executed atomically with respect to the completion of the receive
1523 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1524 * case we ignore this, knowing it will be fixed up for us shortly in
1525 * dmu_recv_end_sync().
1527 if (!recv
&& DS_IS_INCONSISTENT(ds
))
1528 return (SET_ERROR(EBUSY
));
1531 * Skip the check for temporary snapshots or if we have already checked
1532 * the counts in dsl_dataset_snapshot_check. This means we really only
1533 * check the count here when we're receiving a stream.
1535 if (cnt
!= 0 && cr
!= NULL
) {
1536 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, cnt
,
1537 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
, cr
, proc
);
1542 error
= dsl_dataset_snapshot_reserve_space(ds
, tx
);
1550 dsl_dataset_snapshot_check(void *arg
, dmu_tx_t
*tx
)
1552 dsl_dataset_snapshot_arg_t
*ddsa
= arg
;
1553 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1558 * Pre-compute how many total new snapshots will be created for each
1559 * level in the tree and below. This is needed for validating the
1560 * snapshot limit when either taking a recursive snapshot or when
1561 * taking multiple snapshots.
1563 * The problem is that the counts are not actually adjusted when
1564 * we are checking, only when we finally sync. For a single snapshot,
1565 * this is easy, the count will increase by 1 at each node up the tree,
1566 * but its more complicated for the recursive/multiple snapshot case.
1568 * The dsl_fs_ss_limit_check function does recursively check the count
1569 * at each level up the tree but since it is validating each snapshot
1570 * independently we need to be sure that we are validating the complete
1571 * count for the entire set of snapshots. We do this by rolling up the
1572 * counts for each component of the name into an nvlist and then
1573 * checking each of those cases with the aggregated count.
1575 * This approach properly handles not only the recursive snapshot
1576 * case (where we get all of those on the ddsa_snaps list) but also
1577 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1578 * validate the limit on 'a' using a count of 2).
1580 * We validate the snapshot names in the third loop and only report
1583 if (dmu_tx_is_syncing(tx
)) {
1585 nvlist_t
*cnt_track
= NULL
;
1586 cnt_track
= fnvlist_alloc();
1588 nm
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1590 /* Rollup aggregated counts into the cnt_track list */
1591 for (pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, NULL
);
1593 pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, pair
)) {
1597 (void) strlcpy(nm
, nvpair_name(pair
), MAXPATHLEN
);
1598 pdelim
= strchr(nm
, '@');
1604 if (nvlist_lookup_uint64(cnt_track
, nm
,
1606 /* update existing entry */
1607 fnvlist_add_uint64(cnt_track
, nm
,
1611 fnvlist_add_uint64(cnt_track
, nm
, 1);
1614 pdelim
= strrchr(nm
, '/');
1617 } while (pdelim
!= NULL
);
1620 kmem_free(nm
, MAXPATHLEN
);
1622 /* Check aggregated counts at each level */
1623 for (pair
= nvlist_next_nvpair(cnt_track
, NULL
);
1624 pair
!= NULL
; pair
= nvlist_next_nvpair(cnt_track
, pair
)) {
1630 name
= nvpair_name(pair
);
1631 cnt
= fnvpair_value_uint64(pair
);
1634 error
= dsl_dataset_hold(dp
, name
, FTAG
, &ds
);
1636 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, cnt
,
1637 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
,
1638 ddsa
->ddsa_cr
, ddsa
->ddsa_proc
);
1639 dsl_dataset_rele(ds
, FTAG
);
1643 if (ddsa
->ddsa_errors
!= NULL
)
1644 fnvlist_add_int32(ddsa
->ddsa_errors
,
1647 /* only report one error for this check */
1651 nvlist_free(cnt_track
);
1654 for (pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, NULL
);
1655 pair
!= NULL
; pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, pair
)) {
1658 char *name
, *atp
= NULL
;
1659 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
1661 name
= nvpair_name(pair
);
1662 if (strlen(name
) >= ZFS_MAX_DATASET_NAME_LEN
)
1663 error
= SET_ERROR(ENAMETOOLONG
);
1665 atp
= strchr(name
, '@');
1667 error
= SET_ERROR(EINVAL
);
1669 (void) strlcpy(dsname
, name
, atp
- name
+ 1);
1672 error
= dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
);
1674 /* passing 0/NULL skips dsl_fs_ss_limit_check */
1675 error
= dsl_dataset_snapshot_check_impl(ds
,
1676 atp
+ 1, tx
, B_FALSE
, 0, NULL
, NULL
);
1677 dsl_dataset_rele(ds
, FTAG
);
1681 if (ddsa
->ddsa_errors
!= NULL
) {
1682 fnvlist_add_int32(ddsa
->ddsa_errors
,
1693 dsl_dataset_snapshot_sync_impl(dsl_dataset_t
*ds
, const char *snapname
,
1696 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1698 dsl_dataset_phys_t
*dsphys
;
1699 uint64_t dsobj
, crtxg
;
1700 objset_t
*mos
= dp
->dp_meta_objset
;
1701 objset_t
*os __maybe_unused
;
1703 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
1706 * If we are on an old pool, the zil must not be active, in which
1707 * case it will be zeroed. Usually zil_suspend() accomplishes this.
1709 ASSERT(spa_version(dmu_tx_pool(tx
)->dp_spa
) >= SPA_VERSION_FAST_SNAP
||
1710 dmu_objset_from_ds(ds
, &os
) != 0 ||
1711 memcmp(&os
->os_phys
->os_zil_header
, &zero_zil
,
1712 sizeof (zero_zil
)) == 0);
1714 /* Should not snapshot a dirty dataset. */
1715 ASSERT(!txg_list_member(&ds
->ds_dir
->dd_pool
->dp_dirty_datasets
,
1718 dsl_fs_ss_count_adjust(ds
->ds_dir
, 1, DD_FIELD_SNAPSHOT_COUNT
, tx
);
1721 * The origin's ds_creation_txg has to be < TXG_INITIAL
1723 if (strcmp(snapname
, ORIGIN_DIR_NAME
) == 0)
1728 dsobj
= dmu_object_alloc(mos
, DMU_OT_DSL_DATASET
, 0,
1729 DMU_OT_DSL_DATASET
, sizeof (dsl_dataset_phys_t
), tx
);
1730 VERIFY0(dmu_bonus_hold(mos
, dsobj
, FTAG
, &dbuf
));
1731 dmu_buf_will_dirty(dbuf
, tx
);
1732 dsphys
= dbuf
->db_data
;
1733 memset(dsphys
, 0, sizeof (dsl_dataset_phys_t
));
1734 dsphys
->ds_dir_obj
= ds
->ds_dir
->dd_object
;
1735 dsphys
->ds_fsid_guid
= unique_create();
1736 (void) random_get_pseudo_bytes((void*)&dsphys
->ds_guid
,
1737 sizeof (dsphys
->ds_guid
));
1738 dsphys
->ds_prev_snap_obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
1739 dsphys
->ds_prev_snap_txg
= dsl_dataset_phys(ds
)->ds_prev_snap_txg
;
1740 dsphys
->ds_next_snap_obj
= ds
->ds_object
;
1741 dsphys
->ds_num_children
= 1;
1742 dsphys
->ds_creation_time
= gethrestime_sec();
1743 dsphys
->ds_creation_txg
= crtxg
;
1744 dsphys
->ds_deadlist_obj
= dsl_dataset_phys(ds
)->ds_deadlist_obj
;
1745 dsphys
->ds_referenced_bytes
= dsl_dataset_phys(ds
)->ds_referenced_bytes
;
1746 dsphys
->ds_compressed_bytes
= dsl_dataset_phys(ds
)->ds_compressed_bytes
;
1747 dsphys
->ds_uncompressed_bytes
=
1748 dsl_dataset_phys(ds
)->ds_uncompressed_bytes
;
1749 dsphys
->ds_flags
= dsl_dataset_phys(ds
)->ds_flags
;
1750 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1751 dsphys
->ds_bp
= dsl_dataset_phys(ds
)->ds_bp
;
1752 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1753 dmu_buf_rele(dbuf
, FTAG
);
1755 for (spa_feature_t f
= 0; f
< SPA_FEATURES
; f
++) {
1756 if (zfeature_active(f
, ds
->ds_feature
[f
])) {
1757 dsl_dataset_activate_feature(dsobj
, f
,
1758 ds
->ds_feature
[f
], tx
);
1763 * We are not allowed to dirty a filesystem when done receiving
1764 * a snapshot. In this case some flags such as SPA_FEATURE_LARGE_BLOCKS
1765 * will not be set and a subsequent encrypted raw send will fail. Hence
1766 * activate this feature if needed here. This needs to happen only in
1769 if (dmu_tx_is_syncing(tx
)) {
1770 for (spa_feature_t f
= 0; f
< SPA_FEATURES
; f
++) {
1771 if (zfeature_active(f
, ds
->ds_feature_activation
[f
]) &&
1772 !(zfeature_active(f
, ds
->ds_feature
[f
]))) {
1773 dsl_dataset_activate_feature(dsobj
, f
,
1774 ds
->ds_feature_activation
[f
], tx
);
1776 ds
->ds_feature_activation
[f
];
1781 ASSERT3U(ds
->ds_prev
!= 0, ==,
1782 dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0);
1784 uint64_t next_clones_obj
=
1785 dsl_dataset_phys(ds
->ds_prev
)->ds_next_clones_obj
;
1786 ASSERT(dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
==
1788 dsl_dataset_phys(ds
->ds_prev
)->ds_num_children
> 1);
1789 if (dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
==
1791 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
1792 ASSERT3U(dsl_dataset_phys(ds
)->ds_prev_snap_txg
, ==,
1793 dsl_dataset_phys(ds
->ds_prev
)->ds_creation_txg
);
1794 dsl_dataset_phys(ds
->ds_prev
)->ds_next_snap_obj
= dsobj
;
1795 } else if (next_clones_obj
!= 0) {
1796 dsl_dataset_remove_from_next_clones(ds
->ds_prev
,
1797 dsphys
->ds_next_snap_obj
, tx
);
1798 VERIFY0(zap_add_int(mos
,
1799 next_clones_obj
, dsobj
, tx
));
1804 * If we have a reference-reservation on this dataset, we will
1805 * need to increase the amount of refreservation being charged
1806 * since our unique space is going to zero.
1808 if (ds
->ds_reserved
) {
1810 ASSERT(DS_UNIQUE_IS_ACCURATE(ds
));
1811 delta
= MIN(dsl_dataset_phys(ds
)->ds_unique_bytes
,
1813 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_REFRSRV
,
1817 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1818 dsl_dataset_phys(ds
)->ds_deadlist_obj
=
1819 dsl_deadlist_clone(&ds
->ds_deadlist
, UINT64_MAX
,
1820 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, tx
);
1821 dsl_deadlist_close(&ds
->ds_deadlist
);
1822 dsl_deadlist_open(&ds
->ds_deadlist
, mos
,
1823 dsl_dataset_phys(ds
)->ds_deadlist_obj
);
1824 dsl_deadlist_add_key(&ds
->ds_deadlist
,
1825 dsl_dataset_phys(ds
)->ds_prev_snap_txg
, tx
);
1826 dsl_bookmark_snapshotted(ds
, tx
);
1828 if (dsl_dataset_remap_deadlist_exists(ds
)) {
1829 uint64_t remap_deadlist_obj
=
1830 dsl_dataset_get_remap_deadlist_object(ds
);
1832 * Move the remap_deadlist to the snapshot. The head
1833 * will create a new remap deadlist on demand, from
1834 * dsl_dataset_block_remapped().
1836 dsl_dataset_unset_remap_deadlist_object(ds
, tx
);
1837 dsl_deadlist_close(&ds
->ds_remap_deadlist
);
1839 dmu_object_zapify(mos
, dsobj
, DMU_OT_DSL_DATASET
, tx
);
1840 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_REMAP_DEADLIST
,
1841 sizeof (remap_deadlist_obj
), 1, &remap_deadlist_obj
, tx
));
1845 * Create a ivset guid for this snapshot if the dataset is
1846 * encrypted. This may be overridden by a raw receive. A
1847 * previous implementation of this code did not have this
1848 * field as part of the on-disk format for ZFS encryption
1849 * (see errata #4). As part of the remediation for this
1850 * issue, we ask the user to enable the bookmark_v2 feature
1851 * which is now a dependency of the encryption feature. We
1852 * use this as a heuristic to determine when the user has
1853 * elected to correct any datasets created with the old code.
1854 * As a result, we only do this step if the bookmark_v2
1855 * feature is enabled, which limits the number of states a
1856 * given pool / dataset can be in with regards to terms of
1857 * correcting the issue.
1859 if (ds
->ds_dir
->dd_crypto_obj
!= 0 &&
1860 spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_BOOKMARK_V2
)) {
1861 uint64_t ivset_guid
= unique_create();
1863 dmu_object_zapify(mos
, dsobj
, DMU_OT_DSL_DATASET
, tx
);
1864 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_IVSET_GUID
,
1865 sizeof (ivset_guid
), 1, &ivset_guid
, tx
));
1868 ASSERT3U(dsl_dataset_phys(ds
)->ds_prev_snap_txg
, <, tx
->tx_txg
);
1869 dsl_dataset_phys(ds
)->ds_prev_snap_obj
= dsobj
;
1870 dsl_dataset_phys(ds
)->ds_prev_snap_txg
= crtxg
;
1871 dsl_dataset_phys(ds
)->ds_unique_bytes
= 0;
1873 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_UNIQUE_ACCURATE
)
1874 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
1876 VERIFY0(zap_add(mos
, dsl_dataset_phys(ds
)->ds_snapnames_zapobj
,
1877 snapname
, 8, 1, &dsobj
, tx
));
1880 dsl_dataset_rele(ds
->ds_prev
, ds
);
1881 VERIFY0(dsl_dataset_hold_obj(dp
,
1882 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, ds
, &ds
->ds_prev
));
1884 dsl_scan_ds_snapshotted(ds
, tx
);
1886 dsl_dir_snap_cmtime_update(ds
->ds_dir
, tx
);
1888 if (zfs_snapshot_history_enabled
)
1889 spa_history_log_internal_ds(ds
->ds_prev
, "snapshot", tx
, " ");
1893 dsl_dataset_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
1895 dsl_dataset_snapshot_arg_t
*ddsa
= arg
;
1896 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1899 for (pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, NULL
);
1900 pair
!= NULL
; pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, pair
)) {
1903 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
1905 name
= nvpair_name(pair
);
1906 atp
= strchr(name
, '@');
1907 (void) strlcpy(dsname
, name
, atp
- name
+ 1);
1908 VERIFY0(dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
));
1910 dsl_dataset_snapshot_sync_impl(ds
, atp
+ 1, tx
);
1911 if (ddsa
->ddsa_props
!= NULL
) {
1912 dsl_props_set_sync_impl(ds
->ds_prev
,
1913 ZPROP_SRC_LOCAL
, ddsa
->ddsa_props
, tx
);
1915 dsl_dataset_rele(ds
, FTAG
);
1920 * The snapshots must all be in the same pool.
1921 * All-or-nothing: if there are any failures, nothing will be modified.
1924 dsl_dataset_snapshot(nvlist_t
*snaps
, nvlist_t
*props
, nvlist_t
*errors
)
1926 dsl_dataset_snapshot_arg_t ddsa
;
1928 boolean_t needsuspend
;
1932 nvlist_t
*suspended
= NULL
;
1934 pair
= nvlist_next_nvpair(snaps
, NULL
);
1937 firstname
= nvpair_name(pair
);
1939 error
= spa_open(firstname
, &spa
, FTAG
);
1942 needsuspend
= (spa_version(spa
) < SPA_VERSION_FAST_SNAP
);
1943 spa_close(spa
, FTAG
);
1946 suspended
= fnvlist_alloc();
1947 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
1948 pair
= nvlist_next_nvpair(snaps
, pair
)) {
1949 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
1950 char *snapname
= nvpair_name(pair
);
1954 atp
= strchr(snapname
, '@');
1956 error
= SET_ERROR(EINVAL
);
1959 (void) strlcpy(fsname
, snapname
, atp
- snapname
+ 1);
1961 error
= zil_suspend(fsname
, &cookie
);
1964 fnvlist_add_uint64(suspended
, fsname
,
1969 ddsa
.ddsa_snaps
= snaps
;
1970 ddsa
.ddsa_props
= props
;
1971 ddsa
.ddsa_errors
= errors
;
1972 ddsa
.ddsa_cr
= CRED();
1973 ddsa
.ddsa_proc
= curproc
;
1976 error
= dsl_sync_task(firstname
, dsl_dataset_snapshot_check
,
1977 dsl_dataset_snapshot_sync
, &ddsa
,
1978 fnvlist_num_pairs(snaps
) * 3, ZFS_SPACE_CHECK_NORMAL
);
1981 if (suspended
!= NULL
) {
1982 for (pair
= nvlist_next_nvpair(suspended
, NULL
); pair
!= NULL
;
1983 pair
= nvlist_next_nvpair(suspended
, pair
)) {
1984 zil_resume((void *)(uintptr_t)
1985 fnvpair_value_uint64(pair
));
1987 fnvlist_free(suspended
);
1991 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
1992 pair
= nvlist_next_nvpair(snaps
, pair
)) {
1993 zvol_create_minor(nvpair_name(pair
));
2000 typedef struct dsl_dataset_snapshot_tmp_arg
{
2001 const char *ddsta_fsname
;
2002 const char *ddsta_snapname
;
2003 minor_t ddsta_cleanup_minor
;
2004 const char *ddsta_htag
;
2005 } dsl_dataset_snapshot_tmp_arg_t
;
2008 dsl_dataset_snapshot_tmp_check(void *arg
, dmu_tx_t
*tx
)
2010 dsl_dataset_snapshot_tmp_arg_t
*ddsta
= arg
;
2011 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2015 error
= dsl_dataset_hold(dp
, ddsta
->ddsta_fsname
, FTAG
, &ds
);
2019 /* NULL cred means no limit check for tmp snapshot */
2020 error
= dsl_dataset_snapshot_check_impl(ds
, ddsta
->ddsta_snapname
,
2021 tx
, B_FALSE
, 0, NULL
, NULL
);
2023 dsl_dataset_rele(ds
, FTAG
);
2027 if (spa_version(dp
->dp_spa
) < SPA_VERSION_USERREFS
) {
2028 dsl_dataset_rele(ds
, FTAG
);
2029 return (SET_ERROR(ENOTSUP
));
2031 error
= dsl_dataset_user_hold_check_one(NULL
, ddsta
->ddsta_htag
,
2034 dsl_dataset_rele(ds
, FTAG
);
2038 dsl_dataset_rele(ds
, FTAG
);
2043 dsl_dataset_snapshot_tmp_sync(void *arg
, dmu_tx_t
*tx
)
2045 dsl_dataset_snapshot_tmp_arg_t
*ddsta
= arg
;
2046 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2047 dsl_dataset_t
*ds
= NULL
;
2049 VERIFY0(dsl_dataset_hold(dp
, ddsta
->ddsta_fsname
, FTAG
, &ds
));
2051 dsl_dataset_snapshot_sync_impl(ds
, ddsta
->ddsta_snapname
, tx
);
2052 dsl_dataset_user_hold_sync_one(ds
->ds_prev
, ddsta
->ddsta_htag
,
2053 ddsta
->ddsta_cleanup_minor
, gethrestime_sec(), tx
);
2054 dsl_destroy_snapshot_sync_impl(ds
->ds_prev
, B_TRUE
, tx
);
2056 dsl_dataset_rele(ds
, FTAG
);
2060 dsl_dataset_snapshot_tmp(const char *fsname
, const char *snapname
,
2061 minor_t cleanup_minor
, const char *htag
)
2063 dsl_dataset_snapshot_tmp_arg_t ddsta
;
2066 boolean_t needsuspend
;
2069 ddsta
.ddsta_fsname
= fsname
;
2070 ddsta
.ddsta_snapname
= snapname
;
2071 ddsta
.ddsta_cleanup_minor
= cleanup_minor
;
2072 ddsta
.ddsta_htag
= htag
;
2074 error
= spa_open(fsname
, &spa
, FTAG
);
2077 needsuspend
= (spa_version(spa
) < SPA_VERSION_FAST_SNAP
);
2078 spa_close(spa
, FTAG
);
2081 error
= zil_suspend(fsname
, &cookie
);
2086 error
= dsl_sync_task(fsname
, dsl_dataset_snapshot_tmp_check
,
2087 dsl_dataset_snapshot_tmp_sync
, &ddsta
, 3, ZFS_SPACE_CHECK_RESERVED
);
2095 dsl_dataset_sync(dsl_dataset_t
*ds
, zio_t
*zio
, dmu_tx_t
*tx
)
2097 ASSERT(dmu_tx_is_syncing(tx
));
2098 ASSERT(ds
->ds_objset
!= NULL
);
2099 ASSERT(dsl_dataset_phys(ds
)->ds_next_snap_obj
== 0);
2102 * in case we had to change ds_fsid_guid when we opened it,
2105 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2106 dsl_dataset_phys(ds
)->ds_fsid_guid
= ds
->ds_fsid_guid
;
2108 if (ds
->ds_resume_bytes
[tx
->tx_txg
& TXG_MASK
] != 0) {
2109 VERIFY0(zap_update(tx
->tx_pool
->dp_meta_objset
,
2110 ds
->ds_object
, DS_FIELD_RESUME_OBJECT
, 8, 1,
2111 &ds
->ds_resume_object
[tx
->tx_txg
& TXG_MASK
], tx
));
2112 VERIFY0(zap_update(tx
->tx_pool
->dp_meta_objset
,
2113 ds
->ds_object
, DS_FIELD_RESUME_OFFSET
, 8, 1,
2114 &ds
->ds_resume_offset
[tx
->tx_txg
& TXG_MASK
], tx
));
2115 VERIFY0(zap_update(tx
->tx_pool
->dp_meta_objset
,
2116 ds
->ds_object
, DS_FIELD_RESUME_BYTES
, 8, 1,
2117 &ds
->ds_resume_bytes
[tx
->tx_txg
& TXG_MASK
], tx
));
2118 ds
->ds_resume_object
[tx
->tx_txg
& TXG_MASK
] = 0;
2119 ds
->ds_resume_offset
[tx
->tx_txg
& TXG_MASK
] = 0;
2120 ds
->ds_resume_bytes
[tx
->tx_txg
& TXG_MASK
] = 0;
2123 dmu_objset_sync(ds
->ds_objset
, zio
, tx
);
2125 for (spa_feature_t f
= 0; f
< SPA_FEATURES
; f
++) {
2126 if (zfeature_active(f
, ds
->ds_feature_activation
[f
])) {
2127 if (zfeature_active(f
, ds
->ds_feature
[f
]))
2129 dsl_dataset_activate_feature(ds
->ds_object
, f
,
2130 ds
->ds_feature_activation
[f
], tx
);
2131 ds
->ds_feature
[f
] = ds
->ds_feature_activation
[f
];
2137 * Check if the percentage of blocks shared between the clone and the
2138 * snapshot (as opposed to those that are clone only) is below a certain
2142 dsl_livelist_should_disable(dsl_dataset_t
*ds
)
2144 uint64_t used
, referenced
;
2147 used
= dsl_dir_get_usedds(ds
->ds_dir
);
2148 referenced
= dsl_get_referenced(ds
);
2149 if (referenced
== 0)
2151 percent_shared
= (100 * (referenced
- used
)) / referenced
;
2152 if (percent_shared
<= zfs_livelist_min_percent_shared
)
2158 * Check if it is possible to combine two livelist entries into one.
2159 * This is the case if the combined number of 'live' blkptrs (ALLOCs that
2160 * don't have a matching FREE) is under the maximum sublist size.
2161 * We check this by subtracting twice the total number of frees from the total
2162 * number of blkptrs. FREEs are counted twice because each FREE blkptr
2163 * will cancel out an ALLOC blkptr when the livelist is processed.
2166 dsl_livelist_should_condense(dsl_deadlist_entry_t
*first
,
2167 dsl_deadlist_entry_t
*next
)
2169 uint64_t total_free
= first
->dle_bpobj
.bpo_phys
->bpo_num_freed
+
2170 next
->dle_bpobj
.bpo_phys
->bpo_num_freed
;
2171 uint64_t total_entries
= first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
+
2172 next
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
2173 if ((total_entries
- (2 * total_free
)) < zfs_livelist_max_entries
)
2178 typedef struct try_condense_arg
{
2181 } try_condense_arg_t
;
2184 * Iterate over the livelist entries, searching for a pair to condense.
2185 * A nonzero return value means stop, 0 means keep looking.
2188 dsl_livelist_try_condense(void *arg
, dsl_deadlist_entry_t
*first
)
2190 try_condense_arg_t
*tca
= arg
;
2191 spa_t
*spa
= tca
->spa
;
2192 dsl_dataset_t
*ds
= tca
->ds
;
2193 dsl_deadlist_t
*ll
= &ds
->ds_dir
->dd_livelist
;
2194 dsl_deadlist_entry_t
*next
;
2196 /* The condense thread has not yet been created at import */
2197 if (spa
->spa_livelist_condense_zthr
== NULL
)
2200 /* A condense is already in progress */
2201 if (spa
->spa_to_condense
.ds
!= NULL
)
2204 next
= AVL_NEXT(&ll
->dl_tree
, &first
->dle_node
);
2205 /* The livelist has only one entry - don't condense it */
2209 /* Next is the newest entry - don't condense it */
2210 if (AVL_NEXT(&ll
->dl_tree
, &next
->dle_node
) == NULL
)
2213 /* This pair is not ready to condense but keep looking */
2214 if (!dsl_livelist_should_condense(first
, next
))
2218 * Add a ref to prevent the dataset from being evicted while
2219 * the condense zthr or synctask are running. Ref will be
2220 * released at the end of the condense synctask
2222 dmu_buf_add_ref(ds
->ds_dbuf
, spa
);
2224 spa
->spa_to_condense
.ds
= ds
;
2225 spa
->spa_to_condense
.first
= first
;
2226 spa
->spa_to_condense
.next
= next
;
2227 spa
->spa_to_condense
.syncing
= B_FALSE
;
2228 spa
->spa_to_condense
.cancelled
= B_FALSE
;
2230 zthr_wakeup(spa
->spa_livelist_condense_zthr
);
2235 dsl_flush_pending_livelist(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
2237 dsl_dir_t
*dd
= ds
->ds_dir
;
2238 spa_t
*spa
= ds
->ds_dir
->dd_pool
->dp_spa
;
2239 dsl_deadlist_entry_t
*last
= dsl_deadlist_last(&dd
->dd_livelist
);
2241 /* Check if we need to add a new sub-livelist */
2243 /* The livelist is empty */
2244 dsl_deadlist_add_key(&dd
->dd_livelist
,
2245 tx
->tx_txg
- 1, tx
);
2246 } else if (spa_sync_pass(spa
) == 1) {
2248 * Check if the newest entry is full. If it is, make a new one.
2249 * We only do this once per sync because we could overfill a
2250 * sublist in one sync pass and don't want to add another entry
2251 * for a txg that is already represented. This ensures that
2252 * blkptrs born in the same txg are stored in the same sublist.
2254 bpobj_t bpobj
= last
->dle_bpobj
;
2255 uint64_t all
= bpobj
.bpo_phys
->bpo_num_blkptrs
;
2256 uint64_t free
= bpobj
.bpo_phys
->bpo_num_freed
;
2257 uint64_t alloc
= all
- free
;
2258 if (alloc
> zfs_livelist_max_entries
) {
2259 dsl_deadlist_add_key(&dd
->dd_livelist
,
2260 tx
->tx_txg
- 1, tx
);
2264 /* Insert each entry into the on-disk livelist */
2265 bplist_iterate(&dd
->dd_pending_allocs
,
2266 dsl_deadlist_insert_alloc_cb
, &dd
->dd_livelist
, tx
);
2267 bplist_iterate(&dd
->dd_pending_frees
,
2268 dsl_deadlist_insert_free_cb
, &dd
->dd_livelist
, tx
);
2270 /* Attempt to condense every pair of adjacent entries */
2271 try_condense_arg_t arg
= {
2275 dsl_deadlist_iterate(&dd
->dd_livelist
, dsl_livelist_try_condense
,
2280 dsl_dataset_sync_done(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
2282 objset_t
*os
= ds
->ds_objset
;
2284 bplist_iterate(&ds
->ds_pending_deadlist
,
2285 dsl_deadlist_insert_alloc_cb
, &ds
->ds_deadlist
, tx
);
2287 if (dsl_deadlist_is_open(&ds
->ds_dir
->dd_livelist
)) {
2288 dsl_flush_pending_livelist(ds
, tx
);
2289 if (dsl_livelist_should_disable(ds
)) {
2290 dsl_dir_remove_livelist(ds
->ds_dir
, tx
, B_TRUE
);
2294 dsl_bookmark_sync_done(ds
, tx
);
2296 multilist_destroy(&os
->os_synced_dnodes
);
2298 if (os
->os_encrypted
)
2299 os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
] = B_FALSE
;
2301 ASSERT0(os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
]);
2303 ASSERT(!dmu_objset_is_dirty(os
, dmu_tx_get_txg(tx
)));
2305 dmu_buf_rele(ds
->ds_dbuf
, ds
);
2309 get_clones_stat_impl(dsl_dataset_t
*ds
, nvlist_t
*val
)
2312 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
2316 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
2319 * There may be missing entries in ds_next_clones_obj
2320 * due to a bug in a previous version of the code.
2321 * Only trust it if it has the right number of entries.
2323 if (dsl_dataset_phys(ds
)->ds_next_clones_obj
!= 0) {
2324 VERIFY0(zap_count(mos
, dsl_dataset_phys(ds
)->ds_next_clones_obj
,
2327 if (count
!= dsl_dataset_phys(ds
)->ds_num_children
- 1) {
2328 return (SET_ERROR(ENOENT
));
2330 for (zap_cursor_init(&zc
, mos
,
2331 dsl_dataset_phys(ds
)->ds_next_clones_obj
);
2332 zap_cursor_retrieve(&zc
, &za
) == 0;
2333 zap_cursor_advance(&zc
)) {
2334 dsl_dataset_t
*clone
;
2335 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2336 VERIFY0(dsl_dataset_hold_obj(ds
->ds_dir
->dd_pool
,
2337 za
.za_first_integer
, FTAG
, &clone
));
2338 dsl_dir_name(clone
->ds_dir
, buf
);
2339 fnvlist_add_boolean(val
, buf
);
2340 dsl_dataset_rele(clone
, FTAG
);
2342 zap_cursor_fini(&zc
);
2347 get_clones_stat(dsl_dataset_t
*ds
, nvlist_t
*nv
)
2349 nvlist_t
*propval
= fnvlist_alloc();
2350 nvlist_t
*val
= fnvlist_alloc();
2352 if (get_clones_stat_impl(ds
, val
) == 0) {
2353 fnvlist_add_nvlist(propval
, ZPROP_VALUE
, val
);
2354 fnvlist_add_nvlist(nv
, zfs_prop_to_name(ZFS_PROP_CLONES
),
2359 nvlist_free(propval
);
2363 get_receive_resume_token_impl(dsl_dataset_t
*ds
)
2365 if (!dsl_dataset_has_resume_receive_state(ds
))
2368 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
2371 uint8_t *compressed
;
2373 nvlist_t
*token_nv
= fnvlist_alloc();
2374 size_t packed_size
, compressed_size
;
2376 if (zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
2377 DS_FIELD_RESUME_FROMGUID
, sizeof (val
), 1, &val
) == 0) {
2378 fnvlist_add_uint64(token_nv
, "fromguid", val
);
2380 if (zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
2381 DS_FIELD_RESUME_OBJECT
, sizeof (val
), 1, &val
) == 0) {
2382 fnvlist_add_uint64(token_nv
, "object", val
);
2384 if (zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
2385 DS_FIELD_RESUME_OFFSET
, sizeof (val
), 1, &val
) == 0) {
2386 fnvlist_add_uint64(token_nv
, "offset", val
);
2388 if (zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
2389 DS_FIELD_RESUME_BYTES
, sizeof (val
), 1, &val
) == 0) {
2390 fnvlist_add_uint64(token_nv
, "bytes", val
);
2392 if (zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
2393 DS_FIELD_RESUME_TOGUID
, sizeof (val
), 1, &val
) == 0) {
2394 fnvlist_add_uint64(token_nv
, "toguid", val
);
2396 char buf
[MAXNAMELEN
];
2397 if (zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
2398 DS_FIELD_RESUME_TONAME
, 1, sizeof (buf
), buf
) == 0) {
2399 fnvlist_add_string(token_nv
, "toname", buf
);
2401 if (zap_contains(dp
->dp_meta_objset
, ds
->ds_object
,
2402 DS_FIELD_RESUME_LARGEBLOCK
) == 0) {
2403 fnvlist_add_boolean(token_nv
, "largeblockok");
2405 if (zap_contains(dp
->dp_meta_objset
, ds
->ds_object
,
2406 DS_FIELD_RESUME_EMBEDOK
) == 0) {
2407 fnvlist_add_boolean(token_nv
, "embedok");
2409 if (zap_contains(dp
->dp_meta_objset
, ds
->ds_object
,
2410 DS_FIELD_RESUME_COMPRESSOK
) == 0) {
2411 fnvlist_add_boolean(token_nv
, "compressok");
2413 if (zap_contains(dp
->dp_meta_objset
, ds
->ds_object
,
2414 DS_FIELD_RESUME_RAWOK
) == 0) {
2415 fnvlist_add_boolean(token_nv
, "rawok");
2417 if (dsl_dataset_feature_is_active(ds
,
2418 SPA_FEATURE_REDACTED_DATASETS
)) {
2419 uint64_t num_redact_snaps
= 0;
2420 uint64_t *redact_snaps
= NULL
;
2421 VERIFY3B(dsl_dataset_get_uint64_array_feature(ds
,
2422 SPA_FEATURE_REDACTED_DATASETS
, &num_redact_snaps
,
2423 &redact_snaps
), ==, B_TRUE
);
2424 fnvlist_add_uint64_array(token_nv
, "redact_snaps",
2425 redact_snaps
, num_redact_snaps
);
2427 if (zap_contains(dp
->dp_meta_objset
, ds
->ds_object
,
2428 DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS
) == 0) {
2429 uint64_t num_redact_snaps
= 0, int_size
= 0;
2430 uint64_t *redact_snaps
= NULL
;
2431 VERIFY0(zap_length(dp
->dp_meta_objset
, ds
->ds_object
,
2432 DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS
, &int_size
,
2433 &num_redact_snaps
));
2434 ASSERT3U(int_size
, ==, sizeof (uint64_t));
2436 redact_snaps
= kmem_alloc(int_size
* num_redact_snaps
,
2438 VERIFY0(zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
2439 DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS
, int_size
,
2440 num_redact_snaps
, redact_snaps
));
2441 fnvlist_add_uint64_array(token_nv
, "book_redact_snaps",
2442 redact_snaps
, num_redact_snaps
);
2443 kmem_free(redact_snaps
, int_size
* num_redact_snaps
);
2445 packed
= fnvlist_pack(token_nv
, &packed_size
);
2446 fnvlist_free(token_nv
);
2447 compressed
= kmem_alloc(packed_size
, KM_SLEEP
);
2449 compressed_size
= gzip_compress(packed
, compressed
,
2450 packed_size
, packed_size
, 6);
2453 fletcher_4_native_varsize(compressed
, compressed_size
, &cksum
);
2455 size_t alloc_size
= compressed_size
* 2 + 1;
2456 str
= kmem_alloc(alloc_size
, KM_SLEEP
);
2457 for (int i
= 0; i
< compressed_size
; i
++) {
2458 size_t offset
= i
* 2;
2459 (void) snprintf(str
+ offset
, alloc_size
- offset
,
2460 "%02x", compressed
[i
]);
2462 str
[compressed_size
* 2] = '\0';
2463 char *propval
= kmem_asprintf("%u-%llx-%llx-%s",
2464 ZFS_SEND_RESUME_TOKEN_VERSION
,
2465 (longlong_t
)cksum
.zc_word
[0],
2466 (longlong_t
)packed_size
, str
);
2467 kmem_free(packed
, packed_size
);
2468 kmem_free(str
, alloc_size
);
2469 kmem_free(compressed
, packed_size
);
2474 * Returns a string that represents the receive resume state token. It should
2475 * be freed with strfree(). NULL is returned if no resume state is present.
2478 get_receive_resume_token(dsl_dataset_t
*ds
)
2481 * A failed "newfs" (e.g. full) resumable receive leaves
2482 * the stats set on this dataset. Check here for the prop.
2484 char *token
= get_receive_resume_token_impl(ds
);
2488 * A failed incremental resumable receive leaves the
2489 * stats set on our child named "%recv". Check the child
2492 /* 6 extra bytes for /%recv */
2493 char name
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
2494 dsl_dataset_t
*recv_ds
;
2495 dsl_dataset_name(ds
, name
);
2496 if (strlcat(name
, "/", sizeof (name
)) < sizeof (name
) &&
2497 strlcat(name
, recv_clone_name
, sizeof (name
)) < sizeof (name
) &&
2498 dsl_dataset_hold(ds
->ds_dir
->dd_pool
, name
, FTAG
, &recv_ds
) == 0) {
2499 token
= get_receive_resume_token_impl(recv_ds
);
2500 dsl_dataset_rele(recv_ds
, FTAG
);
2506 dsl_get_refratio(dsl_dataset_t
*ds
)
2508 uint64_t ratio
= dsl_dataset_phys(ds
)->ds_compressed_bytes
== 0 ? 100 :
2509 (dsl_dataset_phys(ds
)->ds_uncompressed_bytes
* 100 /
2510 dsl_dataset_phys(ds
)->ds_compressed_bytes
);
2515 dsl_get_logicalreferenced(dsl_dataset_t
*ds
)
2517 return (dsl_dataset_phys(ds
)->ds_uncompressed_bytes
);
2521 dsl_get_compressratio(dsl_dataset_t
*ds
)
2523 if (ds
->ds_is_snapshot
) {
2524 return (dsl_get_refratio(ds
));
2526 dsl_dir_t
*dd
= ds
->ds_dir
;
2527 mutex_enter(&dd
->dd_lock
);
2528 uint64_t val
= dsl_dir_get_compressratio(dd
);
2529 mutex_exit(&dd
->dd_lock
);
2535 dsl_get_used(dsl_dataset_t
*ds
)
2537 if (ds
->ds_is_snapshot
) {
2538 return (dsl_dataset_phys(ds
)->ds_unique_bytes
);
2540 dsl_dir_t
*dd
= ds
->ds_dir
;
2541 mutex_enter(&dd
->dd_lock
);
2542 uint64_t val
= dsl_dir_get_used(dd
);
2543 mutex_exit(&dd
->dd_lock
);
2549 dsl_get_creation(dsl_dataset_t
*ds
)
2551 return (dsl_dataset_phys(ds
)->ds_creation_time
);
2555 dsl_get_creationtxg(dsl_dataset_t
*ds
)
2557 return (dsl_dataset_phys(ds
)->ds_creation_txg
);
2561 dsl_get_refquota(dsl_dataset_t
*ds
)
2563 return (ds
->ds_quota
);
2567 dsl_get_refreservation(dsl_dataset_t
*ds
)
2569 return (ds
->ds_reserved
);
2573 dsl_get_guid(dsl_dataset_t
*ds
)
2575 return (dsl_dataset_phys(ds
)->ds_guid
);
2579 dsl_get_unique(dsl_dataset_t
*ds
)
2581 return (dsl_dataset_phys(ds
)->ds_unique_bytes
);
2585 dsl_get_objsetid(dsl_dataset_t
*ds
)
2587 return (ds
->ds_object
);
2591 dsl_get_userrefs(dsl_dataset_t
*ds
)
2593 return (ds
->ds_userrefs
);
2597 dsl_get_defer_destroy(dsl_dataset_t
*ds
)
2599 return (DS_IS_DEFER_DESTROY(ds
) ? 1 : 0);
2603 dsl_get_referenced(dsl_dataset_t
*ds
)
2605 return (dsl_dataset_phys(ds
)->ds_referenced_bytes
);
2609 dsl_get_numclones(dsl_dataset_t
*ds
)
2611 ASSERT(ds
->ds_is_snapshot
);
2612 return (dsl_dataset_phys(ds
)->ds_num_children
- 1);
2616 dsl_get_inconsistent(dsl_dataset_t
*ds
)
2618 return ((dsl_dataset_phys(ds
)->ds_flags
& DS_FLAG_INCONSISTENT
) ?
2623 dsl_get_redacted(dsl_dataset_t
*ds
)
2625 return (dsl_dataset_feature_is_active(ds
,
2626 SPA_FEATURE_REDACTED_DATASETS
));
2630 dsl_get_available(dsl_dataset_t
*ds
)
2632 uint64_t refdbytes
= dsl_get_referenced(ds
);
2633 uint64_t availbytes
= dsl_dir_space_available(ds
->ds_dir
,
2635 if (ds
->ds_reserved
> dsl_dataset_phys(ds
)->ds_unique_bytes
) {
2637 ds
->ds_reserved
- dsl_dataset_phys(ds
)->ds_unique_bytes
;
2639 if (ds
->ds_quota
!= 0) {
2641 * Adjust available bytes according to refquota
2643 if (refdbytes
< ds
->ds_quota
) {
2644 availbytes
= MIN(availbytes
,
2645 ds
->ds_quota
- refdbytes
);
2650 return (availbytes
);
2654 dsl_get_written(dsl_dataset_t
*ds
, uint64_t *written
)
2656 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
2657 dsl_dataset_t
*prev
;
2658 int err
= dsl_dataset_hold_obj(dp
,
2659 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, FTAG
, &prev
);
2661 uint64_t comp
, uncomp
;
2662 err
= dsl_dataset_space_written(prev
, ds
, written
,
2664 dsl_dataset_rele(prev
, FTAG
);
2670 * 'snap' should be a buffer of size ZFS_MAX_DATASET_NAME_LEN.
2673 dsl_get_prev_snap(dsl_dataset_t
*ds
, char *snap
)
2675 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
2676 if (ds
->ds_prev
!= NULL
&& ds
->ds_prev
!= dp
->dp_origin_snap
) {
2677 dsl_dataset_name(ds
->ds_prev
, snap
);
2680 return (SET_ERROR(ENOENT
));
2685 dsl_get_redact_snaps(dsl_dataset_t
*ds
, nvlist_t
*propval
)
2689 if (dsl_dataset_get_uint64_array_feature(ds
,
2690 SPA_FEATURE_REDACTED_DATASETS
, &nsnaps
, &snaps
)) {
2691 fnvlist_add_uint64_array(propval
, ZPROP_VALUE
, snaps
,
2697 * Returns the mountpoint property and source for the given dataset in the value
2698 * and source buffers. The value buffer must be at least as large as MAXPATHLEN
2699 * and the source buffer as least as large a ZFS_MAX_DATASET_NAME_LEN.
2700 * Returns 0 on success and an error on failure.
2703 dsl_get_mountpoint(dsl_dataset_t
*ds
, const char *dsname
, char *value
,
2707 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
2709 /* Retrieve the mountpoint value stored in the zap object */
2710 error
= dsl_prop_get_ds(ds
, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
), 1,
2711 ZAP_MAXVALUELEN
, value
, source
);
2717 * Process the dsname and source to find the full mountpoint string.
2718 * Can be skipped for 'legacy' or 'none'.
2720 if (value
[0] == '/') {
2721 char *buf
= kmem_alloc(ZAP_MAXVALUELEN
, KM_SLEEP
);
2723 const char *relpath
;
2726 * If we inherit the mountpoint, even from a dataset
2727 * with a received value, the source will be the path of
2728 * the dataset we inherit from. If source is
2729 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2732 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2735 ASSERT0(strncmp(dsname
, source
, strlen(source
)));
2736 relpath
= dsname
+ strlen(source
);
2737 if (relpath
[0] == '/')
2741 spa_altroot(dp
->dp_spa
, root
, ZAP_MAXVALUELEN
);
2744 * Special case an alternate root of '/'. This will
2745 * avoid having multiple leading slashes in the
2748 if (strcmp(root
, "/") == 0)
2752 * If the mountpoint is '/' then skip over this
2753 * if we are obtaining either an alternate root or
2754 * an inherited mountpoint.
2757 if (value
[1] == '\0' && (root
[0] != '\0' ||
2758 relpath
[0] != '\0'))
2761 mnt
= kmem_strdup(mnt
);
2763 if (relpath
[0] == '\0') {
2764 (void) snprintf(value
, ZAP_MAXVALUELEN
, "%s%s",
2767 (void) snprintf(value
, ZAP_MAXVALUELEN
, "%s%s%s%s",
2768 root
, mnt
, relpath
[0] == '@' ? "" : "/",
2771 kmem_free(buf
, ZAP_MAXVALUELEN
);
2779 dsl_dataset_stats(dsl_dataset_t
*ds
, nvlist_t
*nv
)
2781 dsl_pool_t
*dp __maybe_unused
= ds
->ds_dir
->dd_pool
;
2783 ASSERT(dsl_pool_config_held(dp
));
2785 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFRATIO
,
2786 dsl_get_refratio(ds
));
2787 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_LOGICALREFERENCED
,
2788 dsl_get_logicalreferenced(ds
));
2789 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_COMPRESSRATIO
,
2790 dsl_get_compressratio(ds
));
2791 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USED
,
2794 if (ds
->ds_is_snapshot
) {
2795 get_clones_stat(ds
, nv
);
2797 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2798 if (dsl_get_prev_snap(ds
, buf
) == 0)
2799 dsl_prop_nvlist_add_string(nv
, ZFS_PROP_PREV_SNAP
,
2801 dsl_dir_stats(ds
->ds_dir
, nv
);
2804 nvlist_t
*propval
= fnvlist_alloc();
2805 dsl_get_redact_snaps(ds
, propval
);
2806 fnvlist_add_nvlist(nv
, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
),
2808 nvlist_free(propval
);
2810 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_AVAILABLE
,
2811 dsl_get_available(ds
));
2812 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFERENCED
,
2813 dsl_get_referenced(ds
));
2814 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_CREATION
,
2815 dsl_get_creation(ds
));
2816 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_CREATETXG
,
2817 dsl_get_creationtxg(ds
));
2818 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFQUOTA
,
2819 dsl_get_refquota(ds
));
2820 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFRESERVATION
,
2821 dsl_get_refreservation(ds
));
2822 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_GUID
,
2824 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_UNIQUE
,
2825 dsl_get_unique(ds
));
2826 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_OBJSETID
,
2827 dsl_get_objsetid(ds
));
2828 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERREFS
,
2829 dsl_get_userrefs(ds
));
2830 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_DEFER_DESTROY
,
2831 dsl_get_defer_destroy(ds
));
2832 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_SNAPSHOTS_CHANGED
,
2833 dsl_dir_snap_cmtime(ds
->ds_dir
).tv_sec
);
2834 dsl_dataset_crypt_stats(ds
, nv
);
2836 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
!= 0) {
2838 if (dsl_get_written(ds
, &written
) == 0) {
2839 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_WRITTEN
,
2844 if (!dsl_dataset_is_snapshot(ds
)) {
2845 char *token
= get_receive_resume_token(ds
);
2846 if (token
!= NULL
) {
2847 dsl_prop_nvlist_add_string(nv
,
2848 ZFS_PROP_RECEIVE_RESUME_TOKEN
, token
);
2849 kmem_strfree(token
);
2855 dsl_dataset_fast_stat(dsl_dataset_t
*ds
, dmu_objset_stats_t
*stat
)
2857 dsl_pool_t
*dp __maybe_unused
= ds
->ds_dir
->dd_pool
;
2858 ASSERT(dsl_pool_config_held(dp
));
2860 stat
->dds_creation_txg
= dsl_get_creationtxg(ds
);
2861 stat
->dds_inconsistent
= dsl_get_inconsistent(ds
);
2862 stat
->dds_guid
= dsl_get_guid(ds
);
2863 stat
->dds_redacted
= dsl_get_redacted(ds
);
2864 stat
->dds_origin
[0] = '\0';
2865 if (ds
->ds_is_snapshot
) {
2866 stat
->dds_is_snapshot
= B_TRUE
;
2867 stat
->dds_num_clones
= dsl_get_numclones(ds
);
2869 stat
->dds_is_snapshot
= B_FALSE
;
2870 stat
->dds_num_clones
= 0;
2872 if (dsl_dir_is_clone(ds
->ds_dir
)) {
2873 dsl_dir_get_origin(ds
->ds_dir
, stat
->dds_origin
);
2879 dsl_dataset_fsid_guid(dsl_dataset_t
*ds
)
2881 return (ds
->ds_fsid_guid
);
2885 dsl_dataset_space(dsl_dataset_t
*ds
,
2886 uint64_t *refdbytesp
, uint64_t *availbytesp
,
2887 uint64_t *usedobjsp
, uint64_t *availobjsp
)
2889 *refdbytesp
= dsl_dataset_phys(ds
)->ds_referenced_bytes
;
2890 *availbytesp
= dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
);
2891 if (ds
->ds_reserved
> dsl_dataset_phys(ds
)->ds_unique_bytes
)
2893 ds
->ds_reserved
- dsl_dataset_phys(ds
)->ds_unique_bytes
;
2894 if (ds
->ds_quota
!= 0) {
2896 * Adjust available bytes according to refquota
2898 if (*refdbytesp
< ds
->ds_quota
)
2899 *availbytesp
= MIN(*availbytesp
,
2900 ds
->ds_quota
- *refdbytesp
);
2904 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
2905 *usedobjsp
= BP_GET_FILL(&dsl_dataset_phys(ds
)->ds_bp
);
2906 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
2907 *availobjsp
= DN_MAX_OBJECT
- *usedobjsp
;
2911 dsl_dataset_modified_since_snap(dsl_dataset_t
*ds
, dsl_dataset_t
*snap
)
2913 dsl_pool_t
*dp __maybe_unused
= ds
->ds_dir
->dd_pool
;
2916 ASSERT(dsl_pool_config_held(dp
));
2919 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
2920 birth
= dsl_dataset_get_blkptr(ds
)->blk_birth
;
2921 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
2922 if (birth
> dsl_dataset_phys(snap
)->ds_creation_txg
) {
2923 objset_t
*os
, *os_snap
;
2925 * It may be that only the ZIL differs, because it was
2926 * reset in the head. Don't count that as being
2929 if (dmu_objset_from_ds(ds
, &os
) != 0)
2931 if (dmu_objset_from_ds(snap
, &os_snap
) != 0)
2933 return (memcmp(&os
->os_phys
->os_meta_dnode
,
2934 &os_snap
->os_phys
->os_meta_dnode
,
2935 sizeof (os
->os_phys
->os_meta_dnode
)) != 0);
2941 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t
*dp
,
2942 dsl_dataset_t
*hds
, void *arg
)
2945 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
2949 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_oldsnapname
, &val
);
2951 /* ignore nonexistent snapshots */
2952 return (error
== ENOENT
? 0 : error
);
2955 /* new name should not exist */
2956 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_newsnapname
, &val
);
2958 error
= SET_ERROR(EEXIST
);
2959 else if (error
== ENOENT
)
2962 /* dataset name + 1 for the "@" + the new snapshot name must fit */
2963 if (dsl_dir_namelen(hds
->ds_dir
) + 1 +
2964 strlen(ddrsa
->ddrsa_newsnapname
) >= ZFS_MAX_DATASET_NAME_LEN
)
2965 error
= SET_ERROR(ENAMETOOLONG
);
2971 dsl_dataset_rename_snapshot_check(void *arg
, dmu_tx_t
*tx
)
2973 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
2974 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2978 error
= dsl_dataset_hold(dp
, ddrsa
->ddrsa_fsname
, FTAG
, &hds
);
2982 if (ddrsa
->ddrsa_recursive
) {
2983 error
= dmu_objset_find_dp(dp
, hds
->ds_dir
->dd_object
,
2984 dsl_dataset_rename_snapshot_check_impl
, ddrsa
,
2987 error
= dsl_dataset_rename_snapshot_check_impl(dp
, hds
, ddrsa
);
2989 dsl_dataset_rele(hds
, FTAG
);
2994 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t
*dp
,
2995 dsl_dataset_t
*hds
, void *arg
)
2997 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
3000 dmu_tx_t
*tx
= ddrsa
->ddrsa_tx
;
3003 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_oldsnapname
, &val
);
3004 ASSERT(error
== 0 || error
== ENOENT
);
3005 if (error
== ENOENT
) {
3006 /* ignore nonexistent snapshots */
3010 VERIFY0(dsl_dataset_hold_obj(dp
, val
, FTAG
, &ds
));
3012 /* log before we change the name */
3013 spa_history_log_internal_ds(ds
, "rename", tx
,
3014 "-> @%s", ddrsa
->ddrsa_newsnapname
);
3016 VERIFY0(dsl_dataset_snap_remove(hds
, ddrsa
->ddrsa_oldsnapname
, tx
,
3018 mutex_enter(&ds
->ds_lock
);
3019 (void) strlcpy(ds
->ds_snapname
, ddrsa
->ddrsa_newsnapname
,
3020 sizeof (ds
->ds_snapname
));
3021 mutex_exit(&ds
->ds_lock
);
3022 VERIFY0(zap_add(dp
->dp_meta_objset
,
3023 dsl_dataset_phys(hds
)->ds_snapnames_zapobj
,
3024 ds
->ds_snapname
, 8, 1, &ds
->ds_object
, tx
));
3025 zvol_rename_minors(dp
->dp_spa
, ddrsa
->ddrsa_oldsnapname
,
3026 ddrsa
->ddrsa_newsnapname
, B_TRUE
);
3028 dsl_dataset_rele(ds
, FTAG
);
3033 dsl_dataset_rename_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
3035 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
3036 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3037 dsl_dataset_t
*hds
= NULL
;
3039 VERIFY0(dsl_dataset_hold(dp
, ddrsa
->ddrsa_fsname
, FTAG
, &hds
));
3040 ddrsa
->ddrsa_tx
= tx
;
3041 if (ddrsa
->ddrsa_recursive
) {
3042 VERIFY0(dmu_objset_find_dp(dp
, hds
->ds_dir
->dd_object
,
3043 dsl_dataset_rename_snapshot_sync_impl
, ddrsa
,
3046 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp
, hds
, ddrsa
));
3048 dsl_dataset_rele(hds
, FTAG
);
3052 dsl_dataset_rename_snapshot(const char *fsname
,
3053 const char *oldsnapname
, const char *newsnapname
, boolean_t recursive
)
3055 dsl_dataset_rename_snapshot_arg_t ddrsa
;
3057 ddrsa
.ddrsa_fsname
= fsname
;
3058 ddrsa
.ddrsa_oldsnapname
= oldsnapname
;
3059 ddrsa
.ddrsa_newsnapname
= newsnapname
;
3060 ddrsa
.ddrsa_recursive
= recursive
;
3062 return (dsl_sync_task(fsname
, dsl_dataset_rename_snapshot_check
,
3063 dsl_dataset_rename_snapshot_sync
, &ddrsa
,
3064 1, ZFS_SPACE_CHECK_RESERVED
));
3068 * If we're doing an ownership handoff, we need to make sure that there is
3069 * only one long hold on the dataset. We're not allowed to change anything here
3070 * so we don't permanently release the long hold or regular hold here. We want
3071 * to do this only when syncing to avoid the dataset unexpectedly going away
3072 * when we release the long hold.
3075 dsl_dataset_handoff_check(dsl_dataset_t
*ds
, void *owner
, dmu_tx_t
*tx
)
3077 boolean_t held
= B_FALSE
;
3079 if (!dmu_tx_is_syncing(tx
))
3082 dsl_dir_t
*dd
= ds
->ds_dir
;
3083 mutex_enter(&dd
->dd_activity_lock
);
3084 uint64_t holds
= zfs_refcount_count(&ds
->ds_longholds
) -
3085 (owner
!= NULL
? 1 : 0);
3087 * The value of dd_activity_waiters can chance as soon as we drop the
3088 * lock, but we're fine with that; new waiters coming in or old
3089 * waiters leaving doesn't cause problems, since we're going to cancel
3090 * waiters later anyway. The goal of this check is to verify that no
3091 * non-waiters have long-holds, and all new long-holds will be
3092 * prevented because we're holding the pool config as writer.
3094 if (holds
!= dd
->dd_activity_waiters
)
3096 mutex_exit(&dd
->dd_activity_lock
);
3099 return (SET_ERROR(EBUSY
));
3105 dsl_dataset_rollback_check(void *arg
, dmu_tx_t
*tx
)
3107 dsl_dataset_rollback_arg_t
*ddra
= arg
;
3108 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3110 int64_t unused_refres_delta
;
3113 error
= dsl_dataset_hold(dp
, ddra
->ddra_fsname
, FTAG
, &ds
);
3117 /* must not be a snapshot */
3118 if (ds
->ds_is_snapshot
) {
3119 dsl_dataset_rele(ds
, FTAG
);
3120 return (SET_ERROR(EINVAL
));
3123 /* must have a most recent snapshot */
3124 if (dsl_dataset_phys(ds
)->ds_prev_snap_txg
< TXG_INITIAL
) {
3125 dsl_dataset_rele(ds
, FTAG
);
3126 return (SET_ERROR(ESRCH
));
3130 * No rollback to a snapshot created in the current txg, because
3131 * the rollback may dirty the dataset and create blocks that are
3132 * not reachable from the rootbp while having a birth txg that
3133 * falls into the snapshot's range.
3135 if (dmu_tx_is_syncing(tx
) &&
3136 dsl_dataset_phys(ds
)->ds_prev_snap_txg
>= tx
->tx_txg
) {
3137 dsl_dataset_rele(ds
, FTAG
);
3138 return (SET_ERROR(EAGAIN
));
3142 * If the expected target snapshot is specified, then check that
3143 * the latest snapshot is it.
3145 if (ddra
->ddra_tosnap
!= NULL
) {
3146 dsl_dataset_t
*snapds
;
3148 /* Check if the target snapshot exists at all. */
3149 error
= dsl_dataset_hold(dp
, ddra
->ddra_tosnap
, FTAG
, &snapds
);
3152 * ESRCH is used to signal that the target snapshot does
3153 * not exist, while ENOENT is used to report that
3154 * the rolled back dataset does not exist.
3155 * ESRCH is also used to cover other cases where the
3156 * target snapshot is not related to the dataset being
3157 * rolled back such as being in a different pool.
3159 if (error
== ENOENT
|| error
== EXDEV
)
3160 error
= SET_ERROR(ESRCH
);
3161 dsl_dataset_rele(ds
, FTAG
);
3164 ASSERT(snapds
->ds_is_snapshot
);
3166 /* Check if the snapshot is the latest snapshot indeed. */
3167 if (snapds
!= ds
->ds_prev
) {
3169 * Distinguish between the case where the only problem
3170 * is intervening snapshots (EEXIST) vs the snapshot
3171 * not being a valid target for rollback (ESRCH).
3173 if (snapds
->ds_dir
== ds
->ds_dir
||
3174 (dsl_dir_is_clone(ds
->ds_dir
) &&
3175 dsl_dir_phys(ds
->ds_dir
)->dd_origin_obj
==
3176 snapds
->ds_object
)) {
3177 error
= SET_ERROR(EEXIST
);
3179 error
= SET_ERROR(ESRCH
);
3181 dsl_dataset_rele(snapds
, FTAG
);
3182 dsl_dataset_rele(ds
, FTAG
);
3185 dsl_dataset_rele(snapds
, FTAG
);
3188 /* must not have any bookmarks after the most recent snapshot */
3189 if (dsl_bookmark_latest_txg(ds
) >
3190 dsl_dataset_phys(ds
)->ds_prev_snap_txg
) {
3191 dsl_dataset_rele(ds
, FTAG
);
3192 return (SET_ERROR(EEXIST
));
3195 error
= dsl_dataset_handoff_check(ds
, ddra
->ddra_owner
, tx
);
3197 dsl_dataset_rele(ds
, FTAG
);
3202 * Check if the snap we are rolling back to uses more than
3205 if (ds
->ds_quota
!= 0 &&
3206 dsl_dataset_phys(ds
->ds_prev
)->ds_referenced_bytes
> ds
->ds_quota
) {
3207 dsl_dataset_rele(ds
, FTAG
);
3208 return (SET_ERROR(EDQUOT
));
3212 * When we do the clone swap, we will temporarily use more space
3213 * due to the refreservation (the head will no longer have any
3214 * unique space, so the entire amount of the refreservation will need
3215 * to be free). We will immediately destroy the clone, freeing
3216 * this space, but the freeing happens over many txg's.
3218 unused_refres_delta
= (int64_t)MIN(ds
->ds_reserved
,
3219 dsl_dataset_phys(ds
)->ds_unique_bytes
);
3221 if (unused_refres_delta
> 0 &&
3222 unused_refres_delta
>
3223 dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
)) {
3224 dsl_dataset_rele(ds
, FTAG
);
3225 return (SET_ERROR(ENOSPC
));
3228 dsl_dataset_rele(ds
, FTAG
);
3233 dsl_dataset_rollback_sync(void *arg
, dmu_tx_t
*tx
)
3235 dsl_dataset_rollback_arg_t
*ddra
= arg
;
3236 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3237 dsl_dataset_t
*ds
, *clone
;
3239 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
3241 VERIFY0(dsl_dataset_hold(dp
, ddra
->ddra_fsname
, FTAG
, &ds
));
3243 dsl_dataset_name(ds
->ds_prev
, namebuf
);
3244 fnvlist_add_string(ddra
->ddra_result
, "target", namebuf
);
3246 cloneobj
= dsl_dataset_create_sync(ds
->ds_dir
, "%rollback",
3247 ds
->ds_prev
, DS_CREATE_FLAG_NODIRTY
, kcred
, NULL
, tx
);
3249 VERIFY0(dsl_dataset_hold_obj(dp
, cloneobj
, FTAG
, &clone
));
3251 dsl_dataset_clone_swap_sync_impl(clone
, ds
, tx
);
3252 dsl_dataset_zero_zil(ds
, tx
);
3254 dsl_destroy_head_sync_impl(clone
, tx
);
3256 dsl_dataset_rele(clone
, FTAG
);
3257 dsl_dataset_rele(ds
, FTAG
);
3261 * Rolls back the given filesystem or volume to the most recent snapshot.
3262 * The name of the most recent snapshot will be returned under key "target"
3263 * in the result nvlist.
3266 * - The existing dataset MUST be owned by the specified owner at entry
3267 * - Upon return, dataset will still be held by the same owner, whether we
3270 * This mode is required any time the existing filesystem is mounted. See
3271 * notes above zfs_suspend_fs() for further details.
3274 dsl_dataset_rollback(const char *fsname
, const char *tosnap
, void *owner
,
3277 dsl_dataset_rollback_arg_t ddra
;
3279 ddra
.ddra_fsname
= fsname
;
3280 ddra
.ddra_tosnap
= tosnap
;
3281 ddra
.ddra_owner
= owner
;
3282 ddra
.ddra_result
= result
;
3284 return (dsl_sync_task(fsname
, dsl_dataset_rollback_check
,
3285 dsl_dataset_rollback_sync
, &ddra
,
3286 1, ZFS_SPACE_CHECK_RESERVED
));
3289 struct promotenode
{
3294 static int snaplist_space(list_t
*l
, uint64_t mintxg
, uint64_t *spacep
);
3295 static int promote_hold(dsl_dataset_promote_arg_t
*ddpa
, dsl_pool_t
*dp
,
3297 static void promote_rele(dsl_dataset_promote_arg_t
*ddpa
, const void *tag
);
3300 dsl_dataset_promote_check(void *arg
, dmu_tx_t
*tx
)
3302 dsl_dataset_promote_arg_t
*ddpa
= arg
;
3303 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3305 struct promotenode
*snap
;
3309 size_t max_snap_len
;
3310 boolean_t conflicting_snaps
;
3312 err
= promote_hold(ddpa
, dp
, FTAG
);
3316 hds
= ddpa
->ddpa_clone
;
3317 max_snap_len
= MAXNAMELEN
- strlen(ddpa
->ddpa_clonename
) - 1;
3319 if (dsl_dataset_phys(hds
)->ds_flags
& DS_FLAG_NOPROMOTE
) {
3320 promote_rele(ddpa
, FTAG
);
3321 return (SET_ERROR(EXDEV
));
3324 snap
= list_head(&ddpa
->shared_snaps
);
3326 err
= SET_ERROR(ENOENT
);
3329 dsl_dataset_t
*const origin_ds
= snap
->ds
;
3332 * Encrypted clones share a DSL Crypto Key with their origin's dsl dir.
3333 * When doing a promote we must make sure the encryption root for
3334 * both the target and the target's origin does not change to avoid
3335 * needing to rewrap encryption keys
3337 err
= dsl_dataset_promote_crypt_check(hds
->ds_dir
, origin_ds
->ds_dir
);
3342 * Compute and check the amount of space to transfer. Since this is
3343 * so expensive, don't do the preliminary check.
3345 if (!dmu_tx_is_syncing(tx
)) {
3346 promote_rele(ddpa
, FTAG
);
3350 /* compute origin's new unique space */
3351 snap
= list_tail(&ddpa
->clone_snaps
);
3352 ASSERT(snap
!= NULL
);
3353 ASSERT3U(dsl_dataset_phys(snap
->ds
)->ds_prev_snap_obj
, ==,
3354 origin_ds
->ds_object
);
3355 dsl_deadlist_space_range(&snap
->ds
->ds_deadlist
,
3356 dsl_dataset_phys(origin_ds
)->ds_prev_snap_txg
, UINT64_MAX
,
3357 &ddpa
->unique
, &unused
, &unused
);
3360 * Walk the snapshots that we are moving
3362 * Compute space to transfer. Consider the incremental changes
3363 * to used by each snapshot:
3364 * (my used) = (prev's used) + (blocks born) - (blocks killed)
3365 * So each snapshot gave birth to:
3366 * (blocks born) = (my used) - (prev's used) + (blocks killed)
3367 * So a sequence would look like:
3368 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
3369 * Which simplifies to:
3370 * uN + kN + kN-1 + ... + k1 + k0
3371 * Note however, if we stop before we reach the ORIGIN we get:
3372 * uN + kN + kN-1 + ... + kM - uM-1
3374 conflicting_snaps
= B_FALSE
;
3376 ddpa
->used
= dsl_dataset_phys(origin_ds
)->ds_referenced_bytes
;
3377 ddpa
->comp
= dsl_dataset_phys(origin_ds
)->ds_compressed_bytes
;
3378 ddpa
->uncomp
= dsl_dataset_phys(origin_ds
)->ds_uncompressed_bytes
;
3379 for (snap
= list_head(&ddpa
->shared_snaps
); snap
;
3380 snap
= list_next(&ddpa
->shared_snaps
, snap
)) {
3381 uint64_t val
, dlused
, dlcomp
, dluncomp
;
3382 dsl_dataset_t
*ds
= snap
->ds
;
3387 * If there are long holds, we won't be able to evict
3390 if (dsl_dataset_long_held(ds
)) {
3391 err
= SET_ERROR(EBUSY
);
3395 /* Check that the snapshot name does not conflict */
3396 VERIFY0(dsl_dataset_get_snapname(ds
));
3397 if (strlen(ds
->ds_snapname
) >= max_snap_len
) {
3398 err
= SET_ERROR(ENAMETOOLONG
);
3401 err
= dsl_dataset_snap_lookup(hds
, ds
->ds_snapname
, &val
);
3403 fnvlist_add_boolean(ddpa
->err_ds
,
3404 snap
->ds
->ds_snapname
);
3405 conflicting_snaps
= B_TRUE
;
3406 } else if (err
!= ENOENT
) {
3410 /* The very first snapshot does not have a deadlist */
3411 if (dsl_dataset_phys(ds
)->ds_prev_snap_obj
== 0)
3414 dsl_deadlist_space(&ds
->ds_deadlist
,
3415 &dlused
, &dlcomp
, &dluncomp
);
3416 ddpa
->used
+= dlused
;
3417 ddpa
->comp
+= dlcomp
;
3418 ddpa
->uncomp
+= dluncomp
;
3422 * Check that bookmarks that are being transferred don't have
3425 for (dsl_bookmark_node_t
*dbn
= avl_first(&origin_ds
->ds_bookmarks
);
3426 dbn
!= NULL
&& dbn
->dbn_phys
.zbm_creation_txg
<=
3427 dsl_dataset_phys(origin_ds
)->ds_creation_txg
;
3428 dbn
= AVL_NEXT(&origin_ds
->ds_bookmarks
, dbn
)) {
3429 if (strlen(dbn
->dbn_name
) >= max_snap_len
) {
3430 err
= SET_ERROR(ENAMETOOLONG
);
3433 zfs_bookmark_phys_t bm
;
3434 err
= dsl_bookmark_lookup_impl(ddpa
->ddpa_clone
,
3435 dbn
->dbn_name
, &bm
);
3438 fnvlist_add_boolean(ddpa
->err_ds
, dbn
->dbn_name
);
3439 conflicting_snaps
= B_TRUE
;
3440 } else if (err
== ESRCH
) {
3449 * In order to return the full list of conflicting snapshots, we check
3450 * whether there was a conflict after traversing all of them.
3452 if (conflicting_snaps
) {
3453 err
= SET_ERROR(EEXIST
);
3458 * If we are a clone of a clone then we never reached ORIGIN,
3459 * so we need to subtract out the clone origin's used space.
3461 if (ddpa
->origin_origin
) {
3463 dsl_dataset_phys(ddpa
->origin_origin
)->ds_referenced_bytes
;
3465 dsl_dataset_phys(ddpa
->origin_origin
)->ds_compressed_bytes
;
3467 dsl_dataset_phys(ddpa
->origin_origin
)->
3468 ds_uncompressed_bytes
;
3471 /* Check that there is enough space and limit headroom here */
3472 err
= dsl_dir_transfer_possible(origin_ds
->ds_dir
, hds
->ds_dir
,
3473 0, ss_mv_cnt
, ddpa
->used
, ddpa
->cr
, ddpa
->proc
);
3478 * Compute the amounts of space that will be used by snapshots
3479 * after the promotion (for both origin and clone). For each,
3480 * it is the amount of space that will be on all of their
3481 * deadlists (that was not born before their new origin).
3483 if (dsl_dir_phys(hds
->ds_dir
)->dd_flags
& DD_FLAG_USED_BREAKDOWN
) {
3487 * Note, typically this will not be a clone of a clone,
3488 * so dd_origin_txg will be < TXG_INITIAL, so
3489 * these snaplist_space() -> dsl_deadlist_space_range()
3490 * calls will be fast because they do not have to
3491 * iterate over all bps.
3493 snap
= list_head(&ddpa
->origin_snaps
);
3495 err
= SET_ERROR(ENOENT
);
3498 err
= snaplist_space(&ddpa
->shared_snaps
,
3499 snap
->ds
->ds_dir
->dd_origin_txg
, &ddpa
->cloneusedsnap
);
3503 err
= snaplist_space(&ddpa
->clone_snaps
,
3504 snap
->ds
->ds_dir
->dd_origin_txg
, &space
);
3507 ddpa
->cloneusedsnap
+= space
;
3509 if (dsl_dir_phys(origin_ds
->ds_dir
)->dd_flags
&
3510 DD_FLAG_USED_BREAKDOWN
) {
3511 err
= snaplist_space(&ddpa
->origin_snaps
,
3512 dsl_dataset_phys(origin_ds
)->ds_creation_txg
,
3513 &ddpa
->originusedsnap
);
3519 promote_rele(ddpa
, FTAG
);
3524 dsl_dataset_promote_sync(void *arg
, dmu_tx_t
*tx
)
3526 dsl_dataset_promote_arg_t
*ddpa
= arg
;
3527 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
3529 struct promotenode
*snap
;
3530 dsl_dataset_t
*origin_ds
;
3531 dsl_dataset_t
*origin_head
;
3533 dsl_dir_t
*odd
= NULL
;
3534 uint64_t oldnext_obj
;
3537 ASSERT(nvlist_empty(ddpa
->err_ds
));
3539 VERIFY0(promote_hold(ddpa
, dp
, FTAG
));
3540 hds
= ddpa
->ddpa_clone
;
3542 ASSERT0(dsl_dataset_phys(hds
)->ds_flags
& DS_FLAG_NOPROMOTE
);
3544 snap
= list_head(&ddpa
->shared_snaps
);
3545 origin_ds
= snap
->ds
;
3548 snap
= list_head(&ddpa
->origin_snaps
);
3549 origin_head
= snap
->ds
;
3552 * We need to explicitly open odd, since origin_ds's dd will be
3555 VERIFY0(dsl_dir_hold_obj(dp
, origin_ds
->ds_dir
->dd_object
,
3558 dsl_dataset_promote_crypt_sync(hds
->ds_dir
, odd
, tx
);
3560 /* change origin's next snap */
3561 dmu_buf_will_dirty(origin_ds
->ds_dbuf
, tx
);
3562 oldnext_obj
= dsl_dataset_phys(origin_ds
)->ds_next_snap_obj
;
3563 snap
= list_tail(&ddpa
->clone_snaps
);
3564 ASSERT3U(dsl_dataset_phys(snap
->ds
)->ds_prev_snap_obj
, ==,
3565 origin_ds
->ds_object
);
3566 dsl_dataset_phys(origin_ds
)->ds_next_snap_obj
= snap
->ds
->ds_object
;
3568 /* change the origin's next clone */
3569 if (dsl_dataset_phys(origin_ds
)->ds_next_clones_obj
) {
3570 dsl_dataset_remove_from_next_clones(origin_ds
,
3571 snap
->ds
->ds_object
, tx
);
3572 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
3573 dsl_dataset_phys(origin_ds
)->ds_next_clones_obj
,
3578 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
3579 ASSERT3U(dsl_dir_phys(dd
)->dd_origin_obj
, ==, origin_ds
->ds_object
);
3580 dsl_dir_phys(dd
)->dd_origin_obj
= dsl_dir_phys(odd
)->dd_origin_obj
;
3581 dd
->dd_origin_txg
= origin_head
->ds_dir
->dd_origin_txg
;
3582 dmu_buf_will_dirty(odd
->dd_dbuf
, tx
);
3583 dsl_dir_phys(odd
)->dd_origin_obj
= origin_ds
->ds_object
;
3584 origin_head
->ds_dir
->dd_origin_txg
=
3585 dsl_dataset_phys(origin_ds
)->ds_creation_txg
;
3587 /* change dd_clone entries */
3588 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
3589 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
3590 dsl_dir_phys(odd
)->dd_clones
, hds
->ds_object
, tx
));
3591 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
3592 dsl_dir_phys(ddpa
->origin_origin
->ds_dir
)->dd_clones
,
3593 hds
->ds_object
, tx
));
3595 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
3596 dsl_dir_phys(ddpa
->origin_origin
->ds_dir
)->dd_clones
,
3597 origin_head
->ds_object
, tx
));
3598 if (dsl_dir_phys(dd
)->dd_clones
== 0) {
3599 dsl_dir_phys(dd
)->dd_clones
=
3600 zap_create(dp
->dp_meta_objset
, DMU_OT_DSL_CLONES
,
3601 DMU_OT_NONE
, 0, tx
);
3603 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
3604 dsl_dir_phys(dd
)->dd_clones
, origin_head
->ds_object
, tx
));
3608 * Move bookmarks to this dir.
3610 dsl_bookmark_node_t
*dbn_next
;
3611 for (dsl_bookmark_node_t
*dbn
= avl_first(&origin_head
->ds_bookmarks
);
3612 dbn
!= NULL
&& dbn
->dbn_phys
.zbm_creation_txg
<=
3613 dsl_dataset_phys(origin_ds
)->ds_creation_txg
;
3615 dbn_next
= AVL_NEXT(&origin_head
->ds_bookmarks
, dbn
);
3617 avl_remove(&origin_head
->ds_bookmarks
, dbn
);
3618 VERIFY0(zap_remove(dp
->dp_meta_objset
,
3619 origin_head
->ds_bookmarks_obj
, dbn
->dbn_name
, tx
));
3621 dsl_bookmark_node_add(hds
, dbn
, tx
);
3624 dsl_bookmark_next_changed(hds
, origin_ds
, tx
);
3626 /* move snapshots to this dir */
3627 for (snap
= list_head(&ddpa
->shared_snaps
); snap
;
3628 snap
= list_next(&ddpa
->shared_snaps
, snap
)) {
3629 dsl_dataset_t
*ds
= snap
->ds
;
3632 * Property callbacks are registered to a particular
3633 * dsl_dir. Since ours is changing, evict the objset
3634 * so that they will be unregistered from the old dsl_dir.
3636 if (ds
->ds_objset
) {
3637 dmu_objset_evict(ds
->ds_objset
);
3638 ds
->ds_objset
= NULL
;
3641 /* move snap name entry */
3642 VERIFY0(dsl_dataset_get_snapname(ds
));
3643 VERIFY0(dsl_dataset_snap_remove(origin_head
,
3644 ds
->ds_snapname
, tx
, B_TRUE
));
3645 VERIFY0(zap_add(dp
->dp_meta_objset
,
3646 dsl_dataset_phys(hds
)->ds_snapnames_zapobj
, ds
->ds_snapname
,
3647 8, 1, &ds
->ds_object
, tx
));
3648 dsl_fs_ss_count_adjust(hds
->ds_dir
, 1,
3649 DD_FIELD_SNAPSHOT_COUNT
, tx
);
3651 /* change containing dsl_dir */
3652 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
3653 ASSERT3U(dsl_dataset_phys(ds
)->ds_dir_obj
, ==, odd
->dd_object
);
3654 dsl_dataset_phys(ds
)->ds_dir_obj
= dd
->dd_object
;
3655 ASSERT3P(ds
->ds_dir
, ==, odd
);
3656 dsl_dir_rele(ds
->ds_dir
, ds
);
3657 VERIFY0(dsl_dir_hold_obj(dp
, dd
->dd_object
,
3658 NULL
, ds
, &ds
->ds_dir
));
3660 /* move any clone references */
3661 if (dsl_dataset_phys(ds
)->ds_next_clones_obj
&&
3662 spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
3666 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
3667 dsl_dataset_phys(ds
)->ds_next_clones_obj
);
3668 zap_cursor_retrieve(&zc
, &za
) == 0;
3669 zap_cursor_advance(&zc
)) {
3670 dsl_dataset_t
*cnds
;
3673 if (za
.za_first_integer
== oldnext_obj
) {
3675 * We've already moved the
3676 * origin's reference.
3681 VERIFY0(dsl_dataset_hold_obj(dp
,
3682 za
.za_first_integer
, FTAG
, &cnds
));
3683 o
= dsl_dir_phys(cnds
->ds_dir
)->
3684 dd_head_dataset_obj
;
3686 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
3687 dsl_dir_phys(odd
)->dd_clones
, o
, tx
));
3688 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
3689 dsl_dir_phys(dd
)->dd_clones
, o
, tx
));
3690 dsl_dataset_rele(cnds
, FTAG
);
3692 zap_cursor_fini(&zc
);
3695 ASSERT(!dsl_prop_hascb(ds
));
3699 * Change space accounting.
3700 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
3701 * both be valid, or both be 0 (resulting in delta == 0). This
3702 * is true for each of {clone,origin} independently.
3705 delta
= ddpa
->cloneusedsnap
-
3706 dsl_dir_phys(dd
)->dd_used_breakdown
[DD_USED_SNAP
];
3707 ASSERT3S(delta
, >=, 0);
3708 ASSERT3U(ddpa
->used
, >=, delta
);
3709 dsl_dir_diduse_space(dd
, DD_USED_SNAP
, delta
, 0, 0, tx
);
3710 dsl_dir_diduse_space(dd
, DD_USED_HEAD
,
3711 ddpa
->used
- delta
, ddpa
->comp
, ddpa
->uncomp
, tx
);
3713 delta
= ddpa
->originusedsnap
-
3714 dsl_dir_phys(odd
)->dd_used_breakdown
[DD_USED_SNAP
];
3715 ASSERT3S(delta
, <=, 0);
3716 ASSERT3U(ddpa
->used
, >=, -delta
);
3717 dsl_dir_diduse_space(odd
, DD_USED_SNAP
, delta
, 0, 0, tx
);
3718 dsl_dir_diduse_space(odd
, DD_USED_HEAD
,
3719 -ddpa
->used
- delta
, -ddpa
->comp
, -ddpa
->uncomp
, tx
);
3721 dsl_dataset_phys(origin_ds
)->ds_unique_bytes
= ddpa
->unique
;
3724 * Since livelists are specific to a clone's origin txg, they
3725 * are no longer accurate. Destroy the livelist from the clone being
3726 * promoted. If the origin dataset is a clone, destroy its livelist
3729 dsl_dir_remove_livelist(dd
, tx
, B_TRUE
);
3730 dsl_dir_remove_livelist(odd
, tx
, B_TRUE
);
3732 /* log history record */
3733 spa_history_log_internal_ds(hds
, "promote", tx
, " ");
3735 dsl_dir_rele(odd
, FTAG
);
3736 promote_rele(ddpa
, FTAG
);
3739 * Transfer common error blocks from old head to new head.
3741 if (spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_HEAD_ERRLOG
)) {
3742 uint64_t old_head
= origin_head
->ds_object
;
3743 uint64_t new_head
= hds
->ds_object
;
3744 spa_swap_errlog(dp
->dp_spa
, new_head
, old_head
, tx
);
3749 * Make a list of dsl_dataset_t's for the snapshots between first_obj
3750 * (exclusive) and last_obj (inclusive). The list will be in reverse
3751 * order (last_obj will be the list_head()). If first_obj == 0, do all
3752 * snapshots back to this dataset's origin.
3755 snaplist_make(dsl_pool_t
*dp
,
3756 uint64_t first_obj
, uint64_t last_obj
, list_t
*l
, const void *tag
)
3758 uint64_t obj
= last_obj
;
3760 list_create(l
, sizeof (struct promotenode
),
3761 offsetof(struct promotenode
, link
));
3763 while (obj
!= first_obj
) {
3765 struct promotenode
*snap
;
3768 err
= dsl_dataset_hold_obj(dp
, obj
, tag
, &ds
);
3769 ASSERT(err
!= ENOENT
);
3774 first_obj
= dsl_dir_phys(ds
->ds_dir
)->dd_origin_obj
;
3776 snap
= kmem_alloc(sizeof (*snap
), KM_SLEEP
);
3778 list_insert_tail(l
, snap
);
3779 obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
3786 snaplist_space(list_t
*l
, uint64_t mintxg
, uint64_t *spacep
)
3788 struct promotenode
*snap
;
3791 for (snap
= list_head(l
); snap
; snap
= list_next(l
, snap
)) {
3792 uint64_t used
, comp
, uncomp
;
3793 dsl_deadlist_space_range(&snap
->ds
->ds_deadlist
,
3794 mintxg
, UINT64_MAX
, &used
, &comp
, &uncomp
);
3801 snaplist_destroy(list_t
*l
, const void *tag
)
3803 struct promotenode
*snap
;
3805 if (l
== NULL
|| !list_link_active(&l
->list_head
))
3808 while ((snap
= list_tail(l
)) != NULL
) {
3809 list_remove(l
, snap
);
3810 dsl_dataset_rele(snap
->ds
, tag
);
3811 kmem_free(snap
, sizeof (*snap
));
3817 promote_hold(dsl_dataset_promote_arg_t
*ddpa
, dsl_pool_t
*dp
, const void *tag
)
3821 struct promotenode
*snap
;
3823 error
= dsl_dataset_hold(dp
, ddpa
->ddpa_clonename
, tag
,
3827 dd
= ddpa
->ddpa_clone
->ds_dir
;
3829 if (ddpa
->ddpa_clone
->ds_is_snapshot
||
3830 !dsl_dir_is_clone(dd
)) {
3831 dsl_dataset_rele(ddpa
->ddpa_clone
, tag
);
3832 return (SET_ERROR(EINVAL
));
3835 error
= snaplist_make(dp
, 0, dsl_dir_phys(dd
)->dd_origin_obj
,
3836 &ddpa
->shared_snaps
, tag
);
3840 error
= snaplist_make(dp
, 0, ddpa
->ddpa_clone
->ds_object
,
3841 &ddpa
->clone_snaps
, tag
);
3845 snap
= list_head(&ddpa
->shared_snaps
);
3846 ASSERT3U(snap
->ds
->ds_object
, ==, dsl_dir_phys(dd
)->dd_origin_obj
);
3847 error
= snaplist_make(dp
, dsl_dir_phys(dd
)->dd_origin_obj
,
3848 dsl_dir_phys(snap
->ds
->ds_dir
)->dd_head_dataset_obj
,
3849 &ddpa
->origin_snaps
, tag
);
3853 if (dsl_dir_phys(snap
->ds
->ds_dir
)->dd_origin_obj
!= 0) {
3854 error
= dsl_dataset_hold_obj(dp
,
3855 dsl_dir_phys(snap
->ds
->ds_dir
)->dd_origin_obj
,
3856 tag
, &ddpa
->origin_origin
);
3862 promote_rele(ddpa
, tag
);
3867 promote_rele(dsl_dataset_promote_arg_t
*ddpa
, const void *tag
)
3869 snaplist_destroy(&ddpa
->shared_snaps
, tag
);
3870 snaplist_destroy(&ddpa
->clone_snaps
, tag
);
3871 snaplist_destroy(&ddpa
->origin_snaps
, tag
);
3872 if (ddpa
->origin_origin
!= NULL
)
3873 dsl_dataset_rele(ddpa
->origin_origin
, tag
);
3874 dsl_dataset_rele(ddpa
->ddpa_clone
, tag
);
3880 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
3881 * in with the name. (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.)
3884 dsl_dataset_promote(const char *name
, char *conflsnap
)
3886 dsl_dataset_promote_arg_t ddpa
= { 0 };
3889 nvpair_t
*snap_pair
;
3893 * We will modify space proportional to the number of
3894 * snapshots. Compute numsnaps.
3896 error
= dmu_objset_hold(name
, FTAG
, &os
);
3899 error
= zap_count(dmu_objset_pool(os
)->dp_meta_objset
,
3900 dsl_dataset_phys(dmu_objset_ds(os
))->ds_snapnames_zapobj
,
3902 dmu_objset_rele(os
, FTAG
);
3906 ddpa
.ddpa_clonename
= name
;
3907 ddpa
.err_ds
= fnvlist_alloc();
3909 ddpa
.proc
= curproc
;
3911 error
= dsl_sync_task(name
, dsl_dataset_promote_check
,
3912 dsl_dataset_promote_sync
, &ddpa
,
3913 2 + numsnaps
, ZFS_SPACE_CHECK_RESERVED
);
3916 * Return the first conflicting snapshot found.
3918 snap_pair
= nvlist_next_nvpair(ddpa
.err_ds
, NULL
);
3919 if (snap_pair
!= NULL
&& conflsnap
!= NULL
)
3920 (void) strlcpy(conflsnap
, nvpair_name(snap_pair
),
3921 ZFS_MAX_DATASET_NAME_LEN
);
3923 fnvlist_free(ddpa
.err_ds
);
3928 dsl_dataset_clone_swap_check_impl(dsl_dataset_t
*clone
,
3929 dsl_dataset_t
*origin_head
, boolean_t force
, void *owner
, dmu_tx_t
*tx
)
3932 * "slack" factor for received datasets with refquota set on them.
3933 * See the bottom of this function for details on its use.
3935 uint64_t refquota_slack
= (uint64_t)DMU_MAX_ACCESS
*
3936 spa_asize_inflation
;
3937 int64_t unused_refres_delta
;
3939 /* they should both be heads */
3940 if (clone
->ds_is_snapshot
||
3941 origin_head
->ds_is_snapshot
)
3942 return (SET_ERROR(EINVAL
));
3944 /* if we are not forcing, the branch point should be just before them */
3945 if (!force
&& clone
->ds_prev
!= origin_head
->ds_prev
)
3946 return (SET_ERROR(EINVAL
));
3948 /* clone should be the clone (unless they are unrelated) */
3949 if (clone
->ds_prev
!= NULL
&&
3950 clone
->ds_prev
!= clone
->ds_dir
->dd_pool
->dp_origin_snap
&&
3951 origin_head
->ds_dir
!= clone
->ds_prev
->ds_dir
)
3952 return (SET_ERROR(EINVAL
));
3954 /* the clone should be a child of the origin */
3955 if (clone
->ds_dir
->dd_parent
!= origin_head
->ds_dir
)
3956 return (SET_ERROR(EINVAL
));
3958 /* origin_head shouldn't be modified unless 'force' */
3960 dsl_dataset_modified_since_snap(origin_head
, origin_head
->ds_prev
))
3961 return (SET_ERROR(ETXTBSY
));
3963 /* origin_head should have no long holds (e.g. is not mounted) */
3964 if (dsl_dataset_handoff_check(origin_head
, owner
, tx
))
3965 return (SET_ERROR(EBUSY
));
3967 /* check amount of any unconsumed refreservation */
3968 unused_refres_delta
=
3969 (int64_t)MIN(origin_head
->ds_reserved
,
3970 dsl_dataset_phys(origin_head
)->ds_unique_bytes
) -
3971 (int64_t)MIN(origin_head
->ds_reserved
,
3972 dsl_dataset_phys(clone
)->ds_unique_bytes
);
3974 if (unused_refres_delta
> 0 &&
3975 unused_refres_delta
>
3976 dsl_dir_space_available(origin_head
->ds_dir
, NULL
, 0, TRUE
))
3977 return (SET_ERROR(ENOSPC
));
3980 * The clone can't be too much over the head's refquota.
3982 * To ensure that the entire refquota can be used, we allow one
3983 * transaction to exceed the refquota. Therefore, this check
3984 * needs to also allow for the space referenced to be more than the
3985 * refquota. The maximum amount of space that one transaction can use
3986 * on disk is DMU_MAX_ACCESS * spa_asize_inflation. Allowing this
3987 * overage ensures that we are able to receive a filesystem that
3988 * exceeds the refquota on the source system.
3990 * So that overage is the refquota_slack we use below.
3992 if (origin_head
->ds_quota
!= 0 &&
3993 dsl_dataset_phys(clone
)->ds_referenced_bytes
>
3994 origin_head
->ds_quota
+ refquota_slack
)
3995 return (SET_ERROR(EDQUOT
));
4001 dsl_dataset_swap_remap_deadlists(dsl_dataset_t
*clone
,
4002 dsl_dataset_t
*origin
, dmu_tx_t
*tx
)
4004 uint64_t clone_remap_dl_obj
, origin_remap_dl_obj
;
4005 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
4007 ASSERT(dsl_pool_sync_context(dp
));
4009 clone_remap_dl_obj
= dsl_dataset_get_remap_deadlist_object(clone
);
4010 origin_remap_dl_obj
= dsl_dataset_get_remap_deadlist_object(origin
);
4012 if (clone_remap_dl_obj
!= 0) {
4013 dsl_deadlist_close(&clone
->ds_remap_deadlist
);
4014 dsl_dataset_unset_remap_deadlist_object(clone
, tx
);
4016 if (origin_remap_dl_obj
!= 0) {
4017 dsl_deadlist_close(&origin
->ds_remap_deadlist
);
4018 dsl_dataset_unset_remap_deadlist_object(origin
, tx
);
4021 if (clone_remap_dl_obj
!= 0) {
4022 dsl_dataset_set_remap_deadlist_object(origin
,
4023 clone_remap_dl_obj
, tx
);
4024 dsl_deadlist_open(&origin
->ds_remap_deadlist
,
4025 dp
->dp_meta_objset
, clone_remap_dl_obj
);
4027 if (origin_remap_dl_obj
!= 0) {
4028 dsl_dataset_set_remap_deadlist_object(clone
,
4029 origin_remap_dl_obj
, tx
);
4030 dsl_deadlist_open(&clone
->ds_remap_deadlist
,
4031 dp
->dp_meta_objset
, origin_remap_dl_obj
);
4036 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t
*clone
,
4037 dsl_dataset_t
*origin_head
, dmu_tx_t
*tx
)
4039 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
4040 int64_t unused_refres_delta
;
4042 ASSERT(clone
->ds_reserved
== 0);
4044 * NOTE: On DEBUG kernels there could be a race between this and
4045 * the check function if spa_asize_inflation is adjusted...
4047 ASSERT(origin_head
->ds_quota
== 0 ||
4048 dsl_dataset_phys(clone
)->ds_unique_bytes
<= origin_head
->ds_quota
+
4049 DMU_MAX_ACCESS
* spa_asize_inflation
);
4050 ASSERT3P(clone
->ds_prev
, ==, origin_head
->ds_prev
);
4052 dsl_dir_cancel_waiters(origin_head
->ds_dir
);
4055 * Swap per-dataset feature flags.
4057 for (spa_feature_t f
= 0; f
< SPA_FEATURES
; f
++) {
4058 if (!(spa_feature_table
[f
].fi_flags
&
4059 ZFEATURE_FLAG_PER_DATASET
)) {
4060 ASSERT(!dsl_dataset_feature_is_active(clone
, f
));
4061 ASSERT(!dsl_dataset_feature_is_active(origin_head
, f
));
4065 boolean_t clone_inuse
= dsl_dataset_feature_is_active(clone
, f
);
4066 void *clone_feature
= clone
->ds_feature
[f
];
4067 boolean_t origin_head_inuse
=
4068 dsl_dataset_feature_is_active(origin_head
, f
);
4069 void *origin_head_feature
= origin_head
->ds_feature
[f
];
4072 dsl_dataset_deactivate_feature_impl(clone
, f
, tx
);
4073 if (origin_head_inuse
)
4074 dsl_dataset_deactivate_feature_impl(origin_head
, f
, tx
);
4077 dsl_dataset_activate_feature(origin_head
->ds_object
, f
,
4079 origin_head
->ds_feature
[f
] = clone_feature
;
4081 if (origin_head_inuse
) {
4082 dsl_dataset_activate_feature(clone
->ds_object
, f
,
4083 origin_head_feature
, tx
);
4084 clone
->ds_feature
[f
] = origin_head_feature
;
4088 dmu_buf_will_dirty(clone
->ds_dbuf
, tx
);
4089 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
4091 if (clone
->ds_objset
!= NULL
) {
4092 dmu_objset_evict(clone
->ds_objset
);
4093 clone
->ds_objset
= NULL
;
4096 if (origin_head
->ds_objset
!= NULL
) {
4097 dmu_objset_evict(origin_head
->ds_objset
);
4098 origin_head
->ds_objset
= NULL
;
4101 unused_refres_delta
=
4102 (int64_t)MIN(origin_head
->ds_reserved
,
4103 dsl_dataset_phys(origin_head
)->ds_unique_bytes
) -
4104 (int64_t)MIN(origin_head
->ds_reserved
,
4105 dsl_dataset_phys(clone
)->ds_unique_bytes
);
4108 * Reset origin's unique bytes.
4111 dsl_dataset_t
*origin
= clone
->ds_prev
;
4112 uint64_t comp
, uncomp
;
4114 dmu_buf_will_dirty(origin
->ds_dbuf
, tx
);
4115 dsl_deadlist_space_range(&clone
->ds_deadlist
,
4116 dsl_dataset_phys(origin
)->ds_prev_snap_txg
, UINT64_MAX
,
4117 &dsl_dataset_phys(origin
)->ds_unique_bytes
, &comp
, &uncomp
);
4122 rrw_enter(&clone
->ds_bp_rwlock
, RW_WRITER
, FTAG
);
4123 rrw_enter(&origin_head
->ds_bp_rwlock
, RW_WRITER
, FTAG
);
4125 tmp
= dsl_dataset_phys(origin_head
)->ds_bp
;
4126 dsl_dataset_phys(origin_head
)->ds_bp
=
4127 dsl_dataset_phys(clone
)->ds_bp
;
4128 dsl_dataset_phys(clone
)->ds_bp
= tmp
;
4129 rrw_exit(&origin_head
->ds_bp_rwlock
, FTAG
);
4130 rrw_exit(&clone
->ds_bp_rwlock
, FTAG
);
4133 /* set dd_*_bytes */
4135 int64_t dused
, dcomp
, duncomp
;
4136 uint64_t cdl_used
, cdl_comp
, cdl_uncomp
;
4137 uint64_t odl_used
, odl_comp
, odl_uncomp
;
4139 ASSERT3U(dsl_dir_phys(clone
->ds_dir
)->
4140 dd_used_breakdown
[DD_USED_SNAP
], ==, 0);
4142 dsl_deadlist_space(&clone
->ds_deadlist
,
4143 &cdl_used
, &cdl_comp
, &cdl_uncomp
);
4144 dsl_deadlist_space(&origin_head
->ds_deadlist
,
4145 &odl_used
, &odl_comp
, &odl_uncomp
);
4147 dused
= dsl_dataset_phys(clone
)->ds_referenced_bytes
+
4149 (dsl_dataset_phys(origin_head
)->ds_referenced_bytes
+
4151 dcomp
= dsl_dataset_phys(clone
)->ds_compressed_bytes
+
4153 (dsl_dataset_phys(origin_head
)->ds_compressed_bytes
+
4155 duncomp
= dsl_dataset_phys(clone
)->ds_uncompressed_bytes
+
4157 (dsl_dataset_phys(origin_head
)->ds_uncompressed_bytes
+
4160 dsl_dir_diduse_space(origin_head
->ds_dir
, DD_USED_HEAD
,
4161 dused
, dcomp
, duncomp
, tx
);
4162 dsl_dir_diduse_space(clone
->ds_dir
, DD_USED_HEAD
,
4163 -dused
, -dcomp
, -duncomp
, tx
);
4166 * The difference in the space used by snapshots is the
4167 * difference in snapshot space due to the head's
4168 * deadlist (since that's the only thing that's
4169 * changing that affects the snapused).
4171 dsl_deadlist_space_range(&clone
->ds_deadlist
,
4172 origin_head
->ds_dir
->dd_origin_txg
, UINT64_MAX
,
4173 &cdl_used
, &cdl_comp
, &cdl_uncomp
);
4174 dsl_deadlist_space_range(&origin_head
->ds_deadlist
,
4175 origin_head
->ds_dir
->dd_origin_txg
, UINT64_MAX
,
4176 &odl_used
, &odl_comp
, &odl_uncomp
);
4177 dsl_dir_transfer_space(origin_head
->ds_dir
, cdl_used
- odl_used
,
4178 DD_USED_HEAD
, DD_USED_SNAP
, tx
);
4181 /* swap ds_*_bytes */
4182 SWITCH64(dsl_dataset_phys(origin_head
)->ds_referenced_bytes
,
4183 dsl_dataset_phys(clone
)->ds_referenced_bytes
);
4184 SWITCH64(dsl_dataset_phys(origin_head
)->ds_compressed_bytes
,
4185 dsl_dataset_phys(clone
)->ds_compressed_bytes
);
4186 SWITCH64(dsl_dataset_phys(origin_head
)->ds_uncompressed_bytes
,
4187 dsl_dataset_phys(clone
)->ds_uncompressed_bytes
);
4188 SWITCH64(dsl_dataset_phys(origin_head
)->ds_unique_bytes
,
4189 dsl_dataset_phys(clone
)->ds_unique_bytes
);
4191 /* apply any parent delta for change in unconsumed refreservation */
4192 dsl_dir_diduse_space(origin_head
->ds_dir
, DD_USED_REFRSRV
,
4193 unused_refres_delta
, 0, 0, tx
);
4198 dsl_deadlist_close(&clone
->ds_deadlist
);
4199 dsl_deadlist_close(&origin_head
->ds_deadlist
);
4200 SWITCH64(dsl_dataset_phys(origin_head
)->ds_deadlist_obj
,
4201 dsl_dataset_phys(clone
)->ds_deadlist_obj
);
4202 dsl_deadlist_open(&clone
->ds_deadlist
, dp
->dp_meta_objset
,
4203 dsl_dataset_phys(clone
)->ds_deadlist_obj
);
4204 dsl_deadlist_open(&origin_head
->ds_deadlist
, dp
->dp_meta_objset
,
4205 dsl_dataset_phys(origin_head
)->ds_deadlist_obj
);
4206 dsl_dataset_swap_remap_deadlists(clone
, origin_head
, tx
);
4209 * If there is a bookmark at the origin, its "next dataset" is
4210 * changing, so we need to reset its FBN.
4212 dsl_bookmark_next_changed(origin_head
, origin_head
->ds_prev
, tx
);
4214 dsl_scan_ds_clone_swapped(origin_head
, clone
, tx
);
4217 * Destroy any livelists associated with the clone or the origin,
4218 * since after the swap the corresponding livelists are no longer
4221 dsl_dir_remove_livelist(clone
->ds_dir
, tx
, B_TRUE
);
4222 dsl_dir_remove_livelist(origin_head
->ds_dir
, tx
, B_TRUE
);
4224 spa_history_log_internal_ds(clone
, "clone swap", tx
,
4225 "parent=%s", origin_head
->ds_dir
->dd_myname
);
4229 * Given a pool name and a dataset object number in that pool,
4230 * return the name of that dataset.
4233 dsl_dsobj_to_dsname(char *pname
, uint64_t obj
, char *buf
)
4239 error
= dsl_pool_hold(pname
, FTAG
, &dp
);
4243 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
, &ds
);
4245 dsl_dataset_name(ds
, buf
);
4246 dsl_dataset_rele(ds
, FTAG
);
4248 dsl_pool_rele(dp
, FTAG
);
4254 dsl_dataset_check_quota(dsl_dataset_t
*ds
, boolean_t check_quota
,
4255 uint64_t asize
, uint64_t inflight
, uint64_t *used
, uint64_t *ref_rsrv
)
4259 ASSERT3S(asize
, >, 0);
4262 * *ref_rsrv is the portion of asize that will come from any
4263 * unconsumed refreservation space.
4267 mutex_enter(&ds
->ds_lock
);
4269 * Make a space adjustment for reserved bytes.
4271 if (ds
->ds_reserved
> dsl_dataset_phys(ds
)->ds_unique_bytes
) {
4273 ds
->ds_reserved
- dsl_dataset_phys(ds
)->ds_unique_bytes
);
4275 (ds
->ds_reserved
- dsl_dataset_phys(ds
)->ds_unique_bytes
);
4277 asize
- MIN(asize
, parent_delta(ds
, asize
+ inflight
));
4280 if (!check_quota
|| ds
->ds_quota
== 0) {
4281 mutex_exit(&ds
->ds_lock
);
4285 * If they are requesting more space, and our current estimate
4286 * is over quota, they get to try again unless the actual
4287 * on-disk is over quota and there are no pending changes (which
4288 * may free up space for us).
4290 if (dsl_dataset_phys(ds
)->ds_referenced_bytes
+ inflight
>=
4293 dsl_dataset_phys(ds
)->ds_referenced_bytes
< ds
->ds_quota
)
4294 error
= SET_ERROR(ERESTART
);
4296 error
= SET_ERROR(EDQUOT
);
4298 mutex_exit(&ds
->ds_lock
);
4303 typedef struct dsl_dataset_set_qr_arg
{
4304 const char *ddsqra_name
;
4305 zprop_source_t ddsqra_source
;
4306 uint64_t ddsqra_value
;
4307 } dsl_dataset_set_qr_arg_t
;
4311 dsl_dataset_set_refquota_check(void *arg
, dmu_tx_t
*tx
)
4313 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
4314 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
4319 if (spa_version(dp
->dp_spa
) < SPA_VERSION_REFQUOTA
)
4320 return (SET_ERROR(ENOTSUP
));
4322 error
= dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
);
4326 if (ds
->ds_is_snapshot
) {
4327 dsl_dataset_rele(ds
, FTAG
);
4328 return (SET_ERROR(EINVAL
));
4331 error
= dsl_prop_predict(ds
->ds_dir
,
4332 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
4333 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, &newval
);
4335 dsl_dataset_rele(ds
, FTAG
);
4340 dsl_dataset_rele(ds
, FTAG
);
4344 if (newval
< dsl_dataset_phys(ds
)->ds_referenced_bytes
||
4345 newval
< ds
->ds_reserved
) {
4346 dsl_dataset_rele(ds
, FTAG
);
4347 return (SET_ERROR(ENOSPC
));
4350 dsl_dataset_rele(ds
, FTAG
);
4355 dsl_dataset_set_refquota_sync(void *arg
, dmu_tx_t
*tx
)
4357 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
4358 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
4359 dsl_dataset_t
*ds
= NULL
;
4362 VERIFY0(dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
));
4364 dsl_prop_set_sync_impl(ds
,
4365 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
4366 ddsqra
->ddsqra_source
, sizeof (ddsqra
->ddsqra_value
), 1,
4367 &ddsqra
->ddsqra_value
, tx
);
4369 VERIFY0(dsl_prop_get_int_ds(ds
,
4370 zfs_prop_to_name(ZFS_PROP_REFQUOTA
), &newval
));
4372 if (ds
->ds_quota
!= newval
) {
4373 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
4374 ds
->ds_quota
= newval
;
4376 dsl_dataset_rele(ds
, FTAG
);
4380 dsl_dataset_set_refquota(const char *dsname
, zprop_source_t source
,
4383 dsl_dataset_set_qr_arg_t ddsqra
;
4385 ddsqra
.ddsqra_name
= dsname
;
4386 ddsqra
.ddsqra_source
= source
;
4387 ddsqra
.ddsqra_value
= refquota
;
4389 return (dsl_sync_task(dsname
, dsl_dataset_set_refquota_check
,
4390 dsl_dataset_set_refquota_sync
, &ddsqra
, 0,
4391 ZFS_SPACE_CHECK_EXTRA_RESERVED
));
4395 dsl_dataset_set_refreservation_check(void *arg
, dmu_tx_t
*tx
)
4397 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
4398 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
4401 uint64_t newval
, unique
;
4403 if (spa_version(dp
->dp_spa
) < SPA_VERSION_REFRESERVATION
)
4404 return (SET_ERROR(ENOTSUP
));
4406 error
= dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
);
4410 if (ds
->ds_is_snapshot
) {
4411 dsl_dataset_rele(ds
, FTAG
);
4412 return (SET_ERROR(EINVAL
));
4415 error
= dsl_prop_predict(ds
->ds_dir
,
4416 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
4417 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, &newval
);
4419 dsl_dataset_rele(ds
, FTAG
);
4424 * If we are doing the preliminary check in open context, the
4425 * space estimates may be inaccurate.
4427 if (!dmu_tx_is_syncing(tx
)) {
4428 dsl_dataset_rele(ds
, FTAG
);
4432 mutex_enter(&ds
->ds_lock
);
4433 if (!DS_UNIQUE_IS_ACCURATE(ds
))
4434 dsl_dataset_recalc_head_uniq(ds
);
4435 unique
= dsl_dataset_phys(ds
)->ds_unique_bytes
;
4436 mutex_exit(&ds
->ds_lock
);
4438 if (MAX(unique
, newval
) > MAX(unique
, ds
->ds_reserved
)) {
4439 uint64_t delta
= MAX(unique
, newval
) -
4440 MAX(unique
, ds
->ds_reserved
);
4443 dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, B_TRUE
) ||
4444 (ds
->ds_quota
> 0 && newval
> ds
->ds_quota
)) {
4445 dsl_dataset_rele(ds
, FTAG
);
4446 return (SET_ERROR(ENOSPC
));
4450 dsl_dataset_rele(ds
, FTAG
);
4455 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t
*ds
,
4456 zprop_source_t source
, uint64_t value
, dmu_tx_t
*tx
)
4462 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
4463 source
, sizeof (value
), 1, &value
, tx
);
4465 VERIFY0(dsl_prop_get_int_ds(ds
,
4466 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), &newval
));
4468 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
4469 mutex_enter(&ds
->ds_dir
->dd_lock
);
4470 mutex_enter(&ds
->ds_lock
);
4471 ASSERT(DS_UNIQUE_IS_ACCURATE(ds
));
4472 unique
= dsl_dataset_phys(ds
)->ds_unique_bytes
;
4473 delta
= MAX(0, (int64_t)(newval
- unique
)) -
4474 MAX(0, (int64_t)(ds
->ds_reserved
- unique
));
4475 ds
->ds_reserved
= newval
;
4476 mutex_exit(&ds
->ds_lock
);
4478 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_REFRSRV
, delta
, 0, 0, tx
);
4479 mutex_exit(&ds
->ds_dir
->dd_lock
);
4483 dsl_dataset_set_refreservation_sync(void *arg
, dmu_tx_t
*tx
)
4485 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
4486 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
4487 dsl_dataset_t
*ds
= NULL
;
4489 VERIFY0(dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
));
4490 dsl_dataset_set_refreservation_sync_impl(ds
,
4491 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, tx
);
4492 dsl_dataset_rele(ds
, FTAG
);
4496 dsl_dataset_set_refreservation(const char *dsname
, zprop_source_t source
,
4497 uint64_t refreservation
)
4499 dsl_dataset_set_qr_arg_t ddsqra
;
4501 ddsqra
.ddsqra_name
= dsname
;
4502 ddsqra
.ddsqra_source
= source
;
4503 ddsqra
.ddsqra_value
= refreservation
;
4505 return (dsl_sync_task(dsname
, dsl_dataset_set_refreservation_check
,
4506 dsl_dataset_set_refreservation_sync
, &ddsqra
, 0,
4507 ZFS_SPACE_CHECK_EXTRA_RESERVED
));
4510 typedef struct dsl_dataset_set_compression_arg
{
4511 const char *ddsca_name
;
4512 zprop_source_t ddsca_source
;
4513 uint64_t ddsca_value
;
4514 } dsl_dataset_set_compression_arg_t
;
4517 dsl_dataset_set_compression_check(void *arg
, dmu_tx_t
*tx
)
4519 dsl_dataset_set_compression_arg_t
*ddsca
= arg
;
4520 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
4522 uint64_t compval
= ZIO_COMPRESS_ALGO(ddsca
->ddsca_value
);
4523 spa_feature_t f
= zio_compress_to_feature(compval
);
4525 if (f
== SPA_FEATURE_NONE
)
4526 return (SET_ERROR(EINVAL
));
4528 if (!spa_feature_is_enabled(dp
->dp_spa
, f
))
4529 return (SET_ERROR(ENOTSUP
));
4535 dsl_dataset_set_compression_sync(void *arg
, dmu_tx_t
*tx
)
4537 dsl_dataset_set_compression_arg_t
*ddsca
= arg
;
4538 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
4539 dsl_dataset_t
*ds
= NULL
;
4541 uint64_t compval
= ZIO_COMPRESS_ALGO(ddsca
->ddsca_value
);
4542 spa_feature_t f
= zio_compress_to_feature(compval
);
4543 ASSERT3S(f
, !=, SPA_FEATURE_NONE
);
4544 ASSERT3S(spa_feature_table
[f
].fi_type
, ==, ZFEATURE_TYPE_BOOLEAN
);
4546 VERIFY0(dsl_dataset_hold(dp
, ddsca
->ddsca_name
, FTAG
, &ds
));
4547 if (zfeature_active(f
, ds
->ds_feature
[f
]) != B_TRUE
) {
4548 ds
->ds_feature_activation
[f
] = (void *)B_TRUE
;
4549 dsl_dataset_activate_feature(ds
->ds_object
, f
,
4550 ds
->ds_feature_activation
[f
], tx
);
4551 ds
->ds_feature
[f
] = ds
->ds_feature_activation
[f
];
4553 dsl_dataset_rele(ds
, FTAG
);
4557 dsl_dataset_set_compression(const char *dsname
, zprop_source_t source
,
4558 uint64_t compression
)
4560 dsl_dataset_set_compression_arg_t ddsca
;
4563 * The sync task is only required for zstd in order to activate
4564 * the feature flag when the property is first set.
4566 if (ZIO_COMPRESS_ALGO(compression
) != ZIO_COMPRESS_ZSTD
)
4569 ddsca
.ddsca_name
= dsname
;
4570 ddsca
.ddsca_source
= source
;
4571 ddsca
.ddsca_value
= compression
;
4573 return (dsl_sync_task(dsname
, dsl_dataset_set_compression_check
,
4574 dsl_dataset_set_compression_sync
, &ddsca
, 0,
4575 ZFS_SPACE_CHECK_EXTRA_RESERVED
));
4579 * Return (in *usedp) the amount of space referenced by "new" that was not
4580 * referenced at the time the bookmark corresponds to. "New" may be a
4581 * snapshot or a head. The bookmark must be before new, in
4582 * new's filesystem (or its origin) -- caller verifies this.
4584 * The written space is calculated by considering two components: First, we
4585 * ignore any freed space, and calculate the written as new's used space
4586 * minus old's used space. Next, we add in the amount of space that was freed
4587 * between the two time points, thus reducing new's used space relative to
4588 * old's. Specifically, this is the space that was born before
4589 * zbm_creation_txg, and freed before new (ie. on new's deadlist or a
4590 * previous deadlist).
4592 * space freed [---------------------]
4593 * snapshots ---O-------O--------O-------O------
4596 * Note, the bookmark's zbm_*_bytes_refd must be valid, but if the HAS_FBN
4597 * flag is not set, we will calculate the freed_before_next based on the
4598 * next snapshot's deadlist, rather than using zbm_*_freed_before_next_snap.
4601 dsl_dataset_space_written_impl(zfs_bookmark_phys_t
*bmp
,
4602 dsl_dataset_t
*new, uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
4605 dsl_pool_t
*dp
= new->ds_dir
->dd_pool
;
4607 ASSERT(dsl_pool_config_held(dp
));
4608 if (dsl_dataset_is_snapshot(new)) {
4609 ASSERT3U(bmp
->zbm_creation_txg
, <,
4610 dsl_dataset_phys(new)->ds_creation_txg
);
4614 *usedp
+= dsl_dataset_phys(new)->ds_referenced_bytes
;
4615 *usedp
-= bmp
->zbm_referenced_bytes_refd
;
4618 *compp
+= dsl_dataset_phys(new)->ds_compressed_bytes
;
4619 *compp
-= bmp
->zbm_compressed_bytes_refd
;
4622 *uncompp
+= dsl_dataset_phys(new)->ds_uncompressed_bytes
;
4623 *uncompp
-= bmp
->zbm_uncompressed_bytes_refd
;
4625 dsl_dataset_t
*snap
= new;
4627 while (dsl_dataset_phys(snap
)->ds_prev_snap_txg
>
4628 bmp
->zbm_creation_txg
) {
4629 uint64_t used
, comp
, uncomp
;
4631 dsl_deadlist_space_range(&snap
->ds_deadlist
,
4632 0, bmp
->zbm_creation_txg
,
4633 &used
, &comp
, &uncomp
);
4638 uint64_t snapobj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
4640 dsl_dataset_rele(snap
, FTAG
);
4641 err
= dsl_dataset_hold_obj(dp
, snapobj
, FTAG
, &snap
);
4647 * We might not have the FBN if we are calculating written from
4648 * a snapshot (because we didn't know the correct "next" snapshot
4651 if (bmp
->zbm_flags
& ZBM_FLAG_HAS_FBN
) {
4652 *usedp
+= bmp
->zbm_referenced_freed_before_next_snap
;
4653 *compp
+= bmp
->zbm_compressed_freed_before_next_snap
;
4654 *uncompp
+= bmp
->zbm_uncompressed_freed_before_next_snap
;
4656 ASSERT3U(dsl_dataset_phys(snap
)->ds_prev_snap_txg
, ==,
4657 bmp
->zbm_creation_txg
);
4658 uint64_t used
, comp
, uncomp
;
4659 dsl_deadlist_space(&snap
->ds_deadlist
, &used
, &comp
, &uncomp
);
4665 dsl_dataset_rele(snap
, FTAG
);
4670 * Return (in *usedp) the amount of space written in new that was not
4671 * present at the time the bookmark corresponds to. New may be a
4672 * snapshot or the head. Old must be a bookmark before new, in
4673 * new's filesystem (or its origin) -- caller verifies this.
4676 dsl_dataset_space_written_bookmark(zfs_bookmark_phys_t
*bmp
,
4677 dsl_dataset_t
*new, uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
4679 if (!(bmp
->zbm_flags
& ZBM_FLAG_HAS_FBN
))
4680 return (SET_ERROR(ENOTSUP
));
4681 return (dsl_dataset_space_written_impl(bmp
, new,
4682 usedp
, compp
, uncompp
));
4686 * Return (in *usedp) the amount of space written in new that is not
4687 * present in oldsnap. New may be a snapshot or the head. Old must be
4688 * a snapshot before new, in new's filesystem (or its origin). If not then
4689 * fail and return EINVAL.
4692 dsl_dataset_space_written(dsl_dataset_t
*oldsnap
, dsl_dataset_t
*new,
4693 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
4695 if (!dsl_dataset_is_before(new, oldsnap
, 0))
4696 return (SET_ERROR(EINVAL
));
4698 zfs_bookmark_phys_t zbm
= { 0 };
4699 dsl_dataset_phys_t
*dsp
= dsl_dataset_phys(oldsnap
);
4700 zbm
.zbm_guid
= dsp
->ds_guid
;
4701 zbm
.zbm_creation_txg
= dsp
->ds_creation_txg
;
4702 zbm
.zbm_creation_time
= dsp
->ds_creation_time
;
4703 zbm
.zbm_referenced_bytes_refd
= dsp
->ds_referenced_bytes
;
4704 zbm
.zbm_compressed_bytes_refd
= dsp
->ds_compressed_bytes
;
4705 zbm
.zbm_uncompressed_bytes_refd
= dsp
->ds_uncompressed_bytes
;
4708 * If oldsnap is the origin (or origin's origin, ...) of new,
4709 * we can't easily calculate the effective FBN. Therefore,
4710 * we do not set ZBM_FLAG_HAS_FBN, so that the _impl will calculate
4711 * it relative to the correct "next": the next snapshot towards "new",
4712 * rather than the next snapshot in oldsnap's dsl_dir.
4714 return (dsl_dataset_space_written_impl(&zbm
, new,
4715 usedp
, compp
, uncompp
));
4719 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
4720 * lastsnap, and all snapshots in between are deleted.
4722 * blocks that would be freed [---------------------------]
4723 * snapshots ---O-------O--------O-------O--------O
4724 * firstsnap lastsnap
4726 * This is the set of blocks that were born after the snap before firstsnap,
4727 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
4728 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
4729 * We calculate this by iterating over the relevant deadlists (from the snap
4730 * after lastsnap, backward to the snap after firstsnap), summing up the
4731 * space on the deadlist that was born after the snap before firstsnap.
4734 dsl_dataset_space_wouldfree(dsl_dataset_t
*firstsnap
,
4735 dsl_dataset_t
*lastsnap
,
4736 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
4740 dsl_pool_t
*dp
= firstsnap
->ds_dir
->dd_pool
;
4742 ASSERT(firstsnap
->ds_is_snapshot
);
4743 ASSERT(lastsnap
->ds_is_snapshot
);
4746 * Check that the snapshots are in the same dsl_dir, and firstsnap
4747 * is before lastsnap.
4749 if (firstsnap
->ds_dir
!= lastsnap
->ds_dir
||
4750 dsl_dataset_phys(firstsnap
)->ds_creation_txg
>
4751 dsl_dataset_phys(lastsnap
)->ds_creation_txg
)
4752 return (SET_ERROR(EINVAL
));
4754 *usedp
= *compp
= *uncompp
= 0;
4756 snapobj
= dsl_dataset_phys(lastsnap
)->ds_next_snap_obj
;
4757 while (snapobj
!= firstsnap
->ds_object
) {
4759 uint64_t used
, comp
, uncomp
;
4761 err
= dsl_dataset_hold_obj(dp
, snapobj
, FTAG
, &ds
);
4765 dsl_deadlist_space_range(&ds
->ds_deadlist
,
4766 dsl_dataset_phys(firstsnap
)->ds_prev_snap_txg
, UINT64_MAX
,
4767 &used
, &comp
, &uncomp
);
4772 snapobj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
4773 ASSERT3U(snapobj
, !=, 0);
4774 dsl_dataset_rele(ds
, FTAG
);
4780 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
4781 * For example, they could both be snapshots of the same filesystem, and
4782 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
4783 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
4784 * filesystem. Or 'earlier' could be the origin's origin.
4786 * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
4789 dsl_dataset_is_before(dsl_dataset_t
*later
, dsl_dataset_t
*earlier
,
4790 uint64_t earlier_txg
)
4792 dsl_pool_t
*dp
= later
->ds_dir
->dd_pool
;
4796 ASSERT(dsl_pool_config_held(dp
));
4797 ASSERT(earlier
->ds_is_snapshot
|| earlier_txg
!= 0);
4799 if (earlier_txg
== 0)
4800 earlier_txg
= dsl_dataset_phys(earlier
)->ds_creation_txg
;
4802 if (later
->ds_is_snapshot
&&
4803 earlier_txg
>= dsl_dataset_phys(later
)->ds_creation_txg
)
4806 if (later
->ds_dir
== earlier
->ds_dir
)
4810 * We check dd_origin_obj explicitly here rather than using
4811 * dsl_dir_is_clone() so that we will return TRUE if "earlier"
4812 * is $ORIGIN@$ORIGIN. dsl_dataset_space_written() depends on
4815 if (dsl_dir_phys(later
->ds_dir
)->dd_origin_obj
== 0)
4818 dsl_dataset_t
*origin
;
4819 error
= dsl_dataset_hold_obj(dp
,
4820 dsl_dir_phys(later
->ds_dir
)->dd_origin_obj
, FTAG
, &origin
);
4823 if (dsl_dataset_phys(origin
)->ds_creation_txg
== earlier_txg
&&
4824 origin
->ds_dir
== earlier
->ds_dir
) {
4825 dsl_dataset_rele(origin
, FTAG
);
4828 ret
= dsl_dataset_is_before(origin
, earlier
, earlier_txg
);
4829 dsl_dataset_rele(origin
, FTAG
);
4834 dsl_dataset_zapify(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
4836 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
4837 dmu_object_zapify(mos
, ds
->ds_object
, DMU_OT_DSL_DATASET
, tx
);
4841 dsl_dataset_is_zapified(dsl_dataset_t
*ds
)
4843 dmu_object_info_t doi
;
4845 dmu_object_info_from_db(ds
->ds_dbuf
, &doi
);
4846 return (doi
.doi_type
== DMU_OTN_ZAP_METADATA
);
4850 dsl_dataset_has_resume_receive_state(dsl_dataset_t
*ds
)
4852 return (dsl_dataset_is_zapified(ds
) &&
4853 zap_contains(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
4854 ds
->ds_object
, DS_FIELD_RESUME_TOGUID
) == 0);
4858 dsl_dataset_get_remap_deadlist_object(dsl_dataset_t
*ds
)
4860 uint64_t remap_deadlist_obj
;
4863 if (!dsl_dataset_is_zapified(ds
))
4866 err
= zap_lookup(ds
->ds_dir
->dd_pool
->dp_meta_objset
, ds
->ds_object
,
4867 DS_FIELD_REMAP_DEADLIST
, sizeof (remap_deadlist_obj
), 1,
4868 &remap_deadlist_obj
);
4871 VERIFY3S(err
, ==, ENOENT
);
4875 ASSERT(remap_deadlist_obj
!= 0);
4876 return (remap_deadlist_obj
);
4880 dsl_dataset_remap_deadlist_exists(dsl_dataset_t
*ds
)
4882 EQUIV(dsl_deadlist_is_open(&ds
->ds_remap_deadlist
),
4883 dsl_dataset_get_remap_deadlist_object(ds
) != 0);
4884 return (dsl_deadlist_is_open(&ds
->ds_remap_deadlist
));
4888 dsl_dataset_set_remap_deadlist_object(dsl_dataset_t
*ds
, uint64_t obj
,
4892 dsl_dataset_zapify(ds
, tx
);
4893 VERIFY0(zap_add(ds
->ds_dir
->dd_pool
->dp_meta_objset
, ds
->ds_object
,
4894 DS_FIELD_REMAP_DEADLIST
, sizeof (obj
), 1, &obj
, tx
));
4898 dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
4900 VERIFY0(zap_remove(ds
->ds_dir
->dd_pool
->dp_meta_objset
,
4901 ds
->ds_object
, DS_FIELD_REMAP_DEADLIST
, tx
));
4905 dsl_dataset_destroy_remap_deadlist(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
4907 uint64_t remap_deadlist_object
;
4908 spa_t
*spa
= ds
->ds_dir
->dd_pool
->dp_spa
;
4910 ASSERT(dmu_tx_is_syncing(tx
));
4911 ASSERT(dsl_dataset_remap_deadlist_exists(ds
));
4913 remap_deadlist_object
= ds
->ds_remap_deadlist
.dl_object
;
4914 dsl_deadlist_close(&ds
->ds_remap_deadlist
);
4915 dsl_deadlist_free(spa_meta_objset(spa
), remap_deadlist_object
, tx
);
4916 dsl_dataset_unset_remap_deadlist_object(ds
, tx
);
4917 spa_feature_decr(spa
, SPA_FEATURE_OBSOLETE_COUNTS
, tx
);
4921 dsl_dataset_create_remap_deadlist(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
4923 uint64_t remap_deadlist_obj
;
4924 spa_t
*spa
= ds
->ds_dir
->dd_pool
->dp_spa
;
4926 ASSERT(dmu_tx_is_syncing(tx
));
4927 ASSERT(MUTEX_HELD(&ds
->ds_remap_deadlist_lock
));
4929 * Currently we only create remap deadlists when there are indirect
4930 * vdevs with referenced mappings.
4932 ASSERT(spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
4934 remap_deadlist_obj
= dsl_deadlist_clone(
4935 &ds
->ds_deadlist
, UINT64_MAX
,
4936 dsl_dataset_phys(ds
)->ds_prev_snap_obj
, tx
);
4937 dsl_dataset_set_remap_deadlist_object(ds
,
4938 remap_deadlist_obj
, tx
);
4939 dsl_deadlist_open(&ds
->ds_remap_deadlist
, spa_meta_objset(spa
),
4940 remap_deadlist_obj
);
4941 spa_feature_incr(spa
, SPA_FEATURE_OBSOLETE_COUNTS
, tx
);
4945 dsl_dataset_activate_redaction(dsl_dataset_t
*ds
, uint64_t *redact_snaps
,
4946 uint64_t num_redact_snaps
, dmu_tx_t
*tx
)
4948 uint64_t dsobj
= ds
->ds_object
;
4949 struct feature_type_uint64_array_arg
*ftuaa
=
4950 kmem_zalloc(sizeof (*ftuaa
), KM_SLEEP
);
4951 ftuaa
->length
= (int64_t)num_redact_snaps
;
4952 if (num_redact_snaps
> 0) {
4953 ftuaa
->array
= kmem_alloc(num_redact_snaps
* sizeof (uint64_t),
4955 memcpy(ftuaa
->array
, redact_snaps
, num_redact_snaps
*
4958 dsl_dataset_activate_feature(dsobj
, SPA_FEATURE_REDACTED_DATASETS
,
4960 ds
->ds_feature
[SPA_FEATURE_REDACTED_DATASETS
] = ftuaa
;
4964 * Find and return (in *oldest_dsobj) the oldest snapshot of the dsobj
4965 * dataset whose birth time is >= min_txg.
4968 dsl_dataset_oldest_snapshot(spa_t
*spa
, uint64_t head_ds
, uint64_t min_txg
,
4969 uint64_t *oldest_dsobj
)
4972 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
4974 int error
= dsl_dataset_hold_obj(dp
, head_ds
, FTAG
, &ds
);
4978 uint64_t prev_obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
4979 uint64_t prev_obj_txg
= dsl_dataset_phys(ds
)->ds_prev_snap_txg
;
4981 while (prev_obj
!= 0 && min_txg
< prev_obj_txg
) {
4982 dsl_dataset_rele(ds
, FTAG
);
4983 if ((error
= dsl_dataset_hold_obj(dp
, prev_obj
,
4986 prev_obj_txg
= dsl_dataset_phys(ds
)->ds_prev_snap_txg
;
4987 prev_obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
4989 *oldest_dsobj
= ds
->ds_object
;
4990 dsl_dataset_rele(ds
, FTAG
);
4994 ZFS_MODULE_PARAM(zfs
, zfs_
, max_recordsize
, UINT
, ZMOD_RW
,
4995 "Max allowed record size");
4997 ZFS_MODULE_PARAM(zfs
, zfs_
, allow_redacted_dataset_mount
, INT
, ZMOD_RW
,
4998 "Allow mounting of redacted datasets");
5000 ZFS_MODULE_PARAM(zfs
, zfs_
, snapshot_history_enabled
, INT
, ZMOD_RW
,
5001 "Include snapshot events in pool history/events");
5003 EXPORT_SYMBOL(dsl_dataset_hold
);
5004 EXPORT_SYMBOL(dsl_dataset_hold_flags
);
5005 EXPORT_SYMBOL(dsl_dataset_hold_obj
);
5006 EXPORT_SYMBOL(dsl_dataset_hold_obj_flags
);
5007 EXPORT_SYMBOL(dsl_dataset_own
);
5008 EXPORT_SYMBOL(dsl_dataset_own_obj
);
5009 EXPORT_SYMBOL(dsl_dataset_name
);
5010 EXPORT_SYMBOL(dsl_dataset_rele
);
5011 EXPORT_SYMBOL(dsl_dataset_rele_flags
);
5012 EXPORT_SYMBOL(dsl_dataset_disown
);
5013 EXPORT_SYMBOL(dsl_dataset_tryown
);
5014 EXPORT_SYMBOL(dsl_dataset_create_sync
);
5015 EXPORT_SYMBOL(dsl_dataset_create_sync_dd
);
5016 EXPORT_SYMBOL(dsl_dataset_snapshot_check
);
5017 EXPORT_SYMBOL(dsl_dataset_snapshot_sync
);
5018 EXPORT_SYMBOL(dsl_dataset_promote
);
5019 EXPORT_SYMBOL(dsl_dataset_user_hold
);
5020 EXPORT_SYMBOL(dsl_dataset_user_release
);
5021 EXPORT_SYMBOL(dsl_dataset_get_holds
);
5022 EXPORT_SYMBOL(dsl_dataset_get_blkptr
);
5023 EXPORT_SYMBOL(dsl_dataset_get_spa
);
5024 EXPORT_SYMBOL(dsl_dataset_modified_since_snap
);
5025 EXPORT_SYMBOL(dsl_dataset_space_written
);
5026 EXPORT_SYMBOL(dsl_dataset_space_wouldfree
);
5027 EXPORT_SYMBOL(dsl_dataset_sync
);
5028 EXPORT_SYMBOL(dsl_dataset_block_born
);
5029 EXPORT_SYMBOL(dsl_dataset_block_kill
);
5030 EXPORT_SYMBOL(dsl_dataset_dirty
);
5031 EXPORT_SYMBOL(dsl_dataset_stats
);
5032 EXPORT_SYMBOL(dsl_dataset_fast_stat
);
5033 EXPORT_SYMBOL(dsl_dataset_space
);
5034 EXPORT_SYMBOL(dsl_dataset_fsid_guid
);
5035 EXPORT_SYMBOL(dsl_dsobj_to_dsname
);
5036 EXPORT_SYMBOL(dsl_dataset_check_quota
);
5037 EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl
);
5038 EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl
);