fdesc_vfsops.c revision 132093
1228063Sbapt/* 295060Sjmallett * Copyright (c) 1992, 1993, 1995 395060Sjmallett * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software donated to Berkeley by 61590Srgrimes * Jan-Simon Pendry. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 4. Neither the name of the University nor the names of its contributors 171590Srgrimes * may be used to endorse or promote products derived from this software 181590Srgrimes * without specific prior written permission. 19228063Sbapt * 201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301590Srgrimes * SUCH DAMAGE. 311590Srgrimes * 321590Srgrimes * @(#)fdesc_vfsops.c 8.4 (Berkeley) 1/21/94 331590Srgrimes * 341590Srgrimes * $FreeBSD: head/sys/fs/fdescfs/fdesc_vfsops.c 132093 2004-07-13 09:46:46Z phk $ 351590Srgrimes */ 3695060Sjmallett 371590Srgrimes/* 381590Srgrimes * /dev/fd Filesystem 391590Srgrimes */ 401590Srgrimes 411590Srgrimes#include <sys/param.h> 421590Srgrimes#include <sys/systm.h> 431590Srgrimes#include <sys/filedesc.h> 441590Srgrimes#include <sys/kernel.h> 451590Srgrimes#include <sys/lock.h> 461590Srgrimes#include <sys/mutex.h> 471590Srgrimes#include <sys/malloc.h> 48100014Sjmallett#include <sys/mount.h> 49100014Sjmallett#include <sys/proc.h> 50100014Sjmallett#include <sys/resourcevar.h> 511590Srgrimes#include <sys/vnode.h> 521590Srgrimes 531590Srgrimes#include <fs/fdescfs/fdesc.h> 541590Srgrimes 551590Srgrimesstatic MALLOC_DEFINE(M_FDESCMNT, "FDESC mount", "FDESC mount structure"); 561590Srgrimes 57static vfs_nmount_t fdesc_mount; 58static vfs_unmount_t fdesc_unmount; 59static vfs_statfs_t fdesc_statfs; 60 61/* 62 * Mount the per-process file descriptors (/dev/fd) 63 */ 64static int 65fdesc_mount(mp, ndp, td) 66 struct mount *mp; 67 struct nameidata *ndp; 68 struct thread *td; 69{ 70 int error = 0; 71 struct fdescmount *fmp; 72 struct vnode *rvp; 73 74 /* 75 * Update is a no-op 76 */ 77 if (mp->mnt_flag & MNT_UPDATE) 78 return (EOPNOTSUPP); 79 80 error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp, td); 81 if (error) 82 return (error); 83 84 MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount), 85 M_FDESCMNT, M_WAITOK); /* XXX */ 86 rvp->v_type = VDIR; 87 rvp->v_vflag |= VV_ROOT; 88 fmp->f_root = rvp; 89 /* XXX -- don't mark as local to work around fts() problems */ 90 /*mp->mnt_flag |= MNT_LOCAL;*/ 91 mp->mnt_data = (qaddr_t) fmp; 92 vfs_getnewfsid(mp); 93 94 bzero(mp->mnt_stat.f_mntfromname, MNAMELEN); 95 bcopy("fdescfs", mp->mnt_stat.f_mntfromname, sizeof("fdescfs")); 96 (void)fdesc_statfs(mp, &mp->mnt_stat, td); 97 return (0); 98} 99 100static int 101fdesc_unmount(mp, mntflags, td) 102 struct mount *mp; 103 int mntflags; 104 struct thread *td; 105{ 106 int error; 107 int flags = 0; 108 109 if (mntflags & MNT_FORCE) 110 flags |= FORCECLOSE; 111 112 /* 113 * Clear out buffer cache. I don't think we 114 * ever get anything cached at this level at the 115 * moment, but who knows... 116 * 117 * There is 1 extra root vnode reference corresponding 118 * to f_root. 119 */ 120 if ((error = vflush(mp, 1, flags, td)) != 0) 121 return (error); 122 123 /* 124 * Finally, throw away the fdescmount structure 125 */ 126 free(mp->mnt_data, M_FDESCMNT); /* XXX */ 127 mp->mnt_data = 0; 128 129 return (0); 130} 131 132int 133fdesc_root(mp, vpp, td) 134 struct mount *mp; 135 struct vnode **vpp; 136 struct thread *td; 137{ 138 struct vnode *vp; 139 140 /* 141 * Return locked reference to root. 142 */ 143 vp = VFSTOFDESC(mp)->f_root; 144 VREF(vp); 145 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 146 *vpp = vp; 147 return (0); 148} 149 150static int 151fdesc_statfs(mp, sbp, td) 152 struct mount *mp; 153 struct statfs *sbp; 154 struct thread *td; 155{ 156 struct filedesc *fdp; 157 int lim; 158 int i; 159 int last; 160 int freefd; 161 162 /* 163 * Compute number of free file descriptors. 164 * [ Strange results will ensue if the open file 165 * limit is ever reduced below the current number 166 * of open files... ] 167 */ 168 PROC_LOCK(td->td_proc); 169 lim = lim_cur(td->td_proc, RLIMIT_NOFILE); 170 PROC_UNLOCK(td->td_proc); 171 fdp = td->td_proc->p_fd; 172 FILEDESC_LOCK(fdp); 173 last = min(fdp->fd_nfiles, lim); 174 freefd = 0; 175 for (i = fdp->fd_freefile; i < last; i++) 176 if (fdp->fd_ofiles[i] == NULL) 177 freefd++; 178 179 /* 180 * Adjust for the fact that the fdesc array may not 181 * have been fully allocated yet. 182 */ 183 if (fdp->fd_nfiles < lim) 184 freefd += (lim - fdp->fd_nfiles); 185 FILEDESC_UNLOCK(fdp); 186 187 sbp->f_flags = 0; 188 sbp->f_bsize = DEV_BSIZE; 189 sbp->f_iosize = DEV_BSIZE; 190 sbp->f_blocks = 2; /* 1K to keep df happy */ 191 sbp->f_bfree = 0; 192 sbp->f_bavail = 0; 193 sbp->f_files = lim + 1; /* Allow for "." */ 194 sbp->f_ffree = freefd; /* See comments above */ 195 if (sbp != &mp->mnt_stat) { 196 sbp->f_type = mp->mnt_vfc->vfc_typenum; 197 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid)); 198 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 199 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 200 } 201 return (0); 202} 203 204static struct vfsops fdesc_vfsops = { 205 .vfs_init = fdesc_init, 206 .vfs_nmount = fdesc_mount, 207 .vfs_root = fdesc_root, 208 .vfs_statfs = fdesc_statfs, 209 .vfs_unmount = fdesc_unmount, 210}; 211 212VFS_SET(fdesc_vfsops, fdescfs, VFCF_SYNTHETIC); 213