xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 5663:029cc4273b57)
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 /*
223547Smaybee  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
284543Smarks #include <sys/cred.h>
29789Sahrens #include <sys/zfs_context.h>
30789Sahrens #include <sys/dmu_objset.h>
31789Sahrens #include <sys/dsl_dir.h>
32789Sahrens #include <sys/dsl_dataset.h>
33789Sahrens #include <sys/dsl_prop.h>
34789Sahrens #include <sys/dsl_pool.h>
352199Sahrens #include <sys/dsl_synctask.h>
364543Smarks #include <sys/dsl_deleg.h>
37789Sahrens #include <sys/dnode.h>
38789Sahrens #include <sys/dbuf.h>
392885Sahrens #include <sys/zvol.h>
40789Sahrens #include <sys/dmu_tx.h>
41789Sahrens #include <sys/zio_checksum.h>
42789Sahrens #include <sys/zap.h>
43789Sahrens #include <sys/zil.h>
44789Sahrens #include <sys/dmu_impl.h>
454543Smarks #include <sys/zfs_ioctl.h>
46789Sahrens 
47789Sahrens spa_t *
48789Sahrens dmu_objset_spa(objset_t *os)
49789Sahrens {
50789Sahrens 	return (os->os->os_spa);
51789Sahrens }
52789Sahrens 
53789Sahrens zilog_t *
54789Sahrens dmu_objset_zil(objset_t *os)
55789Sahrens {
56789Sahrens 	return (os->os->os_zil);
57789Sahrens }
58789Sahrens 
59789Sahrens dsl_pool_t *
60789Sahrens dmu_objset_pool(objset_t *os)
61789Sahrens {
62789Sahrens 	dsl_dataset_t *ds;
63789Sahrens 
64789Sahrens 	if ((ds = os->os->os_dsl_dataset) != NULL && ds->ds_dir)
65789Sahrens 		return (ds->ds_dir->dd_pool);
66789Sahrens 	else
67789Sahrens 		return (spa_get_dsl(os->os->os_spa));
68789Sahrens }
69789Sahrens 
70789Sahrens dsl_dataset_t *
71789Sahrens dmu_objset_ds(objset_t *os)
72789Sahrens {
73789Sahrens 	return (os->os->os_dsl_dataset);
74789Sahrens }
75789Sahrens 
76789Sahrens dmu_objset_type_t
77789Sahrens dmu_objset_type(objset_t *os)
78789Sahrens {
79789Sahrens 	return (os->os->os_phys->os_type);
80789Sahrens }
81789Sahrens 
82789Sahrens void
83789Sahrens dmu_objset_name(objset_t *os, char *buf)
84789Sahrens {
85789Sahrens 	dsl_dataset_name(os->os->os_dsl_dataset, buf);
86789Sahrens }
87789Sahrens 
88789Sahrens uint64_t
89789Sahrens dmu_objset_id(objset_t *os)
90789Sahrens {
91789Sahrens 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
92789Sahrens 
93789Sahrens 	return (ds ? ds->ds_object : 0);
94789Sahrens }
95789Sahrens 
96789Sahrens static void
97789Sahrens checksum_changed_cb(void *arg, uint64_t newval)
98789Sahrens {
99789Sahrens 	objset_impl_t *osi = arg;
100789Sahrens 
101789Sahrens 	/*
102789Sahrens 	 * Inheritance should have been done by now.
103789Sahrens 	 */
104789Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
105789Sahrens 
106789Sahrens 	osi->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
107789Sahrens }
108789Sahrens 
109789Sahrens static void
110789Sahrens compression_changed_cb(void *arg, uint64_t newval)
111789Sahrens {
112789Sahrens 	objset_impl_t *osi = arg;
113789Sahrens 
114789Sahrens 	/*
115789Sahrens 	 * Inheritance and range checking should have been done by now.
116789Sahrens 	 */
117789Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
118789Sahrens 
119789Sahrens 	osi->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
120789Sahrens }
121789Sahrens 
1223835Sahrens static void
1233835Sahrens copies_changed_cb(void *arg, uint64_t newval)
1243835Sahrens {
1253835Sahrens 	objset_impl_t *osi = arg;
1263835Sahrens 
1273835Sahrens 	/*
1283835Sahrens 	 * Inheritance and range checking should have been done by now.
1293835Sahrens 	 */
1303835Sahrens 	ASSERT(newval > 0);
1313835Sahrens 	ASSERT(newval <= spa_max_replication(osi->os_spa));
1323835Sahrens 
1333835Sahrens 	osi->os_copies = newval;
1343835Sahrens }
1353835Sahrens 
136789Sahrens void
137789Sahrens dmu_objset_byteswap(void *buf, size_t size)
138789Sahrens {
139789Sahrens 	objset_phys_t *osp = buf;
140789Sahrens 
141789Sahrens 	ASSERT(size == sizeof (objset_phys_t));
142789Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
143789Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
144789Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
145789Sahrens }
146789Sahrens 
1471544Seschrock int
1481544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
1491544Seschrock     objset_impl_t **osip)
150789Sahrens {
1514787Sahrens 	objset_impl_t *osi;
152789Sahrens 	int i, err, checksum;
153789Sahrens 
1544787Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
1554787Sahrens 
156789Sahrens 	osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP);
157789Sahrens 	osi->os.os = osi;
158789Sahrens 	osi->os_dsl_dataset = ds;
159789Sahrens 	osi->os_spa = spa;
1603547Smaybee 	osi->os_rootbp = bp;
1613547Smaybee 	if (!BP_IS_HOLE(osi->os_rootbp)) {
1622391Smaybee 		uint32_t aflags = ARC_WAIT;
1631544Seschrock 		zbookmark_t zb;
1641544Seschrock 		zb.zb_objset = ds ? ds->ds_object : 0;
1651544Seschrock 		zb.zb_object = 0;
1661544Seschrock 		zb.zb_level = -1;
1671544Seschrock 		zb.zb_blkid = 0;
1681544Seschrock 
1693547Smaybee 		dprintf_bp(osi->os_rootbp, "reading %s", "");
1703547Smaybee 		err = arc_read(NULL, spa, osi->os_rootbp,
171789Sahrens 		    dmu_ot[DMU_OT_OBJSET].ot_byteswap,
1723547Smaybee 		    arc_getbuf_func, &osi->os_phys_buf,
1732391Smaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
1741544Seschrock 		if (err) {
1751544Seschrock 			kmem_free(osi, sizeof (objset_impl_t));
1761544Seschrock 			return (err);
1771544Seschrock 		}
1783547Smaybee 		osi->os_phys = osi->os_phys_buf->b_data;
1795147Srm160521 		if (ds == NULL || dsl_dataset_is_snapshot(ds) == 0)
1805147Srm160521 			arc_release(osi->os_phys_buf, &osi->os_phys_buf);
181789Sahrens 	} else {
1823547Smaybee 		osi->os_phys_buf = arc_buf_alloc(spa, sizeof (objset_phys_t),
1833547Smaybee 		    &osi->os_phys_buf, ARC_BUFC_METADATA);
1843547Smaybee 		osi->os_phys = osi->os_phys_buf->b_data;
185789Sahrens 		bzero(osi->os_phys, sizeof (objset_phys_t));
186789Sahrens 	}
187789Sahrens 
188789Sahrens 	/*
189789Sahrens 	 * Note: the changed_cb will be called once before the register
190789Sahrens 	 * func returns, thus changing the checksum/compression from the
1912082Seschrock 	 * default (fletcher2/off).  Snapshots don't need to know, and
1922082Seschrock 	 * registering would complicate clone promotion.
193789Sahrens 	 */
1942082Seschrock 	if (ds && ds->ds_phys->ds_num_children == 0) {
195789Sahrens 		err = dsl_prop_register(ds, "checksum",
196789Sahrens 		    checksum_changed_cb, osi);
1971544Seschrock 		if (err == 0)
1981544Seschrock 			err = dsl_prop_register(ds, "compression",
1991544Seschrock 			    compression_changed_cb, osi);
2003835Sahrens 		if (err == 0)
2013835Sahrens 			err = dsl_prop_register(ds, "copies",
2023835Sahrens 			    copies_changed_cb, osi);
2031544Seschrock 		if (err) {
2043547Smaybee 			VERIFY(arc_buf_remove_ref(osi->os_phys_buf,
2053547Smaybee 			    &osi->os_phys_buf) == 1);
2061544Seschrock 			kmem_free(osi, sizeof (objset_impl_t));
2071544Seschrock 			return (err);
2081544Seschrock 		}
2092082Seschrock 	} else if (ds == NULL) {
210789Sahrens 		/* It's the meta-objset. */
211789Sahrens 		osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
2121544Seschrock 		osi->os_compress = ZIO_COMPRESS_LZJB;
2133835Sahrens 		osi->os_copies = spa_max_replication(spa);
214789Sahrens 	}
215789Sahrens 
2161544Seschrock 	osi->os_zil = zil_alloc(&osi->os, &osi->os_phys->os_zil_header);
2171544Seschrock 
218789Sahrens 	/*
219789Sahrens 	 * Metadata always gets compressed and checksummed.
220789Sahrens 	 * If the data checksum is multi-bit correctable, and it's not
221789Sahrens 	 * a ZBT-style checksum, then it's suitable for metadata as well.
222789Sahrens 	 * Otherwise, the metadata checksum defaults to fletcher4.
223789Sahrens 	 */
224789Sahrens 	checksum = osi->os_checksum;
225789Sahrens 
226789Sahrens 	if (zio_checksum_table[checksum].ci_correctable &&
227789Sahrens 	    !zio_checksum_table[checksum].ci_zbt)
228789Sahrens 		osi->os_md_checksum = checksum;
229789Sahrens 	else
230789Sahrens 		osi->os_md_checksum = ZIO_CHECKSUM_FLETCHER_4;
2311544Seschrock 	osi->os_md_compress = ZIO_COMPRESS_LZJB;
232789Sahrens 
233789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
234789Sahrens 		list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t),
235789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
236789Sahrens 		list_create(&osi->os_free_dnodes[i], sizeof (dnode_t),
237789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
238789Sahrens 	}
239789Sahrens 	list_create(&osi->os_dnodes, sizeof (dnode_t),
240789Sahrens 	    offsetof(dnode_t, dn_link));
241789Sahrens 	list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
242789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
243789Sahrens 
2442856Snd150628 	mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL);
2452856Snd150628 	mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
2465326Sek110237 	mutex_init(&osi->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
2472856Snd150628 
248789Sahrens 	osi->os_meta_dnode = dnode_special_open(osi,
249789Sahrens 	    &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
250789Sahrens 
2514787Sahrens 	/*
2524787Sahrens 	 * We should be the only thread trying to do this because we
2534787Sahrens 	 * have ds_opening_lock
2544787Sahrens 	 */
2554787Sahrens 	if (ds) {
2564787Sahrens 		VERIFY(NULL == dsl_dataset_set_user_ptr(ds, osi,
2574787Sahrens 		    dmu_objset_evict));
258789Sahrens 	}
259789Sahrens 
2601544Seschrock 	*osip = osi;
2611544Seschrock 	return (0);
262789Sahrens }
263789Sahrens 
2645367Sahrens static int
2655367Sahrens dmu_objset_open_ds_os(dsl_dataset_t *ds, objset_t *os, dmu_objset_type_t type)
2665367Sahrens {
2675367Sahrens 	objset_impl_t *osi;
2685367Sahrens 	int err;
2695367Sahrens 
2705367Sahrens 	mutex_enter(&ds->ds_opening_lock);
2715367Sahrens 	osi = dsl_dataset_get_user_ptr(ds);
2725367Sahrens 	if (osi == NULL) {
2735367Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
2745367Sahrens 		    ds, &ds->ds_phys->ds_bp, &osi);
2755367Sahrens 		if (err)
2765367Sahrens 			return (err);
2775367Sahrens 	}
2785367Sahrens 	mutex_exit(&ds->ds_opening_lock);
2795367Sahrens 
2805367Sahrens 	os->os = osi;
2815367Sahrens 	os->os_mode = DS_MODE_NONE;
2825367Sahrens 
2835367Sahrens 	if (type != DMU_OST_ANY && type != os->os->os_phys->os_type)
2845367Sahrens 		return (EINVAL);
2855367Sahrens 	return (0);
2865367Sahrens }
2875367Sahrens 
2885367Sahrens int
2895367Sahrens dmu_objset_open_ds(dsl_dataset_t *ds, dmu_objset_type_t type, objset_t **osp)
2905367Sahrens {
2915367Sahrens 	objset_t *os;
2925367Sahrens 	int err;
2935367Sahrens 
2945367Sahrens 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
2955367Sahrens 	err = dmu_objset_open_ds_os(ds, os, type);
2965367Sahrens 	if (err)
2975367Sahrens 		kmem_free(os, sizeof (objset_t));
2985367Sahrens 	else
2995367Sahrens 		*osp = os;
3005367Sahrens 	return (err);
3015367Sahrens }
3025367Sahrens 
303789Sahrens /* called from zpl */
304789Sahrens int
305789Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode,
306789Sahrens     objset_t **osp)
307789Sahrens {
3085326Sek110237 	objset_t *os;
309789Sahrens 	dsl_dataset_t *ds;
310789Sahrens 	int err;
311789Sahrens 
3125367Sahrens 	ASSERT(mode != DS_MODE_NONE);
3135367Sahrens 
314789Sahrens 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
315789Sahrens 	err = dsl_dataset_open(name, mode, os, &ds);
316789Sahrens 	if (err) {
317789Sahrens 		kmem_free(os, sizeof (objset_t));
318789Sahrens 		return (err);
319789Sahrens 	}
320789Sahrens 
3215367Sahrens 	err = dmu_objset_open_ds_os(ds, os, type);
3225367Sahrens 	os->os_mode = mode;
3235367Sahrens 	if (err) {
3245367Sahrens 		kmem_free(os, sizeof (objset_t));
3255367Sahrens 		dsl_dataset_close(ds, mode, os);
3265367Sahrens 	} else {
3275367Sahrens 		*osp = os;
328789Sahrens 	}
3295367Sahrens 	return (err);
330789Sahrens }
331789Sahrens 
332789Sahrens void
333789Sahrens dmu_objset_close(objset_t *os)
334789Sahrens {
3355367Sahrens 	if (os->os_mode != DS_MODE_NONE)
3365367Sahrens 		dsl_dataset_close(os->os->os_dsl_dataset, os->os_mode, os);
337789Sahrens 	kmem_free(os, sizeof (objset_t));
338789Sahrens }
339789Sahrens 
3401646Sperrin int
3414944Smaybee dmu_objset_evict_dbufs(objset_t *os)
3421544Seschrock {
3431544Seschrock 	objset_impl_t *osi = os->os;
3441544Seschrock 	dnode_t *dn;
3451596Sahrens 
3461596Sahrens 	mutex_enter(&osi->os_lock);
3471596Sahrens 
3481596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
3491596Sahrens 	list_remove(&osi->os_dnodes, osi->os_meta_dnode);
3501596Sahrens 	list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode);
3511544Seschrock 
3521544Seschrock 	/*
3531596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
3541596Sahrens 	 * because dnode_add_ref() only works if you already have a
3551596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
3561596Sahrens 	 * skip.
3571544Seschrock 	 */
3581596Sahrens 	for (dn = list_head(&osi->os_dnodes);
3594944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
3601596Sahrens 	    dn = list_next(&osi->os_dnodes, dn))
3611596Sahrens 		continue;
3621596Sahrens 
3631596Sahrens 	while (dn) {
3641596Sahrens 		dnode_t *next_dn = dn;
3651596Sahrens 
3661596Sahrens 		do {
3671596Sahrens 			next_dn = list_next(&osi->os_dnodes, next_dn);
3684944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
3691596Sahrens 
3701596Sahrens 		mutex_exit(&osi->os_lock);
3714944Smaybee 		dnode_evict_dbufs(dn);
3721596Sahrens 		dnode_rele(dn, FTAG);
3731596Sahrens 		mutex_enter(&osi->os_lock);
3741596Sahrens 		dn = next_dn;
3751544Seschrock 	}
3761544Seschrock 	mutex_exit(&osi->os_lock);
3774944Smaybee 	return (list_head(&osi->os_dnodes) != osi->os_meta_dnode);
3781544Seschrock }
3791544Seschrock 
3801544Seschrock void
381789Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg)
382789Sahrens {
383789Sahrens 	objset_impl_t *osi = arg;
3841544Seschrock 	objset_t os;
3852082Seschrock 	int i;
386789Sahrens 
387789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
388789Sahrens 		ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL);
389789Sahrens 		ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL);
390789Sahrens 	}
391789Sahrens 
3922082Seschrock 	if (ds && ds->ds_phys->ds_num_children == 0) {
3932082Seschrock 		VERIFY(0 == dsl_prop_unregister(ds, "checksum",
3942082Seschrock 		    checksum_changed_cb, osi));
3952082Seschrock 		VERIFY(0 == dsl_prop_unregister(ds, "compression",
3962082Seschrock 		    compression_changed_cb, osi));
3973835Sahrens 		VERIFY(0 == dsl_prop_unregister(ds, "copies",
3983835Sahrens 		    copies_changed_cb, osi));
399789Sahrens 	}
400789Sahrens 
4011544Seschrock 	/*
4021544Seschrock 	 * We should need only a single pass over the dnode list, since
4031544Seschrock 	 * nothing can be added to the list at this point.
4041544Seschrock 	 */
4051544Seschrock 	os.os = osi;
4064944Smaybee 	(void) dmu_objset_evict_dbufs(&os);
4071544Seschrock 
408789Sahrens 	ASSERT3P(list_head(&osi->os_dnodes), ==, osi->os_meta_dnode);
409789Sahrens 	ASSERT3P(list_tail(&osi->os_dnodes), ==, osi->os_meta_dnode);
410789Sahrens 	ASSERT3P(list_head(&osi->os_meta_dnode->dn_dbufs), ==, NULL);
411789Sahrens 
412789Sahrens 	dnode_special_close(osi->os_meta_dnode);
413789Sahrens 	zil_free(osi->os_zil);
414789Sahrens 
4153547Smaybee 	VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1);
4162856Snd150628 	mutex_destroy(&osi->os_lock);
4172856Snd150628 	mutex_destroy(&osi->os_obj_lock);
4185326Sek110237 	mutex_destroy(&osi->os_user_ptr_lock);
419789Sahrens 	kmem_free(osi, sizeof (objset_impl_t));
420789Sahrens }
421789Sahrens 
422789Sahrens /* called from dsl for meta-objset */
423789Sahrens objset_impl_t *
4243547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
4253547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
426789Sahrens {
427789Sahrens 	objset_impl_t *osi;
428789Sahrens 	dnode_t *mdn;
429789Sahrens 
430789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
4314787Sahrens 	if (ds)
4324787Sahrens 		mutex_enter(&ds->ds_opening_lock);
4333547Smaybee 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi));
4344787Sahrens 	if (ds)
4354787Sahrens 		mutex_exit(&ds->ds_opening_lock);
436789Sahrens 	mdn = osi->os_meta_dnode;
437789Sahrens 
438789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
439789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
440789Sahrens 
441789Sahrens 	/*
442789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
443789Sahrens 	 * later, because then we could do it in quescing context while
444789Sahrens 	 * we are also accessing it in open context.
445789Sahrens 	 *
446789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
447789Sahrens 	 * because the MOS is only updated in syncing context.
448789Sahrens 	 * This is most fortunate: the MOS is the only objset that
449789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
450789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
451789Sahrens 	 */
4521544Seschrock 	if (ds != NULL) {
4531544Seschrock 		int levels = 1;
4541544Seschrock 
4551544Seschrock 		/*
4561544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
4571544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
4581544Seschrock 		 */
4591544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
4601544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
4611544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
4621544Seschrock 			levels++;
4631544Seschrock 
464789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
4651544Seschrock 		    mdn->dn_nlevels = levels;
4661544Seschrock 	}
467789Sahrens 
468789Sahrens 	ASSERT(type != DMU_OST_NONE);
469789Sahrens 	ASSERT(type != DMU_OST_ANY);
470789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
471789Sahrens 	osi->os_phys->os_type = type;
472789Sahrens 
473789Sahrens 	dsl_dataset_dirty(ds, tx);
474789Sahrens 
475789Sahrens 	return (osi);
476789Sahrens }
477789Sahrens 
478789Sahrens struct oscarg {
4794543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
480789Sahrens 	void *userarg;
481789Sahrens 	dsl_dataset_t *clone_parent;
482789Sahrens 	const char *lastname;
483789Sahrens 	dmu_objset_type_t type;
484789Sahrens };
485789Sahrens 
4864543Smarks /*ARGSUSED*/
487789Sahrens static int
4882199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
489789Sahrens {
4902199Sahrens 	dsl_dir_t *dd = arg1;
4912199Sahrens 	struct oscarg *oa = arg2;
4922199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
4932199Sahrens 	int err;
4942199Sahrens 	uint64_t ddobj;
4952199Sahrens 
4962199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
4972199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
4982199Sahrens 	if (err != ENOENT)
4992199Sahrens 		return (err ? err : EEXIST);
5002199Sahrens 
5012199Sahrens 	if (oa->clone_parent != NULL) {
5022199Sahrens 		/*
5032199Sahrens 		 * You can't clone across pools.
5042199Sahrens 		 */
5052199Sahrens 		if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool)
5062199Sahrens 			return (EXDEV);
5072199Sahrens 
5082199Sahrens 		/*
5092199Sahrens 		 * You can only clone snapshots, not the head datasets.
5102199Sahrens 		 */
5112199Sahrens 		if (oa->clone_parent->ds_phys->ds_num_children == 0)
5122199Sahrens 			return (EINVAL);
5132199Sahrens 	}
5144543Smarks 
5152199Sahrens 	return (0);
5162199Sahrens }
5172199Sahrens 
5182199Sahrens static void
5194543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
5202199Sahrens {
5212199Sahrens 	dsl_dir_t *dd = arg1;
5222199Sahrens 	struct oscarg *oa = arg2;
523789Sahrens 	dsl_dataset_t *ds;
5243547Smaybee 	blkptr_t *bp;
5252199Sahrens 	uint64_t dsobj;
526789Sahrens 
527789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
528789Sahrens 
5292199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
5305367Sahrens 	    oa->clone_parent, cr, tx);
531789Sahrens 
5322199Sahrens 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, dsobj, NULL,
5331544Seschrock 	    DS_MODE_STANDARD | DS_MODE_READONLY, FTAG, &ds));
5343547Smaybee 	bp = dsl_dataset_get_blkptr(ds);
5353547Smaybee 	if (BP_IS_HOLE(bp)) {
536789Sahrens 		objset_impl_t *osi;
537789Sahrens 
538789Sahrens 		/* This is an empty dmu_objset; not a clone. */
539789Sahrens 		osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
5403547Smaybee 		    ds, bp, oa->type, tx);
541789Sahrens 
542789Sahrens 		if (oa->userfunc)
5434543Smarks 			oa->userfunc(&osi->os, oa->userarg, cr, tx);
544789Sahrens 	}
5454543Smarks 
5464543Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
5474543Smarks 	    tx, cr, "dataset = %llu", dsobj);
5484543Smarks 
549789Sahrens 	dsl_dataset_close(ds, DS_MODE_STANDARD | DS_MODE_READONLY, FTAG);
550789Sahrens }
551789Sahrens 
552789Sahrens int
553789Sahrens dmu_objset_create(const char *name, dmu_objset_type_t type,
554789Sahrens     objset_t *clone_parent,
5554543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
556789Sahrens {
5572199Sahrens 	dsl_dir_t *pdd;
558789Sahrens 	const char *tail;
559789Sahrens 	int err = 0;
5602199Sahrens 	struct oscarg oa = { 0 };
561789Sahrens 
5622199Sahrens 	ASSERT(strchr(name, '@') == NULL);
5632199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
5641544Seschrock 	if (err)
5651544Seschrock 		return (err);
566789Sahrens 	if (tail == NULL) {
5672199Sahrens 		dsl_dir_close(pdd, FTAG);
568789Sahrens 		return (EEXIST);
569789Sahrens 	}
570789Sahrens 
571789Sahrens 	dprintf("name=%s\n", name);
572789Sahrens 
5732199Sahrens 	oa.userfunc = func;
5742199Sahrens 	oa.userarg = arg;
5752199Sahrens 	oa.lastname = tail;
5762199Sahrens 	oa.type = type;
5774543Smarks 
5782199Sahrens 	if (clone_parent != NULL) {
579789Sahrens 		/*
5802199Sahrens 		 * You can't clone to a different type.
581789Sahrens 		 */
5822199Sahrens 		if (clone_parent->os->os_phys->os_type != type) {
5832199Sahrens 			dsl_dir_close(pdd, FTAG);
5842199Sahrens 			return (EINVAL);
585789Sahrens 		}
5862199Sahrens 		oa.clone_parent = clone_parent->os->os_dsl_dataset;
587789Sahrens 	}
5882199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
5892199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
5902199Sahrens 	dsl_dir_close(pdd, FTAG);
591789Sahrens 	return (err);
592789Sahrens }
593789Sahrens 
594789Sahrens int
595789Sahrens dmu_objset_destroy(const char *name)
596789Sahrens {
597789Sahrens 	objset_t *os;
598789Sahrens 	int error;
599789Sahrens 
600789Sahrens 	/*
601789Sahrens 	 * If it looks like we'll be able to destroy it, and there's
602789Sahrens 	 * an unplayed replay log sitting around, destroy the log.
603789Sahrens 	 * It would be nicer to do this in dsl_dataset_destroy_sync(),
604789Sahrens 	 * but the replay log objset is modified in open context.
605789Sahrens 	 */
6065367Sahrens 	error = dmu_objset_open(name, DMU_OST_ANY,
6075367Sahrens 	    DS_MODE_EXCLUSIVE|DS_MODE_READONLY, &os);
608789Sahrens 	if (error == 0) {
6095367Sahrens 		dsl_dataset_t *ds = os->os->os_dsl_dataset;
6101807Sbonwick 		zil_destroy(dmu_objset_zil(os), B_FALSE);
6115367Sahrens 
6125367Sahrens 		/*
6135367Sahrens 		 * dsl_dataset_destroy() closes the ds.
6145367Sahrens 		 * os is just used as the tag after it's freed.
6155367Sahrens 		 */
6165367Sahrens 		kmem_free(os, sizeof (objset_t));
6175367Sahrens 		error = dsl_dataset_destroy(ds, os);
618789Sahrens 	}
619789Sahrens 
6205367Sahrens 	return (error);
621789Sahrens }
622789Sahrens 
6235446Sahrens /*
6245446Sahrens  * This will close the objset.
6255446Sahrens  */
626789Sahrens int
6275446Sahrens dmu_objset_rollback(objset_t *os)
628789Sahrens {
629789Sahrens 	int err;
6305367Sahrens 	dsl_dataset_t *ds;
631789Sahrens 
6325446Sahrens 	ds = os->os->os_dsl_dataset;
6334935Sperrin 
6345446Sahrens 	if (!dsl_dataset_tryupgrade(ds, DS_MODE_STANDARD, DS_MODE_EXCLUSIVE)) {
6355446Sahrens 		dmu_objset_close(os);
6365446Sahrens 		return (EBUSY);
6375446Sahrens 	}
6385446Sahrens 
6395367Sahrens 	err = dsl_dataset_rollback(ds, os->os->os_phys->os_type);
6404935Sperrin 
6415367Sahrens 	/*
6425367Sahrens 	 * NB: we close the objset manually because the rollback
6435367Sahrens 	 * actually implicitly called dmu_objset_evict(), thus freeing
6445367Sahrens 	 * the objset_impl_t.
6455367Sahrens 	 */
6465367Sahrens 	dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, os);
6475367Sahrens 	kmem_free(os, sizeof (objset_t));
648789Sahrens 	return (err);
649789Sahrens }
650789Sahrens 
6512199Sahrens struct snaparg {
6522199Sahrens 	dsl_sync_task_group_t *dstg;
6532199Sahrens 	char *snapname;
6542199Sahrens 	char failed[MAXPATHLEN];
6554543Smarks 	boolean_t checkperms;
6565367Sahrens 	list_t objsets;
6575367Sahrens };
6585367Sahrens 
6595367Sahrens struct osnode {
6605367Sahrens 	list_node_t node;
6615367Sahrens 	objset_t *os;
6622199Sahrens };
6632199Sahrens 
6642199Sahrens static int
6652199Sahrens dmu_objset_snapshot_one(char *name, void *arg)
6662199Sahrens {
6672199Sahrens 	struct snaparg *sn = arg;
6682199Sahrens 	objset_t *os;
6693637Srm160521 	dmu_objset_stats_t stat;
6702199Sahrens 	int err;
6712199Sahrens 
6722199Sahrens 	(void) strcpy(sn->failed, name);
6732199Sahrens 
6744543Smarks 	/*
6754543Smarks 	 * Check permissions only when requested.  This only applies when
6764543Smarks 	 * doing a recursive snapshot.  The permission checks for the starting
6774543Smarks 	 * dataset have already been performed in zfs_secpolicy_snapshot()
6784543Smarks 	 */
6794543Smarks 	if (sn->checkperms == B_TRUE &&
6804543Smarks 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
6814543Smarks 		return (err);
6824543Smarks 
6832199Sahrens 	err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_STANDARD, &os);
6842199Sahrens 	if (err != 0)
6852199Sahrens 		return (err);
6862199Sahrens 
6872199Sahrens 	/*
6883637Srm160521 	 * If the objset is in an inconsistent state, return busy.
6893637Srm160521 	 */
6903637Srm160521 	dmu_objset_fast_stat(os, &stat);
6913637Srm160521 	if (stat.dds_inconsistent) {
6923637Srm160521 		dmu_objset_close(os);
6933637Srm160521 		return (EBUSY);
6943637Srm160521 	}
6953637Srm160521 
6963637Srm160521 	/*
6972199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
6982199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
6992199Sahrens 	 * a side effect.
7002199Sahrens 	 */
7012199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
7022199Sahrens 	if (err == 0) {
7035367Sahrens 		struct osnode *osn;
7042199Sahrens 		dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check,
7055367Sahrens 		    dsl_dataset_snapshot_sync, os->os->os_dsl_dataset,
7065367Sahrens 		    sn->snapname, 3);
7075367Sahrens 		osn = kmem_alloc(sizeof (struct osnode), KM_SLEEP);
7085367Sahrens 		osn->os = os;
7095367Sahrens 		list_insert_tail(&sn->objsets, osn);
7103637Srm160521 	} else {
7113637Srm160521 		dmu_objset_close(os);
7122199Sahrens 	}
7133637Srm160521 
7142199Sahrens 	return (err);
7152199Sahrens }
7162199Sahrens 
7172199Sahrens int
7182199Sahrens dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive)
7192199Sahrens {
7202199Sahrens 	dsl_sync_task_t *dst;
7215367Sahrens 	struct osnode *osn;
7222199Sahrens 	struct snaparg sn = { 0 };
7232199Sahrens 	spa_t *spa;
7242199Sahrens 	int err;
7252199Sahrens 
7262199Sahrens 	(void) strcpy(sn.failed, fsname);
7272199Sahrens 
7284603Sahrens 	err = spa_open(fsname, &spa, FTAG);
7292199Sahrens 	if (err)
7302199Sahrens 		return (err);
7312199Sahrens 
7322199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
7332199Sahrens 	sn.snapname = snapname;
7345367Sahrens 	list_create(&sn.objsets, sizeof (struct osnode),
7355367Sahrens 	    offsetof(struct osnode, node));
7362199Sahrens 
7372417Sahrens 	if (recursive) {
7384543Smarks 		sn.checkperms = B_TRUE;
7392417Sahrens 		err = dmu_objset_find(fsname,
7402417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
7412417Sahrens 	} else {
7424543Smarks 		sn.checkperms = B_FALSE;
7432199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
7442417Sahrens 	}
7452199Sahrens 
7462199Sahrens 	if (err)
7472199Sahrens 		goto out;
7482199Sahrens 
7492199Sahrens 	err = dsl_sync_task_group_wait(sn.dstg);
7502199Sahrens 
7512199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
7522199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
7535367Sahrens 		dsl_dataset_t *ds = dst->dst_arg1;
7542199Sahrens 		if (dst->dst_err)
7555367Sahrens 			dsl_dataset_name(ds, sn.failed);
7562199Sahrens 	}
7575367Sahrens 
7585367Sahrens 	while (osn = list_head(&sn.objsets)) {
7595367Sahrens 		list_remove(&sn.objsets, osn);
7605367Sahrens 		zil_resume(dmu_objset_zil(osn->os));
7615367Sahrens 		dmu_objset_close(osn->os);
7625367Sahrens 		kmem_free(osn, sizeof (struct osnode));
7635367Sahrens 	}
7645367Sahrens 	list_destroy(&sn.objsets);
7652199Sahrens out:
7662199Sahrens 	if (err)
7672199Sahrens 		(void) strcpy(fsname, sn.failed);
7682199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
7692199Sahrens 	spa_close(spa, FTAG);
7702199Sahrens 	return (err);
7712199Sahrens }
7722199Sahrens 
773789Sahrens static void
7743547Smaybee dmu_objset_sync_dnodes(list_t *list, dmu_tx_t *tx)
775789Sahrens {
7763547Smaybee 	dnode_t *dn;
777789Sahrens 
7783547Smaybee 	while (dn = list_head(list)) {
7793547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
7803547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
7813547Smaybee 		/*
7823547Smaybee 		 * Initialize dn_zio outside dnode_sync()
7833547Smaybee 		 * to accomodate meta-dnode
7843547Smaybee 		 */
7853547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
7863547Smaybee 		ASSERT(dn->dn_zio);
787789Sahrens 
7883547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
7893547Smaybee 		list_remove(list, dn);
7903547Smaybee 		dnode_sync(dn, tx);
7913547Smaybee 	}
7923547Smaybee }
7932981Sahrens 
7943547Smaybee /* ARGSUSED */
7953547Smaybee static void
7963547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg)
7973547Smaybee {
7983547Smaybee 	objset_impl_t *os = arg;
7993547Smaybee 	blkptr_t *bp = os->os_rootbp;
8003547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
8013547Smaybee 	int i;
8022981Sahrens 
8035329Sgw25295 	ASSERT(bp == zio->io_bp);
8045329Sgw25295 
8053547Smaybee 	/*
8063547Smaybee 	 * Update rootbp fill count.
8073547Smaybee 	 */
8083547Smaybee 	bp->blk_fill = 1;	/* count the meta-dnode */
8093547Smaybee 	for (i = 0; i < dnp->dn_nblkptr; i++)
8103547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
8115329Sgw25295 
8125329Sgw25295 	BP_SET_TYPE(bp, DMU_OT_OBJSET);
8135329Sgw25295 	BP_SET_LEVEL(bp, 0);
8145329Sgw25295 
8155329Sgw25295 	/* We must do this after we've set the bp's type and level */
8165329Sgw25295 	if (!DVA_EQUAL(BP_IDENTITY(bp),
8175329Sgw25295 	    BP_IDENTITY(&zio->io_bp_orig))) {
8185329Sgw25295 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
8195329Sgw25295 			dsl_dataset_block_kill(os->os_dsl_dataset,
8205329Sgw25295 			    &zio->io_bp_orig, NULL, os->os_synctx);
8215329Sgw25295 		dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx);
8225329Sgw25295 	}
823789Sahrens }
824789Sahrens 
825789Sahrens /* ARGSUSED */
826789Sahrens static void
827789Sahrens killer(zio_t *zio, arc_buf_t *abuf, void *arg)
828789Sahrens {
829789Sahrens 	objset_impl_t *os = arg;
830789Sahrens 
831789Sahrens 	ASSERT3U(zio->io_error, ==, 0);
8323547Smaybee 	arc_release(os->os_phys_buf, &os->os_phys_buf);
833789Sahrens }
834789Sahrens 
835789Sahrens /* called from dsl */
836789Sahrens void
8373547Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx)
838789Sahrens {
839789Sahrens 	int txgoff;
8401544Seschrock 	zbookmark_t zb;
8413547Smaybee 	zio_t *zio;
8423547Smaybee 	list_t *list;
8433547Smaybee 	dbuf_dirty_record_t *dr;
8443547Smaybee 
8453547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
846789Sahrens 
847789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
848789Sahrens 	/* XXX the write_done callback should really give us the tx... */
849789Sahrens 	os->os_synctx = tx;
850789Sahrens 
8513882Sahrens 	if (os->os_dsl_dataset == NULL) {
8523882Sahrens 		/*
8533882Sahrens 		 * This is the MOS.  If we have upgraded,
8543882Sahrens 		 * spa_max_replication() could change, so reset
8553882Sahrens 		 * os_copies here.
8563882Sahrens 		 */
8573882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
8583882Sahrens 	}
8593882Sahrens 
8603547Smaybee 	/*
8613547Smaybee 	 * Create the root block IO
8623547Smaybee 	 */
8633547Smaybee 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
8643547Smaybee 	zb.zb_object = 0;
8653547Smaybee 	zb.zb_level = -1;
8663547Smaybee 	zb.zb_blkid = 0;
8674787Sahrens 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
8683547Smaybee 		dsl_dataset_block_kill(os->os_dsl_dataset,
8693547Smaybee 		    os->os_rootbp, pio, tx);
8704787Sahrens 	}
8713547Smaybee 	zio = arc_write(pio, os->os_spa, os->os_md_checksum,
8723547Smaybee 	    os->os_md_compress,
8733835Sahrens 	    dmu_get_replication_level(os, &zb, DMU_OT_OBJSET),
8743547Smaybee 	    tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, killer, os,
8754634Sek110237 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_METADATA,
8764634Sek110237 	    &zb);
8773547Smaybee 
8783547Smaybee 	/*
8793547Smaybee 	 * Sync meta-dnode - the parent IO for the sync is the root block
8803547Smaybee 	 */
8813547Smaybee 	os->os_meta_dnode->dn_zio = zio;
8823547Smaybee 	dnode_sync(os->os_meta_dnode, tx);
883789Sahrens 
884789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
885789Sahrens 
8863547Smaybee 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], tx);
8873547Smaybee 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], tx);
888789Sahrens 
8893547Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
8903547Smaybee 	while (dr = list_head(list)) {
8913547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
8923547Smaybee 		list_remove(list, dr);
8933547Smaybee 		if (dr->dr_zio)
8943547Smaybee 			zio_nowait(dr->dr_zio);
8953547Smaybee 	}
896789Sahrens 	/*
897789Sahrens 	 * Free intent log blocks up to this tx.
898789Sahrens 	 */
899789Sahrens 	zil_sync(os->os_zil, tx);
9003547Smaybee 	zio_nowait(zio);
901789Sahrens }
902789Sahrens 
903789Sahrens void
9042885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
9052885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
9062885Sahrens {
9072885Sahrens 	dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp,
9082885Sahrens 	    usedobjsp, availobjsp);
9092885Sahrens }
9102885Sahrens 
9112885Sahrens uint64_t
9122885Sahrens dmu_objset_fsid_guid(objset_t *os)
9132885Sahrens {
9142885Sahrens 	return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset));
9152885Sahrens }
9162885Sahrens 
9172885Sahrens void
9182885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
919789Sahrens {
9202885Sahrens 	stat->dds_type = os->os->os_phys->os_type;
9212885Sahrens 	if (os->os->os_dsl_dataset)
9222885Sahrens 		dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat);
9232885Sahrens }
9242885Sahrens 
9252885Sahrens void
9262885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
9272885Sahrens {
9282885Sahrens 	ASSERT(os->os->os_dsl_dataset ||
9292885Sahrens 	    os->os->os_phys->os_type == DMU_OST_META);
9302885Sahrens 
9312885Sahrens 	if (os->os->os_dsl_dataset != NULL)
9322885Sahrens 		dsl_dataset_stats(os->os->os_dsl_dataset, nv);
9332885Sahrens 
9342885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
9352885Sahrens 	    os->os->os_phys->os_type);
936789Sahrens }
937789Sahrens 
938789Sahrens int
939789Sahrens dmu_objset_is_snapshot(objset_t *os)
940789Sahrens {
941789Sahrens 	if (os->os->os_dsl_dataset != NULL)
942789Sahrens 		return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset));
943789Sahrens 	else
944789Sahrens 		return (B_FALSE);
945789Sahrens }
946789Sahrens 
947789Sahrens int
948789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
949*5663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
950789Sahrens {
951789Sahrens 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
952789Sahrens 	zap_cursor_t cursor;
953789Sahrens 	zap_attribute_t attr;
954789Sahrens 
955789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
956789Sahrens 		return (ENOENT);
957789Sahrens 
958789Sahrens 	zap_cursor_init_serialized(&cursor,
959789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
960789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
961789Sahrens 
962885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
963885Sahrens 		zap_cursor_fini(&cursor);
964885Sahrens 		return (ENOENT);
965885Sahrens 	}
966885Sahrens 
967885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
968885Sahrens 		zap_cursor_fini(&cursor);
969885Sahrens 		return (ENAMETOOLONG);
970885Sahrens 	}
971885Sahrens 
972885Sahrens 	(void) strcpy(name, attr.za_name);
973885Sahrens 	if (idp)
974885Sahrens 		*idp = attr.za_first_integer;
975*5663Sck153898 	if (case_conflict)
976*5663Sck153898 		*case_conflict = attr.za_normalization_conflict;
977885Sahrens 	zap_cursor_advance(&cursor);
978885Sahrens 	*offp = zap_cursor_serialize(&cursor);
979885Sahrens 	zap_cursor_fini(&cursor);
980885Sahrens 
981885Sahrens 	return (0);
982885Sahrens }
983885Sahrens 
984885Sahrens int
985885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
986885Sahrens     uint64_t *idp, uint64_t *offp)
987885Sahrens {
988885Sahrens 	dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir;
989885Sahrens 	zap_cursor_t cursor;
990885Sahrens 	zap_attribute_t attr;
991885Sahrens 
992885Sahrens 	/* there is no next dir on a snapshot! */
993885Sahrens 	if (os->os->os_dsl_dataset->ds_object !=
994885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
995885Sahrens 		return (ENOENT);
996885Sahrens 
997885Sahrens 	zap_cursor_init_serialized(&cursor,
998885Sahrens 	    dd->dd_pool->dp_meta_objset,
999885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1000885Sahrens 
1001885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1002885Sahrens 		zap_cursor_fini(&cursor);
1003885Sahrens 		return (ENOENT);
1004885Sahrens 	}
1005885Sahrens 
1006885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1007885Sahrens 		zap_cursor_fini(&cursor);
1008789Sahrens 		return (ENAMETOOLONG);
1009885Sahrens 	}
1010789Sahrens 
1011789Sahrens 	(void) strcpy(name, attr.za_name);
1012885Sahrens 	if (idp)
1013885Sahrens 		*idp = attr.za_first_integer;
1014789Sahrens 	zap_cursor_advance(&cursor);
1015789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1016885Sahrens 	zap_cursor_fini(&cursor);
1017789Sahrens 
1018789Sahrens 	return (0);
1019789Sahrens }
1020789Sahrens 
1021789Sahrens /*
1022789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1023789Sahrens  */
10242199Sahrens int
10252199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1026789Sahrens {
1027789Sahrens 	dsl_dir_t *dd;
1028789Sahrens 	objset_t *os;
1029789Sahrens 	uint64_t snapobj;
1030789Sahrens 	zap_cursor_t zc;
10313978Smmusante 	zap_attribute_t *attr;
1032789Sahrens 	char *child;
10331544Seschrock 	int do_self, err;
1034789Sahrens 
10351544Seschrock 	err = dsl_dir_open(name, FTAG, &dd, NULL);
10361544Seschrock 	if (err)
10372199Sahrens 		return (err);
1038789Sahrens 
10392199Sahrens 	/* NB: the $MOS dir doesn't have a head dataset */
1040789Sahrens 	do_self = (dd->dd_phys->dd_head_dataset_obj != 0);
10413978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1042789Sahrens 
1043789Sahrens 	/*
1044789Sahrens 	 * Iterate over all children.
1045789Sahrens 	 */
10462417Sahrens 	if (flags & DS_FIND_CHILDREN) {
10472417Sahrens 		for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset,
10482417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
10493978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
10502417Sahrens 		    (void) zap_cursor_advance(&zc)) {
10513978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
10523978Smmusante 			ASSERT(attr->za_num_integers == 1);
1053789Sahrens 
10542417Sahrens 			/*
10552417Sahrens 			 * No separating '/' because parent's name ends in /.
10562417Sahrens 			 */
10572417Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
10582417Sahrens 			/* XXX could probably just use name here */
10592417Sahrens 			dsl_dir_name(dd, child);
10602417Sahrens 			(void) strcat(child, "/");
10613978Smmusante 			(void) strcat(child, attr->za_name);
10622417Sahrens 			err = dmu_objset_find(child, func, arg, flags);
10632417Sahrens 			kmem_free(child, MAXPATHLEN);
10642417Sahrens 			if (err)
10652417Sahrens 				break;
10662417Sahrens 		}
10672417Sahrens 		zap_cursor_fini(&zc);
10682199Sahrens 
10692417Sahrens 		if (err) {
10702417Sahrens 			dsl_dir_close(dd, FTAG);
10713978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
10722417Sahrens 			return (err);
10732417Sahrens 		}
1074789Sahrens 	}
1075789Sahrens 
1076789Sahrens 	/*
1077789Sahrens 	 * Iterate over all snapshots.
1078789Sahrens 	 */
1079789Sahrens 	if ((flags & DS_FIND_SNAPSHOTS) &&
1080789Sahrens 	    dmu_objset_open(name, DMU_OST_ANY,
1081789Sahrens 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os) == 0) {
1082789Sahrens 
1083789Sahrens 		snapobj = os->os->os_dsl_dataset->ds_phys->ds_snapnames_zapobj;
1084789Sahrens 		dmu_objset_close(os);
1085789Sahrens 
1086789Sahrens 		for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, snapobj);
10873978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
1088789Sahrens 		    (void) zap_cursor_advance(&zc)) {
10893978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
10903978Smmusante 			ASSERT(attr->za_num_integers == 1);
1091789Sahrens 
1092789Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1093789Sahrens 			/* XXX could probably just use name here */
1094789Sahrens 			dsl_dir_name(dd, child);
1095789Sahrens 			(void) strcat(child, "@");
10963978Smmusante 			(void) strcat(child, attr->za_name);
10972199Sahrens 			err = func(child, arg);
1098789Sahrens 			kmem_free(child, MAXPATHLEN);
10992199Sahrens 			if (err)
11002199Sahrens 				break;
1101789Sahrens 		}
1102885Sahrens 		zap_cursor_fini(&zc);
1103789Sahrens 	}
1104789Sahrens 
1105789Sahrens 	dsl_dir_close(dd, FTAG);
11063978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1107789Sahrens 
11082199Sahrens 	if (err)
11092199Sahrens 		return (err);
11102199Sahrens 
1111789Sahrens 	/*
1112789Sahrens 	 * Apply to self if appropriate.
1113789Sahrens 	 */
1114789Sahrens 	if (do_self)
11152199Sahrens 		err = func(name, arg);
11162199Sahrens 	return (err);
1117789Sahrens }
11185326Sek110237 
11195326Sek110237 void
11205326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
11215326Sek110237 {
11225326Sek110237 	ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
11235326Sek110237 	os->os->os_user_ptr = user_ptr;
11245326Sek110237 }
11255326Sek110237 
11265326Sek110237 void *
11275326Sek110237 dmu_objset_get_user(objset_t *os)
11285326Sek110237 {
11295326Sek110237 	ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
11305326Sek110237 	return (os->os->os_user_ptr);
11315326Sek110237 }
1132