BSD 4_3_Reno release
[unix-history] / usr / src / sys / nfs / nfs_vnops.c
index 8aad016..11f2c9a 100644 (file)
@@ -5,19 +5,22 @@
  * This code is derived from software contributed to Berkeley by
  * Rick Macklem at The University of Guelph.
  *
  * This code is derived from software contributed to Berkeley by
  * Rick Macklem at The University of Guelph.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution is only permitted until one year after the first shipment
+ * of 4.4BSD by the Regents.  Otherwise, redistribution and use in source and
+ * binary forms are permitted provided that: (1) source distributions retain
+ * this entire copyright notice and comment, and (2) distributions including
+ * binaries display the following acknowledgement:  This product includes
+ * software developed by the University of California, Berkeley and its
+ * contributors'' in the documentation or other materials provided with the
+ * distribution and in all advertising materials mentioning features or use
+ * of this software.  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 AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
  *
- *     @(#)nfs_vnops.c 7.22 (Berkeley) %G%
+ *     @(#)nfs_vnops.c 7.46 (Berkeley) 7/26/90
  */
 
 /*
  */
 
 /*
 
 #include "machine/pte.h"
 #include "machine/mtpr.h"
 
 #include "machine/pte.h"
 #include "machine/mtpr.h"
-#include "strings.h"
 #include "param.h"
 #include "user.h"
 #include "proc.h"
 #include "param.h"
 #include "user.h"
 #include "proc.h"
+#include "kernel.h"
 #include "mount.h"
 #include "buf.h"
 #include "vm.h"
 #include "mount.h"
 #include "buf.h"
 #include "vm.h"
-#include "../ufs/dir.h"
 #include "malloc.h"
 #include "mbuf.h"
 #include "malloc.h"
 #include "mbuf.h"
-#include "uio.h"
-#include "ucred.h"
-#include "namei.h"
 #include "errno.h"
 #include "file.h"
 #include "conf.h"
 #include "vnode.h"
 #include "errno.h"
 #include "file.h"
 #include "conf.h"
 #include "vnode.h"
-#include "../ufs/inode.h"
+#include "text.h"
 #include "map.h"
 #include "map.h"
+#include "../ufs/quota.h"
+#include "../ufs/inode.h"
 #include "nfsv2.h"
 #include "nfs.h"
 #include "nfsnode.h"
 #include "nfsv2.h"
 #include "nfs.h"
 #include "nfsnode.h"
@@ -86,7 +87,8 @@ int   nfs_lookup(),
        nfs_fsync(),
        nfs_inactive(),
        nfs_reclaim(),
        nfs_fsync(),
        nfs_inactive(),
        nfs_reclaim(),
-       nfs_print();
+       nfs_print(),
+       nfs_islocked();
 
 struct vnodeops nfsv2_vnodeops = {
        nfs_lookup,             /* lookup */
 
 struct vnodeops nfsv2_vnodeops = {
        nfs_lookup,             /* lookup */
@@ -120,6 +122,7 @@ struct vnodeops nfsv2_vnodeops = {
        nfs_bmap,               /* bmap */
        nfs_strategy,           /* strategy */
        nfs_print,              /* print */
        nfs_bmap,               /* bmap */
        nfs_strategy,           /* strategy */
        nfs_print,              /* print */
+       nfs_islocked,           /* islocked */
 };
 
 /* Special device vnode ops */
 };
 
 /* Special device vnode ops */
@@ -167,16 +170,65 @@ struct vnodeops spec_nfsv2nodeops = {
        spec_bmap,              /* bmap */
        spec_strategy,          /* strategy */
        nfs_print,              /* print */
        spec_bmap,              /* bmap */
        spec_strategy,          /* strategy */
        nfs_print,              /* print */
+       nfs_islocked,           /* islocked */
 };
 
 };
 
+#ifdef FIFO
+int    fifo_lookup(),
+       fifo_open(),
+       fifo_read(),
+       fifo_write(),
+       fifo_bmap(),
+       fifo_ioctl(),
+       fifo_select(),
+       fifo_close(),
+       fifo_print(),
+       fifo_badop(),
+       fifo_nullop();
+
+struct vnodeops fifo_nfsv2nodeops = {
+       fifo_lookup,            /* lookup */
+       fifo_badop,             /* create */
+       fifo_badop,             /* mknod */
+       fifo_open,              /* open */
+       fifo_close,             /* close */
+       nfs_access,             /* access */
+       nfs_getattr,            /* getattr */
+       nfs_setattr,            /* setattr */
+       fifo_read,              /* read */
+       fifo_write,             /* write */
+       fifo_ioctl,             /* ioctl */
+       fifo_select,            /* select */
+       fifo_badop,             /* mmap */
+       fifo_nullop,            /* fsync */
+       fifo_badop,             /* seek */
+       fifo_badop,             /* remove */
+       fifo_badop,             /* link */
+       fifo_badop,             /* rename */
+       fifo_badop,             /* mkdir */
+       fifo_badop,             /* rmdir */
+       fifo_badop,             /* symlink */
+       fifo_badop,             /* readdir */
+       fifo_badop,             /* readlink */
+       fifo_badop,             /* abortop */
+       nfs_inactive,           /* inactive */
+       nfs_reclaim,            /* reclaim */
+       nfs_lock,               /* lock */
+       nfs_unlock,             /* unlock */
+       fifo_bmap,              /* bmap */
+       fifo_badop,             /* strategy */
+       nfs_print,              /* print */
+       nfs_islocked,           /* islocked */
+};
+#endif /* FIFO */
+
 extern u_long nfs_procids[NFS_NPROCS];
 extern u_long nfs_prog, nfs_vers;
 extern char nfsiobuf[MAXPHYS+NBPG];
 struct map nfsmap[NFS_MSIZ];
 extern u_long nfs_procids[NFS_NPROCS];
 extern u_long nfs_prog, nfs_vers;
 extern char nfsiobuf[MAXPHYS+NBPG];
 struct map nfsmap[NFS_MSIZ];
-enum vtype v_type[NFLNK+1];
 struct buf nfs_bqueue;         /* Queue head for nfsiod's */
 int nfs_asyncdaemons = 0;
 struct buf nfs_bqueue;         /* Queue head for nfsiod's */
 int nfs_asyncdaemons = 0;
-struct proc *nfs_iodwant[MAX_ASYNCDAEMON];
+struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
 static int nfsmap_want = 0;
 
 /*
 static int nfsmap_want = 0;
 
 /*
@@ -192,7 +244,7 @@ nfs_null(vp, cred)
        struct mbuf *mreq, *mrep, *md, *mb;
        
        nfsm_reqhead(nfs_procids[NFSPROC_NULL], cred, 0);
        struct mbuf *mreq, *mrep, *md, *mb;
        
        nfsm_reqhead(nfs_procids[NFSPROC_NULL], cred, 0);
-       nfsm_request(vp);
+       nfsm_request(vp, NFSPROC_NULL, u.u_procp, 0);
        nfsm_reqdone;
        return (error);
 }
        nfsm_reqdone;
        return (error);
 }
@@ -219,7 +271,7 @@ nfs_access(vp, mode, cred)
        if (cred->cr_uid == 0)
                return (0);
        vap = &vattr;
        if (cred->cr_uid == 0)
                return (0);
        vap = &vattr;
-       if (error = nfs_getattr(vp, vap, cred))
+       if (error = nfs_dogetattr(vp, vap, cred, 0))
                return (error);
        /*
         * Access check is based on only one of owner, group, public.
                return (error);
        /*
         * Access check is based on only one of owner, group, public.
@@ -273,11 +325,11 @@ nfs_close(vp, fflags, cred)
        register struct nfsnode *np = VTONFS(vp);
        int error = 0;
 
        register struct nfsnode *np = VTONFS(vp);
        int error = 0;
 
-       if (vp->v_type == VREG && ((np->n_flag & NMODIFIED) ||
-          ((np->n_flag & NBUFFERED) && np->n_sillyrename))) {
+       if (vp->v_type == VREG && (np->n_flag & NMODIFIED)) {
                nfs_lock(vp);
                nfs_lock(vp);
-               np->n_flag &= ~(NMODIFIED|NBUFFERED);
+               np->n_flag &= ~NMODIFIED;
                vinvalbuf(vp, TRUE);
                vinvalbuf(vp, TRUE);
+               np->n_attrstamp = 0;
                if (np->n_flag & NWRITEERR) {
                        np->n_flag &= ~NWRITEERR;
                        error = np->n_error;
                if (np->n_flag & NWRITEERR) {
                        np->n_flag &= ~NWRITEERR;
                        error = np->n_error;
@@ -294,6 +346,15 @@ nfs_getattr(vp, vap, cred)
        register struct vnode *vp;
        struct vattr *vap;
        struct ucred *cred;
        register struct vnode *vp;
        struct vattr *vap;
        struct ucred *cred;
+{
+       return (nfs_dogetattr(vp, vap, cred, 0));
+}
+
+nfs_dogetattr(vp, vap, cred, tryhard)
+       register struct vnode *vp;
+       struct vattr *vap;
+       struct ucred *cred;
+       int tryhard;
 {
        register caddr_t cp;
        register long t1;
 {
        register caddr_t cp;
        register long t1;
@@ -308,7 +369,7 @@ nfs_getattr(vp, vap, cred)
        nfsstats.rpccnt[NFSPROC_GETATTR]++;
        nfsm_reqhead(nfs_procids[NFSPROC_GETATTR], cred, NFSX_FH);
        nfsm_fhtom(vp);
        nfsstats.rpccnt[NFSPROC_GETATTR]++;
        nfsm_reqhead(nfs_procids[NFSPROC_GETATTR], cred, NFSX_FH);
        nfsm_fhtom(vp);
-       nfsm_request(vp);
+       nfsm_request(vp, NFSPROC_GETATTR, u.u_procp, tryhard);
        nfsm_loadattr(vp, vap);
        nfsm_reqdone;
        return (error);
        nfsm_loadattr(vp, vap);
        nfsm_reqdone;
        return (error);
@@ -348,17 +409,19 @@ nfs_setattr(vp, vap, cred)
        else
                sp->sa_gid = txdr_unsigned(vap->va_gid);
        sp->sa_size = txdr_unsigned(vap->va_size);
        else
                sp->sa_gid = txdr_unsigned(vap->va_gid);
        sp->sa_size = txdr_unsigned(vap->va_size);
-       if (vap->va_size != VNOVAL) {
+       sp->sa_atime.tv_sec = txdr_unsigned(vap->va_atime.tv_sec);
+       sp->sa_atime.tv_usec = txdr_unsigned(vap->va_flags);
+       txdr_time(&vap->va_mtime, &sp->sa_mtime);
+       if (vap->va_size != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
+           vap->va_atime.tv_sec != VNOVAL) {
                np = VTONFS(vp);
                if (np->n_flag & NMODIFIED) {
                        np->n_flag &= ~NMODIFIED;
                        vinvalbuf(vp, TRUE);
                np = VTONFS(vp);
                if (np->n_flag & NMODIFIED) {
                        np->n_flag &= ~NMODIFIED;
                        vinvalbuf(vp, TRUE);
+                       np->n_attrstamp = 0;
                }
        }
                }
        }
-       sp->sa_atime.tv_sec = txdr_unsigned(vap->va_atime.tv_sec);
-       sp->sa_atime.tv_usec = txdr_unsigned(vap->va_flags);
-       txdr_time(&vap->va_mtime, &sp->sa_mtime);
-       nfsm_request(vp);
+       nfsm_request(vp, NFSPROC_SETATTR, u.u_procp, 1);
        nfsm_loadattr(vp, (struct vattr *)0);
        /* should we fill in any vap fields ?? */
        nfsm_reqdone;
        nfsm_loadattr(vp, (struct vattr *)0);
        /* should we fill in any vap fields ?? */
        nfsm_reqdone;
@@ -417,15 +480,20 @@ nfs_lookup(vp, ndp)
                        nfs_unlock(vp);
                }
                if (!error) {
                        nfs_unlock(vp);
                }
                if (!error) {
-                       if (vpid == vdp->v_id &&
-                          !nfs_getattr(vdp, &vattr, ndp->ni_cred)) {
+                       if (vpid == vdp->v_id) {
+                          if (!nfs_dogetattr(vdp, &vattr, ndp->ni_cred, 0) &&
+                              vattr.va_ctime.tv_sec == VTONFS(vdp)->n_ctime) {
                                nfsstats.lookupcache_hits++;
                                return (0);
                                nfsstats.lookupcache_hits++;
                                return (0);
+                          } else {
+                               cache_purge(vdp);
+                               nfs_nput(vdp);
+                          }
                        } else {
                                nfs_nput(vdp);
                        }
                }
                        } else {
                                nfs_nput(vdp);
                        }
                }
