BSD 4_4_Lite1 release
[unix-history] / usr / src / sys / miscfs / fdesc / fdesc_vnops.c
index 78557cc..00d8675 100644 (file)
@@ -1,16 +1,41 @@
 /*
 /*
- * Copyright (c) 1992 The Regents of the University of California
- * Copyright (c) 1990, 1992 Jan-Simon Pendry
- * All rights reserved.
+ * 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.
  *
  *
  * This code is derived from software donated to Berkeley by
  * Jan-Simon Pendry.
  *
- * %sccs.include.redist.c%
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 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.
  *
  *
- *     @(#)fdesc_vnops.c       7.1 (Berkeley) %G%
+ * 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.
  *
  *
- * $Id: fdesc_vnops.c,v 1.7 1992/05/30 10:05:34 jsp Exp jsp $
+ *     @(#)fdesc_vnops.c       8.9 (Berkeley) 1/21/94
+ *
+ * $Id: fdesc_vnops.c,v 1.12 1993/04/06 16:17:17 jsp Exp $
  */
 
 /*
  */
 
 /*
@@ -22,6 +47,7 @@
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/proc.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/proc.h>
+#include <sys/kernel.h>        /* boottime */
 #include <sys/resourcevar.h>
 #include <sys/filedesc.h>
 #include <sys/vnode.h>
 #include <sys/resourcevar.h>
 #include <sys/filedesc.h>
 #include <sys/vnode.h>
 #include <sys/dirent.h>
 #include <miscfs/fdesc/fdesc.h>
 
 #include <sys/dirent.h>
 #include <miscfs/fdesc/fdesc.h>
 
+#define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
+
+#define FDL_WANT       0x01
+#define FDL_LOCKED     0x02
+static int fdcache_lock;
+
+dev_t devctty;
+
+#if (FD_STDIN != FD_STDOUT-1) || (FD_STDOUT != FD_STDERR-1)
+FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n+1, n+2
+#endif
+
+#define        NFDCACHE 3
+#define        FD_NHASH(ix) ((ix) & NFDCACHE)
+
+/*
+ * Cache head
+ */
+struct fdcache {
+       struct fdescnode        *fc_forw;
+       struct fdescnode        *fc_back;
+};
+
+static struct fdcache fdcache[NFDCACHE];
+
+/*
+ * Initialise cache headers
+ */
+fdesc_init()
+{
+       struct fdcache *fc;
+
+       devctty = makedev(nchrdev, 0);
+
+       for (fc = fdcache; fc < fdcache + NFDCACHE; fc++)
+               fc->fc_forw = fc->fc_back = (struct fdescnode *) fc;
+}
+
+/*
+ * Compute hash list for given target vnode
+ */
+static struct fdcache *
+fdesc_hash(ix)
+       int ix;
+{
+
+       return (&fdcache[FD_NHASH(ix)]);
+}
+
+int
+fdesc_allocvp(ftype, ix, mp, vpp)
+       fdntype ftype;
+       int ix;
+       struct mount *mp;
+       struct vnode **vpp;
+{
+       struct fdcache *fc;
+       struct fdescnode *fd;
+       int error = 0;
+
+loop:
+       fc = fdesc_hash(ix);
+       for (fd = fc->fc_forw; fd != (struct fdescnode *) fc; fd = fd->fd_forw) {
+               if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
+                       if (vget(fd->fd_vnode, 0))
+                               goto loop;
+                       *vpp = fd->fd_vnode;
+                       return (error);
+               }
+       }
+
+       /*
+        * otherwise lock the array while we call getnewvnode
+        * since that can block.
+        */ 
+       if (fdcache_lock & FDL_LOCKED) {
+               fdcache_lock |= FDL_WANT;
+               sleep((caddr_t) &fdcache_lock, PINOD);
+               goto loop;
+       }
+       fdcache_lock |= FDL_LOCKED;
+
+       error = getnewvnode(VT_FDESC, mp, fdesc_vnodeop_p, vpp);
+       if (error)
+               goto out;
+       MALLOC(fd, void *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
+       (*vpp)->v_data = fd;
+       fd->fd_vnode = *vpp;
+       fd->fd_type = ftype;
+       fd->fd_fd = -1;
+       fd->fd_link = 0;
+       fd->fd_ix = ix;
+       fc = fdesc_hash(ix);
+       insque(fd, fc);
+
+out:;
+       fdcache_lock &= ~FDL_LOCKED;
+
+       if (fdcache_lock & FDL_WANT) {
+               fdcache_lock &= ~FDL_WANT;
+               wakeup((caddr_t) &fdcache_lock);
+       }
+
+       return (error);
+}
+
 /*
  * vp is the current namei directory
  * ndp is the name to locate in that directory...
  */
 /*
  * vp is the current namei directory
  * ndp is the name to locate in that directory...
  */
