BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / nfs / nfs_srvcache.c
index 44ba223..b274b17 100644 (file)
@@ -5,30 +5,55 @@
  * 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 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.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.
+ *
+ *     @(#)nfs_srvcache.c      7.11 (Berkeley) 4/16/91
+ */
+
+/*
+ * Reference: Chet Juszczak, "Improving the Performance and Correctness
+ *            of an NFS Server", in Proc. Winter 1989 USENIX Conference,
+ *            pages 53-63. San Diego, February 1989.
  */
 
 #include "param.h"
  */
 
 #include "param.h"
-#include "user.h"
+#include "namei.h"
 #include "vnode.h"
 #include "mount.h"
 #include "kernel.h"
 #include "vnode.h"
 #include "mount.h"
 #include "kernel.h"
+#include "systm.h"
 #include "mbuf.h"
 #include "socket.h"
 #include "socketvar.h"
 #include "mbuf.h"
 #include "socket.h"
 #include "socketvar.h"
-#include "netinet/in.h"
+
+#include "../netinet/in.h"
+
 #include "nfsm_subs.h"
 #include "nfsv2.h"
 #include "nfsrvcache.h"
 #include "nfsm_subs.h"
 #include "nfsv2.h"
 #include "nfsrvcache.h"
@@ -54,7 +79,7 @@ static struct nfsrvcache nfsrvcache[NFSRVCACHESIZ];
 /*
  * Static array that defines which nfs rpc's are nonidempotent
  */
 /*
  * Static array that defines which nfs rpc's are nonidempotent
  */
-static int nonidempotent[NFS_NPROCS] = {
+int nonidempotent[NFS_NPROCS] = {
        FALSE,
        FALSE,
        TRUE,
        FALSE,
        FALSE,
        TRUE,
@@ -147,20 +172,18 @@ nfsrv_getcache(nam, xid, proc, repp)
 {
        register struct nfsrvcache *rp;
        register union  rhead *rh;
 {
        register struct nfsrvcache *rp;
        register union  rhead *rh;
-       register u_long saddr;
        struct mbuf *mb;
        caddr_t bpos;
        int ret;
 
        rh = &rhead[NFSRCHASH(xid)];
        struct mbuf *mb;
        caddr_t bpos;
        int ret;
 
        rh = &rhead[NFSRCHASH(xid)];
-       saddr = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
 loop:
        for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
 loop:
        for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
-               if (xid == rp->rc_xid && saddr == rp->rc_saddr &&
-                   proc == rp->rc_proc) {
+               if (xid == rp->rc_xid && proc == rp->rc_proc &&
+                   nfs_netaddr_match(nam, &rp->rc_nam)) {
                        if ((rp->rc_flag & RC_LOCKED) != 0) {
                                rp->rc_flag |= RC_WANTED;
                        if ((rp->rc_flag & RC_LOCKED) != 0) {
                                rp->rc_flag |= RC_WANTED;
-                               sleep((caddr_t)rp, PZERO-1);
+                               (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
                                goto loop;
                        }
                        rp->rc_flag |= RC_LOCKED;
                                goto loop;
                        }
                        rp->rc_flag |= RC_LOCKED;
@@ -179,7 +202,7 @@ loop:
                                ret = RC_REPLY;
                        } else if (rp->rc_flag & RC_REPMBUF) {
                                nfsstats.srvcache_idemdonehits++;
                                ret = RC_REPLY;
                        } else if (rp->rc_flag & RC_REPMBUF) {
                                nfsstats.srvcache_idemdonehits++;
-                               *repp = NFSMCOPY(rp->rc_reply, 0, M_COPYALL,
+                               *repp = m_copym(rp->rc_reply, 0, M_COPYALL,
                                                M_WAIT);
                                rp->rc_timestamp = time.tv_sec;
                                ret = RC_REPLY;
                                                M_WAIT);
                                rp->rc_timestamp = time.tv_sec;
                                ret = RC_REPLY;
@@ -200,7 +223,7 @@ loop:
        rp = nfsrvcachehead.rc_prev;
        while ((rp->rc_flag & RC_LOCKED) != 0) {
                rp->rc_flag |= RC_WANTED;
        rp = nfsrvcachehead.rc_prev;
        while ((rp->rc_flag & RC_LOCKED) != 0) {
                rp->rc_flag |= RC_WANTED;
-               sleep((caddr_t)rp, PZERO-1);
+               (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
        }
        remque(rp);
        put_at_head(rp);
        }
        remque(rp);
        put_at_head(rp);
@@ -211,7 +234,7 @@ loop:
        rp->rc_flag = 0;
        rp->rc_state = RC_INPROG;
        rp->rc_xid = xid;
        rp->rc_flag = 0;
        rp->rc_state = RC_INPROG;
        rp->rc_xid = xid;
-       rp->rc_saddr = saddr;
+       bcopy((caddr_t)nam, (caddr_t)&rp->rc_nam, sizeof (struct mbuf));
        rp->rc_proc = proc;
        insque(rp, rh);
        if (mb)
        rp->rc_proc = proc;
        insque(rp, rh);
        if (mb)
@@ -222,40 +245,49 @@ loop:
 /*
  * Update a request cache entry after the rpc has been done
  */
 /*
  * Update a request cache entry after the rpc has been done
  */
-nfsrv_updatecache(nam, xid, proc, repstat, repmbuf)
+nfsrv_updatecache(nam, xid, proc, repvalid, repstat, repmbuf)
        struct mbuf *nam;
        u_long xid;
        int proc;
        struct mbuf *nam;
        u_long xid;
        int proc;
+       int repvalid;
        int repstat;
        struct mbuf *repmbuf;
 {
        register struct nfsrvcache *rp;
        register union  rhead *rh;
        int repstat;
        struct mbuf *repmbuf;
 {
        register struct nfsrvcache *rp;
        register union  rhead *rh;
-       register u_long saddr;
 
        rh = &rhead[NFSRCHASH(xid)];
 
        rh = &rhead[NFSRCHASH(xid)];
-       saddr = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
 loop:
        for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
 loop:
        for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
-               if (xid == rp->rc_xid && saddr == rp->rc_saddr &&
-                   proc == rp->rc_proc) {
+               if (xid == rp->rc_xid && proc == rp->rc_proc &&
+                   nfs_netaddr_match(nam, &rp->rc_nam)) {
                        if ((rp->rc_flag & RC_LOCKED) != 0) {
                                rp->rc_flag |= RC_WANTED;
                        if ((rp->rc_flag & RC_LOCKED) != 0) {
                                rp->rc_flag |= RC_WANTED;
-                               sleep((caddr_t)rp, PZERO-1);
+                               (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
                                goto loop;
                        }
                        rp->rc_flag |= RC_LOCKED;
                        rp->rc_state = RC_DONE;
                                goto loop;
                        }
                        rp->rc_flag |= RC_LOCKED;
                        rp->rc_state = RC_DONE;
-                       rp->rc_timestamp = time.tv_sec;
-                       if (nonidempotent[proc]) {
-                               if (repliesstatus[proc]) {
-                                       rp->rc_status = repstat;
-                                       rp->rc_flag |= RC_REPSTATUS;
-                               } else {
-                                       rp->rc_reply = NFSMCOPY(repmbuf, 0,
-                                                       M_COPYALL, M_WAIT);
-                                       rp->rc_flag |= RC_REPMBUF;
+                       /*
+                        * 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[proc]) {
+                                       if (repliesstatus[proc]) {
+                                               rp->rc_status = 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) {