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>
52 #define SWITCH64(x, y) \
54 uint64_t __tmp = (x); \
59 #define DS_REF_MAX (1ULL << 62)
61 #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE
64 * Figure out how much of this delta should be propogated to the dsl_dir
65 * layer. If there's a refreservation, that space has already been
66 * partially accounted for in our ancestors.
69 parent_delta(dsl_dataset_t
*ds
, int64_t delta
)
71 uint64_t old_bytes
, new_bytes
;
73 if (ds
->ds_reserved
== 0)
76 old_bytes
= MAX(ds
->ds_phys
->ds_unique_bytes
, ds
->ds_reserved
);
77 new_bytes
= MAX(ds
->ds_phys
->ds_unique_bytes
+ delta
, ds
->ds_reserved
);
79 ASSERT3U(ABS((int64_t)(new_bytes
- old_bytes
)), <=, ABS(delta
));
80 return (new_bytes
- old_bytes
);
84 dsl_dataset_block_born(dsl_dataset_t
*ds
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
86 int used
, compressed
, uncompressed
;
89 used
= bp_get_dsize_sync(tx
->tx_pool
->dp_spa
, bp
);
90 compressed
= BP_GET_PSIZE(bp
);
91 uncompressed
= BP_GET_UCSIZE(bp
);
93 dprintf_bp(bp
, "ds=%p", ds
);
95 ASSERT(dmu_tx_is_syncing(tx
));
96 /* It could have been compressed away to nothing */
99 ASSERT(BP_GET_TYPE(bp
) != DMU_OT_NONE
);
100 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp
)));
102 dsl_pool_mos_diduse_space(tx
->tx_pool
,
103 used
, compressed
, uncompressed
);
107 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
108 mutex_enter(&ds
->ds_lock
);
109 delta
= parent_delta(ds
, used
);
110 ds
->ds_phys
->ds_referenced_bytes
+= used
;
111 ds
->ds_phys
->ds_compressed_bytes
+= compressed
;
112 ds
->ds_phys
->ds_uncompressed_bytes
+= uncompressed
;
113 ds
->ds_phys
->ds_unique_bytes
+= used
;
114 mutex_exit(&ds
->ds_lock
);
115 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_HEAD
, delta
,
116 compressed
, uncompressed
, tx
);
117 dsl_dir_transfer_space(ds
->ds_dir
, used
- delta
,
118 DD_USED_REFRSRV
, DD_USED_HEAD
, tx
);
122 dsl_dataset_block_kill(dsl_dataset_t
*ds
, const blkptr_t
*bp
, dmu_tx_t
*tx
,
125 int used
, compressed
, uncompressed
;
130 ASSERT(dmu_tx_is_syncing(tx
));
131 ASSERT(bp
->blk_birth
<= tx
->tx_txg
);
133 used
= bp_get_dsize_sync(tx
->tx_pool
->dp_spa
, bp
);
134 compressed
= BP_GET_PSIZE(bp
);
135 uncompressed
= BP_GET_UCSIZE(bp
);
139 dsl_free(tx
->tx_pool
, tx
->tx_txg
, bp
);
140 dsl_pool_mos_diduse_space(tx
->tx_pool
,
141 -used
, -compressed
, -uncompressed
);
144 ASSERT3P(tx
->tx_pool
, ==, ds
->ds_dir
->dd_pool
);
146 ASSERT(!dsl_dataset_is_snapshot(ds
));
147 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
149 if (bp
->blk_birth
> ds
->ds_phys
->ds_prev_snap_txg
) {
152 dprintf_bp(bp
, "freeing ds=%llu", ds
->ds_object
);
153 dsl_free(tx
->tx_pool
, tx
->tx_txg
, bp
);
155 mutex_enter(&ds
->ds_lock
);
156 ASSERT(ds
->ds_phys
->ds_unique_bytes
>= used
||
157 !DS_UNIQUE_IS_ACCURATE(ds
));
158 delta
= parent_delta(ds
, -used
);
159 ds
->ds_phys
->ds_unique_bytes
-= used
;
160 mutex_exit(&ds
->ds_lock
);
161 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_HEAD
,
162 delta
, -compressed
, -uncompressed
, tx
);
163 dsl_dir_transfer_space(ds
->ds_dir
, -used
- delta
,
164 DD_USED_REFRSRV
, DD_USED_HEAD
, tx
);
166 dprintf_bp(bp
, "putting on dead list: %s", "");
169 * We are here as part of zio's write done callback,
170 * which means we're a zio interrupt thread. We can't
171 * call dsl_deadlist_insert() now because it may block
172 * waiting for I/O. Instead, put bp on the deferred
173 * queue and let dsl_pool_sync() finish the job.
175 bplist_append(&ds
->ds_pending_deadlist
, bp
);
177 dsl_deadlist_insert(&ds
->ds_deadlist
, bp
, tx
);
179 ASSERT3U(ds
->ds_prev
->ds_object
, ==,
180 ds
->ds_phys
->ds_prev_snap_obj
);
181 ASSERT(ds
->ds_prev
->ds_phys
->ds_num_children
> 0);
182 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
183 if (ds
->ds_prev
->ds_phys
->ds_next_snap_obj
==
184 ds
->ds_object
&& bp
->blk_birth
>
185 ds
->ds_prev
->ds_phys
->ds_prev_snap_txg
) {
186 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
187 mutex_enter(&ds
->ds_prev
->ds_lock
);
188 ds
->ds_prev
->ds_phys
->ds_unique_bytes
+= used
;
189 mutex_exit(&ds
->ds_prev
->ds_lock
);
191 if (bp
->blk_birth
> ds
->ds_dir
->dd_origin_txg
) {
192 dsl_dir_transfer_space(ds
->ds_dir
, used
,
193 DD_USED_HEAD
, DD_USED_SNAP
, tx
);
196 mutex_enter(&ds
->ds_lock
);
197 ASSERT3U(ds
->ds_phys
->ds_referenced_bytes
, >=, used
);
198 ds
->ds_phys
->ds_referenced_bytes
-= used
;
199 ASSERT3U(ds
->ds_phys
->ds_compressed_bytes
, >=, compressed
);
200 ds
->ds_phys
->ds_compressed_bytes
-= compressed
;
201 ASSERT3U(ds
->ds_phys
->ds_uncompressed_bytes
, >=, uncompressed
);
202 ds
->ds_phys
->ds_uncompressed_bytes
-= uncompressed
;
203 mutex_exit(&ds
->ds_lock
);
209 dsl_dataset_prev_snap_txg(dsl_dataset_t
*ds
)
211 uint64_t trysnap
= 0;
216 * The snapshot creation could fail, but that would cause an
217 * incorrect FALSE return, which would only result in an
218 * overestimation of the amount of space that an operation would
219 * consume, which is OK.
221 * There's also a small window where we could miss a pending
222 * snapshot, because we could set the sync task in the quiescing
223 * phase. So this should only be used as a guess.
225 if (ds
->ds_trysnap_txg
>
226 spa_last_synced_txg(ds
->ds_dir
->dd_pool
->dp_spa
))
227 trysnap
= ds
->ds_trysnap_txg
;
228 return (MAX(ds
->ds_phys
->ds_prev_snap_txg
, trysnap
));
232 dsl_dataset_block_freeable(dsl_dataset_t
*ds
, const blkptr_t
*bp
,
235 if (blk_birth
<= dsl_dataset_prev_snap_txg(ds
))
238 ddt_prefetch(dsl_dataset_get_spa(ds
), bp
);
245 dsl_dataset_evict(dmu_buf_t
*db
, void *dsv
)
247 dsl_dataset_t
*ds
= dsv
;
249 ASSERT(ds
->ds_owner
== NULL
);
251 unique_remove(ds
->ds_fsid_guid
);
253 if (ds
->ds_objset
!= NULL
)
254 dmu_objset_evict(ds
->ds_objset
);
257 dsl_dataset_rele(ds
->ds_prev
, ds
);
261 bplist_destroy(&ds
->ds_pending_deadlist
);
262 if (ds
->ds_phys
->ds_deadlist_obj
!= 0)
263 dsl_deadlist_close(&ds
->ds_deadlist
);
265 dsl_dir_rele(ds
->ds_dir
, ds
);
267 ASSERT(!list_link_active(&ds
->ds_synced_link
));
269 mutex_destroy(&ds
->ds_lock
);
270 mutex_destroy(&ds
->ds_opening_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
)
324 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
325 uint64_t snapobj
= ds
->ds_phys
->ds_snapnames_zapobj
;
329 dsl_dir_snap_cmtime_update(ds
->ds_dir
);
331 if (ds
->ds_phys
->ds_flags
& DS_FLAG_CI_DATASET
)
336 err
= zap_remove_norm(mos
, snapobj
, name
, mt
, tx
);
337 if (err
== ENOTSUP
&& mt
== MT_FIRST
)
338 err
= zap_remove(mos
, snapobj
, name
, tx
);
343 dsl_dataset_hold_obj(dsl_pool_t
*dp
, uint64_t dsobj
, void *tag
,
346 objset_t
*mos
= dp
->dp_meta_objset
;
350 dmu_object_info_t doi
;
352 ASSERT(dsl_pool_config_held(dp
));
354 err
= dmu_bonus_hold(mos
, dsobj
, tag
, &dbuf
);
358 /* Make sure dsobj has the correct object type. */
359 dmu_object_info_from_db(dbuf
, &doi
);
360 if (doi
.doi_bonus_type
!= DMU_OT_DSL_DATASET
) {
361 dmu_buf_rele(dbuf
, tag
);
362 return (SET_ERROR(EINVAL
));
365 ds
= dmu_buf_get_user(dbuf
);
367 dsl_dataset_t
*winner
= NULL
;
369 ds
= kmem_zalloc(sizeof (dsl_dataset_t
), KM_PUSHPAGE
);
371 ds
->ds_object
= dsobj
;
372 ds
->ds_phys
= dbuf
->db_data
;
373 list_link_init(&ds
->ds_synced_link
);
375 mutex_init(&ds
->ds_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
376 mutex_init(&ds
->ds_opening_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
377 mutex_init(&ds
->ds_sendstream_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
378 refcount_create(&ds
->ds_longholds
);
380 bplist_create(&ds
->ds_pending_deadlist
);
381 dsl_deadlist_open(&ds
->ds_deadlist
,
382 mos
, ds
->ds_phys
->ds_deadlist_obj
);
384 list_create(&ds
->ds_sendstreams
, sizeof (dmu_sendarg_t
),
385 offsetof(dmu_sendarg_t
, dsa_link
));
388 err
= dsl_dir_hold_obj(dp
,
389 ds
->ds_phys
->ds_dir_obj
, NULL
, ds
, &ds
->ds_dir
);
392 mutex_destroy(&ds
->ds_lock
);
393 mutex_destroy(&ds
->ds_opening_lock
);
394 refcount_destroy(&ds
->ds_longholds
);
395 bplist_destroy(&ds
->ds_pending_deadlist
);
396 dsl_deadlist_close(&ds
->ds_deadlist
);
397 kmem_free(ds
, sizeof (dsl_dataset_t
));
398 dmu_buf_rele(dbuf
, tag
);
402 if (!dsl_dataset_is_snapshot(ds
)) {
403 ds
->ds_snapname
[0] = '\0';
404 if (ds
->ds_phys
->ds_prev_snap_obj
!= 0) {
405 err
= dsl_dataset_hold_obj(dp
,
406 ds
->ds_phys
->ds_prev_snap_obj
,
410 if (zfs_flags
& ZFS_DEBUG_SNAPNAMES
)
411 err
= dsl_dataset_get_snapname(ds
);
412 if (err
== 0 && ds
->ds_phys
->ds_userrefs_obj
!= 0) {
414 ds
->ds_dir
->dd_pool
->dp_meta_objset
,
415 ds
->ds_phys
->ds_userrefs_obj
,
420 if (err
== 0 && !dsl_dataset_is_snapshot(ds
)) {
421 err
= dsl_prop_get_int_ds(ds
,
422 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
425 err
= dsl_prop_get_int_ds(ds
,
426 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
430 ds
->ds_reserved
= ds
->ds_quota
= 0;
433 if (err
!= 0 || (winner
= dmu_buf_set_user_ie(dbuf
, ds
,
434 &ds
->ds_phys
, dsl_dataset_evict
)) != NULL
) {
435 bplist_destroy(&ds
->ds_pending_deadlist
);
436 dsl_deadlist_close(&ds
->ds_deadlist
);
438 dsl_dataset_rele(ds
->ds_prev
, ds
);
439 dsl_dir_rele(ds
->ds_dir
, ds
);
440 mutex_destroy(&ds
->ds_lock
);
441 mutex_destroy(&ds
->ds_opening_lock
);
442 refcount_destroy(&ds
->ds_longholds
);
443 kmem_free(ds
, sizeof (dsl_dataset_t
));
445 dmu_buf_rele(dbuf
, tag
);
451 unique_insert(ds
->ds_phys
->ds_fsid_guid
);
454 ASSERT3P(ds
->ds_dbuf
, ==, dbuf
);
455 ASSERT3P(ds
->ds_phys
, ==, dbuf
->db_data
);
456 ASSERT(ds
->ds_phys
->ds_prev_snap_obj
!= 0 ||
457 spa_version(dp
->dp_spa
) < SPA_VERSION_ORIGIN
||
458 dp
->dp_origin_snap
== NULL
|| ds
== dp
->dp_origin_snap
);
464 dsl_dataset_hold(dsl_pool_t
*dp
, const char *name
,
465 void *tag
, dsl_dataset_t
**dsp
)
468 const char *snapname
;
472 err
= dsl_dir_hold(dp
, name
, FTAG
, &dd
, &snapname
);
476 ASSERT(dsl_pool_config_held(dp
));
477 obj
= dd
->dd_phys
->dd_head_dataset_obj
;
479 err
= dsl_dataset_hold_obj(dp
, obj
, tag
, dsp
);
481 err
= SET_ERROR(ENOENT
);
483 /* we may be looking for a snapshot */
484 if (err
== 0 && snapname
!= NULL
) {
487 if (*snapname
++ != '@') {
488 dsl_dataset_rele(*dsp
, tag
);
489 dsl_dir_rele(dd
, FTAG
);
490 return (SET_ERROR(ENOENT
));
493 dprintf("looking for snapshot '%s'\n", snapname
);
494 err
= dsl_dataset_snap_lookup(*dsp
, snapname
, &obj
);
496 err
= dsl_dataset_hold_obj(dp
, obj
, tag
, &ds
);
497 dsl_dataset_rele(*dsp
, tag
);
500 mutex_enter(&ds
->ds_lock
);
501 if (ds
->ds_snapname
[0] == 0)
502 (void) strlcpy(ds
->ds_snapname
, snapname
,
503 sizeof (ds
->ds_snapname
));
504 mutex_exit(&ds
->ds_lock
);
509 dsl_dir_rele(dd
, FTAG
);
514 dsl_dataset_own_obj(dsl_pool_t
*dp
, uint64_t dsobj
,
515 void *tag
, dsl_dataset_t
**dsp
)
517 int err
= dsl_dataset_hold_obj(dp
, dsobj
, tag
, dsp
);
520 if (!dsl_dataset_tryown(*dsp
, tag
)) {
521 dsl_dataset_rele(*dsp
, tag
);
523 return (SET_ERROR(EBUSY
));
529 dsl_dataset_own(dsl_pool_t
*dp
, const char *name
,
530 void *tag
, dsl_dataset_t
**dsp
)
532 int err
= dsl_dataset_hold(dp
, name
, tag
, dsp
);
535 if (!dsl_dataset_tryown(*dsp
, tag
)) {
536 dsl_dataset_rele(*dsp
, tag
);
537 return (SET_ERROR(EBUSY
));
543 * See the comment above dsl_pool_hold() for details. In summary, a long
544 * hold is used to prevent destruction of a dataset while the pool hold
545 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
547 * The dataset and pool must be held when this function is called. After it
548 * is called, the pool hold may be released while the dataset is still held
552 dsl_dataset_long_hold(dsl_dataset_t
*ds
, void *tag
)
554 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
555 (void) refcount_add(&ds
->ds_longholds
, tag
);
559 dsl_dataset_long_rele(dsl_dataset_t
*ds
, void *tag
)
561 (void) refcount_remove(&ds
->ds_longholds
, tag
);
564 /* Return B_TRUE if there are any long holds on this dataset. */
566 dsl_dataset_long_held(dsl_dataset_t
*ds
)
568 return (!refcount_is_zero(&ds
->ds_longholds
));
572 dsl_dataset_name(dsl_dataset_t
*ds
, char *name
)
575 (void) strcpy(name
, "mos");
577 dsl_dir_name(ds
->ds_dir
, name
);
578 VERIFY0(dsl_dataset_get_snapname(ds
));
579 if (ds
->ds_snapname
[0]) {
580 (void) strcat(name
, "@");
582 * We use a "recursive" mutex so that we
583 * can call dprintf_ds() with ds_lock held.
585 if (!MUTEX_HELD(&ds
->ds_lock
)) {
586 mutex_enter(&ds
->ds_lock
);
587 (void) strcat(name
, ds
->ds_snapname
);
588 mutex_exit(&ds
->ds_lock
);
590 (void) strcat(name
, ds
->ds_snapname
);
597 dsl_dataset_rele(dsl_dataset_t
*ds
, void *tag
)
599 dmu_buf_rele(ds
->ds_dbuf
, tag
);
603 dsl_dataset_disown(dsl_dataset_t
*ds
, void *tag
)
605 ASSERT(ds
->ds_owner
== tag
&& ds
->ds_dbuf
!= NULL
);
607 mutex_enter(&ds
->ds_lock
);
609 mutex_exit(&ds
->ds_lock
);
610 dsl_dataset_long_rele(ds
, tag
);
611 if (ds
->ds_dbuf
!= NULL
)
612 dsl_dataset_rele(ds
, tag
);
614 dsl_dataset_evict(NULL
, ds
);
618 dsl_dataset_tryown(dsl_dataset_t
*ds
, void *tag
)
620 boolean_t gotit
= FALSE
;
622 mutex_enter(&ds
->ds_lock
);
623 if (ds
->ds_owner
== NULL
&& !DS_IS_INCONSISTENT(ds
)) {
625 dsl_dataset_long_hold(ds
, tag
);
628 mutex_exit(&ds
->ds_lock
);
633 dsl_dataset_create_sync_dd(dsl_dir_t
*dd
, dsl_dataset_t
*origin
,
634 uint64_t flags
, dmu_tx_t
*tx
)
636 dsl_pool_t
*dp
= dd
->dd_pool
;
638 dsl_dataset_phys_t
*dsphys
;
640 objset_t
*mos
= dp
->dp_meta_objset
;
643 origin
= dp
->dp_origin_snap
;
645 ASSERT(origin
== NULL
|| origin
->ds_dir
->dd_pool
== dp
);
646 ASSERT(origin
== NULL
|| origin
->ds_phys
->ds_num_children
> 0);
647 ASSERT(dmu_tx_is_syncing(tx
));
648 ASSERT(dd
->dd_phys
->dd_head_dataset_obj
== 0);
650 dsobj
= dmu_object_alloc(mos
, DMU_OT_DSL_DATASET
, 0,
651 DMU_OT_DSL_DATASET
, sizeof (dsl_dataset_phys_t
), tx
);
652 VERIFY0(dmu_bonus_hold(mos
, dsobj
, FTAG
, &dbuf
));
653 dmu_buf_will_dirty(dbuf
, tx
);
654 dsphys
= dbuf
->db_data
;
655 bzero(dsphys
, sizeof (dsl_dataset_phys_t
));
656 dsphys
->ds_dir_obj
= dd
->dd_object
;
657 dsphys
->ds_flags
= flags
;
658 dsphys
->ds_fsid_guid
= unique_create();
659 (void) random_get_pseudo_bytes((void*)&dsphys
->ds_guid
,
660 sizeof (dsphys
->ds_guid
));
661 dsphys
->ds_snapnames_zapobj
=
662 zap_create_norm(mos
, U8_TEXTPREP_TOUPPER
, DMU_OT_DSL_DS_SNAP_MAP
,
664 dsphys
->ds_creation_time
= gethrestime_sec();
665 dsphys
->ds_creation_txg
= tx
->tx_txg
== TXG_INITIAL
? 1 : tx
->tx_txg
;
667 if (origin
== NULL
) {
668 dsphys
->ds_deadlist_obj
= dsl_deadlist_alloc(mos
, tx
);
670 dsl_dataset_t
*ohds
; /* head of the origin snapshot */
672 dsphys
->ds_prev_snap_obj
= origin
->ds_object
;
673 dsphys
->ds_prev_snap_txg
=
674 origin
->ds_phys
->ds_creation_txg
;
675 dsphys
->ds_referenced_bytes
=
676 origin
->ds_phys
->ds_referenced_bytes
;
677 dsphys
->ds_compressed_bytes
=
678 origin
->ds_phys
->ds_compressed_bytes
;
679 dsphys
->ds_uncompressed_bytes
=
680 origin
->ds_phys
->ds_uncompressed_bytes
;
681 dsphys
->ds_bp
= origin
->ds_phys
->ds_bp
;
682 dsphys
->ds_flags
|= origin
->ds_phys
->ds_flags
;
684 dmu_buf_will_dirty(origin
->ds_dbuf
, tx
);
685 origin
->ds_phys
->ds_num_children
++;
687 VERIFY0(dsl_dataset_hold_obj(dp
,
688 origin
->ds_dir
->dd_phys
->dd_head_dataset_obj
, FTAG
, &ohds
));
689 dsphys
->ds_deadlist_obj
= dsl_deadlist_clone(&ohds
->ds_deadlist
,
690 dsphys
->ds_prev_snap_txg
, dsphys
->ds_prev_snap_obj
, tx
);
691 dsl_dataset_rele(ohds
, FTAG
);
693 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_NEXT_CLONES
) {
694 if (origin
->ds_phys
->ds_next_clones_obj
== 0) {
695 origin
->ds_phys
->ds_next_clones_obj
=
697 DMU_OT_NEXT_CLONES
, DMU_OT_NONE
, 0, tx
);
699 VERIFY0(zap_add_int(mos
,
700 origin
->ds_phys
->ds_next_clones_obj
, dsobj
, tx
));
703 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
704 dd
->dd_phys
->dd_origin_obj
= origin
->ds_object
;
705 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
706 if (origin
->ds_dir
->dd_phys
->dd_clones
== 0) {
707 dmu_buf_will_dirty(origin
->ds_dir
->dd_dbuf
, tx
);
708 origin
->ds_dir
->dd_phys
->dd_clones
=
710 DMU_OT_DSL_CLONES
, DMU_OT_NONE
, 0, tx
);
712 VERIFY0(zap_add_int(mos
,
713 origin
->ds_dir
->dd_phys
->dd_clones
, dsobj
, tx
));
717 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_UNIQUE_ACCURATE
)
718 dsphys
->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
720 dmu_buf_rele(dbuf
, FTAG
);
722 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
723 dd
->dd_phys
->dd_head_dataset_obj
= dsobj
;
729 dsl_dataset_zero_zil(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
733 VERIFY0(dmu_objset_from_ds(ds
, &os
));
734 bzero(&os
->os_zil_header
, sizeof (os
->os_zil_header
));
735 dsl_dataset_dirty(ds
, tx
);
739 dsl_dataset_create_sync(dsl_dir_t
*pdd
, const char *lastname
,
740 dsl_dataset_t
*origin
, uint64_t flags
, cred_t
*cr
, dmu_tx_t
*tx
)
742 dsl_pool_t
*dp
= pdd
->dd_pool
;
743 uint64_t dsobj
, ddobj
;
746 ASSERT(dmu_tx_is_syncing(tx
));
747 ASSERT(lastname
[0] != '@');
749 ddobj
= dsl_dir_create_sync(dp
, pdd
, lastname
, tx
);
750 VERIFY0(dsl_dir_hold_obj(dp
, ddobj
, lastname
, FTAG
, &dd
));
752 dsobj
= dsl_dataset_create_sync_dd(dd
, origin
,
753 flags
& ~DS_CREATE_FLAG_NODIRTY
, tx
);
755 dsl_deleg_set_create_perms(dd
, tx
, cr
);
757 dsl_dir_rele(dd
, FTAG
);
760 * If we are creating a clone, make sure we zero out any stale
761 * data from the origin snapshots zil header.
763 if (origin
!= NULL
&& !(flags
& DS_CREATE_FLAG_NODIRTY
)) {
766 VERIFY0(dsl_dataset_hold_obj(dp
, dsobj
, FTAG
, &ds
));
767 dsl_dataset_zero_zil(ds
, tx
);
768 dsl_dataset_rele(ds
, FTAG
);
775 * The unique space in the head dataset can be calculated by subtracting
776 * the space used in the most recent snapshot, that is still being used
777 * in this file system, from the space currently in use. To figure out
778 * the space in the most recent snapshot still in use, we need to take
779 * the total space used in the snapshot and subtract out the space that
780 * has been freed up since the snapshot was taken.
783 dsl_dataset_recalc_head_uniq(dsl_dataset_t
*ds
)
786 uint64_t dlused
, dlcomp
, dluncomp
;
788 ASSERT(!dsl_dataset_is_snapshot(ds
));
790 if (ds
->ds_phys
->ds_prev_snap_obj
!= 0)
791 mrs_used
= ds
->ds_prev
->ds_phys
->ds_referenced_bytes
;
795 dsl_deadlist_space(&ds
->ds_deadlist
, &dlused
, &dlcomp
, &dluncomp
);
797 ASSERT3U(dlused
, <=, mrs_used
);
798 ds
->ds_phys
->ds_unique_bytes
=
799 ds
->ds_phys
->ds_referenced_bytes
- (mrs_used
- dlused
);
801 if (spa_version(ds
->ds_dir
->dd_pool
->dp_spa
) >=
802 SPA_VERSION_UNIQUE_ACCURATE
)
803 ds
->ds_phys
->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
807 dsl_dataset_remove_from_next_clones(dsl_dataset_t
*ds
, uint64_t obj
,
810 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
812 ASSERTV(uint64_t count
);
814 ASSERT(ds
->ds_phys
->ds_num_children
>= 2);
815 err
= zap_remove_int(mos
, ds
->ds_phys
->ds_next_clones_obj
, obj
, tx
);
817 * The err should not be ENOENT, but a bug in a previous version
818 * of the code could cause upgrade_clones_cb() to not set
819 * ds_next_snap_obj when it should, leading to a missing entry.
820 * If we knew that the pool was created after
821 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
822 * ENOENT. However, at least we can check that we don't have
823 * too many entries in the next_clones_obj even after failing to
828 ASSERT0(zap_count(mos
, ds
->ds_phys
->ds_next_clones_obj
,
830 ASSERT3U(count
, <=, ds
->ds_phys
->ds_num_children
- 2);
835 dsl_dataset_get_blkptr(dsl_dataset_t
*ds
)
837 return (&ds
->ds_phys
->ds_bp
);
841 dsl_dataset_set_blkptr(dsl_dataset_t
*ds
, blkptr_t
*bp
, dmu_tx_t
*tx
)
843 ASSERT(dmu_tx_is_syncing(tx
));
844 /* If it's the meta-objset, set dp_meta_rootbp */
846 tx
->tx_pool
->dp_meta_rootbp
= *bp
;
848 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
849 ds
->ds_phys
->ds_bp
= *bp
;
854 dsl_dataset_get_spa(dsl_dataset_t
*ds
)
856 return (ds
->ds_dir
->dd_pool
->dp_spa
);
860 dsl_dataset_dirty(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
864 if (ds
== NULL
) /* this is the meta-objset */
867 ASSERT(ds
->ds_objset
!= NULL
);
869 if (ds
->ds_phys
->ds_next_snap_obj
!= 0)
870 panic("dirtying snapshot!");
872 dp
= ds
->ds_dir
->dd_pool
;
874 if (txg_list_add(&dp
->dp_dirty_datasets
, ds
, tx
->tx_txg
)) {
875 /* up the hold count until we can be written out */
876 dmu_buf_add_ref(ds
->ds_dbuf
, ds
);
881 dsl_dataset_is_dirty(dsl_dataset_t
*ds
)
885 for (t
= 0; t
< TXG_SIZE
; t
++) {
886 if (txg_list_member(&ds
->ds_dir
->dd_pool
->dp_dirty_datasets
,
894 dsl_dataset_snapshot_reserve_space(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
898 if (!dmu_tx_is_syncing(tx
))
902 * If there's an fs-only reservation, any blocks that might become
903 * owned by the snapshot dataset must be accommodated by space
904 * outside of the reservation.
906 ASSERT(ds
->ds_reserved
== 0 || DS_UNIQUE_IS_ACCURATE(ds
));
907 asize
= MIN(ds
->ds_phys
->ds_unique_bytes
, ds
->ds_reserved
);
908 if (asize
> dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
))
909 return (SET_ERROR(ENOSPC
));
912 * Propagate any reserved space for this snapshot to other
913 * snapshot checks in this sync group.
916 dsl_dir_willuse_space(ds
->ds_dir
, asize
, tx
);
921 typedef struct dsl_dataset_snapshot_arg
{
922 nvlist_t
*ddsa_snaps
;
923 nvlist_t
*ddsa_props
;
924 nvlist_t
*ddsa_errors
;
925 } dsl_dataset_snapshot_arg_t
;
928 dsl_dataset_snapshot_check_impl(dsl_dataset_t
*ds
, const char *snapname
,
929 dmu_tx_t
*tx
, boolean_t recv
)
934 ds
->ds_trysnap_txg
= tx
->tx_txg
;
936 if (!dmu_tx_is_syncing(tx
))
940 * We don't allow multiple snapshots of the same txg. If there
941 * is already one, try again.
943 if (ds
->ds_phys
->ds_prev_snap_txg
>= tx
->tx_txg
)
944 return (SET_ERROR(EAGAIN
));
947 * Check for conflicting snapshot name.
949 error
= dsl_dataset_snap_lookup(ds
, snapname
, &value
);
951 return (SET_ERROR(EEXIST
));
956 * We don't allow taking snapshots of inconsistent datasets, such as
957 * those into which we are currently receiving. However, if we are
958 * creating this snapshot as part of a receive, this check will be
959 * executed atomically with respect to the completion of the receive
960 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
961 * case we ignore this, knowing it will be fixed up for us shortly in
962 * dmu_recv_end_sync().
964 if (!recv
&& DS_IS_INCONSISTENT(ds
))
965 return (SET_ERROR(EBUSY
));
967 error
= dsl_dataset_snapshot_reserve_space(ds
, tx
);
975 dsl_dataset_snapshot_check(void *arg
, dmu_tx_t
*tx
)
977 dsl_dataset_snapshot_arg_t
*ddsa
= arg
;
978 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
982 for (pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, NULL
);
983 pair
!= NULL
; pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, pair
)) {
987 char dsname
[MAXNAMELEN
];
989 name
= nvpair_name(pair
);
990 if (strlen(name
) >= MAXNAMELEN
)
991 error
= SET_ERROR(ENAMETOOLONG
);
993 atp
= strchr(name
, '@');
995 error
= SET_ERROR(EINVAL
);
997 (void) strlcpy(dsname
, name
, atp
- name
+ 1);
1000 error
= dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
);
1002 error
= dsl_dataset_snapshot_check_impl(ds
,
1003 atp
+ 1, tx
, B_FALSE
);
1004 dsl_dataset_rele(ds
, FTAG
);
1008 if (ddsa
->ddsa_errors
!= NULL
) {
1009 fnvlist_add_int32(ddsa
->ddsa_errors
,
1019 dsl_dataset_snapshot_sync_impl(dsl_dataset_t
*ds
, const char *snapname
,
1022 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1024 dsl_dataset_phys_t
*dsphys
;
1025 uint64_t dsobj
, crtxg
;
1026 objset_t
*mos
= dp
->dp_meta_objset
;
1027 ASSERTV(static zil_header_t zero_zil
);
1028 ASSERTV(objset_t
*os
);
1030 ASSERT(RRW_WRITE_HELD(&dp
->dp_config_rwlock
));
1033 * If we are on an old pool, the zil must not be active, in which
1034 * case it will be zeroed. Usually zil_suspend() accomplishes this.
1036 ASSERT(spa_version(dmu_tx_pool(tx
)->dp_spa
) >= SPA_VERSION_FAST_SNAP
||
1037 dmu_objset_from_ds(ds
, &os
) != 0 ||
1038 bcmp(&os
->os_phys
->os_zil_header
, &zero_zil
,
1039 sizeof (zero_zil
)) == 0);
1043 * The origin's ds_creation_txg has to be < TXG_INITIAL
1045 if (strcmp(snapname
, ORIGIN_DIR_NAME
) == 0)
1050 dsobj
= dmu_object_alloc(mos
, DMU_OT_DSL_DATASET
, 0,
1051 DMU_OT_DSL_DATASET
, sizeof (dsl_dataset_phys_t
), tx
);
1052 VERIFY0(dmu_bonus_hold(mos
, dsobj
, FTAG
, &dbuf
));
1053 dmu_buf_will_dirty(dbuf
, tx
);
1054 dsphys
= dbuf
->db_data
;
1055 bzero(dsphys
, sizeof (dsl_dataset_phys_t
));
1056 dsphys
->ds_dir_obj
= ds
->ds_dir
->dd_object
;
1057 dsphys
->ds_fsid_guid
= unique_create();
1058 (void) random_get_pseudo_bytes((void*)&dsphys
->ds_guid
,
1059 sizeof (dsphys
->ds_guid
));
1060 dsphys
->ds_prev_snap_obj
= ds
->ds_phys
->ds_prev_snap_obj
;
1061 dsphys
->ds_prev_snap_txg
= ds
->ds_phys
->ds_prev_snap_txg
;
1062 dsphys
->ds_next_snap_obj
= ds
->ds_object
;
1063 dsphys
->ds_num_children
= 1;
1064 dsphys
->ds_creation_time
= gethrestime_sec();
1065 dsphys
->ds_creation_txg
= crtxg
;
1066 dsphys
->ds_deadlist_obj
= ds
->ds_phys
->ds_deadlist_obj
;
1067 dsphys
->ds_referenced_bytes
= ds
->ds_phys
->ds_referenced_bytes
;
1068 dsphys
->ds_compressed_bytes
= ds
->ds_phys
->ds_compressed_bytes
;
1069 dsphys
->ds_uncompressed_bytes
= ds
->ds_phys
->ds_uncompressed_bytes
;
1070 dsphys
->ds_flags
= ds
->ds_phys
->ds_flags
;
1071 dsphys
->ds_bp
= ds
->ds_phys
->ds_bp
;
1072 dmu_buf_rele(dbuf
, FTAG
);
1074 ASSERT3U(ds
->ds_prev
!= 0, ==, ds
->ds_phys
->ds_prev_snap_obj
!= 0);
1076 uint64_t next_clones_obj
=
1077 ds
->ds_prev
->ds_phys
->ds_next_clones_obj
;
1078 ASSERT(ds
->ds_prev
->ds_phys
->ds_next_snap_obj
==
1080 ds
->ds_prev
->ds_phys
->ds_num_children
> 1);
1081 if (ds
->ds_prev
->ds_phys
->ds_next_snap_obj
== ds
->ds_object
) {
1082 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
1083 ASSERT3U(ds
->ds_phys
->ds_prev_snap_txg
, ==,
1084 ds
->ds_prev
->ds_phys
->ds_creation_txg
);
1085 ds
->ds_prev
->ds_phys
->ds_next_snap_obj
= dsobj
;
1086 } else if (next_clones_obj
!= 0) {
1087 dsl_dataset_remove_from_next_clones(ds
->ds_prev
,
1088 dsphys
->ds_next_snap_obj
, tx
);
1089 VERIFY0(zap_add_int(mos
,
1090 next_clones_obj
, dsobj
, tx
));
1095 * If we have a reference-reservation on this dataset, we will
1096 * need to increase the amount of refreservation being charged
1097 * since our unique space is going to zero.
1099 if (ds
->ds_reserved
) {
1101 ASSERT(DS_UNIQUE_IS_ACCURATE(ds
));
1102 delta
= MIN(ds
->ds_phys
->ds_unique_bytes
, ds
->ds_reserved
);
1103 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_REFRSRV
,
1107 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1108 ds
->ds_phys
->ds_deadlist_obj
= dsl_deadlist_clone(&ds
->ds_deadlist
,
1109 UINT64_MAX
, ds
->ds_phys
->ds_prev_snap_obj
, tx
);
1110 dsl_deadlist_close(&ds
->ds_deadlist
);
1111 dsl_deadlist_open(&ds
->ds_deadlist
, mos
, ds
->ds_phys
->ds_deadlist_obj
);
1112 dsl_deadlist_add_key(&ds
->ds_deadlist
,
1113 ds
->ds_phys
->ds_prev_snap_txg
, tx
);
1115 ASSERT3U(ds
->ds_phys
->ds_prev_snap_txg
, <, tx
->tx_txg
);
1116 ds
->ds_phys
->ds_prev_snap_obj
= dsobj
;
1117 ds
->ds_phys
->ds_prev_snap_txg
= crtxg
;
1118 ds
->ds_phys
->ds_unique_bytes
= 0;
1119 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_UNIQUE_ACCURATE
)
1120 ds
->ds_phys
->ds_flags
|= DS_FLAG_UNIQUE_ACCURATE
;
1122 VERIFY0(zap_add(mos
, ds
->ds_phys
->ds_snapnames_zapobj
,
1123 snapname
, 8, 1, &dsobj
, tx
));
1126 dsl_dataset_rele(ds
->ds_prev
, ds
);
1127 VERIFY0(dsl_dataset_hold_obj(dp
,
1128 ds
->ds_phys
->ds_prev_snap_obj
, ds
, &ds
->ds_prev
));
1130 dsl_scan_ds_snapshotted(ds
, tx
);
1132 dsl_dir_snap_cmtime_update(ds
->ds_dir
);
1134 spa_history_log_internal_ds(ds
->ds_prev
, "snapshot", tx
, "");
1138 dsl_dataset_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
1140 dsl_dataset_snapshot_arg_t
*ddsa
= arg
;
1141 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1144 for (pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, NULL
);
1145 pair
!= NULL
; pair
= nvlist_next_nvpair(ddsa
->ddsa_snaps
, pair
)) {
1148 char dsname
[MAXNAMELEN
];
1150 name
= nvpair_name(pair
);
1151 atp
= strchr(name
, '@');
1152 (void) strlcpy(dsname
, name
, atp
- name
+ 1);
1153 VERIFY0(dsl_dataset_hold(dp
, dsname
, FTAG
, &ds
));
1155 dsl_dataset_snapshot_sync_impl(ds
, atp
+ 1, tx
);
1156 if (ddsa
->ddsa_props
!= NULL
) {
1157 dsl_props_set_sync_impl(ds
->ds_prev
,
1158 ZPROP_SRC_LOCAL
, ddsa
->ddsa_props
, tx
);
1160 dsl_dataset_rele(ds
, FTAG
);
1165 * The snapshots must all be in the same pool.
1166 * All-or-nothing: if there are any failures, nothing will be modified.
1169 dsl_dataset_snapshot(nvlist_t
*snaps
, nvlist_t
*props
, nvlist_t
*errors
)
1171 dsl_dataset_snapshot_arg_t ddsa
;
1173 boolean_t needsuspend
;
1177 nvlist_t
*suspended
= NULL
;
1179 pair
= nvlist_next_nvpair(snaps
, NULL
);
1182 firstname
= nvpair_name(pair
);
1184 error
= spa_open(firstname
, &spa
, FTAG
);
1187 needsuspend
= (spa_version(spa
) < SPA_VERSION_FAST_SNAP
);
1188 spa_close(spa
, FTAG
);
1191 suspended
= fnvlist_alloc();
1192 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
1193 pair
= nvlist_next_nvpair(snaps
, pair
)) {
1194 char fsname
[MAXNAMELEN
];
1195 char *snapname
= nvpair_name(pair
);
1199 atp
= strchr(snapname
, '@');
1201 error
= SET_ERROR(EINVAL
);
1204 (void) strlcpy(fsname
, snapname
, atp
- snapname
+ 1);
1206 error
= zil_suspend(fsname
, &cookie
);
1209 fnvlist_add_uint64(suspended
, fsname
,
1214 ddsa
.ddsa_snaps
= snaps
;
1215 ddsa
.ddsa_props
= props
;
1216 ddsa
.ddsa_errors
= errors
;
1219 error
= dsl_sync_task(firstname
, dsl_dataset_snapshot_check
,
1220 dsl_dataset_snapshot_sync
, &ddsa
,
1221 fnvlist_num_pairs(snaps
) * 3);
1224 if (suspended
!= NULL
) {
1225 for (pair
= nvlist_next_nvpair(suspended
, NULL
); pair
!= NULL
;
1226 pair
= nvlist_next_nvpair(suspended
, pair
)) {
1227 zil_resume((void *)(uintptr_t)
1228 fnvpair_value_uint64(pair
));
1230 fnvlist_free(suspended
);
1235 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
1236 pair
= nvlist_next_nvpair(snaps
, pair
)) {
1237 char *snapname
= nvpair_name(pair
);
1238 zvol_create_minors(snapname
);
1246 typedef struct dsl_dataset_snapshot_tmp_arg
{
1247 const char *ddsta_fsname
;
1248 const char *ddsta_snapname
;
1249 minor_t ddsta_cleanup_minor
;
1250 const char *ddsta_htag
;
1251 } dsl_dataset_snapshot_tmp_arg_t
;
1254 dsl_dataset_snapshot_tmp_check(void *arg
, dmu_tx_t
*tx
)
1256 dsl_dataset_snapshot_tmp_arg_t
*ddsta
= arg
;
1257 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1261 error
= dsl_dataset_hold(dp
, ddsta
->ddsta_fsname
, FTAG
, &ds
);
1265 error
= dsl_dataset_snapshot_check_impl(ds
, ddsta
->ddsta_snapname
,
1268 dsl_dataset_rele(ds
, FTAG
);
1272 if (spa_version(dp
->dp_spa
) < SPA_VERSION_USERREFS
) {
1273 dsl_dataset_rele(ds
, FTAG
);
1274 return (SET_ERROR(ENOTSUP
));
1276 error
= dsl_dataset_user_hold_check_one(NULL
, ddsta
->ddsta_htag
,
1279 dsl_dataset_rele(ds
, FTAG
);
1283 dsl_dataset_rele(ds
, FTAG
);
1288 dsl_dataset_snapshot_tmp_sync(void *arg
, dmu_tx_t
*tx
)
1290 dsl_dataset_snapshot_tmp_arg_t
*ddsta
= arg
;
1291 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1294 VERIFY0(dsl_dataset_hold(dp
, ddsta
->ddsta_fsname
, FTAG
, &ds
));
1296 dsl_dataset_snapshot_sync_impl(ds
, ddsta
->ddsta_snapname
, tx
);
1297 dsl_dataset_user_hold_sync_one(ds
->ds_prev
, ddsta
->ddsta_htag
,
1298 ddsta
->ddsta_cleanup_minor
, gethrestime_sec(), tx
);
1299 dsl_destroy_snapshot_sync_impl(ds
->ds_prev
, B_TRUE
, tx
);
1301 dsl_dataset_rele(ds
, FTAG
);
1305 dsl_dataset_snapshot_tmp(const char *fsname
, const char *snapname
,
1306 minor_t cleanup_minor
, const char *htag
)
1308 dsl_dataset_snapshot_tmp_arg_t ddsta
;
1311 boolean_t needsuspend
;
1314 ddsta
.ddsta_fsname
= fsname
;
1315 ddsta
.ddsta_snapname
= snapname
;
1316 ddsta
.ddsta_cleanup_minor
= cleanup_minor
;
1317 ddsta
.ddsta_htag
= htag
;
1319 error
= spa_open(fsname
, &spa
, FTAG
);
1322 needsuspend
= (spa_version(spa
) < SPA_VERSION_FAST_SNAP
);
1323 spa_close(spa
, FTAG
);
1326 error
= zil_suspend(fsname
, &cookie
);
1331 error
= dsl_sync_task(fsname
, dsl_dataset_snapshot_tmp_check
,
1332 dsl_dataset_snapshot_tmp_sync
, &ddsta
, 3);
1341 dsl_dataset_sync(dsl_dataset_t
*ds
, zio_t
*zio
, dmu_tx_t
*tx
)
1343 ASSERT(dmu_tx_is_syncing(tx
));
1344 ASSERT(ds
->ds_objset
!= NULL
);
1345 ASSERT(ds
->ds_phys
->ds_next_snap_obj
== 0);
1348 * in case we had to change ds_fsid_guid when we opened it,
1351 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
1352 ds
->ds_phys
->ds_fsid_guid
= ds
->ds_fsid_guid
;
1354 dmu_objset_sync(ds
->ds_objset
, zio
, tx
);
1358 get_clones_stat(dsl_dataset_t
*ds
, nvlist_t
*nv
)
1361 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
1364 nvlist_t
*propval
= fnvlist_alloc();
1365 nvlist_t
*val
= fnvlist_alloc();
1367 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1370 * There may be missing entries in ds_next_clones_obj
1371 * due to a bug in a previous version of the code.
1372 * Only trust it if it has the right number of entries.
1374 if (ds
->ds_phys
->ds_next_clones_obj
!= 0) {
1375 VERIFY0(zap_count(mos
, ds
->ds_phys
->ds_next_clones_obj
,
1378 if (count
!= ds
->ds_phys
->ds_num_children
- 1)
1380 for (zap_cursor_init(&zc
, mos
, ds
->ds_phys
->ds_next_clones_obj
);
1381 zap_cursor_retrieve(&zc
, &za
) == 0;
1382 zap_cursor_advance(&zc
)) {
1383 dsl_dataset_t
*clone
;
1384 char buf
[ZFS_MAXNAMELEN
];
1385 VERIFY0(dsl_dataset_hold_obj(ds
->ds_dir
->dd_pool
,
1386 za
.za_first_integer
, FTAG
, &clone
));
1387 dsl_dir_name(clone
->ds_dir
, buf
);
1388 fnvlist_add_boolean(val
, buf
);
1389 dsl_dataset_rele(clone
, FTAG
);
1391 zap_cursor_fini(&zc
);
1392 fnvlist_add_nvlist(propval
, ZPROP_VALUE
, val
);
1393 fnvlist_add_nvlist(nv
, zfs_prop_to_name(ZFS_PROP_CLONES
), propval
);
1396 nvlist_free(propval
);
1400 dsl_dataset_stats(dsl_dataset_t
*ds
, nvlist_t
*nv
)
1402 uint64_t refd
, avail
, uobjs
, aobjs
, ratio
;
1403 ASSERTV(dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
);
1405 ASSERT(dsl_pool_config_held(dp
));
1407 ratio
= ds
->ds_phys
->ds_compressed_bytes
== 0 ? 100 :
1408 (ds
->ds_phys
->ds_uncompressed_bytes
* 100 /
1409 ds
->ds_phys
->ds_compressed_bytes
);
1411 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFRATIO
, ratio
);
1412 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_LOGICALREFERENCED
,
1413 ds
->ds_phys
->ds_uncompressed_bytes
);
1415 if (dsl_dataset_is_snapshot(ds
)) {
1416 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_COMPRESSRATIO
, ratio
);
1417 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USED
,
1418 ds
->ds_phys
->ds_unique_bytes
);
1419 get_clones_stat(ds
, nv
);
1421 dsl_dir_stats(ds
->ds_dir
, nv
);
1424 dsl_dataset_space(ds
, &refd
, &avail
, &uobjs
, &aobjs
);
1425 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_AVAILABLE
, avail
);
1426 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFERENCED
, refd
);
1428 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_CREATION
,
1429 ds
->ds_phys
->ds_creation_time
);
1430 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_CREATETXG
,
1431 ds
->ds_phys
->ds_creation_txg
);
1432 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFQUOTA
,
1434 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_REFRESERVATION
,
1436 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_GUID
,
1437 ds
->ds_phys
->ds_guid
);
1438 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_UNIQUE
,
1439 ds
->ds_phys
->ds_unique_bytes
);
1440 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_OBJSETID
,
1442 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USERREFS
,
1444 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_DEFER_DESTROY
,
1445 DS_IS_DEFER_DESTROY(ds
) ? 1 : 0);
1447 if (ds
->ds_phys
->ds_prev_snap_obj
!= 0) {
1448 uint64_t written
, comp
, uncomp
;
1449 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1450 dsl_dataset_t
*prev
;
1453 err
= dsl_dataset_hold_obj(dp
,
1454 ds
->ds_phys
->ds_prev_snap_obj
, FTAG
, &prev
);
1456 err
= dsl_dataset_space_written(prev
, ds
, &written
,
1458 dsl_dataset_rele(prev
, FTAG
);
1460 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_WRITTEN
,
1469 dsl_dataset_fast_stat(dsl_dataset_t
*ds
, dmu_objset_stats_t
*stat
)
1471 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
1472 ASSERT(dsl_pool_config_held(dp
));
1474 stat
->dds_creation_txg
= ds
->ds_phys
->ds_creation_txg
;
1475 stat
->dds_inconsistent
= ds
->ds_phys
->ds_flags
& DS_FLAG_INCONSISTENT
;
1476 stat
->dds_guid
= ds
->ds_phys
->ds_guid
;
1477 stat
->dds_origin
[0] = '\0';
1478 if (dsl_dataset_is_snapshot(ds
)) {
1479 stat
->dds_is_snapshot
= B_TRUE
;
1480 stat
->dds_num_clones
= ds
->ds_phys
->ds_num_children
- 1;
1482 stat
->dds_is_snapshot
= B_FALSE
;
1483 stat
->dds_num_clones
= 0;
1485 if (dsl_dir_is_clone(ds
->ds_dir
)) {
1488 VERIFY0(dsl_dataset_hold_obj(dp
,
1489 ds
->ds_dir
->dd_phys
->dd_origin_obj
, FTAG
, &ods
));
1490 dsl_dataset_name(ods
, stat
->dds_origin
);
1491 dsl_dataset_rele(ods
, FTAG
);
1497 dsl_dataset_fsid_guid(dsl_dataset_t
*ds
)
1499 return (ds
->ds_fsid_guid
);
1503 dsl_dataset_space(dsl_dataset_t
*ds
,
1504 uint64_t *refdbytesp
, uint64_t *availbytesp
,
1505 uint64_t *usedobjsp
, uint64_t *availobjsp
)
1507 *refdbytesp
= ds
->ds_phys
->ds_referenced_bytes
;
1508 *availbytesp
= dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
);
1509 if (ds
->ds_reserved
> ds
->ds_phys
->ds_unique_bytes
)
1510 *availbytesp
+= ds
->ds_reserved
- ds
->ds_phys
->ds_unique_bytes
;
1511 if (ds
->ds_quota
!= 0) {
1513 * Adjust available bytes according to refquota
1515 if (*refdbytesp
< ds
->ds_quota
)
1516 *availbytesp
= MIN(*availbytesp
,
1517 ds
->ds_quota
- *refdbytesp
);
1521 *usedobjsp
= ds
->ds_phys
->ds_bp
.blk_fill
;
1522 *availobjsp
= DN_MAX_OBJECT
- *usedobjsp
;
1526 dsl_dataset_modified_since_snap(dsl_dataset_t
*ds
, dsl_dataset_t
*snap
)
1528 ASSERT(dsl_pool_config_held(ds
->ds_dir
->dd_pool
));
1531 if (ds
->ds_phys
->ds_bp
.blk_birth
>
1532 snap
->ds_phys
->ds_creation_txg
) {
1533 objset_t
*os
, *os_snap
;
1535 * It may be that only the ZIL differs, because it was
1536 * reset in the head. Don't count that as being
1539 if (dmu_objset_from_ds(ds
, &os
) != 0)
1541 if (dmu_objset_from_ds(snap
, &os_snap
) != 0)
1543 return (bcmp(&os
->os_phys
->os_meta_dnode
,
1544 &os_snap
->os_phys
->os_meta_dnode
,
1545 sizeof (os
->os_phys
->os_meta_dnode
)) != 0);
1550 typedef struct dsl_dataset_rename_snapshot_arg
{
1551 const char *ddrsa_fsname
;
1552 const char *ddrsa_oldsnapname
;
1553 const char *ddrsa_newsnapname
;
1554 boolean_t ddrsa_recursive
;
1556 } dsl_dataset_rename_snapshot_arg_t
;
1560 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t
*dp
,
1561 dsl_dataset_t
*hds
, void *arg
)
1563 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1567 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_oldsnapname
, &val
);
1569 /* ignore nonexistent snapshots */
1570 return (error
== ENOENT
? 0 : error
);
1573 /* new name should not exist */
1574 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_newsnapname
, &val
);
1576 error
= SET_ERROR(EEXIST
);
1577 else if (error
== ENOENT
)
1580 /* dataset name + 1 for the "@" + the new snapshot name must fit */
1581 if (dsl_dir_namelen(hds
->ds_dir
) + 1 +
1582 strlen(ddrsa
->ddrsa_newsnapname
) >= MAXNAMELEN
)
1583 error
= SET_ERROR(ENAMETOOLONG
);
1589 dsl_dataset_rename_snapshot_check(void *arg
, dmu_tx_t
*tx
)
1591 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1592 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1596 error
= dsl_dataset_hold(dp
, ddrsa
->ddrsa_fsname
, FTAG
, &hds
);
1600 if (ddrsa
->ddrsa_recursive
) {
1601 error
= dmu_objset_find_dp(dp
, hds
->ds_dir
->dd_object
,
1602 dsl_dataset_rename_snapshot_check_impl
, ddrsa
,
1605 error
= dsl_dataset_rename_snapshot_check_impl(dp
, hds
, ddrsa
);
1607 dsl_dataset_rele(hds
, FTAG
);
1612 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t
*dp
,
1613 dsl_dataset_t
*hds
, void *arg
)
1616 char *oldname
, *newname
;
1618 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1621 dmu_tx_t
*tx
= ddrsa
->ddrsa_tx
;
1624 error
= dsl_dataset_snap_lookup(hds
, ddrsa
->ddrsa_oldsnapname
, &val
);
1625 ASSERT(error
== 0 || error
== ENOENT
);
1626 if (error
== ENOENT
) {
1627 /* ignore nonexistent snapshots */
1631 VERIFY0(dsl_dataset_hold_obj(dp
, val
, FTAG
, &ds
));
1633 /* log before we change the name */
1634 spa_history_log_internal_ds(ds
, "rename", tx
,
1635 "-> @%s", ddrsa
->ddrsa_newsnapname
);
1637 VERIFY0(dsl_dataset_snap_remove(hds
, ddrsa
->ddrsa_oldsnapname
, tx
));
1638 mutex_enter(&ds
->ds_lock
);
1639 (void) strcpy(ds
->ds_snapname
, ddrsa
->ddrsa_newsnapname
);
1640 mutex_exit(&ds
->ds_lock
);
1641 VERIFY0(zap_add(dp
->dp_meta_objset
, hds
->ds_phys
->ds_snapnames_zapobj
,
1642 ds
->ds_snapname
, 8, 1, &ds
->ds_object
, tx
));
1645 oldname
= kmem_alloc(MAXPATHLEN
, KM_PUSHPAGE
);
1646 newname
= kmem_alloc(MAXPATHLEN
, KM_PUSHPAGE
);
1647 snprintf(oldname
, MAXPATHLEN
, "%s@%s", ddrsa
->ddrsa_fsname
,
1648 ddrsa
->ddrsa_oldsnapname
);
1649 snprintf(newname
, MAXPATHLEN
, "%s@%s", ddrsa
->ddrsa_fsname
,
1650 ddrsa
->ddrsa_newsnapname
);
1651 zvol_rename_minors(oldname
, newname
);
1652 kmem_free(newname
, MAXPATHLEN
);
1653 kmem_free(oldname
, MAXPATHLEN
);
1656 dsl_dataset_rele(ds
, FTAG
);
1661 dsl_dataset_rename_snapshot_sync(void *arg
, dmu_tx_t
*tx
)
1663 dsl_dataset_rename_snapshot_arg_t
*ddrsa
= arg
;
1664 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1667 VERIFY0(dsl_dataset_hold(dp
, ddrsa
->ddrsa_fsname
, FTAG
, &hds
));
1668 ddrsa
->ddrsa_tx
= tx
;
1669 if (ddrsa
->ddrsa_recursive
) {
1670 VERIFY0(dmu_objset_find_dp(dp
, hds
->ds_dir
->dd_object
,
1671 dsl_dataset_rename_snapshot_sync_impl
, ddrsa
,
1674 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp
, hds
, ddrsa
));
1676 dsl_dataset_rele(hds
, FTAG
);
1680 dsl_dataset_rename_snapshot(const char *fsname
,
1681 const char *oldsnapname
, const char *newsnapname
, boolean_t recursive
)
1683 dsl_dataset_rename_snapshot_arg_t ddrsa
;
1685 ddrsa
.ddrsa_fsname
= fsname
;
1686 ddrsa
.ddrsa_oldsnapname
= oldsnapname
;
1687 ddrsa
.ddrsa_newsnapname
= newsnapname
;
1688 ddrsa
.ddrsa_recursive
= recursive
;
1690 return (dsl_sync_task(fsname
, dsl_dataset_rename_snapshot_check
,
1691 dsl_dataset_rename_snapshot_sync
, &ddrsa
, 1));
1695 * If we're doing an ownership handoff, we need to make sure that there is
1696 * only one long hold on the dataset. We're not allowed to change anything here
1697 * so we don't permanently release the long hold or regular hold here. We want
1698 * to do this only when syncing to avoid the dataset unexpectedly going away
1699 * when we release the long hold.
1702 dsl_dataset_handoff_check(dsl_dataset_t
*ds
, void *owner
, dmu_tx_t
*tx
)
1706 if (!dmu_tx_is_syncing(tx
))
1709 if (owner
!= NULL
) {
1710 VERIFY3P(ds
->ds_owner
, ==, owner
);
1711 dsl_dataset_long_rele(ds
, owner
);
1714 held
= dsl_dataset_long_held(ds
);
1717 dsl_dataset_long_hold(ds
, owner
);
1720 return (SET_ERROR(EBUSY
));
1725 typedef struct dsl_dataset_rollback_arg
{
1726 const char *ddra_fsname
;
1728 nvlist_t
*ddra_result
;
1729 } dsl_dataset_rollback_arg_t
;
1732 dsl_dataset_rollback_check(void *arg
, dmu_tx_t
*tx
)
1734 dsl_dataset_rollback_arg_t
*ddra
= arg
;
1735 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1737 int64_t unused_refres_delta
;
1740 error
= dsl_dataset_hold(dp
, ddra
->ddra_fsname
, FTAG
, &ds
);
1744 /* must not be a snapshot */
1745 if (dsl_dataset_is_snapshot(ds
)) {
1746 dsl_dataset_rele(ds
, FTAG
);
1747 return (SET_ERROR(EINVAL
));
1750 /* must have a most recent snapshot */
1751 if (ds
->ds_phys
->ds_prev_snap_txg
< TXG_INITIAL
) {
1752 dsl_dataset_rele(ds
, FTAG
);
1753 return (SET_ERROR(EINVAL
));
1756 error
= dsl_dataset_handoff_check(ds
, ddra
->ddra_owner
, tx
);
1758 dsl_dataset_rele(ds
, FTAG
);
1763 * Check if the snap we are rolling back to uses more than
1766 if (ds
->ds_quota
!= 0 &&
1767 ds
->ds_prev
->ds_phys
->ds_referenced_bytes
> ds
->ds_quota
) {
1768 dsl_dataset_rele(ds
, FTAG
);
1769 return (SET_ERROR(EDQUOT
));
1773 * When we do the clone swap, we will temporarily use more space
1774 * due to the refreservation (the head will no longer have any
1775 * unique space, so the entire amount of the refreservation will need
1776 * to be free). We will immediately destroy the clone, freeing
1777 * this space, but the freeing happens over many txg's.
1779 unused_refres_delta
= (int64_t)MIN(ds
->ds_reserved
,
1780 ds
->ds_phys
->ds_unique_bytes
);
1782 if (unused_refres_delta
> 0 &&
1783 unused_refres_delta
>
1784 dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, TRUE
)) {
1785 dsl_dataset_rele(ds
, FTAG
);
1786 return (SET_ERROR(ENOSPC
));
1789 dsl_dataset_rele(ds
, FTAG
);
1794 dsl_dataset_rollback_sync(void *arg
, dmu_tx_t
*tx
)
1796 dsl_dataset_rollback_arg_t
*ddra
= arg
;
1797 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1798 dsl_dataset_t
*ds
, *clone
;
1800 char namebuf
[ZFS_MAXNAMELEN
];
1802 VERIFY0(dsl_dataset_hold(dp
, ddra
->ddra_fsname
, FTAG
, &ds
));
1804 dsl_dataset_name(ds
->ds_prev
, namebuf
);
1805 fnvlist_add_string(ddra
->ddra_result
, "target", namebuf
);
1807 cloneobj
= dsl_dataset_create_sync(ds
->ds_dir
, "%rollback",
1808 ds
->ds_prev
, DS_CREATE_FLAG_NODIRTY
, kcred
, tx
);
1810 VERIFY0(dsl_dataset_hold_obj(dp
, cloneobj
, FTAG
, &clone
));
1812 dsl_dataset_clone_swap_sync_impl(clone
, ds
, tx
);
1813 dsl_dataset_zero_zil(ds
, tx
);
1815 dsl_destroy_head_sync_impl(clone
, tx
);
1817 dsl_dataset_rele(clone
, FTAG
);
1818 dsl_dataset_rele(ds
, FTAG
);
1822 * Rolls back the given filesystem or volume to the most recent snapshot.
1823 * The name of the most recent snapshot will be returned under key "target"
1824 * in the result nvlist.
1827 * - The existing dataset MUST be owned by the specified owner at entry
1828 * - Upon return, dataset will still be held by the same owner, whether we
1831 * This mode is required any time the existing filesystem is mounted. See
1832 * notes above zfs_suspend_fs() for further details.
1835 dsl_dataset_rollback(const char *fsname
, void *owner
, nvlist_t
*result
)
1837 dsl_dataset_rollback_arg_t ddra
;
1839 ddra
.ddra_fsname
= fsname
;
1840 ddra
.ddra_owner
= owner
;
1841 ddra
.ddra_result
= result
;
1843 return (dsl_sync_task(fsname
, dsl_dataset_rollback_check
,
1844 dsl_dataset_rollback_sync
, &ddra
, 1));
1847 struct promotenode
{
1852 typedef struct dsl_dataset_promote_arg
{
1853 const char *ddpa_clonename
;
1854 dsl_dataset_t
*ddpa_clone
;
1855 list_t shared_snaps
, origin_snaps
, clone_snaps
;
1856 dsl_dataset_t
*origin_origin
; /* origin of the origin */
1857 uint64_t used
, comp
, uncomp
, unique
, cloneusedsnap
, originusedsnap
;
1859 } dsl_dataset_promote_arg_t
;
1861 static int snaplist_space(list_t
*l
, uint64_t mintxg
, uint64_t *spacep
);
1862 static int promote_hold(dsl_dataset_promote_arg_t
*ddpa
, dsl_pool_t
*dp
,
1864 static void promote_rele(dsl_dataset_promote_arg_t
*ddpa
, void *tag
);
1867 dsl_dataset_promote_check(void *arg
, dmu_tx_t
*tx
)
1869 dsl_dataset_promote_arg_t
*ddpa
= arg
;
1870 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1872 struct promotenode
*snap
;
1873 dsl_dataset_t
*origin_ds
;
1877 err
= promote_hold(ddpa
, dp
, FTAG
);
1881 hds
= ddpa
->ddpa_clone
;
1883 if (hds
->ds_phys
->ds_flags
& DS_FLAG_NOPROMOTE
) {
1884 promote_rele(ddpa
, FTAG
);
1885 return (SET_ERROR(EXDEV
));
1889 * Compute and check the amount of space to transfer. Since this is
1890 * so expensive, don't do the preliminary check.
1892 if (!dmu_tx_is_syncing(tx
)) {
1893 promote_rele(ddpa
, FTAG
);
1897 snap
= list_head(&ddpa
->shared_snaps
);
1898 origin_ds
= snap
->ds
;
1900 /* compute origin's new unique space */
1901 snap
= list_tail(&ddpa
->clone_snaps
);
1902 ASSERT3U(snap
->ds
->ds_phys
->ds_prev_snap_obj
, ==, origin_ds
->ds_object
);
1903 dsl_deadlist_space_range(&snap
->ds
->ds_deadlist
,
1904 origin_ds
->ds_phys
->ds_prev_snap_txg
, UINT64_MAX
,
1905 &ddpa
->unique
, &unused
, &unused
);
1908 * Walk the snapshots that we are moving
1910 * Compute space to transfer. Consider the incremental changes
1911 * to used by each snapshot:
1912 * (my used) = (prev's used) + (blocks born) - (blocks killed)
1913 * So each snapshot gave birth to:
1914 * (blocks born) = (my used) - (prev's used) + (blocks killed)
1915 * So a sequence would look like:
1916 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
1917 * Which simplifies to:
1918 * uN + kN + kN-1 + ... + k1 + k0
1919 * Note however, if we stop before we reach the ORIGIN we get:
1920 * uN + kN + kN-1 + ... + kM - uM-1
1922 ddpa
->used
= origin_ds
->ds_phys
->ds_referenced_bytes
;
1923 ddpa
->comp
= origin_ds
->ds_phys
->ds_compressed_bytes
;
1924 ddpa
->uncomp
= origin_ds
->ds_phys
->ds_uncompressed_bytes
;
1925 for (snap
= list_head(&ddpa
->shared_snaps
); snap
;
1926 snap
= list_next(&ddpa
->shared_snaps
, snap
)) {
1927 uint64_t val
, dlused
, dlcomp
, dluncomp
;
1928 dsl_dataset_t
*ds
= snap
->ds
;
1931 * If there are long holds, we won't be able to evict
1934 if (dsl_dataset_long_held(ds
)) {
1935 err
= SET_ERROR(EBUSY
);
1939 /* Check that the snapshot name does not conflict */
1940 VERIFY0(dsl_dataset_get_snapname(ds
));
1941 err
= dsl_dataset_snap_lookup(hds
, ds
->ds_snapname
, &val
);
1943 (void) strcpy(ddpa
->err_ds
, snap
->ds
->ds_snapname
);
1944 err
= SET_ERROR(EEXIST
);
1950 /* The very first snapshot does not have a deadlist */
1951 if (ds
->ds_phys
->ds_prev_snap_obj
== 0)
1954 dsl_deadlist_space(&ds
->ds_deadlist
,
1955 &dlused
, &dlcomp
, &dluncomp
);
1956 ddpa
->used
+= dlused
;
1957 ddpa
->comp
+= dlcomp
;
1958 ddpa
->uncomp
+= dluncomp
;
1962 * If we are a clone of a clone then we never reached ORIGIN,
1963 * so we need to subtract out the clone origin's used space.
1965 if (ddpa
->origin_origin
) {
1966 ddpa
->used
-= ddpa
->origin_origin
->ds_phys
->ds_referenced_bytes
;
1967 ddpa
->comp
-= ddpa
->origin_origin
->ds_phys
->ds_compressed_bytes
;
1969 ddpa
->origin_origin
->ds_phys
->ds_uncompressed_bytes
;
1972 /* Check that there is enough space here */
1973 err
= dsl_dir_transfer_possible(origin_ds
->ds_dir
, hds
->ds_dir
,
1979 * Compute the amounts of space that will be used by snapshots
1980 * after the promotion (for both origin and clone). For each,
1981 * it is the amount of space that will be on all of their
1982 * deadlists (that was not born before their new origin).
1984 if (hds
->ds_dir
->dd_phys
->dd_flags
& DD_FLAG_USED_BREAKDOWN
) {
1988 * Note, typically this will not be a clone of a clone,
1989 * so dd_origin_txg will be < TXG_INITIAL, so
1990 * these snaplist_space() -> dsl_deadlist_space_range()
1991 * calls will be fast because they do not have to
1992 * iterate over all bps.
1994 snap
= list_head(&ddpa
->origin_snaps
);
1995 err
= snaplist_space(&ddpa
->shared_snaps
,
1996 snap
->ds
->ds_dir
->dd_origin_txg
, &ddpa
->cloneusedsnap
);
2000 err
= snaplist_space(&ddpa
->clone_snaps
,
2001 snap
->ds
->ds_dir
->dd_origin_txg
, &space
);
2004 ddpa
->cloneusedsnap
+= space
;
2006 if (origin_ds
->ds_dir
->dd_phys
->dd_flags
& DD_FLAG_USED_BREAKDOWN
) {
2007 err
= snaplist_space(&ddpa
->origin_snaps
,
2008 origin_ds
->ds_phys
->ds_creation_txg
, &ddpa
->originusedsnap
);
2014 promote_rele(ddpa
, FTAG
);
2019 dsl_dataset_promote_sync(void *arg
, dmu_tx_t
*tx
)
2021 dsl_dataset_promote_arg_t
*ddpa
= arg
;
2022 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2024 struct promotenode
*snap
;
2025 dsl_dataset_t
*origin_ds
;
2026 dsl_dataset_t
*origin_head
;
2028 dsl_dir_t
*odd
= NULL
;
2029 uint64_t oldnext_obj
;
2032 VERIFY0(promote_hold(ddpa
, dp
, FTAG
));
2033 hds
= ddpa
->ddpa_clone
;
2035 ASSERT0(hds
->ds_phys
->ds_flags
& DS_FLAG_NOPROMOTE
);
2037 snap
= list_head(&ddpa
->shared_snaps
);
2038 origin_ds
= snap
->ds
;
2041 snap
= list_head(&ddpa
->origin_snaps
);
2042 origin_head
= snap
->ds
;
2045 * We need to explicitly open odd, since origin_ds's dd will be
2048 VERIFY0(dsl_dir_hold_obj(dp
, origin_ds
->ds_dir
->dd_object
,
2051 /* change origin's next snap */
2052 dmu_buf_will_dirty(origin_ds
->ds_dbuf
, tx
);
2053 oldnext_obj
= origin_ds
->ds_phys
->ds_next_snap_obj
;
2054 snap
= list_tail(&ddpa
->clone_snaps
);
2055 ASSERT3U(snap
->ds
->ds_phys
->ds_prev_snap_obj
, ==, origin_ds
->ds_object
);
2056 origin_ds
->ds_phys
->ds_next_snap_obj
= snap
->ds
->ds_object
;
2058 /* change the origin's next clone */
2059 if (origin_ds
->ds_phys
->ds_next_clones_obj
) {
2060 dsl_dataset_remove_from_next_clones(origin_ds
,
2061 snap
->ds
->ds_object
, tx
);
2062 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2063 origin_ds
->ds_phys
->ds_next_clones_obj
,
2068 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
2069 ASSERT3U(dd
->dd_phys
->dd_origin_obj
, ==, origin_ds
->ds_object
);
2070 dd
->dd_phys
->dd_origin_obj
= odd
->dd_phys
->dd_origin_obj
;
2071 dd
->dd_origin_txg
= origin_head
->ds_dir
->dd_origin_txg
;
2072 dmu_buf_will_dirty(odd
->dd_dbuf
, tx
);
2073 odd
->dd_phys
->dd_origin_obj
= origin_ds
->ds_object
;
2074 origin_head
->ds_dir
->dd_origin_txg
=
2075 origin_ds
->ds_phys
->ds_creation_txg
;
2077 /* change dd_clone entries */
2078 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
2079 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
2080 odd
->dd_phys
->dd_clones
, hds
->ds_object
, tx
));
2081 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2082 ddpa
->origin_origin
->ds_dir
->dd_phys
->dd_clones
,
2083 hds
->ds_object
, tx
));
2085 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
2086 ddpa
->origin_origin
->ds_dir
->dd_phys
->dd_clones
,
2087 origin_head
->ds_object
, tx
));
2088 if (dd
->dd_phys
->dd_clones
== 0) {
2089 dd
->dd_phys
->dd_clones
= zap_create(dp
->dp_meta_objset
,
2090 DMU_OT_DSL_CLONES
, DMU_OT_NONE
, 0, tx
);
2092 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2093 dd
->dd_phys
->dd_clones
, origin_head
->ds_object
, tx
));
2096 /* move snapshots to this dir */
2097 for (snap
= list_head(&ddpa
->shared_snaps
); snap
;
2098 snap
= list_next(&ddpa
->shared_snaps
, snap
)) {
2099 dsl_dataset_t
*ds
= snap
->ds
;
2102 * Property callbacks are registered to a particular
2103 * dsl_dir. Since ours is changing, evict the objset
2104 * so that they will be unregistered from the old dsl_dir.
2106 if (ds
->ds_objset
) {
2107 dmu_objset_evict(ds
->ds_objset
);
2108 ds
->ds_objset
= NULL
;
2111 /* move snap name entry */
2112 VERIFY0(dsl_dataset_get_snapname(ds
));
2113 VERIFY0(dsl_dataset_snap_remove(origin_head
,
2114 ds
->ds_snapname
, tx
));
2115 VERIFY0(zap_add(dp
->dp_meta_objset
,
2116 hds
->ds_phys
->ds_snapnames_zapobj
, ds
->ds_snapname
,
2117 8, 1, &ds
->ds_object
, tx
));
2119 /* change containing dsl_dir */
2120 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2121 ASSERT3U(ds
->ds_phys
->ds_dir_obj
, ==, odd
->dd_object
);
2122 ds
->ds_phys
->ds_dir_obj
= dd
->dd_object
;
2123 ASSERT3P(ds
->ds_dir
, ==, odd
);
2124 dsl_dir_rele(ds
->ds_dir
, ds
);
2125 VERIFY0(dsl_dir_hold_obj(dp
, dd
->dd_object
,
2126 NULL
, ds
, &ds
->ds_dir
));
2128 /* move any clone references */
2129 if (ds
->ds_phys
->ds_next_clones_obj
&&
2130 spa_version(dp
->dp_spa
) >= SPA_VERSION_DIR_CLONES
) {
2134 for (zap_cursor_init(&zc
, dp
->dp_meta_objset
,
2135 ds
->ds_phys
->ds_next_clones_obj
);
2136 zap_cursor_retrieve(&zc
, &za
) == 0;
2137 zap_cursor_advance(&zc
)) {
2138 dsl_dataset_t
*cnds
;
2141 if (za
.za_first_integer
== oldnext_obj
) {
2143 * We've already moved the
2144 * origin's reference.
2149 VERIFY0(dsl_dataset_hold_obj(dp
,
2150 za
.za_first_integer
, FTAG
, &cnds
));
2151 o
= cnds
->ds_dir
->dd_phys
->dd_head_dataset_obj
;
2153 VERIFY0(zap_remove_int(dp
->dp_meta_objset
,
2154 odd
->dd_phys
->dd_clones
, o
, tx
));
2155 VERIFY0(zap_add_int(dp
->dp_meta_objset
,
2156 dd
->dd_phys
->dd_clones
, o
, tx
));
2157 dsl_dataset_rele(cnds
, FTAG
);
2159 zap_cursor_fini(&zc
);
2162 ASSERT(!dsl_prop_hascb(ds
));
2166 * Change space accounting.
2167 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2168 * both be valid, or both be 0 (resulting in delta == 0). This
2169 * is true for each of {clone,origin} independently.
2172 delta
= ddpa
->cloneusedsnap
-
2173 dd
->dd_phys
->dd_used_breakdown
[DD_USED_SNAP
];
2174 ASSERT3S(delta
, >=, 0);
2175 ASSERT3U(ddpa
->used
, >=, delta
);
2176 dsl_dir_diduse_space(dd
, DD_USED_SNAP
, delta
, 0, 0, tx
);
2177 dsl_dir_diduse_space(dd
, DD_USED_HEAD
,
2178 ddpa
->used
- delta
, ddpa
->comp
, ddpa
->uncomp
, tx
);
2180 delta
= ddpa
->originusedsnap
-
2181 odd
->dd_phys
->dd_used_breakdown
[DD_USED_SNAP
];
2182 ASSERT3S(delta
, <=, 0);
2183 ASSERT3U(ddpa
->used
, >=, -delta
);
2184 dsl_dir_diduse_space(odd
, DD_USED_SNAP
, delta
, 0, 0, tx
);
2185 dsl_dir_diduse_space(odd
, DD_USED_HEAD
,
2186 -ddpa
->used
- delta
, -ddpa
->comp
, -ddpa
->uncomp
, tx
);
2188 origin_ds
->ds_phys
->ds_unique_bytes
= ddpa
->unique
;
2190 /* log history record */
2191 spa_history_log_internal_ds(hds
, "promote", tx
, "");
2193 dsl_dir_rele(odd
, FTAG
);
2194 promote_rele(ddpa
, FTAG
);
2198 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2199 * (exclusive) and last_obj (inclusive). The list will be in reverse
2200 * order (last_obj will be the list_head()). If first_obj == 0, do all
2201 * snapshots back to this dataset's origin.
2204 snaplist_make(dsl_pool_t
*dp
,
2205 uint64_t first_obj
, uint64_t last_obj
, list_t
*l
, void *tag
)
2207 uint64_t obj
= last_obj
;
2209 list_create(l
, sizeof (struct promotenode
),
2210 offsetof(struct promotenode
, link
));
2212 while (obj
!= first_obj
) {
2214 struct promotenode
*snap
;
2217 err
= dsl_dataset_hold_obj(dp
, obj
, tag
, &ds
);
2218 ASSERT(err
!= ENOENT
);
2223 first_obj
= ds
->ds_dir
->dd_phys
->dd_origin_obj
;
2225 snap
= kmem_alloc(sizeof (*snap
), KM_PUSHPAGE
);
2227 list_insert_tail(l
, snap
);
2228 obj
= ds
->ds_phys
->ds_prev_snap_obj
;
2235 snaplist_space(list_t
*l
, uint64_t mintxg
, uint64_t *spacep
)
2237 struct promotenode
*snap
;
2240 for (snap
= list_head(l
); snap
; snap
= list_next(l
, snap
)) {
2241 uint64_t used
, comp
, uncomp
;
2242 dsl_deadlist_space_range(&snap
->ds
->ds_deadlist
,
2243 mintxg
, UINT64_MAX
, &used
, &comp
, &uncomp
);
2250 snaplist_destroy(list_t
*l
, void *tag
)
2252 struct promotenode
*snap
;
2254 if (l
== NULL
|| !list_link_active(&l
->list_head
))
2257 while ((snap
= list_tail(l
)) != NULL
) {
2258 list_remove(l
, snap
);
2259 dsl_dataset_rele(snap
->ds
, tag
);
2260 kmem_free(snap
, sizeof (*snap
));
2266 promote_hold(dsl_dataset_promote_arg_t
*ddpa
, dsl_pool_t
*dp
, void *tag
)
2270 struct promotenode
*snap
;
2272 error
= dsl_dataset_hold(dp
, ddpa
->ddpa_clonename
, tag
,
2276 dd
= ddpa
->ddpa_clone
->ds_dir
;
2278 if (dsl_dataset_is_snapshot(ddpa
->ddpa_clone
) ||
2279 !dsl_dir_is_clone(dd
)) {
2280 dsl_dataset_rele(ddpa
->ddpa_clone
, tag
);
2281 return (SET_ERROR(EINVAL
));
2284 error
= snaplist_make(dp
, 0, dd
->dd_phys
->dd_origin_obj
,
2285 &ddpa
->shared_snaps
, tag
);
2289 error
= snaplist_make(dp
, 0, ddpa
->ddpa_clone
->ds_object
,
2290 &ddpa
->clone_snaps
, tag
);
2294 snap
= list_head(&ddpa
->shared_snaps
);
2295 ASSERT3U(snap
->ds
->ds_object
, ==, dd
->dd_phys
->dd_origin_obj
);
2296 error
= snaplist_make(dp
, dd
->dd_phys
->dd_origin_obj
,
2297 snap
->ds
->ds_dir
->dd_phys
->dd_head_dataset_obj
,
2298 &ddpa
->origin_snaps
, tag
);
2302 if (snap
->ds
->ds_dir
->dd_phys
->dd_origin_obj
!= 0) {
2303 error
= dsl_dataset_hold_obj(dp
,
2304 snap
->ds
->ds_dir
->dd_phys
->dd_origin_obj
,
2305 tag
, &ddpa
->origin_origin
);
2311 promote_rele(ddpa
, tag
);
2316 promote_rele(dsl_dataset_promote_arg_t
*ddpa
, void *tag
)
2318 snaplist_destroy(&ddpa
->shared_snaps
, tag
);
2319 snaplist_destroy(&ddpa
->clone_snaps
, tag
);
2320 snaplist_destroy(&ddpa
->origin_snaps
, tag
);
2321 if (ddpa
->origin_origin
!= NULL
)
2322 dsl_dataset_rele(ddpa
->origin_origin
, tag
);
2323 dsl_dataset_rele(ddpa
->ddpa_clone
, tag
);
2329 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2330 * in with the name. (It must be at least MAXNAMELEN bytes long.)
2333 dsl_dataset_promote(const char *name
, char *conflsnap
)
2335 dsl_dataset_promote_arg_t ddpa
= { 0 };
2341 * We will modify space proportional to the number of
2342 * snapshots. Compute numsnaps.
2344 error
= dmu_objset_hold(name
, FTAG
, &os
);
2347 error
= zap_count(dmu_objset_pool(os
)->dp_meta_objset
,
2348 dmu_objset_ds(os
)->ds_phys
->ds_snapnames_zapobj
, &numsnaps
);
2349 dmu_objset_rele(os
, FTAG
);
2353 ddpa
.ddpa_clonename
= name
;
2354 ddpa
.err_ds
= conflsnap
;
2356 return (dsl_sync_task(name
, dsl_dataset_promote_check
,
2357 dsl_dataset_promote_sync
, &ddpa
, 2 + numsnaps
));
2361 dsl_dataset_clone_swap_check_impl(dsl_dataset_t
*clone
,
2362 dsl_dataset_t
*origin_head
, boolean_t force
, void *owner
, dmu_tx_t
*tx
)
2364 int64_t unused_refres_delta
;
2366 /* they should both be heads */
2367 if (dsl_dataset_is_snapshot(clone
) ||
2368 dsl_dataset_is_snapshot(origin_head
))
2369 return (SET_ERROR(EINVAL
));
2371 /* if we are not forcing, the branch point should be just before them */
2372 if (!force
&& clone
->ds_prev
!= origin_head
->ds_prev
)
2373 return (SET_ERROR(EINVAL
));
2375 /* clone should be the clone (unless they are unrelated) */
2376 if (clone
->ds_prev
!= NULL
&&
2377 clone
->ds_prev
!= clone
->ds_dir
->dd_pool
->dp_origin_snap
&&
2378 origin_head
->ds_dir
!= clone
->ds_prev
->ds_dir
)
2379 return (SET_ERROR(EINVAL
));
2381 /* the clone should be a child of the origin */
2382 if (clone
->ds_dir
->dd_parent
!= origin_head
->ds_dir
)
2383 return (SET_ERROR(EINVAL
));
2385 /* origin_head shouldn't be modified unless 'force' */
2387 dsl_dataset_modified_since_snap(origin_head
, origin_head
->ds_prev
))
2388 return (SET_ERROR(ETXTBSY
));
2390 /* origin_head should have no long holds (e.g. is not mounted) */
2391 if (dsl_dataset_handoff_check(origin_head
, owner
, tx
))
2392 return (SET_ERROR(EBUSY
));
2394 /* check amount of any unconsumed refreservation */
2395 unused_refres_delta
=
2396 (int64_t)MIN(origin_head
->ds_reserved
,
2397 origin_head
->ds_phys
->ds_unique_bytes
) -
2398 (int64_t)MIN(origin_head
->ds_reserved
,
2399 clone
->ds_phys
->ds_unique_bytes
);
2401 if (unused_refres_delta
> 0 &&
2402 unused_refres_delta
>
2403 dsl_dir_space_available(origin_head
->ds_dir
, NULL
, 0, TRUE
))
2404 return (SET_ERROR(ENOSPC
));
2406 /* clone can't be over the head's refquota */
2407 if (origin_head
->ds_quota
!= 0 &&
2408 clone
->ds_phys
->ds_referenced_bytes
> origin_head
->ds_quota
)
2409 return (SET_ERROR(EDQUOT
));
2415 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t
*clone
,
2416 dsl_dataset_t
*origin_head
, dmu_tx_t
*tx
)
2418 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2419 int64_t unused_refres_delta
;
2421 ASSERT(clone
->ds_reserved
== 0);
2422 ASSERT(origin_head
->ds_quota
== 0 ||
2423 clone
->ds_phys
->ds_unique_bytes
<= origin_head
->ds_quota
);
2424 ASSERT3P(clone
->ds_prev
, ==, origin_head
->ds_prev
);
2426 dmu_buf_will_dirty(clone
->ds_dbuf
, tx
);
2427 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
2429 if (clone
->ds_objset
!= NULL
) {
2430 dmu_objset_evict(clone
->ds_objset
);
2431 clone
->ds_objset
= NULL
;
2434 if (origin_head
->ds_objset
!= NULL
) {
2435 dmu_objset_evict(origin_head
->ds_objset
);
2436 origin_head
->ds_objset
= NULL
;
2439 unused_refres_delta
=
2440 (int64_t)MIN(origin_head
->ds_reserved
,
2441 origin_head
->ds_phys
->ds_unique_bytes
) -
2442 (int64_t)MIN(origin_head
->ds_reserved
,
2443 clone
->ds_phys
->ds_unique_bytes
);
2446 * Reset origin's unique bytes, if it exists.
2448 if (clone
->ds_prev
) {
2449 dsl_dataset_t
*origin
= clone
->ds_prev
;
2450 uint64_t comp
, uncomp
;
2452 dmu_buf_will_dirty(origin
->ds_dbuf
, tx
);
2453 dsl_deadlist_space_range(&clone
->ds_deadlist
,
2454 origin
->ds_phys
->ds_prev_snap_txg
, UINT64_MAX
,
2455 &origin
->ds_phys
->ds_unique_bytes
, &comp
, &uncomp
);
2461 tmp
= origin_head
->ds_phys
->ds_bp
;
2462 origin_head
->ds_phys
->ds_bp
= clone
->ds_phys
->ds_bp
;
2463 clone
->ds_phys
->ds_bp
= tmp
;
2466 /* set dd_*_bytes */
2468 int64_t dused
, dcomp
, duncomp
;
2469 uint64_t cdl_used
, cdl_comp
, cdl_uncomp
;
2470 uint64_t odl_used
, odl_comp
, odl_uncomp
;
2472 ASSERT3U(clone
->ds_dir
->dd_phys
->
2473 dd_used_breakdown
[DD_USED_SNAP
], ==, 0);
2475 dsl_deadlist_space(&clone
->ds_deadlist
,
2476 &cdl_used
, &cdl_comp
, &cdl_uncomp
);
2477 dsl_deadlist_space(&origin_head
->ds_deadlist
,
2478 &odl_used
, &odl_comp
, &odl_uncomp
);
2480 dused
= clone
->ds_phys
->ds_referenced_bytes
+ cdl_used
-
2481 (origin_head
->ds_phys
->ds_referenced_bytes
+ odl_used
);
2482 dcomp
= clone
->ds_phys
->ds_compressed_bytes
+ cdl_comp
-
2483 (origin_head
->ds_phys
->ds_compressed_bytes
+ odl_comp
);
2484 duncomp
= clone
->ds_phys
->ds_uncompressed_bytes
+
2486 (origin_head
->ds_phys
->ds_uncompressed_bytes
+ odl_uncomp
);
2488 dsl_dir_diduse_space(origin_head
->ds_dir
, DD_USED_HEAD
,
2489 dused
, dcomp
, duncomp
, tx
);
2490 dsl_dir_diduse_space(clone
->ds_dir
, DD_USED_HEAD
,
2491 -dused
, -dcomp
, -duncomp
, tx
);
2494 * The difference in the space used by snapshots is the
2495 * difference in snapshot space due to the head's
2496 * deadlist (since that's the only thing that's
2497 * changing that affects the snapused).
2499 dsl_deadlist_space_range(&clone
->ds_deadlist
,
2500 origin_head
->ds_dir
->dd_origin_txg
, UINT64_MAX
,
2501 &cdl_used
, &cdl_comp
, &cdl_uncomp
);
2502 dsl_deadlist_space_range(&origin_head
->ds_deadlist
,
2503 origin_head
->ds_dir
->dd_origin_txg
, UINT64_MAX
,
2504 &odl_used
, &odl_comp
, &odl_uncomp
);
2505 dsl_dir_transfer_space(origin_head
->ds_dir
, cdl_used
- odl_used
,
2506 DD_USED_HEAD
, DD_USED_SNAP
, tx
);
2509 /* swap ds_*_bytes */
2510 SWITCH64(origin_head
->ds_phys
->ds_referenced_bytes
,
2511 clone
->ds_phys
->ds_referenced_bytes
);
2512 SWITCH64(origin_head
->ds_phys
->ds_compressed_bytes
,
2513 clone
->ds_phys
->ds_compressed_bytes
);
2514 SWITCH64(origin_head
->ds_phys
->ds_uncompressed_bytes
,
2515 clone
->ds_phys
->ds_uncompressed_bytes
);
2516 SWITCH64(origin_head
->ds_phys
->ds_unique_bytes
,
2517 clone
->ds_phys
->ds_unique_bytes
);
2519 /* apply any parent delta for change in unconsumed refreservation */
2520 dsl_dir_diduse_space(origin_head
->ds_dir
, DD_USED_REFRSRV
,
2521 unused_refres_delta
, 0, 0, tx
);
2526 dsl_deadlist_close(&clone
->ds_deadlist
);
2527 dsl_deadlist_close(&origin_head
->ds_deadlist
);
2528 SWITCH64(origin_head
->ds_phys
->ds_deadlist_obj
,
2529 clone
->ds_phys
->ds_deadlist_obj
);
2530 dsl_deadlist_open(&clone
->ds_deadlist
, dp
->dp_meta_objset
,
2531 clone
->ds_phys
->ds_deadlist_obj
);
2532 dsl_deadlist_open(&origin_head
->ds_deadlist
, dp
->dp_meta_objset
,
2533 origin_head
->ds_phys
->ds_deadlist_obj
);
2535 dsl_scan_ds_clone_swapped(origin_head
, clone
, tx
);
2537 spa_history_log_internal_ds(clone
, "clone swap", tx
,
2538 "parent=%s", origin_head
->ds_dir
->dd_myname
);
2542 * Given a pool name and a dataset object number in that pool,
2543 * return the name of that dataset.
2546 dsl_dsobj_to_dsname(char *pname
, uint64_t obj
, char *buf
)
2552 error
= dsl_pool_hold(pname
, FTAG
, &dp
);
2556 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
, &ds
);
2558 dsl_dataset_name(ds
, buf
);
2559 dsl_dataset_rele(ds
, FTAG
);
2561 dsl_pool_rele(dp
, FTAG
);
2567 dsl_dataset_check_quota(dsl_dataset_t
*ds
, boolean_t check_quota
,
2568 uint64_t asize
, uint64_t inflight
, uint64_t *used
, uint64_t *ref_rsrv
)
2572 ASSERT3S(asize
, >, 0);
2575 * *ref_rsrv is the portion of asize that will come from any
2576 * unconsumed refreservation space.
2580 mutex_enter(&ds
->ds_lock
);
2582 * Make a space adjustment for reserved bytes.
2584 if (ds
->ds_reserved
> ds
->ds_phys
->ds_unique_bytes
) {
2586 ds
->ds_reserved
- ds
->ds_phys
->ds_unique_bytes
);
2587 *used
-= (ds
->ds_reserved
- ds
->ds_phys
->ds_unique_bytes
);
2589 asize
- MIN(asize
, parent_delta(ds
, asize
+ inflight
));
2592 if (!check_quota
|| ds
->ds_quota
== 0) {
2593 mutex_exit(&ds
->ds_lock
);
2597 * If they are requesting more space, and our current estimate
2598 * is over quota, they get to try again unless the actual
2599 * on-disk is over quota and there are no pending changes (which
2600 * may free up space for us).
2602 if (ds
->ds_phys
->ds_referenced_bytes
+ inflight
>= ds
->ds_quota
) {
2604 ds
->ds_phys
->ds_referenced_bytes
< ds
->ds_quota
)
2605 error
= SET_ERROR(ERESTART
);
2607 error
= SET_ERROR(EDQUOT
);
2609 mutex_exit(&ds
->ds_lock
);
2614 typedef struct dsl_dataset_set_qr_arg
{
2615 const char *ddsqra_name
;
2616 zprop_source_t ddsqra_source
;
2617 uint64_t ddsqra_value
;
2618 } dsl_dataset_set_qr_arg_t
;
2623 dsl_dataset_set_refquota_check(void *arg
, dmu_tx_t
*tx
)
2625 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2626 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2631 if (spa_version(dp
->dp_spa
) < SPA_VERSION_REFQUOTA
)
2632 return (SET_ERROR(ENOTSUP
));
2634 error
= dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
);
2638 if (dsl_dataset_is_snapshot(ds
)) {
2639 dsl_dataset_rele(ds
, FTAG
);
2640 return (SET_ERROR(EINVAL
));
2643 error
= dsl_prop_predict(ds
->ds_dir
,
2644 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
2645 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, &newval
);
2647 dsl_dataset_rele(ds
, FTAG
);
2652 dsl_dataset_rele(ds
, FTAG
);
2656 if (newval
< ds
->ds_phys
->ds_referenced_bytes
||
2657 newval
< ds
->ds_reserved
) {
2658 dsl_dataset_rele(ds
, FTAG
);
2659 return (SET_ERROR(ENOSPC
));
2662 dsl_dataset_rele(ds
, FTAG
);
2667 dsl_dataset_set_refquota_sync(void *arg
, dmu_tx_t
*tx
)
2669 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2670 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2674 VERIFY0(dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
));
2676 dsl_prop_set_sync_impl(ds
,
2677 zfs_prop_to_name(ZFS_PROP_REFQUOTA
),
2678 ddsqra
->ddsqra_source
, sizeof (ddsqra
->ddsqra_value
), 1,
2679 &ddsqra
->ddsqra_value
, tx
);
2681 VERIFY0(dsl_prop_get_int_ds(ds
,
2682 zfs_prop_to_name(ZFS_PROP_REFQUOTA
), &newval
));
2684 if (ds
->ds_quota
!= newval
) {
2685 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2686 ds
->ds_quota
= newval
;
2688 dsl_dataset_rele(ds
, FTAG
);
2692 dsl_dataset_set_refquota(const char *dsname
, zprop_source_t source
,
2695 dsl_dataset_set_qr_arg_t ddsqra
;
2697 ddsqra
.ddsqra_name
= dsname
;
2698 ddsqra
.ddsqra_source
= source
;
2699 ddsqra
.ddsqra_value
= refquota
;
2701 return (dsl_sync_task(dsname
, dsl_dataset_set_refquota_check
,
2702 dsl_dataset_set_refquota_sync
, &ddsqra
, 0));
2706 dsl_dataset_set_refreservation_check(void *arg
, dmu_tx_t
*tx
)
2708 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2709 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2712 uint64_t newval
, unique
;
2714 if (spa_version(dp
->dp_spa
) < SPA_VERSION_REFRESERVATION
)
2715 return (SET_ERROR(ENOTSUP
));
2717 error
= dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
);
2721 if (dsl_dataset_is_snapshot(ds
)) {
2722 dsl_dataset_rele(ds
, FTAG
);
2723 return (SET_ERROR(EINVAL
));
2726 error
= dsl_prop_predict(ds
->ds_dir
,
2727 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
2728 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, &newval
);
2730 dsl_dataset_rele(ds
, FTAG
);
2735 * If we are doing the preliminary check in open context, the
2736 * space estimates may be inaccurate.
2738 if (!dmu_tx_is_syncing(tx
)) {
2739 dsl_dataset_rele(ds
, FTAG
);
2743 mutex_enter(&ds
->ds_lock
);
2744 if (!DS_UNIQUE_IS_ACCURATE(ds
))
2745 dsl_dataset_recalc_head_uniq(ds
);
2746 unique
= ds
->ds_phys
->ds_unique_bytes
;
2747 mutex_exit(&ds
->ds_lock
);
2749 if (MAX(unique
, newval
) > MAX(unique
, ds
->ds_reserved
)) {
2750 uint64_t delta
= MAX(unique
, newval
) -
2751 MAX(unique
, ds
->ds_reserved
);
2754 dsl_dir_space_available(ds
->ds_dir
, NULL
, 0, B_TRUE
) ||
2755 (ds
->ds_quota
> 0 && newval
> ds
->ds_quota
)) {
2756 dsl_dataset_rele(ds
, FTAG
);
2757 return (SET_ERROR(ENOSPC
));
2761 dsl_dataset_rele(ds
, FTAG
);
2766 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t
*ds
,
2767 zprop_source_t source
, uint64_t value
, dmu_tx_t
*tx
)
2773 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_REFRESERVATION
),
2774 source
, sizeof (value
), 1, &value
, tx
);
2776 VERIFY0(dsl_prop_get_int_ds(ds
,
2777 zfs_prop_to_name(ZFS_PROP_REFRESERVATION
), &newval
));
2779 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2780 mutex_enter(&ds
->ds_dir
->dd_lock
);
2781 mutex_enter(&ds
->ds_lock
);
2782 ASSERT(DS_UNIQUE_IS_ACCURATE(ds
));
2783 unique
= ds
->ds_phys
->ds_unique_bytes
;
2784 delta
= MAX(0, (int64_t)(newval
- unique
)) -
2785 MAX(0, (int64_t)(ds
->ds_reserved
- unique
));
2786 ds
->ds_reserved
= newval
;
2787 mutex_exit(&ds
->ds_lock
);
2789 dsl_dir_diduse_space(ds
->ds_dir
, DD_USED_REFRSRV
, delta
, 0, 0, tx
);
2790 mutex_exit(&ds
->ds_dir
->dd_lock
);
2794 dsl_dataset_set_refreservation_sync(void *arg
, dmu_tx_t
*tx
)
2796 dsl_dataset_set_qr_arg_t
*ddsqra
= arg
;
2797 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2800 VERIFY0(dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
));
2801 dsl_dataset_set_refreservation_sync_impl(ds
,
2802 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, tx
);
2803 dsl_dataset_rele(ds
, FTAG
);
2807 dsl_dataset_set_refreservation(const char *dsname
, zprop_source_t source
,
2808 uint64_t refreservation
)
2810 dsl_dataset_set_qr_arg_t ddsqra
;
2812 ddsqra
.ddsqra_name
= dsname
;
2813 ddsqra
.ddsqra_source
= source
;
2814 ddsqra
.ddsqra_value
= refreservation
;
2816 return (dsl_sync_task(dsname
, dsl_dataset_set_refreservation_check
,
2817 dsl_dataset_set_refreservation_sync
, &ddsqra
, 0));
2821 * Return (in *usedp) the amount of space written in new that is not
2822 * present in oldsnap. New may be a snapshot or the head. Old must be
2823 * a snapshot before new, in new's filesystem (or its origin). If not then
2824 * fail and return EINVAL.
2826 * The written space is calculated by considering two components: First, we
2827 * ignore any freed space, and calculate the written as new's used space
2828 * minus old's used space. Next, we add in the amount of space that was freed
2829 * between the two snapshots, thus reducing new's used space relative to old's.
2830 * Specifically, this is the space that was born before old->ds_creation_txg,
2831 * and freed before new (ie. on new's deadlist or a previous deadlist).
2833 * space freed [---------------------]
2834 * snapshots ---O-------O--------O-------O------
2838 dsl_dataset_space_written(dsl_dataset_t
*oldsnap
, dsl_dataset_t
*new,
2839 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
2843 dsl_pool_t
*dp
= new->ds_dir
->dd_pool
;
2845 ASSERT(dsl_pool_config_held(dp
));
2848 *usedp
+= new->ds_phys
->ds_referenced_bytes
;
2849 *usedp
-= oldsnap
->ds_phys
->ds_referenced_bytes
;
2852 *compp
+= new->ds_phys
->ds_compressed_bytes
;
2853 *compp
-= oldsnap
->ds_phys
->ds_compressed_bytes
;
2856 *uncompp
+= new->ds_phys
->ds_uncompressed_bytes
;
2857 *uncompp
-= oldsnap
->ds_phys
->ds_uncompressed_bytes
;
2859 snapobj
= new->ds_object
;
2860 while (snapobj
!= oldsnap
->ds_object
) {
2861 dsl_dataset_t
*snap
;
2862 uint64_t used
, comp
, uncomp
;
2864 if (snapobj
== new->ds_object
) {
2867 err
= dsl_dataset_hold_obj(dp
, snapobj
, FTAG
, &snap
);
2872 if (snap
->ds_phys
->ds_prev_snap_txg
==
2873 oldsnap
->ds_phys
->ds_creation_txg
) {
2875 * The blocks in the deadlist can not be born after
2876 * ds_prev_snap_txg, so get the whole deadlist space,
2877 * which is more efficient (especially for old-format
2878 * deadlists). Unfortunately the deadlist code
2879 * doesn't have enough information to make this
2880 * optimization itself.
2882 dsl_deadlist_space(&snap
->ds_deadlist
,
2883 &used
, &comp
, &uncomp
);
2885 dsl_deadlist_space_range(&snap
->ds_deadlist
,
2886 0, oldsnap
->ds_phys
->ds_creation_txg
,
2887 &used
, &comp
, &uncomp
);
2894 * If we get to the beginning of the chain of snapshots
2895 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
2896 * was not a snapshot of/before new.
2898 snapobj
= snap
->ds_phys
->ds_prev_snap_obj
;
2900 dsl_dataset_rele(snap
, FTAG
);
2902 err
= SET_ERROR(EINVAL
);
2911 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
2912 * lastsnap, and all snapshots in between are deleted.
2914 * blocks that would be freed [---------------------------]
2915 * snapshots ---O-------O--------O-------O--------O
2916 * firstsnap lastsnap
2918 * This is the set of blocks that were born after the snap before firstsnap,
2919 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
2920 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
2921 * We calculate this by iterating over the relevant deadlists (from the snap
2922 * after lastsnap, backward to the snap after firstsnap), summing up the
2923 * space on the deadlist that was born after the snap before firstsnap.
2926 dsl_dataset_space_wouldfree(dsl_dataset_t
*firstsnap
,
2927 dsl_dataset_t
*lastsnap
,
2928 uint64_t *usedp
, uint64_t *compp
, uint64_t *uncompp
)
2932 dsl_pool_t
*dp
= firstsnap
->ds_dir
->dd_pool
;
2934 ASSERT(dsl_dataset_is_snapshot(firstsnap
));
2935 ASSERT(dsl_dataset_is_snapshot(lastsnap
));
2938 * Check that the snapshots are in the same dsl_dir, and firstsnap
2939 * is before lastsnap.
2941 if (firstsnap
->ds_dir
!= lastsnap
->ds_dir
||
2942 firstsnap
->ds_phys
->ds_creation_txg
>
2943 lastsnap
->ds_phys
->ds_creation_txg
)
2944 return (SET_ERROR(EINVAL
));
2946 *usedp
= *compp
= *uncompp
= 0;
2948 snapobj
= lastsnap
->ds_phys
->ds_next_snap_obj
;
2949 while (snapobj
!= firstsnap
->ds_object
) {
2951 uint64_t used
, comp
, uncomp
;
2953 err
= dsl_dataset_hold_obj(dp
, snapobj
, FTAG
, &ds
);
2957 dsl_deadlist_space_range(&ds
->ds_deadlist
,
2958 firstsnap
->ds_phys
->ds_prev_snap_txg
, UINT64_MAX
,
2959 &used
, &comp
, &uncomp
);
2964 snapobj
= ds
->ds_phys
->ds_prev_snap_obj
;
2965 ASSERT3U(snapobj
, !=, 0);
2966 dsl_dataset_rele(ds
, FTAG
);
2972 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
2973 * For example, they could both be snapshots of the same filesystem, and
2974 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
2975 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
2976 * filesystem. Or 'earlier' could be the origin's origin.
2979 dsl_dataset_is_before(dsl_dataset_t
*later
, dsl_dataset_t
*earlier
)
2981 dsl_pool_t
*dp
= later
->ds_dir
->dd_pool
;
2984 dsl_dataset_t
*origin
;
2986 ASSERT(dsl_pool_config_held(dp
));
2988 if (earlier
->ds_phys
->ds_creation_txg
>=
2989 later
->ds_phys
->ds_creation_txg
)
2992 if (later
->ds_dir
== earlier
->ds_dir
)
2994 if (!dsl_dir_is_clone(later
->ds_dir
))
2997 if (later
->ds_dir
->dd_phys
->dd_origin_obj
== earlier
->ds_object
)
2999 error
= dsl_dataset_hold_obj(dp
,
3000 later
->ds_dir
->dd_phys
->dd_origin_obj
, FTAG
, &origin
);
3003 ret
= dsl_dataset_is_before(origin
, earlier
);
3004 dsl_dataset_rele(origin
, FTAG
);
3010 dsl_dataset_zapify(dsl_dataset_t
*ds
, dmu_tx_t
*tx
)
3012 objset_t
*mos
= ds
->ds_dir
->dd_pool
->dp_meta_objset
;
3013 dmu_object_zapify(mos
, ds
->ds_object
, DMU_OT_DSL_DATASET
, tx
);
3016 #if defined(_KERNEL) && defined(HAVE_SPL)
3017 EXPORT_SYMBOL(dsl_dataset_hold
);
3018 EXPORT_SYMBOL(dsl_dataset_hold_obj
);
3019 EXPORT_SYMBOL(dsl_dataset_own
);
3020 EXPORT_SYMBOL(dsl_dataset_own_obj
);
3021 EXPORT_SYMBOL(dsl_dataset_name
);
3022 EXPORT_SYMBOL(dsl_dataset_rele
);
3023 EXPORT_SYMBOL(dsl_dataset_disown
);
3024 EXPORT_SYMBOL(dsl_dataset_tryown
);
3025 EXPORT_SYMBOL(dsl_dataset_create_sync
);
3026 EXPORT_SYMBOL(dsl_dataset_create_sync_dd
);
3027 EXPORT_SYMBOL(dsl_dataset_snapshot_check
);
3028 EXPORT_SYMBOL(dsl_dataset_snapshot_sync
);
3029 EXPORT_SYMBOL(dsl_dataset_promote
);
3030 EXPORT_SYMBOL(dsl_dataset_user_hold
);
3031 EXPORT_SYMBOL(dsl_dataset_user_release
);
3032 EXPORT_SYMBOL(dsl_dataset_get_holds
);
3033 EXPORT_SYMBOL(dsl_dataset_get_blkptr
);
3034 EXPORT_SYMBOL(dsl_dataset_set_blkptr
);
3035 EXPORT_SYMBOL(dsl_dataset_get_spa
);
3036 EXPORT_SYMBOL(dsl_dataset_modified_since_snap
);
3037 EXPORT_SYMBOL(dsl_dataset_space_written
);
3038 EXPORT_SYMBOL(dsl_dataset_space_wouldfree
);
3039 EXPORT_SYMBOL(dsl_dataset_sync
);
3040 EXPORT_SYMBOL(dsl_dataset_block_born
);
3041 EXPORT_SYMBOL(dsl_dataset_block_kill
);
3042 EXPORT_SYMBOL(dsl_dataset_block_freeable
);
3043 EXPORT_SYMBOL(dsl_dataset_prev_snap_txg
);
3044 EXPORT_SYMBOL(dsl_dataset_dirty
);
3045 EXPORT_SYMBOL(dsl_dataset_stats
);
3046 EXPORT_SYMBOL(dsl_dataset_fast_stat
);
3047 EXPORT_SYMBOL(dsl_dataset_space
);
3048 EXPORT_SYMBOL(dsl_dataset_fsid_guid
);
3049 EXPORT_SYMBOL(dsl_dsobj_to_dsname
);
3050 EXPORT_SYMBOL(dsl_dataset_check_quota
);
3051 EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl
);
3052 EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl
);