1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/types.h> 29 #include <sys/param.h> 30 #include <sys/cmn_err.h> 31 #include <sys/cred.h> 32 #include <sys/debug.h> 33 #include <sys/errno.h> 34 #include <sys/proc.h> 35 #include <sys/procfs.h> 36 #include <sys/stat.h> 37 #include <sys/statvfs.h> 38 #include <sys/sysmacros.h> 39 #include <sys/systm.h> 40 #include <sys/var.h> 41 #include <sys/vfs.h> 42 #include <sys/vfs_opreg.h> 43 #include <sys/vnode.h> 44 #include <sys/mode.h> 45 #include <sys/signal.h> 46 #include <sys/user.h> 47 #include <sys/mount.h> 48 #include <sys/bitmap.h> 49 #include <sys/kmem.h> 50 #include <sys/policy.h> 51 #include <fs/fs_subr.h> 52 #include <sys/fs/mntdata.h> 53 #include <sys/zone.h> 54 55 /* 56 * This is the loadable module wrapper. 57 */ 58 #include <sys/modctl.h> 59 60 static int mntinit(int, char *); 61 62 static mntopts_t mnt_mntopts = { 63 0, 64 NULL 65 }; 66 67 static vfsdef_t vfw = { 68 VFSDEF_VERSION, 69 "mntfs", 70 mntinit, 71 VSW_HASPROTO|VSW_STATS, 72 &mnt_mntopts 73 }; 74 75 /* 76 * Module linkage information for the kernel. 77 */ 78 extern struct mod_ops mod_fsops; 79 80 static struct modlfs modlfs = { 81 &mod_fsops, "mount information file system", &vfw 82 }; 83 84 static struct modlinkage modlinkage = { 85 MODREV_1, (void *)&modlfs, NULL 86 }; 87 88 int 89 _init(void) 90 { 91 return (mod_install(&modlinkage)); 92 } 93 94 int 95 _info(struct modinfo *modinfop) 96 { 97 return (mod_info(&modlinkage, modinfop)); 98 } 99 100 /* 101 * N.B. 102 * No _fini routine. The module cannot be unloaded once loaded. 103 * The NO_UNLOAD_STUB in modstubs.s must change if this module 104 * is ever modified to become unloadable. 105 */ 106 107 extern int mntfstype; 108 static major_t mnt_major; 109 static minor_t mnt_minor; 110 static kmutex_t mnt_minor_lock; 111 112 extern struct vnode *mntdummyvp; 113 struct vnodeops *mntdummyvnodeops; 114 115 /* 116 * /mnttab VFS operations vector. 117 */ 118 static int mntmount(), mntunmount(), mntroot(), mntstatvfs(); 119 120 static void 121 mntinitrootnode(mntnode_t *mnp, struct vfs *vfsp) 122 { 123 struct vnode *vp; 124 125 bzero((caddr_t)mnp, sizeof (*mnp)); 126 127 mnp->mnt_vnode = vn_alloc(KM_SLEEP); 128 129 vp = MTOV(mnp); 130 131 vp->v_flag = VROOT|VNOCACHE|VNOMAP|VNOSWAP|VNOMOUNT; 132 vn_setops(vp, mntvnodeops); 133 vp->v_type = VREG; 134 vp->v_data = (caddr_t)mnp; 135 136 /* 137 * Only one global mntdummyvp is required, which 138 * needs to get created when the global zone(first zone) 139 * gets mounted. Since this module does not have a _fini routine, 140 * the mntdummyvp vnode does not have to be freed. 141 */ 142 if (mntdummyvp == NULL) { 143 struct vnode *tvp; 144 tvp = vn_alloc(KM_SLEEP); 145 tvp->v_flag = VNOMOUNT|VNOMAP|VNOSWAP|VNOCACHE; 146 vn_setops(tvp, mntdummyvnodeops); 147 tvp->v_type = VREG; 148 tvp->v_data = (caddr_t)mnp; 149 tvp->v_vfsp = vfsp; 150 mntdummyvp = tvp; 151 } 152 } 153 154 static int 155 mntinit(int fstype, char *name) 156 { 157 static const fs_operation_def_t mnt_vfsops_template[] = { 158 VFSNAME_MOUNT, { .vfs_mount = mntmount }, 159 VFSNAME_UNMOUNT, { .vfs_unmount = mntunmount }, 160 VFSNAME_ROOT, { .vfs_root = mntroot }, 161 VFSNAME_STATVFS, { .vfs_statvfs = mntstatvfs }, 162 NULL, NULL 163 }; 164 extern const fs_operation_def_t mnt_vnodeops_template[]; 165 extern const fs_operation_def_t mnt_dummyvnodeops_template[]; 166 int error; 167 168 mntfstype = fstype; 169 ASSERT(mntfstype != 0); 170 /* 171 * Associate VFS ops vector with this fstype. 172 */ 173 error = vfs_setfsops(fstype, mnt_vfsops_template, NULL); 174 if (error != 0) { 175 cmn_err(CE_WARN, "mntinit: bad vfs ops template"); 176 return (error); 177 } 178 179 /* Vnode ops too. */ 180 181 error = vn_make_ops(name, mnt_vnodeops_template, &mntvnodeops); 182 if (!error) { 183 error = vn_make_ops(name, mnt_dummyvnodeops_template, 184 &mntdummyvnodeops); 185 } 186 if (error != 0) { 187 (void) vfs_freevfsops_by_type(fstype); 188 cmn_err(CE_WARN, "mntinit: bad vnode ops template"); 189 return (error); 190 } 191 192 /* 193 * Assign a unique "device" number (used by stat(2)). 194 */ 195 if ((mnt_major = getudev()) == (major_t)-1) { 196 cmn_err(CE_WARN, "mntinit: can't get unique device number"); 197 mnt_major = 0; 198 } 199 mutex_init(&mnt_minor_lock, NULL, MUTEX_DEFAULT, NULL); 200 201 return (0); 202 } 203 204 /* ARGSUSED */ 205 static int 206 mntmount(struct vfs *vfsp, struct vnode *mvp, 207 struct mounta *uap, struct cred *cr) 208 { 209 mntdata_t *mnt; 210 mntnode_t *mnp; 211 zone_t *zone = curproc->p_zone; 212 213 if (secpolicy_fs_mount(cr, mvp, vfsp) != 0) 214 return (EPERM); 215 216 /* 217 * You can only mount mnttab in your current zone. 218 */ 219 if (zone == global_zone) { 220 zone_t *mntzone; 221 222 mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt)); 223 ASSERT(mntzone != NULL); 224 zone_rele(mntzone); 225 if (mntzone != zone) 226 return (EBUSY); 227 } 228 229 /* 230 * Having the resource be anything but "mnttab" doesn't make sense 231 */ 232 vfs_setresource(vfsp, "mnttab"); 233 234 mnt = kmem_alloc(sizeof (*mnt), KM_SLEEP); 235 mutex_enter(&mvp->v_lock); 236 if ((uap->flags & MS_OVERLAY) == 0 && 237 (mvp->v_count > 1 || (mvp->v_flag & VROOT))) { 238 mutex_exit(&mvp->v_lock); 239 kmem_free(mnt, sizeof (*mnt)); 240 return (EBUSY); 241 } 242 mutex_exit(&mvp->v_lock); 243 244 mnt->mnt_nopen = 0; 245 mnt->mnt_size = 0; 246 zone_hold(mnt->mnt_zone = zone); 247 mnp = &mnt->mnt_node; 248 249 vfsp->vfs_fstype = mntfstype; 250 vfsp->vfs_data = (caddr_t)mnt; 251 /* 252 * find an available minor device number for this mount. 253 */ 254 mutex_enter(&mnt_minor_lock); 255 do { 256 mnt_minor = (mnt_minor + 1) & L_MAXMIN32; 257 vfsp->vfs_dev = makedevice(mnt_major, mnt_minor); 258 } while (vfs_devismounted(vfsp->vfs_dev)); 259 mutex_exit(&mnt_minor_lock); 260 vfs_make_fsid(&vfsp->vfs_fsid, vfsp->vfs_dev, mntfstype); 261 vfsp->vfs_bsize = DEV_BSIZE; 262 mntinitrootnode(mnp, vfsp); 263 MTOV(mnp)->v_vfsp = vfsp; 264 mnp->mnt_mountvp = mvp; 265 vn_exists(MTOV(mnp)); 266 return (0); 267 } 268 269 /* ARGSUSED */ 270 static int 271 mntunmount(struct vfs *vfsp, int flag, struct cred *cr) 272 { 273 mntdata_t *mnt = (mntdata_t *)vfsp->vfs_data; 274 vnode_t *vp = MTOV(&mnt->mnt_node); 275 276 if (secpolicy_fs_unmount(cr, vfsp) != 0) 277 return (EPERM); 278 279 /* 280 * Ensure that no /mnttab vnodes are in use on this mount point. 281 */ 282 mutex_enter(&vp->v_lock); 283 if (vp->v_count > 1 || mnt->mnt_nopen > 0) { 284 mutex_exit(&vp->v_lock); 285 return (EBUSY); 286 } 287 288 mutex_exit(&vp->v_lock); 289 zone_rele(mnt->mnt_zone); 290 vn_invalid(vp); 291 vn_free(vp); 292 kmem_free(mnt, sizeof (*mnt)); 293 return (0); 294 } 295 296 /* ARGSUSED */ 297 static int 298 mntroot(struct vfs *vfsp, struct vnode **vpp) 299 { 300 mntnode_t *mnp = &((mntdata_t *)vfsp->vfs_data)->mnt_node; 301 struct vnode *vp = MTOV(mnp); 302 303 VN_HOLD(vp); 304 *vpp = vp; 305 return (0); 306 } 307 308 static int 309 mntstatvfs(struct vfs *vfsp, struct statvfs64 *sp) 310 { 311 dev32_t d32; 312 313 bzero((caddr_t)sp, sizeof (*sp)); 314 sp->f_bsize = DEV_BSIZE; 315 sp->f_frsize = DEV_BSIZE; 316 sp->f_blocks = (fsblkcnt64_t)0; 317 sp->f_bfree = (fsblkcnt64_t)0; 318 sp->f_bavail = (fsblkcnt64_t)0; 319 sp->f_files = (fsfilcnt64_t)1; 320 sp->f_ffree = (fsfilcnt64_t)0; 321 sp->f_favail = (fsfilcnt64_t)0; 322 (void) cmpldev(&d32, vfsp->vfs_dev); 323 sp->f_fsid = d32; 324 (void) strcpy(sp->f_basetype, vfssw[mntfstype].vsw_name); 325 sp->f_flag = vf_to_stf(vfsp->vfs_flag); 326 sp->f_namemax = 64; /* quite arbitrary */ 327 bzero(sp->f_fstr, sizeof (sp->f_fstr)); 328 (void) strcpy(sp->f_fstr, "/mnttab"); 329 (void) strcpy(&sp->f_fstr[8], "/mnttab"); 330 return (0); 331 } 332