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