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