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