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) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 267c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/param.h> 317c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 327c478bd9Sstevel@tonic-gate #include <sys/cred.h> 337c478bd9Sstevel@tonic-gate #include <sys/debug.h> 347c478bd9Sstevel@tonic-gate #include <sys/errno.h> 357c478bd9Sstevel@tonic-gate #include <sys/proc.h> 367c478bd9Sstevel@tonic-gate #include <sys/procfs.h> 377c478bd9Sstevel@tonic-gate #include <sys/stat.h> 387c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 407c478bd9Sstevel@tonic-gate #include <sys/systm.h> 417c478bd9Sstevel@tonic-gate #include <sys/zone.h> 427c478bd9Sstevel@tonic-gate #include <sys/var.h> 437c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 44aa59c4cbSrsb #include <sys/vfs_opreg.h> 457c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 467c478bd9Sstevel@tonic-gate #include <sys/mode.h> 477c478bd9Sstevel@tonic-gate #include <sys/signal.h> 487c478bd9Sstevel@tonic-gate #include <sys/user.h> 497c478bd9Sstevel@tonic-gate #include <sys/mount.h> 507c478bd9Sstevel@tonic-gate #include <sys/bitmap.h> 517c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 527c478bd9Sstevel@tonic-gate #include <sys/policy.h> 537c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h> 547c478bd9Sstevel@tonic-gate #include <fs/proc/prdata.h> 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate /* 577c478bd9Sstevel@tonic-gate * This is the loadable module wrapper. 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate static int prinit(); 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate static mntopts_t proc_mntopts = { 647c478bd9Sstevel@tonic-gate NULL, 657c478bd9Sstevel@tonic-gate 0 667c478bd9Sstevel@tonic-gate }; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static vfsdef_t vfw = { 697c478bd9Sstevel@tonic-gate VFSDEF_VERSION, 707c478bd9Sstevel@tonic-gate "proc", 717c478bd9Sstevel@tonic-gate prinit, 720fbb751dSJohn Levon VSW_HASPROTO|VSW_STATS|VSW_XID|VSW_ZMOUNT, 737c478bd9Sstevel@tonic-gate &proc_mntopts 747c478bd9Sstevel@tonic-gate }; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * Module linkage information for the kernel. 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate extern struct mod_ops mod_fsops; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate static struct modlfs modlfs = { 827c478bd9Sstevel@tonic-gate &mod_fsops, "filesystem for proc", &vfw 837c478bd9Sstevel@tonic-gate }; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 867c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modlfs, NULL 877c478bd9Sstevel@tonic-gate }; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate int 907c478bd9Sstevel@tonic-gate _init(void) 917c478bd9Sstevel@tonic-gate { 927c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate int 967c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 977c478bd9Sstevel@tonic-gate { 987c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * N.B. 1037c478bd9Sstevel@tonic-gate * No _fini routine. The module cannot be unloaded once loaded. 1047c478bd9Sstevel@tonic-gate * The NO_UNLOAD_STUB in modstubs.s must change if this module 1057c478bd9Sstevel@tonic-gate * is ever modified to become unloadable. 1067c478bd9Sstevel@tonic-gate */ 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate int nproc_highbit; /* highbit(v.v_nproc) */ 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate static int procfstype; 1117c478bd9Sstevel@tonic-gate static major_t procfs_major; 1127c478bd9Sstevel@tonic-gate static minor_t procfs_minor; 1137c478bd9Sstevel@tonic-gate static kmutex_t procfs_minor_lock; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate static kmutex_t pr_mount_lock; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * /proc VFS operations vector. 1197c478bd9Sstevel@tonic-gate */ 1207c478bd9Sstevel@tonic-gate static int prmount(), prunmount(), prroot(), prstatvfs(); 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate static void 1237c478bd9Sstevel@tonic-gate prinitrootnode(prnode_t *pnp, vfs_t *vfsp) 1247c478bd9Sstevel@tonic-gate { 1257c478bd9Sstevel@tonic-gate struct vnode *vp; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate bzero((caddr_t)pnp, sizeof (*pnp)); 1287c478bd9Sstevel@tonic-gate pnp->pr_vnode = vp = vn_alloc(KM_SLEEP); 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate mutex_init(&pnp->pr_mutex, NULL, MUTEX_DEFAULT, NULL); 1317c478bd9Sstevel@tonic-gate vp->v_flag = VROOT|VNOCACHE|VNOMAP|VNOSWAP|VNOMOUNT; 1327c478bd9Sstevel@tonic-gate VN_SET_VFS_TYPE_DEV(vp, vfsp, VDIR, 0); 1337c478bd9Sstevel@tonic-gate vn_setops(vp, prvnodeops); 1347c478bd9Sstevel@tonic-gate vp->v_data = (caddr_t)pnp; 1357c478bd9Sstevel@tonic-gate pnp->pr_type = PR_PROCDIR; 1367c478bd9Sstevel@tonic-gate pnp->pr_mode = 0555; /* read-search by everyone */ 1377c478bd9Sstevel@tonic-gate vn_exists(vp); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate static int 1417c478bd9Sstevel@tonic-gate prinit(int fstype, char *name) 1427c478bd9Sstevel@tonic-gate { 1437c478bd9Sstevel@tonic-gate static const fs_operation_def_t pr_vfsops_template[] = { 144aa59c4cbSrsb VFSNAME_MOUNT, { .vfs_mount = prmount }, 145aa59c4cbSrsb VFSNAME_UNMOUNT, { .vfs_unmount = prunmount }, 146aa59c4cbSrsb VFSNAME_ROOT, { .vfs_root = prroot }, 147aa59c4cbSrsb VFSNAME_STATVFS, { .vfs_statvfs = prstatvfs }, 1487c478bd9Sstevel@tonic-gate NULL, NULL 1497c478bd9Sstevel@tonic-gate }; 1507c478bd9Sstevel@tonic-gate extern const fs_operation_def_t pr_vnodeops_template[]; 1517c478bd9Sstevel@tonic-gate int error; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate nproc_highbit = highbit(v.v_proc); 1547c478bd9Sstevel@tonic-gate procfstype = fstype; 1557c478bd9Sstevel@tonic-gate ASSERT(procfstype != 0); 1567c478bd9Sstevel@tonic-gate /* 1577c478bd9Sstevel@tonic-gate * Associate VFS ops vector with this fstype. 1587c478bd9Sstevel@tonic-gate */ 1597c478bd9Sstevel@tonic-gate error = vfs_setfsops(fstype, pr_vfsops_template, NULL); 1607c478bd9Sstevel@tonic-gate if (error != 0) { 1617c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "prinit: bad vfs ops template"); 1627c478bd9Sstevel@tonic-gate return (error); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* 1667c478bd9Sstevel@tonic-gate * Set up vnode ops vector too. 1677c478bd9Sstevel@tonic-gate */ 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate error = vn_make_ops(name, pr_vnodeops_template, &prvnodeops); 1707c478bd9Sstevel@tonic-gate if (error != 0) { 1717c478bd9Sstevel@tonic-gate (void) vfs_freevfsops_by_type(fstype); 1727c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "prinit: bad vnode ops template"); 1737c478bd9Sstevel@tonic-gate return (error); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate /* 1777c478bd9Sstevel@tonic-gate * Assign a unique "device" number (used by stat(2)). 1787c478bd9Sstevel@tonic-gate */ 1797c478bd9Sstevel@tonic-gate if ((procfs_major = getudev()) == (major_t)-1) { 1807c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "prinit: can't get unique device number"); 1817c478bd9Sstevel@tonic-gate procfs_major = 0; 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate mutex_init(&pr_mount_lock, NULL, MUTEX_DEFAULT, NULL); 1847c478bd9Sstevel@tonic-gate mutex_init(&procfs_minor_lock, NULL, MUTEX_DEFAULT, NULL); 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate return (0); 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1907c478bd9Sstevel@tonic-gate static int 1917c478bd9Sstevel@tonic-gate prmount(struct vfs *vfsp, struct vnode *mvp, 1927c478bd9Sstevel@tonic-gate struct mounta *uap, struct cred *cr) 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate prnode_t *pnp; 1957c478bd9Sstevel@tonic-gate zone_t *zone = curproc->p_zone; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate if (secpolicy_fs_mount(cr, mvp, vfsp) != 0) 1987c478bd9Sstevel@tonic-gate return (EPERM); 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate if (mvp->v_type != VDIR) 2017c478bd9Sstevel@tonic-gate return (ENOTDIR); 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate if (zone == global_zone) { 2047c478bd9Sstevel@tonic-gate zone_t *mntzone; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt)); 2077c478bd9Sstevel@tonic-gate zone_rele(mntzone); 2087c478bd9Sstevel@tonic-gate if (zone != mntzone) 2097c478bd9Sstevel@tonic-gate return (EBUSY); 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate /* 2127c478bd9Sstevel@tonic-gate * Having the resource be anything but "proc" doesn't make sense 2137c478bd9Sstevel@tonic-gate */ 214*d7de0ceaSRobert Harris vfs_setresource(vfsp, "proc", 0); 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate pnp = kmem_alloc(sizeof (*pnp), KM_SLEEP); 2177c478bd9Sstevel@tonic-gate mutex_enter(&pr_mount_lock); 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate mutex_enter(&mvp->v_lock); 2207c478bd9Sstevel@tonic-gate if ((uap->flags & MS_OVERLAY) == 0 && 2217c478bd9Sstevel@tonic-gate (mvp->v_count > 1 || (mvp->v_flag & VROOT))) { 2227c478bd9Sstevel@tonic-gate mutex_exit(&mvp->v_lock); 2237c478bd9Sstevel@tonic-gate mutex_exit(&pr_mount_lock); 2247c478bd9Sstevel@tonic-gate kmem_free(pnp, sizeof (*pnp)); 2257c478bd9Sstevel@tonic-gate return (EBUSY); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate mutex_exit(&mvp->v_lock); 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate prinitrootnode(pnp, vfsp); 2307c478bd9Sstevel@tonic-gate vfsp->vfs_fstype = procfstype; 2317c478bd9Sstevel@tonic-gate vfsp->vfs_data = (caddr_t)pnp; 2327c478bd9Sstevel@tonic-gate vfsp->vfs_bsize = DEV_BSIZE; 2337c478bd9Sstevel@tonic-gate /* 2347c478bd9Sstevel@tonic-gate * find an available minor device number for this mount 2357c478bd9Sstevel@tonic-gate */ 2367c478bd9Sstevel@tonic-gate mutex_enter(&procfs_minor_lock); 2377c478bd9Sstevel@tonic-gate do { 2387c478bd9Sstevel@tonic-gate vfsp->vfs_dev = makedevice(procfs_major, procfs_minor); 2397c478bd9Sstevel@tonic-gate procfs_minor = (procfs_minor + 1) & L_MAXMIN32; 2407c478bd9Sstevel@tonic-gate } while (vfs_devismounted(vfsp->vfs_dev)); 2417c478bd9Sstevel@tonic-gate mutex_exit(&procfs_minor_lock); 2427c478bd9Sstevel@tonic-gate vfs_make_fsid(&vfsp->vfs_fsid, vfsp->vfs_dev, procfstype); 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate mutex_exit(&pr_mount_lock); 2457c478bd9Sstevel@tonic-gate return (0); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2497c478bd9Sstevel@tonic-gate static int 2507c478bd9Sstevel@tonic-gate prunmount(struct vfs *vfsp, int flag, struct cred *cr) 2517c478bd9Sstevel@tonic-gate { 2527c478bd9Sstevel@tonic-gate prnode_t *pnp = (prnode_t *)vfsp->vfs_data; 2537c478bd9Sstevel@tonic-gate vnode_t *vp = PTOV(pnp); 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate mutex_enter(&pr_mount_lock); 2567c478bd9Sstevel@tonic-gate if (secpolicy_fs_unmount(cr, vfsp) != 0) { 2577c478bd9Sstevel@tonic-gate mutex_exit(&pr_mount_lock); 2587c478bd9Sstevel@tonic-gate return (EPERM); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate /* 2627c478bd9Sstevel@tonic-gate * forced unmount is not supported by this file system 2637c478bd9Sstevel@tonic-gate * and thus, ENOTSUP, is being returned. 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate if (flag & MS_FORCE) { 2667c478bd9Sstevel@tonic-gate mutex_exit(&pr_mount_lock); 2677c478bd9Sstevel@tonic-gate return (ENOTSUP); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate /* 2717c478bd9Sstevel@tonic-gate * Ensure that no /proc vnodes are in use on this mount point. 2727c478bd9Sstevel@tonic-gate */ 2737c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock); 2747c478bd9Sstevel@tonic-gate if (vp->v_count > 1) { 2757c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 2767c478bd9Sstevel@tonic-gate mutex_exit(&pr_mount_lock); 2777c478bd9Sstevel@tonic-gate return (EBUSY); 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 2817c478bd9Sstevel@tonic-gate mutex_exit(&pr_mount_lock); 2827c478bd9Sstevel@tonic-gate vn_invalid(vp); 2837c478bd9Sstevel@tonic-gate vn_free(vp); 2847c478bd9Sstevel@tonic-gate kmem_free(pnp, sizeof (*pnp)); 2857c478bd9Sstevel@tonic-gate return (0); 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2897c478bd9Sstevel@tonic-gate static int 2907c478bd9Sstevel@tonic-gate prroot(struct vfs *vfsp, struct vnode **vpp) 2917c478bd9Sstevel@tonic-gate { 2927c478bd9Sstevel@tonic-gate vnode_t *vp = PTOV((prnode_t *)vfsp->vfs_data); 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate VN_HOLD(vp); 2957c478bd9Sstevel@tonic-gate *vpp = vp; 2967c478bd9Sstevel@tonic-gate return (0); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate static int 3007c478bd9Sstevel@tonic-gate prstatvfs(struct vfs *vfsp, struct statvfs64 *sp) 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate int n; 3037c478bd9Sstevel@tonic-gate dev32_t d32; 3047c478bd9Sstevel@tonic-gate extern uint_t nproc; 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate n = v.v_proc - nproc; 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate bzero((caddr_t)sp, sizeof (*sp)); 3097c478bd9Sstevel@tonic-gate sp->f_bsize = DEV_BSIZE; 3107c478bd9Sstevel@tonic-gate sp->f_frsize = DEV_BSIZE; 3117c478bd9Sstevel@tonic-gate sp->f_blocks = (fsblkcnt64_t)0; 3127c478bd9Sstevel@tonic-gate sp->f_bfree = (fsblkcnt64_t)0; 3137c478bd9Sstevel@tonic-gate sp->f_bavail = (fsblkcnt64_t)0; 3147c478bd9Sstevel@tonic-gate sp->f_files = (fsfilcnt64_t)v.v_proc + 2; 3157c478bd9Sstevel@tonic-gate sp->f_ffree = (fsfilcnt64_t)n; 3167c478bd9Sstevel@tonic-gate sp->f_favail = (fsfilcnt64_t)n; 3177c478bd9Sstevel@tonic-gate (void) cmpldev(&d32, vfsp->vfs_dev); 3187c478bd9Sstevel@tonic-gate sp->f_fsid = d32; 3197c478bd9Sstevel@tonic-gate (void) strcpy(sp->f_basetype, vfssw[procfstype].vsw_name); 3207c478bd9Sstevel@tonic-gate sp->f_flag = vf_to_stf(vfsp->vfs_flag); 3217c478bd9Sstevel@tonic-gate sp->f_namemax = 64; /* quite arbitrary */ 3227c478bd9Sstevel@tonic-gate bzero(sp->f_fstr, sizeof (sp->f_fstr)); 3237c478bd9Sstevel@tonic-gate (void) strcpy(sp->f_fstr, "/proc"); 3247c478bd9Sstevel@tonic-gate (void) strcpy(&sp->f_fstr[6], "/proc"); 3257c478bd9Sstevel@tonic-gate return (0); 3267c478bd9Sstevel@tonic-gate } 327