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