BSD 4_4_Lite2 release
[unix-history] / usr / src / sys / nfs / nfs_srvcache.c
index 7122869..954987b 100644 (file)
@@ -1,13 +1,39 @@
 /*
 /*
- * Copyright (c) 1989 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1989, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * 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.
  *
- * %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.
  *
  *
- *     @(#)nfs_srvcache.c      7.14 (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.
+ *
+ *     @(#)nfs_srvcache.c      8.3 (Berkeley) 3/30/95
  */
 
 /*
  */
 
 /*
  *             of an NFS Server", in Proc. Winter 1989 USENIX Conference,
  *             pages 53-63. San Diego, February 1989.
  */
  *             of an NFS Server", in Proc. Winter 1989 USENIX Conference,
  *             pages 53-63. San Diego, February 1989.
  */
-#include "param.h"
-#include "vnode.h"
-#include "mount.h"
-#include "kernel.h"
-#include "systm.h"
-#include "proc.h"
-#include "mbuf.h"
-#include "socket.h"
-#include "socketvar.h"
-#include "netinet/in.h"
-#ifdef ISO
-#include "netiso/iso.h"
-#endif
-#include "nfsm_subs.h"
-#include "rpcv2.h"
-#include "nfsv2.h"
-#include "nfsrvcache.h"
-#include "nfs.h"
-#include "nqnfs.h"
+#include <sys/param.h>
+#include <sys/vnode.h>
+#include <sys/mount.h>
+#include <sys/kernel.h>
+#include <sys/systm.h>
+#include <sys/proc.h>
+#include <sys/mbuf.h>
+#include <sys/malloc.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
 
 
-#if    ((NFSRCHSZ&(NFSRCHSZ-1)) == 0)
-#define        NFSRCHASH(xid)          (((xid)+((xid)>>16))&(NFSRCHSZ-1))
-#else
-#define        NFSRCHASH(xid)          (((unsigned)((xid)+((xid)>>16)))%NFSRCHSZ)
+#include <netinet/in.h>
+#ifdef ISO
+#include <netiso/iso.h>
 #endif
 #endif
