xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 77979b9b6355335c7ed83048268aa1e05e10855a)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/cred.h>
27 #include <sys/zfs_context.h>
28 #include <sys/dmu_objset.h>
29 #include <sys/dsl_dir.h>
30 #include <sys/dsl_dataset.h>
31 #include <sys/dsl_prop.h>
32 #include <sys/dsl_pool.h>
33 #include <sys/dsl_synctask.h>
34 #include <sys/dsl_deleg.h>
35 #include <sys/dnode.h>
36 #include <sys/dbuf.h>
37 #include <sys/zvol.h>
38 #include <sys/dmu_tx.h>
39 #include <sys/zio_checksum.h>
40 #include <sys/zap.h>
41 #include <sys/zil.h>
42 #include <sys/dmu_impl.h>
43 #include <sys/zfs_ioctl.h>
44 
45 spa_t *
46 dmu_objset_spa(objset_t *os)
47 {
48 	return (os->os_spa);
49 }
50 
51 zilog_t *
52 dmu_objset_zil(objset_t *os)
53 {
54 	return (os->os_zil);
55 }
56 
57 dsl_pool_t *
58 dmu_objset_pool(objset_t *os)
59 {
60 	dsl_dataset_t *ds;
61 
62 	if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
63 		return (ds->ds_dir->dd_pool);
64 	else
65 		return (spa_get_dsl(os->os_spa));
66 }
67 
68 dsl_dataset_t *
69 dmu_objset_ds(objset_t *os)
70 {
71 	return (os->os_dsl_dataset);
72 }
73 
74 dmu_objset_type_t
75 dmu_objset_type(objset_t *os)
76 {
77 	return (os->os_phys->os_type);
78 }
79 
80 void
81 dmu_objset_name(objset_t *os, char *buf)
82 {
83 	dsl_dataset_name(os->os_dsl_dataset, buf);
84 }
85 
86 uint64_t
87 dmu_objset_id(objset_t *os)
88 {
89 	dsl_dataset_t *ds = os->os_dsl_dataset;
90 
91 	return (ds ? ds->ds_object : 0);
92 }
93 
94 uint64_t
95 dmu_objset_logbias(objset_t *os)
96 {
97 	return (os->os_logbias);
98 }
99 
100 static void
101 checksum_changed_cb(void *arg, uint64_t newval)
102 {
103 	objset_t *os = arg;
104 
105 	/*
106 	 * Inheritance should have been done by now.
107 	 */
108 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
109 
110 	os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
111 }
112 
113 static void
114 compression_changed_cb(void *arg, uint64_t newval)
115 {
116 	objset_t *os = arg;
117 
118 	/*
119 	 * Inheritance and range checking should have been done by now.
120 	 */
121 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
122 
123 	os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
124 }
125 
126 static void
127 copies_changed_cb(void *arg, uint64_t newval)
128 {
129 	objset_t *os = arg;
130 
131 	/*
132 	 * Inheritance and range checking should have been done by now.
133 	 */
134 	ASSERT(newval > 0);
135 	ASSERT(newval <= spa_max_replication(os->os_spa));
136 
137 	os->os_copies = newval;
138 }
139 
140 static void
141 primary_cache_changed_cb(void *arg, uint64_t newval)
142 {
143 	objset_t *os = arg;
144 
145 	/*
146 	 * Inheritance and range checking should have been done by now.
147 	 */
148 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
149 	    newval == ZFS_CACHE_METADATA);
150 
151 	os->os_primary_cache = newval;
152 }
153 
154 static void
155 secondary_cache_changed_cb(void *arg, uint64_t newval)
156 {
157 	objset_t *os = arg;
158 
159 	/*
160 	 * Inheritance and range checking should have been done by now.
161 	 */
162 	ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
163 	    newval == ZFS_CACHE_METADATA);
164 
165 	os->os_secondary_cache = newval;
166 }
167 
168 static void
169 logbias_changed_cb(void *arg, uint64_t newval)
170 {
171 	objset_t *os = arg;
172 
173 	ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
174 	    newval == ZFS_LOGBIAS_THROUGHPUT);
175 	os->os_logbias = newval;
176 	if (os->os_zil)
177 		zil_set_logbias(os->os_zil, newval);
178 }
179 
180 void
181 dmu_objset_byteswap(void *buf, size_t size)
182 {
183 	objset_phys_t *osp = buf;
184 
185 	ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
186 	dnode_byteswap(&osp->os_meta_dnode);
187 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
188 	osp->os_type = BSWAP_64(osp->os_type);
189 	osp->os_flags = BSWAP_64(osp->os_flags);
190 	if (size == sizeof (objset_phys_t)) {
191 		dnode_byteswap(&osp->os_userused_dnode);
192 		dnode_byteswap(&osp->os_groupused_dnode);
193 	}
194 }
195 
196 int
197 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
198     objset_t **osp)
199 {
200 	objset_t *os;
201 	int i, err;
202 
203 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
204 
205 	os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
206 	os->os_dsl_dataset = ds;
207 	os->os_spa = spa;
208 	os->os_rootbp = bp;
209 	if (!BP_IS_HOLE(os->os_rootbp)) {
210 		uint32_t aflags = ARC_WAIT;
211 		zbookmark_t zb;
212 		zb.zb_objset = ds ? ds->ds_object : 0;
213 		zb.zb_object = 0;
214 		zb.zb_level = -1;
215 		zb.zb_blkid = 0;
216 		if (DMU_OS_IS_L2CACHEABLE(os))
217 			aflags |= ARC_L2CACHE;
218 
219 		dprintf_bp(os->os_rootbp, "reading %s", "");
220 		/*
221 		 * NB: when bprewrite scrub can change the bp,
222 		 * and this is called from dmu_objset_open_ds_os, the bp
223 		 * could change, and we'll need a lock.
224 		 */
225 		err = arc_read_nolock(NULL, spa, os->os_rootbp,
226 		    arc_getbuf_func, &os->os_phys_buf,
227 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
228 		if (err) {
229 			kmem_free(os, sizeof (objset_t));
230 			/* convert checksum errors into IO errors */
231 			if (err == ECKSUM)
232 				err = EIO;
233 			return (err);
234 		}
235 
236 		/* Increase the blocksize if we are permitted. */
237 		if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
238 		    arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
239 			arc_buf_t *buf = arc_buf_alloc(spa,
240 			    sizeof (objset_phys_t), &os->os_phys_buf,
241 			    ARC_BUFC_METADATA);
242 			bzero(buf->b_data, sizeof (objset_phys_t));
243 			bcopy(os->os_phys_buf->b_data, buf->b_data,
244 			    arc_buf_size(os->os_phys_buf));
245 			(void) arc_buf_remove_ref(os->os_phys_buf,
246 			    &os->os_phys_buf);
247 			os->os_phys_buf = buf;
248 		}
249 
250 		os->os_phys = os->os_phys_buf->b_data;
251 		os->os_flags = os->os_phys->os_flags;
252 	} else {
253 		int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
254 		    sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
255 		os->os_phys_buf = arc_buf_alloc(spa, size,
256 		    &os->os_phys_buf, ARC_BUFC_METADATA);
257 		os->os_phys = os->os_phys_buf->b_data;
258 		bzero(os->os_phys, size);
259 	}
260 
261 	/*
262 	 * Note: the changed_cb will be called once before the register
263 	 * func returns, thus changing the checksum/compression from the
264 	 * default (fletcher2/off).  Snapshots don't need to know about
265 	 * checksum/compression/copies.
266 	 */
267 	if (ds) {
268 		err = dsl_prop_register(ds, "primarycache",
269 		    primary_cache_changed_cb, os);
270 		if (err == 0)
271 			err = dsl_prop_register(ds, "secondarycache",
272 			    secondary_cache_changed_cb, os);
273 		if (!dsl_dataset_is_snapshot(ds)) {
274 			if (err == 0)
275 				err = dsl_prop_register(ds, "checksum",
276 				    checksum_changed_cb, os);
277 			if (err == 0)
278 				err = dsl_prop_register(ds, "compression",
279 				    compression_changed_cb, os);
280 			if (err == 0)
281 				err = dsl_prop_register(ds, "copies",
282 				    copies_changed_cb, os);
283 			if (err == 0)
284 				err = dsl_prop_register(ds, "logbias",
285 				    logbias_changed_cb, os);
286 		}
287 		if (err) {
288 			VERIFY(arc_buf_remove_ref(os->os_phys_buf,
289 			    &os->os_phys_buf) == 1);
290 			kmem_free(os, sizeof (objset_t));
291 			return (err);
292 		}
293 	} else if (ds == NULL) {
294 		/* It's the meta-objset. */
295 		os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
296 		os->os_compress = ZIO_COMPRESS_LZJB;
297 		os->os_copies = spa_max_replication(spa);
298 		os->os_primary_cache = ZFS_CACHE_ALL;
299 		os->os_secondary_cache = ZFS_CACHE_ALL;
300 	}
301 
302 	os->os_zil_header = os->os_phys->os_zil_header;
303 	os->os_zil = zil_alloc(os, &os->os_zil_header);
304 
305 	for (i = 0; i < TXG_SIZE; i++) {
306 		list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
307 		    offsetof(dnode_t, dn_dirty_link[i]));
308 		list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
309 		    offsetof(dnode_t, dn_dirty_link[i]));
310 	}
311 	list_create(&os->os_dnodes, sizeof (dnode_t),
312 	    offsetof(dnode_t, dn_link));
313 	list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
314 	    offsetof(dmu_buf_impl_t, db_link));
315 
316 	mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
317 	mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
318 	mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
319 
320 	os->os_meta_dnode = dnode_special_open(os,
321 	    &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
322 	if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
323 		os->os_userused_dnode = dnode_special_open(os,
324 		    &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT);
325 		os->os_groupused_dnode = dnode_special_open(os,
326 		    &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT);
327 	}
328 
329 	/*
330 	 * We should be the only thread trying to do this because we
331 	 * have ds_opening_lock
332 	 */
333 	if (ds) {
334 		mutex_enter(&ds->ds_lock);
335 		ASSERT(ds->ds_objset == NULL);
336 		ds->ds_objset = os;
337 		mutex_exit(&ds->ds_lock);
338 	}
339 
340 	*osp = os;
341 	return (0);
342 }
343 
344 int
345 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
346 {
347 	int err = 0;
348 
349 	mutex_enter(&ds->ds_opening_lock);
350 	*osp = ds->ds_objset;
351 	if (*osp == NULL) {
352 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
353 		    ds, &ds->ds_phys->ds_bp, osp);
354 	}
355 	mutex_exit(&ds->ds_opening_lock);
356 	return (err);
357 }
358 
359 /* called from zpl */
360 int
361 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
362 {
363 	dsl_dataset_t *ds;
364 	int err;
365 
366 	err = dsl_dataset_hold(name, tag, &ds);
367 	if (err)
368 		return (err);
369 
370 	err = dmu_objset_from_ds(ds, osp);
371 	if (err)
372 		dsl_dataset_rele(ds, tag);
373 
374 	return (err);
375 }
376 
377 /* called from zpl */
378 int
379 dmu_objset_own(const char *name, dmu_objset_type_t type,
380     boolean_t readonly, void *tag, objset_t **osp)
381 {
382 	dsl_dataset_t *ds;
383 	int err;
384 
385 	err = dsl_dataset_own(name, B_FALSE, tag, &ds);
386 	if (err)
387 		return (err);
388 
389 	err = dmu_objset_from_ds(ds, osp);
390 	if (err) {
391 		dsl_dataset_disown(ds, tag);
392 	} else if ((type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) ||
393 	    (!readonly && dsl_dataset_is_snapshot(ds))) {
394 		dmu_objset_disown(*osp, tag);
395 		return (EINVAL);
396 	}
397 
398 	return (err);
399 }
400 
401 void
402 dmu_objset_rele(objset_t *os, void *tag)
403 {
404 	dsl_dataset_rele(os->os_dsl_dataset, tag);
405 }
406 
407 void
408 dmu_objset_disown(objset_t *os, void *tag)
409 {
410 	dsl_dataset_disown(os->os_dsl_dataset, tag);
411 }
412 
413 int
414 dmu_objset_evict_dbufs(objset_t *os)
415 {
416 	dnode_t *dn;
417 
418 	mutex_enter(&os->os_lock);
419 
420 	/* process the mdn last, since the other dnodes have holds on it */
421 	list_remove(&os->os_dnodes, os->os_meta_dnode);
422 	list_insert_tail(&os->os_dnodes, os->os_meta_dnode);
423 
424 	/*
425 	 * Find the first dnode with holds.  We have to do this dance
426 	 * because dnode_add_ref() only works if you already have a
427 	 * hold.  If there are no holds then it has no dbufs so OK to
428 	 * skip.
429 	 */
430 	for (dn = list_head(&os->os_dnodes);
431 	    dn && !dnode_add_ref(dn, FTAG);
432 	    dn = list_next(&os->os_dnodes, dn))
433 		continue;
434 
435 	while (dn) {
436 		dnode_t *next_dn = dn;
437 
438 		do {
439 			next_dn = list_next(&os->os_dnodes, next_dn);
440 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
441 
442 		mutex_exit(&os->os_lock);
443 		dnode_evict_dbufs(dn);
444 		dnode_rele(dn, FTAG);
445 		mutex_enter(&os->os_lock);
446 		dn = next_dn;
447 	}
448 	mutex_exit(&os->os_lock);
449 	return (list_head(&os->os_dnodes) != os->os_meta_dnode);
450 }
451 
452 void
453 dmu_objset_evict(objset_t *os)
454 {
455 	dsl_dataset_t *ds = os->os_dsl_dataset;
456 	int i;
457 
458 	for (i = 0; i < TXG_SIZE; i++) {
459 		ASSERT(list_head(&os->os_dirty_dnodes[i]) == NULL);
460 		ASSERT(list_head(&os->os_free_dnodes[i]) == NULL);
461 	}
462 
463 	if (ds) {
464 		if (!dsl_dataset_is_snapshot(ds)) {
465 			VERIFY(0 == dsl_prop_unregister(ds, "checksum",
466 			    checksum_changed_cb, os));
467 			VERIFY(0 == dsl_prop_unregister(ds, "compression",
468 			    compression_changed_cb, os));
469 			VERIFY(0 == dsl_prop_unregister(ds, "copies",
470 			    copies_changed_cb, os));
471 			VERIFY(0 == dsl_prop_unregister(ds, "logbias",
472 			    logbias_changed_cb, os));
473 		}
474 		VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
475 		    primary_cache_changed_cb, os));
476 		VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
477 		    secondary_cache_changed_cb, os));
478 	}
479 
480 	/*
481 	 * We should need only a single pass over the dnode list, since
482 	 * nothing can be added to the list at this point.
483 	 */
484 	(void) dmu_objset_evict_dbufs(os);
485 
486 	dnode_special_close(os->os_meta_dnode);
487 	if (os->os_userused_dnode) {
488 		dnode_special_close(os->os_userused_dnode);
489 		dnode_special_close(os->os_groupused_dnode);
490 	}
491 	zil_free(os->os_zil);
492 
493 	ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
494 
495 	VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
496 	mutex_destroy(&os->os_lock);
497 	mutex_destroy(&os->os_obj_lock);
498 	mutex_destroy(&os->os_user_ptr_lock);
499 	kmem_free(os, sizeof (objset_t));
500 }
501 
502 /* called from dsl for meta-objset */
503 objset_t *
504 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
505     dmu_objset_type_t type, dmu_tx_t *tx)
506 {
507 	objset_t *os;
508 	dnode_t *mdn;
509 
510 	ASSERT(dmu_tx_is_syncing(tx));
511 	if (ds)
512 		mutex_enter(&ds->ds_opening_lock);
513 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
514 	if (ds)
515 		mutex_exit(&ds->ds_opening_lock);
516 	mdn = os->os_meta_dnode;
517 
518 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
519 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
520 
521 	/*
522 	 * We don't want to have to increase the meta-dnode's nlevels
523 	 * later, because then we could do it in quescing context while
524 	 * we are also accessing it in open context.
525 	 *
526 	 * This precaution is not necessary for the MOS (ds == NULL),
527 	 * because the MOS is only updated in syncing context.
528 	 * This is most fortunate: the MOS is the only objset that
529 	 * needs to be synced multiple times as spa_sync() iterates
530 	 * to convergence, so minimizing its dn_nlevels matters.
531 	 */
532 	if (ds != NULL) {
533 		int levels = 1;
534 
535 		/*
536 		 * Determine the number of levels necessary for the meta-dnode
537 		 * to contain DN_MAX_OBJECT dnodes.
538 		 */
539 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
540 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
541 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
542 			levels++;
543 
544 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
545 		    mdn->dn_nlevels = levels;
546 	}
547 
548 	ASSERT(type != DMU_OST_NONE);
549 	ASSERT(type != DMU_OST_ANY);
550 	ASSERT(type < DMU_OST_NUMTYPES);
551 	os->os_phys->os_type = type;
552 	if (dmu_objset_userused_enabled(os)) {
553 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
554 		os->os_flags = os->os_phys->os_flags;
555 	}
556 
557 	dsl_dataset_dirty(ds, tx);
558 
559 	return (os);
560 }
561 
562 struct oscarg {
563 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
564 	void *userarg;
565 	dsl_dataset_t *clone_origin;
566 	const char *lastname;
567 	dmu_objset_type_t type;
568 	uint64_t flags;
569 };
570 
571 /*ARGSUSED*/
572 static int
573 dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
574 {
575 	dsl_dir_t *dd = arg1;
576 	struct oscarg *oa = arg2;
577 	objset_t *mos = dd->dd_pool->dp_meta_objset;
578 	int err;
579 	uint64_t ddobj;
580 
581 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
582 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
583 	if (err != ENOENT)
584 		return (err ? err : EEXIST);
585 
586 	if (oa->clone_origin != NULL) {
587 		/* You can't clone across pools. */
588 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
589 			return (EXDEV);
590 
591 		/* You can only clone snapshots, not the head datasets. */
592 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
593 			return (EINVAL);
594 	}
595 
596 	return (0);
597 }
598 
599 static void
600 dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
601 {
602 	dsl_dir_t *dd = arg1;
603 	struct oscarg *oa = arg2;
604 	uint64_t dsobj;
605 
606 	ASSERT(dmu_tx_is_syncing(tx));
607 
608 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
609 	    oa->clone_origin, oa->flags, cr, tx);
610 
611 	if (oa->clone_origin == NULL) {
612 		dsl_dataset_t *ds;
613 		blkptr_t *bp;
614 		objset_t *os;
615 
616 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
617 		    FTAG, &ds));
618 		bp = dsl_dataset_get_blkptr(ds);
619 		ASSERT(BP_IS_HOLE(bp));
620 
621 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
622 		    ds, bp, oa->type, tx);
623 
624 		if (oa->userfunc)
625 			oa->userfunc(os, oa->userarg, cr, tx);
626 		dsl_dataset_rele(ds, FTAG);
627 	}
628 
629 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
630 	    tx, cr, "dataset = %llu", dsobj);
631 }
632 
633 int
634 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
635     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
636 {
637 	dsl_dir_t *pdd;
638 	const char *tail;
639 	int err = 0;
640 	struct oscarg oa = { 0 };
641 
642 	ASSERT(strchr(name, '@') == NULL);
643 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
644 	if (err)
645 		return (err);
646 	if (tail == NULL) {
647 		dsl_dir_close(pdd, FTAG);
648 		return (EEXIST);
649 	}
650 
651 	oa.userfunc = func;
652 	oa.userarg = arg;
653 	oa.lastname = tail;
654 	oa.type = type;
655 	oa.flags = flags;
656 
657 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
658 	    dmu_objset_create_sync, pdd, &oa, 5);
659 	dsl_dir_close(pdd, FTAG);
660 	return (err);
661 }
662 
663 int
664 dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
665 {
666 	dsl_dir_t *pdd;
667 	const char *tail;
668 	int err = 0;
669 	struct oscarg oa = { 0 };
670 
671 	ASSERT(strchr(name, '@') == NULL);
672 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
673 	if (err)
674 		return (err);
675 	if (tail == NULL) {
676 		dsl_dir_close(pdd, FTAG);
677 		return (EEXIST);
678 	}
679 
680 	oa.lastname = tail;
681 	oa.clone_origin = clone_origin;
682 	oa.flags = flags;
683 
684 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
685 	    dmu_objset_create_sync, pdd, &oa, 5);
686 	dsl_dir_close(pdd, FTAG);
687 	return (err);
688 }
689 
690 int
691 dmu_objset_destroy(const char *name, boolean_t defer)
692 {
693 	dsl_dataset_t *ds;
694 	int error;
695 
696 	/*
697 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
698 	 * intent log, but if there is an active log, it has blocks that
699 	 * are allocated, but may not yet be reflected in the on-disk
700 	 * structure.  Only the ZIL knows how to free them, so we have
701 	 * to call into it here.
702 	 */
703 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
704 	if (error == 0) {
705 		objset_t *os;
706 		if (dmu_objset_from_ds(ds, &os) == 0)
707 			zil_destroy(dmu_objset_zil(os), B_FALSE);
708 		error = dsl_dataset_destroy(ds, FTAG, defer);
709 		/* dsl_dataset_destroy() closes the ds. */
710 	}
711 
712 	return (error);
713 }
714 
715 struct snaparg {
716 	dsl_sync_task_group_t *dstg;
717 	char *snapname;
718 	char failed[MAXPATHLEN];
719 	boolean_t checkperms;
720 	nvlist_t *props;
721 };
722 
723 static int
724 snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
725 {
726 	objset_t *os = arg1;
727 	struct snaparg *sn = arg2;
728 
729 	/* The props have already been checked by zfs_check_userprops(). */
730 
731 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
732 	    sn->snapname, tx));
733 }
734 
735 static void
736 snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
737 {
738 	objset_t *os = arg1;
739 	dsl_dataset_t *ds = os->os_dsl_dataset;
740 	struct snaparg *sn = arg2;
741 
742 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
743 
744 	if (sn->props)
745 		dsl_props_set_sync(ds->ds_prev, sn->props, cr, tx);
746 }
747 
748 static int
749 dmu_objset_snapshot_one(char *name, void *arg)
750 {
751 	struct snaparg *sn = arg;
752 	objset_t *os;
753 	int err;
754 
755 	(void) strcpy(sn->failed, name);
756 
757 	/*
758 	 * Check permissions only when requested.  This only applies when
759 	 * doing a recursive snapshot.  The permission checks for the starting
760 	 * dataset have already been performed in zfs_secpolicy_snapshot()
761 	 */
762 	if (sn->checkperms == B_TRUE &&
763 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
764 		return (err);
765 
766 	err = dmu_objset_hold(name, sn, &os);
767 	if (err != 0)
768 		return (err);
769 
770 	/* If the objset is in an inconsistent state, return busy */
771 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
772 		dmu_objset_rele(os, sn);
773 		return (EBUSY);
774 	}
775 
776 	/*
777 	 * NB: we need to wait for all in-flight changes to get to disk,
778 	 * so that we snapshot those changes.  zil_suspend does this as
779 	 * a side effect.
780 	 */
781 	err = zil_suspend(dmu_objset_zil(os));
782 	if (err == 0) {
783 		dsl_sync_task_create(sn->dstg, snapshot_check,
784 		    snapshot_sync, os, sn, 3);
785 	} else {
786 		dmu_objset_rele(os, sn);
787 	}
788 
789 	return (err);
790 }
791 
792 int
793 dmu_objset_snapshot(char *fsname, char *snapname,
794     nvlist_t *props, boolean_t recursive)
795 {
796 	dsl_sync_task_t *dst;
797 	struct snaparg sn;
798 	spa_t *spa;
799 	int err;
800 
801 	(void) strcpy(sn.failed, fsname);
802 
803 	err = spa_open(fsname, &spa, FTAG);
804 	if (err)
805 		return (err);
806 
807 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
808 	sn.snapname = snapname;
809 	sn.props = props;
810 
811 	if (recursive) {
812 		sn.checkperms = B_TRUE;
813 		err = dmu_objset_find(fsname,
814 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
815 	} else {
816 		sn.checkperms = B_FALSE;
817 		err = dmu_objset_snapshot_one(fsname, &sn);
818 	}
819 
820 	if (err == 0)
821 		err = dsl_sync_task_group_wait(sn.dstg);
822 
823 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
824 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
825 		objset_t *os = dst->dst_arg1;
826 		dsl_dataset_t *ds = os->os_dsl_dataset;
827 		if (dst->dst_err)
828 			dsl_dataset_name(ds, sn.failed);
829 		zil_resume(dmu_objset_zil(os));
830 		dmu_objset_rele(os, &sn);
831 	}
832 
833 	if (err)
834 		(void) strcpy(fsname, sn.failed);
835 	dsl_sync_task_group_destroy(sn.dstg);
836 	spa_close(spa, FTAG);
837 	return (err);
838 }
839 
840 static void
841 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
842 {
843 	dnode_t *dn;
844 
845 	while (dn = list_head(list)) {
846 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
847 		ASSERT(dn->dn_dbuf->db_data_pending);
848 		/*
849 		 * Initialize dn_zio outside dnode_sync() because the
850 		 * meta-dnode needs to set it ouside dnode_sync().
851 		 */
852 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
853 		ASSERT(dn->dn_zio);
854 
855 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
856 		list_remove(list, dn);
857 
858 		if (newlist) {
859 			(void) dnode_add_ref(dn, newlist);
860 			list_insert_tail(newlist, dn);
861 		}
862 
863 		dnode_sync(dn, tx);
864 	}
865 }
866 
867 /* ARGSUSED */
868 static void
869 ready(zio_t *zio, arc_buf_t *abuf, void *arg)
870 {
871 	blkptr_t *bp = zio->io_bp;
872 	blkptr_t *bp_orig = &zio->io_bp_orig;
873 	objset_t *os = arg;
874 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
875 
876 	ASSERT(bp == os->os_rootbp);
877 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
878 	ASSERT(BP_GET_LEVEL(bp) == 0);
879 
880 	/*
881 	 * Update rootbp fill count: it should be the number of objects
882 	 * allocated in the object set (not counting the "special"
883 	 * objects that are stored in the objset_phys_t -- the meta
884 	 * dnode and user/group accounting objects).
885 	 */
886 	bp->blk_fill = 0;
887 	for (int i = 0; i < dnp->dn_nblkptr; i++)
888 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
889 
890 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
891 		ASSERT(DVA_EQUAL(BP_IDENTITY(bp), BP_IDENTITY(bp_orig)));
892 	} else {
893 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
894 			(void) dsl_dataset_block_kill(os->os_dsl_dataset,
895 			    &zio->io_bp_orig, zio, os->os_synctx);
896 		dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx);
897 	}
898 }
899 
900 /* called from dsl */
901 void
902 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
903 {
904 	int txgoff;
905 	zbookmark_t zb;
906 	writeprops_t wp = { 0 };
907 	zio_t *zio;
908 	list_t *list;
909 	list_t *newlist = NULL;
910 	dbuf_dirty_record_t *dr;
911 
912 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
913 
914 	ASSERT(dmu_tx_is_syncing(tx));
915 	/* XXX the write_done callback should really give us the tx... */
916 	os->os_synctx = tx;
917 
918 	if (os->os_dsl_dataset == NULL) {
919 		/*
920 		 * This is the MOS.  If we have upgraded,
921 		 * spa_max_replication() could change, so reset
922 		 * os_copies here.
923 		 */
924 		os->os_copies = spa_max_replication(os->os_spa);
925 	}
926 
927 	/*
928 	 * Create the root block IO
929 	 */
930 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
931 	zb.zb_object = 0;
932 	zb.zb_level = -1;	/* for block ordering; it's level 0 on disk */
933 	zb.zb_blkid = 0;
934 
935 	wp.wp_type = DMU_OT_OBJSET;
936 	wp.wp_level = 0;	/* on-disk BP level; see above */
937 	wp.wp_copies = os->os_copies;
938 	wp.wp_oschecksum = os->os_checksum;
939 	wp.wp_oscompress = os->os_compress;
940 
941 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
942 		(void) dsl_dataset_block_kill(os->os_dsl_dataset,
943 		    os->os_rootbp, pio, tx);
944 	}
945 
946 	arc_release(os->os_phys_buf, &os->os_phys_buf);
947 
948 	zio = arc_write(pio, os->os_spa, &wp, DMU_OS_IS_L2CACHEABLE(os),
949 	    tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os,
950 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
951 
952 	/*
953 	 * Sync special dnodes - the parent IO for the sync is the root block
954 	 */
955 	os->os_meta_dnode->dn_zio = zio;
956 	dnode_sync(os->os_meta_dnode, tx);
957 
958 	os->os_phys->os_flags = os->os_flags;
959 
960 	if (os->os_userused_dnode &&
961 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
962 		os->os_userused_dnode->dn_zio = zio;
963 		dnode_sync(os->os_userused_dnode, tx);
964 		os->os_groupused_dnode->dn_zio = zio;
965 		dnode_sync(os->os_groupused_dnode, tx);
966 	}
967 
968 	txgoff = tx->tx_txg & TXG_MASK;
969 
970 	if (dmu_objset_userused_enabled(os)) {
971 		newlist = &os->os_synced_dnodes;
972 		/*
973 		 * We must create the list here because it uses the
974 		 * dn_dirty_link[] of this txg.
975 		 */
976 		list_create(newlist, sizeof (dnode_t),
977 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
978 	}
979 
980 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
981 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
982 
983 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
984 	while (dr = list_head(list)) {
985 		ASSERT(dr->dr_dbuf->db_level == 0);
986 		list_remove(list, dr);
987 		if (dr->dr_zio)
988 			zio_nowait(dr->dr_zio);
989 	}
990 	/*
991 	 * Free intent log blocks up to this tx.
992 	 */
993 	zil_sync(os->os_zil, tx);
994 	os->os_phys->os_zil_header = os->os_zil_header;
995 	zio_nowait(zio);
996 }
997 
998 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
999 
1000 void
1001 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1002 {
1003 	used_cbs[ost] = cb;
1004 }
1005 
1006 boolean_t
1007 dmu_objset_userused_enabled(objset_t *os)
1008 {
1009 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1010 	    used_cbs[os->os_phys->os_type] &&
1011 	    os->os_userused_dnode);
1012 }
1013 
1014 void
1015 dmu_objset_do_userquota_callbacks(objset_t *os, dmu_tx_t *tx)
1016 {
1017 	dnode_t *dn;
1018 	list_t *list = &os->os_synced_dnodes;
1019 	static const char zerobuf[DN_MAX_BONUSLEN] = {0};
1020 
1021 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1022 
1023 	while (dn = list_head(list)) {
1024 		dmu_object_type_t bonustype;
1025 
1026 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1027 		ASSERT(dn->dn_oldphys);
1028 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1029 		    dn->dn_phys->dn_flags &
1030 		    DNODE_FLAG_USERUSED_ACCOUNTED);
1031 
1032 		/* Allocate the user/groupused objects if necessary. */
1033 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
1034 			VERIFY(0 == zap_create_claim(os,
1035 			    DMU_USERUSED_OBJECT,
1036 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1037 			VERIFY(0 == zap_create_claim(os,
1038 			    DMU_GROUPUSED_OBJECT,
1039 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1040 		}
1041 
1042 		/*
1043 		 * If the object was not previously
1044 		 * accounted, pretend that it was free.
1045 		 */
1046 		if (!(dn->dn_oldphys->dn_flags &
1047 		    DNODE_FLAG_USERUSED_ACCOUNTED)) {
1048 			bzero(dn->dn_oldphys, sizeof (dnode_phys_t));
1049 		}
1050 
1051 		/*
1052 		 * If the object was freed, use the previous bonustype.
1053 		 */
1054 		bonustype = dn->dn_phys->dn_bonustype ?
1055 		    dn->dn_phys->dn_bonustype : dn->dn_oldphys->dn_bonustype;
1056 		ASSERT(dn->dn_phys->dn_type != 0 ||
1057 		    (bcmp(DN_BONUS(dn->dn_phys), zerobuf,
1058 		    DN_MAX_BONUSLEN) == 0 &&
1059 		    DN_USED_BYTES(dn->dn_phys) == 0));
1060 		ASSERT(dn->dn_oldphys->dn_type != 0 ||
1061 		    (bcmp(DN_BONUS(dn->dn_oldphys), zerobuf,
1062 		    DN_MAX_BONUSLEN) == 0 &&
1063 		    DN_USED_BYTES(dn->dn_oldphys) == 0));
1064 		used_cbs[os->os_phys->os_type](os, bonustype,
1065 		    DN_BONUS(dn->dn_oldphys), DN_BONUS(dn->dn_phys),
1066 		    DN_USED_BYTES(dn->dn_oldphys),
1067 		    DN_USED_BYTES(dn->dn_phys), tx);
1068 
1069 		/*
1070 		 * The mutex is needed here for interlock with dnode_allocate.
1071 		 */
1072 		mutex_enter(&dn->dn_mtx);
1073 		zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t));
1074 		dn->dn_oldphys = NULL;
1075 		mutex_exit(&dn->dn_mtx);
1076 
1077 		list_remove(list, dn);
1078 		dnode_rele(dn, list);
1079 	}
1080 }
1081 
1082 boolean_t
1083 dmu_objset_userspace_present(objset_t *os)
1084 {
1085 	return (os->os_phys->os_flags &
1086 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1087 }
1088 
1089 int
1090 dmu_objset_userspace_upgrade(objset_t *os)
1091 {
1092 	uint64_t obj;
1093 	int err = 0;
1094 
1095 	if (dmu_objset_userspace_present(os))
1096 		return (0);
1097 	if (!dmu_objset_userused_enabled(os))
1098 		return (ENOTSUP);
1099 	if (dmu_objset_is_snapshot(os))
1100 		return (EINVAL);
1101 
1102 	/*
1103 	 * We simply need to mark every object dirty, so that it will be
1104 	 * synced out and now accounted.  If this is called
1105 	 * concurrently, or if we already did some work before crashing,
1106 	 * that's fine, since we track each object's accounted state
1107 	 * independently.
1108 	 */
1109 
1110 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1111 		dmu_tx_t *tx;
1112 		dmu_buf_t *db;
1113 		int objerr;
1114 
1115 		if (issig(JUSTLOOKING) && issig(FORREAL))
1116 			return (EINTR);
1117 
1118 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1119 		if (objerr)
1120 			continue;
1121 		tx = dmu_tx_create(os);
1122 		dmu_tx_hold_bonus(tx, obj);
1123 		objerr = dmu_tx_assign(tx, TXG_WAIT);
1124 		if (objerr) {
1125 			dmu_tx_abort(tx);
1126 			continue;
1127 		}
1128 		dmu_buf_will_dirty(db, tx);
1129 		dmu_buf_rele(db, FTAG);
1130 		dmu_tx_commit(tx);
1131 	}
1132 
1133 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1134 	txg_wait_synced(dmu_objset_pool(os), 0);
1135 	return (0);
1136 }
1137 
1138 void
1139 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1140     uint64_t *usedobjsp, uint64_t *availobjsp)
1141 {
1142 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1143 	    usedobjsp, availobjsp);
1144 }
1145 
1146 uint64_t
1147 dmu_objset_fsid_guid(objset_t *os)
1148 {
1149 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1150 }
1151 
1152 void
1153 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1154 {
1155 	stat->dds_type = os->os_phys->os_type;
1156 	if (os->os_dsl_dataset)
1157 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1158 }
1159 
1160 void
1161 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1162 {
1163 	ASSERT(os->os_dsl_dataset ||
1164 	    os->os_phys->os_type == DMU_OST_META);
1165 
1166 	if (os->os_dsl_dataset != NULL)
1167 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1168 
1169 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1170 	    os->os_phys->os_type);
1171 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1172 	    dmu_objset_userspace_present(os));
1173 }
1174 
1175 int
1176 dmu_objset_is_snapshot(objset_t *os)
1177 {
1178 	if (os->os_dsl_dataset != NULL)
1179 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1180 	else
1181 		return (B_FALSE);
1182 }
1183 
1184 int
1185 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1186     boolean_t *conflict)
1187 {
1188 	dsl_dataset_t *ds = os->os_dsl_dataset;
1189 	uint64_t ignored;
1190 
1191 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1192 		return (ENOENT);
1193 
1194 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1195 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1196 	    real, maxlen, conflict));
1197 }
1198 
1199 int
1200 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1201     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1202 {
1203 	dsl_dataset_t *ds = os->os_dsl_dataset;
1204 	zap_cursor_t cursor;
1205 	zap_attribute_t attr;
1206 
1207 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1208 		return (ENOENT);
1209 
1210 	zap_cursor_init_serialized(&cursor,
1211 	    ds->ds_dir->dd_pool->dp_meta_objset,
1212 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1213 
1214 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1215 		zap_cursor_fini(&cursor);
1216 		return (ENOENT);
1217 	}
1218 
1219 	if (strlen(attr.za_name) + 1 > namelen) {
1220 		zap_cursor_fini(&cursor);
1221 		return (ENAMETOOLONG);
1222 	}
1223 
1224 	(void) strcpy(name, attr.za_name);
1225 	if (idp)
1226 		*idp = attr.za_first_integer;
1227 	if (case_conflict)
1228 		*case_conflict = attr.za_normalization_conflict;
1229 	zap_cursor_advance(&cursor);
1230 	*offp = zap_cursor_serialize(&cursor);
1231 	zap_cursor_fini(&cursor);
1232 
1233 	return (0);
1234 }
1235 
1236 int
1237 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1238     uint64_t *idp, uint64_t *offp)
1239 {
1240 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1241 	zap_cursor_t cursor;
1242 	zap_attribute_t attr;
1243 
1244 	/* there is no next dir on a snapshot! */
1245 	if (os->os_dsl_dataset->ds_object !=
1246 	    dd->dd_phys->dd_head_dataset_obj)
1247 		return (ENOENT);
1248 
1249 	zap_cursor_init_serialized(&cursor,
1250 	    dd->dd_pool->dp_meta_objset,
1251 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1252 
1253 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1254 		zap_cursor_fini(&cursor);
1255 		return (ENOENT);
1256 	}
1257 
1258 	if (strlen(attr.za_name) + 1 > namelen) {
1259 		zap_cursor_fini(&cursor);
1260 		return (ENAMETOOLONG);
1261 	}
1262 
1263 	(void) strcpy(name, attr.za_name);
1264 	if (idp)
1265 		*idp = attr.za_first_integer;
1266 	zap_cursor_advance(&cursor);
1267 	*offp = zap_cursor_serialize(&cursor);
1268 	zap_cursor_fini(&cursor);
1269 
1270 	return (0);
1271 }
1272 
1273 struct findarg {
1274 	int (*func)(char *, void *);
1275 	void *arg;
1276 };
1277 
1278 /* ARGSUSED */
1279 static int
1280 findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1281 {
1282 	struct findarg *fa = arg;
1283 	return (fa->func((char *)dsname, fa->arg));
1284 }
1285 
1286 /*
1287  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1288  * Perhaps change all callers to use dmu_objset_find_spa()?
1289  */
1290 int
1291 dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1292 {
1293 	struct findarg fa;
1294 	fa.func = func;
1295 	fa.arg = arg;
1296 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
1297 }
1298 
1299 /*
1300  * Find all objsets under name, call func on each
1301  */
1302 int
1303 dmu_objset_find_spa(spa_t *spa, const char *name,
1304     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
1305 {
1306 	dsl_dir_t *dd;
1307 	dsl_pool_t *dp;
1308 	dsl_dataset_t *ds;
1309 	zap_cursor_t zc;
1310 	zap_attribute_t *attr;
1311 	char *child;
1312 	uint64_t thisobj;
1313 	int err;
1314 
1315 	if (name == NULL)
1316 		name = spa_name(spa);
1317 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
1318 	if (err)
1319 		return (err);
1320 
1321 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1322 	if (dd->dd_myname[0] == '$') {
1323 		dsl_dir_close(dd, FTAG);
1324 		return (0);
1325 	}
1326 
1327 	thisobj = dd->dd_phys->dd_head_dataset_obj;
1328 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1329 	dp = dd->dd_pool;
1330 
1331 	/*
1332 	 * Iterate over all children.
1333 	 */
1334 	if (flags & DS_FIND_CHILDREN) {
1335 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1336 		    dd->dd_phys->dd_child_dir_zapobj);
1337 		    zap_cursor_retrieve(&zc, attr) == 0;
1338 		    (void) zap_cursor_advance(&zc)) {
1339 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
1340 			ASSERT(attr->za_num_integers == 1);
1341 
1342 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1343 			(void) strcpy(child, name);
1344 			(void) strcat(child, "/");
1345 			(void) strcat(child, attr->za_name);
1346 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
1347 			kmem_free(child, MAXPATHLEN);
1348 			if (err)
1349 				break;
1350 		}
1351 		zap_cursor_fini(&zc);
1352 
1353 		if (err) {
1354 			dsl_dir_close(dd, FTAG);
1355 			kmem_free(attr, sizeof (zap_attribute_t));
1356 			return (err);
1357 		}
1358 	}
1359 
1360 	/*
1361 	 * Iterate over all snapshots.
1362 	 */
1363 	if (flags & DS_FIND_SNAPSHOTS) {
1364 		if (!dsl_pool_sync_context(dp))
1365 			rw_enter(&dp->dp_config_rwlock, RW_READER);
1366 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1367 		if (!dsl_pool_sync_context(dp))
1368 			rw_exit(&dp->dp_config_rwlock);
1369 
1370 		if (err == 0) {
1371 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1372 			dsl_dataset_rele(ds, FTAG);
1373 
1374 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1375 			    zap_cursor_retrieve(&zc, attr) == 0;
1376 			    (void) zap_cursor_advance(&zc)) {
1377 				ASSERT(attr->za_integer_length ==
1378 				    sizeof (uint64_t));
1379 				ASSERT(attr->za_num_integers == 1);
1380 
1381 				child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1382 				(void) strcpy(child, name);
1383 				(void) strcat(child, "@");
1384 				(void) strcat(child, attr->za_name);
1385 				err = func(spa, attr->za_first_integer,
1386 				    child, arg);
1387 				kmem_free(child, MAXPATHLEN);
1388 				if (err)
1389 					break;
1390 			}
1391 			zap_cursor_fini(&zc);
1392 		}
1393 	}
1394 
1395 	dsl_dir_close(dd, FTAG);
1396 	kmem_free(attr, sizeof (zap_attribute_t));
1397 
1398 	if (err)
1399 		return (err);
1400 
1401 	/*
1402 	 * Apply to self if appropriate.
1403 	 */
1404 	err = func(spa, thisobj, name, arg);
1405 	return (err);
1406 }
1407 
1408 /* ARGSUSED */
1409 int
1410 dmu_objset_prefetch(char *name, void *arg)
1411 {
1412 	dsl_dataset_t *ds;
1413 
1414 	if (dsl_dataset_hold(name, FTAG, &ds))
1415 		return (0);
1416 
1417 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
1418 		mutex_enter(&ds->ds_opening_lock);
1419 		if (ds->ds_objset == NULL) {
1420 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1421 			zbookmark_t zb;
1422 
1423 			zb.zb_objset = ds->ds_object;
1424 			zb.zb_object = 0;
1425 			zb.zb_level = -1;
1426 			zb.zb_blkid = 0;
1427 
1428 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
1429 			    &ds->ds_phys->ds_bp, NULL, NULL,
1430 			    ZIO_PRIORITY_ASYNC_READ,
1431 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1432 			    &aflags, &zb);
1433 		}
1434 		mutex_exit(&ds->ds_opening_lock);
1435 	}
1436 
1437 	dsl_dataset_rele(ds, FTAG);
1438 	return (0);
1439 }
1440 
1441 void
1442 dmu_objset_set_user(objset_t *os, void *user_ptr)
1443 {
1444 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1445 	os->os_user_ptr = user_ptr;
1446 }
1447 
1448 void *
1449 dmu_objset_get_user(objset_t *os)
1450 {
1451 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1452 	return (os->os_user_ptr);
1453 }
1454