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.
27 #include <sys/dmu_objset.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/dsl_dir.h>
30 #include <sys/dsl_prop.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dmu_traverse.h>
33 #include <sys/dmu_impl.h>
34 #include <sys/dmu_tx.h>
38 #include <sys/zfeature.h>
39 #include <sys/unique.h>
40 #include <sys/zfs_context.h>
41 #include <sys/zfs_ioctl.h>
43 #include <sys/zfs_znode.h>
44 #include <sys/zfs_onexit.h>
46 #include <sys/dsl_scan.h>
47 #include <sys/dsl_deadlist.h>
48 #include <sys/dsl_destroy.h>
49 #include <sys/dsl_userhold.h>
51 #define SWITCH64(x, y) \
53 uint64_t __tmp = (x); \
58 #define DS_REF_MAX (1ULL << 62)
60 #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE
63 * Figure out how much of this delta should be propogated to the dsl_dir
64 * layer. If there's a refreservation, that space has already been
65 * partially accounted for in our ancestors.
68 parent_delta(dsl_dataset_t *ds, int64_t delta)
70 uint64_t old_bytes, new_bytes;
72 if (ds->ds_reserved == 0)
75 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
76 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
78 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
79 return (new_bytes - old_bytes);
83 dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
85 int used, compressed, uncompressed;
88 used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
89 compressed = BP_GET_PSIZE(bp);
90 uncompressed = BP_GET_UCSIZE(bp);
92 dprintf_bp(bp, "ds=%p", ds);
94 ASSERT(dmu_tx_is_syncing(tx));
95 /* It could have been compressed away to nothing */
98 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
99 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
101 dsl_pool_mos_diduse_space(tx->tx_pool,
102 used, compressed, uncompressed);
105 dmu_buf_will_dirty(ds->ds_dbuf, tx);
107 mutex_enter(&ds->ds_dir->dd_lock);
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);
119 mutex_exit(&ds->ds_dir->dd_lock);
123 dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
126 int used, compressed, uncompressed;
131 ASSERT(dmu_tx_is_syncing(tx));
132 ASSERT(bp->blk_birth <= tx->tx_txg);
134 used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
135 compressed = BP_GET_PSIZE(bp);
136 uncompressed = BP_GET_UCSIZE(bp);
140 dsl_free(tx->tx_pool, tx->tx_txg, bp);
141 dsl_pool_mos_diduse_space(tx->tx_pool,
142 -used, -compressed, -uncompressed);
145 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
147 ASSERT(!dsl_dataset_is_snapshot(ds));
148 dmu_buf_will_dirty(ds->ds_dbuf, tx);
150 if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
153 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
154 dsl_free(tx->tx_pool, tx->tx_txg, bp);
156 mutex_enter(&ds->ds_dir->dd_lock);
157 mutex_enter(&ds->ds_lock);
158 ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
159 !DS_UNIQUE_IS_ACCURATE(ds));
160 delta = parent_delta(ds, -used);
161 ds->ds_phys->ds_unique_bytes -= used;
162 mutex_exit(&ds->ds_lock);
163 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
164 delta, -compressed, -uncompressed, tx);
165 dsl_dir_transfer_space(ds->ds_dir, -used - delta,
166 DD_USED_REFRSRV, DD_USED_HEAD, tx);
167 mutex_exit(&ds->ds_dir->dd_lock);
169 dprintf_bp(bp, "putting on dead list: %s", "");
172 * We are here as part of zio's write done callback,
173 * which means we're a zio interrupt thread. We can't
174 * call dsl_deadlist_insert() now because it may block
175 * waiting for I/O. Instead, put bp on the deferred
176 * queue and let dsl_pool_sync() finish the job.
178 bplist_append(&ds->ds_pending_deadlist, bp);
180 dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
182 ASSERT3U(ds->ds_prev->ds_object, ==,
183 ds->ds_phys->ds_prev_snap_obj);
184 ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
185 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
186 if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
187 ds->ds_object && bp->blk_birth >
188 ds->ds_prev->ds_phys->ds_prev_snap_txg) {
189 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
190 mutex_enter(&ds->ds_prev->ds_lock);
191 ds->ds_prev->ds_phys->ds_unique_bytes += used;
192 mutex_exit(&ds->ds_prev->ds_lock);
194 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
195 dsl_dir_transfer_space(ds->ds_dir, used,
196 DD_USED_HEAD, DD_USED_SNAP, tx);
199 mutex_enter(&ds->ds_lock);
200 ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used);
201 ds->ds_phys->ds_referenced_bytes -= used;
202 ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
203 ds->ds_phys->ds_compressed_bytes -= compressed;
204 ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
205 ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
206 mutex_exit(&ds->ds_lock);
212 dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
214 uint64_t trysnap = 0;
219 * The snapshot creation could fail, but that would cause an
220 * incorrect FALSE return, which would only result in an
221 * overestimation of the amount of space that an operation would
222 * consume, which is OK.
224 * There's also a small window where we could miss a pending
225 * snapshot, because we could set the sync task in the quiescing
226 * phase. So this should only be used as a guess.
228 if (ds->ds_trysnap_txg >
229 spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
230 trysnap = ds->ds_trysnap_txg;
231 return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
235 dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
238 if (blk_birth <= dsl_dataset_prev_snap_txg(ds))
241 ddt_prefetch(dsl_dataset_get_spa(ds), bp);
248 dsl_dataset_evict(dmu_buf_t *db, void *dsv)
250 dsl_dataset_t *ds = dsv;
252 ASSERT(ds->ds_owner == NULL);
254 unique_remove(ds->ds_fsid_guid);
256 if (ds->ds_objset != NULL)
257 dmu_objset_evict(ds->ds_objset);
260 dsl_dataset_rele(ds->ds_prev, ds);
264 bplist_destroy(&ds->ds_pending_deadlist);
265 if (ds->ds_phys->ds_deadlist_obj != 0)
266 dsl_deadlist_close(&ds->ds_deadlist);
268 dsl_dir_rele(ds->ds_dir, ds);
270 ASSERT(!list_link_active(&ds->ds_synced_link));
272 mutex_destroy(&ds->ds_lock);
273 mutex_destroy(&ds->ds_opening_lock);
274 refcount_destroy(&ds->ds_longholds);
276 kmem_free(ds, sizeof (dsl_dataset_t));
280 dsl_dataset_get_snapname(dsl_dataset_t *ds)
282 dsl_dataset_phys_t *headphys;
285 dsl_pool_t *dp = ds->ds_dir->dd_pool;
286 objset_t *mos = dp->dp_meta_objset;
288 if (ds->ds_snapname[0])
290 if (ds->ds_phys->ds_next_snap_obj == 0)
293 err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
297 headphys = headdbuf->db_data;
298 err = zap_value_search(dp->dp_meta_objset,
299 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
300 dmu_buf_rele(headdbuf, FTAG);
305 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
307 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
308 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
312 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
317 err = zap_lookup_norm(mos, snapobj, name, 8, 1,
318 value, mt, NULL, 0, NULL);
319 if (err == ENOTSUP && mt == MT_FIRST)
320 err = zap_lookup(mos, snapobj, name, 8, 1, value);
325 dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx)
327 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
328 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
332 dsl_dir_snap_cmtime_update(ds->ds_dir);
334 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
339 err = zap_remove_norm(mos, snapobj, name, mt, tx);
340 if (err == ENOTSUP && mt == MT_FIRST)
341 err = zap_remove(mos, snapobj, name, tx);
346 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
349 objset_t *mos = dp->dp_meta_objset;
353 dmu_object_info_t doi;
355 ASSERT(dsl_pool_config_held(dp));
357 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
361 /* Make sure dsobj has the correct object type. */
362 dmu_object_info_from_db(dbuf, &doi);
363 if (doi.doi_type != DMU_OT_DSL_DATASET)
366 ds = dmu_buf_get_user(dbuf);
368 dsl_dataset_t *winner = NULL;
370 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_PUSHPAGE);
372 ds->ds_object = dsobj;
373 ds->ds_phys = dbuf->db_data;
374 list_link_init(&ds->ds_synced_link);
376 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
377 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
378 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
379 refcount_create(&ds->ds_longholds);
381 bplist_create(&ds->ds_pending_deadlist);
382 dsl_deadlist_open(&ds->ds_deadlist,
383 mos, ds->ds_phys->ds_deadlist_obj);
385 list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
386 offsetof(dmu_sendarg_t, dsa_link));
389 err = dsl_dir_hold_obj(dp,
390 ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
393 mutex_destroy(&ds->ds_lock);
394 mutex_destroy(&ds->ds_opening_lock);
395 refcount_destroy(&ds->ds_longholds);
396 bplist_destroy(&ds->ds_pending_deadlist);
397 dsl_deadlist_close(&ds->ds_deadlist);
398 kmem_free(ds, sizeof (dsl_dataset_t));
399 dmu_buf_rele(dbuf, tag);
403 if (!dsl_dataset_is_snapshot(ds)) {
404 ds->ds_snapname[0] = '\0';
405 if (ds->ds_phys->ds_prev_snap_obj != 0) {
406 err = dsl_dataset_hold_obj(dp,
407 ds->ds_phys->ds_prev_snap_obj,
411 if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
412 err = dsl_dataset_get_snapname(ds);
413 if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
415 ds->ds_dir->dd_pool->dp_meta_objset,
416 ds->ds_phys->ds_userrefs_obj,
421 if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
422 err = dsl_prop_get_int_ds(ds,
423 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
426 err = dsl_prop_get_int_ds(ds,
427 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
431 ds->ds_reserved = ds->ds_quota = 0;
434 if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds,
435 &ds->ds_phys, dsl_dataset_evict)) != NULL) {
436 bplist_destroy(&ds->ds_pending_deadlist);
437 dsl_deadlist_close(&ds->ds_deadlist);
439 dsl_dataset_rele(ds->ds_prev, ds);
440 dsl_dir_rele(ds->ds_dir, ds);
441 mutex_destroy(&ds->ds_lock);
442 mutex_destroy(&ds->ds_opening_lock);
443 refcount_destroy(&ds->ds_longholds);
444 kmem_free(ds, sizeof (dsl_dataset_t));
446 dmu_buf_rele(dbuf, tag);
452 unique_insert(ds->ds_phys->ds_fsid_guid);
455 ASSERT3P(ds->ds_dbuf, ==, dbuf);
456 ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
457 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
458 spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
459 dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
465 dsl_dataset_hold(dsl_pool_t *dp, const char *name,
466 void *tag, dsl_dataset_t **dsp)
469 const char *snapname;
473 err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
477 ASSERT(dsl_pool_config_held(dp));
478 obj = dd->dd_phys->dd_head_dataset_obj;
480 err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
484 /* we may be looking for a snapshot */
485 if (err == 0 && snapname != NULL) {
488 if (*snapname++ != '@') {
489 dsl_dataset_rele(*dsp, tag);
490 dsl_dir_rele(dd, FTAG);
494 dprintf("looking for snapshot '%s'\n", snapname);
495 err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
497 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
498 dsl_dataset_rele(*dsp, tag);
501 mutex_enter(&ds->ds_lock);
502 if (ds->ds_snapname[0] == 0)
503 (void) strlcpy(ds->ds_snapname, snapname,
504 sizeof (ds->ds_snapname));
505 mutex_exit(&ds->ds_lock);
510 dsl_dir_rele(dd, FTAG);
515 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
516 void *tag, dsl_dataset_t **dsp)
518 int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
521 if (!dsl_dataset_tryown(*dsp, tag)) {
522 dsl_dataset_rele(*dsp, tag);
530 dsl_dataset_own(dsl_pool_t *dp, const char *name,
531 void *tag, dsl_dataset_t **dsp)
533 int err = dsl_dataset_hold(dp, name, tag, dsp);
536 if (!dsl_dataset_tryown(*dsp, tag)) {
537 dsl_dataset_rele(*dsp, tag);
544 * See the comment above dsl_pool_hold() for details. In summary, a long
545 * hold is used to prevent destruction of a dataset while the pool hold
546 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
548 * The dataset and pool must be held when this function is called. After it
549 * is called, the pool hold may be released while the dataset is still held
553 dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
555 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
556 (void) refcount_add(&ds->ds_longholds, tag);
560 dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
562 (void) refcount_remove(&ds->ds_longholds, tag);
565 /* Return B_TRUE if there are any long holds on this dataset. */
567 dsl_dataset_long_held(dsl_dataset_t *ds)
569 return (!refcount_is_zero(&ds->ds_longholds));
573 dsl_dataset_name(dsl_dataset_t *ds, char *name)
576 (void) strcpy(name, "mos");
578 dsl_dir_name(ds->ds_dir, name);
579 VERIFY0(dsl_dataset_get_snapname(ds));
580 if (ds->ds_snapname[0]) {
581 (void) strcat(name, "@");
583 * We use a "recursive" mutex so that we
584 * can call dprintf_ds() with ds_lock held.
586 if (!MUTEX_HELD(&ds->ds_lock)) {
587 mutex_enter(&ds->ds_lock);
588 (void) strcat(name, ds->ds_snapname);
589 mutex_exit(&ds->ds_lock);
591 (void) strcat(name, ds->ds_snapname);
598 dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
600 dmu_buf_rele(ds->ds_dbuf, tag);
604 dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
606 ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL);
608 mutex_enter(&ds->ds_lock);
610 mutex_exit(&ds->ds_lock);
611 dsl_dataset_long_rele(ds, tag);
612 if (ds->ds_dbuf != NULL)
613 dsl_dataset_rele(ds, tag);
615 dsl_dataset_evict(NULL, ds);
619 dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
621 boolean_t gotit = FALSE;
623 mutex_enter(&ds->ds_lock);
624 if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
626 dsl_dataset_long_hold(ds, tag);
629 mutex_exit(&ds->ds_lock);
634 dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
635 uint64_t flags, dmu_tx_t *tx)
637 dsl_pool_t *dp = dd->dd_pool;
639 dsl_dataset_phys_t *dsphys;
641 objset_t *mos = dp->dp_meta_objset;
644 origin = dp->dp_origin_snap;
646 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
647 ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
648 ASSERT(dmu_tx_is_syncing(tx));
649 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
651 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
652 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
653 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
654 dmu_buf_will_dirty(dbuf, tx);
655 dsphys = dbuf->db_data;
656 bzero(dsphys, sizeof (dsl_dataset_phys_t));
657 dsphys->ds_dir_obj = dd->dd_object;
658 dsphys->ds_flags = flags;
659 dsphys->ds_fsid_guid = unique_create();
660 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
661 sizeof (dsphys->ds_guid));
662 dsphys->ds_snapnames_zapobj =
663 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
665 dsphys->ds_creation_time = gethrestime_sec();
666 dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
668 if (origin == NULL) {
669 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
671 dsl_dataset_t *ohds; /* head of the origin snapshot */
673 dsphys->ds_prev_snap_obj = origin->ds_object;
674 dsphys->ds_prev_snap_txg =
675 origin->ds_phys->ds_creation_txg;
676 dsphys->ds_referenced_bytes =
677 origin->ds_phys->ds_referenced_bytes;
678 dsphys->ds_compressed_bytes =
679 origin->ds_phys->ds_compressed_bytes;
680 dsphys->ds_uncompressed_bytes =
681 origin->ds_phys->ds_uncompressed_bytes;
682 dsphys->ds_bp = origin->ds_phys->ds_bp;
683 dsphys->ds_flags |= origin->ds_phys->ds_flags;
685 dmu_buf_will_dirty(origin->ds_dbuf, tx);
686 origin->ds_phys->ds_num_children++;
688 VERIFY0(dsl_dataset_hold_obj(dp,
689 origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
690 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
691 dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
692 dsl_dataset_rele(ohds, FTAG);
694 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
695 if (origin->ds_phys->ds_next_clones_obj == 0) {
696 origin->ds_phys->ds_next_clones_obj =
698 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
700 VERIFY0(zap_add_int(mos,
701 origin->ds_phys->ds_next_clones_obj, dsobj, tx));
704 dmu_buf_will_dirty(dd->dd_dbuf, tx);
705 dd->dd_phys->dd_origin_obj = origin->ds_object;
706 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
707 if (origin->ds_dir->dd_phys->dd_clones == 0) {
708 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
709 origin->ds_dir->dd_phys->dd_clones =
711 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
713 VERIFY0(zap_add_int(mos,
714 origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
718 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
719 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
721 dmu_buf_rele(dbuf, FTAG);
723 dmu_buf_will_dirty(dd->dd_dbuf, tx);
724 dd->dd_phys->dd_head_dataset_obj = dsobj;
730 dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
734 VERIFY0(dmu_objset_from_ds(ds, &os));
735 bzero(&os->os_zil_header, sizeof (os->os_zil_header));
736 dsl_dataset_dirty(ds, tx);
740 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
741 dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
743 dsl_pool_t *dp = pdd->dd_pool;
744 uint64_t dsobj, ddobj;
747 ASSERT(dmu_tx_is_syncing(tx));
748 ASSERT(lastname[0] != '@');
750 ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
751 VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
753 dsobj = dsl_dataset_create_sync_dd(dd, origin,
754 flags & ~DS_CREATE_FLAG_NODIRTY, tx);
756 dsl_deleg_set_create_perms(dd, tx, cr);
758 dsl_dir_rele(dd, FTAG);
761 * If we are creating a clone, make sure we zero out any stale
762 * data from the origin snapshots zil header.
764 if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
767 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
768 dsl_dataset_zero_zil(ds, tx);
769 dsl_dataset_rele(ds, FTAG);
776 * The unique space in the head dataset can be calculated by subtracting
777 * the space used in the most recent snapshot, that is still being used
778 * in this file system, from the space currently in use. To figure out
779 * the space in the most recent snapshot still in use, we need to take
780 * the total space used in the snapshot and subtract out the space that
781 * has been freed up since the snapshot was taken.
784 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
787 uint64_t dlused, dlcomp, dluncomp;
789 ASSERT(!dsl_dataset_is_snapshot(ds));
791 if (ds->ds_phys->ds_prev_snap_obj != 0)
792 mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
796 dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
798 ASSERT3U(dlused, <=, mrs_used);
799 ds->ds_phys->ds_unique_bytes =
800 ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
802 if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
803 SPA_VERSION_UNIQUE_ACCURATE)
804 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
808 dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
811 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
813 ASSERTV(uint64_t count);
815 ASSERT(ds->ds_phys->ds_num_children >= 2);
816 err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
818 * The err should not be ENOENT, but a bug in a previous version
819 * of the code could cause upgrade_clones_cb() to not set
820 * ds_next_snap_obj when it should, leading to a missing entry.
821 * If we knew that the pool was created after
822 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
823 * ENOENT. However, at least we can check that we don't have
824 * too many entries in the next_clones_obj even after failing to
829 ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
831 ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
836 dsl_dataset_get_blkptr(dsl_dataset_t *ds)
838 return (&ds->ds_phys->ds_bp);
842 dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
844 ASSERT(dmu_tx_is_syncing(tx));
845 /* If it's the meta-objset, set dp_meta_rootbp */
847 tx->tx_pool->dp_meta_rootbp = *bp;
849 dmu_buf_will_dirty(ds->ds_dbuf, tx);
850 ds->ds_phys->ds_bp = *bp;
855 dsl_dataset_get_spa(dsl_dataset_t *ds)
857 return (ds->ds_dir->dd_pool->dp_spa);
861 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
865 if (ds == NULL) /* this is the meta-objset */
868 ASSERT(ds->ds_objset != NULL);
870 if (ds->ds_phys->ds_next_snap_obj != 0)
871 panic("dirtying snapshot!");
873 dp = ds->ds_dir->dd_pool;
875 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
876 /* up the hold count until we can be written out */
877 dmu_buf_add_ref(ds->ds_dbuf, ds);
882 dsl_dataset_is_dirty(dsl_dataset_t *ds)
886 for (t = 0; t < TXG_SIZE; t++) {
887 if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
895 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
899 if (!dmu_tx_is_syncing(tx))
903 * If there's an fs-only reservation, any blocks that might become
904 * owned by the snapshot dataset must be accommodated by space
905 * outside of the reservation.
907 ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
908 asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
909 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
913 * Propagate any reserved space for this snapshot to other
914 * snapshot checks in this sync group.
917 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
922 typedef struct dsl_dataset_snapshot_arg {
923 nvlist_t *ddsa_snaps;
924 nvlist_t *ddsa_props;
925 nvlist_t *ddsa_errors;
926 } dsl_dataset_snapshot_arg_t;
929 dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
935 ds->ds_trysnap_txg = tx->tx_txg;
937 if (!dmu_tx_is_syncing(tx))
941 * We don't allow multiple snapshots of the same txg. If there
942 * is already one, try again.
944 if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
948 * Check for conflicting snapshot name.
950 error = dsl_dataset_snap_lookup(ds, snapname, &value);
956 error = dsl_dataset_snapshot_reserve_space(ds, tx);
964 dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
966 dsl_dataset_snapshot_arg_t *ddsa = arg;
967 dsl_pool_t *dp = dmu_tx_pool(tx);
971 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
972 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
976 char dsname[MAXNAMELEN];
978 name = nvpair_name(pair);
979 if (strlen(name) >= MAXNAMELEN)
980 error = ENAMETOOLONG;
982 atp = strchr(name, '@');
986 (void) strlcpy(dsname, name, atp - name + 1);
989 error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
991 error = dsl_dataset_snapshot_check_impl(ds,
993 dsl_dataset_rele(ds, FTAG);
997 if (ddsa->ddsa_errors != NULL) {
998 fnvlist_add_int32(ddsa->ddsa_errors,
1008 dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1011 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1013 dsl_dataset_phys_t *dsphys;
1014 uint64_t dsobj, crtxg;
1015 objset_t *mos = dp->dp_meta_objset;
1016 ASSERTV(static zil_header_t zero_zil);
1017 ASSERTV(objset_t *os);
1019 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1022 * If we are on an old pool, the zil must not be active, in which
1023 * case it will be zeroed. Usually zil_suspend() accomplishes this.
1025 ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1026 dmu_objset_from_ds(ds, &os) != 0 ||
1027 bcmp(&os->os_phys->os_zil_header, &zero_zil,
1028 sizeof (zero_zil)) == 0);
1032 * The origin's ds_creation_txg has to be < TXG_INITIAL
1034 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1039 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1040 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1041 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1042 dmu_buf_will_dirty(dbuf, tx);
1043 dsphys = dbuf->db_data;
1044 bzero(dsphys, sizeof (dsl_dataset_phys_t));
1045 dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1046 dsphys->ds_fsid_guid = unique_create();
1047 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1048 sizeof (dsphys->ds_guid));
1049 dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1050 dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1051 dsphys->ds_next_snap_obj = ds->ds_object;
1052 dsphys->ds_num_children = 1;
1053 dsphys->ds_creation_time = gethrestime_sec();
1054 dsphys->ds_creation_txg = crtxg;
1055 dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1056 dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
1057 dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1058 dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
1059 dsphys->ds_flags = ds->ds_phys->ds_flags;
1060 dsphys->ds_bp = ds->ds_phys->ds_bp;
1061 dmu_buf_rele(dbuf, FTAG);
1063 ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
1065 uint64_t next_clones_obj =
1066 ds->ds_prev->ds_phys->ds_next_clones_obj;
1067 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1069 ds->ds_prev->ds_phys->ds_num_children > 1);
1070 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1071 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1072 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1073 ds->ds_prev->ds_phys->ds_creation_txg);
1074 ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1075 } else if (next_clones_obj != 0) {
1076 dsl_dataset_remove_from_next_clones(ds->ds_prev,
1077 dsphys->ds_next_snap_obj, tx);
1078 VERIFY0(zap_add_int(mos,
1079 next_clones_obj, dsobj, tx));
1084 * If we have a reference-reservation on this dataset, we will
1085 * need to increase the amount of refreservation being charged
1086 * since our unique space is going to zero.
1088 if (ds->ds_reserved) {
1090 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1091 delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
1092 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1096 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1097 ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
1098 UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
1099 dsl_deadlist_close(&ds->ds_deadlist);
1100 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1101 dsl_deadlist_add_key(&ds->ds_deadlist,
1102 ds->ds_phys->ds_prev_snap_txg, tx);
1104 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1105 ds->ds_phys->ds_prev_snap_obj = dsobj;
1106 ds->ds_phys->ds_prev_snap_txg = crtxg;
1107 ds->ds_phys->ds_unique_bytes = 0;
1108 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1109 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1111 VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1112 snapname, 8, 1, &dsobj, tx));
1115 dsl_dataset_rele(ds->ds_prev, ds);
1116 VERIFY0(dsl_dataset_hold_obj(dp,
1117 ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
1119 dsl_scan_ds_snapshotted(ds, tx);
1121 dsl_dir_snap_cmtime_update(ds->ds_dir);
1123 spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1127 dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1129 dsl_dataset_snapshot_arg_t *ddsa = arg;
1130 dsl_pool_t *dp = dmu_tx_pool(tx);
1133 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1134 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1137 char dsname[MAXNAMELEN];
1139 name = nvpair_name(pair);
1140 atp = strchr(name, '@');
1141 (void) strlcpy(dsname, name, atp - name + 1);
1142 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1144 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1145 if (ddsa->ddsa_props != NULL) {
1146 dsl_props_set_sync_impl(ds->ds_prev,
1147 ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1149 dsl_dataset_rele(ds, FTAG);
1154 * The snapshots must all be in the same pool.
1155 * All-or-nothing: if there are any failures, nothing will be modified.
1158 dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1160 dsl_dataset_snapshot_arg_t ddsa;
1162 boolean_t needsuspend;
1166 nvlist_t *suspended = NULL;
1168 pair = nvlist_next_nvpair(snaps, NULL);
1171 firstname = nvpair_name(pair);
1173 error = spa_open(firstname, &spa, FTAG);
1176 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1177 spa_close(spa, FTAG);
1180 suspended = fnvlist_alloc();
1181 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1182 pair = nvlist_next_nvpair(snaps, pair)) {
1183 char fsname[MAXNAMELEN];
1184 char *snapname = nvpair_name(pair);
1188 atp = strchr(snapname, '@');
1193 (void) strlcpy(fsname, snapname, atp - snapname + 1);
1195 error = zil_suspend(fsname, &cookie);
1198 fnvlist_add_uint64(suspended, fsname,
1203 ddsa.ddsa_snaps = snaps;
1204 ddsa.ddsa_props = props;
1205 ddsa.ddsa_errors = errors;
1208 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1209 dsl_dataset_snapshot_sync, &ddsa,
1210 fnvlist_num_pairs(snaps) * 3);
1213 if (suspended != NULL) {
1214 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1215 pair = nvlist_next_nvpair(suspended, pair)) {
1216 zil_resume((void *)(uintptr_t)
1217 fnvpair_value_uint64(pair));
1219 fnvlist_free(suspended);
1225 typedef struct dsl_dataset_snapshot_tmp_arg {
1226 const char *ddsta_fsname;
1227 const char *ddsta_snapname;
1228 minor_t ddsta_cleanup_minor;
1229 const char *ddsta_htag;
1230 } dsl_dataset_snapshot_tmp_arg_t;
1233 dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1235 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1236 dsl_pool_t *dp = dmu_tx_pool(tx);
1240 error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1244 error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, tx);
1246 dsl_dataset_rele(ds, FTAG);
1250 if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
1251 dsl_dataset_rele(ds, FTAG);
1254 error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
1257 dsl_dataset_rele(ds, FTAG);
1261 dsl_dataset_rele(ds, FTAG);
1266 dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
1268 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1269 dsl_pool_t *dp = dmu_tx_pool(tx);
1272 VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
1274 dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
1275 dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
1276 ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
1277 dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
1279 dsl_dataset_rele(ds, FTAG);
1283 dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
1284 minor_t cleanup_minor, const char *htag)
1286 dsl_dataset_snapshot_tmp_arg_t ddsta;
1289 boolean_t needsuspend;
1292 ddsta.ddsta_fsname = fsname;
1293 ddsta.ddsta_snapname = snapname;
1294 ddsta.ddsta_cleanup_minor = cleanup_minor;
1295 ddsta.ddsta_htag = htag;
1297 error = spa_open(fsname, &spa, FTAG);
1300 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1301 spa_close(spa, FTAG);
1304 error = zil_suspend(fsname, &cookie);
1309 error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
1310 dsl_dataset_snapshot_tmp_sync, &ddsta, 3);
1319 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1321 ASSERT(dmu_tx_is_syncing(tx));
1322 ASSERT(ds->ds_objset != NULL);
1323 ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1326 * in case we had to change ds_fsid_guid when we opened it,
1329 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1330 ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
1332 dmu_objset_sync(ds->ds_objset, zio, tx);
1336 get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1339 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1342 nvlist_t *propval = fnvlist_alloc();
1343 nvlist_t *val = fnvlist_alloc();
1345 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1348 * There may be missing entries in ds_next_clones_obj
1349 * due to a bug in a previous version of the code.
1350 * Only trust it if it has the right number of entries.
1352 if (ds->ds_phys->ds_next_clones_obj != 0) {
1353 ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
1356 if (count != ds->ds_phys->ds_num_children - 1)
1358 for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
1359 zap_cursor_retrieve(&zc, &za) == 0;
1360 zap_cursor_advance(&zc)) {
1361 dsl_dataset_t *clone;
1362 char buf[ZFS_MAXNAMELEN];
1363 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1364 za.za_first_integer, FTAG, &clone));
1365 dsl_dir_name(clone->ds_dir, buf);
1366 fnvlist_add_boolean(val, buf);
1367 dsl_dataset_rele(clone, FTAG);
1369 zap_cursor_fini(&zc);
1370 fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1371 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
1374 nvlist_free(propval);
1378 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1380 uint64_t refd, avail, uobjs, aobjs, ratio;
1381 ASSERTV(dsl_pool_t *dp = ds->ds_dir->dd_pool);
1383 ASSERT(dsl_pool_config_held(dp));
1385 ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
1386 (ds->ds_phys->ds_uncompressed_bytes * 100 /
1387 ds->ds_phys->ds_compressed_bytes);
1389 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
1391 if (dsl_dataset_is_snapshot(ds)) {
1392 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
1393 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1394 ds->ds_phys->ds_unique_bytes);
1395 get_clones_stat(ds, nv);
1397 dsl_dir_stats(ds->ds_dir, nv);
1400 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1401 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1402 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1404 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1405 ds->ds_phys->ds_creation_time);
1406 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1407 ds->ds_phys->ds_creation_txg);
1408 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1410 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1412 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1413 ds->ds_phys->ds_guid);
1414 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1415 ds->ds_phys->ds_unique_bytes);
1416 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
1418 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
1420 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1421 DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1423 if (ds->ds_phys->ds_prev_snap_obj != 0) {
1424 uint64_t written, comp, uncomp;
1425 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1426 dsl_dataset_t *prev;
1429 err = dsl_dataset_hold_obj(dp,
1430 ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1432 err = dsl_dataset_space_written(prev, ds, &written,
1434 dsl_dataset_rele(prev, FTAG);
1436 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
1445 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1447 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1448 ASSERT(dsl_pool_config_held(dp));
1450 stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1451 stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
1452 stat->dds_guid = ds->ds_phys->ds_guid;
1453 stat->dds_origin[0] = '\0';
1454 if (dsl_dataset_is_snapshot(ds)) {
1455 stat->dds_is_snapshot = B_TRUE;
1456 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1458 stat->dds_is_snapshot = B_FALSE;
1459 stat->dds_num_clones = 0;
1461 if (dsl_dir_is_clone(ds->ds_dir)) {
1464 VERIFY0(dsl_dataset_hold_obj(dp,
1465 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
1466 dsl_dataset_name(ods, stat->dds_origin);
1467 dsl_dataset_rele(ods, FTAG);
1473 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1475 return (ds->ds_fsid_guid);
1479 dsl_dataset_space(dsl_dataset_t *ds,
1480 uint64_t *refdbytesp, uint64_t *availbytesp,
1481 uint64_t *usedobjsp, uint64_t *availobjsp)
1483 *refdbytesp = ds->ds_phys->ds_referenced_bytes;
1484 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1485 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
1486 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
1487 if (ds->ds_quota != 0) {
1489 * Adjust available bytes according to refquota
1491 if (*refdbytesp < ds->ds_quota)
1492 *availbytesp = MIN(*availbytesp,
1493 ds->ds_quota - *refdbytesp);
1497 *usedobjsp = ds->ds_phys->ds_bp.blk_fill;
1498 *availobjsp = DN_MAX_OBJECT - *usedobjsp;
1502 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
1504 ASSERTV(dsl_pool_t *dp = ds->ds_dir->dd_pool);
1506 ASSERT(dsl_pool_config_held(dp));
1507 if (ds->ds_prev == NULL)
1509 if (ds->ds_phys->ds_bp.blk_birth >
1510 ds->ds_prev->ds_phys->ds_creation_txg) {
1511 objset_t *os, *os_prev;
1513 * It may be that only the ZIL differs, because it was
1514 * reset in the head. Don't count that as being
1517 if (dmu_objset_from_ds(ds, &os) != 0)
1519 if (dmu_objset_from_ds(ds->ds_prev, &os_prev) != 0)
1521 return (bcmp(&os->os_phys->os_meta_dnode,
1522 &os_prev->os_phys->os_meta_dnode,
1523 sizeof (os->os_phys->os_meta_dnode)) != 0);
1528 typedef struct dsl_dataset_rename_snapshot_arg {
1529 const char *ddrsa_fsname;
1530 const char *ddrsa_oldsnapname;
1531 const char *ddrsa_newsnapname;
1532 boolean_t ddrsa_recursive;
1534 } dsl_dataset_rename_snapshot_arg_t;
1538 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
1539 dsl_dataset_t *hds, void *arg)
1541 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1545 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1547 /* ignore nonexistent snapshots */
1548 return (error == ENOENT ? 0 : error);
1551 /* new name should not exist */
1552 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
1555 else if (error == ENOENT)
1558 /* dataset name + 1 for the "@" + the new snapshot name must fit */
1559 if (dsl_dir_namelen(hds->ds_dir) + 1 +
1560 strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1561 error = ENAMETOOLONG;
1567 dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
1569 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1570 dsl_pool_t *dp = dmu_tx_pool(tx);
1574 error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
1578 if (ddrsa->ddrsa_recursive) {
1579 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1580 dsl_dataset_rename_snapshot_check_impl, ddrsa,
1583 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
1585 dsl_dataset_rele(hds, FTAG);
1590 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
1591 dsl_dataset_t *hds, void *arg)
1593 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1596 dmu_tx_t *tx = ddrsa->ddrsa_tx;
1599 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1600 ASSERT(error == 0 || error == ENOENT);
1601 if (error == ENOENT) {
1602 /* ignore nonexistent snapshots */
1606 VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
1608 /* log before we change the name */
1609 spa_history_log_internal_ds(ds, "rename", tx,
1610 "-> @%s", ddrsa->ddrsa_newsnapname);
1612 VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx));
1613 mutex_enter(&ds->ds_lock);
1614 (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
1615 mutex_exit(&ds->ds_lock);
1616 VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj,
1617 ds->ds_snapname, 8, 1, &ds->ds_object, tx));
1619 dsl_dataset_rele(ds, FTAG);
1624 dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
1626 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1627 dsl_pool_t *dp = dmu_tx_pool(tx);
1630 VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
1631 ddrsa->ddrsa_tx = tx;
1632 if (ddrsa->ddrsa_recursive) {
1633 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1634 dsl_dataset_rename_snapshot_sync_impl, ddrsa,
1637 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
1639 dsl_dataset_rele(hds, FTAG);
1643 dsl_dataset_rename_snapshot(const char *fsname,
1644 const char *oldsnapname, const char *newsnapname, boolean_t recursive)
1646 dsl_dataset_rename_snapshot_arg_t ddrsa;
1648 ddrsa.ddrsa_fsname = fsname;
1649 ddrsa.ddrsa_oldsnapname = oldsnapname;
1650 ddrsa.ddrsa_newsnapname = newsnapname;
1651 ddrsa.ddrsa_recursive = recursive;
1653 return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
1654 dsl_dataset_rename_snapshot_sync, &ddrsa, 1));
1658 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
1660 const char *fsname = arg;
1661 dsl_pool_t *dp = dmu_tx_pool(tx);
1663 int64_t unused_refres_delta;
1666 error = dsl_dataset_hold(dp, fsname, FTAG, &ds);
1670 /* must not be a snapshot */
1671 if (dsl_dataset_is_snapshot(ds)) {
1672 dsl_dataset_rele(ds, FTAG);
1676 /* must have a most recent snapshot */
1677 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
1678 dsl_dataset_rele(ds, FTAG);
1682 if (dsl_dataset_long_held(ds)) {
1683 dsl_dataset_rele(ds, FTAG);
1688 * Check if the snap we are rolling back to uses more than
1691 if (ds->ds_quota != 0 &&
1692 ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) {
1693 dsl_dataset_rele(ds, FTAG);
1698 * When we do the clone swap, we will temporarily use more space
1699 * due to the refreservation (the head will no longer have any
1700 * unique space, so the entire amount of the refreservation will need
1701 * to be free). We will immediately destroy the clone, freeing
1702 * this space, but the freeing happens over many txg's.
1704 unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
1705 ds->ds_phys->ds_unique_bytes);
1707 if (unused_refres_delta > 0 &&
1708 unused_refres_delta >
1709 dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
1710 dsl_dataset_rele(ds, FTAG);
1714 dsl_dataset_rele(ds, FTAG);
1719 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
1721 const char *fsname = arg;
1722 dsl_pool_t *dp = dmu_tx_pool(tx);
1723 dsl_dataset_t *ds, *clone;
1726 VERIFY0(dsl_dataset_hold(dp, fsname, FTAG, &ds));
1728 cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
1729 ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
1731 VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
1733 dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
1734 dsl_dataset_zero_zil(ds, tx);
1736 dsl_destroy_head_sync_impl(clone, tx);
1738 dsl_dataset_rele(clone, FTAG);
1739 dsl_dataset_rele(ds, FTAG);
1743 dsl_dataset_rollback(const char *fsname)
1745 return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
1746 dsl_dataset_rollback_sync, (void *)fsname, 1));
1749 struct promotenode {
1754 typedef struct dsl_dataset_promote_arg {
1755 const char *ddpa_clonename;
1756 dsl_dataset_t *ddpa_clone;
1757 list_t shared_snaps, origin_snaps, clone_snaps;
1758 dsl_dataset_t *origin_origin; /* origin of the origin */
1759 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
1761 } dsl_dataset_promote_arg_t;
1763 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
1764 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
1766 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
1769 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
1771 dsl_dataset_promote_arg_t *ddpa = arg;
1772 dsl_pool_t *dp = dmu_tx_pool(tx);
1774 struct promotenode *snap;
1775 dsl_dataset_t *origin_ds;
1779 err = promote_hold(ddpa, dp, FTAG);
1783 hds = ddpa->ddpa_clone;
1785 if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
1786 promote_rele(ddpa, FTAG);
1791 * Compute and check the amount of space to transfer. Since this is
1792 * so expensive, don't do the preliminary check.
1794 if (!dmu_tx_is_syncing(tx)) {
1795 promote_rele(ddpa, FTAG);
1799 snap = list_head(&ddpa->shared_snaps);
1800 origin_ds = snap->ds;
1802 /* compute origin's new unique space */
1803 snap = list_tail(&ddpa->clone_snaps);
1804 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
1805 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
1806 origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
1807 &ddpa->unique, &unused, &unused);
1810 * Walk the snapshots that we are moving
1812 * Compute space to transfer. Consider the incremental changes
1813 * to used by each snapshot:
1814 * (my used) = (prev's used) + (blocks born) - (blocks killed)
1815 * So each snapshot gave birth to:
1816 * (blocks born) = (my used) - (prev's used) + (blocks killed)
1817 * So a sequence would look like:
1818 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
1819 * Which simplifies to:
1820 * uN + kN + kN-1 + ... + k1 + k0
1821 * Note however, if we stop before we reach the ORIGIN we get:
1822 * uN + kN + kN-1 + ... + kM - uM-1
1824 ddpa->used = origin_ds->ds_phys->ds_referenced_bytes;
1825 ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes;
1826 ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
1827 for (snap = list_head(&ddpa->shared_snaps); snap;
1828 snap = list_next(&ddpa->shared_snaps, snap)) {
1829 uint64_t val, dlused, dlcomp, dluncomp;
1830 dsl_dataset_t *ds = snap->ds;
1833 * If there are long holds, we won't be able to evict
1836 if (dsl_dataset_long_held(ds)) {
1841 /* Check that the snapshot name does not conflict */
1842 VERIFY0(dsl_dataset_get_snapname(ds));
1843 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
1845 (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
1852 /* The very first snapshot does not have a deadlist */
1853 if (ds->ds_phys->ds_prev_snap_obj == 0)
1856 dsl_deadlist_space(&ds->ds_deadlist,
1857 &dlused, &dlcomp, &dluncomp);
1858 ddpa->used += dlused;
1859 ddpa->comp += dlcomp;
1860 ddpa->uncomp += dluncomp;
1864 * If we are a clone of a clone then we never reached ORIGIN,
1865 * so we need to subtract out the clone origin's used space.
1867 if (ddpa->origin_origin) {
1868 ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes;
1869 ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes;
1871 ddpa->origin_origin->ds_phys->ds_uncompressed_bytes;
1874 /* Check that there is enough space here */
1875 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
1881 * Compute the amounts of space that will be used by snapshots
1882 * after the promotion (for both origin and clone). For each,
1883 * it is the amount of space that will be on all of their
1884 * deadlists (that was not born before their new origin).
1886 if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1890 * Note, typically this will not be a clone of a clone,
1891 * so dd_origin_txg will be < TXG_INITIAL, so
1892 * these snaplist_space() -> dsl_deadlist_space_range()
1893 * calls will be fast because they do not have to
1894 * iterate over all bps.
1896 snap = list_head(&ddpa->origin_snaps);
1897 err = snaplist_space(&ddpa->shared_snaps,
1898 snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
1902 err = snaplist_space(&ddpa->clone_snaps,
1903 snap->ds->ds_dir->dd_origin_txg, &space);
1906 ddpa->cloneusedsnap += space;
1908 if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1909 err = snaplist_space(&ddpa->origin_snaps,
1910 origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap);
1916 promote_rele(ddpa, FTAG);
1921 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
1923 dsl_dataset_promote_arg_t *ddpa = arg;
1924 dsl_pool_t *dp = dmu_tx_pool(tx);
1926 struct promotenode *snap;
1927 dsl_dataset_t *origin_ds;
1928 dsl_dataset_t *origin_head;
1930 dsl_dir_t *odd = NULL;
1931 uint64_t oldnext_obj;
1934 VERIFY0(promote_hold(ddpa, dp, FTAG));
1935 hds = ddpa->ddpa_clone;
1937 ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE);
1939 snap = list_head(&ddpa->shared_snaps);
1940 origin_ds = snap->ds;
1943 snap = list_head(&ddpa->origin_snaps);
1944 origin_head = snap->ds;
1947 * We need to explicitly open odd, since origin_ds's dd will be
1950 VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
1953 /* change origin's next snap */
1954 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
1955 oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
1956 snap = list_tail(&ddpa->clone_snaps);
1957 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
1958 origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
1960 /* change the origin's next clone */
1961 if (origin_ds->ds_phys->ds_next_clones_obj) {
1962 dsl_dataset_remove_from_next_clones(origin_ds,
1963 snap->ds->ds_object, tx);
1964 VERIFY0(zap_add_int(dp->dp_meta_objset,
1965 origin_ds->ds_phys->ds_next_clones_obj,
1970 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1971 ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
1972 dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
1973 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
1974 dmu_buf_will_dirty(odd->dd_dbuf, tx);
1975 odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
1976 origin_head->ds_dir->dd_origin_txg =
1977 origin_ds->ds_phys->ds_creation_txg;
1979 /* change dd_clone entries */
1980 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
1981 VERIFY0(zap_remove_int(dp->dp_meta_objset,
1982 odd->dd_phys->dd_clones, hds->ds_object, tx));
1983 VERIFY0(zap_add_int(dp->dp_meta_objset,
1984 ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
1985 hds->ds_object, tx));
1987 VERIFY0(zap_remove_int(dp->dp_meta_objset,
1988 ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
1989 origin_head->ds_object, tx));
1990 if (dd->dd_phys->dd_clones == 0) {
1991 dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
1992 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
1994 VERIFY0(zap_add_int(dp->dp_meta_objset,
1995 dd->dd_phys->dd_clones, origin_head->ds_object, tx));
1998 /* move snapshots to this dir */
1999 for (snap = list_head(&ddpa->shared_snaps); snap;
2000 snap = list_next(&ddpa->shared_snaps, snap)) {
2001 dsl_dataset_t *ds = snap->ds;
2004 * Property callbacks are registered to a particular
2005 * dsl_dir. Since ours is changing, evict the objset
2006 * so that they will be unregistered from the old dsl_dir.
2008 if (ds->ds_objset) {
2009 dmu_objset_evict(ds->ds_objset);
2010 ds->ds_objset = NULL;
2013 /* move snap name entry */
2014 VERIFY0(dsl_dataset_get_snapname(ds));
2015 VERIFY0(dsl_dataset_snap_remove(origin_head,
2016 ds->ds_snapname, tx));
2017 VERIFY0(zap_add(dp->dp_meta_objset,
2018 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
2019 8, 1, &ds->ds_object, tx));
2021 /* change containing dsl_dir */
2022 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2023 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2024 ds->ds_phys->ds_dir_obj = dd->dd_object;
2025 ASSERT3P(ds->ds_dir, ==, odd);
2026 dsl_dir_rele(ds->ds_dir, ds);
2027 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
2028 NULL, ds, &ds->ds_dir));
2030 /* move any clone references */
2031 if (ds->ds_phys->ds_next_clones_obj &&
2032 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2036 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2037 ds->ds_phys->ds_next_clones_obj);
2038 zap_cursor_retrieve(&zc, &za) == 0;
2039 zap_cursor_advance(&zc)) {
2040 dsl_dataset_t *cnds;
2043 if (za.za_first_integer == oldnext_obj) {
2045 * We've already moved the
2046 * origin's reference.
2051 VERIFY0(dsl_dataset_hold_obj(dp,
2052 za.za_first_integer, FTAG, &cnds));
2053 o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2055 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2056 odd->dd_phys->dd_clones, o, tx));
2057 VERIFY0(zap_add_int(dp->dp_meta_objset,
2058 dd->dd_phys->dd_clones, o, tx));
2059 dsl_dataset_rele(cnds, FTAG);
2061 zap_cursor_fini(&zc);
2064 ASSERT(!dsl_prop_hascb(ds));
2068 * Change space accounting.
2069 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2070 * both be valid, or both be 0 (resulting in delta == 0). This
2071 * is true for each of {clone,origin} independently.
2074 delta = ddpa->cloneusedsnap -
2075 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2076 ASSERT3S(delta, >=, 0);
2077 ASSERT3U(ddpa->used, >=, delta);
2078 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2079 dsl_dir_diduse_space(dd, DD_USED_HEAD,
2080 ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
2082 delta = ddpa->originusedsnap -
2083 odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2084 ASSERT3S(delta, <=, 0);
2085 ASSERT3U(ddpa->used, >=, -delta);
2086 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2087 dsl_dir_diduse_space(odd, DD_USED_HEAD,
2088 -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
2090 origin_ds->ds_phys->ds_unique_bytes = ddpa->unique;
2092 /* log history record */
2093 spa_history_log_internal_ds(hds, "promote", tx, "");
2095 dsl_dir_rele(odd, FTAG);
2096 promote_rele(ddpa, FTAG);
2100 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2101 * (exclusive) and last_obj (inclusive). The list will be in reverse
2102 * order (last_obj will be the list_head()). If first_obj == 0, do all
2103 * snapshots back to this dataset's origin.
2106 snaplist_make(dsl_pool_t *dp,
2107 uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2109 uint64_t obj = last_obj;
2111 list_create(l, sizeof (struct promotenode),
2112 offsetof(struct promotenode, link));
2114 while (obj != first_obj) {
2116 struct promotenode *snap;
2119 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
2120 ASSERT(err != ENOENT);
2125 first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
2127 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
2129 list_insert_tail(l, snap);
2130 obj = ds->ds_phys->ds_prev_snap_obj;
2137 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2139 struct promotenode *snap;
2142 for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2143 uint64_t used, comp, uncomp;
2144 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2145 mintxg, UINT64_MAX, &used, &comp, &uncomp);
2152 snaplist_destroy(list_t *l, void *tag)
2154 struct promotenode *snap;
2156 if (l == NULL || !list_link_active(&l->list_head))
2159 while ((snap = list_tail(l)) != NULL) {
2160 list_remove(l, snap);
2161 dsl_dataset_rele(snap->ds, tag);
2162 kmem_free(snap, sizeof (*snap));
2168 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2172 struct promotenode *snap;
2174 error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
2178 dd = ddpa->ddpa_clone->ds_dir;
2180 if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) ||
2181 !dsl_dir_is_clone(dd)) {
2182 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2186 error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj,
2187 &ddpa->shared_snaps, tag);
2191 error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
2192 &ddpa->clone_snaps, tag);
2196 snap = list_head(&ddpa->shared_snaps);
2197 ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
2198 error = snaplist_make(dp, dd->dd_phys->dd_origin_obj,
2199 snap->ds->ds_dir->dd_phys->dd_head_dataset_obj,
2200 &ddpa->origin_snaps, tag);
2204 if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
2205 error = dsl_dataset_hold_obj(dp,
2206 snap->ds->ds_dir->dd_phys->dd_origin_obj,
2207 tag, &ddpa->origin_origin);
2213 promote_rele(ddpa, tag);
2218 promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2220 snaplist_destroy(&ddpa->shared_snaps, tag);
2221 snaplist_destroy(&ddpa->clone_snaps, tag);
2222 snaplist_destroy(&ddpa->origin_snaps, tag);
2223 if (ddpa->origin_origin != NULL)
2224 dsl_dataset_rele(ddpa->origin_origin, tag);
2225 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2231 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2232 * in with the name. (It must be at least MAXNAMELEN bytes long.)
2235 dsl_dataset_promote(const char *name, char *conflsnap)
2237 dsl_dataset_promote_arg_t ddpa = { 0 };
2243 * We will modify space proportional to the number of
2244 * snapshots. Compute numsnaps.
2246 error = dmu_objset_hold(name, FTAG, &os);
2249 error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2250 dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps);
2251 dmu_objset_rele(os, FTAG);
2255 ddpa.ddpa_clonename = name;
2256 ddpa.err_ds = conflsnap;
2258 return (dsl_sync_task(name, dsl_dataset_promote_check,
2259 dsl_dataset_promote_sync, &ddpa, 2 + numsnaps));
2263 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
2264 dsl_dataset_t *origin_head, boolean_t force)
2266 int64_t unused_refres_delta;
2268 /* they should both be heads */
2269 if (dsl_dataset_is_snapshot(clone) ||
2270 dsl_dataset_is_snapshot(origin_head))
2273 /* the branch point should be just before them */
2274 if (clone->ds_prev != origin_head->ds_prev)
2277 /* clone should be the clone (unless they are unrelated) */
2278 if (clone->ds_prev != NULL &&
2279 clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
2280 origin_head->ds_object !=
2281 clone->ds_prev->ds_phys->ds_next_snap_obj)
2284 /* the clone should be a child of the origin */
2285 if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2288 /* origin_head shouldn't be modified unless 'force' */
2289 if (!force && dsl_dataset_modified_since_lastsnap(origin_head))
2292 /* origin_head should have no long holds (e.g. is not mounted) */
2293 if (dsl_dataset_long_held(origin_head))
2296 /* check amount of any unconsumed refreservation */
2297 unused_refres_delta =
2298 (int64_t)MIN(origin_head->ds_reserved,
2299 origin_head->ds_phys->ds_unique_bytes) -
2300 (int64_t)MIN(origin_head->ds_reserved,
2301 clone->ds_phys->ds_unique_bytes);
2303 if (unused_refres_delta > 0 &&
2304 unused_refres_delta >
2305 dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2308 /* clone can't be over the head's refquota */
2309 if (origin_head->ds_quota != 0 &&
2310 clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota)
2317 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2318 dsl_dataset_t *origin_head, dmu_tx_t *tx)
2320 dsl_pool_t *dp = dmu_tx_pool(tx);
2321 int64_t unused_refres_delta;
2323 ASSERT(clone->ds_reserved == 0);
2324 ASSERT(origin_head->ds_quota == 0 ||
2325 clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota);
2327 dmu_buf_will_dirty(clone->ds_dbuf, tx);
2328 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2330 if (clone->ds_objset != NULL) {
2331 dmu_objset_evict(clone->ds_objset);
2332 clone->ds_objset = NULL;
2335 if (origin_head->ds_objset != NULL) {
2336 dmu_objset_evict(origin_head->ds_objset);
2337 origin_head->ds_objset = NULL;
2340 unused_refres_delta =
2341 (int64_t)MIN(origin_head->ds_reserved,
2342 origin_head->ds_phys->ds_unique_bytes) -
2343 (int64_t)MIN(origin_head->ds_reserved,
2344 clone->ds_phys->ds_unique_bytes);
2347 * Reset origin's unique bytes, if it exists.
2349 if (clone->ds_prev) {
2350 dsl_dataset_t *origin = clone->ds_prev;
2351 uint64_t comp, uncomp;
2353 dmu_buf_will_dirty(origin->ds_dbuf, tx);
2354 dsl_deadlist_space_range(&clone->ds_deadlist,
2355 origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2356 &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
2362 tmp = origin_head->ds_phys->ds_bp;
2363 origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp;
2364 clone->ds_phys->ds_bp = tmp;
2367 /* set dd_*_bytes */
2369 int64_t dused, dcomp, duncomp;
2370 uint64_t cdl_used, cdl_comp, cdl_uncomp;
2371 uint64_t odl_used, odl_comp, odl_uncomp;
2373 ASSERT3U(clone->ds_dir->dd_phys->
2374 dd_used_breakdown[DD_USED_SNAP], ==, 0);
2376 dsl_deadlist_space(&clone->ds_deadlist,
2377 &cdl_used, &cdl_comp, &cdl_uncomp);
2378 dsl_deadlist_space(&origin_head->ds_deadlist,
2379 &odl_used, &odl_comp, &odl_uncomp);
2381 dused = clone->ds_phys->ds_referenced_bytes + cdl_used -
2382 (origin_head->ds_phys->ds_referenced_bytes + odl_used);
2383 dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp -
2384 (origin_head->ds_phys->ds_compressed_bytes + odl_comp);
2385 duncomp = clone->ds_phys->ds_uncompressed_bytes +
2387 (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp);
2389 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
2390 dused, dcomp, duncomp, tx);
2391 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
2392 -dused, -dcomp, -duncomp, tx);
2395 * The difference in the space used by snapshots is the
2396 * difference in snapshot space due to the head's
2397 * deadlist (since that's the only thing that's
2398 * changing that affects the snapused).
2400 dsl_deadlist_space_range(&clone->ds_deadlist,
2401 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2402 &cdl_used, &cdl_comp, &cdl_uncomp);
2403 dsl_deadlist_space_range(&origin_head->ds_deadlist,
2404 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2405 &odl_used, &odl_comp, &odl_uncomp);
2406 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
2407 DD_USED_HEAD, DD_USED_SNAP, tx);
2410 /* swap ds_*_bytes */
2411 SWITCH64(origin_head->ds_phys->ds_referenced_bytes,
2412 clone->ds_phys->ds_referenced_bytes);
2413 SWITCH64(origin_head->ds_phys->ds_compressed_bytes,
2414 clone->ds_phys->ds_compressed_bytes);
2415 SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes,
2416 clone->ds_phys->ds_uncompressed_bytes);
2417 SWITCH64(origin_head->ds_phys->ds_unique_bytes,
2418 clone->ds_phys->ds_unique_bytes);
2420 /* apply any parent delta for change in unconsumed refreservation */
2421 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
2422 unused_refres_delta, 0, 0, tx);
2427 dsl_deadlist_close(&clone->ds_deadlist);
2428 dsl_deadlist_close(&origin_head->ds_deadlist);
2429 SWITCH64(origin_head->ds_phys->ds_deadlist_obj,
2430 clone->ds_phys->ds_deadlist_obj);
2431 dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
2432 clone->ds_phys->ds_deadlist_obj);
2433 dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
2434 origin_head->ds_phys->ds_deadlist_obj);
2436 dsl_scan_ds_clone_swapped(origin_head, clone, tx);
2438 spa_history_log_internal_ds(clone, "clone swap", tx,
2439 "parent=%s", origin_head->ds_dir->dd_myname);
2443 * Given a pool name and a dataset object number in that pool,
2444 * return the name of that dataset.
2447 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2453 error = dsl_pool_hold(pname, FTAG, &dp);
2457 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
2459 dsl_dataset_name(ds, buf);
2460 dsl_dataset_rele(ds, FTAG);
2462 dsl_pool_rele(dp, FTAG);
2468 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
2469 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
2473 ASSERT3S(asize, >, 0);
2476 * *ref_rsrv is the portion of asize that will come from any
2477 * unconsumed refreservation space.
2481 mutex_enter(&ds->ds_lock);
2483 * Make a space adjustment for reserved bytes.
2485 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
2487 ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2488 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2490 asize - MIN(asize, parent_delta(ds, asize + inflight));
2493 if (!check_quota || ds->ds_quota == 0) {
2494 mutex_exit(&ds->ds_lock);
2498 * If they are requesting more space, and our current estimate
2499 * is over quota, they get to try again unless the actual
2500 * on-disk is over quota and there are no pending changes (which
2501 * may free up space for us).
2503 if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
2505 ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
2510 mutex_exit(&ds->ds_lock);
2515 typedef struct dsl_dataset_set_qr_arg {
2516 const char *ddsqra_name;
2517 zprop_source_t ddsqra_source;
2518 uint64_t ddsqra_value;
2519 } dsl_dataset_set_qr_arg_t;
2524 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
2526 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2527 dsl_pool_t *dp = dmu_tx_pool(tx);
2532 if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2535 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2539 if (dsl_dataset_is_snapshot(ds)) {
2540 dsl_dataset_rele(ds, FTAG);
2544 error = dsl_prop_predict(ds->ds_dir,
2545 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2546 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2548 dsl_dataset_rele(ds, FTAG);
2553 dsl_dataset_rele(ds, FTAG);
2557 if (newval < ds->ds_phys->ds_referenced_bytes ||
2558 newval < ds->ds_reserved) {
2559 dsl_dataset_rele(ds, FTAG);
2563 dsl_dataset_rele(ds, FTAG);
2568 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
2570 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2571 dsl_pool_t *dp = dmu_tx_pool(tx);
2575 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2577 dsl_prop_set_sync_impl(ds,
2578 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2579 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
2580 &ddsqra->ddsqra_value, tx);
2582 VERIFY0(dsl_prop_get_int_ds(ds,
2583 zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
2585 if (ds->ds_quota != newval) {
2586 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2587 ds->ds_quota = newval;
2589 dsl_dataset_rele(ds, FTAG);
2593 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
2596 dsl_dataset_set_qr_arg_t ddsqra;
2598 ddsqra.ddsqra_name = dsname;
2599 ddsqra.ddsqra_source = source;
2600 ddsqra.ddsqra_value = refquota;
2602 return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
2603 dsl_dataset_set_refquota_sync, &ddsqra, 0));
2607 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
2609 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2610 dsl_pool_t *dp = dmu_tx_pool(tx);
2613 uint64_t newval, unique;
2615 if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2618 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2622 if (dsl_dataset_is_snapshot(ds)) {
2623 dsl_dataset_rele(ds, FTAG);
2627 error = dsl_prop_predict(ds->ds_dir,
2628 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2629 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2631 dsl_dataset_rele(ds, FTAG);
2636 * If we are doing the preliminary check in open context, the
2637 * space estimates may be inaccurate.
2639 if (!dmu_tx_is_syncing(tx)) {
2640 dsl_dataset_rele(ds, FTAG);
2644 mutex_enter(&ds->ds_lock);
2645 if (!DS_UNIQUE_IS_ACCURATE(ds))
2646 dsl_dataset_recalc_head_uniq(ds);
2647 unique = ds->ds_phys->ds_unique_bytes;
2648 mutex_exit(&ds->ds_lock);
2650 if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
2651 uint64_t delta = MAX(unique, newval) -
2652 MAX(unique, ds->ds_reserved);
2655 dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
2656 (ds->ds_quota > 0 && newval > ds->ds_quota)) {
2657 dsl_dataset_rele(ds, FTAG);
2662 dsl_dataset_rele(ds, FTAG);
2667 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
2668 zprop_source_t source, uint64_t value, dmu_tx_t *tx)
2674 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2675 source, sizeof (value), 1, &value, tx);
2677 VERIFY0(dsl_prop_get_int_ds(ds,
2678 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
2680 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2681 mutex_enter(&ds->ds_dir->dd_lock);
2682 mutex_enter(&ds->ds_lock);
2683 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
2684 unique = ds->ds_phys->ds_unique_bytes;
2685 delta = MAX(0, (int64_t)(newval - unique)) -
2686 MAX(0, (int64_t)(ds->ds_reserved - unique));
2687 ds->ds_reserved = newval;
2688 mutex_exit(&ds->ds_lock);
2690 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
2691 mutex_exit(&ds->ds_dir->dd_lock);
2695 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
2697 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2698 dsl_pool_t *dp = dmu_tx_pool(tx);
2701 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2702 dsl_dataset_set_refreservation_sync_impl(ds,
2703 ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
2704 dsl_dataset_rele(ds, FTAG);
2708 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
2709 uint64_t refreservation)
2711 dsl_dataset_set_qr_arg_t ddsqra;
2713 ddsqra.ddsqra_name = dsname;
2714 ddsqra.ddsqra_source = source;
2715 ddsqra.ddsqra_value = refreservation;
2717 return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
2718 dsl_dataset_set_refreservation_sync, &ddsqra, 0));
2722 * Return (in *usedp) the amount of space written in new that is not
2723 * present in oldsnap. New may be a snapshot or the head. Old must be
2724 * a snapshot before new, in new's filesystem (or its origin). If not then
2725 * fail and return EINVAL.
2727 * The written space is calculated by considering two components: First, we
2728 * ignore any freed space, and calculate the written as new's used space
2729 * minus old's used space. Next, we add in the amount of space that was freed
2730 * between the two snapshots, thus reducing new's used space relative to old's.
2731 * Specifically, this is the space that was born before old->ds_creation_txg,
2732 * and freed before new (ie. on new's deadlist or a previous deadlist).
2734 * space freed [---------------------]
2735 * snapshots ---O-------O--------O-------O------
2739 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
2740 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2744 dsl_pool_t *dp = new->ds_dir->dd_pool;
2746 ASSERT(dsl_pool_config_held(dp));
2749 *usedp += new->ds_phys->ds_referenced_bytes;
2750 *usedp -= oldsnap->ds_phys->ds_referenced_bytes;
2753 *compp += new->ds_phys->ds_compressed_bytes;
2754 *compp -= oldsnap->ds_phys->ds_compressed_bytes;
2757 *uncompp += new->ds_phys->ds_uncompressed_bytes;
2758 *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
2760 snapobj = new->ds_object;
2761 while (snapobj != oldsnap->ds_object) {
2762 dsl_dataset_t *snap;
2763 uint64_t used, comp, uncomp;
2765 if (snapobj == new->ds_object) {
2768 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
2773 if (snap->ds_phys->ds_prev_snap_txg ==
2774 oldsnap->ds_phys->ds_creation_txg) {
2776 * The blocks in the deadlist can not be born after
2777 * ds_prev_snap_txg, so get the whole deadlist space,
2778 * which is more efficient (especially for old-format
2779 * deadlists). Unfortunately the deadlist code
2780 * doesn't have enough information to make this
2781 * optimization itself.
2783 dsl_deadlist_space(&snap->ds_deadlist,
2784 &used, &comp, &uncomp);
2786 dsl_deadlist_space_range(&snap->ds_deadlist,
2787 0, oldsnap->ds_phys->ds_creation_txg,
2788 &used, &comp, &uncomp);
2795 * If we get to the beginning of the chain of snapshots
2796 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
2797 * was not a snapshot of/before new.
2799 snapobj = snap->ds_phys->ds_prev_snap_obj;
2801 dsl_dataset_rele(snap, FTAG);
2812 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
2813 * lastsnap, and all snapshots in between are deleted.
2815 * blocks that would be freed [---------------------------]
2816 * snapshots ---O-------O--------O-------O--------O
2817 * firstsnap lastsnap
2819 * This is the set of blocks that were born after the snap before firstsnap,
2820 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
2821 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
2822 * We calculate this by iterating over the relevant deadlists (from the snap
2823 * after lastsnap, backward to the snap after firstsnap), summing up the
2824 * space on the deadlist that was born after the snap before firstsnap.
2827 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
2828 dsl_dataset_t *lastsnap,
2829 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2833 dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
2835 ASSERT(dsl_dataset_is_snapshot(firstsnap));
2836 ASSERT(dsl_dataset_is_snapshot(lastsnap));
2839 * Check that the snapshots are in the same dsl_dir, and firstsnap
2840 * is before lastsnap.
2842 if (firstsnap->ds_dir != lastsnap->ds_dir ||
2843 firstsnap->ds_phys->ds_creation_txg >
2844 lastsnap->ds_phys->ds_creation_txg)
2847 *usedp = *compp = *uncompp = 0;
2849 snapobj = lastsnap->ds_phys->ds_next_snap_obj;
2850 while (snapobj != firstsnap->ds_object) {
2852 uint64_t used, comp, uncomp;
2854 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
2858 dsl_deadlist_space_range(&ds->ds_deadlist,
2859 firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2860 &used, &comp, &uncomp);
2865 snapobj = ds->ds_phys->ds_prev_snap_obj;
2866 ASSERT3U(snapobj, !=, 0);
2867 dsl_dataset_rele(ds, FTAG);
2873 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
2874 * For example, they could both be snapshots of the same filesystem, and
2875 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
2876 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
2877 * filesystem. Or 'earlier' could be the origin's origin.
2880 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier)
2882 dsl_pool_t *dp = later->ds_dir->dd_pool;
2885 dsl_dataset_t *origin;
2887 ASSERT(dsl_pool_config_held(dp));
2889 if (earlier->ds_phys->ds_creation_txg >=
2890 later->ds_phys->ds_creation_txg)
2893 if (later->ds_dir == earlier->ds_dir)
2895 if (!dsl_dir_is_clone(later->ds_dir))
2898 if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object)
2900 error = dsl_dataset_hold_obj(dp,
2901 later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin);
2904 ret = dsl_dataset_is_before(origin, earlier);
2905 dsl_dataset_rele(origin, FTAG);
2909 #if defined(_KERNEL) && defined(HAVE_SPL)
2910 EXPORT_SYMBOL(dsl_dataset_hold);
2911 EXPORT_SYMBOL(dsl_dataset_hold_obj);
2912 EXPORT_SYMBOL(dsl_dataset_own);
2913 EXPORT_SYMBOL(dsl_dataset_own_obj);
2914 EXPORT_SYMBOL(dsl_dataset_name);
2915 EXPORT_SYMBOL(dsl_dataset_rele);
2916 EXPORT_SYMBOL(dsl_dataset_disown);
2917 EXPORT_SYMBOL(dsl_dataset_tryown);
2918 EXPORT_SYMBOL(dsl_dataset_create_sync);
2919 EXPORT_SYMBOL(dsl_dataset_create_sync_dd);
2920 EXPORT_SYMBOL(dsl_dataset_snapshot_check);
2921 EXPORT_SYMBOL(dsl_dataset_snapshot_sync);
2922 EXPORT_SYMBOL(dsl_dataset_promote);
2923 EXPORT_SYMBOL(dsl_dataset_user_hold);
2924 EXPORT_SYMBOL(dsl_dataset_user_release);
2925 EXPORT_SYMBOL(dsl_dataset_get_holds);
2926 EXPORT_SYMBOL(dsl_dataset_get_blkptr);
2927 EXPORT_SYMBOL(dsl_dataset_set_blkptr);
2928 EXPORT_SYMBOL(dsl_dataset_get_spa);
2929 EXPORT_SYMBOL(dsl_dataset_modified_since_lastsnap);
2930 EXPORT_SYMBOL(dsl_dataset_space_written);
2931 EXPORT_SYMBOL(dsl_dataset_space_wouldfree);
2932 EXPORT_SYMBOL(dsl_dataset_sync);
2933 EXPORT_SYMBOL(dsl_dataset_block_born);
2934 EXPORT_SYMBOL(dsl_dataset_block_kill);
2935 EXPORT_SYMBOL(dsl_dataset_block_freeable);
2936 EXPORT_SYMBOL(dsl_dataset_prev_snap_txg);
2937 EXPORT_SYMBOL(dsl_dataset_dirty);
2938 EXPORT_SYMBOL(dsl_dataset_stats);
2939 EXPORT_SYMBOL(dsl_dataset_fast_stat);
2940 EXPORT_SYMBOL(dsl_dataset_space);
2941 EXPORT_SYMBOL(dsl_dataset_fsid_guid);
2942 EXPORT_SYMBOL(dsl_dsobj_to_dsname);
2943 EXPORT_SYMBOL(dsl_dataset_check_quota);
2944 EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl);
2945 EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl);