xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision 7312:3992f9a413c2)
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 #include <sys/dmu_objset.h>
27789Sahrens #include <sys/dsl_dataset.h>
28789Sahrens #include <sys/dsl_dir.h>
292082Seschrock #include <sys/dsl_prop.h>
302199Sahrens #include <sys/dsl_synctask.h>
31789Sahrens #include <sys/dmu_traverse.h>
32789Sahrens #include <sys/dmu_tx.h>
33789Sahrens #include <sys/arc.h>
34789Sahrens #include <sys/zio.h>
35789Sahrens #include <sys/zap.h>
36789Sahrens #include <sys/unique.h>
37789Sahrens #include <sys/zfs_context.h>
384007Smmusante #include <sys/zfs_ioctl.h>
394543Smarks #include <sys/spa.h>
407046Sahrens #include <sys/zfs_znode.h>
414543Smarks #include <sys/sunddi.h>
42789Sahrens 
436689Smaybee static char *dsl_reaper = "the grim reaper";
446689Smaybee 
452199Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check;
462199Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync;
472199Sahrens static dsl_checkfunc_t dsl_dataset_rollback_check;
482199Sahrens static dsl_syncfunc_t dsl_dataset_rollback_sync;
495378Sck153898 static dsl_syncfunc_t dsl_dataset_set_reservation_sync;
501731Sbonwick 
513444Sek110237 #define	DS_REF_MAX	(1ULL << 62)
52789Sahrens 
53789Sahrens #define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
54789Sahrens 
556689Smaybee #define	DSL_DATASET_IS_DESTROYED(ds)	((ds)->ds_owner == dsl_reaper)
566689Smaybee 
57789Sahrens 
585378Sck153898 /*
595378Sck153898  * Figure out how much of this delta should be propogated to the dsl_dir
605378Sck153898  * layer.  If there's a refreservation, that space has already been
615378Sck153898  * partially accounted for in our ancestors.
625378Sck153898  */
635378Sck153898 static int64_t
645378Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta)
655378Sck153898 {
665378Sck153898 	uint64_t old_bytes, new_bytes;
675378Sck153898 
685378Sck153898 	if (ds->ds_reserved == 0)
695378Sck153898 		return (delta);
705378Sck153898 
715378Sck153898 	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
725378Sck153898 	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
735378Sck153898 
745378Sck153898 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
755378Sck153898 	return (new_bytes - old_bytes);
765378Sck153898 }
77789Sahrens 
78789Sahrens void
79789Sahrens dsl_dataset_block_born(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
80789Sahrens {
812082Seschrock 	int used = bp_get_dasize(tx->tx_pool->dp_spa, bp);
82789Sahrens 	int compressed = BP_GET_PSIZE(bp);
83789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
845378Sck153898 	int64_t delta;
85789Sahrens 
86789Sahrens 	dprintf_bp(bp, "born, ds=%p\n", ds);
87789Sahrens 
88789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
89789Sahrens 	/* It could have been compressed away to nothing */
90789Sahrens 	if (BP_IS_HOLE(bp))
91789Sahrens 		return;
92789Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
93789Sahrens 	ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES);
94789Sahrens 	if (ds == NULL) {
95789Sahrens 		/*
96789Sahrens 		 * Account for the meta-objset space in its placeholder
97789Sahrens 		 * dsl_dir.
98789Sahrens 		 */
99789Sahrens 		ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */
100789Sahrens 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir,
101789Sahrens 		    used, compressed, uncompressed, tx);
102789Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
103789Sahrens 		return;
104789Sahrens 	}
105789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
106789Sahrens 	mutex_enter(&ds->ds_lock);
1075378Sck153898 	delta = parent_delta(ds, used);
108789Sahrens 	ds->ds_phys->ds_used_bytes += used;
109789Sahrens 	ds->ds_phys->ds_compressed_bytes += compressed;
110789Sahrens 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
111789Sahrens 	ds->ds_phys->ds_unique_bytes += used;
112789Sahrens 	mutex_exit(&ds->ds_lock);
1135378Sck153898 	dsl_dir_diduse_space(ds->ds_dir, delta, compressed, uncompressed, tx);
114789Sahrens }
115789Sahrens 
1166992Smaybee int
1173547Smaybee dsl_dataset_block_kill(dsl_dataset_t *ds, blkptr_t *bp, zio_t *pio,
1183547Smaybee     dmu_tx_t *tx)
119789Sahrens {
1202082Seschrock 	int used = bp_get_dasize(tx->tx_pool->dp_spa, bp);
121789Sahrens 	int compressed = BP_GET_PSIZE(bp);
122789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
123789Sahrens 
124789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1253547Smaybee 	/* No block pointer => nothing to free */
126789Sahrens 	if (BP_IS_HOLE(bp))
1276992Smaybee 		return (0);
128789Sahrens 
129789Sahrens 	ASSERT(used > 0);
130789Sahrens 	if (ds == NULL) {
1313547Smaybee 		int err;
132789Sahrens 		/*
133789Sahrens 		 * Account for the meta-objset space in its placeholder
134789Sahrens 		 * dataset.
135789Sahrens 		 */
1367046Sahrens 		err = dsl_free(pio, tx->tx_pool,
1373547Smaybee 		    tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT);
1383547Smaybee 		ASSERT(err == 0);
139789Sahrens 
140789Sahrens 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir,
141789Sahrens 		    -used, -compressed, -uncompressed, tx);
142789Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
1436992Smaybee 		return (used);
144789Sahrens 	}
145789Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
146789Sahrens 
147789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
148789Sahrens 
149789Sahrens 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
1503547Smaybee 		int err;
1515378Sck153898 		int64_t delta;
1523547Smaybee 
153789Sahrens 		dprintf_bp(bp, "freeing: %s", "");
1547046Sahrens 		err = dsl_free(pio, tx->tx_pool,
1553547Smaybee 		    tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT);
1563547Smaybee 		ASSERT(err == 0);
157789Sahrens 
158789Sahrens 		mutex_enter(&ds->ds_lock);
1595378Sck153898 		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
1605378Sck153898 		    !DS_UNIQUE_IS_ACCURATE(ds));
1615378Sck153898 		delta = parent_delta(ds, -used);
162789Sahrens 		ds->ds_phys->ds_unique_bytes -= used;
163789Sahrens 		mutex_exit(&ds->ds_lock);
164789Sahrens 		dsl_dir_diduse_space(ds->ds_dir,
1655378Sck153898 		    delta, -compressed, -uncompressed, tx);
166789Sahrens 	} else {
167789Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
1681544Seschrock 		VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx));
1695712Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
1705712Sahrens 		    ds->ds_phys->ds_prev_snap_obj);
1715712Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
172789Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
1735712Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
1745712Sahrens 		    ds->ds_object && bp->blk_birth >
1755712Sahrens 		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
1765712Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1775712Sahrens 			mutex_enter(&ds->ds_prev->ds_lock);
1785712Sahrens 			ds->ds_prev->ds_phys->ds_unique_bytes += used;
1795712Sahrens 			mutex_exit(&ds->ds_prev->ds_lock);
180789Sahrens 		}
181789Sahrens 	}
182789Sahrens 	mutex_enter(&ds->ds_lock);
183789Sahrens 	ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used);
184789Sahrens 	ds->ds_phys->ds_used_bytes -= used;
185789Sahrens 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
186789Sahrens 	ds->ds_phys->ds_compressed_bytes -= compressed;
187789Sahrens 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
188789Sahrens 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
189789Sahrens 	mutex_exit(&ds->ds_lock);
1906992Smaybee 
1916992Smaybee 	return (used);
192789Sahrens }
193789Sahrens 
1941544Seschrock uint64_t
1951544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
196789Sahrens {
1972885Sahrens 	uint64_t trysnap = 0;
1982885Sahrens 
199789Sahrens 	if (ds == NULL)
2001544Seschrock 		return (0);
201789Sahrens 	/*
202789Sahrens 	 * The snapshot creation could fail, but that would cause an
203789Sahrens 	 * incorrect FALSE return, which would only result in an
204789Sahrens 	 * overestimation of the amount of space that an operation would
205789Sahrens 	 * consume, which is OK.
206789Sahrens 	 *
207789Sahrens 	 * There's also a small window where we could miss a pending
208789Sahrens 	 * snapshot, because we could set the sync task in the quiescing
209789Sahrens 	 * phase.  So this should only be used as a guess.
210789Sahrens 	 */
2112885Sahrens 	if (ds->ds_trysnap_txg >
2122885Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
2132885Sahrens 		trysnap = ds->ds_trysnap_txg;
2142885Sahrens 	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
2151544Seschrock }
2161544Seschrock 
2171544Seschrock int
2181544Seschrock dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth)
2191544Seschrock {
2201544Seschrock 	return (blk_birth > dsl_dataset_prev_snap_txg(ds));
221789Sahrens }
222789Sahrens 
223789Sahrens /* ARGSUSED */
224789Sahrens static void
225789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv)
226789Sahrens {
227789Sahrens 	dsl_dataset_t *ds = dsv;
228789Sahrens 
2296689Smaybee 	ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds));
230789Sahrens 
231789Sahrens 	dprintf_ds(ds, "evicting %s\n", "");
232789Sahrens 
2334787Sahrens 	unique_remove(ds->ds_fsid_guid);
234789Sahrens 
235789Sahrens 	if (ds->ds_user_ptr != NULL)
236789Sahrens 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
237789Sahrens 
238789Sahrens 	if (ds->ds_prev) {
2396689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
240789Sahrens 		ds->ds_prev = NULL;
241789Sahrens 	}
242789Sahrens 
243789Sahrens 	bplist_close(&ds->ds_deadlist);
2446689Smaybee 	if (ds->ds_dir)
2456689Smaybee 		dsl_dir_close(ds->ds_dir, ds);
246789Sahrens 
2474787Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
248789Sahrens 
2492856Snd150628 	mutex_destroy(&ds->ds_lock);
2504787Sahrens 	mutex_destroy(&ds->ds_opening_lock);
2512856Snd150628 	mutex_destroy(&ds->ds_deadlist.bpl_lock);
2526689Smaybee 	rw_destroy(&ds->ds_rwlock);
2536689Smaybee 	cv_destroy(&ds->ds_exclusive_cv);
2542856Snd150628 
255789Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
256789Sahrens }
257789Sahrens 
2581544Seschrock static int
259789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
260789Sahrens {
261789Sahrens 	dsl_dataset_phys_t *headphys;
262789Sahrens 	int err;
263789Sahrens 	dmu_buf_t *headdbuf;
264789Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
265789Sahrens 	objset_t *mos = dp->dp_meta_objset;
266789Sahrens 
267789Sahrens 	if (ds->ds_snapname[0])
2681544Seschrock 		return (0);
269789Sahrens 	if (ds->ds_phys->ds_next_snap_obj == 0)
2701544Seschrock 		return (0);
271789Sahrens 
2721544Seschrock 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
2731544Seschrock 	    FTAG, &headdbuf);
2741544Seschrock 	if (err)
2751544Seschrock 		return (err);
276789Sahrens 	headphys = headdbuf->db_data;
277789Sahrens 	err = zap_value_search(dp->dp_meta_objset,
2784577Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
2791544Seschrock 	dmu_buf_rele(headdbuf, FTAG);
2801544Seschrock 	return (err);
281789Sahrens }
282789Sahrens 
2836492Stimh static int
2846689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
2856492Stimh {
2866689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
2876689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
2886492Stimh 	matchtype_t mt;
2896492Stimh 	int err;
2906492Stimh 
2916689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
2926492Stimh 		mt = MT_FIRST;
2936492Stimh 	else
2946492Stimh 		mt = MT_EXACT;
2956492Stimh 
2966689Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
2976492Stimh 	    value, mt, NULL, 0, NULL);
2986492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
2996689Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
3006492Stimh 	return (err);
3016492Stimh }
3026492Stimh 
3036492Stimh static int
3046689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx)
3056492Stimh {
3066689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3076689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
3086492Stimh 	matchtype_t mt;
3096492Stimh 	int err;
3106492Stimh 
3116689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
3126492Stimh 		mt = MT_FIRST;
3136492Stimh 	else
3146492Stimh 		mt = MT_EXACT;
3156492Stimh 
3166689Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
3176492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
3186689Smaybee 		err = zap_remove(mos, snapobj, name, tx);
3196492Stimh 	return (err);
3206492Stimh }
3216492Stimh 
3226689Smaybee static int
3236689Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag,
3246689Smaybee     dsl_dataset_t **dsp)
325789Sahrens {
326789Sahrens 	objset_t *mos = dp->dp_meta_objset;
327789Sahrens 	dmu_buf_t *dbuf;
328789Sahrens 	dsl_dataset_t *ds;
3291544Seschrock 	int err;
330789Sahrens 
331789Sahrens 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
332789Sahrens 	    dsl_pool_sync_context(dp));
333789Sahrens 
3341544Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
3351544Seschrock 	if (err)
3361544Seschrock 		return (err);
337789Sahrens 	ds = dmu_buf_get_user(dbuf);
338789Sahrens 	if (ds == NULL) {
339789Sahrens 		dsl_dataset_t *winner;
340789Sahrens 
341789Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
342789Sahrens 		ds->ds_dbuf = dbuf;
343789Sahrens 		ds->ds_object = dsobj;
344789Sahrens 		ds->ds_phys = dbuf->db_data;
345789Sahrens 
3462856Snd150628 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
3474787Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
3482856Snd150628 		mutex_init(&ds->ds_deadlist.bpl_lock, NULL, MUTEX_DEFAULT,
3492856Snd150628 		    NULL);
3506689Smaybee 		rw_init(&ds->ds_rwlock, 0, 0, 0);
3516689Smaybee 		cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL);
3522856Snd150628 
3531544Seschrock 		err = bplist_open(&ds->ds_deadlist,
354789Sahrens 		    mos, ds->ds_phys->ds_deadlist_obj);
3551544Seschrock 		if (err == 0) {
3561544Seschrock 			err = dsl_dir_open_obj(dp,
3571544Seschrock 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
3581544Seschrock 		}
3591544Seschrock 		if (err) {
3601544Seschrock 			/*
3611544Seschrock 			 * we don't really need to close the blist if we
3621544Seschrock 			 * just opened it.
3631544Seschrock 			 */
3642856Snd150628 			mutex_destroy(&ds->ds_lock);
3654787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
3662856Snd150628 			mutex_destroy(&ds->ds_deadlist.bpl_lock);
3676689Smaybee 			rw_destroy(&ds->ds_rwlock);
3686689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
3691544Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
3701544Seschrock 			dmu_buf_rele(dbuf, tag);
3711544Seschrock 			return (err);
3721544Seschrock 		}
373789Sahrens 
374789Sahrens 		if (ds->ds_dir->dd_phys->dd_head_dataset_obj == dsobj) {
375789Sahrens 			ds->ds_snapname[0] = '\0';
376789Sahrens 			if (ds->ds_phys->ds_prev_snap_obj) {
3776689Smaybee 				err = dsl_dataset_get_ref(dp,
3786689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
3796689Smaybee 				    ds, &ds->ds_prev);
380789Sahrens 			}
3816689Smaybee 		} else if (zfs_flags & ZFS_DEBUG_SNAPNAMES) {
3826689Smaybee 			err = dsl_dataset_get_snapname(ds);
383789Sahrens 		}
384789Sahrens 
3855475Sck153898 		if (!dsl_dataset_is_snapshot(ds)) {
3865569Sck153898 			/*
3875569Sck153898 			 * In sync context, we're called with either no lock
3885569Sck153898 			 * or with the write lock.  If we're not syncing,
3895569Sck153898 			 * we're always called with the read lock held.
3905569Sck153898 			 */
3915475Sck153898 			boolean_t need_lock =
3925569Sck153898 			    !RW_WRITE_HELD(&dp->dp_config_rwlock) &&
3935569Sck153898 			    dsl_pool_sync_context(dp);
3945475Sck153898 
3955475Sck153898 			if (need_lock)
3965475Sck153898 				rw_enter(&dp->dp_config_rwlock, RW_READER);
3975475Sck153898 
3987265Sahrens 			err = dsl_prop_get_ds(ds,
3995475Sck153898 			    "refreservation", sizeof (uint64_t), 1,
4005475Sck153898 			    &ds->ds_reserved, NULL);
4015475Sck153898 			if (err == 0) {
4027265Sahrens 				err = dsl_prop_get_ds(ds,
4035475Sck153898 				    "refquota", sizeof (uint64_t), 1,
4045475Sck153898 				    &ds->ds_quota, NULL);
4055475Sck153898 			}
4065475Sck153898 
4075475Sck153898 			if (need_lock)
4085475Sck153898 				rw_exit(&dp->dp_config_rwlock);
4095475Sck153898 		} else {
4105475Sck153898 			ds->ds_reserved = ds->ds_quota = 0;
4115475Sck153898 		}
4125475Sck153898 
4131544Seschrock 		if (err == 0) {
4141544Seschrock 			winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys,
4151544Seschrock 			    dsl_dataset_evict);
4161544Seschrock 		}
4171544Seschrock 		if (err || winner) {
418789Sahrens 			bplist_close(&ds->ds_deadlist);
4196689Smaybee 			if (ds->ds_prev)
4206689Smaybee 				dsl_dataset_drop_ref(ds->ds_prev, ds);
421789Sahrens 			dsl_dir_close(ds->ds_dir, ds);
4222856Snd150628 			mutex_destroy(&ds->ds_lock);
4234787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
4242856Snd150628 			mutex_destroy(&ds->ds_deadlist.bpl_lock);
4256689Smaybee 			rw_destroy(&ds->ds_rwlock);
4266689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
427789Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
4281544Seschrock 			if (err) {
4291544Seschrock 				dmu_buf_rele(dbuf, tag);
4301544Seschrock 				return (err);
4311544Seschrock 			}
432789Sahrens 			ds = winner;
433789Sahrens 		} else {
4344787Sahrens 			ds->ds_fsid_guid =
435789Sahrens 			    unique_insert(ds->ds_phys->ds_fsid_guid);
436789Sahrens 		}
437789Sahrens 	}
438789Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
439789Sahrens 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
4407046Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
4417061Sahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
4427077Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
443789Sahrens 	mutex_enter(&ds->ds_lock);
4446689Smaybee 	if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) {
445789Sahrens 		mutex_exit(&ds->ds_lock);
4466689Smaybee 		dmu_buf_rele(ds->ds_dbuf, tag);
4476689Smaybee 		return (ENOENT);
4486689Smaybee 	}
4496689Smaybee 	mutex_exit(&ds->ds_lock);
4506689Smaybee 	*dsp = ds;
4516689Smaybee 	return (0);
4526689Smaybee }
4536689Smaybee 
4546689Smaybee static int
4556689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag)
4566689Smaybee {
4576689Smaybee 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
4586689Smaybee 
4596689Smaybee 	/*
4606689Smaybee 	 * In syncing context we don't want the rwlock lock: there
4616689Smaybee 	 * may be an existing writer waiting for sync phase to
4626689Smaybee 	 * finish.  We don't need to worry about such writers, since
4636689Smaybee 	 * sync phase is single-threaded, so the writer can't be
4646689Smaybee 	 * doing anything while we are active.
4656689Smaybee 	 */
4666689Smaybee 	if (dsl_pool_sync_context(dp)) {
4676689Smaybee 		ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
4686689Smaybee 		return (0);
469789Sahrens 	}
4706689Smaybee 
4716689Smaybee 	/*
4726689Smaybee 	 * Normal users will hold the ds_rwlock as a READER until they
4736689Smaybee 	 * are finished (i.e., call dsl_dataset_rele()).  "Owners" will
4746689Smaybee 	 * drop their READER lock after they set the ds_owner field.
4756689Smaybee 	 *
4766689Smaybee 	 * If the dataset is being destroyed, the destroy thread will
4776689Smaybee 	 * obtain a WRITER lock for exclusive access after it's done its
4786689Smaybee 	 * open-context work and then change the ds_owner to
4796689Smaybee 	 * dsl_reaper once destruction is assured.  So threads
4806689Smaybee 	 * may block here temporarily, until the "destructability" of
4816689Smaybee 	 * the dataset is determined.
4826689Smaybee 	 */
4836689Smaybee 	ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock));
4846689Smaybee 	mutex_enter(&ds->ds_lock);
4856689Smaybee 	while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) {
4866689Smaybee 		rw_exit(&dp->dp_config_rwlock);
4876689Smaybee 		cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock);
4886689Smaybee 		if (DSL_DATASET_IS_DESTROYED(ds)) {
4896689Smaybee 			mutex_exit(&ds->ds_lock);
4906689Smaybee 			dsl_dataset_drop_ref(ds, tag);
4916689Smaybee 			rw_enter(&dp->dp_config_rwlock, RW_READER);
4926689Smaybee 			return (ENOENT);
4936689Smaybee 		}
4946689Smaybee 		rw_enter(&dp->dp_config_rwlock, RW_READER);
4956689Smaybee 	}
496789Sahrens 	mutex_exit(&ds->ds_lock);
4971544Seschrock 	return (0);
498789Sahrens }
499789Sahrens 
500789Sahrens int
5016689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
5026689Smaybee     dsl_dataset_t **dsp)
5036689Smaybee {
5046689Smaybee 	int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp);
5056689Smaybee 
5066689Smaybee 	if (err)
5076689Smaybee 		return (err);
5086689Smaybee 	return (dsl_dataset_hold_ref(*dsp, tag));
5096689Smaybee }
5106689Smaybee 
5116689Smaybee int
5126689Smaybee dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, int flags, void *owner,
5136689Smaybee     dsl_dataset_t **dsp)
5146689Smaybee {
5156689Smaybee 	int err = dsl_dataset_hold_obj(dp, dsobj, owner, dsp);
5166689Smaybee 
5176689Smaybee 	ASSERT(DS_MODE_TYPE(flags) != DS_MODE_USER);
5186689Smaybee 
5196689Smaybee 	if (err)
5206689Smaybee 		return (err);
5216689Smaybee 	if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) {
5226689Smaybee 		dsl_dataset_rele(*dsp, owner);
5236689Smaybee 		return (EBUSY);
5246689Smaybee 	}
5256689Smaybee 	return (0);
5266689Smaybee }
5276689Smaybee 
5286689Smaybee int
5296689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp)
530789Sahrens {
531789Sahrens 	dsl_dir_t *dd;
532789Sahrens 	dsl_pool_t *dp;
5336689Smaybee 	const char *snapname;
534789Sahrens 	uint64_t obj;
535789Sahrens 	int err = 0;
536789Sahrens 
5376689Smaybee 	err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname);
5381544Seschrock 	if (err)
5391544Seschrock 		return (err);
540789Sahrens 
541789Sahrens 	dp = dd->dd_pool;
542789Sahrens 	obj = dd->dd_phys->dd_head_dataset_obj;
543789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
5446689Smaybee 	if (obj)
5456689Smaybee 		err = dsl_dataset_get_ref(dp, obj, tag, dsp);
5466689Smaybee 	else
547789Sahrens 		err = ENOENT;
5486689Smaybee 	if (err)
549789Sahrens 		goto out;
5506689Smaybee 
5516689Smaybee 	err = dsl_dataset_hold_ref(*dsp, tag);
5526689Smaybee 
5536689Smaybee 	/* we may be looking for a snapshot */
5546689Smaybee 	if (err == 0 && snapname != NULL) {
5556689Smaybee 		dsl_dataset_t *ds = NULL;
5566689Smaybee 
5576689Smaybee 		if (*snapname++ != '@') {
5586689Smaybee 			dsl_dataset_rele(*dsp, tag);
559789Sahrens 			err = ENOENT;
560789Sahrens 			goto out;
561789Sahrens 		}
5626689Smaybee 
5636689Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
5646689Smaybee 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
5656689Smaybee 		if (err == 0)
5666689Smaybee 			err = dsl_dataset_get_ref(dp, obj, tag, &ds);
5676689Smaybee 		dsl_dataset_rele(*dsp, tag);
5686689Smaybee 
5696689Smaybee 		ASSERT3U((err == 0), ==, (ds != NULL));
5706689Smaybee 
5716689Smaybee 		if (ds) {
5726689Smaybee 			mutex_enter(&ds->ds_lock);
5736689Smaybee 			if (ds->ds_snapname[0] == 0)
5746689Smaybee 				(void) strlcpy(ds->ds_snapname, snapname,
5756689Smaybee 				    sizeof (ds->ds_snapname));
5766689Smaybee 			mutex_exit(&ds->ds_lock);
5776689Smaybee 			err = dsl_dataset_hold_ref(ds, tag);
5786689Smaybee 			*dsp = err ? NULL : ds;
579789Sahrens 		}
580789Sahrens 	}
581789Sahrens out:
582789Sahrens 	rw_exit(&dp->dp_config_rwlock);
583789Sahrens 	dsl_dir_close(dd, FTAG);
584789Sahrens 	return (err);
585789Sahrens }
586789Sahrens 
587789Sahrens int
5886689Smaybee dsl_dataset_own(const char *name, int flags, void *owner, dsl_dataset_t **dsp)
589789Sahrens {
5906689Smaybee 	int err = dsl_dataset_hold(name, owner, dsp);
5916689Smaybee 	if (err)
5926689Smaybee 		return (err);
5936689Smaybee 	if ((*dsp)->ds_phys->ds_num_children > 0 &&
5946689Smaybee 	    !DS_MODE_IS_READONLY(flags)) {
5956689Smaybee 		dsl_dataset_rele(*dsp, owner);
5966689Smaybee 		return (EROFS);
5976689Smaybee 	}
5986689Smaybee 	if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) {
5996689Smaybee 		dsl_dataset_rele(*dsp, owner);
6006689Smaybee 		return (EBUSY);
6016689Smaybee 	}
6026689Smaybee 	return (0);
603789Sahrens }
604789Sahrens 
605789Sahrens void
606789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
607789Sahrens {
608789Sahrens 	if (ds == NULL) {
609789Sahrens 		(void) strcpy(name, "mos");
610789Sahrens 	} else {
611789Sahrens 		dsl_dir_name(ds->ds_dir, name);
6121544Seschrock 		VERIFY(0 == dsl_dataset_get_snapname(ds));
613789Sahrens 		if (ds->ds_snapname[0]) {
614789Sahrens 			(void) strcat(name, "@");
6156689Smaybee 			/*
6166689Smaybee 			 * We use a "recursive" mutex so that we
6176689Smaybee 			 * can call dprintf_ds() with ds_lock held.
6186689Smaybee 			 */
619789Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
620789Sahrens 				mutex_enter(&ds->ds_lock);
621789Sahrens 				(void) strcat(name, ds->ds_snapname);
622789Sahrens 				mutex_exit(&ds->ds_lock);
623789Sahrens 			} else {
624789Sahrens 				(void) strcat(name, ds->ds_snapname);
625789Sahrens 			}
626789Sahrens 		}
627789Sahrens 	}
628789Sahrens }
629789Sahrens 
6303978Smmusante static int
6313978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds)
6323978Smmusante {
6333978Smmusante 	int result;
6343978Smmusante 
6353978Smmusante 	if (ds == NULL) {
6363978Smmusante 		result = 3;	/* "mos" */
6373978Smmusante 	} else {
6383978Smmusante 		result = dsl_dir_namelen(ds->ds_dir);
6393978Smmusante 		VERIFY(0 == dsl_dataset_get_snapname(ds));
6403978Smmusante 		if (ds->ds_snapname[0]) {
6413978Smmusante 			++result;	/* adding one for the @-sign */
6423978Smmusante 			if (!MUTEX_HELD(&ds->ds_lock)) {
6433978Smmusante 				mutex_enter(&ds->ds_lock);
6443978Smmusante 				result += strlen(ds->ds_snapname);
6453978Smmusante 				mutex_exit(&ds->ds_lock);
6463978Smmusante 			} else {
6473978Smmusante 				result += strlen(ds->ds_snapname);
6483978Smmusante 			}
6493978Smmusante 		}
6503978Smmusante 	}
6513978Smmusante 
6523978Smmusante 	return (result);
6533978Smmusante }
6543978Smmusante 
6557046Sahrens void
6566689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag)
657789Sahrens {
6581544Seschrock 	dmu_buf_rele(ds->ds_dbuf, tag);
659789Sahrens }
660789Sahrens 
661789Sahrens void
6626689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
6635367Sahrens {
6646689Smaybee 	if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) {
6656689Smaybee 		rw_exit(&ds->ds_rwlock);
6666689Smaybee 	}
6676689Smaybee 	dsl_dataset_drop_ref(ds, tag);
6686689Smaybee }
6696689Smaybee 
6706689Smaybee void
6716689Smaybee dsl_dataset_disown(dsl_dataset_t *ds, void *owner)
6726689Smaybee {
6736689Smaybee 	ASSERT((ds->ds_owner == owner && ds->ds_dbuf) ||
6746689Smaybee 	    (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL));
6756689Smaybee 
6765367Sahrens 	mutex_enter(&ds->ds_lock);
6776689Smaybee 	ds->ds_owner = NULL;
6786689Smaybee 	if (RW_WRITE_HELD(&ds->ds_rwlock)) {
6796689Smaybee 		rw_exit(&ds->ds_rwlock);
6806689Smaybee 		cv_broadcast(&ds->ds_exclusive_cv);
6816689Smaybee 	}
6825367Sahrens 	mutex_exit(&ds->ds_lock);
6836689Smaybee 	if (ds->ds_dbuf)
6846689Smaybee 		dsl_dataset_drop_ref(ds, owner);
6856689Smaybee 	else
6866689Smaybee 		dsl_dataset_evict(ds->ds_dbuf, ds);
6875367Sahrens }
6885367Sahrens 
6895367Sahrens boolean_t
6906689Smaybee dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *owner)
6915367Sahrens {
6926689Smaybee 	boolean_t gotit = FALSE;
6936689Smaybee 
6945367Sahrens 	mutex_enter(&ds->ds_lock);
6956689Smaybee 	if (ds->ds_owner == NULL &&
6966689Smaybee 	    (!DS_IS_INCONSISTENT(ds) || inconsistentok)) {
6976689Smaybee 		ds->ds_owner = owner;
6986689Smaybee 		if (!dsl_pool_sync_context(ds->ds_dir->dd_pool))
6996689Smaybee 			rw_exit(&ds->ds_rwlock);
7006689Smaybee 		gotit = TRUE;
7015367Sahrens 	}
7025367Sahrens 	mutex_exit(&ds->ds_lock);
7036689Smaybee 	return (gotit);
7046689Smaybee }
7056689Smaybee 
7066689Smaybee void
7076689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner)
7086689Smaybee {
7096689Smaybee 	ASSERT3P(owner, ==, ds->ds_owner);
7106689Smaybee 	if (!RW_WRITE_HELD(&ds->ds_rwlock))
7116689Smaybee 		rw_enter(&ds->ds_rwlock, RW_WRITER);
7125367Sahrens }
7135367Sahrens 
7142199Sahrens uint64_t
7157046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
7166492Stimh     uint64_t flags, dmu_tx_t *tx)
717789Sahrens {
7185367Sahrens 	dsl_pool_t *dp = dd->dd_pool;
719789Sahrens 	dmu_buf_t *dbuf;
720789Sahrens 	dsl_dataset_phys_t *dsphys;
7215367Sahrens 	uint64_t dsobj;
722789Sahrens 	objset_t *mos = dp->dp_meta_objset;
723789Sahrens 
7247046Sahrens 	if (origin == NULL)
7257046Sahrens 		origin = dp->dp_origin_snap;
7267046Sahrens 
7275367Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
7285367Sahrens 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
729789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7305367Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
731789Sahrens 
732928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
733928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
7341544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
735789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
736789Sahrens 	dsphys = dbuf->db_data;
7376689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
738789Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
7396492Stimh 	dsphys->ds_flags = flags;
740789Sahrens 	dsphys->ds_fsid_guid = unique_create();
741789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
742789Sahrens 	    sizeof (dsphys->ds_guid));
743789Sahrens 	dsphys->ds_snapnames_zapobj =
7446492Stimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
7456492Stimh 	    DMU_OT_NONE, 0, tx);
746789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
7477046Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
748789Sahrens 	dsphys->ds_deadlist_obj =
749789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
7505378Sck153898 
7515367Sahrens 	if (origin) {
7525367Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
753789Sahrens 		dsphys->ds_prev_snap_txg =
7545367Sahrens 		    origin->ds_phys->ds_creation_txg;
755789Sahrens 		dsphys->ds_used_bytes =
7565367Sahrens 		    origin->ds_phys->ds_used_bytes;
757789Sahrens 		dsphys->ds_compressed_bytes =
7585367Sahrens 		    origin->ds_phys->ds_compressed_bytes;
759789Sahrens 		dsphys->ds_uncompressed_bytes =
7605367Sahrens 		    origin->ds_phys->ds_uncompressed_bytes;
7615367Sahrens 		dsphys->ds_bp = origin->ds_phys->ds_bp;
7626502Stimh 		dsphys->ds_flags |= origin->ds_phys->ds_flags;
763789Sahrens 
7645367Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
7655367Sahrens 		origin->ds_phys->ds_num_children++;
766789Sahrens 
7677046Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
7687046Sahrens 			if (origin->ds_phys->ds_next_clones_obj == 0) {
7697046Sahrens 				origin->ds_phys->ds_next_clones_obj =
7707046Sahrens 				    zap_create(mos,
7717046Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
7727046Sahrens 			}
7737046Sahrens 			VERIFY(0 == zap_add_int(mos,
7747046Sahrens 			    origin->ds_phys->ds_next_clones_obj,
7757046Sahrens 			    dsobj, tx));
7767046Sahrens 		}
7777046Sahrens 
778789Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
7795367Sahrens 		dd->dd_phys->dd_origin_obj = origin->ds_object;
780789Sahrens 	}
7816492Stimh 
7826492Stimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
7836492Stimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
7846492Stimh 
7851544Seschrock 	dmu_buf_rele(dbuf, FTAG);
786789Sahrens 
787789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
788789Sahrens 	dd->dd_phys->dd_head_dataset_obj = dsobj;
7895367Sahrens 
7905367Sahrens 	return (dsobj);
7915367Sahrens }
7925367Sahrens 
7935367Sahrens uint64_t
7946492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
7956492Stimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
7965367Sahrens {
7975367Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
7985367Sahrens 	uint64_t dsobj, ddobj;
7995367Sahrens 	dsl_dir_t *dd;
8005367Sahrens 
8015367Sahrens 	ASSERT(lastname[0] != '@');
8025367Sahrens 
8037046Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
8045367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
8055367Sahrens 
8067046Sahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx);
8075367Sahrens 
8085367Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
8095367Sahrens 
810789Sahrens 	dsl_dir_close(dd, FTAG);
811789Sahrens 
8122199Sahrens 	return (dsobj);
8132199Sahrens }
8142199Sahrens 
8152199Sahrens struct destroyarg {
8162199Sahrens 	dsl_sync_task_group_t *dstg;
8172199Sahrens 	char *snapname;
8182199Sahrens 	char *failed;
8192199Sahrens };
8202199Sahrens 
8212199Sahrens static int
8222199Sahrens dsl_snapshot_destroy_one(char *name, void *arg)
8232199Sahrens {
8242199Sahrens 	struct destroyarg *da = arg;
8252199Sahrens 	dsl_dataset_t *ds;
8262199Sahrens 	char *cp;
8272199Sahrens 	int err;
8282199Sahrens 
8292199Sahrens 	(void) strcat(name, "@");
8302199Sahrens 	(void) strcat(name, da->snapname);
8316689Smaybee 	err = dsl_dataset_own(name, DS_MODE_READONLY | DS_MODE_INCONSISTENT,
8324007Smmusante 	    da->dstg, &ds);
8332199Sahrens 	cp = strchr(name, '@');
8342199Sahrens 	*cp = '\0';
8356689Smaybee 	if (err == 0) {
8366689Smaybee 		dsl_dataset_make_exclusive(ds, da->dstg);
8377237Sek110237 		if (ds->ds_user_ptr) {
8387237Sek110237 			ds->ds_user_evict_func(ds, ds->ds_user_ptr);
8397237Sek110237 			ds->ds_user_ptr = NULL;
8407237Sek110237 		}
8416689Smaybee 		dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check,
8426689Smaybee 		    dsl_dataset_destroy_sync, ds, da->dstg, 0);
8436689Smaybee 	} else if (err == ENOENT) {
8446689Smaybee 		err = 0;
8456689Smaybee 	} else {
8462199Sahrens 		(void) strcpy(da->failed, name);
8472199Sahrens 	}
8486689Smaybee 	return (err);
849789Sahrens }
850789Sahrens 
8512199Sahrens /*
8522199Sahrens  * Destroy 'snapname' in all descendants of 'fsname'.
8532199Sahrens  */
8542199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy
8552199Sahrens int
8562199Sahrens dsl_snapshots_destroy(char *fsname, char *snapname)
8572199Sahrens {
8582199Sahrens 	int err;
8592199Sahrens 	struct destroyarg da;
8602199Sahrens 	dsl_sync_task_t *dst;
8612199Sahrens 	spa_t *spa;
8622199Sahrens 
8634603Sahrens 	err = spa_open(fsname, &spa, FTAG);
8642199Sahrens 	if (err)
8652199Sahrens 		return (err);
8662199Sahrens 	da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8672199Sahrens 	da.snapname = snapname;
8682199Sahrens 	da.failed = fsname;
8692199Sahrens 
8702199Sahrens 	err = dmu_objset_find(fsname,
8712417Sahrens 	    dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN);
8722199Sahrens 
8732199Sahrens 	if (err == 0)
8742199Sahrens 		err = dsl_sync_task_group_wait(da.dstg);
8752199Sahrens 
8762199Sahrens 	for (dst = list_head(&da.dstg->dstg_tasks); dst;
8772199Sahrens 	    dst = list_next(&da.dstg->dstg_tasks, dst)) {
8782199Sahrens 		dsl_dataset_t *ds = dst->dst_arg1;
8796689Smaybee 		/*
8806689Smaybee 		 * Return the file system name that triggered the error
8816689Smaybee 		 */
8822199Sahrens 		if (dst->dst_err) {
8832199Sahrens 			dsl_dataset_name(ds, fsname);
8844603Sahrens 			*strchr(fsname, '@') = '\0';
8852199Sahrens 		}
8866689Smaybee 		dsl_dataset_disown(ds, da.dstg);
8872199Sahrens 	}
8882199Sahrens 
8892199Sahrens 	dsl_sync_task_group_destroy(da.dstg);
8902199Sahrens 	spa_close(spa, FTAG);
8912199Sahrens 	return (err);
8922199Sahrens }
8932199Sahrens 
8945367Sahrens /*
8956689Smaybee  * ds must be opened as OWNER.  On return (whether successful or not),
8966689Smaybee  * ds will be closed and caller can no longer dereference it.
8975367Sahrens  */
898789Sahrens int
8995367Sahrens dsl_dataset_destroy(dsl_dataset_t *ds, void *tag)
900789Sahrens {
901789Sahrens 	int err;
9022199Sahrens 	dsl_sync_task_group_t *dstg;
9032199Sahrens 	objset_t *os;
904789Sahrens 	dsl_dir_t *dd;
9052199Sahrens 	uint64_t obj;
9062199Sahrens 
9075367Sahrens 	if (dsl_dataset_is_snapshot(ds)) {
9082199Sahrens 		/* Destroying a snapshot is simpler */
9096689Smaybee 		dsl_dataset_make_exclusive(ds, tag);
9107237Sek110237 
9117237Sek110237 		if (ds->ds_user_ptr) {
9127237Sek110237 			ds->ds_user_evict_func(ds, ds->ds_user_ptr);
9137237Sek110237 			ds->ds_user_ptr = NULL;
9147237Sek110237 		}
9152199Sahrens 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
9162199Sahrens 		    dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
9175367Sahrens 		    ds, tag, 0);
9185367Sahrens 		goto out;
9192199Sahrens 	}
9202199Sahrens 
9212199Sahrens 	dd = ds->ds_dir;
922789Sahrens 
9232199Sahrens 	/*
9242199Sahrens 	 * Check for errors and mark this ds as inconsistent, in
9252199Sahrens 	 * case we crash while freeing the objects.
9262199Sahrens 	 */
9272199Sahrens 	err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
9282199Sahrens 	    dsl_dataset_destroy_begin_sync, ds, NULL, 0);
9295367Sahrens 	if (err)
9305367Sahrens 		goto out;
9315367Sahrens 
9325367Sahrens 	err = dmu_objset_open_ds(ds, DMU_OST_ANY, &os);
9335367Sahrens 	if (err)
9345367Sahrens 		goto out;
9352199Sahrens 
9362199Sahrens 	/*
9372199Sahrens 	 * remove the objects in open context, so that we won't
9382199Sahrens 	 * have too much to do in syncing context.
9392199Sahrens 	 */
9403025Sahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
9413025Sahrens 	    ds->ds_phys->ds_prev_snap_txg)) {
9426992Smaybee 		/*
9436992Smaybee 		 * Ignore errors, if there is not enough disk space
9446992Smaybee 		 * we will deal with it in dsl_dataset_destroy_sync().
9456992Smaybee 		 */
9466992Smaybee 		(void) dmu_free_object(os, obj);
9472199Sahrens 	}
9482199Sahrens 
9492199Sahrens 	dmu_objset_close(os);
9502199Sahrens 	if (err != ESRCH)
9515367Sahrens 		goto out;
9522199Sahrens 
9536975Smaybee 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
9546975Smaybee 	err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd);
9556975Smaybee 	rw_exit(&dd->dd_pool->dp_config_rwlock);
9566975Smaybee 
9576975Smaybee 	if (err)
9586975Smaybee 		goto out;
9596975Smaybee 
9605367Sahrens 	if (ds->ds_user_ptr) {
9616689Smaybee 		/*
9626689Smaybee 		 * We need to sync out all in-flight IO before we try
9636689Smaybee 		 * to evict (the dataset evict func is trying to clear
9646689Smaybee 		 * the cached entries for this dataset in the ARC).
9656689Smaybee 		 */
9666689Smaybee 		txg_wait_synced(dd->dd_pool, 0);
9675367Sahrens 	}
9685367Sahrens 
9692199Sahrens 	/*
9702199Sahrens 	 * Blow away the dsl_dir + head dataset.
9712199Sahrens 	 */
9726689Smaybee 	dsl_dataset_make_exclusive(ds, tag);
9736975Smaybee 	if (ds->ds_user_ptr) {
9746975Smaybee 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
9756975Smaybee 		ds->ds_user_ptr = NULL;
9766975Smaybee 	}
9772199Sahrens 	dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
9782199Sahrens 	dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
9795367Sahrens 	    dsl_dataset_destroy_sync, ds, tag, 0);
9802199Sahrens 	dsl_sync_task_create(dstg, dsl_dir_destroy_check,
9812199Sahrens 	    dsl_dir_destroy_sync, dd, FTAG, 0);
9822199Sahrens 	err = dsl_sync_task_group_wait(dstg);
9832199Sahrens 	dsl_sync_task_group_destroy(dstg);
9846689Smaybee 	/* if it is successful, dsl_dir_destroy_sync will close the dd */
9855367Sahrens 	if (err)
9862199Sahrens 		dsl_dir_close(dd, FTAG);
9875367Sahrens out:
9886689Smaybee 	dsl_dataset_disown(ds, tag);
989789Sahrens 	return (err);
990789Sahrens }
991789Sahrens 
992789Sahrens int
9935367Sahrens dsl_dataset_rollback(dsl_dataset_t *ds, dmu_objset_type_t ost)
994789Sahrens {
9956689Smaybee 	ASSERT(ds->ds_owner);
9965367Sahrens 
9972199Sahrens 	return (dsl_sync_task_do(ds->ds_dir->dd_pool,
9982199Sahrens 	    dsl_dataset_rollback_check, dsl_dataset_rollback_sync,
9995367Sahrens 	    ds, &ost, 0));
1000789Sahrens }
1001789Sahrens 
1002789Sahrens void *
1003789Sahrens dsl_dataset_set_user_ptr(dsl_dataset_t *ds,
1004789Sahrens     void *p, dsl_dataset_evict_func_t func)
1005789Sahrens {
1006789Sahrens 	void *old;
1007789Sahrens 
1008789Sahrens 	mutex_enter(&ds->ds_lock);
1009789Sahrens 	old = ds->ds_user_ptr;
1010789Sahrens 	if (old == NULL) {
1011789Sahrens 		ds->ds_user_ptr = p;
1012789Sahrens 		ds->ds_user_evict_func = func;
1013789Sahrens 	}
1014789Sahrens 	mutex_exit(&ds->ds_lock);
1015789Sahrens 	return (old);
1016789Sahrens }
1017789Sahrens 
1018789Sahrens void *
1019789Sahrens dsl_dataset_get_user_ptr(dsl_dataset_t *ds)
1020789Sahrens {
1021789Sahrens 	return (ds->ds_user_ptr);
1022789Sahrens }
1023789Sahrens 
1024789Sahrens 
10253547Smaybee blkptr_t *
10263547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1027789Sahrens {
10283547Smaybee 	return (&ds->ds_phys->ds_bp);
1029789Sahrens }
1030789Sahrens 
1031789Sahrens void
1032789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1033789Sahrens {
1034789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1035789Sahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
1036789Sahrens 	if (ds == NULL) {
1037789Sahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
1038789Sahrens 	} else {
1039789Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1040789Sahrens 		ds->ds_phys->ds_bp = *bp;
1041789Sahrens 	}
1042789Sahrens }
1043789Sahrens 
1044789Sahrens spa_t *
1045789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
1046789Sahrens {
1047789Sahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1048789Sahrens }
1049789Sahrens 
1050789Sahrens void
1051789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1052789Sahrens {
1053789Sahrens 	dsl_pool_t *dp;
1054789Sahrens 
1055789Sahrens 	if (ds == NULL) /* this is the meta-objset */
1056789Sahrens 		return;
1057789Sahrens 
1058789Sahrens 	ASSERT(ds->ds_user_ptr != NULL);
10592885Sahrens 
10602885Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
10612885Sahrens 		panic("dirtying snapshot!");
1062789Sahrens 
1063789Sahrens 	dp = ds->ds_dir->dd_pool;
1064789Sahrens 
1065789Sahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
1066789Sahrens 		/* up the hold count until we can be written out */
1067789Sahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1068789Sahrens 	}
1069789Sahrens }
1070789Sahrens 
10715378Sck153898 /*
10725378Sck153898  * The unique space in the head dataset can be calculated by subtracting
10735378Sck153898  * the space used in the most recent snapshot, that is still being used
10745378Sck153898  * in this file system, from the space currently in use.  To figure out
10755378Sck153898  * the space in the most recent snapshot still in use, we need to take
10765378Sck153898  * the total space used in the snapshot and subtract out the space that
10775378Sck153898  * has been freed up since the snapshot was taken.
10785378Sck153898  */
10795378Sck153898 static void
10805378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
10815378Sck153898 {
10825378Sck153898 	uint64_t mrs_used;
10835378Sck153898 	uint64_t dlused, dlcomp, dluncomp;
10845378Sck153898 
10855378Sck153898 	ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj);
10865378Sck153898 
10875378Sck153898 	if (ds->ds_phys->ds_prev_snap_obj != 0)
10885378Sck153898 		mrs_used = ds->ds_prev->ds_phys->ds_used_bytes;
10895378Sck153898 	else
10905378Sck153898 		mrs_used = 0;
10915378Sck153898 
10925378Sck153898 	VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp,
10935378Sck153898 	    &dluncomp));
10945378Sck153898 
10955378Sck153898 	ASSERT3U(dlused, <=, mrs_used);
10965378Sck153898 	ds->ds_phys->ds_unique_bytes =
10975378Sck153898 	    ds->ds_phys->ds_used_bytes - (mrs_used - dlused);
10985378Sck153898 
10995378Sck153898 	if (!DS_UNIQUE_IS_ACCURATE(ds) &&
11005378Sck153898 	    spa_version(ds->ds_dir->dd_pool->dp_spa) >=
11015378Sck153898 	    SPA_VERSION_UNIQUE_ACCURATE)
11025378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
11035378Sck153898 }
11045378Sck153898 
11055378Sck153898 static uint64_t
11065378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds)
11075378Sck153898 {
11085378Sck153898 	if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds))
11095378Sck153898 		dsl_dataset_recalc_head_uniq(ds);
11105378Sck153898 
11115378Sck153898 	return (ds->ds_phys->ds_unique_bytes);
11125378Sck153898 }
11135378Sck153898 
1114789Sahrens struct killarg {
11155378Sck153898 	int64_t *usedp;
11165378Sck153898 	int64_t *compressedp;
11175378Sck153898 	int64_t *uncompressedp;
1118789Sahrens 	zio_t *zio;
1119789Sahrens 	dmu_tx_t *tx;
1120789Sahrens };
1121789Sahrens 
1122789Sahrens static int
1123789Sahrens kill_blkptr(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
1124789Sahrens {
1125789Sahrens 	struct killarg *ka = arg;
1126789Sahrens 	blkptr_t *bp = &bc->bc_blkptr;
1127789Sahrens 
1128789Sahrens 	ASSERT3U(bc->bc_errno, ==, 0);
1129789Sahrens 
1130789Sahrens 	/*
1131789Sahrens 	 * Since this callback is not called concurrently, no lock is
1132789Sahrens 	 * needed on the accounting values.
1133789Sahrens 	 */
11342082Seschrock 	*ka->usedp += bp_get_dasize(spa, bp);
1135789Sahrens 	*ka->compressedp += BP_GET_PSIZE(bp);
1136789Sahrens 	*ka->uncompressedp += BP_GET_UCSIZE(bp);
1137789Sahrens 	/* XXX check for EIO? */
11387046Sahrens 	(void) dsl_free(ka->zio, spa_get_dsl(spa), ka->tx->tx_txg,
11397046Sahrens 	    bp, NULL, NULL, ARC_NOWAIT);
1140789Sahrens 	return (0);
1141789Sahrens }
1142789Sahrens 
1143789Sahrens /* ARGSUSED */
11442199Sahrens static int
11452199Sahrens dsl_dataset_rollback_check(void *arg1, void *arg2, dmu_tx_t *tx)
1146789Sahrens {
11472199Sahrens 	dsl_dataset_t *ds = arg1;
11485367Sahrens 	dmu_objset_type_t *ost = arg2;
1149789Sahrens 
11502199Sahrens 	/*
11515367Sahrens 	 * We can only roll back to emptyness if it is a ZPL objset.
11522199Sahrens 	 */
11535367Sahrens 	if (*ost != DMU_OST_ZFS && ds->ds_phys->ds_prev_snap_txg == 0)
1154789Sahrens 		return (EINVAL);
1155789Sahrens 
11562199Sahrens 	/*
11572199Sahrens 	 * This must not be a snapshot.
11582199Sahrens 	 */
11592199Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
11602199Sahrens 		return (EINVAL);
1161789Sahrens 
1162789Sahrens 	/*
1163789Sahrens 	 * If we made changes this txg, traverse_dsl_dataset won't find
1164789Sahrens 	 * them.  Try again.
1165789Sahrens 	 */
11662199Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1167789Sahrens 		return (EAGAIN);
11682199Sahrens 
11692199Sahrens 	return (0);
11702199Sahrens }
1171789Sahrens 
11722199Sahrens /* ARGSUSED */
11732199Sahrens static void
11744543Smarks dsl_dataset_rollback_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
11752199Sahrens {
11762199Sahrens 	dsl_dataset_t *ds = arg1;
11775367Sahrens 	dmu_objset_type_t *ost = arg2;
11782199Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1179789Sahrens 
1180789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1181789Sahrens 
11824967Sperrin 	/*
11834967Sperrin 	 * Before the roll back destroy the zil.
11844967Sperrin 	 */
11854967Sperrin 	if (ds->ds_user_ptr != NULL) {
11864967Sperrin 		zil_rollback_destroy(
11874967Sperrin 		    ((objset_impl_t *)ds->ds_user_ptr)->os_zil, tx);
11885367Sahrens 
11895367Sahrens 		/*
11905367Sahrens 		 * We need to make sure that the objset_impl_t is reopened after
11915367Sahrens 		 * we do the rollback, otherwise it will have the wrong
11925367Sahrens 		 * objset_phys_t.  Normally this would happen when this
11936689Smaybee 		 * dataset-open is closed, thus causing the
11945367Sahrens 		 * dataset to be immediately evicted.  But when doing "zfs recv
11955367Sahrens 		 * -F", we reopen the objset before that, so that there is no
11965367Sahrens 		 * window where the dataset is closed and inconsistent.
11975367Sahrens 		 */
11985367Sahrens 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
11995367Sahrens 		ds->ds_user_ptr = NULL;
12004967Sperrin 	}
12014935Sperrin 
1202789Sahrens 	/* Zero out the deadlist. */
1203789Sahrens 	bplist_close(&ds->ds_deadlist);
1204789Sahrens 	bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1205789Sahrens 	ds->ds_phys->ds_deadlist_obj =
1206789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
12071544Seschrock 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
12081544Seschrock 	    ds->ds_phys->ds_deadlist_obj));
1209789Sahrens 
1210789Sahrens 	{
1211789Sahrens 		/* Free blkptrs that we gave birth to */
1212789Sahrens 		zio_t *zio;
12135378Sck153898 		int64_t used = 0, compressed = 0, uncompressed = 0;
1214789Sahrens 		struct killarg ka;
12155518Sck153898 		int64_t delta;
1216789Sahrens 
1217789Sahrens 		zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL,
1218789Sahrens 		    ZIO_FLAG_MUSTSUCCEED);
1219789Sahrens 		ka.usedp = &used;
1220789Sahrens 		ka.compressedp = &compressed;
1221789Sahrens 		ka.uncompressedp = &uncompressed;
1222789Sahrens 		ka.zio = zio;
1223789Sahrens 		ka.tx = tx;
1224789Sahrens 		(void) traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
1225789Sahrens 		    ADVANCE_POST, kill_blkptr, &ka);
1226789Sahrens 		(void) zio_wait(zio);
1227789Sahrens 
12285518Sck153898 		/* only deduct space beyond any refreservation */
12295518Sck153898 		delta = parent_delta(ds, -used);
12302199Sahrens 		dsl_dir_diduse_space(ds->ds_dir,
12315518Sck153898 		    delta, -compressed, -uncompressed, tx);
1232789Sahrens 	}
1233789Sahrens 
12347046Sahrens 	if (ds->ds_prev && ds->ds_prev != ds->ds_dir->dd_pool->dp_origin_snap) {
12355367Sahrens 		/* Change our contents to that of the prev snapshot */
12365367Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
12375367Sahrens 		    ds->ds_phys->ds_prev_snap_obj);
12385367Sahrens 		ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp;
12395367Sahrens 		ds->ds_phys->ds_used_bytes =
12405367Sahrens 		    ds->ds_prev->ds_phys->ds_used_bytes;
12415367Sahrens 		ds->ds_phys->ds_compressed_bytes =
12425367Sahrens 		    ds->ds_prev->ds_phys->ds_compressed_bytes;
12435367Sahrens 		ds->ds_phys->ds_uncompressed_bytes =
12445367Sahrens 		    ds->ds_prev->ds_phys->ds_uncompressed_bytes;
12455367Sahrens 		ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags;
12465367Sahrens 		ds->ds_phys->ds_unique_bytes = 0;
1247789Sahrens 
12485367Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
12495367Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
12505367Sahrens 			ds->ds_prev->ds_phys->ds_unique_bytes = 0;
12515367Sahrens 		}
12525367Sahrens 	} else {
12537046Sahrens 		objset_impl_t *osi;
12547046Sahrens 
12555367Sahrens 		/* Zero out our contents, recreate objset */
12565367Sahrens 		bzero(&ds->ds_phys->ds_bp, sizeof (blkptr_t));
12575367Sahrens 		ds->ds_phys->ds_used_bytes = 0;
12585367Sahrens 		ds->ds_phys->ds_compressed_bytes = 0;
12595367Sahrens 		ds->ds_phys->ds_uncompressed_bytes = 0;
12605367Sahrens 		ds->ds_phys->ds_flags = 0;
12615367Sahrens 		ds->ds_phys->ds_unique_bytes = 0;
12627046Sahrens 		osi = dmu_objset_create_impl(ds->ds_dir->dd_pool->dp_spa, ds,
12635367Sahrens 		    &ds->ds_phys->ds_bp, *ost, tx);
12647046Sahrens #ifdef _KERNEL
12657046Sahrens 		zfs_create_fs(&osi->os, kcred, NULL, tx);
12667046Sahrens #endif
12672532Sahrens 	}
12684543Smarks 
12694543Smarks 	spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa,
12704543Smarks 	    tx, cr, "dataset = %llu", ds->ds_object);
1271789Sahrens }
1272789Sahrens 
12731731Sbonwick /* ARGSUSED */
12741731Sbonwick static int
12752199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
12761731Sbonwick {
12772199Sahrens 	dsl_dataset_t *ds = arg1;
12785367Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
12795367Sahrens 	uint64_t count;
12805367Sahrens 	int err;
12811731Sbonwick 
12821731Sbonwick 	/*
12831731Sbonwick 	 * Can't delete a head dataset if there are snapshots of it.
12841731Sbonwick 	 * (Except if the only snapshots are from the branch we cloned
12851731Sbonwick 	 * from.)
12861731Sbonwick 	 */
12871731Sbonwick 	if (ds->ds_prev != NULL &&
12881731Sbonwick 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
12891731Sbonwick 		return (EINVAL);
12901731Sbonwick 
12915367Sahrens 	/*
12925367Sahrens 	 * This is really a dsl_dir thing, but check it here so that
12935367Sahrens 	 * we'll be less likely to leave this dataset inconsistent &
12945367Sahrens 	 * nearly destroyed.
12955367Sahrens 	 */
12965367Sahrens 	err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
12975367Sahrens 	if (err)
12985367Sahrens 		return (err);
12995367Sahrens 	if (count != 0)
13005367Sahrens 		return (EEXIST);
13015367Sahrens 
13021731Sbonwick 	return (0);
13031731Sbonwick }
13041731Sbonwick 
13052199Sahrens /* ARGSUSED */
13062199Sahrens static void
13074543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1308789Sahrens {
13092199Sahrens 	dsl_dataset_t *ds = arg1;
13104543Smarks 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1311789Sahrens 
13122199Sahrens 	/* Mark it as inconsistent on-disk, in case we crash */
13132199Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
13142199Sahrens 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
13154543Smarks 
13164543Smarks 	spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
13174543Smarks 	    cr, "dataset = %llu", ds->ds_object);
13182199Sahrens }
1319789Sahrens 
13202199Sahrens /* ARGSUSED */
13215367Sahrens int
13222199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
13232199Sahrens {
13242199Sahrens 	dsl_dataset_t *ds = arg1;
1325789Sahrens 
13266689Smaybee 	/* we have an owner hold, so noone else can destroy us */
13276689Smaybee 	ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
13286689Smaybee 
1329789Sahrens 	/* Can't delete a branch point. */
13302199Sahrens 	if (ds->ds_phys->ds_num_children > 1)
13312199Sahrens 		return (EEXIST);
1332789Sahrens 
1333789Sahrens 	/*
1334789Sahrens 	 * Can't delete a head dataset if there are snapshots of it.
1335789Sahrens 	 * (Except if the only snapshots are from the branch we cloned
1336789Sahrens 	 * from.)
1337789Sahrens 	 */
1338789Sahrens 	if (ds->ds_prev != NULL &&
13392199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1340789Sahrens 		return (EINVAL);
1341789Sahrens 
1342789Sahrens 	/*
1343789Sahrens 	 * If we made changes this txg, traverse_dsl_dataset won't find
1344789Sahrens 	 * them.  Try again.
1345789Sahrens 	 */
13462199Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1347789Sahrens 		return (EAGAIN);
13482199Sahrens 
13492199Sahrens 	/* XXX we should do some i/o error checking... */
13502199Sahrens 	return (0);
13512199Sahrens }
13522199Sahrens 
13536689Smaybee struct refsarg {
13546689Smaybee 	kmutex_t lock;
13556689Smaybee 	boolean_t gone;
13566689Smaybee 	kcondvar_t cv;
13576689Smaybee };
13586689Smaybee 
13596689Smaybee /* ARGSUSED */
13606689Smaybee static void
13616689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv)
13626689Smaybee {
13636689Smaybee 	struct refsarg *arg = argv;
13646689Smaybee 
13656689Smaybee 	mutex_enter(&arg->lock);
13666689Smaybee 	arg->gone = TRUE;
13676689Smaybee 	cv_signal(&arg->cv);
13686689Smaybee 	mutex_exit(&arg->lock);
13696689Smaybee }
13706689Smaybee 
13716689Smaybee static void
13726689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag)
13736689Smaybee {
13746689Smaybee 	struct refsarg arg;
13756689Smaybee 
13766689Smaybee 	mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL);
13776689Smaybee 	cv_init(&arg.cv, NULL, CV_DEFAULT, NULL);
13786689Smaybee 	arg.gone = FALSE;
13796689Smaybee 	(void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys,
13806689Smaybee 	    dsl_dataset_refs_gone);
13816689Smaybee 	dmu_buf_rele(ds->ds_dbuf, tag);
13826689Smaybee 	mutex_enter(&arg.lock);
13836689Smaybee 	while (!arg.gone)
13846689Smaybee 		cv_wait(&arg.cv, &arg.lock);
13856689Smaybee 	ASSERT(arg.gone);
13866689Smaybee 	mutex_exit(&arg.lock);
13876689Smaybee 	ds->ds_dbuf = NULL;
13886689Smaybee 	ds->ds_phys = NULL;
13896689Smaybee 	mutex_destroy(&arg.lock);
13906689Smaybee 	cv_destroy(&arg.cv);
13916689Smaybee }
13926689Smaybee 
13935367Sahrens void
13944543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
13952199Sahrens {
13962199Sahrens 	dsl_dataset_t *ds = arg1;
13975378Sck153898 	int64_t used = 0, compressed = 0, uncompressed = 0;
13982199Sahrens 	zio_t *zio;
13992199Sahrens 	int err;
14002199Sahrens 	int after_branch_point = FALSE;
14012199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
14022199Sahrens 	objset_t *mos = dp->dp_meta_objset;
14032199Sahrens 	dsl_dataset_t *ds_prev = NULL;
14042199Sahrens 	uint64_t obj;
14052199Sahrens 
14066689Smaybee 	ASSERT(ds->ds_owner);
14072199Sahrens 	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
14082199Sahrens 	ASSERT(ds->ds_prev == NULL ||
14092199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
14102199Sahrens 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
14112199Sahrens 
14126689Smaybee 	/* signal any waiters that this dataset is going away */
14136689Smaybee 	mutex_enter(&ds->ds_lock);
14146689Smaybee 	ds->ds_owner = dsl_reaper;
14156689Smaybee 	cv_broadcast(&ds->ds_exclusive_cv);
14166689Smaybee 	mutex_exit(&ds->ds_lock);
14176689Smaybee 
14185378Sck153898 	/* Remove our reservation */
14195378Sck153898 	if (ds->ds_reserved != 0) {
14205378Sck153898 		uint64_t val = 0;
14215378Sck153898 		dsl_dataset_set_reservation_sync(ds, &val, cr, tx);
14225378Sck153898 		ASSERT3U(ds->ds_reserved, ==, 0);
14235378Sck153898 	}
14245378Sck153898 
14252199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
14262199Sahrens 
14277046Sahrens 	dsl_pool_ds_destroyed(ds, tx);
14287046Sahrens 
14292199Sahrens 	obj = ds->ds_object;
1430789Sahrens 
1431789Sahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1432789Sahrens 		if (ds->ds_prev) {
1433789Sahrens 			ds_prev = ds->ds_prev;
1434789Sahrens 		} else {
14356689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
14366689Smaybee 			    ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
1437789Sahrens 		}
1438789Sahrens 		after_branch_point =
1439789Sahrens 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
1440789Sahrens 
1441789Sahrens 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1442789Sahrens 		if (after_branch_point &&
14437046Sahrens 		    ds_prev->ds_phys->ds_next_clones_obj != 0) {
14447046Sahrens 			VERIFY(0 == zap_remove_int(mos,
14457046Sahrens 			    ds_prev->ds_phys->ds_next_clones_obj, obj, tx));
14467046Sahrens 			if (ds->ds_phys->ds_next_snap_obj != 0) {
14477046Sahrens 				VERIFY(0 == zap_add_int(mos,
14487046Sahrens 				    ds_prev->ds_phys->ds_next_clones_obj,
14497046Sahrens 				    ds->ds_phys->ds_next_snap_obj, tx));
14507046Sahrens 			}
14517046Sahrens 		}
14527046Sahrens 		if (after_branch_point &&
1453789Sahrens 		    ds->ds_phys->ds_next_snap_obj == 0) {
1454789Sahrens 			/* This clone is toast. */
1455789Sahrens 			ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1456789Sahrens 			ds_prev->ds_phys->ds_num_children--;
1457789Sahrens 		} else if (!after_branch_point) {
1458789Sahrens 			ds_prev->ds_phys->ds_next_snap_obj =
1459789Sahrens 			    ds->ds_phys->ds_next_snap_obj;
1460789Sahrens 		}
1461789Sahrens 	}
1462789Sahrens 
1463789Sahrens 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1464789Sahrens 
1465789Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0) {
14662199Sahrens 		blkptr_t bp;
1467789Sahrens 		dsl_dataset_t *ds_next;
1468789Sahrens 		uint64_t itor = 0;
14695378Sck153898 		uint64_t old_unique;
1470789Sahrens 
14716689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
14726689Smaybee 		    ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
1473789Sahrens 		ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1474789Sahrens 
14755378Sck153898 		old_unique = dsl_dataset_unique(ds_next);
14765378Sck153898 
1477789Sahrens 		dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1478789Sahrens 		ds_next->ds_phys->ds_prev_snap_obj =
1479789Sahrens 		    ds->ds_phys->ds_prev_snap_obj;
1480789Sahrens 		ds_next->ds_phys->ds_prev_snap_txg =
1481789Sahrens 		    ds->ds_phys->ds_prev_snap_txg;
1482789Sahrens 		ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1483789Sahrens 		    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1484789Sahrens 
1485789Sahrens 		/*
1486789Sahrens 		 * Transfer to our deadlist (which will become next's
1487789Sahrens 		 * new deadlist) any entries from next's current
1488789Sahrens 		 * deadlist which were born before prev, and free the
1489789Sahrens 		 * other entries.
1490789Sahrens 		 *
1491789Sahrens 		 * XXX we're doing this long task with the config lock held
1492789Sahrens 		 */
14936689Smaybee 		while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) {
1494789Sahrens 			if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) {
14951544Seschrock 				VERIFY(0 == bplist_enqueue(&ds->ds_deadlist,
14961544Seschrock 				    &bp, tx));
1497789Sahrens 				if (ds_prev && !after_branch_point &&
1498789Sahrens 				    bp.blk_birth >
1499789Sahrens 				    ds_prev->ds_phys->ds_prev_snap_txg) {
1500789Sahrens 					ds_prev->ds_phys->ds_unique_bytes +=
15012082Seschrock 					    bp_get_dasize(dp->dp_spa, &bp);
1502789Sahrens 				}
1503789Sahrens 			} else {
15042082Seschrock 				used += bp_get_dasize(dp->dp_spa, &bp);
1505789Sahrens 				compressed += BP_GET_PSIZE(&bp);
1506789Sahrens 				uncompressed += BP_GET_UCSIZE(&bp);
1507789Sahrens 				/* XXX check return value? */
15087046Sahrens 				(void) dsl_free(zio, dp, tx->tx_txg,
1509789Sahrens 				    &bp, NULL, NULL, ARC_NOWAIT);
1510789Sahrens 			}
1511789Sahrens 		}
1512789Sahrens 
1513789Sahrens 		/* free next's deadlist */
1514789Sahrens 		bplist_close(&ds_next->ds_deadlist);
1515789Sahrens 		bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx);
1516789Sahrens 
1517789Sahrens 		/* set next's deadlist to our deadlist */
15186689Smaybee 		bplist_close(&ds->ds_deadlist);
1519789Sahrens 		ds_next->ds_phys->ds_deadlist_obj =
1520789Sahrens 		    ds->ds_phys->ds_deadlist_obj;
15211544Seschrock 		VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos,
15221544Seschrock 		    ds_next->ds_phys->ds_deadlist_obj));
1523789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1524789Sahrens 
1525789Sahrens 		if (ds_next->ds_phys->ds_next_snap_obj != 0) {
1526789Sahrens 			/*
1527789Sahrens 			 * Update next's unique to include blocks which
1528789Sahrens 			 * were previously shared by only this snapshot
1529789Sahrens 			 * and it.  Those blocks will be born after the
1530789Sahrens 			 * prev snap and before this snap, and will have
1531789Sahrens 			 * died after the next snap and before the one
1532789Sahrens 			 * after that (ie. be on the snap after next's
1533789Sahrens 			 * deadlist).
1534789Sahrens 			 *
1535789Sahrens 			 * XXX we're doing this long task with the
1536789Sahrens 			 * config lock held
1537789Sahrens 			 */
1538789Sahrens 			dsl_dataset_t *ds_after_next;
1539789Sahrens 
15406689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
15416689Smaybee 			    ds_next->ds_phys->ds_next_snap_obj,
15426689Smaybee 			    FTAG, &ds_after_next));
1543789Sahrens 			itor = 0;
1544789Sahrens 			while (bplist_iterate(&ds_after_next->ds_deadlist,
1545789Sahrens 			    &itor, &bp) == 0) {
1546789Sahrens 				if (bp.blk_birth >
1547789Sahrens 				    ds->ds_phys->ds_prev_snap_txg &&
1548789Sahrens 				    bp.blk_birth <=
1549789Sahrens 				    ds->ds_phys->ds_creation_txg) {
1550789Sahrens 					ds_next->ds_phys->ds_unique_bytes +=
15512082Seschrock 					    bp_get_dasize(dp->dp_spa, &bp);
1552789Sahrens 				}
1553789Sahrens 			}
1554789Sahrens 
15556689Smaybee 			dsl_dataset_rele(ds_after_next, FTAG);
1556789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, NULL);
1557789Sahrens 		} else {
1558789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, ds);
15596689Smaybee 			dsl_dataset_drop_ref(ds_next->ds_prev, ds_next);
15606689Smaybee 			ds_next->ds_prev = NULL;
1561789Sahrens 			if (ds_prev) {
15626689Smaybee 				VERIFY(0 == dsl_dataset_get_ref(dp,
15636689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
15646689Smaybee 				    ds_next, &ds_next->ds_prev));
1565789Sahrens 			}
15665378Sck153898 
15675378Sck153898 			dsl_dataset_recalc_head_uniq(ds_next);
15685378Sck153898 
15695378Sck153898 			/*
15705378Sck153898 			 * Reduce the amount of our unconsmed refreservation
15715378Sck153898 			 * being charged to our parent by the amount of
15725378Sck153898 			 * new unique data we have gained.
15735378Sck153898 			 */
15745378Sck153898 			if (old_unique < ds_next->ds_reserved) {
15755378Sck153898 				int64_t mrsdelta;
15765378Sck153898 				uint64_t new_unique =
15775378Sck153898 				    ds_next->ds_phys->ds_unique_bytes;
15785378Sck153898 
15795378Sck153898 				ASSERT(old_unique <= new_unique);
15805378Sck153898 				mrsdelta = MIN(new_unique - old_unique,
15815378Sck153898 				    ds_next->ds_reserved - old_unique);
15825378Sck153898 				dsl_dir_diduse_space(ds->ds_dir, -mrsdelta,
15835378Sck153898 				    0, 0, tx);
15845378Sck153898 			}
1585789Sahrens 		}
15866689Smaybee 		dsl_dataset_rele(ds_next, FTAG);
1587789Sahrens 
1588789Sahrens 		/*
15895378Sck153898 		 * NB: unique_bytes might not be accurate for the head objset.
15905378Sck153898 		 * Before SPA_VERSION 9, we didn't update its value when we
15915378Sck153898 		 * deleted the most recent snapshot.
1592789Sahrens 		 */
1593789Sahrens 		ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes);
1594789Sahrens 	} else {
1595789Sahrens 		/*
1596789Sahrens 		 * There's no next snapshot, so this is a head dataset.
1597789Sahrens 		 * Destroy the deadlist.  Unless it's a clone, the
1598789Sahrens 		 * deadlist should be empty.  (If it's a clone, it's
1599789Sahrens 		 * safe to ignore the deadlist contents.)
1600789Sahrens 		 */
1601789Sahrens 		struct killarg ka;
1602789Sahrens 
1603789Sahrens 		ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist));
1604789Sahrens 		bplist_close(&ds->ds_deadlist);
1605789Sahrens 		bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1606789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1607789Sahrens 
1608789Sahrens 		/*
1609789Sahrens 		 * Free everything that we point to (that's born after
1610789Sahrens 		 * the previous snapshot, if we are a clone)
1611789Sahrens 		 *
1612789Sahrens 		 * XXX we're doing this long task with the config lock held
1613789Sahrens 		 */
1614789Sahrens 		ka.usedp = &used;
1615789Sahrens 		ka.compressedp = &compressed;
1616789Sahrens 		ka.uncompressedp = &uncompressed;
1617789Sahrens 		ka.zio = zio;
1618789Sahrens 		ka.tx = tx;
1619789Sahrens 		err = traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
1620789Sahrens 		    ADVANCE_POST, kill_blkptr, &ka);
1621789Sahrens 		ASSERT3U(err, ==, 0);
16225378Sck153898 		ASSERT(spa_version(dp->dp_spa) <
16235378Sck153898 		    SPA_VERSION_UNIQUE_ACCURATE ||
16245378Sck153898 		    used == ds->ds_phys->ds_unique_bytes);
1625789Sahrens 	}
1626789Sahrens 
1627789Sahrens 	err = zio_wait(zio);
1628789Sahrens 	ASSERT3U(err, ==, 0);
1629789Sahrens 
16302199Sahrens 	dsl_dir_diduse_space(ds->ds_dir, -used, -compressed, -uncompressed, tx);
1631789Sahrens 
16326689Smaybee 	if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
16336689Smaybee 		/* Erase the link in the dir */
16346689Smaybee 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
16356689Smaybee 		ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
16366689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
1637789Sahrens 		err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1638789Sahrens 		ASSERT(err == 0);
1639789Sahrens 	} else {
1640789Sahrens 		/* remove from snapshot namespace */
1641789Sahrens 		dsl_dataset_t *ds_head;
16426689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
16436689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
16446689Smaybee 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
16452207Sahrens 		VERIFY(0 == dsl_dataset_get_snapname(ds));
1646789Sahrens #ifdef ZFS_DEBUG
1647789Sahrens 		{
1648789Sahrens 			uint64_t val;
16496492Stimh 
16506689Smaybee 			err = dsl_dataset_snap_lookup(ds_head,
16516492Stimh 			    ds->ds_snapname, &val);
1652789Sahrens 			ASSERT3U(err, ==, 0);
1653789Sahrens 			ASSERT3U(val, ==, obj);
1654789Sahrens 		}
1655789Sahrens #endif
16566689Smaybee 		err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
1657789Sahrens 		ASSERT(err == 0);
16586689Smaybee 		dsl_dataset_rele(ds_head, FTAG);
1659789Sahrens 	}
1660789Sahrens 
1661789Sahrens 	if (ds_prev && ds->ds_prev != ds_prev)
16626689Smaybee 		dsl_dataset_rele(ds_prev, FTAG);
1663789Sahrens 
16645094Slling 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
16654543Smarks 	spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx,
16664543Smarks 	    cr, "dataset = %llu", ds->ds_object);
16674543Smarks 
16687046Sahrens 	if (ds->ds_phys->ds_next_clones_obj != 0) {
16697046Sahrens 		uint64_t count;
16707046Sahrens 		ASSERT(0 == zap_count(mos,
16717046Sahrens 		    ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
16727046Sahrens 		VERIFY(0 == dmu_object_free(mos,
16737046Sahrens 		    ds->ds_phys->ds_next_clones_obj, tx));
16747046Sahrens 	}
16757265Sahrens 	if (ds->ds_phys->ds_props_obj != 0) {
16767265Sahrens 		VERIFY(0 == zap_destroy(mos,
16777265Sahrens 		    ds->ds_phys->ds_props_obj, tx));
16787265Sahrens 	}
16796689Smaybee 	dsl_dir_close(ds->ds_dir, ds);
16806689Smaybee 	ds->ds_dir = NULL;
16816689Smaybee 	dsl_dataset_drain_refs(ds, tag);
16822199Sahrens 	VERIFY(0 == dmu_object_free(mos, obj, tx));
16832199Sahrens }
16842199Sahrens 
16855378Sck153898 static int
16865378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
16875378Sck153898 {
16885378Sck153898 	uint64_t asize;
16895378Sck153898 
16905378Sck153898 	if (!dmu_tx_is_syncing(tx))
16915378Sck153898 		return (0);
16925378Sck153898 
16935378Sck153898 	/*
16945378Sck153898 	 * If there's an fs-only reservation, any blocks that might become
16955378Sck153898 	 * owned by the snapshot dataset must be accommodated by space
16965378Sck153898 	 * outside of the reservation.
16975378Sck153898 	 */
16985378Sck153898 	asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
16995378Sck153898 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE))
17005378Sck153898 		return (ENOSPC);
17015378Sck153898 
17025378Sck153898 	/*
17035378Sck153898 	 * Propogate any reserved space for this snapshot to other
17045378Sck153898 	 * snapshot checks in this sync group.
17055378Sck153898 	 */
17065378Sck153898 	if (asize > 0)
17075378Sck153898 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
17085378Sck153898 
17095378Sck153898 	return (0);
17105378Sck153898 }
17115378Sck153898 
17122199Sahrens /* ARGSUSED */
17132199Sahrens int
17142199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
17152199Sahrens {
17165367Sahrens 	dsl_dataset_t *ds = arg1;
17172199Sahrens 	const char *snapname = arg2;
17182199Sahrens 	int err;
17192199Sahrens 	uint64_t value;
1720789Sahrens 
1721789Sahrens 	/*
17222199Sahrens 	 * We don't allow multiple snapshots of the same txg.  If there
17232199Sahrens 	 * is already one, try again.
17242199Sahrens 	 */
17252199Sahrens 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
17262199Sahrens 		return (EAGAIN);
17272199Sahrens 
17282199Sahrens 	/*
17292199Sahrens 	 * Check for conflicting name snapshot name.
1730789Sahrens 	 */
17316689Smaybee 	err = dsl_dataset_snap_lookup(ds, snapname, &value);
17322199Sahrens 	if (err == 0)
17332199Sahrens 		return (EEXIST);
17342199Sahrens 	if (err != ENOENT)
17352199Sahrens 		return (err);
1736789Sahrens 
17373978Smmusante 	/*
17383978Smmusante 	 * Check that the dataset's name is not too long.  Name consists
17393978Smmusante 	 * of the dataset's length + 1 for the @-sign + snapshot name's length
17403978Smmusante 	 */
17413978Smmusante 	if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
17423978Smmusante 		return (ENAMETOOLONG);
17433978Smmusante 
17445378Sck153898 	err = dsl_dataset_snapshot_reserve_space(ds, tx);
17455378Sck153898 	if (err)
17465378Sck153898 		return (err);
17475378Sck153898 
17482199Sahrens 	ds->ds_trysnap_txg = tx->tx_txg;
1749789Sahrens 	return (0);
1750789Sahrens }
1751789Sahrens 
17522199Sahrens void
17534543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1754789Sahrens {
17555367Sahrens 	dsl_dataset_t *ds = arg1;
17562199Sahrens 	const char *snapname = arg2;
17572199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1758789Sahrens 	dmu_buf_t *dbuf;
1759789Sahrens 	dsl_dataset_phys_t *dsphys;
17607046Sahrens 	uint64_t dsobj, crtxg;
1761789Sahrens 	objset_t *mos = dp->dp_meta_objset;
1762789Sahrens 	int err;
1763789Sahrens 
17642199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1765789Sahrens 
17667046Sahrens 	/*
17677046Sahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
17687046Sahrens 	 */
17697046Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
17707046Sahrens 		crtxg = 1;
17717046Sahrens 	else
17727046Sahrens 		crtxg = tx->tx_txg;
17737046Sahrens 
1774928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1775928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
17761544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1777789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1778789Sahrens 	dsphys = dbuf->db_data;
17796689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
17802199Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1781789Sahrens 	dsphys->ds_fsid_guid = unique_create();
1782789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1783789Sahrens 	    sizeof (dsphys->ds_guid));
1784789Sahrens 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1785789Sahrens 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1786789Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1787789Sahrens 	dsphys->ds_num_children = 1;
1788789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
17897046Sahrens 	dsphys->ds_creation_txg = crtxg;
1790789Sahrens 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1791789Sahrens 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
1792789Sahrens 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1793789Sahrens 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
17942082Seschrock 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1795789Sahrens 	dsphys->ds_bp = ds->ds_phys->ds_bp;
17961544Seschrock 	dmu_buf_rele(dbuf, FTAG);
1797789Sahrens 
17982199Sahrens 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
17992199Sahrens 	if (ds->ds_prev) {
18007046Sahrens 		uint64_t next_clones_obj =
18017046Sahrens 		    ds->ds_prev->ds_phys->ds_next_clones_obj;
18022199Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1803789Sahrens 		    ds->ds_object ||
18042199Sahrens 		    ds->ds_prev->ds_phys->ds_num_children > 1);
18052199Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
18062199Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1807789Sahrens 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
18082199Sahrens 			    ds->ds_prev->ds_phys->ds_creation_txg);
18092199Sahrens 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
18107046Sahrens 		} else if (next_clones_obj != 0) {
18117046Sahrens 			VERIFY3U(0, ==, zap_remove_int(mos,
18127046Sahrens 			    next_clones_obj, dsphys->ds_next_snap_obj, tx));
18137046Sahrens 			VERIFY3U(0, ==, zap_add_int(mos,
18147046Sahrens 			    next_clones_obj, dsobj, tx));
1815789Sahrens 		}
1816789Sahrens 	}
1817789Sahrens 
18185378Sck153898 	/*
18195378Sck153898 	 * If we have a reference-reservation on this dataset, we will
18205378Sck153898 	 * need to increase the amount of refreservation being charged
18215378Sck153898 	 * since our unique space is going to zero.
18225378Sck153898 	 */
18235378Sck153898 	if (ds->ds_reserved) {
18245378Sck153898 		int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
18255378Sck153898 		dsl_dir_diduse_space(ds->ds_dir, add, 0, 0, tx);
18265378Sck153898 	}
18275378Sck153898 
1828789Sahrens 	bplist_close(&ds->ds_deadlist);
1829789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
18305712Sahrens 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1831789Sahrens 	ds->ds_phys->ds_prev_snap_obj = dsobj;
18327046Sahrens 	ds->ds_phys->ds_prev_snap_txg = crtxg;
1833789Sahrens 	ds->ds_phys->ds_unique_bytes = 0;
18345378Sck153898 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
18355378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1836789Sahrens 	ds->ds_phys->ds_deadlist_obj =
1837789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
18381544Seschrock 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
18391544Seschrock 	    ds->ds_phys->ds_deadlist_obj));
1840789Sahrens 
1841789Sahrens 	dprintf("snap '%s' -> obj %llu\n", snapname, dsobj);
1842789Sahrens 	err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1843789Sahrens 	    snapname, 8, 1, &dsobj, tx);
1844789Sahrens 	ASSERT(err == 0);
1845789Sahrens 
1846789Sahrens 	if (ds->ds_prev)
18476689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
18486689Smaybee 	VERIFY(0 == dsl_dataset_get_ref(dp,
18496689Smaybee 	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
18504543Smarks 
18517046Sahrens 	dsl_pool_ds_snapshotted(ds, tx);
18527046Sahrens 
18534543Smarks 	spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr,
18544603Sahrens 	    "dataset = %llu", dsobj);
1855789Sahrens }
1856789Sahrens 
1857789Sahrens void
18583547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1859789Sahrens {
1860789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1861789Sahrens 	ASSERT(ds->ds_user_ptr != NULL);
1862789Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1863789Sahrens 
18644787Sahrens 	/*
18654787Sahrens 	 * in case we had to change ds_fsid_guid when we opened it,
18664787Sahrens 	 * sync it out now.
18674787Sahrens 	 */
18684787Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
18694787Sahrens 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
18704787Sahrens 
1871789Sahrens 	dsl_dir_dirty(ds->ds_dir, tx);
18723547Smaybee 	dmu_objset_sync(ds->ds_user_ptr, zio, tx);
1873789Sahrens }
1874789Sahrens 
1875789Sahrens void
18762885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1877789Sahrens {
18785378Sck153898 	uint64_t refd, avail, uobjs, aobjs;
18795378Sck153898 
18802885Sahrens 	dsl_dir_stats(ds->ds_dir, nv);
1881789Sahrens 
18825378Sck153898 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
18835378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
18845378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
18855378Sck153898 
18862885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
18872885Sahrens 	    ds->ds_phys->ds_creation_time);
18882885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
18892885Sahrens 	    ds->ds_phys->ds_creation_txg);
18905378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
18915378Sck153898 	    ds->ds_quota);
18925378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
18935378Sck153898 	    ds->ds_reserved);
18946643Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
18956643Seschrock 	    ds->ds_phys->ds_guid);
1896789Sahrens 
1897789Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
1898789Sahrens 		/*
1899789Sahrens 		 * This is a snapshot; override the dd's space used with
19002885Sahrens 		 * our unique space and compression ratio.
1901789Sahrens 		 */
19022885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
19032885Sahrens 		    ds->ds_phys->ds_unique_bytes);
19042885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
19052885Sahrens 		    ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
19062885Sahrens 		    (ds->ds_phys->ds_uncompressed_bytes * 100 /
19072885Sahrens 		    ds->ds_phys->ds_compressed_bytes));
1908789Sahrens 	}
1909789Sahrens }
1910789Sahrens 
19112885Sahrens void
19122885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1913789Sahrens {
19142885Sahrens 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
19152885Sahrens 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
19165367Sahrens 	stat->dds_guid = ds->ds_phys->ds_guid;
19172885Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
19182885Sahrens 		stat->dds_is_snapshot = B_TRUE;
19192885Sahrens 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
19202885Sahrens 	}
19212885Sahrens 
19222885Sahrens 	/* clone origin is really a dsl_dir thing... */
19235446Sahrens 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
19247046Sahrens 	if (dsl_dir_is_clone(ds->ds_dir)) {
19252885Sahrens 		dsl_dataset_t *ods;
19262885Sahrens 
19276689Smaybee 		VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
19286689Smaybee 		    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
19295367Sahrens 		dsl_dataset_name(ods, stat->dds_origin);
19306689Smaybee 		dsl_dataset_drop_ref(ods, FTAG);
19312885Sahrens 	}
19325446Sahrens 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
19332885Sahrens }
19342885Sahrens 
19352885Sahrens uint64_t
19362885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
19372885Sahrens {
19384787Sahrens 	return (ds->ds_fsid_guid);
19392885Sahrens }
19402885Sahrens 
19412885Sahrens void
19422885Sahrens dsl_dataset_space(dsl_dataset_t *ds,
19432885Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
19442885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
19452885Sahrens {
19462885Sahrens 	*refdbytesp = ds->ds_phys->ds_used_bytes;
19472885Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
19485378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
19495378Sck153898 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
19505378Sck153898 	if (ds->ds_quota != 0) {
19515378Sck153898 		/*
19525378Sck153898 		 * Adjust available bytes according to refquota
19535378Sck153898 		 */
19545378Sck153898 		if (*refdbytesp < ds->ds_quota)
19555378Sck153898 			*availbytesp = MIN(*availbytesp,
19565378Sck153898 			    ds->ds_quota - *refdbytesp);
19575378Sck153898 		else
19585378Sck153898 			*availbytesp = 0;
19595378Sck153898 	}
19602885Sahrens 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
19612885Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1962789Sahrens }
1963789Sahrens 
19645326Sek110237 boolean_t
19655326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
19665326Sek110237 {
19675326Sek110237 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
19685326Sek110237 
19695326Sek110237 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
19705326Sek110237 	    dsl_pool_sync_context(dp));
19715326Sek110237 	if (ds->ds_prev == NULL)
19725326Sek110237 		return (B_FALSE);
19735326Sek110237 	if (ds->ds_phys->ds_bp.blk_birth >
19745326Sek110237 	    ds->ds_prev->ds_phys->ds_creation_txg)
19755326Sek110237 		return (B_TRUE);
19765326Sek110237 	return (B_FALSE);
19775326Sek110237 }
19785326Sek110237 
19792199Sahrens /* ARGSUSED */
1980789Sahrens static int
19812199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
1982789Sahrens {
19832199Sahrens 	dsl_dataset_t *ds = arg1;
19842199Sahrens 	char *newsnapname = arg2;
19852199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
19862199Sahrens 	dsl_dataset_t *hds;
19872199Sahrens 	uint64_t val;
1988789Sahrens 	int err;
1989789Sahrens 
19906689Smaybee 	err = dsl_dataset_hold_obj(dd->dd_pool,
19916689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds);
1992789Sahrens 	if (err)
1993789Sahrens 		return (err);
1994789Sahrens 
19952199Sahrens 	/* new name better not be in use */
19966689Smaybee 	err = dsl_dataset_snap_lookup(hds, newsnapname, &val);
19976689Smaybee 	dsl_dataset_rele(hds, FTAG);
1998789Sahrens 
19992199Sahrens 	if (err == 0)
20002199Sahrens 		err = EEXIST;
20012199Sahrens 	else if (err == ENOENT)
20022199Sahrens 		err = 0;
20034007Smmusante 
20044007Smmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
20054007Smmusante 	if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
20064007Smmusante 		err = ENAMETOOLONG;
20074007Smmusante 
20082199Sahrens 	return (err);
20092199Sahrens }
2010789Sahrens 
20112199Sahrens static void
20124543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2,
20134543Smarks     cred_t *cr, dmu_tx_t *tx)
20142199Sahrens {
20152199Sahrens 	dsl_dataset_t *ds = arg1;
20164543Smarks 	const char *newsnapname = arg2;
20172199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
20182199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
20192199Sahrens 	dsl_dataset_t *hds;
20202199Sahrens 	int err;
2021789Sahrens 
20222199Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2023789Sahrens 
20246689Smaybee 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
20256689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
2026789Sahrens 
20272199Sahrens 	VERIFY(0 == dsl_dataset_get_snapname(ds));
20286689Smaybee 	err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
2029789Sahrens 	ASSERT3U(err, ==, 0);
20302199Sahrens 	mutex_enter(&ds->ds_lock);
20312199Sahrens 	(void) strcpy(ds->ds_snapname, newsnapname);
20322199Sahrens 	mutex_exit(&ds->ds_lock);
20332199Sahrens 	err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
20342199Sahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2035789Sahrens 	ASSERT3U(err, ==, 0);
2036789Sahrens 
20374543Smarks 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
20384543Smarks 	    cr, "dataset = %llu", ds->ds_object);
20396689Smaybee 	dsl_dataset_rele(hds, FTAG);
2040789Sahrens }
2041789Sahrens 
20425326Sek110237 struct renamesnaparg {
20434007Smmusante 	dsl_sync_task_group_t *dstg;
20444007Smmusante 	char failed[MAXPATHLEN];
20454007Smmusante 	char *oldsnap;
20464007Smmusante 	char *newsnap;
20474007Smmusante };
20484007Smmusante 
20494007Smmusante static int
20504007Smmusante dsl_snapshot_rename_one(char *name, void *arg)
20514007Smmusante {
20525326Sek110237 	struct renamesnaparg *ra = arg;
20534007Smmusante 	dsl_dataset_t *ds = NULL;
20544007Smmusante 	char *cp;
20554007Smmusante 	int err;
20564007Smmusante 
20574007Smmusante 	cp = name + strlen(name);
20584007Smmusante 	*cp = '@';
20594007Smmusante 	(void) strcpy(cp + 1, ra->oldsnap);
20604543Smarks 
20614543Smarks 	/*
20624543Smarks 	 * For recursive snapshot renames the parent won't be changing
20634543Smarks 	 * so we just pass name for both the to/from argument.
20644543Smarks 	 */
2065*7312SMatthew.Ahrens@Sun.COM 	err = zfs_secpolicy_rename_perms(name, name, CRED());
2066*7312SMatthew.Ahrens@Sun.COM 	if (err == ENOENT) {
2067*7312SMatthew.Ahrens@Sun.COM 		return (0);
2068*7312SMatthew.Ahrens@Sun.COM 	} else if (err) {
20694543Smarks 		(void) strcpy(ra->failed, name);
20704543Smarks 		return (err);
20714543Smarks 	}
20724543Smarks 
20736689Smaybee #ifdef _KERNEL
20746689Smaybee 	/*
20756689Smaybee 	 * For all filesystems undergoing rename, we'll need to unmount it.
20766689Smaybee 	 */
20776689Smaybee 	(void) zfs_unmount_snap(name, NULL);
20786689Smaybee #endif
20796689Smaybee 	err = dsl_dataset_hold(name, ra->dstg, &ds);
20806689Smaybee 	*cp = '\0';
20814007Smmusante 	if (err == ENOENT) {
20824007Smmusante 		return (0);
20836689Smaybee 	} else if (err) {
20844007Smmusante 		(void) strcpy(ra->failed, name);
20854007Smmusante 		return (err);
20864007Smmusante 	}
20874007Smmusante 
20884007Smmusante 	dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
20894007Smmusante 	    dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
20904007Smmusante 
20914007Smmusante 	return (0);
20924007Smmusante }
20934007Smmusante 
20944007Smmusante static int
20954007Smmusante dsl_recursive_rename(char *oldname, const char *newname)
20964007Smmusante {
20974007Smmusante 	int err;
20985326Sek110237 	struct renamesnaparg *ra;
20994007Smmusante 	dsl_sync_task_t *dst;
21004007Smmusante 	spa_t *spa;
21014007Smmusante 	char *cp, *fsname = spa_strdup(oldname);
21024007Smmusante 	int len = strlen(oldname);
21034007Smmusante 
21044007Smmusante 	/* truncate the snapshot name to get the fsname */
21054007Smmusante 	cp = strchr(fsname, '@');
21064007Smmusante 	*cp = '\0';
21074007Smmusante 
21084603Sahrens 	err = spa_open(fsname, &spa, FTAG);
21094007Smmusante 	if (err) {
21104007Smmusante 		kmem_free(fsname, len + 1);
21114007Smmusante 		return (err);
21124007Smmusante 	}
21135326Sek110237 	ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP);
21144007Smmusante 	ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
21154007Smmusante 
21164007Smmusante 	ra->oldsnap = strchr(oldname, '@') + 1;
21174007Smmusante 	ra->newsnap = strchr(newname, '@') + 1;
21184007Smmusante 	*ra->failed = '\0';
21194007Smmusante 
21204007Smmusante 	err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
21214007Smmusante 	    DS_FIND_CHILDREN);
21224007Smmusante 	kmem_free(fsname, len + 1);
21234007Smmusante 
21244007Smmusante 	if (err == 0) {
21254007Smmusante 		err = dsl_sync_task_group_wait(ra->dstg);
21264007Smmusante 	}
21274007Smmusante 
21284007Smmusante 	for (dst = list_head(&ra->dstg->dstg_tasks); dst;
21294007Smmusante 	    dst = list_next(&ra->dstg->dstg_tasks, dst)) {
21304007Smmusante 		dsl_dataset_t *ds = dst->dst_arg1;
21314007Smmusante 		if (dst->dst_err) {
21324007Smmusante 			dsl_dir_name(ds->ds_dir, ra->failed);
21334009Smmusante 			(void) strcat(ra->failed, "@");
21344009Smmusante 			(void) strcat(ra->failed, ra->newsnap);
21354007Smmusante 		}
21366689Smaybee 		dsl_dataset_rele(ds, ra->dstg);
21374007Smmusante 	}
21384007Smmusante 
21394543Smarks 	if (err)
21404543Smarks 		(void) strcpy(oldname, ra->failed);
21414007Smmusante 
21424007Smmusante 	dsl_sync_task_group_destroy(ra->dstg);
21435326Sek110237 	kmem_free(ra, sizeof (struct renamesnaparg));
21444007Smmusante 	spa_close(spa, FTAG);
21454007Smmusante 	return (err);
21464007Smmusante }
21474007Smmusante 
21484569Smmusante static int
21494569Smmusante dsl_valid_rename(char *oldname, void *arg)
21504569Smmusante {
21514569Smmusante 	int delta = *(int *)arg;
21524569Smmusante 
21534569Smmusante 	if (strlen(oldname) + delta >= MAXNAMELEN)
21544569Smmusante 		return (ENAMETOOLONG);
21554569Smmusante 
21564569Smmusante 	return (0);
21574569Smmusante }
21584569Smmusante 
2159789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename
2160789Sahrens int
21616689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive)
2162789Sahrens {
2163789Sahrens 	dsl_dir_t *dd;
21642199Sahrens 	dsl_dataset_t *ds;
2165789Sahrens 	const char *tail;
2166789Sahrens 	int err;
2167789Sahrens 
21682199Sahrens 	err = dsl_dir_open(oldname, FTAG, &dd, &tail);
21691544Seschrock 	if (err)
21701544Seschrock 		return (err);
2171789Sahrens 	if (tail == NULL) {
21724569Smmusante 		int delta = strlen(newname) - strlen(oldname);
21734569Smmusante 
21747046Sahrens 		/* if we're growing, validate child name lengths */
21754569Smmusante 		if (delta > 0)
21764569Smmusante 			err = dmu_objset_find(oldname, dsl_valid_rename,
21774569Smmusante 			    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
21784569Smmusante 
21794569Smmusante 		if (!err)
21804569Smmusante 			err = dsl_dir_rename(dd, newname);
2181789Sahrens 		dsl_dir_close(dd, FTAG);
2182789Sahrens 		return (err);
2183789Sahrens 	}
2184789Sahrens 	if (tail[0] != '@') {
2185789Sahrens 		/* the name ended in a nonexistant component */
2186789Sahrens 		dsl_dir_close(dd, FTAG);
2187789Sahrens 		return (ENOENT);
2188789Sahrens 	}
2189789Sahrens 
21902199Sahrens 	dsl_dir_close(dd, FTAG);
21912199Sahrens 
21922199Sahrens 	/* new name must be snapshot in same filesystem */
21932199Sahrens 	tail = strchr(newname, '@');
21942199Sahrens 	if (tail == NULL)
21952199Sahrens 		return (EINVAL);
21962199Sahrens 	tail++;
21972199Sahrens 	if (strncmp(oldname, newname, tail - newname) != 0)
21982199Sahrens 		return (EXDEV);
2199789Sahrens 
22004007Smmusante 	if (recursive) {
22014007Smmusante 		err = dsl_recursive_rename(oldname, newname);
22024007Smmusante 	} else {
22036689Smaybee 		err = dsl_dataset_hold(oldname, FTAG, &ds);
22044007Smmusante 		if (err)
22054007Smmusante 			return (err);
22062199Sahrens 
22074007Smmusante 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
22084007Smmusante 		    dsl_dataset_snapshot_rename_check,
22094007Smmusante 		    dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
22102199Sahrens 
22116689Smaybee 		dsl_dataset_rele(ds, FTAG);
22124007Smmusante 	}
22132199Sahrens 
2214789Sahrens 	return (err);
2215789Sahrens }
22162082Seschrock 
22177046Sahrens struct promotenode {
22186689Smaybee 	list_node_t link;
22196689Smaybee 	dsl_dataset_t *ds;
22206689Smaybee };
22216689Smaybee 
22222199Sahrens struct promotearg {
22236689Smaybee 	list_t snap_list;
22246689Smaybee 	dsl_dataset_t *clone_origin, *old_head;
22252199Sahrens 	uint64_t used, comp, uncomp, unique;
22266689Smaybee 	uint64_t newnext_obj;
22272199Sahrens };
22282199Sahrens 
22294543Smarks /* ARGSUSED */
22302082Seschrock static int
22312199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
22322082Seschrock {
22332199Sahrens 	dsl_dataset_t *hds = arg1;
22342199Sahrens 	struct promotearg *pa = arg2;
22357046Sahrens 	struct promotenode *snap = list_head(&pa->snap_list);
22362199Sahrens 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
22376689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
22386689Smaybee 	dsl_dataset_t *newnext_ds;
22396689Smaybee 	char *name;
22402199Sahrens 	uint64_t itor = 0;
22412082Seschrock 	blkptr_t bp;
22426689Smaybee 	int err;
22432199Sahrens 
22447046Sahrens 	/* Check that it is a real clone */
22457046Sahrens 	if (!dsl_dir_is_clone(hds->ds_dir))
22462082Seschrock 		return (EINVAL);
22472082Seschrock 
22482199Sahrens 	/* Since this is so expensive, don't do the preliminary check */
22492199Sahrens 	if (!dmu_tx_is_syncing(tx))
22502199Sahrens 		return (0);
22512199Sahrens 
22526689Smaybee 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)
22536689Smaybee 		return (EXDEV);
22542082Seschrock 
22555367Sahrens 	/* find origin's new next ds */
22566689Smaybee 	newnext_ds = hds;
22575367Sahrens 	while (newnext_ds->ds_phys->ds_prev_snap_obj != origin_ds->ds_object) {
22582082Seschrock 		dsl_dataset_t *prev;
22592082Seschrock 
22606689Smaybee 		err = dsl_dataset_hold_obj(dp,
22616689Smaybee 		    newnext_ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
22626689Smaybee 		if (newnext_ds != hds)
22636689Smaybee 			dsl_dataset_rele(newnext_ds, FTAG);
22646689Smaybee 		if (err)
22656689Smaybee 			return (err);
22662082Seschrock 		newnext_ds = prev;
22672082Seschrock 	}
22682199Sahrens 	pa->newnext_obj = newnext_ds->ds_object;
22692082Seschrock 
22705367Sahrens 	/* compute origin's new unique space */
22716689Smaybee 	pa->unique = 0;
22722082Seschrock 	while ((err = bplist_iterate(&newnext_ds->ds_deadlist,
22732082Seschrock 	    &itor, &bp)) == 0) {
22745367Sahrens 		if (bp.blk_birth > origin_ds->ds_phys->ds_prev_snap_txg)
22756689Smaybee 			pa->unique += bp_get_dasize(dp->dp_spa, &bp);
22762082Seschrock 	}
22776689Smaybee 	if (newnext_ds != hds)
22786689Smaybee 		dsl_dataset_rele(newnext_ds, FTAG);
22792082Seschrock 	if (err != ENOENT)
22806689Smaybee 		return (err);
22816689Smaybee 
22822082Seschrock 	name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
22836689Smaybee 
22846689Smaybee 	/*
22856689Smaybee 	 * Walk the snapshots that we are moving
22866689Smaybee 	 *
22876689Smaybee 	 * Compute space to transfer.  Each snapshot gave birth to:
22886689Smaybee 	 * (my used) - (prev's used) + (deadlist's used)
22896689Smaybee 	 * So a sequence would look like:
22906689Smaybee 	 * uN - u(N-1) + dN + ... + u1 - u0 + d1 + u0 - 0 + d0
22916689Smaybee 	 * Which simplifies to:
22926689Smaybee 	 * uN + dN + ... + d1 + d0
22936689Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
22946689Smaybee 	 * uN + dN + ... + dM - uM-1
22956689Smaybee 	 */
22966689Smaybee 	pa->used = origin_ds->ds_phys->ds_used_bytes;
22976689Smaybee 	pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
22986689Smaybee 	pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
22996689Smaybee 	do {
23002082Seschrock 		uint64_t val, dlused, dlcomp, dluncomp;
23016689Smaybee 		dsl_dataset_t *ds = snap->ds;
23022082Seschrock 
23032082Seschrock 		/* Check that the snapshot name does not conflict */
23042082Seschrock 		dsl_dataset_name(ds, name);
23056689Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
23066689Smaybee 		if (err == 0)
23076689Smaybee 			err = EEXIST;
23086689Smaybee 		if (err != ENOENT)
23092082Seschrock 			break;
23106689Smaybee 		err = 0;
23116689Smaybee 
23126689Smaybee 		/* The very first snapshot does not have a deadlist */
23136689Smaybee 		if (ds->ds_phys->ds_prev_snap_obj != 0) {
23146689Smaybee 			if (err = bplist_space(&ds->ds_deadlist,
23156689Smaybee 			    &dlused, &dlcomp, &dluncomp))
23166689Smaybee 				break;
23176689Smaybee 			pa->used += dlused;
23186689Smaybee 			pa->comp += dlcomp;
23196689Smaybee 			pa->uncomp += dluncomp;
23202082Seschrock 		}
23216689Smaybee 	} while (snap = list_next(&pa->snap_list, snap));
23226689Smaybee 
23236689Smaybee 	/*
23246689Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
23256689Smaybee 	 * so we need to subtract out the clone origin's used space.
23266689Smaybee 	 */
23276689Smaybee 	if (pa->clone_origin) {
23286689Smaybee 		pa->used -= pa->clone_origin->ds_phys->ds_used_bytes;
23296689Smaybee 		pa->comp -= pa->clone_origin->ds_phys->ds_compressed_bytes;
23306689Smaybee 		pa->uncomp -= pa->clone_origin->ds_phys->ds_uncompressed_bytes;
23312082Seschrock 	}
23322082Seschrock 
23336689Smaybee 	kmem_free(name, MAXPATHLEN);
23346689Smaybee 
23352082Seschrock 	/* Check that there is enough space here */
23366689Smaybee 	if (err == 0) {
23376689Smaybee 		dsl_dir_t *odd = origin_ds->ds_dir;
23386689Smaybee 		err = dsl_dir_transfer_possible(odd, hds->ds_dir, pa->used);
23396689Smaybee 	}
23406689Smaybee 
23412199Sahrens 	return (err);
23422199Sahrens }
23432082Seschrock 
23442199Sahrens static void
23454543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
23462199Sahrens {
23472199Sahrens 	dsl_dataset_t *hds = arg1;
23482199Sahrens 	struct promotearg *pa = arg2;
23497046Sahrens 	struct promotenode *snap = list_head(&pa->snap_list);
23506689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
23512199Sahrens 	dsl_dir_t *dd = hds->ds_dir;
23522199Sahrens 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
23535367Sahrens 	dsl_dir_t *odd = NULL;
23542199Sahrens 	char *name;
23557046Sahrens 	uint64_t oldnext_obj;
23562199Sahrens 
23572199Sahrens 	ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
23582199Sahrens 
23592417Sahrens 	/*
23605367Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
23612417Sahrens 	 * changing.
23622417Sahrens 	 */
23635367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object,
23645367Sahrens 	    NULL, FTAG, &odd));
23652082Seschrock 
23666689Smaybee 	/* change origin's next snap */
23676689Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
23687046Sahrens 	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
23696689Smaybee 	origin_ds->ds_phys->ds_next_snap_obj = pa->newnext_obj;
23706689Smaybee 
23717046Sahrens 	/* change the origin's next clone */
23727046Sahrens 	if (origin_ds->ds_phys->ds_next_clones_obj) {
23737046Sahrens 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
23747046Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
23757046Sahrens 		    pa->newnext_obj, tx));
23767046Sahrens 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
23777046Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
23787046Sahrens 		    oldnext_obj, tx));
23797046Sahrens 	}
23807046Sahrens 
23816689Smaybee 	/* change origin */
23826689Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
23836689Smaybee 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
23846689Smaybee 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
23856689Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
23866689Smaybee 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
23876689Smaybee 
23882082Seschrock 	/* move snapshots to this dir */
23892199Sahrens 	name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
23906689Smaybee 	do {
23916689Smaybee 		dsl_dataset_t *ds = snap->ds;
23922082Seschrock 
23937237Sek110237 		/* unregister props as dsl_dir is changing */
23947237Sek110237 		if (ds->ds_user_ptr) {
23957237Sek110237 			ds->ds_user_evict_func(ds, ds->ds_user_ptr);
23967237Sek110237 			ds->ds_user_ptr = NULL;
23977237Sek110237 		}
23982082Seschrock 		/* move snap name entry */
23992082Seschrock 		dsl_dataset_name(ds, name);
24006689Smaybee 		VERIFY(0 == dsl_dataset_snap_remove(pa->old_head,
24016689Smaybee 		    ds->ds_snapname, tx));
24022199Sahrens 		VERIFY(0 == zap_add(dp->dp_meta_objset,
24032082Seschrock 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
24042082Seschrock 		    8, 1, &ds->ds_object, tx));
24052082Seschrock 		/* change containing dsl_dir */
24062082Seschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
24075367Sahrens 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
24082082Seschrock 		ds->ds_phys->ds_dir_obj = dd->dd_object;
24095367Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
24102082Seschrock 		dsl_dir_close(ds->ds_dir, ds);
24112199Sahrens 		VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
24122082Seschrock 		    NULL, ds, &ds->ds_dir));
24132082Seschrock 
24142082Seschrock 		ASSERT3U(dsl_prop_numcb(ds), ==, 0);
24156689Smaybee 	} while (snap = list_next(&pa->snap_list, snap));
24162082Seschrock 
24172082Seschrock 	/* change space accounting */
24185367Sahrens 	dsl_dir_diduse_space(odd, -pa->used, -pa->comp, -pa->uncomp, tx);
24192199Sahrens 	dsl_dir_diduse_space(dd, pa->used, pa->comp, pa->uncomp, tx);
24205367Sahrens 	origin_ds->ds_phys->ds_unique_bytes = pa->unique;
24212082Seschrock 
24224543Smarks 	/* log history record */
24234543Smarks 	spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
24246689Smaybee 	    cr, "dataset = %llu", hds->ds_object);
24254543Smarks 
24265367Sahrens 	dsl_dir_close(odd, FTAG);
24272199Sahrens 	kmem_free(name, MAXPATHLEN);
24282082Seschrock }
24292082Seschrock 
24302082Seschrock int
24312082Seschrock dsl_dataset_promote(const char *name)
24322082Seschrock {
24332082Seschrock 	dsl_dataset_t *ds;
24346689Smaybee 	dsl_dir_t *dd;
24356689Smaybee 	dsl_pool_t *dp;
24362082Seschrock 	dmu_object_info_t doi;
24372199Sahrens 	struct promotearg pa;
24387046Sahrens 	struct promotenode *snap;
24396689Smaybee 	uint64_t snap_obj;
24406689Smaybee 	uint64_t last_snap = 0;
24416689Smaybee 	int err;
24426689Smaybee 
24436689Smaybee 	err = dsl_dataset_hold(name, FTAG, &ds);
24442082Seschrock 	if (err)
24452082Seschrock 		return (err);
24466689Smaybee 	dd = ds->ds_dir;
24476689Smaybee 	dp = dd->dd_pool;
24486689Smaybee 
24496689Smaybee 	err = dmu_object_info(dp->dp_meta_objset,
24502082Seschrock 	    ds->ds_phys->ds_snapnames_zapobj, &doi);
24512082Seschrock 	if (err) {
24526689Smaybee 		dsl_dataset_rele(ds, FTAG);
24532082Seschrock 		return (err);
24542082Seschrock 	}
24552082Seschrock 
24562082Seschrock 	/*
24576689Smaybee 	 * We are going to inherit all the snapshots taken before our
24586689Smaybee 	 * origin (i.e., our new origin will be our parent's origin).
24596689Smaybee 	 * Take ownership of them so that we can rename them into our
24606689Smaybee 	 * namespace.
24616689Smaybee 	 */
24626689Smaybee 	pa.clone_origin = NULL;
24636689Smaybee 	list_create(&pa.snap_list,
24647046Sahrens 	    sizeof (struct promotenode), offsetof(struct promotenode, link));
24656689Smaybee 	rw_enter(&dp->dp_config_rwlock, RW_READER);
24666689Smaybee 	ASSERT(dd->dd_phys->dd_origin_obj != 0);
24676689Smaybee 	snap_obj = dd->dd_phys->dd_origin_obj;
24686689Smaybee 	while (snap_obj) {
24697046Sahrens 		dsl_dataset_t *snapds;
24707046Sahrens 
24717046Sahrens 		/*
24727046Sahrens 		 * NB: this would be handled by the below check for
24737046Sahrens 		 * clone of a clone, but then we'd always own_obj() the
24747046Sahrens 		 * $ORIGIN, thus causing unnecessary EBUSYs.  We don't
24757046Sahrens 		 * need to set pa.clone_origin because the $ORIGIN has
24767046Sahrens 		 * no data to account for.
24777046Sahrens 		 */
24787046Sahrens 		if (dp->dp_origin_snap &&
24797046Sahrens 		    snap_obj == dp->dp_origin_snap->ds_object)
24807046Sahrens 			break;
24817046Sahrens 
24827046Sahrens 		err = dsl_dataset_own_obj(dp, snap_obj, 0, FTAG, &snapds);
24836689Smaybee 		if (err == ENOENT) {
24846689Smaybee 			/* lost race with snapshot destroy */
24857046Sahrens 			struct promotenode *last = list_tail(&pa.snap_list);
24866689Smaybee 			ASSERT(snap_obj != last->ds->ds_phys->ds_prev_snap_obj);
24876689Smaybee 			snap_obj = last->ds->ds_phys->ds_prev_snap_obj;
24886689Smaybee 			continue;
24896689Smaybee 		} else if (err) {
24906689Smaybee 			rw_exit(&dp->dp_config_rwlock);
24916689Smaybee 			goto out;
24926689Smaybee 		}
24937046Sahrens 
24946689Smaybee 		/*
24956689Smaybee 		 * We could be a clone of a clone.  If we reach our
24966689Smaybee 		 * parent's branch point, we're done.
24976689Smaybee 		 */
24986689Smaybee 		if (last_snap &&
24997046Sahrens 		    snapds->ds_phys->ds_next_snap_obj != last_snap) {
25007046Sahrens 			pa.clone_origin = snapds;
25017046Sahrens 			break;
25026689Smaybee 		}
25037046Sahrens 
25047046Sahrens 		snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP);
25057046Sahrens 		snap->ds = snapds;
25067046Sahrens 		list_insert_tail(&pa.snap_list, snap);
25077046Sahrens 		last_snap = snap_obj;
25087046Sahrens 		snap_obj = snap->ds->ds_phys->ds_prev_snap_obj;
25096689Smaybee 	}
25106689Smaybee 	snap = list_head(&pa.snap_list);
25116689Smaybee 	ASSERT(snap != NULL);
25126689Smaybee 	err = dsl_dataset_hold_obj(dp,
25136689Smaybee 	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &pa.old_head);
25146689Smaybee 	rw_exit(&dp->dp_config_rwlock);
25156689Smaybee 
25166689Smaybee 	if (err)
25176689Smaybee 		goto out;
25186689Smaybee 
25196689Smaybee 	/*
25202082Seschrock 	 * Add in 128x the snapnames zapobj size, since we will be moving
25212082Seschrock 	 * a bunch of snapnames to the promoted ds, and dirtying their
25222082Seschrock 	 * bonus buffers.
25232082Seschrock 	 */
25246689Smaybee 	err = dsl_sync_task_do(dp, dsl_dataset_promote_check,
25252199Sahrens 	    dsl_dataset_promote_sync, ds, &pa, 2 + 2 * doi.doi_physical_blks);
25266689Smaybee 
25276689Smaybee 	dsl_dataset_rele(pa.old_head, FTAG);
25286689Smaybee out:
25296689Smaybee 	while ((snap = list_tail(&pa.snap_list)) != NULL) {
25306689Smaybee 		list_remove(&pa.snap_list, snap);
25316689Smaybee 		dsl_dataset_disown(snap->ds, FTAG);
25327046Sahrens 		kmem_free(snap, sizeof (struct promotenode));
25336689Smaybee 	}
25346689Smaybee 	list_destroy(&pa.snap_list);
25356689Smaybee 	if (pa.clone_origin)
25366689Smaybee 		dsl_dataset_disown(pa.clone_origin, FTAG);
25376689Smaybee 	dsl_dataset_rele(ds, FTAG);
25382082Seschrock 	return (err);
25392082Seschrock }
25403912Slling 
25415367Sahrens struct cloneswaparg {
25425367Sahrens 	dsl_dataset_t *cds; /* clone dataset */
25435367Sahrens 	dsl_dataset_t *ohds; /* origin's head dataset */
25445367Sahrens 	boolean_t force;
25455481Sck153898 	int64_t unused_refres_delta; /* change in unconsumed refreservation */
25465367Sahrens };
25475326Sek110237 
25485326Sek110237 /* ARGSUSED */
25495326Sek110237 static int
25505326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx)
25515326Sek110237 {
25525367Sahrens 	struct cloneswaparg *csa = arg1;
25535326Sek110237 
25545367Sahrens 	/* they should both be heads */
25555367Sahrens 	if (dsl_dataset_is_snapshot(csa->cds) ||
25565367Sahrens 	    dsl_dataset_is_snapshot(csa->ohds))
25575326Sek110237 		return (EINVAL);
25585326Sek110237 
25595367Sahrens 	/* the branch point should be just before them */
25605367Sahrens 	if (csa->cds->ds_prev != csa->ohds->ds_prev)
25615326Sek110237 		return (EINVAL);
25625326Sek110237 
25635367Sahrens 	/* cds should be the clone */
25645367Sahrens 	if (csa->cds->ds_prev->ds_phys->ds_next_snap_obj !=
25655367Sahrens 	    csa->ohds->ds_object)
25665367Sahrens 		return (EINVAL);
25675326Sek110237 
25685367Sahrens 	/* the clone should be a child of the origin */
25695367Sahrens 	if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir)
25705367Sahrens 		return (EINVAL);
25715326Sek110237 
25725367Sahrens 	/* ohds shouldn't be modified unless 'force' */
25735367Sahrens 	if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds))
25745367Sahrens 		return (ETXTBSY);
25755481Sck153898 
25765481Sck153898 	/* adjust amount of any unconsumed refreservation */
25775481Sck153898 	csa->unused_refres_delta =
25785481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
25795481Sck153898 	    csa->ohds->ds_phys->ds_unique_bytes) -
25805481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
25815481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
25825481Sck153898 
25835481Sck153898 	if (csa->unused_refres_delta > 0 &&
25845481Sck153898 	    csa->unused_refres_delta >
25855481Sck153898 	    dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE))
25865481Sck153898 		return (ENOSPC);
25875481Sck153898 
25885367Sahrens 	return (0);
25895326Sek110237 }
25905326Sek110237 
25915326Sek110237 /* ARGSUSED */
25925326Sek110237 static void
25935326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
25945326Sek110237 {
25955367Sahrens 	struct cloneswaparg *csa = arg1;
25965367Sahrens 	dsl_pool_t *dp = csa->cds->ds_dir->dd_pool;
25975326Sek110237 	uint64_t itor = 0;
25985326Sek110237 	blkptr_t bp;
25995326Sek110237 	uint64_t unique = 0;
26005326Sek110237 	int err;
26015326Sek110237 
26025481Sck153898 	ASSERT(csa->cds->ds_reserved == 0);
26035481Sck153898 	ASSERT(csa->cds->ds_quota == csa->ohds->ds_quota);
26045481Sck153898 
26055367Sahrens 	dmu_buf_will_dirty(csa->cds->ds_dbuf, tx);
26065367Sahrens 	dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx);
26075367Sahrens 	dmu_buf_will_dirty(csa->cds->ds_prev->ds_dbuf, tx);
26085326Sek110237 
26095367Sahrens 	if (csa->cds->ds_user_ptr != NULL) {
26105367Sahrens 		csa->cds->ds_user_evict_func(csa->cds, csa->cds->ds_user_ptr);
26115367Sahrens 		csa->cds->ds_user_ptr = NULL;
26125367Sahrens 	}
26135326Sek110237 
26145367Sahrens 	if (csa->ohds->ds_user_ptr != NULL) {
26155367Sahrens 		csa->ohds->ds_user_evict_func(csa->ohds,
26165367Sahrens 		    csa->ohds->ds_user_ptr);
26175367Sahrens 		csa->ohds->ds_user_ptr = NULL;
26185367Sahrens 	}
26195326Sek110237 
26205326Sek110237 	/* compute unique space */
26215367Sahrens 	while ((err = bplist_iterate(&csa->cds->ds_deadlist,
26225367Sahrens 	    &itor, &bp)) == 0) {
26235367Sahrens 		if (bp.blk_birth > csa->cds->ds_prev->ds_phys->ds_prev_snap_txg)
26245367Sahrens 			unique += bp_get_dasize(dp->dp_spa, &bp);
26255326Sek110237 	}
26265326Sek110237 	VERIFY(err == ENOENT);
26275326Sek110237 
26285326Sek110237 	/* reset origin's unique bytes */
26295367Sahrens 	csa->cds->ds_prev->ds_phys->ds_unique_bytes = unique;
26305326Sek110237 
26315326Sek110237 	/* swap blkptrs */
26325326Sek110237 	{
26335326Sek110237 		blkptr_t tmp;
26345367Sahrens 		tmp = csa->ohds->ds_phys->ds_bp;
26355367Sahrens 		csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp;
26365367Sahrens 		csa->cds->ds_phys->ds_bp = tmp;
26375326Sek110237 	}
26385326Sek110237 
26395326Sek110237 	/* set dd_*_bytes */
26405326Sek110237 	{
26415326Sek110237 		int64_t dused, dcomp, duncomp;
26425326Sek110237 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
26435326Sek110237 		uint64_t odl_used, odl_comp, odl_uncomp;
26445326Sek110237 
26455367Sahrens 		VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used,
26465326Sek110237 		    &cdl_comp, &cdl_uncomp));
26475367Sahrens 		VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used,
26485326Sek110237 		    &odl_comp, &odl_uncomp));
26495367Sahrens 		dused = csa->cds->ds_phys->ds_used_bytes + cdl_used -
26505367Sahrens 		    (csa->ohds->ds_phys->ds_used_bytes + odl_used);
26515367Sahrens 		dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
26525367Sahrens 		    (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
26535367Sahrens 		duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
26545367Sahrens 		    cdl_uncomp -
26555367Sahrens 		    (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
26565326Sek110237 
26575367Sahrens 		dsl_dir_diduse_space(csa->ohds->ds_dir,
26585367Sahrens 		    dused, dcomp, duncomp, tx);
26595367Sahrens 		dsl_dir_diduse_space(csa->cds->ds_dir,
26605367Sahrens 		    -dused, -dcomp, -duncomp, tx);
26615367Sahrens 	}
26625367Sahrens 
26635367Sahrens #define	SWITCH64(x, y) \
26645367Sahrens 	{ \
26655367Sahrens 		uint64_t __tmp = (x); \
26665367Sahrens 		(x) = (y); \
26675367Sahrens 		(y) = __tmp; \
26685326Sek110237 	}
26695326Sek110237 
26705326Sek110237 	/* swap ds_*_bytes */
26715367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_used_bytes,
26725367Sahrens 	    csa->cds->ds_phys->ds_used_bytes);
26735367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
26745367Sahrens 	    csa->cds->ds_phys->ds_compressed_bytes);
26755367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
26765367Sahrens 	    csa->cds->ds_phys->ds_uncompressed_bytes);
26775481Sck153898 	SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
26785481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
26795481Sck153898 
26805481Sck153898 	/* apply any parent delta for change in unconsumed refreservation */
26815481Sck153898 	dsl_dir_diduse_space(csa->ohds->ds_dir, csa->unused_refres_delta,
26825481Sck153898 	    0, 0, tx);
26835326Sek110237 
26845326Sek110237 	/* swap deadlists */
26855367Sahrens 	bplist_close(&csa->cds->ds_deadlist);
26865367Sahrens 	bplist_close(&csa->ohds->ds_deadlist);
26875367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
26885367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj);
26895367Sahrens 	VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
26905367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj));
26915367Sahrens 	VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
26925367Sahrens 	    csa->ohds->ds_phys->ds_deadlist_obj));
26935326Sek110237 }
26945326Sek110237 
26955326Sek110237 /*
26966689Smaybee  * Swap 'clone' with its origin head file system.  Used at the end
26976689Smaybee  * of "online recv" to swizzle the file system to the new version.
26985326Sek110237  */
26995326Sek110237 int
27005367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
27015367Sahrens     boolean_t force)
27025326Sek110237 {
27035367Sahrens 	struct cloneswaparg csa;
27046689Smaybee 	int error;
27056689Smaybee 
27066689Smaybee 	ASSERT(clone->ds_owner);
27076689Smaybee 	ASSERT(origin_head->ds_owner);
27086689Smaybee retry:
27096689Smaybee 	/* Need exclusive access for the swap */
27106689Smaybee 	rw_enter(&clone->ds_rwlock, RW_WRITER);
27116689Smaybee 	if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) {
27126689Smaybee 		rw_exit(&clone->ds_rwlock);
27136689Smaybee 		rw_enter(&origin_head->ds_rwlock, RW_WRITER);
27146689Smaybee 		if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) {
27156689Smaybee 			rw_exit(&origin_head->ds_rwlock);
27166689Smaybee 			goto retry;
27176689Smaybee 		}
27186689Smaybee 	}
27195367Sahrens 	csa.cds = clone;
27205367Sahrens 	csa.ohds = origin_head;
27215367Sahrens 	csa.force = force;
27226689Smaybee 	error = dsl_sync_task_do(clone->ds_dir->dd_pool,
27235326Sek110237 	    dsl_dataset_clone_swap_check,
27246689Smaybee 	    dsl_dataset_clone_swap_sync, &csa, NULL, 9);
27256689Smaybee 	return (error);
27265326Sek110237 }
27275326Sek110237 
27283912Slling /*
27293912Slling  * Given a pool name and a dataset object number in that pool,
27303912Slling  * return the name of that dataset.
27313912Slling  */
27323912Slling int
27333912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
27343912Slling {
27353912Slling 	spa_t *spa;
27363912Slling 	dsl_pool_t *dp;
27376689Smaybee 	dsl_dataset_t *ds;
27383912Slling 	int error;
27393912Slling 
27403912Slling 	if ((error = spa_open(pname, &spa, FTAG)) != 0)
27413912Slling 		return (error);
27423912Slling 	dp = spa_get_dsl(spa);
27433912Slling 	rw_enter(&dp->dp_config_rwlock, RW_READER);
27446689Smaybee 	if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) {
27456689Smaybee 		dsl_dataset_name(ds, buf);
27466689Smaybee 		dsl_dataset_rele(ds, FTAG);
27473912Slling 	}
27483912Slling 	rw_exit(&dp->dp_config_rwlock);
27493912Slling 	spa_close(spa, FTAG);
27503912Slling 
27516689Smaybee 	return (error);
27523912Slling }
27535378Sck153898 
27545378Sck153898 int
27555378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
27566689Smaybee     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
27575378Sck153898 {
27585378Sck153898 	int error = 0;
27595378Sck153898 
27605378Sck153898 	ASSERT3S(asize, >, 0);
27615378Sck153898 
27625831Sck153898 	/*
27635831Sck153898 	 * *ref_rsrv is the portion of asize that will come from any
27645831Sck153898 	 * unconsumed refreservation space.
27655831Sck153898 	 */
27665831Sck153898 	*ref_rsrv = 0;
27675831Sck153898 
27685378Sck153898 	mutex_enter(&ds->ds_lock);
27695378Sck153898 	/*
27705378Sck153898 	 * Make a space adjustment for reserved bytes.
27715378Sck153898 	 */
27725378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
27735378Sck153898 		ASSERT3U(*used, >=,
27745378Sck153898 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
27755378Sck153898 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
27765831Sck153898 		*ref_rsrv =
27775831Sck153898 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
27785378Sck153898 	}
27795378Sck153898 
27805378Sck153898 	if (!check_quota || ds->ds_quota == 0) {
27815378Sck153898 		mutex_exit(&ds->ds_lock);
27825378Sck153898 		return (0);
27835378Sck153898 	}
27845378Sck153898 	/*
27855378Sck153898 	 * If they are requesting more space, and our current estimate
27865378Sck153898 	 * is over quota, they get to try again unless the actual
27875378Sck153898 	 * on-disk is over quota and there are no pending changes (which
27885378Sck153898 	 * may free up space for us).
27895378Sck153898 	 */
27905378Sck153898 	if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) {
27915378Sck153898 		if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota)
27925378Sck153898 			error = ERESTART;
27935378Sck153898 		else
27945378Sck153898 			error = EDQUOT;
27955378Sck153898 	}
27965378Sck153898 	mutex_exit(&ds->ds_lock);
27975378Sck153898 
27985378Sck153898 	return (error);
27995378Sck153898 }
28005378Sck153898 
28015378Sck153898 /* ARGSUSED */
28025378Sck153898 static int
28035378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
28045378Sck153898 {
28055378Sck153898 	dsl_dataset_t *ds = arg1;
28065378Sck153898 	uint64_t *quotap = arg2;
28075378Sck153898 	uint64_t new_quota = *quotap;
28085378Sck153898 
28095378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA)
28105378Sck153898 		return (ENOTSUP);
28115378Sck153898 
28125378Sck153898 	if (new_quota == 0)
28135378Sck153898 		return (0);
28145378Sck153898 
28155378Sck153898 	if (new_quota < ds->ds_phys->ds_used_bytes ||
28165378Sck153898 	    new_quota < ds->ds_reserved)
28175378Sck153898 		return (ENOSPC);
28185378Sck153898 
28195378Sck153898 	return (0);
28205378Sck153898 }
28215378Sck153898 
28225378Sck153898 /* ARGSUSED */
28235378Sck153898 void
28245378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
28255378Sck153898 {
28265378Sck153898 	dsl_dataset_t *ds = arg1;
28275378Sck153898 	uint64_t *quotap = arg2;
28285378Sck153898 	uint64_t new_quota = *quotap;
28295378Sck153898 
28305378Sck153898 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
28315378Sck153898 
28325378Sck153898 	ds->ds_quota = new_quota;
28335378Sck153898 
28345378Sck153898 	dsl_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx);
28355378Sck153898 
28365378Sck153898 	spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa,
28375378Sck153898 	    tx, cr, "%lld dataset = %llu ",
28386689Smaybee 	    (longlong_t)new_quota, ds->ds_object);
28395378Sck153898 }
28405378Sck153898 
28415378Sck153898 int
28425378Sck153898 dsl_dataset_set_quota(const char *dsname, uint64_t quota)
28435378Sck153898 {
28445378Sck153898 	dsl_dataset_t *ds;
28455378Sck153898 	int err;
28465378Sck153898 
28476689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
28485378Sck153898 	if (err)
28495378Sck153898 		return (err);
28505378Sck153898 
28515481Sck153898 	if (quota != ds->ds_quota) {
28525481Sck153898 		/*
28535481Sck153898 		 * If someone removes a file, then tries to set the quota, we
28545481Sck153898 		 * want to make sure the file freeing takes effect.
28555481Sck153898 		 */
28565481Sck153898 		txg_wait_open(ds->ds_dir->dd_pool, 0);
28575481Sck153898 
28585481Sck153898 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
28595481Sck153898 		    dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync,
28605481Sck153898 		    ds, &quota, 0);
28615481Sck153898 	}
28626689Smaybee 	dsl_dataset_rele(ds, FTAG);
28635378Sck153898 	return (err);
28645378Sck153898 }
28655378Sck153898 
28665378Sck153898 static int
28675378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
28685378Sck153898 {
28695378Sck153898 	dsl_dataset_t *ds = arg1;
28705378Sck153898 	uint64_t *reservationp = arg2;
28715378Sck153898 	uint64_t new_reservation = *reservationp;
28725378Sck153898 	int64_t delta;
28735378Sck153898 	uint64_t unique;
28745378Sck153898 
28755378Sck153898 	if (new_reservation > INT64_MAX)
28765378Sck153898 		return (EOVERFLOW);
28775378Sck153898 
28785378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
28795378Sck153898 	    SPA_VERSION_REFRESERVATION)
28805378Sck153898 		return (ENOTSUP);
28815378Sck153898 
28825378Sck153898 	if (dsl_dataset_is_snapshot(ds))
28835378Sck153898 		return (EINVAL);
28845378Sck153898 
28855378Sck153898 	/*
28865378Sck153898 	 * If we are doing the preliminary check in open context, the
28875378Sck153898 	 * space estimates may be inaccurate.
28885378Sck153898 	 */
28895378Sck153898 	if (!dmu_tx_is_syncing(tx))
28905378Sck153898 		return (0);
28915378Sck153898 
28925378Sck153898 	mutex_enter(&ds->ds_lock);
28935378Sck153898 	unique = dsl_dataset_unique(ds);
28945378Sck153898 	delta = MAX(unique, new_reservation) - MAX(unique, ds->ds_reserved);
28955378Sck153898 	mutex_exit(&ds->ds_lock);
28965378Sck153898 
28975378Sck153898 	if (delta > 0 &&
28985378Sck153898 	    delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
28995378Sck153898 		return (ENOSPC);
29005378Sck153898 	if (delta > 0 && ds->ds_quota > 0 &&
29015378Sck153898 	    new_reservation > ds->ds_quota)
29025378Sck153898 		return (ENOSPC);
29035378Sck153898 
29045378Sck153898 	return (0);
29055378Sck153898 }
29065378Sck153898 
29075378Sck153898 /* ARGSUSED */
29085378Sck153898 static void
29095378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr,
29105378Sck153898     dmu_tx_t *tx)
29115378Sck153898 {
29125378Sck153898 	dsl_dataset_t *ds = arg1;
29135378Sck153898 	uint64_t *reservationp = arg2;
29145378Sck153898 	uint64_t new_reservation = *reservationp;
29155378Sck153898 	uint64_t unique;
29165378Sck153898 	int64_t delta;
29175378Sck153898 
29185378Sck153898 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
29195378Sck153898 
29205378Sck153898 	mutex_enter(&ds->ds_lock);
29215378Sck153898 	unique = dsl_dataset_unique(ds);
29225378Sck153898 	delta = MAX(0, (int64_t)(new_reservation - unique)) -
29235378Sck153898 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
29245378Sck153898 	ds->ds_reserved = new_reservation;
29255378Sck153898 	mutex_exit(&ds->ds_lock);
29265378Sck153898 
29275378Sck153898 	dsl_prop_set_uint64_sync(ds->ds_dir, "refreservation",
29285378Sck153898 	    new_reservation, cr, tx);
29295378Sck153898 
29305378Sck153898 	dsl_dir_diduse_space(ds->ds_dir, delta, 0, 0, tx);
29315378Sck153898 
29325378Sck153898 	spa_history_internal_log(LOG_DS_REFRESERV,
29335378Sck153898 	    ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu",
29345378Sck153898 	    (longlong_t)new_reservation,
29355378Sck153898 	    ds->ds_dir->dd_phys->dd_head_dataset_obj);
29365378Sck153898 }
29375378Sck153898 
29385378Sck153898 int
29395378Sck153898 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation)
29405378Sck153898 {
29415378Sck153898 	dsl_dataset_t *ds;
29425378Sck153898 	int err;
29435378Sck153898 
29446689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
29455378Sck153898 	if (err)
29465378Sck153898 		return (err);
29475378Sck153898 
29485378Sck153898 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
29495378Sck153898 	    dsl_dataset_set_reservation_check,
29505378Sck153898 	    dsl_dataset_set_reservation_sync, ds, &reservation, 0);
29516689Smaybee 	dsl_dataset_rele(ds, FTAG);
29525378Sck153898 	return (err);
29535378Sck153898 }
2954