xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 7237:f47d41541b14)
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 /*
226174Sahrens  * Copyright 2008 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 
136*7237Sek110237 static void
137*7237Sek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
138*7237Sek110237 {
139*7237Sek110237 	objset_impl_t *osi = arg;
140*7237Sek110237 
141*7237Sek110237 	/*
142*7237Sek110237 	 * Inheritance and range checking should have been done by now.
143*7237Sek110237 	 */
144*7237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
145*7237Sek110237 	    newval == ZFS_CACHE_METADATA);
146*7237Sek110237 
147*7237Sek110237 	osi->os_primary_cache = newval;
148*7237Sek110237 }
149*7237Sek110237 
150*7237Sek110237 static void
151*7237Sek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
152*7237Sek110237 {
153*7237Sek110237 	objset_impl_t *osi = arg;
154*7237Sek110237 
155*7237Sek110237 	/*
156*7237Sek110237 	 * Inheritance and range checking should have been done by now.
157*7237Sek110237 	 */
158*7237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
159*7237Sek110237 	    newval == ZFS_CACHE_METADATA);
160*7237Sek110237 
161*7237Sek110237 	osi->os_secondary_cache = newval;
162*7237Sek110237 }
163*7237Sek110237 
164789Sahrens void
165789Sahrens dmu_objset_byteswap(void *buf, size_t size)
166789Sahrens {
167789Sahrens 	objset_phys_t *osp = buf;
168789Sahrens 
169789Sahrens 	ASSERT(size == sizeof (objset_phys_t));
170789Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
171789Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
172789Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
173789Sahrens }
174789Sahrens 
1751544Seschrock int
1761544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
1771544Seschrock     objset_impl_t **osip)
178789Sahrens {
1794787Sahrens 	objset_impl_t *osi;
1807046Sahrens 	int i, err;
181789Sahrens 
1824787Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
1834787Sahrens 
184789Sahrens 	osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP);
185789Sahrens 	osi->os.os = osi;
186789Sahrens 	osi->os_dsl_dataset = ds;
187789Sahrens 	osi->os_spa = spa;
1883547Smaybee 	osi->os_rootbp = bp;
1893547Smaybee 	if (!BP_IS_HOLE(osi->os_rootbp)) {
1902391Smaybee 		uint32_t aflags = ARC_WAIT;
1911544Seschrock 		zbookmark_t zb;
1921544Seschrock 		zb.zb_objset = ds ? ds->ds_object : 0;
1931544Seschrock 		zb.zb_object = 0;
1941544Seschrock 		zb.zb_level = -1;
1951544Seschrock 		zb.zb_blkid = 0;
196*7237Sek110237 		if (DMU_OS_IS_L2CACHEABLE(osi))
197*7237Sek110237 			aflags |= ARC_L2CACHE;
1981544Seschrock 
1993547Smaybee 		dprintf_bp(osi->os_rootbp, "reading %s", "");
2007046Sahrens 		/*
2017046Sahrens 		 * NB: when bprewrite scrub can change the bp,
2027046Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
2037046Sahrens 		 * could change, and we'll need a lock.
2047046Sahrens 		 */
2057046Sahrens 		err = arc_read_nolock(NULL, spa, osi->os_rootbp,
2063547Smaybee 		    arc_getbuf_func, &osi->os_phys_buf,
2072391Smaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
2081544Seschrock 		if (err) {
2091544Seschrock 			kmem_free(osi, sizeof (objset_impl_t));
2101544Seschrock 			return (err);
2111544Seschrock 		}
2123547Smaybee 		osi->os_phys = osi->os_phys_buf->b_data;
213789Sahrens 	} else {
2143547Smaybee 		osi->os_phys_buf = arc_buf_alloc(spa, sizeof (objset_phys_t),
2153547Smaybee 		    &osi->os_phys_buf, ARC_BUFC_METADATA);
2163547Smaybee 		osi->os_phys = osi->os_phys_buf->b_data;
217789Sahrens 		bzero(osi->os_phys, sizeof (objset_phys_t));
218789Sahrens 	}
219789Sahrens 
220789Sahrens 	/*
221789Sahrens 	 * Note: the changed_cb will be called once before the register
222789Sahrens 	 * func returns, thus changing the checksum/compression from the
223*7237Sek110237 	 * default (fletcher2/off).  Snapshots don't need to know about
224*7237Sek110237 	 * checksum/compression/copies.
225789Sahrens 	 */
226*7237Sek110237 	if (ds) {
227*7237Sek110237 		err = dsl_prop_register(ds, "primarycache",
228*7237Sek110237 		    primary_cache_changed_cb, osi);
2291544Seschrock 		if (err == 0)
230*7237Sek110237 			err = dsl_prop_register(ds, "secondarycache",
231*7237Sek110237 			    secondary_cache_changed_cb, osi);
232*7237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
233*7237Sek110237 			if (err == 0)
234*7237Sek110237 				err = dsl_prop_register(ds, "checksum",
235*7237Sek110237 				    checksum_changed_cb, osi);
236*7237Sek110237 			if (err == 0)
237*7237Sek110237 				err = dsl_prop_register(ds, "compression",
238*7237Sek110237 				    compression_changed_cb, osi);
239*7237Sek110237 			if (err == 0)
240*7237Sek110237 				err = dsl_prop_register(ds, "copies",
241*7237Sek110237 				    copies_changed_cb, osi);
242*7237Sek110237 		}
2431544Seschrock 		if (err) {
2443547Smaybee 			VERIFY(arc_buf_remove_ref(osi->os_phys_buf,
2453547Smaybee 			    &osi->os_phys_buf) == 1);
2461544Seschrock 			kmem_free(osi, sizeof (objset_impl_t));
2471544Seschrock 			return (err);
2481544Seschrock 		}
2492082Seschrock 	} else if (ds == NULL) {
250789Sahrens 		/* It's the meta-objset. */
251789Sahrens 		osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
2521544Seschrock 		osi->os_compress = ZIO_COMPRESS_LZJB;
2533835Sahrens 		osi->os_copies = spa_max_replication(spa);
254*7237Sek110237 		osi->os_primary_cache = ZFS_CACHE_ALL;
255*7237Sek110237 		osi->os_secondary_cache = ZFS_CACHE_ALL;
256789Sahrens 	}
257789Sahrens 
2587046Sahrens 	osi->os_zil_header = osi->os_phys->os_zil_header;
2597046Sahrens 	osi->os_zil = zil_alloc(&osi->os, &osi->os_zil_header);
260789Sahrens 
261789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
262789Sahrens 		list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t),
263789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
264789Sahrens 		list_create(&osi->os_free_dnodes[i], sizeof (dnode_t),
265789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
266789Sahrens 	}
267789Sahrens 	list_create(&osi->os_dnodes, sizeof (dnode_t),
268789Sahrens 	    offsetof(dnode_t, dn_link));
269789Sahrens 	list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
270789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
271789Sahrens 
2722856Snd150628 	mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL);
2732856Snd150628 	mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
2745326Sek110237 	mutex_init(&osi->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
2752856Snd150628 
276789Sahrens 	osi->os_meta_dnode = dnode_special_open(osi,
277789Sahrens 	    &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
278789Sahrens 
2794787Sahrens 	/*
2804787Sahrens 	 * We should be the only thread trying to do this because we
2814787Sahrens 	 * have ds_opening_lock
2824787Sahrens 	 */
2834787Sahrens 	if (ds) {
2844787Sahrens 		VERIFY(NULL == dsl_dataset_set_user_ptr(ds, osi,
2854787Sahrens 		    dmu_objset_evict));
286789Sahrens 	}
287789Sahrens 
2881544Seschrock 	*osip = osi;
2891544Seschrock 	return (0);
290789Sahrens }
291789Sahrens 
2925367Sahrens static int
2935367Sahrens dmu_objset_open_ds_os(dsl_dataset_t *ds, objset_t *os, dmu_objset_type_t type)
2945367Sahrens {
2955367Sahrens 	objset_impl_t *osi;
2965367Sahrens 
2975367Sahrens 	mutex_enter(&ds->ds_opening_lock);
2985367Sahrens 	osi = dsl_dataset_get_user_ptr(ds);
2995367Sahrens 	if (osi == NULL) {
3006689Smaybee 		int err;
3016689Smaybee 
3025367Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
3035367Sahrens 		    ds, &ds->ds_phys->ds_bp, &osi);
3046689Smaybee 		if (err) {
3056689Smaybee 			mutex_exit(&ds->ds_opening_lock);
3065367Sahrens 			return (err);
3076689Smaybee 		}
3085367Sahrens 	}
3095367Sahrens 	mutex_exit(&ds->ds_opening_lock);
3105367Sahrens 
3115367Sahrens 	os->os = osi;
3126689Smaybee 	os->os_mode = DS_MODE_NOHOLD;
3135367Sahrens 
3145367Sahrens 	if (type != DMU_OST_ANY && type != os->os->os_phys->os_type)
3155367Sahrens 		return (EINVAL);
3165367Sahrens 	return (0);
3175367Sahrens }
3185367Sahrens 
3195367Sahrens int
3205367Sahrens dmu_objset_open_ds(dsl_dataset_t *ds, dmu_objset_type_t type, objset_t **osp)
3215367Sahrens {
3225367Sahrens 	objset_t *os;
3235367Sahrens 	int err;
3245367Sahrens 
3255367Sahrens 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
3265367Sahrens 	err = dmu_objset_open_ds_os(ds, os, type);
3275367Sahrens 	if (err)
3285367Sahrens 		kmem_free(os, sizeof (objset_t));
3295367Sahrens 	else
3305367Sahrens 		*osp = os;
3315367Sahrens 	return (err);
3325367Sahrens }
3335367Sahrens 
334789Sahrens /* called from zpl */
335789Sahrens int
336789Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode,
337789Sahrens     objset_t **osp)
338789Sahrens {
3395326Sek110237 	objset_t *os;
340789Sahrens 	dsl_dataset_t *ds;
341789Sahrens 	int err;
342789Sahrens 
3436689Smaybee 	ASSERT(DS_MODE_TYPE(mode) == DS_MODE_USER ||
3446689Smaybee 	    DS_MODE_TYPE(mode) == DS_MODE_OWNER);
3455367Sahrens 
346789Sahrens 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
3476689Smaybee 	if (DS_MODE_TYPE(mode) == DS_MODE_USER)
3486689Smaybee 		err = dsl_dataset_hold(name, os, &ds);
3496689Smaybee 	else
3506689Smaybee 		err = dsl_dataset_own(name, mode, os, &ds);
351789Sahrens 	if (err) {
352789Sahrens 		kmem_free(os, sizeof (objset_t));
353789Sahrens 		return (err);
354789Sahrens 	}
355789Sahrens 
3565367Sahrens 	err = dmu_objset_open_ds_os(ds, os, type);
3575367Sahrens 	if (err) {
3586689Smaybee 		if (DS_MODE_TYPE(mode) == DS_MODE_USER)
3596689Smaybee 			dsl_dataset_rele(ds, os);
3606689Smaybee 		else
3616689Smaybee 			dsl_dataset_disown(ds, os);
3625367Sahrens 		kmem_free(os, sizeof (objset_t));
3635367Sahrens 	} else {
3646689Smaybee 		os->os_mode = mode;
3655367Sahrens 		*osp = os;
366789Sahrens 	}
3675367Sahrens 	return (err);
368789Sahrens }
369789Sahrens 
370789Sahrens void
371789Sahrens dmu_objset_close(objset_t *os)
372789Sahrens {
3736689Smaybee 	ASSERT(DS_MODE_TYPE(os->os_mode) == DS_MODE_USER ||
3746689Smaybee 	    DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER ||
3756689Smaybee 	    DS_MODE_TYPE(os->os_mode) == DS_MODE_NOHOLD);
3766689Smaybee 
3776689Smaybee 	if (DS_MODE_TYPE(os->os_mode) == DS_MODE_USER)
3786689Smaybee 		dsl_dataset_rele(os->os->os_dsl_dataset, os);
3796689Smaybee 	else if (DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER)
3806689Smaybee 		dsl_dataset_disown(os->os->os_dsl_dataset, os);
381789Sahrens 	kmem_free(os, sizeof (objset_t));
382789Sahrens }
383789Sahrens 
3841646Sperrin int
3854944Smaybee dmu_objset_evict_dbufs(objset_t *os)
3861544Seschrock {
3871544Seschrock 	objset_impl_t *osi = os->os;
3881544Seschrock 	dnode_t *dn;
3891596Sahrens 
3901596Sahrens 	mutex_enter(&osi->os_lock);
3911596Sahrens 
3921596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
3931596Sahrens 	list_remove(&osi->os_dnodes, osi->os_meta_dnode);
3941596Sahrens 	list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode);
3951544Seschrock 
3961544Seschrock 	/*
3971596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
3981596Sahrens 	 * because dnode_add_ref() only works if you already have a
3991596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
4001596Sahrens 	 * skip.
4011544Seschrock 	 */
4021596Sahrens 	for (dn = list_head(&osi->os_dnodes);
4034944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
4041596Sahrens 	    dn = list_next(&osi->os_dnodes, dn))
4051596Sahrens 		continue;
4061596Sahrens 
4071596Sahrens 	while (dn) {
4081596Sahrens 		dnode_t *next_dn = dn;
4091596Sahrens 
4101596Sahrens 		do {
4111596Sahrens 			next_dn = list_next(&osi->os_dnodes, next_dn);
4124944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
4131596Sahrens 
4141596Sahrens 		mutex_exit(&osi->os_lock);
4154944Smaybee 		dnode_evict_dbufs(dn);
4161596Sahrens 		dnode_rele(dn, FTAG);
4171596Sahrens 		mutex_enter(&osi->os_lock);
4181596Sahrens 		dn = next_dn;
4191544Seschrock 	}
4201544Seschrock 	mutex_exit(&osi->os_lock);
4214944Smaybee 	return (list_head(&osi->os_dnodes) != osi->os_meta_dnode);
4221544Seschrock }
4231544Seschrock 
4241544Seschrock void
425789Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg)
426789Sahrens {
427789Sahrens 	objset_impl_t *osi = arg;
4281544Seschrock 	objset_t os;
4292082Seschrock 	int i;
430789Sahrens 
431789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
432789Sahrens 		ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL);
433789Sahrens 		ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL);
434789Sahrens 	}
435789Sahrens 
436*7237Sek110237 	if (ds) {
437*7237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
438*7237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
439*7237Sek110237 			    checksum_changed_cb, osi));
440*7237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
441*7237Sek110237 			    compression_changed_cb, osi));
442*7237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
443*7237Sek110237 			    copies_changed_cb, osi));
444*7237Sek110237 		}
445*7237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
446*7237Sek110237 		    primary_cache_changed_cb, osi));
447*7237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
448*7237Sek110237 		    secondary_cache_changed_cb, osi));
449789Sahrens 	}
450789Sahrens 
4511544Seschrock 	/*
4521544Seschrock 	 * We should need only a single pass over the dnode list, since
4531544Seschrock 	 * nothing can be added to the list at this point.
4541544Seschrock 	 */
4551544Seschrock 	os.os = osi;
4564944Smaybee 	(void) dmu_objset_evict_dbufs(&os);
4571544Seschrock 
458789Sahrens 	ASSERT3P(list_head(&osi->os_dnodes), ==, osi->os_meta_dnode);
459789Sahrens 	ASSERT3P(list_tail(&osi->os_dnodes), ==, osi->os_meta_dnode);
460789Sahrens 	ASSERT3P(list_head(&osi->os_meta_dnode->dn_dbufs), ==, NULL);
461789Sahrens 
462789Sahrens 	dnode_special_close(osi->os_meta_dnode);
463789Sahrens 	zil_free(osi->os_zil);
464789Sahrens 
4653547Smaybee 	VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1);
4662856Snd150628 	mutex_destroy(&osi->os_lock);
4672856Snd150628 	mutex_destroy(&osi->os_obj_lock);
4685326Sek110237 	mutex_destroy(&osi->os_user_ptr_lock);
469789Sahrens 	kmem_free(osi, sizeof (objset_impl_t));
470789Sahrens }
471789Sahrens 
472789Sahrens /* called from dsl for meta-objset */
473789Sahrens objset_impl_t *
4743547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
4753547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
476789Sahrens {
477789Sahrens 	objset_impl_t *osi;
478789Sahrens 	dnode_t *mdn;
479789Sahrens 
480789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
4814787Sahrens 	if (ds)
4824787Sahrens 		mutex_enter(&ds->ds_opening_lock);
4833547Smaybee 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi));
4844787Sahrens 	if (ds)
4854787Sahrens 		mutex_exit(&ds->ds_opening_lock);
486789Sahrens 	mdn = osi->os_meta_dnode;
487789Sahrens 
488789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
489789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
490789Sahrens 
491789Sahrens 	/*
492789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
493789Sahrens 	 * later, because then we could do it in quescing context while
494789Sahrens 	 * we are also accessing it in open context.
495789Sahrens 	 *
496789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
497789Sahrens 	 * because the MOS is only updated in syncing context.
498789Sahrens 	 * This is most fortunate: the MOS is the only objset that
499789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
500789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
501789Sahrens 	 */
5021544Seschrock 	if (ds != NULL) {
5031544Seschrock 		int levels = 1;
5041544Seschrock 
5051544Seschrock 		/*
5061544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
5071544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
5081544Seschrock 		 */
5091544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
5101544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
5111544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
5121544Seschrock 			levels++;
5131544Seschrock 
514789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
5151544Seschrock 		    mdn->dn_nlevels = levels;
5161544Seschrock 	}
517789Sahrens 
518789Sahrens 	ASSERT(type != DMU_OST_NONE);
519789Sahrens 	ASSERT(type != DMU_OST_ANY);
520789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
521789Sahrens 	osi->os_phys->os_type = type;
522789Sahrens 
523789Sahrens 	dsl_dataset_dirty(ds, tx);
524789Sahrens 
525789Sahrens 	return (osi);
526789Sahrens }
527789Sahrens 
528789Sahrens struct oscarg {
5294543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
530789Sahrens 	void *userarg;
531789Sahrens 	dsl_dataset_t *clone_parent;
532789Sahrens 	const char *lastname;
533789Sahrens 	dmu_objset_type_t type;
5346492Stimh 	uint64_t flags;
535789Sahrens };
536789Sahrens 
5374543Smarks /*ARGSUSED*/
538789Sahrens static int
5392199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
540789Sahrens {
5412199Sahrens 	dsl_dir_t *dd = arg1;
5422199Sahrens 	struct oscarg *oa = arg2;
5432199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
5442199Sahrens 	int err;
5452199Sahrens 	uint64_t ddobj;
5462199Sahrens 
5472199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
5482199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
5492199Sahrens 	if (err != ENOENT)
5502199Sahrens 		return (err ? err : EEXIST);
5512199Sahrens 
5522199Sahrens 	if (oa->clone_parent != NULL) {
5532199Sahrens 		/*
5542199Sahrens 		 * You can't clone across pools.
5552199Sahrens 		 */
5562199Sahrens 		if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool)
5572199Sahrens 			return (EXDEV);
5582199Sahrens 
5592199Sahrens 		/*
5602199Sahrens 		 * You can only clone snapshots, not the head datasets.
5612199Sahrens 		 */
5622199Sahrens 		if (oa->clone_parent->ds_phys->ds_num_children == 0)
5632199Sahrens 			return (EINVAL);
5642199Sahrens 	}
5654543Smarks 
5662199Sahrens 	return (0);
5672199Sahrens }
5682199Sahrens 
5692199Sahrens static void
5704543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
5712199Sahrens {
5722199Sahrens 	dsl_dir_t *dd = arg1;
5732199Sahrens 	struct oscarg *oa = arg2;
574789Sahrens 	dsl_dataset_t *ds;
5753547Smaybee 	blkptr_t *bp;
5762199Sahrens 	uint64_t dsobj;
577789Sahrens 
578789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
579789Sahrens 
5802199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
5816492Stimh 	    oa->clone_parent, oa->flags, cr, tx);
582789Sahrens 
5836689Smaybee 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, FTAG, &ds));
5843547Smaybee 	bp = dsl_dataset_get_blkptr(ds);
5853547Smaybee 	if (BP_IS_HOLE(bp)) {
586789Sahrens 		objset_impl_t *osi;
587789Sahrens 
588789Sahrens 		/* This is an empty dmu_objset; not a clone. */
589789Sahrens 		osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
5903547Smaybee 		    ds, bp, oa->type, tx);
591789Sahrens 
592789Sahrens 		if (oa->userfunc)
5934543Smarks 			oa->userfunc(&osi->os, oa->userarg, cr, tx);
594789Sahrens 	}
5954543Smarks 
5964543Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
5974543Smarks 	    tx, cr, "dataset = %llu", dsobj);
5984543Smarks 
5996689Smaybee 	dsl_dataset_rele(ds, FTAG);
600789Sahrens }
601789Sahrens 
602789Sahrens int
603789Sahrens dmu_objset_create(const char *name, dmu_objset_type_t type,
6046492Stimh     objset_t *clone_parent, uint64_t flags,
6054543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
606789Sahrens {
6072199Sahrens 	dsl_dir_t *pdd;
608789Sahrens 	const char *tail;
609789Sahrens 	int err = 0;
6102199Sahrens 	struct oscarg oa = { 0 };
611789Sahrens 
6122199Sahrens 	ASSERT(strchr(name, '@') == NULL);
6132199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
6141544Seschrock 	if (err)
6151544Seschrock 		return (err);
616789Sahrens 	if (tail == NULL) {
6172199Sahrens 		dsl_dir_close(pdd, FTAG);
618789Sahrens 		return (EEXIST);
619789Sahrens 	}
620789Sahrens 
621789Sahrens 	dprintf("name=%s\n", name);
622789Sahrens 
6232199Sahrens 	oa.userfunc = func;
6242199Sahrens 	oa.userarg = arg;
6252199Sahrens 	oa.lastname = tail;
6262199Sahrens 	oa.type = type;
6276492Stimh 	oa.flags = flags;
6284543Smarks 
6292199Sahrens 	if (clone_parent != NULL) {
630789Sahrens 		/*
6312199Sahrens 		 * You can't clone to a different type.
632789Sahrens 		 */
6332199Sahrens 		if (clone_parent->os->os_phys->os_type != type) {
6342199Sahrens 			dsl_dir_close(pdd, FTAG);
6352199Sahrens 			return (EINVAL);
636789Sahrens 		}
6372199Sahrens 		oa.clone_parent = clone_parent->os->os_dsl_dataset;
638789Sahrens 	}
6392199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
6402199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
6412199Sahrens 	dsl_dir_close(pdd, FTAG);
642789Sahrens 	return (err);
643789Sahrens }
644789Sahrens 
645789Sahrens int
646789Sahrens dmu_objset_destroy(const char *name)
647789Sahrens {
648789Sahrens 	objset_t *os;
649789Sahrens 	int error;
650789Sahrens 
651789Sahrens 	/*
652789Sahrens 	 * If it looks like we'll be able to destroy it, and there's
653789Sahrens 	 * an unplayed replay log sitting around, destroy the log.
654789Sahrens 	 * It would be nicer to do this in dsl_dataset_destroy_sync(),
655789Sahrens 	 * but the replay log objset is modified in open context.
656789Sahrens 	 */
6575367Sahrens 	error = dmu_objset_open(name, DMU_OST_ANY,
6586689Smaybee 	    DS_MODE_OWNER|DS_MODE_READONLY|DS_MODE_INCONSISTENT, &os);
659789Sahrens 	if (error == 0) {
6605367Sahrens 		dsl_dataset_t *ds = os->os->os_dsl_dataset;
6611807Sbonwick 		zil_destroy(dmu_objset_zil(os), B_FALSE);
6625367Sahrens 
6636689Smaybee 		error = dsl_dataset_destroy(ds, os);
6645367Sahrens 		/*
6655367Sahrens 		 * dsl_dataset_destroy() closes the ds.
6665367Sahrens 		 */
6675367Sahrens 		kmem_free(os, sizeof (objset_t));
668789Sahrens 	}
669789Sahrens 
6705367Sahrens 	return (error);
671789Sahrens }
672789Sahrens 
6735446Sahrens /*
6745446Sahrens  * This will close the objset.
6755446Sahrens  */
676789Sahrens int
6775446Sahrens dmu_objset_rollback(objset_t *os)
678789Sahrens {
679789Sahrens 	int err;
6805367Sahrens 	dsl_dataset_t *ds;
681789Sahrens 
6825446Sahrens 	ds = os->os->os_dsl_dataset;
6834935Sperrin 
6846689Smaybee 	if (!dsl_dataset_tryown(ds, TRUE, os)) {
6855446Sahrens 		dmu_objset_close(os);
6865446Sahrens 		return (EBUSY);
6875446Sahrens 	}
6885446Sahrens 
6895367Sahrens 	err = dsl_dataset_rollback(ds, os->os->os_phys->os_type);
6904935Sperrin 
6915367Sahrens 	/*
6925367Sahrens 	 * NB: we close the objset manually because the rollback
6935367Sahrens 	 * actually implicitly called dmu_objset_evict(), thus freeing
6945367Sahrens 	 * the objset_impl_t.
6955367Sahrens 	 */
6966689Smaybee 	dsl_dataset_disown(ds, os);
6975367Sahrens 	kmem_free(os, sizeof (objset_t));
698789Sahrens 	return (err);
699789Sahrens }
700789Sahrens 
7012199Sahrens struct snaparg {
7022199Sahrens 	dsl_sync_task_group_t *dstg;
7032199Sahrens 	char *snapname;
7042199Sahrens 	char failed[MAXPATHLEN];
7054543Smarks 	boolean_t checkperms;
7065367Sahrens 	list_t objsets;
7075367Sahrens };
7085367Sahrens 
7095367Sahrens struct osnode {
7105367Sahrens 	list_node_t node;
7115367Sahrens 	objset_t *os;
7122199Sahrens };
7132199Sahrens 
7142199Sahrens static int
7152199Sahrens dmu_objset_snapshot_one(char *name, void *arg)
7162199Sahrens {
7172199Sahrens 	struct snaparg *sn = arg;
7182199Sahrens 	objset_t *os;
7192199Sahrens 	int err;
7202199Sahrens 
7212199Sahrens 	(void) strcpy(sn->failed, name);
7222199Sahrens 
7234543Smarks 	/*
7244543Smarks 	 * Check permissions only when requested.  This only applies when
7254543Smarks 	 * doing a recursive snapshot.  The permission checks for the starting
7264543Smarks 	 * dataset have already been performed in zfs_secpolicy_snapshot()
7274543Smarks 	 */
7284543Smarks 	if (sn->checkperms == B_TRUE &&
7294543Smarks 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
7304543Smarks 		return (err);
7314543Smarks 
7326689Smaybee 	err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_USER, &os);
7332199Sahrens 	if (err != 0)
7342199Sahrens 		return (err);
7352199Sahrens 
7366689Smaybee 	/* If the objset is in an inconsistent state, return busy */
7376689Smaybee 	if (os->os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
7383637Srm160521 		dmu_objset_close(os);
7393637Srm160521 		return (EBUSY);
7403637Srm160521 	}
7413637Srm160521 
7423637Srm160521 	/*
7432199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
7442199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
7452199Sahrens 	 * a side effect.
7462199Sahrens 	 */
7472199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
7482199Sahrens 	if (err == 0) {
7495367Sahrens 		struct osnode *osn;
7502199Sahrens 		dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check,
7515367Sahrens 		    dsl_dataset_snapshot_sync, os->os->os_dsl_dataset,
7525367Sahrens 		    sn->snapname, 3);
7535367Sahrens 		osn = kmem_alloc(sizeof (struct osnode), KM_SLEEP);
7545367Sahrens 		osn->os = os;
7555367Sahrens 		list_insert_tail(&sn->objsets, osn);
7563637Srm160521 	} else {
7573637Srm160521 		dmu_objset_close(os);
7582199Sahrens 	}
7593637Srm160521 
7602199Sahrens 	return (err);
7612199Sahrens }
7622199Sahrens 
7632199Sahrens int
7642199Sahrens dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive)
7652199Sahrens {
7662199Sahrens 	dsl_sync_task_t *dst;
7675367Sahrens 	struct osnode *osn;
7682199Sahrens 	struct snaparg sn = { 0 };
7692199Sahrens 	spa_t *spa;
7702199Sahrens 	int err;
7712199Sahrens 
7722199Sahrens 	(void) strcpy(sn.failed, fsname);
7732199Sahrens 
7744603Sahrens 	err = spa_open(fsname, &spa, FTAG);
7752199Sahrens 	if (err)
7762199Sahrens 		return (err);
7772199Sahrens 
7782199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
7792199Sahrens 	sn.snapname = snapname;
7805367Sahrens 	list_create(&sn.objsets, sizeof (struct osnode),
7815367Sahrens 	    offsetof(struct osnode, node));
7822199Sahrens 
7832417Sahrens 	if (recursive) {
7844543Smarks 		sn.checkperms = B_TRUE;
7852417Sahrens 		err = dmu_objset_find(fsname,
7862417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
7872417Sahrens 	} else {
7884543Smarks 		sn.checkperms = B_FALSE;
7892199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
7902417Sahrens 	}
7912199Sahrens 
7922199Sahrens 	if (err)
7932199Sahrens 		goto out;
7942199Sahrens 
7952199Sahrens 	err = dsl_sync_task_group_wait(sn.dstg);
7962199Sahrens 
7972199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
7982199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
7995367Sahrens 		dsl_dataset_t *ds = dst->dst_arg1;
8002199Sahrens 		if (dst->dst_err)
8015367Sahrens 			dsl_dataset_name(ds, sn.failed);
8022199Sahrens 	}
8035367Sahrens 
8046174Sahrens out:
8055367Sahrens 	while (osn = list_head(&sn.objsets)) {
8065367Sahrens 		list_remove(&sn.objsets, osn);
8075367Sahrens 		zil_resume(dmu_objset_zil(osn->os));
8085367Sahrens 		dmu_objset_close(osn->os);
8095367Sahrens 		kmem_free(osn, sizeof (struct osnode));
8105367Sahrens 	}
8115367Sahrens 	list_destroy(&sn.objsets);
8126174Sahrens 
8132199Sahrens 	if (err)
8142199Sahrens 		(void) strcpy(fsname, sn.failed);
8152199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
8162199Sahrens 	spa_close(spa, FTAG);
8172199Sahrens 	return (err);
8182199Sahrens }
8192199Sahrens 
820789Sahrens static void
8213547Smaybee dmu_objset_sync_dnodes(list_t *list, dmu_tx_t *tx)
822789Sahrens {
8233547Smaybee 	dnode_t *dn;
824789Sahrens 
8253547Smaybee 	while (dn = list_head(list)) {
8263547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
8273547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
8283547Smaybee 		/*
8293547Smaybee 		 * Initialize dn_zio outside dnode_sync()
8303547Smaybee 		 * to accomodate meta-dnode
8313547Smaybee 		 */
8323547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
8333547Smaybee 		ASSERT(dn->dn_zio);
834789Sahrens 
8353547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
8363547Smaybee 		list_remove(list, dn);
8373547Smaybee 		dnode_sync(dn, tx);
8383547Smaybee 	}
8393547Smaybee }
8402981Sahrens 
8413547Smaybee /* ARGSUSED */
8423547Smaybee static void
8433547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg)
8443547Smaybee {
8453547Smaybee 	objset_impl_t *os = arg;
8463547Smaybee 	blkptr_t *bp = os->os_rootbp;
8473547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
8483547Smaybee 	int i;
8492981Sahrens 
8505329Sgw25295 	ASSERT(bp == zio->io_bp);
8515329Sgw25295 
8523547Smaybee 	/*
8533547Smaybee 	 * Update rootbp fill count.
8543547Smaybee 	 */
8553547Smaybee 	bp->blk_fill = 1;	/* count the meta-dnode */
8563547Smaybee 	for (i = 0; i < dnp->dn_nblkptr; i++)
8573547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
8585329Sgw25295 
8595329Sgw25295 	BP_SET_TYPE(bp, DMU_OT_OBJSET);
8605329Sgw25295 	BP_SET_LEVEL(bp, 0);
8615329Sgw25295 
8625329Sgw25295 	/* We must do this after we've set the bp's type and level */
8635329Sgw25295 	if (!DVA_EQUAL(BP_IDENTITY(bp),
8645329Sgw25295 	    BP_IDENTITY(&zio->io_bp_orig))) {
8655329Sgw25295 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
8666992Smaybee 			(void) dsl_dataset_block_kill(os->os_dsl_dataset,
8675329Sgw25295 			    &zio->io_bp_orig, NULL, os->os_synctx);
8685329Sgw25295 		dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx);
8695329Sgw25295 	}
870789Sahrens }
871789Sahrens 
872789Sahrens /* called from dsl */
873789Sahrens void
8743547Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx)
875789Sahrens {
876789Sahrens 	int txgoff;
8771544Seschrock 	zbookmark_t zb;
8787046Sahrens 	writeprops_t wp = { 0 };
8793547Smaybee 	zio_t *zio;
8803547Smaybee 	list_t *list;
8813547Smaybee 	dbuf_dirty_record_t *dr;
8823547Smaybee 
8833547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
884789Sahrens 
885789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
886789Sahrens 	/* XXX the write_done callback should really give us the tx... */
887789Sahrens 	os->os_synctx = tx;
888789Sahrens 
8893882Sahrens 	if (os->os_dsl_dataset == NULL) {
8903882Sahrens 		/*
8913882Sahrens 		 * This is the MOS.  If we have upgraded,
8923882Sahrens 		 * spa_max_replication() could change, so reset
8933882Sahrens 		 * os_copies here.
8943882Sahrens 		 */
8953882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
8963882Sahrens 	}
8973882Sahrens 
8983547Smaybee 	/*
8993547Smaybee 	 * Create the root block IO
9003547Smaybee 	 */
9013547Smaybee 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
9023547Smaybee 	zb.zb_object = 0;
9033547Smaybee 	zb.zb_level = -1;
9043547Smaybee 	zb.zb_blkid = 0;
9054787Sahrens 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
9066992Smaybee 		(void) dsl_dataset_block_kill(os->os_dsl_dataset,
9073547Smaybee 		    os->os_rootbp, pio, tx);
9084787Sahrens 	}
9097046Sahrens 	wp.wp_type = DMU_OT_OBJSET;
9107046Sahrens 	wp.wp_copies = os->os_copies;
9117046Sahrens 	wp.wp_level = (uint8_t)-1;
9127046Sahrens 	wp.wp_oschecksum = os->os_checksum;
9137046Sahrens 	wp.wp_oscompress = os->os_compress;
9147046Sahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
9157046Sahrens 	zio = arc_write(pio, os->os_spa, &wp,
916*7237Sek110237 	    DMU_OS_IS_L2CACHEABLE(os), tx->tx_txg, os->os_rootbp,
917*7237Sek110237 	    os->os_phys_buf, ready, NULL, os, ZIO_PRIORITY_ASYNC_WRITE,
918*7237Sek110237 	    ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_METADATA, &zb);
9193547Smaybee 
9203547Smaybee 	/*
9213547Smaybee 	 * Sync meta-dnode - the parent IO for the sync is the root block
9223547Smaybee 	 */
9233547Smaybee 	os->os_meta_dnode->dn_zio = zio;
9243547Smaybee 	dnode_sync(os->os_meta_dnode, tx);
925789Sahrens 
926789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
927789Sahrens 
9283547Smaybee 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], tx);
9293547Smaybee 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], tx);
930789Sahrens 
9313547Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
9323547Smaybee 	while (dr = list_head(list)) {
9333547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
9343547Smaybee 		list_remove(list, dr);
9353547Smaybee 		if (dr->dr_zio)
9363547Smaybee 			zio_nowait(dr->dr_zio);
9373547Smaybee 	}
938789Sahrens 	/*
939789Sahrens 	 * Free intent log blocks up to this tx.
940789Sahrens 	 */
941789Sahrens 	zil_sync(os->os_zil, tx);
9427046Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
9433547Smaybee 	zio_nowait(zio);
944789Sahrens }
945789Sahrens 
946789Sahrens void
9472885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
9482885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
9492885Sahrens {
9502885Sahrens 	dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp,
9512885Sahrens 	    usedobjsp, availobjsp);
9522885Sahrens }
9532885Sahrens 
9542885Sahrens uint64_t
9552885Sahrens dmu_objset_fsid_guid(objset_t *os)
9562885Sahrens {
9572885Sahrens 	return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset));
9582885Sahrens }
9592885Sahrens 
9602885Sahrens void
9612885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
962789Sahrens {
9632885Sahrens 	stat->dds_type = os->os->os_phys->os_type;
9642885Sahrens 	if (os->os->os_dsl_dataset)
9652885Sahrens 		dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat);
9662885Sahrens }
9672885Sahrens 
9682885Sahrens void
9692885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
9702885Sahrens {
9712885Sahrens 	ASSERT(os->os->os_dsl_dataset ||
9722885Sahrens 	    os->os->os_phys->os_type == DMU_OST_META);
9732885Sahrens 
9742885Sahrens 	if (os->os->os_dsl_dataset != NULL)
9752885Sahrens 		dsl_dataset_stats(os->os->os_dsl_dataset, nv);
9762885Sahrens 
9772885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
9782885Sahrens 	    os->os->os_phys->os_type);
979789Sahrens }
980789Sahrens 
981789Sahrens int
982789Sahrens dmu_objset_is_snapshot(objset_t *os)
983789Sahrens {
984789Sahrens 	if (os->os->os_dsl_dataset != NULL)
985789Sahrens 		return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset));
986789Sahrens 	else
987789Sahrens 		return (B_FALSE);
988789Sahrens }
989789Sahrens 
990789Sahrens int
9916492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
9926492Stimh     boolean_t *conflict)
9936492Stimh {
9946492Stimh 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
9956492Stimh 	uint64_t ignored;
9966492Stimh 
9976492Stimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
9986492Stimh 		return (ENOENT);
9996492Stimh 
10006492Stimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
10016492Stimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
10026492Stimh 	    real, maxlen, conflict));
10036492Stimh }
10046492Stimh 
10056492Stimh int
1006789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
10075663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1008789Sahrens {
1009789Sahrens 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
1010789Sahrens 	zap_cursor_t cursor;
1011789Sahrens 	zap_attribute_t attr;
1012789Sahrens 
1013789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1014789Sahrens 		return (ENOENT);
1015789Sahrens 
1016789Sahrens 	zap_cursor_init_serialized(&cursor,
1017789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1018789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1019789Sahrens 
1020885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1021885Sahrens 		zap_cursor_fini(&cursor);
1022885Sahrens 		return (ENOENT);
1023885Sahrens 	}
1024885Sahrens 
1025885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1026885Sahrens 		zap_cursor_fini(&cursor);
1027885Sahrens 		return (ENAMETOOLONG);
1028885Sahrens 	}
1029885Sahrens 
1030885Sahrens 	(void) strcpy(name, attr.za_name);
1031885Sahrens 	if (idp)
1032885Sahrens 		*idp = attr.za_first_integer;
10335663Sck153898 	if (case_conflict)
10345663Sck153898 		*case_conflict = attr.za_normalization_conflict;
1035885Sahrens 	zap_cursor_advance(&cursor);
1036885Sahrens 	*offp = zap_cursor_serialize(&cursor);
1037885Sahrens 	zap_cursor_fini(&cursor);
1038885Sahrens 
1039885Sahrens 	return (0);
1040885Sahrens }
1041885Sahrens 
1042885Sahrens int
1043885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
1044885Sahrens     uint64_t *idp, uint64_t *offp)
1045885Sahrens {
1046885Sahrens 	dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir;
1047885Sahrens 	zap_cursor_t cursor;
1048885Sahrens 	zap_attribute_t attr;
1049885Sahrens 
1050885Sahrens 	/* there is no next dir on a snapshot! */
1051885Sahrens 	if (os->os->os_dsl_dataset->ds_object !=
1052885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
1053885Sahrens 		return (ENOENT);
1054885Sahrens 
1055885Sahrens 	zap_cursor_init_serialized(&cursor,
1056885Sahrens 	    dd->dd_pool->dp_meta_objset,
1057885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1058885Sahrens 
1059885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1060885Sahrens 		zap_cursor_fini(&cursor);
1061885Sahrens 		return (ENOENT);
1062885Sahrens 	}
1063885Sahrens 
1064885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1065885Sahrens 		zap_cursor_fini(&cursor);
1066789Sahrens 		return (ENAMETOOLONG);
1067885Sahrens 	}
1068789Sahrens 
1069789Sahrens 	(void) strcpy(name, attr.za_name);
1070885Sahrens 	if (idp)
1071885Sahrens 		*idp = attr.za_first_integer;
1072789Sahrens 	zap_cursor_advance(&cursor);
1073789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1074885Sahrens 	zap_cursor_fini(&cursor);
1075789Sahrens 
1076789Sahrens 	return (0);
1077789Sahrens }
1078789Sahrens 
10797046Sahrens struct findarg {
10807046Sahrens 	int (*func)(char *, void *);
10817046Sahrens 	void *arg;
10827046Sahrens };
10837046Sahrens 
10847046Sahrens /* ARGSUSED */
10857046Sahrens static int
10867046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
10877046Sahrens {
10887046Sahrens 	struct findarg *fa = arg;
10897046Sahrens 	return (fa->func((char *)dsname, fa->arg));
10907046Sahrens }
10917046Sahrens 
1092789Sahrens /*
1093789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
10947046Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1095789Sahrens  */
10962199Sahrens int
10972199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1098789Sahrens {
10997046Sahrens 	struct findarg fa;
11007046Sahrens 	fa.func = func;
11017046Sahrens 	fa.arg = arg;
11027046Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
11037046Sahrens }
11047046Sahrens 
11057046Sahrens /*
11067046Sahrens  * Find all objsets under name, call func on each
11077046Sahrens  */
11087046Sahrens int
11097046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
11107046Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
11117046Sahrens {
1112789Sahrens 	dsl_dir_t *dd;
11137046Sahrens 	dsl_pool_t *dp;
11147046Sahrens 	dsl_dataset_t *ds;
1115789Sahrens 	zap_cursor_t zc;
11163978Smmusante 	zap_attribute_t *attr;
1117789Sahrens 	char *child;
11187046Sahrens 	uint64_t thisobj;
11197046Sahrens 	int err;
1120789Sahrens 
11217046Sahrens 	if (name == NULL)
11227046Sahrens 		name = spa_name(spa);
11237046Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
11241544Seschrock 	if (err)
11252199Sahrens 		return (err);
1126789Sahrens 
11277046Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
11287046Sahrens 	if (dd->dd_myname[0] == '$') {
11297046Sahrens 		dsl_dir_close(dd, FTAG);
11307046Sahrens 		return (0);
11317046Sahrens 	}
11327046Sahrens 
11337046Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
11343978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
11357046Sahrens 	dp = dd->dd_pool;
1136789Sahrens 
1137789Sahrens 	/*
1138789Sahrens 	 * Iterate over all children.
1139789Sahrens 	 */
11402417Sahrens 	if (flags & DS_FIND_CHILDREN) {
11417046Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
11422417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
11433978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
11442417Sahrens 		    (void) zap_cursor_advance(&zc)) {
11453978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
11463978Smmusante 			ASSERT(attr->za_num_integers == 1);
1147789Sahrens 
11482417Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
11497046Sahrens 			(void) strcpy(child, name);
11502417Sahrens 			(void) strcat(child, "/");
11513978Smmusante 			(void) strcat(child, attr->za_name);
11527046Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
11532417Sahrens 			kmem_free(child, MAXPATHLEN);
11542417Sahrens 			if (err)
11552417Sahrens 				break;
11562417Sahrens 		}
11572417Sahrens 		zap_cursor_fini(&zc);
11582199Sahrens 
11592417Sahrens 		if (err) {
11602417Sahrens 			dsl_dir_close(dd, FTAG);
11613978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
11622417Sahrens 			return (err);
11632417Sahrens 		}
1164789Sahrens 	}
1165789Sahrens 
1166789Sahrens 	/*
1167789Sahrens 	 * Iterate over all snapshots.
1168789Sahrens 	 */
11697046Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
11707046Sahrens 		if (!dsl_pool_sync_context(dp))
11717046Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
11727046Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
11737046Sahrens 		if (!dsl_pool_sync_context(dp))
11747046Sahrens 			rw_exit(&dp->dp_config_rwlock);
1175789Sahrens 
11767046Sahrens 		if (err == 0) {
11777046Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
11787046Sahrens 			dsl_dataset_rele(ds, FTAG);
1179789Sahrens 
11807046Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
11817046Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
11827046Sahrens 			    (void) zap_cursor_advance(&zc)) {
11837046Sahrens 				ASSERT(attr->za_integer_length ==
11847046Sahrens 				    sizeof (uint64_t));
11857046Sahrens 				ASSERT(attr->za_num_integers == 1);
1186789Sahrens 
11877046Sahrens 				child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
11887046Sahrens 				(void) strcpy(child, name);
11897046Sahrens 				(void) strcat(child, "@");
11907046Sahrens 				(void) strcat(child, attr->za_name);
11917046Sahrens 				err = func(spa, attr->za_first_integer,
11927046Sahrens 				    child, arg);
11937046Sahrens 				kmem_free(child, MAXPATHLEN);
11947046Sahrens 				if (err)
11957046Sahrens 					break;
11967046Sahrens 			}
11977046Sahrens 			zap_cursor_fini(&zc);
1198789Sahrens 		}
1199789Sahrens 	}
1200789Sahrens 
1201789Sahrens 	dsl_dir_close(dd, FTAG);
12023978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1203789Sahrens 
12042199Sahrens 	if (err)
12052199Sahrens 		return (err);
12062199Sahrens 
1207789Sahrens 	/*
1208789Sahrens 	 * Apply to self if appropriate.
1209789Sahrens 	 */
12107046Sahrens 	err = func(spa, thisobj, name, arg);
12112199Sahrens 	return (err);
1212789Sahrens }
12135326Sek110237 
12145326Sek110237 void
12155326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
12165326Sek110237 {
12175326Sek110237 	ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
12185326Sek110237 	os->os->os_user_ptr = user_ptr;
12195326Sek110237 }
12205326Sek110237 
12215326Sek110237 void *
12225326Sek110237 dmu_objset_get_user(objset_t *os)
12235326Sek110237 {
12245326Sek110237 	ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
12255326Sek110237 	return (os->os->os_user_ptr);
12265326Sek110237 }
1227