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