xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 486ae710)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22ea2f5b9eSMatthew Ahrens  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26ecd6cf80Smarks #include <sys/cred.h>
27fa9e4066Sahrens #include <sys/zfs_context.h>
28fa9e4066Sahrens #include <sys/dmu_objset.h>
29fa9e4066Sahrens #include <sys/dsl_dir.h>
30fa9e4066Sahrens #include <sys/dsl_dataset.h>
31fa9e4066Sahrens #include <sys/dsl_prop.h>
32fa9e4066Sahrens #include <sys/dsl_pool.h>
331d452cf5Sahrens #include <sys/dsl_synctask.h>
34ecd6cf80Smarks #include <sys/dsl_deleg.h>
35fa9e4066Sahrens #include <sys/dnode.h>
36fa9e4066Sahrens #include <sys/dbuf.h>
37a2eea2e1Sahrens #include <sys/zvol.h>
38fa9e4066Sahrens #include <sys/dmu_tx.h>
39fa9e4066Sahrens #include <sys/zap.h>
40fa9e4066Sahrens #include <sys/zil.h>
41fa9e4066Sahrens #include <sys/dmu_impl.h>
42ecd6cf80Smarks #include <sys/zfs_ioctl.h>
43*486ae710SMatthew Ahrens #include <sys/sunddi.h>
44fa9e4066Sahrens 
45fa9e4066Sahrens spa_t *
46fa9e4066Sahrens dmu_objset_spa(objset_t *os)
47fa9e4066Sahrens {
48503ad85cSMatthew Ahrens 	return (os->os_spa);
49fa9e4066Sahrens }
50fa9e4066Sahrens 
51fa9e4066Sahrens zilog_t *
52fa9e4066Sahrens dmu_objset_zil(objset_t *os)
53fa9e4066Sahrens {
54503ad85cSMatthew Ahrens 	return (os->os_zil);
55fa9e4066Sahrens }
56fa9e4066Sahrens 
57fa9e4066Sahrens dsl_pool_t *
58fa9e4066Sahrens dmu_objset_pool(objset_t *os)
59fa9e4066Sahrens {
60fa9e4066Sahrens 	dsl_dataset_t *ds;
61fa9e4066Sahrens 
62503ad85cSMatthew Ahrens 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
63fa9e4066Sahrens 		return (ds->ds_dir->dd_pool);
64fa9e4066Sahrens 	else
65503ad85cSMatthew Ahrens 		return (spa_get_dsl(os->os_spa));
66fa9e4066Sahrens }
67fa9e4066Sahrens 
68fa9e4066Sahrens dsl_dataset_t *
69fa9e4066Sahrens dmu_objset_ds(objset_t *os)
70fa9e4066Sahrens {
71503ad85cSMatthew Ahrens 	return (os->os_dsl_dataset);
72fa9e4066Sahrens }
73fa9e4066Sahrens 
74fa9e4066Sahrens dmu_objset_type_t
75fa9e4066Sahrens dmu_objset_type(objset_t *os)
76fa9e4066Sahrens {
77503ad85cSMatthew Ahrens 	return (os->os_phys->os_type);
78fa9e4066Sahrens }
79fa9e4066Sahrens 
80fa9e4066Sahrens void
81fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
82fa9e4066Sahrens {
83503ad85cSMatthew Ahrens 	dsl_dataset_name(os->os_dsl_dataset, buf);
84fa9e4066Sahrens }
85fa9e4066Sahrens 
86fa9e4066Sahrens uint64_t
87fa9e4066Sahrens dmu_objset_id(objset_t *os)
88fa9e4066Sahrens {
89503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
90fa9e4066Sahrens 
91fa9e4066Sahrens 	return (ds ? ds->ds_object : 0);
92fa9e4066Sahrens }
93fa9e4066Sahrens 
94e09fa4daSNeil Perrin uint64_t
95e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
96e09fa4daSNeil Perrin {
97e09fa4daSNeil Perrin 	return (os->os_logbias);
98e09fa4daSNeil Perrin }
99e09fa4daSNeil Perrin 
100fa9e4066Sahrens static void
101fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
102fa9e4066Sahrens {
103503ad85cSMatthew Ahrens 	objset_t *os = arg;
104fa9e4066Sahrens 
105fa9e4066Sahrens 	/*
106fa9e4066Sahrens 	 * Inheritance should have been done by now.
107fa9e4066Sahrens 	 */
108fa9e4066Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
109fa9e4066Sahrens 
110503ad85cSMatthew Ahrens 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
111fa9e4066Sahrens }
112fa9e4066Sahrens 
113fa9e4066Sahrens static void
114fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
115fa9e4066Sahrens {
116503ad85cSMatthew Ahrens 	objset_t *os = arg;
117fa9e4066Sahrens 
118fa9e4066Sahrens 	/*
119fa9e4066Sahrens 	 * Inheritance and range checking should have been done by now.
120fa9e4066Sahrens 	 */
121fa9e4066Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
122fa9e4066Sahrens 
123503ad85cSMatthew Ahrens 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
124fa9e4066Sahrens }
125fa9e4066Sahrens 
126d0ad202dSahrens static void
127d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
128d0ad202dSahrens {
129503ad85cSMatthew Ahrens 	objset_t *os = arg;
130d0ad202dSahrens 
131d0ad202dSahrens 	/*
132d0ad202dSahrens 	 * Inheritance and range checking should have been done by now.
133d0ad202dSahrens 	 */
134d0ad202dSahrens 	ASSERT(newval > 0);
135503ad85cSMatthew Ahrens 	ASSERT(newval <= spa_max_replication(os->os_spa));
136d0ad202dSahrens 
137503ad85cSMatthew Ahrens 	os->os_copies = newval;
138d0ad202dSahrens }
139d0ad202dSahrens 
140b24ab676SJeff Bonwick static void
141b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
142b24ab676SJeff Bonwick {
143b24ab676SJeff Bonwick 	objset_t *os = arg;
144b24ab676SJeff Bonwick 	spa_t *spa = os->os_spa;
145b24ab676SJeff Bonwick 	enum zio_checksum checksum;
146b24ab676SJeff Bonwick 
147b24ab676SJeff Bonwick 	/*
148b24ab676SJeff Bonwick 	 * Inheritance should have been done by now.
149b24ab676SJeff Bonwick 	 */
150b24ab676SJeff Bonwick 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
151b24ab676SJeff Bonwick 
152b24ab676SJeff Bonwick 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
153b24ab676SJeff Bonwick 
154b24ab676SJeff Bonwick 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
155b24ab676SJeff Bonwick 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
156b24ab676SJeff Bonwick }
157b24ab676SJeff Bonwick 
1583baa08fcSek static void
1593baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval)
1603baa08fcSek {
161503ad85cSMatthew Ahrens 	objset_t *os = arg;
1623baa08fcSek 
1633baa08fcSek 	/*
1643baa08fcSek 	 * Inheritance and range checking should have been done by now.
1653baa08fcSek 	 */
1663baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1673baa08fcSek 	    newval == ZFS_CACHE_METADATA);
1683baa08fcSek 
169503ad85cSMatthew Ahrens 	os->os_primary_cache = newval;
1703baa08fcSek }
1713baa08fcSek 
1723baa08fcSek static void
1733baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval)
1743baa08fcSek {
175503ad85cSMatthew Ahrens 	objset_t *os = arg;
1763baa08fcSek 
1773baa08fcSek 	/*
1783baa08fcSek 	 * Inheritance and range checking should have been done by now.
1793baa08fcSek 	 */
1803baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1813baa08fcSek 	    newval == ZFS_CACHE_METADATA);
1823baa08fcSek 
183503ad85cSMatthew Ahrens 	os->os_secondary_cache = newval;
1843baa08fcSek }
1853baa08fcSek 
186e09fa4daSNeil Perrin static void
187e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
188e09fa4daSNeil Perrin {
189e09fa4daSNeil Perrin 	objset_t *os = arg;
190e09fa4daSNeil Perrin 
191e09fa4daSNeil Perrin 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
192e09fa4daSNeil Perrin 	    newval == ZFS_LOGBIAS_THROUGHPUT);
193e09fa4daSNeil Perrin 	os->os_logbias = newval;
194e09fa4daSNeil Perrin 	if (os->os_zil)
195e09fa4daSNeil Perrin 		zil_set_logbias(os->os_zil, newval);
196e09fa4daSNeil Perrin }
197e09fa4daSNeil Perrin 
198fa9e4066Sahrens void
199fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
200fa9e4066Sahrens {
201fa9e4066Sahrens 	objset_phys_t *osp = buf;
202fa9e4066Sahrens 
20314843421SMatthew Ahrens 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
204fa9e4066Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
205fa9e4066Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
206fa9e4066Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
20714843421SMatthew Ahrens 	osp->os_flags = BSWAP_64(osp->os_flags);
20814843421SMatthew Ahrens 	if (size == sizeof (objset_phys_t)) {
20914843421SMatthew Ahrens 		dnode_byteswap(&osp->os_userused_dnode);
21014843421SMatthew Ahrens 		dnode_byteswap(&osp->os_groupused_dnode);
21114843421SMatthew Ahrens 	}
212fa9e4066Sahrens }
213fa9e4066Sahrens 
214ea8dc4b6Seschrock int
215ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
216503ad85cSMatthew Ahrens     objset_t **osp)
217fa9e4066Sahrens {
218503ad85cSMatthew Ahrens 	objset_t *os;
219088f3894Sahrens 	int i, err;
220fa9e4066Sahrens 
22191ebeef5Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
22291ebeef5Sahrens 
223503ad85cSMatthew Ahrens 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
224503ad85cSMatthew Ahrens 	os->os_dsl_dataset = ds;
225503ad85cSMatthew Ahrens 	os->os_spa = spa;
226503ad85cSMatthew Ahrens 	os->os_rootbp = bp;
227503ad85cSMatthew Ahrens 	if (!BP_IS_HOLE(os->os_rootbp)) {
22813506d1eSmaybee 		uint32_t aflags = ARC_WAIT;
229ea8dc4b6Seschrock 		zbookmark_t zb;
230b24ab676SJeff Bonwick 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
231b24ab676SJeff Bonwick 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
232b24ab676SJeff Bonwick 
233503ad85cSMatthew Ahrens 		if (DMU_OS_IS_L2CACHEABLE(os))
2343baa08fcSek 			aflags |= ARC_L2CACHE;
235ea8dc4b6Seschrock 
236503ad85cSMatthew Ahrens 		dprintf_bp(os->os_rootbp, "reading %s", "");
237088f3894Sahrens 		/*
238088f3894Sahrens 		 * NB: when bprewrite scrub can change the bp,
239088f3894Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
240088f3894Sahrens 		 * could change, and we'll need a lock.
241088f3894Sahrens 		 */
242503ad85cSMatthew Ahrens 		err = arc_read_nolock(NULL, spa, os->os_rootbp,
243503ad85cSMatthew Ahrens 		    arc_getbuf_func, &os->os_phys_buf,
24413506d1eSmaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
245ea8dc4b6Seschrock 		if (err) {
246503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
247b87f3af3Sperrin 			/* convert checksum errors into IO errors */
248b87f3af3Sperrin 			if (err == ECKSUM)
249b87f3af3Sperrin 				err = EIO;
250ea8dc4b6Seschrock 			return (err);
251ea8dc4b6Seschrock 		}
25214843421SMatthew Ahrens 
25314843421SMatthew Ahrens 		/* Increase the blocksize if we are permitted. */
25414843421SMatthew Ahrens 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
255503ad85cSMatthew Ahrens 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
25614843421SMatthew Ahrens 			arc_buf_t *buf = arc_buf_alloc(spa,
257503ad85cSMatthew Ahrens 			    sizeof (objset_phys_t), &os->os_phys_buf,
25814843421SMatthew Ahrens 			    ARC_BUFC_METADATA);
25914843421SMatthew Ahrens 			bzero(buf->b_data, sizeof (objset_phys_t));
260503ad85cSMatthew Ahrens 			bcopy(os->os_phys_buf->b_data, buf->b_data,
261503ad85cSMatthew Ahrens 			    arc_buf_size(os->os_phys_buf));
262503ad85cSMatthew Ahrens 			(void) arc_buf_remove_ref(os->os_phys_buf,
263503ad85cSMatthew Ahrens 			    &os->os_phys_buf);
264503ad85cSMatthew Ahrens 			os->os_phys_buf = buf;
26514843421SMatthew Ahrens 		}
26614843421SMatthew Ahrens 
267503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
268503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
269fa9e4066Sahrens 	} else {
27014843421SMatthew Ahrens 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
27114843421SMatthew Ahrens 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
272503ad85cSMatthew Ahrens 		os->os_phys_buf = arc_buf_alloc(spa, size,
273503ad85cSMatthew Ahrens 		    &os->os_phys_buf, ARC_BUFC_METADATA);
274503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
275503ad85cSMatthew Ahrens 		bzero(os->os_phys, size);
276fa9e4066Sahrens 	}
277fa9e4066Sahrens 
278fa9e4066Sahrens 	/*
279fa9e4066Sahrens 	 * Note: the changed_cb will be called once before the register
280fa9e4066Sahrens 	 * func returns, thus changing the checksum/compression from the
2813baa08fcSek 	 * default (fletcher2/off).  Snapshots don't need to know about
2823baa08fcSek 	 * checksum/compression/copies.
283fa9e4066Sahrens 	 */
2843baa08fcSek 	if (ds) {
2853baa08fcSek 		err = dsl_prop_register(ds, "primarycache",
286503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
287d0ad202dSahrens 		if (err == 0)
2883baa08fcSek 			err = dsl_prop_register(ds, "secondarycache",
289503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
2903baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
2913baa08fcSek 			if (err == 0)
2923baa08fcSek 				err = dsl_prop_register(ds, "checksum",
293503ad85cSMatthew Ahrens 				    checksum_changed_cb, os);
2943baa08fcSek 			if (err == 0)
2953baa08fcSek 				err = dsl_prop_register(ds, "compression",
296503ad85cSMatthew Ahrens 				    compression_changed_cb, os);
2973baa08fcSek 			if (err == 0)
2983baa08fcSek 				err = dsl_prop_register(ds, "copies",
299503ad85cSMatthew Ahrens 				    copies_changed_cb, os);
300b24ab676SJeff Bonwick 			if (err == 0)
301b24ab676SJeff Bonwick 				err = dsl_prop_register(ds, "dedup",
302b24ab676SJeff Bonwick 				    dedup_changed_cb, os);
303e09fa4daSNeil Perrin 			if (err == 0)
304e09fa4daSNeil Perrin 				err = dsl_prop_register(ds, "logbias",
305e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
3063baa08fcSek 		}
307ea8dc4b6Seschrock 		if (err) {
308503ad85cSMatthew Ahrens 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
309503ad85cSMatthew Ahrens 			    &os->os_phys_buf) == 1);
310503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
311ea8dc4b6Seschrock 			return (err);
312ea8dc4b6Seschrock 		}
31399653d4eSeschrock 	} else if (ds == NULL) {
314fa9e4066Sahrens 		/* It's the meta-objset. */
315503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
316503ad85cSMatthew Ahrens 		os->os_compress = ZIO_COMPRESS_LZJB;
317503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
318b24ab676SJeff Bonwick 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
319b24ab676SJeff Bonwick 		os->os_dedup_verify = 0;
320b24ab676SJeff Bonwick 		os->os_logbias = 0;
321503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
322503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
323fa9e4066Sahrens 	}
324fa9e4066Sahrens 
325503ad85cSMatthew Ahrens 	os->os_zil_header = os->os_phys->os_zil_header;
326503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
327fa9e4066Sahrens 
328fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
329503ad85cSMatthew Ahrens 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
330fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
331503ad85cSMatthew Ahrens 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
332fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
333fa9e4066Sahrens 	}
334503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
335fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
336503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
337fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
338fa9e4066Sahrens 
339503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
340503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
341503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
342503ad85cSMatthew Ahrens 
343503ad85cSMatthew Ahrens 	os->os_meta_dnode = dnode_special_open(os,
344503ad85cSMatthew Ahrens 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
345503ad85cSMatthew Ahrens 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
346503ad85cSMatthew Ahrens 		os->os_userused_dnode = dnode_special_open(os,
347503ad85cSMatthew Ahrens 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
348503ad85cSMatthew Ahrens 		os->os_groupused_dnode = dnode_special_open(os,
349503ad85cSMatthew Ahrens 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
35014843421SMatthew Ahrens 	}
351fa9e4066Sahrens 
35291ebeef5Sahrens 	/*
35391ebeef5Sahrens 	 * We should be the only thread trying to do this because we
35491ebeef5Sahrens 	 * have ds_opening_lock
35591ebeef5Sahrens 	 */
35691ebeef5Sahrens 	if (ds) {
357503ad85cSMatthew Ahrens 		mutex_enter(&ds->ds_lock);
358503ad85cSMatthew Ahrens 		ASSERT(ds->ds_objset == NULL);
359503ad85cSMatthew Ahrens 		ds->ds_objset = os;
360503ad85cSMatthew Ahrens 		mutex_exit(&ds->ds_lock);
361fa9e4066Sahrens 	}
362fa9e4066Sahrens 
363503ad85cSMatthew Ahrens 	*osp = os;
364ea8dc4b6Seschrock 	return (0);
365fa9e4066Sahrens }
366fa9e4066Sahrens 
367503ad85cSMatthew Ahrens int
368503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
3693cb34c60Sahrens {
370503ad85cSMatthew Ahrens 	int err = 0;
3713cb34c60Sahrens 
3723cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
373503ad85cSMatthew Ahrens 	*osp = ds->ds_objset;
374503ad85cSMatthew Ahrens 	if (*osp == NULL) {
3753cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
376503ad85cSMatthew Ahrens 		    ds, &ds->ds_phys->ds_bp, osp);
3773cb34c60Sahrens 	}
3783cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
379503ad85cSMatthew Ahrens 	return (err);
3803cb34c60Sahrens }
3813cb34c60Sahrens 
382503ad85cSMatthew Ahrens /* called from zpl */
3833cb34c60Sahrens int
384503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
3853cb34c60Sahrens {
386503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
3873cb34c60Sahrens 	int err;
3883cb34c60Sahrens 
389503ad85cSMatthew Ahrens 	err = dsl_dataset_hold(name, tag, &ds);
3903cb34c60Sahrens 	if (err)
391503ad85cSMatthew Ahrens 		return (err);
392503ad85cSMatthew Ahrens 
393503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
394503ad85cSMatthew Ahrens 	if (err)
395503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
396503ad85cSMatthew Ahrens 
3973cb34c60Sahrens 	return (err);
3983cb34c60Sahrens }
3993cb34c60Sahrens 
400fa9e4066Sahrens /* called from zpl */
401fa9e4066Sahrens int
402503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
403503ad85cSMatthew Ahrens     boolean_t readonly, void *tag, objset_t **osp)
404fa9e4066Sahrens {
405f18faf3fSek 	dsl_dataset_t *ds;
406f18faf3fSek 	int err;
407fa9e4066Sahrens 
408503ad85cSMatthew Ahrens 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
409503ad85cSMatthew Ahrens 	if (err)
410fa9e4066Sahrens 		return (err);
411fa9e4066Sahrens 
412503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
4133cb34c60Sahrens 	if (err) {
414503ad85cSMatthew Ahrens 		dsl_dataset_disown(ds, tag);
415681d9761SEric Taylor 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
416503ad85cSMatthew Ahrens 		dmu_objset_disown(*osp, tag);
417503ad85cSMatthew Ahrens 		return (EINVAL);
418681d9761SEric Taylor 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
419681d9761SEric Taylor 		dmu_objset_disown(*osp, tag);
420681d9761SEric Taylor 		return (EROFS);
421fa9e4066Sahrens 	}
4223cb34c60Sahrens 	return (err);
423fa9e4066Sahrens }
424fa9e4066Sahrens 
425fa9e4066Sahrens void
426503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
427fa9e4066Sahrens {
428503ad85cSMatthew Ahrens 	dsl_dataset_rele(os->os_dsl_dataset, tag);
429503ad85cSMatthew Ahrens }
430503ad85cSMatthew Ahrens 
431503ad85cSMatthew Ahrens void
432503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
433503ad85cSMatthew Ahrens {
434503ad85cSMatthew Ahrens 	dsl_dataset_disown(os->os_dsl_dataset, tag);
435fa9e4066Sahrens }
436fa9e4066Sahrens 
437436b2950Sperrin int
4381934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
439ea8dc4b6Seschrock {
440ea8dc4b6Seschrock 	dnode_t *dn;
441c543ec06Sahrens 
442503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
443c543ec06Sahrens 
444c543ec06Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
445503ad85cSMatthew Ahrens 	list_remove(&os->os_dnodes, os->os_meta_dnode);
446503ad85cSMatthew Ahrens 	list_insert_tail(&os->os_dnodes, os->os_meta_dnode);
447ea8dc4b6Seschrock 
448ea8dc4b6Seschrock 	/*
449c543ec06Sahrens 	 * Find the first dnode with holds.  We have to do this dance
450c543ec06Sahrens 	 * because dnode_add_ref() only works if you already have a
451c543ec06Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
452c543ec06Sahrens 	 * skip.
453ea8dc4b6Seschrock 	 */
454503ad85cSMatthew Ahrens 	for (dn = list_head(&os->os_dnodes);
4551934e92fSmaybee 	    dn && !dnode_add_ref(dn, FTAG);
456503ad85cSMatthew Ahrens 	    dn = list_next(&os->os_dnodes, dn))
457c543ec06Sahrens 		continue;
458c543ec06Sahrens 
459c543ec06Sahrens 	while (dn) {
460c543ec06Sahrens 		dnode_t *next_dn = dn;
461c543ec06Sahrens 
462c543ec06Sahrens 		do {
463503ad85cSMatthew Ahrens 			next_dn = list_next(&os->os_dnodes, next_dn);
4641934e92fSmaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
465c543ec06Sahrens 
466503ad85cSMatthew Ahrens 		mutex_exit(&os->os_lock);
4671934e92fSmaybee 		dnode_evict_dbufs(dn);
468c543ec06Sahrens 		dnode_rele(dn, FTAG);
469503ad85cSMatthew Ahrens 		mutex_enter(&os->os_lock);
470c543ec06Sahrens 		dn = next_dn;
471ea8dc4b6Seschrock 	}
472503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
473503ad85cSMatthew Ahrens 	return (list_head(&os->os_dnodes) != os->os_meta_dnode);
474ea8dc4b6Seschrock }
475ea8dc4b6Seschrock 
476fa9e4066Sahrens void
477503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
478fa9e4066Sahrens {
479503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
480fa9e4066Sahrens 
481b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
482b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, t));
483fa9e4066Sahrens 
4843baa08fcSek 	if (ds) {
4853baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
4863baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
487503ad85cSMatthew Ahrens 			    checksum_changed_cb, os));
4883baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
489503ad85cSMatthew Ahrens 			    compression_changed_cb, os));
4903baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
491503ad85cSMatthew Ahrens 			    copies_changed_cb, os));
492b24ab676SJeff Bonwick 			VERIFY(0 == dsl_prop_unregister(ds, "dedup",
493b24ab676SJeff Bonwick 			    dedup_changed_cb, os));
494e09fa4daSNeil Perrin 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
495e09fa4daSNeil Perrin 			    logbias_changed_cb, os));
4963baa08fcSek 		}
4973baa08fcSek 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
498503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os));
4993baa08fcSek 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
500503ad85cSMatthew Ahrens 		    secondary_cache_changed_cb, os));
501fa9e4066Sahrens 	}
502fa9e4066Sahrens 
503ea8dc4b6Seschrock 	/*
504ea8dc4b6Seschrock 	 * We should need only a single pass over the dnode list, since
505ea8dc4b6Seschrock 	 * nothing can be added to the list at this point.
506ea8dc4b6Seschrock 	 */
507503ad85cSMatthew Ahrens 	(void) dmu_objset_evict_dbufs(os);
508ea8dc4b6Seschrock 
509503ad85cSMatthew Ahrens 	dnode_special_close(os->os_meta_dnode);
510503ad85cSMatthew Ahrens 	if (os->os_userused_dnode) {
511503ad85cSMatthew Ahrens 		dnode_special_close(os->os_userused_dnode);
512503ad85cSMatthew Ahrens 		dnode_special_close(os->os_groupused_dnode);
51314843421SMatthew Ahrens 	}
514503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
515fa9e4066Sahrens 
516503ad85cSMatthew Ahrens 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
51714843421SMatthew Ahrens 
518503ad85cSMatthew Ahrens 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
519503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
520503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
521503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
522503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
523fa9e4066Sahrens }
524fa9e4066Sahrens 
52571eb0538SChris Kirby timestruc_t
52671eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
52771eb0538SChris Kirby {
52871eb0538SChris Kirby 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
52971eb0538SChris Kirby }
53071eb0538SChris Kirby 
531fa9e4066Sahrens /* called from dsl for meta-objset */
532503ad85cSMatthew Ahrens objset_t *
533c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
534c717a561Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
535fa9e4066Sahrens {
536503ad85cSMatthew Ahrens 	objset_t *os;
537fa9e4066Sahrens 	dnode_t *mdn;
538fa9e4066Sahrens 
539fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
54091ebeef5Sahrens 	if (ds)
54191ebeef5Sahrens 		mutex_enter(&ds->ds_opening_lock);
542503ad85cSMatthew Ahrens 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
54391ebeef5Sahrens 	if (ds)
54491ebeef5Sahrens 		mutex_exit(&ds->ds_opening_lock);
545503ad85cSMatthew Ahrens 	mdn = os->os_meta_dnode;
546fa9e4066Sahrens 
547fa9e4066Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
548fa9e4066Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
549fa9e4066Sahrens 
550fa9e4066Sahrens 	/*
551fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
552fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
553fa9e4066Sahrens 	 * we are also accessing it in open context.
554fa9e4066Sahrens 	 *
555fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
556fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
557fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
558fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
559fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
560fa9e4066Sahrens 	 */
561ea8dc4b6Seschrock 	if (ds != NULL) {
562ea8dc4b6Seschrock 		int levels = 1;
563ea8dc4b6Seschrock 
564ea8dc4b6Seschrock 		/*
565ea8dc4b6Seschrock 		 * Determine the number of levels necessary for the meta-dnode
566ea8dc4b6Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
567ea8dc4b6Seschrock 		 */
568ea8dc4b6Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
569ea8dc4b6Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
570ea8dc4b6Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
571ea8dc4b6Seschrock 			levels++;
572ea8dc4b6Seschrock 
573fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
574ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
575ea8dc4b6Seschrock 	}
576fa9e4066Sahrens 
577fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
578fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
579fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
580503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
581503ad85cSMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
582503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
583503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
58414843421SMatthew Ahrens 	}
585fa9e4066Sahrens 
586fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
587fa9e4066Sahrens 
588503ad85cSMatthew Ahrens 	return (os);
589fa9e4066Sahrens }
590fa9e4066Sahrens 
591fa9e4066Sahrens struct oscarg {
592ecd6cf80Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
593fa9e4066Sahrens 	void *userarg;
594ae46e4c7SMatthew Ahrens 	dsl_dataset_t *clone_origin;
595fa9e4066Sahrens 	const char *lastname;
596fa9e4066Sahrens 	dmu_objset_type_t type;
597ab04eb8eStimh 	uint64_t flags;
598fa9e4066Sahrens };
599fa9e4066Sahrens 
600ecd6cf80Smarks /*ARGSUSED*/
601fa9e4066Sahrens static int
6021d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
603fa9e4066Sahrens {
6041d452cf5Sahrens 	dsl_dir_t *dd = arg1;
6051d452cf5Sahrens 	struct oscarg *oa = arg2;
6061d452cf5Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
607fa9e4066Sahrens 	int err;
6081d452cf5Sahrens 	uint64_t ddobj;
6091d452cf5Sahrens 
6101d452cf5Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
6111d452cf5Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
6121d452cf5Sahrens 	if (err != ENOENT)
6131d452cf5Sahrens 		return (err ? err : EEXIST);
6141d452cf5Sahrens 
615ae46e4c7SMatthew Ahrens 	if (oa->clone_origin != NULL) {
616ae46e4c7SMatthew Ahrens 		/* You can't clone across pools. */
617ae46e4c7SMatthew Ahrens 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
6181d452cf5Sahrens 			return (EXDEV);
6191d452cf5Sahrens 
620ae46e4c7SMatthew Ahrens 		/* You can only clone snapshots, not the head datasets. */
621ae46e4c7SMatthew Ahrens 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
6221d452cf5Sahrens 			return (EINVAL);
6231d452cf5Sahrens 	}
624ecd6cf80Smarks 
6251d452cf5Sahrens 	return (0);
6261d452cf5Sahrens }
6271d452cf5Sahrens 
6281d452cf5Sahrens static void
629ecd6cf80Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
6301d452cf5Sahrens {
6311d452cf5Sahrens 	dsl_dir_t *dd = arg1;
6321d452cf5Sahrens 	struct oscarg *oa = arg2;
6331d452cf5Sahrens 	uint64_t dsobj;
634fa9e4066Sahrens 
635fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
636fa9e4066Sahrens 
6371d452cf5Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
638ae46e4c7SMatthew Ahrens 	    oa->clone_origin, oa->flags, cr, tx);
639fa9e4066Sahrens 
640ae46e4c7SMatthew Ahrens 	if (oa->clone_origin == NULL) {
641ae46e4c7SMatthew Ahrens 		dsl_dataset_t *ds;
642ae46e4c7SMatthew Ahrens 		blkptr_t *bp;
643503ad85cSMatthew Ahrens 		objset_t *os;
644fa9e4066Sahrens 
645ae46e4c7SMatthew Ahrens 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
646ae46e4c7SMatthew Ahrens 		    FTAG, &ds));
647ae46e4c7SMatthew Ahrens 		bp = dsl_dataset_get_blkptr(ds);
648ae46e4c7SMatthew Ahrens 		ASSERT(BP_IS_HOLE(bp));
649ae46e4c7SMatthew Ahrens 
650503ad85cSMatthew Ahrens 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
651c717a561Smaybee 		    ds, bp, oa->type, tx);
652fa9e4066Sahrens 
653fa9e4066Sahrens 		if (oa->userfunc)
654503ad85cSMatthew Ahrens 			oa->userfunc(os, oa->userarg, cr, tx);
655ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
656fa9e4066Sahrens 	}
657ecd6cf80Smarks 
658ecd6cf80Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
659ecd6cf80Smarks 	    tx, cr, "dataset = %llu", dsobj);
660fa9e4066Sahrens }
661fa9e4066Sahrens 
662fa9e4066Sahrens int
663ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
664ecd6cf80Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
665fa9e4066Sahrens {
6661d452cf5Sahrens 	dsl_dir_t *pdd;
667fa9e4066Sahrens 	const char *tail;
668fa9e4066Sahrens 	int err = 0;
6691d452cf5Sahrens 	struct oscarg oa = { 0 };
670fa9e4066Sahrens 
6711d452cf5Sahrens 	ASSERT(strchr(name, '@') == NULL);
6721d452cf5Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
673ea8dc4b6Seschrock 	if (err)
674ea8dc4b6Seschrock 		return (err);
675fa9e4066Sahrens 	if (tail == NULL) {
6761d452cf5Sahrens 		dsl_dir_close(pdd, FTAG);
677fa9e4066Sahrens 		return (EEXIST);
678fa9e4066Sahrens 	}
679fa9e4066Sahrens 
6801d452cf5Sahrens 	oa.userfunc = func;
6811d452cf5Sahrens 	oa.userarg = arg;
6821d452cf5Sahrens 	oa.lastname = tail;
6831d452cf5Sahrens 	oa.type = type;
684ab04eb8eStimh 	oa.flags = flags;
685ecd6cf80Smarks 
686ae46e4c7SMatthew Ahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
687ae46e4c7SMatthew Ahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
688ae46e4c7SMatthew Ahrens 	dsl_dir_close(pdd, FTAG);
689ae46e4c7SMatthew Ahrens 	return (err);
690ae46e4c7SMatthew Ahrens }
691ae46e4c7SMatthew Ahrens 
692ae46e4c7SMatthew Ahrens int
693ae46e4c7SMatthew Ahrens dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
694ae46e4c7SMatthew Ahrens {
695ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
696ae46e4c7SMatthew Ahrens 	const char *tail;
697ae46e4c7SMatthew Ahrens 	int err = 0;
698ae46e4c7SMatthew Ahrens 	struct oscarg oa = { 0 };
699ae46e4c7SMatthew Ahrens 
700ae46e4c7SMatthew Ahrens 	ASSERT(strchr(name, '@') == NULL);
701ae46e4c7SMatthew Ahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
702ae46e4c7SMatthew Ahrens 	if (err)
703ae46e4c7SMatthew Ahrens 		return (err);
704ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
705ae46e4c7SMatthew Ahrens 		dsl_dir_close(pdd, FTAG);
706ae46e4c7SMatthew Ahrens 		return (EEXIST);
707fa9e4066Sahrens 	}
708ae46e4c7SMatthew Ahrens 
709ae46e4c7SMatthew Ahrens 	oa.lastname = tail;
710ae46e4c7SMatthew Ahrens 	oa.clone_origin = clone_origin;
711ae46e4c7SMatthew Ahrens 	oa.flags = flags;
712ae46e4c7SMatthew Ahrens 
7131d452cf5Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
7141d452cf5Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
7151d452cf5Sahrens 	dsl_dir_close(pdd, FTAG);
716fa9e4066Sahrens 	return (err);
717fa9e4066Sahrens }
718fa9e4066Sahrens 
719fa9e4066Sahrens int
720842727c2SChris Kirby dmu_objset_destroy(const char *name, boolean_t defer)
721fa9e4066Sahrens {
722503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
723fa9e4066Sahrens 	int error;
724fa9e4066Sahrens 
725fa9e4066Sahrens 	/*
726ae46e4c7SMatthew Ahrens 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
727ae46e4c7SMatthew Ahrens 	 * intent log, but if there is an active log, it has blocks that
728ae46e4c7SMatthew Ahrens 	 * are allocated, but may not yet be reflected in the on-disk
729ae46e4c7SMatthew Ahrens 	 * structure.  Only the ZIL knows how to free them, so we have
730ae46e4c7SMatthew Ahrens 	 * to call into it here.
731fa9e4066Sahrens 	 */
732503ad85cSMatthew Ahrens 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
733fa9e4066Sahrens 	if (error == 0) {
734503ad85cSMatthew Ahrens 		objset_t *os;
735503ad85cSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) == 0)
736503ad85cSMatthew Ahrens 			zil_destroy(dmu_objset_zil(os), B_FALSE);
737503ad85cSMatthew Ahrens 		error = dsl_dataset_destroy(ds, FTAG, defer);
738ae46e4c7SMatthew Ahrens 		/* dsl_dataset_destroy() closes the ds. */
739fa9e4066Sahrens 	}
740fa9e4066Sahrens 
7413cb34c60Sahrens 	return (error);
742fa9e4066Sahrens }
743fa9e4066Sahrens 
7441d452cf5Sahrens struct snaparg {
7451d452cf5Sahrens 	dsl_sync_task_group_t *dstg;
7461d452cf5Sahrens 	char *snapname;
7471d452cf5Sahrens 	char failed[MAXPATHLEN];
748ecd6cf80Smarks 	boolean_t checkperms;
749ea2f5b9eSMatthew Ahrens 	nvlist_t *props;
7503cb34c60Sahrens };
7513cb34c60Sahrens 
752ea2f5b9eSMatthew Ahrens static int
753ea2f5b9eSMatthew Ahrens snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
754ea2f5b9eSMatthew Ahrens {
755ea2f5b9eSMatthew Ahrens 	objset_t *os = arg1;
756ea2f5b9eSMatthew Ahrens 	struct snaparg *sn = arg2;
757ea2f5b9eSMatthew Ahrens 
758ea2f5b9eSMatthew Ahrens 	/* The props have already been checked by zfs_check_userprops(). */
759ea2f5b9eSMatthew Ahrens 
760503ad85cSMatthew Ahrens 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
761ea2f5b9eSMatthew Ahrens 	    sn->snapname, tx));
762ea2f5b9eSMatthew Ahrens }
763ea2f5b9eSMatthew Ahrens 
764ea2f5b9eSMatthew Ahrens static void
765ea2f5b9eSMatthew Ahrens snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
766ea2f5b9eSMatthew Ahrens {
767ea2f5b9eSMatthew Ahrens 	objset_t *os = arg1;
768503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
769ea2f5b9eSMatthew Ahrens 	struct snaparg *sn = arg2;
770ea2f5b9eSMatthew Ahrens 
771ea2f5b9eSMatthew Ahrens 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
772ea2f5b9eSMatthew Ahrens 
77392241e0bSTom Erickson 	if (sn->props) {
77492241e0bSTom Erickson 		dsl_props_arg_t pa;
77592241e0bSTom Erickson 		pa.pa_props = sn->props;
77692241e0bSTom Erickson 		pa.pa_source = ZPROP_SRC_LOCAL;
77792241e0bSTom Erickson 		dsl_props_set_sync(ds->ds_prev, &pa, cr, tx);
77892241e0bSTom Erickson 	}
779ea2f5b9eSMatthew Ahrens }
7801d452cf5Sahrens 
7811d452cf5Sahrens static int
7821d452cf5Sahrens dmu_objset_snapshot_one(char *name, void *arg)
7831d452cf5Sahrens {
7841d452cf5Sahrens 	struct snaparg *sn = arg;
7851d452cf5Sahrens 	objset_t *os;
7861d452cf5Sahrens 	int err;
7871d452cf5Sahrens 
7881d452cf5Sahrens 	(void) strcpy(sn->failed, name);
7891d452cf5Sahrens 
790ecd6cf80Smarks 	/*
791ecd6cf80Smarks 	 * Check permissions only when requested.  This only applies when
792ecd6cf80Smarks 	 * doing a recursive snapshot.  The permission checks for the starting
793ecd6cf80Smarks 	 * dataset have already been performed in zfs_secpolicy_snapshot()
794ecd6cf80Smarks 	 */
795ecd6cf80Smarks 	if (sn->checkperms == B_TRUE &&
796ecd6cf80Smarks 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
797ecd6cf80Smarks 		return (err);
798ecd6cf80Smarks 
799503ad85cSMatthew Ahrens 	err = dmu_objset_hold(name, sn, &os);
8001d452cf5Sahrens 	if (err != 0)
8011d452cf5Sahrens 		return (err);
8021d452cf5Sahrens 
803745cd3c5Smaybee 	/* If the objset is in an inconsistent state, return busy */
804503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
805503ad85cSMatthew Ahrens 		dmu_objset_rele(os, sn);
806f2e10be3Srm 		return (EBUSY);
807f2e10be3Srm 	}
808f2e10be3Srm 
8091d452cf5Sahrens 	/*
8101d452cf5Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
8111d452cf5Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
8121d452cf5Sahrens 	 * a side effect.
8131d452cf5Sahrens 	 */
8141d452cf5Sahrens 	err = zil_suspend(dmu_objset_zil(os));
8151d452cf5Sahrens 	if (err == 0) {
816ea2f5b9eSMatthew Ahrens 		dsl_sync_task_create(sn->dstg, snapshot_check,
817ea2f5b9eSMatthew Ahrens 		    snapshot_sync, os, sn, 3);
818f2e10be3Srm 	} else {
819503ad85cSMatthew Ahrens 		dmu_objset_rele(os, sn);
8201d452cf5Sahrens 	}
821f2e10be3Srm 
8221d452cf5Sahrens 	return (err);
8231d452cf5Sahrens }
8241d452cf5Sahrens 
8251d452cf5Sahrens int
826ea2f5b9eSMatthew Ahrens dmu_objset_snapshot(char *fsname, char *snapname,
827ea2f5b9eSMatthew Ahrens     nvlist_t *props, boolean_t recursive)
8281d452cf5Sahrens {
8291d452cf5Sahrens 	dsl_sync_task_t *dst;
830ea2f5b9eSMatthew Ahrens 	struct snaparg sn;
8311d452cf5Sahrens 	spa_t *spa;
8321d452cf5Sahrens 	int err;
8331d452cf5Sahrens 
8341d452cf5Sahrens 	(void) strcpy(sn.failed, fsname);
8351d452cf5Sahrens 
83640feaa91Sahrens 	err = spa_open(fsname, &spa, FTAG);
8371d452cf5Sahrens 	if (err)
8381d452cf5Sahrens 		return (err);
8391d452cf5Sahrens 
8401d452cf5Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8411d452cf5Sahrens 	sn.snapname = snapname;
842ea2f5b9eSMatthew Ahrens 	sn.props = props;
8431d452cf5Sahrens 
8440b69c2f0Sahrens 	if (recursive) {
845ecd6cf80Smarks 		sn.checkperms = B_TRUE;
8460b69c2f0Sahrens 		err = dmu_objset_find(fsname,
8470b69c2f0Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
8480b69c2f0Sahrens 	} else {
849ecd6cf80Smarks 		sn.checkperms = B_FALSE;
8501d452cf5Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
8510b69c2f0Sahrens 	}
8521d452cf5Sahrens 
853ea2f5b9eSMatthew Ahrens 	if (err == 0)
854ea2f5b9eSMatthew Ahrens 		err = dsl_sync_task_group_wait(sn.dstg);
8551d452cf5Sahrens 
8561d452cf5Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
8571d452cf5Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
858ea2f5b9eSMatthew Ahrens 		objset_t *os = dst->dst_arg1;
859503ad85cSMatthew Ahrens 		dsl_dataset_t *ds = os->os_dsl_dataset;
8601d452cf5Sahrens 		if (dst->dst_err)
8613cb34c60Sahrens 			dsl_dataset_name(ds, sn.failed);
862ea2f5b9eSMatthew Ahrens 		zil_resume(dmu_objset_zil(os));
863503ad85cSMatthew Ahrens 		dmu_objset_rele(os, &sn);
8643cb34c60Sahrens 	}
8653cb34c60Sahrens 
8661d452cf5Sahrens 	if (err)
8671d452cf5Sahrens 		(void) strcpy(fsname, sn.failed);
8681d452cf5Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
8691d452cf5Sahrens 	spa_close(spa, FTAG);
8701d452cf5Sahrens 	return (err);
8711d452cf5Sahrens }
8721d452cf5Sahrens 
873fa9e4066Sahrens static void
87414843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
875fa9e4066Sahrens {
876c717a561Smaybee 	dnode_t *dn;
877faafa6e3Sahrens 
878c717a561Smaybee 	while (dn = list_head(list)) {
879c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
880c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
881c717a561Smaybee 		/*
88214843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
88314843421SMatthew Ahrens 		 * meta-dnode needs to set it ouside dnode_sync().
884c717a561Smaybee 		 */
885c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
886c717a561Smaybee 		ASSERT(dn->dn_zio);
887faafa6e3Sahrens 
888c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
889c717a561Smaybee 		list_remove(list, dn);
89014843421SMatthew Ahrens 
89114843421SMatthew Ahrens 		if (newlist) {
89214843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
89314843421SMatthew Ahrens 			list_insert_tail(newlist, dn);
89414843421SMatthew Ahrens 		}
89514843421SMatthew Ahrens 
896c717a561Smaybee 		dnode_sync(dn, tx);
897fa9e4066Sahrens 	}
898fa9e4066Sahrens }
899fa9e4066Sahrens 
900fa9e4066Sahrens /* ARGSUSED */
901fa9e4066Sahrens static void
902b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
903fa9e4066Sahrens {
904e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
905503ad85cSMatthew Ahrens 	objset_t *os = arg;
906c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
907fa9e4066Sahrens 
908e14bb325SJeff Bonwick 	ASSERT(bp == os->os_rootbp);
909e14bb325SJeff Bonwick 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
910e14bb325SJeff Bonwick 	ASSERT(BP_GET_LEVEL(bp) == 0);
9110a4e9518Sgw 
912fa9e4066Sahrens 	/*
91314843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
91414843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
91514843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
91614843421SMatthew Ahrens 	 * dnode and user/group accounting objects).
917fa9e4066Sahrens 	 */
91814843421SMatthew Ahrens 	bp->blk_fill = 0;
919e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
920c717a561Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
921b24ab676SJeff Bonwick }
922b24ab676SJeff Bonwick 
923b24ab676SJeff Bonwick /* ARGSUSED */
924b24ab676SJeff Bonwick static void
925b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
926b24ab676SJeff Bonwick {
927b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
928b24ab676SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
929b24ab676SJeff Bonwick 	objset_t *os = arg;
9300a4e9518Sgw 
931e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
932b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, bp_orig));
933e14bb325SJeff Bonwick 	} else {
934b24ab676SJeff Bonwick 		dsl_dataset_t *ds = os->os_dsl_dataset;
935b24ab676SJeff Bonwick 		dmu_tx_t *tx = os->os_synctx;
936b24ab676SJeff Bonwick 
937b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
938b24ab676SJeff Bonwick 		dsl_dataset_block_born(ds, bp, tx);
9390a4e9518Sgw 	}
940c717a561Smaybee }
941c717a561Smaybee 
942fa9e4066Sahrens /* called from dsl */
943fa9e4066Sahrens void
944503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
945fa9e4066Sahrens {
946fa9e4066Sahrens 	int txgoff;
947ea8dc4b6Seschrock 	zbookmark_t zb;
948b24ab676SJeff Bonwick 	zio_prop_t zp;
949c717a561Smaybee 	zio_t *zio;
950c717a561Smaybee 	list_t *list;
95114843421SMatthew Ahrens 	list_t *newlist = NULL;
952c717a561Smaybee 	dbuf_dirty_record_t *dr;
953c717a561Smaybee 
954c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
955fa9e4066Sahrens 
956fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
957fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
958fa9e4066Sahrens 	os->os_synctx = tx;
959fa9e4066Sahrens 
96087bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
96187bd5c1eSahrens 		/*
96287bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
96387bd5c1eSahrens 		 * spa_max_replication() could change, so reset
96487bd5c1eSahrens 		 * os_copies here.
96587bd5c1eSahrens 		 */
96687bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
96787bd5c1eSahrens 	}
96887bd5c1eSahrens 
969fa9e4066Sahrens 	/*
970c717a561Smaybee 	 * Create the root block IO
971fa9e4066Sahrens 	 */
972088f3894Sahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
97314843421SMatthew Ahrens 
974b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
975b24ab676SJeff Bonwick 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
976b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
977b24ab676SJeff Bonwick 
978b24ab676SJeff Bonwick 	dmu_write_policy(os, NULL, 0, 0, &zp);
979b24ab676SJeff Bonwick 
980b24ab676SJeff Bonwick 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
981b24ab676SJeff Bonwick 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
982b24ab676SJeff Bonwick 	    dmu_objset_write_ready, dmu_objset_write_done, os,
983e14bb325SJeff Bonwick 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
984c717a561Smaybee 
985c717a561Smaybee 	/*
98614843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
987c717a561Smaybee 	 */
988c717a561Smaybee 	os->os_meta_dnode->dn_zio = zio;
989c717a561Smaybee 	dnode_sync(os->os_meta_dnode, tx);
990c717a561Smaybee 
99114843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
99214843421SMatthew Ahrens 
99314843421SMatthew Ahrens 	if (os->os_userused_dnode &&
99414843421SMatthew Ahrens 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
99514843421SMatthew Ahrens 		os->os_userused_dnode->dn_zio = zio;
99614843421SMatthew Ahrens 		dnode_sync(os->os_userused_dnode, tx);
99714843421SMatthew Ahrens 		os->os_groupused_dnode->dn_zio = zio;
99814843421SMatthew Ahrens 		dnode_sync(os->os_groupused_dnode, tx);
99914843421SMatthew Ahrens 	}
100014843421SMatthew Ahrens 
1001c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
1002fa9e4066Sahrens 
100314843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
100414843421SMatthew Ahrens 		newlist = &os->os_synced_dnodes;
100514843421SMatthew Ahrens 		/*
100614843421SMatthew Ahrens 		 * We must create the list here because it uses the
100714843421SMatthew Ahrens 		 * dn_dirty_link[] of this txg.
100814843421SMatthew Ahrens 		 */
100914843421SMatthew Ahrens 		list_create(newlist, sizeof (dnode_t),
101014843421SMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
101114843421SMatthew Ahrens 	}
101214843421SMatthew Ahrens 
101314843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
101414843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1015fa9e4066Sahrens 
1016c717a561Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
1017c717a561Smaybee 	while (dr = list_head(list)) {
1018c717a561Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
1019c717a561Smaybee 		list_remove(list, dr);
1020c717a561Smaybee 		if (dr->dr_zio)
1021c717a561Smaybee 			zio_nowait(dr->dr_zio);
1022c717a561Smaybee 	}
1023c717a561Smaybee 	/*
1024c717a561Smaybee 	 * Free intent log blocks up to this tx.
1025c717a561Smaybee 	 */
1026c717a561Smaybee 	zil_sync(os->os_zil, tx);
1027088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
1028c717a561Smaybee 	zio_nowait(zio);
1029fa9e4066Sahrens }
1030fa9e4066Sahrens 
1031b24ab676SJeff Bonwick boolean_t
1032b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1033b24ab676SJeff Bonwick {
1034b24ab676SJeff Bonwick 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1035b24ab676SJeff Bonwick 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1036b24ab676SJeff Bonwick }
1037b24ab676SJeff Bonwick 
103814843421SMatthew Ahrens static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
103914843421SMatthew Ahrens 
104014843421SMatthew Ahrens void
104114843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
104214843421SMatthew Ahrens {
104314843421SMatthew Ahrens 	used_cbs[ost] = cb;
104414843421SMatthew Ahrens }
104514843421SMatthew Ahrens 
104614843421SMatthew Ahrens boolean_t
1047503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
104814843421SMatthew Ahrens {
104914843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
105014843421SMatthew Ahrens 	    used_cbs[os->os_phys->os_type] &&
105114843421SMatthew Ahrens 	    os->os_userused_dnode);
105214843421SMatthew Ahrens }
105314843421SMatthew Ahrens 
10549966ca11SMatthew Ahrens static void
10559966ca11SMatthew Ahrens do_userquota_callback(objset_t *os, dnode_phys_t *dnp,
10569966ca11SMatthew Ahrens     boolean_t subtract, dmu_tx_t *tx)
10579966ca11SMatthew Ahrens {
10589966ca11SMatthew Ahrens 	static const char zerobuf[DN_MAX_BONUSLEN] = {0};
10599966ca11SMatthew Ahrens 	uint64_t user, group;
10609966ca11SMatthew Ahrens 
10619966ca11SMatthew Ahrens 	ASSERT(dnp->dn_type != 0 ||
10629966ca11SMatthew Ahrens 	    (bcmp(DN_BONUS(dnp), zerobuf, DN_MAX_BONUSLEN) == 0 &&
10639966ca11SMatthew Ahrens 	    DN_USED_BYTES(dnp) == 0));
10649966ca11SMatthew Ahrens 
10659966ca11SMatthew Ahrens 	if ((dnp->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) &&
10669966ca11SMatthew Ahrens 	    0 == used_cbs[os->os_phys->os_type](dnp->dn_bonustype,
10679966ca11SMatthew Ahrens 	    DN_BONUS(dnp), &user, &group)) {
10689966ca11SMatthew Ahrens 		int64_t delta = DNODE_SIZE + DN_USED_BYTES(dnp);
10699966ca11SMatthew Ahrens 		if (subtract)
10709966ca11SMatthew Ahrens 			delta = -delta;
1071c33e334fSMatthew Ahrens 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
10729966ca11SMatthew Ahrens 		    user, delta, tx));
1073c33e334fSMatthew Ahrens 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
10749966ca11SMatthew Ahrens 		    group, delta, tx));
10759966ca11SMatthew Ahrens 	}
10769966ca11SMatthew Ahrens }
10779966ca11SMatthew Ahrens 
107814843421SMatthew Ahrens void
1079503ad85cSMatthew Ahrens dmu_objset_do_userquota_callbacks(objset_t *os, dmu_tx_t *tx)
108014843421SMatthew Ahrens {
108114843421SMatthew Ahrens 	dnode_t *dn;
108214843421SMatthew Ahrens 	list_t *list = &os->os_synced_dnodes;
108314843421SMatthew Ahrens 
108414843421SMatthew Ahrens 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
108514843421SMatthew Ahrens 
108614843421SMatthew Ahrens 	while (dn = list_head(list)) {
108714843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
108814843421SMatthew Ahrens 		ASSERT(dn->dn_oldphys);
108914843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
109014843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
109114843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
109214843421SMatthew Ahrens 
109314843421SMatthew Ahrens 		/* Allocate the user/groupused objects if necessary. */
109414843421SMatthew Ahrens 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
1095503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
109614843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT,
109714843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1098503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
109914843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT,
110014843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
110114843421SMatthew Ahrens 		}
110214843421SMatthew Ahrens 
110314843421SMatthew Ahrens 		/*
11049966ca11SMatthew Ahrens 		 * We intentionally modify the zap object even if the
11059966ca11SMatthew Ahrens 		 * net delta (due to phys-oldphys) is zero.  Otherwise
11069966ca11SMatthew Ahrens 		 * the block of the zap obj could be shared between
11079966ca11SMatthew Ahrens 		 * datasets but need to be different between them after
11089966ca11SMatthew Ahrens 		 * a bprewrite.
110914843421SMatthew Ahrens 		 */
11109966ca11SMatthew Ahrens 		do_userquota_callback(os, dn->dn_oldphys, B_TRUE, tx);
11119966ca11SMatthew Ahrens 		do_userquota_callback(os, dn->dn_phys, B_FALSE, tx);
111214843421SMatthew Ahrens 
111314843421SMatthew Ahrens 		/*
111414843421SMatthew Ahrens 		 * The mutex is needed here for interlock with dnode_allocate.
111514843421SMatthew Ahrens 		 */
111614843421SMatthew Ahrens 		mutex_enter(&dn->dn_mtx);
111714843421SMatthew Ahrens 		zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t));
111814843421SMatthew Ahrens 		dn->dn_oldphys = NULL;
111914843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
112014843421SMatthew Ahrens 
112114843421SMatthew Ahrens 		list_remove(list, dn);
112214843421SMatthew Ahrens 		dnode_rele(dn, list);
112314843421SMatthew Ahrens 	}
112414843421SMatthew Ahrens }
112514843421SMatthew Ahrens 
112614843421SMatthew Ahrens boolean_t
112714843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
112814843421SMatthew Ahrens {
1129503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
113014843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
113114843421SMatthew Ahrens }
113214843421SMatthew Ahrens 
113314843421SMatthew Ahrens int
113414843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
113514843421SMatthew Ahrens {
113614843421SMatthew Ahrens 	uint64_t obj;
113714843421SMatthew Ahrens 	int err = 0;
113814843421SMatthew Ahrens 
113914843421SMatthew Ahrens 	if (dmu_objset_userspace_present(os))
114014843421SMatthew Ahrens 		return (0);
1141503ad85cSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
114214843421SMatthew Ahrens 		return (ENOTSUP);
114314843421SMatthew Ahrens 	if (dmu_objset_is_snapshot(os))
114414843421SMatthew Ahrens 		return (EINVAL);
114514843421SMatthew Ahrens 
114614843421SMatthew Ahrens 	/*
114714843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
114814843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
114914843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
115014843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
115114843421SMatthew Ahrens 	 * independently.
115214843421SMatthew Ahrens 	 */
115314843421SMatthew Ahrens 
115414843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
115568857716SLin Ling 		dmu_tx_t *tx;
115614843421SMatthew Ahrens 		dmu_buf_t *db;
115714843421SMatthew Ahrens 		int objerr;
115814843421SMatthew Ahrens 
115914843421SMatthew Ahrens 		if (issig(JUSTLOOKING) && issig(FORREAL))
116014843421SMatthew Ahrens 			return (EINTR);
116114843421SMatthew Ahrens 
116214843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
116314843421SMatthew Ahrens 		if (objerr)
116414843421SMatthew Ahrens 			continue;
116568857716SLin Ling 		tx = dmu_tx_create(os);
116614843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
116714843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
116814843421SMatthew Ahrens 		if (objerr) {
116914843421SMatthew Ahrens 			dmu_tx_abort(tx);
117014843421SMatthew Ahrens 			continue;
117114843421SMatthew Ahrens 		}
117214843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
117314843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
117414843421SMatthew Ahrens 		dmu_tx_commit(tx);
117514843421SMatthew Ahrens 	}
117614843421SMatthew Ahrens 
1177503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
117814843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
117914843421SMatthew Ahrens 	return (0);
118014843421SMatthew Ahrens }
118114843421SMatthew Ahrens 
1182fa9e4066Sahrens void
1183a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1184a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1185fa9e4066Sahrens {
1186503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1187a2eea2e1Sahrens 	    usedobjsp, availobjsp);
1188a2eea2e1Sahrens }
1189a2eea2e1Sahrens 
1190a2eea2e1Sahrens uint64_t
1191a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1192a2eea2e1Sahrens {
1193503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1194a2eea2e1Sahrens }
1195a2eea2e1Sahrens 
1196a2eea2e1Sahrens void
1197a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1198a2eea2e1Sahrens {
1199503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
1200503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
1201503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1202a2eea2e1Sahrens }
1203a2eea2e1Sahrens 
1204a2eea2e1Sahrens void
1205a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1206a2eea2e1Sahrens {
1207503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
1208503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
1209a2eea2e1Sahrens 
1210503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1211503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1212a2eea2e1Sahrens 
1213a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1214503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
121514843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
121614843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
1217fa9e4066Sahrens }
1218fa9e4066Sahrens 
1219fa9e4066Sahrens int
1220fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1221fa9e4066Sahrens {
1222503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1223503ad85cSMatthew Ahrens 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1224fa9e4066Sahrens 	else
1225fa9e4066Sahrens 		return (B_FALSE);
1226fa9e4066Sahrens }
1227fa9e4066Sahrens 
1228ab04eb8eStimh int
1229ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1230ab04eb8eStimh     boolean_t *conflict)
1231ab04eb8eStimh {
1232503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1233ab04eb8eStimh 	uint64_t ignored;
1234ab04eb8eStimh 
1235ab04eb8eStimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1236ab04eb8eStimh 		return (ENOENT);
1237ab04eb8eStimh 
1238ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1239ab04eb8eStimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1240ab04eb8eStimh 	    real, maxlen, conflict));
1241ab04eb8eStimh }
1242ab04eb8eStimh 
1243fa9e4066Sahrens int
1244fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1245b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1246fa9e4066Sahrens {
1247503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1248fa9e4066Sahrens 	zap_cursor_t cursor;
1249fa9e4066Sahrens 	zap_attribute_t attr;
1250fa9e4066Sahrens 
1251fa9e4066Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1252fa9e4066Sahrens 		return (ENOENT);
1253fa9e4066Sahrens 
1254fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
1255fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1256fa9e4066Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1257fa9e4066Sahrens 
125887e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
125987e5029aSahrens 		zap_cursor_fini(&cursor);
126087e5029aSahrens 		return (ENOENT);
126187e5029aSahrens 	}
126287e5029aSahrens 
126387e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
126487e5029aSahrens 		zap_cursor_fini(&cursor);
126587e5029aSahrens 		return (ENAMETOOLONG);
126687e5029aSahrens 	}
126787e5029aSahrens 
126887e5029aSahrens 	(void) strcpy(name, attr.za_name);
126987e5029aSahrens 	if (idp)
127087e5029aSahrens 		*idp = attr.za_first_integer;
1271b38f0970Sck 	if (case_conflict)
1272b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
127387e5029aSahrens 	zap_cursor_advance(&cursor);
127487e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
127587e5029aSahrens 	zap_cursor_fini(&cursor);
127687e5029aSahrens 
127787e5029aSahrens 	return (0);
127887e5029aSahrens }
127987e5029aSahrens 
128087e5029aSahrens int
128187e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
128287e5029aSahrens     uint64_t *idp, uint64_t *offp)
128387e5029aSahrens {
1284503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
128587e5029aSahrens 	zap_cursor_t cursor;
128687e5029aSahrens 	zap_attribute_t attr;
128787e5029aSahrens 
128887e5029aSahrens 	/* there is no next dir on a snapshot! */
1289503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
129087e5029aSahrens 	    dd->dd_phys->dd_head_dataset_obj)
1291fa9e4066Sahrens 		return (ENOENT);
1292fa9e4066Sahrens 
129387e5029aSahrens 	zap_cursor_init_serialized(&cursor,
129487e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
129587e5029aSahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
129687e5029aSahrens 
129787e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
129887e5029aSahrens 		zap_cursor_fini(&cursor);
129987e5029aSahrens 		return (ENOENT);
130087e5029aSahrens 	}
130187e5029aSahrens 
130287e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
130387e5029aSahrens 		zap_cursor_fini(&cursor);
1304fa9e4066Sahrens 		return (ENAMETOOLONG);
130587e5029aSahrens 	}
1306fa9e4066Sahrens 
1307fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
130887e5029aSahrens 	if (idp)
130987e5029aSahrens 		*idp = attr.za_first_integer;
1310fa9e4066Sahrens 	zap_cursor_advance(&cursor);
1311fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
131287e5029aSahrens 	zap_cursor_fini(&cursor);
1313fa9e4066Sahrens 
1314fa9e4066Sahrens 	return (0);
1315fa9e4066Sahrens }
1316fa9e4066Sahrens 
1317088f3894Sahrens struct findarg {
1318088f3894Sahrens 	int (*func)(char *, void *);
1319088f3894Sahrens 	void *arg;
1320088f3894Sahrens };
1321088f3894Sahrens 
1322088f3894Sahrens /* ARGSUSED */
1323088f3894Sahrens static int
1324088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1325088f3894Sahrens {
1326088f3894Sahrens 	struct findarg *fa = arg;
1327088f3894Sahrens 	return (fa->func((char *)dsname, fa->arg));
1328088f3894Sahrens }
1329088f3894Sahrens 
1330fa9e4066Sahrens /*
1331fa9e4066Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1332088f3894Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1333fa9e4066Sahrens  */
13341d452cf5Sahrens int
13351d452cf5Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1336088f3894Sahrens {
1337088f3894Sahrens 	struct findarg fa;
1338088f3894Sahrens 	fa.func = func;
1339088f3894Sahrens 	fa.arg = arg;
1340088f3894Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
1341088f3894Sahrens }
1342088f3894Sahrens 
1343088f3894Sahrens /*
1344088f3894Sahrens  * Find all objsets under name, call func on each
1345088f3894Sahrens  */
1346088f3894Sahrens int
1347088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
1348088f3894Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
1349fa9e4066Sahrens {
1350fa9e4066Sahrens 	dsl_dir_t *dd;
1351088f3894Sahrens 	dsl_pool_t *dp;
1352088f3894Sahrens 	dsl_dataset_t *ds;
1353fa9e4066Sahrens 	zap_cursor_t zc;
1354b7661cccSmmusante 	zap_attribute_t *attr;
1355fa9e4066Sahrens 	char *child;
1356088f3894Sahrens 	uint64_t thisobj;
1357088f3894Sahrens 	int err;
1358fa9e4066Sahrens 
1359088f3894Sahrens 	if (name == NULL)
1360088f3894Sahrens 		name = spa_name(spa);
1361088f3894Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
1362ea8dc4b6Seschrock 	if (err)
13631d452cf5Sahrens 		return (err);
1364fa9e4066Sahrens 
1365088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1366088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
1367088f3894Sahrens 		dsl_dir_close(dd, FTAG);
1368088f3894Sahrens 		return (0);
1369088f3894Sahrens 	}
1370088f3894Sahrens 
1371088f3894Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
1372b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1373088f3894Sahrens 	dp = dd->dd_pool;
1374fa9e4066Sahrens 
1375fa9e4066Sahrens 	/*
1376fa9e4066Sahrens 	 * Iterate over all children.
1377fa9e4066Sahrens 	 */
13780b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
1379088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
13800b69c2f0Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
1381b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
13820b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
1383b7661cccSmmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
1384b7661cccSmmusante 			ASSERT(attr->za_num_integers == 1);
1385fa9e4066Sahrens 
1386*486ae710SMatthew Ahrens 			child = kmem_asprintf("%s/%s", name, attr->za_name);
1387088f3894Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
1388*486ae710SMatthew Ahrens 			strfree(child);
13890b69c2f0Sahrens 			if (err)
13900b69c2f0Sahrens 				break;
13910b69c2f0Sahrens 		}
13920b69c2f0Sahrens 		zap_cursor_fini(&zc);
13931d452cf5Sahrens 
13940b69c2f0Sahrens 		if (err) {
13950b69c2f0Sahrens 			dsl_dir_close(dd, FTAG);
1396b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
13970b69c2f0Sahrens 			return (err);
13980b69c2f0Sahrens 		}
1399fa9e4066Sahrens 	}
1400fa9e4066Sahrens 
1401fa9e4066Sahrens 	/*
1402fa9e4066Sahrens 	 * Iterate over all snapshots.
1403fa9e4066Sahrens 	 */
1404088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
1405088f3894Sahrens 		if (!dsl_pool_sync_context(dp))
1406088f3894Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
1407088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1408088f3894Sahrens 		if (!dsl_pool_sync_context(dp))
1409088f3894Sahrens 			rw_exit(&dp->dp_config_rwlock);
1410088f3894Sahrens 
1411088f3894Sahrens 		if (err == 0) {
1412088f3894Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1413088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
1414088f3894Sahrens 
1415088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1416088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
1417088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
1418088f3894Sahrens 				ASSERT(attr->za_integer_length ==
1419088f3894Sahrens 				    sizeof (uint64_t));
1420088f3894Sahrens 				ASSERT(attr->za_num_integers == 1);
1421088f3894Sahrens 
1422*486ae710SMatthew Ahrens 				child = kmem_asprintf("%s@%s",
1423*486ae710SMatthew Ahrens 				    name, attr->za_name);
1424088f3894Sahrens 				err = func(spa, attr->za_first_integer,
1425088f3894Sahrens 				    child, arg);
1426*486ae710SMatthew Ahrens 				strfree(child);
1427088f3894Sahrens 				if (err)
1428088f3894Sahrens 					break;
1429088f3894Sahrens 			}
1430088f3894Sahrens 			zap_cursor_fini(&zc);
1431fa9e4066Sahrens 		}
1432fa9e4066Sahrens 	}
1433fa9e4066Sahrens 
1434fa9e4066Sahrens 	dsl_dir_close(dd, FTAG);
1435b7661cccSmmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1436fa9e4066Sahrens 
14371d452cf5Sahrens 	if (err)
14381d452cf5Sahrens 		return (err);
14391d452cf5Sahrens 
1440fa9e4066Sahrens 	/*
1441fa9e4066Sahrens 	 * Apply to self if appropriate.
1442fa9e4066Sahrens 	 */
1443088f3894Sahrens 	err = func(spa, thisobj, name, arg);
14441d452cf5Sahrens 	return (err);
1445fa9e4066Sahrens }
1446f18faf3fSek 
14477f73c863SRich Morris /* ARGSUSED */
14487f73c863SRich Morris int
14497f73c863SRich Morris dmu_objset_prefetch(char *name, void *arg)
14507f73c863SRich Morris {
14517f73c863SRich Morris 	dsl_dataset_t *ds;
14527f73c863SRich Morris 
14537f73c863SRich Morris 	if (dsl_dataset_hold(name, FTAG, &ds))
14547f73c863SRich Morris 		return (0);
14557f73c863SRich Morris 
14567f73c863SRich Morris 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
14577f73c863SRich Morris 		mutex_enter(&ds->ds_opening_lock);
1458503ad85cSMatthew Ahrens 		if (ds->ds_objset == NULL) {
14597f73c863SRich Morris 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
14607f73c863SRich Morris 			zbookmark_t zb;
14617f73c863SRich Morris 
1462b24ab676SJeff Bonwick 			SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
1463b24ab676SJeff Bonwick 			    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
14647f73c863SRich Morris 
14657f73c863SRich Morris 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
14667f73c863SRich Morris 			    &ds->ds_phys->ds_bp, NULL, NULL,
14677f73c863SRich Morris 			    ZIO_PRIORITY_ASYNC_READ,
14687f73c863SRich Morris 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
14697f73c863SRich Morris 			    &aflags, &zb);
14707f73c863SRich Morris 		}
14717f73c863SRich Morris 		mutex_exit(&ds->ds_opening_lock);
14727f73c863SRich Morris 	}
14737f73c863SRich Morris 
14747f73c863SRich Morris 	dsl_dataset_rele(ds, FTAG);
14757f73c863SRich Morris 	return (0);
14767f73c863SRich Morris }
14777f73c863SRich Morris 
1478f18faf3fSek void
1479f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr)
1480f18faf3fSek {
1481503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1482503ad85cSMatthew Ahrens 	os->os_user_ptr = user_ptr;
1483f18faf3fSek }
1484f18faf3fSek 
1485f18faf3fSek void *
1486f18faf3fSek dmu_objset_get_user(objset_t *os)
1487f18faf3fSek {
1488503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1489503ad85cSMatthew Ahrens 	return (os->os_user_ptr);
1490f18faf3fSek }
1491