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 http://www.opensolaris.org/os/licensing.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2014 RackTop Systems.
28 #include <sys/dmu_objset.h>
29 #include <sys/dsl_dataset.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_prop.h>
32 #include <sys/dsl_synctask.h>
33 #include <sys/dmu_traverse.h>
34 #include <sys/dmu_impl.h>
35 #include <sys/dmu_tx.h>
39 #include <sys/zfeature.h>
40 #include <sys/unique.h>
41 #include <sys/zfs_context.h>
42 #include <sys/zfs_ioctl.h>
44 #include <sys/zfs_znode.h>
45 #include <sys/zfs_onexit.h>
47 #include <sys/dsl_scan.h>
48 #include <sys/dsl_deadlist.h>
49 #include <sys/dsl_destroy.h>
50 #include <sys/dsl_userhold.h>
51 #include <sys/dsl_bookmark.h>
53 #define SWITCH64(x, y) \
55 uint64_t __tmp = (x); \
60 #define DS_REF_MAX (1ULL << 62)
62 #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE
65 * Figure out how much of this delta should be propogated to the dsl_dir
66 * layer. If there's a refreservation, that space has already been
67 * partially accounted for in our ancestors.
70 parent_delta(dsl_dataset_t
*ds
, int64_t delta
)
72 uint64_t old_bytes
, new_bytes
;
74 if (ds
->ds_reserved
== 0)
77 old_bytes
= MAX(ds
->ds_phys
->ds_unique_bytes
, ds
->ds_reserved
);
78 new_bytes
= MAX(ds
->ds_phys
->ds_unique_bytes
+ delta
, ds
->ds_reserved
);
80 ASSERT3U(ABS((int64_t)(new_bytes
- old_bytes
)), <=, ABS(delta
));
81 return (new_bytes
- old_bytes
);
85 dsl_dataset_block_born(dsl_dataset_t
*ds
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
87 int used
, compressed
, uncompressed
;
90 used
= bp_get_dsize_sync(tx
->tx_pool
->dp_spa
, bp
);
91 compressed
= BP_GET_PSIZE(bp
);
92 uncompressed
= BP_GET_UCSIZE(bp
);
94 dprintf_bp(bp
, "ds=%p", ds
);
96 ASSERT(dmu_tx_is_syncing(tx
));
97 /* It could have been compressed away to nothing */
100 ASSERT(BP_GET_TYPE(bp
) != DMU_OT_NONE
);
101 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp
)));
103 dsl_pool_mos_diduse_space(tx
->tx_pool
,
104 used
, compressed
, uncompressed
);
108 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
109 mutex_enter(&ds
->ds_lock
);
110 delta
= parent_delta(ds
, used
);
111 ds
->ds_phys
->ds_referenced_bytes
+= used
;
112 ds
->ds_phys
->ds_compressed_bytes
+= compressed
;
113 ds
->ds_phys
->ds_uncompressed_bytes
+= uncompressed
;
114 ds
->ds_phys
->ds_unique_bytes
+= used
;
115 mutex_exit(&ds
->ds_lock
);
116 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_HEAD
, delta
,
117 compressed
, uncompressed
, tx
);
118 dsl_dir_transfer_space(ds
->ds_dir
, used
- delta
,
119 DD_USED_REFRSRV
, DD_USED_HEAD
, tx
);
123 dsl_dataset_block_kill(dsl_dataset_t
*ds
, const blkptr_t
*bp
, dmu_tx_t
*tx
,
126 int used
= bp_get_dsize_sync(tx
->tx_pool
->dp_spa
, bp
);
127 int compressed
= BP_GET_PSIZE(bp
);
128 int uncompressed
= BP_GET_UCSIZE(bp
);
133 ASSERT(dmu_tx_is_syncing(tx
));
134 ASSERT(bp
->blk_birth
<= tx
->tx_txg
);
137 dsl_free(tx
->tx_pool
, tx
->tx_txg
, bp
);
138 dsl_pool_mos_diduse_space(tx
->tx_pool
,
139 -used
, -compressed
, -uncompressed
);
142 ASSERT3P(tx
->tx_pool
, ==, ds
->ds_dir
->dd_pool
);
144 ASSERT(!dsl_dataset_is_snapshot(ds
));
145 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
147 if (bp
->blk_birth
> ds
->ds_phys
->ds_prev_snap_txg
) {
150 dprintf_bp(bp
, "freeing ds=%llu", ds
->ds_object
);
151 dsl_free(tx
->tx_pool
, tx
->tx_txg
, bp
);
153 mutex_enter(&ds
->ds_lock
);
154 ASSERT(ds
->ds_phys
->ds_unique_bytes
>= used
||
155 !DS_UNIQUE_IS_ACCURATE(ds
));
156 delta
= parent_delta(ds
, -used
);
157 ds
->ds_phys
->ds_unique_bytes
-= used
;
158 mutex_exit(&ds
->ds_lock
);
159 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_HEAD
,
160 delta
, -compressed
, -uncompressed
, tx
);
161 dsl_dir_transfer_space(ds
->ds_dir
, -used
- delta
,
162 DD_USED_REFRSRV
, DD_USED_HEAD
, tx
);
164 dprintf_bp(bp
, "putting on dead list: %s", "");
167 * We are here as part of zio's write done callback,
168 * which means we're a zio interrupt thread. We can't
169 * call dsl_deadlist_insert() now because it may block
170 * waiting for I/O. Instead, put bp on the deferred
171 * queue and let dsl_pool_sync() finish the job.
173 bplist_append(&ds
->ds_pending_deadlist
, bp
);
175 dsl_deadlist_insert(&ds
->ds_deadlist
, bp
, tx
);
177 ASSERT3U(ds
->ds_prev
->ds_object
, ==,
178 ds
->ds_phys
->ds_prev_snap_obj
);
179 ASSERT(ds
->ds_prev
->ds_phys
->ds_num_children
> 0);
180 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
181 if (ds
->ds_prev
->ds_phys
->ds_next_snap_obj
==
182 ds
->ds_object
&& bp
->blk_birth
>
183 ds
->ds_prev
->ds_phys
->ds_prev_snap_txg
) {
184 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
185 mutex_enter(&ds
->ds_prev
->ds_lock
);
186 ds
->ds_prev
->ds_phys
->ds_unique_bytes
+= used
;
187 mutex_exit(&ds
->ds_prev
->ds_lock
);
189 if (bp
->blk_birth
> ds
->ds_dir
->dd_origin_txg
) {
190 dsl_dir_transfer_space(ds
->ds_dir
, used
,
191 DD_USED_HEAD
, DD_USED_SNAP
, tx
);
194 mutex_enter(&ds
->ds_lock
);
195 ASSERT3U(ds
->ds_phys
->ds_referenced_bytes
, >=, used
);
196 ds
->ds_phys
->ds_referenced_bytes
-= used
;
197 ASSERT3U(ds
->ds_phys
->ds_compressed_bytes
, >=, compressed
);
198 ds
->ds_phys
->ds_compressed_bytes
-= compressed
;
199 ASSERT3U(ds
->ds_phys
->ds_uncompressed_bytes
, >=, uncompressed
);
200 ds
->ds_phys
->ds_uncompressed_bytes
-= uncompressed
;
201 mutex_exit(&ds
->ds_lock
);
207 dsl_dataset_prev_snap_txg(dsl_dataset_t
*ds
)
209 uint64_t trysnap
= 0;
214 * The snapshot creation could fail, but that would cause an
215 * incorrect FALSE return, which would only result in an
216 * overestimation of the amount of space that an operation would
217 * consume, which is OK.
219 * There's also a small window where we could miss a pending
220 * snapshot, because we could set the sync task in the quiescing
221 * phase. So this should only be used as a guess.
223 if (ds
->ds_trysnap_txg
>
224 spa_last_synced_txg(ds
->ds_dir
->dd_pool
->dp_spa
))
225 trysnap
= ds
->ds_trysnap_txg
;
226 return (MAX(ds
->ds_phys
->ds_prev_snap_txg
, trysnap
));
230 dsl_dataset_block_freeable(dsl_dataset_t
*ds
, const blkptr_t
*bp
,
233 if (blk_birth
<= dsl_dataset_prev_snap_txg(ds
) ||
234 (bp
!= NULL
&& BP_IS_HOLE(bp
)))
237 ddt_prefetch(dsl_dataset_get_spa(ds
), bp
);
244 dsl_dataset_evict(dmu_buf_t
*db
, void *dsv
)
246 dsl_dataset_t
*ds
= dsv
;
248 ASSERT(ds
->ds_owner
== NULL
);
250 unique_remove(ds
->ds_fsid_guid
);
252 if (ds
->ds_objset
!= NULL
)
253 dmu_objset_evict(ds
->ds_objset
);
256 dsl_dataset_rele(ds
->ds_prev
, ds
);
260 bplist_destroy(&ds
->ds_pending_deadlist
);
261 if (ds
->ds_phys
->ds_deadlist_obj
!= 0)
262 dsl_deadlist_close(&ds
->ds_deadlist
);
264 dsl_dir_rele(ds
->ds_dir
, ds
);
266 ASSERT(!list_link_active(&ds
->ds_synced_link
));
268 mutex_destroy(&ds
->ds_lock
);
269 mutex_destroy(&ds
->ds_opening_lock
);
270 mutex_destroy(&ds
->ds_sendstream_lock
);
271 refcount_destroy(&ds
->ds_longholds
);
273 kmem_free(ds
, sizeof (dsl_dataset_t
));
277 dsl_dataset_get_snapname(dsl_dataset_t
*ds
)
279 dsl_dataset_phys_t
*headphys
;
282 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
283 objset_t
*mos
= dp
->dp_meta_objset
;
285 if (ds
->ds_snapname
[0])
287 if (ds
->ds_phys
->ds_next_snap_obj
== 0)
290 err
= dmu_bonus_hold(mos
, ds
->ds_dir
->dd_phys
->dd_head_dataset_obj
,
294 headphys
= headdbuf
->db_data
;
295 err
= zap_value_search(dp
->dp_meta_objset
,
296 headphys
->ds_snapnames_zapobj
, ds
->ds_object
, 0, ds
->ds_snapname
);
297 dmu_buf_rele(headdbuf
, FTAG
);
302 dsl_dataset_snap_lookup(dsl_dataset_t
*ds
, const char *name
, uint64_t *value
)
304 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
305 uint64_t snapobj
= ds
->ds_phys
->ds_snapnames_zapobj
;
309 if (ds
->ds_phys
->ds_flags
& DS_FLAG_CI_DATASET
)
314 err
= zap_lookup_norm(mos
, snapobj
, name
, 8, 1,
315 value
, mt
, NULL
, 0, NULL
);
316 if (err
== ENOTSUP
&& mt
== MT_FIRST
)
317 err
= zap_lookup(mos
, snapobj
, name
, 8, 1, value
);
322 dsl_dataset_snap_remove(dsl_dataset_t
*ds
, const char *name
, dmu_tx_t
*tx
,
325 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
326 uint64_t snapobj
= ds
->ds_phys
->ds_snapnames_zapobj
;
330 dsl_dir_snap_cmtime_update(ds
->ds_dir
);
332 if (ds
->ds_phys
->ds_flags
& DS_FLAG_CI_DATASET
)
337 err
= zap_remove_norm(mos
, snapobj
, name
, mt
, tx
);
338 if (err
== ENOTSUP
&& mt
== MT_FIRST
)
339 err
= zap_remove(mos
, snapobj
, name
, tx
);
341 if (err
== 0 && adj_cnt
)
342 dsl_fs_ss_count_adjust(ds
->ds_dir
, -1,
343 DD_FIELD_SNAPSHOT_COUNT
, tx
);
349 dsl_dataset_hold_obj(dsl_pool_t
*dp
, uint64_t dsobj
, void *tag
,
352 objset_t
*mos
= dp
->dp_meta_objset
;
356 dmu_object_info_t doi
;
358 ASSERT(dsl_pool_config_held(dp
));
360 err
= dmu_bonus_hold(mos
, dsobj
, tag
, &dbuf
);
364 /* Make sure dsobj has the correct object type. */
365 dmu_object_info_from_db(dbuf
, &doi
);
366 if (doi
.doi_bonus_type
!= DMU_OT_DSL_DATASET
) {
367 dmu_buf_rele(dbuf
, tag
);
368 return (SET_ERROR(EINVAL
));
371 ds
= dmu_buf_get_user(dbuf
);
373 dsl_dataset_t
*winner
= NULL
;
375 ds
= kmem_zalloc(sizeof (dsl_dataset_t
), KM_SLEEP
);
377 ds
->ds_object
= dsobj
;
378 ds
->ds_phys
= dbuf
->db_data
;
379 list_link_init(&ds
->ds_synced_link
);
381 mutex_init(&ds
->ds_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
382 mutex_init(&ds
->ds_opening_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
383 mutex_init(&ds
->ds_sendstream_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
384 refcount_create(&ds
->ds_longholds
);
386 bplist_create(&ds
->ds_pending_deadlist
);
387 dsl_deadlist_open(&ds
->ds_deadlist
,
388 mos
, ds
->ds_phys
->ds_deadlist_obj
);
390 list_create(&ds
->ds_sendstreams
, sizeof (dmu_sendarg_t
),
391 offsetof(dmu_sendarg_t
, dsa_link
));
394 err
= dsl_dir_hold_obj(dp
,
395 ds
->ds_phys
->ds_dir_obj
, NULL
, ds
, &ds
->ds_dir
);
398 mutex_destroy(&ds
->ds_lock
);
399 mutex_destroy(&ds
->ds_opening_lock
);
400 mutex_destroy(&ds
->ds_sendstream_lock
);
401 refcount_destroy(&ds
->ds_longholds
);
402 bplist_destroy(&ds
->ds_pending_deadlist
);
403 dsl_deadlist_close(&ds
->ds_deadlist
);
404 kmem_free(ds
, sizeof (dsl_dataset_t
));
405 dmu_buf_rele(dbuf
, tag
);
409 if (!dsl_dataset_is_snapshot(ds
)) {
410 ds
->ds_snapname
[0] = '\0';
411 if (ds
->ds_phys
->ds_prev_snap_obj
!= 0) {
412 err
= dsl_dataset_hold_obj(dp
,
413 ds
->ds_phys
->ds_prev_snap_obj
,
416 if (doi
.doi_type
== DMU_OTN_ZAP_METADATA
) {
417 int zaperr
= zap_lookup(mos
, ds
->ds_object
,
418 DS_FIELD_BOOKMARK_NAMES
,
419 sizeof (ds
->ds_bookmarks
), 1,
421 if (zaperr
!= ENOENT
)
425 if (zfs_flags
& ZFS_DEBUG_SNAPNAMES
)
426 err
= dsl_dataset_get_snapname(ds
);
427 if (err
== 0 && ds
->ds_phys
->ds_userrefs_obj
!= 0) {
429 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
430 ds
->ds_phys
->ds_userrefs_obj
,
435 if (err
== 0 && !dsl_dataset_is_snapshot(ds
)) {
436 err
= dsl_prop_get_int_ds(ds
,
437 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
440 err
= dsl_prop_get_int_ds(ds
,
441 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
445 ds
->ds_reserved
= ds
->ds_quota
= 0;
448 if (err
!= 0 || (winner
= dmu_buf_set_user_ie(dbuf
, ds
,
449 &ds
->ds_phys
, dsl_dataset_evict
)) != NULL
) {
450 bplist_destroy(&ds
->ds_pending_deadlist
);
451 dsl_deadlist_close(&ds
->ds_deadlist
);
453 dsl_dataset_rele(ds
->ds_prev
, ds
);
454 dsl_dir_rele(ds
->ds_dir
, ds
);
455 mutex_destroy(&ds
->ds_lock
);
456 mutex_destroy(&ds
->ds_opening_lock
);
457 mutex_destroy(&ds
->ds_sendstream_lock
);
458 refcount_destroy(&ds
->ds_longholds
);
459 kmem_free(ds
, sizeof (dsl_dataset_t
));
461 dmu_buf_rele(dbuf
, tag
);
467 unique_insert(ds
->ds_phys
->ds_fsid_guid
);
470 ASSERT3P(ds
->ds_dbuf
, ==, dbuf
);
471 ASSERT3P(ds
->ds_phys
, ==, dbuf
->db_data
);
472 ASSERT(ds
->ds_phys
->ds_prev_snap_obj
!= 0 ||
473 spa_version(dp
->dp_spa
) < SPA_VERSION_ORIGIN
||
474 dp
->dp_origin_snap
== NULL
|| ds
== dp
->dp_origin_snap
);
480 dsl_dataset_hold(dsl_pool_t
*dp
, const char *name
,
481 void *tag
, dsl_dataset_t
**dsp
)
484 const char *snapname
;
488 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, &snapname
);
492 ASSERT(dsl_pool_config_held(dp
));
493 obj
= dd
->dd_phys
->dd_head_dataset_obj
;
495 err
= dsl_dataset_hold_obj(dp
, obj
, tag
, dsp
);
497 err
= SET_ERROR(ENOENT
);
499 /* we may be looking for a snapshot */
500 if (err
== 0 && snapname
!= NULL
) {
503 if (*snapname
++ != '@') {
504 dsl_dataset_rele(*dsp
, tag
);
505 dsl_dir_rele(dd
, FTAG
);
506 return (SET_ERROR(ENOENT
));
509 dprintf("looking for snapshot '%s'\n", snapname
);
510 err
= dsl_dataset_snap_lookup(*dsp
, snapname
, &obj
);
512 err
= dsl_dataset_hold_obj(dp
, obj
, tag
, &ds
);
513 dsl_dataset_rele(*dsp
, tag
);
516 mutex_enter(&ds
->ds_lock
);
517 if (ds
->ds_snapname
[0] == 0)
518 (void) strlcpy(ds
->ds_snapname
, snapname
,
519 sizeof (ds
->ds_snapname
));
520 mutex_exit(&ds
->ds_lock
);
525 dsl_dir_rele(dd
, FTAG
);
530 dsl_dataset_own_obj(dsl_pool_t
*dp
, uint64_t dsobj
,
531 void *tag
, dsl_dataset_t
**dsp
)
533 int err
= dsl_dataset_hold_obj(dp
, dsobj
, tag
, dsp
);
536 if (!dsl_dataset_tryown(*dsp
, tag
)) {
537 dsl_dataset_rele(*dsp
, tag
);
539 return (SET_ERROR(EBUSY
));
545 dsl_dataset_own(dsl_pool_t
*dp
, const char *name
,
546 void *tag
, dsl_dataset_t
**dsp
)
548 int err
= dsl_dataset_hold(dp
, name
, tag
, dsp
);
551 if (!dsl_dataset_tryown(*dsp
, tag
)) {
552 dsl_dataset_rele(*dsp
, tag
);
553 return (SET_ERROR(EBUSY
));
559 * See the comment above dsl_pool_hold() for details. In summary, a long
560 * hold is used to prevent destruction of a dataset while the pool hold
561 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
563 * The dataset and pool must be held when this function is called. After it
564 * is called, the pool hold may be released while the dataset is still held
568 dsl_dataset_long_hold(dsl_dataset_t
*ds
, void *tag
)
570 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
571 (void) refcount_add(&ds
->ds_longholds
, tag
);
575 dsl_dataset_long_rele(dsl_dataset_t
*ds
, void *tag
)
577 (void) refcount_remove(&ds
->ds_longholds
, tag
);
580 /* Return B_TRUE if there are any long holds on this dataset. */
582 dsl_dataset_long_held(dsl_dataset_t
*ds
)
584 return (!refcount_is_zero(&ds
->ds_longholds
));
588 dsl_dataset_name(dsl_dataset_t
*ds
, char *name
)
591 (void) strcpy(name
, "mos");
593 dsl_dir_name(ds
->ds_dir
, name
);
594 VERIFY0(dsl_dataset_get_snapname(ds
));
595 if (ds
->ds_snapname
[0]) {
596 (void) strcat(name
, "@");
598 * We use a "recursive" mutex so that we
599 * can call dprintf_ds() with ds_lock held.
601 if (!MUTEX_HELD(&ds
->ds_lock
)) {
602 mutex_enter(&ds
->ds_lock
);
603 (void) strcat(name
, ds
->ds_snapname
);
604 mutex_exit(&ds
->ds_lock
);
606 (void) strcat(name
, ds
->ds_snapname
);
613 dsl_dataset_rele(dsl_dataset_t
*ds
, void *tag
)
615 dmu_buf_rele(ds
->ds_dbuf
, tag
);
619 dsl_dataset_disown(dsl_dataset_t
*ds
, void *tag
)
621 ASSERT3P(ds
->ds_owner
, ==, tag
);
622 ASSERT(ds
->ds_dbuf
!= NULL
);
624 mutex_enter(&ds
->ds_lock
);
626 mutex_exit(&ds
->ds_lock
);
627 dsl_dataset_long_rele(ds
, tag
);
628 dsl_dataset_rele(ds
, tag
);
632 dsl_dataset_tryown(dsl_dataset_t
*ds
, void *tag
)
634 boolean_t gotit
= FALSE
;
636 mutex_enter(&ds
->ds_lock
);
637 if (ds
->ds_owner
== NULL
&& !DS_IS_INCONSISTENT(ds
)) {
639 dsl_dataset_long_hold(ds
, tag
);
642 mutex_exit(&ds
->ds_lock
);
647 dsl_dataset_create_sync_dd(dsl_dir_t
*dd
, dsl_dataset_t
*origin
,
648 uint64_t flags
, dmu_tx_t
*tx
)
650 dsl_pool_t
*dp
= dd
->dd_pool
;
652 dsl_dataset_phys_t
*dsphys
;
654 objset_t
*mos
= dp
->dp_meta_objset
;
657 origin
= dp
->dp_origin_snap
;
659 ASSERT(origin
== NULL
|| origin
->ds_dir
->dd_pool
== dp
);
660 ASSERT(origin
== NULL
|| origin
->ds_phys
->ds_num_children
> 0);
661 ASSERT(dmu_tx_is_syncing(tx
));
662 ASSERT(dd
->dd_phys
->dd_head_dataset_obj
== 0);
664 dsobj
= dmu_object_alloc(mos
, DMU_OT_DSL_DATASET
, 0,
665 DMU_OT_DSL_DATASET
, sizeof (dsl_dataset_phys_t
), tx
);
666 VERIFY0(dmu_bonus_hold(mos
, dsobj
, FTAG
, &dbuf
));
667 dmu_buf_will_dirty(dbuf
, tx
);
668 dsphys
= dbuf
->db_data
;
669 bzero(dsphys
, sizeof (dsl_dataset_phys_t
));
670 dsphys
->ds_dir_obj
= dd
->dd_object
;
671 dsphys
->ds_flags
= flags
;
672 dsphys
->ds_fsid_guid
= unique_create();
673 (void) random_get_pseudo_bytes((void*)&dsphys
->ds_guid
,
674 sizeof (dsphys
->ds_guid
));
675 dsphys
->ds_snapnames_zapobj
=
676 zap_create_norm(mos
, U8_TEXTPREP_TOUPPER
, DMU_OT_DSL_DS_SNAP_MAP
,
678 dsphys
->ds_creation_time
= gethrestime_sec();
679 dsphys
->ds_creation_txg
= tx
->tx_txg
== TXG_INITIAL
? 1 : tx
->tx_txg
;
681 if (origin
== NULL
) {
682 dsphys
->ds_deadlist_obj
= dsl_deadlist_alloc(mos
, tx
);
684 dsl_dataset_t
*ohds
; /* head of the origin snapshot */
686 dsphys
->ds_prev_snap_obj
= origin
->ds_object
;
687 dsphys
->ds_prev_snap_txg
=
688 origin
->ds_phys
->ds_creation_txg
;
689 dsphys
->ds_referenced_bytes
=
690 origin
->ds_phys
->ds_referenced_bytes
;
691 dsphys
->ds_compressed_bytes
=
692 origin
->ds_phys
->ds_compressed_bytes
;
693 dsphys
->ds_uncompressed_bytes
=
694 origin
->ds_phys
->ds_uncompressed_bytes
;
695 dsphys
->ds_bp
= origin
->ds_phys
->ds_bp
;
698 * Inherit flags that describe the dataset's contents
699 * (INCONSISTENT) or properties (Case Insensitive).
701 dsphys
->ds_flags
|= origin
->ds_phys
->ds_flags
&
702 (DS_FLAG_INCONSISTENT
| DS_FLAG_CI_DATASET
);
704 dmu_buf_will_dirty(origin
->ds_dbuf
, tx
);
705 origin
->ds_phys
->ds_num_children
++;
707 VERIFY0(dsl_dataset_hold_obj(dp
,
708 origin
->ds_dir
->dd_phys
->dd_head_dataset_obj
, FTAG
, &ohds
));
709 dsphys
->ds_deadlist_obj
= dsl_deadlist_clone(&ohds
->ds_deadlist
,
710 dsphys
->ds_prev_snap_txg
, dsphys
->ds_prev_snap_obj
, tx
);
711 dsl_dataset_rele(ohds
, FTAG
);
713 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_NEXT_CLONES
) {
714 if (origin
->ds_phys
->ds_next_clones_obj
== 0) {
715 origin
->ds_phys
->ds_next_clones_obj
=
717 DMU_OT_NEXT_CLONES
, DMU_OT_NONE
, 0, tx
);
719 VERIFY0(zap_add_int(mos
,
720 origin
->ds_phys
->ds_next_clones_obj
, dsobj
, tx
));
723 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
724 dd
->dd_phys
->dd_origin_obj
= origin
->ds_object
;
725 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
726 if (origin
->ds_dir
->dd_phys
->dd_clones
== 0) {
727 dmu_buf_will_dirty(origin
->ds_dir
->dd_dbuf
, tx
);
728 origin
->ds_dir
->dd_phys
->dd_clones
=
730 DMU_OT_DSL_CLONES
, DMU_OT_NONE
, 0, tx
);
732 VERIFY0(zap_add_int(mos
,
733 origin
->ds_dir
->dd_phys
->dd_clones
, dsobj
, tx
));
737 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_UNIQUE_ACCURATE
)
738 dsphys
->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
740 dmu_buf_rele(dbuf
, FTAG
);
742 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
743 dd
->dd_phys
->dd_head_dataset_obj
= dsobj
;
749 dsl_dataset_zero_zil(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
753 VERIFY0(dmu_objset_from_ds(ds
, &os
));
754 bzero(&os
->os_zil_header
, sizeof (os
->os_zil_header
));
755 dsl_dataset_dirty(ds
, tx
);
759 dsl_dataset_create_sync(dsl_dir_t
*pdd
, const char *lastname
,
760 dsl_dataset_t
*origin
, uint64_t flags
, cred_t
*cr
, dmu_tx_t
*tx
)
762 dsl_pool_t
*dp
= pdd
->dd_pool
;
763 uint64_t dsobj
, ddobj
;
766 ASSERT(dmu_tx_is_syncing(tx
));
767 ASSERT(lastname
[0] != '@');
769 ddobj
= dsl_dir_create_sync(dp
, pdd
, lastname
, tx
);
770 VERIFY0(dsl_dir_hold_obj(dp
, ddobj
, lastname
, FTAG
, &dd
));
772 dsobj
= dsl_dataset_create_sync_dd(dd
, origin
,
773 flags
& ~DS_CREATE_FLAG_NODIRTY
, tx
);
775 dsl_deleg_set_create_perms(dd
, tx
, cr
);
778 * Since we're creating a new node we know it's a leaf, so we can
779 * initialize the counts if the limit feature is active.
781 if (spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_FS_SS_LIMIT
)) {
783 objset_t
*os
= dd
->dd_pool
->dp_meta_objset
;
785 dsl_dir_zapify(dd
, tx
);
786 VERIFY0(zap_add(os
, dd
->dd_object
, DD_FIELD_FILESYSTEM_COUNT
,
787 sizeof (cnt
), 1, &cnt
, tx
));
788 VERIFY0(zap_add(os
, dd
->dd_object
, DD_FIELD_SNAPSHOT_COUNT
,
789 sizeof (cnt
), 1, &cnt
, tx
));
792 dsl_dir_rele(dd
, FTAG
);
795 * If we are creating a clone, make sure we zero out any stale
796 * data from the origin snapshots zil header.
798 if (origin
!= NULL
&& !(flags
& DS_CREATE_FLAG_NODIRTY
)) {
801 VERIFY0(dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
802 dsl_dataset_zero_zil(ds
, tx
);
803 dsl_dataset_rele(ds
, FTAG
);
810 * The unique space in the head dataset can be calculated by subtracting
811 * the space used in the most recent snapshot, that is still being used
812 * in this file system, from the space currently in use. To figure out
813 * the space in the most recent snapshot still in use, we need to take
814 * the total space used in the snapshot and subtract out the space that
815 * has been freed up since the snapshot was taken.
818 dsl_dataset_recalc_head_uniq(dsl_dataset_t
*ds
)
821 uint64_t dlused
, dlcomp
, dluncomp
;
823 ASSERT(!dsl_dataset_is_snapshot(ds
));
825 if (ds
->ds_phys
->ds_prev_snap_obj
!= 0)
826 mrs_used
= ds
->ds_prev
->ds_phys
->ds_referenced_bytes
;
830 dsl_deadlist_space(&ds
->ds_deadlist
, &dlused
, &dlcomp
, &dluncomp
);
832 ASSERT3U(dlused
, <=, mrs_used
);
833 ds
->ds_phys
->ds_unique_bytes
=
834 ds
->ds_phys
->ds_referenced_bytes
- (mrs_used
- dlused
);
836 if (spa_version(ds
->ds_dir
->dd_pool
->dp_spa
) >=
837 SPA_VERSION_UNIQUE_ACCURATE
)
838 ds
->ds_phys
->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
842 dsl_dataset_remove_from_next_clones(dsl_dataset_t
*ds
, uint64_t obj
,
845 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
847 ASSERTV(uint64_t count
);
849 ASSERT(ds
->ds_phys
->ds_num_children
>= 2);
850 err
= zap_remove_int(mos
, ds
->ds_phys
->ds_next_clones_obj
, obj
, tx
);
852 * The err should not be ENOENT, but a bug in a previous version
853 * of the code could cause upgrade_clones_cb() to not set
854 * ds_next_snap_obj when it should, leading to a missing entry.
855 * If we knew that the pool was created after
856 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
857 * ENOENT. However, at least we can check that we don't have
858 * too many entries in the next_clones_obj even after failing to
863 ASSERT0(zap_count(mos
, ds
->ds_phys
->ds_next_clones_obj
,
865 ASSERT3U(count
, <=, ds
->ds_phys
->ds_num_children
- 2);
870 dsl_dataset_get_blkptr(dsl_dataset_t
*ds
)
872 return (&ds
->ds_phys
->ds_bp
);
876 dsl_dataset_set_blkptr(dsl_dataset_t
*ds
, blkptr_t
*bp
, dmu_tx_t
*tx
)
878 ASSERT(dmu_tx_is_syncing(tx
));
879 /* If it's the meta-objset, set dp_meta_rootbp */
881 tx
->tx_pool
->dp_meta_rootbp
= *bp
;
883 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
884 ds
->ds_phys
->ds_bp
= *bp
;
889 dsl_dataset_get_spa(dsl_dataset_t
*ds
)
891 return (ds
->ds_dir
->dd_pool
->dp_spa
);
895 dsl_dataset_dirty(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
899 if (ds
== NULL
) /* this is the meta-objset */
902 ASSERT(ds
->ds_objset
!= NULL
);
904 if (ds
->ds_phys
->ds_next_snap_obj
!= 0)
905 panic("dirtying snapshot!");
907 dp
= ds
->ds_dir
->dd_pool
;
909 if (txg_list_add(&dp
->dp_dirty_datasets
, ds
, tx
->tx_txg
)) {
910 /* up the hold count until we can be written out */
911 dmu_buf_add_ref(ds
->ds_dbuf
, ds
);
916 dsl_dataset_is_dirty(dsl_dataset_t
*ds
)
920 for (t
= 0; t
< TXG_SIZE
; t
++) {
921 if (txg_list_member(&ds
->ds_dir
->dd_pool
->dp_dirty_datasets
,
929 dsl_dataset_snapshot_reserve_space(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
933 if (!dmu_tx_is_syncing(tx
))
937 * If there's an fs-only reservation, any blocks that might become
938 * owned by the snapshot dataset must be accommodated by space
939 * outside of the reservation.
941 ASSERT(ds
->ds_reserved
== 0 || DS_UNIQUE_IS_ACCURATE(ds
));
942 asize
= MIN(ds
->ds_phys
->ds_unique_bytes
, ds
->ds_reserved
);
943 if (asize
> dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
))
944 return (SET_ERROR(ENOSPC
));
947 * Propagate any reserved space for this snapshot to other
948 * snapshot checks in this sync group.
951 dsl_dir_willuse_space(ds
->ds_dir
, asize
, tx
);
956 typedef struct dsl_dataset_snapshot_arg
{
957 nvlist_t
*ddsa_snaps
;
958 nvlist_t
*ddsa_props
;
959 nvlist_t
*ddsa_errors
;
961 } dsl_dataset_snapshot_arg_t
;
964 dsl_dataset_snapshot_check_impl(dsl_dataset_t
*ds
, const char *snapname
,
965 dmu_tx_t
*tx
, boolean_t recv
, uint64_t cnt
, cred_t
*cr
)
970 ds
->ds_trysnap_txg
= tx
->tx_txg
;
972 if (!dmu_tx_is_syncing(tx
))
976 * We don't allow multiple snapshots of the same txg. If there
977 * is already one, try again.
979 if (ds
->ds_phys
->ds_prev_snap_txg
>= tx
->tx_txg
)
980 return (SET_ERROR(EAGAIN
));
983 * Check for conflicting snapshot name.
985 error
= dsl_dataset_snap_lookup(ds
, snapname
, &value
);
987 return (SET_ERROR(EEXIST
));
992 * We don't allow taking snapshots of inconsistent datasets, such as
993 * those into which we are currently receiving. However, if we are
994 * creating this snapshot as part of a receive, this check will be
995 * executed atomically with respect to the completion of the receive
996 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
997 * case we ignore this, knowing it will be fixed up for us shortly in
998 * dmu_recv_end_sync().
1000 if (!recv
&& DS_IS_INCONSISTENT(ds
))
1001 return (SET_ERROR(EBUSY
));
1004 * Skip the check for temporary snapshots or if we have already checked
1005 * the counts in dsl_dataset_snapshot_check. This means we really only
1006 * check the count here when we're receiving a stream.
1008 if (cnt
!= 0 && cr
!= NULL
) {
1009 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, cnt
,
1010 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
, cr
);
1015 error
= dsl_dataset_snapshot_reserve_space(ds
, tx
);
1023 dsl_dataset_snapshot_check(void *arg
, dmu_tx_t
*tx
)
1025 dsl_dataset_snapshot_arg_t
*ddsa
= arg
;
1026 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1031 * Pre-compute how many total new snapshots will be created for each
1032 * level in the tree and below. This is needed for validating the
1033 * snapshot limit when either taking a recursive snapshot or when
1034 * taking multiple snapshots.
1036 * The problem is that the counts are not actually adjusted when
1037 * we are checking, only when we finally sync. For a single snapshot,
1038 * this is easy, the count will increase by 1 at each node up the tree,
1039 * but its more complicated for the recursive/multiple snapshot case.
1041 * The dsl_fs_ss_limit_check function does recursively check the count
1042 * at each level up the tree but since it is validating each snapshot
1043 * independently we need to be sure that we are validating the complete
1044 * count for the entire set of snapshots. We do this by rolling up the
1045 * counts for each component of the name into an nvlist and then
1046 * checking each of those cases with the aggregated count.
1048 * This approach properly handles not only the recursive snapshot
1049 * case (where we get all of those on the ddsa_snaps list) but also
1050 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1051 * validate the limit on 'a' using a count of 2).
1053 * We validate the snapshot names in the third loop and only report
1056 if (dmu_tx_is_syncing(tx
)) {
1058 nvlist_t
*cnt_track
= NULL
;
1059 cnt_track
= fnvlist_alloc();
1061 nm
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
1063 /* Rollup aggregated counts into the cnt_track list */
1064 for (pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, NULL
);
1066 pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, pair
)) {
1070 (void) strlcpy(nm
, nvpair_name(pair
), MAXPATHLEN
);
1071 pdelim
= strchr(nm
, '@');
1077 if (nvlist_lookup_uint64(cnt_track
, nm
,
1079 /* update existing entry */
1080 fnvlist_add_uint64(cnt_track
, nm
,
1084 fnvlist_add_uint64(cnt_track
, nm
, 1);
1087 pdelim
= strrchr(nm
, '/');
1090 } while (pdelim
!= NULL
);
1093 kmem_free(nm
, MAXPATHLEN
);
1095 /* Check aggregated counts at each level */
1096 for (pair
= nvlist_next_nvpair(cnt_track
, NULL
);
1097 pair
!= NULL
; pair
= nvlist_next_nvpair(cnt_track
, pair
)) {
1103 name
= nvpair_name(pair
);
1104 cnt
= fnvpair_value_uint64(pair
);
1107 error
= dsl_dataset_hold(dp
, name
, FTAG
, &ds
);
1109 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, cnt
,
1110 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
,
1112 dsl_dataset_rele(ds
, FTAG
);
1116 if (ddsa
->ddsa_errors
!= NULL
)
1117 fnvlist_add_int32(ddsa
->ddsa_errors
,
1120 /* only report one error for this check */
1124 nvlist_free(cnt_track
);
1127 for (pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, NULL
);
1128 pair
!= NULL
; pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, pair
)) {
1132 char dsname
[MAXNAMELEN
];
1134 name
= nvpair_name(pair
);
1135 if (strlen(name
) >= MAXNAMELEN
)
1136 error
= SET_ERROR(ENAMETOOLONG
);
1138 atp
= strchr(name
, '@');
1140 error
= SET_ERROR(EINVAL
);
1142 (void) strlcpy(dsname
, name
, atp
- name
+ 1);
1145 error
= dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
);
1147 /* passing 0/NULL skips dsl_fs_ss_limit_check */
1148 error
= dsl_dataset_snapshot_check_impl(ds
,
1149 atp
+ 1, tx
, B_FALSE
, 0, NULL
);
1150 dsl_dataset_rele(ds
, FTAG
);
1154 if (ddsa
->ddsa_errors
!= NULL
) {
1155 fnvlist_add_int32(ddsa
->ddsa_errors
,
1166 dsl_dataset_snapshot_sync_impl(dsl_dataset_t
*ds
, const char *snapname
,
1169 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1171 dsl_dataset_phys_t
*dsphys
;
1172 uint64_t dsobj
, crtxg
;
1173 objset_t
*mos
= dp
->dp_meta_objset
;
1174 ASSERTV(static zil_header_t zero_zil
);
1175 ASSERTV(objset_t
*os
);
1177 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
1180 * If we are on an old pool, the zil must not be active, in which
1181 * case it will be zeroed. Usually zil_suspend() accomplishes this.
1183 ASSERT(spa_version(dmu_tx_pool(tx
)->dp_spa
) >= SPA_VERSION_FAST_SNAP
||
1184 dmu_objset_from_ds(ds
, &os
) != 0 ||
1185 bcmp(&os
->os_phys
->os_zil_header
, &zero_zil
,
1186 sizeof (zero_zil
)) == 0);
1188 dsl_fs_ss_count_adjust(ds
->ds_dir
, 1, DD_FIELD_SNAPSHOT_COUNT
, tx
);
1191 * The origin's ds_creation_txg has to be < TXG_INITIAL
1193 if (strcmp(snapname
, ORIGIN_DIR_NAME
) == 0)
1198 dsobj
= dmu_object_alloc(mos
, DMU_OT_DSL_DATASET
, 0,
1199 DMU_OT_DSL_DATASET
, sizeof (dsl_dataset_phys_t
), tx
);
1200 VERIFY0(dmu_bonus_hold(mos
, dsobj
, FTAG
, &dbuf
));
1201 dmu_buf_will_dirty(dbuf
, tx
);
1202 dsphys
= dbuf
->db_data
;
1203 bzero(dsphys
, sizeof (dsl_dataset_phys_t
));
1204 dsphys
->ds_dir_obj
= ds
->ds_dir
->dd_object
;
1205 dsphys
->ds_fsid_guid
= unique_create();
1206 (void) random_get_pseudo_bytes((void*)&dsphys
->ds_guid
,
1207 sizeof (dsphys
->ds_guid
));
1208 dsphys
->ds_prev_snap_obj
= ds
->ds_phys
->ds_prev_snap_obj
;
1209 dsphys
->ds_prev_snap_txg
= ds
->ds_phys
->ds_prev_snap_txg
;
1210 dsphys
->ds_next_snap_obj
= ds
->ds_object
;
1211 dsphys
->ds_num_children
= 1;
1212 dsphys
->ds_creation_time
= gethrestime_sec();
1213 dsphys
->ds_creation_txg
= crtxg
;
1214 dsphys
->ds_deadlist_obj
= ds
->ds_phys
->ds_deadlist_obj
;
1215 dsphys
->ds_referenced_bytes
= ds
->ds_phys
->ds_referenced_bytes
;
1216 dsphys
->ds_compressed_bytes
= ds
->ds_phys
->ds_compressed_bytes
;
1217 dsphys
->ds_uncompressed_bytes
= ds
->ds_phys
->ds_uncompressed_bytes
;
1218 dsphys
->ds_flags
= ds
->ds_phys
->ds_flags
;
1219 dsphys
->ds_bp
= ds
->ds_phys
->ds_bp
;
1220 dmu_buf_rele(dbuf
, FTAG
);
1222 ASSERT3U(ds
->ds_prev
!= 0, ==, ds
->ds_phys
->ds_prev_snap_obj
!= 0);
1224 uint64_t next_clones_obj
=
1225 ds
->ds_prev
->ds_phys
->ds_next_clones_obj
;
1226 ASSERT(ds
->ds_prev
->ds_phys
->ds_next_snap_obj
==
1228 ds
->ds_prev
->ds_phys
->ds_num_children
> 1);
1229 if (ds
->ds_prev
->ds_phys
->ds_next_snap_obj
== ds
->ds_object
) {
1230 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
1231 ASSERT3U(ds
->ds_phys
->ds_prev_snap_txg
, ==,
1232 ds
->ds_prev
->ds_phys
->ds_creation_txg
);
1233 ds
->ds_prev
->ds_phys
->ds_next_snap_obj
= dsobj
;
1234 } else if (next_clones_obj
!= 0) {
1235 dsl_dataset_remove_from_next_clones(ds
->ds_prev
,
1236 dsphys
->ds_next_snap_obj
, tx
);
1237 VERIFY0(zap_add_int(mos
,
1238 next_clones_obj
, dsobj
, tx
));
1243 * If we have a reference-reservation on this dataset, we will
1244 * need to increase the amount of refreservation being charged
1245 * since our unique space is going to zero.
1247 if (ds
->ds_reserved
) {
1249 ASSERT(DS_UNIQUE_IS_ACCURATE(ds
));
1250 delta
= MIN(ds
->ds_phys
->ds_unique_bytes
, ds
->ds_reserved
);
1251 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_REFRSRV
,
1255 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1256 ds
->ds_phys
->ds_deadlist_obj
= dsl_deadlist_clone(&ds
->ds_deadlist
,
1257 UINT64_MAX
, ds
->ds_phys
->ds_prev_snap_obj
, tx
);
1258 dsl_deadlist_close(&ds
->ds_deadlist
);
1259 dsl_deadlist_open(&ds
->ds_deadlist
, mos
, ds
->ds_phys
->ds_deadlist_obj
);
1260 dsl_deadlist_add_key(&ds
->ds_deadlist
,
1261 ds
->ds_phys
->ds_prev_snap_txg
, tx
);
1263 ASSERT3U(ds
->ds_phys
->ds_prev_snap_txg
, <, tx
->tx_txg
);
1264 ds
->ds_phys
->ds_prev_snap_obj
= dsobj
;
1265 ds
->ds_phys
->ds_prev_snap_txg
= crtxg
;
1266 ds
->ds_phys
->ds_unique_bytes
= 0;
1267 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_UNIQUE_ACCURATE
)
1268 ds
->ds_phys
->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
1270 VERIFY0(zap_add(mos
, ds
->ds_phys
->ds_snapnames_zapobj
,
1271 snapname
, 8, 1, &dsobj
, tx
));
1274 dsl_dataset_rele(ds
->ds_prev
, ds
);
1275 VERIFY0(dsl_dataset_hold_obj(dp
,
1276 ds
->ds_phys
->ds_prev_snap_obj
, ds
, &ds
->ds_prev
));
1278 dsl_scan_ds_snapshotted(ds
, tx
);
1280 dsl_dir_snap_cmtime_update(ds
->ds_dir
);
1282 spa_history_log_internal_ds(ds
->ds_prev
, "snapshot", tx
, "");
1286 dsl_dataset_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
1288 dsl_dataset_snapshot_arg_t
*ddsa
= arg
;
1289 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1292 for (pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, NULL
);
1293 pair
!= NULL
; pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, pair
)) {
1296 char dsname
[MAXNAMELEN
];
1298 name
= nvpair_name(pair
);
1299 atp
= strchr(name
, '@');
1300 (void) strlcpy(dsname
, name
, atp
- name
+ 1);
1301 VERIFY0(dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
));
1303 dsl_dataset_snapshot_sync_impl(ds
, atp
+ 1, tx
);
1304 if (ddsa
->ddsa_props
!= NULL
) {
1305 dsl_props_set_sync_impl(ds
->ds_prev
,
1306 ZPROP_SRC_LOCAL
, ddsa
->ddsa_props
, tx
);
1308 dsl_dataset_rele(ds
, FTAG
);
1313 * The snapshots must all be in the same pool.
1314 * All-or-nothing: if there are any failures, nothing will be modified.
1317 dsl_dataset_snapshot(nvlist_t
*snaps
, nvlist_t
*props
, nvlist_t
*errors
)
1319 dsl_dataset_snapshot_arg_t ddsa
;
1321 boolean_t needsuspend
;
1325 nvlist_t
*suspended
= NULL
;
1327 pair
= nvlist_next_nvpair(snaps
, NULL
);
1330 firstname
= nvpair_name(pair
);
1332 error
= spa_open(firstname
, &spa
, FTAG
);
1335 needsuspend
= (spa_version(spa
) < SPA_VERSION_FAST_SNAP
);
1336 spa_close(spa
, FTAG
);
1339 suspended
= fnvlist_alloc();
1340 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
1341 pair
= nvlist_next_nvpair(snaps
, pair
)) {
1342 char fsname
[MAXNAMELEN
];
1343 char *snapname
= nvpair_name(pair
);
1347 atp
= strchr(snapname
, '@');
1349 error
= SET_ERROR(EINVAL
);
1352 (void) strlcpy(fsname
, snapname
, atp
- snapname
+ 1);
1354 error
= zil_suspend(fsname
, &cookie
);
1357 fnvlist_add_uint64(suspended
, fsname
,
1362 ddsa
.ddsa_snaps
= snaps
;
1363 ddsa
.ddsa_props
= props
;
1364 ddsa
.ddsa_errors
= errors
;
1365 ddsa
.ddsa_cr
= CRED();
1368 error
= dsl_sync_task(firstname
, dsl_dataset_snapshot_check
,
1369 dsl_dataset_snapshot_sync
, &ddsa
,
1370 fnvlist_num_pairs(snaps
) * 3);
1373 if (suspended
!= NULL
) {
1374 for (pair
= nvlist_next_nvpair(suspended
, NULL
); pair
!= NULL
;
1375 pair
= nvlist_next_nvpair(suspended
, pair
)) {
1376 zil_resume((void *)(uintptr_t)
1377 fnvpair_value_uint64(pair
));
1379 fnvlist_free(suspended
);
1384 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
1385 pair
= nvlist_next_nvpair(snaps
, pair
)) {
1386 char *snapname
= nvpair_name(pair
);
1387 zvol_create_minors(snapname
);
1395 typedef struct dsl_dataset_snapshot_tmp_arg
{
1396 const char *ddsta_fsname
;
1397 const char *ddsta_snapname
;
1398 minor_t ddsta_cleanup_minor
;
1399 const char *ddsta_htag
;
1400 } dsl_dataset_snapshot_tmp_arg_t
;
1403 dsl_dataset_snapshot_tmp_check(void *arg
, dmu_tx_t
*tx
)
1405 dsl_dataset_snapshot_tmp_arg_t
*ddsta
= arg
;
1406 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1410 error
= dsl_dataset_hold(dp
, ddsta
->ddsta_fsname
, FTAG
, &ds
);
1414 /* NULL cred means no limit check for tmp snapshot */
1415 error
= dsl_dataset_snapshot_check_impl(ds
, ddsta
->ddsta_snapname
,
1416 tx
, B_FALSE
, 0, NULL
);
1418 dsl_dataset_rele(ds
, FTAG
);
1422 if (spa_version(dp
->dp_spa
) < SPA_VERSION_USERREFS
) {
1423 dsl_dataset_rele(ds
, FTAG
);
1424 return (SET_ERROR(ENOTSUP
));
1426 error
= dsl_dataset_user_hold_check_one(NULL
, ddsta
->ddsta_htag
,
1429 dsl_dataset_rele(ds
, FTAG
);
1433 dsl_dataset_rele(ds
, FTAG
);
1438 dsl_dataset_snapshot_tmp_sync(void *arg
, dmu_tx_t
*tx
)
1440 dsl_dataset_snapshot_tmp_arg_t
*ddsta
= arg
;
1441 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1444 VERIFY0(dsl_dataset_hold(dp
, ddsta
->ddsta_fsname
, FTAG
, &ds
));
1446 dsl_dataset_snapshot_sync_impl(ds
, ddsta
->ddsta_snapname
, tx
);
1447 dsl_dataset_user_hold_sync_one(ds
->ds_prev
, ddsta
->ddsta_htag
,
1448 ddsta
->ddsta_cleanup_minor
, gethrestime_sec(), tx
);
1449 dsl_destroy_snapshot_sync_impl(ds
->ds_prev
, B_TRUE
, tx
);
1451 dsl_dataset_rele(ds
, FTAG
);
1455 dsl_dataset_snapshot_tmp(const char *fsname
, const char *snapname
,
1456 minor_t cleanup_minor
, const char *htag
)
1458 dsl_dataset_snapshot_tmp_arg_t ddsta
;
1461 boolean_t needsuspend
;
1464 ddsta
.ddsta_fsname
= fsname
;
1465 ddsta
.ddsta_snapname
= snapname
;
1466 ddsta
.ddsta_cleanup_minor
= cleanup_minor
;
1467 ddsta
.ddsta_htag
= htag
;
1469 error
= spa_open(fsname
, &spa
, FTAG
);
1472 needsuspend
= (spa_version(spa
) < SPA_VERSION_FAST_SNAP
);
1473 spa_close(spa
, FTAG
);
1476 error
= zil_suspend(fsname
, &cookie
);
1481 error
= dsl_sync_task(fsname
, dsl_dataset_snapshot_tmp_check
,
1482 dsl_dataset_snapshot_tmp_sync
, &ddsta
, 3);
1491 dsl_dataset_sync(dsl_dataset_t
*ds
, zio_t
*zio
, dmu_tx_t
*tx
)
1493 ASSERT(dmu_tx_is_syncing(tx
));
1494 ASSERT(ds
->ds_objset
!= NULL
);
1495 ASSERT(ds
->ds_phys
->ds_next_snap_obj
== 0);
1498 * in case we had to change ds_fsid_guid when we opened it,
1501 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1502 ds
->ds_phys
->ds_fsid_guid
= ds
->ds_fsid_guid
;
1504 dmu_objset_sync(ds
->ds_objset
, zio
, tx
);
1508 get_clones_stat(dsl_dataset_t
*ds
, nvlist_t
*nv
)
1511 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
1514 nvlist_t
*propval
= fnvlist_alloc();
1515 nvlist_t
*val
= fnvlist_alloc();
1517 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1520 * There may be missing entries in ds_next_clones_obj
1521 * due to a bug in a previous version of the code.
1522 * Only trust it if it has the right number of entries.
1524 if (ds
->ds_phys
->ds_next_clones_obj
!= 0) {
1525 VERIFY0(zap_count(mos
, ds
->ds_phys
->ds_next_clones_obj
,
1528 if (count
!= ds
->ds_phys
->ds_num_children
- 1)
1530 for (zap_cursor_init(&zc
, mos
, ds
->ds_phys
->ds_next_clones_obj
);
1531 zap_cursor_retrieve(&zc
, &za
) == 0;
1532 zap_cursor_advance(&zc
)) {
1533 dsl_dataset_t
*clone
;
1534 char buf
[ZFS_MAXNAMELEN
];
1535 VERIFY0(dsl_dataset_hold_obj(ds
->ds_dir
->dd_pool
,
1536 za
.za_first_integer
, FTAG
, &clone
));
1537 dsl_dir_name(clone
->ds_dir
, buf
);
1538 fnvlist_add_boolean(val
, buf
);
1539 dsl_dataset_rele(clone
, FTAG
);
1541 zap_cursor_fini(&zc
);
1542 fnvlist_add_nvlist(propval
, ZPROP_VALUE
, val
);
1543 fnvlist_add_nvlist(nv
, zfs_prop_to_name(ZFS_PROP_CLONES
), propval
);
1546 nvlist_free(propval
);
1550 dsl_dataset_stats(dsl_dataset_t
*ds
, nvlist_t
*nv
)
1552 uint64_t refd
, avail
, uobjs
, aobjs
, ratio
;
1553 ASSERTV(dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
);
1555 ASSERT(dsl_pool_config_held(dp
));
1557 ratio
= ds
->ds_phys
->ds_compressed_bytes
== 0 ? 100 :
1558 (ds
->ds_phys
->ds_uncompressed_bytes
* 100 /
1559 ds
->ds_phys
->ds_compressed_bytes
);
1561 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFRATIO
, ratio
);
1562 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_LOGICALREFERENCED
,
1563 ds
->ds_phys
->ds_uncompressed_bytes
);
1565 if (dsl_dataset_is_snapshot(ds
)) {
1566 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_COMPRESSRATIO
, ratio
);
1567 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USED
,
1568 ds
->ds_phys
->ds_unique_bytes
);
1569 get_clones_stat(ds
, nv
);
1571 dsl_dir_stats(ds
->ds_dir
, nv
);
1574 dsl_dataset_space(ds
, &refd
, &avail
, &uobjs
, &aobjs
);
1575 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_AVAILABLE
, avail
);
1576 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFERENCED
, refd
);
1578 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_CREATION
,
1579 ds
->ds_phys
->ds_creation_time
);
1580 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_CREATETXG
,
1581 ds
->ds_phys
->ds_creation_txg
);
1582 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFQUOTA
,
1584 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFRESERVATION
,
1586 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_GUID
,
1587 ds
->ds_phys
->ds_guid
);
1588 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_UNIQUE
,
1589 ds
->ds_phys
->ds_unique_bytes
);
1590 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_OBJSETID
,
1592 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERREFS
,
1594 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_DEFER_DESTROY
,
1595 DS_IS_DEFER_DESTROY(ds
) ? 1 : 0);
1597 if (ds
->ds_phys
->ds_prev_snap_obj
!= 0) {
1598 uint64_t written
, comp
, uncomp
;
1599 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1600 dsl_dataset_t
*prev
;
1603 err
= dsl_dataset_hold_obj(dp
,
1604 ds
->ds_phys
->ds_prev_snap_obj
, FTAG
, &prev
);
1606 err
= dsl_dataset_space_written(prev
, ds
, &written
,
1608 dsl_dataset_rele(prev
, FTAG
);
1610 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_WRITTEN
,
1619 dsl_dataset_fast_stat(dsl_dataset_t
*ds
, dmu_objset_stats_t
*stat
)
1621 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1622 ASSERT(dsl_pool_config_held(dp
));
1624 stat
->dds_creation_txg
= ds
->ds_phys
->ds_creation_txg
;
1625 stat
->dds_inconsistent
= ds
->ds_phys
->ds_flags
& DS_FLAG_INCONSISTENT
;
1626 stat
->dds_guid
= ds
->ds_phys
->ds_guid
;
1627 stat
->dds_origin
[0] = '\0';
1628 if (dsl_dataset_is_snapshot(ds
)) {
1629 stat
->dds_is_snapshot
= B_TRUE
;
1630 stat
->dds_num_clones
= ds
->ds_phys
->ds_num_children
- 1;
1632 stat
->dds_is_snapshot
= B_FALSE
;
1633 stat
->dds_num_clones
= 0;
1635 if (dsl_dir_is_clone(ds
->ds_dir
)) {
1638 VERIFY0(dsl_dataset_hold_obj(dp
,
1639 ds
->ds_dir
->dd_phys
->dd_origin_obj
, FTAG
, &ods
));
1640 dsl_dataset_name(ods
, stat
->dds_origin
);
1641 dsl_dataset_rele(ods
, FTAG
);
1647 dsl_dataset_fsid_guid(dsl_dataset_t
*ds
)
1649 return (ds
->ds_fsid_guid
);
1653 dsl_dataset_space(dsl_dataset_t
*ds
,
1654 uint64_t *refdbytesp
, uint64_t *availbytesp
,
1655 uint64_t *usedobjsp
, uint64_t *availobjsp
)
1657 *refdbytesp
= ds
->ds_phys
->ds_referenced_bytes
;
1658 *availbytesp
= dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
);
1659 if (ds
->ds_reserved
> ds
->ds_phys
->ds_unique_bytes
)
1660 *availbytesp
+= ds
->ds_reserved
- ds
->ds_phys
->ds_unique_bytes
;
1661 if (ds
->ds_quota
!= 0) {
1663 * Adjust available bytes according to refquota
1665 if (*refdbytesp
< ds
->ds_quota
)
1666 *availbytesp
= MIN(*availbytesp
,
1667 ds
->ds_quota
- *refdbytesp
);
1671 *usedobjsp
= BP_GET_FILL(&ds
->ds_phys
->ds_bp
);
1672 *availobjsp
= DN_MAX_OBJECT
- *usedobjsp
;
1676 dsl_dataset_modified_since_snap(dsl_dataset_t
*ds
, dsl_dataset_t
*snap
)
1678 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1681 if (ds
->ds_phys
->ds_bp
.blk_birth
>
1682 snap
->ds_phys
->ds_creation_txg
) {
1683 objset_t
*os
, *os_snap
;
1685 * It may be that only the ZIL differs, because it was
1686 * reset in the head. Don't count that as being
1689 if (dmu_objset_from_ds(ds
, &os
) != 0)
1691 if (dmu_objset_from_ds(snap
, &os_snap
) != 0)
1693 return (bcmp(&os
->os_phys
->os_meta_dnode
,
1694 &os_snap
->os_phys
->os_meta_dnode
,
1695 sizeof (os
->os_phys
->os_meta_dnode
)) != 0);
1700 typedef struct dsl_dataset_rename_snapshot_arg
{
1701 const char *ddrsa_fsname
;
1702 const char *ddrsa_oldsnapname
;
1703 const char *ddrsa_newsnapname
;
1704 boolean_t ddrsa_recursive
;
1706 } dsl_dataset_rename_snapshot_arg_t
;
1710 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t
*dp
,
1711 dsl_dataset_t
*hds
, void *arg
)
1713 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1717 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_oldsnapname
, &val
);
1719 /* ignore nonexistent snapshots */
1720 return (error
== ENOENT
? 0 : error
);
1723 /* new name should not exist */
1724 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_newsnapname
, &val
);
1726 error
= SET_ERROR(EEXIST
);
1727 else if (error
== ENOENT
)
1730 /* dataset name + 1 for the "@" + the new snapshot name must fit */
1731 if (dsl_dir_namelen(hds
->ds_dir
) + 1 +
1732 strlen(ddrsa
->ddrsa_newsnapname
) >= MAXNAMELEN
)
1733 error
= SET_ERROR(ENAMETOOLONG
);
1739 dsl_dataset_rename_snapshot_check(void *arg
, dmu_tx_t
*tx
)
1741 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1742 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1746 error
= dsl_dataset_hold(dp
, ddrsa
->ddrsa_fsname
, FTAG
, &hds
);
1750 if (ddrsa
->ddrsa_recursive
) {
1751 error
= dmu_objset_find_dp(dp
, hds
->ds_dir
->dd_object
,
1752 dsl_dataset_rename_snapshot_check_impl
, ddrsa
,
1755 error
= dsl_dataset_rename_snapshot_check_impl(dp
, hds
, ddrsa
);
1757 dsl_dataset_rele(hds
, FTAG
);
1762 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t
*dp
,
1763 dsl_dataset_t
*hds
, void *arg
)
1765 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1768 dmu_tx_t
*tx
= ddrsa
->ddrsa_tx
;
1771 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_oldsnapname
, &val
);
1772 ASSERT(error
== 0 || error
== ENOENT
);
1773 if (error
== ENOENT
) {
1774 /* ignore nonexistent snapshots */
1778 VERIFY0(dsl_dataset_hold_obj(dp
, val
, FTAG
, &ds
));
1780 /* log before we change the name */
1781 spa_history_log_internal_ds(ds
, "rename", tx
,
1782 "-> @%s", ddrsa
->ddrsa_newsnapname
);
1784 VERIFY0(dsl_dataset_snap_remove(hds
, ddrsa
->ddrsa_oldsnapname
, tx
,
1786 mutex_enter(&ds
->ds_lock
);
1787 (void) strcpy(ds
->ds_snapname
, ddrsa
->ddrsa_newsnapname
);
1788 mutex_exit(&ds
->ds_lock
);
1789 VERIFY0(zap_add(dp
->dp_meta_objset
, hds
->ds_phys
->ds_snapnames_zapobj
,
1790 ds
->ds_snapname
, 8, 1, &ds
->ds_object
, tx
));
1792 dsl_dataset_rele(ds
, FTAG
);
1797 dsl_dataset_rename_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
1799 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1800 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1803 VERIFY0(dsl_dataset_hold(dp
, ddrsa
->ddrsa_fsname
, FTAG
, &hds
));
1804 ddrsa
->ddrsa_tx
= tx
;
1805 if (ddrsa
->ddrsa_recursive
) {
1806 VERIFY0(dmu_objset_find_dp(dp
, hds
->ds_dir
->dd_object
,
1807 dsl_dataset_rename_snapshot_sync_impl
, ddrsa
,
1810 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp
, hds
, ddrsa
));
1812 dsl_dataset_rele(hds
, FTAG
);
1816 dsl_dataset_rename_snapshot(const char *fsname
,
1817 const char *oldsnapname
, const char *newsnapname
, boolean_t recursive
)
1820 char *oldname
, *newname
;
1824 dsl_dataset_rename_snapshot_arg_t ddrsa
;
1826 ddrsa
.ddrsa_fsname
= fsname
;
1827 ddrsa
.ddrsa_oldsnapname
= oldsnapname
;
1828 ddrsa
.ddrsa_newsnapname
= newsnapname
;
1829 ddrsa
.ddrsa_recursive
= recursive
;
1831 error
= dsl_sync_task(fsname
, dsl_dataset_rename_snapshot_check
,
1832 dsl_dataset_rename_snapshot_sync
, &ddrsa
, 1);
1835 return (SET_ERROR(error
));
1838 oldname
= kmem_asprintf("%s@%s", fsname
, oldsnapname
);
1839 newname
= kmem_asprintf("%s@%s", fsname
, newsnapname
);
1840 zvol_rename_minors(oldname
, newname
);
1849 * If we're doing an ownership handoff, we need to make sure that there is
1850 * only one long hold on the dataset. We're not allowed to change anything here
1851 * so we don't permanently release the long hold or regular hold here. We want
1852 * to do this only when syncing to avoid the dataset unexpectedly going away
1853 * when we release the long hold.
1856 dsl_dataset_handoff_check(dsl_dataset_t
*ds
, void *owner
, dmu_tx_t
*tx
)
1860 if (!dmu_tx_is_syncing(tx
))
1863 if (owner
!= NULL
) {
1864 VERIFY3P(ds
->ds_owner
, ==, owner
);
1865 dsl_dataset_long_rele(ds
, owner
);
1868 held
= dsl_dataset_long_held(ds
);
1871 dsl_dataset_long_hold(ds
, owner
);
1874 return (SET_ERROR(EBUSY
));
1879 typedef struct dsl_dataset_rollback_arg
{
1880 const char *ddra_fsname
;
1882 nvlist_t
*ddra_result
;
1883 } dsl_dataset_rollback_arg_t
;
1886 dsl_dataset_rollback_check(void *arg
, dmu_tx_t
*tx
)
1888 dsl_dataset_rollback_arg_t
*ddra
= arg
;
1889 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1891 int64_t unused_refres_delta
;
1894 nvlist_t
*proprequest
, *bookmarks
;
1896 error
= dsl_dataset_hold(dp
, ddra
->ddra_fsname
, FTAG
, &ds
);
1900 /* must not be a snapshot */
1901 if (dsl_dataset_is_snapshot(ds
)) {
1902 dsl_dataset_rele(ds
, FTAG
);
1903 return (SET_ERROR(EINVAL
));
1906 /* must have a most recent snapshot */
1907 if (ds
->ds_phys
->ds_prev_snap_txg
< TXG_INITIAL
) {
1908 dsl_dataset_rele(ds
, FTAG
);
1909 return (SET_ERROR(EINVAL
));
1912 /* must not have any bookmarks after the most recent snapshot */
1913 proprequest
= fnvlist_alloc();
1914 fnvlist_add_boolean(proprequest
, zfs_prop_to_name(ZFS_PROP_CREATETXG
));
1915 bookmarks
= fnvlist_alloc();
1916 error
= dsl_get_bookmarks_impl(ds
, proprequest
, bookmarks
);
1917 fnvlist_free(proprequest
);
1920 for (pair
= nvlist_next_nvpair(bookmarks
, NULL
);
1921 pair
!= NULL
; pair
= nvlist_next_nvpair(bookmarks
, pair
)) {
1923 fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair
),
1924 zfs_prop_to_name(ZFS_PROP_CREATETXG
));
1925 uint64_t createtxg
= fnvlist_lookup_uint64(valuenv
, "value");
1926 if (createtxg
> ds
->ds_phys
->ds_prev_snap_txg
) {
1927 fnvlist_free(bookmarks
);
1928 dsl_dataset_rele(ds
, FTAG
);
1929 return (SET_ERROR(EEXIST
));
1932 fnvlist_free(bookmarks
);
1934 error
= dsl_dataset_handoff_check(ds
, ddra
->ddra_owner
, tx
);
1936 dsl_dataset_rele(ds
, FTAG
);
1941 * Check if the snap we are rolling back to uses more than
1944 if (ds
->ds_quota
!= 0 &&
1945 ds
->ds_prev
->ds_phys
->ds_referenced_bytes
> ds
->ds_quota
) {
1946 dsl_dataset_rele(ds
, FTAG
);
1947 return (SET_ERROR(EDQUOT
));
1951 * When we do the clone swap, we will temporarily use more space
1952 * due to the refreservation (the head will no longer have any
1953 * unique space, so the entire amount of the refreservation will need
1954 * to be free). We will immediately destroy the clone, freeing
1955 * this space, but the freeing happens over many txg's.
1957 unused_refres_delta
= (int64_t)MIN(ds
->ds_reserved
,
1958 ds
->ds_phys
->ds_unique_bytes
);
1960 if (unused_refres_delta
> 0 &&
1961 unused_refres_delta
>
1962 dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
)) {
1963 dsl_dataset_rele(ds
, FTAG
);
1964 return (SET_ERROR(ENOSPC
));
1967 dsl_dataset_rele(ds
, FTAG
);
1972 dsl_dataset_rollback_sync(void *arg
, dmu_tx_t
*tx
)
1974 dsl_dataset_rollback_arg_t
*ddra
= arg
;
1975 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1976 dsl_dataset_t
*ds
, *clone
;
1978 char namebuf
[ZFS_MAXNAMELEN
];
1980 VERIFY0(dsl_dataset_hold(dp
, ddra
->ddra_fsname
, FTAG
, &ds
));
1982 dsl_dataset_name(ds
->ds_prev
, namebuf
);
1983 fnvlist_add_string(ddra
->ddra_result
, "target", namebuf
);
1985 cloneobj
= dsl_dataset_create_sync(ds
->ds_dir
, "%rollback",
1986 ds
->ds_prev
, DS_CREATE_FLAG_NODIRTY
, kcred
, tx
);
1988 VERIFY0(dsl_dataset_hold_obj(dp
, cloneobj
, FTAG
, &clone
));
1990 dsl_dataset_clone_swap_sync_impl(clone
, ds
, tx
);
1991 dsl_dataset_zero_zil(ds
, tx
);
1993 dsl_destroy_head_sync_impl(clone
, tx
);
1995 dsl_dataset_rele(clone
, FTAG
);
1996 dsl_dataset_rele(ds
, FTAG
);
2000 * Rolls back the given filesystem or volume to the most recent snapshot.
2001 * The name of the most recent snapshot will be returned under key "target"
2002 * in the result nvlist.
2005 * - The existing dataset MUST be owned by the specified owner at entry
2006 * - Upon return, dataset will still be held by the same owner, whether we
2009 * This mode is required any time the existing filesystem is mounted. See
2010 * notes above zfs_suspend_fs() for further details.
2013 dsl_dataset_rollback(const char *fsname
, void *owner
, nvlist_t
*result
)
2015 dsl_dataset_rollback_arg_t ddra
;
2017 ddra
.ddra_fsname
= fsname
;
2018 ddra
.ddra_owner
= owner
;
2019 ddra
.ddra_result
= result
;
2021 return (dsl_sync_task(fsname
, dsl_dataset_rollback_check
,
2022 dsl_dataset_rollback_sync
, &ddra
, 1));
2025 struct promotenode
{
2030 typedef struct dsl_dataset_promote_arg
{
2031 const char *ddpa_clonename
;
2032 dsl_dataset_t
*ddpa_clone
;
2033 list_t shared_snaps
, origin_snaps
, clone_snaps
;
2034 dsl_dataset_t
*origin_origin
; /* origin of the origin */
2035 uint64_t used
, comp
, uncomp
, unique
, cloneusedsnap
, originusedsnap
;
2038 } dsl_dataset_promote_arg_t
;
2040 static int snaplist_space(list_t
*l
, uint64_t mintxg
, uint64_t *spacep
);
2041 static int promote_hold(dsl_dataset_promote_arg_t
*ddpa
, dsl_pool_t
*dp
,
2043 static void promote_rele(dsl_dataset_promote_arg_t
*ddpa
, void *tag
);
2046 dsl_dataset_promote_check(void *arg
, dmu_tx_t
*tx
)
2048 dsl_dataset_promote_arg_t
*ddpa
= arg
;
2049 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2051 struct promotenode
*snap
;
2052 dsl_dataset_t
*origin_ds
;
2057 err
= promote_hold(ddpa
, dp
, FTAG
);
2061 hds
= ddpa
->ddpa_clone
;
2063 if (hds
->ds_phys
->ds_flags
& DS_FLAG_NOPROMOTE
) {
2064 promote_rele(ddpa
, FTAG
);
2065 return (SET_ERROR(EXDEV
));
2069 * Compute and check the amount of space to transfer. Since this is
2070 * so expensive, don't do the preliminary check.
2072 if (!dmu_tx_is_syncing(tx
)) {
2073 promote_rele(ddpa
, FTAG
);
2077 snap
= list_head(&ddpa
->shared_snaps
);
2078 origin_ds
= snap
->ds
;
2080 /* compute origin's new unique space */
2081 snap
= list_tail(&ddpa
->clone_snaps
);
2082 ASSERT3U(snap
->ds
->ds_phys
->ds_prev_snap_obj
, ==, origin_ds
->ds_object
);
2083 dsl_deadlist_space_range(&snap
->ds
->ds_deadlist
,
2084 origin_ds
->ds_phys
->ds_prev_snap_txg
, UINT64_MAX
,
2085 &ddpa
->unique
, &unused
, &unused
);
2088 * Walk the snapshots that we are moving
2090 * Compute space to transfer. Consider the incremental changes
2091 * to used by each snapshot:
2092 * (my used) = (prev's used) + (blocks born) - (blocks killed)
2093 * So each snapshot gave birth to:
2094 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2095 * So a sequence would look like:
2096 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2097 * Which simplifies to:
2098 * uN + kN + kN-1 + ... + k1 + k0
2099 * Note however, if we stop before we reach the ORIGIN we get:
2100 * uN + kN + kN-1 + ... + kM - uM-1
2103 ddpa
->used
= origin_ds
->ds_phys
->ds_referenced_bytes
;
2104 ddpa
->comp
= origin_ds
->ds_phys
->ds_compressed_bytes
;
2105 ddpa
->uncomp
= origin_ds
->ds_phys
->ds_uncompressed_bytes
;
2106 for (snap
= list_head(&ddpa
->shared_snaps
); snap
;
2107 snap
= list_next(&ddpa
->shared_snaps
, snap
)) {
2108 uint64_t val
, dlused
, dlcomp
, dluncomp
;
2109 dsl_dataset_t
*ds
= snap
->ds
;
2114 * If there are long holds, we won't be able to evict
2117 if (dsl_dataset_long_held(ds
)) {
2118 err
= SET_ERROR(EBUSY
);
2122 /* Check that the snapshot name does not conflict */
2123 VERIFY0(dsl_dataset_get_snapname(ds
));
2124 err
= dsl_dataset_snap_lookup(hds
, ds
->ds_snapname
, &val
);
2126 (void) strcpy(ddpa
->err_ds
, snap
->ds
->ds_snapname
);
2127 err
= SET_ERROR(EEXIST
);
2133 /* The very first snapshot does not have a deadlist */
2134 if (ds
->ds_phys
->ds_prev_snap_obj
== 0)
2137 dsl_deadlist_space(&ds
->ds_deadlist
,
2138 &dlused
, &dlcomp
, &dluncomp
);
2139 ddpa
->used
+= dlused
;
2140 ddpa
->comp
+= dlcomp
;
2141 ddpa
->uncomp
+= dluncomp
;
2145 * If we are a clone of a clone then we never reached ORIGIN,
2146 * so we need to subtract out the clone origin's used space.
2148 if (ddpa
->origin_origin
) {
2149 ddpa
->used
-= ddpa
->origin_origin
->ds_phys
->ds_referenced_bytes
;
2150 ddpa
->comp
-= ddpa
->origin_origin
->ds_phys
->ds_compressed_bytes
;
2152 ddpa
->origin_origin
->ds_phys
->ds_uncompressed_bytes
;
2155 /* Check that there is enough space and limit headroom here */
2156 err
= dsl_dir_transfer_possible(origin_ds
->ds_dir
, hds
->ds_dir
,
2157 0, ss_mv_cnt
, ddpa
->used
, ddpa
->cr
);
2162 * Compute the amounts of space that will be used by snapshots
2163 * after the promotion (for both origin and clone). For each,
2164 * it is the amount of space that will be on all of their
2165 * deadlists (that was not born before their new origin).
2167 if (hds
->ds_dir
->dd_phys
->dd_flags
& DD_FLAG_USED_BREAKDOWN
) {
2171 * Note, typically this will not be a clone of a clone,
2172 * so dd_origin_txg will be < TXG_INITIAL, so
2173 * these snaplist_space() -> dsl_deadlist_space_range()
2174 * calls will be fast because they do not have to
2175 * iterate over all bps.
2177 snap
= list_head(&ddpa
->origin_snaps
);
2178 err
= snaplist_space(&ddpa
->shared_snaps
,
2179 snap
->ds
->ds_dir
->dd_origin_txg
, &ddpa
->cloneusedsnap
);
2183 err
= snaplist_space(&ddpa
->clone_snaps
,
2184 snap
->ds
->ds_dir
->dd_origin_txg
, &space
);
2187 ddpa
->cloneusedsnap
+= space
;
2189 if (origin_ds
->ds_dir
->dd_phys
->dd_flags
& DD_FLAG_USED_BREAKDOWN
) {
2190 err
= snaplist_space(&ddpa
->origin_snaps
,
2191 origin_ds
->ds_phys
->ds_creation_txg
, &ddpa
->originusedsnap
);
2197 promote_rele(ddpa
, FTAG
);
2202 dsl_dataset_promote_sync(void *arg
, dmu_tx_t
*tx
)
2204 dsl_dataset_promote_arg_t
*ddpa
= arg
;
2205 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2207 struct promotenode
*snap
;
2208 dsl_dataset_t
*origin_ds
;
2209 dsl_dataset_t
*origin_head
;
2211 dsl_dir_t
*odd
= NULL
;
2212 uint64_t oldnext_obj
;
2215 VERIFY0(promote_hold(ddpa
, dp
, FTAG
));
2216 hds
= ddpa
->ddpa_clone
;
2218 ASSERT0(hds
->ds_phys
->ds_flags
& DS_FLAG_NOPROMOTE
);
2220 snap
= list_head(&ddpa
->shared_snaps
);
2221 origin_ds
= snap
->ds
;
2224 snap
= list_head(&ddpa
->origin_snaps
);
2225 origin_head
= snap
->ds
;
2228 * We need to explicitly open odd, since origin_ds's dd will be
2231 VERIFY0(dsl_dir_hold_obj(dp
, origin_ds
->ds_dir
->dd_object
,
2234 /* change origin's next snap */
2235 dmu_buf_will_dirty(origin_ds
->ds_dbuf
, tx
);
2236 oldnext_obj
= origin_ds
->ds_phys
->ds_next_snap_obj
;
2237 snap
= list_tail(&ddpa
->clone_snaps
);
2238 ASSERT3U(snap
->ds
->ds_phys
->ds_prev_snap_obj
, ==, origin_ds
->ds_object
);
2239 origin_ds
->ds_phys
->ds_next_snap_obj
= snap
->ds
->ds_object
;
2241 /* change the origin's next clone */
2242 if (origin_ds
->ds_phys
->ds_next_clones_obj
) {
2243 dsl_dataset_remove_from_next_clones(origin_ds
,
2244 snap
->ds
->ds_object
, tx
);
2245 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2246 origin_ds
->ds_phys
->ds_next_clones_obj
,
2251 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
2252 ASSERT3U(dd
->dd_phys
->dd_origin_obj
, ==, origin_ds
->ds_object
);
2253 dd
->dd_phys
->dd_origin_obj
= odd
->dd_phys
->dd_origin_obj
;
2254 dd
->dd_origin_txg
= origin_head
->ds_dir
->dd_origin_txg
;
2255 dmu_buf_will_dirty(odd
->dd_dbuf
, tx
);
2256 odd
->dd_phys
->dd_origin_obj
= origin_ds
->ds_object
;
2257 origin_head
->ds_dir
->dd_origin_txg
=
2258 origin_ds
->ds_phys
->ds_creation_txg
;
2260 /* change dd_clone entries */
2261 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
2262 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
2263 odd
->dd_phys
->dd_clones
, hds
->ds_object
, tx
));
2264 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2265 ddpa
->origin_origin
->ds_dir
->dd_phys
->dd_clones
,
2266 hds
->ds_object
, tx
));
2268 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
2269 ddpa
->origin_origin
->ds_dir
->dd_phys
->dd_clones
,
2270 origin_head
->ds_object
, tx
));
2271 if (dd
->dd_phys
->dd_clones
== 0) {
2272 dd
->dd_phys
->dd_clones
= zap_create(dp
->dp_meta_objset
,
2273 DMU_OT_DSL_CLONES
, DMU_OT_NONE
, 0, tx
);
2275 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2276 dd
->dd_phys
->dd_clones
, origin_head
->ds_object
, tx
));
2279 /* move snapshots to this dir */
2280 for (snap
= list_head(&ddpa
->shared_snaps
); snap
;
2281 snap
= list_next(&ddpa
->shared_snaps
, snap
)) {
2282 dsl_dataset_t
*ds
= snap
->ds
;
2285 * Property callbacks are registered to a particular
2286 * dsl_dir. Since ours is changing, evict the objset
2287 * so that they will be unregistered from the old dsl_dir.
2289 if (ds
->ds_objset
) {
2290 dmu_objset_evict(ds
->ds_objset
);
2291 ds
->ds_objset
= NULL
;
2294 /* move snap name entry */
2295 VERIFY0(dsl_dataset_get_snapname(ds
));
2296 VERIFY0(dsl_dataset_snap_remove(origin_head
,
2297 ds
->ds_snapname
, tx
, B_TRUE
));
2298 VERIFY0(zap_add(dp
->dp_meta_objset
,
2299 hds
->ds_phys
->ds_snapnames_zapobj
, ds
->ds_snapname
,
2300 8, 1, &ds
->ds_object
, tx
));
2301 dsl_fs_ss_count_adjust(hds
->ds_dir
, 1,
2302 DD_FIELD_SNAPSHOT_COUNT
, tx
);
2304 /* change containing dsl_dir */
2305 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2306 ASSERT3U(ds
->ds_phys
->ds_dir_obj
, ==, odd
->dd_object
);
2307 ds
->ds_phys
->ds_dir_obj
= dd
->dd_object
;
2308 ASSERT3P(ds
->ds_dir
, ==, odd
);
2309 dsl_dir_rele(ds
->ds_dir
, ds
);
2310 VERIFY0(dsl_dir_hold_obj(dp
, dd
->dd_object
,
2311 NULL
, ds
, &ds
->ds_dir
));
2313 /* move any clone references */
2314 if (ds
->ds_phys
->ds_next_clones_obj
&&
2315 spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
2319 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2320 ds
->ds_phys
->ds_next_clones_obj
);
2321 zap_cursor_retrieve(&zc
, &za
) == 0;
2322 zap_cursor_advance(&zc
)) {
2323 dsl_dataset_t
*cnds
;
2326 if (za
.za_first_integer
== oldnext_obj
) {
2328 * We've already moved the
2329 * origin's reference.
2334 VERIFY0(dsl_dataset_hold_obj(dp
,
2335 za
.za_first_integer
, FTAG
, &cnds
));
2336 o
= cnds
->ds_dir
->dd_phys
->dd_head_dataset_obj
;
2338 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
2339 odd
->dd_phys
->dd_clones
, o
, tx
));
2340 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2341 dd
->dd_phys
->dd_clones
, o
, tx
));
2342 dsl_dataset_rele(cnds
, FTAG
);
2344 zap_cursor_fini(&zc
);
2347 ASSERT(!dsl_prop_hascb(ds
));
2351 * Change space accounting.
2352 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2353 * both be valid, or both be 0 (resulting in delta == 0). This
2354 * is true for each of {clone,origin} independently.
2357 delta
= ddpa
->cloneusedsnap
-
2358 dd
->dd_phys
->dd_used_breakdown
[DD_USED_SNAP
];
2359 ASSERT3S(delta
, >=, 0);
2360 ASSERT3U(ddpa
->used
, >=, delta
);
2361 dsl_dir_diduse_space(dd
, DD_USED_SNAP
, delta
, 0, 0, tx
);
2362 dsl_dir_diduse_space(dd
, DD_USED_HEAD
,
2363 ddpa
->used
- delta
, ddpa
->comp
, ddpa
->uncomp
, tx
);
2365 delta
= ddpa
->originusedsnap
-
2366 odd
->dd_phys
->dd_used_breakdown
[DD_USED_SNAP
];
2367 ASSERT3S(delta
, <=, 0);
2368 ASSERT3U(ddpa
->used
, >=, -delta
);
2369 dsl_dir_diduse_space(odd
, DD_USED_SNAP
, delta
, 0, 0, tx
);
2370 dsl_dir_diduse_space(odd
, DD_USED_HEAD
,
2371 -ddpa
->used
- delta
, -ddpa
->comp
, -ddpa
->uncomp
, tx
);
2373 origin_ds
->ds_phys
->ds_unique_bytes
= ddpa
->unique
;
2375 /* log history record */
2376 spa_history_log_internal_ds(hds
, "promote", tx
, "");
2378 dsl_dir_rele(odd
, FTAG
);
2379 promote_rele(ddpa
, FTAG
);
2383 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2384 * (exclusive) and last_obj (inclusive). The list will be in reverse
2385 * order (last_obj will be the list_head()). If first_obj == 0, do all
2386 * snapshots back to this dataset's origin.
2389 snaplist_make(dsl_pool_t
*dp
,
2390 uint64_t first_obj
, uint64_t last_obj
, list_t
*l
, void *tag
)
2392 uint64_t obj
= last_obj
;
2394 list_create(l
, sizeof (struct promotenode
),
2395 offsetof(struct promotenode
, link
));
2397 while (obj
!= first_obj
) {
2399 struct promotenode
*snap
;
2402 err
= dsl_dataset_hold_obj(dp
, obj
, tag
, &ds
);
2403 ASSERT(err
!= ENOENT
);
2408 first_obj
= ds
->ds_dir
->dd_phys
->dd_origin_obj
;
2410 snap
= kmem_alloc(sizeof (*snap
), KM_SLEEP
);
2412 list_insert_tail(l
, snap
);
2413 obj
= ds
->ds_phys
->ds_prev_snap_obj
;
2420 snaplist_space(list_t
*l
, uint64_t mintxg
, uint64_t *spacep
)
2422 struct promotenode
*snap
;
2425 for (snap
= list_head(l
); snap
; snap
= list_next(l
, snap
)) {
2426 uint64_t used
, comp
, uncomp
;
2427 dsl_deadlist_space_range(&snap
->ds
->ds_deadlist
,
2428 mintxg
, UINT64_MAX
, &used
, &comp
, &uncomp
);
2435 snaplist_destroy(list_t
*l
, void *tag
)
2437 struct promotenode
*snap
;
2439 if (l
== NULL
|| !list_link_active(&l
->list_head
))
2442 while ((snap
= list_tail(l
)) != NULL
) {
2443 list_remove(l
, snap
);
2444 dsl_dataset_rele(snap
->ds
, tag
);
2445 kmem_free(snap
, sizeof (*snap
));
2451 promote_hold(dsl_dataset_promote_arg_t
*ddpa
, dsl_pool_t
*dp
, void *tag
)
2455 struct promotenode
*snap
;
2457 error
= dsl_dataset_hold(dp
, ddpa
->ddpa_clonename
, tag
,
2461 dd
= ddpa
->ddpa_clone
->ds_dir
;
2463 if (dsl_dataset_is_snapshot(ddpa
->ddpa_clone
) ||
2464 !dsl_dir_is_clone(dd
)) {
2465 dsl_dataset_rele(ddpa
->ddpa_clone
, tag
);
2466 return (SET_ERROR(EINVAL
));
2469 error
= snaplist_make(dp
, 0, dd
->dd_phys
->dd_origin_obj
,
2470 &ddpa
->shared_snaps
, tag
);
2474 error
= snaplist_make(dp
, 0, ddpa
->ddpa_clone
->ds_object
,
2475 &ddpa
->clone_snaps
, tag
);
2479 snap
= list_head(&ddpa
->shared_snaps
);
2480 ASSERT3U(snap
->ds
->ds_object
, ==, dd
->dd_phys
->dd_origin_obj
);
2481 error
= snaplist_make(dp
, dd
->dd_phys
->dd_origin_obj
,
2482 snap
->ds
->ds_dir
->dd_phys
->dd_head_dataset_obj
,
2483 &ddpa
->origin_snaps
, tag
);
2487 if (snap
->ds
->ds_dir
->dd_phys
->dd_origin_obj
!= 0) {
2488 error
= dsl_dataset_hold_obj(dp
,
2489 snap
->ds
->ds_dir
->dd_phys
->dd_origin_obj
,
2490 tag
, &ddpa
->origin_origin
);
2496 promote_rele(ddpa
, tag
);
2501 promote_rele(dsl_dataset_promote_arg_t
*ddpa
, void *tag
)
2503 snaplist_destroy(&ddpa
->shared_snaps
, tag
);
2504 snaplist_destroy(&ddpa
->clone_snaps
, tag
);
2505 snaplist_destroy(&ddpa
->origin_snaps
, tag
);
2506 if (ddpa
->origin_origin
!= NULL
)
2507 dsl_dataset_rele(ddpa
->origin_origin
, tag
);
2508 dsl_dataset_rele(ddpa
->ddpa_clone
, tag
);
2514 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2515 * in with the name. (It must be at least MAXNAMELEN bytes long.)
2518 dsl_dataset_promote(const char *name
, char *conflsnap
)
2520 dsl_dataset_promote_arg_t ddpa
= { 0 };
2526 * We will modify space proportional to the number of
2527 * snapshots. Compute numsnaps.
2529 error
= dmu_objset_hold(name
, FTAG
, &os
);
2532 error
= zap_count(dmu_objset_pool(os
)->dp_meta_objset
,
2533 dmu_objset_ds(os
)->ds_phys
->ds_snapnames_zapobj
, &numsnaps
);
2534 dmu_objset_rele(os
, FTAG
);
2538 ddpa
.ddpa_clonename
= name
;
2539 ddpa
.err_ds
= conflsnap
;
2542 return (dsl_sync_task(name
, dsl_dataset_promote_check
,
2543 dsl_dataset_promote_sync
, &ddpa
, 2 + numsnaps
));
2547 dsl_dataset_clone_swap_check_impl(dsl_dataset_t
*clone
,
2548 dsl_dataset_t
*origin_head
, boolean_t force
, void *owner
, dmu_tx_t
*tx
)
2550 int64_t unused_refres_delta
;
2552 /* they should both be heads */
2553 if (dsl_dataset_is_snapshot(clone
) ||
2554 dsl_dataset_is_snapshot(origin_head
))
2555 return (SET_ERROR(EINVAL
));
2557 /* if we are not forcing, the branch point should be just before them */
2558 if (!force
&& clone
->ds_prev
!= origin_head
->ds_prev
)
2559 return (SET_ERROR(EINVAL
));
2561 /* clone should be the clone (unless they are unrelated) */
2562 if (clone
->ds_prev
!= NULL
&&
2563 clone
->ds_prev
!= clone
->ds_dir
->dd_pool
->dp_origin_snap
&&
2564 origin_head
->ds_dir
!= clone
->ds_prev
->ds_dir
)
2565 return (SET_ERROR(EINVAL
));
2567 /* the clone should be a child of the origin */
2568 if (clone
->ds_dir
->dd_parent
!= origin_head
->ds_dir
)
2569 return (SET_ERROR(EINVAL
));
2571 /* origin_head shouldn't be modified unless 'force' */
2573 dsl_dataset_modified_since_snap(origin_head
, origin_head
->ds_prev
))
2574 return (SET_ERROR(ETXTBSY
));
2576 /* origin_head should have no long holds (e.g. is not mounted) */
2577 if (dsl_dataset_handoff_check(origin_head
, owner
, tx
))
2578 return (SET_ERROR(EBUSY
));
2580 /* check amount of any unconsumed refreservation */
2581 unused_refres_delta
=
2582 (int64_t)MIN(origin_head
->ds_reserved
,
2583 origin_head
->ds_phys
->ds_unique_bytes
) -
2584 (int64_t)MIN(origin_head
->ds_reserved
,
2585 clone
->ds_phys
->ds_unique_bytes
);
2587 if (unused_refres_delta
> 0 &&
2588 unused_refres_delta
>
2589 dsl_dir_space_available(origin_head
->ds_dir
, NULL
, 0, TRUE
))
2590 return (SET_ERROR(ENOSPC
));
2592 /* clone can't be over the head's refquota */
2593 if (origin_head
->ds_quota
!= 0 &&
2594 clone
->ds_phys
->ds_referenced_bytes
> origin_head
->ds_quota
)
2595 return (SET_ERROR(EDQUOT
));
2601 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t
*clone
,
2602 dsl_dataset_t
*origin_head
, dmu_tx_t
*tx
)
2604 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2605 int64_t unused_refres_delta
;
2607 ASSERT(clone
->ds_reserved
== 0);
2608 ASSERT(origin_head
->ds_quota
== 0 ||
2609 clone
->ds_phys
->ds_unique_bytes
<= origin_head
->ds_quota
);
2610 ASSERT3P(clone
->ds_prev
, ==, origin_head
->ds_prev
);
2612 dmu_buf_will_dirty(clone
->ds_dbuf
, tx
);
2613 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
2615 if (clone
->ds_objset
!= NULL
) {
2616 dmu_objset_evict(clone
->ds_objset
);
2617 clone
->ds_objset
= NULL
;
2620 if (origin_head
->ds_objset
!= NULL
) {
2621 dmu_objset_evict(origin_head
->ds_objset
);
2622 origin_head
->ds_objset
= NULL
;
2625 unused_refres_delta
=
2626 (int64_t)MIN(origin_head
->ds_reserved
,
2627 origin_head
->ds_phys
->ds_unique_bytes
) -
2628 (int64_t)MIN(origin_head
->ds_reserved
,
2629 clone
->ds_phys
->ds_unique_bytes
);
2632 * Reset origin's unique bytes, if it exists.
2634 if (clone
->ds_prev
) {
2635 dsl_dataset_t
*origin
= clone
->ds_prev
;
2636 uint64_t comp
, uncomp
;
2638 dmu_buf_will_dirty(origin
->ds_dbuf
, tx
);
2639 dsl_deadlist_space_range(&clone
->ds_deadlist
,
2640 origin
->ds_phys
->ds_prev_snap_txg
, UINT64_MAX
,
2641 &origin
->ds_phys
->ds_unique_bytes
, &comp
, &uncomp
);
2647 tmp
= origin_head
->ds_phys
->ds_bp
;
2648 origin_head
->ds_phys
->ds_bp
= clone
->ds_phys
->ds_bp
;
2649 clone
->ds_phys
->ds_bp
= tmp
;
2652 /* set dd_*_bytes */
2654 int64_t dused
, dcomp
, duncomp
;
2655 uint64_t cdl_used
, cdl_comp
, cdl_uncomp
;
2656 uint64_t odl_used
, odl_comp
, odl_uncomp
;
2658 ASSERT3U(clone
->ds_dir
->dd_phys
->
2659 dd_used_breakdown
[DD_USED_SNAP
], ==, 0);
2661 dsl_deadlist_space(&clone
->ds_deadlist
,
2662 &cdl_used
, &cdl_comp
, &cdl_uncomp
);
2663 dsl_deadlist_space(&origin_head
->ds_deadlist
,
2664 &odl_used
, &odl_comp
, &odl_uncomp
);
2666 dused
= clone
->ds_phys
->ds_referenced_bytes
+ cdl_used
-
2667 (origin_head
->ds_phys
->ds_referenced_bytes
+ odl_used
);
2668 dcomp
= clone
->ds_phys
->ds_compressed_bytes
+ cdl_comp
-
2669 (origin_head
->ds_phys
->ds_compressed_bytes
+ odl_comp
);
2670 duncomp
= clone
->ds_phys
->ds_uncompressed_bytes
+
2672 (origin_head
->ds_phys
->ds_uncompressed_bytes
+ odl_uncomp
);
2674 dsl_dir_diduse_space(origin_head
->ds_dir
, DD_USED_HEAD
,
2675 dused
, dcomp
, duncomp
, tx
);
2676 dsl_dir_diduse_space(clone
->ds_dir
, DD_USED_HEAD
,
2677 -dused
, -dcomp
, -duncomp
, tx
);
2680 * The difference in the space used by snapshots is the
2681 * difference in snapshot space due to the head's
2682 * deadlist (since that's the only thing that's
2683 * changing that affects the snapused).
2685 dsl_deadlist_space_range(&clone
->ds_deadlist
,
2686 origin_head
->ds_dir
->dd_origin_txg
, UINT64_MAX
,
2687 &cdl_used
, &cdl_comp
, &cdl_uncomp
);
2688 dsl_deadlist_space_range(&origin_head
->ds_deadlist
,
2689 origin_head
->ds_dir
->dd_origin_txg
, UINT64_MAX
,
2690 &odl_used
, &odl_comp
, &odl_uncomp
);
2691 dsl_dir_transfer_space(origin_head
->ds_dir
, cdl_used
- odl_used
,
2692 DD_USED_HEAD
, DD_USED_SNAP
, tx
);
2695 /* swap ds_*_bytes */
2696 SWITCH64(origin_head
->ds_phys
->ds_referenced_bytes
,
2697 clone
->ds_phys
->ds_referenced_bytes
);
2698 SWITCH64(origin_head
->ds_phys
->ds_compressed_bytes
,
2699 clone
->ds_phys
->ds_compressed_bytes
);
2700 SWITCH64(origin_head
->ds_phys
->ds_uncompressed_bytes
,
2701 clone
->ds_phys
->ds_uncompressed_bytes
);
2702 SWITCH64(origin_head
->ds_phys
->ds_unique_bytes
,
2703 clone
->ds_phys
->ds_unique_bytes
);
2705 /* apply any parent delta for change in unconsumed refreservation */
2706 dsl_dir_diduse_space(origin_head
->ds_dir
, DD_USED_REFRSRV
,
2707 unused_refres_delta
, 0, 0, tx
);
2712 dsl_deadlist_close(&clone
->ds_deadlist
);
2713 dsl_deadlist_close(&origin_head
->ds_deadlist
);
2714 SWITCH64(origin_head
->ds_phys
->ds_deadlist_obj
,
2715 clone
->ds_phys
->ds_deadlist_obj
);
2716 dsl_deadlist_open(&clone
->ds_deadlist
, dp
->dp_meta_objset
,
2717 clone
->ds_phys
->ds_deadlist_obj
);
2718 dsl_deadlist_open(&origin_head
->ds_deadlist
, dp
->dp_meta_objset
,
2719 origin_head
->ds_phys
->ds_deadlist_obj
);
2721 dsl_scan_ds_clone_swapped(origin_head
, clone
, tx
);
2723 spa_history_log_internal_ds(clone
, "clone swap", tx
,
2724 "parent=%s", origin_head
->ds_dir
->dd_myname
);
2728 * Given a pool name and a dataset object number in that pool,
2729 * return the name of that dataset.
2732 dsl_dsobj_to_dsname(char *pname
, uint64_t obj
, char *buf
)
2738 error
= dsl_pool_hold(pname
, FTAG
, &dp
);
2742 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
, &ds
);
2744 dsl_dataset_name(ds
, buf
);
2745 dsl_dataset_rele(ds
, FTAG
);
2747 dsl_pool_rele(dp
, FTAG
);
2753 dsl_dataset_check_quota(dsl_dataset_t
*ds
, boolean_t check_quota
,
2754 uint64_t asize
, uint64_t inflight
, uint64_t *used
, uint64_t *ref_rsrv
)
2758 ASSERT3S(asize
, >, 0);
2761 * *ref_rsrv is the portion of asize that will come from any
2762 * unconsumed refreservation space.
2766 mutex_enter(&ds
->ds_lock
);
2768 * Make a space adjustment for reserved bytes.
2770 if (ds
->ds_reserved
> ds
->ds_phys
->ds_unique_bytes
) {
2772 ds
->ds_reserved
- ds
->ds_phys
->ds_unique_bytes
);
2773 *used
-= (ds
->ds_reserved
- ds
->ds_phys
->ds_unique_bytes
);
2775 asize
- MIN(asize
, parent_delta(ds
, asize
+ inflight
));
2778 if (!check_quota
|| ds
->ds_quota
== 0) {
2779 mutex_exit(&ds
->ds_lock
);
2783 * If they are requesting more space, and our current estimate
2784 * is over quota, they get to try again unless the actual
2785 * on-disk is over quota and there are no pending changes (which
2786 * may free up space for us).
2788 if (ds
->ds_phys
->ds_referenced_bytes
+ inflight
>= ds
->ds_quota
) {
2790 ds
->ds_phys
->ds_referenced_bytes
< ds
->ds_quota
)
2791 error
= SET_ERROR(ERESTART
);
2793 error
= SET_ERROR(EDQUOT
);
2795 mutex_exit(&ds
->ds_lock
);
2800 typedef struct dsl_dataset_set_qr_arg
{
2801 const char *ddsqra_name
;
2802 zprop_source_t ddsqra_source
;
2803 uint64_t ddsqra_value
;
2804 } dsl_dataset_set_qr_arg_t
;
2809 dsl_dataset_set_refquota_check(void *arg
, dmu_tx_t
*tx
)
2811 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2812 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2817 if (spa_version(dp
->dp_spa
) < SPA_VERSION_REFQUOTA
)
2818 return (SET_ERROR(ENOTSUP
));
2820 error
= dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
);
2824 if (dsl_dataset_is_snapshot(ds
)) {
2825 dsl_dataset_rele(ds
, FTAG
);
2826 return (SET_ERROR(EINVAL
));
2829 error
= dsl_prop_predict(ds
->ds_dir
,
2830 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
2831 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, &newval
);
2833 dsl_dataset_rele(ds
, FTAG
);
2838 dsl_dataset_rele(ds
, FTAG
);
2842 if (newval
< ds
->ds_phys
->ds_referenced_bytes
||
2843 newval
< ds
->ds_reserved
) {
2844 dsl_dataset_rele(ds
, FTAG
);
2845 return (SET_ERROR(ENOSPC
));
2848 dsl_dataset_rele(ds
, FTAG
);
2853 dsl_dataset_set_refquota_sync(void *arg
, dmu_tx_t
*tx
)
2855 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2856 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2860 VERIFY0(dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
));
2862 dsl_prop_set_sync_impl(ds
,
2863 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
2864 ddsqra
->ddsqra_source
, sizeof (ddsqra
->ddsqra_value
), 1,
2865 &ddsqra
->ddsqra_value
, tx
);
2867 VERIFY0(dsl_prop_get_int_ds(ds
,
2868 zfs_prop_to_name(ZFS_PROP_REFQUOTA
), &newval
));
2870 if (ds
->ds_quota
!= newval
) {
2871 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2872 ds
->ds_quota
= newval
;
2874 dsl_dataset_rele(ds
, FTAG
);
2878 dsl_dataset_set_refquota(const char *dsname
, zprop_source_t source
,
2881 dsl_dataset_set_qr_arg_t ddsqra
;
2883 ddsqra
.ddsqra_name
= dsname
;
2884 ddsqra
.ddsqra_source
= source
;
2885 ddsqra
.ddsqra_value
= refquota
;
2887 return (dsl_sync_task(dsname
, dsl_dataset_set_refquota_check
,
2888 dsl_dataset_set_refquota_sync
, &ddsqra
, 0));
2892 dsl_dataset_set_refreservation_check(void *arg
, dmu_tx_t
*tx
)
2894 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2895 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2898 uint64_t newval
, unique
;
2900 if (spa_version(dp
->dp_spa
) < SPA_VERSION_REFRESERVATION
)
2901 return (SET_ERROR(ENOTSUP
));
2903 error
= dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
);
2907 if (dsl_dataset_is_snapshot(ds
)) {
2908 dsl_dataset_rele(ds
, FTAG
);
2909 return (SET_ERROR(EINVAL
));
2912 error
= dsl_prop_predict(ds
->ds_dir
,
2913 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
2914 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, &newval
);
2916 dsl_dataset_rele(ds
, FTAG
);
2921 * If we are doing the preliminary check in open context, the
2922 * space estimates may be inaccurate.
2924 if (!dmu_tx_is_syncing(tx
)) {
2925 dsl_dataset_rele(ds
, FTAG
);
2929 mutex_enter(&ds
->ds_lock
);
2930 if (!DS_UNIQUE_IS_ACCURATE(ds
))
2931 dsl_dataset_recalc_head_uniq(ds
);
2932 unique
= ds
->ds_phys
->ds_unique_bytes
;
2933 mutex_exit(&ds
->ds_lock
);
2935 if (MAX(unique
, newval
) > MAX(unique
, ds
->ds_reserved
)) {
2936 uint64_t delta
= MAX(unique
, newval
) -
2937 MAX(unique
, ds
->ds_reserved
);
2940 dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, B_TRUE
) ||
2941 (ds
->ds_quota
> 0 && newval
> ds
->ds_quota
)) {
2942 dsl_dataset_rele(ds
, FTAG
);
2943 return (SET_ERROR(ENOSPC
));
2947 dsl_dataset_rele(ds
, FTAG
);
2952 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t
*ds
,
2953 zprop_source_t source
, uint64_t value
, dmu_tx_t
*tx
)
2959 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
2960 source
, sizeof (value
), 1, &value
, tx
);
2962 VERIFY0(dsl_prop_get_int_ds(ds
,
2963 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), &newval
));
2965 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2966 mutex_enter(&ds
->ds_dir
->dd_lock
);
2967 mutex_enter(&ds
->ds_lock
);
2968 ASSERT(DS_UNIQUE_IS_ACCURATE(ds
));
2969 unique
= ds
->ds_phys
->ds_unique_bytes
;
2970 delta
= MAX(0, (int64_t)(newval
- unique
)) -
2971 MAX(0, (int64_t)(ds
->ds_reserved
- unique
));
2972 ds
->ds_reserved
= newval
;
2973 mutex_exit(&ds
->ds_lock
);
2975 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_REFRSRV
, delta
, 0, 0, tx
);
2976 mutex_exit(&ds
->ds_dir
->dd_lock
);
2980 dsl_dataset_set_refreservation_sync(void *arg
, dmu_tx_t
*tx
)
2982 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2983 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2986 VERIFY0(dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
));
2987 dsl_dataset_set_refreservation_sync_impl(ds
,
2988 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, tx
);
2989 dsl_dataset_rele(ds
, FTAG
);
2993 dsl_dataset_set_refreservation(const char *dsname
, zprop_source_t source
,
2994 uint64_t refreservation
)
2996 dsl_dataset_set_qr_arg_t ddsqra
;
2998 ddsqra
.ddsqra_name
= dsname
;
2999 ddsqra
.ddsqra_source
= source
;
3000 ddsqra
.ddsqra_value
= refreservation
;
3002 return (dsl_sync_task(dsname
, dsl_dataset_set_refreservation_check
,
3003 dsl_dataset_set_refreservation_sync
, &ddsqra
, 0));
3007 * Return (in *usedp) the amount of space written in new that is not
3008 * present in oldsnap. New may be a snapshot or the head. Old must be
3009 * a snapshot before new, in new's filesystem (or its origin). If not then
3010 * fail and return EINVAL.
3012 * The written space is calculated by considering two components: First, we
3013 * ignore any freed space, and calculate the written as new's used space
3014 * minus old's used space. Next, we add in the amount of space that was freed
3015 * between the two snapshots, thus reducing new's used space relative to old's.
3016 * Specifically, this is the space that was born before old->ds_creation_txg,
3017 * and freed before new (ie. on new's deadlist or a previous deadlist).
3019 * space freed [---------------------]
3020 * snapshots ---O-------O--------O-------O------
3024 dsl_dataset_space_written(dsl_dataset_t
*oldsnap
, dsl_dataset_t
*new,
3025 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
3029 dsl_pool_t
*dp
= new->ds_dir
->dd_pool
;
3031 ASSERT(dsl_pool_config_held(dp
));
3034 *usedp
+= new->ds_phys
->ds_referenced_bytes
;
3035 *usedp
-= oldsnap
->ds_phys
->ds_referenced_bytes
;
3038 *compp
+= new->ds_phys
->ds_compressed_bytes
;
3039 *compp
-= oldsnap
->ds_phys
->ds_compressed_bytes
;
3042 *uncompp
+= new->ds_phys
->ds_uncompressed_bytes
;
3043 *uncompp
-= oldsnap
->ds_phys
->ds_uncompressed_bytes
;
3045 snapobj
= new->ds_object
;
3046 while (snapobj
!= oldsnap
->ds_object
) {
3047 dsl_dataset_t
*snap
;
3048 uint64_t used
, comp
, uncomp
;
3050 if (snapobj
== new->ds_object
) {
3053 err
= dsl_dataset_hold_obj(dp
, snapobj
, FTAG
, &snap
);
3058 if (snap
->ds_phys
->ds_prev_snap_txg
==
3059 oldsnap
->ds_phys
->ds_creation_txg
) {
3061 * The blocks in the deadlist can not be born after
3062 * ds_prev_snap_txg, so get the whole deadlist space,
3063 * which is more efficient (especially for old-format
3064 * deadlists). Unfortunately the deadlist code
3065 * doesn't have enough information to make this
3066 * optimization itself.
3068 dsl_deadlist_space(&snap
->ds_deadlist
,
3069 &used
, &comp
, &uncomp
);
3071 dsl_deadlist_space_range(&snap
->ds_deadlist
,
3072 0, oldsnap
->ds_phys
->ds_creation_txg
,
3073 &used
, &comp
, &uncomp
);
3080 * If we get to the beginning of the chain of snapshots
3081 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
3082 * was not a snapshot of/before new.
3084 snapobj
= snap
->ds_phys
->ds_prev_snap_obj
;
3086 dsl_dataset_rele(snap
, FTAG
);
3088 err
= SET_ERROR(EINVAL
);
3097 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
3098 * lastsnap, and all snapshots in between are deleted.
3100 * blocks that would be freed [---------------------------]
3101 * snapshots ---O-------O--------O-------O--------O
3102 * firstsnap lastsnap
3104 * This is the set of blocks that were born after the snap before firstsnap,
3105 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
3106 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
3107 * We calculate this by iterating over the relevant deadlists (from the snap
3108 * after lastsnap, backward to the snap after firstsnap), summing up the
3109 * space on the deadlist that was born after the snap before firstsnap.
3112 dsl_dataset_space_wouldfree(dsl_dataset_t
*firstsnap
,
3113 dsl_dataset_t
*lastsnap
,
3114 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
3118 dsl_pool_t
*dp
= firstsnap
->ds_dir
->dd_pool
;
3120 ASSERT(dsl_dataset_is_snapshot(firstsnap
));
3121 ASSERT(dsl_dataset_is_snapshot(lastsnap
));
3124 * Check that the snapshots are in the same dsl_dir, and firstsnap
3125 * is before lastsnap.
3127 if (firstsnap
->ds_dir
!= lastsnap
->ds_dir
||
3128 firstsnap
->ds_phys
->ds_creation_txg
>
3129 lastsnap
->ds_phys
->ds_creation_txg
)
3130 return (SET_ERROR(EINVAL
));
3132 *usedp
= *compp
= *uncompp
= 0;
3134 snapobj
= lastsnap
->ds_phys
->ds_next_snap_obj
;
3135 while (snapobj
!= firstsnap
->ds_object
) {
3137 uint64_t used
, comp
, uncomp
;
3139 err
= dsl_dataset_hold_obj(dp
, snapobj
, FTAG
, &ds
);
3143 dsl_deadlist_space_range(&ds
->ds_deadlist
,
3144 firstsnap
->ds_phys
->ds_prev_snap_txg
, UINT64_MAX
,
3145 &used
, &comp
, &uncomp
);
3150 snapobj
= ds
->ds_phys
->ds_prev_snap_obj
;
3151 ASSERT3U(snapobj
, !=, 0);
3152 dsl_dataset_rele(ds
, FTAG
);
3158 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
3159 * For example, they could both be snapshots of the same filesystem, and
3160 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
3161 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
3162 * filesystem. Or 'earlier' could be the origin's origin.
3164 * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
3167 dsl_dataset_is_before(dsl_dataset_t
*later
, dsl_dataset_t
*earlier
,
3168 uint64_t earlier_txg
)
3170 dsl_pool_t
*dp
= later
->ds_dir
->dd_pool
;
3173 dsl_dataset_t
*origin
;
3175 ASSERT(dsl_pool_config_held(dp
));
3176 ASSERT(dsl_dataset_is_snapshot(earlier
) || earlier_txg
!= 0);
3178 if (earlier_txg
== 0)
3179 earlier_txg
= earlier
->ds_phys
->ds_creation_txg
;
3181 if (dsl_dataset_is_snapshot(later
) &&
3182 earlier_txg
>= later
->ds_phys
->ds_creation_txg
)
3185 if (later
->ds_dir
== earlier
->ds_dir
)
3187 if (!dsl_dir_is_clone(later
->ds_dir
))
3190 if (later
->ds_dir
->dd_phys
->dd_origin_obj
== earlier
->ds_object
)
3192 error
= dsl_dataset_hold_obj(dp
,
3193 later
->ds_dir
->dd_phys
->dd_origin_obj
, FTAG
, &origin
);
3196 ret
= dsl_dataset_is_before(origin
, earlier
, earlier_txg
);
3197 dsl_dataset_rele(origin
, FTAG
);
3203 dsl_dataset_zapify(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
3205 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
3206 dmu_object_zapify(mos
, ds
->ds_object
, DMU_OT_DSL_DATASET
, tx
);
3209 #if defined(_KERNEL) && defined(HAVE_SPL)
3210 EXPORT_SYMBOL(dsl_dataset_hold
);
3211 EXPORT_SYMBOL(dsl_dataset_hold_obj
);
3212 EXPORT_SYMBOL(dsl_dataset_own
);
3213 EXPORT_SYMBOL(dsl_dataset_own_obj
);
3214 EXPORT_SYMBOL(dsl_dataset_name
);
3215 EXPORT_SYMBOL(dsl_dataset_rele
);
3216 EXPORT_SYMBOL(dsl_dataset_disown
);
3217 EXPORT_SYMBOL(dsl_dataset_tryown
);
3218 EXPORT_SYMBOL(dsl_dataset_create_sync
);
3219 EXPORT_SYMBOL(dsl_dataset_create_sync_dd
);
3220 EXPORT_SYMBOL(dsl_dataset_snapshot_check
);
3221 EXPORT_SYMBOL(dsl_dataset_snapshot_sync
);
3222 EXPORT_SYMBOL(dsl_dataset_promote
);
3223 EXPORT_SYMBOL(dsl_dataset_user_hold
);
3224 EXPORT_SYMBOL(dsl_dataset_user_release
);
3225 EXPORT_SYMBOL(dsl_dataset_get_holds
);
3226 EXPORT_SYMBOL(dsl_dataset_get_blkptr
);
3227 EXPORT_SYMBOL(dsl_dataset_set_blkptr
);
3228 EXPORT_SYMBOL(dsl_dataset_get_spa
);
3229 EXPORT_SYMBOL(dsl_dataset_modified_since_snap
);
3230 EXPORT_SYMBOL(dsl_dataset_space_written
);
3231 EXPORT_SYMBOL(dsl_dataset_space_wouldfree
);
3232 EXPORT_SYMBOL(dsl_dataset_sync
);
3233 EXPORT_SYMBOL(dsl_dataset_block_born
);
3234 EXPORT_SYMBOL(dsl_dataset_block_kill
);
3235 EXPORT_SYMBOL(dsl_dataset_block_freeable
);
3236 EXPORT_SYMBOL(dsl_dataset_prev_snap_txg
);
3237 EXPORT_SYMBOL(dsl_dataset_dirty
);
3238 EXPORT_SYMBOL(dsl_dataset_stats
);
3239 EXPORT_SYMBOL(dsl_dataset_fast_stat
);
3240 EXPORT_SYMBOL(dsl_dataset_space
);
3241 EXPORT_SYMBOL(dsl_dataset_fsid_guid
);
3242 EXPORT_SYMBOL(dsl_dsobj_to_dsname
);
3243 EXPORT_SYMBOL(dsl_dataset_check_quota
);
3244 EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl
);
3245 EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl
);