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