xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 3978:2dd668007b7a)
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 
28789Sahrens #include <sys/zfs_context.h>
29789Sahrens #include <sys/dmu_objset.h>
30789Sahrens #include <sys/dsl_dir.h>
31789Sahrens #include <sys/dsl_dataset.h>
32789Sahrens #include <sys/dsl_prop.h>
33789Sahrens #include <sys/dsl_pool.h>
342199Sahrens #include <sys/dsl_synctask.h>
35789Sahrens #include <sys/dnode.h>
36789Sahrens #include <sys/dbuf.h>
372885Sahrens #include <sys/zvol.h>
38789Sahrens #include <sys/dmu_tx.h>
39789Sahrens #include <sys/zio_checksum.h>
40789Sahrens #include <sys/zap.h>
41789Sahrens #include <sys/zil.h>
42789Sahrens #include <sys/dmu_impl.h>
43789Sahrens 
44789Sahrens 
45789Sahrens spa_t *
46789Sahrens dmu_objset_spa(objset_t *os)
47789Sahrens {
48789Sahrens 	return (os->os->os_spa);
49789Sahrens }
50789Sahrens 
51789Sahrens zilog_t *
52789Sahrens dmu_objset_zil(objset_t *os)
53789Sahrens {
54789Sahrens 	return (os->os->os_zil);
55789Sahrens }
56789Sahrens 
57789Sahrens dsl_pool_t *
58789Sahrens dmu_objset_pool(objset_t *os)
59789Sahrens {
60789Sahrens 	dsl_dataset_t *ds;
61789Sahrens 
62789Sahrens 	if ((ds = os->os->os_dsl_dataset) != NULL && ds->ds_dir)
63789Sahrens 		return (ds->ds_dir->dd_pool);
64789Sahrens 	else
65789Sahrens 		return (spa_get_dsl(os->os->os_spa));
66789Sahrens }
67789Sahrens 
68789Sahrens dsl_dataset_t *
69789Sahrens dmu_objset_ds(objset_t *os)
70789Sahrens {
71789Sahrens 	return (os->os->os_dsl_dataset);
72789Sahrens }
73789Sahrens 
74789Sahrens dmu_objset_type_t
75789Sahrens dmu_objset_type(objset_t *os)
76789Sahrens {
77789Sahrens 	return (os->os->os_phys->os_type);
78789Sahrens }
79789Sahrens 
80789Sahrens void
81789Sahrens dmu_objset_name(objset_t *os, char *buf)
82789Sahrens {
83789Sahrens 	dsl_dataset_name(os->os->os_dsl_dataset, buf);
84789Sahrens }
85789Sahrens 
86789Sahrens uint64_t
87789Sahrens dmu_objset_id(objset_t *os)
88789Sahrens {
89789Sahrens 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
90789Sahrens 
91789Sahrens 	return (ds ? ds->ds_object : 0);
92789Sahrens }
93789Sahrens 
94789Sahrens static void
95789Sahrens checksum_changed_cb(void *arg, uint64_t newval)
96789Sahrens {
97789Sahrens 	objset_impl_t *osi = arg;
98789Sahrens 
99789Sahrens 	/*
100789Sahrens 	 * Inheritance should have been done by now.
101789Sahrens 	 */
102789Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
103789Sahrens 
104789Sahrens 	osi->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
105789Sahrens }
106789Sahrens 
107789Sahrens static void
108789Sahrens compression_changed_cb(void *arg, uint64_t newval)
109789Sahrens {
110789Sahrens 	objset_impl_t *osi = arg;
111789Sahrens 
112789Sahrens 	/*
113789Sahrens 	 * Inheritance and range checking should have been done by now.
114789Sahrens 	 */
115789Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
116789Sahrens 
117789Sahrens 	osi->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
118789Sahrens }
119789Sahrens 
1203835Sahrens static void
1213835Sahrens copies_changed_cb(void *arg, uint64_t newval)
1223835Sahrens {
1233835Sahrens 	objset_impl_t *osi = arg;
1243835Sahrens 
1253835Sahrens 	/*
1263835Sahrens 	 * Inheritance and range checking should have been done by now.
1273835Sahrens 	 */
1283835Sahrens 	ASSERT(newval > 0);
1293835Sahrens 	ASSERT(newval <= spa_max_replication(osi->os_spa));
1303835Sahrens 
1313835Sahrens 	osi->os_copies = newval;
1323835Sahrens }
1333835Sahrens 
134789Sahrens void
135789Sahrens dmu_objset_byteswap(void *buf, size_t size)
136789Sahrens {
137789Sahrens 	objset_phys_t *osp = buf;
138789Sahrens 
139789Sahrens 	ASSERT(size == sizeof (objset_phys_t));
140789Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
141789Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
142789Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
143789Sahrens }
144789Sahrens 
1451544Seschrock int
1461544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
1471544Seschrock     objset_impl_t **osip)
148789Sahrens {
149789Sahrens 	objset_impl_t *winner, *osi;
150789Sahrens 	int i, err, checksum;
151789Sahrens 
152789Sahrens 	osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP);
153789Sahrens 	osi->os.os = osi;
154789Sahrens 	osi->os_dsl_dataset = ds;
155789Sahrens 	osi->os_spa = spa;
1563547Smaybee 	osi->os_rootbp = bp;
1573547Smaybee 	if (!BP_IS_HOLE(osi->os_rootbp)) {
1582391Smaybee 		uint32_t aflags = ARC_WAIT;
1591544Seschrock 		zbookmark_t zb;
1601544Seschrock 		zb.zb_objset = ds ? ds->ds_object : 0;
1611544Seschrock 		zb.zb_object = 0;
1621544Seschrock 		zb.zb_level = -1;
1631544Seschrock 		zb.zb_blkid = 0;
1641544Seschrock 
1653547Smaybee 		dprintf_bp(osi->os_rootbp, "reading %s", "");
1663547Smaybee 		err = arc_read(NULL, spa, osi->os_rootbp,
167789Sahrens 		    dmu_ot[DMU_OT_OBJSET].ot_byteswap,
1683547Smaybee 		    arc_getbuf_func, &osi->os_phys_buf,
1692391Smaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
1701544Seschrock 		if (err) {
1711544Seschrock 			kmem_free(osi, sizeof (objset_impl_t));
1721544Seschrock 			return (err);
1731544Seschrock 		}
1743547Smaybee 		osi->os_phys = osi->os_phys_buf->b_data;
1753547Smaybee 		arc_release(osi->os_phys_buf, &osi->os_phys_buf);
176789Sahrens 	} else {
1773547Smaybee 		osi->os_phys_buf = arc_buf_alloc(spa, sizeof (objset_phys_t),
1783547Smaybee 		    &osi->os_phys_buf, ARC_BUFC_METADATA);
1793547Smaybee 		osi->os_phys = osi->os_phys_buf->b_data;
180789Sahrens 		bzero(osi->os_phys, sizeof (objset_phys_t));
181789Sahrens 	}
182789Sahrens 
183789Sahrens 	/*
184789Sahrens 	 * Note: the changed_cb will be called once before the register
185789Sahrens 	 * func returns, thus changing the checksum/compression from the
1862082Seschrock 	 * default (fletcher2/off).  Snapshots don't need to know, and
1872082Seschrock 	 * registering would complicate clone promotion.
188789Sahrens 	 */
1892082Seschrock 	if (ds && ds->ds_phys->ds_num_children == 0) {
190789Sahrens 		err = dsl_prop_register(ds, "checksum",
191789Sahrens 		    checksum_changed_cb, osi);
1921544Seschrock 		if (err == 0)
1931544Seschrock 			err = dsl_prop_register(ds, "compression",
1941544Seschrock 			    compression_changed_cb, osi);
1953835Sahrens 		if (err == 0)
1963835Sahrens 			err = dsl_prop_register(ds, "copies",
1973835Sahrens 			    copies_changed_cb, osi);
1981544Seschrock 		if (err) {
1993547Smaybee 			VERIFY(arc_buf_remove_ref(osi->os_phys_buf,
2003547Smaybee 			    &osi->os_phys_buf) == 1);
2011544Seschrock 			kmem_free(osi, sizeof (objset_impl_t));
2021544Seschrock 			return (err);
2031544Seschrock 		}
2042082Seschrock 	} else if (ds == NULL) {
205789Sahrens 		/* It's the meta-objset. */
206789Sahrens 		osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
2071544Seschrock 		osi->os_compress = ZIO_COMPRESS_LZJB;
2083835Sahrens 		osi->os_copies = spa_max_replication(spa);
209789Sahrens 	}
210789Sahrens 
2111544Seschrock 	osi->os_zil = zil_alloc(&osi->os, &osi->os_phys->os_zil_header);
2121544Seschrock 
213789Sahrens 	/*
214789Sahrens 	 * Metadata always gets compressed and checksummed.
215789Sahrens 	 * If the data checksum is multi-bit correctable, and it's not
216789Sahrens 	 * a ZBT-style checksum, then it's suitable for metadata as well.
217789Sahrens 	 * Otherwise, the metadata checksum defaults to fletcher4.
218789Sahrens 	 */
219789Sahrens 	checksum = osi->os_checksum;
220789Sahrens 
221789Sahrens 	if (zio_checksum_table[checksum].ci_correctable &&
222789Sahrens 	    !zio_checksum_table[checksum].ci_zbt)
223789Sahrens 		osi->os_md_checksum = checksum;
224789Sahrens 	else
225789Sahrens 		osi->os_md_checksum = ZIO_CHECKSUM_FLETCHER_4;
2261544Seschrock 	osi->os_md_compress = ZIO_COMPRESS_LZJB;
227789Sahrens 
228789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
229789Sahrens 		list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t),
230789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
231789Sahrens 		list_create(&osi->os_free_dnodes[i], sizeof (dnode_t),
232789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
233789Sahrens 	}
234789Sahrens 	list_create(&osi->os_dnodes, sizeof (dnode_t),
235789Sahrens 	    offsetof(dnode_t, dn_link));
236789Sahrens 	list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
237789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
238789Sahrens 
2392856Snd150628 	mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL);
2402856Snd150628 	mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
2412856Snd150628 
242789Sahrens 	osi->os_meta_dnode = dnode_special_open(osi,
243789Sahrens 	    &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
244789Sahrens 
245789Sahrens 	if (ds != NULL) {
246789Sahrens 		winner = dsl_dataset_set_user_ptr(ds, osi, dmu_objset_evict);
247789Sahrens 		if (winner) {
248789Sahrens 			dmu_objset_evict(ds, osi);
249789Sahrens 			osi = winner;
250789Sahrens 		}
251789Sahrens 	}
252789Sahrens 
2531544Seschrock 	*osip = osi;
2541544Seschrock 	return (0);
255789Sahrens }
256789Sahrens 
257789Sahrens /* called from zpl */
258789Sahrens int
259789Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode,
260789Sahrens     objset_t **osp)
261789Sahrens {
262789Sahrens 	dsl_dataset_t *ds;
263789Sahrens 	int err;
264789Sahrens 	objset_t *os;
265789Sahrens 	objset_impl_t *osi;
266789Sahrens 
267789Sahrens 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
268789Sahrens 	err = dsl_dataset_open(name, mode, os, &ds);
269789Sahrens 	if (err) {
270789Sahrens 		kmem_free(os, sizeof (objset_t));
271789Sahrens 		return (err);
272789Sahrens 	}
273789Sahrens 
274789Sahrens 	osi = dsl_dataset_get_user_ptr(ds);
275789Sahrens 	if (osi == NULL) {
2761544Seschrock 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
2773547Smaybee 		    ds, &ds->ds_phys->ds_bp, &osi);
2781544Seschrock 		if (err) {
2791544Seschrock 			dsl_dataset_close(ds, mode, os);
2801544Seschrock 			kmem_free(os, sizeof (objset_t));
2811544Seschrock 			return (err);
2821544Seschrock 		}
283789Sahrens 	}
284789Sahrens 
285789Sahrens 	os->os = osi;
286789Sahrens 	os->os_mode = mode;
287789Sahrens 
288789Sahrens 	if (type != DMU_OST_ANY && type != os->os->os_phys->os_type) {
289789Sahrens 		dmu_objset_close(os);
290789Sahrens 		return (EINVAL);
291789Sahrens 	}
292789Sahrens 	*osp = os;
293789Sahrens 	return (0);
294789Sahrens }
295789Sahrens 
296789Sahrens void
297789Sahrens dmu_objset_close(objset_t *os)
298789Sahrens {
299789Sahrens 	dsl_dataset_close(os->os->os_dsl_dataset, os->os_mode, os);
300789Sahrens 	kmem_free(os, sizeof (objset_t));
301789Sahrens }
302789Sahrens 
3031646Sperrin int
3041646Sperrin dmu_objset_evict_dbufs(objset_t *os, int try)
3051544Seschrock {
3061544Seschrock 	objset_impl_t *osi = os->os;
3071544Seschrock 	dnode_t *dn;
3081596Sahrens 
3091596Sahrens 	mutex_enter(&osi->os_lock);
3101596Sahrens 
3111596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
3121596Sahrens 	list_remove(&osi->os_dnodes, osi->os_meta_dnode);
3131596Sahrens 	list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode);
3141544Seschrock 
3151544Seschrock 	/*
3161596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
3171596Sahrens 	 * because dnode_add_ref() only works if you already have a
3181596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
3191596Sahrens 	 * skip.
3201544Seschrock 	 */
3211596Sahrens 	for (dn = list_head(&osi->os_dnodes);
3221596Sahrens 	    dn && refcount_is_zero(&dn->dn_holds);
3231596Sahrens 	    dn = list_next(&osi->os_dnodes, dn))
3241596Sahrens 		continue;
3251596Sahrens 	if (dn)
3261596Sahrens 		dnode_add_ref(dn, FTAG);
3271596Sahrens 
3281596Sahrens 	while (dn) {
3291596Sahrens 		dnode_t *next_dn = dn;
3301596Sahrens 
3311596Sahrens 		do {
3321596Sahrens 			next_dn = list_next(&osi->os_dnodes, next_dn);
3331596Sahrens 		} while (next_dn && refcount_is_zero(&next_dn->dn_holds));
3341596Sahrens 		if (next_dn)
3351596Sahrens 			dnode_add_ref(next_dn, FTAG);
3361596Sahrens 
3371596Sahrens 		mutex_exit(&osi->os_lock);
3381646Sperrin 		if (dnode_evict_dbufs(dn, try)) {
3391646Sperrin 			dnode_rele(dn, FTAG);
3401646Sperrin 			if (next_dn)
3411646Sperrin 				dnode_rele(next_dn, FTAG);
3421646Sperrin 			return (1);
3431646Sperrin 		}
3441596Sahrens 		dnode_rele(dn, FTAG);
3451596Sahrens 		mutex_enter(&osi->os_lock);
3461596Sahrens 		dn = next_dn;
3471544Seschrock 	}
3481544Seschrock 	mutex_exit(&osi->os_lock);
3491646Sperrin 	return (0);
3501544Seschrock }
3511544Seschrock 
3521544Seschrock void
353789Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg)
354789Sahrens {
355789Sahrens 	objset_impl_t *osi = arg;
3561544Seschrock 	objset_t os;
3572082Seschrock 	int i;
358789Sahrens 
359789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
360789Sahrens 		ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL);
361789Sahrens 		ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL);
362789Sahrens 	}
363789Sahrens 
3642082Seschrock 	if (ds && ds->ds_phys->ds_num_children == 0) {
3652082Seschrock 		VERIFY(0 == dsl_prop_unregister(ds, "checksum",
3662082Seschrock 		    checksum_changed_cb, osi));
3672082Seschrock 		VERIFY(0 == dsl_prop_unregister(ds, "compression",
3682082Seschrock 		    compression_changed_cb, osi));
3693835Sahrens 		VERIFY(0 == dsl_prop_unregister(ds, "copies",
3703835Sahrens 		    copies_changed_cb, osi));
371789Sahrens 	}
372789Sahrens 
3731544Seschrock 	/*
3741544Seschrock 	 * We should need only a single pass over the dnode list, since
3751544Seschrock 	 * nothing can be added to the list at this point.
3761544Seschrock 	 */
3771544Seschrock 	os.os = osi;
3781646Sperrin 	(void) dmu_objset_evict_dbufs(&os, 0);
3791544Seschrock 
380789Sahrens 	ASSERT3P(list_head(&osi->os_dnodes), ==, osi->os_meta_dnode);
381789Sahrens 	ASSERT3P(list_tail(&osi->os_dnodes), ==, osi->os_meta_dnode);
382789Sahrens 	ASSERT3P(list_head(&osi->os_meta_dnode->dn_dbufs), ==, NULL);
383789Sahrens 
384789Sahrens 	dnode_special_close(osi->os_meta_dnode);
385789Sahrens 	zil_free(osi->os_zil);
386789Sahrens 
3873547Smaybee 	VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1);
3882856Snd150628 	mutex_destroy(&osi->os_lock);
3892856Snd150628 	mutex_destroy(&osi->os_obj_lock);
390789Sahrens 	kmem_free(osi, sizeof (objset_impl_t));
391789Sahrens }
392789Sahrens 
393789Sahrens /* called from dsl for meta-objset */
394789Sahrens objset_impl_t *
3953547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
3963547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
397789Sahrens {
398789Sahrens 	objset_impl_t *osi;
399789Sahrens 	dnode_t *mdn;
400789Sahrens 
401789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
4023547Smaybee 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi));
403789Sahrens 	mdn = osi->os_meta_dnode;
404789Sahrens 
405789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
406789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
407789Sahrens 
408789Sahrens 	/*
409789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
410789Sahrens 	 * later, because then we could do it in quescing context while
411789Sahrens 	 * we are also accessing it in open context.
412789Sahrens 	 *
413789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
414789Sahrens 	 * because the MOS is only updated in syncing context.
415789Sahrens 	 * This is most fortunate: the MOS is the only objset that
416789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
417789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
418789Sahrens 	 */
4191544Seschrock 	if (ds != NULL) {
4201544Seschrock 		int levels = 1;
4211544Seschrock 
4221544Seschrock 		/*
4231544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
4241544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
4251544Seschrock 		 */
4261544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
4271544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
4281544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
4291544Seschrock 			levels++;
4301544Seschrock 
431789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
4321544Seschrock 		    mdn->dn_nlevels = levels;
4331544Seschrock 	}
434789Sahrens 
435789Sahrens 	ASSERT(type != DMU_OST_NONE);
436789Sahrens 	ASSERT(type != DMU_OST_ANY);
437789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
438789Sahrens 	osi->os_phys->os_type = type;
439789Sahrens 
440789Sahrens 	dsl_dataset_dirty(ds, tx);
441789Sahrens 
442789Sahrens 	return (osi);
443789Sahrens }
444789Sahrens 
445789Sahrens struct oscarg {
446789Sahrens 	void (*userfunc)(objset_t *os, void *arg, dmu_tx_t *tx);
447789Sahrens 	void *userarg;
448789Sahrens 	dsl_dataset_t *clone_parent;
449789Sahrens 	const char *lastname;
450789Sahrens 	dmu_objset_type_t type;
451789Sahrens };
452789Sahrens 
4532199Sahrens /* ARGSUSED */
454789Sahrens static int
4552199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
456789Sahrens {
4572199Sahrens 	dsl_dir_t *dd = arg1;
4582199Sahrens 	struct oscarg *oa = arg2;
4592199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
4602199Sahrens 	int err;
4612199Sahrens 	uint64_t ddobj;
4622199Sahrens 
4632199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
4642199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
4652199Sahrens 	if (err != ENOENT)
4662199Sahrens 		return (err ? err : EEXIST);
4672199Sahrens 
4682199Sahrens 	if (oa->clone_parent != NULL) {
4692199Sahrens 		/*
4702199Sahrens 		 * You can't clone across pools.
4712199Sahrens 		 */
4722199Sahrens 		if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool)
4732199Sahrens 			return (EXDEV);
4742199Sahrens 
4752199Sahrens 		/*
4762199Sahrens 		 * You can only clone snapshots, not the head datasets.
4772199Sahrens 		 */
4782199Sahrens 		if (oa->clone_parent->ds_phys->ds_num_children == 0)
4792199Sahrens 			return (EINVAL);
4802199Sahrens 	}
4812199Sahrens 	return (0);
4822199Sahrens }
4832199Sahrens 
4842199Sahrens static void
4852199Sahrens dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx)
4862199Sahrens {
4872199Sahrens 	dsl_dir_t *dd = arg1;
4882199Sahrens 	struct oscarg *oa = arg2;
489789Sahrens 	dsl_dataset_t *ds;
4903547Smaybee 	blkptr_t *bp;
4912199Sahrens 	uint64_t dsobj;
492789Sahrens 
493789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
494789Sahrens 
4952199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
496789Sahrens 	    oa->clone_parent, tx);
497789Sahrens 
4982199Sahrens 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, dsobj, NULL,
4991544Seschrock 	    DS_MODE_STANDARD | DS_MODE_READONLY, FTAG, &ds));
5003547Smaybee 	bp = dsl_dataset_get_blkptr(ds);
5013547Smaybee 	if (BP_IS_HOLE(bp)) {
502789Sahrens 		objset_impl_t *osi;
503789Sahrens 
504789Sahrens 		/* This is an empty dmu_objset; not a clone. */
505789Sahrens 		osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
5063547Smaybee 		    ds, bp, oa->type, tx);
507789Sahrens 
508789Sahrens 		if (oa->userfunc)
509789Sahrens 			oa->userfunc(&osi->os, oa->userarg, tx);
510789Sahrens 	}
511789Sahrens 	dsl_dataset_close(ds, DS_MODE_STANDARD | DS_MODE_READONLY, FTAG);
512789Sahrens }
513789Sahrens 
514789Sahrens int
515789Sahrens dmu_objset_create(const char *name, dmu_objset_type_t type,
516789Sahrens     objset_t *clone_parent,
517789Sahrens     void (*func)(objset_t *os, void *arg, dmu_tx_t *tx), void *arg)
518789Sahrens {
5192199Sahrens 	dsl_dir_t *pdd;
520789Sahrens 	const char *tail;
521789Sahrens 	int err = 0;
5222199Sahrens 	struct oscarg oa = { 0 };
523789Sahrens 
5242199Sahrens 	ASSERT(strchr(name, '@') == NULL);
5252199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
5261544Seschrock 	if (err)
5271544Seschrock 		return (err);
528789Sahrens 	if (tail == NULL) {
5292199Sahrens 		dsl_dir_close(pdd, FTAG);
530789Sahrens 		return (EEXIST);
531789Sahrens 	}
532789Sahrens 
533789Sahrens 	dprintf("name=%s\n", name);
534789Sahrens 
5352199Sahrens 	oa.userfunc = func;
5362199Sahrens 	oa.userarg = arg;
5372199Sahrens 	oa.lastname = tail;
5382199Sahrens 	oa.type = type;
5392199Sahrens 	if (clone_parent != NULL) {
540789Sahrens 		/*
5412199Sahrens 		 * You can't clone to a different type.
542789Sahrens 		 */
5432199Sahrens 		if (clone_parent->os->os_phys->os_type != type) {
5442199Sahrens 			dsl_dir_close(pdd, FTAG);
5452199Sahrens 			return (EINVAL);
546789Sahrens 		}
5472199Sahrens 		oa.clone_parent = clone_parent->os->os_dsl_dataset;
548789Sahrens 	}
5492199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
5502199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
5512199Sahrens 	dsl_dir_close(pdd, FTAG);
552789Sahrens 	return (err);
553789Sahrens }
554789Sahrens 
555789Sahrens int
556789Sahrens dmu_objset_destroy(const char *name)
557789Sahrens {
558789Sahrens 	objset_t *os;
559789Sahrens 	int error;
560789Sahrens 
561789Sahrens 	/*
562789Sahrens 	 * If it looks like we'll be able to destroy it, and there's
563789Sahrens 	 * an unplayed replay log sitting around, destroy the log.
564789Sahrens 	 * It would be nicer to do this in dsl_dataset_destroy_sync(),
565789Sahrens 	 * but the replay log objset is modified in open context.
566789Sahrens 	 */
567789Sahrens 	error = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_EXCLUSIVE, &os);
568789Sahrens 	if (error == 0) {
5691807Sbonwick 		zil_destroy(dmu_objset_zil(os), B_FALSE);
570789Sahrens 		dmu_objset_close(os);
571789Sahrens 	}
572789Sahrens 
573789Sahrens 	return (dsl_dataset_destroy(name));
574789Sahrens }
575789Sahrens 
576789Sahrens int
577789Sahrens dmu_objset_rollback(const char *name)
578789Sahrens {
579789Sahrens 	int err;
580789Sahrens 	objset_t *os;
581789Sahrens 
5822199Sahrens 	err = dmu_objset_open(name, DMU_OST_ANY,
5832199Sahrens 	    DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT, &os);
584789Sahrens 	if (err == 0) {
585789Sahrens 		err = zil_suspend(dmu_objset_zil(os));
586789Sahrens 		if (err == 0)
587789Sahrens 			zil_resume(dmu_objset_zil(os));
588789Sahrens 		if (err == 0) {
589789Sahrens 			/* XXX uncache everything? */
5902199Sahrens 			err = dsl_dataset_rollback(os->os->os_dsl_dataset);
591789Sahrens 		}
5922199Sahrens 		dmu_objset_close(os);
593789Sahrens 	}
594789Sahrens 	return (err);
595789Sahrens }
596789Sahrens 
5972199Sahrens struct snaparg {
5982199Sahrens 	dsl_sync_task_group_t *dstg;
5992199Sahrens 	char *snapname;
6002199Sahrens 	char failed[MAXPATHLEN];
6012199Sahrens };
6022199Sahrens 
6032199Sahrens static int
6042199Sahrens dmu_objset_snapshot_one(char *name, void *arg)
6052199Sahrens {
6062199Sahrens 	struct snaparg *sn = arg;
6072199Sahrens 	objset_t *os;
6083637Srm160521 	dmu_objset_stats_t stat;
6092199Sahrens 	int err;
6102199Sahrens 
6112199Sahrens 	(void) strcpy(sn->failed, name);
6122199Sahrens 
6132199Sahrens 	err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_STANDARD, &os);
6142199Sahrens 	if (err != 0)
6152199Sahrens 		return (err);
6162199Sahrens 
6172199Sahrens 	/*
6183637Srm160521 	 * If the objset is in an inconsistent state, return busy.
6193637Srm160521 	 */
6203637Srm160521 	dmu_objset_fast_stat(os, &stat);
6213637Srm160521 	if (stat.dds_inconsistent) {
6223637Srm160521 		dmu_objset_close(os);
6233637Srm160521 		return (EBUSY);
6243637Srm160521 	}
6253637Srm160521 
6263637Srm160521 	/*
6272199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
6282199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
6292199Sahrens 	 * a side effect.
6302199Sahrens 	 */
6312199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
6322199Sahrens 	if (err == 0) {
6332199Sahrens 		dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check,
6342199Sahrens 		    dsl_dataset_snapshot_sync, os, sn->snapname, 3);
6353637Srm160521 	} else {
6363637Srm160521 		dmu_objset_close(os);
6372199Sahrens 	}
6383637Srm160521 
6392199Sahrens 	return (err);
6402199Sahrens }
6412199Sahrens 
6422199Sahrens int
6432199Sahrens dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive)
6442199Sahrens {
6452199Sahrens 	dsl_sync_task_t *dst;
6462199Sahrens 	struct snaparg sn = { 0 };
6472199Sahrens 	char *cp;
6482199Sahrens 	spa_t *spa;
6492199Sahrens 	int err;
6502199Sahrens 
6512199Sahrens 	(void) strcpy(sn.failed, fsname);
6522199Sahrens 
6532199Sahrens 	cp = strchr(fsname, '/');
6542199Sahrens 	if (cp) {
6552199Sahrens 		*cp = '\0';
6562199Sahrens 		err = spa_open(fsname, &spa, FTAG);
6572199Sahrens 		*cp = '/';
6582199Sahrens 	} else {
6592199Sahrens 		err = spa_open(fsname, &spa, FTAG);
6602199Sahrens 	}
6612199Sahrens 	if (err)
6622199Sahrens 		return (err);
6632199Sahrens 
6642199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
6652199Sahrens 	sn.snapname = snapname;
6662199Sahrens 
6672417Sahrens 	if (recursive) {
6682417Sahrens 		err = dmu_objset_find(fsname,
6692417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
6702417Sahrens 	} else {
6712199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
6722417Sahrens 	}
6732199Sahrens 
6742199Sahrens 	if (err)
6752199Sahrens 		goto out;
6762199Sahrens 
6772199Sahrens 	err = dsl_sync_task_group_wait(sn.dstg);
6782199Sahrens 
6792199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
6802199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
6812199Sahrens 		objset_t *os = dst->dst_arg1;
6822199Sahrens 		if (dst->dst_err)
6832199Sahrens 			dmu_objset_name(os, sn.failed);
6842199Sahrens 		zil_resume(dmu_objset_zil(os));
6852199Sahrens 		dmu_objset_close(os);
6862199Sahrens 	}
6872199Sahrens out:
6882199Sahrens 	if (err)
6892199Sahrens 		(void) strcpy(fsname, sn.failed);
6902199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
6912199Sahrens 	spa_close(spa, FTAG);
6922199Sahrens 	return (err);
6932199Sahrens }
6942199Sahrens 
695789Sahrens static void
6963547Smaybee dmu_objset_sync_dnodes(list_t *list, dmu_tx_t *tx)
697789Sahrens {
6983547Smaybee 	dnode_t *dn;
699789Sahrens 
7003547Smaybee 	while (dn = list_head(list)) {
7013547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
7023547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
7033547Smaybee 		/*
7043547Smaybee 		 * Initialize dn_zio outside dnode_sync()
7053547Smaybee 		 * to accomodate meta-dnode
7063547Smaybee 		 */
7073547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
7083547Smaybee 		ASSERT(dn->dn_zio);
709789Sahrens 
7103547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
7113547Smaybee 		list_remove(list, dn);
7123547Smaybee 		dnode_sync(dn, tx);
7133547Smaybee 	}
7143547Smaybee }
7152981Sahrens 
7163547Smaybee /* ARGSUSED */
7173547Smaybee static void
7183547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg)
7193547Smaybee {
7203547Smaybee 	objset_impl_t *os = arg;
7213547Smaybee 	blkptr_t *bp = os->os_rootbp;
7223547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
7233547Smaybee 	int i;
7242981Sahrens 
7253547Smaybee 	/*
7263547Smaybee 	 * Update rootbp fill count.
7273547Smaybee 	 */
7283547Smaybee 	bp->blk_fill = 1;	/* count the meta-dnode */
7293547Smaybee 	for (i = 0; i < dnp->dn_nblkptr; i++)
7303547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
731789Sahrens }
732789Sahrens 
733789Sahrens /* ARGSUSED */
734789Sahrens static void
735789Sahrens killer(zio_t *zio, arc_buf_t *abuf, void *arg)
736789Sahrens {
737789Sahrens 	objset_impl_t *os = arg;
738789Sahrens 
739789Sahrens 	ASSERT3U(zio->io_error, ==, 0);
740789Sahrens 
741789Sahrens 	BP_SET_TYPE(zio->io_bp, DMU_OT_OBJSET);
742789Sahrens 	BP_SET_LEVEL(zio->io_bp, 0);
743789Sahrens 
744789Sahrens 	if (!DVA_EQUAL(BP_IDENTITY(zio->io_bp),
745789Sahrens 	    BP_IDENTITY(&zio->io_bp_orig))) {
7463547Smaybee 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
7473547Smaybee 			dsl_dataset_block_kill(os->os_dsl_dataset,
7483547Smaybee 			    &zio->io_bp_orig, NULL, os->os_synctx);
749789Sahrens 		dsl_dataset_block_born(os->os_dsl_dataset, zio->io_bp,
750789Sahrens 		    os->os_synctx);
751789Sahrens 	}
7523547Smaybee 	arc_release(os->os_phys_buf, &os->os_phys_buf);
753789Sahrens }
754789Sahrens 
755789Sahrens /* called from dsl */
756789Sahrens void
7573547Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx)
758789Sahrens {
759789Sahrens 	int txgoff;
7601544Seschrock 	zbookmark_t zb;
7613547Smaybee 	zio_t *zio;
7623547Smaybee 	list_t *list;
7633547Smaybee 	dbuf_dirty_record_t *dr;
7643689Sek110237 	int zio_flags;
7653547Smaybee 
7663547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
767789Sahrens 
768789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
769789Sahrens 	/* XXX the write_done callback should really give us the tx... */
770789Sahrens 	os->os_synctx = tx;
771789Sahrens 
7723882Sahrens 	if (os->os_dsl_dataset == NULL) {
7733882Sahrens 		/*
7743882Sahrens 		 * This is the MOS.  If we have upgraded,
7753882Sahrens 		 * spa_max_replication() could change, so reset
7763882Sahrens 		 * os_copies here.
7773882Sahrens 		 */
7783882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
7793882Sahrens 	}
7803882Sahrens 
7813547Smaybee 	/*
7823547Smaybee 	 * Create the root block IO
7833547Smaybee 	 */
7843547Smaybee 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
7853547Smaybee 	zb.zb_object = 0;
7863547Smaybee 	zb.zb_level = -1;
7873547Smaybee 	zb.zb_blkid = 0;
7883689Sek110237 	zio_flags = ZIO_FLAG_MUSTSUCCEED;
7893689Sek110237 	if (dmu_ot[DMU_OT_OBJSET].ot_metadata || zb.zb_level != 0)
7903689Sek110237 		zio_flags |= ZIO_FLAG_METADATA;
7913547Smaybee 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg))
7923547Smaybee 		dsl_dataset_block_kill(os->os_dsl_dataset,
7933547Smaybee 		    os->os_rootbp, pio, tx);
7943547Smaybee 	zio = arc_write(pio, os->os_spa, os->os_md_checksum,
7953547Smaybee 	    os->os_md_compress,
7963835Sahrens 	    dmu_get_replication_level(os, &zb, DMU_OT_OBJSET),
7973547Smaybee 	    tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, killer, os,
7983689Sek110237 	    ZIO_PRIORITY_ASYNC_WRITE, zio_flags, &zb);
7993547Smaybee 
8003547Smaybee 	/*
8013547Smaybee 	 * Sync meta-dnode - the parent IO for the sync is the root block
8023547Smaybee 	 */
8033547Smaybee 	os->os_meta_dnode->dn_zio = zio;
8043547Smaybee 	dnode_sync(os->os_meta_dnode, tx);
805789Sahrens 
806789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
807789Sahrens 
8083547Smaybee 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], tx);
8093547Smaybee 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], tx);
810789Sahrens 
8113547Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
8123547Smaybee 	while (dr = list_head(list)) {
8133547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
8143547Smaybee 		list_remove(list, dr);
8153547Smaybee 		if (dr->dr_zio)
8163547Smaybee 			zio_nowait(dr->dr_zio);
8173547Smaybee 	}
818789Sahrens 	/*
819789Sahrens 	 * Free intent log blocks up to this tx.
820789Sahrens 	 */
821789Sahrens 	zil_sync(os->os_zil, tx);
8223547Smaybee 	zio_nowait(zio);
823789Sahrens }
824789Sahrens 
825789Sahrens void
8262885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
8272885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
8282885Sahrens {
8292885Sahrens 	dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp,
8302885Sahrens 	    usedobjsp, availobjsp);
8312885Sahrens }
8322885Sahrens 
8332885Sahrens uint64_t
8342885Sahrens dmu_objset_fsid_guid(objset_t *os)
8352885Sahrens {
8362885Sahrens 	return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset));
8372885Sahrens }
8382885Sahrens 
8392885Sahrens void
8402885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
841789Sahrens {
8422885Sahrens 	stat->dds_type = os->os->os_phys->os_type;
8432885Sahrens 	if (os->os->os_dsl_dataset)
8442885Sahrens 		dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat);
8452885Sahrens }
8462885Sahrens 
8472885Sahrens void
8482885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
8492885Sahrens {
8502885Sahrens 	ASSERT(os->os->os_dsl_dataset ||
8512885Sahrens 	    os->os->os_phys->os_type == DMU_OST_META);
8522885Sahrens 
8532885Sahrens 	if (os->os->os_dsl_dataset != NULL)
8542885Sahrens 		dsl_dataset_stats(os->os->os_dsl_dataset, nv);
8552885Sahrens 
8562885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
8572885Sahrens 	    os->os->os_phys->os_type);
858789Sahrens }
859789Sahrens 
860789Sahrens int
861789Sahrens dmu_objset_is_snapshot(objset_t *os)
862789Sahrens {
863789Sahrens 	if (os->os->os_dsl_dataset != NULL)
864789Sahrens 		return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset));
865789Sahrens 	else
866789Sahrens 		return (B_FALSE);
867789Sahrens }
868789Sahrens 
869789Sahrens int
870789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
871885Sahrens     uint64_t *idp, uint64_t *offp)
872789Sahrens {
873789Sahrens 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
874789Sahrens 	zap_cursor_t cursor;
875789Sahrens 	zap_attribute_t attr;
876789Sahrens 
877789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
878789Sahrens 		return (ENOENT);
879789Sahrens 
880789Sahrens 	zap_cursor_init_serialized(&cursor,
881789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
882789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
883789Sahrens 
884885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
885885Sahrens 		zap_cursor_fini(&cursor);
886885Sahrens 		return (ENOENT);
887885Sahrens 	}
888885Sahrens 
889885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
890885Sahrens 		zap_cursor_fini(&cursor);
891885Sahrens 		return (ENAMETOOLONG);
892885Sahrens 	}
893885Sahrens 
894885Sahrens 	(void) strcpy(name, attr.za_name);
895885Sahrens 	if (idp)
896885Sahrens 		*idp = attr.za_first_integer;
897885Sahrens 	zap_cursor_advance(&cursor);
898885Sahrens 	*offp = zap_cursor_serialize(&cursor);
899885Sahrens 	zap_cursor_fini(&cursor);
900885Sahrens 
901885Sahrens 	return (0);
902885Sahrens }
903885Sahrens 
904885Sahrens int
905885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
906885Sahrens     uint64_t *idp, uint64_t *offp)
907885Sahrens {
908885Sahrens 	dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir;
909885Sahrens 	zap_cursor_t cursor;
910885Sahrens 	zap_attribute_t attr;
911885Sahrens 
912885Sahrens 	/* there is no next dir on a snapshot! */
913885Sahrens 	if (os->os->os_dsl_dataset->ds_object !=
914885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
915885Sahrens 		return (ENOENT);
916885Sahrens 
917885Sahrens 	zap_cursor_init_serialized(&cursor,
918885Sahrens 	    dd->dd_pool->dp_meta_objset,
919885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
920885Sahrens 
921885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
922885Sahrens 		zap_cursor_fini(&cursor);
923885Sahrens 		return (ENOENT);
924885Sahrens 	}
925885Sahrens 
926885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
927885Sahrens 		zap_cursor_fini(&cursor);
928789Sahrens 		return (ENAMETOOLONG);
929885Sahrens 	}
930789Sahrens 
931789Sahrens 	(void) strcpy(name, attr.za_name);
932885Sahrens 	if (idp)
933885Sahrens 		*idp = attr.za_first_integer;
934789Sahrens 	zap_cursor_advance(&cursor);
935789Sahrens 	*offp = zap_cursor_serialize(&cursor);
936885Sahrens 	zap_cursor_fini(&cursor);
937789Sahrens 
938789Sahrens 	return (0);
939789Sahrens }
940789Sahrens 
941789Sahrens /*
942789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
943789Sahrens  */
9442199Sahrens int
9452199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
946789Sahrens {
947789Sahrens 	dsl_dir_t *dd;
948789Sahrens 	objset_t *os;
949789Sahrens 	uint64_t snapobj;
950789Sahrens 	zap_cursor_t zc;
951*3978Smmusante 	zap_attribute_t *attr;
952789Sahrens 	char *child;
9531544Seschrock 	int do_self, err;
954789Sahrens 
9551544Seschrock 	err = dsl_dir_open(name, FTAG, &dd, NULL);
9561544Seschrock 	if (err)
9572199Sahrens 		return (err);
958789Sahrens 
9592199Sahrens 	/* NB: the $MOS dir doesn't have a head dataset */
960789Sahrens 	do_self = (dd->dd_phys->dd_head_dataset_obj != 0);
961*3978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
962789Sahrens 
963789Sahrens 	/*
964789Sahrens 	 * Iterate over all children.
965789Sahrens 	 */
9662417Sahrens 	if (flags & DS_FIND_CHILDREN) {
9672417Sahrens 		for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset,
9682417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
969*3978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
9702417Sahrens 		    (void) zap_cursor_advance(&zc)) {
971*3978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
972*3978Smmusante 			ASSERT(attr->za_num_integers == 1);
973789Sahrens 
9742417Sahrens 			/*
9752417Sahrens 			 * No separating '/' because parent's name ends in /.
9762417Sahrens 			 */
9772417Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
9782417Sahrens 			/* XXX could probably just use name here */
9792417Sahrens 			dsl_dir_name(dd, child);
9802417Sahrens 			(void) strcat(child, "/");
981*3978Smmusante 			(void) strcat(child, attr->za_name);
9822417Sahrens 			err = dmu_objset_find(child, func, arg, flags);
9832417Sahrens 			kmem_free(child, MAXPATHLEN);
9842417Sahrens 			if (err)
9852417Sahrens 				break;
9862417Sahrens 		}
9872417Sahrens 		zap_cursor_fini(&zc);
9882199Sahrens 
9892417Sahrens 		if (err) {
9902417Sahrens 			dsl_dir_close(dd, FTAG);
991*3978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
9922417Sahrens 			return (err);
9932417Sahrens 		}
994789Sahrens 	}
995789Sahrens 
996789Sahrens 	/*
997789Sahrens 	 * Iterate over all snapshots.
998789Sahrens 	 */
999789Sahrens 	if ((flags & DS_FIND_SNAPSHOTS) &&
1000789Sahrens 	    dmu_objset_open(name, DMU_OST_ANY,
1001789Sahrens 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os) == 0) {
1002789Sahrens 
1003789Sahrens 		snapobj = os->os->os_dsl_dataset->ds_phys->ds_snapnames_zapobj;
1004789Sahrens 		dmu_objset_close(os);
1005789Sahrens 
1006789Sahrens 		for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, snapobj);
1007*3978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
1008789Sahrens 		    (void) zap_cursor_advance(&zc)) {
1009*3978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
1010*3978Smmusante 			ASSERT(attr->za_num_integers == 1);
1011789Sahrens 
1012789Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1013789Sahrens 			/* XXX could probably just use name here */
1014789Sahrens 			dsl_dir_name(dd, child);
1015789Sahrens 			(void) strcat(child, "@");
1016*3978Smmusante 			(void) strcat(child, attr->za_name);
10172199Sahrens 			err = func(child, arg);
1018789Sahrens 			kmem_free(child, MAXPATHLEN);
10192199Sahrens 			if (err)
10202199Sahrens 				break;
1021789Sahrens 		}
1022885Sahrens 		zap_cursor_fini(&zc);
1023789Sahrens 	}
1024789Sahrens 
1025789Sahrens 	dsl_dir_close(dd, FTAG);
1026*3978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1027789Sahrens 
10282199Sahrens 	if (err)
10292199Sahrens 		return (err);
10302199Sahrens 
1031789Sahrens 	/*
1032789Sahrens 	 * Apply to self if appropriate.
1033789Sahrens 	 */
1034789Sahrens 	if (do_self)
10352199Sahrens 		err = func(name, arg);
10362199Sahrens 	return (err);
1037789Sahrens }
1038