Index: head/sys/fs/fdescfs/fdesc.h =================================================================== --- head/sys/fs/fdescfs/fdesc.h (revision 179287) +++ head/sys/fs/fdescfs/fdesc.h (revision 179288) @@ -1,64 +1,69 @@ /*- * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software donated to Berkeley by * Jan-Simon Pendry. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)fdesc.h 8.5 (Berkeley) 1/21/94 * * $FreeBSD$ */ #ifdef _KERNEL +/* Private mount flags for fdescfs. */ +#define FMNT_UNMOUNTF 0x01 struct fdescmount { struct vnode *f_root; /* Root node */ + int flags; }; #define FD_ROOT 1 #define FD_DESC 3 typedef enum { Froot, Fdesc } fdntype; struct fdescnode { LIST_ENTRY(fdescnode) fd_hash; /* Hash list */ struct vnode *fd_vnode; /* Back ptr to vnode */ fdntype fd_type; /* Type of this node */ unsigned fd_fd; /* Fd to be dup'ed */ int fd_ix; /* filesystem index */ }; +extern struct mtx fdesc_hashmtx; #define VFSTOFDESC(mp) ((struct fdescmount *)((mp)->mnt_data)) #define VTOFDESC(vp) ((struct fdescnode *)(vp)->v_data) extern vfs_init_t fdesc_init; -extern int fdesc_allocvp(fdntype, int, struct mount *, struct vnode **, - struct thread *); +extern vfs_uninit_t fdesc_uninit; +extern int fdesc_allocvp(fdntype, unsigned, int, struct mount *, + struct vnode **, struct thread *); #endif /* _KERNEL */ Index: head/sys/fs/fdescfs/fdesc_vfsops.c =================================================================== --- head/sys/fs/fdescfs/fdesc_vfsops.c (revision 179287) +++ head/sys/fs/fdescfs/fdesc_vfsops.c (revision 179288) @@ -1,214 +1,238 @@ /*- * Copyright (c) 1992, 1993, 1995 * The Regents of the University of California. All rights reserved. * * This code is derived from software donated to Berkeley by * Jan-Simon Pendry. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)fdesc_vfsops.c 8.4 (Berkeley) 1/21/94 * * $FreeBSD$ */ /* * /dev/fd Filesystem */ #include #include #include #include #include #include #include #include #include #include #include #include static MALLOC_DEFINE(M_FDESCMNT, "fdesc_mount", "FDESC mount structure"); static vfs_cmount_t fdesc_cmount; static vfs_mount_t fdesc_mount; static vfs_unmount_t fdesc_unmount; static vfs_statfs_t fdesc_statfs; static vfs_root_t fdesc_root; /* * Compatibility shim for old mount(2) system call. */ int fdesc_cmount(struct mntarg *ma, void *data, int flags, struct thread *td) { return kernel_mount(ma, flags); } /* * Mount the per-process file descriptors (/dev/fd) */ static int fdesc_mount(struct mount *mp, struct thread *td) { int error = 0; struct fdescmount *fmp; struct vnode *rvp; /* * Update is a no-op */ if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS)) return (EOPNOTSUPP); - error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp, td); - if (error) - return (error); - MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount), M_FDESCMNT, M_WAITOK); /* XXX */ + + /* + * We need to initialize a few bits of our local mount point struct to + * avoid confusion in allocvp. + */ + mp->mnt_data = (qaddr_t) fmp; + fmp->flags = 0; + error = fdesc_allocvp(Froot, -1, FD_ROOT, mp, &rvp, td); + if (error) { + free(fmp, M_FDESCMNT); + mp->mnt_data = 0; + return (error); + } rvp->v_type = VDIR; rvp->v_vflag |= VV_ROOT; fmp->f_root = rvp; + VOP_UNLOCK(rvp, 0); /* XXX -- don't mark as local to work around fts() problems */ /*mp->mnt_flag |= MNT_LOCAL;*/ - mp->mnt_data = fmp; + MNT_ILOCK(mp); + mp->mnt_kern_flag |= MNTK_MPSAFE; + MNT_IUNLOCK(mp); vfs_getnewfsid(mp); vfs_mountedfrom(mp, "fdescfs"); return (0); } static int fdesc_unmount(mp, mntflags, td) struct mount *mp; int mntflags; struct thread *td; { + struct fdescmount *fmp; + caddr_t data; int error; int flags = 0; - if (mntflags & MNT_FORCE) + fmp = (struct fdescmount *)mp->mnt_data; + if (mntflags & MNT_FORCE) { + /* The hash mutex protects the private mount flags. */ + mtx_lock(&fdesc_hashmtx); + fmp->flags |= FMNT_UNMOUNTF; + mtx_unlock(&fdesc_hashmtx); flags |= FORCECLOSE; + } /* * Clear out buffer cache. I don't think we * ever get anything cached at this level at the * moment, but who knows... * * There is 1 extra root vnode reference corresponding * to f_root. */ if ((error = vflush(mp, 1, flags, td)) != 0) return (error); /* - * Finally, throw away the fdescmount structure + * Finally, throw away the fdescmount structure. Hold the hashmtx to + * protect the fdescmount structure. */ - free(mp->mnt_data, M_FDESCMNT); /* XXX */ + mtx_lock(&fdesc_hashmtx); + data = mp->mnt_data; mp->mnt_data = 0; + mtx_unlock(&fdesc_hashmtx); + free(data, M_FDESCMNT); /* XXX */ return (0); } static int fdesc_root(mp, flags, vpp, td) struct mount *mp; int flags; struct vnode **vpp; struct thread *td; { struct vnode *vp; /* * Return locked reference to root. */ vp = VFSTOFDESC(mp)->f_root; - VREF(vp); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); + vget(vp, LK_EXCLUSIVE | LK_RETRY, td); *vpp = vp; return (0); } static int fdesc_statfs(mp, sbp, td) struct mount *mp; struct statfs *sbp; struct thread *td; { struct filedesc *fdp; int lim; int i; int last; int freefd; /* * Compute number of free file descriptors. * [ Strange results will ensue if the open file * limit is ever reduced below the current number * of open files... ] */ PROC_LOCK(td->td_proc); lim = lim_cur(td->td_proc, RLIMIT_NOFILE); PROC_UNLOCK(td->td_proc); fdp = td->td_proc->p_fd; FILEDESC_SLOCK(fdp); last = min(fdp->fd_nfiles, lim); freefd = 0; for (i = fdp->fd_freefile; i < last; i++) if (fdp->fd_ofiles[i] == NULL) freefd++; /* * Adjust for the fact that the fdesc array may not * have been fully allocated yet. */ if (fdp->fd_nfiles < lim) freefd += (lim - fdp->fd_nfiles); FILEDESC_SUNLOCK(fdp); sbp->f_flags = 0; sbp->f_bsize = DEV_BSIZE; sbp->f_iosize = DEV_BSIZE; sbp->f_blocks = 2; /* 1K to keep df happy */ sbp->f_bfree = 0; sbp->f_bavail = 0; sbp->f_files = lim + 1; /* Allow for "." */ sbp->f_ffree = freefd; /* See comments above */ return (0); } static struct vfsops fdesc_vfsops = { .vfs_cmount = fdesc_cmount, .vfs_init = fdesc_init, .vfs_mount = fdesc_mount, .vfs_root = fdesc_root, .vfs_statfs = fdesc_statfs, + .vfs_uninit = fdesc_uninit, .vfs_unmount = fdesc_unmount, }; VFS_SET(fdesc_vfsops, fdescfs, VFCF_SYNTHETIC); Index: head/sys/fs/fdescfs/fdesc_vnops.c =================================================================== --- head/sys/fs/fdescfs/fdesc_vnops.c (revision 179287) +++ head/sys/fs/fdescfs/fdesc_vnops.c (revision 179288) @@ -1,538 +1,624 @@ /*- * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software donated to Berkeley by * Jan-Simon Pendry. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)fdesc_vnops.c 8.9 (Berkeley) 1/21/94 * * $FreeBSD$ */ /* * /dev/fd Filesystem */ #include #include #include #include #include #include /* boottime */ #include #include #include #include /* Must come after sys/malloc.h */ #include #include #include #include #include #include -#define FDL_WANT 0x01 -#define FDL_LOCKED 0x02 -static int fdcache_lock; - #define NFDCACHE 4 #define FD_NHASH(ix) \ (&fdhashtbl[(ix) & fdhash]) static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl; static u_long fdhash; +struct mtx fdesc_hashmtx; + static vop_getattr_t fdesc_getattr; -static vop_inactive_t fdesc_inactive; static vop_lookup_t fdesc_lookup; static vop_open_t fdesc_open; static vop_readdir_t fdesc_readdir; static vop_reclaim_t fdesc_reclaim; static vop_setattr_t fdesc_setattr; static struct vop_vector fdesc_vnodeops = { .vop_default = &default_vnodeops, .vop_access = VOP_NULL, .vop_getattr = fdesc_getattr, - .vop_inactive = fdesc_inactive, .vop_lookup = fdesc_lookup, .vop_open = fdesc_open, .vop_pathconf = vop_stdpathconf, .vop_readdir = fdesc_readdir, .vop_reclaim = fdesc_reclaim, .vop_setattr = fdesc_setattr, }; +static void fdesc_insmntque_dtr(struct vnode *, void *); +static void fdesc_remove_entry(struct fdescnode *); + /* * Initialise cache headers */ int fdesc_init(vfsp) struct vfsconf *vfsp; { + mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF); fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash); return (0); } +/* + * Uninit ready for unload. + */ int -fdesc_allocvp(ftype, ix, mp, vpp, td) +fdesc_uninit(vfsp) + struct vfsconf *vfsp; +{ + + hashdestroy(fdhashtbl, M_CACHE, fdhash); + mtx_destroy(&fdesc_hashmtx); + return (0); +} + +/* + * If allocating vnode fails, call this. + */ +static void +fdesc_insmntque_dtr(struct vnode *vp, void *arg) +{ + + vgone(vp); + vput(vp); +} + +/* + * Remove an entry from the hash if it exists. + */ +static void +fdesc_remove_entry(struct fdescnode *fd) +{ + struct fdhashhead *fc; + struct fdescnode *fd2; + + fc = FD_NHASH(fd->fd_ix); + mtx_lock(&fdesc_hashmtx); + LIST_FOREACH(fd2, fc, fd_hash) { + if (fd == fd2) { + LIST_REMOVE(fd, fd_hash); + break; + } + } + mtx_unlock(&fdesc_hashmtx); +} + +int +fdesc_allocvp(ftype, fd_fd, ix, mp, vpp, td) fdntype ftype; + unsigned fd_fd; int ix; struct mount *mp; struct vnode **vpp; struct thread *td; { + struct fdescmount *fmp; struct fdhashhead *fc; - struct fdescnode *fd; + struct fdescnode *fd, *fd2; + struct vnode *vp, *vp2; int error = 0; fc = FD_NHASH(ix); loop: + mtx_lock(&fdesc_hashmtx); + /* + * If a forced unmount is progressing, we need to drop it. The flags are + * protected by the hashmtx. + */ + fmp = (struct fdescmount *)mp->mnt_data; + if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) { + mtx_unlock(&fdesc_hashmtx); + return (-1); + } + LIST_FOREACH(fd, fc, fd_hash) { if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) { - if (vget(fd->fd_vnode, LK_EXCLUSIVE | LK_CANRECURSE, - td)) + /* Get reference to vnode in case it's being free'd */ + vp = fd->fd_vnode; + VI_LOCK(vp); + mtx_unlock(&fdesc_hashmtx); + if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) goto loop; - *vpp = fd->fd_vnode; - VOP_UNLOCK(*vpp, 0); - return (error); + *vpp = vp; + return (0); } } + mtx_unlock(&fdesc_hashmtx); - /* - * otherwise lock the array while we call getnewvnode - * since that can block. - */ - if (fdcache_lock & FDL_LOCKED) { - fdcache_lock |= FDL_WANT; - (void) tsleep( &fdcache_lock, PINOD, "fdalvp", 0); - goto loop; - } - fdcache_lock |= FDL_LOCKED; - - /* - * Do the MALLOC before the getnewvnode since doing so afterward - * might cause a bogus v_data pointer to get dereferenced - * elsewhere if MALLOC should block. - */ MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK); - error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, vpp); + error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp); if (error) { FREE(fd, M_TEMP); - goto out; + return (error); } - (*vpp)->v_data = fd; - fd->fd_vnode = *vpp; + vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); + vp->v_data = fd; + fd->fd_vnode = vp; fd->fd_type = ftype; - fd->fd_fd = -1; + fd->fd_fd = fd_fd; fd->fd_ix = ix; - /* XXX: vnode should be locked here */ - error = insmntque(*vpp, mp); /* XXX: Too early for mpsafe fs */ + error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL); if (error != 0) { - free(fd, M_TEMP); *vpp = NULLVP; - goto out; + return (error); } - LIST_INSERT_HEAD(fc, fd, fd_hash); -out: - fdcache_lock &= ~FDL_LOCKED; + /* Make sure that someone didn't beat us when inserting the vnode. */ + mtx_lock(&fdesc_hashmtx); + /* + * If a forced unmount is progressing, we need to drop it. The flags are + * protected by the hashmtx. + */ + fmp = (struct fdescmount *)mp->mnt_data; + if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) { + mtx_unlock(&fdesc_hashmtx); + vgone(vp); + vput(vp); + *vpp = NULLVP; + return (-1); + } - if (fdcache_lock & FDL_WANT) { - fdcache_lock &= ~FDL_WANT; - wakeup( &fdcache_lock); + LIST_FOREACH(fd2, fc, fd_hash) { + if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) { + /* Get reference to vnode in case it's being free'd */ + vp2 = fd2->fd_vnode; + VI_LOCK(vp2); + mtx_unlock(&fdesc_hashmtx); + error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, td); + /* Someone beat us, dec use count and wait for reclaim */ + vgone(vp); + vput(vp); + /* If we didn't get it, return no vnode. */ + if (error) + vp2 = NULLVP; + *vpp = vp2; + return (error); + } } - return (error); + /* If we came here, we can insert it safely. */ + LIST_INSERT_HEAD(fc, fd, fd_hash); + mtx_unlock(&fdesc_hashmtx); + *vpp = vp; + return (0); } /* * vp is the current namei directory * ndp is the name to locate in that directory... */ static int fdesc_lookup(ap) struct vop_lookup_args /* { struct vnode * a_dvp; struct vnode ** a_vpp; struct componentname * a_cnp; } */ *ap; { struct vnode **vpp = ap->a_vpp; struct vnode *dvp = ap->a_dvp; struct componentname *cnp = ap->a_cnp; char *pname = cnp->cn_nameptr; struct thread *td = cnp->cn_thread; struct file *fp; int nlen = cnp->cn_namelen; u_int fd; int error; struct vnode *fvp; if ((cnp->cn_flags & ISLASTCN) && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { error = EROFS; goto bad; } if (cnp->cn_namelen == 1 && *pname == '.') { *vpp = dvp; VREF(dvp); return (0); } if (VTOFDESC(dvp)->fd_type != Froot) { error = ENOTDIR; goto bad; } fd = 0; /* the only time a leading 0 is acceptable is if it's "0" */ if (*pname == '0' && nlen != 1) { error = ENOENT; goto bad; } while (nlen--) { if (*pname < '0' || *pname > '9') { error = ENOENT; goto bad; } fd = 10 * fd + *pname++ - '0'; } if ((error = fget(td, fd, &fp)) != 0) goto bad; - error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp, td); - fdrop(fp, td); + /* Check if we're looking up ourselves. */ + if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) { + /* + * In case we're holding the last reference to the file, the dvp + * will be re-acquired. + */ + vhold(dvp); + VOP_UNLOCK(dvp, 0); + fdrop(fp, td); + + /* Re-aquire the lock afterwards. */ + vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE); + vdrop(dvp); + fvp = dvp; + } else { + /* + * Unlock our root node (dvp) when doing this, since we might + * deadlock since the vnode might be locked by another thread + * and the root vnode lock will be obtained afterwards (in case + * we're looking up the fd of the root vnode), which will be the + * opposite lock order. Vhold the root vnode first so we don't + * loose it. + */ + vhold(dvp); + VOP_UNLOCK(dvp, 0); + error = fdesc_allocvp(Fdesc, fd, FD_DESC + fd, dvp->v_mount, + &fvp, td); + fdrop(fp, td); + /* + * The root vnode must be locked last to prevent deadlock condition. + */ + vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE); + vdrop(dvp); + } + if (error) goto bad; - VTOFDESC(fvp)->fd_fd = fd; - if (fvp != dvp) - vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY); *vpp = fvp; return (0); bad: *vpp = NULL; return (error); } static int fdesc_open(ap) struct vop_open_args /* { struct vnode *a_vp; int a_mode; struct ucred *a_cred; struct thread *a_td; } */ *ap; { struct vnode *vp = ap->a_vp; if (VTOFDESC(vp)->fd_type == Froot) return (0); /* * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the the file * descriptor being sought for duplication. The error return ensures * that the vnode for this device will be released by vn_open. Open * will detect this special error and take the actions in dupfdopen. * Other callers of vn_open or VOP_OPEN will simply report the * error. */ ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */ return (ENODEV); } static int fdesc_getattr(ap) struct vop_getattr_args /* { struct vnode *a_vp; struct vattr *a_vap; struct ucred *a_cred; struct thread *a_td; } */ *ap; { struct vnode *vp = ap->a_vp; struct vattr *vap = ap->a_vap; struct file *fp; struct stat stb; u_int fd; int error = 0; switch (VTOFDESC(vp)->fd_type) { case Froot: VATTR_NULL(vap); vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; vap->va_type = VDIR; vap->va_nlink = 2; vap->va_size = DEV_BSIZE; vap->va_fileid = VTOFDESC(vp)->fd_ix; vap->va_uid = 0; vap->va_gid = 0; vap->va_blocksize = DEV_BSIZE; vap->va_atime.tv_sec = boottime.tv_sec; vap->va_atime.tv_nsec = 0; vap->va_mtime = vap->va_atime; vap->va_ctime = vap->va_mtime; vap->va_gen = 0; vap->va_flags = 0; vap->va_rdev = 0; vap->va_bytes = 0; break; case Fdesc: fd = VTOFDESC(vp)->fd_fd; if ((error = fget(ap->a_td, fd, &fp)) != 0) return (error); bzero(&stb, sizeof(stb)); error = fo_stat(fp, &stb, ap->a_td->td_ucred, ap->a_td); fdrop(fp, ap->a_td); if (error == 0) { VATTR_NULL(vap); vap->va_type = IFTOVT(stb.st_mode); vap->va_mode = stb.st_mode; #define FDRX (VREAD|VEXEC) if (vap->va_type == VDIR) vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6)); #undef FDRX vap->va_nlink = 1; vap->va_flags = 0; vap->va_bytes = stb.st_blocks * stb.st_blksize; vap->va_fileid = VTOFDESC(vp)->fd_ix; vap->va_size = stb.st_size; vap->va_blocksize = stb.st_blksize; vap->va_rdev = stb.st_rdev; /* * If no time data is provided, use the current time. */ if (stb.st_atimespec.tv_sec == 0 && stb.st_atimespec.tv_nsec == 0) nanotime(&stb.st_atimespec); if (stb.st_ctimespec.tv_sec == 0 && stb.st_ctimespec.tv_nsec == 0) nanotime(&stb.st_ctimespec); if (stb.st_mtimespec.tv_sec == 0 && stb.st_mtimespec.tv_nsec == 0) nanotime(&stb.st_mtimespec); vap->va_atime = stb.st_atimespec; vap->va_mtime = stb.st_mtimespec; vap->va_ctime = stb.st_ctimespec; vap->va_uid = stb.st_uid; vap->va_gid = stb.st_gid; } break; default: panic("fdesc_getattr"); break; } if (error == 0) vp->v_type = vap->va_type; return (error); } static int fdesc_setattr(ap) struct vop_setattr_args /* { struct vnode *a_vp; struct vattr *a_vap; struct ucred *a_cred; struct thread *a_td; } */ *ap; { struct vattr *vap = ap->a_vap; struct vnode *vp; struct mount *mp; struct file *fp; unsigned fd; int error; /* * Can't mess with the root vnode */ if (VTOFDESC(ap->a_vp)->fd_type == Froot) return (EACCES); fd = VTOFDESC(ap->a_vp)->fd_fd; /* * Allow setattr where there is an underlying vnode. */ error = getvnode(ap->a_td->td_proc->p_fd, fd, &fp); if (error) { /* * getvnode() returns EINVAL if the file descriptor is not * backed by a vnode. Silently drop all changes except * chflags(2) in this case. */ if (error == EINVAL) { if (vap->va_flags != VNOVAL) error = EOPNOTSUPP; else error = 0; } return (error); } vp = fp->f_vnode; if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) { vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred, ap->a_td); VOP_UNLOCK(vp, 0); vn_finished_write(mp); } fdrop(fp, ap->a_td); return (error); } #define UIO_MX 16 static int fdesc_readdir(ap) struct vop_readdir_args /* { struct vnode *a_vp; struct uio *a_uio; struct ucred *a_cred; int *a_eofflag; u_long *a_cookies; int a_ncookies; } */ *ap; { struct uio *uio = ap->a_uio; struct filedesc *fdp; struct dirent d; struct dirent *dp = &d; int error, i, off, fcnt; /* * We don't allow exporting fdesc mounts, and currently local * requests do not need cookies. */ if (ap->a_ncookies) panic("fdesc_readdir: not hungry"); if (VTOFDESC(ap->a_vp)->fd_type != Froot) panic("fdesc_readdir: not dir"); off = (int)uio->uio_offset; if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 || uio->uio_resid < UIO_MX) return (EINVAL); i = (u_int)off / UIO_MX; fdp = uio->uio_td->td_proc->p_fd; error = 0; fcnt = i - 2; /* The first two nodes are `.' and `..' */ FILEDESC_SLOCK(fdp); while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) { switch (i) { case 0: /* `.' */ case 1: /* `..' */ bzero((caddr_t)dp, UIO_MX); dp->d_fileno = i + FD_ROOT; dp->d_namlen = i + 1; dp->d_reclen = UIO_MX; bcopy("..", dp->d_name, dp->d_namlen); dp->d_name[i + 1] = '\0'; dp->d_type = DT_DIR; break; default: if (fdp->fd_ofiles[fcnt] == NULL) { FILEDESC_SUNLOCK(fdp); goto done; } bzero((caddr_t) dp, UIO_MX); dp->d_namlen = sprintf(dp->d_name, "%d", fcnt); dp->d_reclen = UIO_MX; dp->d_type = DT_UNKNOWN; dp->d_fileno = i + FD_DESC; break; } /* * And ship to userland */ FILEDESC_SUNLOCK(fdp); error = uiomove(dp, UIO_MX, uio); if (error) goto done; FILEDESC_SLOCK(fdp); i++; fcnt++; } FILEDESC_SUNLOCK(fdp); done: uio->uio_offset = i * UIO_MX; return (error); } static int -fdesc_inactive(ap) - struct vop_inactive_args /* { - struct vnode *a_vp; - struct thread *a_td; - } */ *ap; -{ - struct vnode *vp = ap->a_vp; - - /* - * Clear out the v_type field to avoid - * nasty things happening in vgone(). - */ - vp->v_type = VNON; - return (0); -} - -static int fdesc_reclaim(ap) struct vop_reclaim_args /* { struct vnode *a_vp; } */ *ap; { - struct vnode *vp = ap->a_vp; - struct fdescnode *fd = VTOFDESC(vp); + struct vnode *vp; + struct fdescnode *fd; - LIST_REMOVE(fd, fd_hash); + vp = ap->a_vp; + fd = VTOFDESC(vp); + fdesc_remove_entry(fd); FREE(vp->v_data, M_TEMP); - vp->v_data = 0; - + vp->v_data = NULL; return (0); }