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