xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision 12296:7cf402a7f374)
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>
41*12296SLin.Ling@Sun.COM #include <sys/dsl_scan.h>
42789Sahrens 
436689Smaybee static char *dsl_reaper = "the grim reaper";
446689Smaybee 
452199Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check;
462199Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync;
475378Sck153898 static dsl_syncfunc_t dsl_dataset_set_reservation_sync;
481731Sbonwick 
493444Sek110237 #define	DS_REF_MAX	(1ULL << 62)
50789Sahrens 
51789Sahrens #define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
52789Sahrens 
536689Smaybee #define	DSL_DATASET_IS_DESTROYED(ds)	((ds)->ds_owner == dsl_reaper)
546689Smaybee 
55789Sahrens 
565378Sck153898 /*
575378Sck153898  * Figure out how much of this delta should be propogated to the dsl_dir
585378Sck153898  * layer.  If there's a refreservation, that space has already been
595378Sck153898  * partially accounted for in our ancestors.
605378Sck153898  */
615378Sck153898 static int64_t
625378Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta)
635378Sck153898 {
645378Sck153898 	uint64_t old_bytes, new_bytes;
655378Sck153898 
665378Sck153898 	if (ds->ds_reserved == 0)
675378Sck153898 		return (delta);
685378Sck153898 
695378Sck153898 	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
705378Sck153898 	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
715378Sck153898 
725378Sck153898 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
735378Sck153898 	return (new_bytes - old_bytes);
745378Sck153898 }
75789Sahrens 
76789Sahrens void
7710922SJeff.Bonwick@Sun.COM dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
78789Sahrens {
7910922SJeff.Bonwick@Sun.COM 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
80789Sahrens 	int compressed = BP_GET_PSIZE(bp);
81789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
825378Sck153898 	int64_t delta;
83789Sahrens 
84*12296SLin.Ling@Sun.COM 	dprintf_bp(bp, "ds=%p", ds);
85789Sahrens 
86789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
87789Sahrens 	/* It could have been compressed away to nothing */
88789Sahrens 	if (BP_IS_HOLE(bp))
89789Sahrens 		return;
90789Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
91789Sahrens 	ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES);
92789Sahrens 	if (ds == NULL) {
93789Sahrens 		/*
94789Sahrens 		 * Account for the meta-objset space in its placeholder
95789Sahrens 		 * dsl_dir.
96789Sahrens 		 */
97789Sahrens 		ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */
987390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
99789Sahrens 		    used, compressed, uncompressed, tx);
100789Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
101789Sahrens 		return;
102789Sahrens 	}
103789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
104*12296SLin.Ling@Sun.COM 
1057595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_dir->dd_lock);
106789Sahrens 	mutex_enter(&ds->ds_lock);
1075378Sck153898 	delta = parent_delta(ds, used);
108789Sahrens 	ds->ds_phys->ds_used_bytes += used;
109789Sahrens 	ds->ds_phys->ds_compressed_bytes += compressed;
110789Sahrens 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
111789Sahrens 	ds->ds_phys->ds_unique_bytes += used;
112789Sahrens 	mutex_exit(&ds->ds_lock);
1137390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
1147390SMatthew.Ahrens@Sun.COM 	    compressed, uncompressed, tx);
1157390SMatthew.Ahrens@Sun.COM 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
1167390SMatthew.Ahrens@Sun.COM 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
1177595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_dir->dd_lock);
118789Sahrens }
119789Sahrens 
1206992Smaybee int
12110922SJeff.Bonwick@Sun.COM dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
12210922SJeff.Bonwick@Sun.COM     boolean_t async)
123789Sahrens {
12410922SJeff.Bonwick@Sun.COM 	if (BP_IS_HOLE(bp))
12510922SJeff.Bonwick@Sun.COM 		return (0);
12610922SJeff.Bonwick@Sun.COM 
12710922SJeff.Bonwick@Sun.COM 	ASSERT(dmu_tx_is_syncing(tx));
12810922SJeff.Bonwick@Sun.COM 	ASSERT(bp->blk_birth <= tx->tx_txg);
12910922SJeff.Bonwick@Sun.COM 
13010922SJeff.Bonwick@Sun.COM 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
131789Sahrens 	int compressed = BP_GET_PSIZE(bp);
132789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
133789Sahrens 
134789Sahrens 	ASSERT(used > 0);
135789Sahrens 	if (ds == NULL) {
136789Sahrens 		/*
137789Sahrens 		 * Account for the meta-objset space in its placeholder
138789Sahrens 		 * dataset.
139789Sahrens 		 */
14010922SJeff.Bonwick@Sun.COM 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
141789Sahrens 
1427390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
143789Sahrens 		    -used, -compressed, -uncompressed, tx);
144789Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
1456992Smaybee 		return (used);
146789Sahrens 	}
147789Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
148789Sahrens 
1497390SMatthew.Ahrens@Sun.COM 	ASSERT(!dsl_dataset_is_snapshot(ds));
150789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
151789Sahrens 
152789Sahrens 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
1535378Sck153898 		int64_t delta;
1543547Smaybee 
155*12296SLin.Ling@Sun.COM 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
15610922SJeff.Bonwick@Sun.COM 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
157789Sahrens 
1587595SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_dir->dd_lock);
159789Sahrens 		mutex_enter(&ds->ds_lock);
1605378Sck153898 		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
1615378Sck153898 		    !DS_UNIQUE_IS_ACCURATE(ds));
1625378Sck153898 		delta = parent_delta(ds, -used);
163789Sahrens 		ds->ds_phys->ds_unique_bytes -= used;
164789Sahrens 		mutex_exit(&ds->ds_lock);
1657390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
1665378Sck153898 		    delta, -compressed, -uncompressed, tx);
1677390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
1687390SMatthew.Ahrens@Sun.COM 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
1697595SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_dir->dd_lock);
170789Sahrens 	} else {
171789Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
17210922SJeff.Bonwick@Sun.COM 		if (async) {
17310922SJeff.Bonwick@Sun.COM 			/*
17410922SJeff.Bonwick@Sun.COM 			 * We are here as part of zio's write done callback,
17510922SJeff.Bonwick@Sun.COM 			 * which means we're a zio interrupt thread.  We can't
17610922SJeff.Bonwick@Sun.COM 			 * call bplist_enqueue() now because it may block
17710922SJeff.Bonwick@Sun.COM 			 * waiting for I/O.  Instead, put bp on the deferred
17810922SJeff.Bonwick@Sun.COM 			 * queue and let dsl_pool_sync() finish the job.
17910922SJeff.Bonwick@Sun.COM 			 */
18010922SJeff.Bonwick@Sun.COM 			bplist_enqueue_deferred(&ds->ds_deadlist, bp);
18110922SJeff.Bonwick@Sun.COM 		} else {
18210922SJeff.Bonwick@Sun.COM 			VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx));
18310922SJeff.Bonwick@Sun.COM 		}
1845712Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
1855712Sahrens 		    ds->ds_phys->ds_prev_snap_obj);
1865712Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
187789Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
1885712Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
1895712Sahrens 		    ds->ds_object && bp->blk_birth >
1905712Sahrens 		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
1915712Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1925712Sahrens 			mutex_enter(&ds->ds_prev->ds_lock);
1935712Sahrens 			ds->ds_prev->ds_phys->ds_unique_bytes += used;
1945712Sahrens 			mutex_exit(&ds->ds_prev->ds_lock);
195789Sahrens 		}
196*12296SLin.Ling@Sun.COM 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
1977390SMatthew.Ahrens@Sun.COM 			dsl_dir_transfer_space(ds->ds_dir, used,
1987390SMatthew.Ahrens@Sun.COM 			    DD_USED_HEAD, DD_USED_SNAP, tx);
1997390SMatthew.Ahrens@Sun.COM 		}
200789Sahrens 	}
201789Sahrens 	mutex_enter(&ds->ds_lock);
202789Sahrens 	ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used);
203789Sahrens 	ds->ds_phys->ds_used_bytes -= used;
204789Sahrens 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
205789Sahrens 	ds->ds_phys->ds_compressed_bytes -= compressed;
206789Sahrens 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
207789Sahrens 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
208789Sahrens 	mutex_exit(&ds->ds_lock);
2096992Smaybee 
2106992Smaybee 	return (used);
211789Sahrens }
212789Sahrens 
2131544Seschrock uint64_t
2141544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
215789Sahrens {
2162885Sahrens 	uint64_t trysnap = 0;
2172885Sahrens 
218789Sahrens 	if (ds == NULL)
2191544Seschrock 		return (0);
220789Sahrens 	/*
221789Sahrens 	 * The snapshot creation could fail, but that would cause an
222789Sahrens 	 * incorrect FALSE return, which would only result in an
223789Sahrens 	 * overestimation of the amount of space that an operation would
224789Sahrens 	 * consume, which is OK.
225789Sahrens 	 *
226789Sahrens 	 * There's also a small window where we could miss a pending
227789Sahrens 	 * snapshot, because we could set the sync task in the quiescing
228789Sahrens 	 * phase.  So this should only be used as a guess.
229789Sahrens 	 */
2302885Sahrens 	if (ds->ds_trysnap_txg >
2312885Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
2322885Sahrens 		trysnap = ds->ds_trysnap_txg;
2332885Sahrens 	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
2341544Seschrock }
2351544Seschrock 
2369653SSanjeev.Bagewadi@Sun.COM boolean_t
2371544Seschrock dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth)
2381544Seschrock {
2391544Seschrock 	return (blk_birth > dsl_dataset_prev_snap_txg(ds));
240789Sahrens }
241789Sahrens 
242789Sahrens /* ARGSUSED */
243789Sahrens static void
244789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv)
245789Sahrens {
246789Sahrens 	dsl_dataset_t *ds = dsv;
247789Sahrens 
2486689Smaybee 	ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds));
249789Sahrens 
2504787Sahrens 	unique_remove(ds->ds_fsid_guid);
251789Sahrens 
25210298SMatthew.Ahrens@Sun.COM 	if (ds->ds_objset != NULL)
25310298SMatthew.Ahrens@Sun.COM 		dmu_objset_evict(ds->ds_objset);
254789Sahrens 
255789Sahrens 	if (ds->ds_prev) {
2566689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
257789Sahrens 		ds->ds_prev = NULL;
258789Sahrens 	}
259789Sahrens 
260789Sahrens 	bplist_close(&ds->ds_deadlist);
2616689Smaybee 	if (ds->ds_dir)
2626689Smaybee 		dsl_dir_close(ds->ds_dir, ds);
263789Sahrens 
2644787Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
265789Sahrens 
2662856Snd150628 	mutex_destroy(&ds->ds_lock);
26710204SMatthew.Ahrens@Sun.COM 	mutex_destroy(&ds->ds_recvlock);
2684787Sahrens 	mutex_destroy(&ds->ds_opening_lock);
2696689Smaybee 	rw_destroy(&ds->ds_rwlock);
2706689Smaybee 	cv_destroy(&ds->ds_exclusive_cv);
27110922SJeff.Bonwick@Sun.COM 	bplist_fini(&ds->ds_deadlist);
2722856Snd150628 
273789Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
274789Sahrens }
275789Sahrens 
2761544Seschrock static int
277789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
278789Sahrens {
279789Sahrens 	dsl_dataset_phys_t *headphys;
280789Sahrens 	int err;
281789Sahrens 	dmu_buf_t *headdbuf;
282789Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
283789Sahrens 	objset_t *mos = dp->dp_meta_objset;
284789Sahrens 
285789Sahrens 	if (ds->ds_snapname[0])
2861544Seschrock 		return (0);
287789Sahrens 	if (ds->ds_phys->ds_next_snap_obj == 0)
2881544Seschrock 		return (0);
289789Sahrens 
2901544Seschrock 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
2911544Seschrock 	    FTAG, &headdbuf);
2921544Seschrock 	if (err)
2931544Seschrock 		return (err);
294789Sahrens 	headphys = headdbuf->db_data;
295789Sahrens 	err = zap_value_search(dp->dp_meta_objset,
2964577Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
2971544Seschrock 	dmu_buf_rele(headdbuf, FTAG);
2981544Seschrock 	return (err);
299789Sahrens }
300789Sahrens 
3016492Stimh static int
3026689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
3036492Stimh {
3046689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3056689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
3066492Stimh 	matchtype_t mt;
3076492Stimh 	int err;
3086492Stimh 
3096689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
3106492Stimh 		mt = MT_FIRST;
3116492Stimh 	else
3126492Stimh 		mt = MT_EXACT;
3136492Stimh 
3146689Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
3156492Stimh 	    value, mt, NULL, 0, NULL);
3166492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
3176689Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
3186492Stimh 	return (err);
3196492Stimh }
3206492Stimh 
3216492Stimh static int
3226689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx)
3236492Stimh {
3246689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3256689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
3266492Stimh 	matchtype_t mt;
3276492Stimh 	int err;
3286492Stimh 
32910373Schris.kirby@sun.com 	dsl_dir_snap_cmtime_update(ds->ds_dir);
33010373Schris.kirby@sun.com 
3316689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
3326492Stimh 		mt = MT_FIRST;
3336492Stimh 	else
3346492Stimh 		mt = MT_EXACT;
3356492Stimh 
3366689Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
3376492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
3386689Smaybee 		err = zap_remove(mos, snapobj, name, tx);
3396492Stimh 	return (err);
3406492Stimh }
3416492Stimh 
3426689Smaybee static int
3436689Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag,
3446689Smaybee     dsl_dataset_t **dsp)
345789Sahrens {
346789Sahrens 	objset_t *mos = dp->dp_meta_objset;
347789Sahrens 	dmu_buf_t *dbuf;
348789Sahrens 	dsl_dataset_t *ds;
3491544Seschrock 	int err;
350789Sahrens 
351789Sahrens 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
352789Sahrens 	    dsl_pool_sync_context(dp));
353789Sahrens 
3541544Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
3551544Seschrock 	if (err)
3561544Seschrock 		return (err);
357789Sahrens 	ds = dmu_buf_get_user(dbuf);
358789Sahrens 	if (ds == NULL) {
359789Sahrens 		dsl_dataset_t *winner;
360789Sahrens 
361789Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
362789Sahrens 		ds->ds_dbuf = dbuf;
363789Sahrens 		ds->ds_object = dsobj;
364789Sahrens 		ds->ds_phys = dbuf->db_data;
365789Sahrens 
3662856Snd150628 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
36710204SMatthew.Ahrens@Sun.COM 		mutex_init(&ds->ds_recvlock, NULL, MUTEX_DEFAULT, NULL);
3684787Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
3696689Smaybee 		rw_init(&ds->ds_rwlock, 0, 0, 0);
3706689Smaybee 		cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL);
37110922SJeff.Bonwick@Sun.COM 		bplist_init(&ds->ds_deadlist);
3722856Snd150628 
3731544Seschrock 		err = bplist_open(&ds->ds_deadlist,
374789Sahrens 		    mos, ds->ds_phys->ds_deadlist_obj);
3751544Seschrock 		if (err == 0) {
3761544Seschrock 			err = dsl_dir_open_obj(dp,
3771544Seschrock 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
3781544Seschrock 		}
3791544Seschrock 		if (err) {
3801544Seschrock 			/*
3811544Seschrock 			 * we don't really need to close the blist if we
3821544Seschrock 			 * just opened it.
3831544Seschrock 			 */
3842856Snd150628 			mutex_destroy(&ds->ds_lock);
38510204SMatthew.Ahrens@Sun.COM 			mutex_destroy(&ds->ds_recvlock);
3864787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
3876689Smaybee 			rw_destroy(&ds->ds_rwlock);
3886689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
38910922SJeff.Bonwick@Sun.COM 			bplist_fini(&ds->ds_deadlist);
3901544Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
3911544Seschrock 			dmu_buf_rele(dbuf, tag);
3921544Seschrock 			return (err);
3931544Seschrock 		}
394789Sahrens 
3957390SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(ds)) {
396789Sahrens 			ds->ds_snapname[0] = '\0';
397789Sahrens 			if (ds->ds_phys->ds_prev_snap_obj) {
3986689Smaybee 				err = dsl_dataset_get_ref(dp,
3996689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
4006689Smaybee 				    ds, &ds->ds_prev);
401789Sahrens 			}
40210242Schris.kirby@sun.com 		} else {
40310242Schris.kirby@sun.com 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
40410242Schris.kirby@sun.com 				err = dsl_dataset_get_snapname(ds);
40510242Schris.kirby@sun.com 			if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
40610242Schris.kirby@sun.com 				err = zap_count(
40710242Schris.kirby@sun.com 				    ds->ds_dir->dd_pool->dp_meta_objset,
40810242Schris.kirby@sun.com 				    ds->ds_phys->ds_userrefs_obj,
40910242Schris.kirby@sun.com 				    &ds->ds_userrefs);
41010242Schris.kirby@sun.com 			}
411789Sahrens 		}
412789Sahrens 
4137390SMatthew.Ahrens@Sun.COM 		if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
4145569Sck153898 			/*
4155569Sck153898 			 * In sync context, we're called with either no lock
4165569Sck153898 			 * or with the write lock.  If we're not syncing,
4175569Sck153898 			 * we're always called with the read lock held.
4185569Sck153898 			 */
4195475Sck153898 			boolean_t need_lock =
4205569Sck153898 			    !RW_WRITE_HELD(&dp->dp_config_rwlock) &&
4215569Sck153898 			    dsl_pool_sync_context(dp);
4225475Sck153898 
4235475Sck153898 			if (need_lock)
4245475Sck153898 				rw_enter(&dp->dp_config_rwlock, RW_READER);
4255475Sck153898 
4267265Sahrens 			err = dsl_prop_get_ds(ds,
4275475Sck153898 			    "refreservation", sizeof (uint64_t), 1,
4285475Sck153898 			    &ds->ds_reserved, NULL);
4295475Sck153898 			if (err == 0) {
4307265Sahrens 				err = dsl_prop_get_ds(ds,
4315475Sck153898 				    "refquota", sizeof (uint64_t), 1,
4325475Sck153898 				    &ds->ds_quota, NULL);
4335475Sck153898 			}
4345475Sck153898 
4355475Sck153898 			if (need_lock)
4365475Sck153898 				rw_exit(&dp->dp_config_rwlock);
4375475Sck153898 		} else {
4385475Sck153898 			ds->ds_reserved = ds->ds_quota = 0;
4395475Sck153898 		}
4405475Sck153898 
4411544Seschrock 		if (err == 0) {
4421544Seschrock 			winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys,
4431544Seschrock 			    dsl_dataset_evict);
4441544Seschrock 		}
4451544Seschrock 		if (err || winner) {
446789Sahrens 			bplist_close(&ds->ds_deadlist);
4476689Smaybee 			if (ds->ds_prev)
4486689Smaybee 				dsl_dataset_drop_ref(ds->ds_prev, ds);
449789Sahrens 			dsl_dir_close(ds->ds_dir, ds);
4502856Snd150628 			mutex_destroy(&ds->ds_lock);
45110204SMatthew.Ahrens@Sun.COM 			mutex_destroy(&ds->ds_recvlock);
4524787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
4536689Smaybee 			rw_destroy(&ds->ds_rwlock);
4546689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
45510922SJeff.Bonwick@Sun.COM 			bplist_fini(&ds->ds_deadlist);
456789Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
4571544Seschrock 			if (err) {
4581544Seschrock 				dmu_buf_rele(dbuf, tag);
4591544Seschrock 				return (err);
4601544Seschrock 			}
461789Sahrens 			ds = winner;
462789Sahrens 		} else {
4634787Sahrens 			ds->ds_fsid_guid =
464789Sahrens 			    unique_insert(ds->ds_phys->ds_fsid_guid);
465789Sahrens 		}
466789Sahrens 	}
467789Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
468789Sahrens 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
4697046Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
4707061Sahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
4717077Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
472789Sahrens 	mutex_enter(&ds->ds_lock);
4736689Smaybee 	if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) {
474789Sahrens 		mutex_exit(&ds->ds_lock);
4756689Smaybee 		dmu_buf_rele(ds->ds_dbuf, tag);
4766689Smaybee 		return (ENOENT);
4776689Smaybee 	}
4786689Smaybee 	mutex_exit(&ds->ds_lock);
4796689Smaybee 	*dsp = ds;
4806689Smaybee 	return (0);
4816689Smaybee }
4826689Smaybee 
4836689Smaybee static int
4846689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag)
4856689Smaybee {
4866689Smaybee 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
4876689Smaybee 
4886689Smaybee 	/*
4896689Smaybee 	 * In syncing context we don't want the rwlock lock: there
4906689Smaybee 	 * may be an existing writer waiting for sync phase to
4916689Smaybee 	 * finish.  We don't need to worry about such writers, since
4926689Smaybee 	 * sync phase is single-threaded, so the writer can't be
4936689Smaybee 	 * doing anything while we are active.
4946689Smaybee 	 */
4956689Smaybee 	if (dsl_pool_sync_context(dp)) {
4966689Smaybee 		ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
4976689Smaybee 		return (0);
498789Sahrens 	}
4996689Smaybee 
5006689Smaybee 	/*
5016689Smaybee 	 * Normal users will hold the ds_rwlock as a READER until they
5026689Smaybee 	 * are finished (i.e., call dsl_dataset_rele()).  "Owners" will
5036689Smaybee 	 * drop their READER lock after they set the ds_owner field.
5046689Smaybee 	 *
5056689Smaybee 	 * If the dataset is being destroyed, the destroy thread will
5066689Smaybee 	 * obtain a WRITER lock for exclusive access after it's done its
5076689Smaybee 	 * open-context work and then change the ds_owner to
5086689Smaybee 	 * dsl_reaper once destruction is assured.  So threads
5096689Smaybee 	 * may block here temporarily, until the "destructability" of
5106689Smaybee 	 * the dataset is determined.
5116689Smaybee 	 */
5126689Smaybee 	ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock));
5136689Smaybee 	mutex_enter(&ds->ds_lock);
5146689Smaybee 	while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) {
5156689Smaybee 		rw_exit(&dp->dp_config_rwlock);
5166689Smaybee 		cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock);
5176689Smaybee 		if (DSL_DATASET_IS_DESTROYED(ds)) {
5186689Smaybee 			mutex_exit(&ds->ds_lock);
5196689Smaybee 			dsl_dataset_drop_ref(ds, tag);
5206689Smaybee 			rw_enter(&dp->dp_config_rwlock, RW_READER);
5216689Smaybee 			return (ENOENT);
5226689Smaybee 		}
52311546SChris.Kirby@sun.com 		/*
52411546SChris.Kirby@sun.com 		 * The dp_config_rwlock lives above the ds_lock. And
52511546SChris.Kirby@sun.com 		 * we need to check DSL_DATASET_IS_DESTROYED() while
52611546SChris.Kirby@sun.com 		 * holding the ds_lock, so we have to drop and reacquire
52711546SChris.Kirby@sun.com 		 * the ds_lock here.
52811546SChris.Kirby@sun.com 		 */
52911546SChris.Kirby@sun.com 		mutex_exit(&ds->ds_lock);
5306689Smaybee 		rw_enter(&dp->dp_config_rwlock, RW_READER);
53111546SChris.Kirby@sun.com 		mutex_enter(&ds->ds_lock);
5326689Smaybee 	}
533789Sahrens 	mutex_exit(&ds->ds_lock);
5341544Seschrock 	return (0);
535789Sahrens }
536789Sahrens 
537789Sahrens int
5386689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
5396689Smaybee     dsl_dataset_t **dsp)
5406689Smaybee {
5416689Smaybee 	int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp);
5426689Smaybee 
5436689Smaybee 	if (err)
5446689Smaybee 		return (err);
5456689Smaybee 	return (dsl_dataset_hold_ref(*dsp, tag));
5466689Smaybee }
5476689Smaybee 
5486689Smaybee int
54910298SMatthew.Ahrens@Sun.COM dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, boolean_t inconsistentok,
55010298SMatthew.Ahrens@Sun.COM     void *tag, dsl_dataset_t **dsp)
5516689Smaybee {
55210298SMatthew.Ahrens@Sun.COM 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
5536689Smaybee 	if (err)
5546689Smaybee 		return (err);
55510298SMatthew.Ahrens@Sun.COM 	if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
55610298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(*dsp, tag);
5578779SMark.Musante@Sun.COM 		*dsp = NULL;
5586689Smaybee 		return (EBUSY);
5596689Smaybee 	}
5606689Smaybee 	return (0);
5616689Smaybee }
5626689Smaybee 
5636689Smaybee int
5646689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp)
565789Sahrens {
566789Sahrens 	dsl_dir_t *dd;
567789Sahrens 	dsl_pool_t *dp;
5686689Smaybee 	const char *snapname;
569789Sahrens 	uint64_t obj;
570789Sahrens 	int err = 0;
571789Sahrens 
5726689Smaybee 	err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname);
5731544Seschrock 	if (err)
5741544Seschrock 		return (err);
575789Sahrens 
576789Sahrens 	dp = dd->dd_pool;
577789Sahrens 	obj = dd->dd_phys->dd_head_dataset_obj;
578789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
5796689Smaybee 	if (obj)
5806689Smaybee 		err = dsl_dataset_get_ref(dp, obj, tag, dsp);
5816689Smaybee 	else
582789Sahrens 		err = ENOENT;
5836689Smaybee 	if (err)
584789Sahrens 		goto out;
5856689Smaybee 
5866689Smaybee 	err = dsl_dataset_hold_ref(*dsp, tag);
5876689Smaybee 
5886689Smaybee 	/* we may be looking for a snapshot */
5896689Smaybee 	if (err == 0 && snapname != NULL) {
5906689Smaybee 		dsl_dataset_t *ds = NULL;
5916689Smaybee 
5926689Smaybee 		if (*snapname++ != '@') {
5936689Smaybee 			dsl_dataset_rele(*dsp, tag);
594789Sahrens 			err = ENOENT;
595789Sahrens 			goto out;
596789Sahrens 		}
5976689Smaybee 
5986689Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
5996689Smaybee 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
6006689Smaybee 		if (err == 0)
6016689Smaybee 			err = dsl_dataset_get_ref(dp, obj, tag, &ds);
6026689Smaybee 		dsl_dataset_rele(*dsp, tag);
6036689Smaybee 
6046689Smaybee 		ASSERT3U((err == 0), ==, (ds != NULL));
6056689Smaybee 
6066689Smaybee 		if (ds) {
6076689Smaybee 			mutex_enter(&ds->ds_lock);
6086689Smaybee 			if (ds->ds_snapname[0] == 0)
6096689Smaybee 				(void) strlcpy(ds->ds_snapname, snapname,
6106689Smaybee 				    sizeof (ds->ds_snapname));
6116689Smaybee 			mutex_exit(&ds->ds_lock);
6126689Smaybee 			err = dsl_dataset_hold_ref(ds, tag);
6136689Smaybee 			*dsp = err ? NULL : ds;
614789Sahrens 		}
615789Sahrens 	}
616789Sahrens out:
617789Sahrens 	rw_exit(&dp->dp_config_rwlock);
618789Sahrens 	dsl_dir_close(dd, FTAG);
619789Sahrens 	return (err);
620789Sahrens }
621789Sahrens 
622789Sahrens int
62310298SMatthew.Ahrens@Sun.COM dsl_dataset_own(const char *name, boolean_t inconsistentok,
62410298SMatthew.Ahrens@Sun.COM     void *tag, dsl_dataset_t **dsp)
625789Sahrens {
62610298SMatthew.Ahrens@Sun.COM 	int err = dsl_dataset_hold(name, tag, dsp);
6276689Smaybee 	if (err)
6286689Smaybee 		return (err);
62910298SMatthew.Ahrens@Sun.COM 	if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
63010298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(*dsp, tag);
6316689Smaybee 		return (EBUSY);
6326689Smaybee 	}
6336689Smaybee 	return (0);
634789Sahrens }
635789Sahrens 
636789Sahrens void
637789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
638789Sahrens {
639789Sahrens 	if (ds == NULL) {
640789Sahrens 		(void) strcpy(name, "mos");
641789Sahrens 	} else {
642789Sahrens 		dsl_dir_name(ds->ds_dir, name);
6431544Seschrock 		VERIFY(0 == dsl_dataset_get_snapname(ds));
644789Sahrens 		if (ds->ds_snapname[0]) {
645789Sahrens 			(void) strcat(name, "@");
6466689Smaybee 			/*
6476689Smaybee 			 * We use a "recursive" mutex so that we
6486689Smaybee 			 * can call dprintf_ds() with ds_lock held.
6496689Smaybee 			 */
650789Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
651789Sahrens 				mutex_enter(&ds->ds_lock);
652789Sahrens 				(void) strcat(name, ds->ds_snapname);
653789Sahrens 				mutex_exit(&ds->ds_lock);
654789Sahrens 			} else {
655789Sahrens 				(void) strcat(name, ds->ds_snapname);
656789Sahrens 			}
657789Sahrens 		}
658789Sahrens 	}
659789Sahrens }
660789Sahrens 
6613978Smmusante static int
6623978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds)
6633978Smmusante {
6643978Smmusante 	int result;
6653978Smmusante 
6663978Smmusante 	if (ds == NULL) {
6673978Smmusante 		result = 3;	/* "mos" */
6683978Smmusante 	} else {
6693978Smmusante 		result = dsl_dir_namelen(ds->ds_dir);
6703978Smmusante 		VERIFY(0 == dsl_dataset_get_snapname(ds));
6713978Smmusante 		if (ds->ds_snapname[0]) {
6723978Smmusante 			++result;	/* adding one for the @-sign */
6733978Smmusante 			if (!MUTEX_HELD(&ds->ds_lock)) {
6743978Smmusante 				mutex_enter(&ds->ds_lock);
6753978Smmusante 				result += strlen(ds->ds_snapname);
6763978Smmusante 				mutex_exit(&ds->ds_lock);
6773978Smmusante 			} else {
6783978Smmusante 				result += strlen(ds->ds_snapname);
6793978Smmusante 			}
6803978Smmusante 		}
6813978Smmusante 	}
6823978Smmusante 
6833978Smmusante 	return (result);
6843978Smmusante }
6853978Smmusante 
6867046Sahrens void
6876689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag)
688789Sahrens {
6891544Seschrock 	dmu_buf_rele(ds->ds_dbuf, tag);
690789Sahrens }
691789Sahrens 
692789Sahrens void
6936689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
6945367Sahrens {
6956689Smaybee 	if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) {
6966689Smaybee 		rw_exit(&ds->ds_rwlock);
6976689Smaybee 	}
6986689Smaybee 	dsl_dataset_drop_ref(ds, tag);
6996689Smaybee }
7006689Smaybee 
7016689Smaybee void
70210298SMatthew.Ahrens@Sun.COM dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
7036689Smaybee {
70410298SMatthew.Ahrens@Sun.COM 	ASSERT((ds->ds_owner == tag && ds->ds_dbuf) ||
7056689Smaybee 	    (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL));
7066689Smaybee 
7075367Sahrens 	mutex_enter(&ds->ds_lock);
7086689Smaybee 	ds->ds_owner = NULL;
7096689Smaybee 	if (RW_WRITE_HELD(&ds->ds_rwlock)) {
7106689Smaybee 		rw_exit(&ds->ds_rwlock);
7116689Smaybee 		cv_broadcast(&ds->ds_exclusive_cv);
7126689Smaybee 	}
7135367Sahrens 	mutex_exit(&ds->ds_lock);
7146689Smaybee 	if (ds->ds_dbuf)
71510298SMatthew.Ahrens@Sun.COM 		dsl_dataset_drop_ref(ds, tag);
7166689Smaybee 	else
7176689Smaybee 		dsl_dataset_evict(ds->ds_dbuf, ds);
7185367Sahrens }
7195367Sahrens 
7205367Sahrens boolean_t
72110298SMatthew.Ahrens@Sun.COM dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *tag)
7225367Sahrens {
7236689Smaybee 	boolean_t gotit = FALSE;
7246689Smaybee 
7255367Sahrens 	mutex_enter(&ds->ds_lock);
7266689Smaybee 	if (ds->ds_owner == NULL &&
7276689Smaybee 	    (!DS_IS_INCONSISTENT(ds) || inconsistentok)) {
72810298SMatthew.Ahrens@Sun.COM 		ds->ds_owner = tag;
7296689Smaybee 		if (!dsl_pool_sync_context(ds->ds_dir->dd_pool))
7306689Smaybee 			rw_exit(&ds->ds_rwlock);
7316689Smaybee 		gotit = TRUE;
7325367Sahrens 	}
7335367Sahrens 	mutex_exit(&ds->ds_lock);
7346689Smaybee 	return (gotit);
7356689Smaybee }
7366689Smaybee 
7376689Smaybee void
7386689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner)
7396689Smaybee {
7406689Smaybee 	ASSERT3P(owner, ==, ds->ds_owner);
7416689Smaybee 	if (!RW_WRITE_HELD(&ds->ds_rwlock))
7426689Smaybee 		rw_enter(&ds->ds_rwlock, RW_WRITER);
7435367Sahrens }
7445367Sahrens 
7452199Sahrens uint64_t
7467046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
7476492Stimh     uint64_t flags, dmu_tx_t *tx)
748789Sahrens {
7495367Sahrens 	dsl_pool_t *dp = dd->dd_pool;
750789Sahrens 	dmu_buf_t *dbuf;
751789Sahrens 	dsl_dataset_phys_t *dsphys;
7525367Sahrens 	uint64_t dsobj;
753789Sahrens 	objset_t *mos = dp->dp_meta_objset;
754789Sahrens 
7557046Sahrens 	if (origin == NULL)
7567046Sahrens 		origin = dp->dp_origin_snap;
7577046Sahrens 
7585367Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
7595367Sahrens 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
760789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7615367Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
762789Sahrens 
763928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
764928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
7651544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
766789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
767789Sahrens 	dsphys = dbuf->db_data;
7686689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
769789Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
7706492Stimh 	dsphys->ds_flags = flags;
771789Sahrens 	dsphys->ds_fsid_guid = unique_create();
772789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
773789Sahrens 	    sizeof (dsphys->ds_guid));
774789Sahrens 	dsphys->ds_snapnames_zapobj =
7756492Stimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
7766492Stimh 	    DMU_OT_NONE, 0, tx);
777789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
7787046Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
779789Sahrens 	dsphys->ds_deadlist_obj =
780789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
7815378Sck153898 
7825367Sahrens 	if (origin) {
7835367Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
784789Sahrens 		dsphys->ds_prev_snap_txg =
7855367Sahrens 		    origin->ds_phys->ds_creation_txg;
786789Sahrens 		dsphys->ds_used_bytes =
7875367Sahrens 		    origin->ds_phys->ds_used_bytes;
788789Sahrens 		dsphys->ds_compressed_bytes =
7895367Sahrens 		    origin->ds_phys->ds_compressed_bytes;
790789Sahrens 		dsphys->ds_uncompressed_bytes =
7915367Sahrens 		    origin->ds_phys->ds_uncompressed_bytes;
7925367Sahrens 		dsphys->ds_bp = origin->ds_phys->ds_bp;
7936502Stimh 		dsphys->ds_flags |= origin->ds_phys->ds_flags;
794789Sahrens 
7955367Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
7965367Sahrens 		origin->ds_phys->ds_num_children++;
797789Sahrens 
7987046Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
7997046Sahrens 			if (origin->ds_phys->ds_next_clones_obj == 0) {
8007046Sahrens 				origin->ds_phys->ds_next_clones_obj =
8017046Sahrens 				    zap_create(mos,
8027046Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
8037046Sahrens 			}
8047046Sahrens 			VERIFY(0 == zap_add_int(mos,
8057046Sahrens 			    origin->ds_phys->ds_next_clones_obj,
8067046Sahrens 			    dsobj, tx));
8077046Sahrens 		}
8087046Sahrens 
809789Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
8105367Sahrens 		dd->dd_phys->dd_origin_obj = origin->ds_object;
811789Sahrens 	}
8126492Stimh 
8136492Stimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
8146492Stimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
8156492Stimh 
8161544Seschrock 	dmu_buf_rele(dbuf, FTAG);
817789Sahrens 
818789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
819789Sahrens 	dd->dd_phys->dd_head_dataset_obj = dsobj;
8205367Sahrens 
8215367Sahrens 	return (dsobj);
8225367Sahrens }
8235367Sahrens 
8245367Sahrens uint64_t
8256492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
8266492Stimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
8275367Sahrens {
8285367Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
8295367Sahrens 	uint64_t dsobj, ddobj;
8305367Sahrens 	dsl_dir_t *dd;
8315367Sahrens 
8325367Sahrens 	ASSERT(lastname[0] != '@');
8335367Sahrens 
8347046Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
8355367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
8365367Sahrens 
8377046Sahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx);
8385367Sahrens 
8395367Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
8405367Sahrens 
841789Sahrens 	dsl_dir_close(dd, FTAG);
842789Sahrens 
8432199Sahrens 	return (dsobj);
8442199Sahrens }
8452199Sahrens 
8462199Sahrens struct destroyarg {
8472199Sahrens 	dsl_sync_task_group_t *dstg;
8482199Sahrens 	char *snapname;
8492199Sahrens 	char *failed;
85010242Schris.kirby@sun.com 	boolean_t defer;
8512199Sahrens };
8522199Sahrens 
8532199Sahrens static int
85411209SMatthew.Ahrens@Sun.COM dsl_snapshot_destroy_one(const char *name, void *arg)
8552199Sahrens {
8562199Sahrens 	struct destroyarg *da = arg;
8572199Sahrens 	dsl_dataset_t *ds;
8582199Sahrens 	int err;
85910242Schris.kirby@sun.com 	char *dsname;
86010272SMatthew.Ahrens@Sun.COM 
86110272SMatthew.Ahrens@Sun.COM 	dsname = kmem_asprintf("%s@%s", name, da->snapname);
86210298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_own(dsname, B_TRUE, da->dstg, &ds);
86310272SMatthew.Ahrens@Sun.COM 	strfree(dsname);
8646689Smaybee 	if (err == 0) {
86510242Schris.kirby@sun.com 		struct dsl_ds_destroyarg *dsda;
86610242Schris.kirby@sun.com 
8676689Smaybee 		dsl_dataset_make_exclusive(ds, da->dstg);
86810242Schris.kirby@sun.com 		dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg), KM_SLEEP);
86910242Schris.kirby@sun.com 		dsda->ds = ds;
87010242Schris.kirby@sun.com 		dsda->defer = da->defer;
8716689Smaybee 		dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check,
87210242Schris.kirby@sun.com 		    dsl_dataset_destroy_sync, dsda, da->dstg, 0);
8736689Smaybee 	} else if (err == ENOENT) {
8746689Smaybee 		err = 0;
8756689Smaybee 	} else {
8762199Sahrens 		(void) strcpy(da->failed, name);
8772199Sahrens 	}
8786689Smaybee 	return (err);
879789Sahrens }
880789Sahrens 
8812199Sahrens /*
8822199Sahrens  * Destroy 'snapname' in all descendants of 'fsname'.
8832199Sahrens  */
8842199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy
8852199Sahrens int
88610242Schris.kirby@sun.com dsl_snapshots_destroy(char *fsname, char *snapname, boolean_t defer)
8872199Sahrens {
8882199Sahrens 	int err;
8892199Sahrens 	struct destroyarg da;
8902199Sahrens 	dsl_sync_task_t *dst;
8912199Sahrens 	spa_t *spa;
8922199Sahrens 
8934603Sahrens 	err = spa_open(fsname, &spa, FTAG);
8942199Sahrens 	if (err)
8952199Sahrens 		return (err);
8962199Sahrens 	da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8972199Sahrens 	da.snapname = snapname;
8982199Sahrens 	da.failed = fsname;
89910242Schris.kirby@sun.com 	da.defer = defer;
9002199Sahrens 
9012199Sahrens 	err = dmu_objset_find(fsname,
9022417Sahrens 	    dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN);
9032199Sahrens 
9042199Sahrens 	if (err == 0)
9052199Sahrens 		err = dsl_sync_task_group_wait(da.dstg);
9062199Sahrens 
9072199Sahrens 	for (dst = list_head(&da.dstg->dstg_tasks); dst;
9082199Sahrens 	    dst = list_next(&da.dstg->dstg_tasks, dst)) {
90910242Schris.kirby@sun.com 		struct dsl_ds_destroyarg *dsda = dst->dst_arg1;
91010242Schris.kirby@sun.com 		dsl_dataset_t *ds = dsda->ds;
91110242Schris.kirby@sun.com 
9126689Smaybee 		/*
9136689Smaybee 		 * Return the file system name that triggered the error
9146689Smaybee 		 */
9152199Sahrens 		if (dst->dst_err) {
9162199Sahrens 			dsl_dataset_name(ds, fsname);
9174603Sahrens 			*strchr(fsname, '@') = '\0';
9182199Sahrens 		}
91910242Schris.kirby@sun.com 		ASSERT3P(dsda->rm_origin, ==, NULL);
9206689Smaybee 		dsl_dataset_disown(ds, da.dstg);
92110242Schris.kirby@sun.com 		kmem_free(dsda, sizeof (struct dsl_ds_destroyarg));
9222199Sahrens 	}
9232199Sahrens 
9242199Sahrens 	dsl_sync_task_group_destroy(da.dstg);
9252199Sahrens 	spa_close(spa, FTAG);
9262199Sahrens 	return (err);
9272199Sahrens }
9282199Sahrens 
92910242Schris.kirby@sun.com static boolean_t
93010242Schris.kirby@sun.com dsl_dataset_might_destroy_origin(dsl_dataset_t *ds)
93110242Schris.kirby@sun.com {
93210242Schris.kirby@sun.com 	boolean_t might_destroy = B_FALSE;
93310242Schris.kirby@sun.com 
93410242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
93510242Schris.kirby@sun.com 	if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 &&
93610242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds))
93710242Schris.kirby@sun.com 		might_destroy = B_TRUE;
93810242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
93910242Schris.kirby@sun.com 
94010242Schris.kirby@sun.com 	return (might_destroy);
94110242Schris.kirby@sun.com }
94210242Schris.kirby@sun.com 
94310242Schris.kirby@sun.com /*
94410242Schris.kirby@sun.com  * If we're removing a clone, and these three conditions are true:
94510242Schris.kirby@sun.com  *	1) the clone's origin has no other children
94610242Schris.kirby@sun.com  *	2) the clone's origin has no user references
94710242Schris.kirby@sun.com  *	3) the clone's origin has been marked for deferred destruction
94810242Schris.kirby@sun.com  * Then, prepare to remove the origin as part of this sync task group.
94910242Schris.kirby@sun.com  */
95010242Schris.kirby@sun.com static int
95110242Schris.kirby@sun.com dsl_dataset_origin_rm_prep(struct dsl_ds_destroyarg *dsda, void *tag)
95210242Schris.kirby@sun.com {
95310242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
95410242Schris.kirby@sun.com 	dsl_dataset_t *origin = ds->ds_prev;
95510242Schris.kirby@sun.com 
95610242Schris.kirby@sun.com 	if (dsl_dataset_might_destroy_origin(origin)) {
95710242Schris.kirby@sun.com 		char *name;
95810242Schris.kirby@sun.com 		int namelen;
95910242Schris.kirby@sun.com 		int error;
96010242Schris.kirby@sun.com 
96110242Schris.kirby@sun.com 		namelen = dsl_dataset_namelen(origin) + 1;
96210242Schris.kirby@sun.com 		name = kmem_alloc(namelen, KM_SLEEP);
96310242Schris.kirby@sun.com 		dsl_dataset_name(origin, name);
96410242Schris.kirby@sun.com #ifdef _KERNEL
96510242Schris.kirby@sun.com 		error = zfs_unmount_snap(name, NULL);
96610242Schris.kirby@sun.com 		if (error) {
96710242Schris.kirby@sun.com 			kmem_free(name, namelen);
96810242Schris.kirby@sun.com 			return (error);
96910242Schris.kirby@sun.com 		}
97010242Schris.kirby@sun.com #endif
97110298SMatthew.Ahrens@Sun.COM 		error = dsl_dataset_own(name, B_TRUE, tag, &origin);
97210242Schris.kirby@sun.com 		kmem_free(name, namelen);
97310242Schris.kirby@sun.com 		if (error)
97410242Schris.kirby@sun.com 			return (error);
97510242Schris.kirby@sun.com 		dsda->rm_origin = origin;
97610242Schris.kirby@sun.com 		dsl_dataset_make_exclusive(origin, tag);
97710242Schris.kirby@sun.com 	}
97810242Schris.kirby@sun.com 
97910242Schris.kirby@sun.com 	return (0);
98010242Schris.kirby@sun.com }
98110242Schris.kirby@sun.com 
9825367Sahrens /*
9836689Smaybee  * ds must be opened as OWNER.  On return (whether successful or not),
9846689Smaybee  * ds will be closed and caller can no longer dereference it.
9855367Sahrens  */
986789Sahrens int
98710242Schris.kirby@sun.com dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer)
988789Sahrens {
989789Sahrens 	int err;
9902199Sahrens 	dsl_sync_task_group_t *dstg;
9912199Sahrens 	objset_t *os;
992789Sahrens 	dsl_dir_t *dd;
9932199Sahrens 	uint64_t obj;
99411022STom.Erickson@Sun.COM 	struct dsl_ds_destroyarg dsda = { 0 };
99511022STom.Erickson@Sun.COM 	dsl_dataset_t dummy_ds = { 0 };
99610242Schris.kirby@sun.com 
99710242Schris.kirby@sun.com 	dsda.ds = ds;
9982199Sahrens 
9995367Sahrens 	if (dsl_dataset_is_snapshot(ds)) {
10002199Sahrens 		/* Destroying a snapshot is simpler */
10016689Smaybee 		dsl_dataset_make_exclusive(ds, tag);
10027237Sek110237 
100310242Schris.kirby@sun.com 		dsda.defer = defer;
10042199Sahrens 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
10052199Sahrens 		    dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
100610242Schris.kirby@sun.com 		    &dsda, tag, 0);
100710242Schris.kirby@sun.com 		ASSERT3P(dsda.rm_origin, ==, NULL);
10085367Sahrens 		goto out;
100910385Schris.kirby@sun.com 	} else if (defer) {
101010385Schris.kirby@sun.com 		err = EINVAL;
101110385Schris.kirby@sun.com 		goto out;
10122199Sahrens 	}
10132199Sahrens 
10142199Sahrens 	dd = ds->ds_dir;
101511022STom.Erickson@Sun.COM 	dummy_ds.ds_dir = dd;
101611022STom.Erickson@Sun.COM 	dummy_ds.ds_object = ds->ds_object;
1017789Sahrens 
10182199Sahrens 	/*
10192199Sahrens 	 * Check for errors and mark this ds as inconsistent, in
10202199Sahrens 	 * case we crash while freeing the objects.
10212199Sahrens 	 */
10222199Sahrens 	err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
10232199Sahrens 	    dsl_dataset_destroy_begin_sync, ds, NULL, 0);
10245367Sahrens 	if (err)
10255367Sahrens 		goto out;
10265367Sahrens 
102710298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, &os);
10285367Sahrens 	if (err)
10295367Sahrens 		goto out;
10302199Sahrens 
10312199Sahrens 	/*
10322199Sahrens 	 * remove the objects in open context, so that we won't
10332199Sahrens 	 * have too much to do in syncing context.
10342199Sahrens 	 */
10353025Sahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
10363025Sahrens 	    ds->ds_phys->ds_prev_snap_txg)) {
10376992Smaybee 		/*
10386992Smaybee 		 * Ignore errors, if there is not enough disk space
10396992Smaybee 		 * we will deal with it in dsl_dataset_destroy_sync().
10406992Smaybee 		 */
10416992Smaybee 		(void) dmu_free_object(os, obj);
10422199Sahrens 	}
10432199Sahrens 
10449396SMatthew.Ahrens@Sun.COM 	/*
10459396SMatthew.Ahrens@Sun.COM 	 * We need to sync out all in-flight IO before we try to evict
10469396SMatthew.Ahrens@Sun.COM 	 * (the dataset evict func is trying to clear the cached entries
10479396SMatthew.Ahrens@Sun.COM 	 * for this dataset in the ARC).
10489396SMatthew.Ahrens@Sun.COM 	 */
10499396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dd->dd_pool, 0);
10509396SMatthew.Ahrens@Sun.COM 
10519396SMatthew.Ahrens@Sun.COM 	/*
10529396SMatthew.Ahrens@Sun.COM 	 * If we managed to free all the objects in open
10539396SMatthew.Ahrens@Sun.COM 	 * context, the user space accounting should be zero.
10549396SMatthew.Ahrens@Sun.COM 	 */
10559396SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_bp.blk_fill == 0 &&
105610298SMatthew.Ahrens@Sun.COM 	    dmu_objset_userused_enabled(os)) {
10579396SMatthew.Ahrens@Sun.COM 		uint64_t count;
10589396SMatthew.Ahrens@Sun.COM 
10599396SMatthew.Ahrens@Sun.COM 		ASSERT(zap_count(os, DMU_USERUSED_OBJECT, &count) != 0 ||
10609396SMatthew.Ahrens@Sun.COM 		    count == 0);
10619396SMatthew.Ahrens@Sun.COM 		ASSERT(zap_count(os, DMU_GROUPUSED_OBJECT, &count) != 0 ||
10629396SMatthew.Ahrens@Sun.COM 		    count == 0);
10639396SMatthew.Ahrens@Sun.COM 	}
10649396SMatthew.Ahrens@Sun.COM 
10652199Sahrens 	if (err != ESRCH)
10665367Sahrens 		goto out;
10672199Sahrens 
10686975Smaybee 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
10696975Smaybee 	err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd);
10706975Smaybee 	rw_exit(&dd->dd_pool->dp_config_rwlock);
10716975Smaybee 
10726975Smaybee 	if (err)
10736975Smaybee 		goto out;
10746975Smaybee 
10752199Sahrens 	/*
10762199Sahrens 	 * Blow away the dsl_dir + head dataset.
10772199Sahrens 	 */
10786689Smaybee 	dsl_dataset_make_exclusive(ds, tag);
107910242Schris.kirby@sun.com 	/*
108010242Schris.kirby@sun.com 	 * If we're removing a clone, we might also need to remove its
108110242Schris.kirby@sun.com 	 * origin.
108210242Schris.kirby@sun.com 	 */
108310242Schris.kirby@sun.com 	do {
108410242Schris.kirby@sun.com 		dsda.need_prep = B_FALSE;
108510242Schris.kirby@sun.com 		if (dsl_dir_is_clone(dd)) {
108610242Schris.kirby@sun.com 			err = dsl_dataset_origin_rm_prep(&dsda, tag);
108710242Schris.kirby@sun.com 			if (err) {
108810242Schris.kirby@sun.com 				dsl_dir_close(dd, FTAG);
108910242Schris.kirby@sun.com 				goto out;
109010242Schris.kirby@sun.com 			}
109110242Schris.kirby@sun.com 		}
109210242Schris.kirby@sun.com 
109310242Schris.kirby@sun.com 		dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
109410242Schris.kirby@sun.com 		dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
109510242Schris.kirby@sun.com 		    dsl_dataset_destroy_sync, &dsda, tag, 0);
109610242Schris.kirby@sun.com 		dsl_sync_task_create(dstg, dsl_dir_destroy_check,
109711022STom.Erickson@Sun.COM 		    dsl_dir_destroy_sync, &dummy_ds, FTAG, 0);
109810242Schris.kirby@sun.com 		err = dsl_sync_task_group_wait(dstg);
109910242Schris.kirby@sun.com 		dsl_sync_task_group_destroy(dstg);
110010242Schris.kirby@sun.com 
110110242Schris.kirby@sun.com 		/*
110210242Schris.kirby@sun.com 		 * We could be racing against 'zfs release' or 'zfs destroy -d'
110310242Schris.kirby@sun.com 		 * on the origin snap, in which case we can get EBUSY if we
110410242Schris.kirby@sun.com 		 * needed to destroy the origin snap but were not ready to
110510242Schris.kirby@sun.com 		 * do so.
110610242Schris.kirby@sun.com 		 */
110710242Schris.kirby@sun.com 		if (dsda.need_prep) {
110810242Schris.kirby@sun.com 			ASSERT(err == EBUSY);
110910242Schris.kirby@sun.com 			ASSERT(dsl_dir_is_clone(dd));
111010242Schris.kirby@sun.com 			ASSERT(dsda.rm_origin == NULL);
111110242Schris.kirby@sun.com 		}
111210242Schris.kirby@sun.com 	} while (dsda.need_prep);
111310242Schris.kirby@sun.com 
111410242Schris.kirby@sun.com 	if (dsda.rm_origin != NULL)
111510242Schris.kirby@sun.com 		dsl_dataset_disown(dsda.rm_origin, tag);
111610242Schris.kirby@sun.com 
11176689Smaybee 	/* if it is successful, dsl_dir_destroy_sync will close the dd */
11185367Sahrens 	if (err)
11192199Sahrens 		dsl_dir_close(dd, FTAG);
11205367Sahrens out:
11216689Smaybee 	dsl_dataset_disown(ds, tag);
1122789Sahrens 	return (err);
1123789Sahrens }
1124789Sahrens 
11253547Smaybee blkptr_t *
11263547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1127789Sahrens {
11283547Smaybee 	return (&ds->ds_phys->ds_bp);
1129789Sahrens }
1130789Sahrens 
1131789Sahrens void
1132789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1133789Sahrens {
1134789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1135789Sahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
1136789Sahrens 	if (ds == NULL) {
1137789Sahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
1138789Sahrens 	} else {
1139789Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1140789Sahrens 		ds->ds_phys->ds_bp = *bp;
1141789Sahrens 	}
1142789Sahrens }
1143789Sahrens 
1144789Sahrens spa_t *
1145789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
1146789Sahrens {
1147789Sahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1148789Sahrens }
1149789Sahrens 
1150789Sahrens void
1151789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1152789Sahrens {
1153789Sahrens 	dsl_pool_t *dp;
1154789Sahrens 
1155789Sahrens 	if (ds == NULL) /* this is the meta-objset */
1156789Sahrens 		return;
1157789Sahrens 
115810298SMatthew.Ahrens@Sun.COM 	ASSERT(ds->ds_objset != NULL);
11592885Sahrens 
11602885Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
11612885Sahrens 		panic("dirtying snapshot!");
1162789Sahrens 
1163789Sahrens 	dp = ds->ds_dir->dd_pool;
1164789Sahrens 
1165789Sahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
1166789Sahrens 		/* up the hold count until we can be written out */
1167789Sahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1168789Sahrens 	}
1169789Sahrens }
1170789Sahrens 
11715378Sck153898 /*
11725378Sck153898  * The unique space in the head dataset can be calculated by subtracting
11735378Sck153898  * the space used in the most recent snapshot, that is still being used
11745378Sck153898  * in this file system, from the space currently in use.  To figure out
11755378Sck153898  * the space in the most recent snapshot still in use, we need to take
11765378Sck153898  * the total space used in the snapshot and subtract out the space that
11775378Sck153898  * has been freed up since the snapshot was taken.
11785378Sck153898  */
11795378Sck153898 static void
11805378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
11815378Sck153898 {
11825378Sck153898 	uint64_t mrs_used;
11835378Sck153898 	uint64_t dlused, dlcomp, dluncomp;
11845378Sck153898 
1185*12296SLin.Ling@Sun.COM 	ASSERT(!dsl_dataset_is_snapshot(ds));
11865378Sck153898 
11875378Sck153898 	if (ds->ds_phys->ds_prev_snap_obj != 0)
11885378Sck153898 		mrs_used = ds->ds_prev->ds_phys->ds_used_bytes;
11895378Sck153898 	else
11905378Sck153898 		mrs_used = 0;
11915378Sck153898 
11925378Sck153898 	VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp,
11935378Sck153898 	    &dluncomp));
11945378Sck153898 
11955378Sck153898 	ASSERT3U(dlused, <=, mrs_used);
11965378Sck153898 	ds->ds_phys->ds_unique_bytes =
11975378Sck153898 	    ds->ds_phys->ds_used_bytes - (mrs_used - dlused);
11985378Sck153898 
1199*12296SLin.Ling@Sun.COM 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
12005378Sck153898 	    SPA_VERSION_UNIQUE_ACCURATE)
12015378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
12025378Sck153898 }
12035378Sck153898 
1204789Sahrens struct killarg {
12057390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
1206789Sahrens 	dmu_tx_t *tx;
1207789Sahrens };
1208789Sahrens 
12097390SMatthew.Ahrens@Sun.COM /* ARGSUSED */
1210789Sahrens static int
1211*12296SLin.Ling@Sun.COM kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
121210922SJeff.Bonwick@Sun.COM     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1213789Sahrens {
1214789Sahrens 	struct killarg *ka = arg;
121510922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx = ka->tx;
12167837SMatthew.Ahrens@Sun.COM 
12177837SMatthew.Ahrens@Sun.COM 	if (bp == NULL)
12187837SMatthew.Ahrens@Sun.COM 		return (0);
1219789Sahrens 
122010922SJeff.Bonwick@Sun.COM 	if (zb->zb_level == ZB_ZIL_LEVEL) {
122110922SJeff.Bonwick@Sun.COM 		ASSERT(zilog != NULL);
12228746SMatthew.Ahrens@Sun.COM 		/*
12238746SMatthew.Ahrens@Sun.COM 		 * It's a block in the intent log.  It has no
12248746SMatthew.Ahrens@Sun.COM 		 * accounting, so just free it.
12258746SMatthew.Ahrens@Sun.COM 		 */
122610922SJeff.Bonwick@Sun.COM 		dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
12278746SMatthew.Ahrens@Sun.COM 	} else {
122810922SJeff.Bonwick@Sun.COM 		ASSERT(zilog == NULL);
12298746SMatthew.Ahrens@Sun.COM 		ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
123010922SJeff.Bonwick@Sun.COM 		(void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
12318746SMatthew.Ahrens@Sun.COM 	}
12327390SMatthew.Ahrens@Sun.COM 
1233789Sahrens 	return (0);
1234789Sahrens }
1235789Sahrens 
1236789Sahrens /* ARGSUSED */
12372199Sahrens static int
12382199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
12391731Sbonwick {
12402199Sahrens 	dsl_dataset_t *ds = arg1;
12415367Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
12425367Sahrens 	uint64_t count;
12435367Sahrens 	int err;
12441731Sbonwick 
12451731Sbonwick 	/*
12461731Sbonwick 	 * Can't delete a head dataset if there are snapshots of it.
12471731Sbonwick 	 * (Except if the only snapshots are from the branch we cloned
12481731Sbonwick 	 * from.)
12491731Sbonwick 	 */
12501731Sbonwick 	if (ds->ds_prev != NULL &&
12511731Sbonwick 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
125210816SVitezslav.Batrla@Sun.COM 		return (EBUSY);
12531731Sbonwick 
12545367Sahrens 	/*
12555367Sahrens 	 * This is really a dsl_dir thing, but check it here so that
12565367Sahrens 	 * we'll be less likely to leave this dataset inconsistent &
12575367Sahrens 	 * nearly destroyed.
12585367Sahrens 	 */
12595367Sahrens 	err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
12605367Sahrens 	if (err)
12615367Sahrens 		return (err);
12625367Sahrens 	if (count != 0)
12635367Sahrens 		return (EEXIST);
12645367Sahrens 
12651731Sbonwick 	return (0);
12661731Sbonwick }
12671731Sbonwick 
12682199Sahrens /* ARGSUSED */
12692199Sahrens static void
1270*12296SLin.Ling@Sun.COM dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1271789Sahrens {
12722199Sahrens 	dsl_dataset_t *ds = arg1;
12734543Smarks 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1274789Sahrens 
12752199Sahrens 	/* Mark it as inconsistent on-disk, in case we crash */
12762199Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
12772199Sahrens 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
12784543Smarks 
1279*12296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
1280*12296SLin.Ling@Sun.COM 	    "dataset = %llu", ds->ds_object);
12812199Sahrens }
1282789Sahrens 
128310242Schris.kirby@sun.com static int
128410242Schris.kirby@sun.com dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag,
128510242Schris.kirby@sun.com     dmu_tx_t *tx)
128610242Schris.kirby@sun.com {
128710242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
128810242Schris.kirby@sun.com 	dsl_dataset_t *ds_prev = ds->ds_prev;
128910242Schris.kirby@sun.com 
129010242Schris.kirby@sun.com 	if (dsl_dataset_might_destroy_origin(ds_prev)) {
129110242Schris.kirby@sun.com 		struct dsl_ds_destroyarg ndsda = {0};
129210242Schris.kirby@sun.com 
129310242Schris.kirby@sun.com 		/*
129410242Schris.kirby@sun.com 		 * If we're not prepared to remove the origin, don't remove
129510242Schris.kirby@sun.com 		 * the clone either.
129610242Schris.kirby@sun.com 		 */
129710242Schris.kirby@sun.com 		if (dsda->rm_origin == NULL) {
129810242Schris.kirby@sun.com 			dsda->need_prep = B_TRUE;
129910242Schris.kirby@sun.com 			return (EBUSY);
130010242Schris.kirby@sun.com 		}
130110242Schris.kirby@sun.com 
130210242Schris.kirby@sun.com 		ndsda.ds = ds_prev;
130310242Schris.kirby@sun.com 		ndsda.is_origin_rm = B_TRUE;
130410242Schris.kirby@sun.com 		return (dsl_dataset_destroy_check(&ndsda, tag, tx));
130510242Schris.kirby@sun.com 	}
130610242Schris.kirby@sun.com 
130710242Schris.kirby@sun.com 	/*
130810242Schris.kirby@sun.com 	 * If we're not going to remove the origin after all,
130910242Schris.kirby@sun.com 	 * undo the open context setup.
131010242Schris.kirby@sun.com 	 */
131110242Schris.kirby@sun.com 	if (dsda->rm_origin != NULL) {
131210242Schris.kirby@sun.com 		dsl_dataset_disown(dsda->rm_origin, tag);
131310242Schris.kirby@sun.com 		dsda->rm_origin = NULL;
131410242Schris.kirby@sun.com 	}
131510242Schris.kirby@sun.com 
131610242Schris.kirby@sun.com 	return (0);
131710242Schris.kirby@sun.com }
131810242Schris.kirby@sun.com 
13192199Sahrens /* ARGSUSED */
13205367Sahrens int
13212199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
13222199Sahrens {
132310242Schris.kirby@sun.com 	struct dsl_ds_destroyarg *dsda = arg1;
132410242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
1325789Sahrens 
13266689Smaybee 	/* we have an owner hold, so noone else can destroy us */
13276689Smaybee 	ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
13286689Smaybee 
132910242Schris.kirby@sun.com 	/*
133010242Schris.kirby@sun.com 	 * Only allow deferred destroy on pools that support it.
133110242Schris.kirby@sun.com 	 * NOTE: deferred destroy is only supported on snapshots.
133210242Schris.kirby@sun.com 	 */
133310242Schris.kirby@sun.com 	if (dsda->defer) {
133410242Schris.kirby@sun.com 		if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
133510242Schris.kirby@sun.com 		    SPA_VERSION_USERREFS)
133610242Schris.kirby@sun.com 			return (ENOTSUP);
133710242Schris.kirby@sun.com 		ASSERT(dsl_dataset_is_snapshot(ds));
133810242Schris.kirby@sun.com 		return (0);
133910242Schris.kirby@sun.com 	}
1340789Sahrens 
1341789Sahrens 	/*
1342789Sahrens 	 * Can't delete a head dataset if there are snapshots of it.
1343789Sahrens 	 * (Except if the only snapshots are from the branch we cloned
1344789Sahrens 	 * from.)
1345789Sahrens 	 */
1346789Sahrens 	if (ds->ds_prev != NULL &&
13472199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
134810816SVitezslav.Batrla@Sun.COM 		return (EBUSY);
1349789Sahrens 
1350789Sahrens 	/*
1351789Sahrens 	 * If we made changes this txg, traverse_dsl_dataset won't find
1352789Sahrens 	 * them.  Try again.
1353789Sahrens 	 */
13542199Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1355789Sahrens 		return (EAGAIN);
13562199Sahrens 
135710242Schris.kirby@sun.com 	if (dsl_dataset_is_snapshot(ds)) {
135810242Schris.kirby@sun.com 		/*
135910242Schris.kirby@sun.com 		 * If this snapshot has an elevated user reference count,
136010242Schris.kirby@sun.com 		 * we can't destroy it yet.
136110242Schris.kirby@sun.com 		 */
136210242Schris.kirby@sun.com 		if (ds->ds_userrefs > 0 && !dsda->releasing)
136310242Schris.kirby@sun.com 			return (EBUSY);
136410242Schris.kirby@sun.com 
136510242Schris.kirby@sun.com 		mutex_enter(&ds->ds_lock);
136610242Schris.kirby@sun.com 		/*
136710242Schris.kirby@sun.com 		 * Can't delete a branch point. However, if we're destroying
136810242Schris.kirby@sun.com 		 * a clone and removing its origin due to it having a user
136910242Schris.kirby@sun.com 		 * hold count of 0 and having been marked for deferred destroy,
137010242Schris.kirby@sun.com 		 * it's OK for the origin to have a single clone.
137110242Schris.kirby@sun.com 		 */
137210242Schris.kirby@sun.com 		if (ds->ds_phys->ds_num_children >
137310242Schris.kirby@sun.com 		    (dsda->is_origin_rm ? 2 : 1)) {
137410242Schris.kirby@sun.com 			mutex_exit(&ds->ds_lock);
137510242Schris.kirby@sun.com 			return (EEXIST);
137610242Schris.kirby@sun.com 		}
137710242Schris.kirby@sun.com 		mutex_exit(&ds->ds_lock);
137810242Schris.kirby@sun.com 	} else if (dsl_dir_is_clone(ds->ds_dir)) {
137910242Schris.kirby@sun.com 		return (dsl_dataset_origin_check(dsda, arg2, tx));
138010242Schris.kirby@sun.com 	}
138110242Schris.kirby@sun.com 
13822199Sahrens 	/* XXX we should do some i/o error checking... */
13832199Sahrens 	return (0);
13842199Sahrens }
13852199Sahrens 
13866689Smaybee struct refsarg {
13876689Smaybee 	kmutex_t lock;
13886689Smaybee 	boolean_t gone;
13896689Smaybee 	kcondvar_t cv;
13906689Smaybee };
13916689Smaybee 
13926689Smaybee /* ARGSUSED */
13936689Smaybee static void
13946689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv)
13956689Smaybee {
13966689Smaybee 	struct refsarg *arg = argv;
13976689Smaybee 
13986689Smaybee 	mutex_enter(&arg->lock);
13996689Smaybee 	arg->gone = TRUE;
14006689Smaybee 	cv_signal(&arg->cv);
14016689Smaybee 	mutex_exit(&arg->lock);
14026689Smaybee }
14036689Smaybee 
14046689Smaybee static void
14056689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag)
14066689Smaybee {
14076689Smaybee 	struct refsarg arg;
14086689Smaybee 
14096689Smaybee 	mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL);
14106689Smaybee 	cv_init(&arg.cv, NULL, CV_DEFAULT, NULL);
14116689Smaybee 	arg.gone = FALSE;
14126689Smaybee 	(void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys,
14136689Smaybee 	    dsl_dataset_refs_gone);
14146689Smaybee 	dmu_buf_rele(ds->ds_dbuf, tag);
14156689Smaybee 	mutex_enter(&arg.lock);
14166689Smaybee 	while (!arg.gone)
14176689Smaybee 		cv_wait(&arg.cv, &arg.lock);
14186689Smaybee 	ASSERT(arg.gone);
14196689Smaybee 	mutex_exit(&arg.lock);
14206689Smaybee 	ds->ds_dbuf = NULL;
14216689Smaybee 	ds->ds_phys = NULL;
14226689Smaybee 	mutex_destroy(&arg.lock);
14236689Smaybee 	cv_destroy(&arg.cv);
14246689Smaybee }
14256689Smaybee 
142610801SMatthew.Ahrens@Sun.COM static void
142710801SMatthew.Ahrens@Sun.COM remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, dmu_tx_t *tx)
142810801SMatthew.Ahrens@Sun.COM {
142910801SMatthew.Ahrens@Sun.COM 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
143010801SMatthew.Ahrens@Sun.COM 	uint64_t count;
143110801SMatthew.Ahrens@Sun.COM 	int err;
143210801SMatthew.Ahrens@Sun.COM 
143310801SMatthew.Ahrens@Sun.COM 	ASSERT(ds->ds_phys->ds_num_children >= 2);
143410801SMatthew.Ahrens@Sun.COM 	err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
143510801SMatthew.Ahrens@Sun.COM 	/*
143610801SMatthew.Ahrens@Sun.COM 	 * The err should not be ENOENT, but a bug in a previous version
143710801SMatthew.Ahrens@Sun.COM 	 * of the code could cause upgrade_clones_cb() to not set
143810801SMatthew.Ahrens@Sun.COM 	 * ds_next_snap_obj when it should, leading to a missing entry.
143910801SMatthew.Ahrens@Sun.COM 	 * If we knew that the pool was created after
144010801SMatthew.Ahrens@Sun.COM 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
144110801SMatthew.Ahrens@Sun.COM 	 * ENOENT.  However, at least we can check that we don't have
144210801SMatthew.Ahrens@Sun.COM 	 * too many entries in the next_clones_obj even after failing to
144310801SMatthew.Ahrens@Sun.COM 	 * remove this one.
144410801SMatthew.Ahrens@Sun.COM 	 */
144510801SMatthew.Ahrens@Sun.COM 	if (err != ENOENT) {
144610801SMatthew.Ahrens@Sun.COM 		VERIFY3U(err, ==, 0);
144710801SMatthew.Ahrens@Sun.COM 	}
144810801SMatthew.Ahrens@Sun.COM 	ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj,
144910801SMatthew.Ahrens@Sun.COM 	    &count));
145010801SMatthew.Ahrens@Sun.COM 	ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
145110801SMatthew.Ahrens@Sun.COM }
145210801SMatthew.Ahrens@Sun.COM 
14535367Sahrens void
1454*12296SLin.Ling@Sun.COM dsl_dataset_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
14552199Sahrens {
145610242Schris.kirby@sun.com 	struct dsl_ds_destroyarg *dsda = arg1;
145710242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
14582199Sahrens 	int err;
14592199Sahrens 	int after_branch_point = FALSE;
14602199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
14612199Sahrens 	objset_t *mos = dp->dp_meta_objset;
14622199Sahrens 	dsl_dataset_t *ds_prev = NULL;
14632199Sahrens 	uint64_t obj;
14642199Sahrens 
14656689Smaybee 	ASSERT(ds->ds_owner);
146610242Schris.kirby@sun.com 	ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1);
14672199Sahrens 	ASSERT(ds->ds_prev == NULL ||
14682199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
14692199Sahrens 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
14702199Sahrens 
147110242Schris.kirby@sun.com 	if (dsda->defer) {
147210242Schris.kirby@sun.com 		ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
147310242Schris.kirby@sun.com 		if (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1) {
147410242Schris.kirby@sun.com 			dmu_buf_will_dirty(ds->ds_dbuf, tx);
147510242Schris.kirby@sun.com 			ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
147610242Schris.kirby@sun.com 			return;
147710242Schris.kirby@sun.com 		}
147810242Schris.kirby@sun.com 	}
147910242Schris.kirby@sun.com 
14806689Smaybee 	/* signal any waiters that this dataset is going away */
14816689Smaybee 	mutex_enter(&ds->ds_lock);
14826689Smaybee 	ds->ds_owner = dsl_reaper;
14836689Smaybee 	cv_broadcast(&ds->ds_exclusive_cv);
14846689Smaybee 	mutex_exit(&ds->ds_lock);
14856689Smaybee 
1486*12296SLin.Ling@Sun.COM 	if (ds->ds_objset) {
1487*12296SLin.Ling@Sun.COM 		dmu_objset_evict(ds->ds_objset);
1488*12296SLin.Ling@Sun.COM 		ds->ds_objset = NULL;
1489*12296SLin.Ling@Sun.COM 	}
1490*12296SLin.Ling@Sun.COM 
14915378Sck153898 	/* Remove our reservation */
14925378Sck153898 	if (ds->ds_reserved != 0) {
149311022STom.Erickson@Sun.COM 		dsl_prop_setarg_t psa;
149411022STom.Erickson@Sun.COM 		uint64_t value = 0;
149511022STom.Erickson@Sun.COM 
149611022STom.Erickson@Sun.COM 		dsl_prop_setarg_init_uint64(&psa, "refreservation",
149711022STom.Erickson@Sun.COM 		    (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
149811022STom.Erickson@Sun.COM 		    &value);
149911022STom.Erickson@Sun.COM 		psa.psa_effective_value = 0;	/* predict default value */
150011022STom.Erickson@Sun.COM 
1501*12296SLin.Ling@Sun.COM 		dsl_dataset_set_reservation_sync(ds, &psa, tx);
15025378Sck153898 		ASSERT3U(ds->ds_reserved, ==, 0);
15035378Sck153898 	}
15045378Sck153898 
15052199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
15062199Sahrens 
1507*12296SLin.Ling@Sun.COM 	dsl_scan_ds_destroyed(ds, tx);
15087046Sahrens 
15092199Sahrens 	obj = ds->ds_object;
1510789Sahrens 
1511789Sahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1512789Sahrens 		if (ds->ds_prev) {
1513789Sahrens 			ds_prev = ds->ds_prev;
1514789Sahrens 		} else {
15156689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
15166689Smaybee 			    ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
1517789Sahrens 		}
1518789Sahrens 		after_branch_point =
1519789Sahrens 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
1520789Sahrens 
1521789Sahrens 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1522789Sahrens 		if (after_branch_point &&
15237046Sahrens 		    ds_prev->ds_phys->ds_next_clones_obj != 0) {
152410801SMatthew.Ahrens@Sun.COM 			remove_from_next_clones(ds_prev, obj, tx);
15257046Sahrens 			if (ds->ds_phys->ds_next_snap_obj != 0) {
15267046Sahrens 				VERIFY(0 == zap_add_int(mos,
15277046Sahrens 				    ds_prev->ds_phys->ds_next_clones_obj,
15287046Sahrens 				    ds->ds_phys->ds_next_snap_obj, tx));
15297046Sahrens 			}
15307046Sahrens 		}
15317046Sahrens 		if (after_branch_point &&
1532789Sahrens 		    ds->ds_phys->ds_next_snap_obj == 0) {
1533789Sahrens 			/* This clone is toast. */
1534789Sahrens 			ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1535789Sahrens 			ds_prev->ds_phys->ds_num_children--;
153610242Schris.kirby@sun.com 
153710242Schris.kirby@sun.com 			/*
153810242Schris.kirby@sun.com 			 * If the clone's origin has no other clones, no
153910242Schris.kirby@sun.com 			 * user holds, and has been marked for deferred
154010242Schris.kirby@sun.com 			 * deletion, then we should have done the necessary
154110242Schris.kirby@sun.com 			 * destroy setup for it.
154210242Schris.kirby@sun.com 			 */
154310242Schris.kirby@sun.com 			if (ds_prev->ds_phys->ds_num_children == 1 &&
154410242Schris.kirby@sun.com 			    ds_prev->ds_userrefs == 0 &&
154510242Schris.kirby@sun.com 			    DS_IS_DEFER_DESTROY(ds_prev)) {
154610242Schris.kirby@sun.com 				ASSERT3P(dsda->rm_origin, !=, NULL);
154710242Schris.kirby@sun.com 			} else {
154810242Schris.kirby@sun.com 				ASSERT3P(dsda->rm_origin, ==, NULL);
154910242Schris.kirby@sun.com 			}
1550789Sahrens 		} else if (!after_branch_point) {
1551789Sahrens 			ds_prev->ds_phys->ds_next_snap_obj =
1552789Sahrens 			    ds->ds_phys->ds_next_snap_obj;
1553789Sahrens 		}
1554789Sahrens 	}
1555789Sahrens 
1556*12296SLin.Ling@Sun.COM 	if (dsl_dataset_is_snapshot(ds)) {
15572199Sahrens 		blkptr_t bp;
155812295SGeorge.Wilson@Sun.COM 		zio_t *pio;
1559789Sahrens 		dsl_dataset_t *ds_next;
1560789Sahrens 		uint64_t itor = 0;
15615378Sck153898 		uint64_t old_unique;
15627390SMatthew.Ahrens@Sun.COM 		int64_t used = 0, compressed = 0, uncompressed = 0;
1563789Sahrens 
15646689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
15656689Smaybee 		    ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
1566789Sahrens 		ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1567789Sahrens 
1568*12296SLin.Ling@Sun.COM 		old_unique = ds_next->ds_phys->ds_unique_bytes;
15695378Sck153898 
1570789Sahrens 		dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1571789Sahrens 		ds_next->ds_phys->ds_prev_snap_obj =
1572789Sahrens 		    ds->ds_phys->ds_prev_snap_obj;
1573789Sahrens 		ds_next->ds_phys->ds_prev_snap_txg =
1574789Sahrens 		    ds->ds_phys->ds_prev_snap_txg;
1575789Sahrens 		ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1576789Sahrens 		    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1577789Sahrens 
157812295SGeorge.Wilson@Sun.COM 		pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
157912295SGeorge.Wilson@Sun.COM 
1580789Sahrens 		/*
1581789Sahrens 		 * Transfer to our deadlist (which will become next's
1582789Sahrens 		 * new deadlist) any entries from next's current
1583789Sahrens 		 * deadlist which were born before prev, and free the
1584789Sahrens 		 * other entries.
1585789Sahrens 		 *
1586789Sahrens 		 * XXX we're doing this long task with the config lock held
1587789Sahrens 		 */
15886689Smaybee 		while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) {
1589789Sahrens 			if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) {
15901544Seschrock 				VERIFY(0 == bplist_enqueue(&ds->ds_deadlist,
15911544Seschrock 				    &bp, tx));
1592789Sahrens 				if (ds_prev && !after_branch_point &&
1593789Sahrens 				    bp.blk_birth >
1594789Sahrens 				    ds_prev->ds_phys->ds_prev_snap_txg) {
1595789Sahrens 					ds_prev->ds_phys->ds_unique_bytes +=
159610922SJeff.Bonwick@Sun.COM 					    bp_get_dsize_sync(dp->dp_spa, &bp);
1597789Sahrens 				}
1598789Sahrens 			} else {
159910922SJeff.Bonwick@Sun.COM 				used += bp_get_dsize_sync(dp->dp_spa, &bp);
1600789Sahrens 				compressed += BP_GET_PSIZE(&bp);
1601789Sahrens 				uncompressed += BP_GET_UCSIZE(&bp);
160212295SGeorge.Wilson@Sun.COM 				dsl_free_sync(pio, dp, tx->tx_txg, &bp);
1603789Sahrens 			}
1604789Sahrens 		}
160512295SGeorge.Wilson@Sun.COM 		VERIFY3U(zio_wait(pio), ==, 0);
16067390SMatthew.Ahrens@Sun.COM 		ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes);
16077390SMatthew.Ahrens@Sun.COM 
16087390SMatthew.Ahrens@Sun.COM 		/* change snapused */
16097390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
16107390SMatthew.Ahrens@Sun.COM 		    -used, -compressed, -uncompressed, tx);
16117390SMatthew.Ahrens@Sun.COM 
1612789Sahrens 		/* free next's deadlist */
1613789Sahrens 		bplist_close(&ds_next->ds_deadlist);
1614789Sahrens 		bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx);
1615789Sahrens 
1616789Sahrens 		/* set next's deadlist to our deadlist */
16176689Smaybee 		bplist_close(&ds->ds_deadlist);
1618789Sahrens 		ds_next->ds_phys->ds_deadlist_obj =
1619789Sahrens 		    ds->ds_phys->ds_deadlist_obj;
16201544Seschrock 		VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos,
16211544Seschrock 		    ds_next->ds_phys->ds_deadlist_obj));
1622789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1623789Sahrens 
1624*12296SLin.Ling@Sun.COM 		if (dsl_dataset_is_snapshot(ds_next)) {
1625789Sahrens 			/*
1626789Sahrens 			 * Update next's unique to include blocks which
1627789Sahrens 			 * were previously shared by only this snapshot
1628789Sahrens 			 * and it.  Those blocks will be born after the
1629789Sahrens 			 * prev snap and before this snap, and will have
1630789Sahrens 			 * died after the next snap and before the one
1631789Sahrens 			 * after that (ie. be on the snap after next's
1632789Sahrens 			 * deadlist).
1633789Sahrens 			 *
1634789Sahrens 			 * XXX we're doing this long task with the
1635789Sahrens 			 * config lock held
1636789Sahrens 			 */
1637789Sahrens 			dsl_dataset_t *ds_after_next;
16387390SMatthew.Ahrens@Sun.COM 			uint64_t space;
1639789Sahrens 
16406689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
16416689Smaybee 			    ds_next->ds_phys->ds_next_snap_obj,
16426689Smaybee 			    FTAG, &ds_after_next));
16437390SMatthew.Ahrens@Sun.COM 
16447390SMatthew.Ahrens@Sun.COM 			VERIFY(0 ==
16457390SMatthew.Ahrens@Sun.COM 			    bplist_space_birthrange(&ds_after_next->ds_deadlist,
16467390SMatthew.Ahrens@Sun.COM 			    ds->ds_phys->ds_prev_snap_txg,
16477390SMatthew.Ahrens@Sun.COM 			    ds->ds_phys->ds_creation_txg, &space));
16487390SMatthew.Ahrens@Sun.COM 			ds_next->ds_phys->ds_unique_bytes += space;
1649789Sahrens 
16506689Smaybee 			dsl_dataset_rele(ds_after_next, FTAG);
1651789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, NULL);
1652789Sahrens 		} else {
1653789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, ds);
16546689Smaybee 			dsl_dataset_drop_ref(ds_next->ds_prev, ds_next);
16556689Smaybee 			ds_next->ds_prev = NULL;
1656789Sahrens 			if (ds_prev) {
16576689Smaybee 				VERIFY(0 == dsl_dataset_get_ref(dp,
16586689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
16596689Smaybee 				    ds_next, &ds_next->ds_prev));
1660789Sahrens 			}
16615378Sck153898 
16625378Sck153898 			dsl_dataset_recalc_head_uniq(ds_next);
16635378Sck153898 
16645378Sck153898 			/*
16655378Sck153898 			 * Reduce the amount of our unconsmed refreservation
16665378Sck153898 			 * being charged to our parent by the amount of
16675378Sck153898 			 * new unique data we have gained.
16685378Sck153898 			 */
16695378Sck153898 			if (old_unique < ds_next->ds_reserved) {
16705378Sck153898 				int64_t mrsdelta;
16715378Sck153898 				uint64_t new_unique =
16725378Sck153898 				    ds_next->ds_phys->ds_unique_bytes;
16735378Sck153898 
16745378Sck153898 				ASSERT(old_unique <= new_unique);
16755378Sck153898 				mrsdelta = MIN(new_unique - old_unique,
16765378Sck153898 				    ds_next->ds_reserved - old_unique);
16777390SMatthew.Ahrens@Sun.COM 				dsl_dir_diduse_space(ds->ds_dir,
16787390SMatthew.Ahrens@Sun.COM 				    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
16795378Sck153898 			}
1680789Sahrens 		}
16816689Smaybee 		dsl_dataset_rele(ds_next, FTAG);
1682789Sahrens 	} else {
1683789Sahrens 		/*
1684789Sahrens 		 * There's no next snapshot, so this is a head dataset.
1685789Sahrens 		 * Destroy the deadlist.  Unless it's a clone, the
1686789Sahrens 		 * deadlist should be empty.  (If it's a clone, it's
1687789Sahrens 		 * safe to ignore the deadlist contents.)
1688789Sahrens 		 */
1689789Sahrens 		struct killarg ka;
1690789Sahrens 
1691789Sahrens 		ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist));
1692789Sahrens 		bplist_close(&ds->ds_deadlist);
1693789Sahrens 		bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1694789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1695789Sahrens 
1696789Sahrens 		/*
1697789Sahrens 		 * Free everything that we point to (that's born after
1698789Sahrens 		 * the previous snapshot, if we are a clone)
1699789Sahrens 		 *
17007390SMatthew.Ahrens@Sun.COM 		 * NB: this should be very quick, because we already
17017390SMatthew.Ahrens@Sun.COM 		 * freed all the objects in open context.
1702789Sahrens 		 */
17037390SMatthew.Ahrens@Sun.COM 		ka.ds = ds;
1704789Sahrens 		ka.tx = tx;
17057837SMatthew.Ahrens@Sun.COM 		err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
17067837SMatthew.Ahrens@Sun.COM 		    TRAVERSE_POST, kill_blkptr, &ka);
1707789Sahrens 		ASSERT3U(err, ==, 0);
17089390Schris.kirby@sun.com 		ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
17097390SMatthew.Ahrens@Sun.COM 		    ds->ds_phys->ds_unique_bytes == 0);
171010342Schris.kirby@sun.com 
171110342Schris.kirby@sun.com 		if (ds->ds_prev != NULL) {
171210342Schris.kirby@sun.com 			dsl_dataset_rele(ds->ds_prev, ds);
171310342Schris.kirby@sun.com 			ds->ds_prev = ds_prev = NULL;
171410342Schris.kirby@sun.com 		}
1715789Sahrens 	}
1716789Sahrens 
17176689Smaybee 	if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
17186689Smaybee 		/* Erase the link in the dir */
17196689Smaybee 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
17206689Smaybee 		ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
17216689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
1722789Sahrens 		err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1723789Sahrens 		ASSERT(err == 0);
1724789Sahrens 	} else {
1725789Sahrens 		/* remove from snapshot namespace */
1726789Sahrens 		dsl_dataset_t *ds_head;
17276689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
17286689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
17296689Smaybee 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
17302207Sahrens 		VERIFY(0 == dsl_dataset_get_snapname(ds));
1731789Sahrens #ifdef ZFS_DEBUG
1732789Sahrens 		{
1733789Sahrens 			uint64_t val;
17346492Stimh 
17356689Smaybee 			err = dsl_dataset_snap_lookup(ds_head,
17366492Stimh 			    ds->ds_snapname, &val);
1737789Sahrens 			ASSERT3U(err, ==, 0);
1738789Sahrens 			ASSERT3U(val, ==, obj);
1739789Sahrens 		}
1740789Sahrens #endif
17416689Smaybee 		err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
1742789Sahrens 		ASSERT(err == 0);
17436689Smaybee 		dsl_dataset_rele(ds_head, FTAG);
1744789Sahrens 	}
1745789Sahrens 
1746789Sahrens 	if (ds_prev && ds->ds_prev != ds_prev)
17476689Smaybee 		dsl_dataset_rele(ds_prev, FTAG);
1748789Sahrens 
17495094Slling 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
1750*12296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_DESTROY, dp->dp_spa, tx,
1751*12296SLin.Ling@Sun.COM 	    "dataset = %llu", ds->ds_object);
17524543Smarks 
17537046Sahrens 	if (ds->ds_phys->ds_next_clones_obj != 0) {
17547046Sahrens 		uint64_t count;
17557046Sahrens 		ASSERT(0 == zap_count(mos,
17567046Sahrens 		    ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
17577046Sahrens 		VERIFY(0 == dmu_object_free(mos,
17587046Sahrens 		    ds->ds_phys->ds_next_clones_obj, tx));
17597046Sahrens 	}
17607390SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_props_obj != 0)
17617390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
176210242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj != 0)
176310242Schris.kirby@sun.com 		VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
17646689Smaybee 	dsl_dir_close(ds->ds_dir, ds);
17656689Smaybee 	ds->ds_dir = NULL;
17666689Smaybee 	dsl_dataset_drain_refs(ds, tag);
17672199Sahrens 	VERIFY(0 == dmu_object_free(mos, obj, tx));
176810242Schris.kirby@sun.com 
176910242Schris.kirby@sun.com 	if (dsda->rm_origin) {
177010242Schris.kirby@sun.com 		/*
177110242Schris.kirby@sun.com 		 * Remove the origin of the clone we just destroyed.
177210242Schris.kirby@sun.com 		 */
177310242Schris.kirby@sun.com 		struct dsl_ds_destroyarg ndsda = {0};
177410242Schris.kirby@sun.com 
177510342Schris.kirby@sun.com 		ndsda.ds = dsda->rm_origin;
1776*12296SLin.Ling@Sun.COM 		dsl_dataset_destroy_sync(&ndsda, tag, tx);
177710242Schris.kirby@sun.com 	}
17782199Sahrens }
17792199Sahrens 
17805378Sck153898 static int
17815378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
17825378Sck153898 {
17835378Sck153898 	uint64_t asize;
17845378Sck153898 
17855378Sck153898 	if (!dmu_tx_is_syncing(tx))
17865378Sck153898 		return (0);
17875378Sck153898 
17885378Sck153898 	/*
17895378Sck153898 	 * If there's an fs-only reservation, any blocks that might become
17905378Sck153898 	 * owned by the snapshot dataset must be accommodated by space
17915378Sck153898 	 * outside of the reservation.
17925378Sck153898 	 */
1793*12296SLin.Ling@Sun.COM 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
1794*12296SLin.Ling@Sun.COM 	asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
17955378Sck153898 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE))
17965378Sck153898 		return (ENOSPC);
17975378Sck153898 
17985378Sck153898 	/*
17995378Sck153898 	 * Propogate any reserved space for this snapshot to other
18005378Sck153898 	 * snapshot checks in this sync group.
18015378Sck153898 	 */
18025378Sck153898 	if (asize > 0)
18035378Sck153898 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
18045378Sck153898 
18055378Sck153898 	return (0);
18065378Sck153898 }
18075378Sck153898 
18082199Sahrens int
18092199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
18102199Sahrens {
18115367Sahrens 	dsl_dataset_t *ds = arg1;
18122199Sahrens 	const char *snapname = arg2;
18132199Sahrens 	int err;
18142199Sahrens 	uint64_t value;
1815789Sahrens 
1816789Sahrens 	/*
18172199Sahrens 	 * We don't allow multiple snapshots of the same txg.  If there
18182199Sahrens 	 * is already one, try again.
18192199Sahrens 	 */
18202199Sahrens 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
18212199Sahrens 		return (EAGAIN);
18222199Sahrens 
18232199Sahrens 	/*
18242199Sahrens 	 * Check for conflicting name snapshot name.
1825789Sahrens 	 */
18266689Smaybee 	err = dsl_dataset_snap_lookup(ds, snapname, &value);
18272199Sahrens 	if (err == 0)
18282199Sahrens 		return (EEXIST);
18292199Sahrens 	if (err != ENOENT)
18302199Sahrens 		return (err);
1831789Sahrens 
18323978Smmusante 	/*
18333978Smmusante 	 * Check that the dataset's name is not too long.  Name consists
18343978Smmusante 	 * of the dataset's length + 1 for the @-sign + snapshot name's length
18353978Smmusante 	 */
18363978Smmusante 	if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
18373978Smmusante 		return (ENAMETOOLONG);
18383978Smmusante 
18395378Sck153898 	err = dsl_dataset_snapshot_reserve_space(ds, tx);
18405378Sck153898 	if (err)
18415378Sck153898 		return (err);
18425378Sck153898 
18432199Sahrens 	ds->ds_trysnap_txg = tx->tx_txg;
1844789Sahrens 	return (0);
1845789Sahrens }
1846789Sahrens 
18472199Sahrens void
1848*12296SLin.Ling@Sun.COM dsl_dataset_snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1849789Sahrens {
18505367Sahrens 	dsl_dataset_t *ds = arg1;
18512199Sahrens 	const char *snapname = arg2;
18522199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1853789Sahrens 	dmu_buf_t *dbuf;
1854789Sahrens 	dsl_dataset_phys_t *dsphys;
18557046Sahrens 	uint64_t dsobj, crtxg;
1856789Sahrens 	objset_t *mos = dp->dp_meta_objset;
1857789Sahrens 	int err;
1858789Sahrens 
18592199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1860789Sahrens 
18617046Sahrens 	/*
18627046Sahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
18637046Sahrens 	 */
18647046Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
18657046Sahrens 		crtxg = 1;
18667046Sahrens 	else
18677046Sahrens 		crtxg = tx->tx_txg;
18687046Sahrens 
1869928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1870928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
18711544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1872789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1873789Sahrens 	dsphys = dbuf->db_data;
18746689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
18752199Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1876789Sahrens 	dsphys->ds_fsid_guid = unique_create();
1877789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1878789Sahrens 	    sizeof (dsphys->ds_guid));
1879789Sahrens 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1880789Sahrens 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1881789Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1882789Sahrens 	dsphys->ds_num_children = 1;
1883789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
18847046Sahrens 	dsphys->ds_creation_txg = crtxg;
1885789Sahrens 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1886789Sahrens 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
1887789Sahrens 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1888789Sahrens 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
18892082Seschrock 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1890789Sahrens 	dsphys->ds_bp = ds->ds_phys->ds_bp;
18911544Seschrock 	dmu_buf_rele(dbuf, FTAG);
1892789Sahrens 
18932199Sahrens 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
18942199Sahrens 	if (ds->ds_prev) {
18957046Sahrens 		uint64_t next_clones_obj =
18967046Sahrens 		    ds->ds_prev->ds_phys->ds_next_clones_obj;
18972199Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1898789Sahrens 		    ds->ds_object ||
18992199Sahrens 		    ds->ds_prev->ds_phys->ds_num_children > 1);
19002199Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
19012199Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1902789Sahrens 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
19032199Sahrens 			    ds->ds_prev->ds_phys->ds_creation_txg);
19042199Sahrens 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
19057046Sahrens 		} else if (next_clones_obj != 0) {
190610801SMatthew.Ahrens@Sun.COM 			remove_from_next_clones(ds->ds_prev,
190710801SMatthew.Ahrens@Sun.COM 			    dsphys->ds_next_snap_obj, tx);
19087046Sahrens 			VERIFY3U(0, ==, zap_add_int(mos,
19097046Sahrens 			    next_clones_obj, dsobj, tx));
1910789Sahrens 		}
1911789Sahrens 	}
1912789Sahrens 
19135378Sck153898 	/*
19145378Sck153898 	 * If we have a reference-reservation on this dataset, we will
19155378Sck153898 	 * need to increase the amount of refreservation being charged
19165378Sck153898 	 * since our unique space is going to zero.
19175378Sck153898 	 */
19185378Sck153898 	if (ds->ds_reserved) {
1919*12296SLin.Ling@Sun.COM 		int64_t delta;
1920*12296SLin.Ling@Sun.COM 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1921*12296SLin.Ling@Sun.COM 		delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
19227390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1923*12296SLin.Ling@Sun.COM 		    delta, 0, 0, tx);
19245378Sck153898 	}
19255378Sck153898 
1926789Sahrens 	bplist_close(&ds->ds_deadlist);
1927789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
19285712Sahrens 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1929789Sahrens 	ds->ds_phys->ds_prev_snap_obj = dsobj;
19307046Sahrens 	ds->ds_phys->ds_prev_snap_txg = crtxg;
1931789Sahrens 	ds->ds_phys->ds_unique_bytes = 0;
19325378Sck153898 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
19335378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1934789Sahrens 	ds->ds_phys->ds_deadlist_obj =
1935789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
19361544Seschrock 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
19371544Seschrock 	    ds->ds_phys->ds_deadlist_obj));
1938789Sahrens 
1939789Sahrens 	dprintf("snap '%s' -> obj %llu\n", snapname, dsobj);
1940789Sahrens 	err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1941789Sahrens 	    snapname, 8, 1, &dsobj, tx);
1942789Sahrens 	ASSERT(err == 0);
1943789Sahrens 
1944789Sahrens 	if (ds->ds_prev)
19456689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
19466689Smaybee 	VERIFY(0 == dsl_dataset_get_ref(dp,
19476689Smaybee 	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
19484543Smarks 
1949*12296SLin.Ling@Sun.COM 	dsl_scan_ds_snapshotted(ds, tx);
19507046Sahrens 
195110373Schris.kirby@sun.com 	dsl_dir_snap_cmtime_update(ds->ds_dir);
195210373Schris.kirby@sun.com 
1953*12296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_SNAPSHOT, dp->dp_spa, tx,
19544603Sahrens 	    "dataset = %llu", dsobj);
1955789Sahrens }
1956789Sahrens 
1957789Sahrens void
19583547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1959789Sahrens {
1960789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
196110298SMatthew.Ahrens@Sun.COM 	ASSERT(ds->ds_objset != NULL);
1962789Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1963789Sahrens 
19644787Sahrens 	/*
19654787Sahrens 	 * in case we had to change ds_fsid_guid when we opened it,
19664787Sahrens 	 * sync it out now.
19674787Sahrens 	 */
19684787Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
19694787Sahrens 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
19704787Sahrens 
1971789Sahrens 	dsl_dir_dirty(ds->ds_dir, tx);
197210298SMatthew.Ahrens@Sun.COM 	dmu_objset_sync(ds->ds_objset, zio, tx);
1973789Sahrens }
1974789Sahrens 
1975789Sahrens void
19762885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1977789Sahrens {
19785378Sck153898 	uint64_t refd, avail, uobjs, aobjs;
19795378Sck153898 
19802885Sahrens 	dsl_dir_stats(ds->ds_dir, nv);
1981789Sahrens 
19825378Sck153898 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
19835378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
19845378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
19855378Sck153898 
19862885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
19872885Sahrens 	    ds->ds_phys->ds_creation_time);
19882885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
19892885Sahrens 	    ds->ds_phys->ds_creation_txg);
19905378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
19915378Sck153898 	    ds->ds_quota);
19925378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
19935378Sck153898 	    ds->ds_reserved);
19946643Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
19956643Seschrock 	    ds->ds_phys->ds_guid);
199610575SEric.Schrock@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1997*12296SLin.Ling@Sun.COM 	    ds->ds_phys->ds_unique_bytes);
199810575SEric.Schrock@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
199910575SEric.Schrock@Sun.COM 	    ds->ds_object);
200011022STom.Erickson@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
200111022STom.Erickson@Sun.COM 	    ds->ds_userrefs);
200210242Schris.kirby@sun.com 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
200310242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
2004789Sahrens 
2005789Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
2006789Sahrens 		/*
2007789Sahrens 		 * This is a snapshot; override the dd's space used with
20082885Sahrens 		 * our unique space and compression ratio.
2009789Sahrens 		 */
20102885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
20112885Sahrens 		    ds->ds_phys->ds_unique_bytes);
20122885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
20132885Sahrens 		    ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
20142885Sahrens 		    (ds->ds_phys->ds_uncompressed_bytes * 100 /
20152885Sahrens 		    ds->ds_phys->ds_compressed_bytes));
2016789Sahrens 	}
2017789Sahrens }
2018789Sahrens 
20192885Sahrens void
20202885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2021789Sahrens {
20222885Sahrens 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
20232885Sahrens 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
20245367Sahrens 	stat->dds_guid = ds->ds_phys->ds_guid;
20252885Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
20262885Sahrens 		stat->dds_is_snapshot = B_TRUE;
20272885Sahrens 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
20288228SEric.Taylor@Sun.COM 	} else {
20298228SEric.Taylor@Sun.COM 		stat->dds_is_snapshot = B_FALSE;
20308228SEric.Taylor@Sun.COM 		stat->dds_num_clones = 0;
20312885Sahrens 	}
20322885Sahrens 
20332885Sahrens 	/* clone origin is really a dsl_dir thing... */
20345446Sahrens 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
20357046Sahrens 	if (dsl_dir_is_clone(ds->ds_dir)) {
20362885Sahrens 		dsl_dataset_t *ods;
20372885Sahrens 
20386689Smaybee 		VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
20396689Smaybee 		    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
20405367Sahrens 		dsl_dataset_name(ods, stat->dds_origin);
20416689Smaybee 		dsl_dataset_drop_ref(ods, FTAG);
20428228SEric.Taylor@Sun.COM 	} else {
20438228SEric.Taylor@Sun.COM 		stat->dds_origin[0] = '\0';
20442885Sahrens 	}
20455446Sahrens 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
20462885Sahrens }
20472885Sahrens 
20482885Sahrens uint64_t
20492885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
20502885Sahrens {
20514787Sahrens 	return (ds->ds_fsid_guid);
20522885Sahrens }
20532885Sahrens 
20542885Sahrens void
20552885Sahrens dsl_dataset_space(dsl_dataset_t *ds,
20562885Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
20572885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
20582885Sahrens {
20592885Sahrens 	*refdbytesp = ds->ds_phys->ds_used_bytes;
20602885Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
20615378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
20625378Sck153898 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
20635378Sck153898 	if (ds->ds_quota != 0) {
20645378Sck153898 		/*
20655378Sck153898 		 * Adjust available bytes according to refquota
20665378Sck153898 		 */
20675378Sck153898 		if (*refdbytesp < ds->ds_quota)
20685378Sck153898 			*availbytesp = MIN(*availbytesp,
20695378Sck153898 			    ds->ds_quota - *refdbytesp);
20705378Sck153898 		else
20715378Sck153898 			*availbytesp = 0;
20725378Sck153898 	}
20732885Sahrens 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
20742885Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
2075789Sahrens }
2076789Sahrens 
20775326Sek110237 boolean_t
20785326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
20795326Sek110237 {
20805326Sek110237 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
20815326Sek110237 
20825326Sek110237 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
20835326Sek110237 	    dsl_pool_sync_context(dp));
20845326Sek110237 	if (ds->ds_prev == NULL)
20855326Sek110237 		return (B_FALSE);
20865326Sek110237 	if (ds->ds_phys->ds_bp.blk_birth >
20875326Sek110237 	    ds->ds_prev->ds_phys->ds_creation_txg)
20885326Sek110237 		return (B_TRUE);
20895326Sek110237 	return (B_FALSE);
20905326Sek110237 }
20915326Sek110237 
20922199Sahrens /* ARGSUSED */
2093789Sahrens static int
20942199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
2095789Sahrens {
20962199Sahrens 	dsl_dataset_t *ds = arg1;
20972199Sahrens 	char *newsnapname = arg2;
20982199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
20992199Sahrens 	dsl_dataset_t *hds;
21002199Sahrens 	uint64_t val;
2101789Sahrens 	int err;
2102789Sahrens 
21036689Smaybee 	err = dsl_dataset_hold_obj(dd->dd_pool,
21046689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds);
2105789Sahrens 	if (err)
2106789Sahrens 		return (err);
2107789Sahrens 
21082199Sahrens 	/* new name better not be in use */
21096689Smaybee 	err = dsl_dataset_snap_lookup(hds, newsnapname, &val);
21106689Smaybee 	dsl_dataset_rele(hds, FTAG);
2111789Sahrens 
21122199Sahrens 	if (err == 0)
21132199Sahrens 		err = EEXIST;
21142199Sahrens 	else if (err == ENOENT)
21152199Sahrens 		err = 0;
21164007Smmusante 
21174007Smmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
21184007Smmusante 	if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
21194007Smmusante 		err = ENAMETOOLONG;
21204007Smmusante 
21212199Sahrens 	return (err);
21222199Sahrens }
2123789Sahrens 
21242199Sahrens static void
2125*12296SLin.Ling@Sun.COM dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx)
21262199Sahrens {
21272199Sahrens 	dsl_dataset_t *ds = arg1;
21284543Smarks 	const char *newsnapname = arg2;
21292199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
21302199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
21312199Sahrens 	dsl_dataset_t *hds;
21322199Sahrens 	int err;
2133789Sahrens 
21342199Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2135789Sahrens 
21366689Smaybee 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
21376689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
2138789Sahrens 
21392199Sahrens 	VERIFY(0 == dsl_dataset_get_snapname(ds));
21406689Smaybee 	err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
2141789Sahrens 	ASSERT3U(err, ==, 0);
21422199Sahrens 	mutex_enter(&ds->ds_lock);
21432199Sahrens 	(void) strcpy(ds->ds_snapname, newsnapname);
21442199Sahrens 	mutex_exit(&ds->ds_lock);
21452199Sahrens 	err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
21462199Sahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2147789Sahrens 	ASSERT3U(err, ==, 0);
2148789Sahrens 
2149*12296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
2150*12296SLin.Ling@Sun.COM 	    "dataset = %llu", ds->ds_object);
21516689Smaybee 	dsl_dataset_rele(hds, FTAG);
2152789Sahrens }
2153789Sahrens 
21545326Sek110237 struct renamesnaparg {
21554007Smmusante 	dsl_sync_task_group_t *dstg;
21564007Smmusante 	char failed[MAXPATHLEN];
21574007Smmusante 	char *oldsnap;
21584007Smmusante 	char *newsnap;
21594007Smmusante };
21604007Smmusante 
21614007Smmusante static int
216211209SMatthew.Ahrens@Sun.COM dsl_snapshot_rename_one(const char *name, void *arg)
21634007Smmusante {
21645326Sek110237 	struct renamesnaparg *ra = arg;
21654007Smmusante 	dsl_dataset_t *ds = NULL;
216611209SMatthew.Ahrens@Sun.COM 	char *snapname;
21674007Smmusante 	int err;
21684007Smmusante 
216911209SMatthew.Ahrens@Sun.COM 	snapname = kmem_asprintf("%s@%s", name, ra->oldsnap);
217011209SMatthew.Ahrens@Sun.COM 	(void) strlcpy(ra->failed, snapname, sizeof (ra->failed));
21714543Smarks 
21724543Smarks 	/*
21734543Smarks 	 * For recursive snapshot renames the parent won't be changing
21744543Smarks 	 * so we just pass name for both the to/from argument.
21754543Smarks 	 */
217611209SMatthew.Ahrens@Sun.COM 	err = zfs_secpolicy_rename_perms(snapname, snapname, CRED());
217711209SMatthew.Ahrens@Sun.COM 	if (err != 0) {
217811209SMatthew.Ahrens@Sun.COM 		strfree(snapname);
217911209SMatthew.Ahrens@Sun.COM 		return (err == ENOENT ? 0 : err);
21804543Smarks 	}
21814543Smarks 
21826689Smaybee #ifdef _KERNEL
21836689Smaybee 	/*
21846689Smaybee 	 * For all filesystems undergoing rename, we'll need to unmount it.
21856689Smaybee 	 */
218611209SMatthew.Ahrens@Sun.COM 	(void) zfs_unmount_snap(snapname, NULL);
21876689Smaybee #endif
218811209SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(snapname, ra->dstg, &ds);
218911609SMatthew.Ahrens@Sun.COM 	strfree(snapname);
219011609SMatthew.Ahrens@Sun.COM 	if (err != 0)
219111209SMatthew.Ahrens@Sun.COM 		return (err == ENOENT ? 0 : err);
21924007Smmusante 
21934007Smmusante 	dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
21944007Smmusante 	    dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
21954007Smmusante 
21964007Smmusante 	return (0);
21974007Smmusante }
21984007Smmusante 
21994007Smmusante static int
22004007Smmusante dsl_recursive_rename(char *oldname, const char *newname)
22014007Smmusante {
22024007Smmusante 	int err;
22035326Sek110237 	struct renamesnaparg *ra;
22044007Smmusante 	dsl_sync_task_t *dst;
22054007Smmusante 	spa_t *spa;
22064007Smmusante 	char *cp, *fsname = spa_strdup(oldname);
220711209SMatthew.Ahrens@Sun.COM 	int len = strlen(oldname) + 1;
22084007Smmusante 
22094007Smmusante 	/* truncate the snapshot name to get the fsname */
22104007Smmusante 	cp = strchr(fsname, '@');
22114007Smmusante 	*cp = '\0';
22124007Smmusante 
22134603Sahrens 	err = spa_open(fsname, &spa, FTAG);
22144007Smmusante 	if (err) {
221511209SMatthew.Ahrens@Sun.COM 		kmem_free(fsname, len);
22164007Smmusante 		return (err);
22174007Smmusante 	}
22185326Sek110237 	ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP);
22194007Smmusante 	ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
22204007Smmusante 
22214007Smmusante 	ra->oldsnap = strchr(oldname, '@') + 1;
22224007Smmusante 	ra->newsnap = strchr(newname, '@') + 1;
22234007Smmusante 	*ra->failed = '\0';
22244007Smmusante 
22254007Smmusante 	err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
22264007Smmusante 	    DS_FIND_CHILDREN);
222711209SMatthew.Ahrens@Sun.COM 	kmem_free(fsname, len);
22284007Smmusante 
22294007Smmusante 	if (err == 0) {
22304007Smmusante 		err = dsl_sync_task_group_wait(ra->dstg);
22314007Smmusante 	}
22324007Smmusante 
22334007Smmusante 	for (dst = list_head(&ra->dstg->dstg_tasks); dst;
22344007Smmusante 	    dst = list_next(&ra->dstg->dstg_tasks, dst)) {
22354007Smmusante 		dsl_dataset_t *ds = dst->dst_arg1;
22364007Smmusante 		if (dst->dst_err) {
22374007Smmusante 			dsl_dir_name(ds->ds_dir, ra->failed);
223811209SMatthew.Ahrens@Sun.COM 			(void) strlcat(ra->failed, "@", sizeof (ra->failed));
223911209SMatthew.Ahrens@Sun.COM 			(void) strlcat(ra->failed, ra->newsnap,
224011209SMatthew.Ahrens@Sun.COM 			    sizeof (ra->failed));
22414007Smmusante 		}
22426689Smaybee 		dsl_dataset_rele(ds, ra->dstg);
22434007Smmusante 	}
22444007Smmusante 
22454543Smarks 	if (err)
224611209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(oldname, ra->failed, sizeof (ra->failed));
22474007Smmusante 
22484007Smmusante 	dsl_sync_task_group_destroy(ra->dstg);
22495326Sek110237 	kmem_free(ra, sizeof (struct renamesnaparg));
22504007Smmusante 	spa_close(spa, FTAG);
22514007Smmusante 	return (err);
22524007Smmusante }
22534007Smmusante 
22544569Smmusante static int
225511209SMatthew.Ahrens@Sun.COM dsl_valid_rename(const char *oldname, void *arg)
22564569Smmusante {
22574569Smmusante 	int delta = *(int *)arg;
22584569Smmusante 
22594569Smmusante 	if (strlen(oldname) + delta >= MAXNAMELEN)
22604569Smmusante 		return (ENAMETOOLONG);
22614569Smmusante 
22624569Smmusante 	return (0);
22634569Smmusante }
22644569Smmusante 
2265789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename
2266789Sahrens int
22676689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive)
2268789Sahrens {
2269789Sahrens 	dsl_dir_t *dd;
22702199Sahrens 	dsl_dataset_t *ds;
2271789Sahrens 	const char *tail;
2272789Sahrens 	int err;
2273789Sahrens 
22742199Sahrens 	err = dsl_dir_open(oldname, FTAG, &dd, &tail);
22751544Seschrock 	if (err)
22761544Seschrock 		return (err);
227711701SSanjeev.Bagewadi@Sun.COM 
2278789Sahrens 	if (tail == NULL) {
22794569Smmusante 		int delta = strlen(newname) - strlen(oldname);
22804569Smmusante 
22817046Sahrens 		/* if we're growing, validate child name lengths */
22824569Smmusante 		if (delta > 0)
22834569Smmusante 			err = dmu_objset_find(oldname, dsl_valid_rename,
22844569Smmusante 			    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
22854569Smmusante 
228611823SMatthew.Ahrens@Sun.COM 		if (err == 0)
22874569Smmusante 			err = dsl_dir_rename(dd, newname);
2288789Sahrens 		dsl_dir_close(dd, FTAG);
2289789Sahrens 		return (err);
2290789Sahrens 	}
229111701SSanjeev.Bagewadi@Sun.COM 
2292789Sahrens 	if (tail[0] != '@') {
229310588SEric.Taylor@Sun.COM 		/* the name ended in a nonexistent component */
2294789Sahrens 		dsl_dir_close(dd, FTAG);
2295789Sahrens 		return (ENOENT);
2296789Sahrens 	}
2297789Sahrens 
22982199Sahrens 	dsl_dir_close(dd, FTAG);
22992199Sahrens 
23002199Sahrens 	/* new name must be snapshot in same filesystem */
23012199Sahrens 	tail = strchr(newname, '@');
23022199Sahrens 	if (tail == NULL)
23032199Sahrens 		return (EINVAL);
23042199Sahrens 	tail++;
23052199Sahrens 	if (strncmp(oldname, newname, tail - newname) != 0)
23062199Sahrens 		return (EXDEV);
2307789Sahrens 
23084007Smmusante 	if (recursive) {
23094007Smmusante 		err = dsl_recursive_rename(oldname, newname);
23104007Smmusante 	} else {
23116689Smaybee 		err = dsl_dataset_hold(oldname, FTAG, &ds);
23124007Smmusante 		if (err)
23134007Smmusante 			return (err);
23142199Sahrens 
23154007Smmusante 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
23164007Smmusante 		    dsl_dataset_snapshot_rename_check,
23174007Smmusante 		    dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
23182199Sahrens 
23196689Smaybee 		dsl_dataset_rele(ds, FTAG);
23204007Smmusante 	}
23212199Sahrens 
2322789Sahrens 	return (err);
2323789Sahrens }
23242082Seschrock 
23257046Sahrens struct promotenode {
23266689Smaybee 	list_node_t link;
23276689Smaybee 	dsl_dataset_t *ds;
23286689Smaybee };
23296689Smaybee 
23302199Sahrens struct promotearg {
23317390SMatthew.Ahrens@Sun.COM 	list_t shared_snaps, origin_snaps, clone_snaps;
2332*12296SLin.Ling@Sun.COM 	dsl_dataset_t *origin_origin;
23337390SMatthew.Ahrens@Sun.COM 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
233410588SEric.Taylor@Sun.COM 	char *err_ds;
23352199Sahrens };
23362199Sahrens 
23377390SMatthew.Ahrens@Sun.COM static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
2338*12296SLin.Ling@Sun.COM static boolean_t snaplist_unstable(list_t *l);
2339*12296SLin.Ling@Sun.COM 
23402082Seschrock static int
23412199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
23422082Seschrock {
23432199Sahrens 	dsl_dataset_t *hds = arg1;
23442199Sahrens 	struct promotearg *pa = arg2;
23457390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap = list_head(&pa->shared_snaps);
23466689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
23476689Smaybee 	int err;
23482199Sahrens 
23497046Sahrens 	/* Check that it is a real clone */
23507046Sahrens 	if (!dsl_dir_is_clone(hds->ds_dir))
23512082Seschrock 		return (EINVAL);
23522082Seschrock 
23532199Sahrens 	/* Since this is so expensive, don't do the preliminary check */
23542199Sahrens 	if (!dmu_tx_is_syncing(tx))
23552199Sahrens 		return (0);
23562199Sahrens 
23576689Smaybee 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)
23586689Smaybee 		return (EXDEV);
23592082Seschrock 
23605367Sahrens 	/* compute origin's new unique space */
23617390SMatthew.Ahrens@Sun.COM 	snap = list_tail(&pa->clone_snaps);
23627390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
23637390SMatthew.Ahrens@Sun.COM 	err = bplist_space_birthrange(&snap->ds->ds_deadlist,
23647390SMatthew.Ahrens@Sun.COM 	    origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, &pa->unique);
23657390SMatthew.Ahrens@Sun.COM 	if (err)
23666689Smaybee 		return (err);
23676689Smaybee 
23686689Smaybee 	/*
23696689Smaybee 	 * Walk the snapshots that we are moving
23706689Smaybee 	 *
23717390SMatthew.Ahrens@Sun.COM 	 * Compute space to transfer.  Consider the incremental changes
23727390SMatthew.Ahrens@Sun.COM 	 * to used for each snapshot:
23737390SMatthew.Ahrens@Sun.COM 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
23747390SMatthew.Ahrens@Sun.COM 	 * So each snapshot gave birth to:
23757390SMatthew.Ahrens@Sun.COM 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
23766689Smaybee 	 * So a sequence would look like:
23777390SMatthew.Ahrens@Sun.COM 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
23786689Smaybee 	 * Which simplifies to:
23797390SMatthew.Ahrens@Sun.COM 	 * uN + kN + kN-1 + ... + k1 + k0
23806689Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
23817390SMatthew.Ahrens@Sun.COM 	 * uN + kN + kN-1 + ... + kM - uM-1
23826689Smaybee 	 */
23836689Smaybee 	pa->used = origin_ds->ds_phys->ds_used_bytes;
23846689Smaybee 	pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
23856689Smaybee 	pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
23867390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(&pa->shared_snaps); snap;
23877390SMatthew.Ahrens@Sun.COM 	    snap = list_next(&pa->shared_snaps, snap)) {
23882082Seschrock 		uint64_t val, dlused, dlcomp, dluncomp;
23896689Smaybee 		dsl_dataset_t *ds = snap->ds;
23902082Seschrock 
23912082Seschrock 		/* Check that the snapshot name does not conflict */
23927390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_get_snapname(ds));
23936689Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
239410588SEric.Taylor@Sun.COM 		if (err == 0) {
239510588SEric.Taylor@Sun.COM 			err = EEXIST;
239610588SEric.Taylor@Sun.COM 			goto out;
239710588SEric.Taylor@Sun.COM 		}
23986689Smaybee 		if (err != ENOENT)
239910588SEric.Taylor@Sun.COM 			goto out;
24006689Smaybee 
24016689Smaybee 		/* The very first snapshot does not have a deadlist */
24027390SMatthew.Ahrens@Sun.COM 		if (ds->ds_phys->ds_prev_snap_obj == 0)
24037390SMatthew.Ahrens@Sun.COM 			continue;
24047390SMatthew.Ahrens@Sun.COM 
24057390SMatthew.Ahrens@Sun.COM 		if (err = bplist_space(&ds->ds_deadlist,
24067390SMatthew.Ahrens@Sun.COM 		    &dlused, &dlcomp, &dluncomp))
240710588SEric.Taylor@Sun.COM 			goto out;
24087390SMatthew.Ahrens@Sun.COM 		pa->used += dlused;
24097390SMatthew.Ahrens@Sun.COM 		pa->comp += dlcomp;
24107390SMatthew.Ahrens@Sun.COM 		pa->uncomp += dluncomp;
24117390SMatthew.Ahrens@Sun.COM 	}
24126689Smaybee 
24136689Smaybee 	/*
24146689Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
24156689Smaybee 	 * so we need to subtract out the clone origin's used space.
24166689Smaybee 	 */
24177390SMatthew.Ahrens@Sun.COM 	if (pa->origin_origin) {
24187390SMatthew.Ahrens@Sun.COM 		pa->used -= pa->origin_origin->ds_phys->ds_used_bytes;
24197390SMatthew.Ahrens@Sun.COM 		pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes;
24207390SMatthew.Ahrens@Sun.COM 		pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes;
24212082Seschrock 	}
24222082Seschrock 
24232082Seschrock 	/* Check that there is enough space here */
24247390SMatthew.Ahrens@Sun.COM 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
24257390SMatthew.Ahrens@Sun.COM 	    pa->used);
24267390SMatthew.Ahrens@Sun.COM 	if (err)
24277390SMatthew.Ahrens@Sun.COM 		return (err);
24287390SMatthew.Ahrens@Sun.COM 
24297390SMatthew.Ahrens@Sun.COM 	/*
24307390SMatthew.Ahrens@Sun.COM 	 * Compute the amounts of space that will be used by snapshots
24317390SMatthew.Ahrens@Sun.COM 	 * after the promotion (for both origin and clone).  For each,
24327390SMatthew.Ahrens@Sun.COM 	 * it is the amount of space that will be on all of their
24337390SMatthew.Ahrens@Sun.COM 	 * deadlists (that was not born before their new origin).
24347390SMatthew.Ahrens@Sun.COM 	 */
24357390SMatthew.Ahrens@Sun.COM 	if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
24367390SMatthew.Ahrens@Sun.COM 		uint64_t space;
24377390SMatthew.Ahrens@Sun.COM 
24387390SMatthew.Ahrens@Sun.COM 		/*
24397390SMatthew.Ahrens@Sun.COM 		 * Note, typically this will not be a clone of a clone,
2440*12296SLin.Ling@Sun.COM 		 * so dd_origin_txg will be < TXG_INITIAL, so
24417390SMatthew.Ahrens@Sun.COM 		 * these snaplist_space() -> bplist_space_birthrange()
24427390SMatthew.Ahrens@Sun.COM 		 * calls will be fast because they do not have to
24437390SMatthew.Ahrens@Sun.COM 		 * iterate over all bps.
24447390SMatthew.Ahrens@Sun.COM 		 */
24457390SMatthew.Ahrens@Sun.COM 		snap = list_head(&pa->origin_snaps);
24467390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->shared_snaps,
2447*12296SLin.Ling@Sun.COM 		    snap->ds->ds_dir->dd_origin_txg, &pa->cloneusedsnap);
24487390SMatthew.Ahrens@Sun.COM 		if (err)
24497390SMatthew.Ahrens@Sun.COM 			return (err);
24507390SMatthew.Ahrens@Sun.COM 
24517390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->clone_snaps,
2452*12296SLin.Ling@Sun.COM 		    snap->ds->ds_dir->dd_origin_txg, &space);
24537390SMatthew.Ahrens@Sun.COM 		if (err)
24547390SMatthew.Ahrens@Sun.COM 			return (err);
24557390SMatthew.Ahrens@Sun.COM 		pa->cloneusedsnap += space;
24566689Smaybee 	}
24577390SMatthew.Ahrens@Sun.COM 	if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
24587390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->origin_snaps,
24597390SMatthew.Ahrens@Sun.COM 		    origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap);
24607390SMatthew.Ahrens@Sun.COM 		if (err)
24617390SMatthew.Ahrens@Sun.COM 			return (err);
24627390SMatthew.Ahrens@Sun.COM 	}
24637390SMatthew.Ahrens@Sun.COM 
24647390SMatthew.Ahrens@Sun.COM 	return (0);
246510588SEric.Taylor@Sun.COM out:
246610588SEric.Taylor@Sun.COM 	pa->err_ds =  snap->ds->ds_snapname;
246710588SEric.Taylor@Sun.COM 	return (err);
24682199Sahrens }
24692082Seschrock 
24702199Sahrens static void
2471*12296SLin.Ling@Sun.COM dsl_dataset_promote_sync(void *arg1, void *arg2, dmu_tx_t *tx)
24722199Sahrens {
24732199Sahrens 	dsl_dataset_t *hds = arg1;
24742199Sahrens 	struct promotearg *pa = arg2;
24757390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap = list_head(&pa->shared_snaps);
24766689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
24777390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *origin_head;
24782199Sahrens 	dsl_dir_t *dd = hds->ds_dir;
24792199Sahrens 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
24805367Sahrens 	dsl_dir_t *odd = NULL;
24817046Sahrens 	uint64_t oldnext_obj;
24827390SMatthew.Ahrens@Sun.COM 	int64_t delta;
24832199Sahrens 
24842199Sahrens 	ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
24852199Sahrens 
24867390SMatthew.Ahrens@Sun.COM 	snap = list_head(&pa->origin_snaps);
24877390SMatthew.Ahrens@Sun.COM 	origin_head = snap->ds;
24887390SMatthew.Ahrens@Sun.COM 
24892417Sahrens 	/*
24905367Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
24912417Sahrens 	 * changing.
24922417Sahrens 	 */
24935367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object,
24945367Sahrens 	    NULL, FTAG, &odd));
24952082Seschrock 
24966689Smaybee 	/* change origin's next snap */
24976689Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
24987046Sahrens 	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
24997390SMatthew.Ahrens@Sun.COM 	snap = list_tail(&pa->clone_snaps);
25007390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
25017390SMatthew.Ahrens@Sun.COM 	origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
25026689Smaybee 
25037046Sahrens 	/* change the origin's next clone */
25047046Sahrens 	if (origin_ds->ds_phys->ds_next_clones_obj) {
250510801SMatthew.Ahrens@Sun.COM 		remove_from_next_clones(origin_ds, snap->ds->ds_object, tx);
25067046Sahrens 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
25077046Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
25087046Sahrens 		    oldnext_obj, tx));
25097046Sahrens 	}
25107046Sahrens 
25116689Smaybee 	/* change origin */
25126689Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
25136689Smaybee 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
25146689Smaybee 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
2515*12296SLin.Ling@Sun.COM 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
25166689Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
25176689Smaybee 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
2518*12296SLin.Ling@Sun.COM 	origin_head->ds_dir->dd_origin_txg =
2519*12296SLin.Ling@Sun.COM 	    origin_ds->ds_phys->ds_creation_txg;
25206689Smaybee 
25212082Seschrock 	/* move snapshots to this dir */
25227390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(&pa->shared_snaps); snap;
25237390SMatthew.Ahrens@Sun.COM 	    snap = list_next(&pa->shared_snaps, snap)) {
25246689Smaybee 		dsl_dataset_t *ds = snap->ds;
25252082Seschrock 
25267237Sek110237 		/* unregister props as dsl_dir is changing */
252710298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset) {
252810298SMatthew.Ahrens@Sun.COM 			dmu_objset_evict(ds->ds_objset);
252910298SMatthew.Ahrens@Sun.COM 			ds->ds_objset = NULL;
25307237Sek110237 		}
25312082Seschrock 		/* move snap name entry */
25327390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_get_snapname(ds));
25337390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_snap_remove(origin_head,
25346689Smaybee 		    ds->ds_snapname, tx));
25352199Sahrens 		VERIFY(0 == zap_add(dp->dp_meta_objset,
25362082Seschrock 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
25372082Seschrock 		    8, 1, &ds->ds_object, tx));
25382082Seschrock 		/* change containing dsl_dir */
25392082Seschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
25405367Sahrens 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
25412082Seschrock 		ds->ds_phys->ds_dir_obj = dd->dd_object;
25425367Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
25432082Seschrock 		dsl_dir_close(ds->ds_dir, ds);
25442199Sahrens 		VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
25452082Seschrock 		    NULL, ds, &ds->ds_dir));
25462082Seschrock 
25472082Seschrock 		ASSERT3U(dsl_prop_numcb(ds), ==, 0);
25487390SMatthew.Ahrens@Sun.COM 	}
25497390SMatthew.Ahrens@Sun.COM 
25507390SMatthew.Ahrens@Sun.COM 	/*
25517390SMatthew.Ahrens@Sun.COM 	 * Change space accounting.
25527390SMatthew.Ahrens@Sun.COM 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
25537390SMatthew.Ahrens@Sun.COM 	 * both be valid, or both be 0 (resulting in delta == 0).  This
25547390SMatthew.Ahrens@Sun.COM 	 * is true for each of {clone,origin} independently.
25557390SMatthew.Ahrens@Sun.COM 	 */
25567390SMatthew.Ahrens@Sun.COM 
25577390SMatthew.Ahrens@Sun.COM 	delta = pa->cloneusedsnap -
25587390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
25597390SMatthew.Ahrens@Sun.COM 	ASSERT3S(delta, >=, 0);
25607390SMatthew.Ahrens@Sun.COM 	ASSERT3U(pa->used, >=, delta);
25617390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
25627390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
25637390SMatthew.Ahrens@Sun.COM 	    pa->used - delta, pa->comp, pa->uncomp, tx);
25647390SMatthew.Ahrens@Sun.COM 
25657390SMatthew.Ahrens@Sun.COM 	delta = pa->originusedsnap -
25667390SMatthew.Ahrens@Sun.COM 	    odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
25677390SMatthew.Ahrens@Sun.COM 	ASSERT3S(delta, <=, 0);
25687390SMatthew.Ahrens@Sun.COM 	ASSERT3U(pa->used, >=, -delta);
25697390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
25707390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
25717390SMatthew.Ahrens@Sun.COM 	    -pa->used - delta, -pa->comp, -pa->uncomp, tx);
25727390SMatthew.Ahrens@Sun.COM 
25735367Sahrens 	origin_ds->ds_phys->ds_unique_bytes = pa->unique;
25742082Seschrock 
25754543Smarks 	/* log history record */
2576*12296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
2577*12296SLin.Ling@Sun.COM 	    "dataset = %llu", hds->ds_object);
25784543Smarks 
25795367Sahrens 	dsl_dir_close(odd, FTAG);
25802082Seschrock }
25812082Seschrock 
25827390SMatthew.Ahrens@Sun.COM static char *snaplist_tag = "snaplist";
25837390SMatthew.Ahrens@Sun.COM /*
25847390SMatthew.Ahrens@Sun.COM  * Make a list of dsl_dataset_t's for the snapshots between first_obj
25857390SMatthew.Ahrens@Sun.COM  * (exclusive) and last_obj (inclusive).  The list will be in reverse
25867390SMatthew.Ahrens@Sun.COM  * order (last_obj will be the list_head()).  If first_obj == 0, do all
25877390SMatthew.Ahrens@Sun.COM  * snapshots back to this dataset's origin.
25887390SMatthew.Ahrens@Sun.COM  */
25897390SMatthew.Ahrens@Sun.COM static int
25907390SMatthew.Ahrens@Sun.COM snaplist_make(dsl_pool_t *dp, boolean_t own,
25917390SMatthew.Ahrens@Sun.COM     uint64_t first_obj, uint64_t last_obj, list_t *l)
25927390SMatthew.Ahrens@Sun.COM {
25937390SMatthew.Ahrens@Sun.COM 	uint64_t obj = last_obj;
25947390SMatthew.Ahrens@Sun.COM 
25957390SMatthew.Ahrens@Sun.COM 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock));
25967390SMatthew.Ahrens@Sun.COM 
25977390SMatthew.Ahrens@Sun.COM 	list_create(l, sizeof (struct promotenode),
25987390SMatthew.Ahrens@Sun.COM 	    offsetof(struct promotenode, link));
25997390SMatthew.Ahrens@Sun.COM 
26007390SMatthew.Ahrens@Sun.COM 	while (obj != first_obj) {
26017390SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
26027390SMatthew.Ahrens@Sun.COM 		struct promotenode *snap;
26037390SMatthew.Ahrens@Sun.COM 		int err;
26047390SMatthew.Ahrens@Sun.COM 
26057390SMatthew.Ahrens@Sun.COM 		if (own) {
26067390SMatthew.Ahrens@Sun.COM 			err = dsl_dataset_own_obj(dp, obj,
26077390SMatthew.Ahrens@Sun.COM 			    0, snaplist_tag, &ds);
26087390SMatthew.Ahrens@Sun.COM 			if (err == 0)
26097390SMatthew.Ahrens@Sun.COM 				dsl_dataset_make_exclusive(ds, snaplist_tag);
26107390SMatthew.Ahrens@Sun.COM 		} else {
26117390SMatthew.Ahrens@Sun.COM 			err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds);
26127390SMatthew.Ahrens@Sun.COM 		}
26137390SMatthew.Ahrens@Sun.COM 		if (err == ENOENT) {
26147390SMatthew.Ahrens@Sun.COM 			/* lost race with snapshot destroy */
26157390SMatthew.Ahrens@Sun.COM 			struct promotenode *last = list_tail(l);
26167390SMatthew.Ahrens@Sun.COM 			ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj);
26177390SMatthew.Ahrens@Sun.COM 			obj = last->ds->ds_phys->ds_prev_snap_obj;
26187390SMatthew.Ahrens@Sun.COM 			continue;
26197390SMatthew.Ahrens@Sun.COM 		} else if (err) {
26207390SMatthew.Ahrens@Sun.COM 			return (err);
26217390SMatthew.Ahrens@Sun.COM 		}
26227390SMatthew.Ahrens@Sun.COM 
26237390SMatthew.Ahrens@Sun.COM 		if (first_obj == 0)
26247390SMatthew.Ahrens@Sun.COM 			first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
26257390SMatthew.Ahrens@Sun.COM 
26267390SMatthew.Ahrens@Sun.COM 		snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP);
26277390SMatthew.Ahrens@Sun.COM 		snap->ds = ds;
26287390SMatthew.Ahrens@Sun.COM 		list_insert_tail(l, snap);
26297390SMatthew.Ahrens@Sun.COM 		obj = ds->ds_phys->ds_prev_snap_obj;
26307390SMatthew.Ahrens@Sun.COM 	}
26317390SMatthew.Ahrens@Sun.COM 
26327390SMatthew.Ahrens@Sun.COM 	return (0);
26337390SMatthew.Ahrens@Sun.COM }
26347390SMatthew.Ahrens@Sun.COM 
26357390SMatthew.Ahrens@Sun.COM static int
26367390SMatthew.Ahrens@Sun.COM snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
26377390SMatthew.Ahrens@Sun.COM {
26387390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap;
26397390SMatthew.Ahrens@Sun.COM 
26407390SMatthew.Ahrens@Sun.COM 	*spacep = 0;
26417390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
26427390SMatthew.Ahrens@Sun.COM 		uint64_t used;
26437390SMatthew.Ahrens@Sun.COM 		int err = bplist_space_birthrange(&snap->ds->ds_deadlist,
26447390SMatthew.Ahrens@Sun.COM 		    mintxg, UINT64_MAX, &used);
26457390SMatthew.Ahrens@Sun.COM 		if (err)
26467390SMatthew.Ahrens@Sun.COM 			return (err);
26477390SMatthew.Ahrens@Sun.COM 		*spacep += used;
26487390SMatthew.Ahrens@Sun.COM 	}
26497390SMatthew.Ahrens@Sun.COM 	return (0);
26507390SMatthew.Ahrens@Sun.COM }
26517390SMatthew.Ahrens@Sun.COM 
26527390SMatthew.Ahrens@Sun.COM static void
26537390SMatthew.Ahrens@Sun.COM snaplist_destroy(list_t *l, boolean_t own)
26547390SMatthew.Ahrens@Sun.COM {
26557390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap;
26567390SMatthew.Ahrens@Sun.COM 
26578779SMark.Musante@Sun.COM 	if (!l || !list_link_active(&l->list_head))
26587390SMatthew.Ahrens@Sun.COM 		return;
26597390SMatthew.Ahrens@Sun.COM 
26607390SMatthew.Ahrens@Sun.COM 	while ((snap = list_tail(l)) != NULL) {
26617390SMatthew.Ahrens@Sun.COM 		list_remove(l, snap);
26627390SMatthew.Ahrens@Sun.COM 		if (own)
26637390SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(snap->ds, snaplist_tag);
26647390SMatthew.Ahrens@Sun.COM 		else
26657390SMatthew.Ahrens@Sun.COM 			dsl_dataset_rele(snap->ds, snaplist_tag);
26667390SMatthew.Ahrens@Sun.COM 		kmem_free(snap, sizeof (struct promotenode));
26677390SMatthew.Ahrens@Sun.COM 	}
26687390SMatthew.Ahrens@Sun.COM 	list_destroy(l);
26697390SMatthew.Ahrens@Sun.COM }
26707390SMatthew.Ahrens@Sun.COM 
26717390SMatthew.Ahrens@Sun.COM /*
26727390SMatthew.Ahrens@Sun.COM  * Promote a clone.  Nomenclature note:
26737390SMatthew.Ahrens@Sun.COM  * "clone" or "cds": the original clone which is being promoted
26747390SMatthew.Ahrens@Sun.COM  * "origin" or "ods": the snapshot which is originally clone's origin
26757390SMatthew.Ahrens@Sun.COM  * "origin head" or "ohds": the dataset which is the head
26767390SMatthew.Ahrens@Sun.COM  * (filesystem/volume) for the origin
26777390SMatthew.Ahrens@Sun.COM  * "origin origin": the origin of the origin's filesystem (typically
26787390SMatthew.Ahrens@Sun.COM  * NULL, indicating that the clone is not a clone of a clone).
26797390SMatthew.Ahrens@Sun.COM  */
26802082Seschrock int
268110588SEric.Taylor@Sun.COM dsl_dataset_promote(const char *name, char *conflsnap)
26822082Seschrock {
26832082Seschrock 	dsl_dataset_t *ds;
26846689Smaybee 	dsl_dir_t *dd;
26856689Smaybee 	dsl_pool_t *dp;
26862082Seschrock 	dmu_object_info_t doi;
26877390SMatthew.Ahrens@Sun.COM 	struct promotearg pa = { 0 };
26887046Sahrens 	struct promotenode *snap;
26896689Smaybee 	int err;
26906689Smaybee 
26916689Smaybee 	err = dsl_dataset_hold(name, FTAG, &ds);
26922082Seschrock 	if (err)
26932082Seschrock 		return (err);
26946689Smaybee 	dd = ds->ds_dir;
26956689Smaybee 	dp = dd->dd_pool;
26966689Smaybee 
26976689Smaybee 	err = dmu_object_info(dp->dp_meta_objset,
26982082Seschrock 	    ds->ds_phys->ds_snapnames_zapobj, &doi);
26992082Seschrock 	if (err) {
27006689Smaybee 		dsl_dataset_rele(ds, FTAG);
27012082Seschrock 		return (err);
27022082Seschrock 	}
27032082Seschrock 
27047390SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) {
27057390SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
27067390SMatthew.Ahrens@Sun.COM 		return (EINVAL);
27077390SMatthew.Ahrens@Sun.COM 	}
27087390SMatthew.Ahrens@Sun.COM 
27092082Seschrock 	/*
27106689Smaybee 	 * We are going to inherit all the snapshots taken before our
27116689Smaybee 	 * origin (i.e., our new origin will be our parent's origin).
27126689Smaybee 	 * Take ownership of them so that we can rename them into our
27136689Smaybee 	 * namespace.
27146689Smaybee 	 */
27156689Smaybee 	rw_enter(&dp->dp_config_rwlock, RW_READER);
27167390SMatthew.Ahrens@Sun.COM 
27177390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj,
27187390SMatthew.Ahrens@Sun.COM 	    &pa.shared_snaps);
27197390SMatthew.Ahrens@Sun.COM 	if (err != 0)
27207390SMatthew.Ahrens@Sun.COM 		goto out;
27217390SMatthew.Ahrens@Sun.COM 
27227390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps);
27237390SMatthew.Ahrens@Sun.COM 	if (err != 0)
27247390SMatthew.Ahrens@Sun.COM 		goto out;
27257390SMatthew.Ahrens@Sun.COM 
27267390SMatthew.Ahrens@Sun.COM 	snap = list_head(&pa.shared_snaps);
27277390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
27287390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj,
27297390SMatthew.Ahrens@Sun.COM 	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps);
27307390SMatthew.Ahrens@Sun.COM 	if (err != 0)
27317390SMatthew.Ahrens@Sun.COM 		goto out;
27327390SMatthew.Ahrens@Sun.COM 
27337390SMatthew.Ahrens@Sun.COM 	if (dsl_dir_is_clone(snap->ds->ds_dir)) {
27347390SMatthew.Ahrens@Sun.COM 		err = dsl_dataset_own_obj(dp,
27357390SMatthew.Ahrens@Sun.COM 		    snap->ds->ds_dir->dd_phys->dd_origin_obj,
27367390SMatthew.Ahrens@Sun.COM 		    0, FTAG, &pa.origin_origin);
27377390SMatthew.Ahrens@Sun.COM 		if (err != 0)
27386689Smaybee 			goto out;
27396689Smaybee 	}
27407390SMatthew.Ahrens@Sun.COM 
27417390SMatthew.Ahrens@Sun.COM out:
27426689Smaybee 	rw_exit(&dp->dp_config_rwlock);
27436689Smaybee 
27446689Smaybee 	/*
27452082Seschrock 	 * Add in 128x the snapnames zapobj size, since we will be moving
27462082Seschrock 	 * a bunch of snapnames to the promoted ds, and dirtying their
27472082Seschrock 	 * bonus buffers.
27482082Seschrock 	 */
27497390SMatthew.Ahrens@Sun.COM 	if (err == 0) {
27507390SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_do(dp, dsl_dataset_promote_check,
27517390SMatthew.Ahrens@Sun.COM 		    dsl_dataset_promote_sync, ds, &pa,
275210922SJeff.Bonwick@Sun.COM 		    2 + 2 * doi.doi_physical_blocks_512);
275310588SEric.Taylor@Sun.COM 		if (err && pa.err_ds && conflsnap)
275410588SEric.Taylor@Sun.COM 			(void) strncpy(conflsnap, pa.err_ds, MAXNAMELEN);
27556689Smaybee 	}
27567390SMatthew.Ahrens@Sun.COM 
27577390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.shared_snaps, B_TRUE);
27587390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.clone_snaps, B_FALSE);
27597390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.origin_snaps, B_FALSE);
27607390SMatthew.Ahrens@Sun.COM 	if (pa.origin_origin)
27617390SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(pa.origin_origin, FTAG);
27626689Smaybee 	dsl_dataset_rele(ds, FTAG);
27632082Seschrock 	return (err);
27642082Seschrock }
27653912Slling 
27665367Sahrens struct cloneswaparg {
27675367Sahrens 	dsl_dataset_t *cds; /* clone dataset */
27685367Sahrens 	dsl_dataset_t *ohds; /* origin's head dataset */
27695367Sahrens 	boolean_t force;
27705481Sck153898 	int64_t unused_refres_delta; /* change in unconsumed refreservation */
27715367Sahrens };
27725326Sek110237 
27735326Sek110237 /* ARGSUSED */
27745326Sek110237 static int
27755326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx)
27765326Sek110237 {
27775367Sahrens 	struct cloneswaparg *csa = arg1;
27785326Sek110237 
27795367Sahrens 	/* they should both be heads */
27805367Sahrens 	if (dsl_dataset_is_snapshot(csa->cds) ||
27815367Sahrens 	    dsl_dataset_is_snapshot(csa->ohds))
27825326Sek110237 		return (EINVAL);
27835326Sek110237 
27845367Sahrens 	/* the branch point should be just before them */
27855367Sahrens 	if (csa->cds->ds_prev != csa->ohds->ds_prev)
27865326Sek110237 		return (EINVAL);
27875326Sek110237 
278810272SMatthew.Ahrens@Sun.COM 	/* cds should be the clone (unless they are unrelated) */
278910272SMatthew.Ahrens@Sun.COM 	if (csa->cds->ds_prev != NULL &&
279010272SMatthew.Ahrens@Sun.COM 	    csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap &&
279110272SMatthew.Ahrens@Sun.COM 	    csa->ohds->ds_object !=
279210272SMatthew.Ahrens@Sun.COM 	    csa->cds->ds_prev->ds_phys->ds_next_snap_obj)
27935367Sahrens 		return (EINVAL);
27945326Sek110237 
27955367Sahrens 	/* the clone should be a child of the origin */
27965367Sahrens 	if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir)
27975367Sahrens 		return (EINVAL);
27985326Sek110237 
27995367Sahrens 	/* ohds shouldn't be modified unless 'force' */
28005367Sahrens 	if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds))
28015367Sahrens 		return (ETXTBSY);
28025481Sck153898 
28035481Sck153898 	/* adjust amount of any unconsumed refreservation */
28045481Sck153898 	csa->unused_refres_delta =
28055481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
28065481Sck153898 	    csa->ohds->ds_phys->ds_unique_bytes) -
28075481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
28085481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
28095481Sck153898 
28105481Sck153898 	if (csa->unused_refres_delta > 0 &&
28115481Sck153898 	    csa->unused_refres_delta >
28125481Sck153898 	    dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE))
28135481Sck153898 		return (ENOSPC);
28145481Sck153898 
281510799SChris.Kirby@sun.com 	if (csa->ohds->ds_quota != 0 &&
281610799SChris.Kirby@sun.com 	    csa->cds->ds_phys->ds_unique_bytes > csa->ohds->ds_quota)
281710799SChris.Kirby@sun.com 		return (EDQUOT);
281810799SChris.Kirby@sun.com 
28195367Sahrens 	return (0);
28205326Sek110237 }
28215326Sek110237 
28225326Sek110237 /* ARGSUSED */
28235326Sek110237 static void
2824*12296SLin.Ling@Sun.COM dsl_dataset_clone_swap_sync(void *arg1, void *arg2, dmu_tx_t *tx)
28255326Sek110237 {
28265367Sahrens 	struct cloneswaparg *csa = arg1;
28275367Sahrens 	dsl_pool_t *dp = csa->cds->ds_dir->dd_pool;
28285326Sek110237 
28295481Sck153898 	ASSERT(csa->cds->ds_reserved == 0);
283010799SChris.Kirby@sun.com 	ASSERT(csa->ohds->ds_quota == 0 ||
283110799SChris.Kirby@sun.com 	    csa->cds->ds_phys->ds_unique_bytes <= csa->ohds->ds_quota);
28325481Sck153898 
28335367Sahrens 	dmu_buf_will_dirty(csa->cds->ds_dbuf, tx);
28345367Sahrens 	dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx);
28355326Sek110237 
283610298SMatthew.Ahrens@Sun.COM 	if (csa->cds->ds_objset != NULL) {
283710298SMatthew.Ahrens@Sun.COM 		dmu_objset_evict(csa->cds->ds_objset);
283810298SMatthew.Ahrens@Sun.COM 		csa->cds->ds_objset = NULL;
28395367Sahrens 	}
28405326Sek110237 
284110298SMatthew.Ahrens@Sun.COM 	if (csa->ohds->ds_objset != NULL) {
284210298SMatthew.Ahrens@Sun.COM 		dmu_objset_evict(csa->ohds->ds_objset);
284310298SMatthew.Ahrens@Sun.COM 		csa->ohds->ds_objset = NULL;
28445367Sahrens 	}
28455326Sek110237 
284610272SMatthew.Ahrens@Sun.COM 	/*
284710272SMatthew.Ahrens@Sun.COM 	 * Reset origin's unique bytes, if it exists.
284810272SMatthew.Ahrens@Sun.COM 	 */
284910272SMatthew.Ahrens@Sun.COM 	if (csa->cds->ds_prev) {
285010272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *origin = csa->cds->ds_prev;
285110272SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
285210272SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist,
285310272SMatthew.Ahrens@Sun.COM 		    origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
285410272SMatthew.Ahrens@Sun.COM 		    &origin->ds_phys->ds_unique_bytes));
285510272SMatthew.Ahrens@Sun.COM 	}
28565326Sek110237 
28575326Sek110237 	/* swap blkptrs */
28585326Sek110237 	{
28595326Sek110237 		blkptr_t tmp;
28605367Sahrens 		tmp = csa->ohds->ds_phys->ds_bp;
28615367Sahrens 		csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp;
28625367Sahrens 		csa->cds->ds_phys->ds_bp = tmp;
28635326Sek110237 	}
28645326Sek110237 
28655326Sek110237 	/* set dd_*_bytes */
28665326Sek110237 	{
28675326Sek110237 		int64_t dused, dcomp, duncomp;
28685326Sek110237 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
28695326Sek110237 		uint64_t odl_used, odl_comp, odl_uncomp;
28705326Sek110237 
28717390SMatthew.Ahrens@Sun.COM 		ASSERT3U(csa->cds->ds_dir->dd_phys->
28727390SMatthew.Ahrens@Sun.COM 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
28737390SMatthew.Ahrens@Sun.COM 
28745367Sahrens 		VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used,
28755326Sek110237 		    &cdl_comp, &cdl_uncomp));
28765367Sahrens 		VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used,
28775326Sek110237 		    &odl_comp, &odl_uncomp));
28787390SMatthew.Ahrens@Sun.COM 
28795367Sahrens 		dused = csa->cds->ds_phys->ds_used_bytes + cdl_used -
28805367Sahrens 		    (csa->ohds->ds_phys->ds_used_bytes + odl_used);
28815367Sahrens 		dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
28825367Sahrens 		    (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
28835367Sahrens 		duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
28845367Sahrens 		    cdl_uncomp -
28855367Sahrens 		    (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
28865326Sek110237 
28877390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD,
28885367Sahrens 		    dused, dcomp, duncomp, tx);
28897390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD,
28905367Sahrens 		    -dused, -dcomp, -duncomp, tx);
28917390SMatthew.Ahrens@Sun.COM 
28927390SMatthew.Ahrens@Sun.COM 		/*
28937390SMatthew.Ahrens@Sun.COM 		 * The difference in the space used by snapshots is the
28947390SMatthew.Ahrens@Sun.COM 		 * difference in snapshot space due to the head's
28957390SMatthew.Ahrens@Sun.COM 		 * deadlist (since that's the only thing that's
28967390SMatthew.Ahrens@Sun.COM 		 * changing that affects the snapused).
28977390SMatthew.Ahrens@Sun.COM 		 */
28987390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist,
2899*12296SLin.Ling@Sun.COM 		    csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX, &cdl_used));
29007390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->ohds->ds_deadlist,
2901*12296SLin.Ling@Sun.COM 		    csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX, &odl_used));
29027390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used,
29037390SMatthew.Ahrens@Sun.COM 		    DD_USED_HEAD, DD_USED_SNAP, tx);
29045367Sahrens 	}
29055367Sahrens 
29065367Sahrens #define	SWITCH64(x, y) \
29075367Sahrens 	{ \
29085367Sahrens 		uint64_t __tmp = (x); \
29095367Sahrens 		(x) = (y); \
29105367Sahrens 		(y) = __tmp; \
29115326Sek110237 	}
29125326Sek110237 
29135326Sek110237 	/* swap ds_*_bytes */
29145367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_used_bytes,
29155367Sahrens 	    csa->cds->ds_phys->ds_used_bytes);
29165367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
29175367Sahrens 	    csa->cds->ds_phys->ds_compressed_bytes);
29185367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
29195367Sahrens 	    csa->cds->ds_phys->ds_uncompressed_bytes);
29205481Sck153898 	SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
29215481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
29225481Sck153898 
29235481Sck153898 	/* apply any parent delta for change in unconsumed refreservation */
29247390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV,
29257390SMatthew.Ahrens@Sun.COM 	    csa->unused_refres_delta, 0, 0, tx);
29265326Sek110237 
29275326Sek110237 	/* swap deadlists */
29285367Sahrens 	bplist_close(&csa->cds->ds_deadlist);
29295367Sahrens 	bplist_close(&csa->ohds->ds_deadlist);
29305367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
29315367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj);
29325367Sahrens 	VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
29335367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj));
29345367Sahrens 	VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
29355367Sahrens 	    csa->ohds->ds_phys->ds_deadlist_obj));
29367837SMatthew.Ahrens@Sun.COM 
2937*12296SLin.Ling@Sun.COM 	dsl_scan_ds_clone_swapped(csa->ohds, csa->cds, tx);
29385326Sek110237 }
29395326Sek110237 
29405326Sek110237 /*
294110272SMatthew.Ahrens@Sun.COM  * Swap 'clone' with its origin head datasets.  Used at the end of "zfs
294210272SMatthew.Ahrens@Sun.COM  * recv" into an existing fs to swizzle the file system to the new
294310272SMatthew.Ahrens@Sun.COM  * version, and by "zfs rollback".  Can also be used to swap two
294410272SMatthew.Ahrens@Sun.COM  * independent head datasets if neither has any snapshots.
29455326Sek110237  */
29465326Sek110237 int
29475367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
29485367Sahrens     boolean_t force)
29495326Sek110237 {
29505367Sahrens 	struct cloneswaparg csa;
29516689Smaybee 	int error;
29526689Smaybee 
29536689Smaybee 	ASSERT(clone->ds_owner);
29546689Smaybee 	ASSERT(origin_head->ds_owner);
29556689Smaybee retry:
29566689Smaybee 	/* Need exclusive access for the swap */
29576689Smaybee 	rw_enter(&clone->ds_rwlock, RW_WRITER);
29586689Smaybee 	if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) {
29596689Smaybee 		rw_exit(&clone->ds_rwlock);
29606689Smaybee 		rw_enter(&origin_head->ds_rwlock, RW_WRITER);
29616689Smaybee 		if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) {
29626689Smaybee 			rw_exit(&origin_head->ds_rwlock);
29636689Smaybee 			goto retry;
29646689Smaybee 		}
29656689Smaybee 	}
29665367Sahrens 	csa.cds = clone;
29675367Sahrens 	csa.ohds = origin_head;
29685367Sahrens 	csa.force = force;
29696689Smaybee 	error = dsl_sync_task_do(clone->ds_dir->dd_pool,
29705326Sek110237 	    dsl_dataset_clone_swap_check,
29716689Smaybee 	    dsl_dataset_clone_swap_sync, &csa, NULL, 9);
29726689Smaybee 	return (error);
29735326Sek110237 }
29745326Sek110237 
29753912Slling /*
29763912Slling  * Given a pool name and a dataset object number in that pool,
29773912Slling  * return the name of that dataset.
29783912Slling  */
29793912Slling int
29803912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
29813912Slling {
29823912Slling 	spa_t *spa;
29833912Slling 	dsl_pool_t *dp;
29846689Smaybee 	dsl_dataset_t *ds;
29853912Slling 	int error;
29863912Slling 
29873912Slling 	if ((error = spa_open(pname, &spa, FTAG)) != 0)
29883912Slling 		return (error);
29893912Slling 	dp = spa_get_dsl(spa);
29903912Slling 	rw_enter(&dp->dp_config_rwlock, RW_READER);
29916689Smaybee 	if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) {
29926689Smaybee 		dsl_dataset_name(ds, buf);
29936689Smaybee 		dsl_dataset_rele(ds, FTAG);
29943912Slling 	}
29953912Slling 	rw_exit(&dp->dp_config_rwlock);
29963912Slling 	spa_close(spa, FTAG);
29973912Slling 
29986689Smaybee 	return (error);
29993912Slling }
30005378Sck153898 
30015378Sck153898 int
30025378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
30036689Smaybee     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
30045378Sck153898 {
30055378Sck153898 	int error = 0;
30065378Sck153898 
30075378Sck153898 	ASSERT3S(asize, >, 0);
30085378Sck153898 
30095831Sck153898 	/*
30105831Sck153898 	 * *ref_rsrv is the portion of asize that will come from any
30115831Sck153898 	 * unconsumed refreservation space.
30125831Sck153898 	 */
30135831Sck153898 	*ref_rsrv = 0;
30145831Sck153898 
30155378Sck153898 	mutex_enter(&ds->ds_lock);
30165378Sck153898 	/*
30175378Sck153898 	 * Make a space adjustment for reserved bytes.
30185378Sck153898 	 */
30195378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
30205378Sck153898 		ASSERT3U(*used, >=,
30215378Sck153898 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
30225378Sck153898 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
30235831Sck153898 		*ref_rsrv =
30245831Sck153898 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
30255378Sck153898 	}
30265378Sck153898 
30275378Sck153898 	if (!check_quota || ds->ds_quota == 0) {
30285378Sck153898 		mutex_exit(&ds->ds_lock);
30295378Sck153898 		return (0);
30305378Sck153898 	}
30315378Sck153898 	/*
30325378Sck153898 	 * If they are requesting more space, and our current estimate
30335378Sck153898 	 * is over quota, they get to try again unless the actual
30345378Sck153898 	 * on-disk is over quota and there are no pending changes (which
30355378Sck153898 	 * may free up space for us).
30365378Sck153898 	 */
30375378Sck153898 	if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) {
30385378Sck153898 		if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota)
30395378Sck153898 			error = ERESTART;
30405378Sck153898 		else
30415378Sck153898 			error = EDQUOT;
30425378Sck153898 	}
30435378Sck153898 	mutex_exit(&ds->ds_lock);
30445378Sck153898 
30455378Sck153898 	return (error);
30465378Sck153898 }
30475378Sck153898 
30485378Sck153898 /* ARGSUSED */
30495378Sck153898 static int
30505378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
30515378Sck153898 {
30525378Sck153898 	dsl_dataset_t *ds = arg1;
305311022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
305411022STom.Erickson@Sun.COM 	int err;
30555378Sck153898 
30565378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA)
30575378Sck153898 		return (ENOTSUP);
30585378Sck153898 
305911022STom.Erickson@Sun.COM 	if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
306011022STom.Erickson@Sun.COM 		return (err);
306111022STom.Erickson@Sun.COM 
306211022STom.Erickson@Sun.COM 	if (psa->psa_effective_value == 0)
30635378Sck153898 		return (0);
30645378Sck153898 
306511022STom.Erickson@Sun.COM 	if (psa->psa_effective_value < ds->ds_phys->ds_used_bytes ||
306611022STom.Erickson@Sun.COM 	    psa->psa_effective_value < ds->ds_reserved)
30675378Sck153898 		return (ENOSPC);
30685378Sck153898 
30695378Sck153898 	return (0);
30705378Sck153898 }
30715378Sck153898 
3072*12296SLin.Ling@Sun.COM extern void dsl_prop_set_sync(void *, void *, dmu_tx_t *);
307311022STom.Erickson@Sun.COM 
30745378Sck153898 void
3075*12296SLin.Ling@Sun.COM dsl_dataset_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
30765378Sck153898 {
30775378Sck153898 	dsl_dataset_t *ds = arg1;
307811022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
307911022STom.Erickson@Sun.COM 	uint64_t effective_value = psa->psa_effective_value;
308011022STom.Erickson@Sun.COM 
3081*12296SLin.Ling@Sun.COM 	dsl_prop_set_sync(ds, psa, tx);
308211022STom.Erickson@Sun.COM 	DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
308311022STom.Erickson@Sun.COM 
308411022STom.Erickson@Sun.COM 	if (ds->ds_quota != effective_value) {
308511022STom.Erickson@Sun.COM 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
308611022STom.Erickson@Sun.COM 		ds->ds_quota = effective_value;
308711022STom.Erickson@Sun.COM 
3088*12296SLin.Ling@Sun.COM 		spa_history_log_internal(LOG_DS_REFQUOTA,
3089*12296SLin.Ling@Sun.COM 		    ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu ",
309011022STom.Erickson@Sun.COM 		    (longlong_t)ds->ds_quota, ds->ds_object);
309111022STom.Erickson@Sun.COM 	}
30925378Sck153898 }
30935378Sck153898 
30945378Sck153898 int
309511022STom.Erickson@Sun.COM dsl_dataset_set_quota(const char *dsname, zprop_source_t source, uint64_t quota)
30965378Sck153898 {
30975378Sck153898 	dsl_dataset_t *ds;
309811022STom.Erickson@Sun.COM 	dsl_prop_setarg_t psa;
30995378Sck153898 	int err;
31005378Sck153898 
310111022STom.Erickson@Sun.COM 	dsl_prop_setarg_init_uint64(&psa, "refquota", source, &quota);
310211022STom.Erickson@Sun.COM 
31036689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
31045378Sck153898 	if (err)
31055378Sck153898 		return (err);
31065378Sck153898 
310711022STom.Erickson@Sun.COM 	/*
310811022STom.Erickson@Sun.COM 	 * If someone removes a file, then tries to set the quota, we
310911022STom.Erickson@Sun.COM 	 * want to make sure the file freeing takes effect.
311011022STom.Erickson@Sun.COM 	 */
311111022STom.Erickson@Sun.COM 	txg_wait_open(ds->ds_dir->dd_pool, 0);
311211022STom.Erickson@Sun.COM 
311311022STom.Erickson@Sun.COM 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
311411022STom.Erickson@Sun.COM 	    dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync,
311511022STom.Erickson@Sun.COM 	    ds, &psa, 0);
311611022STom.Erickson@Sun.COM 
31176689Smaybee 	dsl_dataset_rele(ds, FTAG);
31185378Sck153898 	return (err);
31195378Sck153898 }
31205378Sck153898 
31215378Sck153898 static int
31225378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
31235378Sck153898 {
31245378Sck153898 	dsl_dataset_t *ds = arg1;
312511022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
312611022STom.Erickson@Sun.COM 	uint64_t effective_value;
31275378Sck153898 	uint64_t unique;
312811022STom.Erickson@Sun.COM 	int err;
31295378Sck153898 
31305378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
31315378Sck153898 	    SPA_VERSION_REFRESERVATION)
31325378Sck153898 		return (ENOTSUP);
31335378Sck153898 
31345378Sck153898 	if (dsl_dataset_is_snapshot(ds))
31355378Sck153898 		return (EINVAL);
31365378Sck153898 
313711022STom.Erickson@Sun.COM 	if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
313811022STom.Erickson@Sun.COM 		return (err);
313911022STom.Erickson@Sun.COM 
314011022STom.Erickson@Sun.COM 	effective_value = psa->psa_effective_value;
314111022STom.Erickson@Sun.COM 
31425378Sck153898 	/*
31435378Sck153898 	 * If we are doing the preliminary check in open context, the
31445378Sck153898 	 * space estimates may be inaccurate.
31455378Sck153898 	 */
31465378Sck153898 	if (!dmu_tx_is_syncing(tx))
31475378Sck153898 		return (0);
31485378Sck153898 
31495378Sck153898 	mutex_enter(&ds->ds_lock);
3150*12296SLin.Ling@Sun.COM 	if (!DS_UNIQUE_IS_ACCURATE(ds))
3151*12296SLin.Ling@Sun.COM 		dsl_dataset_recalc_head_uniq(ds);
3152*12296SLin.Ling@Sun.COM 	unique = ds->ds_phys->ds_unique_bytes;
31535378Sck153898 	mutex_exit(&ds->ds_lock);
31545378Sck153898 
315511022STom.Erickson@Sun.COM 	if (MAX(unique, effective_value) > MAX(unique, ds->ds_reserved)) {
315611022STom.Erickson@Sun.COM 		uint64_t delta = MAX(unique, effective_value) -
31578525SEric.Schrock@Sun.COM 		    MAX(unique, ds->ds_reserved);
31588525SEric.Schrock@Sun.COM 
31598525SEric.Schrock@Sun.COM 		if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
31608525SEric.Schrock@Sun.COM 			return (ENOSPC);
31618525SEric.Schrock@Sun.COM 		if (ds->ds_quota > 0 &&
316211022STom.Erickson@Sun.COM 		    effective_value > ds->ds_quota)
31638525SEric.Schrock@Sun.COM 			return (ENOSPC);
31648525SEric.Schrock@Sun.COM 	}
31655378Sck153898 
31665378Sck153898 	return (0);
31675378Sck153898 }
31685378Sck153898 
31695378Sck153898 static void
3170*12296SLin.Ling@Sun.COM dsl_dataset_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx)
31715378Sck153898 {
31725378Sck153898 	dsl_dataset_t *ds = arg1;
317311022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
317411022STom.Erickson@Sun.COM 	uint64_t effective_value = psa->psa_effective_value;
31757595SMatthew.Ahrens@Sun.COM 	uint64_t unique;
31767595SMatthew.Ahrens@Sun.COM 	int64_t delta;
31777595SMatthew.Ahrens@Sun.COM 
3178*12296SLin.Ling@Sun.COM 	dsl_prop_set_sync(ds, psa, tx);
317911022STom.Erickson@Sun.COM 	DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
318011022STom.Erickson@Sun.COM 
31817595SMatthew.Ahrens@Sun.COM 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
31827595SMatthew.Ahrens@Sun.COM 
31837595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_dir->dd_lock);
31847595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_lock);
3185*12296SLin.Ling@Sun.COM 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3186*12296SLin.Ling@Sun.COM 	unique = ds->ds_phys->ds_unique_bytes;
318711022STom.Erickson@Sun.COM 	delta = MAX(0, (int64_t)(effective_value - unique)) -
31887595SMatthew.Ahrens@Sun.COM 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
318911022STom.Erickson@Sun.COM 	ds->ds_reserved = effective_value;
31907595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_lock);
31917595SMatthew.Ahrens@Sun.COM 
31927595SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
31937595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_dir->dd_lock);
31947595SMatthew.Ahrens@Sun.COM 
3195*12296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_REFRESERV,
3196*12296SLin.Ling@Sun.COM 	    ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu",
319711022STom.Erickson@Sun.COM 	    (longlong_t)effective_value, ds->ds_object);
31985378Sck153898 }
31995378Sck153898 
32005378Sck153898 int
320111022STom.Erickson@Sun.COM dsl_dataset_set_reservation(const char *dsname, zprop_source_t source,
320211022STom.Erickson@Sun.COM     uint64_t reservation)
32035378Sck153898 {
32045378Sck153898 	dsl_dataset_t *ds;
320511022STom.Erickson@Sun.COM 	dsl_prop_setarg_t psa;
32065378Sck153898 	int err;
32075378Sck153898 
320811022STom.Erickson@Sun.COM 	dsl_prop_setarg_init_uint64(&psa, "refreservation", source,
320911022STom.Erickson@Sun.COM 	    &reservation);
321011022STom.Erickson@Sun.COM 
32116689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
32125378Sck153898 	if (err)
32135378Sck153898 		return (err);
32145378Sck153898 
32155378Sck153898 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
32165378Sck153898 	    dsl_dataset_set_reservation_check,
321711022STom.Erickson@Sun.COM 	    dsl_dataset_set_reservation_sync, ds, &psa, 0);
321811022STom.Erickson@Sun.COM 
32196689Smaybee 	dsl_dataset_rele(ds, FTAG);
32205378Sck153898 	return (err);
32215378Sck153898 }
322210242Schris.kirby@sun.com 
322310342Schris.kirby@sun.com struct dsl_ds_holdarg {
322410342Schris.kirby@sun.com 	dsl_sync_task_group_t *dstg;
322510342Schris.kirby@sun.com 	char *htag;
322610342Schris.kirby@sun.com 	char *snapname;
322710342Schris.kirby@sun.com 	boolean_t recursive;
322810342Schris.kirby@sun.com 	boolean_t gotone;
322910342Schris.kirby@sun.com 	boolean_t temphold;
323010342Schris.kirby@sun.com 	char failed[MAXPATHLEN];
323110342Schris.kirby@sun.com };
323210342Schris.kirby@sun.com 
323310951SChris.Kirby@sun.com /*
323410951SChris.Kirby@sun.com  * The max length of a temporary tag prefix is the number of hex digits
323510951SChris.Kirby@sun.com  * required to express UINT64_MAX plus one for the hyphen.
323610951SChris.Kirby@sun.com  */
323710951SChris.Kirby@sun.com #define	MAX_TAG_PREFIX_LEN	17
323810951SChris.Kirby@sun.com 
323910242Schris.kirby@sun.com static int
324010242Schris.kirby@sun.com dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx)
324110242Schris.kirby@sun.com {
324210242Schris.kirby@sun.com 	dsl_dataset_t *ds = arg1;
324310342Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg2;
324410342Schris.kirby@sun.com 	char *htag = ha->htag;
324510242Schris.kirby@sun.com 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
324610242Schris.kirby@sun.com 	int error = 0;
324710242Schris.kirby@sun.com 
324810242Schris.kirby@sun.com 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
324910242Schris.kirby@sun.com 		return (ENOTSUP);
325010242Schris.kirby@sun.com 
325110242Schris.kirby@sun.com 	if (!dsl_dataset_is_snapshot(ds))
325210242Schris.kirby@sun.com 		return (EINVAL);
325310242Schris.kirby@sun.com 
325410242Schris.kirby@sun.com 	/* tags must be unique */
325510242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
325610242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj) {
325710242Schris.kirby@sun.com 		error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag,
325810242Schris.kirby@sun.com 		    8, 1, tx);
325910242Schris.kirby@sun.com 		if (error == 0)
326010242Schris.kirby@sun.com 			error = EEXIST;
326110242Schris.kirby@sun.com 		else if (error == ENOENT)
326210242Schris.kirby@sun.com 			error = 0;
326310242Schris.kirby@sun.com 	}
326410242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
326510242Schris.kirby@sun.com 
326610951SChris.Kirby@sun.com 	if (error == 0 && ha->temphold &&
326710951SChris.Kirby@sun.com 	    strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
326810951SChris.Kirby@sun.com 		error = E2BIG;
326910951SChris.Kirby@sun.com 
327010242Schris.kirby@sun.com 	return (error);
327110242Schris.kirby@sun.com }
327210242Schris.kirby@sun.com 
327310242Schris.kirby@sun.com static void
3274*12296SLin.Ling@Sun.COM dsl_dataset_user_hold_sync(void *arg1, void *arg2, dmu_tx_t *tx)
327510242Schris.kirby@sun.com {
327610242Schris.kirby@sun.com 	dsl_dataset_t *ds = arg1;
327710342Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg2;
327810342Schris.kirby@sun.com 	char *htag = ha->htag;
327910342Schris.kirby@sun.com 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
328010342Schris.kirby@sun.com 	objset_t *mos = dp->dp_meta_objset;
328110951SChris.Kirby@sun.com 	uint64_t now = gethrestime_sec();
328210242Schris.kirby@sun.com 	uint64_t zapobj;
328310242Schris.kirby@sun.com 
328410242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
328510242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj == 0) {
328610242Schris.kirby@sun.com 		/*
328710242Schris.kirby@sun.com 		 * This is the first user hold for this dataset.  Create
328810242Schris.kirby@sun.com 		 * the userrefs zap object.
328910242Schris.kirby@sun.com 		 */
329010242Schris.kirby@sun.com 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
329110242Schris.kirby@sun.com 		zapobj = ds->ds_phys->ds_userrefs_obj =
329210242Schris.kirby@sun.com 		    zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx);
329310242Schris.kirby@sun.com 	} else {
329410242Schris.kirby@sun.com 		zapobj = ds->ds_phys->ds_userrefs_obj;
329510242Schris.kirby@sun.com 	}
329610242Schris.kirby@sun.com 	ds->ds_userrefs++;
329710242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
329810242Schris.kirby@sun.com 
329910242Schris.kirby@sun.com 	VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx));
330010242Schris.kirby@sun.com 
330110342Schris.kirby@sun.com 	if (ha->temphold) {
330210342Schris.kirby@sun.com 		VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object,
330310342Schris.kirby@sun.com 		    htag, &now, tx));
330410342Schris.kirby@sun.com 	}
330510342Schris.kirby@sun.com 
3306*12296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_USER_HOLD,
3307*12296SLin.Ling@Sun.COM 	    dp->dp_spa, tx, "<%s> temp = %d dataset = %llu", htag,
330810342Schris.kirby@sun.com 	    (int)ha->temphold, ds->ds_object);
330910242Schris.kirby@sun.com }
331010242Schris.kirby@sun.com 
331110242Schris.kirby@sun.com static int
331211209SMatthew.Ahrens@Sun.COM dsl_dataset_user_hold_one(const char *dsname, void *arg)
331310242Schris.kirby@sun.com {
331410242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg;
331510242Schris.kirby@sun.com 	dsl_dataset_t *ds;
331610242Schris.kirby@sun.com 	int error;
331710242Schris.kirby@sun.com 	char *name;
331810242Schris.kirby@sun.com 
331910242Schris.kirby@sun.com 	/* alloc a buffer to hold dsname@snapname plus terminating NULL */
332010272SMatthew.Ahrens@Sun.COM 	name = kmem_asprintf("%s@%s", dsname, ha->snapname);
332110242Schris.kirby@sun.com 	error = dsl_dataset_hold(name, ha->dstg, &ds);
332210272SMatthew.Ahrens@Sun.COM 	strfree(name);
332310242Schris.kirby@sun.com 	if (error == 0) {
332410268Schris.kirby@sun.com 		ha->gotone = B_TRUE;
332510242Schris.kirby@sun.com 		dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check,
332610342Schris.kirby@sun.com 		    dsl_dataset_user_hold_sync, ds, ha, 0);
332710242Schris.kirby@sun.com 	} else if (error == ENOENT && ha->recursive) {
332810242Schris.kirby@sun.com 		error = 0;
332910242Schris.kirby@sun.com 	} else {
333011209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
333110242Schris.kirby@sun.com 	}
333210242Schris.kirby@sun.com 	return (error);
333310242Schris.kirby@sun.com }
333410242Schris.kirby@sun.com 
333510242Schris.kirby@sun.com int
333610242Schris.kirby@sun.com dsl_dataset_user_hold(char *dsname, char *snapname, char *htag,
333710342Schris.kirby@sun.com     boolean_t recursive, boolean_t temphold)
333810242Schris.kirby@sun.com {
333910242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha;
334010242Schris.kirby@sun.com 	dsl_sync_task_t *dst;
334110242Schris.kirby@sun.com 	spa_t *spa;
334210242Schris.kirby@sun.com 	int error;
334310242Schris.kirby@sun.com 
334410242Schris.kirby@sun.com 	ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
334510242Schris.kirby@sun.com 
334610242Schris.kirby@sun.com 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
334710242Schris.kirby@sun.com 
334810242Schris.kirby@sun.com 	error = spa_open(dsname, &spa, FTAG);
334910242Schris.kirby@sun.com 	if (error) {
335010242Schris.kirby@sun.com 		kmem_free(ha, sizeof (struct dsl_ds_holdarg));
335110242Schris.kirby@sun.com 		return (error);
335210242Schris.kirby@sun.com 	}
335310242Schris.kirby@sun.com 
335410242Schris.kirby@sun.com 	ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
335510242Schris.kirby@sun.com 	ha->htag = htag;
335610242Schris.kirby@sun.com 	ha->snapname = snapname;
335710242Schris.kirby@sun.com 	ha->recursive = recursive;
335810342Schris.kirby@sun.com 	ha->temphold = temphold;
335910242Schris.kirby@sun.com 	if (recursive) {
336010242Schris.kirby@sun.com 		error = dmu_objset_find(dsname, dsl_dataset_user_hold_one,
336110242Schris.kirby@sun.com 		    ha, DS_FIND_CHILDREN);
336210242Schris.kirby@sun.com 	} else {
336310242Schris.kirby@sun.com 		error = dsl_dataset_user_hold_one(dsname, ha);
336410242Schris.kirby@sun.com 	}
336510242Schris.kirby@sun.com 	if (error == 0)
336610242Schris.kirby@sun.com 		error = dsl_sync_task_group_wait(ha->dstg);
336710242Schris.kirby@sun.com 
336810242Schris.kirby@sun.com 	for (dst = list_head(&ha->dstg->dstg_tasks); dst;
336910242Schris.kirby@sun.com 	    dst = list_next(&ha->dstg->dstg_tasks, dst)) {
337010242Schris.kirby@sun.com 		dsl_dataset_t *ds = dst->dst_arg1;
337110242Schris.kirby@sun.com 
337210242Schris.kirby@sun.com 		if (dst->dst_err) {
337310242Schris.kirby@sun.com 			dsl_dataset_name(ds, ha->failed);
337410242Schris.kirby@sun.com 			*strchr(ha->failed, '@') = '\0';
337510242Schris.kirby@sun.com 		}
337610242Schris.kirby@sun.com 		dsl_dataset_rele(ds, ha->dstg);
337710242Schris.kirby@sun.com 	}
337810242Schris.kirby@sun.com 
337910268Schris.kirby@sun.com 	if (error == 0 && recursive && !ha->gotone)
338010268Schris.kirby@sun.com 		error = ENOENT;
338110268Schris.kirby@sun.com 
338210242Schris.kirby@sun.com 	if (error)
338311209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
338410242Schris.kirby@sun.com 
338510242Schris.kirby@sun.com 	dsl_sync_task_group_destroy(ha->dstg);
338610242Schris.kirby@sun.com 	kmem_free(ha, sizeof (struct dsl_ds_holdarg));
338710242Schris.kirby@sun.com 	spa_close(spa, FTAG);
338810242Schris.kirby@sun.com 	return (error);
338910242Schris.kirby@sun.com }
339010242Schris.kirby@sun.com 
339110242Schris.kirby@sun.com struct dsl_ds_releasearg {
339210242Schris.kirby@sun.com 	dsl_dataset_t *ds;
339310242Schris.kirby@sun.com 	const char *htag;
339410242Schris.kirby@sun.com 	boolean_t own;		/* do we own or just hold ds? */
339510242Schris.kirby@sun.com };
339610242Schris.kirby@sun.com 
339710242Schris.kirby@sun.com static int
339810242Schris.kirby@sun.com dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag,
339910242Schris.kirby@sun.com     boolean_t *might_destroy)
340010242Schris.kirby@sun.com {
340110242Schris.kirby@sun.com 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
340210242Schris.kirby@sun.com 	uint64_t zapobj;
340310242Schris.kirby@sun.com 	uint64_t tmp;
340410242Schris.kirby@sun.com 	int error;
340510242Schris.kirby@sun.com 
340610242Schris.kirby@sun.com 	*might_destroy = B_FALSE;
340710242Schris.kirby@sun.com 
340810242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
340910242Schris.kirby@sun.com 	zapobj = ds->ds_phys->ds_userrefs_obj;
341010242Schris.kirby@sun.com 	if (zapobj == 0) {
341110242Schris.kirby@sun.com 		/* The tag can't possibly exist */
341210242Schris.kirby@sun.com 		mutex_exit(&ds->ds_lock);
341310242Schris.kirby@sun.com 		return (ESRCH);
341410242Schris.kirby@sun.com 	}
341510242Schris.kirby@sun.com 
341610242Schris.kirby@sun.com 	/* Make sure the tag exists */
341710242Schris.kirby@sun.com 	error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp);
341810242Schris.kirby@sun.com 	if (error) {
341910242Schris.kirby@sun.com 		mutex_exit(&ds->ds_lock);
342010242Schris.kirby@sun.com 		if (error == ENOENT)
342110242Schris.kirby@sun.com 			error = ESRCH;
342210242Schris.kirby@sun.com 		return (error);
342310242Schris.kirby@sun.com 	}
342410242Schris.kirby@sun.com 
342510242Schris.kirby@sun.com 	if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 &&
342610242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds))
342710242Schris.kirby@sun.com 		*might_destroy = B_TRUE;
342810242Schris.kirby@sun.com 
342910242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
343010242Schris.kirby@sun.com 	return (0);
343110242Schris.kirby@sun.com }
343210242Schris.kirby@sun.com 
343310242Schris.kirby@sun.com static int
343410242Schris.kirby@sun.com dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx)
343510242Schris.kirby@sun.com {
343610242Schris.kirby@sun.com 	struct dsl_ds_releasearg *ra = arg1;
343710242Schris.kirby@sun.com 	dsl_dataset_t *ds = ra->ds;
343810242Schris.kirby@sun.com 	boolean_t might_destroy;
343910242Schris.kirby@sun.com 	int error;
344010242Schris.kirby@sun.com 
344110242Schris.kirby@sun.com 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
344210242Schris.kirby@sun.com 		return (ENOTSUP);
344310242Schris.kirby@sun.com 
344410242Schris.kirby@sun.com 	error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy);
344510242Schris.kirby@sun.com 	if (error)
344610242Schris.kirby@sun.com 		return (error);
344710242Schris.kirby@sun.com 
344810242Schris.kirby@sun.com 	if (might_destroy) {
344910242Schris.kirby@sun.com 		struct dsl_ds_destroyarg dsda = {0};
345010242Schris.kirby@sun.com 
345110242Schris.kirby@sun.com 		if (dmu_tx_is_syncing(tx)) {
345210242Schris.kirby@sun.com 			/*
345310242Schris.kirby@sun.com 			 * If we're not prepared to remove the snapshot,
345410242Schris.kirby@sun.com 			 * we can't allow the release to happen right now.
345510242Schris.kirby@sun.com 			 */
345610242Schris.kirby@sun.com 			if (!ra->own)
345710242Schris.kirby@sun.com 				return (EBUSY);
345810242Schris.kirby@sun.com 		}
345910242Schris.kirby@sun.com 		dsda.ds = ds;
346010242Schris.kirby@sun.com 		dsda.releasing = B_TRUE;
346110242Schris.kirby@sun.com 		return (dsl_dataset_destroy_check(&dsda, tag, tx));
346210242Schris.kirby@sun.com 	}
346310242Schris.kirby@sun.com 
346410242Schris.kirby@sun.com 	return (0);
346510242Schris.kirby@sun.com }
346610242Schris.kirby@sun.com 
346710242Schris.kirby@sun.com static void
3468*12296SLin.Ling@Sun.COM dsl_dataset_user_release_sync(void *arg1, void *tag, dmu_tx_t *tx)
346910242Schris.kirby@sun.com {
347010242Schris.kirby@sun.com 	struct dsl_ds_releasearg *ra = arg1;
347110242Schris.kirby@sun.com 	dsl_dataset_t *ds = ra->ds;
347210342Schris.kirby@sun.com 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
347310342Schris.kirby@sun.com 	objset_t *mos = dp->dp_meta_objset;
347410242Schris.kirby@sun.com 	uint64_t zapobj;
347510242Schris.kirby@sun.com 	uint64_t dsobj = ds->ds_object;
347610242Schris.kirby@sun.com 	uint64_t refs;
347710342Schris.kirby@sun.com 	int error;
347810242Schris.kirby@sun.com 
3479*12296SLin.Ling@Sun.COM 	if (ds->ds_objset) {
3480*12296SLin.Ling@Sun.COM 		dmu_objset_evict(ds->ds_objset);
3481*12296SLin.Ling@Sun.COM 		ds->ds_objset = NULL;
3482*12296SLin.Ling@Sun.COM 	}
3483*12296SLin.Ling@Sun.COM 
348410242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
348510242Schris.kirby@sun.com 	ds->ds_userrefs--;
348610242Schris.kirby@sun.com 	refs = ds->ds_userrefs;
348710242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
348810342Schris.kirby@sun.com 	error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx);
348910342Schris.kirby@sun.com 	VERIFY(error == 0 || error == ENOENT);
349010242Schris.kirby@sun.com 	zapobj = ds->ds_phys->ds_userrefs_obj;
349110242Schris.kirby@sun.com 	VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx));
349210242Schris.kirby@sun.com 	if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 &&
349310242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds)) {
349410242Schris.kirby@sun.com 		struct dsl_ds_destroyarg dsda = {0};
349510242Schris.kirby@sun.com 
349610242Schris.kirby@sun.com 		ASSERT(ra->own);
349710242Schris.kirby@sun.com 		dsda.ds = ds;
349810242Schris.kirby@sun.com 		dsda.releasing = B_TRUE;
349910242Schris.kirby@sun.com 		/* We already did the destroy_check */
3500*12296SLin.Ling@Sun.COM 		dsl_dataset_destroy_sync(&dsda, tag, tx);
350110242Schris.kirby@sun.com 	}
350210242Schris.kirby@sun.com 
3503*12296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_USER_RELEASE,
3504*12296SLin.Ling@Sun.COM 	    dp->dp_spa, tx, "<%s> %lld dataset = %llu",
350510242Schris.kirby@sun.com 	    ra->htag, (longlong_t)refs, dsobj);
350610242Schris.kirby@sun.com }
350710242Schris.kirby@sun.com 
350810242Schris.kirby@sun.com static int
350911209SMatthew.Ahrens@Sun.COM dsl_dataset_user_release_one(const char *dsname, void *arg)
351010242Schris.kirby@sun.com {
351110242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg;
351210242Schris.kirby@sun.com 	struct dsl_ds_releasearg *ra;
351310242Schris.kirby@sun.com 	dsl_dataset_t *ds;
351410242Schris.kirby@sun.com 	int error;
351510242Schris.kirby@sun.com 	void *dtag = ha->dstg;
351610242Schris.kirby@sun.com 	char *name;
351710242Schris.kirby@sun.com 	boolean_t own = B_FALSE;
351810242Schris.kirby@sun.com 	boolean_t might_destroy;
351910242Schris.kirby@sun.com 
352010242Schris.kirby@sun.com 	/* alloc a buffer to hold dsname@snapname, plus the terminating NULL */
352110272SMatthew.Ahrens@Sun.COM 	name = kmem_asprintf("%s@%s", dsname, ha->snapname);
352210242Schris.kirby@sun.com 	error = dsl_dataset_hold(name, dtag, &ds);
352310272SMatthew.Ahrens@Sun.COM 	strfree(name);
352410242Schris.kirby@sun.com 	if (error == ENOENT && ha->recursive)
352510242Schris.kirby@sun.com 		return (0);
352611209SMatthew.Ahrens@Sun.COM 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
352710242Schris.kirby@sun.com 	if (error)
352810242Schris.kirby@sun.com 		return (error);
352910242Schris.kirby@sun.com 
353010268Schris.kirby@sun.com 	ha->gotone = B_TRUE;
353110268Schris.kirby@sun.com 
353210242Schris.kirby@sun.com 	ASSERT(dsl_dataset_is_snapshot(ds));
353310242Schris.kirby@sun.com 
353410242Schris.kirby@sun.com 	error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy);
353510242Schris.kirby@sun.com 	if (error) {
353610242Schris.kirby@sun.com 		dsl_dataset_rele(ds, dtag);
353710242Schris.kirby@sun.com 		return (error);
353810242Schris.kirby@sun.com 	}
353910242Schris.kirby@sun.com 
354010242Schris.kirby@sun.com 	if (might_destroy) {
354110242Schris.kirby@sun.com #ifdef _KERNEL
354212115SChris.Kirby@oracle.com 		name = kmem_asprintf("%s@%s", dsname, ha->snapname);
354310242Schris.kirby@sun.com 		error = zfs_unmount_snap(name, NULL);
354412115SChris.Kirby@oracle.com 		strfree(name);
354510242Schris.kirby@sun.com 		if (error) {
354610242Schris.kirby@sun.com 			dsl_dataset_rele(ds, dtag);
354710242Schris.kirby@sun.com 			return (error);
354810242Schris.kirby@sun.com 		}
354910242Schris.kirby@sun.com #endif
355010298SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) {
355110242Schris.kirby@sun.com 			dsl_dataset_rele(ds, dtag);
355210242Schris.kirby@sun.com 			return (EBUSY);
355310242Schris.kirby@sun.com 		} else {
355410242Schris.kirby@sun.com 			own = B_TRUE;
355510242Schris.kirby@sun.com 			dsl_dataset_make_exclusive(ds, dtag);
355610242Schris.kirby@sun.com 		}
355710242Schris.kirby@sun.com 	}
355810242Schris.kirby@sun.com 
355910242Schris.kirby@sun.com 	ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP);
356010242Schris.kirby@sun.com 	ra->ds = ds;
356110242Schris.kirby@sun.com 	ra->htag = ha->htag;
356210242Schris.kirby@sun.com 	ra->own = own;
356310242Schris.kirby@sun.com 	dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check,
356410242Schris.kirby@sun.com 	    dsl_dataset_user_release_sync, ra, dtag, 0);
356510242Schris.kirby@sun.com 
356610242Schris.kirby@sun.com 	return (0);
356710242Schris.kirby@sun.com }
356810242Schris.kirby@sun.com 
356910242Schris.kirby@sun.com int
357010242Schris.kirby@sun.com dsl_dataset_user_release(char *dsname, char *snapname, char *htag,
357110242Schris.kirby@sun.com     boolean_t recursive)
357210242Schris.kirby@sun.com {
357310242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha;
357410242Schris.kirby@sun.com 	dsl_sync_task_t *dst;
357510242Schris.kirby@sun.com 	spa_t *spa;
357610242Schris.kirby@sun.com 	int error;
357710242Schris.kirby@sun.com 
357811546SChris.Kirby@sun.com top:
357910242Schris.kirby@sun.com 	ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
358010242Schris.kirby@sun.com 
358110242Schris.kirby@sun.com 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
358210242Schris.kirby@sun.com 
358310242Schris.kirby@sun.com 	error = spa_open(dsname, &spa, FTAG);
358410242Schris.kirby@sun.com 	if (error) {
358510242Schris.kirby@sun.com 		kmem_free(ha, sizeof (struct dsl_ds_holdarg));
358610242Schris.kirby@sun.com 		return (error);
358710242Schris.kirby@sun.com 	}
358810242Schris.kirby@sun.com 
358910242Schris.kirby@sun.com 	ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
359010242Schris.kirby@sun.com 	ha->htag = htag;
359110242Schris.kirby@sun.com 	ha->snapname = snapname;
359210242Schris.kirby@sun.com 	ha->recursive = recursive;
359310242Schris.kirby@sun.com 	if (recursive) {
359410242Schris.kirby@sun.com 		error = dmu_objset_find(dsname, dsl_dataset_user_release_one,
359510242Schris.kirby@sun.com 		    ha, DS_FIND_CHILDREN);
359610242Schris.kirby@sun.com 	} else {
359710242Schris.kirby@sun.com 		error = dsl_dataset_user_release_one(dsname, ha);
359810242Schris.kirby@sun.com 	}
359910242Schris.kirby@sun.com 	if (error == 0)
360010242Schris.kirby@sun.com 		error = dsl_sync_task_group_wait(ha->dstg);
360110242Schris.kirby@sun.com 
360210242Schris.kirby@sun.com 	for (dst = list_head(&ha->dstg->dstg_tasks); dst;
360310242Schris.kirby@sun.com 	    dst = list_next(&ha->dstg->dstg_tasks, dst)) {
360410242Schris.kirby@sun.com 		struct dsl_ds_releasearg *ra = dst->dst_arg1;
360510242Schris.kirby@sun.com 		dsl_dataset_t *ds = ra->ds;
360610242Schris.kirby@sun.com 
360710242Schris.kirby@sun.com 		if (dst->dst_err)
360810242Schris.kirby@sun.com 			dsl_dataset_name(ds, ha->failed);
360910242Schris.kirby@sun.com 
361010242Schris.kirby@sun.com 		if (ra->own)
361110242Schris.kirby@sun.com 			dsl_dataset_disown(ds, ha->dstg);
361210242Schris.kirby@sun.com 		else
361310242Schris.kirby@sun.com 			dsl_dataset_rele(ds, ha->dstg);
361410242Schris.kirby@sun.com 
361510242Schris.kirby@sun.com 		kmem_free(ra, sizeof (struct dsl_ds_releasearg));
361610242Schris.kirby@sun.com 	}
361710242Schris.kirby@sun.com 
361810268Schris.kirby@sun.com 	if (error == 0 && recursive && !ha->gotone)
361910268Schris.kirby@sun.com 		error = ENOENT;
362010268Schris.kirby@sun.com 
362111546SChris.Kirby@sun.com 	if (error && error != EBUSY)
362211209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
362310242Schris.kirby@sun.com 
362410242Schris.kirby@sun.com 	dsl_sync_task_group_destroy(ha->dstg);
362510242Schris.kirby@sun.com 	kmem_free(ha, sizeof (struct dsl_ds_holdarg));
362610242Schris.kirby@sun.com 	spa_close(spa, FTAG);
362711546SChris.Kirby@sun.com 
362811546SChris.Kirby@sun.com 	/*
362911546SChris.Kirby@sun.com 	 * We can get EBUSY if we were racing with deferred destroy and
363011546SChris.Kirby@sun.com 	 * dsl_dataset_user_release_check() hadn't done the necessary
363111546SChris.Kirby@sun.com 	 * open context setup.  We can also get EBUSY if we're racing
363211546SChris.Kirby@sun.com 	 * with destroy and that thread is the ds_owner.  Either way
363311546SChris.Kirby@sun.com 	 * the busy condition should be transient, and we should retry
363411546SChris.Kirby@sun.com 	 * the release operation.
363511546SChris.Kirby@sun.com 	 */
363611546SChris.Kirby@sun.com 	if (error == EBUSY)
363711546SChris.Kirby@sun.com 		goto top;
363811546SChris.Kirby@sun.com 
363910242Schris.kirby@sun.com 	return (error);
364010242Schris.kirby@sun.com }
364110242Schris.kirby@sun.com 
364210342Schris.kirby@sun.com /*
364310342Schris.kirby@sun.com  * Called at spa_load time to release a stale temporary user hold.
364410342Schris.kirby@sun.com  */
364510342Schris.kirby@sun.com int
364610342Schris.kirby@sun.com dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag)
364710342Schris.kirby@sun.com {
364810342Schris.kirby@sun.com 	dsl_dataset_t *ds;
364910342Schris.kirby@sun.com 	char *snap;
365010342Schris.kirby@sun.com 	char *name;
365110342Schris.kirby@sun.com 	int namelen;
365210342Schris.kirby@sun.com 	int error;
365310342Schris.kirby@sun.com 
365410342Schris.kirby@sun.com 	rw_enter(&dp->dp_config_rwlock, RW_READER);
365510342Schris.kirby@sun.com 	error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
365610342Schris.kirby@sun.com 	rw_exit(&dp->dp_config_rwlock);
365710342Schris.kirby@sun.com 	if (error)
365810342Schris.kirby@sun.com 		return (error);
365910342Schris.kirby@sun.com 	namelen = dsl_dataset_namelen(ds)+1;
366010342Schris.kirby@sun.com 	name = kmem_alloc(namelen, KM_SLEEP);
366110342Schris.kirby@sun.com 	dsl_dataset_name(ds, name);
366210342Schris.kirby@sun.com 	dsl_dataset_rele(ds, FTAG);
366310342Schris.kirby@sun.com 
366410342Schris.kirby@sun.com 	snap = strchr(name, '@');
366510342Schris.kirby@sun.com 	*snap = '\0';
366610342Schris.kirby@sun.com 	++snap;
366710342Schris.kirby@sun.com 	return (dsl_dataset_user_release(name, snap, htag, B_FALSE));
366810342Schris.kirby@sun.com }
366910342Schris.kirby@sun.com 
367010242Schris.kirby@sun.com int
367110242Schris.kirby@sun.com dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp)
367210242Schris.kirby@sun.com {
367310242Schris.kirby@sun.com 	dsl_dataset_t *ds;
367410242Schris.kirby@sun.com 	int err;
367510242Schris.kirby@sun.com 
367610242Schris.kirby@sun.com 	err = dsl_dataset_hold(dsname, FTAG, &ds);
367710242Schris.kirby@sun.com 	if (err)
367810242Schris.kirby@sun.com 		return (err);
367910242Schris.kirby@sun.com 
368010242Schris.kirby@sun.com 	VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP));
368110242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj != 0) {
368210242Schris.kirby@sun.com 		zap_attribute_t *za;
368310242Schris.kirby@sun.com 		zap_cursor_t zc;
368410242Schris.kirby@sun.com 
368510242Schris.kirby@sun.com 		za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
368610242Schris.kirby@sun.com 		for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset,
368710242Schris.kirby@sun.com 		    ds->ds_phys->ds_userrefs_obj);
368810242Schris.kirby@sun.com 		    zap_cursor_retrieve(&zc, za) == 0;
368910242Schris.kirby@sun.com 		    zap_cursor_advance(&zc)) {
369010242Schris.kirby@sun.com 			VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name,
369110242Schris.kirby@sun.com 			    za->za_first_integer));
369210242Schris.kirby@sun.com 		}
369310242Schris.kirby@sun.com 		zap_cursor_fini(&zc);
369410242Schris.kirby@sun.com 		kmem_free(za, sizeof (zap_attribute_t));
369510242Schris.kirby@sun.com 	}
369610242Schris.kirby@sun.com 	dsl_dataset_rele(ds, FTAG);
369710242Schris.kirby@sun.com 	return (0);
369810242Schris.kirby@sun.com }
369910298SMatthew.Ahrens@Sun.COM 
370010298SMatthew.Ahrens@Sun.COM /*
370110298SMatthew.Ahrens@Sun.COM  * Note, this fuction is used as the callback for dmu_objset_find().  We
370210298SMatthew.Ahrens@Sun.COM  * always return 0 so that we will continue to find and process
370310298SMatthew.Ahrens@Sun.COM  * inconsistent datasets, even if we encounter an error trying to
370410298SMatthew.Ahrens@Sun.COM  * process one of them.
370510298SMatthew.Ahrens@Sun.COM  */
370610298SMatthew.Ahrens@Sun.COM /* ARGSUSED */
370710298SMatthew.Ahrens@Sun.COM int
370811209SMatthew.Ahrens@Sun.COM dsl_destroy_inconsistent(const char *dsname, void *arg)
370910298SMatthew.Ahrens@Sun.COM {
371010298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
371110298SMatthew.Ahrens@Sun.COM 
371210298SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) {
371310298SMatthew.Ahrens@Sun.COM 		if (DS_IS_INCONSISTENT(ds))
371410298SMatthew.Ahrens@Sun.COM 			(void) dsl_dataset_destroy(ds, FTAG, B_FALSE);
371510298SMatthew.Ahrens@Sun.COM 		else
371610298SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(ds, FTAG);
371710298SMatthew.Ahrens@Sun.COM 	}
371810298SMatthew.Ahrens@Sun.COM 	return (0);
371910298SMatthew.Ahrens@Sun.COM }
3720