xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision bc9014e6a81272073b9854d9f65dd59e18d18c35)
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.
26*bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
2955da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
3055da60b9SMark J Musante 
31ecd6cf80Smarks #include <sys/cred.h>
32fa9e4066Sahrens #include <sys/zfs_context.h>
33fa9e4066Sahrens #include <sys/dmu_objset.h>
34fa9e4066Sahrens #include <sys/dsl_dir.h>
35fa9e4066Sahrens #include <sys/dsl_dataset.h>
36fa9e4066Sahrens #include <sys/dsl_prop.h>
37fa9e4066Sahrens #include <sys/dsl_pool.h>
381d452cf5Sahrens #include <sys/dsl_synctask.h>
39ecd6cf80Smarks #include <sys/dsl_deleg.h>
40fa9e4066Sahrens #include <sys/dnode.h>
41fa9e4066Sahrens #include <sys/dbuf.h>
42a2eea2e1Sahrens #include <sys/zvol.h>
43fa9e4066Sahrens #include <sys/dmu_tx.h>
44fa9e4066Sahrens #include <sys/zap.h>
45fa9e4066Sahrens #include <sys/zil.h>
46fa9e4066Sahrens #include <sys/dmu_impl.h>
47ecd6cf80Smarks #include <sys/zfs_ioctl.h>
480a586ceaSMark Shellenbaum #include <sys/sa.h>
4999d5e173STim Haley #include <sys/zfs_onexit.h>
503b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
51fa9e4066Sahrens 
52744947dcSTom Erickson /*
53744947dcSTom Erickson  * Needed to close a window in dnode_move() that allows the objset to be freed
54744947dcSTom Erickson  * before it can be safely accessed.
55744947dcSTom Erickson  */
56744947dcSTom Erickson krwlock_t os_lock;
57744947dcSTom Erickson 
58744947dcSTom Erickson void
59744947dcSTom Erickson dmu_objset_init(void)
60744947dcSTom Erickson {
61744947dcSTom Erickson 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
62744947dcSTom Erickson }
63744947dcSTom Erickson 
64744947dcSTom Erickson void
65744947dcSTom Erickson dmu_objset_fini(void)
66744947dcSTom Erickson {
67744947dcSTom Erickson 	rw_destroy(&os_lock);
68744947dcSTom Erickson }
69744947dcSTom Erickson 
70fa9e4066Sahrens spa_t *
71fa9e4066Sahrens dmu_objset_spa(objset_t *os)
72fa9e4066Sahrens {
73503ad85cSMatthew Ahrens 	return (os->os_spa);
74fa9e4066Sahrens }
75fa9e4066Sahrens 
76fa9e4066Sahrens zilog_t *
77fa9e4066Sahrens dmu_objset_zil(objset_t *os)
78fa9e4066Sahrens {
79503ad85cSMatthew Ahrens 	return (os->os_zil);
80fa9e4066Sahrens }
81fa9e4066Sahrens 
82fa9e4066Sahrens dsl_pool_t *
83fa9e4066Sahrens dmu_objset_pool(objset_t *os)
84fa9e4066Sahrens {
85fa9e4066Sahrens 	dsl_dataset_t *ds;
86fa9e4066Sahrens 
87503ad85cSMatthew Ahrens 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
88fa9e4066Sahrens 		return (ds->ds_dir->dd_pool);
89fa9e4066Sahrens 	else
90503ad85cSMatthew Ahrens 		return (spa_get_dsl(os->os_spa));
91fa9e4066Sahrens }
92fa9e4066Sahrens 
93fa9e4066Sahrens dsl_dataset_t *
94fa9e4066Sahrens dmu_objset_ds(objset_t *os)
95fa9e4066Sahrens {
96503ad85cSMatthew Ahrens 	return (os->os_dsl_dataset);
97fa9e4066Sahrens }
98fa9e4066Sahrens 
99fa9e4066Sahrens dmu_objset_type_t
100fa9e4066Sahrens dmu_objset_type(objset_t *os)
101fa9e4066Sahrens {
102503ad85cSMatthew Ahrens 	return (os->os_phys->os_type);
103fa9e4066Sahrens }
104fa9e4066Sahrens 
105fa9e4066Sahrens void
106fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
107fa9e4066Sahrens {
108503ad85cSMatthew Ahrens 	dsl_dataset_name(os->os_dsl_dataset, buf);
109fa9e4066Sahrens }
110fa9e4066Sahrens 
111fa9e4066Sahrens uint64_t
112fa9e4066Sahrens dmu_objset_id(objset_t *os)
113fa9e4066Sahrens {
114503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
115fa9e4066Sahrens 
116fa9e4066Sahrens 	return (ds ? ds->ds_object : 0);
117fa9e4066Sahrens }
118fa9e4066Sahrens 
119edf345e6SMatthew Ahrens zfs_sync_type_t
12055da60b9SMark J Musante dmu_objset_syncprop(objset_t *os)
12155da60b9SMark J Musante {
12255da60b9SMark J Musante 	return (os->os_sync);
12355da60b9SMark J Musante }
12455da60b9SMark J Musante 
125edf345e6SMatthew Ahrens zfs_logbias_op_t
126e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
127e09fa4daSNeil Perrin {
128e09fa4daSNeil Perrin 	return (os->os_logbias);
129e09fa4daSNeil Perrin }
130e09fa4daSNeil Perrin 
131fa9e4066Sahrens static void
132fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
133fa9e4066Sahrens {
134503ad85cSMatthew Ahrens 	objset_t *os = arg;
135fa9e4066Sahrens 
136fa9e4066Sahrens 	/*
137fa9e4066Sahrens 	 * Inheritance should have been done by now.
138fa9e4066Sahrens 	 */
139fa9e4066Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
140fa9e4066Sahrens 
141503ad85cSMatthew Ahrens 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
142fa9e4066Sahrens }
143fa9e4066Sahrens 
144fa9e4066Sahrens static void
145fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
146fa9e4066Sahrens {
147503ad85cSMatthew Ahrens 	objset_t *os = arg;
148fa9e4066Sahrens 
149fa9e4066Sahrens 	/*
150fa9e4066Sahrens 	 * Inheritance and range checking should have been done by now.
151fa9e4066Sahrens 	 */
152fa9e4066Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
153fa9e4066Sahrens 
154503ad85cSMatthew Ahrens 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
155fa9e4066Sahrens }
156fa9e4066Sahrens 
157d0ad202dSahrens static void
158d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
159d0ad202dSahrens {
160503ad85cSMatthew Ahrens 	objset_t *os = arg;
161d0ad202dSahrens 
162d0ad202dSahrens 	/*
163d0ad202dSahrens 	 * Inheritance and range checking should have been done by now.
164d0ad202dSahrens 	 */
165d0ad202dSahrens 	ASSERT(newval > 0);
166503ad85cSMatthew Ahrens 	ASSERT(newval <= spa_max_replication(os->os_spa));
167d0ad202dSahrens 
168503ad85cSMatthew Ahrens 	os->os_copies = newval;
169d0ad202dSahrens }
170d0ad202dSahrens 
171b24ab676SJeff Bonwick static void
172b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
173b24ab676SJeff Bonwick {
174b24ab676SJeff Bonwick 	objset_t *os = arg;
175b24ab676SJeff Bonwick 	spa_t *spa = os->os_spa;
176b24ab676SJeff Bonwick 	enum zio_checksum checksum;
177b24ab676SJeff Bonwick 
178b24ab676SJeff Bonwick 	/*
179b24ab676SJeff Bonwick 	 * Inheritance should have been done by now.
180b24ab676SJeff Bonwick 	 */
181b24ab676SJeff Bonwick 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
182b24ab676SJeff Bonwick 
183b24ab676SJeff Bonwick 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
184b24ab676SJeff Bonwick 
185b24ab676SJeff Bonwick 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
186b24ab676SJeff Bonwick 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
187b24ab676SJeff Bonwick }
188b24ab676SJeff Bonwick 
1893baa08fcSek static void
1903baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval)
1913baa08fcSek {
192503ad85cSMatthew Ahrens 	objset_t *os = arg;
1933baa08fcSek 
1943baa08fcSek 	/*
1953baa08fcSek 	 * Inheritance and range checking should have been done by now.
1963baa08fcSek 	 */
1973baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1983baa08fcSek 	    newval == ZFS_CACHE_METADATA);
1993baa08fcSek 
200503ad85cSMatthew Ahrens 	os->os_primary_cache = newval;
2013baa08fcSek }
2023baa08fcSek 
2033baa08fcSek static void
2043baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval)
2053baa08fcSek {
206503ad85cSMatthew Ahrens 	objset_t *os = arg;
2073baa08fcSek 
2083baa08fcSek 	/*
2093baa08fcSek 	 * Inheritance and range checking should have been done by now.
2103baa08fcSek 	 */
2113baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2123baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2133baa08fcSek 
214503ad85cSMatthew Ahrens 	os->os_secondary_cache = newval;
2153baa08fcSek }
2163baa08fcSek 
21755da60b9SMark J Musante static void
21855da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval)
21955da60b9SMark J Musante {
22055da60b9SMark J Musante 	objset_t *os = arg;
22155da60b9SMark J Musante 
22255da60b9SMark J Musante 	/*
22355da60b9SMark J Musante 	 * Inheritance and range checking should have been done by now.
22455da60b9SMark J Musante 	 */
22555da60b9SMark J Musante 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
22655da60b9SMark J Musante 	    newval == ZFS_SYNC_DISABLED);
22755da60b9SMark J Musante 
22855da60b9SMark J Musante 	os->os_sync = newval;
22955da60b9SMark J Musante 	if (os->os_zil)
23055da60b9SMark J Musante 		zil_set_sync(os->os_zil, newval);
23155da60b9SMark J Musante }
23255da60b9SMark J Musante 
233edf345e6SMatthew Ahrens static void
234edf345e6SMatthew Ahrens redundant_metadata_changed_cb(void *arg, uint64_t newval)
235edf345e6SMatthew Ahrens {
236edf345e6SMatthew Ahrens 	objset_t *os = arg;
237edf345e6SMatthew Ahrens 
238edf345e6SMatthew Ahrens 	/*
239edf345e6SMatthew Ahrens 	 * Inheritance and range checking should have been done by now.
240edf345e6SMatthew Ahrens 	 */
241edf345e6SMatthew Ahrens 	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
242edf345e6SMatthew Ahrens 	    newval == ZFS_REDUNDANT_METADATA_MOST);
243edf345e6SMatthew Ahrens 
244edf345e6SMatthew Ahrens 	os->os_redundant_metadata = newval;
245edf345e6SMatthew Ahrens }
246edf345e6SMatthew Ahrens 
247e09fa4daSNeil Perrin static void
248e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
249e09fa4daSNeil Perrin {
250e09fa4daSNeil Perrin 	objset_t *os = arg;
251e09fa4daSNeil Perrin 
252e09fa4daSNeil Perrin 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
253e09fa4daSNeil Perrin 	    newval == ZFS_LOGBIAS_THROUGHPUT);
254e09fa4daSNeil Perrin 	os->os_logbias = newval;
255e09fa4daSNeil Perrin 	if (os->os_zil)
256e09fa4daSNeil Perrin 		zil_set_logbias(os->os_zil, newval);
257e09fa4daSNeil Perrin }
258e09fa4daSNeil Perrin 
259b5152584SMatthew Ahrens static void
260b5152584SMatthew Ahrens recordsize_changed_cb(void *arg, uint64_t newval)
261b5152584SMatthew Ahrens {
262b5152584SMatthew Ahrens 	objset_t *os = arg;
263b5152584SMatthew Ahrens 
264b5152584SMatthew Ahrens 	os->os_recordsize = newval;
265b5152584SMatthew Ahrens }
266b5152584SMatthew Ahrens 
267fa9e4066Sahrens void
268fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
269fa9e4066Sahrens {
270fa9e4066Sahrens 	objset_phys_t *osp = buf;
271fa9e4066Sahrens 
27214843421SMatthew Ahrens 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
273fa9e4066Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
274fa9e4066Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
275fa9e4066Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
27614843421SMatthew Ahrens 	osp->os_flags = BSWAP_64(osp->os_flags);
27714843421SMatthew Ahrens 	if (size == sizeof (objset_phys_t)) {
27814843421SMatthew Ahrens 		dnode_byteswap(&osp->os_userused_dnode);
27914843421SMatthew Ahrens 		dnode_byteswap(&osp->os_groupused_dnode);
28014843421SMatthew Ahrens 	}
281fa9e4066Sahrens }
282fa9e4066Sahrens 
283ea8dc4b6Seschrock int
284ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
285503ad85cSMatthew Ahrens     objset_t **osp)
286fa9e4066Sahrens {
287503ad85cSMatthew Ahrens 	objset_t *os;
288088f3894Sahrens 	int i, err;
289fa9e4066Sahrens 
29091ebeef5Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
29191ebeef5Sahrens 
292503ad85cSMatthew Ahrens 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
293503ad85cSMatthew Ahrens 	os->os_dsl_dataset = ds;
294503ad85cSMatthew Ahrens 	os->os_spa = spa;
295503ad85cSMatthew Ahrens 	os->os_rootbp = bp;
296503ad85cSMatthew Ahrens 	if (!BP_IS_HOLE(os->os_rootbp)) {
2977adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
2987802d7bfSMatthew Ahrens 		zbookmark_phys_t zb;
299b24ab676SJeff Bonwick 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
300b24ab676SJeff Bonwick 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
301b24ab676SJeff Bonwick 
302503ad85cSMatthew Ahrens 		if (DMU_OS_IS_L2CACHEABLE(os))
3037adb730bSGeorge Wilson 			aflags |= ARC_FLAG_L2CACHE;
304aad02571SSaso Kiselkov 		if (DMU_OS_IS_L2COMPRESSIBLE(os))
3057adb730bSGeorge Wilson 			aflags |= ARC_FLAG_L2COMPRESS;
306ea8dc4b6Seschrock 
307503ad85cSMatthew Ahrens 		dprintf_bp(os->os_rootbp, "reading %s", "");
3081b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, os->os_rootbp,
309503ad85cSMatthew Ahrens 		    arc_getbuf_func, &os->os_phys_buf,
31013506d1eSmaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
3113b2aab18SMatthew Ahrens 		if (err != 0) {
312503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
313b87f3af3Sperrin 			/* convert checksum errors into IO errors */
314b87f3af3Sperrin 			if (err == ECKSUM)
315be6fd75aSMatthew Ahrens 				err = SET_ERROR(EIO);
316ea8dc4b6Seschrock 			return (err);
317ea8dc4b6Seschrock 		}
31814843421SMatthew Ahrens 
31914843421SMatthew Ahrens 		/* Increase the blocksize if we are permitted. */
32014843421SMatthew Ahrens 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
321503ad85cSMatthew Ahrens 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
32214843421SMatthew Ahrens 			arc_buf_t *buf = arc_buf_alloc(spa,
323503ad85cSMatthew Ahrens 			    sizeof (objset_phys_t), &os->os_phys_buf,
32414843421SMatthew Ahrens 			    ARC_BUFC_METADATA);
32514843421SMatthew Ahrens 			bzero(buf->b_data, sizeof (objset_phys_t));
326503ad85cSMatthew Ahrens 			bcopy(os->os_phys_buf->b_data, buf->b_data,
327503ad85cSMatthew Ahrens 			    arc_buf_size(os->os_phys_buf));
328503ad85cSMatthew Ahrens 			(void) arc_buf_remove_ref(os->os_phys_buf,
329503ad85cSMatthew Ahrens 			    &os->os_phys_buf);
330503ad85cSMatthew Ahrens 			os->os_phys_buf = buf;
33114843421SMatthew Ahrens 		}
33214843421SMatthew Ahrens 
333503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
334503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
335fa9e4066Sahrens 	} else {
33614843421SMatthew Ahrens 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
33714843421SMatthew Ahrens 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
338503ad85cSMatthew Ahrens 		os->os_phys_buf = arc_buf_alloc(spa, size,
339503ad85cSMatthew Ahrens 		    &os->os_phys_buf, ARC_BUFC_METADATA);
340503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
341503ad85cSMatthew Ahrens 		bzero(os->os_phys, size);
342fa9e4066Sahrens 	}
343fa9e4066Sahrens 
344fa9e4066Sahrens 	/*
345fa9e4066Sahrens 	 * Note: the changed_cb will be called once before the register
346fa9e4066Sahrens 	 * func returns, thus changing the checksum/compression from the
3473baa08fcSek 	 * default (fletcher2/off).  Snapshots don't need to know about
3483baa08fcSek 	 * checksum/compression/copies.
349fa9e4066Sahrens 	 */
3505d7b4d43SMatthew Ahrens 	if (ds != NULL) {
3513b2aab18SMatthew Ahrens 		err = dsl_prop_register(ds,
3523b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
353503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
3543b2aab18SMatthew Ahrens 		if (err == 0) {
3553b2aab18SMatthew Ahrens 			err = dsl_prop_register(ds,
3563b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
357503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
3583b2aab18SMatthew Ahrens 		}
359*bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
3603b2aab18SMatthew Ahrens 			if (err == 0) {
3613b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3623b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
363503ad85cSMatthew Ahrens 				    checksum_changed_cb, os);
3643b2aab18SMatthew Ahrens 			}
3653b2aab18SMatthew Ahrens 			if (err == 0) {
3663b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3673b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
368503ad85cSMatthew Ahrens 				    compression_changed_cb, os);
3693b2aab18SMatthew Ahrens 			}
3703b2aab18SMatthew Ahrens 			if (err == 0) {
3713b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3723b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COPIES),
373503ad85cSMatthew Ahrens 				    copies_changed_cb, os);
3743b2aab18SMatthew Ahrens 			}
3753b2aab18SMatthew Ahrens 			if (err == 0) {
3763b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3773b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_DEDUP),
378b24ab676SJeff Bonwick 				    dedup_changed_cb, os);
3793b2aab18SMatthew Ahrens 			}
3803b2aab18SMatthew Ahrens 			if (err == 0) {
3813b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3823b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
383e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
3843b2aab18SMatthew Ahrens 			}
3853b2aab18SMatthew Ahrens 			if (err == 0) {
3863b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
3873b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_SYNC),
38855da60b9SMark J Musante 				    sync_changed_cb, os);
3893b2aab18SMatthew Ahrens 			}
390edf345e6SMatthew Ahrens 			if (err == 0) {
391edf345e6SMatthew Ahrens 				err = dsl_prop_register(ds,
392edf345e6SMatthew Ahrens 				    zfs_prop_to_name(
393edf345e6SMatthew Ahrens 				    ZFS_PROP_REDUNDANT_METADATA),
394edf345e6SMatthew Ahrens 				    redundant_metadata_changed_cb, os);
395edf345e6SMatthew Ahrens 			}
396b5152584SMatthew Ahrens 			if (err == 0) {
397b5152584SMatthew Ahrens 				err = dsl_prop_register(ds,
398b5152584SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
399b5152584SMatthew Ahrens 				    recordsize_changed_cb, os);
400b5152584SMatthew Ahrens 			}
4013baa08fcSek 		}
4023b2aab18SMatthew Ahrens 		if (err != 0) {
403503ad85cSMatthew Ahrens 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
4043b2aab18SMatthew Ahrens 			    &os->os_phys_buf));
405503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
406ea8dc4b6Seschrock 			return (err);
407ea8dc4b6Seschrock 		}
4085d7b4d43SMatthew Ahrens 	} else {
409fa9e4066Sahrens 		/* It's the meta-objset. */
410503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
411503ad85cSMatthew Ahrens 		os->os_compress = ZIO_COMPRESS_LZJB;
412503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
413b24ab676SJeff Bonwick 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
414edf345e6SMatthew Ahrens 		os->os_dedup_verify = B_FALSE;
415edf345e6SMatthew Ahrens 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
416edf345e6SMatthew Ahrens 		os->os_sync = ZFS_SYNC_STANDARD;
417503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
418503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
419fa9e4066Sahrens 	}
420fa9e4066Sahrens 
421*bc9014e6SJustin Gibbs 	if (ds == NULL || !ds->ds_is_snapshot)
4226e0cbcaaSMatthew Ahrens 		os->os_zil_header = os->os_phys->os_zil_header;
423503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
424fa9e4066Sahrens 
425fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
426503ad85cSMatthew Ahrens 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
427fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
428503ad85cSMatthew Ahrens 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
429fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
430fa9e4066Sahrens 	}
431503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
432fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
433503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
434fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
435fa9e4066Sahrens 
436503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
437503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
438503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
439503ad85cSMatthew Ahrens 
440*bc9014e6SJustin Gibbs 	dnode_special_open(os, &os->os_phys->os_meta_dnode,
441*bc9014e6SJustin Gibbs 	    DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
442503ad85cSMatthew Ahrens 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
443*bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_userused_dnode,
444*bc9014e6SJustin Gibbs 		    DMU_USERUSED_OBJECT, &os->os_userused_dnode);
445*bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_groupused_dnode,
446*bc9014e6SJustin Gibbs 		    DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
44714843421SMatthew Ahrens 	}
448fa9e4066Sahrens 
449503ad85cSMatthew Ahrens 	*osp = os;
450ea8dc4b6Seschrock 	return (0);
451fa9e4066Sahrens }
452fa9e4066Sahrens 
453503ad85cSMatthew Ahrens int
454503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4553cb34c60Sahrens {
456503ad85cSMatthew Ahrens 	int err = 0;
4573cb34c60Sahrens 
4583cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
4595d7b4d43SMatthew Ahrens 	if (ds->ds_objset == NULL) {
4605d7b4d43SMatthew Ahrens 		objset_t *os;
4613cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
4625d7b4d43SMatthew Ahrens 		    ds, dsl_dataset_get_blkptr(ds), &os);
4635d7b4d43SMatthew Ahrens 
4645d7b4d43SMatthew Ahrens 		if (err == 0) {
4655d7b4d43SMatthew Ahrens 			mutex_enter(&ds->ds_lock);
4665d7b4d43SMatthew Ahrens 			ASSERT(ds->ds_objset == NULL);
4675d7b4d43SMatthew Ahrens 			ds->ds_objset = os;
4685d7b4d43SMatthew Ahrens 			mutex_exit(&ds->ds_lock);
4695d7b4d43SMatthew Ahrens 		}
4703cb34c60Sahrens 	}
4715d7b4d43SMatthew Ahrens 	*osp = ds->ds_objset;
4723cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
473503ad85cSMatthew Ahrens 	return (err);
4743cb34c60Sahrens }
4753cb34c60Sahrens 
4763b2aab18SMatthew Ahrens /*
4773b2aab18SMatthew Ahrens  * Holds the pool while the objset is held.  Therefore only one objset
4783b2aab18SMatthew Ahrens  * can be held at a time.
4793b2aab18SMatthew Ahrens  */
4803cb34c60Sahrens int
481503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
4823cb34c60Sahrens {
4833b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
484503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
4853cb34c60Sahrens 	int err;
4863cb34c60Sahrens 
4873b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, tag, &dp);
4883b2aab18SMatthew Ahrens 	if (err != 0)
4893b2aab18SMatthew Ahrens 		return (err);
4903b2aab18SMatthew Ahrens 	err = dsl_dataset_hold(dp, name, tag, &ds);
4913b2aab18SMatthew Ahrens 	if (err != 0) {
4923b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
493503ad85cSMatthew Ahrens 		return (err);
4943b2aab18SMatthew Ahrens 	}
495503ad85cSMatthew Ahrens 
496503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
4973b2aab18SMatthew Ahrens 	if (err != 0) {
498503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
4993b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
5003b2aab18SMatthew Ahrens 	}
501503ad85cSMatthew Ahrens 
5023cb34c60Sahrens 	return (err);
5033cb34c60Sahrens }
5043cb34c60Sahrens 
5053b2aab18SMatthew Ahrens /*
5063b2aab18SMatthew Ahrens  * dsl_pool must not be held when this is called.
5073b2aab18SMatthew Ahrens  * Upon successful return, there will be a longhold on the dataset,
5083b2aab18SMatthew Ahrens  * and the dsl_pool will not be held.
5093b2aab18SMatthew Ahrens  */
510fa9e4066Sahrens int
511503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
512503ad85cSMatthew Ahrens     boolean_t readonly, void *tag, objset_t **osp)
513fa9e4066Sahrens {
5143b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
515f18faf3fSek 	dsl_dataset_t *ds;
516f18faf3fSek 	int err;
517fa9e4066Sahrens 
5183b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
5193b2aab18SMatthew Ahrens 	if (err != 0)
5203b2aab18SMatthew Ahrens 		return (err);
5213b2aab18SMatthew Ahrens 	err = dsl_dataset_own(dp, name, tag, &ds);
5223b2aab18SMatthew Ahrens 	if (err != 0) {
5233b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
524fa9e4066Sahrens 		return (err);
5253b2aab18SMatthew Ahrens 	}
526fa9e4066Sahrens 
527503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
5283b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
5293b2aab18SMatthew Ahrens 	if (err != 0) {
530503ad85cSMatthew Ahrens 		dsl_dataset_disown(ds, tag);
531681d9761SEric Taylor 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
5323b2aab18SMatthew Ahrens 		dsl_dataset_disown(ds, tag);
533be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
534*bc9014e6SJustin Gibbs 	} else if (!readonly && ds->ds_is_snapshot) {
5353b2aab18SMatthew Ahrens 		dsl_dataset_disown(ds, tag);
536be6fd75aSMatthew Ahrens 		return (SET_ERROR(EROFS));
537fa9e4066Sahrens 	}
5383cb34c60Sahrens 	return (err);
539fa9e4066Sahrens }
540fa9e4066Sahrens 
541fa9e4066Sahrens void
542503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
543fa9e4066Sahrens {
5443b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(os);
545503ad85cSMatthew Ahrens 	dsl_dataset_rele(os->os_dsl_dataset, tag);
5463b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, tag);
547503ad85cSMatthew Ahrens }
548503ad85cSMatthew Ahrens 
54991948b51SKeith M Wesolowski /*
55091948b51SKeith M Wesolowski  * When we are called, os MUST refer to an objset associated with a dataset
55191948b51SKeith M Wesolowski  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
55291948b51SKeith M Wesolowski  * == tag.  We will then release and reacquire ownership of the dataset while
55391948b51SKeith M Wesolowski  * holding the pool config_rwlock to avoid intervening namespace or ownership
55491948b51SKeith M Wesolowski  * changes may occur.
55591948b51SKeith M Wesolowski  *
55691948b51SKeith M Wesolowski  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
55791948b51SKeith M Wesolowski  * release the hold on its dataset and acquire a new one on the dataset of the
55891948b51SKeith M Wesolowski  * same name so that it can be partially torn down and reconstructed.
55991948b51SKeith M Wesolowski  */
56091948b51SKeith M Wesolowski void
56191948b51SKeith M Wesolowski dmu_objset_refresh_ownership(objset_t *os, void *tag)
56291948b51SKeith M Wesolowski {
56391948b51SKeith M Wesolowski 	dsl_pool_t *dp;
56491948b51SKeith M Wesolowski 	dsl_dataset_t *ds, *newds;
56591948b51SKeith M Wesolowski 	char name[MAXNAMELEN];
56691948b51SKeith M Wesolowski 
56791948b51SKeith M Wesolowski 	ds = os->os_dsl_dataset;
56891948b51SKeith M Wesolowski 	VERIFY3P(ds, !=, NULL);
56991948b51SKeith M Wesolowski 	VERIFY3P(ds->ds_owner, ==, tag);
57091948b51SKeith M Wesolowski 	VERIFY(dsl_dataset_long_held(ds));
57191948b51SKeith M Wesolowski 
57291948b51SKeith M Wesolowski 	dsl_dataset_name(ds, name);
57391948b51SKeith M Wesolowski 	dp = dmu_objset_pool(os);
57491948b51SKeith M Wesolowski 	dsl_pool_config_enter(dp, FTAG);
57591948b51SKeith M Wesolowski 	dmu_objset_disown(os, tag);
57691948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
57791948b51SKeith M Wesolowski 	VERIFY3P(newds, ==, os->os_dsl_dataset);
57891948b51SKeith M Wesolowski 	dsl_pool_config_exit(dp, FTAG);
57991948b51SKeith M Wesolowski }
58091948b51SKeith M Wesolowski 
581503ad85cSMatthew Ahrens void
582503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
583503ad85cSMatthew Ahrens {
584503ad85cSMatthew Ahrens 	dsl_dataset_disown(os->os_dsl_dataset, tag);
585fa9e4066Sahrens }
586fa9e4066Sahrens 
5873b2aab18SMatthew Ahrens void
5881934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
589ea8dc4b6Seschrock {
590*bc9014e6SJustin Gibbs 	dnode_t dn_marker;
591ea8dc4b6Seschrock 	dnode_t *dn;
592c543ec06Sahrens 
593503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
594*bc9014e6SJustin Gibbs 	dn = list_head(&os->os_dnodes);
595*bc9014e6SJustin Gibbs 	while (dn != NULL) {
596*bc9014e6SJustin Gibbs 		/*
597*bc9014e6SJustin Gibbs 		 * Skip dnodes without holds.  We have to do this dance
598*bc9014e6SJustin Gibbs 		 * because dnode_add_ref() only works if there is already a
599*bc9014e6SJustin Gibbs 		 * hold.  If the dnode has no holds, then it has no dbufs.
600*bc9014e6SJustin Gibbs 		 */
601*bc9014e6SJustin Gibbs 		if (dnode_add_ref(dn, FTAG)) {
602*bc9014e6SJustin Gibbs 			list_insert_after(&os->os_dnodes, dn, &dn_marker);
603*bc9014e6SJustin Gibbs 			mutex_exit(&os->os_lock);
604c543ec06Sahrens 
605*bc9014e6SJustin Gibbs 			dnode_evict_dbufs(dn);
606*bc9014e6SJustin Gibbs 			dnode_rele(dn, FTAG);
607c543ec06Sahrens 
608*bc9014e6SJustin Gibbs 			mutex_enter(&os->os_lock);
609*bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, &dn_marker);
610*bc9014e6SJustin Gibbs 			list_remove(&os->os_dnodes, &dn_marker);
611*bc9014e6SJustin Gibbs 		} else {
612*bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, dn);
613*bc9014e6SJustin Gibbs 		}
614ea8dc4b6Seschrock 	}
615503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
616*bc9014e6SJustin Gibbs 
617*bc9014e6SJustin Gibbs 	if (DMU_USERUSED_DNODE(os) != NULL) {
618*bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
619*bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
620*bc9014e6SJustin Gibbs 	}
621*bc9014e6SJustin Gibbs 	dnode_evict_dbufs(DMU_META_DNODE(os));
622ea8dc4b6Seschrock }
623ea8dc4b6Seschrock 
624*bc9014e6SJustin Gibbs /*
625*bc9014e6SJustin Gibbs  * Objset eviction processing is split into into two pieces.
626*bc9014e6SJustin Gibbs  * The first marks the objset as evicting, evicts any dbufs that
627*bc9014e6SJustin Gibbs  * have a refcount of zero, and then queues up the objset for the
628*bc9014e6SJustin Gibbs  * second phase of eviction.  Once os->os_dnodes has been cleared by
629*bc9014e6SJustin Gibbs  * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
630*bc9014e6SJustin Gibbs  * The second phase closes the special dnodes, dequeues the objset from
631*bc9014e6SJustin Gibbs  * the list of those undergoing eviction, and finally frees the objset.
632*bc9014e6SJustin Gibbs  *
633*bc9014e6SJustin Gibbs  * NOTE: Due to asynchronous eviction processing (invocation of
634*bc9014e6SJustin Gibbs  *       dnode_buf_pageout()), it is possible for the meta dnode for the
635*bc9014e6SJustin Gibbs  *       objset to have no holds even though os->os_dnodes is not empty.
636*bc9014e6SJustin Gibbs  */
637fa9e4066Sahrens void
638503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
639fa9e4066Sahrens {
640503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
641fa9e4066Sahrens 
642b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
643b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, t));
644fa9e4066Sahrens 
6453baa08fcSek 	if (ds) {
646*bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
6473b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6483b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
649503ad85cSMatthew Ahrens 			    checksum_changed_cb, os));
6503b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6513b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
652503ad85cSMatthew Ahrens 			    compression_changed_cb, os));
6533b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6543b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_COPIES),
655503ad85cSMatthew Ahrens 			    copies_changed_cb, os));
6563b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6573b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_DEDUP),
658b24ab676SJeff Bonwick 			    dedup_changed_cb, os));
6593b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6603b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
661e09fa4daSNeil Perrin 			    logbias_changed_cb, os));
6623b2aab18SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
6633b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SYNC),
66455da60b9SMark J Musante 			    sync_changed_cb, os));
665edf345e6SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
666edf345e6SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA),
667edf345e6SMatthew Ahrens 			    redundant_metadata_changed_cb, os));
668b5152584SMatthew Ahrens 			VERIFY0(dsl_prop_unregister(ds,
669b5152584SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
670b5152584SMatthew Ahrens 			    recordsize_changed_cb, os));
6713baa08fcSek 		}
6723b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_unregister(ds,
6733b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
674503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os));
6753b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_unregister(ds,
6763b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
677503ad85cSMatthew Ahrens 		    secondary_cache_changed_cb, os));
678fa9e4066Sahrens 	}
679fa9e4066Sahrens 
6800a586ceaSMark Shellenbaum 	if (os->os_sa)
6810a586ceaSMark Shellenbaum 		sa_tear_down(os);
6820a586ceaSMark Shellenbaum 
683*bc9014e6SJustin Gibbs 	os->os_evicting = B_TRUE;
6843b2aab18SMatthew Ahrens 	dmu_objset_evict_dbufs(os);
685ea8dc4b6Seschrock 
686*bc9014e6SJustin Gibbs 	mutex_enter(&os->os_lock);
687*bc9014e6SJustin Gibbs 	spa_evicting_os_register(os->os_spa, os);
688*bc9014e6SJustin Gibbs 	if (list_is_empty(&os->os_dnodes)) {
689*bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
690*bc9014e6SJustin Gibbs 		dmu_objset_evict_done(os);
691*bc9014e6SJustin Gibbs 	} else {
692*bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
693*bc9014e6SJustin Gibbs 	}
694*bc9014e6SJustin Gibbs }
695*bc9014e6SJustin Gibbs 
696*bc9014e6SJustin Gibbs void
697*bc9014e6SJustin Gibbs dmu_objset_evict_done(objset_t *os)
698*bc9014e6SJustin Gibbs {
699*bc9014e6SJustin Gibbs 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
700*bc9014e6SJustin Gibbs 
701744947dcSTom Erickson 	dnode_special_close(&os->os_meta_dnode);
702744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os)) {
703744947dcSTom Erickson 		dnode_special_close(&os->os_userused_dnode);
704744947dcSTom Erickson 		dnode_special_close(&os->os_groupused_dnode);
70514843421SMatthew Ahrens 	}
706503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
707fa9e4066Sahrens 
7083b2aab18SMatthew Ahrens 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
709744947dcSTom Erickson 
710744947dcSTom Erickson 	/*
711744947dcSTom Erickson 	 * This is a barrier to prevent the objset from going away in
712744947dcSTom Erickson 	 * dnode_move() until we can safely ensure that the objset is still in
713744947dcSTom Erickson 	 * use. We consider the objset valid before the barrier and invalid
714744947dcSTom Erickson 	 * after the barrier.
715744947dcSTom Erickson 	 */
716744947dcSTom Erickson 	rw_enter(&os_lock, RW_READER);
717744947dcSTom Erickson 	rw_exit(&os_lock);
718744947dcSTom Erickson 
719503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
720503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
721503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
722*bc9014e6SJustin Gibbs 	spa_evicting_os_deregister(os->os_spa, os);
723503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
724fa9e4066Sahrens }
725fa9e4066Sahrens 
72671eb0538SChris Kirby timestruc_t
72771eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
72871eb0538SChris Kirby {
72971eb0538SChris Kirby 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
73071eb0538SChris Kirby }
73171eb0538SChris Kirby 
732fa9e4066Sahrens /* called from dsl for meta-objset */
733503ad85cSMatthew Ahrens objset_t *
734c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
735c717a561Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
736fa9e4066Sahrens {
737503ad85cSMatthew Ahrens 	objset_t *os;
738fa9e4066Sahrens 	dnode_t *mdn;
739fa9e4066Sahrens 
740fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7413b2aab18SMatthew Ahrens 
742feaa74e4SMark Maybee 	if (ds != NULL)
7433b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_from_ds(ds, &os));
744feaa74e4SMark Maybee 	else
7453b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
746feaa74e4SMark Maybee 
747744947dcSTom Erickson 	mdn = DMU_META_DNODE(os);
748fa9e4066Sahrens 
749fa9e4066Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
750fa9e4066Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
751fa9e4066Sahrens 
752fa9e4066Sahrens 	/*
753fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
754fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
755fa9e4066Sahrens 	 * we are also accessing it in open context.
756fa9e4066Sahrens 	 *
757fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
758fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
759fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
760fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
761fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
762fa9e4066Sahrens 	 */
763ea8dc4b6Seschrock 	if (ds != NULL) {
764ea8dc4b6Seschrock 		int levels = 1;
765ea8dc4b6Seschrock 
766ea8dc4b6Seschrock 		/*
767ea8dc4b6Seschrock 		 * Determine the number of levels necessary for the meta-dnode
768ea8dc4b6Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
769ea8dc4b6Seschrock 		 */
770ea8dc4b6Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
771ea8dc4b6Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
772ea8dc4b6Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
773ea8dc4b6Seschrock 			levels++;
774ea8dc4b6Seschrock 
775fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
776ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
777ea8dc4b6Seschrock 	}
778fa9e4066Sahrens 
779fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
780fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
781fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
782503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
783503ad85cSMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
784503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
785503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
78614843421SMatthew Ahrens 	}
787fa9e4066Sahrens 
788fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
789fa9e4066Sahrens 
790503ad85cSMatthew Ahrens 	return (os);
791fa9e4066Sahrens }
792fa9e4066Sahrens 
7933b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg {
7943b2aab18SMatthew Ahrens 	const char *doca_name;
7953b2aab18SMatthew Ahrens 	cred_t *doca_cred;
7963b2aab18SMatthew Ahrens 	void (*doca_userfunc)(objset_t *os, void *arg,
7973b2aab18SMatthew Ahrens 	    cred_t *cr, dmu_tx_t *tx);
7983b2aab18SMatthew Ahrens 	void *doca_userarg;
7993b2aab18SMatthew Ahrens 	dmu_objset_type_t doca_type;
8003b2aab18SMatthew Ahrens 	uint64_t doca_flags;
8013b2aab18SMatthew Ahrens } dmu_objset_create_arg_t;
802fa9e4066Sahrens 
803ecd6cf80Smarks /*ARGSUSED*/
804fa9e4066Sahrens static int
8053b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx)
806fa9e4066Sahrens {
8073b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
8083b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8093b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
8103b2aab18SMatthew Ahrens 	const char *tail;
8113b2aab18SMatthew Ahrens 	int error;
8121d452cf5Sahrens 
8133b2aab18SMatthew Ahrens 	if (strchr(doca->doca_name, '@') != NULL)
814be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8151d452cf5Sahrens 
8163b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
8173b2aab18SMatthew Ahrens 	if (error != 0)
8183b2aab18SMatthew Ahrens 		return (error);
8193b2aab18SMatthew Ahrens 	if (tail == NULL) {
8203b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
821be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
8221d452cf5Sahrens 	}
823a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
824a2afb611SJerry Jelinek 	    doca->doca_cred);
8253b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
826ecd6cf80Smarks 
827a2afb611SJerry Jelinek 	return (error);
8281d452cf5Sahrens }
8291d452cf5Sahrens 
8301d452cf5Sahrens static void
8313b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
8321d452cf5Sahrens {
8333b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
8343b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8353b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
8363b2aab18SMatthew Ahrens 	const char *tail;
8374445fffbSMatthew Ahrens 	dsl_dataset_t *ds;
8383b2aab18SMatthew Ahrens 	uint64_t obj;
8394445fffbSMatthew Ahrens 	blkptr_t *bp;
8403b2aab18SMatthew Ahrens 	objset_t *os;
841fa9e4066Sahrens 
8423b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
843fa9e4066Sahrens 
8443b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
8453b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
846fa9e4066Sahrens 
8473b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
8484445fffbSMatthew Ahrens 	bp = dsl_dataset_get_blkptr(ds);
8493b2aab18SMatthew Ahrens 	os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
8503b2aab18SMatthew Ahrens 	    ds, bp, doca->doca_type, tx);
851fa9e4066Sahrens 
8523b2aab18SMatthew Ahrens 	if (doca->doca_userfunc != NULL) {
8533b2aab18SMatthew Ahrens 		doca->doca_userfunc(os, doca->doca_userarg,
8543b2aab18SMatthew Ahrens 		    doca->doca_cred, tx);
855fa9e4066Sahrens 	}
856ecd6cf80Smarks 
8573b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "create", tx, "");
8584445fffbSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
8593b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
860fa9e4066Sahrens }
861fa9e4066Sahrens 
862fa9e4066Sahrens int
863ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
864ecd6cf80Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
865fa9e4066Sahrens {
8663b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t doca;
867fa9e4066Sahrens 
8683b2aab18SMatthew Ahrens 	doca.doca_name = name;
8693b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
8703b2aab18SMatthew Ahrens 	doca.doca_flags = flags;
8713b2aab18SMatthew Ahrens 	doca.doca_userfunc = func;
8723b2aab18SMatthew Ahrens 	doca.doca_userarg = arg;
8733b2aab18SMatthew Ahrens 	doca.doca_type = type;
874ecd6cf80Smarks 
8753b2aab18SMatthew Ahrens 	return (dsl_sync_task(name,
8767d46dc6cSMatthew Ahrens 	    dmu_objset_create_check, dmu_objset_create_sync, &doca,
8777d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
878ae46e4c7SMatthew Ahrens }
879ae46e4c7SMatthew Ahrens 
8803b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg {
8813b2aab18SMatthew Ahrens 	const char *doca_clone;
8823b2aab18SMatthew Ahrens 	const char *doca_origin;
8833b2aab18SMatthew Ahrens 	cred_t *doca_cred;
8843b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t;
8853b2aab18SMatthew Ahrens 
8863b2aab18SMatthew Ahrens /*ARGSUSED*/
8873b2aab18SMatthew Ahrens static int
8883b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
889ae46e4c7SMatthew Ahrens {
8903b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
891ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
892ae46e4c7SMatthew Ahrens 	const char *tail;
8933b2aab18SMatthew Ahrens 	int error;
8943b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
8953b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
896ae46e4c7SMatthew Ahrens 
8973b2aab18SMatthew Ahrens 	if (strchr(doca->doca_clone, '@') != NULL)
898be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8993b2aab18SMatthew Ahrens 
9003b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
9013b2aab18SMatthew Ahrens 	if (error != 0)
9023b2aab18SMatthew Ahrens 		return (error);
903ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
9043b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
905be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
906fa9e4066Sahrens 	}
9073b2aab18SMatthew Ahrens 	/* You can't clone across pools. */
9083b2aab18SMatthew Ahrens 	if (pdd->dd_pool != dp) {
9093b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
910be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
911fa9e4066Sahrens 	}
912a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
913a2afb611SJerry Jelinek 	    doca->doca_cred);
914a2afb611SJerry Jelinek 	if (error != 0) {
915a2afb611SJerry Jelinek 		dsl_dir_rele(pdd, FTAG);
916a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
917a2afb611SJerry Jelinek 	}
9183b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
919fa9e4066Sahrens 
9203b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
9213b2aab18SMatthew Ahrens 	if (error != 0)
92299d5e173STim Haley 		return (error);
92399d5e173STim Haley 
9243b2aab18SMatthew Ahrens 	/* You can't clone across pools. */
9253b2aab18SMatthew Ahrens 	if (origin->ds_dir->dd_pool != dp) {
9263b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
927be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
92899d5e173STim Haley 	}
929ea2f5b9eSMatthew Ahrens 
9303b2aab18SMatthew Ahrens 	/* You can only clone snapshots, not the head datasets. */
931*bc9014e6SJustin Gibbs 	if (!origin->ds_is_snapshot) {
9323b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
933be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
93492241e0bSTom Erickson 	}
9353b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
93699d5e173STim Haley 
9373b2aab18SMatthew Ahrens 	return (0);
938ea2f5b9eSMatthew Ahrens }
9391d452cf5Sahrens 
9403b2aab18SMatthew Ahrens static void
9413b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
9421d452cf5Sahrens {
9433b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
9443b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
9453b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
9463b2aab18SMatthew Ahrens 	const char *tail;
9473b2aab18SMatthew Ahrens 	dsl_dataset_t *origin, *ds;
9483b2aab18SMatthew Ahrens 	uint64_t obj;
9493b2aab18SMatthew Ahrens 	char namebuf[MAXNAMELEN];
9504445fffbSMatthew Ahrens 
9513b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
9523b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
9534445fffbSMatthew Ahrens 
9543b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
9553b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
956f2e10be3Srm 
9573b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
9583b2aab18SMatthew Ahrens 	dsl_dataset_name(origin, namebuf);
9593b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "clone", tx,
9603b2aab18SMatthew Ahrens 	    "origin=%s (%llu)", namebuf, origin->ds_object);
9613b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
9623b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
9633b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
9641d452cf5Sahrens }
9651d452cf5Sahrens 
9661d452cf5Sahrens int
9673b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin)
9681d452cf5Sahrens {
9693b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t doca;
9704445fffbSMatthew Ahrens 
9713b2aab18SMatthew Ahrens 	doca.doca_clone = clone;
9723b2aab18SMatthew Ahrens 	doca.doca_origin = origin;
9733b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
97499d5e173STim Haley 
9753b2aab18SMatthew Ahrens 	return (dsl_sync_task(clone,
9767d46dc6cSMatthew Ahrens 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
9777d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
9784445fffbSMatthew Ahrens }
9794445fffbSMatthew Ahrens 
9804445fffbSMatthew Ahrens int
9814445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname)
9824445fffbSMatthew Ahrens {
9834445fffbSMatthew Ahrens 	int err;
9844445fffbSMatthew Ahrens 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
9854445fffbSMatthew Ahrens 	nvlist_t *snaps = fnvlist_alloc();
9864445fffbSMatthew Ahrens 
9874445fffbSMatthew Ahrens 	fnvlist_add_boolean(snaps, longsnap);
9884445fffbSMatthew Ahrens 	strfree(longsnap);
9893b2aab18SMatthew Ahrens 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
9903b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
9914445fffbSMatthew Ahrens 	return (err);
9924445fffbSMatthew Ahrens }
9934445fffbSMatthew Ahrens 
994fa9e4066Sahrens static void
99514843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
996fa9e4066Sahrens {
997c717a561Smaybee 	dnode_t *dn;
998faafa6e3Sahrens 
999c717a561Smaybee 	while (dn = list_head(list)) {
1000c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1001c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
1002c717a561Smaybee 		/*
100314843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
100414843421SMatthew Ahrens 		 * meta-dnode needs to set it ouside dnode_sync().
1005c717a561Smaybee 		 */
1006c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1007c717a561Smaybee 		ASSERT(dn->dn_zio);
1008faafa6e3Sahrens 
1009c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1010c717a561Smaybee 		list_remove(list, dn);
101114843421SMatthew Ahrens 
101214843421SMatthew Ahrens 		if (newlist) {
101314843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
101414843421SMatthew Ahrens 			list_insert_tail(newlist, dn);
101514843421SMatthew Ahrens 		}
101614843421SMatthew Ahrens 
1017c717a561Smaybee 		dnode_sync(dn, tx);
1018fa9e4066Sahrens 	}
1019fa9e4066Sahrens }
1020fa9e4066Sahrens 
1021fa9e4066Sahrens /* ARGSUSED */
1022fa9e4066Sahrens static void
1023b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1024fa9e4066Sahrens {
1025e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1026503ad85cSMatthew Ahrens 	objset_t *os = arg;
1027c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1028fa9e4066Sahrens 
10295d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp));
10303b2aab18SMatthew Ahrens 	ASSERT3P(bp, ==, os->os_rootbp);
10313b2aab18SMatthew Ahrens 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
10323b2aab18SMatthew Ahrens 	ASSERT0(BP_GET_LEVEL(bp));
10330a4e9518Sgw 
1034fa9e4066Sahrens 	/*
103514843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
103614843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
103714843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
103814843421SMatthew Ahrens 	 * dnode and user/group accounting objects).
1039fa9e4066Sahrens 	 */
104014843421SMatthew Ahrens 	bp->blk_fill = 0;
1041e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
10425d7b4d43SMatthew Ahrens 		bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1043b24ab676SJeff Bonwick }
1044b24ab676SJeff Bonwick 
1045b24ab676SJeff Bonwick /* ARGSUSED */
1046b24ab676SJeff Bonwick static void
1047b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1048b24ab676SJeff Bonwick {
1049b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1050b24ab676SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
1051b24ab676SJeff Bonwick 	objset_t *os = arg;
10520a4e9518Sgw 
1053e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1054b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, bp_orig));
1055e14bb325SJeff Bonwick 	} else {
1056b24ab676SJeff Bonwick 		dsl_dataset_t *ds = os->os_dsl_dataset;
1057b24ab676SJeff Bonwick 		dmu_tx_t *tx = os->os_synctx;
1058b24ab676SJeff Bonwick 
1059b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1060b24ab676SJeff Bonwick 		dsl_dataset_block_born(ds, bp, tx);
10610a4e9518Sgw 	}
1062c717a561Smaybee }
1063c717a561Smaybee 
1064fa9e4066Sahrens /* called from dsl */
1065fa9e4066Sahrens void
1066503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1067fa9e4066Sahrens {
1068fa9e4066Sahrens 	int txgoff;
10697802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
1070b24ab676SJeff Bonwick 	zio_prop_t zp;
1071c717a561Smaybee 	zio_t *zio;
1072c717a561Smaybee 	list_t *list;
107314843421SMatthew Ahrens 	list_t *newlist = NULL;
1074c717a561Smaybee 	dbuf_dirty_record_t *dr;
1075c717a561Smaybee 
1076c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1077fa9e4066Sahrens 
1078fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1079fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
1080fa9e4066Sahrens 	os->os_synctx = tx;
1081fa9e4066Sahrens 
108287bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
108387bd5c1eSahrens 		/*
108487bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
108587bd5c1eSahrens 		 * spa_max_replication() could change, so reset
108687bd5c1eSahrens 		 * os_copies here.
108787bd5c1eSahrens 		 */
108887bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
108987bd5c1eSahrens 	}
109087bd5c1eSahrens 
1091fa9e4066Sahrens 	/*
1092c717a561Smaybee 	 * Create the root block IO
1093fa9e4066Sahrens 	 */
1094b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1095b24ab676SJeff Bonwick 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1096b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
10971b912ec7SGeorge Wilson 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1098b24ab676SJeff Bonwick 
1099b24ab676SJeff Bonwick 	dmu_write_policy(os, NULL, 0, 0, &zp);
1100b24ab676SJeff Bonwick 
1101b24ab676SJeff Bonwick 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1102aad02571SSaso Kiselkov 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1103aad02571SSaso Kiselkov 	    DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
110469962b56SMatthew Ahrens 	    NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1105aad02571SSaso Kiselkov 	    ZIO_FLAG_MUSTSUCCEED, &zb);
1106c717a561Smaybee 
1107c717a561Smaybee 	/*
110814843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
1109c717a561Smaybee 	 */
1110744947dcSTom Erickson 	DMU_META_DNODE(os)->dn_zio = zio;
1111744947dcSTom Erickson 	dnode_sync(DMU_META_DNODE(os), tx);
1112c717a561Smaybee 
111314843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
111414843421SMatthew Ahrens 
1115744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) &&
1116744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1117744947dcSTom Erickson 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1118744947dcSTom Erickson 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1119744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1120744947dcSTom Erickson 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
112114843421SMatthew Ahrens 	}
112214843421SMatthew Ahrens 
1123c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
1124fa9e4066Sahrens 
112514843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
112614843421SMatthew Ahrens 		newlist = &os->os_synced_dnodes;
112714843421SMatthew Ahrens 		/*
112814843421SMatthew Ahrens 		 * We must create the list here because it uses the
112914843421SMatthew Ahrens 		 * dn_dirty_link[] of this txg.
113014843421SMatthew Ahrens 		 */
113114843421SMatthew Ahrens 		list_create(newlist, sizeof (dnode_t),
113214843421SMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
113314843421SMatthew Ahrens 	}
113414843421SMatthew Ahrens 
113514843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
113614843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1137fa9e4066Sahrens 
1138744947dcSTom Erickson 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1139c717a561Smaybee 	while (dr = list_head(list)) {
11403b2aab18SMatthew Ahrens 		ASSERT0(dr->dr_dbuf->db_level);
1141c717a561Smaybee 		list_remove(list, dr);
1142c717a561Smaybee 		if (dr->dr_zio)
1143c717a561Smaybee 			zio_nowait(dr->dr_zio);
1144c717a561Smaybee 	}
1145c717a561Smaybee 	/*
1146c717a561Smaybee 	 * Free intent log blocks up to this tx.
1147c717a561Smaybee 	 */
1148c717a561Smaybee 	zil_sync(os->os_zil, tx);
1149088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
1150c717a561Smaybee 	zio_nowait(zio);
1151fa9e4066Sahrens }
1152fa9e4066Sahrens 
1153b24ab676SJeff Bonwick boolean_t
1154b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1155b24ab676SJeff Bonwick {
1156b24ab676SJeff Bonwick 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1157b24ab676SJeff Bonwick 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1158b24ab676SJeff Bonwick }
1159b24ab676SJeff Bonwick 
1160744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
116114843421SMatthew Ahrens 
116214843421SMatthew Ahrens void
116314843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
116414843421SMatthew Ahrens {
116514843421SMatthew Ahrens 	used_cbs[ost] = cb;
116614843421SMatthew Ahrens }
116714843421SMatthew Ahrens 
116814843421SMatthew Ahrens boolean_t
1169503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
117014843421SMatthew Ahrens {
117114843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1172744947dcSTom Erickson 	    used_cbs[os->os_phys->os_type] != NULL &&
1173744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os) != NULL);
117414843421SMatthew Ahrens }
117514843421SMatthew Ahrens 
11769966ca11SMatthew Ahrens static void
11770a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
11780a586ceaSMark Shellenbaum     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
11799966ca11SMatthew Ahrens {
11800a586ceaSMark Shellenbaum 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
11810a586ceaSMark Shellenbaum 		int64_t delta = DNODE_SIZE + used;
11829966ca11SMatthew Ahrens 		if (subtract)
11839966ca11SMatthew Ahrens 			delta = -delta;
1184b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
11859966ca11SMatthew Ahrens 		    user, delta, tx));
1186b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
11879966ca11SMatthew Ahrens 		    group, delta, tx));
11889966ca11SMatthew Ahrens 	}
11899966ca11SMatthew Ahrens }
11909966ca11SMatthew Ahrens 
119114843421SMatthew Ahrens void
11920a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
119314843421SMatthew Ahrens {
119414843421SMatthew Ahrens 	dnode_t *dn;
119514843421SMatthew Ahrens 	list_t *list = &os->os_synced_dnodes;
119614843421SMatthew Ahrens 
119714843421SMatthew Ahrens 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
119814843421SMatthew Ahrens 
119914843421SMatthew Ahrens 	while (dn = list_head(list)) {
12001d8ccc7bSMark Shellenbaum 		int flags;
120114843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
120214843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
120314843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
120414843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
120514843421SMatthew Ahrens 
120614843421SMatthew Ahrens 		/* Allocate the user/groupused objects if necessary. */
1207744947dcSTom Erickson 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1208503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
120914843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT,
121014843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1211503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
121214843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT,
121314843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
121414843421SMatthew Ahrens 		}
121514843421SMatthew Ahrens 
121614843421SMatthew Ahrens 		/*
12179966ca11SMatthew Ahrens 		 * We intentionally modify the zap object even if the
12180a586ceaSMark Shellenbaum 		 * net delta is zero.  Otherwise
12199966ca11SMatthew Ahrens 		 * the block of the zap obj could be shared between
12209966ca11SMatthew Ahrens 		 * datasets but need to be different between them after
12219966ca11SMatthew Ahrens 		 * a bprewrite.
122214843421SMatthew Ahrens 		 */
122314843421SMatthew Ahrens 
12241d8ccc7bSMark Shellenbaum 		flags = dn->dn_id_flags;
12251d8ccc7bSMark Shellenbaum 		ASSERT(flags);
12261d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST)  {
12270a586ceaSMark Shellenbaum 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
12280a586ceaSMark Shellenbaum 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
12290a586ceaSMark Shellenbaum 		}
12301d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_NEW_EXIST) {
12310a586ceaSMark Shellenbaum 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
12320a586ceaSMark Shellenbaum 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
12330a586ceaSMark Shellenbaum 			    dn->dn_newgid, B_FALSE, tx);
12340a586ceaSMark Shellenbaum 		}
12350a586ceaSMark Shellenbaum 
12361d8ccc7bSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
12370a586ceaSMark Shellenbaum 		dn->dn_oldused = 0;
12380a586ceaSMark Shellenbaum 		dn->dn_oldflags = 0;
12390a586ceaSMark Shellenbaum 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
12400a586ceaSMark Shellenbaum 			dn->dn_olduid = dn->dn_newuid;
12410a586ceaSMark Shellenbaum 			dn->dn_oldgid = dn->dn_newgid;
12420a586ceaSMark Shellenbaum 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
12430a586ceaSMark Shellenbaum 			if (dn->dn_bonuslen == 0)
12440a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
12450a586ceaSMark Shellenbaum 			else
12460a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
12470a586ceaSMark Shellenbaum 		}
124828d97a71SMark Shellenbaum 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
124914843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
125014843421SMatthew Ahrens 
125114843421SMatthew Ahrens 		list_remove(list, dn);
125214843421SMatthew Ahrens 		dnode_rele(dn, list);
125314843421SMatthew Ahrens 	}
125414843421SMatthew Ahrens }
125514843421SMatthew Ahrens 
125606e0070dSMark Shellenbaum /*
125706e0070dSMark Shellenbaum  * Returns a pointer to data to find uid/gid from
125806e0070dSMark Shellenbaum  *
125906e0070dSMark Shellenbaum  * If a dirty record for transaction group that is syncing can't
126006e0070dSMark Shellenbaum  * be found then NULL is returned.  In the NULL case it is assumed
126106e0070dSMark Shellenbaum  * the uid/gid aren't changing.
126206e0070dSMark Shellenbaum  */
126306e0070dSMark Shellenbaum static void *
126406e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
126506e0070dSMark Shellenbaum {
126606e0070dSMark Shellenbaum 	dbuf_dirty_record_t *dr, **drp;
126706e0070dSMark Shellenbaum 	void *data;
126806e0070dSMark Shellenbaum 
126906e0070dSMark Shellenbaum 	if (db->db_dirtycnt == 0)
127006e0070dSMark Shellenbaum 		return (db->db.db_data);  /* Nothing is changing */
127106e0070dSMark Shellenbaum 
127206e0070dSMark Shellenbaum 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
127306e0070dSMark Shellenbaum 		if (dr->dr_txg == tx->tx_txg)
127406e0070dSMark Shellenbaum 			break;
127506e0070dSMark Shellenbaum 
1276744947dcSTom Erickson 	if (dr == NULL) {
127706e0070dSMark Shellenbaum 		data = NULL;
1278744947dcSTom Erickson 	} else {
1279744947dcSTom Erickson 		dnode_t *dn;
1280744947dcSTom Erickson 
1281744947dcSTom Erickson 		DB_DNODE_ENTER(dr->dr_dbuf);
1282744947dcSTom Erickson 		dn = DB_DNODE(dr->dr_dbuf);
1283744947dcSTom Erickson 
1284744947dcSTom Erickson 		if (dn->dn_bonuslen == 0 &&
1285744947dcSTom Erickson 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1286744947dcSTom Erickson 			data = dr->dt.dl.dr_data->b_data;
1287744947dcSTom Erickson 		else
1288744947dcSTom Erickson 			data = dr->dt.dl.dr_data;
1289744947dcSTom Erickson 
1290744947dcSTom Erickson 		DB_DNODE_EXIT(dr->dr_dbuf);
1291744947dcSTom Erickson 	}
1292744947dcSTom Erickson 
129306e0070dSMark Shellenbaum 	return (data);
129406e0070dSMark Shellenbaum }
129506e0070dSMark Shellenbaum 
12960a586ceaSMark Shellenbaum void
129706e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
12980a586ceaSMark Shellenbaum {
12990a586ceaSMark Shellenbaum 	objset_t *os = dn->dn_objset;
13000a586ceaSMark Shellenbaum 	void *data = NULL;
130106e0070dSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
1302d5285caeSGeorge Wilson 	uint64_t *user = NULL;
1303d5285caeSGeorge Wilson 	uint64_t *group = NULL;
13040a586ceaSMark Shellenbaum 	int flags = dn->dn_id_flags;
13050a586ceaSMark Shellenbaum 	int error;
130606e0070dSMark Shellenbaum 	boolean_t have_spill = B_FALSE;
13070a586ceaSMark Shellenbaum 
13080a586ceaSMark Shellenbaum 	if (!dmu_objset_userused_enabled(dn->dn_objset))
13090a586ceaSMark Shellenbaum 		return;
13100a586ceaSMark Shellenbaum 
13110a586ceaSMark Shellenbaum 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
13120a586ceaSMark Shellenbaum 	    DN_ID_CHKED_SPILL)))
13130a586ceaSMark Shellenbaum 		return;
13140a586ceaSMark Shellenbaum 
13150a586ceaSMark Shellenbaum 	if (before && dn->dn_bonuslen != 0)
13160a586ceaSMark Shellenbaum 		data = DN_BONUS(dn->dn_phys);
131706e0070dSMark Shellenbaum 	else if (!before && dn->dn_bonuslen != 0) {
131806e0070dSMark Shellenbaum 		if (dn->dn_bonus) {
131906e0070dSMark Shellenbaum 			db = dn->dn_bonus;
132006e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
132106e0070dSMark Shellenbaum 			data = dmu_objset_userquota_find_data(db, tx);
132206e0070dSMark Shellenbaum 		} else {
132306e0070dSMark Shellenbaum 			data = DN_BONUS(dn->dn_phys);
132406e0070dSMark Shellenbaum 		}
132506e0070dSMark Shellenbaum 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
13260a586ceaSMark Shellenbaum 			int rf = 0;
13270a586ceaSMark Shellenbaum 
13280a586ceaSMark Shellenbaum 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
13290a586ceaSMark Shellenbaum 				rf |= DB_RF_HAVESTRUCT;
13301d8ccc7bSMark Shellenbaum 			error = dmu_spill_hold_by_dnode(dn,
13311d8ccc7bSMark Shellenbaum 			    rf | DB_RF_MUST_SUCCEED,
133206e0070dSMark Shellenbaum 			    FTAG, (dmu_buf_t **)&db);
13330a586ceaSMark Shellenbaum 			ASSERT(error == 0);
133406e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
133506e0070dSMark Shellenbaum 			data = (before) ? db->db.db_data :
133606e0070dSMark Shellenbaum 			    dmu_objset_userquota_find_data(db, tx);
133706e0070dSMark Shellenbaum 			have_spill = B_TRUE;
13380a586ceaSMark Shellenbaum 	} else {
13390a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
13400a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13410a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
13420a586ceaSMark Shellenbaum 		return;
13430a586ceaSMark Shellenbaum 	}
13440a586ceaSMark Shellenbaum 
13450a586ceaSMark Shellenbaum 	if (before) {
134606e0070dSMark Shellenbaum 		ASSERT(data);
13470a586ceaSMark Shellenbaum 		user = &dn->dn_olduid;
13480a586ceaSMark Shellenbaum 		group = &dn->dn_oldgid;
134906e0070dSMark Shellenbaum 	} else if (data) {
13500a586ceaSMark Shellenbaum 		user = &dn->dn_newuid;
13510a586ceaSMark Shellenbaum 		group = &dn->dn_newgid;
13520a586ceaSMark Shellenbaum 	}
13530a586ceaSMark Shellenbaum 
135406e0070dSMark Shellenbaum 	/*
135506e0070dSMark Shellenbaum 	 * Must always call the callback in case the object
135606e0070dSMark Shellenbaum 	 * type has changed and that type isn't an object type to track
135706e0070dSMark Shellenbaum 	 */
13580a586ceaSMark Shellenbaum 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
13590a586ceaSMark Shellenbaum 	    user, group);
13600a586ceaSMark Shellenbaum 
136106e0070dSMark Shellenbaum 	/*
136206e0070dSMark Shellenbaum 	 * Preserve existing uid/gid when the callback can't determine
136306e0070dSMark Shellenbaum 	 * what the new uid/gid are and the callback returned EEXIST.
136406e0070dSMark Shellenbaum 	 * The EEXIST error tells us to just use the existing uid/gid.
136506e0070dSMark Shellenbaum 	 * If we don't know what the old values are then just assign
136606e0070dSMark Shellenbaum 	 * them to 0, since that is a new file  being created.
136706e0070dSMark Shellenbaum 	 */
136806e0070dSMark Shellenbaum 	if (!before && data == NULL && error == EEXIST) {
136906e0070dSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST) {
137006e0070dSMark Shellenbaum 			dn->dn_newuid = dn->dn_olduid;
137106e0070dSMark Shellenbaum 			dn->dn_newgid = dn->dn_oldgid;
137206e0070dSMark Shellenbaum 		} else {
137306e0070dSMark Shellenbaum 			dn->dn_newuid = 0;
137406e0070dSMark Shellenbaum 			dn->dn_newgid = 0;
137506e0070dSMark Shellenbaum 		}
137606e0070dSMark Shellenbaum 		error = 0;
137706e0070dSMark Shellenbaum 	}
137806e0070dSMark Shellenbaum 
137906e0070dSMark Shellenbaum 	if (db)
138006e0070dSMark Shellenbaum 		mutex_exit(&db->db_mtx);
138106e0070dSMark Shellenbaum 
13820a586ceaSMark Shellenbaum 	mutex_enter(&dn->dn_mtx);
13830a586ceaSMark Shellenbaum 	if (error == 0 && before)
13840a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
13850a586ceaSMark Shellenbaum 	if (error == 0 && !before)
13860a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
13870a586ceaSMark Shellenbaum 
138806e0070dSMark Shellenbaum 	if (have_spill) {
13890a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
13900a586ceaSMark Shellenbaum 	} else {
13910a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13920a586ceaSMark Shellenbaum 	}
13930a586ceaSMark Shellenbaum 	mutex_exit(&dn->dn_mtx);
139406e0070dSMark Shellenbaum 	if (have_spill)
139506e0070dSMark Shellenbaum 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
13960a586ceaSMark Shellenbaum }
13970a586ceaSMark Shellenbaum 
139814843421SMatthew Ahrens boolean_t
139914843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
140014843421SMatthew Ahrens {
1401503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
140214843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
140314843421SMatthew Ahrens }
140414843421SMatthew Ahrens 
140514843421SMatthew Ahrens int
140614843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
140714843421SMatthew Ahrens {
140814843421SMatthew Ahrens 	uint64_t obj;
140914843421SMatthew Ahrens 	int err = 0;
141014843421SMatthew Ahrens 
141114843421SMatthew Ahrens 	if (dmu_objset_userspace_present(os))
141214843421SMatthew Ahrens 		return (0);
1413503ad85cSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
1414be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
141514843421SMatthew Ahrens 	if (dmu_objset_is_snapshot(os))
1416be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
141714843421SMatthew Ahrens 
141814843421SMatthew Ahrens 	/*
141914843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
142014843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
142114843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
142214843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
142314843421SMatthew Ahrens 	 * independently.
142414843421SMatthew Ahrens 	 */
142514843421SMatthew Ahrens 
142614843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
142768857716SLin Ling 		dmu_tx_t *tx;
142814843421SMatthew Ahrens 		dmu_buf_t *db;
142914843421SMatthew Ahrens 		int objerr;
143014843421SMatthew Ahrens 
143114843421SMatthew Ahrens 		if (issig(JUSTLOOKING) && issig(FORREAL))
1432be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
143314843421SMatthew Ahrens 
143414843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
14353b2aab18SMatthew Ahrens 		if (objerr != 0)
143614843421SMatthew Ahrens 			continue;
143768857716SLin Ling 		tx = dmu_tx_create(os);
143814843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
143914843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
14403b2aab18SMatthew Ahrens 		if (objerr != 0) {
144114843421SMatthew Ahrens 			dmu_tx_abort(tx);
144214843421SMatthew Ahrens 			continue;
144314843421SMatthew Ahrens 		}
144414843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
144514843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
144614843421SMatthew Ahrens 		dmu_tx_commit(tx);
144714843421SMatthew Ahrens 	}
144814843421SMatthew Ahrens 
1449503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
145014843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
145114843421SMatthew Ahrens 	return (0);
145214843421SMatthew Ahrens }
145314843421SMatthew Ahrens 
1454fa9e4066Sahrens void
1455a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1456a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1457fa9e4066Sahrens {
1458503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1459a2eea2e1Sahrens 	    usedobjsp, availobjsp);
1460a2eea2e1Sahrens }
1461a2eea2e1Sahrens 
1462a2eea2e1Sahrens uint64_t
1463a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1464a2eea2e1Sahrens {
1465503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1466a2eea2e1Sahrens }
1467a2eea2e1Sahrens 
1468a2eea2e1Sahrens void
1469a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1470a2eea2e1Sahrens {
1471503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
1472503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
1473503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1474a2eea2e1Sahrens }
1475a2eea2e1Sahrens 
1476a2eea2e1Sahrens void
1477a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1478a2eea2e1Sahrens {
1479503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
1480503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
1481a2eea2e1Sahrens 
1482503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1483503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1484a2eea2e1Sahrens 
1485a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1486503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
148714843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
148814843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
1489fa9e4066Sahrens }
1490fa9e4066Sahrens 
1491fa9e4066Sahrens int
1492fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1493fa9e4066Sahrens {
1494503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1495*bc9014e6SJustin Gibbs 		return (os->os_dsl_dataset->ds_is_snapshot);
1496fa9e4066Sahrens 	else
1497fa9e4066Sahrens 		return (B_FALSE);
1498fa9e4066Sahrens }
1499fa9e4066Sahrens 
1500ab04eb8eStimh int
1501ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1502ab04eb8eStimh     boolean_t *conflict)
1503ab04eb8eStimh {
1504503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1505ab04eb8eStimh 	uint64_t ignored;
1506ab04eb8eStimh 
1507c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1508be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1509ab04eb8eStimh 
1510ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1511c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1512c1379625SJustin T. Gibbs 	    MT_FIRST, real, maxlen, conflict));
1513ab04eb8eStimh }
1514ab04eb8eStimh 
1515fa9e4066Sahrens int
1516fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1517b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1518fa9e4066Sahrens {
1519503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1520fa9e4066Sahrens 	zap_cursor_t cursor;
1521fa9e4066Sahrens 	zap_attribute_t attr;
1522fa9e4066Sahrens 
15233b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
15243b2aab18SMatthew Ahrens 
1525c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1526be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1527fa9e4066Sahrens 
1528fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
1529fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1530c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1531fa9e4066Sahrens 
153287e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
153387e5029aSahrens 		zap_cursor_fini(&cursor);
1534be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
153587e5029aSahrens 	}
153687e5029aSahrens 
153787e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
153887e5029aSahrens 		zap_cursor_fini(&cursor);
1539be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
154087e5029aSahrens 	}
154187e5029aSahrens 
154287e5029aSahrens 	(void) strcpy(name, attr.za_name);
154387e5029aSahrens 	if (idp)
154487e5029aSahrens 		*idp = attr.za_first_integer;
1545b38f0970Sck 	if (case_conflict)
1546b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
154787e5029aSahrens 	zap_cursor_advance(&cursor);
154887e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
154987e5029aSahrens 	zap_cursor_fini(&cursor);
155087e5029aSahrens 
155187e5029aSahrens 	return (0);
155287e5029aSahrens }
155387e5029aSahrens 
155487e5029aSahrens int
155587e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
155687e5029aSahrens     uint64_t *idp, uint64_t *offp)
155787e5029aSahrens {
1558503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
155987e5029aSahrens 	zap_cursor_t cursor;
156087e5029aSahrens 	zap_attribute_t attr;
156187e5029aSahrens 
156287e5029aSahrens 	/* there is no next dir on a snapshot! */
1563503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
1564c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj)
1565be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1566fa9e4066Sahrens 
156787e5029aSahrens 	zap_cursor_init_serialized(&cursor,
156887e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
1569c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
157087e5029aSahrens 
157187e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
157287e5029aSahrens 		zap_cursor_fini(&cursor);
1573be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
157487e5029aSahrens 	}
157587e5029aSahrens 
157687e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
157787e5029aSahrens 		zap_cursor_fini(&cursor);
1578be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
157987e5029aSahrens 	}
1580fa9e4066Sahrens 
1581fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
158287e5029aSahrens 	if (idp)
158387e5029aSahrens 		*idp = attr.za_first_integer;
1584fa9e4066Sahrens 	zap_cursor_advance(&cursor);
1585fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
158687e5029aSahrens 	zap_cursor_fini(&cursor);
1587fa9e4066Sahrens 
1588fa9e4066Sahrens 	return (0);
1589fa9e4066Sahrens }
1590fa9e4066Sahrens 
1591fa9e4066Sahrens /*
15923b2aab18SMatthew Ahrens  * Find objsets under and including ddobj, call func(ds) on each.
1593fa9e4066Sahrens  */
15941d452cf5Sahrens int
15953b2aab18SMatthew Ahrens dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
15963b2aab18SMatthew Ahrens     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1597088f3894Sahrens {
15983b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
15993b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
16003b2aab18SMatthew Ahrens 	zap_cursor_t zc;
16013b2aab18SMatthew Ahrens 	zap_attribute_t *attr;
16023b2aab18SMatthew Ahrens 	uint64_t thisobj;
16033b2aab18SMatthew Ahrens 	int err;
16043b2aab18SMatthew Ahrens 
16053b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
16063b2aab18SMatthew Ahrens 
16073b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
16083b2aab18SMatthew Ahrens 	if (err != 0)
16093b2aab18SMatthew Ahrens 		return (err);
16103b2aab18SMatthew Ahrens 
16113b2aab18SMatthew Ahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
16123b2aab18SMatthew Ahrens 	if (dd->dd_myname[0] == '$') {
16133b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
16143b2aab18SMatthew Ahrens 		return (0);
16153b2aab18SMatthew Ahrens 	}
16163b2aab18SMatthew Ahrens 
1617c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
16183b2aab18SMatthew Ahrens 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
16193b2aab18SMatthew Ahrens 
16203b2aab18SMatthew Ahrens 	/*
16213b2aab18SMatthew Ahrens 	 * Iterate over all children.
16223b2aab18SMatthew Ahrens 	 */
16233b2aab18SMatthew Ahrens 	if (flags & DS_FIND_CHILDREN) {
16243b2aab18SMatthew Ahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1625c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
16263b2aab18SMatthew Ahrens 		    zap_cursor_retrieve(&zc, attr) == 0;
16273b2aab18SMatthew Ahrens 		    (void) zap_cursor_advance(&zc)) {
16283b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
16293b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
16303b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
16313b2aab18SMatthew Ahrens 
16323b2aab18SMatthew Ahrens 			err = dmu_objset_find_dp(dp, attr->za_first_integer,
16333b2aab18SMatthew Ahrens 			    func, arg, flags);
16343b2aab18SMatthew Ahrens 			if (err != 0)
16353b2aab18SMatthew Ahrens 				break;
16363b2aab18SMatthew Ahrens 		}
16373b2aab18SMatthew Ahrens 		zap_cursor_fini(&zc);
16383b2aab18SMatthew Ahrens 
16393b2aab18SMatthew Ahrens 		if (err != 0) {
16403b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
16413b2aab18SMatthew Ahrens 			kmem_free(attr, sizeof (zap_attribute_t));
16423b2aab18SMatthew Ahrens 			return (err);
16433b2aab18SMatthew Ahrens 		}
16443b2aab18SMatthew Ahrens 	}
16453b2aab18SMatthew Ahrens 
16463b2aab18SMatthew Ahrens 	/*
16473b2aab18SMatthew Ahrens 	 * Iterate over all snapshots.
16483b2aab18SMatthew Ahrens 	 */
16493b2aab18SMatthew Ahrens 	if (flags & DS_FIND_SNAPSHOTS) {
16503b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
16513b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
16523b2aab18SMatthew Ahrens 
16533b2aab18SMatthew Ahrens 		if (err == 0) {
1654c1379625SJustin T. Gibbs 			uint64_t snapobj;
1655c1379625SJustin T. Gibbs 
1656c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
16573b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
16583b2aab18SMatthew Ahrens 
16593b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
16603b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
16613b2aab18SMatthew Ahrens 			    (void) zap_cursor_advance(&zc)) {
16623b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
16633b2aab18SMatthew Ahrens 				    sizeof (uint64_t));
16643b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
16653b2aab18SMatthew Ahrens 
16663b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
16673b2aab18SMatthew Ahrens 				    attr->za_first_integer, FTAG, &ds);
16683b2aab18SMatthew Ahrens 				if (err != 0)
16693b2aab18SMatthew Ahrens 					break;
16703b2aab18SMatthew Ahrens 				err = func(dp, ds, arg);
16713b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
16723b2aab18SMatthew Ahrens 				if (err != 0)
16733b2aab18SMatthew Ahrens 					break;
16743b2aab18SMatthew Ahrens 			}
16753b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
16763b2aab18SMatthew Ahrens 		}
16773b2aab18SMatthew Ahrens 	}
16783b2aab18SMatthew Ahrens 
16793b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
16803b2aab18SMatthew Ahrens 	kmem_free(attr, sizeof (zap_attribute_t));
16813b2aab18SMatthew Ahrens 
16823b2aab18SMatthew Ahrens 	if (err != 0)
16833b2aab18SMatthew Ahrens 		return (err);
16843b2aab18SMatthew Ahrens 
16853b2aab18SMatthew Ahrens 	/*
16863b2aab18SMatthew Ahrens 	 * Apply to self.
16873b2aab18SMatthew Ahrens 	 */
16883b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
16893b2aab18SMatthew Ahrens 	if (err != 0)
16903b2aab18SMatthew Ahrens 		return (err);
16913b2aab18SMatthew Ahrens 	err = func(dp, ds, arg);
16923b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
16933b2aab18SMatthew Ahrens 	return (err);
1694088f3894Sahrens }
1695088f3894Sahrens 
1696088f3894Sahrens /*
16973b2aab18SMatthew Ahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
16983b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when this is called, and it
16993b2aab18SMatthew Ahrens  * will not be held when the callback is called.
17003b2aab18SMatthew Ahrens  * Therefore this function should only be used when the pool is not changing
17013b2aab18SMatthew Ahrens  * (e.g. in syncing context), or the callback can deal with the possible races.
1702088f3894Sahrens  */
17033b2aab18SMatthew Ahrens static int
17043b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name,
17053b2aab18SMatthew Ahrens     int func(const char *, void *), void *arg, int flags)
1706fa9e4066Sahrens {
1707fa9e4066Sahrens 	dsl_dir_t *dd;
17083b2aab18SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
1709088f3894Sahrens 	dsl_dataset_t *ds;
1710fa9e4066Sahrens 	zap_cursor_t zc;
1711b7661cccSmmusante 	zap_attribute_t *attr;
1712fa9e4066Sahrens 	char *child;
1713088f3894Sahrens 	uint64_t thisobj;
1714088f3894Sahrens 	int err;
1715fa9e4066Sahrens 
17163b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dp, FTAG);
17173b2aab18SMatthew Ahrens 
17183b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
17193b2aab18SMatthew Ahrens 	if (err != 0) {
17203b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
17211d452cf5Sahrens 		return (err);
17223b2aab18SMatthew Ahrens 	}
1723fa9e4066Sahrens 
1724088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1725088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
17263b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
17273b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
1728088f3894Sahrens 		return (0);
1729088f3894Sahrens 	}
1730088f3894Sahrens 
1731c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1732b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1733fa9e4066Sahrens 
1734fa9e4066Sahrens 	/*
1735fa9e4066Sahrens 	 * Iterate over all children.
1736fa9e4066Sahrens 	 */
17370b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
1738088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1739c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
1740b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
17410b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
17423b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
17433b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
17443b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
1745fa9e4066Sahrens 
1746486ae710SMatthew Ahrens 			child = kmem_asprintf("%s/%s", name, attr->za_name);
17473b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
17483b2aab18SMatthew Ahrens 			err = dmu_objset_find_impl(spa, child,
17493b2aab18SMatthew Ahrens 			    func, arg, flags);
17503b2aab18SMatthew Ahrens 			dsl_pool_config_enter(dp, FTAG);
1751486ae710SMatthew Ahrens 			strfree(child);
17523b2aab18SMatthew Ahrens 			if (err != 0)
17530b69c2f0Sahrens 				break;
17540b69c2f0Sahrens 		}
17550b69c2f0Sahrens 		zap_cursor_fini(&zc);
17561d452cf5Sahrens 
17573b2aab18SMatthew Ahrens 		if (err != 0) {
17583b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
17593b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
1760b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
17610b69c2f0Sahrens 			return (err);
17620b69c2f0Sahrens 		}
1763fa9e4066Sahrens 	}
1764fa9e4066Sahrens 
1765fa9e4066Sahrens 	/*
1766fa9e4066Sahrens 	 * Iterate over all snapshots.
1767fa9e4066Sahrens 	 */
1768088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
1769088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1770088f3894Sahrens 
1771088f3894Sahrens 		if (err == 0) {
1772c1379625SJustin T. Gibbs 			uint64_t snapobj;
1773c1379625SJustin T. Gibbs 
1774c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1775088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
1776088f3894Sahrens 
1777088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1778088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
1779088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
17803b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
1781088f3894Sahrens 				    sizeof (uint64_t));
17823b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
1783088f3894Sahrens 
1784486ae710SMatthew Ahrens 				child = kmem_asprintf("%s@%s",
1785486ae710SMatthew Ahrens 				    name, attr->za_name);
17863b2aab18SMatthew Ahrens 				dsl_pool_config_exit(dp, FTAG);
17873b2aab18SMatthew Ahrens 				err = func(child, arg);
17883b2aab18SMatthew Ahrens 				dsl_pool_config_enter(dp, FTAG);
1789486ae710SMatthew Ahrens 				strfree(child);
17903b2aab18SMatthew Ahrens 				if (err != 0)
1791088f3894Sahrens 					break;
1792088f3894Sahrens 			}
1793088f3894Sahrens 			zap_cursor_fini(&zc);
1794fa9e4066Sahrens 		}
1795fa9e4066Sahrens 	}
1796fa9e4066Sahrens 
17973b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
1798b7661cccSmmusante 	kmem_free(attr, sizeof (zap_attribute_t));
17993b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dp, FTAG);
1800fa9e4066Sahrens 
18013b2aab18SMatthew Ahrens 	if (err != 0)
18021d452cf5Sahrens 		return (err);
18031d452cf5Sahrens 
18043b2aab18SMatthew Ahrens 	/* Apply to self. */
18053b2aab18SMatthew Ahrens 	return (func(name, arg));
1806fa9e4066Sahrens }
1807f18faf3fSek 
18083b2aab18SMatthew Ahrens /*
18093b2aab18SMatthew Ahrens  * See comment above dmu_objset_find_impl().
18103b2aab18SMatthew Ahrens  */
18117f73c863SRich Morris int
18123b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg,
18133b2aab18SMatthew Ahrens     int flags)
18147f73c863SRich Morris {
18153b2aab18SMatthew Ahrens 	spa_t *spa;
18163b2aab18SMatthew Ahrens 	int error;
18177f73c863SRich Morris 
18183b2aab18SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
18193b2aab18SMatthew Ahrens 	if (error != 0)
18203b2aab18SMatthew Ahrens 		return (error);
18213b2aab18SMatthew Ahrens 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
18223b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
18233b2aab18SMatthew Ahrens 	return (error);
18247f73c863SRich Morris }
18257f73c863SRich Morris 
1826f18faf3fSek void
1827f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr)
1828f18faf3fSek {
1829503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1830503ad85cSMatthew Ahrens 	os->os_user_ptr = user_ptr;
1831f18faf3fSek }
1832f18faf3fSek 
1833f18faf3fSek void *
1834f18faf3fSek dmu_objset_get_user(objset_t *os)
1835f18faf3fSek {
1836503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1837503ad85cSMatthew Ahrens 	return (os->os_user_ptr);
1838f18faf3fSek }
18393b2aab18SMatthew Ahrens 
18403b2aab18SMatthew Ahrens /*
18413b2aab18SMatthew Ahrens  * Determine name of filesystem, given name of snapshot.
18423b2aab18SMatthew Ahrens  * buf must be at least MAXNAMELEN bytes
18433b2aab18SMatthew Ahrens  */
18443b2aab18SMatthew Ahrens int
18453b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf)
18463b2aab18SMatthew Ahrens {
18473b2aab18SMatthew Ahrens 	char *atp = strchr(snapname, '@');
18483b2aab18SMatthew Ahrens 	if (atp == NULL)
1849be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
18503b2aab18SMatthew Ahrens 	if (atp - snapname >= MAXNAMELEN)
1851be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
18523b2aab18SMatthew Ahrens 	(void) strlcpy(buf, snapname, atp - snapname + 1);
18533b2aab18SMatthew Ahrens 	return (0);
18543b2aab18SMatthew Ahrens }
1855