+int
 fdesc_lookup(ap)
        struct vop_lookup_args /* {
                struct vnode * a_dvp;
 fdesc_lookup(ap)
        struct vop_lookup_args /* {
                struct vnode * a_dvp;
@@ -53,68 +186,126 @@ fdesc_lookup(ap)
        unsigned fd;
        int error;
        struct vnode *fvp;
        unsigned fd;
        int error;
        struct vnode *fvp;
+       char *ln;
 
 
-#ifdef FDESC_DIAGNOSTIC
-       printf("fdesc_lookup(%x)\n", ap);
-       printf("fdesc_lookup(dp = %x, vpp = %x, cnp = %x)\n", dvp, vpp, ap->a_cnp);
-#endif
        pname = ap->a_cnp->cn_nameptr;
        pname = ap->a_cnp->cn_nameptr;
-#ifdef FDESC_DIAGNOSTIC
-       printf("fdesc_lookup(%s)\n", pname);
-#endif
        if (ap->a_cnp->cn_namelen == 1 && *pname == '.') {
                *vpp = dvp;
                VREF(dvp);      
        if (ap->a_cnp->cn_namelen == 1 && *pname == '.') {
                *vpp = dvp;
                VREF(dvp);      
-               /*VOP_LOCK(dvp);*/
+               VOP_LOCK(dvp);
                return (0);
        }
 
        p = ap->a_cnp->cn_proc;
        nfiles = p->p_fd->fd_nfiles;
 
                return (0);
        }
 
        p = ap->a_cnp->cn_proc;
        nfiles = p->p_fd->fd_nfiles;
 
-       fd = 0;
-       while (*pname >= '0' && *pname <= '9') {
-               fd = 10 * fd + *pname++ - '0';
-               if (fd >= nfiles)
+       switch (VTOFDESC(dvp)->fd_type) {
+       default:
+       case Flink:
+       case Fdesc:
+       case Fctty:
+               error = ENOTDIR;
+               goto bad;
+
+       case Froot:
+               if (ap->a_cnp->cn_namelen == 2 && bcmp(pname, "fd", 2) == 0) {
+                       error = fdesc_allocvp(Fdevfd, FD_DEVFD, dvp->v_mount, &fvp);
+                       if (error)
+                               goto bad;
+                       *vpp = fvp;
+                       fvp->v_type = VDIR;
+                       VOP_LOCK(fvp);
+                       return (0);
+               }
+
+               if (ap->a_cnp->cn_namelen == 3 && bcmp(pname, "tty", 3) == 0) {
+                       struct vnode *ttyvp = cttyvp(p);
+                       if (ttyvp == NULL) {
+                               error = ENXIO;
+                               goto bad;
+                       }
+                       error = fdesc_allocvp(Fctty, FD_CTTY, dvp->v_mount, &fvp);
+                       if (error)
+                               goto bad;
+                       *vpp = fvp;
+                       fvp->v_type = VFIFO;
+                       VOP_LOCK(fvp);
+                       return (0);
+               }
+
+               ln = 0;
+               switch (ap->a_cnp->cn_namelen) {
+               case 5:
+                       if (bcmp(pname, "stdin", 5) == 0) {
+                               ln = "fd/0";
+                               fd = FD_STDIN;
+                       }
                        break;
                        break;
-       }
+               case 6:
+                       if (bcmp(pname, "stdout", 6) == 0) {
+                               ln = "fd/1";
+                               fd = FD_STDOUT;
+                       } else
+                       if (bcmp(pname, "stderr", 6) == 0) {
+                               ln = "fd/2";
+                               fd = FD_STDERR;
+                       }
+                       break;
+               }
 
 
-#ifdef FDESC_DIAGNOSTIC
-       printf("fdesc_lookup: fd = %d, *pname = %x\n", fd, *pname);
-#endif
-       if (*pname != '\0') {
-               error = ENOENT;
-               goto bad;
-       }
+               if (ln) {
+                       error = fdesc_allocvp(Flink, fd, dvp->v_mount, &fvp);
+                       if (error)
+                               goto bad;
+                       VTOFDESC(fvp)->fd_link = ln;
+                       *vpp = fvp;
+                       fvp->v_type = VLNK;
+                       VOP_LOCK(fvp);
+                       return (0);
+               } else {
+                       error = ENOENT;
+                       goto bad;
+               }
 
 
-       if (fd >= nfiles || p->p_fd->fd_ofiles[fd] == NULL) {
-               error = EBADF;
-               goto bad;
-       }
+               /* FALL THROUGH */
 
 
-#ifdef FDESC_DIAGNOSTIC
-       printf("fdesc_lookup: allocate new vnode\n");
-#endif
-       error = getnewvnode(VT_UFS, dvp->v_mount, fdesc_vnodeop_p, &fvp);
-       if (error)
-               goto bad;
-       MALLOC(fvp->v_data, void *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
-       VTOFDESC(fvp)->f_fd = fd;
-       /*VTOFDESC(fvp)->f_isroot = 0;*/
-       *vpp = fvp;
-#ifdef FDESC_DIAGNOSTIC
-       printf("fdesc_lookup: newvp = %x\n", fvp);
-#endif
-       return (0);
+       case Fdevfd:
+               if (ap->a_cnp->cn_namelen == 2 && bcmp(pname, "..", 2) == 0) {
+                       error = fdesc_root(dvp->v_mount, vpp);
+                       return (error);
+               }
+
+               fd = 0;
+               while (*pname >= '0' && *pname <= '9') {
+                       fd = 10 * fd + *pname++ - '0';
+                       if (fd >= nfiles)
+                               break;
+               }
+
+               if (*pname != '\0') {
+                       error = ENOENT;
+                       goto bad;
+               }
+
+               if (fd >= nfiles || p->p_fd->fd_ofiles[fd] == NULL) {
+                       error = EBADF;
+                       goto bad;
+               }
+
+               error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
+               if (error)
+                       goto bad;
+               VTOFDESC(fvp)->fd_fd = fd;
+               *vpp = fvp;
+               return (0);
+       }
 
 bad:;
        *vpp = NULL;
 
 bad:;
        *vpp = NULL;
-#ifdef FDESC_DIAGNOSTIC
-       printf("fdesc_lookup: error = %d\n", error);
-#endif
        return (error);
 }
 
        return (error);
 }
 
