xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 9396:f41cf682d0d3)
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 /*
229355SMatthew.Ahrens@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
264543Smarks #include <sys/cred.h>
27789Sahrens #include <sys/zfs_context.h>
28789Sahrens #include <sys/dmu_objset.h>
29789Sahrens #include <sys/dsl_dir.h>
30789Sahrens #include <sys/dsl_dataset.h>
31789Sahrens #include <sys/dsl_prop.h>
32789Sahrens #include <sys/dsl_pool.h>
332199Sahrens #include <sys/dsl_synctask.h>
344543Smarks #include <sys/dsl_deleg.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>
434543Smarks #include <sys/zfs_ioctl.h>
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 
1347237Sek110237 static void
1357237Sek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
1367237Sek110237 {
1377237Sek110237 	objset_impl_t *osi = arg;
1387237Sek110237 
1397237Sek110237 	/*
1407237Sek110237 	 * Inheritance and range checking should have been done by now.
1417237Sek110237 	 */
1427237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1437237Sek110237 	    newval == ZFS_CACHE_METADATA);
1447237Sek110237 
1457237Sek110237 	osi->os_primary_cache = newval;
1467237Sek110237 }
1477237Sek110237 
1487237Sek110237 static void
1497237Sek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
1507237Sek110237 {
1517237Sek110237 	objset_impl_t *osi = arg;
1527237Sek110237 
1537237Sek110237 	/*
1547237Sek110237 	 * Inheritance and range checking should have been done by now.
1557237Sek110237 	 */
1567237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1577237Sek110237 	    newval == ZFS_CACHE_METADATA);
1587237Sek110237 
1597237Sek110237 	osi->os_secondary_cache = newval;
1607237Sek110237 }
1617237Sek110237 
162789Sahrens void
163789Sahrens dmu_objset_byteswap(void *buf, size_t size)
164789Sahrens {
165789Sahrens 	objset_phys_t *osp = buf;
166789Sahrens 
167*9396SMatthew.Ahrens@Sun.COM 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
168789Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
169789Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
170789Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
171*9396SMatthew.Ahrens@Sun.COM 	osp->os_flags = BSWAP_64(osp->os_flags);
172*9396SMatthew.Ahrens@Sun.COM 	if (size == sizeof (objset_phys_t)) {
173*9396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_userused_dnode);
174*9396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_groupused_dnode);
175*9396SMatthew.Ahrens@Sun.COM 	}
176789Sahrens }
177789Sahrens 
1781544Seschrock int
1791544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
1801544Seschrock     objset_impl_t **osip)
181789Sahrens {
1824787Sahrens 	objset_impl_t *osi;
1837046Sahrens 	int i, err;
184789Sahrens 
1854787Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
1864787Sahrens 
187789Sahrens 	osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP);
188789Sahrens 	osi->os.os = osi;
189789Sahrens 	osi->os_dsl_dataset = ds;
190789Sahrens 	osi->os_spa = spa;
1913547Smaybee 	osi->os_rootbp = bp;
1923547Smaybee 	if (!BP_IS_HOLE(osi->os_rootbp)) {
1932391Smaybee 		uint32_t aflags = ARC_WAIT;
1941544Seschrock 		zbookmark_t zb;
1951544Seschrock 		zb.zb_objset = ds ? ds->ds_object : 0;
1961544Seschrock 		zb.zb_object = 0;
1971544Seschrock 		zb.zb_level = -1;
1981544Seschrock 		zb.zb_blkid = 0;
1997237Sek110237 		if (DMU_OS_IS_L2CACHEABLE(osi))
2007237Sek110237 			aflags |= ARC_L2CACHE;
2011544Seschrock 
2023547Smaybee 		dprintf_bp(osi->os_rootbp, "reading %s", "");
2037046Sahrens 		/*
2047046Sahrens 		 * NB: when bprewrite scrub can change the bp,
2057046Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
2067046Sahrens 		 * could change, and we'll need a lock.
2077046Sahrens 		 */
2087046Sahrens 		err = arc_read_nolock(NULL, spa, osi->os_rootbp,
2093547Smaybee 		    arc_getbuf_func, &osi->os_phys_buf,
2102391Smaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
2111544Seschrock 		if (err) {
2121544Seschrock 			kmem_free(osi, sizeof (objset_impl_t));
2137294Sperrin 			/* convert checksum errors into IO errors */
2147294Sperrin 			if (err == ECKSUM)
2157294Sperrin 				err = EIO;
2161544Seschrock 			return (err);
2171544Seschrock 		}
218*9396SMatthew.Ahrens@Sun.COM 
219*9396SMatthew.Ahrens@Sun.COM 		/* Increase the blocksize if we are permitted. */
220*9396SMatthew.Ahrens@Sun.COM 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
221*9396SMatthew.Ahrens@Sun.COM 		    arc_buf_size(osi->os_phys_buf) < sizeof (objset_phys_t)) {
222*9396SMatthew.Ahrens@Sun.COM 			arc_buf_t *buf = arc_buf_alloc(spa,
223*9396SMatthew.Ahrens@Sun.COM 			    sizeof (objset_phys_t), &osi->os_phys_buf,
224*9396SMatthew.Ahrens@Sun.COM 			    ARC_BUFC_METADATA);
225*9396SMatthew.Ahrens@Sun.COM 			bzero(buf->b_data, sizeof (objset_phys_t));
226*9396SMatthew.Ahrens@Sun.COM 			bcopy(osi->os_phys_buf->b_data, buf->b_data,
227*9396SMatthew.Ahrens@Sun.COM 			    arc_buf_size(osi->os_phys_buf));
228*9396SMatthew.Ahrens@Sun.COM 			arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf);
229*9396SMatthew.Ahrens@Sun.COM 			osi->os_phys_buf = buf;
230*9396SMatthew.Ahrens@Sun.COM 		}
231*9396SMatthew.Ahrens@Sun.COM 
2323547Smaybee 		osi->os_phys = osi->os_phys_buf->b_data;
233*9396SMatthew.Ahrens@Sun.COM 		osi->os_flags = osi->os_phys->os_flags;
234789Sahrens 	} else {
235*9396SMatthew.Ahrens@Sun.COM 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
236*9396SMatthew.Ahrens@Sun.COM 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
237*9396SMatthew.Ahrens@Sun.COM 		osi->os_phys_buf = arc_buf_alloc(spa, size,
2383547Smaybee 		    &osi->os_phys_buf, ARC_BUFC_METADATA);
2393547Smaybee 		osi->os_phys = osi->os_phys_buf->b_data;
240*9396SMatthew.Ahrens@Sun.COM 		bzero(osi->os_phys, size);
241789Sahrens 	}
242789Sahrens 
243789Sahrens 	/*
244789Sahrens 	 * Note: the changed_cb will be called once before the register
245789Sahrens 	 * func returns, thus changing the checksum/compression from the
2467237Sek110237 	 * default (fletcher2/off).  Snapshots don't need to know about
2477237Sek110237 	 * checksum/compression/copies.
248789Sahrens 	 */
2497237Sek110237 	if (ds) {
2507237Sek110237 		err = dsl_prop_register(ds, "primarycache",
2517237Sek110237 		    primary_cache_changed_cb, osi);
2521544Seschrock 		if (err == 0)
2537237Sek110237 			err = dsl_prop_register(ds, "secondarycache",
2547237Sek110237 			    secondary_cache_changed_cb, osi);
2557237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
2567237Sek110237 			if (err == 0)
2577237Sek110237 				err = dsl_prop_register(ds, "checksum",
2587237Sek110237 				    checksum_changed_cb, osi);
2597237Sek110237 			if (err == 0)
2607237Sek110237 				err = dsl_prop_register(ds, "compression",
2617237Sek110237 				    compression_changed_cb, osi);
2627237Sek110237 			if (err == 0)
2637237Sek110237 				err = dsl_prop_register(ds, "copies",
2647237Sek110237 				    copies_changed_cb, osi);
2657237Sek110237 		}
2661544Seschrock 		if (err) {
2673547Smaybee 			VERIFY(arc_buf_remove_ref(osi->os_phys_buf,
2683547Smaybee 			    &osi->os_phys_buf) == 1);
2691544Seschrock 			kmem_free(osi, sizeof (objset_impl_t));
2701544Seschrock 			return (err);
2711544Seschrock 		}
2722082Seschrock 	} else if (ds == NULL) {
273789Sahrens 		/* It's the meta-objset. */
274789Sahrens 		osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
2751544Seschrock 		osi->os_compress = ZIO_COMPRESS_LZJB;
2763835Sahrens 		osi->os_copies = spa_max_replication(spa);
2777237Sek110237 		osi->os_primary_cache = ZFS_CACHE_ALL;
2787237Sek110237 		osi->os_secondary_cache = ZFS_CACHE_ALL;
279789Sahrens 	}
280789Sahrens 
2817046Sahrens 	osi->os_zil_header = osi->os_phys->os_zil_header;
2827046Sahrens 	osi->os_zil = zil_alloc(&osi->os, &osi->os_zil_header);
283789Sahrens 
284789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
285789Sahrens 		list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t),
286789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
287789Sahrens 		list_create(&osi->os_free_dnodes[i], sizeof (dnode_t),
288789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
289789Sahrens 	}
290789Sahrens 	list_create(&osi->os_dnodes, sizeof (dnode_t),
291789Sahrens 	    offsetof(dnode_t, dn_link));
292789Sahrens 	list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
293789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
294789Sahrens 
2952856Snd150628 	mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL);
2962856Snd150628 	mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
2975326Sek110237 	mutex_init(&osi->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
2982856Snd150628 
299789Sahrens 	osi->os_meta_dnode = dnode_special_open(osi,
300789Sahrens 	    &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
301*9396SMatthew.Ahrens@Sun.COM 	if (arc_buf_size(osi->os_phys_buf) >= sizeof (objset_phys_t)) {
302*9396SMatthew.Ahrens@Sun.COM 		osi->os_userused_dnode = dnode_special_open(osi,
303*9396SMatthew.Ahrens@Sun.COM 		    &osi->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
304*9396SMatthew.Ahrens@Sun.COM 		osi->os_groupused_dnode = dnode_special_open(osi,
305*9396SMatthew.Ahrens@Sun.COM 		    &osi->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
306*9396SMatthew.Ahrens@Sun.COM 	}
307789Sahrens 
3084787Sahrens 	/*
3094787Sahrens 	 * We should be the only thread trying to do this because we
3104787Sahrens 	 * have ds_opening_lock
3114787Sahrens 	 */
3124787Sahrens 	if (ds) {
3134787Sahrens 		VERIFY(NULL == dsl_dataset_set_user_ptr(ds, osi,
3144787Sahrens 		    dmu_objset_evict));
315789Sahrens 	}
316789Sahrens 
3171544Seschrock 	*osip = osi;
3181544Seschrock 	return (0);
319789Sahrens }
320789Sahrens 
3215367Sahrens static int
3225367Sahrens dmu_objset_open_ds_os(dsl_dataset_t *ds, objset_t *os, dmu_objset_type_t type)
3235367Sahrens {
3245367Sahrens 	objset_impl_t *osi;
3255367Sahrens 
3265367Sahrens 	mutex_enter(&ds->ds_opening_lock);
3275367Sahrens 	osi = dsl_dataset_get_user_ptr(ds);
3285367Sahrens 	if (osi == NULL) {
3296689Smaybee 		int err;
3306689Smaybee 
3315367Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
3325367Sahrens 		    ds, &ds->ds_phys->ds_bp, &osi);
3336689Smaybee 		if (err) {
3346689Smaybee 			mutex_exit(&ds->ds_opening_lock);
3355367Sahrens 			return (err);
3366689Smaybee 		}
3375367Sahrens 	}
3385367Sahrens 	mutex_exit(&ds->ds_opening_lock);
3395367Sahrens 
3405367Sahrens 	os->os = osi;
3416689Smaybee 	os->os_mode = DS_MODE_NOHOLD;
3425367Sahrens 
3435367Sahrens 	if (type != DMU_OST_ANY && type != os->os->os_phys->os_type)
3445367Sahrens 		return (EINVAL);
3455367Sahrens 	return (0);
3465367Sahrens }
3475367Sahrens 
3485367Sahrens int
3495367Sahrens dmu_objset_open_ds(dsl_dataset_t *ds, dmu_objset_type_t type, objset_t **osp)
3505367Sahrens {
3515367Sahrens 	objset_t *os;
3525367Sahrens 	int err;
3535367Sahrens 
3545367Sahrens 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
3555367Sahrens 	err = dmu_objset_open_ds_os(ds, os, type);
3565367Sahrens 	if (err)
3575367Sahrens 		kmem_free(os, sizeof (objset_t));
3585367Sahrens 	else
3595367Sahrens 		*osp = os;
3605367Sahrens 	return (err);
3615367Sahrens }
3625367Sahrens 
363789Sahrens /* called from zpl */
364789Sahrens int
365789Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode,
366789Sahrens     objset_t **osp)
367789Sahrens {
3685326Sek110237 	objset_t *os;
369789Sahrens 	dsl_dataset_t *ds;
370789Sahrens 	int err;
371789Sahrens 
3726689Smaybee 	ASSERT(DS_MODE_TYPE(mode) == DS_MODE_USER ||
3736689Smaybee 	    DS_MODE_TYPE(mode) == DS_MODE_OWNER);
3745367Sahrens 
375789Sahrens 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
3766689Smaybee 	if (DS_MODE_TYPE(mode) == DS_MODE_USER)
3776689Smaybee 		err = dsl_dataset_hold(name, os, &ds);
3786689Smaybee 	else
3796689Smaybee 		err = dsl_dataset_own(name, mode, os, &ds);
380789Sahrens 	if (err) {
381789Sahrens 		kmem_free(os, sizeof (objset_t));
382789Sahrens 		return (err);
383789Sahrens 	}
384789Sahrens 
3855367Sahrens 	err = dmu_objset_open_ds_os(ds, os, type);
3865367Sahrens 	if (err) {
3876689Smaybee 		if (DS_MODE_TYPE(mode) == DS_MODE_USER)
3886689Smaybee 			dsl_dataset_rele(ds, os);
3896689Smaybee 		else
3906689Smaybee 			dsl_dataset_disown(ds, os);
3915367Sahrens 		kmem_free(os, sizeof (objset_t));
3925367Sahrens 	} else {
3936689Smaybee 		os->os_mode = mode;
3945367Sahrens 		*osp = os;
395789Sahrens 	}
3965367Sahrens 	return (err);
397789Sahrens }
398789Sahrens 
399789Sahrens void
400789Sahrens dmu_objset_close(objset_t *os)
401789Sahrens {
4026689Smaybee 	ASSERT(DS_MODE_TYPE(os->os_mode) == DS_MODE_USER ||
4036689Smaybee 	    DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER ||
4046689Smaybee 	    DS_MODE_TYPE(os->os_mode) == DS_MODE_NOHOLD);
4056689Smaybee 
4066689Smaybee 	if (DS_MODE_TYPE(os->os_mode) == DS_MODE_USER)
4076689Smaybee 		dsl_dataset_rele(os->os->os_dsl_dataset, os);
4086689Smaybee 	else if (DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER)
4096689Smaybee 		dsl_dataset_disown(os->os->os_dsl_dataset, os);
410789Sahrens 	kmem_free(os, sizeof (objset_t));
411789Sahrens }
412789Sahrens 
4131646Sperrin int
4144944Smaybee dmu_objset_evict_dbufs(objset_t *os)
4151544Seschrock {
4161544Seschrock 	objset_impl_t *osi = os->os;
4171544Seschrock 	dnode_t *dn;
4181596Sahrens 
4191596Sahrens 	mutex_enter(&osi->os_lock);
4201596Sahrens 
4211596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
4221596Sahrens 	list_remove(&osi->os_dnodes, osi->os_meta_dnode);
4231596Sahrens 	list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode);
4241544Seschrock 
4251544Seschrock 	/*
4261596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
4271596Sahrens 	 * because dnode_add_ref() only works if you already have a
4281596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
4291596Sahrens 	 * skip.
4301544Seschrock 	 */
4311596Sahrens 	for (dn = list_head(&osi->os_dnodes);
4324944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
4331596Sahrens 	    dn = list_next(&osi->os_dnodes, dn))
4341596Sahrens 		continue;
4351596Sahrens 
4361596Sahrens 	while (dn) {
4371596Sahrens 		dnode_t *next_dn = dn;
4381596Sahrens 
4391596Sahrens 		do {
4401596Sahrens 			next_dn = list_next(&osi->os_dnodes, next_dn);
4414944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
4421596Sahrens 
4431596Sahrens 		mutex_exit(&osi->os_lock);
4444944Smaybee 		dnode_evict_dbufs(dn);
4451596Sahrens 		dnode_rele(dn, FTAG);
4461596Sahrens 		mutex_enter(&osi->os_lock);
4471596Sahrens 		dn = next_dn;
4481544Seschrock 	}
4491544Seschrock 	mutex_exit(&osi->os_lock);
4504944Smaybee 	return (list_head(&osi->os_dnodes) != osi->os_meta_dnode);
4511544Seschrock }
4521544Seschrock 
4531544Seschrock void
454789Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg)
455789Sahrens {
456789Sahrens 	objset_impl_t *osi = arg;
4571544Seschrock 	objset_t os;
4582082Seschrock 	int i;
459789Sahrens 
460789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
461789Sahrens 		ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL);
462789Sahrens 		ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL);
463789Sahrens 	}
464789Sahrens 
4657237Sek110237 	if (ds) {
4667237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
4677237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
4687237Sek110237 			    checksum_changed_cb, osi));
4697237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
4707237Sek110237 			    compression_changed_cb, osi));
4717237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
4727237Sek110237 			    copies_changed_cb, osi));
4737237Sek110237 		}
4747237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
4757237Sek110237 		    primary_cache_changed_cb, osi));
4767237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
4777237Sek110237 		    secondary_cache_changed_cb, osi));
478789Sahrens 	}
479789Sahrens 
4801544Seschrock 	/*
4811544Seschrock 	 * We should need only a single pass over the dnode list, since
4821544Seschrock 	 * nothing can be added to the list at this point.
4831544Seschrock 	 */
4841544Seschrock 	os.os = osi;
4854944Smaybee 	(void) dmu_objset_evict_dbufs(&os);
4861544Seschrock 
487*9396SMatthew.Ahrens@Sun.COM 	dnode_special_close(osi->os_meta_dnode);
488*9396SMatthew.Ahrens@Sun.COM 	if (osi->os_userused_dnode) {
489*9396SMatthew.Ahrens@Sun.COM 		dnode_special_close(osi->os_userused_dnode);
490*9396SMatthew.Ahrens@Sun.COM 		dnode_special_close(osi->os_groupused_dnode);
491*9396SMatthew.Ahrens@Sun.COM 	}
492*9396SMatthew.Ahrens@Sun.COM 	zil_free(osi->os_zil);
493789Sahrens 
494*9396SMatthew.Ahrens@Sun.COM 	ASSERT3P(list_head(&osi->os_dnodes), ==, NULL);
495789Sahrens 
4963547Smaybee 	VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1);
4972856Snd150628 	mutex_destroy(&osi->os_lock);
4982856Snd150628 	mutex_destroy(&osi->os_obj_lock);
4995326Sek110237 	mutex_destroy(&osi->os_user_ptr_lock);
500789Sahrens 	kmem_free(osi, sizeof (objset_impl_t));
501789Sahrens }
502789Sahrens 
503789Sahrens /* called from dsl for meta-objset */
504789Sahrens objset_impl_t *
5053547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
5063547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
507789Sahrens {
508789Sahrens 	objset_impl_t *osi;
509789Sahrens 	dnode_t *mdn;
510789Sahrens 
511789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
5124787Sahrens 	if (ds)
5134787Sahrens 		mutex_enter(&ds->ds_opening_lock);
5143547Smaybee 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi));
5154787Sahrens 	if (ds)
5164787Sahrens 		mutex_exit(&ds->ds_opening_lock);
517789Sahrens 	mdn = osi->os_meta_dnode;
518789Sahrens 
519789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
520789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
521789Sahrens 
522789Sahrens 	/*
523789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
524789Sahrens 	 * later, because then we could do it in quescing context while
525789Sahrens 	 * we are also accessing it in open context.
526789Sahrens 	 *
527789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
528789Sahrens 	 * because the MOS is only updated in syncing context.
529789Sahrens 	 * This is most fortunate: the MOS is the only objset that
530789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
531789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
532789Sahrens 	 */
5331544Seschrock 	if (ds != NULL) {
5341544Seschrock 		int levels = 1;
5351544Seschrock 
5361544Seschrock 		/*
5371544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
5381544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
5391544Seschrock 		 */
5401544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
5411544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
5421544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
5431544Seschrock 			levels++;
5441544Seschrock 
545789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
5461544Seschrock 		    mdn->dn_nlevels = levels;
5471544Seschrock 	}
548789Sahrens 
549789Sahrens 	ASSERT(type != DMU_OST_NONE);
550789Sahrens 	ASSERT(type != DMU_OST_ANY);
551789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
552789Sahrens 	osi->os_phys->os_type = type;
553*9396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(osi)) {
554*9396SMatthew.Ahrens@Sun.COM 		osi->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
555*9396SMatthew.Ahrens@Sun.COM 		osi->os_flags = osi->os_phys->os_flags;
556*9396SMatthew.Ahrens@Sun.COM 	}
557789Sahrens 
558789Sahrens 	dsl_dataset_dirty(ds, tx);
559789Sahrens 
560789Sahrens 	return (osi);
561789Sahrens }
562789Sahrens 
563789Sahrens struct oscarg {
5644543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
565789Sahrens 	void *userarg;
566789Sahrens 	dsl_dataset_t *clone_parent;
567789Sahrens 	const char *lastname;
568789Sahrens 	dmu_objset_type_t type;
5696492Stimh 	uint64_t flags;
570789Sahrens };
571789Sahrens 
5724543Smarks /*ARGSUSED*/
573789Sahrens static int
5742199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
575789Sahrens {
5762199Sahrens 	dsl_dir_t *dd = arg1;
5772199Sahrens 	struct oscarg *oa = arg2;
5782199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
5792199Sahrens 	int err;
5802199Sahrens 	uint64_t ddobj;
5812199Sahrens 
5822199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
5832199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
5842199Sahrens 	if (err != ENOENT)
5852199Sahrens 		return (err ? err : EEXIST);
5862199Sahrens 
5872199Sahrens 	if (oa->clone_parent != NULL) {
5882199Sahrens 		/*
5892199Sahrens 		 * You can't clone across pools.
5902199Sahrens 		 */
5912199Sahrens 		if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool)
5922199Sahrens 			return (EXDEV);
5932199Sahrens 
5942199Sahrens 		/*
5952199Sahrens 		 * You can only clone snapshots, not the head datasets.
5962199Sahrens 		 */
5972199Sahrens 		if (oa->clone_parent->ds_phys->ds_num_children == 0)
5982199Sahrens 			return (EINVAL);
5992199Sahrens 	}
6004543Smarks 
6012199Sahrens 	return (0);
6022199Sahrens }
6032199Sahrens 
6042199Sahrens static void
6054543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
6062199Sahrens {
6072199Sahrens 	dsl_dir_t *dd = arg1;
6082199Sahrens 	struct oscarg *oa = arg2;
609789Sahrens 	dsl_dataset_t *ds;
6103547Smaybee 	blkptr_t *bp;
6112199Sahrens 	uint64_t dsobj;
612789Sahrens 
613789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
614789Sahrens 
6152199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
6166492Stimh 	    oa->clone_parent, oa->flags, cr, tx);
617789Sahrens 
6186689Smaybee 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, FTAG, &ds));
6193547Smaybee 	bp = dsl_dataset_get_blkptr(ds);
6203547Smaybee 	if (BP_IS_HOLE(bp)) {
621789Sahrens 		objset_impl_t *osi;
622789Sahrens 
623789Sahrens 		/* This is an empty dmu_objset; not a clone. */
624789Sahrens 		osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
6253547Smaybee 		    ds, bp, oa->type, tx);
626789Sahrens 
627789Sahrens 		if (oa->userfunc)
6284543Smarks 			oa->userfunc(&osi->os, oa->userarg, cr, tx);
629789Sahrens 	}
6304543Smarks 
6314543Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
6324543Smarks 	    tx, cr, "dataset = %llu", dsobj);
6334543Smarks 
6346689Smaybee 	dsl_dataset_rele(ds, FTAG);
635789Sahrens }
636789Sahrens 
637789Sahrens int
638789Sahrens dmu_objset_create(const char *name, dmu_objset_type_t type,
6396492Stimh     objset_t *clone_parent, uint64_t flags,
6404543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
641789Sahrens {
6422199Sahrens 	dsl_dir_t *pdd;
643789Sahrens 	const char *tail;
644789Sahrens 	int err = 0;
6452199Sahrens 	struct oscarg oa = { 0 };
646789Sahrens 
6472199Sahrens 	ASSERT(strchr(name, '@') == NULL);
6482199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
6491544Seschrock 	if (err)
6501544Seschrock 		return (err);
651789Sahrens 	if (tail == NULL) {
6522199Sahrens 		dsl_dir_close(pdd, FTAG);
653789Sahrens 		return (EEXIST);
654789Sahrens 	}
655789Sahrens 
656789Sahrens 	dprintf("name=%s\n", name);
657789Sahrens 
6582199Sahrens 	oa.userfunc = func;
6592199Sahrens 	oa.userarg = arg;
6602199Sahrens 	oa.lastname = tail;
6612199Sahrens 	oa.type = type;
6626492Stimh 	oa.flags = flags;
6634543Smarks 
6642199Sahrens 	if (clone_parent != NULL) {
665789Sahrens 		/*
6662199Sahrens 		 * You can't clone to a different type.
667789Sahrens 		 */
6682199Sahrens 		if (clone_parent->os->os_phys->os_type != type) {
6692199Sahrens 			dsl_dir_close(pdd, FTAG);
6702199Sahrens 			return (EINVAL);
671789Sahrens 		}
6722199Sahrens 		oa.clone_parent = clone_parent->os->os_dsl_dataset;
673789Sahrens 	}
6742199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
6752199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
6762199Sahrens 	dsl_dir_close(pdd, FTAG);
677789Sahrens 	return (err);
678789Sahrens }
679789Sahrens 
680789Sahrens int
681789Sahrens dmu_objset_destroy(const char *name)
682789Sahrens {
683789Sahrens 	objset_t *os;
684789Sahrens 	int error;
685789Sahrens 
686789Sahrens 	/*
687789Sahrens 	 * If it looks like we'll be able to destroy it, and there's
688789Sahrens 	 * an unplayed replay log sitting around, destroy the log.
689789Sahrens 	 * It would be nicer to do this in dsl_dataset_destroy_sync(),
690789Sahrens 	 * but the replay log objset is modified in open context.
691789Sahrens 	 */
6925367Sahrens 	error = dmu_objset_open(name, DMU_OST_ANY,
6936689Smaybee 	    DS_MODE_OWNER|DS_MODE_READONLY|DS_MODE_INCONSISTENT, &os);
694789Sahrens 	if (error == 0) {
6955367Sahrens 		dsl_dataset_t *ds = os->os->os_dsl_dataset;
6961807Sbonwick 		zil_destroy(dmu_objset_zil(os), B_FALSE);
6975367Sahrens 
6986689Smaybee 		error = dsl_dataset_destroy(ds, os);
6995367Sahrens 		/*
7005367Sahrens 		 * dsl_dataset_destroy() closes the ds.
7015367Sahrens 		 */
7025367Sahrens 		kmem_free(os, sizeof (objset_t));
703789Sahrens 	}
704789Sahrens 
7055367Sahrens 	return (error);
706789Sahrens }
707789Sahrens 
7085446Sahrens /*
7095446Sahrens  * This will close the objset.
7105446Sahrens  */
711789Sahrens int
7125446Sahrens dmu_objset_rollback(objset_t *os)
713789Sahrens {
714789Sahrens 	int err;
7155367Sahrens 	dsl_dataset_t *ds;
716789Sahrens 
7175446Sahrens 	ds = os->os->os_dsl_dataset;
7184935Sperrin 
7196689Smaybee 	if (!dsl_dataset_tryown(ds, TRUE, os)) {
7205446Sahrens 		dmu_objset_close(os);
7215446Sahrens 		return (EBUSY);
7225446Sahrens 	}
7235446Sahrens 
7245367Sahrens 	err = dsl_dataset_rollback(ds, os->os->os_phys->os_type);
7254935Sperrin 
7265367Sahrens 	/*
7275367Sahrens 	 * NB: we close the objset manually because the rollback
7285367Sahrens 	 * actually implicitly called dmu_objset_evict(), thus freeing
7295367Sahrens 	 * the objset_impl_t.
7305367Sahrens 	 */
7316689Smaybee 	dsl_dataset_disown(ds, os);
7325367Sahrens 	kmem_free(os, sizeof (objset_t));
733789Sahrens 	return (err);
734789Sahrens }
735789Sahrens 
7362199Sahrens struct snaparg {
7372199Sahrens 	dsl_sync_task_group_t *dstg;
7382199Sahrens 	char *snapname;
7392199Sahrens 	char failed[MAXPATHLEN];
7404543Smarks 	boolean_t checkperms;
7419355SMatthew.Ahrens@Sun.COM 	nvlist_t *props;
7425367Sahrens };
7435367Sahrens 
7449355SMatthew.Ahrens@Sun.COM static int
7459355SMatthew.Ahrens@Sun.COM snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
7469355SMatthew.Ahrens@Sun.COM {
7479355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
7489355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7499355SMatthew.Ahrens@Sun.COM 
7509355SMatthew.Ahrens@Sun.COM 	/* The props have already been checked by zfs_check_userprops(). */
7519355SMatthew.Ahrens@Sun.COM 
7529355SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_snapshot_check(os->os->os_dsl_dataset,
7539355SMatthew.Ahrens@Sun.COM 	    sn->snapname, tx));
7549355SMatthew.Ahrens@Sun.COM }
7559355SMatthew.Ahrens@Sun.COM 
7569355SMatthew.Ahrens@Sun.COM static void
7579355SMatthew.Ahrens@Sun.COM snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
7589355SMatthew.Ahrens@Sun.COM {
7599355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
7609355SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
7619355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7629355SMatthew.Ahrens@Sun.COM 
7639355SMatthew.Ahrens@Sun.COM 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
7649355SMatthew.Ahrens@Sun.COM 
7659355SMatthew.Ahrens@Sun.COM 	if (sn->props)
7669355SMatthew.Ahrens@Sun.COM 		dsl_props_set_sync(ds->ds_prev, sn->props, cr, tx);
7679355SMatthew.Ahrens@Sun.COM }
7682199Sahrens 
7692199Sahrens static int
7702199Sahrens dmu_objset_snapshot_one(char *name, void *arg)
7712199Sahrens {
7722199Sahrens 	struct snaparg *sn = arg;
7732199Sahrens 	objset_t *os;
7742199Sahrens 	int err;
7752199Sahrens 
7762199Sahrens 	(void) strcpy(sn->failed, name);
7772199Sahrens 
7784543Smarks 	/*
7794543Smarks 	 * Check permissions only when requested.  This only applies when
7804543Smarks 	 * doing a recursive snapshot.  The permission checks for the starting
7814543Smarks 	 * dataset have already been performed in zfs_secpolicy_snapshot()
7824543Smarks 	 */
7834543Smarks 	if (sn->checkperms == B_TRUE &&
7844543Smarks 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
7854543Smarks 		return (err);
7864543Smarks 
7876689Smaybee 	err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_USER, &os);
7882199Sahrens 	if (err != 0)
7892199Sahrens 		return (err);
7902199Sahrens 
7916689Smaybee 	/* If the objset is in an inconsistent state, return busy */
7926689Smaybee 	if (os->os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
7933637Srm160521 		dmu_objset_close(os);
7943637Srm160521 		return (EBUSY);
7953637Srm160521 	}
7963637Srm160521 
7973637Srm160521 	/*
7982199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
7992199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
8002199Sahrens 	 * a side effect.
8012199Sahrens 	 */
8022199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
8032199Sahrens 	if (err == 0) {
8049355SMatthew.Ahrens@Sun.COM 		dsl_sync_task_create(sn->dstg, snapshot_check,
8059355SMatthew.Ahrens@Sun.COM 		    snapshot_sync, os, sn, 3);
8063637Srm160521 	} else {
8073637Srm160521 		dmu_objset_close(os);
8082199Sahrens 	}
8093637Srm160521 
8102199Sahrens 	return (err);
8112199Sahrens }
8122199Sahrens 
8132199Sahrens int
8149355SMatthew.Ahrens@Sun.COM dmu_objset_snapshot(char *fsname, char *snapname,
8159355SMatthew.Ahrens@Sun.COM     nvlist_t *props, boolean_t recursive)
8162199Sahrens {
8172199Sahrens 	dsl_sync_task_t *dst;
8189355SMatthew.Ahrens@Sun.COM 	struct snaparg sn;
8192199Sahrens 	spa_t *spa;
8202199Sahrens 	int err;
8212199Sahrens 
8222199Sahrens 	(void) strcpy(sn.failed, fsname);
8232199Sahrens 
8244603Sahrens 	err = spa_open(fsname, &spa, FTAG);
8252199Sahrens 	if (err)
8262199Sahrens 		return (err);
8272199Sahrens 
8282199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8292199Sahrens 	sn.snapname = snapname;
8309355SMatthew.Ahrens@Sun.COM 	sn.props = props;
8312199Sahrens 
8322417Sahrens 	if (recursive) {
8334543Smarks 		sn.checkperms = B_TRUE;
8342417Sahrens 		err = dmu_objset_find(fsname,
8352417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
8362417Sahrens 	} else {
8374543Smarks 		sn.checkperms = B_FALSE;
8382199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
8392417Sahrens 	}
8402199Sahrens 
8419355SMatthew.Ahrens@Sun.COM 	if (err == 0)
8429355SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_group_wait(sn.dstg);
8432199Sahrens 
8442199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
8452199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
8469355SMatthew.Ahrens@Sun.COM 		objset_t *os = dst->dst_arg1;
8479355SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = os->os->os_dsl_dataset;
8482199Sahrens 		if (dst->dst_err)
8495367Sahrens 			dsl_dataset_name(ds, sn.failed);
8509355SMatthew.Ahrens@Sun.COM 		zil_resume(dmu_objset_zil(os));
8519355SMatthew.Ahrens@Sun.COM 		dmu_objset_close(os);
8522199Sahrens 	}
8535367Sahrens 
8542199Sahrens 	if (err)
8552199Sahrens 		(void) strcpy(fsname, sn.failed);
8562199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
8572199Sahrens 	spa_close(spa, FTAG);
8582199Sahrens 	return (err);
8592199Sahrens }
8602199Sahrens 
861789Sahrens static void
862*9396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
863789Sahrens {
8643547Smaybee 	dnode_t *dn;
865789Sahrens 
8663547Smaybee 	while (dn = list_head(list)) {
8673547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
8683547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
8693547Smaybee 		/*
870*9396SMatthew.Ahrens@Sun.COM 		 * Initialize dn_zio outside dnode_sync() because the
871*9396SMatthew.Ahrens@Sun.COM 		 * meta-dnode needs to set it ouside dnode_sync().
8723547Smaybee 		 */
8733547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
8743547Smaybee 		ASSERT(dn->dn_zio);
875789Sahrens 
8763547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
8773547Smaybee 		list_remove(list, dn);
878*9396SMatthew.Ahrens@Sun.COM 
879*9396SMatthew.Ahrens@Sun.COM 		if (newlist) {
880*9396SMatthew.Ahrens@Sun.COM 			(void) dnode_add_ref(dn, newlist);
881*9396SMatthew.Ahrens@Sun.COM 			list_insert_tail(newlist, dn);
882*9396SMatthew.Ahrens@Sun.COM 		}
883*9396SMatthew.Ahrens@Sun.COM 
8843547Smaybee 		dnode_sync(dn, tx);
8853547Smaybee 	}
8863547Smaybee }
8872981Sahrens 
8883547Smaybee /* ARGSUSED */
8893547Smaybee static void
8903547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg)
8913547Smaybee {
8927754SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
8937754SJeff.Bonwick@Sun.COM 	blkptr_t *bp_orig = &zio->io_bp_orig;
8943547Smaybee 	objset_impl_t *os = arg;
8953547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
8962981Sahrens 
8977754SJeff.Bonwick@Sun.COM 	ASSERT(bp == os->os_rootbp);
8987754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
8997754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(bp) == 0);
9005329Sgw25295 
9013547Smaybee 	/*
902*9396SMatthew.Ahrens@Sun.COM 	 * Update rootbp fill count: it should be the number of objects
903*9396SMatthew.Ahrens@Sun.COM 	 * allocated in the object set (not counting the "special"
904*9396SMatthew.Ahrens@Sun.COM 	 * objects that are stored in the objset_phys_t -- the meta
905*9396SMatthew.Ahrens@Sun.COM 	 * dnode and user/group accounting objects).
9063547Smaybee 	 */
907*9396SMatthew.Ahrens@Sun.COM 	bp->blk_fill = 0;
9087754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < dnp->dn_nblkptr; i++)
9093547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
9105329Sgw25295 
9117754SJeff.Bonwick@Sun.COM 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
9127754SJeff.Bonwick@Sun.COM 		ASSERT(DVA_EQUAL(BP_IDENTITY(bp), BP_IDENTITY(bp_orig)));
9137754SJeff.Bonwick@Sun.COM 	} else {
9145329Sgw25295 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
9156992Smaybee 			(void) dsl_dataset_block_kill(os->os_dsl_dataset,
9167754SJeff.Bonwick@Sun.COM 			    &zio->io_bp_orig, zio, os->os_synctx);
9175329Sgw25295 		dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx);
9185329Sgw25295 	}
919789Sahrens }
920789Sahrens 
921789Sahrens /* called from dsl */
922789Sahrens void
9233547Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx)
924789Sahrens {
925789Sahrens 	int txgoff;
9261544Seschrock 	zbookmark_t zb;
9277046Sahrens 	writeprops_t wp = { 0 };
9283547Smaybee 	zio_t *zio;
9293547Smaybee 	list_t *list;
930*9396SMatthew.Ahrens@Sun.COM 	list_t *newlist = NULL;
9313547Smaybee 	dbuf_dirty_record_t *dr;
9323547Smaybee 
9333547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
934789Sahrens 
935789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
936789Sahrens 	/* XXX the write_done callback should really give us the tx... */
937789Sahrens 	os->os_synctx = tx;
938789Sahrens 
9393882Sahrens 	if (os->os_dsl_dataset == NULL) {
9403882Sahrens 		/*
9413882Sahrens 		 * This is the MOS.  If we have upgraded,
9423882Sahrens 		 * spa_max_replication() could change, so reset
9433882Sahrens 		 * os_copies here.
9443882Sahrens 		 */
9453882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
9463882Sahrens 	}
9473882Sahrens 
9483547Smaybee 	/*
9493547Smaybee 	 * Create the root block IO
9503547Smaybee 	 */
9513547Smaybee 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
9523547Smaybee 	zb.zb_object = 0;
9537754SJeff.Bonwick@Sun.COM 	zb.zb_level = -1;	/* for block ordering; it's level 0 on disk */
9543547Smaybee 	zb.zb_blkid = 0;
9557754SJeff.Bonwick@Sun.COM 
9567754SJeff.Bonwick@Sun.COM 	wp.wp_type = DMU_OT_OBJSET;
9577754SJeff.Bonwick@Sun.COM 	wp.wp_level = 0;	/* on-disk BP level; see above */
9587754SJeff.Bonwick@Sun.COM 	wp.wp_copies = os->os_copies;
9597754SJeff.Bonwick@Sun.COM 	wp.wp_oschecksum = os->os_checksum;
9607754SJeff.Bonwick@Sun.COM 	wp.wp_oscompress = os->os_compress;
9617754SJeff.Bonwick@Sun.COM 
9624787Sahrens 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
9636992Smaybee 		(void) dsl_dataset_block_kill(os->os_dsl_dataset,
9643547Smaybee 		    os->os_rootbp, pio, tx);
9654787Sahrens 	}
9667754SJeff.Bonwick@Sun.COM 
9677046Sahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
968*9396SMatthew.Ahrens@Sun.COM 
9697754SJeff.Bonwick@Sun.COM 	zio = arc_write(pio, os->os_spa, &wp, DMU_OS_IS_L2CACHEABLE(os),
9707754SJeff.Bonwick@Sun.COM 	    tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os,
9717754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
9723547Smaybee 
9733547Smaybee 	/*
974*9396SMatthew.Ahrens@Sun.COM 	 * Sync special dnodes - the parent IO for the sync is the root block
9753547Smaybee 	 */
9763547Smaybee 	os->os_meta_dnode->dn_zio = zio;
9773547Smaybee 	dnode_sync(os->os_meta_dnode, tx);
978789Sahrens 
979*9396SMatthew.Ahrens@Sun.COM 	os->os_phys->os_flags = os->os_flags;
980*9396SMatthew.Ahrens@Sun.COM 
981*9396SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode &&
982*9396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
983*9396SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode->dn_zio = zio;
984*9396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_userused_dnode, tx);
985*9396SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode->dn_zio = zio;
986*9396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_groupused_dnode, tx);
987*9396SMatthew.Ahrens@Sun.COM 	}
988*9396SMatthew.Ahrens@Sun.COM 
989789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
990789Sahrens 
991*9396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
992*9396SMatthew.Ahrens@Sun.COM 		newlist = &os->os_synced_dnodes;
993*9396SMatthew.Ahrens@Sun.COM 		/*
994*9396SMatthew.Ahrens@Sun.COM 		 * We must create the list here because it uses the
995*9396SMatthew.Ahrens@Sun.COM 		 * dn_dirty_link[] of this txg.
996*9396SMatthew.Ahrens@Sun.COM 		 */
997*9396SMatthew.Ahrens@Sun.COM 		list_create(newlist, sizeof (dnode_t),
998*9396SMatthew.Ahrens@Sun.COM 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
999*9396SMatthew.Ahrens@Sun.COM 	}
1000*9396SMatthew.Ahrens@Sun.COM 
1001*9396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1002*9396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1003789Sahrens 
10043547Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
10053547Smaybee 	while (dr = list_head(list)) {
10063547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
10073547Smaybee 		list_remove(list, dr);
10083547Smaybee 		if (dr->dr_zio)
10093547Smaybee 			zio_nowait(dr->dr_zio);
10103547Smaybee 	}
1011789Sahrens 	/*
1012789Sahrens 	 * Free intent log blocks up to this tx.
1013789Sahrens 	 */
1014789Sahrens 	zil_sync(os->os_zil, tx);
10157046Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
10163547Smaybee 	zio_nowait(zio);
1017789Sahrens }
1018789Sahrens 
1019*9396SMatthew.Ahrens@Sun.COM static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1020*9396SMatthew.Ahrens@Sun.COM 
1021*9396SMatthew.Ahrens@Sun.COM void
1022*9396SMatthew.Ahrens@Sun.COM dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1023*9396SMatthew.Ahrens@Sun.COM {
1024*9396SMatthew.Ahrens@Sun.COM 	used_cbs[ost] = cb;
1025*9396SMatthew.Ahrens@Sun.COM }
1026*9396SMatthew.Ahrens@Sun.COM 
1027*9396SMatthew.Ahrens@Sun.COM boolean_t
1028*9396SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(objset_impl_t *os)
1029*9396SMatthew.Ahrens@Sun.COM {
1030*9396SMatthew.Ahrens@Sun.COM 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1031*9396SMatthew.Ahrens@Sun.COM 	    used_cbs[os->os_phys->os_type] &&
1032*9396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode);
1033*9396SMatthew.Ahrens@Sun.COM }
1034*9396SMatthew.Ahrens@Sun.COM 
1035*9396SMatthew.Ahrens@Sun.COM void
1036*9396SMatthew.Ahrens@Sun.COM dmu_objset_do_userquota_callbacks(objset_impl_t *os, dmu_tx_t *tx)
1037*9396SMatthew.Ahrens@Sun.COM {
1038*9396SMatthew.Ahrens@Sun.COM 	dnode_t *dn;
1039*9396SMatthew.Ahrens@Sun.COM 	list_t *list = &os->os_synced_dnodes;
1040*9396SMatthew.Ahrens@Sun.COM 	static const char zerobuf[DN_MAX_BONUSLEN] = {0};
1041*9396SMatthew.Ahrens@Sun.COM 
1042*9396SMatthew.Ahrens@Sun.COM 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1043*9396SMatthew.Ahrens@Sun.COM 
1044*9396SMatthew.Ahrens@Sun.COM 	while (dn = list_head(list)) {
1045*9396SMatthew.Ahrens@Sun.COM 		dmu_object_type_t bonustype;
1046*9396SMatthew.Ahrens@Sun.COM 
1047*9396SMatthew.Ahrens@Sun.COM 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1048*9396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_oldphys);
1049*9396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1050*9396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_flags &
1051*9396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED);
1052*9396SMatthew.Ahrens@Sun.COM 
1053*9396SMatthew.Ahrens@Sun.COM 		/* Allocate the user/groupused objects if necessary. */
1054*9396SMatthew.Ahrens@Sun.COM 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
1055*9396SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(&os->os,
1056*9396SMatthew.Ahrens@Sun.COM 			    DMU_USERUSED_OBJECT,
1057*9396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1058*9396SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(&os->os,
1059*9396SMatthew.Ahrens@Sun.COM 			    DMU_GROUPUSED_OBJECT,
1060*9396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1061*9396SMatthew.Ahrens@Sun.COM 		}
1062*9396SMatthew.Ahrens@Sun.COM 
1063*9396SMatthew.Ahrens@Sun.COM 		/*
1064*9396SMatthew.Ahrens@Sun.COM 		 * If the object was not previously
1065*9396SMatthew.Ahrens@Sun.COM 		 * accounted, pretend that it was free.
1066*9396SMatthew.Ahrens@Sun.COM 		 */
1067*9396SMatthew.Ahrens@Sun.COM 		if (!(dn->dn_oldphys->dn_flags &
1068*9396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED)) {
1069*9396SMatthew.Ahrens@Sun.COM 			bzero(dn->dn_oldphys, sizeof (dnode_phys_t));
1070*9396SMatthew.Ahrens@Sun.COM 		}
1071*9396SMatthew.Ahrens@Sun.COM 
1072*9396SMatthew.Ahrens@Sun.COM 		/*
1073*9396SMatthew.Ahrens@Sun.COM 		 * If the object was freed, use the previous bonustype.
1074*9396SMatthew.Ahrens@Sun.COM 		 */
1075*9396SMatthew.Ahrens@Sun.COM 		bonustype = dn->dn_phys->dn_bonustype ?
1076*9396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_bonustype : dn->dn_oldphys->dn_bonustype;
1077*9396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type != 0 ||
1078*9396SMatthew.Ahrens@Sun.COM 		    (bcmp(DN_BONUS(dn->dn_phys), zerobuf,
1079*9396SMatthew.Ahrens@Sun.COM 		    DN_MAX_BONUSLEN) == 0 &&
1080*9396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_phys) == 0));
1081*9396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_oldphys->dn_type != 0 ||
1082*9396SMatthew.Ahrens@Sun.COM 		    (bcmp(DN_BONUS(dn->dn_oldphys), zerobuf,
1083*9396SMatthew.Ahrens@Sun.COM 		    DN_MAX_BONUSLEN) == 0 &&
1084*9396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_oldphys) == 0));
1085*9396SMatthew.Ahrens@Sun.COM 		used_cbs[os->os_phys->os_type](&os->os, bonustype,
1086*9396SMatthew.Ahrens@Sun.COM 		    DN_BONUS(dn->dn_oldphys), DN_BONUS(dn->dn_phys),
1087*9396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_oldphys),
1088*9396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_phys), tx);
1089*9396SMatthew.Ahrens@Sun.COM 
1090*9396SMatthew.Ahrens@Sun.COM 		/*
1091*9396SMatthew.Ahrens@Sun.COM 		 * The mutex is needed here for interlock with dnode_allocate.
1092*9396SMatthew.Ahrens@Sun.COM 		 */
1093*9396SMatthew.Ahrens@Sun.COM 		mutex_enter(&dn->dn_mtx);
1094*9396SMatthew.Ahrens@Sun.COM 		zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t));
1095*9396SMatthew.Ahrens@Sun.COM 		dn->dn_oldphys = NULL;
1096*9396SMatthew.Ahrens@Sun.COM 		mutex_exit(&dn->dn_mtx);
1097*9396SMatthew.Ahrens@Sun.COM 
1098*9396SMatthew.Ahrens@Sun.COM 		list_remove(list, dn);
1099*9396SMatthew.Ahrens@Sun.COM 		dnode_rele(dn, list);
1100*9396SMatthew.Ahrens@Sun.COM 	}
1101*9396SMatthew.Ahrens@Sun.COM }
1102*9396SMatthew.Ahrens@Sun.COM 
1103*9396SMatthew.Ahrens@Sun.COM boolean_t
1104*9396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(objset_t *os)
1105*9396SMatthew.Ahrens@Sun.COM {
1106*9396SMatthew.Ahrens@Sun.COM 	return (os->os->os_phys->os_flags &
1107*9396SMatthew.Ahrens@Sun.COM 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1108*9396SMatthew.Ahrens@Sun.COM }
1109*9396SMatthew.Ahrens@Sun.COM 
1110*9396SMatthew.Ahrens@Sun.COM int
1111*9396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_upgrade(objset_t *os)
1112*9396SMatthew.Ahrens@Sun.COM {
1113*9396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
1114*9396SMatthew.Ahrens@Sun.COM 	int err = 0;
1115*9396SMatthew.Ahrens@Sun.COM 
1116*9396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userspace_present(os))
1117*9396SMatthew.Ahrens@Sun.COM 		return (0);
1118*9396SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userused_enabled(os->os))
1119*9396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
1120*9396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_is_snapshot(os))
1121*9396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
1122*9396SMatthew.Ahrens@Sun.COM 
1123*9396SMatthew.Ahrens@Sun.COM 	/*
1124*9396SMatthew.Ahrens@Sun.COM 	 * We simply need to mark every object dirty, so that it will be
1125*9396SMatthew.Ahrens@Sun.COM 	 * synced out and now accounted.  If this is called
1126*9396SMatthew.Ahrens@Sun.COM 	 * concurrently, or if we already did some work before crashing,
1127*9396SMatthew.Ahrens@Sun.COM 	 * that's fine, since we track each object's accounted state
1128*9396SMatthew.Ahrens@Sun.COM 	 * independently.
1129*9396SMatthew.Ahrens@Sun.COM 	 */
1130*9396SMatthew.Ahrens@Sun.COM 
1131*9396SMatthew.Ahrens@Sun.COM 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1132*9396SMatthew.Ahrens@Sun.COM 		dmu_tx_t *tx = dmu_tx_create(os);
1133*9396SMatthew.Ahrens@Sun.COM 		dmu_buf_t *db;
1134*9396SMatthew.Ahrens@Sun.COM 		int objerr;
1135*9396SMatthew.Ahrens@Sun.COM 
1136*9396SMatthew.Ahrens@Sun.COM 		if (issig(JUSTLOOKING) && issig(FORREAL))
1137*9396SMatthew.Ahrens@Sun.COM 			return (EINTR);
1138*9396SMatthew.Ahrens@Sun.COM 
1139*9396SMatthew.Ahrens@Sun.COM 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1140*9396SMatthew.Ahrens@Sun.COM 		if (objerr)
1141*9396SMatthew.Ahrens@Sun.COM 			continue;
1142*9396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_bonus(tx, obj);
1143*9396SMatthew.Ahrens@Sun.COM 		objerr = dmu_tx_assign(tx, TXG_WAIT);
1144*9396SMatthew.Ahrens@Sun.COM 		if (objerr) {
1145*9396SMatthew.Ahrens@Sun.COM 			dmu_tx_abort(tx);
1146*9396SMatthew.Ahrens@Sun.COM 			continue;
1147*9396SMatthew.Ahrens@Sun.COM 		}
1148*9396SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(db, tx);
1149*9396SMatthew.Ahrens@Sun.COM 		dmu_buf_rele(db, FTAG);
1150*9396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
1151*9396SMatthew.Ahrens@Sun.COM 	}
1152*9396SMatthew.Ahrens@Sun.COM 
1153*9396SMatthew.Ahrens@Sun.COM 	os->os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1154*9396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
1155*9396SMatthew.Ahrens@Sun.COM 	return (0);
1156*9396SMatthew.Ahrens@Sun.COM }
1157*9396SMatthew.Ahrens@Sun.COM 
1158789Sahrens void
11592885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
11602885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
11612885Sahrens {
11622885Sahrens 	dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp,
11632885Sahrens 	    usedobjsp, availobjsp);
11642885Sahrens }
11652885Sahrens 
11662885Sahrens uint64_t
11672885Sahrens dmu_objset_fsid_guid(objset_t *os)
11682885Sahrens {
11692885Sahrens 	return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset));
11702885Sahrens }
11712885Sahrens 
11722885Sahrens void
11732885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1174789Sahrens {
11752885Sahrens 	stat->dds_type = os->os->os_phys->os_type;
11762885Sahrens 	if (os->os->os_dsl_dataset)
11772885Sahrens 		dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat);
11782885Sahrens }
11792885Sahrens 
11802885Sahrens void
11812885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
11822885Sahrens {
11832885Sahrens 	ASSERT(os->os->os_dsl_dataset ||
11842885Sahrens 	    os->os->os_phys->os_type == DMU_OST_META);
11852885Sahrens 
11862885Sahrens 	if (os->os->os_dsl_dataset != NULL)
11872885Sahrens 		dsl_dataset_stats(os->os->os_dsl_dataset, nv);
11882885Sahrens 
11892885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
11902885Sahrens 	    os->os->os_phys->os_type);
1191*9396SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1192*9396SMatthew.Ahrens@Sun.COM 	    dmu_objset_userspace_present(os));
1193789Sahrens }
1194789Sahrens 
1195789Sahrens int
1196789Sahrens dmu_objset_is_snapshot(objset_t *os)
1197789Sahrens {
1198789Sahrens 	if (os->os->os_dsl_dataset != NULL)
1199789Sahrens 		return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset));
1200789Sahrens 	else
1201789Sahrens 		return (B_FALSE);
1202789Sahrens }
1203789Sahrens 
1204789Sahrens int
12056492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
12066492Stimh     boolean_t *conflict)
12076492Stimh {
12086492Stimh 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
12096492Stimh 	uint64_t ignored;
12106492Stimh 
12116492Stimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
12126492Stimh 		return (ENOENT);
12136492Stimh 
12146492Stimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
12156492Stimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
12166492Stimh 	    real, maxlen, conflict));
12176492Stimh }
12186492Stimh 
12196492Stimh int
1220789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
12215663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1222789Sahrens {
1223789Sahrens 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
1224789Sahrens 	zap_cursor_t cursor;
1225789Sahrens 	zap_attribute_t attr;
1226789Sahrens 
1227789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1228789Sahrens 		return (ENOENT);
1229789Sahrens 
1230789Sahrens 	zap_cursor_init_serialized(&cursor,
1231789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1232789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1233789Sahrens 
1234885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1235885Sahrens 		zap_cursor_fini(&cursor);
1236885Sahrens 		return (ENOENT);
1237885Sahrens 	}
1238885Sahrens 
1239885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1240885Sahrens 		zap_cursor_fini(&cursor);
1241885Sahrens 		return (ENAMETOOLONG);
1242885Sahrens 	}
1243885Sahrens 
1244885Sahrens 	(void) strcpy(name, attr.za_name);
1245885Sahrens 	if (idp)
1246885Sahrens 		*idp = attr.za_first_integer;
12475663Sck153898 	if (case_conflict)
12485663Sck153898 		*case_conflict = attr.za_normalization_conflict;
1249885Sahrens 	zap_cursor_advance(&cursor);
1250885Sahrens 	*offp = zap_cursor_serialize(&cursor);
1251885Sahrens 	zap_cursor_fini(&cursor);
1252885Sahrens 
1253885Sahrens 	return (0);
1254885Sahrens }
1255885Sahrens 
1256885Sahrens int
1257885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
1258885Sahrens     uint64_t *idp, uint64_t *offp)
1259885Sahrens {
1260885Sahrens 	dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir;
1261885Sahrens 	zap_cursor_t cursor;
1262885Sahrens 	zap_attribute_t attr;
1263885Sahrens 
1264885Sahrens 	/* there is no next dir on a snapshot! */
1265885Sahrens 	if (os->os->os_dsl_dataset->ds_object !=
1266885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
1267885Sahrens 		return (ENOENT);
1268885Sahrens 
1269885Sahrens 	zap_cursor_init_serialized(&cursor,
1270885Sahrens 	    dd->dd_pool->dp_meta_objset,
1271885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1272885Sahrens 
1273885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1274885Sahrens 		zap_cursor_fini(&cursor);
1275885Sahrens 		return (ENOENT);
1276885Sahrens 	}
1277885Sahrens 
1278885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1279885Sahrens 		zap_cursor_fini(&cursor);
1280789Sahrens 		return (ENAMETOOLONG);
1281885Sahrens 	}
1282789Sahrens 
1283789Sahrens 	(void) strcpy(name, attr.za_name);
1284885Sahrens 	if (idp)
1285885Sahrens 		*idp = attr.za_first_integer;
1286789Sahrens 	zap_cursor_advance(&cursor);
1287789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1288885Sahrens 	zap_cursor_fini(&cursor);
1289789Sahrens 
1290789Sahrens 	return (0);
1291789Sahrens }
1292789Sahrens 
12937046Sahrens struct findarg {
12947046Sahrens 	int (*func)(char *, void *);
12957046Sahrens 	void *arg;
12967046Sahrens };
12977046Sahrens 
12987046Sahrens /* ARGSUSED */
12997046Sahrens static int
13007046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
13017046Sahrens {
13027046Sahrens 	struct findarg *fa = arg;
13037046Sahrens 	return (fa->func((char *)dsname, fa->arg));
13047046Sahrens }
13057046Sahrens 
1306789Sahrens /*
1307789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
13087046Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1309789Sahrens  */
13102199Sahrens int
13112199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1312789Sahrens {
13137046Sahrens 	struct findarg fa;
13147046Sahrens 	fa.func = func;
13157046Sahrens 	fa.arg = arg;
13167046Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
13177046Sahrens }
13187046Sahrens 
13197046Sahrens /*
13207046Sahrens  * Find all objsets under name, call func on each
13217046Sahrens  */
13227046Sahrens int
13237046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
13247046Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
13257046Sahrens {
1326789Sahrens 	dsl_dir_t *dd;
13277046Sahrens 	dsl_pool_t *dp;
13287046Sahrens 	dsl_dataset_t *ds;
1329789Sahrens 	zap_cursor_t zc;
13303978Smmusante 	zap_attribute_t *attr;
1331789Sahrens 	char *child;
13327046Sahrens 	uint64_t thisobj;
13337046Sahrens 	int err;
1334789Sahrens 
13357046Sahrens 	if (name == NULL)
13367046Sahrens 		name = spa_name(spa);
13377046Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
13381544Seschrock 	if (err)
13392199Sahrens 		return (err);
1340789Sahrens 
13417046Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
13427046Sahrens 	if (dd->dd_myname[0] == '$') {
13437046Sahrens 		dsl_dir_close(dd, FTAG);
13447046Sahrens 		return (0);
13457046Sahrens 	}
13467046Sahrens 
13477046Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
13483978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
13497046Sahrens 	dp = dd->dd_pool;
1350789Sahrens 
1351789Sahrens 	/*
1352789Sahrens 	 * Iterate over all children.
1353789Sahrens 	 */
13542417Sahrens 	if (flags & DS_FIND_CHILDREN) {
13557046Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
13562417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
13573978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
13582417Sahrens 		    (void) zap_cursor_advance(&zc)) {
13593978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
13603978Smmusante 			ASSERT(attr->za_num_integers == 1);
1361789Sahrens 
13622417Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
13637046Sahrens 			(void) strcpy(child, name);
13642417Sahrens 			(void) strcat(child, "/");
13653978Smmusante 			(void) strcat(child, attr->za_name);
13667046Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
13672417Sahrens 			kmem_free(child, MAXPATHLEN);
13682417Sahrens 			if (err)
13692417Sahrens 				break;
13702417Sahrens 		}
13712417Sahrens 		zap_cursor_fini(&zc);
13722199Sahrens 
13732417Sahrens 		if (err) {
13742417Sahrens 			dsl_dir_close(dd, FTAG);
13753978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
13762417Sahrens 			return (err);
13772417Sahrens 		}
1378789Sahrens 	}
1379789Sahrens 
1380789Sahrens 	/*
1381789Sahrens 	 * Iterate over all snapshots.
1382789Sahrens 	 */
13837046Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
13847046Sahrens 		if (!dsl_pool_sync_context(dp))
13857046Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
13867046Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
13877046Sahrens 		if (!dsl_pool_sync_context(dp))
13887046Sahrens 			rw_exit(&dp->dp_config_rwlock);
1389789Sahrens 
13907046Sahrens 		if (err == 0) {
13917046Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
13927046Sahrens 			dsl_dataset_rele(ds, FTAG);
1393789Sahrens 
13947046Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
13957046Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
13967046Sahrens 			    (void) zap_cursor_advance(&zc)) {
13977046Sahrens 				ASSERT(attr->za_integer_length ==
13987046Sahrens 				    sizeof (uint64_t));
13997046Sahrens 				ASSERT(attr->za_num_integers == 1);
1400789Sahrens 
14017046Sahrens 				child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
14027046Sahrens 				(void) strcpy(child, name);
14037046Sahrens 				(void) strcat(child, "@");
14047046Sahrens 				(void) strcat(child, attr->za_name);
14057046Sahrens 				err = func(spa, attr->za_first_integer,
14067046Sahrens 				    child, arg);
14077046Sahrens 				kmem_free(child, MAXPATHLEN);
14087046Sahrens 				if (err)
14097046Sahrens 					break;
14107046Sahrens 			}
14117046Sahrens 			zap_cursor_fini(&zc);
1412789Sahrens 		}
1413789Sahrens 	}
1414789Sahrens 
1415789Sahrens 	dsl_dir_close(dd, FTAG);
14163978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1417789Sahrens 
14182199Sahrens 	if (err)
14192199Sahrens 		return (err);
14202199Sahrens 
1421789Sahrens 	/*
1422789Sahrens 	 * Apply to self if appropriate.
1423789Sahrens 	 */
14247046Sahrens 	err = func(spa, thisobj, name, arg);
14252199Sahrens 	return (err);
1426789Sahrens }
14275326Sek110237 
14288415SRichard.Morris@Sun.COM /* ARGSUSED */
14298415SRichard.Morris@Sun.COM int
14308415SRichard.Morris@Sun.COM dmu_objset_prefetch(char *name, void *arg)
14318415SRichard.Morris@Sun.COM {
14328415SRichard.Morris@Sun.COM 	dsl_dataset_t *ds;
14338415SRichard.Morris@Sun.COM 
14348415SRichard.Morris@Sun.COM 	if (dsl_dataset_hold(name, FTAG, &ds))
14358415SRichard.Morris@Sun.COM 		return (0);
14368415SRichard.Morris@Sun.COM 
14378415SRichard.Morris@Sun.COM 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
14388415SRichard.Morris@Sun.COM 		mutex_enter(&ds->ds_opening_lock);
14398415SRichard.Morris@Sun.COM 		if (!dsl_dataset_get_user_ptr(ds)) {
14408415SRichard.Morris@Sun.COM 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
14418415SRichard.Morris@Sun.COM 			zbookmark_t zb;
14428415SRichard.Morris@Sun.COM 
14438415SRichard.Morris@Sun.COM 			zb.zb_objset = ds->ds_object;
14448415SRichard.Morris@Sun.COM 			zb.zb_object = 0;
14458415SRichard.Morris@Sun.COM 			zb.zb_level = -1;
14468415SRichard.Morris@Sun.COM 			zb.zb_blkid = 0;
14478415SRichard.Morris@Sun.COM 
14488415SRichard.Morris@Sun.COM 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
14498415SRichard.Morris@Sun.COM 			    &ds->ds_phys->ds_bp, NULL, NULL,
14508415SRichard.Morris@Sun.COM 			    ZIO_PRIORITY_ASYNC_READ,
14518415SRichard.Morris@Sun.COM 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
14528415SRichard.Morris@Sun.COM 			    &aflags, &zb);
14538415SRichard.Morris@Sun.COM 		}
14548415SRichard.Morris@Sun.COM 		mutex_exit(&ds->ds_opening_lock);
14558415SRichard.Morris@Sun.COM 	}
14568415SRichard.Morris@Sun.COM 
14578415SRichard.Morris@Sun.COM 	dsl_dataset_rele(ds, FTAG);
14588415SRichard.Morris@Sun.COM 	return (0);
14598415SRichard.Morris@Sun.COM }
14608415SRichard.Morris@Sun.COM 
14615326Sek110237 void
14625326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
14635326Sek110237 {
14645326Sek110237 	ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
14655326Sek110237 	os->os->os_user_ptr = user_ptr;
14665326Sek110237 }
14675326Sek110237 
14685326Sek110237 void *
14695326Sek110237 dmu_objset_get_user(objset_t *os)
14705326Sek110237 {
14715326Sek110237 	ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
14725326Sek110237 	return (os->os->os_user_ptr);
14735326Sek110237 }
1474