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 * Portions Copyright (c) 2011 Martin Matuska <mm@FreeBSD.org>
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2014 RackTop Systems.
29 #include <sys/dmu_objset.h>
30 #include <sys/dsl_dataset.h>
31 #include <sys/dsl_dir.h>
32 #include <sys/dsl_prop.h>
33 #include <sys/dsl_synctask.h>
34 #include <sys/dmu_traverse.h>
35 #include <sys/dmu_impl.h>
36 #include <sys/dmu_tx.h>
40 #include <sys/zfeature.h>
41 #include <sys/unique.h>
42 #include <sys/zfs_context.h>
43 #include <sys/zfs_ioctl.h>
45 #include <sys/zfs_znode.h>
46 #include <sys/zfs_onexit.h>
48 #include <sys/dsl_scan.h>
49 #include <sys/dsl_deadlist.h>
50 #include <sys/dsl_destroy.h>
51 #include <sys/dsl_userhold.h>
53 #define SWITCH64(x, y) \
55 uint64_t __tmp = (x); \
60 #define DS_REF_MAX (1ULL << 62)
62 #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE
65 * Figure out how much of this delta should be propogated to the dsl_dir
66 * layer. If there's a refreservation, that space has already been
67 * partially accounted for in our ancestors.
70 parent_delta(dsl_dataset_t *ds, int64_t delta)
72 uint64_t old_bytes, new_bytes;
74 if (ds->ds_reserved == 0)
77 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
78 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
80 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
81 return (new_bytes - old_bytes);
85 dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
87 int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
88 int compressed = BP_GET_PSIZE(bp);
89 int uncompressed = BP_GET_UCSIZE(bp);
92 dprintf_bp(bp, "ds=%p", ds);
94 ASSERT(dmu_tx_is_syncing(tx));
95 /* It could have been compressed away to nothing */
98 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
99 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
101 dsl_pool_mos_diduse_space(tx->tx_pool,
102 used, compressed, uncompressed);
106 dmu_buf_will_dirty(ds->ds_dbuf, tx);
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);
121 dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
124 int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
125 int compressed = BP_GET_PSIZE(bp);
126 int uncompressed = BP_GET_UCSIZE(bp);
131 ASSERT(dmu_tx_is_syncing(tx));
132 ASSERT(bp->blk_birth <= tx->tx_txg);
135 dsl_free(tx->tx_pool, tx->tx_txg, bp);
136 dsl_pool_mos_diduse_space(tx->tx_pool,
137 -used, -compressed, -uncompressed);
140 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
142 ASSERT(!dsl_dataset_is_snapshot(ds));
143 dmu_buf_will_dirty(ds->ds_dbuf, tx);
145 if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
148 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
149 dsl_free(tx->tx_pool, tx->tx_txg, bp);
151 mutex_enter(&ds->ds_lock);
152 ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
153 !DS_UNIQUE_IS_ACCURATE(ds));
154 delta = parent_delta(ds, -used);
155 ds->ds_phys->ds_unique_bytes -= used;
156 mutex_exit(&ds->ds_lock);
157 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
158 delta, -compressed, -uncompressed, tx);
159 dsl_dir_transfer_space(ds->ds_dir, -used - delta,
160 DD_USED_REFRSRV, DD_USED_HEAD, tx);
162 dprintf_bp(bp, "putting on dead list: %s", "");
165 * We are here as part of zio's write done callback,
166 * which means we're a zio interrupt thread. We can't
167 * call dsl_deadlist_insert() now because it may block
168 * waiting for I/O. Instead, put bp on the deferred
169 * queue and let dsl_pool_sync() finish the job.
171 bplist_append(&ds->ds_pending_deadlist, bp);
173 dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
175 ASSERT3U(ds->ds_prev->ds_object, ==,
176 ds->ds_phys->ds_prev_snap_obj);
177 ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
178 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
179 if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
180 ds->ds_object && bp->blk_birth >
181 ds->ds_prev->ds_phys->ds_prev_snap_txg) {
182 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
183 mutex_enter(&ds->ds_prev->ds_lock);
184 ds->ds_prev->ds_phys->ds_unique_bytes += used;
185 mutex_exit(&ds->ds_prev->ds_lock);
187 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
188 dsl_dir_transfer_space(ds->ds_dir, used,
189 DD_USED_HEAD, DD_USED_SNAP, tx);
192 mutex_enter(&ds->ds_lock);
193 ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used);
194 ds->ds_phys->ds_referenced_bytes -= used;
195 ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
196 ds->ds_phys->ds_compressed_bytes -= compressed;
197 ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
198 ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
199 mutex_exit(&ds->ds_lock);
205 dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
207 uint64_t trysnap = 0;
212 * The snapshot creation could fail, but that would cause an
213 * incorrect FALSE return, which would only result in an
214 * overestimation of the amount of space that an operation would
215 * consume, which is OK.
217 * There's also a small window where we could miss a pending
218 * snapshot, because we could set the sync task in the quiescing
219 * phase. So this should only be used as a guess.
221 if (ds->ds_trysnap_txg >
222 spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
223 trysnap = ds->ds_trysnap_txg;
224 return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
228 dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
231 if (blk_birth <= dsl_dataset_prev_snap_txg(ds) ||
232 (bp != NULL && BP_IS_HOLE(bp)))
235 ddt_prefetch(dsl_dataset_get_spa(ds), bp);
242 dsl_dataset_evict(dmu_buf_t *db, void *dsv)
244 dsl_dataset_t *ds = dsv;
246 ASSERT(ds->ds_owner == NULL);
248 unique_remove(ds->ds_fsid_guid);
250 if (ds->ds_objset != NULL)
251 dmu_objset_evict(ds->ds_objset);
254 dsl_dataset_rele(ds->ds_prev, ds);
258 bplist_destroy(&ds->ds_pending_deadlist);
259 if (ds->ds_phys->ds_deadlist_obj != 0)
260 dsl_deadlist_close(&ds->ds_deadlist);
262 dsl_dir_rele(ds->ds_dir, ds);
264 ASSERT(!list_link_active(&ds->ds_synced_link));
266 if (mutex_owned(&ds->ds_lock))
267 mutex_exit(&ds->ds_lock);
268 mutex_destroy(&ds->ds_lock);
269 if (mutex_owned(&ds->ds_opening_lock))
270 mutex_exit(&ds->ds_opening_lock);
271 mutex_destroy(&ds->ds_opening_lock);
272 refcount_destroy(&ds->ds_longholds);
274 kmem_free(ds, sizeof (dsl_dataset_t));
278 dsl_dataset_get_snapname(dsl_dataset_t *ds)
280 dsl_dataset_phys_t *headphys;
283 dsl_pool_t *dp = ds->ds_dir->dd_pool;
284 objset_t *mos = dp->dp_meta_objset;
286 if (ds->ds_snapname[0])
288 if (ds->ds_phys->ds_next_snap_obj == 0)
291 err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
295 headphys = headdbuf->db_data;
296 err = zap_value_search(dp->dp_meta_objset,
297 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
298 dmu_buf_rele(headdbuf, FTAG);
303 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
305 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
306 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
310 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
315 err = zap_lookup_norm(mos, snapobj, name, 8, 1,
316 value, mt, NULL, 0, NULL);
317 if (err == ENOTSUP && mt == MT_FIRST)
318 err = zap_lookup(mos, snapobj, name, 8, 1, value);
323 dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx)
325 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
326 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
330 dsl_dir_snap_cmtime_update(ds->ds_dir);
332 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
337 err = zap_remove_norm(mos, snapobj, name, mt, tx);
338 if (err == ENOTSUP && mt == MT_FIRST)
339 err = zap_remove(mos, snapobj, name, tx);
344 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
347 objset_t *mos = dp->dp_meta_objset;
351 dmu_object_info_t doi;
353 ASSERT(dsl_pool_config_held(dp));
355 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
359 /* Make sure dsobj has the correct object type. */
360 dmu_object_info_from_db(dbuf, &doi);
361 if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
362 dmu_buf_rele(dbuf, tag);
363 return (SET_ERROR(EINVAL));
366 ds = dmu_buf_get_user(dbuf);
368 dsl_dataset_t *winner = NULL;
370 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
372 ds->ds_object = dsobj;
373 ds->ds_phys = dbuf->db_data;
375 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
376 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
377 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
378 refcount_create(&ds->ds_longholds);
380 bplist_create(&ds->ds_pending_deadlist);
381 dsl_deadlist_open(&ds->ds_deadlist,
382 mos, ds->ds_phys->ds_deadlist_obj);
384 list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
385 offsetof(dmu_sendarg_t, dsa_link));
388 err = dsl_dir_hold_obj(dp,
389 ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
392 mutex_destroy(&ds->ds_lock);
393 mutex_destroy(&ds->ds_opening_lock);
394 refcount_destroy(&ds->ds_longholds);
395 bplist_destroy(&ds->ds_pending_deadlist);
396 dsl_deadlist_close(&ds->ds_deadlist);
397 kmem_free(ds, sizeof (dsl_dataset_t));
398 dmu_buf_rele(dbuf, tag);
402 if (!dsl_dataset_is_snapshot(ds)) {
403 ds->ds_snapname[0] = '\0';
404 if (ds->ds_phys->ds_prev_snap_obj != 0) {
405 err = dsl_dataset_hold_obj(dp,
406 ds->ds_phys->ds_prev_snap_obj,
410 if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
411 err = dsl_dataset_get_snapname(ds);
412 if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
414 ds->ds_dir->dd_pool->dp_meta_objset,
415 ds->ds_phys->ds_userrefs_obj,
420 if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
421 err = dsl_prop_get_int_ds(ds,
422 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
425 err = dsl_prop_get_int_ds(ds,
426 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
430 ds->ds_reserved = ds->ds_quota = 0;
433 if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds,
434 &ds->ds_phys, dsl_dataset_evict)) != NULL) {
435 bplist_destroy(&ds->ds_pending_deadlist);
436 dsl_deadlist_close(&ds->ds_deadlist);
438 dsl_dataset_rele(ds->ds_prev, ds);
439 dsl_dir_rele(ds->ds_dir, ds);
440 mutex_destroy(&ds->ds_lock);
441 mutex_destroy(&ds->ds_opening_lock);
442 refcount_destroy(&ds->ds_longholds);
443 kmem_free(ds, sizeof (dsl_dataset_t));
445 dmu_buf_rele(dbuf, tag);
451 unique_insert(ds->ds_phys->ds_fsid_guid);
454 ASSERT3P(ds->ds_dbuf, ==, dbuf);
455 ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
456 ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
457 spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
458 dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
464 dsl_dataset_hold(dsl_pool_t *dp, const char *name,
465 void *tag, dsl_dataset_t **dsp)
468 const char *snapname;
472 err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
476 ASSERT(dsl_pool_config_held(dp));
477 obj = dd->dd_phys->dd_head_dataset_obj;
479 err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
481 err = SET_ERROR(ENOENT);
483 /* we may be looking for a snapshot */
484 if (err == 0 && snapname != NULL) {
487 if (*snapname++ != '@') {
488 dsl_dataset_rele(*dsp, tag);
489 dsl_dir_rele(dd, FTAG);
490 return (SET_ERROR(ENOENT));
493 dprintf("looking for snapshot '%s'\n", snapname);
494 err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
496 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
497 dsl_dataset_rele(*dsp, tag);
500 mutex_enter(&ds->ds_lock);
501 if (ds->ds_snapname[0] == 0)
502 (void) strlcpy(ds->ds_snapname, snapname,
503 sizeof (ds->ds_snapname));
504 mutex_exit(&ds->ds_lock);
509 dsl_dir_rele(dd, FTAG);
514 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
515 void *tag, dsl_dataset_t **dsp)
517 int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
520 if (!dsl_dataset_tryown(*dsp, tag)) {
521 dsl_dataset_rele(*dsp, tag);
523 return (SET_ERROR(EBUSY));
529 dsl_dataset_own(dsl_pool_t *dp, const char *name,
530 void *tag, dsl_dataset_t **dsp)
532 int err = dsl_dataset_hold(dp, name, tag, dsp);
535 if (!dsl_dataset_tryown(*dsp, tag)) {
536 dsl_dataset_rele(*dsp, tag);
537 return (SET_ERROR(EBUSY));
543 * See the comment above dsl_pool_hold() for details. In summary, a long
544 * hold is used to prevent destruction of a dataset while the pool hold
545 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
547 * The dataset and pool must be held when this function is called. After it
548 * is called, the pool hold may be released while the dataset is still held
552 dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
554 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
555 (void) refcount_add(&ds->ds_longholds, tag);
559 dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
561 (void) refcount_remove(&ds->ds_longholds, tag);
564 /* Return B_TRUE if there are any long holds on this dataset. */
566 dsl_dataset_long_held(dsl_dataset_t *ds)
568 return (!refcount_is_zero(&ds->ds_longholds));
572 dsl_dataset_name(dsl_dataset_t *ds, char *name)
575 (void) strcpy(name, "mos");
577 dsl_dir_name(ds->ds_dir, name);
578 VERIFY0(dsl_dataset_get_snapname(ds));
579 if (ds->ds_snapname[0]) {
580 (void) strcat(name, "@");
582 * We use a "recursive" mutex so that we
583 * can call dprintf_ds() with ds_lock held.
585 if (!MUTEX_HELD(&ds->ds_lock)) {
586 mutex_enter(&ds->ds_lock);
587 (void) strcat(name, ds->ds_snapname);
588 mutex_exit(&ds->ds_lock);
590 (void) strcat(name, ds->ds_snapname);
597 dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
599 dmu_buf_rele(ds->ds_dbuf, tag);
603 dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
605 ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL);
607 mutex_enter(&ds->ds_lock);
609 mutex_exit(&ds->ds_lock);
610 dsl_dataset_long_rele(ds, tag);
611 if (ds->ds_dbuf != NULL)
612 dsl_dataset_rele(ds, tag);
614 dsl_dataset_evict(NULL, ds);
618 dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
620 boolean_t gotit = FALSE;
622 mutex_enter(&ds->ds_lock);
623 if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
625 dsl_dataset_long_hold(ds, tag);
628 mutex_exit(&ds->ds_lock);
633 dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
634 uint64_t flags, dmu_tx_t *tx)
636 dsl_pool_t *dp = dd->dd_pool;
638 dsl_dataset_phys_t *dsphys;
640 objset_t *mos = dp->dp_meta_objset;
643 origin = dp->dp_origin_snap;
645 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
646 ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
647 ASSERT(dmu_tx_is_syncing(tx));
648 ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
650 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
651 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
652 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
653 dmu_buf_will_dirty(dbuf, tx);
654 dsphys = dbuf->db_data;
655 bzero(dsphys, sizeof (dsl_dataset_phys_t));
656 dsphys->ds_dir_obj = dd->dd_object;
657 dsphys->ds_flags = flags;
658 dsphys->ds_fsid_guid = unique_create();
660 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
661 sizeof (dsphys->ds_guid));
662 } while (dsphys->ds_guid == 0);
663 dsphys->ds_snapnames_zapobj =
664 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
666 dsphys->ds_creation_time = gethrestime_sec();
667 dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
669 if (origin == NULL) {
670 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
672 dsl_dataset_t *ohds; /* head of the origin snapshot */
674 dsphys->ds_prev_snap_obj = origin->ds_object;
675 dsphys->ds_prev_snap_txg =
676 origin->ds_phys->ds_creation_txg;
677 dsphys->ds_referenced_bytes =
678 origin->ds_phys->ds_referenced_bytes;
679 dsphys->ds_compressed_bytes =
680 origin->ds_phys->ds_compressed_bytes;
681 dsphys->ds_uncompressed_bytes =
682 origin->ds_phys->ds_uncompressed_bytes;
683 dsphys->ds_bp = origin->ds_phys->ds_bp;
684 dsphys->ds_flags |= origin->ds_phys->ds_flags;
686 dmu_buf_will_dirty(origin->ds_dbuf, tx);
687 origin->ds_phys->ds_num_children++;
689 VERIFY0(dsl_dataset_hold_obj(dp,
690 origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
691 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
692 dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
693 dsl_dataset_rele(ohds, FTAG);
695 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
696 if (origin->ds_phys->ds_next_clones_obj == 0) {
697 origin->ds_phys->ds_next_clones_obj =
699 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
701 VERIFY0(zap_add_int(mos,
702 origin->ds_phys->ds_next_clones_obj, dsobj, tx));
705 dmu_buf_will_dirty(dd->dd_dbuf, tx);
706 dd->dd_phys->dd_origin_obj = origin->ds_object;
707 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
708 if (origin->ds_dir->dd_phys->dd_clones == 0) {
709 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
710 origin->ds_dir->dd_phys->dd_clones =
712 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
714 VERIFY0(zap_add_int(mos,
715 origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
719 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
720 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
722 dmu_buf_rele(dbuf, FTAG);
724 dmu_buf_will_dirty(dd->dd_dbuf, tx);
725 dd->dd_phys->dd_head_dataset_obj = dsobj;
731 dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
735 VERIFY0(dmu_objset_from_ds(ds, &os));
736 bzero(&os->os_zil_header, sizeof (os->os_zil_header));
737 dsl_dataset_dirty(ds, tx);
741 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
742 dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
744 dsl_pool_t *dp = pdd->dd_pool;
745 uint64_t dsobj, ddobj;
748 ASSERT(dmu_tx_is_syncing(tx));
749 ASSERT(lastname[0] != '@');
751 ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
752 VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
754 dsobj = dsl_dataset_create_sync_dd(dd, origin,
755 flags & ~DS_CREATE_FLAG_NODIRTY, tx);
757 dsl_deleg_set_create_perms(dd, tx, cr);
759 dsl_dir_rele(dd, FTAG);
762 * If we are creating a clone, make sure we zero out any stale
763 * data from the origin snapshots zil header.
765 if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
768 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
769 dsl_dataset_zero_zil(ds, tx);
770 dsl_dataset_rele(ds, FTAG);
777 /* FreeBSD ioctl compat begin */
780 const char *snapname;
784 dsl_check_snap_cb(const char *name, void *arg)
786 struct destroyarg *da = arg;
790 dsname = kmem_asprintf("%s@%s", name, da->snapname);
791 fnvlist_add_boolean(da->nvl, dsname);
792 kmem_free(dsname, strlen(dsname) + 1);
798 dmu_get_recursive_snaps_nvl(char *fsname, const char *snapname,
801 struct destroyarg *da;
804 da = kmem_zalloc(sizeof (struct destroyarg), KM_SLEEP);
806 da->snapname = snapname;
807 err = dmu_objset_find(fsname, dsl_check_snap_cb, da,
809 kmem_free(da, sizeof (struct destroyarg));
813 /* FreeBSD ioctl compat end */
814 #endif /* __FreeBSD__ */
817 * The unique space in the head dataset can be calculated by subtracting
818 * the space used in the most recent snapshot, that is still being used
819 * in this file system, from the space currently in use. To figure out
820 * the space in the most recent snapshot still in use, we need to take
821 * the total space used in the snapshot and subtract out the space that
822 * has been freed up since the snapshot was taken.
825 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
828 uint64_t dlused, dlcomp, dluncomp;
830 ASSERT(!dsl_dataset_is_snapshot(ds));
832 if (ds->ds_phys->ds_prev_snap_obj != 0)
833 mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
837 dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
839 ASSERT3U(dlused, <=, mrs_used);
840 ds->ds_phys->ds_unique_bytes =
841 ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
843 if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
844 SPA_VERSION_UNIQUE_ACCURATE)
845 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
849 dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
852 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
856 ASSERT(ds->ds_phys->ds_num_children >= 2);
857 err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
859 * The err should not be ENOENT, but a bug in a previous version
860 * of the code could cause upgrade_clones_cb() to not set
861 * ds_next_snap_obj when it should, leading to a missing entry.
862 * If we knew that the pool was created after
863 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
864 * ENOENT. However, at least we can check that we don't have
865 * too many entries in the next_clones_obj even after failing to
870 ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
872 ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
877 dsl_dataset_get_blkptr(dsl_dataset_t *ds)
879 return (&ds->ds_phys->ds_bp);
883 dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
885 ASSERT(dmu_tx_is_syncing(tx));
886 /* If it's the meta-objset, set dp_meta_rootbp */
888 tx->tx_pool->dp_meta_rootbp = *bp;
890 dmu_buf_will_dirty(ds->ds_dbuf, tx);
891 ds->ds_phys->ds_bp = *bp;
896 dsl_dataset_get_spa(dsl_dataset_t *ds)
898 return (ds->ds_dir->dd_pool->dp_spa);
902 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
906 if (ds == NULL) /* this is the meta-objset */
909 ASSERT(ds->ds_objset != NULL);
911 if (ds->ds_phys->ds_next_snap_obj != 0)
912 panic("dirtying snapshot!");
914 dp = ds->ds_dir->dd_pool;
916 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
917 /* up the hold count until we can be written out */
918 dmu_buf_add_ref(ds->ds_dbuf, ds);
923 dsl_dataset_is_dirty(dsl_dataset_t *ds)
925 for (int t = 0; t < TXG_SIZE; t++) {
926 if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
934 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
938 if (!dmu_tx_is_syncing(tx))
942 * If there's an fs-only reservation, any blocks that might become
943 * owned by the snapshot dataset must be accommodated by space
944 * outside of the reservation.
946 ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
947 asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
948 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
949 return (SET_ERROR(ENOSPC));
952 * Propagate any reserved space for this snapshot to other
953 * snapshot checks in this sync group.
956 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
961 typedef struct dsl_dataset_snapshot_arg {
962 nvlist_t *ddsa_snaps;
963 nvlist_t *ddsa_props;
964 nvlist_t *ddsa_errors;
965 } dsl_dataset_snapshot_arg_t;
968 dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
969 dmu_tx_t *tx, boolean_t recv)
974 ds->ds_trysnap_txg = tx->tx_txg;
976 if (!dmu_tx_is_syncing(tx))
980 * We don't allow multiple snapshots of the same txg. If there
981 * is already one, try again.
983 if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
984 return (SET_ERROR(EAGAIN));
987 * Check for conflicting snapshot name.
989 error = dsl_dataset_snap_lookup(ds, snapname, &value);
991 return (SET_ERROR(EEXIST));
996 * We don't allow taking snapshots of inconsistent datasets, such as
997 * those into which we are currently receiving. However, if we are
998 * creating this snapshot as part of a receive, this check will be
999 * executed atomically with respect to the completion of the receive
1000 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1001 * case we ignore this, knowing it will be fixed up for us shortly in
1002 * dmu_recv_end_sync().
1004 if (!recv && DS_IS_INCONSISTENT(ds))
1005 return (SET_ERROR(EBUSY));
1007 error = dsl_dataset_snapshot_reserve_space(ds, tx);
1015 dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1017 dsl_dataset_snapshot_arg_t *ddsa = arg;
1018 dsl_pool_t *dp = dmu_tx_pool(tx);
1022 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1023 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1027 char dsname[MAXNAMELEN];
1029 name = nvpair_name(pair);
1030 if (strlen(name) >= MAXNAMELEN)
1031 error = SET_ERROR(ENAMETOOLONG);
1033 atp = strchr(name, '@');
1035 error = SET_ERROR(EINVAL);
1037 (void) strlcpy(dsname, name, atp - name + 1);
1040 error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1042 error = dsl_dataset_snapshot_check_impl(ds,
1043 atp + 1, tx, B_FALSE);
1044 dsl_dataset_rele(ds, FTAG);
1048 if (ddsa->ddsa_errors != NULL) {
1049 fnvlist_add_int32(ddsa->ddsa_errors,
1059 dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1062 static zil_header_t zero_zil;
1064 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1066 dsl_dataset_phys_t *dsphys;
1067 uint64_t dsobj, crtxg;
1068 objset_t *mos = dp->dp_meta_objset;
1071 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1074 * If we are on an old pool, the zil must not be active, in which
1075 * case it will be zeroed. Usually zil_suspend() accomplishes this.
1077 ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1078 dmu_objset_from_ds(ds, &os) != 0 ||
1079 bcmp(&os->os_phys->os_zil_header, &zero_zil,
1080 sizeof (zero_zil)) == 0);
1084 * The origin's ds_creation_txg has to be < TXG_INITIAL
1086 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1091 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1092 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1093 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1094 dmu_buf_will_dirty(dbuf, tx);
1095 dsphys = dbuf->db_data;
1096 bzero(dsphys, sizeof (dsl_dataset_phys_t));
1097 dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1098 dsphys->ds_fsid_guid = unique_create();
1100 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1101 sizeof (dsphys->ds_guid));
1102 } while (dsphys->ds_guid == 0);
1103 dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1104 dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1105 dsphys->ds_next_snap_obj = ds->ds_object;
1106 dsphys->ds_num_children = 1;
1107 dsphys->ds_creation_time = gethrestime_sec();
1108 dsphys->ds_creation_txg = crtxg;
1109 dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1110 dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
1111 dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1112 dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
1113 dsphys->ds_flags = ds->ds_phys->ds_flags;
1114 dsphys->ds_bp = ds->ds_phys->ds_bp;
1115 dmu_buf_rele(dbuf, FTAG);
1117 ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
1119 uint64_t next_clones_obj =
1120 ds->ds_prev->ds_phys->ds_next_clones_obj;
1121 ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1123 ds->ds_prev->ds_phys->ds_num_children > 1);
1124 if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
1125 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1126 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1127 ds->ds_prev->ds_phys->ds_creation_txg);
1128 ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1129 } else if (next_clones_obj != 0) {
1130 dsl_dataset_remove_from_next_clones(ds->ds_prev,
1131 dsphys->ds_next_snap_obj, tx);
1132 VERIFY0(zap_add_int(mos,
1133 next_clones_obj, dsobj, tx));
1138 * If we have a reference-reservation on this dataset, we will
1139 * need to increase the amount of refreservation being charged
1140 * since our unique space is going to zero.
1142 if (ds->ds_reserved) {
1144 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1145 delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
1146 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1150 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1151 ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
1152 UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
1153 dsl_deadlist_close(&ds->ds_deadlist);
1154 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1155 dsl_deadlist_add_key(&ds->ds_deadlist,
1156 ds->ds_phys->ds_prev_snap_txg, tx);
1158 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1159 ds->ds_phys->ds_prev_snap_obj = dsobj;
1160 ds->ds_phys->ds_prev_snap_txg = crtxg;
1161 ds->ds_phys->ds_unique_bytes = 0;
1162 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1163 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1165 VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1166 snapname, 8, 1, &dsobj, tx));
1169 dsl_dataset_rele(ds->ds_prev, ds);
1170 VERIFY0(dsl_dataset_hold_obj(dp,
1171 ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
1173 dsl_scan_ds_snapshotted(ds, tx);
1175 dsl_dir_snap_cmtime_update(ds->ds_dir);
1177 spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1181 dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1183 dsl_dataset_snapshot_arg_t *ddsa = arg;
1184 dsl_pool_t *dp = dmu_tx_pool(tx);
1187 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1188 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1191 char dsname[MAXNAMELEN];
1193 name = nvpair_name(pair);
1194 atp = strchr(name, '@');
1195 (void) strlcpy(dsname, name, atp - name + 1);
1196 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1198 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1199 if (ddsa->ddsa_props != NULL) {
1200 dsl_props_set_sync_impl(ds->ds_prev,
1201 ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1203 dsl_dataset_rele(ds, FTAG);
1208 * The snapshots must all be in the same pool.
1209 * All-or-nothing: if there are any failures, nothing will be modified.
1212 dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1214 dsl_dataset_snapshot_arg_t ddsa;
1216 boolean_t needsuspend;
1220 nvlist_t *suspended = NULL;
1222 pair = nvlist_next_nvpair(snaps, NULL);
1225 firstname = nvpair_name(pair);
1227 error = spa_open(firstname, &spa, FTAG);
1230 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1231 spa_close(spa, FTAG);
1234 suspended = fnvlist_alloc();
1235 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1236 pair = nvlist_next_nvpair(snaps, pair)) {
1237 char fsname[MAXNAMELEN];
1238 char *snapname = nvpair_name(pair);
1242 atp = strchr(snapname, '@');
1244 error = SET_ERROR(EINVAL);
1247 (void) strlcpy(fsname, snapname, atp - snapname + 1);
1249 error = zil_suspend(fsname, &cookie);
1252 fnvlist_add_uint64(suspended, fsname,
1257 ddsa.ddsa_snaps = snaps;
1258 ddsa.ddsa_props = props;
1259 ddsa.ddsa_errors = errors;
1262 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1263 dsl_dataset_snapshot_sync, &ddsa,
1264 fnvlist_num_pairs(snaps) * 3);
1267 if (suspended != NULL) {
1268 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1269 pair = nvlist_next_nvpair(suspended, pair)) {
1270 zil_resume((void *)(uintptr_t)
1271 fnvpair_value_uint64(pair));
1273 fnvlist_free(suspended);
1279 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1280 pair = nvlist_next_nvpair(snaps, pair)) {
1281 char *snapname = nvpair_name(pair);
1282 zvol_create_minors(snapname);
1290 typedef struct dsl_dataset_snapshot_tmp_arg {
1291 const char *ddsta_fsname;
1292 const char *ddsta_snapname;
1293 minor_t ddsta_cleanup_minor;
1294 const char *ddsta_htag;
1295 } dsl_dataset_snapshot_tmp_arg_t;
1298 dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1300 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1301 dsl_pool_t *dp = dmu_tx_pool(tx);
1305 error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1309 error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1312 dsl_dataset_rele(ds, FTAG);
1316 if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
1317 dsl_dataset_rele(ds, FTAG);
1318 return (SET_ERROR(ENOTSUP));
1320 error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
1323 dsl_dataset_rele(ds, FTAG);
1327 dsl_dataset_rele(ds, FTAG);
1332 dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
1334 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1335 dsl_pool_t *dp = dmu_tx_pool(tx);
1338 VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
1340 dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
1341 dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
1342 ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
1343 dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
1345 dsl_dataset_rele(ds, FTAG);
1349 dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
1350 minor_t cleanup_minor, const char *htag)
1352 dsl_dataset_snapshot_tmp_arg_t ddsta;
1355 boolean_t needsuspend;
1358 ddsta.ddsta_fsname = fsname;
1359 ddsta.ddsta_snapname = snapname;
1360 ddsta.ddsta_cleanup_minor = cleanup_minor;
1361 ddsta.ddsta_htag = htag;
1363 error = spa_open(fsname, &spa, FTAG);
1366 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1367 spa_close(spa, FTAG);
1370 error = zil_suspend(fsname, &cookie);
1375 error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
1376 dsl_dataset_snapshot_tmp_sync, &ddsta, 3);
1385 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1387 ASSERT(dmu_tx_is_syncing(tx));
1388 ASSERT(ds->ds_objset != NULL);
1389 ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1392 * in case we had to change ds_fsid_guid when we opened it,
1395 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1396 ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
1398 dmu_objset_sync(ds->ds_objset, zio, tx);
1402 get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1405 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1408 nvlist_t *propval = fnvlist_alloc();
1409 nvlist_t *val = fnvlist_alloc();
1411 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1414 * There may be missing entries in ds_next_clones_obj
1415 * due to a bug in a previous version of the code.
1416 * Only trust it if it has the right number of entries.
1418 if (ds->ds_phys->ds_next_clones_obj != 0) {
1419 VERIFY0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
1422 if (count != ds->ds_phys->ds_num_children - 1)
1424 for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
1425 zap_cursor_retrieve(&zc, &za) == 0;
1426 zap_cursor_advance(&zc)) {
1427 dsl_dataset_t *clone;
1428 char buf[ZFS_MAXNAMELEN];
1429 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1430 za.za_first_integer, FTAG, &clone));
1431 dsl_dir_name(clone->ds_dir, buf);
1432 fnvlist_add_boolean(val, buf);
1433 dsl_dataset_rele(clone, FTAG);
1435 zap_cursor_fini(&zc);
1436 fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1437 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
1440 nvlist_free(propval);
1444 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1446 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1447 uint64_t refd, avail, uobjs, aobjs, ratio;
1449 ASSERT(dsl_pool_config_held(dp));
1451 ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
1452 (ds->ds_phys->ds_uncompressed_bytes * 100 /
1453 ds->ds_phys->ds_compressed_bytes);
1455 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
1456 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1457 ds->ds_phys->ds_uncompressed_bytes);
1459 if (dsl_dataset_is_snapshot(ds)) {
1460 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
1461 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1462 ds->ds_phys->ds_unique_bytes);
1463 get_clones_stat(ds, nv);
1465 dsl_dir_stats(ds->ds_dir, nv);
1468 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1469 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1470 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1472 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1473 ds->ds_phys->ds_creation_time);
1474 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1475 ds->ds_phys->ds_creation_txg);
1476 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1478 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1480 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1481 ds->ds_phys->ds_guid);
1482 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1483 ds->ds_phys->ds_unique_bytes);
1484 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
1486 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
1488 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1489 DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1491 if (ds->ds_phys->ds_prev_snap_obj != 0) {
1492 uint64_t written, comp, uncomp;
1493 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1494 dsl_dataset_t *prev;
1496 int err = dsl_dataset_hold_obj(dp,
1497 ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
1499 err = dsl_dataset_space_written(prev, ds, &written,
1501 dsl_dataset_rele(prev, FTAG);
1503 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
1511 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1513 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1514 ASSERT(dsl_pool_config_held(dp));
1516 stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1517 stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
1518 stat->dds_guid = ds->ds_phys->ds_guid;
1519 stat->dds_origin[0] = '\0';
1520 if (dsl_dataset_is_snapshot(ds)) {
1521 stat->dds_is_snapshot = B_TRUE;
1522 stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1524 stat->dds_is_snapshot = B_FALSE;
1525 stat->dds_num_clones = 0;
1527 if (dsl_dir_is_clone(ds->ds_dir)) {
1530 VERIFY0(dsl_dataset_hold_obj(dp,
1531 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
1532 dsl_dataset_name(ods, stat->dds_origin);
1533 dsl_dataset_rele(ods, FTAG);
1539 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1541 return (ds->ds_fsid_guid);
1545 dsl_dataset_space(dsl_dataset_t *ds,
1546 uint64_t *refdbytesp, uint64_t *availbytesp,
1547 uint64_t *usedobjsp, uint64_t *availobjsp)
1549 *refdbytesp = ds->ds_phys->ds_referenced_bytes;
1550 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1551 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
1552 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
1553 if (ds->ds_quota != 0) {
1555 * Adjust available bytes according to refquota
1557 if (*refdbytesp < ds->ds_quota)
1558 *availbytesp = MIN(*availbytesp,
1559 ds->ds_quota - *refdbytesp);
1563 *usedobjsp = ds->ds_phys->ds_bp.blk_fill;
1564 *availobjsp = DN_MAX_OBJECT - *usedobjsp;
1568 dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
1570 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1572 ASSERT(dsl_pool_config_held(dp));
1575 if (ds->ds_phys->ds_bp.blk_birth >
1576 snap->ds_phys->ds_creation_txg) {
1577 objset_t *os, *os_snap;
1579 * It may be that only the ZIL differs, because it was
1580 * reset in the head. Don't count that as being
1583 if (dmu_objset_from_ds(ds, &os) != 0)
1585 if (dmu_objset_from_ds(snap, &os_snap) != 0)
1587 return (bcmp(&os->os_phys->os_meta_dnode,
1588 &os_snap->os_phys->os_meta_dnode,
1589 sizeof (os->os_phys->os_meta_dnode)) != 0);
1594 typedef struct dsl_dataset_rename_snapshot_arg {
1595 const char *ddrsa_fsname;
1596 const char *ddrsa_oldsnapname;
1597 const char *ddrsa_newsnapname;
1598 boolean_t ddrsa_recursive;
1600 } dsl_dataset_rename_snapshot_arg_t;
1604 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
1605 dsl_dataset_t *hds, void *arg)
1607 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1611 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1613 /* ignore nonexistent snapshots */
1614 return (error == ENOENT ? 0 : error);
1617 /* new name should not exist */
1618 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
1620 error = SET_ERROR(EEXIST);
1621 else if (error == ENOENT)
1624 /* dataset name + 1 for the "@" + the new snapshot name must fit */
1625 if (dsl_dir_namelen(hds->ds_dir) + 1 +
1626 strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1627 error = SET_ERROR(ENAMETOOLONG);
1633 dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
1635 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1636 dsl_pool_t *dp = dmu_tx_pool(tx);
1640 error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
1644 if (ddrsa->ddrsa_recursive) {
1645 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1646 dsl_dataset_rename_snapshot_check_impl, ddrsa,
1649 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
1651 dsl_dataset_rele(hds, FTAG);
1656 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
1657 dsl_dataset_t *hds, void *arg)
1661 char *oldname, *newname;
1664 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1667 dmu_tx_t *tx = ddrsa->ddrsa_tx;
1670 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
1671 ASSERT(error == 0 || error == ENOENT);
1672 if (error == ENOENT) {
1673 /* ignore nonexistent snapshots */
1677 VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
1679 /* log before we change the name */
1680 spa_history_log_internal_ds(ds, "rename", tx,
1681 "-> @%s", ddrsa->ddrsa_newsnapname);
1683 VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx));
1684 mutex_enter(&ds->ds_lock);
1685 (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
1686 mutex_exit(&ds->ds_lock);
1687 VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj,
1688 ds->ds_snapname, 8, 1, &ds->ds_object, tx));
1692 oldname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1693 newname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1694 snprintf(oldname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname,
1695 ddrsa->ddrsa_oldsnapname);
1696 snprintf(newname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname,
1697 ddrsa->ddrsa_newsnapname);
1698 zfsvfs_update_fromname(oldname, newname);
1699 zvol_rename_minors(oldname, newname);
1700 kmem_free(newname, MAXPATHLEN);
1701 kmem_free(oldname, MAXPATHLEN);
1704 dsl_dataset_rele(ds, FTAG);
1710 dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
1712 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
1713 dsl_pool_t *dp = dmu_tx_pool(tx);
1716 VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
1717 ddrsa->ddrsa_tx = tx;
1718 if (ddrsa->ddrsa_recursive) {
1719 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
1720 dsl_dataset_rename_snapshot_sync_impl, ddrsa,
1723 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
1725 dsl_dataset_rele(hds, FTAG);
1729 dsl_dataset_rename_snapshot(const char *fsname,
1730 const char *oldsnapname, const char *newsnapname, boolean_t recursive)
1732 dsl_dataset_rename_snapshot_arg_t ddrsa;
1734 ddrsa.ddrsa_fsname = fsname;
1735 ddrsa.ddrsa_oldsnapname = oldsnapname;
1736 ddrsa.ddrsa_newsnapname = newsnapname;
1737 ddrsa.ddrsa_recursive = recursive;
1739 return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
1740 dsl_dataset_rename_snapshot_sync, &ddrsa, 1));
1744 * If we're doing an ownership handoff, we need to make sure that there is
1745 * only one long hold on the dataset. We're not allowed to change anything here
1746 * so we don't permanently release the long hold or regular hold here. We want
1747 * to do this only when syncing to avoid the dataset unexpectedly going away
1748 * when we release the long hold.
1751 dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
1755 if (!dmu_tx_is_syncing(tx))
1758 if (owner != NULL) {
1759 VERIFY3P(ds->ds_owner, ==, owner);
1760 dsl_dataset_long_rele(ds, owner);
1763 held = dsl_dataset_long_held(ds);
1766 dsl_dataset_long_hold(ds, owner);
1769 return (SET_ERROR(EBUSY));
1774 typedef struct dsl_dataset_rollback_arg {
1775 const char *ddra_fsname;
1777 nvlist_t *ddra_result;
1778 } dsl_dataset_rollback_arg_t;
1781 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
1783 dsl_dataset_rollback_arg_t *ddra = arg;
1784 dsl_pool_t *dp = dmu_tx_pool(tx);
1786 int64_t unused_refres_delta;
1789 error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
1793 /* must not be a snapshot */
1794 if (dsl_dataset_is_snapshot(ds)) {
1795 dsl_dataset_rele(ds, FTAG);
1796 return (SET_ERROR(EINVAL));
1799 /* must have a most recent snapshot */
1800 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
1801 dsl_dataset_rele(ds, FTAG);
1802 return (SET_ERROR(EINVAL));
1805 error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
1807 dsl_dataset_rele(ds, FTAG);
1812 * Check if the snap we are rolling back to uses more than
1815 if (ds->ds_quota != 0 &&
1816 ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) {
1817 dsl_dataset_rele(ds, FTAG);
1818 return (SET_ERROR(EDQUOT));
1822 * When we do the clone swap, we will temporarily use more space
1823 * due to the refreservation (the head will no longer have any
1824 * unique space, so the entire amount of the refreservation will need
1825 * to be free). We will immediately destroy the clone, freeing
1826 * this space, but the freeing happens over many txg's.
1828 unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
1829 ds->ds_phys->ds_unique_bytes);
1831 if (unused_refres_delta > 0 &&
1832 unused_refres_delta >
1833 dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
1834 dsl_dataset_rele(ds, FTAG);
1835 return (SET_ERROR(ENOSPC));
1838 dsl_dataset_rele(ds, FTAG);
1843 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
1845 dsl_dataset_rollback_arg_t *ddra = arg;
1846 dsl_pool_t *dp = dmu_tx_pool(tx);
1847 dsl_dataset_t *ds, *clone;
1849 char namebuf[ZFS_MAXNAMELEN];
1851 VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
1853 dsl_dataset_name(ds->ds_prev, namebuf);
1854 fnvlist_add_string(ddra->ddra_result, "target", namebuf);
1856 cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
1857 ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
1859 VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
1861 dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
1862 dsl_dataset_zero_zil(ds, tx);
1864 dsl_destroy_head_sync_impl(clone, tx);
1866 dsl_dataset_rele(clone, FTAG);
1867 dsl_dataset_rele(ds, FTAG);
1871 * Rolls back the given filesystem or volume to the most recent snapshot.
1872 * The name of the most recent snapshot will be returned under key "target"
1873 * in the result nvlist.
1876 * - The existing dataset MUST be owned by the specified owner at entry
1877 * - Upon return, dataset will still be held by the same owner, whether we
1880 * This mode is required any time the existing filesystem is mounted. See
1881 * notes above zfs_suspend_fs() for further details.
1884 dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
1886 dsl_dataset_rollback_arg_t ddra;
1888 ddra.ddra_fsname = fsname;
1889 ddra.ddra_owner = owner;
1890 ddra.ddra_result = result;
1892 return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
1893 dsl_dataset_rollback_sync, &ddra, 1));
1896 struct promotenode {
1901 typedef struct dsl_dataset_promote_arg {
1902 const char *ddpa_clonename;
1903 dsl_dataset_t *ddpa_clone;
1904 list_t shared_snaps, origin_snaps, clone_snaps;
1905 dsl_dataset_t *origin_origin; /* origin of the origin */
1906 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
1908 } dsl_dataset_promote_arg_t;
1910 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
1911 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
1913 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
1916 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
1918 dsl_dataset_promote_arg_t *ddpa = arg;
1919 dsl_pool_t *dp = dmu_tx_pool(tx);
1921 struct promotenode *snap;
1922 dsl_dataset_t *origin_ds;
1926 err = promote_hold(ddpa, dp, FTAG);
1930 hds = ddpa->ddpa_clone;
1932 if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
1933 promote_rele(ddpa, FTAG);
1934 return (SET_ERROR(EXDEV));
1938 * Compute and check the amount of space to transfer. Since this is
1939 * so expensive, don't do the preliminary check.
1941 if (!dmu_tx_is_syncing(tx)) {
1942 promote_rele(ddpa, FTAG);
1946 snap = list_head(&ddpa->shared_snaps);
1947 origin_ds = snap->ds;
1949 /* compute origin's new unique space */
1950 snap = list_tail(&ddpa->clone_snaps);
1951 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
1952 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
1953 origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
1954 &ddpa->unique, &unused, &unused);
1957 * Walk the snapshots that we are moving
1959 * Compute space to transfer. Consider the incremental changes
1960 * to used by each snapshot:
1961 * (my used) = (prev's used) + (blocks born) - (blocks killed)
1962 * So each snapshot gave birth to:
1963 * (blocks born) = (my used) - (prev's used) + (blocks killed)
1964 * So a sequence would look like:
1965 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
1966 * Which simplifies to:
1967 * uN + kN + kN-1 + ... + k1 + k0
1968 * Note however, if we stop before we reach the ORIGIN we get:
1969 * uN + kN + kN-1 + ... + kM - uM-1
1971 ddpa->used = origin_ds->ds_phys->ds_referenced_bytes;
1972 ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes;
1973 ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
1974 for (snap = list_head(&ddpa->shared_snaps); snap;
1975 snap = list_next(&ddpa->shared_snaps, snap)) {
1976 uint64_t val, dlused, dlcomp, dluncomp;
1977 dsl_dataset_t *ds = snap->ds;
1980 * If there are long holds, we won't be able to evict
1983 if (dsl_dataset_long_held(ds)) {
1984 err = SET_ERROR(EBUSY);
1988 /* Check that the snapshot name does not conflict */
1989 VERIFY0(dsl_dataset_get_snapname(ds));
1990 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
1992 (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
1993 err = SET_ERROR(EEXIST);
1999 /* The very first snapshot does not have a deadlist */
2000 if (ds->ds_phys->ds_prev_snap_obj == 0)
2003 dsl_deadlist_space(&ds->ds_deadlist,
2004 &dlused, &dlcomp, &dluncomp);
2005 ddpa->used += dlused;
2006 ddpa->comp += dlcomp;
2007 ddpa->uncomp += dluncomp;
2011 * If we are a clone of a clone then we never reached ORIGIN,
2012 * so we need to subtract out the clone origin's used space.
2014 if (ddpa->origin_origin) {
2015 ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes;
2016 ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes;
2018 ddpa->origin_origin->ds_phys->ds_uncompressed_bytes;
2021 /* Check that there is enough space here */
2022 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2028 * Compute the amounts of space that will be used by snapshots
2029 * after the promotion (for both origin and clone). For each,
2030 * it is the amount of space that will be on all of their
2031 * deadlists (that was not born before their new origin).
2033 if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2037 * Note, typically this will not be a clone of a clone,
2038 * so dd_origin_txg will be < TXG_INITIAL, so
2039 * these snaplist_space() -> dsl_deadlist_space_range()
2040 * calls will be fast because they do not have to
2041 * iterate over all bps.
2043 snap = list_head(&ddpa->origin_snaps);
2044 err = snaplist_space(&ddpa->shared_snaps,
2045 snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
2049 err = snaplist_space(&ddpa->clone_snaps,
2050 snap->ds->ds_dir->dd_origin_txg, &space);
2053 ddpa->cloneusedsnap += space;
2055 if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2056 err = snaplist_space(&ddpa->origin_snaps,
2057 origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap);
2063 promote_rele(ddpa, FTAG);
2068 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
2070 dsl_dataset_promote_arg_t *ddpa = arg;
2071 dsl_pool_t *dp = dmu_tx_pool(tx);
2073 struct promotenode *snap;
2074 dsl_dataset_t *origin_ds;
2075 dsl_dataset_t *origin_head;
2077 dsl_dir_t *odd = NULL;
2078 uint64_t oldnext_obj;
2081 VERIFY0(promote_hold(ddpa, dp, FTAG));
2082 hds = ddpa->ddpa_clone;
2084 ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE);
2086 snap = list_head(&ddpa->shared_snaps);
2087 origin_ds = snap->ds;
2090 snap = list_head(&ddpa->origin_snaps);
2091 origin_head = snap->ds;
2094 * We need to explicitly open odd, since origin_ds's dd will be
2097 VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
2100 /* change origin's next snap */
2101 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2102 oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
2103 snap = list_tail(&ddpa->clone_snaps);
2104 ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2105 origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
2107 /* change the origin's next clone */
2108 if (origin_ds->ds_phys->ds_next_clones_obj) {
2109 dsl_dataset_remove_from_next_clones(origin_ds,
2110 snap->ds->ds_object, tx);
2111 VERIFY0(zap_add_int(dp->dp_meta_objset,
2112 origin_ds->ds_phys->ds_next_clones_obj,
2117 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2118 ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2119 dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
2120 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2121 dmu_buf_will_dirty(odd->dd_dbuf, tx);
2122 odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
2123 origin_head->ds_dir->dd_origin_txg =
2124 origin_ds->ds_phys->ds_creation_txg;
2126 /* change dd_clone entries */
2127 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2128 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2129 odd->dd_phys->dd_clones, hds->ds_object, tx));
2130 VERIFY0(zap_add_int(dp->dp_meta_objset,
2131 ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2132 hds->ds_object, tx));
2134 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2135 ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2136 origin_head->ds_object, tx));
2137 if (dd->dd_phys->dd_clones == 0) {
2138 dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2139 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2141 VERIFY0(zap_add_int(dp->dp_meta_objset,
2142 dd->dd_phys->dd_clones, origin_head->ds_object, tx));
2145 /* move snapshots to this dir */
2146 for (snap = list_head(&ddpa->shared_snaps); snap;
2147 snap = list_next(&ddpa->shared_snaps, snap)) {
2148 dsl_dataset_t *ds = snap->ds;
2151 * Property callbacks are registered to a particular
2152 * dsl_dir. Since ours is changing, evict the objset
2153 * so that they will be unregistered from the old dsl_dir.
2155 if (ds->ds_objset) {
2156 dmu_objset_evict(ds->ds_objset);
2157 ds->ds_objset = NULL;
2160 /* move snap name entry */
2161 VERIFY0(dsl_dataset_get_snapname(ds));
2162 VERIFY0(dsl_dataset_snap_remove(origin_head,
2163 ds->ds_snapname, tx));
2164 VERIFY0(zap_add(dp->dp_meta_objset,
2165 hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
2166 8, 1, &ds->ds_object, tx));
2168 /* change containing dsl_dir */
2169 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2170 ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2171 ds->ds_phys->ds_dir_obj = dd->dd_object;
2172 ASSERT3P(ds->ds_dir, ==, odd);
2173 dsl_dir_rele(ds->ds_dir, ds);
2174 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
2175 NULL, ds, &ds->ds_dir));
2177 /* move any clone references */
2178 if (ds->ds_phys->ds_next_clones_obj &&
2179 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2183 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2184 ds->ds_phys->ds_next_clones_obj);
2185 zap_cursor_retrieve(&zc, &za) == 0;
2186 zap_cursor_advance(&zc)) {
2187 dsl_dataset_t *cnds;
2190 if (za.za_first_integer == oldnext_obj) {
2192 * We've already moved the
2193 * origin's reference.
2198 VERIFY0(dsl_dataset_hold_obj(dp,
2199 za.za_first_integer, FTAG, &cnds));
2200 o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2202 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2203 odd->dd_phys->dd_clones, o, tx));
2204 VERIFY0(zap_add_int(dp->dp_meta_objset,
2205 dd->dd_phys->dd_clones, o, tx));
2206 dsl_dataset_rele(cnds, FTAG);
2208 zap_cursor_fini(&zc);
2211 ASSERT(!dsl_prop_hascb(ds));
2215 * Change space accounting.
2216 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2217 * both be valid, or both be 0 (resulting in delta == 0). This
2218 * is true for each of {clone,origin} independently.
2221 delta = ddpa->cloneusedsnap -
2222 dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2223 ASSERT3S(delta, >=, 0);
2224 ASSERT3U(ddpa->used, >=, delta);
2225 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2226 dsl_dir_diduse_space(dd, DD_USED_HEAD,
2227 ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
2229 delta = ddpa->originusedsnap -
2230 odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
2231 ASSERT3S(delta, <=, 0);
2232 ASSERT3U(ddpa->used, >=, -delta);
2233 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2234 dsl_dir_diduse_space(odd, DD_USED_HEAD,
2235 -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
2237 origin_ds->ds_phys->ds_unique_bytes = ddpa->unique;
2239 /* log history record */
2240 spa_history_log_internal_ds(hds, "promote", tx, "");
2242 dsl_dir_rele(odd, FTAG);
2243 promote_rele(ddpa, FTAG);
2247 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2248 * (exclusive) and last_obj (inclusive). The list will be in reverse
2249 * order (last_obj will be the list_head()). If first_obj == 0, do all
2250 * snapshots back to this dataset's origin.
2253 snaplist_make(dsl_pool_t *dp,
2254 uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2256 uint64_t obj = last_obj;
2258 list_create(l, sizeof (struct promotenode),
2259 offsetof(struct promotenode, link));
2261 while (obj != first_obj) {
2263 struct promotenode *snap;
2266 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
2267 ASSERT(err != ENOENT);
2272 first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
2274 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
2276 list_insert_tail(l, snap);
2277 obj = ds->ds_phys->ds_prev_snap_obj;
2284 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2286 struct promotenode *snap;
2289 for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2290 uint64_t used, comp, uncomp;
2291 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2292 mintxg, UINT64_MAX, &used, &comp, &uncomp);
2299 snaplist_destroy(list_t *l, void *tag)
2301 struct promotenode *snap;
2303 if (l == NULL || !list_link_active(&l->list_head))
2306 while ((snap = list_tail(l)) != NULL) {
2307 list_remove(l, snap);
2308 dsl_dataset_rele(snap->ds, tag);
2309 kmem_free(snap, sizeof (*snap));
2315 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2319 struct promotenode *snap;
2321 error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
2325 dd = ddpa->ddpa_clone->ds_dir;
2327 if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) ||
2328 !dsl_dir_is_clone(dd)) {
2329 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2330 return (SET_ERROR(EINVAL));
2333 error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj,
2334 &ddpa->shared_snaps, tag);
2338 error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
2339 &ddpa->clone_snaps, tag);
2343 snap = list_head(&ddpa->shared_snaps);
2344 ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
2345 error = snaplist_make(dp, dd->dd_phys->dd_origin_obj,
2346 snap->ds->ds_dir->dd_phys->dd_head_dataset_obj,
2347 &ddpa->origin_snaps, tag);
2351 if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
2352 error = dsl_dataset_hold_obj(dp,
2353 snap->ds->ds_dir->dd_phys->dd_origin_obj,
2354 tag, &ddpa->origin_origin);
2360 promote_rele(ddpa, tag);
2365 promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2367 snaplist_destroy(&ddpa->shared_snaps, tag);
2368 snaplist_destroy(&ddpa->clone_snaps, tag);
2369 snaplist_destroy(&ddpa->origin_snaps, tag);
2370 if (ddpa->origin_origin != NULL)
2371 dsl_dataset_rele(ddpa->origin_origin, tag);
2372 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2378 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2379 * in with the name. (It must be at least MAXNAMELEN bytes long.)
2382 dsl_dataset_promote(const char *name, char *conflsnap)
2384 dsl_dataset_promote_arg_t ddpa = { 0 };
2390 * We will modify space proportional to the number of
2391 * snapshots. Compute numsnaps.
2393 error = dmu_objset_hold(name, FTAG, &os);
2396 error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2397 dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps);
2398 dmu_objset_rele(os, FTAG);
2402 ddpa.ddpa_clonename = name;
2403 ddpa.err_ds = conflsnap;
2405 return (dsl_sync_task(name, dsl_dataset_promote_check,
2406 dsl_dataset_promote_sync, &ddpa, 2 + numsnaps));
2410 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
2411 dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2413 int64_t unused_refres_delta;
2415 /* they should both be heads */
2416 if (dsl_dataset_is_snapshot(clone) ||
2417 dsl_dataset_is_snapshot(origin_head))
2418 return (SET_ERROR(EINVAL));
2420 /* if we are not forcing, the branch point should be just before them */
2421 if (!force && clone->ds_prev != origin_head->ds_prev)
2422 return (SET_ERROR(EINVAL));
2424 /* clone should be the clone (unless they are unrelated) */
2425 if (clone->ds_prev != NULL &&
2426 clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
2427 origin_head->ds_dir != clone->ds_prev->ds_dir)
2428 return (SET_ERROR(EINVAL));
2430 /* the clone should be a child of the origin */
2431 if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2432 return (SET_ERROR(EINVAL));
2434 /* origin_head shouldn't be modified unless 'force' */
2436 dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2437 return (SET_ERROR(ETXTBSY));
2439 /* origin_head should have no long holds (e.g. is not mounted) */
2440 if (dsl_dataset_handoff_check(origin_head, owner, tx))
2441 return (SET_ERROR(EBUSY));
2443 /* check amount of any unconsumed refreservation */
2444 unused_refres_delta =
2445 (int64_t)MIN(origin_head->ds_reserved,
2446 origin_head->ds_phys->ds_unique_bytes) -
2447 (int64_t)MIN(origin_head->ds_reserved,
2448 clone->ds_phys->ds_unique_bytes);
2450 if (unused_refres_delta > 0 &&
2451 unused_refres_delta >
2452 dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2453 return (SET_ERROR(ENOSPC));
2455 /* clone can't be over the head's refquota */
2456 if (origin_head->ds_quota != 0 &&
2457 clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota)
2458 return (SET_ERROR(EDQUOT));
2464 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2465 dsl_dataset_t *origin_head, dmu_tx_t *tx)
2467 dsl_pool_t *dp = dmu_tx_pool(tx);
2468 int64_t unused_refres_delta;
2470 ASSERT(clone->ds_reserved == 0);
2471 ASSERT(origin_head->ds_quota == 0 ||
2472 clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota);
2473 ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
2475 dmu_buf_will_dirty(clone->ds_dbuf, tx);
2476 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2478 if (clone->ds_objset != NULL) {
2479 dmu_objset_evict(clone->ds_objset);
2480 clone->ds_objset = NULL;
2483 if (origin_head->ds_objset != NULL) {
2484 dmu_objset_evict(origin_head->ds_objset);
2485 origin_head->ds_objset = NULL;
2488 unused_refres_delta =
2489 (int64_t)MIN(origin_head->ds_reserved,
2490 origin_head->ds_phys->ds_unique_bytes) -
2491 (int64_t)MIN(origin_head->ds_reserved,
2492 clone->ds_phys->ds_unique_bytes);
2495 * Reset origin's unique bytes, if it exists.
2497 if (clone->ds_prev) {
2498 dsl_dataset_t *origin = clone->ds_prev;
2499 uint64_t comp, uncomp;
2501 dmu_buf_will_dirty(origin->ds_dbuf, tx);
2502 dsl_deadlist_space_range(&clone->ds_deadlist,
2503 origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2504 &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
2510 tmp = origin_head->ds_phys->ds_bp;
2511 origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp;
2512 clone->ds_phys->ds_bp = tmp;
2515 /* set dd_*_bytes */
2517 int64_t dused, dcomp, duncomp;
2518 uint64_t cdl_used, cdl_comp, cdl_uncomp;
2519 uint64_t odl_used, odl_comp, odl_uncomp;
2521 ASSERT3U(clone->ds_dir->dd_phys->
2522 dd_used_breakdown[DD_USED_SNAP], ==, 0);
2524 dsl_deadlist_space(&clone->ds_deadlist,
2525 &cdl_used, &cdl_comp, &cdl_uncomp);
2526 dsl_deadlist_space(&origin_head->ds_deadlist,
2527 &odl_used, &odl_comp, &odl_uncomp);
2529 dused = clone->ds_phys->ds_referenced_bytes + cdl_used -
2530 (origin_head->ds_phys->ds_referenced_bytes + odl_used);
2531 dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp -
2532 (origin_head->ds_phys->ds_compressed_bytes + odl_comp);
2533 duncomp = clone->ds_phys->ds_uncompressed_bytes +
2535 (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp);
2537 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
2538 dused, dcomp, duncomp, tx);
2539 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
2540 -dused, -dcomp, -duncomp, tx);
2543 * The difference in the space used by snapshots is the
2544 * difference in snapshot space due to the head's
2545 * deadlist (since that's the only thing that's
2546 * changing that affects the snapused).
2548 dsl_deadlist_space_range(&clone->ds_deadlist,
2549 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2550 &cdl_used, &cdl_comp, &cdl_uncomp);
2551 dsl_deadlist_space_range(&origin_head->ds_deadlist,
2552 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
2553 &odl_used, &odl_comp, &odl_uncomp);
2554 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
2555 DD_USED_HEAD, DD_USED_SNAP, tx);
2558 /* swap ds_*_bytes */
2559 SWITCH64(origin_head->ds_phys->ds_referenced_bytes,
2560 clone->ds_phys->ds_referenced_bytes);
2561 SWITCH64(origin_head->ds_phys->ds_compressed_bytes,
2562 clone->ds_phys->ds_compressed_bytes);
2563 SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes,
2564 clone->ds_phys->ds_uncompressed_bytes);
2565 SWITCH64(origin_head->ds_phys->ds_unique_bytes,
2566 clone->ds_phys->ds_unique_bytes);
2568 /* apply any parent delta for change in unconsumed refreservation */
2569 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
2570 unused_refres_delta, 0, 0, tx);
2575 dsl_deadlist_close(&clone->ds_deadlist);
2576 dsl_deadlist_close(&origin_head->ds_deadlist);
2577 SWITCH64(origin_head->ds_phys->ds_deadlist_obj,
2578 clone->ds_phys->ds_deadlist_obj);
2579 dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
2580 clone->ds_phys->ds_deadlist_obj);
2581 dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
2582 origin_head->ds_phys->ds_deadlist_obj);
2584 dsl_scan_ds_clone_swapped(origin_head, clone, tx);
2586 spa_history_log_internal_ds(clone, "clone swap", tx,
2587 "parent=%s", origin_head->ds_dir->dd_myname);
2591 * Given a pool name and a dataset object number in that pool,
2592 * return the name of that dataset.
2595 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2601 error = dsl_pool_hold(pname, FTAG, &dp);
2605 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
2607 dsl_dataset_name(ds, buf);
2608 dsl_dataset_rele(ds, FTAG);
2610 dsl_pool_rele(dp, FTAG);
2616 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
2617 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
2621 ASSERT3S(asize, >, 0);
2624 * *ref_rsrv is the portion of asize that will come from any
2625 * unconsumed refreservation space.
2629 mutex_enter(&ds->ds_lock);
2631 * Make a space adjustment for reserved bytes.
2633 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
2635 ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2636 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
2638 asize - MIN(asize, parent_delta(ds, asize + inflight));
2641 if (!check_quota || ds->ds_quota == 0) {
2642 mutex_exit(&ds->ds_lock);
2646 * If they are requesting more space, and our current estimate
2647 * is over quota, they get to try again unless the actual
2648 * on-disk is over quota and there are no pending changes (which
2649 * may free up space for us).
2651 if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
2653 ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
2654 error = SET_ERROR(ERESTART);
2656 error = SET_ERROR(EDQUOT);
2658 mutex_exit(&ds->ds_lock);
2663 typedef struct dsl_dataset_set_qr_arg {
2664 const char *ddsqra_name;
2665 zprop_source_t ddsqra_source;
2666 uint64_t ddsqra_value;
2667 } dsl_dataset_set_qr_arg_t;
2672 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
2674 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2675 dsl_pool_t *dp = dmu_tx_pool(tx);
2680 if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2681 return (SET_ERROR(ENOTSUP));
2683 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2687 if (dsl_dataset_is_snapshot(ds)) {
2688 dsl_dataset_rele(ds, FTAG);
2689 return (SET_ERROR(EINVAL));
2692 error = dsl_prop_predict(ds->ds_dir,
2693 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2694 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2696 dsl_dataset_rele(ds, FTAG);
2701 dsl_dataset_rele(ds, FTAG);
2705 if (newval < ds->ds_phys->ds_referenced_bytes ||
2706 newval < ds->ds_reserved) {
2707 dsl_dataset_rele(ds, FTAG);
2708 return (SET_ERROR(ENOSPC));
2711 dsl_dataset_rele(ds, FTAG);
2716 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
2718 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2719 dsl_pool_t *dp = dmu_tx_pool(tx);
2723 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2725 dsl_prop_set_sync_impl(ds,
2726 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
2727 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
2728 &ddsqra->ddsqra_value, tx);
2730 VERIFY0(dsl_prop_get_int_ds(ds,
2731 zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
2733 if (ds->ds_quota != newval) {
2734 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2735 ds->ds_quota = newval;
2737 dsl_dataset_rele(ds, FTAG);
2741 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
2744 dsl_dataset_set_qr_arg_t ddsqra;
2746 ddsqra.ddsqra_name = dsname;
2747 ddsqra.ddsqra_source = source;
2748 ddsqra.ddsqra_value = refquota;
2750 return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
2751 dsl_dataset_set_refquota_sync, &ddsqra, 0));
2755 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
2757 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2758 dsl_pool_t *dp = dmu_tx_pool(tx);
2761 uint64_t newval, unique;
2763 if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2764 return (SET_ERROR(ENOTSUP));
2766 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
2770 if (dsl_dataset_is_snapshot(ds)) {
2771 dsl_dataset_rele(ds, FTAG);
2772 return (SET_ERROR(EINVAL));
2775 error = dsl_prop_predict(ds->ds_dir,
2776 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2777 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
2779 dsl_dataset_rele(ds, FTAG);
2784 * If we are doing the preliminary check in open context, the
2785 * space estimates may be inaccurate.
2787 if (!dmu_tx_is_syncing(tx)) {
2788 dsl_dataset_rele(ds, FTAG);
2792 mutex_enter(&ds->ds_lock);
2793 if (!DS_UNIQUE_IS_ACCURATE(ds))
2794 dsl_dataset_recalc_head_uniq(ds);
2795 unique = ds->ds_phys->ds_unique_bytes;
2796 mutex_exit(&ds->ds_lock);
2798 if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
2799 uint64_t delta = MAX(unique, newval) -
2800 MAX(unique, ds->ds_reserved);
2803 dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
2804 (ds->ds_quota > 0 && newval > ds->ds_quota)) {
2805 dsl_dataset_rele(ds, FTAG);
2806 return (SET_ERROR(ENOSPC));
2810 dsl_dataset_rele(ds, FTAG);
2815 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
2816 zprop_source_t source, uint64_t value, dmu_tx_t *tx)
2822 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2823 source, sizeof (value), 1, &value, tx);
2825 VERIFY0(dsl_prop_get_int_ds(ds,
2826 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
2828 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2829 mutex_enter(&ds->ds_dir->dd_lock);
2830 mutex_enter(&ds->ds_lock);
2831 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
2832 unique = ds->ds_phys->ds_unique_bytes;
2833 delta = MAX(0, (int64_t)(newval - unique)) -
2834 MAX(0, (int64_t)(ds->ds_reserved - unique));
2835 ds->ds_reserved = newval;
2836 mutex_exit(&ds->ds_lock);
2838 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
2839 mutex_exit(&ds->ds_dir->dd_lock);
2843 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
2845 dsl_dataset_set_qr_arg_t *ddsqra = arg;
2846 dsl_pool_t *dp = dmu_tx_pool(tx);
2849 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2850 dsl_dataset_set_refreservation_sync_impl(ds,
2851 ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
2852 dsl_dataset_rele(ds, FTAG);
2856 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
2857 uint64_t refreservation)
2859 dsl_dataset_set_qr_arg_t ddsqra;
2861 ddsqra.ddsqra_name = dsname;
2862 ddsqra.ddsqra_source = source;
2863 ddsqra.ddsqra_value = refreservation;
2865 return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
2866 dsl_dataset_set_refreservation_sync, &ddsqra, 0));
2870 * Return (in *usedp) the amount of space written in new that is not
2871 * present in oldsnap. New may be a snapshot or the head. Old must be
2872 * a snapshot before new, in new's filesystem (or its origin). If not then
2873 * fail and return EINVAL.
2875 * The written space is calculated by considering two components: First, we
2876 * ignore any freed space, and calculate the written as new's used space
2877 * minus old's used space. Next, we add in the amount of space that was freed
2878 * between the two snapshots, thus reducing new's used space relative to old's.
2879 * Specifically, this is the space that was born before old->ds_creation_txg,
2880 * and freed before new (ie. on new's deadlist or a previous deadlist).
2882 * space freed [---------------------]
2883 * snapshots ---O-------O--------O-------O------
2887 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
2888 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2892 dsl_pool_t *dp = new->ds_dir->dd_pool;
2894 ASSERT(dsl_pool_config_held(dp));
2897 *usedp += new->ds_phys->ds_referenced_bytes;
2898 *usedp -= oldsnap->ds_phys->ds_referenced_bytes;
2901 *compp += new->ds_phys->ds_compressed_bytes;
2902 *compp -= oldsnap->ds_phys->ds_compressed_bytes;
2905 *uncompp += new->ds_phys->ds_uncompressed_bytes;
2906 *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
2908 snapobj = new->ds_object;
2909 while (snapobj != oldsnap->ds_object) {
2910 dsl_dataset_t *snap;
2911 uint64_t used, comp, uncomp;
2913 if (snapobj == new->ds_object) {
2916 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
2921 if (snap->ds_phys->ds_prev_snap_txg ==
2922 oldsnap->ds_phys->ds_creation_txg) {
2924 * The blocks in the deadlist can not be born after
2925 * ds_prev_snap_txg, so get the whole deadlist space,
2926 * which is more efficient (especially for old-format
2927 * deadlists). Unfortunately the deadlist code
2928 * doesn't have enough information to make this
2929 * optimization itself.
2931 dsl_deadlist_space(&snap->ds_deadlist,
2932 &used, &comp, &uncomp);
2934 dsl_deadlist_space_range(&snap->ds_deadlist,
2935 0, oldsnap->ds_phys->ds_creation_txg,
2936 &used, &comp, &uncomp);
2943 * If we get to the beginning of the chain of snapshots
2944 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
2945 * was not a snapshot of/before new.
2947 snapobj = snap->ds_phys->ds_prev_snap_obj;
2949 dsl_dataset_rele(snap, FTAG);
2951 err = SET_ERROR(EINVAL);
2960 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
2961 * lastsnap, and all snapshots in between are deleted.
2963 * blocks that would be freed [---------------------------]
2964 * snapshots ---O-------O--------O-------O--------O
2965 * firstsnap lastsnap
2967 * This is the set of blocks that were born after the snap before firstsnap,
2968 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
2969 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
2970 * We calculate this by iterating over the relevant deadlists (from the snap
2971 * after lastsnap, backward to the snap after firstsnap), summing up the
2972 * space on the deadlist that was born after the snap before firstsnap.
2975 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
2976 dsl_dataset_t *lastsnap,
2977 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
2981 dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
2983 ASSERT(dsl_dataset_is_snapshot(firstsnap));
2984 ASSERT(dsl_dataset_is_snapshot(lastsnap));
2987 * Check that the snapshots are in the same dsl_dir, and firstsnap
2988 * is before lastsnap.
2990 if (firstsnap->ds_dir != lastsnap->ds_dir ||
2991 firstsnap->ds_phys->ds_creation_txg >
2992 lastsnap->ds_phys->ds_creation_txg)
2993 return (SET_ERROR(EINVAL));
2995 *usedp = *compp = *uncompp = 0;
2997 snapobj = lastsnap->ds_phys->ds_next_snap_obj;
2998 while (snapobj != firstsnap->ds_object) {
3000 uint64_t used, comp, uncomp;
3002 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
3006 dsl_deadlist_space_range(&ds->ds_deadlist,
3007 firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
3008 &used, &comp, &uncomp);
3013 snapobj = ds->ds_phys->ds_prev_snap_obj;
3014 ASSERT3U(snapobj, !=, 0);
3015 dsl_dataset_rele(ds, FTAG);
3021 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
3022 * For example, they could both be snapshots of the same filesystem, and
3023 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
3024 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
3025 * filesystem. Or 'earlier' could be the origin's origin.
3028 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier)
3030 dsl_pool_t *dp = later->ds_dir->dd_pool;
3034 ASSERT(dsl_pool_config_held(dp));
3036 if (earlier->ds_phys->ds_creation_txg >=
3037 later->ds_phys->ds_creation_txg)
3040 if (later->ds_dir == earlier->ds_dir)
3042 if (!dsl_dir_is_clone(later->ds_dir))
3045 if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object)
3047 dsl_dataset_t *origin;
3048 error = dsl_dataset_hold_obj(dp,
3049 later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin);
3052 ret = dsl_dataset_is_before(origin, earlier);
3053 dsl_dataset_rele(origin, FTAG);
3059 dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
3061 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3062 dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);