-               ndp->ni_vp = (struct vnode *)0;
+               ndp->ni_vp = NULLVP;
        } else
                nfs_unlock(vp);
        error = 0;
        } else
                nfs_unlock(vp);
        error = 0;
@@ -435,13 +503,13 @@ nfs_lookup(vp, ndp)
        nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
        nfsm_fhtom(vp);
        nfsm_strtom(ndp->ni_ptr, len, NFS_MAXNAMLEN);
        nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
        nfsm_fhtom(vp);
        nfsm_strtom(ndp->ni_ptr, len, NFS_MAXNAMLEN);
-       nfsm_request(vp);
+       nfsm_request(vp, NFSPROC_LOOKUP, u.u_procp, 0);
 nfsmout:
        if (error) {
                if (lockparent || (flag != CREATE && flag != RENAME) ||
                    *ndp->ni_next != 0)
                        nfs_lock(vp);
 nfsmout:
        if (error) {
                if (lockparent || (flag != CREATE && flag != RENAME) ||
                    *ndp->ni_next != 0)
                        nfs_lock(vp);
-               return (ENOENT);
+               return (error);
        }
        nfsm_disect(fhp,nfsv2fh_t *,NFSX_FH);
 
        }
        nfsm_disect(fhp,nfsv2fh_t *,NFSX_FH);
 
@@ -536,18 +604,50 @@ nfsmout:
        if (vp == newvp || (lockparent && *ndp->ni_next == '\0'))
                nfs_lock(vp);
        ndp->ni_vp = newvp;
        if (vp == newvp || (lockparent && *ndp->ni_next == '\0'))
                nfs_lock(vp);
        ndp->ni_vp = newvp;
-       if (error == 0 && ndp->ni_makeentry)
+       if (error == 0 && ndp->ni_makeentry) {
+               np->n_ctime = np->n_vattr.va_ctime.tv_sec;
                cache_enter(ndp);
                cache_enter(ndp);
+       }
        return (error);
 }
 
        return (error);
 }
 
+/*
+ * nfs read call.
+ * Just call nfs_bioread() to do the work.
+ */
+nfs_read(vp, uiop, ioflag, cred)
+       register struct vnode *vp;
+       struct uio *uiop;
+       int ioflag;
+       struct ucred *cred;
+{
+       if (vp->v_type != VREG)
+               return (EPERM);
+       return (nfs_bioread(vp, uiop, ioflag, cred));
+}
+
 /*
  * nfs readlink call
  */
 nfs_readlink(vp, uiop, cred)
 /*
  * nfs readlink call
  */
 nfs_readlink(vp, uiop, cred)
+       struct vnode *vp;
+       struct uio *uiop;
+       struct ucred *cred;
+{
+       if (vp->v_type != VLNK)
+               return (EPERM);
+       return (nfs_bioread(vp, uiop, 0, cred));
+}
+
+/*
+ * Do a readlink rpc.
+ * Called by nfs_doio() from below the buffer cache.
+ */
+nfs_readlinkrpc(vp, uiop, cred, procp)
        register struct vnode *vp;
        struct uio *uiop;
        struct ucred *cred;
        register struct vnode *vp;
        struct uio *uiop;
        struct ucred *cred;
