xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision 7754:b80e4842ad54)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
225831Sck153898  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/dmu_objset.h>
27789Sahrens #include <sys/dsl_dataset.h>
28789Sahrens #include <sys/dsl_dir.h>
292082Seschrock #include <sys/dsl_prop.h>
302199Sahrens #include <sys/dsl_synctask.h>
31789Sahrens #include <sys/dmu_traverse.h>
32789Sahrens #include <sys/dmu_tx.h>
33789Sahrens #include <sys/arc.h>
34789Sahrens #include <sys/zio.h>
35789Sahrens #include <sys/zap.h>
36789Sahrens #include <sys/unique.h>
37789Sahrens #include <sys/zfs_context.h>
384007Smmusante #include <sys/zfs_ioctl.h>
394543Smarks #include <sys/spa.h>
407046Sahrens #include <sys/zfs_znode.h>
414543Smarks #include <sys/sunddi.h>
42789Sahrens 
436689Smaybee static char *dsl_reaper = "the grim reaper";
446689Smaybee 
452199Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check;
462199Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync;
472199Sahrens static dsl_checkfunc_t dsl_dataset_rollback_check;
482199Sahrens static dsl_syncfunc_t dsl_dataset_rollback_sync;
495378Sck153898 static dsl_syncfunc_t dsl_dataset_set_reservation_sync;
501731Sbonwick 
513444Sek110237 #define	DS_REF_MAX	(1ULL << 62)
52789Sahrens 
53789Sahrens #define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
54789Sahrens 
556689Smaybee #define	DSL_DATASET_IS_DESTROYED(ds)	((ds)->ds_owner == dsl_reaper)
566689Smaybee 
57789Sahrens 
585378Sck153898 /*
595378Sck153898  * Figure out how much of this delta should be propogated to the dsl_dir
605378Sck153898  * layer.  If there's a refreservation, that space has already been
615378Sck153898  * partially accounted for in our ancestors.
625378Sck153898  */
635378Sck153898 static int64_t
645378Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta)
655378Sck153898 {
665378Sck153898 	uint64_t old_bytes, new_bytes;
675378Sck153898 
685378Sck153898 	if (ds->ds_reserved == 0)
695378Sck153898 		return (delta);
705378Sck153898 
715378Sck153898 	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
725378Sck153898 	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
735378Sck153898 
745378Sck153898 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
755378Sck153898 	return (new_bytes - old_bytes);
765378Sck153898 }
77789Sahrens 
78789Sahrens void
79789Sahrens dsl_dataset_block_born(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
80789Sahrens {
812082Seschrock 	int used = bp_get_dasize(tx->tx_pool->dp_spa, bp);
82789Sahrens 	int compressed = BP_GET_PSIZE(bp);
83789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
845378Sck153898 	int64_t delta;
85789Sahrens 
86789Sahrens 	dprintf_bp(bp, "born, ds=%p\n", ds);
87789Sahrens 
88789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
89789Sahrens 	/* It could have been compressed away to nothing */
90789Sahrens 	if (BP_IS_HOLE(bp))
91789Sahrens 		return;
92789Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
93789Sahrens 	ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES);
94789Sahrens 	if (ds == NULL) {
95789Sahrens 		/*
96789Sahrens 		 * Account for the meta-objset space in its placeholder
97789Sahrens 		 * dsl_dir.
98789Sahrens 		 */
99789Sahrens 		ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */
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 
129*7754SJeff.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,
143*7754SJeff.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,
162*7754SJeff.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);
5516689Smaybee 		return (EBUSY);
5526689Smaybee 	}
5536689Smaybee 	return (0);
5546689Smaybee }
5556689Smaybee 
5566689Smaybee int
5576689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp)
558789Sahrens {
559789Sahrens 	dsl_dir_t *dd;
560789Sahrens 	dsl_pool_t *dp;
5616689Smaybee 	const char *snapname;
562789Sahrens 	uint64_t obj;
563789Sahrens 	int err = 0;
564789Sahrens 
5656689Smaybee 	err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname);
5661544Seschrock 	if (err)
5671544Seschrock 		return (err);
568789Sahrens 
569789Sahrens 	dp = dd->dd_pool;
570789Sahrens 	obj = dd->dd_phys->dd_head_dataset_obj;
571789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
5726689Smaybee 	if (obj)
5736689Smaybee 		err = dsl_dataset_get_ref(dp, obj, tag, dsp);
5746689Smaybee 	else
575789Sahrens 		err = ENOENT;
5766689Smaybee 	if (err)
577789Sahrens 		goto out;
5786689Smaybee 
5796689Smaybee 	err = dsl_dataset_hold_ref(*dsp, tag);
5806689Smaybee 
5816689Smaybee 	/* we may be looking for a snapshot */
5826689Smaybee 	if (err == 0 && snapname != NULL) {
5836689Smaybee 		dsl_dataset_t *ds = NULL;
5846689Smaybee 
5856689Smaybee 		if (*snapname++ != '@') {
5866689Smaybee 			dsl_dataset_rele(*dsp, tag);
587789Sahrens 			err = ENOENT;
588789Sahrens 			goto out;
589789Sahrens 		}
5906689Smaybee 
5916689Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
5926689Smaybee 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
5936689Smaybee 		if (err == 0)
5946689Smaybee 			err = dsl_dataset_get_ref(dp, obj, tag, &ds);
5956689Smaybee 		dsl_dataset_rele(*dsp, tag);
5966689Smaybee 
5976689Smaybee 		ASSERT3U((err == 0), ==, (ds != NULL));
5986689Smaybee 
5996689Smaybee 		if (ds) {
6006689Smaybee 			mutex_enter(&ds->ds_lock);
6016689Smaybee 			if (ds->ds_snapname[0] == 0)
6026689Smaybee 				(void) strlcpy(ds->ds_snapname, snapname,
6036689Smaybee 				    sizeof (ds->ds_snapname));
6046689Smaybee 			mutex_exit(&ds->ds_lock);
6056689Smaybee 			err = dsl_dataset_hold_ref(ds, tag);
6066689Smaybee 			*dsp = err ? NULL : ds;
607789Sahrens 		}
608789Sahrens 	}
609789Sahrens out:
610789Sahrens 	rw_exit(&dp->dp_config_rwlock);
611789Sahrens 	dsl_dir_close(dd, FTAG);
612789Sahrens 	return (err);
613789Sahrens }
614789Sahrens 
615789Sahrens int
6166689Smaybee dsl_dataset_own(const char *name, int flags, void *owner, dsl_dataset_t **dsp)
617789Sahrens {
6186689Smaybee 	int err = dsl_dataset_hold(name, owner, dsp);
6196689Smaybee 	if (err)
6206689Smaybee 		return (err);
6216689Smaybee 	if ((*dsp)->ds_phys->ds_num_children > 0 &&
6226689Smaybee 	    !DS_MODE_IS_READONLY(flags)) {
6236689Smaybee 		dsl_dataset_rele(*dsp, owner);
6246689Smaybee 		return (EROFS);
6256689Smaybee 	}
6266689Smaybee 	if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) {
6276689Smaybee 		dsl_dataset_rele(*dsp, owner);
6286689Smaybee 		return (EBUSY);
6296689Smaybee 	}
6306689Smaybee 	return (0);
631789Sahrens }
632789Sahrens 
633789Sahrens void
634789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
635789Sahrens {
636789Sahrens 	if (ds == NULL) {
637789Sahrens 		(void) strcpy(name, "mos");
638789Sahrens 	} else {
639789Sahrens 		dsl_dir_name(ds->ds_dir, name);
6401544Seschrock 		VERIFY(0 == dsl_dataset_get_snapname(ds));
641789Sahrens 		if (ds->ds_snapname[0]) {
642789Sahrens 			(void) strcat(name, "@");
6436689Smaybee 			/*
6446689Smaybee 			 * We use a "recursive" mutex so that we
6456689Smaybee 			 * can call dprintf_ds() with ds_lock held.
6466689Smaybee 			 */
647789Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
648789Sahrens 				mutex_enter(&ds->ds_lock);
649789Sahrens 				(void) strcat(name, ds->ds_snapname);
650789Sahrens 				mutex_exit(&ds->ds_lock);
651789Sahrens 			} else {
652789Sahrens 				(void) strcat(name, ds->ds_snapname);
653789Sahrens 			}
654789Sahrens 		}
655789Sahrens 	}
656789Sahrens }
657789Sahrens 
6583978Smmusante static int
6593978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds)
6603978Smmusante {
6613978Smmusante 	int result;
6623978Smmusante 
6633978Smmusante 	if (ds == NULL) {
6643978Smmusante 		result = 3;	/* "mos" */
6653978Smmusante 	} else {
6663978Smmusante 		result = dsl_dir_namelen(ds->ds_dir);
6673978Smmusante 		VERIFY(0 == dsl_dataset_get_snapname(ds));
6683978Smmusante 		if (ds->ds_snapname[0]) {
6693978Smmusante 			++result;	/* adding one for the @-sign */
6703978Smmusante 			if (!MUTEX_HELD(&ds->ds_lock)) {
6713978Smmusante 				mutex_enter(&ds->ds_lock);
6723978Smmusante 				result += strlen(ds->ds_snapname);
6733978Smmusante 				mutex_exit(&ds->ds_lock);
6743978Smmusante 			} else {
6753978Smmusante 				result += strlen(ds->ds_snapname);
6763978Smmusante 			}
6773978Smmusante 		}
6783978Smmusante 	}
6793978Smmusante 
6803978Smmusante 	return (result);
6813978Smmusante }
6823978Smmusante 
6837046Sahrens void
6846689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag)
685789Sahrens {
6861544Seschrock 	dmu_buf_rele(ds->ds_dbuf, tag);
687789Sahrens }
688789Sahrens 
689789Sahrens void
6906689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
6915367Sahrens {
6926689Smaybee 	if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) {
6936689Smaybee 		rw_exit(&ds->ds_rwlock);
6946689Smaybee 	}
6956689Smaybee 	dsl_dataset_drop_ref(ds, tag);
6966689Smaybee }
6976689Smaybee 
6986689Smaybee void
6996689Smaybee dsl_dataset_disown(dsl_dataset_t *ds, void *owner)
7006689Smaybee {
7016689Smaybee 	ASSERT((ds->ds_owner == owner && ds->ds_dbuf) ||
7026689Smaybee 	    (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL));
7036689Smaybee 
7045367Sahrens 	mutex_enter(&ds->ds_lock);
7056689Smaybee 	ds->ds_owner = NULL;
7066689Smaybee 	if (RW_WRITE_HELD(&ds->ds_rwlock)) {
7076689Smaybee 		rw_exit(&ds->ds_rwlock);
7086689Smaybee 		cv_broadcast(&ds->ds_exclusive_cv);
7096689Smaybee 	}
7105367Sahrens 	mutex_exit(&ds->ds_lock);
7116689Smaybee 	if (ds->ds_dbuf)
7126689Smaybee 		dsl_dataset_drop_ref(ds, owner);
7136689Smaybee 	else
7146689Smaybee 		dsl_dataset_evict(ds->ds_dbuf, ds);
7155367Sahrens }
7165367Sahrens 
7175367Sahrens boolean_t
7186689Smaybee dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *owner)
7195367Sahrens {
7206689Smaybee 	boolean_t gotit = FALSE;
7216689Smaybee 
7225367Sahrens 	mutex_enter(&ds->ds_lock);
7236689Smaybee 	if (ds->ds_owner == NULL &&
7246689Smaybee 	    (!DS_IS_INCONSISTENT(ds) || inconsistentok)) {
7256689Smaybee 		ds->ds_owner = owner;
7266689Smaybee 		if (!dsl_pool_sync_context(ds->ds_dir->dd_pool))
7276689Smaybee 			rw_exit(&ds->ds_rwlock);
7286689Smaybee 		gotit = TRUE;
7295367Sahrens 	}
7305367Sahrens 	mutex_exit(&ds->ds_lock);
7316689Smaybee 	return (gotit);
7326689Smaybee }
7336689Smaybee 
7346689Smaybee void
7356689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner)
7366689Smaybee {
7376689Smaybee 	ASSERT3P(owner, ==, ds->ds_owner);
7386689Smaybee 	if (!RW_WRITE_HELD(&ds->ds_rwlock))
7396689Smaybee 		rw_enter(&ds->ds_rwlock, RW_WRITER);
7405367Sahrens }
7415367Sahrens 
7422199Sahrens uint64_t
7437046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
7446492Stimh     uint64_t flags, dmu_tx_t *tx)
745789Sahrens {
7465367Sahrens 	dsl_pool_t *dp = dd->dd_pool;
747789Sahrens 	dmu_buf_t *dbuf;
748789Sahrens 	dsl_dataset_phys_t *dsphys;
7495367Sahrens 	uint64_t dsobj;
750789Sahrens 	objset_t *mos = dp->dp_meta_objset;
751789Sahrens 
7527046Sahrens 	if (origin == NULL)
7537046Sahrens 		origin = dp->dp_origin_snap;
7547046Sahrens 
7555367Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
7565367Sahrens 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
757789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7585367Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
759789Sahrens 
760928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
761928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
7621544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
763789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
764789Sahrens 	dsphys = dbuf->db_data;
7656689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
766789Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
7676492Stimh 	dsphys->ds_flags = flags;
768789Sahrens 	dsphys->ds_fsid_guid = unique_create();
769789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
770789Sahrens 	    sizeof (dsphys->ds_guid));
771789Sahrens 	dsphys->ds_snapnames_zapobj =
7726492Stimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
7736492Stimh 	    DMU_OT_NONE, 0, tx);
774789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
7757046Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
776789Sahrens 	dsphys->ds_deadlist_obj =
777789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
7785378Sck153898 
7795367Sahrens 	if (origin) {
7805367Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
781789Sahrens 		dsphys->ds_prev_snap_txg =
7825367Sahrens 		    origin->ds_phys->ds_creation_txg;
783789Sahrens 		dsphys->ds_used_bytes =
7845367Sahrens 		    origin->ds_phys->ds_used_bytes;
785789Sahrens 		dsphys->ds_compressed_bytes =
7865367Sahrens 		    origin->ds_phys->ds_compressed_bytes;
787789Sahrens 		dsphys->ds_uncompressed_bytes =
7885367Sahrens 		    origin->ds_phys->ds_uncompressed_bytes;
7895367Sahrens 		dsphys->ds_bp = origin->ds_phys->ds_bp;
7906502Stimh 		dsphys->ds_flags |= origin->ds_phys->ds_flags;
791789Sahrens 
7925367Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
7935367Sahrens 		origin->ds_phys->ds_num_children++;
794789Sahrens 
7957046Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
7967046Sahrens 			if (origin->ds_phys->ds_next_clones_obj == 0) {
7977046Sahrens 				origin->ds_phys->ds_next_clones_obj =
7987046Sahrens 				    zap_create(mos,
7997046Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
8007046Sahrens 			}
8017046Sahrens 			VERIFY(0 == zap_add_int(mos,
8027046Sahrens 			    origin->ds_phys->ds_next_clones_obj,
8037046Sahrens 			    dsobj, tx));
8047046Sahrens 		}
8057046Sahrens 
806789Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
8075367Sahrens 		dd->dd_phys->dd_origin_obj = origin->ds_object;
808789Sahrens 	}
8096492Stimh 
8106492Stimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
8116492Stimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
8126492Stimh 
8131544Seschrock 	dmu_buf_rele(dbuf, FTAG);
814789Sahrens 
815789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
816789Sahrens 	dd->dd_phys->dd_head_dataset_obj = dsobj;
8175367Sahrens 
8185367Sahrens 	return (dsobj);
8195367Sahrens }
8205367Sahrens 
8215367Sahrens uint64_t
8226492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
8236492Stimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
8245367Sahrens {
8255367Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
8265367Sahrens 	uint64_t dsobj, ddobj;
8275367Sahrens 	dsl_dir_t *dd;
8285367Sahrens 
8295367Sahrens 	ASSERT(lastname[0] != '@');
8305367Sahrens 
8317046Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
8325367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
8335367Sahrens 
8347046Sahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx);
8355367Sahrens 
8365367Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
8375367Sahrens 
838789Sahrens 	dsl_dir_close(dd, FTAG);
839789Sahrens 
8402199Sahrens 	return (dsobj);
8412199Sahrens }
8422199Sahrens 
8432199Sahrens struct destroyarg {
8442199Sahrens 	dsl_sync_task_group_t *dstg;
8452199Sahrens 	char *snapname;
8462199Sahrens 	char *failed;
8472199Sahrens };
8482199Sahrens 
8492199Sahrens static int
8502199Sahrens dsl_snapshot_destroy_one(char *name, void *arg)
8512199Sahrens {
8522199Sahrens 	struct destroyarg *da = arg;
8532199Sahrens 	dsl_dataset_t *ds;
8542199Sahrens 	char *cp;
8552199Sahrens 	int err;
8562199Sahrens 
8572199Sahrens 	(void) strcat(name, "@");
8582199Sahrens 	(void) strcat(name, da->snapname);
8596689Smaybee 	err = dsl_dataset_own(name, DS_MODE_READONLY | DS_MODE_INCONSISTENT,
8604007Smmusante 	    da->dstg, &ds);
8612199Sahrens 	cp = strchr(name, '@');
8622199Sahrens 	*cp = '\0';
8636689Smaybee 	if (err == 0) {
8646689Smaybee 		dsl_dataset_make_exclusive(ds, da->dstg);
8657237Sek110237 		if (ds->ds_user_ptr) {
8667237Sek110237 			ds->ds_user_evict_func(ds, ds->ds_user_ptr);
8677237Sek110237 			ds->ds_user_ptr = NULL;
8687237Sek110237 		}
8696689Smaybee 		dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check,
8706689Smaybee 		    dsl_dataset_destroy_sync, ds, da->dstg, 0);
8716689Smaybee 	} else if (err == ENOENT) {
8726689Smaybee 		err = 0;
8736689Smaybee 	} else {
8742199Sahrens 		(void) strcpy(da->failed, name);
8752199Sahrens 	}
8766689Smaybee 	return (err);
877789Sahrens }
878789Sahrens 
8792199Sahrens /*
8802199Sahrens  * Destroy 'snapname' in all descendants of 'fsname'.
8812199Sahrens  */
8822199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy
8832199Sahrens int
8842199Sahrens dsl_snapshots_destroy(char *fsname, char *snapname)
8852199Sahrens {
8862199Sahrens 	int err;
8872199Sahrens 	struct destroyarg da;
8882199Sahrens 	dsl_sync_task_t *dst;
8892199Sahrens 	spa_t *spa;
8902199Sahrens 
8914603Sahrens 	err = spa_open(fsname, &spa, FTAG);
8922199Sahrens 	if (err)
8932199Sahrens 		return (err);
8942199Sahrens 	da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8952199Sahrens 	da.snapname = snapname;
8962199Sahrens 	da.failed = fsname;
8972199Sahrens 
8982199Sahrens 	err = dmu_objset_find(fsname,
8992417Sahrens 	    dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN);
9002199Sahrens 
9012199Sahrens 	if (err == 0)
9022199Sahrens 		err = dsl_sync_task_group_wait(da.dstg);
9032199Sahrens 
9042199Sahrens 	for (dst = list_head(&da.dstg->dstg_tasks); dst;
9052199Sahrens 	    dst = list_next(&da.dstg->dstg_tasks, dst)) {
9062199Sahrens 		dsl_dataset_t *ds = dst->dst_arg1;
9076689Smaybee 		/*
9086689Smaybee 		 * Return the file system name that triggered the error
9096689Smaybee 		 */
9102199Sahrens 		if (dst->dst_err) {
9112199Sahrens 			dsl_dataset_name(ds, fsname);
9124603Sahrens 			*strchr(fsname, '@') = '\0';
9132199Sahrens 		}
9146689Smaybee 		dsl_dataset_disown(ds, da.dstg);
9152199Sahrens 	}
9162199Sahrens 
9172199Sahrens 	dsl_sync_task_group_destroy(da.dstg);
9182199Sahrens 	spa_close(spa, FTAG);
9192199Sahrens 	return (err);
9202199Sahrens }
9212199Sahrens 
9225367Sahrens /*
9236689Smaybee  * ds must be opened as OWNER.  On return (whether successful or not),
9246689Smaybee  * ds will be closed and caller can no longer dereference it.
9255367Sahrens  */
926789Sahrens int
9275367Sahrens dsl_dataset_destroy(dsl_dataset_t *ds, void *tag)
928789Sahrens {
929789Sahrens 	int err;
9302199Sahrens 	dsl_sync_task_group_t *dstg;
9312199Sahrens 	objset_t *os;
932789Sahrens 	dsl_dir_t *dd;
9332199Sahrens 	uint64_t obj;
9342199Sahrens 
9355367Sahrens 	if (dsl_dataset_is_snapshot(ds)) {
9362199Sahrens 		/* Destroying a snapshot is simpler */
9376689Smaybee 		dsl_dataset_make_exclusive(ds, tag);
9387237Sek110237 
9397237Sek110237 		if (ds->ds_user_ptr) {
9407237Sek110237 			ds->ds_user_evict_func(ds, ds->ds_user_ptr);
9417237Sek110237 			ds->ds_user_ptr = NULL;
9427237Sek110237 		}
9432199Sahrens 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
9442199Sahrens 		    dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
9455367Sahrens 		    ds, tag, 0);
9465367Sahrens 		goto out;
9472199Sahrens 	}
9482199Sahrens 
9492199Sahrens 	dd = ds->ds_dir;
950789Sahrens 
9512199Sahrens 	/*
9522199Sahrens 	 * Check for errors and mark this ds as inconsistent, in
9532199Sahrens 	 * case we crash while freeing the objects.
9542199Sahrens 	 */
9552199Sahrens 	err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
9562199Sahrens 	    dsl_dataset_destroy_begin_sync, ds, NULL, 0);
9575367Sahrens 	if (err)
9585367Sahrens 		goto out;
9595367Sahrens 
9605367Sahrens 	err = dmu_objset_open_ds(ds, DMU_OST_ANY, &os);
9615367Sahrens 	if (err)
9625367Sahrens 		goto out;
9632199Sahrens 
9642199Sahrens 	/*
9652199Sahrens 	 * remove the objects in open context, so that we won't
9662199Sahrens 	 * have too much to do in syncing context.
9672199Sahrens 	 */
9683025Sahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
9693025Sahrens 	    ds->ds_phys->ds_prev_snap_txg)) {
9706992Smaybee 		/*
9716992Smaybee 		 * Ignore errors, if there is not enough disk space
9726992Smaybee 		 * we will deal with it in dsl_dataset_destroy_sync().
9736992Smaybee 		 */
9746992Smaybee 		(void) dmu_free_object(os, obj);
9752199Sahrens 	}
9762199Sahrens 
9772199Sahrens 	dmu_objset_close(os);
9782199Sahrens 	if (err != ESRCH)
9795367Sahrens 		goto out;
9802199Sahrens 
9816975Smaybee 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
9826975Smaybee 	err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd);
9836975Smaybee 	rw_exit(&dd->dd_pool->dp_config_rwlock);
9846975Smaybee 
9856975Smaybee 	if (err)
9866975Smaybee 		goto out;
9876975Smaybee 
9885367Sahrens 	if (ds->ds_user_ptr) {
9896689Smaybee 		/*
9906689Smaybee 		 * We need to sync out all in-flight IO before we try
9916689Smaybee 		 * to evict (the dataset evict func is trying to clear
9926689Smaybee 		 * the cached entries for this dataset in the ARC).
9936689Smaybee 		 */
9946689Smaybee 		txg_wait_synced(dd->dd_pool, 0);
9955367Sahrens 	}
9965367Sahrens 
9972199Sahrens 	/*
9982199Sahrens 	 * Blow away the dsl_dir + head dataset.
9992199Sahrens 	 */
10006689Smaybee 	dsl_dataset_make_exclusive(ds, tag);
10016975Smaybee 	if (ds->ds_user_ptr) {
10026975Smaybee 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
10036975Smaybee 		ds->ds_user_ptr = NULL;
10046975Smaybee 	}
10052199Sahrens 	dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
10062199Sahrens 	dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
10075367Sahrens 	    dsl_dataset_destroy_sync, ds, tag, 0);
10082199Sahrens 	dsl_sync_task_create(dstg, dsl_dir_destroy_check,
10092199Sahrens 	    dsl_dir_destroy_sync, dd, FTAG, 0);
10102199Sahrens 	err = dsl_sync_task_group_wait(dstg);
10112199Sahrens 	dsl_sync_task_group_destroy(dstg);
10126689Smaybee 	/* if it is successful, dsl_dir_destroy_sync will close the dd */
10135367Sahrens 	if (err)
10142199Sahrens 		dsl_dir_close(dd, FTAG);
10155367Sahrens out:
10166689Smaybee 	dsl_dataset_disown(ds, tag);
1017789Sahrens 	return (err);
1018789Sahrens }
1019789Sahrens 
1020789Sahrens int
10215367Sahrens dsl_dataset_rollback(dsl_dataset_t *ds, dmu_objset_type_t ost)
1022789Sahrens {
10237385SMark.Maybee@Sun.COM 	int err;
10247385SMark.Maybee@Sun.COM 
10256689Smaybee 	ASSERT(ds->ds_owner);
10265367Sahrens 
10277385SMark.Maybee@Sun.COM 	dsl_dataset_make_exclusive(ds, ds->ds_owner);
10287385SMark.Maybee@Sun.COM 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
10292199Sahrens 	    dsl_dataset_rollback_check, dsl_dataset_rollback_sync,
10307385SMark.Maybee@Sun.COM 	    ds, &ost, 0);
10317385SMark.Maybee@Sun.COM 	/* drop exclusive access */
10327385SMark.Maybee@Sun.COM 	mutex_enter(&ds->ds_lock);
10337385SMark.Maybee@Sun.COM 	rw_exit(&ds->ds_rwlock);
10347385SMark.Maybee@Sun.COM 	cv_broadcast(&ds->ds_exclusive_cv);
10357385SMark.Maybee@Sun.COM 	mutex_exit(&ds->ds_lock);
10367385SMark.Maybee@Sun.COM 	return (err);
1037789Sahrens }
1038789Sahrens 
1039789Sahrens void *
1040789Sahrens dsl_dataset_set_user_ptr(dsl_dataset_t *ds,
1041789Sahrens     void *p, dsl_dataset_evict_func_t func)
1042789Sahrens {
1043789Sahrens 	void *old;
1044789Sahrens 
1045789Sahrens 	mutex_enter(&ds->ds_lock);
1046789Sahrens 	old = ds->ds_user_ptr;
1047789Sahrens 	if (old == NULL) {
1048789Sahrens 		ds->ds_user_ptr = p;
1049789Sahrens 		ds->ds_user_evict_func = func;
1050789Sahrens 	}
1051789Sahrens 	mutex_exit(&ds->ds_lock);
1052789Sahrens 	return (old);
1053789Sahrens }
1054789Sahrens 
1055789Sahrens void *
1056789Sahrens dsl_dataset_get_user_ptr(dsl_dataset_t *ds)
1057789Sahrens {
1058789Sahrens 	return (ds->ds_user_ptr);
1059789Sahrens }
1060789Sahrens 
1061789Sahrens 
10623547Smaybee blkptr_t *
10633547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1064789Sahrens {
10653547Smaybee 	return (&ds->ds_phys->ds_bp);
1066789Sahrens }
1067789Sahrens 
1068789Sahrens void
1069789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1070789Sahrens {
1071789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1072789Sahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
1073789Sahrens 	if (ds == NULL) {
1074789Sahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
1075789Sahrens 	} else {
1076789Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1077789Sahrens 		ds->ds_phys->ds_bp = *bp;
1078789Sahrens 	}
1079789Sahrens }
1080789Sahrens 
1081789Sahrens spa_t *
1082789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
1083789Sahrens {
1084789Sahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1085789Sahrens }
1086789Sahrens 
1087789Sahrens void
1088789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1089789Sahrens {
1090789Sahrens 	dsl_pool_t *dp;
1091789Sahrens 
1092789Sahrens 	if (ds == NULL) /* this is the meta-objset */
1093789Sahrens 		return;
1094789Sahrens 
1095789Sahrens 	ASSERT(ds->ds_user_ptr != NULL);
10962885Sahrens 
10972885Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
10982885Sahrens 		panic("dirtying snapshot!");
1099789Sahrens 
1100789Sahrens 	dp = ds->ds_dir->dd_pool;
1101789Sahrens 
1102789Sahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
1103789Sahrens 		/* up the hold count until we can be written out */
1104789Sahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1105789Sahrens 	}
1106789Sahrens }
1107789Sahrens 
11085378Sck153898 /*
11095378Sck153898  * The unique space in the head dataset can be calculated by subtracting
11105378Sck153898  * the space used in the most recent snapshot, that is still being used
11115378Sck153898  * in this file system, from the space currently in use.  To figure out
11125378Sck153898  * the space in the most recent snapshot still in use, we need to take
11135378Sck153898  * the total space used in the snapshot and subtract out the space that
11145378Sck153898  * has been freed up since the snapshot was taken.
11155378Sck153898  */
11165378Sck153898 static void
11175378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
11185378Sck153898 {
11195378Sck153898 	uint64_t mrs_used;
11205378Sck153898 	uint64_t dlused, dlcomp, dluncomp;
11215378Sck153898 
11225378Sck153898 	ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj);
11235378Sck153898 
11245378Sck153898 	if (ds->ds_phys->ds_prev_snap_obj != 0)
11255378Sck153898 		mrs_used = ds->ds_prev->ds_phys->ds_used_bytes;
11265378Sck153898 	else
11275378Sck153898 		mrs_used = 0;
11285378Sck153898 
11295378Sck153898 	VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp,
11305378Sck153898 	    &dluncomp));
11315378Sck153898 
11325378Sck153898 	ASSERT3U(dlused, <=, mrs_used);
11335378Sck153898 	ds->ds_phys->ds_unique_bytes =
11345378Sck153898 	    ds->ds_phys->ds_used_bytes - (mrs_used - dlused);
11355378Sck153898 
11365378Sck153898 	if (!DS_UNIQUE_IS_ACCURATE(ds) &&
11375378Sck153898 	    spa_version(ds->ds_dir->dd_pool->dp_spa) >=
11385378Sck153898 	    SPA_VERSION_UNIQUE_ACCURATE)
11395378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
11405378Sck153898 }
11415378Sck153898 
11425378Sck153898 static uint64_t
11435378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds)
11445378Sck153898 {
11455378Sck153898 	if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds))
11465378Sck153898 		dsl_dataset_recalc_head_uniq(ds);
11475378Sck153898 
11485378Sck153898 	return (ds->ds_phys->ds_unique_bytes);
11495378Sck153898 }
11505378Sck153898 
1151789Sahrens struct killarg {
11527390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
1153789Sahrens 	zio_t *zio;
1154789Sahrens 	dmu_tx_t *tx;
1155789Sahrens };
1156789Sahrens 
11577390SMatthew.Ahrens@Sun.COM /* ARGSUSED */
1158789Sahrens static int
1159789Sahrens kill_blkptr(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
1160789Sahrens {
1161789Sahrens 	struct killarg *ka = arg;
1162789Sahrens 	blkptr_t *bp = &bc->bc_blkptr;
1163789Sahrens 
1164789Sahrens 	ASSERT3U(bc->bc_errno, ==, 0);
1165789Sahrens 
11667390SMatthew.Ahrens@Sun.COM 	ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
11677390SMatthew.Ahrens@Sun.COM 	(void) dsl_dataset_block_kill(ka->ds, bp, ka->zio, ka->tx);
11687390SMatthew.Ahrens@Sun.COM 
1169789Sahrens 	return (0);
1170789Sahrens }
1171789Sahrens 
1172789Sahrens /* ARGSUSED */
11732199Sahrens static int
11742199Sahrens dsl_dataset_rollback_check(void *arg1, void *arg2, dmu_tx_t *tx)
1175789Sahrens {
11762199Sahrens 	dsl_dataset_t *ds = arg1;
11775367Sahrens 	dmu_objset_type_t *ost = arg2;
1178789Sahrens 
11792199Sahrens 	/*
11805367Sahrens 	 * We can only roll back to emptyness if it is a ZPL objset.
11812199Sahrens 	 */
11825367Sahrens 	if (*ost != DMU_OST_ZFS && ds->ds_phys->ds_prev_snap_txg == 0)
1183789Sahrens 		return (EINVAL);
1184789Sahrens 
11852199Sahrens 	/*
11862199Sahrens 	 * This must not be a snapshot.
11872199Sahrens 	 */
11882199Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
11892199Sahrens 		return (EINVAL);
1190789Sahrens 
1191789Sahrens 	/*
1192789Sahrens 	 * If we made changes this txg, traverse_dsl_dataset won't find
1193789Sahrens 	 * them.  Try again.
1194789Sahrens 	 */
11952199Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1196789Sahrens 		return (EAGAIN);
11972199Sahrens 
11982199Sahrens 	return (0);
11992199Sahrens }
1200789Sahrens 
12012199Sahrens /* ARGSUSED */
12022199Sahrens static void
12034543Smarks dsl_dataset_rollback_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
12042199Sahrens {
12052199Sahrens 	dsl_dataset_t *ds = arg1;
12065367Sahrens 	dmu_objset_type_t *ost = arg2;
12072199Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1208789Sahrens 
1209789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1210789Sahrens 
12114967Sperrin 	/*
12124967Sperrin 	 * Before the roll back destroy the zil.
12134967Sperrin 	 */
12144967Sperrin 	if (ds->ds_user_ptr != NULL) {
12154967Sperrin 		zil_rollback_destroy(
12164967Sperrin 		    ((objset_impl_t *)ds->ds_user_ptr)->os_zil, tx);
12175367Sahrens 
12185367Sahrens 		/*
12195367Sahrens 		 * We need to make sure that the objset_impl_t is reopened after
12205367Sahrens 		 * we do the rollback, otherwise it will have the wrong
12215367Sahrens 		 * objset_phys_t.  Normally this would happen when this
12226689Smaybee 		 * dataset-open is closed, thus causing the
12235367Sahrens 		 * dataset to be immediately evicted.  But when doing "zfs recv
12245367Sahrens 		 * -F", we reopen the objset before that, so that there is no
12255367Sahrens 		 * window where the dataset is closed and inconsistent.
12265367Sahrens 		 */
12275367Sahrens 		ds->ds_user_evict_func(ds, ds->ds_user_ptr);
12285367Sahrens 		ds->ds_user_ptr = NULL;
12294967Sperrin 	}
12304935Sperrin 
12317390SMatthew.Ahrens@Sun.COM 	/* Transfer space that was freed since last snap back to the head. */
12327390SMatthew.Ahrens@Sun.COM 	{
12337390SMatthew.Ahrens@Sun.COM 		uint64_t used;
12347390SMatthew.Ahrens@Sun.COM 
12357390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&ds->ds_deadlist,
12367390SMatthew.Ahrens@Sun.COM 		    ds->ds_origin_txg, UINT64_MAX, &used));
12377390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(ds->ds_dir, used,
12387390SMatthew.Ahrens@Sun.COM 		    DD_USED_SNAP, DD_USED_HEAD, tx);
12397390SMatthew.Ahrens@Sun.COM 	}
12407390SMatthew.Ahrens@Sun.COM 
1241789Sahrens 	/* Zero out the deadlist. */
1242789Sahrens 	bplist_close(&ds->ds_deadlist);
1243789Sahrens 	bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1244789Sahrens 	ds->ds_phys->ds_deadlist_obj =
1245789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
12461544Seschrock 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
12471544Seschrock 	    ds->ds_phys->ds_deadlist_obj));
1248789Sahrens 
1249789Sahrens 	{
1250789Sahrens 		/* Free blkptrs that we gave birth to */
1251789Sahrens 		zio_t *zio;
1252789Sahrens 		struct killarg ka;
1253789Sahrens 
1254789Sahrens 		zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL,
1255789Sahrens 		    ZIO_FLAG_MUSTSUCCEED);
12567390SMatthew.Ahrens@Sun.COM 		ka.ds = ds;
1257789Sahrens 		ka.zio = zio;
1258789Sahrens 		ka.tx = tx;
1259789Sahrens 		(void) traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
1260789Sahrens 		    ADVANCE_POST, kill_blkptr, &ka);
1261789Sahrens 		(void) zio_wait(zio);
1262789Sahrens 	}
1263789Sahrens 
12647390SMatthew.Ahrens@Sun.COM 	ASSERT(!(ds->ds_phys->ds_flags & DS_FLAG_UNIQUE_ACCURATE) ||
12657390SMatthew.Ahrens@Sun.COM 	    ds->ds_phys->ds_unique_bytes == 0);
12667390SMatthew.Ahrens@Sun.COM 
12677046Sahrens 	if (ds->ds_prev && ds->ds_prev != ds->ds_dir->dd_pool->dp_origin_snap) {
12685367Sahrens 		/* Change our contents to that of the prev snapshot */
12697390SMatthew.Ahrens@Sun.COM 
12705367Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
12715367Sahrens 		    ds->ds_phys->ds_prev_snap_obj);
12727390SMatthew.Ahrens@Sun.COM 		ASSERT3U(ds->ds_phys->ds_used_bytes, <=,
12737390SMatthew.Ahrens@Sun.COM 		    ds->ds_prev->ds_phys->ds_used_bytes);
12747390SMatthew.Ahrens@Sun.COM 
12755367Sahrens 		ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp;
12765367Sahrens 		ds->ds_phys->ds_used_bytes =
12775367Sahrens 		    ds->ds_prev->ds_phys->ds_used_bytes;
12785367Sahrens 		ds->ds_phys->ds_compressed_bytes =
12795367Sahrens 		    ds->ds_prev->ds_phys->ds_compressed_bytes;
12805367Sahrens 		ds->ds_phys->ds_uncompressed_bytes =
12815367Sahrens 		    ds->ds_prev->ds_phys->ds_uncompressed_bytes;
12825367Sahrens 		ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags;
1283789Sahrens 
12845367Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
12855367Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
12865367Sahrens 			ds->ds_prev->ds_phys->ds_unique_bytes = 0;
12875367Sahrens 		}
12885367Sahrens 	} else {
12897046Sahrens 		objset_impl_t *osi;
12907046Sahrens 
12917390SMatthew.Ahrens@Sun.COM 		ASSERT3U(ds->ds_phys->ds_used_bytes, ==, 0);
12927390SMatthew.Ahrens@Sun.COM 		ASSERT3U(ds->ds_phys->ds_compressed_bytes, ==, 0);
12937390SMatthew.Ahrens@Sun.COM 		ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, ==, 0);
12947390SMatthew.Ahrens@Sun.COM 
12955367Sahrens 		bzero(&ds->ds_phys->ds_bp, sizeof (blkptr_t));
12965367Sahrens 		ds->ds_phys->ds_flags = 0;
12975367Sahrens 		ds->ds_phys->ds_unique_bytes = 0;
12987390SMatthew.Ahrens@Sun.COM 		if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
12997390SMatthew.Ahrens@Sun.COM 		    SPA_VERSION_UNIQUE_ACCURATE)
13007390SMatthew.Ahrens@Sun.COM 			ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
13017390SMatthew.Ahrens@Sun.COM 
13027046Sahrens 		osi = dmu_objset_create_impl(ds->ds_dir->dd_pool->dp_spa, ds,
13035367Sahrens 		    &ds->ds_phys->ds_bp, *ost, tx);
13047046Sahrens #ifdef _KERNEL
13057046Sahrens 		zfs_create_fs(&osi->os, kcred, NULL, tx);
13067046Sahrens #endif
13072532Sahrens 	}
13084543Smarks 
13094543Smarks 	spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa,
13104543Smarks 	    tx, cr, "dataset = %llu", ds->ds_object);
1311789Sahrens }
1312789Sahrens 
13131731Sbonwick /* ARGSUSED */
13141731Sbonwick static int
13152199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
13161731Sbonwick {
13172199Sahrens 	dsl_dataset_t *ds = arg1;
13185367Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
13195367Sahrens 	uint64_t count;
13205367Sahrens 	int err;
13211731Sbonwick 
13221731Sbonwick 	/*
13231731Sbonwick 	 * Can't delete a head dataset if there are snapshots of it.
13241731Sbonwick 	 * (Except if the only snapshots are from the branch we cloned
13251731Sbonwick 	 * from.)
13261731Sbonwick 	 */
13271731Sbonwick 	if (ds->ds_prev != NULL &&
13281731Sbonwick 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
13291731Sbonwick 		return (EINVAL);
13301731Sbonwick 
13315367Sahrens 	/*
13325367Sahrens 	 * This is really a dsl_dir thing, but check it here so that
13335367Sahrens 	 * we'll be less likely to leave this dataset inconsistent &
13345367Sahrens 	 * nearly destroyed.
13355367Sahrens 	 */
13365367Sahrens 	err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
13375367Sahrens 	if (err)
13385367Sahrens 		return (err);
13395367Sahrens 	if (count != 0)
13405367Sahrens 		return (EEXIST);
13415367Sahrens 
13421731Sbonwick 	return (0);
13431731Sbonwick }
13441731Sbonwick 
13452199Sahrens /* ARGSUSED */
13462199Sahrens static void
13474543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1348789Sahrens {
13492199Sahrens 	dsl_dataset_t *ds = arg1;
13504543Smarks 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1351789Sahrens 
13522199Sahrens 	/* Mark it as inconsistent on-disk, in case we crash */
13532199Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
13542199Sahrens 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
13554543Smarks 
13564543Smarks 	spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
13574543Smarks 	    cr, "dataset = %llu", ds->ds_object);
13582199Sahrens }
1359789Sahrens 
13602199Sahrens /* ARGSUSED */
13615367Sahrens int
13622199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
13632199Sahrens {
13642199Sahrens 	dsl_dataset_t *ds = arg1;
1365789Sahrens 
13666689Smaybee 	/* we have an owner hold, so noone else can destroy us */
13676689Smaybee 	ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
13686689Smaybee 
1369789Sahrens 	/* Can't delete a branch point. */
13702199Sahrens 	if (ds->ds_phys->ds_num_children > 1)
13712199Sahrens 		return (EEXIST);
1372789Sahrens 
1373789Sahrens 	/*
1374789Sahrens 	 * Can't delete a head dataset if there are snapshots of it.
1375789Sahrens 	 * (Except if the only snapshots are from the branch we cloned
1376789Sahrens 	 * from.)
1377789Sahrens 	 */
1378789Sahrens 	if (ds->ds_prev != NULL &&
13792199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
1380789Sahrens 		return (EINVAL);
1381789Sahrens 
1382789Sahrens 	/*
1383789Sahrens 	 * If we made changes this txg, traverse_dsl_dataset won't find
1384789Sahrens 	 * them.  Try again.
1385789Sahrens 	 */
13862199Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1387789Sahrens 		return (EAGAIN);
13882199Sahrens 
13892199Sahrens 	/* XXX we should do some i/o error checking... */
13902199Sahrens 	return (0);
13912199Sahrens }
13922199Sahrens 
13936689Smaybee struct refsarg {
13946689Smaybee 	kmutex_t lock;
13956689Smaybee 	boolean_t gone;
13966689Smaybee 	kcondvar_t cv;
13976689Smaybee };
13986689Smaybee 
13996689Smaybee /* ARGSUSED */
14006689Smaybee static void
14016689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv)
14026689Smaybee {
14036689Smaybee 	struct refsarg *arg = argv;
14046689Smaybee 
14056689Smaybee 	mutex_enter(&arg->lock);
14066689Smaybee 	arg->gone = TRUE;
14076689Smaybee 	cv_signal(&arg->cv);
14086689Smaybee 	mutex_exit(&arg->lock);
14096689Smaybee }
14106689Smaybee 
14116689Smaybee static void
14126689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag)
14136689Smaybee {
14146689Smaybee 	struct refsarg arg;
14156689Smaybee 
14166689Smaybee 	mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL);
14176689Smaybee 	cv_init(&arg.cv, NULL, CV_DEFAULT, NULL);
14186689Smaybee 	arg.gone = FALSE;
14196689Smaybee 	(void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys,
14206689Smaybee 	    dsl_dataset_refs_gone);
14216689Smaybee 	dmu_buf_rele(ds->ds_dbuf, tag);
14226689Smaybee 	mutex_enter(&arg.lock);
14236689Smaybee 	while (!arg.gone)
14246689Smaybee 		cv_wait(&arg.cv, &arg.lock);
14256689Smaybee 	ASSERT(arg.gone);
14266689Smaybee 	mutex_exit(&arg.lock);
14276689Smaybee 	ds->ds_dbuf = NULL;
14286689Smaybee 	ds->ds_phys = NULL;
14296689Smaybee 	mutex_destroy(&arg.lock);
14306689Smaybee 	cv_destroy(&arg.cv);
14316689Smaybee }
14326689Smaybee 
14335367Sahrens void
14344543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
14352199Sahrens {
14362199Sahrens 	dsl_dataset_t *ds = arg1;
14372199Sahrens 	zio_t *zio;
14382199Sahrens 	int err;
14392199Sahrens 	int after_branch_point = FALSE;
14402199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
14412199Sahrens 	objset_t *mos = dp->dp_meta_objset;
14422199Sahrens 	dsl_dataset_t *ds_prev = NULL;
14432199Sahrens 	uint64_t obj;
14442199Sahrens 
14456689Smaybee 	ASSERT(ds->ds_owner);
14462199Sahrens 	ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
14472199Sahrens 	ASSERT(ds->ds_prev == NULL ||
14482199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
14492199Sahrens 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
14502199Sahrens 
14516689Smaybee 	/* signal any waiters that this dataset is going away */
14526689Smaybee 	mutex_enter(&ds->ds_lock);
14536689Smaybee 	ds->ds_owner = dsl_reaper;
14546689Smaybee 	cv_broadcast(&ds->ds_exclusive_cv);
14556689Smaybee 	mutex_exit(&ds->ds_lock);
14566689Smaybee 
14575378Sck153898 	/* Remove our reservation */
14585378Sck153898 	if (ds->ds_reserved != 0) {
14595378Sck153898 		uint64_t val = 0;
14605378Sck153898 		dsl_dataset_set_reservation_sync(ds, &val, cr, tx);
14615378Sck153898 		ASSERT3U(ds->ds_reserved, ==, 0);
14625378Sck153898 	}
14635378Sck153898 
14642199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
14652199Sahrens 
14667046Sahrens 	dsl_pool_ds_destroyed(ds, tx);
14677046Sahrens 
14682199Sahrens 	obj = ds->ds_object;
1469789Sahrens 
1470789Sahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1471789Sahrens 		if (ds->ds_prev) {
1472789Sahrens 			ds_prev = ds->ds_prev;
1473789Sahrens 		} else {
14746689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
14756689Smaybee 			    ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
1476789Sahrens 		}
1477789Sahrens 		after_branch_point =
1478789Sahrens 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
1479789Sahrens 
1480789Sahrens 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1481789Sahrens 		if (after_branch_point &&
14827046Sahrens 		    ds_prev->ds_phys->ds_next_clones_obj != 0) {
14837046Sahrens 			VERIFY(0 == zap_remove_int(mos,
14847046Sahrens 			    ds_prev->ds_phys->ds_next_clones_obj, obj, tx));
14857046Sahrens 			if (ds->ds_phys->ds_next_snap_obj != 0) {
14867046Sahrens 				VERIFY(0 == zap_add_int(mos,
14877046Sahrens 				    ds_prev->ds_phys->ds_next_clones_obj,
14887046Sahrens 				    ds->ds_phys->ds_next_snap_obj, tx));
14897046Sahrens 			}
14907046Sahrens 		}
14917046Sahrens 		if (after_branch_point &&
1492789Sahrens 		    ds->ds_phys->ds_next_snap_obj == 0) {
1493789Sahrens 			/* This clone is toast. */
1494789Sahrens 			ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1495789Sahrens 			ds_prev->ds_phys->ds_num_children--;
1496789Sahrens 		} else if (!after_branch_point) {
1497789Sahrens 			ds_prev->ds_phys->ds_next_snap_obj =
1498789Sahrens 			    ds->ds_phys->ds_next_snap_obj;
1499789Sahrens 		}
1500789Sahrens 	}
1501789Sahrens 
1502789Sahrens 	zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1503789Sahrens 
1504789Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0) {
15052199Sahrens 		blkptr_t bp;
1506789Sahrens 		dsl_dataset_t *ds_next;
1507789Sahrens 		uint64_t itor = 0;
15085378Sck153898 		uint64_t old_unique;
15097390SMatthew.Ahrens@Sun.COM 		int64_t used = 0, compressed = 0, uncompressed = 0;
1510789Sahrens 
15116689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
15126689Smaybee 		    ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
1513789Sahrens 		ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1514789Sahrens 
15155378Sck153898 		old_unique = dsl_dataset_unique(ds_next);
15165378Sck153898 
1517789Sahrens 		dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1518789Sahrens 		ds_next->ds_phys->ds_prev_snap_obj =
1519789Sahrens 		    ds->ds_phys->ds_prev_snap_obj;
1520789Sahrens 		ds_next->ds_phys->ds_prev_snap_txg =
1521789Sahrens 		    ds->ds_phys->ds_prev_snap_txg;
1522789Sahrens 		ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1523789Sahrens 		    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1524789Sahrens 
1525789Sahrens 		/*
1526789Sahrens 		 * Transfer to our deadlist (which will become next's
1527789Sahrens 		 * new deadlist) any entries from next's current
1528789Sahrens 		 * deadlist which were born before prev, and free the
1529789Sahrens 		 * other entries.
1530789Sahrens 		 *
1531789Sahrens 		 * XXX we're doing this long task with the config lock held
1532789Sahrens 		 */
15336689Smaybee 		while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) {
1534789Sahrens 			if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) {
15351544Seschrock 				VERIFY(0 == bplist_enqueue(&ds->ds_deadlist,
15361544Seschrock 				    &bp, tx));
1537789Sahrens 				if (ds_prev && !after_branch_point &&
1538789Sahrens 				    bp.blk_birth >
1539789Sahrens 				    ds_prev->ds_phys->ds_prev_snap_txg) {
1540789Sahrens 					ds_prev->ds_phys->ds_unique_bytes +=
15412082Seschrock 					    bp_get_dasize(dp->dp_spa, &bp);
1542789Sahrens 				}
1543789Sahrens 			} else {
15442082Seschrock 				used += bp_get_dasize(dp->dp_spa, &bp);
1545789Sahrens 				compressed += BP_GET_PSIZE(&bp);
1546789Sahrens 				uncompressed += BP_GET_UCSIZE(&bp);
1547789Sahrens 				/* XXX check return value? */
15487046Sahrens 				(void) dsl_free(zio, dp, tx->tx_txg,
1549789Sahrens 				    &bp, NULL, NULL, ARC_NOWAIT);
1550789Sahrens 			}
1551789Sahrens 		}
1552789Sahrens 
15537390SMatthew.Ahrens@Sun.COM 		ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes);
15547390SMatthew.Ahrens@Sun.COM 
15557390SMatthew.Ahrens@Sun.COM 		/* change snapused */
15567390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
15577390SMatthew.Ahrens@Sun.COM 		    -used, -compressed, -uncompressed, tx);
15587390SMatthew.Ahrens@Sun.COM 
1559789Sahrens 		/* free next's deadlist */
1560789Sahrens 		bplist_close(&ds_next->ds_deadlist);
1561789Sahrens 		bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx);
1562789Sahrens 
1563789Sahrens 		/* set next's deadlist to our deadlist */
15646689Smaybee 		bplist_close(&ds->ds_deadlist);
1565789Sahrens 		ds_next->ds_phys->ds_deadlist_obj =
1566789Sahrens 		    ds->ds_phys->ds_deadlist_obj;
15671544Seschrock 		VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos,
15681544Seschrock 		    ds_next->ds_phys->ds_deadlist_obj));
1569789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1570789Sahrens 
1571789Sahrens 		if (ds_next->ds_phys->ds_next_snap_obj != 0) {
1572789Sahrens 			/*
1573789Sahrens 			 * Update next's unique to include blocks which
1574789Sahrens 			 * were previously shared by only this snapshot
1575789Sahrens 			 * and it.  Those blocks will be born after the
1576789Sahrens 			 * prev snap and before this snap, and will have
1577789Sahrens 			 * died after the next snap and before the one
1578789Sahrens 			 * after that (ie. be on the snap after next's
1579789Sahrens 			 * deadlist).
1580789Sahrens 			 *
1581789Sahrens 			 * XXX we're doing this long task with the
1582789Sahrens 			 * config lock held
1583789Sahrens 			 */
1584789Sahrens 			dsl_dataset_t *ds_after_next;
15857390SMatthew.Ahrens@Sun.COM 			uint64_t space;
1586789Sahrens 
15876689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
15886689Smaybee 			    ds_next->ds_phys->ds_next_snap_obj,
15896689Smaybee 			    FTAG, &ds_after_next));
15907390SMatthew.Ahrens@Sun.COM 
15917390SMatthew.Ahrens@Sun.COM 			VERIFY(0 ==
15927390SMatthew.Ahrens@Sun.COM 			    bplist_space_birthrange(&ds_after_next->ds_deadlist,
15937390SMatthew.Ahrens@Sun.COM 			    ds->ds_phys->ds_prev_snap_txg,
15947390SMatthew.Ahrens@Sun.COM 			    ds->ds_phys->ds_creation_txg, &space));
15957390SMatthew.Ahrens@Sun.COM 			ds_next->ds_phys->ds_unique_bytes += space;
1596789Sahrens 
15976689Smaybee 			dsl_dataset_rele(ds_after_next, FTAG);
1598789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, NULL);
1599789Sahrens 		} else {
1600789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, ds);
16016689Smaybee 			dsl_dataset_drop_ref(ds_next->ds_prev, ds_next);
16026689Smaybee 			ds_next->ds_prev = NULL;
1603789Sahrens 			if (ds_prev) {
16046689Smaybee 				VERIFY(0 == dsl_dataset_get_ref(dp,
16056689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
16066689Smaybee 				    ds_next, &ds_next->ds_prev));
1607789Sahrens 			}
16085378Sck153898 
16095378Sck153898 			dsl_dataset_recalc_head_uniq(ds_next);
16105378Sck153898 
16115378Sck153898 			/*
16125378Sck153898 			 * Reduce the amount of our unconsmed refreservation
16135378Sck153898 			 * being charged to our parent by the amount of
16145378Sck153898 			 * new unique data we have gained.
16155378Sck153898 			 */
16165378Sck153898 			if (old_unique < ds_next->ds_reserved) {
16175378Sck153898 				int64_t mrsdelta;
16185378Sck153898 				uint64_t new_unique =
16195378Sck153898 				    ds_next->ds_phys->ds_unique_bytes;
16205378Sck153898 
16215378Sck153898 				ASSERT(old_unique <= new_unique);
16225378Sck153898 				mrsdelta = MIN(new_unique - old_unique,
16235378Sck153898 				    ds_next->ds_reserved - old_unique);
16247390SMatthew.Ahrens@Sun.COM 				dsl_dir_diduse_space(ds->ds_dir,
16257390SMatthew.Ahrens@Sun.COM 				    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
16265378Sck153898 			}
1627789Sahrens 		}
16286689Smaybee 		dsl_dataset_rele(ds_next, FTAG);
1629789Sahrens 	} else {
1630789Sahrens 		/*
1631789Sahrens 		 * There's no next snapshot, so this is a head dataset.
1632789Sahrens 		 * Destroy the deadlist.  Unless it's a clone, the
1633789Sahrens 		 * deadlist should be empty.  (If it's a clone, it's
1634789Sahrens 		 * safe to ignore the deadlist contents.)
1635789Sahrens 		 */
1636789Sahrens 		struct killarg ka;
1637789Sahrens 
1638789Sahrens 		ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist));
1639789Sahrens 		bplist_close(&ds->ds_deadlist);
1640789Sahrens 		bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1641789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1642789Sahrens 
1643789Sahrens 		/*
1644789Sahrens 		 * Free everything that we point to (that's born after
1645789Sahrens 		 * the previous snapshot, if we are a clone)
1646789Sahrens 		 *
16477390SMatthew.Ahrens@Sun.COM 		 * NB: this should be very quick, because we already
16487390SMatthew.Ahrens@Sun.COM 		 * freed all the objects in open context.
1649789Sahrens 		 */
16507390SMatthew.Ahrens@Sun.COM 		ka.ds = ds;
1651789Sahrens 		ka.zio = zio;
1652789Sahrens 		ka.tx = tx;
1653789Sahrens 		err = traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
1654789Sahrens 		    ADVANCE_POST, kill_blkptr, &ka);
1655789Sahrens 		ASSERT3U(err, ==, 0);
16567390SMatthew.Ahrens@Sun.COM 		ASSERT(spa_version(dp->dp_spa) < SPA_VERSION_UNIQUE_ACCURATE ||
16577390SMatthew.Ahrens@Sun.COM 		    ds->ds_phys->ds_unique_bytes == 0);
1658789Sahrens 	}
1659789Sahrens 
1660789Sahrens 	err = zio_wait(zio);
1661789Sahrens 	ASSERT3U(err, ==, 0);
1662789Sahrens 
16636689Smaybee 	if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
16646689Smaybee 		/* Erase the link in the dir */
16656689Smaybee 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
16666689Smaybee 		ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
16676689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
1668789Sahrens 		err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1669789Sahrens 		ASSERT(err == 0);
1670789Sahrens 	} else {
1671789Sahrens 		/* remove from snapshot namespace */
1672789Sahrens 		dsl_dataset_t *ds_head;
16736689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
16746689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
16756689Smaybee 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
16762207Sahrens 		VERIFY(0 == dsl_dataset_get_snapname(ds));
1677789Sahrens #ifdef ZFS_DEBUG
1678789Sahrens 		{
1679789Sahrens 			uint64_t val;
16806492Stimh 
16816689Smaybee 			err = dsl_dataset_snap_lookup(ds_head,
16826492Stimh 			    ds->ds_snapname, &val);
1683789Sahrens 			ASSERT3U(err, ==, 0);
1684789Sahrens 			ASSERT3U(val, ==, obj);
1685789Sahrens 		}
1686789Sahrens #endif
16876689Smaybee 		err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
1688789Sahrens 		ASSERT(err == 0);
16896689Smaybee 		dsl_dataset_rele(ds_head, FTAG);
1690789Sahrens 	}
1691789Sahrens 
1692789Sahrens 	if (ds_prev && ds->ds_prev != ds_prev)
16936689Smaybee 		dsl_dataset_rele(ds_prev, FTAG);
1694789Sahrens 
16955094Slling 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
16964543Smarks 	spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx,
16974543Smarks 	    cr, "dataset = %llu", ds->ds_object);
16984543Smarks 
16997046Sahrens 	if (ds->ds_phys->ds_next_clones_obj != 0) {
17007046Sahrens 		uint64_t count;
17017046Sahrens 		ASSERT(0 == zap_count(mos,
17027046Sahrens 		    ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
17037046Sahrens 		VERIFY(0 == dmu_object_free(mos,
17047046Sahrens 		    ds->ds_phys->ds_next_clones_obj, tx));
17057046Sahrens 	}
17067390SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_props_obj != 0)
17077390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
17086689Smaybee 	dsl_dir_close(ds->ds_dir, ds);
17096689Smaybee 	ds->ds_dir = NULL;
17106689Smaybee 	dsl_dataset_drain_refs(ds, tag);
17112199Sahrens 	VERIFY(0 == dmu_object_free(mos, obj, tx));
17122199Sahrens }
17132199Sahrens 
17145378Sck153898 static int
17155378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
17165378Sck153898 {
17175378Sck153898 	uint64_t asize;
17185378Sck153898 
17195378Sck153898 	if (!dmu_tx_is_syncing(tx))
17205378Sck153898 		return (0);
17215378Sck153898 
17225378Sck153898 	/*
17235378Sck153898 	 * If there's an fs-only reservation, any blocks that might become
17245378Sck153898 	 * owned by the snapshot dataset must be accommodated by space
17255378Sck153898 	 * outside of the reservation.
17265378Sck153898 	 */
17275378Sck153898 	asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
17285378Sck153898 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE))
17295378Sck153898 		return (ENOSPC);
17305378Sck153898 
17315378Sck153898 	/*
17325378Sck153898 	 * Propogate any reserved space for this snapshot to other
17335378Sck153898 	 * snapshot checks in this sync group.
17345378Sck153898 	 */
17355378Sck153898 	if (asize > 0)
17365378Sck153898 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
17375378Sck153898 
17385378Sck153898 	return (0);
17395378Sck153898 }
17405378Sck153898 
17412199Sahrens /* ARGSUSED */
17422199Sahrens int
17432199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
17442199Sahrens {
17455367Sahrens 	dsl_dataset_t *ds = arg1;
17462199Sahrens 	const char *snapname = arg2;
17472199Sahrens 	int err;
17482199Sahrens 	uint64_t value;
1749789Sahrens 
1750789Sahrens 	/*
17512199Sahrens 	 * We don't allow multiple snapshots of the same txg.  If there
17522199Sahrens 	 * is already one, try again.
17532199Sahrens 	 */
17542199Sahrens 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
17552199Sahrens 		return (EAGAIN);
17562199Sahrens 
17572199Sahrens 	/*
17582199Sahrens 	 * Check for conflicting name snapshot name.
1759789Sahrens 	 */
17606689Smaybee 	err = dsl_dataset_snap_lookup(ds, snapname, &value);
17612199Sahrens 	if (err == 0)
17622199Sahrens 		return (EEXIST);
17632199Sahrens 	if (err != ENOENT)
17642199Sahrens 		return (err);
1765789Sahrens 
17663978Smmusante 	/*
17673978Smmusante 	 * Check that the dataset's name is not too long.  Name consists
17683978Smmusante 	 * of the dataset's length + 1 for the @-sign + snapshot name's length
17693978Smmusante 	 */
17703978Smmusante 	if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
17713978Smmusante 		return (ENAMETOOLONG);
17723978Smmusante 
17735378Sck153898 	err = dsl_dataset_snapshot_reserve_space(ds, tx);
17745378Sck153898 	if (err)
17755378Sck153898 		return (err);
17765378Sck153898 
17772199Sahrens 	ds->ds_trysnap_txg = tx->tx_txg;
1778789Sahrens 	return (0);
1779789Sahrens }
1780789Sahrens 
17812199Sahrens void
17824543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1783789Sahrens {
17845367Sahrens 	dsl_dataset_t *ds = arg1;
17852199Sahrens 	const char *snapname = arg2;
17862199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1787789Sahrens 	dmu_buf_t *dbuf;
1788789Sahrens 	dsl_dataset_phys_t *dsphys;
17897046Sahrens 	uint64_t dsobj, crtxg;
1790789Sahrens 	objset_t *mos = dp->dp_meta_objset;
1791789Sahrens 	int err;
1792789Sahrens 
17932199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1794789Sahrens 
17957046Sahrens 	/*
17967046Sahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
17977046Sahrens 	 */
17987046Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
17997046Sahrens 		crtxg = 1;
18007046Sahrens 	else
18017046Sahrens 		crtxg = tx->tx_txg;
18027046Sahrens 
1803928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1804928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
18051544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1806789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1807789Sahrens 	dsphys = dbuf->db_data;
18086689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
18092199Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1810789Sahrens 	dsphys->ds_fsid_guid = unique_create();
1811789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1812789Sahrens 	    sizeof (dsphys->ds_guid));
1813789Sahrens 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1814789Sahrens 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1815789Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1816789Sahrens 	dsphys->ds_num_children = 1;
1817789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
18187046Sahrens 	dsphys->ds_creation_txg = crtxg;
1819789Sahrens 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1820789Sahrens 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
1821789Sahrens 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1822789Sahrens 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
18232082Seschrock 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1824789Sahrens 	dsphys->ds_bp = ds->ds_phys->ds_bp;
18251544Seschrock 	dmu_buf_rele(dbuf, FTAG);
1826789Sahrens 
18272199Sahrens 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
18282199Sahrens 	if (ds->ds_prev) {
18297046Sahrens 		uint64_t next_clones_obj =
18307046Sahrens 		    ds->ds_prev->ds_phys->ds_next_clones_obj;
18312199Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1832789Sahrens 		    ds->ds_object ||
18332199Sahrens 		    ds->ds_prev->ds_phys->ds_num_children > 1);
18342199Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
18352199Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1836789Sahrens 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
18372199Sahrens 			    ds->ds_prev->ds_phys->ds_creation_txg);
18382199Sahrens 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
18397046Sahrens 		} else if (next_clones_obj != 0) {
18407046Sahrens 			VERIFY3U(0, ==, zap_remove_int(mos,
18417046Sahrens 			    next_clones_obj, dsphys->ds_next_snap_obj, tx));
18427046Sahrens 			VERIFY3U(0, ==, zap_add_int(mos,
18437046Sahrens 			    next_clones_obj, dsobj, tx));
1844789Sahrens 		}
1845789Sahrens 	}
1846789Sahrens 
18475378Sck153898 	/*
18485378Sck153898 	 * If we have a reference-reservation on this dataset, we will
18495378Sck153898 	 * need to increase the amount of refreservation being charged
18505378Sck153898 	 * since our unique space is going to zero.
18515378Sck153898 	 */
18525378Sck153898 	if (ds->ds_reserved) {
18535378Sck153898 		int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
18547390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
18557390SMatthew.Ahrens@Sun.COM 		    add, 0, 0, tx);
18565378Sck153898 	}
18575378Sck153898 
1858789Sahrens 	bplist_close(&ds->ds_deadlist);
1859789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
18605712Sahrens 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1861789Sahrens 	ds->ds_phys->ds_prev_snap_obj = dsobj;
18627046Sahrens 	ds->ds_phys->ds_prev_snap_txg = crtxg;
1863789Sahrens 	ds->ds_phys->ds_unique_bytes = 0;
18645378Sck153898 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
18655378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1866789Sahrens 	ds->ds_phys->ds_deadlist_obj =
1867789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
18681544Seschrock 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
18691544Seschrock 	    ds->ds_phys->ds_deadlist_obj));
1870789Sahrens 
1871789Sahrens 	dprintf("snap '%s' -> obj %llu\n", snapname, dsobj);
1872789Sahrens 	err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1873789Sahrens 	    snapname, 8, 1, &dsobj, tx);
1874789Sahrens 	ASSERT(err == 0);
1875789Sahrens 
1876789Sahrens 	if (ds->ds_prev)
18776689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
18786689Smaybee 	VERIFY(0 == dsl_dataset_get_ref(dp,
18796689Smaybee 	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
18804543Smarks 
18817046Sahrens 	dsl_pool_ds_snapshotted(ds, tx);
18827046Sahrens 
18834543Smarks 	spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr,
18844603Sahrens 	    "dataset = %llu", dsobj);
1885789Sahrens }
1886789Sahrens 
1887789Sahrens void
18883547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1889789Sahrens {
1890789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1891789Sahrens 	ASSERT(ds->ds_user_ptr != NULL);
1892789Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1893789Sahrens 
18944787Sahrens 	/*
18954787Sahrens 	 * in case we had to change ds_fsid_guid when we opened it,
18964787Sahrens 	 * sync it out now.
18974787Sahrens 	 */
18984787Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
18994787Sahrens 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
19004787Sahrens 
1901789Sahrens 	dsl_dir_dirty(ds->ds_dir, tx);
19023547Smaybee 	dmu_objset_sync(ds->ds_user_ptr, zio, tx);
1903789Sahrens }
1904789Sahrens 
1905789Sahrens void
19062885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1907789Sahrens {
19085378Sck153898 	uint64_t refd, avail, uobjs, aobjs;
19095378Sck153898 
19102885Sahrens 	dsl_dir_stats(ds->ds_dir, nv);
1911789Sahrens 
19125378Sck153898 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
19135378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
19145378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
19155378Sck153898 
19162885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
19172885Sahrens 	    ds->ds_phys->ds_creation_time);
19182885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
19192885Sahrens 	    ds->ds_phys->ds_creation_txg);
19205378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
19215378Sck153898 	    ds->ds_quota);
19225378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
19235378Sck153898 	    ds->ds_reserved);
19246643Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
19256643Seschrock 	    ds->ds_phys->ds_guid);
1926789Sahrens 
1927789Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
1928789Sahrens 		/*
1929789Sahrens 		 * This is a snapshot; override the dd's space used with
19302885Sahrens 		 * our unique space and compression ratio.
1931789Sahrens 		 */
19322885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
19332885Sahrens 		    ds->ds_phys->ds_unique_bytes);
19342885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
19352885Sahrens 		    ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
19362885Sahrens 		    (ds->ds_phys->ds_uncompressed_bytes * 100 /
19372885Sahrens 		    ds->ds_phys->ds_compressed_bytes));
1938789Sahrens 	}
1939789Sahrens }
1940789Sahrens 
19412885Sahrens void
19422885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1943789Sahrens {
19442885Sahrens 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
19452885Sahrens 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
19465367Sahrens 	stat->dds_guid = ds->ds_phys->ds_guid;
19472885Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
19482885Sahrens 		stat->dds_is_snapshot = B_TRUE;
19492885Sahrens 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
19502885Sahrens 	}
19512885Sahrens 
19522885Sahrens 	/* clone origin is really a dsl_dir thing... */
19535446Sahrens 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
19547046Sahrens 	if (dsl_dir_is_clone(ds->ds_dir)) {
19552885Sahrens 		dsl_dataset_t *ods;
19562885Sahrens 
19576689Smaybee 		VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
19586689Smaybee 		    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
19595367Sahrens 		dsl_dataset_name(ods, stat->dds_origin);
19606689Smaybee 		dsl_dataset_drop_ref(ods, FTAG);
19612885Sahrens 	}
19625446Sahrens 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
19632885Sahrens }
19642885Sahrens 
19652885Sahrens uint64_t
19662885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
19672885Sahrens {
19684787Sahrens 	return (ds->ds_fsid_guid);
19692885Sahrens }
19702885Sahrens 
19712885Sahrens void
19722885Sahrens dsl_dataset_space(dsl_dataset_t *ds,
19732885Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
19742885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
19752885Sahrens {
19762885Sahrens 	*refdbytesp = ds->ds_phys->ds_used_bytes;
19772885Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
19785378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
19795378Sck153898 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
19805378Sck153898 	if (ds->ds_quota != 0) {
19815378Sck153898 		/*
19825378Sck153898 		 * Adjust available bytes according to refquota
19835378Sck153898 		 */
19845378Sck153898 		if (*refdbytesp < ds->ds_quota)
19855378Sck153898 			*availbytesp = MIN(*availbytesp,
19865378Sck153898 			    ds->ds_quota - *refdbytesp);
19875378Sck153898 		else
19885378Sck153898 			*availbytesp = 0;
19895378Sck153898 	}
19902885Sahrens 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
19912885Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1992789Sahrens }
1993789Sahrens 
19945326Sek110237 boolean_t
19955326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
19965326Sek110237 {
19975326Sek110237 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
19985326Sek110237 
19995326Sek110237 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
20005326Sek110237 	    dsl_pool_sync_context(dp));
20015326Sek110237 	if (ds->ds_prev == NULL)
20025326Sek110237 		return (B_FALSE);
20035326Sek110237 	if (ds->ds_phys->ds_bp.blk_birth >
20045326Sek110237 	    ds->ds_prev->ds_phys->ds_creation_txg)
20055326Sek110237 		return (B_TRUE);
20065326Sek110237 	return (B_FALSE);
20075326Sek110237 }
20085326Sek110237 
20092199Sahrens /* ARGSUSED */
2010789Sahrens static int
20112199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
2012789Sahrens {
20132199Sahrens 	dsl_dataset_t *ds = arg1;
20142199Sahrens 	char *newsnapname = arg2;
20152199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
20162199Sahrens 	dsl_dataset_t *hds;
20172199Sahrens 	uint64_t val;
2018789Sahrens 	int err;
2019789Sahrens 
20206689Smaybee 	err = dsl_dataset_hold_obj(dd->dd_pool,
20216689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds);
2022789Sahrens 	if (err)
2023789Sahrens 		return (err);
2024789Sahrens 
20252199Sahrens 	/* new name better not be in use */
20266689Smaybee 	err = dsl_dataset_snap_lookup(hds, newsnapname, &val);
20276689Smaybee 	dsl_dataset_rele(hds, FTAG);
2028789Sahrens 
20292199Sahrens 	if (err == 0)
20302199Sahrens 		err = EEXIST;
20312199Sahrens 	else if (err == ENOENT)
20322199Sahrens 		err = 0;
20334007Smmusante 
20344007Smmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
20354007Smmusante 	if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
20364007Smmusante 		err = ENAMETOOLONG;
20374007Smmusante 
20382199Sahrens 	return (err);
20392199Sahrens }
2040789Sahrens 
20412199Sahrens static void
20424543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2,
20434543Smarks     cred_t *cr, dmu_tx_t *tx)
20442199Sahrens {
20452199Sahrens 	dsl_dataset_t *ds = arg1;
20464543Smarks 	const char *newsnapname = arg2;
20472199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
20482199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
20492199Sahrens 	dsl_dataset_t *hds;
20502199Sahrens 	int err;
2051789Sahrens 
20522199Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2053789Sahrens 
20546689Smaybee 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
20556689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
2056789Sahrens 
20572199Sahrens 	VERIFY(0 == dsl_dataset_get_snapname(ds));
20586689Smaybee 	err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
2059789Sahrens 	ASSERT3U(err, ==, 0);
20602199Sahrens 	mutex_enter(&ds->ds_lock);
20612199Sahrens 	(void) strcpy(ds->ds_snapname, newsnapname);
20622199Sahrens 	mutex_exit(&ds->ds_lock);
20632199Sahrens 	err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
20642199Sahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2065789Sahrens 	ASSERT3U(err, ==, 0);
2066789Sahrens 
20674543Smarks 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
20684543Smarks 	    cr, "dataset = %llu", ds->ds_object);
20696689Smaybee 	dsl_dataset_rele(hds, FTAG);
2070789Sahrens }
2071789Sahrens 
20725326Sek110237 struct renamesnaparg {
20734007Smmusante 	dsl_sync_task_group_t *dstg;
20744007Smmusante 	char failed[MAXPATHLEN];
20754007Smmusante 	char *oldsnap;
20764007Smmusante 	char *newsnap;
20774007Smmusante };
20784007Smmusante 
20794007Smmusante static int
20804007Smmusante dsl_snapshot_rename_one(char *name, void *arg)
20814007Smmusante {
20825326Sek110237 	struct renamesnaparg *ra = arg;
20834007Smmusante 	dsl_dataset_t *ds = NULL;
20844007Smmusante 	char *cp;
20854007Smmusante 	int err;
20864007Smmusante 
20874007Smmusante 	cp = name + strlen(name);
20884007Smmusante 	*cp = '@';
20894007Smmusante 	(void) strcpy(cp + 1, ra->oldsnap);
20904543Smarks 
20914543Smarks 	/*
20924543Smarks 	 * For recursive snapshot renames the parent won't be changing
20934543Smarks 	 * so we just pass name for both the to/from argument.
20944543Smarks 	 */
20957312SMatthew.Ahrens@Sun.COM 	err = zfs_secpolicy_rename_perms(name, name, CRED());
20967312SMatthew.Ahrens@Sun.COM 	if (err == ENOENT) {
20977312SMatthew.Ahrens@Sun.COM 		return (0);
20987312SMatthew.Ahrens@Sun.COM 	} else if (err) {
20994543Smarks 		(void) strcpy(ra->failed, name);
21004543Smarks 		return (err);
21014543Smarks 	}
21024543Smarks 
21036689Smaybee #ifdef _KERNEL
21046689Smaybee 	/*
21056689Smaybee 	 * For all filesystems undergoing rename, we'll need to unmount it.
21066689Smaybee 	 */
21076689Smaybee 	(void) zfs_unmount_snap(name, NULL);
21086689Smaybee #endif
21096689Smaybee 	err = dsl_dataset_hold(name, ra->dstg, &ds);
21106689Smaybee 	*cp = '\0';
21114007Smmusante 	if (err == ENOENT) {
21124007Smmusante 		return (0);
21136689Smaybee 	} else if (err) {
21144007Smmusante 		(void) strcpy(ra->failed, name);
21154007Smmusante 		return (err);
21164007Smmusante 	}
21174007Smmusante 
21184007Smmusante 	dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
21194007Smmusante 	    dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
21204007Smmusante 
21214007Smmusante 	return (0);
21224007Smmusante }
21234007Smmusante 
21244007Smmusante static int
21254007Smmusante dsl_recursive_rename(char *oldname, const char *newname)
21264007Smmusante {
21274007Smmusante 	int err;
21285326Sek110237 	struct renamesnaparg *ra;
21294007Smmusante 	dsl_sync_task_t *dst;
21304007Smmusante 	spa_t *spa;
21314007Smmusante 	char *cp, *fsname = spa_strdup(oldname);
21324007Smmusante 	int len = strlen(oldname);
21334007Smmusante 
21344007Smmusante 	/* truncate the snapshot name to get the fsname */
21354007Smmusante 	cp = strchr(fsname, '@');
21364007Smmusante 	*cp = '\0';
21374007Smmusante 
21384603Sahrens 	err = spa_open(fsname, &spa, FTAG);
21394007Smmusante 	if (err) {
21404007Smmusante 		kmem_free(fsname, len + 1);
21414007Smmusante 		return (err);
21424007Smmusante 	}
21435326Sek110237 	ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP);
21444007Smmusante 	ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
21454007Smmusante 
21464007Smmusante 	ra->oldsnap = strchr(oldname, '@') + 1;
21474007Smmusante 	ra->newsnap = strchr(newname, '@') + 1;
21484007Smmusante 	*ra->failed = '\0';
21494007Smmusante 
21504007Smmusante 	err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
21514007Smmusante 	    DS_FIND_CHILDREN);
21524007Smmusante 	kmem_free(fsname, len + 1);
21534007Smmusante 
21544007Smmusante 	if (err == 0) {
21554007Smmusante 		err = dsl_sync_task_group_wait(ra->dstg);
21564007Smmusante 	}
21574007Smmusante 
21584007Smmusante 	for (dst = list_head(&ra->dstg->dstg_tasks); dst;
21594007Smmusante 	    dst = list_next(&ra->dstg->dstg_tasks, dst)) {
21604007Smmusante 		dsl_dataset_t *ds = dst->dst_arg1;
21614007Smmusante 		if (dst->dst_err) {
21624007Smmusante 			dsl_dir_name(ds->ds_dir, ra->failed);
21634009Smmusante 			(void) strcat(ra->failed, "@");
21644009Smmusante 			(void) strcat(ra->failed, ra->newsnap);
21654007Smmusante 		}
21666689Smaybee 		dsl_dataset_rele(ds, ra->dstg);
21674007Smmusante 	}
21684007Smmusante 
21694543Smarks 	if (err)
21704543Smarks 		(void) strcpy(oldname, ra->failed);
21714007Smmusante 
21724007Smmusante 	dsl_sync_task_group_destroy(ra->dstg);
21735326Sek110237 	kmem_free(ra, sizeof (struct renamesnaparg));
21744007Smmusante 	spa_close(spa, FTAG);
21754007Smmusante 	return (err);
21764007Smmusante }
21774007Smmusante 
21784569Smmusante static int
21794569Smmusante dsl_valid_rename(char *oldname, void *arg)
21804569Smmusante {
21814569Smmusante 	int delta = *(int *)arg;
21824569Smmusante 
21834569Smmusante 	if (strlen(oldname) + delta >= MAXNAMELEN)
21844569Smmusante 		return (ENAMETOOLONG);
21854569Smmusante 
21864569Smmusante 	return (0);
21874569Smmusante }
21884569Smmusante 
2189789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename
2190789Sahrens int
21916689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive)
2192789Sahrens {
2193789Sahrens 	dsl_dir_t *dd;
21942199Sahrens 	dsl_dataset_t *ds;
2195789Sahrens 	const char *tail;
2196789Sahrens 	int err;
2197789Sahrens 
21982199Sahrens 	err = dsl_dir_open(oldname, FTAG, &dd, &tail);
21991544Seschrock 	if (err)
22001544Seschrock 		return (err);
2201789Sahrens 	if (tail == NULL) {
22024569Smmusante 		int delta = strlen(newname) - strlen(oldname);
22034569Smmusante 
22047046Sahrens 		/* if we're growing, validate child name lengths */
22054569Smmusante 		if (delta > 0)
22064569Smmusante 			err = dmu_objset_find(oldname, dsl_valid_rename,
22074569Smmusante 			    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
22084569Smmusante 
22094569Smmusante 		if (!err)
22104569Smmusante 			err = dsl_dir_rename(dd, newname);
2211789Sahrens 		dsl_dir_close(dd, FTAG);
2212789Sahrens 		return (err);
2213789Sahrens 	}
2214789Sahrens 	if (tail[0] != '@') {
2215789Sahrens 		/* the name ended in a nonexistant component */
2216789Sahrens 		dsl_dir_close(dd, FTAG);
2217789Sahrens 		return (ENOENT);
2218789Sahrens 	}
2219789Sahrens 
22202199Sahrens 	dsl_dir_close(dd, FTAG);
22212199Sahrens 
22222199Sahrens 	/* new name must be snapshot in same filesystem */
22232199Sahrens 	tail = strchr(newname, '@');
22242199Sahrens 	if (tail == NULL)
22252199Sahrens 		return (EINVAL);
22262199Sahrens 	tail++;
22272199Sahrens 	if (strncmp(oldname, newname, tail - newname) != 0)
22282199Sahrens 		return (EXDEV);
2229789Sahrens 
22304007Smmusante 	if (recursive) {
22314007Smmusante 		err = dsl_recursive_rename(oldname, newname);
22324007Smmusante 	} else {
22336689Smaybee 		err = dsl_dataset_hold(oldname, FTAG, &ds);
22344007Smmusante 		if (err)
22354007Smmusante 			return (err);
22362199Sahrens 
22374007Smmusante 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
22384007Smmusante 		    dsl_dataset_snapshot_rename_check,
22394007Smmusante 		    dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
22402199Sahrens 
22416689Smaybee 		dsl_dataset_rele(ds, FTAG);
22424007Smmusante 	}
22432199Sahrens 
2244789Sahrens 	return (err);
2245789Sahrens }
22462082Seschrock 
22477046Sahrens struct promotenode {
22486689Smaybee 	list_node_t link;
22496689Smaybee 	dsl_dataset_t *ds;
22506689Smaybee };
22516689Smaybee 
22522199Sahrens struct promotearg {
22537390SMatthew.Ahrens@Sun.COM 	list_t shared_snaps, origin_snaps, clone_snaps;
22547390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *origin_origin, *origin_head;
22557390SMatthew.Ahrens@Sun.COM 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
22562199Sahrens };
22572199Sahrens 
22587390SMatthew.Ahrens@Sun.COM static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
22597390SMatthew.Ahrens@Sun.COM 
22604543Smarks /* ARGSUSED */
22612082Seschrock static int
22622199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
22632082Seschrock {
22642199Sahrens 	dsl_dataset_t *hds = arg1;
22652199Sahrens 	struct promotearg *pa = arg2;
22667390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap = list_head(&pa->shared_snaps);
22676689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
22686689Smaybee 	int err;
22692199Sahrens 
22707046Sahrens 	/* Check that it is a real clone */
22717046Sahrens 	if (!dsl_dir_is_clone(hds->ds_dir))
22722082Seschrock 		return (EINVAL);
22732082Seschrock 
22742199Sahrens 	/* Since this is so expensive, don't do the preliminary check */
22752199Sahrens 	if (!dmu_tx_is_syncing(tx))
22762199Sahrens 		return (0);
22772199Sahrens 
22786689Smaybee 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)
22796689Smaybee 		return (EXDEV);
22802082Seschrock 
22815367Sahrens 	/* compute origin's new unique space */
22827390SMatthew.Ahrens@Sun.COM 	snap = list_tail(&pa->clone_snaps);
22837390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
22847390SMatthew.Ahrens@Sun.COM 	err = bplist_space_birthrange(&snap->ds->ds_deadlist,
22857390SMatthew.Ahrens@Sun.COM 	    origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, &pa->unique);
22867390SMatthew.Ahrens@Sun.COM 	if (err)
22876689Smaybee 		return (err);
22886689Smaybee 
22896689Smaybee 	/*
22906689Smaybee 	 * Walk the snapshots that we are moving
22916689Smaybee 	 *
22927390SMatthew.Ahrens@Sun.COM 	 * Compute space to transfer.  Consider the incremental changes
22937390SMatthew.Ahrens@Sun.COM 	 * to used for each snapshot:
22947390SMatthew.Ahrens@Sun.COM 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
22957390SMatthew.Ahrens@Sun.COM 	 * So each snapshot gave birth to:
22967390SMatthew.Ahrens@Sun.COM 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
22976689Smaybee 	 * So a sequence would look like:
22987390SMatthew.Ahrens@Sun.COM 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
22996689Smaybee 	 * Which simplifies to:
23007390SMatthew.Ahrens@Sun.COM 	 * uN + kN + kN-1 + ... + k1 + k0
23016689Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
23027390SMatthew.Ahrens@Sun.COM 	 * uN + kN + kN-1 + ... + kM - uM-1
23036689Smaybee 	 */
23046689Smaybee 	pa->used = origin_ds->ds_phys->ds_used_bytes;
23056689Smaybee 	pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
23066689Smaybee 	pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
23077390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(&pa->shared_snaps); snap;
23087390SMatthew.Ahrens@Sun.COM 	    snap = list_next(&pa->shared_snaps, snap)) {
23092082Seschrock 		uint64_t val, dlused, dlcomp, dluncomp;
23106689Smaybee 		dsl_dataset_t *ds = snap->ds;
23112082Seschrock 
23122082Seschrock 		/* Check that the snapshot name does not conflict */
23137390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_get_snapname(ds));
23146689Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
23156689Smaybee 		if (err == 0)
23167390SMatthew.Ahrens@Sun.COM 			return (EEXIST);
23176689Smaybee 		if (err != ENOENT)
23187390SMatthew.Ahrens@Sun.COM 			return (err);
23196689Smaybee 
23206689Smaybee 		/* The very first snapshot does not have a deadlist */
23217390SMatthew.Ahrens@Sun.COM 		if (ds->ds_phys->ds_prev_snap_obj == 0)
23227390SMatthew.Ahrens@Sun.COM 			continue;
23237390SMatthew.Ahrens@Sun.COM 
23247390SMatthew.Ahrens@Sun.COM 		if (err = bplist_space(&ds->ds_deadlist,
23257390SMatthew.Ahrens@Sun.COM 		    &dlused, &dlcomp, &dluncomp))
23267390SMatthew.Ahrens@Sun.COM 			return (err);
23277390SMatthew.Ahrens@Sun.COM 		pa->used += dlused;
23287390SMatthew.Ahrens@Sun.COM 		pa->comp += dlcomp;
23297390SMatthew.Ahrens@Sun.COM 		pa->uncomp += dluncomp;
23307390SMatthew.Ahrens@Sun.COM 	}
23316689Smaybee 
23326689Smaybee 	/*
23336689Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
23346689Smaybee 	 * so we need to subtract out the clone origin's used space.
23356689Smaybee 	 */
23367390SMatthew.Ahrens@Sun.COM 	if (pa->origin_origin) {
23377390SMatthew.Ahrens@Sun.COM 		pa->used -= pa->origin_origin->ds_phys->ds_used_bytes;
23387390SMatthew.Ahrens@Sun.COM 		pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes;
23397390SMatthew.Ahrens@Sun.COM 		pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes;
23402082Seschrock 	}
23412082Seschrock 
23422082Seschrock 	/* Check that there is enough space here */
23437390SMatthew.Ahrens@Sun.COM 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
23447390SMatthew.Ahrens@Sun.COM 	    pa->used);
23457390SMatthew.Ahrens@Sun.COM 	if (err)
23467390SMatthew.Ahrens@Sun.COM 		return (err);
23477390SMatthew.Ahrens@Sun.COM 
23487390SMatthew.Ahrens@Sun.COM 	/*
23497390SMatthew.Ahrens@Sun.COM 	 * Compute the amounts of space that will be used by snapshots
23507390SMatthew.Ahrens@Sun.COM 	 * after the promotion (for both origin and clone).  For each,
23517390SMatthew.Ahrens@Sun.COM 	 * it is the amount of space that will be on all of their
23527390SMatthew.Ahrens@Sun.COM 	 * deadlists (that was not born before their new origin).
23537390SMatthew.Ahrens@Sun.COM 	 */
23547390SMatthew.Ahrens@Sun.COM 	if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
23557390SMatthew.Ahrens@Sun.COM 		uint64_t space;
23567390SMatthew.Ahrens@Sun.COM 
23577390SMatthew.Ahrens@Sun.COM 		/*
23587390SMatthew.Ahrens@Sun.COM 		 * Note, typically this will not be a clone of a clone,
23597390SMatthew.Ahrens@Sun.COM 		 * so snap->ds->ds_origin_txg will be < TXG_INITIAL, so
23607390SMatthew.Ahrens@Sun.COM 		 * these snaplist_space() -> bplist_space_birthrange()
23617390SMatthew.Ahrens@Sun.COM 		 * calls will be fast because they do not have to
23627390SMatthew.Ahrens@Sun.COM 		 * iterate over all bps.
23637390SMatthew.Ahrens@Sun.COM 		 */
23647390SMatthew.Ahrens@Sun.COM 		snap = list_head(&pa->origin_snaps);
23657390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->shared_snaps,
23667390SMatthew.Ahrens@Sun.COM 		    snap->ds->ds_origin_txg, &pa->cloneusedsnap);
23677390SMatthew.Ahrens@Sun.COM 		if (err)
23687390SMatthew.Ahrens@Sun.COM 			return (err);
23697390SMatthew.Ahrens@Sun.COM 
23707390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->clone_snaps,
23717390SMatthew.Ahrens@Sun.COM 		    snap->ds->ds_origin_txg, &space);
23727390SMatthew.Ahrens@Sun.COM 		if (err)
23737390SMatthew.Ahrens@Sun.COM 			return (err);
23747390SMatthew.Ahrens@Sun.COM 		pa->cloneusedsnap += space;
23756689Smaybee 	}
23767390SMatthew.Ahrens@Sun.COM 	if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
23777390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->origin_snaps,
23787390SMatthew.Ahrens@Sun.COM 		    origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap);
23797390SMatthew.Ahrens@Sun.COM 		if (err)
23807390SMatthew.Ahrens@Sun.COM 			return (err);
23817390SMatthew.Ahrens@Sun.COM 	}
23827390SMatthew.Ahrens@Sun.COM 
23837390SMatthew.Ahrens@Sun.COM 	return (0);
23842199Sahrens }
23852082Seschrock 
23862199Sahrens static void
23874543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
23882199Sahrens {
23892199Sahrens 	dsl_dataset_t *hds = arg1;
23902199Sahrens 	struct promotearg *pa = arg2;
23917390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap = list_head(&pa->shared_snaps);
23926689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
23937390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *origin_head;
23942199Sahrens 	dsl_dir_t *dd = hds->ds_dir;
23952199Sahrens 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
23965367Sahrens 	dsl_dir_t *odd = NULL;
23977046Sahrens 	uint64_t oldnext_obj;
23987390SMatthew.Ahrens@Sun.COM 	int64_t delta;
23992199Sahrens 
24002199Sahrens 	ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
24012199Sahrens 
24027390SMatthew.Ahrens@Sun.COM 	snap = list_head(&pa->origin_snaps);
24037390SMatthew.Ahrens@Sun.COM 	origin_head = snap->ds;
24047390SMatthew.Ahrens@Sun.COM 
24052417Sahrens 	/*
24065367Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
24072417Sahrens 	 * changing.
24082417Sahrens 	 */
24095367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object,
24105367Sahrens 	    NULL, FTAG, &odd));
24112082Seschrock 
24126689Smaybee 	/* change origin's next snap */
24136689Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
24147046Sahrens 	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
24157390SMatthew.Ahrens@Sun.COM 	snap = list_tail(&pa->clone_snaps);
24167390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
24177390SMatthew.Ahrens@Sun.COM 	origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
24186689Smaybee 
24197046Sahrens 	/* change the origin's next clone */
24207046Sahrens 	if (origin_ds->ds_phys->ds_next_clones_obj) {
24217046Sahrens 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
24227046Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
24237390SMatthew.Ahrens@Sun.COM 		    origin_ds->ds_phys->ds_next_snap_obj, tx));
24247046Sahrens 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
24257046Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
24267046Sahrens 		    oldnext_obj, tx));
24277046Sahrens 	}
24287046Sahrens 
24296689Smaybee 	/* change origin */
24306689Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
24316689Smaybee 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
24326689Smaybee 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
24337390SMatthew.Ahrens@Sun.COM 	hds->ds_origin_txg = origin_head->ds_origin_txg;
24346689Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
24356689Smaybee 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
24367390SMatthew.Ahrens@Sun.COM 	origin_head->ds_origin_txg = origin_ds->ds_phys->ds_creation_txg;
24376689Smaybee 
24382082Seschrock 	/* move snapshots to this dir */
24397390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(&pa->shared_snaps); snap;
24407390SMatthew.Ahrens@Sun.COM 	    snap = list_next(&pa->shared_snaps, snap)) {
24416689Smaybee 		dsl_dataset_t *ds = snap->ds;
24422082Seschrock 
24437237Sek110237 		/* unregister props as dsl_dir is changing */
24447237Sek110237 		if (ds->ds_user_ptr) {
24457237Sek110237 			ds->ds_user_evict_func(ds, ds->ds_user_ptr);
24467237Sek110237 			ds->ds_user_ptr = NULL;
24477237Sek110237 		}
24482082Seschrock 		/* move snap name entry */
24497390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_get_snapname(ds));
24507390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_snap_remove(origin_head,
24516689Smaybee 		    ds->ds_snapname, tx));
24522199Sahrens 		VERIFY(0 == zap_add(dp->dp_meta_objset,
24532082Seschrock 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
24542082Seschrock 		    8, 1, &ds->ds_object, tx));
24552082Seschrock 		/* change containing dsl_dir */
24562082Seschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
24575367Sahrens 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
24582082Seschrock 		ds->ds_phys->ds_dir_obj = dd->dd_object;
24595367Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
24602082Seschrock 		dsl_dir_close(ds->ds_dir, ds);
24612199Sahrens 		VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
24622082Seschrock 		    NULL, ds, &ds->ds_dir));
24632082Seschrock 
24642082Seschrock 		ASSERT3U(dsl_prop_numcb(ds), ==, 0);
24657390SMatthew.Ahrens@Sun.COM 	}
24667390SMatthew.Ahrens@Sun.COM 
24677390SMatthew.Ahrens@Sun.COM 	/*
24687390SMatthew.Ahrens@Sun.COM 	 * Change space accounting.
24697390SMatthew.Ahrens@Sun.COM 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
24707390SMatthew.Ahrens@Sun.COM 	 * both be valid, or both be 0 (resulting in delta == 0).  This
24717390SMatthew.Ahrens@Sun.COM 	 * is true for each of {clone,origin} independently.
24727390SMatthew.Ahrens@Sun.COM 	 */
24737390SMatthew.Ahrens@Sun.COM 
24747390SMatthew.Ahrens@Sun.COM 	delta = pa->cloneusedsnap -
24757390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
24767390SMatthew.Ahrens@Sun.COM 	ASSERT3S(delta, >=, 0);
24777390SMatthew.Ahrens@Sun.COM 	ASSERT3U(pa->used, >=, delta);
24787390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
24797390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
24807390SMatthew.Ahrens@Sun.COM 	    pa->used - delta, pa->comp, pa->uncomp, tx);
24817390SMatthew.Ahrens@Sun.COM 
24827390SMatthew.Ahrens@Sun.COM 	delta = pa->originusedsnap -
24837390SMatthew.Ahrens@Sun.COM 	    odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
24847390SMatthew.Ahrens@Sun.COM 	ASSERT3S(delta, <=, 0);
24857390SMatthew.Ahrens@Sun.COM 	ASSERT3U(pa->used, >=, -delta);
24867390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
24877390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
24887390SMatthew.Ahrens@Sun.COM 	    -pa->used - delta, -pa->comp, -pa->uncomp, tx);
24897390SMatthew.Ahrens@Sun.COM 
24905367Sahrens 	origin_ds->ds_phys->ds_unique_bytes = pa->unique;
24912082Seschrock 
24924543Smarks 	/* log history record */
24934543Smarks 	spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
24946689Smaybee 	    cr, "dataset = %llu", hds->ds_object);
24954543Smarks 
24965367Sahrens 	dsl_dir_close(odd, FTAG);
24972082Seschrock }
24982082Seschrock 
24997390SMatthew.Ahrens@Sun.COM static char *snaplist_tag = "snaplist";
25007390SMatthew.Ahrens@Sun.COM /*
25017390SMatthew.Ahrens@Sun.COM  * Make a list of dsl_dataset_t's for the snapshots between first_obj
25027390SMatthew.Ahrens@Sun.COM  * (exclusive) and last_obj (inclusive).  The list will be in reverse
25037390SMatthew.Ahrens@Sun.COM  * order (last_obj will be the list_head()).  If first_obj == 0, do all
25047390SMatthew.Ahrens@Sun.COM  * snapshots back to this dataset's origin.
25057390SMatthew.Ahrens@Sun.COM  */
25067390SMatthew.Ahrens@Sun.COM static int
25077390SMatthew.Ahrens@Sun.COM snaplist_make(dsl_pool_t *dp, boolean_t own,
25087390SMatthew.Ahrens@Sun.COM     uint64_t first_obj, uint64_t last_obj, list_t *l)
25097390SMatthew.Ahrens@Sun.COM {
25107390SMatthew.Ahrens@Sun.COM 	uint64_t obj = last_obj;
25117390SMatthew.Ahrens@Sun.COM 
25127390SMatthew.Ahrens@Sun.COM 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock));
25137390SMatthew.Ahrens@Sun.COM 
25147390SMatthew.Ahrens@Sun.COM 	list_create(l, sizeof (struct promotenode),
25157390SMatthew.Ahrens@Sun.COM 	    offsetof(struct promotenode, link));
25167390SMatthew.Ahrens@Sun.COM 
25177390SMatthew.Ahrens@Sun.COM 	while (obj != first_obj) {
25187390SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
25197390SMatthew.Ahrens@Sun.COM 		struct promotenode *snap;
25207390SMatthew.Ahrens@Sun.COM 		int err;
25217390SMatthew.Ahrens@Sun.COM 
25227390SMatthew.Ahrens@Sun.COM 		if (own) {
25237390SMatthew.Ahrens@Sun.COM 			err = dsl_dataset_own_obj(dp, obj,
25247390SMatthew.Ahrens@Sun.COM 			    0, snaplist_tag, &ds);
25257390SMatthew.Ahrens@Sun.COM 			if (err == 0)
25267390SMatthew.Ahrens@Sun.COM 				dsl_dataset_make_exclusive(ds, snaplist_tag);
25277390SMatthew.Ahrens@Sun.COM 		} else {
25287390SMatthew.Ahrens@Sun.COM 			err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds);
25297390SMatthew.Ahrens@Sun.COM 		}
25307390SMatthew.Ahrens@Sun.COM 		if (err == ENOENT) {
25317390SMatthew.Ahrens@Sun.COM 			/* lost race with snapshot destroy */
25327390SMatthew.Ahrens@Sun.COM 			struct promotenode *last = list_tail(l);
25337390SMatthew.Ahrens@Sun.COM 			ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj);
25347390SMatthew.Ahrens@Sun.COM 			obj = last->ds->ds_phys->ds_prev_snap_obj;
25357390SMatthew.Ahrens@Sun.COM 			continue;
25367390SMatthew.Ahrens@Sun.COM 		} else if (err) {
25377390SMatthew.Ahrens@Sun.COM 			return (err);
25387390SMatthew.Ahrens@Sun.COM 		}
25397390SMatthew.Ahrens@Sun.COM 
25407390SMatthew.Ahrens@Sun.COM 		if (first_obj == 0)
25417390SMatthew.Ahrens@Sun.COM 			first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
25427390SMatthew.Ahrens@Sun.COM 
25437390SMatthew.Ahrens@Sun.COM 		snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP);
25447390SMatthew.Ahrens@Sun.COM 		snap->ds = ds;
25457390SMatthew.Ahrens@Sun.COM 		list_insert_tail(l, snap);
25467390SMatthew.Ahrens@Sun.COM 		obj = ds->ds_phys->ds_prev_snap_obj;
25477390SMatthew.Ahrens@Sun.COM 	}
25487390SMatthew.Ahrens@Sun.COM 
25497390SMatthew.Ahrens@Sun.COM 	return (0);
25507390SMatthew.Ahrens@Sun.COM }
25517390SMatthew.Ahrens@Sun.COM 
25527390SMatthew.Ahrens@Sun.COM static int
25537390SMatthew.Ahrens@Sun.COM snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
25547390SMatthew.Ahrens@Sun.COM {
25557390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap;
25567390SMatthew.Ahrens@Sun.COM 
25577390SMatthew.Ahrens@Sun.COM 	*spacep = 0;
25587390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
25597390SMatthew.Ahrens@Sun.COM 		uint64_t used;
25607390SMatthew.Ahrens@Sun.COM 		int err = bplist_space_birthrange(&snap->ds->ds_deadlist,
25617390SMatthew.Ahrens@Sun.COM 		    mintxg, UINT64_MAX, &used);
25627390SMatthew.Ahrens@Sun.COM 		if (err)
25637390SMatthew.Ahrens@Sun.COM 			return (err);
25647390SMatthew.Ahrens@Sun.COM 		*spacep += used;
25657390SMatthew.Ahrens@Sun.COM 	}
25667390SMatthew.Ahrens@Sun.COM 	return (0);
25677390SMatthew.Ahrens@Sun.COM }
25687390SMatthew.Ahrens@Sun.COM 
25697390SMatthew.Ahrens@Sun.COM static void
25707390SMatthew.Ahrens@Sun.COM snaplist_destroy(list_t *l, boolean_t own)
25717390SMatthew.Ahrens@Sun.COM {
25727390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap;
25737390SMatthew.Ahrens@Sun.COM 
25747390SMatthew.Ahrens@Sun.COM 	if (!list_link_active(&l->list_head))
25757390SMatthew.Ahrens@Sun.COM 		return;
25767390SMatthew.Ahrens@Sun.COM 
25777390SMatthew.Ahrens@Sun.COM 	while ((snap = list_tail(l)) != NULL) {
25787390SMatthew.Ahrens@Sun.COM 		list_remove(l, snap);
25797390SMatthew.Ahrens@Sun.COM 		if (own)
25807390SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(snap->ds, snaplist_tag);
25817390SMatthew.Ahrens@Sun.COM 		else
25827390SMatthew.Ahrens@Sun.COM 			dsl_dataset_rele(snap->ds, snaplist_tag);
25837390SMatthew.Ahrens@Sun.COM 		kmem_free(snap, sizeof (struct promotenode));
25847390SMatthew.Ahrens@Sun.COM 	}
25857390SMatthew.Ahrens@Sun.COM 	list_destroy(l);
25867390SMatthew.Ahrens@Sun.COM }
25877390SMatthew.Ahrens@Sun.COM 
25887390SMatthew.Ahrens@Sun.COM /*
25897390SMatthew.Ahrens@Sun.COM  * Promote a clone.  Nomenclature note:
25907390SMatthew.Ahrens@Sun.COM  * "clone" or "cds": the original clone which is being promoted
25917390SMatthew.Ahrens@Sun.COM  * "origin" or "ods": the snapshot which is originally clone's origin
25927390SMatthew.Ahrens@Sun.COM  * "origin head" or "ohds": the dataset which is the head
25937390SMatthew.Ahrens@Sun.COM  * (filesystem/volume) for the origin
25947390SMatthew.Ahrens@Sun.COM  * "origin origin": the origin of the origin's filesystem (typically
25957390SMatthew.Ahrens@Sun.COM  * NULL, indicating that the clone is not a clone of a clone).
25967390SMatthew.Ahrens@Sun.COM  */
25972082Seschrock int
25982082Seschrock dsl_dataset_promote(const char *name)
25992082Seschrock {
26002082Seschrock 	dsl_dataset_t *ds;
26016689Smaybee 	dsl_dir_t *dd;
26026689Smaybee 	dsl_pool_t *dp;
26032082Seschrock 	dmu_object_info_t doi;
26047390SMatthew.Ahrens@Sun.COM 	struct promotearg pa = { 0 };
26057046Sahrens 	struct promotenode *snap;
26066689Smaybee 	int err;
26076689Smaybee 
26086689Smaybee 	err = dsl_dataset_hold(name, FTAG, &ds);
26092082Seschrock 	if (err)
26102082Seschrock 		return (err);
26116689Smaybee 	dd = ds->ds_dir;
26126689Smaybee 	dp = dd->dd_pool;
26136689Smaybee 
26146689Smaybee 	err = dmu_object_info(dp->dp_meta_objset,
26152082Seschrock 	    ds->ds_phys->ds_snapnames_zapobj, &doi);
26162082Seschrock 	if (err) {
26176689Smaybee 		dsl_dataset_rele(ds, FTAG);
26182082Seschrock 		return (err);
26192082Seschrock 	}
26202082Seschrock 
26217390SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) {
26227390SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
26237390SMatthew.Ahrens@Sun.COM 		return (EINVAL);
26247390SMatthew.Ahrens@Sun.COM 	}
26257390SMatthew.Ahrens@Sun.COM 
26262082Seschrock 	/*
26276689Smaybee 	 * We are going to inherit all the snapshots taken before our
26286689Smaybee 	 * origin (i.e., our new origin will be our parent's origin).
26296689Smaybee 	 * Take ownership of them so that we can rename them into our
26306689Smaybee 	 * namespace.
26316689Smaybee 	 */
26326689Smaybee 	rw_enter(&dp->dp_config_rwlock, RW_READER);
26337390SMatthew.Ahrens@Sun.COM 
26347390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj,
26357390SMatthew.Ahrens@Sun.COM 	    &pa.shared_snaps);
26367390SMatthew.Ahrens@Sun.COM 	if (err != 0)
26377390SMatthew.Ahrens@Sun.COM 		goto out;
26387390SMatthew.Ahrens@Sun.COM 
26397390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps);
26407390SMatthew.Ahrens@Sun.COM 	if (err != 0)
26417390SMatthew.Ahrens@Sun.COM 		goto out;
26427390SMatthew.Ahrens@Sun.COM 
26437390SMatthew.Ahrens@Sun.COM 	snap = list_head(&pa.shared_snaps);
26447390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
26457390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj,
26467390SMatthew.Ahrens@Sun.COM 	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps);
26477390SMatthew.Ahrens@Sun.COM 	if (err != 0)
26487390SMatthew.Ahrens@Sun.COM 		goto out;
26497390SMatthew.Ahrens@Sun.COM 
26507390SMatthew.Ahrens@Sun.COM 	if (dsl_dir_is_clone(snap->ds->ds_dir)) {
26517390SMatthew.Ahrens@Sun.COM 		err = dsl_dataset_own_obj(dp,
26527390SMatthew.Ahrens@Sun.COM 		    snap->ds->ds_dir->dd_phys->dd_origin_obj,
26537390SMatthew.Ahrens@Sun.COM 		    0, FTAG, &pa.origin_origin);
26547390SMatthew.Ahrens@Sun.COM 		if (err != 0)
26556689Smaybee 			goto out;
26566689Smaybee 	}
26577390SMatthew.Ahrens@Sun.COM 
26587390SMatthew.Ahrens@Sun.COM out:
26596689Smaybee 	rw_exit(&dp->dp_config_rwlock);
26606689Smaybee 
26616689Smaybee 	/*
26622082Seschrock 	 * Add in 128x the snapnames zapobj size, since we will be moving
26632082Seschrock 	 * a bunch of snapnames to the promoted ds, and dirtying their
26642082Seschrock 	 * bonus buffers.
26652082Seschrock 	 */
26667390SMatthew.Ahrens@Sun.COM 	if (err == 0) {
26677390SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_do(dp, dsl_dataset_promote_check,
26687390SMatthew.Ahrens@Sun.COM 		    dsl_dataset_promote_sync, ds, &pa,
26697390SMatthew.Ahrens@Sun.COM 		    2 + 2 * doi.doi_physical_blks);
26706689Smaybee 	}
26717390SMatthew.Ahrens@Sun.COM 
26727390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.shared_snaps, B_TRUE);
26737390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.clone_snaps, B_FALSE);
26747390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.origin_snaps, B_FALSE);
26757390SMatthew.Ahrens@Sun.COM 	if (pa.origin_origin)
26767390SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(pa.origin_origin, FTAG);
26776689Smaybee 	dsl_dataset_rele(ds, FTAG);
26782082Seschrock 	return (err);
26792082Seschrock }
26803912Slling 
26815367Sahrens struct cloneswaparg {
26825367Sahrens 	dsl_dataset_t *cds; /* clone dataset */
26835367Sahrens 	dsl_dataset_t *ohds; /* origin's head dataset */
26845367Sahrens 	boolean_t force;
26855481Sck153898 	int64_t unused_refres_delta; /* change in unconsumed refreservation */
26865367Sahrens };
26875326Sek110237 
26885326Sek110237 /* ARGSUSED */
26895326Sek110237 static int
26905326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx)
26915326Sek110237 {
26925367Sahrens 	struct cloneswaparg *csa = arg1;
26935326Sek110237 
26945367Sahrens 	/* they should both be heads */
26955367Sahrens 	if (dsl_dataset_is_snapshot(csa->cds) ||
26965367Sahrens 	    dsl_dataset_is_snapshot(csa->ohds))
26975326Sek110237 		return (EINVAL);
26985326Sek110237 
26995367Sahrens 	/* the branch point should be just before them */
27005367Sahrens 	if (csa->cds->ds_prev != csa->ohds->ds_prev)
27015326Sek110237 		return (EINVAL);
27025326Sek110237 
27035367Sahrens 	/* cds should be the clone */
27045367Sahrens 	if (csa->cds->ds_prev->ds_phys->ds_next_snap_obj !=
27055367Sahrens 	    csa->ohds->ds_object)
27065367Sahrens 		return (EINVAL);
27075326Sek110237 
27085367Sahrens 	/* the clone should be a child of the origin */
27095367Sahrens 	if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir)
27105367Sahrens 		return (EINVAL);
27115326Sek110237 
27125367Sahrens 	/* ohds shouldn't be modified unless 'force' */
27135367Sahrens 	if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds))
27145367Sahrens 		return (ETXTBSY);
27155481Sck153898 
27165481Sck153898 	/* adjust amount of any unconsumed refreservation */
27175481Sck153898 	csa->unused_refres_delta =
27185481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
27195481Sck153898 	    csa->ohds->ds_phys->ds_unique_bytes) -
27205481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
27215481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
27225481Sck153898 
27235481Sck153898 	if (csa->unused_refres_delta > 0 &&
27245481Sck153898 	    csa->unused_refres_delta >
27255481Sck153898 	    dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE))
27265481Sck153898 		return (ENOSPC);
27275481Sck153898 
27285367Sahrens 	return (0);
27295326Sek110237 }
27305326Sek110237 
27315326Sek110237 /* ARGSUSED */
27325326Sek110237 static void
27335326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
27345326Sek110237 {
27355367Sahrens 	struct cloneswaparg *csa = arg1;
27365367Sahrens 	dsl_pool_t *dp = csa->cds->ds_dir->dd_pool;
27375326Sek110237 
27385481Sck153898 	ASSERT(csa->cds->ds_reserved == 0);
27395481Sck153898 	ASSERT(csa->cds->ds_quota == csa->ohds->ds_quota);
27405481Sck153898 
27415367Sahrens 	dmu_buf_will_dirty(csa->cds->ds_dbuf, tx);
27425367Sahrens 	dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx);
27435367Sahrens 	dmu_buf_will_dirty(csa->cds->ds_prev->ds_dbuf, tx);
27445326Sek110237 
27455367Sahrens 	if (csa->cds->ds_user_ptr != NULL) {
27465367Sahrens 		csa->cds->ds_user_evict_func(csa->cds, csa->cds->ds_user_ptr);
27475367Sahrens 		csa->cds->ds_user_ptr = NULL;
27485367Sahrens 	}
27495326Sek110237 
27505367Sahrens 	if (csa->ohds->ds_user_ptr != NULL) {
27515367Sahrens 		csa->ohds->ds_user_evict_func(csa->ohds,
27525367Sahrens 		    csa->ohds->ds_user_ptr);
27535367Sahrens 		csa->ohds->ds_user_ptr = NULL;
27545367Sahrens 	}
27555326Sek110237 
27565326Sek110237 	/* reset origin's unique bytes */
27577390SMatthew.Ahrens@Sun.COM 	VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist,
27587390SMatthew.Ahrens@Sun.COM 	    csa->cds->ds_prev->ds_phys->ds_prev_snap_txg, UINT64_MAX,
27597390SMatthew.Ahrens@Sun.COM 	    &csa->cds->ds_prev->ds_phys->ds_unique_bytes));
27605326Sek110237 
27615326Sek110237 	/* swap blkptrs */
27625326Sek110237 	{
27635326Sek110237 		blkptr_t tmp;
27645367Sahrens 		tmp = csa->ohds->ds_phys->ds_bp;
27655367Sahrens 		csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp;
27665367Sahrens 		csa->cds->ds_phys->ds_bp = tmp;
27675326Sek110237 	}
27685326Sek110237 
27695326Sek110237 	/* set dd_*_bytes */
27705326Sek110237 	{
27715326Sek110237 		int64_t dused, dcomp, duncomp;
27725326Sek110237 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
27735326Sek110237 		uint64_t odl_used, odl_comp, odl_uncomp;
27745326Sek110237 
27757390SMatthew.Ahrens@Sun.COM 		ASSERT3U(csa->cds->ds_dir->dd_phys->
27767390SMatthew.Ahrens@Sun.COM 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
27777390SMatthew.Ahrens@Sun.COM 
27785367Sahrens 		VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used,
27795326Sek110237 		    &cdl_comp, &cdl_uncomp));
27805367Sahrens 		VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used,
27815326Sek110237 		    &odl_comp, &odl_uncomp));
27827390SMatthew.Ahrens@Sun.COM 
27835367Sahrens 		dused = csa->cds->ds_phys->ds_used_bytes + cdl_used -
27845367Sahrens 		    (csa->ohds->ds_phys->ds_used_bytes + odl_used);
27855367Sahrens 		dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
27865367Sahrens 		    (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
27875367Sahrens 		duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
27885367Sahrens 		    cdl_uncomp -
27895367Sahrens 		    (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
27905326Sek110237 
27917390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD,
27925367Sahrens 		    dused, dcomp, duncomp, tx);
27937390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD,
27945367Sahrens 		    -dused, -dcomp, -duncomp, tx);
27957390SMatthew.Ahrens@Sun.COM 
27967390SMatthew.Ahrens@Sun.COM 		/*
27977390SMatthew.Ahrens@Sun.COM 		 * The difference in the space used by snapshots is the
27987390SMatthew.Ahrens@Sun.COM 		 * difference in snapshot space due to the head's
27997390SMatthew.Ahrens@Sun.COM 		 * deadlist (since that's the only thing that's
28007390SMatthew.Ahrens@Sun.COM 		 * changing that affects the snapused).
28017390SMatthew.Ahrens@Sun.COM 		 */
28027390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist,
28037390SMatthew.Ahrens@Sun.COM 		    csa->ohds->ds_origin_txg, UINT64_MAX, &cdl_used));
28047390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->ohds->ds_deadlist,
28057390SMatthew.Ahrens@Sun.COM 		    csa->ohds->ds_origin_txg, UINT64_MAX, &odl_used));
28067390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used,
28077390SMatthew.Ahrens@Sun.COM 		    DD_USED_HEAD, DD_USED_SNAP, tx);
28085367Sahrens 	}
28095367Sahrens 
28105367Sahrens #define	SWITCH64(x, y) \
28115367Sahrens 	{ \
28125367Sahrens 		uint64_t __tmp = (x); \
28135367Sahrens 		(x) = (y); \
28145367Sahrens 		(y) = __tmp; \
28155326Sek110237 	}
28165326Sek110237 
28175326Sek110237 	/* swap ds_*_bytes */
28185367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_used_bytes,
28195367Sahrens 	    csa->cds->ds_phys->ds_used_bytes);
28205367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
28215367Sahrens 	    csa->cds->ds_phys->ds_compressed_bytes);
28225367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
28235367Sahrens 	    csa->cds->ds_phys->ds_uncompressed_bytes);
28245481Sck153898 	SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
28255481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
28265481Sck153898 
28275481Sck153898 	/* apply any parent delta for change in unconsumed refreservation */
28287390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV,
28297390SMatthew.Ahrens@Sun.COM 	    csa->unused_refres_delta, 0, 0, tx);
28305326Sek110237 
28315326Sek110237 	/* swap deadlists */
28325367Sahrens 	bplist_close(&csa->cds->ds_deadlist);
28335367Sahrens 	bplist_close(&csa->ohds->ds_deadlist);
28345367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
28355367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj);
28365367Sahrens 	VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
28375367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj));
28385367Sahrens 	VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
28395367Sahrens 	    csa->ohds->ds_phys->ds_deadlist_obj));
28405326Sek110237 }
28415326Sek110237 
28425326Sek110237 /*
28436689Smaybee  * Swap 'clone' with its origin head file system.  Used at the end
28446689Smaybee  * of "online recv" to swizzle the file system to the new version.
28455326Sek110237  */
28465326Sek110237 int
28475367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
28485367Sahrens     boolean_t force)
28495326Sek110237 {
28505367Sahrens 	struct cloneswaparg csa;
28516689Smaybee 	int error;
28526689Smaybee 
28536689Smaybee 	ASSERT(clone->ds_owner);
28546689Smaybee 	ASSERT(origin_head->ds_owner);
28556689Smaybee retry:
28566689Smaybee 	/* Need exclusive access for the swap */
28576689Smaybee 	rw_enter(&clone->ds_rwlock, RW_WRITER);
28586689Smaybee 	if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) {
28596689Smaybee 		rw_exit(&clone->ds_rwlock);
28606689Smaybee 		rw_enter(&origin_head->ds_rwlock, RW_WRITER);
28616689Smaybee 		if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) {
28626689Smaybee 			rw_exit(&origin_head->ds_rwlock);
28636689Smaybee 			goto retry;
28646689Smaybee 		}
28656689Smaybee 	}
28665367Sahrens 	csa.cds = clone;
28675367Sahrens 	csa.ohds = origin_head;
28685367Sahrens 	csa.force = force;
28696689Smaybee 	error = dsl_sync_task_do(clone->ds_dir->dd_pool,
28705326Sek110237 	    dsl_dataset_clone_swap_check,
28716689Smaybee 	    dsl_dataset_clone_swap_sync, &csa, NULL, 9);
28726689Smaybee 	return (error);
28735326Sek110237 }
28745326Sek110237 
28753912Slling /*
28763912Slling  * Given a pool name and a dataset object number in that pool,
28773912Slling  * return the name of that dataset.
28783912Slling  */
28793912Slling int
28803912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
28813912Slling {
28823912Slling 	spa_t *spa;
28833912Slling 	dsl_pool_t *dp;
28846689Smaybee 	dsl_dataset_t *ds;
28853912Slling 	int error;
28863912Slling 
28873912Slling 	if ((error = spa_open(pname, &spa, FTAG)) != 0)
28883912Slling 		return (error);
28893912Slling 	dp = spa_get_dsl(spa);
28903912Slling 	rw_enter(&dp->dp_config_rwlock, RW_READER);
28916689Smaybee 	if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) {
28926689Smaybee 		dsl_dataset_name(ds, buf);
28936689Smaybee 		dsl_dataset_rele(ds, FTAG);
28943912Slling 	}
28953912Slling 	rw_exit(&dp->dp_config_rwlock);
28963912Slling 	spa_close(spa, FTAG);
28973912Slling 
28986689Smaybee 	return (error);
28993912Slling }
29005378Sck153898 
29015378Sck153898 int
29025378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
29036689Smaybee     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
29045378Sck153898 {
29055378Sck153898 	int error = 0;
29065378Sck153898 
29075378Sck153898 	ASSERT3S(asize, >, 0);
29085378Sck153898 
29095831Sck153898 	/*
29105831Sck153898 	 * *ref_rsrv is the portion of asize that will come from any
29115831Sck153898 	 * unconsumed refreservation space.
29125831Sck153898 	 */
29135831Sck153898 	*ref_rsrv = 0;
29145831Sck153898 
29155378Sck153898 	mutex_enter(&ds->ds_lock);
29165378Sck153898 	/*
29175378Sck153898 	 * Make a space adjustment for reserved bytes.
29185378Sck153898 	 */
29195378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
29205378Sck153898 		ASSERT3U(*used, >=,
29215378Sck153898 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
29225378Sck153898 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
29235831Sck153898 		*ref_rsrv =
29245831Sck153898 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
29255378Sck153898 	}
29265378Sck153898 
29275378Sck153898 	if (!check_quota || ds->ds_quota == 0) {
29285378Sck153898 		mutex_exit(&ds->ds_lock);
29295378Sck153898 		return (0);
29305378Sck153898 	}
29315378Sck153898 	/*
29325378Sck153898 	 * If they are requesting more space, and our current estimate
29335378Sck153898 	 * is over quota, they get to try again unless the actual
29345378Sck153898 	 * on-disk is over quota and there are no pending changes (which
29355378Sck153898 	 * may free up space for us).
29365378Sck153898 	 */
29375378Sck153898 	if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) {
29385378Sck153898 		if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota)
29395378Sck153898 			error = ERESTART;
29405378Sck153898 		else
29415378Sck153898 			error = EDQUOT;
29425378Sck153898 	}
29435378Sck153898 	mutex_exit(&ds->ds_lock);
29445378Sck153898 
29455378Sck153898 	return (error);
29465378Sck153898 }
29475378Sck153898 
29485378Sck153898 /* ARGSUSED */
29495378Sck153898 static int
29505378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
29515378Sck153898 {
29525378Sck153898 	dsl_dataset_t *ds = arg1;
29535378Sck153898 	uint64_t *quotap = arg2;
29545378Sck153898 	uint64_t new_quota = *quotap;
29555378Sck153898 
29565378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA)
29575378Sck153898 		return (ENOTSUP);
29585378Sck153898 
29595378Sck153898 	if (new_quota == 0)
29605378Sck153898 		return (0);
29615378Sck153898 
29625378Sck153898 	if (new_quota < ds->ds_phys->ds_used_bytes ||
29635378Sck153898 	    new_quota < ds->ds_reserved)
29645378Sck153898 		return (ENOSPC);
29655378Sck153898 
29665378Sck153898 	return (0);
29675378Sck153898 }
29685378Sck153898 
29695378Sck153898 /* ARGSUSED */
29705378Sck153898 void
29715378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
29725378Sck153898 {
29735378Sck153898 	dsl_dataset_t *ds = arg1;
29745378Sck153898 	uint64_t *quotap = arg2;
29755378Sck153898 	uint64_t new_quota = *quotap;
29765378Sck153898 
29775378Sck153898 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
29785378Sck153898 
29795378Sck153898 	ds->ds_quota = new_quota;
29805378Sck153898 
29815378Sck153898 	dsl_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx);
29825378Sck153898 
29835378Sck153898 	spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa,
29845378Sck153898 	    tx, cr, "%lld dataset = %llu ",
29856689Smaybee 	    (longlong_t)new_quota, ds->ds_object);
29865378Sck153898 }
29875378Sck153898 
29885378Sck153898 int
29895378Sck153898 dsl_dataset_set_quota(const char *dsname, uint64_t quota)
29905378Sck153898 {
29915378Sck153898 	dsl_dataset_t *ds;
29925378Sck153898 	int err;
29935378Sck153898 
29946689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
29955378Sck153898 	if (err)
29965378Sck153898 		return (err);
29975378Sck153898 
29985481Sck153898 	if (quota != ds->ds_quota) {
29995481Sck153898 		/*
30005481Sck153898 		 * If someone removes a file, then tries to set the quota, we
30015481Sck153898 		 * want to make sure the file freeing takes effect.
30025481Sck153898 		 */
30035481Sck153898 		txg_wait_open(ds->ds_dir->dd_pool, 0);
30045481Sck153898 
30055481Sck153898 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
30065481Sck153898 		    dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync,
30075481Sck153898 		    ds, &quota, 0);
30085481Sck153898 	}
30096689Smaybee 	dsl_dataset_rele(ds, FTAG);
30105378Sck153898 	return (err);
30115378Sck153898 }
30125378Sck153898 
30135378Sck153898 static int
30145378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
30155378Sck153898 {
30165378Sck153898 	dsl_dataset_t *ds = arg1;
30175378Sck153898 	uint64_t *reservationp = arg2;
30185378Sck153898 	uint64_t new_reservation = *reservationp;
30195378Sck153898 	int64_t delta;
30205378Sck153898 	uint64_t unique;
30215378Sck153898 
30225378Sck153898 	if (new_reservation > INT64_MAX)
30235378Sck153898 		return (EOVERFLOW);
30245378Sck153898 
30255378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
30265378Sck153898 	    SPA_VERSION_REFRESERVATION)
30275378Sck153898 		return (ENOTSUP);
30285378Sck153898 
30295378Sck153898 	if (dsl_dataset_is_snapshot(ds))
30305378Sck153898 		return (EINVAL);
30315378Sck153898 
30325378Sck153898 	/*
30335378Sck153898 	 * If we are doing the preliminary check in open context, the
30345378Sck153898 	 * space estimates may be inaccurate.
30355378Sck153898 	 */
30365378Sck153898 	if (!dmu_tx_is_syncing(tx))
30375378Sck153898 		return (0);
30385378Sck153898 
30395378Sck153898 	mutex_enter(&ds->ds_lock);
30405378Sck153898 	unique = dsl_dataset_unique(ds);
30415378Sck153898 	delta = MAX(unique, new_reservation) - MAX(unique, ds->ds_reserved);
30425378Sck153898 	mutex_exit(&ds->ds_lock);
30435378Sck153898 
30445378Sck153898 	if (delta > 0 &&
30455378Sck153898 	    delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
30465378Sck153898 		return (ENOSPC);
30475378Sck153898 	if (delta > 0 && ds->ds_quota > 0 &&
30485378Sck153898 	    new_reservation > ds->ds_quota)
30495378Sck153898 		return (ENOSPC);
30505378Sck153898 
30515378Sck153898 	return (0);
30525378Sck153898 }
30535378Sck153898 
30545378Sck153898 /* ARGSUSED */
30555378Sck153898 static void
30565378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr,
30575378Sck153898     dmu_tx_t *tx)
30585378Sck153898 {
30595378Sck153898 	dsl_dataset_t *ds = arg1;
30605378Sck153898 	uint64_t *reservationp = arg2;
30615378Sck153898 	uint64_t new_reservation = *reservationp;
30627595SMatthew.Ahrens@Sun.COM 	uint64_t unique;
30637595SMatthew.Ahrens@Sun.COM 	int64_t delta;
30647595SMatthew.Ahrens@Sun.COM 
30657595SMatthew.Ahrens@Sun.COM 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
30667595SMatthew.Ahrens@Sun.COM 
30677595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_dir->dd_lock);
30687595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_lock);
30697595SMatthew.Ahrens@Sun.COM 	unique = dsl_dataset_unique(ds);
30707595SMatthew.Ahrens@Sun.COM 	delta = MAX(0, (int64_t)(new_reservation - unique)) -
30717595SMatthew.Ahrens@Sun.COM 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
30727595SMatthew.Ahrens@Sun.COM 	ds->ds_reserved = new_reservation;
30737595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_lock);
30747595SMatthew.Ahrens@Sun.COM 
30757595SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
30767595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_dir->dd_lock);
30777595SMatthew.Ahrens@Sun.COM 	dsl_prop_set_uint64_sync(ds->ds_dir, "refreservation",
30787595SMatthew.Ahrens@Sun.COM 	    new_reservation, cr, tx);
30797595SMatthew.Ahrens@Sun.COM 
30807595SMatthew.Ahrens@Sun.COM 	spa_history_internal_log(LOG_DS_REFRESERV,
30817595SMatthew.Ahrens@Sun.COM 	    ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu",
30827595SMatthew.Ahrens@Sun.COM 	    (longlong_t)new_reservation, ds->ds_object);
30835378Sck153898 }
30845378Sck153898 
30855378Sck153898 int
30865378Sck153898 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation)
30875378Sck153898 {
30885378Sck153898 	dsl_dataset_t *ds;
30895378Sck153898 	int err;
30905378Sck153898 
30916689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
30925378Sck153898 	if (err)
30935378Sck153898 		return (err);
30945378Sck153898 
30955378Sck153898 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
30965378Sck153898 	    dsl_dataset_set_reservation_check,
30975378Sck153898 	    dsl_dataset_set_reservation_sync, ds, &reservation, 0);
30986689Smaybee 	dsl_dataset_rele(ds, FTAG);
30995378Sck153898 	return (err);
31005378Sck153898 }
3101