xref: /original-bsd/sys/nfs/nfs_srvcache.c (revision 9f9a0d6d)
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.6 (Berkeley) 05/14/90
21  */
22 
23 #include "param.h"
24 #include "user.h"
25 #include "vnode.h"
26 #include "mount.h"
27 #include "kernel.h"
28 #include "systm.h"
29 #include "mbuf.h"
30 #include "socket.h"
31 #include "socketvar.h"
32 #include "../netinet/in.h"
33 #include "nfsm_subs.h"
34 #include "nfsv2.h"
35 #include "nfsrvcache.h"
36 #include "nfs.h"
37 
38 #if	((NFSRCHSZ&(NFSRCHSZ-1)) == 0)
39 #define	NFSRCHASH(xid)		(((xid)+((xid)>>16))&(NFSRCHSZ-1))
40 #else
41 #define	NFSRCHASH(xid)		(((unsigned)((xid)+((xid)>>16)))%NFSRCHSZ)
42 #endif
43 
44 union rhead {
45 	union  rhead *rh_head[2];
46 	struct nfsrvcache *rh_chain[2];
47 } rhead[NFSRCHSZ];
48 
49 static struct nfsrvcache nfsrvcachehead;
50 static struct nfsrvcache nfsrvcache[NFSRVCACHESIZ];
51 
52 #define TRUE	1
53 #define	FALSE	0
54 
55 /*
56  * Static array that defines which nfs rpc's are nonidempotent
57  */
58 int nonidempotent[NFS_NPROCS] = {
59 	FALSE,
60 	FALSE,
61 	TRUE,
62 	FALSE,
63 	FALSE,
64 	FALSE,
65 	FALSE,
66 	FALSE,
67 	TRUE,
68 	TRUE,
69 	TRUE,
70 	TRUE,
71 	TRUE,
72 	TRUE,
73 	TRUE,
74 	TRUE,
75 	FALSE,
76 	FALSE,
77 };
78 
79 /* True iff the rpc reply is an nfs status ONLY! */
80 static int repliesstatus[NFS_NPROCS] = {
81 	FALSE,
82 	FALSE,
83 	FALSE,
84 	FALSE,
85 	FALSE,
86 	FALSE,
87 	FALSE,
88 	FALSE,
89 	FALSE,
90 	FALSE,
91 	TRUE,
92 	TRUE,
93 	TRUE,
94 	TRUE,
95 	FALSE,
96 	TRUE,
97 	FALSE,
98 	FALSE,
99 };
100 
101 /*
102  * Initialize the server request cache list
103  */
104 nfsrv_initcache()
105 {
106 	register int i;
107 	register struct nfsrvcache *rp = nfsrvcache;
108 	register struct nfsrvcache *hp = &nfsrvcachehead;
109 	register union  rhead *rh = rhead;
110 
111 	for (i = NFSRCHSZ; --i >= 0; rh++) {
112 		rh->rh_head[0] = rh;
113 		rh->rh_head[1] = rh;
114 	}
115 	hp->rc_next = hp->rc_prev = hp;
116 	for (i = NFSRVCACHESIZ; i-- > 0; ) {
117 		rp->rc_state = RC_UNUSED;
118 		rp->rc_flag = 0;
119 		rp->rc_forw = rp;
120 		rp->rc_back = rp;
121 		rp->rc_next = hp->rc_next;
122 		hp->rc_next->rc_prev = rp;
123 		rp->rc_prev = hp;
124 		hp->rc_next = rp;
125 		rp++;
126 	}
127 }
128 
129 /*
130  * Look for the request in the cache
131  * If found then
132  *    return action and optionally reply
133  * else
134  *    insert it in the cache
135  *
136  * The rules are as follows:
137  * - if in progress, return DROP request
138  * - if completed within DELAY of the current time, return DROP it
139  * - if completed a longer time ago return REPLY if the reply was cached or
140  *   return DOIT
141  * Update/add new request at end of lru list
142  */
143 nfsrv_getcache(nam, xid, proc, repp)
144 	struct mbuf *nam;
145 	u_long xid;
146 	int proc;
147 	struct mbuf **repp;
148 {
149 	register struct nfsrvcache *rp;
150 	register union  rhead *rh;
151 	struct mbuf *mb;
152 	caddr_t bpos;
153 	int ret;
154 
155 	rh = &rhead[NFSRCHASH(xid)];
156 loop:
157 	for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
158 		if (xid == rp->rc_xid && proc == rp->rc_proc &&
159 		    nfs_netaddr_match(nam, &rp->rc_nam)) {
160 			if ((rp->rc_flag & RC_LOCKED) != 0) {
161 				rp->rc_flag |= RC_WANTED;
162 				sleep((caddr_t)rp, PZERO-1);
163 				goto loop;
164 			}
165 			rp->rc_flag |= RC_LOCKED;
166 			put_at_head(rp);
167 			if (rp->rc_state == RC_UNUSED)
168 				panic("nfsrv cache");
169 			if (rp->rc_state == RC_INPROG ||
170 			   (time.tv_sec - rp->rc_timestamp) < RC_DELAY) {
171 				nfsstats.srvcache_inproghits++;
172 				ret = RC_DROPIT;
173 			} else if (rp->rc_flag & RC_REPSTATUS) {
174 				nfsstats.srvcache_idemdonehits++;
175 				nfs_rephead(0, xid, rp->rc_status, repp, &mb,
176 					&bpos);
177 				rp->rc_timestamp = time.tv_sec;
178 				ret = RC_REPLY;
179 			} else if (rp->rc_flag & RC_REPMBUF) {
180 				nfsstats.srvcache_idemdonehits++;
181 				*repp = m_copym(rp->rc_reply, 0, M_COPYALL,
182 						M_WAIT);
183 				rp->rc_timestamp = time.tv_sec;
184 				ret = RC_REPLY;
185 			} else {
186 				nfsstats.srvcache_nonidemdonehits++;
187 				rp->rc_state = RC_INPROG;
188 				ret = RC_DOIT;
189 			}
190 			rp->rc_flag &= ~RC_LOCKED;
191 			if (rp->rc_flag & RC_WANTED) {
192 				rp->rc_flag &= ~RC_WANTED;
193 				wakeup((caddr_t)rp);
194 			}
195 			return (ret);
196 		}
197 	}
198 	nfsstats.srvcache_misses++;
199 	rp = nfsrvcachehead.rc_prev;
200 	while ((rp->rc_flag & RC_LOCKED) != 0) {
201 		rp->rc_flag |= RC_WANTED;
202 		sleep((caddr_t)rp, PZERO-1);
203 	}
204 	remque(rp);
205 	put_at_head(rp);
206 	if (rp->rc_flag & RC_REPMBUF)
207 		mb = rp->rc_reply;
208 	else
209 		mb = (struct mbuf *)0;
210 	rp->rc_flag = 0;
211 	rp->rc_state = RC_INPROG;
212 	rp->rc_xid = xid;
213 	bcopy((caddr_t)nam, (caddr_t)&rp->rc_nam, sizeof (struct mbuf));
214 	rp->rc_proc = proc;
215 	insque(rp, rh);
216 	if (mb)
217 		m_freem(mb);
218 	return (RC_DOIT);
219 }
220 
221 /*
222  * Update a request cache entry after the rpc has been done
223  */
224 nfsrv_updatecache(nam, xid, proc, repvalid, repstat, repmbuf)
225 	struct mbuf *nam;
226 	u_long xid;
227 	int proc;
228 	int repvalid;
229 	int repstat;
230 	struct mbuf *repmbuf;
231 {
232 	register struct nfsrvcache *rp;
233 	register union	rhead *rh;
234 
235 	rh = &rhead[NFSRCHASH(xid)];
236 loop:
237 	for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
238 		if (xid == rp->rc_xid && proc == rp->rc_proc &&
239 		    nfs_netaddr_match(nam, &rp->rc_nam)) {
240 			if ((rp->rc_flag & RC_LOCKED) != 0) {
241 				rp->rc_flag |= RC_WANTED;
242 				sleep((caddr_t)rp, PZERO-1);
243 				goto loop;
244 			}
245 			rp->rc_flag |= RC_LOCKED;
246 			rp->rc_state = RC_DONE;
247 			/*
248 			 * If we have a valid reply update status and save
249 			 * the reply for non-idempotent rpc's.
250 			 * Otherwise invalidate entry by setting the timestamp
251 			 * to nil.
252 			 */
253 			if (repvalid) {
254 				rp->rc_timestamp = time.tv_sec;
255 				if (nonidempotent[proc]) {
256 					if (repliesstatus[proc]) {
257 						rp->rc_status = repstat;
258 						rp->rc_flag |= RC_REPSTATUS;
259 					} else {
260 						rp->rc_reply = m_copym(repmbuf,
261 							0, M_COPYALL, M_WAIT);
262 						rp->rc_flag |= RC_REPMBUF;
263 					}
264 				}
265 			} else {
266 				rp->rc_timestamp = 0;
267 			}
268 			rp->rc_flag &= ~RC_LOCKED;
269 			if (rp->rc_flag & RC_WANTED) {
270 				rp->rc_flag &= ~RC_WANTED;
271 				wakeup((caddr_t)rp);
272 			}
273 			return;
274 		}
275 	}
276 }
277