xref: /csrg-svn/sys/nfs/nfs_srvcache.c (revision 39742)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  *	@(#)nfs_srvcache.c	7.1 (Berkeley) 12/20/89
21  */
22 
23 #include "param.h"
24 #include "user.h"
25 #include "vnode.h"
26 #include "mount.h"
27 #include "kernel.h"
28 #include "mbuf.h"
29 #include "socket.h"
30 #include "socketvar.h"
31 #include "netinet/in.h"
32 #include "nfsm_subs.h"
33 #include "nfsv2.h"
34 #include "nfsrvcache.h"
35 #include "nfs.h"
36 
37 #if	((NFSRCHSZ&(NFSRCHSZ-1)) == 0)
38 #define	NFSRCHASH(xid)		(((xid)+((xid)>>16))&(NFSRCHSZ-1))
39 #else
40 #define	NFSRCHASH(xid)		(((unsigned)((xid)+((xid)>>16)))%NFSRCHSZ)
41 #endif
42 
43 union rhead {
44 	union  rhead *rh_head[2];
45 	struct nfsrvcache *rh_chain[2];
46 } rhead[NFSRCHSZ];
47 
48 static struct nfsrvcache nfsrvcachehead;
49 static struct nfsrvcache nfsrvcache[NFSRVCACHESIZ];
50 
51 #define TRUE	1
52 #define	FALSE	0
53 
54 /*
55  * Static array that defines which nfs rpc's are nonidempotent
56  */
57 static int nonidempotent[NFS_NPROCS] = {
58 	FALSE,
59 	FALSE,
60 	TRUE,
61 	FALSE,
62 	FALSE,
63 	FALSE,
64 	FALSE,
65 	FALSE,
66 	TRUE,
67 	TRUE,
68 	TRUE,
69 	TRUE,
70 	TRUE,
71 	TRUE,
72 	TRUE,
73 	TRUE,
74 	FALSE,
75 	FALSE,
76 };
77 
78 /* True iff the rpc reply is an nfs status ONLY! */
79 static int repliesstatus[NFS_NPROCS] = {
80 	FALSE,
81 	FALSE,
82 	FALSE,
83 	FALSE,
84 	FALSE,
85 	FALSE,
86 	FALSE,
87 	FALSE,
88 	FALSE,
89 	FALSE,
90 	TRUE,
91 	TRUE,
92 	TRUE,
93 	TRUE,
94 	FALSE,
95 	TRUE,
96 	FALSE,
97 	FALSE,
98 };
99 
100 /*
101  * Initialize the server request cache list
102  */
103 nfsrv_initcache()
104 {
105 	register int i;
106 	register struct nfsrvcache *rp = nfsrvcache;
107 	register struct nfsrvcache *hp = &nfsrvcachehead;
108 	register union  rhead *rh = rhead;
109 
110 	for (i = NFSRCHSZ; --i >= 0; rh++) {
111 		rh->rh_head[0] = rh;
112 		rh->rh_head[1] = rh;
113 	}
114 	hp->rc_next = hp->rc_prev = hp;
115 	for (i = NFSRVCACHESIZ; i-- > 0; ) {
116 		rp->rc_state = RC_UNUSED;
117 		rp->rc_flag = 0;
118 		rp->rc_forw = rp;
119 		rp->rc_back = rp;
120 		rp->rc_next = hp->rc_next;
121 		hp->rc_next->rc_prev = rp;
122 		rp->rc_prev = hp;
123 		hp->rc_next = rp;
124 		rp++;
125 	}
126 }
127 
128 /*
129  * Look for the request in the cache
130  * If found then
131  *    return action and optionally reply
132  * else
133  *    insert it in the cache
134  *
135  * The rules are as follows:
136  * - if in progress, return DROP request
137  * - if completed within DELAY of the current time, return DROP it
138  * - if completed a longer time ago return REPLY if the reply was cached or
139  *   return DOIT
140  * Update/add new request at end of lru list
141  */
142 nfsrv_getcache(nam, xid, proc, repp)
143 	struct mbuf *nam;
144 	u_long xid;
145 	int proc;
146 	struct mbuf **repp;
147 {
148 	register struct nfsrvcache *rp;
149 	register union  rhead *rh;
150 	register u_long saddr;
151 	struct mbuf *mb;
152 	caddr_t bpos;
153 	int ret;
154 
155 	rh = &rhead[NFSRCHASH(xid)];
156 	saddr = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
157 loop:
158 	for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
159 		if (xid == rp->rc_xid && saddr == rp->rc_saddr &&
160 		    proc == rp->rc_proc) {
161 			if ((rp->rc_flag & RC_LOCKED) != 0) {
162 				rp->rc_flag |= RC_WANTED;
163 				sleep((caddr_t)rp, PZERO-1);
164 				goto loop;
165 			}
166 			rp->rc_flag |= RC_LOCKED;
167 			put_at_head(rp);
168 			if (rp->rc_state == RC_UNUSED)
169 				panic("nfsrv cache");
170 			if (rp->rc_state == RC_INPROG ||
171 			   (time.tv_sec - rp->rc_timestamp) < RC_DELAY) {
172 				nfsstats.srvcache_inproghits++;
173 				ret = RC_DROPIT;
174 			} else if (rp->rc_flag & RC_REPSTATUS) {
175 				nfsstats.srvcache_idemdonehits++;
176 				nfs_rephead(0, xid, rp->rc_status, repp, &mb,
177 					&bpos);
178 				rp->rc_timestamp = time.tv_sec;
179 				ret = RC_REPLY;
180 			} else if (rp->rc_flag & RC_REPMBUF) {
181 				nfsstats.srvcache_idemdonehits++;
182 				*repp = NFSMCOPY(rp->rc_reply, 0, M_COPYALL,
183 						M_WAIT);
184 				rp->rc_timestamp = time.tv_sec;
185 				ret = RC_REPLY;
186 			} else {
187 				nfsstats.srvcache_nonidemdonehits++;
188 				rp->rc_state = RC_INPROG;
189 				ret = RC_DOIT;
190 			}
191 			rp->rc_flag &= ~RC_LOCKED;
192 			if (rp->rc_flag & RC_WANTED) {
193 				rp->rc_flag &= ~RC_WANTED;
194 				wakeup((caddr_t)rp);
195 			}
196 			return (ret);
197 		}
198 	}
199 	nfsstats.srvcache_misses++;
200 	rp = nfsrvcachehead.rc_prev;
201 	while ((rp->rc_flag & RC_LOCKED) != 0) {
202 		rp->rc_flag |= RC_WANTED;
203 		sleep((caddr_t)rp, PZERO-1);
204 	}
205 	remque(rp);
206 	put_at_head(rp);
207 	if (rp->rc_flag & RC_REPMBUF)
208 		mb = rp->rc_reply;
209 	else
210 		mb = (struct mbuf *)0;
211 	rp->rc_flag = 0;
212 	rp->rc_state = RC_INPROG;
213 	rp->rc_xid = xid;
214 	rp->rc_saddr = saddr;
215 	rp->rc_proc = proc;
216 	insque(rp, rh);
217 	if (mb)
218 		m_freem(mb);
219 	return (RC_DOIT);
220 }
221 
222 /*
223  * Update a request cache entry after the rpc has been done
224  */
225 nfsrv_updatecache(nam, xid, proc, repstat, repmbuf)
226 	struct mbuf *nam;
227 	u_long xid;
228 	int proc;
229 	int repstat;
230 	struct mbuf *repmbuf;
231 {
232 	register struct nfsrvcache *rp;
233 	register union	rhead *rh;
234 	register u_long saddr;
235 
236 	rh = &rhead[NFSRCHASH(xid)];
237 	saddr = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
238 loop:
239 	for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
240 		if (xid == rp->rc_xid && saddr == rp->rc_saddr &&
241 		    proc == rp->rc_proc) {
242 			if ((rp->rc_flag & RC_LOCKED) != 0) {
243 				rp->rc_flag |= RC_WANTED;
244 				sleep((caddr_t)rp, PZERO-1);
245 				goto loop;
246 			}
247 			rp->rc_flag |= RC_LOCKED;
248 			rp->rc_state = RC_DONE;
249 			rp->rc_timestamp = time.tv_sec;
250 			if (nonidempotent[proc]) {
251 				if (repliesstatus[proc]) {
252 					rp->rc_status = repstat;
253 					rp->rc_flag |= RC_REPSTATUS;
254 				} else {
255 					rp->rc_reply = NFSMCOPY(repmbuf, 0,
256 							M_COPYALL, M_WAIT);
257 					rp->rc_flag |= RC_REPMBUF;
258 				}
259 			}
260 			rp->rc_flag &= ~RC_LOCKED;
261 			if (rp->rc_flag & RC_WANTED) {
262 				rp->rc_flag &= ~RC_WANTED;
263 				wakeup((caddr_t)rp);
264 			}
265 			return;
266 		}
267 	}
268 }
269