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) 2012, 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 refcount_destroy(&ds
->ds_longholds
);
272 kmem_free(ds
, sizeof (dsl_dataset_t
));
276 dsl_dataset_get_snapname(dsl_dataset_t
*ds
)
278 dsl_dataset_phys_t
*headphys
;
281 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
282 objset_t
*mos
= dp
->dp_meta_objset
;
284 if (ds
->ds_snapname
[0])
286 if (ds
->ds_phys
->ds_next_snap_obj
== 0)
289 err
= dmu_bonus_hold(mos
, ds
->ds_dir
->dd_phys
->dd_head_dataset_obj
,
293 headphys
= headdbuf
->db_data
;
294 err
= zap_value_search(dp
->dp_meta_objset
,
295 headphys
->ds_snapnames_zapobj
, ds
->ds_object
, 0, ds
->ds_snapname
);
296 dmu_buf_rele(headdbuf
, FTAG
);
301 dsl_dataset_snap_lookup(dsl_dataset_t
*ds
, const char *name
, uint64_t *value
)
303 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
304 uint64_t snapobj
= ds
->ds_phys
->ds_snapnames_zapobj
;
308 if (ds
->ds_phys
->ds_flags
& DS_FLAG_CI_DATASET
)
313 err
= zap_lookup_norm(mos
, snapobj
, name
, 8, 1,
314 value
, mt
, NULL
, 0, NULL
);
315 if (err
== ENOTSUP
&& mt
== MT_FIRST
)
316 err
= zap_lookup(mos
, snapobj
, name
, 8, 1, value
);
321 dsl_dataset_snap_remove(dsl_dataset_t
*ds
, const char *name
, dmu_tx_t
*tx
)
323 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
324 uint64_t snapobj
= ds
->ds_phys
->ds_snapnames_zapobj
;
328 dsl_dir_snap_cmtime_update(ds
->ds_dir
);
330 if (ds
->ds_phys
->ds_flags
& DS_FLAG_CI_DATASET
)
335 err
= zap_remove_norm(mos
, snapobj
, name
, mt
, tx
);
336 if (err
== ENOTSUP
&& mt
== MT_FIRST
)
337 err
= zap_remove(mos
, snapobj
, name
, tx
);
342 dsl_dataset_hold_obj(dsl_pool_t
*dp
, uint64_t dsobj
, void *tag
,
345 objset_t
*mos
= dp
->dp_meta_objset
;
349 dmu_object_info_t doi
;
351 ASSERT(dsl_pool_config_held(dp
));
353 err
= dmu_bonus_hold(mos
, dsobj
, tag
, &dbuf
);
357 /* Make sure dsobj has the correct object type. */
358 dmu_object_info_from_db(dbuf
, &doi
);
359 if (doi
.doi_bonus_type
!= DMU_OT_DSL_DATASET
) {
360 dmu_buf_rele(dbuf
, tag
);
361 return (SET_ERROR(EINVAL
));
364 ds
= dmu_buf_get_user(dbuf
);
366 dsl_dataset_t
*winner
= NULL
;
368 ds
= kmem_zalloc(sizeof (dsl_dataset_t
), KM_SLEEP
);
370 ds
->ds_object
= dsobj
;
371 ds
->ds_phys
= dbuf
->db_data
;
372 list_link_init(&ds
->ds_synced_link
);
374 mutex_init(&ds
->ds_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
375 mutex_init(&ds
->ds_opening_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
376 mutex_init(&ds
->ds_sendstream_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
377 refcount_create(&ds
->ds_longholds
);
379 bplist_create(&ds
->ds_pending_deadlist
);
380 dsl_deadlist_open(&ds
->ds_deadlist
,
381 mos
, ds
->ds_phys
->ds_deadlist_obj
);
383 list_create(&ds
->ds_sendstreams
, sizeof (dmu_sendarg_t
),
384 offsetof(dmu_sendarg_t
, dsa_link
));
387 err
= dsl_dir_hold_obj(dp
,
388 ds
->ds_phys
->ds_dir_obj
, NULL
, ds
, &ds
->ds_dir
);
391 mutex_destroy(&ds
->ds_lock
);
392 mutex_destroy(&ds
->ds_opening_lock
);
393 refcount_destroy(&ds
->ds_longholds
);
394 bplist_destroy(&ds
->ds_pending_deadlist
);
395 dsl_deadlist_close(&ds
->ds_deadlist
);
396 kmem_free(ds
, sizeof (dsl_dataset_t
));
397 dmu_buf_rele(dbuf
, tag
);
401 if (!dsl_dataset_is_snapshot(ds
)) {
402 ds
->ds_snapname
[0] = '\0';
403 if (ds
->ds_phys
->ds_prev_snap_obj
!= 0) {
404 err
= dsl_dataset_hold_obj(dp
,
405 ds
->ds_phys
->ds_prev_snap_obj
,
408 if (doi
.doi_type
== DMU_OTN_ZAP_METADATA
) {
409 int zaperr
= zap_lookup(mos
, ds
->ds_object
,
410 DS_FIELD_BOOKMARK_NAMES
,
411 sizeof (ds
->ds_bookmarks
), 1,
413 if (zaperr
!= ENOENT
)
417 if (zfs_flags
& ZFS_DEBUG_SNAPNAMES
)
418 err
= dsl_dataset_get_snapname(ds
);
419 if (err
== 0 && ds
->ds_phys
->ds_userrefs_obj
!= 0) {
421 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
422 ds
->ds_phys
->ds_userrefs_obj
,
427 if (err
== 0 && !dsl_dataset_is_snapshot(ds
)) {
428 err
= dsl_prop_get_int_ds(ds
,
429 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
432 err
= dsl_prop_get_int_ds(ds
,
433 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
437 ds
->ds_reserved
= ds
->ds_quota
= 0;
440 if (err
!= 0 || (winner
= dmu_buf_set_user_ie(dbuf
, ds
,
441 &ds
->ds_phys
, dsl_dataset_evict
)) != NULL
) {
442 bplist_destroy(&ds
->ds_pending_deadlist
);
443 dsl_deadlist_close(&ds
->ds_deadlist
);
445 dsl_dataset_rele(ds
->ds_prev
, ds
);
446 dsl_dir_rele(ds
->ds_dir
, ds
);
447 mutex_destroy(&ds
->ds_lock
);
448 mutex_destroy(&ds
->ds_opening_lock
);
449 refcount_destroy(&ds
->ds_longholds
);
450 kmem_free(ds
, sizeof (dsl_dataset_t
));
452 dmu_buf_rele(dbuf
, tag
);
458 unique_insert(ds
->ds_phys
->ds_fsid_guid
);
461 ASSERT3P(ds
->ds_dbuf
, ==, dbuf
);
462 ASSERT3P(ds
->ds_phys
, ==, dbuf
->db_data
);
463 ASSERT(ds
->ds_phys
->ds_prev_snap_obj
!= 0 ||
464 spa_version(dp
->dp_spa
) < SPA_VERSION_ORIGIN
||
465 dp
->dp_origin_snap
== NULL
|| ds
== dp
->dp_origin_snap
);
471 dsl_dataset_hold(dsl_pool_t
*dp
, const char *name
,
472 void *tag
, dsl_dataset_t
**dsp
)
475 const char *snapname
;
479 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, &snapname
);
483 ASSERT(dsl_pool_config_held(dp
));
484 obj
= dd
->dd_phys
->dd_head_dataset_obj
;
486 err
= dsl_dataset_hold_obj(dp
, obj
, tag
, dsp
);
488 err
= SET_ERROR(ENOENT
);
490 /* we may be looking for a snapshot */
491 if (err
== 0 && snapname
!= NULL
) {
494 if (*snapname
++ != '@') {
495 dsl_dataset_rele(*dsp
, tag
);
496 dsl_dir_rele(dd
, FTAG
);
497 return (SET_ERROR(ENOENT
));
500 dprintf("looking for snapshot '%s'\n", snapname
);
501 err
= dsl_dataset_snap_lookup(*dsp
, snapname
, &obj
);
503 err
= dsl_dataset_hold_obj(dp
, obj
, tag
, &ds
);
504 dsl_dataset_rele(*dsp
, tag
);
507 mutex_enter(&ds
->ds_lock
);
508 if (ds
->ds_snapname
[0] == 0)
509 (void) strlcpy(ds
->ds_snapname
, snapname
,
510 sizeof (ds
->ds_snapname
));
511 mutex_exit(&ds
->ds_lock
);
516 dsl_dir_rele(dd
, FTAG
);
521 dsl_dataset_own_obj(dsl_pool_t
*dp
, uint64_t dsobj
,
522 void *tag
, dsl_dataset_t
**dsp
)
524 int err
= dsl_dataset_hold_obj(dp
, dsobj
, tag
, dsp
);
527 if (!dsl_dataset_tryown(*dsp
, tag
)) {
528 dsl_dataset_rele(*dsp
, tag
);
530 return (SET_ERROR(EBUSY
));
536 dsl_dataset_own(dsl_pool_t
*dp
, const char *name
,
537 void *tag
, dsl_dataset_t
**dsp
)
539 int err
= dsl_dataset_hold(dp
, name
, tag
, dsp
);
542 if (!dsl_dataset_tryown(*dsp
, tag
)) {
543 dsl_dataset_rele(*dsp
, tag
);
544 return (SET_ERROR(EBUSY
));
550 * See the comment above dsl_pool_hold() for details. In summary, a long
551 * hold is used to prevent destruction of a dataset while the pool hold
552 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
554 * The dataset and pool must be held when this function is called. After it
555 * is called, the pool hold may be released while the dataset is still held
559 dsl_dataset_long_hold(dsl_dataset_t
*ds
, void *tag
)
561 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
562 (void) refcount_add(&ds
->ds_longholds
, tag
);
566 dsl_dataset_long_rele(dsl_dataset_t
*ds
, void *tag
)
568 (void) refcount_remove(&ds
->ds_longholds
, tag
);
571 /* Return B_TRUE if there are any long holds on this dataset. */
573 dsl_dataset_long_held(dsl_dataset_t
*ds
)
575 return (!refcount_is_zero(&ds
->ds_longholds
));
579 dsl_dataset_name(dsl_dataset_t
*ds
, char *name
)
582 (void) strcpy(name
, "mos");
584 dsl_dir_name(ds
->ds_dir
, name
);
585 VERIFY0(dsl_dataset_get_snapname(ds
));
586 if (ds
->ds_snapname
[0]) {
587 (void) strcat(name
, "@");
589 * We use a "recursive" mutex so that we
590 * can call dprintf_ds() with ds_lock held.
592 if (!MUTEX_HELD(&ds
->ds_lock
)) {
593 mutex_enter(&ds
->ds_lock
);
594 (void) strcat(name
, ds
->ds_snapname
);
595 mutex_exit(&ds
->ds_lock
);
597 (void) strcat(name
, ds
->ds_snapname
);
604 dsl_dataset_rele(dsl_dataset_t
*ds
, void *tag
)
606 dmu_buf_rele(ds
->ds_dbuf
, tag
);
610 dsl_dataset_disown(dsl_dataset_t
*ds
, void *tag
)
612 ASSERT(ds
->ds_owner
== tag
&& ds
->ds_dbuf
!= NULL
);
614 mutex_enter(&ds
->ds_lock
);
616 mutex_exit(&ds
->ds_lock
);
617 dsl_dataset_long_rele(ds
, tag
);
618 if (ds
->ds_dbuf
!= NULL
)
619 dsl_dataset_rele(ds
, tag
);
621 dsl_dataset_evict(NULL
, ds
);
625 dsl_dataset_tryown(dsl_dataset_t
*ds
, void *tag
)
627 boolean_t gotit
= FALSE
;
629 mutex_enter(&ds
->ds_lock
);
630 if (ds
->ds_owner
== NULL
&& !DS_IS_INCONSISTENT(ds
)) {
632 dsl_dataset_long_hold(ds
, tag
);
635 mutex_exit(&ds
->ds_lock
);
640 dsl_dataset_create_sync_dd(dsl_dir_t
*dd
, dsl_dataset_t
*origin
,
641 uint64_t flags
, dmu_tx_t
*tx
)
643 dsl_pool_t
*dp
= dd
->dd_pool
;
645 dsl_dataset_phys_t
*dsphys
;
647 objset_t
*mos
= dp
->dp_meta_objset
;
650 origin
= dp
->dp_origin_snap
;
652 ASSERT(origin
== NULL
|| origin
->ds_dir
->dd_pool
== dp
);
653 ASSERT(origin
== NULL
|| origin
->ds_phys
->ds_num_children
> 0);
654 ASSERT(dmu_tx_is_syncing(tx
));
655 ASSERT(dd
->dd_phys
->dd_head_dataset_obj
== 0);
657 dsobj
= dmu_object_alloc(mos
, DMU_OT_DSL_DATASET
, 0,
658 DMU_OT_DSL_DATASET
, sizeof (dsl_dataset_phys_t
), tx
);
659 VERIFY0(dmu_bonus_hold(mos
, dsobj
, FTAG
, &dbuf
));
660 dmu_buf_will_dirty(dbuf
, tx
);
661 dsphys
= dbuf
->db_data
;
662 bzero(dsphys
, sizeof (dsl_dataset_phys_t
));
663 dsphys
->ds_dir_obj
= dd
->dd_object
;
664 dsphys
->ds_flags
= flags
;
665 dsphys
->ds_fsid_guid
= unique_create();
666 (void) random_get_pseudo_bytes((void*)&dsphys
->ds_guid
,
667 sizeof (dsphys
->ds_guid
));
668 dsphys
->ds_snapnames_zapobj
=
669 zap_create_norm(mos
, U8_TEXTPREP_TOUPPER
, DMU_OT_DSL_DS_SNAP_MAP
,
671 dsphys
->ds_creation_time
= gethrestime_sec();
672 dsphys
->ds_creation_txg
= tx
->tx_txg
== TXG_INITIAL
? 1 : tx
->tx_txg
;
674 if (origin
== NULL
) {
675 dsphys
->ds_deadlist_obj
= dsl_deadlist_alloc(mos
, tx
);
677 dsl_dataset_t
*ohds
; /* head of the origin snapshot */
679 dsphys
->ds_prev_snap_obj
= origin
->ds_object
;
680 dsphys
->ds_prev_snap_txg
=
681 origin
->ds_phys
->ds_creation_txg
;
682 dsphys
->ds_referenced_bytes
=
683 origin
->ds_phys
->ds_referenced_bytes
;
684 dsphys
->ds_compressed_bytes
=
685 origin
->ds_phys
->ds_compressed_bytes
;
686 dsphys
->ds_uncompressed_bytes
=
687 origin
->ds_phys
->ds_uncompressed_bytes
;
688 dsphys
->ds_bp
= origin
->ds_phys
->ds_bp
;
691 * Inherit flags that describe the dataset's contents
692 * (INCONSISTENT) or properties (Case Insensitive).
694 dsphys
->ds_flags
|= origin
->ds_phys
->ds_flags
&
695 (DS_FLAG_INCONSISTENT
| DS_FLAG_CI_DATASET
);
697 dmu_buf_will_dirty(origin
->ds_dbuf
, tx
);
698 origin
->ds_phys
->ds_num_children
++;
700 VERIFY0(dsl_dataset_hold_obj(dp
,
701 origin
->ds_dir
->dd_phys
->dd_head_dataset_obj
, FTAG
, &ohds
));
702 dsphys
->ds_deadlist_obj
= dsl_deadlist_clone(&ohds
->ds_deadlist
,
703 dsphys
->ds_prev_snap_txg
, dsphys
->ds_prev_snap_obj
, tx
);
704 dsl_dataset_rele(ohds
, FTAG
);
706 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_NEXT_CLONES
) {
707 if (origin
->ds_phys
->ds_next_clones_obj
== 0) {
708 origin
->ds_phys
->ds_next_clones_obj
=
710 DMU_OT_NEXT_CLONES
, DMU_OT_NONE
, 0, tx
);
712 VERIFY0(zap_add_int(mos
,
713 origin
->ds_phys
->ds_next_clones_obj
, dsobj
, tx
));
716 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
717 dd
->dd_phys
->dd_origin_obj
= origin
->ds_object
;
718 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
719 if (origin
->ds_dir
->dd_phys
->dd_clones
== 0) {
720 dmu_buf_will_dirty(origin
->ds_dir
->dd_dbuf
, tx
);
721 origin
->ds_dir
->dd_phys
->dd_clones
=
723 DMU_OT_DSL_CLONES
, DMU_OT_NONE
, 0, tx
);
725 VERIFY0(zap_add_int(mos
,
726 origin
->ds_dir
->dd_phys
->dd_clones
, dsobj
, tx
));
730 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_UNIQUE_ACCURATE
)
731 dsphys
->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
733 dmu_buf_rele(dbuf
, FTAG
);
735 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
736 dd
->dd_phys
->dd_head_dataset_obj
= dsobj
;
742 dsl_dataset_zero_zil(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
746 VERIFY0(dmu_objset_from_ds(ds
, &os
));
747 bzero(&os
->os_zil_header
, sizeof (os
->os_zil_header
));
748 dsl_dataset_dirty(ds
, tx
);
752 dsl_dataset_create_sync(dsl_dir_t
*pdd
, const char *lastname
,
753 dsl_dataset_t
*origin
, uint64_t flags
, cred_t
*cr
, dmu_tx_t
*tx
)
755 dsl_pool_t
*dp
= pdd
->dd_pool
;
756 uint64_t dsobj
, ddobj
;
759 ASSERT(dmu_tx_is_syncing(tx
));
760 ASSERT(lastname
[0] != '@');
762 ddobj
= dsl_dir_create_sync(dp
, pdd
, lastname
, tx
);
763 VERIFY0(dsl_dir_hold_obj(dp
, ddobj
, lastname
, FTAG
, &dd
));
765 dsobj
= dsl_dataset_create_sync_dd(dd
, origin
,
766 flags
& ~DS_CREATE_FLAG_NODIRTY
, tx
);
768 dsl_deleg_set_create_perms(dd
, tx
, cr
);
770 dsl_dir_rele(dd
, FTAG
);
773 * If we are creating a clone, make sure we zero out any stale
774 * data from the origin snapshots zil header.
776 if (origin
!= NULL
&& !(flags
& DS_CREATE_FLAG_NODIRTY
)) {
779 VERIFY0(dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
780 dsl_dataset_zero_zil(ds
, tx
);
781 dsl_dataset_rele(ds
, FTAG
);
788 * The unique space in the head dataset can be calculated by subtracting
789 * the space used in the most recent snapshot, that is still being used
790 * in this file system, from the space currently in use. To figure out
791 * the space in the most recent snapshot still in use, we need to take
792 * the total space used in the snapshot and subtract out the space that
793 * has been freed up since the snapshot was taken.
796 dsl_dataset_recalc_head_uniq(dsl_dataset_t
*ds
)
799 uint64_t dlused
, dlcomp
, dluncomp
;
801 ASSERT(!dsl_dataset_is_snapshot(ds
));
803 if (ds
->ds_phys
->ds_prev_snap_obj
!= 0)
804 mrs_used
= ds
->ds_prev
->ds_phys
->ds_referenced_bytes
;
808 dsl_deadlist_space(&ds
->ds_deadlist
, &dlused
, &dlcomp
, &dluncomp
);
810 ASSERT3U(dlused
, <=, mrs_used
);
811 ds
->ds_phys
->ds_unique_bytes
=
812 ds
->ds_phys
->ds_referenced_bytes
- (mrs_used
- dlused
);
814 if (spa_version(ds
->ds_dir
->dd_pool
->dp_spa
) >=
815 SPA_VERSION_UNIQUE_ACCURATE
)
816 ds
->ds_phys
->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
820 dsl_dataset_remove_from_next_clones(dsl_dataset_t
*ds
, uint64_t obj
,
823 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
825 ASSERTV(uint64_t count
);
827 ASSERT(ds
->ds_phys
->ds_num_children
>= 2);
828 err
= zap_remove_int(mos
, ds
->ds_phys
->ds_next_clones_obj
, obj
, tx
);
830 * The err should not be ENOENT, but a bug in a previous version
831 * of the code could cause upgrade_clones_cb() to not set
832 * ds_next_snap_obj when it should, leading to a missing entry.
833 * If we knew that the pool was created after
834 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
835 * ENOENT. However, at least we can check that we don't have
836 * too many entries in the next_clones_obj even after failing to
841 ASSERT0(zap_count(mos
, ds
->ds_phys
->ds_next_clones_obj
,
843 ASSERT3U(count
, <=, ds
->ds_phys
->ds_num_children
- 2);
848 dsl_dataset_get_blkptr(dsl_dataset_t
*ds
)
850 return (&ds
->ds_phys
->ds_bp
);
854 dsl_dataset_set_blkptr(dsl_dataset_t
*ds
, blkptr_t
*bp
, dmu_tx_t
*tx
)
856 ASSERT(dmu_tx_is_syncing(tx
));
857 /* If it's the meta-objset, set dp_meta_rootbp */
859 tx
->tx_pool
->dp_meta_rootbp
= *bp
;
861 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
862 ds
->ds_phys
->ds_bp
= *bp
;
867 dsl_dataset_get_spa(dsl_dataset_t
*ds
)
869 return (ds
->ds_dir
->dd_pool
->dp_spa
);
873 dsl_dataset_dirty(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
877 if (ds
== NULL
) /* this is the meta-objset */
880 ASSERT(ds
->ds_objset
!= NULL
);
882 if (ds
->ds_phys
->ds_next_snap_obj
!= 0)
883 panic("dirtying snapshot!");
885 dp
= ds
->ds_dir
->dd_pool
;
887 if (txg_list_add(&dp
->dp_dirty_datasets
, ds
, tx
->tx_txg
)) {
888 /* up the hold count until we can be written out */
889 dmu_buf_add_ref(ds
->ds_dbuf
, ds
);
894 dsl_dataset_is_dirty(dsl_dataset_t
*ds
)
898 for (t
= 0; t
< TXG_SIZE
; t
++) {
899 if (txg_list_member(&ds
->ds_dir
->dd_pool
->dp_dirty_datasets
,
907 dsl_dataset_snapshot_reserve_space(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
911 if (!dmu_tx_is_syncing(tx
))
915 * If there's an fs-only reservation, any blocks that might become
916 * owned by the snapshot dataset must be accommodated by space
917 * outside of the reservation.
919 ASSERT(ds
->ds_reserved
== 0 || DS_UNIQUE_IS_ACCURATE(ds
));
920 asize
= MIN(ds
->ds_phys
->ds_unique_bytes
, ds
->ds_reserved
);
921 if (asize
> dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
))
922 return (SET_ERROR(ENOSPC
));
925 * Propagate any reserved space for this snapshot to other
926 * snapshot checks in this sync group.
929 dsl_dir_willuse_space(ds
->ds_dir
, asize
, tx
);
934 typedef struct dsl_dataset_snapshot_arg
{
935 nvlist_t
*ddsa_snaps
;
936 nvlist_t
*ddsa_props
;
937 nvlist_t
*ddsa_errors
;
938 } dsl_dataset_snapshot_arg_t
;
941 dsl_dataset_snapshot_check_impl(dsl_dataset_t
*ds
, const char *snapname
,
942 dmu_tx_t
*tx
, boolean_t recv
)
947 ds
->ds_trysnap_txg
= tx
->tx_txg
;
949 if (!dmu_tx_is_syncing(tx
))
953 * We don't allow multiple snapshots of the same txg. If there
954 * is already one, try again.
956 if (ds
->ds_phys
->ds_prev_snap_txg
>= tx
->tx_txg
)
957 return (SET_ERROR(EAGAIN
));
960 * Check for conflicting snapshot name.
962 error
= dsl_dataset_snap_lookup(ds
, snapname
, &value
);
964 return (SET_ERROR(EEXIST
));
969 * We don't allow taking snapshots of inconsistent datasets, such as
970 * those into which we are currently receiving. However, if we are
971 * creating this snapshot as part of a receive, this check will be
972 * executed atomically with respect to the completion of the receive
973 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
974 * case we ignore this, knowing it will be fixed up for us shortly in
975 * dmu_recv_end_sync().
977 if (!recv
&& DS_IS_INCONSISTENT(ds
))
978 return (SET_ERROR(EBUSY
));
980 error
= dsl_dataset_snapshot_reserve_space(ds
, tx
);
988 dsl_dataset_snapshot_check(void *arg
, dmu_tx_t
*tx
)
990 dsl_dataset_snapshot_arg_t
*ddsa
= arg
;
991 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
995 for (pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, NULL
);
996 pair
!= NULL
; pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, pair
)) {
1000 char dsname
[MAXNAMELEN
];
1002 name
= nvpair_name(pair
);
1003 if (strlen(name
) >= MAXNAMELEN
)
1004 error
= SET_ERROR(ENAMETOOLONG
);
1006 atp
= strchr(name
, '@');
1008 error
= SET_ERROR(EINVAL
);
1010 (void) strlcpy(dsname
, name
, atp
- name
+ 1);
1013 error
= dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
);
1015 error
= dsl_dataset_snapshot_check_impl(ds
,
1016 atp
+ 1, tx
, B_FALSE
);
1017 dsl_dataset_rele(ds
, FTAG
);
1021 if (ddsa
->ddsa_errors
!= NULL
) {
1022 fnvlist_add_int32(ddsa
->ddsa_errors
,
1032 dsl_dataset_snapshot_sync_impl(dsl_dataset_t
*ds
, const char *snapname
,
1035 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1037 dsl_dataset_phys_t
*dsphys
;
1038 uint64_t dsobj
, crtxg
;
1039 objset_t
*mos
= dp
->dp_meta_objset
;
1040 ASSERTV(static zil_header_t zero_zil
);
1041 ASSERTV(objset_t
*os
);
1043 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
1046 * If we are on an old pool, the zil must not be active, in which
1047 * case it will be zeroed. Usually zil_suspend() accomplishes this.
1049 ASSERT(spa_version(dmu_tx_pool(tx
)->dp_spa
) >= SPA_VERSION_FAST_SNAP
||
1050 dmu_objset_from_ds(ds
, &os
) != 0 ||
1051 bcmp(&os
->os_phys
->os_zil_header
, &zero_zil
,
1052 sizeof (zero_zil
)) == 0);
1056 * The origin's ds_creation_txg has to be < TXG_INITIAL
1058 if (strcmp(snapname
, ORIGIN_DIR_NAME
) == 0)
1063 dsobj
= dmu_object_alloc(mos
, DMU_OT_DSL_DATASET
, 0,
1064 DMU_OT_DSL_DATASET
, sizeof (dsl_dataset_phys_t
), tx
);
1065 VERIFY0(dmu_bonus_hold(mos
, dsobj
, FTAG
, &dbuf
));
1066 dmu_buf_will_dirty(dbuf
, tx
);
1067 dsphys
= dbuf
->db_data
;
1068 bzero(dsphys
, sizeof (dsl_dataset_phys_t
));
1069 dsphys
->ds_dir_obj
= ds
->ds_dir
->dd_object
;
1070 dsphys
->ds_fsid_guid
= unique_create();
1071 (void) random_get_pseudo_bytes((void*)&dsphys
->ds_guid
,
1072 sizeof (dsphys
->ds_guid
));
1073 dsphys
->ds_prev_snap_obj
= ds
->ds_phys
->ds_prev_snap_obj
;
1074 dsphys
->ds_prev_snap_txg
= ds
->ds_phys
->ds_prev_snap_txg
;
1075 dsphys
->ds_next_snap_obj
= ds
->ds_object
;
1076 dsphys
->ds_num_children
= 1;
1077 dsphys
->ds_creation_time
= gethrestime_sec();
1078 dsphys
->ds_creation_txg
= crtxg
;
1079 dsphys
->ds_deadlist_obj
= ds
->ds_phys
->ds_deadlist_obj
;
1080 dsphys
->ds_referenced_bytes
= ds
->ds_phys
->ds_referenced_bytes
;
1081 dsphys
->ds_compressed_bytes
= ds
->ds_phys
->ds_compressed_bytes
;
1082 dsphys
->ds_uncompressed_bytes
= ds
->ds_phys
->ds_uncompressed_bytes
;
1083 dsphys
->ds_flags
= ds
->ds_phys
->ds_flags
;
1084 dsphys
->ds_bp
= ds
->ds_phys
->ds_bp
;
1085 dmu_buf_rele(dbuf
, FTAG
);
1087 ASSERT3U(ds
->ds_prev
!= 0, ==, ds
->ds_phys
->ds_prev_snap_obj
!= 0);
1089 uint64_t next_clones_obj
=
1090 ds
->ds_prev
->ds_phys
->ds_next_clones_obj
;
1091 ASSERT(ds
->ds_prev
->ds_phys
->ds_next_snap_obj
==
1093 ds
->ds_prev
->ds_phys
->ds_num_children
> 1);
1094 if (ds
->ds_prev
->ds_phys
->ds_next_snap_obj
== ds
->ds_object
) {
1095 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
1096 ASSERT3U(ds
->ds_phys
->ds_prev_snap_txg
, ==,
1097 ds
->ds_prev
->ds_phys
->ds_creation_txg
);
1098 ds
->ds_prev
->ds_phys
->ds_next_snap_obj
= dsobj
;
1099 } else if (next_clones_obj
!= 0) {
1100 dsl_dataset_remove_from_next_clones(ds
->ds_prev
,
1101 dsphys
->ds_next_snap_obj
, tx
);
1102 VERIFY0(zap_add_int(mos
,
1103 next_clones_obj
, dsobj
, tx
));
1108 * If we have a reference-reservation on this dataset, we will
1109 * need to increase the amount of refreservation being charged
1110 * since our unique space is going to zero.
1112 if (ds
->ds_reserved
) {
1114 ASSERT(DS_UNIQUE_IS_ACCURATE(ds
));
1115 delta
= MIN(ds
->ds_phys
->ds_unique_bytes
, ds
->ds_reserved
);
1116 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_REFRSRV
,
1120 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1121 ds
->ds_phys
->ds_deadlist_obj
= dsl_deadlist_clone(&ds
->ds_deadlist
,
1122 UINT64_MAX
, ds
->ds_phys
->ds_prev_snap_obj
, tx
);
1123 dsl_deadlist_close(&ds
->ds_deadlist
);
1124 dsl_deadlist_open(&ds
->ds_deadlist
, mos
, ds
->ds_phys
->ds_deadlist_obj
);
1125 dsl_deadlist_add_key(&ds
->ds_deadlist
,
1126 ds
->ds_phys
->ds_prev_snap_txg
, tx
);
1128 ASSERT3U(ds
->ds_phys
->ds_prev_snap_txg
, <, tx
->tx_txg
);
1129 ds
->ds_phys
->ds_prev_snap_obj
= dsobj
;
1130 ds
->ds_phys
->ds_prev_snap_txg
= crtxg
;
1131 ds
->ds_phys
->ds_unique_bytes
= 0;
1132 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_UNIQUE_ACCURATE
)
1133 ds
->ds_phys
->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
1135 VERIFY0(zap_add(mos
, ds
->ds_phys
->ds_snapnames_zapobj
,
1136 snapname
, 8, 1, &dsobj
, tx
));
1139 dsl_dataset_rele(ds
->ds_prev
, ds
);
1140 VERIFY0(dsl_dataset_hold_obj(dp
,
1141 ds
->ds_phys
->ds_prev_snap_obj
, ds
, &ds
->ds_prev
));
1143 dsl_scan_ds_snapshotted(ds
, tx
);
1145 dsl_dir_snap_cmtime_update(ds
->ds_dir
);
1147 spa_history_log_internal_ds(ds
->ds_prev
, "snapshot", tx
, "");
1151 dsl_dataset_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
1153 dsl_dataset_snapshot_arg_t
*ddsa
= arg
;
1154 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1157 for (pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, NULL
);
1158 pair
!= NULL
; pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, pair
)) {
1161 char dsname
[MAXNAMELEN
];
1163 name
= nvpair_name(pair
);
1164 atp
= strchr(name
, '@');
1165 (void) strlcpy(dsname
, name
, atp
- name
+ 1);
1166 VERIFY0(dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
));
1168 dsl_dataset_snapshot_sync_impl(ds
, atp
+ 1, tx
);
1169 if (ddsa
->ddsa_props
!= NULL
) {
1170 dsl_props_set_sync_impl(ds
->ds_prev
,
1171 ZPROP_SRC_LOCAL
, ddsa
->ddsa_props
, tx
);
1173 dsl_dataset_rele(ds
, FTAG
);
1178 * The snapshots must all be in the same pool.
1179 * All-or-nothing: if there are any failures, nothing will be modified.
1182 dsl_dataset_snapshot(nvlist_t
*snaps
, nvlist_t
*props
, nvlist_t
*errors
)
1184 dsl_dataset_snapshot_arg_t ddsa
;
1186 boolean_t needsuspend
;
1190 nvlist_t
*suspended
= NULL
;
1192 pair
= nvlist_next_nvpair(snaps
, NULL
);
1195 firstname
= nvpair_name(pair
);
1197 error
= spa_open(firstname
, &spa
, FTAG
);
1200 needsuspend
= (spa_version(spa
) < SPA_VERSION_FAST_SNAP
);
1201 spa_close(spa
, FTAG
);
1204 suspended
= fnvlist_alloc();
1205 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
1206 pair
= nvlist_next_nvpair(snaps
, pair
)) {
1207 char fsname
[MAXNAMELEN
];
1208 char *snapname
= nvpair_name(pair
);
1212 atp
= strchr(snapname
, '@');
1214 error
= SET_ERROR(EINVAL
);
1217 (void) strlcpy(fsname
, snapname
, atp
- snapname
+ 1);
1219 error
= zil_suspend(fsname
, &cookie
);
1222 fnvlist_add_uint64(suspended
, fsname
,
1227 ddsa
.ddsa_snaps
= snaps
;
1228 ddsa
.ddsa_props
= props
;
1229 ddsa
.ddsa_errors
= errors
;
1232 error
= dsl_sync_task(firstname
, dsl_dataset_snapshot_check
,
1233 dsl_dataset_snapshot_sync
, &ddsa
,
1234 fnvlist_num_pairs(snaps
) * 3);
1237 if (suspended
!= NULL
) {
1238 for (pair
= nvlist_next_nvpair(suspended
, NULL
); pair
!= NULL
;
1239 pair
= nvlist_next_nvpair(suspended
, pair
)) {
1240 zil_resume((void *)(uintptr_t)
1241 fnvpair_value_uint64(pair
));
1243 fnvlist_free(suspended
);
1248 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
1249 pair
= nvlist_next_nvpair(snaps
, pair
)) {
1250 char *snapname
= nvpair_name(pair
);
1251 zvol_create_minors(snapname
);
1259 typedef struct dsl_dataset_snapshot_tmp_arg
{
1260 const char *ddsta_fsname
;
1261 const char *ddsta_snapname
;
1262 minor_t ddsta_cleanup_minor
;
1263 const char *ddsta_htag
;
1264 } dsl_dataset_snapshot_tmp_arg_t
;
1267 dsl_dataset_snapshot_tmp_check(void *arg
, dmu_tx_t
*tx
)
1269 dsl_dataset_snapshot_tmp_arg_t
*ddsta
= arg
;
1270 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1274 error
= dsl_dataset_hold(dp
, ddsta
->ddsta_fsname
, FTAG
, &ds
);
1278 error
= dsl_dataset_snapshot_check_impl(ds
, ddsta
->ddsta_snapname
,
1281 dsl_dataset_rele(ds
, FTAG
);
1285 if (spa_version(dp
->dp_spa
) < SPA_VERSION_USERREFS
) {
1286 dsl_dataset_rele(ds
, FTAG
);
1287 return (SET_ERROR(ENOTSUP
));
1289 error
= dsl_dataset_user_hold_check_one(NULL
, ddsta
->ddsta_htag
,
1292 dsl_dataset_rele(ds
, FTAG
);
1296 dsl_dataset_rele(ds
, FTAG
);
1301 dsl_dataset_snapshot_tmp_sync(void *arg
, dmu_tx_t
*tx
)
1303 dsl_dataset_snapshot_tmp_arg_t
*ddsta
= arg
;
1304 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1307 VERIFY0(dsl_dataset_hold(dp
, ddsta
->ddsta_fsname
, FTAG
, &ds
));
1309 dsl_dataset_snapshot_sync_impl(ds
, ddsta
->ddsta_snapname
, tx
);
1310 dsl_dataset_user_hold_sync_one(ds
->ds_prev
, ddsta
->ddsta_htag
,
1311 ddsta
->ddsta_cleanup_minor
, gethrestime_sec(), tx
);
1312 dsl_destroy_snapshot_sync_impl(ds
->ds_prev
, B_TRUE
, tx
);
1314 dsl_dataset_rele(ds
, FTAG
);
1318 dsl_dataset_snapshot_tmp(const char *fsname
, const char *snapname
,
1319 minor_t cleanup_minor
, const char *htag
)
1321 dsl_dataset_snapshot_tmp_arg_t ddsta
;
1324 boolean_t needsuspend
;
1327 ddsta
.ddsta_fsname
= fsname
;
1328 ddsta
.ddsta_snapname
= snapname
;
1329 ddsta
.ddsta_cleanup_minor
= cleanup_minor
;
1330 ddsta
.ddsta_htag
= htag
;
1332 error
= spa_open(fsname
, &spa
, FTAG
);
1335 needsuspend
= (spa_version(spa
) < SPA_VERSION_FAST_SNAP
);
1336 spa_close(spa
, FTAG
);
1339 error
= zil_suspend(fsname
, &cookie
);
1344 error
= dsl_sync_task(fsname
, dsl_dataset_snapshot_tmp_check
,
1345 dsl_dataset_snapshot_tmp_sync
, &ddsta
, 3);
1354 dsl_dataset_sync(dsl_dataset_t
*ds
, zio_t
*zio
, dmu_tx_t
*tx
)
1356 ASSERT(dmu_tx_is_syncing(tx
));
1357 ASSERT(ds
->ds_objset
!= NULL
);
1358 ASSERT(ds
->ds_phys
->ds_next_snap_obj
== 0);
1361 * in case we had to change ds_fsid_guid when we opened it,
1364 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1365 ds
->ds_phys
->ds_fsid_guid
= ds
->ds_fsid_guid
;
1367 dmu_objset_sync(ds
->ds_objset
, zio
, tx
);
1371 get_clones_stat(dsl_dataset_t
*ds
, nvlist_t
*nv
)
1374 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
1377 nvlist_t
*propval
= fnvlist_alloc();
1378 nvlist_t
*val
= fnvlist_alloc();
1380 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1383 * There may be missing entries in ds_next_clones_obj
1384 * due to a bug in a previous version of the code.
1385 * Only trust it if it has the right number of entries.
1387 if (ds
->ds_phys
->ds_next_clones_obj
!= 0) {
1388 VERIFY0(zap_count(mos
, ds
->ds_phys
->ds_next_clones_obj
,
1391 if (count
!= ds
->ds_phys
->ds_num_children
- 1)
1393 for (zap_cursor_init(&zc
, mos
, ds
->ds_phys
->ds_next_clones_obj
);
1394 zap_cursor_retrieve(&zc
, &za
) == 0;
1395 zap_cursor_advance(&zc
)) {
1396 dsl_dataset_t
*clone
;
1397 char buf
[ZFS_MAXNAMELEN
];
1398 VERIFY0(dsl_dataset_hold_obj(ds
->ds_dir
->dd_pool
,
1399 za
.za_first_integer
, FTAG
, &clone
));
1400 dsl_dir_name(clone
->ds_dir
, buf
);
1401 fnvlist_add_boolean(val
, buf
);
1402 dsl_dataset_rele(clone
, FTAG
);
1404 zap_cursor_fini(&zc
);
1405 fnvlist_add_nvlist(propval
, ZPROP_VALUE
, val
);
1406 fnvlist_add_nvlist(nv
, zfs_prop_to_name(ZFS_PROP_CLONES
), propval
);
1409 nvlist_free(propval
);
1413 dsl_dataset_stats(dsl_dataset_t
*ds
, nvlist_t
*nv
)
1415 uint64_t refd
, avail
, uobjs
, aobjs
, ratio
;
1416 ASSERTV(dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
);
1418 ASSERT(dsl_pool_config_held(dp
));
1420 ratio
= ds
->ds_phys
->ds_compressed_bytes
== 0 ? 100 :
1421 (ds
->ds_phys
->ds_uncompressed_bytes
* 100 /
1422 ds
->ds_phys
->ds_compressed_bytes
);
1424 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFRATIO
, ratio
);
1425 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_LOGICALREFERENCED
,
1426 ds
->ds_phys
->ds_uncompressed_bytes
);
1428 if (dsl_dataset_is_snapshot(ds
)) {
1429 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_COMPRESSRATIO
, ratio
);
1430 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USED
,
1431 ds
->ds_phys
->ds_unique_bytes
);
1432 get_clones_stat(ds
, nv
);
1434 dsl_dir_stats(ds
->ds_dir
, nv
);
1437 dsl_dataset_space(ds
, &refd
, &avail
, &uobjs
, &aobjs
);
1438 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_AVAILABLE
, avail
);
1439 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFERENCED
, refd
);
1441 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_CREATION
,
1442 ds
->ds_phys
->ds_creation_time
);
1443 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_CREATETXG
,
1444 ds
->ds_phys
->ds_creation_txg
);
1445 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFQUOTA
,
1447 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFRESERVATION
,
1449 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_GUID
,
1450 ds
->ds_phys
->ds_guid
);
1451 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_UNIQUE
,
1452 ds
->ds_phys
->ds_unique_bytes
);
1453 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_OBJSETID
,
1455 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERREFS
,
1457 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_DEFER_DESTROY
,
1458 DS_IS_DEFER_DESTROY(ds
) ? 1 : 0);
1460 if (ds
->ds_phys
->ds_prev_snap_obj
!= 0) {
1461 uint64_t written
, comp
, uncomp
;
1462 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1463 dsl_dataset_t
*prev
;
1466 err
= dsl_dataset_hold_obj(dp
,
1467 ds
->ds_phys
->ds_prev_snap_obj
, FTAG
, &prev
);
1469 err
= dsl_dataset_space_written(prev
, ds
, &written
,
1471 dsl_dataset_rele(prev
, FTAG
);
1473 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_WRITTEN
,
1482 dsl_dataset_fast_stat(dsl_dataset_t
*ds
, dmu_objset_stats_t
*stat
)
1484 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1485 ASSERT(dsl_pool_config_held(dp
));
1487 stat
->dds_creation_txg
= ds
->ds_phys
->ds_creation_txg
;
1488 stat
->dds_inconsistent
= ds
->ds_phys
->ds_flags
& DS_FLAG_INCONSISTENT
;
1489 stat
->dds_guid
= ds
->ds_phys
->ds_guid
;
1490 stat
->dds_origin
[0] = '\0';
1491 if (dsl_dataset_is_snapshot(ds
)) {
1492 stat
->dds_is_snapshot
= B_TRUE
;
1493 stat
->dds_num_clones
= ds
->ds_phys
->ds_num_children
- 1;
1495 stat
->dds_is_snapshot
= B_FALSE
;
1496 stat
->dds_num_clones
= 0;
1498 if (dsl_dir_is_clone(ds
->ds_dir
)) {
1501 VERIFY0(dsl_dataset_hold_obj(dp
,
1502 ds
->ds_dir
->dd_phys
->dd_origin_obj
, FTAG
, &ods
));
1503 dsl_dataset_name(ods
, stat
->dds_origin
);
1504 dsl_dataset_rele(ods
, FTAG
);
1510 dsl_dataset_fsid_guid(dsl_dataset_t
*ds
)
1512 return (ds
->ds_fsid_guid
);
1516 dsl_dataset_space(dsl_dataset_t
*ds
,
1517 uint64_t *refdbytesp
, uint64_t *availbytesp
,
1518 uint64_t *usedobjsp
, uint64_t *availobjsp
)
1520 *refdbytesp
= ds
->ds_phys
->ds_referenced_bytes
;
1521 *availbytesp
= dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
);
1522 if (ds
->ds_reserved
> ds
->ds_phys
->ds_unique_bytes
)
1523 *availbytesp
+= ds
->ds_reserved
- ds
->ds_phys
->ds_unique_bytes
;
1524 if (ds
->ds_quota
!= 0) {
1526 * Adjust available bytes according to refquota
1528 if (*refdbytesp
< ds
->ds_quota
)
1529 *availbytesp
= MIN(*availbytesp
,
1530 ds
->ds_quota
- *refdbytesp
);
1534 *usedobjsp
= BP_GET_FILL(&ds
->ds_phys
->ds_bp
);
1535 *availobjsp
= DN_MAX_OBJECT
- *usedobjsp
;
1539 dsl_dataset_modified_since_snap(dsl_dataset_t
*ds
, dsl_dataset_t
*snap
)
1541 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1544 if (ds
->ds_phys
->ds_bp
.blk_birth
>
1545 snap
->ds_phys
->ds_creation_txg
) {
1546 objset_t
*os
, *os_snap
;
1548 * It may be that only the ZIL differs, because it was
1549 * reset in the head. Don't count that as being
1552 if (dmu_objset_from_ds(ds
, &os
) != 0)
1554 if (dmu_objset_from_ds(snap
, &os_snap
) != 0)
1556 return (bcmp(&os
->os_phys
->os_meta_dnode
,
1557 &os_snap
->os_phys
->os_meta_dnode
,
1558 sizeof (os
->os_phys
->os_meta_dnode
)) != 0);
1563 typedef struct dsl_dataset_rename_snapshot_arg
{
1564 const char *ddrsa_fsname
;
1565 const char *ddrsa_oldsnapname
;
1566 const char *ddrsa_newsnapname
;
1567 boolean_t ddrsa_recursive
;
1569 } dsl_dataset_rename_snapshot_arg_t
;
1573 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t
*dp
,
1574 dsl_dataset_t
*hds
, void *arg
)
1576 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1580 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_oldsnapname
, &val
);
1582 /* ignore nonexistent snapshots */
1583 return (error
== ENOENT
? 0 : error
);
1586 /* new name should not exist */
1587 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_newsnapname
, &val
);
1589 error
= SET_ERROR(EEXIST
);
1590 else if (error
== ENOENT
)
1593 /* dataset name + 1 for the "@" + the new snapshot name must fit */
1594 if (dsl_dir_namelen(hds
->ds_dir
) + 1 +
1595 strlen(ddrsa
->ddrsa_newsnapname
) >= MAXNAMELEN
)
1596 error
= SET_ERROR(ENAMETOOLONG
);
1602 dsl_dataset_rename_snapshot_check(void *arg
, dmu_tx_t
*tx
)
1604 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1605 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1609 error
= dsl_dataset_hold(dp
, ddrsa
->ddrsa_fsname
, FTAG
, &hds
);
1613 if (ddrsa
->ddrsa_recursive
) {
1614 error
= dmu_objset_find_dp(dp
, hds
->ds_dir
->dd_object
,
1615 dsl_dataset_rename_snapshot_check_impl
, ddrsa
,
1618 error
= dsl_dataset_rename_snapshot_check_impl(dp
, hds
, ddrsa
);
1620 dsl_dataset_rele(hds
, FTAG
);
1625 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t
*dp
,
1626 dsl_dataset_t
*hds
, void *arg
)
1628 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1631 dmu_tx_t
*tx
= ddrsa
->ddrsa_tx
;
1634 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_oldsnapname
, &val
);
1635 ASSERT(error
== 0 || error
== ENOENT
);
1636 if (error
== ENOENT
) {
1637 /* ignore nonexistent snapshots */
1641 VERIFY0(dsl_dataset_hold_obj(dp
, val
, FTAG
, &ds
));
1643 /* log before we change the name */
1644 spa_history_log_internal_ds(ds
, "rename", tx
,
1645 "-> @%s", ddrsa
->ddrsa_newsnapname
);
1647 VERIFY0(dsl_dataset_snap_remove(hds
, ddrsa
->ddrsa_oldsnapname
, tx
));
1648 mutex_enter(&ds
->ds_lock
);
1649 (void) strcpy(ds
->ds_snapname
, ddrsa
->ddrsa_newsnapname
);
1650 mutex_exit(&ds
->ds_lock
);
1651 VERIFY0(zap_add(dp
->dp_meta_objset
, hds
->ds_phys
->ds_snapnames_zapobj
,
1652 ds
->ds_snapname
, 8, 1, &ds
->ds_object
, tx
));
1654 dsl_dataset_rele(ds
, FTAG
);
1659 dsl_dataset_rename_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
1661 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1662 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1665 VERIFY0(dsl_dataset_hold(dp
, ddrsa
->ddrsa_fsname
, FTAG
, &hds
));
1666 ddrsa
->ddrsa_tx
= tx
;
1667 if (ddrsa
->ddrsa_recursive
) {
1668 VERIFY0(dmu_objset_find_dp(dp
, hds
->ds_dir
->dd_object
,
1669 dsl_dataset_rename_snapshot_sync_impl
, ddrsa
,
1672 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp
, hds
, ddrsa
));
1674 dsl_dataset_rele(hds
, FTAG
);
1678 dsl_dataset_rename_snapshot(const char *fsname
,
1679 const char *oldsnapname
, const char *newsnapname
, boolean_t recursive
)
1682 char *oldname
, *newname
;
1686 dsl_dataset_rename_snapshot_arg_t ddrsa
;
1688 ddrsa
.ddrsa_fsname
= fsname
;
1689 ddrsa
.ddrsa_oldsnapname
= oldsnapname
;
1690 ddrsa
.ddrsa_newsnapname
= newsnapname
;
1691 ddrsa
.ddrsa_recursive
= recursive
;
1693 error
= dsl_sync_task(fsname
, dsl_dataset_rename_snapshot_check
,
1694 dsl_dataset_rename_snapshot_sync
, &ddrsa
, 1);
1697 return (SET_ERROR(error
));
1700 oldname
= kmem_asprintf("%s@%s", fsname
, oldsnapname
);
1701 newname
= kmem_asprintf("%s@%s", fsname
, newsnapname
);
1702 zvol_rename_minors(oldname
, newname
);
1711 * If we're doing an ownership handoff, we need to make sure that there is
1712 * only one long hold on the dataset. We're not allowed to change anything here
1713 * so we don't permanently release the long hold or regular hold here. We want
1714 * to do this only when syncing to avoid the dataset unexpectedly going away
1715 * when we release the long hold.
1718 dsl_dataset_handoff_check(dsl_dataset_t
*ds
, void *owner
, dmu_tx_t
*tx
)
1722 if (!dmu_tx_is_syncing(tx
))
1725 if (owner
!= NULL
) {
1726 VERIFY3P(ds
->ds_owner
, ==, owner
);
1727 dsl_dataset_long_rele(ds
, owner
);
1730 held
= dsl_dataset_long_held(ds
);
1733 dsl_dataset_long_hold(ds
, owner
);
1736 return (SET_ERROR(EBUSY
));
1741 typedef struct dsl_dataset_rollback_arg
{
1742 const char *ddra_fsname
;
1744 nvlist_t
*ddra_result
;
1745 } dsl_dataset_rollback_arg_t
;
1748 dsl_dataset_rollback_check(void *arg
, dmu_tx_t
*tx
)
1750 dsl_dataset_rollback_arg_t
*ddra
= arg
;
1751 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1753 int64_t unused_refres_delta
;
1756 nvlist_t
*proprequest
, *bookmarks
;
1758 error
= dsl_dataset_hold(dp
, ddra
->ddra_fsname
, FTAG
, &ds
);
1762 /* must not be a snapshot */
1763 if (dsl_dataset_is_snapshot(ds
)) {
1764 dsl_dataset_rele(ds
, FTAG
);
1765 return (SET_ERROR(EINVAL
));
1768 /* must have a most recent snapshot */
1769 if (ds
->ds_phys
->ds_prev_snap_txg
< TXG_INITIAL
) {
1770 dsl_dataset_rele(ds
, FTAG
);
1771 return (SET_ERROR(EINVAL
));
1774 /* must not have any bookmarks after the most recent snapshot */
1775 proprequest
= fnvlist_alloc();
1776 fnvlist_add_boolean(proprequest
, zfs_prop_to_name(ZFS_PROP_CREATETXG
));
1777 bookmarks
= fnvlist_alloc();
1778 error
= dsl_get_bookmarks_impl(ds
, proprequest
, bookmarks
);
1779 fnvlist_free(proprequest
);
1782 for (pair
= nvlist_next_nvpair(bookmarks
, NULL
);
1783 pair
!= NULL
; pair
= nvlist_next_nvpair(bookmarks
, pair
)) {
1785 fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair
),
1786 zfs_prop_to_name(ZFS_PROP_CREATETXG
));
1787 uint64_t createtxg
= fnvlist_lookup_uint64(valuenv
, "value");
1788 if (createtxg
> ds
->ds_phys
->ds_prev_snap_txg
) {
1789 fnvlist_free(bookmarks
);
1790 dsl_dataset_rele(ds
, FTAG
);
1791 return (SET_ERROR(EEXIST
));
1794 fnvlist_free(bookmarks
);
1796 error
= dsl_dataset_handoff_check(ds
, ddra
->ddra_owner
, tx
);
1798 dsl_dataset_rele(ds
, FTAG
);
1803 * Check if the snap we are rolling back to uses more than
1806 if (ds
->ds_quota
!= 0 &&
1807 ds
->ds_prev
->ds_phys
->ds_referenced_bytes
> ds
->ds_quota
) {
1808 dsl_dataset_rele(ds
, FTAG
);
1809 return (SET_ERROR(EDQUOT
));
1813 * When we do the clone swap, we will temporarily use more space
1814 * due to the refreservation (the head will no longer have any
1815 * unique space, so the entire amount of the refreservation will need
1816 * to be free). We will immediately destroy the clone, freeing
1817 * this space, but the freeing happens over many txg's.
1819 unused_refres_delta
= (int64_t)MIN(ds
->ds_reserved
,
1820 ds
->ds_phys
->ds_unique_bytes
);
1822 if (unused_refres_delta
> 0 &&
1823 unused_refres_delta
>
1824 dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
)) {
1825 dsl_dataset_rele(ds
, FTAG
);
1826 return (SET_ERROR(ENOSPC
));
1829 dsl_dataset_rele(ds
, FTAG
);
1834 dsl_dataset_rollback_sync(void *arg
, dmu_tx_t
*tx
)
1836 dsl_dataset_rollback_arg_t
*ddra
= arg
;
1837 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1838 dsl_dataset_t
*ds
, *clone
;
1840 char namebuf
[ZFS_MAXNAMELEN
];
1842 VERIFY0(dsl_dataset_hold(dp
, ddra
->ddra_fsname
, FTAG
, &ds
));
1844 dsl_dataset_name(ds
->ds_prev
, namebuf
);
1845 fnvlist_add_string(ddra
->ddra_result
, "target", namebuf
);
1847 cloneobj
= dsl_dataset_create_sync(ds
->ds_dir
, "%rollback",
1848 ds
->ds_prev
, DS_CREATE_FLAG_NODIRTY
, kcred
, tx
);
1850 VERIFY0(dsl_dataset_hold_obj(dp
, cloneobj
, FTAG
, &clone
));
1852 dsl_dataset_clone_swap_sync_impl(clone
, ds
, tx
);
1853 dsl_dataset_zero_zil(ds
, tx
);
1855 dsl_destroy_head_sync_impl(clone
, tx
);
1857 dsl_dataset_rele(clone
, FTAG
);
1858 dsl_dataset_rele(ds
, FTAG
);
1862 * Rolls back the given filesystem or volume to the most recent snapshot.
1863 * The name of the most recent snapshot will be returned under key "target"
1864 * in the result nvlist.
1867 * - The existing dataset MUST be owned by the specified owner at entry
1868 * - Upon return, dataset will still be held by the same owner, whether we
1871 * This mode is required any time the existing filesystem is mounted. See
1872 * notes above zfs_suspend_fs() for further details.
1875 dsl_dataset_rollback(const char *fsname
, void *owner
, nvlist_t
*result
)
1877 dsl_dataset_rollback_arg_t ddra
;
1879 ddra
.ddra_fsname
= fsname
;
1880 ddra
.ddra_owner
= owner
;
1881 ddra
.ddra_result
= result
;
1883 return (dsl_sync_task(fsname
, dsl_dataset_rollback_check
,
1884 dsl_dataset_rollback_sync
, &ddra
, 1));
1887 struct promotenode
{
1892 typedef struct dsl_dataset_promote_arg
{
1893 const char *ddpa_clonename
;
1894 dsl_dataset_t
*ddpa_clone
;
1895 list_t shared_snaps
, origin_snaps
, clone_snaps
;
1896 dsl_dataset_t
*origin_origin
; /* origin of the origin */
1897 uint64_t used
, comp
, uncomp
, unique
, cloneusedsnap
, originusedsnap
;
1899 } dsl_dataset_promote_arg_t
;
1901 static int snaplist_space(list_t
*l
, uint64_t mintxg
, uint64_t *spacep
);
1902 static int promote_hold(dsl_dataset_promote_arg_t
*ddpa
, dsl_pool_t
*dp
,
1904 static void promote_rele(dsl_dataset_promote_arg_t
*ddpa
, void *tag
);
1907 dsl_dataset_promote_check(void *arg
, dmu_tx_t
*tx
)
1909 dsl_dataset_promote_arg_t
*ddpa
= arg
;
1910 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1912 struct promotenode
*snap
;
1913 dsl_dataset_t
*origin_ds
;
1917 err
= promote_hold(ddpa
, dp
, FTAG
);
1921 hds
= ddpa
->ddpa_clone
;
1923 if (hds
->ds_phys
->ds_flags
& DS_FLAG_NOPROMOTE
) {
1924 promote_rele(ddpa
, FTAG
);
1925 return (SET_ERROR(EXDEV
));
1929 * Compute and check the amount of space to transfer. Since this is
1930 * so expensive, don't do the preliminary check.
1932 if (!dmu_tx_is_syncing(tx
)) {
1933 promote_rele(ddpa
, FTAG
);
1937 snap
= list_head(&ddpa
->shared_snaps
);
1938 origin_ds
= snap
->ds
;
1940 /* compute origin's new unique space */
1941 snap
= list_tail(&ddpa
->clone_snaps
);
1942 ASSERT3U(snap
->ds
->ds_phys
->ds_prev_snap_obj
, ==, origin_ds
->ds_object
);
1943 dsl_deadlist_space_range(&snap
->ds
->ds_deadlist
,
1944 origin_ds
->ds_phys
->ds_prev_snap_txg
, UINT64_MAX
,
1945 &ddpa
->unique
, &unused
, &unused
);
1948 * Walk the snapshots that we are moving
1950 * Compute space to transfer. Consider the incremental changes
1951 * to used by each snapshot:
1952 * (my used) = (prev's used) + (blocks born) - (blocks killed)
1953 * So each snapshot gave birth to:
1954 * (blocks born) = (my used) - (prev's used) + (blocks killed)
1955 * So a sequence would look like:
1956 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
1957 * Which simplifies to:
1958 * uN + kN + kN-1 + ... + k1 + k0
1959 * Note however, if we stop before we reach the ORIGIN we get:
1960 * uN + kN + kN-1 + ... + kM - uM-1
1962 ddpa
->used
= origin_ds
->ds_phys
->ds_referenced_bytes
;
1963 ddpa
->comp
= origin_ds
->ds_phys
->ds_compressed_bytes
;
1964 ddpa
->uncomp
= origin_ds
->ds_phys
->ds_uncompressed_bytes
;
1965 for (snap
= list_head(&ddpa
->shared_snaps
); snap
;
1966 snap
= list_next(&ddpa
->shared_snaps
, snap
)) {
1967 uint64_t val
, dlused
, dlcomp
, dluncomp
;
1968 dsl_dataset_t
*ds
= snap
->ds
;
1971 * If there are long holds, we won't be able to evict
1974 if (dsl_dataset_long_held(ds
)) {
1975 err
= SET_ERROR(EBUSY
);
1979 /* Check that the snapshot name does not conflict */
1980 VERIFY0(dsl_dataset_get_snapname(ds
));
1981 err
= dsl_dataset_snap_lookup(hds
, ds
->ds_snapname
, &val
);
1983 (void) strcpy(ddpa
->err_ds
, snap
->ds
->ds_snapname
);
1984 err
= SET_ERROR(EEXIST
);
1990 /* The very first snapshot does not have a deadlist */
1991 if (ds
->ds_phys
->ds_prev_snap_obj
== 0)
1994 dsl_deadlist_space(&ds
->ds_deadlist
,
1995 &dlused
, &dlcomp
, &dluncomp
);
1996 ddpa
->used
+= dlused
;
1997 ddpa
->comp
+= dlcomp
;
1998 ddpa
->uncomp
+= dluncomp
;
2002 * If we are a clone of a clone then we never reached ORIGIN,
2003 * so we need to subtract out the clone origin's used space.
2005 if (ddpa
->origin_origin
) {
2006 ddpa
->used
-= ddpa
->origin_origin
->ds_phys
->ds_referenced_bytes
;
2007 ddpa
->comp
-= ddpa
->origin_origin
->ds_phys
->ds_compressed_bytes
;
2009 ddpa
->origin_origin
->ds_phys
->ds_uncompressed_bytes
;
2012 /* Check that there is enough space here */
2013 err
= dsl_dir_transfer_possible(origin_ds
->ds_dir
, hds
->ds_dir
,
2019 * Compute the amounts of space that will be used by snapshots
2020 * after the promotion (for both origin and clone). For each,
2021 * it is the amount of space that will be on all of their
2022 * deadlists (that was not born before their new origin).
2024 if (hds
->ds_dir
->dd_phys
->dd_flags
& DD_FLAG_USED_BREAKDOWN
) {
2028 * Note, typically this will not be a clone of a clone,
2029 * so dd_origin_txg will be < TXG_INITIAL, so
2030 * these snaplist_space() -> dsl_deadlist_space_range()
2031 * calls will be fast because they do not have to
2032 * iterate over all bps.
2034 snap
= list_head(&ddpa
->origin_snaps
);
2035 err
= snaplist_space(&ddpa
->shared_snaps
,
2036 snap
->ds
->ds_dir
->dd_origin_txg
, &ddpa
->cloneusedsnap
);
2040 err
= snaplist_space(&ddpa
->clone_snaps
,
2041 snap
->ds
->ds_dir
->dd_origin_txg
, &space
);
2044 ddpa
->cloneusedsnap
+= space
;
2046 if (origin_ds
->ds_dir
->dd_phys
->dd_flags
& DD_FLAG_USED_BREAKDOWN
) {
2047 err
= snaplist_space(&ddpa
->origin_snaps
,
2048 origin_ds
->ds_phys
->ds_creation_txg
, &ddpa
->originusedsnap
);
2054 promote_rele(ddpa
, FTAG
);
2059 dsl_dataset_promote_sync(void *arg
, dmu_tx_t
*tx
)
2061 dsl_dataset_promote_arg_t
*ddpa
= arg
;
2062 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2064 struct promotenode
*snap
;
2065 dsl_dataset_t
*origin_ds
;
2066 dsl_dataset_t
*origin_head
;
2068 dsl_dir_t
*odd
= NULL
;
2069 uint64_t oldnext_obj
;
2072 VERIFY0(promote_hold(ddpa
, dp
, FTAG
));
2073 hds
= ddpa
->ddpa_clone
;
2075 ASSERT0(hds
->ds_phys
->ds_flags
& DS_FLAG_NOPROMOTE
);
2077 snap
= list_head(&ddpa
->shared_snaps
);
2078 origin_ds
= snap
->ds
;
2081 snap
= list_head(&ddpa
->origin_snaps
);
2082 origin_head
= snap
->ds
;
2085 * We need to explicitly open odd, since origin_ds's dd will be
2088 VERIFY0(dsl_dir_hold_obj(dp
, origin_ds
->ds_dir
->dd_object
,
2091 /* change origin's next snap */
2092 dmu_buf_will_dirty(origin_ds
->ds_dbuf
, tx
);
2093 oldnext_obj
= origin_ds
->ds_phys
->ds_next_snap_obj
;
2094 snap
= list_tail(&ddpa
->clone_snaps
);
2095 ASSERT3U(snap
->ds
->ds_phys
->ds_prev_snap_obj
, ==, origin_ds
->ds_object
);
2096 origin_ds
->ds_phys
->ds_next_snap_obj
= snap
->ds
->ds_object
;
2098 /* change the origin's next clone */
2099 if (origin_ds
->ds_phys
->ds_next_clones_obj
) {
2100 dsl_dataset_remove_from_next_clones(origin_ds
,
2101 snap
->ds
->ds_object
, tx
);
2102 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2103 origin_ds
->ds_phys
->ds_next_clones_obj
,
2108 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
2109 ASSERT3U(dd
->dd_phys
->dd_origin_obj
, ==, origin_ds
->ds_object
);
2110 dd
->dd_phys
->dd_origin_obj
= odd
->dd_phys
->dd_origin_obj
;
2111 dd
->dd_origin_txg
= origin_head
->ds_dir
->dd_origin_txg
;
2112 dmu_buf_will_dirty(odd
->dd_dbuf
, tx
);
2113 odd
->dd_phys
->dd_origin_obj
= origin_ds
->ds_object
;
2114 origin_head
->ds_dir
->dd_origin_txg
=
2115 origin_ds
->ds_phys
->ds_creation_txg
;
2117 /* change dd_clone entries */
2118 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
2119 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
2120 odd
->dd_phys
->dd_clones
, hds
->ds_object
, tx
));
2121 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2122 ddpa
->origin_origin
->ds_dir
->dd_phys
->dd_clones
,
2123 hds
->ds_object
, tx
));
2125 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
2126 ddpa
->origin_origin
->ds_dir
->dd_phys
->dd_clones
,
2127 origin_head
->ds_object
, tx
));
2128 if (dd
->dd_phys
->dd_clones
== 0) {
2129 dd
->dd_phys
->dd_clones
= zap_create(dp
->dp_meta_objset
,
2130 DMU_OT_DSL_CLONES
, DMU_OT_NONE
, 0, tx
);
2132 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2133 dd
->dd_phys
->dd_clones
, origin_head
->ds_object
, tx
));
2136 /* move snapshots to this dir */
2137 for (snap
= list_head(&ddpa
->shared_snaps
); snap
;
2138 snap
= list_next(&ddpa
->shared_snaps
, snap
)) {
2139 dsl_dataset_t
*ds
= snap
->ds
;
2142 * Property callbacks are registered to a particular
2143 * dsl_dir. Since ours is changing, evict the objset
2144 * so that they will be unregistered from the old dsl_dir.
2146 if (ds
->ds_objset
) {
2147 dmu_objset_evict(ds
->ds_objset
);
2148 ds
->ds_objset
= NULL
;
2151 /* move snap name entry */
2152 VERIFY0(dsl_dataset_get_snapname(ds
));
2153 VERIFY0(dsl_dataset_snap_remove(origin_head
,
2154 ds
->ds_snapname
, tx
));
2155 VERIFY0(zap_add(dp
->dp_meta_objset
,
2156 hds
->ds_phys
->ds_snapnames_zapobj
, ds
->ds_snapname
,
2157 8, 1, &ds
->ds_object
, tx
));
2159 /* change containing dsl_dir */
2160 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2161 ASSERT3U(ds
->ds_phys
->ds_dir_obj
, ==, odd
->dd_object
);
2162 ds
->ds_phys
->ds_dir_obj
= dd
->dd_object
;
2163 ASSERT3P(ds
->ds_dir
, ==, odd
);
2164 dsl_dir_rele(ds
->ds_dir
, ds
);
2165 VERIFY0(dsl_dir_hold_obj(dp
, dd
->dd_object
,
2166 NULL
, ds
, &ds
->ds_dir
));
2168 /* move any clone references */
2169 if (ds
->ds_phys
->ds_next_clones_obj
&&
2170 spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
2174 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2175 ds
->ds_phys
->ds_next_clones_obj
);
2176 zap_cursor_retrieve(&zc
, &za
) == 0;
2177 zap_cursor_advance(&zc
)) {
2178 dsl_dataset_t
*cnds
;
2181 if (za
.za_first_integer
== oldnext_obj
) {
2183 * We've already moved the
2184 * origin's reference.
2189 VERIFY0(dsl_dataset_hold_obj(dp
,
2190 za
.za_first_integer
, FTAG
, &cnds
));
2191 o
= cnds
->ds_dir
->dd_phys
->dd_head_dataset_obj
;
2193 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
2194 odd
->dd_phys
->dd_clones
, o
, tx
));
2195 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2196 dd
->dd_phys
->dd_clones
, o
, tx
));
2197 dsl_dataset_rele(cnds
, FTAG
);
2199 zap_cursor_fini(&zc
);
2202 ASSERT(!dsl_prop_hascb(ds
));
2206 * Change space accounting.
2207 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2208 * both be valid, or both be 0 (resulting in delta == 0). This
2209 * is true for each of {clone,origin} independently.
2212 delta
= ddpa
->cloneusedsnap
-
2213 dd
->dd_phys
->dd_used_breakdown
[DD_USED_SNAP
];
2214 ASSERT3S(delta
, >=, 0);
2215 ASSERT3U(ddpa
->used
, >=, delta
);
2216 dsl_dir_diduse_space(dd
, DD_USED_SNAP
, delta
, 0, 0, tx
);
2217 dsl_dir_diduse_space(dd
, DD_USED_HEAD
,
2218 ddpa
->used
- delta
, ddpa
->comp
, ddpa
->uncomp
, tx
);
2220 delta
= ddpa
->originusedsnap
-
2221 odd
->dd_phys
->dd_used_breakdown
[DD_USED_SNAP
];
2222 ASSERT3S(delta
, <=, 0);
2223 ASSERT3U(ddpa
->used
, >=, -delta
);
2224 dsl_dir_diduse_space(odd
, DD_USED_SNAP
, delta
, 0, 0, tx
);
2225 dsl_dir_diduse_space(odd
, DD_USED_HEAD
,
2226 -ddpa
->used
- delta
, -ddpa
->comp
, -ddpa
->uncomp
, tx
);
2228 origin_ds
->ds_phys
->ds_unique_bytes
= ddpa
->unique
;
2230 /* log history record */
2231 spa_history_log_internal_ds(hds
, "promote", tx
, "");
2233 dsl_dir_rele(odd
, FTAG
);
2234 promote_rele(ddpa
, FTAG
);
2238 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2239 * (exclusive) and last_obj (inclusive). The list will be in reverse
2240 * order (last_obj will be the list_head()). If first_obj == 0, do all
2241 * snapshots back to this dataset's origin.
2244 snaplist_make(dsl_pool_t
*dp
,
2245 uint64_t first_obj
, uint64_t last_obj
, list_t
*l
, void *tag
)
2247 uint64_t obj
= last_obj
;
2249 list_create(l
, sizeof (struct promotenode
),
2250 offsetof(struct promotenode
, link
));
2252 while (obj
!= first_obj
) {
2254 struct promotenode
*snap
;
2257 err
= dsl_dataset_hold_obj(dp
, obj
, tag
, &ds
);
2258 ASSERT(err
!= ENOENT
);
2263 first_obj
= ds
->ds_dir
->dd_phys
->dd_origin_obj
;
2265 snap
= kmem_alloc(sizeof (*snap
), KM_SLEEP
);
2267 list_insert_tail(l
, snap
);
2268 obj
= ds
->ds_phys
->ds_prev_snap_obj
;
2275 snaplist_space(list_t
*l
, uint64_t mintxg
, uint64_t *spacep
)
2277 struct promotenode
*snap
;
2280 for (snap
= list_head(l
); snap
; snap
= list_next(l
, snap
)) {
2281 uint64_t used
, comp
, uncomp
;
2282 dsl_deadlist_space_range(&snap
->ds
->ds_deadlist
,
2283 mintxg
, UINT64_MAX
, &used
, &comp
, &uncomp
);
2290 snaplist_destroy(list_t
*l
, void *tag
)
2292 struct promotenode
*snap
;
2294 if (l
== NULL
|| !list_link_active(&l
->list_head
))
2297 while ((snap
= list_tail(l
)) != NULL
) {
2298 list_remove(l
, snap
);
2299 dsl_dataset_rele(snap
->ds
, tag
);
2300 kmem_free(snap
, sizeof (*snap
));
2306 promote_hold(dsl_dataset_promote_arg_t
*ddpa
, dsl_pool_t
*dp
, void *tag
)
2310 struct promotenode
*snap
;
2312 error
= dsl_dataset_hold(dp
, ddpa
->ddpa_clonename
, tag
,
2316 dd
= ddpa
->ddpa_clone
->ds_dir
;
2318 if (dsl_dataset_is_snapshot(ddpa
->ddpa_clone
) ||
2319 !dsl_dir_is_clone(dd
)) {
2320 dsl_dataset_rele(ddpa
->ddpa_clone
, tag
);
2321 return (SET_ERROR(EINVAL
));
2324 error
= snaplist_make(dp
, 0, dd
->dd_phys
->dd_origin_obj
,
2325 &ddpa
->shared_snaps
, tag
);
2329 error
= snaplist_make(dp
, 0, ddpa
->ddpa_clone
->ds_object
,
2330 &ddpa
->clone_snaps
, tag
);
2334 snap
= list_head(&ddpa
->shared_snaps
);
2335 ASSERT3U(snap
->ds
->ds_object
, ==, dd
->dd_phys
->dd_origin_obj
);
2336 error
= snaplist_make(dp
, dd
->dd_phys
->dd_origin_obj
,
2337 snap
->ds
->ds_dir
->dd_phys
->dd_head_dataset_obj
,
2338 &ddpa
->origin_snaps
, tag
);
2342 if (snap
->ds
->ds_dir
->dd_phys
->dd_origin_obj
!= 0) {
2343 error
= dsl_dataset_hold_obj(dp
,
2344 snap
->ds
->ds_dir
->dd_phys
->dd_origin_obj
,
2345 tag
, &ddpa
->origin_origin
);
2351 promote_rele(ddpa
, tag
);
2356 promote_rele(dsl_dataset_promote_arg_t
*ddpa
, void *tag
)
2358 snaplist_destroy(&ddpa
->shared_snaps
, tag
);
2359 snaplist_destroy(&ddpa
->clone_snaps
, tag
);
2360 snaplist_destroy(&ddpa
->origin_snaps
, tag
);
2361 if (ddpa
->origin_origin
!= NULL
)
2362 dsl_dataset_rele(ddpa
->origin_origin
, tag
);
2363 dsl_dataset_rele(ddpa
->ddpa_clone
, tag
);
2369 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2370 * in with the name. (It must be at least MAXNAMELEN bytes long.)
2373 dsl_dataset_promote(const char *name
, char *conflsnap
)
2375 dsl_dataset_promote_arg_t ddpa
= { 0 };
2381 * We will modify space proportional to the number of
2382 * snapshots. Compute numsnaps.
2384 error
= dmu_objset_hold(name
, FTAG
, &os
);
2387 error
= zap_count(dmu_objset_pool(os
)->dp_meta_objset
,
2388 dmu_objset_ds(os
)->ds_phys
->ds_snapnames_zapobj
, &numsnaps
);
2389 dmu_objset_rele(os
, FTAG
);
2393 ddpa
.ddpa_clonename
= name
;
2394 ddpa
.err_ds
= conflsnap
;
2396 return (dsl_sync_task(name
, dsl_dataset_promote_check
,
2397 dsl_dataset_promote_sync
, &ddpa
, 2 + numsnaps
));
2401 dsl_dataset_clone_swap_check_impl(dsl_dataset_t
*clone
,
2402 dsl_dataset_t
*origin_head
, boolean_t force
, void *owner
, dmu_tx_t
*tx
)
2404 int64_t unused_refres_delta
;
2406 /* they should both be heads */
2407 if (dsl_dataset_is_snapshot(clone
) ||
2408 dsl_dataset_is_snapshot(origin_head
))
2409 return (SET_ERROR(EINVAL
));
2411 /* if we are not forcing, the branch point should be just before them */
2412 if (!force
&& clone
->ds_prev
!= origin_head
->ds_prev
)
2413 return (SET_ERROR(EINVAL
));
2415 /* clone should be the clone (unless they are unrelated) */
2416 if (clone
->ds_prev
!= NULL
&&
2417 clone
->ds_prev
!= clone
->ds_dir
->dd_pool
->dp_origin_snap
&&
2418 origin_head
->ds_dir
!= clone
->ds_prev
->ds_dir
)
2419 return (SET_ERROR(EINVAL
));
2421 /* the clone should be a child of the origin */
2422 if (clone
->ds_dir
->dd_parent
!= origin_head
->ds_dir
)
2423 return (SET_ERROR(EINVAL
));
2425 /* origin_head shouldn't be modified unless 'force' */
2427 dsl_dataset_modified_since_snap(origin_head
, origin_head
->ds_prev
))
2428 return (SET_ERROR(ETXTBSY
));
2430 /* origin_head should have no long holds (e.g. is not mounted) */
2431 if (dsl_dataset_handoff_check(origin_head
, owner
, tx
))
2432 return (SET_ERROR(EBUSY
));
2434 /* check amount of any unconsumed refreservation */
2435 unused_refres_delta
=
2436 (int64_t)MIN(origin_head
->ds_reserved
,
2437 origin_head
->ds_phys
->ds_unique_bytes
) -
2438 (int64_t)MIN(origin_head
->ds_reserved
,
2439 clone
->ds_phys
->ds_unique_bytes
);
2441 if (unused_refres_delta
> 0 &&
2442 unused_refres_delta
>
2443 dsl_dir_space_available(origin_head
->ds_dir
, NULL
, 0, TRUE
))
2444 return (SET_ERROR(ENOSPC
));
2446 /* clone can't be over the head's refquota */
2447 if (origin_head
->ds_quota
!= 0 &&
2448 clone
->ds_phys
->ds_referenced_bytes
> origin_head
->ds_quota
)
2449 return (SET_ERROR(EDQUOT
));
2455 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t
*clone
,
2456 dsl_dataset_t
*origin_head
, dmu_tx_t
*tx
)
2458 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2459 int64_t unused_refres_delta
;
2461 ASSERT(clone
->ds_reserved
== 0);
2462 ASSERT(origin_head
->ds_quota
== 0 ||
2463 clone
->ds_phys
->ds_unique_bytes
<= origin_head
->ds_quota
);
2464 ASSERT3P(clone
->ds_prev
, ==, origin_head
->ds_prev
);
2466 dmu_buf_will_dirty(clone
->ds_dbuf
, tx
);
2467 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
2469 if (clone
->ds_objset
!= NULL
) {
2470 dmu_objset_evict(clone
->ds_objset
);
2471 clone
->ds_objset
= NULL
;
2474 if (origin_head
->ds_objset
!= NULL
) {
2475 dmu_objset_evict(origin_head
->ds_objset
);
2476 origin_head
->ds_objset
= NULL
;
2479 unused_refres_delta
=
2480 (int64_t)MIN(origin_head
->ds_reserved
,
2481 origin_head
->ds_phys
->ds_unique_bytes
) -
2482 (int64_t)MIN(origin_head
->ds_reserved
,
2483 clone
->ds_phys
->ds_unique_bytes
);
2486 * Reset origin's unique bytes, if it exists.
2488 if (clone
->ds_prev
) {
2489 dsl_dataset_t
*origin
= clone
->ds_prev
;
2490 uint64_t comp
, uncomp
;
2492 dmu_buf_will_dirty(origin
->ds_dbuf
, tx
);
2493 dsl_deadlist_space_range(&clone
->ds_deadlist
,
2494 origin
->ds_phys
->ds_prev_snap_txg
, UINT64_MAX
,
2495 &origin
->ds_phys
->ds_unique_bytes
, &comp
, &uncomp
);
2501 tmp
= origin_head
->ds_phys
->ds_bp
;
2502 origin_head
->ds_phys
->ds_bp
= clone
->ds_phys
->ds_bp
;
2503 clone
->ds_phys
->ds_bp
= tmp
;
2506 /* set dd_*_bytes */
2508 int64_t dused
, dcomp
, duncomp
;
2509 uint64_t cdl_used
, cdl_comp
, cdl_uncomp
;
2510 uint64_t odl_used
, odl_comp
, odl_uncomp
;
2512 ASSERT3U(clone
->ds_dir
->dd_phys
->
2513 dd_used_breakdown
[DD_USED_SNAP
], ==, 0);
2515 dsl_deadlist_space(&clone
->ds_deadlist
,
2516 &cdl_used
, &cdl_comp
, &cdl_uncomp
);
2517 dsl_deadlist_space(&origin_head
->ds_deadlist
,
2518 &odl_used
, &odl_comp
, &odl_uncomp
);
2520 dused
= clone
->ds_phys
->ds_referenced_bytes
+ cdl_used
-
2521 (origin_head
->ds_phys
->ds_referenced_bytes
+ odl_used
);
2522 dcomp
= clone
->ds_phys
->ds_compressed_bytes
+ cdl_comp
-
2523 (origin_head
->ds_phys
->ds_compressed_bytes
+ odl_comp
);
2524 duncomp
= clone
->ds_phys
->ds_uncompressed_bytes
+
2526 (origin_head
->ds_phys
->ds_uncompressed_bytes
+ odl_uncomp
);
2528 dsl_dir_diduse_space(origin_head
->ds_dir
, DD_USED_HEAD
,
2529 dused
, dcomp
, duncomp
, tx
);
2530 dsl_dir_diduse_space(clone
->ds_dir
, DD_USED_HEAD
,
2531 -dused
, -dcomp
, -duncomp
, tx
);
2534 * The difference in the space used by snapshots is the
2535 * difference in snapshot space due to the head's
2536 * deadlist (since that's the only thing that's
2537 * changing that affects the snapused).
2539 dsl_deadlist_space_range(&clone
->ds_deadlist
,
2540 origin_head
->ds_dir
->dd_origin_txg
, UINT64_MAX
,
2541 &cdl_used
, &cdl_comp
, &cdl_uncomp
);
2542 dsl_deadlist_space_range(&origin_head
->ds_deadlist
,
2543 origin_head
->ds_dir
->dd_origin_txg
, UINT64_MAX
,
2544 &odl_used
, &odl_comp
, &odl_uncomp
);
2545 dsl_dir_transfer_space(origin_head
->ds_dir
, cdl_used
- odl_used
,
2546 DD_USED_HEAD
, DD_USED_SNAP
, tx
);
2549 /* swap ds_*_bytes */
2550 SWITCH64(origin_head
->ds_phys
->ds_referenced_bytes
,
2551 clone
->ds_phys
->ds_referenced_bytes
);
2552 SWITCH64(origin_head
->ds_phys
->ds_compressed_bytes
,
2553 clone
->ds_phys
->ds_compressed_bytes
);
2554 SWITCH64(origin_head
->ds_phys
->ds_uncompressed_bytes
,
2555 clone
->ds_phys
->ds_uncompressed_bytes
);
2556 SWITCH64(origin_head
->ds_phys
->ds_unique_bytes
,
2557 clone
->ds_phys
->ds_unique_bytes
);
2559 /* apply any parent delta for change in unconsumed refreservation */
2560 dsl_dir_diduse_space(origin_head
->ds_dir
, DD_USED_REFRSRV
,
2561 unused_refres_delta
, 0, 0, tx
);
2566 dsl_deadlist_close(&clone
->ds_deadlist
);
2567 dsl_deadlist_close(&origin_head
->ds_deadlist
);
2568 SWITCH64(origin_head
->ds_phys
->ds_deadlist_obj
,
2569 clone
->ds_phys
->ds_deadlist_obj
);
2570 dsl_deadlist_open(&clone
->ds_deadlist
, dp
->dp_meta_objset
,
2571 clone
->ds_phys
->ds_deadlist_obj
);
2572 dsl_deadlist_open(&origin_head
->ds_deadlist
, dp
->dp_meta_objset
,
2573 origin_head
->ds_phys
->ds_deadlist_obj
);
2575 dsl_scan_ds_clone_swapped(origin_head
, clone
, tx
);
2577 spa_history_log_internal_ds(clone
, "clone swap", tx
,
2578 "parent=%s", origin_head
->ds_dir
->dd_myname
);
2582 * Given a pool name and a dataset object number in that pool,
2583 * return the name of that dataset.
2586 dsl_dsobj_to_dsname(char *pname
, uint64_t obj
, char *buf
)
2592 error
= dsl_pool_hold(pname
, FTAG
, &dp
);
2596 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
, &ds
);
2598 dsl_dataset_name(ds
, buf
);
2599 dsl_dataset_rele(ds
, FTAG
);
2601 dsl_pool_rele(dp
, FTAG
);
2607 dsl_dataset_check_quota(dsl_dataset_t
*ds
, boolean_t check_quota
,
2608 uint64_t asize
, uint64_t inflight
, uint64_t *used
, uint64_t *ref_rsrv
)
2612 ASSERT3S(asize
, >, 0);
2615 * *ref_rsrv is the portion of asize that will come from any
2616 * unconsumed refreservation space.
2620 mutex_enter(&ds
->ds_lock
);
2622 * Make a space adjustment for reserved bytes.
2624 if (ds
->ds_reserved
> ds
->ds_phys
->ds_unique_bytes
) {
2626 ds
->ds_reserved
- ds
->ds_phys
->ds_unique_bytes
);
2627 *used
-= (ds
->ds_reserved
- ds
->ds_phys
->ds_unique_bytes
);
2629 asize
- MIN(asize
, parent_delta(ds
, asize
+ inflight
));
2632 if (!check_quota
|| ds
->ds_quota
== 0) {
2633 mutex_exit(&ds
->ds_lock
);
2637 * If they are requesting more space, and our current estimate
2638 * is over quota, they get to try again unless the actual
2639 * on-disk is over quota and there are no pending changes (which
2640 * may free up space for us).
2642 if (ds
->ds_phys
->ds_referenced_bytes
+ inflight
>= ds
->ds_quota
) {
2644 ds
->ds_phys
->ds_referenced_bytes
< ds
->ds_quota
)
2645 error
= SET_ERROR(ERESTART
);
2647 error
= SET_ERROR(EDQUOT
);
2649 mutex_exit(&ds
->ds_lock
);
2654 typedef struct dsl_dataset_set_qr_arg
{
2655 const char *ddsqra_name
;
2656 zprop_source_t ddsqra_source
;
2657 uint64_t ddsqra_value
;
2658 } dsl_dataset_set_qr_arg_t
;
2663 dsl_dataset_set_refquota_check(void *arg
, dmu_tx_t
*tx
)
2665 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2666 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2671 if (spa_version(dp
->dp_spa
) < SPA_VERSION_REFQUOTA
)
2672 return (SET_ERROR(ENOTSUP
));
2674 error
= dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
);
2678 if (dsl_dataset_is_snapshot(ds
)) {
2679 dsl_dataset_rele(ds
, FTAG
);
2680 return (SET_ERROR(EINVAL
));
2683 error
= dsl_prop_predict(ds
->ds_dir
,
2684 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
2685 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, &newval
);
2687 dsl_dataset_rele(ds
, FTAG
);
2692 dsl_dataset_rele(ds
, FTAG
);
2696 if (newval
< ds
->ds_phys
->ds_referenced_bytes
||
2697 newval
< ds
->ds_reserved
) {
2698 dsl_dataset_rele(ds
, FTAG
);
2699 return (SET_ERROR(ENOSPC
));
2702 dsl_dataset_rele(ds
, FTAG
);
2707 dsl_dataset_set_refquota_sync(void *arg
, dmu_tx_t
*tx
)
2709 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2710 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2714 VERIFY0(dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
));
2716 dsl_prop_set_sync_impl(ds
,
2717 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
2718 ddsqra
->ddsqra_source
, sizeof (ddsqra
->ddsqra_value
), 1,
2719 &ddsqra
->ddsqra_value
, tx
);
2721 VERIFY0(dsl_prop_get_int_ds(ds
,
2722 zfs_prop_to_name(ZFS_PROP_REFQUOTA
), &newval
));
2724 if (ds
->ds_quota
!= newval
) {
2725 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2726 ds
->ds_quota
= newval
;
2728 dsl_dataset_rele(ds
, FTAG
);
2732 dsl_dataset_set_refquota(const char *dsname
, zprop_source_t source
,
2735 dsl_dataset_set_qr_arg_t ddsqra
;
2737 ddsqra
.ddsqra_name
= dsname
;
2738 ddsqra
.ddsqra_source
= source
;
2739 ddsqra
.ddsqra_value
= refquota
;
2741 return (dsl_sync_task(dsname
, dsl_dataset_set_refquota_check
,
2742 dsl_dataset_set_refquota_sync
, &ddsqra
, 0));
2746 dsl_dataset_set_refreservation_check(void *arg
, dmu_tx_t
*tx
)
2748 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2749 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2752 uint64_t newval
, unique
;
2754 if (spa_version(dp
->dp_spa
) < SPA_VERSION_REFRESERVATION
)
2755 return (SET_ERROR(ENOTSUP
));
2757 error
= dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
);
2761 if (dsl_dataset_is_snapshot(ds
)) {
2762 dsl_dataset_rele(ds
, FTAG
);
2763 return (SET_ERROR(EINVAL
));
2766 error
= dsl_prop_predict(ds
->ds_dir
,
2767 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
2768 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, &newval
);
2770 dsl_dataset_rele(ds
, FTAG
);
2775 * If we are doing the preliminary check in open context, the
2776 * space estimates may be inaccurate.
2778 if (!dmu_tx_is_syncing(tx
)) {
2779 dsl_dataset_rele(ds
, FTAG
);
2783 mutex_enter(&ds
->ds_lock
);
2784 if (!DS_UNIQUE_IS_ACCURATE(ds
))
2785 dsl_dataset_recalc_head_uniq(ds
);
2786 unique
= ds
->ds_phys
->ds_unique_bytes
;
2787 mutex_exit(&ds
->ds_lock
);
2789 if (MAX(unique
, newval
) > MAX(unique
, ds
->ds_reserved
)) {
2790 uint64_t delta
= MAX(unique
, newval
) -
2791 MAX(unique
, ds
->ds_reserved
);
2794 dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, B_TRUE
) ||
2795 (ds
->ds_quota
> 0 && newval
> ds
->ds_quota
)) {
2796 dsl_dataset_rele(ds
, FTAG
);
2797 return (SET_ERROR(ENOSPC
));
2801 dsl_dataset_rele(ds
, FTAG
);
2806 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t
*ds
,
2807 zprop_source_t source
, uint64_t value
, dmu_tx_t
*tx
)
2813 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
2814 source
, sizeof (value
), 1, &value
, tx
);
2816 VERIFY0(dsl_prop_get_int_ds(ds
,
2817 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), &newval
));
2819 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2820 mutex_enter(&ds
->ds_dir
->dd_lock
);
2821 mutex_enter(&ds
->ds_lock
);
2822 ASSERT(DS_UNIQUE_IS_ACCURATE(ds
));
2823 unique
= ds
->ds_phys
->ds_unique_bytes
;
2824 delta
= MAX(0, (int64_t)(newval
- unique
)) -
2825 MAX(0, (int64_t)(ds
->ds_reserved
- unique
));
2826 ds
->ds_reserved
= newval
;
2827 mutex_exit(&ds
->ds_lock
);
2829 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_REFRSRV
, delta
, 0, 0, tx
);
2830 mutex_exit(&ds
->ds_dir
->dd_lock
);
2834 dsl_dataset_set_refreservation_sync(void *arg
, dmu_tx_t
*tx
)
2836 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2837 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2840 VERIFY0(dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
));
2841 dsl_dataset_set_refreservation_sync_impl(ds
,
2842 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, tx
);
2843 dsl_dataset_rele(ds
, FTAG
);
2847 dsl_dataset_set_refreservation(const char *dsname
, zprop_source_t source
,
2848 uint64_t refreservation
)
2850 dsl_dataset_set_qr_arg_t ddsqra
;
2852 ddsqra
.ddsqra_name
= dsname
;
2853 ddsqra
.ddsqra_source
= source
;
2854 ddsqra
.ddsqra_value
= refreservation
;
2856 return (dsl_sync_task(dsname
, dsl_dataset_set_refreservation_check
,
2857 dsl_dataset_set_refreservation_sync
, &ddsqra
, 0));
2861 * Return (in *usedp) the amount of space written in new that is not
2862 * present in oldsnap. New may be a snapshot or the head. Old must be
2863 * a snapshot before new, in new's filesystem (or its origin). If not then
2864 * fail and return EINVAL.
2866 * The written space is calculated by considering two components: First, we
2867 * ignore any freed space, and calculate the written as new's used space
2868 * minus old's used space. Next, we add in the amount of space that was freed
2869 * between the two snapshots, thus reducing new's used space relative to old's.
2870 * Specifically, this is the space that was born before old->ds_creation_txg,
2871 * and freed before new (ie. on new's deadlist or a previous deadlist).
2873 * space freed [---------------------]
2874 * snapshots ---O-------O--------O-------O------
2878 dsl_dataset_space_written(dsl_dataset_t
*oldsnap
, dsl_dataset_t
*new,
2879 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
2883 dsl_pool_t
*dp
= new->ds_dir
->dd_pool
;
2885 ASSERT(dsl_pool_config_held(dp
));
2888 *usedp
+= new->ds_phys
->ds_referenced_bytes
;
2889 *usedp
-= oldsnap
->ds_phys
->ds_referenced_bytes
;
2892 *compp
+= new->ds_phys
->ds_compressed_bytes
;
2893 *compp
-= oldsnap
->ds_phys
->ds_compressed_bytes
;
2896 *uncompp
+= new->ds_phys
->ds_uncompressed_bytes
;
2897 *uncompp
-= oldsnap
->ds_phys
->ds_uncompressed_bytes
;
2899 snapobj
= new->ds_object
;
2900 while (snapobj
!= oldsnap
->ds_object
) {
2901 dsl_dataset_t
*snap
;
2902 uint64_t used
, comp
, uncomp
;
2904 if (snapobj
== new->ds_object
) {
2907 err
= dsl_dataset_hold_obj(dp
, snapobj
, FTAG
, &snap
);
2912 if (snap
->ds_phys
->ds_prev_snap_txg
==
2913 oldsnap
->ds_phys
->ds_creation_txg
) {
2915 * The blocks in the deadlist can not be born after
2916 * ds_prev_snap_txg, so get the whole deadlist space,
2917 * which is more efficient (especially for old-format
2918 * deadlists). Unfortunately the deadlist code
2919 * doesn't have enough information to make this
2920 * optimization itself.
2922 dsl_deadlist_space(&snap
->ds_deadlist
,
2923 &used
, &comp
, &uncomp
);
2925 dsl_deadlist_space_range(&snap
->ds_deadlist
,
2926 0, oldsnap
->ds_phys
->ds_creation_txg
,
2927 &used
, &comp
, &uncomp
);
2934 * If we get to the beginning of the chain of snapshots
2935 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
2936 * was not a snapshot of/before new.
2938 snapobj
= snap
->ds_phys
->ds_prev_snap_obj
;
2940 dsl_dataset_rele(snap
, FTAG
);
2942 err
= SET_ERROR(EINVAL
);
2951 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
2952 * lastsnap, and all snapshots in between are deleted.
2954 * blocks that would be freed [---------------------------]
2955 * snapshots ---O-------O--------O-------O--------O
2956 * firstsnap lastsnap
2958 * This is the set of blocks that were born after the snap before firstsnap,
2959 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
2960 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
2961 * We calculate this by iterating over the relevant deadlists (from the snap
2962 * after lastsnap, backward to the snap after firstsnap), summing up the
2963 * space on the deadlist that was born after the snap before firstsnap.
2966 dsl_dataset_space_wouldfree(dsl_dataset_t
*firstsnap
,
2967 dsl_dataset_t
*lastsnap
,
2968 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
2972 dsl_pool_t
*dp
= firstsnap
->ds_dir
->dd_pool
;
2974 ASSERT(dsl_dataset_is_snapshot(firstsnap
));
2975 ASSERT(dsl_dataset_is_snapshot(lastsnap
));
2978 * Check that the snapshots are in the same dsl_dir, and firstsnap
2979 * is before lastsnap.
2981 if (firstsnap
->ds_dir
!= lastsnap
->ds_dir
||
2982 firstsnap
->ds_phys
->ds_creation_txg
>
2983 lastsnap
->ds_phys
->ds_creation_txg
)
2984 return (SET_ERROR(EINVAL
));
2986 *usedp
= *compp
= *uncompp
= 0;
2988 snapobj
= lastsnap
->ds_phys
->ds_next_snap_obj
;
2989 while (snapobj
!= firstsnap
->ds_object
) {
2991 uint64_t used
, comp
, uncomp
;
2993 err
= dsl_dataset_hold_obj(dp
, snapobj
, FTAG
, &ds
);
2997 dsl_deadlist_space_range(&ds
->ds_deadlist
,
2998 firstsnap
->ds_phys
->ds_prev_snap_txg
, UINT64_MAX
,
2999 &used
, &comp
, &uncomp
);
3004 snapobj
= ds
->ds_phys
->ds_prev_snap_obj
;
3005 ASSERT3U(snapobj
, !=, 0);
3006 dsl_dataset_rele(ds
, FTAG
);
3012 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
3013 * For example, they could both be snapshots of the same filesystem, and
3014 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
3015 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
3016 * filesystem. Or 'earlier' could be the origin's origin.
3018 * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
3021 dsl_dataset_is_before(dsl_dataset_t
*later
, dsl_dataset_t
*earlier
,
3022 uint64_t earlier_txg
)
3024 dsl_pool_t
*dp
= later
->ds_dir
->dd_pool
;
3027 dsl_dataset_t
*origin
;
3029 ASSERT(dsl_pool_config_held(dp
));
3030 ASSERT(dsl_dataset_is_snapshot(earlier
) || earlier_txg
!= 0);
3032 if (earlier_txg
== 0)
3033 earlier_txg
= earlier
->ds_phys
->ds_creation_txg
;
3035 if (dsl_dataset_is_snapshot(later
) &&
3036 earlier_txg
>= later
->ds_phys
->ds_creation_txg
)
3039 if (later
->ds_dir
== earlier
->ds_dir
)
3041 if (!dsl_dir_is_clone(later
->ds_dir
))
3044 if (later
->ds_dir
->dd_phys
->dd_origin_obj
== earlier
->ds_object
)
3046 error
= dsl_dataset_hold_obj(dp
,
3047 later
->ds_dir
->dd_phys
->dd_origin_obj
, FTAG
, &origin
);
3050 ret
= dsl_dataset_is_before(origin
, earlier
, earlier_txg
);
3051 dsl_dataset_rele(origin
, FTAG
);
3057 dsl_dataset_zapify(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
3059 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
3060 dmu_object_zapify(mos
, ds
->ds_object
, DMU_OT_DSL_DATASET
, tx
);
3063 #if defined(_KERNEL) && defined(HAVE_SPL)
3064 EXPORT_SYMBOL(dsl_dataset_hold
);
3065 EXPORT_SYMBOL(dsl_dataset_hold_obj
);
3066 EXPORT_SYMBOL(dsl_dataset_own
);
3067 EXPORT_SYMBOL(dsl_dataset_own_obj
);
3068 EXPORT_SYMBOL(dsl_dataset_name
);
3069 EXPORT_SYMBOL(dsl_dataset_rele
);
3070 EXPORT_SYMBOL(dsl_dataset_disown
);
3071 EXPORT_SYMBOL(dsl_dataset_tryown
);
3072 EXPORT_SYMBOL(dsl_dataset_create_sync
);
3073 EXPORT_SYMBOL(dsl_dataset_create_sync_dd
);
3074 EXPORT_SYMBOL(dsl_dataset_snapshot_check
);
3075 EXPORT_SYMBOL(dsl_dataset_snapshot_sync
);
3076 EXPORT_SYMBOL(dsl_dataset_promote
);
3077 EXPORT_SYMBOL(dsl_dataset_user_hold
);
3078 EXPORT_SYMBOL(dsl_dataset_user_release
);
3079 EXPORT_SYMBOL(dsl_dataset_get_holds
);
3080 EXPORT_SYMBOL(dsl_dataset_get_blkptr
);
3081 EXPORT_SYMBOL(dsl_dataset_set_blkptr
);
3082 EXPORT_SYMBOL(dsl_dataset_get_spa
);
3083 EXPORT_SYMBOL(dsl_dataset_modified_since_snap
);
3084 EXPORT_SYMBOL(dsl_dataset_space_written
);
3085 EXPORT_SYMBOL(dsl_dataset_space_wouldfree
);
3086 EXPORT_SYMBOL(dsl_dataset_sync
);
3087 EXPORT_SYMBOL(dsl_dataset_block_born
);
3088 EXPORT_SYMBOL(dsl_dataset_block_kill
);
3089 EXPORT_SYMBOL(dsl_dataset_block_freeable
);
3090 EXPORT_SYMBOL(dsl_dataset_prev_snap_txg
);
3091 EXPORT_SYMBOL(dsl_dataset_dirty
);
3092 EXPORT_SYMBOL(dsl_dataset_stats
);
3093 EXPORT_SYMBOL(dsl_dataset_fast_stat
);
3094 EXPORT_SYMBOL(dsl_dataset_space
);
3095 EXPORT_SYMBOL(dsl_dataset_fsid_guid
);
3096 EXPORT_SYMBOL(dsl_dsobj_to_dsname
);
3097 EXPORT_SYMBOL(dsl_dataset_check_quota
);
3098 EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl
);
3099 EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl
);