xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision a3874b8b1fe5103fc1f961609557c0587435fec0)
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  */
211c17160aSKevin Crowe 
22fa9e4066Sahrens /*
2306e0070dSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2494c2d0ebSMatthew Ahrens  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25aad02571SSaso Kiselkov  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26a2afb611SJerry Jelinek  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2812380e1eSArne Jansen  * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
29c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
301c17160aSKevin Crowe  * Copyright 2017 Nexenta Systems, Inc.
31fa9e4066Sahrens  */
32fa9e4066Sahrens 
3355da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
3455da60b9SMark J Musante 
35ecd6cf80Smarks #include <sys/cred.h>
36fa9e4066Sahrens #include <sys/zfs_context.h>
37fa9e4066Sahrens #include <sys/dmu_objset.h>
38fa9e4066Sahrens #include <sys/dsl_dir.h>
39fa9e4066Sahrens #include <sys/dsl_dataset.h>
40fa9e4066Sahrens #include <sys/dsl_prop.h>
41fa9e4066Sahrens #include <sys/dsl_pool.h>
421d452cf5Sahrens #include <sys/dsl_synctask.h>
43ecd6cf80Smarks #include <sys/dsl_deleg.h>
44fa9e4066Sahrens #include <sys/dnode.h>
45fa9e4066Sahrens #include <sys/dbuf.h>
46a2eea2e1Sahrens #include <sys/zvol.h>
47fa9e4066Sahrens #include <sys/dmu_tx.h>
48fa9e4066Sahrens #include <sys/zap.h>
49fa9e4066Sahrens #include <sys/zil.h>
50fa9e4066Sahrens #include <sys/dmu_impl.h>
51ecd6cf80Smarks #include <sys/zfs_ioctl.h>
520a586ceaSMark Shellenbaum #include <sys/sa.h>
5399d5e173STim Haley #include <sys/zfs_onexit.h>
543b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
5512380e1eSArne Jansen #include <sys/vdev.h>
565cabbc6bSPrashanth Sreenivasa #include <sys/zfeature.h>
575ac95da7SSerapheim Dimitropoulos #include "zfs_namecheck.h"
58fa9e4066Sahrens 
59744947dcSTom Erickson /*
60744947dcSTom Erickson  * Needed to close a window in dnode_move() that allows the objset to be freed
61744947dcSTom Erickson  * before it can be safely accessed.
62744947dcSTom Erickson  */
63744947dcSTom Erickson krwlock_t os_lock;
64744947dcSTom Erickson 
6512380e1eSArne Jansen /*
6612380e1eSArne Jansen  * Tunable to overwrite the maximum number of threads for the parallization
6712380e1eSArne Jansen  * of dmu_objset_find_dp, needed to speed up the import of pools with many
6812380e1eSArne Jansen  * datasets.
6912380e1eSArne Jansen  * Default is 4 times the number of leaf vdevs.
7012380e1eSArne Jansen  */
7112380e1eSArne Jansen int dmu_find_threads = 0;
7212380e1eSArne Jansen 
73af346df5SNed Bass /*
74af346df5SNed Bass  * Backfill lower metadnode objects after this many have been freed.
75af346df5SNed Bass  * Backfilling negatively impacts object creation rates, so only do it
76af346df5SNed Bass  * if there are enough holes to fill.
77af346df5SNed Bass  */
78af346df5SNed Bass int dmu_rescan_dnode_threshold = 131072;
79af346df5SNed Bass 
8012380e1eSArne Jansen static void dmu_objset_find_dp_cb(void *arg);
8112380e1eSArne Jansen 
82744947dcSTom Erickson void
83744947dcSTom Erickson dmu_objset_init(void)
84744947dcSTom Erickson {
85744947dcSTom Erickson 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
86744947dcSTom Erickson }
87744947dcSTom Erickson 
88744947dcSTom Erickson void
89744947dcSTom Erickson dmu_objset_fini(void)
90744947dcSTom Erickson {
91744947dcSTom Erickson 	rw_destroy(&os_lock);
92744947dcSTom Erickson }
93744947dcSTom Erickson 
94fa9e4066Sahrens spa_t *
95fa9e4066Sahrens dmu_objset_spa(objset_t *os)
96fa9e4066Sahrens {
97503ad85cSMatthew Ahrens 	return (os->os_spa);
98fa9e4066Sahrens }
99fa9e4066Sahrens 
100fa9e4066Sahrens zilog_t *
101fa9e4066Sahrens dmu_objset_zil(objset_t *os)
102fa9e4066Sahrens {
103503ad85cSMatthew Ahrens 	return (os->os_zil);
104fa9e4066Sahrens }
105fa9e4066Sahrens 
106fa9e4066Sahrens dsl_pool_t *
107fa9e4066Sahrens dmu_objset_pool(objset_t *os)
108fa9e4066Sahrens {
109fa9e4066Sahrens 	dsl_dataset_t *ds;
110fa9e4066Sahrens 
111503ad85cSMatthew Ahrens 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
112fa9e4066Sahrens 		return (ds->ds_dir->dd_pool);
113fa9e4066Sahrens 	else
114503ad85cSMatthew Ahrens 		return (spa_get_dsl(os->os_spa));
115fa9e4066Sahrens }
116fa9e4066Sahrens 
117fa9e4066Sahrens dsl_dataset_t *
118fa9e4066Sahrens dmu_objset_ds(objset_t *os)
119fa9e4066Sahrens {
120503ad85cSMatthew Ahrens 	return (os->os_dsl_dataset);
121fa9e4066Sahrens }
122fa9e4066Sahrens 
123fa9e4066Sahrens dmu_objset_type_t
124fa9e4066Sahrens dmu_objset_type(objset_t *os)
125fa9e4066Sahrens {
126503ad85cSMatthew Ahrens 	return (os->os_phys->os_type);
127fa9e4066Sahrens }
128fa9e4066Sahrens 
129fa9e4066Sahrens void
130fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
131fa9e4066Sahrens {
132503ad85cSMatthew Ahrens 	dsl_dataset_name(os->os_dsl_dataset, buf);
133fa9e4066Sahrens }
134fa9e4066Sahrens 
135fa9e4066Sahrens uint64_t
136fa9e4066Sahrens dmu_objset_id(objset_t *os)
137fa9e4066Sahrens {
138503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
139fa9e4066Sahrens 
140fa9e4066Sahrens 	return (ds ? ds->ds_object : 0);
141fa9e4066Sahrens }
142fa9e4066Sahrens 
14354811da5SToomas Soome uint64_t
14454811da5SToomas Soome dmu_objset_dnodesize(objset_t *os)
14554811da5SToomas Soome {
14654811da5SToomas Soome 	return (os->os_dnodesize);
14754811da5SToomas Soome }
14854811da5SToomas Soome 
149edf345e6SMatthew Ahrens zfs_sync_type_t
15055da60b9SMark J Musante dmu_objset_syncprop(objset_t *os)
15155da60b9SMark J Musante {
15255da60b9SMark J Musante 	return (os->os_sync);
15355da60b9SMark J Musante }
15455da60b9SMark J Musante 
155edf345e6SMatthew Ahrens zfs_logbias_op_t
156e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
157e09fa4daSNeil Perrin {
158e09fa4daSNeil Perrin 	return (os->os_logbias);
159e09fa4daSNeil Perrin }
160e09fa4daSNeil Perrin 
161fa9e4066Sahrens static void
162fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
163fa9e4066Sahrens {
164503ad85cSMatthew Ahrens 	objset_t *os = arg;
165fa9e4066Sahrens 
166fa9e4066Sahrens 	/*
167fa9e4066Sahrens 	 * Inheritance should have been done by now.
168fa9e4066Sahrens 	 */
169fa9e4066Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
170fa9e4066Sahrens 
171503ad85cSMatthew Ahrens 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
172fa9e4066Sahrens }
173fa9e4066Sahrens 
174fa9e4066Sahrens static void
175fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
176fa9e4066Sahrens {
177503ad85cSMatthew Ahrens 	objset_t *os = arg;
178fa9e4066Sahrens 
179fa9e4066Sahrens 	/*
180fa9e4066Sahrens 	 * Inheritance and range checking should have been done by now.
181fa9e4066Sahrens 	 */
182fa9e4066Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
183fa9e4066Sahrens 
184db1741f5SJustin T. Gibbs 	os->os_compress = zio_compress_select(os->os_spa, newval,
185db1741f5SJustin T. Gibbs 	    ZIO_COMPRESS_ON);
186fa9e4066Sahrens }
187fa9e4066Sahrens 
188d0ad202dSahrens static void
189d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
190d0ad202dSahrens {
191503ad85cSMatthew Ahrens 	objset_t *os = arg;
192d0ad202dSahrens 
193d0ad202dSahrens 	/*
194d0ad202dSahrens 	 * Inheritance and range checking should have been done by now.
195d0ad202dSahrens 	 */
196d0ad202dSahrens 	ASSERT(newval > 0);
197503ad85cSMatthew Ahrens 	ASSERT(newval <= spa_max_replication(os->os_spa));
198d0ad202dSahrens 
199503ad85cSMatthew Ahrens 	os->os_copies = newval;
200d0ad202dSahrens }
201d0ad202dSahrens 
202b24ab676SJeff Bonwick static void
203b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
204b24ab676SJeff Bonwick {
205b24ab676SJeff Bonwick 	objset_t *os = arg;
206b24ab676SJeff Bonwick 	spa_t *spa = os->os_spa;
207b24ab676SJeff Bonwick 	enum zio_checksum checksum;
208b24ab676SJeff Bonwick 
209b24ab676SJeff Bonwick 	/*
210b24ab676SJeff Bonwick 	 * Inheritance should have been done by now.
211b24ab676SJeff Bonwick 	 */
212b24ab676SJeff Bonwick 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
213b24ab676SJeff Bonwick 
214b24ab676SJeff Bonwick 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
215b24ab676SJeff Bonwick 
216b24ab676SJeff Bonwick 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
217b24ab676SJeff Bonwick 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
218b24ab676SJeff Bonwick }
219b24ab676SJeff Bonwick 
2203baa08fcSek static void
2213baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval)
2223baa08fcSek {
223503ad85cSMatthew Ahrens 	objset_t *os = arg;
2243baa08fcSek 
2253baa08fcSek 	/*
2263baa08fcSek 	 * Inheritance and range checking should have been done by now.
2273baa08fcSek 	 */
2283baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2293baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2303baa08fcSek 
231503ad85cSMatthew Ahrens 	os->os_primary_cache = newval;
2323baa08fcSek }
2333baa08fcSek 
2343baa08fcSek static void
2353baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval)
2363baa08fcSek {
237503ad85cSMatthew Ahrens 	objset_t *os = arg;
2383baa08fcSek 
2393baa08fcSek 	/*
2403baa08fcSek 	 * Inheritance and range checking should have been done by now.
2413baa08fcSek 	 */
2423baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2433baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2443baa08fcSek 
245503ad85cSMatthew Ahrens 	os->os_secondary_cache = newval;
2463baa08fcSek }
2473baa08fcSek 
24855da60b9SMark J Musante static void
24955da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval)
25055da60b9SMark J Musante {
25155da60b9SMark J Musante 	objset_t *os = arg;
25255da60b9SMark J Musante 
25355da60b9SMark J Musante 	/*
25455da60b9SMark J Musante 	 * Inheritance and range checking should have been done by now.
25555da60b9SMark J Musante 	 */
25655da60b9SMark J Musante 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
25755da60b9SMark J Musante 	    newval == ZFS_SYNC_DISABLED);
25855da60b9SMark J Musante 
25955da60b9SMark J Musante 	os->os_sync = newval;
26055da60b9SMark J Musante 	if (os->os_zil)
26155da60b9SMark J Musante 		zil_set_sync(os->os_zil, newval);
26255da60b9SMark J Musante }
26355da60b9SMark J Musante 
264edf345e6SMatthew Ahrens static void
265edf345e6SMatthew Ahrens redundant_metadata_changed_cb(void *arg, uint64_t newval)
266edf345e6SMatthew Ahrens {
267edf345e6SMatthew Ahrens 	objset_t *os = arg;
268edf345e6SMatthew Ahrens 
269edf345e6SMatthew Ahrens 	/*
270edf345e6SMatthew Ahrens 	 * Inheritance and range checking should have been done by now.
271edf345e6SMatthew Ahrens 	 */
272edf345e6SMatthew Ahrens 	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
273edf345e6SMatthew Ahrens 	    newval == ZFS_REDUNDANT_METADATA_MOST);
274edf345e6SMatthew Ahrens 
275edf345e6SMatthew Ahrens 	os->os_redundant_metadata = newval;
276edf345e6SMatthew Ahrens }
277edf345e6SMatthew Ahrens 
27854811da5SToomas Soome static void
27954811da5SToomas Soome dnodesize_changed_cb(void *arg, uint64_t newval)
28054811da5SToomas Soome {
28154811da5SToomas Soome 	objset_t *os = arg;
28254811da5SToomas Soome 
28354811da5SToomas Soome 	switch (newval) {
28454811da5SToomas Soome 	case ZFS_DNSIZE_LEGACY:
28554811da5SToomas Soome 		os->os_dnodesize = DNODE_MIN_SIZE;
28654811da5SToomas Soome 		break;
28754811da5SToomas Soome 	case ZFS_DNSIZE_AUTO:
28854811da5SToomas Soome 		/*
28954811da5SToomas Soome 		 * Choose a dnode size that will work well for most
29054811da5SToomas Soome 		 * workloads if the user specified "auto". Future code
29154811da5SToomas Soome 		 * improvements could dynamically select a dnode size
29254811da5SToomas Soome 		 * based on observed workload patterns.
29354811da5SToomas Soome 		 */
29454811da5SToomas Soome 		os->os_dnodesize = DNODE_MIN_SIZE * 2;
29554811da5SToomas Soome 		break;
29654811da5SToomas Soome 	case ZFS_DNSIZE_1K:
29754811da5SToomas Soome 	case ZFS_DNSIZE_2K:
29854811da5SToomas Soome 	case ZFS_DNSIZE_4K:
29954811da5SToomas Soome 	case ZFS_DNSIZE_8K:
30054811da5SToomas Soome 	case ZFS_DNSIZE_16K:
30154811da5SToomas Soome 		os->os_dnodesize = newval;
30254811da5SToomas Soome 		break;
30354811da5SToomas Soome 	}
30454811da5SToomas Soome }
30554811da5SToomas Soome 
306663207adSDon Brady static void
307663207adSDon Brady smallblk_changed_cb(void *arg, uint64_t newval)
308663207adSDon Brady {
309663207adSDon Brady 	objset_t *os = arg;
310663207adSDon Brady 
311663207adSDon Brady 	/*
312663207adSDon Brady 	 * Inheritance and range checking should have been done by now.
313663207adSDon Brady 	 */
314663207adSDon Brady 	ASSERT(newval <= SPA_OLD_MAXBLOCKSIZE);
315663207adSDon Brady 	ASSERT(ISP2(newval));
316663207adSDon Brady 
317663207adSDon Brady 	os->os_zpl_special_smallblock = newval;
318663207adSDon Brady }
319663207adSDon Brady 
320e09fa4daSNeil Perrin static void
321e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
322e09fa4daSNeil Perrin {
323e09fa4daSNeil Perrin 	objset_t *os = arg;
324e09fa4daSNeil Perrin 
325e09fa4daSNeil Perrin 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
326e09fa4daSNeil Perrin 	    newval == ZFS_LOGBIAS_THROUGHPUT);
327e09fa4daSNeil Perrin 	os->os_logbias = newval;
328e09fa4daSNeil Perrin 	if (os->os_zil)
329e09fa4daSNeil Perrin 		zil_set_logbias(os->os_zil, newval);
330e09fa4daSNeil Perrin }
331e09fa4daSNeil Perrin 
332b5152584SMatthew Ahrens static void
333b5152584SMatthew Ahrens recordsize_changed_cb(void *arg, uint64_t newval)
334b5152584SMatthew Ahrens {
335b5152584SMatthew Ahrens 	objset_t *os = arg;
336b5152584SMatthew Ahrens 
337b5152584SMatthew Ahrens 	os->os_recordsize = newval;
338b5152584SMatthew Ahrens }
339b5152584SMatthew Ahrens 
340fa9e4066Sahrens void
341fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
342fa9e4066Sahrens {
343fa9e4066Sahrens 	objset_phys_t *osp = buf;
344fa9e4066Sahrens 
34514843421SMatthew Ahrens 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
346fa9e4066Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
347fa9e4066Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
348fa9e4066Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
34914843421SMatthew Ahrens 	osp->os_flags = BSWAP_64(osp->os_flags);
35014843421SMatthew Ahrens 	if (size == sizeof (objset_phys_t)) {
35114843421SMatthew Ahrens 		dnode_byteswap(&osp->os_userused_dnode);
35214843421SMatthew Ahrens 		dnode_byteswap(&osp->os_groupused_dnode);
35314843421SMatthew Ahrens 	}
354fa9e4066Sahrens }
355fa9e4066Sahrens 
35694c2d0ebSMatthew Ahrens /*
35794c2d0ebSMatthew Ahrens  * The hash is a CRC-based hash of the objset_t pointer and the object number.
35894c2d0ebSMatthew Ahrens  */
35994c2d0ebSMatthew Ahrens static uint64_t
36094c2d0ebSMatthew Ahrens dnode_hash(const objset_t *os, uint64_t obj)
36194c2d0ebSMatthew Ahrens {
36294c2d0ebSMatthew Ahrens 	uintptr_t osv = (uintptr_t)os;
36394c2d0ebSMatthew Ahrens 	uint64_t crc = -1ULL;
36494c2d0ebSMatthew Ahrens 
36594c2d0ebSMatthew Ahrens 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
36694c2d0ebSMatthew Ahrens 	/*
36794c2d0ebSMatthew Ahrens 	 * The low 6 bits of the pointer don't have much entropy, because
36894c2d0ebSMatthew Ahrens 	 * the objset_t is larger than 2^6 bytes long.
36994c2d0ebSMatthew Ahrens 	 */
37094c2d0ebSMatthew Ahrens 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
37194c2d0ebSMatthew Ahrens 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
37294c2d0ebSMatthew Ahrens 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
37394c2d0ebSMatthew Ahrens 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 16)) & 0xFF];
37494c2d0ebSMatthew Ahrens 
37594c2d0ebSMatthew Ahrens 	crc ^= (osv>>14) ^ (obj>>24);
37694c2d0ebSMatthew Ahrens 
37794c2d0ebSMatthew Ahrens 	return (crc);
37894c2d0ebSMatthew Ahrens }
37994c2d0ebSMatthew Ahrens 
38094c2d0ebSMatthew Ahrens unsigned int
38194c2d0ebSMatthew Ahrens dnode_multilist_index_func(multilist_t *ml, void *obj)
38294c2d0ebSMatthew Ahrens {
38394c2d0ebSMatthew Ahrens 	dnode_t *dn = obj;
38494c2d0ebSMatthew Ahrens 	return (dnode_hash(dn->dn_objset, dn->dn_object) %
38594c2d0ebSMatthew Ahrens 	    multilist_get_num_sublists(ml));
38694c2d0ebSMatthew Ahrens }
38794c2d0ebSMatthew Ahrens 
38894c2d0ebSMatthew Ahrens /*
38994c2d0ebSMatthew Ahrens  * Instantiates the objset_t in-memory structure corresponding to the
39094c2d0ebSMatthew Ahrens  * objset_phys_t that's pointed to by the specified blkptr_t.
39194c2d0ebSMatthew Ahrens  */
392ea8dc4b6Seschrock int
393ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
394503ad85cSMatthew Ahrens     objset_t **osp)
395fa9e4066Sahrens {
396503ad85cSMatthew Ahrens 	objset_t *os;
397088f3894Sahrens 	int i, err;
398fa9e4066Sahrens 
39991ebeef5Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
40091ebeef5Sahrens 
401*a3874b8bSToomas Soome #if 0
4025cabbc6bSPrashanth Sreenivasa 	/*
4035cabbc6bSPrashanth Sreenivasa 	 * The $ORIGIN dataset (if it exists) doesn't have an associated
4045cabbc6bSPrashanth Sreenivasa 	 * objset, so there's no reason to open it. The $ORIGIN dataset
4055cabbc6bSPrashanth Sreenivasa 	 * will not exist on pools older than SPA_VERSION_ORIGIN.
4065cabbc6bSPrashanth Sreenivasa 	 */
4075cabbc6bSPrashanth Sreenivasa 	if (ds != NULL && spa_get_dsl(spa) != NULL &&
4085cabbc6bSPrashanth Sreenivasa 	    spa_get_dsl(spa)->dp_origin_snap != NULL) {
4095cabbc6bSPrashanth Sreenivasa 		ASSERT3P(ds->ds_dir, !=,
4105cabbc6bSPrashanth Sreenivasa 		    spa_get_dsl(spa)->dp_origin_snap->ds_dir);
4115cabbc6bSPrashanth Sreenivasa 	}
412*a3874b8bSToomas Soome #endif
4135cabbc6bSPrashanth Sreenivasa 
414503ad85cSMatthew Ahrens 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
415503ad85cSMatthew Ahrens 	os->os_dsl_dataset = ds;
416503ad85cSMatthew Ahrens 	os->os_spa = spa;
417503ad85cSMatthew Ahrens 	os->os_rootbp = bp;
418503ad85cSMatthew Ahrens 	if (!BP_IS_HOLE(os->os_rootbp)) {
4197adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
4207802d7bfSMatthew Ahrens 		zbookmark_phys_t zb;
421b24ab676SJeff Bonwick 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
422b24ab676SJeff Bonwick 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
423b24ab676SJeff Bonwick 
424503ad85cSMatthew Ahrens 		if (DMU_OS_IS_L2CACHEABLE(os))
4257adb730bSGeorge Wilson 			aflags |= ARC_FLAG_L2CACHE;
426ea8dc4b6Seschrock 
427503ad85cSMatthew Ahrens 		dprintf_bp(os->os_rootbp, "reading %s", "");
4281b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, os->os_rootbp,
429503ad85cSMatthew Ahrens 		    arc_getbuf_func, &os->os_phys_buf,
43013506d1eSmaybee 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
4313b2aab18SMatthew Ahrens 		if (err != 0) {
432503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
433b87f3af3Sperrin 			/* convert checksum errors into IO errors */
434b87f3af3Sperrin 			if (err == ECKSUM)
435be6fd75aSMatthew Ahrens 				err = SET_ERROR(EIO);
436ea8dc4b6Seschrock 			return (err);
437ea8dc4b6Seschrock 		}
43814843421SMatthew Ahrens 
43914843421SMatthew Ahrens 		/* Increase the blocksize if we are permitted. */
44014843421SMatthew Ahrens 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
441503ad85cSMatthew Ahrens 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
4425602294fSDan Kimmel 			arc_buf_t *buf = arc_alloc_buf(spa, &os->os_phys_buf,
4435602294fSDan Kimmel 			    ARC_BUFC_METADATA, sizeof (objset_phys_t));
44414843421SMatthew Ahrens 			bzero(buf->b_data, sizeof (objset_phys_t));
445503ad85cSMatthew Ahrens 			bcopy(os->os_phys_buf->b_data, buf->b_data,
446503ad85cSMatthew Ahrens 			    arc_buf_size(os->os_phys_buf));
447dcbf3bd6SGeorge Wilson 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
448503ad85cSMatthew Ahrens 			os->os_phys_buf = buf;
44914843421SMatthew Ahrens 		}
45014843421SMatthew Ahrens 
451503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
452503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
453fa9e4066Sahrens 	} else {
45414843421SMatthew Ahrens 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
45514843421SMatthew Ahrens 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
4565602294fSDan Kimmel 		os->os_phys_buf = arc_alloc_buf(spa, &os->os_phys_buf,
4575602294fSDan Kimmel 		    ARC_BUFC_METADATA, size);
458503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
459503ad85cSMatthew Ahrens 		bzero(os->os_phys, size);
460fa9e4066Sahrens 	}
461fa9e4066Sahrens 
462fa9e4066Sahrens 	/*
463fa9e4066Sahrens 	 * Note: the changed_cb will be called once before the register
464fa9e4066Sahrens 	 * func returns, thus changing the checksum/compression from the
4653baa08fcSek 	 * default (fletcher2/off).  Snapshots don't need to know about
4663baa08fcSek 	 * checksum/compression/copies.
467fa9e4066Sahrens 	 */
4685d7b4d43SMatthew Ahrens 	if (ds != NULL) {
4699c3fd121SMatthew Ahrens 		boolean_t needlock = B_FALSE;
4709c3fd121SMatthew Ahrens 
4719c3fd121SMatthew Ahrens 		/*
4729c3fd121SMatthew Ahrens 		 * Note: it's valid to open the objset if the dataset is
4739c3fd121SMatthew Ahrens 		 * long-held, in which case the pool_config lock will not
4749c3fd121SMatthew Ahrens 		 * be held.
4759c3fd121SMatthew Ahrens 		 */
4769c3fd121SMatthew Ahrens 		if (!dsl_pool_config_held(dmu_objset_pool(os))) {
4779c3fd121SMatthew Ahrens 			needlock = B_TRUE;
4789c3fd121SMatthew Ahrens 			dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
4799c3fd121SMatthew Ahrens 		}
4803b2aab18SMatthew Ahrens 		err = dsl_prop_register(ds,
4813b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
482503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
4833b2aab18SMatthew Ahrens 		if (err == 0) {
4843b2aab18SMatthew Ahrens 			err = dsl_prop_register(ds,
4853b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
486503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
4873b2aab18SMatthew Ahrens 		}
488bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
4893b2aab18SMatthew Ahrens 			if (err == 0) {
4903b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
4913b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
492503ad85cSMatthew Ahrens 				    checksum_changed_cb, os);
4933b2aab18SMatthew Ahrens 			}
4943b2aab18SMatthew Ahrens 			if (err == 0) {
4953b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
4963b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
497503ad85cSMatthew Ahrens 				    compression_changed_cb, os);
4983b2aab18SMatthew Ahrens 			}
4993b2aab18SMatthew Ahrens 			if (err == 0) {
5003b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
5013b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COPIES),
502503ad85cSMatthew Ahrens 				    copies_changed_cb, os);
5033b2aab18SMatthew Ahrens 			}
5043b2aab18SMatthew Ahrens 			if (err == 0) {
5053b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
5063b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_DEDUP),
507b24ab676SJeff Bonwick 				    dedup_changed_cb, os);
5083b2aab18SMatthew Ahrens 			}
5093b2aab18SMatthew Ahrens 			if (err == 0) {
5103b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
5113b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
512e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
5133b2aab18SMatthew Ahrens 			}
5143b2aab18SMatthew Ahrens 			if (err == 0) {
5153b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
5163b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_SYNC),
51755da60b9SMark J Musante 				    sync_changed_cb, os);
5183b2aab18SMatthew Ahrens 			}
519edf345e6SMatthew Ahrens 			if (err == 0) {
520edf345e6SMatthew Ahrens 				err = dsl_prop_register(ds,
521edf345e6SMatthew Ahrens 				    zfs_prop_to_name(
522edf345e6SMatthew Ahrens 				    ZFS_PROP_REDUNDANT_METADATA),
523edf345e6SMatthew Ahrens 				    redundant_metadata_changed_cb, os);
524edf345e6SMatthew Ahrens 			}
525b5152584SMatthew Ahrens 			if (err == 0) {
526b5152584SMatthew Ahrens 				err = dsl_prop_register(ds,
527b5152584SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
528b5152584SMatthew Ahrens 				    recordsize_changed_cb, os);
529b5152584SMatthew Ahrens 			}
53054811da5SToomas Soome 			if (err == 0) {
53154811da5SToomas Soome 				err = dsl_prop_register(ds,
53254811da5SToomas Soome 				    zfs_prop_to_name(ZFS_PROP_DNODESIZE),
53354811da5SToomas Soome 				    dnodesize_changed_cb, os);
53454811da5SToomas Soome 			}
535663207adSDon Brady 			if (err == 0) {
536663207adSDon Brady 				err = dsl_prop_register(ds,
537663207adSDon Brady 				    zfs_prop_to_name(
538663207adSDon Brady 				    ZFS_PROP_SPECIAL_SMALL_BLOCKS),
539663207adSDon Brady 				    smallblk_changed_cb, os);
540663207adSDon Brady 			}
5413baa08fcSek 		}
5429c3fd121SMatthew Ahrens 		if (needlock)
5439c3fd121SMatthew Ahrens 			dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5443b2aab18SMatthew Ahrens 		if (err != 0) {
545dcbf3bd6SGeorge Wilson 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
546503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
547ea8dc4b6Seschrock 			return (err);
548ea8dc4b6Seschrock 		}
5495d7b4d43SMatthew Ahrens 	} else {
550fa9e4066Sahrens 		/* It's the meta-objset. */
551503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
552db1741f5SJustin T. Gibbs 		os->os_compress = ZIO_COMPRESS_ON;
553503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
554b24ab676SJeff Bonwick 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
555edf345e6SMatthew Ahrens 		os->os_dedup_verify = B_FALSE;
556edf345e6SMatthew Ahrens 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
557edf345e6SMatthew Ahrens 		os->os_sync = ZFS_SYNC_STANDARD;
558503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
559503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
56054811da5SToomas Soome 		os->os_dnodesize = DNODE_MIN_SIZE;
561fa9e4066Sahrens 	}
562adb52d92SMatthew Ahrens 	/*
563adb52d92SMatthew Ahrens 	 * These properties will be filled in by the logic in zfs_get_zplprop()
564adb52d92SMatthew Ahrens 	 * when they are queried for the first time.
565adb52d92SMatthew Ahrens 	 */
566adb52d92SMatthew Ahrens 	os->os_version = OBJSET_PROP_UNINITIALIZED;
567adb52d92SMatthew Ahrens 	os->os_normalization = OBJSET_PROP_UNINITIALIZED;
568adb52d92SMatthew Ahrens 	os->os_utf8only = OBJSET_PROP_UNINITIALIZED;
569adb52d92SMatthew Ahrens 	os->os_casesensitivity = OBJSET_PROP_UNINITIALIZED;
570fa9e4066Sahrens 
571bc9014e6SJustin Gibbs 	if (ds == NULL || !ds->ds_is_snapshot)
5726e0cbcaaSMatthew Ahrens 		os->os_zil_header = os->os_phys->os_zil_header;
573503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
574fa9e4066Sahrens 
575fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
57694c2d0ebSMatthew Ahrens 		os->os_dirty_dnodes[i] = multilist_create(sizeof (dnode_t),
57794c2d0ebSMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[i]),
57894c2d0ebSMatthew Ahrens 		    dnode_multilist_index_func);
579fa9e4066Sahrens 	}
580503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
581fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
582503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
583fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
584fa9e4066Sahrens 
585503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
58694c2d0ebSMatthew Ahrens 	mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL);
587503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
588503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
58954811da5SToomas Soome 	os->os_obj_next_percpu_len = boot_ncpus;
59054811da5SToomas Soome 	os->os_obj_next_percpu = kmem_zalloc(os->os_obj_next_percpu_len *
59154811da5SToomas Soome 	    sizeof (os->os_obj_next_percpu[0]), KM_SLEEP);
592503ad85cSMatthew Ahrens 
593bc9014e6SJustin Gibbs 	dnode_special_open(os, &os->os_phys->os_meta_dnode,
594bc9014e6SJustin Gibbs 	    DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
595503ad85cSMatthew Ahrens 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
596bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_userused_dnode,
597bc9014e6SJustin Gibbs 		    DMU_USERUSED_OBJECT, &os->os_userused_dnode);
598bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_groupused_dnode,
599bc9014e6SJustin Gibbs 		    DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
60014843421SMatthew Ahrens 	}
601fa9e4066Sahrens 
602503ad85cSMatthew Ahrens 	*osp = os;
603ea8dc4b6Seschrock 	return (0);
604fa9e4066Sahrens }
605fa9e4066Sahrens 
606503ad85cSMatthew Ahrens int
607503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
6083cb34c60Sahrens {
609503ad85cSMatthew Ahrens 	int err = 0;
6103cb34c60Sahrens 
6119c3fd121SMatthew Ahrens 	/*
6129c3fd121SMatthew Ahrens 	 * We shouldn't be doing anything with dsl_dataset_t's unless the
6139c3fd121SMatthew Ahrens 	 * pool_config lock is held, or the dataset is long-held.
6149c3fd121SMatthew Ahrens 	 */
6159c3fd121SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
6169c3fd121SMatthew Ahrens 	    dsl_dataset_long_held(ds));
6179c3fd121SMatthew Ahrens 
6183cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
6195d7b4d43SMatthew Ahrens 	if (ds->ds_objset == NULL) {
6205d7b4d43SMatthew Ahrens 		objset_t *os;
621c166b69dSPaul Dagnelie 		rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
6223cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
6235d7b4d43SMatthew Ahrens 		    ds, dsl_dataset_get_blkptr(ds), &os);
624c166b69dSPaul Dagnelie 		rrw_exit(&ds->ds_bp_rwlock, FTAG);
6255d7b4d43SMatthew Ahrens 
6265d7b4d43SMatthew Ahrens 		if (err == 0) {
6275d7b4d43SMatthew Ahrens 			mutex_enter(&ds->ds_lock);
6285d7b4d43SMatthew Ahrens 			ASSERT(ds->ds_objset == NULL);
6295d7b4d43SMatthew Ahrens 			ds->ds_objset = os;
6305d7b4d43SMatthew Ahrens 			mutex_exit(&ds->ds_lock);
6315d7b4d43SMatthew Ahrens 		}
6323cb34c60Sahrens 	}
6335d7b4d43SMatthew Ahrens 	*osp = ds->ds_objset;
6343cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
635503ad85cSMatthew Ahrens 	return (err);
6363cb34c60Sahrens }
6373cb34c60Sahrens 
6383b2aab18SMatthew Ahrens /*
6393b2aab18SMatthew Ahrens  * Holds the pool while the objset is held.  Therefore only one objset
6403b2aab18SMatthew Ahrens  * can be held at a time.
6413b2aab18SMatthew Ahrens  */
6423cb34c60Sahrens int
643503ad85cSMatthew Ahrens dmu_objset_hold(const char *name, void *tag, objset_t **osp)
6443cb34c60Sahrens {
6453b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
646503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
6473cb34c60Sahrens 	int err;
6483cb34c60Sahrens 
6493b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, tag, &dp);
6503b2aab18SMatthew Ahrens 	if (err != 0)
6513b2aab18SMatthew Ahrens 		return (err);
6523b2aab18SMatthew Ahrens 	err = dsl_dataset_hold(dp, name, tag, &ds);
6533b2aab18SMatthew Ahrens 	if (err != 0) {
6543b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
655503ad85cSMatthew Ahrens 		return (err);
6563b2aab18SMatthew Ahrens 	}
657503ad85cSMatthew Ahrens 
658503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
6593b2aab18SMatthew Ahrens 	if (err != 0) {
660503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
6613b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
6623b2aab18SMatthew Ahrens 	}
663503ad85cSMatthew Ahrens 
6643cb34c60Sahrens 	return (err);
6653cb34c60Sahrens }
6663cb34c60Sahrens 
66712380e1eSArne Jansen static int
66812380e1eSArne Jansen dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
66912380e1eSArne Jansen     boolean_t readonly, void *tag, objset_t **osp)
67012380e1eSArne Jansen {
67112380e1eSArne Jansen 	int err;
67212380e1eSArne Jansen 
67312380e1eSArne Jansen 	err = dmu_objset_from_ds(ds, osp);
67412380e1eSArne Jansen 	if (err != 0) {
67512380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
67612380e1eSArne Jansen 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
67712380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
67812380e1eSArne Jansen 		return (SET_ERROR(EINVAL));
67912380e1eSArne Jansen 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
68012380e1eSArne Jansen 		dsl_dataset_disown(ds, tag);
68112380e1eSArne Jansen 		return (SET_ERROR(EROFS));
68212380e1eSArne Jansen 	}
68312380e1eSArne Jansen 	return (err);
68412380e1eSArne Jansen }
68512380e1eSArne Jansen 
6863b2aab18SMatthew Ahrens /*
6873b2aab18SMatthew Ahrens  * dsl_pool must not be held when this is called.
6883b2aab18SMatthew Ahrens  * Upon successful return, there will be a longhold on the dataset,
6893b2aab18SMatthew Ahrens  * and the dsl_pool will not be held.
6903b2aab18SMatthew Ahrens  */
691fa9e4066Sahrens int
692503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
693503ad85cSMatthew Ahrens     boolean_t readonly, void *tag, objset_t **osp)
694fa9e4066Sahrens {
6953b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
696f18faf3fSek 	dsl_dataset_t *ds;
697f18faf3fSek 	int err;
698fa9e4066Sahrens 
6993b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
7003b2aab18SMatthew Ahrens 	if (err != 0)
7013b2aab18SMatthew Ahrens 		return (err);
7023b2aab18SMatthew Ahrens 	err = dsl_dataset_own(dp, name, tag, &ds);
7033b2aab18SMatthew Ahrens 	if (err != 0) {
7043b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
705fa9e4066Sahrens 		return (err);
7063b2aab18SMatthew Ahrens 	}
70712380e1eSArne Jansen 	err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
7083b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
70912380e1eSArne Jansen 
7103cb34c60Sahrens 	return (err);
711fa9e4066Sahrens }
712fa9e4066Sahrens 
71312380e1eSArne Jansen int
71412380e1eSArne Jansen dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
71512380e1eSArne Jansen     boolean_t readonly, void *tag, objset_t **osp)
71612380e1eSArne Jansen {
71712380e1eSArne Jansen 	dsl_dataset_t *ds;
71812380e1eSArne Jansen 	int err;
71912380e1eSArne Jansen 
72012380e1eSArne Jansen 	err = dsl_dataset_own_obj(dp, obj, tag, &ds);
72112380e1eSArne Jansen 	if (err != 0)
72212380e1eSArne Jansen 		return (err);
72312380e1eSArne Jansen 
72412380e1eSArne Jansen 	return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
72512380e1eSArne Jansen }
72612380e1eSArne Jansen 
727fa9e4066Sahrens void
728503ad85cSMatthew Ahrens dmu_objset_rele(objset_t *os, void *tag)
729fa9e4066Sahrens {
7303b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(os);
731503ad85cSMatthew Ahrens 	dsl_dataset_rele(os->os_dsl_dataset, tag);
7323b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, tag);
733503ad85cSMatthew Ahrens }
734503ad85cSMatthew Ahrens 
73591948b51SKeith M Wesolowski /*
73691948b51SKeith M Wesolowski  * When we are called, os MUST refer to an objset associated with a dataset
73791948b51SKeith M Wesolowski  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
73891948b51SKeith M Wesolowski  * == tag.  We will then release and reacquire ownership of the dataset while
73991948b51SKeith M Wesolowski  * holding the pool config_rwlock to avoid intervening namespace or ownership
74091948b51SKeith M Wesolowski  * changes may occur.
74191948b51SKeith M Wesolowski  *
74291948b51SKeith M Wesolowski  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
74391948b51SKeith M Wesolowski  * release the hold on its dataset and acquire a new one on the dataset of the
74491948b51SKeith M Wesolowski  * same name so that it can be partially torn down and reconstructed.
74591948b51SKeith M Wesolowski  */
74691948b51SKeith M Wesolowski void
7475f5913bbSAndriy Gapon dmu_objset_refresh_ownership(dsl_dataset_t *ds, dsl_dataset_t **newds,
7485f5913bbSAndriy Gapon     void *tag)
74991948b51SKeith M Wesolowski {
75091948b51SKeith M Wesolowski 	dsl_pool_t *dp;
7519adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
75291948b51SKeith M Wesolowski 
75391948b51SKeith M Wesolowski 	VERIFY3P(ds, !=, NULL);
75491948b51SKeith M Wesolowski 	VERIFY3P(ds->ds_owner, ==, tag);
75591948b51SKeith M Wesolowski 	VERIFY(dsl_dataset_long_held(ds));
75691948b51SKeith M Wesolowski 
75791948b51SKeith M Wesolowski 	dsl_dataset_name(ds, name);
7585f5913bbSAndriy Gapon 	dp = ds->ds_dir->dd_pool;
75991948b51SKeith M Wesolowski 	dsl_pool_config_enter(dp, FTAG);
7605f5913bbSAndriy Gapon 	dsl_dataset_disown(ds, tag);
7615f5913bbSAndriy Gapon 	VERIFY0(dsl_dataset_own(dp, name, tag, newds));
76291948b51SKeith M Wesolowski 	dsl_pool_config_exit(dp, FTAG);
76391948b51SKeith M Wesolowski }
76491948b51SKeith M Wesolowski 
765503ad85cSMatthew Ahrens void
766503ad85cSMatthew Ahrens dmu_objset_disown(objset_t *os, void *tag)
767503ad85cSMatthew Ahrens {
768503ad85cSMatthew Ahrens 	dsl_dataset_disown(os->os_dsl_dataset, tag);
769fa9e4066Sahrens }
770fa9e4066Sahrens 
7713b2aab18SMatthew Ahrens void
7721934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
773ea8dc4b6Seschrock {
774bc9014e6SJustin Gibbs 	dnode_t dn_marker;
775ea8dc4b6Seschrock 	dnode_t *dn;
776c543ec06Sahrens 
777503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
778bc9014e6SJustin Gibbs 	dn = list_head(&os->os_dnodes);
779bc9014e6SJustin Gibbs 	while (dn != NULL) {
780bc9014e6SJustin Gibbs 		/*
781bc9014e6SJustin Gibbs 		 * Skip dnodes without holds.  We have to do this dance
782bc9014e6SJustin Gibbs 		 * because dnode_add_ref() only works if there is already a
783bc9014e6SJustin Gibbs 		 * hold.  If the dnode has no holds, then it has no dbufs.
784bc9014e6SJustin Gibbs 		 */
785bc9014e6SJustin Gibbs 		if (dnode_add_ref(dn, FTAG)) {
786bc9014e6SJustin Gibbs 			list_insert_after(&os->os_dnodes, dn, &dn_marker);
787bc9014e6SJustin Gibbs 			mutex_exit(&os->os_lock);
788c543ec06Sahrens 
789bc9014e6SJustin Gibbs 			dnode_evict_dbufs(dn);
790bc9014e6SJustin Gibbs 			dnode_rele(dn, FTAG);
791c543ec06Sahrens 
792bc9014e6SJustin Gibbs 			mutex_enter(&os->os_lock);
793bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, &dn_marker);
794bc9014e6SJustin Gibbs 			list_remove(&os->os_dnodes, &dn_marker);
795bc9014e6SJustin Gibbs 		} else {
796bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, dn);
797bc9014e6SJustin Gibbs 		}
798ea8dc4b6Seschrock 	}
799503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
800bc9014e6SJustin Gibbs 
801bc9014e6SJustin Gibbs 	if (DMU_USERUSED_DNODE(os) != NULL) {
802bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
803bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
804bc9014e6SJustin Gibbs 	}
805bc9014e6SJustin Gibbs 	dnode_evict_dbufs(DMU_META_DNODE(os));
806ea8dc4b6Seschrock }
807ea8dc4b6Seschrock 
808bc9014e6SJustin Gibbs /*
809bc9014e6SJustin Gibbs  * Objset eviction processing is split into into two pieces.
810bc9014e6SJustin Gibbs  * The first marks the objset as evicting, evicts any dbufs that
811bc9014e6SJustin Gibbs  * have a refcount of zero, and then queues up the objset for the
812bc9014e6SJustin Gibbs  * second phase of eviction.  Once os->os_dnodes has been cleared by
813bc9014e6SJustin Gibbs  * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
814bc9014e6SJustin Gibbs  * The second phase closes the special dnodes, dequeues the objset from
815bc9014e6SJustin Gibbs  * the list of those undergoing eviction, and finally frees the objset.
816bc9014e6SJustin Gibbs  *
817bc9014e6SJustin Gibbs  * NOTE: Due to asynchronous eviction processing (invocation of
818bc9014e6SJustin Gibbs  *       dnode_buf_pageout()), it is possible for the meta dnode for the
819bc9014e6SJustin Gibbs  *       objset to have no holds even though os->os_dnodes is not empty.
820bc9014e6SJustin Gibbs  */
821fa9e4066Sahrens void
822503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
823fa9e4066Sahrens {
824503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
825fa9e4066Sahrens 
826b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
827b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, t));
828fa9e4066Sahrens 
82903bad06fSJustin Gibbs 	if (ds)
83003bad06fSJustin Gibbs 		dsl_prop_unregister_all(ds, os);
831fa9e4066Sahrens 
8320a586ceaSMark Shellenbaum 	if (os->os_sa)
8330a586ceaSMark Shellenbaum 		sa_tear_down(os);
8340a586ceaSMark Shellenbaum 
8353b2aab18SMatthew Ahrens 	dmu_objset_evict_dbufs(os);
836ea8dc4b6Seschrock 
837bc9014e6SJustin Gibbs 	mutex_enter(&os->os_lock);
838bc9014e6SJustin Gibbs 	spa_evicting_os_register(os->os_spa, os);
839bc9014e6SJustin Gibbs 	if (list_is_empty(&os->os_dnodes)) {
840bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
841bc9014e6SJustin Gibbs 		dmu_objset_evict_done(os);
842bc9014e6SJustin Gibbs 	} else {
843bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
844bc9014e6SJustin Gibbs 	}
845bc9014e6SJustin Gibbs }
846bc9014e6SJustin Gibbs 
847bc9014e6SJustin Gibbs void
848bc9014e6SJustin Gibbs dmu_objset_evict_done(objset_t *os)
849bc9014e6SJustin Gibbs {
850bc9014e6SJustin Gibbs 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
851bc9014e6SJustin Gibbs 
852744947dcSTom Erickson 	dnode_special_close(&os->os_meta_dnode);
853744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os)) {
854744947dcSTom Erickson 		dnode_special_close(&os->os_userused_dnode);
855744947dcSTom Erickson 		dnode_special_close(&os->os_groupused_dnode);
85614843421SMatthew Ahrens 	}
857503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
858fa9e4066Sahrens 
859dcbf3bd6SGeorge Wilson 	arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
860744947dcSTom Erickson 
861744947dcSTom Erickson 	/*
862744947dcSTom Erickson 	 * This is a barrier to prevent the objset from going away in
863744947dcSTom Erickson 	 * dnode_move() until we can safely ensure that the objset is still in
864744947dcSTom Erickson 	 * use. We consider the objset valid before the barrier and invalid
865744947dcSTom Erickson 	 * after the barrier.
866744947dcSTom Erickson 	 */
867744947dcSTom Erickson 	rw_enter(&os_lock, RW_READER);
868744947dcSTom Erickson 	rw_exit(&os_lock);
869744947dcSTom Erickson 
87054811da5SToomas Soome 	kmem_free(os->os_obj_next_percpu,
87154811da5SToomas Soome 	    os->os_obj_next_percpu_len * sizeof (os->os_obj_next_percpu[0]));
87254811da5SToomas Soome 
873503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
87494c2d0ebSMatthew Ahrens 	mutex_destroy(&os->os_userused_lock);
875503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
876503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
87794c2d0ebSMatthew Ahrens 	for (int i = 0; i < TXG_SIZE; i++) {
87894c2d0ebSMatthew Ahrens 		multilist_destroy(os->os_dirty_dnodes[i]);
87994c2d0ebSMatthew Ahrens 	}
880bc9014e6SJustin Gibbs 	spa_evicting_os_deregister(os->os_spa, os);
881503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
882fa9e4066Sahrens }
883fa9e4066Sahrens 
88471eb0538SChris Kirby timestruc_t
88571eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
88671eb0538SChris Kirby {
88771eb0538SChris Kirby 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
88871eb0538SChris Kirby }
88971eb0538SChris Kirby 
890fa9e4066Sahrens /* called from dsl for meta-objset */
891503ad85cSMatthew Ahrens objset_t *
892c717a561Smaybee dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
893c717a561Smaybee     dmu_objset_type_t type, dmu_tx_t *tx)
894fa9e4066Sahrens {
895503ad85cSMatthew Ahrens 	objset_t *os;
896fa9e4066Sahrens 	dnode_t *mdn;
897fa9e4066Sahrens 
898fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
8993b2aab18SMatthew Ahrens 
900feaa74e4SMark Maybee 	if (ds != NULL)
9013b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_from_ds(ds, &os));
902feaa74e4SMark Maybee 	else
9033b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
904feaa74e4SMark Maybee 
905744947dcSTom Erickson 	mdn = DMU_META_DNODE(os);
906fa9e4066Sahrens 
90754811da5SToomas Soome 	dnode_allocate(mdn, DMU_OT_DNODE, DNODE_BLOCK_SIZE, DN_MAX_INDBLKSHIFT,
90854811da5SToomas Soome 	    DMU_OT_NONE, 0, DNODE_MIN_SLOTS, tx);
909fa9e4066Sahrens 
910fa9e4066Sahrens 	/*
911fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
912fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
913fa9e4066Sahrens 	 * we are also accessing it in open context.
914fa9e4066Sahrens 	 *
915fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
916fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
917fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
918fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
919fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
920fa9e4066Sahrens 	 */
921ea8dc4b6Seschrock 	if (ds != NULL) {
922ea8dc4b6Seschrock 		int levels = 1;
923ea8dc4b6Seschrock 
924ea8dc4b6Seschrock 		/*
925ea8dc4b6Seschrock 		 * Determine the number of levels necessary for the meta-dnode
9264b5c8e93SMatthew Ahrens 		 * to contain DN_MAX_OBJECT dnodes.  Note that in order to
9274b5c8e93SMatthew Ahrens 		 * ensure that we do not overflow 64 bits, there has to be
9284b5c8e93SMatthew Ahrens 		 * a nlevels that gives us a number of blocks > DN_MAX_OBJECT
9294b5c8e93SMatthew Ahrens 		 * but < 2^64.  Therefore,
9304b5c8e93SMatthew Ahrens 		 * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) (10) must be
9314b5c8e93SMatthew Ahrens 		 * less than (64 - log2(DN_MAX_OBJECT)) (16).
932ea8dc4b6Seschrock 		 */
9334b5c8e93SMatthew Ahrens 		while ((uint64_t)mdn->dn_nblkptr <<
9344b5c8e93SMatthew Ahrens 		    (mdn->dn_datablkshift - DNODE_SHIFT +
935ea8dc4b6Seschrock 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
9364b5c8e93SMatthew Ahrens 		    DN_MAX_OBJECT)
937ea8dc4b6Seschrock 			levels++;
938ea8dc4b6Seschrock 
939fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
940ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
941ea8dc4b6Seschrock 	}
942fa9e4066Sahrens 
943fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
944fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
945fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
946503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
947503ad85cSMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
948503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
949503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
95014843421SMatthew Ahrens 	}
951fa9e4066Sahrens 
952fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
953fa9e4066Sahrens 
954503ad85cSMatthew Ahrens 	return (os);
955fa9e4066Sahrens }
956fa9e4066Sahrens 
9573b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg {
9583b2aab18SMatthew Ahrens 	const char *doca_name;
9593b2aab18SMatthew Ahrens 	cred_t *doca_cred;
9603b2aab18SMatthew Ahrens 	void (*doca_userfunc)(objset_t *os, void *arg,
9613b2aab18SMatthew Ahrens 	    cred_t *cr, dmu_tx_t *tx);
9623b2aab18SMatthew Ahrens 	void *doca_userarg;
9633b2aab18SMatthew Ahrens 	dmu_objset_type_t doca_type;
9643b2aab18SMatthew Ahrens 	uint64_t doca_flags;
9653b2aab18SMatthew Ahrens } dmu_objset_create_arg_t;
966fa9e4066Sahrens 
967ecd6cf80Smarks /*ARGSUSED*/
968fa9e4066Sahrens static int
9693b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx)
970fa9e4066Sahrens {
9713b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
9723b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
9733b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
9743b2aab18SMatthew Ahrens 	const char *tail;
9753b2aab18SMatthew Ahrens 	int error;
9761d452cf5Sahrens 
9773b2aab18SMatthew Ahrens 	if (strchr(doca->doca_name, '@') != NULL)
978be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9791d452cf5Sahrens 
9809adfa60dSMatthew Ahrens 	if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
9819adfa60dSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
9829adfa60dSMatthew Ahrens 
9835ac95da7SSerapheim Dimitropoulos 	if (dataset_nestcheck(doca->doca_name) != 0)
9845ac95da7SSerapheim Dimitropoulos 		return (SET_ERROR(ENAMETOOLONG));
9855ac95da7SSerapheim Dimitropoulos 
9863b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
9873b2aab18SMatthew Ahrens 	if (error != 0)
9883b2aab18SMatthew Ahrens 		return (error);
9893b2aab18SMatthew Ahrens 	if (tail == NULL) {
9903b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
991be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
9921d452cf5Sahrens 	}
993a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
994a2afb611SJerry Jelinek 	    doca->doca_cred);
9953b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
996ecd6cf80Smarks 
997a2afb611SJerry Jelinek 	return (error);
9981d452cf5Sahrens }
9991d452cf5Sahrens 
10001d452cf5Sahrens static void
10013b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
10021d452cf5Sahrens {
10033b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
10043b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
10053b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
10063b2aab18SMatthew Ahrens 	const char *tail;
10074445fffbSMatthew Ahrens 	dsl_dataset_t *ds;
10083b2aab18SMatthew Ahrens 	uint64_t obj;
10094445fffbSMatthew Ahrens 	blkptr_t *bp;
10103b2aab18SMatthew Ahrens 	objset_t *os;
1011fa9e4066Sahrens 
10123b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
1013fa9e4066Sahrens 
10143b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
10153b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
1016fa9e4066Sahrens 
10173b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
1018c166b69dSPaul Dagnelie 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
10194445fffbSMatthew Ahrens 	bp = dsl_dataset_get_blkptr(ds);
10203b2aab18SMatthew Ahrens 	os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
10213b2aab18SMatthew Ahrens 	    ds, bp, doca->doca_type, tx);
1022c166b69dSPaul Dagnelie 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
1023fa9e4066Sahrens 
10243b2aab18SMatthew Ahrens 	if (doca->doca_userfunc != NULL) {
10253b2aab18SMatthew Ahrens 		doca->doca_userfunc(os, doca->doca_userarg,
10263b2aab18SMatthew Ahrens 		    doca->doca_cred, tx);
1027fa9e4066Sahrens 	}
1028ecd6cf80Smarks 
10293b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "create", tx, "");
10304445fffbSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
10313b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
1032fa9e4066Sahrens }
1033fa9e4066Sahrens 
1034fa9e4066Sahrens int
1035ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
1036ecd6cf80Smarks     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
1037fa9e4066Sahrens {
10383b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t doca;
1039fa9e4066Sahrens 
10403b2aab18SMatthew Ahrens 	doca.doca_name = name;
10413b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
10423b2aab18SMatthew Ahrens 	doca.doca_flags = flags;
10433b2aab18SMatthew Ahrens 	doca.doca_userfunc = func;
10443b2aab18SMatthew Ahrens 	doca.doca_userarg = arg;
10453b2aab18SMatthew Ahrens 	doca.doca_type = type;
1046ecd6cf80Smarks 
10473b2aab18SMatthew Ahrens 	return (dsl_sync_task(name,
10487d46dc6cSMatthew Ahrens 	    dmu_objset_create_check, dmu_objset_create_sync, &doca,
10497d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
1050ae46e4c7SMatthew Ahrens }
1051ae46e4c7SMatthew Ahrens 
10523b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg {
10533b2aab18SMatthew Ahrens 	const char *doca_clone;
10543b2aab18SMatthew Ahrens 	const char *doca_origin;
10553b2aab18SMatthew Ahrens 	cred_t *doca_cred;
10563b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t;
10573b2aab18SMatthew Ahrens 
10583b2aab18SMatthew Ahrens /*ARGSUSED*/
10593b2aab18SMatthew Ahrens static int
10603b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
1061ae46e4c7SMatthew Ahrens {
10623b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
1063ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
1064ae46e4c7SMatthew Ahrens 	const char *tail;
10653b2aab18SMatthew Ahrens 	int error;
10663b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
10673b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
1068ae46e4c7SMatthew Ahrens 
10693b2aab18SMatthew Ahrens 	if (strchr(doca->doca_clone, '@') != NULL)
1070be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
10713b2aab18SMatthew Ahrens 
10729adfa60dSMatthew Ahrens 	if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
10739adfa60dSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
10749adfa60dSMatthew Ahrens 
10753b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
10763b2aab18SMatthew Ahrens 	if (error != 0)
10773b2aab18SMatthew Ahrens 		return (error);
1078ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
10793b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
1080be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1081fa9e4066Sahrens 	}
108203b1c297SAlexander Eremin 
1083a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1084a2afb611SJerry Jelinek 	    doca->doca_cred);
1085a2afb611SJerry Jelinek 	if (error != 0) {
1086a2afb611SJerry Jelinek 		dsl_dir_rele(pdd, FTAG);
1087a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
1088a2afb611SJerry Jelinek 	}
10893b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
1090fa9e4066Sahrens 
10913b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
10923b2aab18SMatthew Ahrens 	if (error != 0)
109399d5e173STim Haley 		return (error);
109499d5e173STim Haley 
10953b2aab18SMatthew Ahrens 	/* You can only clone snapshots, not the head datasets. */
1096bc9014e6SJustin Gibbs 	if (!origin->ds_is_snapshot) {
10973b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
1098be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
109992241e0bSTom Erickson 	}
11003b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
110199d5e173STim Haley 
11023b2aab18SMatthew Ahrens 	return (0);
1103ea2f5b9eSMatthew Ahrens }
11041d452cf5Sahrens 
11053b2aab18SMatthew Ahrens static void
11063b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
11071d452cf5Sahrens {
11083b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
11093b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
11103b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
11113b2aab18SMatthew Ahrens 	const char *tail;
11123b2aab18SMatthew Ahrens 	dsl_dataset_t *origin, *ds;
11133b2aab18SMatthew Ahrens 	uint64_t obj;
11149adfa60dSMatthew Ahrens 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
11154445fffbSMatthew Ahrens 
11163b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
11173b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
11184445fffbSMatthew Ahrens 
11193b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
11203b2aab18SMatthew Ahrens 	    doca->doca_cred, tx);
1121f2e10be3Srm 
11223b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
11233b2aab18SMatthew Ahrens 	dsl_dataset_name(origin, namebuf);
11243b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "clone", tx,
11253b2aab18SMatthew Ahrens 	    "origin=%s (%llu)", namebuf, origin->ds_object);
11263b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
11273b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
11283b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
11291d452cf5Sahrens }
11301d452cf5Sahrens 
11311d452cf5Sahrens int
11323b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin)
11331d452cf5Sahrens {
11343b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t doca;
11354445fffbSMatthew Ahrens 
11363b2aab18SMatthew Ahrens 	doca.doca_clone = clone;
11373b2aab18SMatthew Ahrens 	doca.doca_origin = origin;
11383b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
113999d5e173STim Haley 
11403b2aab18SMatthew Ahrens 	return (dsl_sync_task(clone,
11417d46dc6cSMatthew Ahrens 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
11427d46dc6cSMatthew Ahrens 	    5, ZFS_SPACE_CHECK_NORMAL));
11434445fffbSMatthew Ahrens }
11444445fffbSMatthew Ahrens 
11455cabbc6bSPrashanth Sreenivasa static int
11465cabbc6bSPrashanth Sreenivasa dmu_objset_remap_indirects_impl(objset_t *os, uint64_t last_removed_txg)
11475cabbc6bSPrashanth Sreenivasa {
11485cabbc6bSPrashanth Sreenivasa 	int error = 0;
11495cabbc6bSPrashanth Sreenivasa 	uint64_t object = 0;
11505cabbc6bSPrashanth Sreenivasa 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
11515cabbc6bSPrashanth Sreenivasa 		error = dmu_object_remap_indirects(os, object,
11525cabbc6bSPrashanth Sreenivasa 		    last_removed_txg);
11535cabbc6bSPrashanth Sreenivasa 		/*
11545cabbc6bSPrashanth Sreenivasa 		 * If the ZPL removed the object before we managed to dnode_hold
11555cabbc6bSPrashanth Sreenivasa 		 * it, we would get an ENOENT. If the ZPL declares its intent
11565cabbc6bSPrashanth Sreenivasa 		 * to remove the object (dnode_free) before we manage to
11575cabbc6bSPrashanth Sreenivasa 		 * dnode_hold it, we would get an EEXIST. In either case, we
11585cabbc6bSPrashanth Sreenivasa 		 * want to continue remapping the other objects in the objset;
11595cabbc6bSPrashanth Sreenivasa 		 * in all other cases, we want to break early.
11605cabbc6bSPrashanth Sreenivasa 		 */
11615cabbc6bSPrashanth Sreenivasa 		if (error != 0 && error != ENOENT && error != EEXIST) {
11625cabbc6bSPrashanth Sreenivasa 			break;
11635cabbc6bSPrashanth Sreenivasa 		}
11645cabbc6bSPrashanth Sreenivasa 	}
11655cabbc6bSPrashanth Sreenivasa 	if (error == ESRCH) {
11665cabbc6bSPrashanth Sreenivasa 		error = 0;
11675cabbc6bSPrashanth Sreenivasa 	}
11685cabbc6bSPrashanth Sreenivasa 	return (error);
11695cabbc6bSPrashanth Sreenivasa }
11705cabbc6bSPrashanth Sreenivasa 
11715cabbc6bSPrashanth Sreenivasa int
11725cabbc6bSPrashanth Sreenivasa dmu_objset_remap_indirects(const char *fsname)
11735cabbc6bSPrashanth Sreenivasa {
11745cabbc6bSPrashanth Sreenivasa 	int error = 0;
11755cabbc6bSPrashanth Sreenivasa 	objset_t *os = NULL;
11765cabbc6bSPrashanth Sreenivasa 	uint64_t last_removed_txg;
11775cabbc6bSPrashanth Sreenivasa 	uint64_t remap_start_txg;
11785cabbc6bSPrashanth Sreenivasa 	dsl_dir_t *dd;
11795cabbc6bSPrashanth Sreenivasa 
11805cabbc6bSPrashanth Sreenivasa 	error = dmu_objset_hold(fsname, FTAG, &os);
11815cabbc6bSPrashanth Sreenivasa 	if (error != 0) {
11825cabbc6bSPrashanth Sreenivasa 		return (error);
11835cabbc6bSPrashanth Sreenivasa 	}
11845cabbc6bSPrashanth Sreenivasa 	dd = dmu_objset_ds(os)->ds_dir;
11855cabbc6bSPrashanth Sreenivasa 
11865cabbc6bSPrashanth Sreenivasa 	if (!spa_feature_is_enabled(dmu_objset_spa(os),
11875cabbc6bSPrashanth Sreenivasa 	    SPA_FEATURE_OBSOLETE_COUNTS)) {
11885cabbc6bSPrashanth Sreenivasa 		dmu_objset_rele(os, FTAG);
11895cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(ENOTSUP));
11905cabbc6bSPrashanth Sreenivasa 	}
11915cabbc6bSPrashanth Sreenivasa 
11925cabbc6bSPrashanth Sreenivasa 	if (dsl_dataset_is_snapshot(dmu_objset_ds(os))) {
11935cabbc6bSPrashanth Sreenivasa 		dmu_objset_rele(os, FTAG);
11945cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EINVAL));
11955cabbc6bSPrashanth Sreenivasa 	}
11965cabbc6bSPrashanth Sreenivasa 
11975cabbc6bSPrashanth Sreenivasa 	/*
11985cabbc6bSPrashanth Sreenivasa 	 * If there has not been a removal, we're done.
11995cabbc6bSPrashanth Sreenivasa 	 */
12005cabbc6bSPrashanth Sreenivasa 	last_removed_txg = spa_get_last_removal_txg(dmu_objset_spa(os));
12015cabbc6bSPrashanth Sreenivasa 	if (last_removed_txg == -1ULL) {
12025cabbc6bSPrashanth Sreenivasa 		dmu_objset_rele(os, FTAG);
12035cabbc6bSPrashanth Sreenivasa 		return (0);
12045cabbc6bSPrashanth Sreenivasa 	}
12055cabbc6bSPrashanth Sreenivasa 
12065cabbc6bSPrashanth Sreenivasa 	/*
12075cabbc6bSPrashanth Sreenivasa 	 * If we have remapped since the last removal, we're done.
12085cabbc6bSPrashanth Sreenivasa 	 */
12095cabbc6bSPrashanth Sreenivasa 	if (dsl_dir_is_zapified(dd)) {
12105cabbc6bSPrashanth Sreenivasa 		uint64_t last_remap_txg;
12115cabbc6bSPrashanth Sreenivasa 		if (zap_lookup(spa_meta_objset(dmu_objset_spa(os)),
12125cabbc6bSPrashanth Sreenivasa 		    dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
12135cabbc6bSPrashanth Sreenivasa 		    sizeof (last_remap_txg), 1, &last_remap_txg) == 0 &&
12145cabbc6bSPrashanth Sreenivasa 		    last_remap_txg > last_removed_txg) {
12155cabbc6bSPrashanth Sreenivasa 			dmu_objset_rele(os, FTAG);
12165cabbc6bSPrashanth Sreenivasa 			return (0);
12175cabbc6bSPrashanth Sreenivasa 		}
12185cabbc6bSPrashanth Sreenivasa 	}
12195cabbc6bSPrashanth Sreenivasa 
12205cabbc6bSPrashanth Sreenivasa 	dsl_dataset_long_hold(dmu_objset_ds(os), FTAG);
12215cabbc6bSPrashanth Sreenivasa 	dsl_pool_rele(dmu_objset_pool(os), FTAG);
12225cabbc6bSPrashanth Sreenivasa 
12235cabbc6bSPrashanth Sreenivasa 	remap_start_txg = spa_last_synced_txg(dmu_objset_spa(os));
12245cabbc6bSPrashanth Sreenivasa 	error = dmu_objset_remap_indirects_impl(os, last_removed_txg);
12255cabbc6bSPrashanth Sreenivasa 	if (error == 0) {
12265cabbc6bSPrashanth Sreenivasa 		/*
12275cabbc6bSPrashanth Sreenivasa 		 * We update the last_remap_txg to be the start txg so that
12285cabbc6bSPrashanth Sreenivasa 		 * we can guarantee that every block older than last_remap_txg
12295cabbc6bSPrashanth Sreenivasa 		 * that can be remapped has been remapped.
12305cabbc6bSPrashanth Sreenivasa 		 */
12315cabbc6bSPrashanth Sreenivasa 		error = dsl_dir_update_last_remap_txg(dd, remap_start_txg);
12325cabbc6bSPrashanth Sreenivasa 	}
12335cabbc6bSPrashanth Sreenivasa 
12345cabbc6bSPrashanth Sreenivasa 	dsl_dataset_long_rele(dmu_objset_ds(os), FTAG);
12355cabbc6bSPrashanth Sreenivasa 	dsl_dataset_rele(dmu_objset_ds(os), FTAG);
12365cabbc6bSPrashanth Sreenivasa 
12375cabbc6bSPrashanth Sreenivasa 	return (error);
12385cabbc6bSPrashanth Sreenivasa }
12395cabbc6bSPrashanth Sreenivasa 
12404445fffbSMatthew Ahrens int
12414445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname)
12424445fffbSMatthew Ahrens {
12434445fffbSMatthew Ahrens 	int err;
12444445fffbSMatthew Ahrens 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
12454445fffbSMatthew Ahrens 	nvlist_t *snaps = fnvlist_alloc();
12464445fffbSMatthew Ahrens 
12474445fffbSMatthew Ahrens 	fnvlist_add_boolean(snaps, longsnap);
12484445fffbSMatthew Ahrens 	strfree(longsnap);
12493b2aab18SMatthew Ahrens 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
12503b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
12514445fffbSMatthew Ahrens 	return (err);
12524445fffbSMatthew Ahrens }
12534445fffbSMatthew Ahrens 
1254fa9e4066Sahrens static void
125594c2d0ebSMatthew Ahrens dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx)
1256fa9e4066Sahrens {
1257c717a561Smaybee 	dnode_t *dn;
1258faafa6e3Sahrens 
125994c2d0ebSMatthew Ahrens 	while ((dn = multilist_sublist_head(list)) != NULL) {
1260c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1261c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
1262c717a561Smaybee 		/*
126314843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
126414843421SMatthew Ahrens 		 * meta-dnode needs to set it ouside dnode_sync().
1265c717a561Smaybee 		 */
1266c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1267c717a561Smaybee 		ASSERT(dn->dn_zio);
1268faafa6e3Sahrens 
1269c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
127094c2d0ebSMatthew Ahrens 		multilist_sublist_remove(list, dn);
127114843421SMatthew Ahrens 
1272aa02ea01STom Caputi 		/*
1273aa02ea01STom Caputi 		 * If we are not doing useraccounting (os_synced_dnodes == NULL)
1274aa02ea01STom Caputi 		 * we are done with this dnode for this txg. Unset dn_dirty_txg
1275aa02ea01STom Caputi 		 * if later txgs aren't dirtying it so that future holders do
1276aa02ea01STom Caputi 		 * not get a stale value. Otherwise, we will do this in
1277aa02ea01STom Caputi 		 * userquota_updates_task() when processing has completely
1278aa02ea01STom Caputi 		 * finished for this txg.
1279aa02ea01STom Caputi 		 */
128094c2d0ebSMatthew Ahrens 		multilist_t *newlist = dn->dn_objset->os_synced_dnodes;
128194c2d0ebSMatthew Ahrens 		if (newlist != NULL) {
128214843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
128394c2d0ebSMatthew Ahrens 			multilist_insert(newlist, dn);
1284aa02ea01STom Caputi 		} else {
1285aa02ea01STom Caputi 			mutex_enter(&dn->dn_mtx);
1286aa02ea01STom Caputi 			if (dn->dn_dirty_txg == tx->tx_txg)
1287aa02ea01STom Caputi 				dn->dn_dirty_txg = 0;
1288aa02ea01STom Caputi 			mutex_exit(&dn->dn_mtx);
128914843421SMatthew Ahrens 		}
129014843421SMatthew Ahrens 
1291c717a561Smaybee 		dnode_sync(dn, tx);
1292fa9e4066Sahrens 	}
1293fa9e4066Sahrens }
1294fa9e4066Sahrens 
1295fa9e4066Sahrens /* ARGSUSED */
1296fa9e4066Sahrens static void
1297b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1298fa9e4066Sahrens {
1299e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1300503ad85cSMatthew Ahrens 	objset_t *os = arg;
1301c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1302fa9e4066Sahrens 
13035d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp));
13043b2aab18SMatthew Ahrens 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
13053b2aab18SMatthew Ahrens 	ASSERT0(BP_GET_LEVEL(bp));
13060a4e9518Sgw 
1307fa9e4066Sahrens 	/*
130814843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
130914843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
131014843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
131114843421SMatthew Ahrens 	 * dnode and user/group accounting objects).
1312fa9e4066Sahrens 	 */
131314843421SMatthew Ahrens 	bp->blk_fill = 0;
1314e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
13155d7b4d43SMatthew Ahrens 		bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1316c166b69dSPaul Dagnelie 	if (os->os_dsl_dataset != NULL)
1317c166b69dSPaul Dagnelie 		rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1318c166b69dSPaul Dagnelie 	*os->os_rootbp = *bp;
1319c166b69dSPaul Dagnelie 	if (os->os_dsl_dataset != NULL)
1320c166b69dSPaul Dagnelie 		rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1321b24ab676SJeff Bonwick }
1322b24ab676SJeff Bonwick 
1323b24ab676SJeff Bonwick /* ARGSUSED */
1324b24ab676SJeff Bonwick static void
1325b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1326b24ab676SJeff Bonwick {
1327b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1328b24ab676SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
1329b24ab676SJeff Bonwick 	objset_t *os = arg;
13300a4e9518Sgw 
1331e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1332b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, bp_orig));
1333e14bb325SJeff Bonwick 	} else {
1334b24ab676SJeff Bonwick 		dsl_dataset_t *ds = os->os_dsl_dataset;
1335b24ab676SJeff Bonwick 		dmu_tx_t *tx = os->os_synctx;
1336b24ab676SJeff Bonwick 
1337b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1338b24ab676SJeff Bonwick 		dsl_dataset_block_born(ds, bp, tx);
13390a4e9518Sgw 	}
1340c166b69dSPaul Dagnelie 	kmem_free(bp, sizeof (*bp));
1341c717a561Smaybee }
1342c717a561Smaybee 
134394c2d0ebSMatthew Ahrens typedef struct sync_dnodes_arg {
134494c2d0ebSMatthew Ahrens 	multilist_t *sda_list;
134594c2d0ebSMatthew Ahrens 	int sda_sublist_idx;
134694c2d0ebSMatthew Ahrens 	multilist_t *sda_newlist;
134794c2d0ebSMatthew Ahrens 	dmu_tx_t *sda_tx;
134894c2d0ebSMatthew Ahrens } sync_dnodes_arg_t;
134994c2d0ebSMatthew Ahrens 
135094c2d0ebSMatthew Ahrens static void
135194c2d0ebSMatthew Ahrens sync_dnodes_task(void *arg)
135294c2d0ebSMatthew Ahrens {
135394c2d0ebSMatthew Ahrens 	sync_dnodes_arg_t *sda = arg;
135494c2d0ebSMatthew Ahrens 
135594c2d0ebSMatthew Ahrens 	multilist_sublist_t *ms =
135694c2d0ebSMatthew Ahrens 	    multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
135794c2d0ebSMatthew Ahrens 
135894c2d0ebSMatthew Ahrens 	dmu_objset_sync_dnodes(ms, sda->sda_tx);
135994c2d0ebSMatthew Ahrens 
136094c2d0ebSMatthew Ahrens 	multilist_sublist_unlock(ms);
136194c2d0ebSMatthew Ahrens 
136294c2d0ebSMatthew Ahrens 	kmem_free(sda, sizeof (*sda));
136394c2d0ebSMatthew Ahrens }
136494c2d0ebSMatthew Ahrens 
136594c2d0ebSMatthew Ahrens 
1366fa9e4066Sahrens /* called from dsl */
1367fa9e4066Sahrens void
1368503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1369fa9e4066Sahrens {
1370fa9e4066Sahrens 	int txgoff;
13717802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
1372b24ab676SJeff Bonwick 	zio_prop_t zp;
1373c717a561Smaybee 	zio_t *zio;
1374c717a561Smaybee 	list_t *list;
1375c717a561Smaybee 	dbuf_dirty_record_t *dr;
1376c166b69dSPaul Dagnelie 	blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1377c166b69dSPaul Dagnelie 	*blkptr_copy = *os->os_rootbp;
1378c717a561Smaybee 
1379c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1380fa9e4066Sahrens 
1381fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1382fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
1383fa9e4066Sahrens 	os->os_synctx = tx;
1384fa9e4066Sahrens 
138587bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
138687bd5c1eSahrens 		/*
138787bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
138887bd5c1eSahrens 		 * spa_max_replication() could change, so reset
138987bd5c1eSahrens 		 * os_copies here.
139087bd5c1eSahrens 		 */
139187bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
139287bd5c1eSahrens 	}
139387bd5c1eSahrens 
1394fa9e4066Sahrens 	/*
1395c717a561Smaybee 	 * Create the root block IO
1396fa9e4066Sahrens 	 */
1397b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1398b24ab676SJeff Bonwick 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1399b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
14001b912ec7SGeorge Wilson 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1401b24ab676SJeff Bonwick 
1402adaec86aSMatthew Ahrens 	dmu_write_policy(os, NULL, 0, 0, &zp);
1403b24ab676SJeff Bonwick 
1404b24ab676SJeff Bonwick 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1405c166b69dSPaul Dagnelie 	    blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
14068df0bcf0SPaul Dagnelie 	    &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
14078df0bcf0SPaul Dagnelie 	    os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1408c717a561Smaybee 
1409c717a561Smaybee 	/*
141014843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
1411c717a561Smaybee 	 */
1412744947dcSTom Erickson 	DMU_META_DNODE(os)->dn_zio = zio;
1413744947dcSTom Erickson 	dnode_sync(DMU_META_DNODE(os), tx);
1414c717a561Smaybee 
141514843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
141614843421SMatthew Ahrens 
1417744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) &&
1418744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1419744947dcSTom Erickson 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1420744947dcSTom Erickson 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1421744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1422744947dcSTom Erickson 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
142314843421SMatthew Ahrens 	}
142414843421SMatthew Ahrens 
1425c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
1426fa9e4066Sahrens 
142714843421SMatthew Ahrens 	if (dmu_objset_userused_enabled(os)) {
142814843421SMatthew Ahrens 		/*
142914843421SMatthew Ahrens 		 * We must create the list here because it uses the
143094c2d0ebSMatthew Ahrens 		 * dn_dirty_link[] of this txg.  But it may already
143194c2d0ebSMatthew Ahrens 		 * exist because we call dsl_dataset_sync() twice per txg.
143214843421SMatthew Ahrens 		 */
143394c2d0ebSMatthew Ahrens 		if (os->os_synced_dnodes == NULL) {
143494c2d0ebSMatthew Ahrens 			os->os_synced_dnodes =
143594c2d0ebSMatthew Ahrens 			    multilist_create(sizeof (dnode_t),
143694c2d0ebSMatthew Ahrens 			    offsetof(dnode_t, dn_dirty_link[txgoff]),
143794c2d0ebSMatthew Ahrens 			    dnode_multilist_index_func);
143894c2d0ebSMatthew Ahrens 		} else {
143994c2d0ebSMatthew Ahrens 			ASSERT3U(os->os_synced_dnodes->ml_offset, ==,
144094c2d0ebSMatthew Ahrens 			    offsetof(dnode_t, dn_dirty_link[txgoff]));
144194c2d0ebSMatthew Ahrens 		}
144214843421SMatthew Ahrens 	}
144314843421SMatthew Ahrens 
144494c2d0ebSMatthew Ahrens 	for (int i = 0;
144594c2d0ebSMatthew Ahrens 	    i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) {
144694c2d0ebSMatthew Ahrens 		sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
144794c2d0ebSMatthew Ahrens 		sda->sda_list = os->os_dirty_dnodes[txgoff];
144894c2d0ebSMatthew Ahrens 		sda->sda_sublist_idx = i;
144994c2d0ebSMatthew Ahrens 		sda->sda_tx = tx;
145094c2d0ebSMatthew Ahrens 		(void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
145194c2d0ebSMatthew Ahrens 		    sync_dnodes_task, sda, 0);
145294c2d0ebSMatthew Ahrens 		/* callback frees sda */
145394c2d0ebSMatthew Ahrens 	}
145494c2d0ebSMatthew Ahrens 	taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
1455fa9e4066Sahrens 
1456744947dcSTom Erickson 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
145794c2d0ebSMatthew Ahrens 	while ((dr = list_head(list)) != NULL) {
14583b2aab18SMatthew Ahrens 		ASSERT0(dr->dr_dbuf->db_level);
1459c717a561Smaybee 		list_remove(list, dr);
1460c717a561Smaybee 		if (dr->dr_zio)
1461c717a561Smaybee 			zio_nowait(dr->dr_zio);
1462c717a561Smaybee 	}
1463af346df5SNed Bass 
1464af346df5SNed Bass 	/* Enable dnode backfill if enough objects have been freed. */
1465af346df5SNed Bass 	if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1466af346df5SNed Bass 		os->os_rescan_dnodes = B_TRUE;
1467af346df5SNed Bass 		os->os_freed_dnodes = 0;
1468af346df5SNed Bass 	}
1469af346df5SNed Bass 
1470c717a561Smaybee 	/*
1471c717a561Smaybee 	 * Free intent log blocks up to this tx.
1472c717a561Smaybee 	 */
1473c717a561Smaybee 	zil_sync(os->os_zil, tx);
1474088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
1475c717a561Smaybee 	zio_nowait(zio);
1476fa9e4066Sahrens }
1477fa9e4066Sahrens 
1478b24ab676SJeff Bonwick boolean_t
1479b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1480b24ab676SJeff Bonwick {
148194c2d0ebSMatthew Ahrens 	return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK]));
1482b24ab676SJeff Bonwick }
1483b24ab676SJeff Bonwick 
1484744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
148514843421SMatthew Ahrens 
148614843421SMatthew Ahrens void
148714843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
148814843421SMatthew Ahrens {
148914843421SMatthew Ahrens 	used_cbs[ost] = cb;
149014843421SMatthew Ahrens }
149114843421SMatthew Ahrens 
149214843421SMatthew Ahrens boolean_t
1493503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
149414843421SMatthew Ahrens {
149514843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1496744947dcSTom Erickson 	    used_cbs[os->os_phys->os_type] != NULL &&
1497744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os) != NULL);
149814843421SMatthew Ahrens }
149914843421SMatthew Ahrens 
15008b70546bSMatthew Ahrens typedef struct userquota_node {
15018b70546bSMatthew Ahrens 	uint64_t uqn_id;
15028b70546bSMatthew Ahrens 	int64_t uqn_delta;
15038b70546bSMatthew Ahrens 	avl_node_t uqn_node;
15048b70546bSMatthew Ahrens } userquota_node_t;
15058b70546bSMatthew Ahrens 
15068b70546bSMatthew Ahrens typedef struct userquota_cache {
15078b70546bSMatthew Ahrens 	avl_tree_t uqc_user_deltas;
15088b70546bSMatthew Ahrens 	avl_tree_t uqc_group_deltas;
15098b70546bSMatthew Ahrens } userquota_cache_t;
15108b70546bSMatthew Ahrens 
15118b70546bSMatthew Ahrens static int
15128b70546bSMatthew Ahrens userquota_compare(const void *l, const void *r)
15138b70546bSMatthew Ahrens {
15148b70546bSMatthew Ahrens 	const userquota_node_t *luqn = l;
15158b70546bSMatthew Ahrens 	const userquota_node_t *ruqn = r;
15168b70546bSMatthew Ahrens 
15178b70546bSMatthew Ahrens 	if (luqn->uqn_id < ruqn->uqn_id)
15188b70546bSMatthew Ahrens 		return (-1);
15198b70546bSMatthew Ahrens 	if (luqn->uqn_id > ruqn->uqn_id)
15208b70546bSMatthew Ahrens 		return (1);
15218b70546bSMatthew Ahrens 	return (0);
15228b70546bSMatthew Ahrens }
15238b70546bSMatthew Ahrens 
15249966ca11SMatthew Ahrens static void
15258b70546bSMatthew Ahrens do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
15268b70546bSMatthew Ahrens {
15278b70546bSMatthew Ahrens 	void *cookie;
15288b70546bSMatthew Ahrens 	userquota_node_t *uqn;
15298b70546bSMatthew Ahrens 
15308b70546bSMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
15318b70546bSMatthew Ahrens 
15328b70546bSMatthew Ahrens 	cookie = NULL;
15338b70546bSMatthew Ahrens 	while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
15348b70546bSMatthew Ahrens 	    &cookie)) != NULL) {
153594c2d0ebSMatthew Ahrens 		/*
153694c2d0ebSMatthew Ahrens 		 * os_userused_lock protects against concurrent calls to
153794c2d0ebSMatthew Ahrens 		 * zap_increment_int().  It's needed because zap_increment_int()
153894c2d0ebSMatthew Ahrens 		 * is not thread-safe (i.e. not atomic).
153994c2d0ebSMatthew Ahrens 		 */
154094c2d0ebSMatthew Ahrens 		mutex_enter(&os->os_userused_lock);
15418b70546bSMatthew Ahrens 		VERIFY0(zap_increment_int(os, DMU_USERUSED_OBJECT,
15428b70546bSMatthew Ahrens 		    uqn->uqn_id, uqn->uqn_delta, tx));
154394c2d0ebSMatthew Ahrens 		mutex_exit(&os->os_userused_lock);
15448b70546bSMatthew Ahrens 		kmem_free(uqn, sizeof (*uqn));
15458b70546bSMatthew Ahrens 	}
15468b70546bSMatthew Ahrens 	avl_destroy(&cache->uqc_user_deltas);
15478b70546bSMatthew Ahrens 
15488b70546bSMatthew Ahrens 	cookie = NULL;
15498b70546bSMatthew Ahrens 	while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
15508b70546bSMatthew Ahrens 	    &cookie)) != NULL) {
155194c2d0ebSMatthew Ahrens 		mutex_enter(&os->os_userused_lock);
15528b70546bSMatthew Ahrens 		VERIFY0(zap_increment_int(os, DMU_GROUPUSED_OBJECT,
15538b70546bSMatthew Ahrens 		    uqn->uqn_id, uqn->uqn_delta, tx));
155494c2d0ebSMatthew Ahrens 		mutex_exit(&os->os_userused_lock);
15558b70546bSMatthew Ahrens 		kmem_free(uqn, sizeof (*uqn));
15568b70546bSMatthew Ahrens 	}
15578b70546bSMatthew Ahrens 	avl_destroy(&cache->uqc_group_deltas);
15588b70546bSMatthew Ahrens }
15598b70546bSMatthew Ahrens 
15608b70546bSMatthew Ahrens static void
15618b70546bSMatthew Ahrens userquota_update_cache(avl_tree_t *avl, uint64_t id, int64_t delta)
15628b70546bSMatthew Ahrens {
15638b70546bSMatthew Ahrens 	userquota_node_t search = { .uqn_id = id };
15648b70546bSMatthew Ahrens 	avl_index_t idx;
15658b70546bSMatthew Ahrens 
15668b70546bSMatthew Ahrens 	userquota_node_t *uqn = avl_find(avl, &search, &idx);
15678b70546bSMatthew Ahrens 	if (uqn == NULL) {
15688b70546bSMatthew Ahrens 		uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
15698b70546bSMatthew Ahrens 		uqn->uqn_id = id;
15708b70546bSMatthew Ahrens 		avl_insert(avl, uqn, idx);
15718b70546bSMatthew Ahrens 	}
15728b70546bSMatthew Ahrens 	uqn->uqn_delta += delta;
15738b70546bSMatthew Ahrens }
15748b70546bSMatthew Ahrens 
15758b70546bSMatthew Ahrens static void
15768b70546bSMatthew Ahrens do_userquota_update(userquota_cache_t *cache, uint64_t used, uint64_t flags,
15778b70546bSMatthew Ahrens     uint64_t user, uint64_t group, boolean_t subtract)
15789966ca11SMatthew Ahrens {
15790a586ceaSMark Shellenbaum 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
158054811da5SToomas Soome 		int64_t delta = DNODE_MIN_SIZE + used;
15819966ca11SMatthew Ahrens 		if (subtract)
15829966ca11SMatthew Ahrens 			delta = -delta;
15838b70546bSMatthew Ahrens 
15848b70546bSMatthew Ahrens 		userquota_update_cache(&cache->uqc_user_deltas, user, delta);
15858b70546bSMatthew Ahrens 		userquota_update_cache(&cache->uqc_group_deltas, group, delta);
15869966ca11SMatthew Ahrens 	}
15879966ca11SMatthew Ahrens }
15889966ca11SMatthew Ahrens 
158994c2d0ebSMatthew Ahrens typedef struct userquota_updates_arg {
159094c2d0ebSMatthew Ahrens 	objset_t *uua_os;
159194c2d0ebSMatthew Ahrens 	int uua_sublist_idx;
159294c2d0ebSMatthew Ahrens 	dmu_tx_t *uua_tx;
159394c2d0ebSMatthew Ahrens } userquota_updates_arg_t;
159494c2d0ebSMatthew Ahrens 
159594c2d0ebSMatthew Ahrens static void
159694c2d0ebSMatthew Ahrens userquota_updates_task(void *arg)
159714843421SMatthew Ahrens {
159894c2d0ebSMatthew Ahrens 	userquota_updates_arg_t *uua = arg;
159994c2d0ebSMatthew Ahrens 	objset_t *os = uua->uua_os;
160094c2d0ebSMatthew Ahrens 	dmu_tx_t *tx = uua->uua_tx;
160114843421SMatthew Ahrens 	dnode_t *dn;
16028b70546bSMatthew Ahrens 	userquota_cache_t cache = { 0 };
160314843421SMatthew Ahrens 
160494c2d0ebSMatthew Ahrens 	multilist_sublist_t *list =
160594c2d0ebSMatthew Ahrens 	    multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx);
160614843421SMatthew Ahrens 
160794c2d0ebSMatthew Ahrens 	ASSERT(multilist_sublist_head(list) == NULL ||
160894c2d0ebSMatthew Ahrens 	    dmu_objset_userused_enabled(os));
16098b70546bSMatthew Ahrens 	avl_create(&cache.uqc_user_deltas, userquota_compare,
16108b70546bSMatthew Ahrens 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
16118b70546bSMatthew Ahrens 	avl_create(&cache.uqc_group_deltas, userquota_compare,
16128b70546bSMatthew Ahrens 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
16138b70546bSMatthew Ahrens 
161494c2d0ebSMatthew Ahrens 	while ((dn = multilist_sublist_head(list)) != NULL) {
16151d8ccc7bSMark Shellenbaum 		int flags;
161614843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
161714843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
161814843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
161914843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
162014843421SMatthew Ahrens 
16211d8ccc7bSMark Shellenbaum 		flags = dn->dn_id_flags;
16221d8ccc7bSMark Shellenbaum 		ASSERT(flags);
16231d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST)  {
16248b70546bSMatthew Ahrens 			do_userquota_update(&cache,
16258b70546bSMatthew Ahrens 			    dn->dn_oldused, dn->dn_oldflags,
16268b70546bSMatthew Ahrens 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE);
16270a586ceaSMark Shellenbaum 		}
16281d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_NEW_EXIST) {
16298b70546bSMatthew Ahrens 			do_userquota_update(&cache,
16308b70546bSMatthew Ahrens 			    DN_USED_BYTES(dn->dn_phys),
16310a586ceaSMark Shellenbaum 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
16328b70546bSMatthew Ahrens 			    dn->dn_newgid, B_FALSE);
16330a586ceaSMark Shellenbaum 		}
16340a586ceaSMark Shellenbaum 
16351d8ccc7bSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
16360a586ceaSMark Shellenbaum 		dn->dn_oldused = 0;
16370a586ceaSMark Shellenbaum 		dn->dn_oldflags = 0;
16380a586ceaSMark Shellenbaum 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
16390a586ceaSMark Shellenbaum 			dn->dn_olduid = dn->dn_newuid;
16400a586ceaSMark Shellenbaum 			dn->dn_oldgid = dn->dn_newgid;
16410a586ceaSMark Shellenbaum 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
16420a586ceaSMark Shellenbaum 			if (dn->dn_bonuslen == 0)
16430a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
16440a586ceaSMark Shellenbaum 			else
16450a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
16460a586ceaSMark Shellenbaum 		}
164728d97a71SMark Shellenbaum 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1648aa02ea01STom Caputi 		if (dn->dn_dirty_txg == spa_syncing_txg(os->os_spa))
1649aa02ea01STom Caputi 			dn->dn_dirty_txg = 0;
165014843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
165114843421SMatthew Ahrens 
165294c2d0ebSMatthew Ahrens 		multilist_sublist_remove(list, dn);
165394c2d0ebSMatthew Ahrens 		dnode_rele(dn, os->os_synced_dnodes);
165414843421SMatthew Ahrens 	}
16558b70546bSMatthew Ahrens 	do_userquota_cacheflush(os, &cache, tx);
165694c2d0ebSMatthew Ahrens 	multilist_sublist_unlock(list);
165794c2d0ebSMatthew Ahrens 	kmem_free(uua, sizeof (*uua));
165894c2d0ebSMatthew Ahrens }
165994c2d0ebSMatthew Ahrens 
166094c2d0ebSMatthew Ahrens void
166194c2d0ebSMatthew Ahrens dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
166294c2d0ebSMatthew Ahrens {
166394c2d0ebSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
166494c2d0ebSMatthew Ahrens 		return;
166594c2d0ebSMatthew Ahrens 
166694c2d0ebSMatthew Ahrens 	/* Allocate the user/groupused objects if necessary. */
166794c2d0ebSMatthew Ahrens 	if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
166894c2d0ebSMatthew Ahrens 		VERIFY0(zap_create_claim(os,
166994c2d0ebSMatthew Ahrens 		    DMU_USERUSED_OBJECT,
167094c2d0ebSMatthew Ahrens 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
167194c2d0ebSMatthew Ahrens 		VERIFY0(zap_create_claim(os,
167294c2d0ebSMatthew Ahrens 		    DMU_GROUPUSED_OBJECT,
167394c2d0ebSMatthew Ahrens 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
167494c2d0ebSMatthew Ahrens 	}
167594c2d0ebSMatthew Ahrens 
167694c2d0ebSMatthew Ahrens 	for (int i = 0;
167794c2d0ebSMatthew Ahrens 	    i < multilist_get_num_sublists(os->os_synced_dnodes); i++) {
167894c2d0ebSMatthew Ahrens 		userquota_updates_arg_t *uua =
167994c2d0ebSMatthew Ahrens 		    kmem_alloc(sizeof (*uua), KM_SLEEP);
168094c2d0ebSMatthew Ahrens 		uua->uua_os = os;
168194c2d0ebSMatthew Ahrens 		uua->uua_sublist_idx = i;
168294c2d0ebSMatthew Ahrens 		uua->uua_tx = tx;
168394c2d0ebSMatthew Ahrens 		/* note: caller does taskq_wait() */
168494c2d0ebSMatthew Ahrens 		(void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
168594c2d0ebSMatthew Ahrens 		    userquota_updates_task, uua, 0);
168694c2d0ebSMatthew Ahrens 		/* callback frees uua */
168794c2d0ebSMatthew Ahrens 	}
168814843421SMatthew Ahrens }
168914843421SMatthew Ahrens 
169006e0070dSMark Shellenbaum /*
169106e0070dSMark Shellenbaum  * Returns a pointer to data to find uid/gid from
169206e0070dSMark Shellenbaum  *
169306e0070dSMark Shellenbaum  * If a dirty record for transaction group that is syncing can't
169406e0070dSMark Shellenbaum  * be found then NULL is returned.  In the NULL case it is assumed
169506e0070dSMark Shellenbaum  * the uid/gid aren't changing.
169606e0070dSMark Shellenbaum  */
169706e0070dSMark Shellenbaum static void *
169806e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
169906e0070dSMark Shellenbaum {
170006e0070dSMark Shellenbaum 	dbuf_dirty_record_t *dr, **drp;
170106e0070dSMark Shellenbaum 	void *data;
170206e0070dSMark Shellenbaum 
170306e0070dSMark Shellenbaum 	if (db->db_dirtycnt == 0)
170406e0070dSMark Shellenbaum 		return (db->db.db_data);  /* Nothing is changing */
170506e0070dSMark Shellenbaum 
170606e0070dSMark Shellenbaum 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
170706e0070dSMark Shellenbaum 		if (dr->dr_txg == tx->tx_txg)
170806e0070dSMark Shellenbaum 			break;
170906e0070dSMark Shellenbaum 
1710744947dcSTom Erickson 	if (dr == NULL) {
171106e0070dSMark Shellenbaum 		data = NULL;
1712744947dcSTom Erickson 	} else {
1713744947dcSTom Erickson 		dnode_t *dn;
1714744947dcSTom Erickson 
1715744947dcSTom Erickson 		DB_DNODE_ENTER(dr->dr_dbuf);
1716744947dcSTom Erickson 		dn = DB_DNODE(dr->dr_dbuf);
1717744947dcSTom Erickson 
1718744947dcSTom Erickson 		if (dn->dn_bonuslen == 0 &&
1719744947dcSTom Erickson 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1720744947dcSTom Erickson 			data = dr->dt.dl.dr_data->b_data;
1721744947dcSTom Erickson 		else
1722744947dcSTom Erickson 			data = dr->dt.dl.dr_data;
1723744947dcSTom Erickson 
1724744947dcSTom Erickson 		DB_DNODE_EXIT(dr->dr_dbuf);
1725744947dcSTom Erickson 	}
1726744947dcSTom Erickson 
172706e0070dSMark Shellenbaum 	return (data);
172806e0070dSMark Shellenbaum }
172906e0070dSMark Shellenbaum 
17300a586ceaSMark Shellenbaum void
173106e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
17320a586ceaSMark Shellenbaum {
17330a586ceaSMark Shellenbaum 	objset_t *os = dn->dn_objset;
17340a586ceaSMark Shellenbaum 	void *data = NULL;
173506e0070dSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
1736d5285caeSGeorge Wilson 	uint64_t *user = NULL;
1737d5285caeSGeorge Wilson 	uint64_t *group = NULL;
17380a586ceaSMark Shellenbaum 	int flags = dn->dn_id_flags;
17390a586ceaSMark Shellenbaum 	int error;
174006e0070dSMark Shellenbaum 	boolean_t have_spill = B_FALSE;
17410a586ceaSMark Shellenbaum 
17420a586ceaSMark Shellenbaum 	if (!dmu_objset_userused_enabled(dn->dn_objset))
17430a586ceaSMark Shellenbaum 		return;
17440a586ceaSMark Shellenbaum 
17450a586ceaSMark Shellenbaum 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
17460a586ceaSMark Shellenbaum 	    DN_ID_CHKED_SPILL)))
17470a586ceaSMark Shellenbaum 		return;
17480a586ceaSMark Shellenbaum 
17490a586ceaSMark Shellenbaum 	if (before && dn->dn_bonuslen != 0)
17500a586ceaSMark Shellenbaum 		data = DN_BONUS(dn->dn_phys);
175106e0070dSMark Shellenbaum 	else if (!before && dn->dn_bonuslen != 0) {
175206e0070dSMark Shellenbaum 		if (dn->dn_bonus) {
175306e0070dSMark Shellenbaum 			db = dn->dn_bonus;
175406e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
175506e0070dSMark Shellenbaum 			data = dmu_objset_userquota_find_data(db, tx);
175606e0070dSMark Shellenbaum 		} else {
175706e0070dSMark Shellenbaum 			data = DN_BONUS(dn->dn_phys);
175806e0070dSMark Shellenbaum 		}
175906e0070dSMark Shellenbaum 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
17600a586ceaSMark Shellenbaum 			int rf = 0;
17610a586ceaSMark Shellenbaum 
17620a586ceaSMark Shellenbaum 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
17630a586ceaSMark Shellenbaum 				rf |= DB_RF_HAVESTRUCT;
17641d8ccc7bSMark Shellenbaum 			error = dmu_spill_hold_by_dnode(dn,
17651d8ccc7bSMark Shellenbaum 			    rf | DB_RF_MUST_SUCCEED,
176606e0070dSMark Shellenbaum 			    FTAG, (dmu_buf_t **)&db);
17670a586ceaSMark Shellenbaum 			ASSERT(error == 0);
176806e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
176906e0070dSMark Shellenbaum 			data = (before) ? db->db.db_data :
177006e0070dSMark Shellenbaum 			    dmu_objset_userquota_find_data(db, tx);
177106e0070dSMark Shellenbaum 			have_spill = B_TRUE;
17720a586ceaSMark Shellenbaum 	} else {
17730a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
17740a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
17750a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
17760a586ceaSMark Shellenbaum 		return;
17770a586ceaSMark Shellenbaum 	}
17780a586ceaSMark Shellenbaum 
17790a586ceaSMark Shellenbaum 	if (before) {
178006e0070dSMark Shellenbaum 		ASSERT(data);
17810a586ceaSMark Shellenbaum 		user = &dn->dn_olduid;
17820a586ceaSMark Shellenbaum 		group = &dn->dn_oldgid;
178306e0070dSMark Shellenbaum 	} else if (data) {
17840a586ceaSMark Shellenbaum 		user = &dn->dn_newuid;
17850a586ceaSMark Shellenbaum 		group = &dn->dn_newgid;
17860a586ceaSMark Shellenbaum 	}
17870a586ceaSMark Shellenbaum 
178806e0070dSMark Shellenbaum 	/*
178906e0070dSMark Shellenbaum 	 * Must always call the callback in case the object
179006e0070dSMark Shellenbaum 	 * type has changed and that type isn't an object type to track
179106e0070dSMark Shellenbaum 	 */
17920a586ceaSMark Shellenbaum 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
17930a586ceaSMark Shellenbaum 	    user, group);
17940a586ceaSMark Shellenbaum 
179506e0070dSMark Shellenbaum 	/*
179606e0070dSMark Shellenbaum 	 * Preserve existing uid/gid when the callback can't determine
179706e0070dSMark Shellenbaum 	 * what the new uid/gid are and the callback returned EEXIST.
179806e0070dSMark Shellenbaum 	 * The EEXIST error tells us to just use the existing uid/gid.
179906e0070dSMark Shellenbaum 	 * If we don't know what the old values are then just assign
180006e0070dSMark Shellenbaum 	 * them to 0, since that is a new file  being created.
180106e0070dSMark Shellenbaum 	 */
180206e0070dSMark Shellenbaum 	if (!before && data == NULL && error == EEXIST) {
180306e0070dSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST) {
180406e0070dSMark Shellenbaum 			dn->dn_newuid = dn->dn_olduid;
180506e0070dSMark Shellenbaum 			dn->dn_newgid = dn->dn_oldgid;
180606e0070dSMark Shellenbaum 		} else {
180706e0070dSMark Shellenbaum 			dn->dn_newuid = 0;
180806e0070dSMark Shellenbaum 			dn->dn_newgid = 0;
180906e0070dSMark Shellenbaum 		}
181006e0070dSMark Shellenbaum 		error = 0;
181106e0070dSMark Shellenbaum 	}
181206e0070dSMark Shellenbaum 
181306e0070dSMark Shellenbaum 	if (db)
181406e0070dSMark Shellenbaum 		mutex_exit(&db->db_mtx);
181506e0070dSMark Shellenbaum 
18160a586ceaSMark Shellenbaum 	mutex_enter(&dn->dn_mtx);
18170a586ceaSMark Shellenbaum 	if (error == 0 && before)
18180a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
18190a586ceaSMark Shellenbaum 	if (error == 0 && !before)
18200a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
18210a586ceaSMark Shellenbaum 
182206e0070dSMark Shellenbaum 	if (have_spill) {
18230a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
18240a586ceaSMark Shellenbaum 	} else {
18250a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
18260a586ceaSMark Shellenbaum 	}
18270a586ceaSMark Shellenbaum 	mutex_exit(&dn->dn_mtx);
182806e0070dSMark Shellenbaum 	if (have_spill)
182906e0070dSMark Shellenbaum 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
18300a586ceaSMark Shellenbaum }
18310a586ceaSMark Shellenbaum 
183214843421SMatthew Ahrens boolean_t
183314843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
183414843421SMatthew Ahrens {
1835503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
183614843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
183714843421SMatthew Ahrens }
183814843421SMatthew Ahrens 
183914843421SMatthew Ahrens int
184014843421SMatthew Ahrens dmu_objset_userspace_upgrade(objset_t *os)
184114843421SMatthew Ahrens {
184214843421SMatthew Ahrens 	uint64_t obj;
184314843421SMatthew Ahrens 	int err = 0;
184414843421SMatthew Ahrens 
184514843421SMatthew Ahrens 	if (dmu_objset_userspace_present(os))
184614843421SMatthew Ahrens 		return (0);
1847503ad85cSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
1848be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
184914843421SMatthew Ahrens 	if (dmu_objset_is_snapshot(os))
1850be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
185114843421SMatthew Ahrens 
185214843421SMatthew Ahrens 	/*
185314843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
185414843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
185514843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
185614843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
185714843421SMatthew Ahrens 	 * independently.
185814843421SMatthew Ahrens 	 */
185914843421SMatthew Ahrens 
186014843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
186168857716SLin Ling 		dmu_tx_t *tx;
186214843421SMatthew Ahrens 		dmu_buf_t *db;
186314843421SMatthew Ahrens 		int objerr;
186414843421SMatthew Ahrens 
186514843421SMatthew Ahrens 		if (issig(JUSTLOOKING) && issig(FORREAL))
1866be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
186714843421SMatthew Ahrens 
186814843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
18693b2aab18SMatthew Ahrens 		if (objerr != 0)
187014843421SMatthew Ahrens 			continue;
187168857716SLin Ling 		tx = dmu_tx_create(os);
187214843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
187314843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
18743b2aab18SMatthew Ahrens 		if (objerr != 0) {
187514843421SMatthew Ahrens 			dmu_tx_abort(tx);
187614843421SMatthew Ahrens 			continue;
187714843421SMatthew Ahrens 		}
187814843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
187914843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
188014843421SMatthew Ahrens 		dmu_tx_commit(tx);
188114843421SMatthew Ahrens 	}
188214843421SMatthew Ahrens 
1883503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
188414843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
188514843421SMatthew Ahrens 	return (0);
188614843421SMatthew Ahrens }
188714843421SMatthew Ahrens 
1888fa9e4066Sahrens void
1889a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1890a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1891fa9e4066Sahrens {
1892503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1893a2eea2e1Sahrens 	    usedobjsp, availobjsp);
1894a2eea2e1Sahrens }
1895a2eea2e1Sahrens 
1896a2eea2e1Sahrens uint64_t
1897a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
1898a2eea2e1Sahrens {
1899503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1900a2eea2e1Sahrens }
1901a2eea2e1Sahrens 
1902a2eea2e1Sahrens void
1903a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1904a2eea2e1Sahrens {
1905503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
1906503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
1907503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1908a2eea2e1Sahrens }
1909a2eea2e1Sahrens 
1910a2eea2e1Sahrens void
1911a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
1912a2eea2e1Sahrens {
1913503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
1914503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
1915a2eea2e1Sahrens 
1916503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1917503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1918a2eea2e1Sahrens 
1919a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1920503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
192114843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
192214843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
1923fa9e4066Sahrens }
1924fa9e4066Sahrens 
1925fa9e4066Sahrens int
1926fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
1927fa9e4066Sahrens {
1928503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
1929bc9014e6SJustin Gibbs 		return (os->os_dsl_dataset->ds_is_snapshot);
1930fa9e4066Sahrens 	else
1931fa9e4066Sahrens 		return (B_FALSE);
1932fa9e4066Sahrens }
1933fa9e4066Sahrens 
1934ab04eb8eStimh int
1935ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1936ab04eb8eStimh     boolean_t *conflict)
1937ab04eb8eStimh {
1938503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1939ab04eb8eStimh 	uint64_t ignored;
1940ab04eb8eStimh 
1941c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1942be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1943ab04eb8eStimh 
1944ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1945c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
19461c17160aSKevin Crowe 	    MT_NORMALIZE, real, maxlen, conflict));
1947ab04eb8eStimh }
1948ab04eb8eStimh 
1949fa9e4066Sahrens int
1950fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1951b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1952fa9e4066Sahrens {
1953503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
1954fa9e4066Sahrens 	zap_cursor_t cursor;
1955fa9e4066Sahrens 	zap_attribute_t attr;
1956fa9e4066Sahrens 
19573b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
19583b2aab18SMatthew Ahrens 
1959c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1960be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
1961fa9e4066Sahrens 
1962fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
1963fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
1964c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1965fa9e4066Sahrens 
196687e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
196787e5029aSahrens 		zap_cursor_fini(&cursor);
1968be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
196987e5029aSahrens 	}
197087e5029aSahrens 
197187e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
197287e5029aSahrens 		zap_cursor_fini(&cursor);
1973be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
197487e5029aSahrens 	}
197587e5029aSahrens 
197687e5029aSahrens 	(void) strcpy(name, attr.za_name);
197787e5029aSahrens 	if (idp)
197887e5029aSahrens 		*idp = attr.za_first_integer;
1979b38f0970Sck 	if (case_conflict)
1980b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
198187e5029aSahrens 	zap_cursor_advance(&cursor);
198287e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
198387e5029aSahrens 	zap_cursor_fini(&cursor);
198487e5029aSahrens 
198587e5029aSahrens 	return (0);
198687e5029aSahrens }
198787e5029aSahrens 
198887e5029aSahrens int
198987e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
199087e5029aSahrens     uint64_t *idp, uint64_t *offp)
199187e5029aSahrens {
1992503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
199387e5029aSahrens 	zap_cursor_t cursor;
199487e5029aSahrens 	zap_attribute_t attr;
199587e5029aSahrens 
199687e5029aSahrens 	/* there is no next dir on a snapshot! */
1997503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
1998c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj)
1999be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
2000fa9e4066Sahrens 
200187e5029aSahrens 	zap_cursor_init_serialized(&cursor,
200287e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
2003c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
200487e5029aSahrens 
200587e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
200687e5029aSahrens 		zap_cursor_fini(&cursor);
2007be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
200887e5029aSahrens 	}
200987e5029aSahrens 
201087e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
201187e5029aSahrens 		zap_cursor_fini(&cursor);
2012be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
201387e5029aSahrens 	}
2014fa9e4066Sahrens 
2015fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
201687e5029aSahrens 	if (idp)
201787e5029aSahrens 		*idp = attr.za_first_integer;
2018fa9e4066Sahrens 	zap_cursor_advance(&cursor);
2019fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
202087e5029aSahrens 	zap_cursor_fini(&cursor);
2021fa9e4066Sahrens 
2022fa9e4066Sahrens 	return (0);
2023fa9e4066Sahrens }
2024fa9e4066Sahrens 
202512380e1eSArne Jansen typedef struct dmu_objset_find_ctx {
202612380e1eSArne Jansen 	taskq_t		*dc_tq;
202712380e1eSArne Jansen 	dsl_pool_t	*dc_dp;
202812380e1eSArne Jansen 	uint64_t	dc_ddobj;
20297588687eSMatthew Ahrens 	char		*dc_ddname; /* last component of ddobj's name */
203012380e1eSArne Jansen 	int		(*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
203112380e1eSArne Jansen 	void		*dc_arg;
203212380e1eSArne Jansen 	int		dc_flags;
203312380e1eSArne Jansen 	kmutex_t	*dc_error_lock;
203412380e1eSArne Jansen 	int		*dc_error;
203512380e1eSArne Jansen } dmu_objset_find_ctx_t;
203612380e1eSArne Jansen 
203712380e1eSArne Jansen static void
203812380e1eSArne Jansen dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
2039088f3894Sahrens {
204012380e1eSArne Jansen 	dsl_pool_t *dp = dcp->dc_dp;
20413b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
20423b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
20433b2aab18SMatthew Ahrens 	zap_cursor_t zc;
20443b2aab18SMatthew Ahrens 	zap_attribute_t *attr;
20453b2aab18SMatthew Ahrens 	uint64_t thisobj;
204612380e1eSArne Jansen 	int err = 0;
20473b2aab18SMatthew Ahrens 
204812380e1eSArne Jansen 	/* don't process if there already was an error */
204912380e1eSArne Jansen 	if (*dcp->dc_error != 0)
205012380e1eSArne Jansen 		goto out;
20513b2aab18SMatthew Ahrens 
20527588687eSMatthew Ahrens 	/*
20537588687eSMatthew Ahrens 	 * Note: passing the name (dc_ddname) here is optional, but it
20547588687eSMatthew Ahrens 	 * improves performance because we don't need to call
20557588687eSMatthew Ahrens 	 * zap_value_search() to determine the name.
20567588687eSMatthew Ahrens 	 */
20577588687eSMatthew Ahrens 	err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
20583b2aab18SMatthew Ahrens 	if (err != 0)
205912380e1eSArne Jansen 		goto out;
20603b2aab18SMatthew Ahrens 
20613b2aab18SMatthew Ahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
20623b2aab18SMatthew Ahrens 	if (dd->dd_myname[0] == '$') {
20633b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
206412380e1eSArne Jansen 		goto out;
20653b2aab18SMatthew Ahrens 	}
20663b2aab18SMatthew Ahrens 
2067c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
20683b2aab18SMatthew Ahrens 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
20693b2aab18SMatthew Ahrens 
20703b2aab18SMatthew Ahrens 	/*
20713b2aab18SMatthew Ahrens 	 * Iterate over all children.
20723b2aab18SMatthew Ahrens 	 */
207312380e1eSArne Jansen 	if (dcp->dc_flags & DS_FIND_CHILDREN) {
20743b2aab18SMatthew Ahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
2075c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
20763b2aab18SMatthew Ahrens 		    zap_cursor_retrieve(&zc, attr) == 0;
20773b2aab18SMatthew Ahrens 		    (void) zap_cursor_advance(&zc)) {
20783b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
20793b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
20803b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
20813b2aab18SMatthew Ahrens 
20827588687eSMatthew Ahrens 			dmu_objset_find_ctx_t *child_dcp =
20837588687eSMatthew Ahrens 			    kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
208412380e1eSArne Jansen 			*child_dcp = *dcp;
208512380e1eSArne Jansen 			child_dcp->dc_ddobj = attr->za_first_integer;
20867588687eSMatthew Ahrens 			child_dcp->dc_ddname = spa_strdup(attr->za_name);
208712380e1eSArne Jansen 			if (dcp->dc_tq != NULL)
208812380e1eSArne Jansen 				(void) taskq_dispatch(dcp->dc_tq,
208912380e1eSArne Jansen 				    dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
209012380e1eSArne Jansen 			else
209112380e1eSArne Jansen 				dmu_objset_find_dp_impl(child_dcp);
20923b2aab18SMatthew Ahrens 		}
20933b2aab18SMatthew Ahrens 		zap_cursor_fini(&zc);
20943b2aab18SMatthew Ahrens 	}
20953b2aab18SMatthew Ahrens 
20963b2aab18SMatthew Ahrens 	/*
20973b2aab18SMatthew Ahrens 	 * Iterate over all snapshots.
20983b2aab18SMatthew Ahrens 	 */
209912380e1eSArne Jansen 	if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
21003b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
21013b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
21023b2aab18SMatthew Ahrens 
21033b2aab18SMatthew Ahrens 		if (err == 0) {
2104c1379625SJustin T. Gibbs 			uint64_t snapobj;
2105c1379625SJustin T. Gibbs 
2106c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
21073b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
21083b2aab18SMatthew Ahrens 
21093b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
21103b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
21113b2aab18SMatthew Ahrens 			    (void) zap_cursor_advance(&zc)) {
21123b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
21133b2aab18SMatthew Ahrens 				    sizeof (uint64_t));
21143b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
21153b2aab18SMatthew Ahrens 
21163b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
21173b2aab18SMatthew Ahrens 				    attr->za_first_integer, FTAG, &ds);
21183b2aab18SMatthew Ahrens 				if (err != 0)
21193b2aab18SMatthew Ahrens 					break;
212012380e1eSArne Jansen 				err = dcp->dc_func(dp, ds, dcp->dc_arg);
21213b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
21223b2aab18SMatthew Ahrens 				if (err != 0)
21233b2aab18SMatthew Ahrens 					break;
21243b2aab18SMatthew Ahrens 			}
21253b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
21263b2aab18SMatthew Ahrens 		}
21273b2aab18SMatthew Ahrens 	}
21283b2aab18SMatthew Ahrens 
21293b2aab18SMatthew Ahrens 	kmem_free(attr, sizeof (zap_attribute_t));
21303b2aab18SMatthew Ahrens 
21317588687eSMatthew Ahrens 	if (err != 0) {
21327588687eSMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
213312380e1eSArne Jansen 		goto out;
21347588687eSMatthew Ahrens 	}
21353b2aab18SMatthew Ahrens 
21363b2aab18SMatthew Ahrens 	/*
21373b2aab18SMatthew Ahrens 	 * Apply to self.
21383b2aab18SMatthew Ahrens 	 */
21393b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
21407588687eSMatthew Ahrens 
21417588687eSMatthew Ahrens 	/*
21427588687eSMatthew Ahrens 	 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
21437588687eSMatthew Ahrens 	 * that the dir will remain cached, and we won't have to re-instantiate
21447588687eSMatthew Ahrens 	 * it (which could be expensive due to finding its name via
21457588687eSMatthew Ahrens 	 * zap_value_search()).
21467588687eSMatthew Ahrens 	 */
21477588687eSMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
21483b2aab18SMatthew Ahrens 	if (err != 0)
214912380e1eSArne Jansen 		goto out;
215012380e1eSArne Jansen 	err = dcp->dc_func(dp, ds, dcp->dc_arg);
21513b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
215212380e1eSArne Jansen 
215312380e1eSArne Jansen out:
215412380e1eSArne Jansen 	if (err != 0) {
215512380e1eSArne Jansen 		mutex_enter(dcp->dc_error_lock);
215612380e1eSArne Jansen 		/* only keep first error */
215712380e1eSArne Jansen 		if (*dcp->dc_error == 0)
215812380e1eSArne Jansen 			*dcp->dc_error = err;
215912380e1eSArne Jansen 		mutex_exit(dcp->dc_error_lock);
216012380e1eSArne Jansen 	}
216112380e1eSArne Jansen 
21627588687eSMatthew Ahrens 	if (dcp->dc_ddname != NULL)
21637588687eSMatthew Ahrens 		spa_strfree(dcp->dc_ddname);
216412380e1eSArne Jansen 	kmem_free(dcp, sizeof (*dcp));
216512380e1eSArne Jansen }
216612380e1eSArne Jansen 
216712380e1eSArne Jansen static void
216812380e1eSArne Jansen dmu_objset_find_dp_cb(void *arg)
216912380e1eSArne Jansen {
217012380e1eSArne Jansen 	dmu_objset_find_ctx_t *dcp = arg;
217112380e1eSArne Jansen 	dsl_pool_t *dp = dcp->dc_dp;
217212380e1eSArne Jansen 
21731d3f896fSArne Jansen 	/*
21741d3f896fSArne Jansen 	 * We need to get a pool_config_lock here, as there are several
21751d3f896fSArne Jansen 	 * asssert(pool_config_held) down the stack. Getting a lock via
21761d3f896fSArne Jansen 	 * dsl_pool_config_enter is risky, as it might be stalled by a
21771d3f896fSArne Jansen 	 * pending writer. This would deadlock, as the write lock can
21781d3f896fSArne Jansen 	 * only be granted when our parent thread gives up the lock.
21791d3f896fSArne Jansen 	 * The _prio interface gives us priority over a pending writer.
21801d3f896fSArne Jansen 	 */
21811d3f896fSArne Jansen 	dsl_pool_config_enter_prio(dp, FTAG);
218212380e1eSArne Jansen 
218312380e1eSArne Jansen 	dmu_objset_find_dp_impl(dcp);
218412380e1eSArne Jansen 
218512380e1eSArne Jansen 	dsl_pool_config_exit(dp, FTAG);
218612380e1eSArne Jansen }
218712380e1eSArne Jansen 
218812380e1eSArne Jansen /*
218912380e1eSArne Jansen  * Find objsets under and including ddobj, call func(ds) on each.
219012380e1eSArne Jansen  * The order for the enumeration is completely undefined.
219112380e1eSArne Jansen  * func is called with dsl_pool_config held.
219212380e1eSArne Jansen  */
219312380e1eSArne Jansen int
219412380e1eSArne Jansen dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
219512380e1eSArne Jansen     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
219612380e1eSArne Jansen {
219712380e1eSArne Jansen 	int error = 0;
219812380e1eSArne Jansen 	taskq_t *tq = NULL;
219912380e1eSArne Jansen 	int ntasks;
220012380e1eSArne Jansen 	dmu_objset_find_ctx_t *dcp;
220112380e1eSArne Jansen 	kmutex_t err_lock;
220212380e1eSArne Jansen 
220312380e1eSArne Jansen 	mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
220412380e1eSArne Jansen 	dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
220512380e1eSArne Jansen 	dcp->dc_tq = NULL;
220612380e1eSArne Jansen 	dcp->dc_dp = dp;
220712380e1eSArne Jansen 	dcp->dc_ddobj = ddobj;
22087588687eSMatthew Ahrens 	dcp->dc_ddname = NULL;
220912380e1eSArne Jansen 	dcp->dc_func = func;
221012380e1eSArne Jansen 	dcp->dc_arg = arg;
221112380e1eSArne Jansen 	dcp->dc_flags = flags;
221212380e1eSArne Jansen 	dcp->dc_error_lock = &err_lock;
221312380e1eSArne Jansen 	dcp->dc_error = &error;
221412380e1eSArne Jansen 
221512380e1eSArne Jansen 	if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
221612380e1eSArne Jansen 		/*
221712380e1eSArne Jansen 		 * In case a write lock is held we can't make use of
221812380e1eSArne Jansen 		 * parallelism, as down the stack of the worker threads
221912380e1eSArne Jansen 		 * the lock is asserted via dsl_pool_config_held.
222012380e1eSArne Jansen 		 * In case of a read lock this is solved by getting a read
222112380e1eSArne Jansen 		 * lock in each worker thread, which isn't possible in case
222212380e1eSArne Jansen 		 * of a writer lock. So we fall back to the synchronous path
222312380e1eSArne Jansen 		 * here.
222412380e1eSArne Jansen 		 * In the future it might be possible to get some magic into
222512380e1eSArne Jansen 		 * dsl_pool_config_held in a way that it returns true for
222612380e1eSArne Jansen 		 * the worker threads so that a single lock held from this
222712380e1eSArne Jansen 		 * thread suffices. For now, stay single threaded.
222812380e1eSArne Jansen 		 */
222912380e1eSArne Jansen 		dmu_objset_find_dp_impl(dcp);
22302bad2258SSteven Hartland 		mutex_destroy(&err_lock);
223112380e1eSArne Jansen 
223212380e1eSArne Jansen 		return (error);
223312380e1eSArne Jansen 	}
223412380e1eSArne Jansen 
223512380e1eSArne Jansen 	ntasks = dmu_find_threads;
223612380e1eSArne Jansen 	if (ntasks == 0)
223712380e1eSArne Jansen 		ntasks = vdev_count_leaves(dp->dp_spa) * 4;
223812380e1eSArne Jansen 	tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
223912380e1eSArne Jansen 	    INT_MAX, 0);
224012380e1eSArne Jansen 	if (tq == NULL) {
224112380e1eSArne Jansen 		kmem_free(dcp, sizeof (*dcp));
22422bad2258SSteven Hartland 		mutex_destroy(&err_lock);
22432bad2258SSteven Hartland 
224412380e1eSArne Jansen 		return (SET_ERROR(ENOMEM));
224512380e1eSArne Jansen 	}
224612380e1eSArne Jansen 	dcp->dc_tq = tq;
224712380e1eSArne Jansen 
224812380e1eSArne Jansen 	/* dcp will be freed by task */
224912380e1eSArne Jansen 	(void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
225012380e1eSArne Jansen 
225112380e1eSArne Jansen 	/*
225212380e1eSArne Jansen 	 * PORTING: this code relies on the property of taskq_wait to wait
225312380e1eSArne Jansen 	 * until no more tasks are queued and no more tasks are active. As
225412380e1eSArne Jansen 	 * we always queue new tasks from within other tasks, task_wait
225512380e1eSArne Jansen 	 * reliably waits for the full recursion to finish, even though we
225612380e1eSArne Jansen 	 * enqueue new tasks after taskq_wait has been called.
225712380e1eSArne Jansen 	 * On platforms other than illumos, taskq_wait may not have this
225812380e1eSArne Jansen 	 * property.
225912380e1eSArne Jansen 	 */
226012380e1eSArne Jansen 	taskq_wait(tq);
226112380e1eSArne Jansen 	taskq_destroy(tq);
226212380e1eSArne Jansen 	mutex_destroy(&err_lock);
226312380e1eSArne Jansen 
226412380e1eSArne Jansen 	return (error);
2265088f3894Sahrens }
2266088f3894Sahrens 
2267088f3894Sahrens /*
22683b2aab18SMatthew Ahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
22693b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when this is called, and it
22703b2aab18SMatthew Ahrens  * will not be held when the callback is called.
22713b2aab18SMatthew Ahrens  * Therefore this function should only be used when the pool is not changing
22723b2aab18SMatthew Ahrens  * (e.g. in syncing context), or the callback can deal with the possible races.
2273088f3894Sahrens  */
22743b2aab18SMatthew Ahrens static int
22753b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name,
22763b2aab18SMatthew Ahrens     int func(const char *, void *), void *arg, int flags)
2277fa9e4066Sahrens {
2278fa9e4066Sahrens 	dsl_dir_t *dd;
22793b2aab18SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2280088f3894Sahrens 	dsl_dataset_t *ds;
2281fa9e4066Sahrens 	zap_cursor_t zc;
2282b7661cccSmmusante 	zap_attribute_t *attr;
2283fa9e4066Sahrens 	char *child;
2284088f3894Sahrens 	uint64_t thisobj;
2285088f3894Sahrens 	int err;
2286fa9e4066Sahrens 
22873b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dp, FTAG);
22883b2aab18SMatthew Ahrens 
22893b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
22903b2aab18SMatthew Ahrens 	if (err != 0) {
22913b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
22921d452cf5Sahrens 		return (err);
22933b2aab18SMatthew Ahrens 	}
2294fa9e4066Sahrens 
2295088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2296088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
22973b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
22983b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
2299088f3894Sahrens 		return (0);
2300088f3894Sahrens 	}
2301088f3894Sahrens 
2302c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2303b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2304fa9e4066Sahrens 
2305fa9e4066Sahrens 	/*
2306fa9e4066Sahrens 	 * Iterate over all children.
2307fa9e4066Sahrens 	 */
23080b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
2309088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
2310c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
2311b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
23120b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
23133b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
23143b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
23153b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
2316fa9e4066Sahrens 
2317486ae710SMatthew Ahrens 			child = kmem_asprintf("%s/%s", name, attr->za_name);
23183b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
23193b2aab18SMatthew Ahrens 			err = dmu_objset_find_impl(spa, child,
23203b2aab18SMatthew Ahrens 			    func, arg, flags);
23213b2aab18SMatthew Ahrens 			dsl_pool_config_enter(dp, FTAG);
2322486ae710SMatthew Ahrens 			strfree(child);
23233b2aab18SMatthew Ahrens 			if (err != 0)
23240b69c2f0Sahrens 				break;
23250b69c2f0Sahrens 		}
23260b69c2f0Sahrens 		zap_cursor_fini(&zc);
23271d452cf5Sahrens 
23283b2aab18SMatthew Ahrens 		if (err != 0) {
23293b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
23303b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
2331b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
23320b69c2f0Sahrens 			return (err);
23330b69c2f0Sahrens 		}
2334fa9e4066Sahrens 	}
2335fa9e4066Sahrens 
2336fa9e4066Sahrens 	/*
2337fa9e4066Sahrens 	 * Iterate over all snapshots.
2338fa9e4066Sahrens 	 */
2339088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
2340088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2341088f3894Sahrens 
2342088f3894Sahrens 		if (err == 0) {
2343c1379625SJustin T. Gibbs 			uint64_t snapobj;
2344c1379625SJustin T. Gibbs 
2345c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2346088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
2347088f3894Sahrens 
2348088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2349088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
2350088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
23513b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
2352088f3894Sahrens 				    sizeof (uint64_t));
23533b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
2354088f3894Sahrens 
2355486ae710SMatthew Ahrens 				child = kmem_asprintf("%s@%s",
2356486ae710SMatthew Ahrens 				    name, attr->za_name);
23573b2aab18SMatthew Ahrens 				dsl_pool_config_exit(dp, FTAG);
23583b2aab18SMatthew Ahrens 				err = func(child, arg);
23593b2aab18SMatthew Ahrens 				dsl_pool_config_enter(dp, FTAG);
2360486ae710SMatthew Ahrens 				strfree(child);
23613b2aab18SMatthew Ahrens 				if (err != 0)
2362088f3894Sahrens 					break;
2363088f3894Sahrens 			}
2364088f3894Sahrens 			zap_cursor_fini(&zc);
2365fa9e4066Sahrens 		}
2366fa9e4066Sahrens 	}
2367fa9e4066Sahrens 
23683b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
2369b7661cccSmmusante 	kmem_free(attr, sizeof (zap_attribute_t));
23703b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dp, FTAG);
2371fa9e4066Sahrens 
23723b2aab18SMatthew Ahrens 	if (err != 0)
23731d452cf5Sahrens 		return (err);
23741d452cf5Sahrens 
23753b2aab18SMatthew Ahrens 	/* Apply to self. */
23763b2aab18SMatthew Ahrens 	return (func(name, arg));
2377fa9e4066Sahrens }
2378f18faf3fSek 
23793b2aab18SMatthew Ahrens /*
23803b2aab18SMatthew Ahrens  * See comment above dmu_objset_find_impl().
23813b2aab18SMatthew Ahrens  */
23827f73c863SRich Morris int
23833b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg,
23843b2aab18SMatthew Ahrens     int flags)
23857f73c863SRich Morris {
23863b2aab18SMatthew Ahrens 	spa_t *spa;
23873b2aab18SMatthew Ahrens 	int error;
23887f73c863SRich Morris 
23893b2aab18SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
23903b2aab18SMatthew Ahrens 	if (error != 0)
23913b2aab18SMatthew Ahrens 		return (error);
23923b2aab18SMatthew Ahrens 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
23933b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
23943b2aab18SMatthew Ahrens 	return (error);
23957f73c863SRich Morris }
23967f73c863SRich Morris 
2397f18faf3fSek void
2398f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr)
2399f18faf3fSek {
2400503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2401503ad85cSMatthew Ahrens 	os->os_user_ptr = user_ptr;
2402f18faf3fSek }
2403f18faf3fSek 
2404f18faf3fSek void *
2405f18faf3fSek dmu_objset_get_user(objset_t *os)
2406f18faf3fSek {
2407503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2408503ad85cSMatthew Ahrens 	return (os->os_user_ptr);
2409f18faf3fSek }
24103b2aab18SMatthew Ahrens 
24113b2aab18SMatthew Ahrens /*
24123b2aab18SMatthew Ahrens  * Determine name of filesystem, given name of snapshot.
24139adfa60dSMatthew Ahrens  * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
24143b2aab18SMatthew Ahrens  */
24153b2aab18SMatthew Ahrens int
24163b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf)
24173b2aab18SMatthew Ahrens {
24183b2aab18SMatthew Ahrens 	char *atp = strchr(snapname, '@');
24193b2aab18SMatthew Ahrens 	if (atp == NULL)
2420be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
24219adfa60dSMatthew Ahrens 	if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2422be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
24233b2aab18SMatthew Ahrens 	(void) strlcpy(buf, snapname, atp - snapname + 1);
24243b2aab18SMatthew Ahrens 	return (0);
24253b2aab18SMatthew Ahrens }
242661e255ceSMatthew Ahrens 
242761e255ceSMatthew Ahrens /*
242861e255ceSMatthew Ahrens  * Call when we think we're going to write/free space in open context to track
242961e255ceSMatthew Ahrens  * the amount of dirty data in the open txg, which is also the amount
243061e255ceSMatthew Ahrens  * of memory that can not be evicted until this txg syncs.
243161e255ceSMatthew Ahrens  */
243261e255ceSMatthew Ahrens void
243361e255ceSMatthew Ahrens dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
243461e255ceSMatthew Ahrens {
243561e255ceSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
243661e255ceSMatthew Ahrens 	int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
243761e255ceSMatthew Ahrens 
243861e255ceSMatthew Ahrens 	if (ds != NULL) {
243961e255ceSMatthew Ahrens 		dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
244061e255ceSMatthew Ahrens 		dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
244161e255ceSMatthew Ahrens 	}
244261e255ceSMatthew Ahrens }
2443