xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision 8779:f164e0e90508)
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 /*
228517SEric.Taylor@Sun.COM  * Copyright 2009 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 */
1007390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
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);
1067595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_dir->dd_lock);
107789Sahrens 	mutex_enter(&ds->ds_lock);
1085378Sck153898 	delta = parent_delta(ds, used);
109789Sahrens 	ds->ds_phys->ds_used_bytes += used;
110789Sahrens 	ds->ds_phys->ds_compressed_bytes += compressed;
111789Sahrens 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
112789Sahrens 	ds->ds_phys->ds_unique_bytes += used;
113789Sahrens 	mutex_exit(&ds->ds_lock);
1147390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
1157390SMatthew.Ahrens@Sun.COM 	    compressed, uncompressed, tx);
1167390SMatthew.Ahrens@Sun.COM 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
1177390SMatthew.Ahrens@Sun.COM 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
1187595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_dir->dd_lock);
119789Sahrens }
120789Sahrens 
1216992Smaybee int
1223547Smaybee dsl_dataset_block_kill(dsl_dataset_t *ds, blkptr_t *bp, zio_t *pio,
1233547Smaybee     dmu_tx_t *tx)
124789Sahrens {
1252082Seschrock 	int used = bp_get_dasize(tx->tx_pool->dp_spa, bp);
126789Sahrens 	int compressed = BP_GET_PSIZE(bp);
127789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
128789Sahrens 
1297754SJeff.Bonwick@Sun.COM 	ASSERT(pio != NULL);
130789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1313547Smaybee 	/* No block pointer => nothing to free */
132789Sahrens 	if (BP_IS_HOLE(bp))
1336992Smaybee 		return (0);
134789Sahrens 
135789Sahrens 	ASSERT(used > 0);
136789Sahrens 	if (ds == NULL) {
1373547Smaybee 		int err;
138789Sahrens 		/*
139789Sahrens 		 * Account for the meta-objset space in its placeholder
140789Sahrens 		 * dataset.
141789Sahrens 		 */
1427046Sahrens 		err = dsl_free(pio, tx->tx_pool,
1437754SJeff.Bonwick@Sun.COM 		    tx->tx_txg, bp, NULL, NULL, ARC_NOWAIT);
1443547Smaybee 		ASSERT(err == 0);
145789Sahrens 
1467390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
147789Sahrens 		    -used, -compressed, -uncompressed, tx);
148789Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
1496992Smaybee 		return (used);
150789Sahrens 	}
151789Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
152789Sahrens 
1537390SMatthew.Ahrens@Sun.COM 	ASSERT(!dsl_dataset_is_snapshot(ds));
154789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
155789Sahrens 
156789Sahrens 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
1573547Smaybee 		int err;
1585378Sck153898 		int64_t delta;
1593547Smaybee 
160789Sahrens 		dprintf_bp(bp, "freeing: %s", "");
1617046Sahrens 		err = dsl_free(pio, tx->tx_pool,
1627754SJeff.Bonwick@Sun.COM 		    tx->tx_txg, bp, NULL, NULL, ARC_NOWAIT);
1633547Smaybee 		ASSERT(err == 0);
164789Sahrens 
1657595SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_dir->dd_lock);
166789Sahrens 		mutex_enter(&ds->ds_lock);
1675378Sck153898 		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
1685378Sck153898 		    !DS_UNIQUE_IS_ACCURATE(ds));
1695378Sck153898 		delta = parent_delta(ds, -used);
170789Sahrens 		ds->ds_phys->ds_unique_bytes -= used;
171789Sahrens 		mutex_exit(&ds->ds_lock);
1727390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
1735378Sck153898 		    delta, -compressed, -uncompressed, tx);
1747390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
1757390SMatthew.Ahrens@Sun.COM 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
1767595SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_dir->dd_lock);
177789Sahrens 	} else {
178789Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
1791544Seschrock 		VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx));
1805712Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
1815712Sahrens 		    ds->ds_phys->ds_prev_snap_obj);
1825712Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
183789Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
1845712Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
1855712Sahrens 		    ds->ds_object && bp->blk_birth >
1865712Sahrens 		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
1875712Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1885712Sahrens 			mutex_enter(&ds->ds_prev->ds_lock);
1895712Sahrens 			ds->ds_prev->ds_phys->ds_unique_bytes += used;
1905712Sahrens 			mutex_exit(&ds->ds_prev->ds_lock);
191789Sahrens 		}
1927390SMatthew.Ahrens@Sun.COM 		if (bp->blk_birth > ds->ds_origin_txg) {
1937390SMatthew.Ahrens@Sun.COM 			dsl_dir_transfer_space(ds->ds_dir, used,
1947390SMatthew.Ahrens@Sun.COM 			    DD_USED_HEAD, DD_USED_SNAP, tx);
1957390SMatthew.Ahrens@Sun.COM 		}
196789Sahrens 	}
197789Sahrens 	mutex_enter(&ds->ds_lock);
198789Sahrens 	ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used);
199789Sahrens 	ds->ds_phys->ds_used_bytes -= used;
200789Sahrens 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
201789Sahrens 	ds->ds_phys->ds_compressed_bytes -= compressed;
202789Sahrens 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
203789Sahrens 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
204789Sahrens 	mutex_exit(&ds->ds_lock);
2056992Smaybee 
2066992Smaybee 	return (used);
207789Sahrens }
208789Sahrens 
2091544Seschrock uint64_t
2101544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
211789Sahrens {
2122885Sahrens 	uint64_t trysnap = 0;
2132885Sahrens 
214789Sahrens 	if (ds == NULL)
2151544Seschrock 		return (0);
216789Sahrens 	/*
217789Sahrens 	 * The snapshot creation could fail, but that would cause an
218789Sahrens 	 * incorrect FALSE return, which would only result in an
219789Sahrens 	 * overestimation of the amount of space that an operation would
220789Sahrens 	 * consume, which is OK.
221789Sahrens 	 *
222789Sahrens 	 * There's also a small window where we could miss a pending
223789Sahrens 	 * snapshot, because we could set the sync task in the quiescing
224789Sahrens 	 * phase.  So this should only be used as a guess.
225789Sahrens 	 */
2262885Sahrens 	if (ds->ds_trysnap_txg >
2272885Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
2282885Sahrens 		trysnap = ds->ds_trysnap_txg;
2292885Sahrens 	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
2301544Seschrock }
2311544Seschrock 
2321544Seschrock int
2331544Seschrock dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth)
2341544Seschrock {
2351544Seschrock 	return (blk_birth > dsl_dataset_prev_snap_txg(ds));
236789Sahrens }
237789Sahrens 
238789Sahrens /* ARGSUSED */
239789Sahrens static void
240789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv)
241789Sahrens {
242789Sahrens 	dsl_dataset_t *ds = dsv;
243789Sahrens 
2446689Smaybee 	ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds));
245789Sahrens 
246789Sahrens 	dprintf_ds(ds, "evicting %s\n", "");
247789Sahrens 
2484787Sahrens 	unique_remove(ds->ds_fsid_guid);
249789Sahrens 
250789Sahrens 	if (ds->ds_user_ptr != NULL)
251789Sahrens 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
252789Sahrens 
253789Sahrens 	if (ds->ds_prev) {
2546689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
255789Sahrens 		ds->ds_prev = NULL;
256789Sahrens 	}
257789Sahrens 
258789Sahrens 	bplist_close(&ds->ds_deadlist);
2596689Smaybee 	if (ds->ds_dir)
2606689Smaybee 		dsl_dir_close(ds->ds_dir, ds);
261789Sahrens 
2624787Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
263789Sahrens 
2642856Snd150628 	mutex_destroy(&ds->ds_lock);
2654787Sahrens 	mutex_destroy(&ds->ds_opening_lock);
2662856Snd150628 	mutex_destroy(&ds->ds_deadlist.bpl_lock);
2676689Smaybee 	rw_destroy(&ds->ds_rwlock);
2686689Smaybee 	cv_destroy(&ds->ds_exclusive_cv);
2692856Snd150628 
270789Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
271789Sahrens }
272789Sahrens 
2731544Seschrock static int
274789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
275789Sahrens {
276789Sahrens 	dsl_dataset_phys_t *headphys;
277789Sahrens 	int err;
278789Sahrens 	dmu_buf_t *headdbuf;
279789Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
280789Sahrens 	objset_t *mos = dp->dp_meta_objset;
281789Sahrens 
282789Sahrens 	if (ds->ds_snapname[0])
2831544Seschrock 		return (0);
284789Sahrens 	if (ds->ds_phys->ds_next_snap_obj == 0)
2851544Seschrock 		return (0);
286789Sahrens 
2871544Seschrock 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
2881544Seschrock 	    FTAG, &headdbuf);
2891544Seschrock 	if (err)
2901544Seschrock 		return (err);
291789Sahrens 	headphys = headdbuf->db_data;
292789Sahrens 	err = zap_value_search(dp->dp_meta_objset,
2934577Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
2941544Seschrock 	dmu_buf_rele(headdbuf, FTAG);
2951544Seschrock 	return (err);
296789Sahrens }
297789Sahrens 
2986492Stimh static int
2996689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
3006492Stimh {
3016689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3026689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
3036492Stimh 	matchtype_t mt;
3046492Stimh 	int err;
3056492Stimh 
3066689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
3076492Stimh 		mt = MT_FIRST;
3086492Stimh 	else
3096492Stimh 		mt = MT_EXACT;
3106492Stimh 
3116689Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
3126492Stimh 	    value, mt, NULL, 0, NULL);
3136492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
3146689Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
3156492Stimh 	return (err);
3166492Stimh }
3176492Stimh 
3186492Stimh static int
3196689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx)
3206492Stimh {
3216689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3226689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
3236492Stimh 	matchtype_t mt;
3246492Stimh 	int err;
3256492Stimh 
3266689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
3276492Stimh 		mt = MT_FIRST;
3286492Stimh 	else
3296492Stimh 		mt = MT_EXACT;
3306492Stimh 
3316689Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
3326492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
3336689Smaybee 		err = zap_remove(mos, snapobj, name, tx);
3346492Stimh 	return (err);
3356492Stimh }
3366492Stimh 
3376689Smaybee static int
3386689Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag,
3396689Smaybee     dsl_dataset_t **dsp)
340789Sahrens {
341789Sahrens 	objset_t *mos = dp->dp_meta_objset;
342789Sahrens 	dmu_buf_t *dbuf;
343789Sahrens 	dsl_dataset_t *ds;
3441544Seschrock 	int err;
345789Sahrens 
346789Sahrens 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
347789Sahrens 	    dsl_pool_sync_context(dp));
348789Sahrens 
3491544Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
3501544Seschrock 	if (err)
3511544Seschrock 		return (err);
352789Sahrens 	ds = dmu_buf_get_user(dbuf);
353789Sahrens 	if (ds == NULL) {
354789Sahrens 		dsl_dataset_t *winner;
355789Sahrens 
356789Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
357789Sahrens 		ds->ds_dbuf = dbuf;
358789Sahrens 		ds->ds_object = dsobj;
359789Sahrens 		ds->ds_phys = dbuf->db_data;
360789Sahrens 
3612856Snd150628 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
3624787Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
3632856Snd150628 		mutex_init(&ds->ds_deadlist.bpl_lock, NULL, MUTEX_DEFAULT,
3642856Snd150628 		    NULL);
3656689Smaybee 		rw_init(&ds->ds_rwlock, 0, 0, 0);
3666689Smaybee 		cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL);
3672856Snd150628 
3681544Seschrock 		err = bplist_open(&ds->ds_deadlist,
369789Sahrens 		    mos, ds->ds_phys->ds_deadlist_obj);
3701544Seschrock 		if (err == 0) {
3711544Seschrock 			err = dsl_dir_open_obj(dp,
3721544Seschrock 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
3731544Seschrock 		}
3741544Seschrock 		if (err) {
3751544Seschrock 			/*
3761544Seschrock 			 * we don't really need to close the blist if we
3771544Seschrock 			 * just opened it.
3781544Seschrock 			 */
3792856Snd150628 			mutex_destroy(&ds->ds_lock);
3804787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
3812856Snd150628 			mutex_destroy(&ds->ds_deadlist.bpl_lock);
3826689Smaybee 			rw_destroy(&ds->ds_rwlock);
3836689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
3841544Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
3851544Seschrock 			dmu_buf_rele(dbuf, tag);
3861544Seschrock 			return (err);
3871544Seschrock 		}
388789Sahrens 
3897390SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(ds)) {
390789Sahrens 			ds->ds_snapname[0] = '\0';
391789Sahrens 			if (ds->ds_phys->ds_prev_snap_obj) {
3926689Smaybee 				err = dsl_dataset_get_ref(dp,
3936689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
3946689Smaybee 				    ds, &ds->ds_prev);
395789Sahrens 			}
3967390SMatthew.Ahrens@Sun.COM 
3977390SMatthew.Ahrens@Sun.COM 			if (err == 0 && dsl_dir_is_clone(ds->ds_dir)) {
3987390SMatthew.Ahrens@Sun.COM 				dsl_dataset_t *origin;
3997390SMatthew.Ahrens@Sun.COM 
4007390SMatthew.Ahrens@Sun.COM 				err = dsl_dataset_hold_obj(dp,
4017390SMatthew.Ahrens@Sun.COM 				    ds->ds_dir->dd_phys->dd_origin_obj,
4027390SMatthew.Ahrens@Sun.COM 				    FTAG, &origin);
4037390SMatthew.Ahrens@Sun.COM 				if (err == 0) {
4047390SMatthew.Ahrens@Sun.COM 					ds->ds_origin_txg =
4057390SMatthew.Ahrens@Sun.COM 					    origin->ds_phys->ds_creation_txg;
4067390SMatthew.Ahrens@Sun.COM 					dsl_dataset_rele(origin, FTAG);
4077390SMatthew.Ahrens@Sun.COM 				}
4087390SMatthew.Ahrens@Sun.COM 			}
4096689Smaybee 		} else if (zfs_flags & ZFS_DEBUG_SNAPNAMES) {
4106689Smaybee 			err = dsl_dataset_get_snapname(ds);
411789Sahrens 		}
412789Sahrens 
4137390SMatthew.Ahrens@Sun.COM 		if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
4145569Sck153898 			/*
4155569Sck153898 			 * In sync context, we're called with either no lock
4165569Sck153898 			 * or with the write lock.  If we're not syncing,
4175569Sck153898 			 * we're always called with the read lock held.
4185569Sck153898 			 */
4195475Sck153898 			boolean_t need_lock =
4205569Sck153898 			    !RW_WRITE_HELD(&dp->dp_config_rwlock) &&
4215569Sck153898 			    dsl_pool_sync_context(dp);
4225475Sck153898 
4235475Sck153898 			if (need_lock)
4245475Sck153898 				rw_enter(&dp->dp_config_rwlock, RW_READER);
4255475Sck153898 
4267265Sahrens 			err = dsl_prop_get_ds(ds,
4275475Sck153898 			    "refreservation", sizeof (uint64_t), 1,
4285475Sck153898 			    &ds->ds_reserved, NULL);
4295475Sck153898 			if (err == 0) {
4307265Sahrens 				err = dsl_prop_get_ds(ds,
4315475Sck153898 				    "refquota", sizeof (uint64_t), 1,
4325475Sck153898 				    &ds->ds_quota, NULL);
4335475Sck153898 			}
4345475Sck153898 
4355475Sck153898 			if (need_lock)
4365475Sck153898 				rw_exit(&dp->dp_config_rwlock);
4375475Sck153898 		} else {
4385475Sck153898 			ds->ds_reserved = ds->ds_quota = 0;
4395475Sck153898 		}
4405475Sck153898 
4411544Seschrock 		if (err == 0) {
4421544Seschrock 			winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys,
4431544Seschrock 			    dsl_dataset_evict);
4441544Seschrock 		}
4451544Seschrock 		if (err || winner) {
446789Sahrens 			bplist_close(&ds->ds_deadlist);
4476689Smaybee 			if (ds->ds_prev)
4486689Smaybee 				dsl_dataset_drop_ref(ds->ds_prev, ds);
449789Sahrens 			dsl_dir_close(ds->ds_dir, ds);
4502856Snd150628 			mutex_destroy(&ds->ds_lock);
4514787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
4522856Snd150628 			mutex_destroy(&ds->ds_deadlist.bpl_lock);
4536689Smaybee 			rw_destroy(&ds->ds_rwlock);
4546689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
455789Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
4561544Seschrock 			if (err) {
4571544Seschrock 				dmu_buf_rele(dbuf, tag);
4581544Seschrock 				return (err);
4591544Seschrock 			}
460789Sahrens 			ds = winner;
461789Sahrens 		} else {
4624787Sahrens 			ds->ds_fsid_guid =
463789Sahrens 			    unique_insert(ds->ds_phys->ds_fsid_guid);
464789Sahrens 		}
465789Sahrens 	}
466789Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
467789Sahrens 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
4687046Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
4697061Sahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
4707077Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
471789Sahrens 	mutex_enter(&ds->ds_lock);
4726689Smaybee 	if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) {
473789Sahrens 		mutex_exit(&ds->ds_lock);
4746689Smaybee 		dmu_buf_rele(ds->ds_dbuf, tag);
4756689Smaybee 		return (ENOENT);
4766689Smaybee 	}
4776689Smaybee 	mutex_exit(&ds->ds_lock);
4786689Smaybee 	*dsp = ds;
4796689Smaybee 	return (0);
4806689Smaybee }
4816689Smaybee 
4826689Smaybee static int
4836689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag)
4846689Smaybee {
4856689Smaybee 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
4866689Smaybee 
4876689Smaybee 	/*
4886689Smaybee 	 * In syncing context we don't want the rwlock lock: there
4896689Smaybee 	 * may be an existing writer waiting for sync phase to
4906689Smaybee 	 * finish.  We don't need to worry about such writers, since
4916689Smaybee 	 * sync phase is single-threaded, so the writer can't be
4926689Smaybee 	 * doing anything while we are active.
4936689Smaybee 	 */
4946689Smaybee 	if (dsl_pool_sync_context(dp)) {
4956689Smaybee 		ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
4966689Smaybee 		return (0);
497789Sahrens 	}
4986689Smaybee 
4996689Smaybee 	/*
5006689Smaybee 	 * Normal users will hold the ds_rwlock as a READER until they
5016689Smaybee 	 * are finished (i.e., call dsl_dataset_rele()).  "Owners" will
5026689Smaybee 	 * drop their READER lock after they set the ds_owner field.
5036689Smaybee 	 *
5046689Smaybee 	 * If the dataset is being destroyed, the destroy thread will
5056689Smaybee 	 * obtain a WRITER lock for exclusive access after it's done its
5066689Smaybee 	 * open-context work and then change the ds_owner to
5076689Smaybee 	 * dsl_reaper once destruction is assured.  So threads
5086689Smaybee 	 * may block here temporarily, until the "destructability" of
5096689Smaybee 	 * the dataset is determined.
5106689Smaybee 	 */
5116689Smaybee 	ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock));
5126689Smaybee 	mutex_enter(&ds->ds_lock);
5136689Smaybee 	while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) {
5146689Smaybee 		rw_exit(&dp->dp_config_rwlock);
5156689Smaybee 		cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock);
5166689Smaybee 		if (DSL_DATASET_IS_DESTROYED(ds)) {
5176689Smaybee 			mutex_exit(&ds->ds_lock);
5186689Smaybee 			dsl_dataset_drop_ref(ds, tag);
5196689Smaybee 			rw_enter(&dp->dp_config_rwlock, RW_READER);
5206689Smaybee 			return (ENOENT);
5216689Smaybee 		}
5226689Smaybee 		rw_enter(&dp->dp_config_rwlock, RW_READER);
5236689Smaybee 	}
524789Sahrens 	mutex_exit(&ds->ds_lock);
5251544Seschrock 	return (0);
526789Sahrens }
527789Sahrens 
528789Sahrens int
5296689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
5306689Smaybee     dsl_dataset_t **dsp)
5316689Smaybee {
5326689Smaybee 	int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp);
5336689Smaybee 
5346689Smaybee 	if (err)
5356689Smaybee 		return (err);
5366689Smaybee 	return (dsl_dataset_hold_ref(*dsp, tag));
5376689Smaybee }
5386689Smaybee 
5396689Smaybee int
5406689Smaybee dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, int flags, void *owner,
5416689Smaybee     dsl_dataset_t **dsp)
5426689Smaybee {
5436689Smaybee 	int err = dsl_dataset_hold_obj(dp, dsobj, owner, dsp);
5446689Smaybee 
5456689Smaybee 	ASSERT(DS_MODE_TYPE(flags) != DS_MODE_USER);
5466689Smaybee 
5476689Smaybee 	if (err)
5486689Smaybee 		return (err);
5496689Smaybee 	if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) {
5506689Smaybee 		dsl_dataset_rele(*dsp, owner);
551*8779SMark.Musante@Sun.COM 		*dsp = NULL;
5526689Smaybee 		return (EBUSY);
5536689Smaybee 	}
5546689Smaybee 	return (0);
5556689Smaybee }
5566689Smaybee 
5576689Smaybee int
5586689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp)
559789Sahrens {
560789Sahrens 	dsl_dir_t *dd;
561789Sahrens 	dsl_pool_t *dp;
5626689Smaybee 	const char *snapname;
563789Sahrens 	uint64_t obj;
564789Sahrens 	int err = 0;
565789Sahrens 
5666689Smaybee 	err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname);
5671544Seschrock 	if (err)
5681544Seschrock 		return (err);
569789Sahrens 
570789Sahrens 	dp = dd->dd_pool;
571789Sahrens 	obj = dd->dd_phys->dd_head_dataset_obj;
572789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
5736689Smaybee 	if (obj)
5746689Smaybee 		err = dsl_dataset_get_ref(dp, obj, tag, dsp);
5756689Smaybee 	else
576789Sahrens 		err = ENOENT;
5776689Smaybee 	if (err)
578789Sahrens 		goto out;
5796689Smaybee 
5806689Smaybee 	err = dsl_dataset_hold_ref(*dsp, tag);
5816689Smaybee 
5826689Smaybee 	/* we may be looking for a snapshot */
5836689Smaybee 	if (err == 0 && snapname != NULL) {
5846689Smaybee 		dsl_dataset_t *ds = NULL;
5856689Smaybee 
5866689Smaybee 		if (*snapname++ != '@') {
5876689Smaybee 			dsl_dataset_rele(*dsp, tag);
588789Sahrens 			err = ENOENT;
589789Sahrens 			goto out;
590789Sahrens 		}
5916689Smaybee 
5926689Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
5936689Smaybee 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
5946689Smaybee 		if (err == 0)
5956689Smaybee 			err = dsl_dataset_get_ref(dp, obj, tag, &ds);
5966689Smaybee 		dsl_dataset_rele(*dsp, tag);
5976689Smaybee 
5986689Smaybee 		ASSERT3U((err == 0), ==, (ds != NULL));
5996689Smaybee 
6006689Smaybee 		if (ds) {
6016689Smaybee 			mutex_enter(&ds->ds_lock);
6026689Smaybee 			if (ds->ds_snapname[0] == 0)
6036689Smaybee 				(void) strlcpy(ds->ds_snapname, snapname,
6046689Smaybee 				    sizeof (ds->ds_snapname));
6056689Smaybee 			mutex_exit(&ds->ds_lock);
6066689Smaybee 			err = dsl_dataset_hold_ref(ds, tag);
6076689Smaybee 			*dsp = err ? NULL : ds;
608789Sahrens 		}
609789Sahrens 	}
610789Sahrens out:
611789Sahrens 	rw_exit(&dp->dp_config_rwlock);
612789Sahrens 	dsl_dir_close(dd, FTAG);
613789Sahrens 	return (err);
614789Sahrens }
615789Sahrens 
616789Sahrens int
6176689Smaybee dsl_dataset_own(const char *name, int flags, void *owner, dsl_dataset_t **dsp)
618789Sahrens {
6196689Smaybee 	int err = dsl_dataset_hold(name, owner, dsp);
6206689Smaybee 	if (err)
6216689Smaybee 		return (err);
6226689Smaybee 	if ((*dsp)->ds_phys->ds_num_children > 0 &&
6236689Smaybee 	    !DS_MODE_IS_READONLY(flags)) {
6246689Smaybee 		dsl_dataset_rele(*dsp, owner);
6256689Smaybee 		return (EROFS);
6266689Smaybee 	}
6276689Smaybee 	if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) {
6286689Smaybee 		dsl_dataset_rele(*dsp, owner);
6296689Smaybee 		return (EBUSY);
6306689Smaybee 	}
6316689Smaybee 	return (0);
632789Sahrens }
633789Sahrens 
634789Sahrens void
635789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
636789Sahrens {
637789Sahrens 	if (ds == NULL) {
638789Sahrens 		(void) strcpy(name, "mos");
639789Sahrens 	} else {
640789Sahrens 		dsl_dir_name(ds->ds_dir, name);
6411544Seschrock 		VERIFY(0 == dsl_dataset_get_snapname(ds));
642789Sahrens 		if (ds->ds_snapname[0]) {
643789Sahrens 			(void) strcat(name, "@");
6446689Smaybee 			/*
6456689Smaybee 			 * We use a "recursive" mutex so that we
6466689Smaybee 			 * can call dprintf_ds() with ds_lock held.
6476689Smaybee 			 */
648789Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
649789Sahrens 				mutex_enter(&ds->ds_lock);
650789Sahrens 				(void) strcat(name, ds->ds_snapname);
651789Sahrens 				mutex_exit(&ds->ds_lock);
652789Sahrens 			} else {
653789Sahrens 				(void) strcat(name, ds->ds_snapname);
654789Sahrens 			}
655789Sahrens 		}
656789Sahrens 	}
657789Sahrens }
658789Sahrens 
6593978Smmusante static int
6603978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds)
6613978Smmusante {
6623978Smmusante 	int result;
6633978Smmusante 
6643978Smmusante 	if (ds == NULL) {
6653978Smmusante 		result = 3;	/* "mos" */
6663978Smmusante 	} else {
6673978Smmusante 		result = dsl_dir_namelen(ds->ds_dir);
6683978Smmusante 		VERIFY(0 == dsl_dataset_get_snapname(ds));
6693978Smmusante 		if (ds->ds_snapname[0]) {
6703978Smmusante 			++result;	/* adding one for the @-sign */
6713978Smmusante 			if (!MUTEX_HELD(&ds->ds_lock)) {
6723978Smmusante 				mutex_enter(&ds->ds_lock);
6733978Smmusante 				result += strlen(ds->ds_snapname);
6743978Smmusante 				mutex_exit(&ds->ds_lock);
6753978Smmusante 			} else {
6763978Smmusante 				result += strlen(ds->ds_snapname);
6773978Smmusante 			}
6783978Smmusante 		}
6793978Smmusante 	}
6803978Smmusante 
6813978Smmusante 	return (result);
6823978Smmusante }
6833978Smmusante 
6847046Sahrens void
6856689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag)
686789Sahrens {
6871544Seschrock 	dmu_buf_rele(ds->ds_dbuf, tag);
688789Sahrens }
689789Sahrens 
690789Sahrens void
6916689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
6925367Sahrens {
6936689Smaybee 	if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) {
6946689Smaybee 		rw_exit(&ds->ds_rwlock);
6956689Smaybee 	}
6966689Smaybee 	dsl_dataset_drop_ref(ds, tag);
6976689Smaybee }
6986689Smaybee 
6996689Smaybee void
7006689Smaybee dsl_dataset_disown(dsl_dataset_t *ds, void *owner)
7016689Smaybee {
7026689Smaybee 	ASSERT((ds->ds_owner == owner && ds->ds_dbuf) ||
7036689Smaybee 	    (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL));
7046689Smaybee 
7055367Sahrens 	mutex_enter(&ds->ds_lock);
7066689Smaybee 	ds->ds_owner = NULL;
7076689Smaybee 	if (RW_WRITE_HELD(&ds->ds_rwlock)) {
7086689Smaybee 		rw_exit(&ds->ds_rwlock);
7096689Smaybee 		cv_broadcast(&ds->ds_exclusive_cv);
7106689Smaybee 	}
7115367Sahrens 	mutex_exit(&ds->ds_lock);
7126689Smaybee 	if (ds->ds_dbuf)
7136689Smaybee 		dsl_dataset_drop_ref(ds, owner);
7146689Smaybee 	else
7156689Smaybee 		dsl_dataset_evict(ds->ds_dbuf, ds);
7165367Sahrens }
7175367Sahrens 
7185367Sahrens boolean_t
7196689Smaybee dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *owner)
7205367Sahrens {
7216689Smaybee 	boolean_t gotit = FALSE;
7226689Smaybee 
7235367Sahrens 	mutex_enter(&ds->ds_lock);
7246689Smaybee 	if (ds->ds_owner == NULL &&
7256689Smaybee 	    (!DS_IS_INCONSISTENT(ds) || inconsistentok)) {
7266689Smaybee 		ds->ds_owner = owner;
7276689Smaybee 		if (!dsl_pool_sync_context(ds->ds_dir->dd_pool))
7286689Smaybee 			rw_exit(&ds->ds_rwlock);
7296689Smaybee 		gotit = TRUE;
7305367Sahrens 	}
7315367Sahrens 	mutex_exit(&ds->ds_lock);
7326689Smaybee 	return (gotit);
7336689Smaybee }
7346689Smaybee 
7356689Smaybee void
7366689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner)
7376689Smaybee {
7386689Smaybee 	ASSERT3P(owner, ==, ds->ds_owner);
7396689Smaybee 	if (!RW_WRITE_HELD(&ds->ds_rwlock))
7406689Smaybee 		rw_enter(&ds->ds_rwlock, RW_WRITER);
7415367Sahrens }
7425367Sahrens 
7432199Sahrens uint64_t
7447046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
7456492Stimh     uint64_t flags, dmu_tx_t *tx)
746789Sahrens {
7475367Sahrens 	dsl_pool_t *dp = dd->dd_pool;
748789Sahrens 	dmu_buf_t *dbuf;
749789Sahrens 	dsl_dataset_phys_t *dsphys;
7505367Sahrens 	uint64_t dsobj;
751789Sahrens 	objset_t *mos = dp->dp_meta_objset;
752789Sahrens 
7537046Sahrens 	if (origin == NULL)
7547046Sahrens 		origin = dp->dp_origin_snap;
7557046Sahrens 
7565367Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
7575367Sahrens 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
758789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7595367Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
760789Sahrens 
761928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
762928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
7631544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
764789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
765789Sahrens 	dsphys = dbuf->db_data;
7666689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
767789Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
7686492Stimh 	dsphys->ds_flags = flags;
769789Sahrens 	dsphys->ds_fsid_guid = unique_create();
770789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
771789Sahrens 	    sizeof (dsphys->ds_guid));
772789Sahrens 	dsphys->ds_snapnames_zapobj =
7736492Stimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
7746492Stimh 	    DMU_OT_NONE, 0, tx);
775789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
7767046Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
777789Sahrens 	dsphys->ds_deadlist_obj =
778789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
7795378Sck153898 
7805367Sahrens 	if (origin) {
7815367Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
782789Sahrens 		dsphys->ds_prev_snap_txg =
7835367Sahrens 		    origin->ds_phys->ds_creation_txg;
784789Sahrens 		dsphys->ds_used_bytes =
7855367Sahrens 		    origin->ds_phys->ds_used_bytes;
786789Sahrens 		dsphys->ds_compressed_bytes =
7875367Sahrens 		    origin->ds_phys->ds_compressed_bytes;
788789Sahrens 		dsphys->ds_uncompressed_bytes =
7895367Sahrens 		    origin->ds_phys->ds_uncompressed_bytes;
7905367Sahrens 		dsphys->ds_bp = origin->ds_phys->ds_bp;
7916502Stimh 		dsphys->ds_flags |= origin->ds_phys->ds_flags;
792789Sahrens 
7935367Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
7945367Sahrens 		origin->ds_phys->ds_num_children++;
795789Sahrens 
7967046Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
7977046Sahrens 			if (origin->ds_phys->ds_next_clones_obj == 0) {
7987046Sahrens 				origin->ds_phys->ds_next_clones_obj =
7997046Sahrens 				    zap_create(mos,
8007046Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
8017046Sahrens 			}
8027046Sahrens 			VERIFY(0 == zap_add_int(mos,
8037046Sahrens 			    origin->ds_phys->ds_next_clones_obj,
8047046Sahrens 			    dsobj, tx));
8057046Sahrens 		}
8067046Sahrens 
807789Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
8085367Sahrens 		dd->dd_phys->dd_origin_obj = origin->ds_object;
809789Sahrens 	}
8106492Stimh 
8116492Stimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
8126492Stimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
8136492Stimh 
8141544Seschrock 	dmu_buf_rele(dbuf, FTAG);
815789Sahrens 
816789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
817789Sahrens 	dd->dd_phys->dd_head_dataset_obj = dsobj;
8185367Sahrens 
8195367Sahrens 	return (dsobj);
8205367Sahrens }
8215367Sahrens 
8225367Sahrens uint64_t
8236492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
8246492Stimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
8255367Sahrens {
8265367Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
8275367Sahrens 	uint64_t dsobj, ddobj;
8285367Sahrens 	dsl_dir_t *dd;
8295367Sahrens 
8305367Sahrens 	ASSERT(lastname[0] != '@');
8315367Sahrens 
8327046Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
8335367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
8345367Sahrens 
8357046Sahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx);
8365367Sahrens 
8375367Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
8385367Sahrens 
839789Sahrens 	dsl_dir_close(dd, FTAG);
840789Sahrens 
8412199Sahrens 	return (dsobj);
8422199Sahrens }
8432199Sahrens 
8442199Sahrens struct destroyarg {
8452199Sahrens 	dsl_sync_task_group_t *dstg;
8462199Sahrens 	char *snapname;
8472199Sahrens 	char *failed;
8482199Sahrens };
8492199Sahrens 
8502199Sahrens static int
8512199Sahrens dsl_snapshot_destroy_one(char *name, void *arg)
8522199Sahrens {
8532199Sahrens 	struct destroyarg *da = arg;
8542199Sahrens 	dsl_dataset_t *ds;
8552199Sahrens 	char *cp;
8562199Sahrens 	int err;
8572199Sahrens 
8582199Sahrens 	(void) strcat(name, "@");
8592199Sahrens 	(void) strcat(name, da->snapname);
8606689Smaybee 	err = dsl_dataset_own(name, DS_MODE_READONLY | DS_MODE_INCONSISTENT,
8614007Smmusante 	    da->dstg, &ds);
8622199Sahrens 	cp = strchr(name, '@');
8632199Sahrens 	*cp = '\0';
8646689Smaybee 	if (err == 0) {
8656689Smaybee 		dsl_dataset_make_exclusive(ds, da->dstg);
8667237Sek110237 		if (ds->ds_user_ptr) {
8677237Sek110237 			ds->ds_user_evict_func(ds, ds->ds_user_ptr);
8687237Sek110237 			ds->ds_user_ptr = NULL;
8697237Sek110237 		}
8706689Smaybee 		dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check,
8716689Smaybee 		    dsl_dataset_destroy_sync, ds, da->dstg, 0);
8726689Smaybee 	} else if (err == ENOENT) {
8736689Smaybee 		err = 0;
8746689Smaybee 	} else {
8752199Sahrens 		(void) strcpy(da->failed, name);
8762199Sahrens 	}
8776689Smaybee 	return (err);
878789Sahrens }
879789Sahrens 
8802199Sahrens /*
8812199Sahrens  * Destroy 'snapname' in all descendants of 'fsname'.
8822199Sahrens  */
8832199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy
8842199Sahrens int
8852199Sahrens dsl_snapshots_destroy(char *fsname, char *snapname)
8862199Sahrens {
8872199Sahrens 	int err;
8882199Sahrens 	struct destroyarg da;
8892199Sahrens 	dsl_sync_task_t *dst;
8902199Sahrens 	spa_t *spa;
8912199Sahrens 
8924603Sahrens 	err = spa_open(fsname, &spa, FTAG);
8932199Sahrens 	if (err)
8942199Sahrens 		return (err);
8952199Sahrens 	da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8962199Sahrens 	da.snapname = snapname;
8972199Sahrens 	da.failed = fsname;
8982199Sahrens 
8992199Sahrens 	err = dmu_objset_find(fsname,
9002417Sahrens 	    dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN);
9012199Sahrens 
9022199Sahrens 	if (err == 0)
9032199Sahrens 		err = dsl_sync_task_group_wait(da.dstg);
9042199Sahrens 
9052199Sahrens 	for (dst = list_head(&da.dstg->dstg_tasks); dst;
9062199Sahrens 	    dst = list_next(&da.dstg->dstg_tasks, dst)) {
9072199Sahrens 		dsl_dataset_t *ds = dst->dst_arg1;
9086689Smaybee 		/*
9096689Smaybee 		 * Return the file system name that triggered the error
9106689Smaybee 		 */
9112199Sahrens 		if (dst->dst_err) {
9122199Sahrens 			dsl_dataset_name(ds, fsname);
9134603Sahrens 			*strchr(fsname, '@') = '\0';
9142199Sahrens 		}
9156689Smaybee 		dsl_dataset_disown(ds, da.dstg);
9162199Sahrens 	}
9172199Sahrens 
9182199Sahrens 	dsl_sync_task_group_destroy(da.dstg);
9192199Sahrens 	spa_close(spa, FTAG);
9202199Sahrens 	return (err);
9212199Sahrens }
9222199Sahrens 
9235367Sahrens /*
9246689Smaybee  * ds must be opened as OWNER.  On return (whether successful or not),
9256689Smaybee  * ds will be closed and caller can no longer dereference it.
9265367Sahrens  */
927789Sahrens int
9285367Sahrens dsl_dataset_destroy(dsl_dataset_t *ds, void *tag)
929789Sahrens {
930789Sahrens 	int err;
9312199Sahrens 	dsl_sync_task_group_t *dstg;
9322199Sahrens 	objset_t *os;
933789Sahrens 	dsl_dir_t *dd;
9342199Sahrens 	uint64_t obj;
9352199Sahrens 
9365367Sahrens 	if (dsl_dataset_is_snapshot(ds)) {
9372199Sahrens 		/* Destroying a snapshot is simpler */
9386689Smaybee 		dsl_dataset_make_exclusive(ds, tag);
9397237Sek110237 
9407237Sek110237 		if (ds->ds_user_ptr) {
9417237Sek110237 			ds->ds_user_evict_func(ds, ds->ds_user_ptr);
9427237Sek110237 			ds->ds_user_ptr = NULL;
9437237Sek110237 		}
9442199Sahrens 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
9452199Sahrens 		    dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
9465367Sahrens 		    ds, tag, 0);
9475367Sahrens 		goto out;
9482199Sahrens 	}
9492199Sahrens 
9502199Sahrens 	dd = ds->ds_dir;
951789Sahrens 
9522199Sahrens 	/*
9532199Sahrens 	 * Check for errors and mark this ds as inconsistent, in
9542199Sahrens 	 * case we crash while freeing the objects.
9552199Sahrens 	 */
9562199Sahrens 	err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
9572199Sahrens 	    dsl_dataset_destroy_begin_sync, ds, NULL, 0);
9585367Sahrens 	if (err)
9595367Sahrens 		goto out;
9605367Sahrens 
9615367Sahrens 	err = dmu_objset_open_ds(ds, DMU_OST_ANY, &os);
9625367Sahrens 	if (err)
9635367Sahrens 		goto out;
9642199Sahrens 
9652199Sahrens 	/*
9662199Sahrens 	 * remove the objects in open context, so that we won't
9672199Sahrens 	 * have too much to do in syncing context.
9682199Sahrens 	 */
9693025Sahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
9703025Sahrens 	    ds->ds_phys->ds_prev_snap_txg)) {
9716992Smaybee 		/*
9726992Smaybee 		 * Ignore errors, if there is not enough disk space
9736992Smaybee 		 * we will deal with it in dsl_dataset_destroy_sync().
9746992Smaybee 		 */
9756992Smaybee 		(void) dmu_free_object(os, obj);
9762199Sahrens 	}
9772199Sahrens 
9782199Sahrens 	dmu_objset_close(os);
9792199Sahrens 	if (err != ESRCH)
9805367Sahrens 		goto out;
9812199Sahrens 
9826975Smaybee 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
9836975Smaybee 	err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd);
9846975Smaybee 	rw_exit(&dd->dd_pool->dp_config_rwlock);
9856975Smaybee 
9866975Smaybee 	if (err)
9876975Smaybee 		goto out;
9886975Smaybee 
9895367Sahrens 	if (ds->ds_user_ptr) {
9906689Smaybee 		/*
9916689Smaybee 		 * We need to sync out all in-flight IO before we try
9926689Smaybee 		 * to evict (the dataset evict func is trying to clear
9936689Smaybee 		 * the cached entries for this dataset in the ARC).
9946689Smaybee 		 */
9956689Smaybee 		txg_wait_synced(dd->dd_pool, 0);
9965367Sahrens 	}
9975367Sahrens 
9982199Sahrens 	/*
9992199Sahrens 	 * Blow away the dsl_dir + head dataset.
10002199Sahrens 	 */
10016689Smaybee 	dsl_dataset_make_exclusive(ds, tag);
10026975Smaybee 	if (ds->ds_user_ptr) {
10036975Smaybee 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
10046975Smaybee 		ds->ds_user_ptr = NULL;
10056975Smaybee 	}
10062199Sahrens 	dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
10072199Sahrens 	dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
10085367Sahrens 	    dsl_dataset_destroy_sync, ds, tag, 0);
10092199Sahrens 	dsl_sync_task_create(dstg, dsl_dir_destroy_check,
10102199Sahrens 	    dsl_dir_destroy_sync, dd, FTAG, 0);
10112199Sahrens 	err = dsl_sync_task_group_wait(dstg);
10122199Sahrens 	dsl_sync_task_group_destroy(dstg);
10136689Smaybee 	/* if it is successful, dsl_dir_destroy_sync will close the dd */
10145367Sahrens 	if (err)
10152199Sahrens 		dsl_dir_close(dd, FTAG);
10165367Sahrens out:
10176689Smaybee 	dsl_dataset_disown(ds, tag);
1018789Sahrens 	return (err);
1019789Sahrens }
1020789Sahrens 
1021789Sahrens int
10225367Sahrens dsl_dataset_rollback(dsl_dataset_t *ds, dmu_objset_type_t ost)
1023789Sahrens {
10247385SMark.Maybee@Sun.COM 	int err;
10257385SMark.Maybee@Sun.COM 
10266689Smaybee 	ASSERT(ds->ds_owner);
10275367Sahrens 
10287385SMark.Maybee@Sun.COM 	dsl_dataset_make_exclusive(ds, ds->ds_owner);
10297385SMark.Maybee@Sun.COM 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
10302199Sahrens 	    dsl_dataset_rollback_check, dsl_dataset_rollback_sync,
10317385SMark.Maybee@Sun.COM 	    ds, &ost, 0);
10327385SMark.Maybee@Sun.COM 	/* drop exclusive access */
10337385SMark.Maybee@Sun.COM 	mutex_enter(&ds->ds_lock);
10347385SMark.Maybee@Sun.COM 	rw_exit(&ds->ds_rwlock);
10357385SMark.Maybee@Sun.COM 	cv_broadcast(&ds->ds_exclusive_cv);
10367385SMark.Maybee@Sun.COM 	mutex_exit(&ds->ds_lock);
10377385SMark.Maybee@Sun.COM 	return (err);
1038789Sahrens }
1039789Sahrens 
1040789Sahrens void *
1041789Sahrens dsl_dataset_set_user_ptr(dsl_dataset_t *ds,
1042789Sahrens     void *p, dsl_dataset_evict_func_t func)
1043789Sahrens {
1044789Sahrens 	void *old;
1045789Sahrens 
1046789Sahrens 	mutex_enter(&ds->ds_lock);
1047789Sahrens 	old = ds->ds_user_ptr;
1048789Sahrens 	if (old == NULL) {
1049789Sahrens 		ds->ds_user_ptr = p;
1050789Sahrens 		ds->ds_user_evict_func = func;
1051789Sahrens 	}
1052789Sahrens 	mutex_exit(&ds->ds_lock);
1053789Sahrens 	return (old);
1054789Sahrens }
1055789Sahrens 
1056789Sahrens void *
1057789Sahrens dsl_dataset_get_user_ptr(dsl_dataset_t *ds)
1058789Sahrens {
1059789Sahrens 	return (ds->ds_user_ptr);
1060789Sahrens }
1061789Sahrens 
1062789Sahrens 
10633547Smaybee blkptr_t *
10643547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1065789Sahrens {
10663547Smaybee 	return (&ds->ds_phys->ds_bp);
1067789Sahrens }
1068789Sahrens 
1069789Sahrens void
1070789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1071789Sahrens {
1072789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1073789Sahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
1074789Sahrens 	if (ds == NULL) {
1075789Sahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
1076789Sahrens 	} else {
1077789Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1078789Sahrens 		ds->ds_phys->ds_bp = *bp;
1079789Sahrens 	}
1080789Sahrens }
1081789Sahrens 
1082789Sahrens spa_t *
1083789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
1084789Sahrens {
1085789Sahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1086789Sahrens }
1087789Sahrens 
1088789Sahrens void
1089789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1090789Sahrens {
1091789Sahrens 	dsl_pool_t *dp;
1092789Sahrens 
1093789Sahrens 	if (ds == NULL) /* this is the meta-objset */
1094789Sahrens 		return;
1095789Sahrens 
1096789Sahrens 	ASSERT(ds->ds_user_ptr != NULL);
10972885Sahrens 
10982885Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
10992885Sahrens 		panic("dirtying snapshot!");
1100789Sahrens 
1101789Sahrens 	dp = ds->ds_dir->dd_pool;
1102789Sahrens 
1103789Sahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
1104789Sahrens 		/* up the hold count until we can be written out */
1105789Sahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1106789Sahrens 	}
1107789Sahrens }
1108789Sahrens 
11095378Sck153898 /*
11105378Sck153898  * The unique space in the head dataset can be calculated by subtracting
11115378Sck153898  * the space used in the most recent snapshot, that is still being used
11125378Sck153898  * in this file system, from the space currently in use.  To figure out
11135378Sck153898  * the space in the most recent snapshot still in use, we need to take
11145378Sck153898  * the total space used in the snapshot and subtract out the space that
11155378Sck153898  * has been freed up since the snapshot was taken.
11165378Sck153898  */
11175378Sck153898 static void
11185378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
11195378Sck153898 {
11205378Sck153898 	uint64_t mrs_used;
11215378Sck153898 	uint64_t dlused, dlcomp, dluncomp;
11225378Sck153898 
11235378Sck153898 	ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj);
11245378Sck153898 
11255378Sck153898 	if (ds->ds_phys->ds_prev_snap_obj != 0)
11265378Sck153898 		mrs_used = ds->ds_prev->ds_phys->ds_used_bytes;
11275378Sck153898 	else
11285378Sck153898 		mrs_used = 0;
11295378Sck153898 
11305378Sck153898 	VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp,
11315378Sck153898 	    &dluncomp));
11325378Sck153898 
11335378Sck153898 	ASSERT3U(dlused, <=, mrs_used);
11345378Sck153898 	ds->ds_phys->ds_unique_bytes =
11355378Sck153898 	    ds->ds_phys->ds_used_bytes - (mrs_used - dlused);
11365378Sck153898 
11375378Sck153898 	if (!DS_UNIQUE_IS_ACCURATE(ds) &&
11385378Sck153898 	    spa_version(ds->ds_dir->dd_pool->dp_spa) >=
11395378Sck153898 	    SPA_VERSION_UNIQUE_ACCURATE)
11405378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
11415378Sck153898 }
11425378Sck153898 
11435378Sck153898 static uint64_t
11445378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds)
11455378Sck153898 {
11465378Sck153898 	if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds))
11475378Sck153898 		dsl_dataset_recalc_head_uniq(ds);
11485378Sck153898 
11495378Sck153898 	return (ds->ds_phys->ds_unique_bytes);
11505378Sck153898 }
11515378Sck153898 
1152789Sahrens struct killarg {
11537390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
1154789Sahrens 	zio_t *zio;
1155789Sahrens 	dmu_tx_t *tx;
1156789Sahrens };
1157789Sahrens 
11587390SMatthew.Ahrens@Sun.COM /* ARGSUSED */
1159789Sahrens static int
11607837SMatthew.Ahrens@Sun.COM kill_blkptr(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
11617837SMatthew.Ahrens@Sun.COM     const dnode_phys_t *dnp, void *arg)
1162789Sahrens {
1163789Sahrens 	struct killarg *ka = arg;
11647837SMatthew.Ahrens@Sun.COM 
11657837SMatthew.Ahrens@Sun.COM 	if (bp == NULL)
11667837SMatthew.Ahrens@Sun.COM 		return (0);
1167789Sahrens 
11688746SMatthew.Ahrens@Sun.COM 	if ((zb->zb_level == -1ULL && zb->zb_blkid != 0) ||
11698746SMatthew.Ahrens@Sun.COM 	    (zb->zb_object != 0 && dnp == NULL)) {
11708746SMatthew.Ahrens@Sun.COM 		/*
11718746SMatthew.Ahrens@Sun.COM 		 * It's a block in the intent log.  It has no
11728746SMatthew.Ahrens@Sun.COM 		 * accounting, so just free it.
11738746SMatthew.Ahrens@Sun.COM 		 */
11748746SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, dsl_free(ka->zio, ka->tx->tx_pool,
11758746SMatthew.Ahrens@Sun.COM 		    ka->tx->tx_txg, bp, NULL, NULL, ARC_NOWAIT));
11768746SMatthew.Ahrens@Sun.COM 	} else {
11778746SMatthew.Ahrens@Sun.COM 		ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
11788746SMatthew.Ahrens@Sun.COM 		(void) dsl_dataset_block_kill(ka->ds, bp, ka->zio, ka->tx);
11798746SMatthew.Ahrens@Sun.COM 	}
11807390SMatthew.Ahrens@Sun.COM 
1181789Sahrens 	return (0);
1182789Sahrens }
1183789Sahrens 
1184789Sahrens /* ARGSUSED */
11852199Sahrens static int
11862199Sahrens dsl_dataset_rollback_check(void *arg1, void *arg2, dmu_tx_t *tx)
1187789Sahrens {
11882199Sahrens 	dsl_dataset_t *ds = arg1;
11895367Sahrens 	dmu_objset_type_t *ost = arg2;
1190789Sahrens 
11912199Sahrens 	/*
11925367Sahrens 	 * We can only roll back to emptyness if it is a ZPL objset.
11932199Sahrens 	 */
11945367Sahrens 	if (*ost != DMU_OST_ZFS && ds->ds_phys->ds_prev_snap_txg == 0)
1195789Sahrens 		return (EINVAL);
1196789Sahrens 
11972199Sahrens 	/*
11982199Sahrens 	 * This must not be a snapshot.
11992199Sahrens 	 */
12002199Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
12012199Sahrens 		return (EINVAL);
1202789Sahrens 
1203789Sahrens 	/*
12047837SMatthew.Ahrens@Sun.COM 	 * If we made changes this txg, traverse_dataset won't find
1205789Sahrens 	 * them.  Try again.
1206789Sahrens 	 */
12072199Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1208789Sahrens 		return (EAGAIN);
12092199Sahrens 
12102199Sahrens 	return (0);
12112199Sahrens }
1212789Sahrens 
12132199Sahrens /* ARGSUSED */
12142199Sahrens static void
12154543Smarks dsl_dataset_rollback_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
12162199Sahrens {
12172199Sahrens 	dsl_dataset_t *ds = arg1;
12185367Sahrens 	dmu_objset_type_t *ost = arg2;
12192199Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1220789Sahrens 
1221789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1222789Sahrens 
12234967Sperrin 	if (ds->ds_user_ptr != NULL) {
12245367Sahrens 		/*
12255367Sahrens 		 * We need to make sure that the objset_impl_t is reopened after
12265367Sahrens 		 * we do the rollback, otherwise it will have the wrong
12275367Sahrens 		 * objset_phys_t.  Normally this would happen when this
12286689Smaybee 		 * dataset-open is closed, thus causing the
12295367Sahrens 		 * dataset to be immediately evicted.  But when doing "zfs recv
12305367Sahrens 		 * -F", we reopen the objset before that, so that there is no
12315367Sahrens 		 * window where the dataset is closed and inconsistent.
12325367Sahrens 		 */
12335367Sahrens 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
12345367Sahrens 		ds->ds_user_ptr = NULL;
12354967Sperrin 	}
12364935Sperrin 
12377390SMatthew.Ahrens@Sun.COM 	/* Transfer space that was freed since last snap back to the head. */
12387390SMatthew.Ahrens@Sun.COM 	{
12397390SMatthew.Ahrens@Sun.COM 		uint64_t used;
12407390SMatthew.Ahrens@Sun.COM 
12417390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&ds->ds_deadlist,
12427390SMatthew.Ahrens@Sun.COM 		    ds->ds_origin_txg, UINT64_MAX, &used));
12437390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(ds->ds_dir, used,
12447390SMatthew.Ahrens@Sun.COM 		    DD_USED_SNAP, DD_USED_HEAD, tx);
12457390SMatthew.Ahrens@Sun.COM 	}
12467390SMatthew.Ahrens@Sun.COM 
1247789Sahrens 	/* Zero out the deadlist. */
1248789Sahrens 	bplist_close(&ds->ds_deadlist);
1249789Sahrens 	bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1250789Sahrens 	ds->ds_phys->ds_deadlist_obj =
1251789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
12521544Seschrock 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
12531544Seschrock 	    ds->ds_phys->ds_deadlist_obj));
1254789Sahrens 
1255789Sahrens 	{
12568746SMatthew.Ahrens@Sun.COM 		/*
12578746SMatthew.Ahrens@Sun.COM 		 * Free blkptrs that we gave birth to - this covers
12588746SMatthew.Ahrens@Sun.COM 		 * claimed but not played log blocks too.
12598746SMatthew.Ahrens@Sun.COM 		 */
1260789Sahrens 		zio_t *zio;
1261789Sahrens 		struct killarg ka;
1262789Sahrens 
1263789Sahrens 		zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL,
1264789Sahrens 		    ZIO_FLAG_MUSTSUCCEED);
12657390SMatthew.Ahrens@Sun.COM 		ka.ds = ds;
1266789Sahrens 		ka.zio = zio;
1267789Sahrens 		ka.tx = tx;
12687837SMatthew.Ahrens@Sun.COM 		(void) traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
12697837SMatthew.Ahrens@Sun.COM 		    TRAVERSE_POST, kill_blkptr, &ka);
1270789Sahrens 		(void) zio_wait(zio);
1271789Sahrens 	}
1272789Sahrens 
12737390SMatthew.Ahrens@Sun.COM 	ASSERT(!(ds->ds_phys->ds_flags & DS_FLAG_UNIQUE_ACCURATE) ||
12747390SMatthew.Ahrens@Sun.COM 	    ds->ds_phys->ds_unique_bytes == 0);
12757390SMatthew.Ahrens@Sun.COM 
12767046Sahrens 	if (ds->ds_prev && ds->ds_prev != ds->ds_dir->dd_pool->dp_origin_snap) {
12775367Sahrens 		/* Change our contents to that of the prev snapshot */
12787390SMatthew.Ahrens@Sun.COM 
12795367Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
12805367Sahrens 		    ds->ds_phys->ds_prev_snap_obj);
12817390SMatthew.Ahrens@Sun.COM 		ASSERT3U(ds->ds_phys->ds_used_bytes, <=,
12827390SMatthew.Ahrens@Sun.COM 		    ds->ds_prev->ds_phys->ds_used_bytes);
12837390SMatthew.Ahrens@Sun.COM 
12845367Sahrens 		ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp;
12855367Sahrens 		ds->ds_phys->ds_used_bytes =
12865367Sahrens 		    ds->ds_prev->ds_phys->ds_used_bytes;
12875367Sahrens 		ds->ds_phys->ds_compressed_bytes =
12885367Sahrens 		    ds->ds_prev->ds_phys->ds_compressed_bytes;
12895367Sahrens 		ds->ds_phys->ds_uncompressed_bytes =
12905367Sahrens 		    ds->ds_prev->ds_phys->ds_uncompressed_bytes;
12915367Sahrens 		ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags;
1292789Sahrens 
12935367Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
12945367Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
12955367Sahrens 			ds->ds_prev->ds_phys->ds_unique_bytes = 0;
12965367Sahrens 		}
12975367Sahrens 	} else {
12987046Sahrens 		objset_impl_t *osi;
12997046Sahrens 
13007390SMatthew.Ahrens@Sun.COM 		ASSERT3U(ds->ds_phys->ds_used_bytes, ==, 0);
13017390SMatthew.Ahrens@Sun.COM 		ASSERT3U(ds->ds_phys->ds_compressed_bytes, ==, 0);
13027390SMatthew.Ahrens@Sun.COM 		ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, ==, 0);
13037390SMatthew.Ahrens@Sun.COM 
13045367Sahrens 		bzero(&ds->ds_phys->ds_bp, sizeof (blkptr_t));
13055367Sahrens 		ds->ds_phys->ds_flags = 0;
13065367Sahrens 		ds->ds_phys->ds_unique_bytes = 0;
13077390SMatthew.Ahrens@Sun.COM 		if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
13087390SMatthew.Ahrens@Sun.COM 		    SPA_VERSION_UNIQUE_ACCURATE)
13097390SMatthew.Ahrens@Sun.COM 			ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
13107390SMatthew.Ahrens@Sun.COM 
13117046Sahrens 		osi = dmu_objset_create_impl(ds->ds_dir->dd_pool->dp_spa, ds,
13125367Sahrens 		    &ds->ds_phys->ds_bp, *ost, tx);
13137046Sahrens #ifdef _KERNEL
13147046Sahrens 		zfs_create_fs(&osi->os, kcred, NULL, tx);
13157046Sahrens #endif
13162532Sahrens 	}
13174543Smarks 
13184543Smarks 	spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa,
13194543Smarks 	    tx, cr, "dataset = %llu", ds->ds_object);
1320789Sahrens }
1321789Sahrens 
13221731Sbonwick /* ARGSUSED */
13231731Sbonwick static int
13242199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
13251731Sbonwick {
13262199Sahrens 	dsl_dataset_t *ds = arg1;
13275367Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
13285367Sahrens 	uint64_t count;
13295367Sahrens 	int err;
13301731Sbonwick 
13311731Sbonwick 	/*
13321731Sbonwick 	 * Can't delete a head dataset if there are snapshots of it.
13331731Sbonwick 	 * (Except if the only snapshots are from the branch we cloned
13341731Sbonwick 	 * from.)
13351731Sbonwick 	 */
13361731Sbonwick 	if (ds->ds_prev != NULL &&
13371731Sbonwick 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
13381731Sbonwick 		return (EINVAL);
13391731Sbonwick 
13405367Sahrens 	/*
13415367Sahrens 	 * This is really a dsl_dir thing, but check it here so that
13425367Sahrens 	 * we'll be less likely to leave this dataset inconsistent &
13435367Sahrens 	 * nearly destroyed.
13445367Sahrens 	 */
13455367Sahrens 	err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
13465367Sahrens 	if (err)
13475367Sahrens 		return (err);
13485367Sahrens 	if (count != 0)
13495367Sahrens 		return (EEXIST);
13505367Sahrens 
13511731Sbonwick 	return (0);
13521731Sbonwick }
13531731Sbonwick 
13542199Sahrens /* ARGSUSED */
13552199Sahrens static void
13564543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1357789Sahrens {
13582199Sahrens 	dsl_dataset_t *ds = arg1;
13594543Smarks 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1360789Sahrens 
13612199Sahrens 	/* Mark it as inconsistent on-disk, in case we crash */
13622199Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
13632199Sahrens 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
13644543Smarks 
13654543Smarks 	spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
13664543Smarks 	    cr, "dataset = %llu", ds->ds_object);
13672199Sahrens }
1368789Sahrens 
13692199Sahrens /* ARGSUSED */
13705367Sahrens int
13712199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
13722199Sahrens {
13732199Sahrens 	dsl_dataset_t *ds = arg1;
1374789Sahrens 
13756689Smaybee 	/* we have an owner hold, so noone else can destroy us */
13766689Smaybee 	ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
13776689Smaybee 
1378789Sahrens 	/* Can't delete a branch point. */
13792199Sahrens 	if (ds->ds_phys->ds_num_children > 1)
13802199Sahrens 		return (EEXIST);
1381789Sahrens 
1382789Sahrens 	/*
1383789Sahrens 	 * Can't delete a head dataset if there are snapshots of it.
1384789Sahrens 	 * (Except if the only snapshots are from the branch we cloned
1385789Sahrens 	 * from.)
1386789Sahrens 	 */
1387789Sahrens 	if (ds->ds_prev != NULL &&
13882199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1389789Sahrens 		return (EINVAL);
1390789Sahrens 
1391789Sahrens 	/*
1392789Sahrens 	 * If we made changes this txg, traverse_dsl_dataset won't find
1393789Sahrens 	 * them.  Try again.
1394789Sahrens 	 */
13952199Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1396789Sahrens 		return (EAGAIN);
13972199Sahrens 
13982199Sahrens 	/* XXX we should do some i/o error checking... */
13992199Sahrens 	return (0);
14002199Sahrens }
14012199Sahrens 
14026689Smaybee struct refsarg {
14036689Smaybee 	kmutex_t lock;
14046689Smaybee 	boolean_t gone;
14056689Smaybee 	kcondvar_t cv;
14066689Smaybee };
14076689Smaybee 
14086689Smaybee /* ARGSUSED */
14096689Smaybee static void
14106689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv)
14116689Smaybee {
14126689Smaybee 	struct refsarg *arg = argv;
14136689Smaybee 
14146689Smaybee 	mutex_enter(&arg->lock);
14156689Smaybee 	arg->gone = TRUE;
14166689Smaybee 	cv_signal(&arg->cv);
14176689Smaybee 	mutex_exit(&arg->lock);
14186689Smaybee }
14196689Smaybee 
14206689Smaybee static void
14216689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag)
14226689Smaybee {
14236689Smaybee 	struct refsarg arg;
14246689Smaybee 
14256689Smaybee 	mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL);
14266689Smaybee 	cv_init(&arg.cv, NULL, CV_DEFAULT, NULL);
14276689Smaybee 	arg.gone = FALSE;
14286689Smaybee 	(void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys,
14296689Smaybee 	    dsl_dataset_refs_gone);
14306689Smaybee 	dmu_buf_rele(ds->ds_dbuf, tag);
14316689Smaybee 	mutex_enter(&arg.lock);
14326689Smaybee 	while (!arg.gone)
14336689Smaybee 		cv_wait(&arg.cv, &arg.lock);
14346689Smaybee 	ASSERT(arg.gone);
14356689Smaybee 	mutex_exit(&arg.lock);
14366689Smaybee 	ds->ds_dbuf = NULL;
14376689Smaybee 	ds->ds_phys = NULL;
14386689Smaybee 	mutex_destroy(&arg.lock);
14396689Smaybee 	cv_destroy(&arg.cv);
14406689Smaybee }
14416689Smaybee 
14425367Sahrens void
14434543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
14442199Sahrens {
14452199Sahrens 	dsl_dataset_t *ds = arg1;
14462199Sahrens 	zio_t *zio;
14472199Sahrens 	int err;
14482199Sahrens 	int after_branch_point = FALSE;
14492199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
14502199Sahrens 	objset_t *mos = dp->dp_meta_objset;
14512199Sahrens 	dsl_dataset_t *ds_prev = NULL;
14522199Sahrens 	uint64_t obj;
14532199Sahrens 
14546689Smaybee 	ASSERT(ds->ds_owner);
14552199Sahrens 	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
14562199Sahrens 	ASSERT(ds->ds_prev == NULL ||
14572199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
14582199Sahrens 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
14592199Sahrens 
14606689Smaybee 	/* signal any waiters that this dataset is going away */
14616689Smaybee 	mutex_enter(&ds->ds_lock);
14626689Smaybee 	ds->ds_owner = dsl_reaper;
14636689Smaybee 	cv_broadcast(&ds->ds_exclusive_cv);
14646689Smaybee 	mutex_exit(&ds->ds_lock);
14656689Smaybee 
14665378Sck153898 	/* Remove our reservation */
14675378Sck153898 	if (ds->ds_reserved != 0) {
14685378Sck153898 		uint64_t val = 0;
14695378Sck153898 		dsl_dataset_set_reservation_sync(ds, &val, cr, tx);
14705378Sck153898 		ASSERT3U(ds->ds_reserved, ==, 0);
14715378Sck153898 	}
14725378Sck153898 
14732199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
14742199Sahrens 
14757046Sahrens 	dsl_pool_ds_destroyed(ds, tx);
14767046Sahrens 
14772199Sahrens 	obj = ds->ds_object;
1478789Sahrens 
1479789Sahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1480789Sahrens 		if (ds->ds_prev) {
1481789Sahrens 			ds_prev = ds->ds_prev;
1482789Sahrens 		} else {
14836689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
14846689Smaybee 			    ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
1485789Sahrens 		}
1486789Sahrens 		after_branch_point =
1487789Sahrens 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
1488789Sahrens 
1489789Sahrens 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1490789Sahrens 		if (after_branch_point &&
14917046Sahrens 		    ds_prev->ds_phys->ds_next_clones_obj != 0) {
14927046Sahrens 			VERIFY(0 == zap_remove_int(mos,
14937046Sahrens 			    ds_prev->ds_phys->ds_next_clones_obj, obj, tx));
14947046Sahrens 			if (ds->ds_phys->ds_next_snap_obj != 0) {
14957046Sahrens 				VERIFY(0 == zap_add_int(mos,
14967046Sahrens 				    ds_prev->ds_phys->ds_next_clones_obj,
14977046Sahrens 				    ds->ds_phys->ds_next_snap_obj, tx));
14987046Sahrens 			}
14997046Sahrens 		}
15007046Sahrens 		if (after_branch_point &&
1501789Sahrens 		    ds->ds_phys->ds_next_snap_obj == 0) {
1502789Sahrens 			/* This clone is toast. */
1503789Sahrens 			ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1504789Sahrens 			ds_prev->ds_phys->ds_num_children--;
1505789Sahrens 		} else if (!after_branch_point) {
1506789Sahrens 			ds_prev->ds_phys->ds_next_snap_obj =
1507789Sahrens 			    ds->ds_phys->ds_next_snap_obj;
1508789Sahrens 		}
1509789Sahrens 	}
1510789Sahrens 
1511789Sahrens 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1512789Sahrens 
1513789Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0) {
15142199Sahrens 		blkptr_t bp;
1515789Sahrens 		dsl_dataset_t *ds_next;
1516789Sahrens 		uint64_t itor = 0;
15175378Sck153898 		uint64_t old_unique;
15187390SMatthew.Ahrens@Sun.COM 		int64_t used = 0, compressed = 0, uncompressed = 0;
1519789Sahrens 
15206689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
15216689Smaybee 		    ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
1522789Sahrens 		ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1523789Sahrens 
15245378Sck153898 		old_unique = dsl_dataset_unique(ds_next);
15255378Sck153898 
1526789Sahrens 		dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1527789Sahrens 		ds_next->ds_phys->ds_prev_snap_obj =
1528789Sahrens 		    ds->ds_phys->ds_prev_snap_obj;
1529789Sahrens 		ds_next->ds_phys->ds_prev_snap_txg =
1530789Sahrens 		    ds->ds_phys->ds_prev_snap_txg;
1531789Sahrens 		ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1532789Sahrens 		    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1533789Sahrens 
1534789Sahrens 		/*
1535789Sahrens 		 * Transfer to our deadlist (which will become next's
1536789Sahrens 		 * new deadlist) any entries from next's current
1537789Sahrens 		 * deadlist which were born before prev, and free the
1538789Sahrens 		 * other entries.
1539789Sahrens 		 *
1540789Sahrens 		 * XXX we're doing this long task with the config lock held
1541789Sahrens 		 */
15426689Smaybee 		while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) {
1543789Sahrens 			if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) {
15441544Seschrock 				VERIFY(0 == bplist_enqueue(&ds->ds_deadlist,
15451544Seschrock 				    &bp, tx));
1546789Sahrens 				if (ds_prev && !after_branch_point &&
1547789Sahrens 				    bp.blk_birth >
1548789Sahrens 				    ds_prev->ds_phys->ds_prev_snap_txg) {
1549789Sahrens 					ds_prev->ds_phys->ds_unique_bytes +=
15502082Seschrock 					    bp_get_dasize(dp->dp_spa, &bp);
1551789Sahrens 				}
1552789Sahrens 			} else {
15532082Seschrock 				used += bp_get_dasize(dp->dp_spa, &bp);
1554789Sahrens 				compressed += BP_GET_PSIZE(&bp);
1555789Sahrens 				uncompressed += BP_GET_UCSIZE(&bp);
1556789Sahrens 				/* XXX check return value? */
15577046Sahrens 				(void) dsl_free(zio, dp, tx->tx_txg,
1558789Sahrens 				    &bp, NULL, NULL, ARC_NOWAIT);
1559789Sahrens 			}
1560789Sahrens 		}
1561789Sahrens 
15627390SMatthew.Ahrens@Sun.COM 		ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes);
15637390SMatthew.Ahrens@Sun.COM 
15647390SMatthew.Ahrens@Sun.COM 		/* change snapused */
15657390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
15667390SMatthew.Ahrens@Sun.COM 		    -used, -compressed, -uncompressed, tx);
15677390SMatthew.Ahrens@Sun.COM 
1568789Sahrens 		/* free next's deadlist */
1569789Sahrens 		bplist_close(&ds_next->ds_deadlist);
1570789Sahrens 		bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx);
1571789Sahrens 
1572789Sahrens 		/* set next's deadlist to our deadlist */
15736689Smaybee 		bplist_close(&ds->ds_deadlist);
1574789Sahrens 		ds_next->ds_phys->ds_deadlist_obj =
1575789Sahrens 		    ds->ds_phys->ds_deadlist_obj;
15761544Seschrock 		VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos,
15771544Seschrock 		    ds_next->ds_phys->ds_deadlist_obj));
1578789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1579789Sahrens 
1580789Sahrens 		if (ds_next->ds_phys->ds_next_snap_obj != 0) {
1581789Sahrens 			/*
1582789Sahrens 			 * Update next's unique to include blocks which
1583789Sahrens 			 * were previously shared by only this snapshot
1584789Sahrens 			 * and it.  Those blocks will be born after the
1585789Sahrens 			 * prev snap and before this snap, and will have
1586789Sahrens 			 * died after the next snap and before the one
1587789Sahrens 			 * after that (ie. be on the snap after next's
1588789Sahrens 			 * deadlist).
1589789Sahrens 			 *
1590789Sahrens 			 * XXX we're doing this long task with the
1591789Sahrens 			 * config lock held
1592789Sahrens 			 */
1593789Sahrens 			dsl_dataset_t *ds_after_next;
15947390SMatthew.Ahrens@Sun.COM 			uint64_t space;
1595789Sahrens 
15966689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
15976689Smaybee 			    ds_next->ds_phys->ds_next_snap_obj,
15986689Smaybee 			    FTAG, &ds_after_next));
15997390SMatthew.Ahrens@Sun.COM 
16007390SMatthew.Ahrens@Sun.COM 			VERIFY(0 ==
16017390SMatthew.Ahrens@Sun.COM 			    bplist_space_birthrange(&ds_after_next->ds_deadlist,
16027390SMatthew.Ahrens@Sun.COM 			    ds->ds_phys->ds_prev_snap_txg,
16037390SMatthew.Ahrens@Sun.COM 			    ds->ds_phys->ds_creation_txg, &space));
16047390SMatthew.Ahrens@Sun.COM 			ds_next->ds_phys->ds_unique_bytes += space;
1605789Sahrens 
16066689Smaybee 			dsl_dataset_rele(ds_after_next, FTAG);
1607789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, NULL);
1608789Sahrens 		} else {
1609789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, ds);
16106689Smaybee 			dsl_dataset_drop_ref(ds_next->ds_prev, ds_next);
16116689Smaybee 			ds_next->ds_prev = NULL;
1612789Sahrens 			if (ds_prev) {
16136689Smaybee 				VERIFY(0 == dsl_dataset_get_ref(dp,
16146689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
16156689Smaybee 				    ds_next, &ds_next->ds_prev));
1616789Sahrens 			}
16175378Sck153898 
16185378Sck153898 			dsl_dataset_recalc_head_uniq(ds_next);
16195378Sck153898 
16205378Sck153898 			/*
16215378Sck153898 			 * Reduce the amount of our unconsmed refreservation
16225378Sck153898 			 * being charged to our parent by the amount of
16235378Sck153898 			 * new unique data we have gained.
16245378Sck153898 			 */
16255378Sck153898 			if (old_unique < ds_next->ds_reserved) {
16265378Sck153898 				int64_t mrsdelta;
16275378Sck153898 				uint64_t new_unique =
16285378Sck153898 				    ds_next->ds_phys->ds_unique_bytes;
16295378Sck153898 
16305378Sck153898 				ASSERT(old_unique <= new_unique);
16315378Sck153898 				mrsdelta = MIN(new_unique - old_unique,
16325378Sck153898 				    ds_next->ds_reserved - old_unique);
16337390SMatthew.Ahrens@Sun.COM 				dsl_dir_diduse_space(ds->ds_dir,
16347390SMatthew.Ahrens@Sun.COM 				    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
16355378Sck153898 			}
1636789Sahrens 		}
16376689Smaybee 		dsl_dataset_rele(ds_next, FTAG);
1638789Sahrens 	} else {
1639789Sahrens 		/*
1640789Sahrens 		 * There's no next snapshot, so this is a head dataset.
1641789Sahrens 		 * Destroy the deadlist.  Unless it's a clone, the
1642789Sahrens 		 * deadlist should be empty.  (If it's a clone, it's
1643789Sahrens 		 * safe to ignore the deadlist contents.)
1644789Sahrens 		 */
1645789Sahrens 		struct killarg ka;
1646789Sahrens 
1647789Sahrens 		ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist));
1648789Sahrens 		bplist_close(&ds->ds_deadlist);
1649789Sahrens 		bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1650789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1651789Sahrens 
1652789Sahrens 		/*
1653789Sahrens 		 * Free everything that we point to (that's born after
1654789Sahrens 		 * the previous snapshot, if we are a clone)
1655789Sahrens 		 *
16567390SMatthew.Ahrens@Sun.COM 		 * NB: this should be very quick, because we already
16577390SMatthew.Ahrens@Sun.COM 		 * freed all the objects in open context.
1658789Sahrens 		 */
16597390SMatthew.Ahrens@Sun.COM 		ka.ds = ds;
1660789Sahrens 		ka.zio = zio;
1661789Sahrens 		ka.tx = tx;
16627837SMatthew.Ahrens@Sun.COM 		err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
16637837SMatthew.Ahrens@Sun.COM 		    TRAVERSE_POST, kill_blkptr, &ka);
1664789Sahrens 		ASSERT3U(err, ==, 0);
16657390SMatthew.Ahrens@Sun.COM 		ASSERT(spa_version(dp->dp_spa) < SPA_VERSION_UNIQUE_ACCURATE ||
16667390SMatthew.Ahrens@Sun.COM 		    ds->ds_phys->ds_unique_bytes == 0);
1667789Sahrens 	}
1668789Sahrens 
1669789Sahrens 	err = zio_wait(zio);
1670789Sahrens 	ASSERT3U(err, ==, 0);
1671789Sahrens 
16726689Smaybee 	if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
16736689Smaybee 		/* Erase the link in the dir */
16746689Smaybee 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
16756689Smaybee 		ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
16766689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
1677789Sahrens 		err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1678789Sahrens 		ASSERT(err == 0);
1679789Sahrens 	} else {
1680789Sahrens 		/* remove from snapshot namespace */
1681789Sahrens 		dsl_dataset_t *ds_head;
16826689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
16836689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
16846689Smaybee 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
16852207Sahrens 		VERIFY(0 == dsl_dataset_get_snapname(ds));
1686789Sahrens #ifdef ZFS_DEBUG
1687789Sahrens 		{
1688789Sahrens 			uint64_t val;
16896492Stimh 
16906689Smaybee 			err = dsl_dataset_snap_lookup(ds_head,
16916492Stimh 			    ds->ds_snapname, &val);
1692789Sahrens 			ASSERT3U(err, ==, 0);
1693789Sahrens 			ASSERT3U(val, ==, obj);
1694789Sahrens 		}
1695789Sahrens #endif
16966689Smaybee 		err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
1697789Sahrens 		ASSERT(err == 0);
16986689Smaybee 		dsl_dataset_rele(ds_head, FTAG);
1699789Sahrens 	}
1700789Sahrens 
1701789Sahrens 	if (ds_prev && ds->ds_prev != ds_prev)
17026689Smaybee 		dsl_dataset_rele(ds_prev, FTAG);
1703789Sahrens 
17045094Slling 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
17054543Smarks 	spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx,
17064543Smarks 	    cr, "dataset = %llu", ds->ds_object);
17074543Smarks 
17087046Sahrens 	if (ds->ds_phys->ds_next_clones_obj != 0) {
17097046Sahrens 		uint64_t count;
17107046Sahrens 		ASSERT(0 == zap_count(mos,
17117046Sahrens 		    ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
17127046Sahrens 		VERIFY(0 == dmu_object_free(mos,
17137046Sahrens 		    ds->ds_phys->ds_next_clones_obj, tx));
17147046Sahrens 	}
17157390SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_props_obj != 0)
17167390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
17176689Smaybee 	dsl_dir_close(ds->ds_dir, ds);
17186689Smaybee 	ds->ds_dir = NULL;
17196689Smaybee 	dsl_dataset_drain_refs(ds, tag);
17202199Sahrens 	VERIFY(0 == dmu_object_free(mos, obj, tx));
17212199Sahrens }
17222199Sahrens 
17235378Sck153898 static int
17245378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
17255378Sck153898 {
17265378Sck153898 	uint64_t asize;
17275378Sck153898 
17285378Sck153898 	if (!dmu_tx_is_syncing(tx))
17295378Sck153898 		return (0);
17305378Sck153898 
17315378Sck153898 	/*
17325378Sck153898 	 * If there's an fs-only reservation, any blocks that might become
17335378Sck153898 	 * owned by the snapshot dataset must be accommodated by space
17345378Sck153898 	 * outside of the reservation.
17355378Sck153898 	 */
17365378Sck153898 	asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
17375378Sck153898 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE))
17385378Sck153898 		return (ENOSPC);
17395378Sck153898 
17405378Sck153898 	/*
17415378Sck153898 	 * Propogate any reserved space for this snapshot to other
17425378Sck153898 	 * snapshot checks in this sync group.
17435378Sck153898 	 */
17445378Sck153898 	if (asize > 0)
17455378Sck153898 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
17465378Sck153898 
17475378Sck153898 	return (0);
17485378Sck153898 }
17495378Sck153898 
17502199Sahrens /* ARGSUSED */
17512199Sahrens int
17522199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
17532199Sahrens {
17545367Sahrens 	dsl_dataset_t *ds = arg1;
17552199Sahrens 	const char *snapname = arg2;
17562199Sahrens 	int err;
17572199Sahrens 	uint64_t value;
1758789Sahrens 
1759789Sahrens 	/*
17602199Sahrens 	 * We don't allow multiple snapshots of the same txg.  If there
17612199Sahrens 	 * is already one, try again.
17622199Sahrens 	 */
17632199Sahrens 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
17642199Sahrens 		return (EAGAIN);
17652199Sahrens 
17662199Sahrens 	/*
17672199Sahrens 	 * Check for conflicting name snapshot name.
1768789Sahrens 	 */
17696689Smaybee 	err = dsl_dataset_snap_lookup(ds, snapname, &value);
17702199Sahrens 	if (err == 0)
17712199Sahrens 		return (EEXIST);
17722199Sahrens 	if (err != ENOENT)
17732199Sahrens 		return (err);
1774789Sahrens 
17753978Smmusante 	/*
17763978Smmusante 	 * Check that the dataset's name is not too long.  Name consists
17773978Smmusante 	 * of the dataset's length + 1 for the @-sign + snapshot name's length
17783978Smmusante 	 */
17793978Smmusante 	if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
17803978Smmusante 		return (ENAMETOOLONG);
17813978Smmusante 
17825378Sck153898 	err = dsl_dataset_snapshot_reserve_space(ds, tx);
17835378Sck153898 	if (err)
17845378Sck153898 		return (err);
17855378Sck153898 
17862199Sahrens 	ds->ds_trysnap_txg = tx->tx_txg;
1787789Sahrens 	return (0);
1788789Sahrens }
1789789Sahrens 
17902199Sahrens void
17914543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1792789Sahrens {
17935367Sahrens 	dsl_dataset_t *ds = arg1;
17942199Sahrens 	const char *snapname = arg2;
17952199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1796789Sahrens 	dmu_buf_t *dbuf;
1797789Sahrens 	dsl_dataset_phys_t *dsphys;
17987046Sahrens 	uint64_t dsobj, crtxg;
1799789Sahrens 	objset_t *mos = dp->dp_meta_objset;
1800789Sahrens 	int err;
1801789Sahrens 
18022199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1803789Sahrens 
18047046Sahrens 	/*
18057046Sahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
18067046Sahrens 	 */
18077046Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
18087046Sahrens 		crtxg = 1;
18097046Sahrens 	else
18107046Sahrens 		crtxg = tx->tx_txg;
18117046Sahrens 
1812928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1813928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
18141544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1815789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1816789Sahrens 	dsphys = dbuf->db_data;
18176689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
18182199Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1819789Sahrens 	dsphys->ds_fsid_guid = unique_create();
1820789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1821789Sahrens 	    sizeof (dsphys->ds_guid));
1822789Sahrens 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1823789Sahrens 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1824789Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1825789Sahrens 	dsphys->ds_num_children = 1;
1826789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
18277046Sahrens 	dsphys->ds_creation_txg = crtxg;
1828789Sahrens 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1829789Sahrens 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
1830789Sahrens 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1831789Sahrens 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
18322082Seschrock 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1833789Sahrens 	dsphys->ds_bp = ds->ds_phys->ds_bp;
18341544Seschrock 	dmu_buf_rele(dbuf, FTAG);
1835789Sahrens 
18362199Sahrens 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
18372199Sahrens 	if (ds->ds_prev) {
18387046Sahrens 		uint64_t next_clones_obj =
18397046Sahrens 		    ds->ds_prev->ds_phys->ds_next_clones_obj;
18402199Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1841789Sahrens 		    ds->ds_object ||
18422199Sahrens 		    ds->ds_prev->ds_phys->ds_num_children > 1);
18432199Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
18442199Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1845789Sahrens 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
18462199Sahrens 			    ds->ds_prev->ds_phys->ds_creation_txg);
18472199Sahrens 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
18487046Sahrens 		} else if (next_clones_obj != 0) {
18497046Sahrens 			VERIFY3U(0, ==, zap_remove_int(mos,
18507046Sahrens 			    next_clones_obj, dsphys->ds_next_snap_obj, tx));
18517046Sahrens 			VERIFY3U(0, ==, zap_add_int(mos,
18527046Sahrens 			    next_clones_obj, dsobj, tx));
1853789Sahrens 		}
1854789Sahrens 	}
1855789Sahrens 
18565378Sck153898 	/*
18575378Sck153898 	 * If we have a reference-reservation on this dataset, we will
18585378Sck153898 	 * need to increase the amount of refreservation being charged
18595378Sck153898 	 * since our unique space is going to zero.
18605378Sck153898 	 */
18615378Sck153898 	if (ds->ds_reserved) {
18625378Sck153898 		int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
18637390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
18647390SMatthew.Ahrens@Sun.COM 		    add, 0, 0, tx);
18655378Sck153898 	}
18665378Sck153898 
1867789Sahrens 	bplist_close(&ds->ds_deadlist);
1868789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
18695712Sahrens 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1870789Sahrens 	ds->ds_phys->ds_prev_snap_obj = dsobj;
18717046Sahrens 	ds->ds_phys->ds_prev_snap_txg = crtxg;
1872789Sahrens 	ds->ds_phys->ds_unique_bytes = 0;
18735378Sck153898 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
18745378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1875789Sahrens 	ds->ds_phys->ds_deadlist_obj =
1876789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
18771544Seschrock 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
18781544Seschrock 	    ds->ds_phys->ds_deadlist_obj));
1879789Sahrens 
1880789Sahrens 	dprintf("snap '%s' -> obj %llu\n", snapname, dsobj);
1881789Sahrens 	err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1882789Sahrens 	    snapname, 8, 1, &dsobj, tx);
1883789Sahrens 	ASSERT(err == 0);
1884789Sahrens 
1885789Sahrens 	if (ds->ds_prev)
18866689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
18876689Smaybee 	VERIFY(0 == dsl_dataset_get_ref(dp,
18886689Smaybee 	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
18894543Smarks 
18907046Sahrens 	dsl_pool_ds_snapshotted(ds, tx);
18917046Sahrens 
18924543Smarks 	spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr,
18934603Sahrens 	    "dataset = %llu", dsobj);
1894789Sahrens }
1895789Sahrens 
1896789Sahrens void
18973547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1898789Sahrens {
1899789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1900789Sahrens 	ASSERT(ds->ds_user_ptr != NULL);
1901789Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1902789Sahrens 
19034787Sahrens 	/*
19044787Sahrens 	 * in case we had to change ds_fsid_guid when we opened it,
19054787Sahrens 	 * sync it out now.
19064787Sahrens 	 */
19074787Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
19084787Sahrens 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
19094787Sahrens 
1910789Sahrens 	dsl_dir_dirty(ds->ds_dir, tx);
19113547Smaybee 	dmu_objset_sync(ds->ds_user_ptr, zio, tx);
1912789Sahrens }
1913789Sahrens 
1914789Sahrens void
19152885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1916789Sahrens {
19175378Sck153898 	uint64_t refd, avail, uobjs, aobjs;
19185378Sck153898 
19192885Sahrens 	dsl_dir_stats(ds->ds_dir, nv);
1920789Sahrens 
19215378Sck153898 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
19225378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
19235378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
19245378Sck153898 
19252885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
19262885Sahrens 	    ds->ds_phys->ds_creation_time);
19272885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
19282885Sahrens 	    ds->ds_phys->ds_creation_txg);
19295378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
19305378Sck153898 	    ds->ds_quota);
19315378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
19325378Sck153898 	    ds->ds_reserved);
19336643Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
19346643Seschrock 	    ds->ds_phys->ds_guid);
1935789Sahrens 
1936789Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
1937789Sahrens 		/*
1938789Sahrens 		 * This is a snapshot; override the dd's space used with
19392885Sahrens 		 * our unique space and compression ratio.
1940789Sahrens 		 */
19412885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
19422885Sahrens 		    ds->ds_phys->ds_unique_bytes);
19432885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
19442885Sahrens 		    ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
19452885Sahrens 		    (ds->ds_phys->ds_uncompressed_bytes * 100 /
19462885Sahrens 		    ds->ds_phys->ds_compressed_bytes));
1947789Sahrens 	}
1948789Sahrens }
1949789Sahrens 
19502885Sahrens void
19512885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1952789Sahrens {
19532885Sahrens 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
19542885Sahrens 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
19555367Sahrens 	stat->dds_guid = ds->ds_phys->ds_guid;
19562885Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
19572885Sahrens 		stat->dds_is_snapshot = B_TRUE;
19582885Sahrens 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
19598228SEric.Taylor@Sun.COM 	} else {
19608228SEric.Taylor@Sun.COM 		stat->dds_is_snapshot = B_FALSE;
19618228SEric.Taylor@Sun.COM 		stat->dds_num_clones = 0;
19622885Sahrens 	}
19632885Sahrens 
19642885Sahrens 	/* clone origin is really a dsl_dir thing... */
19655446Sahrens 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
19667046Sahrens 	if (dsl_dir_is_clone(ds->ds_dir)) {
19672885Sahrens 		dsl_dataset_t *ods;
19682885Sahrens 
19696689Smaybee 		VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
19706689Smaybee 		    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
19715367Sahrens 		dsl_dataset_name(ods, stat->dds_origin);
19726689Smaybee 		dsl_dataset_drop_ref(ods, FTAG);
19738228SEric.Taylor@Sun.COM 	} else {
19748228SEric.Taylor@Sun.COM 		stat->dds_origin[0] = '\0';
19752885Sahrens 	}
19765446Sahrens 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
19772885Sahrens }
19782885Sahrens 
19792885Sahrens uint64_t
19802885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
19812885Sahrens {
19824787Sahrens 	return (ds->ds_fsid_guid);
19832885Sahrens }
19842885Sahrens 
19852885Sahrens void
19862885Sahrens dsl_dataset_space(dsl_dataset_t *ds,
19872885Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
19882885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
19892885Sahrens {
19902885Sahrens 	*refdbytesp = ds->ds_phys->ds_used_bytes;
19912885Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
19925378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
19935378Sck153898 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
19945378Sck153898 	if (ds->ds_quota != 0) {
19955378Sck153898 		/*
19965378Sck153898 		 * Adjust available bytes according to refquota
19975378Sck153898 		 */
19985378Sck153898 		if (*refdbytesp < ds->ds_quota)
19995378Sck153898 			*availbytesp = MIN(*availbytesp,
20005378Sck153898 			    ds->ds_quota - *refdbytesp);
20015378Sck153898 		else
20025378Sck153898 			*availbytesp = 0;
20035378Sck153898 	}
20042885Sahrens 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
20052885Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
2006789Sahrens }
2007789Sahrens 
20085326Sek110237 boolean_t
20095326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
20105326Sek110237 {
20115326Sek110237 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
20125326Sek110237 
20135326Sek110237 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
20145326Sek110237 	    dsl_pool_sync_context(dp));
20155326Sek110237 	if (ds->ds_prev == NULL)
20165326Sek110237 		return (B_FALSE);
20175326Sek110237 	if (ds->ds_phys->ds_bp.blk_birth >
20185326Sek110237 	    ds->ds_prev->ds_phys->ds_creation_txg)
20195326Sek110237 		return (B_TRUE);
20205326Sek110237 	return (B_FALSE);
20215326Sek110237 }
20225326Sek110237 
20232199Sahrens /* ARGSUSED */
2024789Sahrens static int
20252199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
2026789Sahrens {
20272199Sahrens 	dsl_dataset_t *ds = arg1;
20282199Sahrens 	char *newsnapname = arg2;
20292199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
20302199Sahrens 	dsl_dataset_t *hds;
20312199Sahrens 	uint64_t val;
2032789Sahrens 	int err;
2033789Sahrens 
20346689Smaybee 	err = dsl_dataset_hold_obj(dd->dd_pool,
20356689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds);
2036789Sahrens 	if (err)
2037789Sahrens 		return (err);
2038789Sahrens 
20392199Sahrens 	/* new name better not be in use */
20406689Smaybee 	err = dsl_dataset_snap_lookup(hds, newsnapname, &val);
20416689Smaybee 	dsl_dataset_rele(hds, FTAG);
2042789Sahrens 
20432199Sahrens 	if (err == 0)
20442199Sahrens 		err = EEXIST;
20452199Sahrens 	else if (err == ENOENT)
20462199Sahrens 		err = 0;
20474007Smmusante 
20484007Smmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
20494007Smmusante 	if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
20504007Smmusante 		err = ENAMETOOLONG;
20514007Smmusante 
20522199Sahrens 	return (err);
20532199Sahrens }
2054789Sahrens 
20552199Sahrens static void
20564543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2,
20574543Smarks     cred_t *cr, dmu_tx_t *tx)
20582199Sahrens {
20592199Sahrens 	dsl_dataset_t *ds = arg1;
20604543Smarks 	const char *newsnapname = arg2;
20612199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
20622199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
20632199Sahrens 	dsl_dataset_t *hds;
20642199Sahrens 	int err;
2065789Sahrens 
20662199Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2067789Sahrens 
20686689Smaybee 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
20696689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
2070789Sahrens 
20712199Sahrens 	VERIFY(0 == dsl_dataset_get_snapname(ds));
20726689Smaybee 	err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
2073789Sahrens 	ASSERT3U(err, ==, 0);
20742199Sahrens 	mutex_enter(&ds->ds_lock);
20752199Sahrens 	(void) strcpy(ds->ds_snapname, newsnapname);
20762199Sahrens 	mutex_exit(&ds->ds_lock);
20772199Sahrens 	err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
20782199Sahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2079789Sahrens 	ASSERT3U(err, ==, 0);
2080789Sahrens 
20814543Smarks 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
20824543Smarks 	    cr, "dataset = %llu", ds->ds_object);
20836689Smaybee 	dsl_dataset_rele(hds, FTAG);
2084789Sahrens }
2085789Sahrens 
20865326Sek110237 struct renamesnaparg {
20874007Smmusante 	dsl_sync_task_group_t *dstg;
20884007Smmusante 	char failed[MAXPATHLEN];
20894007Smmusante 	char *oldsnap;
20904007Smmusante 	char *newsnap;
20914007Smmusante };
20924007Smmusante 
20934007Smmusante static int
20944007Smmusante dsl_snapshot_rename_one(char *name, void *arg)
20954007Smmusante {
20965326Sek110237 	struct renamesnaparg *ra = arg;
20974007Smmusante 	dsl_dataset_t *ds = NULL;
20984007Smmusante 	char *cp;
20994007Smmusante 	int err;
21004007Smmusante 
21014007Smmusante 	cp = name + strlen(name);
21024007Smmusante 	*cp = '@';
21034007Smmusante 	(void) strcpy(cp + 1, ra->oldsnap);
21044543Smarks 
21054543Smarks 	/*
21064543Smarks 	 * For recursive snapshot renames the parent won't be changing
21074543Smarks 	 * so we just pass name for both the to/from argument.
21084543Smarks 	 */
21097312SMatthew.Ahrens@Sun.COM 	err = zfs_secpolicy_rename_perms(name, name, CRED());
21107312SMatthew.Ahrens@Sun.COM 	if (err == ENOENT) {
21117312SMatthew.Ahrens@Sun.COM 		return (0);
21127312SMatthew.Ahrens@Sun.COM 	} else if (err) {
21134543Smarks 		(void) strcpy(ra->failed, name);
21144543Smarks 		return (err);
21154543Smarks 	}
21164543Smarks 
21176689Smaybee #ifdef _KERNEL
21186689Smaybee 	/*
21196689Smaybee 	 * For all filesystems undergoing rename, we'll need to unmount it.
21206689Smaybee 	 */
21216689Smaybee 	(void) zfs_unmount_snap(name, NULL);
21226689Smaybee #endif
21236689Smaybee 	err = dsl_dataset_hold(name, ra->dstg, &ds);
21246689Smaybee 	*cp = '\0';
21254007Smmusante 	if (err == ENOENT) {
21264007Smmusante 		return (0);
21276689Smaybee 	} else if (err) {
21284007Smmusante 		(void) strcpy(ra->failed, name);
21294007Smmusante 		return (err);
21304007Smmusante 	}
21314007Smmusante 
21324007Smmusante 	dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
21334007Smmusante 	    dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
21344007Smmusante 
21354007Smmusante 	return (0);
21364007Smmusante }
21374007Smmusante 
21384007Smmusante static int
21394007Smmusante dsl_recursive_rename(char *oldname, const char *newname)
21404007Smmusante {
21414007Smmusante 	int err;
21425326Sek110237 	struct renamesnaparg *ra;
21434007Smmusante 	dsl_sync_task_t *dst;
21444007Smmusante 	spa_t *spa;
21454007Smmusante 	char *cp, *fsname = spa_strdup(oldname);
21464007Smmusante 	int len = strlen(oldname);
21474007Smmusante 
21484007Smmusante 	/* truncate the snapshot name to get the fsname */
21494007Smmusante 	cp = strchr(fsname, '@');
21504007Smmusante 	*cp = '\0';
21514007Smmusante 
21524603Sahrens 	err = spa_open(fsname, &spa, FTAG);
21534007Smmusante 	if (err) {
21544007Smmusante 		kmem_free(fsname, len + 1);
21554007Smmusante 		return (err);
21564007Smmusante 	}
21575326Sek110237 	ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP);
21584007Smmusante 	ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
21594007Smmusante 
21604007Smmusante 	ra->oldsnap = strchr(oldname, '@') + 1;
21614007Smmusante 	ra->newsnap = strchr(newname, '@') + 1;
21624007Smmusante 	*ra->failed = '\0';
21634007Smmusante 
21644007Smmusante 	err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
21654007Smmusante 	    DS_FIND_CHILDREN);
21664007Smmusante 	kmem_free(fsname, len + 1);
21674007Smmusante 
21684007Smmusante 	if (err == 0) {
21694007Smmusante 		err = dsl_sync_task_group_wait(ra->dstg);
21704007Smmusante 	}
21714007Smmusante 
21724007Smmusante 	for (dst = list_head(&ra->dstg->dstg_tasks); dst;
21734007Smmusante 	    dst = list_next(&ra->dstg->dstg_tasks, dst)) {
21744007Smmusante 		dsl_dataset_t *ds = dst->dst_arg1;
21754007Smmusante 		if (dst->dst_err) {
21764007Smmusante 			dsl_dir_name(ds->ds_dir, ra->failed);
21774009Smmusante 			(void) strcat(ra->failed, "@");
21784009Smmusante 			(void) strcat(ra->failed, ra->newsnap);
21794007Smmusante 		}
21806689Smaybee 		dsl_dataset_rele(ds, ra->dstg);
21814007Smmusante 	}
21824007Smmusante 
21834543Smarks 	if (err)
21844543Smarks 		(void) strcpy(oldname, ra->failed);
21854007Smmusante 
21864007Smmusante 	dsl_sync_task_group_destroy(ra->dstg);
21875326Sek110237 	kmem_free(ra, sizeof (struct renamesnaparg));
21884007Smmusante 	spa_close(spa, FTAG);
21894007Smmusante 	return (err);
21904007Smmusante }
21914007Smmusante 
21924569Smmusante static int
21934569Smmusante dsl_valid_rename(char *oldname, void *arg)
21944569Smmusante {
21954569Smmusante 	int delta = *(int *)arg;
21964569Smmusante 
21974569Smmusante 	if (strlen(oldname) + delta >= MAXNAMELEN)
21984569Smmusante 		return (ENAMETOOLONG);
21994569Smmusante 
22004569Smmusante 	return (0);
22014569Smmusante }
22024569Smmusante 
2203789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename
2204789Sahrens int
22056689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive)
2206789Sahrens {
2207789Sahrens 	dsl_dir_t *dd;
22082199Sahrens 	dsl_dataset_t *ds;
2209789Sahrens 	const char *tail;
2210789Sahrens 	int err;
2211789Sahrens 
22122199Sahrens 	err = dsl_dir_open(oldname, FTAG, &dd, &tail);
22131544Seschrock 	if (err)
22141544Seschrock 		return (err);
22158517SEric.Taylor@Sun.COM 	/*
22168517SEric.Taylor@Sun.COM 	 * If there are more than 2 references there may be holds
22178517SEric.Taylor@Sun.COM 	 * hanging around that haven't been cleared out yet.
22188517SEric.Taylor@Sun.COM 	 */
22198517SEric.Taylor@Sun.COM 	if (dmu_buf_refcount(dd->dd_dbuf) > 2)
22208517SEric.Taylor@Sun.COM 		txg_wait_synced(dd->dd_pool, 0);
2221789Sahrens 	if (tail == NULL) {
22224569Smmusante 		int delta = strlen(newname) - strlen(oldname);
22234569Smmusante 
22247046Sahrens 		/* if we're growing, validate child name lengths */
22254569Smmusante 		if (delta > 0)
22264569Smmusante 			err = dmu_objset_find(oldname, dsl_valid_rename,
22274569Smmusante 			    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
22284569Smmusante 
22294569Smmusante 		if (!err)
22304569Smmusante 			err = dsl_dir_rename(dd, newname);
2231789Sahrens 		dsl_dir_close(dd, FTAG);
2232789Sahrens 		return (err);
2233789Sahrens 	}
2234789Sahrens 	if (tail[0] != '@') {
2235789Sahrens 		/* the name ended in a nonexistant component */
2236789Sahrens 		dsl_dir_close(dd, FTAG);
2237789Sahrens 		return (ENOENT);
2238789Sahrens 	}
2239789Sahrens 
22402199Sahrens 	dsl_dir_close(dd, FTAG);
22412199Sahrens 
22422199Sahrens 	/* new name must be snapshot in same filesystem */
22432199Sahrens 	tail = strchr(newname, '@');
22442199Sahrens 	if (tail == NULL)
22452199Sahrens 		return (EINVAL);
22462199Sahrens 	tail++;
22472199Sahrens 	if (strncmp(oldname, newname, tail - newname) != 0)
22482199Sahrens 		return (EXDEV);
2249789Sahrens 
22504007Smmusante 	if (recursive) {
22514007Smmusante 		err = dsl_recursive_rename(oldname, newname);
22524007Smmusante 	} else {
22536689Smaybee 		err = dsl_dataset_hold(oldname, FTAG, &ds);
22544007Smmusante 		if (err)
22554007Smmusante 			return (err);
22562199Sahrens 
22574007Smmusante 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
22584007Smmusante 		    dsl_dataset_snapshot_rename_check,
22594007Smmusante 		    dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
22602199Sahrens 
22616689Smaybee 		dsl_dataset_rele(ds, FTAG);
22624007Smmusante 	}
22632199Sahrens 
2264789Sahrens 	return (err);
2265789Sahrens }
22662082Seschrock 
22677046Sahrens struct promotenode {
22686689Smaybee 	list_node_t link;
22696689Smaybee 	dsl_dataset_t *ds;
22706689Smaybee };
22716689Smaybee 
22722199Sahrens struct promotearg {
22737390SMatthew.Ahrens@Sun.COM 	list_t shared_snaps, origin_snaps, clone_snaps;
22747390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *origin_origin, *origin_head;
22757390SMatthew.Ahrens@Sun.COM 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
22762199Sahrens };
22772199Sahrens 
22787390SMatthew.Ahrens@Sun.COM static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
22797390SMatthew.Ahrens@Sun.COM 
22804543Smarks /* ARGSUSED */
22812082Seschrock static int
22822199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
22832082Seschrock {
22842199Sahrens 	dsl_dataset_t *hds = arg1;
22852199Sahrens 	struct promotearg *pa = arg2;
22867390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap = list_head(&pa->shared_snaps);
22876689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
22886689Smaybee 	int err;
22892199Sahrens 
22907046Sahrens 	/* Check that it is a real clone */
22917046Sahrens 	if (!dsl_dir_is_clone(hds->ds_dir))
22922082Seschrock 		return (EINVAL);
22932082Seschrock 
22942199Sahrens 	/* Since this is so expensive, don't do the preliminary check */
22952199Sahrens 	if (!dmu_tx_is_syncing(tx))
22962199Sahrens 		return (0);
22972199Sahrens 
22986689Smaybee 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)
22996689Smaybee 		return (EXDEV);
23002082Seschrock 
23015367Sahrens 	/* compute origin's new unique space */
23027390SMatthew.Ahrens@Sun.COM 	snap = list_tail(&pa->clone_snaps);
23037390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
23047390SMatthew.Ahrens@Sun.COM 	err = bplist_space_birthrange(&snap->ds->ds_deadlist,
23057390SMatthew.Ahrens@Sun.COM 	    origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, &pa->unique);
23067390SMatthew.Ahrens@Sun.COM 	if (err)
23076689Smaybee 		return (err);
23086689Smaybee 
23096689Smaybee 	/*
23106689Smaybee 	 * Walk the snapshots that we are moving
23116689Smaybee 	 *
23127390SMatthew.Ahrens@Sun.COM 	 * Compute space to transfer.  Consider the incremental changes
23137390SMatthew.Ahrens@Sun.COM 	 * to used for each snapshot:
23147390SMatthew.Ahrens@Sun.COM 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
23157390SMatthew.Ahrens@Sun.COM 	 * So each snapshot gave birth to:
23167390SMatthew.Ahrens@Sun.COM 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
23176689Smaybee 	 * So a sequence would look like:
23187390SMatthew.Ahrens@Sun.COM 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
23196689Smaybee 	 * Which simplifies to:
23207390SMatthew.Ahrens@Sun.COM 	 * uN + kN + kN-1 + ... + k1 + k0
23216689Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
23227390SMatthew.Ahrens@Sun.COM 	 * uN + kN + kN-1 + ... + kM - uM-1
23236689Smaybee 	 */
23246689Smaybee 	pa->used = origin_ds->ds_phys->ds_used_bytes;
23256689Smaybee 	pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
23266689Smaybee 	pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
23277390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(&pa->shared_snaps); snap;
23287390SMatthew.Ahrens@Sun.COM 	    snap = list_next(&pa->shared_snaps, snap)) {
23292082Seschrock 		uint64_t val, dlused, dlcomp, dluncomp;
23306689Smaybee 		dsl_dataset_t *ds = snap->ds;
23312082Seschrock 
23322082Seschrock 		/* Check that the snapshot name does not conflict */
23337390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_get_snapname(ds));
23346689Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
23356689Smaybee 		if (err == 0)
23367390SMatthew.Ahrens@Sun.COM 			return (EEXIST);
23376689Smaybee 		if (err != ENOENT)
23387390SMatthew.Ahrens@Sun.COM 			return (err);
23396689Smaybee 
23406689Smaybee 		/* The very first snapshot does not have a deadlist */
23417390SMatthew.Ahrens@Sun.COM 		if (ds->ds_phys->ds_prev_snap_obj == 0)
23427390SMatthew.Ahrens@Sun.COM 			continue;
23437390SMatthew.Ahrens@Sun.COM 
23447390SMatthew.Ahrens@Sun.COM 		if (err = bplist_space(&ds->ds_deadlist,
23457390SMatthew.Ahrens@Sun.COM 		    &dlused, &dlcomp, &dluncomp))
23467390SMatthew.Ahrens@Sun.COM 			return (err);
23477390SMatthew.Ahrens@Sun.COM 		pa->used += dlused;
23487390SMatthew.Ahrens@Sun.COM 		pa->comp += dlcomp;
23497390SMatthew.Ahrens@Sun.COM 		pa->uncomp += dluncomp;
23507390SMatthew.Ahrens@Sun.COM 	}
23516689Smaybee 
23526689Smaybee 	/*
23536689Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
23546689Smaybee 	 * so we need to subtract out the clone origin's used space.
23556689Smaybee 	 */
23567390SMatthew.Ahrens@Sun.COM 	if (pa->origin_origin) {
23577390SMatthew.Ahrens@Sun.COM 		pa->used -= pa->origin_origin->ds_phys->ds_used_bytes;
23587390SMatthew.Ahrens@Sun.COM 		pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes;
23597390SMatthew.Ahrens@Sun.COM 		pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes;
23602082Seschrock 	}
23612082Seschrock 
23622082Seschrock 	/* Check that there is enough space here */
23637390SMatthew.Ahrens@Sun.COM 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
23647390SMatthew.Ahrens@Sun.COM 	    pa->used);
23657390SMatthew.Ahrens@Sun.COM 	if (err)
23667390SMatthew.Ahrens@Sun.COM 		return (err);
23677390SMatthew.Ahrens@Sun.COM 
23687390SMatthew.Ahrens@Sun.COM 	/*
23697390SMatthew.Ahrens@Sun.COM 	 * Compute the amounts of space that will be used by snapshots
23707390SMatthew.Ahrens@Sun.COM 	 * after the promotion (for both origin and clone).  For each,
23717390SMatthew.Ahrens@Sun.COM 	 * it is the amount of space that will be on all of their
23727390SMatthew.Ahrens@Sun.COM 	 * deadlists (that was not born before their new origin).
23737390SMatthew.Ahrens@Sun.COM 	 */
23747390SMatthew.Ahrens@Sun.COM 	if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
23757390SMatthew.Ahrens@Sun.COM 		uint64_t space;
23767390SMatthew.Ahrens@Sun.COM 
23777390SMatthew.Ahrens@Sun.COM 		/*
23787390SMatthew.Ahrens@Sun.COM 		 * Note, typically this will not be a clone of a clone,
23797390SMatthew.Ahrens@Sun.COM 		 * so snap->ds->ds_origin_txg will be < TXG_INITIAL, so
23807390SMatthew.Ahrens@Sun.COM 		 * these snaplist_space() -> bplist_space_birthrange()
23817390SMatthew.Ahrens@Sun.COM 		 * calls will be fast because they do not have to
23827390SMatthew.Ahrens@Sun.COM 		 * iterate over all bps.
23837390SMatthew.Ahrens@Sun.COM 		 */
23847390SMatthew.Ahrens@Sun.COM 		snap = list_head(&pa->origin_snaps);
23857390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->shared_snaps,
23867390SMatthew.Ahrens@Sun.COM 		    snap->ds->ds_origin_txg, &pa->cloneusedsnap);
23877390SMatthew.Ahrens@Sun.COM 		if (err)
23887390SMatthew.Ahrens@Sun.COM 			return (err);
23897390SMatthew.Ahrens@Sun.COM 
23907390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->clone_snaps,
23917390SMatthew.Ahrens@Sun.COM 		    snap->ds->ds_origin_txg, &space);
23927390SMatthew.Ahrens@Sun.COM 		if (err)
23937390SMatthew.Ahrens@Sun.COM 			return (err);
23947390SMatthew.Ahrens@Sun.COM 		pa->cloneusedsnap += space;
23956689Smaybee 	}
23967390SMatthew.Ahrens@Sun.COM 	if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
23977390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->origin_snaps,
23987390SMatthew.Ahrens@Sun.COM 		    origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap);
23997390SMatthew.Ahrens@Sun.COM 		if (err)
24007390SMatthew.Ahrens@Sun.COM 			return (err);
24017390SMatthew.Ahrens@Sun.COM 	}
24027390SMatthew.Ahrens@Sun.COM 
24037390SMatthew.Ahrens@Sun.COM 	return (0);
24042199Sahrens }
24052082Seschrock 
24062199Sahrens static void
24074543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
24082199Sahrens {
24092199Sahrens 	dsl_dataset_t *hds = arg1;
24102199Sahrens 	struct promotearg *pa = arg2;
24117390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap = list_head(&pa->shared_snaps);
24126689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
24137390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *origin_head;
24142199Sahrens 	dsl_dir_t *dd = hds->ds_dir;
24152199Sahrens 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
24165367Sahrens 	dsl_dir_t *odd = NULL;
24177046Sahrens 	uint64_t oldnext_obj;
24187390SMatthew.Ahrens@Sun.COM 	int64_t delta;
24192199Sahrens 
24202199Sahrens 	ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
24212199Sahrens 
24227390SMatthew.Ahrens@Sun.COM 	snap = list_head(&pa->origin_snaps);
24237390SMatthew.Ahrens@Sun.COM 	origin_head = snap->ds;
24247390SMatthew.Ahrens@Sun.COM 
24252417Sahrens 	/*
24265367Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
24272417Sahrens 	 * changing.
24282417Sahrens 	 */
24295367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object,
24305367Sahrens 	    NULL, FTAG, &odd));
24312082Seschrock 
24326689Smaybee 	/* change origin's next snap */
24336689Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
24347046Sahrens 	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
24357390SMatthew.Ahrens@Sun.COM 	snap = list_tail(&pa->clone_snaps);
24367390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
24377390SMatthew.Ahrens@Sun.COM 	origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
24386689Smaybee 
24397046Sahrens 	/* change the origin's next clone */
24407046Sahrens 	if (origin_ds->ds_phys->ds_next_clones_obj) {
24417046Sahrens 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
24427046Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
24437390SMatthew.Ahrens@Sun.COM 		    origin_ds->ds_phys->ds_next_snap_obj, tx));
24447046Sahrens 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
24457046Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
24467046Sahrens 		    oldnext_obj, tx));
24477046Sahrens 	}
24487046Sahrens 
24496689Smaybee 	/* change origin */
24506689Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
24516689Smaybee 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
24526689Smaybee 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
24537390SMatthew.Ahrens@Sun.COM 	hds->ds_origin_txg = origin_head->ds_origin_txg;
24546689Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
24556689Smaybee 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
24567390SMatthew.Ahrens@Sun.COM 	origin_head->ds_origin_txg = origin_ds->ds_phys->ds_creation_txg;
24576689Smaybee 
24582082Seschrock 	/* move snapshots to this dir */
24597390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(&pa->shared_snaps); snap;
24607390SMatthew.Ahrens@Sun.COM 	    snap = list_next(&pa->shared_snaps, snap)) {
24616689Smaybee 		dsl_dataset_t *ds = snap->ds;
24622082Seschrock 
24637237Sek110237 		/* unregister props as dsl_dir is changing */
24647237Sek110237 		if (ds->ds_user_ptr) {
24657237Sek110237 			ds->ds_user_evict_func(ds, ds->ds_user_ptr);
24667237Sek110237 			ds->ds_user_ptr = NULL;
24677237Sek110237 		}
24682082Seschrock 		/* move snap name entry */
24697390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_get_snapname(ds));
24707390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_snap_remove(origin_head,
24716689Smaybee 		    ds->ds_snapname, tx));
24722199Sahrens 		VERIFY(0 == zap_add(dp->dp_meta_objset,
24732082Seschrock 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
24742082Seschrock 		    8, 1, &ds->ds_object, tx));
24752082Seschrock 		/* change containing dsl_dir */
24762082Seschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
24775367Sahrens 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
24782082Seschrock 		ds->ds_phys->ds_dir_obj = dd->dd_object;
24795367Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
24802082Seschrock 		dsl_dir_close(ds->ds_dir, ds);
24812199Sahrens 		VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
24822082Seschrock 		    NULL, ds, &ds->ds_dir));
24832082Seschrock 
24842082Seschrock 		ASSERT3U(dsl_prop_numcb(ds), ==, 0);
24857390SMatthew.Ahrens@Sun.COM 	}
24867390SMatthew.Ahrens@Sun.COM 
24877390SMatthew.Ahrens@Sun.COM 	/*
24887390SMatthew.Ahrens@Sun.COM 	 * Change space accounting.
24897390SMatthew.Ahrens@Sun.COM 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
24907390SMatthew.Ahrens@Sun.COM 	 * both be valid, or both be 0 (resulting in delta == 0).  This
24917390SMatthew.Ahrens@Sun.COM 	 * is true for each of {clone,origin} independently.
24927390SMatthew.Ahrens@Sun.COM 	 */
24937390SMatthew.Ahrens@Sun.COM 
24947390SMatthew.Ahrens@Sun.COM 	delta = pa->cloneusedsnap -
24957390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
24967390SMatthew.Ahrens@Sun.COM 	ASSERT3S(delta, >=, 0);
24977390SMatthew.Ahrens@Sun.COM 	ASSERT3U(pa->used, >=, delta);
24987390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
24997390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
25007390SMatthew.Ahrens@Sun.COM 	    pa->used - delta, pa->comp, pa->uncomp, tx);
25017390SMatthew.Ahrens@Sun.COM 
25027390SMatthew.Ahrens@Sun.COM 	delta = pa->originusedsnap -
25037390SMatthew.Ahrens@Sun.COM 	    odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
25047390SMatthew.Ahrens@Sun.COM 	ASSERT3S(delta, <=, 0);
25057390SMatthew.Ahrens@Sun.COM 	ASSERT3U(pa->used, >=, -delta);
25067390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
25077390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
25087390SMatthew.Ahrens@Sun.COM 	    -pa->used - delta, -pa->comp, -pa->uncomp, tx);
25097390SMatthew.Ahrens@Sun.COM 
25105367Sahrens 	origin_ds->ds_phys->ds_unique_bytes = pa->unique;
25112082Seschrock 
25124543Smarks 	/* log history record */
25134543Smarks 	spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
25146689Smaybee 	    cr, "dataset = %llu", hds->ds_object);
25154543Smarks 
25165367Sahrens 	dsl_dir_close(odd, FTAG);
25172082Seschrock }
25182082Seschrock 
25197390SMatthew.Ahrens@Sun.COM static char *snaplist_tag = "snaplist";
25207390SMatthew.Ahrens@Sun.COM /*
25217390SMatthew.Ahrens@Sun.COM  * Make a list of dsl_dataset_t's for the snapshots between first_obj
25227390SMatthew.Ahrens@Sun.COM  * (exclusive) and last_obj (inclusive).  The list will be in reverse
25237390SMatthew.Ahrens@Sun.COM  * order (last_obj will be the list_head()).  If first_obj == 0, do all
25247390SMatthew.Ahrens@Sun.COM  * snapshots back to this dataset's origin.
25257390SMatthew.Ahrens@Sun.COM  */
25267390SMatthew.Ahrens@Sun.COM static int
25277390SMatthew.Ahrens@Sun.COM snaplist_make(dsl_pool_t *dp, boolean_t own,
25287390SMatthew.Ahrens@Sun.COM     uint64_t first_obj, uint64_t last_obj, list_t *l)
25297390SMatthew.Ahrens@Sun.COM {
25307390SMatthew.Ahrens@Sun.COM 	uint64_t obj = last_obj;
25317390SMatthew.Ahrens@Sun.COM 
25327390SMatthew.Ahrens@Sun.COM 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock));
25337390SMatthew.Ahrens@Sun.COM 
25347390SMatthew.Ahrens@Sun.COM 	list_create(l, sizeof (struct promotenode),
25357390SMatthew.Ahrens@Sun.COM 	    offsetof(struct promotenode, link));
25367390SMatthew.Ahrens@Sun.COM 
25377390SMatthew.Ahrens@Sun.COM 	while (obj != first_obj) {
25387390SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
25397390SMatthew.Ahrens@Sun.COM 		struct promotenode *snap;
25407390SMatthew.Ahrens@Sun.COM 		int err;
25417390SMatthew.Ahrens@Sun.COM 
25427390SMatthew.Ahrens@Sun.COM 		if (own) {
25437390SMatthew.Ahrens@Sun.COM 			err = dsl_dataset_own_obj(dp, obj,
25447390SMatthew.Ahrens@Sun.COM 			    0, snaplist_tag, &ds);
25457390SMatthew.Ahrens@Sun.COM 			if (err == 0)
25467390SMatthew.Ahrens@Sun.COM 				dsl_dataset_make_exclusive(ds, snaplist_tag);
25477390SMatthew.Ahrens@Sun.COM 		} else {
25487390SMatthew.Ahrens@Sun.COM 			err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds);
25497390SMatthew.Ahrens@Sun.COM 		}
25507390SMatthew.Ahrens@Sun.COM 		if (err == ENOENT) {
25517390SMatthew.Ahrens@Sun.COM 			/* lost race with snapshot destroy */
25527390SMatthew.Ahrens@Sun.COM 			struct promotenode *last = list_tail(l);
25537390SMatthew.Ahrens@Sun.COM 			ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj);
25547390SMatthew.Ahrens@Sun.COM 			obj = last->ds->ds_phys->ds_prev_snap_obj;
25557390SMatthew.Ahrens@Sun.COM 			continue;
25567390SMatthew.Ahrens@Sun.COM 		} else if (err) {
25577390SMatthew.Ahrens@Sun.COM 			return (err);
25587390SMatthew.Ahrens@Sun.COM 		}
25597390SMatthew.Ahrens@Sun.COM 
25607390SMatthew.Ahrens@Sun.COM 		if (first_obj == 0)
25617390SMatthew.Ahrens@Sun.COM 			first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
25627390SMatthew.Ahrens@Sun.COM 
25637390SMatthew.Ahrens@Sun.COM 		snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP);
25647390SMatthew.Ahrens@Sun.COM 		snap->ds = ds;
25657390SMatthew.Ahrens@Sun.COM 		list_insert_tail(l, snap);
25667390SMatthew.Ahrens@Sun.COM 		obj = ds->ds_phys->ds_prev_snap_obj;
25677390SMatthew.Ahrens@Sun.COM 	}
25687390SMatthew.Ahrens@Sun.COM 
25697390SMatthew.Ahrens@Sun.COM 	return (0);
25707390SMatthew.Ahrens@Sun.COM }
25717390SMatthew.Ahrens@Sun.COM 
25727390SMatthew.Ahrens@Sun.COM static int
25737390SMatthew.Ahrens@Sun.COM snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
25747390SMatthew.Ahrens@Sun.COM {
25757390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap;
25767390SMatthew.Ahrens@Sun.COM 
25777390SMatthew.Ahrens@Sun.COM 	*spacep = 0;
25787390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
25797390SMatthew.Ahrens@Sun.COM 		uint64_t used;
25807390SMatthew.Ahrens@Sun.COM 		int err = bplist_space_birthrange(&snap->ds->ds_deadlist,
25817390SMatthew.Ahrens@Sun.COM 		    mintxg, UINT64_MAX, &used);
25827390SMatthew.Ahrens@Sun.COM 		if (err)
25837390SMatthew.Ahrens@Sun.COM 			return (err);
25847390SMatthew.Ahrens@Sun.COM 		*spacep += used;
25857390SMatthew.Ahrens@Sun.COM 	}
25867390SMatthew.Ahrens@Sun.COM 	return (0);
25877390SMatthew.Ahrens@Sun.COM }
25887390SMatthew.Ahrens@Sun.COM 
25897390SMatthew.Ahrens@Sun.COM static void
25907390SMatthew.Ahrens@Sun.COM snaplist_destroy(list_t *l, boolean_t own)
25917390SMatthew.Ahrens@Sun.COM {
25927390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap;
25937390SMatthew.Ahrens@Sun.COM 
2594*8779SMark.Musante@Sun.COM 	if (!l || !list_link_active(&l->list_head))
25957390SMatthew.Ahrens@Sun.COM 		return;
25967390SMatthew.Ahrens@Sun.COM 
25977390SMatthew.Ahrens@Sun.COM 	while ((snap = list_tail(l)) != NULL) {
25987390SMatthew.Ahrens@Sun.COM 		list_remove(l, snap);
25997390SMatthew.Ahrens@Sun.COM 		if (own)
26007390SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(snap->ds, snaplist_tag);
26017390SMatthew.Ahrens@Sun.COM 		else
26027390SMatthew.Ahrens@Sun.COM 			dsl_dataset_rele(snap->ds, snaplist_tag);
26037390SMatthew.Ahrens@Sun.COM 		kmem_free(snap, sizeof (struct promotenode));
26047390SMatthew.Ahrens@Sun.COM 	}
26057390SMatthew.Ahrens@Sun.COM 	list_destroy(l);
26067390SMatthew.Ahrens@Sun.COM }
26077390SMatthew.Ahrens@Sun.COM 
26087390SMatthew.Ahrens@Sun.COM /*
26097390SMatthew.Ahrens@Sun.COM  * Promote a clone.  Nomenclature note:
26107390SMatthew.Ahrens@Sun.COM  * "clone" or "cds": the original clone which is being promoted
26117390SMatthew.Ahrens@Sun.COM  * "origin" or "ods": the snapshot which is originally clone's origin
26127390SMatthew.Ahrens@Sun.COM  * "origin head" or "ohds": the dataset which is the head
26137390SMatthew.Ahrens@Sun.COM  * (filesystem/volume) for the origin
26147390SMatthew.Ahrens@Sun.COM  * "origin origin": the origin of the origin's filesystem (typically
26157390SMatthew.Ahrens@Sun.COM  * NULL, indicating that the clone is not a clone of a clone).
26167390SMatthew.Ahrens@Sun.COM  */
26172082Seschrock int
26182082Seschrock dsl_dataset_promote(const char *name)
26192082Seschrock {
26202082Seschrock 	dsl_dataset_t *ds;
26216689Smaybee 	dsl_dir_t *dd;
26226689Smaybee 	dsl_pool_t *dp;
26232082Seschrock 	dmu_object_info_t doi;
26247390SMatthew.Ahrens@Sun.COM 	struct promotearg pa = { 0 };
26257046Sahrens 	struct promotenode *snap;
26266689Smaybee 	int err;
26276689Smaybee 
26286689Smaybee 	err = dsl_dataset_hold(name, FTAG, &ds);
26292082Seschrock 	if (err)
26302082Seschrock 		return (err);
26316689Smaybee 	dd = ds->ds_dir;
26326689Smaybee 	dp = dd->dd_pool;
26336689Smaybee 
26346689Smaybee 	err = dmu_object_info(dp->dp_meta_objset,
26352082Seschrock 	    ds->ds_phys->ds_snapnames_zapobj, &doi);
26362082Seschrock 	if (err) {
26376689Smaybee 		dsl_dataset_rele(ds, FTAG);
26382082Seschrock 		return (err);
26392082Seschrock 	}
26402082Seschrock 
26417390SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) {
26427390SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
26437390SMatthew.Ahrens@Sun.COM 		return (EINVAL);
26447390SMatthew.Ahrens@Sun.COM 	}
26457390SMatthew.Ahrens@Sun.COM 
26462082Seschrock 	/*
26476689Smaybee 	 * We are going to inherit all the snapshots taken before our
26486689Smaybee 	 * origin (i.e., our new origin will be our parent's origin).
26496689Smaybee 	 * Take ownership of them so that we can rename them into our
26506689Smaybee 	 * namespace.
26516689Smaybee 	 */
26526689Smaybee 	rw_enter(&dp->dp_config_rwlock, RW_READER);
26537390SMatthew.Ahrens@Sun.COM 
26547390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj,
26557390SMatthew.Ahrens@Sun.COM 	    &pa.shared_snaps);
26567390SMatthew.Ahrens@Sun.COM 	if (err != 0)
26577390SMatthew.Ahrens@Sun.COM 		goto out;
26587390SMatthew.Ahrens@Sun.COM 
26597390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps);
26607390SMatthew.Ahrens@Sun.COM 	if (err != 0)
26617390SMatthew.Ahrens@Sun.COM 		goto out;
26627390SMatthew.Ahrens@Sun.COM 
26637390SMatthew.Ahrens@Sun.COM 	snap = list_head(&pa.shared_snaps);
26647390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
26657390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj,
26667390SMatthew.Ahrens@Sun.COM 	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps);
26677390SMatthew.Ahrens@Sun.COM 	if (err != 0)
26687390SMatthew.Ahrens@Sun.COM 		goto out;
26697390SMatthew.Ahrens@Sun.COM 
26707390SMatthew.Ahrens@Sun.COM 	if (dsl_dir_is_clone(snap->ds->ds_dir)) {
26717390SMatthew.Ahrens@Sun.COM 		err = dsl_dataset_own_obj(dp,
26727390SMatthew.Ahrens@Sun.COM 		    snap->ds->ds_dir->dd_phys->dd_origin_obj,
26737390SMatthew.Ahrens@Sun.COM 		    0, FTAG, &pa.origin_origin);
26747390SMatthew.Ahrens@Sun.COM 		if (err != 0)
26756689Smaybee 			goto out;
26766689Smaybee 	}
26777390SMatthew.Ahrens@Sun.COM 
26787390SMatthew.Ahrens@Sun.COM out:
26796689Smaybee 	rw_exit(&dp->dp_config_rwlock);
26806689Smaybee 
26816689Smaybee 	/*
26822082Seschrock 	 * Add in 128x the snapnames zapobj size, since we will be moving
26832082Seschrock 	 * a bunch of snapnames to the promoted ds, and dirtying their
26842082Seschrock 	 * bonus buffers.
26852082Seschrock 	 */
26867390SMatthew.Ahrens@Sun.COM 	if (err == 0) {
26877390SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_do(dp, dsl_dataset_promote_check,
26887390SMatthew.Ahrens@Sun.COM 		    dsl_dataset_promote_sync, ds, &pa,
26897390SMatthew.Ahrens@Sun.COM 		    2 + 2 * doi.doi_physical_blks);
26906689Smaybee 	}
26917390SMatthew.Ahrens@Sun.COM 
26927390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.shared_snaps, B_TRUE);
26937390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.clone_snaps, B_FALSE);
26947390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.origin_snaps, B_FALSE);
26957390SMatthew.Ahrens@Sun.COM 	if (pa.origin_origin)
26967390SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(pa.origin_origin, FTAG);
26976689Smaybee 	dsl_dataset_rele(ds, FTAG);
26982082Seschrock 	return (err);
26992082Seschrock }
27003912Slling 
27015367Sahrens struct cloneswaparg {
27025367Sahrens 	dsl_dataset_t *cds; /* clone dataset */
27035367Sahrens 	dsl_dataset_t *ohds; /* origin's head dataset */
27045367Sahrens 	boolean_t force;
27055481Sck153898 	int64_t unused_refres_delta; /* change in unconsumed refreservation */
27065367Sahrens };
27075326Sek110237 
27085326Sek110237 /* ARGSUSED */
27095326Sek110237 static int
27105326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx)
27115326Sek110237 {
27125367Sahrens 	struct cloneswaparg *csa = arg1;
27135326Sek110237 
27145367Sahrens 	/* they should both be heads */
27155367Sahrens 	if (dsl_dataset_is_snapshot(csa->cds) ||
27165367Sahrens 	    dsl_dataset_is_snapshot(csa->ohds))
27175326Sek110237 		return (EINVAL);
27185326Sek110237 
27195367Sahrens 	/* the branch point should be just before them */
27205367Sahrens 	if (csa->cds->ds_prev != csa->ohds->ds_prev)
27215326Sek110237 		return (EINVAL);
27225326Sek110237 
27235367Sahrens 	/* cds should be the clone */
27245367Sahrens 	if (csa->cds->ds_prev->ds_phys->ds_next_snap_obj !=
27255367Sahrens 	    csa->ohds->ds_object)
27265367Sahrens 		return (EINVAL);
27275326Sek110237 
27285367Sahrens 	/* the clone should be a child of the origin */
27295367Sahrens 	if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir)
27305367Sahrens 		return (EINVAL);
27315326Sek110237 
27325367Sahrens 	/* ohds shouldn't be modified unless 'force' */
27335367Sahrens 	if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds))
27345367Sahrens 		return (ETXTBSY);
27355481Sck153898 
27365481Sck153898 	/* adjust amount of any unconsumed refreservation */
27375481Sck153898 	csa->unused_refres_delta =
27385481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
27395481Sck153898 	    csa->ohds->ds_phys->ds_unique_bytes) -
27405481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
27415481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
27425481Sck153898 
27435481Sck153898 	if (csa->unused_refres_delta > 0 &&
27445481Sck153898 	    csa->unused_refres_delta >
27455481Sck153898 	    dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE))
27465481Sck153898 		return (ENOSPC);
27475481Sck153898 
27485367Sahrens 	return (0);
27495326Sek110237 }
27505326Sek110237 
27515326Sek110237 /* ARGSUSED */
27525326Sek110237 static void
27535326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
27545326Sek110237 {
27555367Sahrens 	struct cloneswaparg *csa = arg1;
27565367Sahrens 	dsl_pool_t *dp = csa->cds->ds_dir->dd_pool;
27575326Sek110237 
27585481Sck153898 	ASSERT(csa->cds->ds_reserved == 0);
27595481Sck153898 	ASSERT(csa->cds->ds_quota == csa->ohds->ds_quota);
27605481Sck153898 
27615367Sahrens 	dmu_buf_will_dirty(csa->cds->ds_dbuf, tx);
27625367Sahrens 	dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx);
27635367Sahrens 	dmu_buf_will_dirty(csa->cds->ds_prev->ds_dbuf, tx);
27645326Sek110237 
27655367Sahrens 	if (csa->cds->ds_user_ptr != NULL) {
27665367Sahrens 		csa->cds->ds_user_evict_func(csa->cds, csa->cds->ds_user_ptr);
27675367Sahrens 		csa->cds->ds_user_ptr = NULL;
27685367Sahrens 	}
27695326Sek110237 
27705367Sahrens 	if (csa->ohds->ds_user_ptr != NULL) {
27715367Sahrens 		csa->ohds->ds_user_evict_func(csa->ohds,
27725367Sahrens 		    csa->ohds->ds_user_ptr);
27735367Sahrens 		csa->ohds->ds_user_ptr = NULL;
27745367Sahrens 	}
27755326Sek110237 
27765326Sek110237 	/* reset origin's unique bytes */
27777390SMatthew.Ahrens@Sun.COM 	VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist,
27787390SMatthew.Ahrens@Sun.COM 	    csa->cds->ds_prev->ds_phys->ds_prev_snap_txg, UINT64_MAX,
27797390SMatthew.Ahrens@Sun.COM 	    &csa->cds->ds_prev->ds_phys->ds_unique_bytes));
27805326Sek110237 
27815326Sek110237 	/* swap blkptrs */
27825326Sek110237 	{
27835326Sek110237 		blkptr_t tmp;
27845367Sahrens 		tmp = csa->ohds->ds_phys->ds_bp;
27855367Sahrens 		csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp;
27865367Sahrens 		csa->cds->ds_phys->ds_bp = tmp;
27875326Sek110237 	}
27885326Sek110237 
27895326Sek110237 	/* set dd_*_bytes */
27905326Sek110237 	{
27915326Sek110237 		int64_t dused, dcomp, duncomp;
27925326Sek110237 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
27935326Sek110237 		uint64_t odl_used, odl_comp, odl_uncomp;
27945326Sek110237 
27957390SMatthew.Ahrens@Sun.COM 		ASSERT3U(csa->cds->ds_dir->dd_phys->
27967390SMatthew.Ahrens@Sun.COM 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
27977390SMatthew.Ahrens@Sun.COM 
27985367Sahrens 		VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used,
27995326Sek110237 		    &cdl_comp, &cdl_uncomp));
28005367Sahrens 		VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used,
28015326Sek110237 		    &odl_comp, &odl_uncomp));
28027390SMatthew.Ahrens@Sun.COM 
28035367Sahrens 		dused = csa->cds->ds_phys->ds_used_bytes + cdl_used -
28045367Sahrens 		    (csa->ohds->ds_phys->ds_used_bytes + odl_used);
28055367Sahrens 		dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
28065367Sahrens 		    (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
28075367Sahrens 		duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
28085367Sahrens 		    cdl_uncomp -
28095367Sahrens 		    (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
28105326Sek110237 
28117390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD,
28125367Sahrens 		    dused, dcomp, duncomp, tx);
28137390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD,
28145367Sahrens 		    -dused, -dcomp, -duncomp, tx);
28157390SMatthew.Ahrens@Sun.COM 
28167390SMatthew.Ahrens@Sun.COM 		/*
28177390SMatthew.Ahrens@Sun.COM 		 * The difference in the space used by snapshots is the
28187390SMatthew.Ahrens@Sun.COM 		 * difference in snapshot space due to the head's
28197390SMatthew.Ahrens@Sun.COM 		 * deadlist (since that's the only thing that's
28207390SMatthew.Ahrens@Sun.COM 		 * changing that affects the snapused).
28217390SMatthew.Ahrens@Sun.COM 		 */
28227390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist,
28237390SMatthew.Ahrens@Sun.COM 		    csa->ohds->ds_origin_txg, UINT64_MAX, &cdl_used));
28247390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->ohds->ds_deadlist,
28257390SMatthew.Ahrens@Sun.COM 		    csa->ohds->ds_origin_txg, UINT64_MAX, &odl_used));
28267390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used,
28277390SMatthew.Ahrens@Sun.COM 		    DD_USED_HEAD, DD_USED_SNAP, tx);
28285367Sahrens 	}
28295367Sahrens 
28305367Sahrens #define	SWITCH64(x, y) \
28315367Sahrens 	{ \
28325367Sahrens 		uint64_t __tmp = (x); \
28335367Sahrens 		(x) = (y); \
28345367Sahrens 		(y) = __tmp; \
28355326Sek110237 	}
28365326Sek110237 
28375326Sek110237 	/* swap ds_*_bytes */
28385367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_used_bytes,
28395367Sahrens 	    csa->cds->ds_phys->ds_used_bytes);
28405367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
28415367Sahrens 	    csa->cds->ds_phys->ds_compressed_bytes);
28425367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
28435367Sahrens 	    csa->cds->ds_phys->ds_uncompressed_bytes);
28445481Sck153898 	SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
28455481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
28465481Sck153898 
28475481Sck153898 	/* apply any parent delta for change in unconsumed refreservation */
28487390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV,
28497390SMatthew.Ahrens@Sun.COM 	    csa->unused_refres_delta, 0, 0, tx);
28505326Sek110237 
28515326Sek110237 	/* swap deadlists */
28525367Sahrens 	bplist_close(&csa->cds->ds_deadlist);
28535367Sahrens 	bplist_close(&csa->ohds->ds_deadlist);
28545367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
28555367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj);
28565367Sahrens 	VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
28575367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj));
28585367Sahrens 	VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
28595367Sahrens 	    csa->ohds->ds_phys->ds_deadlist_obj));
28607837SMatthew.Ahrens@Sun.COM 
28617837SMatthew.Ahrens@Sun.COM 	dsl_pool_ds_clone_swapped(csa->ohds, csa->cds, tx);
28625326Sek110237 }
28635326Sek110237 
28645326Sek110237 /*
28656689Smaybee  * Swap 'clone' with its origin head file system.  Used at the end
28666689Smaybee  * of "online recv" to swizzle the file system to the new version.
28675326Sek110237  */
28685326Sek110237 int
28695367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
28705367Sahrens     boolean_t force)
28715326Sek110237 {
28725367Sahrens 	struct cloneswaparg csa;
28736689Smaybee 	int error;
28746689Smaybee 
28756689Smaybee 	ASSERT(clone->ds_owner);
28766689Smaybee 	ASSERT(origin_head->ds_owner);
28776689Smaybee retry:
28786689Smaybee 	/* Need exclusive access for the swap */
28796689Smaybee 	rw_enter(&clone->ds_rwlock, RW_WRITER);
28806689Smaybee 	if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) {
28816689Smaybee 		rw_exit(&clone->ds_rwlock);
28826689Smaybee 		rw_enter(&origin_head->ds_rwlock, RW_WRITER);
28836689Smaybee 		if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) {
28846689Smaybee 			rw_exit(&origin_head->ds_rwlock);
28856689Smaybee 			goto retry;
28866689Smaybee 		}
28876689Smaybee 	}
28885367Sahrens 	csa.cds = clone;
28895367Sahrens 	csa.ohds = origin_head;
28905367Sahrens 	csa.force = force;
28916689Smaybee 	error = dsl_sync_task_do(clone->ds_dir->dd_pool,
28925326Sek110237 	    dsl_dataset_clone_swap_check,
28936689Smaybee 	    dsl_dataset_clone_swap_sync, &csa, NULL, 9);
28946689Smaybee 	return (error);
28955326Sek110237 }
28965326Sek110237 
28973912Slling /*
28983912Slling  * Given a pool name and a dataset object number in that pool,
28993912Slling  * return the name of that dataset.
29003912Slling  */
29013912Slling int
29023912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
29033912Slling {
29043912Slling 	spa_t *spa;
29053912Slling 	dsl_pool_t *dp;
29066689Smaybee 	dsl_dataset_t *ds;
29073912Slling 	int error;
29083912Slling 
29093912Slling 	if ((error = spa_open(pname, &spa, FTAG)) != 0)
29103912Slling 		return (error);
29113912Slling 	dp = spa_get_dsl(spa);
29123912Slling 	rw_enter(&dp->dp_config_rwlock, RW_READER);
29136689Smaybee 	if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) {
29146689Smaybee 		dsl_dataset_name(ds, buf);
29156689Smaybee 		dsl_dataset_rele(ds, FTAG);
29163912Slling 	}
29173912Slling 	rw_exit(&dp->dp_config_rwlock);
29183912Slling 	spa_close(spa, FTAG);
29193912Slling 
29206689Smaybee 	return (error);
29213912Slling }
29225378Sck153898 
29235378Sck153898 int
29245378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
29256689Smaybee     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
29265378Sck153898 {
29275378Sck153898 	int error = 0;
29285378Sck153898 
29295378Sck153898 	ASSERT3S(asize, >, 0);
29305378Sck153898 
29315831Sck153898 	/*
29325831Sck153898 	 * *ref_rsrv is the portion of asize that will come from any
29335831Sck153898 	 * unconsumed refreservation space.
29345831Sck153898 	 */
29355831Sck153898 	*ref_rsrv = 0;
29365831Sck153898 
29375378Sck153898 	mutex_enter(&ds->ds_lock);
29385378Sck153898 	/*
29395378Sck153898 	 * Make a space adjustment for reserved bytes.
29405378Sck153898 	 */
29415378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
29425378Sck153898 		ASSERT3U(*used, >=,
29435378Sck153898 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
29445378Sck153898 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
29455831Sck153898 		*ref_rsrv =
29465831Sck153898 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
29475378Sck153898 	}
29485378Sck153898 
29495378Sck153898 	if (!check_quota || ds->ds_quota == 0) {
29505378Sck153898 		mutex_exit(&ds->ds_lock);
29515378Sck153898 		return (0);
29525378Sck153898 	}
29535378Sck153898 	/*
29545378Sck153898 	 * If they are requesting more space, and our current estimate
29555378Sck153898 	 * is over quota, they get to try again unless the actual
29565378Sck153898 	 * on-disk is over quota and there are no pending changes (which
29575378Sck153898 	 * may free up space for us).
29585378Sck153898 	 */
29595378Sck153898 	if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) {
29605378Sck153898 		if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota)
29615378Sck153898 			error = ERESTART;
29625378Sck153898 		else
29635378Sck153898 			error = EDQUOT;
29645378Sck153898 	}
29655378Sck153898 	mutex_exit(&ds->ds_lock);
29665378Sck153898 
29675378Sck153898 	return (error);
29685378Sck153898 }
29695378Sck153898 
29705378Sck153898 /* ARGSUSED */
29715378Sck153898 static int
29725378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
29735378Sck153898 {
29745378Sck153898 	dsl_dataset_t *ds = arg1;
29755378Sck153898 	uint64_t *quotap = arg2;
29765378Sck153898 	uint64_t new_quota = *quotap;
29775378Sck153898 
29785378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA)
29795378Sck153898 		return (ENOTSUP);
29805378Sck153898 
29815378Sck153898 	if (new_quota == 0)
29825378Sck153898 		return (0);
29835378Sck153898 
29845378Sck153898 	if (new_quota < ds->ds_phys->ds_used_bytes ||
29855378Sck153898 	    new_quota < ds->ds_reserved)
29865378Sck153898 		return (ENOSPC);
29875378Sck153898 
29885378Sck153898 	return (0);
29895378Sck153898 }
29905378Sck153898 
29915378Sck153898 /* ARGSUSED */
29925378Sck153898 void
29935378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
29945378Sck153898 {
29955378Sck153898 	dsl_dataset_t *ds = arg1;
29965378Sck153898 	uint64_t *quotap = arg2;
29975378Sck153898 	uint64_t new_quota = *quotap;
29985378Sck153898 
29995378Sck153898 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
30005378Sck153898 
30015378Sck153898 	ds->ds_quota = new_quota;
30025378Sck153898 
30035378Sck153898 	dsl_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx);
30045378Sck153898 
30055378Sck153898 	spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa,
30065378Sck153898 	    tx, cr, "%lld dataset = %llu ",
30076689Smaybee 	    (longlong_t)new_quota, ds->ds_object);
30085378Sck153898 }
30095378Sck153898 
30105378Sck153898 int
30115378Sck153898 dsl_dataset_set_quota(const char *dsname, uint64_t quota)
30125378Sck153898 {
30135378Sck153898 	dsl_dataset_t *ds;
30145378Sck153898 	int err;
30155378Sck153898 
30166689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
30175378Sck153898 	if (err)
30185378Sck153898 		return (err);
30195378Sck153898 
30205481Sck153898 	if (quota != ds->ds_quota) {
30215481Sck153898 		/*
30225481Sck153898 		 * If someone removes a file, then tries to set the quota, we
30235481Sck153898 		 * want to make sure the file freeing takes effect.
30245481Sck153898 		 */
30255481Sck153898 		txg_wait_open(ds->ds_dir->dd_pool, 0);
30265481Sck153898 
30275481Sck153898 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
30285481Sck153898 		    dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync,
30295481Sck153898 		    ds, &quota, 0);
30305481Sck153898 	}
30316689Smaybee 	dsl_dataset_rele(ds, FTAG);
30325378Sck153898 	return (err);
30335378Sck153898 }
30345378Sck153898 
30355378Sck153898 static int
30365378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
30375378Sck153898 {
30385378Sck153898 	dsl_dataset_t *ds = arg1;
30395378Sck153898 	uint64_t *reservationp = arg2;
30405378Sck153898 	uint64_t new_reservation = *reservationp;
30415378Sck153898 	uint64_t unique;
30425378Sck153898 
30435378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
30445378Sck153898 	    SPA_VERSION_REFRESERVATION)
30455378Sck153898 		return (ENOTSUP);
30465378Sck153898 
30475378Sck153898 	if (dsl_dataset_is_snapshot(ds))
30485378Sck153898 		return (EINVAL);
30495378Sck153898 
30505378Sck153898 	/*
30515378Sck153898 	 * If we are doing the preliminary check in open context, the
30525378Sck153898 	 * space estimates may be inaccurate.
30535378Sck153898 	 */
30545378Sck153898 	if (!dmu_tx_is_syncing(tx))
30555378Sck153898 		return (0);
30565378Sck153898 
30575378Sck153898 	mutex_enter(&ds->ds_lock);
30585378Sck153898 	unique = dsl_dataset_unique(ds);
30595378Sck153898 	mutex_exit(&ds->ds_lock);
30605378Sck153898 
30618525SEric.Schrock@Sun.COM 	if (MAX(unique, new_reservation) > MAX(unique, ds->ds_reserved)) {
30628525SEric.Schrock@Sun.COM 		uint64_t delta = MAX(unique, new_reservation) -
30638525SEric.Schrock@Sun.COM 		    MAX(unique, ds->ds_reserved);
30648525SEric.Schrock@Sun.COM 
30658525SEric.Schrock@Sun.COM 		if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
30668525SEric.Schrock@Sun.COM 			return (ENOSPC);
30678525SEric.Schrock@Sun.COM 		if (ds->ds_quota > 0 &&
30688525SEric.Schrock@Sun.COM 		    new_reservation > ds->ds_quota)
30698525SEric.Schrock@Sun.COM 			return (ENOSPC);
30708525SEric.Schrock@Sun.COM 	}
30715378Sck153898 
30725378Sck153898 	return (0);
30735378Sck153898 }
30745378Sck153898 
30755378Sck153898 /* ARGSUSED */
30765378Sck153898 static void
30775378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr,
30785378Sck153898     dmu_tx_t *tx)
30795378Sck153898 {
30805378Sck153898 	dsl_dataset_t *ds = arg1;
30815378Sck153898 	uint64_t *reservationp = arg2;
30825378Sck153898 	uint64_t new_reservation = *reservationp;
30837595SMatthew.Ahrens@Sun.COM 	uint64_t unique;
30847595SMatthew.Ahrens@Sun.COM 	int64_t delta;
30857595SMatthew.Ahrens@Sun.COM 
30867595SMatthew.Ahrens@Sun.COM 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
30877595SMatthew.Ahrens@Sun.COM 
30887595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_dir->dd_lock);
30897595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_lock);
30907595SMatthew.Ahrens@Sun.COM 	unique = dsl_dataset_unique(ds);
30917595SMatthew.Ahrens@Sun.COM 	delta = MAX(0, (int64_t)(new_reservation - unique)) -
30927595SMatthew.Ahrens@Sun.COM 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
30937595SMatthew.Ahrens@Sun.COM 	ds->ds_reserved = new_reservation;
30947595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_lock);
30957595SMatthew.Ahrens@Sun.COM 
30967595SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
30977595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_dir->dd_lock);
30987595SMatthew.Ahrens@Sun.COM 	dsl_prop_set_uint64_sync(ds->ds_dir, "refreservation",
30997595SMatthew.Ahrens@Sun.COM 	    new_reservation, cr, tx);
31007595SMatthew.Ahrens@Sun.COM 
31017595SMatthew.Ahrens@Sun.COM 	spa_history_internal_log(LOG_DS_REFRESERV,
31027595SMatthew.Ahrens@Sun.COM 	    ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu",
31037595SMatthew.Ahrens@Sun.COM 	    (longlong_t)new_reservation, ds->ds_object);
31045378Sck153898 }
31055378Sck153898 
31065378Sck153898 int
31075378Sck153898 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation)
31085378Sck153898 {
31095378Sck153898 	dsl_dataset_t *ds;
31105378Sck153898 	int err;
31115378Sck153898 
31126689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
31135378Sck153898 	if (err)
31145378Sck153898 		return (err);
31155378Sck153898 
31165378Sck153898 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
31175378Sck153898 	    dsl_dataset_set_reservation_check,
31185378Sck153898 	    dsl_dataset_set_reservation_sync, ds, &reservation, 0);
31196689Smaybee 	dsl_dataset_rele(ds, FTAG);
31205378Sck153898 	return (err);
31215378Sck153898 }
3122