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