+       struct proc *procp;
 {
        register u_long *p;
        register caddr_t cp;
 {
        register u_long *p;
        register caddr_t cp;
@@ -559,22 +659,26 @@ nfs_readlink(vp, uiop, cred)
        long len;
 
        nfsstats.rpccnt[NFSPROC_READLINK]++;
        long len;
 
        nfsstats.rpccnt[NFSPROC_READLINK]++;
+       nfs_unlock(vp);
        nfsm_reqhead(nfs_procids[NFSPROC_READLINK], cred, NFSX_FH);
        nfsm_fhtom(vp);
        nfsm_reqhead(nfs_procids[NFSPROC_READLINK], cred, NFSX_FH);
        nfsm_fhtom(vp);
-       nfsm_request(vp);
+       nfsm_request(vp, NFSPROC_READLINK, procp, 0);
        nfsm_strsiz(len, NFS_MAXPATHLEN);
        nfsm_mtouio(uiop, len);
        nfsm_reqdone;
        nfsm_strsiz(len, NFS_MAXPATHLEN);
        nfsm_mtouio(uiop, len);
        nfsm_reqdone;
+       nfs_lock(vp);
        return (error);
 }
 
 /*
        return (error);
 }
 
 /*
- * nfs read call
+ * nfs read rpc call
+ * Ditto above
  */
  */
-nfs_readrpc(vp, uiop, cred)
+nfs_readrpc(vp, uiop, cred, procp)
        register struct vnode *vp;
        struct uio *uiop;
        struct ucred *cred;
        register struct vnode *vp;
        struct uio *uiop;
        struct ucred *cred;
+       struct proc *procp;
 {
        register u_long *p;
        register caddr_t cp;
 {
        register u_long *p;
        register caddr_t cp;
@@ -586,7 +690,7 @@ nfs_readrpc(vp, uiop, cred)
        struct nfsmount *nmp;
        long len, retlen, tsiz;
 
        struct nfsmount *nmp;
        long len, retlen, tsiz;
 
-       nmp = vfs_to_nfs(vp->v_mount);
+       nmp = VFSTONFS(vp->v_mount);
        tsiz = uiop->uio_resid;
        while (tsiz > 0) {
                nfsstats.rpccnt[NFSPROC_READ]++;
        tsiz = uiop->uio_resid;
        while (tsiz > 0) {
                nfsstats.rpccnt[NFSPROC_READ]++;
@@ -597,7 +701,7 @@ nfs_readrpc(vp, uiop, cred)
                *p++ = txdr_unsigned(uiop->uio_offset);
                *p++ = txdr_unsigned(len);
                *p = 0;
                *p++ = txdr_unsigned(uiop->uio_offset);
                *p++ = txdr_unsigned(len);
                *p = 0;
-               nfsm_request(vp);
+               nfsm_request(vp, NFSPROC_READ, procp, 1);
                nfsm_loadattr(vp, (struct vattr *)0);
                nfsm_strsiz(retlen, nmp->nm_rsize);
                nfsm_mtouio(uiop, retlen);
                nfsm_loadattr(vp, (struct vattr *)0);
                nfsm_strsiz(retlen, nmp->nm_rsize);
                nfsm_mtouio(uiop, retlen);
@@ -614,10 +718,11 @@ nfsmout:
 /*
  * nfs write call
  */
 /*
  * nfs write call
  */
-nfs_writerpc(vp, uiop, cred)
+nfs_writerpc(vp, uiop, cred, procp)
        register struct vnode *vp;
        struct uio *uiop;
        struct ucred *cred;
        register struct vnode *vp;
        struct uio *uiop;
        struct ucred *cred;
+       struct proc *procp;
 {
        register u_long *p;
        register caddr_t cp;
 {
        register u_long *p;
        register caddr_t cp;
@@ -629,7 +734,7 @@ nfs_writerpc(vp, uiop, cred)
        struct nfsmount *nmp;
        long len, tsiz;
 
        struct nfsmount *nmp;
        long len, tsiz;
 
-       nmp = vfs_to_nfs(vp->v_mount);
+       nmp = VFSTONFS(vp->v_mount);
        tsiz = uiop->uio_resid;
        while (tsiz > 0) {
                nfsstats.rpccnt[NFSPROC_WRITE]++;
        tsiz = uiop->uio_resid;
        while (tsiz > 0) {
                nfsstats.rpccnt[NFSPROC_WRITE]++;
@@ -641,7 +746,7 @@ nfs_writerpc(vp, uiop, cred)
                *(p+1) = txdr_unsigned(uiop->uio_offset);
                *(p+3) = txdr_unsigned(len);
                nfsm_uiotom(uiop, len);
                *(p+1) = txdr_unsigned(uiop->uio_offset);
                *(p+3) = txdr_unsigned(len);
                nfsm_uiotom(uiop, len);
-               nfsm_request(vp);
+               nfsm_request(vp, NFSPROC_WRITE, procp, 1);
                nfsm_loadattr(vp, (struct vattr *)0);
                m_freem(mrep);
                tsiz -= len;
                nfsm_loadattr(vp, (struct vattr *)0);
                m_freem(mrep);
                tsiz -= len;
@@ -652,17 +757,54 @@ nfsmout:
 
 /*
  * nfs mknod call
 
 /*
  * nfs mknod call
- * This call is currently not supported.
+ * This is a kludge. Use a create rpc but with the IFMT bits of the mode
+ * set to specify the file type and the size field for rdev.
  */
 /* ARGSUSED */
 nfs_mknod(ndp, vap, cred)
        struct nameidata *ndp;
        struct ucred *cred;
  */
 /* ARGSUSED */
 nfs_mknod(ndp, vap, cred)
        struct nameidata *ndp;
        struct ucred *cred;
-       struct vattr *vap;
+       register struct vattr *vap;
 {
 {
-
-       nfs_abortop(ndp);
-       return (EOPNOTSUPP);
+       register struct nfsv2_sattr *sp;
+       register u_long *p;
+       register caddr_t cp;
+       register long t1, t2;
+       caddr_t bpos, dpos;
+       u_long xid;
+       int error = 0;
+       struct mbuf *mreq, *mrep, *md, *mb, *mb2;
+       u_long rdev;
+
+       if (vap->va_type == VCHR || vap->va_type == VBLK)
+               rdev = txdr_unsigned(vap->va_rdev);
+#ifdef FIFO
+       else if (vap->va_type == VFIFO)
+               rdev = 0xffffffff;
+#endif /* FIFO */
+       else {
+               VOP_ABORTOP(ndp);
+               vput(ndp->ni_dvp);
+               return (EOPNOTSUPP);
+       }
+       nfsstats.rpccnt[NFSPROC_CREATE]++;
+       nfsm_reqhead(nfs_procids[NFSPROC_CREATE], ndp->ni_cred,
+         NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR);
+       nfsm_fhtom(ndp->ni_dvp);
+       nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
+       nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
+       sp->sa_mode = vtonfs_mode(vap->va_type, vap->va_mode);
+       sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
+       sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
+       sp->sa_size = rdev;
+       /* or should these be VNOVAL ?? */
+       txdr_time(&vap->va_atime, &sp->sa_atime);
+       txdr_time(&vap->va_mtime, &sp->sa_mtime);
+       nfsm_request(ndp->ni_dvp, NFSPROC_CREATE, u.u_procp, 1);
+       nfsm_reqdone;
+       VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
+       nfs_nput(ndp->ni_dvp);
+       return (error);
 }
 
 /*
 }
 
 /*
@@ -694,26 +836,20 @@ nfs_create(ndp, vap)
        /* or should these be VNOVAL ?? */
        txdr_time(&vap->va_atime, &sp->sa_atime);
        txdr_time(&vap->va_mtime, &sp->sa_mtime);
        /* or should these be VNOVAL ?? */
        txdr_time(&vap->va_atime, &sp->sa_atime);
        txdr_time(&vap->va_mtime, &sp->sa_mtime);
-       nfsm_request(ndp->ni_dvp);
+       nfsm_request(ndp->ni_dvp, NFSPROC_CREATE, u.u_procp, 1);
        nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
        nfsm_reqdone;
        nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
        nfsm_reqdone;
+       VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
        nfs_nput(ndp->ni_dvp);
        return (error);
 }
 
 /*
  * nfs file remove call
        nfs_nput(ndp->ni_dvp);
        return (error);
 }
 
 /*
  * nfs file remove call
- * To try and make nfs semantics closer to vfs semantics, a file that has
- * other references to the vnode is renamed instead of removed and then
+ * To try and make nfs semantics closer to ufs semantics, a file that has
+ * other processes using the vnode is renamed instead of removed and then
  * removed later on the last close.
  * removed later on the last close.
- * Unfortunately you must flush the buffer cache and cmap to get rid of
- * all extraneous vnode references before you check the reference cnt.
- * 1 - If the file could have blocks in the buffer cache
- *       flush them out and invalidate them
- *       mpurge the vnode to flush out cmap references
- *       (This is necessary to update the vnode ref cnt as well as sensible
- *        for actual removes, to free up the buffers)
- * 2 - If v_usecount > 1
+ * - If v_usecount > 1
  *       If a rename is not already in the works
  *          call nfs_sillyrename() to set it up
  *     else
  *       If a rename is not already in the works
  *          call nfs_sillyrename() to set it up
  *     else
@@ -732,16 +868,6 @@ nfs_remove(ndp)
        int error = 0;
        struct mbuf *mreq, *mrep, *md, *mb, *mb2;
 
        int error = 0;
        struct mbuf *mreq, *mrep, *md, *mb, *mb2;
 
-       if (vp->v_type == VREG) {
-               if (np->n_flag & (NMODIFIED|NBUFFERED)) {
-                       np->n_flag &= ~(NMODIFIED|NBUFFERED);
-                       vinvalbuf(vp, TRUE);
-               }
-               if (np->n_flag & NPAGEDON) {
-                       np->n_flag &= ~NPAGEDON;
-                       mpurge(vp);     /* In case cmap entries still ref it */
-               }
-       }
        if (vp->v_usecount > 1) {
                if (!np->n_sillyrename)
                        error = nfs_sillyrename(ndp, REMOVE);
        if (vp->v_usecount > 1) {
                if (!np->n_sillyrename)
                        error = nfs_sillyrename(ndp, REMOVE);
@@ -751,8 +877,9 @@ nfs_remove(ndp)
                        NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
                nfsm_fhtom(ndp->ni_dvp);
                nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
                        NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
                nfsm_fhtom(ndp->ni_dvp);
                nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
-               nfsm_request(ndp->ni_dvp);
+               nfsm_request(ndp->ni_dvp, NFSPROC_REMOVE, u.u_procp, 1);
                nfsm_reqdone;
                nfsm_reqdone;
+               VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
                /*
                 * Kludge City: If the first reply to the remove rpc is lost..
                 *   the reply to the retransmitted request will be ENOENT
                /*
                 * Kludge City: If the first reply to the remove rpc is lost..
                 *   the reply to the retransmitted request will be ENOENT
@@ -762,11 +889,12 @@ nfs_remove(ndp)
                if (error == ENOENT)
                        error = 0;
        }
                if (error == ENOENT)
                        error = 0;
        }
-       if (ndp->ni_dvp == ndp->ni_vp)
-               vrele(ndp->ni_vp);
+       np->n_attrstamp = 0;
+       if (ndp->ni_dvp == vp)
+               vrele(vp);
        else
        else
-               nfs_nput(ndp->ni_vp);
-       nfs_nput(ndp->ni_dvp);
+               nfs_nput(ndp->ni_dvp);
+       nfs_nput(vp);
        return (error);
 }
 
        return (error);
 }
 
@@ -789,8 +917,9 @@ nfs_removeit(ndp)
                NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
        nfsm_fhtom(ndp->ni_dvp);
        nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
                NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
        nfsm_fhtom(ndp->ni_dvp);
        nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
-       nfsm_request(ndp->ni_dvp);
+       nfsm_request(ndp->ni_dvp, NFSPROC_REMOVE, u.u_procp, 1);
        nfsm_reqdone;
        nfsm_reqdone;
+       VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
        return (error);
 }
 
        return (error);
 }
 
@@ -816,20 +945,35 @@ nfs_rename(sndp, tndp)
        nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
        nfsm_fhtom(tndp->ni_dvp);
        nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
        nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
        nfsm_fhtom(tndp->ni_dvp);
        nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
-       nfsm_request(sndp->ni_dvp);
+       nfsm_request(sndp->ni_dvp, NFSPROC_RENAME, u.u_procp, 1);
        nfsm_reqdone;
        nfsm_reqdone;
+       VTONFS(sndp->ni_dvp)->n_flag |= NMODIFIED;
+       VTONFS(tndp->ni_dvp)->n_flag |= NMODIFIED;
        if (sndp->ni_vp->v_type == VDIR) {
                if (tndp->ni_vp != NULL && tndp->ni_vp->v_type == VDIR)
                        cache_purge(tndp->ni_dvp);
                cache_purge(sndp->ni_dvp);
        }
        if (sndp->ni_vp->v_type == VDIR) {
                if (tndp->ni_vp != NULL && tndp->ni_vp->v_type == VDIR)
                        cache_purge(tndp->ni_dvp);
                cache_purge(sndp->ni_dvp);
        }
-       nfs_abortop(sndp);
-       nfs_abortop(tndp);
+       VOP_ABORTOP(tndp);
+       if (tndp->ni_dvp == tndp->ni_vp)
+               vrele(tndp->ni_dvp);
+       else
+               vput(tndp->ni_dvp);
+       if (tndp->ni_vp)
+               vput(tndp->ni_vp);
+       VOP_ABORTOP(sndp);
+       vrele(sndp->ni_dvp);
+       vrele(sndp->ni_vp);
+       /*
+        * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
+        */
+       if (error == ENOENT)
+               error = 0;
        return (error);
 }
 
 /*
        return (error);
 }
 
 /*
- * nfs file rename rpc called from above
+ * nfs file rename rpc called from nfs_remove() above
  */
 nfs_renameit(sndp, tndp)
        register struct nameidata *sndp, *tndp;
  */
 nfs_renameit(sndp, tndp)
        register struct nameidata *sndp, *tndp;
@@ -850,8 +994,10 @@ nfs_renameit(sndp, tndp)
        nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
        nfsm_fhtom(tndp->ni_dvp);
        nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
        nfsm_strtom(sndp->ni_dent.d_name,sndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
        nfsm_fhtom(tndp->ni_dvp);
        nfsm_strtom(tndp->ni_dent.d_name,tndp->ni_dent.d_namlen,NFS_MAXNAMLEN);
-       nfsm_request(sndp->ni_dvp);
+       nfsm_request(sndp->ni_dvp, NFSPROC_RENAME, u.u_procp, 1);
        nfsm_reqdone;
        nfsm_reqdone;
+       VTONFS(sndp->ni_dvp)->n_flag |= NMODIFIED;
+       VTONFS(tndp->ni_dvp)->n_flag |= NMODIFIED;
        return (error);
 }
 
        return (error);
 }
 
@@ -878,11 +1024,18 @@ nfs_link(vp, ndp)
        nfsm_fhtom(vp);
        nfsm_fhtom(ndp->ni_dvp);
        nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
        nfsm_fhtom(vp);
        nfsm_fhtom(ndp->ni_dvp);
        nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
-       nfsm_request(vp);
+       nfsm_request(vp, NFSPROC_LINK, u.u_procp, 1);
        nfsm_reqdone;
        nfsm_reqdone;
+       VTONFS(vp)->n_attrstamp = 0;
+       VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
        if (ndp->ni_dvp != vp)
                nfs_unlock(vp);
        nfs_nput(ndp->ni_dvp);
        if (ndp->ni_dvp != vp)
                nfs_unlock(vp);
        nfs_nput(ndp->ni_dvp);
+       /*
+        * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
+        */
+       if (error == EEXIST)
+               error = 0;
        return (error);
 }
 
        return (error);
 }
 
@@ -914,11 +1067,17 @@ nfs_symlink(ndp, vap, nm)
        sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
        sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
        sp->sa_size = txdr_unsigned(VNOVAL);
        sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
        sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
        sp->sa_size = txdr_unsigned(VNOVAL);
-       txdr_time(&vap->va_atime, &sp->sa_atime);               /* or VNOVAL ?? */
+       txdr_time(&vap->va_atime, &sp->sa_atime);       /* or VNOVAL ?? */
        txdr_time(&vap->va_mtime, &sp->sa_mtime);       /* or VNOVAL ?? */
        txdr_time(&vap->va_mtime, &sp->sa_mtime);       /* or VNOVAL ?? */
-       nfsm_request(ndp->ni_dvp);
+       nfsm_request(ndp->ni_dvp, NFSPROC_SYMLINK, u.u_procp, 1);
        nfsm_reqdone;
        nfsm_reqdone;
+       VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
        nfs_nput(ndp->ni_dvp);
        nfs_nput(ndp->ni_dvp);
+       /*
+        * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
+        */
+       if (error == EEXIST)
+               error = 0;
        return (error);
 }
 
        return (error);
 }
 
@@ -933,26 +1092,52 @@ nfs_mkdir(ndp, vap)
        register u_long *p;
        register caddr_t cp;
        register long t1, t2;
        register u_long *p;
        register caddr_t cp;
        register long t1, t2;
+       register int len;
        caddr_t bpos, dpos, cp2;
        u_long xid;
        caddr_t bpos, dpos, cp2;
        u_long xid;
-       int error = 0;
+       int error = 0, firsttry = 1;
        struct mbuf *mreq, *mrep, *md, *mb, *mb2;
 
        struct mbuf *mreq, *mrep, *md, *mb, *mb2;
 
+       len = ndp->ni_dent.d_namlen;
        nfsstats.rpccnt[NFSPROC_MKDIR]++;
        nfsm_reqhead(nfs_procids[NFSPROC_MKDIR], ndp->ni_cred,
        nfsstats.rpccnt[NFSPROC_MKDIR]++;
        nfsm_reqhead(nfs_procids[NFSPROC_MKDIR], ndp->ni_cred,
-         NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen)+NFSX_SATTR);
+         NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len)+NFSX_SATTR);
        nfsm_fhtom(ndp->ni_dvp);
        nfsm_fhtom(ndp->ni_dvp);
-       nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
+       nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN);
        nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
        sp->sa_mode = vtonfs_mode(VDIR, vap->va_mode);
        sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
        sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
        sp->sa_size = txdr_unsigned(VNOVAL);
        nfsm_build(sp, struct nfsv2_sattr *, NFSX_SATTR);
        sp->sa_mode = vtonfs_mode(VDIR, vap->va_mode);
        sp->sa_uid = txdr_unsigned(ndp->ni_cred->cr_uid);
        sp->sa_gid = txdr_unsigned(ndp->ni_cred->cr_gid);
        sp->sa_size = txdr_unsigned(VNOVAL);