+#include <nfs/nfsm_subs.h>
+#include <nfs/rpcv2.h>
+#include <nfs/nfsproto.h>
+#include <nfs/nfs.h>
+#include <nfs/nfsrvcache.h>
+#include <nfs/nqnfs.h>
 
 
-union rhead {
-       union  rhead *rh_head[2];
-       struct nfsrvcache *rh_chain[2];
-} rhead[NFSRCHSZ];
+extern struct nfsstats nfsstats;
+extern int nfsv2_procid[NFS_NPROCS];
+long numnfsrvcache, desirednfsrvcache = NFSRVCACHESIZ;
 
 
-static struct nfsrvcache nfsrvcachehead;
-static struct nfsrvcache nfsrvcache[NFSRVCACHESIZ];
+#define        NFSRCHASH(xid) \
+       (&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
+LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
+TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
+u_long nfsrvhash;
 
 #define TRUE   1
 #define        FALSE   0
 
 #define TRUE   1
 #define        FALSE   0
@@ -66,7 +90,6 @@ int nonidempotent[NFS_NPROCS] = {
        FALSE,
        FALSE,
        FALSE,
        FALSE,
        FALSE,
        FALSE,
-       FALSE,
        TRUE,
        TRUE,
        TRUE,
        TRUE,
        TRUE,
        TRUE,
@@ -75,6 +98,11 @@ int nonidempotent[NFS_NPROCS] = {
        TRUE,
        TRUE,
        TRUE,
        TRUE,
        TRUE,
        TRUE,
+       TRUE,
+       FALSE,
+       FALSE,
+       FALSE,
+       FALSE,
        FALSE,
        FALSE,
        FALSE,
        FALSE,
        FALSE,
        FALSE,
@@ -84,7 +112,7 @@ int nonidempotent[NFS_NPROCS] = {
 };
 
 /* True iff the rpc reply is an nfs status ONLY! */
 };
 
 /* True iff the rpc reply is an nfs status ONLY! */
-static int repliesstatus[NFS_NPROCS] = {
+static int nfsv2_repstat[NFS_NPROCS] = {
        FALSE,
        FALSE,
        FALSE,
        FALSE,
        FALSE,
        FALSE,
@@ -103,38 +131,17 @@ static int repliesstatus[NFS_NPROCS] = {
        TRUE,
        FALSE,
        FALSE,
        TRUE,
        FALSE,
        FALSE,
-       FALSE,
-       FALSE,
-       FALSE,
-       FALSE,
 };
 
 /*
  * Initialize the server request cache list
  */
 };
 
 /*
  * Initialize the server request cache list
  */
+void
 nfsrv_initcache()
 {
 nfsrv_initcache()
 {
-       register int i;
-       register struct nfsrvcache *rp = nfsrvcache;
-       register struct nfsrvcache *hp = &nfsrvcachehead;
-       register union  rhead *rh = rhead;
 
 
-       for (i = NFSRCHSZ; --i >= 0; rh++) {
-               rh->rh_head[0] = rh;
-               rh->rh_head[1] = rh;
-       }
-       hp->rc_next = hp->rc_prev = hp;
-       for (i = NFSRVCACHESIZ; i-- > 0; ) {
-               rp->rc_state = RC_UNUSED;
-               rp->rc_flag = 0;
-               rp->rc_forw = rp;
-               rp->rc_back = rp;
-               rp->rc_next = hp->rc_next;
-               hp->rc_next->rc_prev = rp;
-               rp->rc_prev = hp;
-               hp->rc_next = rp;
-               rp++;
-       }
+       nfsrvhashtbl = hashinit(desirednfsrvcache, M_NFSD, &nfsrvhash);
+       TAILQ_INIT(&nfsrvlruhead);
 }
 
 /*
 }
 
 /*
@@ -151,59 +158,57 @@ nfsrv_initcache()
  *   return DOIT
  * Update/add new request at end of lru list
  */
  *   return DOIT
  * Update/add new request at end of lru list
  */
-nfsrv_getcache(nam, nd, repp)
-       struct mbuf *nam;
-       register struct nfsd *nd;
+int
+nfsrv_getcache(nd, slp, repp)
+       register struct nfsrv_descript *nd;
+       struct nfssvc_sock *slp;
        struct mbuf **repp;
 {
        register struct nfsrvcache *rp;
        struct mbuf **repp;
 {
        register struct nfsrvcache *rp;
-       register union  rhead *rh;
        struct mbuf *mb;
        struct sockaddr_in *saddr;
        caddr_t bpos;
        int ret;
 
        struct mbuf *mb;
        struct sockaddr_in *saddr;
        caddr_t bpos;
        int ret;
 
-       if (nd->nd_nqlflag != NQL_NOVAL)
+       /*
+        * Don't cache recent requests for reliable transport protocols.
+        * (Maybe we should for the case of a reconnect, but..)
+        */
+       if (!nd->nd_nam2)
                return (RC_DOIT);
                return (RC_DOIT);
-       rh = &rhead[NFSRCHASH(nd->nd_retxid)];
 loop:
 loop:
-       for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
+       for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
+           rp = rp->rc_hash.le_next) {
            if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
            if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
-               netaddr_match(NETFAMILY(rp), &rp->rc_haddr, (union nethostaddr *)0, nam)) {
+               netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
                        if ((rp->rc_flag & RC_LOCKED) != 0) {
                                rp->rc_flag |= RC_WANTED;
                                (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
                                goto loop;
                        }
                        rp->rc_flag |= RC_LOCKED;
                        if ((rp->rc_flag & RC_LOCKED) != 0) {
                                rp->rc_flag |= RC_WANTED;
                                (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
                                goto loop;
                        }
                        rp->rc_flag |= RC_LOCKED;
-                       if (rp->rc_prev != &nfsrvcachehead) {
-                               rp->rc_next->rc_prev = rp->rc_prev;
-                               rp->rc_prev->rc_next = rp->rc_next;
-                               rp->rc_next = nfsrvcachehead.rc_next;
-                               nfsrvcachehead.rc_next = rp;
-                               rp->rc_prev = &nfsrvcachehead;
-                               rp->rc_next->rc_prev = rp;
+                       /* If not at end of LRU chain, move it there */
+                       if (rp->rc_lru.tqe_next) {
+                               TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
+                               TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
                        }
                        if (rp->rc_state == RC_UNUSED)
                                panic("nfsrv cache");
                        }
                        if (rp->rc_state == RC_UNUSED)
                                panic("nfsrv cache");
-                       if (rp->rc_state == RC_INPROG ||
-                          (time.tv_sec - rp->rc_timestamp) < RC_DELAY) {
+                       if (rp->rc_state == RC_INPROG) {
                                nfsstats.srvcache_inproghits++;
                                ret = RC_DROPIT;
                        } else if (rp->rc_flag & RC_REPSTATUS) {
                                nfsstats.srvcache_inproghits++;
                                ret = RC_DROPIT;
                        } else if (rp->rc_flag & RC_REPSTATUS) {
-                               nfsstats.srvcache_idemdonehits++;
-                               nfs_rephead(0, nd, rp->rc_status,
+                               nfsstats.srvcache_nonidemdonehits++;
+                               nfs_rephead(0, nd, slp, rp->rc_status,
                                   0, (u_quad_t *)0, repp, &mb, &bpos);
                                   0, (u_quad_t *)0, repp, &mb, &bpos);
-                               rp->rc_timestamp = time.tv_sec;
                                ret = RC_REPLY;
                        } else if (rp->rc_flag & RC_REPMBUF) {
                                ret = RC_REPLY;
                        } else if (rp->rc_flag & RC_REPMBUF) {
-                               nfsstats.srvcache_idemdonehits++;
+                               nfsstats.srvcache_nonidemdonehits++;
                                *repp = m_copym(rp->rc_reply, 0, M_COPYALL,
                                                M_WAIT);
                                *repp = m_copym(rp->rc_reply, 0, M_COPYALL,
                                                M_WAIT);
-                               rp->rc_timestamp = time.tv_sec;
                                ret = RC_REPLY;
                        } else {
                                ret = RC_REPLY;
                        } else {
-                               nfsstats.srvcache_nonidemdonehits++;
+                               nfsstats.srvcache_idemdonehits++;
                                rp->rc_state = RC_INPROG;
                                ret = RC_DOIT;
                        }
                                rp->rc_state = RC_INPROG;
                                ret = RC_DOIT;
                        }
@@ -216,30 +221,32 @@ loop:
                }
        }
        nfsstats.srvcache_misses++;
                }
        }
        nfsstats.srvcache_misses++;
-       rp = nfsrvcachehead.rc_prev;
-       while ((rp->rc_flag & RC_LOCKED) != 0) {
-               rp->rc_flag |= RC_WANTED;
-               (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
-               rp = nfsrvcachehead.rc_prev;
-       }
-       rp->rc_flag |= RC_LOCKED;
-       remque(rp);
-       if (rp->rc_prev != &nfsrvcachehead) {
-               rp->rc_next->rc_prev = rp->rc_prev;
-               rp->rc_prev->rc_next = rp->rc_next;
-               rp->rc_next = nfsrvcachehead.rc_next;
-               nfsrvcachehead.rc_next = rp;
-               rp->rc_prev = &nfsrvcachehead;
-               rp->rc_next->rc_prev = rp;
+       if (numnfsrvcache < desirednfsrvcache) {
+               rp = (struct nfsrvcache *)malloc((u_long)sizeof *rp,
+                   M_NFSD, M_WAITOK);
+               bzero((char *)rp, sizeof *rp);
+               numnfsrvcache++;
+               rp->rc_flag = RC_LOCKED;
+       } else {
+               rp = nfsrvlruhead.tqh_first;
+               while ((rp->rc_flag & RC_LOCKED) != 0) {
+                       rp->rc_flag |= RC_WANTED;
+                       (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
+                       rp = nfsrvlruhead.tqh_first;
+               }
+               rp->rc_flag |= RC_LOCKED;
+               LIST_REMOVE(rp, rc_hash);
+               TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
+               if (rp->rc_flag & RC_REPMBUF)
+                       m_freem(rp->rc_reply);
+               if (rp->rc_flag & RC_NAM)
+                       MFREE(rp->rc_nam, mb);
+               rp->rc_flag &= (RC_LOCKED | RC_WANTED);
        }
        }
-       if (rp->rc_flag & RC_REPMBUF)
-               m_freem(rp->rc_reply);
-       if (rp->rc_flag & RC_NAM)
-               MFREE(rp->rc_nam, mb);
-       rp->rc_flag &= (RC_LOCKED | RC_WANTED);
+       TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
        rp->rc_state = RC_INPROG;
        rp->rc_xid = nd->nd_retxid;
        rp->rc_state = RC_INPROG;
        rp->rc_xid = nd->nd_retxid;
-       saddr = mtod(nam, struct sockaddr_in *);
+       saddr = mtod(nd->nd_nam, struct sockaddr_in *);
        switch (saddr->sin_family) {
        case AF_INET:
                rp->rc_flag |= RC_INETADDR;
        switch (saddr->sin_family) {
        case AF_INET:
                rp->rc_flag |= RC_INETADDR;
@@ -248,11 +255,11 @@ loop:
        case AF_ISO:
        default:
                rp->rc_flag |= RC_NAM;
        case AF_ISO:
        default:
                rp->rc_flag |= RC_NAM;
-               rp->rc_nam = m_copym(nam, 0, M_COPYALL, M_WAIT);
+               rp->rc_nam = m_copym(nd->nd_nam, 0, M_COPYALL, M_WAIT);
                break;
        };
        rp->rc_proc = nd->nd_procnum;
                break;
        };
        rp->rc_proc = nd->nd_procnum;
-       insque(rp, rh);
+       LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
        rp->rc_flag &= ~RC_LOCKED;
        if (rp->rc_flag & RC_WANTED) {
                rp->rc_flag &= ~RC_WANTED;
        rp->rc_flag &= ~RC_LOCKED;
        if (rp->rc_flag & RC_WANTED) {
                rp->rc_flag &= ~RC_WANTED;
@@ -265,22 +272,20 @@ loop:
  * Update a request cache entry after the rpc has been done
  */
 void
  * Update a request cache entry after the rpc has been done
  */
 void
-nfsrv_updatecache(nam, nd, repvalid, repmbuf)
-       struct mbuf *nam;
-       register struct nfsd *nd;
+nfsrv_updatecache(nd, repvalid, repmbuf)
+       register struct nfsrv_descript *nd;
        int repvalid;
        struct mbuf *repmbuf;
 {
        register struct nfsrvcache *rp;
        int repvalid;
        struct mbuf *repmbuf;
 {
        register struct nfsrvcache *rp;
-       register union  rhead *rh;
 
 
-       if (nd->nd_nqlflag != NQL_NOVAL)
+       if (!nd->nd_nam2)
                return;
                return;
-       rh = &rhead[NFSRCHASH(nd->nd_retxid)];
 loop:
 loop:
-       for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
+       for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
+           rp = rp->rc_hash.le_next) {
            if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
            if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
-               netaddr_match(NETFAMILY(rp), &rp->rc_haddr, (union nethostaddr *)0, nam)) {
+               netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
                        if ((rp->rc_flag & RC_LOCKED) != 0) {
                                rp->rc_flag |= RC_WANTED;
                                (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
                        if ((rp->rc_flag & RC_LOCKED) != 0) {
                                rp->rc_flag |= RC_WANTED;
                                (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
@@ -291,23 +296,17 @@ loop:
                        /*
                         * If we have a valid reply update status and save
                         * the reply for non-idempotent rpc's.
                        /*
                         * If we have a valid reply update status and save
                         * the reply for non-idempotent rpc's.
-                        * Otherwise invalidate entry by setting the timestamp
-                        * to nil.
                         */
                         */
-                       if (repvalid) {
-                               rp->rc_timestamp = time.tv_sec;
-                               if (nonidempotent[nd->nd_procnum]) {
-                                       if (repliesstatus[nd->nd_procnum]) {
-                                               rp->rc_status = nd->nd_repstat;
-                                               rp->rc_flag |= RC_REPSTATUS;
-                                       } else {
-                                               rp->rc_reply = m_copym(repmbuf,
-                                                       0, M_COPYALL, M_WAIT);
-                                               rp->rc_flag |= RC_REPMBUF;
-                                       }
+                       if (repvalid && nonidempotent[nd->nd_procnum]) {
+                               if ((nd->nd_flag & ND_NFSV3) == 0 &&
+                                 nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
+                                       rp->rc_status = nd->nd_repstat;
+                                       rp->rc_flag |= RC_REPSTATUS;
+                               } else {
+                                       rp->rc_reply = m_copym(repmbuf,
+                                               0, M_COPYALL, M_WAIT);
+                                       rp->rc_flag |= RC_REPMBUF;
                                }
                                }
-                       } else {
-                               rp->rc_timestamp = 0;
                        }
                        rp->rc_flag &= ~RC_LOCKED;
                        if (rp->rc_flag & RC_WANTED) {
                        }
                        rp->rc_flag &= ~RC_LOCKED;
                        if (rp->rc_flag & RC_WANTED) {
@@ -325,29 +324,13 @@ loop:
 void
 nfsrv_cleancache()
 {
 void
 nfsrv_cleancache()
 {
-       register int i;
-       register struct nfsrvcache *rp = nfsrvcache;
-       register struct nfsrvcache *hp = &nfsrvcachehead;
-       register union  rhead *rh = rhead;
+       register struct nfsrvcache *rp, *nextrp;
 
 
-       for (i = NFSRCHSZ; --i >= 0; rh++) {
-               rh->rh_head[0] = rh;
-               rh->rh_head[1] = rh;
-       }
-       hp->rc_next = hp->rc_prev = hp;
-       for (i = NFSRVCACHESIZ; i-- > 0; ) {
-               if (rp->rc_flag & RC_REPMBUF)
-                       m_freem(rp->rc_reply);
-               if (rp->rc_flag & RC_NAM)
-                       m_freem(rp->rc_nam);
-               rp->rc_state = RC_UNUSED;
-               rp->rc_flag = 0;
-               rp->rc_forw = rp;
-               rp->rc_back = rp;
-               rp->rc_next = hp->rc_next;
-               hp->rc_next->rc_prev = rp;
-               rp->rc_prev = hp;
-               hp->rc_next = rp;
-               rp++;
+       for (rp = nfsrvlruhead.tqh_first; rp != 0; rp = nextrp) {
+               nextrp = rp->rc_lru.tqe_next;
+               LIST_REMOVE(rp, rc_hash);
+               TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
+               free(rp, M_NFSD);
        }
        }
+       numnfsrvcache = 0;
 }
 }