+int
 fdesc_open(ap)
        struct vop_open_args /* {
                struct vnode *a_vp;
 fdesc_open(ap)
        struct vop_open_args /* {
                struct vnode *a_vp;
@@ -124,23 +315,28 @@ fdesc_open(ap)
        } */ *ap;
 {
        struct vnode *vp = ap->a_vp;
        } */ *ap;
 {
        struct vnode *vp = ap->a_vp;
+       int error = 0;
+
+       switch (VTOFDESC(vp)->fd_type) {
+       case Fdesc:
+               /*
+                * XXX Kludge: set p->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_p->p_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
+               error = ENODEV;
+               break;
 
 
-       /*
-        * Can always open the root (modulo perms)
-        */
-       if (vp->v_flag & VROOT)
-               return (0);
+       case Fctty:
+               error = cttyopen(devctty, ap->a_mode, 0, ap->a_p);
+               break;
+       }
 
 
-       /*
-        * XXX Kludge: set ap->a_p->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_p->p_dupfd = VTOFDESC(vp)->f_fd;  /* XXX */
-       return (ENODEV);
+       return (error);
 }
 
 static int
 }
 
 static int
@@ -152,30 +348,45 @@ fdesc_attr(fd, vap, cred, p)
 {
        struct filedesc *fdp = p->p_fd;
        struct file *fp;
 {
        struct filedesc *fdp = p->p_fd;
        struct file *fp;
+       struct stat stb;
        int error;
 
        int error;
 
-#ifdef FDESC_DIAGNOSTIC
-       printf("fdesc_attr: fd = %d, nfiles = %d\n", fd, fdp->fd_nfiles);
-#endif
-       if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
-#ifdef FDESC_DIAGNOSTIC
-               printf("fdesc_attr: fp = %x (EBADF)\n", fp);
-#endif
+       if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
                return (EBADF);
                return (EBADF);
-       }
 
 
-       /*
-        * Can stat the underlying vnode, but not sockets because
-        * they don't use struct vattrs.  Well, we could convert from
-        * a struct stat back to a struct vattr, later...
-        */
        switch (fp->f_type) {
        case DTYPE_VNODE:
                error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred, p);
        switch (fp->f_type) {
        case DTYPE_VNODE:
                error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred, p);
+               if (error == 0 && vap->va_type == VDIR) {
+                       /*
+                        * don't allow directories to show up because
+                        * that causes loops in the namespace.
+                        */
+                       vap->va_type = VFIFO;
+               }
                break;
 
        case DTYPE_SOCKET:
                break;
 
        case DTYPE_SOCKET:
-               error = EOPNOTSUPP;
+               error = soo_stat((struct socket *)fp->f_data, &stb);
+               if (error == 0) {
+                       vattr_null(vap);
+                       vap->va_type = VSOCK;
+                       vap->va_mode = stb.st_mode;
+                       vap->va_nlink = stb.st_nlink;
+                       vap->va_uid = stb.st_uid;
+                       vap->va_gid = stb.st_gid;
+                       vap->va_fsid = stb.st_dev;
+                       vap->va_fileid = stb.st_ino;
+                       vap->va_size = stb.st_size;
+                       vap->va_blocksize = stb.st_blksize;
+                       vap->va_atime = stb.st_atimespec;
+                       vap->va_mtime = stb.st_mtimespec;
+                       vap->va_ctime = stb.st_ctimespec;
+                       vap->va_gen = stb.st_gen;
+                       vap->va_flags = stb.st_flags;
+                       vap->va_rdev = stb.st_rdev;
+                       vap->va_bytes = stb.st_blocks * stb.st_blksize;
+               }
                break;
 
        default:
                break;
 
        default:
@@ -183,12 +394,10 @@ fdesc_attr(fd, vap, cred, p)
                break;
        }
 
                break;
        }
 
