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