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