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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright (c) 2011 Martin Matuska <mm@FreeBSD.org>
25 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
26 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 RackTop Systems.
28 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
30 * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved.
31 * Copyright 2017 Nexenta Systems, Inc.
34 #include <sys/dmu_objset.h>
35 #include <sys/dsl_dataset.h>
36 #include <sys/dsl_dir.h>
37 #include <sys/dsl_prop.h>
38 #include <sys/dsl_synctask.h>
39 #include <sys/dmu_traverse.h>
40 #include <sys/dmu_impl.h>
41 #include <sys/dmu_send.h>
42 #include <sys/dmu_tx.h>
46 #include <sys/zfeature.h>
47 #include <sys/unique.h>
48 #include <sys/zfs_context.h>
49 #include <sys/zfs_ioctl.h>
51 #include <sys/zfs_znode.h>
52 #include <sys/zfs_onexit.h>
54 #include <sys/dsl_scan.h>
55 #include <sys/dsl_deadlist.h>
56 #include <sys/dsl_destroy.h>
57 #include <sys/dsl_userhold.h>
58 #include <sys/dsl_bookmark.h>
59 #include <sys/dmu_send.h>
60 #include <sys/zio_checksum.h>
61 #include <sys/zio_compress.h>
62 #include <zfs_fletcher.h>
64 SYSCTL_DECL(_vfs_zfs);
67 * The SPA supports block sizes up to 16MB. However, very large blocks
68 * can have an impact on i/o latency (e.g. tying up a spinning disk for
69 * ~300ms), and also potentially on the memory allocator. Therefore,
70 * we do not allow the recordsize to be set larger than zfs_max_recordsize
71 * (default 1MB). Larger blocks can be created by changing this tunable,
72 * and pools with larger blocks can always be imported and used, regardless
75 int zfs_max_recordsize = 1 * 1024 * 1024;
76 SYSCTL_INT(_vfs_zfs, OID_AUTO, max_recordsize, CTLFLAG_RWTUN,
77 &zfs_max_recordsize, 0,
78 "Maximum block size. Expect dragons when tuning this.");
80 #define SWITCH64(x, y) \
82 uint64_t __tmp = (x); \
87 #define DS_REF_MAX (1ULL << 62)
89 extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
91 extern int spa_asize_inflation;
93 static zil_header_t zero_zil;
96 * Figure out how much of this delta should be propogated to the dsl_dir
97 * layer. If there's a refreservation, that space has already been
98 * partially accounted for in our ancestors.
101 parent_delta(dsl_dataset_t *ds, int64_t delta)
103 dsl_dataset_phys_t *ds_phys;
104 uint64_t old_bytes, new_bytes;
106 if (ds->ds_reserved == 0)
109 ds_phys = dsl_dataset_phys(ds);
110 old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
111 new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
113 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
114 return (new_bytes - old_bytes);
118 dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
120 int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
121 int compressed = BP_GET_PSIZE(bp);
122 int uncompressed = BP_GET_UCSIZE(bp);
125 dprintf_bp(bp, "ds=%p", ds);
127 ASSERT(dmu_tx_is_syncing(tx));
128 /* It could have been compressed away to nothing */
131 ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
132 ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
134 dsl_pool_mos_diduse_space(tx->tx_pool,
135 used, compressed, uncompressed);
139 ASSERT3U(bp->blk_birth, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
140 dmu_buf_will_dirty(ds->ds_dbuf, tx);
141 mutex_enter(&ds->ds_lock);
142 delta = parent_delta(ds, used);
143 dsl_dataset_phys(ds)->ds_referenced_bytes += used;
144 dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
145 dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
146 dsl_dataset_phys(ds)->ds_unique_bytes += used;
148 if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
149 ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] =
153 spa_feature_t f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp));
154 if (f != SPA_FEATURE_NONE)
155 ds->ds_feature_activation_needed[f] = B_TRUE;
157 mutex_exit(&ds->ds_lock);
158 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
159 compressed, uncompressed, tx);
160 dsl_dir_transfer_space(ds->ds_dir, used - delta,
161 DD_USED_REFRSRV, DD_USED_HEAD, NULL);
165 dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
168 int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
169 int compressed = BP_GET_PSIZE(bp);
170 int uncompressed = BP_GET_UCSIZE(bp);
175 ASSERT(dmu_tx_is_syncing(tx));
176 ASSERT(bp->blk_birth <= tx->tx_txg);
179 dsl_free(tx->tx_pool, tx->tx_txg, bp);
180 dsl_pool_mos_diduse_space(tx->tx_pool,
181 -used, -compressed, -uncompressed);
184 ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
186 ASSERT(!ds->ds_is_snapshot);
187 dmu_buf_will_dirty(ds->ds_dbuf, tx);
189 if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
192 dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
193 dsl_free(tx->tx_pool, tx->tx_txg, bp);
195 mutex_enter(&ds->ds_lock);
196 ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
197 !DS_UNIQUE_IS_ACCURATE(ds));
198 delta = parent_delta(ds, -used);
199 dsl_dataset_phys(ds)->ds_unique_bytes -= used;
200 mutex_exit(&ds->ds_lock);
201 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
202 delta, -compressed, -uncompressed, tx);
203 dsl_dir_transfer_space(ds->ds_dir, -used - delta,
204 DD_USED_REFRSRV, DD_USED_HEAD, NULL);
206 dprintf_bp(bp, "putting on dead list: %s", "");
209 * We are here as part of zio's write done callback,
210 * which means we're a zio interrupt thread. We can't
211 * call dsl_deadlist_insert() now because it may block
212 * waiting for I/O. Instead, put bp on the deferred
213 * queue and let dsl_pool_sync() finish the job.
215 bplist_append(&ds->ds_pending_deadlist, bp);
217 dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
219 ASSERT3U(ds->ds_prev->ds_object, ==,
220 dsl_dataset_phys(ds)->ds_prev_snap_obj);
221 ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
222 /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
223 if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
224 ds->ds_object && bp->blk_birth >
225 dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
226 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
227 mutex_enter(&ds->ds_prev->ds_lock);
228 dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
229 mutex_exit(&ds->ds_prev->ds_lock);
231 if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
232 dsl_dir_transfer_space(ds->ds_dir, used,
233 DD_USED_HEAD, DD_USED_SNAP, tx);
236 mutex_enter(&ds->ds_lock);
237 ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
238 dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
239 ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
240 dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
241 ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
242 dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
243 mutex_exit(&ds->ds_lock);
249 * We have to release the fsid syncronously or we risk that a subsequent
250 * mount of the same dataset will fail to unique_insert the fsid. This
251 * failure would manifest itself as the fsid of this dataset changing
252 * between mounts which makes NFS clients quite unhappy.
255 dsl_dataset_evict_sync(void *dbu)
257 dsl_dataset_t *ds = dbu;
259 ASSERT(ds->ds_owner == NULL);
261 unique_remove(ds->ds_fsid_guid);
265 dsl_dataset_evict_async(void *dbu)
267 dsl_dataset_t *ds = dbu;
269 ASSERT(ds->ds_owner == NULL);
273 if (ds->ds_objset != NULL)
274 dmu_objset_evict(ds->ds_objset);
277 dsl_dataset_rele(ds->ds_prev, ds);
281 bplist_destroy(&ds->ds_pending_deadlist);
282 if (ds->ds_deadlist.dl_os != NULL)
283 dsl_deadlist_close(&ds->ds_deadlist);
285 dsl_dir_async_rele(ds->ds_dir, ds);
287 ASSERT(!list_link_active(&ds->ds_synced_link));
289 list_destroy(&ds->ds_prop_cbs);
290 if (mutex_owned(&ds->ds_lock))
291 mutex_exit(&ds->ds_lock);
292 mutex_destroy(&ds->ds_lock);
293 if (mutex_owned(&ds->ds_opening_lock))
294 mutex_exit(&ds->ds_opening_lock);
295 mutex_destroy(&ds->ds_opening_lock);
296 mutex_destroy(&ds->ds_sendstream_lock);
297 refcount_destroy(&ds->ds_longholds);
298 rrw_destroy(&ds->ds_bp_rwlock);
300 kmem_free(ds, sizeof (dsl_dataset_t));
304 dsl_dataset_get_snapname(dsl_dataset_t *ds)
306 dsl_dataset_phys_t *headphys;
309 dsl_pool_t *dp = ds->ds_dir->dd_pool;
310 objset_t *mos = dp->dp_meta_objset;
312 if (ds->ds_snapname[0])
314 if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
317 err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
321 headphys = headdbuf->db_data;
322 err = zap_value_search(dp->dp_meta_objset,
323 headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
324 dmu_buf_rele(headdbuf, FTAG);
329 dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
331 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
332 uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
336 if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
339 err = zap_lookup_norm(mos, snapobj, name, 8, 1,
340 value, mt, NULL, 0, NULL);
341 if (err == ENOTSUP && (mt & MT_NORMALIZE))
342 err = zap_lookup(mos, snapobj, name, 8, 1, value);
347 dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
350 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
351 uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
355 dsl_dir_snap_cmtime_update(ds->ds_dir);
357 if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
360 err = zap_remove_norm(mos, snapobj, name, mt, tx);
361 if (err == ENOTSUP && (mt & MT_NORMALIZE))
362 err = zap_remove(mos, snapobj, name, tx);
364 if (err == 0 && adj_cnt)
365 dsl_fs_ss_count_adjust(ds->ds_dir, -1,
366 DD_FIELD_SNAPSHOT_COUNT, tx);
372 dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag)
374 dmu_buf_t *dbuf = ds->ds_dbuf;
375 boolean_t result = B_FALSE;
377 if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
378 ds->ds_object, DMU_BONUS_BLKID, tag)) {
380 if (ds == dmu_buf_get_user(dbuf))
383 dmu_buf_rele(dbuf, tag);
390 dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
393 objset_t *mos = dp->dp_meta_objset;
397 dmu_object_info_t doi;
399 ASSERT(dsl_pool_config_held(dp));
401 err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
405 /* Make sure dsobj has the correct object type. */
406 dmu_object_info_from_db(dbuf, &doi);
407 if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
408 dmu_buf_rele(dbuf, tag);
409 return (SET_ERROR(EINVAL));
412 ds = dmu_buf_get_user(dbuf);
414 dsl_dataset_t *winner = NULL;
416 ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
418 ds->ds_object = dsobj;
419 ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
421 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
422 mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
423 mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
424 rrw_init(&ds->ds_bp_rwlock, B_FALSE);
425 refcount_create(&ds->ds_longholds);
427 bplist_create(&ds->ds_pending_deadlist);
428 dsl_deadlist_open(&ds->ds_deadlist,
429 mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
431 list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
432 offsetof(dmu_sendarg_t, dsa_link));
434 list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t),
435 offsetof(dsl_prop_cb_record_t, cbr_ds_node));
437 if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
438 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
439 if (!(spa_feature_table[f].fi_flags &
440 ZFEATURE_FLAG_PER_DATASET))
442 err = zap_contains(mos, dsobj,
443 spa_feature_table[f].fi_guid);
445 ds->ds_feature_inuse[f] = B_TRUE;
447 ASSERT3U(err, ==, ENOENT);
453 err = dsl_dir_hold_obj(dp,
454 dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir);
456 mutex_destroy(&ds->ds_lock);
457 mutex_destroy(&ds->ds_opening_lock);
458 mutex_destroy(&ds->ds_sendstream_lock);
459 refcount_destroy(&ds->ds_longholds);
460 bplist_destroy(&ds->ds_pending_deadlist);
461 dsl_deadlist_close(&ds->ds_deadlist);
462 kmem_free(ds, sizeof (dsl_dataset_t));
463 dmu_buf_rele(dbuf, tag);
467 if (!ds->ds_is_snapshot) {
468 ds->ds_snapname[0] = '\0';
469 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
470 err = dsl_dataset_hold_obj(dp,
471 dsl_dataset_phys(ds)->ds_prev_snap_obj,
474 if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
475 int zaperr = zap_lookup(mos, ds->ds_object,
476 DS_FIELD_BOOKMARK_NAMES,
477 sizeof (ds->ds_bookmarks), 1,
479 if (zaperr != ENOENT)
483 if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
484 err = dsl_dataset_get_snapname(ds);
486 dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
488 ds->ds_dir->dd_pool->dp_meta_objset,
489 dsl_dataset_phys(ds)->ds_userrefs_obj,
494 if (err == 0 && !ds->ds_is_snapshot) {
495 err = dsl_prop_get_int_ds(ds,
496 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
499 err = dsl_prop_get_int_ds(ds,
500 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
504 ds->ds_reserved = ds->ds_quota = 0;
507 dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync,
508 dsl_dataset_evict_async, &ds->ds_dbuf);
510 winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
512 if (err != 0 || winner != NULL) {
513 bplist_destroy(&ds->ds_pending_deadlist);
514 dsl_deadlist_close(&ds->ds_deadlist);
516 dsl_dataset_rele(ds->ds_prev, ds);
517 dsl_dir_rele(ds->ds_dir, ds);
518 mutex_destroy(&ds->ds_lock);
519 mutex_destroy(&ds->ds_opening_lock);
520 mutex_destroy(&ds->ds_sendstream_lock);
521 refcount_destroy(&ds->ds_longholds);
522 kmem_free(ds, sizeof (dsl_dataset_t));
524 dmu_buf_rele(dbuf, tag);
530 unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
531 if (ds->ds_fsid_guid !=
532 dsl_dataset_phys(ds)->ds_fsid_guid) {
533 zfs_dbgmsg("ds_fsid_guid changed from "
534 "%llx to %llx for pool %s dataset id %llu",
536 dsl_dataset_phys(ds)->ds_fsid_guid,
537 (long long)ds->ds_fsid_guid,
538 spa_name(dp->dp_spa),
543 ASSERT3P(ds->ds_dbuf, ==, dbuf);
544 ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
545 ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
546 spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
547 dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
553 dsl_dataset_hold(dsl_pool_t *dp, const char *name,
554 void *tag, dsl_dataset_t **dsp)
557 const char *snapname;
562 err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
566 ASSERT(dsl_pool_config_held(dp));
567 obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
569 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
571 err = SET_ERROR(ENOENT);
573 /* we may be looking for a snapshot */
574 if (err == 0 && snapname != NULL) {
575 dsl_dataset_t *snap_ds;
577 if (*snapname++ != '@') {
578 dsl_dataset_rele(ds, tag);
579 dsl_dir_rele(dd, FTAG);
580 return (SET_ERROR(ENOENT));
583 dprintf("looking for snapshot '%s'\n", snapname);
584 err = dsl_dataset_snap_lookup(ds, snapname, &obj);
586 err = dsl_dataset_hold_obj(dp, obj, tag, &snap_ds);
587 dsl_dataset_rele(ds, tag);
590 mutex_enter(&snap_ds->ds_lock);
591 if (snap_ds->ds_snapname[0] == 0)
592 (void) strlcpy(snap_ds->ds_snapname, snapname,
593 sizeof (snap_ds->ds_snapname));
594 mutex_exit(&snap_ds->ds_lock);
600 dsl_dir_rele(dd, FTAG);
605 dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
606 void *tag, dsl_dataset_t **dsp)
608 int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
611 if (!dsl_dataset_tryown(*dsp, tag)) {
612 dsl_dataset_rele(*dsp, tag);
614 return (SET_ERROR(EBUSY));
620 dsl_dataset_own(dsl_pool_t *dp, const char *name,
621 void *tag, dsl_dataset_t **dsp)
623 int err = dsl_dataset_hold(dp, name, tag, dsp);
626 if (!dsl_dataset_tryown(*dsp, tag)) {
627 dsl_dataset_rele(*dsp, tag);
628 return (SET_ERROR(EBUSY));
634 * See the comment above dsl_pool_hold() for details. In summary, a long
635 * hold is used to prevent destruction of a dataset while the pool hold
636 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
638 * The dataset and pool must be held when this function is called. After it
639 * is called, the pool hold may be released while the dataset is still held
643 dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
645 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
646 (void) refcount_add(&ds->ds_longholds, tag);
650 dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
652 (void) refcount_remove(&ds->ds_longholds, tag);
655 /* Return B_TRUE if there are any long holds on this dataset. */
657 dsl_dataset_long_held(dsl_dataset_t *ds)
659 return (!refcount_is_zero(&ds->ds_longholds));
663 dsl_dataset_name(dsl_dataset_t *ds, char *name)
666 (void) strcpy(name, "mos");
668 dsl_dir_name(ds->ds_dir, name);
669 VERIFY0(dsl_dataset_get_snapname(ds));
670 if (ds->ds_snapname[0]) {
671 VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN),
672 <, ZFS_MAX_DATASET_NAME_LEN);
674 * We use a "recursive" mutex so that we
675 * can call dprintf_ds() with ds_lock held.
677 if (!MUTEX_HELD(&ds->ds_lock)) {
678 mutex_enter(&ds->ds_lock);
679 VERIFY3U(strlcat(name, ds->ds_snapname,
680 ZFS_MAX_DATASET_NAME_LEN), <,
681 ZFS_MAX_DATASET_NAME_LEN);
682 mutex_exit(&ds->ds_lock);
684 VERIFY3U(strlcat(name, ds->ds_snapname,
685 ZFS_MAX_DATASET_NAME_LEN), <,
686 ZFS_MAX_DATASET_NAME_LEN);
693 dsl_dataset_namelen(dsl_dataset_t *ds)
695 VERIFY0(dsl_dataset_get_snapname(ds));
696 mutex_enter(&ds->ds_lock);
697 int len = dsl_dir_namelen(ds->ds_dir) + 1 + strlen(ds->ds_snapname);
698 mutex_exit(&ds->ds_lock);
703 dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
705 dmu_buf_rele(ds->ds_dbuf, tag);
709 dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
711 ASSERT3P(ds->ds_owner, ==, tag);
712 ASSERT(ds->ds_dbuf != NULL);
714 mutex_enter(&ds->ds_lock);
716 mutex_exit(&ds->ds_lock);
717 dsl_dataset_long_rele(ds, tag);
718 dsl_dataset_rele(ds, tag);
722 dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
724 boolean_t gotit = FALSE;
726 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
727 mutex_enter(&ds->ds_lock);
728 if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
730 dsl_dataset_long_hold(ds, tag);
733 mutex_exit(&ds->ds_lock);
738 dsl_dataset_has_owner(dsl_dataset_t *ds)
741 mutex_enter(&ds->ds_lock);
742 rv = (ds->ds_owner != NULL);
743 mutex_exit(&ds->ds_lock);
748 dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
750 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
751 objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
754 VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
756 spa_feature_incr(spa, f, tx);
757 dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
759 VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
760 sizeof (zero), 1, &zero, tx));
764 dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
766 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
767 objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
769 VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
771 VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
772 spa_feature_decr(spa, f, tx);
776 dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
777 uint64_t flags, dmu_tx_t *tx)
779 dsl_pool_t *dp = dd->dd_pool;
781 dsl_dataset_phys_t *dsphys;
783 objset_t *mos = dp->dp_meta_objset;
786 origin = dp->dp_origin_snap;
788 ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
789 ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
790 ASSERT(dmu_tx_is_syncing(tx));
791 ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
793 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
794 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
795 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
796 dmu_buf_will_dirty(dbuf, tx);
797 dsphys = dbuf->db_data;
798 bzero(dsphys, sizeof (dsl_dataset_phys_t));
799 dsphys->ds_dir_obj = dd->dd_object;
800 dsphys->ds_flags = flags;
801 dsphys->ds_fsid_guid = unique_create();
803 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
804 sizeof (dsphys->ds_guid));
805 } while (dsphys->ds_guid == 0);
806 dsphys->ds_snapnames_zapobj =
807 zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
809 dsphys->ds_creation_time = gethrestime_sec();
810 dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
812 if (origin == NULL) {
813 dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
815 dsl_dataset_t *ohds; /* head of the origin snapshot */
817 dsphys->ds_prev_snap_obj = origin->ds_object;
818 dsphys->ds_prev_snap_txg =
819 dsl_dataset_phys(origin)->ds_creation_txg;
820 dsphys->ds_referenced_bytes =
821 dsl_dataset_phys(origin)->ds_referenced_bytes;
822 dsphys->ds_compressed_bytes =
823 dsl_dataset_phys(origin)->ds_compressed_bytes;
824 dsphys->ds_uncompressed_bytes =
825 dsl_dataset_phys(origin)->ds_uncompressed_bytes;
826 rrw_enter(&origin->ds_bp_rwlock, RW_READER, FTAG);
827 dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
828 rrw_exit(&origin->ds_bp_rwlock, FTAG);
831 * Inherit flags that describe the dataset's contents
832 * (INCONSISTENT) or properties (Case Insensitive).
834 dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
835 (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
837 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
838 if (origin->ds_feature_inuse[f])
839 dsl_dataset_activate_feature(dsobj, f, tx);
842 dmu_buf_will_dirty(origin->ds_dbuf, tx);
843 dsl_dataset_phys(origin)->ds_num_children++;
845 VERIFY0(dsl_dataset_hold_obj(dp,
846 dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
848 dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
849 dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
850 dsl_dataset_rele(ohds, FTAG);
852 if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
853 if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
854 dsl_dataset_phys(origin)->ds_next_clones_obj =
856 DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
858 VERIFY0(zap_add_int(mos,
859 dsl_dataset_phys(origin)->ds_next_clones_obj,
863 dmu_buf_will_dirty(dd->dd_dbuf, tx);
864 dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
865 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
866 if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
867 dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
868 dsl_dir_phys(origin->ds_dir)->dd_clones =
870 DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
872 VERIFY0(zap_add_int(mos,
873 dsl_dir_phys(origin->ds_dir)->dd_clones,
878 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
879 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
881 dmu_buf_rele(dbuf, FTAG);
883 dmu_buf_will_dirty(dd->dd_dbuf, tx);
884 dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
890 dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
894 VERIFY0(dmu_objset_from_ds(ds, &os));
895 if (bcmp(&os->os_zil_header, &zero_zil, sizeof (zero_zil)) != 0) {
896 dsl_pool_t *dp = ds->ds_dir->dd_pool;
899 bzero(&os->os_zil_header, sizeof (os->os_zil_header));
901 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
902 dsl_dataset_sync(ds, zio, tx);
903 VERIFY0(zio_wait(zio));
905 /* dsl_dataset_sync_done will drop this reference. */
906 dmu_buf_add_ref(ds->ds_dbuf, ds);
907 dsl_dataset_sync_done(ds, tx);
912 dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
913 dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
915 dsl_pool_t *dp = pdd->dd_pool;
916 uint64_t dsobj, ddobj;
919 ASSERT(dmu_tx_is_syncing(tx));
920 ASSERT(lastname[0] != '@');
922 ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
923 VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
925 dsobj = dsl_dataset_create_sync_dd(dd, origin,
926 flags & ~DS_CREATE_FLAG_NODIRTY, tx);
928 dsl_deleg_set_create_perms(dd, tx, cr);
931 * Since we're creating a new node we know it's a leaf, so we can
932 * initialize the counts if the limit feature is active.
934 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
936 objset_t *os = dd->dd_pool->dp_meta_objset;
938 dsl_dir_zapify(dd, tx);
939 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
940 sizeof (cnt), 1, &cnt, tx));
941 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
942 sizeof (cnt), 1, &cnt, tx));
945 dsl_dir_rele(dd, FTAG);
948 * If we are creating a clone, make sure we zero out any stale
949 * data from the origin snapshots zil header.
951 if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
954 VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
955 dsl_dataset_zero_zil(ds, tx);
956 dsl_dataset_rele(ds, FTAG);
963 /* FreeBSD ioctl compat begin */
966 const char *snapname;
970 dsl_check_snap_cb(const char *name, void *arg)
972 struct destroyarg *da = arg;
976 dsname = kmem_asprintf("%s@%s", name, da->snapname);
977 fnvlist_add_boolean(da->nvl, dsname);
978 kmem_free(dsname, strlen(dsname) + 1);
984 dmu_get_recursive_snaps_nvl(char *fsname, const char *snapname,
987 struct destroyarg *da;
990 da = kmem_zalloc(sizeof (struct destroyarg), KM_SLEEP);
992 da->snapname = snapname;
993 err = dmu_objset_find(fsname, dsl_check_snap_cb, da,
995 kmem_free(da, sizeof (struct destroyarg));
999 /* FreeBSD ioctl compat end */
1000 #endif /* __FreeBSD__ */
1003 * The unique space in the head dataset can be calculated by subtracting
1004 * the space used in the most recent snapshot, that is still being used
1005 * in this file system, from the space currently in use. To figure out
1006 * the space in the most recent snapshot still in use, we need to take
1007 * the total space used in the snapshot and subtract out the space that
1008 * has been freed up since the snapshot was taken.
1011 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
1014 uint64_t dlused, dlcomp, dluncomp;
1016 ASSERT(!ds->ds_is_snapshot);
1018 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
1019 mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
1023 dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
1025 ASSERT3U(dlused, <=, mrs_used);
1026 dsl_dataset_phys(ds)->ds_unique_bytes =
1027 dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
1029 if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
1030 SPA_VERSION_UNIQUE_ACCURATE)
1031 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1035 dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
1038 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1042 ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
1043 err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1046 * The err should not be ENOENT, but a bug in a previous version
1047 * of the code could cause upgrade_clones_cb() to not set
1048 * ds_next_snap_obj when it should, leading to a missing entry.
1049 * If we knew that the pool was created after
1050 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
1051 * ENOENT. However, at least we can check that we don't have
1052 * too many entries in the next_clones_obj even after failing to
1057 ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1059 ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
1064 dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1066 return (&dsl_dataset_phys(ds)->ds_bp);
1070 dsl_dataset_get_spa(dsl_dataset_t *ds)
1072 return (ds->ds_dir->dd_pool->dp_spa);
1076 dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1080 if (ds == NULL) /* this is the meta-objset */
1083 ASSERT(ds->ds_objset != NULL);
1085 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
1086 panic("dirtying snapshot!");
1088 /* Must not dirty a dataset in the same txg where it got snapshotted. */
1089 ASSERT3U(tx->tx_txg, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
1091 dp = ds->ds_dir->dd_pool;
1092 if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1093 /* up the hold count until we can be written out */
1094 dmu_buf_add_ref(ds->ds_dbuf, ds);
1099 dsl_dataset_is_dirty(dsl_dataset_t *ds)
1101 for (int t = 0; t < TXG_SIZE; t++) {
1102 if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
1110 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1114 if (!dmu_tx_is_syncing(tx))
1118 * If there's an fs-only reservation, any blocks that might become
1119 * owned by the snapshot dataset must be accommodated by space
1120 * outside of the reservation.
1122 ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
1123 asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
1124 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
1125 return (SET_ERROR(ENOSPC));
1128 * Propagate any reserved space for this snapshot to other
1129 * snapshot checks in this sync group.
1132 dsl_dir_willuse_space(ds->ds_dir, asize, tx);
1137 typedef struct dsl_dataset_snapshot_arg {
1138 nvlist_t *ddsa_snaps;
1139 nvlist_t *ddsa_props;
1140 nvlist_t *ddsa_errors;
1142 } dsl_dataset_snapshot_arg_t;
1145 dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1146 dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
1151 ds->ds_trysnap_txg = tx->tx_txg;
1153 if (!dmu_tx_is_syncing(tx))
1157 * We don't allow multiple snapshots of the same txg. If there
1158 * is already one, try again.
1160 if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
1161 return (SET_ERROR(EAGAIN));
1164 * Check for conflicting snapshot name.
1166 error = dsl_dataset_snap_lookup(ds, snapname, &value);
1168 return (SET_ERROR(EEXIST));
1169 if (error != ENOENT)
1173 * We don't allow taking snapshots of inconsistent datasets, such as
1174 * those into which we are currently receiving. However, if we are
1175 * creating this snapshot as part of a receive, this check will be
1176 * executed atomically with respect to the completion of the receive
1177 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1178 * case we ignore this, knowing it will be fixed up for us shortly in
1179 * dmu_recv_end_sync().
1181 if (!recv && DS_IS_INCONSISTENT(ds))
1182 return (SET_ERROR(EBUSY));
1185 * Skip the check for temporary snapshots or if we have already checked
1186 * the counts in dsl_dataset_snapshot_check. This means we really only
1187 * check the count here when we're receiving a stream.
1189 if (cnt != 0 && cr != NULL) {
1190 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1191 ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
1196 error = dsl_dataset_snapshot_reserve_space(ds, tx);
1204 dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1206 dsl_dataset_snapshot_arg_t *ddsa = arg;
1207 dsl_pool_t *dp = dmu_tx_pool(tx);
1212 * Pre-compute how many total new snapshots will be created for each
1213 * level in the tree and below. This is needed for validating the
1214 * snapshot limit when either taking a recursive snapshot or when
1215 * taking multiple snapshots.
1217 * The problem is that the counts are not actually adjusted when
1218 * we are checking, only when we finally sync. For a single snapshot,
1219 * this is easy, the count will increase by 1 at each node up the tree,
1220 * but its more complicated for the recursive/multiple snapshot case.
1222 * The dsl_fs_ss_limit_check function does recursively check the count
1223 * at each level up the tree but since it is validating each snapshot
1224 * independently we need to be sure that we are validating the complete
1225 * count for the entire set of snapshots. We do this by rolling up the
1226 * counts for each component of the name into an nvlist and then
1227 * checking each of those cases with the aggregated count.
1229 * This approach properly handles not only the recursive snapshot
1230 * case (where we get all of those on the ddsa_snaps list) but also
1231 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1232 * validate the limit on 'a' using a count of 2).
1234 * We validate the snapshot names in the third loop and only report
1237 if (dmu_tx_is_syncing(tx)) {
1238 nvlist_t *cnt_track = NULL;
1239 cnt_track = fnvlist_alloc();
1241 /* Rollup aggregated counts into the cnt_track list */
1242 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1244 pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1247 char nm[MAXPATHLEN];
1249 (void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1250 pdelim = strchr(nm, '@');
1256 if (nvlist_lookup_uint64(cnt_track, nm,
1258 /* update existing entry */
1259 fnvlist_add_uint64(cnt_track, nm,
1263 fnvlist_add_uint64(cnt_track, nm, 1);
1266 pdelim = strrchr(nm, '/');
1269 } while (pdelim != NULL);
1272 /* Check aggregated counts at each level */
1273 for (pair = nvlist_next_nvpair(cnt_track, NULL);
1274 pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1280 name = nvpair_name(pair);
1281 cnt = fnvpair_value_uint64(pair);
1284 error = dsl_dataset_hold(dp, name, FTAG, &ds);
1286 error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1287 ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1289 dsl_dataset_rele(ds, FTAG);
1293 if (ddsa->ddsa_errors != NULL)
1294 fnvlist_add_int32(ddsa->ddsa_errors,
1297 /* only report one error for this check */
1301 nvlist_free(cnt_track);
1304 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1305 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1309 char dsname[ZFS_MAX_DATASET_NAME_LEN];
1311 name = nvpair_name(pair);
1312 if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN)
1313 error = SET_ERROR(ENAMETOOLONG);
1315 atp = strchr(name, '@');
1317 error = SET_ERROR(EINVAL);
1319 (void) strlcpy(dsname, name, atp - name + 1);
1322 error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1324 /* passing 0/NULL skips dsl_fs_ss_limit_check */
1325 error = dsl_dataset_snapshot_check_impl(ds,
1326 atp + 1, tx, B_FALSE, 0, NULL);
1327 dsl_dataset_rele(ds, FTAG);
1331 if (ddsa->ddsa_errors != NULL) {
1332 fnvlist_add_int32(ddsa->ddsa_errors,
1343 dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1346 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1348 dsl_dataset_phys_t *dsphys;
1349 uint64_t dsobj, crtxg;
1350 objset_t *mos = dp->dp_meta_objset;
1353 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1356 * If we are on an old pool, the zil must not be active, in which
1357 * case it will be zeroed. Usually zil_suspend() accomplishes this.
1359 ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1360 dmu_objset_from_ds(ds, &os) != 0 ||
1361 bcmp(&os->os_phys->os_zil_header, &zero_zil,
1362 sizeof (zero_zil)) == 0);
1364 /* Should not snapshot a dirty dataset. */
1365 ASSERT(!txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
1368 dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1371 * The origin's ds_creation_txg has to be < TXG_INITIAL
1373 if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1378 dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1379 DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1380 VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1381 dmu_buf_will_dirty(dbuf, tx);
1382 dsphys = dbuf->db_data;
1383 bzero(dsphys, sizeof (dsl_dataset_phys_t));
1384 dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1385 dsphys->ds_fsid_guid = unique_create();
1387 (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1388 sizeof (dsphys->ds_guid));
1389 } while (dsphys->ds_guid == 0);
1390 dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1391 dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1392 dsphys->ds_next_snap_obj = ds->ds_object;
1393 dsphys->ds_num_children = 1;
1394 dsphys->ds_creation_time = gethrestime_sec();
1395 dsphys->ds_creation_txg = crtxg;
1396 dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
1397 dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
1398 dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
1399 dsphys->ds_uncompressed_bytes =
1400 dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1401 dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
1402 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1403 dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
1404 rrw_exit(&ds->ds_bp_rwlock, FTAG);
1405 dmu_buf_rele(dbuf, FTAG);
1407 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1408 if (ds->ds_feature_inuse[f])
1409 dsl_dataset_activate_feature(dsobj, f, tx);
1412 ASSERT3U(ds->ds_prev != 0, ==,
1413 dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
1415 uint64_t next_clones_obj =
1416 dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
1417 ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1419 dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
1420 if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1422 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1423 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
1424 dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
1425 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
1426 } else if (next_clones_obj != 0) {
1427 dsl_dataset_remove_from_next_clones(ds->ds_prev,
1428 dsphys->ds_next_snap_obj, tx);
1429 VERIFY0(zap_add_int(mos,
1430 next_clones_obj, dsobj, tx));
1435 * If we have a reference-reservation on this dataset, we will
1436 * need to increase the amount of refreservation being charged
1437 * since our unique space is going to zero.
1439 if (ds->ds_reserved) {
1441 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1442 delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
1444 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1448 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1449 dsl_dataset_phys(ds)->ds_deadlist_obj =
1450 dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
1451 dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
1452 dsl_deadlist_close(&ds->ds_deadlist);
1453 dsl_deadlist_open(&ds->ds_deadlist, mos,
1454 dsl_dataset_phys(ds)->ds_deadlist_obj);
1455 dsl_deadlist_add_key(&ds->ds_deadlist,
1456 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
1458 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
1459 dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
1460 dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
1461 dsl_dataset_phys(ds)->ds_unique_bytes = 0;
1462 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1463 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1465 VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1466 snapname, 8, 1, &dsobj, tx));
1469 dsl_dataset_rele(ds->ds_prev, ds);
1470 VERIFY0(dsl_dataset_hold_obj(dp,
1471 dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
1473 dsl_scan_ds_snapshotted(ds, tx);
1475 dsl_dir_snap_cmtime_update(ds->ds_dir);
1477 spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1481 dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1483 dsl_dataset_snapshot_arg_t *ddsa = arg;
1484 dsl_pool_t *dp = dmu_tx_pool(tx);
1487 for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1488 pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1491 char dsname[ZFS_MAX_DATASET_NAME_LEN];
1493 name = nvpair_name(pair);
1494 atp = strchr(name, '@');
1495 (void) strlcpy(dsname, name, atp - name + 1);
1496 VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1498 dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1499 if (ddsa->ddsa_props != NULL) {
1500 dsl_props_set_sync_impl(ds->ds_prev,
1501 ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1503 dsl_dataset_rele(ds, FTAG);
1508 * The snapshots must all be in the same pool.
1509 * All-or-nothing: if there are any failures, nothing will be modified.
1512 dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1514 dsl_dataset_snapshot_arg_t ddsa;
1516 boolean_t needsuspend;
1520 nvlist_t *suspended = NULL;
1522 pair = nvlist_next_nvpair(snaps, NULL);
1525 firstname = nvpair_name(pair);
1527 error = spa_open(firstname, &spa, FTAG);
1530 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1531 spa_close(spa, FTAG);
1534 suspended = fnvlist_alloc();
1535 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1536 pair = nvlist_next_nvpair(snaps, pair)) {
1537 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1538 char *snapname = nvpair_name(pair);
1542 atp = strchr(snapname, '@');
1544 error = SET_ERROR(EINVAL);
1547 (void) strlcpy(fsname, snapname, atp - snapname + 1);
1549 error = zil_suspend(fsname, &cookie);
1552 fnvlist_add_uint64(suspended, fsname,
1557 ddsa.ddsa_snaps = snaps;
1558 ddsa.ddsa_props = props;
1559 ddsa.ddsa_errors = errors;
1560 ddsa.ddsa_cr = CRED();
1563 error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1564 dsl_dataset_snapshot_sync, &ddsa,
1565 fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
1568 if (suspended != NULL) {
1569 for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1570 pair = nvlist_next_nvpair(suspended, pair)) {
1571 zil_resume((void *)(uintptr_t)
1572 fnvpair_value_uint64(pair));
1574 fnvlist_free(suspended);
1580 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1581 pair = nvlist_next_nvpair(snaps, pair)) {
1582 char *snapname = nvpair_name(pair);
1583 zvol_create_minors(snapname);
1591 typedef struct dsl_dataset_snapshot_tmp_arg {
1592 const char *ddsta_fsname;
1593 const char *ddsta_snapname;
1594 minor_t ddsta_cleanup_minor;
1595 const char *ddsta_htag;
1596 } dsl_dataset_snapshot_tmp_arg_t;
1599 dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1601 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1602 dsl_pool_t *dp = dmu_tx_pool(tx);
1606 error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1610 /* NULL cred means no limit check for tmp snapshot */
1611 error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1612 tx, B_FALSE, 0, NULL);
1614 dsl_dataset_rele(ds, FTAG);
1618 if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
1619 dsl_dataset_rele(ds, FTAG);
1620 return (SET_ERROR(ENOTSUP));
1622 error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
1625 dsl_dataset_rele(ds, FTAG);
1629 dsl_dataset_rele(ds, FTAG);
1634 dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
1636 dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1637 dsl_pool_t *dp = dmu_tx_pool(tx);
1640 VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
1642 dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
1643 dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
1644 ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
1645 dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
1647 dsl_dataset_rele(ds, FTAG);
1651 dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
1652 minor_t cleanup_minor, const char *htag)
1654 dsl_dataset_snapshot_tmp_arg_t ddsta;
1657 boolean_t needsuspend;
1660 ddsta.ddsta_fsname = fsname;
1661 ddsta.ddsta_snapname = snapname;
1662 ddsta.ddsta_cleanup_minor = cleanup_minor;
1663 ddsta.ddsta_htag = htag;
1665 error = spa_open(fsname, &spa, FTAG);
1668 needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1669 spa_close(spa, FTAG);
1672 error = zil_suspend(fsname, &cookie);
1677 error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
1678 dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
1687 dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1689 ASSERT(dmu_tx_is_syncing(tx));
1690 ASSERT(ds->ds_objset != NULL);
1691 ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
1694 * in case we had to change ds_fsid_guid when we opened it,
1697 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1698 dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
1700 if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) {
1701 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
1702 ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1,
1703 &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx));
1704 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
1705 ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1,
1706 &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx));
1707 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
1708 ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1,
1709 &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx));
1710 ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0;
1711 ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0;
1712 ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0;
1715 dmu_objset_sync(ds->ds_objset, zio, tx);
1717 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1718 if (ds->ds_feature_activation_needed[f]) {
1719 if (ds->ds_feature_inuse[f])
1721 dsl_dataset_activate_feature(ds->ds_object, f, tx);
1722 ds->ds_feature_inuse[f] = B_TRUE;
1728 deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1730 dsl_deadlist_t *dl = arg;
1731 dsl_deadlist_insert(dl, bp, tx);
1736 dsl_dataset_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx)
1738 objset_t *os = ds->ds_objset;
1740 bplist_iterate(&ds->ds_pending_deadlist,
1741 deadlist_enqueue_cb, &ds->ds_deadlist, tx);
1743 if (os->os_synced_dnodes != NULL) {
1744 multilist_destroy(os->os_synced_dnodes);
1745 os->os_synced_dnodes = NULL;
1748 ASSERT(!dmu_objset_is_dirty(os, dmu_tx_get_txg(tx)));
1750 dmu_buf_rele(ds->ds_dbuf, ds);
1754 get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1757 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1760 nvlist_t *propval = fnvlist_alloc();
1763 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1766 * We use nvlist_alloc() instead of fnvlist_alloc() because the
1767 * latter would allocate the list with NV_UNIQUE_NAME flag.
1768 * As a result, every time a clone name is appended to the list
1769 * it would be (linearly) searched for for a duplicate name.
1770 * We already know that all clone names must be unique and we
1771 * want avoid the quadratic complexity of double-checking that
1772 * because we can have a large number of clones.
1774 VERIFY0(nvlist_alloc(&val, 0, KM_SLEEP));
1777 * There may be missing entries in ds_next_clones_obj
1778 * due to a bug in a previous version of the code.
1779 * Only trust it if it has the right number of entries.
1781 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
1782 VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1785 if (count != dsl_dataset_phys(ds)->ds_num_children - 1)
1787 for (zap_cursor_init(&zc, mos,
1788 dsl_dataset_phys(ds)->ds_next_clones_obj);
1789 zap_cursor_retrieve(&zc, &za) == 0;
1790 zap_cursor_advance(&zc)) {
1791 dsl_dataset_t *clone;
1792 char buf[ZFS_MAX_DATASET_NAME_LEN];
1793 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1794 za.za_first_integer, FTAG, &clone));
1795 dsl_dir_name(clone->ds_dir, buf);
1796 fnvlist_add_boolean(val, buf);
1797 dsl_dataset_rele(clone, FTAG);
1799 zap_cursor_fini(&zc);
1800 fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1801 fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
1804 nvlist_free(propval);
1808 get_receive_resume_stats(dsl_dataset_t *ds, nvlist_t *nv)
1810 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1812 if (dsl_dataset_has_resume_receive_state(ds)) {
1815 uint8_t *compressed;
1817 nvlist_t *token_nv = fnvlist_alloc();
1818 size_t packed_size, compressed_size;
1820 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1821 DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) {
1822 fnvlist_add_uint64(token_nv, "fromguid", val);
1824 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1825 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) {
1826 fnvlist_add_uint64(token_nv, "object", val);
1828 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1829 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) {
1830 fnvlist_add_uint64(token_nv, "offset", val);
1832 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1833 DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) {
1834 fnvlist_add_uint64(token_nv, "bytes", val);
1836 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1837 DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) {
1838 fnvlist_add_uint64(token_nv, "toguid", val);
1841 if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
1842 DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) {
1843 fnvlist_add_string(token_nv, "toname", buf);
1845 if (zap_contains(dp->dp_meta_objset, ds->ds_object,
1846 DS_FIELD_RESUME_LARGEBLOCK) == 0) {
1847 fnvlist_add_boolean(token_nv, "largeblockok");
1849 if (zap_contains(dp->dp_meta_objset, ds->ds_object,
1850 DS_FIELD_RESUME_EMBEDOK) == 0) {
1851 fnvlist_add_boolean(token_nv, "embedok");
1853 if (zap_contains(dp->dp_meta_objset, ds->ds_object,
1854 DS_FIELD_RESUME_COMPRESSOK) == 0) {
1855 fnvlist_add_boolean(token_nv, "compressok");
1857 packed = fnvlist_pack(token_nv, &packed_size);
1858 fnvlist_free(token_nv);
1859 compressed = kmem_alloc(packed_size, KM_SLEEP);
1861 compressed_size = gzip_compress(packed, compressed,
1862 packed_size, packed_size, 6);
1865 fletcher_4_native(compressed, compressed_size, NULL, &cksum);
1867 str = kmem_alloc(compressed_size * 2 + 1, KM_SLEEP);
1868 for (int i = 0; i < compressed_size; i++) {
1869 (void) sprintf(str + i * 2, "%02x", compressed[i]);
1871 str[compressed_size * 2] = '\0';
1872 char *propval = kmem_asprintf("%u-%llx-%llx-%s",
1873 ZFS_SEND_RESUME_TOKEN_VERSION,
1874 (longlong_t)cksum.zc_word[0],
1875 (longlong_t)packed_size, str);
1876 dsl_prop_nvlist_add_string(nv,
1877 ZFS_PROP_RECEIVE_RESUME_TOKEN, propval);
1878 kmem_free(packed, packed_size);
1879 kmem_free(str, compressed_size * 2 + 1);
1880 kmem_free(compressed, packed_size);
1886 dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1888 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1889 uint64_t refd, avail, uobjs, aobjs, ratio;
1891 ASSERT(dsl_pool_config_held(dp));
1893 ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
1894 (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
1895 dsl_dataset_phys(ds)->ds_compressed_bytes);
1897 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
1898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1899 dsl_dataset_phys(ds)->ds_uncompressed_bytes);
1901 if (ds->ds_is_snapshot) {
1902 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
1903 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1904 dsl_dataset_phys(ds)->ds_unique_bytes);
1905 get_clones_stat(ds, nv);
1907 if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
1908 char buf[ZFS_MAX_DATASET_NAME_LEN];
1909 dsl_dataset_name(ds->ds_prev, buf);
1910 dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf);
1913 dsl_dir_stats(ds->ds_dir, nv);
1916 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1917 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1918 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1920 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1921 dsl_dataset_phys(ds)->ds_creation_time);
1922 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1923 dsl_dataset_phys(ds)->ds_creation_txg);
1924 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1926 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1928 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1929 dsl_dataset_phys(ds)->ds_guid);
1930 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1931 dsl_dataset_phys(ds)->ds_unique_bytes);
1932 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
1934 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
1936 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1937 DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1939 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
1940 uint64_t written, comp, uncomp;
1941 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1942 dsl_dataset_t *prev;
1944 int err = dsl_dataset_hold_obj(dp,
1945 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
1947 err = dsl_dataset_space_written(prev, ds, &written,
1949 dsl_dataset_rele(prev, FTAG);
1951 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
1957 if (!dsl_dataset_is_snapshot(ds)) {
1959 * A failed "newfs" (e.g. full) resumable receive leaves
1960 * the stats set on this dataset. Check here for the prop.
1962 get_receive_resume_stats(ds, nv);
1965 * A failed incremental resumable receive leaves the
1966 * stats set on our child named "%recv". Check the child
1969 /* 6 extra bytes for /%recv */
1970 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1971 dsl_dataset_t *recv_ds;
1972 dsl_dataset_name(ds, recvname);
1973 if (strlcat(recvname, "/", sizeof (recvname)) <
1974 sizeof (recvname) &&
1975 strlcat(recvname, recv_clone_name, sizeof (recvname)) <
1976 sizeof (recvname) &&
1977 dsl_dataset_hold(dp, recvname, FTAG, &recv_ds) == 0) {
1978 get_receive_resume_stats(recv_ds, nv);
1979 dsl_dataset_rele(recv_ds, FTAG);
1985 dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1987 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1988 ASSERT(dsl_pool_config_held(dp));
1990 stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg;
1991 stat->dds_inconsistent =
1992 dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT;
1993 stat->dds_guid = dsl_dataset_phys(ds)->ds_guid;
1994 stat->dds_origin[0] = '\0';
1995 if (ds->ds_is_snapshot) {
1996 stat->dds_is_snapshot = B_TRUE;
1997 stat->dds_num_clones =
1998 dsl_dataset_phys(ds)->ds_num_children - 1;
2000 stat->dds_is_snapshot = B_FALSE;
2001 stat->dds_num_clones = 0;
2003 if (dsl_dir_is_clone(ds->ds_dir)) {
2006 VERIFY0(dsl_dataset_hold_obj(dp,
2007 dsl_dir_phys(ds->ds_dir)->dd_origin_obj,
2009 dsl_dataset_name(ods, stat->dds_origin);
2010 dsl_dataset_rele(ods, FTAG);
2016 dsl_dataset_fsid_guid(dsl_dataset_t *ds)
2018 return (ds->ds_fsid_guid);
2022 dsl_dataset_space(dsl_dataset_t *ds,
2023 uint64_t *refdbytesp, uint64_t *availbytesp,
2024 uint64_t *usedobjsp, uint64_t *availobjsp)
2026 *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
2027 *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
2028 if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
2030 ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
2031 if (ds->ds_quota != 0) {
2033 * Adjust available bytes according to refquota
2035 if (*refdbytesp < ds->ds_quota)
2036 *availbytesp = MIN(*availbytesp,
2037 ds->ds_quota - *refdbytesp);
2041 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2042 *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
2043 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2044 *availobjsp = DN_MAX_OBJECT - *usedobjsp;
2048 dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
2050 dsl_pool_t *dp = ds->ds_dir->dd_pool;
2053 ASSERT(dsl_pool_config_held(dp));
2056 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2057 birth = dsl_dataset_get_blkptr(ds)->blk_birth;
2058 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2059 if (birth > dsl_dataset_phys(snap)->ds_creation_txg) {
2060 objset_t *os, *os_snap;
2062 * It may be that only the ZIL differs, because it was
2063 * reset in the head. Don't count that as being
2066 if (dmu_objset_from_ds(ds, &os) != 0)
2068 if (dmu_objset_from_ds(snap, &os_snap) != 0)
2070 return (bcmp(&os->os_phys->os_meta_dnode,
2071 &os_snap->os_phys->os_meta_dnode,
2072 sizeof (os->os_phys->os_meta_dnode)) != 0);
2077 typedef struct dsl_dataset_rename_snapshot_arg {
2078 const char *ddrsa_fsname;
2079 const char *ddrsa_oldsnapname;
2080 const char *ddrsa_newsnapname;
2081 boolean_t ddrsa_recursive;
2083 } dsl_dataset_rename_snapshot_arg_t;
2087 dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
2088 dsl_dataset_t *hds, void *arg)
2090 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2094 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
2096 /* ignore nonexistent snapshots */
2097 return (error == ENOENT ? 0 : error);
2100 /* new name should not exist */
2101 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
2103 error = SET_ERROR(EEXIST);
2104 else if (error == ENOENT)
2107 /* dataset name + 1 for the "@" + the new snapshot name must fit */
2108 if (dsl_dir_namelen(hds->ds_dir) + 1 +
2109 strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN)
2110 error = SET_ERROR(ENAMETOOLONG);
2116 dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
2118 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2119 dsl_pool_t *dp = dmu_tx_pool(tx);
2123 error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
2127 if (ddrsa->ddrsa_recursive) {
2128 error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
2129 dsl_dataset_rename_snapshot_check_impl, ddrsa,
2132 error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
2134 dsl_dataset_rele(hds, FTAG);
2139 dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
2140 dsl_dataset_t *hds, void *arg)
2144 char *oldname, *newname;
2147 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2150 dmu_tx_t *tx = ddrsa->ddrsa_tx;
2153 error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
2154 ASSERT(error == 0 || error == ENOENT);
2155 if (error == ENOENT) {
2156 /* ignore nonexistent snapshots */
2160 VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
2162 /* log before we change the name */
2163 spa_history_log_internal_ds(ds, "rename", tx,
2164 "-> @%s", ddrsa->ddrsa_newsnapname);
2166 VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
2168 mutex_enter(&ds->ds_lock);
2169 (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
2170 mutex_exit(&ds->ds_lock);
2171 VERIFY0(zap_add(dp->dp_meta_objset,
2172 dsl_dataset_phys(hds)->ds_snapnames_zapobj,
2173 ds->ds_snapname, 8, 1, &ds->ds_object, tx));
2177 oldname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2178 newname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2179 snprintf(oldname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname,
2180 ddrsa->ddrsa_oldsnapname);
2181 snprintf(newname, MAXPATHLEN, "%s@%s", ddrsa->ddrsa_fsname,
2182 ddrsa->ddrsa_newsnapname);
2183 zfsvfs_update_fromname(oldname, newname);
2184 zvol_rename_minors(oldname, newname);
2185 kmem_free(newname, MAXPATHLEN);
2186 kmem_free(oldname, MAXPATHLEN);
2189 dsl_dataset_rele(ds, FTAG);
2195 dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
2197 dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2198 dsl_pool_t *dp = dmu_tx_pool(tx);
2201 VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
2202 ddrsa->ddrsa_tx = tx;
2203 if (ddrsa->ddrsa_recursive) {
2204 VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
2205 dsl_dataset_rename_snapshot_sync_impl, ddrsa,
2208 VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
2210 dsl_dataset_rele(hds, FTAG);
2214 dsl_dataset_rename_snapshot(const char *fsname,
2215 const char *oldsnapname, const char *newsnapname, boolean_t recursive)
2217 dsl_dataset_rename_snapshot_arg_t ddrsa;
2219 ddrsa.ddrsa_fsname = fsname;
2220 ddrsa.ddrsa_oldsnapname = oldsnapname;
2221 ddrsa.ddrsa_newsnapname = newsnapname;
2222 ddrsa.ddrsa_recursive = recursive;
2224 return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
2225 dsl_dataset_rename_snapshot_sync, &ddrsa,
2226 1, ZFS_SPACE_CHECK_RESERVED));
2230 * If we're doing an ownership handoff, we need to make sure that there is
2231 * only one long hold on the dataset. We're not allowed to change anything here
2232 * so we don't permanently release the long hold or regular hold here. We want
2233 * to do this only when syncing to avoid the dataset unexpectedly going away
2234 * when we release the long hold.
2237 dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
2241 if (!dmu_tx_is_syncing(tx))
2244 if (owner != NULL) {
2245 VERIFY3P(ds->ds_owner, ==, owner);
2246 dsl_dataset_long_rele(ds, owner);
2249 held = dsl_dataset_long_held(ds);
2252 dsl_dataset_long_hold(ds, owner);
2255 return (SET_ERROR(EBUSY));
2260 typedef struct dsl_dataset_rollback_arg {
2261 const char *ddra_fsname;
2263 nvlist_t *ddra_result;
2264 } dsl_dataset_rollback_arg_t;
2267 dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
2269 dsl_dataset_rollback_arg_t *ddra = arg;
2270 dsl_pool_t *dp = dmu_tx_pool(tx);
2272 int64_t unused_refres_delta;
2275 error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
2279 /* must not be a snapshot */
2280 if (ds->ds_is_snapshot) {
2281 dsl_dataset_rele(ds, FTAG);
2282 return (SET_ERROR(EINVAL));
2285 /* must have a most recent snapshot */
2286 if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
2287 dsl_dataset_rele(ds, FTAG);
2288 return (SET_ERROR(EINVAL));
2292 * No rollback to a snapshot created in the current txg, because
2293 * the rollback may dirty the dataset and create blocks that are
2294 * not reachable from the rootbp while having a birth txg that
2295 * falls into the snapshot's range.
2297 if (dmu_tx_is_syncing(tx) &&
2298 dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) {
2299 dsl_dataset_rele(ds, FTAG);
2300 return (SET_ERROR(EAGAIN));
2303 /* must not have any bookmarks after the most recent snapshot */
2304 nvlist_t *proprequest = fnvlist_alloc();
2305 fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
2306 nvlist_t *bookmarks = fnvlist_alloc();
2307 error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
2308 fnvlist_free(proprequest);
2311 for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL);
2312 pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
2314 fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
2315 zfs_prop_to_name(ZFS_PROP_CREATETXG));
2316 uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
2317 if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
2318 fnvlist_free(bookmarks);
2319 dsl_dataset_rele(ds, FTAG);
2320 return (SET_ERROR(EEXIST));
2323 fnvlist_free(bookmarks);
2325 error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
2327 dsl_dataset_rele(ds, FTAG);
2332 * Check if the snap we are rolling back to uses more than
2335 if (ds->ds_quota != 0 &&
2336 dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
2337 dsl_dataset_rele(ds, FTAG);
2338 return (SET_ERROR(EDQUOT));
2342 * When we do the clone swap, we will temporarily use more space
2343 * due to the refreservation (the head will no longer have any
2344 * unique space, so the entire amount of the refreservation will need
2345 * to be free). We will immediately destroy the clone, freeing
2346 * this space, but the freeing happens over many txg's.
2348 unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
2349 dsl_dataset_phys(ds)->ds_unique_bytes);
2351 if (unused_refres_delta > 0 &&
2352 unused_refres_delta >
2353 dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
2354 dsl_dataset_rele(ds, FTAG);
2355 return (SET_ERROR(ENOSPC));
2358 dsl_dataset_rele(ds, FTAG);
2363 dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
2365 dsl_dataset_rollback_arg_t *ddra = arg;
2366 dsl_pool_t *dp = dmu_tx_pool(tx);
2367 dsl_dataset_t *ds, *clone;
2369 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
2371 VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
2373 dsl_dataset_name(ds->ds_prev, namebuf);
2374 fnvlist_add_string(ddra->ddra_result, "target", namebuf);
2376 cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
2377 ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
2379 VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
2381 dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
2382 dsl_dataset_zero_zil(ds, tx);
2384 dsl_destroy_head_sync_impl(clone, tx);
2386 dsl_dataset_rele(clone, FTAG);
2387 dsl_dataset_rele(ds, FTAG);
2391 * Rolls back the given filesystem or volume to the most recent snapshot.
2392 * The name of the most recent snapshot will be returned under key "target"
2393 * in the result nvlist.
2396 * - The existing dataset MUST be owned by the specified owner at entry
2397 * - Upon return, dataset will still be held by the same owner, whether we
2400 * This mode is required any time the existing filesystem is mounted. See
2401 * notes above zfs_suspend_fs() for further details.
2404 dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
2406 dsl_dataset_rollback_arg_t ddra;
2408 ddra.ddra_fsname = fsname;
2409 ddra.ddra_owner = owner;
2410 ddra.ddra_result = result;
2412 return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
2413 dsl_dataset_rollback_sync, &ddra,
2414 1, ZFS_SPACE_CHECK_RESERVED));
2417 struct promotenode {
2422 typedef struct dsl_dataset_promote_arg {
2423 const char *ddpa_clonename;
2424 dsl_dataset_t *ddpa_clone;
2425 list_t shared_snaps, origin_snaps, clone_snaps;
2426 dsl_dataset_t *origin_origin; /* origin of the origin */
2427 uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
2430 } dsl_dataset_promote_arg_t;
2432 static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
2433 static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
2435 static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
2438 dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
2440 dsl_dataset_promote_arg_t *ddpa = arg;
2441 dsl_pool_t *dp = dmu_tx_pool(tx);
2443 struct promotenode *snap;
2444 dsl_dataset_t *origin_ds;
2448 size_t max_snap_len;
2450 err = promote_hold(ddpa, dp, FTAG);
2454 hds = ddpa->ddpa_clone;
2455 max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
2457 if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
2458 promote_rele(ddpa, FTAG);
2459 return (SET_ERROR(EXDEV));
2463 * Compute and check the amount of space to transfer. Since this is
2464 * so expensive, don't do the preliminary check.
2466 if (!dmu_tx_is_syncing(tx)) {
2467 promote_rele(ddpa, FTAG);
2471 snap = list_head(&ddpa->shared_snaps);
2472 origin_ds = snap->ds;
2474 /* compute origin's new unique space */
2475 snap = list_tail(&ddpa->clone_snaps);
2476 ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2477 origin_ds->ds_object);
2478 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2479 dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
2480 &ddpa->unique, &unused, &unused);
2483 * Walk the snapshots that we are moving
2485 * Compute space to transfer. Consider the incremental changes
2486 * to used by each snapshot:
2487 * (my used) = (prev's used) + (blocks born) - (blocks killed)
2488 * So each snapshot gave birth to:
2489 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2490 * So a sequence would look like:
2491 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2492 * Which simplifies to:
2493 * uN + kN + kN-1 + ... + k1 + k0
2494 * Note however, if we stop before we reach the ORIGIN we get:
2495 * uN + kN + kN-1 + ... + kM - uM-1
2498 ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
2499 ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
2500 ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
2501 for (snap = list_head(&ddpa->shared_snaps); snap;
2502 snap = list_next(&ddpa->shared_snaps, snap)) {
2503 uint64_t val, dlused, dlcomp, dluncomp;
2504 dsl_dataset_t *ds = snap->ds;
2509 * If there are long holds, we won't be able to evict
2512 if (dsl_dataset_long_held(ds)) {
2513 err = SET_ERROR(EBUSY);
2517 /* Check that the snapshot name does not conflict */
2518 VERIFY0(dsl_dataset_get_snapname(ds));
2519 if (strlen(ds->ds_snapname) >= max_snap_len) {
2520 err = SET_ERROR(ENAMETOOLONG);
2523 err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2525 (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2526 err = SET_ERROR(EEXIST);
2532 /* The very first snapshot does not have a deadlist */
2533 if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
2536 dsl_deadlist_space(&ds->ds_deadlist,
2537 &dlused, &dlcomp, &dluncomp);
2538 ddpa->used += dlused;
2539 ddpa->comp += dlcomp;
2540 ddpa->uncomp += dluncomp;
2544 * If we are a clone of a clone then we never reached ORIGIN,
2545 * so we need to subtract out the clone origin's used space.
2547 if (ddpa->origin_origin) {
2549 dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
2551 dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
2553 dsl_dataset_phys(ddpa->origin_origin)->
2554 ds_uncompressed_bytes;
2557 /* Check that there is enough space and limit headroom here */
2558 err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2559 0, ss_mv_cnt, ddpa->used, ddpa->cr);
2564 * Compute the amounts of space that will be used by snapshots
2565 * after the promotion (for both origin and clone). For each,
2566 * it is the amount of space that will be on all of their
2567 * deadlists (that was not born before their new origin).
2569 if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
2573 * Note, typically this will not be a clone of a clone,
2574 * so dd_origin_txg will be < TXG_INITIAL, so
2575 * these snaplist_space() -> dsl_deadlist_space_range()
2576 * calls will be fast because they do not have to
2577 * iterate over all bps.
2579 snap = list_head(&ddpa->origin_snaps);
2580 err = snaplist_space(&ddpa->shared_snaps,
2581 snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
2585 err = snaplist_space(&ddpa->clone_snaps,
2586 snap->ds->ds_dir->dd_origin_txg, &space);
2589 ddpa->cloneusedsnap += space;
2591 if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
2592 DD_FLAG_USED_BREAKDOWN) {
2593 err = snaplist_space(&ddpa->origin_snaps,
2594 dsl_dataset_phys(origin_ds)->ds_creation_txg,
2595 &ddpa->originusedsnap);
2601 promote_rele(ddpa, FTAG);
2606 dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
2608 dsl_dataset_promote_arg_t *ddpa = arg;
2609 dsl_pool_t *dp = dmu_tx_pool(tx);
2611 struct promotenode *snap;
2612 dsl_dataset_t *origin_ds;
2613 dsl_dataset_t *origin_head;
2615 dsl_dir_t *odd = NULL;
2616 uint64_t oldnext_obj;
2618 #if defined(__FreeBSD__) && defined(_KERNEL)
2619 char *oldname, *newname;
2622 VERIFY0(promote_hold(ddpa, dp, FTAG));
2623 hds = ddpa->ddpa_clone;
2625 ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
2627 snap = list_head(&ddpa->shared_snaps);
2628 origin_ds = snap->ds;
2631 snap = list_head(&ddpa->origin_snaps);
2632 origin_head = snap->ds;
2635 * We need to explicitly open odd, since origin_ds's dd will be
2638 VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
2641 /* change origin's next snap */
2642 dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2643 oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
2644 snap = list_tail(&ddpa->clone_snaps);
2645 ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2646 origin_ds->ds_object);
2647 dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
2649 /* change the origin's next clone */
2650 if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
2651 dsl_dataset_remove_from_next_clones(origin_ds,
2652 snap->ds->ds_object, tx);
2653 VERIFY0(zap_add_int(dp->dp_meta_objset,
2654 dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
2659 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2660 ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
2661 dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
2662 dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2663 dmu_buf_will_dirty(odd->dd_dbuf, tx);
2664 dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
2665 origin_head->ds_dir->dd_origin_txg =
2666 dsl_dataset_phys(origin_ds)->ds_creation_txg;
2668 /* change dd_clone entries */
2669 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2670 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2671 dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
2672 VERIFY0(zap_add_int(dp->dp_meta_objset,
2673 dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2674 hds->ds_object, tx));
2676 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2677 dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2678 origin_head->ds_object, tx));
2679 if (dsl_dir_phys(dd)->dd_clones == 0) {
2680 dsl_dir_phys(dd)->dd_clones =
2681 zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
2682 DMU_OT_NONE, 0, tx);
2684 VERIFY0(zap_add_int(dp->dp_meta_objset,
2685 dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
2688 #if defined(__FreeBSD__) && defined(_KERNEL)
2689 /* Take the spa_namespace_lock early so zvol renames don't deadlock. */
2690 mutex_enter(&spa_namespace_lock);
2692 oldname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2693 newname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2696 /* move snapshots to this dir */
2697 for (snap = list_head(&ddpa->shared_snaps); snap;
2698 snap = list_next(&ddpa->shared_snaps, snap)) {
2699 dsl_dataset_t *ds = snap->ds;
2702 * Property callbacks are registered to a particular
2703 * dsl_dir. Since ours is changing, evict the objset
2704 * so that they will be unregistered from the old dsl_dir.
2706 if (ds->ds_objset) {
2707 dmu_objset_evict(ds->ds_objset);
2708 ds->ds_objset = NULL;
2711 /* move snap name entry */
2712 VERIFY0(dsl_dataset_get_snapname(ds));
2713 VERIFY0(dsl_dataset_snap_remove(origin_head,
2714 ds->ds_snapname, tx, B_TRUE));
2715 VERIFY0(zap_add(dp->dp_meta_objset,
2716 dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
2717 8, 1, &ds->ds_object, tx));
2718 dsl_fs_ss_count_adjust(hds->ds_dir, 1,
2719 DD_FIELD_SNAPSHOT_COUNT, tx);
2721 /* change containing dsl_dir */
2722 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2723 ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
2724 dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
2725 ASSERT3P(ds->ds_dir, ==, odd);
2726 dsl_dir_rele(ds->ds_dir, ds);
2727 VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
2728 NULL, ds, &ds->ds_dir));
2730 #if defined(__FreeBSD__) && defined(_KERNEL)
2731 dsl_dataset_name(ds, newname);
2732 zfsvfs_update_fromname(oldname, newname);
2733 zvol_rename_minors(oldname, newname);
2736 /* move any clone references */
2737 if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
2738 spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2742 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2743 dsl_dataset_phys(ds)->ds_next_clones_obj);
2744 zap_cursor_retrieve(&zc, &za) == 0;
2745 zap_cursor_advance(&zc)) {
2746 dsl_dataset_t *cnds;
2749 if (za.za_first_integer == oldnext_obj) {
2751 * We've already moved the
2752 * origin's reference.
2757 VERIFY0(dsl_dataset_hold_obj(dp,
2758 za.za_first_integer, FTAG, &cnds));
2759 o = dsl_dir_phys(cnds->ds_dir)->
2760 dd_head_dataset_obj;
2762 VERIFY0(zap_remove_int(dp->dp_meta_objset,
2763 dsl_dir_phys(odd)->dd_clones, o, tx));
2764 VERIFY0(zap_add_int(dp->dp_meta_objset,
2765 dsl_dir_phys(dd)->dd_clones, o, tx));
2766 dsl_dataset_rele(cnds, FTAG);
2768 zap_cursor_fini(&zc);
2771 ASSERT(!dsl_prop_hascb(ds));
2774 #if defined(__FreeBSD__) && defined(_KERNEL)
2775 mutex_exit(&spa_namespace_lock);
2777 kmem_free(newname, MAXPATHLEN);
2778 kmem_free(oldname, MAXPATHLEN);
2781 * Change space accounting.
2782 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
2783 * both be valid, or both be 0 (resulting in delta == 0). This
2784 * is true for each of {clone,origin} independently.
2787 delta = ddpa->cloneusedsnap -
2788 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
2789 ASSERT3S(delta, >=, 0);
2790 ASSERT3U(ddpa->used, >=, delta);
2791 dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
2792 dsl_dir_diduse_space(dd, DD_USED_HEAD,
2793 ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
2795 delta = ddpa->originusedsnap -
2796 dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
2797 ASSERT3S(delta, <=, 0);
2798 ASSERT3U(ddpa->used, >=, -delta);
2799 dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
2800 dsl_dir_diduse_space(odd, DD_USED_HEAD,
2801 -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
2803 dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
2805 /* log history record */
2806 spa_history_log_internal_ds(hds, "promote", tx, "");
2808 dsl_dir_rele(odd, FTAG);
2809 promote_rele(ddpa, FTAG);
2813 * Make a list of dsl_dataset_t's for the snapshots between first_obj
2814 * (exclusive) and last_obj (inclusive). The list will be in reverse
2815 * order (last_obj will be the list_head()). If first_obj == 0, do all
2816 * snapshots back to this dataset's origin.
2819 snaplist_make(dsl_pool_t *dp,
2820 uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2822 uint64_t obj = last_obj;
2824 list_create(l, sizeof (struct promotenode),
2825 offsetof(struct promotenode, link));
2827 while (obj != first_obj) {
2829 struct promotenode *snap;
2832 err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
2833 ASSERT(err != ENOENT);
2838 first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
2840 snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
2842 list_insert_tail(l, snap);
2843 obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
2850 snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2852 struct promotenode *snap;
2855 for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2856 uint64_t used, comp, uncomp;
2857 dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2858 mintxg, UINT64_MAX, &used, &comp, &uncomp);
2865 snaplist_destroy(list_t *l, void *tag)
2867 struct promotenode *snap;
2869 if (l == NULL || !list_link_active(&l->list_head))
2872 while ((snap = list_tail(l)) != NULL) {
2873 list_remove(l, snap);
2874 dsl_dataset_rele(snap->ds, tag);
2875 kmem_free(snap, sizeof (*snap));
2881 promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2885 struct promotenode *snap;
2887 error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
2891 dd = ddpa->ddpa_clone->ds_dir;
2893 if (ddpa->ddpa_clone->ds_is_snapshot ||
2894 !dsl_dir_is_clone(dd)) {
2895 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2896 return (SET_ERROR(EINVAL));
2899 error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
2900 &ddpa->shared_snaps, tag);
2904 error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
2905 &ddpa->clone_snaps, tag);
2909 snap = list_head(&ddpa->shared_snaps);
2910 ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
2911 error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
2912 dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
2913 &ddpa->origin_snaps, tag);
2917 if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
2918 error = dsl_dataset_hold_obj(dp,
2919 dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
2920 tag, &ddpa->origin_origin);
2926 promote_rele(ddpa, tag);
2931 promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2933 snaplist_destroy(&ddpa->shared_snaps, tag);
2934 snaplist_destroy(&ddpa->clone_snaps, tag);
2935 snaplist_destroy(&ddpa->origin_snaps, tag);
2936 if (ddpa->origin_origin != NULL)
2937 dsl_dataset_rele(ddpa->origin_origin, tag);
2938 dsl_dataset_rele(ddpa->ddpa_clone, tag);
2944 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2945 * in with the name. (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.)
2948 dsl_dataset_promote(const char *name, char *conflsnap)
2950 dsl_dataset_promote_arg_t ddpa = { 0 };
2956 * We will modify space proportional to the number of
2957 * snapshots. Compute numsnaps.
2959 error = dmu_objset_hold(name, FTAG, &os);
2962 error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2963 dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
2965 dmu_objset_rele(os, FTAG);
2969 ddpa.ddpa_clonename = name;
2970 ddpa.err_ds = conflsnap;
2973 return (dsl_sync_task(name, dsl_dataset_promote_check,
2974 dsl_dataset_promote_sync, &ddpa,
2975 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
2979 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
2980 dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2983 * "slack" factor for received datasets with refquota set on them.
2984 * See the bottom of this function for details on its use.
2986 uint64_t refquota_slack = DMU_MAX_ACCESS * spa_asize_inflation;
2987 int64_t unused_refres_delta;
2989 /* they should both be heads */
2990 if (clone->ds_is_snapshot ||
2991 origin_head->ds_is_snapshot)
2992 return (SET_ERROR(EINVAL));
2994 /* if we are not forcing, the branch point should be just before them */
2995 if (!force && clone->ds_prev != origin_head->ds_prev)
2996 return (SET_ERROR(EINVAL));
2998 /* clone should be the clone (unless they are unrelated) */
2999 if (clone->ds_prev != NULL &&
3000 clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
3001 origin_head->ds_dir != clone->ds_prev->ds_dir)
3002 return (SET_ERROR(EINVAL));
3004 /* the clone should be a child of the origin */
3005 if (clone->ds_dir->dd_parent != origin_head->ds_dir)
3006 return (SET_ERROR(EINVAL));
3008 /* origin_head shouldn't be modified unless 'force' */
3010 dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
3011 return (SET_ERROR(ETXTBSY));
3013 /* origin_head should have no long holds (e.g. is not mounted) */
3014 if (dsl_dataset_handoff_check(origin_head, owner, tx))
3015 return (SET_ERROR(EBUSY));
3017 /* check amount of any unconsumed refreservation */
3018 unused_refres_delta =
3019 (int64_t)MIN(origin_head->ds_reserved,
3020 dsl_dataset_phys(origin_head)->ds_unique_bytes) -
3021 (int64_t)MIN(origin_head->ds_reserved,
3022 dsl_dataset_phys(clone)->ds_unique_bytes);
3024 if (unused_refres_delta > 0 &&
3025 unused_refres_delta >
3026 dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
3027 return (SET_ERROR(ENOSPC));
3030 * The clone can't be too much over the head's refquota.
3032 * To ensure that the entire refquota can be used, we allow one
3033 * transaction to exceed the the refquota. Therefore, this check
3034 * needs to also allow for the space referenced to be more than the
3035 * refquota. The maximum amount of space that one transaction can use
3036 * on disk is DMU_MAX_ACCESS * spa_asize_inflation. Allowing this
3037 * overage ensures that we are able to receive a filesystem that
3038 * exceeds the refquota on the source system.
3040 * So that overage is the refquota_slack we use below.
3042 if (origin_head->ds_quota != 0 &&
3043 dsl_dataset_phys(clone)->ds_referenced_bytes >
3044 origin_head->ds_quota + refquota_slack)
3045 return (SET_ERROR(EDQUOT));
3051 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
3052 dsl_dataset_t *origin_head, dmu_tx_t *tx)
3054 dsl_pool_t *dp = dmu_tx_pool(tx);
3055 int64_t unused_refres_delta;
3057 ASSERT(clone->ds_reserved == 0);
3059 * NOTE: On DEBUG kernels there could be a race between this and
3060 * the check function if spa_asize_inflation is adjusted...
3062 ASSERT(origin_head->ds_quota == 0 ||
3063 dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota +
3064 DMU_MAX_ACCESS * spa_asize_inflation);
3065 ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
3068 * Swap per-dataset feature flags.
3070 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3071 if (!(spa_feature_table[f].fi_flags &
3072 ZFEATURE_FLAG_PER_DATASET)) {
3073 ASSERT(!clone->ds_feature_inuse[f]);
3074 ASSERT(!origin_head->ds_feature_inuse[f]);
3078 boolean_t clone_inuse = clone->ds_feature_inuse[f];
3079 boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f];
3082 dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
3083 clone->ds_feature_inuse[f] = B_FALSE;
3085 if (origin_head_inuse) {
3086 dsl_dataset_deactivate_feature(origin_head->ds_object,
3088 origin_head->ds_feature_inuse[f] = B_FALSE;
3091 dsl_dataset_activate_feature(origin_head->ds_object,
3093 origin_head->ds_feature_inuse[f] = B_TRUE;
3095 if (origin_head_inuse) {
3096 dsl_dataset_activate_feature(clone->ds_object, f, tx);
3097 clone->ds_feature_inuse[f] = B_TRUE;
3101 dmu_buf_will_dirty(clone->ds_dbuf, tx);
3102 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3104 if (clone->ds_objset != NULL) {
3105 dmu_objset_evict(clone->ds_objset);
3106 clone->ds_objset = NULL;
3109 if (origin_head->ds_objset != NULL) {
3110 dmu_objset_evict(origin_head->ds_objset);
3111 origin_head->ds_objset = NULL;
3114 unused_refres_delta =
3115 (int64_t)MIN(origin_head->ds_reserved,
3116 dsl_dataset_phys(origin_head)->ds_unique_bytes) -
3117 (int64_t)MIN(origin_head->ds_reserved,
3118 dsl_dataset_phys(clone)->ds_unique_bytes);
3121 * Reset origin's unique bytes, if it exists.
3123 if (clone->ds_prev) {
3124 dsl_dataset_t *origin = clone->ds_prev;
3125 uint64_t comp, uncomp;
3127 dmu_buf_will_dirty(origin->ds_dbuf, tx);
3128 dsl_deadlist_space_range(&clone->ds_deadlist,
3129 dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
3130 &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
3135 rrw_enter(&clone->ds_bp_rwlock, RW_WRITER, FTAG);
3136 rrw_enter(&origin_head->ds_bp_rwlock, RW_WRITER, FTAG);
3138 tmp = dsl_dataset_phys(origin_head)->ds_bp;
3139 dsl_dataset_phys(origin_head)->ds_bp =
3140 dsl_dataset_phys(clone)->ds_bp;
3141 dsl_dataset_phys(clone)->ds_bp = tmp;
3142 rrw_exit(&origin_head->ds_bp_rwlock, FTAG);
3143 rrw_exit(&clone->ds_bp_rwlock, FTAG);
3146 /* set dd_*_bytes */
3148 int64_t dused, dcomp, duncomp;
3149 uint64_t cdl_used, cdl_comp, cdl_uncomp;
3150 uint64_t odl_used, odl_comp, odl_uncomp;
3152 ASSERT3U(dsl_dir_phys(clone->ds_dir)->
3153 dd_used_breakdown[DD_USED_SNAP], ==, 0);
3155 dsl_deadlist_space(&clone->ds_deadlist,
3156 &cdl_used, &cdl_comp, &cdl_uncomp);
3157 dsl_deadlist_space(&origin_head->ds_deadlist,
3158 &odl_used, &odl_comp, &odl_uncomp);
3160 dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
3162 (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
3164 dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
3166 (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
3168 duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
3170 (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
3173 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
3174 dused, dcomp, duncomp, tx);
3175 dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
3176 -dused, -dcomp, -duncomp, tx);
3179 * The difference in the space used by snapshots is the
3180 * difference in snapshot space due to the head's
3181 * deadlist (since that's the only thing that's
3182 * changing that affects the snapused).
3184 dsl_deadlist_space_range(&clone->ds_deadlist,
3185 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3186 &cdl_used, &cdl_comp, &cdl_uncomp);
3187 dsl_deadlist_space_range(&origin_head->ds_deadlist,
3188 origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3189 &odl_used, &odl_comp, &odl_uncomp);
3190 dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
3191 DD_USED_HEAD, DD_USED_SNAP, NULL);
3194 /* swap ds_*_bytes */
3195 SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
3196 dsl_dataset_phys(clone)->ds_referenced_bytes);
3197 SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
3198 dsl_dataset_phys(clone)->ds_compressed_bytes);
3199 SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
3200 dsl_dataset_phys(clone)->ds_uncompressed_bytes);
3201 SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
3202 dsl_dataset_phys(clone)->ds_unique_bytes);
3204 /* apply any parent delta for change in unconsumed refreservation */
3205 dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
3206 unused_refres_delta, 0, 0, tx);
3211 dsl_deadlist_close(&clone->ds_deadlist);
3212 dsl_deadlist_close(&origin_head->ds_deadlist);
3213 SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
3214 dsl_dataset_phys(clone)->ds_deadlist_obj);
3215 dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
3216 dsl_dataset_phys(clone)->ds_deadlist_obj);
3217 dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
3218 dsl_dataset_phys(origin_head)->ds_deadlist_obj);
3220 dsl_scan_ds_clone_swapped(origin_head, clone, tx);
3222 spa_history_log_internal_ds(clone, "clone swap", tx,
3223 "parent=%s", origin_head->ds_dir->dd_myname);
3227 * Given a pool name and a dataset object number in that pool,
3228 * return the name of that dataset.
3231 dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
3237 error = dsl_pool_hold(pname, FTAG, &dp);
3241 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
3243 dsl_dataset_name(ds, buf);
3244 dsl_dataset_rele(ds, FTAG);
3246 dsl_pool_rele(dp, FTAG);
3252 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
3253 uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
3257 ASSERT3S(asize, >, 0);
3260 * *ref_rsrv is the portion of asize that will come from any
3261 * unconsumed refreservation space.
3265 mutex_enter(&ds->ds_lock);
3267 * Make a space adjustment for reserved bytes.
3269 if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
3271 ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
3273 (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
3275 asize - MIN(asize, parent_delta(ds, asize + inflight));
3278 if (!check_quota || ds->ds_quota == 0) {
3279 mutex_exit(&ds->ds_lock);
3283 * If they are requesting more space, and our current estimate
3284 * is over quota, they get to try again unless the actual
3285 * on-disk is over quota and there are no pending changes (which
3286 * may free up space for us).
3288 if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
3291 dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
3292 error = SET_ERROR(ERESTART);
3294 error = SET_ERROR(EDQUOT);
3296 mutex_exit(&ds->ds_lock);
3301 typedef struct dsl_dataset_set_qr_arg {
3302 const char *ddsqra_name;
3303 zprop_source_t ddsqra_source;
3304 uint64_t ddsqra_value;
3305 } dsl_dataset_set_qr_arg_t;
3310 dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
3312 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3313 dsl_pool_t *dp = dmu_tx_pool(tx);
3318 if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
3319 return (SET_ERROR(ENOTSUP));
3321 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
3325 if (ds->ds_is_snapshot) {
3326 dsl_dataset_rele(ds, FTAG);
3327 return (SET_ERROR(EINVAL));
3330 error = dsl_prop_predict(ds->ds_dir,
3331 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
3332 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
3334 dsl_dataset_rele(ds, FTAG);
3339 dsl_dataset_rele(ds, FTAG);
3343 if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
3344 newval < ds->ds_reserved) {
3345 dsl_dataset_rele(ds, FTAG);
3346 return (SET_ERROR(ENOSPC));
3349 dsl_dataset_rele(ds, FTAG);
3354 dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
3356 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3357 dsl_pool_t *dp = dmu_tx_pool(tx);
3361 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
3363 dsl_prop_set_sync_impl(ds,
3364 zfs_prop_to_name(ZFS_PROP_REFQUOTA),
3365 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
3366 &ddsqra->ddsqra_value, tx);
3368 VERIFY0(dsl_prop_get_int_ds(ds,
3369 zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
3371 if (ds->ds_quota != newval) {
3372 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3373 ds->ds_quota = newval;
3375 dsl_dataset_rele(ds, FTAG);
3379 dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
3382 dsl_dataset_set_qr_arg_t ddsqra;
3384 ddsqra.ddsqra_name = dsname;
3385 ddsqra.ddsqra_source = source;
3386 ddsqra.ddsqra_value = refquota;
3388 return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
3389 dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
3393 dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
3395 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3396 dsl_pool_t *dp = dmu_tx_pool(tx);
3399 uint64_t newval, unique;
3401 if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
3402 return (SET_ERROR(ENOTSUP));
3404 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
3408 if (ds->ds_is_snapshot) {
3409 dsl_dataset_rele(ds, FTAG);
3410 return (SET_ERROR(EINVAL));
3413 error = dsl_prop_predict(ds->ds_dir,
3414 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
3415 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
3417 dsl_dataset_rele(ds, FTAG);
3422 * If we are doing the preliminary check in open context, the
3423 * space estimates may be inaccurate.
3425 if (!dmu_tx_is_syncing(tx)) {
3426 dsl_dataset_rele(ds, FTAG);
3430 mutex_enter(&ds->ds_lock);
3431 if (!DS_UNIQUE_IS_ACCURATE(ds))
3432 dsl_dataset_recalc_head_uniq(ds);
3433 unique = dsl_dataset_phys(ds)->ds_unique_bytes;
3434 mutex_exit(&ds->ds_lock);
3436 if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
3437 uint64_t delta = MAX(unique, newval) -
3438 MAX(unique, ds->ds_reserved);
3441 dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
3442 (ds->ds_quota > 0 && newval > ds->ds_quota)) {
3443 dsl_dataset_rele(ds, FTAG);
3444 return (SET_ERROR(ENOSPC));
3448 dsl_dataset_rele(ds, FTAG);
3453 dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
3454 zprop_source_t source, uint64_t value, dmu_tx_t *tx)
3460 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
3461 source, sizeof (value), 1, &value, tx);
3463 VERIFY0(dsl_prop_get_int_ds(ds,
3464 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
3466 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3467 mutex_enter(&ds->ds_dir->dd_lock);
3468 mutex_enter(&ds->ds_lock);
3469 ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3470 unique = dsl_dataset_phys(ds)->ds_unique_bytes;
3471 delta = MAX(0, (int64_t)(newval - unique)) -
3472 MAX(0, (int64_t)(ds->ds_reserved - unique));
3473 ds->ds_reserved = newval;
3474 mutex_exit(&ds->ds_lock);
3476 dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
3477 mutex_exit(&ds->ds_dir->dd_lock);
3481 dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
3483 dsl_dataset_set_qr_arg_t *ddsqra = arg;
3484 dsl_pool_t *dp = dmu_tx_pool(tx);
3487 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
3488 dsl_dataset_set_refreservation_sync_impl(ds,
3489 ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
3490 dsl_dataset_rele(ds, FTAG);
3494 dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
3495 uint64_t refreservation)
3497 dsl_dataset_set_qr_arg_t ddsqra;
3499 ddsqra.ddsqra_name = dsname;
3500 ddsqra.ddsqra_source = source;
3501 ddsqra.ddsqra_value = refreservation;
3503 return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
3504 dsl_dataset_set_refreservation_sync, &ddsqra,
3505 0, ZFS_SPACE_CHECK_NONE));
3509 * Return (in *usedp) the amount of space written in new that is not
3510 * present in oldsnap. New may be a snapshot or the head. Old must be
3511 * a snapshot before new, in new's filesystem (or its origin). If not then
3512 * fail and return EINVAL.
3514 * The written space is calculated by considering two components: First, we
3515 * ignore any freed space, and calculate the written as new's used space
3516 * minus old's used space. Next, we add in the amount of space that was freed
3517 * between the two snapshots, thus reducing new's used space relative to old's.
3518 * Specifically, this is the space that was born before old->ds_creation_txg,
3519 * and freed before new (ie. on new's deadlist or a previous deadlist).
3521 * space freed [---------------------]
3522 * snapshots ---O-------O--------O-------O------
3526 dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
3527 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
3531 dsl_pool_t *dp = new->ds_dir->dd_pool;
3533 ASSERT(dsl_pool_config_held(dp));
3536 *usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
3537 *usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes;
3540 *compp += dsl_dataset_phys(new)->ds_compressed_bytes;
3541 *compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes;
3544 *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
3545 *uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes;
3547 snapobj = new->ds_object;
3548 while (snapobj != oldsnap->ds_object) {
3549 dsl_dataset_t *snap;
3550 uint64_t used, comp, uncomp;
3552 if (snapobj == new->ds_object) {
3555 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
3560 if (dsl_dataset_phys(snap)->ds_prev_snap_txg ==
3561 dsl_dataset_phys(oldsnap)->ds_creation_txg) {
3563 * The blocks in the deadlist can not be born after
3564 * ds_prev_snap_txg, so get the whole deadlist space,
3565 * which is more efficient (especially for old-format
3566 * deadlists). Unfortunately the deadlist code
3567 * doesn't have enough information to make this
3568 * optimization itself.
3570 dsl_deadlist_space(&snap->ds_deadlist,
3571 &used, &comp, &uncomp);
3573 dsl_deadlist_space_range(&snap->ds_deadlist,
3574 0, dsl_dataset_phys(oldsnap)->ds_creation_txg,
3575 &used, &comp, &uncomp);
3582 * If we get to the beginning of the chain of snapshots
3583 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
3584 * was not a snapshot of/before new.
3586 snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3588 dsl_dataset_rele(snap, FTAG);
3590 err = SET_ERROR(EINVAL);
3599 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
3600 * lastsnap, and all snapshots in between are deleted.
3602 * blocks that would be freed [---------------------------]
3603 * snapshots ---O-------O--------O-------O--------O
3604 * firstsnap lastsnap
3606 * This is the set of blocks that were born after the snap before firstsnap,
3607 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
3608 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
3609 * We calculate this by iterating over the relevant deadlists (from the snap
3610 * after lastsnap, backward to the snap after firstsnap), summing up the
3611 * space on the deadlist that was born after the snap before firstsnap.
3614 dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
3615 dsl_dataset_t *lastsnap,
3616 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
3620 dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
3622 ASSERT(firstsnap->ds_is_snapshot);
3623 ASSERT(lastsnap->ds_is_snapshot);
3626 * Check that the snapshots are in the same dsl_dir, and firstsnap
3627 * is before lastsnap.
3629 if (firstsnap->ds_dir != lastsnap->ds_dir ||
3630 dsl_dataset_phys(firstsnap)->ds_creation_txg >
3631 dsl_dataset_phys(lastsnap)->ds_creation_txg)
3632 return (SET_ERROR(EINVAL));
3634 *usedp = *compp = *uncompp = 0;
3636 snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
3637 while (snapobj != firstsnap->ds_object) {
3639 uint64_t used, comp, uncomp;
3641 err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
3645 dsl_deadlist_space_range(&ds->ds_deadlist,
3646 dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
3647 &used, &comp, &uncomp);
3652 snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
3653 ASSERT3U(snapobj, !=, 0);
3654 dsl_dataset_rele(ds, FTAG);
3660 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
3661 * For example, they could both be snapshots of the same filesystem, and
3662 * 'earlier' is before 'later'. Or 'earlier' could be the origin of
3663 * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's
3664 * filesystem. Or 'earlier' could be the origin's origin.
3666 * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
3669 dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
3670 uint64_t earlier_txg)
3672 dsl_pool_t *dp = later->ds_dir->dd_pool;
3676 ASSERT(dsl_pool_config_held(dp));
3677 ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
3679 if (earlier_txg == 0)
3680 earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
3682 if (later->ds_is_snapshot &&
3683 earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
3686 if (later->ds_dir == earlier->ds_dir)
3688 if (!dsl_dir_is_clone(later->ds_dir))
3691 if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object)
3693 dsl_dataset_t *origin;
3694 error = dsl_dataset_hold_obj(dp,
3695 dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
3698 ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
3699 dsl_dataset_rele(origin, FTAG);
3704 dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
3706 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3707 dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
3711 dsl_dataset_is_zapified(dsl_dataset_t *ds)
3713 dmu_object_info_t doi;
3715 dmu_object_info_from_db(ds->ds_dbuf, &doi);
3716 return (doi.doi_type == DMU_OTN_ZAP_METADATA);
3720 dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds)
3722 return (dsl_dataset_is_zapified(ds) &&
3723 zap_contains(ds->ds_dir->dd_pool->dp_meta_objset,
3724 ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0);