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 /*
2212633Sjohn.levon@sun.com * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/cmn_err.h>
280Sstevel@tonic-gate #include <sys/cred.h>
290Sstevel@tonic-gate #include <sys/debug.h>
300Sstevel@tonic-gate #include <sys/errno.h>
310Sstevel@tonic-gate #include <sys/proc.h>
320Sstevel@tonic-gate #include <sys/procfs.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <sys/statvfs.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/var.h>
380Sstevel@tonic-gate #include <sys/vfs.h>
393898Srsb #include <sys/vfs_opreg.h>
400Sstevel@tonic-gate #include <sys/vnode.h>
410Sstevel@tonic-gate #include <sys/mode.h>
420Sstevel@tonic-gate #include <sys/signal.h>
430Sstevel@tonic-gate #include <sys/user.h>
440Sstevel@tonic-gate #include <sys/mount.h>
450Sstevel@tonic-gate #include <sys/bitmap.h>
460Sstevel@tonic-gate #include <sys/kmem.h>
470Sstevel@tonic-gate #include <sys/policy.h>
480Sstevel@tonic-gate #include <fs/fs_subr.h>
490Sstevel@tonic-gate #include <sys/fs/mntdata.h>
500Sstevel@tonic-gate #include <sys/zone.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate * This is the loadable module wrapper.
540Sstevel@tonic-gate */
550Sstevel@tonic-gate #include <sys/modctl.h>
560Sstevel@tonic-gate
570Sstevel@tonic-gate static int mntinit(int, char *);
580Sstevel@tonic-gate
590Sstevel@tonic-gate static mntopts_t mnt_mntopts = {
600Sstevel@tonic-gate 0,
610Sstevel@tonic-gate NULL
620Sstevel@tonic-gate };
630Sstevel@tonic-gate
640Sstevel@tonic-gate static vfsdef_t vfw = {
650Sstevel@tonic-gate VFSDEF_VERSION,
660Sstevel@tonic-gate "mntfs",
670Sstevel@tonic-gate mntinit,
6812633Sjohn.levon@sun.com VSW_HASPROTO|VSW_STATS|VSW_ZMOUNT,
690Sstevel@tonic-gate &mnt_mntopts
700Sstevel@tonic-gate };
710Sstevel@tonic-gate
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate * Module linkage information for the kernel.
740Sstevel@tonic-gate */
750Sstevel@tonic-gate extern struct mod_ops mod_fsops;
760Sstevel@tonic-gate
770Sstevel@tonic-gate static struct modlfs modlfs = {
780Sstevel@tonic-gate &mod_fsops, "mount information file system", &vfw
790Sstevel@tonic-gate };
800Sstevel@tonic-gate
810Sstevel@tonic-gate static struct modlinkage modlinkage = {
820Sstevel@tonic-gate MODREV_1, (void *)&modlfs, NULL
830Sstevel@tonic-gate };
840Sstevel@tonic-gate
850Sstevel@tonic-gate int
_init(void)860Sstevel@tonic-gate _init(void)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate return (mod_install(&modlinkage));
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate int
_info(struct modinfo * modinfop)920Sstevel@tonic-gate _info(struct modinfo *modinfop)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
950Sstevel@tonic-gate }
960Sstevel@tonic-gate
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate * N.B.
990Sstevel@tonic-gate * No _fini routine. The module cannot be unloaded once loaded.
1000Sstevel@tonic-gate * The NO_UNLOAD_STUB in modstubs.s must change if this module
1010Sstevel@tonic-gate * is ever modified to become unloadable.
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate
1044863Spraks extern int mntfstype;
1050Sstevel@tonic-gate static major_t mnt_major;
1060Sstevel@tonic-gate static minor_t mnt_minor;
1070Sstevel@tonic-gate static kmutex_t mnt_minor_lock;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate * /mnttab VFS operations vector.
1110Sstevel@tonic-gate */
1120Sstevel@tonic-gate static int mntmount(), mntunmount(), mntroot(), mntstatvfs();
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate static void
mntinitrootnode(mntnode_t * mnp)1154813Sdm120769 mntinitrootnode(mntnode_t *mnp)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate struct vnode *vp;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate bzero((caddr_t)mnp, sizeof (*mnp));
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate mnp->mnt_vnode = vn_alloc(KM_SLEEP);
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate vp = MTOV(mnp);
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate vp->v_flag = VROOT|VNOCACHE|VNOMAP|VNOSWAP|VNOMOUNT;
1260Sstevel@tonic-gate vn_setops(vp, mntvnodeops);
1270Sstevel@tonic-gate vp->v_type = VREG;
1280Sstevel@tonic-gate vp->v_data = (caddr_t)mnp;
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate static int
mntinit(int fstype,char * name)1320Sstevel@tonic-gate mntinit(int fstype, char *name)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate static const fs_operation_def_t mnt_vfsops_template[] = {
1353898Srsb VFSNAME_MOUNT, { .vfs_mount = mntmount },
1363898Srsb VFSNAME_UNMOUNT, { .vfs_unmount = mntunmount },
1373898Srsb VFSNAME_ROOT, { .vfs_root = mntroot },
1383898Srsb VFSNAME_STATVFS, { .vfs_statvfs = mntstatvfs },
1393898Srsb NULL, NULL
1400Sstevel@tonic-gate };
1410Sstevel@tonic-gate extern const fs_operation_def_t mnt_vnodeops_template[];
1420Sstevel@tonic-gate int error;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate mntfstype = fstype;
1450Sstevel@tonic-gate ASSERT(mntfstype != 0);
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate * Associate VFS ops vector with this fstype.
1480Sstevel@tonic-gate */
1490Sstevel@tonic-gate error = vfs_setfsops(fstype, mnt_vfsops_template, NULL);
1500Sstevel@tonic-gate if (error != 0) {
1510Sstevel@tonic-gate cmn_err(CE_WARN, "mntinit: bad vfs ops template");
1520Sstevel@tonic-gate return (error);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /* Vnode ops too. */
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate error = vn_make_ops(name, mnt_vnodeops_template, &mntvnodeops);
1580Sstevel@tonic-gate if (error != 0) {
1590Sstevel@tonic-gate (void) vfs_freevfsops_by_type(fstype);
1600Sstevel@tonic-gate cmn_err(CE_WARN, "mntinit: bad vnode ops template");
1610Sstevel@tonic-gate return (error);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate * Assign a unique "device" number (used by stat(2)).
1660Sstevel@tonic-gate */
1670Sstevel@tonic-gate if ((mnt_major = getudev()) == (major_t)-1) {
1680Sstevel@tonic-gate cmn_err(CE_WARN, "mntinit: can't get unique device number");
1690Sstevel@tonic-gate mnt_major = 0;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate mutex_init(&mnt_minor_lock, NULL, MUTEX_DEFAULT, NULL);
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate return (0);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /* ARGSUSED */
1770Sstevel@tonic-gate static int
mntmount(struct vfs * vfsp,struct vnode * mvp,struct mounta * uap,struct cred * cr)1780Sstevel@tonic-gate mntmount(struct vfs *vfsp, struct vnode *mvp,
1790Sstevel@tonic-gate struct mounta *uap, struct cred *cr)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate mntdata_t *mnt;
1820Sstevel@tonic-gate mntnode_t *mnp;
1830Sstevel@tonic-gate zone_t *zone = curproc->p_zone;
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate if (secpolicy_fs_mount(cr, mvp, vfsp) != 0)
1860Sstevel@tonic-gate return (EPERM);
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate * You can only mount mnttab in your current zone.
1900Sstevel@tonic-gate */
1910Sstevel@tonic-gate if (zone == global_zone) {
1920Sstevel@tonic-gate zone_t *mntzone;
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
1950Sstevel@tonic-gate ASSERT(mntzone != NULL);
1960Sstevel@tonic-gate zone_rele(mntzone);
1970Sstevel@tonic-gate if (mntzone != zone)
1980Sstevel@tonic-gate return (EBUSY);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate * Having the resource be anything but "mnttab" doesn't make sense
2030Sstevel@tonic-gate */
20412894SRobert.Harris@Sun.COM vfs_setresource(vfsp, "mnttab", 0);
2050Sstevel@tonic-gate
20611757SRobert.Harris@Sun.COM mnt = kmem_zalloc(sizeof (*mnt), KM_SLEEP);
2070Sstevel@tonic-gate mutex_enter(&mvp->v_lock);
2080Sstevel@tonic-gate if ((uap->flags & MS_OVERLAY) == 0 &&
2090Sstevel@tonic-gate (mvp->v_count > 1 || (mvp->v_flag & VROOT))) {
2100Sstevel@tonic-gate mutex_exit(&mvp->v_lock);
2110Sstevel@tonic-gate kmem_free(mnt, sizeof (*mnt));
2120Sstevel@tonic-gate return (EBUSY);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate mutex_exit(&mvp->v_lock);
2150Sstevel@tonic-gate
216*13096SJordan.Vaughan@Sun.com zone_init_ref(&mnt->mnt_zone_ref);
217*13096SJordan.Vaughan@Sun.com zone_hold_ref(zone, &mnt->mnt_zone_ref, ZONE_REF_MNTFS);
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
mntunmount(struct vfs * vfsp,int flag,struct cred * cr)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);
260*13096SJordan.Vaughan@Sun.com zone_rele_ref(&mnt->mnt_zone_ref, ZONE_REF_MNTFS);
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
mntroot(struct vfs * vfsp,struct vnode ** vpp)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
mntstatvfs(struct vfs * vfsp,struct statvfs64 * sp)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