xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8)
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.
2812380e1eSArne 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>
5312380e1eSArne 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 
6112380e1eSArne Jansen /*
6212380e1eSArne Jansen  * Tunable to overwrite the maximum number of threads for the parallization
6312380e1eSArne Jansen  * of dmu_objset_find_dp, needed to speed up the import of pools with many
6412380e1eSArne Jansen  * datasets.
6512380e1eSArne Jansen  * Default is 4 times the number of leaf vdevs.
6612380e1eSArne Jansen  */
6712380e1eSArne Jansen int dmu_find_threads = 0;
6812380e1eSArne Jansen 
6912380e1eSArne Jansen static void dmu_objset_find_dp_cb(void *arg);
7012380e1eSArne 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) {
365*9c3fd121SMatthew Ahrens 		boolean_t needlock = B_FALSE;
366*9c3fd121SMatthew Ahrens 
367*9c3fd121SMatthew Ahrens 		/*
368*9c3fd121SMatthew Ahrens 		 * Note: it's valid to open the objset if the dataset is
369*9c3fd121SMatthew Ahrens 		 * long-held, in which case the pool_config lock will not
370*9c3fd121SMatthew Ahrens 		 * be held.
371*9c3fd121SMatthew Ahrens 		 */
372*9c3fd121SMatthew Ahrens 		if (!dsl_pool_config_held(dmu_objset_pool(os))) {
373*9c3fd121SMatthew Ahrens 			needlock = B_TRUE;
374*9c3fd121SMatthew Ahrens 			dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
375*9c3fd121SMatthew Ahrens 		}
3763b2aab18SMatthew Ahrens 		err = dsl_prop_register(ds,
3773b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
378503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
3793b2aab18SMatthew Ahrens 		if (err == 0) {
3803b2aab18SMatthew Ahrens 			err = dsl_prop_register(ds,
3813b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
382503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
3833b2aab18SMatthew Ahrens 		}
384bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
3853b2aab18SMatthew Ahrens 			if (err == 0) {
3863b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3873b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
388503ad85cSMatthew Ahrens 				    checksum_changed_cb, os);
3893b2aab18SMatthew Ahrens 			}
3903b2aab18SMatthew Ahrens 			if (err == 0) {
3913b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3923b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
393503ad85cSMatthew Ahrens 				    compression_changed_cb, os);
3943b2aab18SMatthew Ahrens 			}
3953b2aab18SMatthew Ahrens 			if (err == 0) {
3963b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3973b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COPIES),
398503ad85cSMatthew Ahrens 				    copies_changed_cb, os);
3993b2aab18SMatthew Ahrens 			}
4003b2aab18SMatthew Ahrens 			if (err == 0) {
4013b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
4023b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_DEDUP),
403b24ab676SJeff Bonwick 				    dedup_changed_cb, os);
4043b2aab18SMatthew Ahrens 			}
4053b2aab18SMatthew Ahrens 			if (err == 0) {
4063b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
4073b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
408e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
4093b2aab18SMatthew Ahrens 			}
4103b2aab18SMatthew Ahrens 			if (err == 0) {
4113b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
4123b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_SYNC),
41355da60b9SMark J Musante 				    sync_changed_cb, os);
4143b2aab18SMatthew Ahrens 			}
415edf345e6SMatthew Ahrens 			if (err == 0) {
416edf345e6SMatthew Ahrens 				err = dsl_prop_register(ds,
417edf345e6SMatthew Ahrens 				    zfs_prop_to_name(
418edf345e6SMatthew Ahrens 				    ZFS_PROP_REDUNDANT_METADATA),
419edf345e6SMatthew Ahrens 				    redundant_metadata_changed_cb, os);
420edf345e6SMatthew Ahrens 			}
421b5152584SMatthew Ahrens 			if (err == 0) {
422b5152584SMatthew Ahrens 				err = dsl_prop_register(ds,
423b5152584SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
424b5152584SMatthew Ahrens 				    recordsize_changed_cb, os);
425b5152584SMatthew Ahrens 			}
4263baa08fcSek 		}
427*9c3fd121SMatthew Ahrens 		if (needlock)
428*9c3fd121SMatthew Ahrens 			dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
4293b2aab18SMatthew Ahrens 		if (err != 0) {
430503ad85cSMatthew Ahrens 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
4313b2aab18SMatthew Ahrens 			    &os->os_phys_buf));
432503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
433ea8dc4b6Seschrock 			return (err);
434ea8dc4b6Seschrock 		}
4355d7b4d43SMatthew Ahrens 	} else {
436fa9e4066Sahrens 		/* It's the meta-objset. */
437503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
438db1741f5SJustin T. Gibbs 		os->os_compress = ZIO_COMPRESS_ON;
439503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
440b24ab676SJeff Bonwick 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
441edf345e6SMatthew Ahrens 		os->os_dedup_verify = B_FALSE;
442edf345e6SMatthew Ahrens 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
443edf345e6SMatthew Ahrens 		os->os_sync = ZFS_SYNC_STANDARD;
444503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
445503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
446fa9e4066Sahrens 	}
447fa9e4066Sahrens 
448bc9014e6SJustin Gibbs 	if (ds == NULL || !ds->ds_is_snapshot)
4496e0cbcaaSMatthew Ahrens 		os->os_zil_header = os->os_phys->os_zil_header;
450503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
451fa9e4066Sahrens 
452fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
453503ad85cSMatthew Ahrens 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
454fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
455503ad85cSMatthew Ahrens 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
456fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
457fa9e4066Sahrens 	}
458503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
459fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
460503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
461fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
462fa9e4066Sahrens 
463503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
464503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
465503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
466503ad85cSMatthew Ahrens 
467bc9014e6SJustin Gibbs 	dnode_special_open(os, &os->os_phys->os_meta_dnode,
468bc9014e6SJustin Gibbs 	    DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
469503ad85cSMatthew Ahrens 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
470bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_userused_dnode,
471bc9014e6SJustin Gibbs 		    DMU_USERUSED_OBJECT, &os->os_userused_dnode);
472bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_groupused_dnode,
473bc9014e6SJustin Gibbs 		    DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
47414843421SMatthew Ahrens 	}
475fa9e4066Sahrens 
476503ad85cSMatthew Ahrens 	*osp = os;
477ea8dc4b6Seschrock 	return (0);
478fa9e4066Sahrens }
479fa9e4066Sahrens 
480503ad85cSMatthew Ahrens int
481503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4823cb34c60Sahrens {
483503ad85cSMatthew Ahrens 	int err = 0;
4843cb34c60Sahrens 
485*9c3fd121SMatthew Ahrens 	/*
486*9c3fd121SMatthew Ahrens 	 * We shouldn't be doing anything with dsl_dataset_t's unless the
487*9c3fd121SMatthew Ahrens 	 * pool_config lock is held, or the dataset is long-held.
488*9c3fd121SMatthew Ahrens 	 */
489*9c3fd121SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
490*9c3fd121SMatthew Ahrens 	    dsl_dataset_long_held(ds));
491*9c3fd121SMatthew Ahrens 
4923cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
4935d7b4d43SMatthew Ahrens 	if (ds->ds_objset == NULL) {
4945d7b4d43SMatthew Ahrens 		objset_t *os;
4953cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
4965d7b4d43SMatthew Ahrens 		    ds, dsl_dataset_get_blkptr(ds), &os);
4975d7b4d43SMatthew Ahrens 
4985d7b4d43SMatthew Ahrens 		if (err == 0) {
4995d7b4d43SMatthew Ahrens 			mutex_enter(&ds->ds_lock);
5005d7b4d43SMatthew Ahrens 			ASSERT(ds->ds_objset == NULL);
5015d7b4d43SMatthew Ahrens 			ds->ds_objset = os;
5025d7b4d43SMatthew Ahrens 			mutex_exit(&ds->ds_lock);
5035d7b4d43SMatthew Ahrens 		}
5043cb34c60Sahrens 	}
5055d7b4d43SMatthew Ahrens 	*osp = ds->ds_objset;
5063cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
507503ad85cSMatthew Ahrens 	return (err);
5083cb34c60Sahrens }
5093cb34c60Sahrens 
5103b2aab18SMatthew Ahrens /*
5113b2aab18SMatthew Ahrens  * Holds the pool while the objset is held.  Therefore only one objset
5123b2aab18SMatthew Ahrens  * can be held at a time.
5133b2aab18SMatthew Ahrens  */
5143cb34c60Sahrens int
515503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
5163cb34c60Sahrens {
5173b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
518503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
5193cb34c60Sahrens 	int err;
5203cb34c60Sahrens 
5213b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, tag, &dp);
5223b2aab18SMatthew Ahrens 	if (err != 0)
5233b2aab18SMatthew Ahrens 		return (err);
5243b2aab18SMatthew Ahrens 	err = dsl_dataset_hold(dp, name, tag, &ds);
5253b2aab18SMatthew Ahrens 	if (err != 0) {
5263b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
527503ad85cSMatthew Ahrens 		return (err);
5283b2aab18SMatthew Ahrens 	}
529503ad85cSMatthew Ahrens 
530503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
5313b2aab18SMatthew Ahrens 	if (err != 0) {
532503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
5333b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
5343b2aab18SMatthew Ahrens 	}
535503ad85cSMatthew Ahrens 
5363cb34c60Sahrens 	return (err);
5373cb34c60Sahrens }
5383cb34c60Sahrens 
53912380e1eSArne Jansen static int
54012380e1eSArne Jansen dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
54112380e1eSArne Jansen     boolean_t readonly, void *tag, objset_t **osp)
54212380e1eSArne Jansen {
54312380e1eSArne Jansen 	int err;
54412380e1eSArne Jansen 
54512380e1eSArne Jansen 	err = dmu_objset_from_ds(ds, osp);
54612380e1eSArne Jansen 	if (err != 0) {
54712380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
54812380e1eSArne Jansen 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
54912380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
55012380e1eSArne Jansen 		return (SET_ERROR(EINVAL));
55112380e1eSArne Jansen 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
55212380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
55312380e1eSArne Jansen 		return (SET_ERROR(EROFS));
55412380e1eSArne Jansen 	}
55512380e1eSArne Jansen 	return (err);
55612380e1eSArne Jansen }
55712380e1eSArne Jansen 
5583b2aab18SMatthew Ahrens /*
5593b2aab18SMatthew Ahrens  * dsl_pool must not be held when this is called.
5603b2aab18SMatthew Ahrens  * Upon successful return, there will be a longhold on the dataset,
5613b2aab18SMatthew Ahrens  * and the dsl_pool will not be held.
5623b2aab18SMatthew Ahrens  */
563fa9e4066Sahrens int
564503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
565503ad85cSMatthew Ahrens     boolean_t readonly, void *tag, objset_t **osp)
566fa9e4066Sahrens {
5673b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
568f18faf3fSek 	dsl_dataset_t *ds;
569f18faf3fSek 	int err;
570fa9e4066Sahrens 
5713b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
5723b2aab18SMatthew Ahrens 	if (err != 0)
5733b2aab18SMatthew Ahrens 		return (err);
5743b2aab18SMatthew Ahrens 	err = dsl_dataset_own(dp, name, tag, &ds);
5753b2aab18SMatthew Ahrens 	if (err != 0) {
5763b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
577fa9e4066Sahrens 		return (err);
5783b2aab18SMatthew Ahrens 	}
57912380e1eSArne Jansen 	err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
5803b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
58112380e1eSArne Jansen 
5823cb34c60Sahrens 	return (err);
583fa9e4066Sahrens }
584fa9e4066Sahrens 
58512380e1eSArne Jansen int
58612380e1eSArne Jansen dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
58712380e1eSArne Jansen     boolean_t readonly, void *tag, objset_t **osp)
58812380e1eSArne Jansen {
58912380e1eSArne Jansen 	dsl_dataset_t *ds;
59012380e1eSArne Jansen 	int err;
59112380e1eSArne Jansen 
59212380e1eSArne Jansen 	err = dsl_dataset_own_obj(dp, obj, tag, &ds);
59312380e1eSArne Jansen 	if (err != 0)
59412380e1eSArne Jansen 		return (err);
59512380e1eSArne Jansen 
59612380e1eSArne Jansen 	return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
59712380e1eSArne Jansen }
59812380e1eSArne Jansen 
599fa9e4066Sahrens void
600503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
601fa9e4066Sahrens {
6023b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(os);
603503ad85cSMatthew Ahrens 	dsl_dataset_rele(os->os_dsl_dataset, tag);
6043b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, tag);
605503ad85cSMatthew Ahrens }
606503ad85cSMatthew Ahrens 
60791948b51SKeith M Wesolowski /*
60891948b51SKeith M Wesolowski  * When we are called, os MUST refer to an objset associated with a dataset
60991948b51SKeith M Wesolowski  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
61091948b51SKeith M Wesolowski  * == tag.  We will then release and reacquire ownership of the dataset while
61191948b51SKeith M Wesolowski  * holding the pool config_rwlock to avoid intervening namespace or ownership
61291948b51SKeith M Wesolowski  * changes may occur.
61391948b51SKeith M Wesolowski  *
61491948b51SKeith M Wesolowski  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
61591948b51SKeith M Wesolowski  * release the hold on its dataset and acquire a new one on the dataset of the
61691948b51SKeith M Wesolowski  * same name so that it can be partially torn down and reconstructed.
61791948b51SKeith M Wesolowski  */
61891948b51SKeith M Wesolowski void
61991948b51SKeith M Wesolowski dmu_objset_refresh_ownership(objset_t *os, void *tag)
62091948b51SKeith M Wesolowski {
62191948b51SKeith M Wesolowski 	dsl_pool_t *dp;
62291948b51SKeith M Wesolowski 	dsl_dataset_t *ds, *newds;
62391948b51SKeith M Wesolowski 	char name[MAXNAMELEN];
62491948b51SKeith M Wesolowski 
62591948b51SKeith M Wesolowski 	ds = os->os_dsl_dataset;
62691948b51SKeith M Wesolowski 	VERIFY3P(ds, !=, NULL);
62791948b51SKeith M Wesolowski 	VERIFY3P(ds->ds_owner, ==, tag);
62891948b51SKeith M Wesolowski 	VERIFY(dsl_dataset_long_held(ds));
62991948b51SKeith M Wesolowski 
63091948b51SKeith M Wesolowski 	dsl_dataset_name(ds, name);
63191948b51SKeith M Wesolowski 	dp = dmu_objset_pool(os);
63291948b51SKeith M Wesolowski 	dsl_pool_config_enter(dp, FTAG);
63391948b51SKeith M Wesolowski 	dmu_objset_disown(os, tag);
63491948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
63591948b51SKeith M Wesolowski 	VERIFY3P(newds, ==, os->os_dsl_dataset);
63691948b51SKeith M Wesolowski 	dsl_pool_config_exit(dp, FTAG);
63791948b51SKeith M Wesolowski }
63891948b51SKeith M Wesolowski 
639503ad85cSMatthew Ahrens void
640503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
641503ad85cSMatthew Ahrens {
642503ad85cSMatthew Ahrens 	dsl_dataset_disown(os->os_dsl_dataset, tag);
643fa9e4066Sahrens }
644fa9e4066Sahrens 
6453b2aab18SMatthew Ahrens void
6461934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
647ea8dc4b6Seschrock {
648bc9014e6SJustin Gibbs 	dnode_t dn_marker;
649ea8dc4b6Seschrock 	dnode_t *dn;
650c543ec06Sahrens 
651503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
652bc9014e6SJustin Gibbs 	dn = list_head(&os->os_dnodes);
653bc9014e6SJustin Gibbs 	while (dn != NULL) {
654bc9014e6SJustin Gibbs 		/*
655bc9014e6SJustin Gibbs 		 * Skip dnodes without holds.  We have to do this dance
656bc9014e6SJustin Gibbs 		 * because dnode_add_ref() only works if there is already a
657bc9014e6SJustin Gibbs 		 * hold.  If the dnode has no holds, then it has no dbufs.
658bc9014e6SJustin Gibbs 		 */
659bc9014e6SJustin Gibbs 		if (dnode_add_ref(dn, FTAG)) {
660bc9014e6SJustin Gibbs 			list_insert_after(&os->os_dnodes, dn, &dn_marker);
661bc9014e6SJustin Gibbs 			mutex_exit(&os->os_lock);
662c543ec06Sahrens 
663bc9014e6SJustin Gibbs 			dnode_evict_dbufs(dn);
664bc9014e6SJustin Gibbs 			dnode_rele(dn, FTAG);
665c543ec06Sahrens 
666bc9014e6SJustin Gibbs 			mutex_enter(&os->os_lock);
667bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, &dn_marker);
668bc9014e6SJustin Gibbs 			list_remove(&os->os_dnodes, &dn_marker);
669bc9014e6SJustin Gibbs 		} else {
670bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, dn);
671bc9014e6SJustin Gibbs 		}
672ea8dc4b6Seschrock 	}
673503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
674bc9014e6SJustin Gibbs 
675bc9014e6SJustin Gibbs 	if (DMU_USERUSED_DNODE(os) != NULL) {
676bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
677bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
678bc9014e6SJustin Gibbs 	}
679bc9014e6SJustin Gibbs 	dnode_evict_dbufs(DMU_META_DNODE(os));
680ea8dc4b6Seschrock }
681ea8dc4b6Seschrock 
682bc9014e6SJustin Gibbs /*
683bc9014e6SJustin Gibbs  * Objset eviction processing is split into into two pieces.
684bc9014e6SJustin Gibbs  * The first marks the objset as evicting, evicts any dbufs that
685bc9014e6SJustin Gibbs  * have a refcount of zero, and then queues up the objset for the
686bc9014e6SJustin Gibbs  * second phase of eviction.  Once os->os_dnodes has been cleared by
687bc9014e6SJustin Gibbs  * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
688bc9014e6SJustin Gibbs  * The second phase closes the special dnodes, dequeues the objset from
689bc9014e6SJustin Gibbs  * the list of those undergoing eviction, and finally frees the objset.
690bc9014e6SJustin Gibbs  *
691bc9014e6SJustin Gibbs  * NOTE: Due to asynchronous eviction processing (invocation of
692bc9014e6SJustin Gibbs  *       dnode_buf_pageout()), it is possible for the meta dnode for the
693bc9014e6SJustin Gibbs  *       objset to have no holds even though os->os_dnodes is not empty.
694bc9014e6SJustin Gibbs  */
695fa9e4066Sahrens void
696503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
697fa9e4066Sahrens {
698503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
699fa9e4066Sahrens 
700b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
701b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, t));
702fa9e4066Sahrens 
70303bad06fSJustin Gibbs 	if (ds)
70403bad06fSJustin Gibbs 		dsl_prop_unregister_all(ds, os);
705fa9e4066Sahrens 
7060a586ceaSMark Shellenbaum 	if (os->os_sa)
7070a586ceaSMark Shellenbaum 		sa_tear_down(os);
7080a586ceaSMark Shellenbaum 
709bc9014e6SJustin Gibbs 	os->os_evicting = B_TRUE;
7103b2aab18SMatthew Ahrens 	dmu_objset_evict_dbufs(os);
711ea8dc4b6Seschrock 
712bc9014e6SJustin Gibbs 	mutex_enter(&os->os_lock);
713bc9014e6SJustin Gibbs 	spa_evicting_os_register(os->os_spa, os);
714bc9014e6SJustin Gibbs 	if (list_is_empty(&os->os_dnodes)) {
715bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
716bc9014e6SJustin Gibbs 		dmu_objset_evict_done(os);
717bc9014e6SJustin Gibbs 	} else {
718bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
719bc9014e6SJustin Gibbs 	}
720bc9014e6SJustin Gibbs }
721bc9014e6SJustin Gibbs 
722bc9014e6SJustin Gibbs void
723bc9014e6SJustin Gibbs dmu_objset_evict_done(objset_t *os)
724bc9014e6SJustin Gibbs {
725bc9014e6SJustin Gibbs 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
726bc9014e6SJustin Gibbs 
727744947dcSTom Erickson 	dnode_special_close(&os->os_meta_dnode);
728744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os)) {
729744947dcSTom Erickson 		dnode_special_close(&os->os_userused_dnode);
730744947dcSTom Erickson 		dnode_special_close(&os->os_groupused_dnode);
73114843421SMatthew Ahrens 	}
732503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
733fa9e4066Sahrens 
7343b2aab18SMatthew Ahrens 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
735744947dcSTom Erickson 
736744947dcSTom Erickson 	/*
737744947dcSTom Erickson 	 * This is a barrier to prevent the objset from going away in
738744947dcSTom Erickson 	 * dnode_move() until we can safely ensure that the objset is still in
739744947dcSTom Erickson 	 * use. We consider the objset valid before the barrier and invalid
740744947dcSTom Erickson 	 * after the barrier.
741744947dcSTom Erickson 	 */
742744947dcSTom Erickson 	rw_enter(&os_lock, RW_READER);
743744947dcSTom Erickson 	rw_exit(&os_lock);
744744947dcSTom Erickson 
745503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
746503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
747503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
748bc9014e6SJustin Gibbs 	spa_evicting_os_deregister(os->os_spa, os);
749503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
750fa9e4066Sahrens }
751fa9e4066Sahrens 
75271eb0538SChris Kirby timestruc_t
75371eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
75471eb0538SChris Kirby {
75571eb0538SChris Kirby 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
75671eb0538SChris Kirby }
75771eb0538SChris Kirby 
758fa9e4066Sahrens /* called from dsl for meta-objset */
759503ad85cSMatthew Ahrens objset_t *
760c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
761c717a561Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
762fa9e4066Sahrens {
763503ad85cSMatthew Ahrens 	objset_t *os;
764fa9e4066Sahrens 	dnode_t *mdn;
765fa9e4066Sahrens 
766fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7673b2aab18SMatthew Ahrens 
768feaa74e4SMark Maybee 	if (ds != NULL)
7693b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_from_ds(ds, &os));
770feaa74e4SMark Maybee 	else
7713b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
772feaa74e4SMark Maybee 
773744947dcSTom Erickson 	mdn = DMU_META_DNODE(os);
774fa9e4066Sahrens 
775fa9e4066Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
776fa9e4066Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
777fa9e4066Sahrens 
778fa9e4066Sahrens 	/*
779fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
780fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
781fa9e4066Sahrens 	 * we are also accessing it in open context.
782fa9e4066Sahrens 	 *
783fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
784fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
785fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
786fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
787fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
788fa9e4066Sahrens 	 */
789ea8dc4b6Seschrock 	if (ds != NULL) {
790ea8dc4b6Seschrock 		int levels = 1;
791ea8dc4b6Seschrock 
792ea8dc4b6Seschrock 		/*
793ea8dc4b6Seschrock 		 * Determine the number of levels necessary for the meta-dnode
794ea8dc4b6Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
795ea8dc4b6Seschrock 		 */
796ea8dc4b6Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
797ea8dc4b6Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
798ea8dc4b6Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
799ea8dc4b6Seschrock 			levels++;
800ea8dc4b6Seschrock 
801fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
802ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
803ea8dc4b6Seschrock 	}
804fa9e4066Sahrens 
805fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
806fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
807fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
808503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
809503ad85cSMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
810503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
811503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
81214843421SMatthew Ahrens 	}
813fa9e4066Sahrens 
814fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
815fa9e4066Sahrens 
816503ad85cSMatthew Ahrens 	return (os);
817fa9e4066Sahrens }
818fa9e4066Sahrens 
8193b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg {
8203b2aab18SMatthew Ahrens 	const char *doca_name;
8213b2aab18SMatthew Ahrens 	cred_t *doca_cred;
8223b2aab18SMatthew Ahrens 	void (*doca_userfunc)(objset_t *os, void *arg,
8233b2aab18SMatthew Ahrens 	    cred_t *cr, dmu_tx_t *tx);
8243b2aab18SMatthew Ahrens 	void *doca_userarg;
8253b2aab18SMatthew Ahrens 	dmu_objset_type_t doca_type;
8263b2aab18SMatthew Ahrens 	uint64_t doca_flags;
8273b2aab18SMatthew Ahrens } dmu_objset_create_arg_t;
828fa9e4066Sahrens 
829ecd6cf80Smarks /*ARGSUSED*/
830fa9e4066Sahrens static int
8313b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx)
832fa9e4066Sahrens {
8333b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
8343b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8353b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
8363b2aab18SMatthew Ahrens 	const char *tail;
8373b2aab18SMatthew Ahrens 	int error;
8381d452cf5Sahrens 
8393b2aab18SMatthew Ahrens 	if (strchr(doca->doca_name, '@') != NULL)
840be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8411d452cf5Sahrens 
8423b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
8433b2aab18SMatthew Ahrens 	if (error != 0)
8443b2aab18SMatthew Ahrens 		return (error);
8453b2aab18SMatthew Ahrens 	if (tail == NULL) {
8463b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
847be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
8481d452cf5Sahrens 	}
849a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
850a2afb611SJerry Jelinek 	    doca->doca_cred);
8513b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
852ecd6cf80Smarks 
853a2afb611SJerry Jelinek 	return (error);
8541d452cf5Sahrens }
8551d452cf5Sahrens 
8561d452cf5Sahrens static void
8573b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
8581d452cf5Sahrens {
8593b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
8603b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8613b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
8623b2aab18SMatthew Ahrens 	const char *tail;
8634445fffbSMatthew Ahrens 	dsl_dataset_t *ds;
8643b2aab18SMatthew Ahrens 	uint64_t obj;
8654445fffbSMatthew Ahrens 	blkptr_t *bp;
8663b2aab18SMatthew Ahrens 	objset_t *os;
867fa9e4066Sahrens 
8683b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
869fa9e4066Sahrens 
8703b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
8713b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
872fa9e4066Sahrens 
8733b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
8744445fffbSMatthew Ahrens 	bp = dsl_dataset_get_blkptr(ds);
8753b2aab18SMatthew Ahrens 	os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
8763b2aab18SMatthew Ahrens 	    ds, bp, doca->doca_type, tx);
877fa9e4066Sahrens 
8783b2aab18SMatthew Ahrens 	if (doca->doca_userfunc != NULL) {
8793b2aab18SMatthew Ahrens 		doca->doca_userfunc(os, doca->doca_userarg,
8803b2aab18SMatthew Ahrens 		    doca->doca_cred, tx);
881fa9e4066Sahrens 	}
882ecd6cf80Smarks 
8833b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "create", tx, "");
8844445fffbSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
8853b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
886fa9e4066Sahrens }
887fa9e4066Sahrens 
888fa9e4066Sahrens int
889ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
890ecd6cf80Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
891fa9e4066Sahrens {
8923b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t doca;
893fa9e4066Sahrens 
8943b2aab18SMatthew Ahrens 	doca.doca_name = name;
8953b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
8963b2aab18SMatthew Ahrens 	doca.doca_flags = flags;
8973b2aab18SMatthew Ahrens 	doca.doca_userfunc = func;
8983b2aab18SMatthew Ahrens 	doca.doca_userarg = arg;
8993b2aab18SMatthew Ahrens 	doca.doca_type = type;
900ecd6cf80Smarks 
9013b2aab18SMatthew Ahrens 	return (dsl_sync_task(name,
9027d46dc6cSMatthew Ahrens 	    dmu_objset_create_check, dmu_objset_create_sync, &doca,
9037d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
904ae46e4c7SMatthew Ahrens }
905ae46e4c7SMatthew Ahrens 
9063b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg {
9073b2aab18SMatthew Ahrens 	const char *doca_clone;
9083b2aab18SMatthew Ahrens 	const char *doca_origin;
9093b2aab18SMatthew Ahrens 	cred_t *doca_cred;
9103b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t;
9113b2aab18SMatthew Ahrens 
9123b2aab18SMatthew Ahrens /*ARGSUSED*/
9133b2aab18SMatthew Ahrens static int
9143b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
915ae46e4c7SMatthew Ahrens {
9163b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
917ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
918ae46e4c7SMatthew Ahrens 	const char *tail;
9193b2aab18SMatthew Ahrens 	int error;
9203b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
9213b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
922ae46e4c7SMatthew Ahrens 
9233b2aab18SMatthew Ahrens 	if (strchr(doca->doca_clone, '@') != NULL)
924be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9253b2aab18SMatthew Ahrens 
9263b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
9273b2aab18SMatthew Ahrens 	if (error != 0)
9283b2aab18SMatthew Ahrens 		return (error);
929ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
9303b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
931be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
932fa9e4066Sahrens 	}
93303b1c297SAlexander Eremin 
934a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
935a2afb611SJerry Jelinek 	    doca->doca_cred);
936a2afb611SJerry Jelinek 	if (error != 0) {
937a2afb611SJerry Jelinek 		dsl_dir_rele(pdd, FTAG);
938a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
939a2afb611SJerry Jelinek 	}
9403b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
941fa9e4066Sahrens 
9423b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
9433b2aab18SMatthew Ahrens 	if (error != 0)
94499d5e173STim Haley 		return (error);
94599d5e173STim Haley 
9463b2aab18SMatthew Ahrens 	/* You can only clone snapshots, not the head datasets. */
947bc9014e6SJustin Gibbs 	if (!origin->ds_is_snapshot) {
9483b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
949be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
95092241e0bSTom Erickson 	}
9513b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
95299d5e173STim Haley 
9533b2aab18SMatthew Ahrens 	return (0);
954ea2f5b9eSMatthew Ahrens }
9551d452cf5Sahrens 
9563b2aab18SMatthew Ahrens static void
9573b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
9581d452cf5Sahrens {
9593b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
9603b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
9613b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
9623b2aab18SMatthew Ahrens 	const char *tail;
9633b2aab18SMatthew Ahrens 	dsl_dataset_t *origin, *ds;
9643b2aab18SMatthew Ahrens 	uint64_t obj;
9653b2aab18SMatthew Ahrens 	char namebuf[MAXNAMELEN];
9664445fffbSMatthew Ahrens 
9673b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
9683b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
9694445fffbSMatthew Ahrens 
9703b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
9713b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
972f2e10be3Srm 
9733b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
9743b2aab18SMatthew Ahrens 	dsl_dataset_name(origin, namebuf);
9753b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "clone", tx,
9763b2aab18SMatthew Ahrens 	    "origin=%s (%llu)", namebuf, origin->ds_object);
9773b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
9783b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
9793b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
9801d452cf5Sahrens }
9811d452cf5Sahrens 
9821d452cf5Sahrens int
9833b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin)
9841d452cf5Sahrens {
9853b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t doca;
9864445fffbSMatthew Ahrens 
9873b2aab18SMatthew Ahrens 	doca.doca_clone = clone;
9883b2aab18SMatthew Ahrens 	doca.doca_origin = origin;
9893b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
99099d5e173STim Haley 
9913b2aab18SMatthew Ahrens 	return (dsl_sync_task(clone,
9927d46dc6cSMatthew Ahrens 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
9937d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
9944445fffbSMatthew Ahrens }
9954445fffbSMatthew Ahrens 
9964445fffbSMatthew Ahrens int
9974445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname)
9984445fffbSMatthew Ahrens {
9994445fffbSMatthew Ahrens 	int err;
10004445fffbSMatthew Ahrens 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
10014445fffbSMatthew Ahrens 	nvlist_t *snaps = fnvlist_alloc();
10024445fffbSMatthew Ahrens 
10034445fffbSMatthew Ahrens 	fnvlist_add_boolean(snaps, longsnap);
10044445fffbSMatthew Ahrens 	strfree(longsnap);
10053b2aab18SMatthew Ahrens 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
10063b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
10074445fffbSMatthew Ahrens 	return (err);
10084445fffbSMatthew Ahrens }
10094445fffbSMatthew Ahrens 
1010fa9e4066Sahrens static void
101114843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
1012fa9e4066Sahrens {
1013c717a561Smaybee 	dnode_t *dn;
1014faafa6e3Sahrens 
1015c717a561Smaybee 	while (dn = list_head(list)) {
1016c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1017c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
1018c717a561Smaybee 		/*
101914843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
102014843421SMatthew Ahrens 		 * meta-dnode needs to set it ouside dnode_sync().
1021c717a561Smaybee 		 */
1022c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1023c717a561Smaybee 		ASSERT(dn->dn_zio);
1024faafa6e3Sahrens 
1025c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1026c717a561Smaybee 		list_remove(list, dn);
102714843421SMatthew Ahrens 
102814843421SMatthew Ahrens 		if (newlist) {
102914843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
103014843421SMatthew Ahrens 			list_insert_tail(newlist, dn);
103114843421SMatthew Ahrens 		}
103214843421SMatthew Ahrens 
1033c717a561Smaybee 		dnode_sync(dn, tx);
1034fa9e4066Sahrens 	}
1035fa9e4066Sahrens }
1036fa9e4066Sahrens 
1037fa9e4066Sahrens /* ARGSUSED */
1038fa9e4066Sahrens static void
1039b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1040fa9e4066Sahrens {
1041e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1042503ad85cSMatthew Ahrens 	objset_t *os = arg;
1043c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1044fa9e4066Sahrens 
10455d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp));
10463b2aab18SMatthew Ahrens 	ASSERT3P(bp, ==, os->os_rootbp);
10473b2aab18SMatthew Ahrens 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
10483b2aab18SMatthew Ahrens 	ASSERT0(BP_GET_LEVEL(bp));
10490a4e9518Sgw 
1050fa9e4066Sahrens 	/*
105114843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
105214843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
105314843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
105414843421SMatthew Ahrens 	 * dnode and user/group accounting objects).
1055fa9e4066Sahrens 	 */
105614843421SMatthew Ahrens 	bp->blk_fill = 0;
1057e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
10585d7b4d43SMatthew Ahrens 		bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1059b24ab676SJeff Bonwick }
1060b24ab676SJeff Bonwick 
1061b24ab676SJeff Bonwick /* ARGSUSED */
1062b24ab676SJeff Bonwick static void
1063b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1064b24ab676SJeff Bonwick {
1065b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1066b24ab676SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
1067b24ab676SJeff Bonwick 	objset_t *os = arg;
10680a4e9518Sgw 
1069e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1070b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, bp_orig));
1071e14bb325SJeff Bonwick 	} else {
1072b24ab676SJeff Bonwick 		dsl_dataset_t *ds = os->os_dsl_dataset;
1073b24ab676SJeff Bonwick 		dmu_tx_t *tx = os->os_synctx;
1074b24ab676SJeff Bonwick 
1075b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1076b24ab676SJeff Bonwick 		dsl_dataset_block_born(ds, bp, tx);
10770a4e9518Sgw 	}
1078c717a561Smaybee }
1079c717a561Smaybee 
1080fa9e4066Sahrens /* called from dsl */
1081fa9e4066Sahrens void
1082503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1083fa9e4066Sahrens {
1084fa9e4066Sahrens 	int txgoff;
10857802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
1086b24ab676SJeff Bonwick 	zio_prop_t zp;
1087c717a561Smaybee 	zio_t *zio;
1088c717a561Smaybee 	list_t *list;
108914843421SMatthew Ahrens 	list_t *newlist = NULL;
1090c717a561Smaybee 	dbuf_dirty_record_t *dr;
1091c717a561Smaybee 
1092c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1093fa9e4066Sahrens 
1094fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1095fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
1096fa9e4066Sahrens 	os->os_synctx = tx;
1097fa9e4066Sahrens 
109887bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
109987bd5c1eSahrens 		/*
110087bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
110187bd5c1eSahrens 		 * spa_max_replication() could change, so reset
110287bd5c1eSahrens 		 * os_copies here.
110387bd5c1eSahrens 		 */
110487bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
110587bd5c1eSahrens 	}
110687bd5c1eSahrens 
1107fa9e4066Sahrens 	/*
1108c717a561Smaybee 	 * Create the root block IO
1109fa9e4066Sahrens 	 */
1110b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1111b24ab676SJeff Bonwick 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1112b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
11131b912ec7SGeorge Wilson 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1114b24ab676SJeff Bonwick 
1115b24ab676SJeff Bonwick 	dmu_write_policy(os, NULL, 0, 0, &zp);
1116b24ab676SJeff Bonwick 
1117b24ab676SJeff Bonwick 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1118aad02571SSaso Kiselkov 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1119aad02571SSaso Kiselkov 	    DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
112069962b56SMatthew Ahrens 	    NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1121aad02571SSaso Kiselkov 	    ZIO_FLAG_MUSTSUCCEED, &zb);
1122c717a561Smaybee 
1123c717a561Smaybee 	/*
112414843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
1125c717a561Smaybee 	 */
1126744947dcSTom Erickson 	DMU_META_DNODE(os)->dn_zio = zio;
1127744947dcSTom Erickson 	dnode_sync(DMU_META_DNODE(os), tx);
1128c717a561Smaybee 
112914843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
113014843421SMatthew Ahrens 
1131744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) &&
1132744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1133744947dcSTom Erickson 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1134744947dcSTom Erickson 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1135744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1136744947dcSTom Erickson 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
113714843421SMatthew Ahrens 	}
113814843421SMatthew Ahrens 
1139c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
1140fa9e4066Sahrens 
114114843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
114214843421SMatthew Ahrens 		newlist = &os->os_synced_dnodes;
114314843421SMatthew Ahrens 		/*
114414843421SMatthew Ahrens 		 * We must create the list here because it uses the
114514843421SMatthew Ahrens 		 * dn_dirty_link[] of this txg.
114614843421SMatthew Ahrens 		 */
114714843421SMatthew Ahrens 		list_create(newlist, sizeof (dnode_t),
114814843421SMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
114914843421SMatthew Ahrens 	}
115014843421SMatthew Ahrens 
115114843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
115214843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1153fa9e4066Sahrens 
1154744947dcSTom Erickson 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1155c717a561Smaybee 	while (dr = list_head(list)) {
11563b2aab18SMatthew Ahrens 		ASSERT0(dr->dr_dbuf->db_level);
1157c717a561Smaybee 		list_remove(list, dr);
1158c717a561Smaybee 		if (dr->dr_zio)
1159c717a561Smaybee 			zio_nowait(dr->dr_zio);
1160c717a561Smaybee 	}
1161c717a561Smaybee 	/*
1162c717a561Smaybee 	 * Free intent log blocks up to this tx.
1163c717a561Smaybee 	 */
1164c717a561Smaybee 	zil_sync(os->os_zil, tx);
1165088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
1166c717a561Smaybee 	zio_nowait(zio);
1167fa9e4066Sahrens }
1168fa9e4066Sahrens 
1169b24ab676SJeff Bonwick boolean_t
1170b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1171b24ab676SJeff Bonwick {
1172b24ab676SJeff Bonwick 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1173b24ab676SJeff Bonwick 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1174b24ab676SJeff Bonwick }
1175b24ab676SJeff Bonwick 
1176744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
117714843421SMatthew Ahrens 
117814843421SMatthew Ahrens void
117914843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
118014843421SMatthew Ahrens {
118114843421SMatthew Ahrens 	used_cbs[ost] = cb;
118214843421SMatthew Ahrens }
118314843421SMatthew Ahrens 
118414843421SMatthew Ahrens boolean_t
1185503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
118614843421SMatthew Ahrens {
118714843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1188744947dcSTom Erickson 	    used_cbs[os->os_phys->os_type] != NULL &&
1189744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os) != NULL);
119014843421SMatthew Ahrens }
119114843421SMatthew Ahrens 
11929966ca11SMatthew Ahrens static void
11930a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
11940a586ceaSMark Shellenbaum     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
11959966ca11SMatthew Ahrens {
11960a586ceaSMark Shellenbaum 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
11970a586ceaSMark Shellenbaum 		int64_t delta = DNODE_SIZE + used;
11989966ca11SMatthew Ahrens 		if (subtract)
11999966ca11SMatthew Ahrens 			delta = -delta;
1200b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
12019966ca11SMatthew Ahrens 		    user, delta, tx));
1202b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
12039966ca11SMatthew Ahrens 		    group, delta, tx));
12049966ca11SMatthew Ahrens 	}
12059966ca11SMatthew Ahrens }
12069966ca11SMatthew Ahrens 
120714843421SMatthew Ahrens void
12080a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
120914843421SMatthew Ahrens {
121014843421SMatthew Ahrens 	dnode_t *dn;
121114843421SMatthew Ahrens 	list_t *list = &os->os_synced_dnodes;
121214843421SMatthew Ahrens 
121314843421SMatthew Ahrens 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
121414843421SMatthew Ahrens 
121514843421SMatthew Ahrens 	while (dn = list_head(list)) {
12161d8ccc7bSMark Shellenbaum 		int flags;
121714843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
121814843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
121914843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
122014843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
122114843421SMatthew Ahrens 
122214843421SMatthew Ahrens 		/* Allocate the user/groupused objects if necessary. */
1223744947dcSTom Erickson 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1224503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
122514843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT,
122614843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1227503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
122814843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT,
122914843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
123014843421SMatthew Ahrens 		}
123114843421SMatthew Ahrens 
123214843421SMatthew Ahrens 		/*
12339966ca11SMatthew Ahrens 		 * We intentionally modify the zap object even if the
12340a586ceaSMark Shellenbaum 		 * net delta is zero.  Otherwise
12359966ca11SMatthew Ahrens 		 * the block of the zap obj could be shared between
12369966ca11SMatthew Ahrens 		 * datasets but need to be different between them after
12379966ca11SMatthew Ahrens 		 * a bprewrite.
123814843421SMatthew Ahrens 		 */
123914843421SMatthew Ahrens 
12401d8ccc7bSMark Shellenbaum 		flags = dn->dn_id_flags;
12411d8ccc7bSMark Shellenbaum 		ASSERT(flags);
12421d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST)  {
12430a586ceaSMark Shellenbaum 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
12440a586ceaSMark Shellenbaum 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
12450a586ceaSMark Shellenbaum 		}
12461d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_NEW_EXIST) {
12470a586ceaSMark Shellenbaum 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
12480a586ceaSMark Shellenbaum 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
12490a586ceaSMark Shellenbaum 			    dn->dn_newgid, B_FALSE, tx);
12500a586ceaSMark Shellenbaum 		}
12510a586ceaSMark Shellenbaum 
12521d8ccc7bSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
12530a586ceaSMark Shellenbaum 		dn->dn_oldused = 0;
12540a586ceaSMark Shellenbaum 		dn->dn_oldflags = 0;
12550a586ceaSMark Shellenbaum 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
12560a586ceaSMark Shellenbaum 			dn->dn_olduid = dn->dn_newuid;
12570a586ceaSMark Shellenbaum 			dn->dn_oldgid = dn->dn_newgid;
12580a586ceaSMark Shellenbaum 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
12590a586ceaSMark Shellenbaum 			if (dn->dn_bonuslen == 0)
12600a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
12610a586ceaSMark Shellenbaum 			else
12620a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
12630a586ceaSMark Shellenbaum 		}
126428d97a71SMark Shellenbaum 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
126514843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
126614843421SMatthew Ahrens 
126714843421SMatthew Ahrens 		list_remove(list, dn);
126814843421SMatthew Ahrens 		dnode_rele(dn, list);
126914843421SMatthew Ahrens 	}
127014843421SMatthew Ahrens }
127114843421SMatthew Ahrens 
127206e0070dSMark Shellenbaum /*
127306e0070dSMark Shellenbaum  * Returns a pointer to data to find uid/gid from
127406e0070dSMark Shellenbaum  *
127506e0070dSMark Shellenbaum  * If a dirty record for transaction group that is syncing can't
127606e0070dSMark Shellenbaum  * be found then NULL is returned.  In the NULL case it is assumed
127706e0070dSMark Shellenbaum  * the uid/gid aren't changing.
127806e0070dSMark Shellenbaum  */
127906e0070dSMark Shellenbaum static void *
128006e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
128106e0070dSMark Shellenbaum {
128206e0070dSMark Shellenbaum 	dbuf_dirty_record_t *dr, **drp;
128306e0070dSMark Shellenbaum 	void *data;
128406e0070dSMark Shellenbaum 
128506e0070dSMark Shellenbaum 	if (db->db_dirtycnt == 0)
128606e0070dSMark Shellenbaum 		return (db->db.db_data);  /* Nothing is changing */
128706e0070dSMark Shellenbaum 
128806e0070dSMark Shellenbaum 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
128906e0070dSMark Shellenbaum 		if (dr->dr_txg == tx->tx_txg)
129006e0070dSMark Shellenbaum 			break;
129106e0070dSMark Shellenbaum 
1292744947dcSTom Erickson 	if (dr == NULL) {
129306e0070dSMark Shellenbaum 		data = NULL;
1294744947dcSTom Erickson 	} else {
1295744947dcSTom Erickson 		dnode_t *dn;
1296744947dcSTom Erickson 
1297744947dcSTom Erickson 		DB_DNODE_ENTER(dr->dr_dbuf);
1298744947dcSTom Erickson 		dn = DB_DNODE(dr->dr_dbuf);
1299744947dcSTom Erickson 
1300744947dcSTom Erickson 		if (dn->dn_bonuslen == 0 &&
1301744947dcSTom Erickson 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1302744947dcSTom Erickson 			data = dr->dt.dl.dr_data->b_data;
1303744947dcSTom Erickson 		else
1304744947dcSTom Erickson 			data = dr->dt.dl.dr_data;
1305744947dcSTom Erickson 
1306744947dcSTom Erickson 		DB_DNODE_EXIT(dr->dr_dbuf);
1307744947dcSTom Erickson 	}
1308744947dcSTom Erickson 
130906e0070dSMark Shellenbaum 	return (data);
131006e0070dSMark Shellenbaum }
131106e0070dSMark Shellenbaum 
13120a586ceaSMark Shellenbaum void
131306e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
13140a586ceaSMark Shellenbaum {
13150a586ceaSMark Shellenbaum 	objset_t *os = dn->dn_objset;
13160a586ceaSMark Shellenbaum 	void *data = NULL;
131706e0070dSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
1318d5285caeSGeorge Wilson 	uint64_t *user = NULL;
1319d5285caeSGeorge Wilson 	uint64_t *group = NULL;
13200a586ceaSMark Shellenbaum 	int flags = dn->dn_id_flags;
13210a586ceaSMark Shellenbaum 	int error;
132206e0070dSMark Shellenbaum 	boolean_t have_spill = B_FALSE;
13230a586ceaSMark Shellenbaum 
13240a586ceaSMark Shellenbaum 	if (!dmu_objset_userused_enabled(dn->dn_objset))
13250a586ceaSMark Shellenbaum 		return;
13260a586ceaSMark Shellenbaum 
13270a586ceaSMark Shellenbaum 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
13280a586ceaSMark Shellenbaum 	    DN_ID_CHKED_SPILL)))
13290a586ceaSMark Shellenbaum 		return;
13300a586ceaSMark Shellenbaum 
13310a586ceaSMark Shellenbaum 	if (before && dn->dn_bonuslen != 0)
13320a586ceaSMark Shellenbaum 		data = DN_BONUS(dn->dn_phys);
133306e0070dSMark Shellenbaum 	else if (!before && dn->dn_bonuslen != 0) {
133406e0070dSMark Shellenbaum 		if (dn->dn_bonus) {
133506e0070dSMark Shellenbaum 			db = dn->dn_bonus;
133606e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
133706e0070dSMark Shellenbaum 			data = dmu_objset_userquota_find_data(db, tx);
133806e0070dSMark Shellenbaum 		} else {
133906e0070dSMark Shellenbaum 			data = DN_BONUS(dn->dn_phys);
134006e0070dSMark Shellenbaum 		}
134106e0070dSMark Shellenbaum 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
13420a586ceaSMark Shellenbaum 			int rf = 0;
13430a586ceaSMark Shellenbaum 
13440a586ceaSMark Shellenbaum 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
13450a586ceaSMark Shellenbaum 				rf |= DB_RF_HAVESTRUCT;
13461d8ccc7bSMark Shellenbaum 			error = dmu_spill_hold_by_dnode(dn,
13471d8ccc7bSMark Shellenbaum 			    rf | DB_RF_MUST_SUCCEED,
134806e0070dSMark Shellenbaum 			    FTAG, (dmu_buf_t **)&db);
13490a586ceaSMark Shellenbaum 			ASSERT(error == 0);
135006e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
135106e0070dSMark Shellenbaum 			data = (before) ? db->db.db_data :
135206e0070dSMark Shellenbaum 			    dmu_objset_userquota_find_data(db, tx);
135306e0070dSMark Shellenbaum 			have_spill = B_TRUE;
13540a586ceaSMark Shellenbaum 	} else {
13550a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
13560a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13570a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
13580a586ceaSMark Shellenbaum 		return;
13590a586ceaSMark Shellenbaum 	}
13600a586ceaSMark Shellenbaum 
13610a586ceaSMark Shellenbaum 	if (before) {
136206e0070dSMark Shellenbaum 		ASSERT(data);
13630a586ceaSMark Shellenbaum 		user = &dn->dn_olduid;
13640a586ceaSMark Shellenbaum 		group = &dn->dn_oldgid;
136506e0070dSMark Shellenbaum 	} else if (data) {
13660a586ceaSMark Shellenbaum 		user = &dn->dn_newuid;
13670a586ceaSMark Shellenbaum 		group = &dn->dn_newgid;
13680a586ceaSMark Shellenbaum 	}
13690a586ceaSMark Shellenbaum 
137006e0070dSMark Shellenbaum 	/*
137106e0070dSMark Shellenbaum 	 * Must always call the callback in case the object
137206e0070dSMark Shellenbaum 	 * type has changed and that type isn't an object type to track
137306e0070dSMark Shellenbaum 	 */
13740a586ceaSMark Shellenbaum 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
13750a586ceaSMark Shellenbaum 	    user, group);
13760a586ceaSMark Shellenbaum 
137706e0070dSMark Shellenbaum 	/*
137806e0070dSMark Shellenbaum 	 * Preserve existing uid/gid when the callback can't determine
137906e0070dSMark Shellenbaum 	 * what the new uid/gid are and the callback returned EEXIST.
138006e0070dSMark Shellenbaum 	 * The EEXIST error tells us to just use the existing uid/gid.
138106e0070dSMark Shellenbaum 	 * If we don't know what the old values are then just assign
138206e0070dSMark Shellenbaum 	 * them to 0, since that is a new file  being created.
138306e0070dSMark Shellenbaum 	 */
138406e0070dSMark Shellenbaum 	if (!before && data == NULL && error == EEXIST) {
138506e0070dSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST) {
138606e0070dSMark Shellenbaum 			dn->dn_newuid = dn->dn_olduid;
138706e0070dSMark Shellenbaum 			dn->dn_newgid = dn->dn_oldgid;
138806e0070dSMark Shellenbaum 		} else {
138906e0070dSMark Shellenbaum 			dn->dn_newuid = 0;
139006e0070dSMark Shellenbaum 			dn->dn_newgid = 0;
139106e0070dSMark Shellenbaum 		}
139206e0070dSMark Shellenbaum 		error = 0;
139306e0070dSMark Shellenbaum 	}
139406e0070dSMark Shellenbaum 
139506e0070dSMark Shellenbaum 	if (db)
139606e0070dSMark Shellenbaum 		mutex_exit(&db->db_mtx);
139706e0070dSMark Shellenbaum 
13980a586ceaSMark Shellenbaum 	mutex_enter(&dn->dn_mtx);
13990a586ceaSMark Shellenbaum 	if (error == 0 && before)
14000a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
14010a586ceaSMark Shellenbaum 	if (error == 0 && !before)
14020a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
14030a586ceaSMark Shellenbaum 
140406e0070dSMark Shellenbaum 	if (have_spill) {
14050a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
14060a586ceaSMark Shellenbaum 	} else {
14070a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
14080a586ceaSMark Shellenbaum 	}
14090a586ceaSMark Shellenbaum 	mutex_exit(&dn->dn_mtx);
141006e0070dSMark Shellenbaum 	if (have_spill)
141106e0070dSMark Shellenbaum 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
14120a586ceaSMark Shellenbaum }
14130a586ceaSMark Shellenbaum 
141414843421SMatthew Ahrens boolean_t
141514843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
141614843421SMatthew Ahrens {
1417503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
141814843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
141914843421SMatthew Ahrens }
142014843421SMatthew Ahrens 
142114843421SMatthew Ahrens int
142214843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
142314843421SMatthew Ahrens {
142414843421SMatthew Ahrens 	uint64_t obj;
142514843421SMatthew Ahrens 	int err = 0;
142614843421SMatthew Ahrens 
142714843421SMatthew Ahrens 	if (dmu_objset_userspace_present(os))
142814843421SMatthew Ahrens 		return (0);
1429503ad85cSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
1430be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
143114843421SMatthew Ahrens 	if (dmu_objset_is_snapshot(os))
1432be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
143314843421SMatthew Ahrens 
143414843421SMatthew Ahrens 	/*
143514843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
143614843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
143714843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
143814843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
143914843421SMatthew Ahrens 	 * independently.
144014843421SMatthew Ahrens 	 */
144114843421SMatthew Ahrens 
144214843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
144368857716SLin Ling 		dmu_tx_t *tx;
144414843421SMatthew Ahrens 		dmu_buf_t *db;
144514843421SMatthew Ahrens 		int objerr;
144614843421SMatthew Ahrens 
144714843421SMatthew Ahrens 		if (issig(JUSTLOOKING) && issig(FORREAL))
1448be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
144914843421SMatthew Ahrens 
145014843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
14513b2aab18SMatthew Ahrens 		if (objerr != 0)
145214843421SMatthew Ahrens 			continue;
145368857716SLin Ling 		tx = dmu_tx_create(os);
145414843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
145514843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
14563b2aab18SMatthew Ahrens 		if (objerr != 0) {
145714843421SMatthew Ahrens 			dmu_tx_abort(tx);
145814843421SMatthew Ahrens 			continue;
145914843421SMatthew Ahrens 		}
146014843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
146114843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
146214843421SMatthew Ahrens 		dmu_tx_commit(tx);
146314843421SMatthew Ahrens 	}
146414843421SMatthew Ahrens 
1465503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
146614843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
146714843421SMatthew Ahrens 	return (0);
146814843421SMatthew Ahrens }
146914843421SMatthew Ahrens 
1470fa9e4066Sahrens void
1471a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1472a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1473fa9e4066Sahrens {
1474503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1475a2eea2e1Sahrens 	    usedobjsp, availobjsp);
1476a2eea2e1Sahrens }
1477a2eea2e1Sahrens 
1478a2eea2e1Sahrens uint64_t
1479a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1480a2eea2e1Sahrens {
1481503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1482a2eea2e1Sahrens }
1483a2eea2e1Sahrens 
1484a2eea2e1Sahrens void
1485a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1486a2eea2e1Sahrens {
1487503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
1488503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
1489503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1490a2eea2e1Sahrens }
1491a2eea2e1Sahrens 
1492a2eea2e1Sahrens void
1493a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1494a2eea2e1Sahrens {
1495503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
1496503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
1497a2eea2e1Sahrens 
1498503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1499503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1500a2eea2e1Sahrens 
1501a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1502503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
150314843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
150414843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
1505fa9e4066Sahrens }
1506fa9e4066Sahrens 
1507fa9e4066Sahrens int
1508fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1509fa9e4066Sahrens {
1510503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1511bc9014e6SJustin Gibbs 		return (os->os_dsl_dataset->ds_is_snapshot);
1512fa9e4066Sahrens 	else
1513fa9e4066Sahrens 		return (B_FALSE);
1514fa9e4066Sahrens }
1515fa9e4066Sahrens 
1516ab04eb8eStimh int
1517ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1518ab04eb8eStimh     boolean_t *conflict)
1519ab04eb8eStimh {
1520503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1521ab04eb8eStimh 	uint64_t ignored;
1522ab04eb8eStimh 
1523c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1524be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1525ab04eb8eStimh 
1526ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1527c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1528c1379625SJustin T. Gibbs 	    MT_FIRST, real, maxlen, conflict));
1529ab04eb8eStimh }
1530ab04eb8eStimh 
1531fa9e4066Sahrens int
1532fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1533b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1534fa9e4066Sahrens {
1535503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1536fa9e4066Sahrens 	zap_cursor_t cursor;
1537fa9e4066Sahrens 	zap_attribute_t attr;
1538fa9e4066Sahrens 
15393b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
15403b2aab18SMatthew Ahrens 
1541c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1542be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1543fa9e4066Sahrens 
1544fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
1545fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1546c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1547fa9e4066Sahrens 
154887e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
154987e5029aSahrens 		zap_cursor_fini(&cursor);
1550be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
155187e5029aSahrens 	}
155287e5029aSahrens 
155387e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
155487e5029aSahrens 		zap_cursor_fini(&cursor);
1555be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
155687e5029aSahrens 	}
155787e5029aSahrens 
155887e5029aSahrens 	(void) strcpy(name, attr.za_name);
155987e5029aSahrens 	if (idp)
156087e5029aSahrens 		*idp = attr.za_first_integer;
1561b38f0970Sck 	if (case_conflict)
1562b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
156387e5029aSahrens 	zap_cursor_advance(&cursor);
156487e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
156587e5029aSahrens 	zap_cursor_fini(&cursor);
156687e5029aSahrens 
156787e5029aSahrens 	return (0);
156887e5029aSahrens }
156987e5029aSahrens 
157087e5029aSahrens int
157187e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
157287e5029aSahrens     uint64_t *idp, uint64_t *offp)
157387e5029aSahrens {
1574503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
157587e5029aSahrens 	zap_cursor_t cursor;
157687e5029aSahrens 	zap_attribute_t attr;
157787e5029aSahrens 
157887e5029aSahrens 	/* there is no next dir on a snapshot! */
1579503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
1580c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj)
1581be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1582fa9e4066Sahrens 
158387e5029aSahrens 	zap_cursor_init_serialized(&cursor,
158487e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
1585c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
158687e5029aSahrens 
158787e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
158887e5029aSahrens 		zap_cursor_fini(&cursor);
1589be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
159087e5029aSahrens 	}
159187e5029aSahrens 
159287e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
159387e5029aSahrens 		zap_cursor_fini(&cursor);
1594be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
159587e5029aSahrens 	}
1596fa9e4066Sahrens 
1597fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
159887e5029aSahrens 	if (idp)
159987e5029aSahrens 		*idp = attr.za_first_integer;
1600fa9e4066Sahrens 	zap_cursor_advance(&cursor);
1601fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
160287e5029aSahrens 	zap_cursor_fini(&cursor);
1603fa9e4066Sahrens 
1604fa9e4066Sahrens 	return (0);
1605fa9e4066Sahrens }
1606fa9e4066Sahrens 
160712380e1eSArne Jansen typedef struct dmu_objset_find_ctx {
160812380e1eSArne Jansen 	taskq_t		*dc_tq;
160912380e1eSArne Jansen 	dsl_pool_t	*dc_dp;
161012380e1eSArne Jansen 	uint64_t	dc_ddobj;
161112380e1eSArne Jansen 	int		(*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
161212380e1eSArne Jansen 	void		*dc_arg;
161312380e1eSArne Jansen 	int		dc_flags;
161412380e1eSArne Jansen 	kmutex_t	*dc_error_lock;
161512380e1eSArne Jansen 	int		*dc_error;
161612380e1eSArne Jansen } dmu_objset_find_ctx_t;
161712380e1eSArne Jansen 
161812380e1eSArne Jansen static void
161912380e1eSArne Jansen dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
1620088f3894Sahrens {
162112380e1eSArne Jansen 	dsl_pool_t *dp = dcp->dc_dp;
162212380e1eSArne Jansen 	dmu_objset_find_ctx_t *child_dcp;
16233b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
16243b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
16253b2aab18SMatthew Ahrens 	zap_cursor_t zc;
16263b2aab18SMatthew Ahrens 	zap_attribute_t *attr;
16273b2aab18SMatthew Ahrens 	uint64_t thisobj;
162812380e1eSArne Jansen 	int err = 0;
16293b2aab18SMatthew Ahrens 
163012380e1eSArne Jansen 	/* don't process if there already was an error */
163112380e1eSArne Jansen 	if (*dcp->dc_error != 0)
163212380e1eSArne Jansen 		goto out;
16333b2aab18SMatthew Ahrens 
163412380e1eSArne Jansen 	err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd);
16353b2aab18SMatthew Ahrens 	if (err != 0)
163612380e1eSArne Jansen 		goto out;
16373b2aab18SMatthew Ahrens 
16383b2aab18SMatthew Ahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
16393b2aab18SMatthew Ahrens 	if (dd->dd_myname[0] == '$') {
16403b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
164112380e1eSArne Jansen 		goto out;
16423b2aab18SMatthew Ahrens 	}
16433b2aab18SMatthew Ahrens 
1644c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
16453b2aab18SMatthew Ahrens 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
16463b2aab18SMatthew Ahrens 
16473b2aab18SMatthew Ahrens 	/*
16483b2aab18SMatthew Ahrens 	 * Iterate over all children.
16493b2aab18SMatthew Ahrens 	 */
165012380e1eSArne Jansen 	if (dcp->dc_flags & DS_FIND_CHILDREN) {
16513b2aab18SMatthew Ahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1652c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
16533b2aab18SMatthew Ahrens 		    zap_cursor_retrieve(&zc, attr) == 0;
16543b2aab18SMatthew Ahrens 		    (void) zap_cursor_advance(&zc)) {
16553b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
16563b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
16573b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
16583b2aab18SMatthew Ahrens 
165912380e1eSArne Jansen 			child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
166012380e1eSArne Jansen 			*child_dcp = *dcp;
166112380e1eSArne Jansen 			child_dcp->dc_ddobj = attr->za_first_integer;
166212380e1eSArne Jansen 			if (dcp->dc_tq != NULL)
166312380e1eSArne Jansen 				(void) taskq_dispatch(dcp->dc_tq,
166412380e1eSArne Jansen 				    dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
166512380e1eSArne Jansen 			else
166612380e1eSArne Jansen 				dmu_objset_find_dp_impl(child_dcp);
16673b2aab18SMatthew Ahrens 		}
16683b2aab18SMatthew Ahrens 		zap_cursor_fini(&zc);
16693b2aab18SMatthew Ahrens 	}
16703b2aab18SMatthew Ahrens 
16713b2aab18SMatthew Ahrens 	/*
16723b2aab18SMatthew Ahrens 	 * Iterate over all snapshots.
16733b2aab18SMatthew Ahrens 	 */
167412380e1eSArne Jansen 	if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
16753b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
16763b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
16773b2aab18SMatthew Ahrens 
16783b2aab18SMatthew Ahrens 		if (err == 0) {
1679c1379625SJustin T. Gibbs 			uint64_t snapobj;
1680c1379625SJustin T. Gibbs 
1681c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
16823b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
16833b2aab18SMatthew Ahrens 
16843b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
16853b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
16863b2aab18SMatthew Ahrens 			    (void) zap_cursor_advance(&zc)) {
16873b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
16883b2aab18SMatthew Ahrens 				    sizeof (uint64_t));
16893b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
16903b2aab18SMatthew Ahrens 
16913b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
16923b2aab18SMatthew Ahrens 				    attr->za_first_integer, FTAG, &ds);
16933b2aab18SMatthew Ahrens 				if (err != 0)
16943b2aab18SMatthew Ahrens 					break;
169512380e1eSArne Jansen 				err = dcp->dc_func(dp, ds, dcp->dc_arg);
16963b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
16973b2aab18SMatthew Ahrens 				if (err != 0)
16983b2aab18SMatthew Ahrens 					break;
16993b2aab18SMatthew Ahrens 			}
17003b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
17013b2aab18SMatthew Ahrens 		}
17023b2aab18SMatthew Ahrens 	}
17033b2aab18SMatthew Ahrens 
17043b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
17053b2aab18SMatthew Ahrens 	kmem_free(attr, sizeof (zap_attribute_t));
17063b2aab18SMatthew Ahrens 
17073b2aab18SMatthew Ahrens 	if (err != 0)
170812380e1eSArne Jansen 		goto out;
17093b2aab18SMatthew Ahrens 
17103b2aab18SMatthew Ahrens 	/*
17113b2aab18SMatthew Ahrens 	 * Apply to self.
17123b2aab18SMatthew Ahrens 	 */
17133b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
17143b2aab18SMatthew Ahrens 	if (err != 0)
171512380e1eSArne Jansen 		goto out;
171612380e1eSArne Jansen 	err = dcp->dc_func(dp, ds, dcp->dc_arg);
17173b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
171812380e1eSArne Jansen 
171912380e1eSArne Jansen out:
172012380e1eSArne Jansen 	if (err != 0) {
172112380e1eSArne Jansen 		mutex_enter(dcp->dc_error_lock);
172212380e1eSArne Jansen 		/* only keep first error */
172312380e1eSArne Jansen 		if (*dcp->dc_error == 0)
172412380e1eSArne Jansen 			*dcp->dc_error = err;
172512380e1eSArne Jansen 		mutex_exit(dcp->dc_error_lock);
172612380e1eSArne Jansen 	}
172712380e1eSArne Jansen 
172812380e1eSArne Jansen 	kmem_free(dcp, sizeof (*dcp));
172912380e1eSArne Jansen }
173012380e1eSArne Jansen 
173112380e1eSArne Jansen static void
173212380e1eSArne Jansen dmu_objset_find_dp_cb(void *arg)
173312380e1eSArne Jansen {
173412380e1eSArne Jansen 	dmu_objset_find_ctx_t *dcp = arg;
173512380e1eSArne Jansen 	dsl_pool_t *dp = dcp->dc_dp;
173612380e1eSArne Jansen 
17371d3f896fSArne Jansen 	/*
17381d3f896fSArne Jansen 	 * We need to get a pool_config_lock here, as there are several
17391d3f896fSArne Jansen 	 * asssert(pool_config_held) down the stack. Getting a lock via
17401d3f896fSArne Jansen 	 * dsl_pool_config_enter is risky, as it might be stalled by a
17411d3f896fSArne Jansen 	 * pending writer. This would deadlock, as the write lock can
17421d3f896fSArne Jansen 	 * only be granted when our parent thread gives up the lock.
17431d3f896fSArne Jansen 	 * The _prio interface gives us priority over a pending writer.
17441d3f896fSArne Jansen 	 */
17451d3f896fSArne Jansen 	dsl_pool_config_enter_prio(dp, FTAG);
174612380e1eSArne Jansen 
174712380e1eSArne Jansen 	dmu_objset_find_dp_impl(dcp);
174812380e1eSArne Jansen 
174912380e1eSArne Jansen 	dsl_pool_config_exit(dp, FTAG);
175012380e1eSArne Jansen }
175112380e1eSArne Jansen 
175212380e1eSArne Jansen /*
175312380e1eSArne Jansen  * Find objsets under and including ddobj, call func(ds) on each.
175412380e1eSArne Jansen  * The order for the enumeration is completely undefined.
175512380e1eSArne Jansen  * func is called with dsl_pool_config held.
175612380e1eSArne Jansen  */
175712380e1eSArne Jansen int
175812380e1eSArne Jansen dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
175912380e1eSArne Jansen     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
176012380e1eSArne Jansen {
176112380e1eSArne Jansen 	int error = 0;
176212380e1eSArne Jansen 	taskq_t *tq = NULL;
176312380e1eSArne Jansen 	int ntasks;
176412380e1eSArne Jansen 	dmu_objset_find_ctx_t *dcp;
176512380e1eSArne Jansen 	kmutex_t err_lock;
176612380e1eSArne Jansen 
176712380e1eSArne Jansen 	mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
176812380e1eSArne Jansen 	dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
176912380e1eSArne Jansen 	dcp->dc_tq = NULL;
177012380e1eSArne Jansen 	dcp->dc_dp = dp;
177112380e1eSArne Jansen 	dcp->dc_ddobj = ddobj;
177212380e1eSArne Jansen 	dcp->dc_func = func;
177312380e1eSArne Jansen 	dcp->dc_arg = arg;
177412380e1eSArne Jansen 	dcp->dc_flags = flags;
177512380e1eSArne Jansen 	dcp->dc_error_lock = &err_lock;
177612380e1eSArne Jansen 	dcp->dc_error = &error;
177712380e1eSArne Jansen 
177812380e1eSArne Jansen 	if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
177912380e1eSArne Jansen 		/*
178012380e1eSArne Jansen 		 * In case a write lock is held we can't make use of
178112380e1eSArne Jansen 		 * parallelism, as down the stack of the worker threads
178212380e1eSArne Jansen 		 * the lock is asserted via dsl_pool_config_held.
178312380e1eSArne Jansen 		 * In case of a read lock this is solved by getting a read
178412380e1eSArne Jansen 		 * lock in each worker thread, which isn't possible in case
178512380e1eSArne Jansen 		 * of a writer lock. So we fall back to the synchronous path
178612380e1eSArne Jansen 		 * here.
178712380e1eSArne Jansen 		 * In the future it might be possible to get some magic into
178812380e1eSArne Jansen 		 * dsl_pool_config_held in a way that it returns true for
178912380e1eSArne Jansen 		 * the worker threads so that a single lock held from this
179012380e1eSArne Jansen 		 * thread suffices. For now, stay single threaded.
179112380e1eSArne Jansen 		 */
179212380e1eSArne Jansen 		dmu_objset_find_dp_impl(dcp);
179312380e1eSArne Jansen 
179412380e1eSArne Jansen 		return (error);
179512380e1eSArne Jansen 	}
179612380e1eSArne Jansen 
179712380e1eSArne Jansen 	ntasks = dmu_find_threads;
179812380e1eSArne Jansen 	if (ntasks == 0)
179912380e1eSArne Jansen 		ntasks = vdev_count_leaves(dp->dp_spa) * 4;
180012380e1eSArne Jansen 	tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
180112380e1eSArne Jansen 	    INT_MAX, 0);
180212380e1eSArne Jansen 	if (tq == NULL) {
180312380e1eSArne Jansen 		kmem_free(dcp, sizeof (*dcp));
180412380e1eSArne Jansen 		return (SET_ERROR(ENOMEM));
180512380e1eSArne Jansen 	}
180612380e1eSArne Jansen 	dcp->dc_tq = tq;
180712380e1eSArne Jansen 
180812380e1eSArne Jansen 	/* dcp will be freed by task */
180912380e1eSArne Jansen 	(void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
181012380e1eSArne Jansen 
181112380e1eSArne Jansen 	/*
181212380e1eSArne Jansen 	 * PORTING: this code relies on the property of taskq_wait to wait
181312380e1eSArne Jansen 	 * until no more tasks are queued and no more tasks are active. As
181412380e1eSArne Jansen 	 * we always queue new tasks from within other tasks, task_wait
181512380e1eSArne Jansen 	 * reliably waits for the full recursion to finish, even though we
181612380e1eSArne Jansen 	 * enqueue new tasks after taskq_wait has been called.
181712380e1eSArne Jansen 	 * On platforms other than illumos, taskq_wait may not have this
181812380e1eSArne Jansen 	 * property.
181912380e1eSArne Jansen 	 */
182012380e1eSArne Jansen 	taskq_wait(tq);
182112380e1eSArne Jansen 	taskq_destroy(tq);
182212380e1eSArne Jansen 	mutex_destroy(&err_lock);
182312380e1eSArne Jansen 
182412380e1eSArne Jansen 	return (error);
1825088f3894Sahrens }
1826088f3894Sahrens 
1827088f3894Sahrens /*
18283b2aab18SMatthew Ahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
18293b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when this is called, and it
18303b2aab18SMatthew Ahrens  * will not be held when the callback is called.
18313b2aab18SMatthew Ahrens  * Therefore this function should only be used when the pool is not changing
18323b2aab18SMatthew Ahrens  * (e.g. in syncing context), or the callback can deal with the possible races.
1833088f3894Sahrens  */
18343b2aab18SMatthew Ahrens static int
18353b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name,
18363b2aab18SMatthew Ahrens     int func(const char *, void *), void *arg, int flags)
1837fa9e4066Sahrens {
1838fa9e4066Sahrens 	dsl_dir_t *dd;
18393b2aab18SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
1840088f3894Sahrens 	dsl_dataset_t *ds;
1841fa9e4066Sahrens 	zap_cursor_t zc;
1842b7661cccSmmusante 	zap_attribute_t *attr;
1843fa9e4066Sahrens 	char *child;
1844088f3894Sahrens 	uint64_t thisobj;
1845088f3894Sahrens 	int err;
1846fa9e4066Sahrens 
18473b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dp, FTAG);
18483b2aab18SMatthew Ahrens 
18493b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
18503b2aab18SMatthew Ahrens 	if (err != 0) {
18513b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
18521d452cf5Sahrens 		return (err);
18533b2aab18SMatthew Ahrens 	}
1854fa9e4066Sahrens 
1855088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1856088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
18573b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
18583b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
1859088f3894Sahrens 		return (0);
1860088f3894Sahrens 	}
1861088f3894Sahrens 
1862c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1863b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1864fa9e4066Sahrens 
1865fa9e4066Sahrens 	/*
1866fa9e4066Sahrens 	 * Iterate over all children.
1867fa9e4066Sahrens 	 */
18680b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
1869088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1870c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
1871b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
18720b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
18733b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
18743b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
18753b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
1876fa9e4066Sahrens 
1877486ae710SMatthew Ahrens 			child = kmem_asprintf("%s/%s", name, attr->za_name);
18783b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
18793b2aab18SMatthew Ahrens 			err = dmu_objset_find_impl(spa, child,
18803b2aab18SMatthew Ahrens 			    func, arg, flags);
18813b2aab18SMatthew Ahrens 			dsl_pool_config_enter(dp, FTAG);
1882486ae710SMatthew Ahrens 			strfree(child);
18833b2aab18SMatthew Ahrens 			if (err != 0)
18840b69c2f0Sahrens 				break;
18850b69c2f0Sahrens 		}
18860b69c2f0Sahrens 		zap_cursor_fini(&zc);
18871d452cf5Sahrens 
18883b2aab18SMatthew Ahrens 		if (err != 0) {
18893b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
18903b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
1891b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
18920b69c2f0Sahrens 			return (err);
18930b69c2f0Sahrens 		}
1894fa9e4066Sahrens 	}
1895fa9e4066Sahrens 
1896fa9e4066Sahrens 	/*
1897fa9e4066Sahrens 	 * Iterate over all snapshots.
1898fa9e4066Sahrens 	 */
1899088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
1900088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1901088f3894Sahrens 
1902088f3894Sahrens 		if (err == 0) {
1903c1379625SJustin T. Gibbs 			uint64_t snapobj;
1904c1379625SJustin T. Gibbs 
1905c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1906088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
1907088f3894Sahrens 
1908088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1909088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
1910088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
19113b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
1912088f3894Sahrens 				    sizeof (uint64_t));
19133b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
1914088f3894Sahrens 
1915486ae710SMatthew Ahrens 				child = kmem_asprintf("%s@%s",
1916486ae710SMatthew Ahrens 				    name, attr->za_name);
19173b2aab18SMatthew Ahrens 				dsl_pool_config_exit(dp, FTAG);
19183b2aab18SMatthew Ahrens 				err = func(child, arg);
19193b2aab18SMatthew Ahrens 				dsl_pool_config_enter(dp, FTAG);
1920486ae710SMatthew Ahrens 				strfree(child);
19213b2aab18SMatthew Ahrens 				if (err != 0)
1922088f3894Sahrens 					break;
1923088f3894Sahrens 			}
1924088f3894Sahrens 			zap_cursor_fini(&zc);
1925fa9e4066Sahrens 		}
1926fa9e4066Sahrens 	}
1927fa9e4066Sahrens 
19283b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
1929b7661cccSmmusante 	kmem_free(attr, sizeof (zap_attribute_t));
19303b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dp, FTAG);
1931fa9e4066Sahrens 
19323b2aab18SMatthew Ahrens 	if (err != 0)
19331d452cf5Sahrens 		return (err);
19341d452cf5Sahrens 
19353b2aab18SMatthew Ahrens 	/* Apply to self. */
19363b2aab18SMatthew Ahrens 	return (func(name, arg));
1937fa9e4066Sahrens }
1938f18faf3fSek 
19393b2aab18SMatthew Ahrens /*
19403b2aab18SMatthew Ahrens  * See comment above dmu_objset_find_impl().
19413b2aab18SMatthew Ahrens  */
19427f73c863SRich Morris int
19433b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg,
19443b2aab18SMatthew Ahrens     int flags)
19457f73c863SRich Morris {
19463b2aab18SMatthew Ahrens 	spa_t *spa;
19473b2aab18SMatthew Ahrens 	int error;
19487f73c863SRich Morris 
19493b2aab18SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
19503b2aab18SMatthew Ahrens 	if (error != 0)
19513b2aab18SMatthew Ahrens 		return (error);
19523b2aab18SMatthew Ahrens 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
19533b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
19543b2aab18SMatthew Ahrens 	return (error);
19557f73c863SRich Morris }
19567f73c863SRich Morris 
1957f18faf3fSek void
1958f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr)
1959f18faf3fSek {
1960503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1961503ad85cSMatthew Ahrens 	os->os_user_ptr = user_ptr;
1962f18faf3fSek }
1963f18faf3fSek 
1964f18faf3fSek void *
1965f18faf3fSek dmu_objset_get_user(objset_t *os)
1966f18faf3fSek {
1967503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1968503ad85cSMatthew Ahrens 	return (os->os_user_ptr);
1969f18faf3fSek }
19703b2aab18SMatthew Ahrens 
19713b2aab18SMatthew Ahrens /*
19723b2aab18SMatthew Ahrens  * Determine name of filesystem, given name of snapshot.
19733b2aab18SMatthew Ahrens  * buf must be at least MAXNAMELEN bytes
19743b2aab18SMatthew Ahrens  */
19753b2aab18SMatthew Ahrens int
19763b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf)
19773b2aab18SMatthew Ahrens {
19783b2aab18SMatthew Ahrens 	char *atp = strchr(snapname, '@');
19793b2aab18SMatthew Ahrens 	if (atp == NULL)
1980be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
19813b2aab18SMatthew Ahrens 	if (atp - snapname >= MAXNAMELEN)
1982be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
19833b2aab18SMatthew Ahrens 	(void) strlcpy(buf, snapname, atp - snapname + 1);
19843b2aab18SMatthew Ahrens 	return (0);
19853b2aab18SMatthew Ahrens }
1986