-#ifdef FDESC_DIAGNOSTIC
-       printf("fdesc_attr: returns error %d\n", error);
-#endif
        return (error);
 }
 
        return (error);
 }
 
+int
 fdesc_getattr(ap)
        struct vop_getattr_args /* {
                struct vnode *a_vp;
 fdesc_getattr(ap)
        struct vop_getattr_args /* {
                struct vnode *a_vp;
@@ -200,42 +409,70 @@ fdesc_getattr(ap)
        struct vnode *vp = ap->a_vp;
        struct vattr *vap = ap->a_vap;
        unsigned fd;
        struct vnode *vp = ap->a_vp;
        struct vattr *vap = ap->a_vap;
        unsigned fd;
-       int error;
+       int error = 0;
 
 
-       if (vp->v_flag & VROOT) {
-#ifdef FDESC_DIAGNOSTIC
-               printf("fdesc_getattr: stat rootdir\n");
-#endif
+       switch (VTOFDESC(vp)->fd_type) {
+       case Froot:
+       case Fdevfd:
+       case Flink:
+       case Fctty:
                bzero((caddr_t) vap, sizeof(*vap));
                vattr_null(vap);
                bzero((caddr_t) vap, sizeof(*vap));
                vattr_null(vap);
-               vap->va_type = VDIR;
-               vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
-               vap->va_nlink = 2;
+               vap->va_fileid = VTOFDESC(vp)->fd_ix;
+
+               switch (VTOFDESC(vp)->fd_type) {
+               case Flink:
+                       vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
+                       vap->va_type = VLNK;
+                       vap->va_nlink = 1;
+                       vap->va_size = strlen(VTOFDESC(vp)->fd_link);
+                       break;
+
+               case Fctty:
+                       vap->va_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
+                       vap->va_type = VFIFO;
+                       vap->va_nlink = 1;
+                       vap->va_size = 0;
+                       break;
+
+               default:
+                       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;
+                       break;
+               }
                vap->va_uid = 0;
                vap->va_gid = 0;
                vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
                vap->va_uid = 0;
                vap->va_gid = 0;
                vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
-               vap->va_fileid = 2;
-               /* vap->va_qsize = 0; */
-               vap->va_size = DEV_BSIZE;
                vap->va_blocksize = DEV_BSIZE;
                vap->va_blocksize = DEV_BSIZE;
-               microtime(&vap->va_atime);
+               vap->va_atime.ts_sec = boottime.tv_sec;
+               vap->va_atime.ts_nsec = 0;
                vap->va_mtime = vap->va_atime;
                vap->va_mtime = vap->va_atime;
-               vap->va_ctime = vap->va_ctime;
+               vap->va_ctime = vap->va_mtime;
                vap->va_gen = 0;
                vap->va_flags = 0;
                vap->va_rdev = 0;
                vap->va_gen = 0;
                vap->va_flags = 0;
                vap->va_rdev = 0;
-               /* vap->va_qbytes = 0; */
                vap->va_bytes = 0;
                vap->va_bytes = 0;
-               return (0);
+               break;
+
+       case Fdesc:
+               fd = VTOFDESC(vp)->fd_fd;
+               error = fdesc_attr(fd, vap, ap->a_cred, ap->a_p);
+               break;
+
+       default:
+               panic("fdesc_getattr");
+               break;  
        }
 
        }
 
-       fd = VTOFDESC(vp)->f_fd;
-       error = fdesc_attr(fd, vap, ap->a_cred, ap->a_p);
        if (error == 0)
                vp->v_type = vap->va_type;
        if (error == 0)
                vp->v_type = vap->va_type;
+
        return (error);
 }
 
        return (error);
 }
 
+int
 fdesc_setattr(ap)
        struct vop_setattr_args /* {
                struct vnode *a_vp;
 fdesc_setattr(ap)
        struct vop_setattr_args /* {
                struct vnode *a_vp;
@@ -252,17 +489,19 @@ fdesc_setattr(ap)
        /*
         * Can't mess with the root vnode
         */
        /*
         * Can't mess with the root vnode
         */
-       if (ap->a_vp->v_flag & VROOT)
+       switch (VTOFDESC(ap->a_vp)->fd_type) {
+       case Fdesc:
+               break;
+
+       case Fctty:
+               return (0);
+
+       default:
                return (EACCES);
                return (EACCES);
+       }
 
 
-       fd = VTOFDESC(ap->a_vp)->f_fd;
-#ifdef FDESC_DIAGNOSTIC
-       printf("fdesc_setattr: fd = %d, nfiles = %d\n", fd, fdp->fd_nfiles);
-#endif
+       fd = VTOFDESC(ap->a_vp)->fd_fd;
        if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
        if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
