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) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
260Sstevel@tonic-gate /* All Rights Reserved */
270Sstevel@tonic-gate
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #include <sys/cred.h>
330Sstevel@tonic-gate #include <sys/debug.h>
340Sstevel@tonic-gate #include <sys/errno.h>
350Sstevel@tonic-gate #include <sys/proc.h>
360Sstevel@tonic-gate #include <sys/procfs.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate #include <sys/statvfs.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/systm.h>
410Sstevel@tonic-gate #include <sys/zone.h>
420Sstevel@tonic-gate #include <sys/var.h>
430Sstevel@tonic-gate #include <sys/vfs.h>
443898Srsb #include <sys/vfs_opreg.h>
450Sstevel@tonic-gate #include <sys/vnode.h>
460Sstevel@tonic-gate #include <sys/mode.h>
470Sstevel@tonic-gate #include <sys/signal.h>
480Sstevel@tonic-gate #include <sys/user.h>
490Sstevel@tonic-gate #include <sys/mount.h>
500Sstevel@tonic-gate #include <sys/bitmap.h>
510Sstevel@tonic-gate #include <sys/kmem.h>
520Sstevel@tonic-gate #include <sys/policy.h>
530Sstevel@tonic-gate #include <fs/fs_subr.h>
540Sstevel@tonic-gate #include <fs/proc/prdata.h>
550Sstevel@tonic-gate
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * This is the loadable module wrapper.
580Sstevel@tonic-gate */
590Sstevel@tonic-gate #include <sys/modctl.h>
600Sstevel@tonic-gate
610Sstevel@tonic-gate static int prinit();
620Sstevel@tonic-gate
630Sstevel@tonic-gate static mntopts_t proc_mntopts = {
640Sstevel@tonic-gate NULL,
650Sstevel@tonic-gate 0
660Sstevel@tonic-gate };
670Sstevel@tonic-gate
680Sstevel@tonic-gate static vfsdef_t vfw = {
690Sstevel@tonic-gate VFSDEF_VERSION,
700Sstevel@tonic-gate "proc",
710Sstevel@tonic-gate prinit,
7212633Sjohn.levon@sun.com VSW_HASPROTO|VSW_STATS|VSW_XID|VSW_ZMOUNT,
730Sstevel@tonic-gate &proc_mntopts
740Sstevel@tonic-gate };
750Sstevel@tonic-gate
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate * Module linkage information for the kernel.
780Sstevel@tonic-gate */
790Sstevel@tonic-gate extern struct mod_ops mod_fsops;
800Sstevel@tonic-gate
810Sstevel@tonic-gate static struct modlfs modlfs = {
820Sstevel@tonic-gate &mod_fsops, "filesystem for proc", &vfw
830Sstevel@tonic-gate };
840Sstevel@tonic-gate
850Sstevel@tonic-gate static struct modlinkage modlinkage = {
860Sstevel@tonic-gate MODREV_1, (void *)&modlfs, NULL
870Sstevel@tonic-gate };
880Sstevel@tonic-gate
890Sstevel@tonic-gate int
_init(void)900Sstevel@tonic-gate _init(void)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate return (mod_install(&modlinkage));
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate int
_info(struct modinfo * modinfop)960Sstevel@tonic-gate _info(struct modinfo *modinfop)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate * N.B.
1030Sstevel@tonic-gate * No _fini routine. The module cannot be unloaded once loaded.
1040Sstevel@tonic-gate * The NO_UNLOAD_STUB in modstubs.s must change if this module
1050Sstevel@tonic-gate * is ever modified to become unloadable.
1060Sstevel@tonic-gate */
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate int nproc_highbit; /* highbit(v.v_nproc) */
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate static int procfstype;
1110Sstevel@tonic-gate static major_t procfs_major;
1120Sstevel@tonic-gate static minor_t procfs_minor;
1130Sstevel@tonic-gate static kmutex_t procfs_minor_lock;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate static kmutex_t pr_mount_lock;
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate * /proc VFS operations vector.
1190Sstevel@tonic-gate */
1200Sstevel@tonic-gate static int prmount(), prunmount(), prroot(), prstatvfs();
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate static void
prinitrootnode(prnode_t * pnp,vfs_t * vfsp)1230Sstevel@tonic-gate prinitrootnode(prnode_t *pnp, vfs_t *vfsp)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate struct vnode *vp;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate bzero((caddr_t)pnp, sizeof (*pnp));
1280Sstevel@tonic-gate pnp->pr_vnode = vp = vn_alloc(KM_SLEEP);
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate mutex_init(&pnp->pr_mutex, NULL, MUTEX_DEFAULT, NULL);
1310Sstevel@tonic-gate vp->v_flag = VROOT|VNOCACHE|VNOMAP|VNOSWAP|VNOMOUNT;
1320Sstevel@tonic-gate VN_SET_VFS_TYPE_DEV(vp, vfsp, VDIR, 0);
1330Sstevel@tonic-gate vn_setops(vp, prvnodeops);
1340Sstevel@tonic-gate vp->v_data = (caddr_t)pnp;
1350Sstevel@tonic-gate pnp->pr_type = PR_PROCDIR;
1360Sstevel@tonic-gate pnp->pr_mode = 0555; /* read-search by everyone */
1370Sstevel@tonic-gate vn_exists(vp);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate static int
prinit(int fstype,char * name)1410Sstevel@tonic-gate prinit(int fstype, char *name)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate static const fs_operation_def_t pr_vfsops_template[] = {
1443898Srsb VFSNAME_MOUNT, { .vfs_mount = prmount },
1453898Srsb VFSNAME_UNMOUNT, { .vfs_unmount = prunmount },
1463898Srsb VFSNAME_ROOT, { .vfs_root = prroot },
1473898Srsb VFSNAME_STATVFS, { .vfs_statvfs = prstatvfs },
1483898Srsb NULL, NULL
1490Sstevel@tonic-gate };
1500Sstevel@tonic-gate extern const fs_operation_def_t pr_vnodeops_template[];
1510Sstevel@tonic-gate int error;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate nproc_highbit = highbit(v.v_proc);
1540Sstevel@tonic-gate procfstype = fstype;
1550Sstevel@tonic-gate ASSERT(procfstype != 0);
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate * Associate VFS ops vector with this fstype.
1580Sstevel@tonic-gate */
1590Sstevel@tonic-gate error = vfs_setfsops(fstype, pr_vfsops_template, NULL);
1600Sstevel@tonic-gate if (error != 0) {
1610Sstevel@tonic-gate cmn_err(CE_WARN, "prinit: bad vfs ops template");
1620Sstevel@tonic-gate return (error);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate * Set up vnode ops vector too.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate error = vn_make_ops(name, pr_vnodeops_template, &prvnodeops);
1700Sstevel@tonic-gate if (error != 0) {
1710Sstevel@tonic-gate (void) vfs_freevfsops_by_type(fstype);
1720Sstevel@tonic-gate cmn_err(CE_WARN, "prinit: bad vnode ops template");
1730Sstevel@tonic-gate return (error);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * Assign a unique "device" number (used by stat(2)).
1780Sstevel@tonic-gate */
1790Sstevel@tonic-gate if ((procfs_major = getudev()) == (major_t)-1) {
1800Sstevel@tonic-gate cmn_err(CE_WARN, "prinit: can't get unique device number");
1810Sstevel@tonic-gate procfs_major = 0;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate mutex_init(&pr_mount_lock, NULL, MUTEX_DEFAULT, NULL);
1840Sstevel@tonic-gate mutex_init(&procfs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate return (0);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate /* ARGSUSED */
1900Sstevel@tonic-gate static int
prmount(struct vfs * vfsp,struct vnode * mvp,struct mounta * uap,struct cred * cr)1910Sstevel@tonic-gate prmount(struct vfs *vfsp, struct vnode *mvp,
1920Sstevel@tonic-gate struct mounta *uap, struct cred *cr)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate prnode_t *pnp;
1950Sstevel@tonic-gate zone_t *zone = curproc->p_zone;
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate if (secpolicy_fs_mount(cr, mvp, vfsp) != 0)
1980Sstevel@tonic-gate return (EPERM);
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate if (mvp->v_type != VDIR)
2010Sstevel@tonic-gate return (ENOTDIR);
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate if (zone == global_zone) {
2040Sstevel@tonic-gate zone_t *mntzone;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
2070Sstevel@tonic-gate zone_rele(mntzone);
2080Sstevel@tonic-gate if (zone != mntzone)
2090Sstevel@tonic-gate return (EBUSY);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate * Having the resource be anything but "proc" doesn't make sense
2130Sstevel@tonic-gate */
214*12894SRobert.Harris@Sun.COM vfs_setresource(vfsp, "proc", 0);
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate pnp = kmem_alloc(sizeof (*pnp), KM_SLEEP);
2170Sstevel@tonic-gate mutex_enter(&pr_mount_lock);
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate mutex_enter(&mvp->v_lock);
2200Sstevel@tonic-gate if ((uap->flags & MS_OVERLAY) == 0 &&
2210Sstevel@tonic-gate (mvp->v_count > 1 || (mvp->v_flag & VROOT))) {
2220Sstevel@tonic-gate mutex_exit(&mvp->v_lock);
2230Sstevel@tonic-gate mutex_exit(&pr_mount_lock);
2240Sstevel@tonic-gate kmem_free(pnp, sizeof (*pnp));
2250Sstevel@tonic-gate return (EBUSY);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate mutex_exit(&mvp->v_lock);
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate prinitrootnode(pnp, vfsp);
2300Sstevel@tonic-gate vfsp->vfs_fstype = procfstype;
2310Sstevel@tonic-gate vfsp->vfs_data = (caddr_t)pnp;
2320Sstevel@tonic-gate vfsp->vfs_bsize = DEV_BSIZE;
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate * find an available minor device number for this mount
2350Sstevel@tonic-gate */
2360Sstevel@tonic-gate mutex_enter(&procfs_minor_lock);
2370Sstevel@tonic-gate do {
2380Sstevel@tonic-gate vfsp->vfs_dev = makedevice(procfs_major, procfs_minor);
2390Sstevel@tonic-gate procfs_minor = (procfs_minor + 1) & L_MAXMIN32;
2400Sstevel@tonic-gate } while (vfs_devismounted(vfsp->vfs_dev));
2410Sstevel@tonic-gate mutex_exit(&procfs_minor_lock);
2420Sstevel@tonic-gate vfs_make_fsid(&vfsp->vfs_fsid, vfsp->vfs_dev, procfstype);
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate mutex_exit(&pr_mount_lock);
2450Sstevel@tonic-gate return (0);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate /* ARGSUSED */
2490Sstevel@tonic-gate static int
prunmount(struct vfs * vfsp,int flag,struct cred * cr)2500Sstevel@tonic-gate prunmount(struct vfs *vfsp, int flag, struct cred *cr)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate prnode_t *pnp = (prnode_t *)vfsp->vfs_data;
2530Sstevel@tonic-gate vnode_t *vp = PTOV(pnp);
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate mutex_enter(&pr_mount_lock);
2560Sstevel@tonic-gate if (secpolicy_fs_unmount(cr, vfsp) != 0) {
2570Sstevel@tonic-gate mutex_exit(&pr_mount_lock);
2580Sstevel@tonic-gate return (EPERM);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate * forced unmount is not supported by this file system
2630Sstevel@tonic-gate * and thus, ENOTSUP, is being returned.
2640Sstevel@tonic-gate */
2650Sstevel@tonic-gate if (flag & MS_FORCE) {
2660Sstevel@tonic-gate mutex_exit(&pr_mount_lock);
2670Sstevel@tonic-gate return (ENOTSUP);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate * Ensure that no /proc vnodes are in use on this mount point.
2720Sstevel@tonic-gate */
2730Sstevel@tonic-gate mutex_enter(&vp->v_lock);
2740Sstevel@tonic-gate if (vp->v_count > 1) {
2750Sstevel@tonic-gate mutex_exit(&vp->v_lock);
2760Sstevel@tonic-gate mutex_exit(&pr_mount_lock);
2770Sstevel@tonic-gate return (EBUSY);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate mutex_exit(&vp->v_lock);
2810Sstevel@tonic-gate mutex_exit(&pr_mount_lock);
2820Sstevel@tonic-gate vn_invalid(vp);
2830Sstevel@tonic-gate vn_free(vp);
2840Sstevel@tonic-gate kmem_free(pnp, sizeof (*pnp));
2850Sstevel@tonic-gate return (0);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate /* ARGSUSED */
2890Sstevel@tonic-gate static int
prroot(struct vfs * vfsp,struct vnode ** vpp)2900Sstevel@tonic-gate prroot(struct vfs *vfsp, struct vnode **vpp)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate vnode_t *vp = PTOV((prnode_t *)vfsp->vfs_data);
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate VN_HOLD(vp);
2950Sstevel@tonic-gate *vpp = vp;
2960Sstevel@tonic-gate return (0);
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate static int
prstatvfs(struct vfs * vfsp,struct statvfs64 * sp)3000Sstevel@tonic-gate prstatvfs(struct vfs *vfsp, struct statvfs64 *sp)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate int n;
3030Sstevel@tonic-gate dev32_t d32;
3040Sstevel@tonic-gate extern uint_t nproc;
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate n = v.v_proc - nproc;
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate bzero((caddr_t)sp, sizeof (*sp));
3090Sstevel@tonic-gate sp->f_bsize = DEV_BSIZE;
3100Sstevel@tonic-gate sp->f_frsize = DEV_BSIZE;
3110Sstevel@tonic-gate sp->f_blocks = (fsblkcnt64_t)0;
3120Sstevel@tonic-gate sp->f_bfree = (fsblkcnt64_t)0;
3130Sstevel@tonic-gate sp->f_bavail = (fsblkcnt64_t)0;
3140Sstevel@tonic-gate sp->f_files = (fsfilcnt64_t)v.v_proc + 2;
3150Sstevel@tonic-gate sp->f_ffree = (fsfilcnt64_t)n;
3160Sstevel@tonic-gate sp->f_favail = (fsfilcnt64_t)n;
3170Sstevel@tonic-gate (void) cmpldev(&d32, vfsp->vfs_dev);
3180Sstevel@tonic-gate sp->f_fsid = d32;
3190Sstevel@tonic-gate (void) strcpy(sp->f_basetype, vfssw[procfstype].vsw_name);
3200Sstevel@tonic-gate sp->f_flag = vf_to_stf(vfsp->vfs_flag);
3210Sstevel@tonic-gate sp->f_namemax = 64; /* quite arbitrary */
3220Sstevel@tonic-gate bzero(sp->f_fstr, sizeof (sp->f_fstr));
3230Sstevel@tonic-gate (void) strcpy(sp->f_fstr, "/proc");
3240Sstevel@tonic-gate (void) strcpy(&sp->f_fstr[6], "/proc");
3250Sstevel@tonic-gate return (0);
3260Sstevel@tonic-gate }
327