xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_vfsops.c (revision 15fa1d047e03d3f123546d72f130c5ce4b278eba)
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, 2015 by Delphix. All rights reserved.
24  * Copyright (c) 2014 Integros [integros.com]
25  * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
26  * Copyright 2019 Joyent, Inc.
27  * Copyright 2020 Joshua M. Clulow <josh@sysmgr.org>
28  */
29 
30 /* Portions Copyright 2010 Robert Milkowski */
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/kmem.h>
37 #include <sys/pathname.h>
38 #include <sys/vnode.h>
39 #include <sys/vfs.h>
40 #include <sys/vfs_opreg.h>
41 #include <sys/mntent.h>
42 #include <sys/mount.h>
43 #include <sys/cmn_err.h>
44 #include "fs/fs_subr.h"
45 #include <sys/zfs_znode.h>
46 #include <sys/zfs_dir.h>
47 #include <sys/zil.h>
48 #include <sys/fs/zfs.h>
49 #include <sys/dmu.h>
50 #include <sys/dsl_prop.h>
51 #include <sys/dsl_dataset.h>
52 #include <sys/dsl_deleg.h>
53 #include <sys/spa.h>
54 #include <sys/zap.h>
55 #include <sys/sa.h>
56 #include <sys/sa_impl.h>
57 #include <sys/varargs.h>
58 #include <sys/policy.h>
59 #include <sys/atomic.h>
60 #include <sys/mkdev.h>
61 #include <sys/modctl.h>
62 #include <sys/refstr.h>
63 #include <sys/zfs_ioctl.h>
64 #include <sys/zfs_ctldir.h>
65 #include <sys/zfs_fuid.h>
66 #include <sys/bootconf.h>
67 #include <sys/ddi.h>
68 #include <sys/sunddi.h>
69 #include <sys/dnlc.h>
70 #include <sys/dmu_objset.h>
71 #include <sys/spa_boot.h>
72 #include <sys/vdev_impl.h>
73 #include "zfs_comutil.h"
74 
75 int zfsfstype;
76 vfsops_t *zfs_vfsops = NULL;
77 static major_t zfs_major;
78 static minor_t zfs_minor;
79 static kmutex_t	zfs_dev_mtx;
80 
81 extern int sys_shutdown;
82 
83 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
84 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
85 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
86 static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
87 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
88 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
89 static void zfs_freevfs(vfs_t *vfsp);
90 
91 static const fs_operation_def_t zfs_vfsops_template[] = {
92 	VFSNAME_MOUNT,		{ .vfs_mount = zfs_mount },
93 	VFSNAME_MOUNTROOT,	{ .vfs_mountroot = zfs_mountroot },
94 	VFSNAME_UNMOUNT,	{ .vfs_unmount = zfs_umount },
95 	VFSNAME_ROOT,		{ .vfs_root = zfs_root },
96 	VFSNAME_STATVFS,	{ .vfs_statvfs = zfs_statvfs },
97 	VFSNAME_SYNC,		{ .vfs_sync = zfs_sync },
98 	VFSNAME_VGET,		{ .vfs_vget = zfs_vget },
99 	VFSNAME_FREEVFS,	{ .vfs_freevfs = zfs_freevfs },
100 	NULL,			NULL
101 };
102 
103 /*
104  * We need to keep a count of active fs's.
105  * This is necessary to prevent our module
106  * from being unloaded after a umount -f
107  */
108 static uint32_t	zfs_active_fs_count = 0;
109 
110 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
111 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
112 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
113 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
114 
115 /*
116  * MO_DEFAULT is not used since the default value is determined
117  * by the equivalent property.
118  */
119 static mntopt_t mntopts[] = {
120 	{ MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
121 	{ MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
122 	{ MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
123 	{ MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
124 };
125 
126 static mntopts_t zfs_mntopts = {
127 	sizeof (mntopts) / sizeof (mntopt_t),
128 	mntopts
129 };
130 
131 /*ARGSUSED*/
132 int
133 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
134 {
135 	/*
136 	 * Data integrity is job one.  We don't want a compromised kernel
137 	 * writing to the storage pool, so we never sync during panic.
138 	 */
139 	if (panicstr)
140 		return (0);
141 
142 	/*
143 	 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
144 	 * to sync metadata, which they would otherwise cache indefinitely.
145 	 * Semantically, the only requirement is that the sync be initiated.
146 	 * The DMU syncs out txgs frequently, so there's nothing to do.
147 	 */
148 	if (flag & SYNC_ATTR)
149 		return (0);
150 
151 	if (vfsp != NULL) {
152 		/*
153 		 * Sync a specific filesystem.
154 		 */
155 		zfsvfs_t *zfsvfs = vfsp->vfs_data;
156 		dsl_pool_t *dp;
157 
158 		ZFS_ENTER(zfsvfs);
159 		dp = dmu_objset_pool(zfsvfs->z_os);
160 
161 		/*
162 		 * If the system is shutting down, then skip any
163 		 * filesystems which may exist on a suspended pool.
164 		 */
165 		if (sys_shutdown && spa_suspended(dp->dp_spa)) {
166 			ZFS_EXIT(zfsvfs);
167 			return (0);
168 		}
169 
170 		if (zfsvfs->z_log != NULL)
171 			zil_commit(zfsvfs->z_log, 0);
172 
173 		ZFS_EXIT(zfsvfs);
174 	} else {
175 		/*
176 		 * Sync all ZFS filesystems.  This is what happens when you
177 		 * run sync(1M).  Unlike other filesystems, ZFS honors the
178 		 * request by waiting for all pools to commit all dirty data.
179 		 */
180 		spa_sync_allpools();
181 	}
182 
183 	return (0);
184 }
185 
186 static int
187 zfs_create_unique_device(dev_t *dev)
188 {
189 	major_t new_major;
190 
191 	do {
192 		ASSERT3U(zfs_minor, <=, MAXMIN32);
193 		minor_t start = zfs_minor;
194 		do {
195 			mutex_enter(&zfs_dev_mtx);
196 			if (zfs_minor >= MAXMIN32) {
197 				/*
198 				 * If we're still using the real major
199 				 * keep out of /dev/zfs and /dev/zvol minor
200 				 * number space.  If we're using a getudev()'ed
201 				 * major number, we can use all of its minors.
202 				 */
203 				if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
204 					zfs_minor = ZFS_MIN_MINOR;
205 				else
206 					zfs_minor = 0;
207 			} else {
208 				zfs_minor++;
209 			}
210 			*dev = makedevice(zfs_major, zfs_minor);
211 			mutex_exit(&zfs_dev_mtx);
212 		} while (vfs_devismounted(*dev) && zfs_minor != start);
213 		if (zfs_minor == start) {
214 			/*
215 			 * We are using all ~262,000 minor numbers for the
216 			 * current major number.  Create a new major number.
217 			 */
218 			if ((new_major = getudev()) == (major_t)-1) {
219 				cmn_err(CE_WARN,
220 				    "zfs_mount: Can't get unique major "
221 				    "device number.");
222 				return (-1);
223 			}
224 			mutex_enter(&zfs_dev_mtx);
225 			zfs_major = new_major;
226 			zfs_minor = 0;
227 
228 			mutex_exit(&zfs_dev_mtx);
229 		} else {
230 			break;
231 		}
232 		/* CONSTANTCONDITION */
233 	} while (1);
234 
235 	return (0);
236 }
237 
238 static void
239 atime_changed_cb(void *arg, uint64_t newval)
240 {
241 	zfsvfs_t *zfsvfs = arg;
242 
243 	if (newval == TRUE) {
244 		zfsvfs->z_atime = TRUE;
245 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
246 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
247 	} else {
248 		zfsvfs->z_atime = FALSE;
249 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
250 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
251 	}
252 }
253 
254 static void
255 xattr_changed_cb(void *arg, uint64_t newval)
256 {
257 	zfsvfs_t *zfsvfs = arg;
258 
259 	if (newval == TRUE) {
260 		/* XXX locking on vfs_flag? */
261 		zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
262 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
263 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
264 	} else {
265 		/* XXX locking on vfs_flag? */
266 		zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
267 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
268 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
269 	}
270 }
271 
272 static void
273 blksz_changed_cb(void *arg, uint64_t newval)
274 {
275 	zfsvfs_t *zfsvfs = arg;
276 	ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os)));
277 	ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
278 	ASSERT(ISP2(newval));
279 
280 	zfsvfs->z_max_blksz = newval;
281 	zfsvfs->z_vfs->vfs_bsize = newval;
282 }
283 
284 static void
285 readonly_changed_cb(void *arg, uint64_t newval)
286 {
287 	zfsvfs_t *zfsvfs = arg;
288 
289 	if (newval) {
290 		/* XXX locking on vfs_flag? */
291 		zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
292 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
293 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
294 	} else {
295 		/* XXX locking on vfs_flag? */
296 		zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
297 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
298 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
299 	}
300 }
301 
302 static void
303 devices_changed_cb(void *arg, uint64_t newval)
304 {
305 	zfsvfs_t *zfsvfs = arg;
306 
307 	if (newval == FALSE) {
308 		zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
309 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
310 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
311 	} else {
312 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
313 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
314 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
315 	}
316 }
317 
318 static void
319 setuid_changed_cb(void *arg, uint64_t newval)
320 {
321 	zfsvfs_t *zfsvfs = arg;
322 
323 	if (newval == FALSE) {
324 		zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
325 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
326 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
327 	} else {
328 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
329 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
330 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
331 	}
332 }
333 
334 static void
335 exec_changed_cb(void *arg, uint64_t newval)
336 {
337 	zfsvfs_t *zfsvfs = arg;
338 
339 	if (newval == FALSE) {
340 		zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
341 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
342 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
343 	} else {
344 		zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
345 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
346 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
347 	}
348 }
349 
350 /*
351  * The nbmand mount option can be changed at mount time.
352  * We can't allow it to be toggled on live file systems or incorrect
353  * behavior may be seen from cifs clients
354  *
355  * This property isn't registered via dsl_prop_register(), but this callback
356  * will be called when a file system is first mounted
357  */
358 static void
359 nbmand_changed_cb(void *arg, uint64_t newval)
360 {
361 	zfsvfs_t *zfsvfs = arg;
362 	if (newval == FALSE) {
363 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
364 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
365 	} else {
366 		vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
367 		vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
368 	}
369 }
370 
371 static void
372 snapdir_changed_cb(void *arg, uint64_t newval)
373 {
374 	zfsvfs_t *zfsvfs = arg;
375 
376 	zfsvfs->z_show_ctldir = newval;
377 }
378 
379 static void
380 vscan_changed_cb(void *arg, uint64_t newval)
381 {
382 	zfsvfs_t *zfsvfs = arg;
383 
384 	zfsvfs->z_vscan = newval;
385 }
386 
387 static void
388 acl_mode_changed_cb(void *arg, uint64_t newval)
389 {
390 	zfsvfs_t *zfsvfs = arg;
391 
392 	zfsvfs->z_acl_mode = newval;
393 }
394 
395 static void
396 acl_inherit_changed_cb(void *arg, uint64_t newval)
397 {
398 	zfsvfs_t *zfsvfs = arg;
399 
400 	zfsvfs->z_acl_inherit = newval;
401 }
402 
403 static int
404 zfs_register_callbacks(vfs_t *vfsp)
405 {
406 	struct dsl_dataset *ds = NULL;
407 	objset_t *os = NULL;
408 	zfsvfs_t *zfsvfs = NULL;
409 	uint64_t nbmand;
410 	boolean_t readonly = B_FALSE;
411 	boolean_t do_readonly = B_FALSE;
412 	boolean_t setuid = B_FALSE;
413 	boolean_t do_setuid = B_FALSE;
414 	boolean_t exec = B_FALSE;
415 	boolean_t do_exec = B_FALSE;
416 	boolean_t devices = B_FALSE;
417 	boolean_t do_devices = B_FALSE;
418 	boolean_t xattr = B_FALSE;
419 	boolean_t do_xattr = B_FALSE;
420 	boolean_t atime = B_FALSE;
421 	boolean_t do_atime = B_FALSE;
422 	int error = 0;
423 
424 	ASSERT(vfsp);
425 	zfsvfs = vfsp->vfs_data;
426 	ASSERT(zfsvfs);
427 	os = zfsvfs->z_os;
428 
429 	/*
430 	 * The act of registering our callbacks will destroy any mount
431 	 * options we may have.  In order to enable temporary overrides
432 	 * of mount options, we stash away the current values and
433 	 * restore them after we register the callbacks.
434 	 */
435 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
436 	    !spa_writeable(dmu_objset_spa(os))) {
437 		readonly = B_TRUE;
438 		do_readonly = B_TRUE;
439 	} else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
440 		readonly = B_FALSE;
441 		do_readonly = B_TRUE;
442 	}
443 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
444 		devices = B_FALSE;
445 		setuid = B_FALSE;
446 		do_devices = B_TRUE;
447 		do_setuid = B_TRUE;
448 	} else {
449 		if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
450 			devices = B_FALSE;
451 			do_devices = B_TRUE;
452 		} else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
453 			devices = B_TRUE;
454 			do_devices = B_TRUE;
455 		}
456 
457 		if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
458 			setuid = B_FALSE;
459 			do_setuid = B_TRUE;
460 		} else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
461 			setuid = B_TRUE;
462 			do_setuid = B_TRUE;
463 		}
464 	}
465 	if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
466 		exec = B_FALSE;
467 		do_exec = B_TRUE;
468 	} else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
469 		exec = B_TRUE;
470 		do_exec = B_TRUE;
471 	}
472 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
473 		xattr = B_FALSE;
474 		do_xattr = B_TRUE;
475 	} else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
476 		xattr = B_TRUE;
477 		do_xattr = B_TRUE;
478 	}
479 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
480 		atime = B_FALSE;
481 		do_atime = B_TRUE;
482 	} else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
483 		atime = B_TRUE;
484 		do_atime = B_TRUE;
485 	}
486 
487 	/*
488 	 * nbmand is a special property.  It can only be changed at
489 	 * mount time.
490 	 *
491 	 * This is weird, but it is documented to only be changeable
492 	 * at mount time.
493 	 */
494 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
495 		nbmand = B_FALSE;
496 	} else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
497 		nbmand = B_TRUE;
498 	} else {
499 		char osname[ZFS_MAX_DATASET_NAME_LEN];
500 
501 		dmu_objset_name(os, osname);
502 		if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
503 		    NULL)) {
504 			return (error);
505 		}
506 	}
507 
508 	/*
509 	 * Register property callbacks.
510 	 *
511 	 * It would probably be fine to just check for i/o error from
512 	 * the first prop_register(), but I guess I like to go
513 	 * overboard...
514 	 */
515 	ds = dmu_objset_ds(os);
516 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
517 	error = dsl_prop_register(ds,
518 	    zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs);
519 	error = error ? error : dsl_prop_register(ds,
520 	    zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs);
521 	error = error ? error : dsl_prop_register(ds,
522 	    zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs);
523 	error = error ? error : dsl_prop_register(ds,
524 	    zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs);
525 	error = error ? error : dsl_prop_register(ds,
526 	    zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zfsvfs);
527 	error = error ? error : dsl_prop_register(ds,
528 	    zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs);
529 	error = error ? error : dsl_prop_register(ds,
530 	    zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs);
531 	error = error ? error : dsl_prop_register(ds,
532 	    zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs);
533 	error = error ? error : dsl_prop_register(ds,
534 	    zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs);
535 	error = error ? error : dsl_prop_register(ds,
536 	    zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb,
537 	    zfsvfs);
538 	error = error ? error : dsl_prop_register(ds,
539 	    zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zfsvfs);
540 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
541 	if (error)
542 		goto unregister;
543 
544 	/*
545 	 * Invoke our callbacks to restore temporary mount options.
546 	 */
547 	if (do_readonly)
548 		readonly_changed_cb(zfsvfs, readonly);
549 	if (do_setuid)
550 		setuid_changed_cb(zfsvfs, setuid);
551 	if (do_exec)
552 		exec_changed_cb(zfsvfs, exec);
553 	if (do_devices)
554 		devices_changed_cb(zfsvfs, devices);
555 	if (do_xattr)
556 		xattr_changed_cb(zfsvfs, xattr);
557 	if (do_atime)
558 		atime_changed_cb(zfsvfs, atime);
559 
560 	nbmand_changed_cb(zfsvfs, nbmand);
561 
562 	return (0);
563 
564 unregister:
565 	dsl_prop_unregister_all(ds, zfsvfs);
566 	return (error);
567 }
568 
569 static int
570 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
571     uint64_t *userp, uint64_t *groupp, uint64_t *projectp)
572 {
573 	sa_hdr_phys_t sa;
574 	sa_hdr_phys_t *sap = data;
575 	uint64_t flags;
576 	int hdrsize;
577 	boolean_t swap = B_FALSE;
578 
579 	/*
580 	 * Is it a valid type of object to track?
581 	 */
582 	if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
583 		return (SET_ERROR(ENOENT));
584 
585 	/*
586 	 * If we have a NULL data pointer
587 	 * then assume the id's aren't changing and
588 	 * return EEXIST to the dmu to let it know to
589 	 * use the same ids
590 	 */
591 	if (data == NULL)
592 		return (SET_ERROR(EEXIST));
593 
594 	if (bonustype == DMU_OT_ZNODE) {
595 		znode_phys_t *znp = data;
596 		*userp = znp->zp_uid;
597 		*groupp = znp->zp_gid;
598 		*projectp = ZFS_DEFAULT_PROJID;
599 		return (0);
600 	}
601 
602 	if (sap->sa_magic == 0) {
603 		/*
604 		 * This should only happen for newly created files
605 		 * that haven't had the znode data filled in yet.
606 		 */
607 		*userp = 0;
608 		*groupp = 0;
609 		*projectp = ZFS_DEFAULT_PROJID;
610 		return (0);
611 	}
612 
613 	sa = *sap;
614 	if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
615 		sa.sa_magic = SA_MAGIC;
616 		sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
617 		swap = B_TRUE;
618 	} else {
619 		VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
620 	}
621 
622 	hdrsize = sa_hdrsize(&sa);
623 	VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
624 
625 	*userp = *((uint64_t *)((uintptr_t)data + hdrsize + SA_UID_OFFSET));
626 	*groupp = *((uint64_t *)((uintptr_t)data + hdrsize + SA_GID_OFFSET));
627 	flags = *((uint64_t *)((uintptr_t)data + hdrsize + SA_FLAGS_OFFSET));
628 	if (swap)
629 		flags = BSWAP_64(flags);
630 
631 	if (flags & ZFS_PROJID)
632 		*projectp = *((uint64_t *)((uintptr_t)data + hdrsize +
633 		    SA_PROJID_OFFSET));
634 	else
635 		*projectp = ZFS_DEFAULT_PROJID;
636 
637 	if (swap) {
638 		*userp = BSWAP_64(*userp);
639 		*groupp = BSWAP_64(*groupp);
640 		*projectp = BSWAP_64(*projectp);
641 	}
642 	return (0);
643 }
644 
645 static void
646 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
647     char *domainbuf, int buflen, uid_t *ridp)
648 {
649 	uint64_t fuid;
650 	const char *domain;
651 
652 	fuid = zfs_strtonum(fuidstr, NULL);
653 
654 	domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
655 	if (domain)
656 		(void) strlcpy(domainbuf, domain, buflen);
657 	else
658 		domainbuf[0] = '\0';
659 	*ridp = FUID_RID(fuid);
660 }
661 
662 static uint64_t
663 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
664 {
665 	switch (type) {
666 	case ZFS_PROP_USERUSED:
667 	case ZFS_PROP_USEROBJUSED:
668 		return (DMU_USERUSED_OBJECT);
669 	case ZFS_PROP_GROUPUSED:
670 	case ZFS_PROP_GROUPOBJUSED:
671 		return (DMU_GROUPUSED_OBJECT);
672 	case ZFS_PROP_PROJECTUSED:
673 	case ZFS_PROP_PROJECTOBJUSED:
674 		return (DMU_PROJECTUSED_OBJECT);
675 	case ZFS_PROP_USERQUOTA:
676 		return (zfsvfs->z_userquota_obj);
677 	case ZFS_PROP_GROUPQUOTA:
678 		return (zfsvfs->z_groupquota_obj);
679 	case ZFS_PROP_USEROBJQUOTA:
680 		return (zfsvfs->z_userobjquota_obj);
681 	case ZFS_PROP_GROUPOBJQUOTA:
682 		return (zfsvfs->z_groupobjquota_obj);
683 	case ZFS_PROP_PROJECTQUOTA:
684 		return (zfsvfs->z_projectquota_obj);
685 	case ZFS_PROP_PROJECTOBJQUOTA:
686 		return (zfsvfs->z_projectobjquota_obj);
687 	default:
688 		return (ZFS_NO_OBJECT);
689 	}
690 }
691 
692 int
693 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
694     uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
695 {
696 	int error;
697 	zap_cursor_t zc;
698 	zap_attribute_t za;
699 	zfs_useracct_t *buf = vbuf;
700 	uint64_t obj;
701 	int offset = 0;
702 
703 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
704 		return (SET_ERROR(ENOTSUP));
705 
706 	if ((type == ZFS_PROP_PROJECTQUOTA || type == ZFS_PROP_PROJECTUSED ||
707 	    type == ZFS_PROP_PROJECTOBJQUOTA ||
708 	    type == ZFS_PROP_PROJECTOBJUSED) &&
709 	    !dmu_objset_projectquota_present(zfsvfs->z_os))
710 		return (SET_ERROR(ENOTSUP));
711 
712 	if ((type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
713 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
714 	    type == ZFS_PROP_PROJECTOBJUSED ||
715 	    type == ZFS_PROP_PROJECTOBJQUOTA) &&
716 	    !dmu_objset_userobjspace_present(zfsvfs->z_os))
717 		return (SET_ERROR(ENOTSUP));
718 
719 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
720 	if (obj == ZFS_NO_OBJECT) {
721 		*bufsizep = 0;
722 		return (0);
723 	}
724 
725 	if (type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
726 	    type == ZFS_PROP_PROJECTOBJUSED)
727 		offset = DMU_OBJACCT_PREFIX_LEN;
728 
729 	for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
730 	    (error = zap_cursor_retrieve(&zc, &za)) == 0;
731 	    zap_cursor_advance(&zc)) {
732 		if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
733 		    *bufsizep)
734 			break;
735 
736 		/*
737 		 * skip object quota (with zap name prefix DMU_OBJACCT_PREFIX)
738 		 * when dealing with block quota and vice versa.
739 		 */
740 		if ((offset > 0) != (strncmp(za.za_name, DMU_OBJACCT_PREFIX,
741 		    DMU_OBJACCT_PREFIX_LEN) == 0))
742 			continue;
743 
744 		fuidstr_to_sid(zfsvfs, za.za_name + offset,
745 		    buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
746 
747 		buf->zu_space = za.za_first_integer;
748 		buf++;
749 	}
750 	if (error == ENOENT)
751 		error = 0;
752 
753 	ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
754 	*bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
755 	*cookiep = zap_cursor_serialize(&zc);
756 	zap_cursor_fini(&zc);
757 	return (error);
758 }
759 
760 /*
761  * buf must be big enough (eg, 32 bytes)
762  */
763 static int
764 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
765     char *buf, boolean_t addok)
766 {
767 	uint64_t fuid;
768 	int domainid = 0;
769 
770 	if (domain && domain[0]) {
771 		domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
772 		if (domainid == -1)
773 			return (SET_ERROR(ENOENT));
774 	}
775 	fuid = FUID_ENCODE(domainid, rid);
776 	(void) sprintf(buf, "%llx", (longlong_t)fuid);
777 	return (0);
778 }
779 
780 int
781 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
782     const char *domain, uint64_t rid, uint64_t *valp)
783 {
784 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
785 	int offset = 0;
786 	int err;
787 	uint64_t obj;
788 
789 	*valp = 0;
790 
791 	if (!dmu_objset_userspace_present(zfsvfs->z_os))
792 		return (SET_ERROR(ENOTSUP));
793 
794 	if ((type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
795 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
796 	    type == ZFS_PROP_PROJECTOBJUSED ||
797 	    type == ZFS_PROP_PROJECTOBJQUOTA) &&
798 	    !dmu_objset_userobjspace_present(zfsvfs->z_os))
799 		return (SET_ERROR(ENOTSUP));
800 
801 	if (type == ZFS_PROP_PROJECTQUOTA || type == ZFS_PROP_PROJECTUSED ||
802 	    type == ZFS_PROP_PROJECTOBJQUOTA ||
803 	    type == ZFS_PROP_PROJECTOBJUSED) {
804 		if (!dmu_objset_projectquota_present(zfsvfs->z_os))
805 			return (SET_ERROR(ENOTSUP));
806 		if (!zpl_is_valid_projid(rid))
807 			return (SET_ERROR(EINVAL));
808 	}
809 
810 	obj = zfs_userquota_prop_to_obj(zfsvfs, type);
811 	if (obj == ZFS_NO_OBJECT)
812 		return (0);
813 
814 	if (type == ZFS_PROP_USEROBJUSED || type == ZFS_PROP_GROUPOBJUSED ||
815 	    type == ZFS_PROP_PROJECTOBJUSED) {
816 		strncpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN);
817 		offset = DMU_OBJACCT_PREFIX_LEN;
818 	}
819 
820 	err = id_to_fuidstr(zfsvfs, domain, rid, buf + offset, B_FALSE);
821 	if (err)
822 		return (err);
823 
824 	err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
825 	if (err == ENOENT)
826 		err = 0;
827 	return (err);
828 }
829 
830 int
831 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
832     const char *domain, uint64_t rid, uint64_t quota)
833 {
834 	char buf[32];
835 	int err;
836 	dmu_tx_t *tx;
837 	uint64_t *objp;
838 	boolean_t fuid_dirtied;
839 
840 	if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
841 		return (SET_ERROR(ENOTSUP));
842 
843 	switch (type) {
844 	case ZFS_PROP_USERQUOTA:
845 		objp = &zfsvfs->z_userquota_obj;
846 		break;
847 	case ZFS_PROP_GROUPQUOTA:
848 		objp = &zfsvfs->z_groupquota_obj;
849 		break;
850 	case ZFS_PROP_USEROBJQUOTA:
851 		objp = &zfsvfs->z_userobjquota_obj;
852 		break;
853 	case ZFS_PROP_GROUPOBJQUOTA:
854 		objp = &zfsvfs->z_groupobjquota_obj;
855 		break;
856 	case ZFS_PROP_PROJECTQUOTA:
857 		if (!dmu_objset_projectquota_enabled(zfsvfs->z_os))
858 			return (SET_ERROR(ENOTSUP));
859 		if (!zpl_is_valid_projid(rid))
860 			return (SET_ERROR(EINVAL));
861 
862 		objp = &zfsvfs->z_projectquota_obj;
863 		break;
864 	case ZFS_PROP_PROJECTOBJQUOTA:
865 		if (!dmu_objset_projectquota_enabled(zfsvfs->z_os))
866 			return (SET_ERROR(ENOTSUP));
867 		if (!zpl_is_valid_projid(rid))
868 			return (SET_ERROR(EINVAL));
869 
870 		objp = &zfsvfs->z_projectobjquota_obj;
871 		break;
872 	default:
873 		return (SET_ERROR(EINVAL));
874 	}
875 
876 	err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
877 	if (err)
878 		return (err);
879 	fuid_dirtied = zfsvfs->z_fuid_dirty;
880 
881 	tx = dmu_tx_create(zfsvfs->z_os);
882 	dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
883 	if (*objp == 0) {
884 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
885 		    zfs_userquota_prop_prefixes[type]);
886 	}
887 	if (fuid_dirtied)
888 		zfs_fuid_txhold(zfsvfs, tx);
889 	err = dmu_tx_assign(tx, TXG_WAIT);
890 	if (err) {
891 		dmu_tx_abort(tx);
892 		return (err);
893 	}
894 
895 	mutex_enter(&zfsvfs->z_lock);
896 	if (*objp == 0) {
897 		*objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
898 		    DMU_OT_NONE, 0, tx);
899 		VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
900 		    zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
901 	}
902 	mutex_exit(&zfsvfs->z_lock);
903 
904 	if (quota == 0) {
905 		err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
906 		if (err == ENOENT)
907 			err = 0;
908 	} else {
909 		err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
910 	}
911 	ASSERT(err == 0);
912 	if (fuid_dirtied)
913 		zfs_fuid_sync(zfsvfs, tx);
914 	dmu_tx_commit(tx);
915 	return (err);
916 }
917 
918 boolean_t
919 zfs_id_overobjquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
920 {
921 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
922 	uint64_t used, quota, quotaobj;
923 	int err;
924 
925 	if (!dmu_objset_userobjspace_present(zfsvfs->z_os)) {
926 		if (dmu_objset_userobjspace_upgradable(zfsvfs->z_os))
927 			dmu_objset_id_quota_upgrade(zfsvfs->z_os);
928 		return (B_FALSE);
929 	}
930 
931 	if (usedobj == DMU_PROJECTUSED_OBJECT) {
932 		if (!dmu_objset_projectquota_present(zfsvfs->z_os)) {
933 			if (dmu_objset_projectquota_upgradable(zfsvfs->z_os)) {
934 				dsl_pool_config_enter(
935 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
936 				dmu_objset_id_quota_upgrade(zfsvfs->z_os);
937 				dsl_pool_config_exit(
938 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
939 			}
940 			return (B_FALSE);
941 		}
942 		quotaobj = zfsvfs->z_projectobjquota_obj;
943 	} else if (usedobj == DMU_USERUSED_OBJECT) {
944 		quotaobj = zfsvfs->z_userobjquota_obj;
945 	} else if (usedobj == DMU_GROUPUSED_OBJECT) {
946 		quotaobj = zfsvfs->z_groupobjquota_obj;
947 	} else {
948 		return (B_FALSE);
949 	}
950 	if (quotaobj == 0 || zfsvfs->z_replay)
951 		return (B_FALSE);
952 
953 	(void) sprintf(buf, "%llx", (longlong_t)id);
954 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
955 	if (err != 0)
956 		return (B_FALSE);
957 
958 	(void) sprintf(buf, DMU_OBJACCT_PREFIX "%llx", (longlong_t)id);
959 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
960 	if (err != 0)
961 		return (B_FALSE);
962 	return (used >= quota);
963 }
964 
965 boolean_t
966 zfs_id_overblockquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
967 {
968 	char buf[20];
969 	uint64_t used, quota, quotaobj;
970 	int err;
971 
972 	if (usedobj == DMU_PROJECTUSED_OBJECT) {
973 		if (!dmu_objset_projectquota_present(zfsvfs->z_os)) {
974 			if (dmu_objset_projectquota_upgradable(zfsvfs->z_os)) {
975 				dsl_pool_config_enter(
976 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
977 				dmu_objset_id_quota_upgrade(zfsvfs->z_os);
978 				dsl_pool_config_exit(
979 				    dmu_objset_pool(zfsvfs->z_os), FTAG);
980 			}
981 			return (B_FALSE);
982 		}
983 		quotaobj = zfsvfs->z_projectquota_obj;
984 	} else if (usedobj == DMU_USERUSED_OBJECT) {
985 		quotaobj = zfsvfs->z_userquota_obj;
986 	} else if (usedobj == DMU_GROUPUSED_OBJECT) {
987 		quotaobj = zfsvfs->z_groupquota_obj;
988 	} else {
989 		return (B_FALSE);
990 	}
991 	if (quotaobj == 0 || zfsvfs->z_replay)
992 		return (B_FALSE);
993 
994 	(void) sprintf(buf, "%llx", (longlong_t)id);
995 	err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
996 	if (err != 0)
997 		return (B_FALSE);
998 
999 	err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
1000 	if (err != 0)
1001 		return (B_FALSE);
1002 	return (used >= quota);
1003 }
1004 
1005 boolean_t
1006 zfs_id_overquota(zfsvfs_t *zfsvfs, uint64_t usedobj, uint64_t id)
1007 {
1008 	return (zfs_id_overblockquota(zfsvfs, usedobj, id) ||
1009 	    zfs_id_overobjquota(zfsvfs, usedobj, id));
1010 }
1011 
1012 /*
1013  * Associate this zfsvfs with the given objset, which must be owned.
1014  * This will cache a bunch of on-disk state from the objset in the
1015  * zfsvfs.
1016  */
1017 static int
1018 zfsvfs_init(zfsvfs_t *zfsvfs, objset_t *os)
1019 {
1020 	int error;
1021 	uint64_t val;
1022 
1023 	zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
1024 	zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
1025 	zfsvfs->z_os = os;
1026 
1027 	error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
1028 	if (error != 0)
1029 		return (error);
1030 	if (zfsvfs->z_version >
1031 	    zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
1032 		(void) printf("Can't mount a version %lld file system "
1033 		    "on a version %lld pool\n. Pool must be upgraded to mount "
1034 		    "this file system.", (u_longlong_t)zfsvfs->z_version,
1035 		    (u_longlong_t)spa_version(dmu_objset_spa(os)));
1036 		return (SET_ERROR(ENOTSUP));
1037 	}
1038 	error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &val);
1039 	if (error != 0)
1040 		return (error);
1041 	zfsvfs->z_norm = (int)val;
1042 
1043 	error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &val);
1044 	if (error != 0)
1045 		return (error);
1046 	zfsvfs->z_utf8 = (val != 0);
1047 
1048 	error = zfs_get_zplprop(os, ZFS_PROP_CASE, &val);
1049 	if (error != 0)
1050 		return (error);
1051 	zfsvfs->z_case = (uint_t)val;
1052 
1053 	/*
1054 	 * Fold case on file systems that are always or sometimes case
1055 	 * insensitive.
1056 	 */
1057 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
1058 	    zfsvfs->z_case == ZFS_CASE_MIXED)
1059 		zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1060 
1061 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1062 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1063 
1064 	uint64_t sa_obj = 0;
1065 	if (zfsvfs->z_use_sa) {
1066 		/* should either have both of these objects or none */
1067 		error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
1068 		    &sa_obj);
1069 		if (error != 0)
1070 			return (error);
1071 	}
1072 
1073 	error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1074 	    &zfsvfs->z_attr_table);
1075 	if (error != 0)
1076 		return (error);
1077 
1078 	if (zfsvfs->z_version >= ZPL_VERSION_SA)
1079 		sa_register_update_callback(os, zfs_sa_upgrade);
1080 
1081 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
1082 	    &zfsvfs->z_root);
1083 	if (error != 0)
1084 		return (error);
1085 	ASSERT(zfsvfs->z_root != 0);
1086 
1087 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
1088 	    &zfsvfs->z_unlinkedobj);
1089 	if (error != 0)
1090 		return (error);
1091 
1092 	error = zap_lookup(os, MASTER_NODE_OBJ,
1093 	    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
1094 	    8, 1, &zfsvfs->z_userquota_obj);
1095 	if (error == ENOENT)
1096 		zfsvfs->z_userquota_obj = 0;
1097 	else if (error != 0)
1098 		return (error);
1099 
1100 	error = zap_lookup(os, MASTER_NODE_OBJ,
1101 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
1102 	    8, 1, &zfsvfs->z_groupquota_obj);
1103 	if (error == ENOENT)
1104 		zfsvfs->z_groupquota_obj = 0;
1105 	else if (error != 0)
1106 		return (error);
1107 
1108 	error = zap_lookup(os, MASTER_NODE_OBJ,
1109 	    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA],
1110 	    8, 1, &zfsvfs->z_projectquota_obj);
1111 	if (error == ENOENT)
1112 		zfsvfs->z_projectquota_obj = 0;
1113 	else if (error != 0)
1114 		return (error);
1115 
1116 	error = zap_lookup(os, MASTER_NODE_OBJ,
1117 	    zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA],
1118 	    8, 1, &zfsvfs->z_userobjquota_obj);
1119 	if (error == ENOENT)
1120 		zfsvfs->z_userobjquota_obj = 0;
1121 	else if (error != 0)
1122 		return (error);
1123 
1124 	error = zap_lookup(os, MASTER_NODE_OBJ,
1125 	    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA],
1126 	    8, 1, &zfsvfs->z_groupobjquota_obj);
1127 	if (error == ENOENT)
1128 		zfsvfs->z_groupobjquota_obj = 0;
1129 	else if (error != 0)
1130 		return (error);
1131 
1132 	error = zap_lookup(os, MASTER_NODE_OBJ,
1133 	    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTOBJQUOTA],
1134 	    8, 1, &zfsvfs->z_projectobjquota_obj);
1135 	if (error == ENOENT)
1136 		zfsvfs->z_projectobjquota_obj = 0;
1137 	else if (error != 0)
1138 		return (error);
1139 
1140 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
1141 	    &zfsvfs->z_fuid_obj);
1142 	if (error == ENOENT)
1143 		zfsvfs->z_fuid_obj = 0;
1144 	else if (error != 0)
1145 		return (error);
1146 
1147 	error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
1148 	    &zfsvfs->z_shares_dir);
1149 	if (error == ENOENT)
1150 		zfsvfs->z_shares_dir = 0;
1151 	else if (error != 0)
1152 		return (error);
1153 
1154 	return (0);
1155 }
1156 
1157 int
1158 zfsvfs_create(const char *osname, boolean_t readonly, zfsvfs_t **zfvp)
1159 {
1160 	objset_t *os;
1161 	zfsvfs_t *zfsvfs;
1162 	int error;
1163 	boolean_t ro = (readonly || (strchr(osname, '@') != NULL));
1164 
1165 	zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1166 
1167 	error = dmu_objset_own(osname, DMU_OST_ZFS, ro, B_TRUE, zfsvfs, &os);
1168 	if (error != 0) {
1169 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
1170 		return (error);
1171 	}
1172 
1173 	error = zfsvfs_create_impl(zfvp, zfsvfs, os);
1174 	if (error != 0) {
1175 		dmu_objset_disown(os, B_TRUE, zfsvfs);
1176 	}
1177 	return (error);
1178 }
1179 
1180 
1181 int
1182 zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os)
1183 {
1184 	int error;
1185 
1186 	zfsvfs->z_vfs = NULL;
1187 	zfsvfs->z_parent = zfsvfs;
1188 
1189 	mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1190 	mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
1191 	list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1192 	    offsetof(znode_t, z_link_node));
1193 	rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
1194 	rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
1195 	rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
1196 	for (int i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1197 		mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1198 
1199 	error = zfsvfs_init(zfsvfs, os);
1200 	if (error != 0) {
1201 		*zfvp = NULL;
1202 		kmem_free(zfsvfs, sizeof (zfsvfs_t));
1203 		return (error);
1204 	}
1205 
1206 	zfsvfs->z_drain_task = TASKQID_INVALID;
1207 	zfsvfs->z_draining = B_FALSE;
1208 	zfsvfs->z_drain_cancel = B_TRUE;
1209 
1210 	*zfvp = zfsvfs;
1211 	return (0);
1212 }
1213 
1214 static int
1215 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1216 {
1217 	int error;
1218 
1219 	error = zfs_register_callbacks(zfsvfs->z_vfs);
1220 	if (error)
1221 		return (error);
1222 
1223 	zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
1224 
1225 	/*
1226 	 * If we are not mounting (ie: online recv), then we don't
1227 	 * have to worry about replaying the log as we blocked all
1228 	 * operations out since we closed the ZIL.
1229 	 */
1230 	if (mounting) {
1231 		boolean_t readonly;
1232 
1233 		/*
1234 		 * During replay we remove the read only flag to
1235 		 * allow replays to succeed.
1236 		 */
1237 		readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
1238 		if (readonly != 0) {
1239 			zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1240 		} else {
1241 			zfs_unlinked_drain(zfsvfs);
1242 		}
1243 
1244 		/*
1245 		 * Parse and replay the intent log.
1246 		 *
1247 		 * Because of ziltest, this must be done after
1248 		 * zfs_unlinked_drain().  (Further note: ziltest
1249 		 * doesn't use readonly mounts, where
1250 		 * zfs_unlinked_drain() isn't called.)  This is because
1251 		 * ziltest causes spa_sync() to think it's committed,
1252 		 * but actually it is not, so the intent log contains
1253 		 * many txg's worth of changes.
1254 		 *
1255 		 * In particular, if object N is in the unlinked set in
1256 		 * the last txg to actually sync, then it could be
1257 		 * actually freed in a later txg and then reallocated
1258 		 * in a yet later txg.  This would write a "create
1259 		 * object N" record to the intent log.  Normally, this
1260 		 * would be fine because the spa_sync() would have
1261 		 * written out the fact that object N is free, before
1262 		 * we could write the "create object N" intent log
1263 		 * record.
1264 		 *
1265 		 * But when we are in ziltest mode, we advance the "open
1266 		 * txg" without actually spa_sync()-ing the changes to
1267 		 * disk.  So we would see that object N is still
1268 		 * allocated and in the unlinked set, and there is an
1269 		 * intent log record saying to allocate it.
1270 		 */
1271 		if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1272 			if (zil_replay_disable) {
1273 				zil_destroy(zfsvfs->z_log, B_FALSE);
1274 			} else {
1275 				zfsvfs->z_replay = B_TRUE;
1276 				zil_replay(zfsvfs->z_os, zfsvfs,
1277 				    zfs_replay_vector);
1278 				zfsvfs->z_replay = B_FALSE;
1279 			}
1280 		}
1281 
1282 		/* restore readonly bit */
1283 		if (readonly != 0)
1284 			zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
1285 	}
1286 
1287 	/*
1288 	 * Set the objset user_ptr to track its zfsvfs.
1289 	 */
1290 	mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1291 	dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1292 	mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1293 
1294 	return (0);
1295 }
1296 
1297 void
1298 zfsvfs_free(zfsvfs_t *zfsvfs)
1299 {
1300 	int i;
1301 	extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1302 
1303 	/*
1304 	 * This is a barrier to prevent the filesystem from going away in
1305 	 * zfs_znode_move() until we can safely ensure that the filesystem is
1306 	 * not unmounted. We consider the filesystem valid before the barrier
1307 	 * and invalid after the barrier.
1308 	 */
1309 	rw_enter(&zfsvfs_lock, RW_READER);
1310 	rw_exit(&zfsvfs_lock);
1311 
1312 	zfs_fuid_destroy(zfsvfs);
1313 
1314 	mutex_destroy(&zfsvfs->z_znodes_lock);
1315 	mutex_destroy(&zfsvfs->z_lock);
1316 	list_destroy(&zfsvfs->z_all_znodes);
1317 	rrm_destroy(&zfsvfs->z_teardown_lock);
1318 	rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1319 	rw_destroy(&zfsvfs->z_fuid_lock);
1320 	for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1321 		mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1322 	kmem_free(zfsvfs, sizeof (zfsvfs_t));
1323 }
1324 
1325 static void
1326 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1327 {
1328 	zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1329 	if (zfsvfs->z_vfs) {
1330 		if (zfsvfs->z_use_fuids) {
1331 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1332 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1333 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1334 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1335 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1336 			vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1337 		} else {
1338 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1339 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1340 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1341 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1342 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1343 			vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1344 		}
1345 	}
1346 	zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1347 }
1348 
1349 static int
1350 zfs_domount(vfs_t *vfsp, char *osname)
1351 {
1352 	dev_t mount_dev;
1353 	uint64_t recordsize, fsid_guid;
1354 	int error = 0;
1355 	zfsvfs_t *zfsvfs;
1356 	boolean_t readonly = vfsp->vfs_flag & VFS_RDONLY ? B_TRUE : B_FALSE;
1357 
1358 	ASSERT(vfsp);
1359 	ASSERT(osname);
1360 
1361 	error = zfsvfs_create(osname, readonly, &zfsvfs);
1362 	if (error)
1363 		return (error);
1364 	zfsvfs->z_vfs = vfsp;
1365 
1366 	/* Initialize the generic filesystem structure. */
1367 	vfsp->vfs_bcount = 0;
1368 	vfsp->vfs_data = NULL;
1369 
1370 	if (zfs_create_unique_device(&mount_dev) == -1) {
1371 		error = SET_ERROR(ENODEV);
1372 		goto out;
1373 	}
1374 	ASSERT(vfs_devismounted(mount_dev) == 0);
1375 
1376 	if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1377 	    NULL))
1378 		goto out;
1379 
1380 	vfsp->vfs_dev = mount_dev;
1381 	vfsp->vfs_fstype = zfsfstype;
1382 	vfsp->vfs_bsize = recordsize;
1383 	vfsp->vfs_flag |= VFS_NOTRUNC;
1384 	vfsp->vfs_data = zfsvfs;
1385 
1386 	/*
1387 	 * The fsid is 64 bits, composed of an 8-bit fs type, which
1388 	 * separates our fsid from any other filesystem types, and a
1389 	 * 56-bit objset unique ID.  The objset unique ID is unique to
1390 	 * all objsets open on this system, provided by unique_create().
1391 	 * The 8-bit fs type must be put in the low bits of fsid[1]
1392 	 * because that's where other Solaris filesystems put it.
1393 	 */
1394 	fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1395 	ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1396 	vfsp->vfs_fsid.val[0] = fsid_guid;
1397 	vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1398 	    zfsfstype & 0xFF;
1399 
1400 	/*
1401 	 * Set features for file system.
1402 	 */
1403 	zfs_set_fuid_feature(zfsvfs);
1404 	if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1405 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1406 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1407 		vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1408 	} else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1409 		vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1410 		vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1411 	}
1412 	vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1413 
1414 	if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1415 		uint64_t pval;
1416 
1417 		atime_changed_cb(zfsvfs, B_FALSE);
1418 		readonly_changed_cb(zfsvfs, B_TRUE);
1419 		if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
1420 			goto out;
1421 		xattr_changed_cb(zfsvfs, pval);
1422 		zfsvfs->z_issnap = B_TRUE;
1423 		zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1424 
1425 		mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1426 		dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1427 		mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1428 	} else {
1429 		error = zfsvfs_setup(zfsvfs, B_TRUE);
1430 	}
1431 
1432 	if (!zfsvfs->z_issnap)
1433 		zfsctl_create(zfsvfs);
1434 out:
1435 	if (error) {
1436 		dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1437 		zfsvfs_free(zfsvfs);
1438 	} else {
1439 		atomic_inc_32(&zfs_active_fs_count);
1440 	}
1441 
1442 	return (error);
1443 }
1444 
1445 void
1446 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1447 {
1448 	objset_t *os = zfsvfs->z_os;
1449 
1450 	if (!dmu_objset_is_snapshot(os))
1451 		dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1452 }
1453 
1454 /*
1455  * Convert a decimal digit string to a uint64_t integer.
1456  */
1457 static int
1458 str_to_uint64(char *str, uint64_t *objnum)
1459 {
1460 	uint64_t num = 0;
1461 
1462 	while (*str) {
1463 		if (*str < '0' || *str > '9')
1464 			return (SET_ERROR(EINVAL));
1465 
1466 		num = num*10 + *str++ - '0';
1467 	}
1468 
1469 	*objnum = num;
1470 	return (0);
1471 }
1472 
1473 /*
1474  * The boot path passed from the boot loader is in the form of
1475  * "rootpool-name/root-filesystem-object-number'. Convert this
1476  * string to a dataset name: "rootpool-name/root-filesystem-name".
1477  */
1478 static int
1479 zfs_parse_bootfs(char *bpath, char *outpath)
1480 {
1481 	char *slashp;
1482 	uint64_t objnum;
1483 	int error;
1484 
1485 	if (*bpath == 0 || *bpath == '/')
1486 		return (SET_ERROR(EINVAL));
1487 
1488 	(void) strcpy(outpath, bpath);
1489 
1490 	slashp = strchr(bpath, '/');
1491 
1492 	/* if no '/', just return the pool name */
1493 	if (slashp == NULL) {
1494 		return (0);
1495 	}
1496 
1497 	/* if not a number, just return the root dataset name */
1498 	if (str_to_uint64(slashp+1, &objnum)) {
1499 		return (0);
1500 	}
1501 
1502 	*slashp = '\0';
1503 	error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1504 	*slashp = '/';
1505 
1506 	return (error);
1507 }
1508 
1509 /*
1510  * Check that the hex label string is appropriate for the dataset being
1511  * mounted into the global_zone proper.
1512  *
1513  * Return an error if the hex label string is not default or
1514  * admin_low/admin_high.  For admin_low labels, the corresponding
1515  * dataset must be readonly.
1516  */
1517 int
1518 zfs_check_global_label(const char *dsname, const char *hexsl)
1519 {
1520 	if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1521 		return (0);
1522 	if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1523 		return (0);
1524 	if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1525 		/* must be readonly */
1526 		uint64_t rdonly;
1527 
1528 		if (dsl_prop_get_integer(dsname,
1529 		    zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1530 			return (SET_ERROR(EACCES));
1531 		return (rdonly ? 0 : EACCES);
1532 	}
1533 	return (SET_ERROR(EACCES));
1534 }
1535 
1536 static int
1537 zfs_statfs_project(zfsvfs_t *zfsvfs, znode_t *zp, struct statvfs64 *statp,
1538     uint32_t bshift)
1539 {
1540 	char buf[20 + DMU_OBJACCT_PREFIX_LEN];
1541 	uint64_t offset = DMU_OBJACCT_PREFIX_LEN;
1542 	uint64_t quota;
1543 	uint64_t used;
1544 	int err;
1545 
1546 	strlcpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN + 1);
1547 	err = id_to_fuidstr(zfsvfs, NULL, zp->z_projid, buf + offset, B_FALSE);
1548 	if (err)
1549 		return (err);
1550 
1551 	if (zfsvfs->z_projectquota_obj == 0)
1552 		goto objs;
1553 
1554 	err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectquota_obj,
1555 	    buf + offset, 8, 1, &quota);
1556 	if (err == ENOENT)
1557 		goto objs;
1558 	else if (err)
1559 		return (err);
1560 
1561 	err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1562 	    buf + offset, 8, 1, &used);
1563 	if (unlikely(err == ENOENT)) {
1564 		uint32_t blksize;
1565 		u_longlong_t nblocks;
1566 
1567 		/*
1568 		 * Quota accounting is async, so it is possible race case.
1569 		 * There is at least one object with the given project ID.
1570 		 */
1571 		sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
1572 		if (unlikely(zp->z_blksz == 0))
1573 			blksize = zfsvfs->z_max_blksz;
1574 
1575 		used = blksize * nblocks;
1576 	} else if (err) {
1577 		return (err);
1578 	}
1579 
1580 	statp->f_blocks = quota >> bshift;
1581 	statp->f_bfree = (quota > used) ? ((quota - used) >> bshift) : 0;
1582 	statp->f_bavail = statp->f_bfree;
1583 
1584 objs:
1585 	if (zfsvfs->z_projectobjquota_obj == 0)
1586 		return (0);
1587 
1588 	err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectobjquota_obj,
1589 	    buf + offset, 8, 1, &quota);
1590 	if (err == ENOENT)
1591 		return (0);
1592 	else if (err)
1593 		return (err);
1594 
1595 	err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT,
1596 	    buf, 8, 1, &used);
1597 	if (unlikely(err == ENOENT)) {
1598 		/*
1599 		 * Quota accounting is async, so it is possible race case.
1600 		 * There is at least one object with the given project ID.
1601 		 */
1602 		used = 1;
1603 	} else if (err) {
1604 		return (err);
1605 	}
1606 
1607 	statp->f_files = quota;
1608 	statp->f_ffree = (quota > used) ? (quota - used) : 0;
1609 
1610 	return (0);
1611 }
1612 
1613 /*
1614  * Determine whether the mount is allowed according to MAC check.
1615  * by comparing (where appropriate) label of the dataset against
1616  * the label of the zone being mounted into.  If the dataset has
1617  * no label, create one.
1618  *
1619  * Returns 0 if access allowed, error otherwise (e.g. EACCES)
1620  */
1621 static int
1622 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1623 {
1624 	int		error, retv;
1625 	zone_t		*mntzone = NULL;
1626 	ts_label_t	*mnt_tsl;
1627 	bslabel_t	*mnt_sl;
1628 	bslabel_t	ds_sl;
1629 	char		ds_hexsl[MAXNAMELEN];
1630 
1631 	retv = EACCES;				/* assume the worst */
1632 
1633 	/*
1634 	 * Start by getting the dataset label if it exists.
1635 	 */
1636 	error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1637 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1638 	if (error)
1639 		return (SET_ERROR(EACCES));
1640 
1641 	/*
1642 	 * If labeling is NOT enabled, then disallow the mount of datasets
1643 	 * which have a non-default label already.  No other label checks
1644 	 * are needed.
1645 	 */
1646 	if (!is_system_labeled()) {
1647 		if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1648 			return (0);
1649 		return (SET_ERROR(EACCES));
1650 	}
1651 
1652 	/*
1653 	 * Get the label of the mountpoint.  If mounting into the global
1654 	 * zone (i.e. mountpoint is not within an active zone and the
1655 	 * zoned property is off), the label must be default or
1656 	 * admin_low/admin_high only; no other checks are needed.
1657 	 */
1658 	mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1659 	if (mntzone->zone_id == GLOBAL_ZONEID) {
1660 		uint64_t zoned;
1661 
1662 		zone_rele(mntzone);
1663 
1664 		if (dsl_prop_get_integer(osname,
1665 		    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1666 			return (SET_ERROR(EACCES));
1667 		if (!zoned)
1668 			return (zfs_check_global_label(osname, ds_hexsl));
1669 		else
1670 			/*
1671 			 * This is the case of a zone dataset being mounted
1672 			 * initially, before the zone has been fully created;
1673 			 * allow this mount into global zone.
1674 			 */
1675 			return (0);
1676 	}
1677 
1678 	mnt_tsl = mntzone->zone_slabel;
1679 	ASSERT(mnt_tsl != NULL);
1680 	label_hold(mnt_tsl);
1681 	mnt_sl = label2bslabel(mnt_tsl);
1682 
1683 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1684 		/*
1685 		 * The dataset doesn't have a real label, so fabricate one.
1686 		 */
1687 		char *str = NULL;
1688 
1689 		if (l_to_str_internal(mnt_sl, &str) == 0 &&
1690 		    dsl_prop_set_string(osname,
1691 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1692 		    ZPROP_SRC_LOCAL, str) == 0)
1693 			retv = 0;
1694 		if (str != NULL)
1695 			kmem_free(str, strlen(str) + 1);
1696 	} else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1697 		/*
1698 		 * Now compare labels to complete the MAC check.  If the
1699 		 * labels are equal then allow access.  If the mountpoint
1700 		 * label dominates the dataset label, allow readonly access.
1701 		 * Otherwise, access is denied.
1702 		 */
1703 		if (blequal(mnt_sl, &ds_sl))
1704 			retv = 0;
1705 		else if (bldominates(mnt_sl, &ds_sl)) {
1706 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1707 			retv = 0;
1708 		}
1709 	}
1710 
1711 	label_rele(mnt_tsl);
1712 	zone_rele(mntzone);
1713 	return (retv);
1714 }
1715 
1716 /*
1717  * Load a string-valued boot property and attempt to convert it to a 64-bit
1718  * unsigned integer.  If the value is not present, or the conversion fails,
1719  * return the provided default value.
1720  */
1721 static uint64_t
1722 spa_get_bootprop_uint64(const char *name, uint64_t defval)
1723 {
1724 	char *propval;
1725 	u_longlong_t r;
1726 	int e;
1727 
1728 	if ((propval = spa_get_bootprop(name)) == NULL) {
1729 		/*
1730 		 * The property does not exist.
1731 		 */
1732 		return (defval);
1733 	}
1734 
1735 	e = ddi_strtoull(propval, NULL, 10, &r);
1736 
1737 	spa_free_bootprop(propval);
1738 
1739 	/*
1740 	 * If the conversion succeeded, return the value.  If there was any
1741 	 * kind of failure, just return the default value.
1742 	 */
1743 	return (e == 0 ? r : defval);
1744 }
1745 
1746 static int
1747 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1748 {
1749 	int error = 0;
1750 	static int zfsrootdone = 0;
1751 	zfsvfs_t *zfsvfs = NULL;
1752 	znode_t *zp = NULL;
1753 	vnode_t *vp = NULL;
1754 	char *zfs_bootfs;
1755 	char *zfs_devid;
1756 	uint64_t zfs_bootpool;
1757 	uint64_t zfs_bootvdev;
1758 
1759 	ASSERT(vfsp);
1760 
1761 	/*
1762 	 * The filesystem that we mount as root is defined in the
1763 	 * boot property "zfs-bootfs" with a format of
1764 	 * "poolname/root-dataset-objnum".
1765 	 */
1766 	if (why == ROOT_INIT) {
1767 		if (zfsrootdone++)
1768 			return (SET_ERROR(EBUSY));
1769 
1770 		/*
1771 		 * the process of doing a spa_load will require the
1772 		 * clock to be set before we could (for example) do
1773 		 * something better by looking at the timestamp on
1774 		 * an uberblock, so just set it to -1.
1775 		 */
1776 		clkset(-1);
1777 
1778 		if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1779 			cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1780 			    "bootfs name");
1781 			return (SET_ERROR(EINVAL));
1782 		}
1783 		zfs_devid = spa_get_bootprop("diskdevid");
1784 
1785 		/*
1786 		 * The boot loader may also provide us with the GUID for both
1787 		 * the pool and the nominated boot vdev.  A GUID value of 0 is
1788 		 * explicitly invalid (see "spa_change_guid()"), so we use this
1789 		 * as a sentinel value when no GUID is present.
1790 		 */
1791 		zfs_bootpool = spa_get_bootprop_uint64("zfs-bootpool", 0);
1792 		zfs_bootvdev = spa_get_bootprop_uint64("zfs-bootvdev", 0);
1793 
1794 		/*
1795 		 * Initialise the early boot device rescan mechanism.  A scan
1796 		 * will not actually be performed unless we need to do so in
1797 		 * order to find the correct /devices path for a relocated
1798 		 * device.
1799 		 */
1800 		vdev_disk_preroot_init();
1801 
1802 		error = spa_import_rootpool(rootfs.bo_name, zfs_devid,
1803 		    zfs_bootpool, zfs_bootvdev);
1804 
1805 		spa_free_bootprop(zfs_devid);
1806 
1807 		if (error != 0) {
1808 			spa_free_bootprop(zfs_bootfs);
1809 			vdev_disk_preroot_fini();
1810 			cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1811 			    error);
1812 			return (error);
1813 		}
1814 
1815 		if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1816 			spa_free_bootprop(zfs_bootfs);
1817 			vdev_disk_preroot_fini();
1818 			cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1819 			    error);
1820 			return (error);
1821 		}
1822 
1823 		spa_free_bootprop(zfs_bootfs);
1824 		vdev_disk_preroot_fini();
1825 
1826 		if (error = vfs_lock(vfsp))
1827 			return (error);
1828 
1829 		if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1830 			cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1831 			goto out;
1832 		}
1833 
1834 		zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1835 		ASSERT(zfsvfs);
1836 		if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1837 			cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1838 			goto out;
1839 		}
1840 
1841 		vp = ZTOV(zp);
1842 		mutex_enter(&vp->v_lock);
1843 		vp->v_flag |= VROOT;
1844 		mutex_exit(&vp->v_lock);
1845 		rootvp = vp;
1846 
1847 		/*
1848 		 * Leave rootvp held.  The root file system is never unmounted.
1849 		 */
1850 
1851 		vfs_add((struct vnode *)0, vfsp,
1852 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1853 out:
1854 		vfs_unlock(vfsp);
1855 		return (error);
1856 	} else if (why == ROOT_REMOUNT) {
1857 		readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1858 		vfsp->vfs_flag |= VFS_REMOUNT;
1859 
1860 		/* refresh mount options */
1861 		zfs_unregister_callbacks(vfsp->vfs_data);
1862 		return (zfs_register_callbacks(vfsp));
1863 
1864 	} else if (why == ROOT_UNMOUNT) {
1865 		zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1866 		(void) zfs_sync(vfsp, 0, 0);
1867 		return (0);
1868 	}
1869 
1870 	/*
1871 	 * if "why" is equal to anything else other than ROOT_INIT,
1872 	 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1873 	 */
1874 	return (SET_ERROR(ENOTSUP));
1875 }
1876 
1877 /*ARGSUSED*/
1878 static int
1879 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
1880 {
1881 	char		*osname;
1882 	pathname_t	spn;
1883 	int		error = 0;
1884 	uio_seg_t	fromspace = (uap->flags & MS_SYSSPACE) ?
1885 	    UIO_SYSSPACE : UIO_USERSPACE;
1886 	int		canwrite;
1887 
1888 	if (mvp->v_type != VDIR)
1889 		return (SET_ERROR(ENOTDIR));
1890 
1891 	mutex_enter(&mvp->v_lock);
1892 	if ((uap->flags & MS_REMOUNT) == 0 &&
1893 	    (uap->flags & MS_OVERLAY) == 0 &&
1894 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1895 		mutex_exit(&mvp->v_lock);
1896 		return (SET_ERROR(EBUSY));
1897 	}
1898 	mutex_exit(&mvp->v_lock);
1899 
1900 	/*
1901 	 * ZFS does not support passing unparsed data in via MS_DATA.
1902 	 * Users should use the MS_OPTIONSTR interface; this means
1903 	 * that all option parsing is already done and the options struct
1904 	 * can be interrogated.
1905 	 */
1906 	if ((uap->flags & MS_DATA) && uap->datalen > 0)
1907 		return (SET_ERROR(EINVAL));
1908 
1909 	/*
1910 	 * Get the objset name (the "special" mount argument).
1911 	 */
1912 	if (error = pn_get(uap->spec, fromspace, &spn))
1913 		return (error);
1914 
1915 	osname = spn.pn_path;
1916 
1917 	/*
1918 	 * Check for mount privilege?
1919 	 *
1920 	 * If we don't have privilege then see if
1921 	 * we have local permission to allow it
1922 	 */
1923 	error = secpolicy_fs_mount(cr, mvp, vfsp);
1924 	if (error) {
1925 		if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
1926 			vattr_t		vattr;
1927 
1928 			/*
1929 			 * Make sure user is the owner of the mount point
1930 			 * or has sufficient privileges.
1931 			 */
1932 
1933 			vattr.va_mask = AT_UID;
1934 
1935 			if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
1936 				goto out;
1937 			}
1938 
1939 			if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
1940 			    VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
1941 				goto out;
1942 			}
1943 			secpolicy_fs_mount_clearopts(cr, vfsp);
1944 		} else {
1945 			goto out;
1946 		}
1947 	}
1948 
1949 	/*
1950 	 * Refuse to mount a filesystem if we are in a local zone and the
1951 	 * dataset is not visible.
1952 	 */
1953 	if (!INGLOBALZONE(curproc) &&
1954 	    (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1955 		error = SET_ERROR(EPERM);
1956 		goto out;
1957 	}
1958 
1959 	error = zfs_mount_label_policy(vfsp, osname);
1960 	if (error)
1961 		goto out;
1962 
1963 	/*
1964 	 * When doing a remount, we simply refresh our temporary properties
1965 	 * according to those options set in the current VFS options.
1966 	 */
1967 	if (uap->flags & MS_REMOUNT) {
1968 		/* refresh mount options */
1969 		zfs_unregister_callbacks(vfsp->vfs_data);
1970 		error = zfs_register_callbacks(vfsp);
1971 		goto out;
1972 	}
1973 
1974 	error = zfs_domount(vfsp, osname);
1975 
1976 	/*
1977 	 * Add an extra VFS_HOLD on our parent vfs so that it can't
1978 	 * disappear due to a forced unmount.
1979 	 */
1980 	if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1981 		VFS_HOLD(mvp->v_vfsp);
1982 
1983 out:
1984 	pn_free(&spn);
1985 	return (error);
1986 }
1987 
1988 static int
1989 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
1990 {
1991 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
1992 	dev32_t d32;
1993 	uint64_t refdbytes, availbytes, usedobjs, availobjs;
1994 	int err = 0;
1995 
1996 	ZFS_ENTER(zfsvfs);
1997 
1998 	dmu_objset_space(zfsvfs->z_os,
1999 	    &refdbytes, &availbytes, &usedobjs, &availobjs);
2000 
2001 	/*
2002 	 * The underlying storage pool actually uses multiple block sizes.
2003 	 * We report the fragsize as the smallest block size we support,
2004 	 * and we report our blocksize as the filesystem's maximum blocksize.
2005 	 */
2006 	statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
2007 	statp->f_bsize = zfsvfs->z_max_blksz;
2008 
2009 	/*
2010 	 * The following report "total" blocks of various kinds in the
2011 	 * file system, but reported in terms of f_frsize - the
2012 	 * "fragment" size.
2013 	 */
2014 
2015 	statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
2016 	statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
2017 	statp->f_bavail = statp->f_bfree; /* no root reservation */
2018 
2019 	/*
2020 	 * statvfs() should really be called statufs(), because it assumes
2021 	 * static metadata.  ZFS doesn't preallocate files, so the best
2022 	 * we can do is report the max that could possibly fit in f_files,
2023 	 * and that minus the number actually used in f_ffree.
2024 	 * For f_ffree, report the smaller of the number of object available
2025 	 * and the number of blocks (each object will take at least a block).
2026 	 */
2027 	statp->f_ffree = MIN(availobjs, statp->f_bfree);
2028 	statp->f_favail = statp->f_ffree;	/* no "root reservation" */
2029 	statp->f_files = statp->f_ffree + usedobjs;
2030 
2031 	(void) cmpldev(&d32, vfsp->vfs_dev);
2032 	statp->f_fsid = d32;
2033 
2034 	/*
2035 	 * We're a zfs filesystem.
2036 	 */
2037 	(void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
2038 
2039 	statp->f_flag = vf_to_stf(vfsp->vfs_flag);
2040 
2041 	statp->f_namemax = MAXNAMELEN - 1;
2042 
2043 	/*
2044 	 * We have all of 32 characters to stuff a string here.
2045 	 * Is there anything useful we could/should provide?
2046 	 */
2047 	bzero(statp->f_fstr, sizeof (statp->f_fstr));
2048 
2049 	if (dmu_objset_projectquota_enabled(zfsvfs->z_os) &&
2050 	    dmu_objset_projectquota_present(zfsvfs->z_os)) {
2051 		znode_t *zp;
2052 
2053 		/*
2054 		 * In ZoL, zfs_statvfs is passed a Linux dentry (directory
2055 		 * entry), instead of a vfsp. The ZoL code uses the dentry
2056 		 * to get the znode from the dentry's inode. This represents
2057 		 * whatever filename was passed to the user-level statvfs
2058 		 * syscall.
2059 		 *
2060 		 * We're using the VFS root znode here, so this represents a
2061 		 * potential difference from ZoL.
2062 		 */
2063 		if (zfs_zget(zfsvfs, zfsvfs->z_root, &zp) == 0) {
2064 			uint32_t bshift = ddi_fls(statp->f_bsize) - 1;
2065 
2066 			if (zp->z_pflags & ZFS_PROJINHERIT && zp->z_projid &&
2067 			    zpl_is_valid_projid(zp->z_projid))
2068 				err = zfs_statfs_project(zfsvfs, zp, statp,
2069 				    bshift);
2070 			VN_RELE(ZTOV(zp));
2071 		}
2072 	}
2073 
2074 	ZFS_EXIT(zfsvfs);
2075 	return (err);
2076 }
2077 
2078 static int
2079 zfs_root(vfs_t *vfsp, vnode_t **vpp)
2080 {
2081 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2082 	znode_t *rootzp;
2083 	int error;
2084 
2085 	ZFS_ENTER(zfsvfs);
2086 
2087 	error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
2088 	if (error == 0)
2089 		*vpp = ZTOV(rootzp);
2090 
2091 	ZFS_EXIT(zfsvfs);
2092 	return (error);
2093 }
2094 
2095 /*
2096  * Teardown the zfsvfs::z_os.
2097  *
2098  * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
2099  * and 'z_teardown_inactive_lock' held.
2100  */
2101 static int
2102 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
2103 {
2104 	znode_t	*zp;
2105 
2106 	zfs_unlinked_drain_stop_wait(zfsvfs);
2107 
2108 	rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
2109 
2110 	if (!unmounting) {
2111 		/*
2112 		 * We purge the parent filesystem's vfsp as the parent
2113 		 * filesystem and all of its snapshots have their vnode's
2114 		 * v_vfsp set to the parent's filesystem's vfsp.  Note,
2115 		 * 'z_parent' is self referential for non-snapshots.
2116 		 */
2117 		(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
2118 	}
2119 
2120 	/*
2121 	 * Close the zil. NB: Can't close the zil while zfs_inactive
2122 	 * threads are blocked as zil_close can call zfs_inactive.
2123 	 */
2124 	if (zfsvfs->z_log) {
2125 		zil_close(zfsvfs->z_log);
2126 		zfsvfs->z_log = NULL;
2127 	}
2128 
2129 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
2130 
2131 	/*
2132 	 * If we are not unmounting (ie: online recv) and someone already
2133 	 * unmounted this file system while we were doing the switcheroo,
2134 	 * or a reopen of z_os failed then just bail out now.
2135 	 */
2136 	if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
2137 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
2138 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2139 		return (SET_ERROR(EIO));
2140 	}
2141 
2142 	/*
2143 	 * At this point there are no vops active, and any new vops will
2144 	 * fail with EIO since we have z_teardown_lock for writer (only
2145 	 * relavent for forced unmount).
2146 	 *
2147 	 * Release all holds on dbufs.
2148 	 */
2149 	mutex_enter(&zfsvfs->z_znodes_lock);
2150 	for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
2151 	    zp = list_next(&zfsvfs->z_all_znodes, zp))
2152 		if (zp->z_sa_hdl) {
2153 			ASSERT(ZTOV(zp)->v_count > 0);
2154 			zfs_znode_dmu_fini(zp);
2155 		}
2156 	mutex_exit(&zfsvfs->z_znodes_lock);
2157 
2158 	/*
2159 	 * If we are unmounting, set the unmounted flag and let new vops
2160 	 * unblock.  zfs_inactive will have the unmounted behavior, and all
2161 	 * other vops will fail with EIO.
2162 	 */
2163 	if (unmounting) {
2164 		zfsvfs->z_unmounted = B_TRUE;
2165 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
2166 		rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2167 	}
2168 
2169 	/*
2170 	 * z_os will be NULL if there was an error in attempting to reopen
2171 	 * zfsvfs, so just return as the properties had already been
2172 	 * unregistered and cached data had been evicted before.
2173 	 */
2174 	if (zfsvfs->z_os == NULL)
2175 		return (0);
2176 
2177 	/*
2178 	 * Unregister properties.
2179 	 */
2180 	zfs_unregister_callbacks(zfsvfs);
2181 
2182 	/*
2183 	 * Evict cached data
2184 	 */
2185 	if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) &&
2186 	    !(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
2187 		txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
2188 	dmu_objset_evict_dbufs(zfsvfs->z_os);
2189 
2190 	return (0);
2191 }
2192 
2193 /*ARGSUSED*/
2194 static int
2195 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
2196 {
2197 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2198 	objset_t *os;
2199 	int ret;
2200 
2201 	ret = secpolicy_fs_unmount(cr, vfsp);
2202 	if (ret) {
2203 		if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
2204 		    ZFS_DELEG_PERM_MOUNT, cr))
2205 			return (ret);
2206 	}
2207 
2208 	/*
2209 	 * We purge the parent filesystem's vfsp as the parent filesystem
2210 	 * and all of its snapshots have their vnode's v_vfsp set to the
2211 	 * parent's filesystem's vfsp.  Note, 'z_parent' is self
2212 	 * referential for non-snapshots.
2213 	 */
2214 	(void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
2215 
2216 	/*
2217 	 * Unmount any snapshots mounted under .zfs before unmounting the
2218 	 * dataset itself.
2219 	 */
2220 	if (zfsvfs->z_ctldir != NULL &&
2221 	    (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
2222 		return (ret);
2223 	}
2224 
2225 	if (!(fflag & MS_FORCE)) {
2226 		/*
2227 		 * Check the number of active vnodes in the file system.
2228 		 * Our count is maintained in the vfs structure, but the
2229 		 * number is off by 1 to indicate a hold on the vfs
2230 		 * structure itself.
2231 		 *
2232 		 * The '.zfs' directory maintains a reference of its
2233 		 * own, and any active references underneath are
2234 		 * reflected in the vnode count.
2235 		 */
2236 		if (zfsvfs->z_ctldir == NULL) {
2237 			if (vfsp->vfs_count > 1)
2238 				return (SET_ERROR(EBUSY));
2239 		} else {
2240 			if (vfsp->vfs_count > 2 ||
2241 			    zfsvfs->z_ctldir->v_count > 1)
2242 				return (SET_ERROR(EBUSY));
2243 		}
2244 	}
2245 
2246 	vfsp->vfs_flag |= VFS_UNMOUNTED;
2247 
2248 	VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
2249 	os = zfsvfs->z_os;
2250 
2251 	/*
2252 	 * z_os will be NULL if there was an error in
2253 	 * attempting to reopen zfsvfs.
2254 	 */
2255 	if (os != NULL) {
2256 		/*
2257 		 * Unset the objset user_ptr.
2258 		 */
2259 		mutex_enter(&os->os_user_ptr_lock);
2260 		dmu_objset_set_user(os, NULL);
2261 		mutex_exit(&os->os_user_ptr_lock);
2262 
2263 		/*
2264 		 * Finally release the objset
2265 		 */
2266 		dmu_objset_disown(os, B_TRUE, zfsvfs);
2267 	}
2268 
2269 	/*
2270 	 * We can now safely destroy the '.zfs' directory node.
2271 	 */
2272 	if (zfsvfs->z_ctldir != NULL)
2273 		zfsctl_destroy(zfsvfs);
2274 
2275 	return (0);
2276 }
2277 
2278 static int
2279 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
2280 {
2281 	zfsvfs_t	*zfsvfs = vfsp->vfs_data;
2282 	znode_t		*zp;
2283 	uint64_t	object = 0;
2284 	uint64_t	fid_gen = 0;
2285 	uint64_t	gen_mask;
2286 	uint64_t	zp_gen;
2287 	int		i, err;
2288 
2289 	*vpp = NULL;
2290 
2291 	ZFS_ENTER(zfsvfs);
2292 
2293 	if (fidp->fid_len == LONG_FID_LEN) {
2294 		zfid_long_t	*zlfid = (zfid_long_t *)fidp;
2295 		uint64_t	objsetid = 0;
2296 		uint64_t	setgen = 0;
2297 
2298 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
2299 			objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
2300 
2301 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
2302 			setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
2303 
2304 		ZFS_EXIT(zfsvfs);
2305 
2306 		err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
2307 		if (err)
2308 			return (SET_ERROR(EINVAL));
2309 		ZFS_ENTER(zfsvfs);
2310 	}
2311 
2312 	if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
2313 		zfid_short_t	*zfid = (zfid_short_t *)fidp;
2314 
2315 		for (i = 0; i < sizeof (zfid->zf_object); i++)
2316 			object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
2317 
2318 		for (i = 0; i < sizeof (zfid->zf_gen); i++)
2319 			fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
2320 	} else {
2321 		ZFS_EXIT(zfsvfs);
2322 		return (SET_ERROR(EINVAL));
2323 	}
2324 
2325 	/* A zero fid_gen means we are in the .zfs control directories */
2326 	if (fid_gen == 0 &&
2327 	    (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
2328 		*vpp = zfsvfs->z_ctldir;
2329 		ASSERT(*vpp != NULL);
2330 		if (object == ZFSCTL_INO_SNAPDIR) {
2331 			VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
2332 			    0, NULL, NULL, NULL, NULL, NULL) == 0);
2333 		} else {
2334 			VN_HOLD(*vpp);
2335 		}
2336 		ZFS_EXIT(zfsvfs);
2337 		return (0);
2338 	}
2339 
2340 	gen_mask = -1ULL >> (64 - 8 * i);
2341 
2342 	dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
2343 	if (err = zfs_zget(zfsvfs, object, &zp)) {
2344 		ZFS_EXIT(zfsvfs);
2345 		return (err);
2346 	}
2347 	(void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
2348 	    sizeof (uint64_t));
2349 	zp_gen = zp_gen & gen_mask;
2350 	if (zp_gen == 0)
2351 		zp_gen = 1;
2352 	if (zp->z_unlinked || zp_gen != fid_gen) {
2353 		dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
2354 		VN_RELE(ZTOV(zp));
2355 		ZFS_EXIT(zfsvfs);
2356 		return (SET_ERROR(EINVAL));
2357 	}
2358 
2359 	*vpp = ZTOV(zp);
2360 	ZFS_EXIT(zfsvfs);
2361 	return (0);
2362 }
2363 
2364 /*
2365  * Block out VOPs and close zfsvfs_t::z_os
2366  *
2367  * Note, if successful, then we return with the 'z_teardown_lock' and
2368  * 'z_teardown_inactive_lock' write held.  We leave ownership of the underlying
2369  * dataset and objset intact so that they can be atomically handed off during
2370  * a subsequent rollback or recv operation and the resume thereafter.
2371  */
2372 int
2373 zfs_suspend_fs(zfsvfs_t *zfsvfs)
2374 {
2375 	int error;
2376 
2377 	if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2378 		return (error);
2379 
2380 	return (0);
2381 }
2382 
2383 /*
2384  * Rebuild SA and release VOPs.  Note that ownership of the underlying dataset
2385  * is an invariant across any of the operations that can be performed while the
2386  * filesystem was suspended.  Whether it succeeded or failed, the preconditions
2387  * are the same: the relevant objset and associated dataset are owned by
2388  * zfsvfs, held, and long held on entry.
2389  */
2390 int
2391 zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
2392 {
2393 	int err;
2394 	znode_t *zp;
2395 
2396 	ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
2397 	ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2398 
2399 	/*
2400 	 * We already own this, so just update the objset_t, as the one we
2401 	 * had before may have been evicted.
2402 	 */
2403 	objset_t *os;
2404 	VERIFY3P(ds->ds_owner, ==, zfsvfs);
2405 	VERIFY(dsl_dataset_long_held(ds));
2406 	VERIFY0(dmu_objset_from_ds(ds, &os));
2407 
2408 	err = zfsvfs_init(zfsvfs, os);
2409 	if (err != 0)
2410 		goto bail;
2411 
2412 	VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2413 
2414 	zfs_set_fuid_feature(zfsvfs);
2415 
2416 	/*
2417 	 * Attempt to re-establish all the active znodes with
2418 	 * their dbufs.  If a zfs_rezget() fails, then we'll let
2419 	 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
2420 	 * when they try to use their znode.
2421 	 */
2422 	mutex_enter(&zfsvfs->z_znodes_lock);
2423 	for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2424 	    zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2425 		(void) zfs_rezget(zp);
2426 	}
2427 	mutex_exit(&zfsvfs->z_znodes_lock);
2428 
2429 	if (((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0) &&
2430 	    !zfsvfs->z_unmounted) {
2431 		/*
2432 		 * zfs_suspend_fs() could have interrupted freeing
2433 		 * of dnodes. We need to restart this freeing so
2434 		 * that we don't "leak" the space.
2435 		 */
2436 		zfs_unlinked_drain(zfsvfs);
2437 	}
2438 
2439 bail:
2440 	/* release the VOPs */
2441 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
2442 	rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2443 
2444 	if (err) {
2445 		/*
2446 		 * Since we couldn't setup the sa framework, try to force
2447 		 * unmount this file system.
2448 		 */
2449 		if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
2450 			(void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
2451 	}
2452 	return (err);
2453 }
2454 
2455 static void
2456 zfs_freevfs(vfs_t *vfsp)
2457 {
2458 	zfsvfs_t *zfsvfs = vfsp->vfs_data;
2459 
2460 	/*
2461 	 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2462 	 * from zfs_mount().  Release it here.  If we came through
2463 	 * zfs_mountroot() instead, we didn't grab an extra hold, so
2464 	 * skip the VFS_RELE for rootvfs.
2465 	 */
2466 	if (zfsvfs->z_issnap && (vfsp != rootvfs))
2467 		VFS_RELE(zfsvfs->z_parent->z_vfs);
2468 
2469 	zfsvfs_free(zfsvfs);
2470 
2471 	atomic_dec_32(&zfs_active_fs_count);
2472 }
2473 
2474 /*
2475  * VFS_INIT() initialization.  Note that there is no VFS_FINI(),
2476  * so we can't safely do any non-idempotent initialization here.
2477  * Leave that to zfs_init() and zfs_fini(), which are called
2478  * from the module's _init() and _fini() entry points.
2479  */
2480 /*ARGSUSED*/
2481 static int
2482 zfs_vfsinit(int fstype, char *name)
2483 {
2484 	int error;
2485 
2486 	zfsfstype = fstype;
2487 
2488 	/*
2489 	 * Setup vfsops and vnodeops tables.
2490 	 */
2491 	error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
2492 	if (error != 0) {
2493 		cmn_err(CE_WARN, "zfs: bad vfs ops template");
2494 	}
2495 
2496 	error = zfs_create_op_tables();
2497 	if (error) {
2498 		zfs_remove_op_tables();
2499 		cmn_err(CE_WARN, "zfs: bad vnode ops template");
2500 		(void) vfs_freevfsops_by_type(zfsfstype);
2501 		return (error);
2502 	}
2503 
2504 	mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
2505 
2506 	/*
2507 	 * Unique major number for all zfs mounts.
2508 	 * If we run out of 32-bit minors, we'll getudev() another major.
2509 	 */
2510 	zfs_major = ddi_name_to_major(ZFS_DRIVER);
2511 	zfs_minor = ZFS_MIN_MINOR;
2512 
2513 	return (0);
2514 }
2515 
2516 void
2517 zfs_init(void)
2518 {
2519 	/*
2520 	 * Initialize .zfs directory structures
2521 	 */
2522 	zfsctl_init();
2523 
2524 	/*
2525 	 * Initialize znode cache, vnode ops, etc...
2526 	 */
2527 	zfs_znode_init();
2528 
2529 	dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2530 }
2531 
2532 void
2533 zfs_fini(void)
2534 {
2535 	zfsctl_fini();
2536 	zfs_znode_fini();
2537 }
2538 
2539 int
2540 zfs_busy(void)
2541 {
2542 	return (zfs_active_fs_count != 0);
2543 }
2544 
2545 int
2546 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2547 {
2548 	int error;
2549 	objset_t *os = zfsvfs->z_os;
2550 	dmu_tx_t *tx;
2551 
2552 	if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2553 		return (SET_ERROR(EINVAL));
2554 
2555 	if (newvers < zfsvfs->z_version)
2556 		return (SET_ERROR(EINVAL));
2557 
2558 	if (zfs_spa_version_map(newvers) >
2559 	    spa_version(dmu_objset_spa(zfsvfs->z_os)))
2560 		return (SET_ERROR(ENOTSUP));
2561 
2562 	tx = dmu_tx_create(os);
2563 	dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2564 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2565 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2566 		    ZFS_SA_ATTRS);
2567 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2568 	}
2569 	error = dmu_tx_assign(tx, TXG_WAIT);
2570 	if (error) {
2571 		dmu_tx_abort(tx);
2572 		return (error);
2573 	}
2574 
2575 	error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2576 	    8, 1, &newvers, tx);
2577 
2578 	if (error) {
2579 		dmu_tx_commit(tx);
2580 		return (error);
2581 	}
2582 
2583 	if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2584 		uint64_t sa_obj;
2585 
2586 		ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2587 		    SPA_VERSION_SA);
2588 		sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2589 		    DMU_OT_NONE, 0, tx);
2590 
2591 		error = zap_add(os, MASTER_NODE_OBJ,
2592 		    ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2593 		ASSERT0(error);
2594 
2595 		VERIFY(0 == sa_set_sa_object(os, sa_obj));
2596 		sa_register_update_callback(os, zfs_sa_upgrade);
2597 	}
2598 
2599 	spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2600 	    "from %llu to %llu", zfsvfs->z_version, newvers);
2601 
2602 	dmu_tx_commit(tx);
2603 
2604 	zfsvfs->z_version = newvers;
2605 	os->os_version = newvers;
2606 
2607 	zfs_set_fuid_feature(zfsvfs);
2608 
2609 	return (0);
2610 }
2611 
2612 /*
2613  * Read a property stored within the master node.
2614  */
2615 int
2616 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2617 {
2618 	uint64_t *cached_copy = NULL;
2619 
2620 	/*
2621 	 * Figure out where in the objset_t the cached copy would live, if it
2622 	 * is available for the requested property.
2623 	 */
2624 	if (os != NULL) {
2625 		switch (prop) {
2626 		case ZFS_PROP_VERSION:
2627 			cached_copy = &os->os_version;
2628 			break;
2629 		case ZFS_PROP_NORMALIZE:
2630 			cached_copy = &os->os_normalization;
2631 			break;
2632 		case ZFS_PROP_UTF8ONLY:
2633 			cached_copy = &os->os_utf8only;
2634 			break;
2635 		case ZFS_PROP_CASE:
2636 			cached_copy = &os->os_casesensitivity;
2637 			break;
2638 		default:
2639 			break;
2640 		}
2641 	}
2642 	if (cached_copy != NULL && *cached_copy != OBJSET_PROP_UNINITIALIZED) {
2643 		*value = *cached_copy;
2644 		return (0);
2645 	}
2646 
2647 	/*
2648 	 * If the property wasn't cached, look up the file system's value for
2649 	 * the property. For the version property, we look up a slightly
2650 	 * different string.
2651 	 */
2652 	const char *pname;
2653 	int error = ENOENT;
2654 	if (prop == ZFS_PROP_VERSION) {
2655 		pname = ZPL_VERSION_STR;
2656 	} else {
2657 		pname = zfs_prop_to_name(prop);
2658 	}
2659 
2660 	if (os != NULL) {
2661 		ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);
2662 		error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2663 	}
2664 
2665 	if (error == ENOENT) {
2666 		/* No value set, use the default value */
2667 		switch (prop) {
2668 		case ZFS_PROP_VERSION:
2669 			*value = ZPL_VERSION;
2670 			break;
2671 		case ZFS_PROP_NORMALIZE:
2672 		case ZFS_PROP_UTF8ONLY:
2673 			*value = 0;
2674 			break;
2675 		case ZFS_PROP_CASE:
2676 			*value = ZFS_CASE_SENSITIVE;
2677 			break;
2678 		default:
2679 			return (error);
2680 		}
2681 		error = 0;
2682 	}
2683 
2684 	/*
2685 	 * If one of the methods for getting the property value above worked,
2686 	 * copy it into the objset_t's cache.
2687 	 */
2688 	if (error == 0 && cached_copy != NULL) {
2689 		*cached_copy = *value;
2690 	}
2691 
2692 	return (error);
2693 }
2694 
2695 /*
2696  * Return true if the coresponding vfs's unmounted flag is set.
2697  * Otherwise return false.
2698  * If this function returns true we know VFS unmount has been initiated.
2699  */
2700 boolean_t
2701 zfs_get_vfs_flag_unmounted(objset_t *os)
2702 {
2703 	zfsvfs_t *zfvp;
2704 	boolean_t unmounted = B_FALSE;
2705 
2706 	ASSERT(dmu_objset_type(os) == DMU_OST_ZFS);
2707 
2708 	mutex_enter(&os->os_user_ptr_lock);
2709 	zfvp = dmu_objset_get_user(os);
2710 	if (zfvp != NULL && zfvp->z_vfs != NULL &&
2711 	    (zfvp->z_vfs->vfs_flag & VFS_UNMOUNTED))
2712 		unmounted = B_TRUE;
2713 	mutex_exit(&os->os_user_ptr_lock);
2714 
2715 	return (unmounted);
2716 }
2717 
2718 static vfsdef_t vfw = {
2719 	VFSDEF_VERSION,
2720 	MNTTYPE_ZFS,
2721 	zfs_vfsinit,
2722 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
2723 	    VSW_XID|VSW_ZMOUNT,
2724 	&zfs_mntopts
2725 };
2726 
2727 struct modlfs zfs_modlfs = {
2728 	&mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
2729 };
2730