xref: /onnv-gate/usr/src/uts/common/fs/mntfs/mntvfsops.c (revision 11757:2efc2e154aeb)
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 /*
22*11757SRobert.Harris@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/cmn_err.h>
290Sstevel@tonic-gate #include <sys/cred.h>
300Sstevel@tonic-gate #include <sys/debug.h>
310Sstevel@tonic-gate #include <sys/errno.h>
320Sstevel@tonic-gate #include <sys/proc.h>
330Sstevel@tonic-gate #include <sys/procfs.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/statvfs.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/systm.h>
380Sstevel@tonic-gate #include <sys/var.h>
390Sstevel@tonic-gate #include <sys/vfs.h>
403898Srsb #include <sys/vfs_opreg.h>
410Sstevel@tonic-gate #include <sys/vnode.h>
420Sstevel@tonic-gate #include <sys/mode.h>
430Sstevel@tonic-gate #include <sys/signal.h>
440Sstevel@tonic-gate #include <sys/user.h>
450Sstevel@tonic-gate #include <sys/mount.h>
460Sstevel@tonic-gate #include <sys/bitmap.h>
470Sstevel@tonic-gate #include <sys/kmem.h>
480Sstevel@tonic-gate #include <sys/policy.h>
490Sstevel@tonic-gate #include <fs/fs_subr.h>
500Sstevel@tonic-gate #include <sys/fs/mntdata.h>
510Sstevel@tonic-gate #include <sys/zone.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * This is the loadable module wrapper.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate #include <sys/modctl.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static int mntinit(int, char *);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static mntopts_t mnt_mntopts = {
610Sstevel@tonic-gate 	0,
620Sstevel@tonic-gate 	NULL
630Sstevel@tonic-gate };
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static vfsdef_t vfw = {
660Sstevel@tonic-gate 	VFSDEF_VERSION,
670Sstevel@tonic-gate 	"mntfs",
680Sstevel@tonic-gate 	mntinit,
691488Srsb 	VSW_HASPROTO|VSW_STATS,
700Sstevel@tonic-gate 	&mnt_mntopts
710Sstevel@tonic-gate };
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Module linkage information for the kernel.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate extern struct mod_ops mod_fsops;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static struct modlfs modlfs = {
790Sstevel@tonic-gate 	&mod_fsops, "mount information file system", &vfw
800Sstevel@tonic-gate };
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static struct modlinkage modlinkage = {
830Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
840Sstevel@tonic-gate };
850Sstevel@tonic-gate 
860Sstevel@tonic-gate int
870Sstevel@tonic-gate _init(void)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	return (mod_install(&modlinkage));
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate int
930Sstevel@tonic-gate _info(struct modinfo *modinfop)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * N.B.
1000Sstevel@tonic-gate  * No _fini routine. The module cannot be unloaded once loaded.
1010Sstevel@tonic-gate  * The NO_UNLOAD_STUB in modstubs.s must change if this module
1020Sstevel@tonic-gate  * is ever modified to become unloadable.
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate 
1054863Spraks extern int	mntfstype;
1060Sstevel@tonic-gate static major_t	mnt_major;
1070Sstevel@tonic-gate static minor_t	mnt_minor;
1080Sstevel@tonic-gate static kmutex_t	mnt_minor_lock;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate  * /mnttab VFS operations vector.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate static int	mntmount(), mntunmount(), mntroot(), mntstatvfs();
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static void
1164813Sdm120769 mntinitrootnode(mntnode_t *mnp)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	struct vnode *vp;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	bzero((caddr_t)mnp, sizeof (*mnp));
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	mnp->mnt_vnode = vn_alloc(KM_SLEEP);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	vp = MTOV(mnp);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	vp->v_flag = VROOT|VNOCACHE|VNOMAP|VNOSWAP|VNOMOUNT;
1270Sstevel@tonic-gate 	vn_setops(vp, mntvnodeops);
1280Sstevel@tonic-gate 	vp->v_type = VREG;
1290Sstevel@tonic-gate 	vp->v_data = (caddr_t)mnp;
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate static int
1330Sstevel@tonic-gate mntinit(int fstype, char *name)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	static const fs_operation_def_t mnt_vfsops_template[] = {
1363898Srsb 		VFSNAME_MOUNT,		{ .vfs_mount = mntmount },
1373898Srsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = mntunmount },
1383898Srsb 		VFSNAME_ROOT,		{ .vfs_root = mntroot },
1393898Srsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = mntstatvfs },
1403898Srsb 		NULL,			NULL
1410Sstevel@tonic-gate 	};
1420Sstevel@tonic-gate 	extern const fs_operation_def_t mnt_vnodeops_template[];
1430Sstevel@tonic-gate 	int error;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	mntfstype = fstype;
1460Sstevel@tonic-gate 	ASSERT(mntfstype != 0);
1470Sstevel@tonic-gate 	/*
1480Sstevel@tonic-gate 	 * Associate VFS ops vector with this fstype.
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 	error = vfs_setfsops(fstype, mnt_vfsops_template, NULL);
1510Sstevel@tonic-gate 	if (error != 0) {
1520Sstevel@tonic-gate 		cmn_err(CE_WARN, "mntinit: bad vfs ops template");
1530Sstevel@tonic-gate 		return (error);
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	/* Vnode ops too. */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	error = vn_make_ops(name, mnt_vnodeops_template, &mntvnodeops);
1590Sstevel@tonic-gate 	if (error != 0) {
1600Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
1610Sstevel@tonic-gate 		cmn_err(CE_WARN, "mntinit: bad vnode ops template");
1620Sstevel@tonic-gate 		return (error);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/*
1660Sstevel@tonic-gate 	 * Assign a unique "device" number (used by stat(2)).
1670Sstevel@tonic-gate 	 */
1680Sstevel@tonic-gate 	if ((mnt_major = getudev()) == (major_t)-1) {
1690Sstevel@tonic-gate 		cmn_err(CE_WARN, "mntinit: can't get unique device number");
1700Sstevel@tonic-gate 		mnt_major = 0;
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 	mutex_init(&mnt_minor_lock, NULL, MUTEX_DEFAULT, NULL);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	return (0);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /* ARGSUSED */
1780Sstevel@tonic-gate static int
1790Sstevel@tonic-gate mntmount(struct vfs *vfsp, struct vnode *mvp,
1800Sstevel@tonic-gate 	struct mounta *uap, struct cred *cr)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	mntdata_t *mnt;
1830Sstevel@tonic-gate 	mntnode_t *mnp;
1840Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (secpolicy_fs_mount(cr, mvp, vfsp) != 0)
1870Sstevel@tonic-gate 		return (EPERM);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * You can only mount mnttab in your current zone.
1910Sstevel@tonic-gate 	 */
1920Sstevel@tonic-gate 	if (zone == global_zone) {
1930Sstevel@tonic-gate 		zone_t *mntzone;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
1960Sstevel@tonic-gate 		ASSERT(mntzone != NULL);
1970Sstevel@tonic-gate 		zone_rele(mntzone);
1980Sstevel@tonic-gate 		if (mntzone != zone)
1990Sstevel@tonic-gate 			return (EBUSY);
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * Having the resource be anything but "mnttab" doesn't make sense
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	vfs_setresource(vfsp, "mnttab");
2060Sstevel@tonic-gate 
207*11757SRobert.Harris@Sun.COM 	mnt = kmem_zalloc(sizeof (*mnt), KM_SLEEP);
2080Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
2090Sstevel@tonic-gate 	if ((uap->flags & MS_OVERLAY) == 0 &&
2100Sstevel@tonic-gate 	    (mvp->v_count > 1 || (mvp->v_flag & VROOT))) {
2110Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
2120Sstevel@tonic-gate 		kmem_free(mnt, sizeof (*mnt));
2130Sstevel@tonic-gate 		return (EBUSY);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	zone_hold(mnt->mnt_zone = zone);
2180Sstevel@tonic-gate 	mnp = &mnt->mnt_node;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	vfsp->vfs_fstype = mntfstype;
2210Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)mnt;
2220Sstevel@tonic-gate 	/*
2230Sstevel@tonic-gate 	 * find an available minor device number for this mount.
2240Sstevel@tonic-gate 	 */
2250Sstevel@tonic-gate 	mutex_enter(&mnt_minor_lock);
2260Sstevel@tonic-gate 	do {
2270Sstevel@tonic-gate 		mnt_minor = (mnt_minor + 1) & L_MAXMIN32;
2280Sstevel@tonic-gate 		vfsp->vfs_dev = makedevice(mnt_major, mnt_minor);
2290Sstevel@tonic-gate 	} while (vfs_devismounted(vfsp->vfs_dev));
2300Sstevel@tonic-gate 	mutex_exit(&mnt_minor_lock);
2310Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, vfsp->vfs_dev, mntfstype);
2320Sstevel@tonic-gate 	vfsp->vfs_bsize = DEV_BSIZE;
2334813Sdm120769 	mntinitrootnode(mnp);
2340Sstevel@tonic-gate 	MTOV(mnp)->v_vfsp = vfsp;
2350Sstevel@tonic-gate 	mnp->mnt_mountvp = mvp;
2360Sstevel@tonic-gate 	vn_exists(MTOV(mnp));
2370Sstevel@tonic-gate 	return (0);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /* ARGSUSED */
2410Sstevel@tonic-gate static int
2420Sstevel@tonic-gate mntunmount(struct vfs *vfsp, int flag, struct cred *cr)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	mntdata_t *mnt = (mntdata_t *)vfsp->vfs_data;
2450Sstevel@tonic-gate 	vnode_t *vp = MTOV(&mnt->mnt_node);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
2480Sstevel@tonic-gate 		return (EPERM);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	/*
2510Sstevel@tonic-gate 	 * Ensure that no /mnttab vnodes are in use on this mount point.
2520Sstevel@tonic-gate 	 */
2530Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
2540Sstevel@tonic-gate 	if (vp->v_count > 1 || mnt->mnt_nopen > 0) {
2550Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
2560Sstevel@tonic-gate 		return (EBUSY);
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
2600Sstevel@tonic-gate 	zone_rele(mnt->mnt_zone);
2610Sstevel@tonic-gate 	vn_invalid(vp);
2620Sstevel@tonic-gate 	vn_free(vp);
2630Sstevel@tonic-gate 	kmem_free(mnt, sizeof (*mnt));
2640Sstevel@tonic-gate 	return (0);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /* ARGSUSED */
2680Sstevel@tonic-gate static int
2690Sstevel@tonic-gate mntroot(struct vfs *vfsp, struct vnode **vpp)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	mntnode_t *mnp = &((mntdata_t *)vfsp->vfs_data)->mnt_node;
2720Sstevel@tonic-gate 	struct vnode *vp = MTOV(mnp);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	VN_HOLD(vp);
2750Sstevel@tonic-gate 	*vpp = vp;
2760Sstevel@tonic-gate 	return (0);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate static int
2800Sstevel@tonic-gate mntstatvfs(struct vfs *vfsp, struct statvfs64 *sp)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	dev32_t d32;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	bzero((caddr_t)sp, sizeof (*sp));
2850Sstevel@tonic-gate 	sp->f_bsize	= DEV_BSIZE;
2860Sstevel@tonic-gate 	sp->f_frsize	= DEV_BSIZE;
2870Sstevel@tonic-gate 	sp->f_blocks	= (fsblkcnt64_t)0;
2880Sstevel@tonic-gate 	sp->f_bfree	= (fsblkcnt64_t)0;
2890Sstevel@tonic-gate 	sp->f_bavail	= (fsblkcnt64_t)0;
2900Sstevel@tonic-gate 	sp->f_files	= (fsfilcnt64_t)1;
2910Sstevel@tonic-gate 	sp->f_ffree	= (fsfilcnt64_t)0;
2920Sstevel@tonic-gate 	sp->f_favail	= (fsfilcnt64_t)0;
2930Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
2940Sstevel@tonic-gate 	sp->f_fsid	= d32;
2950Sstevel@tonic-gate 	(void) strcpy(sp->f_basetype, vfssw[mntfstype].vsw_name);
2960Sstevel@tonic-gate 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
2970Sstevel@tonic-gate 	sp->f_namemax = 64;		/* quite arbitrary */
2980Sstevel@tonic-gate 	bzero(sp->f_fstr, sizeof (sp->f_fstr));
2990Sstevel@tonic-gate 	(void) strcpy(sp->f_fstr, "/mnttab");
3000Sstevel@tonic-gate 	(void) strcpy(&sp->f_fstr[8], "/mnttab");
3010Sstevel@tonic-gate 	return (0);
3020Sstevel@tonic-gate }
303