xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 5d7b4d438c4a51eccc95e77a83a437b4d48380eb)
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 /*
2206e0070dSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23edf345e6SMatthew Ahrens  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24aad02571SSaso Kiselkov  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25a2afb611SJerry Jelinek  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
2855da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
2955da60b9SMark J Musante 
30ecd6cf80Smarks #include <sys/cred.h>
31fa9e4066Sahrens #include <sys/zfs_context.h>
32fa9e4066Sahrens #include <sys/dmu_objset.h>
33fa9e4066Sahrens #include <sys/dsl_dir.h>
34fa9e4066Sahrens #include <sys/dsl_dataset.h>
35fa9e4066Sahrens #include <sys/dsl_prop.h>
36fa9e4066Sahrens #include <sys/dsl_pool.h>
371d452cf5Sahrens #include <sys/dsl_synctask.h>
38ecd6cf80Smarks #include <sys/dsl_deleg.h>
39fa9e4066Sahrens #include <sys/dnode.h>
40fa9e4066Sahrens #include <sys/dbuf.h>
41a2eea2e1Sahrens #include <sys/zvol.h>
42fa9e4066Sahrens #include <sys/dmu_tx.h>
43fa9e4066Sahrens #include <sys/zap.h>
44fa9e4066Sahrens #include <sys/zil.h>
45fa9e4066Sahrens #include <sys/dmu_impl.h>
46ecd6cf80Smarks #include <sys/zfs_ioctl.h>
470a586ceaSMark Shellenbaum #include <sys/sa.h>
4899d5e173STim Haley #include <sys/zfs_onexit.h>
493b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
50fa9e4066Sahrens 
51744947dcSTom Erickson /*
52744947dcSTom Erickson  * Needed to close a window in dnode_move() that allows the objset to be freed
53744947dcSTom Erickson  * before it can be safely accessed.
54744947dcSTom Erickson  */
55744947dcSTom Erickson krwlock_t os_lock;
56744947dcSTom Erickson 
57744947dcSTom Erickson void
58744947dcSTom Erickson dmu_objset_init(void)
59744947dcSTom Erickson {
60744947dcSTom Erickson 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
61744947dcSTom Erickson }
62744947dcSTom Erickson 
63744947dcSTom Erickson void
64744947dcSTom Erickson dmu_objset_fini(void)
65744947dcSTom Erickson {
66744947dcSTom Erickson 	rw_destroy(&os_lock);
67744947dcSTom Erickson }
68744947dcSTom Erickson 
69fa9e4066Sahrens spa_t *
70fa9e4066Sahrens dmu_objset_spa(objset_t *os)
71fa9e4066Sahrens {
72503ad85cSMatthew Ahrens 	return (os->os_spa);
73fa9e4066Sahrens }
74fa9e4066Sahrens 
75fa9e4066Sahrens zilog_t *
76fa9e4066Sahrens dmu_objset_zil(objset_t *os)
77fa9e4066Sahrens {
78503ad85cSMatthew Ahrens 	return (os->os_zil);
79fa9e4066Sahrens }
80fa9e4066Sahrens 
81fa9e4066Sahrens dsl_pool_t *
82fa9e4066Sahrens dmu_objset_pool(objset_t *os)
83fa9e4066Sahrens {
84fa9e4066Sahrens 	dsl_dataset_t *ds;
85fa9e4066Sahrens 
86503ad85cSMatthew Ahrens 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
87fa9e4066Sahrens 		return (ds->ds_dir->dd_pool);
88fa9e4066Sahrens 	else
89503ad85cSMatthew Ahrens 		return (spa_get_dsl(os->os_spa));
90fa9e4066Sahrens }
91fa9e4066Sahrens 
92fa9e4066Sahrens dsl_dataset_t *
93fa9e4066Sahrens dmu_objset_ds(objset_t *os)
94fa9e4066Sahrens {
95503ad85cSMatthew Ahrens 	return (os->os_dsl_dataset);
96fa9e4066Sahrens }
97fa9e4066Sahrens 
98fa9e4066Sahrens dmu_objset_type_t
99fa9e4066Sahrens dmu_objset_type(objset_t *os)
100fa9e4066Sahrens {
101503ad85cSMatthew Ahrens 	return (os->os_phys->os_type);
102fa9e4066Sahrens }
103fa9e4066Sahrens 
104fa9e4066Sahrens void
105fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
106fa9e4066Sahrens {
107503ad85cSMatthew Ahrens 	dsl_dataset_name(os->os_dsl_dataset, buf);
108fa9e4066Sahrens }
109fa9e4066Sahrens 
110fa9e4066Sahrens uint64_t
111fa9e4066Sahrens dmu_objset_id(objset_t *os)
112fa9e4066Sahrens {
113503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
114fa9e4066Sahrens 
115fa9e4066Sahrens 	return (ds ? ds->ds_object : 0);
116fa9e4066Sahrens }
117fa9e4066Sahrens 
118edf345e6SMatthew Ahrens zfs_sync_type_t
11955da60b9SMark J Musante dmu_objset_syncprop(objset_t *os)
12055da60b9SMark J Musante {
12155da60b9SMark J Musante 	return (os->os_sync);
12255da60b9SMark J Musante }
12355da60b9SMark J Musante 
124edf345e6SMatthew Ahrens zfs_logbias_op_t
125e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
126e09fa4daSNeil Perrin {
127e09fa4daSNeil Perrin 	return (os->os_logbias);
128e09fa4daSNeil Perrin }
129e09fa4daSNeil Perrin 
130fa9e4066Sahrens static void
131fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
132fa9e4066Sahrens {
133503ad85cSMatthew Ahrens 	objset_t *os = arg;
134fa9e4066Sahrens 
135fa9e4066Sahrens 	/*
136fa9e4066Sahrens 	 * Inheritance should have been done by now.
137fa9e4066Sahrens 	 */
138fa9e4066Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
139fa9e4066Sahrens 
140503ad85cSMatthew Ahrens 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
141fa9e4066Sahrens }
142fa9e4066Sahrens 
143fa9e4066Sahrens static void
144fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
145fa9e4066Sahrens {
146503ad85cSMatthew Ahrens 	objset_t *os = arg;
147fa9e4066Sahrens 
148fa9e4066Sahrens 	/*
149fa9e4066Sahrens 	 * Inheritance and range checking should have been done by now.
150fa9e4066Sahrens 	 */
151fa9e4066Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
152fa9e4066Sahrens 
153503ad85cSMatthew Ahrens 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
154fa9e4066Sahrens }
155fa9e4066Sahrens 
156d0ad202dSahrens static void
157d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
158d0ad202dSahrens {
159503ad85cSMatthew Ahrens 	objset_t *os = arg;
160d0ad202dSahrens 
161d0ad202dSahrens 	/*
162d0ad202dSahrens 	 * Inheritance and range checking should have been done by now.
163d0ad202dSahrens 	 */
164d0ad202dSahrens 	ASSERT(newval > 0);
165503ad85cSMatthew Ahrens 	ASSERT(newval <= spa_max_replication(os->os_spa));
166d0ad202dSahrens 
167503ad85cSMatthew Ahrens 	os->os_copies = newval;
168d0ad202dSahrens }
169d0ad202dSahrens 
170b24ab676SJeff Bonwick static void
171b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
172b24ab676SJeff Bonwick {
173b24ab676SJeff Bonwick 	objset_t *os = arg;
174b24ab676SJeff Bonwick 	spa_t *spa = os->os_spa;
175b24ab676SJeff Bonwick 	enum zio_checksum checksum;
176b24ab676SJeff Bonwick 
177b24ab676SJeff Bonwick 	/*
178b24ab676SJeff Bonwick 	 * Inheritance should have been done by now.
179b24ab676SJeff Bonwick 	 */
180b24ab676SJeff Bonwick 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
181b24ab676SJeff Bonwick 
182b24ab676SJeff Bonwick 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
183b24ab676SJeff Bonwick 
184b24ab676SJeff Bonwick 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
185b24ab676SJeff Bonwick 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
186b24ab676SJeff Bonwick }
187b24ab676SJeff Bonwick 
1883baa08fcSek static void
1893baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval)
1903baa08fcSek {
191503ad85cSMatthew Ahrens 	objset_t *os = arg;
1923baa08fcSek 
1933baa08fcSek 	/*
1943baa08fcSek 	 * Inheritance and range checking should have been done by now.
1953baa08fcSek 	 */
1963baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1973baa08fcSek 	    newval == ZFS_CACHE_METADATA);
1983baa08fcSek 
199503ad85cSMatthew Ahrens 	os->os_primary_cache = newval;
2003baa08fcSek }
2013baa08fcSek 
2023baa08fcSek static void
2033baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval)
2043baa08fcSek {
205503ad85cSMatthew Ahrens 	objset_t *os = arg;
2063baa08fcSek 
2073baa08fcSek 	/*
2083baa08fcSek 	 * Inheritance and range checking should have been done by now.
2093baa08fcSek 	 */
2103baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2113baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2123baa08fcSek 
213503ad85cSMatthew Ahrens 	os->os_secondary_cache = newval;
2143baa08fcSek }
2153baa08fcSek 
21655da60b9SMark J Musante static void
21755da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval)
21855da60b9SMark J Musante {
21955da60b9SMark J Musante 	objset_t *os = arg;
22055da60b9SMark J Musante 
22155da60b9SMark J Musante 	/*
22255da60b9SMark J Musante 	 * Inheritance and range checking should have been done by now.
22355da60b9SMark J Musante 	 */
22455da60b9SMark J Musante 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
22555da60b9SMark J Musante 	    newval == ZFS_SYNC_DISABLED);
22655da60b9SMark J Musante 
22755da60b9SMark J Musante 	os->os_sync = newval;
22855da60b9SMark J Musante 	if (os->os_zil)
22955da60b9SMark J Musante 		zil_set_sync(os->os_zil, newval);
23055da60b9SMark J Musante }
23155da60b9SMark J Musante 
232edf345e6SMatthew Ahrens static void
233edf345e6SMatthew Ahrens redundant_metadata_changed_cb(void *arg, uint64_t newval)
234edf345e6SMatthew Ahrens {
235edf345e6SMatthew Ahrens 	objset_t *os = arg;
236edf345e6SMatthew Ahrens 
237edf345e6SMatthew Ahrens 	/*
238edf345e6SMatthew Ahrens 	 * Inheritance and range checking should have been done by now.
239edf345e6SMatthew Ahrens 	 */
240edf345e6SMatthew Ahrens 	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
241edf345e6SMatthew Ahrens 	    newval == ZFS_REDUNDANT_METADATA_MOST);
242edf345e6SMatthew Ahrens 
243edf345e6SMatthew Ahrens 	os->os_redundant_metadata = newval;
244edf345e6SMatthew Ahrens }
245edf345e6SMatthew Ahrens 
246e09fa4daSNeil Perrin static void
247e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
248e09fa4daSNeil Perrin {
249e09fa4daSNeil Perrin 	objset_t *os = arg;
250e09fa4daSNeil Perrin 
251e09fa4daSNeil Perrin 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
252e09fa4daSNeil Perrin 	    newval == ZFS_LOGBIAS_THROUGHPUT);
253e09fa4daSNeil Perrin 	os->os_logbias = newval;
254e09fa4daSNeil Perrin 	if (os->os_zil)
255e09fa4daSNeil Perrin 		zil_set_logbias(os->os_zil, newval);
256e09fa4daSNeil Perrin }
257e09fa4daSNeil Perrin 
258fa9e4066Sahrens void
259fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
260fa9e4066Sahrens {
261fa9e4066Sahrens 	objset_phys_t *osp = buf;
262fa9e4066Sahrens 
26314843421SMatthew Ahrens 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
264fa9e4066Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
265fa9e4066Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
266fa9e4066Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
26714843421SMatthew Ahrens 	osp->os_flags = BSWAP_64(osp->os_flags);
26814843421SMatthew Ahrens 	if (size == sizeof (objset_phys_t)) {
26914843421SMatthew Ahrens 		dnode_byteswap(&osp->os_userused_dnode);
27014843421SMatthew Ahrens 		dnode_byteswap(&osp->os_groupused_dnode);
27114843421SMatthew Ahrens 	}
272fa9e4066Sahrens }
273fa9e4066Sahrens 
274ea8dc4b6Seschrock int
275ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
276503ad85cSMatthew Ahrens     objset_t **osp)
277fa9e4066Sahrens {
278503ad85cSMatthew Ahrens 	objset_t *os;
279088f3894Sahrens 	int i, err;
280fa9e4066Sahrens 
28191ebeef5Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
28291ebeef5Sahrens 
283503ad85cSMatthew Ahrens 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
284503ad85cSMatthew Ahrens 	os->os_dsl_dataset = ds;
285503ad85cSMatthew Ahrens 	os->os_spa = spa;
286503ad85cSMatthew Ahrens 	os->os_rootbp = bp;
287503ad85cSMatthew Ahrens 	if (!BP_IS_HOLE(os->os_rootbp)) {
28813506d1eSmaybee 		uint32_t aflags = ARC_WAIT;
289ea8dc4b6Seschrock 		zbookmark_t zb;
290b24ab676SJeff Bonwick 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
291b24ab676SJeff Bonwick 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
292b24ab676SJeff Bonwick 
293503ad85cSMatthew Ahrens 		if (DMU_OS_IS_L2CACHEABLE(os))
2943baa08fcSek 			aflags |= ARC_L2CACHE;
295aad02571SSaso Kiselkov 		if (DMU_OS_IS_L2COMPRESSIBLE(os))
296aad02571SSaso Kiselkov 			aflags |= ARC_L2COMPRESS;
297ea8dc4b6Seschrock 
298503ad85cSMatthew Ahrens 		dprintf_bp(os->os_rootbp, "reading %s", "");
2991b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, os->os_rootbp,
300503ad85cSMatthew Ahrens 		    arc_getbuf_func, &os->os_phys_buf,
30113506d1eSmaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
3023b2aab18SMatthew Ahrens 		if (err != 0) {
303503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
304b87f3af3Sperrin 			/* convert checksum errors into IO errors */
305b87f3af3Sperrin 			if (err == ECKSUM)
306be6fd75aSMatthew Ahrens 				err = SET_ERROR(EIO);
307ea8dc4b6Seschrock 			return (err);
308ea8dc4b6Seschrock 		}
30914843421SMatthew Ahrens 
31014843421SMatthew Ahrens 		/* Increase the blocksize if we are permitted. */
31114843421SMatthew Ahrens 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
312503ad85cSMatthew Ahrens 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
31314843421SMatthew Ahrens 			arc_buf_t *buf = arc_buf_alloc(spa,
314503ad85cSMatthew Ahrens 			    sizeof (objset_phys_t), &os->os_phys_buf,
31514843421SMatthew Ahrens 			    ARC_BUFC_METADATA);
31614843421SMatthew Ahrens 			bzero(buf->b_data, sizeof (objset_phys_t));
317503ad85cSMatthew Ahrens 			bcopy(os->os_phys_buf->b_data, buf->b_data,
318503ad85cSMatthew Ahrens 			    arc_buf_size(os->os_phys_buf));
319503ad85cSMatthew Ahrens 			(void) arc_buf_remove_ref(os->os_phys_buf,
320503ad85cSMatthew Ahrens 			    &os->os_phys_buf);
321503ad85cSMatthew Ahrens 			os->os_phys_buf = buf;
32214843421SMatthew Ahrens 		}
32314843421SMatthew Ahrens 
324503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
325503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
326fa9e4066Sahrens 	} else {
32714843421SMatthew Ahrens 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
32814843421SMatthew Ahrens 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
329503ad85cSMatthew Ahrens 		os->os_phys_buf = arc_buf_alloc(spa, size,
330503ad85cSMatthew Ahrens 		    &os->os_phys_buf, ARC_BUFC_METADATA);
331503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
332503ad85cSMatthew Ahrens 		bzero(os->os_phys, size);
333fa9e4066Sahrens 	}
334fa9e4066Sahrens 
335fa9e4066Sahrens 	/*
336fa9e4066Sahrens 	 * Note: the changed_cb will be called once before the register
337fa9e4066Sahrens 	 * func returns, thus changing the checksum/compression from the
3383baa08fcSek 	 * default (fletcher2/off).  Snapshots don't need to know about
3393baa08fcSek 	 * checksum/compression/copies.
340fa9e4066Sahrens 	 */
341*5d7b4d43SMatthew Ahrens 	if (ds != NULL) {
3423b2aab18SMatthew Ahrens 		err = dsl_prop_register(ds,
3433b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
344503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
3453b2aab18SMatthew Ahrens 		if (err == 0) {
3463b2aab18SMatthew Ahrens 			err = dsl_prop_register(ds,
3473b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
348503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
3493b2aab18SMatthew Ahrens 		}
3503baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
3513b2aab18SMatthew Ahrens 			if (err == 0) {
3523b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3533b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
354503ad85cSMatthew Ahrens 				    checksum_changed_cb, os);
3553b2aab18SMatthew Ahrens 			}
3563b2aab18SMatthew Ahrens 			if (err == 0) {
3573b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3583b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
359503ad85cSMatthew Ahrens 				    compression_changed_cb, os);
3603b2aab18SMatthew Ahrens 			}
3613b2aab18SMatthew Ahrens 			if (err == 0) {
3623b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3633b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COPIES),
364503ad85cSMatthew Ahrens 				    copies_changed_cb, os);
3653b2aab18SMatthew Ahrens 			}
3663b2aab18SMatthew Ahrens 			if (err == 0) {
3673b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3683b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_DEDUP),
369b24ab676SJeff Bonwick 				    dedup_changed_cb, os);
3703b2aab18SMatthew Ahrens 			}
3713b2aab18SMatthew Ahrens 			if (err == 0) {
3723b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3733b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
374e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
3753b2aab18SMatthew Ahrens 			}
3763b2aab18SMatthew Ahrens 			if (err == 0) {
3773b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3783b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_SYNC),
37955da60b9SMark J Musante 				    sync_changed_cb, os);
3803b2aab18SMatthew Ahrens 			}
381edf345e6SMatthew Ahrens 			if (err == 0) {
382edf345e6SMatthew Ahrens 				err = dsl_prop_register(ds,
383edf345e6SMatthew Ahrens 				    zfs_prop_to_name(
384edf345e6SMatthew Ahrens 				    ZFS_PROP_REDUNDANT_METADATA),
385edf345e6SMatthew Ahrens 				    redundant_metadata_changed_cb, os);
386edf345e6SMatthew Ahrens 			}
3873baa08fcSek 		}
3883b2aab18SMatthew Ahrens 		if (err != 0) {
389503ad85cSMatthew Ahrens 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
3903b2aab18SMatthew Ahrens 			    &os->os_phys_buf));
391503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
392ea8dc4b6Seschrock 			return (err);
393ea8dc4b6Seschrock 		}
394*5d7b4d43SMatthew Ahrens 	} else {
395fa9e4066Sahrens 		/* It's the meta-objset. */
396503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
397503ad85cSMatthew Ahrens 		os->os_compress = ZIO_COMPRESS_LZJB;
398503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
399b24ab676SJeff Bonwick 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
400edf345e6SMatthew Ahrens 		os->os_dedup_verify = B_FALSE;
401edf345e6SMatthew Ahrens 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
402edf345e6SMatthew Ahrens 		os->os_sync = ZFS_SYNC_STANDARD;
403503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
404503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
405fa9e4066Sahrens 	}
406fa9e4066Sahrens 
4076e0cbcaaSMatthew Ahrens 	if (ds == NULL || !dsl_dataset_is_snapshot(ds))
4086e0cbcaaSMatthew Ahrens 		os->os_zil_header = os->os_phys->os_zil_header;
409503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
410fa9e4066Sahrens 
411fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
412503ad85cSMatthew Ahrens 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
413fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
414503ad85cSMatthew Ahrens 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
415fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
416fa9e4066Sahrens 	}
417503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
418fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
419503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
420fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
421fa9e4066Sahrens 
422503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
423503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
424503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
425503ad85cSMatthew Ahrens 
426744947dcSTom Erickson 	DMU_META_DNODE(os) = dnode_special_open(os,
427744947dcSTom Erickson 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
428744947dcSTom Erickson 	    &os->os_meta_dnode);
429503ad85cSMatthew Ahrens 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
430744947dcSTom Erickson 		DMU_USERUSED_DNODE(os) = dnode_special_open(os,
431744947dcSTom Erickson 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
432744947dcSTom Erickson 		    &os->os_userused_dnode);
433744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
434744947dcSTom Erickson 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
435744947dcSTom Erickson 		    &os->os_groupused_dnode);
43614843421SMatthew Ahrens 	}
437fa9e4066Sahrens 
438503ad85cSMatthew Ahrens 	*osp = os;
439ea8dc4b6Seschrock 	return (0);
440fa9e4066Sahrens }
441fa9e4066Sahrens 
442503ad85cSMatthew Ahrens int
443503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4443cb34c60Sahrens {
445503ad85cSMatthew Ahrens 	int err = 0;
4463cb34c60Sahrens 
4473cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
448*5d7b4d43SMatthew Ahrens 	if (ds->ds_objset == NULL) {
449*5d7b4d43SMatthew Ahrens 		objset_t *os;
4503cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
451*5d7b4d43SMatthew Ahrens 		    ds, dsl_dataset_get_blkptr(ds), &os);
452*5d7b4d43SMatthew Ahrens 
453*5d7b4d43SMatthew Ahrens 		if (err == 0) {
454*5d7b4d43SMatthew Ahrens 			mutex_enter(&ds->ds_lock);
455*5d7b4d43SMatthew Ahrens 			ASSERT(ds->ds_objset == NULL);
456*5d7b4d43SMatthew Ahrens 			ds->ds_objset = os;
457*5d7b4d43SMatthew Ahrens 			mutex_exit(&ds->ds_lock);
458*5d7b4d43SMatthew Ahrens 		}
4593cb34c60Sahrens 	}
460*5d7b4d43SMatthew Ahrens 	*osp = ds->ds_objset;
4613cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
462503ad85cSMatthew Ahrens 	return (err);
4633cb34c60Sahrens }
4643cb34c60Sahrens 
4653b2aab18SMatthew Ahrens /*
4663b2aab18SMatthew Ahrens  * Holds the pool while the objset is held.  Therefore only one objset
4673b2aab18SMatthew Ahrens  * can be held at a time.
4683b2aab18SMatthew Ahrens  */
4693cb34c60Sahrens int
470503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
4713cb34c60Sahrens {
4723b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
473503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
4743cb34c60Sahrens 	int err;
4753cb34c60Sahrens 
4763b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, tag, &dp);
4773b2aab18SMatthew Ahrens 	if (err != 0)
4783b2aab18SMatthew Ahrens 		return (err);
4793b2aab18SMatthew Ahrens 	err = dsl_dataset_hold(dp, name, tag, &ds);
4803b2aab18SMatthew Ahrens 	if (err != 0) {
4813b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
482503ad85cSMatthew Ahrens 		return (err);
4833b2aab18SMatthew Ahrens 	}
484503ad85cSMatthew Ahrens 
485503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
4863b2aab18SMatthew Ahrens 	if (err != 0) {
487503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
4883b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
4893b2aab18SMatthew Ahrens 	}
490503ad85cSMatthew Ahrens 
4913cb34c60Sahrens 	return (err);
4923cb34c60Sahrens }
4933cb34c60Sahrens 
4943b2aab18SMatthew Ahrens /*
4953b2aab18SMatthew Ahrens  * dsl_pool must not be held when this is called.
4963b2aab18SMatthew Ahrens  * Upon successful return, there will be a longhold on the dataset,
4973b2aab18SMatthew Ahrens  * and the dsl_pool will not be held.
4983b2aab18SMatthew Ahrens  */
499fa9e4066Sahrens int
500503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
501503ad85cSMatthew Ahrens     boolean_t readonly, void *tag, objset_t **osp)
502fa9e4066Sahrens {
5033b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
504f18faf3fSek 	dsl_dataset_t *ds;
505f18faf3fSek 	int err;
506fa9e4066Sahrens 
5073b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
5083b2aab18SMatthew Ahrens 	if (err != 0)
5093b2aab18SMatthew Ahrens 		return (err);
5103b2aab18SMatthew Ahrens 	err = dsl_dataset_own(dp, name, tag, &ds);
5113b2aab18SMatthew Ahrens 	if (err != 0) {
5123b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
513fa9e4066Sahrens 		return (err);
5143b2aab18SMatthew Ahrens 	}
515fa9e4066Sahrens 
516503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
5173b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
5183b2aab18SMatthew Ahrens 	if (err != 0) {
519503ad85cSMatthew Ahrens 		dsl_dataset_disown(ds, tag);
520681d9761SEric Taylor 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
5213b2aab18SMatthew Ahrens 		dsl_dataset_disown(ds, tag);
522be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
523681d9761SEric Taylor 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
5243b2aab18SMatthew Ahrens 		dsl_dataset_disown(ds, tag);
525be6fd75aSMatthew Ahrens 		return (SET_ERROR(EROFS));
526fa9e4066Sahrens 	}
5273cb34c60Sahrens 	return (err);
528fa9e4066Sahrens }
529fa9e4066Sahrens 
530fa9e4066Sahrens void
531503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
532fa9e4066Sahrens {
5333b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(os);
534503ad85cSMatthew Ahrens 	dsl_dataset_rele(os->os_dsl_dataset, tag);
5353b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, tag);
536503ad85cSMatthew Ahrens }
537503ad85cSMatthew Ahrens 
53891948b51SKeith M Wesolowski /*
53991948b51SKeith M Wesolowski  * When we are called, os MUST refer to an objset associated with a dataset
54091948b51SKeith M Wesolowski  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
54191948b51SKeith M Wesolowski  * == tag.  We will then release and reacquire ownership of the dataset while
54291948b51SKeith M Wesolowski  * holding the pool config_rwlock to avoid intervening namespace or ownership
54391948b51SKeith M Wesolowski  * changes may occur.
54491948b51SKeith M Wesolowski  *
54591948b51SKeith M Wesolowski  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
54691948b51SKeith M Wesolowski  * release the hold on its dataset and acquire a new one on the dataset of the
54791948b51SKeith M Wesolowski  * same name so that it can be partially torn down and reconstructed.
54891948b51SKeith M Wesolowski  */
54991948b51SKeith M Wesolowski void
55091948b51SKeith M Wesolowski dmu_objset_refresh_ownership(objset_t *os, void *tag)
55191948b51SKeith M Wesolowski {
55291948b51SKeith M Wesolowski 	dsl_pool_t *dp;
55391948b51SKeith M Wesolowski 	dsl_dataset_t *ds, *newds;
55491948b51SKeith M Wesolowski 	char name[MAXNAMELEN];
55591948b51SKeith M Wesolowski 
55691948b51SKeith M Wesolowski 	ds = os->os_dsl_dataset;
55791948b51SKeith M Wesolowski 	VERIFY3P(ds, !=, NULL);
55891948b51SKeith M Wesolowski 	VERIFY3P(ds->ds_owner, ==, tag);
55991948b51SKeith M Wesolowski 	VERIFY(dsl_dataset_long_held(ds));
56091948b51SKeith M Wesolowski 
56191948b51SKeith M Wesolowski 	dsl_dataset_name(ds, name);
56291948b51SKeith M Wesolowski 	dp = dmu_objset_pool(os);
56391948b51SKeith M Wesolowski 	dsl_pool_config_enter(dp, FTAG);
56491948b51SKeith M Wesolowski 	dmu_objset_disown(os, tag);
56591948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
56691948b51SKeith M Wesolowski 	VERIFY3P(newds, ==, os->os_dsl_dataset);
56791948b51SKeith M Wesolowski 	dsl_pool_config_exit(dp, FTAG);
56891948b51SKeith M Wesolowski }
56991948b51SKeith M Wesolowski 
570503ad85cSMatthew Ahrens void
571503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
572503ad85cSMatthew Ahrens {
573503ad85cSMatthew Ahrens 	dsl_dataset_disown(os->os_dsl_dataset, tag);
574fa9e4066Sahrens }
575fa9e4066Sahrens 
5763b2aab18SMatthew Ahrens void
5771934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
578ea8dc4b6Seschrock {
579ea8dc4b6Seschrock 	dnode_t *dn;
580c543ec06Sahrens 
581503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
582c543ec06Sahrens 
583c543ec06Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
584744947dcSTom Erickson 	list_remove(&os->os_dnodes, DMU_META_DNODE(os));
585744947dcSTom Erickson 	list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
586ea8dc4b6Seschrock 
587ea8dc4b6Seschrock 	/*
588c543ec06Sahrens 	 * Find the first dnode with holds.  We have to do this dance
589c543ec06Sahrens 	 * because dnode_add_ref() only works if you already have a
590c543ec06Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
591c543ec06Sahrens 	 * skip.
592ea8dc4b6Seschrock 	 */
593503ad85cSMatthew Ahrens 	for (dn = list_head(&os->os_dnodes);
5941934e92fSmaybee 	    dn && !dnode_add_ref(dn, FTAG);
595503ad85cSMatthew Ahrens 	    dn = list_next(&os->os_dnodes, dn))
596c543ec06Sahrens 		continue;
597c543ec06Sahrens 
598c543ec06Sahrens 	while (dn) {
599c543ec06Sahrens 		dnode_t *next_dn = dn;
600c543ec06Sahrens 
601c543ec06Sahrens 		do {
602503ad85cSMatthew Ahrens 			next_dn = list_next(&os->os_dnodes, next_dn);
6031934e92fSmaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
604c543ec06Sahrens 
605503ad85cSMatthew Ahrens 		mutex_exit(&os->os_lock);
6061934e92fSmaybee 		dnode_evict_dbufs(dn);
607c543ec06Sahrens 		dnode_rele(dn, FTAG);
608503ad85cSMatthew Ahrens 		mutex_enter(&os->os_lock);
609c543ec06Sahrens 		dn = next_dn;
610ea8dc4b6Seschrock 	}
611503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
612ea8dc4b6Seschrock }
613ea8dc4b6Seschrock 
614fa9e4066Sahrens void
615503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
616fa9e4066Sahrens {
617503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
618fa9e4066Sahrens 
619b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
620b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, t));
621fa9e4066Sahrens 
6223baa08fcSek 	if (ds) {
6233baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
6243b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6253b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
626503ad85cSMatthew Ahrens 			    checksum_changed_cb, os));
6273b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6283b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
629503ad85cSMatthew Ahrens 			    compression_changed_cb, os));
6303b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6313b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_COPIES),
632503ad85cSMatthew Ahrens 			    copies_changed_cb, os));
6333b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6343b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_DEDUP),
635b24ab676SJeff Bonwick 			    dedup_changed_cb, os));
6363b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6373b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
638e09fa4daSNeil Perrin 			    logbias_changed_cb, os));
6393b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6403b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SYNC),
64155da60b9SMark J Musante 			    sync_changed_cb, os));
642edf345e6SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
643edf345e6SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA),
644edf345e6SMatthew Ahrens 			    redundant_metadata_changed_cb, os));
6453baa08fcSek 		}
6463b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_unregister(ds,
6473b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
648503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os));
6493b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_unregister(ds,
6503b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
651503ad85cSMatthew Ahrens 		    secondary_cache_changed_cb, os));
652fa9e4066Sahrens 	}
653fa9e4066Sahrens 
6540a586ceaSMark Shellenbaum 	if (os->os_sa)
6550a586ceaSMark Shellenbaum 		sa_tear_down(os);
6560a586ceaSMark Shellenbaum 
6573b2aab18SMatthew Ahrens 	dmu_objset_evict_dbufs(os);
658ea8dc4b6Seschrock 
659744947dcSTom Erickson 	dnode_special_close(&os->os_meta_dnode);
660744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os)) {
661744947dcSTom Erickson 		dnode_special_close(&os->os_userused_dnode);
662744947dcSTom Erickson 		dnode_special_close(&os->os_groupused_dnode);
66314843421SMatthew Ahrens 	}
664503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
665fa9e4066Sahrens 
666503ad85cSMatthew Ahrens 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
66714843421SMatthew Ahrens 
6683b2aab18SMatthew Ahrens 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
669744947dcSTom Erickson 
670744947dcSTom Erickson 	/*
671744947dcSTom Erickson 	 * This is a barrier to prevent the objset from going away in
672744947dcSTom Erickson 	 * dnode_move() until we can safely ensure that the objset is still in
673744947dcSTom Erickson 	 * use. We consider the objset valid before the barrier and invalid
674744947dcSTom Erickson 	 * after the barrier.
675744947dcSTom Erickson 	 */
676744947dcSTom Erickson 	rw_enter(&os_lock, RW_READER);
677744947dcSTom Erickson 	rw_exit(&os_lock);
678744947dcSTom Erickson 
679503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
680503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
681503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
682503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
683fa9e4066Sahrens }
684fa9e4066Sahrens 
68571eb0538SChris Kirby timestruc_t
68671eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
68771eb0538SChris Kirby {
68871eb0538SChris Kirby 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
68971eb0538SChris Kirby }
69071eb0538SChris Kirby 
691fa9e4066Sahrens /* called from dsl for meta-objset */
692503ad85cSMatthew Ahrens objset_t *
693c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
694c717a561Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
695fa9e4066Sahrens {
696503ad85cSMatthew Ahrens 	objset_t *os;
697fa9e4066Sahrens 	dnode_t *mdn;
698fa9e4066Sahrens 
699fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7003b2aab18SMatthew Ahrens 
701feaa74e4SMark Maybee 	if (ds != NULL)
7023b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_from_ds(ds, &os));
703feaa74e4SMark Maybee 	else
7043b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
705feaa74e4SMark Maybee 
706744947dcSTom Erickson 	mdn = DMU_META_DNODE(os);
707fa9e4066Sahrens 
708fa9e4066Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
709fa9e4066Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
710fa9e4066Sahrens 
711fa9e4066Sahrens 	/*
712fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
713fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
714fa9e4066Sahrens 	 * we are also accessing it in open context.
715fa9e4066Sahrens 	 *
716fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
717fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
718fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
719fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
720fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
721fa9e4066Sahrens 	 */
722ea8dc4b6Seschrock 	if (ds != NULL) {
723ea8dc4b6Seschrock 		int levels = 1;
724ea8dc4b6Seschrock 
725ea8dc4b6Seschrock 		/*
726ea8dc4b6Seschrock 		 * Determine the number of levels necessary for the meta-dnode
727ea8dc4b6Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
728ea8dc4b6Seschrock 		 */
729ea8dc4b6Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
730ea8dc4b6Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
731ea8dc4b6Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
732ea8dc4b6Seschrock 			levels++;
733ea8dc4b6Seschrock 
734fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
735ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
736ea8dc4b6Seschrock 	}
737fa9e4066Sahrens 
738fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
739fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
740fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
741503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
742503ad85cSMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
743503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
744503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
74514843421SMatthew Ahrens 	}
746fa9e4066Sahrens 
747fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
748fa9e4066Sahrens 
749503ad85cSMatthew Ahrens 	return (os);
750fa9e4066Sahrens }
751fa9e4066Sahrens 
7523b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg {
7533b2aab18SMatthew Ahrens 	const char *doca_name;
7543b2aab18SMatthew Ahrens 	cred_t *doca_cred;
7553b2aab18SMatthew Ahrens 	void (*doca_userfunc)(objset_t *os, void *arg,
7563b2aab18SMatthew Ahrens 	    cred_t *cr, dmu_tx_t *tx);
7573b2aab18SMatthew Ahrens 	void *doca_userarg;
7583b2aab18SMatthew Ahrens 	dmu_objset_type_t doca_type;
7593b2aab18SMatthew Ahrens 	uint64_t doca_flags;
7603b2aab18SMatthew Ahrens } dmu_objset_create_arg_t;
761fa9e4066Sahrens 
762ecd6cf80Smarks /*ARGSUSED*/
763fa9e4066Sahrens static int
7643b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx)
765fa9e4066Sahrens {
7663b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
7673b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
7683b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
7693b2aab18SMatthew Ahrens 	const char *tail;
7703b2aab18SMatthew Ahrens 	int error;
7711d452cf5Sahrens 
7723b2aab18SMatthew Ahrens 	if (strchr(doca->doca_name, '@') != NULL)
773be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
7741d452cf5Sahrens 
7753b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
7763b2aab18SMatthew Ahrens 	if (error != 0)
7773b2aab18SMatthew Ahrens 		return (error);
7783b2aab18SMatthew Ahrens 	if (tail == NULL) {
7793b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
780be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
7811d452cf5Sahrens 	}
782a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
783a2afb611SJerry Jelinek 	    doca->doca_cred);
7843b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
785ecd6cf80Smarks 
786a2afb611SJerry Jelinek 	return (error);
7871d452cf5Sahrens }
7881d452cf5Sahrens 
7891d452cf5Sahrens static void
7903b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
7911d452cf5Sahrens {
7923b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
7933b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
7943b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
7953b2aab18SMatthew Ahrens 	const char *tail;
7964445fffbSMatthew Ahrens 	dsl_dataset_t *ds;
7973b2aab18SMatthew Ahrens 	uint64_t obj;
7984445fffbSMatthew Ahrens 	blkptr_t *bp;
7993b2aab18SMatthew Ahrens 	objset_t *os;
800fa9e4066Sahrens 
8013b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
802fa9e4066Sahrens 
8033b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
8043b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
805fa9e4066Sahrens 
8063b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
8074445fffbSMatthew Ahrens 	bp = dsl_dataset_get_blkptr(ds);
8083b2aab18SMatthew Ahrens 	os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
8093b2aab18SMatthew Ahrens 	    ds, bp, doca->doca_type, tx);
810fa9e4066Sahrens 
8113b2aab18SMatthew Ahrens 	if (doca->doca_userfunc != NULL) {
8123b2aab18SMatthew Ahrens 		doca->doca_userfunc(os, doca->doca_userarg,
8133b2aab18SMatthew Ahrens 		    doca->doca_cred, tx);
814fa9e4066Sahrens 	}
815ecd6cf80Smarks 
8163b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "create", tx, "");
8174445fffbSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
8183b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
819fa9e4066Sahrens }
820fa9e4066Sahrens 
821fa9e4066Sahrens int
822ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
823ecd6cf80Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
824fa9e4066Sahrens {
8253b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t doca;
826fa9e4066Sahrens 
8273b2aab18SMatthew Ahrens 	doca.doca_name = name;
8283b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
8293b2aab18SMatthew Ahrens 	doca.doca_flags = flags;
8303b2aab18SMatthew Ahrens 	doca.doca_userfunc = func;
8313b2aab18SMatthew Ahrens 	doca.doca_userarg = arg;
8323b2aab18SMatthew Ahrens 	doca.doca_type = type;
833ecd6cf80Smarks 
8343b2aab18SMatthew Ahrens 	return (dsl_sync_task(name,
8353b2aab18SMatthew Ahrens 	    dmu_objset_create_check, dmu_objset_create_sync, &doca, 5));
836ae46e4c7SMatthew Ahrens }
837ae46e4c7SMatthew Ahrens 
8383b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg {
8393b2aab18SMatthew Ahrens 	const char *doca_clone;
8403b2aab18SMatthew Ahrens 	const char *doca_origin;
8413b2aab18SMatthew Ahrens 	cred_t *doca_cred;
8423b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t;
8433b2aab18SMatthew Ahrens 
8443b2aab18SMatthew Ahrens /*ARGSUSED*/
8453b2aab18SMatthew Ahrens static int
8463b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
847ae46e4c7SMatthew Ahrens {
8483b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
849ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
850ae46e4c7SMatthew Ahrens 	const char *tail;
8513b2aab18SMatthew Ahrens 	int error;
8523b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
8533b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
854ae46e4c7SMatthew Ahrens 
8553b2aab18SMatthew Ahrens 	if (strchr(doca->doca_clone, '@') != NULL)
856be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8573b2aab18SMatthew Ahrens 
8583b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
8593b2aab18SMatthew Ahrens 	if (error != 0)
8603b2aab18SMatthew Ahrens 		return (error);
861ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
8623b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
863be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
864fa9e4066Sahrens 	}
8653b2aab18SMatthew Ahrens 	/* You can't clone across pools. */
8663b2aab18SMatthew Ahrens 	if (pdd->dd_pool != dp) {
8673b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
868be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
869fa9e4066Sahrens 	}
870a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
871a2afb611SJerry Jelinek 	    doca->doca_cred);
872a2afb611SJerry Jelinek 	if (error != 0) {
873a2afb611SJerry Jelinek 		dsl_dir_rele(pdd, FTAG);
874a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
875a2afb611SJerry Jelinek 	}
8763b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
877fa9e4066Sahrens 
8783b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
8793b2aab18SMatthew Ahrens 	if (error != 0)
88099d5e173STim Haley 		return (error);
88199d5e173STim Haley 
8823b2aab18SMatthew Ahrens 	/* You can't clone across pools. */
8833b2aab18SMatthew Ahrens 	if (origin->ds_dir->dd_pool != dp) {
8843b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
885be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
88699d5e173STim Haley 	}
887ea2f5b9eSMatthew Ahrens 
8883b2aab18SMatthew Ahrens 	/* You can only clone snapshots, not the head datasets. */
8893b2aab18SMatthew Ahrens 	if (!dsl_dataset_is_snapshot(origin)) {
8903b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
891be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
89292241e0bSTom Erickson 	}
8933b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
89499d5e173STim Haley 
8953b2aab18SMatthew Ahrens 	return (0);
896ea2f5b9eSMatthew Ahrens }
8971d452cf5Sahrens 
8983b2aab18SMatthew Ahrens static void
8993b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
9001d452cf5Sahrens {
9013b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
9023b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
9033b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
9043b2aab18SMatthew Ahrens 	const char *tail;
9053b2aab18SMatthew Ahrens 	dsl_dataset_t *origin, *ds;
9063b2aab18SMatthew Ahrens 	uint64_t obj;
9073b2aab18SMatthew Ahrens 	char namebuf[MAXNAMELEN];
9084445fffbSMatthew Ahrens 
9093b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
9103b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
9114445fffbSMatthew Ahrens 
9123b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
9133b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
914f2e10be3Srm 
9153b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
9163b2aab18SMatthew Ahrens 	dsl_dataset_name(origin, namebuf);
9173b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "clone", tx,
9183b2aab18SMatthew Ahrens 	    "origin=%s (%llu)", namebuf, origin->ds_object);
9193b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
9203b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
9213b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
9221d452cf5Sahrens }
9231d452cf5Sahrens 
9241d452cf5Sahrens int
9253b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin)
9261d452cf5Sahrens {
9273b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t doca;
9284445fffbSMatthew Ahrens 
9293b2aab18SMatthew Ahrens 	doca.doca_clone = clone;
9303b2aab18SMatthew Ahrens 	doca.doca_origin = origin;
9313b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
93299d5e173STim Haley 
9333b2aab18SMatthew Ahrens 	return (dsl_sync_task(clone,
9343b2aab18SMatthew Ahrens 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca, 5));
9354445fffbSMatthew Ahrens }
9364445fffbSMatthew Ahrens 
9374445fffbSMatthew Ahrens int
9384445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname)
9394445fffbSMatthew Ahrens {
9404445fffbSMatthew Ahrens 	int err;
9414445fffbSMatthew Ahrens 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
9424445fffbSMatthew Ahrens 	nvlist_t *snaps = fnvlist_alloc();
9434445fffbSMatthew Ahrens 
9444445fffbSMatthew Ahrens 	fnvlist_add_boolean(snaps, longsnap);
9454445fffbSMatthew Ahrens 	strfree(longsnap);
9463b2aab18SMatthew Ahrens 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
9473b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
9484445fffbSMatthew Ahrens 	return (err);
9494445fffbSMatthew Ahrens }
9504445fffbSMatthew Ahrens 
951fa9e4066Sahrens static void
95214843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
953fa9e4066Sahrens {
954c717a561Smaybee 	dnode_t *dn;
955faafa6e3Sahrens 
956c717a561Smaybee 	while (dn = list_head(list)) {
957c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
958c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
959c717a561Smaybee 		/*
96014843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
96114843421SMatthew Ahrens 		 * meta-dnode needs to set it ouside dnode_sync().
962c717a561Smaybee 		 */
963c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
964c717a561Smaybee 		ASSERT(dn->dn_zio);
965faafa6e3Sahrens 
966c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
967c717a561Smaybee 		list_remove(list, dn);
96814843421SMatthew Ahrens 
96914843421SMatthew Ahrens 		if (newlist) {
97014843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
97114843421SMatthew Ahrens 			list_insert_tail(newlist, dn);
97214843421SMatthew Ahrens 		}
97314843421SMatthew Ahrens 
974c717a561Smaybee 		dnode_sync(dn, tx);
975fa9e4066Sahrens 	}
976fa9e4066Sahrens }
977fa9e4066Sahrens 
978fa9e4066Sahrens /* ARGSUSED */
979fa9e4066Sahrens static void
980b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
981fa9e4066Sahrens {
982e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
983503ad85cSMatthew Ahrens 	objset_t *os = arg;
984c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
985fa9e4066Sahrens 
986*5d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp));
9873b2aab18SMatthew Ahrens 	ASSERT3P(bp, ==, os->os_rootbp);
9883b2aab18SMatthew Ahrens 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
9893b2aab18SMatthew Ahrens 	ASSERT0(BP_GET_LEVEL(bp));
9900a4e9518Sgw 
991fa9e4066Sahrens 	/*
99214843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
99314843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
99414843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
99514843421SMatthew Ahrens 	 * dnode and user/group accounting objects).
996fa9e4066Sahrens 	 */
99714843421SMatthew Ahrens 	bp->blk_fill = 0;
998e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
999*5d7b4d43SMatthew Ahrens 		bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1000b24ab676SJeff Bonwick }
1001b24ab676SJeff Bonwick 
1002b24ab676SJeff Bonwick /* ARGSUSED */
1003b24ab676SJeff Bonwick static void
1004b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1005b24ab676SJeff Bonwick {
1006b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1007b24ab676SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
1008b24ab676SJeff Bonwick 	objset_t *os = arg;
10090a4e9518Sgw 
1010e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1011b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, bp_orig));
1012e14bb325SJeff Bonwick 	} else {
1013b24ab676SJeff Bonwick 		dsl_dataset_t *ds = os->os_dsl_dataset;
1014b24ab676SJeff Bonwick 		dmu_tx_t *tx = os->os_synctx;
1015b24ab676SJeff Bonwick 
1016b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1017b24ab676SJeff Bonwick 		dsl_dataset_block_born(ds, bp, tx);
10180a4e9518Sgw 	}
1019c717a561Smaybee }
1020c717a561Smaybee 
1021fa9e4066Sahrens /* called from dsl */
1022fa9e4066Sahrens void
1023503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1024fa9e4066Sahrens {
1025fa9e4066Sahrens 	int txgoff;
1026ea8dc4b6Seschrock 	zbookmark_t zb;
1027b24ab676SJeff Bonwick 	zio_prop_t zp;
1028c717a561Smaybee 	zio_t *zio;
1029c717a561Smaybee 	list_t *list;
103014843421SMatthew Ahrens 	list_t *newlist = NULL;
1031c717a561Smaybee 	dbuf_dirty_record_t *dr;
1032c717a561Smaybee 
1033c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1034fa9e4066Sahrens 
1035fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1036fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
1037fa9e4066Sahrens 	os->os_synctx = tx;
1038fa9e4066Sahrens 
103987bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
104087bd5c1eSahrens 		/*
104187bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
104287bd5c1eSahrens 		 * spa_max_replication() could change, so reset
104387bd5c1eSahrens 		 * os_copies here.
104487bd5c1eSahrens 		 */
104587bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
104687bd5c1eSahrens 	}
104787bd5c1eSahrens 
1048fa9e4066Sahrens 	/*
1049c717a561Smaybee 	 * Create the root block IO
1050fa9e4066Sahrens 	 */
1051b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1052b24ab676SJeff Bonwick 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1053b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
10541b912ec7SGeorge Wilson 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1055b24ab676SJeff Bonwick 
1056b24ab676SJeff Bonwick 	dmu_write_policy(os, NULL, 0, 0, &zp);
1057b24ab676SJeff Bonwick 
1058b24ab676SJeff Bonwick 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1059aad02571SSaso Kiselkov 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1060aad02571SSaso Kiselkov 	    DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
106169962b56SMatthew Ahrens 	    NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1062aad02571SSaso Kiselkov 	    ZIO_FLAG_MUSTSUCCEED, &zb);
1063c717a561Smaybee 
1064c717a561Smaybee 	/*
106514843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
1066c717a561Smaybee 	 */
1067744947dcSTom Erickson 	DMU_META_DNODE(os)->dn_zio = zio;
1068744947dcSTom Erickson 	dnode_sync(DMU_META_DNODE(os), tx);
1069c717a561Smaybee 
107014843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
107114843421SMatthew Ahrens 
1072744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) &&
1073744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1074744947dcSTom Erickson 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1075744947dcSTom Erickson 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1076744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1077744947dcSTom Erickson 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
107814843421SMatthew Ahrens 	}
107914843421SMatthew Ahrens 
1080c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
1081fa9e4066Sahrens 
108214843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
108314843421SMatthew Ahrens 		newlist = &os->os_synced_dnodes;
108414843421SMatthew Ahrens 		/*
108514843421SMatthew Ahrens 		 * We must create the list here because it uses the
108614843421SMatthew Ahrens 		 * dn_dirty_link[] of this txg.
108714843421SMatthew Ahrens 		 */
108814843421SMatthew Ahrens 		list_create(newlist, sizeof (dnode_t),
108914843421SMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
109014843421SMatthew Ahrens 	}
109114843421SMatthew Ahrens 
109214843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
109314843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1094fa9e4066Sahrens 
1095744947dcSTom Erickson 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1096c717a561Smaybee 	while (dr = list_head(list)) {
10973b2aab18SMatthew Ahrens 		ASSERT0(dr->dr_dbuf->db_level);
1098c717a561Smaybee 		list_remove(list, dr);
1099c717a561Smaybee 		if (dr->dr_zio)
1100c717a561Smaybee 			zio_nowait(dr->dr_zio);
1101c717a561Smaybee 	}
1102c717a561Smaybee 	/*
1103c717a561Smaybee 	 * Free intent log blocks up to this tx.
1104c717a561Smaybee 	 */
1105c717a561Smaybee 	zil_sync(os->os_zil, tx);
1106088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
1107c717a561Smaybee 	zio_nowait(zio);
1108fa9e4066Sahrens }
1109fa9e4066Sahrens 
1110b24ab676SJeff Bonwick boolean_t
1111b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1112b24ab676SJeff Bonwick {
1113b24ab676SJeff Bonwick 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1114b24ab676SJeff Bonwick 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1115b24ab676SJeff Bonwick }
1116b24ab676SJeff Bonwick 
1117744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
111814843421SMatthew Ahrens 
111914843421SMatthew Ahrens void
112014843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
112114843421SMatthew Ahrens {
112214843421SMatthew Ahrens 	used_cbs[ost] = cb;
112314843421SMatthew Ahrens }
112414843421SMatthew Ahrens 
112514843421SMatthew Ahrens boolean_t
1126503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
112714843421SMatthew Ahrens {
112814843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1129744947dcSTom Erickson 	    used_cbs[os->os_phys->os_type] != NULL &&
1130744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os) != NULL);
113114843421SMatthew Ahrens }
113214843421SMatthew Ahrens 
11339966ca11SMatthew Ahrens static void
11340a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
11350a586ceaSMark Shellenbaum     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
11369966ca11SMatthew Ahrens {
11370a586ceaSMark Shellenbaum 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
11380a586ceaSMark Shellenbaum 		int64_t delta = DNODE_SIZE + used;
11399966ca11SMatthew Ahrens 		if (subtract)
11409966ca11SMatthew Ahrens 			delta = -delta;
1141b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
11429966ca11SMatthew Ahrens 		    user, delta, tx));
1143b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
11449966ca11SMatthew Ahrens 		    group, delta, tx));
11459966ca11SMatthew Ahrens 	}
11469966ca11SMatthew Ahrens }
11479966ca11SMatthew Ahrens 
114814843421SMatthew Ahrens void
11490a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
115014843421SMatthew Ahrens {
115114843421SMatthew Ahrens 	dnode_t *dn;
115214843421SMatthew Ahrens 	list_t *list = &os->os_synced_dnodes;
115314843421SMatthew Ahrens 
115414843421SMatthew Ahrens 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
115514843421SMatthew Ahrens 
115614843421SMatthew Ahrens 	while (dn = list_head(list)) {
11571d8ccc7bSMark Shellenbaum 		int flags;
115814843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
115914843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
116014843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
116114843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
116214843421SMatthew Ahrens 
116314843421SMatthew Ahrens 		/* Allocate the user/groupused objects if necessary. */
1164744947dcSTom Erickson 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1165503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
116614843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT,
116714843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1168503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
116914843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT,
117014843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
117114843421SMatthew Ahrens 		}
117214843421SMatthew Ahrens 
117314843421SMatthew Ahrens 		/*
11749966ca11SMatthew Ahrens 		 * We intentionally modify the zap object even if the
11750a586ceaSMark Shellenbaum 		 * net delta is zero.  Otherwise
11769966ca11SMatthew Ahrens 		 * the block of the zap obj could be shared between
11779966ca11SMatthew Ahrens 		 * datasets but need to be different between them after
11789966ca11SMatthew Ahrens 		 * a bprewrite.
117914843421SMatthew Ahrens 		 */
118014843421SMatthew Ahrens 
11811d8ccc7bSMark Shellenbaum 		flags = dn->dn_id_flags;
11821d8ccc7bSMark Shellenbaum 		ASSERT(flags);
11831d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST)  {
11840a586ceaSMark Shellenbaum 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
11850a586ceaSMark Shellenbaum 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
11860a586ceaSMark Shellenbaum 		}
11871d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_NEW_EXIST) {
11880a586ceaSMark Shellenbaum 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
11890a586ceaSMark Shellenbaum 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
11900a586ceaSMark Shellenbaum 			    dn->dn_newgid, B_FALSE, tx);
11910a586ceaSMark Shellenbaum 		}
11920a586ceaSMark Shellenbaum 
11931d8ccc7bSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
11940a586ceaSMark Shellenbaum 		dn->dn_oldused = 0;
11950a586ceaSMark Shellenbaum 		dn->dn_oldflags = 0;
11960a586ceaSMark Shellenbaum 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
11970a586ceaSMark Shellenbaum 			dn->dn_olduid = dn->dn_newuid;
11980a586ceaSMark Shellenbaum 			dn->dn_oldgid = dn->dn_newgid;
11990a586ceaSMark Shellenbaum 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
12000a586ceaSMark Shellenbaum 			if (dn->dn_bonuslen == 0)
12010a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
12020a586ceaSMark Shellenbaum 			else
12030a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
12040a586ceaSMark Shellenbaum 		}
120528d97a71SMark Shellenbaum 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
120614843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
120714843421SMatthew Ahrens 
120814843421SMatthew Ahrens 		list_remove(list, dn);
120914843421SMatthew Ahrens 		dnode_rele(dn, list);
121014843421SMatthew Ahrens 	}
121114843421SMatthew Ahrens }
121214843421SMatthew Ahrens 
121306e0070dSMark Shellenbaum /*
121406e0070dSMark Shellenbaum  * Returns a pointer to data to find uid/gid from
121506e0070dSMark Shellenbaum  *
121606e0070dSMark Shellenbaum  * If a dirty record for transaction group that is syncing can't
121706e0070dSMark Shellenbaum  * be found then NULL is returned.  In the NULL case it is assumed
121806e0070dSMark Shellenbaum  * the uid/gid aren't changing.
121906e0070dSMark Shellenbaum  */
122006e0070dSMark Shellenbaum static void *
122106e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
122206e0070dSMark Shellenbaum {
122306e0070dSMark Shellenbaum 	dbuf_dirty_record_t *dr, **drp;
122406e0070dSMark Shellenbaum 	void *data;
122506e0070dSMark Shellenbaum 
122606e0070dSMark Shellenbaum 	if (db->db_dirtycnt == 0)
122706e0070dSMark Shellenbaum 		return (db->db.db_data);  /* Nothing is changing */
122806e0070dSMark Shellenbaum 
122906e0070dSMark Shellenbaum 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
123006e0070dSMark Shellenbaum 		if (dr->dr_txg == tx->tx_txg)
123106e0070dSMark Shellenbaum 			break;
123206e0070dSMark Shellenbaum 
1233744947dcSTom Erickson 	if (dr == NULL) {
123406e0070dSMark Shellenbaum 		data = NULL;
1235744947dcSTom Erickson 	} else {
1236744947dcSTom Erickson 		dnode_t *dn;
1237744947dcSTom Erickson 
1238744947dcSTom Erickson 		DB_DNODE_ENTER(dr->dr_dbuf);
1239744947dcSTom Erickson 		dn = DB_DNODE(dr->dr_dbuf);
1240744947dcSTom Erickson 
1241744947dcSTom Erickson 		if (dn->dn_bonuslen == 0 &&
1242744947dcSTom Erickson 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1243744947dcSTom Erickson 			data = dr->dt.dl.dr_data->b_data;
1244744947dcSTom Erickson 		else
1245744947dcSTom Erickson 			data = dr->dt.dl.dr_data;
1246744947dcSTom Erickson 
1247744947dcSTom Erickson 		DB_DNODE_EXIT(dr->dr_dbuf);
1248744947dcSTom Erickson 	}
1249744947dcSTom Erickson 
125006e0070dSMark Shellenbaum 	return (data);
125106e0070dSMark Shellenbaum }
125206e0070dSMark Shellenbaum 
12530a586ceaSMark Shellenbaum void
125406e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
12550a586ceaSMark Shellenbaum {
12560a586ceaSMark Shellenbaum 	objset_t *os = dn->dn_objset;
12570a586ceaSMark Shellenbaum 	void *data = NULL;
125806e0070dSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
1259d5285caeSGeorge Wilson 	uint64_t *user = NULL;
1260d5285caeSGeorge Wilson 	uint64_t *group = NULL;
12610a586ceaSMark Shellenbaum 	int flags = dn->dn_id_flags;
12620a586ceaSMark Shellenbaum 	int error;
126306e0070dSMark Shellenbaum 	boolean_t have_spill = B_FALSE;
12640a586ceaSMark Shellenbaum 
12650a586ceaSMark Shellenbaum 	if (!dmu_objset_userused_enabled(dn->dn_objset))
12660a586ceaSMark Shellenbaum 		return;
12670a586ceaSMark Shellenbaum 
12680a586ceaSMark Shellenbaum 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
12690a586ceaSMark Shellenbaum 	    DN_ID_CHKED_SPILL)))
12700a586ceaSMark Shellenbaum 		return;
12710a586ceaSMark Shellenbaum 
12720a586ceaSMark Shellenbaum 	if (before && dn->dn_bonuslen != 0)
12730a586ceaSMark Shellenbaum 		data = DN_BONUS(dn->dn_phys);
127406e0070dSMark Shellenbaum 	else if (!before && dn->dn_bonuslen != 0) {
127506e0070dSMark Shellenbaum 		if (dn->dn_bonus) {
127606e0070dSMark Shellenbaum 			db = dn->dn_bonus;
127706e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
127806e0070dSMark Shellenbaum 			data = dmu_objset_userquota_find_data(db, tx);
127906e0070dSMark Shellenbaum 		} else {
128006e0070dSMark Shellenbaum 			data = DN_BONUS(dn->dn_phys);
128106e0070dSMark Shellenbaum 		}
128206e0070dSMark Shellenbaum 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
12830a586ceaSMark Shellenbaum 			int rf = 0;
12840a586ceaSMark Shellenbaum 
12850a586ceaSMark Shellenbaum 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
12860a586ceaSMark Shellenbaum 				rf |= DB_RF_HAVESTRUCT;
12871d8ccc7bSMark Shellenbaum 			error = dmu_spill_hold_by_dnode(dn,
12881d8ccc7bSMark Shellenbaum 			    rf | DB_RF_MUST_SUCCEED,
128906e0070dSMark Shellenbaum 			    FTAG, (dmu_buf_t **)&db);
12900a586ceaSMark Shellenbaum 			ASSERT(error == 0);
129106e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
129206e0070dSMark Shellenbaum 			data = (before) ? db->db.db_data :
129306e0070dSMark Shellenbaum 			    dmu_objset_userquota_find_data(db, tx);
129406e0070dSMark Shellenbaum 			have_spill = B_TRUE;
12950a586ceaSMark Shellenbaum 	} else {
12960a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
12970a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
12980a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
12990a586ceaSMark Shellenbaum 		return;
13000a586ceaSMark Shellenbaum 	}
13010a586ceaSMark Shellenbaum 
13020a586ceaSMark Shellenbaum 	if (before) {
130306e0070dSMark Shellenbaum 		ASSERT(data);
13040a586ceaSMark Shellenbaum 		user = &dn->dn_olduid;
13050a586ceaSMark Shellenbaum 		group = &dn->dn_oldgid;
130606e0070dSMark Shellenbaum 	} else if (data) {
13070a586ceaSMark Shellenbaum 		user = &dn->dn_newuid;
13080a586ceaSMark Shellenbaum 		group = &dn->dn_newgid;
13090a586ceaSMark Shellenbaum 	}
13100a586ceaSMark Shellenbaum 
131106e0070dSMark Shellenbaum 	/*
131206e0070dSMark Shellenbaum 	 * Must always call the callback in case the object
131306e0070dSMark Shellenbaum 	 * type has changed and that type isn't an object type to track
131406e0070dSMark Shellenbaum 	 */
13150a586ceaSMark Shellenbaum 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
13160a586ceaSMark Shellenbaum 	    user, group);
13170a586ceaSMark Shellenbaum 
131806e0070dSMark Shellenbaum 	/*
131906e0070dSMark Shellenbaum 	 * Preserve existing uid/gid when the callback can't determine
132006e0070dSMark Shellenbaum 	 * what the new uid/gid are and the callback returned EEXIST.
132106e0070dSMark Shellenbaum 	 * The EEXIST error tells us to just use the existing uid/gid.
132206e0070dSMark Shellenbaum 	 * If we don't know what the old values are then just assign
132306e0070dSMark Shellenbaum 	 * them to 0, since that is a new file  being created.
132406e0070dSMark Shellenbaum 	 */
132506e0070dSMark Shellenbaum 	if (!before && data == NULL && error == EEXIST) {
132606e0070dSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST) {
132706e0070dSMark Shellenbaum 			dn->dn_newuid = dn->dn_olduid;
132806e0070dSMark Shellenbaum 			dn->dn_newgid = dn->dn_oldgid;
132906e0070dSMark Shellenbaum 		} else {
133006e0070dSMark Shellenbaum 			dn->dn_newuid = 0;
133106e0070dSMark Shellenbaum 			dn->dn_newgid = 0;
133206e0070dSMark Shellenbaum 		}
133306e0070dSMark Shellenbaum 		error = 0;
133406e0070dSMark Shellenbaum 	}
133506e0070dSMark Shellenbaum 
133606e0070dSMark Shellenbaum 	if (db)
133706e0070dSMark Shellenbaum 		mutex_exit(&db->db_mtx);
133806e0070dSMark Shellenbaum 
13390a586ceaSMark Shellenbaum 	mutex_enter(&dn->dn_mtx);
13400a586ceaSMark Shellenbaum 	if (error == 0 && before)
13410a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
13420a586ceaSMark Shellenbaum 	if (error == 0 && !before)
13430a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
13440a586ceaSMark Shellenbaum 
134506e0070dSMark Shellenbaum 	if (have_spill) {
13460a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
13470a586ceaSMark Shellenbaum 	} else {
13480a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13490a586ceaSMark Shellenbaum 	}
13500a586ceaSMark Shellenbaum 	mutex_exit(&dn->dn_mtx);
135106e0070dSMark Shellenbaum 	if (have_spill)
135206e0070dSMark Shellenbaum 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
13530a586ceaSMark Shellenbaum }
13540a586ceaSMark Shellenbaum 
135514843421SMatthew Ahrens boolean_t
135614843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
135714843421SMatthew Ahrens {
1358503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
135914843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
136014843421SMatthew Ahrens }
136114843421SMatthew Ahrens 
136214843421SMatthew Ahrens int
136314843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
136414843421SMatthew Ahrens {
136514843421SMatthew Ahrens 	uint64_t obj;
136614843421SMatthew Ahrens 	int err = 0;
136714843421SMatthew Ahrens 
136814843421SMatthew Ahrens 	if (dmu_objset_userspace_present(os))
136914843421SMatthew Ahrens 		return (0);
1370503ad85cSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
1371be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
137214843421SMatthew Ahrens 	if (dmu_objset_is_snapshot(os))
1373be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
137414843421SMatthew Ahrens 
137514843421SMatthew Ahrens 	/*
137614843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
137714843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
137814843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
137914843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
138014843421SMatthew Ahrens 	 * independently.
138114843421SMatthew Ahrens 	 */
138214843421SMatthew Ahrens 
138314843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
138468857716SLin Ling 		dmu_tx_t *tx;
138514843421SMatthew Ahrens 		dmu_buf_t *db;
138614843421SMatthew Ahrens 		int objerr;
138714843421SMatthew Ahrens 
138814843421SMatthew Ahrens 		if (issig(JUSTLOOKING) && issig(FORREAL))
1389be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
139014843421SMatthew Ahrens 
139114843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
13923b2aab18SMatthew Ahrens 		if (objerr != 0)
139314843421SMatthew Ahrens 			continue;
139468857716SLin Ling 		tx = dmu_tx_create(os);
139514843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
139614843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
13973b2aab18SMatthew Ahrens 		if (objerr != 0) {
139814843421SMatthew Ahrens 			dmu_tx_abort(tx);
139914843421SMatthew Ahrens 			continue;
140014843421SMatthew Ahrens 		}
140114843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
140214843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
140314843421SMatthew Ahrens 		dmu_tx_commit(tx);
140414843421SMatthew Ahrens 	}
140514843421SMatthew Ahrens 
1406503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
140714843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
140814843421SMatthew Ahrens 	return (0);
140914843421SMatthew Ahrens }
141014843421SMatthew Ahrens 
1411fa9e4066Sahrens void
1412a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1413a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1414fa9e4066Sahrens {
1415503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1416a2eea2e1Sahrens 	    usedobjsp, availobjsp);
1417a2eea2e1Sahrens }
1418a2eea2e1Sahrens 
1419a2eea2e1Sahrens uint64_t
1420a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1421a2eea2e1Sahrens {
1422503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1423a2eea2e1Sahrens }
1424a2eea2e1Sahrens 
1425a2eea2e1Sahrens void
1426a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1427a2eea2e1Sahrens {
1428503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
1429503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
1430503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1431a2eea2e1Sahrens }
1432a2eea2e1Sahrens 
1433a2eea2e1Sahrens void
1434a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1435a2eea2e1Sahrens {
1436503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
1437503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
1438a2eea2e1Sahrens 
1439503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1440503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1441a2eea2e1Sahrens 
1442a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1443503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
144414843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
144514843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
1446fa9e4066Sahrens }
1447fa9e4066Sahrens 
1448fa9e4066Sahrens int
1449fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1450fa9e4066Sahrens {
1451503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1452503ad85cSMatthew Ahrens 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1453fa9e4066Sahrens 	else
1454fa9e4066Sahrens 		return (B_FALSE);
1455fa9e4066Sahrens }
1456fa9e4066Sahrens 
1457ab04eb8eStimh int
1458ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1459ab04eb8eStimh     boolean_t *conflict)
1460ab04eb8eStimh {
1461503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1462ab04eb8eStimh 	uint64_t ignored;
1463ab04eb8eStimh 
1464ab04eb8eStimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1465be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1466ab04eb8eStimh 
1467ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1468ab04eb8eStimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1469ab04eb8eStimh 	    real, maxlen, conflict));
1470ab04eb8eStimh }
1471ab04eb8eStimh 
1472fa9e4066Sahrens int
1473fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1474b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1475fa9e4066Sahrens {
1476503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1477fa9e4066Sahrens 	zap_cursor_t cursor;
1478fa9e4066Sahrens 	zap_attribute_t attr;
1479fa9e4066Sahrens 
14803b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
14813b2aab18SMatthew Ahrens 
1482fa9e4066Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1483be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1484fa9e4066Sahrens 
1485fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
1486fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1487fa9e4066Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1488fa9e4066Sahrens 
148987e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
149087e5029aSahrens 		zap_cursor_fini(&cursor);
1491be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
149287e5029aSahrens 	}
149387e5029aSahrens 
149487e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
149587e5029aSahrens 		zap_cursor_fini(&cursor);
1496be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
149787e5029aSahrens 	}
149887e5029aSahrens 
149987e5029aSahrens 	(void) strcpy(name, attr.za_name);
150087e5029aSahrens 	if (idp)
150187e5029aSahrens 		*idp = attr.za_first_integer;
1502b38f0970Sck 	if (case_conflict)
1503b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
150487e5029aSahrens 	zap_cursor_advance(&cursor);
150587e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
150687e5029aSahrens 	zap_cursor_fini(&cursor);
150787e5029aSahrens 
150887e5029aSahrens 	return (0);
150987e5029aSahrens }
151087e5029aSahrens 
151187e5029aSahrens int
151287e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
151387e5029aSahrens     uint64_t *idp, uint64_t *offp)
151487e5029aSahrens {
1515503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
151687e5029aSahrens 	zap_cursor_t cursor;
151787e5029aSahrens 	zap_attribute_t attr;
151887e5029aSahrens 
151987e5029aSahrens 	/* there is no next dir on a snapshot! */
1520503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
152187e5029aSahrens 	    dd->dd_phys->dd_head_dataset_obj)
1522be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1523fa9e4066Sahrens 
152487e5029aSahrens 	zap_cursor_init_serialized(&cursor,
152587e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
152687e5029aSahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
152787e5029aSahrens 
152887e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
152987e5029aSahrens 		zap_cursor_fini(&cursor);
1530be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
153187e5029aSahrens 	}
153287e5029aSahrens 
153387e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
153487e5029aSahrens 		zap_cursor_fini(&cursor);
1535be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
153687e5029aSahrens 	}
1537fa9e4066Sahrens 
1538fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
153987e5029aSahrens 	if (idp)
154087e5029aSahrens 		*idp = attr.za_first_integer;
1541fa9e4066Sahrens 	zap_cursor_advance(&cursor);
1542fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
154387e5029aSahrens 	zap_cursor_fini(&cursor);
1544fa9e4066Sahrens 
1545fa9e4066Sahrens 	return (0);
1546fa9e4066Sahrens }
1547fa9e4066Sahrens 
1548fa9e4066Sahrens /*
15493b2aab18SMatthew Ahrens  * Find objsets under and including ddobj, call func(ds) on each.
1550fa9e4066Sahrens  */
15511d452cf5Sahrens int
15523b2aab18SMatthew Ahrens dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
15533b2aab18SMatthew Ahrens     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1554088f3894Sahrens {
15553b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
15563b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15573b2aab18SMatthew Ahrens 	zap_cursor_t zc;
15583b2aab18SMatthew Ahrens 	zap_attribute_t *attr;
15593b2aab18SMatthew Ahrens 	uint64_t thisobj;
15603b2aab18SMatthew Ahrens 	int err;
15613b2aab18SMatthew Ahrens 
15623b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
15633b2aab18SMatthew Ahrens 
15643b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
15653b2aab18SMatthew Ahrens 	if (err != 0)
15663b2aab18SMatthew Ahrens 		return (err);
15673b2aab18SMatthew Ahrens 
15683b2aab18SMatthew Ahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
15693b2aab18SMatthew Ahrens 	if (dd->dd_myname[0] == '$') {
15703b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
15713b2aab18SMatthew Ahrens 		return (0);
15723b2aab18SMatthew Ahrens 	}
15733b2aab18SMatthew Ahrens 
15743b2aab18SMatthew Ahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
15753b2aab18SMatthew Ahrens 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
15763b2aab18SMatthew Ahrens 
15773b2aab18SMatthew Ahrens 	/*
15783b2aab18SMatthew Ahrens 	 * Iterate over all children.
15793b2aab18SMatthew Ahrens 	 */
15803b2aab18SMatthew Ahrens 	if (flags & DS_FIND_CHILDREN) {
15813b2aab18SMatthew Ahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
15823b2aab18SMatthew Ahrens 		    dd->dd_phys->dd_child_dir_zapobj);
15833b2aab18SMatthew Ahrens 		    zap_cursor_retrieve(&zc, attr) == 0;
15843b2aab18SMatthew Ahrens 		    (void) zap_cursor_advance(&zc)) {
15853b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
15863b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
15873b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
15883b2aab18SMatthew Ahrens 
15893b2aab18SMatthew Ahrens 			err = dmu_objset_find_dp(dp, attr->za_first_integer,
15903b2aab18SMatthew Ahrens 			    func, arg, flags);
15913b2aab18SMatthew Ahrens 			if (err != 0)
15923b2aab18SMatthew Ahrens 				break;
15933b2aab18SMatthew Ahrens 		}
15943b2aab18SMatthew Ahrens 		zap_cursor_fini(&zc);
15953b2aab18SMatthew Ahrens 
15963b2aab18SMatthew Ahrens 		if (err != 0) {
15973b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
15983b2aab18SMatthew Ahrens 			kmem_free(attr, sizeof (zap_attribute_t));
15993b2aab18SMatthew Ahrens 			return (err);
16003b2aab18SMatthew Ahrens 		}
16013b2aab18SMatthew Ahrens 	}
16023b2aab18SMatthew Ahrens 
16033b2aab18SMatthew Ahrens 	/*
16043b2aab18SMatthew Ahrens 	 * Iterate over all snapshots.
16053b2aab18SMatthew Ahrens 	 */
16063b2aab18SMatthew Ahrens 	if (flags & DS_FIND_SNAPSHOTS) {
16073b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
16083b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
16093b2aab18SMatthew Ahrens 
16103b2aab18SMatthew Ahrens 		if (err == 0) {
16113b2aab18SMatthew Ahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
16123b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
16133b2aab18SMatthew Ahrens 
16143b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
16153b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
16163b2aab18SMatthew Ahrens 			    (void) zap_cursor_advance(&zc)) {
16173b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
16183b2aab18SMatthew Ahrens 				    sizeof (uint64_t));
16193b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
16203b2aab18SMatthew Ahrens 
16213b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
16223b2aab18SMatthew Ahrens 				    attr->za_first_integer, FTAG, &ds);
16233b2aab18SMatthew Ahrens 				if (err != 0)
16243b2aab18SMatthew Ahrens 					break;
16253b2aab18SMatthew Ahrens 				err = func(dp, ds, arg);
16263b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
16273b2aab18SMatthew Ahrens 				if (err != 0)
16283b2aab18SMatthew Ahrens 					break;
16293b2aab18SMatthew Ahrens 			}
16303b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
16313b2aab18SMatthew Ahrens 		}
16323b2aab18SMatthew Ahrens 	}
16333b2aab18SMatthew Ahrens 
16343b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
16353b2aab18SMatthew Ahrens 	kmem_free(attr, sizeof (zap_attribute_t));
16363b2aab18SMatthew Ahrens 
16373b2aab18SMatthew Ahrens 	if (err != 0)
16383b2aab18SMatthew Ahrens 		return (err);
16393b2aab18SMatthew Ahrens 
16403b2aab18SMatthew Ahrens 	/*
16413b2aab18SMatthew Ahrens 	 * Apply to self.
16423b2aab18SMatthew Ahrens 	 */
16433b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
16443b2aab18SMatthew Ahrens 	if (err != 0)
16453b2aab18SMatthew Ahrens 		return (err);
16463b2aab18SMatthew Ahrens 	err = func(dp, ds, arg);
16473b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
16483b2aab18SMatthew Ahrens 	return (err);
1649088f3894Sahrens }
1650088f3894Sahrens 
1651088f3894Sahrens /*
16523b2aab18SMatthew Ahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
16533b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when this is called, and it
16543b2aab18SMatthew Ahrens  * will not be held when the callback is called.
16553b2aab18SMatthew Ahrens  * Therefore this function should only be used when the pool is not changing
16563b2aab18SMatthew Ahrens  * (e.g. in syncing context), or the callback can deal with the possible races.
1657088f3894Sahrens  */
16583b2aab18SMatthew Ahrens static int
16593b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name,
16603b2aab18SMatthew Ahrens     int func(const char *, void *), void *arg, int flags)
1661fa9e4066Sahrens {
1662fa9e4066Sahrens 	dsl_dir_t *dd;
16633b2aab18SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
1664088f3894Sahrens 	dsl_dataset_t *ds;
1665fa9e4066Sahrens 	zap_cursor_t zc;
1666b7661cccSmmusante 	zap_attribute_t *attr;
1667fa9e4066Sahrens 	char *child;
1668088f3894Sahrens 	uint64_t thisobj;
1669088f3894Sahrens 	int err;
1670fa9e4066Sahrens 
16713b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dp, FTAG);
16723b2aab18SMatthew Ahrens 
16733b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
16743b2aab18SMatthew Ahrens 	if (err != 0) {
16753b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
16761d452cf5Sahrens 		return (err);
16773b2aab18SMatthew Ahrens 	}
1678fa9e4066Sahrens 
1679088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1680088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
16813b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
16823b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
1683088f3894Sahrens 		return (0);
1684088f3894Sahrens 	}
1685088f3894Sahrens 
1686088f3894Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
1687b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1688fa9e4066Sahrens 
1689fa9e4066Sahrens 	/*
1690fa9e4066Sahrens 	 * Iterate over all children.
1691fa9e4066Sahrens 	 */
16920b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
1693088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
16940b69c2f0Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
1695b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
16960b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
16973b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
16983b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
16993b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
1700fa9e4066Sahrens 
1701486ae710SMatthew Ahrens 			child = kmem_asprintf("%s/%s", name, attr->za_name);
17023b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
17033b2aab18SMatthew Ahrens 			err = dmu_objset_find_impl(spa, child,
17043b2aab18SMatthew Ahrens 			    func, arg, flags);
17053b2aab18SMatthew Ahrens 			dsl_pool_config_enter(dp, FTAG);
1706486ae710SMatthew Ahrens 			strfree(child);
17073b2aab18SMatthew Ahrens 			if (err != 0)
17080b69c2f0Sahrens 				break;
17090b69c2f0Sahrens 		}
17100b69c2f0Sahrens 		zap_cursor_fini(&zc);
17111d452cf5Sahrens 
17123b2aab18SMatthew Ahrens 		if (err != 0) {
17133b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
17143b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
1715b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
17160b69c2f0Sahrens 			return (err);
17170b69c2f0Sahrens 		}
1718fa9e4066Sahrens 	}
1719fa9e4066Sahrens 
1720fa9e4066Sahrens 	/*
1721fa9e4066Sahrens 	 * Iterate over all snapshots.
1722fa9e4066Sahrens 	 */
1723088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
1724088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1725088f3894Sahrens 
1726088f3894Sahrens 		if (err == 0) {
1727088f3894Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1728088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
1729088f3894Sahrens 
1730088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1731088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
1732088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
17333b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
1734088f3894Sahrens 				    sizeof (uint64_t));
17353b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
1736088f3894Sahrens 
1737486ae710SMatthew Ahrens 				child = kmem_asprintf("%s@%s",
1738486ae710SMatthew Ahrens 				    name, attr->za_name);
17393b2aab18SMatthew Ahrens 				dsl_pool_config_exit(dp, FTAG);
17403b2aab18SMatthew Ahrens 				err = func(child, arg);
17413b2aab18SMatthew Ahrens 				dsl_pool_config_enter(dp, FTAG);
1742486ae710SMatthew Ahrens 				strfree(child);
17433b2aab18SMatthew Ahrens 				if (err != 0)
1744088f3894Sahrens 					break;
1745088f3894Sahrens 			}
1746088f3894Sahrens 			zap_cursor_fini(&zc);
1747fa9e4066Sahrens 		}
1748fa9e4066Sahrens 	}
1749fa9e4066Sahrens 
17503b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
1751b7661cccSmmusante 	kmem_free(attr, sizeof (zap_attribute_t));
17523b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dp, FTAG);
1753fa9e4066Sahrens 
17543b2aab18SMatthew Ahrens 	if (err != 0)
17551d452cf5Sahrens 		return (err);
17561d452cf5Sahrens 
17573b2aab18SMatthew Ahrens 	/* Apply to self. */
17583b2aab18SMatthew Ahrens 	return (func(name, arg));
1759fa9e4066Sahrens }
1760f18faf3fSek 
17613b2aab18SMatthew Ahrens /*
17623b2aab18SMatthew Ahrens  * See comment above dmu_objset_find_impl().
17633b2aab18SMatthew Ahrens  */
17647f73c863SRich Morris int
17653b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg,
17663b2aab18SMatthew Ahrens     int flags)
17677f73c863SRich Morris {
17683b2aab18SMatthew Ahrens 	spa_t *spa;
17693b2aab18SMatthew Ahrens 	int error;
17707f73c863SRich Morris 
17713b2aab18SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
17723b2aab18SMatthew Ahrens 	if (error != 0)
17733b2aab18SMatthew Ahrens 		return (error);
17743b2aab18SMatthew Ahrens 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
17753b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
17763b2aab18SMatthew Ahrens 	return (error);
17777f73c863SRich Morris }
17787f73c863SRich Morris 
1779f18faf3fSek void
1780f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr)
1781f18faf3fSek {
1782503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1783503ad85cSMatthew Ahrens 	os->os_user_ptr = user_ptr;
1784f18faf3fSek }
1785f18faf3fSek 
1786f18faf3fSek void *
1787f18faf3fSek dmu_objset_get_user(objset_t *os)
1788f18faf3fSek {
1789503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1790503ad85cSMatthew Ahrens 	return (os->os_user_ptr);
1791f18faf3fSek }
17923b2aab18SMatthew Ahrens 
17933b2aab18SMatthew Ahrens /*
17943b2aab18SMatthew Ahrens  * Determine name of filesystem, given name of snapshot.
17953b2aab18SMatthew Ahrens  * buf must be at least MAXNAMELEN bytes
17963b2aab18SMatthew Ahrens  */
17973b2aab18SMatthew Ahrens int
17983b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf)
17993b2aab18SMatthew Ahrens {
18003b2aab18SMatthew Ahrens 	char *atp = strchr(snapname, '@');
18013b2aab18SMatthew Ahrens 	if (atp == NULL)
1802be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
18033b2aab18SMatthew Ahrens 	if (atp - snapname >= MAXNAMELEN)
1804be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
18053b2aab18SMatthew Ahrens 	(void) strlcpy(buf, snapname, atp - snapname + 1);
18063b2aab18SMatthew Ahrens 	return (0);
18073b2aab18SMatthew Ahrens }
1808