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 * Portions Copyright (c) 2011 Martin Matuska <mm@FreeBSD.org>
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 = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
87 int compressed = BP_GET_PSIZE(bp);
88 int uncompressed = BP_GET_UCSIZE(bp);
91 dprintf_bp(bp, "ds=%p", ds);
93 ASSERT(dmu_tx_is_syncing(tx));
94 /* It could have been compressed away to nothing */
97 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
98 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
100 dsl_pool_mos_diduse_space(tx->tx_pool,
101 used, compressed, uncompressed);
104 dmu_buf_will_dirty(ds->ds_dbuf, tx);
106 mutex_enter(&ds->ds_dir->dd_lock);
107 mutex_enter(&ds->ds_lock);
108 delta = parent_delta(ds, used);
109 ds->ds_phys->ds_referenced_bytes += used;
110 ds->ds_phys->ds_compressed_bytes += compressed;
111 ds->ds_phys->ds_uncompressed_bytes += uncompressed;
112 ds->ds_phys->ds_unique_bytes += used;
113 mutex_exit(&ds->ds_lock);
114 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
115 compressed, uncompressed, tx);
116 dsl_dir_transfer_space(ds->ds_dir, used - delta,
117 DD_USED_REFRSRV, DD_USED_HEAD, tx);
118 mutex_exit(&ds->ds_dir->dd_lock);
122 dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
128 ASSERT(dmu_tx_is_syncing(tx));
129 ASSERT(bp->blk_birth <= tx->tx_txg);
131 int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
132 int compressed = BP_GET_PSIZE(bp);
133 int uncompressed = BP_GET_UCSIZE(bp);
137 dsl_free(tx->tx_pool, tx->tx_txg, bp);
138 dsl_pool_mos_diduse_space(tx->tx_pool,
139 -used, -compressed, -uncompressed);
142 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
144 ASSERT(!dsl_dataset_is_snapshot(ds));
145 dmu_buf_will_dirty(ds->ds_dbuf, tx);
147 if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
150 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
151 dsl_free(tx->tx_pool, tx->tx_txg, bp);
153 mutex_enter(&ds->ds_dir->dd_lock);
154 mutex_enter(&ds->ds_lock);
155 ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
156 !DS_UNIQUE_IS_ACCURATE(ds));
157 delta = parent_delta(ds, -used);
158 ds->ds_phys->ds_unique_bytes -= used;
159 mutex_exit(&ds->ds_lock);
160 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
161 delta, -compressed, -uncompressed, tx);
162 dsl_dir_transfer_space(ds->ds_dir, -used - delta,
163 DD_USED_REFRSRV, DD_USED_HEAD, tx);
164 mutex_exit(&ds->ds_dir->dd_lock);
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 if (mutex_owned(&ds->ds_lock))
270 mutex_exit(&ds->ds_lock);
271 mutex_destroy(&ds->ds_lock);
272 if (mutex_owned(&ds->ds_opening_lock))
273 mutex_exit(&ds->ds_opening_lock);
274 mutex_destroy(&ds->ds_opening_lock);
275 refcount_destroy(&ds->ds_longholds);
277 kmem_free(ds, sizeof (dsl_dataset_t));
281 dsl_dataset_get_snapname(dsl_dataset_t *ds)
283 dsl_dataset_phys_t *headphys;
286 dsl_pool_t *dp = ds->ds_dir->dd_pool;
287 objset_t *mos = dp->dp_meta_objset;
289 if (ds->ds_snapname[0])
291 if (ds->ds_phys->ds_next_snap_obj == 0)
294 err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
298 headphys = headdbuf->db_data;
299 err = zap_value_search(dp->dp_meta_objset,
300 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
301 dmu_buf_rele(headdbuf, FTAG);
306 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
308 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
309 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
313 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
318 err = zap_lookup_norm(mos, snapobj, name, 8, 1,
319 value, mt, NULL, 0, NULL);
320 if (err == ENOTSUP && mt == MT_FIRST)
321 err = zap_lookup(mos, snapobj, name, 8, 1, value);
326 dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx)
328 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
329 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
333 dsl_dir_snap_cmtime_update(ds->ds_dir);
335 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
340 err = zap_remove_norm(mos, snapobj, name, mt, tx);
341 if (err == ENOTSUP && mt == MT_FIRST)
342 err = zap_remove(mos, snapobj, name, tx);
347 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
350 objset_t *mos = dp->dp_meta_objset;
354 dmu_object_info_t doi;
356 ASSERT(dsl_pool_config_held(dp));
358 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
362 /* Make sure dsobj has the correct object type. */
363 dmu_object_info_from_db(dbuf, &doi);
364 if (doi.doi_type != DMU_OT_DSL_DATASET) {
365 dmu_buf_rele(dbuf, tag);
366 return (SET_ERROR(EINVAL));
369 ds = dmu_buf_get_user(dbuf);
371 dsl_dataset_t *winner = NULL;
373 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
375 ds->ds_object = dsobj;
376 ds->ds_phys = dbuf->db_data;
378 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
379 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
380 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
381 refcount_create(&ds->ds_longholds);
383 bplist_create(&ds->ds_pending_deadlist);
384 dsl_deadlist_open(&ds->ds_deadlist,
385 mos, ds->ds_phys->ds_deadlist_obj);
387 list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
388 offsetof(dmu_sendarg_t, dsa_link));
391 err = dsl_dir_hold_obj(dp,
392 ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
395 mutex_destroy(&ds->ds_lock);
396 mutex_destroy(&ds->ds_opening_lock);
397 refcount_destroy(&ds->ds_longholds);
398 bplist_destroy(&ds->ds_pending_deadlist);
399 dsl_deadlist_close(&ds->ds_deadlist);
400 kmem_free(ds, sizeof (dsl_dataset_t));
401 dmu_buf_rele(dbuf, tag);
405 if (!dsl_dataset_is_snapshot(ds)) {
406 ds->ds_snapname[0] = '\0';
407 if (ds->ds_phys->ds_prev_snap_obj != 0) {
408 err = dsl_dataset_hold_obj(dp,
409 ds->ds_phys->ds_prev_snap_obj,
413 if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
414 err = dsl_dataset_get_snapname(ds);
415 if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
417 ds->ds_dir->dd_pool->dp_meta_objset,
418 ds->ds_phys->ds_userrefs_obj,
423 if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
424 err = dsl_prop_get_int_ds(ds,
425 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
428 err = dsl_prop_get_int_ds(ds,
429 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
433 ds->ds_reserved = ds->ds_quota = 0;
436 if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds,
437 &ds->ds_phys, dsl_dataset_evict)) != NULL) {
438 bplist_destroy(&ds->ds_pending_deadlist);
439 dsl_deadlist_close(&ds->ds_deadlist);
441 dsl_dataset_rele(ds->ds_prev, ds);
442 dsl_dir_rele(ds->ds_dir, ds);
443 mutex_destroy(&ds->ds_lock);
444 mutex_destroy(&ds->ds_opening_lock);
445 refcount_destroy(&ds->ds_longholds);
446 kmem_free(ds, sizeof (dsl_dataset_t));
448 dmu_buf_rele(dbuf, tag);
454 unique_insert(ds->ds_phys->ds_fsid_guid);
457 ASSERT3P(ds->ds_dbuf, ==, dbuf);
458 ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
459 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
460 spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
461 dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
467 dsl_dataset_hold(dsl_pool_t *dp, const char *name,
468 void *tag, dsl_dataset_t **dsp)
471 const char *snapname;
475 err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
479 ASSERT(dsl_pool_config_held(dp));
480 obj = dd->dd_phys->dd_head_dataset_obj;
482 err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
484 err = SET_ERROR(ENOENT);
486 /* we may be looking for a snapshot */
487 if (err == 0 && snapname != NULL) {
490 if (*snapname++ != '@') {
491 dsl_dataset_rele(*dsp, tag);
492 dsl_dir_rele(dd, FTAG);
493 return (SET_ERROR(ENOENT));
496 dprintf("looking for snapshot '%s'\n", snapname);
497 err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
499 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
500 dsl_dataset_rele(*dsp, tag);
503 mutex_enter(&ds->ds_lock);
504 if (ds->ds_snapname[0] == 0)
505 (void) strlcpy(ds->ds_snapname, snapname,
506 sizeof (ds->ds_snapname));
507 mutex_exit(&ds->ds_lock);
512 dsl_dir_rele(dd, FTAG);
517 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
518 void *tag, dsl_dataset_t **dsp)
520 int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
523 if (!dsl_dataset_tryown(*dsp, tag)) {
524 dsl_dataset_rele(*dsp, tag);
526 return (SET_ERROR(EBUSY));
532 dsl_dataset_own(dsl_pool_t *dp, const char *name,
533 void *tag, dsl_dataset_t **dsp)
535 int err = dsl_dataset_hold(dp, name, tag, dsp);
538 if (!dsl_dataset_tryown(*dsp, tag)) {
539 dsl_dataset_rele(*dsp, tag);
540 return (SET_ERROR(EBUSY));
546 * See the comment above dsl_pool_hold() for details. In summary, a long
547 * hold is used to prevent destruction of a dataset while the pool hold
548 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
550 * The dataset and pool must be held when this function is called. After it
551 * is called, the pool hold may be released while the dataset is still held
555 dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
557 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
558 (void) refcount_add(&ds->ds_longholds, tag);
562 dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
564 (void) refcount_remove(&ds->ds_longholds, tag);
567 /* Return B_TRUE if there are any long holds on this dataset. */
569 dsl_dataset_long_held(dsl_dataset_t *ds)
571 return (!refcount_is_zero(&ds->ds_longholds));
575 dsl_dataset_name(dsl_dataset_t *ds, char *name)
578 (void) strcpy(name, "mos");
580 dsl_dir_name(ds->ds_dir, name);
581 VERIFY0(dsl_dataset_get_snapname(ds));
582 if (ds->ds_snapname[0]) {
583 (void) strcat(name, "@");
585 * We use a "recursive" mutex so that we
586 * can call dprintf_ds() with ds_lock held.
588 if (!MUTEX_HELD(&ds->ds_lock)) {
589 mutex_enter(&ds->ds_lock);
590 (void) strcat(name, ds->ds_snapname);
591 mutex_exit(&ds->ds_lock);
593 (void) strcat(name, ds->ds_snapname);
600 dsl_dataset_namelen(dsl_dataset_t *ds)
605 result = 3; /* "mos" */
607 result = dsl_dir_namelen(ds->ds_dir);
608 VERIFY0(dsl_dataset_get_snapname(ds));
609 if (ds->ds_snapname[0]) {
610 ++result; /* adding one for the @-sign */
611 if (!MUTEX_HELD(&ds->ds_lock)) {
612 mutex_enter(&ds->ds_lock);
613 result += strlen(ds->ds_snapname);
614 mutex_exit(&ds->ds_lock);
616 result += strlen(ds->ds_snapname);
625 dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
627 dmu_buf_rele(ds->ds_dbuf, tag);
631 dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
633 ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL);
635 mutex_enter(&ds->ds_lock);
637 mutex_exit(&ds->ds_lock);
638 dsl_dataset_long_rele(ds, tag);
639 if (ds->ds_dbuf != NULL)
640 dsl_dataset_rele(ds, tag);
642 dsl_dataset_evict(NULL, ds);
646 dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
648 boolean_t gotit = FALSE;
650 mutex_enter(&ds->ds_lock);
651 if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
653 dsl_dataset_long_hold(ds, tag);
656 mutex_exit(&ds->ds_lock);
661 dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
662 uint64_t flags, dmu_tx_t *tx)
664 dsl_pool_t *dp = dd->dd_pool;
666 dsl_dataset_phys_t *dsphys;
668 objset_t *mos = dp->dp_meta_objset;
671 origin = dp->dp_origin_snap;
673 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
674 ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
675 ASSERT(dmu_tx_is_syncing(tx));
676 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
678 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
679 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
680 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
681 dmu_buf_will_dirty(dbuf, tx);
682 dsphys = dbuf->db_data;
683 bzero(dsphys, sizeof (dsl_dataset_phys_t));
684 dsphys->ds_dir_obj = dd->dd_object;
685 dsphys->ds_flags = flags;
686 dsphys->ds_fsid_guid = unique_create();
688 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
689 sizeof (dsphys->ds_guid));
690 } while (dsphys->ds_guid == 0);
691 dsphys->ds_snapnames_zapobj =
692 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
694 dsphys->ds_creation_time = gethrestime_sec();
695 dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
697 if (origin == NULL) {
698 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
700 dsl_dataset_t *ohds; /* head of the origin snapshot */
702 dsphys->ds_prev_snap_obj = origin->ds_object;
703 dsphys->ds_prev_snap_txg =
704 origin->ds_phys->ds_creation_txg;
705 dsphys->ds_referenced_bytes =
706 origin->ds_phys->ds_referenced_bytes;
707 dsphys->ds_compressed_bytes =
708 origin->ds_phys->ds_compressed_bytes;
709 dsphys->ds_uncompressed_bytes =
710 origin->ds_phys->ds_uncompressed_bytes;
711 dsphys->ds_bp = origin->ds_phys->ds_bp;
712 dsphys->ds_flags |= origin->ds_phys->ds_flags;
714 dmu_buf_will_dirty(origin->ds_dbuf, tx);
715 origin->ds_phys->ds_num_children++;
717 VERIFY0(dsl_dataset_hold_obj(dp,
718 origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
719 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
720 dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
721 dsl_dataset_rele(ohds, FTAG);
723 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
724 if (origin->ds_phys->ds_next_clones_obj == 0) {
725 origin->ds_phys->ds_next_clones_obj =
727 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
729 VERIFY0(zap_add_int(mos,
730 origin->ds_phys->ds_next_clones_obj, dsobj, tx));
733 dmu_buf_will_dirty(dd->dd_dbuf, tx);
734 dd->dd_phys->dd_origin_obj = origin->ds_object;
735 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
736 if (origin->ds_dir->dd_phys->dd_clones == 0) {
737 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
738 origin->ds_dir->dd_phys->dd_clones =
740 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
742 VERIFY0(zap_add_int(mos,
743 origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
747 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
748 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
750 dmu_buf_rele(dbuf, FTAG);
752 dmu_buf_will_dirty(dd->dd_dbuf, tx);
753 dd->dd_phys->dd_head_dataset_obj = dsobj;
759 dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
763 VERIFY0(dmu_objset_from_ds(ds, &os));
764 bzero(&os->os_zil_header, sizeof (os->os_zil_header));
765 dsl_dataset_dirty(ds, tx);
769 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
770 dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
772 dsl_pool_t *dp = pdd->dd_pool;
773 uint64_t dsobj, ddobj;
776 ASSERT(dmu_tx_is_syncing(tx));
777 ASSERT(lastname[0] != '@');
779 ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
780 VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
782 dsobj = dsl_dataset_create_sync_dd(dd, origin,
783 flags & ~DS_CREATE_FLAG_NODIRTY, tx);
785 dsl_deleg_set_create_perms(dd, tx, cr);
787 dsl_dir_rele(dd, FTAG);
790 * If we are creating a clone, make sure we zero out any stale
791 * data from the origin snapshots zil header.
793 if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
796 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
797 dsl_dataset_zero_zil(ds, tx);
798 dsl_dataset_rele(ds, FTAG);
805 /* FreeBSD ioctl compat begin */
808 const char *snapname;
812 dsl_check_snap_cb(const char *name, void *arg)
814 struct destroyarg *da = arg;
818 dsname = kmem_asprintf("%s@%s", name, da->snapname);
819 fnvlist_add_boolean(da->nvl, dsname);
820 kmem_free(dsname, strlen(dsname) + 1);
826 dmu_get_recursive_snaps_nvl(char *fsname, const char *snapname,
829 struct destroyarg *da;
832 da = kmem_zalloc(sizeof (struct destroyarg), KM_SLEEP);
834 da->snapname = snapname;
835 err = dmu_objset_find(fsname, dsl_check_snap_cb, da,
837 kmem_free(da, sizeof (struct destroyarg));
841 /* FreeBSD ioctl compat end */
842 #endif /* __FreeBSD__ */
845 * The unique space in the head dataset can be calculated by subtracting
846 * the space used in the most recent snapshot, that is still being used
847 * in this file system, from the space currently in use. To figure out
848 * the space in the most recent snapshot still in use, we need to take
849 * the total space used in the snapshot and subtract out the space that
850 * has been freed up since the snapshot was taken.
853 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
856 uint64_t dlused, dlcomp, dluncomp;
858 ASSERT(!dsl_dataset_is_snapshot(ds));
860 if (ds->ds_phys->ds_prev_snap_obj != 0)
861 mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
865 dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
867 ASSERT3U(dlused, <=, mrs_used);
868 ds->ds_phys->ds_unique_bytes =
869 ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
871 if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
872 SPA_VERSION_UNIQUE_ACCURATE)
873 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
877 dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
880 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
884 ASSERT(ds->ds_phys->ds_num_children >= 2);
885 err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
887 * The err should not be ENOENT, but a bug in a previous version
888 * of the code could cause upgrade_clones_cb() to not set
889 * ds_next_snap_obj when it should, leading to a missing entry.
890 * If we knew that the pool was created after
891 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
892 * ENOENT. However, at least we can check that we don't have
893 * too many entries in the next_clones_obj even after failing to
898 ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
900 ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
905 dsl_dataset_get_blkptr(dsl_dataset_t *ds)
907 return (&ds->ds_phys->ds_bp);
911 dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
913 ASSERT(dmu_tx_is_syncing(tx));
914 /* If it's the meta-objset, set dp_meta_rootbp */
916 tx->tx_pool->dp_meta_rootbp = *bp;
918 dmu_buf_will_dirty(ds->ds_dbuf, tx);
919 ds->ds_phys->ds_bp = *bp;
924 dsl_dataset_get_spa(dsl_dataset_t *ds)
926 return (ds->ds_dir->dd_pool->dp_spa);
930 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
934 if (ds == NULL) /* this is the meta-objset */
937 ASSERT(ds->ds_objset != NULL);
939 if (ds->ds_phys->ds_next_snap_obj != 0)
940 panic("dirtying snapshot!");
942 dp = ds->ds_dir->dd_pool;
944 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
945 /* up the hold count until we can be written out */
946 dmu_buf_add_ref(ds->ds_dbuf, ds);
951 dsl_dataset_is_dirty(dsl_dataset_t *ds)
953 for (int t = 0; t < TXG_SIZE; t++) {
954 if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
962 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
966 if (!dmu_tx_is_syncing(tx))
970 * If there's an fs-only reservation, any blocks that might become
971 * owned by the snapshot dataset must be accommodated by space
972 * outside of the reservation.
974 ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
975 asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
976 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
977 return (SET_ERROR(ENOSPC));
980 * Propagate any reserved space for this snapshot to other
981 * snapshot checks in this sync group.
984 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
989 typedef struct dsl_dataset_snapshot_arg {
990 nvlist_t *ddsa_snaps;
991 nvlist_t *ddsa_props;
992 nvlist_t *ddsa_errors;
993 } dsl_dataset_snapshot_arg_t;
996 dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1002 ds->ds_trysnap_txg = tx->tx_txg;
1004 if (!dmu_tx_is_syncing(tx))
1008 * We don't allow multiple snapshots of the same txg. If there
1009 * is already one, try again.
1011 if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
1012 return (SET_ERROR(EAGAIN));
1015 * Check for conflicting snapshot name.
1017 error = dsl_dataset_snap_lookup(ds, snapname, &value);
1019 return (SET_ERROR(EEXIST));
1020 if (error != ENOENT)
1023 error = dsl_dataset_snapshot_reserve_space(ds, tx);
1031 dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1033 dsl_dataset_snapshot_arg_t *ddsa = arg;
1034 dsl_pool_t *dp = dmu_tx_pool(tx);
1038 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1039 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1043 char dsname[MAXNAMELEN];
1045 name = nvpair_name(pair);
1046 if (strlen(name) >= MAXNAMELEN)
1047 error = SET_ERROR(ENAMETOOLONG);
1049 atp = strchr(name, '@');
1051 error = SET_ERROR(EINVAL);
1053 (void) strlcpy(dsname, name, atp - name + 1);
1056 error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1058 error = dsl_dataset_snapshot_check_impl(ds,
1060 dsl_dataset_rele(ds, FTAG);
1064 if (ddsa->ddsa_errors != NULL) {
1065 fnvlist_add_int32(ddsa->ddsa_errors,
1075 dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1078 static zil_header_t zero_zil;
1080 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1082 dsl_dataset_phys_t *dsphys;
1083 uint64_t dsobj, crtxg;
1084 objset_t *mos = dp->dp_meta_objset;
1087 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1090 * If we are on an old pool, the zil must not be active, in which
1091 * case it will be zeroed. Usually zil_suspend() accomplishes this.
1093 ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1094 dmu_objset_from_ds(ds, &os) != 0 ||
1095 bcmp(&os->os_phys->os_zil_header, &zero_zil,
1096 sizeof (zero_zil)) == 0);
1100 * The origin's ds_creation_txg has to be < TXG_INITIAL
1102 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1107 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1108 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1109 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1110 dmu_buf_will_dirty(dbuf, tx);
1111 dsphys = dbuf->db_data;
1112 bzero(dsphys, sizeof (dsl_dataset_phys_t));
1113 dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1114 dsphys->ds_fsid_guid = unique_create();
1116 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1117 sizeof (dsphys->ds_guid));
1118 } while (dsphys->ds_guid == 0);
1119 dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1120 dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1121 dsphys->ds_next_snap_obj = ds->ds_object;
1122 dsphys->ds_num_children = 1;
1123 dsphys->ds_creation_time = gethrestime_sec();
1124 dsphys->ds_creation_txg = crtxg;
1125 dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1126 dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
1127 dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1128 dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
1129 dsphys->ds_flags = ds->ds_phys->ds_flags;
1130 dsphys->ds_bp = ds->ds_phys->ds_bp;
1131 dmu_buf_rele(dbuf, FTAG);
1133 ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
1135 uint64_t next_clones_obj =
1136 ds->ds_prev->ds_phys->ds_next_clones_obj;
1137 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1139 ds->ds_prev->ds_phys->ds_num_children > 1);
1140 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1141 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1142 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1143 ds->ds_prev->ds_phys->ds_creation_txg);
1144 ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1145 } else if (next_clones_obj != 0) {
1146 dsl_dataset_remove_from_next_clones(ds->ds_prev,
1147 dsphys->ds_next_snap_obj, tx);
1148 VERIFY0(zap_add_int(mos,
1149 next_clones_obj, dsobj, tx));
1154 * If we have a reference-reservation on this dataset, we will
1155 * need to increase the amount of refreservation being charged
1156 * since our unique space is going to zero.
1158 if (ds->ds_reserved) {
1160 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1161 delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
1162 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1166 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1167 ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
1168 UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
1169 dsl_deadlist_close(&ds->ds_deadlist);
1170 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1171 dsl_deadlist_add_key(&ds->ds_deadlist,
1172 ds->ds_phys->ds_prev_snap_txg, tx);
1174 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1175 ds->ds_phys->ds_prev_snap_obj = dsobj;
1176 ds->ds_phys->ds_prev_snap_txg = crtxg;
1177 ds->ds_phys->ds_unique_bytes = 0;
1178 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1179 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1181 VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1182 snapname, 8, 1, &dsobj, tx));
1185 dsl_dataset_rele(ds->ds_prev, ds);
1186 VERIFY0(dsl_dataset_hold_obj(dp,
1187 ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
1189 dsl_scan_ds_snapshotted(ds, tx);
1191 dsl_dir_snap_cmtime_update(ds->ds_dir);
1193 spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1197 dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1199 dsl_dataset_snapshot_arg_t *ddsa = arg;
1200 dsl_pool_t *dp = dmu_tx_pool(tx);
1203 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1204 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1207 char dsname[MAXNAMELEN];
1209 name = nvpair_name(pair);
1210 atp = strchr(name, '@');
1211 (void) strlcpy(dsname, name, atp - name + 1);
1212 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1214 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1215 if (ddsa->ddsa_props != NULL) {
1216 dsl_props_set_sync_impl(ds->ds_prev,
1217 ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1219 dsl_dataset_rele(ds, FTAG);
1224 * The snapshots must all be in the same pool.
1225 * All-or-nothing: if there are any failures, nothing will be modified.
1228 dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1230 dsl_dataset_snapshot_arg_t ddsa;
1232 boolean_t needsuspend;
1236 nvlist_t *suspended = NULL;
1238 pair = nvlist_next_nvpair(snaps, NULL);
1241 firstname = nvpair_name(pair);
1243 error = spa_open(firstname, &spa, FTAG);
1246 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1247 spa_close(spa, FTAG);
1250 suspended = fnvlist_alloc();
1251 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1252 pair = nvlist_next_nvpair(snaps, pair)) {
1253 char fsname[MAXNAMELEN];
1254 char *snapname = nvpair_name(pair);
1258 atp = strchr(snapname, '@');
1260 error = SET_ERROR(EINVAL);
1263 (void) strlcpy(fsname, snapname, atp - snapname + 1);
1265 error = zil_suspend(fsname, &cookie);
1268 fnvlist_add_uint64(suspended, fsname,
1273 ddsa.ddsa_snaps = snaps;
1274 ddsa.ddsa_props = props;
1275 ddsa.ddsa_errors = errors;
1278 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1279 dsl_dataset_snapshot_sync, &ddsa,
1280 fnvlist_num_pairs(snaps) * 3);
1283 if (suspended != NULL) {
1284 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1285 pair = nvlist_next_nvpair(suspended, pair)) {
1286 zil_resume((void *)(uintptr_t)
1287 fnvpair_value_uint64(pair));
1289 fnvlist_free(suspended);
1295 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1296 pair = nvlist_next_nvpair(snaps, pair)) {
1297 char *snapname = nvpair_name(pair);
1298 zvol_create_minors(snapname);
1306 typedef struct dsl_dataset_snapshot_tmp_arg {
1307 const char *ddsta_fsname;
1308 const char *ddsta_snapname;
1309 minor_t ddsta_cleanup_minor;
1310 const char *ddsta_htag;
1311 } dsl_dataset_snapshot_tmp_arg_t;
1314 dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1316 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1317 dsl_pool_t *dp = dmu_tx_pool(tx);
1321 error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1325 error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, tx);
1327 dsl_dataset_rele(ds, FTAG);
1331 if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
1332 dsl_dataset_rele(ds, FTAG);
1333 return (SET_ERROR(ENOTSUP));
1335 error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
1338 dsl_dataset_rele(ds, FTAG);
1342 dsl_dataset_rele(ds, FTAG);
1347 dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
1349 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1350 dsl_pool_t *dp = dmu_tx_pool(tx);
1353 VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
1355 dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
1356 dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
1357 ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
1358 dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
1360 dsl_dataset_rele(ds, FTAG);
1364 dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
1365 minor_t cleanup_minor, const char *htag)
1367 dsl_dataset_snapshot_tmp_arg_t ddsta;
1370 boolean_t needsuspend;
1373 ddsta.ddsta_fsname = fsname;
1374 ddsta.ddsta_snapname = snapname;
1375 ddsta.ddsta_cleanup_minor = cleanup_minor;
1376 ddsta.ddsta_htag = htag;
1378 error = spa_open(fsname, &spa, FTAG);
1381 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1382 spa_close(spa, FTAG);
1385 error = zil_suspend(fsname, &cookie);
1390 error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
1391 dsl_dataset_snapshot_tmp_sync, &ddsta, 3);
1400 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1402 ASSERT(dmu_tx_is_syncing(tx));
1403 ASSERT(ds->ds_objset != NULL);
1404 ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1407 * in case we had to change ds_fsid_guid when we opened it,
1410 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1411 ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
1413 dmu_objset_sync(ds->ds_objset, zio, tx);
1417 get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1420 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1423 nvlist_t *propval = fnvlist_alloc();
1424 nvlist_t *val = fnvlist_alloc();
1426 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1429 * There may be missing entries in ds_next_clones_obj
1430 * due to a bug in a previous version of the code.
1431 * Only trust it if it has the right number of entries.
1433 if (ds->ds_phys->ds_next_clones_obj != 0) {
1434 ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
1437 if (count != ds->ds_phys->ds_num_children - 1)
1439 for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
1440 zap_cursor_retrieve(&zc, &za) == 0;
1441 zap_cursor_advance(&zc)) {
1442 dsl_dataset_t *clone;
1443 char buf[ZFS_MAXNAMELEN];
1444 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1445 za.za_first_integer, FTAG, &clone));
1446 dsl_dir_name(clone->ds_dir, buf);
1447 fnvlist_add_boolean(val, buf);
1448 dsl_dataset_rele(clone, FTAG);
1450 zap_cursor_fini(&zc);
1451 fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1452 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
1455 nvlist_free(propval);
1459 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1461 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1462 uint64_t refd, avail, uobjs, aobjs, ratio;
1464 ASSERT(dsl_pool_config_held(dp));
1466 ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
1467 (ds->ds_phys->ds_uncompressed_bytes * 100 /
1468 ds->ds_phys->ds_compressed_bytes);
1470 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
1471 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1472 ds->ds_phys->ds_uncompressed_bytes);
1474 if (dsl_dataset_is_snapshot(ds)) {
1475 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
1476 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1477 ds->ds_phys->ds_unique_bytes);
1478 get_clones_stat(ds, nv);
1480 dsl_dir_stats(ds->ds_dir, nv);
1483 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1484 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1485 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1487 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1488 ds->ds_phys->ds_creation_time);
1489 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1490 ds->ds_phys->ds_creation_txg);
1491 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1493 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1495 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1496 ds->ds_phys->ds_guid);
1497 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1498 ds->ds_phys->ds_unique_bytes);
1499 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
1501 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
1503 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1504 DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1506 if (ds->ds_phys->ds_prev_snap_obj != 0) {
1507 uint64_t written, comp, uncomp;
1508 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1509 dsl_dataset_t *prev;
1511 int err = dsl_dataset_hold_obj(dp,
1512 ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1514 err = dsl_dataset_space_written(prev, ds, &written,
1516 dsl_dataset_rele(prev, FTAG);
1518 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
1526 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1528 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1529 ASSERT(dsl_pool_config_held(dp));
1531 stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1532 stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
1533 stat->dds_guid = ds->ds_phys->ds_guid;
1534 stat->dds_origin[0] = '\0';
1535 if (dsl_dataset_is_snapshot(ds)) {
1536 stat->dds_is_snapshot = B_TRUE;
1537 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1539 stat->dds_is_snapshot = B_FALSE;
1540 stat->dds_num_clones = 0;
1542 if (dsl_dir_is_clone(ds->ds_dir)) {
1545 VERIFY0(dsl_dataset_hold_obj(dp,
1546 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
1547 dsl_dataset_name(ods, stat->dds_origin);
1548 dsl_dataset_rele(ods, FTAG);
1554 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1556 return (ds->ds_fsid_guid);
1560 dsl_dataset_space(dsl_dataset_t *ds,
1561 uint64_t *refdbytesp, uint64_t *availbytesp,
1562 uint64_t *usedobjsp, uint64_t *availobjsp)
1564 *refdbytesp = ds->ds_phys->ds_referenced_bytes;
1565 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1566 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
1567 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
1568 if (ds->ds_quota != 0) {
1570 * Adjust available bytes according to refquota
1572 if (*refdbytesp < ds->ds_quota)
1573 *availbytesp = MIN(*availbytesp,
1574 ds->ds_quota - *refdbytesp);
1578 *usedobjsp = ds->ds_phys->ds_bp.blk_fill;
1579 *availobjsp = DN_MAX_OBJECT - *usedobjsp;
1583 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
1585 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1587 ASSERT(dsl_pool_config_held(dp));
1588 if (ds->ds_prev == NULL)
1590 if (ds->ds_phys->ds_bp.blk_birth >
1591 ds->ds_prev->ds_phys->ds_creation_txg) {
1592 objset_t *os, *os_prev;
1594 * It may be that only the ZIL differs, because it was
1595 * reset in the head. Don't count that as being
1598 if (dmu_objset_from_ds(ds, &os) != 0)
1600 if (dmu_objset_from_ds(ds->ds_prev, &os_prev) != 0)
1602 return (bcmp(&os->os_phys->os_meta_dnode,
1603 &os_prev->os_phys->os_meta_dnode,
1604 sizeof (os->os_phys->os_meta_dnode)) != 0);
1609 typedef struct dsl_dataset_rename_snapshot_arg {
1610 const char *ddrsa_fsname;
1611 const char *ddrsa_oldsnapname;
1612 const char *ddrsa_newsnapname;
1613 boolean_t ddrsa_recursive;
1615 } dsl_dataset_rename_snapshot_arg_t;
1619 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
1620 dsl_dataset_t *hds, void *arg)
1622 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1626 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1628 /* ignore nonexistent snapshots */
1629 return (error == ENOENT ? 0 : error);
1632 /* new name should not exist */
1633 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
1635 error = SET_ERROR(EEXIST);
1636 else if (error == ENOENT)
1639 /* dataset name + 1 for the "@" + the new snapshot name must fit */
1640 if (dsl_dir_namelen(hds->ds_dir) + 1 +
1641 strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1642 error = SET_ERROR(ENAMETOOLONG);
1648 dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
1650 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1651 dsl_pool_t *dp = dmu_tx_pool(tx);
1655 error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
1659 if (ddrsa->ddrsa_recursive) {
1660 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1661 dsl_dataset_rename_snapshot_check_impl, ddrsa,
1664 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
1666 dsl_dataset_rele(hds, FTAG);
1671 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
1672 dsl_dataset_t *hds, void *arg)
1676 char *oldname, *newname;
1679 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1682 dmu_tx_t *tx = ddrsa->ddrsa_tx;
1685 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1686 ASSERT(error == 0 || error == ENOENT);
1687 if (error == ENOENT) {
1688 /* ignore nonexistent snapshots */
1692 VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
1694 /* log before we change the name */
1695 spa_history_log_internal_ds(ds, "rename", tx,
1696 "-> @%s", ddrsa->ddrsa_newsnapname);
1698 VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx));
1699 mutex_enter(&ds->ds_lock);
1700 (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
1701 mutex_exit(&ds->ds_lock);
1702 VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj,
1703 ds->ds_snapname, 8, 1, &ds->ds_object, tx));
1707 oldname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1708 newname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1709 snprintf(oldname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname,
1710 ddrsa->ddrsa_oldsnapname);
1711 snprintf(newname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname,
1712 ddrsa->ddrsa_newsnapname);
1713 zfsvfs_update_fromname(oldname, newname);
1714 zvol_rename_minors(oldname, newname);
1715 kmem_free(newname, MAXPATHLEN);
1716 kmem_free(oldname, MAXPATHLEN);
1719 dsl_dataset_rele(ds, FTAG);
1725 dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
1727 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1728 dsl_pool_t *dp = dmu_tx_pool(tx);
1731 VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
1732 ddrsa->ddrsa_tx = tx;
1733 if (ddrsa->ddrsa_recursive) {
1734 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1735 dsl_dataset_rename_snapshot_sync_impl, ddrsa,
1738 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
1740 dsl_dataset_rele(hds, FTAG);
1744 dsl_dataset_rename_snapshot(const char *fsname,
1745 const char *oldsnapname, const char *newsnapname, boolean_t recursive)
1747 dsl_dataset_rename_snapshot_arg_t ddrsa;
1749 ddrsa.ddrsa_fsname = fsname;
1750 ddrsa.ddrsa_oldsnapname = oldsnapname;
1751 ddrsa.ddrsa_newsnapname = newsnapname;
1752 ddrsa.ddrsa_recursive = recursive;
1754 return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
1755 dsl_dataset_rename_snapshot_sync, &ddrsa, 1));
1759 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
1761 const char *fsname = arg;
1762 dsl_pool_t *dp = dmu_tx_pool(tx);
1764 int64_t unused_refres_delta;
1767 error = dsl_dataset_hold(dp, fsname, FTAG, &ds);
1771 /* must not be a snapshot */
1772 if (dsl_dataset_is_snapshot(ds)) {
1773 dsl_dataset_rele(ds, FTAG);
1774 return (SET_ERROR(EINVAL));
1777 /* must have a most recent snapshot */
1778 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
1779 dsl_dataset_rele(ds, FTAG);
1780 return (SET_ERROR(EINVAL));
1783 if (dsl_dataset_long_held(ds)) {
1784 dsl_dataset_rele(ds, FTAG);
1785 return (SET_ERROR(EBUSY));
1789 * Check if the snap we are rolling back to uses more than
1792 if (ds->ds_quota != 0 &&
1793 ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) {
1794 dsl_dataset_rele(ds, FTAG);
1795 return (SET_ERROR(EDQUOT));
1799 * When we do the clone swap, we will temporarily use more space
1800 * due to the refreservation (the head will no longer have any
1801 * unique space, so the entire amount of the refreservation will need
1802 * to be free). We will immediately destroy the clone, freeing
1803 * this space, but the freeing happens over many txg's.
1805 unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
1806 ds->ds_phys->ds_unique_bytes);
1808 if (unused_refres_delta > 0 &&
1809 unused_refres_delta >
1810 dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
1811 dsl_dataset_rele(ds, FTAG);
1812 return (SET_ERROR(ENOSPC));
1815 dsl_dataset_rele(ds, FTAG);
1820 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
1822 const char *fsname = arg;
1823 dsl_pool_t *dp = dmu_tx_pool(tx);
1824 dsl_dataset_t *ds, *clone;
1827 VERIFY0(dsl_dataset_hold(dp, fsname, FTAG, &ds));
1829 cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
1830 ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
1832 VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
1834 dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
1835 dsl_dataset_zero_zil(ds, tx);
1837 dsl_destroy_head_sync_impl(clone, tx);
1839 dsl_dataset_rele(clone, FTAG);
1840 dsl_dataset_rele(ds, FTAG);
1844 dsl_dataset_rollback(const char *fsname)
1846 return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
1847 dsl_dataset_rollback_sync, (void *)fsname, 1));
1850 struct promotenode {
1855 typedef struct dsl_dataset_promote_arg {
1856 const char *ddpa_clonename;
1857 dsl_dataset_t *ddpa_clone;
1858 list_t shared_snaps, origin_snaps, clone_snaps;
1859 dsl_dataset_t *origin_origin; /* origin of the origin */
1860 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
1862 } dsl_dataset_promote_arg_t;
1864 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
1865 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
1867 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
1870 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
1872 dsl_dataset_promote_arg_t *ddpa = arg;
1873 dsl_pool_t *dp = dmu_tx_pool(tx);
1875 struct promotenode *snap;
1876 dsl_dataset_t *origin_ds;
1880 err = promote_hold(ddpa, dp, FTAG);
1884 hds = ddpa->ddpa_clone;
1886 if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
1887 promote_rele(ddpa, FTAG);
1888 return (SET_ERROR(EXDEV));
1892 * Compute and check the amount of space to transfer. Since this is
1893 * so expensive, don't do the preliminary check.
1895 if (!dmu_tx_is_syncing(tx)) {
1896 promote_rele(ddpa, FTAG);
1900 snap = list_head(&ddpa->shared_snaps);
1901 origin_ds = snap->ds;
1903 /* compute origin's new unique space */
1904 snap = list_tail(&ddpa->clone_snaps);
1905 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
1906 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
1907 origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
1908 &ddpa->unique, &unused, &unused);
1911 * Walk the snapshots that we are moving
1913 * Compute space to transfer. Consider the incremental changes
1914 * to used by each snapshot:
1915 * (my used) = (prev's used) + (blocks born) - (blocks killed)
1916 * So each snapshot gave birth to:
1917 * (blocks born) = (my used) - (prev's used) + (blocks killed)
1918 * So a sequence would look like:
1919 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
1920 * Which simplifies to:
1921 * uN + kN + kN-1 + ... + k1 + k0
1922 * Note however, if we stop before we reach the ORIGIN we get:
1923 * uN + kN + kN-1 + ... + kM - uM-1
1925 ddpa->used = origin_ds->ds_phys->ds_referenced_bytes;
1926 ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes;
1927 ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
1928 for (snap = list_head(&ddpa->shared_snaps); snap;
1929 snap = list_next(&ddpa->shared_snaps, snap)) {
1930 uint64_t val, dlused, dlcomp, dluncomp;
1931 dsl_dataset_t *ds = snap->ds;
1934 * If there are long holds, we won't be able to evict
1937 if (dsl_dataset_long_held(ds)) {
1938 err = SET_ERROR(EBUSY);
1942 /* Check that the snapshot name does not conflict */
1943 VERIFY0(dsl_dataset_get_snapname(ds));
1944 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
1946 (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
1947 err = SET_ERROR(EEXIST);
1953 /* The very first snapshot does not have a deadlist */
1954 if (ds->ds_phys->ds_prev_snap_obj == 0)
1957 dsl_deadlist_space(&ds->ds_deadlist,
1958 &dlused, &dlcomp, &dluncomp);
1959 ddpa->used += dlused;
1960 ddpa->comp += dlcomp;
1961 ddpa->uncomp += dluncomp;
1965 * If we are a clone of a clone then we never reached ORIGIN,
1966 * so we need to subtract out the clone origin's used space.
1968 if (ddpa->origin_origin) {
1969 ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes;
1970 ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes;
1972 ddpa->origin_origin->ds_phys->ds_uncompressed_bytes;
1975 /* Check that there is enough space here */
1976 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
1982 * Compute the amounts of space that will be used by snapshots
1983 * after the promotion (for both origin and clone). For each,
1984 * it is the amount of space that will be on all of their
1985 * deadlists (that was not born before their new origin).
1987 if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1991 * Note, typically this will not be a clone of a clone,
1992 * so dd_origin_txg will be < TXG_INITIAL, so
1993 * these snaplist_space() -> dsl_deadlist_space_range()
1994 * calls will be fast because they do not have to
1995 * iterate over all bps.
1997 snap = list_head(&ddpa->origin_snaps);
1998 err = snaplist_space(&ddpa->shared_snaps,
1999 snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
2003 err = snaplist_space(&ddpa->clone_snaps,
2004 snap->ds->ds_dir->dd_origin_txg, &space);
2007 ddpa->cloneusedsnap += space;
2009 if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2010 err = snaplist_space(&ddpa->origin_snaps,
2011 origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap);
2017 promote_rele(ddpa, FTAG);
2022 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
2024 dsl_dataset_promote_arg_t *ddpa = arg;
2025 dsl_pool_t *dp = dmu_tx_pool(tx);
2027 struct promotenode *snap;
2028 dsl_dataset_t *origin_ds;
2029 dsl_dataset_t *origin_head;
2031 dsl_dir_t *odd = NULL;
2032 uint64_t oldnext_obj;
2035 VERIFY0(promote_hold(ddpa, dp, FTAG));
2036 hds = ddpa->ddpa_clone;
2038 ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE);
2040 snap = list_head(&ddpa->shared_snaps);
2041 origin_ds = snap->ds;
2044 snap = list_head(&ddpa->origin_snaps);
2045 origin_head = snap->ds;
2048 * We need to explicitly open odd, since origin_ds's dd will be
2051 VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
2054 /* change origin's next snap */
2055 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2056 oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
2057 snap = list_tail(&ddpa->clone_snaps);
2058 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2059 origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
2061 /* change the origin's next clone */
2062 if (origin_ds->ds_phys->ds_next_clones_obj) {
2063 dsl_dataset_remove_from_next_clones(origin_ds,
2064 snap->ds->ds_object, tx);
2065 VERIFY0(zap_add_int(dp->dp_meta_objset,
2066 origin_ds->ds_phys->ds_next_clones_obj,
2071 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2072 ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2073 dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
2074 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2075 dmu_buf_will_dirty(odd->dd_dbuf, tx);
2076 odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
2077 origin_head->ds_dir->dd_origin_txg =
2078 origin_ds->ds_phys->ds_creation_txg;
2080 /* change dd_clone entries */
2081 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2082 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2083 odd->dd_phys->dd_clones, hds->ds_object, tx));
2084 VERIFY0(zap_add_int(dp->dp_meta_objset,
2085 ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2086 hds->ds_object, tx));
2088 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2089 ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2090 origin_head->ds_object, tx));
2091 if (dd->dd_phys->dd_clones == 0) {
2092 dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2093 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2095 VERIFY0(zap_add_int(dp->dp_meta_objset,
2096 dd->dd_phys->dd_clones, origin_head->ds_object, tx));
2099 /* move snapshots to this dir */
2100 for (snap = list_head(&ddpa->shared_snaps); snap;
2101 snap = list_next(&ddpa->shared_snaps, snap)) {
2102 dsl_dataset_t *ds = snap->ds;
2105 * Property callbacks are registered to a particular
2106 * dsl_dir. Since ours is changing, evict the objset
2107 * so that they will be unregistered from the old dsl_dir.
2109 if (ds->ds_objset) {
2110 dmu_objset_evict(ds->ds_objset);
2111 ds->ds_objset = NULL;
2114 /* move snap name entry */
2115 VERIFY0(dsl_dataset_get_snapname(ds));
2116 VERIFY0(dsl_dataset_snap_remove(origin_head,
2117 ds->ds_snapname, tx));
2118 VERIFY0(zap_add(dp->dp_meta_objset,
2119 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
2120 8, 1, &ds->ds_object, tx));
2122 /* change containing dsl_dir */
2123 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2124 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2125 ds->ds_phys->ds_dir_obj = dd->dd_object;
2126 ASSERT3P(ds->ds_dir, ==, odd);
2127 dsl_dir_rele(ds->ds_dir, ds);
2128 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
2129 NULL, ds, &ds->ds_dir));
2131 /* move any clone references */
2132 if (ds->ds_phys->ds_next_clones_obj &&
2133 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2137 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2138 ds->ds_phys->ds_next_clones_obj);
2139 zap_cursor_retrieve(&zc, &za) == 0;
2140 zap_cursor_advance(&zc)) {
2141 dsl_dataset_t *cnds;
2144 if (za.za_first_integer == oldnext_obj) {
2146 * We've already moved the
2147 * origin's reference.
2152 VERIFY0(dsl_dataset_hold_obj(dp,
2153 za.za_first_integer, FTAG, &cnds));
2154 o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2156 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2157 odd->dd_phys->dd_clones, o, tx));
2158 VERIFY0(zap_add_int(dp->dp_meta_objset,
2159 dd->dd_phys->dd_clones, o, tx));
2160 dsl_dataset_rele(cnds, FTAG);
2162 zap_cursor_fini(&zc);
2165 ASSERT(!dsl_prop_hascb(ds));
2169 * Change space accounting.
2170 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2171 * both be valid, or both be 0 (resulting in delta == 0). This
2172 * is true for each of {clone,origin} independently.
2175 delta = ddpa->cloneusedsnap -
2176 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2177 ASSERT3S(delta, >=, 0);
2178 ASSERT3U(ddpa->used, >=, delta);
2179 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2180 dsl_dir_diduse_space(dd, DD_USED_HEAD,
2181 ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
2183 delta = ddpa->originusedsnap -
2184 odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2185 ASSERT3S(delta, <=, 0);
2186 ASSERT3U(ddpa->used, >=, -delta);
2187 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2188 dsl_dir_diduse_space(odd, DD_USED_HEAD,
2189 -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
2191 origin_ds->ds_phys->ds_unique_bytes = ddpa->unique;
2193 /* log history record */
2194 spa_history_log_internal_ds(hds, "promote", tx, "");
2196 dsl_dir_rele(odd, FTAG);
2197 promote_rele(ddpa, FTAG);
2201 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2202 * (exclusive) and last_obj (inclusive). The list will be in reverse
2203 * order (last_obj will be the list_head()). If first_obj == 0, do all
2204 * snapshots back to this dataset's origin.
2207 snaplist_make(dsl_pool_t *dp,
2208 uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2210 uint64_t obj = last_obj;
2212 list_create(l, sizeof (struct promotenode),
2213 offsetof(struct promotenode, link));
2215 while (obj != first_obj) {
2217 struct promotenode *snap;
2220 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
2221 ASSERT(err != ENOENT);
2226 first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
2228 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
2230 list_insert_tail(l, snap);
2231 obj = ds->ds_phys->ds_prev_snap_obj;
2238 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2240 struct promotenode *snap;
2243 for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2244 uint64_t used, comp, uncomp;
2245 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2246 mintxg, UINT64_MAX, &used, &comp, &uncomp);
2253 snaplist_destroy(list_t *l, void *tag)
2255 struct promotenode *snap;
2257 if (l == NULL || !list_link_active(&l->list_head))
2260 while ((snap = list_tail(l)) != NULL) {
2261 list_remove(l, snap);
2262 dsl_dataset_rele(snap->ds, tag);
2263 kmem_free(snap, sizeof (*snap));
2269 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2273 struct promotenode *snap;
2275 error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
2279 dd = ddpa->ddpa_clone->ds_dir;
2281 if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) ||
2282 !dsl_dir_is_clone(dd)) {
2283 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2284 return (SET_ERROR(EINVAL));
2287 error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj,
2288 &ddpa->shared_snaps, tag);
2292 error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
2293 &ddpa->clone_snaps, tag);
2297 snap = list_head(&ddpa->shared_snaps);
2298 ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
2299 error = snaplist_make(dp, dd->dd_phys->dd_origin_obj,
2300 snap->ds->ds_dir->dd_phys->dd_head_dataset_obj,
2301 &ddpa->origin_snaps, tag);
2305 if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
2306 error = dsl_dataset_hold_obj(dp,
2307 snap->ds->ds_dir->dd_phys->dd_origin_obj,
2308 tag, &ddpa->origin_origin);
2314 promote_rele(ddpa, tag);
2319 promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2321 snaplist_destroy(&ddpa->shared_snaps, tag);
2322 snaplist_destroy(&ddpa->clone_snaps, tag);
2323 snaplist_destroy(&ddpa->origin_snaps, tag);
2324 if (ddpa->origin_origin != NULL)
2325 dsl_dataset_rele(ddpa->origin_origin, tag);
2326 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2332 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2333 * in with the name. (It must be at least MAXNAMELEN bytes long.)
2336 dsl_dataset_promote(const char *name, char *conflsnap)
2338 dsl_dataset_promote_arg_t ddpa = { 0 };
2344 * We will modify space proportional to the number of
2345 * snapshots. Compute numsnaps.
2347 error = dmu_objset_hold(name, FTAG, &os);
2350 error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2351 dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps);
2352 dmu_objset_rele(os, FTAG);
2356 ddpa.ddpa_clonename = name;
2357 ddpa.err_ds = conflsnap;
2359 return (dsl_sync_task(name, dsl_dataset_promote_check,
2360 dsl_dataset_promote_sync, &ddpa, 2 + numsnaps));
2364 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
2365 dsl_dataset_t *origin_head, boolean_t force)
2367 int64_t unused_refres_delta;
2369 /* they should both be heads */
2370 if (dsl_dataset_is_snapshot(clone) ||
2371 dsl_dataset_is_snapshot(origin_head))
2372 return (SET_ERROR(EINVAL));
2374 /* the branch point should be just before them */
2375 if (clone->ds_prev != origin_head->ds_prev)
2376 return (SET_ERROR(EINVAL));
2378 /* clone should be the clone (unless they are unrelated) */
2379 if (clone->ds_prev != NULL &&
2380 clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
2381 origin_head->ds_object !=
2382 clone->ds_prev->ds_phys->ds_next_snap_obj)
2383 return (SET_ERROR(EINVAL));
2385 /* the clone should be a child of the origin */
2386 if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2387 return (SET_ERROR(EINVAL));
2389 /* origin_head shouldn't be modified unless 'force' */
2390 if (!force && dsl_dataset_modified_since_lastsnap(origin_head))
2391 return (SET_ERROR(ETXTBSY));
2393 /* origin_head should have no long holds (e.g. is not mounted) */
2394 if (dsl_dataset_long_held(origin_head))
2395 return (SET_ERROR(EBUSY));
2397 /* check amount of any unconsumed refreservation */
2398 unused_refres_delta =
2399 (int64_t)MIN(origin_head->ds_reserved,
2400 origin_head->ds_phys->ds_unique_bytes) -
2401 (int64_t)MIN(origin_head->ds_reserved,
2402 clone->ds_phys->ds_unique_bytes);
2404 if (unused_refres_delta > 0 &&
2405 unused_refres_delta >
2406 dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2407 return (SET_ERROR(ENOSPC));
2409 /* clone can't be over the head's refquota */
2410 if (origin_head->ds_quota != 0 &&
2411 clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota)
2412 return (SET_ERROR(EDQUOT));
2418 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2419 dsl_dataset_t *origin_head, dmu_tx_t *tx)
2421 dsl_pool_t *dp = dmu_tx_pool(tx);
2422 int64_t unused_refres_delta;
2424 ASSERT(clone->ds_reserved == 0);
2425 ASSERT(origin_head->ds_quota == 0 ||
2426 clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota);
2428 dmu_buf_will_dirty(clone->ds_dbuf, tx);
2429 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2431 if (clone->ds_objset != NULL) {
2432 dmu_objset_evict(clone->ds_objset);
2433 clone->ds_objset = NULL;
2436 if (origin_head->ds_objset != NULL) {
2437 dmu_objset_evict(origin_head->ds_objset);
2438 origin_head->ds_objset = NULL;
2441 unused_refres_delta =
2442 (int64_t)MIN(origin_head->ds_reserved,
2443 origin_head->ds_phys->ds_unique_bytes) -
2444 (int64_t)MIN(origin_head->ds_reserved,
2445 clone->ds_phys->ds_unique_bytes);
2448 * Reset origin's unique bytes, if it exists.
2450 if (clone->ds_prev) {
2451 dsl_dataset_t *origin = clone->ds_prev;
2452 uint64_t comp, uncomp;
2454 dmu_buf_will_dirty(origin->ds_dbuf, tx);
2455 dsl_deadlist_space_range(&clone->ds_deadlist,
2456 origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2457 &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
2463 tmp = origin_head->ds_phys->ds_bp;
2464 origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp;
2465 clone->ds_phys->ds_bp = tmp;
2468 /* set dd_*_bytes */
2470 int64_t dused, dcomp, duncomp;
2471 uint64_t cdl_used, cdl_comp, cdl_uncomp;
2472 uint64_t odl_used, odl_comp, odl_uncomp;
2474 ASSERT3U(clone->ds_dir->dd_phys->
2475 dd_used_breakdown[DD_USED_SNAP], ==, 0);
2477 dsl_deadlist_space(&clone->ds_deadlist,
2478 &cdl_used, &cdl_comp, &cdl_uncomp);
2479 dsl_deadlist_space(&origin_head->ds_deadlist,
2480 &odl_used, &odl_comp, &odl_uncomp);
2482 dused = clone->ds_phys->ds_referenced_bytes + cdl_used -
2483 (origin_head->ds_phys->ds_referenced_bytes + odl_used);
2484 dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp -
2485 (origin_head->ds_phys->ds_compressed_bytes + odl_comp);
2486 duncomp = clone->ds_phys->ds_uncompressed_bytes +
2488 (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp);
2490 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
2491 dused, dcomp, duncomp, tx);
2492 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
2493 -dused, -dcomp, -duncomp, tx);
2496 * The difference in the space used by snapshots is the
2497 * difference in snapshot space due to the head's
2498 * deadlist (since that's the only thing that's
2499 * changing that affects the snapused).
2501 dsl_deadlist_space_range(&clone->ds_deadlist,
2502 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2503 &cdl_used, &cdl_comp, &cdl_uncomp);
2504 dsl_deadlist_space_range(&origin_head->ds_deadlist,
2505 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2506 &odl_used, &odl_comp, &odl_uncomp);
2507 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
2508 DD_USED_HEAD, DD_USED_SNAP, tx);
2511 /* swap ds_*_bytes */
2512 SWITCH64(origin_head->ds_phys->ds_referenced_bytes,
2513 clone->ds_phys->ds_referenced_bytes);
2514 SWITCH64(origin_head->ds_phys->ds_compressed_bytes,
2515 clone->ds_phys->ds_compressed_bytes);
2516 SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes,
2517 clone->ds_phys->ds_uncompressed_bytes);
2518 SWITCH64(origin_head->ds_phys->ds_unique_bytes,
2519 clone->ds_phys->ds_unique_bytes);
2521 /* apply any parent delta for change in unconsumed refreservation */
2522 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
2523 unused_refres_delta, 0, 0, tx);
2528 dsl_deadlist_close(&clone->ds_deadlist);
2529 dsl_deadlist_close(&origin_head->ds_deadlist);
2530 SWITCH64(origin_head->ds_phys->ds_deadlist_obj,
2531 clone->ds_phys->ds_deadlist_obj);
2532 dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
2533 clone->ds_phys->ds_deadlist_obj);
2534 dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
2535 origin_head->ds_phys->ds_deadlist_obj);
2537 dsl_scan_ds_clone_swapped(origin_head, clone, tx);
2539 spa_history_log_internal_ds(clone, "clone swap", tx,
2540 "parent=%s", origin_head->ds_dir->dd_myname);
2544 * Given a pool name and a dataset object number in that pool,
2545 * return the name of that dataset.
2548 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2554 error = dsl_pool_hold(pname, FTAG, &dp);
2558 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
2560 dsl_dataset_name(ds, buf);
2561 dsl_dataset_rele(ds, FTAG);
2563 dsl_pool_rele(dp, FTAG);
2569 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
2570 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
2574 ASSERT3S(asize, >, 0);
2577 * *ref_rsrv is the portion of asize that will come from any
2578 * unconsumed refreservation space.
2582 mutex_enter(&ds->ds_lock);
2584 * Make a space adjustment for reserved bytes.
2586 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
2588 ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2589 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2591 asize - MIN(asize, parent_delta(ds, asize + inflight));
2594 if (!check_quota || ds->ds_quota == 0) {
2595 mutex_exit(&ds->ds_lock);
2599 * If they are requesting more space, and our current estimate
2600 * is over quota, they get to try again unless the actual
2601 * on-disk is over quota and there are no pending changes (which
2602 * may free up space for us).
2604 if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
2606 ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
2607 error = SET_ERROR(ERESTART);
2609 error = SET_ERROR(EDQUOT);
2611 mutex_exit(&ds->ds_lock);
2616 typedef struct dsl_dataset_set_qr_arg {
2617 const char *ddsqra_name;
2618 zprop_source_t ddsqra_source;
2619 uint64_t ddsqra_value;
2620 } dsl_dataset_set_qr_arg_t;
2625 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
2627 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2628 dsl_pool_t *dp = dmu_tx_pool(tx);
2633 if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2634 return (SET_ERROR(ENOTSUP));
2636 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2640 if (dsl_dataset_is_snapshot(ds)) {
2641 dsl_dataset_rele(ds, FTAG);
2642 return (SET_ERROR(EINVAL));
2645 error = dsl_prop_predict(ds->ds_dir,
2646 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2647 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2649 dsl_dataset_rele(ds, FTAG);
2654 dsl_dataset_rele(ds, FTAG);
2658 if (newval < ds->ds_phys->ds_referenced_bytes ||
2659 newval < ds->ds_reserved) {
2660 dsl_dataset_rele(ds, FTAG);
2661 return (SET_ERROR(ENOSPC));
2664 dsl_dataset_rele(ds, FTAG);
2669 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
2671 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2672 dsl_pool_t *dp = dmu_tx_pool(tx);
2676 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2678 dsl_prop_set_sync_impl(ds,
2679 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2680 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
2681 &ddsqra->ddsqra_value, tx);
2683 VERIFY0(dsl_prop_get_int_ds(ds,
2684 zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
2686 if (ds->ds_quota != newval) {
2687 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2688 ds->ds_quota = newval;
2690 dsl_dataset_rele(ds, FTAG);
2694 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
2697 dsl_dataset_set_qr_arg_t ddsqra;
2699 ddsqra.ddsqra_name = dsname;
2700 ddsqra.ddsqra_source = source;
2701 ddsqra.ddsqra_value = refquota;
2703 return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
2704 dsl_dataset_set_refquota_sync, &ddsqra, 0));
2708 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
2710 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2711 dsl_pool_t *dp = dmu_tx_pool(tx);
2714 uint64_t newval, unique;
2716 if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2717 return (SET_ERROR(ENOTSUP));
2719 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2723 if (dsl_dataset_is_snapshot(ds)) {
2724 dsl_dataset_rele(ds, FTAG);
2725 return (SET_ERROR(EINVAL));
2728 error = dsl_prop_predict(ds->ds_dir,
2729 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2730 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2732 dsl_dataset_rele(ds, FTAG);
2737 * If we are doing the preliminary check in open context, the
2738 * space estimates may be inaccurate.
2740 if (!dmu_tx_is_syncing(tx)) {
2741 dsl_dataset_rele(ds, FTAG);
2745 mutex_enter(&ds->ds_lock);
2746 if (!DS_UNIQUE_IS_ACCURATE(ds))
2747 dsl_dataset_recalc_head_uniq(ds);
2748 unique = ds->ds_phys->ds_unique_bytes;
2749 mutex_exit(&ds->ds_lock);
2751 if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
2752 uint64_t delta = MAX(unique, newval) -
2753 MAX(unique, ds->ds_reserved);
2756 dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
2757 (ds->ds_quota > 0 && newval > ds->ds_quota)) {
2758 dsl_dataset_rele(ds, FTAG);
2759 return (SET_ERROR(ENOSPC));
2763 dsl_dataset_rele(ds, FTAG);
2768 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
2769 zprop_source_t source, uint64_t value, dmu_tx_t *tx)
2775 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2776 source, sizeof (value), 1, &value, tx);
2778 VERIFY0(dsl_prop_get_int_ds(ds,
2779 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
2781 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2782 mutex_enter(&ds->ds_dir->dd_lock);
2783 mutex_enter(&ds->ds_lock);
2784 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
2785 unique = ds->ds_phys->ds_unique_bytes;
2786 delta = MAX(0, (int64_t)(newval - unique)) -
2787 MAX(0, (int64_t)(ds->ds_reserved - unique));
2788 ds->ds_reserved = newval;
2789 mutex_exit(&ds->ds_lock);
2791 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
2792 mutex_exit(&ds->ds_dir->dd_lock);
2796 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
2798 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2799 dsl_pool_t *dp = dmu_tx_pool(tx);
2802 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2803 dsl_dataset_set_refreservation_sync_impl(ds,
2804 ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
2805 dsl_dataset_rele(ds, FTAG);
2809 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
2810 uint64_t refreservation)
2812 dsl_dataset_set_qr_arg_t ddsqra;
2814 ddsqra.ddsqra_name = dsname;
2815 ddsqra.ddsqra_source = source;
2816 ddsqra.ddsqra_value = refreservation;
2818 return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
2819 dsl_dataset_set_refreservation_sync, &ddsqra, 0));
2823 * Return (in *usedp) the amount of space written in new that is not
2824 * present in oldsnap. New may be a snapshot or the head. Old must be
2825 * a snapshot before new, in new's filesystem (or its origin). If not then
2826 * fail and return EINVAL.
2828 * The written space is calculated by considering two components: First, we
2829 * ignore any freed space, and calculate the written as new's used space
2830 * minus old's used space. Next, we add in the amount of space that was freed
2831 * between the two snapshots, thus reducing new's used space relative to old's.
2832 * Specifically, this is the space that was born before old->ds_creation_txg,
2833 * and freed before new (ie. on new's deadlist or a previous deadlist).
2835 * space freed [---------------------]
2836 * snapshots ---O-------O--------O-------O------
2840 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
2841 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2845 dsl_pool_t *dp = new->ds_dir->dd_pool;
2847 ASSERT(dsl_pool_config_held(dp));
2850 *usedp += new->ds_phys->ds_referenced_bytes;
2851 *usedp -= oldsnap->ds_phys->ds_referenced_bytes;
2854 *compp += new->ds_phys->ds_compressed_bytes;
2855 *compp -= oldsnap->ds_phys->ds_compressed_bytes;
2858 *uncompp += new->ds_phys->ds_uncompressed_bytes;
2859 *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
2861 snapobj = new->ds_object;
2862 while (snapobj != oldsnap->ds_object) {
2863 dsl_dataset_t *snap;
2864 uint64_t used, comp, uncomp;
2866 if (snapobj == new->ds_object) {
2869 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
2874 if (snap->ds_phys->ds_prev_snap_txg ==
2875 oldsnap->ds_phys->ds_creation_txg) {
2877 * The blocks in the deadlist can not be born after
2878 * ds_prev_snap_txg, so get the whole deadlist space,
2879 * which is more efficient (especially for old-format
2880 * deadlists). Unfortunately the deadlist code
2881 * doesn't have enough information to make this
2882 * optimization itself.
2884 dsl_deadlist_space(&snap->ds_deadlist,
2885 &used, &comp, &uncomp);
2887 dsl_deadlist_space_range(&snap->ds_deadlist,
2888 0, oldsnap->ds_phys->ds_creation_txg,
2889 &used, &comp, &uncomp);
2896 * If we get to the beginning of the chain of snapshots
2897 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
2898 * was not a snapshot of/before new.
2900 snapobj = snap->ds_phys->ds_prev_snap_obj;
2902 dsl_dataset_rele(snap, FTAG);
2904 err = SET_ERROR(EINVAL);
2913 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
2914 * lastsnap, and all snapshots in between are deleted.
2916 * blocks that would be freed [---------------------------]
2917 * snapshots ---O-------O--------O-------O--------O
2918 * firstsnap lastsnap
2920 * This is the set of blocks that were born after the snap before firstsnap,
2921 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
2922 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
2923 * We calculate this by iterating over the relevant deadlists (from the snap
2924 * after lastsnap, backward to the snap after firstsnap), summing up the
2925 * space on the deadlist that was born after the snap before firstsnap.
2928 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
2929 dsl_dataset_t *lastsnap,
2930 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2934 dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
2936 ASSERT(dsl_dataset_is_snapshot(firstsnap));
2937 ASSERT(dsl_dataset_is_snapshot(lastsnap));
2940 * Check that the snapshots are in the same dsl_dir, and firstsnap
2941 * is before lastsnap.
2943 if (firstsnap->ds_dir != lastsnap->ds_dir ||
2944 firstsnap->ds_phys->ds_creation_txg >
2945 lastsnap->ds_phys->ds_creation_txg)
2946 return (SET_ERROR(EINVAL));
2948 *usedp = *compp = *uncompp = 0;
2950 snapobj = lastsnap->ds_phys->ds_next_snap_obj;
2951 while (snapobj != firstsnap->ds_object) {
2953 uint64_t used, comp, uncomp;
2955 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
2959 dsl_deadlist_space_range(&ds->ds_deadlist,
2960 firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2961 &used, &comp, &uncomp);
2966 snapobj = ds->ds_phys->ds_prev_snap_obj;
2967 ASSERT3U(snapobj, !=, 0);
2968 dsl_dataset_rele(ds, FTAG);
2974 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
2975 * For example, they could both be snapshots of the same filesystem, and
2976 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
2977 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
2978 * filesystem. Or 'earlier' could be the origin's origin.
2981 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier)
2983 dsl_pool_t *dp = later->ds_dir->dd_pool;
2987 ASSERT(dsl_pool_config_held(dp));
2989 if (earlier->ds_phys->ds_creation_txg >=
2990 later->ds_phys->ds_creation_txg)
2993 if (later->ds_dir == earlier->ds_dir)
2995 if (!dsl_dir_is_clone(later->ds_dir))
2998 if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object)
3000 dsl_dataset_t *origin;
3001 error = dsl_dataset_hold_obj(dp,
3002 later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin);
3005 ret = dsl_dataset_is_before(origin, earlier);
3006 dsl_dataset_rele(origin, FTAG);