xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 12380e1e701fda28c9e9f32d01cafb54af279eb5)
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.
26bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2703b1c297SAlexander Eremin  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28*12380e1eSArne Jansen  * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
3155da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
3255da60b9SMark J Musante 
33ecd6cf80Smarks #include <sys/cred.h>
34fa9e4066Sahrens #include <sys/zfs_context.h>
35fa9e4066Sahrens #include <sys/dmu_objset.h>
36fa9e4066Sahrens #include <sys/dsl_dir.h>
37fa9e4066Sahrens #include <sys/dsl_dataset.h>
38fa9e4066Sahrens #include <sys/dsl_prop.h>
39fa9e4066Sahrens #include <sys/dsl_pool.h>
401d452cf5Sahrens #include <sys/dsl_synctask.h>
41ecd6cf80Smarks #include <sys/dsl_deleg.h>
42fa9e4066Sahrens #include <sys/dnode.h>
43fa9e4066Sahrens #include <sys/dbuf.h>
44a2eea2e1Sahrens #include <sys/zvol.h>
45fa9e4066Sahrens #include <sys/dmu_tx.h>
46fa9e4066Sahrens #include <sys/zap.h>
47fa9e4066Sahrens #include <sys/zil.h>
48fa9e4066Sahrens #include <sys/dmu_impl.h>
49ecd6cf80Smarks #include <sys/zfs_ioctl.h>
500a586ceaSMark Shellenbaum #include <sys/sa.h>
5199d5e173STim Haley #include <sys/zfs_onexit.h>
523b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
53*12380e1eSArne Jansen #include <sys/vdev.h>
54fa9e4066Sahrens 
55744947dcSTom Erickson /*
56744947dcSTom Erickson  * Needed to close a window in dnode_move() that allows the objset to be freed
57744947dcSTom Erickson  * before it can be safely accessed.
58744947dcSTom Erickson  */
59744947dcSTom Erickson krwlock_t os_lock;
60744947dcSTom Erickson 
61*12380e1eSArne Jansen /*
62*12380e1eSArne Jansen  * Tunable to overwrite the maximum number of threads for the parallization
63*12380e1eSArne Jansen  * of dmu_objset_find_dp, needed to speed up the import of pools with many
64*12380e1eSArne Jansen  * datasets.
65*12380e1eSArne Jansen  * Default is 4 times the number of leaf vdevs.
66*12380e1eSArne Jansen  */
67*12380e1eSArne Jansen int dmu_find_threads = 0;
68*12380e1eSArne Jansen 
69*12380e1eSArne Jansen static void dmu_objset_find_dp_cb(void *arg);
70*12380e1eSArne Jansen 
71744947dcSTom Erickson void
72744947dcSTom Erickson dmu_objset_init(void)
73744947dcSTom Erickson {
74744947dcSTom Erickson 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
75744947dcSTom Erickson }
76744947dcSTom Erickson 
77744947dcSTom Erickson void
78744947dcSTom Erickson dmu_objset_fini(void)
79744947dcSTom Erickson {
80744947dcSTom Erickson 	rw_destroy(&os_lock);
81744947dcSTom Erickson }
82744947dcSTom Erickson 
83fa9e4066Sahrens spa_t *
84fa9e4066Sahrens dmu_objset_spa(objset_t *os)
85fa9e4066Sahrens {
86503ad85cSMatthew Ahrens 	return (os->os_spa);
87fa9e4066Sahrens }
88fa9e4066Sahrens 
89fa9e4066Sahrens zilog_t *
90fa9e4066Sahrens dmu_objset_zil(objset_t *os)
91fa9e4066Sahrens {
92503ad85cSMatthew Ahrens 	return (os->os_zil);
93fa9e4066Sahrens }
94fa9e4066Sahrens 
95fa9e4066Sahrens dsl_pool_t *
96fa9e4066Sahrens dmu_objset_pool(objset_t *os)
97fa9e4066Sahrens {
98fa9e4066Sahrens 	dsl_dataset_t *ds;
99fa9e4066Sahrens 
100503ad85cSMatthew Ahrens 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
101fa9e4066Sahrens 		return (ds->ds_dir->dd_pool);
102fa9e4066Sahrens 	else
103503ad85cSMatthew Ahrens 		return (spa_get_dsl(os->os_spa));
104fa9e4066Sahrens }
105fa9e4066Sahrens 
106fa9e4066Sahrens dsl_dataset_t *
107fa9e4066Sahrens dmu_objset_ds(objset_t *os)
108fa9e4066Sahrens {
109503ad85cSMatthew Ahrens 	return (os->os_dsl_dataset);
110fa9e4066Sahrens }
111fa9e4066Sahrens 
112fa9e4066Sahrens dmu_objset_type_t
113fa9e4066Sahrens dmu_objset_type(objset_t *os)
114fa9e4066Sahrens {
115503ad85cSMatthew Ahrens 	return (os->os_phys->os_type);
116fa9e4066Sahrens }
117fa9e4066Sahrens 
118fa9e4066Sahrens void
119fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
120fa9e4066Sahrens {
121503ad85cSMatthew Ahrens 	dsl_dataset_name(os->os_dsl_dataset, buf);
122fa9e4066Sahrens }
123fa9e4066Sahrens 
124fa9e4066Sahrens uint64_t
125fa9e4066Sahrens dmu_objset_id(objset_t *os)
126fa9e4066Sahrens {
127503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
128fa9e4066Sahrens 
129fa9e4066Sahrens 	return (ds ? ds->ds_object : 0);
130fa9e4066Sahrens }
131fa9e4066Sahrens 
132edf345e6SMatthew Ahrens zfs_sync_type_t
13355da60b9SMark J Musante dmu_objset_syncprop(objset_t *os)
13455da60b9SMark J Musante {
13555da60b9SMark J Musante 	return (os->os_sync);
13655da60b9SMark J Musante }
13755da60b9SMark J Musante 
138edf345e6SMatthew Ahrens zfs_logbias_op_t
139e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
140e09fa4daSNeil Perrin {
141e09fa4daSNeil Perrin 	return (os->os_logbias);
142e09fa4daSNeil Perrin }
143e09fa4daSNeil Perrin 
144fa9e4066Sahrens static void
145fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
146fa9e4066Sahrens {
147503ad85cSMatthew Ahrens 	objset_t *os = arg;
148fa9e4066Sahrens 
149fa9e4066Sahrens 	/*
150fa9e4066Sahrens 	 * Inheritance should have been done by now.
151fa9e4066Sahrens 	 */
152fa9e4066Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
153fa9e4066Sahrens 
154503ad85cSMatthew Ahrens 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
155fa9e4066Sahrens }
156fa9e4066Sahrens 
157fa9e4066Sahrens static void
158fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
159fa9e4066Sahrens {
160503ad85cSMatthew Ahrens 	objset_t *os = arg;
161fa9e4066Sahrens 
162fa9e4066Sahrens 	/*
163fa9e4066Sahrens 	 * Inheritance and range checking should have been done by now.
164fa9e4066Sahrens 	 */
165fa9e4066Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
166fa9e4066Sahrens 
167db1741f5SJustin T. Gibbs 	os->os_compress = zio_compress_select(os->os_spa, newval,
168db1741f5SJustin T. Gibbs 	    ZIO_COMPRESS_ON);
169fa9e4066Sahrens }
170fa9e4066Sahrens 
171d0ad202dSahrens static void
172d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
173d0ad202dSahrens {
174503ad85cSMatthew Ahrens 	objset_t *os = arg;
175d0ad202dSahrens 
176d0ad202dSahrens 	/*
177d0ad202dSahrens 	 * Inheritance and range checking should have been done by now.
178d0ad202dSahrens 	 */
179d0ad202dSahrens 	ASSERT(newval > 0);
180503ad85cSMatthew Ahrens 	ASSERT(newval <= spa_max_replication(os->os_spa));
181d0ad202dSahrens 
182503ad85cSMatthew Ahrens 	os->os_copies = newval;
183d0ad202dSahrens }
184d0ad202dSahrens 
185b24ab676SJeff Bonwick static void
186b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
187b24ab676SJeff Bonwick {
188b24ab676SJeff Bonwick 	objset_t *os = arg;
189b24ab676SJeff Bonwick 	spa_t *spa = os->os_spa;
190b24ab676SJeff Bonwick 	enum zio_checksum checksum;
191b24ab676SJeff Bonwick 
192b24ab676SJeff Bonwick 	/*
193b24ab676SJeff Bonwick 	 * Inheritance should have been done by now.
194b24ab676SJeff Bonwick 	 */
195b24ab676SJeff Bonwick 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
196b24ab676SJeff Bonwick 
197b24ab676SJeff Bonwick 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
198b24ab676SJeff Bonwick 
199b24ab676SJeff Bonwick 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
200b24ab676SJeff Bonwick 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
201b24ab676SJeff Bonwick }
202b24ab676SJeff Bonwick 
2033baa08fcSek static void
2043baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval)
2053baa08fcSek {
206503ad85cSMatthew Ahrens 	objset_t *os = arg;
2073baa08fcSek 
2083baa08fcSek 	/*
2093baa08fcSek 	 * Inheritance and range checking should have been done by now.
2103baa08fcSek 	 */
2113baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2123baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2133baa08fcSek 
214503ad85cSMatthew Ahrens 	os->os_primary_cache = newval;
2153baa08fcSek }
2163baa08fcSek 
2173baa08fcSek static void
2183baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval)
2193baa08fcSek {
220503ad85cSMatthew Ahrens 	objset_t *os = arg;
2213baa08fcSek 
2223baa08fcSek 	/*
2233baa08fcSek 	 * Inheritance and range checking should have been done by now.
2243baa08fcSek 	 */
2253baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2263baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2273baa08fcSek 
228503ad85cSMatthew Ahrens 	os->os_secondary_cache = newval;
2293baa08fcSek }
2303baa08fcSek 
23155da60b9SMark J Musante static void
23255da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval)
23355da60b9SMark J Musante {
23455da60b9SMark J Musante 	objset_t *os = arg;
23555da60b9SMark J Musante 
23655da60b9SMark J Musante 	/*
23755da60b9SMark J Musante 	 * Inheritance and range checking should have been done by now.
23855da60b9SMark J Musante 	 */
23955da60b9SMark J Musante 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
24055da60b9SMark J Musante 	    newval == ZFS_SYNC_DISABLED);
24155da60b9SMark J Musante 
24255da60b9SMark J Musante 	os->os_sync = newval;
24355da60b9SMark J Musante 	if (os->os_zil)
24455da60b9SMark J Musante 		zil_set_sync(os->os_zil, newval);
24555da60b9SMark J Musante }
24655da60b9SMark J Musante 
247edf345e6SMatthew Ahrens static void
248edf345e6SMatthew Ahrens redundant_metadata_changed_cb(void *arg, uint64_t newval)
249edf345e6SMatthew Ahrens {
250edf345e6SMatthew Ahrens 	objset_t *os = arg;
251edf345e6SMatthew Ahrens 
252edf345e6SMatthew Ahrens 	/*
253edf345e6SMatthew Ahrens 	 * Inheritance and range checking should have been done by now.
254edf345e6SMatthew Ahrens 	 */
255edf345e6SMatthew Ahrens 	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
256edf345e6SMatthew Ahrens 	    newval == ZFS_REDUNDANT_METADATA_MOST);
257edf345e6SMatthew Ahrens 
258edf345e6SMatthew Ahrens 	os->os_redundant_metadata = newval;
259edf345e6SMatthew Ahrens }
260edf345e6SMatthew Ahrens 
261e09fa4daSNeil Perrin static void
262e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
263e09fa4daSNeil Perrin {
264e09fa4daSNeil Perrin 	objset_t *os = arg;
265e09fa4daSNeil Perrin 
266e09fa4daSNeil Perrin 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
267e09fa4daSNeil Perrin 	    newval == ZFS_LOGBIAS_THROUGHPUT);
268e09fa4daSNeil Perrin 	os->os_logbias = newval;
269e09fa4daSNeil Perrin 	if (os->os_zil)
270e09fa4daSNeil Perrin 		zil_set_logbias(os->os_zil, newval);
271e09fa4daSNeil Perrin }
272e09fa4daSNeil Perrin 
273b5152584SMatthew Ahrens static void
274b5152584SMatthew Ahrens recordsize_changed_cb(void *arg, uint64_t newval)
275b5152584SMatthew Ahrens {
276b5152584SMatthew Ahrens 	objset_t *os = arg;
277b5152584SMatthew Ahrens 
278b5152584SMatthew Ahrens 	os->os_recordsize = newval;
279b5152584SMatthew Ahrens }
280b5152584SMatthew Ahrens 
281fa9e4066Sahrens void
282fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
283fa9e4066Sahrens {
284fa9e4066Sahrens 	objset_phys_t *osp = buf;
285fa9e4066Sahrens 
28614843421SMatthew Ahrens 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
287fa9e4066Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
288fa9e4066Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
289fa9e4066Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
29014843421SMatthew Ahrens 	osp->os_flags = BSWAP_64(osp->os_flags);
29114843421SMatthew Ahrens 	if (size == sizeof (objset_phys_t)) {
29214843421SMatthew Ahrens 		dnode_byteswap(&osp->os_userused_dnode);
29314843421SMatthew Ahrens 		dnode_byteswap(&osp->os_groupused_dnode);
29414843421SMatthew Ahrens 	}
295fa9e4066Sahrens }
296fa9e4066Sahrens 
297ea8dc4b6Seschrock int
298ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
299503ad85cSMatthew Ahrens     objset_t **osp)
300fa9e4066Sahrens {
301503ad85cSMatthew Ahrens 	objset_t *os;
302088f3894Sahrens 	int i, err;
303fa9e4066Sahrens 
30491ebeef5Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
30591ebeef5Sahrens 
306503ad85cSMatthew Ahrens 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
307503ad85cSMatthew Ahrens 	os->os_dsl_dataset = ds;
308503ad85cSMatthew Ahrens 	os->os_spa = spa;
309503ad85cSMatthew Ahrens 	os->os_rootbp = bp;
310503ad85cSMatthew Ahrens 	if (!BP_IS_HOLE(os->os_rootbp)) {
3117adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
3127802d7bfSMatthew Ahrens 		zbookmark_phys_t zb;
313b24ab676SJeff Bonwick 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
314b24ab676SJeff Bonwick 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
315b24ab676SJeff Bonwick 
316503ad85cSMatthew Ahrens 		if (DMU_OS_IS_L2CACHEABLE(os))
3177adb730bSGeorge Wilson 			aflags |= ARC_FLAG_L2CACHE;
318aad02571SSaso Kiselkov 		if (DMU_OS_IS_L2COMPRESSIBLE(os))
3197adb730bSGeorge Wilson 			aflags |= ARC_FLAG_L2COMPRESS;
320ea8dc4b6Seschrock 
321503ad85cSMatthew Ahrens 		dprintf_bp(os->os_rootbp, "reading %s", "");
3221b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, os->os_rootbp,
323503ad85cSMatthew Ahrens 		    arc_getbuf_func, &os->os_phys_buf,
32413506d1eSmaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
3253b2aab18SMatthew Ahrens 		if (err != 0) {
326503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
327b87f3af3Sperrin 			/* convert checksum errors into IO errors */
328b87f3af3Sperrin 			if (err == ECKSUM)
329be6fd75aSMatthew Ahrens 				err = SET_ERROR(EIO);
330ea8dc4b6Seschrock 			return (err);
331ea8dc4b6Seschrock 		}
33214843421SMatthew Ahrens 
33314843421SMatthew Ahrens 		/* Increase the blocksize if we are permitted. */
33414843421SMatthew Ahrens 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
335503ad85cSMatthew Ahrens 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
33614843421SMatthew Ahrens 			arc_buf_t *buf = arc_buf_alloc(spa,
337503ad85cSMatthew Ahrens 			    sizeof (objset_phys_t), &os->os_phys_buf,
33814843421SMatthew Ahrens 			    ARC_BUFC_METADATA);
33914843421SMatthew Ahrens 			bzero(buf->b_data, sizeof (objset_phys_t));
340503ad85cSMatthew Ahrens 			bcopy(os->os_phys_buf->b_data, buf->b_data,
341503ad85cSMatthew Ahrens 			    arc_buf_size(os->os_phys_buf));
342503ad85cSMatthew Ahrens 			(void) arc_buf_remove_ref(os->os_phys_buf,
343503ad85cSMatthew Ahrens 			    &os->os_phys_buf);
344503ad85cSMatthew Ahrens 			os->os_phys_buf = buf;
34514843421SMatthew Ahrens 		}
34614843421SMatthew Ahrens 
347503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
348503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
349fa9e4066Sahrens 	} else {
35014843421SMatthew Ahrens 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
35114843421SMatthew Ahrens 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
352503ad85cSMatthew Ahrens 		os->os_phys_buf = arc_buf_alloc(spa, size,
353503ad85cSMatthew Ahrens 		    &os->os_phys_buf, ARC_BUFC_METADATA);
354503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
355503ad85cSMatthew Ahrens 		bzero(os->os_phys, size);
356fa9e4066Sahrens 	}
357fa9e4066Sahrens 
358fa9e4066Sahrens 	/*
359fa9e4066Sahrens 	 * Note: the changed_cb will be called once before the register
360fa9e4066Sahrens 	 * func returns, thus changing the checksum/compression from the
3613baa08fcSek 	 * default (fletcher2/off).  Snapshots don't need to know about
3623baa08fcSek 	 * checksum/compression/copies.
363fa9e4066Sahrens 	 */
3645d7b4d43SMatthew Ahrens 	if (ds != NULL) {
3653b2aab18SMatthew Ahrens 		err = dsl_prop_register(ds,
3663b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
367503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
3683b2aab18SMatthew Ahrens 		if (err == 0) {
3693b2aab18SMatthew Ahrens 			err = dsl_prop_register(ds,
3703b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
371503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
3723b2aab18SMatthew Ahrens 		}
373bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
3743b2aab18SMatthew Ahrens 			if (err == 0) {
3753b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3763b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
377503ad85cSMatthew Ahrens 				    checksum_changed_cb, os);
3783b2aab18SMatthew Ahrens 			}
3793b2aab18SMatthew Ahrens 			if (err == 0) {
3803b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3813b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
382503ad85cSMatthew Ahrens 				    compression_changed_cb, os);
3833b2aab18SMatthew Ahrens 			}
3843b2aab18SMatthew Ahrens 			if (err == 0) {
3853b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3863b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COPIES),
387503ad85cSMatthew Ahrens 				    copies_changed_cb, os);
3883b2aab18SMatthew Ahrens 			}
3893b2aab18SMatthew Ahrens 			if (err == 0) {
3903b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3913b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_DEDUP),
392b24ab676SJeff Bonwick 				    dedup_changed_cb, os);
3933b2aab18SMatthew Ahrens 			}
3943b2aab18SMatthew Ahrens 			if (err == 0) {
3953b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3963b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
397e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
3983b2aab18SMatthew Ahrens 			}
3993b2aab18SMatthew Ahrens 			if (err == 0) {
4003b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
4013b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_SYNC),
40255da60b9SMark J Musante 				    sync_changed_cb, os);
4033b2aab18SMatthew Ahrens 			}
404edf345e6SMatthew Ahrens 			if (err == 0) {
405edf345e6SMatthew Ahrens 				err = dsl_prop_register(ds,
406edf345e6SMatthew Ahrens 				    zfs_prop_to_name(
407edf345e6SMatthew Ahrens 				    ZFS_PROP_REDUNDANT_METADATA),
408edf345e6SMatthew Ahrens 				    redundant_metadata_changed_cb, os);
409edf345e6SMatthew Ahrens 			}
410b5152584SMatthew Ahrens 			if (err == 0) {
411b5152584SMatthew Ahrens 				err = dsl_prop_register(ds,
412b5152584SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
413b5152584SMatthew Ahrens 				    recordsize_changed_cb, os);
414b5152584SMatthew Ahrens 			}
4153baa08fcSek 		}
4163b2aab18SMatthew Ahrens 		if (err != 0) {
417503ad85cSMatthew Ahrens 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
4183b2aab18SMatthew Ahrens 			    &os->os_phys_buf));
419503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
420ea8dc4b6Seschrock 			return (err);
421ea8dc4b6Seschrock 		}
4225d7b4d43SMatthew Ahrens 	} else {
423fa9e4066Sahrens 		/* It's the meta-objset. */
424503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
425db1741f5SJustin T. Gibbs 		os->os_compress = ZIO_COMPRESS_ON;
426503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
427b24ab676SJeff Bonwick 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
428edf345e6SMatthew Ahrens 		os->os_dedup_verify = B_FALSE;
429edf345e6SMatthew Ahrens 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
430edf345e6SMatthew Ahrens 		os->os_sync = ZFS_SYNC_STANDARD;
431503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
432503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
433fa9e4066Sahrens 	}
434fa9e4066Sahrens 
435bc9014e6SJustin Gibbs 	if (ds == NULL || !ds->ds_is_snapshot)
4366e0cbcaaSMatthew Ahrens 		os->os_zil_header = os->os_phys->os_zil_header;
437503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
438fa9e4066Sahrens 
439fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
440503ad85cSMatthew Ahrens 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
441fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
442503ad85cSMatthew Ahrens 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
443fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
444fa9e4066Sahrens 	}
445503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
446fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
447503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
448fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
449fa9e4066Sahrens 
450503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
451503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
452503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
453503ad85cSMatthew Ahrens 
454bc9014e6SJustin Gibbs 	dnode_special_open(os, &os->os_phys->os_meta_dnode,
455bc9014e6SJustin Gibbs 	    DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
456503ad85cSMatthew Ahrens 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
457bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_userused_dnode,
458bc9014e6SJustin Gibbs 		    DMU_USERUSED_OBJECT, &os->os_userused_dnode);
459bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_groupused_dnode,
460bc9014e6SJustin Gibbs 		    DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
46114843421SMatthew Ahrens 	}
462fa9e4066Sahrens 
463503ad85cSMatthew Ahrens 	*osp = os;
464ea8dc4b6Seschrock 	return (0);
465fa9e4066Sahrens }
466fa9e4066Sahrens 
467503ad85cSMatthew Ahrens int
468503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4693cb34c60Sahrens {
470503ad85cSMatthew Ahrens 	int err = 0;
4713cb34c60Sahrens 
4723cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
4735d7b4d43SMatthew Ahrens 	if (ds->ds_objset == NULL) {
4745d7b4d43SMatthew Ahrens 		objset_t *os;
4753cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
4765d7b4d43SMatthew Ahrens 		    ds, dsl_dataset_get_blkptr(ds), &os);
4775d7b4d43SMatthew Ahrens 
4785d7b4d43SMatthew Ahrens 		if (err == 0) {
4795d7b4d43SMatthew Ahrens 			mutex_enter(&ds->ds_lock);
4805d7b4d43SMatthew Ahrens 			ASSERT(ds->ds_objset == NULL);
4815d7b4d43SMatthew Ahrens 			ds->ds_objset = os;
4825d7b4d43SMatthew Ahrens 			mutex_exit(&ds->ds_lock);
4835d7b4d43SMatthew Ahrens 		}
4843cb34c60Sahrens 	}
4855d7b4d43SMatthew Ahrens 	*osp = ds->ds_objset;
4863cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
487503ad85cSMatthew Ahrens 	return (err);
4883cb34c60Sahrens }
4893cb34c60Sahrens 
4903b2aab18SMatthew Ahrens /*
4913b2aab18SMatthew Ahrens  * Holds the pool while the objset is held.  Therefore only one objset
4923b2aab18SMatthew Ahrens  * can be held at a time.
4933b2aab18SMatthew Ahrens  */
4943cb34c60Sahrens int
495503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
4963cb34c60Sahrens {
4973b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
498503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
4993cb34c60Sahrens 	int err;
5003cb34c60Sahrens 
5013b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, tag, &dp);
5023b2aab18SMatthew Ahrens 	if (err != 0)
5033b2aab18SMatthew Ahrens 		return (err);
5043b2aab18SMatthew Ahrens 	err = dsl_dataset_hold(dp, name, tag, &ds);
5053b2aab18SMatthew Ahrens 	if (err != 0) {
5063b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
507503ad85cSMatthew Ahrens 		return (err);
5083b2aab18SMatthew Ahrens 	}
509503ad85cSMatthew Ahrens 
510503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
5113b2aab18SMatthew Ahrens 	if (err != 0) {
512503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
5133b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
5143b2aab18SMatthew Ahrens 	}
515503ad85cSMatthew Ahrens 
5163cb34c60Sahrens 	return (err);
5173cb34c60Sahrens }
5183cb34c60Sahrens 
519*12380e1eSArne Jansen static int
520*12380e1eSArne Jansen dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
521*12380e1eSArne Jansen     boolean_t readonly, void *tag, objset_t **osp)
522*12380e1eSArne Jansen {
523*12380e1eSArne Jansen 	int err;
524*12380e1eSArne Jansen 
525*12380e1eSArne Jansen 	err = dmu_objset_from_ds(ds, osp);
526*12380e1eSArne Jansen 	if (err != 0) {
527*12380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
528*12380e1eSArne Jansen 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
529*12380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
530*12380e1eSArne Jansen 		return (SET_ERROR(EINVAL));
531*12380e1eSArne Jansen 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
532*12380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
533*12380e1eSArne Jansen 		return (SET_ERROR(EROFS));
534*12380e1eSArne Jansen 	}
535*12380e1eSArne Jansen 	return (err);
536*12380e1eSArne Jansen }
537*12380e1eSArne Jansen 
5383b2aab18SMatthew Ahrens /*
5393b2aab18SMatthew Ahrens  * dsl_pool must not be held when this is called.
5403b2aab18SMatthew Ahrens  * Upon successful return, there will be a longhold on the dataset,
5413b2aab18SMatthew Ahrens  * and the dsl_pool will not be held.
5423b2aab18SMatthew Ahrens  */
543fa9e4066Sahrens int
544503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
545503ad85cSMatthew Ahrens     boolean_t readonly, void *tag, objset_t **osp)
546fa9e4066Sahrens {
5473b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
548f18faf3fSek 	dsl_dataset_t *ds;
549f18faf3fSek 	int err;
550fa9e4066Sahrens 
5513b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
5523b2aab18SMatthew Ahrens 	if (err != 0)
5533b2aab18SMatthew Ahrens 		return (err);
5543b2aab18SMatthew Ahrens 	err = dsl_dataset_own(dp, name, tag, &ds);
5553b2aab18SMatthew Ahrens 	if (err != 0) {
5563b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
557fa9e4066Sahrens 		return (err);
5583b2aab18SMatthew Ahrens 	}
559*12380e1eSArne Jansen 	err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
5603b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
561*12380e1eSArne Jansen 
5623cb34c60Sahrens 	return (err);
563fa9e4066Sahrens }
564fa9e4066Sahrens 
565*12380e1eSArne Jansen int
566*12380e1eSArne Jansen dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
567*12380e1eSArne Jansen     boolean_t readonly, void *tag, objset_t **osp)
568*12380e1eSArne Jansen {
569*12380e1eSArne Jansen 	dsl_dataset_t *ds;
570*12380e1eSArne Jansen 	int err;
571*12380e1eSArne Jansen 
572*12380e1eSArne Jansen 	err = dsl_dataset_own_obj(dp, obj, tag, &ds);
573*12380e1eSArne Jansen 	if (err != 0)
574*12380e1eSArne Jansen 		return (err);
575*12380e1eSArne Jansen 
576*12380e1eSArne Jansen 	return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
577*12380e1eSArne Jansen }
578*12380e1eSArne Jansen 
579fa9e4066Sahrens void
580503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
581fa9e4066Sahrens {
5823b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(os);
583503ad85cSMatthew Ahrens 	dsl_dataset_rele(os->os_dsl_dataset, tag);
5843b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, tag);
585503ad85cSMatthew Ahrens }
586503ad85cSMatthew Ahrens 
58791948b51SKeith M Wesolowski /*
58891948b51SKeith M Wesolowski  * When we are called, os MUST refer to an objset associated with a dataset
58991948b51SKeith M Wesolowski  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
59091948b51SKeith M Wesolowski  * == tag.  We will then release and reacquire ownership of the dataset while
59191948b51SKeith M Wesolowski  * holding the pool config_rwlock to avoid intervening namespace or ownership
59291948b51SKeith M Wesolowski  * changes may occur.
59391948b51SKeith M Wesolowski  *
59491948b51SKeith M Wesolowski  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
59591948b51SKeith M Wesolowski  * release the hold on its dataset and acquire a new one on the dataset of the
59691948b51SKeith M Wesolowski  * same name so that it can be partially torn down and reconstructed.
59791948b51SKeith M Wesolowski  */
59891948b51SKeith M Wesolowski void
59991948b51SKeith M Wesolowski dmu_objset_refresh_ownership(objset_t *os, void *tag)
60091948b51SKeith M Wesolowski {
60191948b51SKeith M Wesolowski 	dsl_pool_t *dp;
60291948b51SKeith M Wesolowski 	dsl_dataset_t *ds, *newds;
60391948b51SKeith M Wesolowski 	char name[MAXNAMELEN];
60491948b51SKeith M Wesolowski 
60591948b51SKeith M Wesolowski 	ds = os->os_dsl_dataset;
60691948b51SKeith M Wesolowski 	VERIFY3P(ds, !=, NULL);
60791948b51SKeith M Wesolowski 	VERIFY3P(ds->ds_owner, ==, tag);
60891948b51SKeith M Wesolowski 	VERIFY(dsl_dataset_long_held(ds));
60991948b51SKeith M Wesolowski 
61091948b51SKeith M Wesolowski 	dsl_dataset_name(ds, name);
61191948b51SKeith M Wesolowski 	dp = dmu_objset_pool(os);
61291948b51SKeith M Wesolowski 	dsl_pool_config_enter(dp, FTAG);
61391948b51SKeith M Wesolowski 	dmu_objset_disown(os, tag);
61491948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
61591948b51SKeith M Wesolowski 	VERIFY3P(newds, ==, os->os_dsl_dataset);
61691948b51SKeith M Wesolowski 	dsl_pool_config_exit(dp, FTAG);
61791948b51SKeith M Wesolowski }
61891948b51SKeith M Wesolowski 
619503ad85cSMatthew Ahrens void
620503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
621503ad85cSMatthew Ahrens {
622503ad85cSMatthew Ahrens 	dsl_dataset_disown(os->os_dsl_dataset, tag);
623fa9e4066Sahrens }
624fa9e4066Sahrens 
6253b2aab18SMatthew Ahrens void
6261934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
627ea8dc4b6Seschrock {
628bc9014e6SJustin Gibbs 	dnode_t dn_marker;
629ea8dc4b6Seschrock 	dnode_t *dn;
630c543ec06Sahrens 
631503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
632bc9014e6SJustin Gibbs 	dn = list_head(&os->os_dnodes);
633bc9014e6SJustin Gibbs 	while (dn != NULL) {
634bc9014e6SJustin Gibbs 		/*
635bc9014e6SJustin Gibbs 		 * Skip dnodes without holds.  We have to do this dance
636bc9014e6SJustin Gibbs 		 * because dnode_add_ref() only works if there is already a
637bc9014e6SJustin Gibbs 		 * hold.  If the dnode has no holds, then it has no dbufs.
638bc9014e6SJustin Gibbs 		 */
639bc9014e6SJustin Gibbs 		if (dnode_add_ref(dn, FTAG)) {
640bc9014e6SJustin Gibbs 			list_insert_after(&os->os_dnodes, dn, &dn_marker);
641bc9014e6SJustin Gibbs 			mutex_exit(&os->os_lock);
642c543ec06Sahrens 
643bc9014e6SJustin Gibbs 			dnode_evict_dbufs(dn);
644bc9014e6SJustin Gibbs 			dnode_rele(dn, FTAG);
645c543ec06Sahrens 
646bc9014e6SJustin Gibbs 			mutex_enter(&os->os_lock);
647bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, &dn_marker);
648bc9014e6SJustin Gibbs 			list_remove(&os->os_dnodes, &dn_marker);
649bc9014e6SJustin Gibbs 		} else {
650bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, dn);
651bc9014e6SJustin Gibbs 		}
652ea8dc4b6Seschrock 	}
653503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
654bc9014e6SJustin Gibbs 
655bc9014e6SJustin Gibbs 	if (DMU_USERUSED_DNODE(os) != NULL) {
656bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
657bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
658bc9014e6SJustin Gibbs 	}
659bc9014e6SJustin Gibbs 	dnode_evict_dbufs(DMU_META_DNODE(os));
660ea8dc4b6Seschrock }
661ea8dc4b6Seschrock 
662bc9014e6SJustin Gibbs /*
663bc9014e6SJustin Gibbs  * Objset eviction processing is split into into two pieces.
664bc9014e6SJustin Gibbs  * The first marks the objset as evicting, evicts any dbufs that
665bc9014e6SJustin Gibbs  * have a refcount of zero, and then queues up the objset for the
666bc9014e6SJustin Gibbs  * second phase of eviction.  Once os->os_dnodes has been cleared by
667bc9014e6SJustin Gibbs  * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
668bc9014e6SJustin Gibbs  * The second phase closes the special dnodes, dequeues the objset from
669bc9014e6SJustin Gibbs  * the list of those undergoing eviction, and finally frees the objset.
670bc9014e6SJustin Gibbs  *
671bc9014e6SJustin Gibbs  * NOTE: Due to asynchronous eviction processing (invocation of
672bc9014e6SJustin Gibbs  *       dnode_buf_pageout()), it is possible for the meta dnode for the
673bc9014e6SJustin Gibbs  *       objset to have no holds even though os->os_dnodes is not empty.
674bc9014e6SJustin Gibbs  */
675fa9e4066Sahrens void
676503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
677fa9e4066Sahrens {
678503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
679fa9e4066Sahrens 
680b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
681b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, t));
682fa9e4066Sahrens 
6833baa08fcSek 	if (ds) {
684bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
6853b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6863b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
687503ad85cSMatthew Ahrens 			    checksum_changed_cb, os));
6883b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6893b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
690503ad85cSMatthew Ahrens 			    compression_changed_cb, os));
6913b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6923b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_COPIES),
693503ad85cSMatthew Ahrens 			    copies_changed_cb, os));
6943b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6953b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_DEDUP),
696b24ab676SJeff Bonwick 			    dedup_changed_cb, os));
6973b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6983b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
699e09fa4daSNeil Perrin 			    logbias_changed_cb, os));
7003b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
7013b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SYNC),
70255da60b9SMark J Musante 			    sync_changed_cb, os));
703edf345e6SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
704edf345e6SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA),
705edf345e6SMatthew Ahrens 			    redundant_metadata_changed_cb, os));
706b5152584SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
707b5152584SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
708b5152584SMatthew Ahrens 			    recordsize_changed_cb, os));
7093baa08fcSek 		}
7103b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_unregister(ds,
7113b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
712503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os));
7133b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_unregister(ds,
7143b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
715503ad85cSMatthew Ahrens 		    secondary_cache_changed_cb, os));
716fa9e4066Sahrens 	}
717fa9e4066Sahrens 
7180a586ceaSMark Shellenbaum 	if (os->os_sa)
7190a586ceaSMark Shellenbaum 		sa_tear_down(os);
7200a586ceaSMark Shellenbaum 
721bc9014e6SJustin Gibbs 	os->os_evicting = B_TRUE;
7223b2aab18SMatthew Ahrens 	dmu_objset_evict_dbufs(os);
723ea8dc4b6Seschrock 
724bc9014e6SJustin Gibbs 	mutex_enter(&os->os_lock);
725bc9014e6SJustin Gibbs 	spa_evicting_os_register(os->os_spa, os);
726bc9014e6SJustin Gibbs 	if (list_is_empty(&os->os_dnodes)) {
727bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
728bc9014e6SJustin Gibbs 		dmu_objset_evict_done(os);
729bc9014e6SJustin Gibbs 	} else {
730bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
731bc9014e6SJustin Gibbs 	}
732bc9014e6SJustin Gibbs }
733bc9014e6SJustin Gibbs 
734bc9014e6SJustin Gibbs void
735bc9014e6SJustin Gibbs dmu_objset_evict_done(objset_t *os)
736bc9014e6SJustin Gibbs {
737bc9014e6SJustin Gibbs 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
738bc9014e6SJustin Gibbs 
739744947dcSTom Erickson 	dnode_special_close(&os->os_meta_dnode);
740744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os)) {
741744947dcSTom Erickson 		dnode_special_close(&os->os_userused_dnode);
742744947dcSTom Erickson 		dnode_special_close(&os->os_groupused_dnode);
74314843421SMatthew Ahrens 	}
744503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
745fa9e4066Sahrens 
7463b2aab18SMatthew Ahrens 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
747744947dcSTom Erickson 
748744947dcSTom Erickson 	/*
749744947dcSTom Erickson 	 * This is a barrier to prevent the objset from going away in
750744947dcSTom Erickson 	 * dnode_move() until we can safely ensure that the objset is still in
751744947dcSTom Erickson 	 * use. We consider the objset valid before the barrier and invalid
752744947dcSTom Erickson 	 * after the barrier.
753744947dcSTom Erickson 	 */
754744947dcSTom Erickson 	rw_enter(&os_lock, RW_READER);
755744947dcSTom Erickson 	rw_exit(&os_lock);
756744947dcSTom Erickson 
757503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
758503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
759503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
760bc9014e6SJustin Gibbs 	spa_evicting_os_deregister(os->os_spa, os);
761503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
762fa9e4066Sahrens }
763fa9e4066Sahrens 
76471eb0538SChris Kirby timestruc_t
76571eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
76671eb0538SChris Kirby {
76771eb0538SChris Kirby 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
76871eb0538SChris Kirby }
76971eb0538SChris Kirby 
770fa9e4066Sahrens /* called from dsl for meta-objset */
771503ad85cSMatthew Ahrens objset_t *
772c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
773c717a561Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
774fa9e4066Sahrens {
775503ad85cSMatthew Ahrens 	objset_t *os;
776fa9e4066Sahrens 	dnode_t *mdn;
777fa9e4066Sahrens 
778fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7793b2aab18SMatthew Ahrens 
780feaa74e4SMark Maybee 	if (ds != NULL)
7813b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_from_ds(ds, &os));
782feaa74e4SMark Maybee 	else
7833b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
784feaa74e4SMark Maybee 
785744947dcSTom Erickson 	mdn = DMU_META_DNODE(os);
786fa9e4066Sahrens 
787fa9e4066Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
788fa9e4066Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
789fa9e4066Sahrens 
790fa9e4066Sahrens 	/*
791fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
792fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
793fa9e4066Sahrens 	 * we are also accessing it in open context.
794fa9e4066Sahrens 	 *
795fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
796fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
797fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
798fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
799fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
800fa9e4066Sahrens 	 */
801ea8dc4b6Seschrock 	if (ds != NULL) {
802ea8dc4b6Seschrock 		int levels = 1;
803ea8dc4b6Seschrock 
804ea8dc4b6Seschrock 		/*
805ea8dc4b6Seschrock 		 * Determine the number of levels necessary for the meta-dnode
806ea8dc4b6Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
807ea8dc4b6Seschrock 		 */
808ea8dc4b6Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
809ea8dc4b6Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
810ea8dc4b6Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
811ea8dc4b6Seschrock 			levels++;
812ea8dc4b6Seschrock 
813fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
814ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
815ea8dc4b6Seschrock 	}
816fa9e4066Sahrens 
817fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
818fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
819fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
820503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
821503ad85cSMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
822503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
823503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
82414843421SMatthew Ahrens 	}
825fa9e4066Sahrens 
826fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
827fa9e4066Sahrens 
828503ad85cSMatthew Ahrens 	return (os);
829fa9e4066Sahrens }
830fa9e4066Sahrens 
8313b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg {
8323b2aab18SMatthew Ahrens 	const char *doca_name;
8333b2aab18SMatthew Ahrens 	cred_t *doca_cred;
8343b2aab18SMatthew Ahrens 	void (*doca_userfunc)(objset_t *os, void *arg,
8353b2aab18SMatthew Ahrens 	    cred_t *cr, dmu_tx_t *tx);
8363b2aab18SMatthew Ahrens 	void *doca_userarg;
8373b2aab18SMatthew Ahrens 	dmu_objset_type_t doca_type;
8383b2aab18SMatthew Ahrens 	uint64_t doca_flags;
8393b2aab18SMatthew Ahrens } dmu_objset_create_arg_t;
840fa9e4066Sahrens 
841ecd6cf80Smarks /*ARGSUSED*/
842fa9e4066Sahrens static int
8433b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx)
844fa9e4066Sahrens {
8453b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
8463b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8473b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
8483b2aab18SMatthew Ahrens 	const char *tail;
8493b2aab18SMatthew Ahrens 	int error;
8501d452cf5Sahrens 
8513b2aab18SMatthew Ahrens 	if (strchr(doca->doca_name, '@') != NULL)
852be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8531d452cf5Sahrens 
8543b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
8553b2aab18SMatthew Ahrens 	if (error != 0)
8563b2aab18SMatthew Ahrens 		return (error);
8573b2aab18SMatthew Ahrens 	if (tail == NULL) {
8583b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
859be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
8601d452cf5Sahrens 	}
861a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
862a2afb611SJerry Jelinek 	    doca->doca_cred);
8633b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
864ecd6cf80Smarks 
865a2afb611SJerry Jelinek 	return (error);
8661d452cf5Sahrens }
8671d452cf5Sahrens 
8681d452cf5Sahrens static void
8693b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
8701d452cf5Sahrens {
8713b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
8723b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8733b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
8743b2aab18SMatthew Ahrens 	const char *tail;
8754445fffbSMatthew Ahrens 	dsl_dataset_t *ds;
8763b2aab18SMatthew Ahrens 	uint64_t obj;
8774445fffbSMatthew Ahrens 	blkptr_t *bp;
8783b2aab18SMatthew Ahrens 	objset_t *os;
879fa9e4066Sahrens 
8803b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
881fa9e4066Sahrens 
8823b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
8833b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
884fa9e4066Sahrens 
8853b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
8864445fffbSMatthew Ahrens 	bp = dsl_dataset_get_blkptr(ds);
8873b2aab18SMatthew Ahrens 	os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
8883b2aab18SMatthew Ahrens 	    ds, bp, doca->doca_type, tx);
889fa9e4066Sahrens 
8903b2aab18SMatthew Ahrens 	if (doca->doca_userfunc != NULL) {
8913b2aab18SMatthew Ahrens 		doca->doca_userfunc(os, doca->doca_userarg,
8923b2aab18SMatthew Ahrens 		    doca->doca_cred, tx);
893fa9e4066Sahrens 	}
894ecd6cf80Smarks 
8953b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "create", tx, "");
8964445fffbSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
8973b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
898fa9e4066Sahrens }
899fa9e4066Sahrens 
900fa9e4066Sahrens int
901ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
902ecd6cf80Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
903fa9e4066Sahrens {
9043b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t doca;
905fa9e4066Sahrens 
9063b2aab18SMatthew Ahrens 	doca.doca_name = name;
9073b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
9083b2aab18SMatthew Ahrens 	doca.doca_flags = flags;
9093b2aab18SMatthew Ahrens 	doca.doca_userfunc = func;
9103b2aab18SMatthew Ahrens 	doca.doca_userarg = arg;
9113b2aab18SMatthew Ahrens 	doca.doca_type = type;
912ecd6cf80Smarks 
9133b2aab18SMatthew Ahrens 	return (dsl_sync_task(name,
9147d46dc6cSMatthew Ahrens 	    dmu_objset_create_check, dmu_objset_create_sync, &doca,
9157d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
916ae46e4c7SMatthew Ahrens }
917ae46e4c7SMatthew Ahrens 
9183b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg {
9193b2aab18SMatthew Ahrens 	const char *doca_clone;
9203b2aab18SMatthew Ahrens 	const char *doca_origin;
9213b2aab18SMatthew Ahrens 	cred_t *doca_cred;
9223b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t;
9233b2aab18SMatthew Ahrens 
9243b2aab18SMatthew Ahrens /*ARGSUSED*/
9253b2aab18SMatthew Ahrens static int
9263b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
927ae46e4c7SMatthew Ahrens {
9283b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
929ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
930ae46e4c7SMatthew Ahrens 	const char *tail;
9313b2aab18SMatthew Ahrens 	int error;
9323b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
9333b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
934ae46e4c7SMatthew Ahrens 
9353b2aab18SMatthew Ahrens 	if (strchr(doca->doca_clone, '@') != NULL)
936be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9373b2aab18SMatthew Ahrens 
9383b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
9393b2aab18SMatthew Ahrens 	if (error != 0)
9403b2aab18SMatthew Ahrens 		return (error);
941ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
9423b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
943be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
944fa9e4066Sahrens 	}
94503b1c297SAlexander Eremin 
946a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
947a2afb611SJerry Jelinek 	    doca->doca_cred);
948a2afb611SJerry Jelinek 	if (error != 0) {
949a2afb611SJerry Jelinek 		dsl_dir_rele(pdd, FTAG);
950a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
951a2afb611SJerry Jelinek 	}
9523b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
953fa9e4066Sahrens 
9543b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
9553b2aab18SMatthew Ahrens 	if (error != 0)
95699d5e173STim Haley 		return (error);
95799d5e173STim Haley 
9583b2aab18SMatthew Ahrens 	/* You can only clone snapshots, not the head datasets. */
959bc9014e6SJustin Gibbs 	if (!origin->ds_is_snapshot) {
9603b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
961be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
96292241e0bSTom Erickson 	}
9633b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
96499d5e173STim Haley 
9653b2aab18SMatthew Ahrens 	return (0);
966ea2f5b9eSMatthew Ahrens }
9671d452cf5Sahrens 
9683b2aab18SMatthew Ahrens static void
9693b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
9701d452cf5Sahrens {
9713b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
9723b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
9733b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
9743b2aab18SMatthew Ahrens 	const char *tail;
9753b2aab18SMatthew Ahrens 	dsl_dataset_t *origin, *ds;
9763b2aab18SMatthew Ahrens 	uint64_t obj;
9773b2aab18SMatthew Ahrens 	char namebuf[MAXNAMELEN];
9784445fffbSMatthew Ahrens 
9793b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
9803b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
9814445fffbSMatthew Ahrens 
9823b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
9833b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
984f2e10be3Srm 
9853b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
9863b2aab18SMatthew Ahrens 	dsl_dataset_name(origin, namebuf);
9873b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "clone", tx,
9883b2aab18SMatthew Ahrens 	    "origin=%s (%llu)", namebuf, origin->ds_object);
9893b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
9903b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
9913b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
9921d452cf5Sahrens }
9931d452cf5Sahrens 
9941d452cf5Sahrens int
9953b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin)
9961d452cf5Sahrens {
9973b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t doca;
9984445fffbSMatthew Ahrens 
9993b2aab18SMatthew Ahrens 	doca.doca_clone = clone;
10003b2aab18SMatthew Ahrens 	doca.doca_origin = origin;
10013b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
100299d5e173STim Haley 
10033b2aab18SMatthew Ahrens 	return (dsl_sync_task(clone,
10047d46dc6cSMatthew Ahrens 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
10057d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
10064445fffbSMatthew Ahrens }
10074445fffbSMatthew Ahrens 
10084445fffbSMatthew Ahrens int
10094445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname)
10104445fffbSMatthew Ahrens {
10114445fffbSMatthew Ahrens 	int err;
10124445fffbSMatthew Ahrens 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
10134445fffbSMatthew Ahrens 	nvlist_t *snaps = fnvlist_alloc();
10144445fffbSMatthew Ahrens 
10154445fffbSMatthew Ahrens 	fnvlist_add_boolean(snaps, longsnap);
10164445fffbSMatthew Ahrens 	strfree(longsnap);
10173b2aab18SMatthew Ahrens 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
10183b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
10194445fffbSMatthew Ahrens 	return (err);
10204445fffbSMatthew Ahrens }
10214445fffbSMatthew Ahrens 
1022fa9e4066Sahrens static void
102314843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
1024fa9e4066Sahrens {
1025c717a561Smaybee 	dnode_t *dn;
1026faafa6e3Sahrens 
1027c717a561Smaybee 	while (dn = list_head(list)) {
1028c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1029c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
1030c717a561Smaybee 		/*
103114843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
103214843421SMatthew Ahrens 		 * meta-dnode needs to set it ouside dnode_sync().
1033c717a561Smaybee 		 */
1034c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1035c717a561Smaybee 		ASSERT(dn->dn_zio);
1036faafa6e3Sahrens 
1037c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1038c717a561Smaybee 		list_remove(list, dn);
103914843421SMatthew Ahrens 
104014843421SMatthew Ahrens 		if (newlist) {
104114843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
104214843421SMatthew Ahrens 			list_insert_tail(newlist, dn);
104314843421SMatthew Ahrens 		}
104414843421SMatthew Ahrens 
1045c717a561Smaybee 		dnode_sync(dn, tx);
1046fa9e4066Sahrens 	}
1047fa9e4066Sahrens }
1048fa9e4066Sahrens 
1049fa9e4066Sahrens /* ARGSUSED */
1050fa9e4066Sahrens static void
1051b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1052fa9e4066Sahrens {
1053e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1054503ad85cSMatthew Ahrens 	objset_t *os = arg;
1055c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1056fa9e4066Sahrens 
10575d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp));
10583b2aab18SMatthew Ahrens 	ASSERT3P(bp, ==, os->os_rootbp);
10593b2aab18SMatthew Ahrens 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
10603b2aab18SMatthew Ahrens 	ASSERT0(BP_GET_LEVEL(bp));
10610a4e9518Sgw 
1062fa9e4066Sahrens 	/*
106314843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
106414843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
106514843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
106614843421SMatthew Ahrens 	 * dnode and user/group accounting objects).
1067fa9e4066Sahrens 	 */
106814843421SMatthew Ahrens 	bp->blk_fill = 0;
1069e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
10705d7b4d43SMatthew Ahrens 		bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1071b24ab676SJeff Bonwick }
1072b24ab676SJeff Bonwick 
1073b24ab676SJeff Bonwick /* ARGSUSED */
1074b24ab676SJeff Bonwick static void
1075b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1076b24ab676SJeff Bonwick {
1077b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1078b24ab676SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
1079b24ab676SJeff Bonwick 	objset_t *os = arg;
10800a4e9518Sgw 
1081e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1082b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, bp_orig));
1083e14bb325SJeff Bonwick 	} else {
1084b24ab676SJeff Bonwick 		dsl_dataset_t *ds = os->os_dsl_dataset;
1085b24ab676SJeff Bonwick 		dmu_tx_t *tx = os->os_synctx;
1086b24ab676SJeff Bonwick 
1087b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1088b24ab676SJeff Bonwick 		dsl_dataset_block_born(ds, bp, tx);
10890a4e9518Sgw 	}
1090c717a561Smaybee }
1091c717a561Smaybee 
1092fa9e4066Sahrens /* called from dsl */
1093fa9e4066Sahrens void
1094503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1095fa9e4066Sahrens {
1096fa9e4066Sahrens 	int txgoff;
10977802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
1098b24ab676SJeff Bonwick 	zio_prop_t zp;
1099c717a561Smaybee 	zio_t *zio;
1100c717a561Smaybee 	list_t *list;
110114843421SMatthew Ahrens 	list_t *newlist = NULL;
1102c717a561Smaybee 	dbuf_dirty_record_t *dr;
1103c717a561Smaybee 
1104c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1105fa9e4066Sahrens 
1106fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1107fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
1108fa9e4066Sahrens 	os->os_synctx = tx;
1109fa9e4066Sahrens 
111087bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
111187bd5c1eSahrens 		/*
111287bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
111387bd5c1eSahrens 		 * spa_max_replication() could change, so reset
111487bd5c1eSahrens 		 * os_copies here.
111587bd5c1eSahrens 		 */
111687bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
111787bd5c1eSahrens 	}
111887bd5c1eSahrens 
1119fa9e4066Sahrens 	/*
1120c717a561Smaybee 	 * Create the root block IO
1121fa9e4066Sahrens 	 */
1122b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1123b24ab676SJeff Bonwick 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1124b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
11251b912ec7SGeorge Wilson 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1126b24ab676SJeff Bonwick 
1127b24ab676SJeff Bonwick 	dmu_write_policy(os, NULL, 0, 0, &zp);
1128b24ab676SJeff Bonwick 
1129b24ab676SJeff Bonwick 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1130aad02571SSaso Kiselkov 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1131aad02571SSaso Kiselkov 	    DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
113269962b56SMatthew Ahrens 	    NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1133aad02571SSaso Kiselkov 	    ZIO_FLAG_MUSTSUCCEED, &zb);
1134c717a561Smaybee 
1135c717a561Smaybee 	/*
113614843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
1137c717a561Smaybee 	 */
1138744947dcSTom Erickson 	DMU_META_DNODE(os)->dn_zio = zio;
1139744947dcSTom Erickson 	dnode_sync(DMU_META_DNODE(os), tx);
1140c717a561Smaybee 
114114843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
114214843421SMatthew Ahrens 
1143744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) &&
1144744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1145744947dcSTom Erickson 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1146744947dcSTom Erickson 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1147744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1148744947dcSTom Erickson 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
114914843421SMatthew Ahrens 	}
115014843421SMatthew Ahrens 
1151c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
1152fa9e4066Sahrens 
115314843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
115414843421SMatthew Ahrens 		newlist = &os->os_synced_dnodes;
115514843421SMatthew Ahrens 		/*
115614843421SMatthew Ahrens 		 * We must create the list here because it uses the
115714843421SMatthew Ahrens 		 * dn_dirty_link[] of this txg.
115814843421SMatthew Ahrens 		 */
115914843421SMatthew Ahrens 		list_create(newlist, sizeof (dnode_t),
116014843421SMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
116114843421SMatthew Ahrens 	}
116214843421SMatthew Ahrens 
116314843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
116414843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1165fa9e4066Sahrens 
1166744947dcSTom Erickson 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1167c717a561Smaybee 	while (dr = list_head(list)) {
11683b2aab18SMatthew Ahrens 		ASSERT0(dr->dr_dbuf->db_level);
1169c717a561Smaybee 		list_remove(list, dr);
1170c717a561Smaybee 		if (dr->dr_zio)
1171c717a561Smaybee 			zio_nowait(dr->dr_zio);
1172c717a561Smaybee 	}
1173c717a561Smaybee 	/*
1174c717a561Smaybee 	 * Free intent log blocks up to this tx.
1175c717a561Smaybee 	 */
1176c717a561Smaybee 	zil_sync(os->os_zil, tx);
1177088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
1178c717a561Smaybee 	zio_nowait(zio);
1179fa9e4066Sahrens }
1180fa9e4066Sahrens 
1181b24ab676SJeff Bonwick boolean_t
1182b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1183b24ab676SJeff Bonwick {
1184b24ab676SJeff Bonwick 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1185b24ab676SJeff Bonwick 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1186b24ab676SJeff Bonwick }
1187b24ab676SJeff Bonwick 
1188744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
118914843421SMatthew Ahrens 
119014843421SMatthew Ahrens void
119114843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
119214843421SMatthew Ahrens {
119314843421SMatthew Ahrens 	used_cbs[ost] = cb;
119414843421SMatthew Ahrens }
119514843421SMatthew Ahrens 
119614843421SMatthew Ahrens boolean_t
1197503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
119814843421SMatthew Ahrens {
119914843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1200744947dcSTom Erickson 	    used_cbs[os->os_phys->os_type] != NULL &&
1201744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os) != NULL);
120214843421SMatthew Ahrens }
120314843421SMatthew Ahrens 
12049966ca11SMatthew Ahrens static void
12050a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
12060a586ceaSMark Shellenbaum     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
12079966ca11SMatthew Ahrens {
12080a586ceaSMark Shellenbaum 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
12090a586ceaSMark Shellenbaum 		int64_t delta = DNODE_SIZE + used;
12109966ca11SMatthew Ahrens 		if (subtract)
12119966ca11SMatthew Ahrens 			delta = -delta;
1212b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
12139966ca11SMatthew Ahrens 		    user, delta, tx));
1214b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
12159966ca11SMatthew Ahrens 		    group, delta, tx));
12169966ca11SMatthew Ahrens 	}
12179966ca11SMatthew Ahrens }
12189966ca11SMatthew Ahrens 
121914843421SMatthew Ahrens void
12200a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
122114843421SMatthew Ahrens {
122214843421SMatthew Ahrens 	dnode_t *dn;
122314843421SMatthew Ahrens 	list_t *list = &os->os_synced_dnodes;
122414843421SMatthew Ahrens 
122514843421SMatthew Ahrens 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
122614843421SMatthew Ahrens 
122714843421SMatthew Ahrens 	while (dn = list_head(list)) {
12281d8ccc7bSMark Shellenbaum 		int flags;
122914843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
123014843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
123114843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
123214843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
123314843421SMatthew Ahrens 
123414843421SMatthew Ahrens 		/* Allocate the user/groupused objects if necessary. */
1235744947dcSTom Erickson 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1236503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
123714843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT,
123814843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1239503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
124014843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT,
124114843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
124214843421SMatthew Ahrens 		}
124314843421SMatthew Ahrens 
124414843421SMatthew Ahrens 		/*
12459966ca11SMatthew Ahrens 		 * We intentionally modify the zap object even if the
12460a586ceaSMark Shellenbaum 		 * net delta is zero.  Otherwise
12479966ca11SMatthew Ahrens 		 * the block of the zap obj could be shared between
12489966ca11SMatthew Ahrens 		 * datasets but need to be different between them after
12499966ca11SMatthew Ahrens 		 * a bprewrite.
125014843421SMatthew Ahrens 		 */
125114843421SMatthew Ahrens 
12521d8ccc7bSMark Shellenbaum 		flags = dn->dn_id_flags;
12531d8ccc7bSMark Shellenbaum 		ASSERT(flags);
12541d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST)  {
12550a586ceaSMark Shellenbaum 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
12560a586ceaSMark Shellenbaum 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
12570a586ceaSMark Shellenbaum 		}
12581d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_NEW_EXIST) {
12590a586ceaSMark Shellenbaum 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
12600a586ceaSMark Shellenbaum 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
12610a586ceaSMark Shellenbaum 			    dn->dn_newgid, B_FALSE, tx);
12620a586ceaSMark Shellenbaum 		}
12630a586ceaSMark Shellenbaum 
12641d8ccc7bSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
12650a586ceaSMark Shellenbaum 		dn->dn_oldused = 0;
12660a586ceaSMark Shellenbaum 		dn->dn_oldflags = 0;
12670a586ceaSMark Shellenbaum 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
12680a586ceaSMark Shellenbaum 			dn->dn_olduid = dn->dn_newuid;
12690a586ceaSMark Shellenbaum 			dn->dn_oldgid = dn->dn_newgid;
12700a586ceaSMark Shellenbaum 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
12710a586ceaSMark Shellenbaum 			if (dn->dn_bonuslen == 0)
12720a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
12730a586ceaSMark Shellenbaum 			else
12740a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
12750a586ceaSMark Shellenbaum 		}
127628d97a71SMark Shellenbaum 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
127714843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
127814843421SMatthew Ahrens 
127914843421SMatthew Ahrens 		list_remove(list, dn);
128014843421SMatthew Ahrens 		dnode_rele(dn, list);
128114843421SMatthew Ahrens 	}
128214843421SMatthew Ahrens }
128314843421SMatthew Ahrens 
128406e0070dSMark Shellenbaum /*
128506e0070dSMark Shellenbaum  * Returns a pointer to data to find uid/gid from
128606e0070dSMark Shellenbaum  *
128706e0070dSMark Shellenbaum  * If a dirty record for transaction group that is syncing can't
128806e0070dSMark Shellenbaum  * be found then NULL is returned.  In the NULL case it is assumed
128906e0070dSMark Shellenbaum  * the uid/gid aren't changing.
129006e0070dSMark Shellenbaum  */
129106e0070dSMark Shellenbaum static void *
129206e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
129306e0070dSMark Shellenbaum {
129406e0070dSMark Shellenbaum 	dbuf_dirty_record_t *dr, **drp;
129506e0070dSMark Shellenbaum 	void *data;
129606e0070dSMark Shellenbaum 
129706e0070dSMark Shellenbaum 	if (db->db_dirtycnt == 0)
129806e0070dSMark Shellenbaum 		return (db->db.db_data);  /* Nothing is changing */
129906e0070dSMark Shellenbaum 
130006e0070dSMark Shellenbaum 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
130106e0070dSMark Shellenbaum 		if (dr->dr_txg == tx->tx_txg)
130206e0070dSMark Shellenbaum 			break;
130306e0070dSMark Shellenbaum 
1304744947dcSTom Erickson 	if (dr == NULL) {
130506e0070dSMark Shellenbaum 		data = NULL;
1306744947dcSTom Erickson 	} else {
1307744947dcSTom Erickson 		dnode_t *dn;
1308744947dcSTom Erickson 
1309744947dcSTom Erickson 		DB_DNODE_ENTER(dr->dr_dbuf);
1310744947dcSTom Erickson 		dn = DB_DNODE(dr->dr_dbuf);
1311744947dcSTom Erickson 
1312744947dcSTom Erickson 		if (dn->dn_bonuslen == 0 &&
1313744947dcSTom Erickson 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1314744947dcSTom Erickson 			data = dr->dt.dl.dr_data->b_data;
1315744947dcSTom Erickson 		else
1316744947dcSTom Erickson 			data = dr->dt.dl.dr_data;
1317744947dcSTom Erickson 
1318744947dcSTom Erickson 		DB_DNODE_EXIT(dr->dr_dbuf);
1319744947dcSTom Erickson 	}
1320744947dcSTom Erickson 
132106e0070dSMark Shellenbaum 	return (data);
132206e0070dSMark Shellenbaum }
132306e0070dSMark Shellenbaum 
13240a586ceaSMark Shellenbaum void
132506e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
13260a586ceaSMark Shellenbaum {
13270a586ceaSMark Shellenbaum 	objset_t *os = dn->dn_objset;
13280a586ceaSMark Shellenbaum 	void *data = NULL;
132906e0070dSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
1330d5285caeSGeorge Wilson 	uint64_t *user = NULL;
1331d5285caeSGeorge Wilson 	uint64_t *group = NULL;
13320a586ceaSMark Shellenbaum 	int flags = dn->dn_id_flags;
13330a586ceaSMark Shellenbaum 	int error;
133406e0070dSMark Shellenbaum 	boolean_t have_spill = B_FALSE;
13350a586ceaSMark Shellenbaum 
13360a586ceaSMark Shellenbaum 	if (!dmu_objset_userused_enabled(dn->dn_objset))
13370a586ceaSMark Shellenbaum 		return;
13380a586ceaSMark Shellenbaum 
13390a586ceaSMark Shellenbaum 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
13400a586ceaSMark Shellenbaum 	    DN_ID_CHKED_SPILL)))
13410a586ceaSMark Shellenbaum 		return;
13420a586ceaSMark Shellenbaum 
13430a586ceaSMark Shellenbaum 	if (before && dn->dn_bonuslen != 0)
13440a586ceaSMark Shellenbaum 		data = DN_BONUS(dn->dn_phys);
134506e0070dSMark Shellenbaum 	else if (!before && dn->dn_bonuslen != 0) {
134606e0070dSMark Shellenbaum 		if (dn->dn_bonus) {
134706e0070dSMark Shellenbaum 			db = dn->dn_bonus;
134806e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
134906e0070dSMark Shellenbaum 			data = dmu_objset_userquota_find_data(db, tx);
135006e0070dSMark Shellenbaum 		} else {
135106e0070dSMark Shellenbaum 			data = DN_BONUS(dn->dn_phys);
135206e0070dSMark Shellenbaum 		}
135306e0070dSMark Shellenbaum 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
13540a586ceaSMark Shellenbaum 			int rf = 0;
13550a586ceaSMark Shellenbaum 
13560a586ceaSMark Shellenbaum 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
13570a586ceaSMark Shellenbaum 				rf |= DB_RF_HAVESTRUCT;
13581d8ccc7bSMark Shellenbaum 			error = dmu_spill_hold_by_dnode(dn,
13591d8ccc7bSMark Shellenbaum 			    rf | DB_RF_MUST_SUCCEED,
136006e0070dSMark Shellenbaum 			    FTAG, (dmu_buf_t **)&db);
13610a586ceaSMark Shellenbaum 			ASSERT(error == 0);
136206e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
136306e0070dSMark Shellenbaum 			data = (before) ? db->db.db_data :
136406e0070dSMark Shellenbaum 			    dmu_objset_userquota_find_data(db, tx);
136506e0070dSMark Shellenbaum 			have_spill = B_TRUE;
13660a586ceaSMark Shellenbaum 	} else {
13670a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
13680a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13690a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
13700a586ceaSMark Shellenbaum 		return;
13710a586ceaSMark Shellenbaum 	}
13720a586ceaSMark Shellenbaum 
13730a586ceaSMark Shellenbaum 	if (before) {
137406e0070dSMark Shellenbaum 		ASSERT(data);
13750a586ceaSMark Shellenbaum 		user = &dn->dn_olduid;
13760a586ceaSMark Shellenbaum 		group = &dn->dn_oldgid;
137706e0070dSMark Shellenbaum 	} else if (data) {
13780a586ceaSMark Shellenbaum 		user = &dn->dn_newuid;
13790a586ceaSMark Shellenbaum 		group = &dn->dn_newgid;
13800a586ceaSMark Shellenbaum 	}
13810a586ceaSMark Shellenbaum 
138206e0070dSMark Shellenbaum 	/*
138306e0070dSMark Shellenbaum 	 * Must always call the callback in case the object
138406e0070dSMark Shellenbaum 	 * type has changed and that type isn't an object type to track
138506e0070dSMark Shellenbaum 	 */
13860a586ceaSMark Shellenbaum 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
13870a586ceaSMark Shellenbaum 	    user, group);
13880a586ceaSMark Shellenbaum 
138906e0070dSMark Shellenbaum 	/*
139006e0070dSMark Shellenbaum 	 * Preserve existing uid/gid when the callback can't determine
139106e0070dSMark Shellenbaum 	 * what the new uid/gid are and the callback returned EEXIST.
139206e0070dSMark Shellenbaum 	 * The EEXIST error tells us to just use the existing uid/gid.
139306e0070dSMark Shellenbaum 	 * If we don't know what the old values are then just assign
139406e0070dSMark Shellenbaum 	 * them to 0, since that is a new file  being created.
139506e0070dSMark Shellenbaum 	 */
139606e0070dSMark Shellenbaum 	if (!before && data == NULL && error == EEXIST) {
139706e0070dSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST) {
139806e0070dSMark Shellenbaum 			dn->dn_newuid = dn->dn_olduid;
139906e0070dSMark Shellenbaum 			dn->dn_newgid = dn->dn_oldgid;
140006e0070dSMark Shellenbaum 		} else {
140106e0070dSMark Shellenbaum 			dn->dn_newuid = 0;
140206e0070dSMark Shellenbaum 			dn->dn_newgid = 0;
140306e0070dSMark Shellenbaum 		}
140406e0070dSMark Shellenbaum 		error = 0;
140506e0070dSMark Shellenbaum 	}
140606e0070dSMark Shellenbaum 
140706e0070dSMark Shellenbaum 	if (db)
140806e0070dSMark Shellenbaum 		mutex_exit(&db->db_mtx);
140906e0070dSMark Shellenbaum 
14100a586ceaSMark Shellenbaum 	mutex_enter(&dn->dn_mtx);
14110a586ceaSMark Shellenbaum 	if (error == 0 && before)
14120a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
14130a586ceaSMark Shellenbaum 	if (error == 0 && !before)
14140a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
14150a586ceaSMark Shellenbaum 
141606e0070dSMark Shellenbaum 	if (have_spill) {
14170a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
14180a586ceaSMark Shellenbaum 	} else {
14190a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
14200a586ceaSMark Shellenbaum 	}
14210a586ceaSMark Shellenbaum 	mutex_exit(&dn->dn_mtx);
142206e0070dSMark Shellenbaum 	if (have_spill)
142306e0070dSMark Shellenbaum 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
14240a586ceaSMark Shellenbaum }
14250a586ceaSMark Shellenbaum 
142614843421SMatthew Ahrens boolean_t
142714843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
142814843421SMatthew Ahrens {
1429503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
143014843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
143114843421SMatthew Ahrens }
143214843421SMatthew Ahrens 
143314843421SMatthew Ahrens int
143414843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
143514843421SMatthew Ahrens {
143614843421SMatthew Ahrens 	uint64_t obj;
143714843421SMatthew Ahrens 	int err = 0;
143814843421SMatthew Ahrens 
143914843421SMatthew Ahrens 	if (dmu_objset_userspace_present(os))
144014843421SMatthew Ahrens 		return (0);
1441503ad85cSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
1442be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
144314843421SMatthew Ahrens 	if (dmu_objset_is_snapshot(os))
1444be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
144514843421SMatthew Ahrens 
144614843421SMatthew Ahrens 	/*
144714843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
144814843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
144914843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
145014843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
145114843421SMatthew Ahrens 	 * independently.
145214843421SMatthew Ahrens 	 */
145314843421SMatthew Ahrens 
145414843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
145568857716SLin Ling 		dmu_tx_t *tx;
145614843421SMatthew Ahrens 		dmu_buf_t *db;
145714843421SMatthew Ahrens 		int objerr;
145814843421SMatthew Ahrens 
145914843421SMatthew Ahrens 		if (issig(JUSTLOOKING) && issig(FORREAL))
1460be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
146114843421SMatthew Ahrens 
146214843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
14633b2aab18SMatthew Ahrens 		if (objerr != 0)
146414843421SMatthew Ahrens 			continue;
146568857716SLin Ling 		tx = dmu_tx_create(os);
146614843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
146714843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
14683b2aab18SMatthew Ahrens 		if (objerr != 0) {
146914843421SMatthew Ahrens 			dmu_tx_abort(tx);
147014843421SMatthew Ahrens 			continue;
147114843421SMatthew Ahrens 		}
147214843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
147314843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
147414843421SMatthew Ahrens 		dmu_tx_commit(tx);
147514843421SMatthew Ahrens 	}
147614843421SMatthew Ahrens 
1477503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
147814843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
147914843421SMatthew Ahrens 	return (0);
148014843421SMatthew Ahrens }
148114843421SMatthew Ahrens 
1482fa9e4066Sahrens void
1483a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1484a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1485fa9e4066Sahrens {
1486503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1487a2eea2e1Sahrens 	    usedobjsp, availobjsp);
1488a2eea2e1Sahrens }
1489a2eea2e1Sahrens 
1490a2eea2e1Sahrens uint64_t
1491a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1492a2eea2e1Sahrens {
1493503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1494a2eea2e1Sahrens }
1495a2eea2e1Sahrens 
1496a2eea2e1Sahrens void
1497a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1498a2eea2e1Sahrens {
1499503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
1500503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
1501503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1502a2eea2e1Sahrens }
1503a2eea2e1Sahrens 
1504a2eea2e1Sahrens void
1505a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1506a2eea2e1Sahrens {
1507503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
1508503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
1509a2eea2e1Sahrens 
1510503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1511503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1512a2eea2e1Sahrens 
1513a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1514503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
151514843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
151614843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
1517fa9e4066Sahrens }
1518fa9e4066Sahrens 
1519fa9e4066Sahrens int
1520fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1521fa9e4066Sahrens {
1522503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1523bc9014e6SJustin Gibbs 		return (os->os_dsl_dataset->ds_is_snapshot);
1524fa9e4066Sahrens 	else
1525fa9e4066Sahrens 		return (B_FALSE);
1526fa9e4066Sahrens }
1527fa9e4066Sahrens 
1528ab04eb8eStimh int
1529ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1530ab04eb8eStimh     boolean_t *conflict)
1531ab04eb8eStimh {
1532503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1533ab04eb8eStimh 	uint64_t ignored;
1534ab04eb8eStimh 
1535c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1536be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1537ab04eb8eStimh 
1538ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1539c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1540c1379625SJustin T. Gibbs 	    MT_FIRST, real, maxlen, conflict));
1541ab04eb8eStimh }
1542ab04eb8eStimh 
1543fa9e4066Sahrens int
1544fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1545b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1546fa9e4066Sahrens {
1547503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1548fa9e4066Sahrens 	zap_cursor_t cursor;
1549fa9e4066Sahrens 	zap_attribute_t attr;
1550fa9e4066Sahrens 
15513b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
15523b2aab18SMatthew Ahrens 
1553c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1554be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1555fa9e4066Sahrens 
1556fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
1557fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1558c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1559fa9e4066Sahrens 
156087e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
156187e5029aSahrens 		zap_cursor_fini(&cursor);
1562be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
156387e5029aSahrens 	}
156487e5029aSahrens 
156587e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
156687e5029aSahrens 		zap_cursor_fini(&cursor);
1567be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
156887e5029aSahrens 	}
156987e5029aSahrens 
157087e5029aSahrens 	(void) strcpy(name, attr.za_name);
157187e5029aSahrens 	if (idp)
157287e5029aSahrens 		*idp = attr.za_first_integer;
1573b38f0970Sck 	if (case_conflict)
1574b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
157587e5029aSahrens 	zap_cursor_advance(&cursor);
157687e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
157787e5029aSahrens 	zap_cursor_fini(&cursor);
157887e5029aSahrens 
157987e5029aSahrens 	return (0);
158087e5029aSahrens }
158187e5029aSahrens 
158287e5029aSahrens int
158387e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
158487e5029aSahrens     uint64_t *idp, uint64_t *offp)
158587e5029aSahrens {
1586503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
158787e5029aSahrens 	zap_cursor_t cursor;
158887e5029aSahrens 	zap_attribute_t attr;
158987e5029aSahrens 
159087e5029aSahrens 	/* there is no next dir on a snapshot! */
1591503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
1592c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj)
1593be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1594fa9e4066Sahrens 
159587e5029aSahrens 	zap_cursor_init_serialized(&cursor,
159687e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
1597c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
159887e5029aSahrens 
159987e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
160087e5029aSahrens 		zap_cursor_fini(&cursor);
1601be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
160287e5029aSahrens 	}
160387e5029aSahrens 
160487e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
160587e5029aSahrens 		zap_cursor_fini(&cursor);
1606be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
160787e5029aSahrens 	}
1608fa9e4066Sahrens 
1609fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
161087e5029aSahrens 	if (idp)
161187e5029aSahrens 		*idp = attr.za_first_integer;
1612fa9e4066Sahrens 	zap_cursor_advance(&cursor);
1613fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
161487e5029aSahrens 	zap_cursor_fini(&cursor);
1615fa9e4066Sahrens 
1616fa9e4066Sahrens 	return (0);
1617fa9e4066Sahrens }
1618fa9e4066Sahrens 
1619*12380e1eSArne Jansen typedef struct dmu_objset_find_ctx {
1620*12380e1eSArne Jansen 	taskq_t		*dc_tq;
1621*12380e1eSArne Jansen 	dsl_pool_t	*dc_dp;
1622*12380e1eSArne Jansen 	uint64_t	dc_ddobj;
1623*12380e1eSArne Jansen 	int		(*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
1624*12380e1eSArne Jansen 	void		*dc_arg;
1625*12380e1eSArne Jansen 	int		dc_flags;
1626*12380e1eSArne Jansen 	kmutex_t	*dc_error_lock;
1627*12380e1eSArne Jansen 	int		*dc_error;
1628*12380e1eSArne Jansen } dmu_objset_find_ctx_t;
1629*12380e1eSArne Jansen 
1630*12380e1eSArne Jansen static void
1631*12380e1eSArne Jansen dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
1632088f3894Sahrens {
1633*12380e1eSArne Jansen 	dsl_pool_t *dp = dcp->dc_dp;
1634*12380e1eSArne Jansen 	dmu_objset_find_ctx_t *child_dcp;
16353b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
16363b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
16373b2aab18SMatthew Ahrens 	zap_cursor_t zc;
16383b2aab18SMatthew Ahrens 	zap_attribute_t *attr;
16393b2aab18SMatthew Ahrens 	uint64_t thisobj;
1640*12380e1eSArne Jansen 	int err = 0;
16413b2aab18SMatthew Ahrens 
1642*12380e1eSArne Jansen 	/* don't process if there already was an error */
1643*12380e1eSArne Jansen 	if (*dcp->dc_error != 0)
1644*12380e1eSArne Jansen 		goto out;
16453b2aab18SMatthew Ahrens 
1646*12380e1eSArne Jansen 	err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd);
16473b2aab18SMatthew Ahrens 	if (err != 0)
1648*12380e1eSArne Jansen 		goto out;
16493b2aab18SMatthew Ahrens 
16503b2aab18SMatthew Ahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
16513b2aab18SMatthew Ahrens 	if (dd->dd_myname[0] == '$') {
16523b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
1653*12380e1eSArne Jansen 		goto out;
16543b2aab18SMatthew Ahrens 	}
16553b2aab18SMatthew Ahrens 
1656c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
16573b2aab18SMatthew Ahrens 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
16583b2aab18SMatthew Ahrens 
16593b2aab18SMatthew Ahrens 	/*
16603b2aab18SMatthew Ahrens 	 * Iterate over all children.
16613b2aab18SMatthew Ahrens 	 */
1662*12380e1eSArne Jansen 	if (dcp->dc_flags & DS_FIND_CHILDREN) {
16633b2aab18SMatthew Ahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1664c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
16653b2aab18SMatthew Ahrens 		    zap_cursor_retrieve(&zc, attr) == 0;
16663b2aab18SMatthew Ahrens 		    (void) zap_cursor_advance(&zc)) {
16673b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
16683b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
16693b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
16703b2aab18SMatthew Ahrens 
1671*12380e1eSArne Jansen 			child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
1672*12380e1eSArne Jansen 			*child_dcp = *dcp;
1673*12380e1eSArne Jansen 			child_dcp->dc_ddobj = attr->za_first_integer;
1674*12380e1eSArne Jansen 			if (dcp->dc_tq != NULL)
1675*12380e1eSArne Jansen 				(void) taskq_dispatch(dcp->dc_tq,
1676*12380e1eSArne Jansen 				    dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
1677*12380e1eSArne Jansen 			else
1678*12380e1eSArne Jansen 				dmu_objset_find_dp_impl(child_dcp);
16793b2aab18SMatthew Ahrens 		}
16803b2aab18SMatthew Ahrens 		zap_cursor_fini(&zc);
16813b2aab18SMatthew Ahrens 	}
16823b2aab18SMatthew Ahrens 
16833b2aab18SMatthew Ahrens 	/*
16843b2aab18SMatthew Ahrens 	 * Iterate over all snapshots.
16853b2aab18SMatthew Ahrens 	 */
1686*12380e1eSArne Jansen 	if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
16873b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
16883b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
16893b2aab18SMatthew Ahrens 
16903b2aab18SMatthew Ahrens 		if (err == 0) {
1691c1379625SJustin T. Gibbs 			uint64_t snapobj;
1692c1379625SJustin T. Gibbs 
1693c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
16943b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
16953b2aab18SMatthew Ahrens 
16963b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
16973b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
16983b2aab18SMatthew Ahrens 			    (void) zap_cursor_advance(&zc)) {
16993b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
17003b2aab18SMatthew Ahrens 				    sizeof (uint64_t));
17013b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
17023b2aab18SMatthew Ahrens 
17033b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
17043b2aab18SMatthew Ahrens 				    attr->za_first_integer, FTAG, &ds);
17053b2aab18SMatthew Ahrens 				if (err != 0)
17063b2aab18SMatthew Ahrens 					break;
1707*12380e1eSArne Jansen 				err = dcp->dc_func(dp, ds, dcp->dc_arg);
17083b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
17093b2aab18SMatthew Ahrens 				if (err != 0)
17103b2aab18SMatthew Ahrens 					break;
17113b2aab18SMatthew Ahrens 			}
17123b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
17133b2aab18SMatthew Ahrens 		}
17143b2aab18SMatthew Ahrens 	}
17153b2aab18SMatthew Ahrens 
17163b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
17173b2aab18SMatthew Ahrens 	kmem_free(attr, sizeof (zap_attribute_t));
17183b2aab18SMatthew Ahrens 
17193b2aab18SMatthew Ahrens 	if (err != 0)
1720*12380e1eSArne Jansen 		goto out;
17213b2aab18SMatthew Ahrens 
17223b2aab18SMatthew Ahrens 	/*
17233b2aab18SMatthew Ahrens 	 * Apply to self.
17243b2aab18SMatthew Ahrens 	 */
17253b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
17263b2aab18SMatthew Ahrens 	if (err != 0)
1727*12380e1eSArne Jansen 		goto out;
1728*12380e1eSArne Jansen 	err = dcp->dc_func(dp, ds, dcp->dc_arg);
17293b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1730*12380e1eSArne Jansen 
1731*12380e1eSArne Jansen out:
1732*12380e1eSArne Jansen 	if (err != 0) {
1733*12380e1eSArne Jansen 		mutex_enter(dcp->dc_error_lock);
1734*12380e1eSArne Jansen 		/* only keep first error */
1735*12380e1eSArne Jansen 		if (*dcp->dc_error == 0)
1736*12380e1eSArne Jansen 			*dcp->dc_error = err;
1737*12380e1eSArne Jansen 		mutex_exit(dcp->dc_error_lock);
1738*12380e1eSArne Jansen 	}
1739*12380e1eSArne Jansen 
1740*12380e1eSArne Jansen 	kmem_free(dcp, sizeof (*dcp));
1741*12380e1eSArne Jansen }
1742*12380e1eSArne Jansen 
1743*12380e1eSArne Jansen static void
1744*12380e1eSArne Jansen dmu_objset_find_dp_cb(void *arg)
1745*12380e1eSArne Jansen {
1746*12380e1eSArne Jansen 	dmu_objset_find_ctx_t *dcp = arg;
1747*12380e1eSArne Jansen 	dsl_pool_t *dp = dcp->dc_dp;
1748*12380e1eSArne Jansen 
1749*12380e1eSArne Jansen 	dsl_pool_config_enter(dp, FTAG);
1750*12380e1eSArne Jansen 
1751*12380e1eSArne Jansen 	dmu_objset_find_dp_impl(dcp);
1752*12380e1eSArne Jansen 
1753*12380e1eSArne Jansen 	dsl_pool_config_exit(dp, FTAG);
1754*12380e1eSArne Jansen }
1755*12380e1eSArne Jansen 
1756*12380e1eSArne Jansen /*
1757*12380e1eSArne Jansen  * Find objsets under and including ddobj, call func(ds) on each.
1758*12380e1eSArne Jansen  * The order for the enumeration is completely undefined.
1759*12380e1eSArne Jansen  * func is called with dsl_pool_config held.
1760*12380e1eSArne Jansen  */
1761*12380e1eSArne Jansen int
1762*12380e1eSArne Jansen dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
1763*12380e1eSArne Jansen     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1764*12380e1eSArne Jansen {
1765*12380e1eSArne Jansen 	int error = 0;
1766*12380e1eSArne Jansen 	taskq_t *tq = NULL;
1767*12380e1eSArne Jansen 	int ntasks;
1768*12380e1eSArne Jansen 	dmu_objset_find_ctx_t *dcp;
1769*12380e1eSArne Jansen 	kmutex_t err_lock;
1770*12380e1eSArne Jansen 
1771*12380e1eSArne Jansen 	mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
1772*12380e1eSArne Jansen 	dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
1773*12380e1eSArne Jansen 	dcp->dc_tq = NULL;
1774*12380e1eSArne Jansen 	dcp->dc_dp = dp;
1775*12380e1eSArne Jansen 	dcp->dc_ddobj = ddobj;
1776*12380e1eSArne Jansen 	dcp->dc_func = func;
1777*12380e1eSArne Jansen 	dcp->dc_arg = arg;
1778*12380e1eSArne Jansen 	dcp->dc_flags = flags;
1779*12380e1eSArne Jansen 	dcp->dc_error_lock = &err_lock;
1780*12380e1eSArne Jansen 	dcp->dc_error = &error;
1781*12380e1eSArne Jansen 
1782*12380e1eSArne Jansen 	if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
1783*12380e1eSArne Jansen 		/*
1784*12380e1eSArne Jansen 		 * In case a write lock is held we can't make use of
1785*12380e1eSArne Jansen 		 * parallelism, as down the stack of the worker threads
1786*12380e1eSArne Jansen 		 * the lock is asserted via dsl_pool_config_held.
1787*12380e1eSArne Jansen 		 * In case of a read lock this is solved by getting a read
1788*12380e1eSArne Jansen 		 * lock in each worker thread, which isn't possible in case
1789*12380e1eSArne Jansen 		 * of a writer lock. So we fall back to the synchronous path
1790*12380e1eSArne Jansen 		 * here.
1791*12380e1eSArne Jansen 		 * In the future it might be possible to get some magic into
1792*12380e1eSArne Jansen 		 * dsl_pool_config_held in a way that it returns true for
1793*12380e1eSArne Jansen 		 * the worker threads so that a single lock held from this
1794*12380e1eSArne Jansen 		 * thread suffices. For now, stay single threaded.
1795*12380e1eSArne Jansen 		 */
1796*12380e1eSArne Jansen 		dmu_objset_find_dp_impl(dcp);
1797*12380e1eSArne Jansen 
1798*12380e1eSArne Jansen 		return (error);
1799*12380e1eSArne Jansen 	}
1800*12380e1eSArne Jansen 
1801*12380e1eSArne Jansen 	ntasks = dmu_find_threads;
1802*12380e1eSArne Jansen 	if (ntasks == 0)
1803*12380e1eSArne Jansen 		ntasks = vdev_count_leaves(dp->dp_spa) * 4;
1804*12380e1eSArne Jansen 	tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
1805*12380e1eSArne Jansen 	    INT_MAX, 0);
1806*12380e1eSArne Jansen 	if (tq == NULL) {
1807*12380e1eSArne Jansen 		kmem_free(dcp, sizeof (*dcp));
1808*12380e1eSArne Jansen 		return (SET_ERROR(ENOMEM));
1809*12380e1eSArne Jansen 	}
1810*12380e1eSArne Jansen 	dcp->dc_tq = tq;
1811*12380e1eSArne Jansen 
1812*12380e1eSArne Jansen 	/* dcp will be freed by task */
1813*12380e1eSArne Jansen 	(void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
1814*12380e1eSArne Jansen 
1815*12380e1eSArne Jansen 	/*
1816*12380e1eSArne Jansen 	 * PORTING: this code relies on the property of taskq_wait to wait
1817*12380e1eSArne Jansen 	 * until no more tasks are queued and no more tasks are active. As
1818*12380e1eSArne Jansen 	 * we always queue new tasks from within other tasks, task_wait
1819*12380e1eSArne Jansen 	 * reliably waits for the full recursion to finish, even though we
1820*12380e1eSArne Jansen 	 * enqueue new tasks after taskq_wait has been called.
1821*12380e1eSArne Jansen 	 * On platforms other than illumos, taskq_wait may not have this
1822*12380e1eSArne Jansen 	 * property.
1823*12380e1eSArne Jansen 	 */
1824*12380e1eSArne Jansen 	taskq_wait(tq);
1825*12380e1eSArne Jansen 	taskq_destroy(tq);
1826*12380e1eSArne Jansen 	mutex_destroy(&err_lock);
1827*12380e1eSArne Jansen 
1828*12380e1eSArne Jansen 	return (error);
1829088f3894Sahrens }
1830088f3894Sahrens 
1831088f3894Sahrens /*
18323b2aab18SMatthew Ahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
18333b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when this is called, and it
18343b2aab18SMatthew Ahrens  * will not be held when the callback is called.
18353b2aab18SMatthew Ahrens  * Therefore this function should only be used when the pool is not changing
18363b2aab18SMatthew Ahrens  * (e.g. in syncing context), or the callback can deal with the possible races.
1837088f3894Sahrens  */
18383b2aab18SMatthew Ahrens static int
18393b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name,
18403b2aab18SMatthew Ahrens     int func(const char *, void *), void *arg, int flags)
1841fa9e4066Sahrens {
1842fa9e4066Sahrens 	dsl_dir_t *dd;
18433b2aab18SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
1844088f3894Sahrens 	dsl_dataset_t *ds;
1845fa9e4066Sahrens 	zap_cursor_t zc;
1846b7661cccSmmusante 	zap_attribute_t *attr;
1847fa9e4066Sahrens 	char *child;
1848088f3894Sahrens 	uint64_t thisobj;
1849088f3894Sahrens 	int err;
1850fa9e4066Sahrens 
18513b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dp, FTAG);
18523b2aab18SMatthew Ahrens 
18533b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
18543b2aab18SMatthew Ahrens 	if (err != 0) {
18553b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
18561d452cf5Sahrens 		return (err);
18573b2aab18SMatthew Ahrens 	}
1858fa9e4066Sahrens 
1859088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1860088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
18613b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
18623b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
1863088f3894Sahrens 		return (0);
1864088f3894Sahrens 	}
1865088f3894Sahrens 
1866c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1867b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1868fa9e4066Sahrens 
1869fa9e4066Sahrens 	/*
1870fa9e4066Sahrens 	 * Iterate over all children.
1871fa9e4066Sahrens 	 */
18720b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
1873088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1874c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
1875b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
18760b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
18773b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
18783b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
18793b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
1880fa9e4066Sahrens 
1881486ae710SMatthew Ahrens 			child = kmem_asprintf("%s/%s", name, attr->za_name);
18823b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
18833b2aab18SMatthew Ahrens 			err = dmu_objset_find_impl(spa, child,
18843b2aab18SMatthew Ahrens 			    func, arg, flags);
18853b2aab18SMatthew Ahrens 			dsl_pool_config_enter(dp, FTAG);
1886486ae710SMatthew Ahrens 			strfree(child);
18873b2aab18SMatthew Ahrens 			if (err != 0)
18880b69c2f0Sahrens 				break;
18890b69c2f0Sahrens 		}
18900b69c2f0Sahrens 		zap_cursor_fini(&zc);
18911d452cf5Sahrens 
18923b2aab18SMatthew Ahrens 		if (err != 0) {
18933b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
18943b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
1895b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
18960b69c2f0Sahrens 			return (err);
18970b69c2f0Sahrens 		}
1898fa9e4066Sahrens 	}
1899fa9e4066Sahrens 
1900fa9e4066Sahrens 	/*
1901fa9e4066Sahrens 	 * Iterate over all snapshots.
1902fa9e4066Sahrens 	 */
1903088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
1904088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1905088f3894Sahrens 
1906088f3894Sahrens 		if (err == 0) {
1907c1379625SJustin T. Gibbs 			uint64_t snapobj;
1908c1379625SJustin T. Gibbs 
1909c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1910088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
1911088f3894Sahrens 
1912088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1913088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
1914088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
19153b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
1916088f3894Sahrens 				    sizeof (uint64_t));
19173b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
1918088f3894Sahrens 
1919486ae710SMatthew Ahrens 				child = kmem_asprintf("%s@%s",
1920486ae710SMatthew Ahrens 				    name, attr->za_name);
19213b2aab18SMatthew Ahrens 				dsl_pool_config_exit(dp, FTAG);
19223b2aab18SMatthew Ahrens 				err = func(child, arg);
19233b2aab18SMatthew Ahrens 				dsl_pool_config_enter(dp, FTAG);
1924486ae710SMatthew Ahrens 				strfree(child);
19253b2aab18SMatthew Ahrens 				if (err != 0)
1926088f3894Sahrens 					break;
1927088f3894Sahrens 			}
1928088f3894Sahrens 			zap_cursor_fini(&zc);
1929fa9e4066Sahrens 		}
1930fa9e4066Sahrens 	}
1931fa9e4066Sahrens 
19323b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
1933b7661cccSmmusante 	kmem_free(attr, sizeof (zap_attribute_t));
19343b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dp, FTAG);
1935fa9e4066Sahrens 
19363b2aab18SMatthew Ahrens 	if (err != 0)
19371d452cf5Sahrens 		return (err);
19381d452cf5Sahrens 
19393b2aab18SMatthew Ahrens 	/* Apply to self. */
19403b2aab18SMatthew Ahrens 	return (func(name, arg));
1941fa9e4066Sahrens }
1942f18faf3fSek 
19433b2aab18SMatthew Ahrens /*
19443b2aab18SMatthew Ahrens  * See comment above dmu_objset_find_impl().
19453b2aab18SMatthew Ahrens  */
19467f73c863SRich Morris int
19473b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg,
19483b2aab18SMatthew Ahrens     int flags)
19497f73c863SRich Morris {
19503b2aab18SMatthew Ahrens 	spa_t *spa;
19513b2aab18SMatthew Ahrens 	int error;
19527f73c863SRich Morris 
19533b2aab18SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
19543b2aab18SMatthew Ahrens 	if (error != 0)
19553b2aab18SMatthew Ahrens 		return (error);
19563b2aab18SMatthew Ahrens 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
19573b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
19583b2aab18SMatthew Ahrens 	return (error);
19597f73c863SRich Morris }
19607f73c863SRich Morris 
1961f18faf3fSek void
1962f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr)
1963f18faf3fSek {
1964503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1965503ad85cSMatthew Ahrens 	os->os_user_ptr = user_ptr;
1966f18faf3fSek }
1967f18faf3fSek 
1968f18faf3fSek void *
1969f18faf3fSek dmu_objset_get_user(objset_t *os)
1970f18faf3fSek {
1971503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1972503ad85cSMatthew Ahrens 	return (os->os_user_ptr);
1973f18faf3fSek }
19743b2aab18SMatthew Ahrens 
19753b2aab18SMatthew Ahrens /*
19763b2aab18SMatthew Ahrens  * Determine name of filesystem, given name of snapshot.
19773b2aab18SMatthew Ahrens  * buf must be at least MAXNAMELEN bytes
19783b2aab18SMatthew Ahrens  */
19793b2aab18SMatthew Ahrens int
19803b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf)
19813b2aab18SMatthew Ahrens {
19823b2aab18SMatthew Ahrens 	char *atp = strchr(snapname, '@');
19833b2aab18SMatthew Ahrens 	if (atp == NULL)
1984be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
19853b2aab18SMatthew Ahrens 	if (atp - snapname >= MAXNAMELEN)
1986be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
19873b2aab18SMatthew Ahrens 	(void) strlcpy(buf, snapname, atp - snapname + 1);
19883b2aab18SMatthew Ahrens 	return (0);
19893b2aab18SMatthew Ahrens }
1990