xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 6e0cbcaa)
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.
23fa9e4066Sahrens  */
24fa9e4066Sahrens 
2555da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
2655da60b9SMark J Musante 
27ecd6cf80Smarks #include <sys/cred.h>
28fa9e4066Sahrens #include <sys/zfs_context.h>
29fa9e4066Sahrens #include <sys/dmu_objset.h>
30fa9e4066Sahrens #include <sys/dsl_dir.h>
31fa9e4066Sahrens #include <sys/dsl_dataset.h>
32fa9e4066Sahrens #include <sys/dsl_prop.h>
33fa9e4066Sahrens #include <sys/dsl_pool.h>
341d452cf5Sahrens #include <sys/dsl_synctask.h>
35ecd6cf80Smarks #include <sys/dsl_deleg.h>
36fa9e4066Sahrens #include <sys/dnode.h>
37fa9e4066Sahrens #include <sys/dbuf.h>
38a2eea2e1Sahrens #include <sys/zvol.h>
39fa9e4066Sahrens #include <sys/dmu_tx.h>
40fa9e4066Sahrens #include <sys/zap.h>
41fa9e4066Sahrens #include <sys/zil.h>
42fa9e4066Sahrens #include <sys/dmu_impl.h>
43ecd6cf80Smarks #include <sys/zfs_ioctl.h>
440a586ceaSMark Shellenbaum #include <sys/sa.h>
45fa9e4066Sahrens 
46744947dcSTom Erickson /*
47744947dcSTom Erickson  * Needed to close a window in dnode_move() that allows the objset to be freed
48744947dcSTom Erickson  * before it can be safely accessed.
49744947dcSTom Erickson  */
50744947dcSTom Erickson krwlock_t os_lock;
51744947dcSTom Erickson 
52744947dcSTom Erickson void
53744947dcSTom Erickson dmu_objset_init(void)
54744947dcSTom Erickson {
55744947dcSTom Erickson 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
56744947dcSTom Erickson }
57744947dcSTom Erickson 
58744947dcSTom Erickson void
59744947dcSTom Erickson dmu_objset_fini(void)
60744947dcSTom Erickson {
61744947dcSTom Erickson 	rw_destroy(&os_lock);
62744947dcSTom Erickson }
63744947dcSTom Erickson 
64fa9e4066Sahrens spa_t *
65fa9e4066Sahrens dmu_objset_spa(objset_t *os)
66fa9e4066Sahrens {
67503ad85cSMatthew Ahrens 	return (os->os_spa);
68fa9e4066Sahrens }
69fa9e4066Sahrens 
70fa9e4066Sahrens zilog_t *
71fa9e4066Sahrens dmu_objset_zil(objset_t *os)
72fa9e4066Sahrens {
73503ad85cSMatthew Ahrens 	return (os->os_zil);
74fa9e4066Sahrens }
75fa9e4066Sahrens 
76fa9e4066Sahrens dsl_pool_t *
77fa9e4066Sahrens dmu_objset_pool(objset_t *os)
78fa9e4066Sahrens {
79fa9e4066Sahrens 	dsl_dataset_t *ds;
80fa9e4066Sahrens 
81503ad85cSMatthew Ahrens 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
82fa9e4066Sahrens 		return (ds->ds_dir->dd_pool);
83fa9e4066Sahrens 	else
84503ad85cSMatthew Ahrens 		return (spa_get_dsl(os->os_spa));
85fa9e4066Sahrens }
86fa9e4066Sahrens 
87fa9e4066Sahrens dsl_dataset_t *
88fa9e4066Sahrens dmu_objset_ds(objset_t *os)
89fa9e4066Sahrens {
90503ad85cSMatthew Ahrens 	return (os->os_dsl_dataset);
91fa9e4066Sahrens }
92fa9e4066Sahrens 
93fa9e4066Sahrens dmu_objset_type_t
94fa9e4066Sahrens dmu_objset_type(objset_t *os)
95fa9e4066Sahrens {
96503ad85cSMatthew Ahrens 	return (os->os_phys->os_type);
97fa9e4066Sahrens }
98fa9e4066Sahrens 
99fa9e4066Sahrens void
100fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
101fa9e4066Sahrens {
102503ad85cSMatthew Ahrens 	dsl_dataset_name(os->os_dsl_dataset, buf);
103fa9e4066Sahrens }
104fa9e4066Sahrens 
105fa9e4066Sahrens uint64_t
106fa9e4066Sahrens dmu_objset_id(objset_t *os)
107fa9e4066Sahrens {
108503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
109fa9e4066Sahrens 
110fa9e4066Sahrens 	return (ds ? ds->ds_object : 0);
111fa9e4066Sahrens }
112fa9e4066Sahrens 
11355da60b9SMark J Musante uint64_t
11455da60b9SMark J Musante dmu_objset_syncprop(objset_t *os)
11555da60b9SMark J Musante {
11655da60b9SMark J Musante 	return (os->os_sync);
11755da60b9SMark J Musante }
11855da60b9SMark J Musante 
119e09fa4daSNeil Perrin uint64_t
120e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
121e09fa4daSNeil Perrin {
122e09fa4daSNeil Perrin 	return (os->os_logbias);
123e09fa4daSNeil Perrin }
124e09fa4daSNeil Perrin 
125fa9e4066Sahrens static void
126fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
127fa9e4066Sahrens {
128503ad85cSMatthew Ahrens 	objset_t *os = arg;
129fa9e4066Sahrens 
130fa9e4066Sahrens 	/*
131fa9e4066Sahrens 	 * Inheritance should have been done by now.
132fa9e4066Sahrens 	 */
133fa9e4066Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
134fa9e4066Sahrens 
135503ad85cSMatthew Ahrens 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
136fa9e4066Sahrens }
137fa9e4066Sahrens 
138fa9e4066Sahrens static void
139fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
140fa9e4066Sahrens {
141503ad85cSMatthew Ahrens 	objset_t *os = arg;
142fa9e4066Sahrens 
143fa9e4066Sahrens 	/*
144fa9e4066Sahrens 	 * Inheritance and range checking should have been done by now.
145fa9e4066Sahrens 	 */
146fa9e4066Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
147fa9e4066Sahrens 
148503ad85cSMatthew Ahrens 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
149fa9e4066Sahrens }
150fa9e4066Sahrens 
151d0ad202dSahrens static void
152d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
153d0ad202dSahrens {
154503ad85cSMatthew Ahrens 	objset_t *os = arg;
155d0ad202dSahrens 
156d0ad202dSahrens 	/*
157d0ad202dSahrens 	 * Inheritance and range checking should have been done by now.
158d0ad202dSahrens 	 */
159d0ad202dSahrens 	ASSERT(newval > 0);
160503ad85cSMatthew Ahrens 	ASSERT(newval <= spa_max_replication(os->os_spa));
161d0ad202dSahrens 
162503ad85cSMatthew Ahrens 	os->os_copies = newval;
163d0ad202dSahrens }
164d0ad202dSahrens 
165b24ab676SJeff Bonwick static void
166b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
167b24ab676SJeff Bonwick {
168b24ab676SJeff Bonwick 	objset_t *os = arg;
169b24ab676SJeff Bonwick 	spa_t *spa = os->os_spa;
170b24ab676SJeff Bonwick 	enum zio_checksum checksum;
171b24ab676SJeff Bonwick 
172b24ab676SJeff Bonwick 	/*
173b24ab676SJeff Bonwick 	 * Inheritance should have been done by now.
174b24ab676SJeff Bonwick 	 */
175b24ab676SJeff Bonwick 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
176b24ab676SJeff Bonwick 
177b24ab676SJeff Bonwick 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
178b24ab676SJeff Bonwick 
179b24ab676SJeff Bonwick 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
180b24ab676SJeff Bonwick 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
181b24ab676SJeff Bonwick }
182b24ab676SJeff Bonwick 
1833baa08fcSek static void
1843baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval)
1853baa08fcSek {
186503ad85cSMatthew Ahrens 	objset_t *os = arg;
1873baa08fcSek 
1883baa08fcSek 	/*
1893baa08fcSek 	 * Inheritance and range checking should have been done by now.
1903baa08fcSek 	 */
1913baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
1923baa08fcSek 	    newval == ZFS_CACHE_METADATA);
1933baa08fcSek 
194503ad85cSMatthew Ahrens 	os->os_primary_cache = newval;
1953baa08fcSek }
1963baa08fcSek 
1973baa08fcSek static void
1983baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval)
1993baa08fcSek {
200503ad85cSMatthew Ahrens 	objset_t *os = arg;
2013baa08fcSek 
2023baa08fcSek 	/*
2033baa08fcSek 	 * Inheritance and range checking should have been done by now.
2043baa08fcSek 	 */
2053baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2063baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2073baa08fcSek 
208503ad85cSMatthew Ahrens 	os->os_secondary_cache = newval;
2093baa08fcSek }
2103baa08fcSek 
21155da60b9SMark J Musante static void
21255da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval)
21355da60b9SMark J Musante {
21455da60b9SMark J Musante 	objset_t *os = arg;
21555da60b9SMark J Musante 
21655da60b9SMark J Musante 	/*
21755da60b9SMark J Musante 	 * Inheritance and range checking should have been done by now.
21855da60b9SMark J Musante 	 */
21955da60b9SMark J Musante 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
22055da60b9SMark J Musante 	    newval == ZFS_SYNC_DISABLED);
22155da60b9SMark J Musante 
22255da60b9SMark J Musante 	os->os_sync = newval;
22355da60b9SMark J Musante 	if (os->os_zil)
22455da60b9SMark J Musante 		zil_set_sync(os->os_zil, newval);
22555da60b9SMark J Musante }
22655da60b9SMark J Musante 
227e09fa4daSNeil Perrin static void
228e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
229e09fa4daSNeil Perrin {
230e09fa4daSNeil Perrin 	objset_t *os = arg;
231e09fa4daSNeil Perrin 
232e09fa4daSNeil Perrin 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
233e09fa4daSNeil Perrin 	    newval == ZFS_LOGBIAS_THROUGHPUT);
234e09fa4daSNeil Perrin 	os->os_logbias = newval;
235e09fa4daSNeil Perrin 	if (os->os_zil)
236e09fa4daSNeil Perrin 		zil_set_logbias(os->os_zil, newval);
237e09fa4daSNeil Perrin }
238e09fa4daSNeil Perrin 
239fa9e4066Sahrens void
240fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
241fa9e4066Sahrens {
242fa9e4066Sahrens 	objset_phys_t *osp = buf;
243fa9e4066Sahrens 
24414843421SMatthew Ahrens 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
245fa9e4066Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
246fa9e4066Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
247fa9e4066Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
24814843421SMatthew Ahrens 	osp->os_flags = BSWAP_64(osp->os_flags);
24914843421SMatthew Ahrens 	if (size == sizeof (objset_phys_t)) {
25014843421SMatthew Ahrens 		dnode_byteswap(&osp->os_userused_dnode);
25114843421SMatthew Ahrens 		dnode_byteswap(&osp->os_groupused_dnode);
25214843421SMatthew Ahrens 	}
253fa9e4066Sahrens }
254fa9e4066Sahrens 
255ea8dc4b6Seschrock int
256ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
257503ad85cSMatthew Ahrens     objset_t **osp)
258fa9e4066Sahrens {
259503ad85cSMatthew Ahrens 	objset_t *os;
260088f3894Sahrens 	int i, err;
261fa9e4066Sahrens 
26291ebeef5Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
26391ebeef5Sahrens 
264503ad85cSMatthew Ahrens 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
265503ad85cSMatthew Ahrens 	os->os_dsl_dataset = ds;
266503ad85cSMatthew Ahrens 	os->os_spa = spa;
267503ad85cSMatthew Ahrens 	os->os_rootbp = bp;
268503ad85cSMatthew Ahrens 	if (!BP_IS_HOLE(os->os_rootbp)) {
26913506d1eSmaybee 		uint32_t aflags = ARC_WAIT;
270ea8dc4b6Seschrock 		zbookmark_t zb;
271b24ab676SJeff Bonwick 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
272b24ab676SJeff Bonwick 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
273b24ab676SJeff Bonwick 
274503ad85cSMatthew Ahrens 		if (DMU_OS_IS_L2CACHEABLE(os))
2753baa08fcSek 			aflags |= ARC_L2CACHE;
276ea8dc4b6Seschrock 
277503ad85cSMatthew Ahrens 		dprintf_bp(os->os_rootbp, "reading %s", "");
278088f3894Sahrens 		/*
2793f9d6ad7SLin Ling 		 * XXX when bprewrite scrub can change the bp,
280088f3894Sahrens 		 * and this is called from dmu_objset_open_ds_os, the bp
281088f3894Sahrens 		 * could change, and we'll need a lock.
282088f3894Sahrens 		 */
2833f9d6ad7SLin Ling 		err = dsl_read_nolock(NULL, spa, os->os_rootbp,
284503ad85cSMatthew Ahrens 		    arc_getbuf_func, &os->os_phys_buf,
28513506d1eSmaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
286ea8dc4b6Seschrock 		if (err) {
287503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
288b87f3af3Sperrin 			/* convert checksum errors into IO errors */
289b87f3af3Sperrin 			if (err == ECKSUM)
290b87f3af3Sperrin 				err = EIO;
291ea8dc4b6Seschrock 			return (err);
292ea8dc4b6Seschrock 		}
29314843421SMatthew Ahrens 
29414843421SMatthew Ahrens 		/* Increase the blocksize if we are permitted. */
29514843421SMatthew Ahrens 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
296503ad85cSMatthew Ahrens 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
29714843421SMatthew Ahrens 			arc_buf_t *buf = arc_buf_alloc(spa,
298503ad85cSMatthew Ahrens 			    sizeof (objset_phys_t), &os->os_phys_buf,
29914843421SMatthew Ahrens 			    ARC_BUFC_METADATA);
30014843421SMatthew Ahrens 			bzero(buf->b_data, sizeof (objset_phys_t));
301503ad85cSMatthew Ahrens 			bcopy(os->os_phys_buf->b_data, buf->b_data,
302503ad85cSMatthew Ahrens 			    arc_buf_size(os->os_phys_buf));
303503ad85cSMatthew Ahrens 			(void) arc_buf_remove_ref(os->os_phys_buf,
304503ad85cSMatthew Ahrens 			    &os->os_phys_buf);
305503ad85cSMatthew Ahrens 			os->os_phys_buf = buf;
30614843421SMatthew Ahrens 		}
30714843421SMatthew Ahrens 
308503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
309503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
310fa9e4066Sahrens 	} else {
31114843421SMatthew Ahrens 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
31214843421SMatthew Ahrens 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
313503ad85cSMatthew Ahrens 		os->os_phys_buf = arc_buf_alloc(spa, size,
314503ad85cSMatthew Ahrens 		    &os->os_phys_buf, ARC_BUFC_METADATA);
315503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
316503ad85cSMatthew Ahrens 		bzero(os->os_phys, size);
317fa9e4066Sahrens 	}
318fa9e4066Sahrens 
319fa9e4066Sahrens 	/*
320fa9e4066Sahrens 	 * Note: the changed_cb will be called once before the register
321fa9e4066Sahrens 	 * func returns, thus changing the checksum/compression from the
3223baa08fcSek 	 * default (fletcher2/off).  Snapshots don't need to know about
3233baa08fcSek 	 * checksum/compression/copies.
324fa9e4066Sahrens 	 */
3253baa08fcSek 	if (ds) {
3263baa08fcSek 		err = dsl_prop_register(ds, "primarycache",
327503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
328d0ad202dSahrens 		if (err == 0)
3293baa08fcSek 			err = dsl_prop_register(ds, "secondarycache",
330503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
3313baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
3323baa08fcSek 			if (err == 0)
3333baa08fcSek 				err = dsl_prop_register(ds, "checksum",
334503ad85cSMatthew Ahrens 				    checksum_changed_cb, os);
3353baa08fcSek 			if (err == 0)
3363baa08fcSek 				err = dsl_prop_register(ds, "compression",
337503ad85cSMatthew Ahrens 				    compression_changed_cb, os);
3383baa08fcSek 			if (err == 0)
3393baa08fcSek 				err = dsl_prop_register(ds, "copies",
340503ad85cSMatthew Ahrens 				    copies_changed_cb, os);
341b24ab676SJeff Bonwick 			if (err == 0)
342b24ab676SJeff Bonwick 				err = dsl_prop_register(ds, "dedup",
343b24ab676SJeff Bonwick 				    dedup_changed_cb, os);
344e09fa4daSNeil Perrin 			if (err == 0)
345e09fa4daSNeil Perrin 				err = dsl_prop_register(ds, "logbias",
346e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
34755da60b9SMark J Musante 			if (err == 0)
34855da60b9SMark J Musante 				err = dsl_prop_register(ds, "sync",
34955da60b9SMark J Musante 				    sync_changed_cb, os);
3503baa08fcSek 		}
351ea8dc4b6Seschrock 		if (err) {
352503ad85cSMatthew Ahrens 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
353503ad85cSMatthew Ahrens 			    &os->os_phys_buf) == 1);
354503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
355ea8dc4b6Seschrock 			return (err);
356ea8dc4b6Seschrock 		}
35799653d4eSeschrock 	} else if (ds == NULL) {
358fa9e4066Sahrens 		/* It's the meta-objset. */
359503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
360503ad85cSMatthew Ahrens 		os->os_compress = ZIO_COMPRESS_LZJB;
361503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
362b24ab676SJeff Bonwick 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
363b24ab676SJeff Bonwick 		os->os_dedup_verify = 0;
364b24ab676SJeff Bonwick 		os->os_logbias = 0;
36555da60b9SMark J Musante 		os->os_sync = 0;
366503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
367503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
368fa9e4066Sahrens 	}
369fa9e4066Sahrens 
370*6e0cbcaaSMatthew Ahrens 	if (ds == NULL || !dsl_dataset_is_snapshot(ds))
371*6e0cbcaaSMatthew Ahrens 		os->os_zil_header = os->os_phys->os_zil_header;
372503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
373fa9e4066Sahrens 
374fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
375503ad85cSMatthew Ahrens 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
376fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
377503ad85cSMatthew Ahrens 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
378fa9e4066Sahrens 		    offsetof(dnode_t, dn_dirty_link[i]));
379fa9e4066Sahrens 	}
380503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
381fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
382503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
383fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
384fa9e4066Sahrens 
385503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
386503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
387503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
388503ad85cSMatthew Ahrens 
389744947dcSTom Erickson 	DMU_META_DNODE(os) = dnode_special_open(os,
390744947dcSTom Erickson 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
391744947dcSTom Erickson 	    &os->os_meta_dnode);
392503ad85cSMatthew Ahrens 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
393744947dcSTom Erickson 		DMU_USERUSED_DNODE(os) = dnode_special_open(os,
394744947dcSTom Erickson 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
395744947dcSTom Erickson 		    &os->os_userused_dnode);
396744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
397744947dcSTom Erickson 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
398744947dcSTom Erickson 		    &os->os_groupused_dnode);
39914843421SMatthew Ahrens 	}
400fa9e4066Sahrens 
40191ebeef5Sahrens 	/*
40291ebeef5Sahrens 	 * We should be the only thread trying to do this because we
40391ebeef5Sahrens 	 * have ds_opening_lock
40491ebeef5Sahrens 	 */
40591ebeef5Sahrens 	if (ds) {
406503ad85cSMatthew Ahrens 		mutex_enter(&ds->ds_lock);
407503ad85cSMatthew Ahrens 		ASSERT(ds->ds_objset == NULL);
408503ad85cSMatthew Ahrens 		ds->ds_objset = os;
409503ad85cSMatthew Ahrens 		mutex_exit(&ds->ds_lock);
410fa9e4066Sahrens 	}
411fa9e4066Sahrens 
412503ad85cSMatthew Ahrens 	*osp = os;
413ea8dc4b6Seschrock 	return (0);
414fa9e4066Sahrens }
415fa9e4066Sahrens 
416503ad85cSMatthew Ahrens int
417503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
4183cb34c60Sahrens {
419503ad85cSMatthew Ahrens 	int err = 0;
4203cb34c60Sahrens 
4213cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
422503ad85cSMatthew Ahrens 	*osp = ds->ds_objset;
423503ad85cSMatthew Ahrens 	if (*osp == NULL) {
4243cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
425503ad85cSMatthew Ahrens 		    ds, &ds->ds_phys->ds_bp, osp);
4263cb34c60Sahrens 	}
4273cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
428503ad85cSMatthew Ahrens 	return (err);
4293cb34c60Sahrens }
4303cb34c60Sahrens 
431503ad85cSMatthew Ahrens /* called from zpl */
4323cb34c60Sahrens int
433503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
4343cb34c60Sahrens {
435503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
4363cb34c60Sahrens 	int err;
4373cb34c60Sahrens 
438503ad85cSMatthew Ahrens 	err = dsl_dataset_hold(name, tag, &ds);
4393cb34c60Sahrens 	if (err)
440503ad85cSMatthew Ahrens 		return (err);
441503ad85cSMatthew Ahrens 
442503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
443503ad85cSMatthew Ahrens 	if (err)
444503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
445503ad85cSMatthew Ahrens 
4463cb34c60Sahrens 	return (err);
4473cb34c60Sahrens }
4483cb34c60Sahrens 
449fa9e4066Sahrens /* called from zpl */
450fa9e4066Sahrens int
451503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
452503ad85cSMatthew Ahrens     boolean_t readonly, void *tag, objset_t **osp)
453fa9e4066Sahrens {
454f18faf3fSek 	dsl_dataset_t *ds;
455f18faf3fSek 	int err;
456fa9e4066Sahrens 
457503ad85cSMatthew Ahrens 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
458503ad85cSMatthew Ahrens 	if (err)
459fa9e4066Sahrens 		return (err);
460fa9e4066Sahrens 
461503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
4623cb34c60Sahrens 	if (err) {
463503ad85cSMatthew Ahrens 		dsl_dataset_disown(ds, tag);
464681d9761SEric Taylor 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
465503ad85cSMatthew Ahrens 		dmu_objset_disown(*osp, tag);
466503ad85cSMatthew Ahrens 		return (EINVAL);
467681d9761SEric Taylor 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
468681d9761SEric Taylor 		dmu_objset_disown(*osp, tag);
469681d9761SEric Taylor 		return (EROFS);
470fa9e4066Sahrens 	}
4713cb34c60Sahrens 	return (err);
472fa9e4066Sahrens }
473fa9e4066Sahrens 
474fa9e4066Sahrens void
475503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
476fa9e4066Sahrens {
477503ad85cSMatthew Ahrens 	dsl_dataset_rele(os->os_dsl_dataset, tag);
478503ad85cSMatthew Ahrens }
479503ad85cSMatthew Ahrens 
480503ad85cSMatthew Ahrens void
481503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
482503ad85cSMatthew Ahrens {
483503ad85cSMatthew Ahrens 	dsl_dataset_disown(os->os_dsl_dataset, tag);
484fa9e4066Sahrens }
485fa9e4066Sahrens 
486436b2950Sperrin int
4871934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
488ea8dc4b6Seschrock {
489ea8dc4b6Seschrock 	dnode_t *dn;
490c543ec06Sahrens 
491503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
492c543ec06Sahrens 
493c543ec06Sahrens 	/* process the mdn last, since the other dnodes have holds on it */
494744947dcSTom Erickson 	list_remove(&os->os_dnodes, DMU_META_DNODE(os));
495744947dcSTom Erickson 	list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
496ea8dc4b6Seschrock 
497ea8dc4b6Seschrock 	/*
498c543ec06Sahrens 	 * Find the first dnode with holds.  We have to do this dance
499c543ec06Sahrens 	 * because dnode_add_ref() only works if you already have a
500c543ec06Sahrens 	 * hold.  If there are no holds then it has no dbufs so OK to
501c543ec06Sahrens 	 * skip.
502ea8dc4b6Seschrock 	 */
503503ad85cSMatthew Ahrens 	for (dn = list_head(&os->os_dnodes);
5041934e92fSmaybee 	    dn && !dnode_add_ref(dn, FTAG);
505503ad85cSMatthew Ahrens 	    dn = list_next(&os->os_dnodes, dn))
506c543ec06Sahrens 		continue;
507c543ec06Sahrens 
508c543ec06Sahrens 	while (dn) {
509c543ec06Sahrens 		dnode_t *next_dn = dn;
510c543ec06Sahrens 
511c543ec06Sahrens 		do {
512503ad85cSMatthew Ahrens 			next_dn = list_next(&os->os_dnodes, next_dn);
5131934e92fSmaybee 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
514c543ec06Sahrens 
515503ad85cSMatthew Ahrens 		mutex_exit(&os->os_lock);
5161934e92fSmaybee 		dnode_evict_dbufs(dn);
517c543ec06Sahrens 		dnode_rele(dn, FTAG);
518503ad85cSMatthew Ahrens 		mutex_enter(&os->os_lock);
519c543ec06Sahrens 		dn = next_dn;
520ea8dc4b6Seschrock 	}
521744947dcSTom Erickson 	dn = list_head(&os->os_dnodes);
522503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
523744947dcSTom Erickson 	return (dn != DMU_META_DNODE(os));
524ea8dc4b6Seschrock }
525ea8dc4b6Seschrock 
526fa9e4066Sahrens void
527503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
528fa9e4066Sahrens {
529503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
530fa9e4066Sahrens 
531b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
532b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, t));
533fa9e4066Sahrens 
5343baa08fcSek 	if (ds) {
5353baa08fcSek 		if (!dsl_dataset_is_snapshot(ds)) {
5363baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
537503ad85cSMatthew Ahrens 			    checksum_changed_cb, os));
5383baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
539503ad85cSMatthew Ahrens 			    compression_changed_cb, os));
5403baa08fcSek 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
541503ad85cSMatthew Ahrens 			    copies_changed_cb, os));
542b24ab676SJeff Bonwick 			VERIFY(0 == dsl_prop_unregister(ds, "dedup",
543b24ab676SJeff Bonwick 			    dedup_changed_cb, os));
544e09fa4daSNeil Perrin 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
545e09fa4daSNeil Perrin 			    logbias_changed_cb, os));
54655da60b9SMark J Musante 			VERIFY(0 == dsl_prop_unregister(ds, "sync",
54755da60b9SMark J Musante 			    sync_changed_cb, os));
5483baa08fcSek 		}
5493baa08fcSek 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
550503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os));
5513baa08fcSek 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
552503ad85cSMatthew Ahrens 		    secondary_cache_changed_cb, os));
553fa9e4066Sahrens 	}
554fa9e4066Sahrens 
5550a586ceaSMark Shellenbaum 	if (os->os_sa)
5560a586ceaSMark Shellenbaum 		sa_tear_down(os);
5570a586ceaSMark Shellenbaum 
558ea8dc4b6Seschrock 	/*
559ea8dc4b6Seschrock 	 * We should need only a single pass over the dnode list, since
560ea8dc4b6Seschrock 	 * nothing can be added to the list at this point.
561ea8dc4b6Seschrock 	 */
562503ad85cSMatthew Ahrens 	(void) dmu_objset_evict_dbufs(os);
563ea8dc4b6Seschrock 
564744947dcSTom Erickson 	dnode_special_close(&os->os_meta_dnode);
565744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os)) {
566744947dcSTom Erickson 		dnode_special_close(&os->os_userused_dnode);
567744947dcSTom Erickson 		dnode_special_close(&os->os_groupused_dnode);
56814843421SMatthew Ahrens 	}
569503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
570fa9e4066Sahrens 
571503ad85cSMatthew Ahrens 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
57214843421SMatthew Ahrens 
573503ad85cSMatthew Ahrens 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
574744947dcSTom Erickson 
575744947dcSTom Erickson 	/*
576744947dcSTom Erickson 	 * This is a barrier to prevent the objset from going away in
577744947dcSTom Erickson 	 * dnode_move() until we can safely ensure that the objset is still in
578744947dcSTom Erickson 	 * use. We consider the objset valid before the barrier and invalid
579744947dcSTom Erickson 	 * after the barrier.
580744947dcSTom Erickson 	 */
581744947dcSTom Erickson 	rw_enter(&os_lock, RW_READER);
582744947dcSTom Erickson 	rw_exit(&os_lock);
583744947dcSTom Erickson 
584503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
585503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
586503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
587503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
588fa9e4066Sahrens }
589fa9e4066Sahrens 
59071eb0538SChris Kirby timestruc_t
59171eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
59271eb0538SChris Kirby {
59371eb0538SChris Kirby 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
59471eb0538SChris Kirby }
59571eb0538SChris Kirby 
596fa9e4066Sahrens /* called from dsl for meta-objset */
597503ad85cSMatthew Ahrens objset_t *
598c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
599c717a561Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
600fa9e4066Sahrens {
601503ad85cSMatthew Ahrens 	objset_t *os;
602fa9e4066Sahrens 	dnode_t *mdn;
603fa9e4066Sahrens 
604fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
60591ebeef5Sahrens 	if (ds)
60691ebeef5Sahrens 		mutex_enter(&ds->ds_opening_lock);
607503ad85cSMatthew Ahrens 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
60891ebeef5Sahrens 	if (ds)
60991ebeef5Sahrens 		mutex_exit(&ds->ds_opening_lock);
610744947dcSTom Erickson 	mdn = DMU_META_DNODE(os);
611fa9e4066Sahrens 
612fa9e4066Sahrens 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
613fa9e4066Sahrens 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
614fa9e4066Sahrens 
615fa9e4066Sahrens 	/*
616fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
617fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
618fa9e4066Sahrens 	 * we are also accessing it in open context.
619fa9e4066Sahrens 	 *
620fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
621fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
622fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
623fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
624fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
625fa9e4066Sahrens 	 */
626ea8dc4b6Seschrock 	if (ds != NULL) {
627ea8dc4b6Seschrock 		int levels = 1;
628ea8dc4b6Seschrock 
629ea8dc4b6Seschrock 		/*
630ea8dc4b6Seschrock 		 * Determine the number of levels necessary for the meta-dnode
631ea8dc4b6Seschrock 		 * to contain DN_MAX_OBJECT dnodes.
632ea8dc4b6Seschrock 		 */
633ea8dc4b6Seschrock 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
634ea8dc4b6Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
635ea8dc4b6Seschrock 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
636ea8dc4b6Seschrock 			levels++;
637ea8dc4b6Seschrock 
638fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
639ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
640ea8dc4b6Seschrock 	}
641fa9e4066Sahrens 
642fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
643fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
644fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
645503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
646503ad85cSMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
647503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
648503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
64914843421SMatthew Ahrens 	}
650fa9e4066Sahrens 
651fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
652fa9e4066Sahrens 
653503ad85cSMatthew Ahrens 	return (os);
654fa9e4066Sahrens }
655fa9e4066Sahrens 
656fa9e4066Sahrens struct oscarg {
657ecd6cf80Smarks 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
658fa9e4066Sahrens 	void *userarg;
659ae46e4c7SMatthew Ahrens 	dsl_dataset_t *clone_origin;
660fa9e4066Sahrens 	const char *lastname;
661fa9e4066Sahrens 	dmu_objset_type_t type;
662ab04eb8eStimh 	uint64_t flags;
6633f9d6ad7SLin Ling 	cred_t *cr;
664fa9e4066Sahrens };
665fa9e4066Sahrens 
666ecd6cf80Smarks /*ARGSUSED*/
667fa9e4066Sahrens static int
6681d452cf5Sahrens dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
669fa9e4066Sahrens {
6701d452cf5Sahrens 	dsl_dir_t *dd = arg1;
6711d452cf5Sahrens 	struct oscarg *oa = arg2;
6721d452cf5Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
673fa9e4066Sahrens 	int err;
6741d452cf5Sahrens 	uint64_t ddobj;
6751d452cf5Sahrens 
6761d452cf5Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
6771d452cf5Sahrens 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
6781d452cf5Sahrens 	if (err != ENOENT)
6791d452cf5Sahrens 		return (err ? err : EEXIST);
6801d452cf5Sahrens 
681ae46e4c7SMatthew Ahrens 	if (oa->clone_origin != NULL) {
682ae46e4c7SMatthew Ahrens 		/* You can't clone across pools. */
683ae46e4c7SMatthew Ahrens 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
6841d452cf5Sahrens 			return (EXDEV);
6851d452cf5Sahrens 
686ae46e4c7SMatthew Ahrens 		/* You can only clone snapshots, not the head datasets. */
687ae46e4c7SMatthew Ahrens 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
6881d452cf5Sahrens 			return (EINVAL);
6891d452cf5Sahrens 	}
690ecd6cf80Smarks 
6911d452cf5Sahrens 	return (0);
6921d452cf5Sahrens }
6931d452cf5Sahrens 
6941d452cf5Sahrens static void
6953f9d6ad7SLin Ling dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx)
6961d452cf5Sahrens {
6971d452cf5Sahrens 	dsl_dir_t *dd = arg1;
6981d452cf5Sahrens 	struct oscarg *oa = arg2;
6991d452cf5Sahrens 	uint64_t dsobj;
700*6e0cbcaaSMatthew Ahrens 	dsl_dataset_t *ds;
701*6e0cbcaaSMatthew Ahrens 	objset_t *os;
702fa9e4066Sahrens 
703fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
704fa9e4066Sahrens 
7051d452cf5Sahrens 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
7063f9d6ad7SLin Ling 	    oa->clone_origin, oa->flags, oa->cr, tx);
707fa9e4066Sahrens 
708*6e0cbcaaSMatthew Ahrens 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj, FTAG, &ds));
709*6e0cbcaaSMatthew Ahrens 
710ae46e4c7SMatthew Ahrens 	if (oa->clone_origin == NULL) {
711ae46e4c7SMatthew Ahrens 		blkptr_t *bp;
712fa9e4066Sahrens 
713ae46e4c7SMatthew Ahrens 		bp = dsl_dataset_get_blkptr(ds);
714ae46e4c7SMatthew Ahrens 		ASSERT(BP_IS_HOLE(bp));
715ae46e4c7SMatthew Ahrens 
716503ad85cSMatthew Ahrens 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
717c717a561Smaybee 		    ds, bp, oa->type, tx);
718fa9e4066Sahrens 
719fa9e4066Sahrens 		if (oa->userfunc)
7203f9d6ad7SLin Ling 			oa->userfunc(os, oa->userarg, oa->cr, tx);
721*6e0cbcaaSMatthew Ahrens 	} else {
722*6e0cbcaaSMatthew Ahrens 		VERIFY3U(0, ==, dmu_objset_from_ds(ds, &os));
723*6e0cbcaaSMatthew Ahrens 		bzero(&os->os_zil_header, sizeof (os->os_zil_header));
724*6e0cbcaaSMatthew Ahrens 		dsl_dataset_dirty(ds, tx);
725fa9e4066Sahrens 	}
726*6e0cbcaaSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
727ecd6cf80Smarks 
7283f9d6ad7SLin Ling 	spa_history_log_internal(LOG_DS_CREATE, dd->dd_pool->dp_spa,
7293f9d6ad7SLin Ling 	    tx, "dataset = %llu", dsobj);
730fa9e4066Sahrens }
731fa9e4066Sahrens 
732fa9e4066Sahrens int
733ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
734ecd6cf80Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
735fa9e4066Sahrens {
7361d452cf5Sahrens 	dsl_dir_t *pdd;
737fa9e4066Sahrens 	const char *tail;
738fa9e4066Sahrens 	int err = 0;
7391d452cf5Sahrens 	struct oscarg oa = { 0 };
740fa9e4066Sahrens 
7411d452cf5Sahrens 	ASSERT(strchr(name, '@') == NULL);
7421d452cf5Sahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
743ea8dc4b6Seschrock 	if (err)
744ea8dc4b6Seschrock 		return (err);
745fa9e4066Sahrens 	if (tail == NULL) {
7461d452cf5Sahrens 		dsl_dir_close(pdd, FTAG);
747fa9e4066Sahrens 		return (EEXIST);
748fa9e4066Sahrens 	}
749fa9e4066Sahrens 
7501d452cf5Sahrens 	oa.userfunc = func;
7511d452cf5Sahrens 	oa.userarg = arg;
7521d452cf5Sahrens 	oa.lastname = tail;
7531d452cf5Sahrens 	oa.type = type;
754ab04eb8eStimh 	oa.flags = flags;
7553f9d6ad7SLin Ling 	oa.cr = CRED();
756ecd6cf80Smarks 
757ae46e4c7SMatthew Ahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
758ae46e4c7SMatthew Ahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
759ae46e4c7SMatthew Ahrens 	dsl_dir_close(pdd, FTAG);
760ae46e4c7SMatthew Ahrens 	return (err);
761ae46e4c7SMatthew Ahrens }
762ae46e4c7SMatthew Ahrens 
763ae46e4c7SMatthew Ahrens int
764ae46e4c7SMatthew Ahrens dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
765ae46e4c7SMatthew Ahrens {
766ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
767ae46e4c7SMatthew Ahrens 	const char *tail;
768ae46e4c7SMatthew Ahrens 	int err = 0;
769ae46e4c7SMatthew Ahrens 	struct oscarg oa = { 0 };
770ae46e4c7SMatthew Ahrens 
771ae46e4c7SMatthew Ahrens 	ASSERT(strchr(name, '@') == NULL);
772ae46e4c7SMatthew Ahrens 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
773ae46e4c7SMatthew Ahrens 	if (err)
774ae46e4c7SMatthew Ahrens 		return (err);
775ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
776ae46e4c7SMatthew Ahrens 		dsl_dir_close(pdd, FTAG);
777ae46e4c7SMatthew Ahrens 		return (EEXIST);
778fa9e4066Sahrens 	}
779ae46e4c7SMatthew Ahrens 
780ae46e4c7SMatthew Ahrens 	oa.lastname = tail;
781ae46e4c7SMatthew Ahrens 	oa.clone_origin = clone_origin;
782ae46e4c7SMatthew Ahrens 	oa.flags = flags;
7833f9d6ad7SLin Ling 	oa.cr = CRED();
784ae46e4c7SMatthew Ahrens 
7851d452cf5Sahrens 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
7861d452cf5Sahrens 	    dmu_objset_create_sync, pdd, &oa, 5);
7871d452cf5Sahrens 	dsl_dir_close(pdd, FTAG);
788fa9e4066Sahrens 	return (err);
789fa9e4066Sahrens }
790fa9e4066Sahrens 
791fa9e4066Sahrens int
792842727c2SChris Kirby dmu_objset_destroy(const char *name, boolean_t defer)
793fa9e4066Sahrens {
794503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
795fa9e4066Sahrens 	int error;
796fa9e4066Sahrens 
797fa9e4066Sahrens 	/*
798ae46e4c7SMatthew Ahrens 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
799ae46e4c7SMatthew Ahrens 	 * intent log, but if there is an active log, it has blocks that
800ae46e4c7SMatthew Ahrens 	 * are allocated, but may not yet be reflected in the on-disk
801ae46e4c7SMatthew Ahrens 	 * structure.  Only the ZIL knows how to free them, so we have
802ae46e4c7SMatthew Ahrens 	 * to call into it here.
803fa9e4066Sahrens 	 */
804503ad85cSMatthew Ahrens 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
805fa9e4066Sahrens 	if (error == 0) {
806503ad85cSMatthew Ahrens 		objset_t *os;
807503ad85cSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) == 0)
808503ad85cSMatthew Ahrens 			zil_destroy(dmu_objset_zil(os), B_FALSE);
809503ad85cSMatthew Ahrens 		error = dsl_dataset_destroy(ds, FTAG, defer);
810ae46e4c7SMatthew Ahrens 		/* dsl_dataset_destroy() closes the ds. */
811fa9e4066Sahrens 	}
812fa9e4066Sahrens 
8133cb34c60Sahrens 	return (error);
814fa9e4066Sahrens }
815fa9e4066Sahrens 
8161d452cf5Sahrens struct snaparg {
8171d452cf5Sahrens 	dsl_sync_task_group_t *dstg;
8181d452cf5Sahrens 	char *snapname;
8191d452cf5Sahrens 	char failed[MAXPATHLEN];
820d8d780fbSMatthew Ahrens 	boolean_t recursive;
821*6e0cbcaaSMatthew Ahrens 	boolean_t needsuspend;
822ea2f5b9eSMatthew Ahrens 	nvlist_t *props;
8233cb34c60Sahrens };
8243cb34c60Sahrens 
825ea2f5b9eSMatthew Ahrens static int
826ea2f5b9eSMatthew Ahrens snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
827ea2f5b9eSMatthew Ahrens {
828ea2f5b9eSMatthew Ahrens 	objset_t *os = arg1;
829ea2f5b9eSMatthew Ahrens 	struct snaparg *sn = arg2;
830ea2f5b9eSMatthew Ahrens 
831ea2f5b9eSMatthew Ahrens 	/* The props have already been checked by zfs_check_userprops(). */
832ea2f5b9eSMatthew Ahrens 
833503ad85cSMatthew Ahrens 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
834ea2f5b9eSMatthew Ahrens 	    sn->snapname, tx));
835ea2f5b9eSMatthew Ahrens }
836ea2f5b9eSMatthew Ahrens 
837ea2f5b9eSMatthew Ahrens static void
8383f9d6ad7SLin Ling snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)
839ea2f5b9eSMatthew Ahrens {
840ea2f5b9eSMatthew Ahrens 	objset_t *os = arg1;
841503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
842ea2f5b9eSMatthew Ahrens 	struct snaparg *sn = arg2;
843ea2f5b9eSMatthew Ahrens 
8443f9d6ad7SLin Ling 	dsl_dataset_snapshot_sync(ds, sn->snapname, tx);
845ea2f5b9eSMatthew Ahrens 
84692241e0bSTom Erickson 	if (sn->props) {
84792241e0bSTom Erickson 		dsl_props_arg_t pa;
84892241e0bSTom Erickson 		pa.pa_props = sn->props;
84992241e0bSTom Erickson 		pa.pa_source = ZPROP_SRC_LOCAL;
8503f9d6ad7SLin Ling 		dsl_props_set_sync(ds->ds_prev, &pa, tx);
85192241e0bSTom Erickson 	}
852ea2f5b9eSMatthew Ahrens }
8531d452cf5Sahrens 
8541d452cf5Sahrens static int
855fd136879SMatthew Ahrens dmu_objset_snapshot_one(const char *name, void *arg)
8561d452cf5Sahrens {
8571d452cf5Sahrens 	struct snaparg *sn = arg;
8581d452cf5Sahrens 	objset_t *os;
8591d452cf5Sahrens 	int err;
860d8d780fbSMatthew Ahrens 	char *cp;
861d8d780fbSMatthew Ahrens 
862d8d780fbSMatthew Ahrens 	/*
863d8d780fbSMatthew Ahrens 	 * If the objset starts with a '%', then ignore it unless it was
864d8d780fbSMatthew Ahrens 	 * explicitly named (ie, not recursive).  These hidden datasets
865d8d780fbSMatthew Ahrens 	 * are always inconsistent, and by not opening them here, we can
866d8d780fbSMatthew Ahrens 	 * avoid a race with dsl_dir_destroy_check().
867d8d780fbSMatthew Ahrens 	 */
868d8d780fbSMatthew Ahrens 	cp = strrchr(name, '/');
869d8d780fbSMatthew Ahrens 	if (cp && cp[1] == '%' && sn->recursive)
870d8d780fbSMatthew Ahrens 		return (0);
8711d452cf5Sahrens 
8721d452cf5Sahrens 	(void) strcpy(sn->failed, name);
8731d452cf5Sahrens 
874ecd6cf80Smarks 	/*
875d8d780fbSMatthew Ahrens 	 * Check permissions if we are doing a recursive snapshot.  The
876d8d780fbSMatthew Ahrens 	 * permission checks for the starting dataset have already been
877d8d780fbSMatthew Ahrens 	 * performed in zfs_secpolicy_snapshot()
878ecd6cf80Smarks 	 */
879d8d780fbSMatthew Ahrens 	if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED())))
880ecd6cf80Smarks 		return (err);
881ecd6cf80Smarks 
882503ad85cSMatthew Ahrens 	err = dmu_objset_hold(name, sn, &os);
8831d452cf5Sahrens 	if (err != 0)
8841d452cf5Sahrens 		return (err);
8851d452cf5Sahrens 
886d8d780fbSMatthew Ahrens 	/*
887d8d780fbSMatthew Ahrens 	 * If the objset is in an inconsistent state (eg, in the process
888d8d780fbSMatthew Ahrens 	 * of being destroyed), don't snapshot it.  As with %hidden
889d8d780fbSMatthew Ahrens 	 * datasets, we return EBUSY if this name was explicitly
890d8d780fbSMatthew Ahrens 	 * requested (ie, not recursive), and otherwise ignore it.
891d8d780fbSMatthew Ahrens 	 */
892503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
893503ad85cSMatthew Ahrens 		dmu_objset_rele(os, sn);
894d8d780fbSMatthew Ahrens 		return (sn->recursive ? 0 : EBUSY);
895f2e10be3Srm 	}
896f2e10be3Srm 
897*6e0cbcaaSMatthew Ahrens 	if (sn->needsuspend) {
898*6e0cbcaaSMatthew Ahrens 		err = zil_suspend(dmu_objset_zil(os));
899*6e0cbcaaSMatthew Ahrens 		if (err) {
900*6e0cbcaaSMatthew Ahrens 			dmu_objset_rele(os, sn);
901*6e0cbcaaSMatthew Ahrens 			return (err);
902*6e0cbcaaSMatthew Ahrens 		}
9031d452cf5Sahrens 	}
904*6e0cbcaaSMatthew Ahrens 	dsl_sync_task_create(sn->dstg, snapshot_check, snapshot_sync,
905*6e0cbcaaSMatthew Ahrens 	    os, sn, 3);
906f2e10be3Srm 
907*6e0cbcaaSMatthew Ahrens 	return (0);
9081d452cf5Sahrens }
9091d452cf5Sahrens 
9101d452cf5Sahrens int
911ea2f5b9eSMatthew Ahrens dmu_objset_snapshot(char *fsname, char *snapname,
912ea2f5b9eSMatthew Ahrens     nvlist_t *props, boolean_t recursive)
9131d452cf5Sahrens {
9141d452cf5Sahrens 	dsl_sync_task_t *dst;
915ea2f5b9eSMatthew Ahrens 	struct snaparg sn;
9161d452cf5Sahrens 	spa_t *spa;
9171d452cf5Sahrens 	int err;
9181d452cf5Sahrens 
9191d452cf5Sahrens 	(void) strcpy(sn.failed, fsname);
9201d452cf5Sahrens 
92140feaa91Sahrens 	err = spa_open(fsname, &spa, FTAG);
9221d452cf5Sahrens 	if (err)
9231d452cf5Sahrens 		return (err);
9241d452cf5Sahrens 
9251d452cf5Sahrens 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
9261d452cf5Sahrens 	sn.snapname = snapname;
927ea2f5b9eSMatthew Ahrens 	sn.props = props;
928d8d780fbSMatthew Ahrens 	sn.recursive = recursive;
929*6e0cbcaaSMatthew Ahrens 	sn.needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
9301d452cf5Sahrens 
9310b69c2f0Sahrens 	if (recursive) {
9320b69c2f0Sahrens 		err = dmu_objset_find(fsname,
9330b69c2f0Sahrens 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
9340b69c2f0Sahrens 	} else {
9351d452cf5Sahrens 		err = dmu_objset_snapshot_one(fsname, &sn);
9360b69c2f0Sahrens 	}
9371d452cf5Sahrens 
938ea2f5b9eSMatthew Ahrens 	if (err == 0)
939ea2f5b9eSMatthew Ahrens 		err = dsl_sync_task_group_wait(sn.dstg);
9401d452cf5Sahrens 
9411d452cf5Sahrens 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
9421d452cf5Sahrens 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
943ea2f5b9eSMatthew Ahrens 		objset_t *os = dst->dst_arg1;
944503ad85cSMatthew Ahrens 		dsl_dataset_t *ds = os->os_dsl_dataset;
9451d452cf5Sahrens 		if (dst->dst_err)
9463cb34c60Sahrens 			dsl_dataset_name(ds, sn.failed);
947*6e0cbcaaSMatthew Ahrens 		if (sn.needsuspend)
948*6e0cbcaaSMatthew Ahrens 			zil_resume(dmu_objset_zil(os));
949503ad85cSMatthew Ahrens 		dmu_objset_rele(os, &sn);
9503cb34c60Sahrens 	}
9513cb34c60Sahrens 
9521d452cf5Sahrens 	if (err)
9531d452cf5Sahrens 		(void) strcpy(fsname, sn.failed);
9541d452cf5Sahrens 	dsl_sync_task_group_destroy(sn.dstg);
9551d452cf5Sahrens 	spa_close(spa, FTAG);
9561d452cf5Sahrens 	return (err);
9571d452cf5Sahrens }
9581d452cf5Sahrens 
959fa9e4066Sahrens static void
96014843421SMatthew Ahrens dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
961fa9e4066Sahrens {
962c717a561Smaybee 	dnode_t *dn;
963faafa6e3Sahrens 
964c717a561Smaybee 	while (dn = list_head(list)) {
965c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
966c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
967c717a561Smaybee 		/*
96814843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
96914843421SMatthew Ahrens 		 * meta-dnode needs to set it ouside dnode_sync().
970c717a561Smaybee 		 */
971c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
972c717a561Smaybee 		ASSERT(dn->dn_zio);
973faafa6e3Sahrens 
974c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
975c717a561Smaybee 		list_remove(list, dn);
97614843421SMatthew Ahrens 
97714843421SMatthew Ahrens 		if (newlist) {
97814843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
97914843421SMatthew Ahrens 			list_insert_tail(newlist, dn);
98014843421SMatthew Ahrens 		}
98114843421SMatthew Ahrens 
982c717a561Smaybee 		dnode_sync(dn, tx);
983fa9e4066Sahrens 	}
984fa9e4066Sahrens }
985fa9e4066Sahrens 
986fa9e4066Sahrens /* ARGSUSED */
987fa9e4066Sahrens static void
988b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
989fa9e4066Sahrens {
990e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
991503ad85cSMatthew Ahrens 	objset_t *os = arg;
992c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
993fa9e4066Sahrens 
994e14bb325SJeff Bonwick 	ASSERT(bp == os->os_rootbp);
995e14bb325SJeff Bonwick 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
996e14bb325SJeff Bonwick 	ASSERT(BP_GET_LEVEL(bp) == 0);
9970a4e9518Sgw 
998fa9e4066Sahrens 	/*
99914843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
100014843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
100114843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
100214843421SMatthew Ahrens 	 * dnode and user/group accounting objects).
1003fa9e4066Sahrens 	 */
100414843421SMatthew Ahrens 	bp->blk_fill = 0;
1005e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
1006c717a561Smaybee 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
1007b24ab676SJeff Bonwick }
1008b24ab676SJeff Bonwick 
1009b24ab676SJeff Bonwick /* ARGSUSED */
1010b24ab676SJeff Bonwick static void
1011b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1012b24ab676SJeff Bonwick {
1013b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1014b24ab676SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
1015b24ab676SJeff Bonwick 	objset_t *os = arg;
10160a4e9518Sgw 
1017e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1018b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, bp_orig));
1019e14bb325SJeff Bonwick 	} else {
1020b24ab676SJeff Bonwick 		dsl_dataset_t *ds = os->os_dsl_dataset;
1021b24ab676SJeff Bonwick 		dmu_tx_t *tx = os->os_synctx;
1022b24ab676SJeff Bonwick 
1023b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1024b24ab676SJeff Bonwick 		dsl_dataset_block_born(ds, bp, tx);
10250a4e9518Sgw 	}
1026c717a561Smaybee }
1027c717a561Smaybee 
1028fa9e4066Sahrens /* called from dsl */
1029fa9e4066Sahrens void
1030503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1031fa9e4066Sahrens {
1032fa9e4066Sahrens 	int txgoff;
1033ea8dc4b6Seschrock 	zbookmark_t zb;
1034b24ab676SJeff Bonwick 	zio_prop_t zp;
1035c717a561Smaybee 	zio_t *zio;
1036c717a561Smaybee 	list_t *list;
103714843421SMatthew Ahrens 	list_t *newlist = NULL;
1038c717a561Smaybee 	dbuf_dirty_record_t *dr;
1039c717a561Smaybee 
1040c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1041fa9e4066Sahrens 
1042fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1043fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
1044fa9e4066Sahrens 	os->os_synctx = tx;
1045fa9e4066Sahrens 
104687bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
104787bd5c1eSahrens 		/*
104887bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
104987bd5c1eSahrens 		 * spa_max_replication() could change, so reset
105087bd5c1eSahrens 		 * os_copies here.
105187bd5c1eSahrens 		 */
105287bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
105387bd5c1eSahrens 	}
105487bd5c1eSahrens 
1055fa9e4066Sahrens 	/*
1056c717a561Smaybee 	 * Create the root block IO
1057fa9e4066Sahrens 	 */
1058b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1059b24ab676SJeff Bonwick 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1060b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
10613f9d6ad7SLin Ling 	VERIFY3U(0, ==, arc_release_bp(os->os_phys_buf, &os->os_phys_buf,
10623f9d6ad7SLin Ling 	    os->os_rootbp, os->os_spa, &zb));
1063b24ab676SJeff Bonwick 
1064b24ab676SJeff Bonwick 	dmu_write_policy(os, NULL, 0, 0, &zp);
1065b24ab676SJeff Bonwick 
1066b24ab676SJeff Bonwick 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1067b24ab676SJeff Bonwick 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
1068b24ab676SJeff Bonwick 	    dmu_objset_write_ready, dmu_objset_write_done, os,
1069e14bb325SJeff Bonwick 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1070c717a561Smaybee 
1071c717a561Smaybee 	/*
107214843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
1073c717a561Smaybee 	 */
1074744947dcSTom Erickson 	DMU_META_DNODE(os)->dn_zio = zio;
1075744947dcSTom Erickson 	dnode_sync(DMU_META_DNODE(os), tx);
1076c717a561Smaybee 
107714843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
107814843421SMatthew Ahrens 
1079744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) &&
1080744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1081744947dcSTom Erickson 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1082744947dcSTom Erickson 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1083744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1084744947dcSTom Erickson 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
108514843421SMatthew Ahrens 	}
108614843421SMatthew Ahrens 
1087c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
1088fa9e4066Sahrens 
108914843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
109014843421SMatthew Ahrens 		newlist = &os->os_synced_dnodes;
109114843421SMatthew Ahrens 		/*
109214843421SMatthew Ahrens 		 * We must create the list here because it uses the
109314843421SMatthew Ahrens 		 * dn_dirty_link[] of this txg.
109414843421SMatthew Ahrens 		 */
109514843421SMatthew Ahrens 		list_create(newlist, sizeof (dnode_t),
109614843421SMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
109714843421SMatthew Ahrens 	}
109814843421SMatthew Ahrens 
109914843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
110014843421SMatthew Ahrens 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1101fa9e4066Sahrens 
1102744947dcSTom Erickson 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1103c717a561Smaybee 	while (dr = list_head(list)) {
1104c717a561Smaybee 		ASSERT(dr->dr_dbuf->db_level == 0);
1105c717a561Smaybee 		list_remove(list, dr);
1106c717a561Smaybee 		if (dr->dr_zio)
1107c717a561Smaybee 			zio_nowait(dr->dr_zio);
1108c717a561Smaybee 	}
1109c717a561Smaybee 	/*
1110c717a561Smaybee 	 * Free intent log blocks up to this tx.
1111c717a561Smaybee 	 */
1112c717a561Smaybee 	zil_sync(os->os_zil, tx);
1113088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
1114c717a561Smaybee 	zio_nowait(zio);
1115fa9e4066Sahrens }
1116fa9e4066Sahrens 
1117b24ab676SJeff Bonwick boolean_t
1118b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1119b24ab676SJeff Bonwick {
1120b24ab676SJeff Bonwick 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1121b24ab676SJeff Bonwick 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1122b24ab676SJeff Bonwick }
1123b24ab676SJeff Bonwick 
11245aaeed5cSSanjeev Bagewadi boolean_t
11255aaeed5cSSanjeev Bagewadi dmu_objset_is_dirty_anywhere(objset_t *os)
11265aaeed5cSSanjeev Bagewadi {
11275aaeed5cSSanjeev Bagewadi 	for (int t = 0; t < TXG_SIZE; t++)
11285aaeed5cSSanjeev Bagewadi 		if (dmu_objset_is_dirty(os, t))
11295aaeed5cSSanjeev Bagewadi 			return (B_TRUE);
11305aaeed5cSSanjeev Bagewadi 	return (B_FALSE);
11315aaeed5cSSanjeev Bagewadi }
11325aaeed5cSSanjeev Bagewadi 
1133744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
113414843421SMatthew Ahrens 
113514843421SMatthew Ahrens void
113614843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
113714843421SMatthew Ahrens {
113814843421SMatthew Ahrens 	used_cbs[ost] = cb;
113914843421SMatthew Ahrens }
114014843421SMatthew Ahrens 
114114843421SMatthew Ahrens boolean_t
1142503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
114314843421SMatthew Ahrens {
114414843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1145744947dcSTom Erickson 	    used_cbs[os->os_phys->os_type] != NULL &&
1146744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os) != NULL);
114714843421SMatthew Ahrens }
114814843421SMatthew Ahrens 
11499966ca11SMatthew Ahrens static void
11500a586ceaSMark Shellenbaum do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
11510a586ceaSMark Shellenbaum     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
11529966ca11SMatthew Ahrens {
11530a586ceaSMark Shellenbaum 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
11540a586ceaSMark Shellenbaum 		int64_t delta = DNODE_SIZE + used;
11559966ca11SMatthew Ahrens 		if (subtract)
11569966ca11SMatthew Ahrens 			delta = -delta;
1157c33e334fSMatthew Ahrens 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
11589966ca11SMatthew Ahrens 		    user, delta, tx));
1159c33e334fSMatthew Ahrens 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
11609966ca11SMatthew Ahrens 		    group, delta, tx));
11619966ca11SMatthew Ahrens 	}
11629966ca11SMatthew Ahrens }
11639966ca11SMatthew Ahrens 
116414843421SMatthew Ahrens void
11650a586ceaSMark Shellenbaum dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
116614843421SMatthew Ahrens {
116714843421SMatthew Ahrens 	dnode_t *dn;
116814843421SMatthew Ahrens 	list_t *list = &os->os_synced_dnodes;
116914843421SMatthew Ahrens 
117014843421SMatthew Ahrens 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
117114843421SMatthew Ahrens 
117214843421SMatthew Ahrens 	while (dn = list_head(list)) {
11731d8ccc7bSMark Shellenbaum 		int flags;
117414843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
117514843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
117614843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
117714843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
117814843421SMatthew Ahrens 
117914843421SMatthew Ahrens 		/* Allocate the user/groupused objects if necessary. */
1180744947dcSTom Erickson 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1181503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
118214843421SMatthew Ahrens 			    DMU_USERUSED_OBJECT,
118314843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1184503ad85cSMatthew Ahrens 			VERIFY(0 == zap_create_claim(os,
118514843421SMatthew Ahrens 			    DMU_GROUPUSED_OBJECT,
118614843421SMatthew Ahrens 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
118714843421SMatthew Ahrens 		}
118814843421SMatthew Ahrens 
118914843421SMatthew Ahrens 		/*
11909966ca11SMatthew Ahrens 		 * We intentionally modify the zap object even if the
11910a586ceaSMark Shellenbaum 		 * net delta is zero.  Otherwise
11929966ca11SMatthew Ahrens 		 * the block of the zap obj could be shared between
11939966ca11SMatthew Ahrens 		 * datasets but need to be different between them after
11949966ca11SMatthew Ahrens 		 * a bprewrite.
119514843421SMatthew Ahrens 		 */
119614843421SMatthew Ahrens 
11971d8ccc7bSMark Shellenbaum 		flags = dn->dn_id_flags;
11981d8ccc7bSMark Shellenbaum 		ASSERT(flags);
11991d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST)  {
12000a586ceaSMark Shellenbaum 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
12010a586ceaSMark Shellenbaum 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
12020a586ceaSMark Shellenbaum 		}
12031d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_NEW_EXIST) {
12040a586ceaSMark Shellenbaum 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
12050a586ceaSMark Shellenbaum 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
12060a586ceaSMark Shellenbaum 			    dn->dn_newgid, B_FALSE, tx);
12070a586ceaSMark Shellenbaum 		}
12080a586ceaSMark Shellenbaum 
12091d8ccc7bSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
12100a586ceaSMark Shellenbaum 		dn->dn_oldused = 0;
12110a586ceaSMark Shellenbaum 		dn->dn_oldflags = 0;
12120a586ceaSMark Shellenbaum 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
12130a586ceaSMark Shellenbaum 			dn->dn_olduid = dn->dn_newuid;
12140a586ceaSMark Shellenbaum 			dn->dn_oldgid = dn->dn_newgid;
12150a586ceaSMark Shellenbaum 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
12160a586ceaSMark Shellenbaum 			if (dn->dn_bonuslen == 0)
12170a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
12180a586ceaSMark Shellenbaum 			else
12190a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
12200a586ceaSMark Shellenbaum 		}
122128d97a71SMark Shellenbaum 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
122214843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
122314843421SMatthew Ahrens 
122414843421SMatthew Ahrens 		list_remove(list, dn);
122514843421SMatthew Ahrens 		dnode_rele(dn, list);
122614843421SMatthew Ahrens 	}
122714843421SMatthew Ahrens }
122814843421SMatthew Ahrens 
122906e0070dSMark Shellenbaum /*
123006e0070dSMark Shellenbaum  * Returns a pointer to data to find uid/gid from
123106e0070dSMark Shellenbaum  *
123206e0070dSMark Shellenbaum  * If a dirty record for transaction group that is syncing can't
123306e0070dSMark Shellenbaum  * be found then NULL is returned.  In the NULL case it is assumed
123406e0070dSMark Shellenbaum  * the uid/gid aren't changing.
123506e0070dSMark Shellenbaum  */
123606e0070dSMark Shellenbaum static void *
123706e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
123806e0070dSMark Shellenbaum {
123906e0070dSMark Shellenbaum 	dbuf_dirty_record_t *dr, **drp;
124006e0070dSMark Shellenbaum 	void *data;
124106e0070dSMark Shellenbaum 
124206e0070dSMark Shellenbaum 	if (db->db_dirtycnt == 0)
124306e0070dSMark Shellenbaum 		return (db->db.db_data);  /* Nothing is changing */
124406e0070dSMark Shellenbaum 
124506e0070dSMark Shellenbaum 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
124606e0070dSMark Shellenbaum 		if (dr->dr_txg == tx->tx_txg)
124706e0070dSMark Shellenbaum 			break;
124806e0070dSMark Shellenbaum 
1249744947dcSTom Erickson 	if (dr == NULL) {
125006e0070dSMark Shellenbaum 		data = NULL;
1251744947dcSTom Erickson 	} else {
1252744947dcSTom Erickson 		dnode_t *dn;
1253744947dcSTom Erickson 
1254744947dcSTom Erickson 		DB_DNODE_ENTER(dr->dr_dbuf);
1255744947dcSTom Erickson 		dn = DB_DNODE(dr->dr_dbuf);
1256744947dcSTom Erickson 
1257744947dcSTom Erickson 		if (dn->dn_bonuslen == 0 &&
1258744947dcSTom Erickson 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1259744947dcSTom Erickson 			data = dr->dt.dl.dr_data->b_data;
1260744947dcSTom Erickson 		else
1261744947dcSTom Erickson 			data = dr->dt.dl.dr_data;
1262744947dcSTom Erickson 
1263744947dcSTom Erickson 		DB_DNODE_EXIT(dr->dr_dbuf);
1264744947dcSTom Erickson 	}
1265744947dcSTom Erickson 
126606e0070dSMark Shellenbaum 	return (data);
126706e0070dSMark Shellenbaum }
126806e0070dSMark Shellenbaum 
12690a586ceaSMark Shellenbaum void
127006e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
12710a586ceaSMark Shellenbaum {
12720a586ceaSMark Shellenbaum 	objset_t *os = dn->dn_objset;
12730a586ceaSMark Shellenbaum 	void *data = NULL;
127406e0070dSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
12750a586ceaSMark Shellenbaum 	uint64_t *user, *group;
12760a586ceaSMark Shellenbaum 	int flags = dn->dn_id_flags;
12770a586ceaSMark Shellenbaum 	int error;
127806e0070dSMark Shellenbaum 	boolean_t have_spill = B_FALSE;
12790a586ceaSMark Shellenbaum 
12800a586ceaSMark Shellenbaum 	if (!dmu_objset_userused_enabled(dn->dn_objset))
12810a586ceaSMark Shellenbaum 		return;
12820a586ceaSMark Shellenbaum 
12830a586ceaSMark Shellenbaum 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
12840a586ceaSMark Shellenbaum 	    DN_ID_CHKED_SPILL)))
12850a586ceaSMark Shellenbaum 		return;
12860a586ceaSMark Shellenbaum 
12870a586ceaSMark Shellenbaum 	if (before && dn->dn_bonuslen != 0)
12880a586ceaSMark Shellenbaum 		data = DN_BONUS(dn->dn_phys);
128906e0070dSMark Shellenbaum 	else if (!before && dn->dn_bonuslen != 0) {
129006e0070dSMark Shellenbaum 		if (dn->dn_bonus) {
129106e0070dSMark Shellenbaum 			db = dn->dn_bonus;
129206e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
129306e0070dSMark Shellenbaum 			data = dmu_objset_userquota_find_data(db, tx);
129406e0070dSMark Shellenbaum 		} else {
129506e0070dSMark Shellenbaum 			data = DN_BONUS(dn->dn_phys);
129606e0070dSMark Shellenbaum 		}
129706e0070dSMark Shellenbaum 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
12980a586ceaSMark Shellenbaum 			int rf = 0;
12990a586ceaSMark Shellenbaum 
13000a586ceaSMark Shellenbaum 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
13010a586ceaSMark Shellenbaum 				rf |= DB_RF_HAVESTRUCT;
13021d8ccc7bSMark Shellenbaum 			error = dmu_spill_hold_by_dnode(dn,
13031d8ccc7bSMark Shellenbaum 			    rf | DB_RF_MUST_SUCCEED,
130406e0070dSMark Shellenbaum 			    FTAG, (dmu_buf_t **)&db);
13050a586ceaSMark Shellenbaum 			ASSERT(error == 0);
130606e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
130706e0070dSMark Shellenbaum 			data = (before) ? db->db.db_data :
130806e0070dSMark Shellenbaum 			    dmu_objset_userquota_find_data(db, tx);
130906e0070dSMark Shellenbaum 			have_spill = B_TRUE;
13100a586ceaSMark Shellenbaum 	} else {
13110a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
13120a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13130a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
13140a586ceaSMark Shellenbaum 		return;
13150a586ceaSMark Shellenbaum 	}
13160a586ceaSMark Shellenbaum 
13170a586ceaSMark Shellenbaum 	if (before) {
131806e0070dSMark Shellenbaum 		ASSERT(data);
13190a586ceaSMark Shellenbaum 		user = &dn->dn_olduid;
13200a586ceaSMark Shellenbaum 		group = &dn->dn_oldgid;
132106e0070dSMark Shellenbaum 	} else if (data) {
13220a586ceaSMark Shellenbaum 		user = &dn->dn_newuid;
13230a586ceaSMark Shellenbaum 		group = &dn->dn_newgid;
13240a586ceaSMark Shellenbaum 	}
13250a586ceaSMark Shellenbaum 
132606e0070dSMark Shellenbaum 	/*
132706e0070dSMark Shellenbaum 	 * Must always call the callback in case the object
132806e0070dSMark Shellenbaum 	 * type has changed and that type isn't an object type to track
132906e0070dSMark Shellenbaum 	 */
13300a586ceaSMark Shellenbaum 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
13310a586ceaSMark Shellenbaum 	    user, group);
13320a586ceaSMark Shellenbaum 
133306e0070dSMark Shellenbaum 	/*
133406e0070dSMark Shellenbaum 	 * Preserve existing uid/gid when the callback can't determine
133506e0070dSMark Shellenbaum 	 * what the new uid/gid are and the callback returned EEXIST.
133606e0070dSMark Shellenbaum 	 * The EEXIST error tells us to just use the existing uid/gid.
133706e0070dSMark Shellenbaum 	 * If we don't know what the old values are then just assign
133806e0070dSMark Shellenbaum 	 * them to 0, since that is a new file  being created.
133906e0070dSMark Shellenbaum 	 */
134006e0070dSMark Shellenbaum 	if (!before && data == NULL && error == EEXIST) {
134106e0070dSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST) {
134206e0070dSMark Shellenbaum 			dn->dn_newuid = dn->dn_olduid;
134306e0070dSMark Shellenbaum 			dn->dn_newgid = dn->dn_oldgid;
134406e0070dSMark Shellenbaum 		} else {
134506e0070dSMark Shellenbaum 			dn->dn_newuid = 0;
134606e0070dSMark Shellenbaum 			dn->dn_newgid = 0;
134706e0070dSMark Shellenbaum 		}
134806e0070dSMark Shellenbaum 		error = 0;
134906e0070dSMark Shellenbaum 	}
135006e0070dSMark Shellenbaum 
135106e0070dSMark Shellenbaum 	if (db)
135206e0070dSMark Shellenbaum 		mutex_exit(&db->db_mtx);
135306e0070dSMark Shellenbaum 
13540a586ceaSMark Shellenbaum 	mutex_enter(&dn->dn_mtx);
13550a586ceaSMark Shellenbaum 	if (error == 0 && before)
13560a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
13570a586ceaSMark Shellenbaum 	if (error == 0 && !before)
13580a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
13590a586ceaSMark Shellenbaum 
136006e0070dSMark Shellenbaum 	if (have_spill) {
13610a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
13620a586ceaSMark Shellenbaum 	} else {
13630a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
13640a586ceaSMark Shellenbaum 	}
13650a586ceaSMark Shellenbaum 	mutex_exit(&dn->dn_mtx);
136606e0070dSMark Shellenbaum 	if (have_spill)
136706e0070dSMark Shellenbaum 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
13680a586ceaSMark Shellenbaum }
13690a586ceaSMark Shellenbaum 
137014843421SMatthew Ahrens boolean_t
137114843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
137214843421SMatthew Ahrens {
1373503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
137414843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
137514843421SMatthew Ahrens }
137614843421SMatthew Ahrens 
137714843421SMatthew Ahrens int
137814843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
137914843421SMatthew Ahrens {
138014843421SMatthew Ahrens 	uint64_t obj;
138114843421SMatthew Ahrens 	int err = 0;
138214843421SMatthew Ahrens 
138314843421SMatthew Ahrens 	if (dmu_objset_userspace_present(os))
138414843421SMatthew Ahrens 		return (0);
1385503ad85cSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
138614843421SMatthew Ahrens 		return (ENOTSUP);
138714843421SMatthew Ahrens 	if (dmu_objset_is_snapshot(os))
138814843421SMatthew Ahrens 		return (EINVAL);
138914843421SMatthew Ahrens 
139014843421SMatthew Ahrens 	/*
139114843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
139214843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
139314843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
139414843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
139514843421SMatthew Ahrens 	 * independently.
139614843421SMatthew Ahrens 	 */
139714843421SMatthew Ahrens 
139814843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
139968857716SLin Ling 		dmu_tx_t *tx;
140014843421SMatthew Ahrens 		dmu_buf_t *db;
140114843421SMatthew Ahrens 		int objerr;
140214843421SMatthew Ahrens 
140314843421SMatthew Ahrens 		if (issig(JUSTLOOKING) && issig(FORREAL))
140414843421SMatthew Ahrens 			return (EINTR);
140514843421SMatthew Ahrens 
140614843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
140714843421SMatthew Ahrens 		if (objerr)
140814843421SMatthew Ahrens 			continue;
140968857716SLin Ling 		tx = dmu_tx_create(os);
141014843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
141114843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
141214843421SMatthew Ahrens 		if (objerr) {
141314843421SMatthew Ahrens 			dmu_tx_abort(tx);
141414843421SMatthew Ahrens 			continue;
141514843421SMatthew Ahrens 		}
141614843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
141714843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
141814843421SMatthew Ahrens 		dmu_tx_commit(tx);
141914843421SMatthew Ahrens 	}
142014843421SMatthew Ahrens 
1421503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
142214843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
142314843421SMatthew Ahrens 	return (0);
142414843421SMatthew Ahrens }
142514843421SMatthew Ahrens 
1426fa9e4066Sahrens void
1427a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1428a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1429fa9e4066Sahrens {
1430503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1431a2eea2e1Sahrens 	    usedobjsp, availobjsp);
1432a2eea2e1Sahrens }
1433a2eea2e1Sahrens 
1434a2eea2e1Sahrens uint64_t
1435a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1436a2eea2e1Sahrens {
1437503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1438a2eea2e1Sahrens }
1439a2eea2e1Sahrens 
1440a2eea2e1Sahrens void
1441a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1442a2eea2e1Sahrens {
1443503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
1444503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
1445503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1446a2eea2e1Sahrens }
1447a2eea2e1Sahrens 
1448a2eea2e1Sahrens void
1449a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1450a2eea2e1Sahrens {
1451503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
1452503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
1453a2eea2e1Sahrens 
1454503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1455503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1456a2eea2e1Sahrens 
1457a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1458503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
145914843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
146014843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
1461fa9e4066Sahrens }
1462fa9e4066Sahrens 
1463fa9e4066Sahrens int
1464fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1465fa9e4066Sahrens {
1466503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1467503ad85cSMatthew Ahrens 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1468fa9e4066Sahrens 	else
1469fa9e4066Sahrens 		return (B_FALSE);
1470fa9e4066Sahrens }
1471fa9e4066Sahrens 
1472ab04eb8eStimh int
1473ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1474ab04eb8eStimh     boolean_t *conflict)
1475ab04eb8eStimh {
1476503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1477ab04eb8eStimh 	uint64_t ignored;
1478ab04eb8eStimh 
1479ab04eb8eStimh 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1480ab04eb8eStimh 		return (ENOENT);
1481ab04eb8eStimh 
1482ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1483ab04eb8eStimh 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1484ab04eb8eStimh 	    real, maxlen, conflict));
1485ab04eb8eStimh }
1486ab04eb8eStimh 
1487fa9e4066Sahrens int
1488fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1489b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1490fa9e4066Sahrens {
1491503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1492fa9e4066Sahrens 	zap_cursor_t cursor;
1493fa9e4066Sahrens 	zap_attribute_t attr;
1494fa9e4066Sahrens 
1495fa9e4066Sahrens 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1496fa9e4066Sahrens 		return (ENOENT);
1497fa9e4066Sahrens 
1498fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
1499fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1500fa9e4066Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1501fa9e4066Sahrens 
150287e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
150387e5029aSahrens 		zap_cursor_fini(&cursor);
150487e5029aSahrens 		return (ENOENT);
150587e5029aSahrens 	}
150687e5029aSahrens 
150787e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
150887e5029aSahrens 		zap_cursor_fini(&cursor);
150987e5029aSahrens 		return (ENAMETOOLONG);
151087e5029aSahrens 	}
151187e5029aSahrens 
151287e5029aSahrens 	(void) strcpy(name, attr.za_name);
151387e5029aSahrens 	if (idp)
151487e5029aSahrens 		*idp = attr.za_first_integer;
1515b38f0970Sck 	if (case_conflict)
1516b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
151787e5029aSahrens 	zap_cursor_advance(&cursor);
151887e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
151987e5029aSahrens 	zap_cursor_fini(&cursor);
152087e5029aSahrens 
152187e5029aSahrens 	return (0);
152287e5029aSahrens }
152387e5029aSahrens 
152487e5029aSahrens int
152587e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
152687e5029aSahrens     uint64_t *idp, uint64_t *offp)
152787e5029aSahrens {
1528503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
152987e5029aSahrens 	zap_cursor_t cursor;
153087e5029aSahrens 	zap_attribute_t attr;
153187e5029aSahrens 
153287e5029aSahrens 	/* there is no next dir on a snapshot! */
1533503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
153487e5029aSahrens 	    dd->dd_phys->dd_head_dataset_obj)
1535fa9e4066Sahrens 		return (ENOENT);
1536fa9e4066Sahrens 
153787e5029aSahrens 	zap_cursor_init_serialized(&cursor,
153887e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
153987e5029aSahrens 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
154087e5029aSahrens 
154187e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
154287e5029aSahrens 		zap_cursor_fini(&cursor);
154387e5029aSahrens 		return (ENOENT);
154487e5029aSahrens 	}
154587e5029aSahrens 
154687e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
154787e5029aSahrens 		zap_cursor_fini(&cursor);
1548fa9e4066Sahrens 		return (ENAMETOOLONG);
154987e5029aSahrens 	}
1550fa9e4066Sahrens 
1551fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
155287e5029aSahrens 	if (idp)
155387e5029aSahrens 		*idp = attr.za_first_integer;
1554fa9e4066Sahrens 	zap_cursor_advance(&cursor);
1555fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
155687e5029aSahrens 	zap_cursor_fini(&cursor);
1557fa9e4066Sahrens 
1558fa9e4066Sahrens 	return (0);
1559fa9e4066Sahrens }
1560fa9e4066Sahrens 
1561088f3894Sahrens struct findarg {
1562fd136879SMatthew Ahrens 	int (*func)(const char *, void *);
1563088f3894Sahrens 	void *arg;
1564088f3894Sahrens };
1565088f3894Sahrens 
1566088f3894Sahrens /* ARGSUSED */
1567088f3894Sahrens static int
1568088f3894Sahrens findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1569088f3894Sahrens {
1570088f3894Sahrens 	struct findarg *fa = arg;
1571fd136879SMatthew Ahrens 	return (fa->func(dsname, fa->arg));
1572088f3894Sahrens }
1573088f3894Sahrens 
1574fa9e4066Sahrens /*
1575fa9e4066Sahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1576088f3894Sahrens  * Perhaps change all callers to use dmu_objset_find_spa()?
1577fa9e4066Sahrens  */
15781d452cf5Sahrens int
1579fd136879SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg,
1580fd136879SMatthew Ahrens     int flags)
1581088f3894Sahrens {
1582088f3894Sahrens 	struct findarg fa;
1583088f3894Sahrens 	fa.func = func;
1584088f3894Sahrens 	fa.arg = arg;
1585088f3894Sahrens 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
1586088f3894Sahrens }
1587088f3894Sahrens 
1588088f3894Sahrens /*
1589088f3894Sahrens  * Find all objsets under name, call func on each
1590088f3894Sahrens  */
1591088f3894Sahrens int
1592088f3894Sahrens dmu_objset_find_spa(spa_t *spa, const char *name,
1593088f3894Sahrens     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
1594fa9e4066Sahrens {
1595fa9e4066Sahrens 	dsl_dir_t *dd;
1596088f3894Sahrens 	dsl_pool_t *dp;
1597088f3894Sahrens 	dsl_dataset_t *ds;
1598fa9e4066Sahrens 	zap_cursor_t zc;
1599b7661cccSmmusante 	zap_attribute_t *attr;
1600fa9e4066Sahrens 	char *child;
1601088f3894Sahrens 	uint64_t thisobj;
1602088f3894Sahrens 	int err;
1603fa9e4066Sahrens 
1604088f3894Sahrens 	if (name == NULL)
1605088f3894Sahrens 		name = spa_name(spa);
1606088f3894Sahrens 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
1607ea8dc4b6Seschrock 	if (err)
16081d452cf5Sahrens 		return (err);
1609fa9e4066Sahrens 
1610088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1611088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
1612088f3894Sahrens 		dsl_dir_close(dd, FTAG);
1613088f3894Sahrens 		return (0);
1614088f3894Sahrens 	}
1615088f3894Sahrens 
1616088f3894Sahrens 	thisobj = dd->dd_phys->dd_head_dataset_obj;
1617b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1618088f3894Sahrens 	dp = dd->dd_pool;
1619fa9e4066Sahrens 
1620fa9e4066Sahrens 	/*
1621fa9e4066Sahrens 	 * Iterate over all children.
1622fa9e4066Sahrens 	 */
16230b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
1624088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
16250b69c2f0Sahrens 		    dd->dd_phys->dd_child_dir_zapobj);
1626b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
16270b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
1628b7661cccSmmusante 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
1629b7661cccSmmusante 			ASSERT(attr->za_num_integers == 1);
1630fa9e4066Sahrens 
1631486ae710SMatthew Ahrens 			child = kmem_asprintf("%s/%s", name, attr->za_name);
1632088f3894Sahrens 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
1633486ae710SMatthew Ahrens 			strfree(child);
16340b69c2f0Sahrens 			if (err)
16350b69c2f0Sahrens 				break;
16360b69c2f0Sahrens 		}
16370b69c2f0Sahrens 		zap_cursor_fini(&zc);
16381d452cf5Sahrens 
16390b69c2f0Sahrens 		if (err) {
16400b69c2f0Sahrens 			dsl_dir_close(dd, FTAG);
1641b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
16420b69c2f0Sahrens 			return (err);
16430b69c2f0Sahrens 		}
1644fa9e4066Sahrens 	}
1645fa9e4066Sahrens 
1646fa9e4066Sahrens 	/*
1647fa9e4066Sahrens 	 * Iterate over all snapshots.
1648fa9e4066Sahrens 	 */
1649088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
1650088f3894Sahrens 		if (!dsl_pool_sync_context(dp))
1651088f3894Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
1652088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1653088f3894Sahrens 		if (!dsl_pool_sync_context(dp))
1654088f3894Sahrens 			rw_exit(&dp->dp_config_rwlock);
1655088f3894Sahrens 
1656088f3894Sahrens 		if (err == 0) {
1657088f3894Sahrens 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1658088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
1659088f3894Sahrens 
1660088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1661088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
1662088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
1663088f3894Sahrens 				ASSERT(attr->za_integer_length ==
1664088f3894Sahrens 				    sizeof (uint64_t));
1665088f3894Sahrens 				ASSERT(attr->za_num_integers == 1);
1666088f3894Sahrens 
1667486ae710SMatthew Ahrens 				child = kmem_asprintf("%s@%s",
1668486ae710SMatthew Ahrens 				    name, attr->za_name);
1669088f3894Sahrens 				err = func(spa, attr->za_first_integer,
1670088f3894Sahrens 				    child, arg);
1671486ae710SMatthew Ahrens 				strfree(child);
1672088f3894Sahrens 				if (err)
1673088f3894Sahrens 					break;
1674088f3894Sahrens 			}
1675088f3894Sahrens 			zap_cursor_fini(&zc);
1676fa9e4066Sahrens 		}
1677fa9e4066Sahrens 	}
1678fa9e4066Sahrens 
1679fa9e4066Sahrens 	dsl_dir_close(dd, FTAG);
1680b7661cccSmmusante 	kmem_free(attr, sizeof (zap_attribute_t));
1681fa9e4066Sahrens 
16821d452cf5Sahrens 	if (err)
16831d452cf5Sahrens 		return (err);
16841d452cf5Sahrens 
1685fa9e4066Sahrens 	/*
1686fa9e4066Sahrens 	 * Apply to self if appropriate.
1687fa9e4066Sahrens 	 */
1688088f3894Sahrens 	err = func(spa, thisobj, name, arg);
16891d452cf5Sahrens 	return (err);
1690fa9e4066Sahrens }
1691f18faf3fSek 
16927f73c863SRich Morris /* ARGSUSED */
16937f73c863SRich Morris int
1694fd136879SMatthew Ahrens dmu_objset_prefetch(const char *name, void *arg)
16957f73c863SRich Morris {
16967f73c863SRich Morris 	dsl_dataset_t *ds;
16977f73c863SRich Morris 
16987f73c863SRich Morris 	if (dsl_dataset_hold(name, FTAG, &ds))
16997f73c863SRich Morris 		return (0);
17007f73c863SRich Morris 
17017f73c863SRich Morris 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
17027f73c863SRich Morris 		mutex_enter(&ds->ds_opening_lock);
1703503ad85cSMatthew Ahrens 		if (ds->ds_objset == NULL) {
17047f73c863SRich Morris 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
17057f73c863SRich Morris 			zbookmark_t zb;
17067f73c863SRich Morris 
1707b24ab676SJeff Bonwick 			SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
1708b24ab676SJeff Bonwick 			    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
17097f73c863SRich Morris 
17103f9d6ad7SLin Ling 			(void) dsl_read_nolock(NULL, dsl_dataset_get_spa(ds),
17117f73c863SRich Morris 			    &ds->ds_phys->ds_bp, NULL, NULL,
17127f73c863SRich Morris 			    ZIO_PRIORITY_ASYNC_READ,
17137f73c863SRich Morris 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
17147f73c863SRich Morris 			    &aflags, &zb);
17157f73c863SRich Morris 		}
17167f73c863SRich Morris 		mutex_exit(&ds->ds_opening_lock);
17177f73c863SRich Morris 	}
17187f73c863SRich Morris 
17197f73c863SRich Morris 	dsl_dataset_rele(ds, FTAG);
17207f73c863SRich Morris 	return (0);
17217f73c863SRich Morris }
17227f73c863SRich Morris 
1723f18faf3fSek void
1724f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr)
1725f18faf3fSek {
1726503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1727503ad85cSMatthew Ahrens 	os->os_user_ptr = user_ptr;
1728f18faf3fSek }
1729f18faf3fSek 
1730f18faf3fSek void *
1731f18faf3fSek dmu_objset_get_user(objset_t *os)
1732f18faf3fSek {
1733503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1734503ad85cSMatthew Ahrens 	return (os->os_user_ptr);
1735f18faf3fSek }
1736