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,
997 dmu_tx_t *tx, boolean_t recv)
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)
1024 * We don't allow taking snapshots of inconsistent datasets, such as
1025 * those into which we are currently receiving. However, if we are
1026 * creating this snapshot as part of a receive, this check will be
1027 * executed atomically with respect to the completion of the receive
1028 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1029 * case we ignore this, knowing it will be fixed up for us shortly in
1030 * dmu_recv_end_sync().
1032 if (!recv && DS_IS_INCONSISTENT(ds))
1033 return (SET_ERROR(EBUSY));
1035 error = dsl_dataset_snapshot_reserve_space(ds, tx);
1043 dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1045 dsl_dataset_snapshot_arg_t *ddsa = arg;
1046 dsl_pool_t *dp = dmu_tx_pool(tx);
1050 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1051 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1055 char dsname[MAXNAMELEN];
1057 name = nvpair_name(pair);
1058 if (strlen(name) >= MAXNAMELEN)
1059 error = SET_ERROR(ENAMETOOLONG);
1061 atp = strchr(name, '@');
1063 error = SET_ERROR(EINVAL);
1065 (void) strlcpy(dsname, name, atp - name + 1);
1068 error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1070 error = dsl_dataset_snapshot_check_impl(ds,
1071 atp + 1, tx, B_FALSE);
1072 dsl_dataset_rele(ds, FTAG);
1076 if (ddsa->ddsa_errors != NULL) {
1077 fnvlist_add_int32(ddsa->ddsa_errors,
1087 dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1090 static zil_header_t zero_zil;
1092 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1094 dsl_dataset_phys_t *dsphys;
1095 uint64_t dsobj, crtxg;
1096 objset_t *mos = dp->dp_meta_objset;
1099 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1102 * If we are on an old pool, the zil must not be active, in which
1103 * case it will be zeroed. Usually zil_suspend() accomplishes this.
1105 ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1106 dmu_objset_from_ds(ds, &os) != 0 ||
1107 bcmp(&os->os_phys->os_zil_header, &zero_zil,
1108 sizeof (zero_zil)) == 0);
1112 * The origin's ds_creation_txg has to be < TXG_INITIAL
1114 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1119 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1120 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1121 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1122 dmu_buf_will_dirty(dbuf, tx);
1123 dsphys = dbuf->db_data;
1124 bzero(dsphys, sizeof (dsl_dataset_phys_t));
1125 dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1126 dsphys->ds_fsid_guid = unique_create();
1128 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1129 sizeof (dsphys->ds_guid));
1130 } while (dsphys->ds_guid == 0);
1131 dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1132 dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1133 dsphys->ds_next_snap_obj = ds->ds_object;
1134 dsphys->ds_num_children = 1;
1135 dsphys->ds_creation_time = gethrestime_sec();
1136 dsphys->ds_creation_txg = crtxg;
1137 dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1138 dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
1139 dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1140 dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
1141 dsphys->ds_flags = ds->ds_phys->ds_flags;
1142 dsphys->ds_bp = ds->ds_phys->ds_bp;
1143 dmu_buf_rele(dbuf, FTAG);
1145 ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
1147 uint64_t next_clones_obj =
1148 ds->ds_prev->ds_phys->ds_next_clones_obj;
1149 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1151 ds->ds_prev->ds_phys->ds_num_children > 1);
1152 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1153 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1154 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1155 ds->ds_prev->ds_phys->ds_creation_txg);
1156 ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1157 } else if (next_clones_obj != 0) {
1158 dsl_dataset_remove_from_next_clones(ds->ds_prev,
1159 dsphys->ds_next_snap_obj, tx);
1160 VERIFY0(zap_add_int(mos,
1161 next_clones_obj, dsobj, tx));
1166 * If we have a reference-reservation on this dataset, we will
1167 * need to increase the amount of refreservation being charged
1168 * since our unique space is going to zero.
1170 if (ds->ds_reserved) {
1172 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1173 delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
1174 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1178 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1179 ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
1180 UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
1181 dsl_deadlist_close(&ds->ds_deadlist);
1182 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1183 dsl_deadlist_add_key(&ds->ds_deadlist,
1184 ds->ds_phys->ds_prev_snap_txg, tx);
1186 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1187 ds->ds_phys->ds_prev_snap_obj = dsobj;
1188 ds->ds_phys->ds_prev_snap_txg = crtxg;
1189 ds->ds_phys->ds_unique_bytes = 0;
1190 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1191 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1193 VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1194 snapname, 8, 1, &dsobj, tx));
1197 dsl_dataset_rele(ds->ds_prev, ds);
1198 VERIFY0(dsl_dataset_hold_obj(dp,
1199 ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
1201 dsl_scan_ds_snapshotted(ds, tx);
1203 dsl_dir_snap_cmtime_update(ds->ds_dir);
1205 spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1209 dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1211 dsl_dataset_snapshot_arg_t *ddsa = arg;
1212 dsl_pool_t *dp = dmu_tx_pool(tx);
1215 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1216 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1219 char dsname[MAXNAMELEN];
1221 name = nvpair_name(pair);
1222 atp = strchr(name, '@');
1223 (void) strlcpy(dsname, name, atp - name + 1);
1224 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1226 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1227 if (ddsa->ddsa_props != NULL) {
1228 dsl_props_set_sync_impl(ds->ds_prev,
1229 ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1231 dsl_dataset_rele(ds, FTAG);
1236 * The snapshots must all be in the same pool.
1237 * All-or-nothing: if there are any failures, nothing will be modified.
1240 dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1242 dsl_dataset_snapshot_arg_t ddsa;
1244 boolean_t needsuspend;
1248 nvlist_t *suspended = NULL;
1250 pair = nvlist_next_nvpair(snaps, NULL);
1253 firstname = nvpair_name(pair);
1255 error = spa_open(firstname, &spa, FTAG);
1258 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1259 spa_close(spa, FTAG);
1262 suspended = fnvlist_alloc();
1263 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1264 pair = nvlist_next_nvpair(snaps, pair)) {
1265 char fsname[MAXNAMELEN];
1266 char *snapname = nvpair_name(pair);
1270 atp = strchr(snapname, '@');
1272 error = SET_ERROR(EINVAL);
1275 (void) strlcpy(fsname, snapname, atp - snapname + 1);
1277 error = zil_suspend(fsname, &cookie);
1280 fnvlist_add_uint64(suspended, fsname,
1285 ddsa.ddsa_snaps = snaps;
1286 ddsa.ddsa_props = props;
1287 ddsa.ddsa_errors = errors;
1290 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1291 dsl_dataset_snapshot_sync, &ddsa,
1292 fnvlist_num_pairs(snaps) * 3);
1295 if (suspended != NULL) {
1296 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1297 pair = nvlist_next_nvpair(suspended, pair)) {
1298 zil_resume((void *)(uintptr_t)
1299 fnvpair_value_uint64(pair));
1301 fnvlist_free(suspended);
1307 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1308 pair = nvlist_next_nvpair(snaps, pair)) {
1309 char *snapname = nvpair_name(pair);
1310 zvol_create_minors(snapname);
1318 typedef struct dsl_dataset_snapshot_tmp_arg {
1319 const char *ddsta_fsname;
1320 const char *ddsta_snapname;
1321 minor_t ddsta_cleanup_minor;
1322 const char *ddsta_htag;
1323 } dsl_dataset_snapshot_tmp_arg_t;
1326 dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1328 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1329 dsl_pool_t *dp = dmu_tx_pool(tx);
1333 error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1337 error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1340 dsl_dataset_rele(ds, FTAG);
1344 if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
1345 dsl_dataset_rele(ds, FTAG);
1346 return (SET_ERROR(ENOTSUP));
1348 error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
1351 dsl_dataset_rele(ds, FTAG);
1355 dsl_dataset_rele(ds, FTAG);
1360 dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
1362 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1363 dsl_pool_t *dp = dmu_tx_pool(tx);
1366 VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
1368 dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
1369 dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
1370 ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
1371 dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
1373 dsl_dataset_rele(ds, FTAG);
1377 dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
1378 minor_t cleanup_minor, const char *htag)
1380 dsl_dataset_snapshot_tmp_arg_t ddsta;
1383 boolean_t needsuspend;
1386 ddsta.ddsta_fsname = fsname;
1387 ddsta.ddsta_snapname = snapname;
1388 ddsta.ddsta_cleanup_minor = cleanup_minor;
1389 ddsta.ddsta_htag = htag;
1391 error = spa_open(fsname, &spa, FTAG);
1394 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1395 spa_close(spa, FTAG);
1398 error = zil_suspend(fsname, &cookie);
1403 error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
1404 dsl_dataset_snapshot_tmp_sync, &ddsta, 3);
1413 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1415 ASSERT(dmu_tx_is_syncing(tx));
1416 ASSERT(ds->ds_objset != NULL);
1417 ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1420 * in case we had to change ds_fsid_guid when we opened it,
1423 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1424 ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
1426 dmu_objset_sync(ds->ds_objset, zio, tx);
1430 get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1433 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1436 nvlist_t *propval = fnvlist_alloc();
1437 nvlist_t *val = fnvlist_alloc();
1439 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1442 * There may be missing entries in ds_next_clones_obj
1443 * due to a bug in a previous version of the code.
1444 * Only trust it if it has the right number of entries.
1446 if (ds->ds_phys->ds_next_clones_obj != 0) {
1447 ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
1450 if (count != ds->ds_phys->ds_num_children - 1)
1452 for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
1453 zap_cursor_retrieve(&zc, &za) == 0;
1454 zap_cursor_advance(&zc)) {
1455 dsl_dataset_t *clone;
1456 char buf[ZFS_MAXNAMELEN];
1457 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1458 za.za_first_integer, FTAG, &clone));
1459 dsl_dir_name(clone->ds_dir, buf);
1460 fnvlist_add_boolean(val, buf);
1461 dsl_dataset_rele(clone, FTAG);
1463 zap_cursor_fini(&zc);
1464 fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1465 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
1468 nvlist_free(propval);
1472 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1474 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1475 uint64_t refd, avail, uobjs, aobjs, ratio;
1477 ASSERT(dsl_pool_config_held(dp));
1479 ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
1480 (ds->ds_phys->ds_uncompressed_bytes * 100 /
1481 ds->ds_phys->ds_compressed_bytes);
1483 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
1484 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1485 ds->ds_phys->ds_uncompressed_bytes);
1487 if (dsl_dataset_is_snapshot(ds)) {
1488 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
1489 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1490 ds->ds_phys->ds_unique_bytes);
1491 get_clones_stat(ds, nv);
1493 dsl_dir_stats(ds->ds_dir, nv);
1496 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1497 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1498 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1500 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1501 ds->ds_phys->ds_creation_time);
1502 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1503 ds->ds_phys->ds_creation_txg);
1504 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1506 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1508 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1509 ds->ds_phys->ds_guid);
1510 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1511 ds->ds_phys->ds_unique_bytes);
1512 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
1514 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
1516 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1517 DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1519 if (ds->ds_phys->ds_prev_snap_obj != 0) {
1520 uint64_t written, comp, uncomp;
1521 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1522 dsl_dataset_t *prev;
1524 int err = dsl_dataset_hold_obj(dp,
1525 ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1527 err = dsl_dataset_space_written(prev, ds, &written,
1529 dsl_dataset_rele(prev, FTAG);
1531 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
1539 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1541 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1542 ASSERT(dsl_pool_config_held(dp));
1544 stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1545 stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
1546 stat->dds_guid = ds->ds_phys->ds_guid;
1547 stat->dds_origin[0] = '\0';
1548 if (dsl_dataset_is_snapshot(ds)) {
1549 stat->dds_is_snapshot = B_TRUE;
1550 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1552 stat->dds_is_snapshot = B_FALSE;
1553 stat->dds_num_clones = 0;
1555 if (dsl_dir_is_clone(ds->ds_dir)) {
1558 VERIFY0(dsl_dataset_hold_obj(dp,
1559 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
1560 dsl_dataset_name(ods, stat->dds_origin);
1561 dsl_dataset_rele(ods, FTAG);
1567 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1569 return (ds->ds_fsid_guid);
1573 dsl_dataset_space(dsl_dataset_t *ds,
1574 uint64_t *refdbytesp, uint64_t *availbytesp,
1575 uint64_t *usedobjsp, uint64_t *availobjsp)
1577 *refdbytesp = ds->ds_phys->ds_referenced_bytes;
1578 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1579 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
1580 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
1581 if (ds->ds_quota != 0) {
1583 * Adjust available bytes according to refquota
1585 if (*refdbytesp < ds->ds_quota)
1586 *availbytesp = MIN(*availbytesp,
1587 ds->ds_quota - *refdbytesp);
1591 *usedobjsp = ds->ds_phys->ds_bp.blk_fill;
1592 *availobjsp = DN_MAX_OBJECT - *usedobjsp;
1596 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
1598 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1600 ASSERT(dsl_pool_config_held(dp));
1601 if (ds->ds_prev == NULL)
1603 if (ds->ds_phys->ds_bp.blk_birth >
1604 ds->ds_prev->ds_phys->ds_creation_txg) {
1605 objset_t *os, *os_prev;
1607 * It may be that only the ZIL differs, because it was
1608 * reset in the head. Don't count that as being
1611 if (dmu_objset_from_ds(ds, &os) != 0)
1613 if (dmu_objset_from_ds(ds->ds_prev, &os_prev) != 0)
1615 return (bcmp(&os->os_phys->os_meta_dnode,
1616 &os_prev->os_phys->os_meta_dnode,
1617 sizeof (os->os_phys->os_meta_dnode)) != 0);
1622 typedef struct dsl_dataset_rename_snapshot_arg {
1623 const char *ddrsa_fsname;
1624 const char *ddrsa_oldsnapname;
1625 const char *ddrsa_newsnapname;
1626 boolean_t ddrsa_recursive;
1628 } dsl_dataset_rename_snapshot_arg_t;
1632 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
1633 dsl_dataset_t *hds, void *arg)
1635 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1639 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1641 /* ignore nonexistent snapshots */
1642 return (error == ENOENT ? 0 : error);
1645 /* new name should not exist */
1646 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
1648 error = SET_ERROR(EEXIST);
1649 else if (error == ENOENT)
1652 /* dataset name + 1 for the "@" + the new snapshot name must fit */
1653 if (dsl_dir_namelen(hds->ds_dir) + 1 +
1654 strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1655 error = SET_ERROR(ENAMETOOLONG);
1661 dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
1663 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1664 dsl_pool_t *dp = dmu_tx_pool(tx);
1668 error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
1672 if (ddrsa->ddrsa_recursive) {
1673 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1674 dsl_dataset_rename_snapshot_check_impl, ddrsa,
1677 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
1679 dsl_dataset_rele(hds, FTAG);
1684 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
1685 dsl_dataset_t *hds, void *arg)
1689 char *oldname, *newname;
1692 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1695 dmu_tx_t *tx = ddrsa->ddrsa_tx;
1698 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1699 ASSERT(error == 0 || error == ENOENT);
1700 if (error == ENOENT) {
1701 /* ignore nonexistent snapshots */
1705 VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
1707 /* log before we change the name */
1708 spa_history_log_internal_ds(ds, "rename", tx,
1709 "-> @%s", ddrsa->ddrsa_newsnapname);
1711 VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx));
1712 mutex_enter(&ds->ds_lock);
1713 (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
1714 mutex_exit(&ds->ds_lock);
1715 VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj,
1716 ds->ds_snapname, 8, 1, &ds->ds_object, tx));
1720 oldname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1721 newname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1722 snprintf(oldname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname,
1723 ddrsa->ddrsa_oldsnapname);
1724 snprintf(newname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname,
1725 ddrsa->ddrsa_newsnapname);
1726 zfsvfs_update_fromname(oldname, newname);
1727 zvol_rename_minors(oldname, newname);
1728 kmem_free(newname, MAXPATHLEN);
1729 kmem_free(oldname, MAXPATHLEN);
1732 dsl_dataset_rele(ds, FTAG);
1738 dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
1740 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1741 dsl_pool_t *dp = dmu_tx_pool(tx);
1744 VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
1745 ddrsa->ddrsa_tx = tx;
1746 if (ddrsa->ddrsa_recursive) {
1747 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1748 dsl_dataset_rename_snapshot_sync_impl, ddrsa,
1751 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
1753 dsl_dataset_rele(hds, FTAG);
1757 dsl_dataset_rename_snapshot(const char *fsname,
1758 const char *oldsnapname, const char *newsnapname, boolean_t recursive)
1760 dsl_dataset_rename_snapshot_arg_t ddrsa;
1762 ddrsa.ddrsa_fsname = fsname;
1763 ddrsa.ddrsa_oldsnapname = oldsnapname;
1764 ddrsa.ddrsa_newsnapname = newsnapname;
1765 ddrsa.ddrsa_recursive = recursive;
1767 return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
1768 dsl_dataset_rename_snapshot_sync, &ddrsa, 1));
1772 * If we're doing an ownership handoff, we need to make sure that there is
1773 * only one long hold on the dataset. We're not allowed to change anything here
1774 * so we don't permanently release the long hold or regular hold here. We want
1775 * to do this only when syncing to avoid the dataset unexpectedly going away
1776 * when we release the long hold.
1779 dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
1783 if (!dmu_tx_is_syncing(tx))
1786 if (owner != NULL) {
1787 VERIFY3P(ds->ds_owner, ==, owner);
1788 dsl_dataset_long_rele(ds, owner);
1791 held = dsl_dataset_long_held(ds);
1794 dsl_dataset_long_hold(ds, owner);
1797 return (SET_ERROR(EBUSY));
1802 typedef struct dsl_dataset_rollback_arg {
1803 const char *ddra_fsname;
1805 } dsl_dataset_rollback_arg_t;
1808 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
1810 dsl_dataset_rollback_arg_t *ddra = arg;
1811 dsl_pool_t *dp = dmu_tx_pool(tx);
1813 int64_t unused_refres_delta;
1816 error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
1820 /* must not be a snapshot */
1821 if (dsl_dataset_is_snapshot(ds)) {
1822 dsl_dataset_rele(ds, FTAG);
1823 return (SET_ERROR(EINVAL));
1826 /* must have a most recent snapshot */
1827 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
1828 dsl_dataset_rele(ds, FTAG);
1829 return (SET_ERROR(EINVAL));
1832 error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
1834 dsl_dataset_rele(ds, FTAG);
1839 * Check if the snap we are rolling back to uses more than
1842 if (ds->ds_quota != 0 &&
1843 ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) {
1844 dsl_dataset_rele(ds, FTAG);
1845 return (SET_ERROR(EDQUOT));
1849 * When we do the clone swap, we will temporarily use more space
1850 * due to the refreservation (the head will no longer have any
1851 * unique space, so the entire amount of the refreservation will need
1852 * to be free). We will immediately destroy the clone, freeing
1853 * this space, but the freeing happens over many txg's.
1855 unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
1856 ds->ds_phys->ds_unique_bytes);
1858 if (unused_refres_delta > 0 &&
1859 unused_refres_delta >
1860 dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
1861 dsl_dataset_rele(ds, FTAG);
1862 return (SET_ERROR(ENOSPC));
1865 dsl_dataset_rele(ds, FTAG);
1870 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
1872 dsl_dataset_rollback_arg_t *ddra = arg;
1873 dsl_pool_t *dp = dmu_tx_pool(tx);
1874 dsl_dataset_t *ds, *clone;
1877 VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
1879 cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
1880 ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
1882 VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
1884 dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
1885 dsl_dataset_zero_zil(ds, tx);
1887 dsl_destroy_head_sync_impl(clone, tx);
1889 dsl_dataset_rele(clone, FTAG);
1890 dsl_dataset_rele(ds, FTAG);
1896 * - The existing dataset MUST be owned by the specified owner at entry
1897 * - Upon return, dataset will still be held by the same owner, whether we
1900 * This mode is required any time the existing filesystem is mounted. See
1901 * notes above zfs_suspend_fs() for further details.
1904 dsl_dataset_rollback(const char *fsname, void *owner)
1906 dsl_dataset_rollback_arg_t ddra;
1908 ddra.ddra_fsname = fsname;
1909 ddra.ddra_owner = owner;
1911 return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
1912 dsl_dataset_rollback_sync, (void *)&ddra, 1));
1915 struct promotenode {
1920 typedef struct dsl_dataset_promote_arg {
1921 const char *ddpa_clonename;
1922 dsl_dataset_t *ddpa_clone;
1923 list_t shared_snaps, origin_snaps, clone_snaps;
1924 dsl_dataset_t *origin_origin; /* origin of the origin */
1925 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
1927 } dsl_dataset_promote_arg_t;
1929 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
1930 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
1932 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
1935 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
1937 dsl_dataset_promote_arg_t *ddpa = arg;
1938 dsl_pool_t *dp = dmu_tx_pool(tx);
1940 struct promotenode *snap;
1941 dsl_dataset_t *origin_ds;
1945 err = promote_hold(ddpa, dp, FTAG);
1949 hds = ddpa->ddpa_clone;
1951 if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
1952 promote_rele(ddpa, FTAG);
1953 return (SET_ERROR(EXDEV));
1957 * Compute and check the amount of space to transfer. Since this is
1958 * so expensive, don't do the preliminary check.
1960 if (!dmu_tx_is_syncing(tx)) {
1961 promote_rele(ddpa, FTAG);
1965 snap = list_head(&ddpa->shared_snaps);
1966 origin_ds = snap->ds;
1968 /* compute origin's new unique space */
1969 snap = list_tail(&ddpa->clone_snaps);
1970 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
1971 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
1972 origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
1973 &ddpa->unique, &unused, &unused);
1976 * Walk the snapshots that we are moving
1978 * Compute space to transfer. Consider the incremental changes
1979 * to used by each snapshot:
1980 * (my used) = (prev's used) + (blocks born) - (blocks killed)
1981 * So each snapshot gave birth to:
1982 * (blocks born) = (my used) - (prev's used) + (blocks killed)
1983 * So a sequence would look like:
1984 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
1985 * Which simplifies to:
1986 * uN + kN + kN-1 + ... + k1 + k0
1987 * Note however, if we stop before we reach the ORIGIN we get:
1988 * uN + kN + kN-1 + ... + kM - uM-1
1990 ddpa->used = origin_ds->ds_phys->ds_referenced_bytes;
1991 ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes;
1992 ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
1993 for (snap = list_head(&ddpa->shared_snaps); snap;
1994 snap = list_next(&ddpa->shared_snaps, snap)) {
1995 uint64_t val, dlused, dlcomp, dluncomp;
1996 dsl_dataset_t *ds = snap->ds;
1999 * If there are long holds, we won't be able to evict
2002 if (dsl_dataset_long_held(ds)) {
2003 err = SET_ERROR(EBUSY);
2007 /* Check that the snapshot name does not conflict */
2008 VERIFY0(dsl_dataset_get_snapname(ds));
2009 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2011 (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2012 err = SET_ERROR(EEXIST);
2018 /* The very first snapshot does not have a deadlist */
2019 if (ds->ds_phys->ds_prev_snap_obj == 0)
2022 dsl_deadlist_space(&ds->ds_deadlist,
2023 &dlused, &dlcomp, &dluncomp);
2024 ddpa->used += dlused;
2025 ddpa->comp += dlcomp;
2026 ddpa->uncomp += dluncomp;
2030 * If we are a clone of a clone then we never reached ORIGIN,
2031 * so we need to subtract out the clone origin's used space.
2033 if (ddpa->origin_origin) {
2034 ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes;
2035 ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes;
2037 ddpa->origin_origin->ds_phys->ds_uncompressed_bytes;
2040 /* Check that there is enough space here */
2041 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2047 * Compute the amounts of space that will be used by snapshots
2048 * after the promotion (for both origin and clone). For each,
2049 * it is the amount of space that will be on all of their
2050 * deadlists (that was not born before their new origin).
2052 if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2056 * Note, typically this will not be a clone of a clone,
2057 * so dd_origin_txg will be < TXG_INITIAL, so
2058 * these snaplist_space() -> dsl_deadlist_space_range()
2059 * calls will be fast because they do not have to
2060 * iterate over all bps.
2062 snap = list_head(&ddpa->origin_snaps);
2063 err = snaplist_space(&ddpa->shared_snaps,
2064 snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
2068 err = snaplist_space(&ddpa->clone_snaps,
2069 snap->ds->ds_dir->dd_origin_txg, &space);
2072 ddpa->cloneusedsnap += space;
2074 if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2075 err = snaplist_space(&ddpa->origin_snaps,
2076 origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap);
2082 promote_rele(ddpa, FTAG);
2087 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
2089 dsl_dataset_promote_arg_t *ddpa = arg;
2090 dsl_pool_t *dp = dmu_tx_pool(tx);
2092 struct promotenode *snap;
2093 dsl_dataset_t *origin_ds;
2094 dsl_dataset_t *origin_head;
2096 dsl_dir_t *odd = NULL;
2097 uint64_t oldnext_obj;
2100 VERIFY0(promote_hold(ddpa, dp, FTAG));
2101 hds = ddpa->ddpa_clone;
2103 ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE);
2105 snap = list_head(&ddpa->shared_snaps);
2106 origin_ds = snap->ds;
2109 snap = list_head(&ddpa->origin_snaps);
2110 origin_head = snap->ds;
2113 * We need to explicitly open odd, since origin_ds's dd will be
2116 VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
2119 /* change origin's next snap */
2120 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2121 oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
2122 snap = list_tail(&ddpa->clone_snaps);
2123 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2124 origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
2126 /* change the origin's next clone */
2127 if (origin_ds->ds_phys->ds_next_clones_obj) {
2128 dsl_dataset_remove_from_next_clones(origin_ds,
2129 snap->ds->ds_object, tx);
2130 VERIFY0(zap_add_int(dp->dp_meta_objset,
2131 origin_ds->ds_phys->ds_next_clones_obj,
2136 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2137 ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2138 dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
2139 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2140 dmu_buf_will_dirty(odd->dd_dbuf, tx);
2141 odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
2142 origin_head->ds_dir->dd_origin_txg =
2143 origin_ds->ds_phys->ds_creation_txg;
2145 /* change dd_clone entries */
2146 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2147 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2148 odd->dd_phys->dd_clones, hds->ds_object, tx));
2149 VERIFY0(zap_add_int(dp->dp_meta_objset,
2150 ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2151 hds->ds_object, tx));
2153 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2154 ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2155 origin_head->ds_object, tx));
2156 if (dd->dd_phys->dd_clones == 0) {
2157 dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2158 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2160 VERIFY0(zap_add_int(dp->dp_meta_objset,
2161 dd->dd_phys->dd_clones, origin_head->ds_object, tx));
2164 /* move snapshots to this dir */
2165 for (snap = list_head(&ddpa->shared_snaps); snap;
2166 snap = list_next(&ddpa->shared_snaps, snap)) {
2167 dsl_dataset_t *ds = snap->ds;
2170 * Property callbacks are registered to a particular
2171 * dsl_dir. Since ours is changing, evict the objset
2172 * so that they will be unregistered from the old dsl_dir.
2174 if (ds->ds_objset) {
2175 dmu_objset_evict(ds->ds_objset);
2176 ds->ds_objset = NULL;
2179 /* move snap name entry */
2180 VERIFY0(dsl_dataset_get_snapname(ds));
2181 VERIFY0(dsl_dataset_snap_remove(origin_head,
2182 ds->ds_snapname, tx));
2183 VERIFY0(zap_add(dp->dp_meta_objset,
2184 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
2185 8, 1, &ds->ds_object, tx));
2187 /* change containing dsl_dir */
2188 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2189 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2190 ds->ds_phys->ds_dir_obj = dd->dd_object;
2191 ASSERT3P(ds->ds_dir, ==, odd);
2192 dsl_dir_rele(ds->ds_dir, ds);
2193 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
2194 NULL, ds, &ds->ds_dir));
2196 /* move any clone references */
2197 if (ds->ds_phys->ds_next_clones_obj &&
2198 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2202 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2203 ds->ds_phys->ds_next_clones_obj);
2204 zap_cursor_retrieve(&zc, &za) == 0;
2205 zap_cursor_advance(&zc)) {
2206 dsl_dataset_t *cnds;
2209 if (za.za_first_integer == oldnext_obj) {
2211 * We've already moved the
2212 * origin's reference.
2217 VERIFY0(dsl_dataset_hold_obj(dp,
2218 za.za_first_integer, FTAG, &cnds));
2219 o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2221 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2222 odd->dd_phys->dd_clones, o, tx));
2223 VERIFY0(zap_add_int(dp->dp_meta_objset,
2224 dd->dd_phys->dd_clones, o, tx));
2225 dsl_dataset_rele(cnds, FTAG);
2227 zap_cursor_fini(&zc);
2230 ASSERT(!dsl_prop_hascb(ds));
2234 * Change space accounting.
2235 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2236 * both be valid, or both be 0 (resulting in delta == 0). This
2237 * is true for each of {clone,origin} independently.
2240 delta = ddpa->cloneusedsnap -
2241 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2242 ASSERT3S(delta, >=, 0);
2243 ASSERT3U(ddpa->used, >=, delta);
2244 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2245 dsl_dir_diduse_space(dd, DD_USED_HEAD,
2246 ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
2248 delta = ddpa->originusedsnap -
2249 odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2250 ASSERT3S(delta, <=, 0);
2251 ASSERT3U(ddpa->used, >=, -delta);
2252 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2253 dsl_dir_diduse_space(odd, DD_USED_HEAD,
2254 -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
2256 origin_ds->ds_phys->ds_unique_bytes = ddpa->unique;
2258 /* log history record */
2259 spa_history_log_internal_ds(hds, "promote", tx, "");
2261 dsl_dir_rele(odd, FTAG);
2262 promote_rele(ddpa, FTAG);
2266 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2267 * (exclusive) and last_obj (inclusive). The list will be in reverse
2268 * order (last_obj will be the list_head()). If first_obj == 0, do all
2269 * snapshots back to this dataset's origin.
2272 snaplist_make(dsl_pool_t *dp,
2273 uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2275 uint64_t obj = last_obj;
2277 list_create(l, sizeof (struct promotenode),
2278 offsetof(struct promotenode, link));
2280 while (obj != first_obj) {
2282 struct promotenode *snap;
2285 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
2286 ASSERT(err != ENOENT);
2291 first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
2293 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
2295 list_insert_tail(l, snap);
2296 obj = ds->ds_phys->ds_prev_snap_obj;
2303 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2305 struct promotenode *snap;
2308 for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2309 uint64_t used, comp, uncomp;
2310 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2311 mintxg, UINT64_MAX, &used, &comp, &uncomp);
2318 snaplist_destroy(list_t *l, void *tag)
2320 struct promotenode *snap;
2322 if (l == NULL || !list_link_active(&l->list_head))
2325 while ((snap = list_tail(l)) != NULL) {
2326 list_remove(l, snap);
2327 dsl_dataset_rele(snap->ds, tag);
2328 kmem_free(snap, sizeof (*snap));
2334 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2338 struct promotenode *snap;
2340 error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
2344 dd = ddpa->ddpa_clone->ds_dir;
2346 if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) ||
2347 !dsl_dir_is_clone(dd)) {
2348 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2349 return (SET_ERROR(EINVAL));
2352 error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj,
2353 &ddpa->shared_snaps, tag);
2357 error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
2358 &ddpa->clone_snaps, tag);
2362 snap = list_head(&ddpa->shared_snaps);
2363 ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
2364 error = snaplist_make(dp, dd->dd_phys->dd_origin_obj,
2365 snap->ds->ds_dir->dd_phys->dd_head_dataset_obj,
2366 &ddpa->origin_snaps, tag);
2370 if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
2371 error = dsl_dataset_hold_obj(dp,
2372 snap->ds->ds_dir->dd_phys->dd_origin_obj,
2373 tag, &ddpa->origin_origin);
2379 promote_rele(ddpa, tag);
2384 promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2386 snaplist_destroy(&ddpa->shared_snaps, tag);
2387 snaplist_destroy(&ddpa->clone_snaps, tag);
2388 snaplist_destroy(&ddpa->origin_snaps, tag);
2389 if (ddpa->origin_origin != NULL)
2390 dsl_dataset_rele(ddpa->origin_origin, tag);
2391 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2397 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2398 * in with the name. (It must be at least MAXNAMELEN bytes long.)
2401 dsl_dataset_promote(const char *name, char *conflsnap)
2403 dsl_dataset_promote_arg_t ddpa = { 0 };
2409 * We will modify space proportional to the number of
2410 * snapshots. Compute numsnaps.
2412 error = dmu_objset_hold(name, FTAG, &os);
2415 error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2416 dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps);
2417 dmu_objset_rele(os, FTAG);
2421 ddpa.ddpa_clonename = name;
2422 ddpa.err_ds = conflsnap;
2424 return (dsl_sync_task(name, dsl_dataset_promote_check,
2425 dsl_dataset_promote_sync, &ddpa, 2 + numsnaps));
2429 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
2430 dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2432 int64_t unused_refres_delta;
2434 /* they should both be heads */
2435 if (dsl_dataset_is_snapshot(clone) ||
2436 dsl_dataset_is_snapshot(origin_head))
2437 return (SET_ERROR(EINVAL));
2439 /* the branch point should be just before them */
2440 if (clone->ds_prev != origin_head->ds_prev)
2441 return (SET_ERROR(EINVAL));
2443 /* clone should be the clone (unless they are unrelated) */
2444 if (clone->ds_prev != NULL &&
2445 clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
2446 origin_head->ds_object !=
2447 clone->ds_prev->ds_phys->ds_next_snap_obj)
2448 return (SET_ERROR(EINVAL));
2450 /* the clone should be a child of the origin */
2451 if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2452 return (SET_ERROR(EINVAL));
2454 /* origin_head shouldn't be modified unless 'force' */
2455 if (!force && dsl_dataset_modified_since_lastsnap(origin_head))
2456 return (SET_ERROR(ETXTBSY));
2458 /* origin_head should have no long holds (e.g. is not mounted) */
2459 if (dsl_dataset_handoff_check(origin_head, owner, tx))
2460 return (SET_ERROR(EBUSY));
2462 /* check amount of any unconsumed refreservation */
2463 unused_refres_delta =
2464 (int64_t)MIN(origin_head->ds_reserved,
2465 origin_head->ds_phys->ds_unique_bytes) -
2466 (int64_t)MIN(origin_head->ds_reserved,
2467 clone->ds_phys->ds_unique_bytes);
2469 if (unused_refres_delta > 0 &&
2470 unused_refres_delta >
2471 dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2472 return (SET_ERROR(ENOSPC));
2474 /* clone can't be over the head's refquota */
2475 if (origin_head->ds_quota != 0 &&
2476 clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota)
2477 return (SET_ERROR(EDQUOT));
2483 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2484 dsl_dataset_t *origin_head, dmu_tx_t *tx)
2486 dsl_pool_t *dp = dmu_tx_pool(tx);
2487 int64_t unused_refres_delta;
2489 ASSERT(clone->ds_reserved == 0);
2490 ASSERT(origin_head->ds_quota == 0 ||
2491 clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota);
2493 dmu_buf_will_dirty(clone->ds_dbuf, tx);
2494 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2496 if (clone->ds_objset != NULL) {
2497 dmu_objset_evict(clone->ds_objset);
2498 clone->ds_objset = NULL;
2501 if (origin_head->ds_objset != NULL) {
2502 dmu_objset_evict(origin_head->ds_objset);
2503 origin_head->ds_objset = NULL;
2506 unused_refres_delta =
2507 (int64_t)MIN(origin_head->ds_reserved,
2508 origin_head->ds_phys->ds_unique_bytes) -
2509 (int64_t)MIN(origin_head->ds_reserved,
2510 clone->ds_phys->ds_unique_bytes);
2513 * Reset origin's unique bytes, if it exists.
2515 if (clone->ds_prev) {
2516 dsl_dataset_t *origin = clone->ds_prev;
2517 uint64_t comp, uncomp;
2519 dmu_buf_will_dirty(origin->ds_dbuf, tx);
2520 dsl_deadlist_space_range(&clone->ds_deadlist,
2521 origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2522 &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
2528 tmp = origin_head->ds_phys->ds_bp;
2529 origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp;
2530 clone->ds_phys->ds_bp = tmp;
2533 /* set dd_*_bytes */
2535 int64_t dused, dcomp, duncomp;
2536 uint64_t cdl_used, cdl_comp, cdl_uncomp;
2537 uint64_t odl_used, odl_comp, odl_uncomp;
2539 ASSERT3U(clone->ds_dir->dd_phys->
2540 dd_used_breakdown[DD_USED_SNAP], ==, 0);
2542 dsl_deadlist_space(&clone->ds_deadlist,
2543 &cdl_used, &cdl_comp, &cdl_uncomp);
2544 dsl_deadlist_space(&origin_head->ds_deadlist,
2545 &odl_used, &odl_comp, &odl_uncomp);
2547 dused = clone->ds_phys->ds_referenced_bytes + cdl_used -
2548 (origin_head->ds_phys->ds_referenced_bytes + odl_used);
2549 dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp -
2550 (origin_head->ds_phys->ds_compressed_bytes + odl_comp);
2551 duncomp = clone->ds_phys->ds_uncompressed_bytes +
2553 (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp);
2555 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
2556 dused, dcomp, duncomp, tx);
2557 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
2558 -dused, -dcomp, -duncomp, tx);
2561 * The difference in the space used by snapshots is the
2562 * difference in snapshot space due to the head's
2563 * deadlist (since that's the only thing that's
2564 * changing that affects the snapused).
2566 dsl_deadlist_space_range(&clone->ds_deadlist,
2567 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2568 &cdl_used, &cdl_comp, &cdl_uncomp);
2569 dsl_deadlist_space_range(&origin_head->ds_deadlist,
2570 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2571 &odl_used, &odl_comp, &odl_uncomp);
2572 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
2573 DD_USED_HEAD, DD_USED_SNAP, tx);
2576 /* swap ds_*_bytes */
2577 SWITCH64(origin_head->ds_phys->ds_referenced_bytes,
2578 clone->ds_phys->ds_referenced_bytes);
2579 SWITCH64(origin_head->ds_phys->ds_compressed_bytes,
2580 clone->ds_phys->ds_compressed_bytes);
2581 SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes,
2582 clone->ds_phys->ds_uncompressed_bytes);
2583 SWITCH64(origin_head->ds_phys->ds_unique_bytes,
2584 clone->ds_phys->ds_unique_bytes);
2586 /* apply any parent delta for change in unconsumed refreservation */
2587 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
2588 unused_refres_delta, 0, 0, tx);
2593 dsl_deadlist_close(&clone->ds_deadlist);
2594 dsl_deadlist_close(&origin_head->ds_deadlist);
2595 SWITCH64(origin_head->ds_phys->ds_deadlist_obj,
2596 clone->ds_phys->ds_deadlist_obj);
2597 dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
2598 clone->ds_phys->ds_deadlist_obj);
2599 dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
2600 origin_head->ds_phys->ds_deadlist_obj);
2602 dsl_scan_ds_clone_swapped(origin_head, clone, tx);
2604 spa_history_log_internal_ds(clone, "clone swap", tx,
2605 "parent=%s", origin_head->ds_dir->dd_myname);
2609 * Given a pool name and a dataset object number in that pool,
2610 * return the name of that dataset.
2613 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2619 error = dsl_pool_hold(pname, FTAG, &dp);
2623 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
2625 dsl_dataset_name(ds, buf);
2626 dsl_dataset_rele(ds, FTAG);
2628 dsl_pool_rele(dp, FTAG);
2634 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
2635 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
2639 ASSERT3S(asize, >, 0);
2642 * *ref_rsrv is the portion of asize that will come from any
2643 * unconsumed refreservation space.
2647 mutex_enter(&ds->ds_lock);
2649 * Make a space adjustment for reserved bytes.
2651 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
2653 ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2654 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2656 asize - MIN(asize, parent_delta(ds, asize + inflight));
2659 if (!check_quota || ds->ds_quota == 0) {
2660 mutex_exit(&ds->ds_lock);
2664 * If they are requesting more space, and our current estimate
2665 * is over quota, they get to try again unless the actual
2666 * on-disk is over quota and there are no pending changes (which
2667 * may free up space for us).
2669 if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
2671 ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
2672 error = SET_ERROR(ERESTART);
2674 error = SET_ERROR(EDQUOT);
2676 mutex_exit(&ds->ds_lock);
2681 typedef struct dsl_dataset_set_qr_arg {
2682 const char *ddsqra_name;
2683 zprop_source_t ddsqra_source;
2684 uint64_t ddsqra_value;
2685 } dsl_dataset_set_qr_arg_t;
2690 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
2692 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2693 dsl_pool_t *dp = dmu_tx_pool(tx);
2698 if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2699 return (SET_ERROR(ENOTSUP));
2701 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2705 if (dsl_dataset_is_snapshot(ds)) {
2706 dsl_dataset_rele(ds, FTAG);
2707 return (SET_ERROR(EINVAL));
2710 error = dsl_prop_predict(ds->ds_dir,
2711 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2712 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2714 dsl_dataset_rele(ds, FTAG);
2719 dsl_dataset_rele(ds, FTAG);
2723 if (newval < ds->ds_phys->ds_referenced_bytes ||
2724 newval < ds->ds_reserved) {
2725 dsl_dataset_rele(ds, FTAG);
2726 return (SET_ERROR(ENOSPC));
2729 dsl_dataset_rele(ds, FTAG);
2734 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
2736 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2737 dsl_pool_t *dp = dmu_tx_pool(tx);
2741 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2743 dsl_prop_set_sync_impl(ds,
2744 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2745 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
2746 &ddsqra->ddsqra_value, tx);
2748 VERIFY0(dsl_prop_get_int_ds(ds,
2749 zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
2751 if (ds->ds_quota != newval) {
2752 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2753 ds->ds_quota = newval;
2755 dsl_dataset_rele(ds, FTAG);
2759 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
2762 dsl_dataset_set_qr_arg_t ddsqra;
2764 ddsqra.ddsqra_name = dsname;
2765 ddsqra.ddsqra_source = source;
2766 ddsqra.ddsqra_value = refquota;
2768 return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
2769 dsl_dataset_set_refquota_sync, &ddsqra, 0));
2773 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
2775 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2776 dsl_pool_t *dp = dmu_tx_pool(tx);
2779 uint64_t newval, unique;
2781 if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2782 return (SET_ERROR(ENOTSUP));
2784 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2788 if (dsl_dataset_is_snapshot(ds)) {
2789 dsl_dataset_rele(ds, FTAG);
2790 return (SET_ERROR(EINVAL));
2793 error = dsl_prop_predict(ds->ds_dir,
2794 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2795 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2797 dsl_dataset_rele(ds, FTAG);
2802 * If we are doing the preliminary check in open context, the
2803 * space estimates may be inaccurate.
2805 if (!dmu_tx_is_syncing(tx)) {
2806 dsl_dataset_rele(ds, FTAG);
2810 mutex_enter(&ds->ds_lock);
2811 if (!DS_UNIQUE_IS_ACCURATE(ds))
2812 dsl_dataset_recalc_head_uniq(ds);
2813 unique = ds->ds_phys->ds_unique_bytes;
2814 mutex_exit(&ds->ds_lock);
2816 if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
2817 uint64_t delta = MAX(unique, newval) -
2818 MAX(unique, ds->ds_reserved);
2821 dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
2822 (ds->ds_quota > 0 && newval > ds->ds_quota)) {
2823 dsl_dataset_rele(ds, FTAG);
2824 return (SET_ERROR(ENOSPC));
2828 dsl_dataset_rele(ds, FTAG);
2833 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
2834 zprop_source_t source, uint64_t value, dmu_tx_t *tx)
2840 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2841 source, sizeof (value), 1, &value, tx);
2843 VERIFY0(dsl_prop_get_int_ds(ds,
2844 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
2846 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2847 mutex_enter(&ds->ds_dir->dd_lock);
2848 mutex_enter(&ds->ds_lock);
2849 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
2850 unique = ds->ds_phys->ds_unique_bytes;
2851 delta = MAX(0, (int64_t)(newval - unique)) -
2852 MAX(0, (int64_t)(ds->ds_reserved - unique));
2853 ds->ds_reserved = newval;
2854 mutex_exit(&ds->ds_lock);
2856 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
2857 mutex_exit(&ds->ds_dir->dd_lock);
2861 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
2863 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2864 dsl_pool_t *dp = dmu_tx_pool(tx);
2867 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2868 dsl_dataset_set_refreservation_sync_impl(ds,
2869 ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
2870 dsl_dataset_rele(ds, FTAG);
2874 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
2875 uint64_t refreservation)
2877 dsl_dataset_set_qr_arg_t ddsqra;
2879 ddsqra.ddsqra_name = dsname;
2880 ddsqra.ddsqra_source = source;
2881 ddsqra.ddsqra_value = refreservation;
2883 return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
2884 dsl_dataset_set_refreservation_sync, &ddsqra, 0));
2888 * Return (in *usedp) the amount of space written in new that is not
2889 * present in oldsnap. New may be a snapshot or the head. Old must be
2890 * a snapshot before new, in new's filesystem (or its origin). If not then
2891 * fail and return EINVAL.
2893 * The written space is calculated by considering two components: First, we
2894 * ignore any freed space, and calculate the written as new's used space
2895 * minus old's used space. Next, we add in the amount of space that was freed
2896 * between the two snapshots, thus reducing new's used space relative to old's.
2897 * Specifically, this is the space that was born before old->ds_creation_txg,
2898 * and freed before new (ie. on new's deadlist or a previous deadlist).
2900 * space freed [---------------------]
2901 * snapshots ---O-------O--------O-------O------
2905 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
2906 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2910 dsl_pool_t *dp = new->ds_dir->dd_pool;
2912 ASSERT(dsl_pool_config_held(dp));
2915 *usedp += new->ds_phys->ds_referenced_bytes;
2916 *usedp -= oldsnap->ds_phys->ds_referenced_bytes;
2919 *compp += new->ds_phys->ds_compressed_bytes;
2920 *compp -= oldsnap->ds_phys->ds_compressed_bytes;
2923 *uncompp += new->ds_phys->ds_uncompressed_bytes;
2924 *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
2926 snapobj = new->ds_object;
2927 while (snapobj != oldsnap->ds_object) {
2928 dsl_dataset_t *snap;
2929 uint64_t used, comp, uncomp;
2931 if (snapobj == new->ds_object) {
2934 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
2939 if (snap->ds_phys->ds_prev_snap_txg ==
2940 oldsnap->ds_phys->ds_creation_txg) {
2942 * The blocks in the deadlist can not be born after
2943 * ds_prev_snap_txg, so get the whole deadlist space,
2944 * which is more efficient (especially for old-format
2945 * deadlists). Unfortunately the deadlist code
2946 * doesn't have enough information to make this
2947 * optimization itself.
2949 dsl_deadlist_space(&snap->ds_deadlist,
2950 &used, &comp, &uncomp);
2952 dsl_deadlist_space_range(&snap->ds_deadlist,
2953 0, oldsnap->ds_phys->ds_creation_txg,
2954 &used, &comp, &uncomp);
2961 * If we get to the beginning of the chain of snapshots
2962 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
2963 * was not a snapshot of/before new.
2965 snapobj = snap->ds_phys->ds_prev_snap_obj;
2967 dsl_dataset_rele(snap, FTAG);
2969 err = SET_ERROR(EINVAL);
2978 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
2979 * lastsnap, and all snapshots in between are deleted.
2981 * blocks that would be freed [---------------------------]
2982 * snapshots ---O-------O--------O-------O--------O
2983 * firstsnap lastsnap
2985 * This is the set of blocks that were born after the snap before firstsnap,
2986 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
2987 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
2988 * We calculate this by iterating over the relevant deadlists (from the snap
2989 * after lastsnap, backward to the snap after firstsnap), summing up the
2990 * space on the deadlist that was born after the snap before firstsnap.
2993 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
2994 dsl_dataset_t *lastsnap,
2995 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2999 dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
3001 ASSERT(dsl_dataset_is_snapshot(firstsnap));
3002 ASSERT(dsl_dataset_is_snapshot(lastsnap));
3005 * Check that the snapshots are in the same dsl_dir, and firstsnap
3006 * is before lastsnap.
3008 if (firstsnap->ds_dir != lastsnap->ds_dir ||
3009 firstsnap->ds_phys->ds_creation_txg >
3010 lastsnap->ds_phys->ds_creation_txg)
3011 return (SET_ERROR(EINVAL));
3013 *usedp = *compp = *uncompp = 0;
3015 snapobj = lastsnap->ds_phys->ds_next_snap_obj;
3016 while (snapobj != firstsnap->ds_object) {
3018 uint64_t used, comp, uncomp;
3020 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
3024 dsl_deadlist_space_range(&ds->ds_deadlist,
3025 firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
3026 &used, &comp, &uncomp);
3031 snapobj = ds->ds_phys->ds_prev_snap_obj;
3032 ASSERT3U(snapobj, !=, 0);
3033 dsl_dataset_rele(ds, FTAG);
3039 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
3040 * For example, they could both be snapshots of the same filesystem, and
3041 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
3042 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
3043 * filesystem. Or 'earlier' could be the origin's origin.
3046 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier)
3048 dsl_pool_t *dp = later->ds_dir->dd_pool;
3052 ASSERT(dsl_pool_config_held(dp));
3054 if (earlier->ds_phys->ds_creation_txg >=
3055 later->ds_phys->ds_creation_txg)
3058 if (later->ds_dir == earlier->ds_dir)
3060 if (!dsl_dir_is_clone(later->ds_dir))
3063 if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object)
3065 dsl_dataset_t *origin;
3066 error = dsl_dataset_hold_obj(dp,
3067 later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin);
3070 ret = dsl_dataset_is_before(origin, earlier);
3071 dsl_dataset_rele(origin, FTAG);