xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision dcbf3bd6a1f1360fc1afcee9e22c6dcff7844bf2)
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  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28  * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
29  * Copyright (c) 2014 Integros [integros.com]
30  */
31 
32 /* Portions Copyright 2010 Robert Milkowski */
33 
34 #include <sys/cred.h>
35 #include <sys/zfs_context.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dsl_dataset.h>
39 #include <sys/dsl_prop.h>
40 #include <sys/dsl_pool.h>
41 #include <sys/dsl_synctask.h>
42 #include <sys/dsl_deleg.h>
43 #include <sys/dnode.h>
44 #include <sys/dbuf.h>
45 #include <sys/zvol.h>
46 #include <sys/dmu_tx.h>
47 #include <sys/zap.h>
48 #include <sys/zil.h>
49 #include <sys/dmu_impl.h>
50 #include <sys/zfs_ioctl.h>
51 #include <sys/sa.h>
52 #include <sys/zfs_onexit.h>
53 #include <sys/dsl_destroy.h>
54 #include <sys/vdev.h>
55 
56 /*
57  * Needed to close a window in dnode_move() that allows the objset to be freed
58  * before it can be safely accessed.
59  */
60 krwlock_t os_lock;
61 
62 /*
63  * Tunable to overwrite the maximum number of threads for the parallization
64  * of dmu_objset_find_dp, needed to speed up the import of pools with many
65  * datasets.
66  * Default is 4 times the number of leaf vdevs.
67  */
68 int dmu_find_threads = 0;
69 
70 static void dmu_objset_find_dp_cb(void *arg);
71 
72 void
73 dmu_objset_init(void)
74 {
75 	rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
76 }
77 
78 void
79 dmu_objset_fini(void)
80 {
81 	rw_destroy(&os_lock);
82 }
83 
84 spa_t *
85 dmu_objset_spa(objset_t *os)
86 {
87 	return (os->os_spa);
88 }
89 
90 zilog_t *
91 dmu_objset_zil(objset_t *os)
92 {
93 	return (os->os_zil);
94 }
95 
96 dsl_pool_t *
97 dmu_objset_pool(objset_t *os)
98 {
99 	dsl_dataset_t *ds;
100 
101 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
102 		return (ds->ds_dir->dd_pool);
103 	else
104 		return (spa_get_dsl(os->os_spa));
105 }
106 
107 dsl_dataset_t *
108 dmu_objset_ds(objset_t *os)
109 {
110 	return (os->os_dsl_dataset);
111 }
112 
113 dmu_objset_type_t
114 dmu_objset_type(objset_t *os)
115 {
116 	return (os->os_phys->os_type);
117 }
118 
119 void
120 dmu_objset_name(objset_t *os, char *buf)
121 {
122 	dsl_dataset_name(os->os_dsl_dataset, buf);
123 }
124 
125 uint64_t
126 dmu_objset_id(objset_t *os)
127 {
128 	dsl_dataset_t *ds = os->os_dsl_dataset;
129 
130 	return (ds ? ds->ds_object : 0);
131 }
132 
133 zfs_sync_type_t
134 dmu_objset_syncprop(objset_t *os)
135 {
136 	return (os->os_sync);
137 }
138 
139 zfs_logbias_op_t
140 dmu_objset_logbias(objset_t *os)
141 {
142 	return (os->os_logbias);
143 }
144 
145 static void
146 checksum_changed_cb(void *arg, uint64_t newval)
147 {
148 	objset_t *os = arg;
149 
150 	/*
151 	 * Inheritance should have been done by now.
152 	 */
153 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
154 
155 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
156 }
157 
158 static void
159 compression_changed_cb(void *arg, uint64_t newval)
160 {
161 	objset_t *os = arg;
162 
163 	/*
164 	 * Inheritance and range checking should have been done by now.
165 	 */
166 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
167 
168 	os->os_compress = zio_compress_select(os->os_spa, newval,
169 	    ZIO_COMPRESS_ON);
170 }
171 
172 static void
173 copies_changed_cb(void *arg, uint64_t newval)
174 {
175 	objset_t *os = arg;
176 
177 	/*
178 	 * Inheritance and range checking should have been done by now.
179 	 */
180 	ASSERT(newval > 0);
181 	ASSERT(newval <= spa_max_replication(os->os_spa));
182 
183 	os->os_copies = newval;
184 }
185 
186 static void
187 dedup_changed_cb(void *arg, uint64_t newval)
188 {
189 	objset_t *os = arg;
190 	spa_t *spa = os->os_spa;
191 	enum zio_checksum checksum;
192 
193 	/*
194 	 * Inheritance should have been done by now.
195 	 */
196 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
197 
198 	checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
199 
200 	os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
201 	os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
202 }
203 
204 static void
205 primary_cache_changed_cb(void *arg, uint64_t newval)
206 {
207 	objset_t *os = arg;
208 
209 	/*
210 	 * Inheritance and range checking should have been done by now.
211 	 */
212 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
213 	    newval == ZFS_CACHE_METADATA);
214 
215 	os->os_primary_cache = newval;
216 }
217 
218 static void
219 secondary_cache_changed_cb(void *arg, uint64_t newval)
220 {
221 	objset_t *os = arg;
222 
223 	/*
224 	 * Inheritance and range checking should have been done by now.
225 	 */
226 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
227 	    newval == ZFS_CACHE_METADATA);
228 
229 	os->os_secondary_cache = newval;
230 }
231 
232 static void
233 sync_changed_cb(void *arg, uint64_t newval)
234 {
235 	objset_t *os = arg;
236 
237 	/*
238 	 * Inheritance and range checking should have been done by now.
239 	 */
240 	ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
241 	    newval == ZFS_SYNC_DISABLED);
242 
243 	os->os_sync = newval;
244 	if (os->os_zil)
245 		zil_set_sync(os->os_zil, newval);
246 }
247 
248 static void
249 redundant_metadata_changed_cb(void *arg, uint64_t newval)
250 {
251 	objset_t *os = arg;
252 
253 	/*
254 	 * Inheritance and range checking should have been done by now.
255 	 */
256 	ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
257 	    newval == ZFS_REDUNDANT_METADATA_MOST);
258 
259 	os->os_redundant_metadata = newval;
260 }
261 
262 static void
263 logbias_changed_cb(void *arg, uint64_t newval)
264 {
265 	objset_t *os = arg;
266 
267 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
268 	    newval == ZFS_LOGBIAS_THROUGHPUT);
269 	os->os_logbias = newval;
270 	if (os->os_zil)
271 		zil_set_logbias(os->os_zil, newval);
272 }
273 
274 static void
275 recordsize_changed_cb(void *arg, uint64_t newval)
276 {
277 	objset_t *os = arg;
278 
279 	os->os_recordsize = newval;
280 }
281 
282 void
283 dmu_objset_byteswap(void *buf, size_t size)
284 {
285 	objset_phys_t *osp = buf;
286 
287 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
288 	dnode_byteswap(&osp->os_meta_dnode);
289 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
290 	osp->os_type = BSWAP_64(osp->os_type);
291 	osp->os_flags = BSWAP_64(osp->os_flags);
292 	if (size == sizeof (objset_phys_t)) {
293 		dnode_byteswap(&osp->os_userused_dnode);
294 		dnode_byteswap(&osp->os_groupused_dnode);
295 	}
296 }
297 
298 int
299 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
300     objset_t **osp)
301 {
302 	objset_t *os;
303 	int i, err;
304 
305 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
306 
307 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
308 	os->os_dsl_dataset = ds;
309 	os->os_spa = spa;
310 	os->os_rootbp = bp;
311 	if (!BP_IS_HOLE(os->os_rootbp)) {
312 		arc_flags_t aflags = ARC_FLAG_WAIT;
313 		zbookmark_phys_t zb;
314 		SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
315 		    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
316 
317 		if (DMU_OS_IS_L2CACHEABLE(os))
318 			aflags |= ARC_FLAG_L2CACHE;
319 
320 		dprintf_bp(os->os_rootbp, "reading %s", "");
321 		err = arc_read(NULL, spa, os->os_rootbp,
322 		    arc_getbuf_func, &os->os_phys_buf,
323 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
324 		if (err != 0) {
325 			kmem_free(os, sizeof (objset_t));
326 			/* convert checksum errors into IO errors */
327 			if (err == ECKSUM)
328 				err = SET_ERROR(EIO);
329 			return (err);
330 		}
331 
332 		/* Increase the blocksize if we are permitted. */
333 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
334 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
335 			arc_buf_t *buf = arc_alloc_buf(spa,
336 			    sizeof (objset_phys_t), &os->os_phys_buf,
337 			    ARC_BUFC_METADATA);
338 			bzero(buf->b_data, sizeof (objset_phys_t));
339 			bcopy(os->os_phys_buf->b_data, buf->b_data,
340 			    arc_buf_size(os->os_phys_buf));
341 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
342 			os->os_phys_buf = buf;
343 		}
344 
345 		os->os_phys = os->os_phys_buf->b_data;
346 		os->os_flags = os->os_phys->os_flags;
347 	} else {
348 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
349 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
350 		os->os_phys_buf = arc_alloc_buf(spa, size,
351 		    &os->os_phys_buf, ARC_BUFC_METADATA);
352 		os->os_phys = os->os_phys_buf->b_data;
353 		bzero(os->os_phys, size);
354 	}
355 
356 	/*
357 	 * Note: the changed_cb will be called once before the register
358 	 * func returns, thus changing the checksum/compression from the
359 	 * default (fletcher2/off).  Snapshots don't need to know about
360 	 * checksum/compression/copies.
361 	 */
362 	if (ds != NULL) {
363 		boolean_t needlock = B_FALSE;
364 
365 		/*
366 		 * Note: it's valid to open the objset if the dataset is
367 		 * long-held, in which case the pool_config lock will not
368 		 * be held.
369 		 */
370 		if (!dsl_pool_config_held(dmu_objset_pool(os))) {
371 			needlock = B_TRUE;
372 			dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
373 		}
374 		err = dsl_prop_register(ds,
375 		    zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
376 		    primary_cache_changed_cb, os);
377 		if (err == 0) {
378 			err = dsl_prop_register(ds,
379 			    zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
380 			    secondary_cache_changed_cb, os);
381 		}
382 		if (!ds->ds_is_snapshot) {
383 			if (err == 0) {
384 				err = dsl_prop_register(ds,
385 				    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
386 				    checksum_changed_cb, os);
387 			}
388 			if (err == 0) {
389 				err = dsl_prop_register(ds,
390 				    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
391 				    compression_changed_cb, os);
392 			}
393 			if (err == 0) {
394 				err = dsl_prop_register(ds,
395 				    zfs_prop_to_name(ZFS_PROP_COPIES),
396 				    copies_changed_cb, os);
397 			}
398 			if (err == 0) {
399 				err = dsl_prop_register(ds,
400 				    zfs_prop_to_name(ZFS_PROP_DEDUP),
401 				    dedup_changed_cb, os);
402 			}
403 			if (err == 0) {
404 				err = dsl_prop_register(ds,
405 				    zfs_prop_to_name(ZFS_PROP_LOGBIAS),
406 				    logbias_changed_cb, os);
407 			}
408 			if (err == 0) {
409 				err = dsl_prop_register(ds,
410 				    zfs_prop_to_name(ZFS_PROP_SYNC),
411 				    sync_changed_cb, os);
412 			}
413 			if (err == 0) {
414 				err = dsl_prop_register(ds,
415 				    zfs_prop_to_name(
416 				    ZFS_PROP_REDUNDANT_METADATA),
417 				    redundant_metadata_changed_cb, os);
418 			}
419 			if (err == 0) {
420 				err = dsl_prop_register(ds,
421 				    zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
422 				    recordsize_changed_cb, os);
423 			}
424 		}
425 		if (needlock)
426 			dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
427 		if (err != 0) {
428 			arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
429 			kmem_free(os, sizeof (objset_t));
430 			return (err);
431 		}
432 	} else {
433 		/* It's the meta-objset. */
434 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
435 		os->os_compress = ZIO_COMPRESS_ON;
436 		os->os_copies = spa_max_replication(spa);
437 		os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
438 		os->os_dedup_verify = B_FALSE;
439 		os->os_logbias = ZFS_LOGBIAS_LATENCY;
440 		os->os_sync = ZFS_SYNC_STANDARD;
441 		os->os_primary_cache = ZFS_CACHE_ALL;
442 		os->os_secondary_cache = ZFS_CACHE_ALL;
443 	}
444 
445 	if (ds == NULL || !ds->ds_is_snapshot)
446 		os->os_zil_header = os->os_phys->os_zil_header;
447 	os->os_zil = zil_alloc(os, &os->os_zil_header);
448 
449 	for (i = 0; i < TXG_SIZE; i++) {
450 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
451 		    offsetof(dnode_t, dn_dirty_link[i]));
452 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
453 		    offsetof(dnode_t, dn_dirty_link[i]));
454 	}
455 	list_create(&os->os_dnodes, sizeof (dnode_t),
456 	    offsetof(dnode_t, dn_link));
457 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
458 	    offsetof(dmu_buf_impl_t, db_link));
459 
460 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
461 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
462 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
463 
464 	dnode_special_open(os, &os->os_phys->os_meta_dnode,
465 	    DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
466 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
467 		dnode_special_open(os, &os->os_phys->os_userused_dnode,
468 		    DMU_USERUSED_OBJECT, &os->os_userused_dnode);
469 		dnode_special_open(os, &os->os_phys->os_groupused_dnode,
470 		    DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
471 	}
472 
473 	*osp = os;
474 	return (0);
475 }
476 
477 int
478 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
479 {
480 	int err = 0;
481 
482 	/*
483 	 * We shouldn't be doing anything with dsl_dataset_t's unless the
484 	 * pool_config lock is held, or the dataset is long-held.
485 	 */
486 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
487 	    dsl_dataset_long_held(ds));
488 
489 	mutex_enter(&ds->ds_opening_lock);
490 	if (ds->ds_objset == NULL) {
491 		objset_t *os;
492 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
493 		    ds, dsl_dataset_get_blkptr(ds), &os);
494 
495 		if (err == 0) {
496 			mutex_enter(&ds->ds_lock);
497 			ASSERT(ds->ds_objset == NULL);
498 			ds->ds_objset = os;
499 			mutex_exit(&ds->ds_lock);
500 		}
501 	}
502 	*osp = ds->ds_objset;
503 	mutex_exit(&ds->ds_opening_lock);
504 	return (err);
505 }
506 
507 /*
508  * Holds the pool while the objset is held.  Therefore only one objset
509  * can be held at a time.
510  */
511 int
512 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
513 {
514 	dsl_pool_t *dp;
515 	dsl_dataset_t *ds;
516 	int err;
517 
518 	err = dsl_pool_hold(name, tag, &dp);
519 	if (err != 0)
520 		return (err);
521 	err = dsl_dataset_hold(dp, name, tag, &ds);
522 	if (err != 0) {
523 		dsl_pool_rele(dp, tag);
524 		return (err);
525 	}
526 
527 	err = dmu_objset_from_ds(ds, osp);
528 	if (err != 0) {
529 		dsl_dataset_rele(ds, tag);
530 		dsl_pool_rele(dp, tag);
531 	}
532 
533 	return (err);
534 }
535 
536 static int
537 dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
538     boolean_t readonly, void *tag, objset_t **osp)
539 {
540 	int err;
541 
542 	err = dmu_objset_from_ds(ds, osp);
543 	if (err != 0) {
544 		dsl_dataset_disown(ds, tag);
545 	} else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
546 		dsl_dataset_disown(ds, tag);
547 		return (SET_ERROR(EINVAL));
548 	} else if (!readonly && dsl_dataset_is_snapshot(ds)) {
549 		dsl_dataset_disown(ds, tag);
550 		return (SET_ERROR(EROFS));
551 	}
552 	return (err);
553 }
554 
555 /*
556  * dsl_pool must not be held when this is called.
557  * Upon successful return, there will be a longhold on the dataset,
558  * and the dsl_pool will not be held.
559  */
560 int
561 dmu_objset_own(const char *name, dmu_objset_type_t type,
562     boolean_t readonly, void *tag, objset_t **osp)
563 {
564 	dsl_pool_t *dp;
565 	dsl_dataset_t *ds;
566 	int err;
567 
568 	err = dsl_pool_hold(name, FTAG, &dp);
569 	if (err != 0)
570 		return (err);
571 	err = dsl_dataset_own(dp, name, tag, &ds);
572 	if (err != 0) {
573 		dsl_pool_rele(dp, FTAG);
574 		return (err);
575 	}
576 	err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
577 	dsl_pool_rele(dp, FTAG);
578 
579 	return (err);
580 }
581 
582 int
583 dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
584     boolean_t readonly, void *tag, objset_t **osp)
585 {
586 	dsl_dataset_t *ds;
587 	int err;
588 
589 	err = dsl_dataset_own_obj(dp, obj, tag, &ds);
590 	if (err != 0)
591 		return (err);
592 
593 	return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
594 }
595 
596 void
597 dmu_objset_rele(objset_t *os, void *tag)
598 {
599 	dsl_pool_t *dp = dmu_objset_pool(os);
600 	dsl_dataset_rele(os->os_dsl_dataset, tag);
601 	dsl_pool_rele(dp, tag);
602 }
603 
604 /*
605  * When we are called, os MUST refer to an objset associated with a dataset
606  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
607  * == tag.  We will then release and reacquire ownership of the dataset while
608  * holding the pool config_rwlock to avoid intervening namespace or ownership
609  * changes may occur.
610  *
611  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
612  * release the hold on its dataset and acquire a new one on the dataset of the
613  * same name so that it can be partially torn down and reconstructed.
614  */
615 void
616 dmu_objset_refresh_ownership(objset_t *os, void *tag)
617 {
618 	dsl_pool_t *dp;
619 	dsl_dataset_t *ds, *newds;
620 	char name[ZFS_MAX_DATASET_NAME_LEN];
621 
622 	ds = os->os_dsl_dataset;
623 	VERIFY3P(ds, !=, NULL);
624 	VERIFY3P(ds->ds_owner, ==, tag);
625 	VERIFY(dsl_dataset_long_held(ds));
626 
627 	dsl_dataset_name(ds, name);
628 	dp = dmu_objset_pool(os);
629 	dsl_pool_config_enter(dp, FTAG);
630 	dmu_objset_disown(os, tag);
631 	VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
632 	VERIFY3P(newds, ==, os->os_dsl_dataset);
633 	dsl_pool_config_exit(dp, FTAG);
634 }
635 
636 void
637 dmu_objset_disown(objset_t *os, void *tag)
638 {
639 	dsl_dataset_disown(os->os_dsl_dataset, tag);
640 }
641 
642 void
643 dmu_objset_evict_dbufs(objset_t *os)
644 {
645 	dnode_t dn_marker;
646 	dnode_t *dn;
647 
648 	mutex_enter(&os->os_lock);
649 	dn = list_head(&os->os_dnodes);
650 	while (dn != NULL) {
651 		/*
652 		 * Skip dnodes without holds.  We have to do this dance
653 		 * because dnode_add_ref() only works if there is already a
654 		 * hold.  If the dnode has no holds, then it has no dbufs.
655 		 */
656 		if (dnode_add_ref(dn, FTAG)) {
657 			list_insert_after(&os->os_dnodes, dn, &dn_marker);
658 			mutex_exit(&os->os_lock);
659 
660 			dnode_evict_dbufs(dn);
661 			dnode_rele(dn, FTAG);
662 
663 			mutex_enter(&os->os_lock);
664 			dn = list_next(&os->os_dnodes, &dn_marker);
665 			list_remove(&os->os_dnodes, &dn_marker);
666 		} else {
667 			dn = list_next(&os->os_dnodes, dn);
668 		}
669 	}
670 	mutex_exit(&os->os_lock);
671 
672 	if (DMU_USERUSED_DNODE(os) != NULL) {
673 		dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
674 		dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
675 	}
676 	dnode_evict_dbufs(DMU_META_DNODE(os));
677 }
678 
679 /*
680  * Objset eviction processing is split into into two pieces.
681  * The first marks the objset as evicting, evicts any dbufs that
682  * have a refcount of zero, and then queues up the objset for the
683  * second phase of eviction.  Once os->os_dnodes has been cleared by
684  * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
685  * The second phase closes the special dnodes, dequeues the objset from
686  * the list of those undergoing eviction, and finally frees the objset.
687  *
688  * NOTE: Due to asynchronous eviction processing (invocation of
689  *       dnode_buf_pageout()), it is possible for the meta dnode for the
690  *       objset to have no holds even though os->os_dnodes is not empty.
691  */
692 void
693 dmu_objset_evict(objset_t *os)
694 {
695 	dsl_dataset_t *ds = os->os_dsl_dataset;
696 
697 	for (int t = 0; t < TXG_SIZE; t++)
698 		ASSERT(!dmu_objset_is_dirty(os, t));
699 
700 	if (ds)
701 		dsl_prop_unregister_all(ds, os);
702 
703 	if (os->os_sa)
704 		sa_tear_down(os);
705 
706 	dmu_objset_evict_dbufs(os);
707 
708 	mutex_enter(&os->os_lock);
709 	spa_evicting_os_register(os->os_spa, os);
710 	if (list_is_empty(&os->os_dnodes)) {
711 		mutex_exit(&os->os_lock);
712 		dmu_objset_evict_done(os);
713 	} else {
714 		mutex_exit(&os->os_lock);
715 	}
716 }
717 
718 void
719 dmu_objset_evict_done(objset_t *os)
720 {
721 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
722 
723 	dnode_special_close(&os->os_meta_dnode);
724 	if (DMU_USERUSED_DNODE(os)) {
725 		dnode_special_close(&os->os_userused_dnode);
726 		dnode_special_close(&os->os_groupused_dnode);
727 	}
728 	zil_free(os->os_zil);
729 
730 	arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
731 
732 	/*
733 	 * This is a barrier to prevent the objset from going away in
734 	 * dnode_move() until we can safely ensure that the objset is still in
735 	 * use. We consider the objset valid before the barrier and invalid
736 	 * after the barrier.
737 	 */
738 	rw_enter(&os_lock, RW_READER);
739 	rw_exit(&os_lock);
740 
741 	mutex_destroy(&os->os_lock);
742 	mutex_destroy(&os->os_obj_lock);
743 	mutex_destroy(&os->os_user_ptr_lock);
744 	spa_evicting_os_deregister(os->os_spa, os);
745 	kmem_free(os, sizeof (objset_t));
746 }
747 
748 timestruc_t
749 dmu_objset_snap_cmtime(objset_t *os)
750 {
751 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
752 }
753 
754 /* called from dsl for meta-objset */
755 objset_t *
756 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
757     dmu_objset_type_t type, dmu_tx_t *tx)
758 {
759 	objset_t *os;
760 	dnode_t *mdn;
761 
762 	ASSERT(dmu_tx_is_syncing(tx));
763 
764 	if (ds != NULL)
765 		VERIFY0(dmu_objset_from_ds(ds, &os));
766 	else
767 		VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
768 
769 	mdn = DMU_META_DNODE(os);
770 
771 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
772 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
773 
774 	/*
775 	 * We don't want to have to increase the meta-dnode's nlevels
776 	 * later, because then we could do it in quescing context while
777 	 * we are also accessing it in open context.
778 	 *
779 	 * This precaution is not necessary for the MOS (ds == NULL),
780 	 * because the MOS is only updated in syncing context.
781 	 * This is most fortunate: the MOS is the only objset that
782 	 * needs to be synced multiple times as spa_sync() iterates
783 	 * to convergence, so minimizing its dn_nlevels matters.
784 	 */
785 	if (ds != NULL) {
786 		int levels = 1;
787 
788 		/*
789 		 * Determine the number of levels necessary for the meta-dnode
790 		 * to contain DN_MAX_OBJECT dnodes.
791 		 */
792 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
793 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
794 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
795 			levels++;
796 
797 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
798 		    mdn->dn_nlevels = levels;
799 	}
800 
801 	ASSERT(type != DMU_OST_NONE);
802 	ASSERT(type != DMU_OST_ANY);
803 	ASSERT(type < DMU_OST_NUMTYPES);
804 	os->os_phys->os_type = type;
805 	if (dmu_objset_userused_enabled(os)) {
806 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
807 		os->os_flags = os->os_phys->os_flags;
808 	}
809 
810 	dsl_dataset_dirty(ds, tx);
811 
812 	return (os);
813 }
814 
815 typedef struct dmu_objset_create_arg {
816 	const char *doca_name;
817 	cred_t *doca_cred;
818 	void (*doca_userfunc)(objset_t *os, void *arg,
819 	    cred_t *cr, dmu_tx_t *tx);
820 	void *doca_userarg;
821 	dmu_objset_type_t doca_type;
822 	uint64_t doca_flags;
823 } dmu_objset_create_arg_t;
824 
825 /*ARGSUSED*/
826 static int
827 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
828 {
829 	dmu_objset_create_arg_t *doca = arg;
830 	dsl_pool_t *dp = dmu_tx_pool(tx);
831 	dsl_dir_t *pdd;
832 	const char *tail;
833 	int error;
834 
835 	if (strchr(doca->doca_name, '@') != NULL)
836 		return (SET_ERROR(EINVAL));
837 
838 	if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
839 		return (SET_ERROR(ENAMETOOLONG));
840 
841 	error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
842 	if (error != 0)
843 		return (error);
844 	if (tail == NULL) {
845 		dsl_dir_rele(pdd, FTAG);
846 		return (SET_ERROR(EEXIST));
847 	}
848 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
849 	    doca->doca_cred);
850 	dsl_dir_rele(pdd, FTAG);
851 
852 	return (error);
853 }
854 
855 static void
856 dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
857 {
858 	dmu_objset_create_arg_t *doca = arg;
859 	dsl_pool_t *dp = dmu_tx_pool(tx);
860 	dsl_dir_t *pdd;
861 	const char *tail;
862 	dsl_dataset_t *ds;
863 	uint64_t obj;
864 	blkptr_t *bp;
865 	objset_t *os;
866 
867 	VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
868 
869 	obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
870 	    doca->doca_cred, tx);
871 
872 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
873 	bp = dsl_dataset_get_blkptr(ds);
874 	os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
875 	    ds, bp, doca->doca_type, tx);
876 
877 	if (doca->doca_userfunc != NULL) {
878 		doca->doca_userfunc(os, doca->doca_userarg,
879 		    doca->doca_cred, tx);
880 	}
881 
882 	spa_history_log_internal_ds(ds, "create", tx, "");
883 	dsl_dataset_rele(ds, FTAG);
884 	dsl_dir_rele(pdd, FTAG);
885 }
886 
887 int
888 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
889     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
890 {
891 	dmu_objset_create_arg_t doca;
892 
893 	doca.doca_name = name;
894 	doca.doca_cred = CRED();
895 	doca.doca_flags = flags;
896 	doca.doca_userfunc = func;
897 	doca.doca_userarg = arg;
898 	doca.doca_type = type;
899 
900 	return (dsl_sync_task(name,
901 	    dmu_objset_create_check, dmu_objset_create_sync, &doca,
902 	    5, ZFS_SPACE_CHECK_NORMAL));
903 }
904 
905 typedef struct dmu_objset_clone_arg {
906 	const char *doca_clone;
907 	const char *doca_origin;
908 	cred_t *doca_cred;
909 } dmu_objset_clone_arg_t;
910 
911 /*ARGSUSED*/
912 static int
913 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
914 {
915 	dmu_objset_clone_arg_t *doca = arg;
916 	dsl_dir_t *pdd;
917 	const char *tail;
918 	int error;
919 	dsl_dataset_t *origin;
920 	dsl_pool_t *dp = dmu_tx_pool(tx);
921 
922 	if (strchr(doca->doca_clone, '@') != NULL)
923 		return (SET_ERROR(EINVAL));
924 
925 	if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
926 		return (SET_ERROR(ENAMETOOLONG));
927 
928 	error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
929 	if (error != 0)
930 		return (error);
931 	if (tail == NULL) {
932 		dsl_dir_rele(pdd, FTAG);
933 		return (SET_ERROR(EEXIST));
934 	}
935 
936 	error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
937 	    doca->doca_cred);
938 	if (error != 0) {
939 		dsl_dir_rele(pdd, FTAG);
940 		return (SET_ERROR(EDQUOT));
941 	}
942 	dsl_dir_rele(pdd, FTAG);
943 
944 	error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
945 	if (error != 0)
946 		return (error);
947 
948 	/* You can only clone snapshots, not the head datasets. */
949 	if (!origin->ds_is_snapshot) {
950 		dsl_dataset_rele(origin, FTAG);
951 		return (SET_ERROR(EINVAL));
952 	}
953 	dsl_dataset_rele(origin, FTAG);
954 
955 	return (0);
956 }
957 
958 static void
959 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
960 {
961 	dmu_objset_clone_arg_t *doca = arg;
962 	dsl_pool_t *dp = dmu_tx_pool(tx);
963 	dsl_dir_t *pdd;
964 	const char *tail;
965 	dsl_dataset_t *origin, *ds;
966 	uint64_t obj;
967 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
968 
969 	VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
970 	VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
971 
972 	obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
973 	    doca->doca_cred, tx);
974 
975 	VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
976 	dsl_dataset_name(origin, namebuf);
977 	spa_history_log_internal_ds(ds, "clone", tx,
978 	    "origin=%s (%llu)", namebuf, origin->ds_object);
979 	dsl_dataset_rele(ds, FTAG);
980 	dsl_dataset_rele(origin, FTAG);
981 	dsl_dir_rele(pdd, FTAG);
982 }
983 
984 int
985 dmu_objset_clone(const char *clone, const char *origin)
986 {
987 	dmu_objset_clone_arg_t doca;
988 
989 	doca.doca_clone = clone;
990 	doca.doca_origin = origin;
991 	doca.doca_cred = CRED();
992 
993 	return (dsl_sync_task(clone,
994 	    dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
995 	    5, ZFS_SPACE_CHECK_NORMAL));
996 }
997 
998 int
999 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
1000 {
1001 	int err;
1002 	char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1003 	nvlist_t *snaps = fnvlist_alloc();
1004 
1005 	fnvlist_add_boolean(snaps, longsnap);
1006 	strfree(longsnap);
1007 	err = dsl_dataset_snapshot(snaps, NULL, NULL);
1008 	fnvlist_free(snaps);
1009 	return (err);
1010 }
1011 
1012 static void
1013 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
1014 {
1015 	dnode_t *dn;
1016 
1017 	while (dn = list_head(list)) {
1018 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1019 		ASSERT(dn->dn_dbuf->db_data_pending);
1020 		/*
1021 		 * Initialize dn_zio outside dnode_sync() because the
1022 		 * meta-dnode needs to set it ouside dnode_sync().
1023 		 */
1024 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1025 		ASSERT(dn->dn_zio);
1026 
1027 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1028 		list_remove(list, dn);
1029 
1030 		if (newlist) {
1031 			(void) dnode_add_ref(dn, newlist);
1032 			list_insert_tail(newlist, dn);
1033 		}
1034 
1035 		dnode_sync(dn, tx);
1036 	}
1037 }
1038 
1039 /* ARGSUSED */
1040 static void
1041 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1042 {
1043 	blkptr_t *bp = zio->io_bp;
1044 	objset_t *os = arg;
1045 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1046 
1047 	ASSERT(!BP_IS_EMBEDDED(bp));
1048 	ASSERT3P(bp, ==, os->os_rootbp);
1049 	ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1050 	ASSERT0(BP_GET_LEVEL(bp));
1051 
1052 	/*
1053 	 * Update rootbp fill count: it should be the number of objects
1054 	 * allocated in the object set (not counting the "special"
1055 	 * objects that are stored in the objset_phys_t -- the meta
1056 	 * dnode and user/group accounting objects).
1057 	 */
1058 	bp->blk_fill = 0;
1059 	for (int i = 0; i < dnp->dn_nblkptr; i++)
1060 		bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1061 }
1062 
1063 /* ARGSUSED */
1064 static void
1065 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1066 {
1067 	blkptr_t *bp = zio->io_bp;
1068 	blkptr_t *bp_orig = &zio->io_bp_orig;
1069 	objset_t *os = arg;
1070 
1071 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1072 		ASSERT(BP_EQUAL(bp, bp_orig));
1073 	} else {
1074 		dsl_dataset_t *ds = os->os_dsl_dataset;
1075 		dmu_tx_t *tx = os->os_synctx;
1076 
1077 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1078 		dsl_dataset_block_born(ds, bp, tx);
1079 	}
1080 }
1081 
1082 /* called from dsl */
1083 void
1084 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1085 {
1086 	int txgoff;
1087 	zbookmark_phys_t zb;
1088 	zio_prop_t zp;
1089 	zio_t *zio;
1090 	list_t *list;
1091 	list_t *newlist = NULL;
1092 	dbuf_dirty_record_t *dr;
1093 
1094 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1095 
1096 	ASSERT(dmu_tx_is_syncing(tx));
1097 	/* XXX the write_done callback should really give us the tx... */
1098 	os->os_synctx = tx;
1099 
1100 	if (os->os_dsl_dataset == NULL) {
1101 		/*
1102 		 * This is the MOS.  If we have upgraded,
1103 		 * spa_max_replication() could change, so reset
1104 		 * os_copies here.
1105 		 */
1106 		os->os_copies = spa_max_replication(os->os_spa);
1107 	}
1108 
1109 	/*
1110 	 * Create the root block IO
1111 	 */
1112 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1113 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1114 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1115 	arc_release(os->os_phys_buf, &os->os_phys_buf);
1116 
1117 	dmu_write_policy(os, NULL, 0, 0, &zp);
1118 
1119 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1120 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1121 	    &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
1122 	    os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1123 
1124 	/*
1125 	 * Sync special dnodes - the parent IO for the sync is the root block
1126 	 */
1127 	DMU_META_DNODE(os)->dn_zio = zio;
1128 	dnode_sync(DMU_META_DNODE(os), tx);
1129 
1130 	os->os_phys->os_flags = os->os_flags;
1131 
1132 	if (DMU_USERUSED_DNODE(os) &&
1133 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1134 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1135 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1136 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1137 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1138 	}
1139 
1140 	txgoff = tx->tx_txg & TXG_MASK;
1141 
1142 	if (dmu_objset_userused_enabled(os)) {
1143 		newlist = &os->os_synced_dnodes;
1144 		/*
1145 		 * We must create the list here because it uses the
1146 		 * dn_dirty_link[] of this txg.
1147 		 */
1148 		list_create(newlist, sizeof (dnode_t),
1149 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
1150 	}
1151 
1152 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1153 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1154 
1155 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1156 	while (dr = list_head(list)) {
1157 		ASSERT0(dr->dr_dbuf->db_level);
1158 		list_remove(list, dr);
1159 		if (dr->dr_zio)
1160 			zio_nowait(dr->dr_zio);
1161 	}
1162 	/*
1163 	 * Free intent log blocks up to this tx.
1164 	 */
1165 	zil_sync(os->os_zil, tx);
1166 	os->os_phys->os_zil_header = os->os_zil_header;
1167 	zio_nowait(zio);
1168 }
1169 
1170 boolean_t
1171 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1172 {
1173 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1174 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1175 }
1176 
1177 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1178 
1179 void
1180 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1181 {
1182 	used_cbs[ost] = cb;
1183 }
1184 
1185 boolean_t
1186 dmu_objset_userused_enabled(objset_t *os)
1187 {
1188 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1189 	    used_cbs[os->os_phys->os_type] != NULL &&
1190 	    DMU_USERUSED_DNODE(os) != NULL);
1191 }
1192 
1193 static void
1194 do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1195     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
1196 {
1197 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1198 		int64_t delta = DNODE_SIZE + used;
1199 		if (subtract)
1200 			delta = -delta;
1201 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
1202 		    user, delta, tx));
1203 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1204 		    group, delta, tx));
1205 	}
1206 }
1207 
1208 void
1209 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1210 {
1211 	dnode_t *dn;
1212 	list_t *list = &os->os_synced_dnodes;
1213 
1214 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1215 
1216 	while (dn = list_head(list)) {
1217 		int flags;
1218 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1219 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1220 		    dn->dn_phys->dn_flags &
1221 		    DNODE_FLAG_USERUSED_ACCOUNTED);
1222 
1223 		/* Allocate the user/groupused objects if necessary. */
1224 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1225 			VERIFY(0 == zap_create_claim(os,
1226 			    DMU_USERUSED_OBJECT,
1227 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1228 			VERIFY(0 == zap_create_claim(os,
1229 			    DMU_GROUPUSED_OBJECT,
1230 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1231 		}
1232 
1233 		/*
1234 		 * We intentionally modify the zap object even if the
1235 		 * net delta is zero.  Otherwise
1236 		 * the block of the zap obj could be shared between
1237 		 * datasets but need to be different between them after
1238 		 * a bprewrite.
1239 		 */
1240 
1241 		flags = dn->dn_id_flags;
1242 		ASSERT(flags);
1243 		if (flags & DN_ID_OLD_EXIST)  {
1244 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1245 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1246 		}
1247 		if (flags & DN_ID_NEW_EXIST) {
1248 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1249 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
1250 			    dn->dn_newgid, B_FALSE, tx);
1251 		}
1252 
1253 		mutex_enter(&dn->dn_mtx);
1254 		dn->dn_oldused = 0;
1255 		dn->dn_oldflags = 0;
1256 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1257 			dn->dn_olduid = dn->dn_newuid;
1258 			dn->dn_oldgid = dn->dn_newgid;
1259 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
1260 			if (dn->dn_bonuslen == 0)
1261 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1262 			else
1263 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1264 		}
1265 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1266 		mutex_exit(&dn->dn_mtx);
1267 
1268 		list_remove(list, dn);
1269 		dnode_rele(dn, list);
1270 	}
1271 }
1272 
1273 /*
1274  * Returns a pointer to data to find uid/gid from
1275  *
1276  * If a dirty record for transaction group that is syncing can't
1277  * be found then NULL is returned.  In the NULL case it is assumed
1278  * the uid/gid aren't changing.
1279  */
1280 static void *
1281 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1282 {
1283 	dbuf_dirty_record_t *dr, **drp;
1284 	void *data;
1285 
1286 	if (db->db_dirtycnt == 0)
1287 		return (db->db.db_data);  /* Nothing is changing */
1288 
1289 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1290 		if (dr->dr_txg == tx->tx_txg)
1291 			break;
1292 
1293 	if (dr == NULL) {
1294 		data = NULL;
1295 	} else {
1296 		dnode_t *dn;
1297 
1298 		DB_DNODE_ENTER(dr->dr_dbuf);
1299 		dn = DB_DNODE(dr->dr_dbuf);
1300 
1301 		if (dn->dn_bonuslen == 0 &&
1302 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1303 			data = dr->dt.dl.dr_data->b_data;
1304 		else
1305 			data = dr->dt.dl.dr_data;
1306 
1307 		DB_DNODE_EXIT(dr->dr_dbuf);
1308 	}
1309 
1310 	return (data);
1311 }
1312 
1313 void
1314 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1315 {
1316 	objset_t *os = dn->dn_objset;
1317 	void *data = NULL;
1318 	dmu_buf_impl_t *db = NULL;
1319 	uint64_t *user = NULL;
1320 	uint64_t *group = NULL;
1321 	int flags = dn->dn_id_flags;
1322 	int error;
1323 	boolean_t have_spill = B_FALSE;
1324 
1325 	if (!dmu_objset_userused_enabled(dn->dn_objset))
1326 		return;
1327 
1328 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1329 	    DN_ID_CHKED_SPILL)))
1330 		return;
1331 
1332 	if (before && dn->dn_bonuslen != 0)
1333 		data = DN_BONUS(dn->dn_phys);
1334 	else if (!before && dn->dn_bonuslen != 0) {
1335 		if (dn->dn_bonus) {
1336 			db = dn->dn_bonus;
1337 			mutex_enter(&db->db_mtx);
1338 			data = dmu_objset_userquota_find_data(db, tx);
1339 		} else {
1340 			data = DN_BONUS(dn->dn_phys);
1341 		}
1342 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1343 			int rf = 0;
1344 
1345 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1346 				rf |= DB_RF_HAVESTRUCT;
1347 			error = dmu_spill_hold_by_dnode(dn,
1348 			    rf | DB_RF_MUST_SUCCEED,
1349 			    FTAG, (dmu_buf_t **)&db);
1350 			ASSERT(error == 0);
1351 			mutex_enter(&db->db_mtx);
1352 			data = (before) ? db->db.db_data :
1353 			    dmu_objset_userquota_find_data(db, tx);
1354 			have_spill = B_TRUE;
1355 	} else {
1356 		mutex_enter(&dn->dn_mtx);
1357 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1358 		mutex_exit(&dn->dn_mtx);
1359 		return;
1360 	}
1361 
1362 	if (before) {
1363 		ASSERT(data);
1364 		user = &dn->dn_olduid;
1365 		group = &dn->dn_oldgid;
1366 	} else if (data) {
1367 		user = &dn->dn_newuid;
1368 		group = &dn->dn_newgid;
1369 	}
1370 
1371 	/*
1372 	 * Must always call the callback in case the object
1373 	 * type has changed and that type isn't an object type to track
1374 	 */
1375 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1376 	    user, group);
1377 
1378 	/*
1379 	 * Preserve existing uid/gid when the callback can't determine
1380 	 * what the new uid/gid are and the callback returned EEXIST.
1381 	 * The EEXIST error tells us to just use the existing uid/gid.
1382 	 * If we don't know what the old values are then just assign
1383 	 * them to 0, since that is a new file  being created.
1384 	 */
1385 	if (!before && data == NULL && error == EEXIST) {
1386 		if (flags & DN_ID_OLD_EXIST) {
1387 			dn->dn_newuid = dn->dn_olduid;
1388 			dn->dn_newgid = dn->dn_oldgid;
1389 		} else {
1390 			dn->dn_newuid = 0;
1391 			dn->dn_newgid = 0;
1392 		}
1393 		error = 0;
1394 	}
1395 
1396 	if (db)
1397 		mutex_exit(&db->db_mtx);
1398 
1399 	mutex_enter(&dn->dn_mtx);
1400 	if (error == 0 && before)
1401 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
1402 	if (error == 0 && !before)
1403 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
1404 
1405 	if (have_spill) {
1406 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1407 	} else {
1408 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1409 	}
1410 	mutex_exit(&dn->dn_mtx);
1411 	if (have_spill)
1412 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
1413 }
1414 
1415 boolean_t
1416 dmu_objset_userspace_present(objset_t *os)
1417 {
1418 	return (os->os_phys->os_flags &
1419 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1420 }
1421 
1422 int
1423 dmu_objset_userspace_upgrade(objset_t *os)
1424 {
1425 	uint64_t obj;
1426 	int err = 0;
1427 
1428 	if (dmu_objset_userspace_present(os))
1429 		return (0);
1430 	if (!dmu_objset_userused_enabled(os))
1431 		return (SET_ERROR(ENOTSUP));
1432 	if (dmu_objset_is_snapshot(os))
1433 		return (SET_ERROR(EINVAL));
1434 
1435 	/*
1436 	 * We simply need to mark every object dirty, so that it will be
1437 	 * synced out and now accounted.  If this is called
1438 	 * concurrently, or if we already did some work before crashing,
1439 	 * that's fine, since we track each object's accounted state
1440 	 * independently.
1441 	 */
1442 
1443 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1444 		dmu_tx_t *tx;
1445 		dmu_buf_t *db;
1446 		int objerr;
1447 
1448 		if (issig(JUSTLOOKING) && issig(FORREAL))
1449 			return (SET_ERROR(EINTR));
1450 
1451 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1452 		if (objerr != 0)
1453 			continue;
1454 		tx = dmu_tx_create(os);
1455 		dmu_tx_hold_bonus(tx, obj);
1456 		objerr = dmu_tx_assign(tx, TXG_WAIT);
1457 		if (objerr != 0) {
1458 			dmu_tx_abort(tx);
1459 			continue;
1460 		}
1461 		dmu_buf_will_dirty(db, tx);
1462 		dmu_buf_rele(db, FTAG);
1463 		dmu_tx_commit(tx);
1464 	}
1465 
1466 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1467 	txg_wait_synced(dmu_objset_pool(os), 0);
1468 	return (0);
1469 }
1470 
1471 void
1472 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1473     uint64_t *usedobjsp, uint64_t *availobjsp)
1474 {
1475 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1476 	    usedobjsp, availobjsp);
1477 }
1478 
1479 uint64_t
1480 dmu_objset_fsid_guid(objset_t *os)
1481 {
1482 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1483 }
1484 
1485 void
1486 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1487 {
1488 	stat->dds_type = os->os_phys->os_type;
1489 	if (os->os_dsl_dataset)
1490 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1491 }
1492 
1493 void
1494 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1495 {
1496 	ASSERT(os->os_dsl_dataset ||
1497 	    os->os_phys->os_type == DMU_OST_META);
1498 
1499 	if (os->os_dsl_dataset != NULL)
1500 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1501 
1502 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1503 	    os->os_phys->os_type);
1504 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1505 	    dmu_objset_userspace_present(os));
1506 }
1507 
1508 int
1509 dmu_objset_is_snapshot(objset_t *os)
1510 {
1511 	if (os->os_dsl_dataset != NULL)
1512 		return (os->os_dsl_dataset->ds_is_snapshot);
1513 	else
1514 		return (B_FALSE);
1515 }
1516 
1517 int
1518 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1519     boolean_t *conflict)
1520 {
1521 	dsl_dataset_t *ds = os->os_dsl_dataset;
1522 	uint64_t ignored;
1523 
1524 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1525 		return (SET_ERROR(ENOENT));
1526 
1527 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1528 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1529 	    MT_FIRST, real, maxlen, conflict));
1530 }
1531 
1532 int
1533 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1534     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1535 {
1536 	dsl_dataset_t *ds = os->os_dsl_dataset;
1537 	zap_cursor_t cursor;
1538 	zap_attribute_t attr;
1539 
1540 	ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1541 
1542 	if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1543 		return (SET_ERROR(ENOENT));
1544 
1545 	zap_cursor_init_serialized(&cursor,
1546 	    ds->ds_dir->dd_pool->dp_meta_objset,
1547 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1548 
1549 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1550 		zap_cursor_fini(&cursor);
1551 		return (SET_ERROR(ENOENT));
1552 	}
1553 
1554 	if (strlen(attr.za_name) + 1 > namelen) {
1555 		zap_cursor_fini(&cursor);
1556 		return (SET_ERROR(ENAMETOOLONG));
1557 	}
1558 
1559 	(void) strcpy(name, attr.za_name);
1560 	if (idp)
1561 		*idp = attr.za_first_integer;
1562 	if (case_conflict)
1563 		*case_conflict = attr.za_normalization_conflict;
1564 	zap_cursor_advance(&cursor);
1565 	*offp = zap_cursor_serialize(&cursor);
1566 	zap_cursor_fini(&cursor);
1567 
1568 	return (0);
1569 }
1570 
1571 int
1572 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1573     uint64_t *idp, uint64_t *offp)
1574 {
1575 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1576 	zap_cursor_t cursor;
1577 	zap_attribute_t attr;
1578 
1579 	/* there is no next dir on a snapshot! */
1580 	if (os->os_dsl_dataset->ds_object !=
1581 	    dsl_dir_phys(dd)->dd_head_dataset_obj)
1582 		return (SET_ERROR(ENOENT));
1583 
1584 	zap_cursor_init_serialized(&cursor,
1585 	    dd->dd_pool->dp_meta_objset,
1586 	    dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
1587 
1588 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1589 		zap_cursor_fini(&cursor);
1590 		return (SET_ERROR(ENOENT));
1591 	}
1592 
1593 	if (strlen(attr.za_name) + 1 > namelen) {
1594 		zap_cursor_fini(&cursor);
1595 		return (SET_ERROR(ENAMETOOLONG));
1596 	}
1597 
1598 	(void) strcpy(name, attr.za_name);
1599 	if (idp)
1600 		*idp = attr.za_first_integer;
1601 	zap_cursor_advance(&cursor);
1602 	*offp = zap_cursor_serialize(&cursor);
1603 	zap_cursor_fini(&cursor);
1604 
1605 	return (0);
1606 }
1607 
1608 typedef struct dmu_objset_find_ctx {
1609 	taskq_t		*dc_tq;
1610 	dsl_pool_t	*dc_dp;
1611 	uint64_t	dc_ddobj;
1612 	int		(*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
1613 	void		*dc_arg;
1614 	int		dc_flags;
1615 	kmutex_t	*dc_error_lock;
1616 	int		*dc_error;
1617 } dmu_objset_find_ctx_t;
1618 
1619 static void
1620 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
1621 {
1622 	dsl_pool_t *dp = dcp->dc_dp;
1623 	dmu_objset_find_ctx_t *child_dcp;
1624 	dsl_dir_t *dd;
1625 	dsl_dataset_t *ds;
1626 	zap_cursor_t zc;
1627 	zap_attribute_t *attr;
1628 	uint64_t thisobj;
1629 	int err = 0;
1630 
1631 	/* don't process if there already was an error */
1632 	if (*dcp->dc_error != 0)
1633 		goto out;
1634 
1635 	err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd);
1636 	if (err != 0)
1637 		goto out;
1638 
1639 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1640 	if (dd->dd_myname[0] == '$') {
1641 		dsl_dir_rele(dd, FTAG);
1642 		goto out;
1643 	}
1644 
1645 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1646 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1647 
1648 	/*
1649 	 * Iterate over all children.
1650 	 */
1651 	if (dcp->dc_flags & DS_FIND_CHILDREN) {
1652 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1653 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
1654 		    zap_cursor_retrieve(&zc, attr) == 0;
1655 		    (void) zap_cursor_advance(&zc)) {
1656 			ASSERT3U(attr->za_integer_length, ==,
1657 			    sizeof (uint64_t));
1658 			ASSERT3U(attr->za_num_integers, ==, 1);
1659 
1660 			child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
1661 			*child_dcp = *dcp;
1662 			child_dcp->dc_ddobj = attr->za_first_integer;
1663 			if (dcp->dc_tq != NULL)
1664 				(void) taskq_dispatch(dcp->dc_tq,
1665 				    dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
1666 			else
1667 				dmu_objset_find_dp_impl(child_dcp);
1668 		}
1669 		zap_cursor_fini(&zc);
1670 	}
1671 
1672 	/*
1673 	 * Iterate over all snapshots.
1674 	 */
1675 	if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
1676 		dsl_dataset_t *ds;
1677 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1678 
1679 		if (err == 0) {
1680 			uint64_t snapobj;
1681 
1682 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1683 			dsl_dataset_rele(ds, FTAG);
1684 
1685 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1686 			    zap_cursor_retrieve(&zc, attr) == 0;
1687 			    (void) zap_cursor_advance(&zc)) {
1688 				ASSERT3U(attr->za_integer_length, ==,
1689 				    sizeof (uint64_t));
1690 				ASSERT3U(attr->za_num_integers, ==, 1);
1691 
1692 				err = dsl_dataset_hold_obj(dp,
1693 				    attr->za_first_integer, FTAG, &ds);
1694 				if (err != 0)
1695 					break;
1696 				err = dcp->dc_func(dp, ds, dcp->dc_arg);
1697 				dsl_dataset_rele(ds, FTAG);
1698 				if (err != 0)
1699 					break;
1700 			}
1701 			zap_cursor_fini(&zc);
1702 		}
1703 	}
1704 
1705 	dsl_dir_rele(dd, FTAG);
1706 	kmem_free(attr, sizeof (zap_attribute_t));
1707 
1708 	if (err != 0)
1709 		goto out;
1710 
1711 	/*
1712 	 * Apply to self.
1713 	 */
1714 	err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1715 	if (err != 0)
1716 		goto out;
1717 	err = dcp->dc_func(dp, ds, dcp->dc_arg);
1718 	dsl_dataset_rele(ds, FTAG);
1719 
1720 out:
1721 	if (err != 0) {
1722 		mutex_enter(dcp->dc_error_lock);
1723 		/* only keep first error */
1724 		if (*dcp->dc_error == 0)
1725 			*dcp->dc_error = err;
1726 		mutex_exit(dcp->dc_error_lock);
1727 	}
1728 
1729 	kmem_free(dcp, sizeof (*dcp));
1730 }
1731 
1732 static void
1733 dmu_objset_find_dp_cb(void *arg)
1734 {
1735 	dmu_objset_find_ctx_t *dcp = arg;
1736 	dsl_pool_t *dp = dcp->dc_dp;
1737 
1738 	/*
1739 	 * We need to get a pool_config_lock here, as there are several
1740 	 * asssert(pool_config_held) down the stack. Getting a lock via
1741 	 * dsl_pool_config_enter is risky, as it might be stalled by a
1742 	 * pending writer. This would deadlock, as the write lock can
1743 	 * only be granted when our parent thread gives up the lock.
1744 	 * The _prio interface gives us priority over a pending writer.
1745 	 */
1746 	dsl_pool_config_enter_prio(dp, FTAG);
1747 
1748 	dmu_objset_find_dp_impl(dcp);
1749 
1750 	dsl_pool_config_exit(dp, FTAG);
1751 }
1752 
1753 /*
1754  * Find objsets under and including ddobj, call func(ds) on each.
1755  * The order for the enumeration is completely undefined.
1756  * func is called with dsl_pool_config held.
1757  */
1758 int
1759 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
1760     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1761 {
1762 	int error = 0;
1763 	taskq_t *tq = NULL;
1764 	int ntasks;
1765 	dmu_objset_find_ctx_t *dcp;
1766 	kmutex_t err_lock;
1767 
1768 	mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
1769 	dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
1770 	dcp->dc_tq = NULL;
1771 	dcp->dc_dp = dp;
1772 	dcp->dc_ddobj = ddobj;
1773 	dcp->dc_func = func;
1774 	dcp->dc_arg = arg;
1775 	dcp->dc_flags = flags;
1776 	dcp->dc_error_lock = &err_lock;
1777 	dcp->dc_error = &error;
1778 
1779 	if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
1780 		/*
1781 		 * In case a write lock is held we can't make use of
1782 		 * parallelism, as down the stack of the worker threads
1783 		 * the lock is asserted via dsl_pool_config_held.
1784 		 * In case of a read lock this is solved by getting a read
1785 		 * lock in each worker thread, which isn't possible in case
1786 		 * of a writer lock. So we fall back to the synchronous path
1787 		 * here.
1788 		 * In the future it might be possible to get some magic into
1789 		 * dsl_pool_config_held in a way that it returns true for
1790 		 * the worker threads so that a single lock held from this
1791 		 * thread suffices. For now, stay single threaded.
1792 		 */
1793 		dmu_objset_find_dp_impl(dcp);
1794 		mutex_destroy(&err_lock);
1795 
1796 		return (error);
1797 	}
1798 
1799 	ntasks = dmu_find_threads;
1800 	if (ntasks == 0)
1801 		ntasks = vdev_count_leaves(dp->dp_spa) * 4;
1802 	tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks,
1803 	    INT_MAX, 0);
1804 	if (tq == NULL) {
1805 		kmem_free(dcp, sizeof (*dcp));
1806 		mutex_destroy(&err_lock);
1807 
1808 		return (SET_ERROR(ENOMEM));
1809 	}
1810 	dcp->dc_tq = tq;
1811 
1812 	/* dcp will be freed by task */
1813 	(void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
1814 
1815 	/*
1816 	 * PORTING: this code relies on the property of taskq_wait to wait
1817 	 * until no more tasks are queued and no more tasks are active. As
1818 	 * we always queue new tasks from within other tasks, task_wait
1819 	 * reliably waits for the full recursion to finish, even though we
1820 	 * enqueue new tasks after taskq_wait has been called.
1821 	 * On platforms other than illumos, taskq_wait may not have this
1822 	 * property.
1823 	 */
1824 	taskq_wait(tq);
1825 	taskq_destroy(tq);
1826 	mutex_destroy(&err_lock);
1827 
1828 	return (error);
1829 }
1830 
1831 /*
1832  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1833  * The dp_config_rwlock must not be held when this is called, and it
1834  * will not be held when the callback is called.
1835  * Therefore this function should only be used when the pool is not changing
1836  * (e.g. in syncing context), or the callback can deal with the possible races.
1837  */
1838 static int
1839 dmu_objset_find_impl(spa_t *spa, const char *name,
1840     int func(const char *, void *), void *arg, int flags)
1841 {
1842 	dsl_dir_t *dd;
1843 	dsl_pool_t *dp = spa_get_dsl(spa);
1844 	dsl_dataset_t *ds;
1845 	zap_cursor_t zc;
1846 	zap_attribute_t *attr;
1847 	char *child;
1848 	uint64_t thisobj;
1849 	int err;
1850 
1851 	dsl_pool_config_enter(dp, FTAG);
1852 
1853 	err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
1854 	if (err != 0) {
1855 		dsl_pool_config_exit(dp, FTAG);
1856 		return (err);
1857 	}
1858 
1859 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1860 	if (dd->dd_myname[0] == '$') {
1861 		dsl_dir_rele(dd, FTAG);
1862 		dsl_pool_config_exit(dp, FTAG);
1863 		return (0);
1864 	}
1865 
1866 	thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1867 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1868 
1869 	/*
1870 	 * Iterate over all children.
1871 	 */
1872 	if (flags & DS_FIND_CHILDREN) {
1873 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1874 		    dsl_dir_phys(dd)->dd_child_dir_zapobj);
1875 		    zap_cursor_retrieve(&zc, attr) == 0;
1876 		    (void) zap_cursor_advance(&zc)) {
1877 			ASSERT3U(attr->za_integer_length, ==,
1878 			    sizeof (uint64_t));
1879 			ASSERT3U(attr->za_num_integers, ==, 1);
1880 
1881 			child = kmem_asprintf("%s/%s", name, attr->za_name);
1882 			dsl_pool_config_exit(dp, FTAG);
1883 			err = dmu_objset_find_impl(spa, child,
1884 			    func, arg, flags);
1885 			dsl_pool_config_enter(dp, FTAG);
1886 			strfree(child);
1887 			if (err != 0)
1888 				break;
1889 		}
1890 		zap_cursor_fini(&zc);
1891 
1892 		if (err != 0) {
1893 			dsl_dir_rele(dd, FTAG);
1894 			dsl_pool_config_exit(dp, FTAG);
1895 			kmem_free(attr, sizeof (zap_attribute_t));
1896 			return (err);
1897 		}
1898 	}
1899 
1900 	/*
1901 	 * Iterate over all snapshots.
1902 	 */
1903 	if (flags & DS_FIND_SNAPSHOTS) {
1904 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1905 
1906 		if (err == 0) {
1907 			uint64_t snapobj;
1908 
1909 			snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1910 			dsl_dataset_rele(ds, FTAG);
1911 
1912 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1913 			    zap_cursor_retrieve(&zc, attr) == 0;
1914 			    (void) zap_cursor_advance(&zc)) {
1915 				ASSERT3U(attr->za_integer_length, ==,
1916 				    sizeof (uint64_t));
1917 				ASSERT3U(attr->za_num_integers, ==, 1);
1918 
1919 				child = kmem_asprintf("%s@%s",
1920 				    name, attr->za_name);
1921 				dsl_pool_config_exit(dp, FTAG);
1922 				err = func(child, arg);
1923 				dsl_pool_config_enter(dp, FTAG);
1924 				strfree(child);
1925 				if (err != 0)
1926 					break;
1927 			}
1928 			zap_cursor_fini(&zc);
1929 		}
1930 	}
1931 
1932 	dsl_dir_rele(dd, FTAG);
1933 	kmem_free(attr, sizeof (zap_attribute_t));
1934 	dsl_pool_config_exit(dp, FTAG);
1935 
1936 	if (err != 0)
1937 		return (err);
1938 
1939 	/* Apply to self. */
1940 	return (func(name, arg));
1941 }
1942 
1943 /*
1944  * See comment above dmu_objset_find_impl().
1945  */
1946 int
1947 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
1948     int flags)
1949 {
1950 	spa_t *spa;
1951 	int error;
1952 
1953 	error = spa_open(name, &spa, FTAG);
1954 	if (error != 0)
1955 		return (error);
1956 	error = dmu_objset_find_impl(spa, name, func, arg, flags);
1957 	spa_close(spa, FTAG);
1958 	return (error);
1959 }
1960 
1961 void
1962 dmu_objset_set_user(objset_t *os, void *user_ptr)
1963 {
1964 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1965 	os->os_user_ptr = user_ptr;
1966 }
1967 
1968 void *
1969 dmu_objset_get_user(objset_t *os)
1970 {
1971 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1972 	return (os->os_user_ptr);
1973 }
1974 
1975 /*
1976  * Determine name of filesystem, given name of snapshot.
1977  * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
1978  */
1979 int
1980 dmu_fsname(const char *snapname, char *buf)
1981 {
1982 	char *atp = strchr(snapname, '@');
1983 	if (atp == NULL)
1984 		return (SET_ERROR(EINVAL));
1985 	if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
1986 		return (SET_ERROR(ENAMETOOLONG));
1987 	(void) strlcpy(buf, snapname, atp - snapname + 1);
1988 	return (0);
1989 }
1990