-#ifdef FDESC_DIAGNOSTIC
-               printf("fdesc_setattr: fp = %x (EBADF)\n", fp);
-#endif
                return (EBADF);
        }
 
                return (EBADF);
        }
 
@@ -275,7 +514,7 @@ fdesc_setattr(ap)
                break;
 
        case DTYPE_SOCKET:
                break;
 
        case DTYPE_SOCKET:
-               error = EOPNOTSUPP;
+               error = 0;
                break;
 
        default:
                break;
 
        default:
@@ -283,12 +522,26 @@ fdesc_setattr(ap)
                break;
        }
 
                break;
        }
 
-#ifdef FDESC_DIAGNOSTIC
-       printf("fdesc_setattr: returns error %d\n", error);
-#endif
        return (error);
 }
 
        return (error);
 }
 
+#define UIO_MX 16
+
+static struct dirtmp {
+       u_long d_fileno;
+       u_short d_reclen;
+       u_short d_namlen;
+       char d_name[8];
+} rootent[] = {
+       { FD_DEVFD, UIO_MX, 2, "fd" },
+       { FD_STDIN, UIO_MX, 5, "stdin" },
+       { FD_STDOUT, UIO_MX, 6, "stdout" },
+       { FD_STDERR, UIO_MX, 6, "stderr" },
+       { FD_CTTY, UIO_MX, 3, "tty" },
+       { 0 }
+};
+
+int
 fdesc_readdir(ap)
        struct vop_readdir_args /* {
                struct vnode *a_vp;
 fdesc_readdir(ap)
        struct vop_readdir_args /* {
                struct vnode *a_vp;
@@ -301,43 +554,80 @@ fdesc_readdir(ap)
        int i;
        int error;
 
        int i;
        int error;
 
-#define UIO_MX 16
+       switch (VTOFDESC(ap->a_vp)->fd_type) {
+       case Fctty:
+               return (0);
+
+       case Fdesc:
+               return (ENOTDIR);
+
+       default:
+               break;
+       }
 
        fdp = uio->uio_procp->p_fd;
 
        fdp = uio->uio_procp->p_fd;
+
+       if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
+               struct dirent d;
+               struct dirent *dp = &d;
+               struct dirtmp *dt;
+
+               i = uio->uio_offset / UIO_MX;
+               error = 0;
+
+               while (uio->uio_resid > 0) {
+                       dt = &rootent[i];
+                       if (dt->d_fileno == 0) {
+                               /**eofflagp = 1;*/
+                               break;
+                       }
+                       i++;
+                       
+                       switch (dt->d_fileno) {
+                       case FD_CTTY:
+                               if (cttyvp(uio->uio_procp) == NULL)
+                                       continue;
+                               break;
+
+                       case FD_STDIN:
+                       case FD_STDOUT:
+                       case FD_STDERR:
+                               if ((dt->d_fileno-FD_STDIN) >= fdp->fd_nfiles)
+                                       continue;
+                               if (fdp->fd_ofiles[dt->d_fileno-FD_STDIN] == NULL)
+                                       continue;
+                               break;
+                       }
+                       bzero((caddr_t) dp, UIO_MX);
+                       dp->d_fileno = dt->d_fileno;
+                       dp->d_namlen = dt->d_namlen;
+                       dp->d_type = DT_UNKNOWN;
+                       dp->d_reclen = dt->d_reclen;
+                       bcopy(dt->d_name, dp->d_name, dp->d_namlen+1);
+                       error = uiomove((caddr_t) dp, UIO_MX, uio);
+                       if (error)
+                               break;
+               }
+               uio->uio_offset = i * UIO_MX;
+               return (error);
+       }
+
        i = uio->uio_offset / UIO_MX;
        error = 0;
        while (uio->uio_resid > 0) {
        i = uio->uio_offset / UIO_MX;
        error = 0;
        while (uio->uio_resid > 0) {
-               if (i >= fdp->fd_nfiles) {
-                       /* *ap->a_eofflagp = 1; */
+               if (i >= fdp->fd_nfiles)
                        break;
                        break;
-               }
+
                if (fdp->fd_ofiles[i] != NULL) {
                        struct dirent d;
                        struct dirent *dp = &d;
                if (fdp->fd_ofiles[i] != NULL) {
                        struct dirent d;
                        struct dirent *dp = &d;
-                       char *cp = dp->d_name;
-#ifdef FDESC_FILEID
-                       struct vattr va;
-#endif
+
                        bzero((caddr_t) dp, UIO_MX);
 
                        dp->d_namlen = sprintf(dp->d_name, "%d", i);
                        bzero((caddr_t) dp, UIO_MX);
 
                        dp->d_namlen = sprintf(dp->d_name, "%d", i);
-                       /*
-                        * Fill in the remaining fields
-                        */
                        dp->d_reclen = UIO_MX;
                        dp->d_type = DT_UNKNOWN;
                        dp->d_reclen = UIO_MX;
                        dp->d_type = DT_UNKNOWN;
-                       dp->d_fileno = i + 3;
-#ifdef FDESC_FILEID
-                       /*
-                        * If we want the file ids to match the
-                        * we must call getattr on the underlying file.
-                        * fdesc_attr may return an error, in which case
-                        * we ignore the returned file id.
-                        */
-                       error = fdesc_attr(i, &va, ap->a_cred, p);
-                       if (error == 0)
-                               dp->d_ino = va.va_fileid;
-#endif
+                       dp->d_fileno = i + FD_STDIN;
                        /*
                         * And ship to userland
                         */
                        /*
                         * And ship to userland
                         */
@@ -352,6 +642,131 @@ fdesc_readdir(ap)
        return (error);
 }
 
        return (error);
 }
 
+int
+fdesc_readlink(ap)
+       struct vop_readlink_args /* {
+               struct vnode *a_vp;
+               struct uio *a_uio;
+               struct ucred *a_cred;
+       } */ *ap;
+{
+       struct vnode *vp = ap->a_vp;
+       int error;
+
+       if (vp->v_type != VLNK)
+               return (EPERM);
+
+       if (VTOFDESC(vp)->fd_type == Flink) {
+               char *ln = VTOFDESC(vp)->fd_link;
+               error = uiomove(ln, strlen(ln), ap->a_uio);
+       } else {
+               error = EOPNOTSUPP;
+       }
+
+       return (error);
+}
+
+int
+fdesc_read(ap)
+       struct vop_read_args /* {
+               struct vnode *a_vp;
+               struct uio *a_uio;
+               int  a_ioflag;
+               struct ucred *a_cred;
+       } */ *ap;
+{
+       int error = EOPNOTSUPP;
+
+       switch (VTOFDESC(ap->a_vp)->fd_type) {
+       case Fctty:
+               error = cttyread(devctty, ap->a_uio, ap->a_ioflag);
+               break;
+
+       default:
+               error = EOPNOTSUPP;
+               break;
+       }
+       
+       return (error);
+}
+
+int
+fdesc_write(ap)
+       struct vop_write_args /* {
+               struct vnode *a_vp;
+               struct uio *a_uio;
+               int  a_ioflag;
+               struct ucred *a_cred;
+       } */ *ap;
+{
+       int error = EOPNOTSUPP;
+
+       switch (VTOFDESC(ap->a_vp)->fd_type) {
+       case Fctty:
+               error = cttywrite(devctty, ap->a_uio, ap->a_ioflag);
+               break;
+
+       default:
+               error = EOPNOTSUPP;
+               break;
+       }
+       
+       return (error);
+}
+
+int
+fdesc_ioctl(ap)
+       struct vop_ioctl_args /* {
+               struct vnode *a_vp;
+               int  a_command;
+               caddr_t  a_data;
+               int  a_fflag;
+               struct ucred *a_cred;
+               struct proc *a_p;
+       } */ *ap;
+{
+       int error = EOPNOTSUPP;
+
+       switch (VTOFDESC(ap->a_vp)->fd_type) {
+       case Fctty:
+               error = cttyioctl(devctty, ap->a_command, ap->a_data,
+                                       ap->a_fflag, ap->a_p);
+               break;
+
+       default:
+               error = EOPNOTSUPP;
+               break;
+       }
+       
+       return (error);
+}
+
+int
+fdesc_select(ap)
+       struct vop_select_args /* {
+               struct vnode *a_vp;
+               int  a_which;
+               int  a_fflags;
+               struct ucred *a_cred;
+               struct proc *a_p;
+       } */ *ap;
+{
+       int error = EOPNOTSUPP;
+
+       switch (VTOFDESC(ap->a_vp)->fd_type) {
+       case Fctty:
+               error = cttyselect(devctty, ap->a_fflags, ap->a_p);
+               break;
+
+       default:
+               error = EOPNOTSUPP;
+               break;
+       }
+       
+       return (error);
+}
+
+int
 fdesc_inactive(ap)
        struct vop_inactive_args /* {
                struct vnode *a_vp;
 fdesc_inactive(ap)
        struct vop_inactive_args /* {
                struct vnode *a_vp;
@@ -364,30 +779,65 @@ fdesc_inactive(ap)
         * nasty things happening in vgone().
         */
        vp->v_type = VNON;
         * nasty things happening in vgone().
         */
        vp->v_type = VNON;
-#ifdef FDESC_DIAGNOSTIC
-       printf("fdesc_inactive(%x)\n", vp);
-#endif
        return (0);
 }
 
        return (0);
 }
 
