xref: /onnv-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision 12450:c77e20e4e046)
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>
4112296SLin.Ling@Sun.COM #include <sys/dsl_scan.h>
42789Sahrens 
43*12450SGeorge.Wilson@Sun.COM /*
44*12450SGeorge.Wilson@Sun.COM  * Enable/disable prefetching of dedup-ed blocks which are going to be freed.
45*12450SGeorge.Wilson@Sun.COM  */
46*12450SGeorge.Wilson@Sun.COM int zfs_dedup_prefetch = 1;
47*12450SGeorge.Wilson@Sun.COM 
486689Smaybee static char *dsl_reaper = "the grim reaper";
496689Smaybee 
502199Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check;
512199Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync;
525378Sck153898 static dsl_syncfunc_t dsl_dataset_set_reservation_sync;
531731Sbonwick 
543444Sek110237 #define	DS_REF_MAX	(1ULL << 62)
55789Sahrens 
56789Sahrens #define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
57789Sahrens 
586689Smaybee #define	DSL_DATASET_IS_DESTROYED(ds)	((ds)->ds_owner == dsl_reaper)
596689Smaybee 
60789Sahrens 
615378Sck153898 /*
625378Sck153898  * Figure out how much of this delta should be propogated to the dsl_dir
635378Sck153898  * layer.  If there's a refreservation, that space has already been
645378Sck153898  * partially accounted for in our ancestors.
655378Sck153898  */
665378Sck153898 static int64_t
675378Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta)
685378Sck153898 {
695378Sck153898 	uint64_t old_bytes, new_bytes;
705378Sck153898 
715378Sck153898 	if (ds->ds_reserved == 0)
725378Sck153898 		return (delta);
735378Sck153898 
745378Sck153898 	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
755378Sck153898 	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
765378Sck153898 
775378Sck153898 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
785378Sck153898 	return (new_bytes - old_bytes);
795378Sck153898 }
80789Sahrens 
81789Sahrens void
8210922SJeff.Bonwick@Sun.COM dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
83789Sahrens {
8410922SJeff.Bonwick@Sun.COM 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
85789Sahrens 	int compressed = BP_GET_PSIZE(bp);
86789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
875378Sck153898 	int64_t delta;
88789Sahrens 
8912296SLin.Ling@Sun.COM 	dprintf_bp(bp, "ds=%p", ds);
90789Sahrens 
91789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
92789Sahrens 	/* It could have been compressed away to nothing */
93789Sahrens 	if (BP_IS_HOLE(bp))
94789Sahrens 		return;
95789Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
96789Sahrens 	ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES);
97789Sahrens 	if (ds == NULL) {
98789Sahrens 		/*
99789Sahrens 		 * Account for the meta-objset space in its placeholder
100789Sahrens 		 * dsl_dir.
101789Sahrens 		 */
102789Sahrens 		ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */
1037390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
104789Sahrens 		    used, compressed, uncompressed, tx);
105789Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
106789Sahrens 		return;
107789Sahrens 	}
108789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
10912296SLin.Ling@Sun.COM 
1107595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_dir->dd_lock);
111789Sahrens 	mutex_enter(&ds->ds_lock);
1125378Sck153898 	delta = parent_delta(ds, used);
113789Sahrens 	ds->ds_phys->ds_used_bytes += used;
114789Sahrens 	ds->ds_phys->ds_compressed_bytes += compressed;
115789Sahrens 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
116789Sahrens 	ds->ds_phys->ds_unique_bytes += used;
117789Sahrens 	mutex_exit(&ds->ds_lock);
1187390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
1197390SMatthew.Ahrens@Sun.COM 	    compressed, uncompressed, tx);
1207390SMatthew.Ahrens@Sun.COM 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
1217390SMatthew.Ahrens@Sun.COM 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
1227595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_dir->dd_lock);
123789Sahrens }
124789Sahrens 
1256992Smaybee int
12610922SJeff.Bonwick@Sun.COM dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
12710922SJeff.Bonwick@Sun.COM     boolean_t async)
128789Sahrens {
12910922SJeff.Bonwick@Sun.COM 	if (BP_IS_HOLE(bp))
13010922SJeff.Bonwick@Sun.COM 		return (0);
13110922SJeff.Bonwick@Sun.COM 
13210922SJeff.Bonwick@Sun.COM 	ASSERT(dmu_tx_is_syncing(tx));
13310922SJeff.Bonwick@Sun.COM 	ASSERT(bp->blk_birth <= tx->tx_txg);
13410922SJeff.Bonwick@Sun.COM 
13510922SJeff.Bonwick@Sun.COM 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
136789Sahrens 	int compressed = BP_GET_PSIZE(bp);
137789Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
138789Sahrens 
139789Sahrens 	ASSERT(used > 0);
140789Sahrens 	if (ds == NULL) {
141789Sahrens 		/*
142789Sahrens 		 * Account for the meta-objset space in its placeholder
143789Sahrens 		 * dataset.
144789Sahrens 		 */
14510922SJeff.Bonwick@Sun.COM 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
146789Sahrens 
1477390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
148789Sahrens 		    -used, -compressed, -uncompressed, tx);
149789Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
1506992Smaybee 		return (used);
151789Sahrens 	}
152789Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
153789Sahrens 
1547390SMatthew.Ahrens@Sun.COM 	ASSERT(!dsl_dataset_is_snapshot(ds));
155789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
156789Sahrens 
157789Sahrens 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
1585378Sck153898 		int64_t delta;
1593547Smaybee 
16012296SLin.Ling@Sun.COM 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
16110922SJeff.Bonwick@Sun.COM 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
162789Sahrens 
1637595SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_dir->dd_lock);
164789Sahrens 		mutex_enter(&ds->ds_lock);
1655378Sck153898 		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
1665378Sck153898 		    !DS_UNIQUE_IS_ACCURATE(ds));
1675378Sck153898 		delta = parent_delta(ds, -used);
168789Sahrens 		ds->ds_phys->ds_unique_bytes -= used;
169789Sahrens 		mutex_exit(&ds->ds_lock);
1707390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
1715378Sck153898 		    delta, -compressed, -uncompressed, tx);
1727390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
1737390SMatthew.Ahrens@Sun.COM 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
1747595SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_dir->dd_lock);
175789Sahrens 	} else {
176789Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
17710922SJeff.Bonwick@Sun.COM 		if (async) {
17810922SJeff.Bonwick@Sun.COM 			/*
17910922SJeff.Bonwick@Sun.COM 			 * We are here as part of zio's write done callback,
18010922SJeff.Bonwick@Sun.COM 			 * which means we're a zio interrupt thread.  We can't
18110922SJeff.Bonwick@Sun.COM 			 * call bplist_enqueue() now because it may block
18210922SJeff.Bonwick@Sun.COM 			 * waiting for I/O.  Instead, put bp on the deferred
18310922SJeff.Bonwick@Sun.COM 			 * queue and let dsl_pool_sync() finish the job.
18410922SJeff.Bonwick@Sun.COM 			 */
18510922SJeff.Bonwick@Sun.COM 			bplist_enqueue_deferred(&ds->ds_deadlist, bp);
18610922SJeff.Bonwick@Sun.COM 		} else {
18710922SJeff.Bonwick@Sun.COM 			VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx));
18810922SJeff.Bonwick@Sun.COM 		}
1895712Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
1905712Sahrens 		    ds->ds_phys->ds_prev_snap_obj);
1915712Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
192789Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
1935712Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
1945712Sahrens 		    ds->ds_object && bp->blk_birth >
1955712Sahrens 		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
1965712Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1975712Sahrens 			mutex_enter(&ds->ds_prev->ds_lock);
1985712Sahrens 			ds->ds_prev->ds_phys->ds_unique_bytes += used;
1995712Sahrens 			mutex_exit(&ds->ds_prev->ds_lock);
200789Sahrens 		}
20112296SLin.Ling@Sun.COM 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
2027390SMatthew.Ahrens@Sun.COM 			dsl_dir_transfer_space(ds->ds_dir, used,
2037390SMatthew.Ahrens@Sun.COM 			    DD_USED_HEAD, DD_USED_SNAP, tx);
2047390SMatthew.Ahrens@Sun.COM 		}
205789Sahrens 	}
206789Sahrens 	mutex_enter(&ds->ds_lock);
207789Sahrens 	ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used);
208789Sahrens 	ds->ds_phys->ds_used_bytes -= used;
209789Sahrens 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
210789Sahrens 	ds->ds_phys->ds_compressed_bytes -= compressed;
211789Sahrens 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
212789Sahrens 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
213789Sahrens 	mutex_exit(&ds->ds_lock);
2146992Smaybee 
2156992Smaybee 	return (used);
216789Sahrens }
217789Sahrens 
2181544Seschrock uint64_t
2191544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
220789Sahrens {
2212885Sahrens 	uint64_t trysnap = 0;
2222885Sahrens 
223789Sahrens 	if (ds == NULL)
2241544Seschrock 		return (0);
225789Sahrens 	/*
226789Sahrens 	 * The snapshot creation could fail, but that would cause an
227789Sahrens 	 * incorrect FALSE return, which would only result in an
228789Sahrens 	 * overestimation of the amount of space that an operation would
229789Sahrens 	 * consume, which is OK.
230789Sahrens 	 *
231789Sahrens 	 * There's also a small window where we could miss a pending
232789Sahrens 	 * snapshot, because we could set the sync task in the quiescing
233789Sahrens 	 * phase.  So this should only be used as a guess.
234789Sahrens 	 */
2352885Sahrens 	if (ds->ds_trysnap_txg >
2362885Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
2372885Sahrens 		trysnap = ds->ds_trysnap_txg;
2382885Sahrens 	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
2391544Seschrock }
2401544Seschrock 
2419653SSanjeev.Bagewadi@Sun.COM boolean_t
242*12450SGeorge.Wilson@Sun.COM dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
243*12450SGeorge.Wilson@Sun.COM     uint64_t blk_birth)
2441544Seschrock {
245*12450SGeorge.Wilson@Sun.COM 	if (blk_birth <= dsl_dataset_prev_snap_txg(ds))
246*12450SGeorge.Wilson@Sun.COM 		return (B_FALSE);
247*12450SGeorge.Wilson@Sun.COM 
248*12450SGeorge.Wilson@Sun.COM 	if (zfs_dedup_prefetch && bp && BP_GET_DEDUP(bp))
249*12450SGeorge.Wilson@Sun.COM 		ddt_prefetch(dsl_dataset_get_spa(ds), bp);
250*12450SGeorge.Wilson@Sun.COM 
251*12450SGeorge.Wilson@Sun.COM 	return (B_TRUE);
252789Sahrens }
253789Sahrens 
254789Sahrens /* ARGSUSED */
255789Sahrens static void
256789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv)
257789Sahrens {
258789Sahrens 	dsl_dataset_t *ds = dsv;
259789Sahrens 
2606689Smaybee 	ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds));
261789Sahrens 
2624787Sahrens 	unique_remove(ds->ds_fsid_guid);
263789Sahrens 
26410298SMatthew.Ahrens@Sun.COM 	if (ds->ds_objset != NULL)
26510298SMatthew.Ahrens@Sun.COM 		dmu_objset_evict(ds->ds_objset);
266789Sahrens 
267789Sahrens 	if (ds->ds_prev) {
2686689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
269789Sahrens 		ds->ds_prev = NULL;
270789Sahrens 	}
271789Sahrens 
272789Sahrens 	bplist_close(&ds->ds_deadlist);
2736689Smaybee 	if (ds->ds_dir)
2746689Smaybee 		dsl_dir_close(ds->ds_dir, ds);
275789Sahrens 
2764787Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
277789Sahrens 
2782856Snd150628 	mutex_destroy(&ds->ds_lock);
27910204SMatthew.Ahrens@Sun.COM 	mutex_destroy(&ds->ds_recvlock);
2804787Sahrens 	mutex_destroy(&ds->ds_opening_lock);
2816689Smaybee 	rw_destroy(&ds->ds_rwlock);
2826689Smaybee 	cv_destroy(&ds->ds_exclusive_cv);
28310922SJeff.Bonwick@Sun.COM 	bplist_fini(&ds->ds_deadlist);
2842856Snd150628 
285789Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
286789Sahrens }
287789Sahrens 
2881544Seschrock static int
289789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
290789Sahrens {
291789Sahrens 	dsl_dataset_phys_t *headphys;
292789Sahrens 	int err;
293789Sahrens 	dmu_buf_t *headdbuf;
294789Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
295789Sahrens 	objset_t *mos = dp->dp_meta_objset;
296789Sahrens 
297789Sahrens 	if (ds->ds_snapname[0])
2981544Seschrock 		return (0);
299789Sahrens 	if (ds->ds_phys->ds_next_snap_obj == 0)
3001544Seschrock 		return (0);
301789Sahrens 
3021544Seschrock 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
3031544Seschrock 	    FTAG, &headdbuf);
3041544Seschrock 	if (err)
3051544Seschrock 		return (err);
306789Sahrens 	headphys = headdbuf->db_data;
307789Sahrens 	err = zap_value_search(dp->dp_meta_objset,
3084577Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
3091544Seschrock 	dmu_buf_rele(headdbuf, FTAG);
3101544Seschrock 	return (err);
311789Sahrens }
312789Sahrens 
3136492Stimh static int
3146689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
3156492Stimh {
3166689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3176689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
3186492Stimh 	matchtype_t mt;
3196492Stimh 	int err;
3206492Stimh 
3216689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
3226492Stimh 		mt = MT_FIRST;
3236492Stimh 	else
3246492Stimh 		mt = MT_EXACT;
3256492Stimh 
3266689Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
3276492Stimh 	    value, mt, NULL, 0, NULL);
3286492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
3296689Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
3306492Stimh 	return (err);
3316492Stimh }
3326492Stimh 
3336492Stimh static int
3346689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx)
3356492Stimh {
3366689Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3376689Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
3386492Stimh 	matchtype_t mt;
3396492Stimh 	int err;
3406492Stimh 
34110373Schris.kirby@sun.com 	dsl_dir_snap_cmtime_update(ds->ds_dir);
34210373Schris.kirby@sun.com 
3436689Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
3446492Stimh 		mt = MT_FIRST;
3456492Stimh 	else
3466492Stimh 		mt = MT_EXACT;
3476492Stimh 
3486689Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
3496492Stimh 	if (err == ENOTSUP && mt == MT_FIRST)
3506689Smaybee 		err = zap_remove(mos, snapobj, name, tx);
3516492Stimh 	return (err);
3526492Stimh }
3536492Stimh 
3546689Smaybee static int
3556689Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag,
3566689Smaybee     dsl_dataset_t **dsp)
357789Sahrens {
358789Sahrens 	objset_t *mos = dp->dp_meta_objset;
359789Sahrens 	dmu_buf_t *dbuf;
360789Sahrens 	dsl_dataset_t *ds;
3611544Seschrock 	int err;
362789Sahrens 
363789Sahrens 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
364789Sahrens 	    dsl_pool_sync_context(dp));
365789Sahrens 
3661544Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
3671544Seschrock 	if (err)
3681544Seschrock 		return (err);
369789Sahrens 	ds = dmu_buf_get_user(dbuf);
370789Sahrens 	if (ds == NULL) {
371789Sahrens 		dsl_dataset_t *winner;
372789Sahrens 
373789Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
374789Sahrens 		ds->ds_dbuf = dbuf;
375789Sahrens 		ds->ds_object = dsobj;
376789Sahrens 		ds->ds_phys = dbuf->db_data;
377789Sahrens 
3782856Snd150628 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
37910204SMatthew.Ahrens@Sun.COM 		mutex_init(&ds->ds_recvlock, NULL, MUTEX_DEFAULT, NULL);
3804787Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
3816689Smaybee 		rw_init(&ds->ds_rwlock, 0, 0, 0);
3826689Smaybee 		cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL);
38310922SJeff.Bonwick@Sun.COM 		bplist_init(&ds->ds_deadlist);
3842856Snd150628 
3851544Seschrock 		err = bplist_open(&ds->ds_deadlist,
386789Sahrens 		    mos, ds->ds_phys->ds_deadlist_obj);
3871544Seschrock 		if (err == 0) {
3881544Seschrock 			err = dsl_dir_open_obj(dp,
3891544Seschrock 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
3901544Seschrock 		}
3911544Seschrock 		if (err) {
3921544Seschrock 			/*
3931544Seschrock 			 * we don't really need to close the blist if we
3941544Seschrock 			 * just opened it.
3951544Seschrock 			 */
3962856Snd150628 			mutex_destroy(&ds->ds_lock);
39710204SMatthew.Ahrens@Sun.COM 			mutex_destroy(&ds->ds_recvlock);
3984787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
3996689Smaybee 			rw_destroy(&ds->ds_rwlock);
4006689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
40110922SJeff.Bonwick@Sun.COM 			bplist_fini(&ds->ds_deadlist);
4021544Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
4031544Seschrock 			dmu_buf_rele(dbuf, tag);
4041544Seschrock 			return (err);
4051544Seschrock 		}
406789Sahrens 
4077390SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(ds)) {
408789Sahrens 			ds->ds_snapname[0] = '\0';
409789Sahrens 			if (ds->ds_phys->ds_prev_snap_obj) {
4106689Smaybee 				err = dsl_dataset_get_ref(dp,
4116689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
4126689Smaybee 				    ds, &ds->ds_prev);
413789Sahrens 			}
41410242Schris.kirby@sun.com 		} else {
41510242Schris.kirby@sun.com 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
41610242Schris.kirby@sun.com 				err = dsl_dataset_get_snapname(ds);
41710242Schris.kirby@sun.com 			if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
41810242Schris.kirby@sun.com 				err = zap_count(
41910242Schris.kirby@sun.com 				    ds->ds_dir->dd_pool->dp_meta_objset,
42010242Schris.kirby@sun.com 				    ds->ds_phys->ds_userrefs_obj,
42110242Schris.kirby@sun.com 				    &ds->ds_userrefs);
42210242Schris.kirby@sun.com 			}
423789Sahrens 		}
424789Sahrens 
4257390SMatthew.Ahrens@Sun.COM 		if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
4265569Sck153898 			/*
4275569Sck153898 			 * In sync context, we're called with either no lock
4285569Sck153898 			 * or with the write lock.  If we're not syncing,
4295569Sck153898 			 * we're always called with the read lock held.
4305569Sck153898 			 */
4315475Sck153898 			boolean_t need_lock =
4325569Sck153898 			    !RW_WRITE_HELD(&dp->dp_config_rwlock) &&
4335569Sck153898 			    dsl_pool_sync_context(dp);
4345475Sck153898 
4355475Sck153898 			if (need_lock)
4365475Sck153898 				rw_enter(&dp->dp_config_rwlock, RW_READER);
4375475Sck153898 
4387265Sahrens 			err = dsl_prop_get_ds(ds,
4395475Sck153898 			    "refreservation", sizeof (uint64_t), 1,
4405475Sck153898 			    &ds->ds_reserved, NULL);
4415475Sck153898 			if (err == 0) {
4427265Sahrens 				err = dsl_prop_get_ds(ds,
4435475Sck153898 				    "refquota", sizeof (uint64_t), 1,
4445475Sck153898 				    &ds->ds_quota, NULL);
4455475Sck153898 			}
4465475Sck153898 
4475475Sck153898 			if (need_lock)
4485475Sck153898 				rw_exit(&dp->dp_config_rwlock);
4495475Sck153898 		} else {
4505475Sck153898 			ds->ds_reserved = ds->ds_quota = 0;
4515475Sck153898 		}
4525475Sck153898 
4531544Seschrock 		if (err == 0) {
4541544Seschrock 			winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys,
4551544Seschrock 			    dsl_dataset_evict);
4561544Seschrock 		}
4571544Seschrock 		if (err || winner) {
458789Sahrens 			bplist_close(&ds->ds_deadlist);
4596689Smaybee 			if (ds->ds_prev)
4606689Smaybee 				dsl_dataset_drop_ref(ds->ds_prev, ds);
461789Sahrens 			dsl_dir_close(ds->ds_dir, ds);
4622856Snd150628 			mutex_destroy(&ds->ds_lock);
46310204SMatthew.Ahrens@Sun.COM 			mutex_destroy(&ds->ds_recvlock);
4644787Sahrens 			mutex_destroy(&ds->ds_opening_lock);
4656689Smaybee 			rw_destroy(&ds->ds_rwlock);
4666689Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
46710922SJeff.Bonwick@Sun.COM 			bplist_fini(&ds->ds_deadlist);
468789Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
4691544Seschrock 			if (err) {
4701544Seschrock 				dmu_buf_rele(dbuf, tag);
4711544Seschrock 				return (err);
4721544Seschrock 			}
473789Sahrens 			ds = winner;
474789Sahrens 		} else {
4754787Sahrens 			ds->ds_fsid_guid =
476789Sahrens 			    unique_insert(ds->ds_phys->ds_fsid_guid);
477789Sahrens 		}
478789Sahrens 	}
479789Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
480789Sahrens 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
4817046Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
4827061Sahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
4837077Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
484789Sahrens 	mutex_enter(&ds->ds_lock);
4856689Smaybee 	if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) {
486789Sahrens 		mutex_exit(&ds->ds_lock);
4876689Smaybee 		dmu_buf_rele(ds->ds_dbuf, tag);
4886689Smaybee 		return (ENOENT);
4896689Smaybee 	}
4906689Smaybee 	mutex_exit(&ds->ds_lock);
4916689Smaybee 	*dsp = ds;
4926689Smaybee 	return (0);
4936689Smaybee }
4946689Smaybee 
4956689Smaybee static int
4966689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag)
4976689Smaybee {
4986689Smaybee 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
4996689Smaybee 
5006689Smaybee 	/*
5016689Smaybee 	 * In syncing context we don't want the rwlock lock: there
5026689Smaybee 	 * may be an existing writer waiting for sync phase to
5036689Smaybee 	 * finish.  We don't need to worry about such writers, since
5046689Smaybee 	 * sync phase is single-threaded, so the writer can't be
5056689Smaybee 	 * doing anything while we are active.
5066689Smaybee 	 */
5076689Smaybee 	if (dsl_pool_sync_context(dp)) {
5086689Smaybee 		ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
5096689Smaybee 		return (0);
510789Sahrens 	}
5116689Smaybee 
5126689Smaybee 	/*
5136689Smaybee 	 * Normal users will hold the ds_rwlock as a READER until they
5146689Smaybee 	 * are finished (i.e., call dsl_dataset_rele()).  "Owners" will
5156689Smaybee 	 * drop their READER lock after they set the ds_owner field.
5166689Smaybee 	 *
5176689Smaybee 	 * If the dataset is being destroyed, the destroy thread will
5186689Smaybee 	 * obtain a WRITER lock for exclusive access after it's done its
5196689Smaybee 	 * open-context work and then change the ds_owner to
5206689Smaybee 	 * dsl_reaper once destruction is assured.  So threads
5216689Smaybee 	 * may block here temporarily, until the "destructability" of
5226689Smaybee 	 * the dataset is determined.
5236689Smaybee 	 */
5246689Smaybee 	ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock));
5256689Smaybee 	mutex_enter(&ds->ds_lock);
5266689Smaybee 	while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) {
5276689Smaybee 		rw_exit(&dp->dp_config_rwlock);
5286689Smaybee 		cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock);
5296689Smaybee 		if (DSL_DATASET_IS_DESTROYED(ds)) {
5306689Smaybee 			mutex_exit(&ds->ds_lock);
5316689Smaybee 			dsl_dataset_drop_ref(ds, tag);
5326689Smaybee 			rw_enter(&dp->dp_config_rwlock, RW_READER);
5336689Smaybee 			return (ENOENT);
5346689Smaybee 		}
53511546SChris.Kirby@sun.com 		/*
53611546SChris.Kirby@sun.com 		 * The dp_config_rwlock lives above the ds_lock. And
53711546SChris.Kirby@sun.com 		 * we need to check DSL_DATASET_IS_DESTROYED() while
53811546SChris.Kirby@sun.com 		 * holding the ds_lock, so we have to drop and reacquire
53911546SChris.Kirby@sun.com 		 * the ds_lock here.
54011546SChris.Kirby@sun.com 		 */
54111546SChris.Kirby@sun.com 		mutex_exit(&ds->ds_lock);
5426689Smaybee 		rw_enter(&dp->dp_config_rwlock, RW_READER);
54311546SChris.Kirby@sun.com 		mutex_enter(&ds->ds_lock);
5446689Smaybee 	}
545789Sahrens 	mutex_exit(&ds->ds_lock);
5461544Seschrock 	return (0);
547789Sahrens }
548789Sahrens 
549789Sahrens int
5506689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
5516689Smaybee     dsl_dataset_t **dsp)
5526689Smaybee {
5536689Smaybee 	int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp);
5546689Smaybee 
5556689Smaybee 	if (err)
5566689Smaybee 		return (err);
5576689Smaybee 	return (dsl_dataset_hold_ref(*dsp, tag));
5586689Smaybee }
5596689Smaybee 
5606689Smaybee int
56110298SMatthew.Ahrens@Sun.COM dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, boolean_t inconsistentok,
56210298SMatthew.Ahrens@Sun.COM     void *tag, dsl_dataset_t **dsp)
5636689Smaybee {
56410298SMatthew.Ahrens@Sun.COM 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
5656689Smaybee 	if (err)
5666689Smaybee 		return (err);
56710298SMatthew.Ahrens@Sun.COM 	if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
56810298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(*dsp, tag);
5698779SMark.Musante@Sun.COM 		*dsp = NULL;
5706689Smaybee 		return (EBUSY);
5716689Smaybee 	}
5726689Smaybee 	return (0);
5736689Smaybee }
5746689Smaybee 
5756689Smaybee int
5766689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp)
577789Sahrens {
578789Sahrens 	dsl_dir_t *dd;
579789Sahrens 	dsl_pool_t *dp;
5806689Smaybee 	const char *snapname;
581789Sahrens 	uint64_t obj;
582789Sahrens 	int err = 0;
583789Sahrens 
5846689Smaybee 	err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname);
5851544Seschrock 	if (err)
5861544Seschrock 		return (err);
587789Sahrens 
588789Sahrens 	dp = dd->dd_pool;
589789Sahrens 	obj = dd->dd_phys->dd_head_dataset_obj;
590789Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
5916689Smaybee 	if (obj)
5926689Smaybee 		err = dsl_dataset_get_ref(dp, obj, tag, dsp);
5936689Smaybee 	else
594789Sahrens 		err = ENOENT;
5956689Smaybee 	if (err)
596789Sahrens 		goto out;
5976689Smaybee 
5986689Smaybee 	err = dsl_dataset_hold_ref(*dsp, tag);
5996689Smaybee 
6006689Smaybee 	/* we may be looking for a snapshot */
6016689Smaybee 	if (err == 0 && snapname != NULL) {
6026689Smaybee 		dsl_dataset_t *ds = NULL;
6036689Smaybee 
6046689Smaybee 		if (*snapname++ != '@') {
6056689Smaybee 			dsl_dataset_rele(*dsp, tag);
606789Sahrens 			err = ENOENT;
607789Sahrens 			goto out;
608789Sahrens 		}
6096689Smaybee 
6106689Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
6116689Smaybee 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
6126689Smaybee 		if (err == 0)
6136689Smaybee 			err = dsl_dataset_get_ref(dp, obj, tag, &ds);
6146689Smaybee 		dsl_dataset_rele(*dsp, tag);
6156689Smaybee 
6166689Smaybee 		ASSERT3U((err == 0), ==, (ds != NULL));
6176689Smaybee 
6186689Smaybee 		if (ds) {
6196689Smaybee 			mutex_enter(&ds->ds_lock);
6206689Smaybee 			if (ds->ds_snapname[0] == 0)
6216689Smaybee 				(void) strlcpy(ds->ds_snapname, snapname,
6226689Smaybee 				    sizeof (ds->ds_snapname));
6236689Smaybee 			mutex_exit(&ds->ds_lock);
6246689Smaybee 			err = dsl_dataset_hold_ref(ds, tag);
6256689Smaybee 			*dsp = err ? NULL : ds;
626789Sahrens 		}
627789Sahrens 	}
628789Sahrens out:
629789Sahrens 	rw_exit(&dp->dp_config_rwlock);
630789Sahrens 	dsl_dir_close(dd, FTAG);
631789Sahrens 	return (err);
632789Sahrens }
633789Sahrens 
634789Sahrens int
63510298SMatthew.Ahrens@Sun.COM dsl_dataset_own(const char *name, boolean_t inconsistentok,
63610298SMatthew.Ahrens@Sun.COM     void *tag, dsl_dataset_t **dsp)
637789Sahrens {
63810298SMatthew.Ahrens@Sun.COM 	int err = dsl_dataset_hold(name, tag, dsp);
6396689Smaybee 	if (err)
6406689Smaybee 		return (err);
64110298SMatthew.Ahrens@Sun.COM 	if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
64210298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(*dsp, tag);
6436689Smaybee 		return (EBUSY);
6446689Smaybee 	}
6456689Smaybee 	return (0);
646789Sahrens }
647789Sahrens 
648789Sahrens void
649789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
650789Sahrens {
651789Sahrens 	if (ds == NULL) {
652789Sahrens 		(void) strcpy(name, "mos");
653789Sahrens 	} else {
654789Sahrens 		dsl_dir_name(ds->ds_dir, name);
6551544Seschrock 		VERIFY(0 == dsl_dataset_get_snapname(ds));
656789Sahrens 		if (ds->ds_snapname[0]) {
657789Sahrens 			(void) strcat(name, "@");
6586689Smaybee 			/*
6596689Smaybee 			 * We use a "recursive" mutex so that we
6606689Smaybee 			 * can call dprintf_ds() with ds_lock held.
6616689Smaybee 			 */
662789Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
663789Sahrens 				mutex_enter(&ds->ds_lock);
664789Sahrens 				(void) strcat(name, ds->ds_snapname);
665789Sahrens 				mutex_exit(&ds->ds_lock);
666789Sahrens 			} else {
667789Sahrens 				(void) strcat(name, ds->ds_snapname);
668789Sahrens 			}
669789Sahrens 		}
670789Sahrens 	}
671789Sahrens }
672789Sahrens 
6733978Smmusante static int
6743978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds)
6753978Smmusante {
6763978Smmusante 	int result;
6773978Smmusante 
6783978Smmusante 	if (ds == NULL) {
6793978Smmusante 		result = 3;	/* "mos" */
6803978Smmusante 	} else {
6813978Smmusante 		result = dsl_dir_namelen(ds->ds_dir);
6823978Smmusante 		VERIFY(0 == dsl_dataset_get_snapname(ds));
6833978Smmusante 		if (ds->ds_snapname[0]) {
6843978Smmusante 			++result;	/* adding one for the @-sign */
6853978Smmusante 			if (!MUTEX_HELD(&ds->ds_lock)) {
6863978Smmusante 				mutex_enter(&ds->ds_lock);
6873978Smmusante 				result += strlen(ds->ds_snapname);
6883978Smmusante 				mutex_exit(&ds->ds_lock);
6893978Smmusante 			} else {
6903978Smmusante 				result += strlen(ds->ds_snapname);
6913978Smmusante 			}
6923978Smmusante 		}
6933978Smmusante 	}
6943978Smmusante 
6953978Smmusante 	return (result);
6963978Smmusante }
6973978Smmusante 
6987046Sahrens void
6996689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag)
700789Sahrens {
7011544Seschrock 	dmu_buf_rele(ds->ds_dbuf, tag);
702789Sahrens }
703789Sahrens 
704789Sahrens void
7056689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
7065367Sahrens {
7076689Smaybee 	if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) {
7086689Smaybee 		rw_exit(&ds->ds_rwlock);
7096689Smaybee 	}
7106689Smaybee 	dsl_dataset_drop_ref(ds, tag);
7116689Smaybee }
7126689Smaybee 
7136689Smaybee void
71410298SMatthew.Ahrens@Sun.COM dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
7156689Smaybee {
71610298SMatthew.Ahrens@Sun.COM 	ASSERT((ds->ds_owner == tag && ds->ds_dbuf) ||
7176689Smaybee 	    (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL));
7186689Smaybee 
7195367Sahrens 	mutex_enter(&ds->ds_lock);
7206689Smaybee 	ds->ds_owner = NULL;
7216689Smaybee 	if (RW_WRITE_HELD(&ds->ds_rwlock)) {
7226689Smaybee 		rw_exit(&ds->ds_rwlock);
7236689Smaybee 		cv_broadcast(&ds->ds_exclusive_cv);
7246689Smaybee 	}
7255367Sahrens 	mutex_exit(&ds->ds_lock);
7266689Smaybee 	if (ds->ds_dbuf)
72710298SMatthew.Ahrens@Sun.COM 		dsl_dataset_drop_ref(ds, tag);
7286689Smaybee 	else
7296689Smaybee 		dsl_dataset_evict(ds->ds_dbuf, ds);
7305367Sahrens }
7315367Sahrens 
7325367Sahrens boolean_t
73310298SMatthew.Ahrens@Sun.COM dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *tag)
7345367Sahrens {
7356689Smaybee 	boolean_t gotit = FALSE;
7366689Smaybee 
7375367Sahrens 	mutex_enter(&ds->ds_lock);
7386689Smaybee 	if (ds->ds_owner == NULL &&
7396689Smaybee 	    (!DS_IS_INCONSISTENT(ds) || inconsistentok)) {
74010298SMatthew.Ahrens@Sun.COM 		ds->ds_owner = tag;
7416689Smaybee 		if (!dsl_pool_sync_context(ds->ds_dir->dd_pool))
7426689Smaybee 			rw_exit(&ds->ds_rwlock);
7436689Smaybee 		gotit = TRUE;
7445367Sahrens 	}
7455367Sahrens 	mutex_exit(&ds->ds_lock);
7466689Smaybee 	return (gotit);
7476689Smaybee }
7486689Smaybee 
7496689Smaybee void
7506689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner)
7516689Smaybee {
7526689Smaybee 	ASSERT3P(owner, ==, ds->ds_owner);
7536689Smaybee 	if (!RW_WRITE_HELD(&ds->ds_rwlock))
7546689Smaybee 		rw_enter(&ds->ds_rwlock, RW_WRITER);
7555367Sahrens }
7565367Sahrens 
7572199Sahrens uint64_t
7587046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
7596492Stimh     uint64_t flags, dmu_tx_t *tx)
760789Sahrens {
7615367Sahrens 	dsl_pool_t *dp = dd->dd_pool;
762789Sahrens 	dmu_buf_t *dbuf;
763789Sahrens 	dsl_dataset_phys_t *dsphys;
7645367Sahrens 	uint64_t dsobj;
765789Sahrens 	objset_t *mos = dp->dp_meta_objset;
766789Sahrens 
7677046Sahrens 	if (origin == NULL)
7687046Sahrens 		origin = dp->dp_origin_snap;
7697046Sahrens 
7705367Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
7715367Sahrens 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
772789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7735367Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
774789Sahrens 
775928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
776928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
7771544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
778789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
779789Sahrens 	dsphys = dbuf->db_data;
7806689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
781789Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
7826492Stimh 	dsphys->ds_flags = flags;
783789Sahrens 	dsphys->ds_fsid_guid = unique_create();
784789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
785789Sahrens 	    sizeof (dsphys->ds_guid));
786789Sahrens 	dsphys->ds_snapnames_zapobj =
7876492Stimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
7886492Stimh 	    DMU_OT_NONE, 0, tx);
789789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
7907046Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
791789Sahrens 	dsphys->ds_deadlist_obj =
792789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
7935378Sck153898 
7945367Sahrens 	if (origin) {
7955367Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
796789Sahrens 		dsphys->ds_prev_snap_txg =
7975367Sahrens 		    origin->ds_phys->ds_creation_txg;
798789Sahrens 		dsphys->ds_used_bytes =
7995367Sahrens 		    origin->ds_phys->ds_used_bytes;
800789Sahrens 		dsphys->ds_compressed_bytes =
8015367Sahrens 		    origin->ds_phys->ds_compressed_bytes;
802789Sahrens 		dsphys->ds_uncompressed_bytes =
8035367Sahrens 		    origin->ds_phys->ds_uncompressed_bytes;
8045367Sahrens 		dsphys->ds_bp = origin->ds_phys->ds_bp;
8056502Stimh 		dsphys->ds_flags |= origin->ds_phys->ds_flags;
806789Sahrens 
8075367Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
8085367Sahrens 		origin->ds_phys->ds_num_children++;
809789Sahrens 
8107046Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
8117046Sahrens 			if (origin->ds_phys->ds_next_clones_obj == 0) {
8127046Sahrens 				origin->ds_phys->ds_next_clones_obj =
8137046Sahrens 				    zap_create(mos,
8147046Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
8157046Sahrens 			}
8167046Sahrens 			VERIFY(0 == zap_add_int(mos,
8177046Sahrens 			    origin->ds_phys->ds_next_clones_obj,
8187046Sahrens 			    dsobj, tx));
8197046Sahrens 		}
8207046Sahrens 
821789Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
8225367Sahrens 		dd->dd_phys->dd_origin_obj = origin->ds_object;
823789Sahrens 	}
8246492Stimh 
8256492Stimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
8266492Stimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
8276492Stimh 
8281544Seschrock 	dmu_buf_rele(dbuf, FTAG);
829789Sahrens 
830789Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
831789Sahrens 	dd->dd_phys->dd_head_dataset_obj = dsobj;
8325367Sahrens 
8335367Sahrens 	return (dsobj);
8345367Sahrens }
8355367Sahrens 
8365367Sahrens uint64_t
8376492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
8386492Stimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
8395367Sahrens {
8405367Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
8415367Sahrens 	uint64_t dsobj, ddobj;
8425367Sahrens 	dsl_dir_t *dd;
8435367Sahrens 
8445367Sahrens 	ASSERT(lastname[0] != '@');
8455367Sahrens 
8467046Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
8475367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
8485367Sahrens 
8497046Sahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx);
8505367Sahrens 
8515367Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
8525367Sahrens 
853789Sahrens 	dsl_dir_close(dd, FTAG);
854789Sahrens 
8552199Sahrens 	return (dsobj);
8562199Sahrens }
8572199Sahrens 
8582199Sahrens struct destroyarg {
8592199Sahrens 	dsl_sync_task_group_t *dstg;
8602199Sahrens 	char *snapname;
8612199Sahrens 	char *failed;
86210242Schris.kirby@sun.com 	boolean_t defer;
8632199Sahrens };
8642199Sahrens 
8652199Sahrens static int
86611209SMatthew.Ahrens@Sun.COM dsl_snapshot_destroy_one(const char *name, void *arg)
8672199Sahrens {
8682199Sahrens 	struct destroyarg *da = arg;
8692199Sahrens 	dsl_dataset_t *ds;
8702199Sahrens 	int err;
87110242Schris.kirby@sun.com 	char *dsname;
87210272SMatthew.Ahrens@Sun.COM 
87310272SMatthew.Ahrens@Sun.COM 	dsname = kmem_asprintf("%s@%s", name, da->snapname);
87410298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_own(dsname, B_TRUE, da->dstg, &ds);
87510272SMatthew.Ahrens@Sun.COM 	strfree(dsname);
8766689Smaybee 	if (err == 0) {
87710242Schris.kirby@sun.com 		struct dsl_ds_destroyarg *dsda;
87810242Schris.kirby@sun.com 
8796689Smaybee 		dsl_dataset_make_exclusive(ds, da->dstg);
88010242Schris.kirby@sun.com 		dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg), KM_SLEEP);
88110242Schris.kirby@sun.com 		dsda->ds = ds;
88210242Schris.kirby@sun.com 		dsda->defer = da->defer;
8836689Smaybee 		dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check,
88410242Schris.kirby@sun.com 		    dsl_dataset_destroy_sync, dsda, da->dstg, 0);
8856689Smaybee 	} else if (err == ENOENT) {
8866689Smaybee 		err = 0;
8876689Smaybee 	} else {
8882199Sahrens 		(void) strcpy(da->failed, name);
8892199Sahrens 	}
8906689Smaybee 	return (err);
891789Sahrens }
892789Sahrens 
8932199Sahrens /*
8942199Sahrens  * Destroy 'snapname' in all descendants of 'fsname'.
8952199Sahrens  */
8962199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy
8972199Sahrens int
89810242Schris.kirby@sun.com dsl_snapshots_destroy(char *fsname, char *snapname, boolean_t defer)
8992199Sahrens {
9002199Sahrens 	int err;
9012199Sahrens 	struct destroyarg da;
9022199Sahrens 	dsl_sync_task_t *dst;
9032199Sahrens 	spa_t *spa;
9042199Sahrens 
9054603Sahrens 	err = spa_open(fsname, &spa, FTAG);
9062199Sahrens 	if (err)
9072199Sahrens 		return (err);
9082199Sahrens 	da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
9092199Sahrens 	da.snapname = snapname;
9102199Sahrens 	da.failed = fsname;
91110242Schris.kirby@sun.com 	da.defer = defer;
9122199Sahrens 
9132199Sahrens 	err = dmu_objset_find(fsname,
9142417Sahrens 	    dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN);
9152199Sahrens 
9162199Sahrens 	if (err == 0)
9172199Sahrens 		err = dsl_sync_task_group_wait(da.dstg);
9182199Sahrens 
9192199Sahrens 	for (dst = list_head(&da.dstg->dstg_tasks); dst;
9202199Sahrens 	    dst = list_next(&da.dstg->dstg_tasks, dst)) {
92110242Schris.kirby@sun.com 		struct dsl_ds_destroyarg *dsda = dst->dst_arg1;
92210242Schris.kirby@sun.com 		dsl_dataset_t *ds = dsda->ds;
92310242Schris.kirby@sun.com 
9246689Smaybee 		/*
9256689Smaybee 		 * Return the file system name that triggered the error
9266689Smaybee 		 */
9272199Sahrens 		if (dst->dst_err) {
9282199Sahrens 			dsl_dataset_name(ds, fsname);
9294603Sahrens 			*strchr(fsname, '@') = '\0';
9302199Sahrens 		}
93110242Schris.kirby@sun.com 		ASSERT3P(dsda->rm_origin, ==, NULL);
9326689Smaybee 		dsl_dataset_disown(ds, da.dstg);
93310242Schris.kirby@sun.com 		kmem_free(dsda, sizeof (struct dsl_ds_destroyarg));
9342199Sahrens 	}
9352199Sahrens 
9362199Sahrens 	dsl_sync_task_group_destroy(da.dstg);
9372199Sahrens 	spa_close(spa, FTAG);
9382199Sahrens 	return (err);
9392199Sahrens }
9402199Sahrens 
94110242Schris.kirby@sun.com static boolean_t
94210242Schris.kirby@sun.com dsl_dataset_might_destroy_origin(dsl_dataset_t *ds)
94310242Schris.kirby@sun.com {
94410242Schris.kirby@sun.com 	boolean_t might_destroy = B_FALSE;
94510242Schris.kirby@sun.com 
94610242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
94710242Schris.kirby@sun.com 	if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 &&
94810242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds))
94910242Schris.kirby@sun.com 		might_destroy = B_TRUE;
95010242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
95110242Schris.kirby@sun.com 
95210242Schris.kirby@sun.com 	return (might_destroy);
95310242Schris.kirby@sun.com }
95410242Schris.kirby@sun.com 
95510242Schris.kirby@sun.com /*
95610242Schris.kirby@sun.com  * If we're removing a clone, and these three conditions are true:
95710242Schris.kirby@sun.com  *	1) the clone's origin has no other children
95810242Schris.kirby@sun.com  *	2) the clone's origin has no user references
95910242Schris.kirby@sun.com  *	3) the clone's origin has been marked for deferred destruction
96010242Schris.kirby@sun.com  * Then, prepare to remove the origin as part of this sync task group.
96110242Schris.kirby@sun.com  */
96210242Schris.kirby@sun.com static int
96310242Schris.kirby@sun.com dsl_dataset_origin_rm_prep(struct dsl_ds_destroyarg *dsda, void *tag)
96410242Schris.kirby@sun.com {
96510242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
96610242Schris.kirby@sun.com 	dsl_dataset_t *origin = ds->ds_prev;
96710242Schris.kirby@sun.com 
96810242Schris.kirby@sun.com 	if (dsl_dataset_might_destroy_origin(origin)) {
96910242Schris.kirby@sun.com 		char *name;
97010242Schris.kirby@sun.com 		int namelen;
97110242Schris.kirby@sun.com 		int error;
97210242Schris.kirby@sun.com 
97310242Schris.kirby@sun.com 		namelen = dsl_dataset_namelen(origin) + 1;
97410242Schris.kirby@sun.com 		name = kmem_alloc(namelen, KM_SLEEP);
97510242Schris.kirby@sun.com 		dsl_dataset_name(origin, name);
97610242Schris.kirby@sun.com #ifdef _KERNEL
97710242Schris.kirby@sun.com 		error = zfs_unmount_snap(name, NULL);
97810242Schris.kirby@sun.com 		if (error) {
97910242Schris.kirby@sun.com 			kmem_free(name, namelen);
98010242Schris.kirby@sun.com 			return (error);
98110242Schris.kirby@sun.com 		}
98210242Schris.kirby@sun.com #endif
98310298SMatthew.Ahrens@Sun.COM 		error = dsl_dataset_own(name, B_TRUE, tag, &origin);
98410242Schris.kirby@sun.com 		kmem_free(name, namelen);
98510242Schris.kirby@sun.com 		if (error)
98610242Schris.kirby@sun.com 			return (error);
98710242Schris.kirby@sun.com 		dsda->rm_origin = origin;
98810242Schris.kirby@sun.com 		dsl_dataset_make_exclusive(origin, tag);
98910242Schris.kirby@sun.com 	}
99010242Schris.kirby@sun.com 
99110242Schris.kirby@sun.com 	return (0);
99210242Schris.kirby@sun.com }
99310242Schris.kirby@sun.com 
9945367Sahrens /*
9956689Smaybee  * ds must be opened as OWNER.  On return (whether successful or not),
9966689Smaybee  * ds will be closed and caller can no longer dereference it.
9975367Sahrens  */
998789Sahrens int
99910242Schris.kirby@sun.com dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer)
1000789Sahrens {
1001789Sahrens 	int err;
10022199Sahrens 	dsl_sync_task_group_t *dstg;
10032199Sahrens 	objset_t *os;
1004789Sahrens 	dsl_dir_t *dd;
10052199Sahrens 	uint64_t obj;
100611022STom.Erickson@Sun.COM 	struct dsl_ds_destroyarg dsda = { 0 };
100711022STom.Erickson@Sun.COM 	dsl_dataset_t dummy_ds = { 0 };
100810242Schris.kirby@sun.com 
100910242Schris.kirby@sun.com 	dsda.ds = ds;
10102199Sahrens 
10115367Sahrens 	if (dsl_dataset_is_snapshot(ds)) {
10122199Sahrens 		/* Destroying a snapshot is simpler */
10136689Smaybee 		dsl_dataset_make_exclusive(ds, tag);
10147237Sek110237 
101510242Schris.kirby@sun.com 		dsda.defer = defer;
10162199Sahrens 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
10172199Sahrens 		    dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
101810242Schris.kirby@sun.com 		    &dsda, tag, 0);
101910242Schris.kirby@sun.com 		ASSERT3P(dsda.rm_origin, ==, NULL);
10205367Sahrens 		goto out;
102110385Schris.kirby@sun.com 	} else if (defer) {
102210385Schris.kirby@sun.com 		err = EINVAL;
102310385Schris.kirby@sun.com 		goto out;
10242199Sahrens 	}
10252199Sahrens 
10262199Sahrens 	dd = ds->ds_dir;
102711022STom.Erickson@Sun.COM 	dummy_ds.ds_dir = dd;
102811022STom.Erickson@Sun.COM 	dummy_ds.ds_object = ds->ds_object;
1029789Sahrens 
10302199Sahrens 	/*
10312199Sahrens 	 * Check for errors and mark this ds as inconsistent, in
10322199Sahrens 	 * case we crash while freeing the objects.
10332199Sahrens 	 */
10342199Sahrens 	err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
10352199Sahrens 	    dsl_dataset_destroy_begin_sync, ds, NULL, 0);
10365367Sahrens 	if (err)
10375367Sahrens 		goto out;
10385367Sahrens 
103910298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, &os);
10405367Sahrens 	if (err)
10415367Sahrens 		goto out;
10422199Sahrens 
10432199Sahrens 	/*
10442199Sahrens 	 * remove the objects in open context, so that we won't
10452199Sahrens 	 * have too much to do in syncing context.
10462199Sahrens 	 */
10473025Sahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
10483025Sahrens 	    ds->ds_phys->ds_prev_snap_txg)) {
10496992Smaybee 		/*
10506992Smaybee 		 * Ignore errors, if there is not enough disk space
10516992Smaybee 		 * we will deal with it in dsl_dataset_destroy_sync().
10526992Smaybee 		 */
10536992Smaybee 		(void) dmu_free_object(os, obj);
10542199Sahrens 	}
10552199Sahrens 
10569396SMatthew.Ahrens@Sun.COM 	/*
10579396SMatthew.Ahrens@Sun.COM 	 * We need to sync out all in-flight IO before we try to evict
10589396SMatthew.Ahrens@Sun.COM 	 * (the dataset evict func is trying to clear the cached entries
10599396SMatthew.Ahrens@Sun.COM 	 * for this dataset in the ARC).
10609396SMatthew.Ahrens@Sun.COM 	 */
10619396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dd->dd_pool, 0);
10629396SMatthew.Ahrens@Sun.COM 
10639396SMatthew.Ahrens@Sun.COM 	/*
10649396SMatthew.Ahrens@Sun.COM 	 * If we managed to free all the objects in open
10659396SMatthew.Ahrens@Sun.COM 	 * context, the user space accounting should be zero.
10669396SMatthew.Ahrens@Sun.COM 	 */
10679396SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_bp.blk_fill == 0 &&
106810298SMatthew.Ahrens@Sun.COM 	    dmu_objset_userused_enabled(os)) {
10699396SMatthew.Ahrens@Sun.COM 		uint64_t count;
10709396SMatthew.Ahrens@Sun.COM 
10719396SMatthew.Ahrens@Sun.COM 		ASSERT(zap_count(os, DMU_USERUSED_OBJECT, &count) != 0 ||
10729396SMatthew.Ahrens@Sun.COM 		    count == 0);
10739396SMatthew.Ahrens@Sun.COM 		ASSERT(zap_count(os, DMU_GROUPUSED_OBJECT, &count) != 0 ||
10749396SMatthew.Ahrens@Sun.COM 		    count == 0);
10759396SMatthew.Ahrens@Sun.COM 	}
10769396SMatthew.Ahrens@Sun.COM 
10772199Sahrens 	if (err != ESRCH)
10785367Sahrens 		goto out;
10792199Sahrens 
10806975Smaybee 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
10816975Smaybee 	err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd);
10826975Smaybee 	rw_exit(&dd->dd_pool->dp_config_rwlock);
10836975Smaybee 
10846975Smaybee 	if (err)
10856975Smaybee 		goto out;
10866975Smaybee 
10872199Sahrens 	/*
10882199Sahrens 	 * Blow away the dsl_dir + head dataset.
10892199Sahrens 	 */
10906689Smaybee 	dsl_dataset_make_exclusive(ds, tag);
109110242Schris.kirby@sun.com 	/*
109210242Schris.kirby@sun.com 	 * If we're removing a clone, we might also need to remove its
109310242Schris.kirby@sun.com 	 * origin.
109410242Schris.kirby@sun.com 	 */
109510242Schris.kirby@sun.com 	do {
109610242Schris.kirby@sun.com 		dsda.need_prep = B_FALSE;
109710242Schris.kirby@sun.com 		if (dsl_dir_is_clone(dd)) {
109810242Schris.kirby@sun.com 			err = dsl_dataset_origin_rm_prep(&dsda, tag);
109910242Schris.kirby@sun.com 			if (err) {
110010242Schris.kirby@sun.com 				dsl_dir_close(dd, FTAG);
110110242Schris.kirby@sun.com 				goto out;
110210242Schris.kirby@sun.com 			}
110310242Schris.kirby@sun.com 		}
110410242Schris.kirby@sun.com 
110510242Schris.kirby@sun.com 		dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
110610242Schris.kirby@sun.com 		dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
110710242Schris.kirby@sun.com 		    dsl_dataset_destroy_sync, &dsda, tag, 0);
110810242Schris.kirby@sun.com 		dsl_sync_task_create(dstg, dsl_dir_destroy_check,
110911022STom.Erickson@Sun.COM 		    dsl_dir_destroy_sync, &dummy_ds, FTAG, 0);
111010242Schris.kirby@sun.com 		err = dsl_sync_task_group_wait(dstg);
111110242Schris.kirby@sun.com 		dsl_sync_task_group_destroy(dstg);
111210242Schris.kirby@sun.com 
111310242Schris.kirby@sun.com 		/*
111410242Schris.kirby@sun.com 		 * We could be racing against 'zfs release' or 'zfs destroy -d'
111510242Schris.kirby@sun.com 		 * on the origin snap, in which case we can get EBUSY if we
111610242Schris.kirby@sun.com 		 * needed to destroy the origin snap but were not ready to
111710242Schris.kirby@sun.com 		 * do so.
111810242Schris.kirby@sun.com 		 */
111910242Schris.kirby@sun.com 		if (dsda.need_prep) {
112010242Schris.kirby@sun.com 			ASSERT(err == EBUSY);
112110242Schris.kirby@sun.com 			ASSERT(dsl_dir_is_clone(dd));
112210242Schris.kirby@sun.com 			ASSERT(dsda.rm_origin == NULL);
112310242Schris.kirby@sun.com 		}
112410242Schris.kirby@sun.com 	} while (dsda.need_prep);
112510242Schris.kirby@sun.com 
112610242Schris.kirby@sun.com 	if (dsda.rm_origin != NULL)
112710242Schris.kirby@sun.com 		dsl_dataset_disown(dsda.rm_origin, tag);
112810242Schris.kirby@sun.com 
11296689Smaybee 	/* if it is successful, dsl_dir_destroy_sync will close the dd */
11305367Sahrens 	if (err)
11312199Sahrens 		dsl_dir_close(dd, FTAG);
11325367Sahrens out:
11336689Smaybee 	dsl_dataset_disown(ds, tag);
1134789Sahrens 	return (err);
1135789Sahrens }
1136789Sahrens 
11373547Smaybee blkptr_t *
11383547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1139789Sahrens {
11403547Smaybee 	return (&ds->ds_phys->ds_bp);
1141789Sahrens }
1142789Sahrens 
1143789Sahrens void
1144789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1145789Sahrens {
1146789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1147789Sahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
1148789Sahrens 	if (ds == NULL) {
1149789Sahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
1150789Sahrens 	} else {
1151789Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1152789Sahrens 		ds->ds_phys->ds_bp = *bp;
1153789Sahrens 	}
1154789Sahrens }
1155789Sahrens 
1156789Sahrens spa_t *
1157789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
1158789Sahrens {
1159789Sahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1160789Sahrens }
1161789Sahrens 
1162789Sahrens void
1163789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1164789Sahrens {
1165789Sahrens 	dsl_pool_t *dp;
1166789Sahrens 
1167789Sahrens 	if (ds == NULL) /* this is the meta-objset */
1168789Sahrens 		return;
1169789Sahrens 
117010298SMatthew.Ahrens@Sun.COM 	ASSERT(ds->ds_objset != NULL);
11712885Sahrens 
11722885Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
11732885Sahrens 		panic("dirtying snapshot!");
1174789Sahrens 
1175789Sahrens 	dp = ds->ds_dir->dd_pool;
1176789Sahrens 
1177789Sahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
1178789Sahrens 		/* up the hold count until we can be written out */
1179789Sahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1180789Sahrens 	}
1181789Sahrens }
1182789Sahrens 
11835378Sck153898 /*
11845378Sck153898  * The unique space in the head dataset can be calculated by subtracting
11855378Sck153898  * the space used in the most recent snapshot, that is still being used
11865378Sck153898  * in this file system, from the space currently in use.  To figure out
11875378Sck153898  * the space in the most recent snapshot still in use, we need to take
11885378Sck153898  * the total space used in the snapshot and subtract out the space that
11895378Sck153898  * has been freed up since the snapshot was taken.
11905378Sck153898  */
11915378Sck153898 static void
11925378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
11935378Sck153898 {
11945378Sck153898 	uint64_t mrs_used;
11955378Sck153898 	uint64_t dlused, dlcomp, dluncomp;
11965378Sck153898 
119712296SLin.Ling@Sun.COM 	ASSERT(!dsl_dataset_is_snapshot(ds));
11985378Sck153898 
11995378Sck153898 	if (ds->ds_phys->ds_prev_snap_obj != 0)
12005378Sck153898 		mrs_used = ds->ds_prev->ds_phys->ds_used_bytes;
12015378Sck153898 	else
12025378Sck153898 		mrs_used = 0;
12035378Sck153898 
12045378Sck153898 	VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp,
12055378Sck153898 	    &dluncomp));
12065378Sck153898 
12075378Sck153898 	ASSERT3U(dlused, <=, mrs_used);
12085378Sck153898 	ds->ds_phys->ds_unique_bytes =
12095378Sck153898 	    ds->ds_phys->ds_used_bytes - (mrs_used - dlused);
12105378Sck153898 
121112296SLin.Ling@Sun.COM 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
12125378Sck153898 	    SPA_VERSION_UNIQUE_ACCURATE)
12135378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
12145378Sck153898 }
12155378Sck153898 
1216789Sahrens struct killarg {
12177390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
1218789Sahrens 	dmu_tx_t *tx;
1219789Sahrens };
1220789Sahrens 
12217390SMatthew.Ahrens@Sun.COM /* ARGSUSED */
1222789Sahrens static int
122312296SLin.Ling@Sun.COM kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
122410922SJeff.Bonwick@Sun.COM     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1225789Sahrens {
1226789Sahrens 	struct killarg *ka = arg;
122710922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx = ka->tx;
12287837SMatthew.Ahrens@Sun.COM 
12297837SMatthew.Ahrens@Sun.COM 	if (bp == NULL)
12307837SMatthew.Ahrens@Sun.COM 		return (0);
1231789Sahrens 
123210922SJeff.Bonwick@Sun.COM 	if (zb->zb_level == ZB_ZIL_LEVEL) {
123310922SJeff.Bonwick@Sun.COM 		ASSERT(zilog != NULL);
12348746SMatthew.Ahrens@Sun.COM 		/*
12358746SMatthew.Ahrens@Sun.COM 		 * It's a block in the intent log.  It has no
12368746SMatthew.Ahrens@Sun.COM 		 * accounting, so just free it.
12378746SMatthew.Ahrens@Sun.COM 		 */
123810922SJeff.Bonwick@Sun.COM 		dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
12398746SMatthew.Ahrens@Sun.COM 	} else {
124010922SJeff.Bonwick@Sun.COM 		ASSERT(zilog == NULL);
12418746SMatthew.Ahrens@Sun.COM 		ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
124210922SJeff.Bonwick@Sun.COM 		(void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
12438746SMatthew.Ahrens@Sun.COM 	}
12447390SMatthew.Ahrens@Sun.COM 
1245789Sahrens 	return (0);
1246789Sahrens }
1247789Sahrens 
1248789Sahrens /* ARGSUSED */
12492199Sahrens static int
12502199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
12511731Sbonwick {
12522199Sahrens 	dsl_dataset_t *ds = arg1;
12535367Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
12545367Sahrens 	uint64_t count;
12555367Sahrens 	int err;
12561731Sbonwick 
12571731Sbonwick 	/*
12581731Sbonwick 	 * Can't delete a head dataset if there are snapshots of it.
12591731Sbonwick 	 * (Except if the only snapshots are from the branch we cloned
12601731Sbonwick 	 * from.)
12611731Sbonwick 	 */
12621731Sbonwick 	if (ds->ds_prev != NULL &&
12631731Sbonwick 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
126410816SVitezslav.Batrla@Sun.COM 		return (EBUSY);
12651731Sbonwick 
12665367Sahrens 	/*
12675367Sahrens 	 * This is really a dsl_dir thing, but check it here so that
12685367Sahrens 	 * we'll be less likely to leave this dataset inconsistent &
12695367Sahrens 	 * nearly destroyed.
12705367Sahrens 	 */
12715367Sahrens 	err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
12725367Sahrens 	if (err)
12735367Sahrens 		return (err);
12745367Sahrens 	if (count != 0)
12755367Sahrens 		return (EEXIST);
12765367Sahrens 
12771731Sbonwick 	return (0);
12781731Sbonwick }
12791731Sbonwick 
12802199Sahrens /* ARGSUSED */
12812199Sahrens static void
128212296SLin.Ling@Sun.COM dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1283789Sahrens {
12842199Sahrens 	dsl_dataset_t *ds = arg1;
12854543Smarks 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1286789Sahrens 
12872199Sahrens 	/* Mark it as inconsistent on-disk, in case we crash */
12882199Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
12892199Sahrens 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
12904543Smarks 
129112296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
129212296SLin.Ling@Sun.COM 	    "dataset = %llu", ds->ds_object);
12932199Sahrens }
1294789Sahrens 
129510242Schris.kirby@sun.com static int
129610242Schris.kirby@sun.com dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag,
129710242Schris.kirby@sun.com     dmu_tx_t *tx)
129810242Schris.kirby@sun.com {
129910242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
130010242Schris.kirby@sun.com 	dsl_dataset_t *ds_prev = ds->ds_prev;
130110242Schris.kirby@sun.com 
130210242Schris.kirby@sun.com 	if (dsl_dataset_might_destroy_origin(ds_prev)) {
130310242Schris.kirby@sun.com 		struct dsl_ds_destroyarg ndsda = {0};
130410242Schris.kirby@sun.com 
130510242Schris.kirby@sun.com 		/*
130610242Schris.kirby@sun.com 		 * If we're not prepared to remove the origin, don't remove
130710242Schris.kirby@sun.com 		 * the clone either.
130810242Schris.kirby@sun.com 		 */
130910242Schris.kirby@sun.com 		if (dsda->rm_origin == NULL) {
131010242Schris.kirby@sun.com 			dsda->need_prep = B_TRUE;
131110242Schris.kirby@sun.com 			return (EBUSY);
131210242Schris.kirby@sun.com 		}
131310242Schris.kirby@sun.com 
131410242Schris.kirby@sun.com 		ndsda.ds = ds_prev;
131510242Schris.kirby@sun.com 		ndsda.is_origin_rm = B_TRUE;
131610242Schris.kirby@sun.com 		return (dsl_dataset_destroy_check(&ndsda, tag, tx));
131710242Schris.kirby@sun.com 	}
131810242Schris.kirby@sun.com 
131910242Schris.kirby@sun.com 	/*
132010242Schris.kirby@sun.com 	 * If we're not going to remove the origin after all,
132110242Schris.kirby@sun.com 	 * undo the open context setup.
132210242Schris.kirby@sun.com 	 */
132310242Schris.kirby@sun.com 	if (dsda->rm_origin != NULL) {
132410242Schris.kirby@sun.com 		dsl_dataset_disown(dsda->rm_origin, tag);
132510242Schris.kirby@sun.com 		dsda->rm_origin = NULL;
132610242Schris.kirby@sun.com 	}
132710242Schris.kirby@sun.com 
132810242Schris.kirby@sun.com 	return (0);
132910242Schris.kirby@sun.com }
133010242Schris.kirby@sun.com 
13312199Sahrens /* ARGSUSED */
13325367Sahrens int
13332199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
13342199Sahrens {
133510242Schris.kirby@sun.com 	struct dsl_ds_destroyarg *dsda = arg1;
133610242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
1337789Sahrens 
13386689Smaybee 	/* we have an owner hold, so noone else can destroy us */
13396689Smaybee 	ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
13406689Smaybee 
134110242Schris.kirby@sun.com 	/*
134210242Schris.kirby@sun.com 	 * Only allow deferred destroy on pools that support it.
134310242Schris.kirby@sun.com 	 * NOTE: deferred destroy is only supported on snapshots.
134410242Schris.kirby@sun.com 	 */
134510242Schris.kirby@sun.com 	if (dsda->defer) {
134610242Schris.kirby@sun.com 		if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
134710242Schris.kirby@sun.com 		    SPA_VERSION_USERREFS)
134810242Schris.kirby@sun.com 			return (ENOTSUP);
134910242Schris.kirby@sun.com 		ASSERT(dsl_dataset_is_snapshot(ds));
135010242Schris.kirby@sun.com 		return (0);
135110242Schris.kirby@sun.com 	}
1352789Sahrens 
1353789Sahrens 	/*
1354789Sahrens 	 * Can't delete a head dataset if there are snapshots of it.
1355789Sahrens 	 * (Except if the only snapshots are from the branch we cloned
1356789Sahrens 	 * from.)
1357789Sahrens 	 */
1358789Sahrens 	if (ds->ds_prev != NULL &&
13592199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
136010816SVitezslav.Batrla@Sun.COM 		return (EBUSY);
1361789Sahrens 
1362789Sahrens 	/*
1363789Sahrens 	 * If we made changes this txg, traverse_dsl_dataset won't find
1364789Sahrens 	 * them.  Try again.
1365789Sahrens 	 */
13662199Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1367789Sahrens 		return (EAGAIN);
13682199Sahrens 
136910242Schris.kirby@sun.com 	if (dsl_dataset_is_snapshot(ds)) {
137010242Schris.kirby@sun.com 		/*
137110242Schris.kirby@sun.com 		 * If this snapshot has an elevated user reference count,
137210242Schris.kirby@sun.com 		 * we can't destroy it yet.
137310242Schris.kirby@sun.com 		 */
137410242Schris.kirby@sun.com 		if (ds->ds_userrefs > 0 && !dsda->releasing)
137510242Schris.kirby@sun.com 			return (EBUSY);
137610242Schris.kirby@sun.com 
137710242Schris.kirby@sun.com 		mutex_enter(&ds->ds_lock);
137810242Schris.kirby@sun.com 		/*
137910242Schris.kirby@sun.com 		 * Can't delete a branch point. However, if we're destroying
138010242Schris.kirby@sun.com 		 * a clone and removing its origin due to it having a user
138110242Schris.kirby@sun.com 		 * hold count of 0 and having been marked for deferred destroy,
138210242Schris.kirby@sun.com 		 * it's OK for the origin to have a single clone.
138310242Schris.kirby@sun.com 		 */
138410242Schris.kirby@sun.com 		if (ds->ds_phys->ds_num_children >
138510242Schris.kirby@sun.com 		    (dsda->is_origin_rm ? 2 : 1)) {
138610242Schris.kirby@sun.com 			mutex_exit(&ds->ds_lock);
138710242Schris.kirby@sun.com 			return (EEXIST);
138810242Schris.kirby@sun.com 		}
138910242Schris.kirby@sun.com 		mutex_exit(&ds->ds_lock);
139010242Schris.kirby@sun.com 	} else if (dsl_dir_is_clone(ds->ds_dir)) {
139110242Schris.kirby@sun.com 		return (dsl_dataset_origin_check(dsda, arg2, tx));
139210242Schris.kirby@sun.com 	}
139310242Schris.kirby@sun.com 
13942199Sahrens 	/* XXX we should do some i/o error checking... */
13952199Sahrens 	return (0);
13962199Sahrens }
13972199Sahrens 
13986689Smaybee struct refsarg {
13996689Smaybee 	kmutex_t lock;
14006689Smaybee 	boolean_t gone;
14016689Smaybee 	kcondvar_t cv;
14026689Smaybee };
14036689Smaybee 
14046689Smaybee /* ARGSUSED */
14056689Smaybee static void
14066689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv)
14076689Smaybee {
14086689Smaybee 	struct refsarg *arg = argv;
14096689Smaybee 
14106689Smaybee 	mutex_enter(&arg->lock);
14116689Smaybee 	arg->gone = TRUE;
14126689Smaybee 	cv_signal(&arg->cv);
14136689Smaybee 	mutex_exit(&arg->lock);
14146689Smaybee }
14156689Smaybee 
14166689Smaybee static void
14176689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag)
14186689Smaybee {
14196689Smaybee 	struct refsarg arg;
14206689Smaybee 
14216689Smaybee 	mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL);
14226689Smaybee 	cv_init(&arg.cv, NULL, CV_DEFAULT, NULL);
14236689Smaybee 	arg.gone = FALSE;
14246689Smaybee 	(void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys,
14256689Smaybee 	    dsl_dataset_refs_gone);
14266689Smaybee 	dmu_buf_rele(ds->ds_dbuf, tag);
14276689Smaybee 	mutex_enter(&arg.lock);
14286689Smaybee 	while (!arg.gone)
14296689Smaybee 		cv_wait(&arg.cv, &arg.lock);
14306689Smaybee 	ASSERT(arg.gone);
14316689Smaybee 	mutex_exit(&arg.lock);
14326689Smaybee 	ds->ds_dbuf = NULL;
14336689Smaybee 	ds->ds_phys = NULL;
14346689Smaybee 	mutex_destroy(&arg.lock);
14356689Smaybee 	cv_destroy(&arg.cv);
14366689Smaybee }
14376689Smaybee 
143810801SMatthew.Ahrens@Sun.COM static void
143910801SMatthew.Ahrens@Sun.COM remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, dmu_tx_t *tx)
144010801SMatthew.Ahrens@Sun.COM {
144110801SMatthew.Ahrens@Sun.COM 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
144210801SMatthew.Ahrens@Sun.COM 	uint64_t count;
144310801SMatthew.Ahrens@Sun.COM 	int err;
144410801SMatthew.Ahrens@Sun.COM 
144510801SMatthew.Ahrens@Sun.COM 	ASSERT(ds->ds_phys->ds_num_children >= 2);
144610801SMatthew.Ahrens@Sun.COM 	err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
144710801SMatthew.Ahrens@Sun.COM 	/*
144810801SMatthew.Ahrens@Sun.COM 	 * The err should not be ENOENT, but a bug in a previous version
144910801SMatthew.Ahrens@Sun.COM 	 * of the code could cause upgrade_clones_cb() to not set
145010801SMatthew.Ahrens@Sun.COM 	 * ds_next_snap_obj when it should, leading to a missing entry.
145110801SMatthew.Ahrens@Sun.COM 	 * If we knew that the pool was created after
145210801SMatthew.Ahrens@Sun.COM 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
145310801SMatthew.Ahrens@Sun.COM 	 * ENOENT.  However, at least we can check that we don't have
145410801SMatthew.Ahrens@Sun.COM 	 * too many entries in the next_clones_obj even after failing to
145510801SMatthew.Ahrens@Sun.COM 	 * remove this one.
145610801SMatthew.Ahrens@Sun.COM 	 */
145710801SMatthew.Ahrens@Sun.COM 	if (err != ENOENT) {
145810801SMatthew.Ahrens@Sun.COM 		VERIFY3U(err, ==, 0);
145910801SMatthew.Ahrens@Sun.COM 	}
146010801SMatthew.Ahrens@Sun.COM 	ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj,
146110801SMatthew.Ahrens@Sun.COM 	    &count));
146210801SMatthew.Ahrens@Sun.COM 	ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
146310801SMatthew.Ahrens@Sun.COM }
146410801SMatthew.Ahrens@Sun.COM 
14655367Sahrens void
146612296SLin.Ling@Sun.COM dsl_dataset_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
14672199Sahrens {
146810242Schris.kirby@sun.com 	struct dsl_ds_destroyarg *dsda = arg1;
146910242Schris.kirby@sun.com 	dsl_dataset_t *ds = dsda->ds;
14702199Sahrens 	int err;
14712199Sahrens 	int after_branch_point = FALSE;
14722199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
14732199Sahrens 	objset_t *mos = dp->dp_meta_objset;
14742199Sahrens 	dsl_dataset_t *ds_prev = NULL;
14752199Sahrens 	uint64_t obj;
14762199Sahrens 
14776689Smaybee 	ASSERT(ds->ds_owner);
147810242Schris.kirby@sun.com 	ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1);
14792199Sahrens 	ASSERT(ds->ds_prev == NULL ||
14802199Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
14812199Sahrens 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
14822199Sahrens 
148310242Schris.kirby@sun.com 	if (dsda->defer) {
148410242Schris.kirby@sun.com 		ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
148510242Schris.kirby@sun.com 		if (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1) {
148610242Schris.kirby@sun.com 			dmu_buf_will_dirty(ds->ds_dbuf, tx);
148710242Schris.kirby@sun.com 			ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
148810242Schris.kirby@sun.com 			return;
148910242Schris.kirby@sun.com 		}
149010242Schris.kirby@sun.com 	}
149110242Schris.kirby@sun.com 
14926689Smaybee 	/* signal any waiters that this dataset is going away */
14936689Smaybee 	mutex_enter(&ds->ds_lock);
14946689Smaybee 	ds->ds_owner = dsl_reaper;
14956689Smaybee 	cv_broadcast(&ds->ds_exclusive_cv);
14966689Smaybee 	mutex_exit(&ds->ds_lock);
14976689Smaybee 
149812296SLin.Ling@Sun.COM 	if (ds->ds_objset) {
149912296SLin.Ling@Sun.COM 		dmu_objset_evict(ds->ds_objset);
150012296SLin.Ling@Sun.COM 		ds->ds_objset = NULL;
150112296SLin.Ling@Sun.COM 	}
150212296SLin.Ling@Sun.COM 
15035378Sck153898 	/* Remove our reservation */
15045378Sck153898 	if (ds->ds_reserved != 0) {
150511022STom.Erickson@Sun.COM 		dsl_prop_setarg_t psa;
150611022STom.Erickson@Sun.COM 		uint64_t value = 0;
150711022STom.Erickson@Sun.COM 
150811022STom.Erickson@Sun.COM 		dsl_prop_setarg_init_uint64(&psa, "refreservation",
150911022STom.Erickson@Sun.COM 		    (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
151011022STom.Erickson@Sun.COM 		    &value);
151111022STom.Erickson@Sun.COM 		psa.psa_effective_value = 0;	/* predict default value */
151211022STom.Erickson@Sun.COM 
151312296SLin.Ling@Sun.COM 		dsl_dataset_set_reservation_sync(ds, &psa, tx);
15145378Sck153898 		ASSERT3U(ds->ds_reserved, ==, 0);
15155378Sck153898 	}
15165378Sck153898 
15172199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
15182199Sahrens 
151912296SLin.Ling@Sun.COM 	dsl_scan_ds_destroyed(ds, tx);
15207046Sahrens 
15212199Sahrens 	obj = ds->ds_object;
1522789Sahrens 
1523789Sahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1524789Sahrens 		if (ds->ds_prev) {
1525789Sahrens 			ds_prev = ds->ds_prev;
1526789Sahrens 		} else {
15276689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
15286689Smaybee 			    ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
1529789Sahrens 		}
1530789Sahrens 		after_branch_point =
1531789Sahrens 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
1532789Sahrens 
1533789Sahrens 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1534789Sahrens 		if (after_branch_point &&
15357046Sahrens 		    ds_prev->ds_phys->ds_next_clones_obj != 0) {
153610801SMatthew.Ahrens@Sun.COM 			remove_from_next_clones(ds_prev, obj, tx);
15377046Sahrens 			if (ds->ds_phys->ds_next_snap_obj != 0) {
15387046Sahrens 				VERIFY(0 == zap_add_int(mos,
15397046Sahrens 				    ds_prev->ds_phys->ds_next_clones_obj,
15407046Sahrens 				    ds->ds_phys->ds_next_snap_obj, tx));
15417046Sahrens 			}
15427046Sahrens 		}
15437046Sahrens 		if (after_branch_point &&
1544789Sahrens 		    ds->ds_phys->ds_next_snap_obj == 0) {
1545789Sahrens 			/* This clone is toast. */
1546789Sahrens 			ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1547789Sahrens 			ds_prev->ds_phys->ds_num_children--;
154810242Schris.kirby@sun.com 
154910242Schris.kirby@sun.com 			/*
155010242Schris.kirby@sun.com 			 * If the clone's origin has no other clones, no
155110242Schris.kirby@sun.com 			 * user holds, and has been marked for deferred
155210242Schris.kirby@sun.com 			 * deletion, then we should have done the necessary
155310242Schris.kirby@sun.com 			 * destroy setup for it.
155410242Schris.kirby@sun.com 			 */
155510242Schris.kirby@sun.com 			if (ds_prev->ds_phys->ds_num_children == 1 &&
155610242Schris.kirby@sun.com 			    ds_prev->ds_userrefs == 0 &&
155710242Schris.kirby@sun.com 			    DS_IS_DEFER_DESTROY(ds_prev)) {
155810242Schris.kirby@sun.com 				ASSERT3P(dsda->rm_origin, !=, NULL);
155910242Schris.kirby@sun.com 			} else {
156010242Schris.kirby@sun.com 				ASSERT3P(dsda->rm_origin, ==, NULL);
156110242Schris.kirby@sun.com 			}
1562789Sahrens 		} else if (!after_branch_point) {
1563789Sahrens 			ds_prev->ds_phys->ds_next_snap_obj =
1564789Sahrens 			    ds->ds_phys->ds_next_snap_obj;
1565789Sahrens 		}
1566789Sahrens 	}
1567789Sahrens 
156812296SLin.Ling@Sun.COM 	if (dsl_dataset_is_snapshot(ds)) {
15692199Sahrens 		blkptr_t bp;
157012295SGeorge.Wilson@Sun.COM 		zio_t *pio;
1571789Sahrens 		dsl_dataset_t *ds_next;
1572789Sahrens 		uint64_t itor = 0;
15735378Sck153898 		uint64_t old_unique;
15747390SMatthew.Ahrens@Sun.COM 		int64_t used = 0, compressed = 0, uncompressed = 0;
1575789Sahrens 
15766689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
15776689Smaybee 		    ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
1578789Sahrens 		ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1579789Sahrens 
158012296SLin.Ling@Sun.COM 		old_unique = ds_next->ds_phys->ds_unique_bytes;
15815378Sck153898 
1582789Sahrens 		dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1583789Sahrens 		ds_next->ds_phys->ds_prev_snap_obj =
1584789Sahrens 		    ds->ds_phys->ds_prev_snap_obj;
1585789Sahrens 		ds_next->ds_phys->ds_prev_snap_txg =
1586789Sahrens 		    ds->ds_phys->ds_prev_snap_txg;
1587789Sahrens 		ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1588789Sahrens 		    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1589789Sahrens 
159012295SGeorge.Wilson@Sun.COM 		pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
159112295SGeorge.Wilson@Sun.COM 
1592789Sahrens 		/*
1593789Sahrens 		 * Transfer to our deadlist (which will become next's
1594789Sahrens 		 * new deadlist) any entries from next's current
1595789Sahrens 		 * deadlist which were born before prev, and free the
1596789Sahrens 		 * other entries.
1597789Sahrens 		 *
1598789Sahrens 		 * XXX we're doing this long task with the config lock held
1599789Sahrens 		 */
16006689Smaybee 		while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) {
1601789Sahrens 			if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) {
16021544Seschrock 				VERIFY(0 == bplist_enqueue(&ds->ds_deadlist,
16031544Seschrock 				    &bp, tx));
1604789Sahrens 				if (ds_prev && !after_branch_point &&
1605789Sahrens 				    bp.blk_birth >
1606789Sahrens 				    ds_prev->ds_phys->ds_prev_snap_txg) {
1607789Sahrens 					ds_prev->ds_phys->ds_unique_bytes +=
160810922SJeff.Bonwick@Sun.COM 					    bp_get_dsize_sync(dp->dp_spa, &bp);
1609789Sahrens 				}
1610789Sahrens 			} else {
161110922SJeff.Bonwick@Sun.COM 				used += bp_get_dsize_sync(dp->dp_spa, &bp);
1612789Sahrens 				compressed += BP_GET_PSIZE(&bp);
1613789Sahrens 				uncompressed += BP_GET_UCSIZE(&bp);
161412295SGeorge.Wilson@Sun.COM 				dsl_free_sync(pio, dp, tx->tx_txg, &bp);
1615789Sahrens 			}
1616789Sahrens 		}
161712295SGeorge.Wilson@Sun.COM 		VERIFY3U(zio_wait(pio), ==, 0);
16187390SMatthew.Ahrens@Sun.COM 		ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes);
16197390SMatthew.Ahrens@Sun.COM 
16207390SMatthew.Ahrens@Sun.COM 		/* change snapused */
16217390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
16227390SMatthew.Ahrens@Sun.COM 		    -used, -compressed, -uncompressed, tx);
16237390SMatthew.Ahrens@Sun.COM 
1624789Sahrens 		/* free next's deadlist */
1625789Sahrens 		bplist_close(&ds_next->ds_deadlist);
1626789Sahrens 		bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx);
1627789Sahrens 
1628789Sahrens 		/* set next's deadlist to our deadlist */
16296689Smaybee 		bplist_close(&ds->ds_deadlist);
1630789Sahrens 		ds_next->ds_phys->ds_deadlist_obj =
1631789Sahrens 		    ds->ds_phys->ds_deadlist_obj;
16321544Seschrock 		VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos,
16331544Seschrock 		    ds_next->ds_phys->ds_deadlist_obj));
1634789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1635789Sahrens 
163612296SLin.Ling@Sun.COM 		if (dsl_dataset_is_snapshot(ds_next)) {
1637789Sahrens 			/*
1638789Sahrens 			 * Update next's unique to include blocks which
1639789Sahrens 			 * were previously shared by only this snapshot
1640789Sahrens 			 * and it.  Those blocks will be born after the
1641789Sahrens 			 * prev snap and before this snap, and will have
1642789Sahrens 			 * died after the next snap and before the one
1643789Sahrens 			 * after that (ie. be on the snap after next's
1644789Sahrens 			 * deadlist).
1645789Sahrens 			 *
1646789Sahrens 			 * XXX we're doing this long task with the
1647789Sahrens 			 * config lock held
1648789Sahrens 			 */
1649789Sahrens 			dsl_dataset_t *ds_after_next;
16507390SMatthew.Ahrens@Sun.COM 			uint64_t space;
1651789Sahrens 
16526689Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
16536689Smaybee 			    ds_next->ds_phys->ds_next_snap_obj,
16546689Smaybee 			    FTAG, &ds_after_next));
16557390SMatthew.Ahrens@Sun.COM 
16567390SMatthew.Ahrens@Sun.COM 			VERIFY(0 ==
16577390SMatthew.Ahrens@Sun.COM 			    bplist_space_birthrange(&ds_after_next->ds_deadlist,
16587390SMatthew.Ahrens@Sun.COM 			    ds->ds_phys->ds_prev_snap_txg,
16597390SMatthew.Ahrens@Sun.COM 			    ds->ds_phys->ds_creation_txg, &space));
16607390SMatthew.Ahrens@Sun.COM 			ds_next->ds_phys->ds_unique_bytes += space;
1661789Sahrens 
16626689Smaybee 			dsl_dataset_rele(ds_after_next, FTAG);
1663789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, NULL);
1664789Sahrens 		} else {
1665789Sahrens 			ASSERT3P(ds_next->ds_prev, ==, ds);
16666689Smaybee 			dsl_dataset_drop_ref(ds_next->ds_prev, ds_next);
16676689Smaybee 			ds_next->ds_prev = NULL;
1668789Sahrens 			if (ds_prev) {
16696689Smaybee 				VERIFY(0 == dsl_dataset_get_ref(dp,
16706689Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
16716689Smaybee 				    ds_next, &ds_next->ds_prev));
1672789Sahrens 			}
16735378Sck153898 
16745378Sck153898 			dsl_dataset_recalc_head_uniq(ds_next);
16755378Sck153898 
16765378Sck153898 			/*
16775378Sck153898 			 * Reduce the amount of our unconsmed refreservation
16785378Sck153898 			 * being charged to our parent by the amount of
16795378Sck153898 			 * new unique data we have gained.
16805378Sck153898 			 */
16815378Sck153898 			if (old_unique < ds_next->ds_reserved) {
16825378Sck153898 				int64_t mrsdelta;
16835378Sck153898 				uint64_t new_unique =
16845378Sck153898 				    ds_next->ds_phys->ds_unique_bytes;
16855378Sck153898 
16865378Sck153898 				ASSERT(old_unique <= new_unique);
16875378Sck153898 				mrsdelta = MIN(new_unique - old_unique,
16885378Sck153898 				    ds_next->ds_reserved - old_unique);
16897390SMatthew.Ahrens@Sun.COM 				dsl_dir_diduse_space(ds->ds_dir,
16907390SMatthew.Ahrens@Sun.COM 				    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
16915378Sck153898 			}
1692789Sahrens 		}
16936689Smaybee 		dsl_dataset_rele(ds_next, FTAG);
1694789Sahrens 	} else {
1695789Sahrens 		/*
1696789Sahrens 		 * There's no next snapshot, so this is a head dataset.
1697789Sahrens 		 * Destroy the deadlist.  Unless it's a clone, the
1698789Sahrens 		 * deadlist should be empty.  (If it's a clone, it's
1699789Sahrens 		 * safe to ignore the deadlist contents.)
1700789Sahrens 		 */
1701789Sahrens 		struct killarg ka;
1702789Sahrens 
1703789Sahrens 		ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist));
1704789Sahrens 		bplist_close(&ds->ds_deadlist);
1705789Sahrens 		bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx);
1706789Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1707789Sahrens 
1708789Sahrens 		/*
1709789Sahrens 		 * Free everything that we point to (that's born after
1710789Sahrens 		 * the previous snapshot, if we are a clone)
1711789Sahrens 		 *
17127390SMatthew.Ahrens@Sun.COM 		 * NB: this should be very quick, because we already
17137390SMatthew.Ahrens@Sun.COM 		 * freed all the objects in open context.
1714789Sahrens 		 */
17157390SMatthew.Ahrens@Sun.COM 		ka.ds = ds;
1716789Sahrens 		ka.tx = tx;
17177837SMatthew.Ahrens@Sun.COM 		err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
17187837SMatthew.Ahrens@Sun.COM 		    TRAVERSE_POST, kill_blkptr, &ka);
1719789Sahrens 		ASSERT3U(err, ==, 0);
17209390Schris.kirby@sun.com 		ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
17217390SMatthew.Ahrens@Sun.COM 		    ds->ds_phys->ds_unique_bytes == 0);
172210342Schris.kirby@sun.com 
172310342Schris.kirby@sun.com 		if (ds->ds_prev != NULL) {
172410342Schris.kirby@sun.com 			dsl_dataset_rele(ds->ds_prev, ds);
172510342Schris.kirby@sun.com 			ds->ds_prev = ds_prev = NULL;
172610342Schris.kirby@sun.com 		}
1727789Sahrens 	}
1728789Sahrens 
17296689Smaybee 	if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
17306689Smaybee 		/* Erase the link in the dir */
17316689Smaybee 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
17326689Smaybee 		ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
17336689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
1734789Sahrens 		err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1735789Sahrens 		ASSERT(err == 0);
1736789Sahrens 	} else {
1737789Sahrens 		/* remove from snapshot namespace */
1738789Sahrens 		dsl_dataset_t *ds_head;
17396689Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
17406689Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
17416689Smaybee 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
17422207Sahrens 		VERIFY(0 == dsl_dataset_get_snapname(ds));
1743789Sahrens #ifdef ZFS_DEBUG
1744789Sahrens 		{
1745789Sahrens 			uint64_t val;
17466492Stimh 
17476689Smaybee 			err = dsl_dataset_snap_lookup(ds_head,
17486492Stimh 			    ds->ds_snapname, &val);
1749789Sahrens 			ASSERT3U(err, ==, 0);
1750789Sahrens 			ASSERT3U(val, ==, obj);
1751789Sahrens 		}
1752789Sahrens #endif
17536689Smaybee 		err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
1754789Sahrens 		ASSERT(err == 0);
17556689Smaybee 		dsl_dataset_rele(ds_head, FTAG);
1756789Sahrens 	}
1757789Sahrens 
1758789Sahrens 	if (ds_prev && ds->ds_prev != ds_prev)
17596689Smaybee 		dsl_dataset_rele(ds_prev, FTAG);
1760789Sahrens 
17615094Slling 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
176212296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_DESTROY, dp->dp_spa, tx,
176312296SLin.Ling@Sun.COM 	    "dataset = %llu", ds->ds_object);
17644543Smarks 
17657046Sahrens 	if (ds->ds_phys->ds_next_clones_obj != 0) {
17667046Sahrens 		uint64_t count;
17677046Sahrens 		ASSERT(0 == zap_count(mos,
17687046Sahrens 		    ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
17697046Sahrens 		VERIFY(0 == dmu_object_free(mos,
17707046Sahrens 		    ds->ds_phys->ds_next_clones_obj, tx));
17717046Sahrens 	}
17727390SMatthew.Ahrens@Sun.COM 	if (ds->ds_phys->ds_props_obj != 0)
17737390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
177410242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj != 0)
177510242Schris.kirby@sun.com 		VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
17766689Smaybee 	dsl_dir_close(ds->ds_dir, ds);
17776689Smaybee 	ds->ds_dir = NULL;
17786689Smaybee 	dsl_dataset_drain_refs(ds, tag);
17792199Sahrens 	VERIFY(0 == dmu_object_free(mos, obj, tx));
178010242Schris.kirby@sun.com 
178110242Schris.kirby@sun.com 	if (dsda->rm_origin) {
178210242Schris.kirby@sun.com 		/*
178310242Schris.kirby@sun.com 		 * Remove the origin of the clone we just destroyed.
178410242Schris.kirby@sun.com 		 */
178510242Schris.kirby@sun.com 		struct dsl_ds_destroyarg ndsda = {0};
178610242Schris.kirby@sun.com 
178710342Schris.kirby@sun.com 		ndsda.ds = dsda->rm_origin;
178812296SLin.Ling@Sun.COM 		dsl_dataset_destroy_sync(&ndsda, tag, tx);
178910242Schris.kirby@sun.com 	}
17902199Sahrens }
17912199Sahrens 
17925378Sck153898 static int
17935378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
17945378Sck153898 {
17955378Sck153898 	uint64_t asize;
17965378Sck153898 
17975378Sck153898 	if (!dmu_tx_is_syncing(tx))
17985378Sck153898 		return (0);
17995378Sck153898 
18005378Sck153898 	/*
18015378Sck153898 	 * If there's an fs-only reservation, any blocks that might become
18025378Sck153898 	 * owned by the snapshot dataset must be accommodated by space
18035378Sck153898 	 * outside of the reservation.
18045378Sck153898 	 */
180512296SLin.Ling@Sun.COM 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
180612296SLin.Ling@Sun.COM 	asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
18075378Sck153898 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE))
18085378Sck153898 		return (ENOSPC);
18095378Sck153898 
18105378Sck153898 	/*
18115378Sck153898 	 * Propogate any reserved space for this snapshot to other
18125378Sck153898 	 * snapshot checks in this sync group.
18135378Sck153898 	 */
18145378Sck153898 	if (asize > 0)
18155378Sck153898 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
18165378Sck153898 
18175378Sck153898 	return (0);
18185378Sck153898 }
18195378Sck153898 
18202199Sahrens int
18212199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
18222199Sahrens {
18235367Sahrens 	dsl_dataset_t *ds = arg1;
18242199Sahrens 	const char *snapname = arg2;
18252199Sahrens 	int err;
18262199Sahrens 	uint64_t value;
1827789Sahrens 
1828789Sahrens 	/*
18292199Sahrens 	 * We don't allow multiple snapshots of the same txg.  If there
18302199Sahrens 	 * is already one, try again.
18312199Sahrens 	 */
18322199Sahrens 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
18332199Sahrens 		return (EAGAIN);
18342199Sahrens 
18352199Sahrens 	/*
18362199Sahrens 	 * Check for conflicting name snapshot name.
1837789Sahrens 	 */
18386689Smaybee 	err = dsl_dataset_snap_lookup(ds, snapname, &value);
18392199Sahrens 	if (err == 0)
18402199Sahrens 		return (EEXIST);
18412199Sahrens 	if (err != ENOENT)
18422199Sahrens 		return (err);
1843789Sahrens 
18443978Smmusante 	/*
18453978Smmusante 	 * Check that the dataset's name is not too long.  Name consists
18463978Smmusante 	 * of the dataset's length + 1 for the @-sign + snapshot name's length
18473978Smmusante 	 */
18483978Smmusante 	if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
18493978Smmusante 		return (ENAMETOOLONG);
18503978Smmusante 
18515378Sck153898 	err = dsl_dataset_snapshot_reserve_space(ds, tx);
18525378Sck153898 	if (err)
18535378Sck153898 		return (err);
18545378Sck153898 
18552199Sahrens 	ds->ds_trysnap_txg = tx->tx_txg;
1856789Sahrens 	return (0);
1857789Sahrens }
1858789Sahrens 
18592199Sahrens void
186012296SLin.Ling@Sun.COM dsl_dataset_snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1861789Sahrens {
18625367Sahrens 	dsl_dataset_t *ds = arg1;
18632199Sahrens 	const char *snapname = arg2;
18642199Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1865789Sahrens 	dmu_buf_t *dbuf;
1866789Sahrens 	dsl_dataset_phys_t *dsphys;
18677046Sahrens 	uint64_t dsobj, crtxg;
1868789Sahrens 	objset_t *mos = dp->dp_meta_objset;
1869789Sahrens 	int err;
1870789Sahrens 
18712199Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
1872789Sahrens 
18737046Sahrens 	/*
18747046Sahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
18757046Sahrens 	 */
18767046Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
18777046Sahrens 		crtxg = 1;
18787046Sahrens 	else
18797046Sahrens 		crtxg = tx->tx_txg;
18807046Sahrens 
1881928Stabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1882928Stabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
18831544Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1884789Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1885789Sahrens 	dsphys = dbuf->db_data;
18866689Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
18872199Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1888789Sahrens 	dsphys->ds_fsid_guid = unique_create();
1889789Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1890789Sahrens 	    sizeof (dsphys->ds_guid));
1891789Sahrens 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1892789Sahrens 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1893789Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1894789Sahrens 	dsphys->ds_num_children = 1;
1895789Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
18967046Sahrens 	dsphys->ds_creation_txg = crtxg;
1897789Sahrens 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1898789Sahrens 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
1899789Sahrens 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1900789Sahrens 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
19012082Seschrock 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1902789Sahrens 	dsphys->ds_bp = ds->ds_phys->ds_bp;
19031544Seschrock 	dmu_buf_rele(dbuf, FTAG);
1904789Sahrens 
19052199Sahrens 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
19062199Sahrens 	if (ds->ds_prev) {
19077046Sahrens 		uint64_t next_clones_obj =
19087046Sahrens 		    ds->ds_prev->ds_phys->ds_next_clones_obj;
19092199Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1910789Sahrens 		    ds->ds_object ||
19112199Sahrens 		    ds->ds_prev->ds_phys->ds_num_children > 1);
19122199Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
19132199Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1914789Sahrens 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
19152199Sahrens 			    ds->ds_prev->ds_phys->ds_creation_txg);
19162199Sahrens 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
19177046Sahrens 		} else if (next_clones_obj != 0) {
191810801SMatthew.Ahrens@Sun.COM 			remove_from_next_clones(ds->ds_prev,
191910801SMatthew.Ahrens@Sun.COM 			    dsphys->ds_next_snap_obj, tx);
19207046Sahrens 			VERIFY3U(0, ==, zap_add_int(mos,
19217046Sahrens 			    next_clones_obj, dsobj, tx));
1922789Sahrens 		}
1923789Sahrens 	}
1924789Sahrens 
19255378Sck153898 	/*
19265378Sck153898 	 * If we have a reference-reservation on this dataset, we will
19275378Sck153898 	 * need to increase the amount of refreservation being charged
19285378Sck153898 	 * since our unique space is going to zero.
19295378Sck153898 	 */
19305378Sck153898 	if (ds->ds_reserved) {
193112296SLin.Ling@Sun.COM 		int64_t delta;
193212296SLin.Ling@Sun.COM 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
193312296SLin.Ling@Sun.COM 		delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
19347390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
193512296SLin.Ling@Sun.COM 		    delta, 0, 0, tx);
19365378Sck153898 	}
19375378Sck153898 
1938789Sahrens 	bplist_close(&ds->ds_deadlist);
1939789Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
19405712Sahrens 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1941789Sahrens 	ds->ds_phys->ds_prev_snap_obj = dsobj;
19427046Sahrens 	ds->ds_phys->ds_prev_snap_txg = crtxg;
1943789Sahrens 	ds->ds_phys->ds_unique_bytes = 0;
19445378Sck153898 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
19455378Sck153898 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1946789Sahrens 	ds->ds_phys->ds_deadlist_obj =
1947789Sahrens 	    bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx);
19481544Seschrock 	VERIFY(0 == bplist_open(&ds->ds_deadlist, mos,
19491544Seschrock 	    ds->ds_phys->ds_deadlist_obj));
1950789Sahrens 
1951789Sahrens 	dprintf("snap '%s' -> obj %llu\n", snapname, dsobj);
1952789Sahrens 	err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
1953789Sahrens 	    snapname, 8, 1, &dsobj, tx);
1954789Sahrens 	ASSERT(err == 0);
1955789Sahrens 
1956789Sahrens 	if (ds->ds_prev)
19576689Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
19586689Smaybee 	VERIFY(0 == dsl_dataset_get_ref(dp,
19596689Smaybee 	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
19604543Smarks 
196112296SLin.Ling@Sun.COM 	dsl_scan_ds_snapshotted(ds, tx);
19627046Sahrens 
196310373Schris.kirby@sun.com 	dsl_dir_snap_cmtime_update(ds->ds_dir);
196410373Schris.kirby@sun.com 
196512296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_SNAPSHOT, dp->dp_spa, tx,
19664603Sahrens 	    "dataset = %llu", dsobj);
1967789Sahrens }
1968789Sahrens 
1969789Sahrens void
19703547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1971789Sahrens {
1972789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
197310298SMatthew.Ahrens@Sun.COM 	ASSERT(ds->ds_objset != NULL);
1974789Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
1975789Sahrens 
19764787Sahrens 	/*
19774787Sahrens 	 * in case we had to change ds_fsid_guid when we opened it,
19784787Sahrens 	 * sync it out now.
19794787Sahrens 	 */
19804787Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
19814787Sahrens 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
19824787Sahrens 
1983789Sahrens 	dsl_dir_dirty(ds->ds_dir, tx);
198410298SMatthew.Ahrens@Sun.COM 	dmu_objset_sync(ds->ds_objset, zio, tx);
1985789Sahrens }
1986789Sahrens 
1987789Sahrens void
19882885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1989789Sahrens {
19905378Sck153898 	uint64_t refd, avail, uobjs, aobjs;
19915378Sck153898 
19922885Sahrens 	dsl_dir_stats(ds->ds_dir, nv);
1993789Sahrens 
19945378Sck153898 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
19955378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
19965378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
19975378Sck153898 
19982885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
19992885Sahrens 	    ds->ds_phys->ds_creation_time);
20002885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
20012885Sahrens 	    ds->ds_phys->ds_creation_txg);
20025378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
20035378Sck153898 	    ds->ds_quota);
20045378Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
20055378Sck153898 	    ds->ds_reserved);
20066643Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
20076643Seschrock 	    ds->ds_phys->ds_guid);
200810575SEric.Schrock@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
200912296SLin.Ling@Sun.COM 	    ds->ds_phys->ds_unique_bytes);
201010575SEric.Schrock@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
201110575SEric.Schrock@Sun.COM 	    ds->ds_object);
201211022STom.Erickson@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
201311022STom.Erickson@Sun.COM 	    ds->ds_userrefs);
201410242Schris.kirby@sun.com 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
201510242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
2016789Sahrens 
2017789Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
2018789Sahrens 		/*
2019789Sahrens 		 * This is a snapshot; override the dd's space used with
20202885Sahrens 		 * our unique space and compression ratio.
2021789Sahrens 		 */
20222885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
20232885Sahrens 		    ds->ds_phys->ds_unique_bytes);
20242885Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
20252885Sahrens 		    ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
20262885Sahrens 		    (ds->ds_phys->ds_uncompressed_bytes * 100 /
20272885Sahrens 		    ds->ds_phys->ds_compressed_bytes));
2028789Sahrens 	}
2029789Sahrens }
2030789Sahrens 
20312885Sahrens void
20322885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2033789Sahrens {
20342885Sahrens 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
20352885Sahrens 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
20365367Sahrens 	stat->dds_guid = ds->ds_phys->ds_guid;
20372885Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
20382885Sahrens 		stat->dds_is_snapshot = B_TRUE;
20392885Sahrens 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
20408228SEric.Taylor@Sun.COM 	} else {
20418228SEric.Taylor@Sun.COM 		stat->dds_is_snapshot = B_FALSE;
20428228SEric.Taylor@Sun.COM 		stat->dds_num_clones = 0;
20432885Sahrens 	}
20442885Sahrens 
20452885Sahrens 	/* clone origin is really a dsl_dir thing... */
20465446Sahrens 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
20477046Sahrens 	if (dsl_dir_is_clone(ds->ds_dir)) {
20482885Sahrens 		dsl_dataset_t *ods;
20492885Sahrens 
20506689Smaybee 		VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
20516689Smaybee 		    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
20525367Sahrens 		dsl_dataset_name(ods, stat->dds_origin);
20536689Smaybee 		dsl_dataset_drop_ref(ods, FTAG);
20548228SEric.Taylor@Sun.COM 	} else {
20558228SEric.Taylor@Sun.COM 		stat->dds_origin[0] = '\0';
20562885Sahrens 	}
20575446Sahrens 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
20582885Sahrens }
20592885Sahrens 
20602885Sahrens uint64_t
20612885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
20622885Sahrens {
20634787Sahrens 	return (ds->ds_fsid_guid);
20642885Sahrens }
20652885Sahrens 
20662885Sahrens void
20672885Sahrens dsl_dataset_space(dsl_dataset_t *ds,
20682885Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
20692885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
20702885Sahrens {
20712885Sahrens 	*refdbytesp = ds->ds_phys->ds_used_bytes;
20722885Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
20735378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
20745378Sck153898 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
20755378Sck153898 	if (ds->ds_quota != 0) {
20765378Sck153898 		/*
20775378Sck153898 		 * Adjust available bytes according to refquota
20785378Sck153898 		 */
20795378Sck153898 		if (*refdbytesp < ds->ds_quota)
20805378Sck153898 			*availbytesp = MIN(*availbytesp,
20815378Sck153898 			    ds->ds_quota - *refdbytesp);
20825378Sck153898 		else
20835378Sck153898 			*availbytesp = 0;
20845378Sck153898 	}
20852885Sahrens 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
20862885Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
2087789Sahrens }
2088789Sahrens 
20895326Sek110237 boolean_t
20905326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
20915326Sek110237 {
20925326Sek110237 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
20935326Sek110237 
20945326Sek110237 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
20955326Sek110237 	    dsl_pool_sync_context(dp));
20965326Sek110237 	if (ds->ds_prev == NULL)
20975326Sek110237 		return (B_FALSE);
20985326Sek110237 	if (ds->ds_phys->ds_bp.blk_birth >
20995326Sek110237 	    ds->ds_prev->ds_phys->ds_creation_txg)
21005326Sek110237 		return (B_TRUE);
21015326Sek110237 	return (B_FALSE);
21025326Sek110237 }
21035326Sek110237 
21042199Sahrens /* ARGSUSED */
2105789Sahrens static int
21062199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
2107789Sahrens {
21082199Sahrens 	dsl_dataset_t *ds = arg1;
21092199Sahrens 	char *newsnapname = arg2;
21102199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
21112199Sahrens 	dsl_dataset_t *hds;
21122199Sahrens 	uint64_t val;
2113789Sahrens 	int err;
2114789Sahrens 
21156689Smaybee 	err = dsl_dataset_hold_obj(dd->dd_pool,
21166689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds);
2117789Sahrens 	if (err)
2118789Sahrens 		return (err);
2119789Sahrens 
21202199Sahrens 	/* new name better not be in use */
21216689Smaybee 	err = dsl_dataset_snap_lookup(hds, newsnapname, &val);
21226689Smaybee 	dsl_dataset_rele(hds, FTAG);
2123789Sahrens 
21242199Sahrens 	if (err == 0)
21252199Sahrens 		err = EEXIST;
21262199Sahrens 	else if (err == ENOENT)
21272199Sahrens 		err = 0;
21284007Smmusante 
21294007Smmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
21304007Smmusante 	if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
21314007Smmusante 		err = ENAMETOOLONG;
21324007Smmusante 
21332199Sahrens 	return (err);
21342199Sahrens }
2135789Sahrens 
21362199Sahrens static void
213712296SLin.Ling@Sun.COM dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx)
21382199Sahrens {
21392199Sahrens 	dsl_dataset_t *ds = arg1;
21404543Smarks 	const char *newsnapname = arg2;
21412199Sahrens 	dsl_dir_t *dd = ds->ds_dir;
21422199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
21432199Sahrens 	dsl_dataset_t *hds;
21442199Sahrens 	int err;
2145789Sahrens 
21462199Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2147789Sahrens 
21486689Smaybee 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
21496689Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
2150789Sahrens 
21512199Sahrens 	VERIFY(0 == dsl_dataset_get_snapname(ds));
21526689Smaybee 	err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
2153789Sahrens 	ASSERT3U(err, ==, 0);
21542199Sahrens 	mutex_enter(&ds->ds_lock);
21552199Sahrens 	(void) strcpy(ds->ds_snapname, newsnapname);
21562199Sahrens 	mutex_exit(&ds->ds_lock);
21572199Sahrens 	err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
21582199Sahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2159789Sahrens 	ASSERT3U(err, ==, 0);
2160789Sahrens 
216112296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
216212296SLin.Ling@Sun.COM 	    "dataset = %llu", ds->ds_object);
21636689Smaybee 	dsl_dataset_rele(hds, FTAG);
2164789Sahrens }
2165789Sahrens 
21665326Sek110237 struct renamesnaparg {
21674007Smmusante 	dsl_sync_task_group_t *dstg;
21684007Smmusante 	char failed[MAXPATHLEN];
21694007Smmusante 	char *oldsnap;
21704007Smmusante 	char *newsnap;
21714007Smmusante };
21724007Smmusante 
21734007Smmusante static int
217411209SMatthew.Ahrens@Sun.COM dsl_snapshot_rename_one(const char *name, void *arg)
21754007Smmusante {
21765326Sek110237 	struct renamesnaparg *ra = arg;
21774007Smmusante 	dsl_dataset_t *ds = NULL;
217811209SMatthew.Ahrens@Sun.COM 	char *snapname;
21794007Smmusante 	int err;
21804007Smmusante 
218111209SMatthew.Ahrens@Sun.COM 	snapname = kmem_asprintf("%s@%s", name, ra->oldsnap);
218211209SMatthew.Ahrens@Sun.COM 	(void) strlcpy(ra->failed, snapname, sizeof (ra->failed));
21834543Smarks 
21844543Smarks 	/*
21854543Smarks 	 * For recursive snapshot renames the parent won't be changing
21864543Smarks 	 * so we just pass name for both the to/from argument.
21874543Smarks 	 */
218811209SMatthew.Ahrens@Sun.COM 	err = zfs_secpolicy_rename_perms(snapname, snapname, CRED());
218911209SMatthew.Ahrens@Sun.COM 	if (err != 0) {
219011209SMatthew.Ahrens@Sun.COM 		strfree(snapname);
219111209SMatthew.Ahrens@Sun.COM 		return (err == ENOENT ? 0 : err);
21924543Smarks 	}
21934543Smarks 
21946689Smaybee #ifdef _KERNEL
21956689Smaybee 	/*
21966689Smaybee 	 * For all filesystems undergoing rename, we'll need to unmount it.
21976689Smaybee 	 */
219811209SMatthew.Ahrens@Sun.COM 	(void) zfs_unmount_snap(snapname, NULL);
21996689Smaybee #endif
220011209SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(snapname, ra->dstg, &ds);
220111609SMatthew.Ahrens@Sun.COM 	strfree(snapname);
220211609SMatthew.Ahrens@Sun.COM 	if (err != 0)
220311209SMatthew.Ahrens@Sun.COM 		return (err == ENOENT ? 0 : err);
22044007Smmusante 
22054007Smmusante 	dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
22064007Smmusante 	    dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
22074007Smmusante 
22084007Smmusante 	return (0);
22094007Smmusante }
22104007Smmusante 
22114007Smmusante static int
22124007Smmusante dsl_recursive_rename(char *oldname, const char *newname)
22134007Smmusante {
22144007Smmusante 	int err;
22155326Sek110237 	struct renamesnaparg *ra;
22164007Smmusante 	dsl_sync_task_t *dst;
22174007Smmusante 	spa_t *spa;
22184007Smmusante 	char *cp, *fsname = spa_strdup(oldname);
221911209SMatthew.Ahrens@Sun.COM 	int len = strlen(oldname) + 1;
22204007Smmusante 
22214007Smmusante 	/* truncate the snapshot name to get the fsname */
22224007Smmusante 	cp = strchr(fsname, '@');
22234007Smmusante 	*cp = '\0';
22244007Smmusante 
22254603Sahrens 	err = spa_open(fsname, &spa, FTAG);
22264007Smmusante 	if (err) {
222711209SMatthew.Ahrens@Sun.COM 		kmem_free(fsname, len);
22284007Smmusante 		return (err);
22294007Smmusante 	}
22305326Sek110237 	ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP);
22314007Smmusante 	ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
22324007Smmusante 
22334007Smmusante 	ra->oldsnap = strchr(oldname, '@') + 1;
22344007Smmusante 	ra->newsnap = strchr(newname, '@') + 1;
22354007Smmusante 	*ra->failed = '\0';
22364007Smmusante 
22374007Smmusante 	err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
22384007Smmusante 	    DS_FIND_CHILDREN);
223911209SMatthew.Ahrens@Sun.COM 	kmem_free(fsname, len);
22404007Smmusante 
22414007Smmusante 	if (err == 0) {
22424007Smmusante 		err = dsl_sync_task_group_wait(ra->dstg);
22434007Smmusante 	}
22444007Smmusante 
22454007Smmusante 	for (dst = list_head(&ra->dstg->dstg_tasks); dst;
22464007Smmusante 	    dst = list_next(&ra->dstg->dstg_tasks, dst)) {
22474007Smmusante 		dsl_dataset_t *ds = dst->dst_arg1;
22484007Smmusante 		if (dst->dst_err) {
22494007Smmusante 			dsl_dir_name(ds->ds_dir, ra->failed);
225011209SMatthew.Ahrens@Sun.COM 			(void) strlcat(ra->failed, "@", sizeof (ra->failed));
225111209SMatthew.Ahrens@Sun.COM 			(void) strlcat(ra->failed, ra->newsnap,
225211209SMatthew.Ahrens@Sun.COM 			    sizeof (ra->failed));
22534007Smmusante 		}
22546689Smaybee 		dsl_dataset_rele(ds, ra->dstg);
22554007Smmusante 	}
22564007Smmusante 
22574543Smarks 	if (err)
225811209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(oldname, ra->failed, sizeof (ra->failed));
22594007Smmusante 
22604007Smmusante 	dsl_sync_task_group_destroy(ra->dstg);
22615326Sek110237 	kmem_free(ra, sizeof (struct renamesnaparg));
22624007Smmusante 	spa_close(spa, FTAG);
22634007Smmusante 	return (err);
22644007Smmusante }
22654007Smmusante 
22664569Smmusante static int
226711209SMatthew.Ahrens@Sun.COM dsl_valid_rename(const char *oldname, void *arg)
22684569Smmusante {
22694569Smmusante 	int delta = *(int *)arg;
22704569Smmusante 
22714569Smmusante 	if (strlen(oldname) + delta >= MAXNAMELEN)
22724569Smmusante 		return (ENAMETOOLONG);
22734569Smmusante 
22744569Smmusante 	return (0);
22754569Smmusante }
22764569Smmusante 
2277789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename
2278789Sahrens int
22796689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive)
2280789Sahrens {
2281789Sahrens 	dsl_dir_t *dd;
22822199Sahrens 	dsl_dataset_t *ds;
2283789Sahrens 	const char *tail;
2284789Sahrens 	int err;
2285789Sahrens 
22862199Sahrens 	err = dsl_dir_open(oldname, FTAG, &dd, &tail);
22871544Seschrock 	if (err)
22881544Seschrock 		return (err);
228911701SSanjeev.Bagewadi@Sun.COM 
2290789Sahrens 	if (tail == NULL) {
22914569Smmusante 		int delta = strlen(newname) - strlen(oldname);
22924569Smmusante 
22937046Sahrens 		/* if we're growing, validate child name lengths */
22944569Smmusante 		if (delta > 0)
22954569Smmusante 			err = dmu_objset_find(oldname, dsl_valid_rename,
22964569Smmusante 			    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
22974569Smmusante 
229811823SMatthew.Ahrens@Sun.COM 		if (err == 0)
22994569Smmusante 			err = dsl_dir_rename(dd, newname);
2300789Sahrens 		dsl_dir_close(dd, FTAG);
2301789Sahrens 		return (err);
2302789Sahrens 	}
230311701SSanjeev.Bagewadi@Sun.COM 
2304789Sahrens 	if (tail[0] != '@') {
230510588SEric.Taylor@Sun.COM 		/* the name ended in a nonexistent component */
2306789Sahrens 		dsl_dir_close(dd, FTAG);
2307789Sahrens 		return (ENOENT);
2308789Sahrens 	}
2309789Sahrens 
23102199Sahrens 	dsl_dir_close(dd, FTAG);
23112199Sahrens 
23122199Sahrens 	/* new name must be snapshot in same filesystem */
23132199Sahrens 	tail = strchr(newname, '@');
23142199Sahrens 	if (tail == NULL)
23152199Sahrens 		return (EINVAL);
23162199Sahrens 	tail++;
23172199Sahrens 	if (strncmp(oldname, newname, tail - newname) != 0)
23182199Sahrens 		return (EXDEV);
2319789Sahrens 
23204007Smmusante 	if (recursive) {
23214007Smmusante 		err = dsl_recursive_rename(oldname, newname);
23224007Smmusante 	} else {
23236689Smaybee 		err = dsl_dataset_hold(oldname, FTAG, &ds);
23244007Smmusante 		if (err)
23254007Smmusante 			return (err);
23262199Sahrens 
23274007Smmusante 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
23284007Smmusante 		    dsl_dataset_snapshot_rename_check,
23294007Smmusante 		    dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
23302199Sahrens 
23316689Smaybee 		dsl_dataset_rele(ds, FTAG);
23324007Smmusante 	}
23332199Sahrens 
2334789Sahrens 	return (err);
2335789Sahrens }
23362082Seschrock 
23377046Sahrens struct promotenode {
23386689Smaybee 	list_node_t link;
23396689Smaybee 	dsl_dataset_t *ds;
23406689Smaybee };
23416689Smaybee 
23422199Sahrens struct promotearg {
23437390SMatthew.Ahrens@Sun.COM 	list_t shared_snaps, origin_snaps, clone_snaps;
234412296SLin.Ling@Sun.COM 	dsl_dataset_t *origin_origin;
23457390SMatthew.Ahrens@Sun.COM 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
234610588SEric.Taylor@Sun.COM 	char *err_ds;
23472199Sahrens };
23482199Sahrens 
23497390SMatthew.Ahrens@Sun.COM static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
235012296SLin.Ling@Sun.COM static boolean_t snaplist_unstable(list_t *l);
235112296SLin.Ling@Sun.COM 
23522082Seschrock static int
23532199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
23542082Seschrock {
23552199Sahrens 	dsl_dataset_t *hds = arg1;
23562199Sahrens 	struct promotearg *pa = arg2;
23577390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap = list_head(&pa->shared_snaps);
23586689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
23596689Smaybee 	int err;
23602199Sahrens 
23617046Sahrens 	/* Check that it is a real clone */
23627046Sahrens 	if (!dsl_dir_is_clone(hds->ds_dir))
23632082Seschrock 		return (EINVAL);
23642082Seschrock 
23652199Sahrens 	/* Since this is so expensive, don't do the preliminary check */
23662199Sahrens 	if (!dmu_tx_is_syncing(tx))
23672199Sahrens 		return (0);
23682199Sahrens 
23696689Smaybee 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)
23706689Smaybee 		return (EXDEV);
23712082Seschrock 
23725367Sahrens 	/* compute origin's new unique space */
23737390SMatthew.Ahrens@Sun.COM 	snap = list_tail(&pa->clone_snaps);
23747390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
23757390SMatthew.Ahrens@Sun.COM 	err = bplist_space_birthrange(&snap->ds->ds_deadlist,
23767390SMatthew.Ahrens@Sun.COM 	    origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, &pa->unique);
23777390SMatthew.Ahrens@Sun.COM 	if (err)
23786689Smaybee 		return (err);
23796689Smaybee 
23806689Smaybee 	/*
23816689Smaybee 	 * Walk the snapshots that we are moving
23826689Smaybee 	 *
23837390SMatthew.Ahrens@Sun.COM 	 * Compute space to transfer.  Consider the incremental changes
23847390SMatthew.Ahrens@Sun.COM 	 * to used for each snapshot:
23857390SMatthew.Ahrens@Sun.COM 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
23867390SMatthew.Ahrens@Sun.COM 	 * So each snapshot gave birth to:
23877390SMatthew.Ahrens@Sun.COM 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
23886689Smaybee 	 * So a sequence would look like:
23897390SMatthew.Ahrens@Sun.COM 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
23906689Smaybee 	 * Which simplifies to:
23917390SMatthew.Ahrens@Sun.COM 	 * uN + kN + kN-1 + ... + k1 + k0
23926689Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
23937390SMatthew.Ahrens@Sun.COM 	 * uN + kN + kN-1 + ... + kM - uM-1
23946689Smaybee 	 */
23956689Smaybee 	pa->used = origin_ds->ds_phys->ds_used_bytes;
23966689Smaybee 	pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
23976689Smaybee 	pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
23987390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(&pa->shared_snaps); snap;
23997390SMatthew.Ahrens@Sun.COM 	    snap = list_next(&pa->shared_snaps, snap)) {
24002082Seschrock 		uint64_t val, dlused, dlcomp, dluncomp;
24016689Smaybee 		dsl_dataset_t *ds = snap->ds;
24022082Seschrock 
24032082Seschrock 		/* Check that the snapshot name does not conflict */
24047390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_get_snapname(ds));
24056689Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
240610588SEric.Taylor@Sun.COM 		if (err == 0) {
240710588SEric.Taylor@Sun.COM 			err = EEXIST;
240810588SEric.Taylor@Sun.COM 			goto out;
240910588SEric.Taylor@Sun.COM 		}
24106689Smaybee 		if (err != ENOENT)
241110588SEric.Taylor@Sun.COM 			goto out;
24126689Smaybee 
24136689Smaybee 		/* The very first snapshot does not have a deadlist */
24147390SMatthew.Ahrens@Sun.COM 		if (ds->ds_phys->ds_prev_snap_obj == 0)
24157390SMatthew.Ahrens@Sun.COM 			continue;
24167390SMatthew.Ahrens@Sun.COM 
24177390SMatthew.Ahrens@Sun.COM 		if (err = bplist_space(&ds->ds_deadlist,
24187390SMatthew.Ahrens@Sun.COM 		    &dlused, &dlcomp, &dluncomp))
241910588SEric.Taylor@Sun.COM 			goto out;
24207390SMatthew.Ahrens@Sun.COM 		pa->used += dlused;
24217390SMatthew.Ahrens@Sun.COM 		pa->comp += dlcomp;
24227390SMatthew.Ahrens@Sun.COM 		pa->uncomp += dluncomp;
24237390SMatthew.Ahrens@Sun.COM 	}
24246689Smaybee 
24256689Smaybee 	/*
24266689Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
24276689Smaybee 	 * so we need to subtract out the clone origin's used space.
24286689Smaybee 	 */
24297390SMatthew.Ahrens@Sun.COM 	if (pa->origin_origin) {
24307390SMatthew.Ahrens@Sun.COM 		pa->used -= pa->origin_origin->ds_phys->ds_used_bytes;
24317390SMatthew.Ahrens@Sun.COM 		pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes;
24327390SMatthew.Ahrens@Sun.COM 		pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes;
24332082Seschrock 	}
24342082Seschrock 
24352082Seschrock 	/* Check that there is enough space here */
24367390SMatthew.Ahrens@Sun.COM 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
24377390SMatthew.Ahrens@Sun.COM 	    pa->used);
24387390SMatthew.Ahrens@Sun.COM 	if (err)
24397390SMatthew.Ahrens@Sun.COM 		return (err);
24407390SMatthew.Ahrens@Sun.COM 
24417390SMatthew.Ahrens@Sun.COM 	/*
24427390SMatthew.Ahrens@Sun.COM 	 * Compute the amounts of space that will be used by snapshots
24437390SMatthew.Ahrens@Sun.COM 	 * after the promotion (for both origin and clone).  For each,
24447390SMatthew.Ahrens@Sun.COM 	 * it is the amount of space that will be on all of their
24457390SMatthew.Ahrens@Sun.COM 	 * deadlists (that was not born before their new origin).
24467390SMatthew.Ahrens@Sun.COM 	 */
24477390SMatthew.Ahrens@Sun.COM 	if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
24487390SMatthew.Ahrens@Sun.COM 		uint64_t space;
24497390SMatthew.Ahrens@Sun.COM 
24507390SMatthew.Ahrens@Sun.COM 		/*
24517390SMatthew.Ahrens@Sun.COM 		 * Note, typically this will not be a clone of a clone,
245212296SLin.Ling@Sun.COM 		 * so dd_origin_txg will be < TXG_INITIAL, so
24537390SMatthew.Ahrens@Sun.COM 		 * these snaplist_space() -> bplist_space_birthrange()
24547390SMatthew.Ahrens@Sun.COM 		 * calls will be fast because they do not have to
24557390SMatthew.Ahrens@Sun.COM 		 * iterate over all bps.
24567390SMatthew.Ahrens@Sun.COM 		 */
24577390SMatthew.Ahrens@Sun.COM 		snap = list_head(&pa->origin_snaps);
24587390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->shared_snaps,
245912296SLin.Ling@Sun.COM 		    snap->ds->ds_dir->dd_origin_txg, &pa->cloneusedsnap);
24607390SMatthew.Ahrens@Sun.COM 		if (err)
24617390SMatthew.Ahrens@Sun.COM 			return (err);
24627390SMatthew.Ahrens@Sun.COM 
24637390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->clone_snaps,
246412296SLin.Ling@Sun.COM 		    snap->ds->ds_dir->dd_origin_txg, &space);
24657390SMatthew.Ahrens@Sun.COM 		if (err)
24667390SMatthew.Ahrens@Sun.COM 			return (err);
24677390SMatthew.Ahrens@Sun.COM 		pa->cloneusedsnap += space;
24686689Smaybee 	}
24697390SMatthew.Ahrens@Sun.COM 	if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
24707390SMatthew.Ahrens@Sun.COM 		err = snaplist_space(&pa->origin_snaps,
24717390SMatthew.Ahrens@Sun.COM 		    origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap);
24727390SMatthew.Ahrens@Sun.COM 		if (err)
24737390SMatthew.Ahrens@Sun.COM 			return (err);
24747390SMatthew.Ahrens@Sun.COM 	}
24757390SMatthew.Ahrens@Sun.COM 
24767390SMatthew.Ahrens@Sun.COM 	return (0);
247710588SEric.Taylor@Sun.COM out:
247810588SEric.Taylor@Sun.COM 	pa->err_ds =  snap->ds->ds_snapname;
247910588SEric.Taylor@Sun.COM 	return (err);
24802199Sahrens }
24812082Seschrock 
24822199Sahrens static void
248312296SLin.Ling@Sun.COM dsl_dataset_promote_sync(void *arg1, void *arg2, dmu_tx_t *tx)
24842199Sahrens {
24852199Sahrens 	dsl_dataset_t *hds = arg1;
24862199Sahrens 	struct promotearg *pa = arg2;
24877390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap = list_head(&pa->shared_snaps);
24886689Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
24897390SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *origin_head;
24902199Sahrens 	dsl_dir_t *dd = hds->ds_dir;
24912199Sahrens 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
24925367Sahrens 	dsl_dir_t *odd = NULL;
24937046Sahrens 	uint64_t oldnext_obj;
24947390SMatthew.Ahrens@Sun.COM 	int64_t delta;
24952199Sahrens 
24962199Sahrens 	ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
24972199Sahrens 
24987390SMatthew.Ahrens@Sun.COM 	snap = list_head(&pa->origin_snaps);
24997390SMatthew.Ahrens@Sun.COM 	origin_head = snap->ds;
25007390SMatthew.Ahrens@Sun.COM 
25012417Sahrens 	/*
25025367Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
25032417Sahrens 	 * changing.
25042417Sahrens 	 */
25055367Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object,
25065367Sahrens 	    NULL, FTAG, &odd));
25072082Seschrock 
25086689Smaybee 	/* change origin's next snap */
25096689Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
25107046Sahrens 	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
25117390SMatthew.Ahrens@Sun.COM 	snap = list_tail(&pa->clone_snaps);
25127390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
25137390SMatthew.Ahrens@Sun.COM 	origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
25146689Smaybee 
25157046Sahrens 	/* change the origin's next clone */
25167046Sahrens 	if (origin_ds->ds_phys->ds_next_clones_obj) {
251710801SMatthew.Ahrens@Sun.COM 		remove_from_next_clones(origin_ds, snap->ds->ds_object, tx);
25187046Sahrens 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
25197046Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
25207046Sahrens 		    oldnext_obj, tx));
25217046Sahrens 	}
25227046Sahrens 
25236689Smaybee 	/* change origin */
25246689Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
25256689Smaybee 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
25266689Smaybee 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
252712296SLin.Ling@Sun.COM 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
25286689Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
25296689Smaybee 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
253012296SLin.Ling@Sun.COM 	origin_head->ds_dir->dd_origin_txg =
253112296SLin.Ling@Sun.COM 	    origin_ds->ds_phys->ds_creation_txg;
25326689Smaybee 
25332082Seschrock 	/* move snapshots to this dir */
25347390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(&pa->shared_snaps); snap;
25357390SMatthew.Ahrens@Sun.COM 	    snap = list_next(&pa->shared_snaps, snap)) {
25366689Smaybee 		dsl_dataset_t *ds = snap->ds;
25372082Seschrock 
25387237Sek110237 		/* unregister props as dsl_dir is changing */
253910298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset) {
254010298SMatthew.Ahrens@Sun.COM 			dmu_objset_evict(ds->ds_objset);
254110298SMatthew.Ahrens@Sun.COM 			ds->ds_objset = NULL;
25427237Sek110237 		}
25432082Seschrock 		/* move snap name entry */
25447390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_get_snapname(ds));
25457390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_snap_remove(origin_head,
25466689Smaybee 		    ds->ds_snapname, tx));
25472199Sahrens 		VERIFY(0 == zap_add(dp->dp_meta_objset,
25482082Seschrock 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
25492082Seschrock 		    8, 1, &ds->ds_object, tx));
25502082Seschrock 		/* change containing dsl_dir */
25512082Seschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
25525367Sahrens 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
25532082Seschrock 		ds->ds_phys->ds_dir_obj = dd->dd_object;
25545367Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
25552082Seschrock 		dsl_dir_close(ds->ds_dir, ds);
25562199Sahrens 		VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
25572082Seschrock 		    NULL, ds, &ds->ds_dir));
25582082Seschrock 
25592082Seschrock 		ASSERT3U(dsl_prop_numcb(ds), ==, 0);
25607390SMatthew.Ahrens@Sun.COM 	}
25617390SMatthew.Ahrens@Sun.COM 
25627390SMatthew.Ahrens@Sun.COM 	/*
25637390SMatthew.Ahrens@Sun.COM 	 * Change space accounting.
25647390SMatthew.Ahrens@Sun.COM 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
25657390SMatthew.Ahrens@Sun.COM 	 * both be valid, or both be 0 (resulting in delta == 0).  This
25667390SMatthew.Ahrens@Sun.COM 	 * is true for each of {clone,origin} independently.
25677390SMatthew.Ahrens@Sun.COM 	 */
25687390SMatthew.Ahrens@Sun.COM 
25697390SMatthew.Ahrens@Sun.COM 	delta = pa->cloneusedsnap -
25707390SMatthew.Ahrens@Sun.COM 	    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
25717390SMatthew.Ahrens@Sun.COM 	ASSERT3S(delta, >=, 0);
25727390SMatthew.Ahrens@Sun.COM 	ASSERT3U(pa->used, >=, delta);
25737390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
25747390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
25757390SMatthew.Ahrens@Sun.COM 	    pa->used - delta, pa->comp, pa->uncomp, tx);
25767390SMatthew.Ahrens@Sun.COM 
25777390SMatthew.Ahrens@Sun.COM 	delta = pa->originusedsnap -
25787390SMatthew.Ahrens@Sun.COM 	    odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
25797390SMatthew.Ahrens@Sun.COM 	ASSERT3S(delta, <=, 0);
25807390SMatthew.Ahrens@Sun.COM 	ASSERT3U(pa->used, >=, -delta);
25817390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
25827390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
25837390SMatthew.Ahrens@Sun.COM 	    -pa->used - delta, -pa->comp, -pa->uncomp, tx);
25847390SMatthew.Ahrens@Sun.COM 
25855367Sahrens 	origin_ds->ds_phys->ds_unique_bytes = pa->unique;
25862082Seschrock 
25874543Smarks 	/* log history record */
258812296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
258912296SLin.Ling@Sun.COM 	    "dataset = %llu", hds->ds_object);
25904543Smarks 
25915367Sahrens 	dsl_dir_close(odd, FTAG);
25922082Seschrock }
25932082Seschrock 
25947390SMatthew.Ahrens@Sun.COM static char *snaplist_tag = "snaplist";
25957390SMatthew.Ahrens@Sun.COM /*
25967390SMatthew.Ahrens@Sun.COM  * Make a list of dsl_dataset_t's for the snapshots between first_obj
25977390SMatthew.Ahrens@Sun.COM  * (exclusive) and last_obj (inclusive).  The list will be in reverse
25987390SMatthew.Ahrens@Sun.COM  * order (last_obj will be the list_head()).  If first_obj == 0, do all
25997390SMatthew.Ahrens@Sun.COM  * snapshots back to this dataset's origin.
26007390SMatthew.Ahrens@Sun.COM  */
26017390SMatthew.Ahrens@Sun.COM static int
26027390SMatthew.Ahrens@Sun.COM snaplist_make(dsl_pool_t *dp, boolean_t own,
26037390SMatthew.Ahrens@Sun.COM     uint64_t first_obj, uint64_t last_obj, list_t *l)
26047390SMatthew.Ahrens@Sun.COM {
26057390SMatthew.Ahrens@Sun.COM 	uint64_t obj = last_obj;
26067390SMatthew.Ahrens@Sun.COM 
26077390SMatthew.Ahrens@Sun.COM 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock));
26087390SMatthew.Ahrens@Sun.COM 
26097390SMatthew.Ahrens@Sun.COM 	list_create(l, sizeof (struct promotenode),
26107390SMatthew.Ahrens@Sun.COM 	    offsetof(struct promotenode, link));
26117390SMatthew.Ahrens@Sun.COM 
26127390SMatthew.Ahrens@Sun.COM 	while (obj != first_obj) {
26137390SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
26147390SMatthew.Ahrens@Sun.COM 		struct promotenode *snap;
26157390SMatthew.Ahrens@Sun.COM 		int err;
26167390SMatthew.Ahrens@Sun.COM 
26177390SMatthew.Ahrens@Sun.COM 		if (own) {
26187390SMatthew.Ahrens@Sun.COM 			err = dsl_dataset_own_obj(dp, obj,
26197390SMatthew.Ahrens@Sun.COM 			    0, snaplist_tag, &ds);
26207390SMatthew.Ahrens@Sun.COM 			if (err == 0)
26217390SMatthew.Ahrens@Sun.COM 				dsl_dataset_make_exclusive(ds, snaplist_tag);
26227390SMatthew.Ahrens@Sun.COM 		} else {
26237390SMatthew.Ahrens@Sun.COM 			err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds);
26247390SMatthew.Ahrens@Sun.COM 		}
26257390SMatthew.Ahrens@Sun.COM 		if (err == ENOENT) {
26267390SMatthew.Ahrens@Sun.COM 			/* lost race with snapshot destroy */
26277390SMatthew.Ahrens@Sun.COM 			struct promotenode *last = list_tail(l);
26287390SMatthew.Ahrens@Sun.COM 			ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj);
26297390SMatthew.Ahrens@Sun.COM 			obj = last->ds->ds_phys->ds_prev_snap_obj;
26307390SMatthew.Ahrens@Sun.COM 			continue;
26317390SMatthew.Ahrens@Sun.COM 		} else if (err) {
26327390SMatthew.Ahrens@Sun.COM 			return (err);
26337390SMatthew.Ahrens@Sun.COM 		}
26347390SMatthew.Ahrens@Sun.COM 
26357390SMatthew.Ahrens@Sun.COM 		if (first_obj == 0)
26367390SMatthew.Ahrens@Sun.COM 			first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
26377390SMatthew.Ahrens@Sun.COM 
26387390SMatthew.Ahrens@Sun.COM 		snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP);
26397390SMatthew.Ahrens@Sun.COM 		snap->ds = ds;
26407390SMatthew.Ahrens@Sun.COM 		list_insert_tail(l, snap);
26417390SMatthew.Ahrens@Sun.COM 		obj = ds->ds_phys->ds_prev_snap_obj;
26427390SMatthew.Ahrens@Sun.COM 	}
26437390SMatthew.Ahrens@Sun.COM 
26447390SMatthew.Ahrens@Sun.COM 	return (0);
26457390SMatthew.Ahrens@Sun.COM }
26467390SMatthew.Ahrens@Sun.COM 
26477390SMatthew.Ahrens@Sun.COM static int
26487390SMatthew.Ahrens@Sun.COM snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
26497390SMatthew.Ahrens@Sun.COM {
26507390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap;
26517390SMatthew.Ahrens@Sun.COM 
26527390SMatthew.Ahrens@Sun.COM 	*spacep = 0;
26537390SMatthew.Ahrens@Sun.COM 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
26547390SMatthew.Ahrens@Sun.COM 		uint64_t used;
26557390SMatthew.Ahrens@Sun.COM 		int err = bplist_space_birthrange(&snap->ds->ds_deadlist,
26567390SMatthew.Ahrens@Sun.COM 		    mintxg, UINT64_MAX, &used);
26577390SMatthew.Ahrens@Sun.COM 		if (err)
26587390SMatthew.Ahrens@Sun.COM 			return (err);
26597390SMatthew.Ahrens@Sun.COM 		*spacep += used;
26607390SMatthew.Ahrens@Sun.COM 	}
26617390SMatthew.Ahrens@Sun.COM 	return (0);
26627390SMatthew.Ahrens@Sun.COM }
26637390SMatthew.Ahrens@Sun.COM 
26647390SMatthew.Ahrens@Sun.COM static void
26657390SMatthew.Ahrens@Sun.COM snaplist_destroy(list_t *l, boolean_t own)
26667390SMatthew.Ahrens@Sun.COM {
26677390SMatthew.Ahrens@Sun.COM 	struct promotenode *snap;
26687390SMatthew.Ahrens@Sun.COM 
26698779SMark.Musante@Sun.COM 	if (!l || !list_link_active(&l->list_head))
26707390SMatthew.Ahrens@Sun.COM 		return;
26717390SMatthew.Ahrens@Sun.COM 
26727390SMatthew.Ahrens@Sun.COM 	while ((snap = list_tail(l)) != NULL) {
26737390SMatthew.Ahrens@Sun.COM 		list_remove(l, snap);
26747390SMatthew.Ahrens@Sun.COM 		if (own)
26757390SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(snap->ds, snaplist_tag);
26767390SMatthew.Ahrens@Sun.COM 		else
26777390SMatthew.Ahrens@Sun.COM 			dsl_dataset_rele(snap->ds, snaplist_tag);
26787390SMatthew.Ahrens@Sun.COM 		kmem_free(snap, sizeof (struct promotenode));
26797390SMatthew.Ahrens@Sun.COM 	}
26807390SMatthew.Ahrens@Sun.COM 	list_destroy(l);
26817390SMatthew.Ahrens@Sun.COM }
26827390SMatthew.Ahrens@Sun.COM 
26837390SMatthew.Ahrens@Sun.COM /*
26847390SMatthew.Ahrens@Sun.COM  * Promote a clone.  Nomenclature note:
26857390SMatthew.Ahrens@Sun.COM  * "clone" or "cds": the original clone which is being promoted
26867390SMatthew.Ahrens@Sun.COM  * "origin" or "ods": the snapshot which is originally clone's origin
26877390SMatthew.Ahrens@Sun.COM  * "origin head" or "ohds": the dataset which is the head
26887390SMatthew.Ahrens@Sun.COM  * (filesystem/volume) for the origin
26897390SMatthew.Ahrens@Sun.COM  * "origin origin": the origin of the origin's filesystem (typically
26907390SMatthew.Ahrens@Sun.COM  * NULL, indicating that the clone is not a clone of a clone).
26917390SMatthew.Ahrens@Sun.COM  */
26922082Seschrock int
269310588SEric.Taylor@Sun.COM dsl_dataset_promote(const char *name, char *conflsnap)
26942082Seschrock {
26952082Seschrock 	dsl_dataset_t *ds;
26966689Smaybee 	dsl_dir_t *dd;
26976689Smaybee 	dsl_pool_t *dp;
26982082Seschrock 	dmu_object_info_t doi;
26997390SMatthew.Ahrens@Sun.COM 	struct promotearg pa = { 0 };
27007046Sahrens 	struct promotenode *snap;
27016689Smaybee 	int err;
27026689Smaybee 
27036689Smaybee 	err = dsl_dataset_hold(name, FTAG, &ds);
27042082Seschrock 	if (err)
27052082Seschrock 		return (err);
27066689Smaybee 	dd = ds->ds_dir;
27076689Smaybee 	dp = dd->dd_pool;
27086689Smaybee 
27096689Smaybee 	err = dmu_object_info(dp->dp_meta_objset,
27102082Seschrock 	    ds->ds_phys->ds_snapnames_zapobj, &doi);
27112082Seschrock 	if (err) {
27126689Smaybee 		dsl_dataset_rele(ds, FTAG);
27132082Seschrock 		return (err);
27142082Seschrock 	}
27152082Seschrock 
27167390SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) {
27177390SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
27187390SMatthew.Ahrens@Sun.COM 		return (EINVAL);
27197390SMatthew.Ahrens@Sun.COM 	}
27207390SMatthew.Ahrens@Sun.COM 
27212082Seschrock 	/*
27226689Smaybee 	 * We are going to inherit all the snapshots taken before our
27236689Smaybee 	 * origin (i.e., our new origin will be our parent's origin).
27246689Smaybee 	 * Take ownership of them so that we can rename them into our
27256689Smaybee 	 * namespace.
27266689Smaybee 	 */
27276689Smaybee 	rw_enter(&dp->dp_config_rwlock, RW_READER);
27287390SMatthew.Ahrens@Sun.COM 
27297390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj,
27307390SMatthew.Ahrens@Sun.COM 	    &pa.shared_snaps);
27317390SMatthew.Ahrens@Sun.COM 	if (err != 0)
27327390SMatthew.Ahrens@Sun.COM 		goto out;
27337390SMatthew.Ahrens@Sun.COM 
27347390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps);
27357390SMatthew.Ahrens@Sun.COM 	if (err != 0)
27367390SMatthew.Ahrens@Sun.COM 		goto out;
27377390SMatthew.Ahrens@Sun.COM 
27387390SMatthew.Ahrens@Sun.COM 	snap = list_head(&pa.shared_snaps);
27397390SMatthew.Ahrens@Sun.COM 	ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
27407390SMatthew.Ahrens@Sun.COM 	err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj,
27417390SMatthew.Ahrens@Sun.COM 	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps);
27427390SMatthew.Ahrens@Sun.COM 	if (err != 0)
27437390SMatthew.Ahrens@Sun.COM 		goto out;
27447390SMatthew.Ahrens@Sun.COM 
27457390SMatthew.Ahrens@Sun.COM 	if (dsl_dir_is_clone(snap->ds->ds_dir)) {
27467390SMatthew.Ahrens@Sun.COM 		err = dsl_dataset_own_obj(dp,
27477390SMatthew.Ahrens@Sun.COM 		    snap->ds->ds_dir->dd_phys->dd_origin_obj,
27487390SMatthew.Ahrens@Sun.COM 		    0, FTAG, &pa.origin_origin);
27497390SMatthew.Ahrens@Sun.COM 		if (err != 0)
27506689Smaybee 			goto out;
27516689Smaybee 	}
27527390SMatthew.Ahrens@Sun.COM 
27537390SMatthew.Ahrens@Sun.COM out:
27546689Smaybee 	rw_exit(&dp->dp_config_rwlock);
27556689Smaybee 
27566689Smaybee 	/*
27572082Seschrock 	 * Add in 128x the snapnames zapobj size, since we will be moving
27582082Seschrock 	 * a bunch of snapnames to the promoted ds, and dirtying their
27592082Seschrock 	 * bonus buffers.
27602082Seschrock 	 */
27617390SMatthew.Ahrens@Sun.COM 	if (err == 0) {
27627390SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_do(dp, dsl_dataset_promote_check,
27637390SMatthew.Ahrens@Sun.COM 		    dsl_dataset_promote_sync, ds, &pa,
276410922SJeff.Bonwick@Sun.COM 		    2 + 2 * doi.doi_physical_blocks_512);
276510588SEric.Taylor@Sun.COM 		if (err && pa.err_ds && conflsnap)
276610588SEric.Taylor@Sun.COM 			(void) strncpy(conflsnap, pa.err_ds, MAXNAMELEN);
27676689Smaybee 	}
27687390SMatthew.Ahrens@Sun.COM 
27697390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.shared_snaps, B_TRUE);
27707390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.clone_snaps, B_FALSE);
27717390SMatthew.Ahrens@Sun.COM 	snaplist_destroy(&pa.origin_snaps, B_FALSE);
27727390SMatthew.Ahrens@Sun.COM 	if (pa.origin_origin)
27737390SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(pa.origin_origin, FTAG);
27746689Smaybee 	dsl_dataset_rele(ds, FTAG);
27752082Seschrock 	return (err);
27762082Seschrock }
27773912Slling 
27785367Sahrens struct cloneswaparg {
27795367Sahrens 	dsl_dataset_t *cds; /* clone dataset */
27805367Sahrens 	dsl_dataset_t *ohds; /* origin's head dataset */
27815367Sahrens 	boolean_t force;
27825481Sck153898 	int64_t unused_refres_delta; /* change in unconsumed refreservation */
27835367Sahrens };
27845326Sek110237 
27855326Sek110237 /* ARGSUSED */
27865326Sek110237 static int
27875326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx)
27885326Sek110237 {
27895367Sahrens 	struct cloneswaparg *csa = arg1;
27905326Sek110237 
27915367Sahrens 	/* they should both be heads */
27925367Sahrens 	if (dsl_dataset_is_snapshot(csa->cds) ||
27935367Sahrens 	    dsl_dataset_is_snapshot(csa->ohds))
27945326Sek110237 		return (EINVAL);
27955326Sek110237 
27965367Sahrens 	/* the branch point should be just before them */
27975367Sahrens 	if (csa->cds->ds_prev != csa->ohds->ds_prev)
27985326Sek110237 		return (EINVAL);
27995326Sek110237 
280010272SMatthew.Ahrens@Sun.COM 	/* cds should be the clone (unless they are unrelated) */
280110272SMatthew.Ahrens@Sun.COM 	if (csa->cds->ds_prev != NULL &&
280210272SMatthew.Ahrens@Sun.COM 	    csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap &&
280310272SMatthew.Ahrens@Sun.COM 	    csa->ohds->ds_object !=
280410272SMatthew.Ahrens@Sun.COM 	    csa->cds->ds_prev->ds_phys->ds_next_snap_obj)
28055367Sahrens 		return (EINVAL);
28065326Sek110237 
28075367Sahrens 	/* the clone should be a child of the origin */
28085367Sahrens 	if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir)
28095367Sahrens 		return (EINVAL);
28105326Sek110237 
28115367Sahrens 	/* ohds shouldn't be modified unless 'force' */
28125367Sahrens 	if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds))
28135367Sahrens 		return (ETXTBSY);
28145481Sck153898 
28155481Sck153898 	/* adjust amount of any unconsumed refreservation */
28165481Sck153898 	csa->unused_refres_delta =
28175481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
28185481Sck153898 	    csa->ohds->ds_phys->ds_unique_bytes) -
28195481Sck153898 	    (int64_t)MIN(csa->ohds->ds_reserved,
28205481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
28215481Sck153898 
28225481Sck153898 	if (csa->unused_refres_delta > 0 &&
28235481Sck153898 	    csa->unused_refres_delta >
28245481Sck153898 	    dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE))
28255481Sck153898 		return (ENOSPC);
28265481Sck153898 
282710799SChris.Kirby@sun.com 	if (csa->ohds->ds_quota != 0 &&
282810799SChris.Kirby@sun.com 	    csa->cds->ds_phys->ds_unique_bytes > csa->ohds->ds_quota)
282910799SChris.Kirby@sun.com 		return (EDQUOT);
283010799SChris.Kirby@sun.com 
28315367Sahrens 	return (0);
28325326Sek110237 }
28335326Sek110237 
28345326Sek110237 /* ARGSUSED */
28355326Sek110237 static void
283612296SLin.Ling@Sun.COM dsl_dataset_clone_swap_sync(void *arg1, void *arg2, dmu_tx_t *tx)
28375326Sek110237 {
28385367Sahrens 	struct cloneswaparg *csa = arg1;
28395367Sahrens 	dsl_pool_t *dp = csa->cds->ds_dir->dd_pool;
28405326Sek110237 
28415481Sck153898 	ASSERT(csa->cds->ds_reserved == 0);
284210799SChris.Kirby@sun.com 	ASSERT(csa->ohds->ds_quota == 0 ||
284310799SChris.Kirby@sun.com 	    csa->cds->ds_phys->ds_unique_bytes <= csa->ohds->ds_quota);
28445481Sck153898 
28455367Sahrens 	dmu_buf_will_dirty(csa->cds->ds_dbuf, tx);
28465367Sahrens 	dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx);
28475326Sek110237 
284810298SMatthew.Ahrens@Sun.COM 	if (csa->cds->ds_objset != NULL) {
284910298SMatthew.Ahrens@Sun.COM 		dmu_objset_evict(csa->cds->ds_objset);
285010298SMatthew.Ahrens@Sun.COM 		csa->cds->ds_objset = NULL;
28515367Sahrens 	}
28525326Sek110237 
285310298SMatthew.Ahrens@Sun.COM 	if (csa->ohds->ds_objset != NULL) {
285410298SMatthew.Ahrens@Sun.COM 		dmu_objset_evict(csa->ohds->ds_objset);
285510298SMatthew.Ahrens@Sun.COM 		csa->ohds->ds_objset = NULL;
28565367Sahrens 	}
28575326Sek110237 
285810272SMatthew.Ahrens@Sun.COM 	/*
285910272SMatthew.Ahrens@Sun.COM 	 * Reset origin's unique bytes, if it exists.
286010272SMatthew.Ahrens@Sun.COM 	 */
286110272SMatthew.Ahrens@Sun.COM 	if (csa->cds->ds_prev) {
286210272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *origin = csa->cds->ds_prev;
286310272SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
286410272SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist,
286510272SMatthew.Ahrens@Sun.COM 		    origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
286610272SMatthew.Ahrens@Sun.COM 		    &origin->ds_phys->ds_unique_bytes));
286710272SMatthew.Ahrens@Sun.COM 	}
28685326Sek110237 
28695326Sek110237 	/* swap blkptrs */
28705326Sek110237 	{
28715326Sek110237 		blkptr_t tmp;
28725367Sahrens 		tmp = csa->ohds->ds_phys->ds_bp;
28735367Sahrens 		csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp;
28745367Sahrens 		csa->cds->ds_phys->ds_bp = tmp;
28755326Sek110237 	}
28765326Sek110237 
28775326Sek110237 	/* set dd_*_bytes */
28785326Sek110237 	{
28795326Sek110237 		int64_t dused, dcomp, duncomp;
28805326Sek110237 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
28815326Sek110237 		uint64_t odl_used, odl_comp, odl_uncomp;
28825326Sek110237 
28837390SMatthew.Ahrens@Sun.COM 		ASSERT3U(csa->cds->ds_dir->dd_phys->
28847390SMatthew.Ahrens@Sun.COM 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
28857390SMatthew.Ahrens@Sun.COM 
28865367Sahrens 		VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used,
28875326Sek110237 		    &cdl_comp, &cdl_uncomp));
28885367Sahrens 		VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used,
28895326Sek110237 		    &odl_comp, &odl_uncomp));
28907390SMatthew.Ahrens@Sun.COM 
28915367Sahrens 		dused = csa->cds->ds_phys->ds_used_bytes + cdl_used -
28925367Sahrens 		    (csa->ohds->ds_phys->ds_used_bytes + odl_used);
28935367Sahrens 		dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
28945367Sahrens 		    (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
28955367Sahrens 		duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
28965367Sahrens 		    cdl_uncomp -
28975367Sahrens 		    (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
28985326Sek110237 
28997390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD,
29005367Sahrens 		    dused, dcomp, duncomp, tx);
29017390SMatthew.Ahrens@Sun.COM 		dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD,
29025367Sahrens 		    -dused, -dcomp, -duncomp, tx);
29037390SMatthew.Ahrens@Sun.COM 
29047390SMatthew.Ahrens@Sun.COM 		/*
29057390SMatthew.Ahrens@Sun.COM 		 * The difference in the space used by snapshots is the
29067390SMatthew.Ahrens@Sun.COM 		 * difference in snapshot space due to the head's
29077390SMatthew.Ahrens@Sun.COM 		 * deadlist (since that's the only thing that's
29087390SMatthew.Ahrens@Sun.COM 		 * changing that affects the snapused).
29097390SMatthew.Ahrens@Sun.COM 		 */
29107390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist,
291112296SLin.Ling@Sun.COM 		    csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX, &cdl_used));
29127390SMatthew.Ahrens@Sun.COM 		VERIFY(0 == bplist_space_birthrange(&csa->ohds->ds_deadlist,
291312296SLin.Ling@Sun.COM 		    csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX, &odl_used));
29147390SMatthew.Ahrens@Sun.COM 		dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used,
29157390SMatthew.Ahrens@Sun.COM 		    DD_USED_HEAD, DD_USED_SNAP, tx);
29165367Sahrens 	}
29175367Sahrens 
29185367Sahrens #define	SWITCH64(x, y) \
29195367Sahrens 	{ \
29205367Sahrens 		uint64_t __tmp = (x); \
29215367Sahrens 		(x) = (y); \
29225367Sahrens 		(y) = __tmp; \
29235326Sek110237 	}
29245326Sek110237 
29255326Sek110237 	/* swap ds_*_bytes */
29265367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_used_bytes,
29275367Sahrens 	    csa->cds->ds_phys->ds_used_bytes);
29285367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
29295367Sahrens 	    csa->cds->ds_phys->ds_compressed_bytes);
29305367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
29315367Sahrens 	    csa->cds->ds_phys->ds_uncompressed_bytes);
29325481Sck153898 	SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
29335481Sck153898 	    csa->cds->ds_phys->ds_unique_bytes);
29345481Sck153898 
29355481Sck153898 	/* apply any parent delta for change in unconsumed refreservation */
29367390SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV,
29377390SMatthew.Ahrens@Sun.COM 	    csa->unused_refres_delta, 0, 0, tx);
29385326Sek110237 
29395326Sek110237 	/* swap deadlists */
29405367Sahrens 	bplist_close(&csa->cds->ds_deadlist);
29415367Sahrens 	bplist_close(&csa->ohds->ds_deadlist);
29425367Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
29435367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj);
29445367Sahrens 	VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
29455367Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj));
29465367Sahrens 	VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
29475367Sahrens 	    csa->ohds->ds_phys->ds_deadlist_obj));
29487837SMatthew.Ahrens@Sun.COM 
294912296SLin.Ling@Sun.COM 	dsl_scan_ds_clone_swapped(csa->ohds, csa->cds, tx);
29505326Sek110237 }
29515326Sek110237 
29525326Sek110237 /*
295310272SMatthew.Ahrens@Sun.COM  * Swap 'clone' with its origin head datasets.  Used at the end of "zfs
295410272SMatthew.Ahrens@Sun.COM  * recv" into an existing fs to swizzle the file system to the new
295510272SMatthew.Ahrens@Sun.COM  * version, and by "zfs rollback".  Can also be used to swap two
295610272SMatthew.Ahrens@Sun.COM  * independent head datasets if neither has any snapshots.
29575326Sek110237  */
29585326Sek110237 int
29595367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
29605367Sahrens     boolean_t force)
29615326Sek110237 {
29625367Sahrens 	struct cloneswaparg csa;
29636689Smaybee 	int error;
29646689Smaybee 
29656689Smaybee 	ASSERT(clone->ds_owner);
29666689Smaybee 	ASSERT(origin_head->ds_owner);
29676689Smaybee retry:
29686689Smaybee 	/* Need exclusive access for the swap */
29696689Smaybee 	rw_enter(&clone->ds_rwlock, RW_WRITER);
29706689Smaybee 	if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) {
29716689Smaybee 		rw_exit(&clone->ds_rwlock);
29726689Smaybee 		rw_enter(&origin_head->ds_rwlock, RW_WRITER);
29736689Smaybee 		if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) {
29746689Smaybee 			rw_exit(&origin_head->ds_rwlock);
29756689Smaybee 			goto retry;
29766689Smaybee 		}
29776689Smaybee 	}
29785367Sahrens 	csa.cds = clone;
29795367Sahrens 	csa.ohds = origin_head;
29805367Sahrens 	csa.force = force;
29816689Smaybee 	error = dsl_sync_task_do(clone->ds_dir->dd_pool,
29825326Sek110237 	    dsl_dataset_clone_swap_check,
29836689Smaybee 	    dsl_dataset_clone_swap_sync, &csa, NULL, 9);
29846689Smaybee 	return (error);
29855326Sek110237 }
29865326Sek110237 
29873912Slling /*
29883912Slling  * Given a pool name and a dataset object number in that pool,
29893912Slling  * return the name of that dataset.
29903912Slling  */
29913912Slling int
29923912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
29933912Slling {
29943912Slling 	spa_t *spa;
29953912Slling 	dsl_pool_t *dp;
29966689Smaybee 	dsl_dataset_t *ds;
29973912Slling 	int error;
29983912Slling 
29993912Slling 	if ((error = spa_open(pname, &spa, FTAG)) != 0)
30003912Slling 		return (error);
30013912Slling 	dp = spa_get_dsl(spa);
30023912Slling 	rw_enter(&dp->dp_config_rwlock, RW_READER);
30036689Smaybee 	if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) {
30046689Smaybee 		dsl_dataset_name(ds, buf);
30056689Smaybee 		dsl_dataset_rele(ds, FTAG);
30063912Slling 	}
30073912Slling 	rw_exit(&dp->dp_config_rwlock);
30083912Slling 	spa_close(spa, FTAG);
30093912Slling 
30106689Smaybee 	return (error);
30113912Slling }
30125378Sck153898 
30135378Sck153898 int
30145378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
30156689Smaybee     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
30165378Sck153898 {
30175378Sck153898 	int error = 0;
30185378Sck153898 
30195378Sck153898 	ASSERT3S(asize, >, 0);
30205378Sck153898 
30215831Sck153898 	/*
30225831Sck153898 	 * *ref_rsrv is the portion of asize that will come from any
30235831Sck153898 	 * unconsumed refreservation space.
30245831Sck153898 	 */
30255831Sck153898 	*ref_rsrv = 0;
30265831Sck153898 
30275378Sck153898 	mutex_enter(&ds->ds_lock);
30285378Sck153898 	/*
30295378Sck153898 	 * Make a space adjustment for reserved bytes.
30305378Sck153898 	 */
30315378Sck153898 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
30325378Sck153898 		ASSERT3U(*used, >=,
30335378Sck153898 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
30345378Sck153898 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
30355831Sck153898 		*ref_rsrv =
30365831Sck153898 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
30375378Sck153898 	}
30385378Sck153898 
30395378Sck153898 	if (!check_quota || ds->ds_quota == 0) {
30405378Sck153898 		mutex_exit(&ds->ds_lock);
30415378Sck153898 		return (0);
30425378Sck153898 	}
30435378Sck153898 	/*
30445378Sck153898 	 * If they are requesting more space, and our current estimate
30455378Sck153898 	 * is over quota, they get to try again unless the actual
30465378Sck153898 	 * on-disk is over quota and there are no pending changes (which
30475378Sck153898 	 * may free up space for us).
30485378Sck153898 	 */
30495378Sck153898 	if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) {
30505378Sck153898 		if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota)
30515378Sck153898 			error = ERESTART;
30525378Sck153898 		else
30535378Sck153898 			error = EDQUOT;
30545378Sck153898 	}
30555378Sck153898 	mutex_exit(&ds->ds_lock);
30565378Sck153898 
30575378Sck153898 	return (error);
30585378Sck153898 }
30595378Sck153898 
30605378Sck153898 /* ARGSUSED */
30615378Sck153898 static int
30625378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
30635378Sck153898 {
30645378Sck153898 	dsl_dataset_t *ds = arg1;
306511022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
306611022STom.Erickson@Sun.COM 	int err;
30675378Sck153898 
30685378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA)
30695378Sck153898 		return (ENOTSUP);
30705378Sck153898 
307111022STom.Erickson@Sun.COM 	if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
307211022STom.Erickson@Sun.COM 		return (err);
307311022STom.Erickson@Sun.COM 
307411022STom.Erickson@Sun.COM 	if (psa->psa_effective_value == 0)
30755378Sck153898 		return (0);
30765378Sck153898 
307711022STom.Erickson@Sun.COM 	if (psa->psa_effective_value < ds->ds_phys->ds_used_bytes ||
307811022STom.Erickson@Sun.COM 	    psa->psa_effective_value < ds->ds_reserved)
30795378Sck153898 		return (ENOSPC);
30805378Sck153898 
30815378Sck153898 	return (0);
30825378Sck153898 }
30835378Sck153898 
308412296SLin.Ling@Sun.COM extern void dsl_prop_set_sync(void *, void *, dmu_tx_t *);
308511022STom.Erickson@Sun.COM 
30865378Sck153898 void
308712296SLin.Ling@Sun.COM dsl_dataset_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
30885378Sck153898 {
30895378Sck153898 	dsl_dataset_t *ds = arg1;
309011022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
309111022STom.Erickson@Sun.COM 	uint64_t effective_value = psa->psa_effective_value;
309211022STom.Erickson@Sun.COM 
309312296SLin.Ling@Sun.COM 	dsl_prop_set_sync(ds, psa, tx);
309411022STom.Erickson@Sun.COM 	DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
309511022STom.Erickson@Sun.COM 
309611022STom.Erickson@Sun.COM 	if (ds->ds_quota != effective_value) {
309711022STom.Erickson@Sun.COM 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
309811022STom.Erickson@Sun.COM 		ds->ds_quota = effective_value;
309911022STom.Erickson@Sun.COM 
310012296SLin.Ling@Sun.COM 		spa_history_log_internal(LOG_DS_REFQUOTA,
310112296SLin.Ling@Sun.COM 		    ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu ",
310211022STom.Erickson@Sun.COM 		    (longlong_t)ds->ds_quota, ds->ds_object);
310311022STom.Erickson@Sun.COM 	}
31045378Sck153898 }
31055378Sck153898 
31065378Sck153898 int
310711022STom.Erickson@Sun.COM dsl_dataset_set_quota(const char *dsname, zprop_source_t source, uint64_t quota)
31085378Sck153898 {
31095378Sck153898 	dsl_dataset_t *ds;
311011022STom.Erickson@Sun.COM 	dsl_prop_setarg_t psa;
31115378Sck153898 	int err;
31125378Sck153898 
311311022STom.Erickson@Sun.COM 	dsl_prop_setarg_init_uint64(&psa, "refquota", source, &quota);
311411022STom.Erickson@Sun.COM 
31156689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
31165378Sck153898 	if (err)
31175378Sck153898 		return (err);
31185378Sck153898 
311911022STom.Erickson@Sun.COM 	/*
312011022STom.Erickson@Sun.COM 	 * If someone removes a file, then tries to set the quota, we
312111022STom.Erickson@Sun.COM 	 * want to make sure the file freeing takes effect.
312211022STom.Erickson@Sun.COM 	 */
312311022STom.Erickson@Sun.COM 	txg_wait_open(ds->ds_dir->dd_pool, 0);
312411022STom.Erickson@Sun.COM 
312511022STom.Erickson@Sun.COM 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
312611022STom.Erickson@Sun.COM 	    dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync,
312711022STom.Erickson@Sun.COM 	    ds, &psa, 0);
312811022STom.Erickson@Sun.COM 
31296689Smaybee 	dsl_dataset_rele(ds, FTAG);
31305378Sck153898 	return (err);
31315378Sck153898 }
31325378Sck153898 
31335378Sck153898 static int
31345378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
31355378Sck153898 {
31365378Sck153898 	dsl_dataset_t *ds = arg1;
313711022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
313811022STom.Erickson@Sun.COM 	uint64_t effective_value;
31395378Sck153898 	uint64_t unique;
314011022STom.Erickson@Sun.COM 	int err;
31415378Sck153898 
31425378Sck153898 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
31435378Sck153898 	    SPA_VERSION_REFRESERVATION)
31445378Sck153898 		return (ENOTSUP);
31455378Sck153898 
31465378Sck153898 	if (dsl_dataset_is_snapshot(ds))
31475378Sck153898 		return (EINVAL);
31485378Sck153898 
314911022STom.Erickson@Sun.COM 	if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
315011022STom.Erickson@Sun.COM 		return (err);
315111022STom.Erickson@Sun.COM 
315211022STom.Erickson@Sun.COM 	effective_value = psa->psa_effective_value;
315311022STom.Erickson@Sun.COM 
31545378Sck153898 	/*
31555378Sck153898 	 * If we are doing the preliminary check in open context, the
31565378Sck153898 	 * space estimates may be inaccurate.
31575378Sck153898 	 */
31585378Sck153898 	if (!dmu_tx_is_syncing(tx))
31595378Sck153898 		return (0);
31605378Sck153898 
31615378Sck153898 	mutex_enter(&ds->ds_lock);
316212296SLin.Ling@Sun.COM 	if (!DS_UNIQUE_IS_ACCURATE(ds))
316312296SLin.Ling@Sun.COM 		dsl_dataset_recalc_head_uniq(ds);
316412296SLin.Ling@Sun.COM 	unique = ds->ds_phys->ds_unique_bytes;
31655378Sck153898 	mutex_exit(&ds->ds_lock);
31665378Sck153898 
316711022STom.Erickson@Sun.COM 	if (MAX(unique, effective_value) > MAX(unique, ds->ds_reserved)) {
316811022STom.Erickson@Sun.COM 		uint64_t delta = MAX(unique, effective_value) -
31698525SEric.Schrock@Sun.COM 		    MAX(unique, ds->ds_reserved);
31708525SEric.Schrock@Sun.COM 
31718525SEric.Schrock@Sun.COM 		if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
31728525SEric.Schrock@Sun.COM 			return (ENOSPC);
31738525SEric.Schrock@Sun.COM 		if (ds->ds_quota > 0 &&
317411022STom.Erickson@Sun.COM 		    effective_value > ds->ds_quota)
31758525SEric.Schrock@Sun.COM 			return (ENOSPC);
31768525SEric.Schrock@Sun.COM 	}
31775378Sck153898 
31785378Sck153898 	return (0);
31795378Sck153898 }
31805378Sck153898 
31815378Sck153898 static void
318212296SLin.Ling@Sun.COM dsl_dataset_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx)
31835378Sck153898 {
31845378Sck153898 	dsl_dataset_t *ds = arg1;
318511022STom.Erickson@Sun.COM 	dsl_prop_setarg_t *psa = arg2;
318611022STom.Erickson@Sun.COM 	uint64_t effective_value = psa->psa_effective_value;
31877595SMatthew.Ahrens@Sun.COM 	uint64_t unique;
31887595SMatthew.Ahrens@Sun.COM 	int64_t delta;
31897595SMatthew.Ahrens@Sun.COM 
319012296SLin.Ling@Sun.COM 	dsl_prop_set_sync(ds, psa, tx);
319111022STom.Erickson@Sun.COM 	DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
319211022STom.Erickson@Sun.COM 
31937595SMatthew.Ahrens@Sun.COM 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
31947595SMatthew.Ahrens@Sun.COM 
31957595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_dir->dd_lock);
31967595SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_lock);
319712296SLin.Ling@Sun.COM 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
319812296SLin.Ling@Sun.COM 	unique = ds->ds_phys->ds_unique_bytes;
319911022STom.Erickson@Sun.COM 	delta = MAX(0, (int64_t)(effective_value - unique)) -
32007595SMatthew.Ahrens@Sun.COM 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
320111022STom.Erickson@Sun.COM 	ds->ds_reserved = effective_value;
32027595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_lock);
32037595SMatthew.Ahrens@Sun.COM 
32047595SMatthew.Ahrens@Sun.COM 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
32057595SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_dir->dd_lock);
32067595SMatthew.Ahrens@Sun.COM 
320712296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_REFRESERV,
320812296SLin.Ling@Sun.COM 	    ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu",
320911022STom.Erickson@Sun.COM 	    (longlong_t)effective_value, ds->ds_object);
32105378Sck153898 }
32115378Sck153898 
32125378Sck153898 int
321311022STom.Erickson@Sun.COM dsl_dataset_set_reservation(const char *dsname, zprop_source_t source,
321411022STom.Erickson@Sun.COM     uint64_t reservation)
32155378Sck153898 {
32165378Sck153898 	dsl_dataset_t *ds;
321711022STom.Erickson@Sun.COM 	dsl_prop_setarg_t psa;
32185378Sck153898 	int err;
32195378Sck153898 
322011022STom.Erickson@Sun.COM 	dsl_prop_setarg_init_uint64(&psa, "refreservation", source,
322111022STom.Erickson@Sun.COM 	    &reservation);
322211022STom.Erickson@Sun.COM 
32236689Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
32245378Sck153898 	if (err)
32255378Sck153898 		return (err);
32265378Sck153898 
32275378Sck153898 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
32285378Sck153898 	    dsl_dataset_set_reservation_check,
322911022STom.Erickson@Sun.COM 	    dsl_dataset_set_reservation_sync, ds, &psa, 0);
323011022STom.Erickson@Sun.COM 
32316689Smaybee 	dsl_dataset_rele(ds, FTAG);
32325378Sck153898 	return (err);
32335378Sck153898 }
323410242Schris.kirby@sun.com 
323510342Schris.kirby@sun.com struct dsl_ds_holdarg {
323610342Schris.kirby@sun.com 	dsl_sync_task_group_t *dstg;
323710342Schris.kirby@sun.com 	char *htag;
323810342Schris.kirby@sun.com 	char *snapname;
323910342Schris.kirby@sun.com 	boolean_t recursive;
324010342Schris.kirby@sun.com 	boolean_t gotone;
324110342Schris.kirby@sun.com 	boolean_t temphold;
324210342Schris.kirby@sun.com 	char failed[MAXPATHLEN];
324310342Schris.kirby@sun.com };
324410342Schris.kirby@sun.com 
324510951SChris.Kirby@sun.com /*
324610951SChris.Kirby@sun.com  * The max length of a temporary tag prefix is the number of hex digits
324710951SChris.Kirby@sun.com  * required to express UINT64_MAX plus one for the hyphen.
324810951SChris.Kirby@sun.com  */
324910951SChris.Kirby@sun.com #define	MAX_TAG_PREFIX_LEN	17
325010951SChris.Kirby@sun.com 
325110242Schris.kirby@sun.com static int
325210242Schris.kirby@sun.com dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx)
325310242Schris.kirby@sun.com {
325410242Schris.kirby@sun.com 	dsl_dataset_t *ds = arg1;
325510342Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg2;
325610342Schris.kirby@sun.com 	char *htag = ha->htag;
325710242Schris.kirby@sun.com 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
325810242Schris.kirby@sun.com 	int error = 0;
325910242Schris.kirby@sun.com 
326010242Schris.kirby@sun.com 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
326110242Schris.kirby@sun.com 		return (ENOTSUP);
326210242Schris.kirby@sun.com 
326310242Schris.kirby@sun.com 	if (!dsl_dataset_is_snapshot(ds))
326410242Schris.kirby@sun.com 		return (EINVAL);
326510242Schris.kirby@sun.com 
326610242Schris.kirby@sun.com 	/* tags must be unique */
326710242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
326810242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj) {
326910242Schris.kirby@sun.com 		error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag,
327010242Schris.kirby@sun.com 		    8, 1, tx);
327110242Schris.kirby@sun.com 		if (error == 0)
327210242Schris.kirby@sun.com 			error = EEXIST;
327310242Schris.kirby@sun.com 		else if (error == ENOENT)
327410242Schris.kirby@sun.com 			error = 0;
327510242Schris.kirby@sun.com 	}
327610242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
327710242Schris.kirby@sun.com 
327810951SChris.Kirby@sun.com 	if (error == 0 && ha->temphold &&
327910951SChris.Kirby@sun.com 	    strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
328010951SChris.Kirby@sun.com 		error = E2BIG;
328110951SChris.Kirby@sun.com 
328210242Schris.kirby@sun.com 	return (error);
328310242Schris.kirby@sun.com }
328410242Schris.kirby@sun.com 
328510242Schris.kirby@sun.com static void
328612296SLin.Ling@Sun.COM dsl_dataset_user_hold_sync(void *arg1, void *arg2, dmu_tx_t *tx)
328710242Schris.kirby@sun.com {
328810242Schris.kirby@sun.com 	dsl_dataset_t *ds = arg1;
328910342Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg2;
329010342Schris.kirby@sun.com 	char *htag = ha->htag;
329110342Schris.kirby@sun.com 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
329210342Schris.kirby@sun.com 	objset_t *mos = dp->dp_meta_objset;
329310951SChris.Kirby@sun.com 	uint64_t now = gethrestime_sec();
329410242Schris.kirby@sun.com 	uint64_t zapobj;
329510242Schris.kirby@sun.com 
329610242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
329710242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj == 0) {
329810242Schris.kirby@sun.com 		/*
329910242Schris.kirby@sun.com 		 * This is the first user hold for this dataset.  Create
330010242Schris.kirby@sun.com 		 * the userrefs zap object.
330110242Schris.kirby@sun.com 		 */
330210242Schris.kirby@sun.com 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
330310242Schris.kirby@sun.com 		zapobj = ds->ds_phys->ds_userrefs_obj =
330410242Schris.kirby@sun.com 		    zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx);
330510242Schris.kirby@sun.com 	} else {
330610242Schris.kirby@sun.com 		zapobj = ds->ds_phys->ds_userrefs_obj;
330710242Schris.kirby@sun.com 	}
330810242Schris.kirby@sun.com 	ds->ds_userrefs++;
330910242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
331010242Schris.kirby@sun.com 
331110242Schris.kirby@sun.com 	VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx));
331210242Schris.kirby@sun.com 
331310342Schris.kirby@sun.com 	if (ha->temphold) {
331410342Schris.kirby@sun.com 		VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object,
331510342Schris.kirby@sun.com 		    htag, &now, tx));
331610342Schris.kirby@sun.com 	}
331710342Schris.kirby@sun.com 
331812296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_USER_HOLD,
331912296SLin.Ling@Sun.COM 	    dp->dp_spa, tx, "<%s> temp = %d dataset = %llu", htag,
332010342Schris.kirby@sun.com 	    (int)ha->temphold, ds->ds_object);
332110242Schris.kirby@sun.com }
332210242Schris.kirby@sun.com 
332310242Schris.kirby@sun.com static int
332411209SMatthew.Ahrens@Sun.COM dsl_dataset_user_hold_one(const char *dsname, void *arg)
332510242Schris.kirby@sun.com {
332610242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg;
332710242Schris.kirby@sun.com 	dsl_dataset_t *ds;
332810242Schris.kirby@sun.com 	int error;
332910242Schris.kirby@sun.com 	char *name;
333010242Schris.kirby@sun.com 
333110242Schris.kirby@sun.com 	/* alloc a buffer to hold dsname@snapname plus terminating NULL */
333210272SMatthew.Ahrens@Sun.COM 	name = kmem_asprintf("%s@%s", dsname, ha->snapname);
333310242Schris.kirby@sun.com 	error = dsl_dataset_hold(name, ha->dstg, &ds);
333410272SMatthew.Ahrens@Sun.COM 	strfree(name);
333510242Schris.kirby@sun.com 	if (error == 0) {
333610268Schris.kirby@sun.com 		ha->gotone = B_TRUE;
333710242Schris.kirby@sun.com 		dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check,
333810342Schris.kirby@sun.com 		    dsl_dataset_user_hold_sync, ds, ha, 0);
333910242Schris.kirby@sun.com 	} else if (error == ENOENT && ha->recursive) {
334010242Schris.kirby@sun.com 		error = 0;
334110242Schris.kirby@sun.com 	} else {
334211209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
334310242Schris.kirby@sun.com 	}
334410242Schris.kirby@sun.com 	return (error);
334510242Schris.kirby@sun.com }
334610242Schris.kirby@sun.com 
334710242Schris.kirby@sun.com int
334810242Schris.kirby@sun.com dsl_dataset_user_hold(char *dsname, char *snapname, char *htag,
334910342Schris.kirby@sun.com     boolean_t recursive, boolean_t temphold)
335010242Schris.kirby@sun.com {
335110242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha;
335210242Schris.kirby@sun.com 	dsl_sync_task_t *dst;
335310242Schris.kirby@sun.com 	spa_t *spa;
335410242Schris.kirby@sun.com 	int error;
335510242Schris.kirby@sun.com 
335610242Schris.kirby@sun.com 	ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
335710242Schris.kirby@sun.com 
335810242Schris.kirby@sun.com 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
335910242Schris.kirby@sun.com 
336010242Schris.kirby@sun.com 	error = spa_open(dsname, &spa, FTAG);
336110242Schris.kirby@sun.com 	if (error) {
336210242Schris.kirby@sun.com 		kmem_free(ha, sizeof (struct dsl_ds_holdarg));
336310242Schris.kirby@sun.com 		return (error);
336410242Schris.kirby@sun.com 	}
336510242Schris.kirby@sun.com 
336610242Schris.kirby@sun.com 	ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
336710242Schris.kirby@sun.com 	ha->htag = htag;
336810242Schris.kirby@sun.com 	ha->snapname = snapname;
336910242Schris.kirby@sun.com 	ha->recursive = recursive;
337010342Schris.kirby@sun.com 	ha->temphold = temphold;
337110242Schris.kirby@sun.com 	if (recursive) {
337210242Schris.kirby@sun.com 		error = dmu_objset_find(dsname, dsl_dataset_user_hold_one,
337310242Schris.kirby@sun.com 		    ha, DS_FIND_CHILDREN);
337410242Schris.kirby@sun.com 	} else {
337510242Schris.kirby@sun.com 		error = dsl_dataset_user_hold_one(dsname, ha);
337610242Schris.kirby@sun.com 	}
337710242Schris.kirby@sun.com 	if (error == 0)
337810242Schris.kirby@sun.com 		error = dsl_sync_task_group_wait(ha->dstg);
337910242Schris.kirby@sun.com 
338010242Schris.kirby@sun.com 	for (dst = list_head(&ha->dstg->dstg_tasks); dst;
338110242Schris.kirby@sun.com 	    dst = list_next(&ha->dstg->dstg_tasks, dst)) {
338210242Schris.kirby@sun.com 		dsl_dataset_t *ds = dst->dst_arg1;
338310242Schris.kirby@sun.com 
338410242Schris.kirby@sun.com 		if (dst->dst_err) {
338510242Schris.kirby@sun.com 			dsl_dataset_name(ds, ha->failed);
338610242Schris.kirby@sun.com 			*strchr(ha->failed, '@') = '\0';
338710242Schris.kirby@sun.com 		}
338810242Schris.kirby@sun.com 		dsl_dataset_rele(ds, ha->dstg);
338910242Schris.kirby@sun.com 	}
339010242Schris.kirby@sun.com 
339110268Schris.kirby@sun.com 	if (error == 0 && recursive && !ha->gotone)
339210268Schris.kirby@sun.com 		error = ENOENT;
339310268Schris.kirby@sun.com 
339410242Schris.kirby@sun.com 	if (error)
339511209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
339610242Schris.kirby@sun.com 
339710242Schris.kirby@sun.com 	dsl_sync_task_group_destroy(ha->dstg);
339810242Schris.kirby@sun.com 	kmem_free(ha, sizeof (struct dsl_ds_holdarg));
339910242Schris.kirby@sun.com 	spa_close(spa, FTAG);
340010242Schris.kirby@sun.com 	return (error);
340110242Schris.kirby@sun.com }
340210242Schris.kirby@sun.com 
340310242Schris.kirby@sun.com struct dsl_ds_releasearg {
340410242Schris.kirby@sun.com 	dsl_dataset_t *ds;
340510242Schris.kirby@sun.com 	const char *htag;
340610242Schris.kirby@sun.com 	boolean_t own;		/* do we own or just hold ds? */
340710242Schris.kirby@sun.com };
340810242Schris.kirby@sun.com 
340910242Schris.kirby@sun.com static int
341010242Schris.kirby@sun.com dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag,
341110242Schris.kirby@sun.com     boolean_t *might_destroy)
341210242Schris.kirby@sun.com {
341310242Schris.kirby@sun.com 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
341410242Schris.kirby@sun.com 	uint64_t zapobj;
341510242Schris.kirby@sun.com 	uint64_t tmp;
341610242Schris.kirby@sun.com 	int error;
341710242Schris.kirby@sun.com 
341810242Schris.kirby@sun.com 	*might_destroy = B_FALSE;
341910242Schris.kirby@sun.com 
342010242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
342110242Schris.kirby@sun.com 	zapobj = ds->ds_phys->ds_userrefs_obj;
342210242Schris.kirby@sun.com 	if (zapobj == 0) {
342310242Schris.kirby@sun.com 		/* The tag can't possibly exist */
342410242Schris.kirby@sun.com 		mutex_exit(&ds->ds_lock);
342510242Schris.kirby@sun.com 		return (ESRCH);
342610242Schris.kirby@sun.com 	}
342710242Schris.kirby@sun.com 
342810242Schris.kirby@sun.com 	/* Make sure the tag exists */
342910242Schris.kirby@sun.com 	error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp);
343010242Schris.kirby@sun.com 	if (error) {
343110242Schris.kirby@sun.com 		mutex_exit(&ds->ds_lock);
343210242Schris.kirby@sun.com 		if (error == ENOENT)
343310242Schris.kirby@sun.com 			error = ESRCH;
343410242Schris.kirby@sun.com 		return (error);
343510242Schris.kirby@sun.com 	}
343610242Schris.kirby@sun.com 
343710242Schris.kirby@sun.com 	if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 &&
343810242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds))
343910242Schris.kirby@sun.com 		*might_destroy = B_TRUE;
344010242Schris.kirby@sun.com 
344110242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
344210242Schris.kirby@sun.com 	return (0);
344310242Schris.kirby@sun.com }
344410242Schris.kirby@sun.com 
344510242Schris.kirby@sun.com static int
344610242Schris.kirby@sun.com dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx)
344710242Schris.kirby@sun.com {
344810242Schris.kirby@sun.com 	struct dsl_ds_releasearg *ra = arg1;
344910242Schris.kirby@sun.com 	dsl_dataset_t *ds = ra->ds;
345010242Schris.kirby@sun.com 	boolean_t might_destroy;
345110242Schris.kirby@sun.com 	int error;
345210242Schris.kirby@sun.com 
345310242Schris.kirby@sun.com 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
345410242Schris.kirby@sun.com 		return (ENOTSUP);
345510242Schris.kirby@sun.com 
345610242Schris.kirby@sun.com 	error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy);
345710242Schris.kirby@sun.com 	if (error)
345810242Schris.kirby@sun.com 		return (error);
345910242Schris.kirby@sun.com 
346010242Schris.kirby@sun.com 	if (might_destroy) {
346110242Schris.kirby@sun.com 		struct dsl_ds_destroyarg dsda = {0};
346210242Schris.kirby@sun.com 
346310242Schris.kirby@sun.com 		if (dmu_tx_is_syncing(tx)) {
346410242Schris.kirby@sun.com 			/*
346510242Schris.kirby@sun.com 			 * If we're not prepared to remove the snapshot,
346610242Schris.kirby@sun.com 			 * we can't allow the release to happen right now.
346710242Schris.kirby@sun.com 			 */
346810242Schris.kirby@sun.com 			if (!ra->own)
346910242Schris.kirby@sun.com 				return (EBUSY);
347010242Schris.kirby@sun.com 		}
347110242Schris.kirby@sun.com 		dsda.ds = ds;
347210242Schris.kirby@sun.com 		dsda.releasing = B_TRUE;
347310242Schris.kirby@sun.com 		return (dsl_dataset_destroy_check(&dsda, tag, tx));
347410242Schris.kirby@sun.com 	}
347510242Schris.kirby@sun.com 
347610242Schris.kirby@sun.com 	return (0);
347710242Schris.kirby@sun.com }
347810242Schris.kirby@sun.com 
347910242Schris.kirby@sun.com static void
348012296SLin.Ling@Sun.COM dsl_dataset_user_release_sync(void *arg1, void *tag, dmu_tx_t *tx)
348110242Schris.kirby@sun.com {
348210242Schris.kirby@sun.com 	struct dsl_ds_releasearg *ra = arg1;
348310242Schris.kirby@sun.com 	dsl_dataset_t *ds = ra->ds;
348410342Schris.kirby@sun.com 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
348510342Schris.kirby@sun.com 	objset_t *mos = dp->dp_meta_objset;
348610242Schris.kirby@sun.com 	uint64_t zapobj;
348710242Schris.kirby@sun.com 	uint64_t dsobj = ds->ds_object;
348810242Schris.kirby@sun.com 	uint64_t refs;
348910342Schris.kirby@sun.com 	int error;
349010242Schris.kirby@sun.com 
349112296SLin.Ling@Sun.COM 	if (ds->ds_objset) {
349212296SLin.Ling@Sun.COM 		dmu_objset_evict(ds->ds_objset);
349312296SLin.Ling@Sun.COM 		ds->ds_objset = NULL;
349412296SLin.Ling@Sun.COM 	}
349512296SLin.Ling@Sun.COM 
349610242Schris.kirby@sun.com 	mutex_enter(&ds->ds_lock);
349710242Schris.kirby@sun.com 	ds->ds_userrefs--;
349810242Schris.kirby@sun.com 	refs = ds->ds_userrefs;
349910242Schris.kirby@sun.com 	mutex_exit(&ds->ds_lock);
350010342Schris.kirby@sun.com 	error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx);
350110342Schris.kirby@sun.com 	VERIFY(error == 0 || error == ENOENT);
350210242Schris.kirby@sun.com 	zapobj = ds->ds_phys->ds_userrefs_obj;
350310242Schris.kirby@sun.com 	VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx));
350410242Schris.kirby@sun.com 	if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 &&
350510242Schris.kirby@sun.com 	    DS_IS_DEFER_DESTROY(ds)) {
350610242Schris.kirby@sun.com 		struct dsl_ds_destroyarg dsda = {0};
350710242Schris.kirby@sun.com 
350810242Schris.kirby@sun.com 		ASSERT(ra->own);
350910242Schris.kirby@sun.com 		dsda.ds = ds;
351010242Schris.kirby@sun.com 		dsda.releasing = B_TRUE;
351110242Schris.kirby@sun.com 		/* We already did the destroy_check */
351212296SLin.Ling@Sun.COM 		dsl_dataset_destroy_sync(&dsda, tag, tx);
351310242Schris.kirby@sun.com 	}
351410242Schris.kirby@sun.com 
351512296SLin.Ling@Sun.COM 	spa_history_log_internal(LOG_DS_USER_RELEASE,
351612296SLin.Ling@Sun.COM 	    dp->dp_spa, tx, "<%s> %lld dataset = %llu",
351710242Schris.kirby@sun.com 	    ra->htag, (longlong_t)refs, dsobj);
351810242Schris.kirby@sun.com }
351910242Schris.kirby@sun.com 
352010242Schris.kirby@sun.com static int
352111209SMatthew.Ahrens@Sun.COM dsl_dataset_user_release_one(const char *dsname, void *arg)
352210242Schris.kirby@sun.com {
352310242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha = arg;
352410242Schris.kirby@sun.com 	struct dsl_ds_releasearg *ra;
352510242Schris.kirby@sun.com 	dsl_dataset_t *ds;
352610242Schris.kirby@sun.com 	int error;
352710242Schris.kirby@sun.com 	void *dtag = ha->dstg;
352810242Schris.kirby@sun.com 	char *name;
352910242Schris.kirby@sun.com 	boolean_t own = B_FALSE;
353010242Schris.kirby@sun.com 	boolean_t might_destroy;
353110242Schris.kirby@sun.com 
353210242Schris.kirby@sun.com 	/* alloc a buffer to hold dsname@snapname, plus the terminating NULL */
353310272SMatthew.Ahrens@Sun.COM 	name = kmem_asprintf("%s@%s", dsname, ha->snapname);
353410242Schris.kirby@sun.com 	error = dsl_dataset_hold(name, dtag, &ds);
353510272SMatthew.Ahrens@Sun.COM 	strfree(name);
353610242Schris.kirby@sun.com 	if (error == ENOENT && ha->recursive)
353710242Schris.kirby@sun.com 		return (0);
353811209SMatthew.Ahrens@Sun.COM 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
353910242Schris.kirby@sun.com 	if (error)
354010242Schris.kirby@sun.com 		return (error);
354110242Schris.kirby@sun.com 
354210268Schris.kirby@sun.com 	ha->gotone = B_TRUE;
354310268Schris.kirby@sun.com 
354410242Schris.kirby@sun.com 	ASSERT(dsl_dataset_is_snapshot(ds));
354510242Schris.kirby@sun.com 
354610242Schris.kirby@sun.com 	error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy);
354710242Schris.kirby@sun.com 	if (error) {
354810242Schris.kirby@sun.com 		dsl_dataset_rele(ds, dtag);
354910242Schris.kirby@sun.com 		return (error);
355010242Schris.kirby@sun.com 	}
355110242Schris.kirby@sun.com 
355210242Schris.kirby@sun.com 	if (might_destroy) {
355310242Schris.kirby@sun.com #ifdef _KERNEL
355412115SChris.Kirby@oracle.com 		name = kmem_asprintf("%s@%s", dsname, ha->snapname);
355510242Schris.kirby@sun.com 		error = zfs_unmount_snap(name, NULL);
355612115SChris.Kirby@oracle.com 		strfree(name);
355710242Schris.kirby@sun.com 		if (error) {
355810242Schris.kirby@sun.com 			dsl_dataset_rele(ds, dtag);
355910242Schris.kirby@sun.com 			return (error);
356010242Schris.kirby@sun.com 		}
356110242Schris.kirby@sun.com #endif
356210298SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) {
356310242Schris.kirby@sun.com 			dsl_dataset_rele(ds, dtag);
356410242Schris.kirby@sun.com 			return (EBUSY);
356510242Schris.kirby@sun.com 		} else {
356610242Schris.kirby@sun.com 			own = B_TRUE;
356710242Schris.kirby@sun.com 			dsl_dataset_make_exclusive(ds, dtag);
356810242Schris.kirby@sun.com 		}
356910242Schris.kirby@sun.com 	}
357010242Schris.kirby@sun.com 
357110242Schris.kirby@sun.com 	ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP);
357210242Schris.kirby@sun.com 	ra->ds = ds;
357310242Schris.kirby@sun.com 	ra->htag = ha->htag;
357410242Schris.kirby@sun.com 	ra->own = own;
357510242Schris.kirby@sun.com 	dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check,
357610242Schris.kirby@sun.com 	    dsl_dataset_user_release_sync, ra, dtag, 0);
357710242Schris.kirby@sun.com 
357810242Schris.kirby@sun.com 	return (0);
357910242Schris.kirby@sun.com }
358010242Schris.kirby@sun.com 
358110242Schris.kirby@sun.com int
358210242Schris.kirby@sun.com dsl_dataset_user_release(char *dsname, char *snapname, char *htag,
358310242Schris.kirby@sun.com     boolean_t recursive)
358410242Schris.kirby@sun.com {
358510242Schris.kirby@sun.com 	struct dsl_ds_holdarg *ha;
358610242Schris.kirby@sun.com 	dsl_sync_task_t *dst;
358710242Schris.kirby@sun.com 	spa_t *spa;
358810242Schris.kirby@sun.com 	int error;
358910242Schris.kirby@sun.com 
359011546SChris.Kirby@sun.com top:
359110242Schris.kirby@sun.com 	ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
359210242Schris.kirby@sun.com 
359310242Schris.kirby@sun.com 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
359410242Schris.kirby@sun.com 
359510242Schris.kirby@sun.com 	error = spa_open(dsname, &spa, FTAG);
359610242Schris.kirby@sun.com 	if (error) {
359710242Schris.kirby@sun.com 		kmem_free(ha, sizeof (struct dsl_ds_holdarg));
359810242Schris.kirby@sun.com 		return (error);
359910242Schris.kirby@sun.com 	}
360010242Schris.kirby@sun.com 
360110242Schris.kirby@sun.com 	ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
360210242Schris.kirby@sun.com 	ha->htag = htag;
360310242Schris.kirby@sun.com 	ha->snapname = snapname;
360410242Schris.kirby@sun.com 	ha->recursive = recursive;
360510242Schris.kirby@sun.com 	if (recursive) {
360610242Schris.kirby@sun.com 		error = dmu_objset_find(dsname, dsl_dataset_user_release_one,
360710242Schris.kirby@sun.com 		    ha, DS_FIND_CHILDREN);
360810242Schris.kirby@sun.com 	} else {
360910242Schris.kirby@sun.com 		error = dsl_dataset_user_release_one(dsname, ha);
361010242Schris.kirby@sun.com 	}
361110242Schris.kirby@sun.com 	if (error == 0)
361210242Schris.kirby@sun.com 		error = dsl_sync_task_group_wait(ha->dstg);
361310242Schris.kirby@sun.com 
361410242Schris.kirby@sun.com 	for (dst = list_head(&ha->dstg->dstg_tasks); dst;
361510242Schris.kirby@sun.com 	    dst = list_next(&ha->dstg->dstg_tasks, dst)) {
361610242Schris.kirby@sun.com 		struct dsl_ds_releasearg *ra = dst->dst_arg1;
361710242Schris.kirby@sun.com 		dsl_dataset_t *ds = ra->ds;
361810242Schris.kirby@sun.com 
361910242Schris.kirby@sun.com 		if (dst->dst_err)
362010242Schris.kirby@sun.com 			dsl_dataset_name(ds, ha->failed);
362110242Schris.kirby@sun.com 
362210242Schris.kirby@sun.com 		if (ra->own)
362310242Schris.kirby@sun.com 			dsl_dataset_disown(ds, ha->dstg);
362410242Schris.kirby@sun.com 		else
362510242Schris.kirby@sun.com 			dsl_dataset_rele(ds, ha->dstg);
362610242Schris.kirby@sun.com 
362710242Schris.kirby@sun.com 		kmem_free(ra, sizeof (struct dsl_ds_releasearg));
362810242Schris.kirby@sun.com 	}
362910242Schris.kirby@sun.com 
363010268Schris.kirby@sun.com 	if (error == 0 && recursive && !ha->gotone)
363110268Schris.kirby@sun.com 		error = ENOENT;
363210268Schris.kirby@sun.com 
363311546SChris.Kirby@sun.com 	if (error && error != EBUSY)
363411209SMatthew.Ahrens@Sun.COM 		(void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
363510242Schris.kirby@sun.com 
363610242Schris.kirby@sun.com 	dsl_sync_task_group_destroy(ha->dstg);
363710242Schris.kirby@sun.com 	kmem_free(ha, sizeof (struct dsl_ds_holdarg));
363810242Schris.kirby@sun.com 	spa_close(spa, FTAG);
363911546SChris.Kirby@sun.com 
364011546SChris.Kirby@sun.com 	/*
364111546SChris.Kirby@sun.com 	 * We can get EBUSY if we were racing with deferred destroy and
364211546SChris.Kirby@sun.com 	 * dsl_dataset_user_release_check() hadn't done the necessary
364311546SChris.Kirby@sun.com 	 * open context setup.  We can also get EBUSY if we're racing
364411546SChris.Kirby@sun.com 	 * with destroy and that thread is the ds_owner.  Either way
364511546SChris.Kirby@sun.com 	 * the busy condition should be transient, and we should retry
364611546SChris.Kirby@sun.com 	 * the release operation.
364711546SChris.Kirby@sun.com 	 */
364811546SChris.Kirby@sun.com 	if (error == EBUSY)
364911546SChris.Kirby@sun.com 		goto top;
365011546SChris.Kirby@sun.com 
365110242Schris.kirby@sun.com 	return (error);
365210242Schris.kirby@sun.com }
365310242Schris.kirby@sun.com 
365410342Schris.kirby@sun.com /*
365510342Schris.kirby@sun.com  * Called at spa_load time to release a stale temporary user hold.
365610342Schris.kirby@sun.com  */
365710342Schris.kirby@sun.com int
365810342Schris.kirby@sun.com dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag)
365910342Schris.kirby@sun.com {
366010342Schris.kirby@sun.com 	dsl_dataset_t *ds;
366110342Schris.kirby@sun.com 	char *snap;
366210342Schris.kirby@sun.com 	char *name;
366310342Schris.kirby@sun.com 	int namelen;
366410342Schris.kirby@sun.com 	int error;
366510342Schris.kirby@sun.com 
366610342Schris.kirby@sun.com 	rw_enter(&dp->dp_config_rwlock, RW_READER);
366710342Schris.kirby@sun.com 	error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
366810342Schris.kirby@sun.com 	rw_exit(&dp->dp_config_rwlock);
366910342Schris.kirby@sun.com 	if (error)
367010342Schris.kirby@sun.com 		return (error);
367110342Schris.kirby@sun.com 	namelen = dsl_dataset_namelen(ds)+1;
367210342Schris.kirby@sun.com 	name = kmem_alloc(namelen, KM_SLEEP);
367310342Schris.kirby@sun.com 	dsl_dataset_name(ds, name);
367410342Schris.kirby@sun.com 	dsl_dataset_rele(ds, FTAG);
367510342Schris.kirby@sun.com 
367610342Schris.kirby@sun.com 	snap = strchr(name, '@');
367710342Schris.kirby@sun.com 	*snap = '\0';
367810342Schris.kirby@sun.com 	++snap;
367910342Schris.kirby@sun.com 	return (dsl_dataset_user_release(name, snap, htag, B_FALSE));
368010342Schris.kirby@sun.com }
368110342Schris.kirby@sun.com 
368210242Schris.kirby@sun.com int
368310242Schris.kirby@sun.com dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp)
368410242Schris.kirby@sun.com {
368510242Schris.kirby@sun.com 	dsl_dataset_t *ds;
368610242Schris.kirby@sun.com 	int err;
368710242Schris.kirby@sun.com 
368810242Schris.kirby@sun.com 	err = dsl_dataset_hold(dsname, FTAG, &ds);
368910242Schris.kirby@sun.com 	if (err)
369010242Schris.kirby@sun.com 		return (err);
369110242Schris.kirby@sun.com 
369210242Schris.kirby@sun.com 	VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP));
369310242Schris.kirby@sun.com 	if (ds->ds_phys->ds_userrefs_obj != 0) {
369410242Schris.kirby@sun.com 		zap_attribute_t *za;
369510242Schris.kirby@sun.com 		zap_cursor_t zc;
369610242Schris.kirby@sun.com 
369710242Schris.kirby@sun.com 		za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
369810242Schris.kirby@sun.com 		for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset,
369910242Schris.kirby@sun.com 		    ds->ds_phys->ds_userrefs_obj);
370010242Schris.kirby@sun.com 		    zap_cursor_retrieve(&zc, za) == 0;
370110242Schris.kirby@sun.com 		    zap_cursor_advance(&zc)) {
370210242Schris.kirby@sun.com 			VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name,
370310242Schris.kirby@sun.com 			    za->za_first_integer));
370410242Schris.kirby@sun.com 		}
370510242Schris.kirby@sun.com 		zap_cursor_fini(&zc);
370610242Schris.kirby@sun.com 		kmem_free(za, sizeof (zap_attribute_t));
370710242Schris.kirby@sun.com 	}
370810242Schris.kirby@sun.com 	dsl_dataset_rele(ds, FTAG);
370910242Schris.kirby@sun.com 	return (0);
371010242Schris.kirby@sun.com }
371110298SMatthew.Ahrens@Sun.COM 
371210298SMatthew.Ahrens@Sun.COM /*
371310298SMatthew.Ahrens@Sun.COM  * Note, this fuction is used as the callback for dmu_objset_find().  We
371410298SMatthew.Ahrens@Sun.COM  * always return 0 so that we will continue to find and process
371510298SMatthew.Ahrens@Sun.COM  * inconsistent datasets, even if we encounter an error trying to
371610298SMatthew.Ahrens@Sun.COM  * process one of them.
371710298SMatthew.Ahrens@Sun.COM  */
371810298SMatthew.Ahrens@Sun.COM /* ARGSUSED */
371910298SMatthew.Ahrens@Sun.COM int
372011209SMatthew.Ahrens@Sun.COM dsl_destroy_inconsistent(const char *dsname, void *arg)
372110298SMatthew.Ahrens@Sun.COM {
372210298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
372310298SMatthew.Ahrens@Sun.COM 
372410298SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) {
372510298SMatthew.Ahrens@Sun.COM 		if (DS_IS_INCONSISTENT(ds))
372610298SMatthew.Ahrens@Sun.COM 			(void) dsl_dataset_destroy(ds, FTAG, B_FALSE);
372710298SMatthew.Ahrens@Sun.COM 		else
372810298SMatthew.Ahrens@Sun.COM 			dsl_dataset_disown(ds, FTAG);
372910298SMatthew.Ahrens@Sun.COM 	}
373010298SMatthew.Ahrens@Sun.COM 	return (0);
373110298SMatthew.Ahrens@Sun.COM }
3732