-       txdr_time(&vap->va_atime, &sp->sa_atime);               /* or VNOVAL ?? */
+       txdr_time(&vap->va_atime, &sp->sa_atime);       /* or VNOVAL ?? */
        txdr_time(&vap->va_mtime, &sp->sa_mtime);       /* or VNOVAL ?? */
        txdr_time(&vap->va_mtime, &sp->sa_mtime);       /* or VNOVAL ?? */
-       nfsm_request(ndp->ni_dvp);
+       nfsm_request(ndp->ni_dvp, NFSPROC_MKDIR, u.u_procp, 1);
        nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
        nfsm_reqdone;
        nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
        nfsm_reqdone;
+       VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
+       /*
+        * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
+        * if we can succeed in looking up the directory.
+        * "firsttry" is necessary since the macros may "goto nfsmout" which
+        * is above the if on errors. (Ugh)
+        */
+       if (error == EEXIST && firsttry) {
+               firsttry = 0;
+               error = 0;
+               nfsstats.rpccnt[NFSPROC_LOOKUP]++;
+               ndp->ni_vp = NULL;
+               nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred,
+                   NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
+               nfsm_fhtom(ndp->ni_dvp);
+               nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN);
+               nfsm_request(ndp->ni_dvp, NFSPROC_LOOKUP, u.u_procp, 1);
+               nfsm_mtofh(ndp->ni_dvp, ndp->ni_vp);
+               if (ndp->ni_vp->v_type != VDIR) {
+                       vput(ndp->ni_vp);
+                       error = EEXIST;
+               }
+               m_freem(mrep);
+       }
        nfs_nput(ndp->ni_dvp);
        return (error);
 }
        nfs_nput(ndp->ni_dvp);
        return (error);
 }