+int
 fdesc_reclaim(ap)
        struct vop_reclaim_args /* {
                struct vnode *a_vp;
        } */ *ap;
 {
        struct vnode *vp = ap->a_vp;
 fdesc_reclaim(ap)
        struct vop_reclaim_args /* {
                struct vnode *a_vp;
        } */ *ap;
 {
        struct vnode *vp = ap->a_vp;
-       printf("fdesc_reclaim(%x)\n", vp);
-       if (vp->v_data) {
-               FREE(vp->v_data, M_TEMP);
-               vp->v_data = 0;
-       }
+
+       remque(VTOFDESC(vp));
+       FREE(vp->v_data, M_TEMP);
+       vp->v_data = 0;
+
        return (0);
 }
 
        return (0);
 }
 
+/*
+ * Return POSIX pathconf information applicable to special devices.
+ */
+fdesc_pathconf(ap)
+       struct vop_pathconf_args /* {
+               struct vnode *a_vp;
+               int a_name;
+               int *a_retval;
+       } */ *ap;
+{
+
+       switch (ap->a_name) {
+       case _PC_LINK_MAX:
+               *ap->a_retval = LINK_MAX;
+               return (0);
+       case _PC_MAX_CANON:
+               *ap->a_retval = MAX_CANON;
+               return (0);
+       case _PC_MAX_INPUT:
+               *ap->a_retval = MAX_INPUT;
+               return (0);
+       case _PC_PIPE_BUF:
+               *ap->a_retval = PIPE_BUF;
+               return (0);
+       case _PC_CHOWN_RESTRICTED:
+               *ap->a_retval = 1;
+               return (0);
+       case _PC_VDISABLE:
+               *ap->a_retval = _POSIX_VDISABLE;
+               return (0);
+       default:
+               return (EINVAL);
+       }
+       /* NOTREACHED */
+}
+
 /*
  * Print out the contents of a /dev/fd vnode.
  */
 /* ARGSUSED */
 /*
  * Print out the contents of a /dev/fd vnode.
  */
 /* ARGSUSED */
