xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision e09fa4dacfb671e707d50a55ae9b5cc191e1b8cb)
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/zio_checksum.h>
40fa9e4066Sahrens #include <sys/zap.h>
41fa9e4066Sahrens #include <sys/zil.h>
42fa9e4066Sahrens #include <sys/dmu_impl.h>
43ecd6cf80Smarks #include <sys/zfs_ioctl.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 
94*e09fa4daSNeil Perrin uint64_t
95*e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
96*e09fa4daSNeil Perrin {
97*e09fa4daSNeil Perrin 	return (os->os_logbias);
98*e09fa4daSNeil Perrin }
99*e09fa4daSNeil 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 
1403baa08fcSek static void
1413baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval)
1423baa08fcSek {
143503ad85cSMatthew Ahrens 	objset_t *os = arg;
1443baa08fcSek 
1453baa08fcSek 	/*
1463baa08fcSek 	 * Inheritance and range checking should have been done by now.
1473baa08fcSek 	 */
1483baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1493baa08fcSek 	    newval == ZFS_CACHE_METADATA);
1503baa08fcSek 
151503ad85cSMatthew Ahrens 	os->os_primary_cache = newval;
1523baa08fcSek }
1533baa08fcSek 
1543baa08fcSek static void
1553baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval)
1563baa08fcSek {
157503ad85cSMatthew Ahrens 	objset_t *os = arg;
1583baa08fcSek 
1593baa08fcSek 	/*
1603baa08fcSek 	 * Inheritance and range checking should have been done by now.
1613baa08fcSek 	 */
1623baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1633baa08fcSek 	    newval == ZFS_CACHE_METADATA);
1643baa08fcSek 
165503ad85cSMatthew Ahrens 	os->os_secondary_cache = newval;
1663baa08fcSek }
1673baa08fcSek 
168*e09fa4daSNeil Perrin static void
169*e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
170*e09fa4daSNeil Perrin {
171*e09fa4daSNeil Perrin 	objset_t *os = arg;
172*e09fa4daSNeil Perrin 
173*e09fa4daSNeil Perrin 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
174*e09fa4daSNeil Perrin 	    newval == ZFS_LOGBIAS_THROUGHPUT);
175*e09fa4daSNeil Perrin 	os->os_logbias = newval;
176*e09fa4daSNeil Perrin 	if (os->os_zil)
177*e09fa4daSNeil Perrin 		zil_set_logbias(os->os_zil, newval);
178*e09fa4daSNeil Perrin }
179*e09fa4daSNeil Perrin 
180fa9e4066Sahrens void
181fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
182fa9e4066Sahrens {
183fa9e4066Sahrens 	objset_phys_t *osp = buf;
184fa9e4066Sahrens 
18514843421SMatthew Ahrens 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
186fa9e4066Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
187fa9e4066Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
188fa9e4066Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
18914843421SMatthew Ahrens 	osp->os_flags = BSWAP_64(osp->os_flags);
19014843421SMatthew Ahrens 	if (size == sizeof (objset_phys_t)) {
19114843421SMatthew Ahrens 		dnode_byteswap(&osp->os_userused_dnode);
19214843421SMatthew Ahrens 		dnode_byteswap(&osp->os_groupused_dnode);
19314843421SMatthew Ahrens 	}
194fa9e4066Sahrens }
195fa9e4066Sahrens 
196ea8dc4b6Seschrock int
197ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
198503ad85cSMatthew Ahrens     objset_t **osp)
199fa9e4066Sahrens {
200503ad85cSMatthew Ahrens 	objset_t *os;
201088f3894Sahrens 	int i, err;
202fa9e4066Sahrens 
20391ebeef5Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
20491ebeef5Sahrens 
205503ad85cSMatthew Ahrens 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
206503ad85cSMatthew Ahrens 	os->os_dsl_dataset = ds;
207503ad85cSMatthew Ahrens 	os->os_spa = spa;
208503ad85cSMatthew Ahrens 	os->os_rootbp = bp;
209503ad85cSMatthew Ahrens 	if (!BP_IS_HOLE(os->os_rootbp)) {
21013506d1eSmaybee 		uint32_t aflags = ARC_WAIT;
211ea8dc4b6Seschrock 		zbookmark_t zb;
212ea8dc4b6Seschrock 		zb.zb_objset = ds ? ds->ds_object : 0;
213ea8dc4b6Seschrock 		zb.zb_object = 0;
214ea8dc4b6Seschrock 		zb.zb_level = -1;
215ea8dc4b6Seschrock 		zb.zb_blkid = 0;
216503ad85cSMatthew Ahrens 		if (DMU_OS_IS_L2CACHEABLE(os))
2173baa08fcSek 			aflags |= ARC_L2CACHE;
218ea8dc4b6Seschrock 
219503ad85cSMatthew Ahrens 		dprintf_bp(os->os_rootbp, "reading %s", "");
220088f3894Sahrens 		/*
221088f3894Sahrens 		 * NB: when bprewrite scrub can change the bp,
222088f3894Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
223088f3894Sahrens 		 * could change, and we'll need a lock.
224088f3894Sahrens 		 */
225503ad85cSMatthew Ahrens 		err = arc_read_nolock(NULL, spa, os->os_rootbp,
226503ad85cSMatthew Ahrens 		    arc_getbuf_func, &os->os_phys_buf,
22713506d1eSmaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
228ea8dc4b6Seschrock 		if (err) {
229503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
230b87f3af3Sperrin 			/* convert checksum errors into IO errors */
231b87f3af3Sperrin 			if (err == ECKSUM)
232b87f3af3Sperrin 				err = EIO;
233ea8dc4b6Seschrock 			return (err);
234ea8dc4b6Seschrock 		}
23514843421SMatthew Ahrens 
23614843421SMatthew Ahrens 		/* Increase the blocksize if we are permitted. */
23714843421SMatthew Ahrens 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
238503ad85cSMatthew Ahrens 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
23914843421SMatthew Ahrens 			arc_buf_t *buf = arc_buf_alloc(spa,
240503ad85cSMatthew Ahrens 			    sizeof (objset_phys_t), &os->os_phys_buf,
24114843421SMatthew Ahrens 			    ARC_BUFC_METADATA);
24214843421SMatthew Ahrens 			bzero(buf->b_data, sizeof (objset_phys_t));
243503ad85cSMatthew Ahrens 			bcopy(os->os_phys_buf->b_data, buf->b_data,
244503ad85cSMatthew Ahrens 			    arc_buf_size(os->os_phys_buf));
245503ad85cSMatthew Ahrens 			(void) arc_buf_remove_ref(os->os_phys_buf,
246503ad85cSMatthew Ahrens 			    &os->os_phys_buf);
247503ad85cSMatthew Ahrens 			os->os_phys_buf = buf;
24814843421SMatthew Ahrens 		}
24914843421SMatthew Ahrens 
250503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
251503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
252fa9e4066Sahrens 	} else {
25314843421SMatthew Ahrens 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
25414843421SMatthew Ahrens 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
255503ad85cSMatthew Ahrens 		os->os_phys_buf = arc_buf_alloc(spa, size,
256503ad85cSMatthew Ahrens 		    &os->os_phys_buf, ARC_BUFC_METADATA);
257503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
258503ad85cSMatthew Ahrens 		bzero(os->os_phys, size);
259fa9e4066Sahrens 	}
260fa9e4066Sahrens 
261fa9e4066Sahrens 	/*
262fa9e4066Sahrens 	 * Note: the changed_cb will be called once before the register
263fa9e4066Sahrens 	 * func returns, thus changing the checksum/compression from the
2643baa08fcSek 	 * default (fletcher2/off).  Snapshots don't need to know about
2653baa08fcSek 	 * checksum/compression/copies.
266fa9e4066Sahrens 	 */
2673baa08fcSek 	if (ds) {
2683baa08fcSek 		err = dsl_prop_register(ds, "primarycache",
269503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
270d0ad202dSahrens 		if (err == 0)
2713baa08fcSek 			err = dsl_prop_register(ds, "secondarycache",
272503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
2733baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
2743baa08fcSek 			if (err == 0)
2753baa08fcSek 				err = dsl_prop_register(ds, "checksum",
276503ad85cSMatthew Ahrens 				    checksum_changed_cb, os);
2773baa08fcSek 			if (err == 0)
2783baa08fcSek 				err = dsl_prop_register(ds, "compression",
279503ad85cSMatthew Ahrens 				    compression_changed_cb, os);
2803baa08fcSek 			if (err == 0)
2813baa08fcSek 				err = dsl_prop_register(ds, "copies",
282503ad85cSMatthew Ahrens 				    copies_changed_cb, os);
283*e09fa4daSNeil Perrin 			if (err == 0)
284*e09fa4daSNeil Perrin 				err = dsl_prop_register(ds, "logbias",
285*e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
2863baa08fcSek 		}
287ea8dc4b6Seschrock 		if (err) {
288503ad85cSMatthew Ahrens 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
289503ad85cSMatthew Ahrens 			    &os->os_phys_buf) == 1);
290503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
291ea8dc4b6Seschrock 			return (err);
292ea8dc4b6Seschrock 		}
29399653d4eSeschrock 	} else if (ds == NULL) {
294fa9e4066Sahrens 		/* It's the meta-objset. */
295503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
296503ad85cSMatthew Ahrens 		os->os_compress = ZIO_COMPRESS_LZJB;
297503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
298503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
299503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
300fa9e4066Sahrens 	}
301fa9e4066Sahrens 
302503ad85cSMatthew Ahrens 	os->os_zil_header = os->os_phys->os_zil_header;
303503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
304fa9e4066Sahrens 
305fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
306503ad85cSMatthew Ahrens 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
307fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
308503ad85cSMatthew Ahrens 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
309fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
310fa9e4066Sahrens 	}
311503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
312fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
313503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
314fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
315fa9e4066Sahrens 
316503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
317503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
318503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
319503ad85cSMatthew Ahrens 
320503ad85cSMatthew Ahrens 	os->os_meta_dnode = dnode_special_open(os,
321503ad85cSMatthew Ahrens 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
322503ad85cSMatthew Ahrens 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
323503ad85cSMatthew Ahrens 		os->os_userused_dnode = dnode_special_open(os,
324503ad85cSMatthew Ahrens 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
325503ad85cSMatthew Ahrens 		os->os_groupused_dnode = dnode_special_open(os,
326503ad85cSMatthew Ahrens 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
32714843421SMatthew Ahrens 	}
328fa9e4066Sahrens 
32991ebeef5Sahrens 	/*
33091ebeef5Sahrens 	 * We should be the only thread trying to do this because we
33191ebeef5Sahrens 	 * have ds_opening_lock
33291ebeef5Sahrens 	 */
33391ebeef5Sahrens 	if (ds) {
334503ad85cSMatthew Ahrens 		mutex_enter(&ds->ds_lock);
335503ad85cSMatthew Ahrens 		ASSERT(ds->ds_objset == NULL);
336503ad85cSMatthew Ahrens 		ds->ds_objset = os;
337503ad85cSMatthew Ahrens 		mutex_exit(&ds->ds_lock);
338fa9e4066Sahrens 	}
339fa9e4066Sahrens 
340503ad85cSMatthew Ahrens 	*osp = os;
341ea8dc4b6Seschrock 	return (0);
342fa9e4066Sahrens }
343fa9e4066Sahrens 
344503ad85cSMatthew Ahrens int
345503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
3463cb34c60Sahrens {
347503ad85cSMatthew Ahrens 	int err = 0;
3483cb34c60Sahrens 
3493cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
350503ad85cSMatthew Ahrens 	*osp = ds->ds_objset;
351503ad85cSMatthew Ahrens 	if (*osp == NULL) {
3523cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
353503ad85cSMatthew Ahrens 		    ds, &ds->ds_phys->ds_bp, osp);
3543cb34c60Sahrens 	}
3553cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
356503ad85cSMatthew Ahrens 	return (err);
3573cb34c60Sahrens }
3583cb34c60Sahrens 
359503ad85cSMatthew Ahrens /* called from zpl */
3603cb34c60Sahrens int
361503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
3623cb34c60Sahrens {
363503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
3643cb34c60Sahrens 	int err;
3653cb34c60Sahrens 
366503ad85cSMatthew Ahrens 	err = dsl_dataset_hold(name, tag, &ds);
3673cb34c60Sahrens 	if (err)
368503ad85cSMatthew Ahrens 		return (err);
369503ad85cSMatthew Ahrens 
370503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
371503ad85cSMatthew Ahrens 	if (err)
372503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
373503ad85cSMatthew Ahrens 
3743cb34c60Sahrens 	return (err);
3753cb34c60Sahrens }
3763cb34c60Sahrens 
377fa9e4066Sahrens /* called from zpl */
378fa9e4066Sahrens int
379503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
380503ad85cSMatthew Ahrens     boolean_t readonly, void *tag, objset_t **osp)
381fa9e4066Sahrens {
382f18faf3fSek 	dsl_dataset_t *ds;
383f18faf3fSek 	int err;
384fa9e4066Sahrens 
385503ad85cSMatthew Ahrens 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
386503ad85cSMatthew Ahrens 	if (err)
387fa9e4066Sahrens 		return (err);
388fa9e4066Sahrens 
389503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
3903cb34c60Sahrens 	if (err) {
391503ad85cSMatthew Ahrens 		dsl_dataset_disown(ds, tag);
392503ad85cSMatthew Ahrens 	} else if ((type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) ||
393503ad85cSMatthew Ahrens 	    (!readonly && dsl_dataset_is_snapshot(ds))) {
394503ad85cSMatthew Ahrens 		dmu_objset_disown(*osp, tag);
395503ad85cSMatthew Ahrens 		return (EINVAL);
396fa9e4066Sahrens 	}
397503ad85cSMatthew Ahrens 
3983cb34c60Sahrens 	return (err);
399fa9e4066Sahrens }
400fa9e4066Sahrens 
401fa9e4066Sahrens void
402503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
403fa9e4066Sahrens {
404503ad85cSMatthew Ahrens 	dsl_dataset_rele(os->os_dsl_dataset, tag);
405503ad85cSMatthew Ahrens }
406503ad85cSMatthew Ahrens 
407503ad85cSMatthew Ahrens void
408503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
409503ad85cSMatthew Ahrens {
410503ad85cSMatthew Ahrens 	dsl_dataset_disown(os->os_dsl_dataset, tag);
411fa9e4066Sahrens }
412fa9e4066Sahrens 
413436b2950Sperrin int
4141934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
415ea8dc4b6Seschrock {
416ea8dc4b6Seschrock 	dnode_t *dn;
417c543ec06Sahrens 
418503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
419c543ec06Sahrens 
420c543ec06Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
421503ad85cSMatthew Ahrens 	list_remove(&os->os_dnodes, os->os_meta_dnode);
422503ad85cSMatthew Ahrens 	list_insert_tail(&os->os_dnodes, os->os_meta_dnode);
423ea8dc4b6Seschrock 
424ea8dc4b6Seschrock 	/*
425c543ec06Sahrens 	 * Find the first dnode with holds.  We have to do this dance
426c543ec06Sahrens 	 * because dnode_add_ref() only works if you already have a
427c543ec06Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
428c543ec06Sahrens 	 * skip.
429ea8dc4b6Seschrock 	 */
430503ad85cSMatthew Ahrens 	for (dn = list_head(&os->os_dnodes);
4311934e92fSmaybee 	    dn && !dnode_add_ref(dn, FTAG);
432503ad85cSMatthew Ahrens 	    dn = list_next(&os->os_dnodes, dn))
433c543ec06Sahrens 		continue;
434c543ec06Sahrens 
435c543ec06Sahrens 	while (dn) {
436c543ec06Sahrens 		dnode_t *next_dn = dn;
437c543ec06Sahrens 
438c543ec06Sahrens 		do {
439503ad85cSMatthew Ahrens 			next_dn = list_next(&os->os_dnodes, next_dn);
4401934e92fSmaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
441c543ec06Sahrens 
442503ad85cSMatthew Ahrens 		mutex_exit(&os->os_lock);
4431934e92fSmaybee 		dnode_evict_dbufs(dn);
444c543ec06Sahrens 		dnode_rele(dn, FTAG);
445503ad85cSMatthew Ahrens 		mutex_enter(&os->os_lock);
446c543ec06Sahrens 		dn = next_dn;
447ea8dc4b6Seschrock 	}
448503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
449503ad85cSMatthew Ahrens 	return (list_head(&os->os_dnodes) != os->os_meta_dnode);
450ea8dc4b6Seschrock }
451ea8dc4b6Seschrock 
452fa9e4066Sahrens void
453503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
454fa9e4066Sahrens {
455503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
45699653d4eSeschrock 	int i;
457fa9e4066Sahrens 
458fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
459503ad85cSMatthew Ahrens 		ASSERT(list_head(&os->os_dirty_dnodes[i]) == NULL);
460503ad85cSMatthew Ahrens 		ASSERT(list_head(&os->os_free_dnodes[i]) == NULL);
461fa9e4066Sahrens 	}
462fa9e4066Sahrens 
4633baa08fcSek 	if (ds) {
4643baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
4653baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
466503ad85cSMatthew Ahrens 			    checksum_changed_cb, os));
4673baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
468503ad85cSMatthew Ahrens 			    compression_changed_cb, os));
4693baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
470503ad85cSMatthew Ahrens 			    copies_changed_cb, os));
471*e09fa4daSNeil Perrin 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
472*e09fa4daSNeil Perrin 			    logbias_changed_cb, os));
4733baa08fcSek 		}
4743baa08fcSek 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
475503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os));
4763baa08fcSek 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
477503ad85cSMatthew Ahrens 		    secondary_cache_changed_cb, os));
478fa9e4066Sahrens 	}
479fa9e4066Sahrens 
480ea8dc4b6Seschrock 	/*
481ea8dc4b6Seschrock 	 * We should need only a single pass over the dnode list, since
482ea8dc4b6Seschrock 	 * nothing can be added to the list at this point.
483ea8dc4b6Seschrock 	 */
484503ad85cSMatthew Ahrens 	(void) dmu_objset_evict_dbufs(os);
485ea8dc4b6Seschrock 
486503ad85cSMatthew Ahrens 	dnode_special_close(os->os_meta_dnode);
487503ad85cSMatthew Ahrens 	if (os->os_userused_dnode) {
488503ad85cSMatthew Ahrens 		dnode_special_close(os->os_userused_dnode);
489503ad85cSMatthew Ahrens 		dnode_special_close(os->os_groupused_dnode);
49014843421SMatthew Ahrens 	}
491503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
492fa9e4066Sahrens 
493503ad85cSMatthew Ahrens 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
49414843421SMatthew Ahrens 
495503ad85cSMatthew Ahrens 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
496503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
497503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
498503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
499503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
500fa9e4066Sahrens }
501fa9e4066Sahrens 
502fa9e4066Sahrens /* called from dsl for meta-objset */
503503ad85cSMatthew Ahrens objset_t *
504c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
505c717a561Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
506fa9e4066Sahrens {
507503ad85cSMatthew Ahrens 	objset_t *os;
508fa9e4066Sahrens 	dnode_t *mdn;
509fa9e4066Sahrens 
510fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
51191ebeef5Sahrens 	if (ds)
51291ebeef5Sahrens 		mutex_enter(&ds->ds_opening_lock);
513503ad85cSMatthew Ahrens 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
51491ebeef5Sahrens 	if (ds)
51591ebeef5Sahrens 		mutex_exit(&ds->ds_opening_lock);
516503ad85cSMatthew Ahrens 	mdn = os->os_meta_dnode;
517fa9e4066Sahrens 
518fa9e4066Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
519fa9e4066Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
520fa9e4066Sahrens 
521fa9e4066Sahrens 	/*
522fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
523fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
524fa9e4066Sahrens 	 * we are also accessing it in open context.
525fa9e4066Sahrens 	 *
526fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
527fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
528fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
529fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
530fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
531fa9e4066Sahrens 	 */
532ea8dc4b6Seschrock 	if (ds != NULL) {
533ea8dc4b6Seschrock 		int levels = 1;
534ea8dc4b6Seschrock 
535ea8dc4b6Seschrock 		/*
536ea8dc4b6Seschrock 		 * Determine the number of levels necessary for the meta-dnode
537ea8dc4b6Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
538ea8dc4b6Seschrock 		 */
539ea8dc4b6Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
540ea8dc4b6Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
541ea8dc4b6Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
542ea8dc4b6Seschrock 			levels++;
543ea8dc4b6Seschrock 
544fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
545ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
546ea8dc4b6Seschrock 	}
547fa9e4066Sahrens 
548fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
549fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
550fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
551503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
552503ad85cSMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
553503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
554503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
55514843421SMatthew Ahrens 	}
556fa9e4066Sahrens 
557fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
558fa9e4066Sahrens 
559503ad85cSMatthew Ahrens 	return (os);
560fa9e4066Sahrens }
561fa9e4066Sahrens 
562fa9e4066Sahrens struct oscarg {
563ecd6cf80Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
564fa9e4066Sahrens 	void *userarg;
565ae46e4c7SMatthew Ahrens 	dsl_dataset_t *clone_origin;
566fa9e4066Sahrens 	const char *lastname;
567fa9e4066Sahrens 	dmu_objset_type_t type;
568ab04eb8eStimh 	uint64_t flags;
569fa9e4066Sahrens };
570fa9e4066Sahrens 
571ecd6cf80Smarks /*ARGSUSED*/
572fa9e4066Sahrens static int
5731d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
574fa9e4066Sahrens {
5751d452cf5Sahrens 	dsl_dir_t *dd = arg1;
5761d452cf5Sahrens 	struct oscarg *oa = arg2;
5771d452cf5Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
578fa9e4066Sahrens 	int err;
5791d452cf5Sahrens 	uint64_t ddobj;
5801d452cf5Sahrens 
5811d452cf5Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
5821d452cf5Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
5831d452cf5Sahrens 	if (err != ENOENT)
5841d452cf5Sahrens 		return (err ? err : EEXIST);
5851d452cf5Sahrens 
586ae46e4c7SMatthew Ahrens 	if (oa->clone_origin != NULL) {
587ae46e4c7SMatthew Ahrens 		/* You can't clone across pools. */
588ae46e4c7SMatthew Ahrens 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
5891d452cf5Sahrens 			return (EXDEV);
5901d452cf5Sahrens 
591ae46e4c7SMatthew Ahrens 		/* You can only clone snapshots, not the head datasets. */
592ae46e4c7SMatthew Ahrens 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
5931d452cf5Sahrens 			return (EINVAL);
5941d452cf5Sahrens 	}
595ecd6cf80Smarks 
5961d452cf5Sahrens 	return (0);
5971d452cf5Sahrens }
5981d452cf5Sahrens 
5991d452cf5Sahrens static void
600ecd6cf80Smarks dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
6011d452cf5Sahrens {
6021d452cf5Sahrens 	dsl_dir_t *dd = arg1;
6031d452cf5Sahrens 	struct oscarg *oa = arg2;
6041d452cf5Sahrens 	uint64_t dsobj;
605fa9e4066Sahrens 
606fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
607fa9e4066Sahrens 
6081d452cf5Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
609ae46e4c7SMatthew Ahrens 	    oa->clone_origin, oa->flags, cr, tx);
610fa9e4066Sahrens 
611ae46e4c7SMatthew Ahrens 	if (oa->clone_origin == NULL) {
612ae46e4c7SMatthew Ahrens 		dsl_dataset_t *ds;
613ae46e4c7SMatthew Ahrens 		blkptr_t *bp;
614503ad85cSMatthew Ahrens 		objset_t *os;
615fa9e4066Sahrens 
616ae46e4c7SMatthew Ahrens 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
617ae46e4c7SMatthew Ahrens 		    FTAG, &ds));
618ae46e4c7SMatthew Ahrens 		bp = dsl_dataset_get_blkptr(ds);
619ae46e4c7SMatthew Ahrens 		ASSERT(BP_IS_HOLE(bp));
620ae46e4c7SMatthew Ahrens 
621503ad85cSMatthew Ahrens 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
622c717a561Smaybee 		    ds, bp, oa->type, tx);
623fa9e4066Sahrens 
624fa9e4066Sahrens 		if (oa->userfunc)
625503ad85cSMatthew Ahrens 			oa->userfunc(os, oa->userarg, cr, tx);
626ae46e4c7SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
627fa9e4066Sahrens 	}
628ecd6cf80Smarks 
629ecd6cf80Smarks 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
630ecd6cf80Smarks 	    tx, cr, "dataset = %llu", dsobj);
631fa9e4066Sahrens }
632fa9e4066Sahrens 
633fa9e4066Sahrens int
634ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
635ecd6cf80Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
636fa9e4066Sahrens {
6371d452cf5Sahrens 	dsl_dir_t *pdd;
638fa9e4066Sahrens 	const char *tail;
639fa9e4066Sahrens 	int err = 0;
6401d452cf5Sahrens 	struct oscarg oa = { 0 };
641fa9e4066Sahrens 
6421d452cf5Sahrens 	ASSERT(strchr(name, '@') == NULL);
6431d452cf5Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
644ea8dc4b6Seschrock 	if (err)
645ea8dc4b6Seschrock 		return (err);
646fa9e4066Sahrens 	if (tail == NULL) {
6471d452cf5Sahrens 		dsl_dir_close(pdd, FTAG);
648fa9e4066Sahrens 		return (EEXIST);
649fa9e4066Sahrens 	}
650fa9e4066Sahrens 
6511d452cf5Sahrens 	oa.userfunc = func;
6521d452cf5Sahrens 	oa.userarg = arg;
6531d452cf5Sahrens 	oa.lastname = tail;
6541d452cf5Sahrens 	oa.type = type;
655ab04eb8eStimh 	oa.flags = flags;
656ecd6cf80Smarks 
657ae46e4c7SMatthew Ahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
658ae46e4c7SMatthew Ahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
659ae46e4c7SMatthew Ahrens 	dsl_dir_close(pdd, FTAG);
660ae46e4c7SMatthew Ahrens 	return (err);
661ae46e4c7SMatthew Ahrens }
662ae46e4c7SMatthew Ahrens 
663ae46e4c7SMatthew Ahrens int
664ae46e4c7SMatthew Ahrens dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
665ae46e4c7SMatthew Ahrens {
666ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
667ae46e4c7SMatthew Ahrens 	const char *tail;
668ae46e4c7SMatthew Ahrens 	int err = 0;
669ae46e4c7SMatthew Ahrens 	struct oscarg oa = { 0 };
670ae46e4c7SMatthew Ahrens 
671ae46e4c7SMatthew Ahrens 	ASSERT(strchr(name, '@') == NULL);
672ae46e4c7SMatthew Ahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
673ae46e4c7SMatthew Ahrens 	if (err)
674ae46e4c7SMatthew Ahrens 		return (err);
675ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
676ae46e4c7SMatthew Ahrens 		dsl_dir_close(pdd, FTAG);
677ae46e4c7SMatthew Ahrens 		return (EEXIST);
678fa9e4066Sahrens 	}
679ae46e4c7SMatthew Ahrens 
680ae46e4c7SMatthew Ahrens 	oa.lastname = tail;
681ae46e4c7SMatthew Ahrens 	oa.clone_origin = clone_origin;
682ae46e4c7SMatthew Ahrens 	oa.flags = flags;
683ae46e4c7SMatthew Ahrens 
6841d452cf5Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
6851d452cf5Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
6861d452cf5Sahrens 	dsl_dir_close(pdd, FTAG);
687fa9e4066Sahrens 	return (err);
688fa9e4066Sahrens }
689fa9e4066Sahrens 
690fa9e4066Sahrens int
691842727c2SChris Kirby dmu_objset_destroy(const char *name, boolean_t defer)
692fa9e4066Sahrens {
693503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
694fa9e4066Sahrens 	int error;
695fa9e4066Sahrens 
696fa9e4066Sahrens 	/*
697ae46e4c7SMatthew Ahrens 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
698ae46e4c7SMatthew Ahrens 	 * intent log, but if there is an active log, it has blocks that
699ae46e4c7SMatthew Ahrens 	 * are allocated, but may not yet be reflected in the on-disk
700ae46e4c7SMatthew Ahrens 	 * structure.  Only the ZIL knows how to free them, so we have
701ae46e4c7SMatthew Ahrens 	 * to call into it here.
702fa9e4066Sahrens 	 */
703503ad85cSMatthew Ahrens 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
704fa9e4066Sahrens 	if (error == 0) {
705503ad85cSMatthew Ahrens 		objset_t *os;
706503ad85cSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) == 0)
707503ad85cSMatthew Ahrens 			zil_destroy(dmu_objset_zil(os), B_FALSE);
708503ad85cSMatthew Ahrens 		error = dsl_dataset_destroy(ds, FTAG, defer);
709ae46e4c7SMatthew Ahrens 		/* dsl_dataset_destroy() closes the ds. */
710fa9e4066Sahrens 	}
711fa9e4066Sahrens 
7123cb34c60Sahrens 	return (error);
713fa9e4066Sahrens }
714fa9e4066Sahrens 
7151d452cf5Sahrens struct snaparg {
7161d452cf5Sahrens 	dsl_sync_task_group_t *dstg;
7171d452cf5Sahrens 	char *snapname;
7181d452cf5Sahrens 	char failed[MAXPATHLEN];
719ecd6cf80Smarks 	boolean_t checkperms;
720ea2f5b9eSMatthew Ahrens 	nvlist_t *props;
7213cb34c60Sahrens };
7223cb34c60Sahrens 
723ea2f5b9eSMatthew Ahrens static int
724ea2f5b9eSMatthew Ahrens snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
725ea2f5b9eSMatthew Ahrens {
726ea2f5b9eSMatthew Ahrens 	objset_t *os = arg1;
727ea2f5b9eSMatthew Ahrens 	struct snaparg *sn = arg2;
728ea2f5b9eSMatthew Ahrens 
729ea2f5b9eSMatthew Ahrens 	/* The props have already been checked by zfs_check_userprops(). */
730ea2f5b9eSMatthew Ahrens 
731503ad85cSMatthew Ahrens 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
732ea2f5b9eSMatthew Ahrens 	    sn->snapname, tx));
733ea2f5b9eSMatthew Ahrens }
734ea2f5b9eSMatthew Ahrens 
735ea2f5b9eSMatthew Ahrens static void
736ea2f5b9eSMatthew Ahrens snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
737ea2f5b9eSMatthew Ahrens {
738ea2f5b9eSMatthew Ahrens 	objset_t *os = arg1;
739503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
740ea2f5b9eSMatthew Ahrens 	struct snaparg *sn = arg2;
741ea2f5b9eSMatthew Ahrens 
742ea2f5b9eSMatthew Ahrens 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
743ea2f5b9eSMatthew Ahrens 
744ea2f5b9eSMatthew Ahrens 	if (sn->props)
745ea2f5b9eSMatthew Ahrens 		dsl_props_set_sync(ds->ds_prev, sn->props, cr, tx);
746ea2f5b9eSMatthew Ahrens }
7471d452cf5Sahrens 
7481d452cf5Sahrens static int
7491d452cf5Sahrens dmu_objset_snapshot_one(char *name, void *arg)
7501d452cf5Sahrens {
7511d452cf5Sahrens 	struct snaparg *sn = arg;
7521d452cf5Sahrens 	objset_t *os;
7531d452cf5Sahrens 	int err;
7541d452cf5Sahrens 
7551d452cf5Sahrens 	(void) strcpy(sn->failed, name);
7561d452cf5Sahrens 
757ecd6cf80Smarks 	/*
758ecd6cf80Smarks 	 * Check permissions only when requested.  This only applies when
759ecd6cf80Smarks 	 * doing a recursive snapshot.  The permission checks for the starting
760ecd6cf80Smarks 	 * dataset have already been performed in zfs_secpolicy_snapshot()
761ecd6cf80Smarks 	 */
762ecd6cf80Smarks 	if (sn->checkperms == B_TRUE &&
763ecd6cf80Smarks 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
764ecd6cf80Smarks 		return (err);
765ecd6cf80Smarks 
766503ad85cSMatthew Ahrens 	err = dmu_objset_hold(name, sn, &os);
7671d452cf5Sahrens 	if (err != 0)
7681d452cf5Sahrens 		return (err);
7691d452cf5Sahrens 
770745cd3c5Smaybee 	/* If the objset is in an inconsistent state, return busy */
771503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
772503ad85cSMatthew Ahrens 		dmu_objset_rele(os, sn);
773f2e10be3Srm 		return (EBUSY);
774f2e10be3Srm 	}
775f2e10be3Srm 
7761d452cf5Sahrens 	/*
7771d452cf5Sahrens 	 * NB: we need to wait for all in-flight changes to get to disk,
7781d452cf5Sahrens 	 * so that we snapshot those changes.  zil_suspend does this as
7791d452cf5Sahrens 	 * a side effect.
7801d452cf5Sahrens 	 */
7811d452cf5Sahrens 	err = zil_suspend(dmu_objset_zil(os));
7821d452cf5Sahrens 	if (err == 0) {
783ea2f5b9eSMatthew Ahrens 		dsl_sync_task_create(sn->dstg, snapshot_check,
784ea2f5b9eSMatthew Ahrens 		    snapshot_sync, os, sn, 3);
785f2e10be3Srm 	} else {
786503ad85cSMatthew Ahrens 		dmu_objset_rele(os, sn);
7871d452cf5Sahrens 	}
788f2e10be3Srm 
7891d452cf5Sahrens 	return (err);
7901d452cf5Sahrens }
7911d452cf5Sahrens 
7921d452cf5Sahrens int
793ea2f5b9eSMatthew Ahrens dmu_objset_snapshot(char *fsname, char *snapname,
794ea2f5b9eSMatthew Ahrens     nvlist_t *props, boolean_t recursive)
7951d452cf5Sahrens {
7961d452cf5Sahrens 	dsl_sync_task_t *dst;
797ea2f5b9eSMatthew Ahrens 	struct snaparg sn;
7981d452cf5Sahrens 	spa_t *spa;
7991d452cf5Sahrens 	int err;
8001d452cf5Sahrens 
8011d452cf5Sahrens 	(void) strcpy(sn.failed, fsname);
8021d452cf5Sahrens 
80340feaa91Sahrens 	err = spa_open(fsname, &spa, FTAG);
8041d452cf5Sahrens 	if (err)
8051d452cf5Sahrens 		return (err);
8061d452cf5Sahrens 
8071d452cf5Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
8081d452cf5Sahrens 	sn.snapname = snapname;
809ea2f5b9eSMatthew Ahrens 	sn.props = props;
8101d452cf5Sahrens 
8110b69c2f0Sahrens 	if (recursive) {
812ecd6cf80Smarks 		sn.checkperms = B_TRUE;
8130b69c2f0Sahrens 		err = dmu_objset_find(fsname,
8140b69c2f0Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
8150b69c2f0Sahrens 	} else {
816ecd6cf80Smarks 		sn.checkperms = B_FALSE;
8171d452cf5Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
8180b69c2f0Sahrens 	}
8191d452cf5Sahrens 
820ea2f5b9eSMatthew Ahrens 	if (err == 0)
821ea2f5b9eSMatthew Ahrens 		err = dsl_sync_task_group_wait(sn.dstg);
8221d452cf5Sahrens 
8231d452cf5Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
8241d452cf5Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
825ea2f5b9eSMatthew Ahrens 		objset_t *os = dst->dst_arg1;
826503ad85cSMatthew Ahrens 		dsl_dataset_t *ds = os->os_dsl_dataset;
8271d452cf5Sahrens 		if (dst->dst_err)
8283cb34c60Sahrens 			dsl_dataset_name(ds, sn.failed);
829ea2f5b9eSMatthew Ahrens 		zil_resume(dmu_objset_zil(os));
830503ad85cSMatthew Ahrens 		dmu_objset_rele(os, &sn);
8313cb34c60Sahrens 	}
8323cb34c60Sahrens 
8331d452cf5Sahrens 	if (err)
8341d452cf5Sahrens 		(void) strcpy(fsname, sn.failed);
8351d452cf5Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
8361d452cf5Sahrens 	spa_close(spa, FTAG);
8371d452cf5Sahrens 	return (err);
8381d452cf5Sahrens }
8391d452cf5Sahrens 
840fa9e4066Sahrens static void
84114843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
842fa9e4066Sahrens {
843c717a561Smaybee 	dnode_t *dn;
844faafa6e3Sahrens 
845c717a561Smaybee 	while (dn = list_head(list)) {
846c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
847c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
848c717a561Smaybee 		/*
84914843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
85014843421SMatthew Ahrens 		 * meta-dnode needs to set it ouside dnode_sync().
851c717a561Smaybee 		 */
852c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
853c717a561Smaybee 		ASSERT(dn->dn_zio);
854faafa6e3Sahrens 
855c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
856c717a561Smaybee 		list_remove(list, dn);
85714843421SMatthew Ahrens 
85814843421SMatthew Ahrens 		if (newlist) {
85914843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
86014843421SMatthew Ahrens 			list_insert_tail(newlist, dn);
86114843421SMatthew Ahrens 		}
86214843421SMatthew Ahrens 
863c717a561Smaybee 		dnode_sync(dn, tx);
864fa9e4066Sahrens 	}
865fa9e4066Sahrens }
866fa9e4066Sahrens 
867fa9e4066Sahrens /* ARGSUSED */
868fa9e4066Sahrens static void
869c717a561Smaybee ready(zio_t *zio, arc_buf_t *abuf, void *arg)
870fa9e4066Sahrens {
871e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
872e14bb325SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
873503ad85cSMatthew Ahrens 	objset_t *os = arg;
874c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
875fa9e4066Sahrens 
876e14bb325SJeff Bonwick 	ASSERT(bp == os->os_rootbp);
877e14bb325SJeff Bonwick 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
878e14bb325SJeff Bonwick 	ASSERT(BP_GET_LEVEL(bp) == 0);
8790a4e9518Sgw 
880fa9e4066Sahrens 	/*
88114843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
88214843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
88314843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
88414843421SMatthew Ahrens 	 * dnode and user/group accounting objects).
885fa9e4066Sahrens 	 */
88614843421SMatthew Ahrens 	bp->blk_fill = 0;
887e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
888c717a561Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
8890a4e9518Sgw 
890e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
891e14bb325SJeff Bonwick 		ASSERT(DVA_EQUAL(BP_IDENTITY(bp), BP_IDENTITY(bp_orig)));
892e14bb325SJeff Bonwick 	} else {
8930a4e9518Sgw 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
894cdb0ab79Smaybee 			(void) dsl_dataset_block_kill(os->os_dsl_dataset,
895e14bb325SJeff Bonwick 			    &zio->io_bp_orig, zio, os->os_synctx);
8960a4e9518Sgw 		dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx);
8970a4e9518Sgw 	}
898c717a561Smaybee }
899c717a561Smaybee 
900fa9e4066Sahrens /* called from dsl */
901fa9e4066Sahrens void
902503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
903fa9e4066Sahrens {
904fa9e4066Sahrens 	int txgoff;
905ea8dc4b6Seschrock 	zbookmark_t zb;
906088f3894Sahrens 	writeprops_t wp = { 0 };
907c717a561Smaybee 	zio_t *zio;
908c717a561Smaybee 	list_t *list;
90914843421SMatthew Ahrens 	list_t *newlist = NULL;
910c717a561Smaybee 	dbuf_dirty_record_t *dr;
911c717a561Smaybee 
912c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
913fa9e4066Sahrens 
914fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
915fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
916fa9e4066Sahrens 	os->os_synctx = tx;
917fa9e4066Sahrens 
91887bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
91987bd5c1eSahrens 		/*
92087bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
92187bd5c1eSahrens 		 * spa_max_replication() could change, so reset
92287bd5c1eSahrens 		 * os_copies here.
92387bd5c1eSahrens 		 */
92487bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
92587bd5c1eSahrens 	}
92687bd5c1eSahrens 
927fa9e4066Sahrens 	/*
928c717a561Smaybee 	 * Create the root block IO
929fa9e4066Sahrens 	 */
930ea8dc4b6Seschrock 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
931ea8dc4b6Seschrock 	zb.zb_object = 0;
932e14bb325SJeff Bonwick 	zb.zb_level = -1;	/* for block ordering; it's level 0 on disk */
933ea8dc4b6Seschrock 	zb.zb_blkid = 0;
934e14bb325SJeff Bonwick 
935088f3894Sahrens 	wp.wp_type = DMU_OT_OBJSET;
936e14bb325SJeff Bonwick 	wp.wp_level = 0;	/* on-disk BP level; see above */
937088f3894Sahrens 	wp.wp_copies = os->os_copies;
938088f3894Sahrens 	wp.wp_oschecksum = os->os_checksum;
939088f3894Sahrens 	wp.wp_oscompress = os->os_compress;
940e14bb325SJeff Bonwick 
941e14bb325SJeff Bonwick 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
942e14bb325SJeff Bonwick 		(void) dsl_dataset_block_kill(os->os_dsl_dataset,
943e14bb325SJeff Bonwick 		    os->os_rootbp, pio, tx);
944e14bb325SJeff Bonwick 	}
945e14bb325SJeff Bonwick 
946088f3894Sahrens 	arc_release(os->os_phys_buf, &os->os_phys_buf);
94714843421SMatthew Ahrens 
948e14bb325SJeff Bonwick 	zio = arc_write(pio, os->os_spa, &wp, DMU_OS_IS_L2CACHEABLE(os),
949e14bb325SJeff Bonwick 	    tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os,
950e14bb325SJeff Bonwick 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
951c717a561Smaybee 
952c717a561Smaybee 	/*
95314843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
954c717a561Smaybee 	 */
955c717a561Smaybee 	os->os_meta_dnode->dn_zio = zio;
956c717a561Smaybee 	dnode_sync(os->os_meta_dnode, tx);
957c717a561Smaybee 
95814843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
95914843421SMatthew Ahrens 
96014843421SMatthew Ahrens 	if (os->os_userused_dnode &&
96114843421SMatthew Ahrens 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
96214843421SMatthew Ahrens 		os->os_userused_dnode->dn_zio = zio;
96314843421SMatthew Ahrens 		dnode_sync(os->os_userused_dnode, tx);
96414843421SMatthew Ahrens 		os->os_groupused_dnode->dn_zio = zio;
96514843421SMatthew Ahrens 		dnode_sync(os->os_groupused_dnode, tx);
96614843421SMatthew Ahrens 	}
96714843421SMatthew Ahrens 
968c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
969fa9e4066Sahrens 
97014843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
97114843421SMatthew Ahrens 		newlist = &os->os_synced_dnodes;
97214843421SMatthew Ahrens 		/*
97314843421SMatthew Ahrens 		 * We must create the list here because it uses the
97414843421SMatthew Ahrens 		 * dn_dirty_link[] of this txg.
97514843421SMatthew Ahrens 		 */
97614843421SMatthew Ahrens 		list_create(newlist, sizeof (dnode_t),
97714843421SMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
97814843421SMatthew Ahrens 	}
97914843421SMatthew Ahrens 
98014843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
98114843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
982fa9e4066Sahrens 
983c717a561Smaybee 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
984c717a561Smaybee 	while (dr = list_head(list)) {
985c717a561Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
986c717a561Smaybee 		list_remove(list, dr);
987c717a561Smaybee 		if (dr->dr_zio)
988c717a561Smaybee 			zio_nowait(dr->dr_zio);
989c717a561Smaybee 	}
990c717a561Smaybee 	/*
991c717a561Smaybee 	 * Free intent log blocks up to this tx.
992c717a561Smaybee 	 */
993c717a561Smaybee 	zil_sync(os->os_zil, tx);
994088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
995c717a561Smaybee 	zio_nowait(zio);
996fa9e4066Sahrens }
997fa9e4066Sahrens 
99814843421SMatthew Ahrens static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
99914843421SMatthew Ahrens 
100014843421SMatthew Ahrens void
100114843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
100214843421SMatthew Ahrens {
100314843421SMatthew Ahrens 	used_cbs[ost] = cb;
100414843421SMatthew Ahrens }
100514843421SMatthew Ahrens 
100614843421SMatthew Ahrens boolean_t
1007503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
100814843421SMatthew Ahrens {
100914843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
101014843421SMatthew Ahrens 	    used_cbs[os->os_phys->os_type] &&
101114843421SMatthew Ahrens 	    os->os_userused_dnode);
101214843421SMatthew Ahrens }
101314843421SMatthew Ahrens 
101414843421SMatthew Ahrens void
1015503ad85cSMatthew Ahrens dmu_objset_do_userquota_callbacks(objset_t *os, dmu_tx_t *tx)
101614843421SMatthew Ahrens {
101714843421SMatthew Ahrens 	dnode_t *dn;
101814843421SMatthew Ahrens 	list_t *list = &os->os_synced_dnodes;
101914843421SMatthew Ahrens 	static const char zerobuf[DN_MAX_BONUSLEN] = {0};
102014843421SMatthew Ahrens 
102114843421SMatthew Ahrens 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
102214843421SMatthew Ahrens 
102314843421SMatthew Ahrens 	while (dn = list_head(list)) {
102414843421SMatthew Ahrens 		dmu_object_type_t bonustype;
102514843421SMatthew Ahrens 
102614843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
102714843421SMatthew Ahrens 		ASSERT(dn->dn_oldphys);
102814843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
102914843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
103014843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
103114843421SMatthew Ahrens 
103214843421SMatthew Ahrens 		/* Allocate the user/groupused objects if necessary. */
103314843421SMatthew Ahrens 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
1034503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
103514843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT,
103614843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1037503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
103814843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT,
103914843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
104014843421SMatthew Ahrens 		}
104114843421SMatthew Ahrens 
104214843421SMatthew Ahrens 		/*
104314843421SMatthew Ahrens 		 * If the object was not previously
104414843421SMatthew Ahrens 		 * accounted, pretend that it was free.
104514843421SMatthew Ahrens 		 */
104614843421SMatthew Ahrens 		if (!(dn->dn_oldphys->dn_flags &
104714843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED)) {
104814843421SMatthew Ahrens 			bzero(dn->dn_oldphys, sizeof (dnode_phys_t));
104914843421SMatthew Ahrens 		}
105014843421SMatthew Ahrens 
105114843421SMatthew Ahrens 		/*
105214843421SMatthew Ahrens 		 * If the object was freed, use the previous bonustype.
105314843421SMatthew Ahrens 		 */
105414843421SMatthew Ahrens 		bonustype = dn->dn_phys->dn_bonustype ?
105514843421SMatthew Ahrens 		    dn->dn_phys->dn_bonustype : dn->dn_oldphys->dn_bonustype;
105614843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type != 0 ||
105714843421SMatthew Ahrens 		    (bcmp(DN_BONUS(dn->dn_phys), zerobuf,
105814843421SMatthew Ahrens 		    DN_MAX_BONUSLEN) == 0 &&
105914843421SMatthew Ahrens 		    DN_USED_BYTES(dn->dn_phys) == 0));
106014843421SMatthew Ahrens 		ASSERT(dn->dn_oldphys->dn_type != 0 ||
106114843421SMatthew Ahrens 		    (bcmp(DN_BONUS(dn->dn_oldphys), zerobuf,
106214843421SMatthew Ahrens 		    DN_MAX_BONUSLEN) == 0 &&
106314843421SMatthew Ahrens 		    DN_USED_BYTES(dn->dn_oldphys) == 0));
1064503ad85cSMatthew Ahrens 		used_cbs[os->os_phys->os_type](os, bonustype,
106514843421SMatthew Ahrens 		    DN_BONUS(dn->dn_oldphys), DN_BONUS(dn->dn_phys),
106614843421SMatthew Ahrens 		    DN_USED_BYTES(dn->dn_oldphys),
106714843421SMatthew Ahrens 		    DN_USED_BYTES(dn->dn_phys), tx);
106814843421SMatthew Ahrens 
106914843421SMatthew Ahrens 		/*
107014843421SMatthew Ahrens 		 * The mutex is needed here for interlock with dnode_allocate.
107114843421SMatthew Ahrens 		 */
107214843421SMatthew Ahrens 		mutex_enter(&dn->dn_mtx);
107314843421SMatthew Ahrens 		zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t));
107414843421SMatthew Ahrens 		dn->dn_oldphys = NULL;
107514843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
107614843421SMatthew Ahrens 
107714843421SMatthew Ahrens 		list_remove(list, dn);
107814843421SMatthew Ahrens 		dnode_rele(dn, list);
107914843421SMatthew Ahrens 	}
108014843421SMatthew Ahrens }
108114843421SMatthew Ahrens 
108214843421SMatthew Ahrens boolean_t
108314843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
108414843421SMatthew Ahrens {
1085503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
108614843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
108714843421SMatthew Ahrens }
108814843421SMatthew Ahrens 
108914843421SMatthew Ahrens int
109014843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
109114843421SMatthew Ahrens {
109214843421SMatthew Ahrens 	uint64_t obj;
109314843421SMatthew Ahrens 	int err = 0;
109414843421SMatthew Ahrens 
109514843421SMatthew Ahrens 	if (dmu_objset_userspace_present(os))
109614843421SMatthew Ahrens 		return (0);
1097503ad85cSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
109814843421SMatthew Ahrens 		return (ENOTSUP);
109914843421SMatthew Ahrens 	if (dmu_objset_is_snapshot(os))
110014843421SMatthew Ahrens 		return (EINVAL);
110114843421SMatthew Ahrens 
110214843421SMatthew Ahrens 	/*
110314843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
110414843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
110514843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
110614843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
110714843421SMatthew Ahrens 	 * independently.
110814843421SMatthew Ahrens 	 */
110914843421SMatthew Ahrens 
111014843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
111168857716SLin Ling 		dmu_tx_t *tx;
111214843421SMatthew Ahrens 		dmu_buf_t *db;
111314843421SMatthew Ahrens 		int objerr;
111414843421SMatthew Ahrens 
111514843421SMatthew Ahrens 		if (issig(JUSTLOOKING) && issig(FORREAL))
111614843421SMatthew Ahrens 			return (EINTR);
111714843421SMatthew Ahrens 
111814843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
111914843421SMatthew Ahrens 		if (objerr)
112014843421SMatthew Ahrens 			continue;
112168857716SLin Ling 		tx = dmu_tx_create(os);
112214843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
112314843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
112414843421SMatthew Ahrens 		if (objerr) {
112514843421SMatthew Ahrens 			dmu_tx_abort(tx);
112614843421SMatthew Ahrens 			continue;
112714843421SMatthew Ahrens 		}
112814843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
112914843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
113014843421SMatthew Ahrens 		dmu_tx_commit(tx);
113114843421SMatthew Ahrens 	}
113214843421SMatthew Ahrens 
1133503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
113414843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
113514843421SMatthew Ahrens 	return (0);
113614843421SMatthew Ahrens }
113714843421SMatthew Ahrens 
1138fa9e4066Sahrens void
1139a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1140a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1141fa9e4066Sahrens {
1142503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1143a2eea2e1Sahrens 	    usedobjsp, availobjsp);
1144a2eea2e1Sahrens }
1145a2eea2e1Sahrens 
1146a2eea2e1Sahrens uint64_t
1147a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1148a2eea2e1Sahrens {
1149503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1150a2eea2e1Sahrens }
1151a2eea2e1Sahrens 
1152a2eea2e1Sahrens void
1153a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1154a2eea2e1Sahrens {
1155503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
1156503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
1157503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1158a2eea2e1Sahrens }
1159a2eea2e1Sahrens 
1160a2eea2e1Sahrens void
1161a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1162a2eea2e1Sahrens {
1163503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
1164503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
1165a2eea2e1Sahrens 
1166503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1167503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1168a2eea2e1Sahrens 
1169a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1170503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
117114843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
117214843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
1173fa9e4066Sahrens }
1174fa9e4066Sahrens 
1175fa9e4066Sahrens int
1176fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1177fa9e4066Sahrens {
1178503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1179503ad85cSMatthew Ahrens 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1180fa9e4066Sahrens 	else
1181fa9e4066Sahrens 		return (B_FALSE);
1182fa9e4066Sahrens }
1183fa9e4066Sahrens 
1184ab04eb8eStimh int
1185ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1186ab04eb8eStimh     boolean_t *conflict)
1187ab04eb8eStimh {
1188503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1189ab04eb8eStimh 	uint64_t ignored;
1190ab04eb8eStimh 
1191ab04eb8eStimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1192ab04eb8eStimh 		return (ENOENT);
1193ab04eb8eStimh 
1194ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1195ab04eb8eStimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1196ab04eb8eStimh 	    real, maxlen, conflict));
1197ab04eb8eStimh }
1198ab04eb8eStimh 
1199fa9e4066Sahrens int
1200fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1201b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1202fa9e4066Sahrens {
1203503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1204fa9e4066Sahrens 	zap_cursor_t cursor;
1205fa9e4066Sahrens 	zap_attribute_t attr;
1206fa9e4066Sahrens 
1207fa9e4066Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1208fa9e4066Sahrens 		return (ENOENT);
1209fa9e4066Sahrens 
1210fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
1211fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1212fa9e4066Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1213fa9e4066Sahrens 
121487e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
121587e5029aSahrens 		zap_cursor_fini(&cursor);
121687e5029aSahrens 		return (ENOENT);
121787e5029aSahrens 	}
121887e5029aSahrens 
121987e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
122087e5029aSahrens 		zap_cursor_fini(&cursor);
122187e5029aSahrens 		return (ENAMETOOLONG);
122287e5029aSahrens 	}
122387e5029aSahrens 
122487e5029aSahrens 	(void) strcpy(name, attr.za_name);
122587e5029aSahrens 	if (idp)
122687e5029aSahrens 		*idp = attr.za_first_integer;
1227b38f0970Sck 	if (case_conflict)
1228b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
122987e5029aSahrens 	zap_cursor_advance(&cursor);
123087e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
123187e5029aSahrens 	zap_cursor_fini(&cursor);
123287e5029aSahrens 
123387e5029aSahrens 	return (0);
123487e5029aSahrens }
123587e5029aSahrens 
123687e5029aSahrens int
123787e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
123887e5029aSahrens     uint64_t *idp, uint64_t *offp)
123987e5029aSahrens {
1240503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
124187e5029aSahrens 	zap_cursor_t cursor;
124287e5029aSahrens 	zap_attribute_t attr;
124387e5029aSahrens 
124487e5029aSahrens 	/* there is no next dir on a snapshot! */
1245503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
124687e5029aSahrens 	    dd->dd_phys->dd_head_dataset_obj)
1247fa9e4066Sahrens 		return (ENOENT);
1248fa9e4066Sahrens 
124987e5029aSahrens 	zap_cursor_init_serialized(&cursor,
125087e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
125187e5029aSahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
125287e5029aSahrens 
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);
1260fa9e4066Sahrens 		return (ENAMETOOLONG);
126187e5029aSahrens 	}
1262fa9e4066Sahrens 
1263fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
126487e5029aSahrens 	if (idp)
126587e5029aSahrens 		*idp = attr.za_first_integer;
1266fa9e4066Sahrens 	zap_cursor_advance(&cursor);
1267fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
126887e5029aSahrens 	zap_cursor_fini(&cursor);
1269fa9e4066Sahrens 
1270fa9e4066Sahrens 	return (0);
1271fa9e4066Sahrens }
1272fa9e4066Sahrens 
1273088f3894Sahrens struct findarg {
1274088f3894Sahrens 	int (*func)(char *, void *);
1275088f3894Sahrens 	void *arg;
1276088f3894Sahrens };
1277088f3894Sahrens 
1278088f3894Sahrens /* ARGSUSED */
1279088f3894Sahrens static int
1280088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1281088f3894Sahrens {
1282088f3894Sahrens 	struct findarg *fa = arg;
1283088f3894Sahrens 	return (fa->func((char *)dsname, fa->arg));
1284088f3894Sahrens }
1285088f3894Sahrens 
1286fa9e4066Sahrens /*
1287fa9e4066Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1288088f3894Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1289fa9e4066Sahrens  */
12901d452cf5Sahrens int
12911d452cf5Sahrens dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1292088f3894Sahrens {
1293088f3894Sahrens 	struct findarg fa;
1294088f3894Sahrens 	fa.func = func;
1295088f3894Sahrens 	fa.arg = arg;
1296088f3894Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
1297088f3894Sahrens }
1298088f3894Sahrens 
1299088f3894Sahrens /*
1300088f3894Sahrens  * Find all objsets under name, call func on each
1301088f3894Sahrens  */
1302088f3894Sahrens int
1303088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
1304088f3894Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
1305fa9e4066Sahrens {
1306fa9e4066Sahrens 	dsl_dir_t *dd;
1307088f3894Sahrens 	dsl_pool_t *dp;
1308088f3894Sahrens 	dsl_dataset_t *ds;
1309fa9e4066Sahrens 	zap_cursor_t zc;
1310b7661cccSmmusante 	zap_attribute_t *attr;
1311fa9e4066Sahrens 	char *child;
1312088f3894Sahrens 	uint64_t thisobj;
1313088f3894Sahrens 	int err;
1314fa9e4066Sahrens 
1315088f3894Sahrens 	if (name == NULL)
1316088f3894Sahrens 		name = spa_name(spa);
1317088f3894Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
1318ea8dc4b6Seschrock 	if (err)
13191d452cf5Sahrens 		return (err);
1320fa9e4066Sahrens 
1321088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1322088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
1323088f3894Sahrens 		dsl_dir_close(dd, FTAG);
1324088f3894Sahrens 		return (0);
1325088f3894Sahrens 	}
1326088f3894Sahrens 
1327088f3894Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
1328b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1329088f3894Sahrens 	dp = dd->dd_pool;
1330fa9e4066Sahrens 
1331fa9e4066Sahrens 	/*
1332fa9e4066Sahrens 	 * Iterate over all children.
1333fa9e4066Sahrens 	 */
13340b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
1335088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
13360b69c2f0Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
1337b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
13380b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
1339b7661cccSmmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
1340b7661cccSmmusante 			ASSERT(attr->za_num_integers == 1);
1341fa9e4066Sahrens 
13420b69c2f0Sahrens 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1343088f3894Sahrens 			(void) strcpy(child, name);
13440b69c2f0Sahrens 			(void) strcat(child, "/");
1345b7661cccSmmusante 			(void) strcat(child, attr->za_name);
1346088f3894Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
13470b69c2f0Sahrens 			kmem_free(child, MAXPATHLEN);
13480b69c2f0Sahrens 			if (err)
13490b69c2f0Sahrens 				break;
13500b69c2f0Sahrens 		}
13510b69c2f0Sahrens 		zap_cursor_fini(&zc);
13521d452cf5Sahrens 
13530b69c2f0Sahrens 		if (err) {
13540b69c2f0Sahrens 			dsl_dir_close(dd, FTAG);
1355b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
13560b69c2f0Sahrens 			return (err);
13570b69c2f0Sahrens 		}
1358fa9e4066Sahrens 	}
1359fa9e4066Sahrens 
1360fa9e4066Sahrens 	/*
1361fa9e4066Sahrens 	 * Iterate over all snapshots.
1362fa9e4066Sahrens 	 */
1363088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
1364088f3894Sahrens 		if (!dsl_pool_sync_context(dp))
1365088f3894Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
1366088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1367088f3894Sahrens 		if (!dsl_pool_sync_context(dp))
1368088f3894Sahrens 			rw_exit(&dp->dp_config_rwlock);
1369088f3894Sahrens 
1370088f3894Sahrens 		if (err == 0) {
1371088f3894Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1372088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
1373088f3894Sahrens 
1374088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1375088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
1376088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
1377088f3894Sahrens 				ASSERT(attr->za_integer_length ==
1378088f3894Sahrens 				    sizeof (uint64_t));
1379088f3894Sahrens 				ASSERT(attr->za_num_integers == 1);
1380088f3894Sahrens 
1381088f3894Sahrens 				child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1382088f3894Sahrens 				(void) strcpy(child, name);
1383088f3894Sahrens 				(void) strcat(child, "@");
1384088f3894Sahrens 				(void) strcat(child, attr->za_name);
1385088f3894Sahrens 				err = func(spa, attr->za_first_integer,
1386088f3894Sahrens 				    child, arg);
1387088f3894Sahrens 				kmem_free(child, MAXPATHLEN);
1388088f3894Sahrens 				if (err)
1389088f3894Sahrens 					break;
1390088f3894Sahrens 			}
1391088f3894Sahrens 			zap_cursor_fini(&zc);
1392fa9e4066Sahrens 		}
1393fa9e4066Sahrens 	}
1394fa9e4066Sahrens 
1395fa9e4066Sahrens 	dsl_dir_close(dd, FTAG);
1396b7661cccSmmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1397fa9e4066Sahrens 
13981d452cf5Sahrens 	if (err)
13991d452cf5Sahrens 		return (err);
14001d452cf5Sahrens 
1401fa9e4066Sahrens 	/*
1402fa9e4066Sahrens 	 * Apply to self if appropriate.
1403fa9e4066Sahrens 	 */
1404088f3894Sahrens 	err = func(spa, thisobj, name, arg);
14051d452cf5Sahrens 	return (err);
1406fa9e4066Sahrens }
1407f18faf3fSek 
14087f73c863SRich Morris /* ARGSUSED */
14097f73c863SRich Morris int
14107f73c863SRich Morris dmu_objset_prefetch(char *name, void *arg)
14117f73c863SRich Morris {
14127f73c863SRich Morris 	dsl_dataset_t *ds;
14137f73c863SRich Morris 
14147f73c863SRich Morris 	if (dsl_dataset_hold(name, FTAG, &ds))
14157f73c863SRich Morris 		return (0);
14167f73c863SRich Morris 
14177f73c863SRich Morris 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
14187f73c863SRich Morris 		mutex_enter(&ds->ds_opening_lock);
1419503ad85cSMatthew Ahrens 		if (ds->ds_objset == NULL) {
14207f73c863SRich Morris 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
14217f73c863SRich Morris 			zbookmark_t zb;
14227f73c863SRich Morris 
14237f73c863SRich Morris 			zb.zb_objset = ds->ds_object;
14247f73c863SRich Morris 			zb.zb_object = 0;
14257f73c863SRich Morris 			zb.zb_level = -1;
14267f73c863SRich Morris 			zb.zb_blkid = 0;
14277f73c863SRich Morris 
14287f73c863SRich Morris 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
14297f73c863SRich Morris 			    &ds->ds_phys->ds_bp, NULL, NULL,
14307f73c863SRich Morris 			    ZIO_PRIORITY_ASYNC_READ,
14317f73c863SRich Morris 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
14327f73c863SRich Morris 			    &aflags, &zb);
14337f73c863SRich Morris 		}
14347f73c863SRich Morris 		mutex_exit(&ds->ds_opening_lock);
14357f73c863SRich Morris 	}
14367f73c863SRich Morris 
14377f73c863SRich Morris 	dsl_dataset_rele(ds, FTAG);
14387f73c863SRich Morris 	return (0);
14397f73c863SRich Morris }
14407f73c863SRich Morris 
1441f18faf3fSek void
1442f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr)
1443f18faf3fSek {
1444503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1445503ad85cSMatthew Ahrens 	os->os_user_ptr = user_ptr;
1446f18faf3fSek }
1447f18faf3fSek 
1448f18faf3fSek void *
1449f18faf3fSek dmu_objset_get_user(objset_t *os)
1450f18faf3fSek {
1451503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1452503ad85cSMatthew Ahrens 	return (os->os_user_ptr);
1453f18faf3fSek }
1454