@@ -981,12 +1166,18 @@ nfs_rmdir(ndp)
                NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
        nfsm_fhtom(ndp->ni_dvp);
        nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
                NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(ndp->ni_dent.d_namlen));
        nfsm_fhtom(ndp->ni_dvp);
        nfsm_strtom(ndp->ni_dent.d_name, ndp->ni_dent.d_namlen, NFS_MAXNAMLEN);
-       nfsm_request(ndp->ni_dvp);
+       nfsm_request(ndp->ni_dvp, NFSPROC_RMDIR, u.u_procp, 1);
        nfsm_reqdone;
        nfsm_reqdone;
+       VTONFS(ndp->ni_dvp)->n_flag |= NMODIFIED;
        cache_purge(ndp->ni_dvp);
        cache_purge(ndp->ni_vp);
        nfs_nput(ndp->ni_vp);
        nfs_nput(ndp->ni_dvp);
        cache_purge(ndp->ni_dvp);
        cache_purge(ndp->ni_vp);
        nfs_nput(ndp->ni_vp);
        nfs_nput(ndp->ni_dvp);
+       /*
+        * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
+        */
+       if (error == ENOENT)
+               error = 0;
        return (error);
 }
 
        return (error);
 }
 
@@ -996,16 +1187,60 @@ nfs_rmdir(ndp)
  * order so that it looks more sensible. This appears consistent with the
  * Ultrix implementation of NFS.
  */
  * order so that it looks more sensible. This appears consistent with the
  * Ultrix implementation of NFS.
  */
-nfs_readdir(vp, uiop, cred)
+nfs_readdir(vp, uiop, cred, eofflagp)
        register struct vnode *vp;
        struct uio *uiop;
        struct ucred *cred;
        register struct vnode *vp;
        struct uio *uiop;
        struct ucred *cred;