+int
 fdesc_print(ap)
        struct vop_print_args /* {
                struct vnode *a_vp;
 fdesc_print(ap)
        struct vop_print_args /* {
                struct vnode *a_vp;
@@ -399,6 +849,7 @@ fdesc_print(ap)
 }
 
 /*void*/
 }
 
 /*void*/
+int
 fdesc_vfree(ap)
        struct vop_vfree_args /* {
                struct vnode *a_pvp;
 fdesc_vfree(ap)
        struct vop_vfree_args /* {
                struct vnode *a_pvp;
@@ -413,6 +864,7 @@ fdesc_vfree(ap)
 /*
  * /dev/fd vnode unsupported operation
  */
 /*
  * /dev/fd vnode unsupported operation
  */
+int
 fdesc_enotsupp()
 {
 
 fdesc_enotsupp()
 {
 
@@ -422,6 +874,7 @@ fdesc_enotsupp()
 /*
  * /dev/fd "should never get here" operation
  */
 /*
  * /dev/fd "should never get here" operation
  */
+int
 fdesc_badop()
 {
 
 fdesc_badop()
 {
 
@@ -432,6 +885,7 @@ fdesc_badop()
 /*
  * /dev/fd vnode null operation
  */
 /*
  * /dev/fd vnode null operation
  */
+int
 fdesc_nullop()
 {
 
 fdesc_nullop()
 {
 
@@ -442,10 +896,6 @@ fdesc_nullop()
 #define fdesc_mknod ((int (*) __P((struct  vop_mknod_args *)))fdesc_enotsupp)
 #define fdesc_close ((int (*) __P((struct  vop_close_args *)))nullop)
 #define fdesc_access ((int (*) __P((struct  vop_access_args *)))nullop)
 #define fdesc_mknod ((int (*) __P((struct  vop_mknod_args *)))fdesc_enotsupp)
 #define fdesc_close ((int (*) __P((struct  vop_close_args *)))nullop)
 #define fdesc_access ((int (*) __P((struct  vop_access_args *)))nullop)
-#define fdesc_read ((int (*) __P((struct  vop_read_args *)))fdesc_enotsupp)
-#define fdesc_write ((int (*) __P((struct  vop_write_args *)))fdesc_enotsupp)
-#define fdesc_ioctl ((int (*) __P((struct  vop_ioctl_args *)))fdesc_enotsupp)
-#define fdesc_select ((int (*) __P((struct  vop_select_args *)))fdesc_enotsupp)
 #define fdesc_mmap ((int (*) __P((struct  vop_mmap_args *)))fdesc_enotsupp)
 #define fdesc_fsync ((int (*) __P((struct  vop_fsync_args *)))nullop)
 #define fdesc_seek ((int (*) __P((struct  vop_seek_args *)))nullop)
 #define fdesc_mmap ((int (*) __P((struct  vop_mmap_args *)))fdesc_enotsupp)
 #define fdesc_fsync ((int (*) __P((struct  vop_fsync_args *)))nullop)
 #define fdesc_seek ((int (*) __P((struct  vop_seek_args *)))nullop)
@@ -455,8 +905,6 @@ fdesc_nullop()
 #define fdesc_mkdir ((int (*) __P((struct  vop_mkdir_args *)))fdesc_enotsupp)
 #define fdesc_rmdir ((int (*) __P((struct  vop_rmdir_args *)))fdesc_enotsupp)
 #define fdesc_symlink ((int (*) __P((struct vop_symlink_args *)))fdesc_enotsupp)
 #define fdesc_mkdir ((int (*) __P((struct  vop_mkdir_args *)))fdesc_enotsupp)
 #define fdesc_rmdir ((int (*) __P((struct  vop_rmdir_args *)))fdesc_enotsupp)
 #define fdesc_symlink ((int (*) __P((struct vop_symlink_args *)))fdesc_enotsupp)
-#define fdesc_readlink
-       ((int (*) __P((struct  vop_readlink_args *)))fdesc_enotsupp)
 #define fdesc_abortop ((int (*) __P((struct  vop_abortop_args *)))nullop)
 #define fdesc_lock ((int (*) __P((struct  vop_lock_args *)))nullop)
 #define fdesc_unlock ((int (*) __P((struct  vop_unlock_args *)))nullop)
 #define fdesc_abortop ((int (*) __P((struct  vop_abortop_args *)))nullop)
 #define fdesc_lock ((int (*) __P((struct  vop_lock_args *)))nullop)
 #define fdesc_unlock ((int (*) __P((struct  vop_unlock_args *)))nullop)
@@ -464,7 +912,7 @@ fdesc_nullop()
 #define fdesc_strategy ((int (*) __P((struct  vop_strategy_args *)))fdesc_badop)
 #define fdesc_islocked ((int (*) __P((struct  vop_islocked_args *)))nullop)
 #define fdesc_advlock ((int (*) __P((struct vop_advlock_args *)))fdesc_enotsupp)
 #define fdesc_strategy ((int (*) __P((struct  vop_strategy_args *)))fdesc_badop)
 #define fdesc_islocked ((int (*) __P((struct  vop_islocked_args *)))nullop)
 #define fdesc_advlock ((int (*) __P((struct vop_advlock_args *)))fdesc_enotsupp)
-#define fdesc_blkatoff
+#define fdesc_blkatoff \
        ((int (*) __P((struct  vop_blkatoff_args *)))fdesc_enotsupp)
 #define fdesc_vget ((int (*) __P((struct  vop_vget_args *)))fdesc_enotsupp)
 #define fdesc_valloc ((int(*) __P(( \
        ((int (*) __P((struct  vop_blkatoff_args *)))fdesc_enotsupp)
 #define fdesc_vget ((int (*) __P((struct  vop_vget_args *)))fdesc_enotsupp)
 #define fdesc_valloc ((int(*) __P(( \
@@ -472,7 +920,7 @@ fdesc_nullop()
                int mode, \
                struct ucred *cred, \
                struct vnode **vpp))) fdesc_enotsupp)
                int mode, \
                struct ucred *cred, \
                struct vnode **vpp))) fdesc_enotsupp)
