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