+       int *eofflagp;
+{
+       register struct nfsnode *np = VTONFS(vp);
+       int tresid, error;
+       struct vattr vattr;
+
+       if (vp->v_type != VDIR)
+               return (EPERM);
+       /*
+        * First, check for hit on the EOF offset cache
+        */
+       if (uiop->uio_offset != 0 && uiop->uio_offset == np->n_direofoffset &&
+           (np->n_flag & NMODIFIED) == 0 &&
+           nfs_dogetattr(vp, &vattr, cred, 0) == 0 &&
+           np->n_mtime == vattr.va_mtime.tv_sec) {
+               *eofflagp = 1;
+               nfsstats.direofcache_hits++;
+               return (0);
+       }
+
+       /*
+        * Call nfs_bioread() to do the real work.
+        */
+       tresid = uiop->uio_resid;
+       error = nfs_bioread(vp, uiop, 0, cred);
+
+       if (!error && uiop->uio_resid == tresid) {
+               *eofflagp = 1;
+               nfsstats.direofcache_misses++;
+       } else
+               *eofflagp = 0;
+       return (error);
+}
+
+/*
+ * Readdir rpc call.
+ * Called from below the buffer cache by nfs_doio().
+ */
+nfs_readdirrpc(vp, uiop, cred, procp)
+       register struct vnode *vp;
+       struct uio *uiop;
+       struct ucred *cred;
+       struct proc *procp;
 {
        register long len;
        register struct direct *dp;
        register u_long *p;
        register caddr_t cp;
        register long t1;
 {
        register long len;
        register struct direct *dp;
        register u_long *p;
        register caddr_t cp;
        register long t1;
+       long tlen, lastlen;
        caddr_t bpos, dpos, cp2;
        u_long xid;
        int error = 0;
        caddr_t bpos, dpos, cp2;
        u_long xid;
        int error = 0;
@@ -1013,122 +1248,123 @@ nfs_readdir(vp, uiop, cred)
        struct mbuf *md2;
        caddr_t dpos2;
        int siz;
        struct mbuf *md2;
        caddr_t dpos2;
        int siz;
-       int more_dirs;
+       int more_dirs = 1;
        off_t off, savoff;
        struct direct *savdp;
        off_t off, savoff;
        struct direct *savdp;
+       struct nfsmount *nmp;
+       struct nfsnode *np = VTONFS(vp);
+       long tresid;
 
 
-       nfsstats.rpccnt[NFSPROC_READDIR]++;
-       nfsm_reqhead(nfs_procids[NFSPROC_READDIR], cred, xid);
-       nfsm_fhtom(vp);
-       nfsm_build(p, u_long *, 2*NFSX_UNSIGNED);
-       *p++ = txdr_unsigned(uiop->uio_offset);
-       *p = txdr_unsigned(uiop->uio_resid);
-       nfsm_request(vp);
-       siz = 0;
-       nfsm_disect(p, u_long *, NFSX_UNSIGNED);
-       more_dirs = fxdr_unsigned(int, *p);
-
-       /* Save the position so that we can do nfsm_mtouio() later */
-       dpos2 = dpos;
-       md2 = md;
-
-       /* loop thru the dir entries, doctoring them to 4bsd form */
-       savoff = off = 0;
-       savdp = dp = NULL;
-       while (more_dirs && siz < uiop->uio_resid) {
-               savoff = off;           /* Hold onto offset and dp */
-               savdp = dp;
-               nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED);
-               dp = (struct direct *)p;
-               dp->d_ino = fxdr_unsigned(u_long, *p++);
-               len = fxdr_unsigned(int, *p);
-               if (len <= 0 || len > NFS_MAXNAMLEN) {
-                       error = EBADRPC;
-                       m_freem(mrep);
-                       goto nfsmout;
-               }
-               dp->d_namlen = (u_short)len;
-               len = nfsm_rndup(len);
-               nfsm_adv(len);
-               nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED);
-               off = fxdr_unsigned(off_t, *p);
-               *p++ = 0;               /* Ensures null termination of name */
-               more_dirs = fxdr_unsigned(int, *p);
-               dp->d_reclen = len+4*NFSX_UNSIGNED;
-               siz += dp->d_reclen;
-       }
+       nmp = VFSTONFS(vp->v_mount);
+       tresid = uiop->uio_resid;
        /*
        /*
-        * If at end of rpc data, get the eof boolean
+        * Loop around doing readdir rpc's of size uio_resid or nm_rsize,
+        * whichever is smaller, truncated to a multiple of DIRBLKSIZ.
+        * The stopping criteria is EOF or buffer full.
         */
         */
-       if (!more_dirs)
-               nfsm_disecton(p, u_long *, NFSX_UNSIGNED);
+       while (more_dirs && uiop->uio_resid >= DIRBLKSIZ) {
+               nfsstats.rpccnt[NFSPROC_READDIR]++;
+               nfsm_reqhead(nfs_procids[NFSPROC_READDIR], cred, xid);
+               nfsm_fhtom(vp);
+               nfsm_build(p, u_long *, 2*NFSX_UNSIGNED);
+               *p++ = txdr_unsigned(uiop->uio_offset);
+               *p = txdr_unsigned(((uiop->uio_resid > nmp->nm_rsize) ?
+                       nmp->nm_rsize : uiop->uio_resid) & ~(DIRBLKSIZ-1));
+               nfsm_request(vp, NFSPROC_READDIR, procp, 0);
+               siz = 0;
+               nfsm_disect(p, u_long *, NFSX_UNSIGNED);
+               more_dirs = fxdr_unsigned(int, *p);
+       
+               /* Save the position so that we can do nfsm_mtouio() later */
+               dpos2 = dpos;
+               md2 = md;
+       
+               /* loop thru the dir entries, doctoring them to 4bsd form */
+               off = uiop->uio_offset;
+#ifdef lint
+               dp = (struct direct *)0;
+#endif /* lint */
+               while (more_dirs && siz < uiop->uio_resid) {
+                       savoff = off;           /* Hold onto offset and dp */
+                       savdp = dp;
+                       nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED);
+                       dp = (struct direct *)p;
+                       dp->d_ino = fxdr_unsigned(u_long, *p++);
+                       len = fxdr_unsigned(int, *p);
+                       if (len <= 0 || len > NFS_MAXNAMLEN) {
+                               error = EBADRPC;
+                               m_freem(mrep);
+                               goto nfsmout;
+                       }
+                       dp->d_namlen = (u_short)len;
+                       nfsm_adv(len);          /* Point past name */
+                       tlen = nfsm_rndup(len);
+                       /*
+                        * This should not be necessary, but some servers have
+                        * broken XDR such that these bytes are not null filled.
+                        */
+                       if (tlen != len) {
+                               *dpos = '\0';   /* Null-terminate */
+                               nfsm_adv(tlen - len);
+                               len = tlen;
+                       }
+                       nfsm_disecton(p, u_long *, 2*NFSX_UNSIGNED);
+                       off = fxdr_unsigned(off_t, *p);
+                       *p++ = 0;       /* Ensures null termination of name */
+                       more_dirs = fxdr_unsigned(int, *p);
+                       dp->d_reclen = len+4*NFSX_UNSIGNED;
+                       siz += dp->d_reclen;
+               }
+               /*
+                * If at end of rpc data, get the eof boolean
+                */
+               if (!more_dirs) {
+                       nfsm_disecton(p, u_long *, NFSX_UNSIGNED);
+                       more_dirs = (fxdr_unsigned(int, *p) == 0);
+
+                       /*
+                        * If at EOF, cache directory offset
+                        */
+                       if (!more_dirs)
+                               np->n_direofoffset = off;
+               }
+               /*
+                * If there is too much to fit in the data buffer, use savoff and
+                * savdp to trim off the last record.
+                * --> we are not at eof
+                */
+               if (siz > uiop->uio_resid) {
+                       off = savoff;
+                       siz -= dp->d_reclen;
+                       dp = savdp;
+                       more_dirs = 0;  /* Paranoia */
+               }
+               if (siz > 0) {
+                       lastlen = dp->d_reclen;
+                       md = md2;
+                       dpos = dpos2;
+                       nfsm_mtouio(uiop, siz);
+                       uiop->uio_offset = off;
+               } else
+                       more_dirs = 0;  /* Ugh, never happens, but in case.. */
+               m_freem(mrep);
+       }
        /*
        /*
-        * If there is too much to fit in the data buffer, use savoff and
-        * savdp to trim off the last record.
-        * --> we are not at eof
+        * Fill last record, iff any, out to a multiple of DIRBLKSIZ
+        * by increasing d_reclen for the last record.
         */
         */
