1 /* $NetBSD: fdesc_vfsops.c,v 1.52 2004/09/13 19:19:44 jdolecek 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.52 2004/09/13 19:19:44 jdolecek 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_stat.f_namemax = MAXNAMLEN; 111 mp->mnt_flag |= MNT_LOCAL; 112 mp->mnt_data = fmp; 113 vfs_getnewfsid(mp); 114 115 error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE, 116 mp, p); 117 VOP_UNLOCK(rvp, 0); 118 return error; 119 } 120 121 int 122 fdesc_start(mp, flags, p) 123 struct mount *mp; 124 int flags; 125 struct proc *p; 126 { 127 return (0); 128 } 129 130 int 131 fdesc_unmount(mp, mntflags, p) 132 struct mount *mp; 133 int mntflags; 134 struct proc *p; 135 { 136 int error; 137 int flags = 0; 138 struct vnode *rootvp = VFSTOFDESC(mp)->f_root; 139 140 if (mntflags & MNT_FORCE) 141 flags |= FORCECLOSE; 142 143 /* 144 * Clear out buffer cache. I don't think we 145 * ever get anything cached at this level at the 146 * moment, but who knows... 147 */ 148 if (rootvp->v_usecount > 1) 149 return (EBUSY); 150 if ((error = vflush(mp, rootvp, flags)) != 0) 151 return (error); 152 153 /* 154 * Release reference on underlying root vnode 155 */ 156 vrele(rootvp); 157 /* 158 * And blow it away for future re-use 159 */ 160 vgone(rootvp); 161 /* 162 * Finally, throw away the fdescmount structure 163 */ 164 free(mp->mnt_data, M_UFSMNT); /* XXX */ 165 mp->mnt_data = 0; 166 167 return (0); 168 } 169 170 int 171 fdesc_root(mp, vpp) 172 struct mount *mp; 173 struct vnode **vpp; 174 { 175 struct vnode *vp; 176 177 /* 178 * Return locked reference to root. 179 */ 180 vp = VFSTOFDESC(mp)->f_root; 181 VREF(vp); 182 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 183 *vpp = vp; 184 return (0); 185 } 186 187 int 188 fdesc_quotactl(mp, cmd, uid, arg, p) 189 struct mount *mp; 190 int cmd; 191 uid_t uid; 192 void *arg; 193 struct proc *p; 194 { 195 196 return (EOPNOTSUPP); 197 } 198 199 int 200 fdesc_statvfs(mp, sbp, p) 201 struct mount *mp; 202 struct statvfs *sbp; 203 struct proc *p; 204 { 205 struct filedesc *fdp; 206 int lim; 207 int i; 208 int last; 209 int freefd; 210 211 /* 212 * Compute number of free file descriptors. 213 * [ Strange results will ensue if the open file 214 * limit is ever reduced below the current number 215 * of open files... ] 216 */ 217 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur; 218 fdp = p->p_fd; 219 last = min(fdp->fd_nfiles, lim); 220 freefd = 0; 221 for (i = fdp->fd_freefile; i < last; i++) 222 if (fdp->fd_ofiles[i] == NULL) 223 freefd++; 224 225 /* 226 * Adjust for the fact that the fdesc array may not 227 * have been fully allocated yet. 228 */ 229 if (fdp->fd_nfiles < lim) 230 freefd += (lim - fdp->fd_nfiles); 231 232 sbp->f_bsize = DEV_BSIZE; 233 sbp->f_frsize = DEV_BSIZE; 234 sbp->f_iosize = DEV_BSIZE; 235 sbp->f_blocks = 2; /* 1K to keep df happy */ 236 sbp->f_bfree = 0; 237 sbp->f_bavail = 0; 238 sbp->f_bresvd = 0; 239 sbp->f_files = lim + 1; /* Allow for "." */ 240 sbp->f_ffree = freefd; /* See comments above */ 241 sbp->f_favail = freefd; /* See comments above */ 242 sbp->f_fresvd = 0; 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