xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision 7077:a63bdc0b8352)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
225831Sck153898  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/dmu_objset.h>
29789Sahrens #include <sys/dsl_dataset.h>
30789Sahrens #include <sys/dsl_dir.h>
312082Seschrock #include <sys/dsl_prop.h>
322199Sahrens #include <sys/dsl_synctask.h>
33789Sahrens #include <sys/dmu_traverse.h>
34789Sahrens #include <sys/dmu_tx.h>
35789Sahrens #include <sys/arc.h>
36789Sahrens #include <sys/zio.h>
37789Sahrens #include <sys/zap.h>
38789Sahrens #include <sys/unique.h>
39789Sahrens #include <sys/zfs_context.h>
404007Smmusante #include <sys/zfs_ioctl.h>
414543Smarks #include <sys/spa.h>
427046Sahrens #include <sys/zfs_znode.h>
434543Smarks #include <sys/sunddi.h>
44789Sahrens 
456689Smaybee static char *dsl_reaper = "the grim reaper";
466689Smaybee 
472199Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check;
482199Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync;
492199Sahrens static dsl_checkfunc_t dsl_dataset_rollback_check;
502199Sahrens static dsl_syncfunc_t dsl_dataset_rollback_sync;
515378Sck153898 static dsl_syncfunc_t dsl_dataset_set_reservation_sync;
521731Sbonwick 
533444Sek110237 #define	DS_REF_MAX	(1ULL << 62)
54789Sahrens 
55789Sahrens #define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
56789Sahrens 
576689Smaybee #define	DSL_DATASET_IS_DESTROYED(ds)	((ds)->ds_owner == dsl_reaper)
586689Smaybee 
59789Sahrens 
605378Sck153898 /*
615378Sck153898  * Figure out how much of this delta should be propogated to the dsl_dir
625378Sck153898  * layer.  If there's a refreservation, that space has already been
635378Sck153898  * partially accounted for in our ancestors.
645378Sck153898  */
655378Sck153898 static int64_t
665378Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta)
675378Sck153898 {
685378Sck153898 	uint64_t old_bytes, new_bytes;
695378Sck153898 
705378Sck153898 	if (ds->ds_reserved == 0)
715378Sck153898 		return (delta);
725378Sck153898 
735378Sck153898 	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
745378Sck153898 	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
755378Sck153898 
765378Sck153898 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
775378Sck153898 	return (new_bytes - old_bytes);
785378Sck153898 }
79789Sahrens 
80789Sahrens void
81789Sahrens dsl_dataset_block_born(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
82789Sahrens {
832082Seschrock 	int used = bp_get_dasize(tx->tx_pool->dp_spa, bp);
84789Sahrens 	int compressed = BP_GET_PSIZE(bp);
85789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
865378Sck153898 	int64_t delta;
87789Sahrens 
88789Sahrens 	dprintf_bp(bp, "born, ds=%p\n", ds);
89789Sahrens 
90789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
91789Sahrens 	/* It could have been compressed away to nothing */
92789Sahrens 	if (BP_IS_HOLE(bp))
93789Sahrens 		return;
94789Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
95789Sahrens 	ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES);
96789Sahrens 	if (ds == NULL) {
97789Sahrens 		/*
98789Sahrens 		 * Account for the meta-objset space in its placeholder
99789Sahrens 		 * dsl_dir.
100789Sahrens 		 */
101789Sahrens 		ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */
102789Sahrens 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir,
103789Sahrens 		    used, compressed, uncompressed, tx);
104789Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
105789Sahrens 		return;
106789Sahrens 	}
107789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
108789Sahrens 	mutex_enter(&ds->ds_lock);
1095378Sck153898 	delta = parent_delta(ds, used);
110789Sahrens 	ds->ds_phys->ds_used_bytes += used;
111789Sahrens 	ds->ds_phys->ds_compressed_bytes += compressed;
112789Sahrens 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
113789Sahrens 	ds->ds_phys->ds_unique_bytes += used;
114789Sahrens 	mutex_exit(&ds->ds_lock);
1155378Sck153898 	dsl_dir_diduse_space(ds->ds_dir, delta, compressed, uncompressed, tx);
116789Sahrens }
117789Sahrens 
1186992Smaybee int
1193547Smaybee dsl_dataset_block_kill(dsl_dataset_t *ds, blkptr_t *bp, zio_t *pio,
1203547Smaybee     dmu_tx_t *tx)
121789Sahrens {
1222082Seschrock 	int used = bp_get_dasize(tx->tx_pool->dp_spa, bp);
123789Sahrens 	int compressed = BP_GET_PSIZE(bp);
124789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
125789Sahrens 
126789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1273547Smaybee 	/* No block pointer => nothing to free */
128789Sahrens 	if (BP_IS_HOLE(bp))
1296992Smaybee 		return (0);
130789Sahrens 
131789Sahrens 	ASSERT(used > 0);
132789Sahrens 	if (ds == NULL) {
1333547Smaybee 		int err;
134789Sahrens 		/*
135789Sahrens 		 * Account for the meta-objset space in its placeholder
136789Sahrens 		 * dataset.
137789Sahrens 		 */
1387046Sahrens 		err = dsl_free(pio, tx->tx_pool,
1393547Smaybee 		    tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT);
1403547Smaybee 		ASSERT(err == 0);
141789Sahrens 
142789Sahrens 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir,
143789Sahrens 		    -used, -compressed, -uncompressed, tx);
144789Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
1456992Smaybee 		return (used);
146789Sahrens 	}
147789Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
148789Sahrens 
149789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
150789Sahrens 
151789Sahrens 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
1523547Smaybee 		int err;
1535378Sck153898 		int64_t delta;
1543547Smaybee 
155789Sahrens 		dprintf_bp(bp, "freeing: %s", "");
1567046Sahrens 		err = dsl_free(pio, tx->tx_pool,
1573547Smaybee 		    tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT);
1583547Smaybee 		ASSERT(err == 0);
159789Sahrens 
160789Sahrens 		mutex_enter(&ds->ds_lock);
1615378Sck153898 		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
1625378Sck153898 		    !DS_UNIQUE_IS_ACCURATE(ds));
1635378Sck153898 		delta = parent_delta(ds, -used);
164789Sahrens 		ds->ds_phys->ds_unique_bytes -= used;
165789Sahrens 		mutex_exit(&ds->ds_lock);
166789Sahrens 		dsl_dir_diduse_space(ds->ds_dir,
1675378Sck153898 		    delta, -compressed, -uncompressed, tx);
168789Sahrens 	} else {
169789Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
1701544Seschrock 		VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx));
1715712Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
1725712Sahrens 		    ds->ds_phys->ds_prev_snap_obj);
1735712Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
174789Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
1755712Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
1765712Sahrens 		    ds->ds_object && bp->blk_birth >
1775712Sahrens 		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
1785712Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1795712Sahrens 			mutex_enter(&ds->ds_prev->ds_lock);
1805712Sahrens 			ds->ds_prev->ds_phys->ds_unique_bytes += used;
1815712Sahrens 			mutex_exit(&ds->ds_prev->ds_lock);
182789Sahrens 		}
183789Sahrens 	}
184789Sahrens 	mutex_enter(&ds->ds_lock);
185789Sahrens 	ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used);
186789Sahrens 	ds->ds_phys->ds_used_bytes -= used;
187789Sahrens 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
188789Sahrens 	ds->ds_phys->ds_compressed_bytes -= compressed;
189789Sahrens 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
190789Sahrens 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
191789Sahrens 	mutex_exit(&ds->ds_lock);
1926992Smaybee 
1936992Smaybee 	return (used);
194789Sahrens }
195789Sahrens 
1961544Seschrock uint64_t
1971544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
198789Sahrens {
1992885Sahrens 	uint64_t trysnap = 0;
2002885Sahrens 
201789Sahrens 	if (ds == NULL)
2021544Seschrock 		return (0);
203789Sahrens 	/*
204789Sahrens 	 * The snapshot creation could fail, but that would cause an
205789Sahrens 	 * incorrect FALSE return, which would only result in an
206789Sahrens 	 * overestimation of the amount of space that an operation would
207789Sahrens 	 * consume, which is OK.
208789Sahrens 	 *
209789Sahrens 	 * There's also a small window where we could miss a pending
210789Sahrens 	 * snapshot, because we could set the sync task in the quiescing
211789Sahrens 	 * phase.  So this should only be used as a guess.
212789Sahrens 	 */
2132885Sahrens 	if (ds->ds_trysnap_txg >
2142885Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
2152885Sahrens 		trysnap = ds->ds_trysnap_txg;
2162885Sahrens 	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
2171544Seschrock }
2181544Seschrock 
2191544Seschrock int
2201544Seschrock dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth)
2211544Seschrock {
2221544Seschrock 	return (blk_birth > dsl_dataset_prev_snap_txg(ds));
223789Sahrens }
224789Sahrens 
225789Sahrens /* ARGSUSED */
226789Sahrens static void
227789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv)
228789Sahrens {
229789Sahrens 	dsl_dataset_t *ds = dsv;
230789Sahrens 
2316689Smaybee 	ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds));
232789Sahrens 
233789Sahrens 	dprintf_ds(ds, "evicting %s\n", "");
234789Sahrens 
2354787Sahrens 	unique_remove(ds->ds_fsid_guid);
236789Sahrens 
237789Sahrens 	if (ds->ds_user_ptr != NULL)
238789Sahrens 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
239789Sahrens 
240789Sahrens 	if (ds->ds_prev) {
2416689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
242789Sahrens 		ds->ds_prev = NULL;
243789Sahrens 	}
244789Sahrens 
245789Sahrens 	bplist_close(&ds->ds_deadlist);
2466689Smaybee 	if (ds->ds_dir)
2476689Smaybee 		dsl_dir_close(ds->ds_dir, ds);
248789Sahrens 
2494787Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
250789Sahrens 
2512856Snd150628 	mutex_destroy(&ds->ds_lock);
2524787Sahrens 	mutex_destroy(&ds->ds_opening_lock);
2532856Snd150628 	mutex_destroy(&ds->ds_deadlist.bpl_lock);
2546689Smaybee 	rw_destroy(&ds->ds_rwlock);
2556689Smaybee 	cv_destroy(&ds->ds_exclusive_cv);
2562856Snd150628 
257789Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
258789Sahrens }
259789Sahrens 
2601544Seschrock static int
261789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
262789Sahrens {
263789Sahrens 	dsl_dataset_phys_t *headphys;
264789Sahrens 	int err;
265789Sahrens 	dmu_buf_t *headdbuf;
266789Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
267789Sahrens 	objset_t *mos = dp->dp_meta_objset;
268789Sahrens 
269789Sahrens 	if (ds->ds_snapname[0])
2701544Seschrock 		return (0);
271789Sahrens 	if (ds->ds_phys->ds_next_snap_obj == 0)
2721544Seschrock 		return (0);
273789Sahrens 
2741544Seschrock 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
2751544Seschrock 	    FTAG, &headdbuf);
2761544Seschrock 	if (err)
2771544Seschrock 		return (err);
278789Sahrens 	headphys = headdbuf->db_data;
279789Sahrens 	err = zap_value_search(dp->dp_meta_objset,
2804577Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
2811544Seschrock 	dmu_buf_rele(headdbuf, FTAG);
2821544Seschrock 	return (err);
283789Sahrens }
284789Sahrens 
2856492Stimh static int
2866689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
2876492Stimh {
2886689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
2896689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
2906492Stimh 	matchtype_t mt;
2916492Stimh 	int err;
2926492Stimh 
2936689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
2946492Stimh 		mt = MT_FIRST;
2956492Stimh 	else
2966492Stimh 		mt = MT_EXACT;
2976492Stimh 
2986689Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
2996492Stimh 	    value, mt, NULL, 0, NULL);
3006492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
3016689Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
3026492Stimh 	return (err);
3036492Stimh }
3046492Stimh 
3056492Stimh static int
3066689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx)
3076492Stimh {
3086689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3096689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
3106492Stimh 	matchtype_t mt;
3116492Stimh 	int err;
3126492Stimh 
3136689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
3146492Stimh 		mt = MT_FIRST;
3156492Stimh 	else
3166492Stimh 		mt = MT_EXACT;
3176492Stimh 
3186689Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
3196492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
3206689Smaybee 		err = zap_remove(mos, snapobj, name, tx);
3216492Stimh 	return (err);
3226492Stimh }
3236492Stimh 
3246689Smaybee static int
3256689Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag,
3266689Smaybee     dsl_dataset_t **dsp)
327789Sahrens {
328789Sahrens 	objset_t *mos = dp->dp_meta_objset;
329789Sahrens 	dmu_buf_t *dbuf;
330789Sahrens 	dsl_dataset_t *ds;
3311544Seschrock 	int err;
332789Sahrens 
333789Sahrens 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
334789Sahrens 	    dsl_pool_sync_context(dp));
335789Sahrens 
3361544Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
3371544Seschrock 	if (err)
3381544Seschrock 		return (err);
339789Sahrens 	ds = dmu_buf_get_user(dbuf);
340789Sahrens 	if (ds == NULL) {
341789Sahrens 		dsl_dataset_t *winner;
342789Sahrens 
343789Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
344789Sahrens 		ds->ds_dbuf = dbuf;
345789Sahrens 		ds->ds_object = dsobj;
346789Sahrens 		ds->ds_phys = dbuf->db_data;
347789Sahrens 
3482856Snd150628 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
3494787Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
3502856Snd150628 		mutex_init(&ds->ds_deadlist.bpl_lock, NULL, MUTEX_DEFAULT,
3512856Snd150628 		    NULL);
3526689Smaybee 		rw_init(&ds->ds_rwlock, 0, 0, 0);
3536689Smaybee 		cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL);
3542856Snd150628 
3551544Seschrock 		err = bplist_open(&ds->ds_deadlist,
356789Sahrens 		    mos, ds->ds_phys->ds_deadlist_obj);
3571544Seschrock 		if (err == 0) {
3581544Seschrock 			err = dsl_dir_open_obj(dp,
3591544Seschrock 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
3601544Seschrock 		}
3611544Seschrock 		if (err) {
3621544Seschrock 			/*
3631544Seschrock 			 * we don't really need to close the blist if we
3641544Seschrock 			 * just opened it.
3651544Seschrock 			 */
3662856Snd150628 			mutex_destroy(&ds->ds_lock);
3674787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
3682856Snd150628 			mutex_destroy(&ds->ds_deadlist.bpl_lock);
3696689Smaybee 			rw_destroy(&ds->ds_rwlock);
3706689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
3711544Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
3721544Seschrock 			dmu_buf_rele(dbuf, tag);
3731544Seschrock 			return (err);
3741544Seschrock 		}
375789Sahrens 
376789Sahrens 		if (ds->ds_dir->dd_phys->dd_head_dataset_obj == dsobj) {
377789Sahrens 			ds->ds_snapname[0] = '\0';
378789Sahrens 			if (ds->ds_phys->ds_prev_snap_obj) {
3796689Smaybee 				err = dsl_dataset_get_ref(dp,
3806689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
3816689Smaybee 				    ds, &ds->ds_prev);
382789Sahrens 			}
3836689Smaybee 		} else if (zfs_flags & ZFS_DEBUG_SNAPNAMES) {
3846689Smaybee 			err = dsl_dataset_get_snapname(ds);
385789Sahrens 		}
386789Sahrens 
3875475Sck153898 		if (!dsl_dataset_is_snapshot(ds)) {
3885569Sck153898 			/*
3895569Sck153898 			 * In sync context, we're called with either no lock
3905569Sck153898 			 * or with the write lock.  If we're not syncing,
3915569Sck153898 			 * we're always called with the read lock held.
3925569Sck153898 			 */
3935475Sck153898 			boolean_t need_lock =
3945569Sck153898 			    !RW_WRITE_HELD(&dp->dp_config_rwlock) &&
3955569Sck153898 			    dsl_pool_sync_context(dp);
3965475Sck153898 
3975475Sck153898 			if (need_lock)
3985475Sck153898 				rw_enter(&dp->dp_config_rwlock, RW_READER);
3995475Sck153898 
4005475Sck153898 			err = dsl_prop_get_ds_locked(ds->ds_dir,
4015475Sck153898 			    "refreservation", sizeof (uint64_t), 1,
4025475Sck153898 			    &ds->ds_reserved, NULL);
4035475Sck153898 			if (err == 0) {
4045475Sck153898 				err = dsl_prop_get_ds_locked(ds->ds_dir,
4055475Sck153898 				    "refquota", sizeof (uint64_t), 1,
4065475Sck153898 				    &ds->ds_quota, NULL);
4075475Sck153898 			}
4085475Sck153898 
4095475Sck153898 			if (need_lock)
4105475Sck153898 				rw_exit(&dp->dp_config_rwlock);
4115475Sck153898 		} else {
4125475Sck153898 			ds->ds_reserved = ds->ds_quota = 0;
4135475Sck153898 		}
4145475Sck153898 
4151544Seschrock 		if (err == 0) {
4161544Seschrock 			winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys,
4171544Seschrock 			    dsl_dataset_evict);
4181544Seschrock 		}
4191544Seschrock 		if (err || winner) {
420789Sahrens 			bplist_close(&ds->ds_deadlist);
4216689Smaybee 			if (ds->ds_prev)
4226689Smaybee 				dsl_dataset_drop_ref(ds->ds_prev, ds);
423789Sahrens 			dsl_dir_close(ds->ds_dir, ds);
4242856Snd150628 			mutex_destroy(&ds->ds_lock);
4254787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
4262856Snd150628 			mutex_destroy(&ds->ds_deadlist.bpl_lock);
4276689Smaybee 			rw_destroy(&ds->ds_rwlock);
4286689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
429789Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
4301544Seschrock 			if (err) {
4311544Seschrock 				dmu_buf_rele(dbuf, tag);
4321544Seschrock 				return (err);
4331544Seschrock 			}
434789Sahrens 			ds = winner;
435789Sahrens 		} else {
4364787Sahrens 			ds->ds_fsid_guid =
437789Sahrens 			    unique_insert(ds->ds_phys->ds_fsid_guid);
438789Sahrens 		}
439789Sahrens 	}
440789Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
441789Sahrens 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
4427046Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
4437061Sahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
444*7077Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
445789Sahrens 	mutex_enter(&ds->ds_lock);
4466689Smaybee 	if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) {
447789Sahrens 		mutex_exit(&ds->ds_lock);
4486689Smaybee 		dmu_buf_rele(ds->ds_dbuf, tag);
4496689Smaybee 		return (ENOENT);
4506689Smaybee 	}
4516689Smaybee 	mutex_exit(&ds->ds_lock);
4526689Smaybee 	*dsp = ds;
4536689Smaybee 	return (0);
4546689Smaybee }
4556689Smaybee 
4566689Smaybee static int
4576689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag)
4586689Smaybee {
4596689Smaybee 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
4606689Smaybee 
4616689Smaybee 	/*
4626689Smaybee 	 * In syncing context we don't want the rwlock lock: there
4636689Smaybee 	 * may be an existing writer waiting for sync phase to
4646689Smaybee 	 * finish.  We don't need to worry about such writers, since
4656689Smaybee 	 * sync phase is single-threaded, so the writer can't be
4666689Smaybee 	 * doing anything while we are active.
4676689Smaybee 	 */
4686689Smaybee 	if (dsl_pool_sync_context(dp)) {
4696689Smaybee 		ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
4706689Smaybee 		return (0);
471789Sahrens 	}
4726689Smaybee 
4736689Smaybee 	/*
4746689Smaybee 	 * Normal users will hold the ds_rwlock as a READER until they
4756689Smaybee 	 * are finished (i.e., call dsl_dataset_rele()).  "Owners" will
4766689Smaybee 	 * drop their READER lock after they set the ds_owner field.
4776689Smaybee 	 *
4786689Smaybee 	 * If the dataset is being destroyed, the destroy thread will
4796689Smaybee 	 * obtain a WRITER lock for exclusive access after it's done its
4806689Smaybee 	 * open-context work and then change the ds_owner to
4816689Smaybee 	 * dsl_reaper once destruction is assured.  So threads
4826689Smaybee 	 * may block here temporarily, until the "destructability" of
4836689Smaybee 	 * the dataset is determined.
4846689Smaybee 	 */
4856689Smaybee 	ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock));
4866689Smaybee 	mutex_enter(&ds->ds_lock);
4876689Smaybee 	while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) {
4886689Smaybee 		rw_exit(&dp->dp_config_rwlock);
4896689Smaybee 		cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock);
4906689Smaybee 		if (DSL_DATASET_IS_DESTROYED(ds)) {
4916689Smaybee 			mutex_exit(&ds->ds_lock);
4926689Smaybee 			dsl_dataset_drop_ref(ds, tag);
4936689Smaybee 			rw_enter(&dp->dp_config_rwlock, RW_READER);
4946689Smaybee 			return (ENOENT);
4956689Smaybee 		}
4966689Smaybee 		rw_enter(&dp->dp_config_rwlock, RW_READER);
4976689Smaybee 	}
498789Sahrens 	mutex_exit(&ds->ds_lock);
4991544Seschrock 	return (0);
500789Sahrens }
501789Sahrens 
502789Sahrens int
5036689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
5046689Smaybee     dsl_dataset_t **dsp)
5056689Smaybee {
5066689Smaybee 	int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp);
5076689Smaybee 
5086689Smaybee 	if (err)
5096689Smaybee 		return (err);
5106689Smaybee 	return (dsl_dataset_hold_ref(*dsp, tag));
5116689Smaybee }
5126689Smaybee 
5136689Smaybee int
5146689Smaybee dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, int flags, void *owner,
5156689Smaybee     dsl_dataset_t **dsp)
5166689Smaybee {
5176689Smaybee 	int err = dsl_dataset_hold_obj(dp, dsobj, owner, dsp);
5186689Smaybee 
5196689Smaybee 	ASSERT(DS_MODE_TYPE(flags) != DS_MODE_USER);
5206689Smaybee 
5216689Smaybee 	if (err)
5226689Smaybee 		return (err);
5236689Smaybee 	if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) {
5246689Smaybee 		dsl_dataset_rele(*dsp, owner);
5256689Smaybee 		return (EBUSY);
5266689Smaybee 	}
5276689Smaybee 	return (0);
5286689Smaybee }
5296689Smaybee 
5306689Smaybee int
5316689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp)
532789Sahrens {
533789Sahrens 	dsl_dir_t *dd;
534789Sahrens 	dsl_pool_t *dp;
5356689Smaybee 	const char *snapname;
536789Sahrens 	uint64_t obj;
537789Sahrens 	int err = 0;
538789Sahrens 
5396689Smaybee 	err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname);
5401544Seschrock 	if (err)
5411544Seschrock 		return (err);
542789Sahrens 
543789Sahrens 	dp = dd->dd_pool;
544789Sahrens 	obj = dd->dd_phys->dd_head_dataset_obj;
545789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
5466689Smaybee 	if (obj)
5476689Smaybee 		err = dsl_dataset_get_ref(dp, obj, tag, dsp);
5486689Smaybee 	else
549789Sahrens 		err = ENOENT;
5506689Smaybee 	if (err)
551789Sahrens 		goto out;
5526689Smaybee 
5536689Smaybee 	err = dsl_dataset_hold_ref(*dsp, tag);
5546689Smaybee 
5556689Smaybee 	/* we may be looking for a snapshot */
5566689Smaybee 	if (err == 0 && snapname != NULL) {
5576689Smaybee 		dsl_dataset_t *ds = NULL;
5586689Smaybee 
5596689Smaybee 		if (*snapname++ != '@') {
5606689Smaybee 			dsl_dataset_rele(*dsp, tag);
561789Sahrens 			err = ENOENT;
562789Sahrens 			goto out;
563789Sahrens 		}
5646689Smaybee 
5656689Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
5666689Smaybee 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
5676689Smaybee 		if (err == 0)
5686689Smaybee 			err = dsl_dataset_get_ref(dp, obj, tag, &ds);
5696689Smaybee 		dsl_dataset_rele(*dsp, tag);
5706689Smaybee 
5716689Smaybee 		ASSERT3U((err == 0), ==, (ds != NULL));
5726689Smaybee 
5736689Smaybee 		if (ds) {
5746689Smaybee 			mutex_enter(&ds->ds_lock);
5756689Smaybee 			if (ds->ds_snapname[0] == 0)
5766689Smaybee 				(void) strlcpy(ds->ds_snapname, snapname,
5776689Smaybee 				    sizeof (ds->ds_snapname));
5786689Smaybee 			mutex_exit(&ds->ds_lock);
5796689Smaybee 			err = dsl_dataset_hold_ref(ds, tag);
5806689Smaybee 			*dsp = err ? NULL : ds;
581789Sahrens 		}
582789Sahrens 	}
583789Sahrens out:
584789Sahrens 	rw_exit(&dp->dp_config_rwlock);
585789Sahrens 	dsl_dir_close(dd, FTAG);
586789Sahrens 	return (err);
587789Sahrens }
588789Sahrens 
589789Sahrens int
5906689Smaybee dsl_dataset_own(const char *name, int flags, void *owner, dsl_dataset_t **dsp)
591789Sahrens {
5926689Smaybee 	int err = dsl_dataset_hold(name, owner, dsp);
5936689Smaybee 	if (err)
5946689Smaybee 		return (err);
5956689Smaybee 	if ((*dsp)->ds_phys->ds_num_children > 0 &&
5966689Smaybee 	    !DS_MODE_IS_READONLY(flags)) {
5976689Smaybee 		dsl_dataset_rele(*dsp, owner);
5986689Smaybee 		return (EROFS);
5996689Smaybee 	}
6006689Smaybee 	if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) {
6016689Smaybee 		dsl_dataset_rele(*dsp, owner);
6026689Smaybee 		return (EBUSY);
6036689Smaybee 	}
6046689Smaybee 	return (0);
605789Sahrens }
606789Sahrens 
607789Sahrens void
608789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
609789Sahrens {
610789Sahrens 	if (ds == NULL) {
611789Sahrens 		(void) strcpy(name, "mos");
612789Sahrens 	} else {
613789Sahrens 		dsl_dir_name(ds->ds_dir, name);
6141544Seschrock 		VERIFY(0 == dsl_dataset_get_snapname(ds));
615789Sahrens 		if (ds->ds_snapname[0]) {
616789Sahrens 			(void) strcat(name, "@");
6176689Smaybee 			/*
6186689Smaybee 			 * We use a "recursive" mutex so that we
6196689Smaybee 			 * can call dprintf_ds() with ds_lock held.
6206689Smaybee 			 */
621789Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
622789Sahrens 				mutex_enter(&ds->ds_lock);
623789Sahrens 				(void) strcat(name, ds->ds_snapname);
624789Sahrens 				mutex_exit(&ds->ds_lock);
625789Sahrens 			} else {
626789Sahrens 				(void) strcat(name, ds->ds_snapname);
627789Sahrens 			}
628789Sahrens 		}
629789Sahrens 	}
630789Sahrens }
631789Sahrens 
6323978Smmusante static int
6333978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds)
6343978Smmusante {
6353978Smmusante 	int result;
6363978Smmusante 
6373978Smmusante 	if (ds == NULL) {
6383978Smmusante 		result = 3;	/* "mos" */
6393978Smmusante 	} else {
6403978Smmusante 		result = dsl_dir_namelen(ds->ds_dir);
6413978Smmusante 		VERIFY(0 == dsl_dataset_get_snapname(ds));
6423978Smmusante 		if (ds->ds_snapname[0]) {
6433978Smmusante 			++result;	/* adding one for the @-sign */
6443978Smmusante 			if (!MUTEX_HELD(&ds->ds_lock)) {
6453978Smmusante 				mutex_enter(&ds->ds_lock);
6463978Smmusante 				result += strlen(ds->ds_snapname);
6473978Smmusante 				mutex_exit(&ds->ds_lock);
6483978Smmusante 			} else {
6493978Smmusante 				result += strlen(ds->ds_snapname);
6503978Smmusante 			}
6513978Smmusante 		}
6523978Smmusante 	}
6533978Smmusante 
6543978Smmusante 	return (result);
6553978Smmusante }
6563978Smmusante 
6577046Sahrens void
6586689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag)
659789Sahrens {
6601544Seschrock 	dmu_buf_rele(ds->ds_dbuf, tag);
661789Sahrens }
662789Sahrens 
663789Sahrens void
6646689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
6655367Sahrens {
6666689Smaybee 	if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) {
6676689Smaybee 		rw_exit(&ds->ds_rwlock);
6686689Smaybee 	}
6696689Smaybee 	dsl_dataset_drop_ref(ds, tag);
6706689Smaybee }
6716689Smaybee 
6726689Smaybee void
6736689Smaybee dsl_dataset_disown(dsl_dataset_t *ds, void *owner)
6746689Smaybee {
6756689Smaybee 	ASSERT((ds->ds_owner == owner && ds->ds_dbuf) ||
6766689Smaybee 	    (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL));
6776689Smaybee 
6785367Sahrens 	mutex_enter(&ds->ds_lock);
6796689Smaybee 	ds->ds_owner = NULL;
6806689Smaybee 	if (RW_WRITE_HELD(&ds->ds_rwlock)) {
6816689Smaybee 		rw_exit(&ds->ds_rwlock);
6826689Smaybee 		cv_broadcast(&ds->ds_exclusive_cv);
6836689Smaybee 	}
6845367Sahrens 	mutex_exit(&ds->ds_lock);
6856689Smaybee 	if (ds->ds_dbuf)
6866689Smaybee 		dsl_dataset_drop_ref(ds, owner);
6876689Smaybee 	else
6886689Smaybee 		dsl_dataset_evict(ds->ds_dbuf, ds);
6895367Sahrens }
6905367Sahrens 
6915367Sahrens boolean_t
6926689Smaybee dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *owner)
6935367Sahrens {
6946689Smaybee 	boolean_t gotit = FALSE;
6956689Smaybee 
6965367Sahrens 	mutex_enter(&ds->ds_lock);
6976689Smaybee 	if (ds->ds_owner == NULL &&
6986689Smaybee 	    (!DS_IS_INCONSISTENT(ds) || inconsistentok)) {
6996689Smaybee 		ds->ds_owner = owner;
7006689Smaybee 		if (!dsl_pool_sync_context(ds->ds_dir->dd_pool))
7016689Smaybee 			rw_exit(&ds->ds_rwlock);
7026689Smaybee 		gotit = TRUE;
7035367Sahrens 	}
7045367Sahrens 	mutex_exit(&ds->ds_lock);
7056689Smaybee 	return (gotit);
7066689Smaybee }
7076689Smaybee 
7086689Smaybee void
7096689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner)
7106689Smaybee {
7116689Smaybee 	ASSERT3P(owner, ==, ds->ds_owner);
7126689Smaybee 	if (!RW_WRITE_HELD(&ds->ds_rwlock))
7136689Smaybee 		rw_enter(&ds->ds_rwlock, RW_WRITER);
7145367Sahrens }
7155367Sahrens 
7162199Sahrens uint64_t
7177046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
7186492Stimh     uint64_t flags, dmu_tx_t *tx)
719789Sahrens {
7205367Sahrens 	dsl_pool_t *dp = dd->dd_pool;
721789Sahrens 	dmu_buf_t *dbuf;
722789Sahrens 	dsl_dataset_phys_t *dsphys;
7235367Sahrens 	uint64_t dsobj;
724789Sahrens 	objset_t *mos = dp->dp_meta_objset;
725789Sahrens 
7267046Sahrens 	if (origin == NULL)
7277046Sahrens 		origin = dp->dp_origin_snap;
7287046Sahrens 
7295367Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
7305367Sahrens 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
731789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7325367Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
733789Sahrens 
734928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
735928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
7361544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
737789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
738789Sahrens 	dsphys = dbuf->db_data;
7396689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
740789Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
7416492Stimh 	dsphys->ds_flags = flags;
742789Sahrens 	dsphys->ds_fsid_guid = unique_create();
743789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
744789Sahrens 	    sizeof (dsphys->ds_guid));
745789Sahrens 	dsphys->ds_snapnames_zapobj =
7466492Stimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
7476492Stimh 	    DMU_OT_NONE, 0, tx);
748789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
7497046Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
750789Sahrens 	dsphys->ds_deadlist_obj =
751789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
7525378Sck153898 
7535367Sahrens 	if (origin) {
7545367Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
755789Sahrens 		dsphys->ds_prev_snap_txg =
7565367Sahrens 		    origin->ds_phys->ds_creation_txg;
757789Sahrens 		dsphys->ds_used_bytes =
7585367Sahrens 		    origin->ds_phys->ds_used_bytes;
759789Sahrens 		dsphys->ds_compressed_bytes =
7605367Sahrens 		    origin->ds_phys->ds_compressed_bytes;
761789Sahrens 		dsphys->ds_uncompressed_bytes =
7625367Sahrens 		    origin->ds_phys->ds_uncompressed_bytes;
7635367Sahrens 		dsphys->ds_bp = origin->ds_phys->ds_bp;
7646502Stimh 		dsphys->ds_flags |= origin->ds_phys->ds_flags;
765789Sahrens 
7665367Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
7675367Sahrens 		origin->ds_phys->ds_num_children++;
768789Sahrens 
7697046Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
7707046Sahrens 			if (origin->ds_phys->ds_next_clones_obj == 0) {
7717046Sahrens 				origin->ds_phys->ds_next_clones_obj =
7727046Sahrens 				    zap_create(mos,
7737046Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
7747046Sahrens 			}
7757046Sahrens 			VERIFY(0 == zap_add_int(mos,
7767046Sahrens 			    origin->ds_phys->ds_next_clones_obj,
7777046Sahrens 			    dsobj, tx));
7787046Sahrens 		}
7797046Sahrens 
780789Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
7815367Sahrens 		dd->dd_phys->dd_origin_obj = origin->ds_object;
782789Sahrens 	}
7836492Stimh 
7846492Stimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
7856492Stimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
7866492Stimh 
7871544Seschrock 	dmu_buf_rele(dbuf, FTAG);
788789Sahrens 
789789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
790789Sahrens 	dd->dd_phys->dd_head_dataset_obj = dsobj;
7915367Sahrens 
7925367Sahrens 	return (dsobj);
7935367Sahrens }
7945367Sahrens 
7955367Sahrens uint64_t
7966492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
7976492Stimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
7985367Sahrens {
7995367Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
8005367Sahrens 	uint64_t dsobj, ddobj;
8015367Sahrens 	dsl_dir_t *dd;
8025367Sahrens 
8035367Sahrens 	ASSERT(lastname[0] != '@');
8045367Sahrens 
8057046Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
8065367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
8075367Sahrens 
8087046Sahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx);
8095367Sahrens 
8105367Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
8115367Sahrens 
812789Sahrens 	dsl_dir_close(dd, FTAG);
813789Sahrens 
8142199Sahrens 	return (dsobj);
8152199Sahrens }
8162199Sahrens 
8172199Sahrens struct destroyarg {
8182199Sahrens 	dsl_sync_task_group_t *dstg;
8192199Sahrens 	char *snapname;
8202199Sahrens 	char *failed;
8212199Sahrens };
8222199Sahrens 
8232199Sahrens static int
8242199Sahrens dsl_snapshot_destroy_one(char *name, void *arg)
8252199Sahrens {
8262199Sahrens 	struct destroyarg *da = arg;
8272199Sahrens 	dsl_dataset_t *ds;
8282199Sahrens 	char *cp;
8292199Sahrens 	int err;
8302199Sahrens 
8312199Sahrens 	(void) strcat(name, "@");
8322199Sahrens 	(void) strcat(name, da->snapname);
8336689Smaybee 	err = dsl_dataset_own(name, DS_MODE_READONLY | DS_MODE_INCONSISTENT,
8344007Smmusante 	    da->dstg, &ds);
8352199Sahrens 	cp = strchr(name, '@');
8362199Sahrens 	*cp = '\0';
8376689Smaybee 	if (err == 0) {
8386689Smaybee 		dsl_dataset_make_exclusive(ds, da->dstg);
8396689Smaybee 		dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check,
8406689Smaybee 		    dsl_dataset_destroy_sync, ds, da->dstg, 0);
8416689Smaybee 	} else if (err == ENOENT) {
8426689Smaybee 		err = 0;
8436689Smaybee 	} else {
8442199Sahrens 		(void) strcpy(da->failed, name);
8452199Sahrens 	}
8466689Smaybee 	return (err);
847789Sahrens }
848789Sahrens 
8492199Sahrens /*
8502199Sahrens  * Destroy 'snapname' in all descendants of 'fsname'.
8512199Sahrens  */
8522199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy
8532199Sahrens int
8542199Sahrens dsl_snapshots_destroy(char *fsname, char *snapname)
8552199Sahrens {
8562199Sahrens 	int err;
8572199Sahrens 	struct destroyarg da;
8582199Sahrens 	dsl_sync_task_t *dst;
8592199Sahrens 	spa_t *spa;
8602199Sahrens 
8614603Sahrens 	err = spa_open(fsname, &spa, FTAG);
8622199Sahrens 	if (err)
8632199Sahrens 		return (err);
8642199Sahrens 	da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8652199Sahrens 	da.snapname = snapname;
8662199Sahrens 	da.failed = fsname;
8672199Sahrens 
8682199Sahrens 	err = dmu_objset_find(fsname,
8692417Sahrens 	    dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN);
8702199Sahrens 
8712199Sahrens 	if (err == 0)
8722199Sahrens 		err = dsl_sync_task_group_wait(da.dstg);
8732199Sahrens 
8742199Sahrens 	for (dst = list_head(&da.dstg->dstg_tasks); dst;
8752199Sahrens 	    dst = list_next(&da.dstg->dstg_tasks, dst)) {
8762199Sahrens 		dsl_dataset_t *ds = dst->dst_arg1;
8776689Smaybee 		/*
8786689Smaybee 		 * Return the file system name that triggered the error
8796689Smaybee 		 */
8802199Sahrens 		if (dst->dst_err) {
8812199Sahrens 			dsl_dataset_name(ds, fsname);
8824603Sahrens 			*strchr(fsname, '@') = '\0';
8832199Sahrens 		}
8846689Smaybee 		dsl_dataset_disown(ds, da.dstg);
8852199Sahrens 	}
8862199Sahrens 
8872199Sahrens 	dsl_sync_task_group_destroy(da.dstg);
8882199Sahrens 	spa_close(spa, FTAG);
8892199Sahrens 	return (err);
8902199Sahrens }
8912199Sahrens 
8925367Sahrens /*
8936689Smaybee  * ds must be opened as OWNER.  On return (whether successful or not),
8946689Smaybee  * ds will be closed and caller can no longer dereference it.
8955367Sahrens  */
896789Sahrens int
8975367Sahrens dsl_dataset_destroy(dsl_dataset_t *ds, void *tag)
898789Sahrens {
899789Sahrens 	int err;
9002199Sahrens 	dsl_sync_task_group_t *dstg;
9012199Sahrens 	objset_t *os;
902789Sahrens 	dsl_dir_t *dd;
9032199Sahrens 	uint64_t obj;
9042199Sahrens 
9055367Sahrens 	if (dsl_dataset_is_snapshot(ds)) {
9062199Sahrens 		/* Destroying a snapshot is simpler */
9076689Smaybee 		dsl_dataset_make_exclusive(ds, tag);
9082199Sahrens 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
9092199Sahrens 		    dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
9105367Sahrens 		    ds, tag, 0);
9115367Sahrens 		goto out;
9122199Sahrens 	}
9132199Sahrens 
9142199Sahrens 	dd = ds->ds_dir;
915789Sahrens 
9162199Sahrens 	/*
9172199Sahrens 	 * Check for errors and mark this ds as inconsistent, in
9182199Sahrens 	 * case we crash while freeing the objects.
9192199Sahrens 	 */
9202199Sahrens 	err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
9212199Sahrens 	    dsl_dataset_destroy_begin_sync, ds, NULL, 0);
9225367Sahrens 	if (err)
9235367Sahrens 		goto out;
9245367Sahrens 
9255367Sahrens 	err = dmu_objset_open_ds(ds, DMU_OST_ANY, &os);
9265367Sahrens 	if (err)
9275367Sahrens 		goto out;
9282199Sahrens 
9292199Sahrens 	/*
9302199Sahrens 	 * remove the objects in open context, so that we won't
9312199Sahrens 	 * have too much to do in syncing context.
9322199Sahrens 	 */
9333025Sahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
9343025Sahrens 	    ds->ds_phys->ds_prev_snap_txg)) {
9356992Smaybee 		/*
9366992Smaybee 		 * Ignore errors, if there is not enough disk space
9376992Smaybee 		 * we will deal with it in dsl_dataset_destroy_sync().
9386992Smaybee 		 */
9396992Smaybee 		(void) dmu_free_object(os, obj);
9402199Sahrens 	}
9412199Sahrens 
9422199Sahrens 	dmu_objset_close(os);
9432199Sahrens 	if (err != ESRCH)
9445367Sahrens 		goto out;
9452199Sahrens 
9466975Smaybee 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
9476975Smaybee 	err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd);
9486975Smaybee 	rw_exit(&dd->dd_pool->dp_config_rwlock);
9496975Smaybee 
9506975Smaybee 	if (err)
9516975Smaybee 		goto out;
9526975Smaybee 
9535367Sahrens 	if (ds->ds_user_ptr) {
9546689Smaybee 		/*
9556689Smaybee 		 * We need to sync out all in-flight IO before we try
9566689Smaybee 		 * to evict (the dataset evict func is trying to clear
9576689Smaybee 		 * the cached entries for this dataset in the ARC).
9586689Smaybee 		 */
9596689Smaybee 		txg_wait_synced(dd->dd_pool, 0);
9605367Sahrens 	}
9615367Sahrens 
9622199Sahrens 	/*
9632199Sahrens 	 * Blow away the dsl_dir + head dataset.
9642199Sahrens 	 */
9656689Smaybee 	dsl_dataset_make_exclusive(ds, tag);
9666975Smaybee 	if (ds->ds_user_ptr) {
9676975Smaybee 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
9686975Smaybee 		ds->ds_user_ptr = NULL;
9696975Smaybee 	}
9702199Sahrens 	dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
9712199Sahrens 	dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
9725367Sahrens 	    dsl_dataset_destroy_sync, ds, tag, 0);
9732199Sahrens 	dsl_sync_task_create(dstg, dsl_dir_destroy_check,
9742199Sahrens 	    dsl_dir_destroy_sync, dd, FTAG, 0);
9752199Sahrens 	err = dsl_sync_task_group_wait(dstg);
9762199Sahrens 	dsl_sync_task_group_destroy(dstg);
9776689Smaybee 	/* if it is successful, dsl_dir_destroy_sync will close the dd */
9785367Sahrens 	if (err)
9792199Sahrens 		dsl_dir_close(dd, FTAG);
9805367Sahrens out:
9816689Smaybee 	dsl_dataset_disown(ds, tag);
982789Sahrens 	return (err);
983789Sahrens }
984789Sahrens 
985789Sahrens int
9865367Sahrens dsl_dataset_rollback(dsl_dataset_t *ds, dmu_objset_type_t ost)
987789Sahrens {
9886689Smaybee 	ASSERT(ds->ds_owner);
9895367Sahrens 
9902199Sahrens 	return (dsl_sync_task_do(ds->ds_dir->dd_pool,
9912199Sahrens 	    dsl_dataset_rollback_check, dsl_dataset_rollback_sync,
9925367Sahrens 	    ds, &ost, 0));
993789Sahrens }
994789Sahrens 
995789Sahrens void *
996789Sahrens dsl_dataset_set_user_ptr(dsl_dataset_t *ds,
997789Sahrens     void *p, dsl_dataset_evict_func_t func)
998789Sahrens {
999789Sahrens 	void *old;
1000789Sahrens 
1001789Sahrens 	mutex_enter(&ds->ds_lock);
1002789Sahrens 	old = ds->ds_user_ptr;
1003789Sahrens 	if (old == NULL) {
1004789Sahrens 		ds->ds_user_ptr = p;
1005789Sahrens 		ds->ds_user_evict_func = func;
1006789Sahrens 	}
1007789Sahrens 	mutex_exit(&ds->ds_lock);
1008789Sahrens 	return (old);
1009789Sahrens }
1010789Sahrens 
1011789Sahrens void *
1012789Sahrens dsl_dataset_get_user_ptr(dsl_dataset_t *ds)
1013789Sahrens {
1014789Sahrens 	return (ds->ds_user_ptr);
1015789Sahrens }
1016789Sahrens 
1017789Sahrens 
10183547Smaybee blkptr_t *
10193547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1020789Sahrens {
10213547Smaybee 	return (&ds->ds_phys->ds_bp);
1022789Sahrens }
1023789Sahrens 
1024789Sahrens void
1025789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1026789Sahrens {
1027789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1028789Sahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
1029789Sahrens 	if (ds == NULL) {
1030789Sahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
1031789Sahrens 	} else {
1032789Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1033789Sahrens 		ds->ds_phys->ds_bp = *bp;
1034789Sahrens 	}
1035789Sahrens }
1036789Sahrens 
1037789Sahrens spa_t *
1038789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
1039789Sahrens {
1040789Sahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1041789Sahrens }
1042789Sahrens 
1043789Sahrens void
1044789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1045789Sahrens {
1046789Sahrens 	dsl_pool_t *dp;
1047789Sahrens 
1048789Sahrens 	if (ds == NULL) /* this is the meta-objset */
1049789Sahrens 		return;
1050789Sahrens 
1051789Sahrens 	ASSERT(ds->ds_user_ptr != NULL);
10522885Sahrens 
10532885Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
10542885Sahrens 		panic("dirtying snapshot!");
1055789Sahrens 
1056789Sahrens 	dp = ds->ds_dir->dd_pool;
1057789Sahrens 
1058789Sahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
1059789Sahrens 		/* up the hold count until we can be written out */
1060789Sahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1061789Sahrens 	}
1062789Sahrens }
1063789Sahrens 
10645378Sck153898 /*
10655378Sck153898  * The unique space in the head dataset can be calculated by subtracting
10665378Sck153898  * the space used in the most recent snapshot, that is still being used
10675378Sck153898  * in this file system, from the space currently in use.  To figure out
10685378Sck153898  * the space in the most recent snapshot still in use, we need to take
10695378Sck153898  * the total space used in the snapshot and subtract out the space that
10705378Sck153898  * has been freed up since the snapshot was taken.
10715378Sck153898  */
10725378Sck153898 static void
10735378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
10745378Sck153898 {
10755378Sck153898 	uint64_t mrs_used;
10765378Sck153898 	uint64_t dlused, dlcomp, dluncomp;
10775378Sck153898 
10785378Sck153898 	ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj);
10795378Sck153898 
10805378Sck153898 	if (ds->ds_phys->ds_prev_snap_obj != 0)
10815378Sck153898 		mrs_used = ds->ds_prev->ds_phys->ds_used_bytes;
10825378Sck153898 	else
10835378Sck153898 		mrs_used = 0;
10845378Sck153898 
10855378Sck153898 	VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp,
10865378Sck153898 	    &dluncomp));
10875378Sck153898 
10885378Sck153898 	ASSERT3U(dlused, <=, mrs_used);
10895378Sck153898 	ds->ds_phys->ds_unique_bytes =
10905378Sck153898 	    ds->ds_phys->ds_used_bytes - (mrs_used - dlused);
10915378Sck153898 
10925378Sck153898 	if (!DS_UNIQUE_IS_ACCURATE(ds) &&
10935378Sck153898 	    spa_version(ds->ds_dir->dd_pool->dp_spa) >=
10945378Sck153898 	    SPA_VERSION_UNIQUE_ACCURATE)
10955378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
10965378Sck153898 }
10975378Sck153898 
10985378Sck153898 static uint64_t
10995378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds)
11005378Sck153898 {
11015378Sck153898 	if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds))
11025378Sck153898 		dsl_dataset_recalc_head_uniq(ds);
11035378Sck153898 
11045378Sck153898 	return (ds->ds_phys->ds_unique_bytes);
11055378Sck153898 }
11065378Sck153898 
1107789Sahrens struct killarg {
11085378Sck153898 	int64_t *usedp;
11095378Sck153898 	int64_t *compressedp;
11105378Sck153898 	int64_t *uncompressedp;
1111789Sahrens 	zio_t *zio;
1112789Sahrens 	dmu_tx_t *tx;
1113789Sahrens };
1114789Sahrens 
1115789Sahrens static int
1116789Sahrens kill_blkptr(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
1117789Sahrens {
1118789Sahrens 	struct killarg *ka = arg;
1119789Sahrens 	blkptr_t *bp = &bc->bc_blkptr;
1120789Sahrens 
1121789Sahrens 	ASSERT3U(bc->bc_errno, ==, 0);
1122789Sahrens 
1123789Sahrens 	/*
1124789Sahrens 	 * Since this callback is not called concurrently, no lock is
1125789Sahrens 	 * needed on the accounting values.
1126789Sahrens 	 */
11272082Seschrock 	*ka->usedp += bp_get_dasize(spa, bp);
1128789Sahrens 	*ka->compressedp += BP_GET_PSIZE(bp);
1129789Sahrens 	*ka->uncompressedp += BP_GET_UCSIZE(bp);
1130789Sahrens 	/* XXX check for EIO? */
11317046Sahrens 	(void) dsl_free(ka->zio, spa_get_dsl(spa), ka->tx->tx_txg,
11327046Sahrens 	    bp, NULL, NULL, ARC_NOWAIT);
1133789Sahrens 	return (0);
1134789Sahrens }
1135789Sahrens 
1136789Sahrens /* ARGSUSED */
11372199Sahrens static int
11382199Sahrens dsl_dataset_rollback_check(void *arg1, void *arg2, dmu_tx_t *tx)
1139789Sahrens {
11402199Sahrens 	dsl_dataset_t *ds = arg1;
11415367Sahrens 	dmu_objset_type_t *ost = arg2;
1142789Sahrens 
11432199Sahrens 	/*
11445367Sahrens 	 * We can only roll back to emptyness if it is a ZPL objset.
11452199Sahrens 	 */
11465367Sahrens 	if (*ost != DMU_OST_ZFS && ds->ds_phys->ds_prev_snap_txg == 0)
1147789Sahrens 		return (EINVAL);
1148789Sahrens 
11492199Sahrens 	/*
11502199Sahrens 	 * This must not be a snapshot.
11512199Sahrens 	 */
11522199Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
11532199Sahrens 		return (EINVAL);
1154789Sahrens 
1155789Sahrens 	/*
1156789Sahrens 	 * If we made changes this txg, traverse_dsl_dataset won't find
1157789Sahrens 	 * them.  Try again.
1158789Sahrens 	 */
11592199Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1160789Sahrens 		return (EAGAIN);
11612199Sahrens 
11622199Sahrens 	return (0);
11632199Sahrens }
1164789Sahrens 
11652199Sahrens /* ARGSUSED */
11662199Sahrens static void
11674543Smarks dsl_dataset_rollback_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
11682199Sahrens {
11692199Sahrens 	dsl_dataset_t *ds = arg1;
11705367Sahrens 	dmu_objset_type_t *ost = arg2;
11712199Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1172789Sahrens 
1173789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1174789Sahrens 
11754967Sperrin 	/*
11764967Sperrin 	 * Before the roll back destroy the zil.
11774967Sperrin 	 */
11784967Sperrin 	if (ds->ds_user_ptr != NULL) {
11794967Sperrin 		zil_rollback_destroy(
11804967Sperrin 		    ((objset_impl_t *)ds->ds_user_ptr)->os_zil, tx);
11815367Sahrens 
11825367Sahrens 		/*
11835367Sahrens 		 * We need to make sure that the objset_impl_t is reopened after
11845367Sahrens 		 * we do the rollback, otherwise it will have the wrong
11855367Sahrens 		 * objset_phys_t.  Normally this would happen when this
11866689Smaybee 		 * dataset-open is closed, thus causing the
11875367Sahrens 		 * dataset to be immediately evicted.  But when doing "zfs recv
11885367Sahrens 		 * -F", we reopen the objset before that, so that there is no
11895367Sahrens 		 * window where the dataset is closed and inconsistent.
11905367Sahrens 		 */
11915367Sahrens 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
11925367Sahrens 		ds->ds_user_ptr = NULL;
11934967Sperrin 	}
11944935Sperrin 
1195789Sahrens 	/* Zero out the deadlist. */
1196789Sahrens 	bplist_close(&ds->ds_deadlist);
1197789Sahrens 	bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1198789Sahrens 	ds->ds_phys->ds_deadlist_obj =
1199789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
12001544Seschrock 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
12011544Seschrock 	    ds->ds_phys->ds_deadlist_obj));
1202789Sahrens 
1203789Sahrens 	{
1204789Sahrens 		/* Free blkptrs that we gave birth to */
1205789Sahrens 		zio_t *zio;
12065378Sck153898 		int64_t used = 0, compressed = 0, uncompressed = 0;
1207789Sahrens 		struct killarg ka;
12085518Sck153898 		int64_t delta;
1209789Sahrens 
1210789Sahrens 		zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL,
1211789Sahrens 		    ZIO_FLAG_MUSTSUCCEED);
1212789Sahrens 		ka.usedp = &used;
1213789Sahrens 		ka.compressedp = &compressed;
1214789Sahrens 		ka.uncompressedp = &uncompressed;
1215789Sahrens 		ka.zio = zio;
1216789Sahrens 		ka.tx = tx;
1217789Sahrens 		(void) traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
1218789Sahrens 		    ADVANCE_POST, kill_blkptr, &ka);
1219789Sahrens 		(void) zio_wait(zio);
1220789Sahrens 
12215518Sck153898 		/* only deduct space beyond any refreservation */
12225518Sck153898 		delta = parent_delta(ds, -used);
12232199Sahrens 		dsl_dir_diduse_space(ds->ds_dir,
12245518Sck153898 		    delta, -compressed, -uncompressed, tx);
1225789Sahrens 	}
1226789Sahrens 
12277046Sahrens 	if (ds->ds_prev && ds->ds_prev != ds->ds_dir->dd_pool->dp_origin_snap) {
12285367Sahrens 		/* Change our contents to that of the prev snapshot */
12295367Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
12305367Sahrens 		    ds->ds_phys->ds_prev_snap_obj);
12315367Sahrens 		ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp;
12325367Sahrens 		ds->ds_phys->ds_used_bytes =
12335367Sahrens 		    ds->ds_prev->ds_phys->ds_used_bytes;
12345367Sahrens 		ds->ds_phys->ds_compressed_bytes =
12355367Sahrens 		    ds->ds_prev->ds_phys->ds_compressed_bytes;
12365367Sahrens 		ds->ds_phys->ds_uncompressed_bytes =
12375367Sahrens 		    ds->ds_prev->ds_phys->ds_uncompressed_bytes;
12385367Sahrens 		ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags;
12395367Sahrens 		ds->ds_phys->ds_unique_bytes = 0;
1240789Sahrens 
12415367Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
12425367Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
12435367Sahrens 			ds->ds_prev->ds_phys->ds_unique_bytes = 0;
12445367Sahrens 		}
12455367Sahrens 	} else {
12467046Sahrens 		objset_impl_t *osi;
12477046Sahrens 
12485367Sahrens 		/* Zero out our contents, recreate objset */
12495367Sahrens 		bzero(&ds->ds_phys->ds_bp, sizeof (blkptr_t));
12505367Sahrens 		ds->ds_phys->ds_used_bytes = 0;
12515367Sahrens 		ds->ds_phys->ds_compressed_bytes = 0;
12525367Sahrens 		ds->ds_phys->ds_uncompressed_bytes = 0;
12535367Sahrens 		ds->ds_phys->ds_flags = 0;
12545367Sahrens 		ds->ds_phys->ds_unique_bytes = 0;
12557046Sahrens 		osi = dmu_objset_create_impl(ds->ds_dir->dd_pool->dp_spa, ds,
12565367Sahrens 		    &ds->ds_phys->ds_bp, *ost, tx);
12577046Sahrens #ifdef _KERNEL
12587046Sahrens 		zfs_create_fs(&osi->os, kcred, NULL, tx);
12597046Sahrens #endif
12602532Sahrens 	}
12614543Smarks 
12624543Smarks 	spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa,
12634543Smarks 	    tx, cr, "dataset = %llu", ds->ds_object);
1264789Sahrens }
1265789Sahrens 
12661731Sbonwick /* ARGSUSED */
12671731Sbonwick static int
12682199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
12691731Sbonwick {
12702199Sahrens 	dsl_dataset_t *ds = arg1;
12715367Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
12725367Sahrens 	uint64_t count;
12735367Sahrens 	int err;
12741731Sbonwick 
12751731Sbonwick 	/*
12761731Sbonwick 	 * Can't delete a head dataset if there are snapshots of it.
12771731Sbonwick 	 * (Except if the only snapshots are from the branch we cloned
12781731Sbonwick 	 * from.)
12791731Sbonwick 	 */
12801731Sbonwick 	if (ds->ds_prev != NULL &&
12811731Sbonwick 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
12821731Sbonwick 		return (EINVAL);
12831731Sbonwick 
12845367Sahrens 	/*
12855367Sahrens 	 * This is really a dsl_dir thing, but check it here so that
12865367Sahrens 	 * we'll be less likely to leave this dataset inconsistent &
12875367Sahrens 	 * nearly destroyed.
12885367Sahrens 	 */
12895367Sahrens 	err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
12905367Sahrens 	if (err)
12915367Sahrens 		return (err);
12925367Sahrens 	if (count != 0)
12935367Sahrens 		return (EEXIST);
12945367Sahrens 
12951731Sbonwick 	return (0);
12961731Sbonwick }
12971731Sbonwick 
12982199Sahrens /* ARGSUSED */
12992199Sahrens static void
13004543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1301789Sahrens {
13022199Sahrens 	dsl_dataset_t *ds = arg1;
13034543Smarks 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1304789Sahrens 
13052199Sahrens 	/* Mark it as inconsistent on-disk, in case we crash */
13062199Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
13072199Sahrens 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
13084543Smarks 
13094543Smarks 	spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
13104543Smarks 	    cr, "dataset = %llu", ds->ds_object);
13112199Sahrens }
1312789Sahrens 
13132199Sahrens /* ARGSUSED */
13145367Sahrens int
13152199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
13162199Sahrens {
13172199Sahrens 	dsl_dataset_t *ds = arg1;
1318789Sahrens 
13196689Smaybee 	/* we have an owner hold, so noone else can destroy us */
13206689Smaybee 	ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
13216689Smaybee 
1322789Sahrens 	/* Can't delete a branch point. */
13232199Sahrens 	if (ds->ds_phys->ds_num_children > 1)
13242199Sahrens 		return (EEXIST);
1325789Sahrens 
1326789Sahrens 	/*
1327789Sahrens 	 * Can't delete a head dataset if there are snapshots of it.
1328789Sahrens 	 * (Except if the only snapshots are from the branch we cloned
1329789Sahrens 	 * from.)
1330789Sahrens 	 */
1331789Sahrens 	if (ds->ds_prev != NULL &&
13322199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1333789Sahrens 		return (EINVAL);
1334789Sahrens 
1335789Sahrens 	/*
1336789Sahrens 	 * If we made changes this txg, traverse_dsl_dataset won't find
1337789Sahrens 	 * them.  Try again.
1338789Sahrens 	 */
13392199Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1340789Sahrens 		return (EAGAIN);
13412199Sahrens 
13422199Sahrens 	/* XXX we should do some i/o error checking... */
13432199Sahrens 	return (0);
13442199Sahrens }
13452199Sahrens 
13466689Smaybee struct refsarg {
13476689Smaybee 	kmutex_t lock;
13486689Smaybee 	boolean_t gone;
13496689Smaybee 	kcondvar_t cv;
13506689Smaybee };
13516689Smaybee 
13526689Smaybee /* ARGSUSED */
13536689Smaybee static void
13546689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv)
13556689Smaybee {
13566689Smaybee 	struct refsarg *arg = argv;
13576689Smaybee 
13586689Smaybee 	mutex_enter(&arg->lock);
13596689Smaybee 	arg->gone = TRUE;
13606689Smaybee 	cv_signal(&arg->cv);
13616689Smaybee 	mutex_exit(&arg->lock);
13626689Smaybee }
13636689Smaybee 
13646689Smaybee static void
13656689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag)
13666689Smaybee {
13676689Smaybee 	struct refsarg arg;
13686689Smaybee 
13696689Smaybee 	mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL);
13706689Smaybee 	cv_init(&arg.cv, NULL, CV_DEFAULT, NULL);
13716689Smaybee 	arg.gone = FALSE;
13726689Smaybee 	(void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys,
13736689Smaybee 	    dsl_dataset_refs_gone);
13746689Smaybee 	dmu_buf_rele(ds->ds_dbuf, tag);
13756689Smaybee 	mutex_enter(&arg.lock);
13766689Smaybee 	while (!arg.gone)
13776689Smaybee 		cv_wait(&arg.cv, &arg.lock);
13786689Smaybee 	ASSERT(arg.gone);
13796689Smaybee 	mutex_exit(&arg.lock);
13806689Smaybee 	ds->ds_dbuf = NULL;
13816689Smaybee 	ds->ds_phys = NULL;
13826689Smaybee 	mutex_destroy(&arg.lock);
13836689Smaybee 	cv_destroy(&arg.cv);
13846689Smaybee }
13856689Smaybee 
13865367Sahrens void
13874543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
13882199Sahrens {
13892199Sahrens 	dsl_dataset_t *ds = arg1;
13905378Sck153898 	int64_t used = 0, compressed = 0, uncompressed = 0;
13912199Sahrens 	zio_t *zio;
13922199Sahrens 	int err;
13932199Sahrens 	int after_branch_point = FALSE;
13942199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
13952199Sahrens 	objset_t *mos = dp->dp_meta_objset;
13962199Sahrens 	dsl_dataset_t *ds_prev = NULL;
13972199Sahrens 	uint64_t obj;
13982199Sahrens 
13996689Smaybee 	ASSERT(ds->ds_owner);
14002199Sahrens 	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
14012199Sahrens 	ASSERT(ds->ds_prev == NULL ||
14022199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
14032199Sahrens 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
14042199Sahrens 
14056689Smaybee 	/* signal any waiters that this dataset is going away */
14066689Smaybee 	mutex_enter(&ds->ds_lock);
14076689Smaybee 	ds->ds_owner = dsl_reaper;
14086689Smaybee 	cv_broadcast(&ds->ds_exclusive_cv);
14096689Smaybee 	mutex_exit(&ds->ds_lock);
14106689Smaybee 
14115378Sck153898 	/* Remove our reservation */
14125378Sck153898 	if (ds->ds_reserved != 0) {
14135378Sck153898 		uint64_t val = 0;
14145378Sck153898 		dsl_dataset_set_reservation_sync(ds, &val, cr, tx);
14155378Sck153898 		ASSERT3U(ds->ds_reserved, ==, 0);
14165378Sck153898 	}
14175378Sck153898 
14182199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
14192199Sahrens 
14207046Sahrens 	dsl_pool_ds_destroyed(ds, tx);
14217046Sahrens 
14222199Sahrens 	obj = ds->ds_object;
1423789Sahrens 
1424789Sahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1425789Sahrens 		if (ds->ds_prev) {
1426789Sahrens 			ds_prev = ds->ds_prev;
1427789Sahrens 		} else {
14286689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
14296689Smaybee 			    ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
1430789Sahrens 		}
1431789Sahrens 		after_branch_point =
1432789Sahrens 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
1433789Sahrens 
1434789Sahrens 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1435789Sahrens 		if (after_branch_point &&
14367046Sahrens 		    ds_prev->ds_phys->ds_next_clones_obj != 0) {
14377046Sahrens 			VERIFY(0 == zap_remove_int(mos,
14387046Sahrens 			    ds_prev->ds_phys->ds_next_clones_obj, obj, tx));
14397046Sahrens 			if (ds->ds_phys->ds_next_snap_obj != 0) {
14407046Sahrens 				VERIFY(0 == zap_add_int(mos,
14417046Sahrens 				    ds_prev->ds_phys->ds_next_clones_obj,
14427046Sahrens 				    ds->ds_phys->ds_next_snap_obj, tx));
14437046Sahrens 			}
14447046Sahrens 		}
14457046Sahrens 		if (after_branch_point &&
1446789Sahrens 		    ds->ds_phys->ds_next_snap_obj == 0) {
1447789Sahrens 			/* This clone is toast. */
1448789Sahrens 			ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1449789Sahrens 			ds_prev->ds_phys->ds_num_children--;
1450789Sahrens 		} else if (!after_branch_point) {
1451789Sahrens 			ds_prev->ds_phys->ds_next_snap_obj =
1452789Sahrens 			    ds->ds_phys->ds_next_snap_obj;
1453789Sahrens 		}
1454789Sahrens 	}
1455789Sahrens 
1456789Sahrens 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1457789Sahrens 
1458789Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0) {
14592199Sahrens 		blkptr_t bp;
1460789Sahrens 		dsl_dataset_t *ds_next;
1461789Sahrens 		uint64_t itor = 0;
14625378Sck153898 		uint64_t old_unique;
1463789Sahrens 
14646689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
14656689Smaybee 		    ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
1466789Sahrens 		ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1467789Sahrens 
14685378Sck153898 		old_unique = dsl_dataset_unique(ds_next);
14695378Sck153898 
1470789Sahrens 		dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1471789Sahrens 		ds_next->ds_phys->ds_prev_snap_obj =
1472789Sahrens 		    ds->ds_phys->ds_prev_snap_obj;
1473789Sahrens 		ds_next->ds_phys->ds_prev_snap_txg =
1474789Sahrens 		    ds->ds_phys->ds_prev_snap_txg;
1475789Sahrens 		ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1476789Sahrens 		    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1477789Sahrens 
1478789Sahrens 		/*
1479789Sahrens 		 * Transfer to our deadlist (which will become next's
1480789Sahrens 		 * new deadlist) any entries from next's current
1481789Sahrens 		 * deadlist which were born before prev, and free the
1482789Sahrens 		 * other entries.
1483789Sahrens 		 *
1484789Sahrens 		 * XXX we're doing this long task with the config lock held
1485789Sahrens 		 */
14866689Smaybee 		while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) {
1487789Sahrens 			if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) {
14881544Seschrock 				VERIFY(0 == bplist_enqueue(&ds->ds_deadlist,
14891544Seschrock 				    &bp, tx));
1490789Sahrens 				if (ds_prev && !after_branch_point &&
1491789Sahrens 				    bp.blk_birth >
1492789Sahrens 				    ds_prev->ds_phys->ds_prev_snap_txg) {
1493789Sahrens 					ds_prev->ds_phys->ds_unique_bytes +=
14942082Seschrock 					    bp_get_dasize(dp->dp_spa, &bp);
1495789Sahrens 				}
1496789Sahrens 			} else {
14972082Seschrock 				used += bp_get_dasize(dp->dp_spa, &bp);
1498789Sahrens 				compressed += BP_GET_PSIZE(&bp);
1499789Sahrens 				uncompressed += BP_GET_UCSIZE(&bp);
1500789Sahrens 				/* XXX check return value? */
15017046Sahrens 				(void) dsl_free(zio, dp, tx->tx_txg,
1502789Sahrens 				    &bp, NULL, NULL, ARC_NOWAIT);
1503789Sahrens 			}
1504789Sahrens 		}
1505789Sahrens 
1506789Sahrens 		/* free next's deadlist */
1507789Sahrens 		bplist_close(&ds_next->ds_deadlist);
1508789Sahrens 		bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx);
1509789Sahrens 
1510789Sahrens 		/* set next's deadlist to our deadlist */
15116689Smaybee 		bplist_close(&ds->ds_deadlist);
1512789Sahrens 		ds_next->ds_phys->ds_deadlist_obj =
1513789Sahrens 		    ds->ds_phys->ds_deadlist_obj;
15141544Seschrock 		VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos,
15151544Seschrock 		    ds_next->ds_phys->ds_deadlist_obj));
1516789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1517789Sahrens 
1518789Sahrens 		if (ds_next->ds_phys->ds_next_snap_obj != 0) {
1519789Sahrens 			/*
1520789Sahrens 			 * Update next's unique to include blocks which
1521789Sahrens 			 * were previously shared by only this snapshot
1522789Sahrens 			 * and it.  Those blocks will be born after the
1523789Sahrens 			 * prev snap and before this snap, and will have
1524789Sahrens 			 * died after the next snap and before the one
1525789Sahrens 			 * after that (ie. be on the snap after next's
1526789Sahrens 			 * deadlist).
1527789Sahrens 			 *
1528789Sahrens 			 * XXX we're doing this long task with the
1529789Sahrens 			 * config lock held
1530789Sahrens 			 */
1531789Sahrens 			dsl_dataset_t *ds_after_next;
1532789Sahrens 
15336689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
15346689Smaybee 			    ds_next->ds_phys->ds_next_snap_obj,
15356689Smaybee 			    FTAG, &ds_after_next));
1536789Sahrens 			itor = 0;
1537789Sahrens 			while (bplist_iterate(&ds_after_next->ds_deadlist,
1538789Sahrens 			    &itor, &bp) == 0) {
1539789Sahrens 				if (bp.blk_birth >
1540789Sahrens 				    ds->ds_phys->ds_prev_snap_txg &&
1541789Sahrens 				    bp.blk_birth <=
1542789Sahrens 				    ds->ds_phys->ds_creation_txg) {
1543789Sahrens 					ds_next->ds_phys->ds_unique_bytes +=
15442082Seschrock 					    bp_get_dasize(dp->dp_spa, &bp);
1545789Sahrens 				}
1546789Sahrens 			}
1547789Sahrens 
15486689Smaybee 			dsl_dataset_rele(ds_after_next, FTAG);
1549789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, NULL);
1550789Sahrens 		} else {
1551789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, ds);
15526689Smaybee 			dsl_dataset_drop_ref(ds_next->ds_prev, ds_next);
15536689Smaybee 			ds_next->ds_prev = NULL;
1554789Sahrens 			if (ds_prev) {
15556689Smaybee 				VERIFY(0 == dsl_dataset_get_ref(dp,
15566689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
15576689Smaybee 				    ds_next, &ds_next->ds_prev));
1558789Sahrens 			}
15595378Sck153898 
15605378Sck153898 			dsl_dataset_recalc_head_uniq(ds_next);
15615378Sck153898 
15625378Sck153898 			/*
15635378Sck153898 			 * Reduce the amount of our unconsmed refreservation
15645378Sck153898 			 * being charged to our parent by the amount of
15655378Sck153898 			 * new unique data we have gained.
15665378Sck153898 			 */
15675378Sck153898 			if (old_unique < ds_next->ds_reserved) {
15685378Sck153898 				int64_t mrsdelta;
15695378Sck153898 				uint64_t new_unique =
15705378Sck153898 				    ds_next->ds_phys->ds_unique_bytes;
15715378Sck153898 
15725378Sck153898 				ASSERT(old_unique <= new_unique);
15735378Sck153898 				mrsdelta = MIN(new_unique - old_unique,
15745378Sck153898 				    ds_next->ds_reserved - old_unique);
15755378Sck153898 				dsl_dir_diduse_space(ds->ds_dir, -mrsdelta,
15765378Sck153898 				    0, 0, tx);
15775378Sck153898 			}
1578789Sahrens 		}
15796689Smaybee 		dsl_dataset_rele(ds_next, FTAG);
1580789Sahrens 
1581789Sahrens 		/*
15825378Sck153898 		 * NB: unique_bytes might not be accurate for the head objset.
15835378Sck153898 		 * Before SPA_VERSION 9, we didn't update its value when we
15845378Sck153898 		 * deleted the most recent snapshot.
1585789Sahrens 		 */
1586789Sahrens 		ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes);
1587789Sahrens 	} else {
1588789Sahrens 		/*
1589789Sahrens 		 * There's no next snapshot, so this is a head dataset.
1590789Sahrens 		 * Destroy the deadlist.  Unless it's a clone, the
1591789Sahrens 		 * deadlist should be empty.  (If it's a clone, it's
1592789Sahrens 		 * safe to ignore the deadlist contents.)
1593789Sahrens 		 */
1594789Sahrens 		struct killarg ka;
1595789Sahrens 
1596789Sahrens 		ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist));
1597789Sahrens 		bplist_close(&ds->ds_deadlist);
1598789Sahrens 		bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1599789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1600789Sahrens 
1601789Sahrens 		/*
1602789Sahrens 		 * Free everything that we point to (that's born after
1603789Sahrens 		 * the previous snapshot, if we are a clone)
1604789Sahrens 		 *
1605789Sahrens 		 * XXX we're doing this long task with the config lock held
1606789Sahrens 		 */
1607789Sahrens 		ka.usedp = &used;
1608789Sahrens 		ka.compressedp = &compressed;
1609789Sahrens 		ka.uncompressedp = &uncompressed;
1610789Sahrens 		ka.zio = zio;
1611789Sahrens 		ka.tx = tx;
1612789Sahrens 		err = traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
1613789Sahrens 		    ADVANCE_POST, kill_blkptr, &ka);
1614789Sahrens 		ASSERT3U(err, ==, 0);
16155378Sck153898 		ASSERT(spa_version(dp->dp_spa) <
16165378Sck153898 		    SPA_VERSION_UNIQUE_ACCURATE ||
16175378Sck153898 		    used == ds->ds_phys->ds_unique_bytes);
1618789Sahrens 	}
1619789Sahrens 
1620789Sahrens 	err = zio_wait(zio);
1621789Sahrens 	ASSERT3U(err, ==, 0);
1622789Sahrens 
16232199Sahrens 	dsl_dir_diduse_space(ds->ds_dir, -used, -compressed, -uncompressed, tx);
1624789Sahrens 
16256689Smaybee 	if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
16266689Smaybee 		/* Erase the link in the dir */
16276689Smaybee 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
16286689Smaybee 		ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
16296689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
1630789Sahrens 		err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1631789Sahrens 		ASSERT(err == 0);
1632789Sahrens 	} else {
1633789Sahrens 		/* remove from snapshot namespace */
1634789Sahrens 		dsl_dataset_t *ds_head;
16356689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
16366689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
16376689Smaybee 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
16382207Sahrens 		VERIFY(0 == dsl_dataset_get_snapname(ds));
1639789Sahrens #ifdef ZFS_DEBUG
1640789Sahrens 		{
1641789Sahrens 			uint64_t val;
16426492Stimh 
16436689Smaybee 			err = dsl_dataset_snap_lookup(ds_head,
16446492Stimh 			    ds->ds_snapname, &val);
1645789Sahrens 			ASSERT3U(err, ==, 0);
1646789Sahrens 			ASSERT3U(val, ==, obj);
1647789Sahrens 		}
1648789Sahrens #endif
16496689Smaybee 		err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
1650789Sahrens 		ASSERT(err == 0);
16516689Smaybee 		dsl_dataset_rele(ds_head, FTAG);
1652789Sahrens 	}
1653789Sahrens 
1654789Sahrens 	if (ds_prev && ds->ds_prev != ds_prev)
16556689Smaybee 		dsl_dataset_rele(ds_prev, FTAG);
1656789Sahrens 
16575094Slling 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
16584543Smarks 	spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx,
16594543Smarks 	    cr, "dataset = %llu", ds->ds_object);
16604543Smarks 
16617046Sahrens 	if (ds->ds_phys->ds_next_clones_obj != 0) {
16627046Sahrens 		uint64_t count;
16637046Sahrens 		ASSERT(0 == zap_count(mos,
16647046Sahrens 		    ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
16657046Sahrens 		VERIFY(0 == dmu_object_free(mos,
16667046Sahrens 		    ds->ds_phys->ds_next_clones_obj, tx));
16677046Sahrens 	}
16686689Smaybee 	dsl_dir_close(ds->ds_dir, ds);
16696689Smaybee 	ds->ds_dir = NULL;
16706689Smaybee 	dsl_dataset_drain_refs(ds, tag);
16712199Sahrens 	VERIFY(0 == dmu_object_free(mos, obj, tx));
16722199Sahrens }
16732199Sahrens 
16745378Sck153898 static int
16755378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
16765378Sck153898 {
16775378Sck153898 	uint64_t asize;
16785378Sck153898 
16795378Sck153898 	if (!dmu_tx_is_syncing(tx))
16805378Sck153898 		return (0);
16815378Sck153898 
16825378Sck153898 	/*
16835378Sck153898 	 * If there's an fs-only reservation, any blocks that might become
16845378Sck153898 	 * owned by the snapshot dataset must be accommodated by space
16855378Sck153898 	 * outside of the reservation.
16865378Sck153898 	 */
16875378Sck153898 	asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
16885378Sck153898 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE))
16895378Sck153898 		return (ENOSPC);
16905378Sck153898 
16915378Sck153898 	/*
16925378Sck153898 	 * Propogate any reserved space for this snapshot to other
16935378Sck153898 	 * snapshot checks in this sync group.
16945378Sck153898 	 */
16955378Sck153898 	if (asize > 0)
16965378Sck153898 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
16975378Sck153898 
16985378Sck153898 	return (0);
16995378Sck153898 }
17005378Sck153898 
17012199Sahrens /* ARGSUSED */
17022199Sahrens int
17032199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
17042199Sahrens {
17055367Sahrens 	dsl_dataset_t *ds = arg1;
17062199Sahrens 	const char *snapname = arg2;
17072199Sahrens 	int err;
17082199Sahrens 	uint64_t value;
1709789Sahrens 
1710789Sahrens 	/*
17112199Sahrens 	 * We don't allow multiple snapshots of the same txg.  If there
17122199Sahrens 	 * is already one, try again.
17132199Sahrens 	 */
17142199Sahrens 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
17152199Sahrens 		return (EAGAIN);
17162199Sahrens 
17172199Sahrens 	/*
17182199Sahrens 	 * Check for conflicting name snapshot name.
1719789Sahrens 	 */
17206689Smaybee 	err = dsl_dataset_snap_lookup(ds, snapname, &value);
17212199Sahrens 	if (err == 0)
17222199Sahrens 		return (EEXIST);
17232199Sahrens 	if (err != ENOENT)
17242199Sahrens 		return (err);
1725789Sahrens 
17263978Smmusante 	/*
17273978Smmusante 	 * Check that the dataset's name is not too long.  Name consists
17283978Smmusante 	 * of the dataset's length + 1 for the @-sign + snapshot name's length
17293978Smmusante 	 */
17303978Smmusante 	if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
17313978Smmusante 		return (ENAMETOOLONG);
17323978Smmusante 
17335378Sck153898 	err = dsl_dataset_snapshot_reserve_space(ds, tx);
17345378Sck153898 	if (err)
17355378Sck153898 		return (err);
17365378Sck153898 
17372199Sahrens 	ds->ds_trysnap_txg = tx->tx_txg;
1738789Sahrens 	return (0);
1739789Sahrens }
1740789Sahrens 
17412199Sahrens void
17424543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1743789Sahrens {
17445367Sahrens 	dsl_dataset_t *ds = arg1;
17452199Sahrens 	const char *snapname = arg2;
17462199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1747789Sahrens 	dmu_buf_t *dbuf;
1748789Sahrens 	dsl_dataset_phys_t *dsphys;
17497046Sahrens 	uint64_t dsobj, crtxg;
1750789Sahrens 	objset_t *mos = dp->dp_meta_objset;
1751789Sahrens 	int err;
1752789Sahrens 
17532199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1754789Sahrens 
17557046Sahrens 	/*
17567046Sahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
17577046Sahrens 	 */
17587046Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
17597046Sahrens 		crtxg = 1;
17607046Sahrens 	else
17617046Sahrens 		crtxg = tx->tx_txg;
17627046Sahrens 
1763928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1764928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
17651544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1766789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1767789Sahrens 	dsphys = dbuf->db_data;
17686689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
17692199Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1770789Sahrens 	dsphys->ds_fsid_guid = unique_create();
1771789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1772789Sahrens 	    sizeof (dsphys->ds_guid));
1773789Sahrens 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1774789Sahrens 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1775789Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1776789Sahrens 	dsphys->ds_num_children = 1;
1777789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
17787046Sahrens 	dsphys->ds_creation_txg = crtxg;
1779789Sahrens 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1780789Sahrens 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
1781789Sahrens 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1782789Sahrens 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
17832082Seschrock 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1784789Sahrens 	dsphys->ds_bp = ds->ds_phys->ds_bp;
17851544Seschrock 	dmu_buf_rele(dbuf, FTAG);
1786789Sahrens 
17872199Sahrens 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
17882199Sahrens 	if (ds->ds_prev) {
17897046Sahrens 		uint64_t next_clones_obj =
17907046Sahrens 		    ds->ds_prev->ds_phys->ds_next_clones_obj;
17912199Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1792789Sahrens 		    ds->ds_object ||
17932199Sahrens 		    ds->ds_prev->ds_phys->ds_num_children > 1);
17942199Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
17952199Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1796789Sahrens 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
17972199Sahrens 			    ds->ds_prev->ds_phys->ds_creation_txg);
17982199Sahrens 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
17997046Sahrens 		} else if (next_clones_obj != 0) {
18007046Sahrens 			VERIFY3U(0, ==, zap_remove_int(mos,
18017046Sahrens 			    next_clones_obj, dsphys->ds_next_snap_obj, tx));
18027046Sahrens 			VERIFY3U(0, ==, zap_add_int(mos,
18037046Sahrens 			    next_clones_obj, dsobj, tx));
1804789Sahrens 		}
1805789Sahrens 	}
1806789Sahrens 
18075378Sck153898 	/*
18085378Sck153898 	 * If we have a reference-reservation on this dataset, we will
18095378Sck153898 	 * need to increase the amount of refreservation being charged
18105378Sck153898 	 * since our unique space is going to zero.
18115378Sck153898 	 */
18125378Sck153898 	if (ds->ds_reserved) {
18135378Sck153898 		int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
18145378Sck153898 		dsl_dir_diduse_space(ds->ds_dir, add, 0, 0, tx);
18155378Sck153898 	}
18165378Sck153898 
1817789Sahrens 	bplist_close(&ds->ds_deadlist);
1818789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
18195712Sahrens 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1820789Sahrens 	ds->ds_phys->ds_prev_snap_obj = dsobj;
18217046Sahrens 	ds->ds_phys->ds_prev_snap_txg = crtxg;
1822789Sahrens 	ds->ds_phys->ds_unique_bytes = 0;
18235378Sck153898 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
18245378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1825789Sahrens 	ds->ds_phys->ds_deadlist_obj =
1826789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
18271544Seschrock 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
18281544Seschrock 	    ds->ds_phys->ds_deadlist_obj));
1829789Sahrens 
1830789Sahrens 	dprintf("snap '%s' -> obj %llu\n", snapname, dsobj);
1831789Sahrens 	err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1832789Sahrens 	    snapname, 8, 1, &dsobj, tx);
1833789Sahrens 	ASSERT(err == 0);
1834789Sahrens 
1835789Sahrens 	if (ds->ds_prev)
18366689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
18376689Smaybee 	VERIFY(0 == dsl_dataset_get_ref(dp,
18386689Smaybee 	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
18394543Smarks 
18407046Sahrens 	dsl_pool_ds_snapshotted(ds, tx);
18417046Sahrens 
18424543Smarks 	spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr,
18434603Sahrens 	    "dataset = %llu", dsobj);
1844789Sahrens }
1845789Sahrens 
1846789Sahrens void
18473547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1848789Sahrens {
1849789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1850789Sahrens 	ASSERT(ds->ds_user_ptr != NULL);
1851789Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1852789Sahrens 
18534787Sahrens 	/*
18544787Sahrens 	 * in case we had to change ds_fsid_guid when we opened it,
18554787Sahrens 	 * sync it out now.
18564787Sahrens 	 */
18574787Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
18584787Sahrens 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
18594787Sahrens 
1860789Sahrens 	dsl_dir_dirty(ds->ds_dir, tx);
18613547Smaybee 	dmu_objset_sync(ds->ds_user_ptr, zio, tx);
1862789Sahrens }
1863789Sahrens 
1864789Sahrens void
18652885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1866789Sahrens {
18675378Sck153898 	uint64_t refd, avail, uobjs, aobjs;
18685378Sck153898 
18692885Sahrens 	dsl_dir_stats(ds->ds_dir, nv);
1870789Sahrens 
18715378Sck153898 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
18725378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
18735378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
18745378Sck153898 
18752885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
18762885Sahrens 	    ds->ds_phys->ds_creation_time);
18772885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
18782885Sahrens 	    ds->ds_phys->ds_creation_txg);
18795378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
18805378Sck153898 	    ds->ds_quota);
18815378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
18825378Sck153898 	    ds->ds_reserved);
18836643Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
18846643Seschrock 	    ds->ds_phys->ds_guid);
1885789Sahrens 
1886789Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
1887789Sahrens 		/*
1888789Sahrens 		 * This is a snapshot; override the dd's space used with
18892885Sahrens 		 * our unique space and compression ratio.
1890789Sahrens 		 */
18912885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
18922885Sahrens 		    ds->ds_phys->ds_unique_bytes);
18932885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
18942885Sahrens 		    ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
18952885Sahrens 		    (ds->ds_phys->ds_uncompressed_bytes * 100 /
18962885Sahrens 		    ds->ds_phys->ds_compressed_bytes));
1897789Sahrens 	}
1898789Sahrens }
1899789Sahrens 
19002885Sahrens void
19012885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1902789Sahrens {
19032885Sahrens 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
19042885Sahrens 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
19055367Sahrens 	stat->dds_guid = ds->ds_phys->ds_guid;
19062885Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
19072885Sahrens 		stat->dds_is_snapshot = B_TRUE;
19082885Sahrens 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
19092885Sahrens 	}
19102885Sahrens 
19112885Sahrens 	/* clone origin is really a dsl_dir thing... */
19125446Sahrens 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
19137046Sahrens 	if (dsl_dir_is_clone(ds->ds_dir)) {
19142885Sahrens 		dsl_dataset_t *ods;
19152885Sahrens 
19166689Smaybee 		VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
19176689Smaybee 		    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
19185367Sahrens 		dsl_dataset_name(ods, stat->dds_origin);
19196689Smaybee 		dsl_dataset_drop_ref(ods, FTAG);
19202885Sahrens 	}
19215446Sahrens 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
19222885Sahrens }
19232885Sahrens 
19242885Sahrens uint64_t
19252885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
19262885Sahrens {
19274787Sahrens 	return (ds->ds_fsid_guid);
19282885Sahrens }
19292885Sahrens 
19302885Sahrens void
19312885Sahrens dsl_dataset_space(dsl_dataset_t *ds,
19322885Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
19332885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
19342885Sahrens {
19352885Sahrens 	*refdbytesp = ds->ds_phys->ds_used_bytes;
19362885Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
19375378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
19385378Sck153898 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
19395378Sck153898 	if (ds->ds_quota != 0) {
19405378Sck153898 		/*
19415378Sck153898 		 * Adjust available bytes according to refquota
19425378Sck153898 		 */
19435378Sck153898 		if (*refdbytesp < ds->ds_quota)
19445378Sck153898 			*availbytesp = MIN(*availbytesp,
19455378Sck153898 			    ds->ds_quota - *refdbytesp);
19465378Sck153898 		else
19475378Sck153898 			*availbytesp = 0;
19485378Sck153898 	}
19492885Sahrens 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
19502885Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1951789Sahrens }
1952789Sahrens 
19535326Sek110237 boolean_t
19545326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
19555326Sek110237 {
19565326Sek110237 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
19575326Sek110237 
19585326Sek110237 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
19595326Sek110237 	    dsl_pool_sync_context(dp));
19605326Sek110237 	if (ds->ds_prev == NULL)
19615326Sek110237 		return (B_FALSE);
19625326Sek110237 	if (ds->ds_phys->ds_bp.blk_birth >
19635326Sek110237 	    ds->ds_prev->ds_phys->ds_creation_txg)
19645326Sek110237 		return (B_TRUE);
19655326Sek110237 	return (B_FALSE);
19665326Sek110237 }
19675326Sek110237 
19682199Sahrens /* ARGSUSED */
1969789Sahrens static int
19702199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1971789Sahrens {
19722199Sahrens 	dsl_dataset_t *ds = arg1;
19732199Sahrens 	char *newsnapname = arg2;
19742199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
19752199Sahrens 	dsl_dataset_t *hds;
19762199Sahrens 	uint64_t val;
1977789Sahrens 	int err;
1978789Sahrens 
19796689Smaybee 	err = dsl_dataset_hold_obj(dd->dd_pool,
19806689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds);
1981789Sahrens 	if (err)
1982789Sahrens 		return (err);
1983789Sahrens 
19842199Sahrens 	/* new name better not be in use */
19856689Smaybee 	err = dsl_dataset_snap_lookup(hds, newsnapname, &val);
19866689Smaybee 	dsl_dataset_rele(hds, FTAG);
1987789Sahrens 
19882199Sahrens 	if (err == 0)
19892199Sahrens 		err = EEXIST;
19902199Sahrens 	else if (err == ENOENT)
19912199Sahrens 		err = 0;
19924007Smmusante 
19934007Smmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
19944007Smmusante 	if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
19954007Smmusante 		err = ENAMETOOLONG;
19964007Smmusante 
19972199Sahrens 	return (err);
19982199Sahrens }
1999789Sahrens 
20002199Sahrens static void
20014543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2,
20024543Smarks     cred_t *cr, dmu_tx_t *tx)
20032199Sahrens {
20042199Sahrens 	dsl_dataset_t *ds = arg1;
20054543Smarks 	const char *newsnapname = arg2;
20062199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
20072199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
20082199Sahrens 	dsl_dataset_t *hds;
20092199Sahrens 	int err;
2010789Sahrens 
20112199Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2012789Sahrens 
20136689Smaybee 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
20146689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
2015789Sahrens 
20162199Sahrens 	VERIFY(0 == dsl_dataset_get_snapname(ds));
20176689Smaybee 	err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
2018789Sahrens 	ASSERT3U(err, ==, 0);
20192199Sahrens 	mutex_enter(&ds->ds_lock);
20202199Sahrens 	(void) strcpy(ds->ds_snapname, newsnapname);
20212199Sahrens 	mutex_exit(&ds->ds_lock);
20222199Sahrens 	err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
20232199Sahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2024789Sahrens 	ASSERT3U(err, ==, 0);
2025789Sahrens 
20264543Smarks 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
20274543Smarks 	    cr, "dataset = %llu", ds->ds_object);
20286689Smaybee 	dsl_dataset_rele(hds, FTAG);
2029789Sahrens }
2030789Sahrens 
20315326Sek110237 struct renamesnaparg {
20324007Smmusante 	dsl_sync_task_group_t *dstg;
20334007Smmusante 	char failed[MAXPATHLEN];
20344007Smmusante 	char *oldsnap;
20354007Smmusante 	char *newsnap;
20364007Smmusante };
20374007Smmusante 
20384007Smmusante static int
20394007Smmusante dsl_snapshot_rename_one(char *name, void *arg)
20404007Smmusante {
20415326Sek110237 	struct renamesnaparg *ra = arg;
20424007Smmusante 	dsl_dataset_t *ds = NULL;
20434007Smmusante 	char *cp;
20444007Smmusante 	int err;
20454007Smmusante 
20464007Smmusante 	cp = name + strlen(name);
20474007Smmusante 	*cp = '@';
20484007Smmusante 	(void) strcpy(cp + 1, ra->oldsnap);
20494543Smarks 
20504543Smarks 	/*
20514543Smarks 	 * For recursive snapshot renames the parent won't be changing
20524543Smarks 	 * so we just pass name for both the to/from argument.
20534543Smarks 	 */
20544543Smarks 	if (err = zfs_secpolicy_rename_perms(name, name, CRED())) {
20554543Smarks 		(void) strcpy(ra->failed, name);
20564543Smarks 		return (err);
20574543Smarks 	}
20584543Smarks 
20596689Smaybee #ifdef _KERNEL
20606689Smaybee 	/*
20616689Smaybee 	 * For all filesystems undergoing rename, we'll need to unmount it.
20626689Smaybee 	 */
20636689Smaybee 	(void) zfs_unmount_snap(name, NULL);
20646689Smaybee #endif
20656689Smaybee 	err = dsl_dataset_hold(name, ra->dstg, &ds);
20666689Smaybee 	*cp = '\0';
20674007Smmusante 	if (err == ENOENT) {
20684007Smmusante 		return (0);
20696689Smaybee 	} else if (err) {
20704007Smmusante 		(void) strcpy(ra->failed, name);
20714007Smmusante 		return (err);
20724007Smmusante 	}
20734007Smmusante 
20744007Smmusante 	dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
20754007Smmusante 	    dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
20764007Smmusante 
20774007Smmusante 	return (0);
20784007Smmusante }
20794007Smmusante 
20804007Smmusante static int
20814007Smmusante dsl_recursive_rename(char *oldname, const char *newname)
20824007Smmusante {
20834007Smmusante 	int err;
20845326Sek110237 	struct renamesnaparg *ra;
20854007Smmusante 	dsl_sync_task_t *dst;
20864007Smmusante 	spa_t *spa;
20874007Smmusante 	char *cp, *fsname = spa_strdup(oldname);
20884007Smmusante 	int len = strlen(oldname);
20894007Smmusante 
20904007Smmusante 	/* truncate the snapshot name to get the fsname */
20914007Smmusante 	cp = strchr(fsname, '@');
20924007Smmusante 	*cp = '\0';
20934007Smmusante 
20944603Sahrens 	err = spa_open(fsname, &spa, FTAG);
20954007Smmusante 	if (err) {
20964007Smmusante 		kmem_free(fsname, len + 1);
20974007Smmusante 		return (err);
20984007Smmusante 	}
20995326Sek110237 	ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP);
21004007Smmusante 	ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
21014007Smmusante 
21024007Smmusante 	ra->oldsnap = strchr(oldname, '@') + 1;
21034007Smmusante 	ra->newsnap = strchr(newname, '@') + 1;
21044007Smmusante 	*ra->failed = '\0';
21054007Smmusante 
21064007Smmusante 	err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
21074007Smmusante 	    DS_FIND_CHILDREN);
21084007Smmusante 	kmem_free(fsname, len + 1);
21094007Smmusante 
21104007Smmusante 	if (err == 0) {
21114007Smmusante 		err = dsl_sync_task_group_wait(ra->dstg);
21124007Smmusante 	}
21134007Smmusante 
21144007Smmusante 	for (dst = list_head(&ra->dstg->dstg_tasks); dst;
21154007Smmusante 	    dst = list_next(&ra->dstg->dstg_tasks, dst)) {
21164007Smmusante 		dsl_dataset_t *ds = dst->dst_arg1;
21174007Smmusante 		if (dst->dst_err) {
21184007Smmusante 			dsl_dir_name(ds->ds_dir, ra->failed);
21194009Smmusante 			(void) strcat(ra->failed, "@");
21204009Smmusante 			(void) strcat(ra->failed, ra->newsnap);
21214007Smmusante 		}
21226689Smaybee 		dsl_dataset_rele(ds, ra->dstg);
21234007Smmusante 	}
21244007Smmusante 
21254543Smarks 	if (err)
21264543Smarks 		(void) strcpy(oldname, ra->failed);
21274007Smmusante 
21284007Smmusante 	dsl_sync_task_group_destroy(ra->dstg);
21295326Sek110237 	kmem_free(ra, sizeof (struct renamesnaparg));
21304007Smmusante 	spa_close(spa, FTAG);
21314007Smmusante 	return (err);
21324007Smmusante }
21334007Smmusante 
21344569Smmusante static int
21354569Smmusante dsl_valid_rename(char *oldname, void *arg)
21364569Smmusante {
21374569Smmusante 	int delta = *(int *)arg;
21384569Smmusante 
21394569Smmusante 	if (strlen(oldname) + delta >= MAXNAMELEN)
21404569Smmusante 		return (ENAMETOOLONG);
21414569Smmusante 
21424569Smmusante 	return (0);
21434569Smmusante }
21444569Smmusante 
2145789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename
2146789Sahrens int
21476689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive)
2148789Sahrens {
2149789Sahrens 	dsl_dir_t *dd;
21502199Sahrens 	dsl_dataset_t *ds;
2151789Sahrens 	const char *tail;
2152789Sahrens 	int err;
2153789Sahrens 
21542199Sahrens 	err = dsl_dir_open(oldname, FTAG, &dd, &tail);
21551544Seschrock 	if (err)
21561544Seschrock 		return (err);
2157789Sahrens 	if (tail == NULL) {
21584569Smmusante 		int delta = strlen(newname) - strlen(oldname);
21594569Smmusante 
21607046Sahrens 		/* if we're growing, validate child name lengths */
21614569Smmusante 		if (delta > 0)
21624569Smmusante 			err = dmu_objset_find(oldname, dsl_valid_rename,
21634569Smmusante 			    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
21644569Smmusante 
21654569Smmusante 		if (!err)
21664569Smmusante 			err = dsl_dir_rename(dd, newname);
2167789Sahrens 		dsl_dir_close(dd, FTAG);
2168789Sahrens 		return (err);
2169789Sahrens 	}
2170789Sahrens 	if (tail[0] != '@') {
2171789Sahrens 		/* the name ended in a nonexistant component */
2172789Sahrens 		dsl_dir_close(dd, FTAG);
2173789Sahrens 		return (ENOENT);
2174789Sahrens 	}
2175789Sahrens 
21762199Sahrens 	dsl_dir_close(dd, FTAG);
21772199Sahrens 
21782199Sahrens 	/* new name must be snapshot in same filesystem */
21792199Sahrens 	tail = strchr(newname, '@');
21802199Sahrens 	if (tail == NULL)
21812199Sahrens 		return (EINVAL);
21822199Sahrens 	tail++;
21832199Sahrens 	if (strncmp(oldname, newname, tail - newname) != 0)
21842199Sahrens 		return (EXDEV);
2185789Sahrens 
21864007Smmusante 	if (recursive) {
21874007Smmusante 		err = dsl_recursive_rename(oldname, newname);
21884007Smmusante 	} else {
21896689Smaybee 		err = dsl_dataset_hold(oldname, FTAG, &ds);
21904007Smmusante 		if (err)
21914007Smmusante 			return (err);
21922199Sahrens 
21934007Smmusante 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
21944007Smmusante 		    dsl_dataset_snapshot_rename_check,
21954007Smmusante 		    dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
21962199Sahrens 
21976689Smaybee 		dsl_dataset_rele(ds, FTAG);
21984007Smmusante 	}
21992199Sahrens 
2200789Sahrens 	return (err);
2201789Sahrens }
22022082Seschrock 
22037046Sahrens struct promotenode {
22046689Smaybee 	list_node_t link;
22056689Smaybee 	dsl_dataset_t *ds;
22066689Smaybee };
22076689Smaybee 
22082199Sahrens struct promotearg {
22096689Smaybee 	list_t snap_list;
22106689Smaybee 	dsl_dataset_t *clone_origin, *old_head;
22112199Sahrens 	uint64_t used, comp, uncomp, unique;
22126689Smaybee 	uint64_t newnext_obj;
22132199Sahrens };
22142199Sahrens 
22154543Smarks /* ARGSUSED */
22162082Seschrock static int
22172199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
22182082Seschrock {
22192199Sahrens 	dsl_dataset_t *hds = arg1;
22202199Sahrens 	struct promotearg *pa = arg2;
22217046Sahrens 	struct promotenode *snap = list_head(&pa->snap_list);
22222199Sahrens 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
22236689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
22246689Smaybee 	dsl_dataset_t *newnext_ds;
22256689Smaybee 	char *name;
22262199Sahrens 	uint64_t itor = 0;
22272082Seschrock 	blkptr_t bp;
22286689Smaybee 	int err;
22292199Sahrens 
22307046Sahrens 	/* Check that it is a real clone */
22317046Sahrens 	if (!dsl_dir_is_clone(hds->ds_dir))
22322082Seschrock 		return (EINVAL);
22332082Seschrock 
22342199Sahrens 	/* Since this is so expensive, don't do the preliminary check */
22352199Sahrens 	if (!dmu_tx_is_syncing(tx))
22362199Sahrens 		return (0);
22372199Sahrens 
22386689Smaybee 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)
22396689Smaybee 		return (EXDEV);
22402082Seschrock 
22415367Sahrens 	/* find origin's new next ds */
22426689Smaybee 	newnext_ds = hds;
22435367Sahrens 	while (newnext_ds->ds_phys->ds_prev_snap_obj != origin_ds->ds_object) {
22442082Seschrock 		dsl_dataset_t *prev;
22452082Seschrock 
22466689Smaybee 		err = dsl_dataset_hold_obj(dp,
22476689Smaybee 		    newnext_ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
22486689Smaybee 		if (newnext_ds != hds)
22496689Smaybee 			dsl_dataset_rele(newnext_ds, FTAG);
22506689Smaybee 		if (err)
22516689Smaybee 			return (err);
22522082Seschrock 		newnext_ds = prev;
22532082Seschrock 	}
22542199Sahrens 	pa->newnext_obj = newnext_ds->ds_object;
22552082Seschrock 
22565367Sahrens 	/* compute origin's new unique space */
22576689Smaybee 	pa->unique = 0;
22582082Seschrock 	while ((err = bplist_iterate(&newnext_ds->ds_deadlist,
22592082Seschrock 	    &itor, &bp)) == 0) {
22605367Sahrens 		if (bp.blk_birth > origin_ds->ds_phys->ds_prev_snap_txg)
22616689Smaybee 			pa->unique += bp_get_dasize(dp->dp_spa, &bp);
22622082Seschrock 	}
22636689Smaybee 	if (newnext_ds != hds)
22646689Smaybee 		dsl_dataset_rele(newnext_ds, FTAG);
22652082Seschrock 	if (err != ENOENT)
22666689Smaybee 		return (err);
22676689Smaybee 
22682082Seschrock 	name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
22696689Smaybee 
22706689Smaybee 	/*
22716689Smaybee 	 * Walk the snapshots that we are moving
22726689Smaybee 	 *
22736689Smaybee 	 * Compute space to transfer.  Each snapshot gave birth to:
22746689Smaybee 	 * (my used) - (prev's used) + (deadlist's used)
22756689Smaybee 	 * So a sequence would look like:
22766689Smaybee 	 * uN - u(N-1) + dN + ... + u1 - u0 + d1 + u0 - 0 + d0
22776689Smaybee 	 * Which simplifies to:
22786689Smaybee 	 * uN + dN + ... + d1 + d0
22796689Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
22806689Smaybee 	 * uN + dN + ... + dM - uM-1
22816689Smaybee 	 */
22826689Smaybee 	pa->used = origin_ds->ds_phys->ds_used_bytes;
22836689Smaybee 	pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
22846689Smaybee 	pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
22856689Smaybee 	do {
22862082Seschrock 		uint64_t val, dlused, dlcomp, dluncomp;
22876689Smaybee 		dsl_dataset_t *ds = snap->ds;
22882082Seschrock 
22892082Seschrock 		/* Check that the snapshot name does not conflict */
22902082Seschrock 		dsl_dataset_name(ds, name);
22916689Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
22926689Smaybee 		if (err == 0)
22936689Smaybee 			err = EEXIST;
22946689Smaybee 		if (err != ENOENT)
22952082Seschrock 			break;
22966689Smaybee 		err = 0;
22976689Smaybee 
22986689Smaybee 		/* The very first snapshot does not have a deadlist */
22996689Smaybee 		if (ds->ds_phys->ds_prev_snap_obj != 0) {
23006689Smaybee 			if (err = bplist_space(&ds->ds_deadlist,
23016689Smaybee 			    &dlused, &dlcomp, &dluncomp))
23026689Smaybee 				break;
23036689Smaybee 			pa->used += dlused;
23046689Smaybee 			pa->comp += dlcomp;
23056689Smaybee 			pa->uncomp += dluncomp;
23062082Seschrock 		}
23076689Smaybee 	} while (snap = list_next(&pa->snap_list, snap));
23086689Smaybee 
23096689Smaybee 	/*
23106689Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
23116689Smaybee 	 * so we need to subtract out the clone origin's used space.
23126689Smaybee 	 */
23136689Smaybee 	if (pa->clone_origin) {
23146689Smaybee 		pa->used -= pa->clone_origin->ds_phys->ds_used_bytes;
23156689Smaybee 		pa->comp -= pa->clone_origin->ds_phys->ds_compressed_bytes;
23166689Smaybee 		pa->uncomp -= pa->clone_origin->ds_phys->ds_uncompressed_bytes;
23172082Seschrock 	}
23182082Seschrock 
23196689Smaybee 	kmem_free(name, MAXPATHLEN);
23206689Smaybee 
23212082Seschrock 	/* Check that there is enough space here */
23226689Smaybee 	if (err == 0) {
23236689Smaybee 		dsl_dir_t *odd = origin_ds->ds_dir;
23246689Smaybee 		err = dsl_dir_transfer_possible(odd, hds->ds_dir, pa->used);
23256689Smaybee 	}
23266689Smaybee 
23272199Sahrens 	return (err);
23282199Sahrens }
23292082Seschrock 
23302199Sahrens static void
23314543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
23322199Sahrens {
23332199Sahrens 	dsl_dataset_t *hds = arg1;
23342199Sahrens 	struct promotearg *pa = arg2;
23357046Sahrens 	struct promotenode *snap = list_head(&pa->snap_list);
23366689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
23372199Sahrens 	dsl_dir_t *dd = hds->ds_dir;
23382199Sahrens 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
23395367Sahrens 	dsl_dir_t *odd = NULL;
23402199Sahrens 	char *name;
23417046Sahrens 	uint64_t oldnext_obj;
23422199Sahrens 
23432199Sahrens 	ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
23442199Sahrens 
23452417Sahrens 	/*
23465367Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
23472417Sahrens 	 * changing.
23482417Sahrens 	 */
23495367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object,
23505367Sahrens 	    NULL, FTAG, &odd));
23512082Seschrock 
23526689Smaybee 	/* change origin's next snap */
23536689Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
23547046Sahrens 	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
23556689Smaybee 	origin_ds->ds_phys->ds_next_snap_obj = pa->newnext_obj;
23566689Smaybee 
23577046Sahrens 	/* change the origin's next clone */
23587046Sahrens 	if (origin_ds->ds_phys->ds_next_clones_obj) {
23597046Sahrens 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
23607046Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
23617046Sahrens 		    pa->newnext_obj, tx));
23627046Sahrens 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
23637046Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
23647046Sahrens 		    oldnext_obj, tx));
23657046Sahrens 	}
23667046Sahrens 
23676689Smaybee 	/* change origin */
23686689Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
23696689Smaybee 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
23706689Smaybee 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
23716689Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
23726689Smaybee 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
23736689Smaybee 
23742082Seschrock 	/* move snapshots to this dir */
23752199Sahrens 	name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
23766689Smaybee 	do {
23776689Smaybee 		dsl_dataset_t *ds = snap->ds;
23782082Seschrock 
23792082Seschrock 		/* move snap name entry */
23802082Seschrock 		dsl_dataset_name(ds, name);
23816689Smaybee 		VERIFY(0 == dsl_dataset_snap_remove(pa->old_head,
23826689Smaybee 		    ds->ds_snapname, tx));
23832199Sahrens 		VERIFY(0 == zap_add(dp->dp_meta_objset,
23842082Seschrock 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
23852082Seschrock 		    8, 1, &ds->ds_object, tx));
23862082Seschrock 
23872082Seschrock 		/* change containing dsl_dir */
23882082Seschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
23895367Sahrens 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
23902082Seschrock 		ds->ds_phys->ds_dir_obj = dd->dd_object;
23915367Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
23922082Seschrock 		dsl_dir_close(ds->ds_dir, ds);
23932199Sahrens 		VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
23942082Seschrock 		    NULL, ds, &ds->ds_dir));
23952082Seschrock 
23962082Seschrock 		ASSERT3U(dsl_prop_numcb(ds), ==, 0);
23976689Smaybee 	} while (snap = list_next(&pa->snap_list, snap));
23982082Seschrock 
23992082Seschrock 	/* change space accounting */
24005367Sahrens 	dsl_dir_diduse_space(odd, -pa->used, -pa->comp, -pa->uncomp, tx);
24012199Sahrens 	dsl_dir_diduse_space(dd, pa->used, pa->comp, pa->uncomp, tx);
24025367Sahrens 	origin_ds->ds_phys->ds_unique_bytes = pa->unique;
24032082Seschrock 
24044543Smarks 	/* log history record */
24054543Smarks 	spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
24066689Smaybee 	    cr, "dataset = %llu", hds->ds_object);
24074543Smarks 
24085367Sahrens 	dsl_dir_close(odd, FTAG);
24092199Sahrens 	kmem_free(name, MAXPATHLEN);
24102082Seschrock }
24112082Seschrock 
24122082Seschrock int
24132082Seschrock dsl_dataset_promote(const char *name)
24142082Seschrock {
24152082Seschrock 	dsl_dataset_t *ds;
24166689Smaybee 	dsl_dir_t *dd;
24176689Smaybee 	dsl_pool_t *dp;
24182082Seschrock 	dmu_object_info_t doi;
24192199Sahrens 	struct promotearg pa;
24207046Sahrens 	struct promotenode *snap;
24216689Smaybee 	uint64_t snap_obj;
24226689Smaybee 	uint64_t last_snap = 0;
24236689Smaybee 	int err;
24246689Smaybee 
24256689Smaybee 	err = dsl_dataset_hold(name, FTAG, &ds);
24262082Seschrock 	if (err)
24272082Seschrock 		return (err);
24286689Smaybee 	dd = ds->ds_dir;
24296689Smaybee 	dp = dd->dd_pool;
24306689Smaybee 
24316689Smaybee 	err = dmu_object_info(dp->dp_meta_objset,
24322082Seschrock 	    ds->ds_phys->ds_snapnames_zapobj, &doi);
24332082Seschrock 	if (err) {
24346689Smaybee 		dsl_dataset_rele(ds, FTAG);
24352082Seschrock 		return (err);
24362082Seschrock 	}
24372082Seschrock 
24382082Seschrock 	/*
24396689Smaybee 	 * We are going to inherit all the snapshots taken before our
24406689Smaybee 	 * origin (i.e., our new origin will be our parent's origin).
24416689Smaybee 	 * Take ownership of them so that we can rename them into our
24426689Smaybee 	 * namespace.
24436689Smaybee 	 */
24446689Smaybee 	pa.clone_origin = NULL;
24456689Smaybee 	list_create(&pa.snap_list,
24467046Sahrens 	    sizeof (struct promotenode), offsetof(struct promotenode, link));
24476689Smaybee 	rw_enter(&dp->dp_config_rwlock, RW_READER);
24486689Smaybee 	ASSERT(dd->dd_phys->dd_origin_obj != 0);
24496689Smaybee 	snap_obj = dd->dd_phys->dd_origin_obj;
24506689Smaybee 	while (snap_obj) {
24517046Sahrens 		dsl_dataset_t *snapds;
24527046Sahrens 
24537046Sahrens 		/*
24547046Sahrens 		 * NB: this would be handled by the below check for
24557046Sahrens 		 * clone of a clone, but then we'd always own_obj() the
24567046Sahrens 		 * $ORIGIN, thus causing unnecessary EBUSYs.  We don't
24577046Sahrens 		 * need to set pa.clone_origin because the $ORIGIN has
24587046Sahrens 		 * no data to account for.
24597046Sahrens 		 */
24607046Sahrens 		if (dp->dp_origin_snap &&
24617046Sahrens 		    snap_obj == dp->dp_origin_snap->ds_object)
24627046Sahrens 			break;
24637046Sahrens 
24647046Sahrens 		err = dsl_dataset_own_obj(dp, snap_obj, 0, FTAG, &snapds);
24656689Smaybee 		if (err == ENOENT) {
24666689Smaybee 			/* lost race with snapshot destroy */
24677046Sahrens 			struct promotenode *last = list_tail(&pa.snap_list);
24686689Smaybee 			ASSERT(snap_obj != last->ds->ds_phys->ds_prev_snap_obj);
24696689Smaybee 			snap_obj = last->ds->ds_phys->ds_prev_snap_obj;
24706689Smaybee 			continue;
24716689Smaybee 		} else if (err) {
24726689Smaybee 			rw_exit(&dp->dp_config_rwlock);
24736689Smaybee 			goto out;
24746689Smaybee 		}
24757046Sahrens 
24766689Smaybee 		/*
24776689Smaybee 		 * We could be a clone of a clone.  If we reach our
24786689Smaybee 		 * parent's branch point, we're done.
24796689Smaybee 		 */
24806689Smaybee 		if (last_snap &&
24817046Sahrens 		    snapds->ds_phys->ds_next_snap_obj != last_snap) {
24827046Sahrens 			pa.clone_origin = snapds;
24837046Sahrens 			break;
24846689Smaybee 		}
24857046Sahrens 
24867046Sahrens 		snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP);
24877046Sahrens 		snap->ds = snapds;
24887046Sahrens 		list_insert_tail(&pa.snap_list, snap);
24897046Sahrens 		last_snap = snap_obj;
24907046Sahrens 		snap_obj = snap->ds->ds_phys->ds_prev_snap_obj;
24916689Smaybee 	}
24926689Smaybee 	snap = list_head(&pa.snap_list);
24936689Smaybee 	ASSERT(snap != NULL);
24946689Smaybee 	err = dsl_dataset_hold_obj(dp,
24956689Smaybee 	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &pa.old_head);
24966689Smaybee 	rw_exit(&dp->dp_config_rwlock);
24976689Smaybee 
24986689Smaybee 	if (err)
24996689Smaybee 		goto out;
25006689Smaybee 
25016689Smaybee 	/*
25022082Seschrock 	 * Add in 128x the snapnames zapobj size, since we will be moving
25032082Seschrock 	 * a bunch of snapnames to the promoted ds, and dirtying their
25042082Seschrock 	 * bonus buffers.
25052082Seschrock 	 */
25066689Smaybee 	err = dsl_sync_task_do(dp, dsl_dataset_promote_check,
25072199Sahrens 	    dsl_dataset_promote_sync, ds, &pa, 2 + 2 * doi.doi_physical_blks);
25086689Smaybee 
25096689Smaybee 	dsl_dataset_rele(pa.old_head, FTAG);
25106689Smaybee out:
25116689Smaybee 	while ((snap = list_tail(&pa.snap_list)) != NULL) {
25126689Smaybee 		list_remove(&pa.snap_list, snap);
25136689Smaybee 		dsl_dataset_disown(snap->ds, FTAG);
25147046Sahrens 		kmem_free(snap, sizeof (struct promotenode));
25156689Smaybee 	}
25166689Smaybee 	list_destroy(&pa.snap_list);
25176689Smaybee 	if (pa.clone_origin)
25186689Smaybee 		dsl_dataset_disown(pa.clone_origin, FTAG);
25196689Smaybee 	dsl_dataset_rele(ds, FTAG);
25202082Seschrock 	return (err);
25212082Seschrock }
25223912Slling 
25235367Sahrens struct cloneswaparg {
25245367Sahrens 	dsl_dataset_t *cds; /* clone dataset */
25255367Sahrens 	dsl_dataset_t *ohds; /* origin's head dataset */
25265367Sahrens 	boolean_t force;
25275481Sck153898 	int64_t unused_refres_delta; /* change in unconsumed refreservation */
25285367Sahrens };
25295326Sek110237 
25305326Sek110237 /* ARGSUSED */
25315326Sek110237 static int
25325326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx)
25335326Sek110237 {
25345367Sahrens 	struct cloneswaparg *csa = arg1;
25355326Sek110237 
25365367Sahrens 	/* they should both be heads */
25375367Sahrens 	if (dsl_dataset_is_snapshot(csa->cds) ||
25385367Sahrens 	    dsl_dataset_is_snapshot(csa->ohds))
25395326Sek110237 		return (EINVAL);
25405326Sek110237 
25415367Sahrens 	/* the branch point should be just before them */
25425367Sahrens 	if (csa->cds->ds_prev != csa->ohds->ds_prev)
25435326Sek110237 		return (EINVAL);
25445326Sek110237 
25455367Sahrens 	/* cds should be the clone */
25465367Sahrens 	if (csa->cds->ds_prev->ds_phys->ds_next_snap_obj !=
25475367Sahrens 	    csa->ohds->ds_object)
25485367Sahrens 		return (EINVAL);
25495326Sek110237 
25505367Sahrens 	/* the clone should be a child of the origin */
25515367Sahrens 	if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir)
25525367Sahrens 		return (EINVAL);
25535326Sek110237 
25545367Sahrens 	/* ohds shouldn't be modified unless 'force' */
25555367Sahrens 	if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds))
25565367Sahrens 		return (ETXTBSY);
25575481Sck153898 
25585481Sck153898 	/* adjust amount of any unconsumed refreservation */
25595481Sck153898 	csa->unused_refres_delta =
25605481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
25615481Sck153898 	    csa->ohds->ds_phys->ds_unique_bytes) -
25625481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
25635481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
25645481Sck153898 
25655481Sck153898 	if (csa->unused_refres_delta > 0 &&
25665481Sck153898 	    csa->unused_refres_delta >
25675481Sck153898 	    dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE))
25685481Sck153898 		return (ENOSPC);
25695481Sck153898 
25705367Sahrens 	return (0);
25715326Sek110237 }
25725326Sek110237 
25735326Sek110237 /* ARGSUSED */
25745326Sek110237 static void
25755326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
25765326Sek110237 {
25775367Sahrens 	struct cloneswaparg *csa = arg1;
25785367Sahrens 	dsl_pool_t *dp = csa->cds->ds_dir->dd_pool;
25795326Sek110237 	uint64_t itor = 0;
25805326Sek110237 	blkptr_t bp;
25815326Sek110237 	uint64_t unique = 0;
25825326Sek110237 	int err;
25835326Sek110237 
25845481Sck153898 	ASSERT(csa->cds->ds_reserved == 0);
25855481Sck153898 	ASSERT(csa->cds->ds_quota == csa->ohds->ds_quota);
25865481Sck153898 
25875367Sahrens 	dmu_buf_will_dirty(csa->cds->ds_dbuf, tx);
25885367Sahrens 	dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx);
25895367Sahrens 	dmu_buf_will_dirty(csa->cds->ds_prev->ds_dbuf, tx);
25905326Sek110237 
25915367Sahrens 	if (csa->cds->ds_user_ptr != NULL) {
25925367Sahrens 		csa->cds->ds_user_evict_func(csa->cds, csa->cds->ds_user_ptr);
25935367Sahrens 		csa->cds->ds_user_ptr = NULL;
25945367Sahrens 	}
25955326Sek110237 
25965367Sahrens 	if (csa->ohds->ds_user_ptr != NULL) {
25975367Sahrens 		csa->ohds->ds_user_evict_func(csa->ohds,
25985367Sahrens 		    csa->ohds->ds_user_ptr);
25995367Sahrens 		csa->ohds->ds_user_ptr = NULL;
26005367Sahrens 	}
26015326Sek110237 
26025326Sek110237 	/* compute unique space */
26035367Sahrens 	while ((err = bplist_iterate(&csa->cds->ds_deadlist,
26045367Sahrens 	    &itor, &bp)) == 0) {
26055367Sahrens 		if (bp.blk_birth > csa->cds->ds_prev->ds_phys->ds_prev_snap_txg)
26065367Sahrens 			unique += bp_get_dasize(dp->dp_spa, &bp);
26075326Sek110237 	}
26085326Sek110237 	VERIFY(err == ENOENT);
26095326Sek110237 
26105326Sek110237 	/* reset origin's unique bytes */
26115367Sahrens 	csa->cds->ds_prev->ds_phys->ds_unique_bytes = unique;
26125326Sek110237 
26135326Sek110237 	/* swap blkptrs */
26145326Sek110237 	{
26155326Sek110237 		blkptr_t tmp;
26165367Sahrens 		tmp = csa->ohds->ds_phys->ds_bp;
26175367Sahrens 		csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp;
26185367Sahrens 		csa->cds->ds_phys->ds_bp = tmp;
26195326Sek110237 	}
26205326Sek110237 
26215326Sek110237 	/* set dd_*_bytes */
26225326Sek110237 	{
26235326Sek110237 		int64_t dused, dcomp, duncomp;
26245326Sek110237 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
26255326Sek110237 		uint64_t odl_used, odl_comp, odl_uncomp;
26265326Sek110237 
26275367Sahrens 		VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used,
26285326Sek110237 		    &cdl_comp, &cdl_uncomp));
26295367Sahrens 		VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used,
26305326Sek110237 		    &odl_comp, &odl_uncomp));
26315367Sahrens 		dused = csa->cds->ds_phys->ds_used_bytes + cdl_used -
26325367Sahrens 		    (csa->ohds->ds_phys->ds_used_bytes + odl_used);
26335367Sahrens 		dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
26345367Sahrens 		    (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
26355367Sahrens 		duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
26365367Sahrens 		    cdl_uncomp -
26375367Sahrens 		    (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
26385326Sek110237 
26395367Sahrens 		dsl_dir_diduse_space(csa->ohds->ds_dir,
26405367Sahrens 		    dused, dcomp, duncomp, tx);
26415367Sahrens 		dsl_dir_diduse_space(csa->cds->ds_dir,
26425367Sahrens 		    -dused, -dcomp, -duncomp, tx);
26435367Sahrens 	}
26445367Sahrens 
26455367Sahrens #define	SWITCH64(x, y) \
26465367Sahrens 	{ \
26475367Sahrens 		uint64_t __tmp = (x); \
26485367Sahrens 		(x) = (y); \
26495367Sahrens 		(y) = __tmp; \
26505326Sek110237 	}
26515326Sek110237 
26525326Sek110237 	/* swap ds_*_bytes */
26535367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_used_bytes,
26545367Sahrens 	    csa->cds->ds_phys->ds_used_bytes);
26555367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
26565367Sahrens 	    csa->cds->ds_phys->ds_compressed_bytes);
26575367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
26585367Sahrens 	    csa->cds->ds_phys->ds_uncompressed_bytes);
26595481Sck153898 	SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
26605481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
26615481Sck153898 
26625481Sck153898 	/* apply any parent delta for change in unconsumed refreservation */
26635481Sck153898 	dsl_dir_diduse_space(csa->ohds->ds_dir, csa->unused_refres_delta,
26645481Sck153898 	    0, 0, tx);
26655326Sek110237 
26665326Sek110237 	/* swap deadlists */
26675367Sahrens 	bplist_close(&csa->cds->ds_deadlist);
26685367Sahrens 	bplist_close(&csa->ohds->ds_deadlist);
26695367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
26705367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj);
26715367Sahrens 	VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
26725367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj));
26735367Sahrens 	VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
26745367Sahrens 	    csa->ohds->ds_phys->ds_deadlist_obj));
26755326Sek110237 }
26765326Sek110237 
26775326Sek110237 /*
26786689Smaybee  * Swap 'clone' with its origin head file system.  Used at the end
26796689Smaybee  * of "online recv" to swizzle the file system to the new version.
26805326Sek110237  */
26815326Sek110237 int
26825367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
26835367Sahrens     boolean_t force)
26845326Sek110237 {
26855367Sahrens 	struct cloneswaparg csa;
26866689Smaybee 	int error;
26876689Smaybee 
26886689Smaybee 	ASSERT(clone->ds_owner);
26896689Smaybee 	ASSERT(origin_head->ds_owner);
26906689Smaybee retry:
26916689Smaybee 	/* Need exclusive access for the swap */
26926689Smaybee 	rw_enter(&clone->ds_rwlock, RW_WRITER);
26936689Smaybee 	if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) {
26946689Smaybee 		rw_exit(&clone->ds_rwlock);
26956689Smaybee 		rw_enter(&origin_head->ds_rwlock, RW_WRITER);
26966689Smaybee 		if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) {
26976689Smaybee 			rw_exit(&origin_head->ds_rwlock);
26986689Smaybee 			goto retry;
26996689Smaybee 		}
27006689Smaybee 	}
27015367Sahrens 	csa.cds = clone;
27025367Sahrens 	csa.ohds = origin_head;
27035367Sahrens 	csa.force = force;
27046689Smaybee 	error = dsl_sync_task_do(clone->ds_dir->dd_pool,
27055326Sek110237 	    dsl_dataset_clone_swap_check,
27066689Smaybee 	    dsl_dataset_clone_swap_sync, &csa, NULL, 9);
27076689Smaybee 	return (error);
27085326Sek110237 }
27095326Sek110237 
27103912Slling /*
27113912Slling  * Given a pool name and a dataset object number in that pool,
27123912Slling  * return the name of that dataset.
27133912Slling  */
27143912Slling int
27153912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
27163912Slling {
27173912Slling 	spa_t *spa;
27183912Slling 	dsl_pool_t *dp;
27196689Smaybee 	dsl_dataset_t *ds;
27203912Slling 	int error;
27213912Slling 
27223912Slling 	if ((error = spa_open(pname, &spa, FTAG)) != 0)
27233912Slling 		return (error);
27243912Slling 	dp = spa_get_dsl(spa);
27253912Slling 	rw_enter(&dp->dp_config_rwlock, RW_READER);
27266689Smaybee 	if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) {
27276689Smaybee 		dsl_dataset_name(ds, buf);
27286689Smaybee 		dsl_dataset_rele(ds, FTAG);
27293912Slling 	}
27303912Slling 	rw_exit(&dp->dp_config_rwlock);
27313912Slling 	spa_close(spa, FTAG);
27323912Slling 
27336689Smaybee 	return (error);
27343912Slling }
27355378Sck153898 
27365378Sck153898 int
27375378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
27386689Smaybee     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
27395378Sck153898 {
27405378Sck153898 	int error = 0;
27415378Sck153898 
27425378Sck153898 	ASSERT3S(asize, >, 0);
27435378Sck153898 
27445831Sck153898 	/*
27455831Sck153898 	 * *ref_rsrv is the portion of asize that will come from any
27465831Sck153898 	 * unconsumed refreservation space.
27475831Sck153898 	 */
27485831Sck153898 	*ref_rsrv = 0;
27495831Sck153898 
27505378Sck153898 	mutex_enter(&ds->ds_lock);
27515378Sck153898 	/*
27525378Sck153898 	 * Make a space adjustment for reserved bytes.
27535378Sck153898 	 */
27545378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
27555378Sck153898 		ASSERT3U(*used, >=,
27565378Sck153898 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
27575378Sck153898 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
27585831Sck153898 		*ref_rsrv =
27595831Sck153898 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
27605378Sck153898 	}
27615378Sck153898 
27625378Sck153898 	if (!check_quota || ds->ds_quota == 0) {
27635378Sck153898 		mutex_exit(&ds->ds_lock);
27645378Sck153898 		return (0);
27655378Sck153898 	}
27665378Sck153898 	/*
27675378Sck153898 	 * If they are requesting more space, and our current estimate
27685378Sck153898 	 * is over quota, they get to try again unless the actual
27695378Sck153898 	 * on-disk is over quota and there are no pending changes (which
27705378Sck153898 	 * may free up space for us).
27715378Sck153898 	 */
27725378Sck153898 	if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) {
27735378Sck153898 		if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota)
27745378Sck153898 			error = ERESTART;
27755378Sck153898 		else
27765378Sck153898 			error = EDQUOT;
27775378Sck153898 	}
27785378Sck153898 	mutex_exit(&ds->ds_lock);
27795378Sck153898 
27805378Sck153898 	return (error);
27815378Sck153898 }
27825378Sck153898 
27835378Sck153898 /* ARGSUSED */
27845378Sck153898 static int
27855378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
27865378Sck153898 {
27875378Sck153898 	dsl_dataset_t *ds = arg1;
27885378Sck153898 	uint64_t *quotap = arg2;
27895378Sck153898 	uint64_t new_quota = *quotap;
27905378Sck153898 
27915378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA)
27925378Sck153898 		return (ENOTSUP);
27935378Sck153898 
27945378Sck153898 	if (new_quota == 0)
27955378Sck153898 		return (0);
27965378Sck153898 
27975378Sck153898 	if (new_quota < ds->ds_phys->ds_used_bytes ||
27985378Sck153898 	    new_quota < ds->ds_reserved)
27995378Sck153898 		return (ENOSPC);
28005378Sck153898 
28015378Sck153898 	return (0);
28025378Sck153898 }
28035378Sck153898 
28045378Sck153898 /* ARGSUSED */
28055378Sck153898 void
28065378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
28075378Sck153898 {
28085378Sck153898 	dsl_dataset_t *ds = arg1;
28095378Sck153898 	uint64_t *quotap = arg2;
28105378Sck153898 	uint64_t new_quota = *quotap;
28115378Sck153898 
28125378Sck153898 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
28135378Sck153898 
28145378Sck153898 	ds->ds_quota = new_quota;
28155378Sck153898 
28165378Sck153898 	dsl_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx);
28175378Sck153898 
28185378Sck153898 	spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa,
28195378Sck153898 	    tx, cr, "%lld dataset = %llu ",
28206689Smaybee 	    (longlong_t)new_quota, ds->ds_object);
28215378Sck153898 }
28225378Sck153898 
28235378Sck153898 int
28245378Sck153898 dsl_dataset_set_quota(const char *dsname, uint64_t quota)
28255378Sck153898 {
28265378Sck153898 	dsl_dataset_t *ds;
28275378Sck153898 	int err;
28285378Sck153898 
28296689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
28305378Sck153898 	if (err)
28315378Sck153898 		return (err);
28325378Sck153898 
28335481Sck153898 	if (quota != ds->ds_quota) {
28345481Sck153898 		/*
28355481Sck153898 		 * If someone removes a file, then tries to set the quota, we
28365481Sck153898 		 * want to make sure the file freeing takes effect.
28375481Sck153898 		 */
28385481Sck153898 		txg_wait_open(ds->ds_dir->dd_pool, 0);
28395481Sck153898 
28405481Sck153898 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
28415481Sck153898 		    dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync,
28425481Sck153898 		    ds, &quota, 0);
28435481Sck153898 	}
28446689Smaybee 	dsl_dataset_rele(ds, FTAG);
28455378Sck153898 	return (err);
28465378Sck153898 }
28475378Sck153898 
28485378Sck153898 static int
28495378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
28505378Sck153898 {
28515378Sck153898 	dsl_dataset_t *ds = arg1;
28525378Sck153898 	uint64_t *reservationp = arg2;
28535378Sck153898 	uint64_t new_reservation = *reservationp;
28545378Sck153898 	int64_t delta;
28555378Sck153898 	uint64_t unique;
28565378Sck153898 
28575378Sck153898 	if (new_reservation > INT64_MAX)
28585378Sck153898 		return (EOVERFLOW);
28595378Sck153898 
28605378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
28615378Sck153898 	    SPA_VERSION_REFRESERVATION)
28625378Sck153898 		return (ENOTSUP);
28635378Sck153898 
28645378Sck153898 	if (dsl_dataset_is_snapshot(ds))
28655378Sck153898 		return (EINVAL);
28665378Sck153898 
28675378Sck153898 	/*
28685378Sck153898 	 * If we are doing the preliminary check in open context, the
28695378Sck153898 	 * space estimates may be inaccurate.
28705378Sck153898 	 */
28715378Sck153898 	if (!dmu_tx_is_syncing(tx))
28725378Sck153898 		return (0);
28735378Sck153898 
28745378Sck153898 	mutex_enter(&ds->ds_lock);
28755378Sck153898 	unique = dsl_dataset_unique(ds);
28765378Sck153898 	delta = MAX(unique, new_reservation) - MAX(unique, ds->ds_reserved);
28775378Sck153898 	mutex_exit(&ds->ds_lock);
28785378Sck153898 
28795378Sck153898 	if (delta > 0 &&
28805378Sck153898 	    delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
28815378Sck153898 		return (ENOSPC);
28825378Sck153898 	if (delta > 0 && ds->ds_quota > 0 &&
28835378Sck153898 	    new_reservation > ds->ds_quota)
28845378Sck153898 		return (ENOSPC);
28855378Sck153898 
28865378Sck153898 	return (0);
28875378Sck153898 }
28885378Sck153898 
28895378Sck153898 /* ARGSUSED */
28905378Sck153898 static void
28915378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr,
28925378Sck153898     dmu_tx_t *tx)
28935378Sck153898 {
28945378Sck153898 	dsl_dataset_t *ds = arg1;
28955378Sck153898 	uint64_t *reservationp = arg2;
28965378Sck153898 	uint64_t new_reservation = *reservationp;
28975378Sck153898 	uint64_t unique;
28985378Sck153898 	int64_t delta;
28995378Sck153898 
29005378Sck153898 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
29015378Sck153898 
29025378Sck153898 	mutex_enter(&ds->ds_lock);
29035378Sck153898 	unique = dsl_dataset_unique(ds);
29045378Sck153898 	delta = MAX(0, (int64_t)(new_reservation - unique)) -
29055378Sck153898 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
29065378Sck153898 	ds->ds_reserved = new_reservation;
29075378Sck153898 	mutex_exit(&ds->ds_lock);
29085378Sck153898 
29095378Sck153898 	dsl_prop_set_uint64_sync(ds->ds_dir, "refreservation",
29105378Sck153898 	    new_reservation, cr, tx);
29115378Sck153898 
29125378Sck153898 	dsl_dir_diduse_space(ds->ds_dir, delta, 0, 0, tx);
29135378Sck153898 
29145378Sck153898 	spa_history_internal_log(LOG_DS_REFRESERV,
29155378Sck153898 	    ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu",
29165378Sck153898 	    (longlong_t)new_reservation,
29175378Sck153898 	    ds->ds_dir->dd_phys->dd_head_dataset_obj);
29185378Sck153898 }
29195378Sck153898 
29205378Sck153898 int
29215378Sck153898 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation)
29225378Sck153898 {
29235378Sck153898 	dsl_dataset_t *ds;
29245378Sck153898 	int err;
29255378Sck153898 
29266689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
29275378Sck153898 	if (err)
29285378Sck153898 		return (err);
29295378Sck153898 
29305378Sck153898 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
29315378Sck153898 	    dsl_dataset_set_reservation_check,
29325378Sck153898 	    dsl_dataset_set_reservation_sync, ds, &reservation, 0);
29336689Smaybee 	dsl_dataset_rele(ds, FTAG);
29345378Sck153898 	return (err);
29355378Sck153898 }
2936