xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision b515258426fed6c7311fd3f1dea697cfbd4085c6)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2206e0070dSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23edf345e6SMatthew Ahrens  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24aad02571SSaso Kiselkov  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25a2afb611SJerry Jelinek  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
2855da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
2955da60b9SMark J Musante 
30ecd6cf80Smarks #include <sys/cred.h>
31fa9e4066Sahrens #include <sys/zfs_context.h>
32fa9e4066Sahrens #include <sys/dmu_objset.h>
33fa9e4066Sahrens #include <sys/dsl_dir.h>
34fa9e4066Sahrens #include <sys/dsl_dataset.h>
35fa9e4066Sahrens #include <sys/dsl_prop.h>
36fa9e4066Sahrens #include <sys/dsl_pool.h>
371d452cf5Sahrens #include <sys/dsl_synctask.h>
38ecd6cf80Smarks #include <sys/dsl_deleg.h>
39fa9e4066Sahrens #include <sys/dnode.h>
40fa9e4066Sahrens #include <sys/dbuf.h>
41a2eea2e1Sahrens #include <sys/zvol.h>
42fa9e4066Sahrens #include <sys/dmu_tx.h>
43fa9e4066Sahrens #include <sys/zap.h>
44fa9e4066Sahrens #include <sys/zil.h>
45fa9e4066Sahrens #include <sys/dmu_impl.h>
46ecd6cf80Smarks #include <sys/zfs_ioctl.h>
470a586ceaSMark Shellenbaum #include <sys/sa.h>
4899d5e173STim Haley #include <sys/zfs_onexit.h>
493b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
50fa9e4066Sahrens 
51744947dcSTom Erickson /*
52744947dcSTom Erickson  * Needed to close a window in dnode_move() that allows the objset to be freed
53744947dcSTom Erickson  * before it can be safely accessed.
54744947dcSTom Erickson  */
55744947dcSTom Erickson krwlock_t os_lock;
56744947dcSTom Erickson 
57744947dcSTom Erickson void
58744947dcSTom Erickson dmu_objset_init(void)
59744947dcSTom Erickson {
60744947dcSTom Erickson 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
61744947dcSTom Erickson }
62744947dcSTom Erickson 
63744947dcSTom Erickson void
64744947dcSTom Erickson dmu_objset_fini(void)
65744947dcSTom Erickson {
66744947dcSTom Erickson 	rw_destroy(&os_lock);
67744947dcSTom Erickson }
68744947dcSTom Erickson 
69fa9e4066Sahrens spa_t *
70fa9e4066Sahrens dmu_objset_spa(objset_t *os)
71fa9e4066Sahrens {
72503ad85cSMatthew Ahrens 	return (os->os_spa);
73fa9e4066Sahrens }
74fa9e4066Sahrens 
75fa9e4066Sahrens zilog_t *
76fa9e4066Sahrens dmu_objset_zil(objset_t *os)
77fa9e4066Sahrens {
78503ad85cSMatthew Ahrens 	return (os->os_zil);
79fa9e4066Sahrens }
80fa9e4066Sahrens 
81fa9e4066Sahrens dsl_pool_t *
82fa9e4066Sahrens dmu_objset_pool(objset_t *os)
83fa9e4066Sahrens {
84fa9e4066Sahrens 	dsl_dataset_t *ds;
85fa9e4066Sahrens 
86503ad85cSMatthew Ahrens 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
87fa9e4066Sahrens 		return (ds->ds_dir->dd_pool);
88fa9e4066Sahrens 	else
89503ad85cSMatthew Ahrens 		return (spa_get_dsl(os->os_spa));
90fa9e4066Sahrens }
91fa9e4066Sahrens 
92fa9e4066Sahrens dsl_dataset_t *
93fa9e4066Sahrens dmu_objset_ds(objset_t *os)
94fa9e4066Sahrens {
95503ad85cSMatthew Ahrens 	return (os->os_dsl_dataset);
96fa9e4066Sahrens }
97fa9e4066Sahrens 
98fa9e4066Sahrens dmu_objset_type_t
99fa9e4066Sahrens dmu_objset_type(objset_t *os)
100fa9e4066Sahrens {
101503ad85cSMatthew Ahrens 	return (os->os_phys->os_type);
102fa9e4066Sahrens }
103fa9e4066Sahrens 
104fa9e4066Sahrens void
105fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
106fa9e4066Sahrens {
107503ad85cSMatthew Ahrens 	dsl_dataset_name(os->os_dsl_dataset, buf);
108fa9e4066Sahrens }
109fa9e4066Sahrens 
110fa9e4066Sahrens uint64_t
111fa9e4066Sahrens dmu_objset_id(objset_t *os)
112fa9e4066Sahrens {
113503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
114fa9e4066Sahrens 
115fa9e4066Sahrens 	return (ds ? ds->ds_object : 0);
116fa9e4066Sahrens }
117fa9e4066Sahrens 
118edf345e6SMatthew Ahrens zfs_sync_type_t
11955da60b9SMark J Musante dmu_objset_syncprop(objset_t *os)
12055da60b9SMark J Musante {
12155da60b9SMark J Musante 	return (os->os_sync);
12255da60b9SMark J Musante }
12355da60b9SMark J Musante 
124edf345e6SMatthew Ahrens zfs_logbias_op_t
125e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
126e09fa4daSNeil Perrin {
127e09fa4daSNeil Perrin 	return (os->os_logbias);
128e09fa4daSNeil Perrin }
129e09fa4daSNeil Perrin 
130fa9e4066Sahrens static void
131fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
132fa9e4066Sahrens {
133503ad85cSMatthew Ahrens 	objset_t *os = arg;
134fa9e4066Sahrens 
135fa9e4066Sahrens 	/*
136fa9e4066Sahrens 	 * Inheritance should have been done by now.
137fa9e4066Sahrens 	 */
138fa9e4066Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
139fa9e4066Sahrens 
140503ad85cSMatthew Ahrens 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
141fa9e4066Sahrens }
142fa9e4066Sahrens 
143fa9e4066Sahrens static void
144fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
145fa9e4066Sahrens {
146503ad85cSMatthew Ahrens 	objset_t *os = arg;
147fa9e4066Sahrens 
148fa9e4066Sahrens 	/*
149fa9e4066Sahrens 	 * Inheritance and range checking should have been done by now.
150fa9e4066Sahrens 	 */
151fa9e4066Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
152fa9e4066Sahrens 
153503ad85cSMatthew Ahrens 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
154fa9e4066Sahrens }
155fa9e4066Sahrens 
156d0ad202dSahrens static void
157d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
158d0ad202dSahrens {
159503ad85cSMatthew Ahrens 	objset_t *os = arg;
160d0ad202dSahrens 
161d0ad202dSahrens 	/*
162d0ad202dSahrens 	 * Inheritance and range checking should have been done by now.
163d0ad202dSahrens 	 */
164d0ad202dSahrens 	ASSERT(newval > 0);
165503ad85cSMatthew Ahrens 	ASSERT(newval <= spa_max_replication(os->os_spa));
166d0ad202dSahrens 
167503ad85cSMatthew Ahrens 	os->os_copies = newval;
168d0ad202dSahrens }
169d0ad202dSahrens 
170b24ab676SJeff Bonwick static void
171b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
172b24ab676SJeff Bonwick {
173b24ab676SJeff Bonwick 	objset_t *os = arg;
174b24ab676SJeff Bonwick 	spa_t *spa = os->os_spa;
175b24ab676SJeff Bonwick 	enum zio_checksum checksum;
176b24ab676SJeff Bonwick 
177b24ab676SJeff Bonwick 	/*
178b24ab676SJeff Bonwick 	 * Inheritance should have been done by now.
179b24ab676SJeff Bonwick 	 */
180b24ab676SJeff Bonwick 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
181b24ab676SJeff Bonwick 
182b24ab676SJeff Bonwick 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
183b24ab676SJeff Bonwick 
184b24ab676SJeff Bonwick 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
185b24ab676SJeff Bonwick 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
186b24ab676SJeff Bonwick }
187b24ab676SJeff Bonwick 
1883baa08fcSek static void
1893baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval)
1903baa08fcSek {
191503ad85cSMatthew Ahrens 	objset_t *os = arg;
1923baa08fcSek 
1933baa08fcSek 	/*
1943baa08fcSek 	 * Inheritance and range checking should have been done by now.
1953baa08fcSek 	 */
1963baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1973baa08fcSek 	    newval == ZFS_CACHE_METADATA);
1983baa08fcSek 
199503ad85cSMatthew Ahrens 	os->os_primary_cache = newval;
2003baa08fcSek }
2013baa08fcSek 
2023baa08fcSek static void
2033baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval)
2043baa08fcSek {
205503ad85cSMatthew Ahrens 	objset_t *os = arg;
2063baa08fcSek 
2073baa08fcSek 	/*
2083baa08fcSek 	 * Inheritance and range checking should have been done by now.
2093baa08fcSek 	 */
2103baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2113baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2123baa08fcSek 
213503ad85cSMatthew Ahrens 	os->os_secondary_cache = newval;
2143baa08fcSek }
2153baa08fcSek 
21655da60b9SMark J Musante static void
21755da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval)
21855da60b9SMark J Musante {
21955da60b9SMark J Musante 	objset_t *os = arg;
22055da60b9SMark J Musante 
22155da60b9SMark J Musante 	/*
22255da60b9SMark J Musante 	 * Inheritance and range checking should have been done by now.
22355da60b9SMark J Musante 	 */
22455da60b9SMark J Musante 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
22555da60b9SMark J Musante 	    newval == ZFS_SYNC_DISABLED);
22655da60b9SMark J Musante 
22755da60b9SMark J Musante 	os->os_sync = newval;
22855da60b9SMark J Musante 	if (os->os_zil)
22955da60b9SMark J Musante 		zil_set_sync(os->os_zil, newval);
23055da60b9SMark J Musante }
23155da60b9SMark J Musante 
232edf345e6SMatthew Ahrens static void
233edf345e6SMatthew Ahrens redundant_metadata_changed_cb(void *arg, uint64_t newval)
234edf345e6SMatthew Ahrens {
235edf345e6SMatthew Ahrens 	objset_t *os = arg;
236edf345e6SMatthew Ahrens 
237edf345e6SMatthew Ahrens 	/*
238edf345e6SMatthew Ahrens 	 * Inheritance and range checking should have been done by now.
239edf345e6SMatthew Ahrens 	 */
240edf345e6SMatthew Ahrens 	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
241edf345e6SMatthew Ahrens 	    newval == ZFS_REDUNDANT_METADATA_MOST);
242edf345e6SMatthew Ahrens 
243edf345e6SMatthew Ahrens 	os->os_redundant_metadata = newval;
244edf345e6SMatthew Ahrens }
245edf345e6SMatthew Ahrens 
246e09fa4daSNeil Perrin static void
247e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
248e09fa4daSNeil Perrin {
249e09fa4daSNeil Perrin 	objset_t *os = arg;
250e09fa4daSNeil Perrin 
251e09fa4daSNeil Perrin 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
252e09fa4daSNeil Perrin 	    newval == ZFS_LOGBIAS_THROUGHPUT);
253e09fa4daSNeil Perrin 	os->os_logbias = newval;
254e09fa4daSNeil Perrin 	if (os->os_zil)
255e09fa4daSNeil Perrin 		zil_set_logbias(os->os_zil, newval);
256e09fa4daSNeil Perrin }
257e09fa4daSNeil Perrin 
258*b5152584SMatthew Ahrens static void
259*b5152584SMatthew Ahrens recordsize_changed_cb(void *arg, uint64_t newval)
260*b5152584SMatthew Ahrens {
261*b5152584SMatthew Ahrens 	objset_t *os = arg;
262*b5152584SMatthew Ahrens 
263*b5152584SMatthew Ahrens 	os->os_recordsize = newval;
264*b5152584SMatthew Ahrens }
265*b5152584SMatthew Ahrens 
266fa9e4066Sahrens void
267fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
268fa9e4066Sahrens {
269fa9e4066Sahrens 	objset_phys_t *osp = buf;
270fa9e4066Sahrens 
27114843421SMatthew Ahrens 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
272fa9e4066Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
273fa9e4066Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
274fa9e4066Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
27514843421SMatthew Ahrens 	osp->os_flags = BSWAP_64(osp->os_flags);
27614843421SMatthew Ahrens 	if (size == sizeof (objset_phys_t)) {
27714843421SMatthew Ahrens 		dnode_byteswap(&osp->os_userused_dnode);
27814843421SMatthew Ahrens 		dnode_byteswap(&osp->os_groupused_dnode);
27914843421SMatthew Ahrens 	}
280fa9e4066Sahrens }
281fa9e4066Sahrens 
282ea8dc4b6Seschrock int
283ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
284503ad85cSMatthew Ahrens     objset_t **osp)
285fa9e4066Sahrens {
286503ad85cSMatthew Ahrens 	objset_t *os;
287088f3894Sahrens 	int i, err;
288fa9e4066Sahrens 
28991ebeef5Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
29091ebeef5Sahrens 
291503ad85cSMatthew Ahrens 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
292503ad85cSMatthew Ahrens 	os->os_dsl_dataset = ds;
293503ad85cSMatthew Ahrens 	os->os_spa = spa;
294503ad85cSMatthew Ahrens 	os->os_rootbp = bp;
295503ad85cSMatthew Ahrens 	if (!BP_IS_HOLE(os->os_rootbp)) {
29613506d1eSmaybee 		uint32_t aflags = ARC_WAIT;
2977802d7bfSMatthew Ahrens 		zbookmark_phys_t zb;
298b24ab676SJeff Bonwick 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
299b24ab676SJeff Bonwick 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
300b24ab676SJeff Bonwick 
301503ad85cSMatthew Ahrens 		if (DMU_OS_IS_L2CACHEABLE(os))
3023baa08fcSek 			aflags |= ARC_L2CACHE;
303aad02571SSaso Kiselkov 		if (DMU_OS_IS_L2COMPRESSIBLE(os))
304aad02571SSaso Kiselkov 			aflags |= ARC_L2COMPRESS;
305ea8dc4b6Seschrock 
306503ad85cSMatthew Ahrens 		dprintf_bp(os->os_rootbp, "reading %s", "");
3071b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, os->os_rootbp,
308503ad85cSMatthew Ahrens 		    arc_getbuf_func, &os->os_phys_buf,
30913506d1eSmaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
3103b2aab18SMatthew Ahrens 		if (err != 0) {
311503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
312b87f3af3Sperrin 			/* convert checksum errors into IO errors */
313b87f3af3Sperrin 			if (err == ECKSUM)
314be6fd75aSMatthew Ahrens 				err = SET_ERROR(EIO);
315ea8dc4b6Seschrock 			return (err);
316ea8dc4b6Seschrock 		}
31714843421SMatthew Ahrens 
31814843421SMatthew Ahrens 		/* Increase the blocksize if we are permitted. */
31914843421SMatthew Ahrens 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
320503ad85cSMatthew Ahrens 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
32114843421SMatthew Ahrens 			arc_buf_t *buf = arc_buf_alloc(spa,
322503ad85cSMatthew Ahrens 			    sizeof (objset_phys_t), &os->os_phys_buf,
32314843421SMatthew Ahrens 			    ARC_BUFC_METADATA);
32414843421SMatthew Ahrens 			bzero(buf->b_data, sizeof (objset_phys_t));
325503ad85cSMatthew Ahrens 			bcopy(os->os_phys_buf->b_data, buf->b_data,
326503ad85cSMatthew Ahrens 			    arc_buf_size(os->os_phys_buf));
327503ad85cSMatthew Ahrens 			(void) arc_buf_remove_ref(os->os_phys_buf,
328503ad85cSMatthew Ahrens 			    &os->os_phys_buf);
329503ad85cSMatthew Ahrens 			os->os_phys_buf = buf;
33014843421SMatthew Ahrens 		}
33114843421SMatthew Ahrens 
332503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
333503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
334fa9e4066Sahrens 	} else {
33514843421SMatthew Ahrens 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
33614843421SMatthew Ahrens 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
337503ad85cSMatthew Ahrens 		os->os_phys_buf = arc_buf_alloc(spa, size,
338503ad85cSMatthew Ahrens 		    &os->os_phys_buf, ARC_BUFC_METADATA);
339503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
340503ad85cSMatthew Ahrens 		bzero(os->os_phys, size);
341fa9e4066Sahrens 	}
342fa9e4066Sahrens 
343fa9e4066Sahrens 	/*
344fa9e4066Sahrens 	 * Note: the changed_cb will be called once before the register
345fa9e4066Sahrens 	 * func returns, thus changing the checksum/compression from the
3463baa08fcSek 	 * default (fletcher2/off).  Snapshots don't need to know about
3473baa08fcSek 	 * checksum/compression/copies.
348fa9e4066Sahrens 	 */
3495d7b4d43SMatthew Ahrens 	if (ds != NULL) {
3503b2aab18SMatthew Ahrens 		err = dsl_prop_register(ds,
3513b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
352503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
3533b2aab18SMatthew Ahrens 		if (err == 0) {
3543b2aab18SMatthew Ahrens 			err = dsl_prop_register(ds,
3553b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
356503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
3573b2aab18SMatthew Ahrens 		}
3583baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
3593b2aab18SMatthew Ahrens 			if (err == 0) {
3603b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3613b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
362503ad85cSMatthew Ahrens 				    checksum_changed_cb, os);
3633b2aab18SMatthew Ahrens 			}
3643b2aab18SMatthew Ahrens 			if (err == 0) {
3653b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3663b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
367503ad85cSMatthew Ahrens 				    compression_changed_cb, os);
3683b2aab18SMatthew Ahrens 			}
3693b2aab18SMatthew Ahrens 			if (err == 0) {
3703b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3713b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COPIES),
372503ad85cSMatthew Ahrens 				    copies_changed_cb, os);
3733b2aab18SMatthew Ahrens 			}
3743b2aab18SMatthew Ahrens 			if (err == 0) {
3753b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3763b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_DEDUP),
377b24ab676SJeff Bonwick 				    dedup_changed_cb, os);
3783b2aab18SMatthew Ahrens 			}
3793b2aab18SMatthew Ahrens 			if (err == 0) {
3803b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3813b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
382e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
3833b2aab18SMatthew Ahrens 			}
3843b2aab18SMatthew Ahrens 			if (err == 0) {
3853b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3863b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_SYNC),
38755da60b9SMark J Musante 				    sync_changed_cb, os);
3883b2aab18SMatthew Ahrens 			}
389edf345e6SMatthew Ahrens 			if (err == 0) {
390edf345e6SMatthew Ahrens 				err = dsl_prop_register(ds,
391edf345e6SMatthew Ahrens 				    zfs_prop_to_name(
392edf345e6SMatthew Ahrens 				    ZFS_PROP_REDUNDANT_METADATA),
393edf345e6SMatthew Ahrens 				    redundant_metadata_changed_cb, os);
394edf345e6SMatthew Ahrens 			}
395*b5152584SMatthew Ahrens 			if (err == 0) {
396*b5152584SMatthew Ahrens 				err = dsl_prop_register(ds,
397*b5152584SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
398*b5152584SMatthew Ahrens 				    recordsize_changed_cb, os);
399*b5152584SMatthew Ahrens 			}
4003baa08fcSek 		}
4013b2aab18SMatthew Ahrens 		if (err != 0) {
402503ad85cSMatthew Ahrens 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
4033b2aab18SMatthew Ahrens 			    &os->os_phys_buf));
404503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
405ea8dc4b6Seschrock 			return (err);
406ea8dc4b6Seschrock 		}
4075d7b4d43SMatthew Ahrens 	} else {
408fa9e4066Sahrens 		/* It's the meta-objset. */
409503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
410503ad85cSMatthew Ahrens 		os->os_compress = ZIO_COMPRESS_LZJB;
411503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
412b24ab676SJeff Bonwick 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
413edf345e6SMatthew Ahrens 		os->os_dedup_verify = B_FALSE;
414edf345e6SMatthew Ahrens 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
415edf345e6SMatthew Ahrens 		os->os_sync = ZFS_SYNC_STANDARD;
416503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
417503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
418fa9e4066Sahrens 	}
419fa9e4066Sahrens 
4206e0cbcaaSMatthew Ahrens 	if (ds == NULL || !dsl_dataset_is_snapshot(ds))
4216e0cbcaaSMatthew Ahrens 		os->os_zil_header = os->os_phys->os_zil_header;
422503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
423fa9e4066Sahrens 
424fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
425503ad85cSMatthew Ahrens 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
426fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
427503ad85cSMatthew Ahrens 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
428fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
429fa9e4066Sahrens 	}
430503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
431fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
432503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
433fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
434fa9e4066Sahrens 
435503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
436503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
437503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
438503ad85cSMatthew Ahrens 
439744947dcSTom Erickson 	DMU_META_DNODE(os) = dnode_special_open(os,
440744947dcSTom Erickson 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
441744947dcSTom Erickson 	    &os->os_meta_dnode);
442503ad85cSMatthew Ahrens 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
443744947dcSTom Erickson 		DMU_USERUSED_DNODE(os) = dnode_special_open(os,
444744947dcSTom Erickson 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
445744947dcSTom Erickson 		    &os->os_userused_dnode);
446744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
447744947dcSTom Erickson 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
448744947dcSTom Erickson 		    &os->os_groupused_dnode);
44914843421SMatthew Ahrens 	}
450fa9e4066Sahrens 
451503ad85cSMatthew Ahrens 	*osp = os;
452ea8dc4b6Seschrock 	return (0);
453fa9e4066Sahrens }
454fa9e4066Sahrens 
455503ad85cSMatthew Ahrens int
456503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4573cb34c60Sahrens {
458503ad85cSMatthew Ahrens 	int err = 0;
4593cb34c60Sahrens 
4603cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
4615d7b4d43SMatthew Ahrens 	if (ds->ds_objset == NULL) {
4625d7b4d43SMatthew Ahrens 		objset_t *os;
4633cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
4645d7b4d43SMatthew Ahrens 		    ds, dsl_dataset_get_blkptr(ds), &os);
4655d7b4d43SMatthew Ahrens 
4665d7b4d43SMatthew Ahrens 		if (err == 0) {
4675d7b4d43SMatthew Ahrens 			mutex_enter(&ds->ds_lock);
4685d7b4d43SMatthew Ahrens 			ASSERT(ds->ds_objset == NULL);
4695d7b4d43SMatthew Ahrens 			ds->ds_objset = os;
4705d7b4d43SMatthew Ahrens 			mutex_exit(&ds->ds_lock);
4715d7b4d43SMatthew Ahrens 		}
4723cb34c60Sahrens 	}
4735d7b4d43SMatthew Ahrens 	*osp = ds->ds_objset;
4743cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
475503ad85cSMatthew Ahrens 	return (err);
4763cb34c60Sahrens }
4773cb34c60Sahrens 
4783b2aab18SMatthew Ahrens /*
4793b2aab18SMatthew Ahrens  * Holds the pool while the objset is held.  Therefore only one objset
4803b2aab18SMatthew Ahrens  * can be held at a time.
4813b2aab18SMatthew Ahrens  */
4823cb34c60Sahrens int
483503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
4843cb34c60Sahrens {
4853b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
486503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
4873cb34c60Sahrens 	int err;
4883cb34c60Sahrens 
4893b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, tag, &dp);
4903b2aab18SMatthew Ahrens 	if (err != 0)
4913b2aab18SMatthew Ahrens 		return (err);
4923b2aab18SMatthew Ahrens 	err = dsl_dataset_hold(dp, name, tag, &ds);
4933b2aab18SMatthew Ahrens 	if (err != 0) {
4943b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
495503ad85cSMatthew Ahrens 		return (err);
4963b2aab18SMatthew Ahrens 	}
497503ad85cSMatthew Ahrens 
498503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
4993b2aab18SMatthew Ahrens 	if (err != 0) {
500503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
5013b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
5023b2aab18SMatthew Ahrens 	}
503503ad85cSMatthew Ahrens 
5043cb34c60Sahrens 	return (err);
5053cb34c60Sahrens }
5063cb34c60Sahrens 
5073b2aab18SMatthew Ahrens /*
5083b2aab18SMatthew Ahrens  * dsl_pool must not be held when this is called.
5093b2aab18SMatthew Ahrens  * Upon successful return, there will be a longhold on the dataset,
5103b2aab18SMatthew Ahrens  * and the dsl_pool will not be held.
5113b2aab18SMatthew Ahrens  */
512fa9e4066Sahrens int
513503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
514503ad85cSMatthew Ahrens     boolean_t readonly, void *tag, objset_t **osp)
515fa9e4066Sahrens {
5163b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
517f18faf3fSek 	dsl_dataset_t *ds;
518f18faf3fSek 	int err;
519fa9e4066Sahrens 
5203b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
5213b2aab18SMatthew Ahrens 	if (err != 0)
5223b2aab18SMatthew Ahrens 		return (err);
5233b2aab18SMatthew Ahrens 	err = dsl_dataset_own(dp, name, tag, &ds);
5243b2aab18SMatthew Ahrens 	if (err != 0) {
5253b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
526fa9e4066Sahrens 		return (err);
5273b2aab18SMatthew Ahrens 	}
528fa9e4066Sahrens 
529503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
5303b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
5313b2aab18SMatthew Ahrens 	if (err != 0) {
532503ad85cSMatthew Ahrens 		dsl_dataset_disown(ds, tag);
533681d9761SEric Taylor 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
5343b2aab18SMatthew Ahrens 		dsl_dataset_disown(ds, tag);
535be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
536681d9761SEric Taylor 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
5373b2aab18SMatthew Ahrens 		dsl_dataset_disown(ds, tag);
538be6fd75aSMatthew Ahrens 		return (SET_ERROR(EROFS));
539fa9e4066Sahrens 	}
5403cb34c60Sahrens 	return (err);
541fa9e4066Sahrens }
542fa9e4066Sahrens 
543fa9e4066Sahrens void
544503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
545fa9e4066Sahrens {
5463b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(os);
547503ad85cSMatthew Ahrens 	dsl_dataset_rele(os->os_dsl_dataset, tag);
5483b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, tag);
549503ad85cSMatthew Ahrens }
550503ad85cSMatthew Ahrens 
55191948b51SKeith M Wesolowski /*
55291948b51SKeith M Wesolowski  * When we are called, os MUST refer to an objset associated with a dataset
55391948b51SKeith M Wesolowski  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
55491948b51SKeith M Wesolowski  * == tag.  We will then release and reacquire ownership of the dataset while
55591948b51SKeith M Wesolowski  * holding the pool config_rwlock to avoid intervening namespace or ownership
55691948b51SKeith M Wesolowski  * changes may occur.
55791948b51SKeith M Wesolowski  *
55891948b51SKeith M Wesolowski  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
55991948b51SKeith M Wesolowski  * release the hold on its dataset and acquire a new one on the dataset of the
56091948b51SKeith M Wesolowski  * same name so that it can be partially torn down and reconstructed.
56191948b51SKeith M Wesolowski  */
56291948b51SKeith M Wesolowski void
56391948b51SKeith M Wesolowski dmu_objset_refresh_ownership(objset_t *os, void *tag)
56491948b51SKeith M Wesolowski {
56591948b51SKeith M Wesolowski 	dsl_pool_t *dp;
56691948b51SKeith M Wesolowski 	dsl_dataset_t *ds, *newds;
56791948b51SKeith M Wesolowski 	char name[MAXNAMELEN];
56891948b51SKeith M Wesolowski 
56991948b51SKeith M Wesolowski 	ds = os->os_dsl_dataset;
57091948b51SKeith M Wesolowski 	VERIFY3P(ds, !=, NULL);
57191948b51SKeith M Wesolowski 	VERIFY3P(ds->ds_owner, ==, tag);
57291948b51SKeith M Wesolowski 	VERIFY(dsl_dataset_long_held(ds));
57391948b51SKeith M Wesolowski 
57491948b51SKeith M Wesolowski 	dsl_dataset_name(ds, name);
57591948b51SKeith M Wesolowski 	dp = dmu_objset_pool(os);
57691948b51SKeith M Wesolowski 	dsl_pool_config_enter(dp, FTAG);
57791948b51SKeith M Wesolowski 	dmu_objset_disown(os, tag);
57891948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
57991948b51SKeith M Wesolowski 	VERIFY3P(newds, ==, os->os_dsl_dataset);
58091948b51SKeith M Wesolowski 	dsl_pool_config_exit(dp, FTAG);
58191948b51SKeith M Wesolowski }
58291948b51SKeith M Wesolowski 
583503ad85cSMatthew Ahrens void
584503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
585503ad85cSMatthew Ahrens {
586503ad85cSMatthew Ahrens 	dsl_dataset_disown(os->os_dsl_dataset, tag);
587fa9e4066Sahrens }
588fa9e4066Sahrens 
5893b2aab18SMatthew Ahrens void
5901934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
591ea8dc4b6Seschrock {
592ea8dc4b6Seschrock 	dnode_t *dn;
593c543ec06Sahrens 
594503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
595c543ec06Sahrens 
596c543ec06Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
597744947dcSTom Erickson 	list_remove(&os->os_dnodes, DMU_META_DNODE(os));
598744947dcSTom Erickson 	list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
599ea8dc4b6Seschrock 
600ea8dc4b6Seschrock 	/*
601c543ec06Sahrens 	 * Find the first dnode with holds.  We have to do this dance
602c543ec06Sahrens 	 * because dnode_add_ref() only works if you already have a
603c543ec06Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
604c543ec06Sahrens 	 * skip.
605ea8dc4b6Seschrock 	 */
606503ad85cSMatthew Ahrens 	for (dn = list_head(&os->os_dnodes);
6071934e92fSmaybee 	    dn && !dnode_add_ref(dn, FTAG);
608503ad85cSMatthew Ahrens 	    dn = list_next(&os->os_dnodes, dn))
609c543ec06Sahrens 		continue;
610c543ec06Sahrens 
611c543ec06Sahrens 	while (dn) {
612c543ec06Sahrens 		dnode_t *next_dn = dn;
613c543ec06Sahrens 
614c543ec06Sahrens 		do {
615503ad85cSMatthew Ahrens 			next_dn = list_next(&os->os_dnodes, next_dn);
6161934e92fSmaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
617c543ec06Sahrens 
618503ad85cSMatthew Ahrens 		mutex_exit(&os->os_lock);
6191934e92fSmaybee 		dnode_evict_dbufs(dn);
620c543ec06Sahrens 		dnode_rele(dn, FTAG);
621503ad85cSMatthew Ahrens 		mutex_enter(&os->os_lock);
622c543ec06Sahrens 		dn = next_dn;
623ea8dc4b6Seschrock 	}
624503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
625ea8dc4b6Seschrock }
626ea8dc4b6Seschrock 
627fa9e4066Sahrens void
628503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
629fa9e4066Sahrens {
630503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
631fa9e4066Sahrens 
632b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
633b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, t));
634fa9e4066Sahrens 
6353baa08fcSek 	if (ds) {
6363baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
6373b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6383b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
639503ad85cSMatthew Ahrens 			    checksum_changed_cb, os));
6403b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6413b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
642503ad85cSMatthew Ahrens 			    compression_changed_cb, os));
6433b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6443b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_COPIES),
645503ad85cSMatthew Ahrens 			    copies_changed_cb, os));
6463b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6473b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_DEDUP),
648b24ab676SJeff Bonwick 			    dedup_changed_cb, os));
6493b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6503b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
651e09fa4daSNeil Perrin 			    logbias_changed_cb, os));
6523b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6533b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SYNC),
65455da60b9SMark J Musante 			    sync_changed_cb, os));
655edf345e6SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
656edf345e6SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA),
657edf345e6SMatthew Ahrens 			    redundant_metadata_changed_cb, os));
658*b5152584SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
659*b5152584SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
660*b5152584SMatthew Ahrens 			    recordsize_changed_cb, os));
6613baa08fcSek 		}
6623b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_unregister(ds,
6633b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
664503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os));
6653b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_unregister(ds,
6663b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
667503ad85cSMatthew Ahrens 		    secondary_cache_changed_cb, os));
668fa9e4066Sahrens 	}
669fa9e4066Sahrens 
6700a586ceaSMark Shellenbaum 	if (os->os_sa)
6710a586ceaSMark Shellenbaum 		sa_tear_down(os);
6720a586ceaSMark Shellenbaum 
6733b2aab18SMatthew Ahrens 	dmu_objset_evict_dbufs(os);
674ea8dc4b6Seschrock 
675744947dcSTom Erickson 	dnode_special_close(&os->os_meta_dnode);
676744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os)) {
677744947dcSTom Erickson 		dnode_special_close(&os->os_userused_dnode);
678744947dcSTom Erickson 		dnode_special_close(&os->os_groupused_dnode);
67914843421SMatthew Ahrens 	}
680503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
681fa9e4066Sahrens 
682503ad85cSMatthew Ahrens 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
68314843421SMatthew Ahrens 
6843b2aab18SMatthew Ahrens 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
685744947dcSTom Erickson 
686744947dcSTom Erickson 	/*
687744947dcSTom Erickson 	 * This is a barrier to prevent the objset from going away in
688744947dcSTom Erickson 	 * dnode_move() until we can safely ensure that the objset is still in
689744947dcSTom Erickson 	 * use. We consider the objset valid before the barrier and invalid
690744947dcSTom Erickson 	 * after the barrier.
691744947dcSTom Erickson 	 */
692744947dcSTom Erickson 	rw_enter(&os_lock, RW_READER);
693744947dcSTom Erickson 	rw_exit(&os_lock);
694744947dcSTom Erickson 
695503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
696503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
697503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
698503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
699fa9e4066Sahrens }
700fa9e4066Sahrens 
70171eb0538SChris Kirby timestruc_t
70271eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
70371eb0538SChris Kirby {
70471eb0538SChris Kirby 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
70571eb0538SChris Kirby }
70671eb0538SChris Kirby 
707fa9e4066Sahrens /* called from dsl for meta-objset */
708503ad85cSMatthew Ahrens objset_t *
709c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
710c717a561Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
711fa9e4066Sahrens {
712503ad85cSMatthew Ahrens 	objset_t *os;
713fa9e4066Sahrens 	dnode_t *mdn;
714fa9e4066Sahrens 
715fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7163b2aab18SMatthew Ahrens 
717feaa74e4SMark Maybee 	if (ds != NULL)
7183b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_from_ds(ds, &os));
719feaa74e4SMark Maybee 	else
7203b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
721feaa74e4SMark Maybee 
722744947dcSTom Erickson 	mdn = DMU_META_DNODE(os);
723fa9e4066Sahrens 
724fa9e4066Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
725fa9e4066Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
726fa9e4066Sahrens 
727fa9e4066Sahrens 	/*
728fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
729fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
730fa9e4066Sahrens 	 * we are also accessing it in open context.
731fa9e4066Sahrens 	 *
732fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
733fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
734fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
735fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
736fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
737fa9e4066Sahrens 	 */
738ea8dc4b6Seschrock 	if (ds != NULL) {
739ea8dc4b6Seschrock 		int levels = 1;
740ea8dc4b6Seschrock 
741ea8dc4b6Seschrock 		/*
742ea8dc4b6Seschrock 		 * Determine the number of levels necessary for the meta-dnode
743ea8dc4b6Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
744ea8dc4b6Seschrock 		 */
745ea8dc4b6Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
746ea8dc4b6Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
747ea8dc4b6Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
748ea8dc4b6Seschrock 			levels++;
749ea8dc4b6Seschrock 
750fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
751ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
752ea8dc4b6Seschrock 	}
753fa9e4066Sahrens 
754fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
755fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
756fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
757503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
758503ad85cSMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
759503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
760503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
76114843421SMatthew Ahrens 	}
762fa9e4066Sahrens 
763fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
764fa9e4066Sahrens 
765503ad85cSMatthew Ahrens 	return (os);
766fa9e4066Sahrens }
767fa9e4066Sahrens 
7683b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg {
7693b2aab18SMatthew Ahrens 	const char *doca_name;
7703b2aab18SMatthew Ahrens 	cred_t *doca_cred;
7713b2aab18SMatthew Ahrens 	void (*doca_userfunc)(objset_t *os, void *arg,
7723b2aab18SMatthew Ahrens 	    cred_t *cr, dmu_tx_t *tx);
7733b2aab18SMatthew Ahrens 	void *doca_userarg;
7743b2aab18SMatthew Ahrens 	dmu_objset_type_t doca_type;
7753b2aab18SMatthew Ahrens 	uint64_t doca_flags;
7763b2aab18SMatthew Ahrens } dmu_objset_create_arg_t;
777fa9e4066Sahrens 
778ecd6cf80Smarks /*ARGSUSED*/
779fa9e4066Sahrens static int
7803b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx)
781fa9e4066Sahrens {
7823b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
7833b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
7843b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
7853b2aab18SMatthew Ahrens 	const char *tail;
7863b2aab18SMatthew Ahrens 	int error;
7871d452cf5Sahrens 
7883b2aab18SMatthew Ahrens 	if (strchr(doca->doca_name, '@') != NULL)
789be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
7901d452cf5Sahrens 
7913b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
7923b2aab18SMatthew Ahrens 	if (error != 0)
7933b2aab18SMatthew Ahrens 		return (error);
7943b2aab18SMatthew Ahrens 	if (tail == NULL) {
7953b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
796be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
7971d452cf5Sahrens 	}
798a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
799a2afb611SJerry Jelinek 	    doca->doca_cred);
8003b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
801ecd6cf80Smarks 
802a2afb611SJerry Jelinek 	return (error);
8031d452cf5Sahrens }
8041d452cf5Sahrens 
8051d452cf5Sahrens static void
8063b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
8071d452cf5Sahrens {
8083b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
8093b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8103b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
8113b2aab18SMatthew Ahrens 	const char *tail;
8124445fffbSMatthew Ahrens 	dsl_dataset_t *ds;
8133b2aab18SMatthew Ahrens 	uint64_t obj;
8144445fffbSMatthew Ahrens 	blkptr_t *bp;
8153b2aab18SMatthew Ahrens 	objset_t *os;
816fa9e4066Sahrens 
8173b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
818fa9e4066Sahrens 
8193b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
8203b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
821fa9e4066Sahrens 
8223b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
8234445fffbSMatthew Ahrens 	bp = dsl_dataset_get_blkptr(ds);
8243b2aab18SMatthew Ahrens 	os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
8253b2aab18SMatthew Ahrens 	    ds, bp, doca->doca_type, tx);
826fa9e4066Sahrens 
8273b2aab18SMatthew Ahrens 	if (doca->doca_userfunc != NULL) {
8283b2aab18SMatthew Ahrens 		doca->doca_userfunc(os, doca->doca_userarg,
8293b2aab18SMatthew Ahrens 		    doca->doca_cred, tx);
830fa9e4066Sahrens 	}
831ecd6cf80Smarks 
8323b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "create", tx, "");
8334445fffbSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
8343b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
835fa9e4066Sahrens }
836fa9e4066Sahrens 
837fa9e4066Sahrens int
838ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
839ecd6cf80Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
840fa9e4066Sahrens {
8413b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t doca;
842fa9e4066Sahrens 
8433b2aab18SMatthew Ahrens 	doca.doca_name = name;
8443b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
8453b2aab18SMatthew Ahrens 	doca.doca_flags = flags;
8463b2aab18SMatthew Ahrens 	doca.doca_userfunc = func;
8473b2aab18SMatthew Ahrens 	doca.doca_userarg = arg;
8483b2aab18SMatthew Ahrens 	doca.doca_type = type;
849ecd6cf80Smarks 
8503b2aab18SMatthew Ahrens 	return (dsl_sync_task(name,
8517d46dc6cSMatthew Ahrens 	    dmu_objset_create_check, dmu_objset_create_sync, &doca,
8527d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
853ae46e4c7SMatthew Ahrens }
854ae46e4c7SMatthew Ahrens 
8553b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg {
8563b2aab18SMatthew Ahrens 	const char *doca_clone;
8573b2aab18SMatthew Ahrens 	const char *doca_origin;
8583b2aab18SMatthew Ahrens 	cred_t *doca_cred;
8593b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t;
8603b2aab18SMatthew Ahrens 
8613b2aab18SMatthew Ahrens /*ARGSUSED*/
8623b2aab18SMatthew Ahrens static int
8633b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
864ae46e4c7SMatthew Ahrens {
8653b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
866ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
867ae46e4c7SMatthew Ahrens 	const char *tail;
8683b2aab18SMatthew Ahrens 	int error;
8693b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
8703b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
871ae46e4c7SMatthew Ahrens 
8723b2aab18SMatthew Ahrens 	if (strchr(doca->doca_clone, '@') != NULL)
873be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8743b2aab18SMatthew Ahrens 
8753b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
8763b2aab18SMatthew Ahrens 	if (error != 0)
8773b2aab18SMatthew Ahrens 		return (error);
878ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
8793b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
880be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
881fa9e4066Sahrens 	}
8823b2aab18SMatthew Ahrens 	/* You can't clone across pools. */
8833b2aab18SMatthew Ahrens 	if (pdd->dd_pool != dp) {
8843b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
885be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
886fa9e4066Sahrens 	}
887a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
888a2afb611SJerry Jelinek 	    doca->doca_cred);
889a2afb611SJerry Jelinek 	if (error != 0) {
890a2afb611SJerry Jelinek 		dsl_dir_rele(pdd, FTAG);
891a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
892a2afb611SJerry Jelinek 	}
8933b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
894fa9e4066Sahrens 
8953b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
8963b2aab18SMatthew Ahrens 	if (error != 0)
89799d5e173STim Haley 		return (error);
89899d5e173STim Haley 
8993b2aab18SMatthew Ahrens 	/* You can't clone across pools. */
9003b2aab18SMatthew Ahrens 	if (origin->ds_dir->dd_pool != dp) {
9013b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
902be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
90399d5e173STim Haley 	}
904ea2f5b9eSMatthew Ahrens 
9053b2aab18SMatthew Ahrens 	/* You can only clone snapshots, not the head datasets. */
9063b2aab18SMatthew Ahrens 	if (!dsl_dataset_is_snapshot(origin)) {
9073b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
908be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
90992241e0bSTom Erickson 	}
9103b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
91199d5e173STim Haley 
9123b2aab18SMatthew Ahrens 	return (0);
913ea2f5b9eSMatthew Ahrens }
9141d452cf5Sahrens 
9153b2aab18SMatthew Ahrens static void
9163b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
9171d452cf5Sahrens {
9183b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
9193b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
9203b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
9213b2aab18SMatthew Ahrens 	const char *tail;
9223b2aab18SMatthew Ahrens 	dsl_dataset_t *origin, *ds;
9233b2aab18SMatthew Ahrens 	uint64_t obj;
9243b2aab18SMatthew Ahrens 	char namebuf[MAXNAMELEN];
9254445fffbSMatthew Ahrens 
9263b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
9273b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
9284445fffbSMatthew Ahrens 
9293b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
9303b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
931f2e10be3Srm 
9323b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
9333b2aab18SMatthew Ahrens 	dsl_dataset_name(origin, namebuf);
9343b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "clone", tx,
9353b2aab18SMatthew Ahrens 	    "origin=%s (%llu)", namebuf, origin->ds_object);
9363b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
9373b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
9383b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
9391d452cf5Sahrens }
9401d452cf5Sahrens 
9411d452cf5Sahrens int
9423b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin)
9431d452cf5Sahrens {
9443b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t doca;
9454445fffbSMatthew Ahrens 
9463b2aab18SMatthew Ahrens 	doca.doca_clone = clone;
9473b2aab18SMatthew Ahrens 	doca.doca_origin = origin;
9483b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
94999d5e173STim Haley 
9503b2aab18SMatthew Ahrens 	return (dsl_sync_task(clone,
9517d46dc6cSMatthew Ahrens 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
9527d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
9534445fffbSMatthew Ahrens }
9544445fffbSMatthew Ahrens 
9554445fffbSMatthew Ahrens int
9564445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname)
9574445fffbSMatthew Ahrens {
9584445fffbSMatthew Ahrens 	int err;
9594445fffbSMatthew Ahrens 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
9604445fffbSMatthew Ahrens 	nvlist_t *snaps = fnvlist_alloc();
9614445fffbSMatthew Ahrens 
9624445fffbSMatthew Ahrens 	fnvlist_add_boolean(snaps, longsnap);
9634445fffbSMatthew Ahrens 	strfree(longsnap);
9643b2aab18SMatthew Ahrens 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
9653b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
9664445fffbSMatthew Ahrens 	return (err);
9674445fffbSMatthew Ahrens }
9684445fffbSMatthew Ahrens 
969fa9e4066Sahrens static void
97014843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
971fa9e4066Sahrens {
972c717a561Smaybee 	dnode_t *dn;
973faafa6e3Sahrens 
974c717a561Smaybee 	while (dn = list_head(list)) {
975c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
976c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
977c717a561Smaybee 		/*
97814843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
97914843421SMatthew Ahrens 		 * meta-dnode needs to set it ouside dnode_sync().
980c717a561Smaybee 		 */
981c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
982c717a561Smaybee 		ASSERT(dn->dn_zio);
983faafa6e3Sahrens 
984c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
985c717a561Smaybee 		list_remove(list, dn);
98614843421SMatthew Ahrens 
98714843421SMatthew Ahrens 		if (newlist) {
98814843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
98914843421SMatthew Ahrens 			list_insert_tail(newlist, dn);
99014843421SMatthew Ahrens 		}
99114843421SMatthew Ahrens 
992c717a561Smaybee 		dnode_sync(dn, tx);
993fa9e4066Sahrens 	}
994fa9e4066Sahrens }
995fa9e4066Sahrens 
996fa9e4066Sahrens /* ARGSUSED */
997fa9e4066Sahrens static void
998b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
999fa9e4066Sahrens {
1000e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1001503ad85cSMatthew Ahrens 	objset_t *os = arg;
1002c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1003fa9e4066Sahrens 
10045d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp));
10053b2aab18SMatthew Ahrens 	ASSERT3P(bp, ==, os->os_rootbp);
10063b2aab18SMatthew Ahrens 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
10073b2aab18SMatthew Ahrens 	ASSERT0(BP_GET_LEVEL(bp));
10080a4e9518Sgw 
1009fa9e4066Sahrens 	/*
101014843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
101114843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
101214843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
101314843421SMatthew Ahrens 	 * dnode and user/group accounting objects).
1014fa9e4066Sahrens 	 */
101514843421SMatthew Ahrens 	bp->blk_fill = 0;
1016e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
10175d7b4d43SMatthew Ahrens 		bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1018b24ab676SJeff Bonwick }
1019b24ab676SJeff Bonwick 
1020b24ab676SJeff Bonwick /* ARGSUSED */
1021b24ab676SJeff Bonwick static void
1022b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1023b24ab676SJeff Bonwick {
1024b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1025b24ab676SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
1026b24ab676SJeff Bonwick 	objset_t *os = arg;
10270a4e9518Sgw 
1028e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1029b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, bp_orig));
1030e14bb325SJeff Bonwick 	} else {
1031b24ab676SJeff Bonwick 		dsl_dataset_t *ds = os->os_dsl_dataset;
1032b24ab676SJeff Bonwick 		dmu_tx_t *tx = os->os_synctx;
1033b24ab676SJeff Bonwick 
1034b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1035b24ab676SJeff Bonwick 		dsl_dataset_block_born(ds, bp, tx);
10360a4e9518Sgw 	}
1037c717a561Smaybee }
1038c717a561Smaybee 
1039fa9e4066Sahrens /* called from dsl */
1040fa9e4066Sahrens void
1041503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1042fa9e4066Sahrens {
1043fa9e4066Sahrens 	int txgoff;
10447802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
1045b24ab676SJeff Bonwick 	zio_prop_t zp;
1046c717a561Smaybee 	zio_t *zio;
1047c717a561Smaybee 	list_t *list;
104814843421SMatthew Ahrens 	list_t *newlist = NULL;
1049c717a561Smaybee 	dbuf_dirty_record_t *dr;
1050c717a561Smaybee 
1051c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1052fa9e4066Sahrens 
1053fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1054fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
1055fa9e4066Sahrens 	os->os_synctx = tx;
1056fa9e4066Sahrens 
105787bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
105887bd5c1eSahrens 		/*
105987bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
106087bd5c1eSahrens 		 * spa_max_replication() could change, so reset
106187bd5c1eSahrens 		 * os_copies here.
106287bd5c1eSahrens 		 */
106387bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
106487bd5c1eSahrens 	}
106587bd5c1eSahrens 
1066fa9e4066Sahrens 	/*
1067c717a561Smaybee 	 * Create the root block IO
1068fa9e4066Sahrens 	 */
1069b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1070b24ab676SJeff Bonwick 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1071b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
10721b912ec7SGeorge Wilson 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1073b24ab676SJeff Bonwick 
1074b24ab676SJeff Bonwick 	dmu_write_policy(os, NULL, 0, 0, &zp);
1075b24ab676SJeff Bonwick 
1076b24ab676SJeff Bonwick 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1077aad02571SSaso Kiselkov 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1078aad02571SSaso Kiselkov 	    DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
107969962b56SMatthew Ahrens 	    NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1080aad02571SSaso Kiselkov 	    ZIO_FLAG_MUSTSUCCEED, &zb);
1081c717a561Smaybee 
1082c717a561Smaybee 	/*
108314843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
1084c717a561Smaybee 	 */
1085744947dcSTom Erickson 	DMU_META_DNODE(os)->dn_zio = zio;
1086744947dcSTom Erickson 	dnode_sync(DMU_META_DNODE(os), tx);
1087c717a561Smaybee 
108814843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
108914843421SMatthew Ahrens 
1090744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) &&
1091744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1092744947dcSTom Erickson 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1093744947dcSTom Erickson 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1094744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1095744947dcSTom Erickson 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
109614843421SMatthew Ahrens 	}
109714843421SMatthew Ahrens 
1098c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
1099fa9e4066Sahrens 
110014843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
110114843421SMatthew Ahrens 		newlist = &os->os_synced_dnodes;
110214843421SMatthew Ahrens 		/*
110314843421SMatthew Ahrens 		 * We must create the list here because it uses the
110414843421SMatthew Ahrens 		 * dn_dirty_link[] of this txg.
110514843421SMatthew Ahrens 		 */
110614843421SMatthew Ahrens 		list_create(newlist, sizeof (dnode_t),
110714843421SMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
110814843421SMatthew Ahrens 	}
110914843421SMatthew Ahrens 
111014843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
111114843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1112fa9e4066Sahrens 
1113744947dcSTom Erickson 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1114c717a561Smaybee 	while (dr = list_head(list)) {
11153b2aab18SMatthew Ahrens 		ASSERT0(dr->dr_dbuf->db_level);
1116c717a561Smaybee 		list_remove(list, dr);
1117c717a561Smaybee 		if (dr->dr_zio)
1118c717a561Smaybee 			zio_nowait(dr->dr_zio);
1119c717a561Smaybee 	}
1120c717a561Smaybee 	/*
1121c717a561Smaybee 	 * Free intent log blocks up to this tx.
1122c717a561Smaybee 	 */
1123c717a561Smaybee 	zil_sync(os->os_zil, tx);
1124088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
1125c717a561Smaybee 	zio_nowait(zio);
1126fa9e4066Sahrens }
1127fa9e4066Sahrens 
1128b24ab676SJeff Bonwick boolean_t
1129b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1130b24ab676SJeff Bonwick {
1131b24ab676SJeff Bonwick 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1132b24ab676SJeff Bonwick 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1133b24ab676SJeff Bonwick }
1134b24ab676SJeff Bonwick 
1135744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
113614843421SMatthew Ahrens 
113714843421SMatthew Ahrens void
113814843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
113914843421SMatthew Ahrens {
114014843421SMatthew Ahrens 	used_cbs[ost] = cb;
114114843421SMatthew Ahrens }
114214843421SMatthew Ahrens 
114314843421SMatthew Ahrens boolean_t
1144503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
114514843421SMatthew Ahrens {
114614843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1147744947dcSTom Erickson 	    used_cbs[os->os_phys->os_type] != NULL &&
1148744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os) != NULL);
114914843421SMatthew Ahrens }
115014843421SMatthew Ahrens 
11519966ca11SMatthew Ahrens static void
11520a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
11530a586ceaSMark Shellenbaum     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
11549966ca11SMatthew Ahrens {
11550a586ceaSMark Shellenbaum 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
11560a586ceaSMark Shellenbaum 		int64_t delta = DNODE_SIZE + used;
11579966ca11SMatthew Ahrens 		if (subtract)
11589966ca11SMatthew Ahrens 			delta = -delta;
1159b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
11609966ca11SMatthew Ahrens 		    user, delta, tx));
1161b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
11629966ca11SMatthew Ahrens 		    group, delta, tx));
11639966ca11SMatthew Ahrens 	}
11649966ca11SMatthew Ahrens }
11659966ca11SMatthew Ahrens 
116614843421SMatthew Ahrens void
11670a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
116814843421SMatthew Ahrens {
116914843421SMatthew Ahrens 	dnode_t *dn;
117014843421SMatthew Ahrens 	list_t *list = &os->os_synced_dnodes;
117114843421SMatthew Ahrens 
117214843421SMatthew Ahrens 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
117314843421SMatthew Ahrens 
117414843421SMatthew Ahrens 	while (dn = list_head(list)) {
11751d8ccc7bSMark Shellenbaum 		int flags;
117614843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
117714843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
117814843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
117914843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
118014843421SMatthew Ahrens 
118114843421SMatthew Ahrens 		/* Allocate the user/groupused objects if necessary. */
1182744947dcSTom Erickson 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1183503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
118414843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT,
118514843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1186503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
118714843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT,
118814843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
118914843421SMatthew Ahrens 		}
119014843421SMatthew Ahrens 
119114843421SMatthew Ahrens 		/*
11929966ca11SMatthew Ahrens 		 * We intentionally modify the zap object even if the
11930a586ceaSMark Shellenbaum 		 * net delta is zero.  Otherwise
11949966ca11SMatthew Ahrens 		 * the block of the zap obj could be shared between
11959966ca11SMatthew Ahrens 		 * datasets but need to be different between them after
11969966ca11SMatthew Ahrens 		 * a bprewrite.
119714843421SMatthew Ahrens 		 */
119814843421SMatthew Ahrens 
11991d8ccc7bSMark Shellenbaum 		flags = dn->dn_id_flags;
12001d8ccc7bSMark Shellenbaum 		ASSERT(flags);
12011d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST)  {
12020a586ceaSMark Shellenbaum 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
12030a586ceaSMark Shellenbaum 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
12040a586ceaSMark Shellenbaum 		}
12051d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_NEW_EXIST) {
12060a586ceaSMark Shellenbaum 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
12070a586ceaSMark Shellenbaum 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
12080a586ceaSMark Shellenbaum 			    dn->dn_newgid, B_FALSE, tx);
12090a586ceaSMark Shellenbaum 		}
12100a586ceaSMark Shellenbaum 
12111d8ccc7bSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
12120a586ceaSMark Shellenbaum 		dn->dn_oldused = 0;
12130a586ceaSMark Shellenbaum 		dn->dn_oldflags = 0;
12140a586ceaSMark Shellenbaum 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
12150a586ceaSMark Shellenbaum 			dn->dn_olduid = dn->dn_newuid;
12160a586ceaSMark Shellenbaum 			dn->dn_oldgid = dn->dn_newgid;
12170a586ceaSMark Shellenbaum 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
12180a586ceaSMark Shellenbaum 			if (dn->dn_bonuslen == 0)
12190a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
12200a586ceaSMark Shellenbaum 			else
12210a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
12220a586ceaSMark Shellenbaum 		}
122328d97a71SMark Shellenbaum 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
122414843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
122514843421SMatthew Ahrens 
122614843421SMatthew Ahrens 		list_remove(list, dn);
122714843421SMatthew Ahrens 		dnode_rele(dn, list);
122814843421SMatthew Ahrens 	}
122914843421SMatthew Ahrens }
123014843421SMatthew Ahrens 
123106e0070dSMark Shellenbaum /*
123206e0070dSMark Shellenbaum  * Returns a pointer to data to find uid/gid from
123306e0070dSMark Shellenbaum  *
123406e0070dSMark Shellenbaum  * If a dirty record for transaction group that is syncing can't
123506e0070dSMark Shellenbaum  * be found then NULL is returned.  In the NULL case it is assumed
123606e0070dSMark Shellenbaum  * the uid/gid aren't changing.
123706e0070dSMark Shellenbaum  */
123806e0070dSMark Shellenbaum static void *
123906e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
124006e0070dSMark Shellenbaum {
124106e0070dSMark Shellenbaum 	dbuf_dirty_record_t *dr, **drp;
124206e0070dSMark Shellenbaum 	void *data;
124306e0070dSMark Shellenbaum 
124406e0070dSMark Shellenbaum 	if (db->db_dirtycnt == 0)
124506e0070dSMark Shellenbaum 		return (db->db.db_data);  /* Nothing is changing */
124606e0070dSMark Shellenbaum 
124706e0070dSMark Shellenbaum 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
124806e0070dSMark Shellenbaum 		if (dr->dr_txg == tx->tx_txg)
124906e0070dSMark Shellenbaum 			break;
125006e0070dSMark Shellenbaum 
1251744947dcSTom Erickson 	if (dr == NULL) {
125206e0070dSMark Shellenbaum 		data = NULL;
1253744947dcSTom Erickson 	} else {
1254744947dcSTom Erickson 		dnode_t *dn;
1255744947dcSTom Erickson 
1256744947dcSTom Erickson 		DB_DNODE_ENTER(dr->dr_dbuf);
1257744947dcSTom Erickson 		dn = DB_DNODE(dr->dr_dbuf);
1258744947dcSTom Erickson 
1259744947dcSTom Erickson 		if (dn->dn_bonuslen == 0 &&
1260744947dcSTom Erickson 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1261744947dcSTom Erickson 			data = dr->dt.dl.dr_data->b_data;
1262744947dcSTom Erickson 		else
1263744947dcSTom Erickson 			data = dr->dt.dl.dr_data;
1264744947dcSTom Erickson 
1265744947dcSTom Erickson 		DB_DNODE_EXIT(dr->dr_dbuf);
1266744947dcSTom Erickson 	}
1267744947dcSTom Erickson 
126806e0070dSMark Shellenbaum 	return (data);
126906e0070dSMark Shellenbaum }
127006e0070dSMark Shellenbaum 
12710a586ceaSMark Shellenbaum void
127206e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
12730a586ceaSMark Shellenbaum {
12740a586ceaSMark Shellenbaum 	objset_t *os = dn->dn_objset;
12750a586ceaSMark Shellenbaum 	void *data = NULL;
127606e0070dSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
1277d5285caeSGeorge Wilson 	uint64_t *user = NULL;
1278d5285caeSGeorge Wilson 	uint64_t *group = NULL;
12790a586ceaSMark Shellenbaum 	int flags = dn->dn_id_flags;
12800a586ceaSMark Shellenbaum 	int error;
128106e0070dSMark Shellenbaum 	boolean_t have_spill = B_FALSE;
12820a586ceaSMark Shellenbaum 
12830a586ceaSMark Shellenbaum 	if (!dmu_objset_userused_enabled(dn->dn_objset))
12840a586ceaSMark Shellenbaum 		return;
12850a586ceaSMark Shellenbaum 
12860a586ceaSMark Shellenbaum 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
12870a586ceaSMark Shellenbaum 	    DN_ID_CHKED_SPILL)))
12880a586ceaSMark Shellenbaum 		return;
12890a586ceaSMark Shellenbaum 
12900a586ceaSMark Shellenbaum 	if (before && dn->dn_bonuslen != 0)
12910a586ceaSMark Shellenbaum 		data = DN_BONUS(dn->dn_phys);
129206e0070dSMark Shellenbaum 	else if (!before && dn->dn_bonuslen != 0) {
129306e0070dSMark Shellenbaum 		if (dn->dn_bonus) {
129406e0070dSMark Shellenbaum 			db = dn->dn_bonus;
129506e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
129606e0070dSMark Shellenbaum 			data = dmu_objset_userquota_find_data(db, tx);
129706e0070dSMark Shellenbaum 		} else {
129806e0070dSMark Shellenbaum 			data = DN_BONUS(dn->dn_phys);
129906e0070dSMark Shellenbaum 		}
130006e0070dSMark Shellenbaum 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
13010a586ceaSMark Shellenbaum 			int rf = 0;
13020a586ceaSMark Shellenbaum 
13030a586ceaSMark Shellenbaum 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
13040a586ceaSMark Shellenbaum 				rf |= DB_RF_HAVESTRUCT;
13051d8ccc7bSMark Shellenbaum 			error = dmu_spill_hold_by_dnode(dn,
13061d8ccc7bSMark Shellenbaum 			    rf | DB_RF_MUST_SUCCEED,
130706e0070dSMark Shellenbaum 			    FTAG, (dmu_buf_t **)&db);
13080a586ceaSMark Shellenbaum 			ASSERT(error == 0);
130906e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
131006e0070dSMark Shellenbaum 			data = (before) ? db->db.db_data :
131106e0070dSMark Shellenbaum 			    dmu_objset_userquota_find_data(db, tx);
131206e0070dSMark Shellenbaum 			have_spill = B_TRUE;
13130a586ceaSMark Shellenbaum 	} else {
13140a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
13150a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13160a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
13170a586ceaSMark Shellenbaum 		return;
13180a586ceaSMark Shellenbaum 	}
13190a586ceaSMark Shellenbaum 
13200a586ceaSMark Shellenbaum 	if (before) {
132106e0070dSMark Shellenbaum 		ASSERT(data);
13220a586ceaSMark Shellenbaum 		user = &dn->dn_olduid;
13230a586ceaSMark Shellenbaum 		group = &dn->dn_oldgid;
132406e0070dSMark Shellenbaum 	} else if (data) {
13250a586ceaSMark Shellenbaum 		user = &dn->dn_newuid;
13260a586ceaSMark Shellenbaum 		group = &dn->dn_newgid;
13270a586ceaSMark Shellenbaum 	}
13280a586ceaSMark Shellenbaum 
132906e0070dSMark Shellenbaum 	/*
133006e0070dSMark Shellenbaum 	 * Must always call the callback in case the object
133106e0070dSMark Shellenbaum 	 * type has changed and that type isn't an object type to track
133206e0070dSMark Shellenbaum 	 */
13330a586ceaSMark Shellenbaum 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
13340a586ceaSMark Shellenbaum 	    user, group);
13350a586ceaSMark Shellenbaum 
133606e0070dSMark Shellenbaum 	/*
133706e0070dSMark Shellenbaum 	 * Preserve existing uid/gid when the callback can't determine
133806e0070dSMark Shellenbaum 	 * what the new uid/gid are and the callback returned EEXIST.
133906e0070dSMark Shellenbaum 	 * The EEXIST error tells us to just use the existing uid/gid.
134006e0070dSMark Shellenbaum 	 * If we don't know what the old values are then just assign
134106e0070dSMark Shellenbaum 	 * them to 0, since that is a new file  being created.
134206e0070dSMark Shellenbaum 	 */
134306e0070dSMark Shellenbaum 	if (!before && data == NULL && error == EEXIST) {
134406e0070dSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST) {
134506e0070dSMark Shellenbaum 			dn->dn_newuid = dn->dn_olduid;
134606e0070dSMark Shellenbaum 			dn->dn_newgid = dn->dn_oldgid;
134706e0070dSMark Shellenbaum 		} else {
134806e0070dSMark Shellenbaum 			dn->dn_newuid = 0;
134906e0070dSMark Shellenbaum 			dn->dn_newgid = 0;
135006e0070dSMark Shellenbaum 		}
135106e0070dSMark Shellenbaum 		error = 0;
135206e0070dSMark Shellenbaum 	}
135306e0070dSMark Shellenbaum 
135406e0070dSMark Shellenbaum 	if (db)
135506e0070dSMark Shellenbaum 		mutex_exit(&db->db_mtx);
135606e0070dSMark Shellenbaum 
13570a586ceaSMark Shellenbaum 	mutex_enter(&dn->dn_mtx);
13580a586ceaSMark Shellenbaum 	if (error == 0 && before)
13590a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
13600a586ceaSMark Shellenbaum 	if (error == 0 && !before)
13610a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
13620a586ceaSMark Shellenbaum 
136306e0070dSMark Shellenbaum 	if (have_spill) {
13640a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
13650a586ceaSMark Shellenbaum 	} else {
13660a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13670a586ceaSMark Shellenbaum 	}
13680a586ceaSMark Shellenbaum 	mutex_exit(&dn->dn_mtx);
136906e0070dSMark Shellenbaum 	if (have_spill)
137006e0070dSMark Shellenbaum 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
13710a586ceaSMark Shellenbaum }
13720a586ceaSMark Shellenbaum 
137314843421SMatthew Ahrens boolean_t
137414843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
137514843421SMatthew Ahrens {
1376503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
137714843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
137814843421SMatthew Ahrens }
137914843421SMatthew Ahrens 
138014843421SMatthew Ahrens int
138114843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
138214843421SMatthew Ahrens {
138314843421SMatthew Ahrens 	uint64_t obj;
138414843421SMatthew Ahrens 	int err = 0;
138514843421SMatthew Ahrens 
138614843421SMatthew Ahrens 	if (dmu_objset_userspace_present(os))
138714843421SMatthew Ahrens 		return (0);
1388503ad85cSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
1389be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
139014843421SMatthew Ahrens 	if (dmu_objset_is_snapshot(os))
1391be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
139214843421SMatthew Ahrens 
139314843421SMatthew Ahrens 	/*
139414843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
139514843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
139614843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
139714843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
139814843421SMatthew Ahrens 	 * independently.
139914843421SMatthew Ahrens 	 */
140014843421SMatthew Ahrens 
140114843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
140268857716SLin Ling 		dmu_tx_t *tx;
140314843421SMatthew Ahrens 		dmu_buf_t *db;
140414843421SMatthew Ahrens 		int objerr;
140514843421SMatthew Ahrens 
140614843421SMatthew Ahrens 		if (issig(JUSTLOOKING) && issig(FORREAL))
1407be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
140814843421SMatthew Ahrens 
140914843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
14103b2aab18SMatthew Ahrens 		if (objerr != 0)
141114843421SMatthew Ahrens 			continue;
141268857716SLin Ling 		tx = dmu_tx_create(os);
141314843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
141414843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
14153b2aab18SMatthew Ahrens 		if (objerr != 0) {
141614843421SMatthew Ahrens 			dmu_tx_abort(tx);
141714843421SMatthew Ahrens 			continue;
141814843421SMatthew Ahrens 		}
141914843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
142014843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
142114843421SMatthew Ahrens 		dmu_tx_commit(tx);
142214843421SMatthew Ahrens 	}
142314843421SMatthew Ahrens 
1424503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
142514843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
142614843421SMatthew Ahrens 	return (0);
142714843421SMatthew Ahrens }
142814843421SMatthew Ahrens 
1429fa9e4066Sahrens void
1430a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1431a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1432fa9e4066Sahrens {
1433503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1434a2eea2e1Sahrens 	    usedobjsp, availobjsp);
1435a2eea2e1Sahrens }
1436a2eea2e1Sahrens 
1437a2eea2e1Sahrens uint64_t
1438a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1439a2eea2e1Sahrens {
1440503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1441a2eea2e1Sahrens }
1442a2eea2e1Sahrens 
1443a2eea2e1Sahrens void
1444a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1445a2eea2e1Sahrens {
1446503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
1447503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
1448503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1449a2eea2e1Sahrens }
1450a2eea2e1Sahrens 
1451a2eea2e1Sahrens void
1452a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1453a2eea2e1Sahrens {
1454503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
1455503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
1456a2eea2e1Sahrens 
1457503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1458503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1459a2eea2e1Sahrens 
1460a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1461503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
146214843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
146314843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
1464fa9e4066Sahrens }
1465fa9e4066Sahrens 
1466fa9e4066Sahrens int
1467fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1468fa9e4066Sahrens {
1469503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1470503ad85cSMatthew Ahrens 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1471fa9e4066Sahrens 	else
1472fa9e4066Sahrens 		return (B_FALSE);
1473fa9e4066Sahrens }
1474fa9e4066Sahrens 
1475ab04eb8eStimh int
1476ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1477ab04eb8eStimh     boolean_t *conflict)
1478ab04eb8eStimh {
1479503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1480ab04eb8eStimh 	uint64_t ignored;
1481ab04eb8eStimh 
1482ab04eb8eStimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1483be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1484ab04eb8eStimh 
1485ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1486ab04eb8eStimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1487ab04eb8eStimh 	    real, maxlen, conflict));
1488ab04eb8eStimh }
1489ab04eb8eStimh 
1490fa9e4066Sahrens int
1491fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1492b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1493fa9e4066Sahrens {
1494503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1495fa9e4066Sahrens 	zap_cursor_t cursor;
1496fa9e4066Sahrens 	zap_attribute_t attr;
1497fa9e4066Sahrens 
14983b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
14993b2aab18SMatthew Ahrens 
1500fa9e4066Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1501be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1502fa9e4066Sahrens 
1503fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
1504fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1505fa9e4066Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1506fa9e4066Sahrens 
150787e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
150887e5029aSahrens 		zap_cursor_fini(&cursor);
1509be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
151087e5029aSahrens 	}
151187e5029aSahrens 
151287e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
151387e5029aSahrens 		zap_cursor_fini(&cursor);
1514be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
151587e5029aSahrens 	}
151687e5029aSahrens 
151787e5029aSahrens 	(void) strcpy(name, attr.za_name);
151887e5029aSahrens 	if (idp)
151987e5029aSahrens 		*idp = attr.za_first_integer;
1520b38f0970Sck 	if (case_conflict)
1521b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
152287e5029aSahrens 	zap_cursor_advance(&cursor);
152387e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
152487e5029aSahrens 	zap_cursor_fini(&cursor);
152587e5029aSahrens 
152687e5029aSahrens 	return (0);
152787e5029aSahrens }
152887e5029aSahrens 
152987e5029aSahrens int
153087e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
153187e5029aSahrens     uint64_t *idp, uint64_t *offp)
153287e5029aSahrens {
1533503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
153487e5029aSahrens 	zap_cursor_t cursor;
153587e5029aSahrens 	zap_attribute_t attr;
153687e5029aSahrens 
153787e5029aSahrens 	/* there is no next dir on a snapshot! */
1538503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
153987e5029aSahrens 	    dd->dd_phys->dd_head_dataset_obj)
1540be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1541fa9e4066Sahrens 
154287e5029aSahrens 	zap_cursor_init_serialized(&cursor,
154387e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
154487e5029aSahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
154587e5029aSahrens 
154687e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
154787e5029aSahrens 		zap_cursor_fini(&cursor);
1548be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
154987e5029aSahrens 	}
155087e5029aSahrens 
155187e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
155287e5029aSahrens 		zap_cursor_fini(&cursor);
1553be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
155487e5029aSahrens 	}
1555fa9e4066Sahrens 
1556fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
155787e5029aSahrens 	if (idp)
155887e5029aSahrens 		*idp = attr.za_first_integer;
1559fa9e4066Sahrens 	zap_cursor_advance(&cursor);
1560fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
156187e5029aSahrens 	zap_cursor_fini(&cursor);
1562fa9e4066Sahrens 
1563fa9e4066Sahrens 	return (0);
1564fa9e4066Sahrens }
1565fa9e4066Sahrens 
1566fa9e4066Sahrens /*
15673b2aab18SMatthew Ahrens  * Find objsets under and including ddobj, call func(ds) on each.
1568fa9e4066Sahrens  */
15691d452cf5Sahrens int
15703b2aab18SMatthew Ahrens dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
15713b2aab18SMatthew Ahrens     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1572088f3894Sahrens {
15733b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
15743b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15753b2aab18SMatthew Ahrens 	zap_cursor_t zc;
15763b2aab18SMatthew Ahrens 	zap_attribute_t *attr;
15773b2aab18SMatthew Ahrens 	uint64_t thisobj;
15783b2aab18SMatthew Ahrens 	int err;
15793b2aab18SMatthew Ahrens 
15803b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
15813b2aab18SMatthew Ahrens 
15823b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
15833b2aab18SMatthew Ahrens 	if (err != 0)
15843b2aab18SMatthew Ahrens 		return (err);
15853b2aab18SMatthew Ahrens 
15863b2aab18SMatthew Ahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
15873b2aab18SMatthew Ahrens 	if (dd->dd_myname[0] == '$') {
15883b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
15893b2aab18SMatthew Ahrens 		return (0);
15903b2aab18SMatthew Ahrens 	}
15913b2aab18SMatthew Ahrens 
15923b2aab18SMatthew Ahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
15933b2aab18SMatthew Ahrens 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
15943b2aab18SMatthew Ahrens 
15953b2aab18SMatthew Ahrens 	/*
15963b2aab18SMatthew Ahrens 	 * Iterate over all children.
15973b2aab18SMatthew Ahrens 	 */
15983b2aab18SMatthew Ahrens 	if (flags & DS_FIND_CHILDREN) {
15993b2aab18SMatthew Ahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
16003b2aab18SMatthew Ahrens 		    dd->dd_phys->dd_child_dir_zapobj);
16013b2aab18SMatthew Ahrens 		    zap_cursor_retrieve(&zc, attr) == 0;
16023b2aab18SMatthew Ahrens 		    (void) zap_cursor_advance(&zc)) {
16033b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
16043b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
16053b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
16063b2aab18SMatthew Ahrens 
16073b2aab18SMatthew Ahrens 			err = dmu_objset_find_dp(dp, attr->za_first_integer,
16083b2aab18SMatthew Ahrens 			    func, arg, flags);
16093b2aab18SMatthew Ahrens 			if (err != 0)
16103b2aab18SMatthew Ahrens 				break;
16113b2aab18SMatthew Ahrens 		}
16123b2aab18SMatthew Ahrens 		zap_cursor_fini(&zc);
16133b2aab18SMatthew Ahrens 
16143b2aab18SMatthew Ahrens 		if (err != 0) {
16153b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
16163b2aab18SMatthew Ahrens 			kmem_free(attr, sizeof (zap_attribute_t));
16173b2aab18SMatthew Ahrens 			return (err);
16183b2aab18SMatthew Ahrens 		}
16193b2aab18SMatthew Ahrens 	}
16203b2aab18SMatthew Ahrens 
16213b2aab18SMatthew Ahrens 	/*
16223b2aab18SMatthew Ahrens 	 * Iterate over all snapshots.
16233b2aab18SMatthew Ahrens 	 */
16243b2aab18SMatthew Ahrens 	if (flags & DS_FIND_SNAPSHOTS) {
16253b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
16263b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
16273b2aab18SMatthew Ahrens 
16283b2aab18SMatthew Ahrens 		if (err == 0) {
16293b2aab18SMatthew Ahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
16303b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
16313b2aab18SMatthew Ahrens 
16323b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
16333b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
16343b2aab18SMatthew Ahrens 			    (void) zap_cursor_advance(&zc)) {
16353b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
16363b2aab18SMatthew Ahrens 				    sizeof (uint64_t));
16373b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
16383b2aab18SMatthew Ahrens 
16393b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
16403b2aab18SMatthew Ahrens 				    attr->za_first_integer, FTAG, &ds);
16413b2aab18SMatthew Ahrens 				if (err != 0)
16423b2aab18SMatthew Ahrens 					break;
16433b2aab18SMatthew Ahrens 				err = func(dp, ds, arg);
16443b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
16453b2aab18SMatthew Ahrens 				if (err != 0)
16463b2aab18SMatthew Ahrens 					break;
16473b2aab18SMatthew Ahrens 			}
16483b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
16493b2aab18SMatthew Ahrens 		}
16503b2aab18SMatthew Ahrens 	}
16513b2aab18SMatthew Ahrens 
16523b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
16533b2aab18SMatthew Ahrens 	kmem_free(attr, sizeof (zap_attribute_t));
16543b2aab18SMatthew Ahrens 
16553b2aab18SMatthew Ahrens 	if (err != 0)
16563b2aab18SMatthew Ahrens 		return (err);
16573b2aab18SMatthew Ahrens 
16583b2aab18SMatthew Ahrens 	/*
16593b2aab18SMatthew Ahrens 	 * Apply to self.
16603b2aab18SMatthew Ahrens 	 */
16613b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
16623b2aab18SMatthew Ahrens 	if (err != 0)
16633b2aab18SMatthew Ahrens 		return (err);
16643b2aab18SMatthew Ahrens 	err = func(dp, ds, arg);
16653b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
16663b2aab18SMatthew Ahrens 	return (err);
1667088f3894Sahrens }
1668088f3894Sahrens 
1669088f3894Sahrens /*
16703b2aab18SMatthew Ahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
16713b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when this is called, and it
16723b2aab18SMatthew Ahrens  * will not be held when the callback is called.
16733b2aab18SMatthew Ahrens  * Therefore this function should only be used when the pool is not changing
16743b2aab18SMatthew Ahrens  * (e.g. in syncing context), or the callback can deal with the possible races.
1675088f3894Sahrens  */
16763b2aab18SMatthew Ahrens static int
16773b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name,
16783b2aab18SMatthew Ahrens     int func(const char *, void *), void *arg, int flags)
1679fa9e4066Sahrens {
1680fa9e4066Sahrens 	dsl_dir_t *dd;
16813b2aab18SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
1682088f3894Sahrens 	dsl_dataset_t *ds;
1683fa9e4066Sahrens 	zap_cursor_t zc;
1684b7661cccSmmusante 	zap_attribute_t *attr;
1685fa9e4066Sahrens 	char *child;
1686088f3894Sahrens 	uint64_t thisobj;
1687088f3894Sahrens 	int err;
1688fa9e4066Sahrens 
16893b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dp, FTAG);
16903b2aab18SMatthew Ahrens 
16913b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
16923b2aab18SMatthew Ahrens 	if (err != 0) {
16933b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
16941d452cf5Sahrens 		return (err);
16953b2aab18SMatthew Ahrens 	}
1696fa9e4066Sahrens 
1697088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1698088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
16993b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
17003b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
1701088f3894Sahrens 		return (0);
1702088f3894Sahrens 	}
1703088f3894Sahrens 
1704088f3894Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
1705b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1706fa9e4066Sahrens 
1707fa9e4066Sahrens 	/*
1708fa9e4066Sahrens 	 * Iterate over all children.
1709fa9e4066Sahrens 	 */
17100b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
1711088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
17120b69c2f0Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
1713b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
17140b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
17153b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
17163b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
17173b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
1718fa9e4066Sahrens 
1719486ae710SMatthew Ahrens 			child = kmem_asprintf("%s/%s", name, attr->za_name);
17203b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
17213b2aab18SMatthew Ahrens 			err = dmu_objset_find_impl(spa, child,
17223b2aab18SMatthew Ahrens 			    func, arg, flags);
17233b2aab18SMatthew Ahrens 			dsl_pool_config_enter(dp, FTAG);
1724486ae710SMatthew Ahrens 			strfree(child);
17253b2aab18SMatthew Ahrens 			if (err != 0)
17260b69c2f0Sahrens 				break;
17270b69c2f0Sahrens 		}
17280b69c2f0Sahrens 		zap_cursor_fini(&zc);
17291d452cf5Sahrens 
17303b2aab18SMatthew Ahrens 		if (err != 0) {
17313b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
17323b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
1733b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
17340b69c2f0Sahrens 			return (err);
17350b69c2f0Sahrens 		}
1736fa9e4066Sahrens 	}
1737fa9e4066Sahrens 
1738fa9e4066Sahrens 	/*
1739fa9e4066Sahrens 	 * Iterate over all snapshots.
1740fa9e4066Sahrens 	 */
1741088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
1742088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1743088f3894Sahrens 
1744088f3894Sahrens 		if (err == 0) {
1745088f3894Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1746088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
1747088f3894Sahrens 
1748088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1749088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
1750088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
17513b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
1752088f3894Sahrens 				    sizeof (uint64_t));
17533b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
1754088f3894Sahrens 
1755486ae710SMatthew Ahrens 				child = kmem_asprintf("%s@%s",
1756486ae710SMatthew Ahrens 				    name, attr->za_name);
17573b2aab18SMatthew Ahrens 				dsl_pool_config_exit(dp, FTAG);
17583b2aab18SMatthew Ahrens 				err = func(child, arg);
17593b2aab18SMatthew Ahrens 				dsl_pool_config_enter(dp, FTAG);
1760486ae710SMatthew Ahrens 				strfree(child);
17613b2aab18SMatthew Ahrens 				if (err != 0)
1762088f3894Sahrens 					break;
1763088f3894Sahrens 			}
1764088f3894Sahrens 			zap_cursor_fini(&zc);
1765fa9e4066Sahrens 		}
1766fa9e4066Sahrens 	}
1767fa9e4066Sahrens 
17683b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
1769b7661cccSmmusante 	kmem_free(attr, sizeof (zap_attribute_t));
17703b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dp, FTAG);
1771fa9e4066Sahrens 
17723b2aab18SMatthew Ahrens 	if (err != 0)
17731d452cf5Sahrens 		return (err);
17741d452cf5Sahrens 
17753b2aab18SMatthew Ahrens 	/* Apply to self. */
17763b2aab18SMatthew Ahrens 	return (func(name, arg));
1777fa9e4066Sahrens }
1778f18faf3fSek 
17793b2aab18SMatthew Ahrens /*
17803b2aab18SMatthew Ahrens  * See comment above dmu_objset_find_impl().
17813b2aab18SMatthew Ahrens  */
17827f73c863SRich Morris int
17833b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg,
17843b2aab18SMatthew Ahrens     int flags)
17857f73c863SRich Morris {
17863b2aab18SMatthew Ahrens 	spa_t *spa;
17873b2aab18SMatthew Ahrens 	int error;
17887f73c863SRich Morris 
17893b2aab18SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
17903b2aab18SMatthew Ahrens 	if (error != 0)
17913b2aab18SMatthew Ahrens 		return (error);
17923b2aab18SMatthew Ahrens 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
17933b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
17943b2aab18SMatthew Ahrens 	return (error);
17957f73c863SRich Morris }
17967f73c863SRich Morris 
1797f18faf3fSek void
1798f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr)
1799f18faf3fSek {
1800503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1801503ad85cSMatthew Ahrens 	os->os_user_ptr = user_ptr;
1802f18faf3fSek }
1803f18faf3fSek 
1804f18faf3fSek void *
1805f18faf3fSek dmu_objset_get_user(objset_t *os)
1806f18faf3fSek {
1807503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1808503ad85cSMatthew Ahrens 	return (os->os_user_ptr);
1809f18faf3fSek }
18103b2aab18SMatthew Ahrens 
18113b2aab18SMatthew Ahrens /*
18123b2aab18SMatthew Ahrens  * Determine name of filesystem, given name of snapshot.
18133b2aab18SMatthew Ahrens  * buf must be at least MAXNAMELEN bytes
18143b2aab18SMatthew Ahrens  */
18153b2aab18SMatthew Ahrens int
18163b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf)
18173b2aab18SMatthew Ahrens {
18183b2aab18SMatthew Ahrens 	char *atp = strchr(snapname, '@');
18193b2aab18SMatthew Ahrens 	if (atp == NULL)
1820be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
18213b2aab18SMatthew Ahrens 	if (atp - snapname >= MAXNAMELEN)
1822be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
18233b2aab18SMatthew Ahrens 	(void) strlcpy(buf, snapname, atp - snapname + 1);
18243b2aab18SMatthew Ahrens 	return (0);
18253b2aab18SMatthew Ahrens }
1826