xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_objset.c (revision feaa74e41c407fe56e66a47e097c2842d4f65b9f)
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, dsl_dataset_get_blkptr(ds), 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 != NULL)
606 		VERIFY(0 == dmu_objset_from_ds(ds, &os));
607 	else
608 		VERIFY(0 == dmu_objset_open_impl(spa, NULL, bp, &os));
609 
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 	spa_t *spa = dd->dd_pool->dp_spa;
699 	struct oscarg *oa = arg2;
700 	uint64_t obj;
701 
702 	ASSERT(dmu_tx_is_syncing(tx));
703 
704 	obj = dsl_dataset_create_sync(dd, oa->lastname,
705 	    oa->clone_origin, oa->flags, oa->cr, tx);
706 
707 	if (oa->clone_origin == NULL) {
708 		dsl_pool_t *dp = dd->dd_pool;
709 		dsl_dataset_t *ds;
710 		blkptr_t *bp;
711 		objset_t *os;
712 
713 		VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
714 		bp = dsl_dataset_get_blkptr(ds);
715 		ASSERT(BP_IS_HOLE(bp));
716 
717 		os = dmu_objset_create_impl(spa, ds, bp, oa->type, tx);
718 
719 		if (oa->userfunc)
720 			oa->userfunc(os, oa->userarg, oa->cr, tx);
721 		dsl_dataset_rele(ds, FTAG);
722 	}
723 
724 	spa_history_log_internal(LOG_DS_CREATE, spa, tx, "dataset = %llu", obj);
725 }
726 
727 int
728 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
729     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
730 {
731 	dsl_dir_t *pdd;
732 	const char *tail;
733 	int err = 0;
734 	struct oscarg oa = { 0 };
735 
736 	ASSERT(strchr(name, '@') == NULL);
737 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
738 	if (err)
739 		return (err);
740 	if (tail == NULL) {
741 		dsl_dir_close(pdd, FTAG);
742 		return (EEXIST);
743 	}
744 
745 	oa.userfunc = func;
746 	oa.userarg = arg;
747 	oa.lastname = tail;
748 	oa.type = type;
749 	oa.flags = flags;
750 	oa.cr = CRED();
751 
752 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
753 	    dmu_objset_create_sync, pdd, &oa, 5);
754 	dsl_dir_close(pdd, FTAG);
755 	return (err);
756 }
757 
758 int
759 dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
760 {
761 	dsl_dir_t *pdd;
762 	const char *tail;
763 	int err = 0;
764 	struct oscarg oa = { 0 };
765 
766 	ASSERT(strchr(name, '@') == NULL);
767 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
768 	if (err)
769 		return (err);
770 	if (tail == NULL) {
771 		dsl_dir_close(pdd, FTAG);
772 		return (EEXIST);
773 	}
774 
775 	oa.lastname = tail;
776 	oa.clone_origin = clone_origin;
777 	oa.flags = flags;
778 	oa.cr = CRED();
779 
780 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
781 	    dmu_objset_create_sync, pdd, &oa, 5);
782 	dsl_dir_close(pdd, FTAG);
783 	return (err);
784 }
785 
786 int
787 dmu_objset_destroy(const char *name, boolean_t defer)
788 {
789 	dsl_dataset_t *ds;
790 	int error;
791 
792 	error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
793 	if (error == 0) {
794 		error = dsl_dataset_destroy(ds, FTAG, defer);
795 		/* dsl_dataset_destroy() closes the ds. */
796 	}
797 
798 	return (error);
799 }
800 
801 struct snaparg {
802 	dsl_sync_task_group_t *dstg;
803 	char *snapname;
804 	char failed[MAXPATHLEN];
805 	boolean_t recursive;
806 	boolean_t needsuspend;
807 	nvlist_t *props;
808 };
809 
810 static int
811 snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
812 {
813 	objset_t *os = arg1;
814 	struct snaparg *sn = arg2;
815 
816 	/* The props have already been checked by zfs_check_userprops(). */
817 
818 	return (dsl_dataset_snapshot_check(os->os_dsl_dataset,
819 	    sn->snapname, tx));
820 }
821 
822 static void
823 snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)
824 {
825 	objset_t *os = arg1;
826 	dsl_dataset_t *ds = os->os_dsl_dataset;
827 	struct snaparg *sn = arg2;
828 
829 	dsl_dataset_snapshot_sync(ds, sn->snapname, tx);
830 
831 	if (sn->props) {
832 		dsl_props_arg_t pa;
833 		pa.pa_props = sn->props;
834 		pa.pa_source = ZPROP_SRC_LOCAL;
835 		dsl_props_set_sync(ds->ds_prev, &pa, tx);
836 	}
837 }
838 
839 static int
840 dmu_objset_snapshot_one(const char *name, void *arg)
841 {
842 	struct snaparg *sn = arg;
843 	objset_t *os;
844 	int err;
845 	char *cp;
846 
847 	/*
848 	 * If the objset starts with a '%', then ignore it unless it was
849 	 * explicitly named (ie, not recursive).  These hidden datasets
850 	 * are always inconsistent, and by not opening them here, we can
851 	 * avoid a race with dsl_dir_destroy_check().
852 	 */
853 	cp = strrchr(name, '/');
854 	if (cp && cp[1] == '%' && sn->recursive)
855 		return (0);
856 
857 	(void) strcpy(sn->failed, name);
858 
859 	/*
860 	 * Check permissions if we are doing a recursive snapshot.  The
861 	 * permission checks for the starting dataset have already been
862 	 * performed in zfs_secpolicy_snapshot()
863 	 */
864 	if (sn->recursive && (err = zfs_secpolicy_snapshot_perms(name, CRED())))
865 		return (err);
866 
867 	err = dmu_objset_hold(name, sn, &os);
868 	if (err != 0)
869 		return (err);
870 
871 	/*
872 	 * If the objset is in an inconsistent state (eg, in the process
873 	 * of being destroyed), don't snapshot it.  As with %hidden
874 	 * datasets, we return EBUSY if this name was explicitly
875 	 * requested (ie, not recursive), and otherwise ignore it.
876 	 */
877 	if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
878 		dmu_objset_rele(os, sn);
879 		return (sn->recursive ? 0 : EBUSY);
880 	}
881 
882 	if (sn->needsuspend) {
883 		err = zil_suspend(dmu_objset_zil(os));
884 		if (err) {
885 			dmu_objset_rele(os, sn);
886 			return (err);
887 		}
888 	}
889 	dsl_sync_task_create(sn->dstg, snapshot_check, snapshot_sync,
890 	    os, sn, 3);
891 
892 	return (0);
893 }
894 
895 int
896 dmu_objset_snapshot(char *fsname, char *snapname,
897     nvlist_t *props, boolean_t recursive)
898 {
899 	dsl_sync_task_t *dst;
900 	struct snaparg sn;
901 	spa_t *spa;
902 	int err;
903 
904 	(void) strcpy(sn.failed, fsname);
905 
906 	err = spa_open(fsname, &spa, FTAG);
907 	if (err)
908 		return (err);
909 
910 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
911 	sn.snapname = snapname;
912 	sn.props = props;
913 	sn.recursive = recursive;
914 	sn.needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
915 
916 	if (recursive) {
917 		err = dmu_objset_find(fsname,
918 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
919 	} else {
920 		err = dmu_objset_snapshot_one(fsname, &sn);
921 	}
922 
923 	if (err == 0)
924 		err = dsl_sync_task_group_wait(sn.dstg);
925 
926 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
927 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
928 		objset_t *os = dst->dst_arg1;
929 		dsl_dataset_t *ds = os->os_dsl_dataset;
930 		if (dst->dst_err)
931 			dsl_dataset_name(ds, sn.failed);
932 		if (sn.needsuspend)
933 			zil_resume(dmu_objset_zil(os));
934 		dmu_objset_rele(os, &sn);
935 	}
936 
937 	if (err)
938 		(void) strcpy(fsname, sn.failed);
939 	dsl_sync_task_group_destroy(sn.dstg);
940 	spa_close(spa, FTAG);
941 	return (err);
942 }
943 
944 static void
945 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
946 {
947 	dnode_t *dn;
948 
949 	while (dn = list_head(list)) {
950 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
951 		ASSERT(dn->dn_dbuf->db_data_pending);
952 		/*
953 		 * Initialize dn_zio outside dnode_sync() because the
954 		 * meta-dnode needs to set it ouside dnode_sync().
955 		 */
956 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
957 		ASSERT(dn->dn_zio);
958 
959 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
960 		list_remove(list, dn);
961 
962 		if (newlist) {
963 			(void) dnode_add_ref(dn, newlist);
964 			list_insert_tail(newlist, dn);
965 		}
966 
967 		dnode_sync(dn, tx);
968 	}
969 }
970 
971 /* ARGSUSED */
972 static void
973 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
974 {
975 	blkptr_t *bp = zio->io_bp;
976 	objset_t *os = arg;
977 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
978 
979 	ASSERT(bp == os->os_rootbp);
980 	ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
981 	ASSERT(BP_GET_LEVEL(bp) == 0);
982 
983 	/*
984 	 * Update rootbp fill count: it should be the number of objects
985 	 * allocated in the object set (not counting the "special"
986 	 * objects that are stored in the objset_phys_t -- the meta
987 	 * dnode and user/group accounting objects).
988 	 */
989 	bp->blk_fill = 0;
990 	for (int i = 0; i < dnp->dn_nblkptr; i++)
991 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
992 }
993 
994 /* ARGSUSED */
995 static void
996 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
997 {
998 	blkptr_t *bp = zio->io_bp;
999 	blkptr_t *bp_orig = &zio->io_bp_orig;
1000 	objset_t *os = arg;
1001 
1002 	if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1003 		ASSERT(BP_EQUAL(bp, bp_orig));
1004 	} else {
1005 		dsl_dataset_t *ds = os->os_dsl_dataset;
1006 		dmu_tx_t *tx = os->os_synctx;
1007 
1008 		(void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1009 		dsl_dataset_block_born(ds, bp, tx);
1010 	}
1011 }
1012 
1013 /* called from dsl */
1014 void
1015 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1016 {
1017 	int txgoff;
1018 	zbookmark_t zb;
1019 	zio_prop_t zp;
1020 	zio_t *zio;
1021 	list_t *list;
1022 	list_t *newlist = NULL;
1023 	dbuf_dirty_record_t *dr;
1024 
1025 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1026 
1027 	ASSERT(dmu_tx_is_syncing(tx));
1028 	/* XXX the write_done callback should really give us the tx... */
1029 	os->os_synctx = tx;
1030 
1031 	if (os->os_dsl_dataset == NULL) {
1032 		/*
1033 		 * This is the MOS.  If we have upgraded,
1034 		 * spa_max_replication() could change, so reset
1035 		 * os_copies here.
1036 		 */
1037 		os->os_copies = spa_max_replication(os->os_spa);
1038 	}
1039 
1040 	/*
1041 	 * Create the root block IO
1042 	 */
1043 	SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1044 	    os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1045 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1046 	VERIFY3U(0, ==, arc_release_bp(os->os_phys_buf, &os->os_phys_buf,
1047 	    os->os_rootbp, os->os_spa, &zb));
1048 
1049 	dmu_write_policy(os, NULL, 0, 0, &zp);
1050 
1051 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
1052 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
1053 	    dmu_objset_write_ready, dmu_objset_write_done, os,
1054 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1055 
1056 	/*
1057 	 * Sync special dnodes - the parent IO for the sync is the root block
1058 	 */
1059 	DMU_META_DNODE(os)->dn_zio = zio;
1060 	dnode_sync(DMU_META_DNODE(os), tx);
1061 
1062 	os->os_phys->os_flags = os->os_flags;
1063 
1064 	if (DMU_USERUSED_DNODE(os) &&
1065 	    DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1066 		DMU_USERUSED_DNODE(os)->dn_zio = zio;
1067 		dnode_sync(DMU_USERUSED_DNODE(os), tx);
1068 		DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1069 		dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1070 	}
1071 
1072 	txgoff = tx->tx_txg & TXG_MASK;
1073 
1074 	if (dmu_objset_userused_enabled(os)) {
1075 		newlist = &os->os_synced_dnodes;
1076 		/*
1077 		 * We must create the list here because it uses the
1078 		 * dn_dirty_link[] of this txg.
1079 		 */
1080 		list_create(newlist, sizeof (dnode_t),
1081 		    offsetof(dnode_t, dn_dirty_link[txgoff]));
1082 	}
1083 
1084 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1085 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1086 
1087 	list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1088 	while (dr = list_head(list)) {
1089 		ASSERT(dr->dr_dbuf->db_level == 0);
1090 		list_remove(list, dr);
1091 		if (dr->dr_zio)
1092 			zio_nowait(dr->dr_zio);
1093 	}
1094 	/*
1095 	 * Free intent log blocks up to this tx.
1096 	 */
1097 	zil_sync(os->os_zil, tx);
1098 	os->os_phys->os_zil_header = os->os_zil_header;
1099 	zio_nowait(zio);
1100 }
1101 
1102 boolean_t
1103 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1104 {
1105 	return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1106 	    !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1107 }
1108 
1109 boolean_t
1110 dmu_objset_is_dirty_anywhere(objset_t *os)
1111 {
1112 	for (int t = 0; t < TXG_SIZE; t++)
1113 		if (dmu_objset_is_dirty(os, t))
1114 			return (B_TRUE);
1115 	return (B_FALSE);
1116 }
1117 
1118 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1119 
1120 void
1121 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1122 {
1123 	used_cbs[ost] = cb;
1124 }
1125 
1126 boolean_t
1127 dmu_objset_userused_enabled(objset_t *os)
1128 {
1129 	return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1130 	    used_cbs[os->os_phys->os_type] != NULL &&
1131 	    DMU_USERUSED_DNODE(os) != NULL);
1132 }
1133 
1134 static void
1135 do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1136     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
1137 {
1138 	if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1139 		int64_t delta = DNODE_SIZE + used;
1140 		if (subtract)
1141 			delta = -delta;
1142 		VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
1143 		    user, delta, tx));
1144 		VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1145 		    group, delta, tx));
1146 	}
1147 }
1148 
1149 void
1150 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1151 {
1152 	dnode_t *dn;
1153 	list_t *list = &os->os_synced_dnodes;
1154 
1155 	ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1156 
1157 	while (dn = list_head(list)) {
1158 		int flags;
1159 		ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1160 		ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1161 		    dn->dn_phys->dn_flags &
1162 		    DNODE_FLAG_USERUSED_ACCOUNTED);
1163 
1164 		/* Allocate the user/groupused objects if necessary. */
1165 		if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1166 			VERIFY(0 == zap_create_claim(os,
1167 			    DMU_USERUSED_OBJECT,
1168 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1169 			VERIFY(0 == zap_create_claim(os,
1170 			    DMU_GROUPUSED_OBJECT,
1171 			    DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1172 		}
1173 
1174 		/*
1175 		 * We intentionally modify the zap object even if the
1176 		 * net delta is zero.  Otherwise
1177 		 * the block of the zap obj could be shared between
1178 		 * datasets but need to be different between them after
1179 		 * a bprewrite.
1180 		 */
1181 
1182 		flags = dn->dn_id_flags;
1183 		ASSERT(flags);
1184 		if (flags & DN_ID_OLD_EXIST)  {
1185 			do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1186 			    dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1187 		}
1188 		if (flags & DN_ID_NEW_EXIST) {
1189 			do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1190 			    dn->dn_phys->dn_flags,  dn->dn_newuid,
1191 			    dn->dn_newgid, B_FALSE, tx);
1192 		}
1193 
1194 		mutex_enter(&dn->dn_mtx);
1195 		dn->dn_oldused = 0;
1196 		dn->dn_oldflags = 0;
1197 		if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1198 			dn->dn_olduid = dn->dn_newuid;
1199 			dn->dn_oldgid = dn->dn_newgid;
1200 			dn->dn_id_flags |= DN_ID_OLD_EXIST;
1201 			if (dn->dn_bonuslen == 0)
1202 				dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1203 			else
1204 				dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1205 		}
1206 		dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1207 		mutex_exit(&dn->dn_mtx);
1208 
1209 		list_remove(list, dn);
1210 		dnode_rele(dn, list);
1211 	}
1212 }
1213 
1214 /*
1215  * Returns a pointer to data to find uid/gid from
1216  *
1217  * If a dirty record for transaction group that is syncing can't
1218  * be found then NULL is returned.  In the NULL case it is assumed
1219  * the uid/gid aren't changing.
1220  */
1221 static void *
1222 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1223 {
1224 	dbuf_dirty_record_t *dr, **drp;
1225 	void *data;
1226 
1227 	if (db->db_dirtycnt == 0)
1228 		return (db->db.db_data);  /* Nothing is changing */
1229 
1230 	for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1231 		if (dr->dr_txg == tx->tx_txg)
1232 			break;
1233 
1234 	if (dr == NULL) {
1235 		data = NULL;
1236 	} else {
1237 		dnode_t *dn;
1238 
1239 		DB_DNODE_ENTER(dr->dr_dbuf);
1240 		dn = DB_DNODE(dr->dr_dbuf);
1241 
1242 		if (dn->dn_bonuslen == 0 &&
1243 		    dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1244 			data = dr->dt.dl.dr_data->b_data;
1245 		else
1246 			data = dr->dt.dl.dr_data;
1247 
1248 		DB_DNODE_EXIT(dr->dr_dbuf);
1249 	}
1250 
1251 	return (data);
1252 }
1253 
1254 void
1255 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1256 {
1257 	objset_t *os = dn->dn_objset;
1258 	void *data = NULL;
1259 	dmu_buf_impl_t *db = NULL;
1260 	uint64_t *user, *group;
1261 	int flags = dn->dn_id_flags;
1262 	int error;
1263 	boolean_t have_spill = B_FALSE;
1264 
1265 	if (!dmu_objset_userused_enabled(dn->dn_objset))
1266 		return;
1267 
1268 	if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1269 	    DN_ID_CHKED_SPILL)))
1270 		return;
1271 
1272 	if (before && dn->dn_bonuslen != 0)
1273 		data = DN_BONUS(dn->dn_phys);
1274 	else if (!before && dn->dn_bonuslen != 0) {
1275 		if (dn->dn_bonus) {
1276 			db = dn->dn_bonus;
1277 			mutex_enter(&db->db_mtx);
1278 			data = dmu_objset_userquota_find_data(db, tx);
1279 		} else {
1280 			data = DN_BONUS(dn->dn_phys);
1281 		}
1282 	} else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1283 			int rf = 0;
1284 
1285 			if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1286 				rf |= DB_RF_HAVESTRUCT;
1287 			error = dmu_spill_hold_by_dnode(dn,
1288 			    rf | DB_RF_MUST_SUCCEED,
1289 			    FTAG, (dmu_buf_t **)&db);
1290 			ASSERT(error == 0);
1291 			mutex_enter(&db->db_mtx);
1292 			data = (before) ? db->db.db_data :
1293 			    dmu_objset_userquota_find_data(db, tx);
1294 			have_spill = B_TRUE;
1295 	} else {
1296 		mutex_enter(&dn->dn_mtx);
1297 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1298 		mutex_exit(&dn->dn_mtx);
1299 		return;
1300 	}
1301 
1302 	if (before) {
1303 		ASSERT(data);
1304 		user = &dn->dn_olduid;
1305 		group = &dn->dn_oldgid;
1306 	} else if (data) {
1307 		user = &dn->dn_newuid;
1308 		group = &dn->dn_newgid;
1309 	}
1310 
1311 	/*
1312 	 * Must always call the callback in case the object
1313 	 * type has changed and that type isn't an object type to track
1314 	 */
1315 	error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1316 	    user, group);
1317 
1318 	/*
1319 	 * Preserve existing uid/gid when the callback can't determine
1320 	 * what the new uid/gid are and the callback returned EEXIST.
1321 	 * The EEXIST error tells us to just use the existing uid/gid.
1322 	 * If we don't know what the old values are then just assign
1323 	 * them to 0, since that is a new file  being created.
1324 	 */
1325 	if (!before && data == NULL && error == EEXIST) {
1326 		if (flags & DN_ID_OLD_EXIST) {
1327 			dn->dn_newuid = dn->dn_olduid;
1328 			dn->dn_newgid = dn->dn_oldgid;
1329 		} else {
1330 			dn->dn_newuid = 0;
1331 			dn->dn_newgid = 0;
1332 		}
1333 		error = 0;
1334 	}
1335 
1336 	if (db)
1337 		mutex_exit(&db->db_mtx);
1338 
1339 	mutex_enter(&dn->dn_mtx);
1340 	if (error == 0 && before)
1341 		dn->dn_id_flags |= DN_ID_OLD_EXIST;
1342 	if (error == 0 && !before)
1343 		dn->dn_id_flags |= DN_ID_NEW_EXIST;
1344 
1345 	if (have_spill) {
1346 		dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1347 	} else {
1348 		dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1349 	}
1350 	mutex_exit(&dn->dn_mtx);
1351 	if (have_spill)
1352 		dmu_buf_rele((dmu_buf_t *)db, FTAG);
1353 }
1354 
1355 boolean_t
1356 dmu_objset_userspace_present(objset_t *os)
1357 {
1358 	return (os->os_phys->os_flags &
1359 	    OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1360 }
1361 
1362 int
1363 dmu_objset_userspace_upgrade(objset_t *os)
1364 {
1365 	uint64_t obj;
1366 	int err = 0;
1367 
1368 	if (dmu_objset_userspace_present(os))
1369 		return (0);
1370 	if (!dmu_objset_userused_enabled(os))
1371 		return (ENOTSUP);
1372 	if (dmu_objset_is_snapshot(os))
1373 		return (EINVAL);
1374 
1375 	/*
1376 	 * We simply need to mark every object dirty, so that it will be
1377 	 * synced out and now accounted.  If this is called
1378 	 * concurrently, or if we already did some work before crashing,
1379 	 * that's fine, since we track each object's accounted state
1380 	 * independently.
1381 	 */
1382 
1383 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1384 		dmu_tx_t *tx;
1385 		dmu_buf_t *db;
1386 		int objerr;
1387 
1388 		if (issig(JUSTLOOKING) && issig(FORREAL))
1389 			return (EINTR);
1390 
1391 		objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1392 		if (objerr)
1393 			continue;
1394 		tx = dmu_tx_create(os);
1395 		dmu_tx_hold_bonus(tx, obj);
1396 		objerr = dmu_tx_assign(tx, TXG_WAIT);
1397 		if (objerr) {
1398 			dmu_tx_abort(tx);
1399 			continue;
1400 		}
1401 		dmu_buf_will_dirty(db, tx);
1402 		dmu_buf_rele(db, FTAG);
1403 		dmu_tx_commit(tx);
1404 	}
1405 
1406 	os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1407 	txg_wait_synced(dmu_objset_pool(os), 0);
1408 	return (0);
1409 }
1410 
1411 void
1412 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1413     uint64_t *usedobjsp, uint64_t *availobjsp)
1414 {
1415 	dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1416 	    usedobjsp, availobjsp);
1417 }
1418 
1419 uint64_t
1420 dmu_objset_fsid_guid(objset_t *os)
1421 {
1422 	return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1423 }
1424 
1425 void
1426 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1427 {
1428 	stat->dds_type = os->os_phys->os_type;
1429 	if (os->os_dsl_dataset)
1430 		dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1431 }
1432 
1433 void
1434 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1435 {
1436 	ASSERT(os->os_dsl_dataset ||
1437 	    os->os_phys->os_type == DMU_OST_META);
1438 
1439 	if (os->os_dsl_dataset != NULL)
1440 		dsl_dataset_stats(os->os_dsl_dataset, nv);
1441 
1442 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1443 	    os->os_phys->os_type);
1444 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1445 	    dmu_objset_userspace_present(os));
1446 }
1447 
1448 int
1449 dmu_objset_is_snapshot(objset_t *os)
1450 {
1451 	if (os->os_dsl_dataset != NULL)
1452 		return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1453 	else
1454 		return (B_FALSE);
1455 }
1456 
1457 int
1458 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1459     boolean_t *conflict)
1460 {
1461 	dsl_dataset_t *ds = os->os_dsl_dataset;
1462 	uint64_t ignored;
1463 
1464 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1465 		return (ENOENT);
1466 
1467 	return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1468 	    ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1469 	    real, maxlen, conflict));
1470 }
1471 
1472 int
1473 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1474     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1475 {
1476 	dsl_dataset_t *ds = os->os_dsl_dataset;
1477 	zap_cursor_t cursor;
1478 	zap_attribute_t attr;
1479 
1480 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
1481 		return (ENOENT);
1482 
1483 	zap_cursor_init_serialized(&cursor,
1484 	    ds->ds_dir->dd_pool->dp_meta_objset,
1485 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
1486 
1487 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1488 		zap_cursor_fini(&cursor);
1489 		return (ENOENT);
1490 	}
1491 
1492 	if (strlen(attr.za_name) + 1 > namelen) {
1493 		zap_cursor_fini(&cursor);
1494 		return (ENAMETOOLONG);
1495 	}
1496 
1497 	(void) strcpy(name, attr.za_name);
1498 	if (idp)
1499 		*idp = attr.za_first_integer;
1500 	if (case_conflict)
1501 		*case_conflict = attr.za_normalization_conflict;
1502 	zap_cursor_advance(&cursor);
1503 	*offp = zap_cursor_serialize(&cursor);
1504 	zap_cursor_fini(&cursor);
1505 
1506 	return (0);
1507 }
1508 
1509 int
1510 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1511     uint64_t *idp, uint64_t *offp)
1512 {
1513 	dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1514 	zap_cursor_t cursor;
1515 	zap_attribute_t attr;
1516 
1517 	/* there is no next dir on a snapshot! */
1518 	if (os->os_dsl_dataset->ds_object !=
1519 	    dd->dd_phys->dd_head_dataset_obj)
1520 		return (ENOENT);
1521 
1522 	zap_cursor_init_serialized(&cursor,
1523 	    dd->dd_pool->dp_meta_objset,
1524 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
1525 
1526 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1527 		zap_cursor_fini(&cursor);
1528 		return (ENOENT);
1529 	}
1530 
1531 	if (strlen(attr.za_name) + 1 > namelen) {
1532 		zap_cursor_fini(&cursor);
1533 		return (ENAMETOOLONG);
1534 	}
1535 
1536 	(void) strcpy(name, attr.za_name);
1537 	if (idp)
1538 		*idp = attr.za_first_integer;
1539 	zap_cursor_advance(&cursor);
1540 	*offp = zap_cursor_serialize(&cursor);
1541 	zap_cursor_fini(&cursor);
1542 
1543 	return (0);
1544 }
1545 
1546 struct findarg {
1547 	int (*func)(const char *, void *);
1548 	void *arg;
1549 };
1550 
1551 /* ARGSUSED */
1552 static int
1553 findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1554 {
1555 	struct findarg *fa = arg;
1556 	return (fa->func(dsname, fa->arg));
1557 }
1558 
1559 /*
1560  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1561  * Perhaps change all callers to use dmu_objset_find_spa()?
1562  */
1563 int
1564 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
1565     int flags)
1566 {
1567 	struct findarg fa;
1568 	fa.func = func;
1569 	fa.arg = arg;
1570 	return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
1571 }
1572 
1573 /*
1574  * Find all objsets under name, call func on each
1575  */
1576 int
1577 dmu_objset_find_spa(spa_t *spa, const char *name,
1578     int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
1579 {
1580 	dsl_dir_t *dd;
1581 	dsl_pool_t *dp;
1582 	dsl_dataset_t *ds;
1583 	zap_cursor_t zc;
1584 	zap_attribute_t *attr;
1585 	char *child;
1586 	uint64_t thisobj;
1587 	int err;
1588 
1589 	if (name == NULL)
1590 		name = spa_name(spa);
1591 	err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
1592 	if (err)
1593 		return (err);
1594 
1595 	/* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1596 	if (dd->dd_myname[0] == '$') {
1597 		dsl_dir_close(dd, FTAG);
1598 		return (0);
1599 	}
1600 
1601 	thisobj = dd->dd_phys->dd_head_dataset_obj;
1602 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1603 	dp = dd->dd_pool;
1604 
1605 	/*
1606 	 * Iterate over all children.
1607 	 */
1608 	if (flags & DS_FIND_CHILDREN) {
1609 		for (zap_cursor_init(&zc, dp->dp_meta_objset,
1610 		    dd->dd_phys->dd_child_dir_zapobj);
1611 		    zap_cursor_retrieve(&zc, attr) == 0;
1612 		    (void) zap_cursor_advance(&zc)) {
1613 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
1614 			ASSERT(attr->za_num_integers == 1);
1615 
1616 			child = kmem_asprintf("%s/%s", name, attr->za_name);
1617 			err = dmu_objset_find_spa(spa, child, func, arg, flags);
1618 			strfree(child);
1619 			if (err)
1620 				break;
1621 		}
1622 		zap_cursor_fini(&zc);
1623 
1624 		if (err) {
1625 			dsl_dir_close(dd, FTAG);
1626 			kmem_free(attr, sizeof (zap_attribute_t));
1627 			return (err);
1628 		}
1629 	}
1630 
1631 	/*
1632 	 * Iterate over all snapshots.
1633 	 */
1634 	if (flags & DS_FIND_SNAPSHOTS) {
1635 		if (!dsl_pool_sync_context(dp))
1636 			rw_enter(&dp->dp_config_rwlock, RW_READER);
1637 		err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1638 		if (!dsl_pool_sync_context(dp))
1639 			rw_exit(&dp->dp_config_rwlock);
1640 
1641 		if (err == 0) {
1642 			uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1643 			dsl_dataset_rele(ds, FTAG);
1644 
1645 			for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1646 			    zap_cursor_retrieve(&zc, attr) == 0;
1647 			    (void) zap_cursor_advance(&zc)) {
1648 				ASSERT(attr->za_integer_length ==
1649 				    sizeof (uint64_t));
1650 				ASSERT(attr->za_num_integers == 1);
1651 
1652 				child = kmem_asprintf("%s@%s",
1653 				    name, attr->za_name);
1654 				err = func(spa, attr->za_first_integer,
1655 				    child, arg);
1656 				strfree(child);
1657 				if (err)
1658 					break;
1659 			}
1660 			zap_cursor_fini(&zc);
1661 		}
1662 	}
1663 
1664 	dsl_dir_close(dd, FTAG);
1665 	kmem_free(attr, sizeof (zap_attribute_t));
1666 
1667 	if (err)
1668 		return (err);
1669 
1670 	/*
1671 	 * Apply to self if appropriate.
1672 	 */
1673 	err = func(spa, thisobj, name, arg);
1674 	return (err);
1675 }
1676 
1677 /* ARGSUSED */
1678 int
1679 dmu_objset_prefetch(const char *name, void *arg)
1680 {
1681 	dsl_dataset_t *ds;
1682 
1683 	if (dsl_dataset_hold(name, FTAG, &ds))
1684 		return (0);
1685 
1686 	if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
1687 		mutex_enter(&ds->ds_opening_lock);
1688 		if (ds->ds_objset == NULL) {
1689 			uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1690 			zbookmark_t zb;
1691 
1692 			SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
1693 			    ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1694 
1695 			(void) dsl_read_nolock(NULL, dsl_dataset_get_spa(ds),
1696 			    &ds->ds_phys->ds_bp, NULL, NULL,
1697 			    ZIO_PRIORITY_ASYNC_READ,
1698 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1699 			    &aflags, &zb);
1700 		}
1701 		mutex_exit(&ds->ds_opening_lock);
1702 	}
1703 
1704 	dsl_dataset_rele(ds, FTAG);
1705 	return (0);
1706 }
1707 
1708 void
1709 dmu_objset_set_user(objset_t *os, void *user_ptr)
1710 {
1711 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1712 	os->os_user_ptr = user_ptr;
1713 }
1714 
1715 void *
1716 dmu_objset_get_user(objset_t *os)
1717 {
1718 	ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1719 	return (os->os_user_ptr);
1720 }
1721