xref: /titanic_50/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 48722b5f422aa5e059f333d8c7384ffd184fe739)
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 timestruc_t
503 dmu_objset_snap_cmtime(objset_t *os)
504 {
505 	return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
506 }
507 
508 /* called from dsl for meta-objset */
509 objset_t *
510 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
511     dmu_objset_type_t type, dmu_tx_t *tx)
512 {
513 	objset_t *os;
514 	dnode_t *mdn;
515 
516 	ASSERT(dmu_tx_is_syncing(tx));
517 	if (ds)
518 		mutex_enter(&ds->ds_opening_lock);
519 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &os));
520 	if (ds)
521 		mutex_exit(&ds->ds_opening_lock);
522 	mdn = os->os_meta_dnode;
523 
524 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
525 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
526 
527 	/*
528 	 * We don't want to have to increase the meta-dnode's nlevels
529 	 * later, because then we could do it in quescing context while
530 	 * we are also accessing it in open context.
531 	 *
532 	 * This precaution is not necessary for the MOS (ds == NULL),
533 	 * because the MOS is only updated in syncing context.
534 	 * This is most fortunate: the MOS is the only objset that
535 	 * needs to be synced multiple times as spa_sync() iterates
536 	 * to convergence, so minimizing its dn_nlevels matters.
537 	 */
538 	if (ds != NULL) {
539 		int levels = 1;
540 
541 		/*
542 		 * Determine the number of levels necessary for the meta-dnode
543 		 * to contain DN_MAX_OBJECT dnodes.
544 		 */
545 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
546 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
547 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
548 			levels++;
549 
550 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
551 		    mdn->dn_nlevels = levels;
552 	}
553 
554 	ASSERT(type != DMU_OST_NONE);
555 	ASSERT(type != DMU_OST_ANY);
556 	ASSERT(type < DMU_OST_NUMTYPES);
557 	os->os_phys->os_type = type;
558 	if (dmu_objset_userused_enabled(os)) {
559 		os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
560 		os->os_flags = os->os_phys->os_flags;
561 	}
562 
563 	dsl_dataset_dirty(ds, tx);
564 
565 	return (os);
566 }
567 
568 struct oscarg {
569 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
570 	void *userarg;
571 	dsl_dataset_t *clone_origin;
572 	const char *lastname;
573 	dmu_objset_type_t type;
574 	uint64_t flags;
575 };
576 
577 /*ARGSUSED*/
578 static int
579 dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
580 {
581 	dsl_dir_t *dd = arg1;
582 	struct oscarg *oa = arg2;
583 	objset_t *mos = dd->dd_pool->dp_meta_objset;
584 	int err;
585 	uint64_t ddobj;
586 
587 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
588 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
589 	if (err != ENOENT)
590 		return (err ? err : EEXIST);
591 
592 	if (oa->clone_origin != NULL) {
593 		/* You can't clone across pools. */
594 		if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
595 			return (EXDEV);
596 
597 		/* You can only clone snapshots, not the head datasets. */
598 		if (!dsl_dataset_is_snapshot(oa->clone_origin))
599 			return (EINVAL);
600 	}
601 
602 	return (0);
603 }
604 
605 static void
606 dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
607 {
608 	dsl_dir_t *dd = arg1;
609 	struct oscarg *oa = arg2;
610 	uint64_t dsobj;
611 
612 	ASSERT(dmu_tx_is_syncing(tx));
613 
614 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
615 	    oa->clone_origin, oa->flags, cr, tx);
616 
617 	if (oa->clone_origin == NULL) {
618 		dsl_dataset_t *ds;
619 		blkptr_t *bp;
620 		objset_t *os;
621 
622 		VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, dsobj,
623 		    FTAG, &ds));
624 		bp = dsl_dataset_get_blkptr(ds);
625 		ASSERT(BP_IS_HOLE(bp));
626 
627 		os = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
628 		    ds, bp, oa->type, tx);
629 
630 		if (oa->userfunc)
631 			oa->userfunc(os, oa->userarg, cr, tx);
632 		dsl_dataset_rele(ds, FTAG);
633 	}
634 
635 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
636 	    tx, cr, "dataset = %llu", dsobj);
637 }
638 
639 int
640 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
641     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
642 {
643 	dsl_dir_t *pdd;
644 	const char *tail;
645 	int err = 0;
646 	struct oscarg oa = { 0 };
647 
648 	ASSERT(strchr(name, '@') == NULL);
649 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
650 	if (err)
651 		return (err);
652 	if (tail == NULL) {
653 		dsl_dir_close(pdd, FTAG);
654 		return (EEXIST);
655 	}
656 
657 	oa.userfunc = func;
658 	oa.userarg = arg;
659 	oa.lastname = tail;
660 	oa.type = type;
661 	oa.flags = flags;
662 
663 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
664 	    dmu_objset_create_sync, pdd, &oa, 5);
665 	dsl_dir_close(pdd, FTAG);
666 	return (err);
667 }
668 
669 int
670 dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
671 {
672 	dsl_dir_t *pdd;
673 	const char *tail;
674 	int err = 0;
675 	struct oscarg oa = { 0 };
676 
677 	ASSERT(strchr(name, '@') == NULL);
678 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
679 	if (err)
680 		return (err);
681 	if (tail == NULL) {
682 		dsl_dir_close(pdd, FTAG);
683 		return (EEXIST);
684 	}
685 
686 	oa.lastname = tail;
687 	oa.clone_origin = clone_origin;
688 	oa.flags = flags;
689 
690 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
691 	    dmu_objset_create_sync, pdd, &oa, 5);
692 	dsl_dir_close(pdd, FTAG);
693 	return (err);
694 }
695 
696 int
697 dmu_objset_destroy(const char *name, boolean_t defer)
698 {
699 	dsl_dataset_t *ds;
700 	int error;
701 
702 	/*
703 	 * dsl_dataset_destroy() can free any claimed-but-unplayed
704 	 * intent log, but if there is an active log, it has blocks that
705 	 * are allocated, but may not yet be reflected in the on-disk
706 	 * structure.  Only the ZIL knows how to free them, so we have
707 	 * to call into it here.
708 	 */
709 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
710 	if (error == 0) {
711 		objset_t *os;
712 		if (dmu_objset_from_ds(ds, &os) == 0)
713 			zil_destroy(dmu_objset_zil(os), B_FALSE);
714 		error = dsl_dataset_destroy(ds, FTAG, defer);
715 		/* dsl_dataset_destroy() closes the ds. */
716 	}
717 
718 	return (error);
719 }
720 
721 struct snaparg {
722 	dsl_sync_task_group_t *dstg;
723 	char *snapname;
724 	char failed[MAXPATHLEN];
725 	boolean_t checkperms;
726 	nvlist_t *props;
727 };
728 
729 static int
730 snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
731 {
732 	objset_t *os = arg1;
733 	struct snaparg *sn = arg2;
734 
735 	/* The props have already been checked by zfs_check_userprops(). */
736 
737 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
738 	    sn->snapname, tx));
739 }
740 
741 static void
742 snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
743 {
744 	objset_t *os = arg1;
745 	dsl_dataset_t *ds = os->os_dsl_dataset;
746 	struct snaparg *sn = arg2;
747 
748 	dsl_dataset_snapshot_sync(ds, sn->snapname, cr, tx);
749 
750 	if (sn->props)
751 		dsl_props_set_sync(ds->ds_prev, sn->props, cr, tx);
752 }
753 
754 static int
755 dmu_objset_snapshot_one(char *name, void *arg)
756 {
757 	struct snaparg *sn = arg;
758 	objset_t *os;
759 	int err;
760 
761 	(void) strcpy(sn->failed, name);
762 
763 	/*
764 	 * Check permissions only when requested.  This only applies when
765 	 * doing a recursive snapshot.  The permission checks for the starting
766 	 * dataset have already been performed in zfs_secpolicy_snapshot()
767 	 */
768 	if (sn->checkperms == B_TRUE &&
769 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
770 		return (err);
771 
772 	err = dmu_objset_hold(name, sn, &os);
773 	if (err != 0)
774 		return (err);
775 
776 	/* If the objset is in an inconsistent state, return busy */
777 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
778 		dmu_objset_rele(os, sn);
779 		return (EBUSY);
780 	}
781 
782 	/*
783 	 * NB: we need to wait for all in-flight changes to get to disk,
784 	 * so that we snapshot those changes.  zil_suspend does this as
785 	 * a side effect.
786 	 */
787 	err = zil_suspend(dmu_objset_zil(os));
788 	if (err == 0) {
789 		dsl_sync_task_create(sn->dstg, snapshot_check,
790 		    snapshot_sync, os, sn, 3);
791 	} else {
792 		dmu_objset_rele(os, sn);
793 	}
794 
795 	return (err);
796 }
797 
798 int
799 dmu_objset_snapshot(char *fsname, char *snapname,
800     nvlist_t *props, boolean_t recursive)
801 {
802 	dsl_sync_task_t *dst;
803 	struct snaparg sn;
804 	spa_t *spa;
805 	int err;
806 
807 	(void) strcpy(sn.failed, fsname);
808 
809 	err = spa_open(fsname, &spa, FTAG);
810 	if (err)
811 		return (err);
812 
813 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
814 	sn.snapname = snapname;
815 	sn.props = props;
816 
817 	if (recursive) {
818 		sn.checkperms = B_TRUE;
819 		err = dmu_objset_find(fsname,
820 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
821 	} else {
822 		sn.checkperms = B_FALSE;
823 		err = dmu_objset_snapshot_one(fsname, &sn);
824 	}
825 
826 	if (err == 0)
827 		err = dsl_sync_task_group_wait(sn.dstg);
828 
829 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
830 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
831 		objset_t *os = dst->dst_arg1;
832 		dsl_dataset_t *ds = os->os_dsl_dataset;
833 		if (dst->dst_err)
834 			dsl_dataset_name(ds, sn.failed);
835 		zil_resume(dmu_objset_zil(os));
836 		dmu_objset_rele(os, &sn);
837 	}
838 
839 	if (err)
840 		(void) strcpy(fsname, sn.failed);
841 	dsl_sync_task_group_destroy(sn.dstg);
842 	spa_close(spa, FTAG);
843 	return (err);
844 }
845 
846 static void
847 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
848 {
849 	dnode_t *dn;
850 
851 	while (dn = list_head(list)) {
852 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
853 		ASSERT(dn->dn_dbuf->db_data_pending);
854 		/*
855 		 * Initialize dn_zio outside dnode_sync() because the
856 		 * meta-dnode needs to set it ouside dnode_sync().
857 		 */
858 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
859 		ASSERT(dn->dn_zio);
860 
861 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
862 		list_remove(list, dn);
863 
864 		if (newlist) {
865 			(void) dnode_add_ref(dn, newlist);
866 			list_insert_tail(newlist, dn);
867 		}
868 
869 		dnode_sync(dn, tx);
870 	}
871 }
872 
873 /* ARGSUSED */
874 static void
875 ready(zio_t *zio, arc_buf_t *abuf, void *arg)
876 {
877 	blkptr_t *bp = zio->io_bp;
878 	blkptr_t *bp_orig = &zio->io_bp_orig;
879 	objset_t *os = arg;
880 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
881 
882 	ASSERT(bp == os->os_rootbp);
883 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
884 	ASSERT(BP_GET_LEVEL(bp) == 0);
885 
886 	/*
887 	 * Update rootbp fill count: it should be the number of objects
888 	 * allocated in the object set (not counting the "special"
889 	 * objects that are stored in the objset_phys_t -- the meta
890 	 * dnode and user/group accounting objects).
891 	 */
892 	bp->blk_fill = 0;
893 	for (int i = 0; i < dnp->dn_nblkptr; i++)
894 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
895 
896 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
897 		ASSERT(DVA_EQUAL(BP_IDENTITY(bp), BP_IDENTITY(bp_orig)));
898 	} else {
899 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
900 			(void) dsl_dataset_block_kill(os->os_dsl_dataset,
901 			    &zio->io_bp_orig, zio, os->os_synctx);
902 		dsl_dataset_block_born(os->os_dsl_dataset, bp, os->os_synctx);
903 	}
904 }
905 
906 /* called from dsl */
907 void
908 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
909 {
910 	int txgoff;
911 	zbookmark_t zb;
912 	writeprops_t wp = { 0 };
913 	zio_t *zio;
914 	list_t *list;
915 	list_t *newlist = NULL;
916 	dbuf_dirty_record_t *dr;
917 
918 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
919 
920 	ASSERT(dmu_tx_is_syncing(tx));
921 	/* XXX the write_done callback should really give us the tx... */
922 	os->os_synctx = tx;
923 
924 	if (os->os_dsl_dataset == NULL) {
925 		/*
926 		 * This is the MOS.  If we have upgraded,
927 		 * spa_max_replication() could change, so reset
928 		 * os_copies here.
929 		 */
930 		os->os_copies = spa_max_replication(os->os_spa);
931 	}
932 
933 	/*
934 	 * Create the root block IO
935 	 */
936 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
937 	zb.zb_object = 0;
938 	zb.zb_level = -1;	/* for block ordering; it's level 0 on disk */
939 	zb.zb_blkid = 0;
940 
941 	wp.wp_type = DMU_OT_OBJSET;
942 	wp.wp_level = 0;	/* on-disk BP level; see above */
943 	wp.wp_copies = os->os_copies;
944 	wp.wp_oschecksum = os->os_checksum;
945 	wp.wp_oscompress = os->os_compress;
946 
947 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
948 		(void) dsl_dataset_block_kill(os->os_dsl_dataset,
949 		    os->os_rootbp, pio, tx);
950 	}
951 
952 	arc_release(os->os_phys_buf, &os->os_phys_buf);
953 
954 	zio = arc_write(pio, os->os_spa, &wp, DMU_OS_IS_L2CACHEABLE(os),
955 	    tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, NULL, os,
956 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
957 
958 	/*
959 	 * Sync special dnodes - the parent IO for the sync is the root block
960 	 */
961 	os->os_meta_dnode->dn_zio = zio;
962 	dnode_sync(os->os_meta_dnode, tx);
963 
964 	os->os_phys->os_flags = os->os_flags;
965 
966 	if (os->os_userused_dnode &&
967 	    os->os_userused_dnode->dn_type != DMU_OT_NONE) {
968 		os->os_userused_dnode->dn_zio = zio;
969 		dnode_sync(os->os_userused_dnode, tx);
970 		os->os_groupused_dnode->dn_zio = zio;
971 		dnode_sync(os->os_groupused_dnode, tx);
972 	}
973 
974 	txgoff = tx->tx_txg & TXG_MASK;
975 
976 	if (dmu_objset_userused_enabled(os)) {
977 		newlist = &os->os_synced_dnodes;
978 		/*
979 		 * We must create the list here because it uses the
980 		 * dn_dirty_link[] of this txg.
981 		 */
982 		list_create(newlist, sizeof (dnode_t),
983 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
984 	}
985 
986 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
987 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
988 
989 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
990 	while (dr = list_head(list)) {
991 		ASSERT(dr->dr_dbuf->db_level == 0);
992 		list_remove(list, dr);
993 		if (dr->dr_zio)
994 			zio_nowait(dr->dr_zio);
995 	}
996 	/*
997 	 * Free intent log blocks up to this tx.
998 	 */
999 	zil_sync(os->os_zil, tx);
1000 	os->os_phys->os_zil_header = os->os_zil_header;
1001 	zio_nowait(zio);
1002 }
1003 
1004 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1005 
1006 void
1007 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1008 {
1009 	used_cbs[ost] = cb;
1010 }
1011 
1012 boolean_t
1013 dmu_objset_userused_enabled(objset_t *os)
1014 {
1015 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1016 	    used_cbs[os->os_phys->os_type] &&
1017 	    os->os_userused_dnode);
1018 }
1019 
1020 static void
1021 do_userquota_callback(objset_t *os, dnode_phys_t *dnp,
1022     boolean_t subtract, dmu_tx_t *tx)
1023 {
1024 	static const char zerobuf[DN_MAX_BONUSLEN] = {0};
1025 	uint64_t user, group;
1026 
1027 	ASSERT(dnp->dn_type != 0 ||
1028 	    (bcmp(DN_BONUS(dnp), zerobuf, DN_MAX_BONUSLEN) == 0 &&
1029 	    DN_USED_BYTES(dnp) == 0));
1030 
1031 	if ((dnp->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) &&
1032 	    0 == used_cbs[os->os_phys->os_type](dnp->dn_bonustype,
1033 	    DN_BONUS(dnp), &user, &group)) {
1034 		int64_t delta = DNODE_SIZE + DN_USED_BYTES(dnp);
1035 		if (subtract)
1036 			delta = -delta;
1037 		VERIFY(0 == zap_increment_int(os, DMU_USERUSED_OBJECT,
1038 		    user, delta, tx));
1039 		VERIFY(0 == zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1040 		    group, delta, tx));
1041 	}
1042 }
1043 
1044 void
1045 dmu_objset_do_userquota_callbacks(objset_t *os, dmu_tx_t *tx)
1046 {
1047 	dnode_t *dn;
1048 	list_t *list = &os->os_synced_dnodes;
1049 
1050 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1051 
1052 	while (dn = list_head(list)) {
1053 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1054 		ASSERT(dn->dn_oldphys);
1055 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1056 		    dn->dn_phys->dn_flags &
1057 		    DNODE_FLAG_USERUSED_ACCOUNTED);
1058 
1059 		/* Allocate the user/groupused objects if necessary. */
1060 		if (os->os_userused_dnode->dn_type == DMU_OT_NONE) {
1061 			VERIFY(0 == zap_create_claim(os,
1062 			    DMU_USERUSED_OBJECT,
1063 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1064 			VERIFY(0 == zap_create_claim(os,
1065 			    DMU_GROUPUSED_OBJECT,
1066 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1067 		}
1068 
1069 		/*
1070 		 * We intentionally modify the zap object even if the
1071 		 * net delta (due to phys-oldphys) is zero.  Otherwise
1072 		 * the block of the zap obj could be shared between
1073 		 * datasets but need to be different between them after
1074 		 * a bprewrite.
1075 		 */
1076 		do_userquota_callback(os, dn->dn_oldphys, B_TRUE, tx);
1077 		do_userquota_callback(os, dn->dn_phys, B_FALSE, tx);
1078 
1079 		/*
1080 		 * The mutex is needed here for interlock with dnode_allocate.
1081 		 */
1082 		mutex_enter(&dn->dn_mtx);
1083 		zio_buf_free(dn->dn_oldphys, sizeof (dnode_phys_t));
1084 		dn->dn_oldphys = NULL;
1085 		mutex_exit(&dn->dn_mtx);
1086 
1087 		list_remove(list, dn);
1088 		dnode_rele(dn, list);
1089 	}
1090 }
1091 
1092 boolean_t
1093 dmu_objset_userspace_present(objset_t *os)
1094 {
1095 	return (os->os_phys->os_flags &
1096 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1097 }
1098 
1099 int
1100 dmu_objset_userspace_upgrade(objset_t *os)
1101 {
1102 	uint64_t obj;
1103 	int err = 0;
1104 
1105 	if (dmu_objset_userspace_present(os))
1106 		return (0);
1107 	if (!dmu_objset_userused_enabled(os))
1108 		return (ENOTSUP);
1109 	if (dmu_objset_is_snapshot(os))
1110 		return (EINVAL);
1111 
1112 	/*
1113 	 * We simply need to mark every object dirty, so that it will be
1114 	 * synced out and now accounted.  If this is called
1115 	 * concurrently, or if we already did some work before crashing,
1116 	 * that's fine, since we track each object's accounted state
1117 	 * independently.
1118 	 */
1119 
1120 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1121 		dmu_tx_t *tx;
1122 		dmu_buf_t *db;
1123 		int objerr;
1124 
1125 		if (issig(JUSTLOOKING) && issig(FORREAL))
1126 			return (EINTR);
1127 
1128 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1129 		if (objerr)
1130 			continue;
1131 		tx = dmu_tx_create(os);
1132 		dmu_tx_hold_bonus(tx, obj);
1133 		objerr = dmu_tx_assign(tx, TXG_WAIT);
1134 		if (objerr) {
1135 			dmu_tx_abort(tx);
1136 			continue;
1137 		}
1138 		dmu_buf_will_dirty(db, tx);
1139 		dmu_buf_rele(db, FTAG);
1140 		dmu_tx_commit(tx);
1141 	}
1142 
1143 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1144 	txg_wait_synced(dmu_objset_pool(os), 0);
1145 	return (0);
1146 }
1147 
1148 void
1149 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1150     uint64_t *usedobjsp, uint64_t *availobjsp)
1151 {
1152 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1153 	    usedobjsp, availobjsp);
1154 }
1155 
1156 uint64_t
1157 dmu_objset_fsid_guid(objset_t *os)
1158 {
1159 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1160 }
1161 
1162 void
1163 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1164 {
1165 	stat->dds_type = os->os_phys->os_type;
1166 	if (os->os_dsl_dataset)
1167 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1168 }
1169 
1170 void
1171 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1172 {
1173 	ASSERT(os->os_dsl_dataset ||
1174 	    os->os_phys->os_type == DMU_OST_META);
1175 
1176 	if (os->os_dsl_dataset != NULL)
1177 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1178 
1179 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1180 	    os->os_phys->os_type);
1181 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1182 	    dmu_objset_userspace_present(os));
1183 }
1184 
1185 int
1186 dmu_objset_is_snapshot(objset_t *os)
1187 {
1188 	if (os->os_dsl_dataset != NULL)
1189 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1190 	else
1191 		return (B_FALSE);
1192 }
1193 
1194 int
1195 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1196     boolean_t *conflict)
1197 {
1198 	dsl_dataset_t *ds = os->os_dsl_dataset;
1199 	uint64_t ignored;
1200 
1201 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1202 		return (ENOENT);
1203 
1204 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1205 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1206 	    real, maxlen, conflict));
1207 }
1208 
1209 int
1210 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1211     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1212 {
1213 	dsl_dataset_t *ds = os->os_dsl_dataset;
1214 	zap_cursor_t cursor;
1215 	zap_attribute_t attr;
1216 
1217 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1218 		return (ENOENT);
1219 
1220 	zap_cursor_init_serialized(&cursor,
1221 	    ds->ds_dir->dd_pool->dp_meta_objset,
1222 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1223 
1224 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1225 		zap_cursor_fini(&cursor);
1226 		return (ENOENT);
1227 	}
1228 
1229 	if (strlen(attr.za_name) + 1 > namelen) {
1230 		zap_cursor_fini(&cursor);
1231 		return (ENAMETOOLONG);
1232 	}
1233 
1234 	(void) strcpy(name, attr.za_name);
1235 	if (idp)
1236 		*idp = attr.za_first_integer;
1237 	if (case_conflict)
1238 		*case_conflict = attr.za_normalization_conflict;
1239 	zap_cursor_advance(&cursor);
1240 	*offp = zap_cursor_serialize(&cursor);
1241 	zap_cursor_fini(&cursor);
1242 
1243 	return (0);
1244 }
1245 
1246 int
1247 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1248     uint64_t *idp, uint64_t *offp)
1249 {
1250 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1251 	zap_cursor_t cursor;
1252 	zap_attribute_t attr;
1253 
1254 	/* there is no next dir on a snapshot! */
1255 	if (os->os_dsl_dataset->ds_object !=
1256 	    dd->dd_phys->dd_head_dataset_obj)
1257 		return (ENOENT);
1258 
1259 	zap_cursor_init_serialized(&cursor,
1260 	    dd->dd_pool->dp_meta_objset,
1261 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1262 
1263 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1264 		zap_cursor_fini(&cursor);
1265 		return (ENOENT);
1266 	}
1267 
1268 	if (strlen(attr.za_name) + 1 > namelen) {
1269 		zap_cursor_fini(&cursor);
1270 		return (ENAMETOOLONG);
1271 	}
1272 
1273 	(void) strcpy(name, attr.za_name);
1274 	if (idp)
1275 		*idp = attr.za_first_integer;
1276 	zap_cursor_advance(&cursor);
1277 	*offp = zap_cursor_serialize(&cursor);
1278 	zap_cursor_fini(&cursor);
1279 
1280 	return (0);
1281 }
1282 
1283 struct findarg {
1284 	int (*func)(char *, void *);
1285 	void *arg;
1286 };
1287 
1288 /* ARGSUSED */
1289 static int
1290 findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1291 {
1292 	struct findarg *fa = arg;
1293 	return (fa->func((char *)dsname, fa->arg));
1294 }
1295 
1296 /*
1297  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1298  * Perhaps change all callers to use dmu_objset_find_spa()?
1299  */
1300 int
1301 dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
1302 {
1303 	struct findarg fa;
1304 	fa.func = func;
1305 	fa.arg = arg;
1306 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
1307 }
1308 
1309 /*
1310  * Find all objsets under name, call func on each
1311  */
1312 int
1313 dmu_objset_find_spa(spa_t *spa, const char *name,
1314     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
1315 {
1316 	dsl_dir_t *dd;
1317 	dsl_pool_t *dp;
1318 	dsl_dataset_t *ds;
1319 	zap_cursor_t zc;
1320 	zap_attribute_t *attr;
1321 	char *child;
1322 	uint64_t thisobj;
1323 	int err;
1324 
1325 	if (name == NULL)
1326 		name = spa_name(spa);
1327 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
1328 	if (err)
1329 		return (err);
1330 
1331 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1332 	if (dd->dd_myname[0] == '$') {
1333 		dsl_dir_close(dd, FTAG);
1334 		return (0);
1335 	}
1336 
1337 	thisobj = dd->dd_phys->dd_head_dataset_obj;
1338 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1339 	dp = dd->dd_pool;
1340 
1341 	/*
1342 	 * Iterate over all children.
1343 	 */
1344 	if (flags & DS_FIND_CHILDREN) {
1345 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1346 		    dd->dd_phys->dd_child_dir_zapobj);
1347 		    zap_cursor_retrieve(&zc, attr) == 0;
1348 		    (void) zap_cursor_advance(&zc)) {
1349 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
1350 			ASSERT(attr->za_num_integers == 1);
1351 
1352 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1353 			(void) strcpy(child, name);
1354 			(void) strcat(child, "/");
1355 			(void) strcat(child, attr->za_name);
1356 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
1357 			kmem_free(child, MAXPATHLEN);
1358 			if (err)
1359 				break;
1360 		}
1361 		zap_cursor_fini(&zc);
1362 
1363 		if (err) {
1364 			dsl_dir_close(dd, FTAG);
1365 			kmem_free(attr, sizeof (zap_attribute_t));
1366 			return (err);
1367 		}
1368 	}
1369 
1370 	/*
1371 	 * Iterate over all snapshots.
1372 	 */
1373 	if (flags & DS_FIND_SNAPSHOTS) {
1374 		if (!dsl_pool_sync_context(dp))
1375 			rw_enter(&dp->dp_config_rwlock, RW_READER);
1376 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1377 		if (!dsl_pool_sync_context(dp))
1378 			rw_exit(&dp->dp_config_rwlock);
1379 
1380 		if (err == 0) {
1381 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1382 			dsl_dataset_rele(ds, FTAG);
1383 
1384 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1385 			    zap_cursor_retrieve(&zc, attr) == 0;
1386 			    (void) zap_cursor_advance(&zc)) {
1387 				ASSERT(attr->za_integer_length ==
1388 				    sizeof (uint64_t));
1389 				ASSERT(attr->za_num_integers == 1);
1390 
1391 				child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1392 				(void) strcpy(child, name);
1393 				(void) strcat(child, "@");
1394 				(void) strcat(child, attr->za_name);
1395 				err = func(spa, attr->za_first_integer,
1396 				    child, arg);
1397 				kmem_free(child, MAXPATHLEN);
1398 				if (err)
1399 					break;
1400 			}
1401 			zap_cursor_fini(&zc);
1402 		}
1403 	}
1404 
1405 	dsl_dir_close(dd, FTAG);
1406 	kmem_free(attr, sizeof (zap_attribute_t));
1407 
1408 	if (err)
1409 		return (err);
1410 
1411 	/*
1412 	 * Apply to self if appropriate.
1413 	 */
1414 	err = func(spa, thisobj, name, arg);
1415 	return (err);
1416 }
1417 
1418 /* ARGSUSED */
1419 int
1420 dmu_objset_prefetch(char *name, void *arg)
1421 {
1422 	dsl_dataset_t *ds;
1423 
1424 	if (dsl_dataset_hold(name, FTAG, &ds))
1425 		return (0);
1426 
1427 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
1428 		mutex_enter(&ds->ds_opening_lock);
1429 		if (ds->ds_objset == NULL) {
1430 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1431 			zbookmark_t zb;
1432 
1433 			zb.zb_objset = ds->ds_object;
1434 			zb.zb_object = 0;
1435 			zb.zb_level = -1;
1436 			zb.zb_blkid = 0;
1437 
1438 			(void) arc_read_nolock(NULL, dsl_dataset_get_spa(ds),
1439 			    &ds->ds_phys->ds_bp, NULL, NULL,
1440 			    ZIO_PRIORITY_ASYNC_READ,
1441 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1442 			    &aflags, &zb);
1443 		}
1444 		mutex_exit(&ds->ds_opening_lock);
1445 	}
1446 
1447 	dsl_dataset_rele(ds, FTAG);
1448 	return (0);
1449 }
1450 
1451 void
1452 dmu_objset_set_user(objset_t *os, void *user_ptr)
1453 {
1454 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1455 	os->os_user_ptr = user_ptr;
1456 }
1457 
1458 void *
1459 dmu_objset_get_user(objset_t *os)
1460 {
1461 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1462 	return (os->os_user_ptr);
1463 }
1464