xref: /freebsd-src/sys/contrib/openzfs/module/zfs/dmu_objset.c (revision d409305fa3838fb39b38c26fc085fb729b8766d5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
25  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28  * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
29  * Copyright (c) 2016 Actifio, Inc. All rights reserved.
30  * Copyright 2017 Nexenta Systems, Inc.
31  * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
32  * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
33  * Copyright (c) 2019, Klara Inc.
34  * Copyright (c) 2019, Allan Jude
35  */
36 
37 /* Portions Copyright 2010 Robert Milkowski */
38 
39 #include <sys/cred.h>
40 #include <sys/zfs_context.h>
41 #include <sys/dmu_objset.h>
42 #include <sys/dsl_dir.h>
43 #include <sys/dsl_dataset.h>
44 #include <sys/dsl_prop.h>
45 #include <sys/dsl_pool.h>
46 #include <sys/dsl_synctask.h>
47 #include <sys/dsl_deleg.h>
48 #include <sys/dnode.h>
49 #include <sys/dbuf.h>
50 #include <sys/zvol.h>
51 #include <sys/dmu_tx.h>
52 #include <sys/zap.h>
53 #include <sys/zil.h>
54 #include <sys/dmu_impl.h>
55 #include <sys/zfs_ioctl.h>
56 #include <sys/sa.h>
57 #include <sys/zfs_onexit.h>
58 #include <sys/dsl_destroy.h>
59 #include <sys/vdev.h>
60 #include <sys/zfeature.h>
61 #include <sys/policy.h>
62 #include <sys/spa_impl.h>
63 #include <sys/dmu_recv.h>
64 #include <sys/zfs_project.h>
65 #include "zfs_namecheck.h"
66 
67 /*
68  * Needed to close a window in dnode_move() that allows the objset to be freed
69  * before it can be safely accessed.
70  */
71 krwlock_t os_lock;
72 
73 /*
74  * Tunable to overwrite the maximum number of threads for the parallelization
75  * of dmu_objset_find_dp, needed to speed up the import of pools with many
76  * datasets.
77  * Default is 4 times the number of leaf vdevs.
78  */
79 int dmu_find_threads = 0;
80 
81 /*
82  * Backfill lower metadnode objects after this many have been freed.
83  * Backfilling negatively impacts object creation rates, so only do it
84  * if there are enough holes to fill.
85  */
86 int dmu_rescan_dnode_threshold = 1 << DN_MAX_INDBLKSHIFT;
87 
88 static char *upgrade_tag = "upgrade_tag";
89 
90 static void dmu_objset_find_dp_cb(void *arg);
91 
92 static void dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb);
93 static void dmu_objset_upgrade_stop(objset_t *os);
94 
95 void
96 dmu_objset_init(void)
97 {
98 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
99 }
100 
101 void
102 dmu_objset_fini(void)
103 {
104 	rw_destroy(&os_lock);
105 }
106 
107 spa_t *
108 dmu_objset_spa(objset_t *os)
109 {
110 	return (os->os_spa);
111 }
112 
113 zilog_t *
114 dmu_objset_zil(objset_t *os)
115 {
116 	return (os->os_zil);
117 }
118 
119 dsl_pool_t *
120 dmu_objset_pool(objset_t *os)
121 {
122 	dsl_dataset_t *ds;
123 
124 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
125 		return (ds->ds_dir->dd_pool);
126 	else
127 		return (spa_get_dsl(os->os_spa));
128 }
129 
130 dsl_dataset_t *
131 dmu_objset_ds(objset_t *os)
132 {
133 	return (os->os_dsl_dataset);
134 }
135 
136 dmu_objset_type_t
137 dmu_objset_type(objset_t *os)
138 {
139 	return (os->os_phys->os_type);
140 }
141 
142 void
143 dmu_objset_name(objset_t *os, char *buf)
144 {
145 	dsl_dataset_name(os->os_dsl_dataset, buf);
146 }
147 
148 uint64_t
149 dmu_objset_id(objset_t *os)
150 {
151 	dsl_dataset_t *ds = os->os_dsl_dataset;
152 
153 	return (ds ? ds->ds_object : 0);
154 }
155 
156 uint64_t
157 dmu_objset_dnodesize(objset_t *os)
158 {
159 	return (os->os_dnodesize);
160 }
161 
162 zfs_sync_type_t
163 dmu_objset_syncprop(objset_t *os)
164 {
165 	return (os->os_sync);
166 }
167 
168 zfs_logbias_op_t
169 dmu_objset_logbias(objset_t *os)
170 {
171 	return (os->os_logbias);
172 }
173 
174 static void
175 checksum_changed_cb(void *arg, uint64_t newval)
176 {
177 	objset_t *os = arg;
178 
179 	/*
180 	 * Inheritance should have been done by now.
181 	 */
182 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
183 
184 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
185 }
186 
187 static void
188 compression_changed_cb(void *arg, uint64_t newval)
189 {
190 	objset_t *os = arg;
191 
192 	/*
193 	 * Inheritance and range checking should have been done by now.
194 	 */
195 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
196 
197 	os->os_compress = zio_compress_select(os->os_spa,
198 	    ZIO_COMPRESS_ALGO(newval), ZIO_COMPRESS_ON);
199 	os->os_complevel = zio_complevel_select(os->os_spa, os->os_compress,
200 	    ZIO_COMPRESS_LEVEL(newval), ZIO_COMPLEVEL_DEFAULT);
201 }
202 
203 static void
204 copies_changed_cb(void *arg, uint64_t newval)
205 {
206 	objset_t *os = arg;
207 
208 	/*
209 	 * Inheritance and range checking should have been done by now.
210 	 */
211 	ASSERT(newval > 0);
212 	ASSERT(newval <= spa_max_replication(os->os_spa));
213 
214 	os->os_copies = newval;
215 }
216 
217 static void
218 dedup_changed_cb(void *arg, uint64_t newval)
219 {
220 	objset_t *os = arg;
221 	spa_t *spa = os->os_spa;
222 	enum zio_checksum checksum;
223 
224 	/*
225 	 * Inheritance should have been done by now.
226 	 */
227 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
228 
229 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
230 
231 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
232 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
233 }
234 
235 static void
236 primary_cache_changed_cb(void *arg, uint64_t newval)
237 {
238 	objset_t *os = arg;
239 
240 	/*
241 	 * Inheritance and range checking should have been done by now.
242 	 */
243 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
244 	    newval == ZFS_CACHE_METADATA);
245 
246 	os->os_primary_cache = newval;
247 }
248 
249 static void
250 secondary_cache_changed_cb(void *arg, uint64_t newval)
251 {
252 	objset_t *os = arg;
253 
254 	/*
255 	 * Inheritance and range checking should have been done by now.
256 	 */
257 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
258 	    newval == ZFS_CACHE_METADATA);
259 
260 	os->os_secondary_cache = newval;
261 }
262 
263 static void
264 sync_changed_cb(void *arg, uint64_t newval)
265 {
266 	objset_t *os = arg;
267 
268 	/*
269 	 * Inheritance and range checking should have been done by now.
270 	 */
271 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
272 	    newval == ZFS_SYNC_DISABLED);
273 
274 	os->os_sync = newval;
275 	if (os->os_zil)
276 		zil_set_sync(os->os_zil, newval);
277 }
278 
279 static void
280 redundant_metadata_changed_cb(void *arg, uint64_t newval)
281 {
282 	objset_t *os = arg;
283 
284 	/*
285 	 * Inheritance and range checking should have been done by now.
286 	 */
287 	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
288 	    newval == ZFS_REDUNDANT_METADATA_MOST);
289 
290 	os->os_redundant_metadata = newval;
291 }
292 
293 static void
294 dnodesize_changed_cb(void *arg, uint64_t newval)
295 {
296 	objset_t *os = arg;
297 
298 	switch (newval) {
299 	case ZFS_DNSIZE_LEGACY:
300 		os->os_dnodesize = DNODE_MIN_SIZE;
301 		break;
302 	case ZFS_DNSIZE_AUTO:
303 		/*
304 		 * Choose a dnode size that will work well for most
305 		 * workloads if the user specified "auto". Future code
306 		 * improvements could dynamically select a dnode size
307 		 * based on observed workload patterns.
308 		 */
309 		os->os_dnodesize = DNODE_MIN_SIZE * 2;
310 		break;
311 	case ZFS_DNSIZE_1K:
312 	case ZFS_DNSIZE_2K:
313 	case ZFS_DNSIZE_4K:
314 	case ZFS_DNSIZE_8K:
315 	case ZFS_DNSIZE_16K:
316 		os->os_dnodesize = newval;
317 		break;
318 	}
319 }
320 
321 static void
322 smallblk_changed_cb(void *arg, uint64_t newval)
323 {
324 	objset_t *os = arg;
325 
326 	/*
327 	 * Inheritance and range checking should have been done by now.
328 	 */
329 	ASSERT(newval <= SPA_MAXBLOCKSIZE);
330 	ASSERT(ISP2(newval));
331 
332 	os->os_zpl_special_smallblock = newval;
333 }
334 
335 static void
336 logbias_changed_cb(void *arg, uint64_t newval)
337 {
338 	objset_t *os = arg;
339 
340 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
341 	    newval == ZFS_LOGBIAS_THROUGHPUT);
342 	os->os_logbias = newval;
343 	if (os->os_zil)
344 		zil_set_logbias(os->os_zil, newval);
345 }
346 
347 static void
348 recordsize_changed_cb(void *arg, uint64_t newval)
349 {
350 	objset_t *os = arg;
351 
352 	os->os_recordsize = newval;
353 }
354 
355 void
356 dmu_objset_byteswap(void *buf, size_t size)
357 {
358 	objset_phys_t *osp = buf;
359 
360 	ASSERT(size == OBJSET_PHYS_SIZE_V1 || size == OBJSET_PHYS_SIZE_V2 ||
361 	    size == sizeof (objset_phys_t));
362 	dnode_byteswap(&osp->os_meta_dnode);
363 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
364 	osp->os_type = BSWAP_64(osp->os_type);
365 	osp->os_flags = BSWAP_64(osp->os_flags);
366 	if (size >= OBJSET_PHYS_SIZE_V2) {
367 		dnode_byteswap(&osp->os_userused_dnode);
368 		dnode_byteswap(&osp->os_groupused_dnode);
369 		if (size >= sizeof (objset_phys_t))
370 			dnode_byteswap(&osp->os_projectused_dnode);
371 	}
372 }
373 
374 /*
375  * The hash is a CRC-based hash of the objset_t pointer and the object number.
376  */
377 static uint64_t
378 dnode_hash(const objset_t *os, uint64_t obj)
379 {
380 	uintptr_t osv = (uintptr_t)os;
381 	uint64_t crc = -1ULL;
382 
383 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
384 	/*
385 	 * The low 6 bits of the pointer don't have much entropy, because
386 	 * the objset_t is larger than 2^6 bytes long.
387 	 */
388 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
389 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
390 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
391 	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 16)) & 0xFF];
392 
393 	crc ^= (osv>>14) ^ (obj>>24);
394 
395 	return (crc);
396 }
397 
398 static unsigned int
399 dnode_multilist_index_func(multilist_t *ml, void *obj)
400 {
401 	dnode_t *dn = obj;
402 	return (dnode_hash(dn->dn_objset, dn->dn_object) %
403 	    multilist_get_num_sublists(ml));
404 }
405 
406 /*
407  * Instantiates the objset_t in-memory structure corresponding to the
408  * objset_phys_t that's pointed to by the specified blkptr_t.
409  */
410 int
411 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
412     objset_t **osp)
413 {
414 	objset_t *os;
415 	int i, err;
416 
417 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
418 	ASSERT(!BP_IS_REDACTED(bp));
419 
420 	/*
421 	 * We need the pool config lock to get properties.
422 	 */
423 	ASSERT(ds == NULL || dsl_pool_config_held(ds->ds_dir->dd_pool));
424 
425 	/*
426 	 * The $ORIGIN dataset (if it exists) doesn't have an associated
427 	 * objset, so there's no reason to open it. The $ORIGIN dataset
428 	 * will not exist on pools older than SPA_VERSION_ORIGIN.
429 	 */
430 	if (ds != NULL && spa_get_dsl(spa) != NULL &&
431 	    spa_get_dsl(spa)->dp_origin_snap != NULL) {
432 		ASSERT3P(ds->ds_dir, !=,
433 		    spa_get_dsl(spa)->dp_origin_snap->ds_dir);
434 	}
435 
436 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
437 	os->os_dsl_dataset = ds;
438 	os->os_spa = spa;
439 	os->os_rootbp = bp;
440 	if (!BP_IS_HOLE(os->os_rootbp)) {
441 		arc_flags_t aflags = ARC_FLAG_WAIT;
442 		zbookmark_phys_t zb;
443 		int size;
444 		enum zio_flag zio_flags = ZIO_FLAG_CANFAIL;
445 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
446 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
447 
448 		if (DMU_OS_IS_L2CACHEABLE(os))
449 			aflags |= ARC_FLAG_L2CACHE;
450 
451 		if (ds != NULL && ds->ds_dir->dd_crypto_obj != 0) {
452 			ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
453 			ASSERT(BP_IS_AUTHENTICATED(bp));
454 			zio_flags |= ZIO_FLAG_RAW;
455 		}
456 
457 		dprintf_bp(os->os_rootbp, "reading %s", "");
458 		err = arc_read(NULL, spa, os->os_rootbp,
459 		    arc_getbuf_func, &os->os_phys_buf,
460 		    ZIO_PRIORITY_SYNC_READ, zio_flags, &aflags, &zb);
461 		if (err != 0) {
462 			kmem_free(os, sizeof (objset_t));
463 			/* convert checksum errors into IO errors */
464 			if (err == ECKSUM)
465 				err = SET_ERROR(EIO);
466 			return (err);
467 		}
468 
469 		if (spa_version(spa) < SPA_VERSION_USERSPACE)
470 			size = OBJSET_PHYS_SIZE_V1;
471 		else if (!spa_feature_is_enabled(spa,
472 		    SPA_FEATURE_PROJECT_QUOTA))
473 			size = OBJSET_PHYS_SIZE_V2;
474 		else
475 			size = sizeof (objset_phys_t);
476 
477 		/* Increase the blocksize if we are permitted. */
478 		if (arc_buf_size(os->os_phys_buf) < size) {
479 			arc_buf_t *buf = arc_alloc_buf(spa, &os->os_phys_buf,
480 			    ARC_BUFC_METADATA, size);
481 			bzero(buf->b_data, size);
482 			bcopy(os->os_phys_buf->b_data, buf->b_data,
483 			    arc_buf_size(os->os_phys_buf));
484 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
485 			os->os_phys_buf = buf;
486 		}
487 
488 		os->os_phys = os->os_phys_buf->b_data;
489 		os->os_flags = os->os_phys->os_flags;
490 	} else {
491 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
492 		    sizeof (objset_phys_t) : OBJSET_PHYS_SIZE_V1;
493 		os->os_phys_buf = arc_alloc_buf(spa, &os->os_phys_buf,
494 		    ARC_BUFC_METADATA, size);
495 		os->os_phys = os->os_phys_buf->b_data;
496 		bzero(os->os_phys, size);
497 	}
498 	/*
499 	 * These properties will be filled in by the logic in zfs_get_zplprop()
500 	 * when they are queried for the first time.
501 	 */
502 	os->os_version = OBJSET_PROP_UNINITIALIZED;
503 	os->os_normalization = OBJSET_PROP_UNINITIALIZED;
504 	os->os_utf8only = OBJSET_PROP_UNINITIALIZED;
505 	os->os_casesensitivity = OBJSET_PROP_UNINITIALIZED;
506 
507 	/*
508 	 * Note: the changed_cb will be called once before the register
509 	 * func returns, thus changing the checksum/compression from the
510 	 * default (fletcher2/off).  Snapshots don't need to know about
511 	 * checksum/compression/copies.
512 	 */
513 	if (ds != NULL) {
514 		os->os_encrypted = (ds->ds_dir->dd_crypto_obj != 0);
515 
516 		err = dsl_prop_register(ds,
517 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
518 		    primary_cache_changed_cb, os);
519 		if (err == 0) {
520 			err = dsl_prop_register(ds,
521 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
522 			    secondary_cache_changed_cb, os);
523 		}
524 		if (!ds->ds_is_snapshot) {
525 			if (err == 0) {
526 				err = dsl_prop_register(ds,
527 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
528 				    checksum_changed_cb, os);
529 			}
530 			if (err == 0) {
531 				err = dsl_prop_register(ds,
532 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
533 				    compression_changed_cb, os);
534 			}
535 			if (err == 0) {
536 				err = dsl_prop_register(ds,
537 				    zfs_prop_to_name(ZFS_PROP_COPIES),
538 				    copies_changed_cb, os);
539 			}
540 			if (err == 0) {
541 				err = dsl_prop_register(ds,
542 				    zfs_prop_to_name(ZFS_PROP_DEDUP),
543 				    dedup_changed_cb, os);
544 			}
545 			if (err == 0) {
546 				err = dsl_prop_register(ds,
547 				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
548 				    logbias_changed_cb, os);
549 			}
550 			if (err == 0) {
551 				err = dsl_prop_register(ds,
552 				    zfs_prop_to_name(ZFS_PROP_SYNC),
553 				    sync_changed_cb, os);
554 			}
555 			if (err == 0) {
556 				err = dsl_prop_register(ds,
557 				    zfs_prop_to_name(
558 				    ZFS_PROP_REDUNDANT_METADATA),
559 				    redundant_metadata_changed_cb, os);
560 			}
561 			if (err == 0) {
562 				err = dsl_prop_register(ds,
563 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
564 				    recordsize_changed_cb, os);
565 			}
566 			if (err == 0) {
567 				err = dsl_prop_register(ds,
568 				    zfs_prop_to_name(ZFS_PROP_DNODESIZE),
569 				    dnodesize_changed_cb, os);
570 			}
571 			if (err == 0) {
572 				err = dsl_prop_register(ds,
573 				    zfs_prop_to_name(
574 				    ZFS_PROP_SPECIAL_SMALL_BLOCKS),
575 				    smallblk_changed_cb, os);
576 			}
577 		}
578 		if (err != 0) {
579 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
580 			kmem_free(os, sizeof (objset_t));
581 			return (err);
582 		}
583 	} else {
584 		/* It's the meta-objset. */
585 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
586 		os->os_compress = ZIO_COMPRESS_ON;
587 		os->os_complevel = ZIO_COMPLEVEL_DEFAULT;
588 		os->os_encrypted = B_FALSE;
589 		os->os_copies = spa_max_replication(spa);
590 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
591 		os->os_dedup_verify = B_FALSE;
592 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
593 		os->os_sync = ZFS_SYNC_STANDARD;
594 		os->os_primary_cache = ZFS_CACHE_ALL;
595 		os->os_secondary_cache = ZFS_CACHE_ALL;
596 		os->os_dnodesize = DNODE_MIN_SIZE;
597 	}
598 
599 	if (ds == NULL || !ds->ds_is_snapshot)
600 		os->os_zil_header = os->os_phys->os_zil_header;
601 	os->os_zil = zil_alloc(os, &os->os_zil_header);
602 
603 	for (i = 0; i < TXG_SIZE; i++) {
604 		multilist_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
605 		    offsetof(dnode_t, dn_dirty_link[i]),
606 		    dnode_multilist_index_func);
607 	}
608 	list_create(&os->os_dnodes, sizeof (dnode_t),
609 	    offsetof(dnode_t, dn_link));
610 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
611 	    offsetof(dmu_buf_impl_t, db_link));
612 
613 	list_link_init(&os->os_evicting_node);
614 
615 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
616 	mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL);
617 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
618 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
619 	os->os_obj_next_percpu_len = boot_ncpus;
620 	os->os_obj_next_percpu = kmem_zalloc(os->os_obj_next_percpu_len *
621 	    sizeof (os->os_obj_next_percpu[0]), KM_SLEEP);
622 
623 	dnode_special_open(os, &os->os_phys->os_meta_dnode,
624 	    DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
625 	if (OBJSET_BUF_HAS_USERUSED(os->os_phys_buf)) {
626 		dnode_special_open(os, &os->os_phys->os_userused_dnode,
627 		    DMU_USERUSED_OBJECT, &os->os_userused_dnode);
628 		dnode_special_open(os, &os->os_phys->os_groupused_dnode,
629 		    DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
630 		if (OBJSET_BUF_HAS_PROJECTUSED(os->os_phys_buf))
631 			dnode_special_open(os,
632 			    &os->os_phys->os_projectused_dnode,
633 			    DMU_PROJECTUSED_OBJECT, &os->os_projectused_dnode);
634 	}
635 
636 	mutex_init(&os->os_upgrade_lock, NULL, MUTEX_DEFAULT, NULL);
637 
638 	*osp = os;
639 	return (0);
640 }
641 
642 int
643 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
644 {
645 	int err = 0;
646 
647 	/*
648 	 * We need the pool_config lock to manipulate the dsl_dataset_t.
649 	 * Even if the dataset is long-held, we need the pool_config lock
650 	 * to open the objset, as it needs to get properties.
651 	 */
652 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
653 
654 	mutex_enter(&ds->ds_opening_lock);
655 	if (ds->ds_objset == NULL) {
656 		objset_t *os;
657 		rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
658 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
659 		    ds, dsl_dataset_get_blkptr(ds), &os);
660 		rrw_exit(&ds->ds_bp_rwlock, FTAG);
661 
662 		if (err == 0) {
663 			mutex_enter(&ds->ds_lock);
664 			ASSERT(ds->ds_objset == NULL);
665 			ds->ds_objset = os;
666 			mutex_exit(&ds->ds_lock);
667 		}
668 	}
669 	*osp = ds->ds_objset;
670 	mutex_exit(&ds->ds_opening_lock);
671 	return (err);
672 }
673 
674 /*
675  * Holds the pool while the objset is held.  Therefore only one objset
676  * can be held at a time.
677  */
678 int
679 dmu_objset_hold_flags(const char *name, boolean_t decrypt, void *tag,
680     objset_t **osp)
681 {
682 	dsl_pool_t *dp;
683 	dsl_dataset_t *ds;
684 	int err;
685 	ds_hold_flags_t flags;
686 
687 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
688 	err = dsl_pool_hold(name, tag, &dp);
689 	if (err != 0)
690 		return (err);
691 	err = dsl_dataset_hold_flags(dp, name, flags, tag, &ds);
692 	if (err != 0) {
693 		dsl_pool_rele(dp, tag);
694 		return (err);
695 	}
696 
697 	err = dmu_objset_from_ds(ds, osp);
698 	if (err != 0) {
699 		dsl_dataset_rele(ds, tag);
700 		dsl_pool_rele(dp, tag);
701 	}
702 
703 	return (err);
704 }
705 
706 int
707 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
708 {
709 	return (dmu_objset_hold_flags(name, B_FALSE, tag, osp));
710 }
711 
712 static int
713 dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
714     boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
715 {
716 	int err;
717 
718 	err = dmu_objset_from_ds(ds, osp);
719 	if (err != 0) {
720 		return (err);
721 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
722 		return (SET_ERROR(EINVAL));
723 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
724 		return (SET_ERROR(EROFS));
725 	} else if (!readonly && decrypt &&
726 	    dsl_dir_incompatible_encryption_version(ds->ds_dir)) {
727 		return (SET_ERROR(EROFS));
728 	}
729 
730 	/* if we are decrypting, we can now check MACs in os->os_phys_buf */
731 	if (decrypt && arc_is_unauthenticated((*osp)->os_phys_buf)) {
732 		zbookmark_phys_t zb;
733 
734 		SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
735 		    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
736 		err = arc_untransform((*osp)->os_phys_buf, (*osp)->os_spa,
737 		    &zb, B_FALSE);
738 		if (err != 0)
739 			return (err);
740 
741 		ASSERT0(arc_is_unauthenticated((*osp)->os_phys_buf));
742 	}
743 
744 	return (0);
745 }
746 
747 /*
748  * dsl_pool must not be held when this is called.
749  * Upon successful return, there will be a longhold on the dataset,
750  * and the dsl_pool will not be held.
751  */
752 int
753 dmu_objset_own(const char *name, dmu_objset_type_t type,
754     boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
755 {
756 	dsl_pool_t *dp;
757 	dsl_dataset_t *ds;
758 	int err;
759 	ds_hold_flags_t flags;
760 
761 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
762 	err = dsl_pool_hold(name, FTAG, &dp);
763 	if (err != 0)
764 		return (err);
765 	err = dsl_dataset_own(dp, name, flags, tag, &ds);
766 	if (err != 0) {
767 		dsl_pool_rele(dp, FTAG);
768 		return (err);
769 	}
770 	err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp);
771 	if (err != 0) {
772 		dsl_dataset_disown(ds, flags, tag);
773 		dsl_pool_rele(dp, FTAG);
774 		return (err);
775 	}
776 
777 	/*
778 	 * User accounting requires the dataset to be decrypted and rw.
779 	 * We also don't begin user accounting during claiming to help
780 	 * speed up pool import times and to keep this txg reserved
781 	 * completely for recovery work.
782 	 */
783 	if (!readonly && !dp->dp_spa->spa_claiming &&
784 	    (ds->ds_dir->dd_crypto_obj == 0 || decrypt)) {
785 		if (dmu_objset_userobjspace_upgradable(*osp) ||
786 		    dmu_objset_projectquota_upgradable(*osp)) {
787 			dmu_objset_id_quota_upgrade(*osp);
788 		} else if (dmu_objset_userused_enabled(*osp)) {
789 			dmu_objset_userspace_upgrade(*osp);
790 		}
791 	}
792 
793 	dsl_pool_rele(dp, FTAG);
794 	return (0);
795 }
796 
797 int
798 dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
799     boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
800 {
801 	dsl_dataset_t *ds;
802 	int err;
803 	ds_hold_flags_t flags;
804 
805 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
806 	err = dsl_dataset_own_obj(dp, obj, flags, tag, &ds);
807 	if (err != 0)
808 		return (err);
809 
810 	err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp);
811 	if (err != 0) {
812 		dsl_dataset_disown(ds, flags, tag);
813 		return (err);
814 	}
815 
816 	return (0);
817 }
818 
819 void
820 dmu_objset_rele_flags(objset_t *os, boolean_t decrypt, void *tag)
821 {
822 	ds_hold_flags_t flags;
823 	dsl_pool_t *dp = dmu_objset_pool(os);
824 
825 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
826 	dsl_dataset_rele_flags(os->os_dsl_dataset, flags, tag);
827 	dsl_pool_rele(dp, tag);
828 }
829 
830 void
831 dmu_objset_rele(objset_t *os, void *tag)
832 {
833 	dmu_objset_rele_flags(os, B_FALSE, tag);
834 }
835 
836 /*
837  * When we are called, os MUST refer to an objset associated with a dataset
838  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
839  * == tag.  We will then release and reacquire ownership of the dataset while
840  * holding the pool config_rwlock to avoid intervening namespace or ownership
841  * changes may occur.
842  *
843  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
844  * release the hold on its dataset and acquire a new one on the dataset of the
845  * same name so that it can be partially torn down and reconstructed.
846  */
847 void
848 dmu_objset_refresh_ownership(dsl_dataset_t *ds, dsl_dataset_t **newds,
849     boolean_t decrypt, void *tag)
850 {
851 	dsl_pool_t *dp;
852 	char name[ZFS_MAX_DATASET_NAME_LEN];
853 	ds_hold_flags_t flags;
854 
855 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
856 	VERIFY3P(ds, !=, NULL);
857 	VERIFY3P(ds->ds_owner, ==, tag);
858 	VERIFY(dsl_dataset_long_held(ds));
859 
860 	dsl_dataset_name(ds, name);
861 	dp = ds->ds_dir->dd_pool;
862 	dsl_pool_config_enter(dp, FTAG);
863 	dsl_dataset_disown(ds, flags, tag);
864 	VERIFY0(dsl_dataset_own(dp, name, flags, tag, newds));
865 	dsl_pool_config_exit(dp, FTAG);
866 }
867 
868 void
869 dmu_objset_disown(objset_t *os, boolean_t decrypt, void *tag)
870 {
871 	ds_hold_flags_t flags;
872 
873 	flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : DS_HOLD_FLAG_NONE;
874 	/*
875 	 * Stop upgrading thread
876 	 */
877 	dmu_objset_upgrade_stop(os);
878 	dsl_dataset_disown(os->os_dsl_dataset, flags, tag);
879 }
880 
881 void
882 dmu_objset_evict_dbufs(objset_t *os)
883 {
884 	dnode_t *dn_marker;
885 	dnode_t *dn;
886 
887 	dn_marker = kmem_alloc(sizeof (dnode_t), KM_SLEEP);
888 
889 	mutex_enter(&os->os_lock);
890 	dn = list_head(&os->os_dnodes);
891 	while (dn != NULL) {
892 		/*
893 		 * Skip dnodes without holds.  We have to do this dance
894 		 * because dnode_add_ref() only works if there is already a
895 		 * hold.  If the dnode has no holds, then it has no dbufs.
896 		 */
897 		if (dnode_add_ref(dn, FTAG)) {
898 			list_insert_after(&os->os_dnodes, dn, dn_marker);
899 			mutex_exit(&os->os_lock);
900 
901 			dnode_evict_dbufs(dn);
902 			dnode_rele(dn, FTAG);
903 
904 			mutex_enter(&os->os_lock);
905 			dn = list_next(&os->os_dnodes, dn_marker);
906 			list_remove(&os->os_dnodes, dn_marker);
907 		} else {
908 			dn = list_next(&os->os_dnodes, dn);
909 		}
910 	}
911 	mutex_exit(&os->os_lock);
912 
913 	kmem_free(dn_marker, sizeof (dnode_t));
914 
915 	if (DMU_USERUSED_DNODE(os) != NULL) {
916 		if (DMU_PROJECTUSED_DNODE(os) != NULL)
917 			dnode_evict_dbufs(DMU_PROJECTUSED_DNODE(os));
918 		dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
919 		dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
920 	}
921 	dnode_evict_dbufs(DMU_META_DNODE(os));
922 }
923 
924 /*
925  * Objset eviction processing is split into into two pieces.
926  * The first marks the objset as evicting, evicts any dbufs that
927  * have a refcount of zero, and then queues up the objset for the
928  * second phase of eviction.  Once os->os_dnodes has been cleared by
929  * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
930  * The second phase closes the special dnodes, dequeues the objset from
931  * the list of those undergoing eviction, and finally frees the objset.
932  *
933  * NOTE: Due to asynchronous eviction processing (invocation of
934  *       dnode_buf_pageout()), it is possible for the meta dnode for the
935  *       objset to have no holds even though os->os_dnodes is not empty.
936  */
937 void
938 dmu_objset_evict(objset_t *os)
939 {
940 	dsl_dataset_t *ds = os->os_dsl_dataset;
941 
942 	for (int t = 0; t < TXG_SIZE; t++)
943 		ASSERT(!dmu_objset_is_dirty(os, t));
944 
945 	if (ds)
946 		dsl_prop_unregister_all(ds, os);
947 
948 	if (os->os_sa)
949 		sa_tear_down(os);
950 
951 	dmu_objset_evict_dbufs(os);
952 
953 	mutex_enter(&os->os_lock);
954 	spa_evicting_os_register(os->os_spa, os);
955 	if (list_is_empty(&os->os_dnodes)) {
956 		mutex_exit(&os->os_lock);
957 		dmu_objset_evict_done(os);
958 	} else {
959 		mutex_exit(&os->os_lock);
960 	}
961 
962 
963 }
964 
965 void
966 dmu_objset_evict_done(objset_t *os)
967 {
968 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
969 
970 	dnode_special_close(&os->os_meta_dnode);
971 	if (DMU_USERUSED_DNODE(os)) {
972 		if (DMU_PROJECTUSED_DNODE(os))
973 			dnode_special_close(&os->os_projectused_dnode);
974 		dnode_special_close(&os->os_userused_dnode);
975 		dnode_special_close(&os->os_groupused_dnode);
976 	}
977 	zil_free(os->os_zil);
978 
979 	arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
980 
981 	/*
982 	 * This is a barrier to prevent the objset from going away in
983 	 * dnode_move() until we can safely ensure that the objset is still in
984 	 * use. We consider the objset valid before the barrier and invalid
985 	 * after the barrier.
986 	 */
987 	rw_enter(&os_lock, RW_READER);
988 	rw_exit(&os_lock);
989 
990 	kmem_free(os->os_obj_next_percpu,
991 	    os->os_obj_next_percpu_len * sizeof (os->os_obj_next_percpu[0]));
992 
993 	mutex_destroy(&os->os_lock);
994 	mutex_destroy(&os->os_userused_lock);
995 	mutex_destroy(&os->os_obj_lock);
996 	mutex_destroy(&os->os_user_ptr_lock);
997 	mutex_destroy(&os->os_upgrade_lock);
998 	for (int i = 0; i < TXG_SIZE; i++)
999 		multilist_destroy(&os->os_dirty_dnodes[i]);
1000 	spa_evicting_os_deregister(os->os_spa, os);
1001 	kmem_free(os, sizeof (objset_t));
1002 }
1003 
1004 inode_timespec_t
1005 dmu_objset_snap_cmtime(objset_t *os)
1006 {
1007 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
1008 }
1009 
1010 objset_t *
1011 dmu_objset_create_impl_dnstats(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
1012     dmu_objset_type_t type, int levels, int blksz, int ibs, dmu_tx_t *tx)
1013 {
1014 	objset_t *os;
1015 	dnode_t *mdn;
1016 
1017 	ASSERT(dmu_tx_is_syncing(tx));
1018 
1019 	if (blksz == 0)
1020 		blksz = DNODE_BLOCK_SIZE;
1021 	if (ibs == 0)
1022 		ibs = DN_MAX_INDBLKSHIFT;
1023 
1024 	if (ds != NULL)
1025 		VERIFY0(dmu_objset_from_ds(ds, &os));
1026 	else
1027 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
1028 
1029 	mdn = DMU_META_DNODE(os);
1030 
1031 	dnode_allocate(mdn, DMU_OT_DNODE, blksz, ibs, DMU_OT_NONE, 0,
1032 	    DNODE_MIN_SLOTS, tx);
1033 
1034 	/*
1035 	 * We don't want to have to increase the meta-dnode's nlevels
1036 	 * later, because then we could do it in quiescing context while
1037 	 * we are also accessing it in open context.
1038 	 *
1039 	 * This precaution is not necessary for the MOS (ds == NULL),
1040 	 * because the MOS is only updated in syncing context.
1041 	 * This is most fortunate: the MOS is the only objset that
1042 	 * needs to be synced multiple times as spa_sync() iterates
1043 	 * to convergence, so minimizing its dn_nlevels matters.
1044 	 */
1045 	if (ds != NULL) {
1046 		if (levels == 0) {
1047 			levels = 1;
1048 
1049 			/*
1050 			 * Determine the number of levels necessary for the
1051 			 * meta-dnode to contain DN_MAX_OBJECT dnodes.  Note
1052 			 * that in order to ensure that we do not overflow
1053 			 * 64 bits, there has to be a nlevels that gives us a
1054 			 * number of blocks > DN_MAX_OBJECT but < 2^64.
1055 			 * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)
1056 			 * (10) must be less than (64 - log2(DN_MAX_OBJECT))
1057 			 * (16).
1058 			 */
1059 			while ((uint64_t)mdn->dn_nblkptr <<
1060 			    (mdn->dn_datablkshift - DNODE_SHIFT + (levels - 1) *
1061 			    (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
1062 			    DN_MAX_OBJECT)
1063 				levels++;
1064 		}
1065 
1066 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
1067 		    mdn->dn_nlevels = levels;
1068 	}
1069 
1070 	ASSERT(type != DMU_OST_NONE);
1071 	ASSERT(type != DMU_OST_ANY);
1072 	ASSERT(type < DMU_OST_NUMTYPES);
1073 	os->os_phys->os_type = type;
1074 
1075 	/*
1076 	 * Enable user accounting if it is enabled and this is not an
1077 	 * encrypted receive.
1078 	 */
1079 	if (dmu_objset_userused_enabled(os) &&
1080 	    (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
1081 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1082 		if (dmu_objset_userobjused_enabled(os)) {
1083 			ds->ds_feature_activation[
1084 			    SPA_FEATURE_USEROBJ_ACCOUNTING] = (void *)B_TRUE;
1085 			os->os_phys->os_flags |=
1086 			    OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
1087 		}
1088 		if (dmu_objset_projectquota_enabled(os)) {
1089 			ds->ds_feature_activation[
1090 			    SPA_FEATURE_PROJECT_QUOTA] = (void *)B_TRUE;
1091 			os->os_phys->os_flags |=
1092 			    OBJSET_FLAG_PROJECTQUOTA_COMPLETE;
1093 		}
1094 		os->os_flags = os->os_phys->os_flags;
1095 	}
1096 
1097 	dsl_dataset_dirty(ds, tx);
1098 
1099 	return (os);
1100 }
1101 
1102 /* called from dsl for meta-objset */
1103 objset_t *
1104 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
1105     dmu_objset_type_t type, dmu_tx_t *tx)
1106 {
1107 	return (dmu_objset_create_impl_dnstats(spa, ds, bp, type, 0, 0, 0, tx));
1108 }
1109 
1110 typedef struct dmu_objset_create_arg {
1111 	const char *doca_name;
1112 	cred_t *doca_cred;
1113 	proc_t *doca_proc;
1114 	void (*doca_userfunc)(objset_t *os, void *arg,
1115 	    cred_t *cr, dmu_tx_t *tx);
1116 	void *doca_userarg;
1117 	dmu_objset_type_t doca_type;
1118 	uint64_t doca_flags;
1119 	dsl_crypto_params_t *doca_dcp;
1120 } dmu_objset_create_arg_t;
1121 
1122 /*ARGSUSED*/
1123 static int
1124 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
1125 {
1126 	dmu_objset_create_arg_t *doca = arg;
1127 	dsl_pool_t *dp = dmu_tx_pool(tx);
1128 	dsl_dir_t *pdd;
1129 	dsl_dataset_t *parentds;
1130 	objset_t *parentos;
1131 	const char *tail;
1132 	int error;
1133 
1134 	if (strchr(doca->doca_name, '@') != NULL)
1135 		return (SET_ERROR(EINVAL));
1136 
1137 	if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
1138 		return (SET_ERROR(ENAMETOOLONG));
1139 
1140 	if (dataset_nestcheck(doca->doca_name) != 0)
1141 		return (SET_ERROR(ENAMETOOLONG));
1142 
1143 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
1144 	if (error != 0)
1145 		return (error);
1146 	if (tail == NULL) {
1147 		dsl_dir_rele(pdd, FTAG);
1148 		return (SET_ERROR(EEXIST));
1149 	}
1150 
1151 	error = dmu_objset_create_crypt_check(pdd, doca->doca_dcp, NULL);
1152 	if (error != 0) {
1153 		dsl_dir_rele(pdd, FTAG);
1154 		return (error);
1155 	}
1156 
1157 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1158 	    doca->doca_cred, doca->doca_proc);
1159 	if (error != 0) {
1160 		dsl_dir_rele(pdd, FTAG);
1161 		return (error);
1162 	}
1163 
1164 	/* can't create below anything but filesystems (eg. no ZVOLs) */
1165 	error = dsl_dataset_hold_obj(pdd->dd_pool,
1166 	    dsl_dir_phys(pdd)->dd_head_dataset_obj, FTAG, &parentds);
1167 	if (error != 0) {
1168 		dsl_dir_rele(pdd, FTAG);
1169 		return (error);
1170 	}
1171 	error = dmu_objset_from_ds(parentds, &parentos);
1172 	if (error != 0) {
1173 		dsl_dataset_rele(parentds, FTAG);
1174 		dsl_dir_rele(pdd, FTAG);
1175 		return (error);
1176 	}
1177 	if (dmu_objset_type(parentos) != DMU_OST_ZFS) {
1178 		dsl_dataset_rele(parentds, FTAG);
1179 		dsl_dir_rele(pdd, FTAG);
1180 		return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
1181 	}
1182 	dsl_dataset_rele(parentds, FTAG);
1183 	dsl_dir_rele(pdd, FTAG);
1184 
1185 	return (error);
1186 }
1187 
1188 static void
1189 dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
1190 {
1191 	dmu_objset_create_arg_t *doca = arg;
1192 	dsl_pool_t *dp = dmu_tx_pool(tx);
1193 	spa_t *spa = dp->dp_spa;
1194 	dsl_dir_t *pdd;
1195 	const char *tail;
1196 	dsl_dataset_t *ds;
1197 	uint64_t obj;
1198 	blkptr_t *bp;
1199 	objset_t *os;
1200 	zio_t *rzio;
1201 
1202 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
1203 
1204 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
1205 	    doca->doca_cred, doca->doca_dcp, tx);
1206 
1207 	VERIFY0(dsl_dataset_hold_obj_flags(pdd->dd_pool, obj,
1208 	    DS_HOLD_FLAG_DECRYPT, FTAG, &ds));
1209 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1210 	bp = dsl_dataset_get_blkptr(ds);
1211 	os = dmu_objset_create_impl(spa, ds, bp, doca->doca_type, tx);
1212 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
1213 
1214 	if (doca->doca_userfunc != NULL) {
1215 		doca->doca_userfunc(os, doca->doca_userarg,
1216 		    doca->doca_cred, tx);
1217 	}
1218 
1219 	/*
1220 	 * The doca_userfunc() may write out some data that needs to be
1221 	 * encrypted if the dataset is encrypted (specifically the root
1222 	 * directory).  This data must be written out before the encryption
1223 	 * key mapping is removed by dsl_dataset_rele_flags().  Force the
1224 	 * I/O to occur immediately by invoking the relevant sections of
1225 	 * dsl_pool_sync().
1226 	 */
1227 	if (os->os_encrypted) {
1228 		dsl_dataset_t *tmpds = NULL;
1229 		boolean_t need_sync_done = B_FALSE;
1230 
1231 		mutex_enter(&ds->ds_lock);
1232 		ds->ds_owner = FTAG;
1233 		mutex_exit(&ds->ds_lock);
1234 
1235 		rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1236 		tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds,
1237 		    tx->tx_txg);
1238 		if (tmpds != NULL) {
1239 			dsl_dataset_sync(ds, rzio, tx);
1240 			need_sync_done = B_TRUE;
1241 		}
1242 		VERIFY0(zio_wait(rzio));
1243 
1244 		dmu_objset_sync_done(os, tx);
1245 		taskq_wait(dp->dp_sync_taskq);
1246 		if (txg_list_member(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1247 			ASSERT3P(ds->ds_key_mapping, !=, NULL);
1248 			key_mapping_rele(spa, ds->ds_key_mapping, ds);
1249 		}
1250 
1251 		rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1252 		tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds,
1253 		    tx->tx_txg);
1254 		if (tmpds != NULL) {
1255 			dmu_buf_rele(ds->ds_dbuf, ds);
1256 			dsl_dataset_sync(ds, rzio, tx);
1257 		}
1258 		VERIFY0(zio_wait(rzio));
1259 
1260 		if (need_sync_done) {
1261 			ASSERT3P(ds->ds_key_mapping, !=, NULL);
1262 			key_mapping_rele(spa, ds->ds_key_mapping, ds);
1263 			dsl_dataset_sync_done(ds, tx);
1264 		}
1265 
1266 		mutex_enter(&ds->ds_lock);
1267 		ds->ds_owner = NULL;
1268 		mutex_exit(&ds->ds_lock);
1269 	}
1270 
1271 	spa_history_log_internal_ds(ds, "create", tx, " ");
1272 
1273 	dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
1274 	dsl_dir_rele(pdd, FTAG);
1275 }
1276 
1277 int
1278 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
1279     dsl_crypto_params_t *dcp, dmu_objset_create_sync_func_t func, void *arg)
1280 {
1281 	dmu_objset_create_arg_t doca;
1282 	dsl_crypto_params_t tmp_dcp = { 0 };
1283 
1284 	doca.doca_name = name;
1285 	doca.doca_cred = CRED();
1286 	doca.doca_proc = curproc;
1287 	doca.doca_flags = flags;
1288 	doca.doca_userfunc = func;
1289 	doca.doca_userarg = arg;
1290 	doca.doca_type = type;
1291 
1292 	/*
1293 	 * Some callers (mostly for testing) do not provide a dcp on their
1294 	 * own but various code inside the sync task will require it to be
1295 	 * allocated. Rather than adding NULL checks throughout this code
1296 	 * or adding dummy dcp's to all of the callers we simply create a
1297 	 * dummy one here and use that. This zero dcp will have the same
1298 	 * effect as asking for inheritance of all encryption params.
1299 	 */
1300 	doca.doca_dcp = (dcp != NULL) ? dcp : &tmp_dcp;
1301 
1302 	int rv = dsl_sync_task(name,
1303 	    dmu_objset_create_check, dmu_objset_create_sync, &doca,
1304 	    6, ZFS_SPACE_CHECK_NORMAL);
1305 
1306 	if (rv == 0)
1307 		zvol_create_minor(name);
1308 	return (rv);
1309 }
1310 
1311 typedef struct dmu_objset_clone_arg {
1312 	const char *doca_clone;
1313 	const char *doca_origin;
1314 	cred_t *doca_cred;
1315 	proc_t *doca_proc;
1316 } dmu_objset_clone_arg_t;
1317 
1318 /*ARGSUSED*/
1319 static int
1320 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
1321 {
1322 	dmu_objset_clone_arg_t *doca = arg;
1323 	dsl_dir_t *pdd;
1324 	const char *tail;
1325 	int error;
1326 	dsl_dataset_t *origin;
1327 	dsl_pool_t *dp = dmu_tx_pool(tx);
1328 
1329 	if (strchr(doca->doca_clone, '@') != NULL)
1330 		return (SET_ERROR(EINVAL));
1331 
1332 	if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
1333 		return (SET_ERROR(ENAMETOOLONG));
1334 
1335 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
1336 	if (error != 0)
1337 		return (error);
1338 	if (tail == NULL) {
1339 		dsl_dir_rele(pdd, FTAG);
1340 		return (SET_ERROR(EEXIST));
1341 	}
1342 
1343 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1344 	    doca->doca_cred, doca->doca_proc);
1345 	if (error != 0) {
1346 		dsl_dir_rele(pdd, FTAG);
1347 		return (SET_ERROR(EDQUOT));
1348 	}
1349 
1350 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
1351 	if (error != 0) {
1352 		dsl_dir_rele(pdd, FTAG);
1353 		return (error);
1354 	}
1355 
1356 	/* You can only clone snapshots, not the head datasets. */
1357 	if (!origin->ds_is_snapshot) {
1358 		dsl_dataset_rele(origin, FTAG);
1359 		dsl_dir_rele(pdd, FTAG);
1360 		return (SET_ERROR(EINVAL));
1361 	}
1362 
1363 	dsl_dataset_rele(origin, FTAG);
1364 	dsl_dir_rele(pdd, FTAG);
1365 
1366 	return (0);
1367 }
1368 
1369 static void
1370 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
1371 {
1372 	dmu_objset_clone_arg_t *doca = arg;
1373 	dsl_pool_t *dp = dmu_tx_pool(tx);
1374 	dsl_dir_t *pdd;
1375 	const char *tail;
1376 	dsl_dataset_t *origin, *ds;
1377 	uint64_t obj;
1378 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1379 
1380 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
1381 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
1382 
1383 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
1384 	    doca->doca_cred, NULL, tx);
1385 
1386 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
1387 	dsl_dataset_name(origin, namebuf);
1388 	spa_history_log_internal_ds(ds, "clone", tx,
1389 	    "origin=%s (%llu)", namebuf, (u_longlong_t)origin->ds_object);
1390 	dsl_dataset_rele(ds, FTAG);
1391 	dsl_dataset_rele(origin, FTAG);
1392 	dsl_dir_rele(pdd, FTAG);
1393 }
1394 
1395 int
1396 dmu_objset_clone(const char *clone, const char *origin)
1397 {
1398 	dmu_objset_clone_arg_t doca;
1399 
1400 	doca.doca_clone = clone;
1401 	doca.doca_origin = origin;
1402 	doca.doca_cred = CRED();
1403 	doca.doca_proc = curproc;
1404 
1405 	int rv = dsl_sync_task(clone,
1406 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
1407 	    6, ZFS_SPACE_CHECK_NORMAL);
1408 
1409 	if (rv == 0)
1410 		zvol_create_minor(clone);
1411 
1412 	return (rv);
1413 }
1414 
1415 int
1416 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
1417 {
1418 	int err;
1419 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1420 	nvlist_t *snaps = fnvlist_alloc();
1421 
1422 	fnvlist_add_boolean(snaps, longsnap);
1423 	kmem_strfree(longsnap);
1424 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
1425 	fnvlist_free(snaps);
1426 	return (err);
1427 }
1428 
1429 static void
1430 dmu_objset_upgrade_task_cb(void *data)
1431 {
1432 	objset_t *os = data;
1433 
1434 	mutex_enter(&os->os_upgrade_lock);
1435 	os->os_upgrade_status = EINTR;
1436 	if (!os->os_upgrade_exit) {
1437 		int status;
1438 
1439 		mutex_exit(&os->os_upgrade_lock);
1440 
1441 		status = os->os_upgrade_cb(os);
1442 
1443 		mutex_enter(&os->os_upgrade_lock);
1444 
1445 		os->os_upgrade_status = status;
1446 	}
1447 	os->os_upgrade_exit = B_TRUE;
1448 	os->os_upgrade_id = 0;
1449 	mutex_exit(&os->os_upgrade_lock);
1450 	dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1451 }
1452 
1453 static void
1454 dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb)
1455 {
1456 	if (os->os_upgrade_id != 0)
1457 		return;
1458 
1459 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1460 	dsl_dataset_long_hold(dmu_objset_ds(os), upgrade_tag);
1461 
1462 	mutex_enter(&os->os_upgrade_lock);
1463 	if (os->os_upgrade_id == 0 && os->os_upgrade_status == 0) {
1464 		os->os_upgrade_exit = B_FALSE;
1465 		os->os_upgrade_cb = cb;
1466 		os->os_upgrade_id = taskq_dispatch(
1467 		    os->os_spa->spa_upgrade_taskq,
1468 		    dmu_objset_upgrade_task_cb, os, TQ_SLEEP);
1469 		if (os->os_upgrade_id == TASKQID_INVALID) {
1470 			dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1471 			os->os_upgrade_status = ENOMEM;
1472 		}
1473 	} else {
1474 		dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1475 	}
1476 	mutex_exit(&os->os_upgrade_lock);
1477 }
1478 
1479 static void
1480 dmu_objset_upgrade_stop(objset_t *os)
1481 {
1482 	mutex_enter(&os->os_upgrade_lock);
1483 	os->os_upgrade_exit = B_TRUE;
1484 	if (os->os_upgrade_id != 0) {
1485 		taskqid_t id = os->os_upgrade_id;
1486 
1487 		os->os_upgrade_id = 0;
1488 		mutex_exit(&os->os_upgrade_lock);
1489 
1490 		if ((taskq_cancel_id(os->os_spa->spa_upgrade_taskq, id)) == 0) {
1491 			dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1492 		}
1493 		txg_wait_synced(os->os_spa->spa_dsl_pool, 0);
1494 	} else {
1495 		mutex_exit(&os->os_upgrade_lock);
1496 	}
1497 }
1498 
1499 static void
1500 dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx)
1501 {
1502 	dnode_t *dn;
1503 
1504 	while ((dn = multilist_sublist_head(list)) != NULL) {
1505 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1506 		ASSERT(dn->dn_dbuf->db_data_pending);
1507 		/*
1508 		 * Initialize dn_zio outside dnode_sync() because the
1509 		 * meta-dnode needs to set it outside dnode_sync().
1510 		 */
1511 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1512 		ASSERT(dn->dn_zio);
1513 
1514 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1515 		multilist_sublist_remove(list, dn);
1516 
1517 		/*
1518 		 * See the comment above dnode_rele_task() for an explanation
1519 		 * of why this dnode hold is always needed (even when not
1520 		 * doing user accounting).
1521 		 */
1522 		multilist_t *newlist = &dn->dn_objset->os_synced_dnodes;
1523 		(void) dnode_add_ref(dn, newlist);
1524 		multilist_insert(newlist, dn);
1525 
1526 		dnode_sync(dn, tx);
1527 	}
1528 }
1529 
1530 /* ARGSUSED */
1531 static void
1532 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1533 {
1534 	blkptr_t *bp = zio->io_bp;
1535 	objset_t *os = arg;
1536 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1537 	uint64_t fill = 0;
1538 
1539 	ASSERT(!BP_IS_EMBEDDED(bp));
1540 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1541 	ASSERT0(BP_GET_LEVEL(bp));
1542 
1543 	/*
1544 	 * Update rootbp fill count: it should be the number of objects
1545 	 * allocated in the object set (not counting the "special"
1546 	 * objects that are stored in the objset_phys_t -- the meta
1547 	 * dnode and user/group/project accounting objects).
1548 	 */
1549 	for (int i = 0; i < dnp->dn_nblkptr; i++)
1550 		fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1551 
1552 	BP_SET_FILL(bp, fill);
1553 
1554 	if (os->os_dsl_dataset != NULL)
1555 		rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1556 	*os->os_rootbp = *bp;
1557 	if (os->os_dsl_dataset != NULL)
1558 		rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1559 }
1560 
1561 /* ARGSUSED */
1562 static void
1563 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1564 {
1565 	blkptr_t *bp = zio->io_bp;
1566 	blkptr_t *bp_orig = &zio->io_bp_orig;
1567 	objset_t *os = arg;
1568 
1569 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1570 		ASSERT(BP_EQUAL(bp, bp_orig));
1571 	} else {
1572 		dsl_dataset_t *ds = os->os_dsl_dataset;
1573 		dmu_tx_t *tx = os->os_synctx;
1574 
1575 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1576 		dsl_dataset_block_born(ds, bp, tx);
1577 	}
1578 	kmem_free(bp, sizeof (*bp));
1579 }
1580 
1581 typedef struct sync_dnodes_arg {
1582 	multilist_t *sda_list;
1583 	int sda_sublist_idx;
1584 	multilist_t *sda_newlist;
1585 	dmu_tx_t *sda_tx;
1586 } sync_dnodes_arg_t;
1587 
1588 static void
1589 sync_dnodes_task(void *arg)
1590 {
1591 	sync_dnodes_arg_t *sda = arg;
1592 
1593 	multilist_sublist_t *ms =
1594 	    multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
1595 
1596 	dmu_objset_sync_dnodes(ms, sda->sda_tx);
1597 
1598 	multilist_sublist_unlock(ms);
1599 
1600 	kmem_free(sda, sizeof (*sda));
1601 }
1602 
1603 
1604 /* called from dsl */
1605 void
1606 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1607 {
1608 	int txgoff;
1609 	zbookmark_phys_t zb;
1610 	zio_prop_t zp;
1611 	zio_t *zio;
1612 	list_t *list;
1613 	dbuf_dirty_record_t *dr;
1614 	int num_sublists;
1615 	multilist_t *ml;
1616 	blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1617 	*blkptr_copy = *os->os_rootbp;
1618 
1619 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1620 
1621 	ASSERT(dmu_tx_is_syncing(tx));
1622 	/* XXX the write_done callback should really give us the tx... */
1623 	os->os_synctx = tx;
1624 
1625 	if (os->os_dsl_dataset == NULL) {
1626 		/*
1627 		 * This is the MOS.  If we have upgraded,
1628 		 * spa_max_replication() could change, so reset
1629 		 * os_copies here.
1630 		 */
1631 		os->os_copies = spa_max_replication(os->os_spa);
1632 	}
1633 
1634 	/*
1635 	 * Create the root block IO
1636 	 */
1637 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1638 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1639 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1640 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1641 
1642 	dmu_write_policy(os, NULL, 0, 0, &zp);
1643 
1644 	/*
1645 	 * If we are either claiming the ZIL or doing a raw receive, write
1646 	 * out the os_phys_buf raw. Neither of these actions will effect the
1647 	 * MAC at this point.
1648 	 */
1649 	if (os->os_raw_receive ||
1650 	    os->os_next_write_raw[tx->tx_txg & TXG_MASK]) {
1651 		ASSERT(os->os_encrypted);
1652 		arc_convert_to_raw(os->os_phys_buf,
1653 		    os->os_dsl_dataset->ds_object, ZFS_HOST_BYTEORDER,
1654 		    DMU_OT_OBJSET, NULL, NULL, NULL);
1655 	}
1656 
1657 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1658 	    blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1659 	    &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
1660 	    os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1661 
1662 	/*
1663 	 * Sync special dnodes - the parent IO for the sync is the root block
1664 	 */
1665 	DMU_META_DNODE(os)->dn_zio = zio;
1666 	dnode_sync(DMU_META_DNODE(os), tx);
1667 
1668 	os->os_phys->os_flags = os->os_flags;
1669 
1670 	if (DMU_USERUSED_DNODE(os) &&
1671 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1672 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1673 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1674 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1675 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1676 	}
1677 
1678 	if (DMU_PROJECTUSED_DNODE(os) &&
1679 	    DMU_PROJECTUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1680 		DMU_PROJECTUSED_DNODE(os)->dn_zio = zio;
1681 		dnode_sync(DMU_PROJECTUSED_DNODE(os), tx);
1682 	}
1683 
1684 	txgoff = tx->tx_txg & TXG_MASK;
1685 
1686 	/*
1687 	 * We must create the list here because it uses the
1688 	 * dn_dirty_link[] of this txg.  But it may already
1689 	 * exist because we call dsl_dataset_sync() twice per txg.
1690 	 */
1691 	if (os->os_synced_dnodes.ml_sublists == NULL) {
1692 		multilist_create(&os->os_synced_dnodes, sizeof (dnode_t),
1693 		    offsetof(dnode_t, dn_dirty_link[txgoff]),
1694 		    dnode_multilist_index_func);
1695 	} else {
1696 		ASSERT3U(os->os_synced_dnodes.ml_offset, ==,
1697 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
1698 	}
1699 
1700 	ml = &os->os_dirty_dnodes[txgoff];
1701 	num_sublists = multilist_get_num_sublists(ml);
1702 	for (int i = 0; i < num_sublists; i++) {
1703 		if (multilist_sublist_is_empty_idx(ml, i))
1704 			continue;
1705 		sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
1706 		sda->sda_list = ml;
1707 		sda->sda_sublist_idx = i;
1708 		sda->sda_tx = tx;
1709 		(void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1710 		    sync_dnodes_task, sda, 0);
1711 		/* callback frees sda */
1712 	}
1713 	taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
1714 
1715 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1716 	while ((dr = list_head(list)) != NULL) {
1717 		ASSERT0(dr->dr_dbuf->db_level);
1718 		list_remove(list, dr);
1719 		zio_nowait(dr->dr_zio);
1720 	}
1721 
1722 	/* Enable dnode backfill if enough objects have been freed. */
1723 	if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1724 		os->os_rescan_dnodes = B_TRUE;
1725 		os->os_freed_dnodes = 0;
1726 	}
1727 
1728 	/*
1729 	 * Free intent log blocks up to this tx.
1730 	 */
1731 	zil_sync(os->os_zil, tx);
1732 	os->os_phys->os_zil_header = os->os_zil_header;
1733 	zio_nowait(zio);
1734 }
1735 
1736 boolean_t
1737 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1738 {
1739 	return (!multilist_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]));
1740 }
1741 
1742 static file_info_cb_t *file_cbs[DMU_OST_NUMTYPES];
1743 
1744 void
1745 dmu_objset_register_type(dmu_objset_type_t ost, file_info_cb_t *cb)
1746 {
1747 	file_cbs[ost] = cb;
1748 }
1749 
1750 int
1751 dmu_get_file_info(objset_t *os, dmu_object_type_t bonustype, const void *data,
1752     zfs_file_info_t *zfi)
1753 {
1754 	file_info_cb_t *cb = file_cbs[os->os_phys->os_type];
1755 	if (cb == NULL)
1756 		return (EINVAL);
1757 	return (cb(bonustype, data, zfi));
1758 }
1759 
1760 boolean_t
1761 dmu_objset_userused_enabled(objset_t *os)
1762 {
1763 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1764 	    file_cbs[os->os_phys->os_type] != NULL &&
1765 	    DMU_USERUSED_DNODE(os) != NULL);
1766 }
1767 
1768 boolean_t
1769 dmu_objset_userobjused_enabled(objset_t *os)
1770 {
1771 	return (dmu_objset_userused_enabled(os) &&
1772 	    spa_feature_is_enabled(os->os_spa, SPA_FEATURE_USEROBJ_ACCOUNTING));
1773 }
1774 
1775 boolean_t
1776 dmu_objset_projectquota_enabled(objset_t *os)
1777 {
1778 	return (file_cbs[os->os_phys->os_type] != NULL &&
1779 	    DMU_PROJECTUSED_DNODE(os) != NULL &&
1780 	    spa_feature_is_enabled(os->os_spa, SPA_FEATURE_PROJECT_QUOTA));
1781 }
1782 
1783 typedef struct userquota_node {
1784 	/* must be in the first filed, see userquota_update_cache() */
1785 	char		uqn_id[20 + DMU_OBJACCT_PREFIX_LEN];
1786 	int64_t		uqn_delta;
1787 	avl_node_t	uqn_node;
1788 } userquota_node_t;
1789 
1790 typedef struct userquota_cache {
1791 	avl_tree_t uqc_user_deltas;
1792 	avl_tree_t uqc_group_deltas;
1793 	avl_tree_t uqc_project_deltas;
1794 } userquota_cache_t;
1795 
1796 static int
1797 userquota_compare(const void *l, const void *r)
1798 {
1799 	const userquota_node_t *luqn = l;
1800 	const userquota_node_t *ruqn = r;
1801 	int rv;
1802 
1803 	/*
1804 	 * NB: can only access uqn_id because userquota_update_cache() doesn't
1805 	 * pass in an entire userquota_node_t.
1806 	 */
1807 	rv = strcmp(luqn->uqn_id, ruqn->uqn_id);
1808 
1809 	return (TREE_ISIGN(rv));
1810 }
1811 
1812 static void
1813 do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
1814 {
1815 	void *cookie;
1816 	userquota_node_t *uqn;
1817 
1818 	ASSERT(dmu_tx_is_syncing(tx));
1819 
1820 	cookie = NULL;
1821 	while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
1822 	    &cookie)) != NULL) {
1823 		/*
1824 		 * os_userused_lock protects against concurrent calls to
1825 		 * zap_increment_int().  It's needed because zap_increment_int()
1826 		 * is not thread-safe (i.e. not atomic).
1827 		 */
1828 		mutex_enter(&os->os_userused_lock);
1829 		VERIFY0(zap_increment(os, DMU_USERUSED_OBJECT,
1830 		    uqn->uqn_id, uqn->uqn_delta, tx));
1831 		mutex_exit(&os->os_userused_lock);
1832 		kmem_free(uqn, sizeof (*uqn));
1833 	}
1834 	avl_destroy(&cache->uqc_user_deltas);
1835 
1836 	cookie = NULL;
1837 	while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
1838 	    &cookie)) != NULL) {
1839 		mutex_enter(&os->os_userused_lock);
1840 		VERIFY0(zap_increment(os, DMU_GROUPUSED_OBJECT,
1841 		    uqn->uqn_id, uqn->uqn_delta, tx));
1842 		mutex_exit(&os->os_userused_lock);
1843 		kmem_free(uqn, sizeof (*uqn));
1844 	}
1845 	avl_destroy(&cache->uqc_group_deltas);
1846 
1847 	if (dmu_objset_projectquota_enabled(os)) {
1848 		cookie = NULL;
1849 		while ((uqn = avl_destroy_nodes(&cache->uqc_project_deltas,
1850 		    &cookie)) != NULL) {
1851 			mutex_enter(&os->os_userused_lock);
1852 			VERIFY0(zap_increment(os, DMU_PROJECTUSED_OBJECT,
1853 			    uqn->uqn_id, uqn->uqn_delta, tx));
1854 			mutex_exit(&os->os_userused_lock);
1855 			kmem_free(uqn, sizeof (*uqn));
1856 		}
1857 		avl_destroy(&cache->uqc_project_deltas);
1858 	}
1859 }
1860 
1861 static void
1862 userquota_update_cache(avl_tree_t *avl, const char *id, int64_t delta)
1863 {
1864 	userquota_node_t *uqn;
1865 	avl_index_t idx;
1866 
1867 	ASSERT(strlen(id) < sizeof (uqn->uqn_id));
1868 	/*
1869 	 * Use id directly for searching because uqn_id is the first field of
1870 	 * userquota_node_t and fields after uqn_id won't be accessed in
1871 	 * avl_find().
1872 	 */
1873 	uqn = avl_find(avl, (const void *)id, &idx);
1874 	if (uqn == NULL) {
1875 		uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
1876 		strlcpy(uqn->uqn_id, id, sizeof (uqn->uqn_id));
1877 		avl_insert(avl, uqn, idx);
1878 	}
1879 	uqn->uqn_delta += delta;
1880 }
1881 
1882 static void
1883 do_userquota_update(objset_t *os, userquota_cache_t *cache, uint64_t used,
1884     uint64_t flags, uint64_t user, uint64_t group, uint64_t project,
1885     boolean_t subtract)
1886 {
1887 	if (flags & DNODE_FLAG_USERUSED_ACCOUNTED) {
1888 		int64_t delta = DNODE_MIN_SIZE + used;
1889 		char name[20];
1890 
1891 		if (subtract)
1892 			delta = -delta;
1893 
1894 		(void) snprintf(name, sizeof (name), "%llx", (longlong_t)user);
1895 		userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1896 
1897 		(void) snprintf(name, sizeof (name), "%llx", (longlong_t)group);
1898 		userquota_update_cache(&cache->uqc_group_deltas, name, delta);
1899 
1900 		if (dmu_objset_projectquota_enabled(os)) {
1901 			(void) snprintf(name, sizeof (name), "%llx",
1902 			    (longlong_t)project);
1903 			userquota_update_cache(&cache->uqc_project_deltas,
1904 			    name, delta);
1905 		}
1906 	}
1907 }
1908 
1909 static void
1910 do_userobjquota_update(objset_t *os, userquota_cache_t *cache, uint64_t flags,
1911     uint64_t user, uint64_t group, uint64_t project, boolean_t subtract)
1912 {
1913 	if (flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) {
1914 		char name[20 + DMU_OBJACCT_PREFIX_LEN];
1915 		int delta = subtract ? -1 : 1;
1916 
1917 		(void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1918 		    (longlong_t)user);
1919 		userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1920 
1921 		(void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1922 		    (longlong_t)group);
1923 		userquota_update_cache(&cache->uqc_group_deltas, name, delta);
1924 
1925 		if (dmu_objset_projectquota_enabled(os)) {
1926 			(void) snprintf(name, sizeof (name),
1927 			    DMU_OBJACCT_PREFIX "%llx", (longlong_t)project);
1928 			userquota_update_cache(&cache->uqc_project_deltas,
1929 			    name, delta);
1930 		}
1931 	}
1932 }
1933 
1934 typedef struct userquota_updates_arg {
1935 	objset_t *uua_os;
1936 	int uua_sublist_idx;
1937 	dmu_tx_t *uua_tx;
1938 } userquota_updates_arg_t;
1939 
1940 static void
1941 userquota_updates_task(void *arg)
1942 {
1943 	userquota_updates_arg_t *uua = arg;
1944 	objset_t *os = uua->uua_os;
1945 	dmu_tx_t *tx = uua->uua_tx;
1946 	dnode_t *dn;
1947 	userquota_cache_t cache = { { 0 } };
1948 
1949 	multilist_sublist_t *list =
1950 	    multilist_sublist_lock(&os->os_synced_dnodes, uua->uua_sublist_idx);
1951 
1952 	ASSERT(multilist_sublist_head(list) == NULL ||
1953 	    dmu_objset_userused_enabled(os));
1954 	avl_create(&cache.uqc_user_deltas, userquota_compare,
1955 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1956 	avl_create(&cache.uqc_group_deltas, userquota_compare,
1957 	    sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1958 	if (dmu_objset_projectquota_enabled(os))
1959 		avl_create(&cache.uqc_project_deltas, userquota_compare,
1960 		    sizeof (userquota_node_t), offsetof(userquota_node_t,
1961 		    uqn_node));
1962 
1963 	while ((dn = multilist_sublist_head(list)) != NULL) {
1964 		int flags;
1965 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1966 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1967 		    dn->dn_phys->dn_flags &
1968 		    DNODE_FLAG_USERUSED_ACCOUNTED);
1969 
1970 		flags = dn->dn_id_flags;
1971 		ASSERT(flags);
1972 		if (flags & DN_ID_OLD_EXIST)  {
1973 			do_userquota_update(os, &cache, dn->dn_oldused,
1974 			    dn->dn_oldflags, dn->dn_olduid, dn->dn_oldgid,
1975 			    dn->dn_oldprojid, B_TRUE);
1976 			do_userobjquota_update(os, &cache, dn->dn_oldflags,
1977 			    dn->dn_olduid, dn->dn_oldgid,
1978 			    dn->dn_oldprojid, B_TRUE);
1979 		}
1980 		if (flags & DN_ID_NEW_EXIST) {
1981 			do_userquota_update(os, &cache,
1982 			    DN_USED_BYTES(dn->dn_phys), dn->dn_phys->dn_flags,
1983 			    dn->dn_newuid, dn->dn_newgid,
1984 			    dn->dn_newprojid, B_FALSE);
1985 			do_userobjquota_update(os, &cache,
1986 			    dn->dn_phys->dn_flags, dn->dn_newuid, dn->dn_newgid,
1987 			    dn->dn_newprojid, B_FALSE);
1988 		}
1989 
1990 		mutex_enter(&dn->dn_mtx);
1991 		dn->dn_oldused = 0;
1992 		dn->dn_oldflags = 0;
1993 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1994 			dn->dn_olduid = dn->dn_newuid;
1995 			dn->dn_oldgid = dn->dn_newgid;
1996 			dn->dn_oldprojid = dn->dn_newprojid;
1997 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
1998 			if (dn->dn_bonuslen == 0)
1999 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
2000 			else
2001 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2002 		}
2003 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
2004 		mutex_exit(&dn->dn_mtx);
2005 
2006 		multilist_sublist_remove(list, dn);
2007 		dnode_rele(dn, &os->os_synced_dnodes);
2008 	}
2009 	do_userquota_cacheflush(os, &cache, tx);
2010 	multilist_sublist_unlock(list);
2011 	kmem_free(uua, sizeof (*uua));
2012 }
2013 
2014 /*
2015  * Release dnode holds from dmu_objset_sync_dnodes().  When the dnode is being
2016  * synced (i.e. we have issued the zio's for blocks in the dnode), it can't be
2017  * evicted because the block containing the dnode can't be evicted until it is
2018  * written out.  However, this hold is necessary to prevent the dnode_t from
2019  * being moved (via dnode_move()) while it's still referenced by
2020  * dbuf_dirty_record_t:dr_dnode.  And dr_dnode is needed for
2021  * dirty_lightweight_leaf-type dirty records.
2022  *
2023  * If we are doing user-object accounting, the dnode_rele() happens from
2024  * userquota_updates_task() instead.
2025  */
2026 static void
2027 dnode_rele_task(void *arg)
2028 {
2029 	userquota_updates_arg_t *uua = arg;
2030 	objset_t *os = uua->uua_os;
2031 
2032 	multilist_sublist_t *list =
2033 	    multilist_sublist_lock(&os->os_synced_dnodes, uua->uua_sublist_idx);
2034 
2035 	dnode_t *dn;
2036 	while ((dn = multilist_sublist_head(list)) != NULL) {
2037 		multilist_sublist_remove(list, dn);
2038 		dnode_rele(dn, &os->os_synced_dnodes);
2039 	}
2040 	multilist_sublist_unlock(list);
2041 	kmem_free(uua, sizeof (*uua));
2042 }
2043 
2044 /*
2045  * Return TRUE if userquota updates are needed.
2046  */
2047 static boolean_t
2048 dmu_objset_do_userquota_updates_prep(objset_t *os, dmu_tx_t *tx)
2049 {
2050 	if (!dmu_objset_userused_enabled(os))
2051 		return (B_FALSE);
2052 
2053 	/*
2054 	 * If this is a raw receive just return and handle accounting
2055 	 * later when we have the keys loaded. We also don't do user
2056 	 * accounting during claiming since the datasets are not owned
2057 	 * for the duration of claiming and this txg should only be
2058 	 * used for recovery.
2059 	 */
2060 	if (os->os_encrypted && dmu_objset_is_receiving(os))
2061 		return (B_FALSE);
2062 
2063 	if (tx->tx_txg <= os->os_spa->spa_claim_max_txg)
2064 		return (B_FALSE);
2065 
2066 	/* Allocate the user/group/project used objects if necessary. */
2067 	if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
2068 		VERIFY0(zap_create_claim(os,
2069 		    DMU_USERUSED_OBJECT,
2070 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2071 		VERIFY0(zap_create_claim(os,
2072 		    DMU_GROUPUSED_OBJECT,
2073 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2074 	}
2075 
2076 	if (dmu_objset_projectquota_enabled(os) &&
2077 	    DMU_PROJECTUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
2078 		VERIFY0(zap_create_claim(os, DMU_PROJECTUSED_OBJECT,
2079 		    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2080 	}
2081 	return (B_TRUE);
2082 }
2083 
2084 /*
2085  * Dispatch taskq tasks to dp_sync_taskq to update the user accounting, and
2086  * also release the holds on the dnodes from dmu_objset_sync_dnodes().
2087  * The caller must taskq_wait(dp_sync_taskq).
2088  */
2089 void
2090 dmu_objset_sync_done(objset_t *os, dmu_tx_t *tx)
2091 {
2092 	boolean_t need_userquota = dmu_objset_do_userquota_updates_prep(os, tx);
2093 
2094 	int num_sublists = multilist_get_num_sublists(&os->os_synced_dnodes);
2095 	for (int i = 0; i < num_sublists; i++) {
2096 		userquota_updates_arg_t *uua =
2097 		    kmem_alloc(sizeof (*uua), KM_SLEEP);
2098 		uua->uua_os = os;
2099 		uua->uua_sublist_idx = i;
2100 		uua->uua_tx = tx;
2101 
2102 		/*
2103 		 * If we don't need to update userquotas, use
2104 		 * dnode_rele_task() to call dnode_rele()
2105 		 */
2106 		(void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
2107 		    need_userquota ? userquota_updates_task : dnode_rele_task,
2108 		    uua, 0);
2109 		/* callback frees uua */
2110 	}
2111 }
2112 
2113 
2114 /*
2115  * Returns a pointer to data to find uid/gid from
2116  *
2117  * If a dirty record for transaction group that is syncing can't
2118  * be found then NULL is returned.  In the NULL case it is assumed
2119  * the uid/gid aren't changing.
2120  */
2121 static void *
2122 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
2123 {
2124 	dbuf_dirty_record_t *dr;
2125 	void *data;
2126 
2127 	if (db->db_dirtycnt == 0)
2128 		return (db->db.db_data);  /* Nothing is changing */
2129 
2130 	dr = dbuf_find_dirty_eq(db, tx->tx_txg);
2131 
2132 	if (dr == NULL) {
2133 		data = NULL;
2134 	} else {
2135 		if (dr->dr_dnode->dn_bonuslen == 0 &&
2136 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
2137 			data = dr->dt.dl.dr_data->b_data;
2138 		else
2139 			data = dr->dt.dl.dr_data;
2140 	}
2141 
2142 	return (data);
2143 }
2144 
2145 void
2146 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
2147 {
2148 	objset_t *os = dn->dn_objset;
2149 	void *data = NULL;
2150 	dmu_buf_impl_t *db = NULL;
2151 	int flags = dn->dn_id_flags;
2152 	int error;
2153 	boolean_t have_spill = B_FALSE;
2154 
2155 	if (!dmu_objset_userused_enabled(dn->dn_objset))
2156 		return;
2157 
2158 	/*
2159 	 * Raw receives introduce a problem with user accounting. Raw
2160 	 * receives cannot update the user accounting info because the
2161 	 * user ids and the sizes are encrypted. To guarantee that we
2162 	 * never end up with bad user accounting, we simply disable it
2163 	 * during raw receives. We also disable this for normal receives
2164 	 * so that an incremental raw receive may be done on top of an
2165 	 * existing non-raw receive.
2166 	 */
2167 	if (os->os_encrypted && dmu_objset_is_receiving(os))
2168 		return;
2169 
2170 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
2171 	    DN_ID_CHKED_SPILL)))
2172 		return;
2173 
2174 	if (before && dn->dn_bonuslen != 0)
2175 		data = DN_BONUS(dn->dn_phys);
2176 	else if (!before && dn->dn_bonuslen != 0) {
2177 		if (dn->dn_bonus) {
2178 			db = dn->dn_bonus;
2179 			mutex_enter(&db->db_mtx);
2180 			data = dmu_objset_userquota_find_data(db, tx);
2181 		} else {
2182 			data = DN_BONUS(dn->dn_phys);
2183 		}
2184 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
2185 			int rf = 0;
2186 
2187 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
2188 				rf |= DB_RF_HAVESTRUCT;
2189 			error = dmu_spill_hold_by_dnode(dn,
2190 			    rf | DB_RF_MUST_SUCCEED,
2191 			    FTAG, (dmu_buf_t **)&db);
2192 			ASSERT(error == 0);
2193 			mutex_enter(&db->db_mtx);
2194 			data = (before) ? db->db.db_data :
2195 			    dmu_objset_userquota_find_data(db, tx);
2196 			have_spill = B_TRUE;
2197 	} else {
2198 		mutex_enter(&dn->dn_mtx);
2199 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2200 		mutex_exit(&dn->dn_mtx);
2201 		return;
2202 	}
2203 
2204 	/*
2205 	 * Must always call the callback in case the object
2206 	 * type has changed and that type isn't an object type to track
2207 	 */
2208 	zfs_file_info_t zfi;
2209 	error = file_cbs[os->os_phys->os_type](dn->dn_bonustype, data, &zfi);
2210 
2211 	if (before) {
2212 		ASSERT(data);
2213 		dn->dn_olduid = zfi.zfi_user;
2214 		dn->dn_oldgid = zfi.zfi_group;
2215 		dn->dn_oldprojid = zfi.zfi_project;
2216 	} else if (data) {
2217 		dn->dn_newuid = zfi.zfi_user;
2218 		dn->dn_newgid = zfi.zfi_group;
2219 		dn->dn_newprojid = zfi.zfi_project;
2220 	}
2221 
2222 	/*
2223 	 * Preserve existing uid/gid when the callback can't determine
2224 	 * what the new uid/gid are and the callback returned EEXIST.
2225 	 * The EEXIST error tells us to just use the existing uid/gid.
2226 	 * If we don't know what the old values are then just assign
2227 	 * them to 0, since that is a new file  being created.
2228 	 */
2229 	if (!before && data == NULL && error == EEXIST) {
2230 		if (flags & DN_ID_OLD_EXIST) {
2231 			dn->dn_newuid = dn->dn_olduid;
2232 			dn->dn_newgid = dn->dn_oldgid;
2233 			dn->dn_newprojid = dn->dn_oldprojid;
2234 		} else {
2235 			dn->dn_newuid = 0;
2236 			dn->dn_newgid = 0;
2237 			dn->dn_newprojid = ZFS_DEFAULT_PROJID;
2238 		}
2239 		error = 0;
2240 	}
2241 
2242 	if (db)
2243 		mutex_exit(&db->db_mtx);
2244 
2245 	mutex_enter(&dn->dn_mtx);
2246 	if (error == 0 && before)
2247 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
2248 	if (error == 0 && !before)
2249 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
2250 
2251 	if (have_spill) {
2252 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
2253 	} else {
2254 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2255 	}
2256 	mutex_exit(&dn->dn_mtx);
2257 	if (have_spill)
2258 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
2259 }
2260 
2261 boolean_t
2262 dmu_objset_userspace_present(objset_t *os)
2263 {
2264 	return (os->os_phys->os_flags &
2265 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
2266 }
2267 
2268 boolean_t
2269 dmu_objset_userobjspace_present(objset_t *os)
2270 {
2271 	return (os->os_phys->os_flags &
2272 	    OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE);
2273 }
2274 
2275 boolean_t
2276 dmu_objset_projectquota_present(objset_t *os)
2277 {
2278 	return (os->os_phys->os_flags &
2279 	    OBJSET_FLAG_PROJECTQUOTA_COMPLETE);
2280 }
2281 
2282 static int
2283 dmu_objset_space_upgrade(objset_t *os)
2284 {
2285 	uint64_t obj;
2286 	int err = 0;
2287 
2288 	/*
2289 	 * We simply need to mark every object dirty, so that it will be
2290 	 * synced out and now accounted.  If this is called
2291 	 * concurrently, or if we already did some work before crashing,
2292 	 * that's fine, since we track each object's accounted state
2293 	 * independently.
2294 	 */
2295 
2296 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
2297 		dmu_tx_t *tx;
2298 		dmu_buf_t *db;
2299 		int objerr;
2300 
2301 		mutex_enter(&os->os_upgrade_lock);
2302 		if (os->os_upgrade_exit)
2303 			err = SET_ERROR(EINTR);
2304 		mutex_exit(&os->os_upgrade_lock);
2305 		if (err != 0)
2306 			return (err);
2307 
2308 		if (issig(JUSTLOOKING) && issig(FORREAL))
2309 			return (SET_ERROR(EINTR));
2310 
2311 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
2312 		if (objerr != 0)
2313 			continue;
2314 		tx = dmu_tx_create(os);
2315 		dmu_tx_hold_bonus(tx, obj);
2316 		objerr = dmu_tx_assign(tx, TXG_WAIT);
2317 		if (objerr != 0) {
2318 			dmu_buf_rele(db, FTAG);
2319 			dmu_tx_abort(tx);
2320 			continue;
2321 		}
2322 		dmu_buf_will_dirty(db, tx);
2323 		dmu_buf_rele(db, FTAG);
2324 		dmu_tx_commit(tx);
2325 	}
2326 	return (0);
2327 }
2328 
2329 static int
2330 dmu_objset_userspace_upgrade_cb(objset_t *os)
2331 {
2332 	int err = 0;
2333 
2334 	if (dmu_objset_userspace_present(os))
2335 		return (0);
2336 	if (dmu_objset_is_snapshot(os))
2337 		return (SET_ERROR(EINVAL));
2338 	if (!dmu_objset_userused_enabled(os))
2339 		return (SET_ERROR(ENOTSUP));
2340 
2341 	err = dmu_objset_space_upgrade(os);
2342 	if (err)
2343 		return (err);
2344 
2345 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
2346 	txg_wait_synced(dmu_objset_pool(os), 0);
2347 	return (0);
2348 }
2349 
2350 void
2351 dmu_objset_userspace_upgrade(objset_t *os)
2352 {
2353 	dmu_objset_upgrade(os, dmu_objset_userspace_upgrade_cb);
2354 }
2355 
2356 static int
2357 dmu_objset_id_quota_upgrade_cb(objset_t *os)
2358 {
2359 	int err = 0;
2360 
2361 	if (dmu_objset_userobjspace_present(os) &&
2362 	    dmu_objset_projectquota_present(os))
2363 		return (0);
2364 	if (dmu_objset_is_snapshot(os))
2365 		return (SET_ERROR(EINVAL));
2366 	if (!dmu_objset_userused_enabled(os))
2367 		return (SET_ERROR(ENOTSUP));
2368 	if (!dmu_objset_projectquota_enabled(os) &&
2369 	    dmu_objset_userobjspace_present(os))
2370 		return (SET_ERROR(ENOTSUP));
2371 
2372 	if (dmu_objset_userobjused_enabled(os))
2373 		dmu_objset_ds(os)->ds_feature_activation[
2374 		    SPA_FEATURE_USEROBJ_ACCOUNTING] = (void *)B_TRUE;
2375 	if (dmu_objset_projectquota_enabled(os))
2376 		dmu_objset_ds(os)->ds_feature_activation[
2377 		    SPA_FEATURE_PROJECT_QUOTA] = (void *)B_TRUE;
2378 
2379 	err = dmu_objset_space_upgrade(os);
2380 	if (err)
2381 		return (err);
2382 
2383 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
2384 	if (dmu_objset_userobjused_enabled(os))
2385 		os->os_flags |= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
2386 	if (dmu_objset_projectquota_enabled(os))
2387 		os->os_flags |= OBJSET_FLAG_PROJECTQUOTA_COMPLETE;
2388 
2389 	txg_wait_synced(dmu_objset_pool(os), 0);
2390 	return (0);
2391 }
2392 
2393 void
2394 dmu_objset_id_quota_upgrade(objset_t *os)
2395 {
2396 	dmu_objset_upgrade(os, dmu_objset_id_quota_upgrade_cb);
2397 }
2398 
2399 boolean_t
2400 dmu_objset_userobjspace_upgradable(objset_t *os)
2401 {
2402 	return (dmu_objset_type(os) == DMU_OST_ZFS &&
2403 	    !dmu_objset_is_snapshot(os) &&
2404 	    dmu_objset_userobjused_enabled(os) &&
2405 	    !dmu_objset_userobjspace_present(os) &&
2406 	    spa_writeable(dmu_objset_spa(os)));
2407 }
2408 
2409 boolean_t
2410 dmu_objset_projectquota_upgradable(objset_t *os)
2411 {
2412 	return (dmu_objset_type(os) == DMU_OST_ZFS &&
2413 	    !dmu_objset_is_snapshot(os) &&
2414 	    dmu_objset_projectquota_enabled(os) &&
2415 	    !dmu_objset_projectquota_present(os) &&
2416 	    spa_writeable(dmu_objset_spa(os)));
2417 }
2418 
2419 void
2420 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
2421     uint64_t *usedobjsp, uint64_t *availobjsp)
2422 {
2423 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
2424 	    usedobjsp, availobjsp);
2425 }
2426 
2427 uint64_t
2428 dmu_objset_fsid_guid(objset_t *os)
2429 {
2430 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
2431 }
2432 
2433 void
2434 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
2435 {
2436 	stat->dds_type = os->os_phys->os_type;
2437 	if (os->os_dsl_dataset)
2438 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
2439 }
2440 
2441 void
2442 dmu_objset_stats(objset_t *os, nvlist_t *nv)
2443 {
2444 	ASSERT(os->os_dsl_dataset ||
2445 	    os->os_phys->os_type == DMU_OST_META);
2446 
2447 	if (os->os_dsl_dataset != NULL)
2448 		dsl_dataset_stats(os->os_dsl_dataset, nv);
2449 
2450 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
2451 	    os->os_phys->os_type);
2452 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
2453 	    dmu_objset_userspace_present(os));
2454 }
2455 
2456 int
2457 dmu_objset_is_snapshot(objset_t *os)
2458 {
2459 	if (os->os_dsl_dataset != NULL)
2460 		return (os->os_dsl_dataset->ds_is_snapshot);
2461 	else
2462 		return (B_FALSE);
2463 }
2464 
2465 int
2466 dmu_snapshot_realname(objset_t *os, const char *name, char *real, int maxlen,
2467     boolean_t *conflict)
2468 {
2469 	dsl_dataset_t *ds = os->os_dsl_dataset;
2470 	uint64_t ignored;
2471 
2472 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2473 		return (SET_ERROR(ENOENT));
2474 
2475 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
2476 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
2477 	    MT_NORMALIZE, real, maxlen, conflict));
2478 }
2479 
2480 int
2481 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
2482     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
2483 {
2484 	dsl_dataset_t *ds = os->os_dsl_dataset;
2485 	zap_cursor_t cursor;
2486 	zap_attribute_t attr;
2487 
2488 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
2489 
2490 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2491 		return (SET_ERROR(ENOENT));
2492 
2493 	zap_cursor_init_serialized(&cursor,
2494 	    ds->ds_dir->dd_pool->dp_meta_objset,
2495 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
2496 
2497 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2498 		zap_cursor_fini(&cursor);
2499 		return (SET_ERROR(ENOENT));
2500 	}
2501 
2502 	if (strlen(attr.za_name) + 1 > namelen) {
2503 		zap_cursor_fini(&cursor);
2504 		return (SET_ERROR(ENAMETOOLONG));
2505 	}
2506 
2507 	(void) strlcpy(name, attr.za_name, namelen);
2508 	if (idp)
2509 		*idp = attr.za_first_integer;
2510 	if (case_conflict)
2511 		*case_conflict = attr.za_normalization_conflict;
2512 	zap_cursor_advance(&cursor);
2513 	*offp = zap_cursor_serialize(&cursor);
2514 	zap_cursor_fini(&cursor);
2515 
2516 	return (0);
2517 }
2518 
2519 int
2520 dmu_snapshot_lookup(objset_t *os, const char *name, uint64_t *value)
2521 {
2522 	return (dsl_dataset_snap_lookup(os->os_dsl_dataset, name, value));
2523 }
2524 
2525 int
2526 dmu_dir_list_next(objset_t *os, int namelen, char *name,
2527     uint64_t *idp, uint64_t *offp)
2528 {
2529 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
2530 	zap_cursor_t cursor;
2531 	zap_attribute_t attr;
2532 
2533 	/* there is no next dir on a snapshot! */
2534 	if (os->os_dsl_dataset->ds_object !=
2535 	    dsl_dir_phys(dd)->dd_head_dataset_obj)
2536 		return (SET_ERROR(ENOENT));
2537 
2538 	zap_cursor_init_serialized(&cursor,
2539 	    dd->dd_pool->dp_meta_objset,
2540 	    dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
2541 
2542 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2543 		zap_cursor_fini(&cursor);
2544 		return (SET_ERROR(ENOENT));
2545 	}
2546 
2547 	if (strlen(attr.za_name) + 1 > namelen) {
2548 		zap_cursor_fini(&cursor);
2549 		return (SET_ERROR(ENAMETOOLONG));
2550 	}
2551 
2552 	(void) strlcpy(name, attr.za_name, namelen);
2553 	if (idp)
2554 		*idp = attr.za_first_integer;
2555 	zap_cursor_advance(&cursor);
2556 	*offp = zap_cursor_serialize(&cursor);
2557 	zap_cursor_fini(&cursor);
2558 
2559 	return (0);
2560 }
2561 
2562 typedef struct dmu_objset_find_ctx {
2563 	taskq_t		*dc_tq;
2564 	dsl_pool_t	*dc_dp;
2565 	uint64_t	dc_ddobj;
2566 	char		*dc_ddname; /* last component of ddobj's name */
2567 	int		(*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
2568 	void		*dc_arg;
2569 	int		dc_flags;
2570 	kmutex_t	*dc_error_lock;
2571 	int		*dc_error;
2572 } dmu_objset_find_ctx_t;
2573 
2574 static void
2575 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
2576 {
2577 	dsl_pool_t *dp = dcp->dc_dp;
2578 	dsl_dir_t *dd;
2579 	dsl_dataset_t *ds;
2580 	zap_cursor_t zc;
2581 	zap_attribute_t *attr;
2582 	uint64_t thisobj;
2583 	int err = 0;
2584 
2585 	/* don't process if there already was an error */
2586 	if (*dcp->dc_error != 0)
2587 		goto out;
2588 
2589 	/*
2590 	 * Note: passing the name (dc_ddname) here is optional, but it
2591 	 * improves performance because we don't need to call
2592 	 * zap_value_search() to determine the name.
2593 	 */
2594 	err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
2595 	if (err != 0)
2596 		goto out;
2597 
2598 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2599 	if (dd->dd_myname[0] == '$') {
2600 		dsl_dir_rele(dd, FTAG);
2601 		goto out;
2602 	}
2603 
2604 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2605 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2606 
2607 	/*
2608 	 * Iterate over all children.
2609 	 */
2610 	if (dcp->dc_flags & DS_FIND_CHILDREN) {
2611 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
2612 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
2613 		    zap_cursor_retrieve(&zc, attr) == 0;
2614 		    (void) zap_cursor_advance(&zc)) {
2615 			ASSERT3U(attr->za_integer_length, ==,
2616 			    sizeof (uint64_t));
2617 			ASSERT3U(attr->za_num_integers, ==, 1);
2618 
2619 			dmu_objset_find_ctx_t *child_dcp =
2620 			    kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
2621 			*child_dcp = *dcp;
2622 			child_dcp->dc_ddobj = attr->za_first_integer;
2623 			child_dcp->dc_ddname = spa_strdup(attr->za_name);
2624 			if (dcp->dc_tq != NULL)
2625 				(void) taskq_dispatch(dcp->dc_tq,
2626 				    dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
2627 			else
2628 				dmu_objset_find_dp_impl(child_dcp);
2629 		}
2630 		zap_cursor_fini(&zc);
2631 	}
2632 
2633 	/*
2634 	 * Iterate over all snapshots.
2635 	 */
2636 	if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
2637 		dsl_dataset_t *ds;
2638 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2639 
2640 		if (err == 0) {
2641 			uint64_t snapobj;
2642 
2643 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2644 			dsl_dataset_rele(ds, FTAG);
2645 
2646 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2647 			    zap_cursor_retrieve(&zc, attr) == 0;
2648 			    (void) zap_cursor_advance(&zc)) {
2649 				ASSERT3U(attr->za_integer_length, ==,
2650 				    sizeof (uint64_t));
2651 				ASSERT3U(attr->za_num_integers, ==, 1);
2652 
2653 				err = dsl_dataset_hold_obj(dp,
2654 				    attr->za_first_integer, FTAG, &ds);
2655 				if (err != 0)
2656 					break;
2657 				err = dcp->dc_func(dp, ds, dcp->dc_arg);
2658 				dsl_dataset_rele(ds, FTAG);
2659 				if (err != 0)
2660 					break;
2661 			}
2662 			zap_cursor_fini(&zc);
2663 		}
2664 	}
2665 
2666 	kmem_free(attr, sizeof (zap_attribute_t));
2667 
2668 	if (err != 0) {
2669 		dsl_dir_rele(dd, FTAG);
2670 		goto out;
2671 	}
2672 
2673 	/*
2674 	 * Apply to self.
2675 	 */
2676 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2677 
2678 	/*
2679 	 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2680 	 * that the dir will remain cached, and we won't have to re-instantiate
2681 	 * it (which could be expensive due to finding its name via
2682 	 * zap_value_search()).
2683 	 */
2684 	dsl_dir_rele(dd, FTAG);
2685 	if (err != 0)
2686 		goto out;
2687 	err = dcp->dc_func(dp, ds, dcp->dc_arg);
2688 	dsl_dataset_rele(ds, FTAG);
2689 
2690 out:
2691 	if (err != 0) {
2692 		mutex_enter(dcp->dc_error_lock);
2693 		/* only keep first error */
2694 		if (*dcp->dc_error == 0)
2695 			*dcp->dc_error = err;
2696 		mutex_exit(dcp->dc_error_lock);
2697 	}
2698 
2699 	if (dcp->dc_ddname != NULL)
2700 		spa_strfree(dcp->dc_ddname);
2701 	kmem_free(dcp, sizeof (*dcp));
2702 }
2703 
2704 static void
2705 dmu_objset_find_dp_cb(void *arg)
2706 {
2707 	dmu_objset_find_ctx_t *dcp = arg;
2708 	dsl_pool_t *dp = dcp->dc_dp;
2709 
2710 	/*
2711 	 * We need to get a pool_config_lock here, as there are several
2712 	 * assert(pool_config_held) down the stack. Getting a lock via
2713 	 * dsl_pool_config_enter is risky, as it might be stalled by a
2714 	 * pending writer. This would deadlock, as the write lock can
2715 	 * only be granted when our parent thread gives up the lock.
2716 	 * The _prio interface gives us priority over a pending writer.
2717 	 */
2718 	dsl_pool_config_enter_prio(dp, FTAG);
2719 
2720 	dmu_objset_find_dp_impl(dcp);
2721 
2722 	dsl_pool_config_exit(dp, FTAG);
2723 }
2724 
2725 /*
2726  * Find objsets under and including ddobj, call func(ds) on each.
2727  * The order for the enumeration is completely undefined.
2728  * func is called with dsl_pool_config held.
2729  */
2730 int
2731 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
2732     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
2733 {
2734 	int error = 0;
2735 	taskq_t *tq = NULL;
2736 	int ntasks;
2737 	dmu_objset_find_ctx_t *dcp;
2738 	kmutex_t err_lock;
2739 
2740 	mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
2741 	dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
2742 	dcp->dc_tq = NULL;
2743 	dcp->dc_dp = dp;
2744 	dcp->dc_ddobj = ddobj;
2745 	dcp->dc_ddname = NULL;
2746 	dcp->dc_func = func;
2747 	dcp->dc_arg = arg;
2748 	dcp->dc_flags = flags;
2749 	dcp->dc_error_lock = &err_lock;
2750 	dcp->dc_error = &error;
2751 
2752 	if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
2753 		/*
2754 		 * In case a write lock is held we can't make use of
2755 		 * parallelism, as down the stack of the worker threads
2756 		 * the lock is asserted via dsl_pool_config_held.
2757 		 * In case of a read lock this is solved by getting a read
2758 		 * lock in each worker thread, which isn't possible in case
2759 		 * of a writer lock. So we fall back to the synchronous path
2760 		 * here.
2761 		 * In the future it might be possible to get some magic into
2762 		 * dsl_pool_config_held in a way that it returns true for
2763 		 * the worker threads so that a single lock held from this
2764 		 * thread suffices. For now, stay single threaded.
2765 		 */
2766 		dmu_objset_find_dp_impl(dcp);
2767 		mutex_destroy(&err_lock);
2768 
2769 		return (error);
2770 	}
2771 
2772 	ntasks = dmu_find_threads;
2773 	if (ntasks == 0)
2774 		ntasks = vdev_count_leaves(dp->dp_spa) * 4;
2775 	tq = taskq_create("dmu_objset_find", ntasks, maxclsyspri, ntasks,
2776 	    INT_MAX, 0);
2777 	if (tq == NULL) {
2778 		kmem_free(dcp, sizeof (*dcp));
2779 		mutex_destroy(&err_lock);
2780 
2781 		return (SET_ERROR(ENOMEM));
2782 	}
2783 	dcp->dc_tq = tq;
2784 
2785 	/* dcp will be freed by task */
2786 	(void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
2787 
2788 	/*
2789 	 * PORTING: this code relies on the property of taskq_wait to wait
2790 	 * until no more tasks are queued and no more tasks are active. As
2791 	 * we always queue new tasks from within other tasks, task_wait
2792 	 * reliably waits for the full recursion to finish, even though we
2793 	 * enqueue new tasks after taskq_wait has been called.
2794 	 * On platforms other than illumos, taskq_wait may not have this
2795 	 * property.
2796 	 */
2797 	taskq_wait(tq);
2798 	taskq_destroy(tq);
2799 	mutex_destroy(&err_lock);
2800 
2801 	return (error);
2802 }
2803 
2804 /*
2805  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2806  * The dp_config_rwlock must not be held when this is called, and it
2807  * will not be held when the callback is called.
2808  * Therefore this function should only be used when the pool is not changing
2809  * (e.g. in syncing context), or the callback can deal with the possible races.
2810  */
2811 static int
2812 dmu_objset_find_impl(spa_t *spa, const char *name,
2813     int func(const char *, void *), void *arg, int flags)
2814 {
2815 	dsl_dir_t *dd;
2816 	dsl_pool_t *dp = spa_get_dsl(spa);
2817 	dsl_dataset_t *ds;
2818 	zap_cursor_t zc;
2819 	zap_attribute_t *attr;
2820 	char *child;
2821 	uint64_t thisobj;
2822 	int err;
2823 
2824 	dsl_pool_config_enter(dp, FTAG);
2825 
2826 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
2827 	if (err != 0) {
2828 		dsl_pool_config_exit(dp, FTAG);
2829 		return (err);
2830 	}
2831 
2832 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2833 	if (dd->dd_myname[0] == '$') {
2834 		dsl_dir_rele(dd, FTAG);
2835 		dsl_pool_config_exit(dp, FTAG);
2836 		return (0);
2837 	}
2838 
2839 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2840 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2841 
2842 	/*
2843 	 * Iterate over all children.
2844 	 */
2845 	if (flags & DS_FIND_CHILDREN) {
2846 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
2847 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
2848 		    zap_cursor_retrieve(&zc, attr) == 0;
2849 		    (void) zap_cursor_advance(&zc)) {
2850 			ASSERT3U(attr->za_integer_length, ==,
2851 			    sizeof (uint64_t));
2852 			ASSERT3U(attr->za_num_integers, ==, 1);
2853 
2854 			child = kmem_asprintf("%s/%s", name, attr->za_name);
2855 			dsl_pool_config_exit(dp, FTAG);
2856 			err = dmu_objset_find_impl(spa, child,
2857 			    func, arg, flags);
2858 			dsl_pool_config_enter(dp, FTAG);
2859 			kmem_strfree(child);
2860 			if (err != 0)
2861 				break;
2862 		}
2863 		zap_cursor_fini(&zc);
2864 
2865 		if (err != 0) {
2866 			dsl_dir_rele(dd, FTAG);
2867 			dsl_pool_config_exit(dp, FTAG);
2868 			kmem_free(attr, sizeof (zap_attribute_t));
2869 			return (err);
2870 		}
2871 	}
2872 
2873 	/*
2874 	 * Iterate over all snapshots.
2875 	 */
2876 	if (flags & DS_FIND_SNAPSHOTS) {
2877 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2878 
2879 		if (err == 0) {
2880 			uint64_t snapobj;
2881 
2882 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2883 			dsl_dataset_rele(ds, FTAG);
2884 
2885 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2886 			    zap_cursor_retrieve(&zc, attr) == 0;
2887 			    (void) zap_cursor_advance(&zc)) {
2888 				ASSERT3U(attr->za_integer_length, ==,
2889 				    sizeof (uint64_t));
2890 				ASSERT3U(attr->za_num_integers, ==, 1);
2891 
2892 				child = kmem_asprintf("%s@%s",
2893 				    name, attr->za_name);
2894 				dsl_pool_config_exit(dp, FTAG);
2895 				err = func(child, arg);
2896 				dsl_pool_config_enter(dp, FTAG);
2897 				kmem_strfree(child);
2898 				if (err != 0)
2899 					break;
2900 			}
2901 			zap_cursor_fini(&zc);
2902 		}
2903 	}
2904 
2905 	dsl_dir_rele(dd, FTAG);
2906 	kmem_free(attr, sizeof (zap_attribute_t));
2907 	dsl_pool_config_exit(dp, FTAG);
2908 
2909 	if (err != 0)
2910 		return (err);
2911 
2912 	/* Apply to self. */
2913 	return (func(name, arg));
2914 }
2915 
2916 /*
2917  * See comment above dmu_objset_find_impl().
2918  */
2919 int
2920 dmu_objset_find(const char *name, int func(const char *, void *), void *arg,
2921     int flags)
2922 {
2923 	spa_t *spa;
2924 	int error;
2925 
2926 	error = spa_open(name, &spa, FTAG);
2927 	if (error != 0)
2928 		return (error);
2929 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
2930 	spa_close(spa, FTAG);
2931 	return (error);
2932 }
2933 
2934 boolean_t
2935 dmu_objset_incompatible_encryption_version(objset_t *os)
2936 {
2937 	return (dsl_dir_incompatible_encryption_version(
2938 	    os->os_dsl_dataset->ds_dir));
2939 }
2940 
2941 void
2942 dmu_objset_set_user(objset_t *os, void *user_ptr)
2943 {
2944 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2945 	os->os_user_ptr = user_ptr;
2946 }
2947 
2948 void *
2949 dmu_objset_get_user(objset_t *os)
2950 {
2951 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2952 	return (os->os_user_ptr);
2953 }
2954 
2955 /*
2956  * Determine name of filesystem, given name of snapshot.
2957  * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2958  */
2959 int
2960 dmu_fsname(const char *snapname, char *buf)
2961 {
2962 	char *atp = strchr(snapname, '@');
2963 	if (atp == NULL)
2964 		return (SET_ERROR(EINVAL));
2965 	if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2966 		return (SET_ERROR(ENAMETOOLONG));
2967 	(void) strlcpy(buf, snapname, atp - snapname + 1);
2968 	return (0);
2969 }
2970 
2971 /*
2972  * Call when we think we're going to write/free space in open context
2973  * to track the amount of dirty data in the open txg, which is also the
2974  * amount of memory that can not be evicted until this txg syncs.
2975  *
2976  * Note that there are two conditions where this can be called from
2977  * syncing context:
2978  *
2979  * [1] When we just created the dataset, in which case we go on with
2980  *     updating any accounting of dirty data as usual.
2981  * [2] When we are dirtying MOS data, in which case we only update the
2982  *     pool's accounting of dirty data.
2983  */
2984 void
2985 dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
2986 {
2987 	dsl_dataset_t *ds = os->os_dsl_dataset;
2988 	int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
2989 
2990 	if (ds != NULL) {
2991 		dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
2992 	}
2993 
2994 	dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
2995 }
2996 
2997 #if defined(_KERNEL)
2998 EXPORT_SYMBOL(dmu_objset_zil);
2999 EXPORT_SYMBOL(dmu_objset_pool);
3000 EXPORT_SYMBOL(dmu_objset_ds);
3001 EXPORT_SYMBOL(dmu_objset_type);
3002 EXPORT_SYMBOL(dmu_objset_name);
3003 EXPORT_SYMBOL(dmu_objset_hold);
3004 EXPORT_SYMBOL(dmu_objset_hold_flags);
3005 EXPORT_SYMBOL(dmu_objset_own);
3006 EXPORT_SYMBOL(dmu_objset_rele);
3007 EXPORT_SYMBOL(dmu_objset_rele_flags);
3008 EXPORT_SYMBOL(dmu_objset_disown);
3009 EXPORT_SYMBOL(dmu_objset_from_ds);
3010 EXPORT_SYMBOL(dmu_objset_create);
3011 EXPORT_SYMBOL(dmu_objset_clone);
3012 EXPORT_SYMBOL(dmu_objset_stats);
3013 EXPORT_SYMBOL(dmu_objset_fast_stat);
3014 EXPORT_SYMBOL(dmu_objset_spa);
3015 EXPORT_SYMBOL(dmu_objset_space);
3016 EXPORT_SYMBOL(dmu_objset_fsid_guid);
3017 EXPORT_SYMBOL(dmu_objset_find);
3018 EXPORT_SYMBOL(dmu_objset_byteswap);
3019 EXPORT_SYMBOL(dmu_objset_evict_dbufs);
3020 EXPORT_SYMBOL(dmu_objset_snap_cmtime);
3021 EXPORT_SYMBOL(dmu_objset_dnodesize);
3022 
3023 EXPORT_SYMBOL(dmu_objset_sync);
3024 EXPORT_SYMBOL(dmu_objset_is_dirty);
3025 EXPORT_SYMBOL(dmu_objset_create_impl_dnstats);
3026 EXPORT_SYMBOL(dmu_objset_create_impl);
3027 EXPORT_SYMBOL(dmu_objset_open_impl);
3028 EXPORT_SYMBOL(dmu_objset_evict);
3029 EXPORT_SYMBOL(dmu_objset_register_type);
3030 EXPORT_SYMBOL(dmu_objset_sync_done);
3031 EXPORT_SYMBOL(dmu_objset_userquota_get_ids);
3032 EXPORT_SYMBOL(dmu_objset_userused_enabled);
3033 EXPORT_SYMBOL(dmu_objset_userspace_upgrade);
3034 EXPORT_SYMBOL(dmu_objset_userspace_present);
3035 EXPORT_SYMBOL(dmu_objset_userobjused_enabled);
3036 EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable);
3037 EXPORT_SYMBOL(dmu_objset_userobjspace_present);
3038 EXPORT_SYMBOL(dmu_objset_projectquota_enabled);
3039 EXPORT_SYMBOL(dmu_objset_projectquota_present);
3040 EXPORT_SYMBOL(dmu_objset_projectquota_upgradable);
3041 EXPORT_SYMBOL(dmu_objset_id_quota_upgrade);
3042 #endif
3043