xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 11620:ebeec53e6212)
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 /*
22*11620SMatthew.Ahrens@Sun.COM  * Copyright 2010 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/zap.h>
40789Sahrens #include <sys/zil.h>
41789Sahrens #include <sys/dmu_impl.h>
424543Smarks #include <sys/zfs_ioctl.h>
4311165SMatthew.Ahrens@Sun.COM #include <sys/sunddi.h>
44789Sahrens 
45789Sahrens spa_t *
46789Sahrens dmu_objset_spa(objset_t *os)
47789Sahrens {
4810298SMatthew.Ahrens@Sun.COM 	return (os->os_spa);
49789Sahrens }
50789Sahrens 
51789Sahrens zilog_t *
52789Sahrens dmu_objset_zil(objset_t *os)
53789Sahrens {
5410298SMatthew.Ahrens@Sun.COM 	return (os->os_zil);
55789Sahrens }
56789Sahrens 
57789Sahrens dsl_pool_t *
58789Sahrens dmu_objset_pool(objset_t *os)
59789Sahrens {
60789Sahrens 	dsl_dataset_t *ds;
61789Sahrens 
6210298SMatthew.Ahrens@Sun.COM 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
63789Sahrens 		return (ds->ds_dir->dd_pool);
64789Sahrens 	else
6510298SMatthew.Ahrens@Sun.COM 		return (spa_get_dsl(os->os_spa));
66789Sahrens }
67789Sahrens 
68789Sahrens dsl_dataset_t *
69789Sahrens dmu_objset_ds(objset_t *os)
70789Sahrens {
7110298SMatthew.Ahrens@Sun.COM 	return (os->os_dsl_dataset);
72789Sahrens }
73789Sahrens 
74789Sahrens dmu_objset_type_t
75789Sahrens dmu_objset_type(objset_t *os)
76789Sahrens {
7710298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_type);
78789Sahrens }
79789Sahrens 
80789Sahrens void
81789Sahrens dmu_objset_name(objset_t *os, char *buf)
82789Sahrens {
8310298SMatthew.Ahrens@Sun.COM 	dsl_dataset_name(os->os_dsl_dataset, buf);
84789Sahrens }
85789Sahrens 
86789Sahrens uint64_t
87789Sahrens dmu_objset_id(objset_t *os)
88789Sahrens {
8910298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
90789Sahrens 
91789Sahrens 	return (ds ? ds->ds_object : 0);
92789Sahrens }
93789Sahrens 
9410310SNeil.Perrin@Sun.COM uint64_t
9510310SNeil.Perrin@Sun.COM dmu_objset_logbias(objset_t *os)
9610310SNeil.Perrin@Sun.COM {
9710310SNeil.Perrin@Sun.COM 	return (os->os_logbias);
9810310SNeil.Perrin@Sun.COM }
9910310SNeil.Perrin@Sun.COM 
100789Sahrens static void
101789Sahrens checksum_changed_cb(void *arg, uint64_t newval)
102789Sahrens {
10310298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
104789Sahrens 
105789Sahrens 	/*
106789Sahrens 	 * Inheritance should have been done by now.
107789Sahrens 	 */
108789Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
109789Sahrens 
11010298SMatthew.Ahrens@Sun.COM 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
111789Sahrens }
112789Sahrens 
113789Sahrens static void
114789Sahrens compression_changed_cb(void *arg, uint64_t newval)
115789Sahrens {
11610298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
117789Sahrens 
118789Sahrens 	/*
119789Sahrens 	 * Inheritance and range checking should have been done by now.
120789Sahrens 	 */
121789Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
122789Sahrens 
12310298SMatthew.Ahrens@Sun.COM 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
124789Sahrens }
125789Sahrens 
1263835Sahrens static void
1273835Sahrens copies_changed_cb(void *arg, uint64_t newval)
1283835Sahrens {
12910298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1303835Sahrens 
1313835Sahrens 	/*
1323835Sahrens 	 * Inheritance and range checking should have been done by now.
1333835Sahrens 	 */
1343835Sahrens 	ASSERT(newval > 0);
13510298SMatthew.Ahrens@Sun.COM 	ASSERT(newval <= spa_max_replication(os->os_spa));
1363835Sahrens 
13710298SMatthew.Ahrens@Sun.COM 	os->os_copies = newval;
1383835Sahrens }
1393835Sahrens 
1407237Sek110237 static void
14110922SJeff.Bonwick@Sun.COM dedup_changed_cb(void *arg, uint64_t newval)
14210922SJeff.Bonwick@Sun.COM {
14310922SJeff.Bonwick@Sun.COM 	objset_t *os = arg;
14410922SJeff.Bonwick@Sun.COM 	spa_t *spa = os->os_spa;
14510922SJeff.Bonwick@Sun.COM 	enum zio_checksum checksum;
14610922SJeff.Bonwick@Sun.COM 
14710922SJeff.Bonwick@Sun.COM 	/*
14810922SJeff.Bonwick@Sun.COM 	 * Inheritance should have been done by now.
14910922SJeff.Bonwick@Sun.COM 	 */
15010922SJeff.Bonwick@Sun.COM 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
15110922SJeff.Bonwick@Sun.COM 
15210922SJeff.Bonwick@Sun.COM 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
15310922SJeff.Bonwick@Sun.COM 
15410922SJeff.Bonwick@Sun.COM 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
15510922SJeff.Bonwick@Sun.COM 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
15610922SJeff.Bonwick@Sun.COM }
15710922SJeff.Bonwick@Sun.COM 
15810922SJeff.Bonwick@Sun.COM static void
1597237Sek110237 primary_cache_changed_cb(void *arg, uint64_t newval)
1607237Sek110237 {
16110298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1627237Sek110237 
1637237Sek110237 	/*
1647237Sek110237 	 * Inheritance and range checking should have been done by now.
1657237Sek110237 	 */
1667237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1677237Sek110237 	    newval == ZFS_CACHE_METADATA);
1687237Sek110237 
16910298SMatthew.Ahrens@Sun.COM 	os->os_primary_cache = newval;
1707237Sek110237 }
1717237Sek110237 
1727237Sek110237 static void
1737237Sek110237 secondary_cache_changed_cb(void *arg, uint64_t newval)
1747237Sek110237 {
17510298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
1767237Sek110237 
1777237Sek110237 	/*
1787237Sek110237 	 * Inheritance and range checking should have been done by now.
1797237Sek110237 	 */
1807237Sek110237 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1817237Sek110237 	    newval == ZFS_CACHE_METADATA);
1827237Sek110237 
18310298SMatthew.Ahrens@Sun.COM 	os->os_secondary_cache = newval;
1847237Sek110237 }
1857237Sek110237 
18610310SNeil.Perrin@Sun.COM static void
18710310SNeil.Perrin@Sun.COM logbias_changed_cb(void *arg, uint64_t newval)
18810310SNeil.Perrin@Sun.COM {
18910310SNeil.Perrin@Sun.COM 	objset_t *os = arg;
19010310SNeil.Perrin@Sun.COM 
19110310SNeil.Perrin@Sun.COM 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
19210310SNeil.Perrin@Sun.COM 	    newval == ZFS_LOGBIAS_THROUGHPUT);
19310310SNeil.Perrin@Sun.COM 	os->os_logbias = newval;
19410310SNeil.Perrin@Sun.COM 	if (os->os_zil)
19510310SNeil.Perrin@Sun.COM 		zil_set_logbias(os->os_zil, newval);
19610310SNeil.Perrin@Sun.COM }
19710310SNeil.Perrin@Sun.COM 
198789Sahrens void
199789Sahrens dmu_objset_byteswap(void *buf, size_t size)
200789Sahrens {
201789Sahrens 	objset_phys_t *osp = buf;
202789Sahrens 
2039396SMatthew.Ahrens@Sun.COM 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
204789Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
205789Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
206789Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
2079396SMatthew.Ahrens@Sun.COM 	osp->os_flags = BSWAP_64(osp->os_flags);
2089396SMatthew.Ahrens@Sun.COM 	if (size == sizeof (objset_phys_t)) {
2099396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_userused_dnode);
2109396SMatthew.Ahrens@Sun.COM 		dnode_byteswap(&osp->os_groupused_dnode);
2119396SMatthew.Ahrens@Sun.COM 	}
212789Sahrens }
213789Sahrens 
2141544Seschrock int
2151544Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
21610298SMatthew.Ahrens@Sun.COM     objset_t **osp)
217789Sahrens {
21810298SMatthew.Ahrens@Sun.COM 	objset_t *os;
2197046Sahrens 	int i, err;
220789Sahrens 
2214787Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
2224787Sahrens 
22310298SMatthew.Ahrens@Sun.COM 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
22410298SMatthew.Ahrens@Sun.COM 	os->os_dsl_dataset = ds;
22510298SMatthew.Ahrens@Sun.COM 	os->os_spa = spa;
22610298SMatthew.Ahrens@Sun.COM 	os->os_rootbp = bp;
22710298SMatthew.Ahrens@Sun.COM 	if (!BP_IS_HOLE(os->os_rootbp)) {
2282391Smaybee 		uint32_t aflags = ARC_WAIT;
2291544Seschrock 		zbookmark_t zb;
23010922SJeff.Bonwick@Sun.COM 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
23110922SJeff.Bonwick@Sun.COM 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
23210922SJeff.Bonwick@Sun.COM 
23310298SMatthew.Ahrens@Sun.COM 		if (DMU_OS_IS_L2CACHEABLE(os))
2347237Sek110237 			aflags |= ARC_L2CACHE;
2351544Seschrock 
23610298SMatthew.Ahrens@Sun.COM 		dprintf_bp(os->os_rootbp, "reading %s", "");
2377046Sahrens 		/*
2387046Sahrens 		 * NB: when bprewrite scrub can change the bp,
2397046Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
2407046Sahrens 		 * could change, and we'll need a lock.
2417046Sahrens 		 */
24210298SMatthew.Ahrens@Sun.COM 		err = arc_read_nolock(NULL, spa, os->os_rootbp,
24310298SMatthew.Ahrens@Sun.COM 		    arc_getbuf_func, &os->os_phys_buf,
2442391Smaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
2451544Seschrock 		if (err) {
24610298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
2477294Sperrin 			/* convert checksum errors into IO errors */
2487294Sperrin 			if (err == ECKSUM)
2497294Sperrin 				err = EIO;
2501544Seschrock 			return (err);
2511544Seschrock 		}
2529396SMatthew.Ahrens@Sun.COM 
2539396SMatthew.Ahrens@Sun.COM 		/* Increase the blocksize if we are permitted. */
2549396SMatthew.Ahrens@Sun.COM 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
25510298SMatthew.Ahrens@Sun.COM 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
2569396SMatthew.Ahrens@Sun.COM 			arc_buf_t *buf = arc_buf_alloc(spa,
25710298SMatthew.Ahrens@Sun.COM 			    sizeof (objset_phys_t), &os->os_phys_buf,
2589396SMatthew.Ahrens@Sun.COM 			    ARC_BUFC_METADATA);
2599396SMatthew.Ahrens@Sun.COM 			bzero(buf->b_data, sizeof (objset_phys_t));
26010298SMatthew.Ahrens@Sun.COM 			bcopy(os->os_phys_buf->b_data, buf->b_data,
26110298SMatthew.Ahrens@Sun.COM 			    arc_buf_size(os->os_phys_buf));
26210298SMatthew.Ahrens@Sun.COM 			(void) arc_buf_remove_ref(os->os_phys_buf,
26310298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf);
26410298SMatthew.Ahrens@Sun.COM 			os->os_phys_buf = buf;
2659396SMatthew.Ahrens@Sun.COM 		}
2669396SMatthew.Ahrens@Sun.COM 
26710298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
26810298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
269789Sahrens 	} else {
2709396SMatthew.Ahrens@Sun.COM 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
2719396SMatthew.Ahrens@Sun.COM 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
27210298SMatthew.Ahrens@Sun.COM 		os->os_phys_buf = arc_buf_alloc(spa, size,
27310298SMatthew.Ahrens@Sun.COM 		    &os->os_phys_buf, ARC_BUFC_METADATA);
27410298SMatthew.Ahrens@Sun.COM 		os->os_phys = os->os_phys_buf->b_data;
27510298SMatthew.Ahrens@Sun.COM 		bzero(os->os_phys, size);
276789Sahrens 	}
277789Sahrens 
278789Sahrens 	/*
279789Sahrens 	 * Note: the changed_cb will be called once before the register
280789Sahrens 	 * func returns, thus changing the checksum/compression from the
2817237Sek110237 	 * default (fletcher2/off).  Snapshots don't need to know about
2827237Sek110237 	 * checksum/compression/copies.
283789Sahrens 	 */
2847237Sek110237 	if (ds) {
2857237Sek110237 		err = dsl_prop_register(ds, "primarycache",
28610298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os);
2871544Seschrock 		if (err == 0)
2887237Sek110237 			err = dsl_prop_register(ds, "secondarycache",
28910298SMatthew.Ahrens@Sun.COM 			    secondary_cache_changed_cb, os);
2907237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
2917237Sek110237 			if (err == 0)
2927237Sek110237 				err = dsl_prop_register(ds, "checksum",
29310298SMatthew.Ahrens@Sun.COM 				    checksum_changed_cb, os);
2947237Sek110237 			if (err == 0)
2957237Sek110237 				err = dsl_prop_register(ds, "compression",
29610298SMatthew.Ahrens@Sun.COM 				    compression_changed_cb, os);
2977237Sek110237 			if (err == 0)
2987237Sek110237 				err = dsl_prop_register(ds, "copies",
29910298SMatthew.Ahrens@Sun.COM 				    copies_changed_cb, os);
30010310SNeil.Perrin@Sun.COM 			if (err == 0)
30110922SJeff.Bonwick@Sun.COM 				err = dsl_prop_register(ds, "dedup",
30210922SJeff.Bonwick@Sun.COM 				    dedup_changed_cb, os);
30310922SJeff.Bonwick@Sun.COM 			if (err == 0)
30410310SNeil.Perrin@Sun.COM 				err = dsl_prop_register(ds, "logbias",
30510310SNeil.Perrin@Sun.COM 				    logbias_changed_cb, os);
3067237Sek110237 		}
3071544Seschrock 		if (err) {
30810298SMatthew.Ahrens@Sun.COM 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
30910298SMatthew.Ahrens@Sun.COM 			    &os->os_phys_buf) == 1);
31010298SMatthew.Ahrens@Sun.COM 			kmem_free(os, sizeof (objset_t));
3111544Seschrock 			return (err);
3121544Seschrock 		}
3132082Seschrock 	} else if (ds == NULL) {
314789Sahrens 		/* It's the meta-objset. */
31510298SMatthew.Ahrens@Sun.COM 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
31610298SMatthew.Ahrens@Sun.COM 		os->os_compress = ZIO_COMPRESS_LZJB;
31710298SMatthew.Ahrens@Sun.COM 		os->os_copies = spa_max_replication(spa);
31810922SJeff.Bonwick@Sun.COM 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
31910922SJeff.Bonwick@Sun.COM 		os->os_dedup_verify = 0;
32010922SJeff.Bonwick@Sun.COM 		os->os_logbias = 0;
32110298SMatthew.Ahrens@Sun.COM 		os->os_primary_cache = ZFS_CACHE_ALL;
32210298SMatthew.Ahrens@Sun.COM 		os->os_secondary_cache = ZFS_CACHE_ALL;
323789Sahrens 	}
324789Sahrens 
32510298SMatthew.Ahrens@Sun.COM 	os->os_zil_header = os->os_phys->os_zil_header;
32610298SMatthew.Ahrens@Sun.COM 	os->os_zil = zil_alloc(os, &os->os_zil_header);
327789Sahrens 
328789Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
32910298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
330789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
33110298SMatthew.Ahrens@Sun.COM 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
332789Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
333789Sahrens 	}
33410298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_dnodes, sizeof (dnode_t),
335789Sahrens 	    offsetof(dnode_t, dn_link));
33610298SMatthew.Ahrens@Sun.COM 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
337789Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
338789Sahrens 
33910298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
34010298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
34110298SMatthew.Ahrens@Sun.COM 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
3422856Snd150628 
34310298SMatthew.Ahrens@Sun.COM 	os->os_meta_dnode = dnode_special_open(os,
34410298SMatthew.Ahrens@Sun.COM 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
34510298SMatthew.Ahrens@Sun.COM 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
34610298SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode = dnode_special_open(os,
34710298SMatthew.Ahrens@Sun.COM 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
34810298SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode = dnode_special_open(os,
34910298SMatthew.Ahrens@Sun.COM 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
3509396SMatthew.Ahrens@Sun.COM 	}
351789Sahrens 
3524787Sahrens 	/*
3534787Sahrens 	 * We should be the only thread trying to do this because we
3544787Sahrens 	 * have ds_opening_lock
3554787Sahrens 	 */
3564787Sahrens 	if (ds) {
35710298SMatthew.Ahrens@Sun.COM 		mutex_enter(&ds->ds_lock);
35810298SMatthew.Ahrens@Sun.COM 		ASSERT(ds->ds_objset == NULL);
35910298SMatthew.Ahrens@Sun.COM 		ds->ds_objset = os;
36010298SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_lock);
361789Sahrens 	}
362789Sahrens 
36310298SMatthew.Ahrens@Sun.COM 	*osp = os;
3645367Sahrens 	return (0);
3655367Sahrens }
3665367Sahrens 
3675367Sahrens int
36810298SMatthew.Ahrens@Sun.COM dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
3695367Sahrens {
37010298SMatthew.Ahrens@Sun.COM 	int err = 0;
37110298SMatthew.Ahrens@Sun.COM 
37210298SMatthew.Ahrens@Sun.COM 	mutex_enter(&ds->ds_opening_lock);
37310298SMatthew.Ahrens@Sun.COM 	*osp = ds->ds_objset;
37410298SMatthew.Ahrens@Sun.COM 	if (*osp == NULL) {
37510298SMatthew.Ahrens@Sun.COM 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
37610298SMatthew.Ahrens@Sun.COM 		    ds, &ds->ds_phys->ds_bp, osp);
37710298SMatthew.Ahrens@Sun.COM 	}
37810298SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_opening_lock);
37910298SMatthew.Ahrens@Sun.COM 	return (err);
38010298SMatthew.Ahrens@Sun.COM }
38110298SMatthew.Ahrens@Sun.COM 
38210298SMatthew.Ahrens@Sun.COM /* called from zpl */
38310298SMatthew.Ahrens@Sun.COM int
38410298SMatthew.Ahrens@Sun.COM dmu_objset_hold(const char *name, void *tag, objset_t **osp)
38510298SMatthew.Ahrens@Sun.COM {
38610298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
3875367Sahrens 	int err;
3885367Sahrens 
38910298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(name, tag, &ds);
3905367Sahrens 	if (err)
39110298SMatthew.Ahrens@Sun.COM 		return (err);
39210298SMatthew.Ahrens@Sun.COM 
39310298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
39410298SMatthew.Ahrens@Sun.COM 	if (err)
39510298SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, tag);
39610298SMatthew.Ahrens@Sun.COM 
3975367Sahrens 	return (err);
3985367Sahrens }
3995367Sahrens 
400789Sahrens /* called from zpl */
401789Sahrens int
40210298SMatthew.Ahrens@Sun.COM dmu_objset_own(const char *name, dmu_objset_type_t type,
40310298SMatthew.Ahrens@Sun.COM     boolean_t readonly, void *tag, objset_t **osp)
404789Sahrens {
405789Sahrens 	dsl_dataset_t *ds;
406789Sahrens 	int err;
407789Sahrens 
40810298SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
40910298SMatthew.Ahrens@Sun.COM 	if (err)
41010298SMatthew.Ahrens@Sun.COM 		return (err);
4115367Sahrens 
41210298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_from_ds(ds, osp);
413789Sahrens 	if (err) {
41410298SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(ds, tag);
41510588SEric.Taylor@Sun.COM 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
41610298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(*osp, tag);
41710298SMatthew.Ahrens@Sun.COM 		return (EINVAL);
41810588SEric.Taylor@Sun.COM 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
41910588SEric.Taylor@Sun.COM 		dmu_objset_disown(*osp, tag);
42010588SEric.Taylor@Sun.COM 		return (EROFS);
421789Sahrens 	}
4225367Sahrens 	return (err);
423789Sahrens }
424789Sahrens 
425789Sahrens void
42610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(objset_t *os, void *tag)
427789Sahrens {
42810298SMatthew.Ahrens@Sun.COM 	dsl_dataset_rele(os->os_dsl_dataset, tag);
42910298SMatthew.Ahrens@Sun.COM }
4306689Smaybee 
43110298SMatthew.Ahrens@Sun.COM void
43210298SMatthew.Ahrens@Sun.COM dmu_objset_disown(objset_t *os, void *tag)
43310298SMatthew.Ahrens@Sun.COM {
43410298SMatthew.Ahrens@Sun.COM 	dsl_dataset_disown(os->os_dsl_dataset, tag);
435789Sahrens }
436789Sahrens 
4371646Sperrin int
4384944Smaybee dmu_objset_evict_dbufs(objset_t *os)
4391544Seschrock {
4401544Seschrock 	dnode_t *dn;
4411596Sahrens 
44210298SMatthew.Ahrens@Sun.COM 	mutex_enter(&os->os_lock);
4431596Sahrens 
4441596Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
44510298SMatthew.Ahrens@Sun.COM 	list_remove(&os->os_dnodes, os->os_meta_dnode);
44610298SMatthew.Ahrens@Sun.COM 	list_insert_tail(&os->os_dnodes, os->os_meta_dnode);
4471544Seschrock 
4481544Seschrock 	/*
4491596Sahrens 	 * Find the first dnode with holds.  We have to do this dance
4501596Sahrens 	 * because dnode_add_ref() only works if you already have a
4511596Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
4521596Sahrens 	 * skip.
4531544Seschrock 	 */
45410298SMatthew.Ahrens@Sun.COM 	for (dn = list_head(&os->os_dnodes);
4554944Smaybee 	    dn && !dnode_add_ref(dn, FTAG);
45610298SMatthew.Ahrens@Sun.COM 	    dn = list_next(&os->os_dnodes, dn))
4571596Sahrens 		continue;
4581596Sahrens 
4591596Sahrens 	while (dn) {
4601596Sahrens 		dnode_t *next_dn = dn;
4611596Sahrens 
4621596Sahrens 		do {
46310298SMatthew.Ahrens@Sun.COM 			next_dn = list_next(&os->os_dnodes, next_dn);
4644944Smaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
4651596Sahrens 
46610298SMatthew.Ahrens@Sun.COM 		mutex_exit(&os->os_lock);
4674944Smaybee 		dnode_evict_dbufs(dn);
4681596Sahrens 		dnode_rele(dn, FTAG);
46910298SMatthew.Ahrens@Sun.COM 		mutex_enter(&os->os_lock);
4701596Sahrens 		dn = next_dn;
4711544Seschrock 	}
47210298SMatthew.Ahrens@Sun.COM 	mutex_exit(&os->os_lock);
47310298SMatthew.Ahrens@Sun.COM 	return (list_head(&os->os_dnodes) != os->os_meta_dnode);
4741544Seschrock }
4751544Seschrock 
4761544Seschrock void
47710298SMatthew.Ahrens@Sun.COM dmu_objset_evict(objset_t *os)
478789Sahrens {
47910298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
480789Sahrens 
48110922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < TXG_SIZE; t++)
48210922SJeff.Bonwick@Sun.COM 		ASSERT(!dmu_objset_is_dirty(os, t));
483789Sahrens 
4847237Sek110237 	if (ds) {
4857237Sek110237 		if (!dsl_dataset_is_snapshot(ds)) {
4867237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
48710298SMatthew.Ahrens@Sun.COM 			    checksum_changed_cb, os));
4887237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
48910298SMatthew.Ahrens@Sun.COM 			    compression_changed_cb, os));
4907237Sek110237 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
49110298SMatthew.Ahrens@Sun.COM 			    copies_changed_cb, os));
49210922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "dedup",
49310922SJeff.Bonwick@Sun.COM 			    dedup_changed_cb, os));
49410310SNeil.Perrin@Sun.COM 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
49510310SNeil.Perrin@Sun.COM 			    logbias_changed_cb, os));
4967237Sek110237 		}
4977237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
49810298SMatthew.Ahrens@Sun.COM 		    primary_cache_changed_cb, os));
4997237Sek110237 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
50010298SMatthew.Ahrens@Sun.COM 		    secondary_cache_changed_cb, os));
501789Sahrens 	}
502789Sahrens 
5031544Seschrock 	/*
5041544Seschrock 	 * We should need only a single pass over the dnode list, since
5051544Seschrock 	 * nothing can be added to the list at this point.
5061544Seschrock 	 */
50710298SMatthew.Ahrens@Sun.COM 	(void) dmu_objset_evict_dbufs(os);
5081544Seschrock 
50910298SMatthew.Ahrens@Sun.COM 	dnode_special_close(os->os_meta_dnode);
51010298SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode) {
51110298SMatthew.Ahrens@Sun.COM 		dnode_special_close(os->os_userused_dnode);
51210298SMatthew.Ahrens@Sun.COM 		dnode_special_close(os->os_groupused_dnode);
5139396SMatthew.Ahrens@Sun.COM 	}
51410298SMatthew.Ahrens@Sun.COM 	zil_free(os->os_zil);
515789Sahrens 
51610298SMatthew.Ahrens@Sun.COM 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
517789Sahrens 
51810298SMatthew.Ahrens@Sun.COM 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
51910298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_lock);
52010298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_obj_lock);
52110298SMatthew.Ahrens@Sun.COM 	mutex_destroy(&os->os_user_ptr_lock);
52210298SMatthew.Ahrens@Sun.COM 	kmem_free(os, sizeof (objset_t));
523789Sahrens }
524789Sahrens 
52510373Schris.kirby@sun.com timestruc_t
52610373Schris.kirby@sun.com dmu_objset_snap_cmtime(objset_t *os)
52710373Schris.kirby@sun.com {
52810373Schris.kirby@sun.com 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
52910373Schris.kirby@sun.com }
53010373Schris.kirby@sun.com 
531789Sahrens /* called from dsl for meta-objset */
53210298SMatthew.Ahrens@Sun.COM objset_t *
5333547Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
5343547Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
535789Sahrens {
53610298SMatthew.Ahrens@Sun.COM 	objset_t *os;
537789Sahrens 	dnode_t *mdn;
538789Sahrens 
539789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
5404787Sahrens 	if (ds)
5414787Sahrens 		mutex_enter(&ds->ds_opening_lock);
54210298SMatthew.Ahrens@Sun.COM 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
5434787Sahrens 	if (ds)
5444787Sahrens 		mutex_exit(&ds->ds_opening_lock);
54510298SMatthew.Ahrens@Sun.COM 	mdn = os->os_meta_dnode;
546789Sahrens 
547789Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
548789Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
549789Sahrens 
550789Sahrens 	/*
551789Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
552789Sahrens 	 * later, because then we could do it in quescing context while
553789Sahrens 	 * we are also accessing it in open context.
554789Sahrens 	 *
555789Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
556789Sahrens 	 * because the MOS is only updated in syncing context.
557789Sahrens 	 * This is most fortunate: the MOS is the only objset that
558789Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
559789Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
560789Sahrens 	 */
5611544Seschrock 	if (ds != NULL) {
5621544Seschrock 		int levels = 1;
5631544Seschrock 
5641544Seschrock 		/*
5651544Seschrock 		 * Determine the number of levels necessary for the meta-dnode
5661544Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
5671544Seschrock 		 */
5681544Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
5691544Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
5701544Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
5711544Seschrock 			levels++;
5721544Seschrock 
573789Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
5741544Seschrock 		    mdn->dn_nlevels = levels;
5751544Seschrock 	}
576789Sahrens 
577789Sahrens 	ASSERT(type != DMU_OST_NONE);
578789Sahrens 	ASSERT(type != DMU_OST_ANY);
579789Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
58010298SMatthew.Ahrens@Sun.COM 	os->os_phys->os_type = type;
58110298SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
58210298SMatthew.Ahrens@Sun.COM 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
58310298SMatthew.Ahrens@Sun.COM 		os->os_flags = os->os_phys->os_flags;
5849396SMatthew.Ahrens@Sun.COM 	}
585789Sahrens 
586789Sahrens 	dsl_dataset_dirty(ds, tx);
587789Sahrens 
58810298SMatthew.Ahrens@Sun.COM 	return (os);
589789Sahrens }
590789Sahrens 
591789Sahrens struct oscarg {
5924543Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
593789Sahrens 	void *userarg;
59410272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *clone_origin;
595789Sahrens 	const char *lastname;
596789Sahrens 	dmu_objset_type_t type;
5976492Stimh 	uint64_t flags;
598789Sahrens };
599789Sahrens 
6004543Smarks /*ARGSUSED*/
601789Sahrens static int
6022199Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
603789Sahrens {
6042199Sahrens 	dsl_dir_t *dd = arg1;
6052199Sahrens 	struct oscarg *oa = arg2;
6062199Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
6072199Sahrens 	int err;
6082199Sahrens 	uint64_t ddobj;
6092199Sahrens 
6102199Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
6112199Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
6122199Sahrens 	if (err != ENOENT)
6132199Sahrens 		return (err ? err : EEXIST);
6142199Sahrens 
61510272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin != NULL) {
61610272SMatthew.Ahrens@Sun.COM 		/* You can't clone across pools. */
61710272SMatthew.Ahrens@Sun.COM 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
6182199Sahrens 			return (EXDEV);
6192199Sahrens 
62010272SMatthew.Ahrens@Sun.COM 		/* You can only clone snapshots, not the head datasets. */
62110272SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
6222199Sahrens 			return (EINVAL);
6232199Sahrens 	}
6244543Smarks 
6252199Sahrens 	return (0);
6262199Sahrens }
6272199Sahrens 
6282199Sahrens static void
6294543Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
6302199Sahrens {
6312199Sahrens 	dsl_dir_t *dd = arg1;
6322199Sahrens 	struct oscarg *oa = arg2;
6332199Sahrens 	uint64_t dsobj;
634789Sahrens 
635789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
636789Sahrens 
6372199Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
63810272SMatthew.Ahrens@Sun.COM 	    oa->clone_origin, oa->flags, cr, tx);
639789Sahrens 
64010272SMatthew.Ahrens@Sun.COM 	if (oa->clone_origin == NULL) {
64110272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds;
64210272SMatthew.Ahrens@Sun.COM 		blkptr_t *bp;
64310298SMatthew.Ahrens@Sun.COM 		objset_t *os;
644789Sahrens 
64510272SMatthew.Ahrens@Sun.COM 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
64610272SMatthew.Ahrens@Sun.COM 		    FTAG, &ds));
64710272SMatthew.Ahrens@Sun.COM 		bp = dsl_dataset_get_blkptr(ds);
64810272SMatthew.Ahrens@Sun.COM 		ASSERT(BP_IS_HOLE(bp));
64910272SMatthew.Ahrens@Sun.COM 
65010298SMatthew.Ahrens@Sun.COM 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
6513547Smaybee 		    ds, bp, oa->type, tx);
652789Sahrens 
653789Sahrens 		if (oa->userfunc)
65410298SMatthew.Ahrens@Sun.COM 			oa->userfunc(os, oa->userarg, cr, tx);
65510272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, FTAG);
656789Sahrens 	}
6574543Smarks 
6584543Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
6594543Smarks 	    tx, cr, "dataset = %llu", dsobj);
660789Sahrens }
661789Sahrens 
662789Sahrens int
66310272SMatthew.Ahrens@Sun.COM dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
6644543Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
665789Sahrens {
6662199Sahrens 	dsl_dir_t *pdd;
667789Sahrens 	const char *tail;
668789Sahrens 	int err = 0;
6692199Sahrens 	struct oscarg oa = { 0 };
670789Sahrens 
6712199Sahrens 	ASSERT(strchr(name, '@') == NULL);
6722199Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
6731544Seschrock 	if (err)
6741544Seschrock 		return (err);
675789Sahrens 	if (tail == NULL) {
6762199Sahrens 		dsl_dir_close(pdd, FTAG);
677789Sahrens 		return (EEXIST);
678789Sahrens 	}
679789Sahrens 
6802199Sahrens 	oa.userfunc = func;
6812199Sahrens 	oa.userarg = arg;
6822199Sahrens 	oa.lastname = tail;
6832199Sahrens 	oa.type = type;
6846492Stimh 	oa.flags = flags;
6854543Smarks 
68610272SMatthew.Ahrens@Sun.COM 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
68710272SMatthew.Ahrens@Sun.COM 	    dmu_objset_create_sync, pdd, &oa, 5);
68810272SMatthew.Ahrens@Sun.COM 	dsl_dir_close(pdd, FTAG);
68910272SMatthew.Ahrens@Sun.COM 	return (err);
69010272SMatthew.Ahrens@Sun.COM }
69110272SMatthew.Ahrens@Sun.COM 
69210272SMatthew.Ahrens@Sun.COM int
69310272SMatthew.Ahrens@Sun.COM dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
69410272SMatthew.Ahrens@Sun.COM {
69510272SMatthew.Ahrens@Sun.COM 	dsl_dir_t *pdd;
69610272SMatthew.Ahrens@Sun.COM 	const char *tail;
69710272SMatthew.Ahrens@Sun.COM 	int err = 0;
69810272SMatthew.Ahrens@Sun.COM 	struct oscarg oa = { 0 };
69910272SMatthew.Ahrens@Sun.COM 
70010272SMatthew.Ahrens@Sun.COM 	ASSERT(strchr(name, '@') == NULL);
70110272SMatthew.Ahrens@Sun.COM 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
70210272SMatthew.Ahrens@Sun.COM 	if (err)
70310272SMatthew.Ahrens@Sun.COM 		return (err);
70410272SMatthew.Ahrens@Sun.COM 	if (tail == NULL) {
70510272SMatthew.Ahrens@Sun.COM 		dsl_dir_close(pdd, FTAG);
70610272SMatthew.Ahrens@Sun.COM 		return (EEXIST);
707789Sahrens 	}
70810272SMatthew.Ahrens@Sun.COM 
70910272SMatthew.Ahrens@Sun.COM 	oa.lastname = tail;
71010272SMatthew.Ahrens@Sun.COM 	oa.clone_origin = clone_origin;
71110272SMatthew.Ahrens@Sun.COM 	oa.flags = flags;
71210272SMatthew.Ahrens@Sun.COM 
7132199Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
7142199Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
7152199Sahrens 	dsl_dir_close(pdd, FTAG);
716789Sahrens 	return (err);
717789Sahrens }
718789Sahrens 
719789Sahrens int
72010242Schris.kirby@sun.com dmu_objset_destroy(const char *name, boolean_t defer)
721789Sahrens {
72210298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds;
723789Sahrens 	int error;
724789Sahrens 
725789Sahrens 	/*
72610272SMatthew.Ahrens@Sun.COM 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
72710272SMatthew.Ahrens@Sun.COM 	 * intent log, but if there is an active log, it has blocks that
72810272SMatthew.Ahrens@Sun.COM 	 * are allocated, but may not yet be reflected in the on-disk
72910272SMatthew.Ahrens@Sun.COM 	 * structure.  Only the ZIL knows how to free them, so we have
73010272SMatthew.Ahrens@Sun.COM 	 * to call into it here.
731789Sahrens 	 */
73210298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
733789Sahrens 	if (error == 0) {
73410298SMatthew.Ahrens@Sun.COM 		objset_t *os;
73510298SMatthew.Ahrens@Sun.COM 		if (dmu_objset_from_ds(ds, &os) == 0)
73610298SMatthew.Ahrens@Sun.COM 			zil_destroy(dmu_objset_zil(os), B_FALSE);
73710298SMatthew.Ahrens@Sun.COM 		error = dsl_dataset_destroy(ds, FTAG, defer);
73810272SMatthew.Ahrens@Sun.COM 		/* dsl_dataset_destroy() closes the ds. */
739789Sahrens 	}
740789Sahrens 
7415367Sahrens 	return (error);
742789Sahrens }
743789Sahrens 
7442199Sahrens struct snaparg {
7452199Sahrens 	dsl_sync_task_group_t *dstg;
7462199Sahrens 	char *snapname;
7472199Sahrens 	char failed[MAXPATHLEN];
748*11620SMatthew.Ahrens@Sun.COM 	boolean_t recursive;
7499355SMatthew.Ahrens@Sun.COM 	nvlist_t *props;
7505367Sahrens };
7515367Sahrens 
7529355SMatthew.Ahrens@Sun.COM static int
7539355SMatthew.Ahrens@Sun.COM snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
7549355SMatthew.Ahrens@Sun.COM {
7559355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
7569355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7579355SMatthew.Ahrens@Sun.COM 
7589355SMatthew.Ahrens@Sun.COM 	/* The props have already been checked by zfs_check_userprops(). */
7599355SMatthew.Ahrens@Sun.COM 
76010298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
7619355SMatthew.Ahrens@Sun.COM 	    sn->snapname, tx));
7629355SMatthew.Ahrens@Sun.COM }
7639355SMatthew.Ahrens@Sun.COM 
7649355SMatthew.Ahrens@Sun.COM static void
7659355SMatthew.Ahrens@Sun.COM snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
7669355SMatthew.Ahrens@Sun.COM {
7679355SMatthew.Ahrens@Sun.COM 	objset_t *os = arg1;
76810298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
7699355SMatthew.Ahrens@Sun.COM 	struct snaparg *sn = arg2;
7709355SMatthew.Ahrens@Sun.COM 
7719355SMatthew.Ahrens@Sun.COM 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
7729355SMatthew.Ahrens@Sun.COM 
77311022STom.Erickson@Sun.COM 	if (sn->props) {
77411022STom.Erickson@Sun.COM 		dsl_props_arg_t pa;
77511022STom.Erickson@Sun.COM 		pa.pa_props = sn->props;
77611022STom.Erickson@Sun.COM 		pa.pa_source = ZPROP_SRC_LOCAL;
77711022STom.Erickson@Sun.COM 		dsl_props_set_sync(ds->ds_prev, &pa, cr, tx);
77811022STom.Erickson@Sun.COM 	}
7799355SMatthew.Ahrens@Sun.COM }
7802199Sahrens 
7812199Sahrens static int
78211209SMatthew.Ahrens@Sun.COM dmu_objset_snapshot_one(const char *name, void *arg)
7832199Sahrens {
7842199Sahrens 	struct snaparg *sn = arg;
7852199Sahrens 	objset_t *os;
7862199Sahrens 	int err;
787*11620SMatthew.Ahrens@Sun.COM 	char *cp;
788*11620SMatthew.Ahrens@Sun.COM 
789*11620SMatthew.Ahrens@Sun.COM 	/*
790*11620SMatthew.Ahrens@Sun.COM 	 * If the objset starts with a '%', then ignore it unless it was
791*11620SMatthew.Ahrens@Sun.COM 	 * explicitly named (ie, not recursive).  These hidden datasets
792*11620SMatthew.Ahrens@Sun.COM 	 * are always inconsistent, and by not opening them here, we can
793*11620SMatthew.Ahrens@Sun.COM 	 * avoid a race with dsl_dir_destroy_check().
794*11620SMatthew.Ahrens@Sun.COM 	 */
795*11620SMatthew.Ahrens@Sun.COM 	cp = strrchr(name, '/');
796*11620SMatthew.Ahrens@Sun.COM 	if (cp && cp[1] == '%' && sn->recursive)
797*11620SMatthew.Ahrens@Sun.COM 		return (0);
7982199Sahrens 
7992199Sahrens 	(void) strcpy(sn->failed, name);
8002199Sahrens 
8014543Smarks 	/*
802*11620SMatthew.Ahrens@Sun.COM 	 * Check permissions if we are doing a recursive snapshot.  The
803*11620SMatthew.Ahrens@Sun.COM 	 * permission checks for the starting dataset have already been
804*11620SMatthew.Ahrens@Sun.COM 	 * performed in zfs_secpolicy_snapshot()
8054543Smarks 	 */
806*11620SMatthew.Ahrens@Sun.COM 	if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED())))
8074543Smarks 		return (err);
8084543Smarks 
80910298SMatthew.Ahrens@Sun.COM 	err = dmu_objset_hold(name, sn, &os);
8102199Sahrens 	if (err != 0)
8112199Sahrens 		return (err);
8122199Sahrens 
813*11620SMatthew.Ahrens@Sun.COM 	/*
814*11620SMatthew.Ahrens@Sun.COM 	 * If the objset is in an inconsistent state (eg, in the process
815*11620SMatthew.Ahrens@Sun.COM 	 * of being destroyed), don't snapshot it.  As with %hidden
816*11620SMatthew.Ahrens@Sun.COM 	 * datasets, we return EBUSY if this name was explicitly
817*11620SMatthew.Ahrens@Sun.COM 	 * requested (ie, not recursive), and otherwise ignore it.
818*11620SMatthew.Ahrens@Sun.COM 	 */
81910298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
82010298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
821*11620SMatthew.Ahrens@Sun.COM 		return (sn->recursive ? 0 : EBUSY);
8223637Srm160521 	}
8233637Srm160521 
8243637Srm160521 	/*
8252199Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
8262199Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
8272199Sahrens 	 * a side effect.
8282199Sahrens 	 */
8292199Sahrens 	err = zil_suspend(dmu_objset_zil(os));
8302199Sahrens 	if (err == 0) {
8319355SMatthew.Ahrens@Sun.COM 		dsl_sync_task_create(sn->dstg, snapshot_check,
8329355SMatthew.Ahrens@Sun.COM 		    snapshot_sync, os, sn, 3);
8333637Srm160521 	} else {
83410298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, sn);
8352199Sahrens 	}
8363637Srm160521 
8372199Sahrens 	return (err);
8382199Sahrens }
8392199Sahrens 
8402199Sahrens int
8419355SMatthew.Ahrens@Sun.COM dmu_objset_snapshot(char *fsname, char *snapname,
8429355SMatthew.Ahrens@Sun.COM     nvlist_t *props, boolean_t recursive)
8432199Sahrens {
8442199Sahrens 	dsl_sync_task_t *dst;
8459355SMatthew.Ahrens@Sun.COM 	struct snaparg sn;
8462199Sahrens 	spa_t *spa;
8472199Sahrens 	int err;
8482199Sahrens 
8492199Sahrens 	(void) strcpy(sn.failed, fsname);
8502199Sahrens 
8514603Sahrens 	err = spa_open(fsname, &spa, FTAG);
8522199Sahrens 	if (err)
8532199Sahrens 		return (err);
8542199Sahrens 
8552199Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8562199Sahrens 	sn.snapname = snapname;
8579355SMatthew.Ahrens@Sun.COM 	sn.props = props;
858*11620SMatthew.Ahrens@Sun.COM 	sn.recursive = recursive;
8592199Sahrens 
8602417Sahrens 	if (recursive) {
8612417Sahrens 		err = dmu_objset_find(fsname,
8622417Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
8632417Sahrens 	} else {
8642199Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
8652417Sahrens 	}
8662199Sahrens 
8679355SMatthew.Ahrens@Sun.COM 	if (err == 0)
8689355SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_group_wait(sn.dstg);
8692199Sahrens 
8702199Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
8712199Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
8729355SMatthew.Ahrens@Sun.COM 		objset_t *os = dst->dst_arg1;
87310298SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
8742199Sahrens 		if (dst->dst_err)
8755367Sahrens 			dsl_dataset_name(ds, sn.failed);
8769355SMatthew.Ahrens@Sun.COM 		zil_resume(dmu_objset_zil(os));
87710298SMatthew.Ahrens@Sun.COM 		dmu_objset_rele(os, &sn);
8782199Sahrens 	}
8795367Sahrens 
8802199Sahrens 	if (err)
8812199Sahrens 		(void) strcpy(fsname, sn.failed);
8822199Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
8832199Sahrens 	spa_close(spa, FTAG);
8842199Sahrens 	return (err);
8852199Sahrens }
8862199Sahrens 
887789Sahrens static void
8889396SMatthew.Ahrens@Sun.COM dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
889789Sahrens {
8903547Smaybee 	dnode_t *dn;
891789Sahrens 
8923547Smaybee 	while (dn = list_head(list)) {
8933547Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
8943547Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
8953547Smaybee 		/*
8969396SMatthew.Ahrens@Sun.COM 		 * Initialize dn_zio outside dnode_sync() because the
8979396SMatthew.Ahrens@Sun.COM 		 * meta-dnode needs to set it ouside dnode_sync().
8983547Smaybee 		 */
8993547Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
9003547Smaybee 		ASSERT(dn->dn_zio);
901789Sahrens 
9023547Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
9033547Smaybee 		list_remove(list, dn);
9049396SMatthew.Ahrens@Sun.COM 
9059396SMatthew.Ahrens@Sun.COM 		if (newlist) {
9069396SMatthew.Ahrens@Sun.COM 			(void) dnode_add_ref(dn, newlist);
9079396SMatthew.Ahrens@Sun.COM 			list_insert_tail(newlist, dn);
9089396SMatthew.Ahrens@Sun.COM 		}
9099396SMatthew.Ahrens@Sun.COM 
9103547Smaybee 		dnode_sync(dn, tx);
9113547Smaybee 	}
9123547Smaybee }
9132981Sahrens 
9143547Smaybee /* ARGSUSED */
9153547Smaybee static void
91610922SJeff.Bonwick@Sun.COM dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
9173547Smaybee {
9187754SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
91910298SMatthew.Ahrens@Sun.COM 	objset_t *os = arg;
9203547Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
9212981Sahrens 
9227754SJeff.Bonwick@Sun.COM 	ASSERT(bp == os->os_rootbp);
9237754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
9247754SJeff.Bonwick@Sun.COM 	ASSERT(BP_GET_LEVEL(bp) == 0);
9255329Sgw25295 
9263547Smaybee 	/*
9279396SMatthew.Ahrens@Sun.COM 	 * Update rootbp fill count: it should be the number of objects
9289396SMatthew.Ahrens@Sun.COM 	 * allocated in the object set (not counting the "special"
9299396SMatthew.Ahrens@Sun.COM 	 * objects that are stored in the objset_phys_t -- the meta
9309396SMatthew.Ahrens@Sun.COM 	 * dnode and user/group accounting objects).
9313547Smaybee 	 */
9329396SMatthew.Ahrens@Sun.COM 	bp->blk_fill = 0;
9337754SJeff.Bonwick@Sun.COM 	for (int i = 0; i < dnp->dn_nblkptr; i++)
9343547Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
93510922SJeff.Bonwick@Sun.COM }
93610922SJeff.Bonwick@Sun.COM 
93710922SJeff.Bonwick@Sun.COM /* ARGSUSED */
93810922SJeff.Bonwick@Sun.COM static void
93910922SJeff.Bonwick@Sun.COM dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
94010922SJeff.Bonwick@Sun.COM {
94110922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = zio->io_bp;
94210922SJeff.Bonwick@Sun.COM 	blkptr_t *bp_orig = &zio->io_bp_orig;
94310922SJeff.Bonwick@Sun.COM 	objset_t *os = arg;
9445329Sgw25295 
9457754SJeff.Bonwick@Sun.COM 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
94610922SJeff.Bonwick@Sun.COM 		ASSERT(BP_EQUAL(bp, bp_orig));
9477754SJeff.Bonwick@Sun.COM 	} else {
94810922SJeff.Bonwick@Sun.COM 		dsl_dataset_t *ds = os->os_dsl_dataset;
94910922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx = os->os_synctx;
95010922SJeff.Bonwick@Sun.COM 
95110922SJeff.Bonwick@Sun.COM 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
95210922SJeff.Bonwick@Sun.COM 		dsl_dataset_block_born(ds, bp, tx);
9535329Sgw25295 	}
954789Sahrens }
955789Sahrens 
956789Sahrens /* called from dsl */
957789Sahrens void
95810298SMatthew.Ahrens@Sun.COM dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
959789Sahrens {
960789Sahrens 	int txgoff;
9611544Seschrock 	zbookmark_t zb;
96210922SJeff.Bonwick@Sun.COM 	zio_prop_t zp;
9633547Smaybee 	zio_t *zio;
9643547Smaybee 	list_t *list;
9659396SMatthew.Ahrens@Sun.COM 	list_t *newlist = NULL;
9663547Smaybee 	dbuf_dirty_record_t *dr;
9673547Smaybee 
9683547Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
969789Sahrens 
970789Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
971789Sahrens 	/* XXX the write_done callback should really give us the tx... */
972789Sahrens 	os->os_synctx = tx;
973789Sahrens 
9743882Sahrens 	if (os->os_dsl_dataset == NULL) {
9753882Sahrens 		/*
9763882Sahrens 		 * This is the MOS.  If we have upgraded,
9773882Sahrens 		 * spa_max_replication() could change, so reset
9783882Sahrens 		 * os_copies here.
9793882Sahrens 		 */
9803882Sahrens 		os->os_copies = spa_max_replication(os->os_spa);
9813882Sahrens 	}
9823882Sahrens 
9833547Smaybee 	/*
9843547Smaybee 	 * Create the root block IO
9853547Smaybee 	 */
9867046Sahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
9879396SMatthew.Ahrens@Sun.COM 
98810922SJeff.Bonwick@Sun.COM 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
98910922SJeff.Bonwick@Sun.COM 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
99010922SJeff.Bonwick@Sun.COM 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
99110922SJeff.Bonwick@Sun.COM 
99210922SJeff.Bonwick@Sun.COM 	dmu_write_policy(os, NULL, 0, 0, &zp);
99310922SJeff.Bonwick@Sun.COM 
99410922SJeff.Bonwick@Sun.COM 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
99510922SJeff.Bonwick@Sun.COM 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
99610922SJeff.Bonwick@Sun.COM 	    dmu_objset_write_ready, dmu_objset_write_done, os,
9977754SJeff.Bonwick@Sun.COM 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
9983547Smaybee 
9993547Smaybee 	/*
10009396SMatthew.Ahrens@Sun.COM 	 * Sync special dnodes - the parent IO for the sync is the root block
10013547Smaybee 	 */
10023547Smaybee 	os->os_meta_dnode->dn_zio = zio;
10033547Smaybee 	dnode_sync(os->os_meta_dnode, tx);
1004789Sahrens 
10059396SMatthew.Ahrens@Sun.COM 	os->os_phys->os_flags = os->os_flags;
10069396SMatthew.Ahrens@Sun.COM 
10079396SMatthew.Ahrens@Sun.COM 	if (os->os_userused_dnode &&
10089396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
10099396SMatthew.Ahrens@Sun.COM 		os->os_userused_dnode->dn_zio = zio;
10109396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_userused_dnode, tx);
10119396SMatthew.Ahrens@Sun.COM 		os->os_groupused_dnode->dn_zio = zio;
10129396SMatthew.Ahrens@Sun.COM 		dnode_sync(os->os_groupused_dnode, tx);
10139396SMatthew.Ahrens@Sun.COM 	}
10149396SMatthew.Ahrens@Sun.COM 
1015789Sahrens 	txgoff = tx->tx_txg & TXG_MASK;
1016789Sahrens 
10179396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userused_enabled(os)) {
10189396SMatthew.Ahrens@Sun.COM 		newlist = &os->os_synced_dnodes;
10199396SMatthew.Ahrens@Sun.COM 		/*
10209396SMatthew.Ahrens@Sun.COM 		 * We must create the list here because it uses the
10219396SMatthew.Ahrens@Sun.COM 		 * dn_dirty_link[] of this txg.
10229396SMatthew.Ahrens@Sun.COM 		 */
10239396SMatthew.Ahrens@Sun.COM 		list_create(newlist, sizeof (dnode_t),
10249396SMatthew.Ahrens@Sun.COM 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
10259396SMatthew.Ahrens@Sun.COM 	}
10269396SMatthew.Ahrens@Sun.COM 
10279396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
10289396SMatthew.Ahrens@Sun.COM 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1029789Sahrens 
10303547Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
10313547Smaybee 	while (dr = list_head(list)) {
10323547Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
10333547Smaybee 		list_remove(list, dr);
10343547Smaybee 		if (dr->dr_zio)
10353547Smaybee 			zio_nowait(dr->dr_zio);
10363547Smaybee 	}
1037789Sahrens 	/*
1038789Sahrens 	 * Free intent log blocks up to this tx.
1039789Sahrens 	 */
1040789Sahrens 	zil_sync(os->os_zil, tx);
10417046Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
10423547Smaybee 	zio_nowait(zio);
1043789Sahrens }
1044789Sahrens 
104510922SJeff.Bonwick@Sun.COM boolean_t
104610922SJeff.Bonwick@Sun.COM dmu_objset_is_dirty(objset_t *os, uint64_t txg)
104710922SJeff.Bonwick@Sun.COM {
104810922SJeff.Bonwick@Sun.COM 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
104910922SJeff.Bonwick@Sun.COM 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
105010922SJeff.Bonwick@Sun.COM }
105110922SJeff.Bonwick@Sun.COM 
10529396SMatthew.Ahrens@Sun.COM static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
10539396SMatthew.Ahrens@Sun.COM 
10549396SMatthew.Ahrens@Sun.COM void
10559396SMatthew.Ahrens@Sun.COM dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
10569396SMatthew.Ahrens@Sun.COM {
10579396SMatthew.Ahrens@Sun.COM 	used_cbs[ost] = cb;
10589396SMatthew.Ahrens@Sun.COM }
10599396SMatthew.Ahrens@Sun.COM 
10609396SMatthew.Ahrens@Sun.COM boolean_t
106110298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(objset_t *os)
10629396SMatthew.Ahrens@Sun.COM {
10639396SMatthew.Ahrens@Sun.COM 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
10649396SMatthew.Ahrens@Sun.COM 	    used_cbs[os->os_phys->os_type] &&
10659396SMatthew.Ahrens@Sun.COM 	    os->os_userused_dnode);
10669396SMatthew.Ahrens@Sun.COM }
10679396SMatthew.Ahrens@Sun.COM 
106810407SMatthew.Ahrens@Sun.COM static void
106910407SMatthew.Ahrens@Sun.COM do_userquota_callback(objset_t *os, dnode_phys_t *dnp,
107010407SMatthew.Ahrens@Sun.COM     boolean_t subtract, dmu_tx_t *tx)
107110407SMatthew.Ahrens@Sun.COM {
107210407SMatthew.Ahrens@Sun.COM 	static const char zerobuf[DN_MAX_BONUSLEN] = {0};
107310407SMatthew.Ahrens@Sun.COM 	uint64_t user, group;
107410407SMatthew.Ahrens@Sun.COM 
107510407SMatthew.Ahrens@Sun.COM 	ASSERT(dnp->dn_type != 0 ||
107610407SMatthew.Ahrens@Sun.COM 	    (bcmp(DN_BONUS(dnp), zerobuf, DN_MAX_BONUSLEN) == 0 &&
107710407SMatthew.Ahrens@Sun.COM 	    DN_USED_BYTES(dnp) == 0));
107810407SMatthew.Ahrens@Sun.COM 
107910407SMatthew.Ahrens@Sun.COM 	if ((dnp->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) &&
108010407SMatthew.Ahrens@Sun.COM 	    0 == used_cbs[os->os_phys->os_type](dnp->dn_bonustype,
108110407SMatthew.Ahrens@Sun.COM 	    DN_BONUS(dnp), &user, &group)) {
108210407SMatthew.Ahrens@Sun.COM 		int64_t delta = DNODE_SIZE + DN_USED_BYTES(dnp);
108310407SMatthew.Ahrens@Sun.COM 		if (subtract)
108410407SMatthew.Ahrens@Sun.COM 			delta = -delta;
108510801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
108610407SMatthew.Ahrens@Sun.COM 		    user, delta, tx));
108710801SMatthew.Ahrens@Sun.COM 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
108810407SMatthew.Ahrens@Sun.COM 		    group, delta, tx));
108910407SMatthew.Ahrens@Sun.COM 	}
109010407SMatthew.Ahrens@Sun.COM }
109110407SMatthew.Ahrens@Sun.COM 
10929396SMatthew.Ahrens@Sun.COM void
109310298SMatthew.Ahrens@Sun.COM dmu_objset_do_userquota_callbacks(objset_t *os, dmu_tx_t *tx)
10949396SMatthew.Ahrens@Sun.COM {
10959396SMatthew.Ahrens@Sun.COM 	dnode_t *dn;
10969396SMatthew.Ahrens@Sun.COM 	list_t *list = &os->os_synced_dnodes;
10979396SMatthew.Ahrens@Sun.COM 
10989396SMatthew.Ahrens@Sun.COM 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
10999396SMatthew.Ahrens@Sun.COM 
11009396SMatthew.Ahrens@Sun.COM 	while (dn = list_head(list)) {
11019396SMatthew.Ahrens@Sun.COM 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
11029396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_oldphys);
11039396SMatthew.Ahrens@Sun.COM 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
11049396SMatthew.Ahrens@Sun.COM 		    dn->dn_phys->dn_flags &
11059396SMatthew.Ahrens@Sun.COM 		    DNODE_FLAG_USERUSED_ACCOUNTED);
11069396SMatthew.Ahrens@Sun.COM 
11079396SMatthew.Ahrens@Sun.COM 		/* Allocate the user/groupused objects if necessary. */
11089396SMatthew.Ahrens@Sun.COM 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
110910298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
11109396SMatthew.Ahrens@Sun.COM 			    DMU_USERUSED_OBJECT,
11119396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
111210298SMatthew.Ahrens@Sun.COM 			VERIFY(0 == zap_create_claim(os,
11139396SMatthew.Ahrens@Sun.COM 			    DMU_GROUPUSED_OBJECT,
11149396SMatthew.Ahrens@Sun.COM 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
11159396SMatthew.Ahrens@Sun.COM 		}
11169396SMatthew.Ahrens@Sun.COM 
11179396SMatthew.Ahrens@Sun.COM 		/*
111810407SMatthew.Ahrens@Sun.COM 		 * We intentionally modify the zap object even if the
111910407SMatthew.Ahrens@Sun.COM 		 * net delta (due to phys-oldphys) is zero.  Otherwise
112010407SMatthew.Ahrens@Sun.COM 		 * the block of the zap obj could be shared between
112110407SMatthew.Ahrens@Sun.COM 		 * datasets but need to be different between them after
112210407SMatthew.Ahrens@Sun.COM 		 * a bprewrite.
11239396SMatthew.Ahrens@Sun.COM 		 */
112410407SMatthew.Ahrens@Sun.COM 		do_userquota_callback(os, dn->dn_oldphys, B_TRUE, tx);
112510407SMatthew.Ahrens@Sun.COM 		do_userquota_callback(os, dn->dn_phys, B_FALSE, tx);
11269396SMatthew.Ahrens@Sun.COM 
11279396SMatthew.Ahrens@Sun.COM 		/*
11289396SMatthew.Ahrens@Sun.COM 		 * The mutex is needed here for interlock with dnode_allocate.
11299396SMatthew.Ahrens@Sun.COM 		 */
11309396SMatthew.Ahrens@Sun.COM 		mutex_enter(&dn->dn_mtx);
11319396SMatthew.Ahrens@Sun.COM 		zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t));
11329396SMatthew.Ahrens@Sun.COM 		dn->dn_oldphys = NULL;
11339396SMatthew.Ahrens@Sun.COM 		mutex_exit(&dn->dn_mtx);
11349396SMatthew.Ahrens@Sun.COM 
11359396SMatthew.Ahrens@Sun.COM 		list_remove(list, dn);
11369396SMatthew.Ahrens@Sun.COM 		dnode_rele(dn, list);
11379396SMatthew.Ahrens@Sun.COM 	}
11389396SMatthew.Ahrens@Sun.COM }
11399396SMatthew.Ahrens@Sun.COM 
11409396SMatthew.Ahrens@Sun.COM boolean_t
11419396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_present(objset_t *os)
11429396SMatthew.Ahrens@Sun.COM {
114310298SMatthew.Ahrens@Sun.COM 	return (os->os_phys->os_flags &
11449396SMatthew.Ahrens@Sun.COM 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
11459396SMatthew.Ahrens@Sun.COM }
11469396SMatthew.Ahrens@Sun.COM 
11479396SMatthew.Ahrens@Sun.COM int
11489396SMatthew.Ahrens@Sun.COM dmu_objset_userspace_upgrade(objset_t *os)
11499396SMatthew.Ahrens@Sun.COM {
11509396SMatthew.Ahrens@Sun.COM 	uint64_t obj;
11519396SMatthew.Ahrens@Sun.COM 	int err = 0;
11529396SMatthew.Ahrens@Sun.COM 
11539396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_userspace_present(os))
11549396SMatthew.Ahrens@Sun.COM 		return (0);
115510298SMatthew.Ahrens@Sun.COM 	if (!dmu_objset_userused_enabled(os))
11569396SMatthew.Ahrens@Sun.COM 		return (ENOTSUP);
11579396SMatthew.Ahrens@Sun.COM 	if (dmu_objset_is_snapshot(os))
11589396SMatthew.Ahrens@Sun.COM 		return (EINVAL);
11599396SMatthew.Ahrens@Sun.COM 
11609396SMatthew.Ahrens@Sun.COM 	/*
11619396SMatthew.Ahrens@Sun.COM 	 * We simply need to mark every object dirty, so that it will be
11629396SMatthew.Ahrens@Sun.COM 	 * synced out and now accounted.  If this is called
11639396SMatthew.Ahrens@Sun.COM 	 * concurrently, or if we already did some work before crashing,
11649396SMatthew.Ahrens@Sun.COM 	 * that's fine, since we track each object's accounted state
11659396SMatthew.Ahrens@Sun.COM 	 * independently.
11669396SMatthew.Ahrens@Sun.COM 	 */
11679396SMatthew.Ahrens@Sun.COM 
11689396SMatthew.Ahrens@Sun.COM 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
11699951SLin.Ling@Sun.COM 		dmu_tx_t *tx;
11709396SMatthew.Ahrens@Sun.COM 		dmu_buf_t *db;
11719396SMatthew.Ahrens@Sun.COM 		int objerr;
11729396SMatthew.Ahrens@Sun.COM 
11739396SMatthew.Ahrens@Sun.COM 		if (issig(JUSTLOOKING) && issig(FORREAL))
11749396SMatthew.Ahrens@Sun.COM 			return (EINTR);
11759396SMatthew.Ahrens@Sun.COM 
11769396SMatthew.Ahrens@Sun.COM 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
11779396SMatthew.Ahrens@Sun.COM 		if (objerr)
11789396SMatthew.Ahrens@Sun.COM 			continue;
11799951SLin.Ling@Sun.COM 		tx = dmu_tx_create(os);
11809396SMatthew.Ahrens@Sun.COM 		dmu_tx_hold_bonus(tx, obj);
11819396SMatthew.Ahrens@Sun.COM 		objerr = dmu_tx_assign(tx, TXG_WAIT);
11829396SMatthew.Ahrens@Sun.COM 		if (objerr) {
11839396SMatthew.Ahrens@Sun.COM 			dmu_tx_abort(tx);
11849396SMatthew.Ahrens@Sun.COM 			continue;
11859396SMatthew.Ahrens@Sun.COM 		}
11869396SMatthew.Ahrens@Sun.COM 		dmu_buf_will_dirty(db, tx);
11879396SMatthew.Ahrens@Sun.COM 		dmu_buf_rele(db, FTAG);
11889396SMatthew.Ahrens@Sun.COM 		dmu_tx_commit(tx);
11899396SMatthew.Ahrens@Sun.COM 	}
11909396SMatthew.Ahrens@Sun.COM 
119110298SMatthew.Ahrens@Sun.COM 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
11929396SMatthew.Ahrens@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
11939396SMatthew.Ahrens@Sun.COM 	return (0);
11949396SMatthew.Ahrens@Sun.COM }
11959396SMatthew.Ahrens@Sun.COM 
1196789Sahrens void
11972885Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
11982885Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
11992885Sahrens {
120010298SMatthew.Ahrens@Sun.COM 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
12012885Sahrens 	    usedobjsp, availobjsp);
12022885Sahrens }
12032885Sahrens 
12042885Sahrens uint64_t
12052885Sahrens dmu_objset_fsid_guid(objset_t *os)
12062885Sahrens {
120710298SMatthew.Ahrens@Sun.COM 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
12082885Sahrens }
12092885Sahrens 
12102885Sahrens void
12112885Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1212789Sahrens {
121310298SMatthew.Ahrens@Sun.COM 	stat->dds_type = os->os_phys->os_type;
121410298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset)
121510298SMatthew.Ahrens@Sun.COM 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
12162885Sahrens }
12172885Sahrens 
12182885Sahrens void
12192885Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
12202885Sahrens {
122110298SMatthew.Ahrens@Sun.COM 	ASSERT(os->os_dsl_dataset ||
122210298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type == DMU_OST_META);
12232885Sahrens 
122410298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
122510298SMatthew.Ahrens@Sun.COM 		dsl_dataset_stats(os->os_dsl_dataset, nv);
12262885Sahrens 
12272885Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
122810298SMatthew.Ahrens@Sun.COM 	    os->os_phys->os_type);
12299396SMatthew.Ahrens@Sun.COM 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
12309396SMatthew.Ahrens@Sun.COM 	    dmu_objset_userspace_present(os));
1231789Sahrens }
1232789Sahrens 
1233789Sahrens int
1234789Sahrens dmu_objset_is_snapshot(objset_t *os)
1235789Sahrens {
123610298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset != NULL)
123710298SMatthew.Ahrens@Sun.COM 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1238789Sahrens 	else
1239789Sahrens 		return (B_FALSE);
1240789Sahrens }
1241789Sahrens 
1242789Sahrens int
12436492Stimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
12446492Stimh     boolean_t *conflict)
12456492Stimh {
124610298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
12476492Stimh 	uint64_t ignored;
12486492Stimh 
12496492Stimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
12506492Stimh 		return (ENOENT);
12516492Stimh 
12526492Stimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
12536492Stimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
12546492Stimh 	    real, maxlen, conflict));
12556492Stimh }
12566492Stimh 
12576492Stimh int
1258789Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
12595663Sck153898     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1260789Sahrens {
126110298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = os->os_dsl_dataset;
1262789Sahrens 	zap_cursor_t cursor;
1263789Sahrens 	zap_attribute_t attr;
1264789Sahrens 
1265789Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1266789Sahrens 		return (ENOENT);
1267789Sahrens 
1268789Sahrens 	zap_cursor_init_serialized(&cursor,
1269789Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1270789Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1271789Sahrens 
1272885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1273885Sahrens 		zap_cursor_fini(&cursor);
1274885Sahrens 		return (ENOENT);
1275885Sahrens 	}
1276885Sahrens 
1277885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1278885Sahrens 		zap_cursor_fini(&cursor);
1279885Sahrens 		return (ENAMETOOLONG);
1280885Sahrens 	}
1281885Sahrens 
1282885Sahrens 	(void) strcpy(name, attr.za_name);
1283885Sahrens 	if (idp)
1284885Sahrens 		*idp = attr.za_first_integer;
12855663Sck153898 	if (case_conflict)
12865663Sck153898 		*case_conflict = attr.za_normalization_conflict;
1287885Sahrens 	zap_cursor_advance(&cursor);
1288885Sahrens 	*offp = zap_cursor_serialize(&cursor);
1289885Sahrens 	zap_cursor_fini(&cursor);
1290885Sahrens 
1291885Sahrens 	return (0);
1292885Sahrens }
1293885Sahrens 
1294885Sahrens int
1295885Sahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
1296885Sahrens     uint64_t *idp, uint64_t *offp)
1297885Sahrens {
129810298SMatthew.Ahrens@Sun.COM 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1299885Sahrens 	zap_cursor_t cursor;
1300885Sahrens 	zap_attribute_t attr;
1301885Sahrens 
1302885Sahrens 	/* there is no next dir on a snapshot! */
130310298SMatthew.Ahrens@Sun.COM 	if (os->os_dsl_dataset->ds_object !=
1304885Sahrens 	    dd->dd_phys->dd_head_dataset_obj)
1305885Sahrens 		return (ENOENT);
1306885Sahrens 
1307885Sahrens 	zap_cursor_init_serialized(&cursor,
1308885Sahrens 	    dd->dd_pool->dp_meta_objset,
1309885Sahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1310885Sahrens 
1311885Sahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1312885Sahrens 		zap_cursor_fini(&cursor);
1313885Sahrens 		return (ENOENT);
1314885Sahrens 	}
1315885Sahrens 
1316885Sahrens 	if (strlen(attr.za_name) + 1 > namelen) {
1317885Sahrens 		zap_cursor_fini(&cursor);
1318789Sahrens 		return (ENAMETOOLONG);
1319885Sahrens 	}
1320789Sahrens 
1321789Sahrens 	(void) strcpy(name, attr.za_name);
1322885Sahrens 	if (idp)
1323885Sahrens 		*idp = attr.za_first_integer;
1324789Sahrens 	zap_cursor_advance(&cursor);
1325789Sahrens 	*offp = zap_cursor_serialize(&cursor);
1326885Sahrens 	zap_cursor_fini(&cursor);
1327789Sahrens 
1328789Sahrens 	return (0);
1329789Sahrens }
1330789Sahrens 
13317046Sahrens struct findarg {
133211209SMatthew.Ahrens@Sun.COM 	int (*func)(const char *, void *);
13337046Sahrens 	void *arg;
13347046Sahrens };
13357046Sahrens 
13367046Sahrens /* ARGSUSED */
13377046Sahrens static int
13387046Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
13397046Sahrens {
13407046Sahrens 	struct findarg *fa = arg;
134111209SMatthew.Ahrens@Sun.COM 	return (fa->func(dsname, fa->arg));
13427046Sahrens }
13437046Sahrens 
1344789Sahrens /*
1345789Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
13467046Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1347789Sahrens  */
13482199Sahrens int
134911209SMatthew.Ahrens@Sun.COM dmu_objset_find(char *name, int func(const char *, void *), void *arg,
135011209SMatthew.Ahrens@Sun.COM     int flags)
1351789Sahrens {
13527046Sahrens 	struct findarg fa;
13537046Sahrens 	fa.func = func;
13547046Sahrens 	fa.arg = arg;
13557046Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
13567046Sahrens }
13577046Sahrens 
13587046Sahrens /*
13597046Sahrens  * Find all objsets under name, call func on each
13607046Sahrens  */
13617046Sahrens int
13627046Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
13637046Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
13647046Sahrens {
1365789Sahrens 	dsl_dir_t *dd;
13667046Sahrens 	dsl_pool_t *dp;
13677046Sahrens 	dsl_dataset_t *ds;
1368789Sahrens 	zap_cursor_t zc;
13693978Smmusante 	zap_attribute_t *attr;
1370789Sahrens 	char *child;
13717046Sahrens 	uint64_t thisobj;
13727046Sahrens 	int err;
1373789Sahrens 
13747046Sahrens 	if (name == NULL)
13757046Sahrens 		name = spa_name(spa);
13767046Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
13771544Seschrock 	if (err)
13782199Sahrens 		return (err);
1379789Sahrens 
13807046Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
13817046Sahrens 	if (dd->dd_myname[0] == '$') {
13827046Sahrens 		dsl_dir_close(dd, FTAG);
13837046Sahrens 		return (0);
13847046Sahrens 	}
13857046Sahrens 
13867046Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
13873978Smmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
13887046Sahrens 	dp = dd->dd_pool;
1389789Sahrens 
1390789Sahrens 	/*
1391789Sahrens 	 * Iterate over all children.
1392789Sahrens 	 */
13932417Sahrens 	if (flags & DS_FIND_CHILDREN) {
13947046Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
13952417Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
13963978Smmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
13972417Sahrens 		    (void) zap_cursor_advance(&zc)) {
13983978Smmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
13993978Smmusante 			ASSERT(attr->za_num_integers == 1);
1400789Sahrens 
140111165SMatthew.Ahrens@Sun.COM 			child = kmem_asprintf("%s/%s", name, attr->za_name);
14027046Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
140311165SMatthew.Ahrens@Sun.COM 			strfree(child);
14042417Sahrens 			if (err)
14052417Sahrens 				break;
14062417Sahrens 		}
14072417Sahrens 		zap_cursor_fini(&zc);
14082199Sahrens 
14092417Sahrens 		if (err) {
14102417Sahrens 			dsl_dir_close(dd, FTAG);
14113978Smmusante 			kmem_free(attr, sizeof (zap_attribute_t));
14122417Sahrens 			return (err);
14132417Sahrens 		}
1414789Sahrens 	}
1415789Sahrens 
1416789Sahrens 	/*
1417789Sahrens 	 * Iterate over all snapshots.
1418789Sahrens 	 */
14197046Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
14207046Sahrens 		if (!dsl_pool_sync_context(dp))
14217046Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
14227046Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
14237046Sahrens 		if (!dsl_pool_sync_context(dp))
14247046Sahrens 			rw_exit(&dp->dp_config_rwlock);
1425789Sahrens 
14267046Sahrens 		if (err == 0) {
14277046Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
14287046Sahrens 			dsl_dataset_rele(ds, FTAG);
1429789Sahrens 
14307046Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
14317046Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
14327046Sahrens 			    (void) zap_cursor_advance(&zc)) {
14337046Sahrens 				ASSERT(attr->za_integer_length ==
14347046Sahrens 				    sizeof (uint64_t));
14357046Sahrens 				ASSERT(attr->za_num_integers == 1);
1436789Sahrens 
143711165SMatthew.Ahrens@Sun.COM 				child = kmem_asprintf("%s@%s",
143811165SMatthew.Ahrens@Sun.COM 				    name, attr->za_name);
14397046Sahrens 				err = func(spa, attr->za_first_integer,
14407046Sahrens 				    child, arg);
144111165SMatthew.Ahrens@Sun.COM 				strfree(child);
14427046Sahrens 				if (err)
14437046Sahrens 					break;
14447046Sahrens 			}
14457046Sahrens 			zap_cursor_fini(&zc);
1446789Sahrens 		}
1447789Sahrens 	}
1448789Sahrens 
1449789Sahrens 	dsl_dir_close(dd, FTAG);
14503978Smmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1451789Sahrens 
14522199Sahrens 	if (err)
14532199Sahrens 		return (err);
14542199Sahrens 
1455789Sahrens 	/*
1456789Sahrens 	 * Apply to self if appropriate.
1457789Sahrens 	 */
14587046Sahrens 	err = func(spa, thisobj, name, arg);
14592199Sahrens 	return (err);
1460789Sahrens }
14615326Sek110237 
14628415SRichard.Morris@Sun.COM /* ARGSUSED */
14638415SRichard.Morris@Sun.COM int
146411209SMatthew.Ahrens@Sun.COM dmu_objset_prefetch(const char *name, void *arg)
14658415SRichard.Morris@Sun.COM {
14668415SRichard.Morris@Sun.COM 	dsl_dataset_t *ds;
14678415SRichard.Morris@Sun.COM 
14688415SRichard.Morris@Sun.COM 	if (dsl_dataset_hold(name, FTAG, &ds))
14698415SRichard.Morris@Sun.COM 		return (0);
14708415SRichard.Morris@Sun.COM 
14718415SRichard.Morris@Sun.COM 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
14728415SRichard.Morris@Sun.COM 		mutex_enter(&ds->ds_opening_lock);
147310298SMatthew.Ahrens@Sun.COM 		if (ds->ds_objset == NULL) {
14748415SRichard.Morris@Sun.COM 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
14758415SRichard.Morris@Sun.COM 			zbookmark_t zb;
14768415SRichard.Morris@Sun.COM 
147710922SJeff.Bonwick@Sun.COM 			SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
147810922SJeff.Bonwick@Sun.COM 			    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
14798415SRichard.Morris@Sun.COM 
14808415SRichard.Morris@Sun.COM 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
14818415SRichard.Morris@Sun.COM 			    &ds->ds_phys->ds_bp, NULL, NULL,
14828415SRichard.Morris@Sun.COM 			    ZIO_PRIORITY_ASYNC_READ,
14838415SRichard.Morris@Sun.COM 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
14848415SRichard.Morris@Sun.COM 			    &aflags, &zb);
14858415SRichard.Morris@Sun.COM 		}
14868415SRichard.Morris@Sun.COM 		mutex_exit(&ds->ds_opening_lock);
14878415SRichard.Morris@Sun.COM 	}
14888415SRichard.Morris@Sun.COM 
14898415SRichard.Morris@Sun.COM 	dsl_dataset_rele(ds, FTAG);
14908415SRichard.Morris@Sun.COM 	return (0);
14918415SRichard.Morris@Sun.COM }
14928415SRichard.Morris@Sun.COM 
14935326Sek110237 void
14945326Sek110237 dmu_objset_set_user(objset_t *os, void *user_ptr)
14955326Sek110237 {
149610298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
149710298SMatthew.Ahrens@Sun.COM 	os->os_user_ptr = user_ptr;
14985326Sek110237 }
14995326Sek110237 
15005326Sek110237 void *
15015326Sek110237 dmu_objset_get_user(objset_t *os)
15025326Sek110237 {
150310298SMatthew.Ahrens@Sun.COM 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
150410298SMatthew.Ahrens@Sun.COM 	return (os->os_user_ptr);
15055326Sek110237 }
1506