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