17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55a59a8b3Srsb  * Common Development and Distribution License (the "License").
65a59a8b3Srsb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22770915ebSRic Aleshire  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/param.h>
267c478bd9Sstevel@tonic-gate #include <sys/errno.h>
277c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
28aa59c4cbSrsb #include <sys/vfs_opreg.h>
297c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
307c478bd9Sstevel@tonic-gate #include <sys/uio.h>
317c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
327c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
337c478bd9Sstevel@tonic-gate #include <sys/cred.h>
347c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
357c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
367c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_node.h>
377c478bd9Sstevel@tonic-gate #include <sys/mount.h>
387c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
397c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
4045916cd2Sjpk #include <sys/priv.h>
417c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
447c478bd9Sstevel@tonic-gate #include <sys/policy.h>
4545916cd2Sjpk #include <sys/tsol/label.h>
467c478bd9Sstevel@tonic-gate #include "fs/fs_subr.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static mntopts_t lofs_mntopts;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int lofsinit(int, char *);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static vfsdef_t vfw = {
587c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
597c478bd9Sstevel@tonic-gate 	"lofs",
607c478bd9Sstevel@tonic-gate 	lofsinit,
610fbb751dSJohn Levon 	VSW_HASPROTO|VSW_STATS|VSW_ZMOUNT,
627c478bd9Sstevel@tonic-gate 	&lofs_mntopts
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * LOFS mount options table
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
697c478bd9Sstevel@tonic-gate static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
707c478bd9Sstevel@tonic-gate static char *sub_cancel[] = { MNTOPT_LOFS_NOSUB, NULL };
717c478bd9Sstevel@tonic-gate static char *nosub_cancel[] = { MNTOPT_LOFS_SUB, NULL };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static mntopt_t mntopts[] = {
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  *	option name		cancel option	default arg	flags
767c478bd9Sstevel@tonic-gate  *		private data
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate 	{ MNTOPT_XATTR,		xattr_cancel,	NULL,		0,
797c478bd9Sstevel@tonic-gate 		(void *)0 },
807c478bd9Sstevel@tonic-gate 	{ MNTOPT_NOXATTR,	noxattr_cancel,	NULL,		0,
817c478bd9Sstevel@tonic-gate 		(void *)0 },
827c478bd9Sstevel@tonic-gate 	{ MNTOPT_LOFS_SUB,	sub_cancel,	NULL,		0,
837c478bd9Sstevel@tonic-gate 		(void *)0 },
847c478bd9Sstevel@tonic-gate 	{ MNTOPT_LOFS_NOSUB,	nosub_cancel,	NULL,		0,
857c478bd9Sstevel@tonic-gate 		(void *)0 },
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static mntopts_t lofs_mntopts = {
897c478bd9Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
907c478bd9Sstevel@tonic-gate 	mntopts
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static struct modlfs modlfs = {
987c478bd9Sstevel@tonic-gate 	&mod_fsops, "filesystem for lofs", &vfw
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1027c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
1037c478bd9Sstevel@tonic-gate };
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * This is the module initialization routine.
1077c478bd9Sstevel@tonic-gate  */
10845916cd2Sjpk 
1097c478bd9Sstevel@tonic-gate int
_init(void)11045916cd2Sjpk _init(void)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	int status;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	lofs_subrinit();
1157c478bd9Sstevel@tonic-gate 	status = mod_install(&modlinkage);
1167c478bd9Sstevel@tonic-gate 	if (status != 0) {
1177c478bd9Sstevel@tonic-gate 		/*
1187c478bd9Sstevel@tonic-gate 		 * Cleanup previously initialized work.
1197c478bd9Sstevel@tonic-gate 		 */
1207c478bd9Sstevel@tonic-gate 		lofs_subrfini();
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	return (status);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Don't allow the lofs module to be unloaded for now.
1287c478bd9Sstevel@tonic-gate  * There is a memory leak if it gets unloaded.
1297c478bd9Sstevel@tonic-gate  */
13045916cd2Sjpk 
1317c478bd9Sstevel@tonic-gate int
_fini(void)13245916cd2Sjpk _fini(void)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	return (EBUSY);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1387c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static int lofsfstype;
1457c478bd9Sstevel@tonic-gate vfsops_t *lo_vfsops;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * lo mount vfsop
1497c478bd9Sstevel@tonic-gate  * Set up mount info record and attach it to vfs struct.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1527c478bd9Sstevel@tonic-gate static int
lo_mount(struct vfs * vfsp,struct vnode * vp,struct mounta * uap,struct cred * cr)1537c478bd9Sstevel@tonic-gate lo_mount(struct vfs *vfsp,
1547c478bd9Sstevel@tonic-gate 	struct vnode *vp,
1557c478bd9Sstevel@tonic-gate 	struct mounta *uap,
1567c478bd9Sstevel@tonic-gate 	struct cred *cr)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	int error;
1597c478bd9Sstevel@tonic-gate 	struct vnode *srootvp = NULL;	/* the server's root */
1607c478bd9Sstevel@tonic-gate 	struct vnode *realrootvp;
1617c478bd9Sstevel@tonic-gate 	struct loinfo *li;
1627c478bd9Sstevel@tonic-gate 	int nodev;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	nodev = vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, vp, vfsp)) != 0)
1677c478bd9Sstevel@tonic-gate 		return (EPERM);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Loopback devices which get "nodevices" added can be done without
1717c478bd9Sstevel@tonic-gate 	 * "nodevices" set because we cannot import devices into a zone
1727c478bd9Sstevel@tonic-gate 	 * with loopback.  Note that we have all zone privileges when
1737c478bd9Sstevel@tonic-gate 	 * this happens; if not, we'd have gotten "nosuid".
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	if (!nodev && vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
1767c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_DEVICES, NULL, VFS_NODISPLAY);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
1797c478bd9Sstevel@tonic-gate 	if (!(uap->flags & MS_OVERLAY) &&
18045916cd2Sjpk 	    (vp->v_count != 1 || (vp->v_flag & VROOT))) {
1817c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
1827c478bd9Sstevel@tonic-gate 		return (EBUSY);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/*
1877c478bd9Sstevel@tonic-gate 	 * Find real root, and make vfs point to real vfs
1887c478bd9Sstevel@tonic-gate 	 */
18979a28c7aSmarks 
1907c478bd9Sstevel@tonic-gate 	if (error = lookupname(uap->spec, (uap->flags & MS_SYSSPACE) ?
19179a28c7aSmarks 	    UIO_SYSSPACE : UIO_USERSPACE, FOLLOW, NULLVPP, &realrootvp))
1927c478bd9Sstevel@tonic-gate 		return (error);
1937c478bd9Sstevel@tonic-gate 
19445916cd2Sjpk 	/*
19545916cd2Sjpk 	 * Enforce MAC policy if needed.
19645916cd2Sjpk 	 *
19745916cd2Sjpk 	 * Loopback mounts must not allow writing up. The dominance test
19845916cd2Sjpk 	 * is intended to prevent a global zone caller from accidentally
19945916cd2Sjpk 	 * creating write-up conditions between two labeled zones.
20045916cd2Sjpk 	 * Local zones can't violate MAC on their own without help from
20145916cd2Sjpk 	 * the global zone because they can't name a pathname that
20245916cd2Sjpk 	 * they don't already have.
20345916cd2Sjpk 	 *
20445916cd2Sjpk 	 * The special case check for the NET_MAC_AWARE process flag is
20545916cd2Sjpk 	 * to support the case of the automounter in the global zone. We
20645916cd2Sjpk 	 * permit automounting of local zone directories such as home
20745916cd2Sjpk 	 * directories, into the global zone as required by setlabel,
20845916cd2Sjpk 	 * zonecopy, and saving of desktop sessions. Such mounts are
20945916cd2Sjpk 	 * trusted not to expose the contents of one zone's directories
21045916cd2Sjpk 	 * to another by leaking them through the global zone.
21145916cd2Sjpk 	 */
21245916cd2Sjpk 	if (is_system_labeled() && crgetzoneid(cr) == GLOBAL_ZONEID) {
21366aa579dSrica 		char	specname[MAXPATHLEN];
21466aa579dSrica 		zone_t	*from_zptr;
21566aa579dSrica 		zone_t	*to_zptr;
21666aa579dSrica 
21766aa579dSrica 		if (vnodetopath(NULL, realrootvp, specname,
2187f66f2f8Srica 		    sizeof (specname), CRED()) != 0) {
2197f66f2f8Srica 			VN_RELE(realrootvp);
22066aa579dSrica 			return (EACCES);
2217f66f2f8Srica 		}
22245916cd2Sjpk 
22366aa579dSrica 		from_zptr = zone_find_by_path(specname);
22445916cd2Sjpk 		to_zptr = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
22545916cd2Sjpk 
22645916cd2Sjpk 		/*
227770915ebSRic Aleshire 		 * Special case for scratch zones used for Live Upgrade:
22848451833Scarlsonj 		 * this is used to mount the zone's root from /root to /a in
22948451833Scarlsonj 		 * the scratch zone.  As with the other special case, this
23048451833Scarlsonj 		 * appears to be outside of the zone because it's not under
23148451833Scarlsonj 		 * the zone rootpath, which is $ZONEPATH/lu in the scratch
23248451833Scarlsonj 		 * zone case.
23345916cd2Sjpk 		 */
23445916cd2Sjpk 
23552782930Sszhou 		if (from_zptr != to_zptr &&
23648451833Scarlsonj 		    !(to_zptr->zone_flags & ZF_IS_SCRATCH)) {
23745916cd2Sjpk 			/*
23845916cd2Sjpk 			 * We know at this point that the labels aren't equal
23945916cd2Sjpk 			 * because the zone pointers aren't equal, and zones
24045916cd2Sjpk 			 * can't share a label.
24145916cd2Sjpk 			 *
24245916cd2Sjpk 			 * If the source is the global zone then making
24345916cd2Sjpk 			 * it available to a local zone must be done in
24445916cd2Sjpk 			 * read-only mode as the label will become admin_low.
24545916cd2Sjpk 			 *
24645916cd2Sjpk 			 * If it is a mount between local zones then if
24745916cd2Sjpk 			 * the current process is in the global zone and has
24845916cd2Sjpk 			 * the NET_MAC_AWARE flag, then regular read-write
24945916cd2Sjpk 			 * access is allowed.  If it's in some other zone, but
25045916cd2Sjpk 			 * the label on the mount point dominates the original
25145916cd2Sjpk 			 * source, then allow the mount as read-only
25245916cd2Sjpk 			 * ("read-down").
25345916cd2Sjpk 			 */
25445916cd2Sjpk 			if (from_zptr->zone_id == GLOBAL_ZONEID) {
25545916cd2Sjpk 				/* make the mount read-only */
25645916cd2Sjpk 				vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
25745916cd2Sjpk 			} else { /* cross-zone mount */
25845916cd2Sjpk 				if (to_zptr->zone_id == GLOBAL_ZONEID &&
25945916cd2Sjpk 				    /* LINTED: no consequent */
26045916cd2Sjpk 				    getpflags(NET_MAC_AWARE, cr) != 0) {
26145916cd2Sjpk 					/* Allow the mount as read-write */
26245916cd2Sjpk 				} else if (bldominates(
26345916cd2Sjpk 				    label2bslabel(to_zptr->zone_slabel),
26445916cd2Sjpk 				    label2bslabel(from_zptr->zone_slabel))) {
26545916cd2Sjpk 					/* make the mount read-only */
26645916cd2Sjpk 					vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
26745916cd2Sjpk 				} else {
2687f66f2f8Srica 					VN_RELE(realrootvp);
26945916cd2Sjpk 					zone_rele(to_zptr);
27045916cd2Sjpk 					zone_rele(from_zptr);
27145916cd2Sjpk 					return (EACCES);
27245916cd2Sjpk 				}
27345916cd2Sjpk 			}
27445916cd2Sjpk 		}
27545916cd2Sjpk 		zone_rele(to_zptr);
27645916cd2Sjpk 		zone_rele(from_zptr);
27745916cd2Sjpk 	}
27845916cd2Sjpk 
2797c478bd9Sstevel@tonic-gate 	/*
280*f4e22627SJan Kryl 	 * realrootvp may be an AUTOFS node, in which case we perform a
281*f4e22627SJan Kryl 	 * VOP_ACCESS() to trigger the mount of the intended filesystem.
282*f4e22627SJan Kryl 	 * This causes a loopback mount of the intended filesystem instead
283*f4e22627SJan Kryl 	 * of the AUTOFS filesystem.
284*f4e22627SJan Kryl 	 *
285*f4e22627SJan Kryl 	 * If a lofs mount creates a mount loop (such that a lofs vfs is
286*f4e22627SJan Kryl 	 * mounted on an autofs node and that lofs vfs points back to the
287*f4e22627SJan Kryl 	 * autofs node which it is mounted on) then a VOP_ACCESS call will
288*f4e22627SJan Kryl 	 * create a deadlock. Once this deadlock is released, VOP_ACCESS will
289*f4e22627SJan Kryl 	 * return EINTR. In such a case we don't want the lofs vfs to be
290*f4e22627SJan Kryl 	 * created as the loop could panic the system.
2917c478bd9Sstevel@tonic-gate 	 */
292*f4e22627SJan Kryl 	if ((error = VOP_ACCESS(realrootvp, 0, 0, cr, NULL)) != 0) {
293*f4e22627SJan Kryl 		VN_RELE(realrootvp);
294*f4e22627SJan Kryl 		return (error);
295*f4e22627SJan Kryl 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/*
2987c478bd9Sstevel@tonic-gate 	 * We're interested in the top most filesystem.
2997c478bd9Sstevel@tonic-gate 	 * This is specially important when uap->spec is a trigger
3007c478bd9Sstevel@tonic-gate 	 * AUTOFS node, since we're really interested in mounting the
3017c478bd9Sstevel@tonic-gate 	 * filesystem AUTOFS mounted as result of the VOP_ACCESS()
3027c478bd9Sstevel@tonic-gate 	 * call not the AUTOFS node itself.
3037c478bd9Sstevel@tonic-gate 	 */
3047c478bd9Sstevel@tonic-gate 	if (vn_mountedvfs(realrootvp) != NULL) {
3057c478bd9Sstevel@tonic-gate 		if (error = traverse(&realrootvp)) {
3067c478bd9Sstevel@tonic-gate 			VN_RELE(realrootvp);
3077c478bd9Sstevel@tonic-gate 			return (error);
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/*
3127c478bd9Sstevel@tonic-gate 	 * Allocate a vfs info struct and attach it
3137c478bd9Sstevel@tonic-gate 	 */
3147c478bd9Sstevel@tonic-gate 	li = kmem_zalloc(sizeof (struct loinfo), KM_SLEEP);
3157c478bd9Sstevel@tonic-gate 	li->li_realvfs = realrootvp->v_vfsp;
3167c478bd9Sstevel@tonic-gate 	li->li_mountvfs = vfsp;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/*
3197c478bd9Sstevel@tonic-gate 	 * Set mount flags to be inherited by loopback vfs's
3207c478bd9Sstevel@tonic-gate 	 */
3217c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
3227c478bd9Sstevel@tonic-gate 		li->li_mflag |= VFS_RDONLY;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
3257c478bd9Sstevel@tonic-gate 		li->li_mflag |= (VFS_NOSETUID|VFS_NODEVICES);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
3287c478bd9Sstevel@tonic-gate 		li->li_mflag |= VFS_NODEVICES;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
3317c478bd9Sstevel@tonic-gate 		li->li_mflag |= VFS_NOSETUID;
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 	/*
3347c478bd9Sstevel@tonic-gate 	 * Permissive flags are added to the "deny" bitmap.
3357c478bd9Sstevel@tonic-gate 	 */
3367c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
3377c478bd9Sstevel@tonic-gate 		li->li_dflag |= VFS_XATTR;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
3407c478bd9Sstevel@tonic-gate 		li->li_dflag |= VFS_NBMAND;
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 * Propagate inheritable mount flags from the real vfs.
3457c478bd9Sstevel@tonic-gate 	 */
3467c478bd9Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_RDONLY) &&
3477c478bd9Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_RO, NULL))
3487c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL,
3497c478bd9Sstevel@tonic-gate 		    VFS_NODISPLAY);
3507c478bd9Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_NOSETUID) &&
3517c478bd9Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
3527c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NOSETUID, NULL,
3537c478bd9Sstevel@tonic-gate 		    VFS_NODISPLAY);
3547c478bd9Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_NODEVICES) &&
3557c478bd9Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
3567c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NODEVICES, NULL,
3577c478bd9Sstevel@tonic-gate 		    VFS_NODISPLAY);
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * Permissive flags such as VFS_XATTR, as opposed to restrictive flags
3607c478bd9Sstevel@tonic-gate 	 * such as VFS_RDONLY, are handled differently.  An explicit
3617c478bd9Sstevel@tonic-gate 	 * MNTOPT_NOXATTR should override the underlying filesystem's VFS_XATTR.
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_XATTR) &&
3647c478bd9Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL) &&
3657c478bd9Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
3667c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_XATTR, NULL,
3677c478bd9Sstevel@tonic-gate 		    VFS_NODISPLAY);
3687c478bd9Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_NBMAND) &&
3697c478bd9Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL) &&
3707c478bd9Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL))
3717c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NBMAND, NULL,
3727c478bd9Sstevel@tonic-gate 		    VFS_NODISPLAY);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	li->li_refct = 0;
3757c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)li;
3767c478bd9Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
3777c478bd9Sstevel@tonic-gate 	vfsp->vfs_fstype = lofsfstype;
3787c478bd9Sstevel@tonic-gate 	vfsp->vfs_bsize = li->li_realvfs->vfs_bsize;
3797c478bd9Sstevel@tonic-gate 
38052782930Sszhou 	vfsp->vfs_dev = li->li_realvfs->vfs_dev;
38152782930Sszhou 	vfsp->vfs_fsid.val[0] = li->li_realvfs->vfs_fsid.val[0];
38252782930Sszhou 	vfsp->vfs_fsid.val[1] = li->li_realvfs->vfs_fsid.val[1];
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_LOFS_NOSUB, NULL)) {
3857c478bd9Sstevel@tonic-gate 		li->li_flag |= LO_NOSUB;
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
38879a28c7aSmarks 	/*
38979a28c7aSmarks 	 * Propagate any VFS features
39079a28c7aSmarks 	 */
39179a28c7aSmarks 
39279a28c7aSmarks 	vfs_propagate_features(li->li_realvfs, vfsp);
39379a28c7aSmarks 
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * Setup the hashtable. If the root of this mount isn't a directory,
3967c478bd9Sstevel@tonic-gate 	 * there's no point in allocating a large hashtable. A table with one
3977c478bd9Sstevel@tonic-gate 	 * bucket is sufficient.
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	if (realrootvp->v_type != VDIR)
4007c478bd9Sstevel@tonic-gate 		lsetup(li, 1);
4017c478bd9Sstevel@tonic-gate 	else
4027c478bd9Sstevel@tonic-gate 		lsetup(li, 0);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	/*
4057c478bd9Sstevel@tonic-gate 	 * Make the root vnode
4067c478bd9Sstevel@tonic-gate 	 */
407b431137cSowenr 	srootvp = makelonode(realrootvp, li, 0);
4087c478bd9Sstevel@tonic-gate 	srootvp->v_flag |= VROOT;
4097c478bd9Sstevel@tonic-gate 	li->li_rootvp = srootvp;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate #ifdef LODEBUG
4127c478bd9Sstevel@tonic-gate 	lo_dprint(4, "lo_mount: vfs %p realvfs %p root %p realroot %p li %p\n",
4137c478bd9Sstevel@tonic-gate 	    vfsp, li->li_realvfs, srootvp, realrootvp, li);
4147c478bd9Sstevel@tonic-gate #endif
4157c478bd9Sstevel@tonic-gate 	return (0);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  * Undo loopback mount
4207c478bd9Sstevel@tonic-gate  */
4217c478bd9Sstevel@tonic-gate static int
lo_unmount(struct vfs * vfsp,int flag,struct cred * cr)4227c478bd9Sstevel@tonic-gate lo_unmount(struct vfs *vfsp, int flag, struct cred *cr)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	struct loinfo *li;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
4277c478bd9Sstevel@tonic-gate 		return (EPERM);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * Forced unmount is not supported by this file system
4317c478bd9Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
4327c478bd9Sstevel@tonic-gate 	 */
4337c478bd9Sstevel@tonic-gate 	if (flag & MS_FORCE)
4347c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	li = vtoli(vfsp);
4377c478bd9Sstevel@tonic-gate #ifdef LODEBUG
4387c478bd9Sstevel@tonic-gate 	lo_dprint(4, "lo_unmount(%p) li %p\n", vfsp, li);
4397c478bd9Sstevel@tonic-gate #endif
4407c478bd9Sstevel@tonic-gate 	if (li->li_refct != 1 || li->li_rootvp->v_count != 1) {
4417c478bd9Sstevel@tonic-gate #ifdef LODEBUG
4427c478bd9Sstevel@tonic-gate 		lo_dprint(4, "refct %d v_ct %d\n", li->li_refct,
4437c478bd9Sstevel@tonic-gate 		    li->li_rootvp->v_count);
4447c478bd9Sstevel@tonic-gate #endif
4457c478bd9Sstevel@tonic-gate 		return (EBUSY);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 	VN_RELE(li->li_rootvp);
4487c478bd9Sstevel@tonic-gate 	return (0);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate /*
4527c478bd9Sstevel@tonic-gate  * Find root of lofs mount.
4537c478bd9Sstevel@tonic-gate  */
4547c478bd9Sstevel@tonic-gate static int
lo_root(struct vfs * vfsp,struct vnode ** vpp)4557c478bd9Sstevel@tonic-gate lo_root(struct vfs *vfsp, struct vnode **vpp)
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate 	*vpp = vtoli(vfsp)->li_rootvp;
4587c478bd9Sstevel@tonic-gate #ifdef LODEBUG
4597c478bd9Sstevel@tonic-gate 	lo_dprint(4, "lo_root(0x%p) = %p\n", vfsp, *vpp);
4607c478bd9Sstevel@tonic-gate #endif
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 * If the root of the filesystem is a special file, return the specvp
4637c478bd9Sstevel@tonic-gate 	 * version of the vnode. We don't save the specvp vnode in our
4647c478bd9Sstevel@tonic-gate 	 * hashtable since that's exclusively for lnodes.
4657c478bd9Sstevel@tonic-gate 	 */
4667c478bd9Sstevel@tonic-gate 	if (IS_DEVVP(*vpp)) {
4677c478bd9Sstevel@tonic-gate 		struct vnode *svp;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, kcred);
4707c478bd9Sstevel@tonic-gate 		if (svp == NULL)
4717c478bd9Sstevel@tonic-gate 			return (ENOSYS);
4727c478bd9Sstevel@tonic-gate 		*vpp = svp;
4737c478bd9Sstevel@tonic-gate 	} else {
4747c478bd9Sstevel@tonic-gate 		VN_HOLD(*vpp);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	return (0);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * Get file system statistics.
4827c478bd9Sstevel@tonic-gate  */
4837c478bd9Sstevel@tonic-gate static int
lo_statvfs(register struct vfs * vfsp,struct statvfs64 * sbp)4847c478bd9Sstevel@tonic-gate lo_statvfs(register struct vfs *vfsp, struct statvfs64 *sbp)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	vnode_t *realrootvp;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate #ifdef LODEBUG
4897c478bd9Sstevel@tonic-gate 	lo_dprint(4, "lostatvfs %p\n", vfsp);
4907c478bd9Sstevel@tonic-gate #endif
4917c478bd9Sstevel@tonic-gate 	/*
4927c478bd9Sstevel@tonic-gate 	 * Using realrootvp->v_vfsp (instead of the realvfsp that was
4937c478bd9Sstevel@tonic-gate 	 * cached) is necessary to make lofs work woth forced UFS unmounts.
4947c478bd9Sstevel@tonic-gate 	 * In the case of a forced unmount, UFS stores a set of dummy vfsops
4957c478bd9Sstevel@tonic-gate 	 * in all the (i)vnodes in the filesystem. The dummy ops simply
4967c478bd9Sstevel@tonic-gate 	 * returns back EIO.
4977c478bd9Sstevel@tonic-gate 	 */
4987c478bd9Sstevel@tonic-gate 	(void) lo_realvfs(vfsp, &realrootvp);
4997c478bd9Sstevel@tonic-gate 	if (realrootvp != NULL)
5007c478bd9Sstevel@tonic-gate 		return (VFS_STATVFS(realrootvp->v_vfsp, sbp));
5017c478bd9Sstevel@tonic-gate 	else
5027c478bd9Sstevel@tonic-gate 		return (EIO);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate /*
5067c478bd9Sstevel@tonic-gate  * LOFS doesn't have any data or metadata to flush, pending I/O on the
5077c478bd9Sstevel@tonic-gate  * underlying filesystem will be flushed when such filesystem is synched.
5087c478bd9Sstevel@tonic-gate  */
5097c478bd9Sstevel@tonic-gate /* ARGSUSED */
5107c478bd9Sstevel@tonic-gate static int
lo_sync(struct vfs * vfsp,short flag,struct cred * cr)5117c478bd9Sstevel@tonic-gate lo_sync(struct vfs *vfsp,
5127c478bd9Sstevel@tonic-gate 	short flag,
5137c478bd9Sstevel@tonic-gate 	struct cred *cr)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate #ifdef LODEBUG
5167c478bd9Sstevel@tonic-gate 	lo_dprint(4, "lo_sync: %p\n", vfsp);
5177c478bd9Sstevel@tonic-gate #endif
5187c478bd9Sstevel@tonic-gate 	return (0);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate  * Obtain the vnode from the underlying filesystem.
5237c478bd9Sstevel@tonic-gate  */
5247c478bd9Sstevel@tonic-gate static int
lo_vget(struct vfs * vfsp,struct vnode ** vpp,struct fid * fidp)5257c478bd9Sstevel@tonic-gate lo_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	vnode_t *realrootvp;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate #ifdef LODEBUG
5307c478bd9Sstevel@tonic-gate 	lo_dprint(4, "lo_vget: %p\n", vfsp);
5317c478bd9Sstevel@tonic-gate #endif
5327c478bd9Sstevel@tonic-gate 	(void) lo_realvfs(vfsp, &realrootvp);
5337c478bd9Sstevel@tonic-gate 	if (realrootvp != NULL)
5347c478bd9Sstevel@tonic-gate 		return (VFS_VGET(realrootvp->v_vfsp, vpp, fidp));
5357c478bd9Sstevel@tonic-gate 	else
5367c478bd9Sstevel@tonic-gate 		return (EIO);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * Free mount-specific data.
5417c478bd9Sstevel@tonic-gate  */
5427c478bd9Sstevel@tonic-gate static void
lo_freevfs(struct vfs * vfsp)5437c478bd9Sstevel@tonic-gate lo_freevfs(struct vfs *vfsp)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	struct loinfo *li = vtoli(vfsp);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	ldestroy(li);
5487c478bd9Sstevel@tonic-gate 	kmem_free(li, sizeof (struct loinfo));
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate static int
lofsinit(int fstyp,char * name)5527c478bd9Sstevel@tonic-gate lofsinit(int fstyp, char *name)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t lo_vfsops_template[] = {
555aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .vfs_mount = lo_mount },
556aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = lo_unmount },
557aa59c4cbSrsb 		VFSNAME_ROOT,		{ .vfs_root = lo_root },
558aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = lo_statvfs },
559aa59c4cbSrsb 		VFSNAME_SYNC,		{ .vfs_sync = lo_sync },
560aa59c4cbSrsb 		VFSNAME_VGET,		{ .vfs_vget = lo_vget },
561aa59c4cbSrsb 		VFSNAME_FREEVFS,	{ .vfs_freevfs = lo_freevfs },
562aa59c4cbSrsb 		NULL,			NULL
5637c478bd9Sstevel@tonic-gate 	};
5647c478bd9Sstevel@tonic-gate 	int error;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstyp, lo_vfsops_template, &lo_vfsops);
5677c478bd9Sstevel@tonic-gate 	if (error != 0) {
5687c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "lofsinit: bad vfs ops template");
5697c478bd9Sstevel@tonic-gate 		return (error);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, lo_vnodeops_template, &lo_vnodeops);
5737c478bd9Sstevel@tonic-gate 	if (error != 0) {
5747c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstyp);
5757c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "lofsinit: bad vnode ops template");
5767c478bd9Sstevel@tonic-gate 		return (error);
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	lofsfstype = fstyp;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	return (0);
5827c478bd9Sstevel@tonic-gate }
583