xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision b24ab6762772a3f6a89393947930c7fa61306783)
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>
43fa9e4066Sahrens 
44fa9e4066Sahrens spa_t *
45fa9e4066Sahrens dmu_objset_spa(objset_t *os)
46fa9e4066Sahrens {
47503ad85cSMatthew Ahrens 	return (os->os_spa);
48fa9e4066Sahrens }
49fa9e4066Sahrens 
50fa9e4066Sahrens zilog_t *
51fa9e4066Sahrens dmu_objset_zil(objset_t *os)
52fa9e4066Sahrens {
53503ad85cSMatthew Ahrens 	return (os->os_zil);
54fa9e4066Sahrens }
55fa9e4066Sahrens 
56fa9e4066Sahrens dsl_pool_t *
57fa9e4066Sahrens dmu_objset_pool(objset_t *os)
58fa9e4066Sahrens {
59fa9e4066Sahrens 	dsl_dataset_t *ds;
60fa9e4066Sahrens 
61503ad85cSMatthew Ahrens 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
62fa9e4066Sahrens 		return (ds->ds_dir->dd_pool);
63fa9e4066Sahrens 	else
64503ad85cSMatthew Ahrens 		return (spa_get_dsl(os->os_spa));
65fa9e4066Sahrens }
66fa9e4066Sahrens 
67fa9e4066Sahrens dsl_dataset_t *
68fa9e4066Sahrens dmu_objset_ds(objset_t *os)
69fa9e4066Sahrens {
70503ad85cSMatthew Ahrens 	return (os->os_dsl_dataset);
71fa9e4066Sahrens }
72fa9e4066Sahrens 
73fa9e4066Sahrens dmu_objset_type_t
74fa9e4066Sahrens dmu_objset_type(objset_t *os)
75fa9e4066Sahrens {
76503ad85cSMatthew Ahrens 	return (os->os_phys->os_type);
77fa9e4066Sahrens }
78fa9e4066Sahrens 
79fa9e4066Sahrens void
80fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
81fa9e4066Sahrens {
82503ad85cSMatthew Ahrens 	dsl_dataset_name(os->os_dsl_dataset, buf);
83fa9e4066Sahrens }
84fa9e4066Sahrens 
85fa9e4066Sahrens uint64_t
86fa9e4066Sahrens dmu_objset_id(objset_t *os)
87fa9e4066Sahrens {
88503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
89fa9e4066Sahrens 
90fa9e4066Sahrens 	return (ds ? ds->ds_object : 0);
91fa9e4066Sahrens }
92fa9e4066Sahrens 
93e09fa4daSNeil Perrin uint64_t
94e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
95e09fa4daSNeil Perrin {
96e09fa4daSNeil Perrin 	return (os->os_logbias);
97e09fa4daSNeil Perrin }
98e09fa4daSNeil Perrin 
99fa9e4066Sahrens static void
100fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
101fa9e4066Sahrens {
102503ad85cSMatthew Ahrens 	objset_t *os = arg;
103fa9e4066Sahrens 
104fa9e4066Sahrens 	/*
105fa9e4066Sahrens 	 * Inheritance should have been done by now.
106fa9e4066Sahrens 	 */
107fa9e4066Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
108fa9e4066Sahrens 
109503ad85cSMatthew Ahrens 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
110fa9e4066Sahrens }
111fa9e4066Sahrens 
112fa9e4066Sahrens static void
113fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
114fa9e4066Sahrens {
115503ad85cSMatthew Ahrens 	objset_t *os = arg;
116fa9e4066Sahrens 
117fa9e4066Sahrens 	/*
118fa9e4066Sahrens 	 * Inheritance and range checking should have been done by now.
119fa9e4066Sahrens 	 */
120fa9e4066Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
121fa9e4066Sahrens 
122503ad85cSMatthew Ahrens 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
123fa9e4066Sahrens }
124fa9e4066Sahrens 
125d0ad202dSahrens static void
126d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
127d0ad202dSahrens {
128503ad85cSMatthew Ahrens 	objset_t *os = arg;
129d0ad202dSahrens 
130d0ad202dSahrens 	/*
131d0ad202dSahrens 	 * Inheritance and range checking should have been done by now.
132d0ad202dSahrens 	 */
133d0ad202dSahrens 	ASSERT(newval > 0);
134503ad85cSMatthew Ahrens 	ASSERT(newval <= spa_max_replication(os->os_spa));
135d0ad202dSahrens 
136503ad85cSMatthew Ahrens 	os->os_copies = newval;
137d0ad202dSahrens }
138d0ad202dSahrens 
139*b24ab676SJeff Bonwick static void
140*b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
141*b24ab676SJeff Bonwick {
142*b24ab676SJeff Bonwick 	objset_t *os = arg;
143*b24ab676SJeff Bonwick 	spa_t *spa = os->os_spa;
144*b24ab676SJeff Bonwick 	enum zio_checksum checksum;
145*b24ab676SJeff Bonwick 
146*b24ab676SJeff Bonwick 	/*
147*b24ab676SJeff Bonwick 	 * Inheritance should have been done by now.
148*b24ab676SJeff Bonwick 	 */
149*b24ab676SJeff Bonwick 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
150*b24ab676SJeff Bonwick 
151*b24ab676SJeff Bonwick 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
152*b24ab676SJeff Bonwick 
153*b24ab676SJeff Bonwick 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
154*b24ab676SJeff Bonwick 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
155*b24ab676SJeff Bonwick }
156*b24ab676SJeff Bonwick 
1573baa08fcSek static void
1583baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval)
1593baa08fcSek {
160503ad85cSMatthew Ahrens 	objset_t *os = arg;
1613baa08fcSek 
1623baa08fcSek 	/*
1633baa08fcSek 	 * Inheritance and range checking should have been done by now.
1643baa08fcSek 	 */
1653baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1663baa08fcSek 	    newval == ZFS_CACHE_METADATA);
1673baa08fcSek 
168503ad85cSMatthew Ahrens 	os->os_primary_cache = newval;
1693baa08fcSek }
1703baa08fcSek 
1713baa08fcSek static void
1723baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval)
1733baa08fcSek {
174503ad85cSMatthew Ahrens 	objset_t *os = arg;
1753baa08fcSek 
1763baa08fcSek 	/*
1773baa08fcSek 	 * Inheritance and range checking should have been done by now.
1783baa08fcSek 	 */
1793baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1803baa08fcSek 	    newval == ZFS_CACHE_METADATA);
1813baa08fcSek 
182503ad85cSMatthew Ahrens 	os->os_secondary_cache = newval;
1833baa08fcSek }
1843baa08fcSek 
185e09fa4daSNeil Perrin static void
186e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
187e09fa4daSNeil Perrin {
188e09fa4daSNeil Perrin 	objset_t *os = arg;
189e09fa4daSNeil Perrin 
190e09fa4daSNeil Perrin 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
191e09fa4daSNeil Perrin 	    newval == ZFS_LOGBIAS_THROUGHPUT);
192e09fa4daSNeil Perrin 	os->os_logbias = newval;
193e09fa4daSNeil Perrin 	if (os->os_zil)
194e09fa4daSNeil Perrin 		zil_set_logbias(os->os_zil, newval);
195e09fa4daSNeil Perrin }
196e09fa4daSNeil Perrin 
197fa9e4066Sahrens void
198fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
199fa9e4066Sahrens {
200fa9e4066Sahrens 	objset_phys_t *osp = buf;
201fa9e4066Sahrens 
20214843421SMatthew Ahrens 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
203fa9e4066Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
204fa9e4066Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
205fa9e4066Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
20614843421SMatthew Ahrens 	osp->os_flags = BSWAP_64(osp->os_flags);
20714843421SMatthew Ahrens 	if (size == sizeof (objset_phys_t)) {
20814843421SMatthew Ahrens 		dnode_byteswap(&osp->os_userused_dnode);
20914843421SMatthew Ahrens 		dnode_byteswap(&osp->os_groupused_dnode);
21014843421SMatthew Ahrens 	}
211fa9e4066Sahrens }
212fa9e4066Sahrens 
213ea8dc4b6Seschrock int
214ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
215503ad85cSMatthew Ahrens     objset_t **osp)
216fa9e4066Sahrens {
217503ad85cSMatthew Ahrens 	objset_t *os;
218088f3894Sahrens 	int i, err;
219fa9e4066Sahrens 
22091ebeef5Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
22191ebeef5Sahrens 
222503ad85cSMatthew Ahrens 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
223503ad85cSMatthew Ahrens 	os->os_dsl_dataset = ds;
224503ad85cSMatthew Ahrens 	os->os_spa = spa;
225503ad85cSMatthew Ahrens 	os->os_rootbp = bp;
226503ad85cSMatthew Ahrens 	if (!BP_IS_HOLE(os->os_rootbp)) {
22713506d1eSmaybee 		uint32_t aflags = ARC_WAIT;
228ea8dc4b6Seschrock 		zbookmark_t zb;
229*b24ab676SJeff Bonwick 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
230*b24ab676SJeff Bonwick 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
231*b24ab676SJeff Bonwick 
232503ad85cSMatthew Ahrens 		if (DMU_OS_IS_L2CACHEABLE(os))
2333baa08fcSek 			aflags |= ARC_L2CACHE;
234ea8dc4b6Seschrock 
235503ad85cSMatthew Ahrens 		dprintf_bp(os->os_rootbp, "reading %s", "");
236088f3894Sahrens 		/*
237088f3894Sahrens 		 * NB: when bprewrite scrub can change the bp,
238088f3894Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
239088f3894Sahrens 		 * could change, and we'll need a lock.
240088f3894Sahrens 		 */
241503ad85cSMatthew Ahrens 		err = arc_read_nolock(NULL, spa, os->os_rootbp,
242503ad85cSMatthew Ahrens 		    arc_getbuf_func, &os->os_phys_buf,
24313506d1eSmaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
244ea8dc4b6Seschrock 		if (err) {
245503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
246b87f3af3Sperrin 			/* convert checksum errors into IO errors */
247b87f3af3Sperrin 			if (err == ECKSUM)
248b87f3af3Sperrin 				err = EIO;
249ea8dc4b6Seschrock 			return (err);
250ea8dc4b6Seschrock 		}
25114843421SMatthew Ahrens 
25214843421SMatthew Ahrens 		/* Increase the blocksize if we are permitted. */
25314843421SMatthew Ahrens 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
254503ad85cSMatthew Ahrens 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
25514843421SMatthew Ahrens 			arc_buf_t *buf = arc_buf_alloc(spa,
256503ad85cSMatthew Ahrens 			    sizeof (objset_phys_t), &os->os_phys_buf,
25714843421SMatthew Ahrens 			    ARC_BUFC_METADATA);
25814843421SMatthew Ahrens 			bzero(buf->b_data, sizeof (objset_phys_t));
259503ad85cSMatthew Ahrens 			bcopy(os->os_phys_buf->b_data, buf->b_data,
260503ad85cSMatthew Ahrens 			    arc_buf_size(os->os_phys_buf));
261503ad85cSMatthew Ahrens 			(void) arc_buf_remove_ref(os->os_phys_buf,
262503ad85cSMatthew Ahrens 			    &os->os_phys_buf);
263503ad85cSMatthew Ahrens 			os->os_phys_buf = buf;
26414843421SMatthew Ahrens 		}
26514843421SMatthew Ahrens 
266503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
267503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
268fa9e4066Sahrens 	} else {
26914843421SMatthew Ahrens 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
27014843421SMatthew Ahrens 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
271503ad85cSMatthew Ahrens 		os->os_phys_buf = arc_buf_alloc(spa, size,
272503ad85cSMatthew Ahrens 		    &os->os_phys_buf, ARC_BUFC_METADATA);
273503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
274503ad85cSMatthew Ahrens 		bzero(os->os_phys, size);
275fa9e4066Sahrens 	}
276fa9e4066Sahrens 
277fa9e4066Sahrens 	/*
278fa9e4066Sahrens 	 * Note: the changed_cb will be called once before the register
279fa9e4066Sahrens 	 * func returns, thus changing the checksum/compression from the
2803baa08fcSek 	 * default (fletcher2/off).  Snapshots don't need to know about
2813baa08fcSek 	 * checksum/compression/copies.
282fa9e4066Sahrens 	 */
2833baa08fcSek 	if (ds) {
2843baa08fcSek 		err = dsl_prop_register(ds, "primarycache",
285503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
286d0ad202dSahrens 		if (err == 0)
2873baa08fcSek 			err = dsl_prop_register(ds, "secondarycache",
288503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
2893baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
2903baa08fcSek 			if (err == 0)
2913baa08fcSek 				err = dsl_prop_register(ds, "checksum",
292503ad85cSMatthew Ahrens 				    checksum_changed_cb, os);
2933baa08fcSek 			if (err == 0)
2943baa08fcSek 				err = dsl_prop_register(ds, "compression",
295503ad85cSMatthew Ahrens 				    compression_changed_cb, os);
2963baa08fcSek 			if (err == 0)
2973baa08fcSek 				err = dsl_prop_register(ds, "copies",
298503ad85cSMatthew Ahrens 				    copies_changed_cb, os);
299*b24ab676SJeff Bonwick 			if (err == 0)
300*b24ab676SJeff Bonwick 				err = dsl_prop_register(ds, "dedup",
301*b24ab676SJeff Bonwick 				    dedup_changed_cb, os);
302e09fa4daSNeil Perrin 			if (err == 0)
303e09fa4daSNeil Perrin 				err = dsl_prop_register(ds, "logbias",
304e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
3053baa08fcSek 		}
306ea8dc4b6Seschrock 		if (err) {
307503ad85cSMatthew Ahrens 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
308503ad85cSMatthew Ahrens 			    &os->os_phys_buf) == 1);
309503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
310ea8dc4b6Seschrock 			return (err);
311ea8dc4b6Seschrock 		}
31299653d4eSeschrock 	} else if (ds == NULL) {
313fa9e4066Sahrens 		/* It's the meta-objset. */
314503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
315503ad85cSMatthew Ahrens 		os->os_compress = ZIO_COMPRESS_LZJB;
316503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
317*b24ab676SJeff Bonwick 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
318*b24ab676SJeff Bonwick 		os->os_dedup_verify = 0;
319*b24ab676SJeff Bonwick 		os->os_logbias = 0;
320503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
321503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
322fa9e4066Sahrens 	}
323fa9e4066Sahrens 
324503ad85cSMatthew Ahrens 	os->os_zil_header = os->os_phys->os_zil_header;
325503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
326fa9e4066Sahrens 
327fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
328503ad85cSMatthew Ahrens 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
329fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
330503ad85cSMatthew Ahrens 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
331fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
332fa9e4066Sahrens 	}
333503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
334fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
335503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
336fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
337fa9e4066Sahrens 
338503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
339503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
340503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
341503ad85cSMatthew Ahrens 
342503ad85cSMatthew Ahrens 	os->os_meta_dnode = dnode_special_open(os,
343503ad85cSMatthew Ahrens 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
344503ad85cSMatthew Ahrens 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
345503ad85cSMatthew Ahrens 		os->os_userused_dnode = dnode_special_open(os,
346503ad85cSMatthew Ahrens 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
347503ad85cSMatthew Ahrens 		os->os_groupused_dnode = dnode_special_open(os,
348503ad85cSMatthew Ahrens 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
34914843421SMatthew Ahrens 	}
350fa9e4066Sahrens 
35191ebeef5Sahrens 	/*
35291ebeef5Sahrens 	 * We should be the only thread trying to do this because we
35391ebeef5Sahrens 	 * have ds_opening_lock
35491ebeef5Sahrens 	 */
35591ebeef5Sahrens 	if (ds) {
356503ad85cSMatthew Ahrens 		mutex_enter(&ds->ds_lock);
357503ad85cSMatthew Ahrens 		ASSERT(ds->ds_objset == NULL);
358503ad85cSMatthew Ahrens 		ds->ds_objset = os;
359503ad85cSMatthew Ahrens 		mutex_exit(&ds->ds_lock);
360fa9e4066Sahrens 	}
361fa9e4066Sahrens 
362503ad85cSMatthew Ahrens 	*osp = os;
363ea8dc4b6Seschrock 	return (0);
364fa9e4066Sahrens }
365fa9e4066Sahrens 
366503ad85cSMatthew Ahrens int
367503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
3683cb34c60Sahrens {
369503ad85cSMatthew Ahrens 	int err = 0;
3703cb34c60Sahrens 
3713cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
372503ad85cSMatthew Ahrens 	*osp = ds->ds_objset;
373503ad85cSMatthew Ahrens 	if (*osp == NULL) {
3743cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
375503ad85cSMatthew Ahrens 		    ds, &ds->ds_phys->ds_bp, osp);
3763cb34c60Sahrens 	}
3773cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
378503ad85cSMatthew Ahrens 	return (err);
3793cb34c60Sahrens }
3803cb34c60Sahrens 
381503ad85cSMatthew Ahrens /* called from zpl */
3823cb34c60Sahrens int
383503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
3843cb34c60Sahrens {
385503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
3863cb34c60Sahrens 	int err;
3873cb34c60Sahrens 
388503ad85cSMatthew Ahrens 	err = dsl_dataset_hold(name, tag, &ds);
3893cb34c60Sahrens 	if (err)
390503ad85cSMatthew Ahrens 		return (err);
391503ad85cSMatthew Ahrens 
392503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
393503ad85cSMatthew Ahrens 	if (err)
394503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
395503ad85cSMatthew Ahrens 
3963cb34c60Sahrens 	return (err);
3973cb34c60Sahrens }
3983cb34c60Sahrens 
399fa9e4066Sahrens /* called from zpl */
400fa9e4066Sahrens int
401503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
402503ad85cSMatthew Ahrens     boolean_t readonly, void *tag, objset_t **osp)
403fa9e4066Sahrens {
404f18faf3fSek 	dsl_dataset_t *ds;
405f18faf3fSek 	int err;
406fa9e4066Sahrens 
407503ad85cSMatthew Ahrens 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
408503ad85cSMatthew Ahrens 	if (err)
409fa9e4066Sahrens 		return (err);
410fa9e4066Sahrens 
411503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
4123cb34c60Sahrens 	if (err) {
413503ad85cSMatthew Ahrens 		dsl_dataset_disown(ds, tag);
414681d9761SEric Taylor 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
415503ad85cSMatthew Ahrens 		dmu_objset_disown(*osp, tag);
416503ad85cSMatthew Ahrens 		return (EINVAL);
417681d9761SEric Taylor 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
418681d9761SEric Taylor 		dmu_objset_disown(*osp, tag);
419681d9761SEric Taylor 		return (EROFS);
420fa9e4066Sahrens 	}
4213cb34c60Sahrens 	return (err);
422fa9e4066Sahrens }
423fa9e4066Sahrens 
424fa9e4066Sahrens void
425503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
426fa9e4066Sahrens {
427503ad85cSMatthew Ahrens 	dsl_dataset_rele(os->os_dsl_dataset, tag);
428503ad85cSMatthew Ahrens }
429503ad85cSMatthew Ahrens 
430503ad85cSMatthew Ahrens void
431503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
432503ad85cSMatthew Ahrens {
433503ad85cSMatthew Ahrens 	dsl_dataset_disown(os->os_dsl_dataset, tag);
434fa9e4066Sahrens }
435fa9e4066Sahrens 
436436b2950Sperrin int
4371934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
438ea8dc4b6Seschrock {
439ea8dc4b6Seschrock 	dnode_t *dn;
440c543ec06Sahrens 
441503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
442c543ec06Sahrens 
443c543ec06Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
444503ad85cSMatthew Ahrens 	list_remove(&os->os_dnodes, os->os_meta_dnode);
445503ad85cSMatthew Ahrens 	list_insert_tail(&os->os_dnodes, os->os_meta_dnode);
446ea8dc4b6Seschrock 
447ea8dc4b6Seschrock 	/*
448c543ec06Sahrens 	 * Find the first dnode with holds.  We have to do this dance
449c543ec06Sahrens 	 * because dnode_add_ref() only works if you already have a
450c543ec06Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
451c543ec06Sahrens 	 * skip.
452ea8dc4b6Seschrock 	 */
453503ad85cSMatthew Ahrens 	for (dn = list_head(&os->os_dnodes);
4541934e92fSmaybee 	    dn && !dnode_add_ref(dn, FTAG);
455503ad85cSMatthew Ahrens 	    dn = list_next(&os->os_dnodes, dn))
456c543ec06Sahrens 		continue;
457c543ec06Sahrens 
458c543ec06Sahrens 	while (dn) {
459c543ec06Sahrens 		dnode_t *next_dn = dn;
460c543ec06Sahrens 
461c543ec06Sahrens 		do {
462503ad85cSMatthew Ahrens 			next_dn = list_next(&os->os_dnodes, next_dn);
4631934e92fSmaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
464c543ec06Sahrens 
465503ad85cSMatthew Ahrens 		mutex_exit(&os->os_lock);
4661934e92fSmaybee 		dnode_evict_dbufs(dn);
467c543ec06Sahrens 		dnode_rele(dn, FTAG);
468503ad85cSMatthew Ahrens 		mutex_enter(&os->os_lock);
469c543ec06Sahrens 		dn = next_dn;
470ea8dc4b6Seschrock 	}
471503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
472503ad85cSMatthew Ahrens 	return (list_head(&os->os_dnodes) != os->os_meta_dnode);
473ea8dc4b6Seschrock }
474ea8dc4b6Seschrock 
475fa9e4066Sahrens void
476503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
477fa9e4066Sahrens {
478503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
479fa9e4066Sahrens 
480*b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
481*b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, t));
482fa9e4066Sahrens 
4833baa08fcSek 	if (ds) {
4843baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
4853baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
486503ad85cSMatthew Ahrens 			    checksum_changed_cb, os));
4873baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
488503ad85cSMatthew Ahrens 			    compression_changed_cb, os));
4893baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
490503ad85cSMatthew Ahrens 			    copies_changed_cb, os));
491*b24ab676SJeff Bonwick 			VERIFY(0 == dsl_prop_unregister(ds, "dedup",
492*b24ab676SJeff Bonwick 			    dedup_changed_cb, os));
493e09fa4daSNeil Perrin 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
494e09fa4daSNeil Perrin 			    logbias_changed_cb, os));
4953baa08fcSek 		}
4963baa08fcSek 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
497503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os));
4983baa08fcSek 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
499503ad85cSMatthew Ahrens 		    secondary_cache_changed_cb, os));
500fa9e4066Sahrens 	}
501fa9e4066Sahrens 
502ea8dc4b6Seschrock 	/*
503ea8dc4b6Seschrock 	 * We should need only a single pass over the dnode list, since
504ea8dc4b6Seschrock 	 * nothing can be added to the list at this point.
505ea8dc4b6Seschrock 	 */
506503ad85cSMatthew Ahrens 	(void) dmu_objset_evict_dbufs(os);
507ea8dc4b6Seschrock 
508503ad85cSMatthew Ahrens 	dnode_special_close(os->os_meta_dnode);
509503ad85cSMatthew Ahrens 	if (os->os_userused_dnode) {
510503ad85cSMatthew Ahrens 		dnode_special_close(os->os_userused_dnode);
511503ad85cSMatthew Ahrens 		dnode_special_close(os->os_groupused_dnode);
51214843421SMatthew Ahrens 	}
513503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
514fa9e4066Sahrens 
515503ad85cSMatthew Ahrens 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
51614843421SMatthew Ahrens 
517503ad85cSMatthew Ahrens 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
518503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
519503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
520503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
521503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
522fa9e4066Sahrens }
523fa9e4066Sahrens 
52471eb0538SChris Kirby timestruc_t
52571eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
52671eb0538SChris Kirby {
52771eb0538SChris Kirby 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
52871eb0538SChris Kirby }
52971eb0538SChris Kirby 
530fa9e4066Sahrens /* called from dsl for meta-objset */
531503ad85cSMatthew Ahrens objset_t *
532c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
533c717a561Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
534fa9e4066Sahrens {
535503ad85cSMatthew Ahrens 	objset_t *os;
536fa9e4066Sahrens 	dnode_t *mdn;
537fa9e4066Sahrens 
538fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
53991ebeef5Sahrens 	if (ds)
54091ebeef5Sahrens 		mutex_enter(&ds->ds_opening_lock);
541503ad85cSMatthew Ahrens 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
54291ebeef5Sahrens 	if (ds)
54391ebeef5Sahrens 		mutex_exit(&ds->ds_opening_lock);
544503ad85cSMatthew Ahrens 	mdn = os->os_meta_dnode;
545fa9e4066Sahrens 
546fa9e4066Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
547fa9e4066Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
548fa9e4066Sahrens 
549fa9e4066Sahrens 	/*
550fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
551fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
552fa9e4066Sahrens 	 * we are also accessing it in open context.
553fa9e4066Sahrens 	 *
554fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
555fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
556fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
557fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
558fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
559fa9e4066Sahrens 	 */
560ea8dc4b6Seschrock 	if (ds != NULL) {
561ea8dc4b6Seschrock 		int levels = 1;
562ea8dc4b6Seschrock 
563ea8dc4b6Seschrock 		/*
564ea8dc4b6Seschrock 		 * Determine the number of levels necessary for the meta-dnode
565ea8dc4b6Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
566ea8dc4b6Seschrock 		 */
567ea8dc4b6Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
568ea8dc4b6Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
569ea8dc4b6Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
570ea8dc4b6Seschrock 			levels++;
571ea8dc4b6Seschrock 
572fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
573ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
574ea8dc4b6Seschrock 	}
575fa9e4066Sahrens 
576fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
577fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
578fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
579503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
580503ad85cSMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
581503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
582503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
58314843421SMatthew Ahrens 	}
584fa9e4066Sahrens 
585fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
586fa9e4066Sahrens 
587503ad85cSMatthew Ahrens 	return (os);
588fa9e4066Sahrens }
589fa9e4066Sahrens 
590fa9e4066Sahrens struct oscarg {
591ecd6cf80Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
592fa9e4066Sahrens 	void *userarg;
593ae46e4c7SMatthew Ahrens 	dsl_dataset_t *clone_origin;
594fa9e4066Sahrens 	const char *lastname;
595fa9e4066Sahrens 	dmu_objset_type_t type;
596ab04eb8eStimh 	uint64_t flags;
597fa9e4066Sahrens };
598fa9e4066Sahrens 
599ecd6cf80Smarks /*ARGSUSED*/
600fa9e4066Sahrens static int
6011d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
602fa9e4066Sahrens {
6031d452cf5Sahrens 	dsl_dir_t *dd = arg1;
6041d452cf5Sahrens 	struct oscarg *oa = arg2;
6051d452cf5Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
606fa9e4066Sahrens 	int err;
6071d452cf5Sahrens 	uint64_t ddobj;
6081d452cf5Sahrens 
6091d452cf5Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
6101d452cf5Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
6111d452cf5Sahrens 	if (err != ENOENT)
6121d452cf5Sahrens 		return (err ? err : EEXIST);
6131d452cf5Sahrens 
614ae46e4c7SMatthew Ahrens 	if (oa->clone_origin != NULL) {
615ae46e4c7SMatthew Ahrens 		/* You can't clone across pools. */
616ae46e4c7SMatthew Ahrens 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
6171d452cf5Sahrens 			return (EXDEV);
6181d452cf5Sahrens 
619ae46e4c7SMatthew Ahrens 		/* You can only clone snapshots, not the head datasets. */
620ae46e4c7SMatthew Ahrens 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
6211d452cf5Sahrens 			return (EINVAL);
6221d452cf5Sahrens 	}
623ecd6cf80Smarks 
6241d452cf5Sahrens 	return (0);
6251d452cf5Sahrens }
6261d452cf5Sahrens 
6271d452cf5Sahrens static void
628ecd6cf80Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
6291d452cf5Sahrens {
6301d452cf5Sahrens 	dsl_dir_t *dd = arg1;
6311d452cf5Sahrens 	struct oscarg *oa = arg2;
6321d452cf5Sahrens 	uint64_t dsobj;
633fa9e4066Sahrens 
634fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
635fa9e4066Sahrens 
6361d452cf5Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
637ae46e4c7SMatthew Ahrens 	    oa->clone_origin, oa->flags, cr, tx);
638fa9e4066Sahrens 
639ae46e4c7SMatthew Ahrens 	if (oa->clone_origin == NULL) {
640ae46e4c7SMatthew Ahrens 		dsl_dataset_t *ds;
641ae46e4c7SMatthew Ahrens 		blkptr_t *bp;
642503ad85cSMatthew Ahrens 		objset_t *os;
643fa9e4066Sahrens 
644ae46e4c7SMatthew Ahrens 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
645ae46e4c7SMatthew Ahrens 		    FTAG, &ds));
646ae46e4c7SMatthew Ahrens 		bp = dsl_dataset_get_blkptr(ds);
647ae46e4c7SMatthew Ahrens 		ASSERT(BP_IS_HOLE(bp));
648ae46e4c7SMatthew Ahrens 
649503ad85cSMatthew Ahrens 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
650c717a561Smaybee 		    ds, bp, oa->type, tx);
651fa9e4066Sahrens 
652fa9e4066Sahrens 		if (oa->userfunc)
653503ad85cSMatthew Ahrens 			oa->userfunc(os, oa->userarg, cr, tx);
654ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
655fa9e4066Sahrens 	}
656ecd6cf80Smarks 
657ecd6cf80Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
658ecd6cf80Smarks 	    tx, cr, "dataset = %llu", dsobj);
659fa9e4066Sahrens }
660fa9e4066Sahrens 
661fa9e4066Sahrens int
662ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
663ecd6cf80Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
664fa9e4066Sahrens {
6651d452cf5Sahrens 	dsl_dir_t *pdd;
666fa9e4066Sahrens 	const char *tail;
667fa9e4066Sahrens 	int err = 0;
6681d452cf5Sahrens 	struct oscarg oa = { 0 };
669fa9e4066Sahrens 
6701d452cf5Sahrens 	ASSERT(strchr(name, '@') == NULL);
6711d452cf5Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
672ea8dc4b6Seschrock 	if (err)
673ea8dc4b6Seschrock 		return (err);
674fa9e4066Sahrens 	if (tail == NULL) {
6751d452cf5Sahrens 		dsl_dir_close(pdd, FTAG);
676fa9e4066Sahrens 		return (EEXIST);
677fa9e4066Sahrens 	}
678fa9e4066Sahrens 
6791d452cf5Sahrens 	oa.userfunc = func;
6801d452cf5Sahrens 	oa.userarg = arg;
6811d452cf5Sahrens 	oa.lastname = tail;
6821d452cf5Sahrens 	oa.type = type;
683ab04eb8eStimh 	oa.flags = flags;
684ecd6cf80Smarks 
685ae46e4c7SMatthew Ahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
686ae46e4c7SMatthew Ahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
687ae46e4c7SMatthew Ahrens 	dsl_dir_close(pdd, FTAG);
688ae46e4c7SMatthew Ahrens 	return (err);
689ae46e4c7SMatthew Ahrens }
690ae46e4c7SMatthew Ahrens 
691ae46e4c7SMatthew Ahrens int
692ae46e4c7SMatthew Ahrens dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
693ae46e4c7SMatthew Ahrens {
694ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
695ae46e4c7SMatthew Ahrens 	const char *tail;
696ae46e4c7SMatthew Ahrens 	int err = 0;
697ae46e4c7SMatthew Ahrens 	struct oscarg oa = { 0 };
698ae46e4c7SMatthew Ahrens 
699ae46e4c7SMatthew Ahrens 	ASSERT(strchr(name, '@') == NULL);
700ae46e4c7SMatthew Ahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
701ae46e4c7SMatthew Ahrens 	if (err)
702ae46e4c7SMatthew Ahrens 		return (err);
703ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
704ae46e4c7SMatthew Ahrens 		dsl_dir_close(pdd, FTAG);
705ae46e4c7SMatthew Ahrens 		return (EEXIST);
706fa9e4066Sahrens 	}
707ae46e4c7SMatthew Ahrens 
708ae46e4c7SMatthew Ahrens 	oa.lastname = tail;
709ae46e4c7SMatthew Ahrens 	oa.clone_origin = clone_origin;
710ae46e4c7SMatthew Ahrens 	oa.flags = flags;
711ae46e4c7SMatthew Ahrens 
7121d452cf5Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
7131d452cf5Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
7141d452cf5Sahrens 	dsl_dir_close(pdd, FTAG);
715fa9e4066Sahrens 	return (err);
716fa9e4066Sahrens }
717fa9e4066Sahrens 
718fa9e4066Sahrens int
719842727c2SChris Kirby dmu_objset_destroy(const char *name, boolean_t defer)
720fa9e4066Sahrens {
721503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
722fa9e4066Sahrens 	int error;
723fa9e4066Sahrens 
724fa9e4066Sahrens 	/*
725ae46e4c7SMatthew Ahrens 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
726ae46e4c7SMatthew Ahrens 	 * intent log, but if there is an active log, it has blocks that
727ae46e4c7SMatthew Ahrens 	 * are allocated, but may not yet be reflected in the on-disk
728ae46e4c7SMatthew Ahrens 	 * structure.  Only the ZIL knows how to free them, so we have
729ae46e4c7SMatthew Ahrens 	 * to call into it here.
730fa9e4066Sahrens 	 */
731503ad85cSMatthew Ahrens 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
732fa9e4066Sahrens 	if (error == 0) {
733503ad85cSMatthew Ahrens 		objset_t *os;
734503ad85cSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) == 0)
735503ad85cSMatthew Ahrens 			zil_destroy(dmu_objset_zil(os), B_FALSE);
736503ad85cSMatthew Ahrens 		error = dsl_dataset_destroy(ds, FTAG, defer);
737ae46e4c7SMatthew Ahrens 		/* dsl_dataset_destroy() closes the ds. */
738fa9e4066Sahrens 	}
739fa9e4066Sahrens 
7403cb34c60Sahrens 	return (error);
741fa9e4066Sahrens }
742fa9e4066Sahrens 
7431d452cf5Sahrens struct snaparg {
7441d452cf5Sahrens 	dsl_sync_task_group_t *dstg;
7451d452cf5Sahrens 	char *snapname;
7461d452cf5Sahrens 	char failed[MAXPATHLEN];
747ecd6cf80Smarks 	boolean_t checkperms;
748ea2f5b9eSMatthew Ahrens 	nvlist_t *props;
7493cb34c60Sahrens };
7503cb34c60Sahrens 
751ea2f5b9eSMatthew Ahrens static int
752ea2f5b9eSMatthew Ahrens snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
753ea2f5b9eSMatthew Ahrens {
754ea2f5b9eSMatthew Ahrens 	objset_t *os = arg1;
755ea2f5b9eSMatthew Ahrens 	struct snaparg *sn = arg2;
756ea2f5b9eSMatthew Ahrens 
757ea2f5b9eSMatthew Ahrens 	/* The props have already been checked by zfs_check_userprops(). */
758ea2f5b9eSMatthew Ahrens 
759503ad85cSMatthew Ahrens 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
760ea2f5b9eSMatthew Ahrens 	    sn->snapname, tx));
761ea2f5b9eSMatthew Ahrens }
762ea2f5b9eSMatthew Ahrens 
763ea2f5b9eSMatthew Ahrens static void
764ea2f5b9eSMatthew Ahrens snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
765ea2f5b9eSMatthew Ahrens {
766ea2f5b9eSMatthew Ahrens 	objset_t *os = arg1;
767503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
768ea2f5b9eSMatthew Ahrens 	struct snaparg *sn = arg2;
769ea2f5b9eSMatthew Ahrens 
770ea2f5b9eSMatthew Ahrens 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
771ea2f5b9eSMatthew Ahrens 
772ea2f5b9eSMatthew Ahrens 	if (sn->props)
773ea2f5b9eSMatthew Ahrens 		dsl_props_set_sync(ds->ds_prev, sn->props, cr, tx);
774ea2f5b9eSMatthew Ahrens }
7751d452cf5Sahrens 
7761d452cf5Sahrens static int
7771d452cf5Sahrens dmu_objset_snapshot_one(char *name, void *arg)
7781d452cf5Sahrens {
7791d452cf5Sahrens 	struct snaparg *sn = arg;
7801d452cf5Sahrens 	objset_t *os;
7811d452cf5Sahrens 	int err;
7821d452cf5Sahrens 
7831d452cf5Sahrens 	(void) strcpy(sn->failed, name);
7841d452cf5Sahrens 
785ecd6cf80Smarks 	/*
786ecd6cf80Smarks 	 * Check permissions only when requested.  This only applies when
787ecd6cf80Smarks 	 * doing a recursive snapshot.  The permission checks for the starting
788ecd6cf80Smarks 	 * dataset have already been performed in zfs_secpolicy_snapshot()
789ecd6cf80Smarks 	 */
790ecd6cf80Smarks 	if (sn->checkperms == B_TRUE &&
791ecd6cf80Smarks 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
792ecd6cf80Smarks 		return (err);
793ecd6cf80Smarks 
794503ad85cSMatthew Ahrens 	err = dmu_objset_hold(name, sn, &os);
7951d452cf5Sahrens 	if (err != 0)
7961d452cf5Sahrens 		return (err);
7971d452cf5Sahrens 
798745cd3c5Smaybee 	/* If the objset is in an inconsistent state, return busy */
799503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
800503ad85cSMatthew Ahrens 		dmu_objset_rele(os, sn);
801f2e10be3Srm 		return (EBUSY);
802f2e10be3Srm 	}
803f2e10be3Srm 
8041d452cf5Sahrens 	/*
8051d452cf5Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
8061d452cf5Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
8071d452cf5Sahrens 	 * a side effect.
8081d452cf5Sahrens 	 */
8091d452cf5Sahrens 	err = zil_suspend(dmu_objset_zil(os));
8101d452cf5Sahrens 	if (err == 0) {
811ea2f5b9eSMatthew Ahrens 		dsl_sync_task_create(sn->dstg, snapshot_check,
812ea2f5b9eSMatthew Ahrens 		    snapshot_sync, os, sn, 3);
813f2e10be3Srm 	} else {
814503ad85cSMatthew Ahrens 		dmu_objset_rele(os, sn);
8151d452cf5Sahrens 	}
816f2e10be3Srm 
8171d452cf5Sahrens 	return (err);
8181d452cf5Sahrens }
8191d452cf5Sahrens 
8201d452cf5Sahrens int
821ea2f5b9eSMatthew Ahrens dmu_objset_snapshot(char *fsname, char *snapname,
822ea2f5b9eSMatthew Ahrens     nvlist_t *props, boolean_t recursive)
8231d452cf5Sahrens {
8241d452cf5Sahrens 	dsl_sync_task_t *dst;
825ea2f5b9eSMatthew Ahrens 	struct snaparg sn;
8261d452cf5Sahrens 	spa_t *spa;
8271d452cf5Sahrens 	int err;
8281d452cf5Sahrens 
8291d452cf5Sahrens 	(void) strcpy(sn.failed, fsname);
8301d452cf5Sahrens 
83140feaa91Sahrens 	err = spa_open(fsname, &spa, FTAG);
8321d452cf5Sahrens 	if (err)
8331d452cf5Sahrens 		return (err);
8341d452cf5Sahrens 
8351d452cf5Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8361d452cf5Sahrens 	sn.snapname = snapname;
837ea2f5b9eSMatthew Ahrens 	sn.props = props;
8381d452cf5Sahrens 
8390b69c2f0Sahrens 	if (recursive) {
840ecd6cf80Smarks 		sn.checkperms = B_TRUE;
8410b69c2f0Sahrens 		err = dmu_objset_find(fsname,
8420b69c2f0Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
8430b69c2f0Sahrens 	} else {
844ecd6cf80Smarks 		sn.checkperms = B_FALSE;
8451d452cf5Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
8460b69c2f0Sahrens 	}
8471d452cf5Sahrens 
848ea2f5b9eSMatthew Ahrens 	if (err == 0)
849ea2f5b9eSMatthew Ahrens 		err = dsl_sync_task_group_wait(sn.dstg);
8501d452cf5Sahrens 
8511d452cf5Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
8521d452cf5Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
853ea2f5b9eSMatthew Ahrens 		objset_t *os = dst->dst_arg1;
854503ad85cSMatthew Ahrens 		dsl_dataset_t *ds = os->os_dsl_dataset;
8551d452cf5Sahrens 		if (dst->dst_err)
8563cb34c60Sahrens 			dsl_dataset_name(ds, sn.failed);
857ea2f5b9eSMatthew Ahrens 		zil_resume(dmu_objset_zil(os));
858503ad85cSMatthew Ahrens 		dmu_objset_rele(os, &sn);
8593cb34c60Sahrens 	}
8603cb34c60Sahrens 
8611d452cf5Sahrens 	if (err)
8621d452cf5Sahrens 		(void) strcpy(fsname, sn.failed);
8631d452cf5Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
8641d452cf5Sahrens 	spa_close(spa, FTAG);
8651d452cf5Sahrens 	return (err);
8661d452cf5Sahrens }
8671d452cf5Sahrens 
868fa9e4066Sahrens static void
86914843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
870fa9e4066Sahrens {
871c717a561Smaybee 	dnode_t *dn;
872faafa6e3Sahrens 
873c717a561Smaybee 	while (dn = list_head(list)) {
874c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
875c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
876c717a561Smaybee 		/*
87714843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
87814843421SMatthew Ahrens 		 * meta-dnode needs to set it ouside dnode_sync().
879c717a561Smaybee 		 */
880c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
881c717a561Smaybee 		ASSERT(dn->dn_zio);
882faafa6e3Sahrens 
883c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
884c717a561Smaybee 		list_remove(list, dn);
88514843421SMatthew Ahrens 
88614843421SMatthew Ahrens 		if (newlist) {
88714843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
88814843421SMatthew Ahrens 			list_insert_tail(newlist, dn);
88914843421SMatthew Ahrens 		}
89014843421SMatthew Ahrens 
891c717a561Smaybee 		dnode_sync(dn, tx);
892fa9e4066Sahrens 	}
893fa9e4066Sahrens }
894fa9e4066Sahrens 
895fa9e4066Sahrens /* ARGSUSED */
896fa9e4066Sahrens static void
897*b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
898fa9e4066Sahrens {
899e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
900503ad85cSMatthew Ahrens 	objset_t *os = arg;
901c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
902fa9e4066Sahrens 
903e14bb325SJeff Bonwick 	ASSERT(bp == os->os_rootbp);
904e14bb325SJeff Bonwick 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
905e14bb325SJeff Bonwick 	ASSERT(BP_GET_LEVEL(bp) == 0);
9060a4e9518Sgw 
907fa9e4066Sahrens 	/*
90814843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
90914843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
91014843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
91114843421SMatthew Ahrens 	 * dnode and user/group accounting objects).
912fa9e4066Sahrens 	 */
91314843421SMatthew Ahrens 	bp->blk_fill = 0;
914e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
915c717a561Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
916*b24ab676SJeff Bonwick }
917*b24ab676SJeff Bonwick 
918*b24ab676SJeff Bonwick /* ARGSUSED */
919*b24ab676SJeff Bonwick static void
920*b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
921*b24ab676SJeff Bonwick {
922*b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
923*b24ab676SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
924*b24ab676SJeff Bonwick 	objset_t *os = arg;
9250a4e9518Sgw 
926e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
927*b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, bp_orig));
928e14bb325SJeff Bonwick 	} else {
929*b24ab676SJeff Bonwick 		dsl_dataset_t *ds = os->os_dsl_dataset;
930*b24ab676SJeff Bonwick 		dmu_tx_t *tx = os->os_synctx;
931*b24ab676SJeff Bonwick 
932*b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
933*b24ab676SJeff Bonwick 		dsl_dataset_block_born(ds, bp, tx);
9340a4e9518Sgw 	}
935c717a561Smaybee }
936c717a561Smaybee 
937fa9e4066Sahrens /* called from dsl */
938fa9e4066Sahrens void
939503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
940fa9e4066Sahrens {
941fa9e4066Sahrens 	int txgoff;
942ea8dc4b6Seschrock 	zbookmark_t zb;
943*b24ab676SJeff Bonwick 	zio_prop_t zp;
944c717a561Smaybee 	zio_t *zio;
945c717a561Smaybee 	list_t *list;
94614843421SMatthew Ahrens 	list_t *newlist = NULL;
947c717a561Smaybee 	dbuf_dirty_record_t *dr;
948c717a561Smaybee 
949c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
950fa9e4066Sahrens 
951fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
952fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
953fa9e4066Sahrens 	os->os_synctx = tx;
954fa9e4066Sahrens 
95587bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
95687bd5c1eSahrens 		/*
95787bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
95887bd5c1eSahrens 		 * spa_max_replication() could change, so reset
95987bd5c1eSahrens 		 * os_copies here.
96087bd5c1eSahrens 		 */
96187bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
96287bd5c1eSahrens 	}
96387bd5c1eSahrens 
964fa9e4066Sahrens 	/*
965c717a561Smaybee 	 * Create the root block IO
966fa9e4066Sahrens 	 */
967088f3894Sahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
96814843421SMatthew Ahrens 
969*b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
970*b24ab676SJeff Bonwick 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
971*b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
972*b24ab676SJeff Bonwick 
973*b24ab676SJeff Bonwick 	dmu_write_policy(os, NULL, 0, 0, &zp);
974*b24ab676SJeff Bonwick 
975*b24ab676SJeff Bonwick 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
976*b24ab676SJeff Bonwick 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
977*b24ab676SJeff Bonwick 	    dmu_objset_write_ready, dmu_objset_write_done, os,
978e14bb325SJeff Bonwick 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
979c717a561Smaybee 
980c717a561Smaybee 	/*
98114843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
982c717a561Smaybee 	 */
983c717a561Smaybee 	os->os_meta_dnode->dn_zio = zio;
984c717a561Smaybee 	dnode_sync(os->os_meta_dnode, tx);
985c717a561Smaybee 
98614843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
98714843421SMatthew Ahrens 
98814843421SMatthew Ahrens 	if (os->os_userused_dnode &&
98914843421SMatthew Ahrens 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
99014843421SMatthew Ahrens 		os->os_userused_dnode->dn_zio = zio;
99114843421SMatthew Ahrens 		dnode_sync(os->os_userused_dnode, tx);
99214843421SMatthew Ahrens 		os->os_groupused_dnode->dn_zio = zio;
99314843421SMatthew Ahrens 		dnode_sync(os->os_groupused_dnode, tx);
99414843421SMatthew Ahrens 	}
99514843421SMatthew Ahrens 
996c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
997fa9e4066Sahrens 
99814843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
99914843421SMatthew Ahrens 		newlist = &os->os_synced_dnodes;
100014843421SMatthew Ahrens 		/*
100114843421SMatthew Ahrens 		 * We must create the list here because it uses the
100214843421SMatthew Ahrens 		 * dn_dirty_link[] of this txg.
100314843421SMatthew Ahrens 		 */
100414843421SMatthew Ahrens 		list_create(newlist, sizeof (dnode_t),
100514843421SMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
100614843421SMatthew Ahrens 	}
100714843421SMatthew Ahrens 
100814843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
100914843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1010fa9e4066Sahrens 
1011c717a561Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
1012c717a561Smaybee 	while (dr = list_head(list)) {
1013c717a561Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
1014c717a561Smaybee 		list_remove(list, dr);
1015c717a561Smaybee 		if (dr->dr_zio)
1016c717a561Smaybee 			zio_nowait(dr->dr_zio);
1017c717a561Smaybee 	}
1018c717a561Smaybee 	/*
1019c717a561Smaybee 	 * Free intent log blocks up to this tx.
1020c717a561Smaybee 	 */
1021c717a561Smaybee 	zil_sync(os->os_zil, tx);
1022088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
1023c717a561Smaybee 	zio_nowait(zio);
1024fa9e4066Sahrens }
1025fa9e4066Sahrens 
1026*b24ab676SJeff Bonwick boolean_t
1027*b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1028*b24ab676SJeff Bonwick {
1029*b24ab676SJeff Bonwick 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1030*b24ab676SJeff Bonwick 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1031*b24ab676SJeff Bonwick }
1032*b24ab676SJeff Bonwick 
103314843421SMatthew Ahrens static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
103414843421SMatthew Ahrens 
103514843421SMatthew Ahrens void
103614843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
103714843421SMatthew Ahrens {
103814843421SMatthew Ahrens 	used_cbs[ost] = cb;
103914843421SMatthew Ahrens }
104014843421SMatthew Ahrens 
104114843421SMatthew Ahrens boolean_t
1042503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
104314843421SMatthew Ahrens {
104414843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
104514843421SMatthew Ahrens 	    used_cbs[os->os_phys->os_type] &&
104614843421SMatthew Ahrens 	    os->os_userused_dnode);
104714843421SMatthew Ahrens }
104814843421SMatthew Ahrens 
10499966ca11SMatthew Ahrens static void
10509966ca11SMatthew Ahrens do_userquota_callback(objset_t *os, dnode_phys_t *dnp,
10519966ca11SMatthew Ahrens     boolean_t subtract, dmu_tx_t *tx)
10529966ca11SMatthew Ahrens {
10539966ca11SMatthew Ahrens 	static const char zerobuf[DN_MAX_BONUSLEN] = {0};
10549966ca11SMatthew Ahrens 	uint64_t user, group;
10559966ca11SMatthew Ahrens 
10569966ca11SMatthew Ahrens 	ASSERT(dnp->dn_type != 0 ||
10579966ca11SMatthew Ahrens 	    (bcmp(DN_BONUS(dnp), zerobuf, DN_MAX_BONUSLEN) == 0 &&
10589966ca11SMatthew Ahrens 	    DN_USED_BYTES(dnp) == 0));
10599966ca11SMatthew Ahrens 
10609966ca11SMatthew Ahrens 	if ((dnp->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) &&
10619966ca11SMatthew Ahrens 	    0 == used_cbs[os->os_phys->os_type](dnp->dn_bonustype,
10629966ca11SMatthew Ahrens 	    DN_BONUS(dnp), &user, &group)) {
10639966ca11SMatthew Ahrens 		int64_t delta = DNODE_SIZE + DN_USED_BYTES(dnp);
10649966ca11SMatthew Ahrens 		if (subtract)
10659966ca11SMatthew Ahrens 			delta = -delta;
1066c33e334fSMatthew Ahrens 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
10679966ca11SMatthew Ahrens 		    user, delta, tx));
1068c33e334fSMatthew Ahrens 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
10699966ca11SMatthew Ahrens 		    group, delta, tx));
10709966ca11SMatthew Ahrens 	}
10719966ca11SMatthew Ahrens }
10729966ca11SMatthew Ahrens 
107314843421SMatthew Ahrens void
1074503ad85cSMatthew Ahrens dmu_objset_do_userquota_callbacks(objset_t *os, dmu_tx_t *tx)
107514843421SMatthew Ahrens {
107614843421SMatthew Ahrens 	dnode_t *dn;
107714843421SMatthew Ahrens 	list_t *list = &os->os_synced_dnodes;
107814843421SMatthew Ahrens 
107914843421SMatthew Ahrens 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
108014843421SMatthew Ahrens 
108114843421SMatthew Ahrens 	while (dn = list_head(list)) {
108214843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
108314843421SMatthew Ahrens 		ASSERT(dn->dn_oldphys);
108414843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
108514843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
108614843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
108714843421SMatthew Ahrens 
108814843421SMatthew Ahrens 		/* Allocate the user/groupused objects if necessary. */
108914843421SMatthew Ahrens 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
1090503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
109114843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT,
109214843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1093503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
109414843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT,
109514843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
109614843421SMatthew Ahrens 		}
109714843421SMatthew Ahrens 
109814843421SMatthew Ahrens 		/*
10999966ca11SMatthew Ahrens 		 * We intentionally modify the zap object even if the
11009966ca11SMatthew Ahrens 		 * net delta (due to phys-oldphys) is zero.  Otherwise
11019966ca11SMatthew Ahrens 		 * the block of the zap obj could be shared between
11029966ca11SMatthew Ahrens 		 * datasets but need to be different between them after
11039966ca11SMatthew Ahrens 		 * a bprewrite.
110414843421SMatthew Ahrens 		 */
11059966ca11SMatthew Ahrens 		do_userquota_callback(os, dn->dn_oldphys, B_TRUE, tx);
11069966ca11SMatthew Ahrens 		do_userquota_callback(os, dn->dn_phys, B_FALSE, tx);
110714843421SMatthew Ahrens 
110814843421SMatthew Ahrens 		/*
110914843421SMatthew Ahrens 		 * The mutex is needed here for interlock with dnode_allocate.
111014843421SMatthew Ahrens 		 */
111114843421SMatthew Ahrens 		mutex_enter(&dn->dn_mtx);
111214843421SMatthew Ahrens 		zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t));
111314843421SMatthew Ahrens 		dn->dn_oldphys = NULL;
111414843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
111514843421SMatthew Ahrens 
111614843421SMatthew Ahrens 		list_remove(list, dn);
111714843421SMatthew Ahrens 		dnode_rele(dn, list);
111814843421SMatthew Ahrens 	}
111914843421SMatthew Ahrens }
112014843421SMatthew Ahrens 
112114843421SMatthew Ahrens boolean_t
112214843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
112314843421SMatthew Ahrens {
1124503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
112514843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
112614843421SMatthew Ahrens }
112714843421SMatthew Ahrens 
112814843421SMatthew Ahrens int
112914843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
113014843421SMatthew Ahrens {
113114843421SMatthew Ahrens 	uint64_t obj;
113214843421SMatthew Ahrens 	int err = 0;
113314843421SMatthew Ahrens 
113414843421SMatthew Ahrens 	if (dmu_objset_userspace_present(os))
113514843421SMatthew Ahrens 		return (0);
1136503ad85cSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
113714843421SMatthew Ahrens 		return (ENOTSUP);
113814843421SMatthew Ahrens 	if (dmu_objset_is_snapshot(os))
113914843421SMatthew Ahrens 		return (EINVAL);
114014843421SMatthew Ahrens 
114114843421SMatthew Ahrens 	/*
114214843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
114314843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
114414843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
114514843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
114614843421SMatthew Ahrens 	 * independently.
114714843421SMatthew Ahrens 	 */
114814843421SMatthew Ahrens 
114914843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
115068857716SLin Ling 		dmu_tx_t *tx;
115114843421SMatthew Ahrens 		dmu_buf_t *db;
115214843421SMatthew Ahrens 		int objerr;
115314843421SMatthew Ahrens 
115414843421SMatthew Ahrens 		if (issig(JUSTLOOKING) && issig(FORREAL))
115514843421SMatthew Ahrens 			return (EINTR);
115614843421SMatthew Ahrens 
115714843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
115814843421SMatthew Ahrens 		if (objerr)
115914843421SMatthew Ahrens 			continue;
116068857716SLin Ling 		tx = dmu_tx_create(os);
116114843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
116214843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
116314843421SMatthew Ahrens 		if (objerr) {
116414843421SMatthew Ahrens 			dmu_tx_abort(tx);
116514843421SMatthew Ahrens 			continue;
116614843421SMatthew Ahrens 		}
116714843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
116814843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
116914843421SMatthew Ahrens 		dmu_tx_commit(tx);
117014843421SMatthew Ahrens 	}
117114843421SMatthew Ahrens 
1172503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
117314843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
117414843421SMatthew Ahrens 	return (0);
117514843421SMatthew Ahrens }
117614843421SMatthew Ahrens 
1177fa9e4066Sahrens void
1178a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1179a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1180fa9e4066Sahrens {
1181503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1182a2eea2e1Sahrens 	    usedobjsp, availobjsp);
1183a2eea2e1Sahrens }
1184a2eea2e1Sahrens 
1185a2eea2e1Sahrens uint64_t
1186a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1187a2eea2e1Sahrens {
1188503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1189a2eea2e1Sahrens }
1190a2eea2e1Sahrens 
1191a2eea2e1Sahrens void
1192a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1193a2eea2e1Sahrens {
1194503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
1195503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
1196503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1197a2eea2e1Sahrens }
1198a2eea2e1Sahrens 
1199a2eea2e1Sahrens void
1200a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1201a2eea2e1Sahrens {
1202503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
1203503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
1204a2eea2e1Sahrens 
1205503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1206503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1207a2eea2e1Sahrens 
1208a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1209503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
121014843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
121114843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
1212fa9e4066Sahrens }
1213fa9e4066Sahrens 
1214fa9e4066Sahrens int
1215fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1216fa9e4066Sahrens {
1217503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1218503ad85cSMatthew Ahrens 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1219fa9e4066Sahrens 	else
1220fa9e4066Sahrens 		return (B_FALSE);
1221fa9e4066Sahrens }
1222fa9e4066Sahrens 
1223ab04eb8eStimh int
1224ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1225ab04eb8eStimh     boolean_t *conflict)
1226ab04eb8eStimh {
1227503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1228ab04eb8eStimh 	uint64_t ignored;
1229ab04eb8eStimh 
1230ab04eb8eStimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1231ab04eb8eStimh 		return (ENOENT);
1232ab04eb8eStimh 
1233ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1234ab04eb8eStimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1235ab04eb8eStimh 	    real, maxlen, conflict));
1236ab04eb8eStimh }
1237ab04eb8eStimh 
1238fa9e4066Sahrens int
1239fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1240b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1241fa9e4066Sahrens {
1242503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1243fa9e4066Sahrens 	zap_cursor_t cursor;
1244fa9e4066Sahrens 	zap_attribute_t attr;
1245fa9e4066Sahrens 
1246fa9e4066Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1247fa9e4066Sahrens 		return (ENOENT);
1248fa9e4066Sahrens 
1249fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
1250fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1251fa9e4066Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1252fa9e4066Sahrens 
125387e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
125487e5029aSahrens 		zap_cursor_fini(&cursor);
125587e5029aSahrens 		return (ENOENT);
125687e5029aSahrens 	}
125787e5029aSahrens 
125887e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
125987e5029aSahrens 		zap_cursor_fini(&cursor);
126087e5029aSahrens 		return (ENAMETOOLONG);
126187e5029aSahrens 	}
126287e5029aSahrens 
126387e5029aSahrens 	(void) strcpy(name, attr.za_name);
126487e5029aSahrens 	if (idp)
126587e5029aSahrens 		*idp = attr.za_first_integer;
1266b38f0970Sck 	if (case_conflict)
1267b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
126887e5029aSahrens 	zap_cursor_advance(&cursor);
126987e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
127087e5029aSahrens 	zap_cursor_fini(&cursor);
127187e5029aSahrens 
127287e5029aSahrens 	return (0);
127387e5029aSahrens }
127487e5029aSahrens 
127587e5029aSahrens int
127687e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
127787e5029aSahrens     uint64_t *idp, uint64_t *offp)
127887e5029aSahrens {
1279503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
128087e5029aSahrens 	zap_cursor_t cursor;
128187e5029aSahrens 	zap_attribute_t attr;
128287e5029aSahrens 
128387e5029aSahrens 	/* there is no next dir on a snapshot! */
1284503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
128587e5029aSahrens 	    dd->dd_phys->dd_head_dataset_obj)
1286fa9e4066Sahrens 		return (ENOENT);
1287fa9e4066Sahrens 
128887e5029aSahrens 	zap_cursor_init_serialized(&cursor,
128987e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
129087e5029aSahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
129187e5029aSahrens 
129287e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
129387e5029aSahrens 		zap_cursor_fini(&cursor);
129487e5029aSahrens 		return (ENOENT);
129587e5029aSahrens 	}
129687e5029aSahrens 
129787e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
129887e5029aSahrens 		zap_cursor_fini(&cursor);
1299fa9e4066Sahrens 		return (ENAMETOOLONG);
130087e5029aSahrens 	}
1301fa9e4066Sahrens 
1302fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
130387e5029aSahrens 	if (idp)
130487e5029aSahrens 		*idp = attr.za_first_integer;
1305fa9e4066Sahrens 	zap_cursor_advance(&cursor);
1306fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
130787e5029aSahrens 	zap_cursor_fini(&cursor);
1308fa9e4066Sahrens 
1309fa9e4066Sahrens 	return (0);
1310fa9e4066Sahrens }
1311fa9e4066Sahrens 
1312088f3894Sahrens struct findarg {
1313088f3894Sahrens 	int (*func)(char *, void *);
1314088f3894Sahrens 	void *arg;
1315088f3894Sahrens };
1316088f3894Sahrens 
1317088f3894Sahrens /* ARGSUSED */
1318088f3894Sahrens static int
1319088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1320088f3894Sahrens {
1321088f3894Sahrens 	struct findarg *fa = arg;
1322088f3894Sahrens 	return (fa->func((char *)dsname, fa->arg));
1323088f3894Sahrens }
1324088f3894Sahrens 
1325fa9e4066Sahrens /*
1326fa9e4066Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1327088f3894Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1328fa9e4066Sahrens  */
13291d452cf5Sahrens int
13301d452cf5Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1331088f3894Sahrens {
1332088f3894Sahrens 	struct findarg fa;
1333088f3894Sahrens 	fa.func = func;
1334088f3894Sahrens 	fa.arg = arg;
1335088f3894Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
1336088f3894Sahrens }
1337088f3894Sahrens 
1338088f3894Sahrens /*
1339088f3894Sahrens  * Find all objsets under name, call func on each
1340088f3894Sahrens  */
1341088f3894Sahrens int
1342088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
1343088f3894Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
1344fa9e4066Sahrens {
1345fa9e4066Sahrens 	dsl_dir_t *dd;
1346088f3894Sahrens 	dsl_pool_t *dp;
1347088f3894Sahrens 	dsl_dataset_t *ds;
1348fa9e4066Sahrens 	zap_cursor_t zc;
1349b7661cccSmmusante 	zap_attribute_t *attr;
1350fa9e4066Sahrens 	char *child;
1351088f3894Sahrens 	uint64_t thisobj;
1352088f3894Sahrens 	int err;
1353fa9e4066Sahrens 
1354088f3894Sahrens 	if (name == NULL)
1355088f3894Sahrens 		name = spa_name(spa);
1356088f3894Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
1357ea8dc4b6Seschrock 	if (err)
13581d452cf5Sahrens 		return (err);
1359fa9e4066Sahrens 
1360088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1361088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
1362088f3894Sahrens 		dsl_dir_close(dd, FTAG);
1363088f3894Sahrens 		return (0);
1364088f3894Sahrens 	}
1365088f3894Sahrens 
1366088f3894Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
1367b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1368088f3894Sahrens 	dp = dd->dd_pool;
1369fa9e4066Sahrens 
1370fa9e4066Sahrens 	/*
1371fa9e4066Sahrens 	 * Iterate over all children.
1372fa9e4066Sahrens 	 */
13730b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
1374088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
13750b69c2f0Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
1376b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
13770b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
1378b7661cccSmmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
1379b7661cccSmmusante 			ASSERT(attr->za_num_integers == 1);
1380fa9e4066Sahrens 
13810b69c2f0Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1382088f3894Sahrens 			(void) strcpy(child, name);
13830b69c2f0Sahrens 			(void) strcat(child, "/");
1384b7661cccSmmusante 			(void) strcat(child, attr->za_name);
1385088f3894Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
13860b69c2f0Sahrens 			kmem_free(child, MAXPATHLEN);
13870b69c2f0Sahrens 			if (err)
13880b69c2f0Sahrens 				break;
13890b69c2f0Sahrens 		}
13900b69c2f0Sahrens 		zap_cursor_fini(&zc);
13911d452cf5Sahrens 
13920b69c2f0Sahrens 		if (err) {
13930b69c2f0Sahrens 			dsl_dir_close(dd, FTAG);
1394b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
13950b69c2f0Sahrens 			return (err);
13960b69c2f0Sahrens 		}
1397fa9e4066Sahrens 	}
1398fa9e4066Sahrens 
1399fa9e4066Sahrens 	/*
1400fa9e4066Sahrens 	 * Iterate over all snapshots.
1401fa9e4066Sahrens 	 */
1402088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
1403088f3894Sahrens 		if (!dsl_pool_sync_context(dp))
1404088f3894Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
1405088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1406088f3894Sahrens 		if (!dsl_pool_sync_context(dp))
1407088f3894Sahrens 			rw_exit(&dp->dp_config_rwlock);
1408088f3894Sahrens 
1409088f3894Sahrens 		if (err == 0) {
1410088f3894Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1411088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
1412088f3894Sahrens 
1413088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1414088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
1415088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
1416088f3894Sahrens 				ASSERT(attr->za_integer_length ==
1417088f3894Sahrens 				    sizeof (uint64_t));
1418088f3894Sahrens 				ASSERT(attr->za_num_integers == 1);
1419088f3894Sahrens 
1420088f3894Sahrens 				child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1421088f3894Sahrens 				(void) strcpy(child, name);
1422088f3894Sahrens 				(void) strcat(child, "@");
1423088f3894Sahrens 				(void) strcat(child, attr->za_name);
1424088f3894Sahrens 				err = func(spa, attr->za_first_integer,
1425088f3894Sahrens 				    child, arg);
1426088f3894Sahrens 				kmem_free(child, MAXPATHLEN);
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 
1462*b24ab676SJeff Bonwick 			SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
1463*b24ab676SJeff 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