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 /*
220fbb751dSJohn Levon  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
267c478bd9Sstevel@tonic-gate #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
287c478bd9Sstevel@tonic-gate #include <sys/cred.h>
297c478bd9Sstevel@tonic-gate #include <sys/debug.h>
307c478bd9Sstevel@tonic-gate #include <sys/errno.h>
317c478bd9Sstevel@tonic-gate #include <sys/proc.h>
327c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/var.h>
387c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
39aa59c4cbSrsb #include <sys/vfs_opreg.h>
407c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
417c478bd9Sstevel@tonic-gate #include <sys/mode.h>
427c478bd9Sstevel@tonic-gate #include <sys/signal.h>
437c478bd9Sstevel@tonic-gate #include <sys/user.h>
447c478bd9Sstevel@tonic-gate #include <sys/mount.h>
457c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
467c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
477c478bd9Sstevel@tonic-gate #include <sys/policy.h>
487c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
497c478bd9Sstevel@tonic-gate #include <sys/fs/mntdata.h>
507c478bd9Sstevel@tonic-gate #include <sys/zone.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static int mntinit(int, char *);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static mntopts_t mnt_mntopts = {
607c478bd9Sstevel@tonic-gate 	0,
617c478bd9Sstevel@tonic-gate 	NULL
627c478bd9Sstevel@tonic-gate };
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static vfsdef_t vfw = {
657c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
667c478bd9Sstevel@tonic-gate 	"mntfs",
677c478bd9Sstevel@tonic-gate 	mntinit,
680fbb751dSJohn Levon 	VSW_HASPROTO|VSW_STATS|VSW_ZMOUNT,
697c478bd9Sstevel@tonic-gate 	&mnt_mntopts
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate extern struct mod_ops mod_fsops;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static struct modlfs modlfs = {
787c478bd9Sstevel@tonic-gate 	&mod_fsops, "mount information file system", &vfw
797c478bd9Sstevel@tonic-gate };
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
827c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
837c478bd9Sstevel@tonic-gate };
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate int
_init(void)867c478bd9Sstevel@tonic-gate _init(void)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)927c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * N.B.
997c478bd9Sstevel@tonic-gate  * No _fini routine. The module cannot be unloaded once loaded.
100*5d9d9091SRichard Lowe  * The NO_UNLOAD_STUB in modstubs.S must change if this module
1017c478bd9Sstevel@tonic-gate  * is ever modified to become unloadable.
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate 
104df2381bfSpraks extern int	mntfstype;
1057c478bd9Sstevel@tonic-gate static major_t	mnt_major;
1067c478bd9Sstevel@tonic-gate static minor_t	mnt_minor;
1077c478bd9Sstevel@tonic-gate static kmutex_t	mnt_minor_lock;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * /mnttab VFS operations vector.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate static int	mntmount(), mntunmount(), mntroot(), mntstatvfs();
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static void
mntinitrootnode(mntnode_t * mnp)115ffc349abSdm mntinitrootnode(mntnode_t *mnp)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	struct vnode *vp;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	bzero((caddr_t)mnp, sizeof (*mnp));
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	mnp->mnt_vnode = vn_alloc(KM_SLEEP);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	vp = MTOV(mnp);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	vp->v_flag = VROOT|VNOCACHE|VNOMAP|VNOSWAP|VNOMOUNT;
1267c478bd9Sstevel@tonic-gate 	vn_setops(vp, mntvnodeops);
1277c478bd9Sstevel@tonic-gate 	vp->v_type = VREG;
1287c478bd9Sstevel@tonic-gate 	vp->v_data = (caddr_t)mnp;
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static int
mntinit(int fstype,char * name)1327c478bd9Sstevel@tonic-gate mntinit(int fstype, char *name)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t mnt_vfsops_template[] = {
135aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .vfs_mount = mntmount },
136aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = mntunmount },
137aa59c4cbSrsb 		VFSNAME_ROOT,		{ .vfs_root = mntroot },
138aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = mntstatvfs },
139aa59c4cbSrsb 		NULL,			NULL
1407c478bd9Sstevel@tonic-gate 	};
1417c478bd9Sstevel@tonic-gate 	extern const fs_operation_def_t mnt_vnodeops_template[];
1427c478bd9Sstevel@tonic-gate 	int error;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	mntfstype = fstype;
1457c478bd9Sstevel@tonic-gate 	ASSERT(mntfstype != 0);
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * Associate VFS ops vector with this fstype.
1487c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, mnt_vfsops_template, NULL);
1507c478bd9Sstevel@tonic-gate 	if (error != 0) {
1517c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "mntinit: bad vfs ops template");
1527c478bd9Sstevel@tonic-gate 		return (error);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/* Vnode ops too. */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, mnt_vnodeops_template, &mntvnodeops);
1587c478bd9Sstevel@tonic-gate 	if (error != 0) {
1597c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
1607c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "mntinit: bad vnode ops template");
1617c478bd9Sstevel@tonic-gate 		return (error);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * Assign a unique "device" number (used by stat(2)).
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 	if ((mnt_major = getudev()) == (major_t)-1) {
1687c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "mntinit: can't get unique device number");
1697c478bd9Sstevel@tonic-gate 		mnt_major = 0;
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 	mutex_init(&mnt_minor_lock, NULL, MUTEX_DEFAULT, NULL);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	return (0);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /* ARGSUSED */
1777c478bd9Sstevel@tonic-gate static int
mntmount(struct vfs * vfsp,struct vnode * mvp,struct mounta * uap,struct cred * cr)1787c478bd9Sstevel@tonic-gate mntmount(struct vfs *vfsp, struct vnode *mvp,
1797c478bd9Sstevel@tonic-gate 	struct mounta *uap, struct cred *cr)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	mntdata_t *mnt;
1827c478bd9Sstevel@tonic-gate 	mntnode_t *mnp;
1837c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_mount(cr, mvp, vfsp) != 0)
1867c478bd9Sstevel@tonic-gate 		return (EPERM);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/*
1897c478bd9Sstevel@tonic-gate 	 * You can only mount mnttab in your current zone.
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	if (zone == global_zone) {
1927c478bd9Sstevel@tonic-gate 		zone_t *mntzone;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
1957c478bd9Sstevel@tonic-gate 		ASSERT(mntzone != NULL);
1967c478bd9Sstevel@tonic-gate 		zone_rele(mntzone);
1977c478bd9Sstevel@tonic-gate 		if (mntzone != zone)
1987c478bd9Sstevel@tonic-gate 			return (EBUSY);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * Having the resource be anything but "mnttab" doesn't make sense
2037c478bd9Sstevel@tonic-gate 	 */
204d7de0ceaSRobert Harris 	vfs_setresource(vfsp, "mnttab", 0);
2057c478bd9Sstevel@tonic-gate 
2065545576aSRobert Harris 	mnt = kmem_zalloc(sizeof (*mnt), KM_SLEEP);
2077c478bd9Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
2087c478bd9Sstevel@tonic-gate 	if ((uap->flags & MS_OVERLAY) == 0 &&
2097c478bd9Sstevel@tonic-gate 	    (mvp->v_count > 1 || (mvp->v_flag & VROOT))) {
2107c478bd9Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
2117c478bd9Sstevel@tonic-gate 		kmem_free(mnt, sizeof (*mnt));
2127c478bd9Sstevel@tonic-gate 		return (EBUSY);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
2157c478bd9Sstevel@tonic-gate 
216a19609f8Sjv 	zone_init_ref(&mnt->mnt_zone_ref);
217a19609f8Sjv 	zone_hold_ref(zone, &mnt->mnt_zone_ref, ZONE_REF_MNTFS);
2187c478bd9Sstevel@tonic-gate 	mnp = &mnt->mnt_node;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	vfsp->vfs_fstype = mntfstype;
2217c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)mnt;
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * find an available minor device number for this mount.
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	mutex_enter(&mnt_minor_lock);
2267c478bd9Sstevel@tonic-gate 	do {
2277c478bd9Sstevel@tonic-gate 		mnt_minor = (mnt_minor + 1) & L_MAXMIN32;
2287c478bd9Sstevel@tonic-gate 		vfsp->vfs_dev = makedevice(mnt_major, mnt_minor);
2297c478bd9Sstevel@tonic-gate 	} while (vfs_devismounted(vfsp->vfs_dev));
2307c478bd9Sstevel@tonic-gate 	mutex_exit(&mnt_minor_lock);
2317c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, vfsp->vfs_dev, mntfstype);
2327c478bd9Sstevel@tonic-gate 	vfsp->vfs_bsize = DEV_BSIZE;
233ffc349abSdm 	mntinitrootnode(mnp);
2347c478bd9Sstevel@tonic-gate 	MTOV(mnp)->v_vfsp = vfsp;
2357c478bd9Sstevel@tonic-gate 	mnp->mnt_mountvp = mvp;
2367c478bd9Sstevel@tonic-gate 	vn_exists(MTOV(mnp));
2377c478bd9Sstevel@tonic-gate 	return (0);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /* ARGSUSED */
2417c478bd9Sstevel@tonic-gate static int
mntunmount(struct vfs * vfsp,int flag,struct cred * cr)2427c478bd9Sstevel@tonic-gate mntunmount(struct vfs *vfsp, int flag, struct cred *cr)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	mntdata_t *mnt = (mntdata_t *)vfsp->vfs_data;
2457c478bd9Sstevel@tonic-gate 	vnode_t *vp = MTOV(&mnt->mnt_node);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
2487c478bd9Sstevel@tonic-gate 		return (EPERM);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/*
2517c478bd9Sstevel@tonic-gate 	 * Ensure that no /mnttab vnodes are in use on this mount point.
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
2547c478bd9Sstevel@tonic-gate 	if (vp->v_count > 1 || mnt->mnt_nopen > 0) {
2557c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
2567c478bd9Sstevel@tonic-gate 		return (EBUSY);
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
260a19609f8Sjv 	zone_rele_ref(&mnt->mnt_zone_ref, ZONE_REF_MNTFS);
2617c478bd9Sstevel@tonic-gate 	vn_invalid(vp);
2627c478bd9Sstevel@tonic-gate 	vn_free(vp);
2637c478bd9Sstevel@tonic-gate 	kmem_free(mnt, sizeof (*mnt));
2647c478bd9Sstevel@tonic-gate 	return (0);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /* ARGSUSED */
2687c478bd9Sstevel@tonic-gate static int
mntroot(struct vfs * vfsp,struct vnode ** vpp)2697c478bd9Sstevel@tonic-gate mntroot(struct vfs *vfsp, struct vnode **vpp)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	mntnode_t *mnp = &((mntdata_t *)vfsp->vfs_data)->mnt_node;
2727c478bd9Sstevel@tonic-gate 	struct vnode *vp = MTOV(mnp);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
2757c478bd9Sstevel@tonic-gate 	*vpp = vp;
2767c478bd9Sstevel@tonic-gate 	return (0);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate static int
mntstatvfs(struct vfs * vfsp,struct statvfs64 * sp)2807c478bd9Sstevel@tonic-gate mntstatvfs(struct vfs *vfsp, struct statvfs64 *sp)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	dev32_t d32;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	bzero((caddr_t)sp, sizeof (*sp));
2857c478bd9Sstevel@tonic-gate 	sp->f_bsize	= DEV_BSIZE;
2867c478bd9Sstevel@tonic-gate 	sp->f_frsize	= DEV_BSIZE;
2877c478bd9Sstevel@tonic-gate 	sp->f_blocks	= (fsblkcnt64_t)0;
2887c478bd9Sstevel@tonic-gate 	sp->f_bfree	= (fsblkcnt64_t)0;
2897c478bd9Sstevel@tonic-gate 	sp->f_bavail	= (fsblkcnt64_t)0;
2907c478bd9Sstevel@tonic-gate 	sp->f_files	= (fsfilcnt64_t)1;
2917c478bd9Sstevel@tonic-gate 	sp->f_ffree	= (fsfilcnt64_t)0;
2927c478bd9Sstevel@tonic-gate 	sp->f_favail	= (fsfilcnt64_t)0;
2937c478bd9Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
2947c478bd9Sstevel@tonic-gate 	sp->f_fsid	= d32;
2957c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->f_basetype, vfssw[mntfstype].vsw_name);
2967c478bd9Sstevel@tonic-gate 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
2977c478bd9Sstevel@tonic-gate 	sp->f_namemax = 64;		/* quite arbitrary */
2987c478bd9Sstevel@tonic-gate 	bzero(sp->f_fstr, sizeof (sp->f_fstr));
2997c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->f_fstr, "/mnttab");
3007c478bd9Sstevel@tonic-gate 	(void) strcpy(&sp->f_fstr[8], "/mnttab");
3017c478bd9Sstevel@tonic-gate 	return (0);
3027c478bd9Sstevel@tonic-gate }
303