| 1 | /* $NetBSD: fdesc_vfsops.c,v 1.91 2014/11/09 18:08:06 maxv Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1992, 1993, 1995 |
| 5 | * The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software donated to Berkeley by |
| 8 | * Jan-Simon Pendry. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. Neither the name of the University nor the names of its contributors |
| 19 | * may be used to endorse or promote products derived from this software |
| 20 | * without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 | * SUCH DAMAGE. |
| 33 | * |
| 34 | * @(#)fdesc_vfsops.c 8.10 (Berkeley) 5/14/95 |
| 35 | * |
| 36 | * #Id: fdesc_vfsops.c,v 1.9 1993/04/06 15:28:33 jsp Exp # |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | * /dev/fd Filesystem |
| 41 | */ |
| 42 | |
| 43 | #include <sys/cdefs.h> |
| 44 | __KERNEL_RCSID(0, "$NetBSD: fdesc_vfsops.c,v 1.91 2014/11/09 18:08:06 maxv Exp $" ); |
| 45 | |
| 46 | #if defined(_KERNEL_OPT) |
| 47 | #include "opt_compat_netbsd.h" |
| 48 | #endif |
| 49 | |
| 50 | #include <sys/param.h> |
| 51 | #include <sys/systm.h> |
| 52 | #include <sys/sysctl.h> |
| 53 | #include <sys/time.h> |
| 54 | #include <sys/proc.h> |
| 55 | #include <sys/resourcevar.h> |
| 56 | #include <sys/filedesc.h> |
| 57 | #include <sys/vnode.h> |
| 58 | #include <sys/mount.h> |
| 59 | #include <sys/dirent.h> |
| 60 | #include <sys/namei.h> |
| 61 | #include <sys/kauth.h> |
| 62 | #include <sys/module.h> |
| 63 | |
| 64 | #include <miscfs/genfs/genfs.h> |
| 65 | #include <miscfs/fdesc/fdesc.h> |
| 66 | |
| 67 | MODULE(MODULE_CLASS_VFS, fdesc, NULL); |
| 68 | |
| 69 | VFS_PROTOS(fdesc); |
| 70 | |
| 71 | static struct sysctllog *fdesc_sysctl_log; |
| 72 | |
| 73 | /* |
| 74 | * Mount the per-process file descriptors (/dev/fd) |
| 75 | */ |
| 76 | int |
| 77 | fdesc_mount(struct mount *mp, const char *path, void *data, size_t *data_len) |
| 78 | { |
| 79 | struct lwp *l = curlwp; |
| 80 | int error = 0, ix; |
| 81 | struct vnode *rvp; |
| 82 | |
| 83 | if (mp->mnt_flag & MNT_GETARGS) { |
| 84 | *data_len = 0; |
| 85 | return 0; |
| 86 | } |
| 87 | /* |
| 88 | * Update is a no-op |
| 89 | */ |
| 90 | if (mp->mnt_flag & MNT_UPDATE) |
| 91 | return (EOPNOTSUPP); |
| 92 | |
| 93 | ix = FD_ROOT; |
| 94 | error = vcache_get(mp, &ix, sizeof(ix), &rvp); |
| 95 | if (error) |
| 96 | return error; |
| 97 | |
| 98 | mp->mnt_stat.f_namemax = FDESC_MAXNAMLEN; |
| 99 | mp->mnt_flag |= MNT_LOCAL; |
| 100 | mp->mnt_data = rvp; |
| 101 | vfs_getnewfsid(mp); |
| 102 | |
| 103 | error = set_statvfs_info(path, UIO_USERSPACE, "fdesc" , UIO_SYSSPACE, |
| 104 | mp->mnt_op->vfs_name, mp, l); |
| 105 | return error; |
| 106 | } |
| 107 | |
| 108 | int |
| 109 | fdesc_start(struct mount *mp, int flags) |
| 110 | { |
| 111 | return (0); |
| 112 | } |
| 113 | |
| 114 | int |
| 115 | fdesc_unmount(struct mount *mp, int mntflags) |
| 116 | { |
| 117 | int error; |
| 118 | int flags = 0; |
| 119 | struct vnode *rtvp = mp->mnt_data; |
| 120 | |
| 121 | if (mntflags & MNT_FORCE) |
| 122 | flags |= FORCECLOSE; |
| 123 | |
| 124 | if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0) |
| 125 | return (EBUSY); |
| 126 | if ((error = vflush(mp, rtvp, flags)) != 0) |
| 127 | return (error); |
| 128 | |
| 129 | /* |
| 130 | * Blow it away for future re-use |
| 131 | */ |
| 132 | vgone(rtvp); |
| 133 | mp->mnt_data = NULL; |
| 134 | |
| 135 | return (0); |
| 136 | } |
| 137 | |
| 138 | int |
| 139 | fdesc_root(struct mount *mp, struct vnode **vpp) |
| 140 | { |
| 141 | struct vnode *vp; |
| 142 | |
| 143 | /* |
| 144 | * Return locked reference to root. |
| 145 | */ |
| 146 | vp = mp->mnt_data; |
| 147 | vref(vp); |
| 148 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| 149 | *vpp = vp; |
| 150 | return (0); |
| 151 | } |
| 152 | |
| 153 | /*ARGSUSED*/ |
| 154 | int |
| 155 | fdesc_sync(struct mount *mp, int waitfor, |
| 156 | kauth_cred_t uc) |
| 157 | { |
| 158 | |
| 159 | return (0); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Fdesc flat namespace lookup. |
| 164 | * Currently unsupported. |
| 165 | */ |
| 166 | int |
| 167 | fdesc_vget(struct mount *mp, ino_t ino, |
| 168 | struct vnode **vpp) |
| 169 | { |
| 170 | |
| 171 | return (EOPNOTSUPP); |
| 172 | } |
| 173 | |
| 174 | int |
| 175 | fdesc_loadvnode(struct mount *mp, struct vnode *vp, |
| 176 | const void *key, size_t key_len, const void **new_key) |
| 177 | { |
| 178 | int ix; |
| 179 | struct fdescnode *fd; |
| 180 | |
| 181 | KASSERT(key_len == sizeof(ix)); |
| 182 | memcpy(&ix, key, key_len); |
| 183 | |
| 184 | fd = kmem_alloc(sizeof(struct fdescnode), KM_SLEEP); |
| 185 | fd->fd_fd = -1; |
| 186 | fd->fd_link = NULL; |
| 187 | fd->fd_ix = ix; |
| 188 | fd->fd_vnode = vp; |
| 189 | vp->v_tag = VT_FDESC; |
| 190 | vp->v_op = fdesc_vnodeop_p; |
| 191 | vp->v_data = fd; |
| 192 | switch (ix) { |
| 193 | case FD_ROOT: |
| 194 | fd->fd_type = Froot; |
| 195 | vp->v_type = VDIR; |
| 196 | vp->v_vflag |= VV_ROOT; |
| 197 | break; |
| 198 | case FD_DEVFD: |
| 199 | fd->fd_type = Fdevfd; |
| 200 | vp->v_type = VDIR; |
| 201 | break; |
| 202 | case FD_CTTY: |
| 203 | fd->fd_type = Fctty; |
| 204 | vp->v_type = VCHR; |
| 205 | break; |
| 206 | case FD_STDIN: |
| 207 | fd->fd_type = Flink; |
| 208 | fd->fd_link = "fd/0" ; |
| 209 | vp->v_type = VLNK; |
| 210 | break; |
| 211 | case FD_STDOUT: |
| 212 | fd->fd_type = Flink; |
| 213 | fd->fd_link = "fd/1" ; |
| 214 | vp->v_type = VLNK; |
| 215 | break; |
| 216 | case FD_STDERR: |
| 217 | fd->fd_type = Flink; |
| 218 | fd->fd_link = "fd/2" ; |
| 219 | vp->v_type = VLNK; |
| 220 | break; |
| 221 | default: |
| 222 | KASSERT(ix >= FD_DESC); |
| 223 | fd->fd_type = Fdesc; |
| 224 | fd->fd_fd = ix - FD_DESC; |
| 225 | vp->v_type = VNON; |
| 226 | break; |
| 227 | } |
| 228 | uvm_vnp_setsize(vp, 0); |
| 229 | *new_key = &fd->fd_ix; |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc; |
| 235 | |
| 236 | const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = { |
| 237 | &fdesc_vnodeop_opv_desc, |
| 238 | NULL, |
| 239 | }; |
| 240 | |
| 241 | struct vfsops fdesc_vfsops = { |
| 242 | .vfs_name = MOUNT_FDESC, |
| 243 | .vfs_min_mount_data = 0, |
| 244 | .vfs_mount = fdesc_mount, |
| 245 | .vfs_start = fdesc_start, |
| 246 | .vfs_unmount = fdesc_unmount, |
| 247 | .vfs_root = fdesc_root, |
| 248 | .vfs_quotactl = (void *)eopnotsupp, |
| 249 | .vfs_statvfs = genfs_statvfs, |
| 250 | .vfs_sync = fdesc_sync, |
| 251 | .vfs_vget = fdesc_vget, |
| 252 | .vfs_loadvnode = fdesc_loadvnode, |
| 253 | .vfs_fhtovp = (void *)eopnotsupp, |
| 254 | .vfs_vptofh = (void *)eopnotsupp, |
| 255 | .vfs_init = fdesc_init, |
| 256 | .vfs_done = fdesc_done, |
| 257 | .vfs_snapshot = (void *)eopnotsupp, |
| 258 | .vfs_extattrctl = vfs_stdextattrctl, |
| 259 | .vfs_suspendctl = (void *)eopnotsupp, |
| 260 | .vfs_renamelock_enter = genfs_renamelock_enter, |
| 261 | .vfs_renamelock_exit = genfs_renamelock_exit, |
| 262 | .vfs_fsync = (void *)eopnotsupp, |
| 263 | .vfs_opv_descs = fdesc_vnodeopv_descs |
| 264 | }; |
| 265 | |
| 266 | static int |
| 267 | fdesc_modcmd(modcmd_t cmd, void *arg) |
| 268 | { |
| 269 | int error; |
| 270 | |
| 271 | switch (cmd) { |
| 272 | case MODULE_CMD_INIT: |
| 273 | error = vfs_attach(&fdesc_vfsops); |
| 274 | if (error != 0) |
| 275 | break; |
| 276 | sysctl_createv(&fdesc_sysctl_log, 0, NULL, NULL, |
| 277 | CTLFLAG_PERMANENT, |
| 278 | CTLTYPE_NODE, "fdesc" , |
| 279 | SYSCTL_DESCR("File-descriptor file system" ), |
| 280 | NULL, 0, NULL, 0, |
| 281 | CTL_VFS, 7, CTL_EOL); |
| 282 | /* |
| 283 | * XXX the "7" above could be dynamic, thereby eliminating one |
| 284 | * more instance of the "number to vfs" mapping problem, but |
| 285 | * "7" is the order as taken from sys/mount.h |
| 286 | */ |
| 287 | break; |
| 288 | case MODULE_CMD_FINI: |
| 289 | error = vfs_detach(&fdesc_vfsops); |
| 290 | if (error != 0) |
| 291 | break; |
| 292 | sysctl_teardown(&fdesc_sysctl_log); |
| 293 | break; |
| 294 | default: |
| 295 | error = ENOTTY; |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | return (error); |
| 300 | } |
| 301 | |