xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 5367:c40abbe796be)
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 
264*5367Sahrens static int
265*5367Sahrens dmu_objset_open_ds_os(dsl_dataset_t *ds, objset_t *os, dmu_objset_type_t type)
266*5367Sahrens {
267*5367Sahrens 	objset_impl_t *osi;
268*5367Sahrens 	int err;
269*5367Sahrens 
270*5367Sahrens 	mutex_enter(&ds->ds_opening_lock);
271*5367Sahrens 	osi = dsl_dataset_get_user_ptr(ds);
272*5367Sahrens 	if (osi == NULL) {
273*5367Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
274*5367Sahrens 		    ds, &ds->ds_phys->ds_bp, &osi);
275*5367Sahrens 		if (err)
276*5367Sahrens 			return (err);
277*5367Sahrens 	}
278*5367Sahrens 	mutex_exit(&ds->ds_opening_lock);
279*5367Sahrens 
280*5367Sahrens 	os->os = osi;
281*5367Sahrens 	os->os_mode = DS_MODE_NONE;
282*5367Sahrens 
283*5367Sahrens 	if (type != DMU_OST_ANY && type != os->os->os_phys->os_type)
284*5367Sahrens 		return (EINVAL);
285*5367Sahrens 	return (0);
286*5367Sahrens }
287*5367Sahrens 
288*5367Sahrens int
289*5367Sahrens dmu_objset_open_ds(dsl_dataset_t *ds, dmu_objset_type_t type, objset_t **osp)
290*5367Sahrens {
291*5367Sahrens 	objset_t *os;
292*5367Sahrens 	int err;
293*5367Sahrens 
294*5367Sahrens 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
295*5367Sahrens 	err = dmu_objset_open_ds_os(ds, os, type);
296*5367Sahrens 	if (err)
297*5367Sahrens 		kmem_free(os, sizeof (objset_t));
298*5367Sahrens 	else
299*5367Sahrens 		*osp = os;
300*5367Sahrens 	return (err);
301*5367Sahrens }
302*5367Sahrens 
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 
312*5367Sahrens 	ASSERT(mode != DS_MODE_NONE);
313*5367Sahrens 
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 
321*5367Sahrens 	err = dmu_objset_open_ds_os(ds, os, type);
322*5367Sahrens 	os->os_mode = mode;
323*5367Sahrens 	if (err) {
324*5367Sahrens 		kmem_free(os, sizeof (objset_t));
325*5367Sahrens 		dsl_dataset_close(ds, mode, os);
326*5367Sahrens 	} else {
327*5367Sahrens 		*osp = os;
328789Sahrens 	}
329*5367Sahrens 	return (err);
330789Sahrens }
331789Sahrens 
332789Sahrens void
333789Sahrens dmu_objset_close(objset_t *os)
334789Sahrens {
335*5367Sahrens 	if (os->os_mode != DS_MODE_NONE)
336*5367Sahrens 		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,
530*5367Sahrens 	    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 	 */
606*5367Sahrens 	error = dmu_objset_open(name, DMU_OST_ANY,
607*5367Sahrens 	    DS_MODE_EXCLUSIVE|DS_MODE_READONLY, &os);
608789Sahrens 	if (error == 0) {
609*5367Sahrens 		dsl_dataset_t *ds = os->os->os_dsl_dataset;
6101807Sbonwick 		zil_destroy(dmu_objset_zil(os), B_FALSE);
611*5367Sahrens 
612*5367Sahrens 		/*
613*5367Sahrens 		 * dsl_dataset_destroy() closes the ds.
614*5367Sahrens 		 * os is just used as the tag after it's freed.
615*5367Sahrens 		 */
616*5367Sahrens 		kmem_free(os, sizeof (objset_t));
617*5367Sahrens 		error = dsl_dataset_destroy(ds, os);
618789Sahrens 	}
619789Sahrens 
620*5367Sahrens 	return (error);
621789Sahrens }
622789Sahrens 
623789Sahrens int
624789Sahrens dmu_objset_rollback(const char *name)
625789Sahrens {
626789Sahrens 	int err;
627789Sahrens 	objset_t *os;
628*5367Sahrens 	dsl_dataset_t *ds;
629789Sahrens 
6302199Sahrens 	err = dmu_objset_open(name, DMU_OST_ANY,
6312199Sahrens 	    DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT, &os);
6324935Sperrin 	if (err)
6334935Sperrin 		return (err);
6344935Sperrin 
635*5367Sahrens 	ds = os->os->os_dsl_dataset;
636*5367Sahrens 	err = dsl_dataset_rollback(ds, os->os->os_phys->os_type);
6374935Sperrin 
638*5367Sahrens 	/*
639*5367Sahrens 	 * NB: we close the objset manually because the rollback
640*5367Sahrens 	 * actually implicitly called dmu_objset_evict(), thus freeing
641*5367Sahrens 	 * the objset_impl_t.
642*5367Sahrens 	 */
643*5367Sahrens 	dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, os);
644*5367Sahrens 	kmem_free(os, sizeof (objset_t));
645789Sahrens 	return (err);
646789Sahrens }
647789Sahrens 
6482199Sahrens struct snaparg {
6492199Sahrens 	dsl_sync_task_group_t *dstg;
6502199Sahrens 	char *snapname;
6512199Sahrens 	char failed[MAXPATHLEN];
6524543Smarks 	boolean_t checkperms;
653*5367Sahrens 	list_t objsets;
654*5367Sahrens };
655*5367Sahrens 
656*5367Sahrens struct osnode {
657*5367Sahrens 	list_node_t node;
658*5367Sahrens 	objset_t *os;
6592199Sahrens };
6602199Sahrens 
6612199Sahrens static int
6622199Sahrens dmu_objset_snapshot_one(char *name, void *arg)
6632199Sahrens {
6642199Sahrens 	struct snaparg *sn = arg;
6652199Sahrens 	objset_t *os;
6663637Srm160521 	dmu_objset_stats_t stat;
6672199Sahrens 	int err;
6682199Sahrens 
6692199Sahrens 	(void) strcpy(sn->failed, name);
6702199Sahrens 
6714543Smarks 	/*
6724543Smarks 	 * Check permissions only when requested.  This only applies when
6734543Smarks 	 * doing a recursive snapshot.  The permission checks for the starting
6744543Smarks 	 * dataset have already been performed in zfs_secpolicy_snapshot()
6754543Smarks 	 */
6764543Smarks 	if (sn->checkperms == B_TRUE &&
6774543Smarks 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
6784543Smarks 		return (err);
6794543Smarks 
6802199Sahrens 	err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_STANDARD, &os);
6812199Sahrens 	if (err != 0)
6822199Sahrens 		return (err);
6832199Sahrens 
6842199Sahrens 	/*
6853637Srm160521 	 * If the objset is in an inconsistent state, return busy.
6863637Srm160521 	 */
6873637Srm160521 	dmu_objset_fast_stat(os, &stat);
6883637Srm160521 	if (stat.dds_inconsistent) {
6893637Srm160521 		dmu_objset_close(os);
6903637Srm160521 		return (EBUSY);
6913637Srm160521 	}
6923637Srm160521 
6933637Srm160521 	/*
6942199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
6952199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
6962199Sahrens 	 * a side effect.
6972199Sahrens 	 */
6982199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
6992199Sahrens 	if (err == 0) {
700*5367Sahrens 		struct osnode *osn;
7012199Sahrens 		dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check,
702*5367Sahrens 		    dsl_dataset_snapshot_sync, os->os->os_dsl_dataset,
703*5367Sahrens 		    sn->snapname, 3);
704*5367Sahrens 		osn = kmem_alloc(sizeof (struct osnode), KM_SLEEP);
705*5367Sahrens 		osn->os = os;
706*5367Sahrens 		list_insert_tail(&sn->objsets, osn);
7073637Srm160521 	} else {
7083637Srm160521 		dmu_objset_close(os);
7092199Sahrens 	}
7103637Srm160521 
7112199Sahrens 	return (err);
7122199Sahrens }
7132199Sahrens 
7142199Sahrens int
7152199Sahrens dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive)
7162199Sahrens {
7172199Sahrens 	dsl_sync_task_t *dst;
718*5367Sahrens 	struct osnode *osn;
7192199Sahrens 	struct snaparg sn = { 0 };
7202199Sahrens 	spa_t *spa;
7212199Sahrens 	int err;
7222199Sahrens 
7232199Sahrens 	(void) strcpy(sn.failed, fsname);
7242199Sahrens 
7254603Sahrens 	err = spa_open(fsname, &spa, FTAG);
7262199Sahrens 	if (err)
7272199Sahrens 		return (err);
7282199Sahrens 
7292199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
7302199Sahrens 	sn.snapname = snapname;
731*5367Sahrens 	list_create(&sn.objsets, sizeof (struct osnode),
732*5367Sahrens 	    offsetof(struct osnode, node));
7332199Sahrens 
7342417Sahrens 	if (recursive) {
7354543Smarks 		sn.checkperms = B_TRUE;
7362417Sahrens 		err = dmu_objset_find(fsname,
7372417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
7382417Sahrens 	} else {
7394543Smarks 		sn.checkperms = B_FALSE;
7402199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
7412417Sahrens 	}
7422199Sahrens 
7432199Sahrens 	if (err)
7442199Sahrens 		goto out;
7452199Sahrens 
7462199Sahrens 	err = dsl_sync_task_group_wait(sn.dstg);
7472199Sahrens 
7482199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
7492199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
750*5367Sahrens 		dsl_dataset_t *ds = dst->dst_arg1;
7512199Sahrens 		if (dst->dst_err)
752*5367Sahrens 			dsl_dataset_name(ds, sn.failed);
7532199Sahrens 	}
754*5367Sahrens 
755*5367Sahrens 	while (osn = list_head(&sn.objsets)) {
756*5367Sahrens 		list_remove(&sn.objsets, osn);
757*5367Sahrens 		zil_resume(dmu_objset_zil(osn->os));
758*5367Sahrens 		dmu_objset_close(osn->os);
759*5367Sahrens 		kmem_free(osn, sizeof (struct osnode));
760*5367Sahrens 	}
761*5367Sahrens 	list_destroy(&sn.objsets);
7622199Sahrens out:
7632199Sahrens 	if (err)
7642199Sahrens 		(void) strcpy(fsname, sn.failed);
7652199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
7662199Sahrens 	spa_close(spa, FTAG);
7672199Sahrens 	return (err);
7682199Sahrens }
7692199Sahrens 
770789Sahrens static void
7713547Smaybee dmu_objset_sync_dnodes(list_t *list, dmu_tx_t *tx)
772789Sahrens {
7733547Smaybee 	dnode_t *dn;
774789Sahrens 
7753547Smaybee 	while (dn = list_head(list)) {
7763547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
7773547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
7783547Smaybee 		/*
7793547Smaybee 		 * Initialize dn_zio outside dnode_sync()
7803547Smaybee 		 * to accomodate meta-dnode
7813547Smaybee 		 */
7823547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
7833547Smaybee 		ASSERT(dn->dn_zio);
784789Sahrens 
7853547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
7863547Smaybee 		list_remove(list, dn);
7873547Smaybee 		dnode_sync(dn, tx);
7883547Smaybee 	}
7893547Smaybee }
7902981Sahrens 
7913547Smaybee /* ARGSUSED */
7923547Smaybee static void
7933547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg)
7943547Smaybee {
7953547Smaybee 	objset_impl_t *os = arg;
7963547Smaybee 	blkptr_t *bp = os->os_rootbp;
7973547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
7983547Smaybee 	int i;
7992981Sahrens 
8005329Sgw25295 	ASSERT(bp == zio->io_bp);
8015329Sgw25295 
8023547Smaybee 	/*
8033547Smaybee 	 * Update rootbp fill count.
8043547Smaybee 	 */
8053547Smaybee 	bp->blk_fill = 1;	/* count the meta-dnode */
8063547Smaybee 	for (i = 0; i < dnp->dn_nblkptr; i++)
8073547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
8085329Sgw25295 
8095329Sgw25295 	BP_SET_TYPE(bp, DMU_OT_OBJSET);
8105329Sgw25295 	BP_SET_LEVEL(bp, 0);
8115329Sgw25295 
8125329Sgw25295 	/* We must do this after we've set the bp's type and level */
8135329Sgw25295 	if (!DVA_EQUAL(BP_IDENTITY(bp),
8145329Sgw25295 	    BP_IDENTITY(&zio->io_bp_orig))) {
8155329Sgw25295 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
8165329Sgw25295 			dsl_dataset_block_kill(os->os_dsl_dataset,
8175329Sgw25295 			    &zio->io_bp_orig, NULL, os->os_synctx);
8185329Sgw25295 		dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx);
8195329Sgw25295 	}
820789Sahrens }
821789Sahrens 
822789Sahrens /* ARGSUSED */
823789Sahrens static void
824789Sahrens killer(zio_t *zio, arc_buf_t *abuf, void *arg)
825789Sahrens {
826789Sahrens 	objset_impl_t *os = arg;
827789Sahrens 
828789Sahrens 	ASSERT3U(zio->io_error, ==, 0);
8293547Smaybee 	arc_release(os->os_phys_buf, &os->os_phys_buf);
830789Sahrens }
831789Sahrens 
832789Sahrens /* called from dsl */
833789Sahrens void
8343547Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx)
835789Sahrens {
836789Sahrens 	int txgoff;
8371544Seschrock 	zbookmark_t zb;
8383547Smaybee 	zio_t *zio;
8393547Smaybee 	list_t *list;
8403547Smaybee 	dbuf_dirty_record_t *dr;
8413547Smaybee 
8423547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
843789Sahrens 
844789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
845789Sahrens 	/* XXX the write_done callback should really give us the tx... */
846789Sahrens 	os->os_synctx = tx;
847789Sahrens 
8483882Sahrens 	if (os->os_dsl_dataset == NULL) {
8493882Sahrens 		/*
8503882Sahrens 		 * This is the MOS.  If we have upgraded,
8513882Sahrens 		 * spa_max_replication() could change, so reset
8523882Sahrens 		 * os_copies here.
8533882Sahrens 		 */
8543882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
8553882Sahrens 	}
8563882Sahrens 
8573547Smaybee 	/*
8583547Smaybee 	 * Create the root block IO
8593547Smaybee 	 */
8603547Smaybee 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
8613547Smaybee 	zb.zb_object = 0;
8623547Smaybee 	zb.zb_level = -1;
8633547Smaybee 	zb.zb_blkid = 0;
8644787Sahrens 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
8653547Smaybee 		dsl_dataset_block_kill(os->os_dsl_dataset,
8663547Smaybee 		    os->os_rootbp, pio, tx);
8674787Sahrens 	}
8683547Smaybee 	zio = arc_write(pio, os->os_spa, os->os_md_checksum,
8693547Smaybee 	    os->os_md_compress,
8703835Sahrens 	    dmu_get_replication_level(os, &zb, DMU_OT_OBJSET),
8713547Smaybee 	    tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, killer, os,
8724634Sek110237 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_METADATA,
8734634Sek110237 	    &zb);
8743547Smaybee 
8753547Smaybee 	/*
8763547Smaybee 	 * Sync meta-dnode - the parent IO for the sync is the root block
8773547Smaybee 	 */
8783547Smaybee 	os->os_meta_dnode->dn_zio = zio;
8793547Smaybee 	dnode_sync(os->os_meta_dnode, tx);
880789Sahrens 
881789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
882789Sahrens 
8833547Smaybee 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], tx);
8843547Smaybee 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], tx);
885789Sahrens 
8863547Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
8873547Smaybee 	while (dr = list_head(list)) {
8883547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
8893547Smaybee 		list_remove(list, dr);
8903547Smaybee 		if (dr->dr_zio)
8913547Smaybee 			zio_nowait(dr->dr_zio);
8923547Smaybee 	}
893789Sahrens 	/*
894789Sahrens 	 * Free intent log blocks up to this tx.
895789Sahrens 	 */
896789Sahrens 	zil_sync(os->os_zil, tx);
8973547Smaybee 	zio_nowait(zio);
898789Sahrens }
899789Sahrens 
900789Sahrens void
9012885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
9022885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
9032885Sahrens {
9042885Sahrens 	dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp,
9052885Sahrens 	    usedobjsp, availobjsp);
9062885Sahrens }
9072885Sahrens 
9082885Sahrens uint64_t
9092885Sahrens dmu_objset_fsid_guid(objset_t *os)
9102885Sahrens {
9112885Sahrens 	return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset));
9122885Sahrens }
9132885Sahrens 
9142885Sahrens void
9152885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
916789Sahrens {
9172885Sahrens 	stat->dds_type = os->os->os_phys->os_type;
9182885Sahrens 	if (os->os->os_dsl_dataset)
9192885Sahrens 		dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat);
9202885Sahrens }
9212885Sahrens 
9222885Sahrens void
9232885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
9242885Sahrens {
9252885Sahrens 	ASSERT(os->os->os_dsl_dataset ||
9262885Sahrens 	    os->os->os_phys->os_type == DMU_OST_META);
9272885Sahrens 
9282885Sahrens 	if (os->os->os_dsl_dataset != NULL)
9292885Sahrens 		dsl_dataset_stats(os->os->os_dsl_dataset, nv);
9302885Sahrens 
9312885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
9322885Sahrens 	    os->os->os_phys->os_type);
933789Sahrens }
934789Sahrens 
935789Sahrens int
936789Sahrens dmu_objset_is_snapshot(objset_t *os)
937789Sahrens {
938789Sahrens 	if (os->os->os_dsl_dataset != NULL)
939789Sahrens 		return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset));
940789Sahrens 	else
941789Sahrens 		return (B_FALSE);
942789Sahrens }
943789Sahrens 
944789Sahrens int
945789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
946885Sahrens     uint64_t *idp, uint64_t *offp)
947789Sahrens {
948789Sahrens 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
949789Sahrens 	zap_cursor_t cursor;
950789Sahrens 	zap_attribute_t attr;
951789Sahrens 
952789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
953789Sahrens 		return (ENOENT);
954789Sahrens 
955789Sahrens 	zap_cursor_init_serialized(&cursor,
956789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
957789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
958789Sahrens 
959885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
960885Sahrens 		zap_cursor_fini(&cursor);
961885Sahrens 		return (ENOENT);
962885Sahrens 	}
963885Sahrens 
964885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
965885Sahrens 		zap_cursor_fini(&cursor);
966885Sahrens 		return (ENAMETOOLONG);
967885Sahrens 	}
968885Sahrens 
969885Sahrens 	(void) strcpy(name, attr.za_name);
970885Sahrens 	if (idp)
971885Sahrens 		*idp = attr.za_first_integer;
972885Sahrens 	zap_cursor_advance(&cursor);
973885Sahrens 	*offp = zap_cursor_serialize(&cursor);
974885Sahrens 	zap_cursor_fini(&cursor);
975885Sahrens 
976885Sahrens 	return (0);
977885Sahrens }
978885Sahrens 
979885Sahrens int
980885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
981885Sahrens     uint64_t *idp, uint64_t *offp)
982885Sahrens {
983885Sahrens 	dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir;
984885Sahrens 	zap_cursor_t cursor;
985885Sahrens 	zap_attribute_t attr;
986885Sahrens 
987885Sahrens 	/* there is no next dir on a snapshot! */
988885Sahrens 	if (os->os->os_dsl_dataset->ds_object !=
989885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
990885Sahrens 		return (ENOENT);
991885Sahrens 
992885Sahrens 	zap_cursor_init_serialized(&cursor,
993885Sahrens 	    dd->dd_pool->dp_meta_objset,
994885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
995885Sahrens 
996885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
997885Sahrens 		zap_cursor_fini(&cursor);
998885Sahrens 		return (ENOENT);
999885Sahrens 	}
1000885Sahrens 
1001885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1002885Sahrens 		zap_cursor_fini(&cursor);
1003789Sahrens 		return (ENAMETOOLONG);
1004885Sahrens 	}
1005789Sahrens 
1006789Sahrens 	(void) strcpy(name, attr.za_name);
1007885Sahrens 	if (idp)
1008885Sahrens 		*idp = attr.za_first_integer;
1009789Sahrens 	zap_cursor_advance(&cursor);
1010789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1011885Sahrens 	zap_cursor_fini(&cursor);
1012789Sahrens 
1013789Sahrens 	return (0);
1014789Sahrens }
1015789Sahrens 
1016789Sahrens /*
1017789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1018789Sahrens  */
10192199Sahrens int
10202199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1021789Sahrens {
1022789Sahrens 	dsl_dir_t *dd;
1023789Sahrens 	objset_t *os;
1024789Sahrens 	uint64_t snapobj;
1025789Sahrens 	zap_cursor_t zc;
10263978Smmusante 	zap_attribute_t *attr;
1027789Sahrens 	char *child;
10281544Seschrock 	int do_self, err;
1029789Sahrens 
10301544Seschrock 	err = dsl_dir_open(name, FTAG, &dd, NULL);
10311544Seschrock 	if (err)
10322199Sahrens 		return (err);
1033789Sahrens 
10342199Sahrens 	/* NB: the $MOS dir doesn't have a head dataset */
1035789Sahrens 	do_self = (dd->dd_phys->dd_head_dataset_obj != 0);
10363978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1037789Sahrens 
1038789Sahrens 	/*
1039789Sahrens 	 * Iterate over all children.
1040789Sahrens 	 */
10412417Sahrens 	if (flags & DS_FIND_CHILDREN) {
10422417Sahrens 		for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset,
10432417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
10443978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
10452417Sahrens 		    (void) zap_cursor_advance(&zc)) {
10463978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
10473978Smmusante 			ASSERT(attr->za_num_integers == 1);
1048789Sahrens 
10492417Sahrens 			/*
10502417Sahrens 			 * No separating '/' because parent's name ends in /.
10512417Sahrens 			 */
10522417Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
10532417Sahrens 			/* XXX could probably just use name here */
10542417Sahrens 			dsl_dir_name(dd, child);
10552417Sahrens 			(void) strcat(child, "/");
10563978Smmusante 			(void) strcat(child, attr->za_name);
10572417Sahrens 			err = dmu_objset_find(child, func, arg, flags);
10582417Sahrens 			kmem_free(child, MAXPATHLEN);
10592417Sahrens 			if (err)
10602417Sahrens 				break;
10612417Sahrens 		}
10622417Sahrens 		zap_cursor_fini(&zc);
10632199Sahrens 
10642417Sahrens 		if (err) {
10652417Sahrens 			dsl_dir_close(dd, FTAG);
10663978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
10672417Sahrens 			return (err);
10682417Sahrens 		}
1069789Sahrens 	}
1070789Sahrens 
1071789Sahrens 	/*
1072789Sahrens 	 * Iterate over all snapshots.
1073789Sahrens 	 */
1074789Sahrens 	if ((flags & DS_FIND_SNAPSHOTS) &&
1075789Sahrens 	    dmu_objset_open(name, DMU_OST_ANY,
1076789Sahrens 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os) == 0) {
1077789Sahrens 
1078789Sahrens 		snapobj = os->os->os_dsl_dataset->ds_phys->ds_snapnames_zapobj;
1079789Sahrens 		dmu_objset_close(os);
1080789Sahrens 
1081789Sahrens 		for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, snapobj);
10823978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
1083789Sahrens 		    (void) zap_cursor_advance(&zc)) {
10843978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
10853978Smmusante 			ASSERT(attr->za_num_integers == 1);
1086789Sahrens 
1087789Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1088789Sahrens 			/* XXX could probably just use name here */
1089789Sahrens 			dsl_dir_name(dd, child);
1090789Sahrens 			(void) strcat(child, "@");
10913978Smmusante 			(void) strcat(child, attr->za_name);
10922199Sahrens 			err = func(child, arg);
1093789Sahrens 			kmem_free(child, MAXPATHLEN);
10942199Sahrens 			if (err)
10952199Sahrens 				break;
1096789Sahrens 		}
1097885Sahrens 		zap_cursor_fini(&zc);
1098789Sahrens 	}
1099789Sahrens 
1100789Sahrens 	dsl_dir_close(dd, FTAG);
11013978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1102789Sahrens 
11032199Sahrens 	if (err)
11042199Sahrens 		return (err);
11052199Sahrens 
1106789Sahrens 	/*
1107789Sahrens 	 * Apply to self if appropriate.
1108789Sahrens 	 */
1109789Sahrens 	if (do_self)
11102199Sahrens 		err = func(name, arg);
11112199Sahrens 	return (err);
1112789Sahrens }
11135326Sek110237 
11145326Sek110237 void
11155326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
11165326Sek110237 {
11175326Sek110237 	ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
11185326Sek110237 	os->os->os_user_ptr = user_ptr;
11195326Sek110237 }
11205326Sek110237 
11215326Sek110237 void *
11225326Sek110237 dmu_objset_get_user(objset_t *os)
11235326Sek110237 {
11245326Sek110237 	ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
11255326Sek110237 	return (os->os->os_user_ptr);
11265326Sek110237 }
1127