xref: /onnv-gate/usr/src/uts/common/fs/lofs/lofs_vfsops.c (revision 6538:659156ccd03f)
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 /*
226224Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <sys/vfs.h>
313898Srsb #include <sys/vfs_opreg.h>
320Sstevel@tonic-gate #include <sys/vnode.h>
330Sstevel@tonic-gate #include <sys/uio.h>
340Sstevel@tonic-gate #include <sys/pathname.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/cred.h>
370Sstevel@tonic-gate #include <sys/statvfs.h>
380Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
390Sstevel@tonic-gate #include <sys/fs/lofs_node.h>
400Sstevel@tonic-gate #include <sys/mount.h>
410Sstevel@tonic-gate #include <sys/mntent.h>
420Sstevel@tonic-gate #include <sys/mkdev.h>
431676Sjpk #include <sys/priv.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate #include <sys/systm.h>
460Sstevel@tonic-gate #include <sys/cmn_err.h>
470Sstevel@tonic-gate #include <sys/policy.h>
481676Sjpk #include <sys/tsol/label.h>
490Sstevel@tonic-gate #include "fs/fs_subr.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * This is the loadable module wrapper.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate #include <sys/modctl.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static mntopts_t lofs_mntopts;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static int lofsinit(int, char *);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static vfsdef_t vfw = {
610Sstevel@tonic-gate 	VFSDEF_VERSION,
620Sstevel@tonic-gate 	"lofs",
630Sstevel@tonic-gate 	lofsinit,
641488Srsb 	VSW_HASPROTO|VSW_STATS,
650Sstevel@tonic-gate 	&lofs_mntopts
660Sstevel@tonic-gate };
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * LOFS mount options table
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
720Sstevel@tonic-gate static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
730Sstevel@tonic-gate static char *sub_cancel[] = { MNTOPT_LOFS_NOSUB, NULL };
740Sstevel@tonic-gate static char *nosub_cancel[] = { MNTOPT_LOFS_SUB, NULL };
750Sstevel@tonic-gate 
760Sstevel@tonic-gate static mntopt_t mntopts[] = {
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  *	option name		cancel option	default arg	flags
790Sstevel@tonic-gate  *		private data
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate 	{ MNTOPT_XATTR,		xattr_cancel,	NULL,		0,
820Sstevel@tonic-gate 		(void *)0 },
830Sstevel@tonic-gate 	{ MNTOPT_NOXATTR,	noxattr_cancel,	NULL,		0,
840Sstevel@tonic-gate 		(void *)0 },
850Sstevel@tonic-gate 	{ MNTOPT_LOFS_SUB,	sub_cancel,	NULL,		0,
860Sstevel@tonic-gate 		(void *)0 },
870Sstevel@tonic-gate 	{ MNTOPT_LOFS_NOSUB,	nosub_cancel,	NULL,		0,
880Sstevel@tonic-gate 		(void *)0 },
890Sstevel@tonic-gate };
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static mntopts_t lofs_mntopts = {
920Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
930Sstevel@tonic-gate 	mntopts
940Sstevel@tonic-gate };
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Module linkage information for the kernel.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static struct modlfs modlfs = {
1010Sstevel@tonic-gate 	&mod_fsops, "filesystem for lofs", &vfw
1020Sstevel@tonic-gate };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static struct modlinkage modlinkage = {
1050Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * This is the module initialization routine.
1100Sstevel@tonic-gate  */
1111676Sjpk 
1120Sstevel@tonic-gate int
1131676Sjpk _init(void)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	int status;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	lofs_subrinit();
1180Sstevel@tonic-gate 	status = mod_install(&modlinkage);
1190Sstevel@tonic-gate 	if (status != 0) {
1200Sstevel@tonic-gate 		/*
1210Sstevel@tonic-gate 		 * Cleanup previously initialized work.
1220Sstevel@tonic-gate 		 */
1230Sstevel@tonic-gate 		lofs_subrfini();
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	return (status);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * Don't allow the lofs module to be unloaded for now.
1310Sstevel@tonic-gate  * There is a memory leak if it gets unloaded.
1320Sstevel@tonic-gate  */
1331676Sjpk 
1340Sstevel@tonic-gate int
1351676Sjpk _fini(void)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	return (EBUSY);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate int
1410Sstevel@tonic-gate _info(struct modinfo *modinfop)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static int lofsfstype;
1480Sstevel@tonic-gate vfsops_t *lo_vfsops;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * lo mount vfsop
1520Sstevel@tonic-gate  * Set up mount info record and attach it to vfs struct.
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate /*ARGSUSED*/
1550Sstevel@tonic-gate static int
1560Sstevel@tonic-gate lo_mount(struct vfs *vfsp,
1570Sstevel@tonic-gate 	struct vnode *vp,
1580Sstevel@tonic-gate 	struct mounta *uap,
1590Sstevel@tonic-gate 	struct cred *cr)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	int error;
1620Sstevel@tonic-gate 	struct vnode *srootvp = NULL;	/* the server's root */
1630Sstevel@tonic-gate 	struct vnode *realrootvp;
1640Sstevel@tonic-gate 	struct loinfo *li;
1650Sstevel@tonic-gate 	int nodev;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	nodev = vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, vp, vfsp)) != 0)
1700Sstevel@tonic-gate 		return (EPERM);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/*
1730Sstevel@tonic-gate 	 * Loopback devices which get "nodevices" added can be done without
1740Sstevel@tonic-gate 	 * "nodevices" set because we cannot import devices into a zone
1750Sstevel@tonic-gate 	 * with loopback.  Note that we have all zone privileges when
1760Sstevel@tonic-gate 	 * this happens; if not, we'd have gotten "nosuid".
1770Sstevel@tonic-gate 	 */
1780Sstevel@tonic-gate 	if (!nodev && vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
1790Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_DEVICES, NULL, VFS_NODISPLAY);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
1820Sstevel@tonic-gate 	if (!(uap->flags & MS_OVERLAY) &&
1831676Sjpk 	    (vp->v_count != 1 || (vp->v_flag & VROOT))) {
1840Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
1850Sstevel@tonic-gate 		return (EBUSY);
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * Find real root, and make vfs point to real vfs
1910Sstevel@tonic-gate 	 */
1926224Smarks 
1930Sstevel@tonic-gate 	if (error = lookupname(uap->spec, (uap->flags & MS_SYSSPACE) ?
1946224Smarks 	    UIO_SYSSPACE : UIO_USERSPACE, FOLLOW, NULLVPP, &realrootvp))
1950Sstevel@tonic-gate 		return (error);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/*
1981676Sjpk 	 * Enforce MAC policy if needed.
1991676Sjpk 	 *
2001676Sjpk 	 * Loopback mounts must not allow writing up. The dominance test
2011676Sjpk 	 * is intended to prevent a global zone caller from accidentally
2021676Sjpk 	 * creating write-up conditions between two labeled zones.
2031676Sjpk 	 * Local zones can't violate MAC on their own without help from
2041676Sjpk 	 * the global zone because they can't name a pathname that
2051676Sjpk 	 * they don't already have.
2061676Sjpk 	 *
2071676Sjpk 	 * The special case check for the NET_MAC_AWARE process flag is
2081676Sjpk 	 * to support the case of the automounter in the global zone. We
2091676Sjpk 	 * permit automounting of local zone directories such as home
2101676Sjpk 	 * directories, into the global zone as required by setlabel,
2111676Sjpk 	 * zonecopy, and saving of desktop sessions. Such mounts are
2121676Sjpk 	 * trusted not to expose the contents of one zone's directories
2131676Sjpk 	 * to another by leaking them through the global zone.
2141676Sjpk 	 */
2151676Sjpk 	if (is_system_labeled() && crgetzoneid(cr) == GLOBAL_ZONEID) {
2161748Srica 		char	specname[MAXPATHLEN];
2171748Srica 		zone_t	*from_zptr;
2181748Srica 		zone_t	*to_zptr;
2191676Sjpk 
2201748Srica 		if (vnodetopath(NULL, realrootvp, specname,
221*6538Srica 		    sizeof (specname), CRED()) != 0) {
222*6538Srica 			VN_RELE(realrootvp);
2231748Srica 			return (EACCES);
224*6538Srica 		}
2251748Srica 
2261676Sjpk 		from_zptr = zone_find_by_path(specname);
2271676Sjpk 		to_zptr = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
2281676Sjpk 
2291676Sjpk 		/*
2301676Sjpk 		 * Special case for zone devfs: the zone for /dev will
2311676Sjpk 		 * incorrectly appear as the global zone since it's not
2321676Sjpk 		 * under the zone rootpath.  So for zone devfs check allow
2331676Sjpk 		 * read-write mounts.
2341769Scarlsonj 		 *
2351769Scarlsonj 		 * Second special case for scratch zones used for Live Upgrade:
2361769Scarlsonj 		 * this is used to mount the zone's root from /root to /a in
2371769Scarlsonj 		 * the scratch zone.  As with the other special case, this
2381769Scarlsonj 		 * appears to be outside of the zone because it's not under
2391769Scarlsonj 		 * the zone rootpath, which is $ZONEPATH/lu in the scratch
2401769Scarlsonj 		 * zone case.
2411676Sjpk 		 */
2421676Sjpk 
2432656Sszhou 		if (from_zptr != to_zptr &&
2441769Scarlsonj 		    !(to_zptr->zone_flags & ZF_IS_SCRATCH)) {
2451676Sjpk 			/*
2461676Sjpk 			 * We know at this point that the labels aren't equal
2471676Sjpk 			 * because the zone pointers aren't equal, and zones
2481676Sjpk 			 * can't share a label.
2491676Sjpk 			 *
2501676Sjpk 			 * If the source is the global zone then making
2511676Sjpk 			 * it available to a local zone must be done in
2521676Sjpk 			 * read-only mode as the label will become admin_low.
2531676Sjpk 			 *
2541676Sjpk 			 * If it is a mount between local zones then if
2551676Sjpk 			 * the current process is in the global zone and has
2561676Sjpk 			 * the NET_MAC_AWARE flag, then regular read-write
2571676Sjpk 			 * access is allowed.  If it's in some other zone, but
2581676Sjpk 			 * the label on the mount point dominates the original
2591676Sjpk 			 * source, then allow the mount as read-only
2601676Sjpk 			 * ("read-down").
2611676Sjpk 			 */
2621676Sjpk 			if (from_zptr->zone_id == GLOBAL_ZONEID) {
2631676Sjpk 				/* make the mount read-only */
2641676Sjpk 				vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
2651676Sjpk 			} else { /* cross-zone mount */
2661676Sjpk 				if (to_zptr->zone_id == GLOBAL_ZONEID &&
2671676Sjpk 				    /* LINTED: no consequent */
2681676Sjpk 				    getpflags(NET_MAC_AWARE, cr) != 0) {
2691676Sjpk 					/* Allow the mount as read-write */
2701676Sjpk 				} else if (bldominates(
2711676Sjpk 				    label2bslabel(to_zptr->zone_slabel),
2721676Sjpk 				    label2bslabel(from_zptr->zone_slabel))) {
2731676Sjpk 					/* make the mount read-only */
2741676Sjpk 					vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
2751676Sjpk 				} else {
276*6538Srica 					VN_RELE(realrootvp);
2771676Sjpk 					zone_rele(to_zptr);
2781676Sjpk 					zone_rele(from_zptr);
2791676Sjpk 					return (EACCES);
2801676Sjpk 				}
2811676Sjpk 			}
2821676Sjpk 		}
2831676Sjpk 		zone_rele(to_zptr);
2841676Sjpk 		zone_rele(from_zptr);
2851676Sjpk 	}
2861676Sjpk 
2871676Sjpk 	/*
2880Sstevel@tonic-gate 	 * realrootvp may be an AUTOFS node, in which case we
2890Sstevel@tonic-gate 	 * perform a VOP_ACCESS() to trigger the mount of the
2900Sstevel@tonic-gate 	 * intended filesystem, so we loopback mount the intended
2910Sstevel@tonic-gate 	 * filesystem instead of the AUTOFS filesystem.
2920Sstevel@tonic-gate 	 */
2935331Samw 	(void) VOP_ACCESS(realrootvp, 0, 0, cr, NULL);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/*
2960Sstevel@tonic-gate 	 * We're interested in the top most filesystem.
2970Sstevel@tonic-gate 	 * This is specially important when uap->spec is a trigger
2980Sstevel@tonic-gate 	 * AUTOFS node, since we're really interested in mounting the
2990Sstevel@tonic-gate 	 * filesystem AUTOFS mounted as result of the VOP_ACCESS()
3000Sstevel@tonic-gate 	 * call not the AUTOFS node itself.
3010Sstevel@tonic-gate 	 */
3020Sstevel@tonic-gate 	if (vn_mountedvfs(realrootvp) != NULL) {
3030Sstevel@tonic-gate 		if (error = traverse(&realrootvp)) {
3040Sstevel@tonic-gate 			VN_RELE(realrootvp);
3050Sstevel@tonic-gate 			return (error);
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	/*
3100Sstevel@tonic-gate 	 * Allocate a vfs info struct and attach it
3110Sstevel@tonic-gate 	 */
3120Sstevel@tonic-gate 	li = kmem_zalloc(sizeof (struct loinfo), KM_SLEEP);
3130Sstevel@tonic-gate 	li->li_realvfs = realrootvp->v_vfsp;
3140Sstevel@tonic-gate 	li->li_mountvfs = vfsp;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	/*
3170Sstevel@tonic-gate 	 * Set mount flags to be inherited by loopback vfs's
3180Sstevel@tonic-gate 	 */
3190Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
3200Sstevel@tonic-gate 		li->li_mflag |= VFS_RDONLY;
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
3230Sstevel@tonic-gate 		li->li_mflag |= (VFS_NOSETUID|VFS_NODEVICES);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
3260Sstevel@tonic-gate 		li->li_mflag |= VFS_NODEVICES;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
3290Sstevel@tonic-gate 		li->li_mflag |= VFS_NOSETUID;
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 	/*
3320Sstevel@tonic-gate 	 * Permissive flags are added to the "deny" bitmap.
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
3350Sstevel@tonic-gate 		li->li_dflag |= VFS_XATTR;
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
3380Sstevel@tonic-gate 		li->li_dflag |= VFS_NBMAND;
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/*
3420Sstevel@tonic-gate 	 * Propagate inheritable mount flags from the real vfs.
3430Sstevel@tonic-gate 	 */
3440Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_RDONLY) &&
3450Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_RO, NULL))
3460Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL,
3470Sstevel@tonic-gate 		    VFS_NODISPLAY);
3480Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_NOSETUID) &&
3490Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
3500Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NOSETUID, NULL,
3510Sstevel@tonic-gate 		    VFS_NODISPLAY);
3520Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_NODEVICES) &&
3530Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL))
3540Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NODEVICES, NULL,
3550Sstevel@tonic-gate 		    VFS_NODISPLAY);
3560Sstevel@tonic-gate 	/*
3570Sstevel@tonic-gate 	 * Permissive flags such as VFS_XATTR, as opposed to restrictive flags
3580Sstevel@tonic-gate 	 * such as VFS_RDONLY, are handled differently.  An explicit
3590Sstevel@tonic-gate 	 * MNTOPT_NOXATTR should override the underlying filesystem's VFS_XATTR.
3600Sstevel@tonic-gate 	 */
3610Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_XATTR) &&
3620Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL) &&
3630Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
3640Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_XATTR, NULL,
3650Sstevel@tonic-gate 		    VFS_NODISPLAY);
3660Sstevel@tonic-gate 	if ((li->li_realvfs->vfs_flag & VFS_NBMAND) &&
3670Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL) &&
3680Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL))
3690Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NBMAND, NULL,
3700Sstevel@tonic-gate 		    VFS_NODISPLAY);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	li->li_refct = 0;
3730Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)li;
3740Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
3750Sstevel@tonic-gate 	vfsp->vfs_fstype = lofsfstype;
3760Sstevel@tonic-gate 	vfsp->vfs_bsize = li->li_realvfs->vfs_bsize;
3770Sstevel@tonic-gate 
3782656Sszhou 	vfsp->vfs_dev = li->li_realvfs->vfs_dev;
3792656Sszhou 	vfsp->vfs_fsid.val[0] = li->li_realvfs->vfs_fsid.val[0];
3802656Sszhou 	vfsp->vfs_fsid.val[1] = li->li_realvfs->vfs_fsid.val[1];
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (vfs_optionisset(vfsp, MNTOPT_LOFS_NOSUB, NULL)) {
3830Sstevel@tonic-gate 		li->li_flag |= LO_NOSUB;
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	/*
3876224Smarks 	 * Propagate any VFS features
3886224Smarks 	 */
3896224Smarks 
3906224Smarks 	vfs_propagate_features(li->li_realvfs, vfsp);
3916224Smarks 
3926224Smarks 	/*
3930Sstevel@tonic-gate 	 * Setup the hashtable. If the root of this mount isn't a directory,
3940Sstevel@tonic-gate 	 * there's no point in allocating a large hashtable. A table with one
3950Sstevel@tonic-gate 	 * bucket is sufficient.
3960Sstevel@tonic-gate 	 */
3970Sstevel@tonic-gate 	if (realrootvp->v_type != VDIR)
3980Sstevel@tonic-gate 		lsetup(li, 1);
3990Sstevel@tonic-gate 	else
4000Sstevel@tonic-gate 		lsetup(li, 0);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	/*
4030Sstevel@tonic-gate 	 * Make the root vnode
4040Sstevel@tonic-gate 	 */
405324Sowenr 	srootvp = makelonode(realrootvp, li, 0);
4060Sstevel@tonic-gate 	srootvp->v_flag |= VROOT;
4070Sstevel@tonic-gate 	li->li_rootvp = srootvp;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate #ifdef LODEBUG
4100Sstevel@tonic-gate 	lo_dprint(4, "lo_mount: vfs %p realvfs %p root %p realroot %p li %p\n",
4110Sstevel@tonic-gate 	    vfsp, li->li_realvfs, srootvp, realrootvp, li);
4120Sstevel@tonic-gate #endif
4130Sstevel@tonic-gate 	return (0);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate  * Undo loopback mount
4180Sstevel@tonic-gate  */
4190Sstevel@tonic-gate static int
4200Sstevel@tonic-gate lo_unmount(struct vfs *vfsp, int flag, struct cred *cr)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	struct loinfo *li;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
4250Sstevel@tonic-gate 		return (EPERM);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * Forced unmount is not supported by this file system
4290Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
4300Sstevel@tonic-gate 	 */
4310Sstevel@tonic-gate 	if (flag & MS_FORCE)
4320Sstevel@tonic-gate 		return (ENOTSUP);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	li = vtoli(vfsp);
4350Sstevel@tonic-gate #ifdef LODEBUG
4360Sstevel@tonic-gate 	lo_dprint(4, "lo_unmount(%p) li %p\n", vfsp, li);
4370Sstevel@tonic-gate #endif
4380Sstevel@tonic-gate 	if (li->li_refct != 1 || li->li_rootvp->v_count != 1) {
4390Sstevel@tonic-gate #ifdef LODEBUG
4400Sstevel@tonic-gate 		lo_dprint(4, "refct %d v_ct %d\n", li->li_refct,
4410Sstevel@tonic-gate 		    li->li_rootvp->v_count);
4420Sstevel@tonic-gate #endif
4430Sstevel@tonic-gate 		return (EBUSY);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 	VN_RELE(li->li_rootvp);
4460Sstevel@tonic-gate 	return (0);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate /*
4500Sstevel@tonic-gate  * Find root of lofs mount.
4510Sstevel@tonic-gate  */
4520Sstevel@tonic-gate static int
4530Sstevel@tonic-gate lo_root(struct vfs *vfsp, struct vnode **vpp)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	*vpp = vtoli(vfsp)->li_rootvp;
4560Sstevel@tonic-gate #ifdef LODEBUG
4570Sstevel@tonic-gate 	lo_dprint(4, "lo_root(0x%p) = %p\n", vfsp, *vpp);
4580Sstevel@tonic-gate #endif
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * If the root of the filesystem is a special file, return the specvp
4610Sstevel@tonic-gate 	 * version of the vnode. We don't save the specvp vnode in our
4620Sstevel@tonic-gate 	 * hashtable since that's exclusively for lnodes.
4630Sstevel@tonic-gate 	 */
4640Sstevel@tonic-gate 	if (IS_DEVVP(*vpp)) {
4650Sstevel@tonic-gate 		struct vnode *svp;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, kcred);
4680Sstevel@tonic-gate 		if (svp == NULL)
4690Sstevel@tonic-gate 			return (ENOSYS);
4700Sstevel@tonic-gate 		*vpp = svp;
4710Sstevel@tonic-gate 	} else {
4720Sstevel@tonic-gate 		VN_HOLD(*vpp);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	return (0);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate /*
4790Sstevel@tonic-gate  * Get file system statistics.
4800Sstevel@tonic-gate  */
4810Sstevel@tonic-gate static int
4820Sstevel@tonic-gate lo_statvfs(register struct vfs *vfsp, struct statvfs64 *sbp)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	vnode_t *realrootvp;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate #ifdef LODEBUG
4870Sstevel@tonic-gate 	lo_dprint(4, "lostatvfs %p\n", vfsp);
4880Sstevel@tonic-gate #endif
4890Sstevel@tonic-gate 	/*
4900Sstevel@tonic-gate 	 * Using realrootvp->v_vfsp (instead of the realvfsp that was
4910Sstevel@tonic-gate 	 * cached) is necessary to make lofs work woth forced UFS unmounts.
4920Sstevel@tonic-gate 	 * In the case of a forced unmount, UFS stores a set of dummy vfsops
4930Sstevel@tonic-gate 	 * in all the (i)vnodes in the filesystem. The dummy ops simply
4940Sstevel@tonic-gate 	 * returns back EIO.
4950Sstevel@tonic-gate 	 */
4960Sstevel@tonic-gate 	(void) lo_realvfs(vfsp, &realrootvp);
4970Sstevel@tonic-gate 	if (realrootvp != NULL)
4980Sstevel@tonic-gate 		return (VFS_STATVFS(realrootvp->v_vfsp, sbp));
4990Sstevel@tonic-gate 	else
5000Sstevel@tonic-gate 		return (EIO);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate  * LOFS doesn't have any data or metadata to flush, pending I/O on the
5050Sstevel@tonic-gate  * underlying filesystem will be flushed when such filesystem is synched.
5060Sstevel@tonic-gate  */
5070Sstevel@tonic-gate /* ARGSUSED */
5080Sstevel@tonic-gate static int
5090Sstevel@tonic-gate lo_sync(struct vfs *vfsp,
5100Sstevel@tonic-gate 	short flag,
5110Sstevel@tonic-gate 	struct cred *cr)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate #ifdef LODEBUG
5140Sstevel@tonic-gate 	lo_dprint(4, "lo_sync: %p\n", vfsp);
5150Sstevel@tonic-gate #endif
5160Sstevel@tonic-gate 	return (0);
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate /*
5200Sstevel@tonic-gate  * Obtain the vnode from the underlying filesystem.
5210Sstevel@tonic-gate  */
5220Sstevel@tonic-gate static int
5230Sstevel@tonic-gate lo_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate 	vnode_t *realrootvp;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate #ifdef LODEBUG
5280Sstevel@tonic-gate 	lo_dprint(4, "lo_vget: %p\n", vfsp);
5290Sstevel@tonic-gate #endif
5300Sstevel@tonic-gate 	(void) lo_realvfs(vfsp, &realrootvp);
5310Sstevel@tonic-gate 	if (realrootvp != NULL)
5320Sstevel@tonic-gate 		return (VFS_VGET(realrootvp->v_vfsp, vpp, fidp));
5330Sstevel@tonic-gate 	else
5340Sstevel@tonic-gate 		return (EIO);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate  * Free mount-specific data.
5390Sstevel@tonic-gate  */
5400Sstevel@tonic-gate static void
5410Sstevel@tonic-gate lo_freevfs(struct vfs *vfsp)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	struct loinfo *li = vtoli(vfsp);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	ldestroy(li);
5460Sstevel@tonic-gate 	kmem_free(li, sizeof (struct loinfo));
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate static int
5500Sstevel@tonic-gate lofsinit(int fstyp, char *name)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	static const fs_operation_def_t lo_vfsops_template[] = {
5533898Srsb 		VFSNAME_MOUNT,		{ .vfs_mount = lo_mount },
5543898Srsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = lo_unmount },
5553898Srsb 		VFSNAME_ROOT,		{ .vfs_root = lo_root },
5563898Srsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = lo_statvfs },
5573898Srsb 		VFSNAME_SYNC,		{ .vfs_sync = lo_sync },
5583898Srsb 		VFSNAME_VGET,		{ .vfs_vget = lo_vget },
5593898Srsb 		VFSNAME_FREEVFS,	{ .vfs_freevfs = lo_freevfs },
5603898Srsb 		NULL,			NULL
5610Sstevel@tonic-gate 	};
5620Sstevel@tonic-gate 	int error;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	error = vfs_setfsops(fstyp, lo_vfsops_template, &lo_vfsops);
5650Sstevel@tonic-gate 	if (error != 0) {
5660Sstevel@tonic-gate 		cmn_err(CE_WARN, "lofsinit: bad vfs ops template");
5670Sstevel@tonic-gate 		return (error);
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	error = vn_make_ops(name, lo_vnodeops_template, &lo_vnodeops);
5710Sstevel@tonic-gate 	if (error != 0) {
5720Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstyp);
5730Sstevel@tonic-gate 		cmn_err(CE_WARN, "lofsinit: bad vnode ops template");
5740Sstevel@tonic-gate 		return (error);
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	lofsfstype = fstyp;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	return (0);
5800Sstevel@tonic-gate }
581