xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision af346df58864e8fe897b1ff1a3a4c12f9294391b)
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.
238df0bcf0SPaul Dagnelie  * Copyright (c) 2012, 2016 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.
29c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
30fa9e4066Sahrens  */
31fa9e4066Sahrens 
3255da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
3355da60b9SMark J Musante 
34ecd6cf80Smarks #include <sys/cred.h>
35fa9e4066Sahrens #include <sys/zfs_context.h>
36fa9e4066Sahrens #include <sys/dmu_objset.h>
37fa9e4066Sahrens #include <sys/dsl_dir.h>
38fa9e4066Sahrens #include <sys/dsl_dataset.h>
39fa9e4066Sahrens #include <sys/dsl_prop.h>
40fa9e4066Sahrens #include <sys/dsl_pool.h>
411d452cf5Sahrens #include <sys/dsl_synctask.h>
42ecd6cf80Smarks #include <sys/dsl_deleg.h>
43fa9e4066Sahrens #include <sys/dnode.h>
44fa9e4066Sahrens #include <sys/dbuf.h>
45a2eea2e1Sahrens #include <sys/zvol.h>
46fa9e4066Sahrens #include <sys/dmu_tx.h>
47fa9e4066Sahrens #include <sys/zap.h>
48fa9e4066Sahrens #include <sys/zil.h>
49fa9e4066Sahrens #include <sys/dmu_impl.h>
50ecd6cf80Smarks #include <sys/zfs_ioctl.h>
510a586ceaSMark Shellenbaum #include <sys/sa.h>
5299d5e173STim Haley #include <sys/zfs_onexit.h>
533b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
5412380e1eSArne Jansen #include <sys/vdev.h>
55fa9e4066Sahrens 
56744947dcSTom Erickson /*
57744947dcSTom Erickson  * Needed to close a window in dnode_move() that allows the objset to be freed
58744947dcSTom Erickson  * before it can be safely accessed.
59744947dcSTom Erickson  */
60744947dcSTom Erickson krwlock_t os_lock;
61744947dcSTom Erickson 
6212380e1eSArne Jansen /*
6312380e1eSArne Jansen  * Tunable to overwrite the maximum number of threads for the parallization
6412380e1eSArne Jansen  * of dmu_objset_find_dp, needed to speed up the import of pools with many
6512380e1eSArne Jansen  * datasets.
6612380e1eSArne Jansen  * Default is 4 times the number of leaf vdevs.
6712380e1eSArne Jansen  */
6812380e1eSArne Jansen int dmu_find_threads = 0;
6912380e1eSArne Jansen 
70*af346df5SNed Bass /*
71*af346df5SNed Bass  * Backfill lower metadnode objects after this many have been freed.
72*af346df5SNed Bass  * Backfilling negatively impacts object creation rates, so only do it
73*af346df5SNed Bass  * if there are enough holes to fill.
74*af346df5SNed Bass  */
75*af346df5SNed Bass int dmu_rescan_dnode_threshold = 131072;
76*af346df5SNed Bass 
7712380e1eSArne Jansen static void dmu_objset_find_dp_cb(void *arg);
7812380e1eSArne Jansen 
79744947dcSTom Erickson void
80744947dcSTom Erickson dmu_objset_init(void)
81744947dcSTom Erickson {
82744947dcSTom Erickson 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
83744947dcSTom Erickson }
84744947dcSTom Erickson 
85744947dcSTom Erickson void
86744947dcSTom Erickson dmu_objset_fini(void)
87744947dcSTom Erickson {
88744947dcSTom Erickson 	rw_destroy(&os_lock);
89744947dcSTom Erickson }
90744947dcSTom Erickson 
91fa9e4066Sahrens spa_t *
92fa9e4066Sahrens dmu_objset_spa(objset_t *os)
93fa9e4066Sahrens {
94503ad85cSMatthew Ahrens 	return (os->os_spa);
95fa9e4066Sahrens }
96fa9e4066Sahrens 
97fa9e4066Sahrens zilog_t *
98fa9e4066Sahrens dmu_objset_zil(objset_t *os)
99fa9e4066Sahrens {
100503ad85cSMatthew Ahrens 	return (os->os_zil);
101fa9e4066Sahrens }
102fa9e4066Sahrens 
103fa9e4066Sahrens dsl_pool_t *
104fa9e4066Sahrens dmu_objset_pool(objset_t *os)
105fa9e4066Sahrens {
106fa9e4066Sahrens 	dsl_dataset_t *ds;
107fa9e4066Sahrens 
108503ad85cSMatthew Ahrens 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
109fa9e4066Sahrens 		return (ds->ds_dir->dd_pool);
110fa9e4066Sahrens 	else
111503ad85cSMatthew Ahrens 		return (spa_get_dsl(os->os_spa));
112fa9e4066Sahrens }
113fa9e4066Sahrens 
114fa9e4066Sahrens dsl_dataset_t *
115fa9e4066Sahrens dmu_objset_ds(objset_t *os)
116fa9e4066Sahrens {
117503ad85cSMatthew Ahrens 	return (os->os_dsl_dataset);
118fa9e4066Sahrens }
119fa9e4066Sahrens 
120fa9e4066Sahrens dmu_objset_type_t
121fa9e4066Sahrens dmu_objset_type(objset_t *os)
122fa9e4066Sahrens {
123503ad85cSMatthew Ahrens 	return (os->os_phys->os_type);
124fa9e4066Sahrens }
125fa9e4066Sahrens 
126fa9e4066Sahrens void
127fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
128fa9e4066Sahrens {
129503ad85cSMatthew Ahrens 	dsl_dataset_name(os->os_dsl_dataset, buf);
130fa9e4066Sahrens }
131fa9e4066Sahrens 
132fa9e4066Sahrens uint64_t
133fa9e4066Sahrens dmu_objset_id(objset_t *os)
134fa9e4066Sahrens {
135503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
136fa9e4066Sahrens 
137fa9e4066Sahrens 	return (ds ? ds->ds_object : 0);
138fa9e4066Sahrens }
139fa9e4066Sahrens 
140edf345e6SMatthew Ahrens zfs_sync_type_t
14155da60b9SMark J Musante dmu_objset_syncprop(objset_t *os)
14255da60b9SMark J Musante {
14355da60b9SMark J Musante 	return (os->os_sync);
14455da60b9SMark J Musante }
14555da60b9SMark J Musante 
146edf345e6SMatthew Ahrens zfs_logbias_op_t
147e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
148e09fa4daSNeil Perrin {
149e09fa4daSNeil Perrin 	return (os->os_logbias);
150e09fa4daSNeil Perrin }
151e09fa4daSNeil Perrin 
152fa9e4066Sahrens static void
153fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
154fa9e4066Sahrens {
155503ad85cSMatthew Ahrens 	objset_t *os = arg;
156fa9e4066Sahrens 
157fa9e4066Sahrens 	/*
158fa9e4066Sahrens 	 * Inheritance should have been done by now.
159fa9e4066Sahrens 	 */
160fa9e4066Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
161fa9e4066Sahrens 
162503ad85cSMatthew Ahrens 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
163fa9e4066Sahrens }
164fa9e4066Sahrens 
165fa9e4066Sahrens static void
166fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
167fa9e4066Sahrens {
168503ad85cSMatthew Ahrens 	objset_t *os = arg;
169fa9e4066Sahrens 
170fa9e4066Sahrens 	/*
171fa9e4066Sahrens 	 * Inheritance and range checking should have been done by now.
172fa9e4066Sahrens 	 */
173fa9e4066Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
174fa9e4066Sahrens 
175db1741f5SJustin T. Gibbs 	os->os_compress = zio_compress_select(os->os_spa, newval,
176db1741f5SJustin T. Gibbs 	    ZIO_COMPRESS_ON);
177fa9e4066Sahrens }
178fa9e4066Sahrens 
179d0ad202dSahrens static void
180d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
181d0ad202dSahrens {
182503ad85cSMatthew Ahrens 	objset_t *os = arg;
183d0ad202dSahrens 
184d0ad202dSahrens 	/*
185d0ad202dSahrens 	 * Inheritance and range checking should have been done by now.
186d0ad202dSahrens 	 */
187d0ad202dSahrens 	ASSERT(newval > 0);
188503ad85cSMatthew Ahrens 	ASSERT(newval <= spa_max_replication(os->os_spa));
189d0ad202dSahrens 
190503ad85cSMatthew Ahrens 	os->os_copies = newval;
191d0ad202dSahrens }
192d0ad202dSahrens 
193b24ab676SJeff Bonwick static void
194b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
195b24ab676SJeff Bonwick {
196b24ab676SJeff Bonwick 	objset_t *os = arg;
197b24ab676SJeff Bonwick 	spa_t *spa = os->os_spa;
198b24ab676SJeff Bonwick 	enum zio_checksum checksum;
199b24ab676SJeff Bonwick 
200b24ab676SJeff Bonwick 	/*
201b24ab676SJeff Bonwick 	 * Inheritance should have been done by now.
202b24ab676SJeff Bonwick 	 */
203b24ab676SJeff Bonwick 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
204b24ab676SJeff Bonwick 
205b24ab676SJeff Bonwick 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
206b24ab676SJeff Bonwick 
207b24ab676SJeff Bonwick 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
208b24ab676SJeff Bonwick 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
209b24ab676SJeff Bonwick }
210b24ab676SJeff Bonwick 
2113baa08fcSek static void
2123baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval)
2133baa08fcSek {
214503ad85cSMatthew Ahrens 	objset_t *os = arg;
2153baa08fcSek 
2163baa08fcSek 	/*
2173baa08fcSek 	 * Inheritance and range checking should have been done by now.
2183baa08fcSek 	 */
2193baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2203baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2213baa08fcSek 
222503ad85cSMatthew Ahrens 	os->os_primary_cache = newval;
2233baa08fcSek }
2243baa08fcSek 
2253baa08fcSek static void
2263baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval)
2273baa08fcSek {
228503ad85cSMatthew Ahrens 	objset_t *os = arg;
2293baa08fcSek 
2303baa08fcSek 	/*
2313baa08fcSek 	 * Inheritance and range checking should have been done by now.
2323baa08fcSek 	 */
2333baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2343baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2353baa08fcSek 
236503ad85cSMatthew Ahrens 	os->os_secondary_cache = newval;
2373baa08fcSek }
2383baa08fcSek 
23955da60b9SMark J Musante static void
24055da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval)
24155da60b9SMark J Musante {
24255da60b9SMark J Musante 	objset_t *os = arg;
24355da60b9SMark J Musante 
24455da60b9SMark J Musante 	/*
24555da60b9SMark J Musante 	 * Inheritance and range checking should have been done by now.
24655da60b9SMark J Musante 	 */
24755da60b9SMark J Musante 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
24855da60b9SMark J Musante 	    newval == ZFS_SYNC_DISABLED);
24955da60b9SMark J Musante 
25055da60b9SMark J Musante 	os->os_sync = newval;
25155da60b9SMark J Musante 	if (os->os_zil)
25255da60b9SMark J Musante 		zil_set_sync(os->os_zil, newval);
25355da60b9SMark J Musante }
25455da60b9SMark J Musante 
255edf345e6SMatthew Ahrens static void
256edf345e6SMatthew Ahrens redundant_metadata_changed_cb(void *arg, uint64_t newval)
257edf345e6SMatthew Ahrens {
258edf345e6SMatthew Ahrens 	objset_t *os = arg;
259edf345e6SMatthew Ahrens 
260edf345e6SMatthew Ahrens 	/*
261edf345e6SMatthew Ahrens 	 * Inheritance and range checking should have been done by now.
262edf345e6SMatthew Ahrens 	 */
263edf345e6SMatthew Ahrens 	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
264edf345e6SMatthew Ahrens 	    newval == ZFS_REDUNDANT_METADATA_MOST);
265edf345e6SMatthew Ahrens 
266edf345e6SMatthew Ahrens 	os->os_redundant_metadata = newval;
267edf345e6SMatthew Ahrens }
268edf345e6SMatthew Ahrens 
269e09fa4daSNeil Perrin static void
270e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
271e09fa4daSNeil Perrin {
272e09fa4daSNeil Perrin 	objset_t *os = arg;
273e09fa4daSNeil Perrin 
274e09fa4daSNeil Perrin 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
275e09fa4daSNeil Perrin 	    newval == ZFS_LOGBIAS_THROUGHPUT);
276e09fa4daSNeil Perrin 	os->os_logbias = newval;
277e09fa4daSNeil Perrin 	if (os->os_zil)
278e09fa4daSNeil Perrin 		zil_set_logbias(os->os_zil, newval);
279e09fa4daSNeil Perrin }
280e09fa4daSNeil Perrin 
281b5152584SMatthew Ahrens static void
282b5152584SMatthew Ahrens recordsize_changed_cb(void *arg, uint64_t newval)
283b5152584SMatthew Ahrens {
284b5152584SMatthew Ahrens 	objset_t *os = arg;
285b5152584SMatthew Ahrens 
286b5152584SMatthew Ahrens 	os->os_recordsize = newval;
287b5152584SMatthew Ahrens }
288b5152584SMatthew Ahrens 
289fa9e4066Sahrens void
290fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
291fa9e4066Sahrens {
292fa9e4066Sahrens 	objset_phys_t *osp = buf;
293fa9e4066Sahrens 
29414843421SMatthew Ahrens 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
295fa9e4066Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
296fa9e4066Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
297fa9e4066Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
29814843421SMatthew Ahrens 	osp->os_flags = BSWAP_64(osp->os_flags);
29914843421SMatthew Ahrens 	if (size == sizeof (objset_phys_t)) {
30014843421SMatthew Ahrens 		dnode_byteswap(&osp->os_userused_dnode);
30114843421SMatthew Ahrens 		dnode_byteswap(&osp->os_groupused_dnode);
30214843421SMatthew Ahrens 	}
303fa9e4066Sahrens }
304fa9e4066Sahrens 
305ea8dc4b6Seschrock int
306ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
307503ad85cSMatthew Ahrens     objset_t **osp)
308fa9e4066Sahrens {
309503ad85cSMatthew Ahrens 	objset_t *os;
310088f3894Sahrens 	int i, err;
311fa9e4066Sahrens 
31291ebeef5Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
31391ebeef5Sahrens 
314503ad85cSMatthew Ahrens 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
315503ad85cSMatthew Ahrens 	os->os_dsl_dataset = ds;
316503ad85cSMatthew Ahrens 	os->os_spa = spa;
317503ad85cSMatthew Ahrens 	os->os_rootbp = bp;
318503ad85cSMatthew Ahrens 	if (!BP_IS_HOLE(os->os_rootbp)) {
3197adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
3207802d7bfSMatthew Ahrens 		zbookmark_phys_t zb;
321b24ab676SJeff Bonwick 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
322b24ab676SJeff Bonwick 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
323b24ab676SJeff Bonwick 
324503ad85cSMatthew Ahrens 		if (DMU_OS_IS_L2CACHEABLE(os))
3257adb730bSGeorge Wilson 			aflags |= ARC_FLAG_L2CACHE;
326ea8dc4b6Seschrock 
327503ad85cSMatthew Ahrens 		dprintf_bp(os->os_rootbp, "reading %s", "");
3281b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, os->os_rootbp,
329503ad85cSMatthew Ahrens 		    arc_getbuf_func, &os->os_phys_buf,
33013506d1eSmaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
3313b2aab18SMatthew Ahrens 		if (err != 0) {
332503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
333b87f3af3Sperrin 			/* convert checksum errors into IO errors */
334b87f3af3Sperrin 			if (err == ECKSUM)
335be6fd75aSMatthew Ahrens 				err = SET_ERROR(EIO);
336ea8dc4b6Seschrock 			return (err);
337ea8dc4b6Seschrock 		}
33814843421SMatthew Ahrens 
33914843421SMatthew Ahrens 		/* Increase the blocksize if we are permitted. */
34014843421SMatthew Ahrens 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
341503ad85cSMatthew Ahrens 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
342dcbf3bd6SGeorge Wilson 			arc_buf_t *buf = arc_alloc_buf(spa,
343503ad85cSMatthew Ahrens 			    sizeof (objset_phys_t), &os->os_phys_buf,
34414843421SMatthew Ahrens 			    ARC_BUFC_METADATA);
34514843421SMatthew Ahrens 			bzero(buf->b_data, sizeof (objset_phys_t));
346503ad85cSMatthew Ahrens 			bcopy(os->os_phys_buf->b_data, buf->b_data,
347503ad85cSMatthew Ahrens 			    arc_buf_size(os->os_phys_buf));
348dcbf3bd6SGeorge Wilson 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
349503ad85cSMatthew Ahrens 			os->os_phys_buf = buf;
35014843421SMatthew Ahrens 		}
35114843421SMatthew Ahrens 
352503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
353503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
354fa9e4066Sahrens 	} else {
35514843421SMatthew Ahrens 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
35614843421SMatthew Ahrens 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
357dcbf3bd6SGeorge Wilson 		os->os_phys_buf = arc_alloc_buf(spa, size,
358503ad85cSMatthew Ahrens 		    &os->os_phys_buf, ARC_BUFC_METADATA);
359503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
360503ad85cSMatthew Ahrens 		bzero(os->os_phys, size);
361fa9e4066Sahrens 	}
362fa9e4066Sahrens 
363fa9e4066Sahrens 	/*
364fa9e4066Sahrens 	 * Note: the changed_cb will be called once before the register
365fa9e4066Sahrens 	 * func returns, thus changing the checksum/compression from the
3663baa08fcSek 	 * default (fletcher2/off).  Snapshots don't need to know about
3673baa08fcSek 	 * checksum/compression/copies.
368fa9e4066Sahrens 	 */
3695d7b4d43SMatthew Ahrens 	if (ds != NULL) {
3709c3fd121SMatthew Ahrens 		boolean_t needlock = B_FALSE;
3719c3fd121SMatthew Ahrens 
3729c3fd121SMatthew Ahrens 		/*
3739c3fd121SMatthew Ahrens 		 * Note: it's valid to open the objset if the dataset is
3749c3fd121SMatthew Ahrens 		 * long-held, in which case the pool_config lock will not
3759c3fd121SMatthew Ahrens 		 * be held.
3769c3fd121SMatthew Ahrens 		 */
3779c3fd121SMatthew Ahrens 		if (!dsl_pool_config_held(dmu_objset_pool(os))) {
3789c3fd121SMatthew Ahrens 			needlock = B_TRUE;
3799c3fd121SMatthew Ahrens 			dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
3809c3fd121SMatthew Ahrens 		}
3813b2aab18SMatthew Ahrens 		err = dsl_prop_register(ds,
3823b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
383503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
3843b2aab18SMatthew Ahrens 		if (err == 0) {
3853b2aab18SMatthew Ahrens 			err = dsl_prop_register(ds,
3863b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
387503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
3883b2aab18SMatthew Ahrens 		}
389bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
3903b2aab18SMatthew Ahrens 			if (err == 0) {
3913b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3923b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
393503ad85cSMatthew Ahrens 				    checksum_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_COMPRESSION),
398503ad85cSMatthew Ahrens 				    compression_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_COPIES),
403503ad85cSMatthew Ahrens 				    copies_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_DEDUP),
408b24ab676SJeff Bonwick 				    dedup_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_LOGBIAS),
413e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
4143b2aab18SMatthew Ahrens 			}
4153b2aab18SMatthew Ahrens 			if (err == 0) {
4163b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
4173b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_SYNC),
41855da60b9SMark J Musante 				    sync_changed_cb, os);
4193b2aab18SMatthew Ahrens 			}
420edf345e6SMatthew Ahrens 			if (err == 0) {
421edf345e6SMatthew Ahrens 				err = dsl_prop_register(ds,
422edf345e6SMatthew Ahrens 				    zfs_prop_to_name(
423edf345e6SMatthew Ahrens 				    ZFS_PROP_REDUNDANT_METADATA),
424edf345e6SMatthew Ahrens 				    redundant_metadata_changed_cb, os);
425edf345e6SMatthew Ahrens 			}
426b5152584SMatthew Ahrens 			if (err == 0) {
427b5152584SMatthew Ahrens 				err = dsl_prop_register(ds,
428b5152584SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
429b5152584SMatthew Ahrens 				    recordsize_changed_cb, os);
430b5152584SMatthew Ahrens 			}
4313baa08fcSek 		}
4329c3fd121SMatthew Ahrens 		if (needlock)
4339c3fd121SMatthew Ahrens 			dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
4343b2aab18SMatthew Ahrens 		if (err != 0) {
435dcbf3bd6SGeorge Wilson 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
436503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
437ea8dc4b6Seschrock 			return (err);
438ea8dc4b6Seschrock 		}
4395d7b4d43SMatthew Ahrens 	} else {
440fa9e4066Sahrens 		/* It's the meta-objset. */
441503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
442db1741f5SJustin T. Gibbs 		os->os_compress = ZIO_COMPRESS_ON;
443503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
444b24ab676SJeff Bonwick 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
445edf345e6SMatthew Ahrens 		os->os_dedup_verify = B_FALSE;
446edf345e6SMatthew Ahrens 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
447edf345e6SMatthew Ahrens 		os->os_sync = ZFS_SYNC_STANDARD;
448503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
449503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
450fa9e4066Sahrens 	}
451fa9e4066Sahrens 
452bc9014e6SJustin Gibbs 	if (ds == NULL || !ds->ds_is_snapshot)
4536e0cbcaaSMatthew Ahrens 		os->os_zil_header = os->os_phys->os_zil_header;
454503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
455fa9e4066Sahrens 
456fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
457503ad85cSMatthew Ahrens 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
458fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
459503ad85cSMatthew Ahrens 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
460fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
461fa9e4066Sahrens 	}
462503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
463fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
464503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
465fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
466fa9e4066Sahrens 
467503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
468503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
469503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
470503ad85cSMatthew Ahrens 
471bc9014e6SJustin Gibbs 	dnode_special_open(os, &os->os_phys->os_meta_dnode,
472bc9014e6SJustin Gibbs 	    DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
473503ad85cSMatthew Ahrens 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
474bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_userused_dnode,
475bc9014e6SJustin Gibbs 		    DMU_USERUSED_OBJECT, &os->os_userused_dnode);
476bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_groupused_dnode,
477bc9014e6SJustin Gibbs 		    DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
47814843421SMatthew Ahrens 	}
479fa9e4066Sahrens 
480503ad85cSMatthew Ahrens 	*osp = os;
481ea8dc4b6Seschrock 	return (0);
482fa9e4066Sahrens }
483fa9e4066Sahrens 
484503ad85cSMatthew Ahrens int
485503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4863cb34c60Sahrens {
487503ad85cSMatthew Ahrens 	int err = 0;
4883cb34c60Sahrens 
4899c3fd121SMatthew Ahrens 	/*
4909c3fd121SMatthew Ahrens 	 * We shouldn't be doing anything with dsl_dataset_t's unless the
4919c3fd121SMatthew Ahrens 	 * pool_config lock is held, or the dataset is long-held.
4929c3fd121SMatthew Ahrens 	 */
4939c3fd121SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
4949c3fd121SMatthew Ahrens 	    dsl_dataset_long_held(ds));
4959c3fd121SMatthew Ahrens 
4963cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
4975d7b4d43SMatthew Ahrens 	if (ds->ds_objset == NULL) {
4985d7b4d43SMatthew Ahrens 		objset_t *os;
499c166b69dSPaul Dagnelie 		rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
5003cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
5015d7b4d43SMatthew Ahrens 		    ds, dsl_dataset_get_blkptr(ds), &os);
502c166b69dSPaul Dagnelie 		rrw_exit(&ds->ds_bp_rwlock, FTAG);
5035d7b4d43SMatthew Ahrens 
5045d7b4d43SMatthew Ahrens 		if (err == 0) {
5055d7b4d43SMatthew Ahrens 			mutex_enter(&ds->ds_lock);
5065d7b4d43SMatthew Ahrens 			ASSERT(ds->ds_objset == NULL);
5075d7b4d43SMatthew Ahrens 			ds->ds_objset = os;
5085d7b4d43SMatthew Ahrens 			mutex_exit(&ds->ds_lock);
5095d7b4d43SMatthew Ahrens 		}
5103cb34c60Sahrens 	}
5115d7b4d43SMatthew Ahrens 	*osp = ds->ds_objset;
5123cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
513503ad85cSMatthew Ahrens 	return (err);
5143cb34c60Sahrens }
5153cb34c60Sahrens 
5163b2aab18SMatthew Ahrens /*
5173b2aab18SMatthew Ahrens  * Holds the pool while the objset is held.  Therefore only one objset
5183b2aab18SMatthew Ahrens  * can be held at a time.
5193b2aab18SMatthew Ahrens  */
5203cb34c60Sahrens int
521503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
5223cb34c60Sahrens {
5233b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
524503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
5253cb34c60Sahrens 	int err;
5263cb34c60Sahrens 
5273b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, tag, &dp);
5283b2aab18SMatthew Ahrens 	if (err != 0)
5293b2aab18SMatthew Ahrens 		return (err);
5303b2aab18SMatthew Ahrens 	err = dsl_dataset_hold(dp, name, tag, &ds);
5313b2aab18SMatthew Ahrens 	if (err != 0) {
5323b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
533503ad85cSMatthew Ahrens 		return (err);
5343b2aab18SMatthew Ahrens 	}
535503ad85cSMatthew Ahrens 
536503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
5373b2aab18SMatthew Ahrens 	if (err != 0) {
538503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
5393b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
5403b2aab18SMatthew Ahrens 	}
541503ad85cSMatthew Ahrens 
5423cb34c60Sahrens 	return (err);
5433cb34c60Sahrens }
5443cb34c60Sahrens 
54512380e1eSArne Jansen static int
54612380e1eSArne Jansen dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
54712380e1eSArne Jansen     boolean_t readonly, void *tag, objset_t **osp)
54812380e1eSArne Jansen {
54912380e1eSArne Jansen 	int err;
55012380e1eSArne Jansen 
55112380e1eSArne Jansen 	err = dmu_objset_from_ds(ds, osp);
55212380e1eSArne Jansen 	if (err != 0) {
55312380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
55412380e1eSArne Jansen 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
55512380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
55612380e1eSArne Jansen 		return (SET_ERROR(EINVAL));
55712380e1eSArne Jansen 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
55812380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
55912380e1eSArne Jansen 		return (SET_ERROR(EROFS));
56012380e1eSArne Jansen 	}
56112380e1eSArne Jansen 	return (err);
56212380e1eSArne Jansen }
56312380e1eSArne Jansen 
5643b2aab18SMatthew Ahrens /*
5653b2aab18SMatthew Ahrens  * dsl_pool must not be held when this is called.
5663b2aab18SMatthew Ahrens  * Upon successful return, there will be a longhold on the dataset,
5673b2aab18SMatthew Ahrens  * and the dsl_pool will not be held.
5683b2aab18SMatthew Ahrens  */
569fa9e4066Sahrens int
570503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
571503ad85cSMatthew Ahrens     boolean_t readonly, void *tag, objset_t **osp)
572fa9e4066Sahrens {
5733b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
574f18faf3fSek 	dsl_dataset_t *ds;
575f18faf3fSek 	int err;
576fa9e4066Sahrens 
5773b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
5783b2aab18SMatthew Ahrens 	if (err != 0)
5793b2aab18SMatthew Ahrens 		return (err);
5803b2aab18SMatthew Ahrens 	err = dsl_dataset_own(dp, name, tag, &ds);
5813b2aab18SMatthew Ahrens 	if (err != 0) {
5823b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
583fa9e4066Sahrens 		return (err);
5843b2aab18SMatthew Ahrens 	}
58512380e1eSArne Jansen 	err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
5863b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
58712380e1eSArne Jansen 
5883cb34c60Sahrens 	return (err);
589fa9e4066Sahrens }
590fa9e4066Sahrens 
59112380e1eSArne Jansen int
59212380e1eSArne Jansen dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
59312380e1eSArne Jansen     boolean_t readonly, void *tag, objset_t **osp)
59412380e1eSArne Jansen {
59512380e1eSArne Jansen 	dsl_dataset_t *ds;
59612380e1eSArne Jansen 	int err;
59712380e1eSArne Jansen 
59812380e1eSArne Jansen 	err = dsl_dataset_own_obj(dp, obj, tag, &ds);
59912380e1eSArne Jansen 	if (err != 0)
60012380e1eSArne Jansen 		return (err);
60112380e1eSArne Jansen 
60212380e1eSArne Jansen 	return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
60312380e1eSArne Jansen }
60412380e1eSArne Jansen 
605fa9e4066Sahrens void
606503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
607fa9e4066Sahrens {
6083b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(os);
609503ad85cSMatthew Ahrens 	dsl_dataset_rele(os->os_dsl_dataset, tag);
6103b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, tag);
611503ad85cSMatthew Ahrens }
612503ad85cSMatthew Ahrens 
61391948b51SKeith M Wesolowski /*
61491948b51SKeith M Wesolowski  * When we are called, os MUST refer to an objset associated with a dataset
61591948b51SKeith M Wesolowski  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
61691948b51SKeith M Wesolowski  * == tag.  We will then release and reacquire ownership of the dataset while
61791948b51SKeith M Wesolowski  * holding the pool config_rwlock to avoid intervening namespace or ownership
61891948b51SKeith M Wesolowski  * changes may occur.
61991948b51SKeith M Wesolowski  *
62091948b51SKeith M Wesolowski  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
62191948b51SKeith M Wesolowski  * release the hold on its dataset and acquire a new one on the dataset of the
62291948b51SKeith M Wesolowski  * same name so that it can be partially torn down and reconstructed.
62391948b51SKeith M Wesolowski  */
62491948b51SKeith M Wesolowski void
62591948b51SKeith M Wesolowski dmu_objset_refresh_ownership(objset_t *os, void *tag)
62691948b51SKeith M Wesolowski {
62791948b51SKeith M Wesolowski 	dsl_pool_t *dp;
62891948b51SKeith M Wesolowski 	dsl_dataset_t *ds, *newds;
6299adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
63091948b51SKeith M Wesolowski 
63191948b51SKeith M Wesolowski 	ds = os->os_dsl_dataset;
63291948b51SKeith M Wesolowski 	VERIFY3P(ds, !=, NULL);
63391948b51SKeith M Wesolowski 	VERIFY3P(ds->ds_owner, ==, tag);
63491948b51SKeith M Wesolowski 	VERIFY(dsl_dataset_long_held(ds));
63591948b51SKeith M Wesolowski 
63691948b51SKeith M Wesolowski 	dsl_dataset_name(ds, name);
63791948b51SKeith M Wesolowski 	dp = dmu_objset_pool(os);
63891948b51SKeith M Wesolowski 	dsl_pool_config_enter(dp, FTAG);
63991948b51SKeith M Wesolowski 	dmu_objset_disown(os, tag);
64091948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
64191948b51SKeith M Wesolowski 	VERIFY3P(newds, ==, os->os_dsl_dataset);
64291948b51SKeith M Wesolowski 	dsl_pool_config_exit(dp, FTAG);
64391948b51SKeith M Wesolowski }
64491948b51SKeith M Wesolowski 
645503ad85cSMatthew Ahrens void
646503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
647503ad85cSMatthew Ahrens {
648503ad85cSMatthew Ahrens 	dsl_dataset_disown(os->os_dsl_dataset, tag);
649fa9e4066Sahrens }
650fa9e4066Sahrens 
6513b2aab18SMatthew Ahrens void
6521934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
653ea8dc4b6Seschrock {
654bc9014e6SJustin Gibbs 	dnode_t dn_marker;
655ea8dc4b6Seschrock 	dnode_t *dn;
656c543ec06Sahrens 
657503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
658bc9014e6SJustin Gibbs 	dn = list_head(&os->os_dnodes);
659bc9014e6SJustin Gibbs 	while (dn != NULL) {
660bc9014e6SJustin Gibbs 		/*
661bc9014e6SJustin Gibbs 		 * Skip dnodes without holds.  We have to do this dance
662bc9014e6SJustin Gibbs 		 * because dnode_add_ref() only works if there is already a
663bc9014e6SJustin Gibbs 		 * hold.  If the dnode has no holds, then it has no dbufs.
664bc9014e6SJustin Gibbs 		 */
665bc9014e6SJustin Gibbs 		if (dnode_add_ref(dn, FTAG)) {
666bc9014e6SJustin Gibbs 			list_insert_after(&os->os_dnodes, dn, &dn_marker);
667bc9014e6SJustin Gibbs 			mutex_exit(&os->os_lock);
668c543ec06Sahrens 
669bc9014e6SJustin Gibbs 			dnode_evict_dbufs(dn);
670bc9014e6SJustin Gibbs 			dnode_rele(dn, FTAG);
671c543ec06Sahrens 
672bc9014e6SJustin Gibbs 			mutex_enter(&os->os_lock);
673bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, &dn_marker);
674bc9014e6SJustin Gibbs 			list_remove(&os->os_dnodes, &dn_marker);
675bc9014e6SJustin Gibbs 		} else {
676bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, dn);
677bc9014e6SJustin Gibbs 		}
678ea8dc4b6Seschrock 	}
679503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
680bc9014e6SJustin Gibbs 
681bc9014e6SJustin Gibbs 	if (DMU_USERUSED_DNODE(os) != NULL) {
682bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
683bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
684bc9014e6SJustin Gibbs 	}
685bc9014e6SJustin Gibbs 	dnode_evict_dbufs(DMU_META_DNODE(os));
686ea8dc4b6Seschrock }
687ea8dc4b6Seschrock 
688bc9014e6SJustin Gibbs /*
689bc9014e6SJustin Gibbs  * Objset eviction processing is split into into two pieces.
690bc9014e6SJustin Gibbs  * The first marks the objset as evicting, evicts any dbufs that
691bc9014e6SJustin Gibbs  * have a refcount of zero, and then queues up the objset for the
692bc9014e6SJustin Gibbs  * second phase of eviction.  Once os->os_dnodes has been cleared by
693bc9014e6SJustin Gibbs  * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
694bc9014e6SJustin Gibbs  * The second phase closes the special dnodes, dequeues the objset from
695bc9014e6SJustin Gibbs  * the list of those undergoing eviction, and finally frees the objset.
696bc9014e6SJustin Gibbs  *
697bc9014e6SJustin Gibbs  * NOTE: Due to asynchronous eviction processing (invocation of
698bc9014e6SJustin Gibbs  *       dnode_buf_pageout()), it is possible for the meta dnode for the
699bc9014e6SJustin Gibbs  *       objset to have no holds even though os->os_dnodes is not empty.
700bc9014e6SJustin Gibbs  */
701fa9e4066Sahrens void
702503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
703fa9e4066Sahrens {
704503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
705fa9e4066Sahrens 
706b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
707b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, t));
708fa9e4066Sahrens 
70903bad06fSJustin Gibbs 	if (ds)
71003bad06fSJustin Gibbs 		dsl_prop_unregister_all(ds, os);
711fa9e4066Sahrens 
7120a586ceaSMark Shellenbaum 	if (os->os_sa)
7130a586ceaSMark Shellenbaum 		sa_tear_down(os);
7140a586ceaSMark Shellenbaum 
7153b2aab18SMatthew Ahrens 	dmu_objset_evict_dbufs(os);
716ea8dc4b6Seschrock 
717bc9014e6SJustin Gibbs 	mutex_enter(&os->os_lock);
718bc9014e6SJustin Gibbs 	spa_evicting_os_register(os->os_spa, os);
719bc9014e6SJustin Gibbs 	if (list_is_empty(&os->os_dnodes)) {
720bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
721bc9014e6SJustin Gibbs 		dmu_objset_evict_done(os);
722bc9014e6SJustin Gibbs 	} else {
723bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
724bc9014e6SJustin Gibbs 	}
725bc9014e6SJustin Gibbs }
726bc9014e6SJustin Gibbs 
727bc9014e6SJustin Gibbs void
728bc9014e6SJustin Gibbs dmu_objset_evict_done(objset_t *os)
729bc9014e6SJustin Gibbs {
730bc9014e6SJustin Gibbs 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
731bc9014e6SJustin Gibbs 
732744947dcSTom Erickson 	dnode_special_close(&os->os_meta_dnode);
733744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os)) {
734744947dcSTom Erickson 		dnode_special_close(&os->os_userused_dnode);
735744947dcSTom Erickson 		dnode_special_close(&os->os_groupused_dnode);
73614843421SMatthew Ahrens 	}
737503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
738fa9e4066Sahrens 
739dcbf3bd6SGeorge Wilson 	arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
740744947dcSTom Erickson 
741744947dcSTom Erickson 	/*
742744947dcSTom Erickson 	 * This is a barrier to prevent the objset from going away in
743744947dcSTom Erickson 	 * dnode_move() until we can safely ensure that the objset is still in
744744947dcSTom Erickson 	 * use. We consider the objset valid before the barrier and invalid
745744947dcSTom Erickson 	 * after the barrier.
746744947dcSTom Erickson 	 */
747744947dcSTom Erickson 	rw_enter(&os_lock, RW_READER);
748744947dcSTom Erickson 	rw_exit(&os_lock);
749744947dcSTom Erickson 
750503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
751503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
752503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
753bc9014e6SJustin Gibbs 	spa_evicting_os_deregister(os->os_spa, os);
754503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
755fa9e4066Sahrens }
756fa9e4066Sahrens 
75771eb0538SChris Kirby timestruc_t
75871eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
75971eb0538SChris Kirby {
76071eb0538SChris Kirby 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
76171eb0538SChris Kirby }
76271eb0538SChris Kirby 
763fa9e4066Sahrens /* called from dsl for meta-objset */
764503ad85cSMatthew Ahrens objset_t *
765c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
766c717a561Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
767fa9e4066Sahrens {
768503ad85cSMatthew Ahrens 	objset_t *os;
769fa9e4066Sahrens 	dnode_t *mdn;
770fa9e4066Sahrens 
771fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7723b2aab18SMatthew Ahrens 
773feaa74e4SMark Maybee 	if (ds != NULL)
7743b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_from_ds(ds, &os));
775feaa74e4SMark Maybee 	else
7763b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
777feaa74e4SMark Maybee 
778744947dcSTom Erickson 	mdn = DMU_META_DNODE(os);
779fa9e4066Sahrens 
780fa9e4066Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
781fa9e4066Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
782fa9e4066Sahrens 
783fa9e4066Sahrens 	/*
784fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
785fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
786fa9e4066Sahrens 	 * we are also accessing it in open context.
787fa9e4066Sahrens 	 *
788fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
789fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
790fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
791fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
792fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
793fa9e4066Sahrens 	 */
794ea8dc4b6Seschrock 	if (ds != NULL) {
795ea8dc4b6Seschrock 		int levels = 1;
796ea8dc4b6Seschrock 
797ea8dc4b6Seschrock 		/*
798ea8dc4b6Seschrock 		 * Determine the number of levels necessary for the meta-dnode
7994b5c8e93SMatthew Ahrens 		 * to contain DN_MAX_OBJECT dnodes.  Note that in order to
8004b5c8e93SMatthew Ahrens 		 * ensure that we do not overflow 64 bits, there has to be
8014b5c8e93SMatthew Ahrens 		 * a nlevels that gives us a number of blocks > DN_MAX_OBJECT
8024b5c8e93SMatthew Ahrens 		 * but < 2^64.  Therefore,
8034b5c8e93SMatthew Ahrens 		 * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) (10) must be
8044b5c8e93SMatthew Ahrens 		 * less than (64 - log2(DN_MAX_OBJECT)) (16).
805ea8dc4b6Seschrock 		 */
8064b5c8e93SMatthew Ahrens 		while ((uint64_t)mdn->dn_nblkptr <<
8074b5c8e93SMatthew Ahrens 		    (mdn->dn_datablkshift - DNODE_SHIFT +
808ea8dc4b6Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
8094b5c8e93SMatthew Ahrens 		    DN_MAX_OBJECT)
810ea8dc4b6Seschrock 			levels++;
811ea8dc4b6Seschrock 
812fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
813ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
814ea8dc4b6Seschrock 	}
815fa9e4066Sahrens 
816fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
817fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
818fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
819503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
820503ad85cSMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
821503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
822503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
82314843421SMatthew Ahrens 	}
824fa9e4066Sahrens 
825fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
826fa9e4066Sahrens 
827503ad85cSMatthew Ahrens 	return (os);
828fa9e4066Sahrens }
829fa9e4066Sahrens 
8303b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg {
8313b2aab18SMatthew Ahrens 	const char *doca_name;
8323b2aab18SMatthew Ahrens 	cred_t *doca_cred;
8333b2aab18SMatthew Ahrens 	void (*doca_userfunc)(objset_t *os, void *arg,
8343b2aab18SMatthew Ahrens 	    cred_t *cr, dmu_tx_t *tx);
8353b2aab18SMatthew Ahrens 	void *doca_userarg;
8363b2aab18SMatthew Ahrens 	dmu_objset_type_t doca_type;
8373b2aab18SMatthew Ahrens 	uint64_t doca_flags;
8383b2aab18SMatthew Ahrens } dmu_objset_create_arg_t;
839fa9e4066Sahrens 
840ecd6cf80Smarks /*ARGSUSED*/
841fa9e4066Sahrens static int
8423b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx)
843fa9e4066Sahrens {
8443b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
8453b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8463b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
8473b2aab18SMatthew Ahrens 	const char *tail;
8483b2aab18SMatthew Ahrens 	int error;
8491d452cf5Sahrens 
8503b2aab18SMatthew Ahrens 	if (strchr(doca->doca_name, '@') != NULL)
851be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8521d452cf5Sahrens 
8539adfa60dSMatthew Ahrens 	if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
8549adfa60dSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
8559adfa60dSMatthew Ahrens 
8563b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
8573b2aab18SMatthew Ahrens 	if (error != 0)
8583b2aab18SMatthew Ahrens 		return (error);
8593b2aab18SMatthew Ahrens 	if (tail == NULL) {
8603b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
861be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
8621d452cf5Sahrens 	}
863a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
864a2afb611SJerry Jelinek 	    doca->doca_cred);
8653b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
866ecd6cf80Smarks 
867a2afb611SJerry Jelinek 	return (error);
8681d452cf5Sahrens }
8691d452cf5Sahrens 
8701d452cf5Sahrens static void
8713b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
8721d452cf5Sahrens {
8733b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
8743b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8753b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
8763b2aab18SMatthew Ahrens 	const char *tail;
8774445fffbSMatthew Ahrens 	dsl_dataset_t *ds;
8783b2aab18SMatthew Ahrens 	uint64_t obj;
8794445fffbSMatthew Ahrens 	blkptr_t *bp;
8803b2aab18SMatthew Ahrens 	objset_t *os;
881fa9e4066Sahrens 
8823b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
883fa9e4066Sahrens 
8843b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
8853b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
886fa9e4066Sahrens 
8873b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
888c166b69dSPaul Dagnelie 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
8894445fffbSMatthew Ahrens 	bp = dsl_dataset_get_blkptr(ds);
8903b2aab18SMatthew Ahrens 	os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
8913b2aab18SMatthew Ahrens 	    ds, bp, doca->doca_type, tx);
892c166b69dSPaul Dagnelie 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
893fa9e4066Sahrens 
8943b2aab18SMatthew Ahrens 	if (doca->doca_userfunc != NULL) {
8953b2aab18SMatthew Ahrens 		doca->doca_userfunc(os, doca->doca_userarg,
8963b2aab18SMatthew Ahrens 		    doca->doca_cred, tx);
897fa9e4066Sahrens 	}
898ecd6cf80Smarks 
8993b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "create", tx, "");
9004445fffbSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
9013b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
902fa9e4066Sahrens }
903fa9e4066Sahrens 
904fa9e4066Sahrens int
905ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
906ecd6cf80Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
907fa9e4066Sahrens {
9083b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t doca;
909fa9e4066Sahrens 
9103b2aab18SMatthew Ahrens 	doca.doca_name = name;
9113b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
9123b2aab18SMatthew Ahrens 	doca.doca_flags = flags;
9133b2aab18SMatthew Ahrens 	doca.doca_userfunc = func;
9143b2aab18SMatthew Ahrens 	doca.doca_userarg = arg;
9153b2aab18SMatthew Ahrens 	doca.doca_type = type;
916ecd6cf80Smarks 
9173b2aab18SMatthew Ahrens 	return (dsl_sync_task(name,
9187d46dc6cSMatthew Ahrens 	    dmu_objset_create_check, dmu_objset_create_sync, &doca,
9197d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
920ae46e4c7SMatthew Ahrens }
921ae46e4c7SMatthew Ahrens 
9223b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg {
9233b2aab18SMatthew Ahrens 	const char *doca_clone;
9243b2aab18SMatthew Ahrens 	const char *doca_origin;
9253b2aab18SMatthew Ahrens 	cred_t *doca_cred;
9263b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t;
9273b2aab18SMatthew Ahrens 
9283b2aab18SMatthew Ahrens /*ARGSUSED*/
9293b2aab18SMatthew Ahrens static int
9303b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
931ae46e4c7SMatthew Ahrens {
9323b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
933ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
934ae46e4c7SMatthew Ahrens 	const char *tail;
9353b2aab18SMatthew Ahrens 	int error;
9363b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
9373b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
938ae46e4c7SMatthew Ahrens 
9393b2aab18SMatthew Ahrens 	if (strchr(doca->doca_clone, '@') != NULL)
940be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9413b2aab18SMatthew Ahrens 
9429adfa60dSMatthew Ahrens 	if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
9439adfa60dSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
9449adfa60dSMatthew Ahrens 
9453b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
9463b2aab18SMatthew Ahrens 	if (error != 0)
9473b2aab18SMatthew Ahrens 		return (error);
948ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
9493b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
950be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
951fa9e4066Sahrens 	}
95203b1c297SAlexander Eremin 
953a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
954a2afb611SJerry Jelinek 	    doca->doca_cred);
955a2afb611SJerry Jelinek 	if (error != 0) {
956a2afb611SJerry Jelinek 		dsl_dir_rele(pdd, FTAG);
957a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
958a2afb611SJerry Jelinek 	}
9593b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
960fa9e4066Sahrens 
9613b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
9623b2aab18SMatthew Ahrens 	if (error != 0)
96399d5e173STim Haley 		return (error);
96499d5e173STim Haley 
9653b2aab18SMatthew Ahrens 	/* You can only clone snapshots, not the head datasets. */
966bc9014e6SJustin Gibbs 	if (!origin->ds_is_snapshot) {
9673b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
968be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
96992241e0bSTom Erickson 	}
9703b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
97199d5e173STim Haley 
9723b2aab18SMatthew Ahrens 	return (0);
973ea2f5b9eSMatthew Ahrens }
9741d452cf5Sahrens 
9753b2aab18SMatthew Ahrens static void
9763b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
9771d452cf5Sahrens {
9783b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
9793b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
9803b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
9813b2aab18SMatthew Ahrens 	const char *tail;
9823b2aab18SMatthew Ahrens 	dsl_dataset_t *origin, *ds;
9833b2aab18SMatthew Ahrens 	uint64_t obj;
9849adfa60dSMatthew Ahrens 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
9854445fffbSMatthew Ahrens 
9863b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
9873b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
9884445fffbSMatthew Ahrens 
9893b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
9903b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
991f2e10be3Srm 
9923b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
9933b2aab18SMatthew Ahrens 	dsl_dataset_name(origin, namebuf);
9943b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "clone", tx,
9953b2aab18SMatthew Ahrens 	    "origin=%s (%llu)", namebuf, origin->ds_object);
9963b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
9973b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
9983b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
9991d452cf5Sahrens }
10001d452cf5Sahrens 
10011d452cf5Sahrens int
10023b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin)
10031d452cf5Sahrens {
10043b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t doca;
10054445fffbSMatthew Ahrens 
10063b2aab18SMatthew Ahrens 	doca.doca_clone = clone;
10073b2aab18SMatthew Ahrens 	doca.doca_origin = origin;
10083b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
100999d5e173STim Haley 
10103b2aab18SMatthew Ahrens 	return (dsl_sync_task(clone,
10117d46dc6cSMatthew Ahrens 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
10127d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
10134445fffbSMatthew Ahrens }
10144445fffbSMatthew Ahrens 
10154445fffbSMatthew Ahrens int
10164445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname)
10174445fffbSMatthew Ahrens {
10184445fffbSMatthew Ahrens 	int err;
10194445fffbSMatthew Ahrens 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
10204445fffbSMatthew Ahrens 	nvlist_t *snaps = fnvlist_alloc();
10214445fffbSMatthew Ahrens 
10224445fffbSMatthew Ahrens 	fnvlist_add_boolean(snaps, longsnap);
10234445fffbSMatthew Ahrens 	strfree(longsnap);
10243b2aab18SMatthew Ahrens 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
10253b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
10264445fffbSMatthew Ahrens 	return (err);
10274445fffbSMatthew Ahrens }
10284445fffbSMatthew Ahrens 
1029fa9e4066Sahrens static void
103014843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
1031fa9e4066Sahrens {
1032c717a561Smaybee 	dnode_t *dn;
1033faafa6e3Sahrens 
1034c717a561Smaybee 	while (dn = list_head(list)) {
1035c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1036c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
1037c717a561Smaybee 		/*
103814843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
103914843421SMatthew Ahrens 		 * meta-dnode needs to set it ouside dnode_sync().
1040c717a561Smaybee 		 */
1041c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1042c717a561Smaybee 		ASSERT(dn->dn_zio);
1043faafa6e3Sahrens 
1044c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1045c717a561Smaybee 		list_remove(list, dn);
104614843421SMatthew Ahrens 
104714843421SMatthew Ahrens 		if (newlist) {
104814843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
104914843421SMatthew Ahrens 			list_insert_tail(newlist, dn);
105014843421SMatthew Ahrens 		}
105114843421SMatthew Ahrens 
1052c717a561Smaybee 		dnode_sync(dn, tx);
1053fa9e4066Sahrens 	}
1054fa9e4066Sahrens }
1055fa9e4066Sahrens 
1056fa9e4066Sahrens /* ARGSUSED */
1057fa9e4066Sahrens static void
1058b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1059fa9e4066Sahrens {
1060e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1061503ad85cSMatthew Ahrens 	objset_t *os = arg;
1062c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1063fa9e4066Sahrens 
10645d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp));
10653b2aab18SMatthew Ahrens 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
10663b2aab18SMatthew Ahrens 	ASSERT0(BP_GET_LEVEL(bp));
10670a4e9518Sgw 
1068fa9e4066Sahrens 	/*
106914843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
107014843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
107114843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
107214843421SMatthew Ahrens 	 * dnode and user/group accounting objects).
1073fa9e4066Sahrens 	 */
107414843421SMatthew Ahrens 	bp->blk_fill = 0;
1075e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
10765d7b4d43SMatthew Ahrens 		bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1077c166b69dSPaul Dagnelie 	if (os->os_dsl_dataset != NULL)
1078c166b69dSPaul Dagnelie 		rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1079c166b69dSPaul Dagnelie 	*os->os_rootbp = *bp;
1080c166b69dSPaul Dagnelie 	if (os->os_dsl_dataset != NULL)
1081c166b69dSPaul Dagnelie 		rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1082b24ab676SJeff Bonwick }
1083b24ab676SJeff Bonwick 
1084b24ab676SJeff Bonwick /* ARGSUSED */
1085b24ab676SJeff Bonwick static void
1086b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1087b24ab676SJeff Bonwick {
1088b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1089b24ab676SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
1090b24ab676SJeff Bonwick 	objset_t *os = arg;
10910a4e9518Sgw 
1092e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1093b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, bp_orig));
1094e14bb325SJeff Bonwick 	} else {
1095b24ab676SJeff Bonwick 		dsl_dataset_t *ds = os->os_dsl_dataset;
1096b24ab676SJeff Bonwick 		dmu_tx_t *tx = os->os_synctx;
1097b24ab676SJeff Bonwick 
1098b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1099b24ab676SJeff Bonwick 		dsl_dataset_block_born(ds, bp, tx);
11000a4e9518Sgw 	}
1101c166b69dSPaul Dagnelie 	kmem_free(bp, sizeof (*bp));
1102c717a561Smaybee }
1103c717a561Smaybee 
1104fa9e4066Sahrens /* called from dsl */
1105fa9e4066Sahrens void
1106503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1107fa9e4066Sahrens {
1108fa9e4066Sahrens 	int txgoff;
11097802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
1110b24ab676SJeff Bonwick 	zio_prop_t zp;
1111c717a561Smaybee 	zio_t *zio;
1112c717a561Smaybee 	list_t *list;
111314843421SMatthew Ahrens 	list_t *newlist = NULL;
1114c717a561Smaybee 	dbuf_dirty_record_t *dr;
1115c166b69dSPaul Dagnelie 	blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1116c166b69dSPaul Dagnelie 	*blkptr_copy = *os->os_rootbp;
1117c717a561Smaybee 
1118c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1119fa9e4066Sahrens 
1120fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1121fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
1122fa9e4066Sahrens 	os->os_synctx = tx;
1123fa9e4066Sahrens 
112487bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
112587bd5c1eSahrens 		/*
112687bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
112787bd5c1eSahrens 		 * spa_max_replication() could change, so reset
112887bd5c1eSahrens 		 * os_copies here.
112987bd5c1eSahrens 		 */
113087bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
113187bd5c1eSahrens 	}
113287bd5c1eSahrens 
1133fa9e4066Sahrens 	/*
1134c717a561Smaybee 	 * Create the root block IO
1135fa9e4066Sahrens 	 */
1136b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1137b24ab676SJeff Bonwick 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1138b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
11391b912ec7SGeorge Wilson 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1140b24ab676SJeff Bonwick 
1141b24ab676SJeff Bonwick 	dmu_write_policy(os, NULL, 0, 0, &zp);
1142b24ab676SJeff Bonwick 
1143b24ab676SJeff Bonwick 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1144c166b69dSPaul Dagnelie 	    blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
11458df0bcf0SPaul Dagnelie 	    &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
11468df0bcf0SPaul Dagnelie 	    os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1147c717a561Smaybee 
1148c717a561Smaybee 	/*
114914843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
1150c717a561Smaybee 	 */
1151744947dcSTom Erickson 	DMU_META_DNODE(os)->dn_zio = zio;
1152744947dcSTom Erickson 	dnode_sync(DMU_META_DNODE(os), tx);
1153c717a561Smaybee 
115414843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
115514843421SMatthew Ahrens 
1156744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) &&
1157744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1158744947dcSTom Erickson 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1159744947dcSTom Erickson 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1160744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1161744947dcSTom Erickson 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
116214843421SMatthew Ahrens 	}
116314843421SMatthew Ahrens 
1164c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
1165fa9e4066Sahrens 
116614843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
116714843421SMatthew Ahrens 		newlist = &os->os_synced_dnodes;
116814843421SMatthew Ahrens 		/*
116914843421SMatthew Ahrens 		 * We must create the list here because it uses the
117014843421SMatthew Ahrens 		 * dn_dirty_link[] of this txg.
117114843421SMatthew Ahrens 		 */
117214843421SMatthew Ahrens 		list_create(newlist, sizeof (dnode_t),
117314843421SMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
117414843421SMatthew Ahrens 	}
117514843421SMatthew Ahrens 
117614843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
117714843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1178fa9e4066Sahrens 
1179744947dcSTom Erickson 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1180c717a561Smaybee 	while (dr = list_head(list)) {
11813b2aab18SMatthew Ahrens 		ASSERT0(dr->dr_dbuf->db_level);
1182c717a561Smaybee 		list_remove(list, dr);
1183c717a561Smaybee 		if (dr->dr_zio)
1184c717a561Smaybee 			zio_nowait(dr->dr_zio);
1185c717a561Smaybee 	}
1186*af346df5SNed Bass 
1187*af346df5SNed Bass 	/* Enable dnode backfill if enough objects have been freed. */
1188*af346df5SNed Bass 	if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1189*af346df5SNed Bass 		os->os_rescan_dnodes = B_TRUE;
1190*af346df5SNed Bass 		os->os_freed_dnodes = 0;
1191*af346df5SNed Bass 	}
1192*af346df5SNed Bass 
1193c717a561Smaybee 	/*
1194c717a561Smaybee 	 * Free intent log blocks up to this tx.
1195c717a561Smaybee 	 */
1196c717a561Smaybee 	zil_sync(os->os_zil, tx);
1197088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
1198c717a561Smaybee 	zio_nowait(zio);
1199fa9e4066Sahrens }
1200fa9e4066Sahrens 
1201b24ab676SJeff Bonwick boolean_t
1202b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1203b24ab676SJeff Bonwick {
1204b24ab676SJeff Bonwick 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1205b24ab676SJeff Bonwick 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1206b24ab676SJeff Bonwick }
1207b24ab676SJeff Bonwick 
1208744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
120914843421SMatthew Ahrens 
121014843421SMatthew Ahrens void
121114843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
121214843421SMatthew Ahrens {
121314843421SMatthew Ahrens 	used_cbs[ost] = cb;
121414843421SMatthew Ahrens }
121514843421SMatthew Ahrens 
121614843421SMatthew Ahrens boolean_t
1217503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
121814843421SMatthew Ahrens {
121914843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1220744947dcSTom Erickson 	    used_cbs[os->os_phys->os_type] != NULL &&
1221744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os) != NULL);
122214843421SMatthew Ahrens }
122314843421SMatthew Ahrens 
12248b70546bSMatthew Ahrens typedef struct userquota_node {
12258b70546bSMatthew Ahrens 	uint64_t uqn_id;
12268b70546bSMatthew Ahrens 	int64_t uqn_delta;
12278b70546bSMatthew Ahrens 	avl_node_t uqn_node;
12288b70546bSMatthew Ahrens } userquota_node_t;
12298b70546bSMatthew Ahrens 
12308b70546bSMatthew Ahrens typedef struct userquota_cache {
12318b70546bSMatthew Ahrens 	avl_tree_t uqc_user_deltas;
12328b70546bSMatthew Ahrens 	avl_tree_t uqc_group_deltas;
12338b70546bSMatthew Ahrens } userquota_cache_t;
12348b70546bSMatthew Ahrens 
12358b70546bSMatthew Ahrens static int
12368b70546bSMatthew Ahrens userquota_compare(const void *l, const void *r)
12378b70546bSMatthew Ahrens {
12388b70546bSMatthew Ahrens 	const userquota_node_t *luqn = l;
12398b70546bSMatthew Ahrens 	const userquota_node_t *ruqn = r;
12408b70546bSMatthew Ahrens 
12418b70546bSMatthew Ahrens 	if (luqn->uqn_id < ruqn->uqn_id)
12428b70546bSMatthew Ahrens 		return (-1);
12438b70546bSMatthew Ahrens 	if (luqn->uqn_id > ruqn->uqn_id)
12448b70546bSMatthew Ahrens 		return (1);
12458b70546bSMatthew Ahrens 	return (0);
12468b70546bSMatthew Ahrens }
12478b70546bSMatthew Ahrens 
12489966ca11SMatthew Ahrens static void
12498b70546bSMatthew Ahrens do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
12508b70546bSMatthew Ahrens {
12518b70546bSMatthew Ahrens 	void *cookie;
12528b70546bSMatthew Ahrens 	userquota_node_t *uqn;
12538b70546bSMatthew Ahrens 
12548b70546bSMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
12558b70546bSMatthew Ahrens 
12568b70546bSMatthew Ahrens 	cookie = NULL;
12578b70546bSMatthew Ahrens 	while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
12588b70546bSMatthew Ahrens 	    &cookie)) != NULL) {
12598b70546bSMatthew Ahrens 		VERIFY0(zap_increment_int(os, DMU_USERUSED_OBJECT,
12608b70546bSMatthew Ahrens 		    uqn->uqn_id, uqn->uqn_delta, tx));
12618b70546bSMatthew Ahrens 		kmem_free(uqn, sizeof (*uqn));
12628b70546bSMatthew Ahrens 	}
12638b70546bSMatthew Ahrens 	avl_destroy(&cache->uqc_user_deltas);
12648b70546bSMatthew Ahrens 
12658b70546bSMatthew Ahrens 	cookie = NULL;
12668b70546bSMatthew Ahrens 	while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
12678b70546bSMatthew Ahrens 	    &cookie)) != NULL) {
12688b70546bSMatthew Ahrens 		VERIFY0(zap_increment_int(os, DMU_GROUPUSED_OBJECT,
12698b70546bSMatthew Ahrens 		    uqn->uqn_id, uqn->uqn_delta, tx));
12708b70546bSMatthew Ahrens 		kmem_free(uqn, sizeof (*uqn));
12718b70546bSMatthew Ahrens 	}
12728b70546bSMatthew Ahrens 	avl_destroy(&cache->uqc_group_deltas);
12738b70546bSMatthew Ahrens }
12748b70546bSMatthew Ahrens 
12758b70546bSMatthew Ahrens static void
12768b70546bSMatthew Ahrens userquota_update_cache(avl_tree_t *avl, uint64_t id, int64_t delta)
12778b70546bSMatthew Ahrens {
12788b70546bSMatthew Ahrens 	userquota_node_t search = { .uqn_id = id };
12798b70546bSMatthew Ahrens 	avl_index_t idx;
12808b70546bSMatthew Ahrens 
12818b70546bSMatthew Ahrens 	userquota_node_t *uqn = avl_find(avl, &search, &idx);
12828b70546bSMatthew Ahrens 	if (uqn == NULL) {
12838b70546bSMatthew Ahrens 		uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
12848b70546bSMatthew Ahrens 		uqn->uqn_id = id;
12858b70546bSMatthew Ahrens 		avl_insert(avl, uqn, idx);
12868b70546bSMatthew Ahrens 	}
12878b70546bSMatthew Ahrens 	uqn->uqn_delta += delta;
12888b70546bSMatthew Ahrens }
12898b70546bSMatthew Ahrens 
12908b70546bSMatthew Ahrens static void
12918b70546bSMatthew Ahrens do_userquota_update(userquota_cache_t *cache, uint64_t used, uint64_t flags,
12928b70546bSMatthew Ahrens     uint64_t user, uint64_t group, boolean_t subtract)
12939966ca11SMatthew Ahrens {
12940a586ceaSMark Shellenbaum 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
12950a586ceaSMark Shellenbaum 		int64_t delta = DNODE_SIZE + used;
12969966ca11SMatthew Ahrens 		if (subtract)
12979966ca11SMatthew Ahrens 			delta = -delta;
12988b70546bSMatthew Ahrens 
12998b70546bSMatthew Ahrens 		userquota_update_cache(&cache->uqc_user_deltas, user, delta);
13008b70546bSMatthew Ahrens 		userquota_update_cache(&cache->uqc_group_deltas, group, delta);
13019966ca11SMatthew Ahrens 	}
13029966ca11SMatthew Ahrens }
13039966ca11SMatthew Ahrens 
130414843421SMatthew Ahrens void
13050a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
130614843421SMatthew Ahrens {
130714843421SMatthew Ahrens 	dnode_t *dn;
130814843421SMatthew Ahrens 	list_t *list = &os->os_synced_dnodes;
13098b70546bSMatthew Ahrens 	userquota_cache_t cache = { 0 };
131014843421SMatthew Ahrens 
131114843421SMatthew Ahrens 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
131214843421SMatthew Ahrens 
13138b70546bSMatthew Ahrens 	avl_create(&cache.uqc_user_deltas, userquota_compare,
13148b70546bSMatthew Ahrens 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
13158b70546bSMatthew Ahrens 	avl_create(&cache.uqc_group_deltas, userquota_compare,
13168b70546bSMatthew Ahrens 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
13178b70546bSMatthew Ahrens 
131814843421SMatthew Ahrens 	while (dn = list_head(list)) {
13191d8ccc7bSMark Shellenbaum 		int flags;
132014843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
132114843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
132214843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
132314843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
132414843421SMatthew Ahrens 
132514843421SMatthew Ahrens 		/* Allocate the user/groupused objects if necessary. */
1326744947dcSTom Erickson 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
13278b70546bSMatthew Ahrens 			VERIFY0(zap_create_claim(os,
132814843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT,
132914843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
13308b70546bSMatthew Ahrens 			VERIFY0(zap_create_claim(os,
133114843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT,
133214843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
133314843421SMatthew Ahrens 		}
133414843421SMatthew Ahrens 
13351d8ccc7bSMark Shellenbaum 		flags = dn->dn_id_flags;
13361d8ccc7bSMark Shellenbaum 		ASSERT(flags);
13371d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST)  {
13388b70546bSMatthew Ahrens 			do_userquota_update(&cache,
13398b70546bSMatthew Ahrens 			    dn->dn_oldused, dn->dn_oldflags,
13408b70546bSMatthew Ahrens 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE);
13410a586ceaSMark Shellenbaum 		}
13421d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_NEW_EXIST) {
13438b70546bSMatthew Ahrens 			do_userquota_update(&cache,
13448b70546bSMatthew Ahrens 			    DN_USED_BYTES(dn->dn_phys),
13450a586ceaSMark Shellenbaum 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
13468b70546bSMatthew Ahrens 			    dn->dn_newgid, B_FALSE);
13470a586ceaSMark Shellenbaum 		}
13480a586ceaSMark Shellenbaum 
13491d8ccc7bSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
13500a586ceaSMark Shellenbaum 		dn->dn_oldused = 0;
13510a586ceaSMark Shellenbaum 		dn->dn_oldflags = 0;
13520a586ceaSMark Shellenbaum 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
13530a586ceaSMark Shellenbaum 			dn->dn_olduid = dn->dn_newuid;
13540a586ceaSMark Shellenbaum 			dn->dn_oldgid = dn->dn_newgid;
13550a586ceaSMark Shellenbaum 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
13560a586ceaSMark Shellenbaum 			if (dn->dn_bonuslen == 0)
13570a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
13580a586ceaSMark Shellenbaum 			else
13590a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13600a586ceaSMark Shellenbaum 		}
136128d97a71SMark Shellenbaum 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
136214843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
136314843421SMatthew Ahrens 
136414843421SMatthew Ahrens 		list_remove(list, dn);
136514843421SMatthew Ahrens 		dnode_rele(dn, list);
136614843421SMatthew Ahrens 	}
13678b70546bSMatthew Ahrens 	do_userquota_cacheflush(os, &cache, tx);
136814843421SMatthew Ahrens }
136914843421SMatthew Ahrens 
137006e0070dSMark Shellenbaum /*
137106e0070dSMark Shellenbaum  * Returns a pointer to data to find uid/gid from
137206e0070dSMark Shellenbaum  *
137306e0070dSMark Shellenbaum  * If a dirty record for transaction group that is syncing can't
137406e0070dSMark Shellenbaum  * be found then NULL is returned.  In the NULL case it is assumed
137506e0070dSMark Shellenbaum  * the uid/gid aren't changing.
137606e0070dSMark Shellenbaum  */
137706e0070dSMark Shellenbaum static void *
137806e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
137906e0070dSMark Shellenbaum {
138006e0070dSMark Shellenbaum 	dbuf_dirty_record_t *dr, **drp;
138106e0070dSMark Shellenbaum 	void *data;
138206e0070dSMark Shellenbaum 
138306e0070dSMark Shellenbaum 	if (db->db_dirtycnt == 0)
138406e0070dSMark Shellenbaum 		return (db->db.db_data);  /* Nothing is changing */
138506e0070dSMark Shellenbaum 
138606e0070dSMark Shellenbaum 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
138706e0070dSMark Shellenbaum 		if (dr->dr_txg == tx->tx_txg)
138806e0070dSMark Shellenbaum 			break;
138906e0070dSMark Shellenbaum 
1390744947dcSTom Erickson 	if (dr == NULL) {
139106e0070dSMark Shellenbaum 		data = NULL;
1392744947dcSTom Erickson 	} else {
1393744947dcSTom Erickson 		dnode_t *dn;
1394744947dcSTom Erickson 
1395744947dcSTom Erickson 		DB_DNODE_ENTER(dr->dr_dbuf);
1396744947dcSTom Erickson 		dn = DB_DNODE(dr->dr_dbuf);
1397744947dcSTom Erickson 
1398744947dcSTom Erickson 		if (dn->dn_bonuslen == 0 &&
1399744947dcSTom Erickson 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1400744947dcSTom Erickson 			data = dr->dt.dl.dr_data->b_data;
1401744947dcSTom Erickson 		else
1402744947dcSTom Erickson 			data = dr->dt.dl.dr_data;
1403744947dcSTom Erickson 
1404744947dcSTom Erickson 		DB_DNODE_EXIT(dr->dr_dbuf);
1405744947dcSTom Erickson 	}
1406744947dcSTom Erickson 
140706e0070dSMark Shellenbaum 	return (data);
140806e0070dSMark Shellenbaum }
140906e0070dSMark Shellenbaum 
14100a586ceaSMark Shellenbaum void
141106e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
14120a586ceaSMark Shellenbaum {
14130a586ceaSMark Shellenbaum 	objset_t *os = dn->dn_objset;
14140a586ceaSMark Shellenbaum 	void *data = NULL;
141506e0070dSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
1416d5285caeSGeorge Wilson 	uint64_t *user = NULL;
1417d5285caeSGeorge Wilson 	uint64_t *group = NULL;
14180a586ceaSMark Shellenbaum 	int flags = dn->dn_id_flags;
14190a586ceaSMark Shellenbaum 	int error;
142006e0070dSMark Shellenbaum 	boolean_t have_spill = B_FALSE;
14210a586ceaSMark Shellenbaum 
14220a586ceaSMark Shellenbaum 	if (!dmu_objset_userused_enabled(dn->dn_objset))
14230a586ceaSMark Shellenbaum 		return;
14240a586ceaSMark Shellenbaum 
14250a586ceaSMark Shellenbaum 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
14260a586ceaSMark Shellenbaum 	    DN_ID_CHKED_SPILL)))
14270a586ceaSMark Shellenbaum 		return;
14280a586ceaSMark Shellenbaum 
14290a586ceaSMark Shellenbaum 	if (before && dn->dn_bonuslen != 0)
14300a586ceaSMark Shellenbaum 		data = DN_BONUS(dn->dn_phys);
143106e0070dSMark Shellenbaum 	else if (!before && dn->dn_bonuslen != 0) {
143206e0070dSMark Shellenbaum 		if (dn->dn_bonus) {
143306e0070dSMark Shellenbaum 			db = dn->dn_bonus;
143406e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
143506e0070dSMark Shellenbaum 			data = dmu_objset_userquota_find_data(db, tx);
143606e0070dSMark Shellenbaum 		} else {
143706e0070dSMark Shellenbaum 			data = DN_BONUS(dn->dn_phys);
143806e0070dSMark Shellenbaum 		}
143906e0070dSMark Shellenbaum 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
14400a586ceaSMark Shellenbaum 			int rf = 0;
14410a586ceaSMark Shellenbaum 
14420a586ceaSMark Shellenbaum 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
14430a586ceaSMark Shellenbaum 				rf |= DB_RF_HAVESTRUCT;
14441d8ccc7bSMark Shellenbaum 			error = dmu_spill_hold_by_dnode(dn,
14451d8ccc7bSMark Shellenbaum 			    rf | DB_RF_MUST_SUCCEED,
144606e0070dSMark Shellenbaum 			    FTAG, (dmu_buf_t **)&db);
14470a586ceaSMark Shellenbaum 			ASSERT(error == 0);
144806e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
144906e0070dSMark Shellenbaum 			data = (before) ? db->db.db_data :
145006e0070dSMark Shellenbaum 			    dmu_objset_userquota_find_data(db, tx);
145106e0070dSMark Shellenbaum 			have_spill = B_TRUE;
14520a586ceaSMark Shellenbaum 	} else {
14530a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
14540a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
14550a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
14560a586ceaSMark Shellenbaum 		return;
14570a586ceaSMark Shellenbaum 	}
14580a586ceaSMark Shellenbaum 
14590a586ceaSMark Shellenbaum 	if (before) {
146006e0070dSMark Shellenbaum 		ASSERT(data);
14610a586ceaSMark Shellenbaum 		user = &dn->dn_olduid;
14620a586ceaSMark Shellenbaum 		group = &dn->dn_oldgid;
146306e0070dSMark Shellenbaum 	} else if (data) {
14640a586ceaSMark Shellenbaum 		user = &dn->dn_newuid;
14650a586ceaSMark Shellenbaum 		group = &dn->dn_newgid;
14660a586ceaSMark Shellenbaum 	}
14670a586ceaSMark Shellenbaum 
146806e0070dSMark Shellenbaum 	/*
146906e0070dSMark Shellenbaum 	 * Must always call the callback in case the object
147006e0070dSMark Shellenbaum 	 * type has changed and that type isn't an object type to track
147106e0070dSMark Shellenbaum 	 */
14720a586ceaSMark Shellenbaum 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
14730a586ceaSMark Shellenbaum 	    user, group);
14740a586ceaSMark Shellenbaum 
147506e0070dSMark Shellenbaum 	/*
147606e0070dSMark Shellenbaum 	 * Preserve existing uid/gid when the callback can't determine
147706e0070dSMark Shellenbaum 	 * what the new uid/gid are and the callback returned EEXIST.
147806e0070dSMark Shellenbaum 	 * The EEXIST error tells us to just use the existing uid/gid.
147906e0070dSMark Shellenbaum 	 * If we don't know what the old values are then just assign
148006e0070dSMark Shellenbaum 	 * them to 0, since that is a new file  being created.
148106e0070dSMark Shellenbaum 	 */
148206e0070dSMark Shellenbaum 	if (!before && data == NULL && error == EEXIST) {
148306e0070dSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST) {
148406e0070dSMark Shellenbaum 			dn->dn_newuid = dn->dn_olduid;
148506e0070dSMark Shellenbaum 			dn->dn_newgid = dn->dn_oldgid;
148606e0070dSMark Shellenbaum 		} else {
148706e0070dSMark Shellenbaum 			dn->dn_newuid = 0;
148806e0070dSMark Shellenbaum 			dn->dn_newgid = 0;
148906e0070dSMark Shellenbaum 		}
149006e0070dSMark Shellenbaum 		error = 0;
149106e0070dSMark Shellenbaum 	}
149206e0070dSMark Shellenbaum 
149306e0070dSMark Shellenbaum 	if (db)
149406e0070dSMark Shellenbaum 		mutex_exit(&db->db_mtx);
149506e0070dSMark Shellenbaum 
14960a586ceaSMark Shellenbaum 	mutex_enter(&dn->dn_mtx);
14970a586ceaSMark Shellenbaum 	if (error == 0 && before)
14980a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
14990a586ceaSMark Shellenbaum 	if (error == 0 && !before)
15000a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
15010a586ceaSMark Shellenbaum 
150206e0070dSMark Shellenbaum 	if (have_spill) {
15030a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
15040a586ceaSMark Shellenbaum 	} else {
15050a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
15060a586ceaSMark Shellenbaum 	}
15070a586ceaSMark Shellenbaum 	mutex_exit(&dn->dn_mtx);
150806e0070dSMark Shellenbaum 	if (have_spill)
150906e0070dSMark Shellenbaum 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
15100a586ceaSMark Shellenbaum }
15110a586ceaSMark Shellenbaum 
151214843421SMatthew Ahrens boolean_t
151314843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
151414843421SMatthew Ahrens {
1515503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
151614843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
151714843421SMatthew Ahrens }
151814843421SMatthew Ahrens 
151914843421SMatthew Ahrens int
152014843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
152114843421SMatthew Ahrens {
152214843421SMatthew Ahrens 	uint64_t obj;
152314843421SMatthew Ahrens 	int err = 0;
152414843421SMatthew Ahrens 
152514843421SMatthew Ahrens 	if (dmu_objset_userspace_present(os))
152614843421SMatthew Ahrens 		return (0);
1527503ad85cSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
1528be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
152914843421SMatthew Ahrens 	if (dmu_objset_is_snapshot(os))
1530be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
153114843421SMatthew Ahrens 
153214843421SMatthew Ahrens 	/*
153314843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
153414843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
153514843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
153614843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
153714843421SMatthew Ahrens 	 * independently.
153814843421SMatthew Ahrens 	 */
153914843421SMatthew Ahrens 
154014843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
154168857716SLin Ling 		dmu_tx_t *tx;
154214843421SMatthew Ahrens 		dmu_buf_t *db;
154314843421SMatthew Ahrens 		int objerr;
154414843421SMatthew Ahrens 
154514843421SMatthew Ahrens 		if (issig(JUSTLOOKING) && issig(FORREAL))
1546be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
154714843421SMatthew Ahrens 
154814843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
15493b2aab18SMatthew Ahrens 		if (objerr != 0)
155014843421SMatthew Ahrens 			continue;
155168857716SLin Ling 		tx = dmu_tx_create(os);
155214843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
155314843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
15543b2aab18SMatthew Ahrens 		if (objerr != 0) {
155514843421SMatthew Ahrens 			dmu_tx_abort(tx);
155614843421SMatthew Ahrens 			continue;
155714843421SMatthew Ahrens 		}
155814843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
155914843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
156014843421SMatthew Ahrens 		dmu_tx_commit(tx);
156114843421SMatthew Ahrens 	}
156214843421SMatthew Ahrens 
1563503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
156414843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
156514843421SMatthew Ahrens 	return (0);
156614843421SMatthew Ahrens }
156714843421SMatthew Ahrens 
1568fa9e4066Sahrens void
1569a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1570a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1571fa9e4066Sahrens {
1572503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1573a2eea2e1Sahrens 	    usedobjsp, availobjsp);
1574a2eea2e1Sahrens }
1575a2eea2e1Sahrens 
1576a2eea2e1Sahrens uint64_t
1577a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1578a2eea2e1Sahrens {
1579503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1580a2eea2e1Sahrens }
1581a2eea2e1Sahrens 
1582a2eea2e1Sahrens void
1583a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1584a2eea2e1Sahrens {
1585503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
1586503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
1587503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1588a2eea2e1Sahrens }
1589a2eea2e1Sahrens 
1590a2eea2e1Sahrens void
1591a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1592a2eea2e1Sahrens {
1593503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
1594503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
1595a2eea2e1Sahrens 
1596503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1597503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1598a2eea2e1Sahrens 
1599a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1600503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
160114843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
160214843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
1603fa9e4066Sahrens }
1604fa9e4066Sahrens 
1605fa9e4066Sahrens int
1606fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1607fa9e4066Sahrens {
1608503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1609bc9014e6SJustin Gibbs 		return (os->os_dsl_dataset->ds_is_snapshot);
1610fa9e4066Sahrens 	else
1611fa9e4066Sahrens 		return (B_FALSE);
1612fa9e4066Sahrens }
1613fa9e4066Sahrens 
1614ab04eb8eStimh int
1615ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1616ab04eb8eStimh     boolean_t *conflict)
1617ab04eb8eStimh {
1618503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1619ab04eb8eStimh 	uint64_t ignored;
1620ab04eb8eStimh 
1621c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1622be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1623ab04eb8eStimh 
1624ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1625c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1626c1379625SJustin T. Gibbs 	    MT_FIRST, real, maxlen, conflict));
1627ab04eb8eStimh }
1628ab04eb8eStimh 
1629fa9e4066Sahrens int
1630fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1631b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1632fa9e4066Sahrens {
1633503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1634fa9e4066Sahrens 	zap_cursor_t cursor;
1635fa9e4066Sahrens 	zap_attribute_t attr;
1636fa9e4066Sahrens 
16373b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
16383b2aab18SMatthew Ahrens 
1639c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1640be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1641fa9e4066Sahrens 
1642fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
1643fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1644c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1645fa9e4066Sahrens 
164687e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
164787e5029aSahrens 		zap_cursor_fini(&cursor);
1648be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
164987e5029aSahrens 	}
165087e5029aSahrens 
165187e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
165287e5029aSahrens 		zap_cursor_fini(&cursor);
1653be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
165487e5029aSahrens 	}
165587e5029aSahrens 
165687e5029aSahrens 	(void) strcpy(name, attr.za_name);
165787e5029aSahrens 	if (idp)
165887e5029aSahrens 		*idp = attr.za_first_integer;
1659b38f0970Sck 	if (case_conflict)
1660b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
166187e5029aSahrens 	zap_cursor_advance(&cursor);
166287e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
166387e5029aSahrens 	zap_cursor_fini(&cursor);
166487e5029aSahrens 
166587e5029aSahrens 	return (0);
166687e5029aSahrens }
166787e5029aSahrens 
166887e5029aSahrens int
166987e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
167087e5029aSahrens     uint64_t *idp, uint64_t *offp)
167187e5029aSahrens {
1672503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
167387e5029aSahrens 	zap_cursor_t cursor;
167487e5029aSahrens 	zap_attribute_t attr;
167587e5029aSahrens 
167687e5029aSahrens 	/* there is no next dir on a snapshot! */
1677503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
1678c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj)
1679be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1680fa9e4066Sahrens 
168187e5029aSahrens 	zap_cursor_init_serialized(&cursor,
168287e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
1683c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
168487e5029aSahrens 
168587e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
168687e5029aSahrens 		zap_cursor_fini(&cursor);
1687be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
168887e5029aSahrens 	}
168987e5029aSahrens 
169087e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
169187e5029aSahrens 		zap_cursor_fini(&cursor);
1692be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
169387e5029aSahrens 	}
1694fa9e4066Sahrens 
1695fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
169687e5029aSahrens 	if (idp)
169787e5029aSahrens 		*idp = attr.za_first_integer;
1698fa9e4066Sahrens 	zap_cursor_advance(&cursor);
1699fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
170087e5029aSahrens 	zap_cursor_fini(&cursor);
1701fa9e4066Sahrens 
1702fa9e4066Sahrens 	return (0);
1703fa9e4066Sahrens }
1704fa9e4066Sahrens 
170512380e1eSArne Jansen typedef struct dmu_objset_find_ctx {
170612380e1eSArne Jansen 	taskq_t		*dc_tq;
170712380e1eSArne Jansen 	dsl_pool_t	*dc_dp;
170812380e1eSArne Jansen 	uint64_t	dc_ddobj;
170912380e1eSArne Jansen 	int		(*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
171012380e1eSArne Jansen 	void		*dc_arg;
171112380e1eSArne Jansen 	int		dc_flags;
171212380e1eSArne Jansen 	kmutex_t	*dc_error_lock;
171312380e1eSArne Jansen 	int		*dc_error;
171412380e1eSArne Jansen } dmu_objset_find_ctx_t;
171512380e1eSArne Jansen 
171612380e1eSArne Jansen static void
171712380e1eSArne Jansen dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
1718088f3894Sahrens {
171912380e1eSArne Jansen 	dsl_pool_t *dp = dcp->dc_dp;
172012380e1eSArne Jansen 	dmu_objset_find_ctx_t *child_dcp;
17213b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
17223b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
17233b2aab18SMatthew Ahrens 	zap_cursor_t zc;
17243b2aab18SMatthew Ahrens 	zap_attribute_t *attr;
17253b2aab18SMatthew Ahrens 	uint64_t thisobj;
172612380e1eSArne Jansen 	int err = 0;
17273b2aab18SMatthew Ahrens 
172812380e1eSArne Jansen 	/* don't process if there already was an error */
172912380e1eSArne Jansen 	if (*dcp->dc_error != 0)
173012380e1eSArne Jansen 		goto out;
17313b2aab18SMatthew Ahrens 
173212380e1eSArne Jansen 	err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd);
17333b2aab18SMatthew Ahrens 	if (err != 0)
173412380e1eSArne Jansen 		goto out;
17353b2aab18SMatthew Ahrens 
17363b2aab18SMatthew Ahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
17373b2aab18SMatthew Ahrens 	if (dd->dd_myname[0] == '$') {
17383b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
173912380e1eSArne Jansen 		goto out;
17403b2aab18SMatthew Ahrens 	}
17413b2aab18SMatthew Ahrens 
1742c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
17433b2aab18SMatthew Ahrens 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
17443b2aab18SMatthew Ahrens 
17453b2aab18SMatthew Ahrens 	/*
17463b2aab18SMatthew Ahrens 	 * Iterate over all children.
17473b2aab18SMatthew Ahrens 	 */
174812380e1eSArne Jansen 	if (dcp->dc_flags & DS_FIND_CHILDREN) {
17493b2aab18SMatthew Ahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1750c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
17513b2aab18SMatthew Ahrens 		    zap_cursor_retrieve(&zc, attr) == 0;
17523b2aab18SMatthew Ahrens 		    (void) zap_cursor_advance(&zc)) {
17533b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
17543b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
17553b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
17563b2aab18SMatthew Ahrens 
175712380e1eSArne Jansen 			child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
175812380e1eSArne Jansen 			*child_dcp = *dcp;
175912380e1eSArne Jansen 			child_dcp->dc_ddobj = attr->za_first_integer;
176012380e1eSArne Jansen 			if (dcp->dc_tq != NULL)
176112380e1eSArne Jansen 				(void) taskq_dispatch(dcp->dc_tq,
176212380e1eSArne Jansen 				    dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
176312380e1eSArne Jansen 			else
176412380e1eSArne Jansen 				dmu_objset_find_dp_impl(child_dcp);
17653b2aab18SMatthew Ahrens 		}
17663b2aab18SMatthew Ahrens 		zap_cursor_fini(&zc);
17673b2aab18SMatthew Ahrens 	}
17683b2aab18SMatthew Ahrens 
17693b2aab18SMatthew Ahrens 	/*
17703b2aab18SMatthew Ahrens 	 * Iterate over all snapshots.
17713b2aab18SMatthew Ahrens 	 */
177212380e1eSArne Jansen 	if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
17733b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
17743b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
17753b2aab18SMatthew Ahrens 
17763b2aab18SMatthew Ahrens 		if (err == 0) {
1777c1379625SJustin T. Gibbs 			uint64_t snapobj;
1778c1379625SJustin T. Gibbs 
1779c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
17803b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
17813b2aab18SMatthew Ahrens 
17823b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
17833b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
17843b2aab18SMatthew Ahrens 			    (void) zap_cursor_advance(&zc)) {
17853b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
17863b2aab18SMatthew Ahrens 				    sizeof (uint64_t));
17873b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
17883b2aab18SMatthew Ahrens 
17893b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
17903b2aab18SMatthew Ahrens 				    attr->za_first_integer, FTAG, &ds);
17913b2aab18SMatthew Ahrens 				if (err != 0)
17923b2aab18SMatthew Ahrens 					break;
179312380e1eSArne Jansen 				err = dcp->dc_func(dp, ds, dcp->dc_arg);
17943b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
17953b2aab18SMatthew Ahrens 				if (err != 0)
17963b2aab18SMatthew Ahrens 					break;
17973b2aab18SMatthew Ahrens 			}
17983b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
17993b2aab18SMatthew Ahrens 		}
18003b2aab18SMatthew Ahrens 	}
18013b2aab18SMatthew Ahrens 
18023b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
18033b2aab18SMatthew Ahrens 	kmem_free(attr, sizeof (zap_attribute_t));
18043b2aab18SMatthew Ahrens 
18053b2aab18SMatthew Ahrens 	if (err != 0)
180612380e1eSArne Jansen 		goto out;
18073b2aab18SMatthew Ahrens 
18083b2aab18SMatthew Ahrens 	/*
18093b2aab18SMatthew Ahrens 	 * Apply to self.
18103b2aab18SMatthew Ahrens 	 */
18113b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
18123b2aab18SMatthew Ahrens 	if (err != 0)
181312380e1eSArne Jansen 		goto out;
181412380e1eSArne Jansen 	err = dcp->dc_func(dp, ds, dcp->dc_arg);
18153b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
181612380e1eSArne Jansen 
181712380e1eSArne Jansen out:
181812380e1eSArne Jansen 	if (err != 0) {
181912380e1eSArne Jansen 		mutex_enter(dcp->dc_error_lock);
182012380e1eSArne Jansen 		/* only keep first error */
182112380e1eSArne Jansen 		if (*dcp->dc_error == 0)
182212380e1eSArne Jansen 			*dcp->dc_error = err;
182312380e1eSArne Jansen 		mutex_exit(dcp->dc_error_lock);
182412380e1eSArne Jansen 	}
182512380e1eSArne Jansen 
182612380e1eSArne Jansen 	kmem_free(dcp, sizeof (*dcp));
182712380e1eSArne Jansen }
182812380e1eSArne Jansen 
182912380e1eSArne Jansen static void
183012380e1eSArne Jansen dmu_objset_find_dp_cb(void *arg)
183112380e1eSArne Jansen {
183212380e1eSArne Jansen 	dmu_objset_find_ctx_t *dcp = arg;
183312380e1eSArne Jansen 	dsl_pool_t *dp = dcp->dc_dp;
183412380e1eSArne Jansen 
18351d3f896fSArne Jansen 	/*
18361d3f896fSArne Jansen 	 * We need to get a pool_config_lock here, as there are several
18371d3f896fSArne Jansen 	 * asssert(pool_config_held) down the stack. Getting a lock via
18381d3f896fSArne Jansen 	 * dsl_pool_config_enter is risky, as it might be stalled by a
18391d3f896fSArne Jansen 	 * pending writer. This would deadlock, as the write lock can
18401d3f896fSArne Jansen 	 * only be granted when our parent thread gives up the lock.
18411d3f896fSArne Jansen 	 * The _prio interface gives us priority over a pending writer.
18421d3f896fSArne Jansen 	 */
18431d3f896fSArne Jansen 	dsl_pool_config_enter_prio(dp, FTAG);
184412380e1eSArne Jansen 
184512380e1eSArne Jansen 	dmu_objset_find_dp_impl(dcp);
184612380e1eSArne Jansen 
184712380e1eSArne Jansen 	dsl_pool_config_exit(dp, FTAG);
184812380e1eSArne Jansen }
184912380e1eSArne Jansen 
185012380e1eSArne Jansen /*
185112380e1eSArne Jansen  * Find objsets under and including ddobj, call func(ds) on each.
185212380e1eSArne Jansen  * The order for the enumeration is completely undefined.
185312380e1eSArne Jansen  * func is called with dsl_pool_config held.
185412380e1eSArne Jansen  */
185512380e1eSArne Jansen int
185612380e1eSArne Jansen dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
185712380e1eSArne Jansen     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
185812380e1eSArne Jansen {
185912380e1eSArne Jansen 	int error = 0;
186012380e1eSArne Jansen 	taskq_t *tq = NULL;
186112380e1eSArne Jansen 	int ntasks;
186212380e1eSArne Jansen 	dmu_objset_find_ctx_t *dcp;
186312380e1eSArne Jansen 	kmutex_t err_lock;
186412380e1eSArne Jansen 
186512380e1eSArne Jansen 	mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
186612380e1eSArne Jansen 	dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
186712380e1eSArne Jansen 	dcp->dc_tq = NULL;
186812380e1eSArne Jansen 	dcp->dc_dp = dp;
186912380e1eSArne Jansen 	dcp->dc_ddobj = ddobj;
187012380e1eSArne Jansen 	dcp->dc_func = func;
187112380e1eSArne Jansen 	dcp->dc_arg = arg;
187212380e1eSArne Jansen 	dcp->dc_flags = flags;
187312380e1eSArne Jansen 	dcp->dc_error_lock = &err_lock;
187412380e1eSArne Jansen 	dcp->dc_error = &error;
187512380e1eSArne Jansen 
187612380e1eSArne Jansen 	if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
187712380e1eSArne Jansen 		/*
187812380e1eSArne Jansen 		 * In case a write lock is held we can't make use of
187912380e1eSArne Jansen 		 * parallelism, as down the stack of the worker threads
188012380e1eSArne Jansen 		 * the lock is asserted via dsl_pool_config_held.
188112380e1eSArne Jansen 		 * In case of a read lock this is solved by getting a read
188212380e1eSArne Jansen 		 * lock in each worker thread, which isn't possible in case
188312380e1eSArne Jansen 		 * of a writer lock. So we fall back to the synchronous path
188412380e1eSArne Jansen 		 * here.
188512380e1eSArne Jansen 		 * In the future it might be possible to get some magic into
188612380e1eSArne Jansen 		 * dsl_pool_config_held in a way that it returns true for
188712380e1eSArne Jansen 		 * the worker threads so that a single lock held from this
188812380e1eSArne Jansen 		 * thread suffices. For now, stay single threaded.
188912380e1eSArne Jansen 		 */
189012380e1eSArne Jansen 		dmu_objset_find_dp_impl(dcp);
18912bad2258SSteven Hartland 		mutex_destroy(&err_lock);
189212380e1eSArne Jansen 
189312380e1eSArne Jansen 		return (error);
189412380e1eSArne Jansen 	}
189512380e1eSArne Jansen 
189612380e1eSArne Jansen 	ntasks = dmu_find_threads;
189712380e1eSArne Jansen 	if (ntasks == 0)
189812380e1eSArne Jansen 		ntasks = vdev_count_leaves(dp->dp_spa) * 4;
189912380e1eSArne Jansen 	tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
190012380e1eSArne Jansen 	    INT_MAX, 0);
190112380e1eSArne Jansen 	if (tq == NULL) {
190212380e1eSArne Jansen 		kmem_free(dcp, sizeof (*dcp));
19032bad2258SSteven Hartland 		mutex_destroy(&err_lock);
19042bad2258SSteven Hartland 
190512380e1eSArne Jansen 		return (SET_ERROR(ENOMEM));
190612380e1eSArne Jansen 	}
190712380e1eSArne Jansen 	dcp->dc_tq = tq;
190812380e1eSArne Jansen 
190912380e1eSArne Jansen 	/* dcp will be freed by task */
191012380e1eSArne Jansen 	(void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
191112380e1eSArne Jansen 
191212380e1eSArne Jansen 	/*
191312380e1eSArne Jansen 	 * PORTING: this code relies on the property of taskq_wait to wait
191412380e1eSArne Jansen 	 * until no more tasks are queued and no more tasks are active. As
191512380e1eSArne Jansen 	 * we always queue new tasks from within other tasks, task_wait
191612380e1eSArne Jansen 	 * reliably waits for the full recursion to finish, even though we
191712380e1eSArne Jansen 	 * enqueue new tasks after taskq_wait has been called.
191812380e1eSArne Jansen 	 * On platforms other than illumos, taskq_wait may not have this
191912380e1eSArne Jansen 	 * property.
192012380e1eSArne Jansen 	 */
192112380e1eSArne Jansen 	taskq_wait(tq);
192212380e1eSArne Jansen 	taskq_destroy(tq);
192312380e1eSArne Jansen 	mutex_destroy(&err_lock);
192412380e1eSArne Jansen 
192512380e1eSArne Jansen 	return (error);
1926088f3894Sahrens }
1927088f3894Sahrens 
1928088f3894Sahrens /*
19293b2aab18SMatthew Ahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
19303b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when this is called, and it
19313b2aab18SMatthew Ahrens  * will not be held when the callback is called.
19323b2aab18SMatthew Ahrens  * Therefore this function should only be used when the pool is not changing
19333b2aab18SMatthew Ahrens  * (e.g. in syncing context), or the callback can deal with the possible races.
1934088f3894Sahrens  */
19353b2aab18SMatthew Ahrens static int
19363b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name,
19373b2aab18SMatthew Ahrens     int func(const char *, void *), void *arg, int flags)
1938fa9e4066Sahrens {
1939fa9e4066Sahrens 	dsl_dir_t *dd;
19403b2aab18SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
1941088f3894Sahrens 	dsl_dataset_t *ds;
1942fa9e4066Sahrens 	zap_cursor_t zc;
1943b7661cccSmmusante 	zap_attribute_t *attr;
1944fa9e4066Sahrens 	char *child;
1945088f3894Sahrens 	uint64_t thisobj;
1946088f3894Sahrens 	int err;
1947fa9e4066Sahrens 
19483b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dp, FTAG);
19493b2aab18SMatthew Ahrens 
19503b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
19513b2aab18SMatthew Ahrens 	if (err != 0) {
19523b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
19531d452cf5Sahrens 		return (err);
19543b2aab18SMatthew Ahrens 	}
1955fa9e4066Sahrens 
1956088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1957088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
19583b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
19593b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
1960088f3894Sahrens 		return (0);
1961088f3894Sahrens 	}
1962088f3894Sahrens 
1963c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1964b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1965fa9e4066Sahrens 
1966fa9e4066Sahrens 	/*
1967fa9e4066Sahrens 	 * Iterate over all children.
1968fa9e4066Sahrens 	 */
19690b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
1970088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1971c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
1972b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
19730b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
19743b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
19753b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
19763b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
1977fa9e4066Sahrens 
1978486ae710SMatthew Ahrens 			child = kmem_asprintf("%s/%s", name, attr->za_name);
19793b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
19803b2aab18SMatthew Ahrens 			err = dmu_objset_find_impl(spa, child,
19813b2aab18SMatthew Ahrens 			    func, arg, flags);
19823b2aab18SMatthew Ahrens 			dsl_pool_config_enter(dp, FTAG);
1983486ae710SMatthew Ahrens 			strfree(child);
19843b2aab18SMatthew Ahrens 			if (err != 0)
19850b69c2f0Sahrens 				break;
19860b69c2f0Sahrens 		}
19870b69c2f0Sahrens 		zap_cursor_fini(&zc);
19881d452cf5Sahrens 
19893b2aab18SMatthew Ahrens 		if (err != 0) {
19903b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
19913b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
1992b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
19930b69c2f0Sahrens 			return (err);
19940b69c2f0Sahrens 		}
1995fa9e4066Sahrens 	}
1996fa9e4066Sahrens 
1997fa9e4066Sahrens 	/*
1998fa9e4066Sahrens 	 * Iterate over all snapshots.
1999fa9e4066Sahrens 	 */
2000088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
2001088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2002088f3894Sahrens 
2003088f3894Sahrens 		if (err == 0) {
2004c1379625SJustin T. Gibbs 			uint64_t snapobj;
2005c1379625SJustin T. Gibbs 
2006c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2007088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
2008088f3894Sahrens 
2009088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2010088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
2011088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
20123b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
2013088f3894Sahrens 				    sizeof (uint64_t));
20143b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
2015088f3894Sahrens 
2016486ae710SMatthew Ahrens 				child = kmem_asprintf("%s@%s",
2017486ae710SMatthew Ahrens 				    name, attr->za_name);
20183b2aab18SMatthew Ahrens 				dsl_pool_config_exit(dp, FTAG);
20193b2aab18SMatthew Ahrens 				err = func(child, arg);
20203b2aab18SMatthew Ahrens 				dsl_pool_config_enter(dp, FTAG);
2021486ae710SMatthew Ahrens 				strfree(child);
20223b2aab18SMatthew Ahrens 				if (err != 0)
2023088f3894Sahrens 					break;
2024088f3894Sahrens 			}
2025088f3894Sahrens 			zap_cursor_fini(&zc);
2026fa9e4066Sahrens 		}
2027fa9e4066Sahrens 	}
2028fa9e4066Sahrens 
20293b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
2030b7661cccSmmusante 	kmem_free(attr, sizeof (zap_attribute_t));
20313b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dp, FTAG);
2032fa9e4066Sahrens 
20333b2aab18SMatthew Ahrens 	if (err != 0)
20341d452cf5Sahrens 		return (err);
20351d452cf5Sahrens 
20363b2aab18SMatthew Ahrens 	/* Apply to self. */
20373b2aab18SMatthew Ahrens 	return (func(name, arg));
2038fa9e4066Sahrens }
2039f18faf3fSek 
20403b2aab18SMatthew Ahrens /*
20413b2aab18SMatthew Ahrens  * See comment above dmu_objset_find_impl().
20423b2aab18SMatthew Ahrens  */
20437f73c863SRich Morris int
20443b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg,
20453b2aab18SMatthew Ahrens     int flags)
20467f73c863SRich Morris {
20473b2aab18SMatthew Ahrens 	spa_t *spa;
20483b2aab18SMatthew Ahrens 	int error;
20497f73c863SRich Morris 
20503b2aab18SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
20513b2aab18SMatthew Ahrens 	if (error != 0)
20523b2aab18SMatthew Ahrens 		return (error);
20533b2aab18SMatthew Ahrens 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
20543b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
20553b2aab18SMatthew Ahrens 	return (error);
20567f73c863SRich Morris }
20577f73c863SRich Morris 
2058f18faf3fSek void
2059f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr)
2060f18faf3fSek {
2061503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2062503ad85cSMatthew Ahrens 	os->os_user_ptr = user_ptr;
2063f18faf3fSek }
2064f18faf3fSek 
2065f18faf3fSek void *
2066f18faf3fSek dmu_objset_get_user(objset_t *os)
2067f18faf3fSek {
2068503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2069503ad85cSMatthew Ahrens 	return (os->os_user_ptr);
2070f18faf3fSek }
20713b2aab18SMatthew Ahrens 
20723b2aab18SMatthew Ahrens /*
20733b2aab18SMatthew Ahrens  * Determine name of filesystem, given name of snapshot.
20749adfa60dSMatthew Ahrens  * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
20753b2aab18SMatthew Ahrens  */
20763b2aab18SMatthew Ahrens int
20773b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf)
20783b2aab18SMatthew Ahrens {
20793b2aab18SMatthew Ahrens 	char *atp = strchr(snapname, '@');
20803b2aab18SMatthew Ahrens 	if (atp == NULL)
2081be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
20829adfa60dSMatthew Ahrens 	if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2083be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
20843b2aab18SMatthew Ahrens 	(void) strlcpy(buf, snapname, atp - snapname + 1);
20853b2aab18SMatthew Ahrens 	return (0);
20863b2aab18SMatthew Ahrens }
2087