-       if (siz > uiop->uio_resid) {
-               off = savoff;
-               siz -= dp->d_reclen;
-               dp = savdp;
-       }
-       if (siz > 0) {
-               md = md2;
-               dpos = dpos2;
-               nfsm_mtouio(uiop, siz);
-               uiop->uio_offset = off;
+       if (uiop->uio_resid < tresid) {
+               len = uiop->uio_resid & (DIRBLKSIZ - 1);
+               if (len > 0) {
+                       dp = (struct direct *)
+                               (uiop->uio_iov->iov_base - lastlen);
+                       dp->d_reclen += len;
+                       uiop->uio_iov->iov_base += len;
+                       uiop->uio_iov->iov_len -= len;
+                       uiop->uio_resid -= len;
+               }
        }
        }
-       nfsm_reqdone;
-       return (error);
-}
-
-/*
- * nfs statfs call
- * (Actually a vfsop, not a vnode op)
- */
-nfs_statfs(mp, sbp)
-       struct mount *mp;
-       register struct statfs *sbp;
-{
-       register struct vnode *vp;
-       register struct nfsv2_statfs *sfp;
-       register caddr_t cp;
-       register long t1;
-       caddr_t bpos, dpos, cp2;
-       u_long xid;
-       int error = 0;
-       struct mbuf *mreq, *mrep, *md, *mb, *mb2;
-       struct nfsmount *nmp;
-       struct ucred *cred;
-       struct nfsnode *np;
-
-       nmp = vfs_to_nfs(mp);
-       if (error = nfs_nget(mp, &nmp->nm_fh, &np))
-               return (error);
-       vp = NFSTOV(np);
-       nfsstats.rpccnt[NFSPROC_STATFS]++;
-       cred = crget();
-       cred->cr_ngroups = 1;
-       nfsm_reqhead(nfs_procids[NFSPROC_STATFS], cred, NFSX_FH);
-       nfsm_fhtom(vp);
-       nfsm_request(vp);
-       nfsm_disect(sfp, struct nfsv2_statfs *, NFSX_STATFS);
-       sbp->f_type = MOUNT_NFS;
-       sbp->f_flags = nmp->nm_flag;
-       sbp->f_bsize = fxdr_unsigned(long, sfp->sf_tsize);
-       sbp->f_fsize = fxdr_unsigned(long, sfp->sf_bsize);
-       sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
-       sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
-       sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
-       sbp->f_files = 0;
-       sbp->f_ffree = 0;
-       sbp->f_fsid.val[0] = mp->m_fsid.val[0];
-       sbp->f_fsid.val[1] = mp->m_fsid.val[1];
-       bcopy(nmp->nm_path, sbp->f_mntonname, MNAMELEN);
-       bcopy(nmp->nm_host, sbp->f_mntfromname, MNAMELEN);
-       nfsm_reqdone;
-       nfs_nput(vp);
-       crfree(cred);
+nfsmout:
        return (error);
 }
 
        return (error);
 }
 
@@ -1217,7 +1453,7 @@ nfs_lookitup(vp, ndp, fhp)
        nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
        nfsm_fhtom(vp);
        nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN);
        nfsm_reqhead(nfs_procids[NFSPROC_LOOKUP], ndp->ni_cred, NFSX_FH+NFSX_UNSIGNED+nfsm_rndup(len));
        nfsm_fhtom(vp);
        nfsm_strtom(ndp->ni_dent.d_name, len, NFS_MAXNAMLEN);
