xref: /onnv-gate/usr/src/uts/common/fs/lofs/lofs_vfsops.c (revision 12645:0de9ed7d05ab)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51488Srsb  * Common Development and Distribution License (the "License").
61488Srsb  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212338SRic.Aleshire@Sun.COM  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/param.h>
260Sstevel@tonic-gate #include <sys/errno.h>
270Sstevel@tonic-gate #include <sys/vfs.h>
283898Srsb #include <sys/vfs_opreg.h>
290Sstevel@tonic-gate #include <sys/vnode.h>
300Sstevel@tonic-gate #include <sys/uio.h>
310Sstevel@tonic-gate #include <sys/pathname.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #include <sys/cred.h>
340Sstevel@tonic-gate #include <sys/statvfs.h>
350Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
360Sstevel@tonic-gate #include <sys/fs/lofs_node.h>
370Sstevel@tonic-gate #include <sys/mount.h>
380Sstevel@tonic-gate #include <sys/mntent.h>
390Sstevel@tonic-gate #include <sys/mkdev.h>
401676Sjpk #include <sys/priv.h>
410Sstevel@tonic-gate #include <sys/sysmacros.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/cmn_err.h>
440Sstevel@tonic-gate #include <sys/policy.h>
451676Sjpk #include <sys/tsol/label.h>
460Sstevel@tonic-gate #include "fs/fs_subr.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * This is the loadable module wrapper.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate #include <sys/modctl.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static mntopts_t lofs_mntopts;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static int lofsinit(int, char *);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static vfsdef_t vfw = {
580Sstevel@tonic-gate 	VFSDEF_VERSION,
590Sstevel@tonic-gate 	"lofs",
600Sstevel@tonic-gate 	lofsinit,
6112633Sjohn.levon@sun.com 	VSW_HASPROTO|VSW_STATS|VSW_ZMOUNT,
620Sstevel@tonic-gate 	&lofs_mntopts
630Sstevel@tonic-gate };
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * LOFS mount options table
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
690Sstevel@tonic-gate static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
700Sstevel@tonic-gate static char *sub_cancel[] = { MNTOPT_LOFS_NOSUB, NULL };
710Sstevel@tonic-gate static char *nosub_cancel[] = { MNTOPT_LOFS_SUB, NULL };
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static mntopt_t mntopts[] = {
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  *	option name		cancel option	default arg	flags
760Sstevel@tonic-gate  *		private data
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate 	{ MNTOPT_XATTR,		xattr_cancel,	NULL,		0,
790Sstevel@tonic-gate 		(void *)0 },
800Sstevel@tonic-gate 	{ MNTOPT_NOXATTR,	noxattr_cancel,	NULL,		0,
810Sstevel@tonic-gate 		(void *)0 },
820Sstevel@tonic-gate 	{ MNTOPT_LOFS_SUB,	sub_cancel,	NULL,		0,
830Sstevel@tonic-gate 		(void *)0 },
840Sstevel@tonic-gate 	{ MNTOPT_LOFS_NOSUB,	nosub_cancel,	NULL,		0,
850Sstevel@tonic-gate 		(void *)0 },
860Sstevel@tonic-gate };
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static mntopts_t lofs_mntopts = {
890Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
900Sstevel@tonic-gate 	mntopts
910Sstevel@tonic-gate };
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * Module linkage information for the kernel.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static struct modlfs modlfs = {
980Sstevel@tonic-gate 	&mod_fsops, "filesystem for lofs", &vfw
990Sstevel@tonic-gate };
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static struct modlinkage modlinkage = {
1020Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
1030Sstevel@tonic-gate };
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * This is the module initialization routine.
1070Sstevel@tonic-gate  */
1081676Sjpk 
1090Sstevel@tonic-gate int
_init(void)1101676Sjpk _init(void)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	int status;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	lofs_subrinit();
1150Sstevel@tonic-gate 	status = mod_install(&modlinkage);
1160Sstevel@tonic-gate 	if (status != 0) {
1170Sstevel@tonic-gate 		/*
1180Sstevel@tonic-gate 		 * Cleanup previously initialized work.
1190Sstevel@tonic-gate 		 */
1200Sstevel@tonic-gate 		lofs_subrfini();
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	return (status);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * Don't allow the lofs module to be unloaded for now.
1280Sstevel@tonic-gate  * There is a memory leak if it gets unloaded.
1290Sstevel@tonic-gate  */
1301676Sjpk 
1310Sstevel@tonic-gate int
_fini(void)1321676Sjpk _fini(void)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	return (EBUSY);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1380Sstevel@tonic-gate _info(struct modinfo *modinfop)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static int lofsfstype;
1450Sstevel@tonic-gate vfsops_t *lo_vfsops;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * lo mount vfsop
1490Sstevel@tonic-gate  * Set up mount info record and attach it to vfs struct.
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate /*ARGSUSED*/
1520Sstevel@tonic-gate static int
lo_mount(struct vfs * vfsp,struct vnode * vp,struct mounta * uap,struct cred * cr)1530Sstevel@tonic-gate lo_mount(struct vfs *vfsp,
1540Sstevel@tonic-gate 	struct vnode *vp,
1550Sstevel@tonic-gate 	struct mounta *uap,
1560Sstevel@tonic-gate 	struct cred *cr)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	int error;
1590Sstevel@tonic-gate 	struct vnode *srootvp = NULL;	/* the server's root */
1600Sstevel@tonic-gate 	struct vnode *realrootvp;
1610Sstevel@tonic-gate 	struct loinfo *li;
1620Sstevel@tonic-gate 	int nodev;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	nodev = vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, vp, vfsp)) != 0)
1670Sstevel@tonic-gate 		return (EPERM);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	/*
1700Sstevel@tonic-gate 	 * Loopback devices which get "nodevices" added can be done without
1710Sstevel@tonic-gate 	 * "nodevices" set because we cannot import devices into a zone
1720Sstevel@tonic-gate 	 * with loopback.  Note that we have all zone privileges when
1730Sstevel@tonic-gate 	 * this happens; if not, we'd have gotten "nosuid".
1740Sstevel@tonic-gate 	 */
1750Sstevel@tonic-gate 	if (!nodev && vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
1760Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_DEVICES, NULL, VFS_NODISPLAY);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
1790Sstevel@tonic-gate 	if (!(uap->flags & MS_OVERLAY) &&
1801676Sjpk 	    (vp->v_count != 1 || (vp->v_flag & VROOT))) {
1810Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
1820Sstevel@tonic-gate 		return (EBUSY);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * Find real root, and make vfs point to real vfs
1880Sstevel@tonic-gate 	 */
1896224Smarks 
1900Sstevel@tonic-gate 	if (error = lookupname(uap->spec, (uap->flags & MS_SYSSPACE) ?
1916224Smarks 	    UIO_SYSSPACE : UIO_USERSPACE, FOLLOW, NULLVPP, &realrootvp))
1920Sstevel@tonic-gate 		return (error);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	/*
1951676Sjpk 	 * Enforce MAC policy if needed.
1961676Sjpk 	 *
1971676Sjpk 	 * Loopback mounts must not allow writing up. The dominance test
1981676Sjpk 	 * is intended to prevent a global zone caller from accidentally
1991676Sjpk 	 * creating write-up conditions between two labeled zones.
2001676Sjpk 	 * Local zones can't violate MAC on their own without help from
2011676Sjpk 	 * the global zone because they can't name a pathname that
2021676Sjpk 	 * they don't already have.
2031676Sjpk 	 *
2041676Sjpk 	 * The special case check for the NET_MAC_AWARE process flag is
2051676Sjpk 	 * to support the case of the automounter in the global zone. We
2061676Sjpk 	 * permit automounting of local zone directories such as home
2071676Sjpk 	 * directories, into the global zone as required by setlabel,
2081676Sjpk 	 * zonecopy, and saving of desktop sessions. Such mounts are
2091676Sjpk 	 * trusted not to expose the contents of one zone's directories
2101676Sjpk 	 * to another by leaking them through the global zone.
2111676Sjpk 	 */
2121676Sjpk 	if (is_system_labeled() && crgetzoneid(cr) == GLOBAL_ZONEID) {
2131748Srica 		char	specname[MAXPATHLEN];
2141748Srica 		zone_t	*from_zptr;
2151748Srica 		zone_t	*to_zptr;
2161676Sjpk 
2171748Srica 		if (vnodetopath(NULL, realrootvp, specname,
2186538Srica 		    sizeof (specname), CRED()) != 0) {
2196538Srica 			VN_RELE(realrootvp);
2201748Srica 			return (EACCES);
2216538Srica 		}
2221748Srica 
2231676Sjpk 		from_zptr = zone_find_by_path(specname);
2241676Sjpk 		to_zptr = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
2251676Sjpk 
2261676Sjpk 		/*
22712338SRic.Aleshire@Sun.COM 		 * Special case for scratch zones used for Live Upgrade:
2281769Scarlsonj 		 * this is used to mount the zone's root from /root to /a in
2291769Scarlsonj 		 * the scratch zone.  As with the other special case, this
2301769Scarlsonj 		 * appears to be outside of the zone because it's not under
2311769Scarlsonj 		 * the zone rootpath, which is $ZONEPATH/lu in the scratch
2321769Scarlsonj 		 * zone case.
2331676Sjpk 		 */
2341676Sjpk 
2352656Sszhou 		if (from_zptr != to_zptr &&
2361769Scarlsonj 		    !(to_zptr->zone_flags & ZF_IS_SCRATCH)) {
2371676Sjpk 			/*
2381676Sjpk 			 * We know at this point that the labels aren't equal
2391676Sjpk 			 * because the zone pointers aren't equal, and zones
2401676Sjpk 			 * can't share a label.
2411676Sjpk 			 *
2421676Sjpk 			 * If the source is the global zone then making
2431676Sjpk 			 * it available to a local zone must be done in
2441676Sjpk 			 * read-only mode as the label will become admin_low.
2451676Sjpk 			 *
2461676Sjpk 			 * If it is a mount between local zones then if
2471676Sjpk 			 * the current process is in the global zone and has
2481676Sjpk 			 * the NET_MAC_AWARE flag, then regular read-write
2491676Sjpk 			 * access is allowed.  If it's in some other zone, but
2501676Sjpk 			 * the label on the mount point dominates the original
2511676Sjpk 			 * source, then allow the mount as read-only
2521676Sjpk 			 * ("read-down").
2531676Sjpk 			 */
2541676Sjpk 			if (from_zptr->zone_id == GLOBAL_ZONEID) {
2551676Sjpk 				/* make the mount read-only */
2561676Sjpk 				vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
2571676Sjpk 			} else { /* cross-zone mount */
2581676Sjpk 				if (to_zptr->zone_id == GLOBAL_ZONEID &&
2591676Sjpk 				    /* LINTED: no consequent */
2601676Sjpk 				    getpflags(NET_MAC_AWARE, cr) != 0) {
2611676Sjpk 					/* Allow the mount as read-write */
2621676Sjpk 				} else if (bldominates(
2631676Sjpk 				    label2bslabel(to_zptr->zone_slabel),
2641676Sjpk 				    label2bslabel(from_zptr->zone_slabel))) {
2651676Sjpk 					/* make the mount read-only */
2661676Sjpk 					vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
2671676Sjpk 				} else {
2686538Srica 					VN_RELE(realrootvp);
2691676Sjpk 					zone_rele(to_zptr);
2701676Sjpk 					zone_rele(from_zptr);
2711676Sjpk 					return (EACCES);
2721676Sjpk 				}
2731676Sjpk 			}
2741676Sjpk 		}
2751676Sjpk 		zone_rele(to_zptr);
2761676Sjpk 		zone_rele(from_zptr);
2771676Sjpk 	}
2781676Sjpk 
2791676Sjpk 	/*
280*12645SJan.Kryl@Sun.COM 	 * realrootvp may be an AUTOFS node, in which case we perform a
281*12645SJan.Kryl@Sun.COM 	 * VOP_ACCESS() to trigger the mount of the intended filesystem.
282*12645SJan.Kryl@Sun.COM 	 * This causes a loopback mount of the intended filesystem instead
283*12645SJan.Kryl@Sun.COM 	 * of the AUTOFS filesystem.
284*12645SJan.Kryl@Sun.COM 	 *
285*12645SJan.Kryl@Sun.COM 	 * If a lofs mount creates a mount loop (such that a lofs vfs is
286*12645SJan.Kryl@Sun.COM 	 * mounted on an autofs node and that lofs vfs points back to the
287*12645SJan.Kryl@Sun.COM 	 * autofs node which it is mounted on) then a VOP_ACCESS call will
288*12645SJan.Kryl@Sun.COM 	 * create a deadlock. Once this deadlock is released, VOP_ACCESS will
289*12645SJan.Kryl@Sun.COM 	 * return EINTR. In such a case we don't want the lofs vfs to be
290*12645SJan.Kryl@Sun.COM 	 * created as the loop could panic the system.
2910Sstevel@tonic-gate 	 */
292*12645SJan.Kryl@Sun.COM 	if ((error = VOP_ACCESS(realrootvp, 0, 0, cr, NULL)) != 0) {
293*12645SJan.Kryl@Sun.COM 		VN_RELE(realrootvp);
294*12645SJan.Kryl@Sun.COM 		return (error);
295*12645SJan.Kryl@Sun.COM 	}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/*
2980Sstevel@tonic-gate 	 * We're interested in the top most filesystem.
2990Sstevel@tonic-gate 	 * This is specially important when uap->spec is a trigger
3000Sstevel@tonic-gate 	 * AUTOFS node, since we're really interested in mounting the
3010Sstevel@tonic-gate 	 * filesystem AUTOFS mounted as result of the VOP_ACCESS()
3020Sstevel@tonic-gate 	 * call not the AUTOFS node itself.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 	if (vn_mountedvfs(realrootvp) != NULL) {
3050Sstevel@tonic-gate 		if (error = traverse(&realrootvp)) {
3060Sstevel@tonic-gate 			VN_RELE(realrootvp);
3070Sstevel@tonic-gate 			return (error);
3080Sstevel@tonic-gate 		}
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/*
3120Sstevel@tonic-gate 	 * Allocate a vfs info struct and attach it
3130Sstevel@tonic-gate 	 */
3140Sstevel@tonic-gate 	li = kmem_zalloc(sizeof (struct loinfo), KM_SLEEP);
3150Sstevel@tonic-gate 	li->li_realvfs = realrootvp->v_vfsp;
3160Sstevel@tonic-gate 	li->li_mountvfs = vfsp;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	/*
3190Sstevel@tonic-gate 	 * Set mount flags to be inherited by loopback vfs's
3200Sstevel@tonic-gate 	 */
3210Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
3220Sstevel@tonic-gate 		li->li_mflag |= VFS_RDONLY;
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
3250Sstevel@tonic-gate 		li->li_mflag |= (VFS_NOSETUID|VFS_NODEVICES);
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
3280Sstevel@tonic-gate 		li->li_mflag |= VFS_NODEVICES;
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
3310Sstevel@tonic-gate 		li->li_mflag |= VFS_NOSETUID;
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 	/*
3340Sstevel@tonic-gate 	 * Permissive flags are added to the "deny" bitmap.
3350Sstevel@tonic-gate 	 */
3360Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
3370Sstevel@tonic-gate 		li->li_dflag |= VFS_XATTR;
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
3400Sstevel@tonic-gate 		li->li_dflag |= VFS_NBMAND;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * Propagate inheritable mount flags from the real vfs.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_RDONLY) &&
3470Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_RO, NULL))
3480Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL,
3490Sstevel@tonic-gate 		    VFS_NODISPLAY);
3500Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_NOSETUID) &&
3510Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
3520Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NOSETUID, NULL,
3530Sstevel@tonic-gate 		    VFS_NODISPLAY);
3540Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_NODEVICES) &&
3550Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
3560Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NODEVICES, NULL,
3570Sstevel@tonic-gate 		    VFS_NODISPLAY);
3580Sstevel@tonic-gate 	/*
3590Sstevel@tonic-gate 	 * Permissive flags such as VFS_XATTR, as opposed to restrictive flags
3600Sstevel@tonic-gate 	 * such as VFS_RDONLY, are handled differently.  An explicit
3610Sstevel@tonic-gate 	 * MNTOPT_NOXATTR should override the underlying filesystem's VFS_XATTR.
3620Sstevel@tonic-gate 	 */
3630Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_XATTR) &&
3640Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL) &&
3650Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
3660Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_XATTR, NULL,
3670Sstevel@tonic-gate 		    VFS_NODISPLAY);
3680Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_NBMAND) &&
3690Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL) &&
3700Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL))
3710Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NBMAND, NULL,
3720Sstevel@tonic-gate 		    VFS_NODISPLAY);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	li->li_refct = 0;
3750Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)li;
3760Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
3770Sstevel@tonic-gate 	vfsp->vfs_fstype = lofsfstype;
3780Sstevel@tonic-gate 	vfsp->vfs_bsize = li->li_realvfs->vfs_bsize;
3790Sstevel@tonic-gate 
3802656Sszhou 	vfsp->vfs_dev = li->li_realvfs->vfs_dev;
3812656Sszhou 	vfsp->vfs_fsid.val[0] = li->li_realvfs->vfs_fsid.val[0];
3822656Sszhou 	vfsp->vfs_fsid.val[1] = li->li_realvfs->vfs_fsid.val[1];
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_LOFS_NOSUB, NULL)) {
3850Sstevel@tonic-gate 		li->li_flag |= LO_NOSUB;
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	/*
3896224Smarks 	 * Propagate any VFS features
3906224Smarks 	 */
3916224Smarks 
3926224Smarks 	vfs_propagate_features(li->li_realvfs, vfsp);
3936224Smarks 
3946224Smarks 	/*
3950Sstevel@tonic-gate 	 * Setup the hashtable. If the root of this mount isn't a directory,
3960Sstevel@tonic-gate 	 * there's no point in allocating a large hashtable. A table with one
3970Sstevel@tonic-gate 	 * bucket is sufficient.
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	if (realrootvp->v_type != VDIR)
4000Sstevel@tonic-gate 		lsetup(li, 1);
4010Sstevel@tonic-gate 	else
4020Sstevel@tonic-gate 		lsetup(li, 0);
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	/*
4050Sstevel@tonic-gate 	 * Make the root vnode
4060Sstevel@tonic-gate 	 */
407324Sowenr 	srootvp = makelonode(realrootvp, li, 0);
4080Sstevel@tonic-gate 	srootvp->v_flag |= VROOT;
4090Sstevel@tonic-gate 	li->li_rootvp = srootvp;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate #ifdef LODEBUG
4120Sstevel@tonic-gate 	lo_dprint(4, "lo_mount: vfs %p realvfs %p root %p realroot %p li %p\n",
4130Sstevel@tonic-gate 	    vfsp, li->li_realvfs, srootvp, realrootvp, li);
4140Sstevel@tonic-gate #endif
4150Sstevel@tonic-gate 	return (0);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate /*
4190Sstevel@tonic-gate  * Undo loopback mount
4200Sstevel@tonic-gate  */
4210Sstevel@tonic-gate static int
lo_unmount(struct vfs * vfsp,int flag,struct cred * cr)4220Sstevel@tonic-gate lo_unmount(struct vfs *vfsp, int flag, struct cred *cr)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	struct loinfo *li;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
4270Sstevel@tonic-gate 		return (EPERM);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	/*
4300Sstevel@tonic-gate 	 * Forced unmount is not supported by this file system
4310Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
4320Sstevel@tonic-gate 	 */
4330Sstevel@tonic-gate 	if (flag & MS_FORCE)
4340Sstevel@tonic-gate 		return (ENOTSUP);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	li = vtoli(vfsp);
4370Sstevel@tonic-gate #ifdef LODEBUG
4380Sstevel@tonic-gate 	lo_dprint(4, "lo_unmount(%p) li %p\n", vfsp, li);
4390Sstevel@tonic-gate #endif
4400Sstevel@tonic-gate 	if (li->li_refct != 1 || li->li_rootvp->v_count != 1) {
4410Sstevel@tonic-gate #ifdef LODEBUG
4420Sstevel@tonic-gate 		lo_dprint(4, "refct %d v_ct %d\n", li->li_refct,
4430Sstevel@tonic-gate 		    li->li_rootvp->v_count);
4440Sstevel@tonic-gate #endif
4450Sstevel@tonic-gate 		return (EBUSY);
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 	VN_RELE(li->li_rootvp);
4480Sstevel@tonic-gate 	return (0);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate /*
4520Sstevel@tonic-gate  * Find root of lofs mount.
4530Sstevel@tonic-gate  */
4540Sstevel@tonic-gate static int
lo_root(struct vfs * vfsp,struct vnode ** vpp)4550Sstevel@tonic-gate lo_root(struct vfs *vfsp, struct vnode **vpp)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 	*vpp = vtoli(vfsp)->li_rootvp;
4580Sstevel@tonic-gate #ifdef LODEBUG
4590Sstevel@tonic-gate 	lo_dprint(4, "lo_root(0x%p) = %p\n", vfsp, *vpp);
4600Sstevel@tonic-gate #endif
4610Sstevel@tonic-gate 	/*
4620Sstevel@tonic-gate 	 * If the root of the filesystem is a special file, return the specvp
4630Sstevel@tonic-gate 	 * version of the vnode. We don't save the specvp vnode in our
4640Sstevel@tonic-gate 	 * hashtable since that's exclusively for lnodes.
4650Sstevel@tonic-gate 	 */
4660Sstevel@tonic-gate 	if (IS_DEVVP(*vpp)) {
4670Sstevel@tonic-gate 		struct vnode *svp;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, kcred);
4700Sstevel@tonic-gate 		if (svp == NULL)
4710Sstevel@tonic-gate 			return (ENOSYS);
4720Sstevel@tonic-gate 		*vpp = svp;
4730Sstevel@tonic-gate 	} else {
4740Sstevel@tonic-gate 		VN_HOLD(*vpp);
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	return (0);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate /*
4810Sstevel@tonic-gate  * Get file system statistics.
4820Sstevel@tonic-gate  */
4830Sstevel@tonic-gate static int
lo_statvfs(register struct vfs * vfsp,struct statvfs64 * sbp)4840Sstevel@tonic-gate lo_statvfs(register struct vfs *vfsp, struct statvfs64 *sbp)
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	vnode_t *realrootvp;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate #ifdef LODEBUG
4890Sstevel@tonic-gate 	lo_dprint(4, "lostatvfs %p\n", vfsp);
4900Sstevel@tonic-gate #endif
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * Using realrootvp->v_vfsp (instead of the realvfsp that was
4930Sstevel@tonic-gate 	 * cached) is necessary to make lofs work woth forced UFS unmounts.
4940Sstevel@tonic-gate 	 * In the case of a forced unmount, UFS stores a set of dummy vfsops
4950Sstevel@tonic-gate 	 * in all the (i)vnodes in the filesystem. The dummy ops simply
4960Sstevel@tonic-gate 	 * returns back EIO.
4970Sstevel@tonic-gate 	 */
4980Sstevel@tonic-gate 	(void) lo_realvfs(vfsp, &realrootvp);
4990Sstevel@tonic-gate 	if (realrootvp != NULL)
5000Sstevel@tonic-gate 		return (VFS_STATVFS(realrootvp->v_vfsp, sbp));
5010Sstevel@tonic-gate 	else
5020Sstevel@tonic-gate 		return (EIO);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate /*
5060Sstevel@tonic-gate  * LOFS doesn't have any data or metadata to flush, pending I/O on the
5070Sstevel@tonic-gate  * underlying filesystem will be flushed when such filesystem is synched.
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate /* ARGSUSED */
5100Sstevel@tonic-gate static int
lo_sync(struct vfs * vfsp,short flag,struct cred * cr)5110Sstevel@tonic-gate lo_sync(struct vfs *vfsp,
5120Sstevel@tonic-gate 	short flag,
5130Sstevel@tonic-gate 	struct cred *cr)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate #ifdef LODEBUG
5160Sstevel@tonic-gate 	lo_dprint(4, "lo_sync: %p\n", vfsp);
5170Sstevel@tonic-gate #endif
5180Sstevel@tonic-gate 	return (0);
5190Sstevel@tonic-gate }
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate /*
5220Sstevel@tonic-gate  * Obtain the vnode from the underlying filesystem.
5230Sstevel@tonic-gate  */
5240Sstevel@tonic-gate static int
lo_vget(struct vfs * vfsp,struct vnode ** vpp,struct fid * fidp)5250Sstevel@tonic-gate lo_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
5260Sstevel@tonic-gate {
5270Sstevel@tonic-gate 	vnode_t *realrootvp;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate #ifdef LODEBUG
5300Sstevel@tonic-gate 	lo_dprint(4, "lo_vget: %p\n", vfsp);
5310Sstevel@tonic-gate #endif
5320Sstevel@tonic-gate 	(void) lo_realvfs(vfsp, &realrootvp);
5330Sstevel@tonic-gate 	if (realrootvp != NULL)
5340Sstevel@tonic-gate 		return (VFS_VGET(realrootvp->v_vfsp, vpp, fidp));
5350Sstevel@tonic-gate 	else
5360Sstevel@tonic-gate 		return (EIO);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate  * Free mount-specific data.
5410Sstevel@tonic-gate  */
5420Sstevel@tonic-gate static void
lo_freevfs(struct vfs * vfsp)5430Sstevel@tonic-gate lo_freevfs(struct vfs *vfsp)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	struct loinfo *li = vtoli(vfsp);
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	ldestroy(li);
5480Sstevel@tonic-gate 	kmem_free(li, sizeof (struct loinfo));
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate static int
lofsinit(int fstyp,char * name)5520Sstevel@tonic-gate lofsinit(int fstyp, char *name)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	static const fs_operation_def_t lo_vfsops_template[] = {
5553898Srsb 		VFSNAME_MOUNT,		{ .vfs_mount = lo_mount },
5563898Srsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = lo_unmount },
5573898Srsb 		VFSNAME_ROOT,		{ .vfs_root = lo_root },
5583898Srsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = lo_statvfs },
5593898Srsb 		VFSNAME_SYNC,		{ .vfs_sync = lo_sync },
5603898Srsb 		VFSNAME_VGET,		{ .vfs_vget = lo_vget },
5613898Srsb 		VFSNAME_FREEVFS,	{ .vfs_freevfs = lo_freevfs },
5623898Srsb 		NULL,			NULL
5630Sstevel@tonic-gate 	};
5640Sstevel@tonic-gate 	int error;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	error = vfs_setfsops(fstyp, lo_vfsops_template, &lo_vfsops);
5670Sstevel@tonic-gate 	if (error != 0) {
5680Sstevel@tonic-gate 		cmn_err(CE_WARN, "lofsinit: bad vfs ops template");
5690Sstevel@tonic-gate 		return (error);
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	error = vn_make_ops(name, lo_vnodeops_template, &lo_vnodeops);
5730Sstevel@tonic-gate 	if (error != 0) {
5740Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstyp);
5750Sstevel@tonic-gate 		cmn_err(CE_WARN, "lofsinit: bad vnode ops template");
5760Sstevel@tonic-gate 		return (error);
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	lofsfstype = fstyp;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	return (0);
5820Sstevel@tonic-gate }
583