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