xref: /original-bsd/sys/nfs/nfs_srvcache.c (revision 37071c60)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  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	8.1 (Berkeley) 06/10/93
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 
29 #include <netinet/in.h>
30 #ifdef ISO
31 #include <netiso/iso.h>
32 #endif
33 #include <nfs/nfsm_subs.h>
34 #include <nfs/rpcv2.h>
35 #include <nfs/nfsv2.h>
36 #include <nfs/nfs.h>
37 #include <nfs/nfsrvcache.h>
38 #include <nfs/nqnfs.h>
39 
40 long numnfsrvcache, desirednfsrvcache = NFSRVCACHESIZ;
41 
42 #define	NFSRCHASH(xid)		(((xid) + ((xid) >> 24)) & rheadhash)
43 static struct nfsrvcache *nfsrvlruhead, **nfsrvlrutail = &nfsrvlruhead;
44 static struct nfsrvcache **rheadhtbl;
45 static u_long rheadhash;
46 
47 #define TRUE	1
48 #define	FALSE	0
49 
50 #define	NETFAMILY(rp) \
51 		(((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO)
52 
53 /*
54  * Static array that defines which nfs rpc's are nonidempotent
55  */
56 int nonidempotent[NFS_NPROCS] = {
57 	FALSE,
58 	FALSE,
59 	TRUE,
60 	FALSE,
61 	FALSE,
62 	FALSE,
63 	FALSE,
64 	FALSE,
65 	TRUE,
66 	TRUE,
67 	TRUE,
68 	TRUE,
69 	TRUE,
70 	TRUE,
71 	TRUE,
72 	TRUE,
73 	FALSE,
74 	FALSE,
75 	FALSE,
76 	FALSE,
77 	FALSE,
78 	FALSE,
79 	FALSE,
80 };
81 
82 /* True iff the rpc reply is an nfs status ONLY! */
83 static int repliesstatus[NFS_NPROCS] = {
84 	FALSE,
85 	FALSE,
86 	FALSE,
87 	FALSE,
88 	FALSE,
89 	FALSE,
90 	FALSE,
91 	FALSE,
92 	FALSE,
93 	FALSE,
94 	TRUE,
95 	TRUE,
96 	TRUE,
97 	TRUE,
98 	FALSE,
99 	TRUE,
100 	FALSE,
101 	FALSE,
102 	FALSE,
103 	FALSE,
104 	FALSE,
105 	FALSE,
106 	TRUE,
107 };
108 
109 /*
110  * Initialize the server request cache list
111  */
112 nfsrv_initcache()
113 {
114 
115 	rheadhtbl = hashinit(desirednfsrvcache, M_NFSD, &rheadhash);
116 }
117 
118 /*
119  * Look for the request in the cache
120  * If found then
121  *    return action and optionally reply
122  * else
123  *    insert it in the cache
124  *
125  * The rules are as follows:
126  * - if in progress, return DROP request
127  * - if completed within DELAY of the current time, return DROP it
128  * - if completed a longer time ago return REPLY if the reply was cached or
129  *   return DOIT
130  * Update/add new request at end of lru list
131  */
132 nfsrv_getcache(nam, nd, repp)
133 	struct mbuf *nam;
134 	register struct nfsd *nd;
135 	struct mbuf **repp;
136 {
137 	register struct nfsrvcache *rp, *rq, **rpp;
138 	struct mbuf *mb;
139 	struct sockaddr_in *saddr;
140 	caddr_t bpos;
141 	int ret;
142 
143 	if (nd->nd_nqlflag != NQL_NOVAL)
144 		return (RC_DOIT);
145 	rpp = &rheadhtbl[NFSRCHASH(nd->nd_retxid)];
146 loop:
147 	for (rp = *rpp; rp; rp = rp->rc_forw) {
148 	    if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
149 		netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nam)) {
150 			if ((rp->rc_flag & RC_LOCKED) != 0) {
151 				rp->rc_flag |= RC_WANTED;
152 				(void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
153 				goto loop;
154 			}
155 			rp->rc_flag |= RC_LOCKED;
156 			/* If not at end of LRU chain, move it there */
157 			if (rp->rc_next) {
158 				/* remove from LRU chain */
159 				*rp->rc_prev = rp->rc_next;
160 				rp->rc_next->rc_prev = rp->rc_prev;
161 				/* and replace at end of it */
162 				rp->rc_next = NULL;
163 				rp->rc_prev = nfsrvlrutail;
164 				*nfsrvlrutail = rp;
165 				nfsrvlrutail = &rp->rc_next;
166 			}
167 			if (rp->rc_state == RC_UNUSED)
168 				panic("nfsrv cache");
169 			if (rp->rc_state == RC_INPROG) {
170 				nfsstats.srvcache_inproghits++;
171 				ret = RC_DROPIT;
172 			} else if (rp->rc_flag & RC_REPSTATUS) {
173 				nfsstats.srvcache_nonidemdonehits++;
174 				nfs_rephead(0, nd, rp->rc_status,
175 				   0, (u_quad_t *)0, repp, &mb, &bpos);
176 				ret = RC_REPLY;
177 			} else if (rp->rc_flag & RC_REPMBUF) {
178 				nfsstats.srvcache_nonidemdonehits++;
179 				*repp = m_copym(rp->rc_reply, 0, M_COPYALL,
180 						M_WAIT);
181 				ret = RC_REPLY;
182 			} else {
183 				nfsstats.srvcache_idemdonehits++;
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_forw = 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, 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 			 */
286 			if (repvalid && nonidempotent[nd->nd_procnum]) {
287 				if (repliesstatus[nd->nd_procnum]) {
288 					rp->rc_status = nd->nd_repstat;
289 					rp->rc_flag |= RC_REPSTATUS;
290 				} else {
291 					rp->rc_reply = m_copym(repmbuf,
292 						0, M_COPYALL, M_WAIT);
293 					rp->rc_flag |= RC_REPMBUF;
294 				}
295 			}
296 			rp->rc_flag &= ~RC_LOCKED;
297 			if (rp->rc_flag & RC_WANTED) {
298 				rp->rc_flag &= ~RC_WANTED;
299 				wakeup((caddr_t)rp);
300 			}
301 			return;
302 		}
303 	}
304 }
305 
306 /*
307  * Clean out the cache. Called when the last nfsd terminates.
308  */
309 void
310 nfsrv_cleancache()
311 {
312 	register struct nfsrvcache *rp, *nextrp;
313 
314 	for (rp = nfsrvlruhead; rp; rp = nextrp) {
315 		nextrp = rp->rc_next;
316 		free(rp, M_NFSD);
317 	}
318 	bzero((char *)rheadhtbl, (rheadhash + 1) * sizeof(void *));
319 	nfsrvlruhead = NULL;
320 	nfsrvlrutail = &nfsrvlruhead;
321 	numnfsrvcache = 0;
322 }
323