xref: /original-bsd/sys/nfs/nfs_srvcache.c (revision 860e07fc)
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  * %sccs.include.redist.c%
9  *
10  *	@(#)nfs_srvcache.c	7.16 (Berkeley) 07/22/92
11  */
12 
13 /*
14  * Reference: Chet Juszczak, "Improving the Performance and Correctness
15  *		of an NFS Server", in Proc. Winter 1989 USENIX Conference,
16  *		pages 53-63. San Diego, February 1989.
17  */
18 #include <sys/param.h>
19 #include <sys/vnode.h>
20 #include <sys/mount.h>
21 #include <sys/kernel.h>
22 #include <sys/systm.h>
23 #include <sys/proc.h>
24 #include <sys/mbuf.h>
25 #include <sys/malloc.h>
26 #include <sys/socket.h>
27 #include <sys/socketvar.h>
28 #include <netinet/in.h>
29 #ifdef ISO
30 #include <netiso/iso.h>
31 #endif
32 #include <nfs/nfsm_subs.h>
33 #include <nfs/rpcv2.h>
34 #include <nfs/nfsv2.h>
35 #include <nfs/nfs.h>
36 #include <nfs/nfsrvcache.h>
37 #include <nfs/nqnfs.h>
38 
39 long numnfsrvcache, desirednfsrvcache = NFSRVCACHESIZ;
40 
41 #define	NFSRCHASH(xid)		(((xid) + ((xid) >> 16)) & rheadhash)
42 static struct nfsrvcache *nfsrvlruhead, **nfsrvlrutail = &nfsrvlruhead;
43 static struct nfsrvcache **rheadhtbl;
44 static u_long rheadhash;
45 
46 #define TRUE	1
47 #define	FALSE	0
48 
49 #define	NETFAMILY(rp) \
50 		(((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO)
51 
52 /*
53  * Static array that defines which nfs rpc's are nonidempotent
54  */
55 int nonidempotent[NFS_NPROCS] = {
56 	FALSE,
57 	FALSE,
58 	TRUE,
59 	FALSE,
60 	FALSE,
61 	FALSE,
62 	FALSE,
63 	FALSE,
64 	TRUE,
65 	TRUE,
66 	TRUE,
67 	TRUE,
68 	TRUE,
69 	TRUE,
70 	TRUE,
71 	TRUE,
72 	FALSE,
73 	FALSE,
74 	FALSE,
75 	FALSE,
76 	FALSE,
77 	FALSE,
78 };
79 
80 /* True iff the rpc reply is an nfs status ONLY! */
81 static int repliesstatus[NFS_NPROCS] = {
82 	FALSE,
83 	FALSE,
84 	FALSE,
85 	FALSE,
86 	FALSE,
87 	FALSE,
88 	FALSE,
89 	FALSE,
90 	FALSE,
91 	FALSE,
92 	TRUE,
93 	TRUE,
94 	TRUE,
95 	TRUE,
96 	FALSE,
97 	TRUE,
98 	FALSE,
99 	FALSE,
100 	FALSE,
101 	FALSE,
102 	FALSE,
103 	FALSE,
104 };
105 
106 /*
107  * Initialize the server request cache list
108  */
109 nfsrv_initcache()
110 {
111 
112 	rheadhtbl = hashinit(desirednfsrvcache, M_NFSD, &rheadhash);
113 }
114 
115 /*
116  * Look for the request in the cache
117  * If found then
118  *    return action and optionally reply
119  * else
120  *    insert it in the cache
121  *
122  * The rules are as follows:
123  * - if in progress, return DROP request
124  * - if completed within DELAY of the current time, return DROP it
125  * - if completed a longer time ago return REPLY if the reply was cached or
126  *   return DOIT
127  * Update/add new request at end of lru list
128  */
129 nfsrv_getcache(nam, nd, repp)
130 	struct mbuf *nam;
131 	register struct nfsd *nd;
132 	struct mbuf **repp;
133 {
134 	register struct nfsrvcache *rp, *rq, **rpp;
135 	struct mbuf *mb;
136 	struct sockaddr_in *saddr;
137 	caddr_t bpos;
138 	int ret;
139 
140 	if (nd->nd_nqlflag != NQL_NOVAL)
141 		return (RC_DOIT);
142 	rpp = &rheadhtbl[NFSRCHASH(nd->nd_retxid)];
143 loop:
144 	for (rp = *rpp; rp; rp = rp->rc_forw) {
145 	    if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
146 		netaddr_match(NETFAMILY(rp), &rp->rc_haddr, (union nethostaddr *)0, nam)) {
147 			if ((rp->rc_flag & RC_LOCKED) != 0) {
148 				rp->rc_flag |= RC_WANTED;
149 				(void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
150 				goto loop;
151 			}
152 			rp->rc_flag |= RC_LOCKED;
153 			/* If not at end of LRU chain, move it there */
154 			if (rp->rc_next) {
155 				/* remove from LRU chain */
156 				*rp->rc_prev = rp->rc_next;
157 				rp->rc_next->rc_prev = rp->rc_prev;
158 				/* and replace at end of it */
159 				rp->rc_next = NULL;
160 				rp->rc_prev = nfsrvlrutail;
161 				*nfsrvlrutail = rp;
162 				nfsrvlrutail = &rp->rc_next;
163 			}
164 			if (rp->rc_state == RC_UNUSED)
165 				panic("nfsrv cache");
166 			if (rp->rc_state == RC_INPROG ||
167 			   (time.tv_sec - rp->rc_timestamp) < RC_DELAY) {
168 				nfsstats.srvcache_inproghits++;
169 				ret = RC_DROPIT;
170 			} else if (rp->rc_flag & RC_REPSTATUS) {
171 				nfsstats.srvcache_idemdonehits++;
172 				nfs_rephead(0, nd, rp->rc_status,
173 				   0, (u_quad_t *)0, repp, &mb, &bpos);
174 				rp->rc_timestamp = time.tv_sec;
175 				ret = RC_REPLY;
176 			} else if (rp->rc_flag & RC_REPMBUF) {
177 				nfsstats.srvcache_idemdonehits++;
178 				*repp = m_copym(rp->rc_reply, 0, M_COPYALL,
179 						M_WAIT);
180 				rp->rc_timestamp = time.tv_sec;
181 				ret = RC_REPLY;
182 			} else {
183 				nfsstats.srvcache_nonidemdonehits++;
184 				rp->rc_state = RC_INPROG;
185 				ret = RC_DOIT;
186 			}
187 			rp->rc_flag &= ~RC_LOCKED;
188 			if (rp->rc_flag & RC_WANTED) {
189 				rp->rc_flag &= ~RC_WANTED;
190 				wakeup((caddr_t)rp);
191 			}
192 			return (ret);
193 		}
194 	}
195 	nfsstats.srvcache_misses++;
196 	if (numnfsrvcache < desirednfsrvcache) {
197 		rp = (struct nfsrvcache *)malloc((u_long)sizeof *rp,
198 		    M_NFSD, M_WAITOK);
199 		bzero((char *)rp, sizeof *rp);
200 		numnfsrvcache++;
201 		rp->rc_flag = RC_LOCKED;
202 	} else {
203 		rp = nfsrvlruhead;
204 		while ((rp->rc_flag & RC_LOCKED) != 0) {
205 			rp->rc_flag |= RC_WANTED;
206 			(void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
207 			rp = nfsrvlruhead;
208 		}
209 		rp->rc_flag |= RC_LOCKED;
210 		/* remove from hash chain */
211 		if (rq = rp->rc_forw)
212 			rq->rc_back = rp->rc_back;
213 		*rp->rc_back = rq;
214 		/* remove from LRU chain */
215 		*rp->rc_prev = rp->rc_next;
216 		rp->rc_next->rc_prev = rp->rc_prev;
217 		if (rp->rc_flag & RC_REPMBUF)
218 			m_freem(rp->rc_reply);
219 		if (rp->rc_flag & RC_NAM)
220 			MFREE(rp->rc_nam, mb);
221 		rp->rc_flag &= (RC_LOCKED | RC_WANTED);
222 	}
223 	/* place at end of LRU list */
224 	rp->rc_next = NULL;
225 	rp->rc_prev = nfsrvlrutail;
226 	*nfsrvlrutail = rp;
227 	nfsrvlrutail = &rp->rc_next;
228 	rp->rc_state = RC_INPROG;
229 	rp->rc_xid = nd->nd_retxid;
230 	saddr = mtod(nam, struct sockaddr_in *);
231 	switch (saddr->sin_family) {
232 	case AF_INET:
233 		rp->rc_flag |= RC_INETADDR;
234 		rp->rc_inetaddr = saddr->sin_addr.s_addr;
235 		break;
236 	case AF_ISO:
237 	default:
238 		rp->rc_flag |= RC_NAM;
239 		rp->rc_nam = m_copym(nam, 0, M_COPYALL, M_WAIT);
240 		break;
241 	};
242 	rp->rc_proc = nd->nd_procnum;
243 	/* insert into hash chain */
244 	if (rq = *rpp)
245 		rq->rc_back = &rp->rc_forw;
246 	rp->rc_next = rq;
247 	rp->rc_back = rpp;
248 	*rpp = rp;
249 	rp->rc_flag &= ~RC_LOCKED;
250 	if (rp->rc_flag & RC_WANTED) {
251 		rp->rc_flag &= ~RC_WANTED;
252 		wakeup((caddr_t)rp);
253 	}
254 	return (RC_DOIT);
255 }
256 
257 /*
258  * Update a request cache entry after the rpc has been done
259  */
260 void
261 nfsrv_updatecache(nam, nd, repvalid, repmbuf)
262 	struct mbuf *nam;
263 	register struct nfsd *nd;
264 	int repvalid;
265 	struct mbuf *repmbuf;
266 {
267 	register struct nfsrvcache *rp;
268 
269 	if (nd->nd_nqlflag != NQL_NOVAL)
270 		return;
271 loop:
272 	for (rp = rheadhtbl[NFSRCHASH(nd->nd_retxid)]; rp; rp = rp->rc_forw) {
273 	    if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
274 		netaddr_match(NETFAMILY(rp), &rp->rc_haddr, (union nethostaddr *)0, nam)) {
275 			if ((rp->rc_flag & RC_LOCKED) != 0) {
276 				rp->rc_flag |= RC_WANTED;
277 				(void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
278 				goto loop;
279 			}
280 			rp->rc_flag |= RC_LOCKED;
281 			rp->rc_state = RC_DONE;
282 			/*
283 			 * If we have a valid reply update status and save
284 			 * the reply for non-idempotent rpc's.
285 			 * Otherwise invalidate entry by setting the timestamp
286 			 * to nil.
287 			 */
288 			if (repvalid) {
289 				rp->rc_timestamp = time.tv_sec;
290 				if (nonidempotent[nd->nd_procnum]) {
291 					if (repliesstatus[nd->nd_procnum]) {
292 						rp->rc_status = nd->nd_repstat;
293 						rp->rc_flag |= RC_REPSTATUS;
294 					} else {
295 						rp->rc_reply = m_copym(repmbuf,
296 							0, M_COPYALL, M_WAIT);
297 						rp->rc_flag |= RC_REPMBUF;
298 					}
299 				}
300 			} else {
301 				rp->rc_timestamp = 0;
302 			}
303 			rp->rc_flag &= ~RC_LOCKED;
304 			if (rp->rc_flag & RC_WANTED) {
305 				rp->rc_flag &= ~RC_WANTED;
306 				wakeup((caddr_t)rp);
307 			}
308 			return;
309 		}
310 	}
311 }
312 
313 /*
314  * Clean out the cache. Called when the last nfsd terminates.
315  */
316 void
317 nfsrv_cleancache()
318 {
319 	register struct nfsrvcache *rp, *nextrp;
320 
321 	for (rp = nfsrvlruhead; rp; rp = nextrp) {
322 		nextrp = rp->rc_next;
323 		free(rp, M_NFSD);
324 	}
325 	bzero((char *)rheadhtbl, (rheadhash + 1) * sizeof(void *));
326 	numnfsrvcache = 0;
327 }
328