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