-       nfsm_request(vp);
+       nfsm_request(vp, NFSPROC_LOOKUP, u.u_procp, 1);
        if (fhp != NULL) {
                nfsm_disect(cp, caddr_t, NFSX_FH);
                bcopy(cp, (caddr_t)fhp, NFSX_FH);
        if (fhp != NULL) {
                nfsm_disect(cp, caddr_t, NFSX_FH);
                bcopy(cp, (caddr_t)fhp, NFSX_FH);
@@ -1229,7 +1465,7 @@ nfs_lookitup(vp, ndp, fhp)
 /*
  * Kludge City..
  * - make nfs_bmap() essentially a no-op that does no translation
 /*
  * Kludge City..
  * - make nfs_bmap() essentially a no-op that does no translation
- * - do nfs_strategy() by faking physical I/O with nfs_readit/nfs_writeit
+ * - do nfs_strategy() by faking physical I/O with nfs_readrpc/nfs_writerpc
  *   after mapping the physical addresses into Kernel Virtual space in the
  *   nfsiobuf area.
  *   (Maybe I could use the process's page mapping, but I was concerned that
  *   after mapping the physical addresses into Kernel Virtual space in the
  *   nfsiobuf area.
  *   (Maybe I could use the process's page mapping, but I was concerned that
@@ -1246,7 +1482,7 @@ nfs_bmap(vp, bn, vpp, bnp)
        if (vpp != NULL)
                *vpp = vp;
        if (bnp != NULL)
        if (vpp != NULL)
                *vpp = vp;
        if (bnp != NULL)
-               *bnp = bn * btodb(vp->v_mount->m_bsize);
+               *bnp = bn * btodb(vp->v_mount->mnt_stat.f_bsize);
        return (0);
 }
 
        return (0);
 }
 
@@ -1264,6 +1500,17 @@ nfs_strategy(bp)
        int error = 0;
        int fnd = 0;
 
        int error = 0;
        int fnd = 0;
 
+       /*
+        * Set b_proc. It seems a bit silly to do it here, but since bread()
+        * doesn't set it, I will.
+        * Set b_proc == NULL for asynchronous reads, since these may still
+        * be hanging about after the process terminates.
+        */
+       if ((bp->b_flags & (B_READ | B_ASYNC)) == (B_READ | B_ASYNC))
+               bp->b_proc = (struct proc *)0;
+       else
+               bp->b_proc = u.u_procp;
+
        /*
         * If an i/o daemon is waiting
         * queue the request, wake it up and wait for completion
        /*
         * If an i/o daemon is waiting
         * queue the request, wake it up and wait for completion
@@ -1305,29 +1552,32 @@ nfs_strategy(bp)
  * Essentially play ubasetup() and disk interrupt service routine by
  * mapping the data buffer into kernel virtual space and doing the
  * nfs read or write rpc's from it.
  * Essentially play ubasetup() and disk interrupt service routine by
  * mapping the data buffer into kernel virtual space and doing the
  * nfs read or write rpc's from it.
- * If the biod's are not running, this is just called from nfs_strategy(),
- * otherwise it is called by the biod's to do what would normally be
+ * If the nfsiod's are not running, this is just called from nfs_strategy(),
+ * otherwise it is called by the nfsiods to do what would normally be
  * partially disk interrupt driven.
  */
 nfs_doio(bp)
        register struct buf *bp;
 {
  * partially disk interrupt driven.
  */
 nfs_doio(bp)
        register struct buf *bp;
 {
-       register struct pte *pte, *ppte;
-       register caddr_t vaddr;
        register struct uio *uiop;
        register struct vnode *vp;
        struct nfsnode *np;
        struct ucred *cr;
        register struct uio *uiop;
        register struct vnode *vp;
        struct nfsnode *np;
        struct ucred *cr;
-       int npf, npf2;
-       int reg;
-       caddr_t vbase;
-       unsigned v;
        struct proc *rp;
        struct proc *rp;
-       int o, error;
+       int error;
        struct uio uio;
        struct iovec io;
        struct uio uio;
        struct iovec io;
+#if !defined(hp300) && !defined(i386)
+       register struct pte *pte, *ppte;
+       register caddr_t vaddr;
+       int npf, npf2;
+       int reg, o;
+       caddr_t vbase;
+       unsigned v;
+#endif
 
        vp = bp->b_vp;
 
        vp = bp->b_vp;
+       np = VTONFS(vp);
        uiop = &uio;
        uiop->uio_iov = &io;
        uiop->uio_iovcnt = 1;
        uiop = &uio;
        uiop->uio_iov = &io;
        uiop->uio_iovcnt = 1;
@@ -1340,12 +1590,15 @@ nfs_doio(bp)
         * and a guess at a group
         */
        if (bp->b_flags & B_PHYS) {
         * and a guess at a group
         */
        if (bp->b_flags & B_PHYS) {
-               VTONFS(vp)->n_flag |= NPAGEDON;
                bp->b_rcred = cr = crget();
                rp = (bp->b_flags & B_DIRTY) ? &proc[2] : bp->b_proc;
                cr->cr_uid = rp->p_uid;
                cr->cr_gid = 0;         /* Anything ?? */
                cr->cr_ngroups = 1;
                bp->b_rcred = cr = crget();
                rp = (bp->b_flags & B_DIRTY) ? &proc[2] : bp->b_proc;
                cr->cr_uid = rp->p_uid;
                cr->cr_gid = 0;         /* Anything ?? */
                cr->cr_ngroups = 1;
+#if defined(hp300) || defined(i386)
+               /* mapping was already done by vmapbuf */
+               io.iov_base = bp->b_un.b_addr;
+#else
                o = (int)bp->b_un.b_addr & PGOFSET;
                npf2 = npf = btoc(bp->b_bcount + o);
 
                o = (int)bp->b_un.b_addr & PGOFSET;
                npf2 = npf = btoc(bp->b_bcount + o);
 
@@ -1354,7 +1607,8 @@ nfs_doio(bp)
                 */
                while ((reg = rmalloc(nfsmap, (long)npf)) == 0) {
                        nfsmap_want++;
                 */
                while ((reg = rmalloc(nfsmap, (long)npf)) == 0) {
                        nfsmap_want++;
-                       sleep((caddr_t)&nfsmap_want, PZERO-1);
+                       (void) tsleep((caddr_t)&nfsmap_want, PZERO-1, "nfsmap",
+                                       0);
                }
                reg--;
                if (bp->b_flags & B_PAGET)
                }
                reg--;
                if (bp->b_flags & B_PAGET)
@@ -1382,40 +1636,76 @@ nfs_doio(bp)
                        vaddr += NBPG;
                        --npf;
                }
                        vaddr += NBPG;
                        --npf;
                }
+               io.iov_base = vbase+o;
+#endif /* !defined(hp300) */
 
                /*
                 * And do the i/o rpc
                 */
 
                /*
                 * And do the i/o rpc
                 */
-               io.iov_base = vbase+o;
                io.iov_len = uiop->uio_resid = bp->b_bcount;
                uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
                if (bp->b_flags & B_READ) {
                        uiop->uio_rw = UIO_READ;
                        nfsstats.read_physios++;
                io.iov_len = uiop->uio_resid = bp->b_bcount;
                uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
                if (bp->b_flags & B_READ) {
                        uiop->uio_rw = UIO_READ;
                        nfsstats.read_physios++;
-                       bp->b_error = error = nfs_readrpc(vp, uiop, bp->b_rcred);
+                       bp->b_error = error = nfs_readrpc(vp, uiop,
+                               bp->b_rcred, bp->b_proc);
+                       /*
+                        * If a text file has been modified since it was exec'd
+                        * blow the process' out of the water. This is the
+                        * closest we can come to "Text File Busy" in good old
+                        * stateless nfs.
+                        */
+                       if ((vp->v_flag & VTEXT) &&
+                           (vp->v_text->x_mtime != np->n_vattr.va_mtime.tv_sec))
+                               xinval(vp);
                } else {
                        uiop->uio_rw = UIO_WRITE;
                        nfsstats.write_physios++;
                } else {
                        uiop->uio_rw = UIO_WRITE;
                        nfsstats.write_physios++;
-                       bp->b_error = error = nfs_writerpc(vp, uiop, bp->b_wcred);
+                       bp->b_error = error = nfs_writerpc(vp, uiop,
+                               bp->b_wcred, bp->b_proc);
                }
 
                /*
                 * Finally, release pte's used by physical i/o
                 */
                crfree(cr);
                }
 
                /*
                 * Finally, release pte's used by physical i/o
                 */
                crfree(cr);
+#if !defined(hp300) && !defined(i386)
                rmfree(nfsmap, (long)npf2, (long)++reg);
                if (nfsmap_want) {
                        nfsmap_want = 0;
                        wakeup((caddr_t)&nfsmap_want);
                }
                rmfree(nfsmap, (long)npf2, (long)++reg);
                if (nfsmap_want) {
                        nfsmap_want = 0;
                        wakeup((caddr_t)&nfsmap_want);
                }
+#endif
        } else {
                if (bp->b_flags & B_READ) {
                        io.iov_len = uiop->uio_resid = bp->b_bcount;
        } else {
                if (bp->b_flags & B_READ) {
                        io.iov_len = uiop->uio_resid = bp->b_bcount;
-                       uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
                        io.iov_base = bp->b_un.b_addr;
                        uiop->uio_rw = UIO_READ;
                        io.iov_base = bp->b_un.b_addr;
                        uiop->uio_rw = UIO_READ;
-                       nfsstats.read_bios++;
-                       bp->b_error = error = nfs_readrpc(vp, uiop, bp->b_rcred);
+                       switch (vp->v_type) {
+                       case VREG:
+                               uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
+                               nfsstats.read_bios++;
+                               error = nfs_readrpc(vp, uiop, bp->b_rcred,
+                                       bp->b_proc);
+                               break;
+                       case VLNK:
+                               uiop->uio_offset = 0;
+                               nfsstats.readlink_bios++;
+                               error = nfs_readlinkrpc(vp, uiop, bp->b_rcred,
+                                               bp->b_proc);
+                               break;
+                       case VDIR:
+                               uiop->uio_offset = bp->b_lblkno;
+                               nfsstats.readdir_bios++;
+                               error = nfs_readdirrpc(vp, uiop, bp->b_rcred,
+                                           bp->b_proc);
+                               /*
+                                * Save offset cookie in b_blkno.
+                                */
+                               bp->b_blkno = uiop->uio_offset;
+                               break;
+                       };
+                       bp->b_error = error;
                } else {
                        io.iov_len = uiop->uio_resid = bp->b_dirtyend
                                - bp->b_dirtyoff;
                } else {
                        io.iov_len = uiop->uio_resid = bp->b_dirtyend
                                - bp->b_dirtyoff;
@@ -1424,9 +1714,9 @@ nfs_doio(bp)
                        io.iov_base = bp->b_un.b_addr + bp->b_dirtyoff;
                        uiop->uio_rw = UIO_WRITE;
                        nfsstats.write_bios++;
                        io.iov_base = bp->b_un.b_addr + bp->b_dirtyoff;
                        uiop->uio_rw = UIO_WRITE;
                        nfsstats.write_bios++;
-                       bp->b_error = error = nfs_writerpc(vp, uiop, bp->b_wcred);
+                       bp->b_error = error = nfs_writerpc(vp, uiop,
+                               bp->b_wcred, bp->b_proc);
                        if (error) {
                        if (error) {
-                               np = VTONFS(vp);
                                np->n_error = error;
                                np->n_flag |= NWRITEERR;
                        }
                                np->n_error = error;
                                np->n_flag |= NWRITEERR;
                        }
@@ -1472,7 +1762,17 @@ nfs_print(vp)
 {
        register struct nfsnode *np = VTONFS(vp);
 
 {
        register struct nfsnode *np = VTONFS(vp);
 
-       printf("tag VT_NFS, fileid %d fsid 0x%x%s\n",
-               np->n_vattr.va_fileid, np->n_vattr.va_fsid,
-               (np->n_flag & NLOCKED) ? " (LOCKED)" : "");
+       printf("tag VT_NFS, fileid %d fsid 0x%x",
+               np->n_vattr.va_fileid, np->n_vattr.va_fsid);
+#ifdef FIFO
+       if (vp->v_type == VFIFO)
+               fifo_printinfo(vp);
+#endif /* FIFO */
+       printf("%s\n", (np->n_flag & NLOCKED) ? " (LOCKED)" : "");
+       if (np->n_lockholder == 0)
+               return;
+       printf("\towner pid %d", np->n_lockholder);
+       if (np->n_lockwaiter)
+               printf(" waiting pid %d", np->n_lockwaiter);
+       printf("\n");
 }
 }