xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision ef96fc31)
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.
26f67950b2SNasf-Fan  * Copyright 2019 Joyent, Inc.
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.
31a39df08cSArkadiusz Bubała  * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32fa9e4066Sahrens  */
33fa9e4066Sahrens 
3455da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
3555da60b9SMark J Musante 
36f67950b2SNasf-Fan #include <sys/zfeature.h>
37ecd6cf80Smarks #include <sys/cred.h>
38fa9e4066Sahrens #include <sys/zfs_context.h>
39fa9e4066Sahrens #include <sys/dmu_objset.h>
40fa9e4066Sahrens #include <sys/dsl_dir.h>
41fa9e4066Sahrens #include <sys/dsl_dataset.h>
42fa9e4066Sahrens #include <sys/dsl_prop.h>
43fa9e4066Sahrens #include <sys/dsl_pool.h>
441d452cf5Sahrens #include <sys/dsl_synctask.h>
45ecd6cf80Smarks #include <sys/dsl_deleg.h>
46fa9e4066Sahrens #include <sys/dnode.h>
47fa9e4066Sahrens #include <sys/dbuf.h>
48a2eea2e1Sahrens #include <sys/zvol.h>
49fa9e4066Sahrens #include <sys/dmu_tx.h>
50fa9e4066Sahrens #include <sys/zap.h>
51fa9e4066Sahrens #include <sys/zil.h>
52fa9e4066Sahrens #include <sys/dmu_impl.h>
53ecd6cf80Smarks #include <sys/zfs_ioctl.h>
540a586ceaSMark Shellenbaum #include <sys/sa.h>
5599d5e173STim Haley #include <sys/zfs_onexit.h>
563b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
5712380e1eSArne Jansen #include <sys/vdev.h>
585cabbc6bSPrashanth Sreenivasa #include <sys/zfeature.h>
59f67950b2SNasf-Fan #include <sys/spa_impl.h>
60eb633035STom Caputi #include <sys/dmu_recv.h>
61f67950b2SNasf-Fan #include <sys/zfs_project.h>
625ac95da7SSerapheim Dimitropoulos #include "zfs_namecheck.h"
63fa9e4066Sahrens 
64744947dcSTom Erickson /*
65744947dcSTom Erickson  * Needed to close a window in dnode_move() that allows the objset to be freed
66744947dcSTom Erickson  * before it can be safely accessed.
67744947dcSTom Erickson  */
68744947dcSTom Erickson krwlock_t os_lock;
69744947dcSTom Erickson 
7012380e1eSArne Jansen /*
7112380e1eSArne Jansen  * Tunable to overwrite the maximum number of threads for the parallization
7212380e1eSArne Jansen  * of dmu_objset_find_dp, needed to speed up the import of pools with many
7312380e1eSArne Jansen  * datasets.
7412380e1eSArne Jansen  * Default is 4 times the number of leaf vdevs.
7512380e1eSArne Jansen  */
7612380e1eSArne Jansen int dmu_find_threads = 0;
7712380e1eSArne Jansen 
78af346df5SNed Bass /*
79af346df5SNed Bass  * Backfill lower metadnode objects after this many have been freed.
80af346df5SNed Bass  * Backfilling negatively impacts object creation rates, so only do it
81af346df5SNed Bass  * if there are enough holes to fill.
82af346df5SNed Bass  */
83af346df5SNed Bass int dmu_rescan_dnode_threshold = 131072;
84af346df5SNed Bass 
85a39df08cSArkadiusz Bubała static char *upgrade_tag = "upgrade_tag";
86a39df08cSArkadiusz Bubała 
8712380e1eSArne Jansen static void dmu_objset_find_dp_cb(void *arg);
8812380e1eSArne Jansen 
89f67950b2SNasf-Fan static void dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb);
90f67950b2SNasf-Fan static void dmu_objset_upgrade_stop(objset_t *os);
91f67950b2SNasf-Fan 
92744947dcSTom Erickson void
dmu_objset_init(void)93744947dcSTom Erickson dmu_objset_init(void)
94744947dcSTom Erickson {
95744947dcSTom Erickson 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
96744947dcSTom Erickson }
97744947dcSTom Erickson 
98744947dcSTom Erickson void
dmu_objset_fini(void)99744947dcSTom Erickson dmu_objset_fini(void)
100744947dcSTom Erickson {
101744947dcSTom Erickson 	rw_destroy(&os_lock);
102744947dcSTom Erickson }
103744947dcSTom Erickson 
104fa9e4066Sahrens spa_t *
dmu_objset_spa(objset_t * os)105fa9e4066Sahrens dmu_objset_spa(objset_t *os)
106fa9e4066Sahrens {
107503ad85cSMatthew Ahrens 	return (os->os_spa);
108fa9e4066Sahrens }
109fa9e4066Sahrens 
110fa9e4066Sahrens zilog_t *
dmu_objset_zil(objset_t * os)111fa9e4066Sahrens dmu_objset_zil(objset_t *os)
112fa9e4066Sahrens {
113503ad85cSMatthew Ahrens 	return (os->os_zil);
114fa9e4066Sahrens }
115fa9e4066Sahrens 
116fa9e4066Sahrens dsl_pool_t *
dmu_objset_pool(objset_t * os)117fa9e4066Sahrens dmu_objset_pool(objset_t *os)
118fa9e4066Sahrens {
119fa9e4066Sahrens 	dsl_dataset_t *ds;
120fa9e4066Sahrens 
121503ad85cSMatthew Ahrens 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
122fa9e4066Sahrens 		return (ds->ds_dir->dd_pool);
123fa9e4066Sahrens 	else
124503ad85cSMatthew Ahrens 		return (spa_get_dsl(os->os_spa));
125fa9e4066Sahrens }
126fa9e4066Sahrens 
127fa9e4066Sahrens dsl_dataset_t *
dmu_objset_ds(objset_t * os)128fa9e4066Sahrens dmu_objset_ds(objset_t *os)
129fa9e4066Sahrens {
130503ad85cSMatthew Ahrens 	return (os->os_dsl_dataset);
131fa9e4066Sahrens }
132fa9e4066Sahrens 
133fa9e4066Sahrens dmu_objset_type_t
dmu_objset_type(objset_t * os)134fa9e4066Sahrens dmu_objset_type(objset_t *os)
135fa9e4066Sahrens {
136503ad85cSMatthew Ahrens 	return (os->os_phys->os_type);
137fa9e4066Sahrens }
138fa9e4066Sahrens 
139fa9e4066Sahrens void
dmu_objset_name(objset_t * os,char * buf)140fa9e4066Sahrens dmu_objset_name(objset_t *os, char *buf)
141fa9e4066Sahrens {
142503ad85cSMatthew Ahrens 	dsl_dataset_name(os->os_dsl_dataset, buf);
143fa9e4066Sahrens }
144fa9e4066Sahrens 
145fa9e4066Sahrens uint64_t
dmu_objset_id(objset_t * os)146fa9e4066Sahrens dmu_objset_id(objset_t *os)
147fa9e4066Sahrens {
148503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
149fa9e4066Sahrens 
150fa9e4066Sahrens 	return (ds ? ds->ds_object : 0);
151fa9e4066Sahrens }
152fa9e4066Sahrens 
15354811da5SToomas Soome uint64_t
dmu_objset_dnodesize(objset_t * os)15454811da5SToomas Soome dmu_objset_dnodesize(objset_t *os)
15554811da5SToomas Soome {
15654811da5SToomas Soome 	return (os->os_dnodesize);
15754811da5SToomas Soome }
15854811da5SToomas Soome 
159edf345e6SMatthew Ahrens zfs_sync_type_t
dmu_objset_syncprop(objset_t * os)16055da60b9SMark J Musante dmu_objset_syncprop(objset_t *os)
16155da60b9SMark J Musante {
16255da60b9SMark J Musante 	return (os->os_sync);
16355da60b9SMark J Musante }
16455da60b9SMark J Musante 
165edf345e6SMatthew Ahrens zfs_logbias_op_t
dmu_objset_logbias(objset_t * os)166e09fa4daSNeil Perrin dmu_objset_logbias(objset_t *os)
167e09fa4daSNeil Perrin {
168e09fa4daSNeil Perrin 	return (os->os_logbias);
169e09fa4daSNeil Perrin }
170e09fa4daSNeil Perrin 
171fa9e4066Sahrens static void
checksum_changed_cb(void * arg,uint64_t newval)172fa9e4066Sahrens checksum_changed_cb(void *arg, uint64_t newval)
173fa9e4066Sahrens {
174503ad85cSMatthew Ahrens 	objset_t *os = arg;
175fa9e4066Sahrens 
176fa9e4066Sahrens 	/*
177fa9e4066Sahrens 	 * Inheritance should have been done by now.
178fa9e4066Sahrens 	 */
179fa9e4066Sahrens 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
180fa9e4066Sahrens 
181503ad85cSMatthew Ahrens 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
182fa9e4066Sahrens }
183fa9e4066Sahrens 
184fa9e4066Sahrens static void
compression_changed_cb(void * arg,uint64_t newval)185fa9e4066Sahrens compression_changed_cb(void *arg, uint64_t newval)
186fa9e4066Sahrens {
187503ad85cSMatthew Ahrens 	objset_t *os = arg;
188fa9e4066Sahrens 
189fa9e4066Sahrens 	/*
190fa9e4066Sahrens 	 * Inheritance and range checking should have been done by now.
191fa9e4066Sahrens 	 */
192fa9e4066Sahrens 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
193fa9e4066Sahrens 
194db1741f5SJustin T. Gibbs 	os->os_compress = zio_compress_select(os->os_spa, newval,
195db1741f5SJustin T. Gibbs 	    ZIO_COMPRESS_ON);
196fa9e4066Sahrens }
197fa9e4066Sahrens 
198d0ad202dSahrens static void
copies_changed_cb(void * arg,uint64_t newval)199d0ad202dSahrens copies_changed_cb(void *arg, uint64_t newval)
200d0ad202dSahrens {
201503ad85cSMatthew Ahrens 	objset_t *os = arg;
202d0ad202dSahrens 
203d0ad202dSahrens 	/*
204d0ad202dSahrens 	 * Inheritance and range checking should have been done by now.
205d0ad202dSahrens 	 */
206d0ad202dSahrens 	ASSERT(newval > 0);
207503ad85cSMatthew Ahrens 	ASSERT(newval <= spa_max_replication(os->os_spa));
208d0ad202dSahrens 
209503ad85cSMatthew Ahrens 	os->os_copies = newval;
210d0ad202dSahrens }
211d0ad202dSahrens 
212b24ab676SJeff Bonwick static void
dedup_changed_cb(void * arg,uint64_t newval)213b24ab676SJeff Bonwick dedup_changed_cb(void *arg, uint64_t newval)
214b24ab676SJeff Bonwick {
215b24ab676SJeff Bonwick 	objset_t *os = arg;
216b24ab676SJeff Bonwick 	spa_t *spa = os->os_spa;
217b24ab676SJeff Bonwick 	enum zio_checksum checksum;
218b24ab676SJeff Bonwick 
219b24ab676SJeff Bonwick 	/*
220b24ab676SJeff Bonwick 	 * Inheritance should have been done by now.
221b24ab676SJeff Bonwick 	 */
222b24ab676SJeff Bonwick 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
223b24ab676SJeff Bonwick 
224b24ab676SJeff Bonwick 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
225b24ab676SJeff Bonwick 
226b24ab676SJeff Bonwick 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
227b24ab676SJeff Bonwick 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
228b24ab676SJeff Bonwick }
229b24ab676SJeff Bonwick 
2303baa08fcSek static void
primary_cache_changed_cb(void * arg,uint64_t newval)2313baa08fcSek primary_cache_changed_cb(void *arg, uint64_t newval)
2323baa08fcSek {
233503ad85cSMatthew Ahrens 	objset_t *os = arg;
2343baa08fcSek 
2353baa08fcSek 	/*
2363baa08fcSek 	 * Inheritance and range checking should have been done by now.
2373baa08fcSek 	 */
2383baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2393baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2403baa08fcSek 
241503ad85cSMatthew Ahrens 	os->os_primary_cache = newval;
2423baa08fcSek }
2433baa08fcSek 
2443baa08fcSek static void
secondary_cache_changed_cb(void * arg,uint64_t newval)2453baa08fcSek secondary_cache_changed_cb(void *arg, uint64_t newval)
2463baa08fcSek {
247503ad85cSMatthew Ahrens 	objset_t *os = arg;
2483baa08fcSek 
2493baa08fcSek 	/*
2503baa08fcSek 	 * Inheritance and range checking should have been done by now.
2513baa08fcSek 	 */
2523baa08fcSek 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
2533baa08fcSek 	    newval == ZFS_CACHE_METADATA);
2543baa08fcSek 
255503ad85cSMatthew Ahrens 	os->os_secondary_cache = newval;
2563baa08fcSek }
2573baa08fcSek 
25855da60b9SMark J Musante static void
sync_changed_cb(void * arg,uint64_t newval)25955da60b9SMark J Musante sync_changed_cb(void *arg, uint64_t newval)
26055da60b9SMark J Musante {
26155da60b9SMark J Musante 	objset_t *os = arg;
26255da60b9SMark J Musante 
26355da60b9SMark J Musante 	/*
26455da60b9SMark J Musante 	 * Inheritance and range checking should have been done by now.
26555da60b9SMark J Musante 	 */
26655da60b9SMark J Musante 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
26755da60b9SMark J Musante 	    newval == ZFS_SYNC_DISABLED);
26855da60b9SMark J Musante 
26955da60b9SMark J Musante 	os->os_sync = newval;
27055da60b9SMark J Musante 	if (os->os_zil)
27155da60b9SMark J Musante 		zil_set_sync(os->os_zil, newval);
27255da60b9SMark J Musante }
27355da60b9SMark J Musante 
274edf345e6SMatthew Ahrens static void
redundant_metadata_changed_cb(void * arg,uint64_t newval)275edf345e6SMatthew Ahrens redundant_metadata_changed_cb(void *arg, uint64_t newval)
276edf345e6SMatthew Ahrens {
277edf345e6SMatthew Ahrens 	objset_t *os = arg;
278edf345e6SMatthew Ahrens 
279edf345e6SMatthew Ahrens 	/*
280edf345e6SMatthew Ahrens 	 * Inheritance and range checking should have been done by now.
281edf345e6SMatthew Ahrens 	 */
282edf345e6SMatthew Ahrens 	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
283edf345e6SMatthew Ahrens 	    newval == ZFS_REDUNDANT_METADATA_MOST);
284edf345e6SMatthew Ahrens 
285edf345e6SMatthew Ahrens 	os->os_redundant_metadata = newval;
286edf345e6SMatthew Ahrens }
287edf345e6SMatthew Ahrens 
28854811da5SToomas Soome static void
dnodesize_changed_cb(void * arg,uint64_t newval)28954811da5SToomas Soome dnodesize_changed_cb(void *arg, uint64_t newval)
29054811da5SToomas Soome {
29154811da5SToomas Soome 	objset_t *os = arg;
29254811da5SToomas Soome 
29354811da5SToomas Soome 	switch (newval) {
29454811da5SToomas Soome 	case ZFS_DNSIZE_LEGACY:
29554811da5SToomas Soome 		os->os_dnodesize = DNODE_MIN_SIZE;
29654811da5SToomas Soome 		break;
29754811da5SToomas Soome 	case ZFS_DNSIZE_AUTO:
29854811da5SToomas Soome 		/*
29954811da5SToomas Soome 		 * Choose a dnode size that will work well for most
30054811da5SToomas Soome 		 * workloads if the user specified "auto". Future code
30154811da5SToomas Soome 		 * improvements could dynamically select a dnode size
30254811da5SToomas Soome 		 * based on observed workload patterns.
30354811da5SToomas Soome 		 */
30454811da5SToomas Soome 		os->os_dnodesize = DNODE_MIN_SIZE * 2;
30554811da5SToomas Soome 		break;
30654811da5SToomas Soome 	case ZFS_DNSIZE_1K:
30754811da5SToomas Soome 	case ZFS_DNSIZE_2K:
30854811da5SToomas Soome 	case ZFS_DNSIZE_4K:
30954811da5SToomas Soome 	case ZFS_DNSIZE_8K:
31054811da5SToomas Soome 	case ZFS_DNSIZE_16K:
31154811da5SToomas Soome 		os->os_dnodesize = newval;
31254811da5SToomas Soome 		break;
31354811da5SToomas Soome 	}
31454811da5SToomas Soome }
31554811da5SToomas Soome 
316663207adSDon Brady static void
smallblk_changed_cb(void * arg,uint64_t newval)317663207adSDon Brady smallblk_changed_cb(void *arg, uint64_t newval)
318663207adSDon Brady {
319663207adSDon Brady 	objset_t *os = arg;
320663207adSDon Brady 
321663207adSDon Brady 	/*
322663207adSDon Brady 	 * Inheritance and range checking should have been done by now.
323663207adSDon Brady 	 */
324663207adSDon Brady 	ASSERT(newval <= SPA_OLD_MAXBLOCKSIZE);
325663207adSDon Brady 	ASSERT(ISP2(newval));
326663207adSDon Brady 
327663207adSDon Brady 	os->os_zpl_special_smallblock = newval;
328663207adSDon Brady }
329663207adSDon Brady 
330e09fa4daSNeil Perrin static void
logbias_changed_cb(void * arg,uint64_t newval)331e09fa4daSNeil Perrin logbias_changed_cb(void *arg, uint64_t newval)
332e09fa4daSNeil Perrin {
333e09fa4daSNeil Perrin 	objset_t *os = arg;
334e09fa4daSNeil Perrin 
335e09fa4daSNeil Perrin 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
336e09fa4daSNeil Perrin 	    newval == ZFS_LOGBIAS_THROUGHPUT);
337e09fa4daSNeil Perrin 	os->os_logbias = newval;
338e09fa4daSNeil Perrin 	if (os->os_zil)
339e09fa4daSNeil Perrin 		zil_set_logbias(os->os_zil, newval);
340e09fa4daSNeil Perrin }
341e09fa4daSNeil Perrin 
342b5152584SMatthew Ahrens static void
recordsize_changed_cb(void * arg,uint64_t newval)343b5152584SMatthew Ahrens recordsize_changed_cb(void *arg, uint64_t newval)
344b5152584SMatthew Ahrens {
345b5152584SMatthew Ahrens 	objset_t *os = arg;
346b5152584SMatthew Ahrens 
347b5152584SMatthew Ahrens 	os->os_recordsize = newval;
348b5152584SMatthew Ahrens }
349b5152584SMatthew Ahrens 
350fa9e4066Sahrens void
dmu_objset_byteswap(void * buf,size_t size)351fa9e4066Sahrens dmu_objset_byteswap(void *buf, size_t size)
352fa9e4066Sahrens {
353fa9e4066Sahrens 	objset_phys_t *osp = buf;
354fa9e4066Sahrens 
355f67950b2SNasf-Fan 	ASSERT(size == OBJSET_PHYS_SIZE_V1 || size == OBJSET_PHYS_SIZE_V2 ||
356f67950b2SNasf-Fan 	    size == sizeof (objset_phys_t));
357fa9e4066Sahrens 	dnode_byteswap(&osp->os_meta_dnode);
358fa9e4066Sahrens 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
359fa9e4066Sahrens 	osp->os_type = BSWAP_64(osp->os_type);
36014843421SMatthew Ahrens 	osp->os_flags = BSWAP_64(osp->os_flags);
361f67950b2SNasf-Fan 	if (size >= OBJSET_PHYS_SIZE_V2) {
36214843421SMatthew Ahrens 		dnode_byteswap(&osp->os_userused_dnode);
36314843421SMatthew Ahrens 		dnode_byteswap(&osp->os_groupused_dnode);
364f67950b2SNasf-Fan 		if (size >= sizeof (objset_phys_t))
365f67950b2SNasf-Fan 			dnode_byteswap(&osp->os_projectused_dnode);
36614843421SMatthew Ahrens 	}
367fa9e4066Sahrens }
368fa9e4066Sahrens 
36994c2d0ebSMatthew Ahrens /*
37094c2d0ebSMatthew Ahrens  * The hash is a CRC-based hash of the objset_t pointer and the object number.
37194c2d0ebSMatthew Ahrens  */
37294c2d0ebSMatthew Ahrens static uint64_t
dnode_hash(const objset_t * os,uint64_t obj)37394c2d0ebSMatthew Ahrens dnode_hash(const objset_t *os, uint64_t obj)
37494c2d0ebSMatthew Ahrens {
37594c2d0ebSMatthew Ahrens 	uintptr_t osv = (uintptr_t)os;
37694c2d0ebSMatthew Ahrens 	uint64_t crc = -1ULL;
37794c2d0ebSMatthew Ahrens 
37894c2d0ebSMatthew Ahrens 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
37994c2d0ebSMatthew Ahrens 	/*
38094c2d0ebSMatthew Ahrens 	 * The low 6 bits of the pointer don't have much entropy, because
38194c2d0ebSMatthew Ahrens 	 * the objset_t is larger than 2^6 bytes long.
38294c2d0ebSMatthew Ahrens 	 */
38394c2d0ebSMatthew Ahrens 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
38494c2d0ebSMatthew Ahrens 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
38594c2d0ebSMatthew Ahrens 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
38694c2d0ebSMatthew Ahrens 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 16)) & 0xFF];
38794c2d0ebSMatthew Ahrens 
38894c2d0ebSMatthew Ahrens 	crc ^= (osv>>14) ^ (obj>>24);
38994c2d0ebSMatthew Ahrens 
39094c2d0ebSMatthew Ahrens 	return (crc);
39194c2d0ebSMatthew Ahrens }
39294c2d0ebSMatthew Ahrens 
39394c2d0ebSMatthew Ahrens unsigned int
dnode_multilist_index_func(multilist_t * ml,void * obj)39494c2d0ebSMatthew Ahrens dnode_multilist_index_func(multilist_t *ml, void *obj)
39594c2d0ebSMatthew Ahrens {
39694c2d0ebSMatthew Ahrens 	dnode_t *dn = obj;
39794c2d0ebSMatthew Ahrens 	return (dnode_hash(dn->dn_objset, dn->dn_object) %
39894c2d0ebSMatthew Ahrens 	    multilist_get_num_sublists(ml));
39994c2d0ebSMatthew Ahrens }
40094c2d0ebSMatthew Ahrens 
40194c2d0ebSMatthew Ahrens /*
40294c2d0ebSMatthew Ahrens  * Instantiates the objset_t in-memory structure corresponding to the
40394c2d0ebSMatthew Ahrens  * objset_phys_t that's pointed to by the specified blkptr_t.
40494c2d0ebSMatthew Ahrens  */
405ea8dc4b6Seschrock int
dmu_objset_open_impl(spa_t * spa,dsl_dataset_t * ds,blkptr_t * bp,objset_t ** osp)406ea8dc4b6Seschrock dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
407503ad85cSMatthew Ahrens     objset_t **osp)
408fa9e4066Sahrens {
409503ad85cSMatthew Ahrens 	objset_t *os;
410088f3894Sahrens 	int i, err;
411fa9e4066Sahrens 
41291ebeef5Sahrens 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
41391ebeef5Sahrens 
414a3874b8bSToomas Soome #if 0
4155cabbc6bSPrashanth Sreenivasa 	/*
4165cabbc6bSPrashanth Sreenivasa 	 * The $ORIGIN dataset (if it exists) doesn't have an associated
4175cabbc6bSPrashanth Sreenivasa 	 * objset, so there's no reason to open it. The $ORIGIN dataset
4185cabbc6bSPrashanth Sreenivasa 	 * will not exist on pools older than SPA_VERSION_ORIGIN.
4195cabbc6bSPrashanth Sreenivasa 	 */
4205cabbc6bSPrashanth Sreenivasa 	if (ds != NULL && spa_get_dsl(spa) != NULL &&
4215cabbc6bSPrashanth Sreenivasa 	    spa_get_dsl(spa)->dp_origin_snap != NULL) {
4225cabbc6bSPrashanth Sreenivasa 		ASSERT3P(ds->ds_dir, !=,
4235cabbc6bSPrashanth Sreenivasa 		    spa_get_dsl(spa)->dp_origin_snap->ds_dir);
4245cabbc6bSPrashanth Sreenivasa 	}
425a3874b8bSToomas Soome #endif
4265cabbc6bSPrashanth Sreenivasa 
427503ad85cSMatthew Ahrens 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
428503ad85cSMatthew Ahrens 	os->os_dsl_dataset = ds;
429503ad85cSMatthew Ahrens 	os->os_spa = spa;
430503ad85cSMatthew Ahrens 	os->os_rootbp = bp;
431503ad85cSMatthew Ahrens 	if (!BP_IS_HOLE(os->os_rootbp)) {
4327adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
4337802d7bfSMatthew Ahrens 		zbookmark_phys_t zb;
434f67950b2SNasf-Fan 		int size;
435eb633035STom Caputi 		enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
436b24ab676SJeff Bonwick 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
437b24ab676SJeff Bonwick 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
438b24ab676SJeff Bonwick 
439503ad85cSMatthew Ahrens 		if (DMU_OS_IS_L2CACHEABLE(os))
4407adb730bSGeorge Wilson 			aflags |= ARC_FLAG_L2CACHE;
441ea8dc4b6Seschrock 
442eb633035STom Caputi 		if (ds != NULL && ds->ds_dir->dd_crypto_obj != 0) {
443eb633035STom Caputi 			ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
444eb633035STom Caputi 			ASSERT(BP_IS_AUTHENTICATED(bp));
445eb633035STom Caputi 			zio_flags |= ZIO_FLAG_RAW;
446eb633035STom Caputi 		}
447eb633035STom Caputi 
448503ad85cSMatthew Ahrens 		dprintf_bp(os->os_rootbp, "reading %s", "");
4491b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, os->os_rootbp,
450503ad85cSMatthew Ahrens 		    arc_getbuf_func, &os->os_phys_buf,
451eb633035STom Caputi 		    ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
4523b2aab18SMatthew Ahrens 		if (err != 0) {
453503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
454b87f3af3Sperrin 			/* convert checksum errors into IO errors */
455b87f3af3Sperrin 			if (err == ECKSUM)
456be6fd75aSMatthew Ahrens 				err = SET_ERROR(EIO);
457ea8dc4b6Seschrock 			return (err);
458ea8dc4b6Seschrock 		}
45914843421SMatthew Ahrens 
460f67950b2SNasf-Fan 		if (spa_version(spa) < SPA_VERSION_USERSPACE)
461f67950b2SNasf-Fan 			size = OBJSET_PHYS_SIZE_V1;
462f67950b2SNasf-Fan 		else if (!spa_feature_is_enabled(spa,
463f67950b2SNasf-Fan 		    SPA_FEATURE_PROJECT_QUOTA))
464f67950b2SNasf-Fan 			size = OBJSET_PHYS_SIZE_V2;
465f67950b2SNasf-Fan 		else
466f67950b2SNasf-Fan 			size = sizeof (objset_phys_t);
467f67950b2SNasf-Fan 
46814843421SMatthew Ahrens 		/* Increase the blocksize if we are permitted. */
469f67950b2SNasf-Fan 		if (arc_buf_size(os->os_phys_buf) < size) {
4705602294fSDan Kimmel 			arc_buf_t *buf = arc_alloc_buf(spa, &os->os_phys_buf,
471f67950b2SNasf-Fan 			    ARC_BUFC_METADATA, size);
472f67950b2SNasf-Fan 			bzero(buf->b_data, size);
473503ad85cSMatthew Ahrens 			bcopy(os->os_phys_buf->b_data, buf->b_data,
474503ad85cSMatthew Ahrens 			    arc_buf_size(os->os_phys_buf));
475dcbf3bd6SGeorge Wilson 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
476503ad85cSMatthew Ahrens 			os->os_phys_buf = buf;
47714843421SMatthew Ahrens 		}
47814843421SMatthew Ahrens 
479503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
480503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
481fa9e4066Sahrens 	} else {
48214843421SMatthew Ahrens 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
483f67950b2SNasf-Fan 		    sizeof (objset_phys_t) : OBJSET_PHYS_SIZE_V1;
4845602294fSDan Kimmel 		os->os_phys_buf = arc_alloc_buf(spa, &os->os_phys_buf,
4855602294fSDan Kimmel 		    ARC_BUFC_METADATA, size);
486503ad85cSMatthew Ahrens 		os->os_phys = os->os_phys_buf->b_data;
487503ad85cSMatthew Ahrens 		bzero(os->os_phys, size);
488fa9e4066Sahrens 	}
489fa9e4066Sahrens 
490fa9e4066Sahrens 	/*
491fa9e4066Sahrens 	 * Note: the changed_cb will be called once before the register
492fa9e4066Sahrens 	 * func returns, thus changing the checksum/compression from the
4933baa08fcSek 	 * default (fletcher2/off).  Snapshots don't need to know about
4943baa08fcSek 	 * checksum/compression/copies.
495fa9e4066Sahrens 	 */
4965d7b4d43SMatthew Ahrens 	if (ds != NULL) {
4979c3fd121SMatthew Ahrens 		boolean_t needlock = B_FALSE;
4989c3fd121SMatthew Ahrens 
499eb633035STom Caputi 		os->os_encrypted = (ds->ds_dir->dd_crypto_obj != 0);
500eb633035STom Caputi 
5019c3fd121SMatthew Ahrens 		/*
5029c3fd121SMatthew Ahrens 		 * Note: it's valid to open the objset if the dataset is
5039c3fd121SMatthew Ahrens 		 * long-held, in which case the pool_config lock will not
5049c3fd121SMatthew Ahrens 		 * be held.
5059c3fd121SMatthew Ahrens 		 */
5069c3fd121SMatthew Ahrens 		if (!dsl_pool_config_held(dmu_objset_pool(os))) {
5079c3fd121SMatthew Ahrens 			needlock = B_TRUE;
5089c3fd121SMatthew Ahrens 			dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5099c3fd121SMatthew Ahrens 		}
510eb633035STom Caputi 
5113b2aab18SMatthew Ahrens 		err = dsl_prop_register(ds,
5123b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
513503ad85cSMatthew Ahrens 		    primary_cache_changed_cb, os);
5143b2aab18SMatthew Ahrens 		if (err == 0) {
5153b2aab18SMatthew Ahrens 			err = dsl_prop_register(ds,
5163b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
517503ad85cSMatthew Ahrens 			    secondary_cache_changed_cb, os);
5183b2aab18SMatthew Ahrens 		}
519bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
5203b2aab18SMatthew Ahrens 			if (err == 0) {
5213b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
5223b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
523503ad85cSMatthew Ahrens 				    checksum_changed_cb, os);
5243b2aab18SMatthew Ahrens 			}
5253b2aab18SMatthew Ahrens 			if (err == 0) {
5263b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
5273b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
528503ad85cSMatthew Ahrens 				    compression_changed_cb, os);
5293b2aab18SMatthew Ahrens 			}
5303b2aab18SMatthew Ahrens 			if (err == 0) {
5313b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
5323b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_COPIES),
533503ad85cSMatthew Ahrens 				    copies_changed_cb, os);
5343b2aab18SMatthew Ahrens 			}
5353b2aab18SMatthew Ahrens 			if (err == 0) {
5363b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
5373b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_DEDUP),
538b24ab676SJeff Bonwick 				    dedup_changed_cb, os);
5393b2aab18SMatthew Ahrens 			}
5403b2aab18SMatthew Ahrens 			if (err == 0) {
5413b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
5423b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
543e09fa4daSNeil Perrin 				    logbias_changed_cb, os);
5443b2aab18SMatthew Ahrens 			}
5453b2aab18SMatthew Ahrens 			if (err == 0) {
5463b2aab18SMatthew Ahrens 				err = dsl_prop_register(ds,
5473b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_SYNC),
54855da60b9SMark J Musante 				    sync_changed_cb, os);
5493b2aab18SMatthew Ahrens 			}
550edf345e6SMatthew Ahrens 			if (err == 0) {
551edf345e6SMatthew Ahrens 				err = dsl_prop_register(ds,
552edf345e6SMatthew Ahrens 				    zfs_prop_to_name(
553edf345e6SMatthew Ahrens 				    ZFS_PROP_REDUNDANT_METADATA),
554edf345e6SMatthew Ahrens 				    redundant_metadata_changed_cb, os);
555edf345e6SMatthew Ahrens 			}
556b5152584SMatthew Ahrens 			if (err == 0) {
557b5152584SMatthew Ahrens 				err = dsl_prop_register(ds,
558b5152584SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
559b5152584SMatthew Ahrens 				    recordsize_changed_cb, os);
560b5152584SMatthew Ahrens 			}
56154811da5SToomas Soome 			if (err == 0) {
56254811da5SToomas Soome 				err = dsl_prop_register(ds,
56354811da5SToomas Soome 				    zfs_prop_to_name(ZFS_PROP_DNODESIZE),
56454811da5SToomas Soome 				    dnodesize_changed_cb, os);
56554811da5SToomas Soome 			}
566663207adSDon Brady 			if (err == 0) {
567663207adSDon Brady 				err = dsl_prop_register(ds,
568663207adSDon Brady 				    zfs_prop_to_name(
569663207adSDon Brady 				    ZFS_PROP_SPECIAL_SMALL_BLOCKS),
570663207adSDon Brady 				    smallblk_changed_cb, os);
571663207adSDon Brady 			}
5723baa08fcSek 		}
5739c3fd121SMatthew Ahrens 		if (needlock)
5749c3fd121SMatthew Ahrens 			dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5753b2aab18SMatthew Ahrens 		if (err != 0) {
576dcbf3bd6SGeorge Wilson 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
577503ad85cSMatthew Ahrens 			kmem_free(os, sizeof (objset_t));
578ea8dc4b6Seschrock 			return (err);
579ea8dc4b6Seschrock 		}
5805d7b4d43SMatthew Ahrens 	} else {
581fa9e4066Sahrens 		/* It's the meta-objset. */
582503ad85cSMatthew Ahrens 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
583db1741f5SJustin T. Gibbs 		os->os_compress = ZIO_COMPRESS_ON;
584eb633035STom Caputi 		os->os_encrypted = B_FALSE;
585503ad85cSMatthew Ahrens 		os->os_copies = spa_max_replication(spa);
586b24ab676SJeff Bonwick 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
587edf345e6SMatthew Ahrens 		os->os_dedup_verify = B_FALSE;
588edf345e6SMatthew Ahrens 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
589edf345e6SMatthew Ahrens 		os->os_sync = ZFS_SYNC_STANDARD;
590503ad85cSMatthew Ahrens 		os->os_primary_cache = ZFS_CACHE_ALL;
591503ad85cSMatthew Ahrens 		os->os_secondary_cache = ZFS_CACHE_ALL;
59254811da5SToomas Soome 		os->os_dnodesize = DNODE_MIN_SIZE;
593fa9e4066Sahrens 	}
594adb52d92SMatthew Ahrens 	/*
595adb52d92SMatthew Ahrens 	 * These properties will be filled in by the logic in zfs_get_zplprop()
596adb52d92SMatthew Ahrens 	 * when they are queried for the first time.
597adb52d92SMatthew Ahrens 	 */
598adb52d92SMatthew Ahrens 	os->os_version = OBJSET_PROP_UNINITIALIZED;
599adb52d92SMatthew Ahrens 	os->os_normalization = OBJSET_PROP_UNINITIALIZED;
600adb52d92SMatthew Ahrens 	os->os_utf8only = OBJSET_PROP_UNINITIALIZED;
601adb52d92SMatthew Ahrens 	os->os_casesensitivity = OBJSET_PROP_UNINITIALIZED;
602fa9e4066Sahrens 
603bc9014e6SJustin Gibbs 	if (ds == NULL || !ds->ds_is_snapshot)
6046e0cbcaaSMatthew Ahrens 		os->os_zil_header = os->os_phys->os_zil_header;
605503ad85cSMatthew Ahrens 	os->os_zil = zil_alloc(os, &os->os_zil_header);
606fa9e4066Sahrens 
607fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
60894c2d0ebSMatthew Ahrens 		os->os_dirty_dnodes[i] = multilist_create(sizeof (dnode_t),
60994c2d0ebSMatthew Ahrens 		    offsetof(dnode_t, dn_dirty_link[i]),
61094c2d0ebSMatthew Ahrens 		    dnode_multilist_index_func);
611fa9e4066Sahrens 	}
612503ad85cSMatthew Ahrens 	list_create(&os->os_dnodes, sizeof (dnode_t),
613fa9e4066Sahrens 	    offsetof(dnode_t, dn_link));
614503ad85cSMatthew Ahrens 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
615fa9e4066Sahrens 	    offsetof(dmu_buf_impl_t, db_link));
616fa9e4066Sahrens 
617503ad85cSMatthew Ahrens 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
61894c2d0ebSMatthew Ahrens 	mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL);
619503ad85cSMatthew Ahrens 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
620503ad85cSMatthew Ahrens 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
62154811da5SToomas Soome 	os->os_obj_next_percpu_len = boot_ncpus;
62254811da5SToomas Soome 	os->os_obj_next_percpu = kmem_zalloc(os->os_obj_next_percpu_len *
62354811da5SToomas Soome 	    sizeof (os->os_obj_next_percpu[0]), KM_SLEEP);
624503ad85cSMatthew Ahrens 
625bc9014e6SJustin Gibbs 	dnode_special_open(os, &os->os_phys->os_meta_dnode,
626bc9014e6SJustin Gibbs 	    DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
627f67950b2SNasf-Fan 	if (OBJSET_BUF_HAS_USERUSED(os->os_phys_buf)) {
628bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_userused_dnode,
629bc9014e6SJustin Gibbs 		    DMU_USERUSED_OBJECT, &os->os_userused_dnode);
630bc9014e6SJustin Gibbs 		dnode_special_open(os, &os->os_phys->os_groupused_dnode,
631bc9014e6SJustin Gibbs 		    DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
632f67950b2SNasf-Fan 		if (OBJSET_BUF_HAS_PROJECTUSED(os->os_phys_buf))
633f67950b2SNasf-Fan 			dnode_special_open(os,
634f67950b2SNasf-Fan 			    &os->os_phys->os_projectused_dnode,
635f67950b2SNasf-Fan 			    DMU_PROJECTUSED_OBJECT, &os->os_projectused_dnode);
63614843421SMatthew Ahrens 	}
637fa9e4066Sahrens 
638f67950b2SNasf-Fan 	mutex_init(&os->os_upgrade_lock, NULL, MUTEX_DEFAULT, NULL);
639f67950b2SNasf-Fan 
640503ad85cSMatthew Ahrens 	*osp = os;
641ea8dc4b6Seschrock 	return (0);
642fa9e4066Sahrens }
643fa9e4066Sahrens 
644503ad85cSMatthew Ahrens int
dmu_objset_from_ds(dsl_dataset_t * ds,objset_t ** osp)645503ad85cSMatthew Ahrens dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
6463cb34c60Sahrens {
647503ad85cSMatthew Ahrens 	int err = 0;
6483cb34c60Sahrens 
6499c3fd121SMatthew Ahrens 	/*
6509c3fd121SMatthew Ahrens 	 * We shouldn't be doing anything with dsl_dataset_t's unless the
6519c3fd121SMatthew Ahrens 	 * pool_config lock is held, or the dataset is long-held.
6529c3fd121SMatthew Ahrens 	 */
6539c3fd121SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
6549c3fd121SMatthew Ahrens 	    dsl_dataset_long_held(ds));
6559c3fd121SMatthew Ahrens 
6563cb34c60Sahrens 	mutex_enter(&ds->ds_opening_lock);
6575d7b4d43SMatthew Ahrens 	if (ds->ds_objset == NULL) {
6585d7b4d43SMatthew Ahrens 		objset_t *os;
659c166b69dSPaul Dagnelie 		rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
6603cb34c60Sahrens 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
6615d7b4d43SMatthew Ahrens 		    ds, dsl_dataset_get_blkptr(ds), &os);
662c166b69dSPaul Dagnelie 		rrw_exit(&ds->ds_bp_rwlock, FTAG);
6635d7b4d43SMatthew Ahrens 
6645d7b4d43SMatthew Ahrens 		if (err == 0) {
6655d7b4d43SMatthew Ahrens 			mutex_enter(&ds->ds_lock);
6665d7b4d43SMatthew Ahrens 			ASSERT(ds->ds_objset == NULL);
6675d7b4d43SMatthew Ahrens 			ds->ds_objset = os;
6685d7b4d43SMatthew Ahrens 			mutex_exit(&ds->ds_lock);
6695d7b4d43SMatthew Ahrens 		}
6703cb34c60Sahrens 	}
6715d7b4d43SMatthew Ahrens 	*osp = ds->ds_objset;
6723cb34c60Sahrens 	mutex_exit(&ds->ds_opening_lock);
673503ad85cSMatthew Ahrens 	return (err);
6743cb34c60Sahrens }
6753cb34c60Sahrens 
6763b2aab18SMatthew Ahrens /*
6773b2aab18SMatthew Ahrens  * Holds the pool while the objset is held.  Therefore only one objset
6783b2aab18SMatthew Ahrens  * can be held at a time.
6793b2aab18SMatthew Ahrens  */
6803cb34c60Sahrens int
dmu_objset_hold_flags(const char * name,boolean_t decrypt,void * tag,objset_t ** osp)681eb633035STom Caputi dmu_objset_hold_flags(const char *name, boolean_t decrypt, void *tag,
682eb633035STom Caputi     objset_t **osp)
6833cb34c60Sahrens {
6843b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
685503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
6863cb34c60Sahrens 	int err;
687*ef96fc31SToomas Soome 	ds_hold_flags_t flags;
6883cb34c60Sahrens 
689*ef96fc31SToomas Soome 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
6903b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, tag, &dp);
6913b2aab18SMatthew Ahrens 	if (err != 0)
6923b2aab18SMatthew Ahrens 		return (err);
693eb633035STom Caputi 	err = dsl_dataset_hold_flags(dp, name, flags, tag, &ds);
6943b2aab18SMatthew Ahrens 	if (err != 0) {
6953b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
696503ad85cSMatthew Ahrens 		return (err);
6973b2aab18SMatthew Ahrens 	}
698503ad85cSMatthew Ahrens 
699503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, osp);
7003b2aab18SMatthew Ahrens 	if (err != 0) {
701503ad85cSMatthew Ahrens 		dsl_dataset_rele(ds, tag);
7023b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
7033b2aab18SMatthew Ahrens 	}
704503ad85cSMatthew Ahrens 
7053cb34c60Sahrens 	return (err);
7063cb34c60Sahrens }
7073cb34c60Sahrens 
708eb633035STom Caputi int
dmu_objset_hold(const char * name,void * tag,objset_t ** osp)709eb633035STom Caputi dmu_objset_hold(const char *name, void *tag, objset_t **osp)
710eb633035STom Caputi {
711eb633035STom Caputi 	return (dmu_objset_hold_flags(name, B_FALSE, tag, osp));
712eb633035STom Caputi }
713eb633035STom Caputi 
714eb633035STom Caputi /* ARGSUSED */
71512380e1eSArne Jansen static int
dmu_objset_own_impl(dsl_dataset_t * ds,dmu_objset_type_t type,boolean_t readonly,boolean_t decrypt,void * tag,objset_t ** osp)71612380e1eSArne Jansen dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
717eb633035STom Caputi     boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
71812380e1eSArne Jansen {
71912380e1eSArne Jansen 	int err;
72012380e1eSArne Jansen 
72112380e1eSArne Jansen 	err = dmu_objset_from_ds(ds, osp);
72212380e1eSArne Jansen 	if (err != 0) {
723eb633035STom Caputi 		return (err);
72412380e1eSArne Jansen 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
72512380e1eSArne Jansen 		return (SET_ERROR(EINVAL));
72612380e1eSArne Jansen 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
727eb633035STom Caputi 		return (SET_ERROR(EROFS));
728eb633035STom Caputi 	} else if (!readonly && decrypt &&
729eb633035STom Caputi 	    dsl_dir_incompatible_encryption_version(ds->ds_dir)) {
73012380e1eSArne Jansen 		return (SET_ERROR(EROFS));
73112380e1eSArne Jansen 	}
732eb633035STom Caputi 
733eb633035STom Caputi 	/* if we are decrypting, we can now check MACs in os->os_phys_buf */
734eb633035STom Caputi 	if (decrypt && arc_is_unauthenticated((*osp)->os_phys_buf)) {
735eb633035STom Caputi 		zbookmark_phys_t zb;
736eb633035STom Caputi 
737eb633035STom Caputi 		SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
738eb633035STom Caputi 		    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
739eb633035STom Caputi 		err = arc_untransform((*osp)->os_phys_buf, (*osp)->os_spa,
740eb633035STom Caputi 		    &zb, B_FALSE);
741eb633035STom Caputi 		if (err != 0)
742eb633035STom Caputi 			return (err);
743eb633035STom Caputi 
744eb633035STom Caputi 		ASSERT0(arc_is_unauthenticated((*osp)->os_phys_buf));
745eb633035STom Caputi 	}
746eb633035STom Caputi 
747eb633035STom Caputi 	return (0);
74812380e1eSArne Jansen }
74912380e1eSArne Jansen 
7503b2aab18SMatthew Ahrens /*
7513b2aab18SMatthew Ahrens  * dsl_pool must not be held when this is called.
7523b2aab18SMatthew Ahrens  * Upon successful return, there will be a longhold on the dataset,
7533b2aab18SMatthew Ahrens  * and the dsl_pool will not be held.
7543b2aab18SMatthew Ahrens  */
755fa9e4066Sahrens int
dmu_objset_own(const char * name,dmu_objset_type_t type,boolean_t readonly,boolean_t decrypt,void * tag,objset_t ** osp)756503ad85cSMatthew Ahrens dmu_objset_own(const char *name, dmu_objset_type_t type,
757eb633035STom Caputi     boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
758fa9e4066Sahrens {
7593b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
760f18faf3fSek 	dsl_dataset_t *ds;
761f18faf3fSek 	int err;
762*ef96fc31SToomas Soome 	ds_hold_flags_t flags;
763fa9e4066Sahrens 
764*ef96fc31SToomas Soome 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
7653b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
7663b2aab18SMatthew Ahrens 	if (err != 0)
7673b2aab18SMatthew Ahrens 		return (err);
768eb633035STom Caputi 	err = dsl_dataset_own(dp, name, flags, tag, &ds);
7693b2aab18SMatthew Ahrens 	if (err != 0) {
7703b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
771fa9e4066Sahrens 		return (err);
7723b2aab18SMatthew Ahrens 	}
773eb633035STom Caputi 	err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp);
774eb633035STom Caputi 	if (err != 0) {
775eb633035STom Caputi 		dsl_dataset_disown(ds, flags, tag);
776eb633035STom Caputi 		dsl_pool_rele(dp, FTAG);
777eb633035STom Caputi 		return (err);
778eb633035STom Caputi 	}
779eb633035STom Caputi 
780f67950b2SNasf-Fan 	/*
781f67950b2SNasf-Fan 	 * User accounting requires the dataset to be decrypted and rw.
782f67950b2SNasf-Fan 	 * We also don't begin user accounting during claiming to help
783f67950b2SNasf-Fan 	 * speed up pool import times and to keep this txg reserved
784f67950b2SNasf-Fan 	 * completely for recovery work.
785f67950b2SNasf-Fan 	 */
786f67950b2SNasf-Fan 	if ((dmu_objset_userobjspace_upgradable(*osp) ||
787f67950b2SNasf-Fan 	    dmu_objset_projectquota_upgradable(*osp)) &&
788f67950b2SNasf-Fan 	    !readonly && !dp->dp_spa->spa_claiming &&
789f67950b2SNasf-Fan 	    (ds->ds_dir->dd_crypto_obj == 0 || decrypt))
790f67950b2SNasf-Fan 		dmu_objset_id_quota_upgrade(*osp);
79112380e1eSArne Jansen 
792f67950b2SNasf-Fan 	dsl_pool_rele(dp, FTAG);
793eb633035STom Caputi 	return (0);
794fa9e4066Sahrens }
795fa9e4066Sahrens 
79612380e1eSArne Jansen int
dmu_objset_own_obj(dsl_pool_t * dp,uint64_t obj,dmu_objset_type_t type,boolean_t readonly,boolean_t decrypt,void * tag,objset_t ** osp)79712380e1eSArne Jansen dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
798eb633035STom Caputi     boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
79912380e1eSArne Jansen {
80012380e1eSArne Jansen 	dsl_dataset_t *ds;
80112380e1eSArne Jansen 	int err;
802*ef96fc31SToomas Soome 	ds_hold_flags_t flags;
80312380e1eSArne Jansen 
804*ef96fc31SToomas Soome 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
805eb633035STom Caputi 	err = dsl_dataset_own_obj(dp, obj, flags, tag, &ds);
80612380e1eSArne Jansen 	if (err != 0)
80712380e1eSArne Jansen 		return (err);
80812380e1eSArne Jansen 
809eb633035STom Caputi 	err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp);
810eb633035STom Caputi 	if (err != 0) {
811eb633035STom Caputi 		dsl_dataset_disown(ds, flags, tag);
812eb633035STom Caputi 		return (err);
813eb633035STom Caputi 	}
814eb633035STom Caputi 
815eb633035STom Caputi 	return (0);
81612380e1eSArne Jansen }
81712380e1eSArne Jansen 
818fa9e4066Sahrens void
dmu_objset_rele_flags(objset_t * os,boolean_t decrypt,void * tag)819eb633035STom Caputi dmu_objset_rele_flags(objset_t *os, boolean_t decrypt, void *tag)
820fa9e4066Sahrens {
821*ef96fc31SToomas Soome 	ds_hold_flags_t flags;
8223b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_objset_pool(os);
823*ef96fc31SToomas Soome 
824*ef96fc31SToomas Soome 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
825eb633035STom Caputi 	dsl_dataset_rele_flags(os->os_dsl_dataset, flags, tag);
8263b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, tag);
827503ad85cSMatthew Ahrens }
828503ad85cSMatthew Ahrens 
829eb633035STom Caputi void
dmu_objset_rele(objset_t * os,void * tag)830eb633035STom Caputi dmu_objset_rele(objset_t *os, void *tag)
831eb633035STom Caputi {
832eb633035STom Caputi 	dmu_objset_rele_flags(os, B_FALSE, tag);
833eb633035STom Caputi }
834eb633035STom Caputi 
83591948b51SKeith M Wesolowski /*
83691948b51SKeith M Wesolowski  * When we are called, os MUST refer to an objset associated with a dataset
83791948b51SKeith M Wesolowski  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
83891948b51SKeith M Wesolowski  * == tag.  We will then release and reacquire ownership of the dataset while
83991948b51SKeith M Wesolowski  * holding the pool config_rwlock to avoid intervening namespace or ownership
84091948b51SKeith M Wesolowski  * changes may occur.
84191948b51SKeith M Wesolowski  *
84291948b51SKeith M Wesolowski  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
84391948b51SKeith M Wesolowski  * release the hold on its dataset and acquire a new one on the dataset of the
84491948b51SKeith M Wesolowski  * same name so that it can be partially torn down and reconstructed.
84591948b51SKeith M Wesolowski  */
84691948b51SKeith M Wesolowski void
dmu_objset_refresh_ownership(dsl_dataset_t * ds,dsl_dataset_t ** newds,boolean_t decrypt,void * tag)8475f5913bbSAndriy Gapon dmu_objset_refresh_ownership(dsl_dataset_t *ds, dsl_dataset_t **newds,
848eb633035STom Caputi     boolean_t decrypt, void *tag)
84991948b51SKeith M Wesolowski {
85091948b51SKeith M Wesolowski 	dsl_pool_t *dp;
8519adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
852*ef96fc31SToomas Soome 	ds_hold_flags_t flags;
85391948b51SKeith M Wesolowski 
854*ef96fc31SToomas Soome 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
85591948b51SKeith M Wesolowski 	VERIFY3P(ds, !=, NULL);
85691948b51SKeith M Wesolowski 	VERIFY3P(ds->ds_owner, ==, tag);
85791948b51SKeith M Wesolowski 	VERIFY(dsl_dataset_long_held(ds));
85891948b51SKeith M Wesolowski 
85991948b51SKeith M Wesolowski 	dsl_dataset_name(ds, name);
8605f5913bbSAndriy Gapon 	dp = ds->ds_dir->dd_pool;
86191948b51SKeith M Wesolowski 	dsl_pool_config_enter(dp, FTAG);
862eb633035STom Caputi 
863*ef96fc31SToomas Soome 	dsl_dataset_disown(ds, flags, tag);
864*ef96fc31SToomas Soome 	VERIFY0(dsl_dataset_own(dp, name, flags, tag, newds));
86591948b51SKeith M Wesolowski 	dsl_pool_config_exit(dp, FTAG);
86691948b51SKeith M Wesolowski }
86791948b51SKeith M Wesolowski 
868503ad85cSMatthew Ahrens void
dmu_objset_disown(objset_t * os,boolean_t decrypt,void * tag)869eb633035STom Caputi dmu_objset_disown(objset_t *os, boolean_t decrypt, void *tag)
870503ad85cSMatthew Ahrens {
871*ef96fc31SToomas Soome 	ds_hold_flags_t flags;
872*ef96fc31SToomas Soome 
873*ef96fc31SToomas Soome 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
874f67950b2SNasf-Fan 	/*
875f67950b2SNasf-Fan 	 * Stop upgrading thread
876f67950b2SNasf-Fan 	 */
877f67950b2SNasf-Fan 	dmu_objset_upgrade_stop(os);
878*ef96fc31SToomas Soome 	dsl_dataset_disown(os->os_dsl_dataset, flags, tag);
879fa9e4066Sahrens }
880fa9e4066Sahrens 
8813b2aab18SMatthew Ahrens void
dmu_objset_evict_dbufs(objset_t * os)8821934e92fSmaybee dmu_objset_evict_dbufs(objset_t *os)
883ea8dc4b6Seschrock {
884bc9014e6SJustin Gibbs 	dnode_t dn_marker;
885ea8dc4b6Seschrock 	dnode_t *dn;
886c543ec06Sahrens 
887503ad85cSMatthew Ahrens 	mutex_enter(&os->os_lock);
888bc9014e6SJustin Gibbs 	dn = list_head(&os->os_dnodes);
889bc9014e6SJustin Gibbs 	while (dn != NULL) {
890bc9014e6SJustin Gibbs 		/*
891bc9014e6SJustin Gibbs 		 * Skip dnodes without holds.  We have to do this dance
892bc9014e6SJustin Gibbs 		 * because dnode_add_ref() only works if there is already a
893bc9014e6SJustin Gibbs 		 * hold.  If the dnode has no holds, then it has no dbufs.
894bc9014e6SJustin Gibbs 		 */
895bc9014e6SJustin Gibbs 		if (dnode_add_ref(dn, FTAG)) {
896bc9014e6SJustin Gibbs 			list_insert_after(&os->os_dnodes, dn, &dn_marker);
897bc9014e6SJustin Gibbs 			mutex_exit(&os->os_lock);
898c543ec06Sahrens 
899bc9014e6SJustin Gibbs 			dnode_evict_dbufs(dn);
900bc9014e6SJustin Gibbs 			dnode_rele(dn, FTAG);
901c543ec06Sahrens 
902bc9014e6SJustin Gibbs 			mutex_enter(&os->os_lock);
903bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, &dn_marker);
904bc9014e6SJustin Gibbs 			list_remove(&os->os_dnodes, &dn_marker);
905bc9014e6SJustin Gibbs 		} else {
906bc9014e6SJustin Gibbs 			dn = list_next(&os->os_dnodes, dn);
907bc9014e6SJustin Gibbs 		}
908ea8dc4b6Seschrock 	}
909503ad85cSMatthew Ahrens 	mutex_exit(&os->os_lock);
910bc9014e6SJustin Gibbs 
911bc9014e6SJustin Gibbs 	if (DMU_USERUSED_DNODE(os) != NULL) {
912f67950b2SNasf-Fan 		if (DMU_PROJECTUSED_DNODE(os) != NULL)
913f67950b2SNasf-Fan 			dnode_evict_dbufs(DMU_PROJECTUSED_DNODE(os));
914bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
915bc9014e6SJustin Gibbs 		dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
916bc9014e6SJustin Gibbs 	}
917bc9014e6SJustin Gibbs 	dnode_evict_dbufs(DMU_META_DNODE(os));
918ea8dc4b6Seschrock }
919ea8dc4b6Seschrock 
920bc9014e6SJustin Gibbs /*
921bc9014e6SJustin Gibbs  * Objset eviction processing is split into into two pieces.
922bc9014e6SJustin Gibbs  * The first marks the objset as evicting, evicts any dbufs that
923bc9014e6SJustin Gibbs  * have a refcount of zero, and then queues up the objset for the
924bc9014e6SJustin Gibbs  * second phase of eviction.  Once os->os_dnodes has been cleared by
925bc9014e6SJustin Gibbs  * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
926bc9014e6SJustin Gibbs  * The second phase closes the special dnodes, dequeues the objset from
927bc9014e6SJustin Gibbs  * the list of those undergoing eviction, and finally frees the objset.
928bc9014e6SJustin Gibbs  *
929bc9014e6SJustin Gibbs  * NOTE: Due to asynchronous eviction processing (invocation of
930bc9014e6SJustin Gibbs  *       dnode_buf_pageout()), it is possible for the meta dnode for the
931bc9014e6SJustin Gibbs  *       objset to have no holds even though os->os_dnodes is not empty.
932bc9014e6SJustin Gibbs  */
933fa9e4066Sahrens void
dmu_objset_evict(objset_t * os)934503ad85cSMatthew Ahrens dmu_objset_evict(objset_t *os)
935fa9e4066Sahrens {
936503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
937fa9e4066Sahrens 
938b24ab676SJeff Bonwick 	for (int t = 0; t < TXG_SIZE; t++)
939b24ab676SJeff Bonwick 		ASSERT(!dmu_objset_is_dirty(os, t));
940fa9e4066Sahrens 
94103bad06fSJustin Gibbs 	if (ds)
94203bad06fSJustin Gibbs 		dsl_prop_unregister_all(ds, os);
943fa9e4066Sahrens 
9440a586ceaSMark Shellenbaum 	if (os->os_sa)
9450a586ceaSMark Shellenbaum 		sa_tear_down(os);
9460a586ceaSMark Shellenbaum 
9473b2aab18SMatthew Ahrens 	dmu_objset_evict_dbufs(os);
948ea8dc4b6Seschrock 
949bc9014e6SJustin Gibbs 	mutex_enter(&os->os_lock);
950bc9014e6SJustin Gibbs 	spa_evicting_os_register(os->os_spa, os);
951bc9014e6SJustin Gibbs 	if (list_is_empty(&os->os_dnodes)) {
952bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
953bc9014e6SJustin Gibbs 		dmu_objset_evict_done(os);
954bc9014e6SJustin Gibbs 	} else {
955bc9014e6SJustin Gibbs 		mutex_exit(&os->os_lock);
956bc9014e6SJustin Gibbs 	}
957eb633035STom Caputi 
958eb633035STom Caputi 
959bc9014e6SJustin Gibbs }
960bc9014e6SJustin Gibbs 
961bc9014e6SJustin Gibbs void
dmu_objset_evict_done(objset_t * os)962bc9014e6SJustin Gibbs dmu_objset_evict_done(objset_t *os)
963bc9014e6SJustin Gibbs {
964bc9014e6SJustin Gibbs 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
965bc9014e6SJustin Gibbs 
966744947dcSTom Erickson 	dnode_special_close(&os->os_meta_dnode);
967744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os)) {
968f67950b2SNasf-Fan 		if (DMU_PROJECTUSED_DNODE(os))
969f67950b2SNasf-Fan 			dnode_special_close(&os->os_projectused_dnode);
970744947dcSTom Erickson 		dnode_special_close(&os->os_userused_dnode);
971744947dcSTom Erickson 		dnode_special_close(&os->os_groupused_dnode);
97214843421SMatthew Ahrens 	}
973503ad85cSMatthew Ahrens 	zil_free(os->os_zil);
974fa9e4066Sahrens 
975dcbf3bd6SGeorge Wilson 	arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
976744947dcSTom Erickson 
977744947dcSTom Erickson 	/*
978744947dcSTom Erickson 	 * This is a barrier to prevent the objset from going away in
979744947dcSTom Erickson 	 * dnode_move() until we can safely ensure that the objset is still in
980744947dcSTom Erickson 	 * use. We consider the objset valid before the barrier and invalid
981744947dcSTom Erickson 	 * after the barrier.
982744947dcSTom Erickson 	 */
983744947dcSTom Erickson 	rw_enter(&os_lock, RW_READER);
984744947dcSTom Erickson 	rw_exit(&os_lock);
985744947dcSTom Erickson 
98654811da5SToomas Soome 	kmem_free(os->os_obj_next_percpu,
98754811da5SToomas Soome 	    os->os_obj_next_percpu_len * sizeof (os->os_obj_next_percpu[0]));
98854811da5SToomas Soome 
989503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_lock);
99094c2d0ebSMatthew Ahrens 	mutex_destroy(&os->os_userused_lock);
991503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_obj_lock);
992503ad85cSMatthew Ahrens 	mutex_destroy(&os->os_user_ptr_lock);
993cc37296fSAndy Fiddaman 	mutex_destroy(&os->os_upgrade_lock);
99494c2d0ebSMatthew Ahrens 	for (int i = 0; i < TXG_SIZE; i++) {
99594c2d0ebSMatthew Ahrens 		multilist_destroy(os->os_dirty_dnodes[i]);
99694c2d0ebSMatthew Ahrens 	}
997bc9014e6SJustin Gibbs 	spa_evicting_os_deregister(os->os_spa, os);
998503ad85cSMatthew Ahrens 	kmem_free(os, sizeof (objset_t));
999fa9e4066Sahrens }
1000fa9e4066Sahrens 
100171eb0538SChris Kirby timestruc_t
dmu_objset_snap_cmtime(objset_t * os)100271eb0538SChris Kirby dmu_objset_snap_cmtime(objset_t *os)
100371eb0538SChris Kirby {
100471eb0538SChris Kirby 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
100571eb0538SChris Kirby }
100671eb0538SChris Kirby 
1007eb633035STom Caputi /* ARGSUSED */
1008503ad85cSMatthew Ahrens objset_t *
dmu_objset_create_impl_dnstats(spa_t * spa,dsl_dataset_t * ds,blkptr_t * bp,dmu_objset_type_t type,int levels,int blksz,int ibs,dmu_tx_t * tx)1009eb633035STom Caputi dmu_objset_create_impl_dnstats(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
1010eb633035STom Caputi     dmu_objset_type_t type, int levels, int blksz, int ibs, dmu_tx_t *tx)
1011fa9e4066Sahrens {
1012503ad85cSMatthew Ahrens 	objset_t *os;
1013fa9e4066Sahrens 	dnode_t *mdn;
1014fa9e4066Sahrens 
1015fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
10163b2aab18SMatthew Ahrens 
1017eb633035STom Caputi 	if (blksz == 0)
1018eb633035STom Caputi 		blksz = 1 << DNODE_BLOCK_SHIFT;
1019eb633035STom Caputi 	if (ibs == 0)
1020eb633035STom Caputi 		ibs = DN_MAX_INDBLKSHIFT;
1021eb633035STom Caputi 
1022feaa74e4SMark Maybee 	if (ds != NULL)
10233b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_from_ds(ds, &os));
1024feaa74e4SMark Maybee 	else
10253b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
1026feaa74e4SMark Maybee 
1027744947dcSTom Erickson 	mdn = DMU_META_DNODE(os);
1028fa9e4066Sahrens 
102954811da5SToomas Soome 	dnode_allocate(mdn, DMU_OT_DNODE, DNODE_BLOCK_SIZE, DN_MAX_INDBLKSHIFT,
103054811da5SToomas Soome 	    DMU_OT_NONE, 0, DNODE_MIN_SLOTS, tx);
1031fa9e4066Sahrens 
1032fa9e4066Sahrens 	/*
1033fa9e4066Sahrens 	 * We don't want to have to increase the meta-dnode's nlevels
1034fa9e4066Sahrens 	 * later, because then we could do it in quescing context while
1035fa9e4066Sahrens 	 * we are also accessing it in open context.
1036fa9e4066Sahrens 	 *
1037fa9e4066Sahrens 	 * This precaution is not necessary for the MOS (ds == NULL),
1038fa9e4066Sahrens 	 * because the MOS is only updated in syncing context.
1039fa9e4066Sahrens 	 * This is most fortunate: the MOS is the only objset that
1040fa9e4066Sahrens 	 * needs to be synced multiple times as spa_sync() iterates
1041fa9e4066Sahrens 	 * to convergence, so minimizing its dn_nlevels matters.
1042fa9e4066Sahrens 	 */
1043ea8dc4b6Seschrock 	if (ds != NULL) {
1044eb633035STom Caputi 		if (levels == 0) {
1045eb633035STom Caputi 			levels = 1;
1046eb633035STom Caputi 
1047eb633035STom Caputi 			/*
1048eb633035STom Caputi 			 * Determine the number of levels necessary for the
1049eb633035STom Caputi 			 * meta-dnode to contain DN_MAX_OBJECT dnodes.  Note
1050eb633035STom Caputi 			 * that in order to ensure that we do not overflow
1051eb633035STom Caputi 			 * 64 bits, there has to be a nlevels that gives us a
1052eb633035STom Caputi 			 * number of blocks > DN_MAX_OBJECT but < 2^64.
1053eb633035STom Caputi 			 * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)
1054eb633035STom Caputi 			 * (10) must be less than (64 - log2(DN_MAX_OBJECT))
1055eb633035STom Caputi 			 * (16).
1056eb633035STom Caputi 			 */
1057eb633035STom Caputi 			while ((uint64_t)mdn->dn_nblkptr <<
1058eb633035STom Caputi 			    (mdn->dn_datablkshift - DNODE_SHIFT + (levels - 1) *
1059eb633035STom Caputi 			    (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
1060eb633035STom Caputi 			    DN_MAX_OBJECT)
1061eb633035STom Caputi 				levels++;
1062eb633035STom Caputi 		}
1063ea8dc4b6Seschrock 
1064fa9e4066Sahrens 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
1065ea8dc4b6Seschrock 		    mdn->dn_nlevels = levels;
1066ea8dc4b6Seschrock 	}
1067fa9e4066Sahrens 
1068fa9e4066Sahrens 	ASSERT(type != DMU_OST_NONE);
1069fa9e4066Sahrens 	ASSERT(type != DMU_OST_ANY);
1070fa9e4066Sahrens 	ASSERT(type < DMU_OST_NUMTYPES);
1071503ad85cSMatthew Ahrens 	os->os_phys->os_type = type;
1072eb633035STom Caputi 
1073eb633035STom Caputi 	/*
1074eb633035STom Caputi 	 * Enable user accounting if it is enabled and this is not an
1075eb633035STom Caputi 	 * encrypted receive.
1076eb633035STom Caputi 	 */
1077eb633035STom Caputi 	if (dmu_objset_userused_enabled(os) &&
1078eb633035STom Caputi 	    (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
1079503ad85cSMatthew Ahrens 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1080f67950b2SNasf-Fan 		if (dmu_objset_userobjused_enabled(os)) {
1081f67950b2SNasf-Fan 			ds->ds_feature_activation_needed[
1082f67950b2SNasf-Fan 			    SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE;
1083f67950b2SNasf-Fan 			os->os_phys->os_flags |=
1084f67950b2SNasf-Fan 			    OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
1085f67950b2SNasf-Fan 		}
1086f67950b2SNasf-Fan 		if (dmu_objset_projectquota_enabled(os)) {
1087f67950b2SNasf-Fan 			ds->ds_feature_activation_needed[
1088f67950b2SNasf-Fan 			    SPA_FEATURE_PROJECT_QUOTA] = B_TRUE;
1089f67950b2SNasf-Fan 			os->os_phys->os_flags |=
1090f67950b2SNasf-Fan 			    OBJSET_FLAG_PROJECTQUOTA_COMPLETE;
1091f67950b2SNasf-Fan 		}
1092503ad85cSMatthew Ahrens 		os->os_flags = os->os_phys->os_flags;
109314843421SMatthew Ahrens 	}
1094fa9e4066Sahrens 
1095fa9e4066Sahrens 	dsl_dataset_dirty(ds, tx);
1096fa9e4066Sahrens 
1097503ad85cSMatthew Ahrens 	return (os);
1098fa9e4066Sahrens }
1099fa9e4066Sahrens 
1100eb633035STom Caputi /* called from dsl for meta-objset */
1101eb633035STom Caputi objset_t *
dmu_objset_create_impl(spa_t * spa,dsl_dataset_t * ds,blkptr_t * bp,dmu_objset_type_t type,dmu_tx_t * tx)1102eb633035STom Caputi dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
1103eb633035STom Caputi     dmu_objset_type_t type, dmu_tx_t *tx)
1104eb633035STom Caputi {
1105eb633035STom Caputi 	return (dmu_objset_create_impl_dnstats(spa, ds, bp, type, 0, 0, 0, tx));
1106eb633035STom Caputi }
1107eb633035STom Caputi 
11083b2aab18SMatthew Ahrens typedef struct dmu_objset_create_arg {
11093b2aab18SMatthew Ahrens 	const char *doca_name;
11103b2aab18SMatthew Ahrens 	cred_t *doca_cred;
11113b2aab18SMatthew Ahrens 	void (*doca_userfunc)(objset_t *os, void *arg,
11123b2aab18SMatthew Ahrens 	    cred_t *cr, dmu_tx_t *tx);
11133b2aab18SMatthew Ahrens 	void *doca_userarg;
11143b2aab18SMatthew Ahrens 	dmu_objset_type_t doca_type;
11153b2aab18SMatthew Ahrens 	uint64_t doca_flags;
1116eb633035STom Caputi 	dsl_crypto_params_t *doca_dcp;
11173b2aab18SMatthew Ahrens } dmu_objset_create_arg_t;
1118fa9e4066Sahrens 
1119ecd6cf80Smarks /*ARGSUSED*/
1120fa9e4066Sahrens static int
dmu_objset_create_check(void * arg,dmu_tx_t * tx)11213b2aab18SMatthew Ahrens dmu_objset_create_check(void *arg, dmu_tx_t *tx)
1122fa9e4066Sahrens {
11233b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
11243b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
11253b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
11263b2aab18SMatthew Ahrens 	const char *tail;
11273b2aab18SMatthew Ahrens 	int error;
11281d452cf5Sahrens 
11293b2aab18SMatthew Ahrens 	if (strchr(doca->doca_name, '@') != NULL)
1130be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
11311d452cf5Sahrens 
11329adfa60dSMatthew Ahrens 	if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
11339adfa60dSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
11349adfa60dSMatthew Ahrens 
11355ac95da7SSerapheim Dimitropoulos 	if (dataset_nestcheck(doca->doca_name) != 0)
11365ac95da7SSerapheim Dimitropoulos 		return (SET_ERROR(ENAMETOOLONG));
11375ac95da7SSerapheim Dimitropoulos 
11383b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
11393b2aab18SMatthew Ahrens 	if (error != 0)
11403b2aab18SMatthew Ahrens 		return (error);
11413b2aab18SMatthew Ahrens 	if (tail == NULL) {
11423b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
1143be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
11441d452cf5Sahrens 	}
1145eb633035STom Caputi 
1146eb633035STom Caputi 	error = dmu_objset_create_crypt_check(pdd, doca->doca_dcp, NULL);
1147eb633035STom Caputi 	if (error != 0) {
1148eb633035STom Caputi 		dsl_dir_rele(pdd, FTAG);
1149eb633035STom Caputi 		return (error);
1150eb633035STom Caputi 	}
1151eb633035STom Caputi 
1152a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1153a2afb611SJerry Jelinek 	    doca->doca_cred);
1154eb633035STom Caputi 
11553b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
1156ecd6cf80Smarks 
1157a2afb611SJerry Jelinek 	return (error);
11581d452cf5Sahrens }
11591d452cf5Sahrens 
11601d452cf5Sahrens static void
dmu_objset_create_sync(void * arg,dmu_tx_t * tx)11613b2aab18SMatthew Ahrens dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
11621d452cf5Sahrens {
11633b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t *doca = arg;
11643b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
1165eb633035STom Caputi 	spa_t *spa = dp->dp_spa;
11663b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
11673b2aab18SMatthew Ahrens 	const char *tail;
11684445fffbSMatthew Ahrens 	dsl_dataset_t *ds;
11693b2aab18SMatthew Ahrens 	uint64_t obj;
11704445fffbSMatthew Ahrens 	blkptr_t *bp;
11713b2aab18SMatthew Ahrens 	objset_t *os;
1172eb633035STom Caputi 	zio_t *rzio;
1173fa9e4066Sahrens 
11743b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
1175fa9e4066Sahrens 
11763b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
1177eb633035STom Caputi 	    doca->doca_cred, doca->doca_dcp, tx);
1178fa9e4066Sahrens 
1179eb633035STom Caputi 	VERIFY0(dsl_dataset_hold_obj_flags(pdd->dd_pool, obj,
1180eb633035STom Caputi 	    DS_HOLD_FLAG_DECRYPT, FTAG, &ds));
1181c166b69dSPaul Dagnelie 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
11824445fffbSMatthew Ahrens 	bp = dsl_dataset_get_blkptr(ds);
1183eb633035STom Caputi 	os = dmu_objset_create_impl(spa, ds, bp, doca->doca_type, tx);
1184c166b69dSPaul Dagnelie 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
1185fa9e4066Sahrens 
11863b2aab18SMatthew Ahrens 	if (doca->doca_userfunc != NULL) {
11873b2aab18SMatthew Ahrens 		doca->doca_userfunc(os, doca->doca_userarg,
11883b2aab18SMatthew Ahrens 		    doca->doca_cred, tx);
1189fa9e4066Sahrens 	}
1190ecd6cf80Smarks 
1191eb633035STom Caputi 	/*
1192eb633035STom Caputi 	 * The doca_userfunc() may write out some data that needs to be
1193eb633035STom Caputi 	 * encrypted if the dataset is encrypted (specifically the root
1194eb633035STom Caputi 	 * directory).  This data must be written out before the encryption
1195eb633035STom Caputi 	 * key mapping is removed by dsl_dataset_rele_flags().  Force the
1196eb633035STom Caputi 	 * I/O to occur immediately by invoking the relevant sections of
1197eb633035STom Caputi 	 * dsl_pool_sync().
1198eb633035STom Caputi 	 */
1199eb633035STom Caputi 	if (os->os_encrypted) {
1200eb633035STom Caputi 		dsl_dataset_t *tmpds = NULL;
1201eb633035STom Caputi 		boolean_t need_sync_done = B_FALSE;
1202eb633035STom Caputi 
1203eb633035STom Caputi 		mutex_enter(&ds->ds_lock);
1204eb633035STom Caputi 		ds->ds_owner = FTAG;
1205eb633035STom Caputi 		mutex_exit(&ds->ds_lock);
1206eb633035STom Caputi 
1207eb633035STom Caputi 		rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1208eb633035STom Caputi 		tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds,
1209eb633035STom Caputi 		    tx->tx_txg);
1210eb633035STom Caputi 		if (tmpds != NULL) {
1211eb633035STom Caputi 			dsl_dataset_sync(ds, rzio, tx);
1212eb633035STom Caputi 			need_sync_done = B_TRUE;
1213eb633035STom Caputi 		}
1214eb633035STom Caputi 		VERIFY0(zio_wait(rzio));
1215eb633035STom Caputi 		dmu_objset_do_userquota_updates(os, tx);
1216eb633035STom Caputi 		taskq_wait(dp->dp_sync_taskq);
1217eb633035STom Caputi 		if (txg_list_member(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1218eb633035STom Caputi 			ASSERT3P(ds->ds_key_mapping, !=, NULL);
1219eb633035STom Caputi 			key_mapping_rele(spa, ds->ds_key_mapping, ds);
1220eb633035STom Caputi 		}
1221eb633035STom Caputi 
1222eb633035STom Caputi 		rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1223eb633035STom Caputi 		tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds,
1224eb633035STom Caputi 		    tx->tx_txg);
1225eb633035STom Caputi 		if (tmpds != NULL) {
1226eb633035STom Caputi 			dmu_buf_rele(ds->ds_dbuf, ds);
1227eb633035STom Caputi 			dsl_dataset_sync(ds, rzio, tx);
1228eb633035STom Caputi 		}
1229eb633035STom Caputi 		VERIFY0(zio_wait(rzio));
1230eb633035STom Caputi 
1231eb633035STom Caputi 		if (need_sync_done) {
1232eb633035STom Caputi 			ASSERT3P(ds->ds_key_mapping, !=, NULL);
1233eb633035STom Caputi 			key_mapping_rele(spa, ds->ds_key_mapping, ds);
1234eb633035STom Caputi 			dsl_dataset_sync_done(ds, tx);
1235eb633035STom Caputi 		}
1236eb633035STom Caputi 
1237eb633035STom Caputi 		mutex_enter(&ds->ds_lock);
1238eb633035STom Caputi 		ds->ds_owner = NULL;
1239eb633035STom Caputi 		mutex_exit(&ds->ds_lock);
1240eb633035STom Caputi 	}
1241eb633035STom Caputi 
12423b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "create", tx, "");
1243eb633035STom Caputi 	dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
12443b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
1245fa9e4066Sahrens }
1246fa9e4066Sahrens 
1247fa9e4066Sahrens int
dmu_objset_create(const char * name,dmu_objset_type_t type,uint64_t flags,dsl_crypto_params_t * dcp,dmu_objset_create_sync_func_t func,void * arg)1248ae46e4c7SMatthew Ahrens dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
1249eb633035STom Caputi     dsl_crypto_params_t *dcp, dmu_objset_create_sync_func_t func, void *arg)
1250fa9e4066Sahrens {
12513b2aab18SMatthew Ahrens 	dmu_objset_create_arg_t doca;
1252eb633035STom Caputi 	dsl_crypto_params_t tmp_dcp = { 0 };
1253fa9e4066Sahrens 
12543b2aab18SMatthew Ahrens 	doca.doca_name = name;
12553b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
12563b2aab18SMatthew Ahrens 	doca.doca_flags = flags;
12573b2aab18SMatthew Ahrens 	doca.doca_userfunc = func;
12583b2aab18SMatthew Ahrens 	doca.doca_userarg = arg;
12593b2aab18SMatthew Ahrens 	doca.doca_type = type;
1260ecd6cf80Smarks 
1261eb633035STom Caputi 	/*
1262eb633035STom Caputi 	 * Some callers (mostly for testing) do not provide a dcp on their
1263eb633035STom Caputi 	 * own but various code inside the sync task will require it to be
1264eb633035STom Caputi 	 * allocated. Rather than adding NULL checks throughout this code
1265eb633035STom Caputi 	 * or adding dummy dcp's to all of the callers we simply create a
1266eb633035STom Caputi 	 * dummy one here and use that. This zero dcp will have the same
1267eb633035STom Caputi 	 * effect as asking for inheritence of all encryption params.
1268eb633035STom Caputi 	 */
1269eb633035STom Caputi 	doca.doca_dcp = (dcp != NULL) ? dcp : &tmp_dcp;
1270eb633035STom Caputi 
12713b2aab18SMatthew Ahrens 	return (dsl_sync_task(name,
12727d46dc6cSMatthew Ahrens 	    dmu_objset_create_check, dmu_objset_create_sync, &doca,
1273eb633035STom Caputi 	    6, ZFS_SPACE_CHECK_NORMAL));
1274ae46e4c7SMatthew Ahrens }
1275ae46e4c7SMatthew Ahrens 
12763b2aab18SMatthew Ahrens typedef struct dmu_objset_clone_arg {
12773b2aab18SMatthew Ahrens 	const char *doca_clone;
12783b2aab18SMatthew Ahrens 	const char *doca_origin;
12793b2aab18SMatthew Ahrens 	cred_t *doca_cred;
12803b2aab18SMatthew Ahrens } dmu_objset_clone_arg_t;
12813b2aab18SMatthew Ahrens 
12823b2aab18SMatthew Ahrens /*ARGSUSED*/
12833b2aab18SMatthew Ahrens static int
dmu_objset_clone_check(void * arg,dmu_tx_t * tx)12843b2aab18SMatthew Ahrens dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
1285ae46e4c7SMatthew Ahrens {
12863b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
1287ae46e4c7SMatthew Ahrens 	dsl_dir_t *pdd;
1288ae46e4c7SMatthew Ahrens 	const char *tail;
12893b2aab18SMatthew Ahrens 	int error;
12903b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
12913b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
1292ae46e4c7SMatthew Ahrens 
12933b2aab18SMatthew Ahrens 	if (strchr(doca->doca_clone, '@') != NULL)
1294be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12953b2aab18SMatthew Ahrens 
12969adfa60dSMatthew Ahrens 	if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
12979adfa60dSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
12989adfa60dSMatthew Ahrens 
12993b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
13003b2aab18SMatthew Ahrens 	if (error != 0)
13013b2aab18SMatthew Ahrens 		return (error);
1302ae46e4c7SMatthew Ahrens 	if (tail == NULL) {
13033b2aab18SMatthew Ahrens 		dsl_dir_rele(pdd, FTAG);
1304be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1305fa9e4066Sahrens 	}
130603b1c297SAlexander Eremin 
1307a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1308a2afb611SJerry Jelinek 	    doca->doca_cred);
1309a2afb611SJerry Jelinek 	if (error != 0) {
1310a2afb611SJerry Jelinek 		dsl_dir_rele(pdd, FTAG);
1311a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
1312a2afb611SJerry Jelinek 	}
1313fa9e4066Sahrens 
13143b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
1315eb633035STom Caputi 	if (error != 0) {
1316eb633035STom Caputi 		dsl_dir_rele(pdd, FTAG);
131799d5e173STim Haley 		return (error);
1318eb633035STom Caputi 	}
131999d5e173STim Haley 
13203b2aab18SMatthew Ahrens 	/* You can only clone snapshots, not the head datasets. */
1321bc9014e6SJustin Gibbs 	if (!origin->ds_is_snapshot) {
13223b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
1323eb633035STom Caputi 		dsl_dir_rele(pdd, FTAG);
1324be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
132592241e0bSTom Erickson 	}
1326eb633035STom Caputi 
13273b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
1328eb633035STom Caputi 	dsl_dir_rele(pdd, FTAG);
132999d5e173STim Haley 
13303b2aab18SMatthew Ahrens 	return (0);
1331ea2f5b9eSMatthew Ahrens }
13321d452cf5Sahrens 
13333b2aab18SMatthew Ahrens static void
dmu_objset_clone_sync(void * arg,dmu_tx_t * tx)13343b2aab18SMatthew Ahrens dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
13351d452cf5Sahrens {
13363b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t *doca = arg;
13373b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
13383b2aab18SMatthew Ahrens 	dsl_dir_t *pdd;
13393b2aab18SMatthew Ahrens 	const char *tail;
13403b2aab18SMatthew Ahrens 	dsl_dataset_t *origin, *ds;
13413b2aab18SMatthew Ahrens 	uint64_t obj;
13429adfa60dSMatthew Ahrens 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
13434445fffbSMatthew Ahrens 
13443b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
13453b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
13464445fffbSMatthew Ahrens 
13473b2aab18SMatthew Ahrens 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
1348eb633035STom Caputi 	    doca->doca_cred, NULL, tx);
1349f2e10be3Srm 
13503b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
13513b2aab18SMatthew Ahrens 	dsl_dataset_name(origin, namebuf);
13523b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "clone", tx,
13533b2aab18SMatthew Ahrens 	    "origin=%s (%llu)", namebuf, origin->ds_object);
13543b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
13553b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
13563b2aab18SMatthew Ahrens 	dsl_dir_rele(pdd, FTAG);
13571d452cf5Sahrens }
13581d452cf5Sahrens 
13591d452cf5Sahrens int
dmu_objset_clone(const char * clone,const char * origin)13603b2aab18SMatthew Ahrens dmu_objset_clone(const char *clone, const char *origin)
13611d452cf5Sahrens {
13623b2aab18SMatthew Ahrens 	dmu_objset_clone_arg_t doca;
13634445fffbSMatthew Ahrens 
13643b2aab18SMatthew Ahrens 	doca.doca_clone = clone;
13653b2aab18SMatthew Ahrens 	doca.doca_origin = origin;
13663b2aab18SMatthew Ahrens 	doca.doca_cred = CRED();
136799d5e173STim Haley 
13683b2aab18SMatthew Ahrens 	return (dsl_sync_task(clone,
13697d46dc6cSMatthew Ahrens 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
1370eb633035STom Caputi 	    6, ZFS_SPACE_CHECK_NORMAL));
13714445fffbSMatthew Ahrens }
13724445fffbSMatthew Ahrens 
13735cabbc6bSPrashanth Sreenivasa static int
dmu_objset_remap_indirects_impl(objset_t * os,uint64_t last_removed_txg)13745cabbc6bSPrashanth Sreenivasa dmu_objset_remap_indirects_impl(objset_t *os, uint64_t last_removed_txg)
13755cabbc6bSPrashanth Sreenivasa {
13765cabbc6bSPrashanth Sreenivasa 	int error = 0;
13775cabbc6bSPrashanth Sreenivasa 	uint64_t object = 0;
13785cabbc6bSPrashanth Sreenivasa 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
13795cabbc6bSPrashanth Sreenivasa 		error = dmu_object_remap_indirects(os, object,
13805cabbc6bSPrashanth Sreenivasa 		    last_removed_txg);
13815cabbc6bSPrashanth Sreenivasa 		/*
13825cabbc6bSPrashanth Sreenivasa 		 * If the ZPL removed the object before we managed to dnode_hold
13835cabbc6bSPrashanth Sreenivasa 		 * it, we would get an ENOENT. If the ZPL declares its intent
13845cabbc6bSPrashanth Sreenivasa 		 * to remove the object (dnode_free) before we manage to
13855cabbc6bSPrashanth Sreenivasa 		 * dnode_hold it, we would get an EEXIST. In either case, we
13865cabbc6bSPrashanth Sreenivasa 		 * want to continue remapping the other objects in the objset;
13875cabbc6bSPrashanth Sreenivasa 		 * in all other cases, we want to break early.
13885cabbc6bSPrashanth Sreenivasa 		 */
13895cabbc6bSPrashanth Sreenivasa 		if (error != 0 && error != ENOENT && error != EEXIST) {
13905cabbc6bSPrashanth Sreenivasa 			break;
13915cabbc6bSPrashanth Sreenivasa 		}
13925cabbc6bSPrashanth Sreenivasa 	}
13935cabbc6bSPrashanth Sreenivasa 	if (error == ESRCH) {
13945cabbc6bSPrashanth Sreenivasa 		error = 0;
13955cabbc6bSPrashanth Sreenivasa 	}
13965cabbc6bSPrashanth Sreenivasa 	return (error);
13975cabbc6bSPrashanth Sreenivasa }
13985cabbc6bSPrashanth Sreenivasa 
13995cabbc6bSPrashanth Sreenivasa int
dmu_objset_remap_indirects(const char * fsname)14005cabbc6bSPrashanth Sreenivasa dmu_objset_remap_indirects(const char *fsname)
14015cabbc6bSPrashanth Sreenivasa {
14025cabbc6bSPrashanth Sreenivasa 	int error = 0;
14035cabbc6bSPrashanth Sreenivasa 	objset_t *os = NULL;
14045cabbc6bSPrashanth Sreenivasa 	uint64_t last_removed_txg;
14055cabbc6bSPrashanth Sreenivasa 	uint64_t remap_start_txg;
14065cabbc6bSPrashanth Sreenivasa 	dsl_dir_t *dd;
14075cabbc6bSPrashanth Sreenivasa 
14085cabbc6bSPrashanth Sreenivasa 	error = dmu_objset_hold(fsname, FTAG, &os);
14095cabbc6bSPrashanth Sreenivasa 	if (error != 0) {
14105cabbc6bSPrashanth Sreenivasa 		return (error);
14115cabbc6bSPrashanth Sreenivasa 	}
14125cabbc6bSPrashanth Sreenivasa 	dd = dmu_objset_ds(os)->ds_dir;
14135cabbc6bSPrashanth Sreenivasa 
14145cabbc6bSPrashanth Sreenivasa 	if (!spa_feature_is_enabled(dmu_objset_spa(os),
14155cabbc6bSPrashanth Sreenivasa 	    SPA_FEATURE_OBSOLETE_COUNTS)) {
14165cabbc6bSPrashanth Sreenivasa 		dmu_objset_rele(os, FTAG);
14175cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(ENOTSUP));
14185cabbc6bSPrashanth Sreenivasa 	}
14195cabbc6bSPrashanth Sreenivasa 
14205cabbc6bSPrashanth Sreenivasa 	if (dsl_dataset_is_snapshot(dmu_objset_ds(os))) {
14215cabbc6bSPrashanth Sreenivasa 		dmu_objset_rele(os, FTAG);
14225cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EINVAL));
14235cabbc6bSPrashanth Sreenivasa 	}
14245cabbc6bSPrashanth Sreenivasa 
14255cabbc6bSPrashanth Sreenivasa 	/*
14265cabbc6bSPrashanth Sreenivasa 	 * If there has not been a removal, we're done.
14275cabbc6bSPrashanth Sreenivasa 	 */
14285cabbc6bSPrashanth Sreenivasa 	last_removed_txg = spa_get_last_removal_txg(dmu_objset_spa(os));
14295cabbc6bSPrashanth Sreenivasa 	if (last_removed_txg == -1ULL) {
14305cabbc6bSPrashanth Sreenivasa 		dmu_objset_rele(os, FTAG);
14315cabbc6bSPrashanth Sreenivasa 		return (0);
14325cabbc6bSPrashanth Sreenivasa 	}
14335cabbc6bSPrashanth Sreenivasa 
14345cabbc6bSPrashanth Sreenivasa 	/*
14355cabbc6bSPrashanth Sreenivasa 	 * If we have remapped since the last removal, we're done.
14365cabbc6bSPrashanth Sreenivasa 	 */
14375cabbc6bSPrashanth Sreenivasa 	if (dsl_dir_is_zapified(dd)) {
14385cabbc6bSPrashanth Sreenivasa 		uint64_t last_remap_txg;
14395cabbc6bSPrashanth Sreenivasa 		if (zap_lookup(spa_meta_objset(dmu_objset_spa(os)),
14405cabbc6bSPrashanth Sreenivasa 		    dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
14415cabbc6bSPrashanth Sreenivasa 		    sizeof (last_remap_txg), 1, &last_remap_txg) == 0 &&
14425cabbc6bSPrashanth Sreenivasa 		    last_remap_txg > last_removed_txg) {
14435cabbc6bSPrashanth Sreenivasa 			dmu_objset_rele(os, FTAG);
14445cabbc6bSPrashanth Sreenivasa 			return (0);
14455cabbc6bSPrashanth Sreenivasa 		}
14465cabbc6bSPrashanth Sreenivasa 	}
14475cabbc6bSPrashanth Sreenivasa 
14485cabbc6bSPrashanth Sreenivasa 	dsl_dataset_long_hold(dmu_objset_ds(os), FTAG);
14495cabbc6bSPrashanth Sreenivasa 	dsl_pool_rele(dmu_objset_pool(os), FTAG);
14505cabbc6bSPrashanth Sreenivasa 
14515cabbc6bSPrashanth Sreenivasa 	remap_start_txg = spa_last_synced_txg(dmu_objset_spa(os));
14525cabbc6bSPrashanth Sreenivasa 	error = dmu_objset_remap_indirects_impl(os, last_removed_txg);
14535cabbc6bSPrashanth Sreenivasa 	if (error == 0) {
14545cabbc6bSPrashanth Sreenivasa 		/*
14555cabbc6bSPrashanth Sreenivasa 		 * We update the last_remap_txg to be the start txg so that
14565cabbc6bSPrashanth Sreenivasa 		 * we can guarantee that every block older than last_remap_txg
14575cabbc6bSPrashanth Sreenivasa 		 * that can be remapped has been remapped.
14585cabbc6bSPrashanth Sreenivasa 		 */
14595cabbc6bSPrashanth Sreenivasa 		error = dsl_dir_update_last_remap_txg(dd, remap_start_txg);
14605cabbc6bSPrashanth Sreenivasa 	}
14615cabbc6bSPrashanth Sreenivasa 
14625cabbc6bSPrashanth Sreenivasa 	dsl_dataset_long_rele(dmu_objset_ds(os), FTAG);
14635cabbc6bSPrashanth Sreenivasa 	dsl_dataset_rele(dmu_objset_ds(os), FTAG);
14645cabbc6bSPrashanth Sreenivasa 
14655cabbc6bSPrashanth Sreenivasa 	return (error);
14665cabbc6bSPrashanth Sreenivasa }
14675cabbc6bSPrashanth Sreenivasa 
14684445fffbSMatthew Ahrens int
dmu_objset_snapshot_one(const char * fsname,const char * snapname)14694445fffbSMatthew Ahrens dmu_objset_snapshot_one(const char *fsname, const char *snapname)
14704445fffbSMatthew Ahrens {
14714445fffbSMatthew Ahrens 	int err;
14724445fffbSMatthew Ahrens 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
14734445fffbSMatthew Ahrens 	nvlist_t *snaps = fnvlist_alloc();
14744445fffbSMatthew Ahrens 
14754445fffbSMatthew Ahrens 	fnvlist_add_boolean(snaps, longsnap);
14764445fffbSMatthew Ahrens 	strfree(longsnap);
14773b2aab18SMatthew Ahrens 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
14783b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
14794445fffbSMatthew Ahrens 	return (err);
14804445fffbSMatthew Ahrens }
14814445fffbSMatthew Ahrens 
1482f67950b2SNasf-Fan static void
dmu_objset_upgrade_task_cb(void * data)1483f67950b2SNasf-Fan dmu_objset_upgrade_task_cb(void *data)
1484f67950b2SNasf-Fan {
1485f67950b2SNasf-Fan 	objset_t *os = data;
1486f67950b2SNasf-Fan 
1487f67950b2SNasf-Fan 	mutex_enter(&os->os_upgrade_lock);
1488f67950b2SNasf-Fan 	os->os_upgrade_status = EINTR;
1489f67950b2SNasf-Fan 	if (!os->os_upgrade_exit) {
1490cc37296fSAndy Fiddaman 		int status;
1491cc37296fSAndy Fiddaman 
1492f67950b2SNasf-Fan 		mutex_exit(&os->os_upgrade_lock);
1493f67950b2SNasf-Fan 
1494cc37296fSAndy Fiddaman 		status = os->os_upgrade_cb(os);
1495cc37296fSAndy Fiddaman 
1496f67950b2SNasf-Fan 		mutex_enter(&os->os_upgrade_lock);
1497cc37296fSAndy Fiddaman 
1498cc37296fSAndy Fiddaman 		os->os_upgrade_status = status;
1499f67950b2SNasf-Fan 	}
1500f67950b2SNasf-Fan 	os->os_upgrade_exit = B_TRUE;
1501f67950b2SNasf-Fan 	os->os_upgrade_id = 0;
1502f67950b2SNasf-Fan 	mutex_exit(&os->os_upgrade_lock);
1503a39df08cSArkadiusz Bubała 	dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1504f67950b2SNasf-Fan }
1505f67950b2SNasf-Fan 
1506f67950b2SNasf-Fan static void
dmu_objset_upgrade(objset_t * os,dmu_objset_upgrade_cb_t cb)1507f67950b2SNasf-Fan dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb)
1508f67950b2SNasf-Fan {
1509f67950b2SNasf-Fan 	if (os->os_upgrade_id != 0)
1510f67950b2SNasf-Fan 		return;
1511f67950b2SNasf-Fan 
1512a39df08cSArkadiusz Bubała 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1513a39df08cSArkadiusz Bubała 	dsl_dataset_long_hold(dmu_objset_ds(os), upgrade_tag);
1514a39df08cSArkadiusz Bubała 
1515f67950b2SNasf-Fan 	mutex_enter(&os->os_upgrade_lock);
1516f67950b2SNasf-Fan 	if (os->os_upgrade_id == 0 && os->os_upgrade_status == 0) {
1517f67950b2SNasf-Fan 		os->os_upgrade_exit = B_FALSE;
1518f67950b2SNasf-Fan 		os->os_upgrade_cb = cb;
1519f67950b2SNasf-Fan 		os->os_upgrade_id = taskq_dispatch(
1520f67950b2SNasf-Fan 		    os->os_spa->spa_upgrade_taskq,
1521f67950b2SNasf-Fan 		    dmu_objset_upgrade_task_cb, os, TQ_SLEEP);
1522a39df08cSArkadiusz Bubała 		if (os->os_upgrade_id == TASKQID_INVALID) {
1523a39df08cSArkadiusz Bubała 			dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1524f67950b2SNasf-Fan 			os->os_upgrade_status = ENOMEM;
1525a39df08cSArkadiusz Bubała 		}
1526cc37296fSAndy Fiddaman 	} else {
1527cc37296fSAndy Fiddaman 		dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1528f67950b2SNasf-Fan 	}
1529f67950b2SNasf-Fan 	mutex_exit(&os->os_upgrade_lock);
1530f67950b2SNasf-Fan }
1531f67950b2SNasf-Fan 
1532f67950b2SNasf-Fan static void
dmu_objset_upgrade_stop(objset_t * os)1533f67950b2SNasf-Fan dmu_objset_upgrade_stop(objset_t *os)
1534f67950b2SNasf-Fan {
1535f67950b2SNasf-Fan 	mutex_enter(&os->os_upgrade_lock);
1536f67950b2SNasf-Fan 	os->os_upgrade_exit = B_TRUE;
1537f67950b2SNasf-Fan 	if (os->os_upgrade_id != 0) {
1538a39df08cSArkadiusz Bubała 		taskqid_t tid = os->os_upgrade_id;
1539a39df08cSArkadiusz Bubała 
1540f67950b2SNasf-Fan 		mutex_exit(&os->os_upgrade_lock);
1541f67950b2SNasf-Fan 
1542a39df08cSArkadiusz Bubała 		taskq_wait_id(os->os_spa->spa_upgrade_taskq, tid);
1543cc37296fSAndy Fiddaman 		txg_wait_synced(os->os_spa->spa_dsl_pool, 0);
1544f67950b2SNasf-Fan 	} else {
1545f67950b2SNasf-Fan 		mutex_exit(&os->os_upgrade_lock);
1546f67950b2SNasf-Fan 	}
1547f67950b2SNasf-Fan }
1548f67950b2SNasf-Fan 
1549fa9e4066Sahrens static void
dmu_objset_sync_dnodes(multilist_sublist_t * list,dmu_tx_t * tx)155094c2d0ebSMatthew Ahrens dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx)
1551fa9e4066Sahrens {
1552c717a561Smaybee 	dnode_t *dn;
1553faafa6e3Sahrens 
155494c2d0ebSMatthew Ahrens 	while ((dn = multilist_sublist_head(list)) != NULL) {
1555c717a561Smaybee 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1556c717a561Smaybee 		ASSERT(dn->dn_dbuf->db_data_pending);
1557c717a561Smaybee 		/*
155814843421SMatthew Ahrens 		 * Initialize dn_zio outside dnode_sync() because the
1559814dcd43SSerapheim Dimitropoulos 		 * meta-dnode needs to set it outside dnode_sync().
1560c717a561Smaybee 		 */
1561c717a561Smaybee 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1562c717a561Smaybee 		ASSERT(dn->dn_zio);
1563faafa6e3Sahrens 
1564c717a561Smaybee 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
156594c2d0ebSMatthew Ahrens 		multilist_sublist_remove(list, dn);
156614843421SMatthew Ahrens 
1567aa02ea01STom Caputi 		/*
1568aa02ea01STom Caputi 		 * If we are not doing useraccounting (os_synced_dnodes == NULL)
1569aa02ea01STom Caputi 		 * we are done with this dnode for this txg. Unset dn_dirty_txg
1570aa02ea01STom Caputi 		 * if later txgs aren't dirtying it so that future holders do
1571aa02ea01STom Caputi 		 * not get a stale value. Otherwise, we will do this in
1572aa02ea01STom Caputi 		 * userquota_updates_task() when processing has completely
1573aa02ea01STom Caputi 		 * finished for this txg.
1574aa02ea01STom Caputi 		 */
157594c2d0ebSMatthew Ahrens 		multilist_t *newlist = dn->dn_objset->os_synced_dnodes;
157694c2d0ebSMatthew Ahrens 		if (newlist != NULL) {
157714843421SMatthew Ahrens 			(void) dnode_add_ref(dn, newlist);
157894c2d0ebSMatthew Ahrens 			multilist_insert(newlist, dn);
1579aa02ea01STom Caputi 		} else {
1580aa02ea01STom Caputi 			mutex_enter(&dn->dn_mtx);
1581aa02ea01STom Caputi 			if (dn->dn_dirty_txg == tx->tx_txg)
1582aa02ea01STom Caputi 				dn->dn_dirty_txg = 0;
1583aa02ea01STom Caputi 			mutex_exit(&dn->dn_mtx);
158414843421SMatthew Ahrens 		}
158514843421SMatthew Ahrens 
1586c717a561Smaybee 		dnode_sync(dn, tx);
1587fa9e4066Sahrens 	}
1588fa9e4066Sahrens }
1589fa9e4066Sahrens 
1590fa9e4066Sahrens /* ARGSUSED */
1591fa9e4066Sahrens static void
dmu_objset_write_ready(zio_t * zio,arc_buf_t * abuf,void * arg)1592b24ab676SJeff Bonwick dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1593fa9e4066Sahrens {
1594e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1595503ad85cSMatthew Ahrens 	objset_t *os = arg;
1596c717a561Smaybee 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1597eb633035STom Caputi 	uint64_t fill = 0;
1598fa9e4066Sahrens 
15995d7b4d43SMatthew Ahrens 	ASSERT(!BP_IS_EMBEDDED(bp));
16003b2aab18SMatthew Ahrens 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
16010a4e9518Sgw 
1602fa9e4066Sahrens 	/*
160314843421SMatthew Ahrens 	 * Update rootbp fill count: it should be the number of objects
160414843421SMatthew Ahrens 	 * allocated in the object set (not counting the "special"
160514843421SMatthew Ahrens 	 * objects that are stored in the objset_phys_t -- the meta
1606f67950b2SNasf-Fan 	 * dnode and user/group/project accounting objects).
1607fa9e4066Sahrens 	 */
1608e14bb325SJeff Bonwick 	for (int i = 0; i < dnp->dn_nblkptr; i++)
1609eb633035STom Caputi 		fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1610eb633035STom Caputi 
1611eb633035STom Caputi 	BP_SET_FILL(bp, fill);
1612eb633035STom Caputi 
1613c166b69dSPaul Dagnelie 	if (os->os_dsl_dataset != NULL)
1614c166b69dSPaul Dagnelie 		rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1615c166b69dSPaul Dagnelie 	*os->os_rootbp = *bp;
1616c166b69dSPaul Dagnelie 	if (os->os_dsl_dataset != NULL)
1617c166b69dSPaul Dagnelie 		rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1618b24ab676SJeff Bonwick }
1619b24ab676SJeff Bonwick 
1620b24ab676SJeff Bonwick /* ARGSUSED */
1621b24ab676SJeff Bonwick static void
dmu_objset_write_done(zio_t * zio,arc_buf_t * abuf,void * arg)1622b24ab676SJeff Bonwick dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1623b24ab676SJeff Bonwick {
1624b24ab676SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
1625b24ab676SJeff Bonwick 	blkptr_t *bp_orig = &zio->io_bp_orig;
1626b24ab676SJeff Bonwick 	objset_t *os = arg;
16270a4e9518Sgw 
1628e14bb325SJeff Bonwick 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1629b24ab676SJeff Bonwick 		ASSERT(BP_EQUAL(bp, bp_orig));
1630e14bb325SJeff Bonwick 	} else {
1631b24ab676SJeff Bonwick 		dsl_dataset_t *ds = os->os_dsl_dataset;
1632b24ab676SJeff Bonwick 		dmu_tx_t *tx = os->os_synctx;
1633b24ab676SJeff Bonwick 
1634b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1635b24ab676SJeff Bonwick 		dsl_dataset_block_born(ds, bp, tx);
16360a4e9518Sgw 	}
1637c166b69dSPaul Dagnelie 	kmem_free(bp, sizeof (*bp));
1638c717a561Smaybee }
1639c717a561Smaybee 
164094c2d0ebSMatthew Ahrens typedef struct sync_dnodes_arg {
164194c2d0ebSMatthew Ahrens 	multilist_t *sda_list;
164294c2d0ebSMatthew Ahrens 	int sda_sublist_idx;
164394c2d0ebSMatthew Ahrens 	multilist_t *sda_newlist;
164494c2d0ebSMatthew Ahrens 	dmu_tx_t *sda_tx;
164594c2d0ebSMatthew Ahrens } sync_dnodes_arg_t;
164694c2d0ebSMatthew Ahrens 
164794c2d0ebSMatthew Ahrens static void
sync_dnodes_task(void * arg)164894c2d0ebSMatthew Ahrens sync_dnodes_task(void *arg)
164994c2d0ebSMatthew Ahrens {
165094c2d0ebSMatthew Ahrens 	sync_dnodes_arg_t *sda = arg;
165194c2d0ebSMatthew Ahrens 
165294c2d0ebSMatthew Ahrens 	multilist_sublist_t *ms =
165394c2d0ebSMatthew Ahrens 	    multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
165494c2d0ebSMatthew Ahrens 
165594c2d0ebSMatthew Ahrens 	dmu_objset_sync_dnodes(ms, sda->sda_tx);
165694c2d0ebSMatthew Ahrens 
165794c2d0ebSMatthew Ahrens 	multilist_sublist_unlock(ms);
165894c2d0ebSMatthew Ahrens 
165994c2d0ebSMatthew Ahrens 	kmem_free(sda, sizeof (*sda));
166094c2d0ebSMatthew Ahrens }
166194c2d0ebSMatthew Ahrens 
166294c2d0ebSMatthew Ahrens 
1663fa9e4066Sahrens /* called from dsl */
1664fa9e4066Sahrens void
dmu_objset_sync(objset_t * os,zio_t * pio,dmu_tx_t * tx)1665503ad85cSMatthew Ahrens dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1666fa9e4066Sahrens {
1667fa9e4066Sahrens 	int txgoff;
16687802d7bfSMatthew Ahrens 	zbookmark_phys_t zb;
1669b24ab676SJeff Bonwick 	zio_prop_t zp;
1670c717a561Smaybee 	zio_t *zio;
1671c717a561Smaybee 	list_t *list;
1672c717a561Smaybee 	dbuf_dirty_record_t *dr;
1673c166b69dSPaul Dagnelie 	blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1674c166b69dSPaul Dagnelie 	*blkptr_copy = *os->os_rootbp;
1675c717a561Smaybee 
1676c717a561Smaybee 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1677fa9e4066Sahrens 
1678fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1679fa9e4066Sahrens 	/* XXX the write_done callback should really give us the tx... */
1680fa9e4066Sahrens 	os->os_synctx = tx;
1681fa9e4066Sahrens 
168287bd5c1eSahrens 	if (os->os_dsl_dataset == NULL) {
168387bd5c1eSahrens 		/*
168487bd5c1eSahrens 		 * This is the MOS.  If we have upgraded,
168587bd5c1eSahrens 		 * spa_max_replication() could change, so reset
168687bd5c1eSahrens 		 * os_copies here.
168787bd5c1eSahrens 		 */
168887bd5c1eSahrens 		os->os_copies = spa_max_replication(os->os_spa);
168987bd5c1eSahrens 	}
169087bd5c1eSahrens 
1691fa9e4066Sahrens 	/*
1692c717a561Smaybee 	 * Create the root block IO
1693fa9e4066Sahrens 	 */
1694b24ab676SJeff Bonwick 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1695b24ab676SJeff Bonwick 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1696b24ab676SJeff Bonwick 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
16971b912ec7SGeorge Wilson 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1698b24ab676SJeff Bonwick 
1699adaec86aSMatthew Ahrens 	dmu_write_policy(os, NULL, 0, 0, &zp);
1700b24ab676SJeff Bonwick 
1701eb633035STom Caputi 	/*
1702eb633035STom Caputi 	 * If we are either claiming the ZIL or doing a raw receive, write
1703eb633035STom Caputi 	 * out the os_phys_buf raw. Neither of these actions will effect the
1704eb633035STom Caputi 	 * MAC at this point.
1705eb633035STom Caputi 	 */
1706eb633035STom Caputi 	if (os->os_raw_receive ||
1707eb633035STom Caputi 	    os->os_next_write_raw[tx->tx_txg & TXG_MASK]) {
1708eb633035STom Caputi 		ASSERT(os->os_encrypted);
1709eb633035STom Caputi 		arc_convert_to_raw(os->os_phys_buf,
1710eb633035STom Caputi 		    os->os_dsl_dataset->ds_object, ZFS_HOST_BYTEORDER,
1711eb633035STom Caputi 		    DMU_OT_OBJSET, NULL, NULL, NULL);
1712eb633035STom Caputi 	}
1713eb633035STom Caputi 
1714b24ab676SJeff Bonwick 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1715c166b69dSPaul Dagnelie 	    blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
17168df0bcf0SPaul Dagnelie 	    &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
17178df0bcf0SPaul Dagnelie 	    os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1718c717a561Smaybee 
1719c717a561Smaybee 	/*
172014843421SMatthew Ahrens 	 * Sync special dnodes - the parent IO for the sync is the root block
1721c717a561Smaybee 	 */
1722744947dcSTom Erickson 	DMU_META_DNODE(os)->dn_zio = zio;
1723744947dcSTom Erickson 	dnode_sync(DMU_META_DNODE(os), tx);
1724c717a561Smaybee 
172514843421SMatthew Ahrens 	os->os_phys->os_flags = os->os_flags;
172614843421SMatthew Ahrens 
1727744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) &&
1728744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1729744947dcSTom Erickson 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1730744947dcSTom Erickson 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1731744947dcSTom Erickson 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1732744947dcSTom Erickson 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
173314843421SMatthew Ahrens 	}
173414843421SMatthew Ahrens 
1735f67950b2SNasf-Fan 	if (DMU_PROJECTUSED_DNODE(os) &&
1736f67950b2SNasf-Fan 	    DMU_PROJECTUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1737f67950b2SNasf-Fan 		DMU_PROJECTUSED_DNODE(os)->dn_zio = zio;
1738f67950b2SNasf-Fan 		dnode_sync(DMU_PROJECTUSED_DNODE(os), tx);
1739f67950b2SNasf-Fan 	}
1740f67950b2SNasf-Fan 
1741c717a561Smaybee 	txgoff = tx->tx_txg & TXG_MASK;
1742fa9e4066Sahrens 
1743eb633035STom Caputi 	if (dmu_objset_userused_enabled(os) &&
1744eb633035STom Caputi 	    (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
174514843421SMatthew Ahrens 		/*
174614843421SMatthew Ahrens 		 * We must create the list here because it uses the
174794c2d0ebSMatthew Ahrens 		 * dn_dirty_link[] of this txg.  But it may already
174894c2d0ebSMatthew Ahrens 		 * exist because we call dsl_dataset_sync() twice per txg.
174914843421SMatthew Ahrens 		 */
175094c2d0ebSMatthew Ahrens 		if (os->os_synced_dnodes == NULL) {
175194c2d0ebSMatthew Ahrens 			os->os_synced_dnodes =
175294c2d0ebSMatthew Ahrens 			    multilist_create(sizeof (dnode_t),
175394c2d0ebSMatthew Ahrens 			    offsetof(dnode_t, dn_dirty_link[txgoff]),
175494c2d0ebSMatthew Ahrens 			    dnode_multilist_index_func);
175594c2d0ebSMatthew Ahrens 		} else {
175694c2d0ebSMatthew Ahrens 			ASSERT3U(os->os_synced_dnodes->ml_offset, ==,
175794c2d0ebSMatthew Ahrens 			    offsetof(dnode_t, dn_dirty_link[txgoff]));
175894c2d0ebSMatthew Ahrens 		}
175914843421SMatthew Ahrens 	}
176014843421SMatthew Ahrens 
176194c2d0ebSMatthew Ahrens 	for (int i = 0;
176294c2d0ebSMatthew Ahrens 	    i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) {
176394c2d0ebSMatthew Ahrens 		sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
176494c2d0ebSMatthew Ahrens 		sda->sda_list = os->os_dirty_dnodes[txgoff];
176594c2d0ebSMatthew Ahrens 		sda->sda_sublist_idx = i;
176694c2d0ebSMatthew Ahrens 		sda->sda_tx = tx;
176794c2d0ebSMatthew Ahrens 		(void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
176894c2d0ebSMatthew Ahrens 		    sync_dnodes_task, sda, 0);
176994c2d0ebSMatthew Ahrens 		/* callback frees sda */
177094c2d0ebSMatthew Ahrens 	}
177194c2d0ebSMatthew Ahrens 	taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
1772fa9e4066Sahrens 
1773744947dcSTom Erickson 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
177494c2d0ebSMatthew Ahrens 	while ((dr = list_head(list)) != NULL) {
17753b2aab18SMatthew Ahrens 		ASSERT0(dr->dr_dbuf->db_level);
1776c717a561Smaybee 		list_remove(list, dr);
1777c717a561Smaybee 		if (dr->dr_zio)
1778c717a561Smaybee 			zio_nowait(dr->dr_zio);
1779c717a561Smaybee 	}
1780af346df5SNed Bass 
1781af346df5SNed Bass 	/* Enable dnode backfill if enough objects have been freed. */
1782af346df5SNed Bass 	if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1783af346df5SNed Bass 		os->os_rescan_dnodes = B_TRUE;
1784af346df5SNed Bass 		os->os_freed_dnodes = 0;
1785af346df5SNed Bass 	}
1786af346df5SNed Bass 
1787c717a561Smaybee 	/*
1788c717a561Smaybee 	 * Free intent log blocks up to this tx.
1789c717a561Smaybee 	 */
1790c717a561Smaybee 	zil_sync(os->os_zil, tx);
1791088f3894Sahrens 	os->os_phys->os_zil_header = os->os_zil_header;
1792c717a561Smaybee 	zio_nowait(zio);
1793fa9e4066Sahrens }
1794fa9e4066Sahrens 
1795b24ab676SJeff Bonwick boolean_t
dmu_objset_is_dirty(objset_t * os,uint64_t txg)1796b24ab676SJeff Bonwick dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1797b24ab676SJeff Bonwick {
179894c2d0ebSMatthew Ahrens 	return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK]));
1799b24ab676SJeff Bonwick }
1800b24ab676SJeff Bonwick 
1801744947dcSTom Erickson static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
180214843421SMatthew Ahrens 
180314843421SMatthew Ahrens void
dmu_objset_register_type(dmu_objset_type_t ost,objset_used_cb_t * cb)180414843421SMatthew Ahrens dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
180514843421SMatthew Ahrens {
180614843421SMatthew Ahrens 	used_cbs[ost] = cb;
180714843421SMatthew Ahrens }
180814843421SMatthew Ahrens 
180914843421SMatthew Ahrens boolean_t
dmu_objset_userused_enabled(objset_t * os)1810503ad85cSMatthew Ahrens dmu_objset_userused_enabled(objset_t *os)
181114843421SMatthew Ahrens {
181214843421SMatthew Ahrens 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1813744947dcSTom Erickson 	    used_cbs[os->os_phys->os_type] != NULL &&
1814744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os) != NULL);
181514843421SMatthew Ahrens }
181614843421SMatthew Ahrens 
1817f67950b2SNasf-Fan boolean_t
dmu_objset_userobjused_enabled(objset_t * os)1818f67950b2SNasf-Fan dmu_objset_userobjused_enabled(objset_t *os)
1819f67950b2SNasf-Fan {
1820f67950b2SNasf-Fan 	return (dmu_objset_userused_enabled(os) &&
1821f67950b2SNasf-Fan 	    spa_feature_is_enabled(os->os_spa, SPA_FEATURE_USEROBJ_ACCOUNTING));
1822f67950b2SNasf-Fan }
1823f67950b2SNasf-Fan 
1824f67950b2SNasf-Fan boolean_t
dmu_objset_projectquota_enabled(objset_t * os)1825f67950b2SNasf-Fan dmu_objset_projectquota_enabled(objset_t *os)
1826f67950b2SNasf-Fan {
1827f67950b2SNasf-Fan 	return (used_cbs[os->os_phys->os_type] != NULL &&
1828f67950b2SNasf-Fan 	    DMU_PROJECTUSED_DNODE(os) != NULL &&
1829f67950b2SNasf-Fan 	    spa_feature_is_enabled(os->os_spa, SPA_FEATURE_PROJECT_QUOTA));
1830f67950b2SNasf-Fan }
1831f67950b2SNasf-Fan 
18328b70546bSMatthew Ahrens typedef struct userquota_node {
1833f67950b2SNasf-Fan 	/* must be in the first field, see userquota_update_cache() */
1834f67950b2SNasf-Fan 	char		uqn_id[20 + DMU_OBJACCT_PREFIX_LEN];
1835f67950b2SNasf-Fan 	int64_t		uqn_delta;
1836f67950b2SNasf-Fan 	avl_node_t	uqn_node;
18378b70546bSMatthew Ahrens } userquota_node_t;
18388b70546bSMatthew Ahrens 
18398b70546bSMatthew Ahrens typedef struct userquota_cache {
18408b70546bSMatthew Ahrens 	avl_tree_t uqc_user_deltas;
18418b70546bSMatthew Ahrens 	avl_tree_t uqc_group_deltas;
1842f67950b2SNasf-Fan 	avl_tree_t uqc_project_deltas;
18438b70546bSMatthew Ahrens } userquota_cache_t;
18448b70546bSMatthew Ahrens 
18458b70546bSMatthew Ahrens static int
userquota_compare(const void * l,const void * r)18468b70546bSMatthew Ahrens userquota_compare(const void *l, const void *r)
18478b70546bSMatthew Ahrens {
18488b70546bSMatthew Ahrens 	const userquota_node_t *luqn = l;
18498b70546bSMatthew Ahrens 	const userquota_node_t *ruqn = r;
1850f67950b2SNasf-Fan 	int rv;
18518b70546bSMatthew Ahrens 
1852f67950b2SNasf-Fan 	/*
1853f67950b2SNasf-Fan 	 * NB: can only access uqn_id because userquota_update_cache() doesn't
1854f67950b2SNasf-Fan 	 * pass in an entire userquota_node_t.
1855f67950b2SNasf-Fan 	 */
1856f67950b2SNasf-Fan 	rv = strcmp(luqn->uqn_id, ruqn->uqn_id);
1857f67950b2SNasf-Fan 
18584d7988d6SPaul Dagnelie 	return (TREE_ISIGN(rv));
18598b70546bSMatthew Ahrens }
18608b70546bSMatthew Ahrens 
18619966ca11SMatthew Ahrens static void
do_userquota_cacheflush(objset_t * os,userquota_cache_t * cache,dmu_tx_t * tx)18628b70546bSMatthew Ahrens do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
18638b70546bSMatthew Ahrens {
18648b70546bSMatthew Ahrens 	void *cookie;
18658b70546bSMatthew Ahrens 	userquota_node_t *uqn;
18668b70546bSMatthew Ahrens 
18678b70546bSMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
18688b70546bSMatthew Ahrens 
18698b70546bSMatthew Ahrens 	cookie = NULL;
18708b70546bSMatthew Ahrens 	while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
18718b70546bSMatthew Ahrens 	    &cookie)) != NULL) {
187294c2d0ebSMatthew Ahrens 		/*
187394c2d0ebSMatthew Ahrens 		 * os_userused_lock protects against concurrent calls to
187494c2d0ebSMatthew Ahrens 		 * zap_increment_int().  It's needed because zap_increment_int()
187594c2d0ebSMatthew Ahrens 		 * is not thread-safe (i.e. not atomic).
187694c2d0ebSMatthew Ahrens 		 */
187794c2d0ebSMatthew Ahrens 		mutex_enter(&os->os_userused_lock);
1878f67950b2SNasf-Fan 		VERIFY0(zap_increment(os, DMU_USERUSED_OBJECT,
18798b70546bSMatthew Ahrens 		    uqn->uqn_id, uqn->uqn_delta, tx));
188094c2d0ebSMatthew Ahrens 		mutex_exit(&os->os_userused_lock);
18818b70546bSMatthew Ahrens 		kmem_free(uqn, sizeof (*uqn));
18828b70546bSMatthew Ahrens 	}
18838b70546bSMatthew Ahrens 	avl_destroy(&cache->uqc_user_deltas);
18848b70546bSMatthew Ahrens 
18858b70546bSMatthew Ahrens 	cookie = NULL;
18868b70546bSMatthew Ahrens 	while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
18878b70546bSMatthew Ahrens 	    &cookie)) != NULL) {
188894c2d0ebSMatthew Ahrens 		mutex_enter(&os->os_userused_lock);
1889f67950b2SNasf-Fan 		VERIFY0(zap_increment(os, DMU_GROUPUSED_OBJECT,
18908b70546bSMatthew Ahrens 		    uqn->uqn_id, uqn->uqn_delta, tx));
189194c2d0ebSMatthew Ahrens 		mutex_exit(&os->os_userused_lock);
18928b70546bSMatthew Ahrens 		kmem_free(uqn, sizeof (*uqn));
18938b70546bSMatthew Ahrens 	}
18948b70546bSMatthew Ahrens 	avl_destroy(&cache->uqc_group_deltas);
1895f67950b2SNasf-Fan 
1896f67950b2SNasf-Fan 	if (dmu_objset_projectquota_enabled(os)) {
1897f67950b2SNasf-Fan 		cookie = NULL;
1898f67950b2SNasf-Fan 		while ((uqn = avl_destroy_nodes(&cache->uqc_project_deltas,
1899f67950b2SNasf-Fan 		    &cookie)) != NULL) {
1900f67950b2SNasf-Fan 			mutex_enter(&os->os_userused_lock);
1901f67950b2SNasf-Fan 			VERIFY0(zap_increment(os, DMU_PROJECTUSED_OBJECT,
1902f67950b2SNasf-Fan 			    uqn->uqn_id, uqn->uqn_delta, tx));
1903f67950b2SNasf-Fan 			mutex_exit(&os->os_userused_lock);
1904f67950b2SNasf-Fan 			kmem_free(uqn, sizeof (*uqn));
1905f67950b2SNasf-Fan 		}
1906f67950b2SNasf-Fan 		avl_destroy(&cache->uqc_project_deltas);
1907f67950b2SNasf-Fan 	}
19088b70546bSMatthew Ahrens }
19098b70546bSMatthew Ahrens 
19108b70546bSMatthew Ahrens static void
userquota_update_cache(avl_tree_t * avl,const char * id,int64_t delta)1911f67950b2SNasf-Fan userquota_update_cache(avl_tree_t *avl, const char *id, int64_t delta)
19128b70546bSMatthew Ahrens {
1913f67950b2SNasf-Fan 	userquota_node_t *uqn;
19148b70546bSMatthew Ahrens 	avl_index_t idx;
19158b70546bSMatthew Ahrens 
1916f67950b2SNasf-Fan 	ASSERT(strlen(id) < sizeof (uqn->uqn_id));
1917f67950b2SNasf-Fan 	/*
1918f67950b2SNasf-Fan 	 * Use id directly for searching because uqn_id is the first field of
1919f67950b2SNasf-Fan 	 * userquota_node_t and fields after uqn_id won't be accessed in
1920f67950b2SNasf-Fan 	 * avl_find().
1921f67950b2SNasf-Fan 	 */
1922f67950b2SNasf-Fan 	uqn = avl_find(avl, (const void *)id, &idx);
19238b70546bSMatthew Ahrens 	if (uqn == NULL) {
19248b70546bSMatthew Ahrens 		uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
1925f67950b2SNasf-Fan 		(void) strlcpy(uqn->uqn_id, id, sizeof (uqn->uqn_id));
19268b70546bSMatthew Ahrens 		avl_insert(avl, uqn, idx);
19278b70546bSMatthew Ahrens 	}
19288b70546bSMatthew Ahrens 	uqn->uqn_delta += delta;
19298b70546bSMatthew Ahrens }
19308b70546bSMatthew Ahrens 
19318b70546bSMatthew Ahrens static void
do_userquota_update(objset_t * os,userquota_cache_t * cache,uint64_t used,uint64_t flags,uint64_t user,uint64_t group,uint64_t project,boolean_t subtract)1932f67950b2SNasf-Fan do_userquota_update(objset_t *os, userquota_cache_t *cache, uint64_t used,
1933f67950b2SNasf-Fan     uint64_t flags, uint64_t user, uint64_t group, uint64_t project,
1934f67950b2SNasf-Fan     boolean_t subtract)
19359966ca11SMatthew Ahrens {
19360a586ceaSMark Shellenbaum 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
193754811da5SToomas Soome 		int64_t delta = DNODE_MIN_SIZE + used;
1938f67950b2SNasf-Fan 		char name[20];
1939f67950b2SNasf-Fan 
19409966ca11SMatthew Ahrens 		if (subtract)
19419966ca11SMatthew Ahrens 			delta = -delta;
19428b70546bSMatthew Ahrens 
1943f67950b2SNasf-Fan 		(void) sprintf(name, "%llx", (longlong_t)user);
1944f67950b2SNasf-Fan 		userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1945f67950b2SNasf-Fan 
1946f67950b2SNasf-Fan 		(void) sprintf(name, "%llx", (longlong_t)group);
1947f67950b2SNasf-Fan 		userquota_update_cache(&cache->uqc_group_deltas, name, delta);
1948f67950b2SNasf-Fan 
1949f67950b2SNasf-Fan 		if (dmu_objset_projectquota_enabled(os)) {
1950f67950b2SNasf-Fan 			(void) sprintf(name, "%llx", (longlong_t)project);
1951f67950b2SNasf-Fan 			userquota_update_cache(&cache->uqc_project_deltas,
1952f67950b2SNasf-Fan 			    name, delta);
1953f67950b2SNasf-Fan 		}
1954f67950b2SNasf-Fan 	}
1955f67950b2SNasf-Fan }
1956f67950b2SNasf-Fan 
1957f67950b2SNasf-Fan static void
do_userobjquota_update(objset_t * os,userquota_cache_t * cache,uint64_t flags,uint64_t user,uint64_t group,uint64_t project,boolean_t subtract)1958f67950b2SNasf-Fan do_userobjquota_update(objset_t *os, userquota_cache_t *cache, uint64_t flags,
1959f67950b2SNasf-Fan     uint64_t user, uint64_t group, uint64_t project, boolean_t subtract)
1960f67950b2SNasf-Fan {
1961f67950b2SNasf-Fan 	if (flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) {
1962f67950b2SNasf-Fan 		char name[20 + DMU_OBJACCT_PREFIX_LEN];
1963f67950b2SNasf-Fan 		int delta = subtract ? -1 : 1;
1964f67950b2SNasf-Fan 
1965f67950b2SNasf-Fan 		(void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1966f67950b2SNasf-Fan 		    (longlong_t)user);
1967f67950b2SNasf-Fan 		userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1968f67950b2SNasf-Fan 
1969f67950b2SNasf-Fan 		(void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1970f67950b2SNasf-Fan 		    (longlong_t)group);
1971f67950b2SNasf-Fan 		userquota_update_cache(&cache->uqc_group_deltas, name, delta);
1972f67950b2SNasf-Fan 
1973f67950b2SNasf-Fan 		if (dmu_objset_projectquota_enabled(os)) {
1974f67950b2SNasf-Fan 			(void) snprintf(name, sizeof (name),
1975f67950b2SNasf-Fan 			    DMU_OBJACCT_PREFIX "%llx", (longlong_t)project);
1976f67950b2SNasf-Fan 			userquota_update_cache(&cache->uqc_project_deltas,
1977f67950b2SNasf-Fan 			    name, delta);
1978f67950b2SNasf-Fan 		}
19799966ca11SMatthew Ahrens 	}
19809966ca11SMatthew Ahrens }
19819966ca11SMatthew Ahrens 
198294c2d0ebSMatthew Ahrens typedef struct userquota_updates_arg {
198394c2d0ebSMatthew Ahrens 	objset_t *uua_os;
198494c2d0ebSMatthew Ahrens 	int uua_sublist_idx;
198594c2d0ebSMatthew Ahrens 	dmu_tx_t *uua_tx;
198694c2d0ebSMatthew Ahrens } userquota_updates_arg_t;
198794c2d0ebSMatthew Ahrens 
198894c2d0ebSMatthew Ahrens static void
userquota_updates_task(void * arg)198994c2d0ebSMatthew Ahrens userquota_updates_task(void *arg)
199014843421SMatthew Ahrens {
199194c2d0ebSMatthew Ahrens 	userquota_updates_arg_t *uua = arg;
199294c2d0ebSMatthew Ahrens 	objset_t *os = uua->uua_os;
199394c2d0ebSMatthew Ahrens 	dmu_tx_t *tx = uua->uua_tx;
199414843421SMatthew Ahrens 	dnode_t *dn;
19958b70546bSMatthew Ahrens 	userquota_cache_t cache = { 0 };
199614843421SMatthew Ahrens 
199794c2d0ebSMatthew Ahrens 	multilist_sublist_t *list =
199894c2d0ebSMatthew Ahrens 	    multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx);
199914843421SMatthew Ahrens 
200094c2d0ebSMatthew Ahrens 	ASSERT(multilist_sublist_head(list) == NULL ||
200194c2d0ebSMatthew Ahrens 	    dmu_objset_userused_enabled(os));
20028b70546bSMatthew Ahrens 	avl_create(&cache.uqc_user_deltas, userquota_compare,
20038b70546bSMatthew Ahrens 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
20048b70546bSMatthew Ahrens 	avl_create(&cache.uqc_group_deltas, userquota_compare,
20058b70546bSMatthew Ahrens 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
2006f67950b2SNasf-Fan 	if (dmu_objset_projectquota_enabled(os))
2007f67950b2SNasf-Fan 		avl_create(&cache.uqc_project_deltas, userquota_compare,
2008f67950b2SNasf-Fan 		    sizeof (userquota_node_t), offsetof(userquota_node_t,
2009f67950b2SNasf-Fan 		    uqn_node));
20108b70546bSMatthew Ahrens 
201194c2d0ebSMatthew Ahrens 	while ((dn = multilist_sublist_head(list)) != NULL) {
20121d8ccc7bSMark Shellenbaum 		int flags;
201314843421SMatthew Ahrens 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
201414843421SMatthew Ahrens 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
201514843421SMatthew Ahrens 		    dn->dn_phys->dn_flags &
201614843421SMatthew Ahrens 		    DNODE_FLAG_USERUSED_ACCOUNTED);
201714843421SMatthew Ahrens 
20181d8ccc7bSMark Shellenbaum 		flags = dn->dn_id_flags;
20191d8ccc7bSMark Shellenbaum 		ASSERT(flags);
20201d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST)  {
2021f67950b2SNasf-Fan 			do_userquota_update(os, &cache, dn->dn_oldused,
2022f67950b2SNasf-Fan 			    dn->dn_oldflags, dn->dn_olduid, dn->dn_oldgid,
2023f67950b2SNasf-Fan 			    dn->dn_oldprojid, B_TRUE);
2024f67950b2SNasf-Fan 			do_userobjquota_update(os, &cache, dn->dn_oldflags,
2025f67950b2SNasf-Fan 			    dn->dn_olduid, dn->dn_oldgid,
2026f67950b2SNasf-Fan 			    dn->dn_oldprojid, B_TRUE);
20270a586ceaSMark Shellenbaum 		}
20281d8ccc7bSMark Shellenbaum 		if (flags & DN_ID_NEW_EXIST) {
2029f67950b2SNasf-Fan 			do_userquota_update(os, &cache,
2030f67950b2SNasf-Fan 			    DN_USED_BYTES(dn->dn_phys), dn->dn_phys->dn_flags,
2031f67950b2SNasf-Fan 			    dn->dn_newuid, dn->dn_newgid,
2032f67950b2SNasf-Fan 			    dn->dn_newprojid, B_FALSE);
2033f67950b2SNasf-Fan 			do_userobjquota_update(os, &cache,
2034f67950b2SNasf-Fan 			    dn->dn_phys->dn_flags, dn->dn_newuid, dn->dn_newgid,
2035f67950b2SNasf-Fan 			    dn->dn_newprojid, B_FALSE);
20360a586ceaSMark Shellenbaum 		}
20370a586ceaSMark Shellenbaum 
20381d8ccc7bSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
20390a586ceaSMark Shellenbaum 		dn->dn_oldused = 0;
20400a586ceaSMark Shellenbaum 		dn->dn_oldflags = 0;
20410a586ceaSMark Shellenbaum 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
20420a586ceaSMark Shellenbaum 			dn->dn_olduid = dn->dn_newuid;
20430a586ceaSMark Shellenbaum 			dn->dn_oldgid = dn->dn_newgid;
2044f67950b2SNasf-Fan 			dn->dn_oldprojid = dn->dn_newprojid;
20450a586ceaSMark Shellenbaum 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
20460a586ceaSMark Shellenbaum 			if (dn->dn_bonuslen == 0)
20470a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
20480a586ceaSMark Shellenbaum 			else
20490a586ceaSMark Shellenbaum 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
20500a586ceaSMark Shellenbaum 		}
205128d97a71SMark Shellenbaum 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
2052aa02ea01STom Caputi 		if (dn->dn_dirty_txg == spa_syncing_txg(os->os_spa))
2053aa02ea01STom Caputi 			dn->dn_dirty_txg = 0;
205414843421SMatthew Ahrens 		mutex_exit(&dn->dn_mtx);
205514843421SMatthew Ahrens 
205694c2d0ebSMatthew Ahrens 		multilist_sublist_remove(list, dn);
205794c2d0ebSMatthew Ahrens 		dnode_rele(dn, os->os_synced_dnodes);
205814843421SMatthew Ahrens 	}
20598b70546bSMatthew Ahrens 	do_userquota_cacheflush(os, &cache, tx);
206094c2d0ebSMatthew Ahrens 	multilist_sublist_unlock(list);
206194c2d0ebSMatthew Ahrens 	kmem_free(uua, sizeof (*uua));
206294c2d0ebSMatthew Ahrens }
206394c2d0ebSMatthew Ahrens 
206494c2d0ebSMatthew Ahrens void
dmu_objset_do_userquota_updates(objset_t * os,dmu_tx_t * tx)206594c2d0ebSMatthew Ahrens dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
206694c2d0ebSMatthew Ahrens {
206794c2d0ebSMatthew Ahrens 	if (!dmu_objset_userused_enabled(os))
206894c2d0ebSMatthew Ahrens 		return;
206994c2d0ebSMatthew Ahrens 
2070f67950b2SNasf-Fan 	/*
2071f67950b2SNasf-Fan 	 * If this is a raw receive just return and handle accounting
2072f67950b2SNasf-Fan 	 * later when we have the keys loaded. We also don't do user
2073f67950b2SNasf-Fan 	 * accounting during claiming since the datasets are not owned
2074f67950b2SNasf-Fan 	 * for the duration of claiming and this txg should only be
2075f67950b2SNasf-Fan 	 * used for recovery.
2076f67950b2SNasf-Fan 	 */
2077eb633035STom Caputi 	if (os->os_encrypted && dmu_objset_is_receiving(os))
2078eb633035STom Caputi 		return;
2079eb633035STom Caputi 
20808bf394f1STom Caputi 	if (tx->tx_txg <= os->os_spa->spa_claim_max_txg)
20818bf394f1STom Caputi 		return;
20828bf394f1STom Caputi 
2083f67950b2SNasf-Fan 	/* Allocate the user/group/project used objects if necessary. */
208494c2d0ebSMatthew Ahrens 	if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
208594c2d0ebSMatthew Ahrens 		VERIFY0(zap_create_claim(os,
208694c2d0ebSMatthew Ahrens 		    DMU_USERUSED_OBJECT,
208794c2d0ebSMatthew Ahrens 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
208894c2d0ebSMatthew Ahrens 		VERIFY0(zap_create_claim(os,
208994c2d0ebSMatthew Ahrens 		    DMU_GROUPUSED_OBJECT,
209094c2d0ebSMatthew Ahrens 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
209194c2d0ebSMatthew Ahrens 	}
209294c2d0ebSMatthew Ahrens 
2093f67950b2SNasf-Fan 	if (dmu_objset_projectquota_enabled(os) &&
2094f67950b2SNasf-Fan 	    DMU_PROJECTUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
2095f67950b2SNasf-Fan 		VERIFY0(zap_create_claim(os, DMU_PROJECTUSED_OBJECT,
2096f67950b2SNasf-Fan 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2097f67950b2SNasf-Fan 	}
2098f67950b2SNasf-Fan 
209994c2d0ebSMatthew Ahrens 	for (int i = 0;
210094c2d0ebSMatthew Ahrens 	    i < multilist_get_num_sublists(os->os_synced_dnodes); i++) {
210194c2d0ebSMatthew Ahrens 		userquota_updates_arg_t *uua =
210294c2d0ebSMatthew Ahrens 		    kmem_alloc(sizeof (*uua), KM_SLEEP);
210394c2d0ebSMatthew Ahrens 		uua->uua_os = os;
210494c2d0ebSMatthew Ahrens 		uua->uua_sublist_idx = i;
210594c2d0ebSMatthew Ahrens 		uua->uua_tx = tx;
210694c2d0ebSMatthew Ahrens 		/* note: caller does taskq_wait() */
210794c2d0ebSMatthew Ahrens 		(void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
210894c2d0ebSMatthew Ahrens 		    userquota_updates_task, uua, 0);
210994c2d0ebSMatthew Ahrens 		/* callback frees uua */
211094c2d0ebSMatthew Ahrens 	}
211114843421SMatthew Ahrens }
211214843421SMatthew Ahrens 
211306e0070dSMark Shellenbaum /*
211406e0070dSMark Shellenbaum  * Returns a pointer to data to find uid/gid from
211506e0070dSMark Shellenbaum  *
211606e0070dSMark Shellenbaum  * If a dirty record for transaction group that is syncing can't
211706e0070dSMark Shellenbaum  * be found then NULL is returned.  In the NULL case it is assumed
211806e0070dSMark Shellenbaum  * the uid/gid aren't changing.
211906e0070dSMark Shellenbaum  */
212006e0070dSMark Shellenbaum static void *
dmu_objset_userquota_find_data(dmu_buf_impl_t * db,dmu_tx_t * tx)212106e0070dSMark Shellenbaum dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
212206e0070dSMark Shellenbaum {
212306e0070dSMark Shellenbaum 	dbuf_dirty_record_t *dr, **drp;
212406e0070dSMark Shellenbaum 	void *data;
212506e0070dSMark Shellenbaum 
212606e0070dSMark Shellenbaum 	if (db->db_dirtycnt == 0)
212706e0070dSMark Shellenbaum 		return (db->db.db_data);  /* Nothing is changing */
212806e0070dSMark Shellenbaum 
212906e0070dSMark Shellenbaum 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
213006e0070dSMark Shellenbaum 		if (dr->dr_txg == tx->tx_txg)
213106e0070dSMark Shellenbaum 			break;
213206e0070dSMark Shellenbaum 
2133744947dcSTom Erickson 	if (dr == NULL) {
213406e0070dSMark Shellenbaum 		data = NULL;
2135744947dcSTom Erickson 	} else {
2136744947dcSTom Erickson 		dnode_t *dn;
2137744947dcSTom Erickson 
2138744947dcSTom Erickson 		DB_DNODE_ENTER(dr->dr_dbuf);
2139744947dcSTom Erickson 		dn = DB_DNODE(dr->dr_dbuf);
2140744947dcSTom Erickson 
2141744947dcSTom Erickson 		if (dn->dn_bonuslen == 0 &&
2142744947dcSTom Erickson 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
2143744947dcSTom Erickson 			data = dr->dt.dl.dr_data->b_data;
2144744947dcSTom Erickson 		else
2145744947dcSTom Erickson 			data = dr->dt.dl.dr_data;
2146744947dcSTom Erickson 
2147744947dcSTom Erickson 		DB_DNODE_EXIT(dr->dr_dbuf);
2148744947dcSTom Erickson 	}
2149744947dcSTom Erickson 
215006e0070dSMark Shellenbaum 	return (data);
215106e0070dSMark Shellenbaum }
215206e0070dSMark Shellenbaum 
21530a586ceaSMark Shellenbaum void
dmu_objset_userquota_get_ids(dnode_t * dn,boolean_t before,dmu_tx_t * tx)215406e0070dSMark Shellenbaum dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
21550a586ceaSMark Shellenbaum {
21560a586ceaSMark Shellenbaum 	objset_t *os = dn->dn_objset;
21570a586ceaSMark Shellenbaum 	void *data = NULL;
215806e0070dSMark Shellenbaum 	dmu_buf_impl_t *db = NULL;
2159d5285caeSGeorge Wilson 	uint64_t *user = NULL;
2160d5285caeSGeorge Wilson 	uint64_t *group = NULL;
2161f67950b2SNasf-Fan 	uint64_t *project = NULL;
21620a586ceaSMark Shellenbaum 	int flags = dn->dn_id_flags;
21630a586ceaSMark Shellenbaum 	int error;
216406e0070dSMark Shellenbaum 	boolean_t have_spill = B_FALSE;
21650a586ceaSMark Shellenbaum 
21660a586ceaSMark Shellenbaum 	if (!dmu_objset_userused_enabled(dn->dn_objset))
21670a586ceaSMark Shellenbaum 		return;
21680a586ceaSMark Shellenbaum 
2169eb633035STom Caputi 	/*
2170eb633035STom Caputi 	 * Raw receives introduce a problem with user accounting. Raw
2171eb633035STom Caputi 	 * receives cannot update the user accounting info because the
2172eb633035STom Caputi 	 * user ids and the sizes are encrypted. To guarantee that we
2173eb633035STom Caputi 	 * never end up with bad user accounting, we simply disable it
2174eb633035STom Caputi 	 * during raw receives. We also disable this for normal receives
2175eb633035STom Caputi 	 * so that an incremental raw receive may be done on top of an
2176eb633035STom Caputi 	 * existing non-raw receive.
2177eb633035STom Caputi 	 */
2178eb633035STom Caputi 	if (os->os_encrypted && dmu_objset_is_receiving(os))
2179eb633035STom Caputi 		return;
2180eb633035STom Caputi 
21810a586ceaSMark Shellenbaum 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
21820a586ceaSMark Shellenbaum 	    DN_ID_CHKED_SPILL)))
21830a586ceaSMark Shellenbaum 		return;
21840a586ceaSMark Shellenbaum 
21850a586ceaSMark Shellenbaum 	if (before && dn->dn_bonuslen != 0)
21860a586ceaSMark Shellenbaum 		data = DN_BONUS(dn->dn_phys);
218706e0070dSMark Shellenbaum 	else if (!before && dn->dn_bonuslen != 0) {
218806e0070dSMark Shellenbaum 		if (dn->dn_bonus) {
218906e0070dSMark Shellenbaum 			db = dn->dn_bonus;
219006e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
219106e0070dSMark Shellenbaum 			data = dmu_objset_userquota_find_data(db, tx);
219206e0070dSMark Shellenbaum 		} else {
219306e0070dSMark Shellenbaum 			data = DN_BONUS(dn->dn_phys);
219406e0070dSMark Shellenbaum 		}
219506e0070dSMark Shellenbaum 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
21960a586ceaSMark Shellenbaum 			int rf = 0;
21970a586ceaSMark Shellenbaum 
21980a586ceaSMark Shellenbaum 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
21990a586ceaSMark Shellenbaum 				rf |= DB_RF_HAVESTRUCT;
22001d8ccc7bSMark Shellenbaum 			error = dmu_spill_hold_by_dnode(dn,
22011d8ccc7bSMark Shellenbaum 			    rf | DB_RF_MUST_SUCCEED,
220206e0070dSMark Shellenbaum 			    FTAG, (dmu_buf_t **)&db);
22030a586ceaSMark Shellenbaum 			ASSERT(error == 0);
220406e0070dSMark Shellenbaum 			mutex_enter(&db->db_mtx);
220506e0070dSMark Shellenbaum 			data = (before) ? db->db.db_data :
220606e0070dSMark Shellenbaum 			    dmu_objset_userquota_find_data(db, tx);
220706e0070dSMark Shellenbaum 			have_spill = B_TRUE;
22080a586ceaSMark Shellenbaum 	} else {
22090a586ceaSMark Shellenbaum 		mutex_enter(&dn->dn_mtx);
22100a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
22110a586ceaSMark Shellenbaum 		mutex_exit(&dn->dn_mtx);
22120a586ceaSMark Shellenbaum 		return;
22130a586ceaSMark Shellenbaum 	}
22140a586ceaSMark Shellenbaum 
22150a586ceaSMark Shellenbaum 	if (before) {
221606e0070dSMark Shellenbaum 		ASSERT(data);
22170a586ceaSMark Shellenbaum 		user = &dn->dn_olduid;
22180a586ceaSMark Shellenbaum 		group = &dn->dn_oldgid;
2219f67950b2SNasf-Fan 		project = &dn->dn_oldprojid;
222006e0070dSMark Shellenbaum 	} else if (data) {
22210a586ceaSMark Shellenbaum 		user = &dn->dn_newuid;
22220a586ceaSMark Shellenbaum 		group = &dn->dn_newgid;
2223f67950b2SNasf-Fan 		project = &dn->dn_newprojid;
22240a586ceaSMark Shellenbaum 	}
22250a586ceaSMark Shellenbaum 
222606e0070dSMark Shellenbaum 	/*
222706e0070dSMark Shellenbaum 	 * Must always call the callback in case the object
222806e0070dSMark Shellenbaum 	 * type has changed and that type isn't an object type to track
222906e0070dSMark Shellenbaum 	 */
22300a586ceaSMark Shellenbaum 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
2231f67950b2SNasf-Fan 	    user, group, project);
22320a586ceaSMark Shellenbaum 
223306e0070dSMark Shellenbaum 	/*
223406e0070dSMark Shellenbaum 	 * Preserve existing uid/gid when the callback can't determine
223506e0070dSMark Shellenbaum 	 * what the new uid/gid are and the callback returned EEXIST.
223606e0070dSMark Shellenbaum 	 * The EEXIST error tells us to just use the existing uid/gid.
223706e0070dSMark Shellenbaum 	 * If we don't know what the old values are then just assign
223806e0070dSMark Shellenbaum 	 * them to 0, since that is a new file  being created.
223906e0070dSMark Shellenbaum 	 */
224006e0070dSMark Shellenbaum 	if (!before && data == NULL && error == EEXIST) {
224106e0070dSMark Shellenbaum 		if (flags & DN_ID_OLD_EXIST) {
224206e0070dSMark Shellenbaum 			dn->dn_newuid = dn->dn_olduid;
224306e0070dSMark Shellenbaum 			dn->dn_newgid = dn->dn_oldgid;
2244cc37296fSAndy Fiddaman 			dn->dn_newprojid = dn->dn_oldprojid;
224506e0070dSMark Shellenbaum 		} else {
224606e0070dSMark Shellenbaum 			dn->dn_newuid = 0;
224706e0070dSMark Shellenbaum 			dn->dn_newgid = 0;
2248f67950b2SNasf-Fan 			dn->dn_newprojid = ZFS_DEFAULT_PROJID;
224906e0070dSMark Shellenbaum 		}
225006e0070dSMark Shellenbaum 		error = 0;
225106e0070dSMark Shellenbaum 	}
225206e0070dSMark Shellenbaum 
225306e0070dSMark Shellenbaum 	if (db)
225406e0070dSMark Shellenbaum 		mutex_exit(&db->db_mtx);
225506e0070dSMark Shellenbaum 
22560a586ceaSMark Shellenbaum 	mutex_enter(&dn->dn_mtx);
22570a586ceaSMark Shellenbaum 	if (error == 0 && before)
22580a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
22590a586ceaSMark Shellenbaum 	if (error == 0 && !before)
22600a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
22610a586ceaSMark Shellenbaum 
226206e0070dSMark Shellenbaum 	if (have_spill) {
22630a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
22640a586ceaSMark Shellenbaum 	} else {
22650a586ceaSMark Shellenbaum 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
22660a586ceaSMark Shellenbaum 	}
22670a586ceaSMark Shellenbaum 	mutex_exit(&dn->dn_mtx);
226806e0070dSMark Shellenbaum 	if (have_spill)
226906e0070dSMark Shellenbaum 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
22700a586ceaSMark Shellenbaum }
22710a586ceaSMark Shellenbaum 
227214843421SMatthew Ahrens boolean_t
dmu_objset_userspace_present(objset_t * os)227314843421SMatthew Ahrens dmu_objset_userspace_present(objset_t *os)
227414843421SMatthew Ahrens {
2275503ad85cSMatthew Ahrens 	return (os->os_phys->os_flags &
227614843421SMatthew Ahrens 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
227714843421SMatthew Ahrens }
227814843421SMatthew Ahrens 
2279f67950b2SNasf-Fan boolean_t
dmu_objset_userobjspace_present(objset_t * os)2280f67950b2SNasf-Fan dmu_objset_userobjspace_present(objset_t *os)
2281f67950b2SNasf-Fan {
2282f67950b2SNasf-Fan 	return (os->os_phys->os_flags &
2283f67950b2SNasf-Fan 	    OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE);
2284f67950b2SNasf-Fan }
2285f67950b2SNasf-Fan 
2286f67950b2SNasf-Fan boolean_t
dmu_objset_projectquota_present(objset_t * os)2287f67950b2SNasf-Fan dmu_objset_projectquota_present(objset_t *os)
2288f67950b2SNasf-Fan {
2289f67950b2SNasf-Fan 	return (os->os_phys->os_flags &
2290f67950b2SNasf-Fan 	    OBJSET_FLAG_PROJECTQUOTA_COMPLETE);
2291f67950b2SNasf-Fan }
2292f67950b2SNasf-Fan 
2293f67950b2SNasf-Fan static int
dmu_objset_space_upgrade(objset_t * os)2294f67950b2SNasf-Fan dmu_objset_space_upgrade(objset_t *os)
229514843421SMatthew Ahrens {
229614843421SMatthew Ahrens 	uint64_t obj;
229714843421SMatthew Ahrens 	int err = 0;
229814843421SMatthew Ahrens 
229914843421SMatthew Ahrens 	/*
230014843421SMatthew Ahrens 	 * We simply need to mark every object dirty, so that it will be
230114843421SMatthew Ahrens 	 * synced out and now accounted.  If this is called
230214843421SMatthew Ahrens 	 * concurrently, or if we already did some work before crashing,
230314843421SMatthew Ahrens 	 * that's fine, since we track each object's accounted state
230414843421SMatthew Ahrens 	 * independently.
230514843421SMatthew Ahrens 	 */
230614843421SMatthew Ahrens 
230714843421SMatthew Ahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
230868857716SLin Ling 		dmu_tx_t *tx;
230914843421SMatthew Ahrens 		dmu_buf_t *db;
231014843421SMatthew Ahrens 		int objerr;
231114843421SMatthew Ahrens 
2312f67950b2SNasf-Fan 		mutex_enter(&os->os_upgrade_lock);
2313f67950b2SNasf-Fan 		if (os->os_upgrade_exit)
2314f67950b2SNasf-Fan 			err = SET_ERROR(EINTR);
2315f67950b2SNasf-Fan 		mutex_exit(&os->os_upgrade_lock);
2316f67950b2SNasf-Fan 		if (err != 0)
2317f67950b2SNasf-Fan 			return (err);
2318f67950b2SNasf-Fan 
2319f67950b2SNasf-Fan 		/*
2320f67950b2SNasf-Fan 		 * The following is only valid on Linux since we cannot send
2321f67950b2SNasf-Fan 		 * a signal to a kernel thread on illumos (because we have no
2322f67950b2SNasf-Fan 		 * lwp and never return to user-land).
2323f67950b2SNasf-Fan 		 *
2324f67950b2SNasf-Fan 		 * if (issig(JUSTLOOKING) && issig(FORREAL))
2325f67950b2SNasf-Fan 		 *    return (SET_ERROR(EINTR));
2326f67950b2SNasf-Fan 		 */
232714843421SMatthew Ahrens 
232814843421SMatthew Ahrens 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
23293b2aab18SMatthew Ahrens 		if (objerr != 0)
233014843421SMatthew Ahrens 			continue;
233168857716SLin Ling 		tx = dmu_tx_create(os);
233214843421SMatthew Ahrens 		dmu_tx_hold_bonus(tx, obj);
233314843421SMatthew Ahrens 		objerr = dmu_tx_assign(tx, TXG_WAIT);
23343b2aab18SMatthew Ahrens 		if (objerr != 0) {
233579db7483SGvozden Neskovic 			dmu_buf_rele(db, FTAG);
233614843421SMatthew Ahrens 			dmu_tx_abort(tx);
233714843421SMatthew Ahrens 			continue;
233814843421SMatthew Ahrens 		}
233914843421SMatthew Ahrens 		dmu_buf_will_dirty(db, tx);
234014843421SMatthew Ahrens 		dmu_buf_rele(db, FTAG);
234114843421SMatthew Ahrens 		dmu_tx_commit(tx);
234214843421SMatthew Ahrens 	}
2343f67950b2SNasf-Fan 	return (0);
2344f67950b2SNasf-Fan }
2345f67950b2SNasf-Fan 
2346f67950b2SNasf-Fan int
dmu_objset_userspace_upgrade(objset_t * os)2347f67950b2SNasf-Fan dmu_objset_userspace_upgrade(objset_t *os)
2348f67950b2SNasf-Fan {
2349f67950b2SNasf-Fan 	int err = 0;
2350f67950b2SNasf-Fan 
2351f67950b2SNasf-Fan 	if (dmu_objset_userspace_present(os))
2352f67950b2SNasf-Fan 		return (0);
2353f67950b2SNasf-Fan 	if (dmu_objset_is_snapshot(os))
2354f67950b2SNasf-Fan 		return (SET_ERROR(EINVAL));
2355f67950b2SNasf-Fan 	if (!dmu_objset_userused_enabled(os))
2356f67950b2SNasf-Fan 		return (SET_ERROR(ENOTSUP));
2357f67950b2SNasf-Fan 
2358f67950b2SNasf-Fan 	err = dmu_objset_space_upgrade(os);
2359f67950b2SNasf-Fan 	if (err)
2360f67950b2SNasf-Fan 		return (err);
236114843421SMatthew Ahrens 
2362503ad85cSMatthew Ahrens 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
236314843421SMatthew Ahrens 	txg_wait_synced(dmu_objset_pool(os), 0);
236414843421SMatthew Ahrens 	return (0);
236514843421SMatthew Ahrens }
236614843421SMatthew Ahrens 
2367f67950b2SNasf-Fan static int
dmu_objset_id_quota_upgrade_cb(objset_t * os)2368f67950b2SNasf-Fan dmu_objset_id_quota_upgrade_cb(objset_t *os)
2369f67950b2SNasf-Fan {
2370f67950b2SNasf-Fan 	int err = 0;
2371f67950b2SNasf-Fan 
2372f67950b2SNasf-Fan 	if (dmu_objset_userobjspace_present(os) &&
2373f67950b2SNasf-Fan 	    dmu_objset_projectquota_present(os))
2374f67950b2SNasf-Fan 		return (0);
2375f67950b2SNasf-Fan 	if (dmu_objset_is_snapshot(os))
2376f67950b2SNasf-Fan 		return (SET_ERROR(EINVAL));
2377f67950b2SNasf-Fan 	if (!dmu_objset_userobjused_enabled(os))
2378f67950b2SNasf-Fan 		return (SET_ERROR(ENOTSUP));
2379f67950b2SNasf-Fan 	if (!dmu_objset_projectquota_enabled(os) &&
2380f67950b2SNasf-Fan 	    dmu_objset_userobjspace_present(os))
2381f67950b2SNasf-Fan 		return (SET_ERROR(ENOTSUP));
2382f67950b2SNasf-Fan 
2383f67950b2SNasf-Fan 	dmu_objset_ds(os)->ds_feature_activation_needed[
2384f67950b2SNasf-Fan 	    SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE;
2385f67950b2SNasf-Fan 	if (dmu_objset_projectquota_enabled(os))
2386f67950b2SNasf-Fan 		dmu_objset_ds(os)->ds_feature_activation_needed[
2387f67950b2SNasf-Fan 		    SPA_FEATURE_PROJECT_QUOTA] = B_TRUE;
2388f67950b2SNasf-Fan 
2389f67950b2SNasf-Fan 	err = dmu_objset_space_upgrade(os);
2390f67950b2SNasf-Fan 	if (err)
2391f67950b2SNasf-Fan 		return (err);
2392f67950b2SNasf-Fan 
2393f67950b2SNasf-Fan 	os->os_flags |= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
2394f67950b2SNasf-Fan 	if (dmu_objset_projectquota_enabled(os))
2395f67950b2SNasf-Fan 		os->os_flags |= OBJSET_FLAG_PROJECTQUOTA_COMPLETE;
2396f67950b2SNasf-Fan 
2397f67950b2SNasf-Fan 	txg_wait_synced(dmu_objset_pool(os), 0);
2398f67950b2SNasf-Fan 	return (0);
2399f67950b2SNasf-Fan }
2400f67950b2SNasf-Fan 
2401f67950b2SNasf-Fan void
dmu_objset_id_quota_upgrade(objset_t * os)2402f67950b2SNasf-Fan dmu_objset_id_quota_upgrade(objset_t *os)
2403f67950b2SNasf-Fan {
2404f67950b2SNasf-Fan 	dmu_objset_upgrade(os, dmu_objset_id_quota_upgrade_cb);
2405f67950b2SNasf-Fan }
2406f67950b2SNasf-Fan 
2407f67950b2SNasf-Fan boolean_t
dmu_objset_userobjspace_upgradable(objset_t * os)2408f67950b2SNasf-Fan dmu_objset_userobjspace_upgradable(objset_t *os)
2409f67950b2SNasf-Fan {
2410f67950b2SNasf-Fan 	return (dmu_objset_type(os) == DMU_OST_ZFS &&
2411f67950b2SNasf-Fan 	    !dmu_objset_is_snapshot(os) &&
2412f67950b2SNasf-Fan 	    dmu_objset_userobjused_enabled(os) &&
2413f67950b2SNasf-Fan 	    !dmu_objset_userobjspace_present(os) &&
2414f67950b2SNasf-Fan 	    spa_writeable(dmu_objset_spa(os)));
2415f67950b2SNasf-Fan }
2416f67950b2SNasf-Fan 
2417f67950b2SNasf-Fan boolean_t
dmu_objset_projectquota_upgradable(objset_t * os)2418f67950b2SNasf-Fan dmu_objset_projectquota_upgradable(objset_t *os)
2419f67950b2SNasf-Fan {
2420f67950b2SNasf-Fan 	return (dmu_objset_type(os) == DMU_OST_ZFS &&
2421f67950b2SNasf-Fan 	    !dmu_objset_is_snapshot(os) &&
2422f67950b2SNasf-Fan 	    dmu_objset_projectquota_enabled(os) &&
24238bf394f1STom Caputi 	    !dmu_objset_projectquota_present(os) &&
24248bf394f1STom Caputi 	    spa_writeable(dmu_objset_spa(os)));
2425f67950b2SNasf-Fan }
2426f67950b2SNasf-Fan 
2427fa9e4066Sahrens void
dmu_objset_space(objset_t * os,uint64_t * refdbytesp,uint64_t * availbytesp,uint64_t * usedobjsp,uint64_t * availobjsp)2428a2eea2e1Sahrens dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
2429a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
2430fa9e4066Sahrens {
2431503ad85cSMatthew Ahrens 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
2432a2eea2e1Sahrens 	    usedobjsp, availobjsp);
2433a2eea2e1Sahrens }
2434a2eea2e1Sahrens 
2435a2eea2e1Sahrens uint64_t
dmu_objset_fsid_guid(objset_t * os)2436a2eea2e1Sahrens dmu_objset_fsid_guid(objset_t *os)
2437a2eea2e1Sahrens {
2438503ad85cSMatthew Ahrens 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
2439a2eea2e1Sahrens }
2440a2eea2e1Sahrens 
2441a2eea2e1Sahrens void
dmu_objset_fast_stat(objset_t * os,dmu_objset_stats_t * stat)2442a2eea2e1Sahrens dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
2443a2eea2e1Sahrens {
2444503ad85cSMatthew Ahrens 	stat->dds_type = os->os_phys->os_type;
2445503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset)
2446503ad85cSMatthew Ahrens 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
2447a2eea2e1Sahrens }
2448a2eea2e1Sahrens 
2449a2eea2e1Sahrens void
dmu_objset_stats(objset_t * os,nvlist_t * nv)2450a2eea2e1Sahrens dmu_objset_stats(objset_t *os, nvlist_t *nv)
2451a2eea2e1Sahrens {
2452503ad85cSMatthew Ahrens 	ASSERT(os->os_dsl_dataset ||
2453503ad85cSMatthew Ahrens 	    os->os_phys->os_type == DMU_OST_META);
2454a2eea2e1Sahrens 
2455503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
2456503ad85cSMatthew Ahrens 		dsl_dataset_stats(os->os_dsl_dataset, nv);
2457a2eea2e1Sahrens 
2458a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
2459503ad85cSMatthew Ahrens 	    os->os_phys->os_type);
246014843421SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
246114843421SMatthew Ahrens 	    dmu_objset_userspace_present(os));
2462fa9e4066Sahrens }
2463fa9e4066Sahrens 
2464fa9e4066Sahrens int
dmu_objset_is_snapshot(objset_t * os)2465fa9e4066Sahrens dmu_objset_is_snapshot(objset_t *os)
2466fa9e4066Sahrens {
2467503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset != NULL)
2468bc9014e6SJustin Gibbs 		return (os->os_dsl_dataset->ds_is_snapshot);
2469fa9e4066Sahrens 	else
2470fa9e4066Sahrens 		return (B_FALSE);
2471fa9e4066Sahrens }
2472fa9e4066Sahrens 
2473ab04eb8eStimh int
dmu_snapshot_realname(objset_t * os,char * name,char * real,int maxlen,boolean_t * conflict)2474ab04eb8eStimh dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
2475ab04eb8eStimh     boolean_t *conflict)
2476ab04eb8eStimh {
2477503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
2478ab04eb8eStimh 	uint64_t ignored;
2479ab04eb8eStimh 
2480c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2481be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
2482ab04eb8eStimh 
2483ab04eb8eStimh 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
2484c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
24851c17160aSKevin Crowe 	    MT_NORMALIZE, real, maxlen, conflict));
2486ab04eb8eStimh }
2487ab04eb8eStimh 
2488fa9e4066Sahrens int
dmu_snapshot_list_next(objset_t * os,int namelen,char * name,uint64_t * idp,uint64_t * offp,boolean_t * case_conflict)2489fa9e4066Sahrens dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
2490b38f0970Sck     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
2491fa9e4066Sahrens {
2492503ad85cSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
2493fa9e4066Sahrens 	zap_cursor_t cursor;
2494fa9e4066Sahrens 	zap_attribute_t attr;
2495fa9e4066Sahrens 
24963b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
24973b2aab18SMatthew Ahrens 
2498c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2499be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
2500fa9e4066Sahrens 
2501fa9e4066Sahrens 	zap_cursor_init_serialized(&cursor,
2502fa9e4066Sahrens 	    ds->ds_dir->dd_pool->dp_meta_objset,
2503c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
2504fa9e4066Sahrens 
250587e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
250687e5029aSahrens 		zap_cursor_fini(&cursor);
2507be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
250887e5029aSahrens 	}
250987e5029aSahrens 
251087e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
251187e5029aSahrens 		zap_cursor_fini(&cursor);
2512be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
251387e5029aSahrens 	}
251487e5029aSahrens 
251587e5029aSahrens 	(void) strcpy(name, attr.za_name);
251687e5029aSahrens 	if (idp)
251787e5029aSahrens 		*idp = attr.za_first_integer;
2518b38f0970Sck 	if (case_conflict)
2519b38f0970Sck 		*case_conflict = attr.za_normalization_conflict;
252087e5029aSahrens 	zap_cursor_advance(&cursor);
252187e5029aSahrens 	*offp = zap_cursor_serialize(&cursor);
252287e5029aSahrens 	zap_cursor_fini(&cursor);
252387e5029aSahrens 
252487e5029aSahrens 	return (0);
252587e5029aSahrens }
252687e5029aSahrens 
252787e5029aSahrens int
dmu_dir_list_next(objset_t * os,int namelen,char * name,uint64_t * idp,uint64_t * offp)252887e5029aSahrens dmu_dir_list_next(objset_t *os, int namelen, char *name,
252987e5029aSahrens     uint64_t *idp, uint64_t *offp)
253087e5029aSahrens {
2531503ad85cSMatthew Ahrens 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
253287e5029aSahrens 	zap_cursor_t cursor;
253387e5029aSahrens 	zap_attribute_t attr;
253487e5029aSahrens 
253587e5029aSahrens 	/* there is no next dir on a snapshot! */
2536503ad85cSMatthew Ahrens 	if (os->os_dsl_dataset->ds_object !=
2537c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj)
2538be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
2539fa9e4066Sahrens 
254087e5029aSahrens 	zap_cursor_init_serialized(&cursor,
254187e5029aSahrens 	    dd->dd_pool->dp_meta_objset,
2542c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
254387e5029aSahrens 
254487e5029aSahrens 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
254587e5029aSahrens 		zap_cursor_fini(&cursor);
2546be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
254787e5029aSahrens 	}
254887e5029aSahrens 
254987e5029aSahrens 	if (strlen(attr.za_name) + 1 > namelen) {
255087e5029aSahrens 		zap_cursor_fini(&cursor);
2551be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
255287e5029aSahrens 	}
2553fa9e4066Sahrens 
2554fa9e4066Sahrens 	(void) strcpy(name, attr.za_name);
255587e5029aSahrens 	if (idp)
255687e5029aSahrens 		*idp = attr.za_first_integer;
2557fa9e4066Sahrens 	zap_cursor_advance(&cursor);
2558fa9e4066Sahrens 	*offp = zap_cursor_serialize(&cursor);
255987e5029aSahrens 	zap_cursor_fini(&cursor);
2560fa9e4066Sahrens 
2561fa9e4066Sahrens 	return (0);
2562fa9e4066Sahrens }
2563fa9e4066Sahrens 
256412380e1eSArne Jansen typedef struct dmu_objset_find_ctx {
256512380e1eSArne Jansen 	taskq_t		*dc_tq;
256612380e1eSArne Jansen 	dsl_pool_t	*dc_dp;
256712380e1eSArne Jansen 	uint64_t	dc_ddobj;
25687588687eSMatthew Ahrens 	char		*dc_ddname; /* last component of ddobj's name */
256912380e1eSArne Jansen 	int		(*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
257012380e1eSArne Jansen 	void		*dc_arg;
257112380e1eSArne Jansen 	int		dc_flags;
257212380e1eSArne Jansen 	kmutex_t	*dc_error_lock;
257312380e1eSArne Jansen 	int		*dc_error;
257412380e1eSArne Jansen } dmu_objset_find_ctx_t;
257512380e1eSArne Jansen 
257612380e1eSArne Jansen static void
dmu_objset_find_dp_impl(dmu_objset_find_ctx_t * dcp)257712380e1eSArne Jansen dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
2578088f3894Sahrens {
257912380e1eSArne Jansen 	dsl_pool_t *dp = dcp->dc_dp;
25803b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
25813b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
25823b2aab18SMatthew Ahrens 	zap_cursor_t zc;
25833b2aab18SMatthew Ahrens 	zap_attribute_t *attr;
25843b2aab18SMatthew Ahrens 	uint64_t thisobj;
258512380e1eSArne Jansen 	int err = 0;
25863b2aab18SMatthew Ahrens 
258712380e1eSArne Jansen 	/* don't process if there already was an error */
258812380e1eSArne Jansen 	if (*dcp->dc_error != 0)
258912380e1eSArne Jansen 		goto out;
25903b2aab18SMatthew Ahrens 
25917588687eSMatthew Ahrens 	/*
25927588687eSMatthew Ahrens 	 * Note: passing the name (dc_ddname) here is optional, but it
25937588687eSMatthew Ahrens 	 * improves performance because we don't need to call
25947588687eSMatthew Ahrens 	 * zap_value_search() to determine the name.
25957588687eSMatthew Ahrens 	 */
25967588687eSMatthew Ahrens 	err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
25973b2aab18SMatthew Ahrens 	if (err != 0)
259812380e1eSArne Jansen 		goto out;
25993b2aab18SMatthew Ahrens 
26003b2aab18SMatthew Ahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
26013b2aab18SMatthew Ahrens 	if (dd->dd_myname[0] == '$') {
26023b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
260312380e1eSArne Jansen 		goto out;
26043b2aab18SMatthew Ahrens 	}
26053b2aab18SMatthew Ahrens 
2606c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
26073b2aab18SMatthew Ahrens 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
26083b2aab18SMatthew Ahrens 
26093b2aab18SMatthew Ahrens 	/*
26103b2aab18SMatthew Ahrens 	 * Iterate over all children.
26113b2aab18SMatthew Ahrens 	 */
261212380e1eSArne Jansen 	if (dcp->dc_flags & DS_FIND_CHILDREN) {
26133b2aab18SMatthew Ahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
2614c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
26153b2aab18SMatthew Ahrens 		    zap_cursor_retrieve(&zc, attr) == 0;
26163b2aab18SMatthew Ahrens 		    (void) zap_cursor_advance(&zc)) {
26173b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
26183b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
26193b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
26203b2aab18SMatthew Ahrens 
26217588687eSMatthew Ahrens 			dmu_objset_find_ctx_t *child_dcp =
26227588687eSMatthew Ahrens 			    kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
262312380e1eSArne Jansen 			*child_dcp = *dcp;
262412380e1eSArne Jansen 			child_dcp->dc_ddobj = attr->za_first_integer;
26257588687eSMatthew Ahrens 			child_dcp->dc_ddname = spa_strdup(attr->za_name);
262612380e1eSArne Jansen 			if (dcp->dc_tq != NULL)
262712380e1eSArne Jansen 				(void) taskq_dispatch(dcp->dc_tq,
262812380e1eSArne Jansen 				    dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
262912380e1eSArne Jansen 			else
263012380e1eSArne Jansen 				dmu_objset_find_dp_impl(child_dcp);
26313b2aab18SMatthew Ahrens 		}
26323b2aab18SMatthew Ahrens 		zap_cursor_fini(&zc);
26333b2aab18SMatthew Ahrens 	}
26343b2aab18SMatthew Ahrens 
26353b2aab18SMatthew Ahrens 	/*
26363b2aab18SMatthew Ahrens 	 * Iterate over all snapshots.
26373b2aab18SMatthew Ahrens 	 */
263812380e1eSArne Jansen 	if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
26393b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
26403b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
26413b2aab18SMatthew Ahrens 
26423b2aab18SMatthew Ahrens 		if (err == 0) {
2643c1379625SJustin T. Gibbs 			uint64_t snapobj;
2644c1379625SJustin T. Gibbs 
2645c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
26463b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
26473b2aab18SMatthew Ahrens 
26483b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
26493b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
26503b2aab18SMatthew Ahrens 			    (void) zap_cursor_advance(&zc)) {
26513b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
26523b2aab18SMatthew Ahrens 				    sizeof (uint64_t));
26533b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
26543b2aab18SMatthew Ahrens 
26553b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
26563b2aab18SMatthew Ahrens 				    attr->za_first_integer, FTAG, &ds);
26573b2aab18SMatthew Ahrens 				if (err != 0)
26583b2aab18SMatthew Ahrens 					break;
265912380e1eSArne Jansen 				err = dcp->dc_func(dp, ds, dcp->dc_arg);
26603b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
26613b2aab18SMatthew Ahrens 				if (err != 0)
26623b2aab18SMatthew Ahrens 					break;
26633b2aab18SMatthew Ahrens 			}
26643b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
26653b2aab18SMatthew Ahrens 		}
26663b2aab18SMatthew Ahrens 	}
26673b2aab18SMatthew Ahrens 
26683b2aab18SMatthew Ahrens 	kmem_free(attr, sizeof (zap_attribute_t));
26693b2aab18SMatthew Ahrens 
26707588687eSMatthew Ahrens 	if (err != 0) {
26717588687eSMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
267212380e1eSArne Jansen 		goto out;
26737588687eSMatthew Ahrens 	}
26743b2aab18SMatthew Ahrens 
26753b2aab18SMatthew Ahrens 	/*
26763b2aab18SMatthew Ahrens 	 * Apply to self.
26773b2aab18SMatthew Ahrens 	 */
26783b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
26797588687eSMatthew Ahrens 
26807588687eSMatthew Ahrens 	/*
26817588687eSMatthew Ahrens 	 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
26827588687eSMatthew Ahrens 	 * that the dir will remain cached, and we won't have to re-instantiate
26837588687eSMatthew Ahrens 	 * it (which could be expensive due to finding its name via
26847588687eSMatthew Ahrens 	 * zap_value_search()).
26857588687eSMatthew Ahrens 	 */
26867588687eSMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
26873b2aab18SMatthew Ahrens 	if (err != 0)
268812380e1eSArne Jansen 		goto out;
268912380e1eSArne Jansen 	err = dcp->dc_func(dp, ds, dcp->dc_arg);
26903b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
269112380e1eSArne Jansen 
269212380e1eSArne Jansen out:
269312380e1eSArne Jansen 	if (err != 0) {
269412380e1eSArne Jansen 		mutex_enter(dcp->dc_error_lock);
269512380e1eSArne Jansen 		/* only keep first error */
269612380e1eSArne Jansen 		if (*dcp->dc_error == 0)
269712380e1eSArne Jansen 			*dcp->dc_error = err;
269812380e1eSArne Jansen 		mutex_exit(dcp->dc_error_lock);
269912380e1eSArne Jansen 	}
270012380e1eSArne Jansen 
27017588687eSMatthew Ahrens 	if (dcp->dc_ddname != NULL)
27027588687eSMatthew Ahrens 		spa_strfree(dcp->dc_ddname);
270312380e1eSArne Jansen 	kmem_free(dcp, sizeof (*dcp));
270412380e1eSArne Jansen }
270512380e1eSArne Jansen 
270612380e1eSArne Jansen static void
dmu_objset_find_dp_cb(void * arg)270712380e1eSArne Jansen dmu_objset_find_dp_cb(void *arg)
270812380e1eSArne Jansen {
270912380e1eSArne Jansen 	dmu_objset_find_ctx_t *dcp = arg;
271012380e1eSArne Jansen 	dsl_pool_t *dp = dcp->dc_dp;
271112380e1eSArne Jansen 
27121d3f896fSArne Jansen 	/*
27131d3f896fSArne Jansen 	 * We need to get a pool_config_lock here, as there are several
27141d3f896fSArne Jansen 	 * asssert(pool_config_held) down the stack. Getting a lock via
27151d3f896fSArne Jansen 	 * dsl_pool_config_enter is risky, as it might be stalled by a
27161d3f896fSArne Jansen 	 * pending writer. This would deadlock, as the write lock can
27171d3f896fSArne Jansen 	 * only be granted when our parent thread gives up the lock.
27181d3f896fSArne Jansen 	 * The _prio interface gives us priority over a pending writer.
27191d3f896fSArne Jansen 	 */
27201d3f896fSArne Jansen 	dsl_pool_config_enter_prio(dp, FTAG);
272112380e1eSArne Jansen 
272212380e1eSArne Jansen 	dmu_objset_find_dp_impl(dcp);
272312380e1eSArne Jansen 
272412380e1eSArne Jansen 	dsl_pool_config_exit(dp, FTAG);
272512380e1eSArne Jansen }
272612380e1eSArne Jansen 
272712380e1eSArne Jansen /*
272812380e1eSArne Jansen  * Find objsets under and including ddobj, call func(ds) on each.
272912380e1eSArne Jansen  * The order for the enumeration is completely undefined.
273012380e1eSArne Jansen  * func is called with dsl_pool_config held.
273112380e1eSArne Jansen  */
273212380e1eSArne Jansen int
dmu_objset_find_dp(dsl_pool_t * dp,uint64_t ddobj,int func (dsl_pool_t *,dsl_dataset_t *,void *),void * arg,int flags)273312380e1eSArne Jansen dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
273412380e1eSArne Jansen     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
273512380e1eSArne Jansen {
273612380e1eSArne Jansen 	int error = 0;
273712380e1eSArne Jansen 	taskq_t *tq = NULL;
273812380e1eSArne Jansen 	int ntasks;
273912380e1eSArne Jansen 	dmu_objset_find_ctx_t *dcp;
274012380e1eSArne Jansen 	kmutex_t err_lock;
274112380e1eSArne Jansen 
274212380e1eSArne Jansen 	mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
274312380e1eSArne Jansen 	dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
274412380e1eSArne Jansen 	dcp->dc_tq = NULL;
274512380e1eSArne Jansen 	dcp->dc_dp = dp;
274612380e1eSArne Jansen 	dcp->dc_ddobj = ddobj;
27477588687eSMatthew Ahrens 	dcp->dc_ddname = NULL;
274812380e1eSArne Jansen 	dcp->dc_func = func;
274912380e1eSArne Jansen 	dcp->dc_arg = arg;
275012380e1eSArne Jansen 	dcp->dc_flags = flags;
275112380e1eSArne Jansen 	dcp->dc_error_lock = &err_lock;
275212380e1eSArne Jansen 	dcp->dc_error = &error;
275312380e1eSArne Jansen 
275412380e1eSArne Jansen 	if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
275512380e1eSArne Jansen 		/*
275612380e1eSArne Jansen 		 * In case a write lock is held we can't make use of
275712380e1eSArne Jansen 		 * parallelism, as down the stack of the worker threads
275812380e1eSArne Jansen 		 * the lock is asserted via dsl_pool_config_held.
275912380e1eSArne Jansen 		 * In case of a read lock this is solved by getting a read
276012380e1eSArne Jansen 		 * lock in each worker thread, which isn't possible in case
276112380e1eSArne Jansen 		 * of a writer lock. So we fall back to the synchronous path
276212380e1eSArne Jansen 		 * here.
276312380e1eSArne Jansen 		 * In the future it might be possible to get some magic into
276412380e1eSArne Jansen 		 * dsl_pool_config_held in a way that it returns true for
276512380e1eSArne Jansen 		 * the worker threads so that a single lock held from this
276612380e1eSArne Jansen 		 * thread suffices. For now, stay single threaded.
276712380e1eSArne Jansen 		 */
276812380e1eSArne Jansen 		dmu_objset_find_dp_impl(dcp);
27692bad2258SSteven Hartland 		mutex_destroy(&err_lock);
277012380e1eSArne Jansen 
277112380e1eSArne Jansen 		return (error);
277212380e1eSArne Jansen 	}
277312380e1eSArne Jansen 
277412380e1eSArne Jansen 	ntasks = dmu_find_threads;
277512380e1eSArne Jansen 	if (ntasks == 0)
277612380e1eSArne Jansen 		ntasks = vdev_count_leaves(dp->dp_spa) * 4;
277712380e1eSArne Jansen 	tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
277812380e1eSArne Jansen 	    INT_MAX, 0);
277912380e1eSArne Jansen 	if (tq == NULL) {
278012380e1eSArne Jansen 		kmem_free(dcp, sizeof (*dcp));
27812bad2258SSteven Hartland 		mutex_destroy(&err_lock);
27822bad2258SSteven Hartland 
278312380e1eSArne Jansen 		return (SET_ERROR(ENOMEM));
278412380e1eSArne Jansen 	}
278512380e1eSArne Jansen 	dcp->dc_tq = tq;
278612380e1eSArne Jansen 
278712380e1eSArne Jansen 	/* dcp will be freed by task */
278812380e1eSArne Jansen 	(void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
278912380e1eSArne Jansen 
279012380e1eSArne Jansen 	/*
279112380e1eSArne Jansen 	 * PORTING: this code relies on the property of taskq_wait to wait
279212380e1eSArne Jansen 	 * until no more tasks are queued and no more tasks are active. As
279312380e1eSArne Jansen 	 * we always queue new tasks from within other tasks, task_wait
279412380e1eSArne Jansen 	 * reliably waits for the full recursion to finish, even though we
279512380e1eSArne Jansen 	 * enqueue new tasks after taskq_wait has been called.
279612380e1eSArne Jansen 	 * On platforms other than illumos, taskq_wait may not have this
279712380e1eSArne Jansen 	 * property.
279812380e1eSArne Jansen 	 */
279912380e1eSArne Jansen 	taskq_wait(tq);
280012380e1eSArne Jansen 	taskq_destroy(tq);
280112380e1eSArne Jansen 	mutex_destroy(&err_lock);
280212380e1eSArne Jansen 
280312380e1eSArne Jansen 	return (error);
2804088f3894Sahrens }
2805088f3894Sahrens 
2806088f3894Sahrens /*
28073b2aab18SMatthew Ahrens  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
28083b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when this is called, and it
28093b2aab18SMatthew Ahrens  * will not be held when the callback is called.
28103b2aab18SMatthew Ahrens  * Therefore this function should only be used when the pool is not changing
28113b2aab18SMatthew Ahrens  * (e.g. in syncing context), or the callback can deal with the possible races.
2812088f3894Sahrens  */
28133b2aab18SMatthew Ahrens static int
dmu_objset_find_impl(spa_t * spa,const char * name,int func (const char *,void *),void * arg,int flags)28143b2aab18SMatthew Ahrens dmu_objset_find_impl(spa_t *spa, const char *name,
28153b2aab18SMatthew Ahrens     int func(const char *, void *), void *arg, int flags)
2816fa9e4066Sahrens {
2817fa9e4066Sahrens 	dsl_dir_t *dd;
28183b2aab18SMatthew Ahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2819088f3894Sahrens 	dsl_dataset_t *ds;
2820fa9e4066Sahrens 	zap_cursor_t zc;
2821b7661cccSmmusante 	zap_attribute_t *attr;
2822fa9e4066Sahrens 	char *child;
2823088f3894Sahrens 	uint64_t thisobj;
2824088f3894Sahrens 	int err;
2825fa9e4066Sahrens 
28263b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dp, FTAG);
28273b2aab18SMatthew Ahrens 
28283b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
28293b2aab18SMatthew Ahrens 	if (err != 0) {
28303b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
28311d452cf5Sahrens 		return (err);
28323b2aab18SMatthew Ahrens 	}
2833fa9e4066Sahrens 
2834088f3894Sahrens 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2835088f3894Sahrens 	if (dd->dd_myname[0] == '$') {
28363b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
28373b2aab18SMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
2838088f3894Sahrens 		return (0);
2839088f3894Sahrens 	}
2840088f3894Sahrens 
2841c1379625SJustin T. Gibbs 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2842b7661cccSmmusante 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2843fa9e4066Sahrens 
2844fa9e4066Sahrens 	/*
2845fa9e4066Sahrens 	 * Iterate over all children.
2846fa9e4066Sahrens 	 */
28470b69c2f0Sahrens 	if (flags & DS_FIND_CHILDREN) {
2848088f3894Sahrens 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
2849c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
2850b7661cccSmmusante 		    zap_cursor_retrieve(&zc, attr) == 0;
28510b69c2f0Sahrens 		    (void) zap_cursor_advance(&zc)) {
28523b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_integer_length, ==,
28533b2aab18SMatthew Ahrens 			    sizeof (uint64_t));
28543b2aab18SMatthew Ahrens 			ASSERT3U(attr->za_num_integers, ==, 1);
2855fa9e4066Sahrens 
2856486ae710SMatthew Ahrens 			child = kmem_asprintf("%s/%s", name, attr->za_name);
28573b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
28583b2aab18SMatthew Ahrens 			err = dmu_objset_find_impl(spa, child,
28593b2aab18SMatthew Ahrens 			    func, arg, flags);
28603b2aab18SMatthew Ahrens 			dsl_pool_config_enter(dp, FTAG);
2861486ae710SMatthew Ahrens 			strfree(child);
28623b2aab18SMatthew Ahrens 			if (err != 0)
28630b69c2f0Sahrens 				break;
28640b69c2f0Sahrens 		}
28650b69c2f0Sahrens 		zap_cursor_fini(&zc);
28661d452cf5Sahrens 
28673b2aab18SMatthew Ahrens 		if (err != 0) {
28683b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
28693b2aab18SMatthew Ahrens 			dsl_pool_config_exit(dp, FTAG);
2870b7661cccSmmusante 			kmem_free(attr, sizeof (zap_attribute_t));
28710b69c2f0Sahrens 			return (err);
28720b69c2f0Sahrens 		}
2873fa9e4066Sahrens 	}
2874fa9e4066Sahrens 
2875fa9e4066Sahrens 	/*
2876fa9e4066Sahrens 	 * Iterate over all snapshots.
2877fa9e4066Sahrens 	 */
2878088f3894Sahrens 	if (flags & DS_FIND_SNAPSHOTS) {
2879088f3894Sahrens 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2880088f3894Sahrens 
2881088f3894Sahrens 		if (err == 0) {
2882c1379625SJustin T. Gibbs 			uint64_t snapobj;
2883c1379625SJustin T. Gibbs 
2884c1379625SJustin T. Gibbs 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2885088f3894Sahrens 			dsl_dataset_rele(ds, FTAG);
2886088f3894Sahrens 
2887088f3894Sahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2888088f3894Sahrens 			    zap_cursor_retrieve(&zc, attr) == 0;
2889088f3894Sahrens 			    (void) zap_cursor_advance(&zc)) {
28903b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_integer_length, ==,
2891088f3894Sahrens 				    sizeof (uint64_t));
28923b2aab18SMatthew Ahrens 				ASSERT3U(attr->za_num_integers, ==, 1);
2893088f3894Sahrens 
2894486ae710SMatthew Ahrens 				child = kmem_asprintf("%s@%s",
2895486ae710SMatthew Ahrens 				    name, attr->za_name);
28963b2aab18SMatthew Ahrens 				dsl_pool_config_exit(dp, FTAG);
28973b2aab18SMatthew Ahrens 				err = func(child, arg);
28983b2aab18SMatthew Ahrens 				dsl_pool_config_enter(dp, FTAG);
2899486ae710SMatthew Ahrens 				strfree(child);
29003b2aab18SMatthew Ahrens 				if (err != 0)
2901088f3894Sahrens 					break;
2902088f3894Sahrens 			}
2903088f3894Sahrens 			zap_cursor_fini(&zc);
2904fa9e4066Sahrens 		}
2905fa9e4066Sahrens 	}
2906fa9e4066Sahrens 
29073b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
2908b7661cccSmmusante 	kmem_free(attr, sizeof (zap_attribute_t));
29093b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dp, FTAG);
2910fa9e4066Sahrens 
29113b2aab18SMatthew Ahrens 	if (err != 0)
29121d452cf5Sahrens 		return (err);
29131d452cf5Sahrens 
29143b2aab18SMatthew Ahrens 	/* Apply to self. */
29153b2aab18SMatthew Ahrens 	return (func(name, arg));
2916fa9e4066Sahrens }
2917f18faf3fSek 
29183b2aab18SMatthew Ahrens /*
29193b2aab18SMatthew Ahrens  * See comment above dmu_objset_find_impl().
29203b2aab18SMatthew Ahrens  */
29217f73c863SRich Morris int
dmu_objset_find(char * name,int func (const char *,void *),void * arg,int flags)29223b2aab18SMatthew Ahrens dmu_objset_find(char *name, int func(const char *, void *), void *arg,
29233b2aab18SMatthew Ahrens     int flags)
29247f73c863SRich Morris {
29253b2aab18SMatthew Ahrens 	spa_t *spa;
29263b2aab18SMatthew Ahrens 	int error;
29277f73c863SRich Morris 
29283b2aab18SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
29293b2aab18SMatthew Ahrens 	if (error != 0)
29303b2aab18SMatthew Ahrens 		return (error);
29313b2aab18SMatthew Ahrens 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
29323b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
29333b2aab18SMatthew Ahrens 	return (error);
29347f73c863SRich Morris }
29357f73c863SRich Morris 
2936eb633035STom Caputi boolean_t
dmu_objset_incompatible_encryption_version(objset_t * os)2937eb633035STom Caputi dmu_objset_incompatible_encryption_version(objset_t *os)
2938eb633035STom Caputi {
2939eb633035STom Caputi 	return (dsl_dir_incompatible_encryption_version(
2940eb633035STom Caputi 	    os->os_dsl_dataset->ds_dir));
2941eb633035STom Caputi }
2942eb633035STom Caputi 
2943f18faf3fSek void
dmu_objset_set_user(objset_t * os,void * user_ptr)2944f18faf3fSek dmu_objset_set_user(objset_t *os, void *user_ptr)
2945f18faf3fSek {
2946503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2947503ad85cSMatthew Ahrens 	os->os_user_ptr = user_ptr;
2948f18faf3fSek }
2949f18faf3fSek 
2950f18faf3fSek void *
dmu_objset_get_user(objset_t * os)2951f18faf3fSek dmu_objset_get_user(objset_t *os)
2952f18faf3fSek {
2953503ad85cSMatthew Ahrens 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2954503ad85cSMatthew Ahrens 	return (os->os_user_ptr);
2955f18faf3fSek }
29563b2aab18SMatthew Ahrens 
29573b2aab18SMatthew Ahrens /*
29583b2aab18SMatthew Ahrens  * Determine name of filesystem, given name of snapshot.
29599adfa60dSMatthew Ahrens  * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
29603b2aab18SMatthew Ahrens  */
29613b2aab18SMatthew Ahrens int
dmu_fsname(const char * snapname,char * buf)29623b2aab18SMatthew Ahrens dmu_fsname(const char *snapname, char *buf)
29633b2aab18SMatthew Ahrens {
29643b2aab18SMatthew Ahrens 	char *atp = strchr(snapname, '@');
29653b2aab18SMatthew Ahrens 	if (atp == NULL)
2966be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
29679adfa60dSMatthew Ahrens 	if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2968be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
29693b2aab18SMatthew Ahrens 	(void) strlcpy(buf, snapname, atp - snapname + 1);
29703b2aab18SMatthew Ahrens 	return (0);
29713b2aab18SMatthew Ahrens }
297261e255ceSMatthew Ahrens 
297361e255ceSMatthew Ahrens /*
297461e255ceSMatthew Ahrens  * Call when we think we're going to write/free space in open context to track
297561e255ceSMatthew Ahrens  * the amount of dirty data in the open txg, which is also the amount
297661e255ceSMatthew Ahrens  * of memory that can not be evicted until this txg syncs.
297761e255ceSMatthew Ahrens  */
297861e255ceSMatthew Ahrens void
dmu_objset_willuse_space(objset_t * os,int64_t space,dmu_tx_t * tx)297961e255ceSMatthew Ahrens dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
298061e255ceSMatthew Ahrens {
298161e255ceSMatthew Ahrens 	dsl_dataset_t *ds = os->os_dsl_dataset;
298261e255ceSMatthew Ahrens 	int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
298361e255ceSMatthew Ahrens 
298461e255ceSMatthew Ahrens 	if (ds != NULL) {
298561e255ceSMatthew Ahrens 		dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
298661e255ceSMatthew Ahrens 		dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
298761e255ceSMatthew Ahrens 	}
298861e255ceSMatthew Ahrens }
2989