xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision 12295:e16f396f04a1)
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 /*
2212115SChris.Kirby@oracle.com  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens #include <sys/dmu_objset.h>
26789Sahrens #include <sys/dsl_dataset.h>
27789Sahrens #include <sys/dsl_dir.h>
282082Seschrock #include <sys/dsl_prop.h>
292199Sahrens #include <sys/dsl_synctask.h>
30789Sahrens #include <sys/dmu_traverse.h>
31789Sahrens #include <sys/dmu_tx.h>
32789Sahrens #include <sys/arc.h>
33789Sahrens #include <sys/zio.h>
34789Sahrens #include <sys/zap.h>
35789Sahrens #include <sys/unique.h>
36789Sahrens #include <sys/zfs_context.h>
374007Smmusante #include <sys/zfs_ioctl.h>
384543Smarks #include <sys/spa.h>
397046Sahrens #include <sys/zfs_znode.h>
4010242Schris.kirby@sun.com #include <sys/zvol.h>
41789Sahrens 
426689Smaybee static char *dsl_reaper = "the grim reaper";
436689Smaybee 
442199Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check;
452199Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync;
465378Sck153898 static dsl_syncfunc_t dsl_dataset_set_reservation_sync;
471731Sbonwick 
483444Sek110237 #define	DS_REF_MAX	(1ULL << 62)
49789Sahrens 
50789Sahrens #define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
51789Sahrens 
526689Smaybee #define	DSL_DATASET_IS_DESTROYED(ds)	((ds)->ds_owner == dsl_reaper)
536689Smaybee 
54789Sahrens 
555378Sck153898 /*
565378Sck153898  * Figure out how much of this delta should be propogated to the dsl_dir
575378Sck153898  * layer.  If there's a refreservation, that space has already been
585378Sck153898  * partially accounted for in our ancestors.
595378Sck153898  */
605378Sck153898 static int64_t
615378Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta)
625378Sck153898 {
635378Sck153898 	uint64_t old_bytes, new_bytes;
645378Sck153898 
655378Sck153898 	if (ds->ds_reserved == 0)
665378Sck153898 		return (delta);
675378Sck153898 
685378Sck153898 	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
695378Sck153898 	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
705378Sck153898 
715378Sck153898 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
725378Sck153898 	return (new_bytes - old_bytes);
735378Sck153898 }
74789Sahrens 
75789Sahrens void
7610922SJeff.Bonwick@Sun.COM dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
77789Sahrens {
7810922SJeff.Bonwick@Sun.COM 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
79789Sahrens 	int compressed = BP_GET_PSIZE(bp);
80789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
815378Sck153898 	int64_t delta;
82789Sahrens 
83789Sahrens 	dprintf_bp(bp, "born, ds=%p\n", ds);
84789Sahrens 
85789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
86789Sahrens 	/* It could have been compressed away to nothing */
87789Sahrens 	if (BP_IS_HOLE(bp))
88789Sahrens 		return;
89789Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
90789Sahrens 	ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES);
91789Sahrens 	if (ds == NULL) {
92789Sahrens 		/*
93789Sahrens 		 * Account for the meta-objset space in its placeholder
94789Sahrens 		 * dsl_dir.
95789Sahrens 		 */
96789Sahrens 		ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */
977390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
98789Sahrens 		    used, compressed, uncompressed, tx);
99789Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
100789Sahrens 		return;
101789Sahrens 	}
102789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1037595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_dir->dd_lock);
104789Sahrens 	mutex_enter(&ds->ds_lock);
1055378Sck153898 	delta = parent_delta(ds, used);
106789Sahrens 	ds->ds_phys->ds_used_bytes += used;
107789Sahrens 	ds->ds_phys->ds_compressed_bytes += compressed;
108789Sahrens 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
109789Sahrens 	ds->ds_phys->ds_unique_bytes += used;
110789Sahrens 	mutex_exit(&ds->ds_lock);
1117390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
1127390SMatthew.Ahrens@Sun.COM 	    compressed, uncompressed, tx);
1137390SMatthew.Ahrens@Sun.COM 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
1147390SMatthew.Ahrens@Sun.COM 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
1157595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_dir->dd_lock);
116789Sahrens }
117789Sahrens 
1186992Smaybee int
11910922SJeff.Bonwick@Sun.COM dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
12010922SJeff.Bonwick@Sun.COM     boolean_t async)
121789Sahrens {
12210922SJeff.Bonwick@Sun.COM 	if (BP_IS_HOLE(bp))
12310922SJeff.Bonwick@Sun.COM 		return (0);
12410922SJeff.Bonwick@Sun.COM 
12510922SJeff.Bonwick@Sun.COM 	ASSERT(dmu_tx_is_syncing(tx));
12610922SJeff.Bonwick@Sun.COM 	ASSERT(bp->blk_birth <= tx->tx_txg);
12710922SJeff.Bonwick@Sun.COM 
12810922SJeff.Bonwick@Sun.COM 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
129789Sahrens 	int compressed = BP_GET_PSIZE(bp);
130789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
131789Sahrens 
132789Sahrens 	ASSERT(used > 0);
133789Sahrens 	if (ds == NULL) {
134789Sahrens 		/*
135789Sahrens 		 * Account for the meta-objset space in its placeholder
136789Sahrens 		 * dataset.
137789Sahrens 		 */
13810922SJeff.Bonwick@Sun.COM 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
139789Sahrens 
1407390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
141789Sahrens 		    -used, -compressed, -uncompressed, tx);
142789Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
1436992Smaybee 		return (used);
144789Sahrens 	}
145789Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
146789Sahrens 
1477390SMatthew.Ahrens@Sun.COM 	ASSERT(!dsl_dataset_is_snapshot(ds));
148789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
149789Sahrens 
150789Sahrens 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
1515378Sck153898 		int64_t delta;
1523547Smaybee 
153789Sahrens 		dprintf_bp(bp, "freeing: %s", "");
15410922SJeff.Bonwick@Sun.COM 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
155789Sahrens 
1567595SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_dir->dd_lock);
157789Sahrens 		mutex_enter(&ds->ds_lock);
1585378Sck153898 		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
1595378Sck153898 		    !DS_UNIQUE_IS_ACCURATE(ds));
1605378Sck153898 		delta = parent_delta(ds, -used);
161789Sahrens 		ds->ds_phys->ds_unique_bytes -= used;
162789Sahrens 		mutex_exit(&ds->ds_lock);
1637390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
1645378Sck153898 		    delta, -compressed, -uncompressed, tx);
1657390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
1667390SMatthew.Ahrens@Sun.COM 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
1677595SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_dir->dd_lock);
168789Sahrens 	} else {
169789Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
17010922SJeff.Bonwick@Sun.COM 		if (async) {
17110922SJeff.Bonwick@Sun.COM 			/*
17210922SJeff.Bonwick@Sun.COM 			 * We are here as part of zio's write done callback,
17310922SJeff.Bonwick@Sun.COM 			 * which means we're a zio interrupt thread.  We can't
17410922SJeff.Bonwick@Sun.COM 			 * call bplist_enqueue() now because it may block
17510922SJeff.Bonwick@Sun.COM 			 * waiting for I/O.  Instead, put bp on the deferred
17610922SJeff.Bonwick@Sun.COM 			 * queue and let dsl_pool_sync() finish the job.
17710922SJeff.Bonwick@Sun.COM 			 */
17810922SJeff.Bonwick@Sun.COM 			bplist_enqueue_deferred(&ds->ds_deadlist, bp);
17910922SJeff.Bonwick@Sun.COM 		} else {
18010922SJeff.Bonwick@Sun.COM 			VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx));
18110922SJeff.Bonwick@Sun.COM 		}
1825712Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
1835712Sahrens 		    ds->ds_phys->ds_prev_snap_obj);
1845712Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
185789Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
1865712Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
1875712Sahrens 		    ds->ds_object && bp->blk_birth >
1885712Sahrens 		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
1895712Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1905712Sahrens 			mutex_enter(&ds->ds_prev->ds_lock);
1915712Sahrens 			ds->ds_prev->ds_phys->ds_unique_bytes += used;
1925712Sahrens 			mutex_exit(&ds->ds_prev->ds_lock);
193789Sahrens 		}
1947390SMatthew.Ahrens@Sun.COM 		if (bp->blk_birth > ds->ds_origin_txg) {
1957390SMatthew.Ahrens@Sun.COM 			dsl_dir_transfer_space(ds->ds_dir, used,
1967390SMatthew.Ahrens@Sun.COM 			    DD_USED_HEAD, DD_USED_SNAP, tx);
1977390SMatthew.Ahrens@Sun.COM 		}
198789Sahrens 	}
199789Sahrens 	mutex_enter(&ds->ds_lock);
200789Sahrens 	ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used);
201789Sahrens 	ds->ds_phys->ds_used_bytes -= used;
202789Sahrens 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
203789Sahrens 	ds->ds_phys->ds_compressed_bytes -= compressed;
204789Sahrens 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
205789Sahrens 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
206789Sahrens 	mutex_exit(&ds->ds_lock);
2076992Smaybee 
2086992Smaybee 	return (used);
209789Sahrens }
210789Sahrens 
2111544Seschrock uint64_t
2121544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
213789Sahrens {
2142885Sahrens 	uint64_t trysnap = 0;
2152885Sahrens 
216789Sahrens 	if (ds == NULL)
2171544Seschrock 		return (0);
218789Sahrens 	/*
219789Sahrens 	 * The snapshot creation could fail, but that would cause an
220789Sahrens 	 * incorrect FALSE return, which would only result in an
221789Sahrens 	 * overestimation of the amount of space that an operation would
222789Sahrens 	 * consume, which is OK.
223789Sahrens 	 *
224789Sahrens 	 * There's also a small window where we could miss a pending
225789Sahrens 	 * snapshot, because we could set the sync task in the quiescing
226789Sahrens 	 * phase.  So this should only be used as a guess.
227789Sahrens 	 */
2282885Sahrens 	if (ds->ds_trysnap_txg >
2292885Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
2302885Sahrens 		trysnap = ds->ds_trysnap_txg;
2312885Sahrens 	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
2321544Seschrock }
2331544Seschrock 
2349653SSanjeev.Bagewadi@Sun.COM boolean_t
2351544Seschrock dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth)
2361544Seschrock {
2371544Seschrock 	return (blk_birth > dsl_dataset_prev_snap_txg(ds));
238789Sahrens }
239789Sahrens 
240789Sahrens /* ARGSUSED */
241789Sahrens static void
242789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv)
243789Sahrens {
244789Sahrens 	dsl_dataset_t *ds = dsv;
245789Sahrens 
2466689Smaybee 	ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds));
247789Sahrens 
2484787Sahrens 	unique_remove(ds->ds_fsid_guid);
249789Sahrens 
25010298SMatthew.Ahrens@Sun.COM 	if (ds->ds_objset != NULL)
25110298SMatthew.Ahrens@Sun.COM 		dmu_objset_evict(ds->ds_objset);
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);
26510204SMatthew.Ahrens@Sun.COM 	mutex_destroy(&ds->ds_recvlock);
2664787Sahrens 	mutex_destroy(&ds->ds_opening_lock);
2676689Smaybee 	rw_destroy(&ds->ds_rwlock);
2686689Smaybee 	cv_destroy(&ds->ds_exclusive_cv);
26910922SJeff.Bonwick@Sun.COM 	bplist_fini(&ds->ds_deadlist);
2702856Snd150628 
271789Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
272789Sahrens }
273789Sahrens 
2741544Seschrock static int
275789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
276789Sahrens {
277789Sahrens 	dsl_dataset_phys_t *headphys;
278789Sahrens 	int err;
279789Sahrens 	dmu_buf_t *headdbuf;
280789Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
281789Sahrens 	objset_t *mos = dp->dp_meta_objset;
282789Sahrens 
283789Sahrens 	if (ds->ds_snapname[0])
2841544Seschrock 		return (0);
285789Sahrens 	if (ds->ds_phys->ds_next_snap_obj == 0)
2861544Seschrock 		return (0);
287789Sahrens 
2881544Seschrock 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
2891544Seschrock 	    FTAG, &headdbuf);
2901544Seschrock 	if (err)
2911544Seschrock 		return (err);
292789Sahrens 	headphys = headdbuf->db_data;
293789Sahrens 	err = zap_value_search(dp->dp_meta_objset,
2944577Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
2951544Seschrock 	dmu_buf_rele(headdbuf, FTAG);
2961544Seschrock 	return (err);
297789Sahrens }
298789Sahrens 
2996492Stimh static int
3006689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
3016492Stimh {
3026689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3036689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
3046492Stimh 	matchtype_t mt;
3056492Stimh 	int err;
3066492Stimh 
3076689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
3086492Stimh 		mt = MT_FIRST;
3096492Stimh 	else
3106492Stimh 		mt = MT_EXACT;
3116492Stimh 
3126689Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
3136492Stimh 	    value, mt, NULL, 0, NULL);
3146492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
3156689Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
3166492Stimh 	return (err);
3176492Stimh }
3186492Stimh 
3196492Stimh static int
3206689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx)
3216492Stimh {
3226689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3236689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
3246492Stimh 	matchtype_t mt;
3256492Stimh 	int err;
3266492Stimh 
32710373Schris.kirby@sun.com 	dsl_dir_snap_cmtime_update(ds->ds_dir);
32810373Schris.kirby@sun.com 
3296689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
3306492Stimh 		mt = MT_FIRST;
3316492Stimh 	else
3326492Stimh 		mt = MT_EXACT;
3336492Stimh 
3346689Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
3356492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
3366689Smaybee 		err = zap_remove(mos, snapobj, name, tx);
3376492Stimh 	return (err);
3386492Stimh }
3396492Stimh 
3406689Smaybee static int
3416689Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag,
3426689Smaybee     dsl_dataset_t **dsp)
343789Sahrens {
344789Sahrens 	objset_t *mos = dp->dp_meta_objset;
345789Sahrens 	dmu_buf_t *dbuf;
346789Sahrens 	dsl_dataset_t *ds;
3471544Seschrock 	int err;
348789Sahrens 
349789Sahrens 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
350789Sahrens 	    dsl_pool_sync_context(dp));
351789Sahrens 
3521544Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
3531544Seschrock 	if (err)
3541544Seschrock 		return (err);
355789Sahrens 	ds = dmu_buf_get_user(dbuf);
356789Sahrens 	if (ds == NULL) {
357789Sahrens 		dsl_dataset_t *winner;
358789Sahrens 
359789Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
360789Sahrens 		ds->ds_dbuf = dbuf;
361789Sahrens 		ds->ds_object = dsobj;
362789Sahrens 		ds->ds_phys = dbuf->db_data;
363789Sahrens 
3642856Snd150628 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
36510204SMatthew.Ahrens@Sun.COM 		mutex_init(&ds->ds_recvlock, NULL, MUTEX_DEFAULT, NULL);
3664787Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
3676689Smaybee 		rw_init(&ds->ds_rwlock, 0, 0, 0);
3686689Smaybee 		cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL);
36910922SJeff.Bonwick@Sun.COM 		bplist_init(&ds->ds_deadlist);
3702856Snd150628 
3711544Seschrock 		err = bplist_open(&ds->ds_deadlist,
372789Sahrens 		    mos, ds->ds_phys->ds_deadlist_obj);
3731544Seschrock 		if (err == 0) {
3741544Seschrock 			err = dsl_dir_open_obj(dp,
3751544Seschrock 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
3761544Seschrock 		}
3771544Seschrock 		if (err) {
3781544Seschrock 			/*
3791544Seschrock 			 * we don't really need to close the blist if we
3801544Seschrock 			 * just opened it.
3811544Seschrock 			 */
3822856Snd150628 			mutex_destroy(&ds->ds_lock);
38310204SMatthew.Ahrens@Sun.COM 			mutex_destroy(&ds->ds_recvlock);
3844787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
3856689Smaybee 			rw_destroy(&ds->ds_rwlock);
3866689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
38710922SJeff.Bonwick@Sun.COM 			bplist_fini(&ds->ds_deadlist);
3881544Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
3891544Seschrock 			dmu_buf_rele(dbuf, tag);
3901544Seschrock 			return (err);
3911544Seschrock 		}
392789Sahrens 
3937390SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(ds)) {
394789Sahrens 			ds->ds_snapname[0] = '\0';
395789Sahrens 			if (ds->ds_phys->ds_prev_snap_obj) {
3966689Smaybee 				err = dsl_dataset_get_ref(dp,
3976689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
3986689Smaybee 				    ds, &ds->ds_prev);
399789Sahrens 			}
4007390SMatthew.Ahrens@Sun.COM 
4017390SMatthew.Ahrens@Sun.COM 			if (err == 0 && dsl_dir_is_clone(ds->ds_dir)) {
4027390SMatthew.Ahrens@Sun.COM 				dsl_dataset_t *origin;
4037390SMatthew.Ahrens@Sun.COM 
4047390SMatthew.Ahrens@Sun.COM 				err = dsl_dataset_hold_obj(dp,
4057390SMatthew.Ahrens@Sun.COM 				    ds->ds_dir->dd_phys->dd_origin_obj,
4067390SMatthew.Ahrens@Sun.COM 				    FTAG, &origin);
4077390SMatthew.Ahrens@Sun.COM 				if (err == 0) {
4087390SMatthew.Ahrens@Sun.COM 					ds->ds_origin_txg =
4097390SMatthew.Ahrens@Sun.COM 					    origin->ds_phys->ds_creation_txg;
4107390SMatthew.Ahrens@Sun.COM 					dsl_dataset_rele(origin, FTAG);
4117390SMatthew.Ahrens@Sun.COM 				}
4127390SMatthew.Ahrens@Sun.COM 			}
41310242Schris.kirby@sun.com 		} else {
41410242Schris.kirby@sun.com 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
41510242Schris.kirby@sun.com 				err = dsl_dataset_get_snapname(ds);
41610242Schris.kirby@sun.com 			if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
41710242Schris.kirby@sun.com 				err = zap_count(
41810242Schris.kirby@sun.com 				    ds->ds_dir->dd_pool->dp_meta_objset,
41910242Schris.kirby@sun.com 				    ds->ds_phys->ds_userrefs_obj,
42010242Schris.kirby@sun.com 				    &ds->ds_userrefs);
42110242Schris.kirby@sun.com 			}
422789Sahrens 		}
423789Sahrens 
4247390SMatthew.Ahrens@Sun.COM 		if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
4255569Sck153898 			/*
4265569Sck153898 			 * In sync context, we're called with either no lock
4275569Sck153898 			 * or with the write lock.  If we're not syncing,
4285569Sck153898 			 * we're always called with the read lock held.
4295569Sck153898 			 */
4305475Sck153898 			boolean_t need_lock =
4315569Sck153898 			    !RW_WRITE_HELD(&dp->dp_config_rwlock) &&
4325569Sck153898 			    dsl_pool_sync_context(dp);
4335475Sck153898 
4345475Sck153898 			if (need_lock)
4355475Sck153898 				rw_enter(&dp->dp_config_rwlock, RW_READER);
4365475Sck153898 
4377265Sahrens 			err = dsl_prop_get_ds(ds,
4385475Sck153898 			    "refreservation", sizeof (uint64_t), 1,
4395475Sck153898 			    &ds->ds_reserved, NULL);
4405475Sck153898 			if (err == 0) {
4417265Sahrens 				err = dsl_prop_get_ds(ds,
4425475Sck153898 				    "refquota", sizeof (uint64_t), 1,
4435475Sck153898 				    &ds->ds_quota, NULL);
4445475Sck153898 			}
4455475Sck153898 
4465475Sck153898 			if (need_lock)
4475475Sck153898 				rw_exit(&dp->dp_config_rwlock);
4485475Sck153898 		} else {
4495475Sck153898 			ds->ds_reserved = ds->ds_quota = 0;
4505475Sck153898 		}
4515475Sck153898 
4521544Seschrock 		if (err == 0) {
4531544Seschrock 			winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys,
4541544Seschrock 			    dsl_dataset_evict);
4551544Seschrock 		}
4561544Seschrock 		if (err || winner) {
457789Sahrens 			bplist_close(&ds->ds_deadlist);
4586689Smaybee 			if (ds->ds_prev)
4596689Smaybee 				dsl_dataset_drop_ref(ds->ds_prev, ds);
460789Sahrens 			dsl_dir_close(ds->ds_dir, ds);
4612856Snd150628 			mutex_destroy(&ds->ds_lock);
46210204SMatthew.Ahrens@Sun.COM 			mutex_destroy(&ds->ds_recvlock);
4634787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
4646689Smaybee 			rw_destroy(&ds->ds_rwlock);
4656689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
46610922SJeff.Bonwick@Sun.COM 			bplist_fini(&ds->ds_deadlist);
467789Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
4681544Seschrock 			if (err) {
4691544Seschrock 				dmu_buf_rele(dbuf, tag);
4701544Seschrock 				return (err);
4711544Seschrock 			}
472789Sahrens 			ds = winner;
473789Sahrens 		} else {
4744787Sahrens 			ds->ds_fsid_guid =
475789Sahrens 			    unique_insert(ds->ds_phys->ds_fsid_guid);
476789Sahrens 		}
477789Sahrens 	}
478789Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
479789Sahrens 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
4807046Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
4817061Sahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
4827077Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
483789Sahrens 	mutex_enter(&ds->ds_lock);
4846689Smaybee 	if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) {
485789Sahrens 		mutex_exit(&ds->ds_lock);
4866689Smaybee 		dmu_buf_rele(ds->ds_dbuf, tag);
4876689Smaybee 		return (ENOENT);
4886689Smaybee 	}
4896689Smaybee 	mutex_exit(&ds->ds_lock);
4906689Smaybee 	*dsp = ds;
4916689Smaybee 	return (0);
4926689Smaybee }
4936689Smaybee 
4946689Smaybee static int
4956689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag)
4966689Smaybee {
4976689Smaybee 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
4986689Smaybee 
4996689Smaybee 	/*
5006689Smaybee 	 * In syncing context we don't want the rwlock lock: there
5016689Smaybee 	 * may be an existing writer waiting for sync phase to
5026689Smaybee 	 * finish.  We don't need to worry about such writers, since
5036689Smaybee 	 * sync phase is single-threaded, so the writer can't be
5046689Smaybee 	 * doing anything while we are active.
5056689Smaybee 	 */
5066689Smaybee 	if (dsl_pool_sync_context(dp)) {
5076689Smaybee 		ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
5086689Smaybee 		return (0);
509789Sahrens 	}
5106689Smaybee 
5116689Smaybee 	/*
5126689Smaybee 	 * Normal users will hold the ds_rwlock as a READER until they
5136689Smaybee 	 * are finished (i.e., call dsl_dataset_rele()).  "Owners" will
5146689Smaybee 	 * drop their READER lock after they set the ds_owner field.
5156689Smaybee 	 *
5166689Smaybee 	 * If the dataset is being destroyed, the destroy thread will
5176689Smaybee 	 * obtain a WRITER lock for exclusive access after it's done its
5186689Smaybee 	 * open-context work and then change the ds_owner to
5196689Smaybee 	 * dsl_reaper once destruction is assured.  So threads
5206689Smaybee 	 * may block here temporarily, until the "destructability" of
5216689Smaybee 	 * the dataset is determined.
5226689Smaybee 	 */
5236689Smaybee 	ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock));
5246689Smaybee 	mutex_enter(&ds->ds_lock);
5256689Smaybee 	while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) {
5266689Smaybee 		rw_exit(&dp->dp_config_rwlock);
5276689Smaybee 		cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock);
5286689Smaybee 		if (DSL_DATASET_IS_DESTROYED(ds)) {
5296689Smaybee 			mutex_exit(&ds->ds_lock);
5306689Smaybee 			dsl_dataset_drop_ref(ds, tag);
5316689Smaybee 			rw_enter(&dp->dp_config_rwlock, RW_READER);
5326689Smaybee 			return (ENOENT);
5336689Smaybee 		}
53411546SChris.Kirby@sun.com 		/*
53511546SChris.Kirby@sun.com 		 * The dp_config_rwlock lives above the ds_lock. And
53611546SChris.Kirby@sun.com 		 * we need to check DSL_DATASET_IS_DESTROYED() while
53711546SChris.Kirby@sun.com 		 * holding the ds_lock, so we have to drop and reacquire
53811546SChris.Kirby@sun.com 		 * the ds_lock here.
53911546SChris.Kirby@sun.com 		 */
54011546SChris.Kirby@sun.com 		mutex_exit(&ds->ds_lock);
5416689Smaybee 		rw_enter(&dp->dp_config_rwlock, RW_READER);
54211546SChris.Kirby@sun.com 		mutex_enter(&ds->ds_lock);
5436689Smaybee 	}
544789Sahrens 	mutex_exit(&ds->ds_lock);
5451544Seschrock 	return (0);
546789Sahrens }
547789Sahrens 
548789Sahrens int
5496689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
5506689Smaybee     dsl_dataset_t **dsp)
5516689Smaybee {
5526689Smaybee 	int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp);
5536689Smaybee 
5546689Smaybee 	if (err)
5556689Smaybee 		return (err);
5566689Smaybee 	return (dsl_dataset_hold_ref(*dsp, tag));
5576689Smaybee }
5586689Smaybee 
5596689Smaybee int
56010298SMatthew.Ahrens@Sun.COM dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, boolean_t inconsistentok,
56110298SMatthew.Ahrens@Sun.COM     void *tag, dsl_dataset_t **dsp)
5626689Smaybee {
56310298SMatthew.Ahrens@Sun.COM 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
5646689Smaybee 	if (err)
5656689Smaybee 		return (err);
56610298SMatthew.Ahrens@Sun.COM 	if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
56710298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(*dsp, tag);
5688779SMark.Musante@Sun.COM 		*dsp = NULL;
5696689Smaybee 		return (EBUSY);
5706689Smaybee 	}
5716689Smaybee 	return (0);
5726689Smaybee }
5736689Smaybee 
5746689Smaybee int
5756689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp)
576789Sahrens {
577789Sahrens 	dsl_dir_t *dd;
578789Sahrens 	dsl_pool_t *dp;
5796689Smaybee 	const char *snapname;
580789Sahrens 	uint64_t obj;
581789Sahrens 	int err = 0;
582789Sahrens 
5836689Smaybee 	err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname);
5841544Seschrock 	if (err)
5851544Seschrock 		return (err);
586789Sahrens 
587789Sahrens 	dp = dd->dd_pool;
588789Sahrens 	obj = dd->dd_phys->dd_head_dataset_obj;
589789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
5906689Smaybee 	if (obj)
5916689Smaybee 		err = dsl_dataset_get_ref(dp, obj, tag, dsp);
5926689Smaybee 	else
593789Sahrens 		err = ENOENT;
5946689Smaybee 	if (err)
595789Sahrens 		goto out;
5966689Smaybee 
5976689Smaybee 	err = dsl_dataset_hold_ref(*dsp, tag);
5986689Smaybee 
5996689Smaybee 	/* we may be looking for a snapshot */
6006689Smaybee 	if (err == 0 && snapname != NULL) {
6016689Smaybee 		dsl_dataset_t *ds = NULL;
6026689Smaybee 
6036689Smaybee 		if (*snapname++ != '@') {
6046689Smaybee 			dsl_dataset_rele(*dsp, tag);
605789Sahrens 			err = ENOENT;
606789Sahrens 			goto out;
607789Sahrens 		}
6086689Smaybee 
6096689Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
6106689Smaybee 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
6116689Smaybee 		if (err == 0)
6126689Smaybee 			err = dsl_dataset_get_ref(dp, obj, tag, &ds);
6136689Smaybee 		dsl_dataset_rele(*dsp, tag);
6146689Smaybee 
6156689Smaybee 		ASSERT3U((err == 0), ==, (ds != NULL));
6166689Smaybee 
6176689Smaybee 		if (ds) {
6186689Smaybee 			mutex_enter(&ds->ds_lock);
6196689Smaybee 			if (ds->ds_snapname[0] == 0)
6206689Smaybee 				(void) strlcpy(ds->ds_snapname, snapname,
6216689Smaybee 				    sizeof (ds->ds_snapname));
6226689Smaybee 			mutex_exit(&ds->ds_lock);
6236689Smaybee 			err = dsl_dataset_hold_ref(ds, tag);
6246689Smaybee 			*dsp = err ? NULL : ds;
625789Sahrens 		}
626789Sahrens 	}
627789Sahrens out:
628789Sahrens 	rw_exit(&dp->dp_config_rwlock);
629789Sahrens 	dsl_dir_close(dd, FTAG);
630789Sahrens 	return (err);
631789Sahrens }
632789Sahrens 
633789Sahrens int
63410298SMatthew.Ahrens@Sun.COM dsl_dataset_own(const char *name, boolean_t inconsistentok,
63510298SMatthew.Ahrens@Sun.COM     void *tag, dsl_dataset_t **dsp)
636789Sahrens {
63710298SMatthew.Ahrens@Sun.COM 	int err = dsl_dataset_hold(name, tag, dsp);
6386689Smaybee 	if (err)
6396689Smaybee 		return (err);
64010298SMatthew.Ahrens@Sun.COM 	if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
64110298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(*dsp, tag);
6426689Smaybee 		return (EBUSY);
6436689Smaybee 	}
6446689Smaybee 	return (0);
645789Sahrens }
646789Sahrens 
647789Sahrens void
648789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
649789Sahrens {
650789Sahrens 	if (ds == NULL) {
651789Sahrens 		(void) strcpy(name, "mos");
652789Sahrens 	} else {
653789Sahrens 		dsl_dir_name(ds->ds_dir, name);
6541544Seschrock 		VERIFY(0 == dsl_dataset_get_snapname(ds));
655789Sahrens 		if (ds->ds_snapname[0]) {
656789Sahrens 			(void) strcat(name, "@");
6576689Smaybee 			/*
6586689Smaybee 			 * We use a "recursive" mutex so that we
6596689Smaybee 			 * can call dprintf_ds() with ds_lock held.
6606689Smaybee 			 */
661789Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
662789Sahrens 				mutex_enter(&ds->ds_lock);
663789Sahrens 				(void) strcat(name, ds->ds_snapname);
664789Sahrens 				mutex_exit(&ds->ds_lock);
665789Sahrens 			} else {
666789Sahrens 				(void) strcat(name, ds->ds_snapname);
667789Sahrens 			}
668789Sahrens 		}
669789Sahrens 	}
670789Sahrens }
671789Sahrens 
6723978Smmusante static int
6733978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds)
6743978Smmusante {
6753978Smmusante 	int result;
6763978Smmusante 
6773978Smmusante 	if (ds == NULL) {
6783978Smmusante 		result = 3;	/* "mos" */
6793978Smmusante 	} else {
6803978Smmusante 		result = dsl_dir_namelen(ds->ds_dir);
6813978Smmusante 		VERIFY(0 == dsl_dataset_get_snapname(ds));
6823978Smmusante 		if (ds->ds_snapname[0]) {
6833978Smmusante 			++result;	/* adding one for the @-sign */
6843978Smmusante 			if (!MUTEX_HELD(&ds->ds_lock)) {
6853978Smmusante 				mutex_enter(&ds->ds_lock);
6863978Smmusante 				result += strlen(ds->ds_snapname);
6873978Smmusante 				mutex_exit(&ds->ds_lock);
6883978Smmusante 			} else {
6893978Smmusante 				result += strlen(ds->ds_snapname);
6903978Smmusante 			}
6913978Smmusante 		}
6923978Smmusante 	}
6933978Smmusante 
6943978Smmusante 	return (result);
6953978Smmusante }
6963978Smmusante 
6977046Sahrens void
6986689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag)
699789Sahrens {
7001544Seschrock 	dmu_buf_rele(ds->ds_dbuf, tag);
701789Sahrens }
702789Sahrens 
703789Sahrens void
7046689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
7055367Sahrens {
7066689Smaybee 	if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) {
7076689Smaybee 		rw_exit(&ds->ds_rwlock);
7086689Smaybee 	}
7096689Smaybee 	dsl_dataset_drop_ref(ds, tag);
7106689Smaybee }
7116689Smaybee 
7126689Smaybee void
71310298SMatthew.Ahrens@Sun.COM dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
7146689Smaybee {
71510298SMatthew.Ahrens@Sun.COM 	ASSERT((ds->ds_owner == tag && ds->ds_dbuf) ||
7166689Smaybee 	    (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL));
7176689Smaybee 
7185367Sahrens 	mutex_enter(&ds->ds_lock);
7196689Smaybee 	ds->ds_owner = NULL;
7206689Smaybee 	if (RW_WRITE_HELD(&ds->ds_rwlock)) {
7216689Smaybee 		rw_exit(&ds->ds_rwlock);
7226689Smaybee 		cv_broadcast(&ds->ds_exclusive_cv);
7236689Smaybee 	}
7245367Sahrens 	mutex_exit(&ds->ds_lock);
7256689Smaybee 	if (ds->ds_dbuf)
72610298SMatthew.Ahrens@Sun.COM 		dsl_dataset_drop_ref(ds, tag);
7276689Smaybee 	else
7286689Smaybee 		dsl_dataset_evict(ds->ds_dbuf, ds);
7295367Sahrens }
7305367Sahrens 
7315367Sahrens boolean_t
73210298SMatthew.Ahrens@Sun.COM dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *tag)
7335367Sahrens {
7346689Smaybee 	boolean_t gotit = FALSE;
7356689Smaybee 
7365367Sahrens 	mutex_enter(&ds->ds_lock);
7376689Smaybee 	if (ds->ds_owner == NULL &&
7386689Smaybee 	    (!DS_IS_INCONSISTENT(ds) || inconsistentok)) {
73910298SMatthew.Ahrens@Sun.COM 		ds->ds_owner = tag;
7406689Smaybee 		if (!dsl_pool_sync_context(ds->ds_dir->dd_pool))
7416689Smaybee 			rw_exit(&ds->ds_rwlock);
7426689Smaybee 		gotit = TRUE;
7435367Sahrens 	}
7445367Sahrens 	mutex_exit(&ds->ds_lock);
7456689Smaybee 	return (gotit);
7466689Smaybee }
7476689Smaybee 
7486689Smaybee void
7496689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner)
7506689Smaybee {
7516689Smaybee 	ASSERT3P(owner, ==, ds->ds_owner);
7526689Smaybee 	if (!RW_WRITE_HELD(&ds->ds_rwlock))
7536689Smaybee 		rw_enter(&ds->ds_rwlock, RW_WRITER);
7545367Sahrens }
7555367Sahrens 
7562199Sahrens uint64_t
7577046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
7586492Stimh     uint64_t flags, dmu_tx_t *tx)
759789Sahrens {
7605367Sahrens 	dsl_pool_t *dp = dd->dd_pool;
761789Sahrens 	dmu_buf_t *dbuf;
762789Sahrens 	dsl_dataset_phys_t *dsphys;
7635367Sahrens 	uint64_t dsobj;
764789Sahrens 	objset_t *mos = dp->dp_meta_objset;
765789Sahrens 
7667046Sahrens 	if (origin == NULL)
7677046Sahrens 		origin = dp->dp_origin_snap;
7687046Sahrens 
7695367Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
7705367Sahrens 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
771789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7725367Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
773789Sahrens 
774928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
775928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
7761544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
777789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
778789Sahrens 	dsphys = dbuf->db_data;
7796689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
780789Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
7816492Stimh 	dsphys->ds_flags = flags;
782789Sahrens 	dsphys->ds_fsid_guid = unique_create();
783789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
784789Sahrens 	    sizeof (dsphys->ds_guid));
785789Sahrens 	dsphys->ds_snapnames_zapobj =
7866492Stimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
7876492Stimh 	    DMU_OT_NONE, 0, tx);
788789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
7897046Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
790789Sahrens 	dsphys->ds_deadlist_obj =
791789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
7925378Sck153898 
7935367Sahrens 	if (origin) {
7945367Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
795789Sahrens 		dsphys->ds_prev_snap_txg =
7965367Sahrens 		    origin->ds_phys->ds_creation_txg;
797789Sahrens 		dsphys->ds_used_bytes =
7985367Sahrens 		    origin->ds_phys->ds_used_bytes;
799789Sahrens 		dsphys->ds_compressed_bytes =
8005367Sahrens 		    origin->ds_phys->ds_compressed_bytes;
801789Sahrens 		dsphys->ds_uncompressed_bytes =
8025367Sahrens 		    origin->ds_phys->ds_uncompressed_bytes;
8035367Sahrens 		dsphys->ds_bp = origin->ds_phys->ds_bp;
8046502Stimh 		dsphys->ds_flags |= origin->ds_phys->ds_flags;
805789Sahrens 
8065367Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
8075367Sahrens 		origin->ds_phys->ds_num_children++;
808789Sahrens 
8097046Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
8107046Sahrens 			if (origin->ds_phys->ds_next_clones_obj == 0) {
8117046Sahrens 				origin->ds_phys->ds_next_clones_obj =
8127046Sahrens 				    zap_create(mos,
8137046Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
8147046Sahrens 			}
8157046Sahrens 			VERIFY(0 == zap_add_int(mos,
8167046Sahrens 			    origin->ds_phys->ds_next_clones_obj,
8177046Sahrens 			    dsobj, tx));
8187046Sahrens 		}
8197046Sahrens 
820789Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
8215367Sahrens 		dd->dd_phys->dd_origin_obj = origin->ds_object;
822789Sahrens 	}
8236492Stimh 
8246492Stimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
8256492Stimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
8266492Stimh 
8271544Seschrock 	dmu_buf_rele(dbuf, FTAG);
828789Sahrens 
829789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
830789Sahrens 	dd->dd_phys->dd_head_dataset_obj = dsobj;
8315367Sahrens 
8325367Sahrens 	return (dsobj);
8335367Sahrens }
8345367Sahrens 
8355367Sahrens uint64_t
8366492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
8376492Stimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
8385367Sahrens {
8395367Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
8405367Sahrens 	uint64_t dsobj, ddobj;
8415367Sahrens 	dsl_dir_t *dd;
8425367Sahrens 
8435367Sahrens 	ASSERT(lastname[0] != '@');
8445367Sahrens 
8457046Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
8465367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
8475367Sahrens 
8487046Sahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx);
8495367Sahrens 
8505367Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
8515367Sahrens 
852789Sahrens 	dsl_dir_close(dd, FTAG);
853789Sahrens 
8542199Sahrens 	return (dsobj);
8552199Sahrens }
8562199Sahrens 
8572199Sahrens struct destroyarg {
8582199Sahrens 	dsl_sync_task_group_t *dstg;
8592199Sahrens 	char *snapname;
8602199Sahrens 	char *failed;
86110242Schris.kirby@sun.com 	boolean_t defer;
8622199Sahrens };
8632199Sahrens 
8642199Sahrens static int
86511209SMatthew.Ahrens@Sun.COM dsl_snapshot_destroy_one(const char *name, void *arg)
8662199Sahrens {
8672199Sahrens 	struct destroyarg *da = arg;
8682199Sahrens 	dsl_dataset_t *ds;
8692199Sahrens 	int err;
87010242Schris.kirby@sun.com 	char *dsname;
87110272SMatthew.Ahrens@Sun.COM 
87210272SMatthew.Ahrens@Sun.COM 	dsname = kmem_asprintf("%s@%s", name, da->snapname);
87310298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_own(dsname, B_TRUE, da->dstg, &ds);
87410272SMatthew.Ahrens@Sun.COM 	strfree(dsname);
8756689Smaybee 	if (err == 0) {
87610242Schris.kirby@sun.com 		struct dsl_ds_destroyarg *dsda;
87710242Schris.kirby@sun.com 
8786689Smaybee 		dsl_dataset_make_exclusive(ds, da->dstg);
87910298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset != NULL) {
88010298SMatthew.Ahrens@Sun.COM 			dmu_objset_evict(ds->ds_objset);
88110298SMatthew.Ahrens@Sun.COM 			ds->ds_objset = NULL;
8827237Sek110237 		}
88310242Schris.kirby@sun.com 		dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg), KM_SLEEP);
88410242Schris.kirby@sun.com 		dsda->ds = ds;
88510242Schris.kirby@sun.com 		dsda->defer = da->defer;
8866689Smaybee 		dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check,
88710242Schris.kirby@sun.com 		    dsl_dataset_destroy_sync, dsda, da->dstg, 0);
8886689Smaybee 	} else if (err == ENOENT) {
8896689Smaybee 		err = 0;
8906689Smaybee 	} else {
8912199Sahrens 		(void) strcpy(da->failed, name);
8922199Sahrens 	}
8936689Smaybee 	return (err);
894789Sahrens }
895789Sahrens 
8962199Sahrens /*
8972199Sahrens  * Destroy 'snapname' in all descendants of 'fsname'.
8982199Sahrens  */
8992199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy
9002199Sahrens int
90110242Schris.kirby@sun.com dsl_snapshots_destroy(char *fsname, char *snapname, boolean_t defer)
9022199Sahrens {
9032199Sahrens 	int err;
9042199Sahrens 	struct destroyarg da;
9052199Sahrens 	dsl_sync_task_t *dst;
9062199Sahrens 	spa_t *spa;
9072199Sahrens 
9084603Sahrens 	err = spa_open(fsname, &spa, FTAG);
9092199Sahrens 	if (err)
9102199Sahrens 		return (err);
9112199Sahrens 	da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
9122199Sahrens 	da.snapname = snapname;
9132199Sahrens 	da.failed = fsname;
91410242Schris.kirby@sun.com 	da.defer = defer;
9152199Sahrens 
9162199Sahrens 	err = dmu_objset_find(fsname,
9172417Sahrens 	    dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN);
9182199Sahrens 
9192199Sahrens 	if (err == 0)
9202199Sahrens 		err = dsl_sync_task_group_wait(da.dstg);
9212199Sahrens 
9222199Sahrens 	for (dst = list_head(&da.dstg->dstg_tasks); dst;
9232199Sahrens 	    dst = list_next(&da.dstg->dstg_tasks, dst)) {
92410242Schris.kirby@sun.com 		struct dsl_ds_destroyarg *dsda = dst->dst_arg1;
92510242Schris.kirby@sun.com 		dsl_dataset_t *ds = dsda->ds;
92610242Schris.kirby@sun.com 
9276689Smaybee 		/*
9286689Smaybee 		 * Return the file system name that triggered the error
9296689Smaybee 		 */
9302199Sahrens 		if (dst->dst_err) {
9312199Sahrens 			dsl_dataset_name(ds, fsname);
9324603Sahrens 			*strchr(fsname, '@') = '\0';
9332199Sahrens 		}
93410242Schris.kirby@sun.com 		ASSERT3P(dsda->rm_origin, ==, NULL);
9356689Smaybee 		dsl_dataset_disown(ds, da.dstg);
93610242Schris.kirby@sun.com 		kmem_free(dsda, sizeof (struct dsl_ds_destroyarg));
9372199Sahrens 	}
9382199Sahrens 
9392199Sahrens 	dsl_sync_task_group_destroy(da.dstg);
9402199Sahrens 	spa_close(spa, FTAG);
9412199Sahrens 	return (err);
9422199Sahrens }
9432199Sahrens 
94410242Schris.kirby@sun.com static boolean_t
94510242Schris.kirby@sun.com dsl_dataset_might_destroy_origin(dsl_dataset_t *ds)
94610242Schris.kirby@sun.com {
94710242Schris.kirby@sun.com 	boolean_t might_destroy = B_FALSE;
94810242Schris.kirby@sun.com 
94910242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
95010242Schris.kirby@sun.com 	if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 &&
95110242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds))
95210242Schris.kirby@sun.com 		might_destroy = B_TRUE;
95310242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
95410242Schris.kirby@sun.com 
95510242Schris.kirby@sun.com 	return (might_destroy);
95610242Schris.kirby@sun.com }
95710242Schris.kirby@sun.com 
95810242Schris.kirby@sun.com /*
95910242Schris.kirby@sun.com  * If we're removing a clone, and these three conditions are true:
96010242Schris.kirby@sun.com  *	1) the clone's origin has no other children
96110242Schris.kirby@sun.com  *	2) the clone's origin has no user references
96210242Schris.kirby@sun.com  *	3) the clone's origin has been marked for deferred destruction
96310242Schris.kirby@sun.com  * Then, prepare to remove the origin as part of this sync task group.
96410242Schris.kirby@sun.com  */
96510242Schris.kirby@sun.com static int
96610242Schris.kirby@sun.com dsl_dataset_origin_rm_prep(struct dsl_ds_destroyarg *dsda, void *tag)
96710242Schris.kirby@sun.com {
96810242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
96910242Schris.kirby@sun.com 	dsl_dataset_t *origin = ds->ds_prev;
97010242Schris.kirby@sun.com 
97110242Schris.kirby@sun.com 	if (dsl_dataset_might_destroy_origin(origin)) {
97210242Schris.kirby@sun.com 		char *name;
97310242Schris.kirby@sun.com 		int namelen;
97410242Schris.kirby@sun.com 		int error;
97510242Schris.kirby@sun.com 
97610242Schris.kirby@sun.com 		namelen = dsl_dataset_namelen(origin) + 1;
97710242Schris.kirby@sun.com 		name = kmem_alloc(namelen, KM_SLEEP);
97810242Schris.kirby@sun.com 		dsl_dataset_name(origin, name);
97910242Schris.kirby@sun.com #ifdef _KERNEL
98010242Schris.kirby@sun.com 		error = zfs_unmount_snap(name, NULL);
98110242Schris.kirby@sun.com 		if (error) {
98210242Schris.kirby@sun.com 			kmem_free(name, namelen);
98310242Schris.kirby@sun.com 			return (error);
98410242Schris.kirby@sun.com 		}
98510242Schris.kirby@sun.com #endif
98610298SMatthew.Ahrens@Sun.COM 		error = dsl_dataset_own(name, B_TRUE, tag, &origin);
98710242Schris.kirby@sun.com 		kmem_free(name, namelen);
98810242Schris.kirby@sun.com 		if (error)
98910242Schris.kirby@sun.com 			return (error);
99010242Schris.kirby@sun.com 		dsda->rm_origin = origin;
99110242Schris.kirby@sun.com 		dsl_dataset_make_exclusive(origin, tag);
99210342Schris.kirby@sun.com 
99310342Schris.kirby@sun.com 		if (origin->ds_objset != NULL) {
99410342Schris.kirby@sun.com 			dmu_objset_evict(origin->ds_objset);
99510342Schris.kirby@sun.com 			origin->ds_objset = NULL;
99610342Schris.kirby@sun.com 		}
99710242Schris.kirby@sun.com 	}
99810242Schris.kirby@sun.com 
99910242Schris.kirby@sun.com 	return (0);
100010242Schris.kirby@sun.com }
100110242Schris.kirby@sun.com 
10025367Sahrens /*
10036689Smaybee  * ds must be opened as OWNER.  On return (whether successful or not),
10046689Smaybee  * ds will be closed and caller can no longer dereference it.
10055367Sahrens  */
1006789Sahrens int
100710242Schris.kirby@sun.com dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer)
1008789Sahrens {
1009789Sahrens 	int err;
10102199Sahrens 	dsl_sync_task_group_t *dstg;
10112199Sahrens 	objset_t *os;
1012789Sahrens 	dsl_dir_t *dd;
10132199Sahrens 	uint64_t obj;
101411022STom.Erickson@Sun.COM 	struct dsl_ds_destroyarg dsda = { 0 };
101511022STom.Erickson@Sun.COM 	dsl_dataset_t dummy_ds = { 0 };
101610242Schris.kirby@sun.com 
101710242Schris.kirby@sun.com 	dsda.ds = ds;
10182199Sahrens 
10195367Sahrens 	if (dsl_dataset_is_snapshot(ds)) {
10202199Sahrens 		/* Destroying a snapshot is simpler */
10216689Smaybee 		dsl_dataset_make_exclusive(ds, tag);
10227237Sek110237 
102310298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset != NULL) {
102410298SMatthew.Ahrens@Sun.COM 			dmu_objset_evict(ds->ds_objset);
102510298SMatthew.Ahrens@Sun.COM 			ds->ds_objset = NULL;
10267237Sek110237 		}
102710242Schris.kirby@sun.com 		dsda.defer = defer;
10282199Sahrens 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
10292199Sahrens 		    dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
103010242Schris.kirby@sun.com 		    &dsda, tag, 0);
103110242Schris.kirby@sun.com 		ASSERT3P(dsda.rm_origin, ==, NULL);
10325367Sahrens 		goto out;
103310385Schris.kirby@sun.com 	} else if (defer) {
103410385Schris.kirby@sun.com 		err = EINVAL;
103510385Schris.kirby@sun.com 		goto out;
10362199Sahrens 	}
10372199Sahrens 
10382199Sahrens 	dd = ds->ds_dir;
103911022STom.Erickson@Sun.COM 	dummy_ds.ds_dir = dd;
104011022STom.Erickson@Sun.COM 	dummy_ds.ds_object = ds->ds_object;
1041789Sahrens 
10422199Sahrens 	/*
10432199Sahrens 	 * Check for errors and mark this ds as inconsistent, in
10442199Sahrens 	 * case we crash while freeing the objects.
10452199Sahrens 	 */
10462199Sahrens 	err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
10472199Sahrens 	    dsl_dataset_destroy_begin_sync, ds, NULL, 0);
10485367Sahrens 	if (err)
10495367Sahrens 		goto out;
10505367Sahrens 
105110298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, &os);
10525367Sahrens 	if (err)
10535367Sahrens 		goto out;
10542199Sahrens 
10552199Sahrens 	/*
10562199Sahrens 	 * remove the objects in open context, so that we won't
10572199Sahrens 	 * have too much to do in syncing context.
10582199Sahrens 	 */
10593025Sahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
10603025Sahrens 	    ds->ds_phys->ds_prev_snap_txg)) {
10616992Smaybee 		/*
10626992Smaybee 		 * Ignore errors, if there is not enough disk space
10636992Smaybee 		 * we will deal with it in dsl_dataset_destroy_sync().
10646992Smaybee 		 */
10656992Smaybee 		(void) dmu_free_object(os, obj);
10662199Sahrens 	}
10672199Sahrens 
10689396SMatthew.Ahrens@Sun.COM 	/*
10699396SMatthew.Ahrens@Sun.COM 	 * We need to sync out all in-flight IO before we try to evict
10709396SMatthew.Ahrens@Sun.COM 	 * (the dataset evict func is trying to clear the cached entries
10719396SMatthew.Ahrens@Sun.COM 	 * for this dataset in the ARC).
10729396SMatthew.Ahrens@Sun.COM 	 */
10739396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dd->dd_pool, 0);
10749396SMatthew.Ahrens@Sun.COM 
10759396SMatthew.Ahrens@Sun.COM 	/*
10769396SMatthew.Ahrens@Sun.COM 	 * If we managed to free all the objects in open
10779396SMatthew.Ahrens@Sun.COM 	 * context, the user space accounting should be zero.
10789396SMatthew.Ahrens@Sun.COM 	 */
10799396SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_bp.blk_fill == 0 &&
108010298SMatthew.Ahrens@Sun.COM 	    dmu_objset_userused_enabled(os)) {
10819396SMatthew.Ahrens@Sun.COM 		uint64_t count;
10829396SMatthew.Ahrens@Sun.COM 
10839396SMatthew.Ahrens@Sun.COM 		ASSERT(zap_count(os, DMU_USERUSED_OBJECT, &count) != 0 ||
10849396SMatthew.Ahrens@Sun.COM 		    count == 0);
10859396SMatthew.Ahrens@Sun.COM 		ASSERT(zap_count(os, DMU_GROUPUSED_OBJECT, &count) != 0 ||
10869396SMatthew.Ahrens@Sun.COM 		    count == 0);
10879396SMatthew.Ahrens@Sun.COM 	}
10889396SMatthew.Ahrens@Sun.COM 
10892199Sahrens 	if (err != ESRCH)
10905367Sahrens 		goto out;
10912199Sahrens 
10926975Smaybee 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
10936975Smaybee 	err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd);
10946975Smaybee 	rw_exit(&dd->dd_pool->dp_config_rwlock);
10956975Smaybee 
10966975Smaybee 	if (err)
10976975Smaybee 		goto out;
10986975Smaybee 
109910298SMatthew.Ahrens@Sun.COM 	if (ds->ds_objset) {
11006689Smaybee 		/*
11016689Smaybee 		 * We need to sync out all in-flight IO before we try
11026689Smaybee 		 * to evict (the dataset evict func is trying to clear
11036689Smaybee 		 * the cached entries for this dataset in the ARC).
11046689Smaybee 		 */
11056689Smaybee 		txg_wait_synced(dd->dd_pool, 0);
11065367Sahrens 	}
11075367Sahrens 
11082199Sahrens 	/*
11092199Sahrens 	 * Blow away the dsl_dir + head dataset.
11102199Sahrens 	 */
11116689Smaybee 	dsl_dataset_make_exclusive(ds, tag);
111210298SMatthew.Ahrens@Sun.COM 	if (ds->ds_objset) {
111310298SMatthew.Ahrens@Sun.COM 		dmu_objset_evict(ds->ds_objset);
111410298SMatthew.Ahrens@Sun.COM 		ds->ds_objset = NULL;
11156975Smaybee 	}
111610242Schris.kirby@sun.com 
111710242Schris.kirby@sun.com 	/*
111810242Schris.kirby@sun.com 	 * If we're removing a clone, we might also need to remove its
111910242Schris.kirby@sun.com 	 * origin.
112010242Schris.kirby@sun.com 	 */
112110242Schris.kirby@sun.com 	do {
112210242Schris.kirby@sun.com 		dsda.need_prep = B_FALSE;
112310242Schris.kirby@sun.com 		if (dsl_dir_is_clone(dd)) {
112410242Schris.kirby@sun.com 			err = dsl_dataset_origin_rm_prep(&dsda, tag);
112510242Schris.kirby@sun.com 			if (err) {
112610242Schris.kirby@sun.com 				dsl_dir_close(dd, FTAG);
112710242Schris.kirby@sun.com 				goto out;
112810242Schris.kirby@sun.com 			}
112910242Schris.kirby@sun.com 		}
113010242Schris.kirby@sun.com 
113110242Schris.kirby@sun.com 		dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
113210242Schris.kirby@sun.com 		dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
113310242Schris.kirby@sun.com 		    dsl_dataset_destroy_sync, &dsda, tag, 0);
113410242Schris.kirby@sun.com 		dsl_sync_task_create(dstg, dsl_dir_destroy_check,
113511022STom.Erickson@Sun.COM 		    dsl_dir_destroy_sync, &dummy_ds, FTAG, 0);
113610242Schris.kirby@sun.com 		err = dsl_sync_task_group_wait(dstg);
113710242Schris.kirby@sun.com 		dsl_sync_task_group_destroy(dstg);
113810242Schris.kirby@sun.com 
113910242Schris.kirby@sun.com 		/*
114010242Schris.kirby@sun.com 		 * We could be racing against 'zfs release' or 'zfs destroy -d'
114110242Schris.kirby@sun.com 		 * on the origin snap, in which case we can get EBUSY if we
114210242Schris.kirby@sun.com 		 * needed to destroy the origin snap but were not ready to
114310242Schris.kirby@sun.com 		 * do so.
114410242Schris.kirby@sun.com 		 */
114510242Schris.kirby@sun.com 		if (dsda.need_prep) {
114610242Schris.kirby@sun.com 			ASSERT(err == EBUSY);
114710242Schris.kirby@sun.com 			ASSERT(dsl_dir_is_clone(dd));
114810242Schris.kirby@sun.com 			ASSERT(dsda.rm_origin == NULL);
114910242Schris.kirby@sun.com 		}
115010242Schris.kirby@sun.com 	} while (dsda.need_prep);
115110242Schris.kirby@sun.com 
115210242Schris.kirby@sun.com 	if (dsda.rm_origin != NULL)
115310242Schris.kirby@sun.com 		dsl_dataset_disown(dsda.rm_origin, tag);
115410242Schris.kirby@sun.com 
11556689Smaybee 	/* if it is successful, dsl_dir_destroy_sync will close the dd */
11565367Sahrens 	if (err)
11572199Sahrens 		dsl_dir_close(dd, FTAG);
11585367Sahrens out:
11596689Smaybee 	dsl_dataset_disown(ds, tag);
1160789Sahrens 	return (err);
1161789Sahrens }
1162789Sahrens 
11633547Smaybee blkptr_t *
11643547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1165789Sahrens {
11663547Smaybee 	return (&ds->ds_phys->ds_bp);
1167789Sahrens }
1168789Sahrens 
1169789Sahrens void
1170789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1171789Sahrens {
1172789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1173789Sahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
1174789Sahrens 	if (ds == NULL) {
1175789Sahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
1176789Sahrens 	} else {
1177789Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1178789Sahrens 		ds->ds_phys->ds_bp = *bp;
1179789Sahrens 	}
1180789Sahrens }
1181789Sahrens 
1182789Sahrens spa_t *
1183789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
1184789Sahrens {
1185789Sahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1186789Sahrens }
1187789Sahrens 
1188789Sahrens void
1189789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1190789Sahrens {
1191789Sahrens 	dsl_pool_t *dp;
1192789Sahrens 
1193789Sahrens 	if (ds == NULL) /* this is the meta-objset */
1194789Sahrens 		return;
1195789Sahrens 
119610298SMatthew.Ahrens@Sun.COM 	ASSERT(ds->ds_objset != NULL);
11972885Sahrens 
11982885Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
11992885Sahrens 		panic("dirtying snapshot!");
1200789Sahrens 
1201789Sahrens 	dp = ds->ds_dir->dd_pool;
1202789Sahrens 
1203789Sahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
1204789Sahrens 		/* up the hold count until we can be written out */
1205789Sahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1206789Sahrens 	}
1207789Sahrens }
1208789Sahrens 
12095378Sck153898 /*
12105378Sck153898  * The unique space in the head dataset can be calculated by subtracting
12115378Sck153898  * the space used in the most recent snapshot, that is still being used
12125378Sck153898  * in this file system, from the space currently in use.  To figure out
12135378Sck153898  * the space in the most recent snapshot still in use, we need to take
12145378Sck153898  * the total space used in the snapshot and subtract out the space that
12155378Sck153898  * has been freed up since the snapshot was taken.
12165378Sck153898  */
12175378Sck153898 static void
12185378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
12195378Sck153898 {
12205378Sck153898 	uint64_t mrs_used;
12215378Sck153898 	uint64_t dlused, dlcomp, dluncomp;
12225378Sck153898 
12235378Sck153898 	ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj);
12245378Sck153898 
12255378Sck153898 	if (ds->ds_phys->ds_prev_snap_obj != 0)
12265378Sck153898 		mrs_used = ds->ds_prev->ds_phys->ds_used_bytes;
12275378Sck153898 	else
12285378Sck153898 		mrs_used = 0;
12295378Sck153898 
12305378Sck153898 	VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp,
12315378Sck153898 	    &dluncomp));
12325378Sck153898 
12335378Sck153898 	ASSERT3U(dlused, <=, mrs_used);
12345378Sck153898 	ds->ds_phys->ds_unique_bytes =
12355378Sck153898 	    ds->ds_phys->ds_used_bytes - (mrs_used - dlused);
12365378Sck153898 
12375378Sck153898 	if (!DS_UNIQUE_IS_ACCURATE(ds) &&
12385378Sck153898 	    spa_version(ds->ds_dir->dd_pool->dp_spa) >=
12395378Sck153898 	    SPA_VERSION_UNIQUE_ACCURATE)
12405378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
12415378Sck153898 }
12425378Sck153898 
12435378Sck153898 static uint64_t
12445378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds)
12455378Sck153898 {
12465378Sck153898 	if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds))
12475378Sck153898 		dsl_dataset_recalc_head_uniq(ds);
12485378Sck153898 
12495378Sck153898 	return (ds->ds_phys->ds_unique_bytes);
12505378Sck153898 }
12515378Sck153898 
1252789Sahrens struct killarg {
12537390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
1254789Sahrens 	dmu_tx_t *tx;
1255789Sahrens };
1256789Sahrens 
12577390SMatthew.Ahrens@Sun.COM /* ARGSUSED */
1258789Sahrens static int
125910922SJeff.Bonwick@Sun.COM kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
126010922SJeff.Bonwick@Sun.COM     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1261789Sahrens {
1262789Sahrens 	struct killarg *ka = arg;
126310922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx = ka->tx;
12647837SMatthew.Ahrens@Sun.COM 
12657837SMatthew.Ahrens@Sun.COM 	if (bp == NULL)
12667837SMatthew.Ahrens@Sun.COM 		return (0);
1267789Sahrens 
126810922SJeff.Bonwick@Sun.COM 	if (zb->zb_level == ZB_ZIL_LEVEL) {
126910922SJeff.Bonwick@Sun.COM 		ASSERT(zilog != NULL);
12708746SMatthew.Ahrens@Sun.COM 		/*
12718746SMatthew.Ahrens@Sun.COM 		 * It's a block in the intent log.  It has no
12728746SMatthew.Ahrens@Sun.COM 		 * accounting, so just free it.
12738746SMatthew.Ahrens@Sun.COM 		 */
127410922SJeff.Bonwick@Sun.COM 		dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
12758746SMatthew.Ahrens@Sun.COM 	} else {
127610922SJeff.Bonwick@Sun.COM 		ASSERT(zilog == NULL);
12778746SMatthew.Ahrens@Sun.COM 		ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
127810922SJeff.Bonwick@Sun.COM 		(void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
12798746SMatthew.Ahrens@Sun.COM 	}
12807390SMatthew.Ahrens@Sun.COM 
1281789Sahrens 	return (0);
1282789Sahrens }
1283789Sahrens 
1284789Sahrens /* ARGSUSED */
12852199Sahrens static int
12862199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
12871731Sbonwick {
12882199Sahrens 	dsl_dataset_t *ds = arg1;
12895367Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
12905367Sahrens 	uint64_t count;
12915367Sahrens 	int err;
12921731Sbonwick 
12931731Sbonwick 	/*
12941731Sbonwick 	 * Can't delete a head dataset if there are snapshots of it.
12951731Sbonwick 	 * (Except if the only snapshots are from the branch we cloned
12961731Sbonwick 	 * from.)
12971731Sbonwick 	 */
12981731Sbonwick 	if (ds->ds_prev != NULL &&
12991731Sbonwick 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
130010816SVitezslav.Batrla@Sun.COM 		return (EBUSY);
13011731Sbonwick 
13025367Sahrens 	/*
13035367Sahrens 	 * This is really a dsl_dir thing, but check it here so that
13045367Sahrens 	 * we'll be less likely to leave this dataset inconsistent &
13055367Sahrens 	 * nearly destroyed.
13065367Sahrens 	 */
13075367Sahrens 	err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
13085367Sahrens 	if (err)
13095367Sahrens 		return (err);
13105367Sahrens 	if (count != 0)
13115367Sahrens 		return (EEXIST);
13125367Sahrens 
13131731Sbonwick 	return (0);
13141731Sbonwick }
13151731Sbonwick 
13162199Sahrens /* ARGSUSED */
13172199Sahrens static void
13184543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1319789Sahrens {
13202199Sahrens 	dsl_dataset_t *ds = arg1;
13214543Smarks 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1322789Sahrens 
13232199Sahrens 	/* Mark it as inconsistent on-disk, in case we crash */
13242199Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
13252199Sahrens 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
13264543Smarks 
13274543Smarks 	spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
13284543Smarks 	    cr, "dataset = %llu", ds->ds_object);
13292199Sahrens }
1330789Sahrens 
133110242Schris.kirby@sun.com static int
133210242Schris.kirby@sun.com dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag,
133310242Schris.kirby@sun.com     dmu_tx_t *tx)
133410242Schris.kirby@sun.com {
133510242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
133610242Schris.kirby@sun.com 	dsl_dataset_t *ds_prev = ds->ds_prev;
133710242Schris.kirby@sun.com 
133810242Schris.kirby@sun.com 	if (dsl_dataset_might_destroy_origin(ds_prev)) {
133910242Schris.kirby@sun.com 		struct dsl_ds_destroyarg ndsda = {0};
134010242Schris.kirby@sun.com 
134110242Schris.kirby@sun.com 		/*
134210242Schris.kirby@sun.com 		 * If we're not prepared to remove the origin, don't remove
134310242Schris.kirby@sun.com 		 * the clone either.
134410242Schris.kirby@sun.com 		 */
134510242Schris.kirby@sun.com 		if (dsda->rm_origin == NULL) {
134610242Schris.kirby@sun.com 			dsda->need_prep = B_TRUE;
134710242Schris.kirby@sun.com 			return (EBUSY);
134810242Schris.kirby@sun.com 		}
134910242Schris.kirby@sun.com 
135010242Schris.kirby@sun.com 		ndsda.ds = ds_prev;
135110242Schris.kirby@sun.com 		ndsda.is_origin_rm = B_TRUE;
135210242Schris.kirby@sun.com 		return (dsl_dataset_destroy_check(&ndsda, tag, tx));
135310242Schris.kirby@sun.com 	}
135410242Schris.kirby@sun.com 
135510242Schris.kirby@sun.com 	/*
135610242Schris.kirby@sun.com 	 * If we're not going to remove the origin after all,
135710242Schris.kirby@sun.com 	 * undo the open context setup.
135810242Schris.kirby@sun.com 	 */
135910242Schris.kirby@sun.com 	if (dsda->rm_origin != NULL) {
136010242Schris.kirby@sun.com 		dsl_dataset_disown(dsda->rm_origin, tag);
136110242Schris.kirby@sun.com 		dsda->rm_origin = NULL;
136210242Schris.kirby@sun.com 	}
136310242Schris.kirby@sun.com 
136410242Schris.kirby@sun.com 	return (0);
136510242Schris.kirby@sun.com }
136610242Schris.kirby@sun.com 
13672199Sahrens /* ARGSUSED */
13685367Sahrens int
13692199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
13702199Sahrens {
137110242Schris.kirby@sun.com 	struct dsl_ds_destroyarg *dsda = arg1;
137210242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
1373789Sahrens 
13746689Smaybee 	/* we have an owner hold, so noone else can destroy us */
13756689Smaybee 	ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
13766689Smaybee 
137710242Schris.kirby@sun.com 	/*
137810242Schris.kirby@sun.com 	 * Only allow deferred destroy on pools that support it.
137910242Schris.kirby@sun.com 	 * NOTE: deferred destroy is only supported on snapshots.
138010242Schris.kirby@sun.com 	 */
138110242Schris.kirby@sun.com 	if (dsda->defer) {
138210242Schris.kirby@sun.com 		if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
138310242Schris.kirby@sun.com 		    SPA_VERSION_USERREFS)
138410242Schris.kirby@sun.com 			return (ENOTSUP);
138510242Schris.kirby@sun.com 		ASSERT(dsl_dataset_is_snapshot(ds));
138610242Schris.kirby@sun.com 		return (0);
138710242Schris.kirby@sun.com 	}
1388789Sahrens 
1389789Sahrens 	/*
1390789Sahrens 	 * Can't delete a head dataset if there are snapshots of it.
1391789Sahrens 	 * (Except if the only snapshots are from the branch we cloned
1392789Sahrens 	 * from.)
1393789Sahrens 	 */
1394789Sahrens 	if (ds->ds_prev != NULL &&
13952199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
139610816SVitezslav.Batrla@Sun.COM 		return (EBUSY);
1397789Sahrens 
1398789Sahrens 	/*
1399789Sahrens 	 * If we made changes this txg, traverse_dsl_dataset won't find
1400789Sahrens 	 * them.  Try again.
1401789Sahrens 	 */
14022199Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1403789Sahrens 		return (EAGAIN);
14042199Sahrens 
140510242Schris.kirby@sun.com 	if (dsl_dataset_is_snapshot(ds)) {
140610242Schris.kirby@sun.com 		/*
140710242Schris.kirby@sun.com 		 * If this snapshot has an elevated user reference count,
140810242Schris.kirby@sun.com 		 * we can't destroy it yet.
140910242Schris.kirby@sun.com 		 */
141010242Schris.kirby@sun.com 		if (ds->ds_userrefs > 0 && !dsda->releasing)
141110242Schris.kirby@sun.com 			return (EBUSY);
141210242Schris.kirby@sun.com 
141310242Schris.kirby@sun.com 		mutex_enter(&ds->ds_lock);
141410242Schris.kirby@sun.com 		/*
141510242Schris.kirby@sun.com 		 * Can't delete a branch point. However, if we're destroying
141610242Schris.kirby@sun.com 		 * a clone and removing its origin due to it having a user
141710242Schris.kirby@sun.com 		 * hold count of 0 and having been marked for deferred destroy,
141810242Schris.kirby@sun.com 		 * it's OK for the origin to have a single clone.
141910242Schris.kirby@sun.com 		 */
142010242Schris.kirby@sun.com 		if (ds->ds_phys->ds_num_children >
142110242Schris.kirby@sun.com 		    (dsda->is_origin_rm ? 2 : 1)) {
142210242Schris.kirby@sun.com 			mutex_exit(&ds->ds_lock);
142310242Schris.kirby@sun.com 			return (EEXIST);
142410242Schris.kirby@sun.com 		}
142510242Schris.kirby@sun.com 		mutex_exit(&ds->ds_lock);
142610242Schris.kirby@sun.com 	} else if (dsl_dir_is_clone(ds->ds_dir)) {
142710242Schris.kirby@sun.com 		return (dsl_dataset_origin_check(dsda, arg2, tx));
142810242Schris.kirby@sun.com 	}
142910242Schris.kirby@sun.com 
14302199Sahrens 	/* XXX we should do some i/o error checking... */
14312199Sahrens 	return (0);
14322199Sahrens }
14332199Sahrens 
14346689Smaybee struct refsarg {
14356689Smaybee 	kmutex_t lock;
14366689Smaybee 	boolean_t gone;
14376689Smaybee 	kcondvar_t cv;
14386689Smaybee };
14396689Smaybee 
14406689Smaybee /* ARGSUSED */
14416689Smaybee static void
14426689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv)
14436689Smaybee {
14446689Smaybee 	struct refsarg *arg = argv;
14456689Smaybee 
14466689Smaybee 	mutex_enter(&arg->lock);
14476689Smaybee 	arg->gone = TRUE;
14486689Smaybee 	cv_signal(&arg->cv);
14496689Smaybee 	mutex_exit(&arg->lock);
14506689Smaybee }
14516689Smaybee 
14526689Smaybee static void
14536689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag)
14546689Smaybee {
14556689Smaybee 	struct refsarg arg;
14566689Smaybee 
14576689Smaybee 	mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL);
14586689Smaybee 	cv_init(&arg.cv, NULL, CV_DEFAULT, NULL);
14596689Smaybee 	arg.gone = FALSE;
14606689Smaybee 	(void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys,
14616689Smaybee 	    dsl_dataset_refs_gone);
14626689Smaybee 	dmu_buf_rele(ds->ds_dbuf, tag);
14636689Smaybee 	mutex_enter(&arg.lock);
14646689Smaybee 	while (!arg.gone)
14656689Smaybee 		cv_wait(&arg.cv, &arg.lock);
14666689Smaybee 	ASSERT(arg.gone);
14676689Smaybee 	mutex_exit(&arg.lock);
14686689Smaybee 	ds->ds_dbuf = NULL;
14696689Smaybee 	ds->ds_phys = NULL;
14706689Smaybee 	mutex_destroy(&arg.lock);
14716689Smaybee 	cv_destroy(&arg.cv);
14726689Smaybee }
14736689Smaybee 
147410801SMatthew.Ahrens@Sun.COM static void
147510801SMatthew.Ahrens@Sun.COM remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, dmu_tx_t *tx)
147610801SMatthew.Ahrens@Sun.COM {
147710801SMatthew.Ahrens@Sun.COM 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
147810801SMatthew.Ahrens@Sun.COM 	uint64_t count;
147910801SMatthew.Ahrens@Sun.COM 	int err;
148010801SMatthew.Ahrens@Sun.COM 
148110801SMatthew.Ahrens@Sun.COM 	ASSERT(ds->ds_phys->ds_num_children >= 2);
148210801SMatthew.Ahrens@Sun.COM 	err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
148310801SMatthew.Ahrens@Sun.COM 	/*
148410801SMatthew.Ahrens@Sun.COM 	 * The err should not be ENOENT, but a bug in a previous version
148510801SMatthew.Ahrens@Sun.COM 	 * of the code could cause upgrade_clones_cb() to not set
148610801SMatthew.Ahrens@Sun.COM 	 * ds_next_snap_obj when it should, leading to a missing entry.
148710801SMatthew.Ahrens@Sun.COM 	 * If we knew that the pool was created after
148810801SMatthew.Ahrens@Sun.COM 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
148910801SMatthew.Ahrens@Sun.COM 	 * ENOENT.  However, at least we can check that we don't have
149010801SMatthew.Ahrens@Sun.COM 	 * too many entries in the next_clones_obj even after failing to
149110801SMatthew.Ahrens@Sun.COM 	 * remove this one.
149210801SMatthew.Ahrens@Sun.COM 	 */
149310801SMatthew.Ahrens@Sun.COM 	if (err != ENOENT) {
149410801SMatthew.Ahrens@Sun.COM 		VERIFY3U(err, ==, 0);
149510801SMatthew.Ahrens@Sun.COM 	}
149610801SMatthew.Ahrens@Sun.COM 	ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj,
149710801SMatthew.Ahrens@Sun.COM 	    &count));
149810801SMatthew.Ahrens@Sun.COM 	ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
149910801SMatthew.Ahrens@Sun.COM }
150010801SMatthew.Ahrens@Sun.COM 
15015367Sahrens void
15024543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
15032199Sahrens {
150410242Schris.kirby@sun.com 	struct dsl_ds_destroyarg *dsda = arg1;
150510242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
15062199Sahrens 	int err;
15072199Sahrens 	int after_branch_point = FALSE;
15082199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
15092199Sahrens 	objset_t *mos = dp->dp_meta_objset;
15102199Sahrens 	dsl_dataset_t *ds_prev = NULL;
15112199Sahrens 	uint64_t obj;
15122199Sahrens 
15136689Smaybee 	ASSERT(ds->ds_owner);
151410242Schris.kirby@sun.com 	ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1);
15152199Sahrens 	ASSERT(ds->ds_prev == NULL ||
15162199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
15172199Sahrens 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
15182199Sahrens 
151910242Schris.kirby@sun.com 	if (dsda->defer) {
152010242Schris.kirby@sun.com 		ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
152110242Schris.kirby@sun.com 		if (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1) {
152210242Schris.kirby@sun.com 			dmu_buf_will_dirty(ds->ds_dbuf, tx);
152310242Schris.kirby@sun.com 			ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
152410242Schris.kirby@sun.com 			return;
152510242Schris.kirby@sun.com 		}
152610242Schris.kirby@sun.com 	}
152710242Schris.kirby@sun.com 
15286689Smaybee 	/* signal any waiters that this dataset is going away */
15296689Smaybee 	mutex_enter(&ds->ds_lock);
15306689Smaybee 	ds->ds_owner = dsl_reaper;
15316689Smaybee 	cv_broadcast(&ds->ds_exclusive_cv);
15326689Smaybee 	mutex_exit(&ds->ds_lock);
15336689Smaybee 
15345378Sck153898 	/* Remove our reservation */
15355378Sck153898 	if (ds->ds_reserved != 0) {
153611022STom.Erickson@Sun.COM 		dsl_prop_setarg_t psa;
153711022STom.Erickson@Sun.COM 		uint64_t value = 0;
153811022STom.Erickson@Sun.COM 
153911022STom.Erickson@Sun.COM 		dsl_prop_setarg_init_uint64(&psa, "refreservation",
154011022STom.Erickson@Sun.COM 		    (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
154111022STom.Erickson@Sun.COM 		    &value);
154211022STom.Erickson@Sun.COM 		psa.psa_effective_value = 0;	/* predict default value */
154311022STom.Erickson@Sun.COM 
154411022STom.Erickson@Sun.COM 		dsl_dataset_set_reservation_sync(ds, &psa, cr, tx);
15455378Sck153898 		ASSERT3U(ds->ds_reserved, ==, 0);
15465378Sck153898 	}
15475378Sck153898 
15482199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
15492199Sahrens 
15507046Sahrens 	dsl_pool_ds_destroyed(ds, tx);
15517046Sahrens 
15522199Sahrens 	obj = ds->ds_object;
1553789Sahrens 
1554789Sahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1555789Sahrens 		if (ds->ds_prev) {
1556789Sahrens 			ds_prev = ds->ds_prev;
1557789Sahrens 		} else {
15586689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
15596689Smaybee 			    ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
1560789Sahrens 		}
1561789Sahrens 		after_branch_point =
1562789Sahrens 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
1563789Sahrens 
1564789Sahrens 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1565789Sahrens 		if (after_branch_point &&
15667046Sahrens 		    ds_prev->ds_phys->ds_next_clones_obj != 0) {
156710801SMatthew.Ahrens@Sun.COM 			remove_from_next_clones(ds_prev, obj, tx);
15687046Sahrens 			if (ds->ds_phys->ds_next_snap_obj != 0) {
15697046Sahrens 				VERIFY(0 == zap_add_int(mos,
15707046Sahrens 				    ds_prev->ds_phys->ds_next_clones_obj,
15717046Sahrens 				    ds->ds_phys->ds_next_snap_obj, tx));
15727046Sahrens 			}
15737046Sahrens 		}
15747046Sahrens 		if (after_branch_point &&
1575789Sahrens 		    ds->ds_phys->ds_next_snap_obj == 0) {
1576789Sahrens 			/* This clone is toast. */
1577789Sahrens 			ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1578789Sahrens 			ds_prev->ds_phys->ds_num_children--;
157910242Schris.kirby@sun.com 
158010242Schris.kirby@sun.com 			/*
158110242Schris.kirby@sun.com 			 * If the clone's origin has no other clones, no
158210242Schris.kirby@sun.com 			 * user holds, and has been marked for deferred
158310242Schris.kirby@sun.com 			 * deletion, then we should have done the necessary
158410242Schris.kirby@sun.com 			 * destroy setup for it.
158510242Schris.kirby@sun.com 			 */
158610242Schris.kirby@sun.com 			if (ds_prev->ds_phys->ds_num_children == 1 &&
158710242Schris.kirby@sun.com 			    ds_prev->ds_userrefs == 0 &&
158810242Schris.kirby@sun.com 			    DS_IS_DEFER_DESTROY(ds_prev)) {
158910242Schris.kirby@sun.com 				ASSERT3P(dsda->rm_origin, !=, NULL);
159010242Schris.kirby@sun.com 			} else {
159110242Schris.kirby@sun.com 				ASSERT3P(dsda->rm_origin, ==, NULL);
159210242Schris.kirby@sun.com 			}
1593789Sahrens 		} else if (!after_branch_point) {
1594789Sahrens 			ds_prev->ds_phys->ds_next_snap_obj =
1595789Sahrens 			    ds->ds_phys->ds_next_snap_obj;
1596789Sahrens 		}
1597789Sahrens 	}
1598789Sahrens 
1599789Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0) {
16002199Sahrens 		blkptr_t bp;
1601*12295SGeorge.Wilson@Sun.COM 		zio_t *pio;
1602789Sahrens 		dsl_dataset_t *ds_next;
1603789Sahrens 		uint64_t itor = 0;
16045378Sck153898 		uint64_t old_unique;
16057390SMatthew.Ahrens@Sun.COM 		int64_t used = 0, compressed = 0, uncompressed = 0;
1606789Sahrens 
16076689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
16086689Smaybee 		    ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
1609789Sahrens 		ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1610789Sahrens 
16115378Sck153898 		old_unique = dsl_dataset_unique(ds_next);
16125378Sck153898 
1613789Sahrens 		dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1614789Sahrens 		ds_next->ds_phys->ds_prev_snap_obj =
1615789Sahrens 		    ds->ds_phys->ds_prev_snap_obj;
1616789Sahrens 		ds_next->ds_phys->ds_prev_snap_txg =
1617789Sahrens 		    ds->ds_phys->ds_prev_snap_txg;
1618789Sahrens 		ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1619789Sahrens 		    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1620789Sahrens 
1621*12295SGeorge.Wilson@Sun.COM 		pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1622*12295SGeorge.Wilson@Sun.COM 
1623789Sahrens 		/*
1624789Sahrens 		 * Transfer to our deadlist (which will become next's
1625789Sahrens 		 * new deadlist) any entries from next's current
1626789Sahrens 		 * deadlist which were born before prev, and free the
1627789Sahrens 		 * other entries.
1628789Sahrens 		 *
1629789Sahrens 		 * XXX we're doing this long task with the config lock held
1630789Sahrens 		 */
16316689Smaybee 		while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) {
1632789Sahrens 			if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) {
16331544Seschrock 				VERIFY(0 == bplist_enqueue(&ds->ds_deadlist,
16341544Seschrock 				    &bp, tx));
1635789Sahrens 				if (ds_prev && !after_branch_point &&
1636789Sahrens 				    bp.blk_birth >
1637789Sahrens 				    ds_prev->ds_phys->ds_prev_snap_txg) {
1638789Sahrens 					ds_prev->ds_phys->ds_unique_bytes +=
163910922SJeff.Bonwick@Sun.COM 					    bp_get_dsize_sync(dp->dp_spa, &bp);
1640789Sahrens 				}
1641789Sahrens 			} else {
164210922SJeff.Bonwick@Sun.COM 				used += bp_get_dsize_sync(dp->dp_spa, &bp);
1643789Sahrens 				compressed += BP_GET_PSIZE(&bp);
1644789Sahrens 				uncompressed += BP_GET_UCSIZE(&bp);
1645*12295SGeorge.Wilson@Sun.COM 				dsl_free_sync(pio, dp, tx->tx_txg, &bp);
1646789Sahrens 			}
1647789Sahrens 		}
1648*12295SGeorge.Wilson@Sun.COM 		VERIFY3U(zio_wait(pio), ==, 0);
16497390SMatthew.Ahrens@Sun.COM 		ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes);
16507390SMatthew.Ahrens@Sun.COM 
16517390SMatthew.Ahrens@Sun.COM 		/* change snapused */
16527390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
16537390SMatthew.Ahrens@Sun.COM 		    -used, -compressed, -uncompressed, tx);
16547390SMatthew.Ahrens@Sun.COM 
1655789Sahrens 		/* free next's deadlist */
1656789Sahrens 		bplist_close(&ds_next->ds_deadlist);
1657789Sahrens 		bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx);
1658789Sahrens 
1659789Sahrens 		/* set next's deadlist to our deadlist */
16606689Smaybee 		bplist_close(&ds->ds_deadlist);
1661789Sahrens 		ds_next->ds_phys->ds_deadlist_obj =
1662789Sahrens 		    ds->ds_phys->ds_deadlist_obj;
16631544Seschrock 		VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos,
16641544Seschrock 		    ds_next->ds_phys->ds_deadlist_obj));
1665789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1666789Sahrens 
1667789Sahrens 		if (ds_next->ds_phys->ds_next_snap_obj != 0) {
1668789Sahrens 			/*
1669789Sahrens 			 * Update next's unique to include blocks which
1670789Sahrens 			 * were previously shared by only this snapshot
1671789Sahrens 			 * and it.  Those blocks will be born after the
1672789Sahrens 			 * prev snap and before this snap, and will have
1673789Sahrens 			 * died after the next snap and before the one
1674789Sahrens 			 * after that (ie. be on the snap after next's
1675789Sahrens 			 * deadlist).
1676789Sahrens 			 *
1677789Sahrens 			 * XXX we're doing this long task with the
1678789Sahrens 			 * config lock held
1679789Sahrens 			 */
1680789Sahrens 			dsl_dataset_t *ds_after_next;
16817390SMatthew.Ahrens@Sun.COM 			uint64_t space;
1682789Sahrens 
16836689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
16846689Smaybee 			    ds_next->ds_phys->ds_next_snap_obj,
16856689Smaybee 			    FTAG, &ds_after_next));
16867390SMatthew.Ahrens@Sun.COM 
16877390SMatthew.Ahrens@Sun.COM 			VERIFY(0 ==
16887390SMatthew.Ahrens@Sun.COM 			    bplist_space_birthrange(&ds_after_next->ds_deadlist,
16897390SMatthew.Ahrens@Sun.COM 			    ds->ds_phys->ds_prev_snap_txg,
16907390SMatthew.Ahrens@Sun.COM 			    ds->ds_phys->ds_creation_txg, &space));
16917390SMatthew.Ahrens@Sun.COM 			ds_next->ds_phys->ds_unique_bytes += space;
1692789Sahrens 
16936689Smaybee 			dsl_dataset_rele(ds_after_next, FTAG);
1694789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, NULL);
1695789Sahrens 		} else {
1696789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, ds);
16976689Smaybee 			dsl_dataset_drop_ref(ds_next->ds_prev, ds_next);
16986689Smaybee 			ds_next->ds_prev = NULL;
1699789Sahrens 			if (ds_prev) {
17006689Smaybee 				VERIFY(0 == dsl_dataset_get_ref(dp,
17016689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
17026689Smaybee 				    ds_next, &ds_next->ds_prev));
1703789Sahrens 			}
17045378Sck153898 
17055378Sck153898 			dsl_dataset_recalc_head_uniq(ds_next);
17065378Sck153898 
17075378Sck153898 			/*
17085378Sck153898 			 * Reduce the amount of our unconsmed refreservation
17095378Sck153898 			 * being charged to our parent by the amount of
17105378Sck153898 			 * new unique data we have gained.
17115378Sck153898 			 */
17125378Sck153898 			if (old_unique < ds_next->ds_reserved) {
17135378Sck153898 				int64_t mrsdelta;
17145378Sck153898 				uint64_t new_unique =
17155378Sck153898 				    ds_next->ds_phys->ds_unique_bytes;
17165378Sck153898 
17175378Sck153898 				ASSERT(old_unique <= new_unique);
17185378Sck153898 				mrsdelta = MIN(new_unique - old_unique,
17195378Sck153898 				    ds_next->ds_reserved - old_unique);
17207390SMatthew.Ahrens@Sun.COM 				dsl_dir_diduse_space(ds->ds_dir,
17217390SMatthew.Ahrens@Sun.COM 				    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
17225378Sck153898 			}
1723789Sahrens 		}
17246689Smaybee 		dsl_dataset_rele(ds_next, FTAG);
1725789Sahrens 	} else {
1726789Sahrens 		/*
1727789Sahrens 		 * There's no next snapshot, so this is a head dataset.
1728789Sahrens 		 * Destroy the deadlist.  Unless it's a clone, the
1729789Sahrens 		 * deadlist should be empty.  (If it's a clone, it's
1730789Sahrens 		 * safe to ignore the deadlist contents.)
1731789Sahrens 		 */
1732789Sahrens 		struct killarg ka;
1733789Sahrens 
1734789Sahrens 		ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist));
1735789Sahrens 		bplist_close(&ds->ds_deadlist);
1736789Sahrens 		bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1737789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1738789Sahrens 
1739789Sahrens 		/*
1740789Sahrens 		 * Free everything that we point to (that's born after
1741789Sahrens 		 * the previous snapshot, if we are a clone)
1742789Sahrens 		 *
17437390SMatthew.Ahrens@Sun.COM 		 * NB: this should be very quick, because we already
17447390SMatthew.Ahrens@Sun.COM 		 * freed all the objects in open context.
1745789Sahrens 		 */
17467390SMatthew.Ahrens@Sun.COM 		ka.ds = ds;
1747789Sahrens 		ka.tx = tx;
17487837SMatthew.Ahrens@Sun.COM 		err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
17497837SMatthew.Ahrens@Sun.COM 		    TRAVERSE_POST, kill_blkptr, &ka);
1750789Sahrens 		ASSERT3U(err, ==, 0);
17519390Schris.kirby@sun.com 		ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
17527390SMatthew.Ahrens@Sun.COM 		    ds->ds_phys->ds_unique_bytes == 0);
175310342Schris.kirby@sun.com 
175410342Schris.kirby@sun.com 		if (ds->ds_prev != NULL) {
175510342Schris.kirby@sun.com 			dsl_dataset_rele(ds->ds_prev, ds);
175610342Schris.kirby@sun.com 			ds->ds_prev = ds_prev = NULL;
175710342Schris.kirby@sun.com 		}
1758789Sahrens 	}
1759789Sahrens 
17606689Smaybee 	if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
17616689Smaybee 		/* Erase the link in the dir */
17626689Smaybee 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
17636689Smaybee 		ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
17646689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
1765789Sahrens 		err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1766789Sahrens 		ASSERT(err == 0);
1767789Sahrens 	} else {
1768789Sahrens 		/* remove from snapshot namespace */
1769789Sahrens 		dsl_dataset_t *ds_head;
17706689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
17716689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
17726689Smaybee 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
17732207Sahrens 		VERIFY(0 == dsl_dataset_get_snapname(ds));
1774789Sahrens #ifdef ZFS_DEBUG
1775789Sahrens 		{
1776789Sahrens 			uint64_t val;
17776492Stimh 
17786689Smaybee 			err = dsl_dataset_snap_lookup(ds_head,
17796492Stimh 			    ds->ds_snapname, &val);
1780789Sahrens 			ASSERT3U(err, ==, 0);
1781789Sahrens 			ASSERT3U(val, ==, obj);
1782789Sahrens 		}
1783789Sahrens #endif
17846689Smaybee 		err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
1785789Sahrens 		ASSERT(err == 0);
17866689Smaybee 		dsl_dataset_rele(ds_head, FTAG);
1787789Sahrens 	}
1788789Sahrens 
1789789Sahrens 	if (ds_prev && ds->ds_prev != ds_prev)
17906689Smaybee 		dsl_dataset_rele(ds_prev, FTAG);
1791789Sahrens 
17925094Slling 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
17934543Smarks 	spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx,
17944543Smarks 	    cr, "dataset = %llu", ds->ds_object);
17954543Smarks 
17967046Sahrens 	if (ds->ds_phys->ds_next_clones_obj != 0) {
17977046Sahrens 		uint64_t count;
17987046Sahrens 		ASSERT(0 == zap_count(mos,
17997046Sahrens 		    ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
18007046Sahrens 		VERIFY(0 == dmu_object_free(mos,
18017046Sahrens 		    ds->ds_phys->ds_next_clones_obj, tx));
18027046Sahrens 	}
18037390SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_props_obj != 0)
18047390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
180510242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj != 0)
180610242Schris.kirby@sun.com 		VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
18076689Smaybee 	dsl_dir_close(ds->ds_dir, ds);
18086689Smaybee 	ds->ds_dir = NULL;
18096689Smaybee 	dsl_dataset_drain_refs(ds, tag);
18102199Sahrens 	VERIFY(0 == dmu_object_free(mos, obj, tx));
181110242Schris.kirby@sun.com 
181210242Schris.kirby@sun.com 	if (dsda->rm_origin) {
181310242Schris.kirby@sun.com 		/*
181410242Schris.kirby@sun.com 		 * Remove the origin of the clone we just destroyed.
181510242Schris.kirby@sun.com 		 */
181610242Schris.kirby@sun.com 		struct dsl_ds_destroyarg ndsda = {0};
181710242Schris.kirby@sun.com 
181810342Schris.kirby@sun.com 		ndsda.ds = dsda->rm_origin;
181910242Schris.kirby@sun.com 		dsl_dataset_destroy_sync(&ndsda, tag, cr, tx);
182010242Schris.kirby@sun.com 	}
18212199Sahrens }
18222199Sahrens 
18235378Sck153898 static int
18245378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
18255378Sck153898 {
18265378Sck153898 	uint64_t asize;
18275378Sck153898 
18285378Sck153898 	if (!dmu_tx_is_syncing(tx))
18295378Sck153898 		return (0);
18305378Sck153898 
18315378Sck153898 	/*
18325378Sck153898 	 * If there's an fs-only reservation, any blocks that might become
18335378Sck153898 	 * owned by the snapshot dataset must be accommodated by space
18345378Sck153898 	 * outside of the reservation.
18355378Sck153898 	 */
18365378Sck153898 	asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
18375378Sck153898 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE))
18385378Sck153898 		return (ENOSPC);
18395378Sck153898 
18405378Sck153898 	/*
18415378Sck153898 	 * Propogate any reserved space for this snapshot to other
18425378Sck153898 	 * snapshot checks in this sync group.
18435378Sck153898 	 */
18445378Sck153898 	if (asize > 0)
18455378Sck153898 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
18465378Sck153898 
18475378Sck153898 	return (0);
18485378Sck153898 }
18495378Sck153898 
18502199Sahrens /* ARGSUSED */
18512199Sahrens int
18522199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
18532199Sahrens {
18545367Sahrens 	dsl_dataset_t *ds = arg1;
18552199Sahrens 	const char *snapname = arg2;
18562199Sahrens 	int err;
18572199Sahrens 	uint64_t value;
1858789Sahrens 
1859789Sahrens 	/*
18602199Sahrens 	 * We don't allow multiple snapshots of the same txg.  If there
18612199Sahrens 	 * is already one, try again.
18622199Sahrens 	 */
18632199Sahrens 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
18642199Sahrens 		return (EAGAIN);
18652199Sahrens 
18662199Sahrens 	/*
18672199Sahrens 	 * Check for conflicting name snapshot name.
1868789Sahrens 	 */
18696689Smaybee 	err = dsl_dataset_snap_lookup(ds, snapname, &value);
18702199Sahrens 	if (err == 0)
18712199Sahrens 		return (EEXIST);
18722199Sahrens 	if (err != ENOENT)
18732199Sahrens 		return (err);
1874789Sahrens 
18753978Smmusante 	/*
18763978Smmusante 	 * Check that the dataset's name is not too long.  Name consists
18773978Smmusante 	 * of the dataset's length + 1 for the @-sign + snapshot name's length
18783978Smmusante 	 */
18793978Smmusante 	if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
18803978Smmusante 		return (ENAMETOOLONG);
18813978Smmusante 
18825378Sck153898 	err = dsl_dataset_snapshot_reserve_space(ds, tx);
18835378Sck153898 	if (err)
18845378Sck153898 		return (err);
18855378Sck153898 
18862199Sahrens 	ds->ds_trysnap_txg = tx->tx_txg;
1887789Sahrens 	return (0);
1888789Sahrens }
1889789Sahrens 
18902199Sahrens void
18914543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1892789Sahrens {
18935367Sahrens 	dsl_dataset_t *ds = arg1;
18942199Sahrens 	const char *snapname = arg2;
18952199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1896789Sahrens 	dmu_buf_t *dbuf;
1897789Sahrens 	dsl_dataset_phys_t *dsphys;
18987046Sahrens 	uint64_t dsobj, crtxg;
1899789Sahrens 	objset_t *mos = dp->dp_meta_objset;
1900789Sahrens 	int err;
1901789Sahrens 
19022199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1903789Sahrens 
19047046Sahrens 	/*
19057046Sahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
19067046Sahrens 	 */
19077046Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
19087046Sahrens 		crtxg = 1;
19097046Sahrens 	else
19107046Sahrens 		crtxg = tx->tx_txg;
19117046Sahrens 
1912928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1913928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
19141544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1915789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1916789Sahrens 	dsphys = dbuf->db_data;
19176689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
19182199Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1919789Sahrens 	dsphys->ds_fsid_guid = unique_create();
1920789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1921789Sahrens 	    sizeof (dsphys->ds_guid));
1922789Sahrens 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1923789Sahrens 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1924789Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1925789Sahrens 	dsphys->ds_num_children = 1;
1926789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
19277046Sahrens 	dsphys->ds_creation_txg = crtxg;
1928789Sahrens 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1929789Sahrens 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
1930789Sahrens 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1931789Sahrens 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
19322082Seschrock 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1933789Sahrens 	dsphys->ds_bp = ds->ds_phys->ds_bp;
19341544Seschrock 	dmu_buf_rele(dbuf, FTAG);
1935789Sahrens 
19362199Sahrens 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
19372199Sahrens 	if (ds->ds_prev) {
19387046Sahrens 		uint64_t next_clones_obj =
19397046Sahrens 		    ds->ds_prev->ds_phys->ds_next_clones_obj;
19402199Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1941789Sahrens 		    ds->ds_object ||
19422199Sahrens 		    ds->ds_prev->ds_phys->ds_num_children > 1);
19432199Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
19442199Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1945789Sahrens 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
19462199Sahrens 			    ds->ds_prev->ds_phys->ds_creation_txg);
19472199Sahrens 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
19487046Sahrens 		} else if (next_clones_obj != 0) {
194910801SMatthew.Ahrens@Sun.COM 			remove_from_next_clones(ds->ds_prev,
195010801SMatthew.Ahrens@Sun.COM 			    dsphys->ds_next_snap_obj, tx);
19517046Sahrens 			VERIFY3U(0, ==, zap_add_int(mos,
19527046Sahrens 			    next_clones_obj, dsobj, tx));
1953789Sahrens 		}
1954789Sahrens 	}
1955789Sahrens 
19565378Sck153898 	/*
19575378Sck153898 	 * If we have a reference-reservation on this dataset, we will
19585378Sck153898 	 * need to increase the amount of refreservation being charged
19595378Sck153898 	 * since our unique space is going to zero.
19605378Sck153898 	 */
19615378Sck153898 	if (ds->ds_reserved) {
19625378Sck153898 		int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved);
19637390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
19647390SMatthew.Ahrens@Sun.COM 		    add, 0, 0, tx);
19655378Sck153898 	}
19665378Sck153898 
1967789Sahrens 	bplist_close(&ds->ds_deadlist);
1968789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
19695712Sahrens 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1970789Sahrens 	ds->ds_phys->ds_prev_snap_obj = dsobj;
19717046Sahrens 	ds->ds_phys->ds_prev_snap_txg = crtxg;
1972789Sahrens 	ds->ds_phys->ds_unique_bytes = 0;
19735378Sck153898 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
19745378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1975789Sahrens 	ds->ds_phys->ds_deadlist_obj =
1976789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
19771544Seschrock 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
19781544Seschrock 	    ds->ds_phys->ds_deadlist_obj));
1979789Sahrens 
1980789Sahrens 	dprintf("snap '%s' -> obj %llu\n", snapname, dsobj);
1981789Sahrens 	err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1982789Sahrens 	    snapname, 8, 1, &dsobj, tx);
1983789Sahrens 	ASSERT(err == 0);
1984789Sahrens 
1985789Sahrens 	if (ds->ds_prev)
19866689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
19876689Smaybee 	VERIFY(0 == dsl_dataset_get_ref(dp,
19886689Smaybee 	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
19894543Smarks 
19907046Sahrens 	dsl_pool_ds_snapshotted(ds, tx);
19917046Sahrens 
199210373Schris.kirby@sun.com 	dsl_dir_snap_cmtime_update(ds->ds_dir);
199310373Schris.kirby@sun.com 
19944543Smarks 	spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr,
19954603Sahrens 	    "dataset = %llu", dsobj);
1996789Sahrens }
1997789Sahrens 
1998789Sahrens void
19993547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
2000789Sahrens {
2001789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
200210298SMatthew.Ahrens@Sun.COM 	ASSERT(ds->ds_objset != NULL);
2003789Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
2004789Sahrens 
20054787Sahrens 	/*
20064787Sahrens 	 * in case we had to change ds_fsid_guid when we opened it,
20074787Sahrens 	 * sync it out now.
20084787Sahrens 	 */
20094787Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
20104787Sahrens 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
20114787Sahrens 
2012789Sahrens 	dsl_dir_dirty(ds->ds_dir, tx);
201310298SMatthew.Ahrens@Sun.COM 	dmu_objset_sync(ds->ds_objset, zio, tx);
2014789Sahrens }
2015789Sahrens 
2016789Sahrens void
20172885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
2018789Sahrens {
20195378Sck153898 	uint64_t refd, avail, uobjs, aobjs;
20205378Sck153898 
20212885Sahrens 	dsl_dir_stats(ds->ds_dir, nv);
2022789Sahrens 
20235378Sck153898 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
20245378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
20255378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
20265378Sck153898 
20272885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
20282885Sahrens 	    ds->ds_phys->ds_creation_time);
20292885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
20302885Sahrens 	    ds->ds_phys->ds_creation_txg);
20315378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
20325378Sck153898 	    ds->ds_quota);
20335378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
20345378Sck153898 	    ds->ds_reserved);
20356643Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
20366643Seschrock 	    ds->ds_phys->ds_guid);
203710575SEric.Schrock@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
203810575SEric.Schrock@Sun.COM 	    dsl_dataset_unique(ds));
203910575SEric.Schrock@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
204010575SEric.Schrock@Sun.COM 	    ds->ds_object);
204111022STom.Erickson@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
204211022STom.Erickson@Sun.COM 	    ds->ds_userrefs);
204310242Schris.kirby@sun.com 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
204410242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
2045789Sahrens 
2046789Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
2047789Sahrens 		/*
2048789Sahrens 		 * This is a snapshot; override the dd's space used with
20492885Sahrens 		 * our unique space and compression ratio.
2050789Sahrens 		 */
20512885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
20522885Sahrens 		    ds->ds_phys->ds_unique_bytes);
20532885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
20542885Sahrens 		    ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
20552885Sahrens 		    (ds->ds_phys->ds_uncompressed_bytes * 100 /
20562885Sahrens 		    ds->ds_phys->ds_compressed_bytes));
2057789Sahrens 	}
2058789Sahrens }
2059789Sahrens 
20602885Sahrens void
20612885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2062789Sahrens {
20632885Sahrens 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
20642885Sahrens 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
20655367Sahrens 	stat->dds_guid = ds->ds_phys->ds_guid;
20662885Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
20672885Sahrens 		stat->dds_is_snapshot = B_TRUE;
20682885Sahrens 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
20698228SEric.Taylor@Sun.COM 	} else {
20708228SEric.Taylor@Sun.COM 		stat->dds_is_snapshot = B_FALSE;
20718228SEric.Taylor@Sun.COM 		stat->dds_num_clones = 0;
20722885Sahrens 	}
20732885Sahrens 
20742885Sahrens 	/* clone origin is really a dsl_dir thing... */
20755446Sahrens 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
20767046Sahrens 	if (dsl_dir_is_clone(ds->ds_dir)) {
20772885Sahrens 		dsl_dataset_t *ods;
20782885Sahrens 
20796689Smaybee 		VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
20806689Smaybee 		    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
20815367Sahrens 		dsl_dataset_name(ods, stat->dds_origin);
20826689Smaybee 		dsl_dataset_drop_ref(ods, FTAG);
20838228SEric.Taylor@Sun.COM 	} else {
20848228SEric.Taylor@Sun.COM 		stat->dds_origin[0] = '\0';
20852885Sahrens 	}
20865446Sahrens 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
20872885Sahrens }
20882885Sahrens 
20892885Sahrens uint64_t
20902885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
20912885Sahrens {
20924787Sahrens 	return (ds->ds_fsid_guid);
20932885Sahrens }
20942885Sahrens 
20952885Sahrens void
20962885Sahrens dsl_dataset_space(dsl_dataset_t *ds,
20972885Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
20982885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
20992885Sahrens {
21002885Sahrens 	*refdbytesp = ds->ds_phys->ds_used_bytes;
21012885Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
21025378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
21035378Sck153898 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
21045378Sck153898 	if (ds->ds_quota != 0) {
21055378Sck153898 		/*
21065378Sck153898 		 * Adjust available bytes according to refquota
21075378Sck153898 		 */
21085378Sck153898 		if (*refdbytesp < ds->ds_quota)
21095378Sck153898 			*availbytesp = MIN(*availbytesp,
21105378Sck153898 			    ds->ds_quota - *refdbytesp);
21115378Sck153898 		else
21125378Sck153898 			*availbytesp = 0;
21135378Sck153898 	}
21142885Sahrens 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
21152885Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
2116789Sahrens }
2117789Sahrens 
21185326Sek110237 boolean_t
21195326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
21205326Sek110237 {
21215326Sek110237 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
21225326Sek110237 
21235326Sek110237 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
21245326Sek110237 	    dsl_pool_sync_context(dp));
21255326Sek110237 	if (ds->ds_prev == NULL)
21265326Sek110237 		return (B_FALSE);
21275326Sek110237 	if (ds->ds_phys->ds_bp.blk_birth >
21285326Sek110237 	    ds->ds_prev->ds_phys->ds_creation_txg)
21295326Sek110237 		return (B_TRUE);
21305326Sek110237 	return (B_FALSE);
21315326Sek110237 }
21325326Sek110237 
21332199Sahrens /* ARGSUSED */
2134789Sahrens static int
21352199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
2136789Sahrens {
21372199Sahrens 	dsl_dataset_t *ds = arg1;
21382199Sahrens 	char *newsnapname = arg2;
21392199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
21402199Sahrens 	dsl_dataset_t *hds;
21412199Sahrens 	uint64_t val;
2142789Sahrens 	int err;
2143789Sahrens 
21446689Smaybee 	err = dsl_dataset_hold_obj(dd->dd_pool,
21456689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds);
2146789Sahrens 	if (err)
2147789Sahrens 		return (err);
2148789Sahrens 
21492199Sahrens 	/* new name better not be in use */
21506689Smaybee 	err = dsl_dataset_snap_lookup(hds, newsnapname, &val);
21516689Smaybee 	dsl_dataset_rele(hds, FTAG);
2152789Sahrens 
21532199Sahrens 	if (err == 0)
21542199Sahrens 		err = EEXIST;
21552199Sahrens 	else if (err == ENOENT)
21562199Sahrens 		err = 0;
21574007Smmusante 
21584007Smmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
21594007Smmusante 	if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
21604007Smmusante 		err = ENAMETOOLONG;
21614007Smmusante 
21622199Sahrens 	return (err);
21632199Sahrens }
2164789Sahrens 
21652199Sahrens static void
21664543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2,
21674543Smarks     cred_t *cr, dmu_tx_t *tx)
21682199Sahrens {
21692199Sahrens 	dsl_dataset_t *ds = arg1;
21704543Smarks 	const char *newsnapname = arg2;
21712199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
21722199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
21732199Sahrens 	dsl_dataset_t *hds;
21742199Sahrens 	int err;
2175789Sahrens 
21762199Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2177789Sahrens 
21786689Smaybee 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
21796689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
2180789Sahrens 
21812199Sahrens 	VERIFY(0 == dsl_dataset_get_snapname(ds));
21826689Smaybee 	err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
2183789Sahrens 	ASSERT3U(err, ==, 0);
21842199Sahrens 	mutex_enter(&ds->ds_lock);
21852199Sahrens 	(void) strcpy(ds->ds_snapname, newsnapname);
21862199Sahrens 	mutex_exit(&ds->ds_lock);
21872199Sahrens 	err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
21882199Sahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2189789Sahrens 	ASSERT3U(err, ==, 0);
2190789Sahrens 
21914543Smarks 	spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
21924543Smarks 	    cr, "dataset = %llu", ds->ds_object);
21936689Smaybee 	dsl_dataset_rele(hds, FTAG);
2194789Sahrens }
2195789Sahrens 
21965326Sek110237 struct renamesnaparg {
21974007Smmusante 	dsl_sync_task_group_t *dstg;
21984007Smmusante 	char failed[MAXPATHLEN];
21994007Smmusante 	char *oldsnap;
22004007Smmusante 	char *newsnap;
22014007Smmusante };
22024007Smmusante 
22034007Smmusante static int
220411209SMatthew.Ahrens@Sun.COM dsl_snapshot_rename_one(const char *name, void *arg)
22054007Smmusante {
22065326Sek110237 	struct renamesnaparg *ra = arg;
22074007Smmusante 	dsl_dataset_t *ds = NULL;
220811209SMatthew.Ahrens@Sun.COM 	char *snapname;
22094007Smmusante 	int err;
22104007Smmusante 
221111209SMatthew.Ahrens@Sun.COM 	snapname = kmem_asprintf("%s@%s", name, ra->oldsnap);
221211209SMatthew.Ahrens@Sun.COM 	(void) strlcpy(ra->failed, snapname, sizeof (ra->failed));
22134543Smarks 
22144543Smarks 	/*
22154543Smarks 	 * For recursive snapshot renames the parent won't be changing
22164543Smarks 	 * so we just pass name for both the to/from argument.
22174543Smarks 	 */
221811209SMatthew.Ahrens@Sun.COM 	err = zfs_secpolicy_rename_perms(snapname, snapname, CRED());
221911209SMatthew.Ahrens@Sun.COM 	if (err != 0) {
222011209SMatthew.Ahrens@Sun.COM 		strfree(snapname);
222111209SMatthew.Ahrens@Sun.COM 		return (err == ENOENT ? 0 : err);
22224543Smarks 	}
22234543Smarks 
22246689Smaybee #ifdef _KERNEL
22256689Smaybee 	/*
22266689Smaybee 	 * For all filesystems undergoing rename, we'll need to unmount it.
22276689Smaybee 	 */
222811209SMatthew.Ahrens@Sun.COM 	(void) zfs_unmount_snap(snapname, NULL);
22296689Smaybee #endif
223011209SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(snapname, ra->dstg, &ds);
223111609SMatthew.Ahrens@Sun.COM 	strfree(snapname);
223211609SMatthew.Ahrens@Sun.COM 	if (err != 0)
223311209SMatthew.Ahrens@Sun.COM 		return (err == ENOENT ? 0 : err);
22344007Smmusante 
22354007Smmusante 	dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
22364007Smmusante 	    dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
22374007Smmusante 
22384007Smmusante 	return (0);
22394007Smmusante }
22404007Smmusante 
22414007Smmusante static int
22424007Smmusante dsl_recursive_rename(char *oldname, const char *newname)
22434007Smmusante {
22444007Smmusante 	int err;
22455326Sek110237 	struct renamesnaparg *ra;
22464007Smmusante 	dsl_sync_task_t *dst;
22474007Smmusante 	spa_t *spa;
22484007Smmusante 	char *cp, *fsname = spa_strdup(oldname);
224911209SMatthew.Ahrens@Sun.COM 	int len = strlen(oldname) + 1;
22504007Smmusante 
22514007Smmusante 	/* truncate the snapshot name to get the fsname */
22524007Smmusante 	cp = strchr(fsname, '@');
22534007Smmusante 	*cp = '\0';
22544007Smmusante 
22554603Sahrens 	err = spa_open(fsname, &spa, FTAG);
22564007Smmusante 	if (err) {
225711209SMatthew.Ahrens@Sun.COM 		kmem_free(fsname, len);
22584007Smmusante 		return (err);
22594007Smmusante 	}
22605326Sek110237 	ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP);
22614007Smmusante 	ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
22624007Smmusante 
22634007Smmusante 	ra->oldsnap = strchr(oldname, '@') + 1;
22644007Smmusante 	ra->newsnap = strchr(newname, '@') + 1;
22654007Smmusante 	*ra->failed = '\0';
22664007Smmusante 
22674007Smmusante 	err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
22684007Smmusante 	    DS_FIND_CHILDREN);
226911209SMatthew.Ahrens@Sun.COM 	kmem_free(fsname, len);
22704007Smmusante 
22714007Smmusante 	if (err == 0) {
22724007Smmusante 		err = dsl_sync_task_group_wait(ra->dstg);
22734007Smmusante 	}
22744007Smmusante 
22754007Smmusante 	for (dst = list_head(&ra->dstg->dstg_tasks); dst;
22764007Smmusante 	    dst = list_next(&ra->dstg->dstg_tasks, dst)) {
22774007Smmusante 		dsl_dataset_t *ds = dst->dst_arg1;
22784007Smmusante 		if (dst->dst_err) {
22794007Smmusante 			dsl_dir_name(ds->ds_dir, ra->failed);
228011209SMatthew.Ahrens@Sun.COM 			(void) strlcat(ra->failed, "@", sizeof (ra->failed));
228111209SMatthew.Ahrens@Sun.COM 			(void) strlcat(ra->failed, ra->newsnap,
228211209SMatthew.Ahrens@Sun.COM 			    sizeof (ra->failed));
22834007Smmusante 		}
22846689Smaybee 		dsl_dataset_rele(ds, ra->dstg);
22854007Smmusante 	}
22864007Smmusante 
22874543Smarks 	if (err)
228811209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(oldname, ra->failed, sizeof (ra->failed));
22894007Smmusante 
22904007Smmusante 	dsl_sync_task_group_destroy(ra->dstg);
22915326Sek110237 	kmem_free(ra, sizeof (struct renamesnaparg));
22924007Smmusante 	spa_close(spa, FTAG);
22934007Smmusante 	return (err);
22944007Smmusante }
22954007Smmusante 
22964569Smmusante static int
229711209SMatthew.Ahrens@Sun.COM dsl_valid_rename(const char *oldname, void *arg)
22984569Smmusante {
22994569Smmusante 	int delta = *(int *)arg;
23004569Smmusante 
23014569Smmusante 	if (strlen(oldname) + delta >= MAXNAMELEN)
23024569Smmusante 		return (ENAMETOOLONG);
23034569Smmusante 
23044569Smmusante 	return (0);
23054569Smmusante }
23064569Smmusante 
2307789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename
2308789Sahrens int
23096689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive)
2310789Sahrens {
2311789Sahrens 	dsl_dir_t *dd;
23122199Sahrens 	dsl_dataset_t *ds;
2313789Sahrens 	const char *tail;
2314789Sahrens 	int err;
2315789Sahrens 
23162199Sahrens 	err = dsl_dir_open(oldname, FTAG, &dd, &tail);
23171544Seschrock 	if (err)
23181544Seschrock 		return (err);
231911701SSanjeev.Bagewadi@Sun.COM 
2320789Sahrens 	if (tail == NULL) {
23214569Smmusante 		int delta = strlen(newname) - strlen(oldname);
23224569Smmusante 
23237046Sahrens 		/* if we're growing, validate child name lengths */
23244569Smmusante 		if (delta > 0)
23254569Smmusante 			err = dmu_objset_find(oldname, dsl_valid_rename,
23264569Smmusante 			    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
23274569Smmusante 
232811823SMatthew.Ahrens@Sun.COM 		if (err == 0)
23294569Smmusante 			err = dsl_dir_rename(dd, newname);
2330789Sahrens 		dsl_dir_close(dd, FTAG);
2331789Sahrens 		return (err);
2332789Sahrens 	}
233311701SSanjeev.Bagewadi@Sun.COM 
2334789Sahrens 	if (tail[0] != '@') {
233510588SEric.Taylor@Sun.COM 		/* the name ended in a nonexistent component */
2336789Sahrens 		dsl_dir_close(dd, FTAG);
2337789Sahrens 		return (ENOENT);
2338789Sahrens 	}
2339789Sahrens 
23402199Sahrens 	dsl_dir_close(dd, FTAG);
23412199Sahrens 
23422199Sahrens 	/* new name must be snapshot in same filesystem */
23432199Sahrens 	tail = strchr(newname, '@');
23442199Sahrens 	if (tail == NULL)
23452199Sahrens 		return (EINVAL);
23462199Sahrens 	tail++;
23472199Sahrens 	if (strncmp(oldname, newname, tail - newname) != 0)
23482199Sahrens 		return (EXDEV);
2349789Sahrens 
23504007Smmusante 	if (recursive) {
23514007Smmusante 		err = dsl_recursive_rename(oldname, newname);
23524007Smmusante 	} else {
23536689Smaybee 		err = dsl_dataset_hold(oldname, FTAG, &ds);
23544007Smmusante 		if (err)
23554007Smmusante 			return (err);
23562199Sahrens 
23574007Smmusante 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
23584007Smmusante 		    dsl_dataset_snapshot_rename_check,
23594007Smmusante 		    dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
23602199Sahrens 
23616689Smaybee 		dsl_dataset_rele(ds, FTAG);
23624007Smmusante 	}
23632199Sahrens 
2364789Sahrens 	return (err);
2365789Sahrens }
23662082Seschrock 
23677046Sahrens struct promotenode {
23686689Smaybee 	list_node_t link;
23696689Smaybee 	dsl_dataset_t *ds;
23706689Smaybee };
23716689Smaybee 
23722199Sahrens struct promotearg {
23737390SMatthew.Ahrens@Sun.COM 	list_t shared_snaps, origin_snaps, clone_snaps;
23747390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *origin_origin, *origin_head;
23757390SMatthew.Ahrens@Sun.COM 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
237610588SEric.Taylor@Sun.COM 	char *err_ds;
23772199Sahrens };
23782199Sahrens 
23797390SMatthew.Ahrens@Sun.COM static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
23807390SMatthew.Ahrens@Sun.COM 
23814543Smarks /* ARGSUSED */
23822082Seschrock static int
23832199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
23842082Seschrock {
23852199Sahrens 	dsl_dataset_t *hds = arg1;
23862199Sahrens 	struct promotearg *pa = arg2;
23877390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap = list_head(&pa->shared_snaps);
23886689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
23896689Smaybee 	int err;
23902199Sahrens 
23917046Sahrens 	/* Check that it is a real clone */
23927046Sahrens 	if (!dsl_dir_is_clone(hds->ds_dir))
23932082Seschrock 		return (EINVAL);
23942082Seschrock 
23952199Sahrens 	/* Since this is so expensive, don't do the preliminary check */
23962199Sahrens 	if (!dmu_tx_is_syncing(tx))
23972199Sahrens 		return (0);
23982199Sahrens 
23996689Smaybee 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)
24006689Smaybee 		return (EXDEV);
24012082Seschrock 
24025367Sahrens 	/* compute origin's new unique space */
24037390SMatthew.Ahrens@Sun.COM 	snap = list_tail(&pa->clone_snaps);
24047390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
24057390SMatthew.Ahrens@Sun.COM 	err = bplist_space_birthrange(&snap->ds->ds_deadlist,
24067390SMatthew.Ahrens@Sun.COM 	    origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, &pa->unique);
24077390SMatthew.Ahrens@Sun.COM 	if (err)
24086689Smaybee 		return (err);
24096689Smaybee 
24106689Smaybee 	/*
24116689Smaybee 	 * Walk the snapshots that we are moving
24126689Smaybee 	 *
24137390SMatthew.Ahrens@Sun.COM 	 * Compute space to transfer.  Consider the incremental changes
24147390SMatthew.Ahrens@Sun.COM 	 * to used for each snapshot:
24157390SMatthew.Ahrens@Sun.COM 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
24167390SMatthew.Ahrens@Sun.COM 	 * So each snapshot gave birth to:
24177390SMatthew.Ahrens@Sun.COM 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
24186689Smaybee 	 * So a sequence would look like:
24197390SMatthew.Ahrens@Sun.COM 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
24206689Smaybee 	 * Which simplifies to:
24217390SMatthew.Ahrens@Sun.COM 	 * uN + kN + kN-1 + ... + k1 + k0
24226689Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
24237390SMatthew.Ahrens@Sun.COM 	 * uN + kN + kN-1 + ... + kM - uM-1
24246689Smaybee 	 */
24256689Smaybee 	pa->used = origin_ds->ds_phys->ds_used_bytes;
24266689Smaybee 	pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
24276689Smaybee 	pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
24287390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(&pa->shared_snaps); snap;
24297390SMatthew.Ahrens@Sun.COM 	    snap = list_next(&pa->shared_snaps, snap)) {
24302082Seschrock 		uint64_t val, dlused, dlcomp, dluncomp;
24316689Smaybee 		dsl_dataset_t *ds = snap->ds;
24322082Seschrock 
24332082Seschrock 		/* Check that the snapshot name does not conflict */
24347390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_get_snapname(ds));
24356689Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
243610588SEric.Taylor@Sun.COM 		if (err == 0) {
243710588SEric.Taylor@Sun.COM 			err = EEXIST;
243810588SEric.Taylor@Sun.COM 			goto out;
243910588SEric.Taylor@Sun.COM 		}
24406689Smaybee 		if (err != ENOENT)
244110588SEric.Taylor@Sun.COM 			goto out;
24426689Smaybee 
24436689Smaybee 		/* The very first snapshot does not have a deadlist */
24447390SMatthew.Ahrens@Sun.COM 		if (ds->ds_phys->ds_prev_snap_obj == 0)
24457390SMatthew.Ahrens@Sun.COM 			continue;
24467390SMatthew.Ahrens@Sun.COM 
24477390SMatthew.Ahrens@Sun.COM 		if (err = bplist_space(&ds->ds_deadlist,
24487390SMatthew.Ahrens@Sun.COM 		    &dlused, &dlcomp, &dluncomp))
244910588SEric.Taylor@Sun.COM 			goto out;
24507390SMatthew.Ahrens@Sun.COM 		pa->used += dlused;
24517390SMatthew.Ahrens@Sun.COM 		pa->comp += dlcomp;
24527390SMatthew.Ahrens@Sun.COM 		pa->uncomp += dluncomp;
24537390SMatthew.Ahrens@Sun.COM 	}
24546689Smaybee 
24556689Smaybee 	/*
24566689Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
24576689Smaybee 	 * so we need to subtract out the clone origin's used space.
24586689Smaybee 	 */
24597390SMatthew.Ahrens@Sun.COM 	if (pa->origin_origin) {
24607390SMatthew.Ahrens@Sun.COM 		pa->used -= pa->origin_origin->ds_phys->ds_used_bytes;
24617390SMatthew.Ahrens@Sun.COM 		pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes;
24627390SMatthew.Ahrens@Sun.COM 		pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes;
24632082Seschrock 	}
24642082Seschrock 
24652082Seschrock 	/* Check that there is enough space here */
24667390SMatthew.Ahrens@Sun.COM 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
24677390SMatthew.Ahrens@Sun.COM 	    pa->used);
24687390SMatthew.Ahrens@Sun.COM 	if (err)
24697390SMatthew.Ahrens@Sun.COM 		return (err);
24707390SMatthew.Ahrens@Sun.COM 
24717390SMatthew.Ahrens@Sun.COM 	/*
24727390SMatthew.Ahrens@Sun.COM 	 * Compute the amounts of space that will be used by snapshots
24737390SMatthew.Ahrens@Sun.COM 	 * after the promotion (for both origin and clone).  For each,
24747390SMatthew.Ahrens@Sun.COM 	 * it is the amount of space that will be on all of their
24757390SMatthew.Ahrens@Sun.COM 	 * deadlists (that was not born before their new origin).
24767390SMatthew.Ahrens@Sun.COM 	 */
24777390SMatthew.Ahrens@Sun.COM 	if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
24787390SMatthew.Ahrens@Sun.COM 		uint64_t space;
24797390SMatthew.Ahrens@Sun.COM 
24807390SMatthew.Ahrens@Sun.COM 		/*
24817390SMatthew.Ahrens@Sun.COM 		 * Note, typically this will not be a clone of a clone,
24827390SMatthew.Ahrens@Sun.COM 		 * so snap->ds->ds_origin_txg will be < TXG_INITIAL, so
24837390SMatthew.Ahrens@Sun.COM 		 * these snaplist_space() -> bplist_space_birthrange()
24847390SMatthew.Ahrens@Sun.COM 		 * calls will be fast because they do not have to
24857390SMatthew.Ahrens@Sun.COM 		 * iterate over all bps.
24867390SMatthew.Ahrens@Sun.COM 		 */
24877390SMatthew.Ahrens@Sun.COM 		snap = list_head(&pa->origin_snaps);
24887390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->shared_snaps,
24897390SMatthew.Ahrens@Sun.COM 		    snap->ds->ds_origin_txg, &pa->cloneusedsnap);
24907390SMatthew.Ahrens@Sun.COM 		if (err)
24917390SMatthew.Ahrens@Sun.COM 			return (err);
24927390SMatthew.Ahrens@Sun.COM 
24937390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->clone_snaps,
24947390SMatthew.Ahrens@Sun.COM 		    snap->ds->ds_origin_txg, &space);
24957390SMatthew.Ahrens@Sun.COM 		if (err)
24967390SMatthew.Ahrens@Sun.COM 			return (err);
24977390SMatthew.Ahrens@Sun.COM 		pa->cloneusedsnap += space;
24986689Smaybee 	}
24997390SMatthew.Ahrens@Sun.COM 	if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
25007390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->origin_snaps,
25017390SMatthew.Ahrens@Sun.COM 		    origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap);
25027390SMatthew.Ahrens@Sun.COM 		if (err)
25037390SMatthew.Ahrens@Sun.COM 			return (err);
25047390SMatthew.Ahrens@Sun.COM 	}
25057390SMatthew.Ahrens@Sun.COM 
25067390SMatthew.Ahrens@Sun.COM 	return (0);
250710588SEric.Taylor@Sun.COM out:
250810588SEric.Taylor@Sun.COM 	pa->err_ds =  snap->ds->ds_snapname;
250910588SEric.Taylor@Sun.COM 	return (err);
25102199Sahrens }
25112082Seschrock 
25122199Sahrens static void
25134543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
25142199Sahrens {
25152199Sahrens 	dsl_dataset_t *hds = arg1;
25162199Sahrens 	struct promotearg *pa = arg2;
25177390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap = list_head(&pa->shared_snaps);
25186689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
25197390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *origin_head;
25202199Sahrens 	dsl_dir_t *dd = hds->ds_dir;
25212199Sahrens 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
25225367Sahrens 	dsl_dir_t *odd = NULL;
25237046Sahrens 	uint64_t oldnext_obj;
25247390SMatthew.Ahrens@Sun.COM 	int64_t delta;
25252199Sahrens 
25262199Sahrens 	ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
25272199Sahrens 
25287390SMatthew.Ahrens@Sun.COM 	snap = list_head(&pa->origin_snaps);
25297390SMatthew.Ahrens@Sun.COM 	origin_head = snap->ds;
25307390SMatthew.Ahrens@Sun.COM 
25312417Sahrens 	/*
25325367Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
25332417Sahrens 	 * changing.
25342417Sahrens 	 */
25355367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object,
25365367Sahrens 	    NULL, FTAG, &odd));
25372082Seschrock 
25386689Smaybee 	/* change origin's next snap */
25396689Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
25407046Sahrens 	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
25417390SMatthew.Ahrens@Sun.COM 	snap = list_tail(&pa->clone_snaps);
25427390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
25437390SMatthew.Ahrens@Sun.COM 	origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
25446689Smaybee 
25457046Sahrens 	/* change the origin's next clone */
25467046Sahrens 	if (origin_ds->ds_phys->ds_next_clones_obj) {
254710801SMatthew.Ahrens@Sun.COM 		remove_from_next_clones(origin_ds, snap->ds->ds_object, tx);
25487046Sahrens 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
25497046Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
25507046Sahrens 		    oldnext_obj, tx));
25517046Sahrens 	}
25527046Sahrens 
25536689Smaybee 	/* change origin */
25546689Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
25556689Smaybee 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
25566689Smaybee 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
25577390SMatthew.Ahrens@Sun.COM 	hds->ds_origin_txg = origin_head->ds_origin_txg;
25586689Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
25596689Smaybee 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
25607390SMatthew.Ahrens@Sun.COM 	origin_head->ds_origin_txg = origin_ds->ds_phys->ds_creation_txg;
25616689Smaybee 
25622082Seschrock 	/* move snapshots to this dir */
25637390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(&pa->shared_snaps); snap;
25647390SMatthew.Ahrens@Sun.COM 	    snap = list_next(&pa->shared_snaps, snap)) {
25656689Smaybee 		dsl_dataset_t *ds = snap->ds;
25662082Seschrock 
25677237Sek110237 		/* unregister props as dsl_dir is changing */
256810298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset) {
256910298SMatthew.Ahrens@Sun.COM 			dmu_objset_evict(ds->ds_objset);
257010298SMatthew.Ahrens@Sun.COM 			ds->ds_objset = NULL;
25717237Sek110237 		}
25722082Seschrock 		/* move snap name entry */
25737390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_get_snapname(ds));
25747390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_snap_remove(origin_head,
25756689Smaybee 		    ds->ds_snapname, tx));
25762199Sahrens 		VERIFY(0 == zap_add(dp->dp_meta_objset,
25772082Seschrock 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
25782082Seschrock 		    8, 1, &ds->ds_object, tx));
25792082Seschrock 		/* change containing dsl_dir */
25802082Seschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
25815367Sahrens 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
25822082Seschrock 		ds->ds_phys->ds_dir_obj = dd->dd_object;
25835367Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
25842082Seschrock 		dsl_dir_close(ds->ds_dir, ds);
25852199Sahrens 		VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
25862082Seschrock 		    NULL, ds, &ds->ds_dir));
25872082Seschrock 
25882082Seschrock 		ASSERT3U(dsl_prop_numcb(ds), ==, 0);
25897390SMatthew.Ahrens@Sun.COM 	}
25907390SMatthew.Ahrens@Sun.COM 
25917390SMatthew.Ahrens@Sun.COM 	/*
25927390SMatthew.Ahrens@Sun.COM 	 * Change space accounting.
25937390SMatthew.Ahrens@Sun.COM 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
25947390SMatthew.Ahrens@Sun.COM 	 * both be valid, or both be 0 (resulting in delta == 0).  This
25957390SMatthew.Ahrens@Sun.COM 	 * is true for each of {clone,origin} independently.
25967390SMatthew.Ahrens@Sun.COM 	 */
25977390SMatthew.Ahrens@Sun.COM 
25987390SMatthew.Ahrens@Sun.COM 	delta = pa->cloneusedsnap -
25997390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
26007390SMatthew.Ahrens@Sun.COM 	ASSERT3S(delta, >=, 0);
26017390SMatthew.Ahrens@Sun.COM 	ASSERT3U(pa->used, >=, delta);
26027390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
26037390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
26047390SMatthew.Ahrens@Sun.COM 	    pa->used - delta, pa->comp, pa->uncomp, tx);
26057390SMatthew.Ahrens@Sun.COM 
26067390SMatthew.Ahrens@Sun.COM 	delta = pa->originusedsnap -
26077390SMatthew.Ahrens@Sun.COM 	    odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
26087390SMatthew.Ahrens@Sun.COM 	ASSERT3S(delta, <=, 0);
26097390SMatthew.Ahrens@Sun.COM 	ASSERT3U(pa->used, >=, -delta);
26107390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
26117390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
26127390SMatthew.Ahrens@Sun.COM 	    -pa->used - delta, -pa->comp, -pa->uncomp, tx);
26137390SMatthew.Ahrens@Sun.COM 
26145367Sahrens 	origin_ds->ds_phys->ds_unique_bytes = pa->unique;
26152082Seschrock 
26164543Smarks 	/* log history record */
26174543Smarks 	spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
26186689Smaybee 	    cr, "dataset = %llu", hds->ds_object);
26194543Smarks 
26205367Sahrens 	dsl_dir_close(odd, FTAG);
26212082Seschrock }
26222082Seschrock 
26237390SMatthew.Ahrens@Sun.COM static char *snaplist_tag = "snaplist";
26247390SMatthew.Ahrens@Sun.COM /*
26257390SMatthew.Ahrens@Sun.COM  * Make a list of dsl_dataset_t's for the snapshots between first_obj
26267390SMatthew.Ahrens@Sun.COM  * (exclusive) and last_obj (inclusive).  The list will be in reverse
26277390SMatthew.Ahrens@Sun.COM  * order (last_obj will be the list_head()).  If first_obj == 0, do all
26287390SMatthew.Ahrens@Sun.COM  * snapshots back to this dataset's origin.
26297390SMatthew.Ahrens@Sun.COM  */
26307390SMatthew.Ahrens@Sun.COM static int
26317390SMatthew.Ahrens@Sun.COM snaplist_make(dsl_pool_t *dp, boolean_t own,
26327390SMatthew.Ahrens@Sun.COM     uint64_t first_obj, uint64_t last_obj, list_t *l)
26337390SMatthew.Ahrens@Sun.COM {
26347390SMatthew.Ahrens@Sun.COM 	uint64_t obj = last_obj;
26357390SMatthew.Ahrens@Sun.COM 
26367390SMatthew.Ahrens@Sun.COM 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock));
26377390SMatthew.Ahrens@Sun.COM 
26387390SMatthew.Ahrens@Sun.COM 	list_create(l, sizeof (struct promotenode),
26397390SMatthew.Ahrens@Sun.COM 	    offsetof(struct promotenode, link));
26407390SMatthew.Ahrens@Sun.COM 
26417390SMatthew.Ahrens@Sun.COM 	while (obj != first_obj) {
26427390SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
26437390SMatthew.Ahrens@Sun.COM 		struct promotenode *snap;
26447390SMatthew.Ahrens@Sun.COM 		int err;
26457390SMatthew.Ahrens@Sun.COM 
26467390SMatthew.Ahrens@Sun.COM 		if (own) {
26477390SMatthew.Ahrens@Sun.COM 			err = dsl_dataset_own_obj(dp, obj,
26487390SMatthew.Ahrens@Sun.COM 			    0, snaplist_tag, &ds);
26497390SMatthew.Ahrens@Sun.COM 			if (err == 0)
26507390SMatthew.Ahrens@Sun.COM 				dsl_dataset_make_exclusive(ds, snaplist_tag);
26517390SMatthew.Ahrens@Sun.COM 		} else {
26527390SMatthew.Ahrens@Sun.COM 			err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds);
26537390SMatthew.Ahrens@Sun.COM 		}
26547390SMatthew.Ahrens@Sun.COM 		if (err == ENOENT) {
26557390SMatthew.Ahrens@Sun.COM 			/* lost race with snapshot destroy */
26567390SMatthew.Ahrens@Sun.COM 			struct promotenode *last = list_tail(l);
26577390SMatthew.Ahrens@Sun.COM 			ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj);
26587390SMatthew.Ahrens@Sun.COM 			obj = last->ds->ds_phys->ds_prev_snap_obj;
26597390SMatthew.Ahrens@Sun.COM 			continue;
26607390SMatthew.Ahrens@Sun.COM 		} else if (err) {
26617390SMatthew.Ahrens@Sun.COM 			return (err);
26627390SMatthew.Ahrens@Sun.COM 		}
26637390SMatthew.Ahrens@Sun.COM 
26647390SMatthew.Ahrens@Sun.COM 		if (first_obj == 0)
26657390SMatthew.Ahrens@Sun.COM 			first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
26667390SMatthew.Ahrens@Sun.COM 
26677390SMatthew.Ahrens@Sun.COM 		snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP);
26687390SMatthew.Ahrens@Sun.COM 		snap->ds = ds;
26697390SMatthew.Ahrens@Sun.COM 		list_insert_tail(l, snap);
26707390SMatthew.Ahrens@Sun.COM 		obj = ds->ds_phys->ds_prev_snap_obj;
26717390SMatthew.Ahrens@Sun.COM 	}
26727390SMatthew.Ahrens@Sun.COM 
26737390SMatthew.Ahrens@Sun.COM 	return (0);
26747390SMatthew.Ahrens@Sun.COM }
26757390SMatthew.Ahrens@Sun.COM 
26767390SMatthew.Ahrens@Sun.COM static int
26777390SMatthew.Ahrens@Sun.COM snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
26787390SMatthew.Ahrens@Sun.COM {
26797390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap;
26807390SMatthew.Ahrens@Sun.COM 
26817390SMatthew.Ahrens@Sun.COM 	*spacep = 0;
26827390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
26837390SMatthew.Ahrens@Sun.COM 		uint64_t used;
26847390SMatthew.Ahrens@Sun.COM 		int err = bplist_space_birthrange(&snap->ds->ds_deadlist,
26857390SMatthew.Ahrens@Sun.COM 		    mintxg, UINT64_MAX, &used);
26867390SMatthew.Ahrens@Sun.COM 		if (err)
26877390SMatthew.Ahrens@Sun.COM 			return (err);
26887390SMatthew.Ahrens@Sun.COM 		*spacep += used;
26897390SMatthew.Ahrens@Sun.COM 	}
26907390SMatthew.Ahrens@Sun.COM 	return (0);
26917390SMatthew.Ahrens@Sun.COM }
26927390SMatthew.Ahrens@Sun.COM 
26937390SMatthew.Ahrens@Sun.COM static void
26947390SMatthew.Ahrens@Sun.COM snaplist_destroy(list_t *l, boolean_t own)
26957390SMatthew.Ahrens@Sun.COM {
26967390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap;
26977390SMatthew.Ahrens@Sun.COM 
26988779SMark.Musante@Sun.COM 	if (!l || !list_link_active(&l->list_head))
26997390SMatthew.Ahrens@Sun.COM 		return;
27007390SMatthew.Ahrens@Sun.COM 
27017390SMatthew.Ahrens@Sun.COM 	while ((snap = list_tail(l)) != NULL) {
27027390SMatthew.Ahrens@Sun.COM 		list_remove(l, snap);
27037390SMatthew.Ahrens@Sun.COM 		if (own)
27047390SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(snap->ds, snaplist_tag);
27057390SMatthew.Ahrens@Sun.COM 		else
27067390SMatthew.Ahrens@Sun.COM 			dsl_dataset_rele(snap->ds, snaplist_tag);
27077390SMatthew.Ahrens@Sun.COM 		kmem_free(snap, sizeof (struct promotenode));
27087390SMatthew.Ahrens@Sun.COM 	}
27097390SMatthew.Ahrens@Sun.COM 	list_destroy(l);
27107390SMatthew.Ahrens@Sun.COM }
27117390SMatthew.Ahrens@Sun.COM 
27127390SMatthew.Ahrens@Sun.COM /*
27137390SMatthew.Ahrens@Sun.COM  * Promote a clone.  Nomenclature note:
27147390SMatthew.Ahrens@Sun.COM  * "clone" or "cds": the original clone which is being promoted
27157390SMatthew.Ahrens@Sun.COM  * "origin" or "ods": the snapshot which is originally clone's origin
27167390SMatthew.Ahrens@Sun.COM  * "origin head" or "ohds": the dataset which is the head
27177390SMatthew.Ahrens@Sun.COM  * (filesystem/volume) for the origin
27187390SMatthew.Ahrens@Sun.COM  * "origin origin": the origin of the origin's filesystem (typically
27197390SMatthew.Ahrens@Sun.COM  * NULL, indicating that the clone is not a clone of a clone).
27207390SMatthew.Ahrens@Sun.COM  */
27212082Seschrock int
272210588SEric.Taylor@Sun.COM dsl_dataset_promote(const char *name, char *conflsnap)
27232082Seschrock {
27242082Seschrock 	dsl_dataset_t *ds;
27256689Smaybee 	dsl_dir_t *dd;
27266689Smaybee 	dsl_pool_t *dp;
27272082Seschrock 	dmu_object_info_t doi;
27287390SMatthew.Ahrens@Sun.COM 	struct promotearg pa = { 0 };
27297046Sahrens 	struct promotenode *snap;
27306689Smaybee 	int err;
27316689Smaybee 
27326689Smaybee 	err = dsl_dataset_hold(name, FTAG, &ds);
27332082Seschrock 	if (err)
27342082Seschrock 		return (err);
27356689Smaybee 	dd = ds->ds_dir;
27366689Smaybee 	dp = dd->dd_pool;
27376689Smaybee 
27386689Smaybee 	err = dmu_object_info(dp->dp_meta_objset,
27392082Seschrock 	    ds->ds_phys->ds_snapnames_zapobj, &doi);
27402082Seschrock 	if (err) {
27416689Smaybee 		dsl_dataset_rele(ds, FTAG);
27422082Seschrock 		return (err);
27432082Seschrock 	}
27442082Seschrock 
27457390SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) {
27467390SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
27477390SMatthew.Ahrens@Sun.COM 		return (EINVAL);
27487390SMatthew.Ahrens@Sun.COM 	}
27497390SMatthew.Ahrens@Sun.COM 
27502082Seschrock 	/*
27516689Smaybee 	 * We are going to inherit all the snapshots taken before our
27526689Smaybee 	 * origin (i.e., our new origin will be our parent's origin).
27536689Smaybee 	 * Take ownership of them so that we can rename them into our
27546689Smaybee 	 * namespace.
27556689Smaybee 	 */
27566689Smaybee 	rw_enter(&dp->dp_config_rwlock, RW_READER);
27577390SMatthew.Ahrens@Sun.COM 
27587390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj,
27597390SMatthew.Ahrens@Sun.COM 	    &pa.shared_snaps);
27607390SMatthew.Ahrens@Sun.COM 	if (err != 0)
27617390SMatthew.Ahrens@Sun.COM 		goto out;
27627390SMatthew.Ahrens@Sun.COM 
27637390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps);
27647390SMatthew.Ahrens@Sun.COM 	if (err != 0)
27657390SMatthew.Ahrens@Sun.COM 		goto out;
27667390SMatthew.Ahrens@Sun.COM 
27677390SMatthew.Ahrens@Sun.COM 	snap = list_head(&pa.shared_snaps);
27687390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
27697390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj,
27707390SMatthew.Ahrens@Sun.COM 	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps);
27717390SMatthew.Ahrens@Sun.COM 	if (err != 0)
27727390SMatthew.Ahrens@Sun.COM 		goto out;
27737390SMatthew.Ahrens@Sun.COM 
27747390SMatthew.Ahrens@Sun.COM 	if (dsl_dir_is_clone(snap->ds->ds_dir)) {
27757390SMatthew.Ahrens@Sun.COM 		err = dsl_dataset_own_obj(dp,
27767390SMatthew.Ahrens@Sun.COM 		    snap->ds->ds_dir->dd_phys->dd_origin_obj,
27777390SMatthew.Ahrens@Sun.COM 		    0, FTAG, &pa.origin_origin);
27787390SMatthew.Ahrens@Sun.COM 		if (err != 0)
27796689Smaybee 			goto out;
27806689Smaybee 	}
27817390SMatthew.Ahrens@Sun.COM 
27827390SMatthew.Ahrens@Sun.COM out:
27836689Smaybee 	rw_exit(&dp->dp_config_rwlock);
27846689Smaybee 
27856689Smaybee 	/*
27862082Seschrock 	 * Add in 128x the snapnames zapobj size, since we will be moving
27872082Seschrock 	 * a bunch of snapnames to the promoted ds, and dirtying their
27882082Seschrock 	 * bonus buffers.
27892082Seschrock 	 */
27907390SMatthew.Ahrens@Sun.COM 	if (err == 0) {
27917390SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_do(dp, dsl_dataset_promote_check,
27927390SMatthew.Ahrens@Sun.COM 		    dsl_dataset_promote_sync, ds, &pa,
279310922SJeff.Bonwick@Sun.COM 		    2 + 2 * doi.doi_physical_blocks_512);
279410588SEric.Taylor@Sun.COM 		if (err && pa.err_ds && conflsnap)
279510588SEric.Taylor@Sun.COM 			(void) strncpy(conflsnap, pa.err_ds, MAXNAMELEN);
27966689Smaybee 	}
27977390SMatthew.Ahrens@Sun.COM 
27987390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.shared_snaps, B_TRUE);
27997390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.clone_snaps, B_FALSE);
28007390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.origin_snaps, B_FALSE);
28017390SMatthew.Ahrens@Sun.COM 	if (pa.origin_origin)
28027390SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(pa.origin_origin, FTAG);
28036689Smaybee 	dsl_dataset_rele(ds, FTAG);
28042082Seschrock 	return (err);
28052082Seschrock }
28063912Slling 
28075367Sahrens struct cloneswaparg {
28085367Sahrens 	dsl_dataset_t *cds; /* clone dataset */
28095367Sahrens 	dsl_dataset_t *ohds; /* origin's head dataset */
28105367Sahrens 	boolean_t force;
28115481Sck153898 	int64_t unused_refres_delta; /* change in unconsumed refreservation */
28125367Sahrens };
28135326Sek110237 
28145326Sek110237 /* ARGSUSED */
28155326Sek110237 static int
28165326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx)
28175326Sek110237 {
28185367Sahrens 	struct cloneswaparg *csa = arg1;
28195326Sek110237 
28205367Sahrens 	/* they should both be heads */
28215367Sahrens 	if (dsl_dataset_is_snapshot(csa->cds) ||
28225367Sahrens 	    dsl_dataset_is_snapshot(csa->ohds))
28235326Sek110237 		return (EINVAL);
28245326Sek110237 
28255367Sahrens 	/* the branch point should be just before them */
28265367Sahrens 	if (csa->cds->ds_prev != csa->ohds->ds_prev)
28275326Sek110237 		return (EINVAL);
28285326Sek110237 
282910272SMatthew.Ahrens@Sun.COM 	/* cds should be the clone (unless they are unrelated) */
283010272SMatthew.Ahrens@Sun.COM 	if (csa->cds->ds_prev != NULL &&
283110272SMatthew.Ahrens@Sun.COM 	    csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap &&
283210272SMatthew.Ahrens@Sun.COM 	    csa->ohds->ds_object !=
283310272SMatthew.Ahrens@Sun.COM 	    csa->cds->ds_prev->ds_phys->ds_next_snap_obj)
28345367Sahrens 		return (EINVAL);
28355326Sek110237 
28365367Sahrens 	/* the clone should be a child of the origin */
28375367Sahrens 	if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir)
28385367Sahrens 		return (EINVAL);
28395326Sek110237 
28405367Sahrens 	/* ohds shouldn't be modified unless 'force' */
28415367Sahrens 	if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds))
28425367Sahrens 		return (ETXTBSY);
28435481Sck153898 
28445481Sck153898 	/* adjust amount of any unconsumed refreservation */
28455481Sck153898 	csa->unused_refres_delta =
28465481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
28475481Sck153898 	    csa->ohds->ds_phys->ds_unique_bytes) -
28485481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
28495481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
28505481Sck153898 
28515481Sck153898 	if (csa->unused_refres_delta > 0 &&
28525481Sck153898 	    csa->unused_refres_delta >
28535481Sck153898 	    dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE))
28545481Sck153898 		return (ENOSPC);
28555481Sck153898 
285610799SChris.Kirby@sun.com 	if (csa->ohds->ds_quota != 0 &&
285710799SChris.Kirby@sun.com 	    csa->cds->ds_phys->ds_unique_bytes > csa->ohds->ds_quota)
285810799SChris.Kirby@sun.com 		return (EDQUOT);
285910799SChris.Kirby@sun.com 
28605367Sahrens 	return (0);
28615326Sek110237 }
28625326Sek110237 
28635326Sek110237 /* ARGSUSED */
28645326Sek110237 static void
28655326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
28665326Sek110237 {
28675367Sahrens 	struct cloneswaparg *csa = arg1;
28685367Sahrens 	dsl_pool_t *dp = csa->cds->ds_dir->dd_pool;
28695326Sek110237 
28705481Sck153898 	ASSERT(csa->cds->ds_reserved == 0);
287110799SChris.Kirby@sun.com 	ASSERT(csa->ohds->ds_quota == 0 ||
287210799SChris.Kirby@sun.com 	    csa->cds->ds_phys->ds_unique_bytes <= csa->ohds->ds_quota);
28735481Sck153898 
28745367Sahrens 	dmu_buf_will_dirty(csa->cds->ds_dbuf, tx);
28755367Sahrens 	dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx);
28765326Sek110237 
287710298SMatthew.Ahrens@Sun.COM 	if (csa->cds->ds_objset != NULL) {
287810298SMatthew.Ahrens@Sun.COM 		dmu_objset_evict(csa->cds->ds_objset);
287910298SMatthew.Ahrens@Sun.COM 		csa->cds->ds_objset = NULL;
28805367Sahrens 	}
28815326Sek110237 
288210298SMatthew.Ahrens@Sun.COM 	if (csa->ohds->ds_objset != NULL) {
288310298SMatthew.Ahrens@Sun.COM 		dmu_objset_evict(csa->ohds->ds_objset);
288410298SMatthew.Ahrens@Sun.COM 		csa->ohds->ds_objset = NULL;
28855367Sahrens 	}
28865326Sek110237 
288710272SMatthew.Ahrens@Sun.COM 	/*
288810272SMatthew.Ahrens@Sun.COM 	 * Reset origin's unique bytes, if it exists.
288910272SMatthew.Ahrens@Sun.COM 	 */
289010272SMatthew.Ahrens@Sun.COM 	if (csa->cds->ds_prev) {
289110272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *origin = csa->cds->ds_prev;
289210272SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
289310272SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist,
289410272SMatthew.Ahrens@Sun.COM 		    origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
289510272SMatthew.Ahrens@Sun.COM 		    &origin->ds_phys->ds_unique_bytes));
289610272SMatthew.Ahrens@Sun.COM 	}
28975326Sek110237 
28985326Sek110237 	/* swap blkptrs */
28995326Sek110237 	{
29005326Sek110237 		blkptr_t tmp;
29015367Sahrens 		tmp = csa->ohds->ds_phys->ds_bp;
29025367Sahrens 		csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp;
29035367Sahrens 		csa->cds->ds_phys->ds_bp = tmp;
29045326Sek110237 	}
29055326Sek110237 
29065326Sek110237 	/* set dd_*_bytes */
29075326Sek110237 	{
29085326Sek110237 		int64_t dused, dcomp, duncomp;
29095326Sek110237 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
29105326Sek110237 		uint64_t odl_used, odl_comp, odl_uncomp;
29115326Sek110237 
29127390SMatthew.Ahrens@Sun.COM 		ASSERT3U(csa->cds->ds_dir->dd_phys->
29137390SMatthew.Ahrens@Sun.COM 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
29147390SMatthew.Ahrens@Sun.COM 
29155367Sahrens 		VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used,
29165326Sek110237 		    &cdl_comp, &cdl_uncomp));
29175367Sahrens 		VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used,
29185326Sek110237 		    &odl_comp, &odl_uncomp));
29197390SMatthew.Ahrens@Sun.COM 
29205367Sahrens 		dused = csa->cds->ds_phys->ds_used_bytes + cdl_used -
29215367Sahrens 		    (csa->ohds->ds_phys->ds_used_bytes + odl_used);
29225367Sahrens 		dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
29235367Sahrens 		    (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
29245367Sahrens 		duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
29255367Sahrens 		    cdl_uncomp -
29265367Sahrens 		    (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
29275326Sek110237 
29287390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD,
29295367Sahrens 		    dused, dcomp, duncomp, tx);
29307390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD,
29315367Sahrens 		    -dused, -dcomp, -duncomp, tx);
29327390SMatthew.Ahrens@Sun.COM 
29337390SMatthew.Ahrens@Sun.COM 		/*
29347390SMatthew.Ahrens@Sun.COM 		 * The difference in the space used by snapshots is the
29357390SMatthew.Ahrens@Sun.COM 		 * difference in snapshot space due to the head's
29367390SMatthew.Ahrens@Sun.COM 		 * deadlist (since that's the only thing that's
29377390SMatthew.Ahrens@Sun.COM 		 * changing that affects the snapused).
29387390SMatthew.Ahrens@Sun.COM 		 */
29397390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist,
29407390SMatthew.Ahrens@Sun.COM 		    csa->ohds->ds_origin_txg, UINT64_MAX, &cdl_used));
29417390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->ohds->ds_deadlist,
29427390SMatthew.Ahrens@Sun.COM 		    csa->ohds->ds_origin_txg, UINT64_MAX, &odl_used));
29437390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used,
29447390SMatthew.Ahrens@Sun.COM 		    DD_USED_HEAD, DD_USED_SNAP, tx);
29455367Sahrens 	}
29465367Sahrens 
29475367Sahrens #define	SWITCH64(x, y) \
29485367Sahrens 	{ \
29495367Sahrens 		uint64_t __tmp = (x); \
29505367Sahrens 		(x) = (y); \
29515367Sahrens 		(y) = __tmp; \
29525326Sek110237 	}
29535326Sek110237 
29545326Sek110237 	/* swap ds_*_bytes */
29555367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_used_bytes,
29565367Sahrens 	    csa->cds->ds_phys->ds_used_bytes);
29575367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
29585367Sahrens 	    csa->cds->ds_phys->ds_compressed_bytes);
29595367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
29605367Sahrens 	    csa->cds->ds_phys->ds_uncompressed_bytes);
29615481Sck153898 	SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
29625481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
29635481Sck153898 
29645481Sck153898 	/* apply any parent delta for change in unconsumed refreservation */
29657390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV,
29667390SMatthew.Ahrens@Sun.COM 	    csa->unused_refres_delta, 0, 0, tx);
29675326Sek110237 
29685326Sek110237 	/* swap deadlists */
29695367Sahrens 	bplist_close(&csa->cds->ds_deadlist);
29705367Sahrens 	bplist_close(&csa->ohds->ds_deadlist);
29715367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
29725367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj);
29735367Sahrens 	VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
29745367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj));
29755367Sahrens 	VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
29765367Sahrens 	    csa->ohds->ds_phys->ds_deadlist_obj));
29777837SMatthew.Ahrens@Sun.COM 
29787837SMatthew.Ahrens@Sun.COM 	dsl_pool_ds_clone_swapped(csa->ohds, csa->cds, tx);
29795326Sek110237 }
29805326Sek110237 
29815326Sek110237 /*
298210272SMatthew.Ahrens@Sun.COM  * Swap 'clone' with its origin head datasets.  Used at the end of "zfs
298310272SMatthew.Ahrens@Sun.COM  * recv" into an existing fs to swizzle the file system to the new
298410272SMatthew.Ahrens@Sun.COM  * version, and by "zfs rollback".  Can also be used to swap two
298510272SMatthew.Ahrens@Sun.COM  * independent head datasets if neither has any snapshots.
29865326Sek110237  */
29875326Sek110237 int
29885367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
29895367Sahrens     boolean_t force)
29905326Sek110237 {
29915367Sahrens 	struct cloneswaparg csa;
29926689Smaybee 	int error;
29936689Smaybee 
29946689Smaybee 	ASSERT(clone->ds_owner);
29956689Smaybee 	ASSERT(origin_head->ds_owner);
29966689Smaybee retry:
29976689Smaybee 	/* Need exclusive access for the swap */
29986689Smaybee 	rw_enter(&clone->ds_rwlock, RW_WRITER);
29996689Smaybee 	if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) {
30006689Smaybee 		rw_exit(&clone->ds_rwlock);
30016689Smaybee 		rw_enter(&origin_head->ds_rwlock, RW_WRITER);
30026689Smaybee 		if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) {
30036689Smaybee 			rw_exit(&origin_head->ds_rwlock);
30046689Smaybee 			goto retry;
30056689Smaybee 		}
30066689Smaybee 	}
30075367Sahrens 	csa.cds = clone;
30085367Sahrens 	csa.ohds = origin_head;
30095367Sahrens 	csa.force = force;
30106689Smaybee 	error = dsl_sync_task_do(clone->ds_dir->dd_pool,
30115326Sek110237 	    dsl_dataset_clone_swap_check,
30126689Smaybee 	    dsl_dataset_clone_swap_sync, &csa, NULL, 9);
30136689Smaybee 	return (error);
30145326Sek110237 }
30155326Sek110237 
30163912Slling /*
30173912Slling  * Given a pool name and a dataset object number in that pool,
30183912Slling  * return the name of that dataset.
30193912Slling  */
30203912Slling int
30213912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
30223912Slling {
30233912Slling 	spa_t *spa;
30243912Slling 	dsl_pool_t *dp;
30256689Smaybee 	dsl_dataset_t *ds;
30263912Slling 	int error;
30273912Slling 
30283912Slling 	if ((error = spa_open(pname, &spa, FTAG)) != 0)
30293912Slling 		return (error);
30303912Slling 	dp = spa_get_dsl(spa);
30313912Slling 	rw_enter(&dp->dp_config_rwlock, RW_READER);
30326689Smaybee 	if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) {
30336689Smaybee 		dsl_dataset_name(ds, buf);
30346689Smaybee 		dsl_dataset_rele(ds, FTAG);
30353912Slling 	}
30363912Slling 	rw_exit(&dp->dp_config_rwlock);
30373912Slling 	spa_close(spa, FTAG);
30383912Slling 
30396689Smaybee 	return (error);
30403912Slling }
30415378Sck153898 
30425378Sck153898 int
30435378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
30446689Smaybee     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
30455378Sck153898 {
30465378Sck153898 	int error = 0;
30475378Sck153898 
30485378Sck153898 	ASSERT3S(asize, >, 0);
30495378Sck153898 
30505831Sck153898 	/*
30515831Sck153898 	 * *ref_rsrv is the portion of asize that will come from any
30525831Sck153898 	 * unconsumed refreservation space.
30535831Sck153898 	 */
30545831Sck153898 	*ref_rsrv = 0;
30555831Sck153898 
30565378Sck153898 	mutex_enter(&ds->ds_lock);
30575378Sck153898 	/*
30585378Sck153898 	 * Make a space adjustment for reserved bytes.
30595378Sck153898 	 */
30605378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
30615378Sck153898 		ASSERT3U(*used, >=,
30625378Sck153898 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
30635378Sck153898 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
30645831Sck153898 		*ref_rsrv =
30655831Sck153898 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
30665378Sck153898 	}
30675378Sck153898 
30685378Sck153898 	if (!check_quota || ds->ds_quota == 0) {
30695378Sck153898 		mutex_exit(&ds->ds_lock);
30705378Sck153898 		return (0);
30715378Sck153898 	}
30725378Sck153898 	/*
30735378Sck153898 	 * If they are requesting more space, and our current estimate
30745378Sck153898 	 * is over quota, they get to try again unless the actual
30755378Sck153898 	 * on-disk is over quota and there are no pending changes (which
30765378Sck153898 	 * may free up space for us).
30775378Sck153898 	 */
30785378Sck153898 	if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) {
30795378Sck153898 		if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota)
30805378Sck153898 			error = ERESTART;
30815378Sck153898 		else
30825378Sck153898 			error = EDQUOT;
30835378Sck153898 	}
30845378Sck153898 	mutex_exit(&ds->ds_lock);
30855378Sck153898 
30865378Sck153898 	return (error);
30875378Sck153898 }
30885378Sck153898 
30895378Sck153898 /* ARGSUSED */
30905378Sck153898 static int
30915378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
30925378Sck153898 {
30935378Sck153898 	dsl_dataset_t *ds = arg1;
309411022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
309511022STom.Erickson@Sun.COM 	int err;
30965378Sck153898 
30975378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA)
30985378Sck153898 		return (ENOTSUP);
30995378Sck153898 
310011022STom.Erickson@Sun.COM 	if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
310111022STom.Erickson@Sun.COM 		return (err);
310211022STom.Erickson@Sun.COM 
310311022STom.Erickson@Sun.COM 	if (psa->psa_effective_value == 0)
31045378Sck153898 		return (0);
31055378Sck153898 
310611022STom.Erickson@Sun.COM 	if (psa->psa_effective_value < ds->ds_phys->ds_used_bytes ||
310711022STom.Erickson@Sun.COM 	    psa->psa_effective_value < ds->ds_reserved)
31085378Sck153898 		return (ENOSPC);
31095378Sck153898 
31105378Sck153898 	return (0);
31115378Sck153898 }
31125378Sck153898 
311311022STom.Erickson@Sun.COM extern void dsl_prop_set_sync(void *, void *, cred_t *, dmu_tx_t *);
311411022STom.Erickson@Sun.COM 
31155378Sck153898 void
31165378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
31175378Sck153898 {
31185378Sck153898 	dsl_dataset_t *ds = arg1;
311911022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
312011022STom.Erickson@Sun.COM 	uint64_t effective_value = psa->psa_effective_value;
312111022STom.Erickson@Sun.COM 
312211022STom.Erickson@Sun.COM 	dsl_prop_set_sync(ds, psa, cr, tx);
312311022STom.Erickson@Sun.COM 	DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
312411022STom.Erickson@Sun.COM 
312511022STom.Erickson@Sun.COM 	if (ds->ds_quota != effective_value) {
312611022STom.Erickson@Sun.COM 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
312711022STom.Erickson@Sun.COM 		ds->ds_quota = effective_value;
312811022STom.Erickson@Sun.COM 
312911022STom.Erickson@Sun.COM 		spa_history_internal_log(LOG_DS_REFQUOTA,
313011022STom.Erickson@Sun.COM 		    ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu ",
313111022STom.Erickson@Sun.COM 		    (longlong_t)ds->ds_quota, ds->ds_object);
313211022STom.Erickson@Sun.COM 	}
31335378Sck153898 }
31345378Sck153898 
31355378Sck153898 int
313611022STom.Erickson@Sun.COM dsl_dataset_set_quota(const char *dsname, zprop_source_t source, uint64_t quota)
31375378Sck153898 {
31385378Sck153898 	dsl_dataset_t *ds;
313911022STom.Erickson@Sun.COM 	dsl_prop_setarg_t psa;
31405378Sck153898 	int err;
31415378Sck153898 
314211022STom.Erickson@Sun.COM 	dsl_prop_setarg_init_uint64(&psa, "refquota", source, &quota);
314311022STom.Erickson@Sun.COM 
31446689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
31455378Sck153898 	if (err)
31465378Sck153898 		return (err);
31475378Sck153898 
314811022STom.Erickson@Sun.COM 	/*
314911022STom.Erickson@Sun.COM 	 * If someone removes a file, then tries to set the quota, we
315011022STom.Erickson@Sun.COM 	 * want to make sure the file freeing takes effect.
315111022STom.Erickson@Sun.COM 	 */
315211022STom.Erickson@Sun.COM 	txg_wait_open(ds->ds_dir->dd_pool, 0);
315311022STom.Erickson@Sun.COM 
315411022STom.Erickson@Sun.COM 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
315511022STom.Erickson@Sun.COM 	    dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync,
315611022STom.Erickson@Sun.COM 	    ds, &psa, 0);
315711022STom.Erickson@Sun.COM 
31586689Smaybee 	dsl_dataset_rele(ds, FTAG);
31595378Sck153898 	return (err);
31605378Sck153898 }
31615378Sck153898 
31625378Sck153898 static int
31635378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
31645378Sck153898 {
31655378Sck153898 	dsl_dataset_t *ds = arg1;
316611022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
316711022STom.Erickson@Sun.COM 	uint64_t effective_value;
31685378Sck153898 	uint64_t unique;
316911022STom.Erickson@Sun.COM 	int err;
31705378Sck153898 
31715378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
31725378Sck153898 	    SPA_VERSION_REFRESERVATION)
31735378Sck153898 		return (ENOTSUP);
31745378Sck153898 
31755378Sck153898 	if (dsl_dataset_is_snapshot(ds))
31765378Sck153898 		return (EINVAL);
31775378Sck153898 
317811022STom.Erickson@Sun.COM 	if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
317911022STom.Erickson@Sun.COM 		return (err);
318011022STom.Erickson@Sun.COM 
318111022STom.Erickson@Sun.COM 	effective_value = psa->psa_effective_value;
318211022STom.Erickson@Sun.COM 
31835378Sck153898 	/*
31845378Sck153898 	 * If we are doing the preliminary check in open context, the
31855378Sck153898 	 * space estimates may be inaccurate.
31865378Sck153898 	 */
31875378Sck153898 	if (!dmu_tx_is_syncing(tx))
31885378Sck153898 		return (0);
31895378Sck153898 
31905378Sck153898 	mutex_enter(&ds->ds_lock);
31915378Sck153898 	unique = dsl_dataset_unique(ds);
31925378Sck153898 	mutex_exit(&ds->ds_lock);
31935378Sck153898 
319411022STom.Erickson@Sun.COM 	if (MAX(unique, effective_value) > MAX(unique, ds->ds_reserved)) {
319511022STom.Erickson@Sun.COM 		uint64_t delta = MAX(unique, effective_value) -
31968525SEric.Schrock@Sun.COM 		    MAX(unique, ds->ds_reserved);
31978525SEric.Schrock@Sun.COM 
31988525SEric.Schrock@Sun.COM 		if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
31998525SEric.Schrock@Sun.COM 			return (ENOSPC);
32008525SEric.Schrock@Sun.COM 		if (ds->ds_quota > 0 &&
320111022STom.Erickson@Sun.COM 		    effective_value > ds->ds_quota)
32028525SEric.Schrock@Sun.COM 			return (ENOSPC);
32038525SEric.Schrock@Sun.COM 	}
32045378Sck153898 
32055378Sck153898 	return (0);
32065378Sck153898 }
32075378Sck153898 
32085378Sck153898 /* ARGSUSED */
32095378Sck153898 static void
32105378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr,
32115378Sck153898     dmu_tx_t *tx)
32125378Sck153898 {
32135378Sck153898 	dsl_dataset_t *ds = arg1;
321411022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
321511022STom.Erickson@Sun.COM 	uint64_t effective_value = psa->psa_effective_value;
32167595SMatthew.Ahrens@Sun.COM 	uint64_t unique;
32177595SMatthew.Ahrens@Sun.COM 	int64_t delta;
32187595SMatthew.Ahrens@Sun.COM 
321911022STom.Erickson@Sun.COM 	dsl_prop_set_sync(ds, psa, cr, tx);
322011022STom.Erickson@Sun.COM 	DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
322111022STom.Erickson@Sun.COM 
32227595SMatthew.Ahrens@Sun.COM 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
32237595SMatthew.Ahrens@Sun.COM 
32247595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_dir->dd_lock);
32257595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_lock);
32267595SMatthew.Ahrens@Sun.COM 	unique = dsl_dataset_unique(ds);
322711022STom.Erickson@Sun.COM 	delta = MAX(0, (int64_t)(effective_value - unique)) -
32287595SMatthew.Ahrens@Sun.COM 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
322911022STom.Erickson@Sun.COM 	ds->ds_reserved = effective_value;
32307595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_lock);
32317595SMatthew.Ahrens@Sun.COM 
32327595SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
32337595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_dir->dd_lock);
32347595SMatthew.Ahrens@Sun.COM 
32357595SMatthew.Ahrens@Sun.COM 	spa_history_internal_log(LOG_DS_REFRESERV,
32367595SMatthew.Ahrens@Sun.COM 	    ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu",
323711022STom.Erickson@Sun.COM 	    (longlong_t)effective_value, ds->ds_object);
32385378Sck153898 }
32395378Sck153898 
32405378Sck153898 int
324111022STom.Erickson@Sun.COM dsl_dataset_set_reservation(const char *dsname, zprop_source_t source,
324211022STom.Erickson@Sun.COM     uint64_t reservation)
32435378Sck153898 {
32445378Sck153898 	dsl_dataset_t *ds;
324511022STom.Erickson@Sun.COM 	dsl_prop_setarg_t psa;
32465378Sck153898 	int err;
32475378Sck153898 
324811022STom.Erickson@Sun.COM 	dsl_prop_setarg_init_uint64(&psa, "refreservation", source,
324911022STom.Erickson@Sun.COM 	    &reservation);
325011022STom.Erickson@Sun.COM 
32516689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
32525378Sck153898 	if (err)
32535378Sck153898 		return (err);
32545378Sck153898 
32555378Sck153898 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
32565378Sck153898 	    dsl_dataset_set_reservation_check,
325711022STom.Erickson@Sun.COM 	    dsl_dataset_set_reservation_sync, ds, &psa, 0);
325811022STom.Erickson@Sun.COM 
32596689Smaybee 	dsl_dataset_rele(ds, FTAG);
32605378Sck153898 	return (err);
32615378Sck153898 }
326210242Schris.kirby@sun.com 
326310342Schris.kirby@sun.com struct dsl_ds_holdarg {
326410342Schris.kirby@sun.com 	dsl_sync_task_group_t *dstg;
326510342Schris.kirby@sun.com 	char *htag;
326610342Schris.kirby@sun.com 	char *snapname;
326710342Schris.kirby@sun.com 	boolean_t recursive;
326810342Schris.kirby@sun.com 	boolean_t gotone;
326910342Schris.kirby@sun.com 	boolean_t temphold;
327010342Schris.kirby@sun.com 	char failed[MAXPATHLEN];
327110342Schris.kirby@sun.com };
327210342Schris.kirby@sun.com 
327310951SChris.Kirby@sun.com /*
327410951SChris.Kirby@sun.com  * The max length of a temporary tag prefix is the number of hex digits
327510951SChris.Kirby@sun.com  * required to express UINT64_MAX plus one for the hyphen.
327610951SChris.Kirby@sun.com  */
327710951SChris.Kirby@sun.com #define	MAX_TAG_PREFIX_LEN	17
327810951SChris.Kirby@sun.com 
327910242Schris.kirby@sun.com static int
328010242Schris.kirby@sun.com dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx)
328110242Schris.kirby@sun.com {
328210242Schris.kirby@sun.com 	dsl_dataset_t *ds = arg1;
328310342Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg2;
328410342Schris.kirby@sun.com 	char *htag = ha->htag;
328510242Schris.kirby@sun.com 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
328610242Schris.kirby@sun.com 	int error = 0;
328710242Schris.kirby@sun.com 
328810242Schris.kirby@sun.com 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
328910242Schris.kirby@sun.com 		return (ENOTSUP);
329010242Schris.kirby@sun.com 
329110242Schris.kirby@sun.com 	if (!dsl_dataset_is_snapshot(ds))
329210242Schris.kirby@sun.com 		return (EINVAL);
329310242Schris.kirby@sun.com 
329410242Schris.kirby@sun.com 	/* tags must be unique */
329510242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
329610242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj) {
329710242Schris.kirby@sun.com 		error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag,
329810242Schris.kirby@sun.com 		    8, 1, tx);
329910242Schris.kirby@sun.com 		if (error == 0)
330010242Schris.kirby@sun.com 			error = EEXIST;
330110242Schris.kirby@sun.com 		else if (error == ENOENT)
330210242Schris.kirby@sun.com 			error = 0;
330310242Schris.kirby@sun.com 	}
330410242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
330510242Schris.kirby@sun.com 
330610951SChris.Kirby@sun.com 	if (error == 0 && ha->temphold &&
330710951SChris.Kirby@sun.com 	    strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
330810951SChris.Kirby@sun.com 		error = E2BIG;
330910951SChris.Kirby@sun.com 
331010242Schris.kirby@sun.com 	return (error);
331110242Schris.kirby@sun.com }
331210242Schris.kirby@sun.com 
331310242Schris.kirby@sun.com static void
331410242Schris.kirby@sun.com dsl_dataset_user_hold_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
331510242Schris.kirby@sun.com {
331610242Schris.kirby@sun.com 	dsl_dataset_t *ds = arg1;
331710342Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg2;
331810342Schris.kirby@sun.com 	char *htag = ha->htag;
331910342Schris.kirby@sun.com 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
332010342Schris.kirby@sun.com 	objset_t *mos = dp->dp_meta_objset;
332110951SChris.Kirby@sun.com 	uint64_t now = gethrestime_sec();
332210242Schris.kirby@sun.com 	uint64_t zapobj;
332310242Schris.kirby@sun.com 
332410242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
332510242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj == 0) {
332610242Schris.kirby@sun.com 		/*
332710242Schris.kirby@sun.com 		 * This is the first user hold for this dataset.  Create
332810242Schris.kirby@sun.com 		 * the userrefs zap object.
332910242Schris.kirby@sun.com 		 */
333010242Schris.kirby@sun.com 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
333110242Schris.kirby@sun.com 		zapobj = ds->ds_phys->ds_userrefs_obj =
333210242Schris.kirby@sun.com 		    zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx);
333310242Schris.kirby@sun.com 	} else {
333410242Schris.kirby@sun.com 		zapobj = ds->ds_phys->ds_userrefs_obj;
333510242Schris.kirby@sun.com 	}
333610242Schris.kirby@sun.com 	ds->ds_userrefs++;
333710242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
333810242Schris.kirby@sun.com 
333910242Schris.kirby@sun.com 	VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx));
334010242Schris.kirby@sun.com 
334110342Schris.kirby@sun.com 	if (ha->temphold) {
334210342Schris.kirby@sun.com 		VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object,
334310342Schris.kirby@sun.com 		    htag, &now, tx));
334410342Schris.kirby@sun.com 	}
334510342Schris.kirby@sun.com 
334610242Schris.kirby@sun.com 	spa_history_internal_log(LOG_DS_USER_HOLD,
334710342Schris.kirby@sun.com 	    dp->dp_spa, tx, cr, "<%s> temp = %d dataset = %llu", htag,
334810342Schris.kirby@sun.com 	    (int)ha->temphold, ds->ds_object);
334910242Schris.kirby@sun.com }
335010242Schris.kirby@sun.com 
335110242Schris.kirby@sun.com static int
335211209SMatthew.Ahrens@Sun.COM dsl_dataset_user_hold_one(const char *dsname, void *arg)
335310242Schris.kirby@sun.com {
335410242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg;
335510242Schris.kirby@sun.com 	dsl_dataset_t *ds;
335610242Schris.kirby@sun.com 	int error;
335710242Schris.kirby@sun.com 	char *name;
335810242Schris.kirby@sun.com 
335910242Schris.kirby@sun.com 	/* alloc a buffer to hold dsname@snapname plus terminating NULL */
336010272SMatthew.Ahrens@Sun.COM 	name = kmem_asprintf("%s@%s", dsname, ha->snapname);
336110242Schris.kirby@sun.com 	error = dsl_dataset_hold(name, ha->dstg, &ds);
336210272SMatthew.Ahrens@Sun.COM 	strfree(name);
336310242Schris.kirby@sun.com 	if (error == 0) {
336410268Schris.kirby@sun.com 		ha->gotone = B_TRUE;
336510242Schris.kirby@sun.com 		dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check,
336610342Schris.kirby@sun.com 		    dsl_dataset_user_hold_sync, ds, ha, 0);
336710242Schris.kirby@sun.com 	} else if (error == ENOENT && ha->recursive) {
336810242Schris.kirby@sun.com 		error = 0;
336910242Schris.kirby@sun.com 	} else {
337011209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
337110242Schris.kirby@sun.com 	}
337210242Schris.kirby@sun.com 	return (error);
337310242Schris.kirby@sun.com }
337410242Schris.kirby@sun.com 
337510242Schris.kirby@sun.com int
337610242Schris.kirby@sun.com dsl_dataset_user_hold(char *dsname, char *snapname, char *htag,
337710342Schris.kirby@sun.com     boolean_t recursive, boolean_t temphold)
337810242Schris.kirby@sun.com {
337910242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha;
338010242Schris.kirby@sun.com 	dsl_sync_task_t *dst;
338110242Schris.kirby@sun.com 	spa_t *spa;
338210242Schris.kirby@sun.com 	int error;
338310242Schris.kirby@sun.com 
338410242Schris.kirby@sun.com 	ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
338510242Schris.kirby@sun.com 
338610242Schris.kirby@sun.com 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
338710242Schris.kirby@sun.com 
338810242Schris.kirby@sun.com 	error = spa_open(dsname, &spa, FTAG);
338910242Schris.kirby@sun.com 	if (error) {
339010242Schris.kirby@sun.com 		kmem_free(ha, sizeof (struct dsl_ds_holdarg));
339110242Schris.kirby@sun.com 		return (error);
339210242Schris.kirby@sun.com 	}
339310242Schris.kirby@sun.com 
339410242Schris.kirby@sun.com 	ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
339510242Schris.kirby@sun.com 	ha->htag = htag;
339610242Schris.kirby@sun.com 	ha->snapname = snapname;
339710242Schris.kirby@sun.com 	ha->recursive = recursive;
339810342Schris.kirby@sun.com 	ha->temphold = temphold;
339910242Schris.kirby@sun.com 	if (recursive) {
340010242Schris.kirby@sun.com 		error = dmu_objset_find(dsname, dsl_dataset_user_hold_one,
340110242Schris.kirby@sun.com 		    ha, DS_FIND_CHILDREN);
340210242Schris.kirby@sun.com 	} else {
340310242Schris.kirby@sun.com 		error = dsl_dataset_user_hold_one(dsname, ha);
340410242Schris.kirby@sun.com 	}
340510242Schris.kirby@sun.com 	if (error == 0)
340610242Schris.kirby@sun.com 		error = dsl_sync_task_group_wait(ha->dstg);
340710242Schris.kirby@sun.com 
340810242Schris.kirby@sun.com 	for (dst = list_head(&ha->dstg->dstg_tasks); dst;
340910242Schris.kirby@sun.com 	    dst = list_next(&ha->dstg->dstg_tasks, dst)) {
341010242Schris.kirby@sun.com 		dsl_dataset_t *ds = dst->dst_arg1;
341110242Schris.kirby@sun.com 
341210242Schris.kirby@sun.com 		if (dst->dst_err) {
341310242Schris.kirby@sun.com 			dsl_dataset_name(ds, ha->failed);
341410242Schris.kirby@sun.com 			*strchr(ha->failed, '@') = '\0';
341510242Schris.kirby@sun.com 		}
341610242Schris.kirby@sun.com 		dsl_dataset_rele(ds, ha->dstg);
341710242Schris.kirby@sun.com 	}
341810242Schris.kirby@sun.com 
341910268Schris.kirby@sun.com 	if (error == 0 && recursive && !ha->gotone)
342010268Schris.kirby@sun.com 		error = ENOENT;
342110268Schris.kirby@sun.com 
342210242Schris.kirby@sun.com 	if (error)
342311209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
342410242Schris.kirby@sun.com 
342510242Schris.kirby@sun.com 	dsl_sync_task_group_destroy(ha->dstg);
342610242Schris.kirby@sun.com 	kmem_free(ha, sizeof (struct dsl_ds_holdarg));
342710242Schris.kirby@sun.com 	spa_close(spa, FTAG);
342810242Schris.kirby@sun.com 	return (error);
342910242Schris.kirby@sun.com }
343010242Schris.kirby@sun.com 
343110242Schris.kirby@sun.com struct dsl_ds_releasearg {
343210242Schris.kirby@sun.com 	dsl_dataset_t *ds;
343310242Schris.kirby@sun.com 	const char *htag;
343410242Schris.kirby@sun.com 	boolean_t own;		/* do we own or just hold ds? */
343510242Schris.kirby@sun.com };
343610242Schris.kirby@sun.com 
343710242Schris.kirby@sun.com static int
343810242Schris.kirby@sun.com dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag,
343910242Schris.kirby@sun.com     boolean_t *might_destroy)
344010242Schris.kirby@sun.com {
344110242Schris.kirby@sun.com 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
344210242Schris.kirby@sun.com 	uint64_t zapobj;
344310242Schris.kirby@sun.com 	uint64_t tmp;
344410242Schris.kirby@sun.com 	int error;
344510242Schris.kirby@sun.com 
344610242Schris.kirby@sun.com 	*might_destroy = B_FALSE;
344710242Schris.kirby@sun.com 
344810242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
344910242Schris.kirby@sun.com 	zapobj = ds->ds_phys->ds_userrefs_obj;
345010242Schris.kirby@sun.com 	if (zapobj == 0) {
345110242Schris.kirby@sun.com 		/* The tag can't possibly exist */
345210242Schris.kirby@sun.com 		mutex_exit(&ds->ds_lock);
345310242Schris.kirby@sun.com 		return (ESRCH);
345410242Schris.kirby@sun.com 	}
345510242Schris.kirby@sun.com 
345610242Schris.kirby@sun.com 	/* Make sure the tag exists */
345710242Schris.kirby@sun.com 	error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp);
345810242Schris.kirby@sun.com 	if (error) {
345910242Schris.kirby@sun.com 		mutex_exit(&ds->ds_lock);
346010242Schris.kirby@sun.com 		if (error == ENOENT)
346110242Schris.kirby@sun.com 			error = ESRCH;
346210242Schris.kirby@sun.com 		return (error);
346310242Schris.kirby@sun.com 	}
346410242Schris.kirby@sun.com 
346510242Schris.kirby@sun.com 	if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 &&
346610242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds))
346710242Schris.kirby@sun.com 		*might_destroy = B_TRUE;
346810242Schris.kirby@sun.com 
346910242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
347010242Schris.kirby@sun.com 	return (0);
347110242Schris.kirby@sun.com }
347210242Schris.kirby@sun.com 
347310242Schris.kirby@sun.com static int
347410242Schris.kirby@sun.com dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx)
347510242Schris.kirby@sun.com {
347610242Schris.kirby@sun.com 	struct dsl_ds_releasearg *ra = arg1;
347710242Schris.kirby@sun.com 	dsl_dataset_t *ds = ra->ds;
347810242Schris.kirby@sun.com 	boolean_t might_destroy;
347910242Schris.kirby@sun.com 	int error;
348010242Schris.kirby@sun.com 
348110242Schris.kirby@sun.com 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
348210242Schris.kirby@sun.com 		return (ENOTSUP);
348310242Schris.kirby@sun.com 
348410242Schris.kirby@sun.com 	error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy);
348510242Schris.kirby@sun.com 	if (error)
348610242Schris.kirby@sun.com 		return (error);
348710242Schris.kirby@sun.com 
348810242Schris.kirby@sun.com 	if (might_destroy) {
348910242Schris.kirby@sun.com 		struct dsl_ds_destroyarg dsda = {0};
349010242Schris.kirby@sun.com 
349110242Schris.kirby@sun.com 		if (dmu_tx_is_syncing(tx)) {
349210242Schris.kirby@sun.com 			/*
349310242Schris.kirby@sun.com 			 * If we're not prepared to remove the snapshot,
349410242Schris.kirby@sun.com 			 * we can't allow the release to happen right now.
349510242Schris.kirby@sun.com 			 */
349610242Schris.kirby@sun.com 			if (!ra->own)
349710242Schris.kirby@sun.com 				return (EBUSY);
349810298SMatthew.Ahrens@Sun.COM 			if (ds->ds_objset) {
349910298SMatthew.Ahrens@Sun.COM 				dmu_objset_evict(ds->ds_objset);
350010298SMatthew.Ahrens@Sun.COM 				ds->ds_objset = NULL;
350110242Schris.kirby@sun.com 			}
350210242Schris.kirby@sun.com 		}
350310242Schris.kirby@sun.com 		dsda.ds = ds;
350410242Schris.kirby@sun.com 		dsda.releasing = B_TRUE;
350510242Schris.kirby@sun.com 		return (dsl_dataset_destroy_check(&dsda, tag, tx));
350610242Schris.kirby@sun.com 	}
350710242Schris.kirby@sun.com 
350810242Schris.kirby@sun.com 	return (0);
350910242Schris.kirby@sun.com }
351010242Schris.kirby@sun.com 
351110242Schris.kirby@sun.com static void
351210242Schris.kirby@sun.com dsl_dataset_user_release_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx)
351310242Schris.kirby@sun.com {
351410242Schris.kirby@sun.com 	struct dsl_ds_releasearg *ra = arg1;
351510242Schris.kirby@sun.com 	dsl_dataset_t *ds = ra->ds;
351610342Schris.kirby@sun.com 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
351710342Schris.kirby@sun.com 	objset_t *mos = dp->dp_meta_objset;
351810242Schris.kirby@sun.com 	uint64_t zapobj;
351910242Schris.kirby@sun.com 	uint64_t dsobj = ds->ds_object;
352010242Schris.kirby@sun.com 	uint64_t refs;
352110342Schris.kirby@sun.com 	int error;
352210242Schris.kirby@sun.com 
352310242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
352410242Schris.kirby@sun.com 	ds->ds_userrefs--;
352510242Schris.kirby@sun.com 	refs = ds->ds_userrefs;
352610242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
352710342Schris.kirby@sun.com 	error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx);
352810342Schris.kirby@sun.com 	VERIFY(error == 0 || error == ENOENT);
352910242Schris.kirby@sun.com 	zapobj = ds->ds_phys->ds_userrefs_obj;
353010242Schris.kirby@sun.com 	VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx));
353110242Schris.kirby@sun.com 	if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 &&
353210242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds)) {
353310242Schris.kirby@sun.com 		struct dsl_ds_destroyarg dsda = {0};
353410242Schris.kirby@sun.com 
353510242Schris.kirby@sun.com 		ASSERT(ra->own);
353610242Schris.kirby@sun.com 		dsda.ds = ds;
353710242Schris.kirby@sun.com 		dsda.releasing = B_TRUE;
353810242Schris.kirby@sun.com 		/* We already did the destroy_check */
353910242Schris.kirby@sun.com 		dsl_dataset_destroy_sync(&dsda, tag, cr, tx);
354010242Schris.kirby@sun.com 	}
354110242Schris.kirby@sun.com 
354210242Schris.kirby@sun.com 	spa_history_internal_log(LOG_DS_USER_RELEASE,
354310342Schris.kirby@sun.com 	    dp->dp_spa, tx, cr, "<%s> %lld dataset = %llu",
354410242Schris.kirby@sun.com 	    ra->htag, (longlong_t)refs, dsobj);
354510242Schris.kirby@sun.com }
354610242Schris.kirby@sun.com 
354710242Schris.kirby@sun.com static int
354811209SMatthew.Ahrens@Sun.COM dsl_dataset_user_release_one(const char *dsname, void *arg)
354910242Schris.kirby@sun.com {
355010242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg;
355110242Schris.kirby@sun.com 	struct dsl_ds_releasearg *ra;
355210242Schris.kirby@sun.com 	dsl_dataset_t *ds;
355310242Schris.kirby@sun.com 	int error;
355410242Schris.kirby@sun.com 	void *dtag = ha->dstg;
355510242Schris.kirby@sun.com 	char *name;
355610242Schris.kirby@sun.com 	boolean_t own = B_FALSE;
355710242Schris.kirby@sun.com 	boolean_t might_destroy;
355810242Schris.kirby@sun.com 
355910242Schris.kirby@sun.com 	/* alloc a buffer to hold dsname@snapname, plus the terminating NULL */
356010272SMatthew.Ahrens@Sun.COM 	name = kmem_asprintf("%s@%s", dsname, ha->snapname);
356110242Schris.kirby@sun.com 	error = dsl_dataset_hold(name, dtag, &ds);
356210272SMatthew.Ahrens@Sun.COM 	strfree(name);
356310242Schris.kirby@sun.com 	if (error == ENOENT && ha->recursive)
356410242Schris.kirby@sun.com 		return (0);
356511209SMatthew.Ahrens@Sun.COM 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
356610242Schris.kirby@sun.com 	if (error)
356710242Schris.kirby@sun.com 		return (error);
356810242Schris.kirby@sun.com 
356910268Schris.kirby@sun.com 	ha->gotone = B_TRUE;
357010268Schris.kirby@sun.com 
357110242Schris.kirby@sun.com 	ASSERT(dsl_dataset_is_snapshot(ds));
357210242Schris.kirby@sun.com 
357310242Schris.kirby@sun.com 	error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy);
357410242Schris.kirby@sun.com 	if (error) {
357510242Schris.kirby@sun.com 		dsl_dataset_rele(ds, dtag);
357610242Schris.kirby@sun.com 		return (error);
357710242Schris.kirby@sun.com 	}
357810242Schris.kirby@sun.com 
357910242Schris.kirby@sun.com 	if (might_destroy) {
358010242Schris.kirby@sun.com #ifdef _KERNEL
358112115SChris.Kirby@oracle.com 		name = kmem_asprintf("%s@%s", dsname, ha->snapname);
358210242Schris.kirby@sun.com 		error = zfs_unmount_snap(name, NULL);
358312115SChris.Kirby@oracle.com 		strfree(name);
358410242Schris.kirby@sun.com 		if (error) {
358510242Schris.kirby@sun.com 			dsl_dataset_rele(ds, dtag);
358610242Schris.kirby@sun.com 			return (error);
358710242Schris.kirby@sun.com 		}
358810242Schris.kirby@sun.com #endif
358910298SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) {
359010242Schris.kirby@sun.com 			dsl_dataset_rele(ds, dtag);
359110242Schris.kirby@sun.com 			return (EBUSY);
359210242Schris.kirby@sun.com 		} else {
359310242Schris.kirby@sun.com 			own = B_TRUE;
359410242Schris.kirby@sun.com 			dsl_dataset_make_exclusive(ds, dtag);
359510242Schris.kirby@sun.com 		}
359610242Schris.kirby@sun.com 	}
359710242Schris.kirby@sun.com 
359810242Schris.kirby@sun.com 	ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP);
359910242Schris.kirby@sun.com 	ra->ds = ds;
360010242Schris.kirby@sun.com 	ra->htag = ha->htag;
360110242Schris.kirby@sun.com 	ra->own = own;
360210242Schris.kirby@sun.com 	dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check,
360310242Schris.kirby@sun.com 	    dsl_dataset_user_release_sync, ra, dtag, 0);
360410242Schris.kirby@sun.com 
360510242Schris.kirby@sun.com 	return (0);
360610242Schris.kirby@sun.com }
360710242Schris.kirby@sun.com 
360810242Schris.kirby@sun.com int
360910242Schris.kirby@sun.com dsl_dataset_user_release(char *dsname, char *snapname, char *htag,
361010242Schris.kirby@sun.com     boolean_t recursive)
361110242Schris.kirby@sun.com {
361210242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha;
361310242Schris.kirby@sun.com 	dsl_sync_task_t *dst;
361410242Schris.kirby@sun.com 	spa_t *spa;
361510242Schris.kirby@sun.com 	int error;
361610242Schris.kirby@sun.com 
361711546SChris.Kirby@sun.com top:
361810242Schris.kirby@sun.com 	ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
361910242Schris.kirby@sun.com 
362010242Schris.kirby@sun.com 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
362110242Schris.kirby@sun.com 
362210242Schris.kirby@sun.com 	error = spa_open(dsname, &spa, FTAG);
362310242Schris.kirby@sun.com 	if (error) {
362410242Schris.kirby@sun.com 		kmem_free(ha, sizeof (struct dsl_ds_holdarg));
362510242Schris.kirby@sun.com 		return (error);
362610242Schris.kirby@sun.com 	}
362710242Schris.kirby@sun.com 
362810242Schris.kirby@sun.com 	ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
362910242Schris.kirby@sun.com 	ha->htag = htag;
363010242Schris.kirby@sun.com 	ha->snapname = snapname;
363110242Schris.kirby@sun.com 	ha->recursive = recursive;
363210242Schris.kirby@sun.com 	if (recursive) {
363310242Schris.kirby@sun.com 		error = dmu_objset_find(dsname, dsl_dataset_user_release_one,
363410242Schris.kirby@sun.com 		    ha, DS_FIND_CHILDREN);
363510242Schris.kirby@sun.com 	} else {
363610242Schris.kirby@sun.com 		error = dsl_dataset_user_release_one(dsname, ha);
363710242Schris.kirby@sun.com 	}
363810242Schris.kirby@sun.com 	if (error == 0)
363910242Schris.kirby@sun.com 		error = dsl_sync_task_group_wait(ha->dstg);
364010242Schris.kirby@sun.com 
364110242Schris.kirby@sun.com 	for (dst = list_head(&ha->dstg->dstg_tasks); dst;
364210242Schris.kirby@sun.com 	    dst = list_next(&ha->dstg->dstg_tasks, dst)) {
364310242Schris.kirby@sun.com 		struct dsl_ds_releasearg *ra = dst->dst_arg1;
364410242Schris.kirby@sun.com 		dsl_dataset_t *ds = ra->ds;
364510242Schris.kirby@sun.com 
364610242Schris.kirby@sun.com 		if (dst->dst_err)
364710242Schris.kirby@sun.com 			dsl_dataset_name(ds, ha->failed);
364810242Schris.kirby@sun.com 
364910242Schris.kirby@sun.com 		if (ra->own)
365010242Schris.kirby@sun.com 			dsl_dataset_disown(ds, ha->dstg);
365110242Schris.kirby@sun.com 		else
365210242Schris.kirby@sun.com 			dsl_dataset_rele(ds, ha->dstg);
365310242Schris.kirby@sun.com 
365410242Schris.kirby@sun.com 		kmem_free(ra, sizeof (struct dsl_ds_releasearg));
365510242Schris.kirby@sun.com 	}
365610242Schris.kirby@sun.com 
365710268Schris.kirby@sun.com 	if (error == 0 && recursive && !ha->gotone)
365810268Schris.kirby@sun.com 		error = ENOENT;
365910268Schris.kirby@sun.com 
366011546SChris.Kirby@sun.com 	if (error && error != EBUSY)
366111209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
366210242Schris.kirby@sun.com 
366310242Schris.kirby@sun.com 	dsl_sync_task_group_destroy(ha->dstg);
366410242Schris.kirby@sun.com 	kmem_free(ha, sizeof (struct dsl_ds_holdarg));
366510242Schris.kirby@sun.com 	spa_close(spa, FTAG);
366611546SChris.Kirby@sun.com 
366711546SChris.Kirby@sun.com 	/*
366811546SChris.Kirby@sun.com 	 * We can get EBUSY if we were racing with deferred destroy and
366911546SChris.Kirby@sun.com 	 * dsl_dataset_user_release_check() hadn't done the necessary
367011546SChris.Kirby@sun.com 	 * open context setup.  We can also get EBUSY if we're racing
367111546SChris.Kirby@sun.com 	 * with destroy and that thread is the ds_owner.  Either way
367211546SChris.Kirby@sun.com 	 * the busy condition should be transient, and we should retry
367311546SChris.Kirby@sun.com 	 * the release operation.
367411546SChris.Kirby@sun.com 	 */
367511546SChris.Kirby@sun.com 	if (error == EBUSY)
367611546SChris.Kirby@sun.com 		goto top;
367711546SChris.Kirby@sun.com 
367810242Schris.kirby@sun.com 	return (error);
367910242Schris.kirby@sun.com }
368010242Schris.kirby@sun.com 
368110342Schris.kirby@sun.com /*
368210342Schris.kirby@sun.com  * Called at spa_load time to release a stale temporary user hold.
368310342Schris.kirby@sun.com  */
368410342Schris.kirby@sun.com int
368510342Schris.kirby@sun.com dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag)
368610342Schris.kirby@sun.com {
368710342Schris.kirby@sun.com 	dsl_dataset_t *ds;
368810342Schris.kirby@sun.com 	char *snap;
368910342Schris.kirby@sun.com 	char *name;
369010342Schris.kirby@sun.com 	int namelen;
369110342Schris.kirby@sun.com 	int error;
369210342Schris.kirby@sun.com 
369310342Schris.kirby@sun.com 	rw_enter(&dp->dp_config_rwlock, RW_READER);
369410342Schris.kirby@sun.com 	error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
369510342Schris.kirby@sun.com 	rw_exit(&dp->dp_config_rwlock);
369610342Schris.kirby@sun.com 	if (error)
369710342Schris.kirby@sun.com 		return (error);
369810342Schris.kirby@sun.com 	namelen = dsl_dataset_namelen(ds)+1;
369910342Schris.kirby@sun.com 	name = kmem_alloc(namelen, KM_SLEEP);
370010342Schris.kirby@sun.com 	dsl_dataset_name(ds, name);
370110342Schris.kirby@sun.com 	dsl_dataset_rele(ds, FTAG);
370210342Schris.kirby@sun.com 
370310342Schris.kirby@sun.com 	snap = strchr(name, '@');
370410342Schris.kirby@sun.com 	*snap = '\0';
370510342Schris.kirby@sun.com 	++snap;
370610342Schris.kirby@sun.com 	return (dsl_dataset_user_release(name, snap, htag, B_FALSE));
370710342Schris.kirby@sun.com }
370810342Schris.kirby@sun.com 
370910242Schris.kirby@sun.com int
371010242Schris.kirby@sun.com dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp)
371110242Schris.kirby@sun.com {
371210242Schris.kirby@sun.com 	dsl_dataset_t *ds;
371310242Schris.kirby@sun.com 	int err;
371410242Schris.kirby@sun.com 
371510242Schris.kirby@sun.com 	err = dsl_dataset_hold(dsname, FTAG, &ds);
371610242Schris.kirby@sun.com 	if (err)
371710242Schris.kirby@sun.com 		return (err);
371810242Schris.kirby@sun.com 
371910242Schris.kirby@sun.com 	VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP));
372010242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj != 0) {
372110242Schris.kirby@sun.com 		zap_attribute_t *za;
372210242Schris.kirby@sun.com 		zap_cursor_t zc;
372310242Schris.kirby@sun.com 
372410242Schris.kirby@sun.com 		za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
372510242Schris.kirby@sun.com 		for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset,
372610242Schris.kirby@sun.com 		    ds->ds_phys->ds_userrefs_obj);
372710242Schris.kirby@sun.com 		    zap_cursor_retrieve(&zc, za) == 0;
372810242Schris.kirby@sun.com 		    zap_cursor_advance(&zc)) {
372910242Schris.kirby@sun.com 			VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name,
373010242Schris.kirby@sun.com 			    za->za_first_integer));
373110242Schris.kirby@sun.com 		}
373210242Schris.kirby@sun.com 		zap_cursor_fini(&zc);
373310242Schris.kirby@sun.com 		kmem_free(za, sizeof (zap_attribute_t));
373410242Schris.kirby@sun.com 	}
373510242Schris.kirby@sun.com 	dsl_dataset_rele(ds, FTAG);
373610242Schris.kirby@sun.com 	return (0);
373710242Schris.kirby@sun.com }
373810298SMatthew.Ahrens@Sun.COM 
373910298SMatthew.Ahrens@Sun.COM /*
374010298SMatthew.Ahrens@Sun.COM  * Note, this fuction is used as the callback for dmu_objset_find().  We
374110298SMatthew.Ahrens@Sun.COM  * always return 0 so that we will continue to find and process
374210298SMatthew.Ahrens@Sun.COM  * inconsistent datasets, even if we encounter an error trying to
374310298SMatthew.Ahrens@Sun.COM  * process one of them.
374410298SMatthew.Ahrens@Sun.COM  */
374510298SMatthew.Ahrens@Sun.COM /* ARGSUSED */
374610298SMatthew.Ahrens@Sun.COM int
374711209SMatthew.Ahrens@Sun.COM dsl_destroy_inconsistent(const char *dsname, void *arg)
374810298SMatthew.Ahrens@Sun.COM {
374910298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
375010298SMatthew.Ahrens@Sun.COM 
375110298SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) {
375210298SMatthew.Ahrens@Sun.COM 		if (DS_IS_INCONSISTENT(ds))
375310298SMatthew.Ahrens@Sun.COM 			(void) dsl_dataset_destroy(ds, FTAG, B_FALSE);
375410298SMatthew.Ahrens@Sun.COM 		else
375510298SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(ds, FTAG);
375610298SMatthew.Ahrens@Sun.COM 	}
375710298SMatthew.Ahrens@Sun.COM 	return (0);
375810298SMatthew.Ahrens@Sun.COM }
3759