-#define fdesc_truncate
+#define fdesc_truncate \
        ((int (*) __P((struct  vop_truncate_args *)))fdesc_enotsupp)
 #define fdesc_update ((int (*) __P((struct  vop_update_args *)))fdesc_enotsupp)
 #define fdesc_bwrite ((int (*) __P((struct  vop_bwrite_args *)))fdesc_enotsupp)
        ((int (*) __P((struct  vop_truncate_args *)))fdesc_enotsupp)
 #define fdesc_update ((int (*) __P((struct  vop_update_args *)))fdesc_enotsupp)
 #define fdesc_bwrite ((int (*) __P((struct  vop_bwrite_args *)))fdesc_enotsupp)
@@ -512,6 +960,7 @@ struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
        { &vop_strategy_desc, fdesc_strategy }, /* strategy */
        { &vop_print_desc, fdesc_print },       /* print */
        { &vop_islocked_desc, fdesc_islocked }, /* islocked */
        { &vop_strategy_desc, fdesc_strategy }, /* strategy */
        { &vop_print_desc, fdesc_print },       /* print */
        { &vop_islocked_desc, fdesc_islocked }, /* islocked */
+       { &vop_pathconf_desc, fdesc_pathconf }, /* pathconf */
        { &vop_advlock_desc, fdesc_advlock },   /* advlock */
        { &vop_blkatoff_desc, fdesc_blkatoff }, /* blkatoff */
        { &vop_valloc_desc, fdesc_valloc },     /* valloc */
        { &vop_advlock_desc, fdesc_advlock },   /* advlock */
        { &vop_blkatoff_desc, fdesc_blkatoff }, /* blkatoff */
        { &vop_valloc_desc, fdesc_valloc },     /* valloc */