1 /* $NetBSD: fdesc_vfsops.c,v 1.73 2008/01/28 14:31:18 dholland 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.73 2008/01/28 14:31:18 dholland 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/malloc.h> 62 #include <sys/kauth.h> 63 64 #include <miscfs/genfs/genfs.h> 65 #include <miscfs/fdesc/fdesc.h> 66 67 VFS_PROTOS(fdesc); 68 69 /* 70 * Mount the per-process file descriptors (/dev/fd) 71 */ 72 int 73 fdesc_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 74 { 75 struct lwp *l = curlwp; 76 int error = 0; 77 struct fdescmount *fmp; 78 struct vnode *rvp; 79 80 if (mp->mnt_flag & MNT_GETARGS) { 81 *data_len = 0; 82 return 0; 83 } 84 /* 85 * Update is a no-op 86 */ 87 if (mp->mnt_flag & MNT_UPDATE) 88 return (EOPNOTSUPP); 89 90 error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp); 91 if (error) 92 return (error); 93 94 MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount), 95 M_UFSMNT, M_WAITOK); /* XXX */ 96 rvp->v_type = VDIR; 97 rvp->v_vflag |= VV_ROOT; 98 fmp->f_root = rvp; 99 mp->mnt_stat.f_namemax = MAXNAMLEN; 100 mp->mnt_flag |= MNT_LOCAL; 101 mp->mnt_data = fmp; 102 vfs_getnewfsid(mp); 103 104 error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE, 105 mp->mnt_op->vfs_name, mp, l); 106 VOP_UNLOCK(rvp, 0); 107 return error; 108 } 109 110 int 111 fdesc_start(struct mount *mp, int flags) 112 { 113 return (0); 114 } 115 116 int 117 fdesc_unmount(struct mount *mp, int mntflags) 118 { 119 int error; 120 int flags = 0; 121 struct vnode *rtvp = VFSTOFDESC(mp)->f_root; 122 123 if (mntflags & MNT_FORCE) 124 flags |= FORCECLOSE; 125 126 if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0) 127 return (EBUSY); 128 if ((error = vflush(mp, rtvp, flags)) != 0) 129 return (error); 130 131 /* 132 * Blow it away for future re-use 133 */ 134 vgone(rtvp); 135 /* 136 * Finally, throw away the fdescmount structure 137 */ 138 free(mp->mnt_data, M_UFSMNT); /* XXX */ 139 mp->mnt_data = 0; 140 141 return (0); 142 } 143 144 int 145 fdesc_root(mp, vpp) 146 struct mount *mp; 147 struct vnode **vpp; 148 { 149 struct vnode *vp; 150 151 /* 152 * Return locked reference to root. 153 */ 154 vp = VFSTOFDESC(mp)->f_root; 155 VREF(vp); 156 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 157 *vpp = vp; 158 return (0); 159 } 160 161 int 162 fdesc_statvfs(mp, sbp) 163 struct mount *mp; 164 struct statvfs *sbp; 165 { 166 struct lwp *l = curlwp; 167 struct filedesc *fdp; 168 struct proc *p; 169 int lim; 170 int i; 171 int last; 172 int freefd; 173 174 /* 175 * Compute number of free file descriptors. 176 * [ Strange results will ensue if the open file 177 * limit is ever reduced below the current number 178 * of open files... ] 179 */ 180 p = l->l_proc; 181 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur; 182 fdp = p->p_fd; 183 last = min(fdp->fd_nfiles, lim); 184 freefd = 0; 185 for (i = fdp->fd_freefile; i < last; i++) 186 if (fdp->fd_ofiles[i] == NULL) 187 freefd++; 188 189 /* 190 * Adjust for the fact that the fdesc array may not 191 * have been fully allocated yet. 192 */ 193 if (fdp->fd_nfiles < lim) 194 freefd += (lim - fdp->fd_nfiles); 195 196 sbp->f_bsize = DEV_BSIZE; 197 sbp->f_frsize = DEV_BSIZE; 198 sbp->f_iosize = DEV_BSIZE; 199 sbp->f_blocks = 2; /* 1K to keep df happy */ 200 sbp->f_bfree = 0; 201 sbp->f_bavail = 0; 202 sbp->f_bresvd = 0; 203 sbp->f_files = lim + 1; /* Allow for "." */ 204 sbp->f_ffree = freefd; /* See comments above */ 205 sbp->f_favail = freefd; /* See comments above */ 206 sbp->f_fresvd = 0; 207 copy_statvfs_info(sbp, mp); 208 return (0); 209 } 210 211 /*ARGSUSED*/ 212 int 213 fdesc_sync(struct mount *mp, int waitfor, 214 kauth_cred_t uc) 215 { 216 217 return (0); 218 } 219 220 /* 221 * Fdesc flat namespace lookup. 222 * Currently unsupported. 223 */ 224 int 225 fdesc_vget(struct mount *mp, ino_t ino, 226 struct vnode **vpp) 227 { 228 229 return (EOPNOTSUPP); 230 } 231 232 233 SYSCTL_SETUP(sysctl_vfs_fdesc_setup, "sysctl vfs.fdesc subtree setup") 234 { 235 236 sysctl_createv(clog, 0, NULL, NULL, 237 CTLFLAG_PERMANENT, 238 CTLTYPE_NODE, "vfs", NULL, 239 NULL, 0, NULL, 0, 240 CTL_VFS, CTL_EOL); 241 sysctl_createv(clog, 0, NULL, NULL, 242 CTLFLAG_PERMANENT, 243 CTLTYPE_NODE, "fdesc", 244 SYSCTL_DESCR("File-descriptor file system"), 245 NULL, 0, NULL, 0, 246 CTL_VFS, 7, CTL_EOL); 247 /* 248 * XXX the "7" above could be dynamic, thereby eliminating one 249 * more instance of the "number to vfs" mapping problem, but 250 * "7" is the order as taken from sys/mount.h 251 */ 252 } 253 254 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc; 255 256 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = { 257 &fdesc_vnodeop_opv_desc, 258 NULL, 259 }; 260 261 struct vfsops fdesc_vfsops = { 262 MOUNT_FDESC, 263 0, 264 fdesc_mount, 265 fdesc_start, 266 fdesc_unmount, 267 fdesc_root, 268 (void *)eopnotsupp, /* vfs_quotactl */ 269 fdesc_statvfs, 270 fdesc_sync, 271 fdesc_vget, 272 (void *)eopnotsupp, /* vfs_fhtovp */ 273 (void *)eopnotsupp, /* vfs_vptofh */ 274 fdesc_init, 275 NULL, 276 fdesc_done, 277 NULL, /* vfs_mountroot */ 278 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 279 vfs_stdextattrctl, 280 (void *)eopnotsupp, /* vfs_suspendctl */ 281 genfs_renamelock_enter, 282 genfs_renamelock_exit, 283 fdesc_vnodeopv_descs, 284 0, 285 { NULL, NULL}, 286 }; 287 VFS_ATTACH(fdesc_vfsops); 288