nfs_srvcache.c revision 1.11 1 /* $NetBSD: nfs_srvcache.c,v 1.11 1996/02/09 21:48:32 christos Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nfs_srvcache.c 8.2 (Berkeley) 8/18/94
39 */
40
41 /*
42 * Reference: Chet Juszczak, "Improving the Performance and Correctness
43 * of an NFS Server", in Proc. Winter 1989 USENIX Conference,
44 * pages 53-63. San Diego, February 1989.
45 */
46 #include <sys/param.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/proc.h>
52 #include <sys/mbuf.h>
53 #include <sys/malloc.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56
57 #include <netinet/in.h>
58 #ifdef ISO
59 #include <netiso/iso.h>
60 #endif
61 #include <nfs/nfsm_subs.h>
62 #include <nfs/rpcv2.h>
63 #include <nfs/nfsv2.h>
64 #include <nfs/nfs.h>
65 #include <nfs/nfsrvcache.h>
66 #include <nfs/nqnfs.h>
67 #include <nfs/nfs_var.h>
68
69 long numnfsrvcache, desirednfsrvcache = NFSRVCACHESIZ;
70
71 #define NFSRCHASH(xid) \
72 (&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
73 LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
74 TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
75 u_long nfsrvhash;
76
77 #define TRUE 1
78 #define FALSE 0
79
80 #define NETFAMILY(rp) \
81 (((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO)
82
83 /*
84 * Static array that defines which nfs rpc's are nonidempotent
85 */
86 int nonidempotent[NFS_NPROCS] = {
87 FALSE,
88 FALSE,
89 TRUE,
90 FALSE,
91 FALSE,
92 FALSE,
93 FALSE,
94 FALSE,
95 TRUE,
96 TRUE,
97 TRUE,
98 TRUE,
99 TRUE,
100 TRUE,
101 TRUE,
102 TRUE,
103 FALSE,
104 FALSE,
105 FALSE,
106 FALSE,
107 FALSE,
108 FALSE,
109 FALSE,
110 };
111
112 /* True iff the rpc reply is an nfs status ONLY! */
113 static int repliesstatus[NFS_NPROCS] = {
114 FALSE,
115 FALSE,
116 FALSE,
117 FALSE,
118 FALSE,
119 FALSE,
120 FALSE,
121 FALSE,
122 FALSE,
123 FALSE,
124 TRUE,
125 TRUE,
126 TRUE,
127 TRUE,
128 FALSE,
129 TRUE,
130 FALSE,
131 FALSE,
132 FALSE,
133 FALSE,
134 FALSE,
135 FALSE,
136 TRUE,
137 };
138
139 /*
140 * Initialize the server request cache list
141 */
142 void
143 nfsrv_initcache()
144 {
145
146 nfsrvhashtbl = hashinit(desirednfsrvcache, M_NFSD, &nfsrvhash);
147 TAILQ_INIT(&nfsrvlruhead);
148 }
149
150 /*
151 * Look for the request in the cache
152 * If found then
153 * return action and optionally reply
154 * else
155 * insert it in the cache
156 *
157 * The rules are as follows:
158 * - if in progress, return DROP request
159 * - if completed within DELAY of the current time, return DROP it
160 * - if completed a longer time ago return REPLY if the reply was cached or
161 * return DOIT
162 * Update/add new request at end of lru list
163 */
164 int
165 nfsrv_getcache(nam, nd, repp)
166 struct mbuf *nam;
167 register struct nfsd *nd;
168 struct mbuf **repp;
169 {
170 register struct nfsrvcache *rp;
171 struct mbuf *mb;
172 struct sockaddr_in *saddr;
173 caddr_t bpos;
174 int ret;
175
176 if (nd->nd_nqlflag != NQL_NOVAL)
177 return (RC_DOIT);
178 loop:
179 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
180 rp = rp->rc_hash.le_next) {
181 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
182 netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nam)) {
183 if ((rp->rc_flag & RC_LOCKED) != 0) {
184 rp->rc_flag |= RC_WANTED;
185 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
186 goto loop;
187 }
188 rp->rc_flag |= RC_LOCKED;
189 /* If not at end of LRU chain, move it there */
190 if (rp->rc_lru.tqe_next) {
191 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
192 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
193 }
194 if (rp->rc_state == RC_UNUSED)
195 panic("nfsrv cache");
196 if (rp->rc_state == RC_INPROG) {
197 nfsstats.srvcache_inproghits++;
198 ret = RC_DROPIT;
199 } else if (rp->rc_flag & RC_REPSTATUS) {
200 nfsstats.srvcache_nonidemdonehits++;
201 nfs_rephead(0, nd, rp->rc_status,
202 0, (u_quad_t *)0, repp, &mb, &bpos);
203 ret = RC_REPLY;
204 } else if (rp->rc_flag & RC_REPMBUF) {
205 nfsstats.srvcache_nonidemdonehits++;
206 *repp = m_copym(rp->rc_reply, 0, M_COPYALL,
207 M_WAIT);
208 ret = RC_REPLY;
209 } else {
210 nfsstats.srvcache_idemdonehits++;
211 rp->rc_state = RC_INPROG;
212 ret = RC_DOIT;
213 }
214 rp->rc_flag &= ~RC_LOCKED;
215 if (rp->rc_flag & RC_WANTED) {
216 rp->rc_flag &= ~RC_WANTED;
217 wakeup((caddr_t)rp);
218 }
219 return (ret);
220 }
221 }
222 nfsstats.srvcache_misses++;
223 if (numnfsrvcache < desirednfsrvcache) {
224 rp = (struct nfsrvcache *)malloc((u_long)sizeof *rp,
225 M_NFSD, M_WAITOK);
226 bzero((char *)rp, sizeof *rp);
227 numnfsrvcache++;
228 rp->rc_flag = RC_LOCKED;
229 } else {
230 rp = nfsrvlruhead.tqh_first;
231 while ((rp->rc_flag & RC_LOCKED) != 0) {
232 rp->rc_flag |= RC_WANTED;
233 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
234 rp = nfsrvlruhead.tqh_first;
235 }
236 rp->rc_flag |= RC_LOCKED;
237 LIST_REMOVE(rp, rc_hash);
238 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
239 if (rp->rc_flag & RC_REPMBUF)
240 m_freem(rp->rc_reply);
241 if (rp->rc_flag & RC_NAM)
242 MFREE(rp->rc_nam, mb);
243 rp->rc_flag &= (RC_LOCKED | RC_WANTED);
244 }
245 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
246 rp->rc_state = RC_INPROG;
247 rp->rc_xid = nd->nd_retxid;
248 saddr = mtod(nam, struct sockaddr_in *);
249 switch (saddr->sin_family) {
250 case AF_INET:
251 rp->rc_flag |= RC_INETADDR;
252 rp->rc_inetaddr = saddr->sin_addr.s_addr;
253 break;
254 case AF_ISO:
255 default:
256 rp->rc_flag |= RC_NAM;
257 rp->rc_nam = m_copym(nam, 0, M_COPYALL, M_WAIT);
258 break;
259 };
260 rp->rc_proc = nd->nd_procnum;
261 LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
262 rp->rc_flag &= ~RC_LOCKED;
263 if (rp->rc_flag & RC_WANTED) {
264 rp->rc_flag &= ~RC_WANTED;
265 wakeup((caddr_t)rp);
266 }
267 return (RC_DOIT);
268 }
269
270 /*
271 * Update a request cache entry after the rpc has been done
272 */
273 void
274 nfsrv_updatecache(nam, nd, repvalid, repmbuf)
275 struct mbuf *nam;
276 register struct nfsd *nd;
277 int repvalid;
278 struct mbuf *repmbuf;
279 {
280 register struct nfsrvcache *rp;
281
282 if (nd->nd_nqlflag != NQL_NOVAL)
283 return;
284 loop:
285 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
286 rp = rp->rc_hash.le_next) {
287 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
288 netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nam)) {
289 if ((rp->rc_flag & RC_LOCKED) != 0) {
290 rp->rc_flag |= RC_WANTED;
291 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
292 goto loop;
293 }
294 rp->rc_flag |= RC_LOCKED;
295 rp->rc_state = RC_DONE;
296 /*
297 * If we have a valid reply update status and save
298 * the reply for non-idempotent rpc's.
299 */
300 if (repvalid && nonidempotent[nd->nd_procnum]) {
301 if (repliesstatus[nd->nd_procnum]) {
302 rp->rc_status = nd->nd_repstat;
303 rp->rc_flag |= RC_REPSTATUS;
304 } else {
305 rp->rc_reply = m_copym(repmbuf,
306 0, M_COPYALL, M_WAIT);
307 rp->rc_flag |= RC_REPMBUF;
308 }
309 }
310 rp->rc_flag &= ~RC_LOCKED;
311 if (rp->rc_flag & RC_WANTED) {
312 rp->rc_flag &= ~RC_WANTED;
313 wakeup((caddr_t)rp);
314 }
315 return;
316 }
317 }
318 }
319
320 /*
321 * Clean out the cache. Called when the last nfsd terminates.
322 */
323 void
324 nfsrv_cleancache()
325 {
326 register struct nfsrvcache *rp, *nextrp;
327
328 for (rp = nfsrvlruhead.tqh_first; rp != 0; rp = nextrp) {
329 nextrp = rp->rc_lru.tqe_next;
330 LIST_REMOVE(rp, rc_hash);
331 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
332 free(rp, M_NFSD);
333 }
334 numnfsrvcache = 0;
335 }
336