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