xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 10272:a0669934e974)
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 
1679396SMatthew.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);
1719396SMatthew.Ahrens@Sun.COM 	osp->os_flags = BSWAP_64(osp->os_flags);
1729396SMatthew.Ahrens@Sun.COM 	if (size == sizeof (objset_phys_t)) {
1739396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_userused_dnode);
1749396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_groupused_dnode);
1759396SMatthew.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 		}
2189396SMatthew.Ahrens@Sun.COM 
2199396SMatthew.Ahrens@Sun.COM 		/* Increase the blocksize if we are permitted. */
2209396SMatthew.Ahrens@Sun.COM 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
2219396SMatthew.Ahrens@Sun.COM 		    arc_buf_size(osi->os_phys_buf) < sizeof (objset_phys_t)) {
2229396SMatthew.Ahrens@Sun.COM 			arc_buf_t *buf = arc_buf_alloc(spa,
2239396SMatthew.Ahrens@Sun.COM 			    sizeof (objset_phys_t), &osi->os_phys_buf,
2249396SMatthew.Ahrens@Sun.COM 			    ARC_BUFC_METADATA);
2259396SMatthew.Ahrens@Sun.COM 			bzero(buf->b_data, sizeof (objset_phys_t));
2269396SMatthew.Ahrens@Sun.COM 			bcopy(osi->os_phys_buf->b_data, buf->b_data,
2279396SMatthew.Ahrens@Sun.COM 			    arc_buf_size(osi->os_phys_buf));
2289554SMatthew.Ahrens@Sun.COM 			(void) arc_buf_remove_ref(osi->os_phys_buf,
2299554SMatthew.Ahrens@Sun.COM 			    &osi->os_phys_buf);
2309396SMatthew.Ahrens@Sun.COM 			osi->os_phys_buf = buf;
2319396SMatthew.Ahrens@Sun.COM 		}
2329396SMatthew.Ahrens@Sun.COM 
2333547Smaybee 		osi->os_phys = osi->os_phys_buf->b_data;
2349396SMatthew.Ahrens@Sun.COM 		osi->os_flags = osi->os_phys->os_flags;
235789Sahrens 	} else {
2369396SMatthew.Ahrens@Sun.COM 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
2379396SMatthew.Ahrens@Sun.COM 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
2389396SMatthew.Ahrens@Sun.COM 		osi->os_phys_buf = arc_buf_alloc(spa, size,
2393547Smaybee 		    &osi->os_phys_buf, ARC_BUFC_METADATA);
2403547Smaybee 		osi->os_phys = osi->os_phys_buf->b_data;
2419396SMatthew.Ahrens@Sun.COM 		bzero(osi->os_phys, size);
242789Sahrens 	}
243789Sahrens 
244789Sahrens 	/*
245789Sahrens 	 * Note: the changed_cb will be called once before the register
246789Sahrens 	 * func returns, thus changing the checksum/compression from the
2477237Sek110237 	 * default (fletcher2/off).  Snapshots don't need to know about
2487237Sek110237 	 * checksum/compression/copies.
249789Sahrens 	 */
2507237Sek110237 	if (ds) {
2517237Sek110237 		err = dsl_prop_register(ds, "primarycache",
2527237Sek110237 		    primary_cache_changed_cb, osi);
2531544Seschrock 		if (err == 0)
2547237Sek110237 			err = dsl_prop_register(ds, "secondarycache",
2557237Sek110237 			    secondary_cache_changed_cb, osi);
2567237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
2577237Sek110237 			if (err == 0)
2587237Sek110237 				err = dsl_prop_register(ds, "checksum",
2597237Sek110237 				    checksum_changed_cb, osi);
2607237Sek110237 			if (err == 0)
2617237Sek110237 				err = dsl_prop_register(ds, "compression",
2627237Sek110237 				    compression_changed_cb, osi);
2637237Sek110237 			if (err == 0)
2647237Sek110237 				err = dsl_prop_register(ds, "copies",
2657237Sek110237 				    copies_changed_cb, osi);
2667237Sek110237 		}
2671544Seschrock 		if (err) {
2683547Smaybee 			VERIFY(arc_buf_remove_ref(osi->os_phys_buf,
2693547Smaybee 			    &osi->os_phys_buf) == 1);
2701544Seschrock 			kmem_free(osi, sizeof (objset_impl_t));
2711544Seschrock 			return (err);
2721544Seschrock 		}
2732082Seschrock 	} else if (ds == NULL) {
274789Sahrens 		/* It's the meta-objset. */
275789Sahrens 		osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
2761544Seschrock 		osi->os_compress = ZIO_COMPRESS_LZJB;
2773835Sahrens 		osi->os_copies = spa_max_replication(spa);
2787237Sek110237 		osi->os_primary_cache = ZFS_CACHE_ALL;
2797237Sek110237 		osi->os_secondary_cache = ZFS_CACHE_ALL;
280789Sahrens 	}
281789Sahrens 
2827046Sahrens 	osi->os_zil_header = osi->os_phys->os_zil_header;
2837046Sahrens 	osi->os_zil = zil_alloc(&osi->os, &osi->os_zil_header);
284789Sahrens 
285789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
286789Sahrens 		list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t),
287789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
288789Sahrens 		list_create(&osi->os_free_dnodes[i], sizeof (dnode_t),
289789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
290789Sahrens 	}
291789Sahrens 	list_create(&osi->os_dnodes, sizeof (dnode_t),
292789Sahrens 	    offsetof(dnode_t, dn_link));
293789Sahrens 	list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
294789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
295789Sahrens 
2962856Snd150628 	mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL);
2972856Snd150628 	mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
2985326Sek110237 	mutex_init(&osi->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
2992856Snd150628 
300789Sahrens 	osi->os_meta_dnode = dnode_special_open(osi,
301789Sahrens 	    &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
3029396SMatthew.Ahrens@Sun.COM 	if (arc_buf_size(osi->os_phys_buf) >= sizeof (objset_phys_t)) {
3039396SMatthew.Ahrens@Sun.COM 		osi->os_userused_dnode = dnode_special_open(osi,
3049396SMatthew.Ahrens@Sun.COM 		    &osi->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
3059396SMatthew.Ahrens@Sun.COM 		osi->os_groupused_dnode = dnode_special_open(osi,
3069396SMatthew.Ahrens@Sun.COM 		    &osi->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
3079396SMatthew.Ahrens@Sun.COM 	}
308789Sahrens 
3094787Sahrens 	/*
3104787Sahrens 	 * We should be the only thread trying to do this because we
3114787Sahrens 	 * have ds_opening_lock
3124787Sahrens 	 */
3134787Sahrens 	if (ds) {
3144787Sahrens 		VERIFY(NULL == dsl_dataset_set_user_ptr(ds, osi,
3154787Sahrens 		    dmu_objset_evict));
316789Sahrens 	}
317789Sahrens 
3181544Seschrock 	*osip = osi;
3191544Seschrock 	return (0);
320789Sahrens }
321789Sahrens 
3225367Sahrens static int
3235367Sahrens dmu_objset_open_ds_os(dsl_dataset_t *ds, objset_t *os, dmu_objset_type_t type)
3245367Sahrens {
3255367Sahrens 	objset_impl_t *osi;
3265367Sahrens 
3275367Sahrens 	mutex_enter(&ds->ds_opening_lock);
3285367Sahrens 	osi = dsl_dataset_get_user_ptr(ds);
3295367Sahrens 	if (osi == NULL) {
3306689Smaybee 		int err;
3316689Smaybee 
3325367Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
3335367Sahrens 		    ds, &ds->ds_phys->ds_bp, &osi);
3346689Smaybee 		if (err) {
3356689Smaybee 			mutex_exit(&ds->ds_opening_lock);
3365367Sahrens 			return (err);
3376689Smaybee 		}
3385367Sahrens 	}
3395367Sahrens 	mutex_exit(&ds->ds_opening_lock);
3405367Sahrens 
3415367Sahrens 	os->os = osi;
3426689Smaybee 	os->os_mode = DS_MODE_NOHOLD;
3435367Sahrens 
3445367Sahrens 	if (type != DMU_OST_ANY && type != os->os->os_phys->os_type)
3455367Sahrens 		return (EINVAL);
3465367Sahrens 	return (0);
3475367Sahrens }
3485367Sahrens 
3495367Sahrens int
3505367Sahrens dmu_objset_open_ds(dsl_dataset_t *ds, dmu_objset_type_t type, objset_t **osp)
3515367Sahrens {
3525367Sahrens 	objset_t *os;
3535367Sahrens 	int err;
3545367Sahrens 
3555367Sahrens 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
3565367Sahrens 	err = dmu_objset_open_ds_os(ds, os, type);
3575367Sahrens 	if (err)
3585367Sahrens 		kmem_free(os, sizeof (objset_t));
3595367Sahrens 	else
3605367Sahrens 		*osp = os;
3615367Sahrens 	return (err);
3625367Sahrens }
3635367Sahrens 
364789Sahrens /* called from zpl */
365789Sahrens int
366789Sahrens dmu_objset_open(const char *name, dmu_objset_type_t type, int mode,
367789Sahrens     objset_t **osp)
368789Sahrens {
3695326Sek110237 	objset_t *os;
370789Sahrens 	dsl_dataset_t *ds;
371789Sahrens 	int err;
372789Sahrens 
3736689Smaybee 	ASSERT(DS_MODE_TYPE(mode) == DS_MODE_USER ||
3746689Smaybee 	    DS_MODE_TYPE(mode) == DS_MODE_OWNER);
3755367Sahrens 
376789Sahrens 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
3776689Smaybee 	if (DS_MODE_TYPE(mode) == DS_MODE_USER)
3786689Smaybee 		err = dsl_dataset_hold(name, os, &ds);
3796689Smaybee 	else
3806689Smaybee 		err = dsl_dataset_own(name, mode, os, &ds);
381789Sahrens 	if (err) {
382789Sahrens 		kmem_free(os, sizeof (objset_t));
383789Sahrens 		return (err);
384789Sahrens 	}
385789Sahrens 
3865367Sahrens 	err = dmu_objset_open_ds_os(ds, os, type);
3875367Sahrens 	if (err) {
3886689Smaybee 		if (DS_MODE_TYPE(mode) == DS_MODE_USER)
3896689Smaybee 			dsl_dataset_rele(ds, os);
3906689Smaybee 		else
3916689Smaybee 			dsl_dataset_disown(ds, os);
3925367Sahrens 		kmem_free(os, sizeof (objset_t));
3935367Sahrens 	} else {
3946689Smaybee 		os->os_mode = mode;
3955367Sahrens 		*osp = os;
396789Sahrens 	}
3975367Sahrens 	return (err);
398789Sahrens }
399789Sahrens 
400789Sahrens void
401789Sahrens dmu_objset_close(objset_t *os)
402789Sahrens {
4036689Smaybee 	ASSERT(DS_MODE_TYPE(os->os_mode) == DS_MODE_USER ||
4046689Smaybee 	    DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER ||
4056689Smaybee 	    DS_MODE_TYPE(os->os_mode) == DS_MODE_NOHOLD);
4066689Smaybee 
4076689Smaybee 	if (DS_MODE_TYPE(os->os_mode) == DS_MODE_USER)
4086689Smaybee 		dsl_dataset_rele(os->os->os_dsl_dataset, os);
4096689Smaybee 	else if (DS_MODE_TYPE(os->os_mode) == DS_MODE_OWNER)
4106689Smaybee 		dsl_dataset_disown(os->os->os_dsl_dataset, os);
411789Sahrens 	kmem_free(os, sizeof (objset_t));
412789Sahrens }
413789Sahrens 
4141646Sperrin int
4154944Smaybee dmu_objset_evict_dbufs(objset_t *os)
4161544Seschrock {
4171544Seschrock 	objset_impl_t *osi = os->os;
4181544Seschrock 	dnode_t *dn;
4191596Sahrens 
4201596Sahrens 	mutex_enter(&osi->os_lock);
4211596Sahrens 
4221596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
4231596Sahrens 	list_remove(&osi->os_dnodes, osi->os_meta_dnode);
4241596Sahrens 	list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode);
4251544Seschrock 
4261544Seschrock 	/*
4271596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
4281596Sahrens 	 * because dnode_add_ref() only works if you already have a
4291596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
4301596Sahrens 	 * skip.
4311544Seschrock 	 */
4321596Sahrens 	for (dn = list_head(&osi->os_dnodes);
4334944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
4341596Sahrens 	    dn = list_next(&osi->os_dnodes, dn))
4351596Sahrens 		continue;
4361596Sahrens 
4371596Sahrens 	while (dn) {
4381596Sahrens 		dnode_t *next_dn = dn;
4391596Sahrens 
4401596Sahrens 		do {
4411596Sahrens 			next_dn = list_next(&osi->os_dnodes, next_dn);
4424944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
4431596Sahrens 
4441596Sahrens 		mutex_exit(&osi->os_lock);
4454944Smaybee 		dnode_evict_dbufs(dn);
4461596Sahrens 		dnode_rele(dn, FTAG);
4471596Sahrens 		mutex_enter(&osi->os_lock);
4481596Sahrens 		dn = next_dn;
4491544Seschrock 	}
4501544Seschrock 	mutex_exit(&osi->os_lock);
4514944Smaybee 	return (list_head(&osi->os_dnodes) != osi->os_meta_dnode);
4521544Seschrock }
4531544Seschrock 
4541544Seschrock void
455789Sahrens dmu_objset_evict(dsl_dataset_t *ds, void *arg)
456789Sahrens {
457789Sahrens 	objset_impl_t *osi = arg;
4581544Seschrock 	objset_t os;
4592082Seschrock 	int i;
460789Sahrens 
461789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
462789Sahrens 		ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL);
463789Sahrens 		ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL);
464789Sahrens 	}
465789Sahrens 
4667237Sek110237 	if (ds) {
4677237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
4687237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
4697237Sek110237 			    checksum_changed_cb, osi));
4707237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
4717237Sek110237 			    compression_changed_cb, osi));
4727237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
4737237Sek110237 			    copies_changed_cb, osi));
4747237Sek110237 		}
4757237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
4767237Sek110237 		    primary_cache_changed_cb, osi));
4777237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
4787237Sek110237 		    secondary_cache_changed_cb, osi));
479789Sahrens 	}
480789Sahrens 
4811544Seschrock 	/*
4821544Seschrock 	 * We should need only a single pass over the dnode list, since
4831544Seschrock 	 * nothing can be added to the list at this point.
4841544Seschrock 	 */
4851544Seschrock 	os.os = osi;
4864944Smaybee 	(void) dmu_objset_evict_dbufs(&os);
4871544Seschrock 
4889396SMatthew.Ahrens@Sun.COM 	dnode_special_close(osi->os_meta_dnode);
4899396SMatthew.Ahrens@Sun.COM 	if (osi->os_userused_dnode) {
4909396SMatthew.Ahrens@Sun.COM 		dnode_special_close(osi->os_userused_dnode);
4919396SMatthew.Ahrens@Sun.COM 		dnode_special_close(osi->os_groupused_dnode);
4929396SMatthew.Ahrens@Sun.COM 	}
4939396SMatthew.Ahrens@Sun.COM 	zil_free(osi->os_zil);
494789Sahrens 
4959396SMatthew.Ahrens@Sun.COM 	ASSERT3P(list_head(&osi->os_dnodes), ==, NULL);
496789Sahrens 
4973547Smaybee 	VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1);
4982856Snd150628 	mutex_destroy(&osi->os_lock);
4992856Snd150628 	mutex_destroy(&osi->os_obj_lock);
5005326Sek110237 	mutex_destroy(&osi->os_user_ptr_lock);
501789Sahrens 	kmem_free(osi, sizeof (objset_impl_t));
502789Sahrens }
503789Sahrens 
504789Sahrens /* called from dsl for meta-objset */
505789Sahrens objset_impl_t *
5063547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
5073547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
508789Sahrens {
509789Sahrens 	objset_impl_t *osi;
510789Sahrens 	dnode_t *mdn;
511789Sahrens 
512789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
5134787Sahrens 	if (ds)
5144787Sahrens 		mutex_enter(&ds->ds_opening_lock);
5153547Smaybee 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi));
5164787Sahrens 	if (ds)
5174787Sahrens 		mutex_exit(&ds->ds_opening_lock);
518789Sahrens 	mdn = osi->os_meta_dnode;
519789Sahrens 
520789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
521789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
522789Sahrens 
523789Sahrens 	/*
524789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
525789Sahrens 	 * later, because then we could do it in quescing context while
526789Sahrens 	 * we are also accessing it in open context.
527789Sahrens 	 *
528789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
529789Sahrens 	 * because the MOS is only updated in syncing context.
530789Sahrens 	 * This is most fortunate: the MOS is the only objset that
531789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
532789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
533789Sahrens 	 */
5341544Seschrock 	if (ds != NULL) {
5351544Seschrock 		int levels = 1;
5361544Seschrock 
5371544Seschrock 		/*
5381544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
5391544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
5401544Seschrock 		 */
5411544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
5421544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
5431544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
5441544Seschrock 			levels++;
5451544Seschrock 
546789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
5471544Seschrock 		    mdn->dn_nlevels = levels;
5481544Seschrock 	}
549789Sahrens 
550789Sahrens 	ASSERT(type != DMU_OST_NONE);
551789Sahrens 	ASSERT(type != DMU_OST_ANY);
552789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
553789Sahrens 	osi->os_phys->os_type = type;
5549396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(osi)) {
5559396SMatthew.Ahrens@Sun.COM 		osi->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
5569396SMatthew.Ahrens@Sun.COM 		osi->os_flags = osi->os_phys->os_flags;
5579396SMatthew.Ahrens@Sun.COM 	}
558789Sahrens 
559789Sahrens 	dsl_dataset_dirty(ds, tx);
560789Sahrens 
561789Sahrens 	return (osi);
562789Sahrens }
563789Sahrens 
564789Sahrens struct oscarg {
5654543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
566789Sahrens 	void *userarg;
567*10272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *clone_origin;
568789Sahrens 	const char *lastname;
569789Sahrens 	dmu_objset_type_t type;
5706492Stimh 	uint64_t flags;
571789Sahrens };
572789Sahrens 
5734543Smarks /*ARGSUSED*/
574789Sahrens static int
5752199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
576789Sahrens {
5772199Sahrens 	dsl_dir_t *dd = arg1;
5782199Sahrens 	struct oscarg *oa = arg2;
5792199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
5802199Sahrens 	int err;
5812199Sahrens 	uint64_t ddobj;
5822199Sahrens 
5832199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
5842199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
5852199Sahrens 	if (err != ENOENT)
5862199Sahrens 		return (err ? err : EEXIST);
5872199Sahrens 
588*10272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin != NULL) {
589*10272SMatthew.Ahrens@Sun.COM 		/* You can't clone across pools. */
590*10272SMatthew.Ahrens@Sun.COM 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
5912199Sahrens 			return (EXDEV);
5922199Sahrens 
593*10272SMatthew.Ahrens@Sun.COM 		/* You can only clone snapshots, not the head datasets. */
594*10272SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
5952199Sahrens 			return (EINVAL);
5962199Sahrens 	}
5974543Smarks 
5982199Sahrens 	return (0);
5992199Sahrens }
6002199Sahrens 
6012199Sahrens static void
6024543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
6032199Sahrens {
6042199Sahrens 	dsl_dir_t *dd = arg1;
6052199Sahrens 	struct oscarg *oa = arg2;
6062199Sahrens 	uint64_t dsobj;
607789Sahrens 
608789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
609789Sahrens 
6102199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
611*10272SMatthew.Ahrens@Sun.COM 	    oa->clone_origin, oa->flags, cr, tx);
612789Sahrens 
613*10272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin == NULL) {
614*10272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
615*10272SMatthew.Ahrens@Sun.COM 		blkptr_t *bp;
616789Sahrens 		objset_impl_t *osi;
617789Sahrens 
618*10272SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
619*10272SMatthew.Ahrens@Sun.COM 		    FTAG, &ds));
620*10272SMatthew.Ahrens@Sun.COM 		bp = dsl_dataset_get_blkptr(ds);
621*10272SMatthew.Ahrens@Sun.COM 		ASSERT(BP_IS_HOLE(bp));
622*10272SMatthew.Ahrens@Sun.COM 
623789Sahrens 		osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
6243547Smaybee 		    ds, bp, oa->type, tx);
625789Sahrens 
626789Sahrens 		if (oa->userfunc)
6274543Smarks 			oa->userfunc(&osi->os, oa->userarg, cr, tx);
628*10272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
629789Sahrens 	}
6304543Smarks 
6314543Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
6324543Smarks 	    tx, cr, "dataset = %llu", dsobj);
633789Sahrens }
634789Sahrens 
635789Sahrens int
636*10272SMatthew.Ahrens@Sun.COM dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
6374543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
638789Sahrens {
6392199Sahrens 	dsl_dir_t *pdd;
640789Sahrens 	const char *tail;
641789Sahrens 	int err = 0;
6422199Sahrens 	struct oscarg oa = { 0 };
643789Sahrens 
6442199Sahrens 	ASSERT(strchr(name, '@') == NULL);
6452199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
6461544Seschrock 	if (err)
6471544Seschrock 		return (err);
648789Sahrens 	if (tail == NULL) {
6492199Sahrens 		dsl_dir_close(pdd, FTAG);
650789Sahrens 		return (EEXIST);
651789Sahrens 	}
652789Sahrens 
6532199Sahrens 	oa.userfunc = func;
6542199Sahrens 	oa.userarg = arg;
6552199Sahrens 	oa.lastname = tail;
6562199Sahrens 	oa.type = type;
6576492Stimh 	oa.flags = flags;
6584543Smarks 
659*10272SMatthew.Ahrens@Sun.COM 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
660*10272SMatthew.Ahrens@Sun.COM 	    dmu_objset_create_sync, pdd, &oa, 5);
661*10272SMatthew.Ahrens@Sun.COM 	dsl_dir_close(pdd, FTAG);
662*10272SMatthew.Ahrens@Sun.COM 	return (err);
663*10272SMatthew.Ahrens@Sun.COM }
664*10272SMatthew.Ahrens@Sun.COM 
665*10272SMatthew.Ahrens@Sun.COM int
666*10272SMatthew.Ahrens@Sun.COM dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
667*10272SMatthew.Ahrens@Sun.COM {
668*10272SMatthew.Ahrens@Sun.COM 	dsl_dir_t *pdd;
669*10272SMatthew.Ahrens@Sun.COM 	const char *tail;
670*10272SMatthew.Ahrens@Sun.COM 	int err = 0;
671*10272SMatthew.Ahrens@Sun.COM 	struct oscarg oa = { 0 };
672*10272SMatthew.Ahrens@Sun.COM 
673*10272SMatthew.Ahrens@Sun.COM 	ASSERT(strchr(name, '@') == NULL);
674*10272SMatthew.Ahrens@Sun.COM 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
675*10272SMatthew.Ahrens@Sun.COM 	if (err)
676*10272SMatthew.Ahrens@Sun.COM 		return (err);
677*10272SMatthew.Ahrens@Sun.COM 	if (tail == NULL) {
678*10272SMatthew.Ahrens@Sun.COM 		dsl_dir_close(pdd, FTAG);
679*10272SMatthew.Ahrens@Sun.COM 		return (EEXIST);
680789Sahrens 	}
681*10272SMatthew.Ahrens@Sun.COM 
682*10272SMatthew.Ahrens@Sun.COM 	oa.lastname = tail;
683*10272SMatthew.Ahrens@Sun.COM 	oa.clone_origin = clone_origin;
684*10272SMatthew.Ahrens@Sun.COM 	oa.flags = flags;
685*10272SMatthew.Ahrens@Sun.COM 
6862199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
6872199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
6882199Sahrens 	dsl_dir_close(pdd, FTAG);
689789Sahrens 	return (err);
690789Sahrens }
691789Sahrens 
692789Sahrens int
69310242Schris.kirby@sun.com dmu_objset_destroy(const char *name, boolean_t defer)
694789Sahrens {
695789Sahrens 	objset_t *os;
696789Sahrens 	int error;
697789Sahrens 
698789Sahrens 	/*
699*10272SMatthew.Ahrens@Sun.COM 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
700*10272SMatthew.Ahrens@Sun.COM 	 * intent log, but if there is an active log, it has blocks that
701*10272SMatthew.Ahrens@Sun.COM 	 * are allocated, but may not yet be reflected in the on-disk
702*10272SMatthew.Ahrens@Sun.COM 	 * structure.  Only the ZIL knows how to free them, so we have
703*10272SMatthew.Ahrens@Sun.COM 	 * to call into it here.
704789Sahrens 	 */
7055367Sahrens 	error = dmu_objset_open(name, DMU_OST_ANY,
7066689Smaybee 	    DS_MODE_OWNER|DS_MODE_READONLY|DS_MODE_INCONSISTENT, &os);
707789Sahrens 	if (error == 0) {
7085367Sahrens 		dsl_dataset_t *ds = os->os->os_dsl_dataset;
7091807Sbonwick 		zil_destroy(dmu_objset_zil(os), B_FALSE);
7105367Sahrens 
71110242Schris.kirby@sun.com 		error = dsl_dataset_destroy(ds, os, defer);
712*10272SMatthew.Ahrens@Sun.COM 		/* dsl_dataset_destroy() closes the ds. */
7135367Sahrens 		kmem_free(os, sizeof (objset_t));
714789Sahrens 	}
715789Sahrens 
7165367Sahrens 	return (error);
717789Sahrens }
718789Sahrens 
7192199Sahrens struct snaparg {
7202199Sahrens 	dsl_sync_task_group_t *dstg;
7212199Sahrens 	char *snapname;
7222199Sahrens 	char failed[MAXPATHLEN];
7234543Smarks 	boolean_t checkperms;
7249355SMatthew.Ahrens@Sun.COM 	nvlist_t *props;
7255367Sahrens };
7265367Sahrens 
7279355SMatthew.Ahrens@Sun.COM static int
7289355SMatthew.Ahrens@Sun.COM snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
7299355SMatthew.Ahrens@Sun.COM {
7309355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
7319355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7329355SMatthew.Ahrens@Sun.COM 
7339355SMatthew.Ahrens@Sun.COM 	/* The props have already been checked by zfs_check_userprops(). */
7349355SMatthew.Ahrens@Sun.COM 
7359355SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_snapshot_check(os->os->os_dsl_dataset,
7369355SMatthew.Ahrens@Sun.COM 	    sn->snapname, tx));
7379355SMatthew.Ahrens@Sun.COM }
7389355SMatthew.Ahrens@Sun.COM 
7399355SMatthew.Ahrens@Sun.COM static void
7409355SMatthew.Ahrens@Sun.COM snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
7419355SMatthew.Ahrens@Sun.COM {
7429355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
7439355SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
7449355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7459355SMatthew.Ahrens@Sun.COM 
7469355SMatthew.Ahrens@Sun.COM 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
7479355SMatthew.Ahrens@Sun.COM 
7489355SMatthew.Ahrens@Sun.COM 	if (sn->props)
7499355SMatthew.Ahrens@Sun.COM 		dsl_props_set_sync(ds->ds_prev, sn->props, cr, tx);
7509355SMatthew.Ahrens@Sun.COM }
7512199Sahrens 
7522199Sahrens static int
7532199Sahrens dmu_objset_snapshot_one(char *name, void *arg)
7542199Sahrens {
7552199Sahrens 	struct snaparg *sn = arg;
7562199Sahrens 	objset_t *os;
7572199Sahrens 	int err;
7582199Sahrens 
7592199Sahrens 	(void) strcpy(sn->failed, name);
7602199Sahrens 
7614543Smarks 	/*
7624543Smarks 	 * Check permissions only when requested.  This only applies when
7634543Smarks 	 * doing a recursive snapshot.  The permission checks for the starting
7644543Smarks 	 * dataset have already been performed in zfs_secpolicy_snapshot()
7654543Smarks 	 */
7664543Smarks 	if (sn->checkperms == B_TRUE &&
7674543Smarks 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
7684543Smarks 		return (err);
7694543Smarks 
7706689Smaybee 	err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_USER, &os);
7712199Sahrens 	if (err != 0)
7722199Sahrens 		return (err);
7732199Sahrens 
7746689Smaybee 	/* If the objset is in an inconsistent state, return busy */
7756689Smaybee 	if (os->os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
7763637Srm160521 		dmu_objset_close(os);
7773637Srm160521 		return (EBUSY);
7783637Srm160521 	}
7793637Srm160521 
7803637Srm160521 	/*
7812199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
7822199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
7832199Sahrens 	 * a side effect.
7842199Sahrens 	 */
7852199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
7862199Sahrens 	if (err == 0) {
7879355SMatthew.Ahrens@Sun.COM 		dsl_sync_task_create(sn->dstg, snapshot_check,
7889355SMatthew.Ahrens@Sun.COM 		    snapshot_sync, os, sn, 3);
7893637Srm160521 	} else {
7903637Srm160521 		dmu_objset_close(os);
7912199Sahrens 	}
7923637Srm160521 
7932199Sahrens 	return (err);
7942199Sahrens }
7952199Sahrens 
7962199Sahrens int
7979355SMatthew.Ahrens@Sun.COM dmu_objset_snapshot(char *fsname, char *snapname,
7989355SMatthew.Ahrens@Sun.COM     nvlist_t *props, boolean_t recursive)
7992199Sahrens {
8002199Sahrens 	dsl_sync_task_t *dst;
8019355SMatthew.Ahrens@Sun.COM 	struct snaparg sn;
8022199Sahrens 	spa_t *spa;
8032199Sahrens 	int err;
8042199Sahrens 
8052199Sahrens 	(void) strcpy(sn.failed, fsname);
8062199Sahrens 
8074603Sahrens 	err = spa_open(fsname, &spa, FTAG);
8082199Sahrens 	if (err)
8092199Sahrens 		return (err);
8102199Sahrens 
8112199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8122199Sahrens 	sn.snapname = snapname;
8139355SMatthew.Ahrens@Sun.COM 	sn.props = props;
8142199Sahrens 
8152417Sahrens 	if (recursive) {
8164543Smarks 		sn.checkperms = B_TRUE;
8172417Sahrens 		err = dmu_objset_find(fsname,
8182417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
8192417Sahrens 	} else {
8204543Smarks 		sn.checkperms = B_FALSE;
8212199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
8222417Sahrens 	}
8232199Sahrens 
8249355SMatthew.Ahrens@Sun.COM 	if (err == 0)
8259355SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_group_wait(sn.dstg);
8262199Sahrens 
8272199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
8282199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
8299355SMatthew.Ahrens@Sun.COM 		objset_t *os = dst->dst_arg1;
8309355SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = os->os->os_dsl_dataset;
8312199Sahrens 		if (dst->dst_err)
8325367Sahrens 			dsl_dataset_name(ds, sn.failed);
8339355SMatthew.Ahrens@Sun.COM 		zil_resume(dmu_objset_zil(os));
8349355SMatthew.Ahrens@Sun.COM 		dmu_objset_close(os);
8352199Sahrens 	}
8365367Sahrens 
8372199Sahrens 	if (err)
8382199Sahrens 		(void) strcpy(fsname, sn.failed);
8392199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
8402199Sahrens 	spa_close(spa, FTAG);
8412199Sahrens 	return (err);
8422199Sahrens }
8432199Sahrens 
844789Sahrens static void
8459396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
846789Sahrens {
8473547Smaybee 	dnode_t *dn;
848789Sahrens 
8493547Smaybee 	while (dn = list_head(list)) {
8503547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
8513547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
8523547Smaybee 		/*
8539396SMatthew.Ahrens@Sun.COM 		 * Initialize dn_zio outside dnode_sync() because the
8549396SMatthew.Ahrens@Sun.COM 		 * meta-dnode needs to set it ouside dnode_sync().
8553547Smaybee 		 */
8563547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
8573547Smaybee 		ASSERT(dn->dn_zio);
858789Sahrens 
8593547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
8603547Smaybee 		list_remove(list, dn);
8619396SMatthew.Ahrens@Sun.COM 
8629396SMatthew.Ahrens@Sun.COM 		if (newlist) {
8639396SMatthew.Ahrens@Sun.COM 			(void) dnode_add_ref(dn, newlist);
8649396SMatthew.Ahrens@Sun.COM 			list_insert_tail(newlist, dn);
8659396SMatthew.Ahrens@Sun.COM 		}
8669396SMatthew.Ahrens@Sun.COM 
8673547Smaybee 		dnode_sync(dn, tx);
8683547Smaybee 	}
8693547Smaybee }
8702981Sahrens 
8713547Smaybee /* ARGSUSED */
8723547Smaybee static void
8733547Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg)
8743547Smaybee {
8757754SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
8767754SJeff.Bonwick@Sun.COM 	blkptr_t *bp_orig = &zio->io_bp_orig;
8773547Smaybee 	objset_impl_t *os = arg;
8783547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
8792981Sahrens 
8807754SJeff.Bonwick@Sun.COM 	ASSERT(bp == os->os_rootbp);
8817754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
8827754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(bp) == 0);
8835329Sgw25295 
8843547Smaybee 	/*
8859396SMatthew.Ahrens@Sun.COM 	 * Update rootbp fill count: it should be the number of objects
8869396SMatthew.Ahrens@Sun.COM 	 * allocated in the object set (not counting the "special"
8879396SMatthew.Ahrens@Sun.COM 	 * objects that are stored in the objset_phys_t -- the meta
8889396SMatthew.Ahrens@Sun.COM 	 * dnode and user/group accounting objects).
8893547Smaybee 	 */
8909396SMatthew.Ahrens@Sun.COM 	bp->blk_fill = 0;
8917754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < dnp->dn_nblkptr; i++)
8923547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
8935329Sgw25295 
8947754SJeff.Bonwick@Sun.COM 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
8957754SJeff.Bonwick@Sun.COM 		ASSERT(DVA_EQUAL(BP_IDENTITY(bp), BP_IDENTITY(bp_orig)));
8967754SJeff.Bonwick@Sun.COM 	} else {
8975329Sgw25295 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
8986992Smaybee 			(void) dsl_dataset_block_kill(os->os_dsl_dataset,
8997754SJeff.Bonwick@Sun.COM 			    &zio->io_bp_orig, zio, os->os_synctx);
9005329Sgw25295 		dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx);
9015329Sgw25295 	}
902789Sahrens }
903789Sahrens 
904789Sahrens /* called from dsl */
905789Sahrens void
9063547Smaybee dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx)
907789Sahrens {
908789Sahrens 	int txgoff;
9091544Seschrock 	zbookmark_t zb;
9107046Sahrens 	writeprops_t wp = { 0 };
9113547Smaybee 	zio_t *zio;
9123547Smaybee 	list_t *list;
9139396SMatthew.Ahrens@Sun.COM 	list_t *newlist = NULL;
9143547Smaybee 	dbuf_dirty_record_t *dr;
9153547Smaybee 
9163547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
917789Sahrens 
918789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
919789Sahrens 	/* XXX the write_done callback should really give us the tx... */
920789Sahrens 	os->os_synctx = tx;
921789Sahrens 
9223882Sahrens 	if (os->os_dsl_dataset == NULL) {
9233882Sahrens 		/*
9243882Sahrens 		 * This is the MOS.  If we have upgraded,
9253882Sahrens 		 * spa_max_replication() could change, so reset
9263882Sahrens 		 * os_copies here.
9273882Sahrens 		 */
9283882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
9293882Sahrens 	}
9303882Sahrens 
9313547Smaybee 	/*
9323547Smaybee 	 * Create the root block IO
9333547Smaybee 	 */
9343547Smaybee 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
9353547Smaybee 	zb.zb_object = 0;
9367754SJeff.Bonwick@Sun.COM 	zb.zb_level = -1;	/* for block ordering; it's level 0 on disk */
9373547Smaybee 	zb.zb_blkid = 0;
9387754SJeff.Bonwick@Sun.COM 
9397754SJeff.Bonwick@Sun.COM 	wp.wp_type = DMU_OT_OBJSET;
9407754SJeff.Bonwick@Sun.COM 	wp.wp_level = 0;	/* on-disk BP level; see above */
9417754SJeff.Bonwick@Sun.COM 	wp.wp_copies = os->os_copies;
9427754SJeff.Bonwick@Sun.COM 	wp.wp_oschecksum = os->os_checksum;
9437754SJeff.Bonwick@Sun.COM 	wp.wp_oscompress = os->os_compress;
9447754SJeff.Bonwick@Sun.COM 
9454787Sahrens 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
9466992Smaybee 		(void) dsl_dataset_block_kill(os->os_dsl_dataset,
9473547Smaybee 		    os->os_rootbp, pio, tx);
9484787Sahrens 	}
9497754SJeff.Bonwick@Sun.COM 
9507046Sahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
9519396SMatthew.Ahrens@Sun.COM 
9527754SJeff.Bonwick@Sun.COM 	zio = arc_write(pio, os->os_spa, &wp, DMU_OS_IS_L2CACHEABLE(os),
9537754SJeff.Bonwick@Sun.COM 	    tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os,
9547754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
9553547Smaybee 
9563547Smaybee 	/*
9579396SMatthew.Ahrens@Sun.COM 	 * Sync special dnodes - the parent IO for the sync is the root block
9583547Smaybee 	 */
9593547Smaybee 	os->os_meta_dnode->dn_zio = zio;
9603547Smaybee 	dnode_sync(os->os_meta_dnode, tx);
961789Sahrens 
9629396SMatthew.Ahrens@Sun.COM 	os->os_phys->os_flags = os->os_flags;
9639396SMatthew.Ahrens@Sun.COM 
9649396SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode &&
9659396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
9669396SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode->dn_zio = zio;
9679396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_userused_dnode, tx);
9689396SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode->dn_zio = zio;
9699396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_groupused_dnode, tx);
9709396SMatthew.Ahrens@Sun.COM 	}
9719396SMatthew.Ahrens@Sun.COM 
972789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
973789Sahrens 
9749396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
9759396SMatthew.Ahrens@Sun.COM 		newlist = &os->os_synced_dnodes;
9769396SMatthew.Ahrens@Sun.COM 		/*
9779396SMatthew.Ahrens@Sun.COM 		 * We must create the list here because it uses the
9789396SMatthew.Ahrens@Sun.COM 		 * dn_dirty_link[] of this txg.
9799396SMatthew.Ahrens@Sun.COM 		 */
9809396SMatthew.Ahrens@Sun.COM 		list_create(newlist, sizeof (dnode_t),
9819396SMatthew.Ahrens@Sun.COM 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
9829396SMatthew.Ahrens@Sun.COM 	}
9839396SMatthew.Ahrens@Sun.COM 
9849396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
9859396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
986789Sahrens 
9873547Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
9883547Smaybee 	while (dr = list_head(list)) {
9893547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
9903547Smaybee 		list_remove(list, dr);
9913547Smaybee 		if (dr->dr_zio)
9923547Smaybee 			zio_nowait(dr->dr_zio);
9933547Smaybee 	}
994789Sahrens 	/*
995789Sahrens 	 * Free intent log blocks up to this tx.
996789Sahrens 	 */
997789Sahrens 	zil_sync(os->os_zil, tx);
9987046Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
9993547Smaybee 	zio_nowait(zio);
1000789Sahrens }
1001789Sahrens 
10029396SMatthew.Ahrens@Sun.COM static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
10039396SMatthew.Ahrens@Sun.COM 
10049396SMatthew.Ahrens@Sun.COM void
10059396SMatthew.Ahrens@Sun.COM dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
10069396SMatthew.Ahrens@Sun.COM {
10079396SMatthew.Ahrens@Sun.COM 	used_cbs[ost] = cb;
10089396SMatthew.Ahrens@Sun.COM }
10099396SMatthew.Ahrens@Sun.COM 
10109396SMatthew.Ahrens@Sun.COM boolean_t
10119396SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(objset_impl_t *os)
10129396SMatthew.Ahrens@Sun.COM {
10139396SMatthew.Ahrens@Sun.COM 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
10149396SMatthew.Ahrens@Sun.COM 	    used_cbs[os->os_phys->os_type] &&
10159396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode);
10169396SMatthew.Ahrens@Sun.COM }
10179396SMatthew.Ahrens@Sun.COM 
10189396SMatthew.Ahrens@Sun.COM void
10199396SMatthew.Ahrens@Sun.COM dmu_objset_do_userquota_callbacks(objset_impl_t *os, dmu_tx_t *tx)
10209396SMatthew.Ahrens@Sun.COM {
10219396SMatthew.Ahrens@Sun.COM 	dnode_t *dn;
10229396SMatthew.Ahrens@Sun.COM 	list_t *list = &os->os_synced_dnodes;
10239396SMatthew.Ahrens@Sun.COM 	static const char zerobuf[DN_MAX_BONUSLEN] = {0};
10249396SMatthew.Ahrens@Sun.COM 
10259396SMatthew.Ahrens@Sun.COM 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
10269396SMatthew.Ahrens@Sun.COM 
10279396SMatthew.Ahrens@Sun.COM 	while (dn = list_head(list)) {
10289396SMatthew.Ahrens@Sun.COM 		dmu_object_type_t bonustype;
10299396SMatthew.Ahrens@Sun.COM 
10309396SMatthew.Ahrens@Sun.COM 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
10319396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_oldphys);
10329396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
10339396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_flags &
10349396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED);
10359396SMatthew.Ahrens@Sun.COM 
10369396SMatthew.Ahrens@Sun.COM 		/* Allocate the user/groupused objects if necessary. */
10379396SMatthew.Ahrens@Sun.COM 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
10389396SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(&os->os,
10399396SMatthew.Ahrens@Sun.COM 			    DMU_USERUSED_OBJECT,
10409396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
10419396SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(&os->os,
10429396SMatthew.Ahrens@Sun.COM 			    DMU_GROUPUSED_OBJECT,
10439396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
10449396SMatthew.Ahrens@Sun.COM 		}
10459396SMatthew.Ahrens@Sun.COM 
10469396SMatthew.Ahrens@Sun.COM 		/*
10479396SMatthew.Ahrens@Sun.COM 		 * If the object was not previously
10489396SMatthew.Ahrens@Sun.COM 		 * accounted, pretend that it was free.
10499396SMatthew.Ahrens@Sun.COM 		 */
10509396SMatthew.Ahrens@Sun.COM 		if (!(dn->dn_oldphys->dn_flags &
10519396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED)) {
10529396SMatthew.Ahrens@Sun.COM 			bzero(dn->dn_oldphys, sizeof (dnode_phys_t));
10539396SMatthew.Ahrens@Sun.COM 		}
10549396SMatthew.Ahrens@Sun.COM 
10559396SMatthew.Ahrens@Sun.COM 		/*
10569396SMatthew.Ahrens@Sun.COM 		 * If the object was freed, use the previous bonustype.
10579396SMatthew.Ahrens@Sun.COM 		 */
10589396SMatthew.Ahrens@Sun.COM 		bonustype = dn->dn_phys->dn_bonustype ?
10599396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_bonustype : dn->dn_oldphys->dn_bonustype;
10609396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type != 0 ||
10619396SMatthew.Ahrens@Sun.COM 		    (bcmp(DN_BONUS(dn->dn_phys), zerobuf,
10629396SMatthew.Ahrens@Sun.COM 		    DN_MAX_BONUSLEN) == 0 &&
10639396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_phys) == 0));
10649396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_oldphys->dn_type != 0 ||
10659396SMatthew.Ahrens@Sun.COM 		    (bcmp(DN_BONUS(dn->dn_oldphys), zerobuf,
10669396SMatthew.Ahrens@Sun.COM 		    DN_MAX_BONUSLEN) == 0 &&
10679396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_oldphys) == 0));
10689396SMatthew.Ahrens@Sun.COM 		used_cbs[os->os_phys->os_type](&os->os, bonustype,
10699396SMatthew.Ahrens@Sun.COM 		    DN_BONUS(dn->dn_oldphys), DN_BONUS(dn->dn_phys),
10709396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_oldphys),
10719396SMatthew.Ahrens@Sun.COM 		    DN_USED_BYTES(dn->dn_phys), tx);
10729396SMatthew.Ahrens@Sun.COM 
10739396SMatthew.Ahrens@Sun.COM 		/*
10749396SMatthew.Ahrens@Sun.COM 		 * The mutex is needed here for interlock with dnode_allocate.
10759396SMatthew.Ahrens@Sun.COM 		 */
10769396SMatthew.Ahrens@Sun.COM 		mutex_enter(&dn->dn_mtx);
10779396SMatthew.Ahrens@Sun.COM 		zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t));
10789396SMatthew.Ahrens@Sun.COM 		dn->dn_oldphys = NULL;
10799396SMatthew.Ahrens@Sun.COM 		mutex_exit(&dn->dn_mtx);
10809396SMatthew.Ahrens@Sun.COM 
10819396SMatthew.Ahrens@Sun.COM 		list_remove(list, dn);
10829396SMatthew.Ahrens@Sun.COM 		dnode_rele(dn, list);
10839396SMatthew.Ahrens@Sun.COM 	}
10849396SMatthew.Ahrens@Sun.COM }
10859396SMatthew.Ahrens@Sun.COM 
10869396SMatthew.Ahrens@Sun.COM boolean_t
10879396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(objset_t *os)
10889396SMatthew.Ahrens@Sun.COM {
10899396SMatthew.Ahrens@Sun.COM 	return (os->os->os_phys->os_flags &
10909396SMatthew.Ahrens@Sun.COM 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
10919396SMatthew.Ahrens@Sun.COM }
10929396SMatthew.Ahrens@Sun.COM 
10939396SMatthew.Ahrens@Sun.COM int
10949396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_upgrade(objset_t *os)
10959396SMatthew.Ahrens@Sun.COM {
10969396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
10979396SMatthew.Ahrens@Sun.COM 	int err = 0;
10989396SMatthew.Ahrens@Sun.COM 
10999396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userspace_present(os))
11009396SMatthew.Ahrens@Sun.COM 		return (0);
11019396SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userused_enabled(os->os))
11029396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
11039396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_is_snapshot(os))
11049396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
11059396SMatthew.Ahrens@Sun.COM 
11069396SMatthew.Ahrens@Sun.COM 	/*
11079396SMatthew.Ahrens@Sun.COM 	 * We simply need to mark every object dirty, so that it will be
11089396SMatthew.Ahrens@Sun.COM 	 * synced out and now accounted.  If this is called
11099396SMatthew.Ahrens@Sun.COM 	 * concurrently, or if we already did some work before crashing,
11109396SMatthew.Ahrens@Sun.COM 	 * that's fine, since we track each object's accounted state
11119396SMatthew.Ahrens@Sun.COM 	 * independently.
11129396SMatthew.Ahrens@Sun.COM 	 */
11139396SMatthew.Ahrens@Sun.COM 
11149396SMatthew.Ahrens@Sun.COM 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
11159951SLin.Ling@Sun.COM 		dmu_tx_t *tx;
11169396SMatthew.Ahrens@Sun.COM 		dmu_buf_t *db;
11179396SMatthew.Ahrens@Sun.COM 		int objerr;
11189396SMatthew.Ahrens@Sun.COM 
11199396SMatthew.Ahrens@Sun.COM 		if (issig(JUSTLOOKING) && issig(FORREAL))
11209396SMatthew.Ahrens@Sun.COM 			return (EINTR);
11219396SMatthew.Ahrens@Sun.COM 
11229396SMatthew.Ahrens@Sun.COM 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
11239396SMatthew.Ahrens@Sun.COM 		if (objerr)
11249396SMatthew.Ahrens@Sun.COM 			continue;
11259951SLin.Ling@Sun.COM 		tx = dmu_tx_create(os);
11269396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_bonus(tx, obj);
11279396SMatthew.Ahrens@Sun.COM 		objerr = dmu_tx_assign(tx, TXG_WAIT);
11289396SMatthew.Ahrens@Sun.COM 		if (objerr) {
11299396SMatthew.Ahrens@Sun.COM 			dmu_tx_abort(tx);
11309396SMatthew.Ahrens@Sun.COM 			continue;
11319396SMatthew.Ahrens@Sun.COM 		}
11329396SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(db, tx);
11339396SMatthew.Ahrens@Sun.COM 		dmu_buf_rele(db, FTAG);
11349396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
11359396SMatthew.Ahrens@Sun.COM 	}
11369396SMatthew.Ahrens@Sun.COM 
11379396SMatthew.Ahrens@Sun.COM 	os->os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
11389396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
11399396SMatthew.Ahrens@Sun.COM 	return (0);
11409396SMatthew.Ahrens@Sun.COM }
11419396SMatthew.Ahrens@Sun.COM 
1142789Sahrens void
11432885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
11442885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
11452885Sahrens {
11462885Sahrens 	dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp,
11472885Sahrens 	    usedobjsp, availobjsp);
11482885Sahrens }
11492885Sahrens 
11502885Sahrens uint64_t
11512885Sahrens dmu_objset_fsid_guid(objset_t *os)
11522885Sahrens {
11532885Sahrens 	return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset));
11542885Sahrens }
11552885Sahrens 
11562885Sahrens void
11572885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1158789Sahrens {
11592885Sahrens 	stat->dds_type = os->os->os_phys->os_type;
11602885Sahrens 	if (os->os->os_dsl_dataset)
11612885Sahrens 		dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat);
11622885Sahrens }
11632885Sahrens 
11642885Sahrens void
11652885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
11662885Sahrens {
11672885Sahrens 	ASSERT(os->os->os_dsl_dataset ||
11682885Sahrens 	    os->os->os_phys->os_type == DMU_OST_META);
11692885Sahrens 
11702885Sahrens 	if (os->os->os_dsl_dataset != NULL)
11712885Sahrens 		dsl_dataset_stats(os->os->os_dsl_dataset, nv);
11722885Sahrens 
11732885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
11742885Sahrens 	    os->os->os_phys->os_type);
11759396SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
11769396SMatthew.Ahrens@Sun.COM 	    dmu_objset_userspace_present(os));
1177789Sahrens }
1178789Sahrens 
1179789Sahrens int
1180789Sahrens dmu_objset_is_snapshot(objset_t *os)
1181789Sahrens {
1182789Sahrens 	if (os->os->os_dsl_dataset != NULL)
1183789Sahrens 		return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset));
1184789Sahrens 	else
1185789Sahrens 		return (B_FALSE);
1186789Sahrens }
1187789Sahrens 
1188789Sahrens int
11896492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
11906492Stimh     boolean_t *conflict)
11916492Stimh {
11926492Stimh 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
11936492Stimh 	uint64_t ignored;
11946492Stimh 
11956492Stimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
11966492Stimh 		return (ENOENT);
11976492Stimh 
11986492Stimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
11996492Stimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
12006492Stimh 	    real, maxlen, conflict));
12016492Stimh }
12026492Stimh 
12036492Stimh int
1204789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
12055663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1206789Sahrens {
1207789Sahrens 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
1208789Sahrens 	zap_cursor_t cursor;
1209789Sahrens 	zap_attribute_t attr;
1210789Sahrens 
1211789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1212789Sahrens 		return (ENOENT);
1213789Sahrens 
1214789Sahrens 	zap_cursor_init_serialized(&cursor,
1215789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1216789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1217789Sahrens 
1218885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1219885Sahrens 		zap_cursor_fini(&cursor);
1220885Sahrens 		return (ENOENT);
1221885Sahrens 	}
1222885Sahrens 
1223885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1224885Sahrens 		zap_cursor_fini(&cursor);
1225885Sahrens 		return (ENAMETOOLONG);
1226885Sahrens 	}
1227885Sahrens 
1228885Sahrens 	(void) strcpy(name, attr.za_name);
1229885Sahrens 	if (idp)
1230885Sahrens 		*idp = attr.za_first_integer;
12315663Sck153898 	if (case_conflict)
12325663Sck153898 		*case_conflict = attr.za_normalization_conflict;
1233885Sahrens 	zap_cursor_advance(&cursor);
1234885Sahrens 	*offp = zap_cursor_serialize(&cursor);
1235885Sahrens 	zap_cursor_fini(&cursor);
1236885Sahrens 
1237885Sahrens 	return (0);
1238885Sahrens }
1239885Sahrens 
1240885Sahrens int
1241885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
1242885Sahrens     uint64_t *idp, uint64_t *offp)
1243885Sahrens {
1244885Sahrens 	dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir;
1245885Sahrens 	zap_cursor_t cursor;
1246885Sahrens 	zap_attribute_t attr;
1247885Sahrens 
1248885Sahrens 	/* there is no next dir on a snapshot! */
1249885Sahrens 	if (os->os->os_dsl_dataset->ds_object !=
1250885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
1251885Sahrens 		return (ENOENT);
1252885Sahrens 
1253885Sahrens 	zap_cursor_init_serialized(&cursor,
1254885Sahrens 	    dd->dd_pool->dp_meta_objset,
1255885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1256885Sahrens 
1257885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1258885Sahrens 		zap_cursor_fini(&cursor);
1259885Sahrens 		return (ENOENT);
1260885Sahrens 	}
1261885Sahrens 
1262885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1263885Sahrens 		zap_cursor_fini(&cursor);
1264789Sahrens 		return (ENAMETOOLONG);
1265885Sahrens 	}
1266789Sahrens 
1267789Sahrens 	(void) strcpy(name, attr.za_name);
1268885Sahrens 	if (idp)
1269885Sahrens 		*idp = attr.za_first_integer;
1270789Sahrens 	zap_cursor_advance(&cursor);
1271789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1272885Sahrens 	zap_cursor_fini(&cursor);
1273789Sahrens 
1274789Sahrens 	return (0);
1275789Sahrens }
1276789Sahrens 
12777046Sahrens struct findarg {
12787046Sahrens 	int (*func)(char *, void *);
12797046Sahrens 	void *arg;
12807046Sahrens };
12817046Sahrens 
12827046Sahrens /* ARGSUSED */
12837046Sahrens static int
12847046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
12857046Sahrens {
12867046Sahrens 	struct findarg *fa = arg;
12877046Sahrens 	return (fa->func((char *)dsname, fa->arg));
12887046Sahrens }
12897046Sahrens 
1290789Sahrens /*
1291789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
12927046Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1293789Sahrens  */
12942199Sahrens int
12952199Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1296789Sahrens {
12977046Sahrens 	struct findarg fa;
12987046Sahrens 	fa.func = func;
12997046Sahrens 	fa.arg = arg;
13007046Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
13017046Sahrens }
13027046Sahrens 
13037046Sahrens /*
13047046Sahrens  * Find all objsets under name, call func on each
13057046Sahrens  */
13067046Sahrens int
13077046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
13087046Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
13097046Sahrens {
1310789Sahrens 	dsl_dir_t *dd;
13117046Sahrens 	dsl_pool_t *dp;
13127046Sahrens 	dsl_dataset_t *ds;
1313789Sahrens 	zap_cursor_t zc;
13143978Smmusante 	zap_attribute_t *attr;
1315789Sahrens 	char *child;
13167046Sahrens 	uint64_t thisobj;
13177046Sahrens 	int err;
1318789Sahrens 
13197046Sahrens 	if (name == NULL)
13207046Sahrens 		name = spa_name(spa);
13217046Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
13221544Seschrock 	if (err)
13232199Sahrens 		return (err);
1324789Sahrens 
13257046Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
13267046Sahrens 	if (dd->dd_myname[0] == '$') {
13277046Sahrens 		dsl_dir_close(dd, FTAG);
13287046Sahrens 		return (0);
13297046Sahrens 	}
13307046Sahrens 
13317046Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
13323978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
13337046Sahrens 	dp = dd->dd_pool;
1334789Sahrens 
1335789Sahrens 	/*
1336789Sahrens 	 * Iterate over all children.
1337789Sahrens 	 */
13382417Sahrens 	if (flags & DS_FIND_CHILDREN) {
13397046Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
13402417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
13413978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
13422417Sahrens 		    (void) zap_cursor_advance(&zc)) {
13433978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
13443978Smmusante 			ASSERT(attr->za_num_integers == 1);
1345789Sahrens 
13462417Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
13477046Sahrens 			(void) strcpy(child, name);
13482417Sahrens 			(void) strcat(child, "/");
13493978Smmusante 			(void) strcat(child, attr->za_name);
13507046Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
13512417Sahrens 			kmem_free(child, MAXPATHLEN);
13522417Sahrens 			if (err)
13532417Sahrens 				break;
13542417Sahrens 		}
13552417Sahrens 		zap_cursor_fini(&zc);
13562199Sahrens 
13572417Sahrens 		if (err) {
13582417Sahrens 			dsl_dir_close(dd, FTAG);
13593978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
13602417Sahrens 			return (err);
13612417Sahrens 		}
1362789Sahrens 	}
1363789Sahrens 
1364789Sahrens 	/*
1365789Sahrens 	 * Iterate over all snapshots.
1366789Sahrens 	 */
13677046Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
13687046Sahrens 		if (!dsl_pool_sync_context(dp))
13697046Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
13707046Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
13717046Sahrens 		if (!dsl_pool_sync_context(dp))
13727046Sahrens 			rw_exit(&dp->dp_config_rwlock);
1373789Sahrens 
13747046Sahrens 		if (err == 0) {
13757046Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
13767046Sahrens 			dsl_dataset_rele(ds, FTAG);
1377789Sahrens 
13787046Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
13797046Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
13807046Sahrens 			    (void) zap_cursor_advance(&zc)) {
13817046Sahrens 				ASSERT(attr->za_integer_length ==
13827046Sahrens 				    sizeof (uint64_t));
13837046Sahrens 				ASSERT(attr->za_num_integers == 1);
1384789Sahrens 
13857046Sahrens 				child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
13867046Sahrens 				(void) strcpy(child, name);
13877046Sahrens 				(void) strcat(child, "@");
13887046Sahrens 				(void) strcat(child, attr->za_name);
13897046Sahrens 				err = func(spa, attr->za_first_integer,
13907046Sahrens 				    child, arg);
13917046Sahrens 				kmem_free(child, MAXPATHLEN);
13927046Sahrens 				if (err)
13937046Sahrens 					break;
13947046Sahrens 			}
13957046Sahrens 			zap_cursor_fini(&zc);
1396789Sahrens 		}
1397789Sahrens 	}
1398789Sahrens 
1399789Sahrens 	dsl_dir_close(dd, FTAG);
14003978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1401789Sahrens 
14022199Sahrens 	if (err)
14032199Sahrens 		return (err);
14042199Sahrens 
1405789Sahrens 	/*
1406789Sahrens 	 * Apply to self if appropriate.
1407789Sahrens 	 */
14087046Sahrens 	err = func(spa, thisobj, name, arg);
14092199Sahrens 	return (err);
1410789Sahrens }
14115326Sek110237 
14128415SRichard.Morris@Sun.COM /* ARGSUSED */
14138415SRichard.Morris@Sun.COM int
14148415SRichard.Morris@Sun.COM dmu_objset_prefetch(char *name, void *arg)
14158415SRichard.Morris@Sun.COM {
14168415SRichard.Morris@Sun.COM 	dsl_dataset_t *ds;
14178415SRichard.Morris@Sun.COM 
14188415SRichard.Morris@Sun.COM 	if (dsl_dataset_hold(name, FTAG, &ds))
14198415SRichard.Morris@Sun.COM 		return (0);
14208415SRichard.Morris@Sun.COM 
14218415SRichard.Morris@Sun.COM 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
14228415SRichard.Morris@Sun.COM 		mutex_enter(&ds->ds_opening_lock);
14238415SRichard.Morris@Sun.COM 		if (!dsl_dataset_get_user_ptr(ds)) {
14248415SRichard.Morris@Sun.COM 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
14258415SRichard.Morris@Sun.COM 			zbookmark_t zb;
14268415SRichard.Morris@Sun.COM 
14278415SRichard.Morris@Sun.COM 			zb.zb_objset = ds->ds_object;
14288415SRichard.Morris@Sun.COM 			zb.zb_object = 0;
14298415SRichard.Morris@Sun.COM 			zb.zb_level = -1;
14308415SRichard.Morris@Sun.COM 			zb.zb_blkid = 0;
14318415SRichard.Morris@Sun.COM 
14328415SRichard.Morris@Sun.COM 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
14338415SRichard.Morris@Sun.COM 			    &ds->ds_phys->ds_bp, NULL, NULL,
14348415SRichard.Morris@Sun.COM 			    ZIO_PRIORITY_ASYNC_READ,
14358415SRichard.Morris@Sun.COM 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
14368415SRichard.Morris@Sun.COM 			    &aflags, &zb);
14378415SRichard.Morris@Sun.COM 		}
14388415SRichard.Morris@Sun.COM 		mutex_exit(&ds->ds_opening_lock);
14398415SRichard.Morris@Sun.COM 	}
14408415SRichard.Morris@Sun.COM 
14418415SRichard.Morris@Sun.COM 	dsl_dataset_rele(ds, FTAG);
14428415SRichard.Morris@Sun.COM 	return (0);
14438415SRichard.Morris@Sun.COM }
14448415SRichard.Morris@Sun.COM 
14455326Sek110237 void
14465326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
14475326Sek110237 {
14485326Sek110237 	ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
14495326Sek110237 	os->os->os_user_ptr = user_ptr;
14505326Sek110237 }
14515326Sek110237 
14525326Sek110237 void *
14535326Sek110237 dmu_objset_get_user(objset_t *os)
14545326Sek110237 {
14555326Sek110237 	ASSERT(MUTEX_HELD(&os->os->os_user_ptr_lock));
14565326Sek110237 	return (os->os->os_user_ptr);
14575326Sek110237 }
1458