nfs_srvcache.c revision 1.34 1 /* $NetBSD: nfs_srvcache.c,v 1.34 2007/01/17 12:40:08 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.34 2007/01/17 12:40:08 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 #define NETFAMILY(rp) \
85 (((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO)
86
87 static struct nfsrvcache *nfsrv_lookupcache(struct nfsrv_descript *nd);
88 static void nfsrv_unlockcache(struct nfsrvcache *rp);
89
90 /*
91 * Static array that defines which nfs rpc's are nonidempotent
92 */
93 const int nonidempotent[NFS_NPROCS] = {
94 FALSE, /* NULL */
95 FALSE, /* GETATTR */
96 TRUE, /* SETATTR */
97 FALSE, /* LOOKUP */
98 FALSE, /* ACCESS */
99 FALSE, /* READLINK */
100 FALSE, /* READ */
101 TRUE, /* WRITE */
102 TRUE, /* CREATE */
103 TRUE, /* MKDIR */
104 TRUE, /* SYMLINK */
105 TRUE, /* MKNOD */
106 TRUE, /* REMOVE */
107 TRUE, /* RMDIR */
108 TRUE, /* RENAME */
109 TRUE, /* LINK */
110 FALSE, /* READDIR */
111 FALSE, /* READDIRPLUS */
112 FALSE, /* FSSTAT */
113 FALSE, /* FSINFO */
114 FALSE, /* PATHCONF */
115 FALSE, /* COMMIT */
116 FALSE, /* NOOP */
117 };
118
119 /* True iff the rpc reply is an nfs status ONLY! */
120 static const int nfsv2_repstat[NFS_NPROCS] = {
121 FALSE, /* NULL */
122 FALSE, /* GETATTR */
123 FALSE, /* SETATTR */
124 FALSE, /* NOOP */
125 FALSE, /* LOOKUP */
126 FALSE, /* READLINK */
127 FALSE, /* READ */
128 FALSE, /* Obsolete WRITECACHE */
129 FALSE, /* WRITE */
130 FALSE, /* CREATE */
131 TRUE, /* REMOVE */
132 TRUE, /* RENAME */
133 TRUE, /* LINK */
134 TRUE, /* SYMLINK */
135 FALSE, /* MKDIR */
136 TRUE, /* RMDIR */
137 FALSE, /* READDIR */
138 FALSE, /* STATFS */
139 };
140
141 static void
142 cleanentry(struct nfsrvcache *rp)
143 {
144
145 if ((rp->rc_flag & RC_REPMBUF) != 0) {
146 m_freem(rp->rc_reply);
147 }
148 if ((rp->rc_flag & RC_NAM) != 0) {
149 m_free(rp->rc_nam);
150 }
151 rp->rc_flag &= ~(RC_REPSTATUS|RC_REPMBUF);
152 }
153
154 /*
155 * Initialize the server request cache list
156 */
157 void
158 nfsrv_initcache()
159 {
160
161 nfsrvhashtbl = hashinit(desirednfsrvcache, HASH_LIST, M_NFSD,
162 M_WAITOK, &nfsrvhash);
163 TAILQ_INIT(&nfsrvlruhead);
164 pool_init(&nfs_reqcache_pool, sizeof(struct nfsrvcache), 0, 0, 0,
165 "nfsreqcachepl", &pool_allocator_nointr);
166 }
167
168 /*
169 * Lookup a cache and lock it
170 */
171 static struct nfsrvcache *
172 nfsrv_lookupcache(nd)
173 struct nfsrv_descript *nd;
174 {
175 struct nfsrvcache *rp;
176
177 LOCK_ASSERT(simple_lock_held(&nfsrv_reqcache_lock));
178
179 loop:
180 LIST_FOREACH(rp, NFSRCHASH(nd->nd_retxid), rc_hash) {
181 if (nd->nd_retxid == rp->rc_xid &&
182 nd->nd_procnum == rp->rc_proc &&
183 netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
184 if ((rp->rc_flag & RC_LOCKED) != 0) {
185 rp->rc_flag |= RC_WANTED;
186 (void) ltsleep(rp, PZERO - 1, "nfsrc", 0,
187 &nfsrv_reqcache_lock);
188 goto loop;
189 }
190 rp->rc_flag |= RC_LOCKED;
191 break;
192 }
193 }
194
195 return rp;
196 }
197
198 /*
199 * Unlock a cache
200 */
201 static void
202 nfsrv_unlockcache(rp)
203 struct nfsrvcache *rp;
204 {
205
206 LOCK_ASSERT(simple_lock_held(&nfsrv_reqcache_lock));
207
208 rp->rc_flag &= ~RC_LOCKED;
209 if (rp->rc_flag & RC_WANTED) {
210 rp->rc_flag &= ~RC_WANTED;
211 wakeup(rp);
212 }
213 }
214
215 /*
216 * Look for the request in the cache
217 * If found then
218 * return action and optionally reply
219 * else
220 * insert it in the cache
221 *
222 * The rules are as follows:
223 * - if in progress, return DROP request
224 * - if completed within DELAY of the current time, return DROP it
225 * - if completed a longer time ago return REPLY if the reply was cached or
226 * return DOIT
227 * Update/add new request at end of lru list
228 */
229 int
230 nfsrv_getcache(nd, slp, repp)
231 struct nfsrv_descript *nd;
232 struct nfssvc_sock *slp;
233 struct mbuf **repp;
234 {
235 struct nfsrvcache *rp, *rpdup;
236 struct mbuf *mb;
237 struct sockaddr_in *saddr;
238 caddr_t bpos;
239 int ret;
240
241 simple_lock(&nfsrv_reqcache_lock);
242 rp = nfsrv_lookupcache(nd);
243 if (rp) {
244 simple_unlock(&nfsrv_reqcache_lock);
245 found:
246 /* If not at end of LRU chain, move it there */
247 if (TAILQ_NEXT(rp, rc_lru)) { /* racy but ok */
248 simple_lock(&nfsrv_reqcache_lock);
249 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
250 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
251 simple_unlock(&nfsrv_reqcache_lock);
252 }
253 if (rp->rc_state == RC_UNUSED)
254 panic("nfsrv cache");
255 if (rp->rc_state == RC_INPROG) {
256 nfsstats.srvcache_inproghits++;
257 ret = RC_DROPIT;
258 } else if (rp->rc_flag & RC_REPSTATUS) {
259 nfsstats.srvcache_nonidemdonehits++;
260 nfs_rephead(0, nd, slp, rp->rc_status,
261 0, (u_quad_t *)0, repp, &mb, &bpos);
262 ret = RC_REPLY;
263 } else if (rp->rc_flag & RC_REPMBUF) {
264 nfsstats.srvcache_nonidemdonehits++;
265 *repp = m_copym(rp->rc_reply, 0, M_COPYALL,
266 M_WAIT);
267 ret = RC_REPLY;
268 } else {
269 nfsstats.srvcache_idemdonehits++;
270 rp->rc_state = RC_INPROG;
271 ret = RC_DOIT;
272 }
273 simple_lock(&nfsrv_reqcache_lock);
274 nfsrv_unlockcache(rp);
275 simple_unlock(&nfsrv_reqcache_lock);
276 return ret;
277 }
278 nfsstats.srvcache_misses++;
279 if (numnfsrvcache < desirednfsrvcache) {
280 numnfsrvcache++;
281 simple_unlock(&nfsrv_reqcache_lock);
282 rp = pool_get(&nfs_reqcache_pool, PR_WAITOK);
283 memset(rp, 0, sizeof *rp);
284 rp->rc_flag = RC_LOCKED;
285 } else {
286 rp = TAILQ_FIRST(&nfsrvlruhead);
287 while ((rp->rc_flag & RC_LOCKED) != 0) {
288 rp->rc_flag |= RC_WANTED;
289 (void) ltsleep(rp, PZERO-1, "nfsrc", 0,
290 &nfsrv_reqcache_lock);
291 rp = TAILQ_FIRST(&nfsrvlruhead);
292 }
293 rp->rc_flag |= RC_LOCKED;
294 LIST_REMOVE(rp, rc_hash);
295 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
296 simple_unlock(&nfsrv_reqcache_lock);
297 cleanentry(rp);
298 rp->rc_flag &= (RC_LOCKED | RC_WANTED);
299 }
300 rp->rc_state = RC_INPROG;
301 rp->rc_xid = nd->nd_retxid;
302 saddr = mtod(nd->nd_nam, struct sockaddr_in *);
303 switch (saddr->sin_family) {
304 case AF_INET:
305 rp->rc_flag |= RC_INETADDR;
306 rp->rc_inetaddr = saddr->sin_addr.s_addr;
307 break;
308 case AF_ISO:
309 default:
310 rp->rc_flag |= RC_NAM;
311 rp->rc_nam = m_copym(nd->nd_nam, 0, M_COPYALL, M_WAIT);
312 break;
313 };
314 rp->rc_proc = nd->nd_procnum;
315 simple_lock(&nfsrv_reqcache_lock);
316 rpdup = nfsrv_lookupcache(nd);
317 if (rpdup != NULL) {
318 /*
319 * other thread made duplicate cache entry.
320 */
321 simple_unlock(&nfsrv_reqcache_lock);
322 pool_put(&nfs_reqcache_pool, rp);
323 rp = rpdup;
324 goto found;
325 }
326 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
327 LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
328 nfsrv_unlockcache(rp);
329 simple_unlock(&nfsrv_reqcache_lock);
330 return RC_DOIT;
331 }
332
333 /*
334 * Update a request cache entry after the rpc has been done
335 */
336 void
337 nfsrv_updatecache(nd, repvalid, repmbuf)
338 struct nfsrv_descript *nd;
339 int repvalid;
340 struct mbuf *repmbuf;
341 {
342 struct nfsrvcache *rp;
343
344 if (!nd->nd_nam2)
345 return;
346 simple_lock(&nfsrv_reqcache_lock);
347 rp = nfsrv_lookupcache(nd);
348 simple_unlock(&nfsrv_reqcache_lock);
349 if (rp) {
350 cleanentry(rp);
351 rp->rc_state = RC_DONE;
352 /*
353 * If we have a valid reply update status and save
354 * the reply for non-idempotent rpc's.
355 */
356 if (repvalid && nonidempotent[nd->nd_procnum]) {
357 if ((nd->nd_flag & ND_NFSV3) == 0 &&
358 nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
359 rp->rc_status = nd->nd_repstat;
360 rp->rc_flag |= RC_REPSTATUS;
361 } else {
362 rp->rc_reply = m_copym(repmbuf,
363 0, M_COPYALL, M_WAIT);
364 rp->rc_flag |= RC_REPMBUF;
365 }
366 }
367 simple_lock(&nfsrv_reqcache_lock);
368 nfsrv_unlockcache(rp);
369 simple_unlock(&nfsrv_reqcache_lock);
370 }
371 }
372
373 /*
374 * Clean out the cache. Called when the last nfsd terminates.
375 */
376 void
377 nfsrv_cleancache()
378 {
379 struct nfsrvcache *rp, *nextrp;
380
381 simple_lock(&nfsrv_reqcache_lock);
382 for (rp = TAILQ_FIRST(&nfsrvlruhead); rp != 0; rp = nextrp) {
383 nextrp = TAILQ_NEXT(rp, rc_lru);
384 LIST_REMOVE(rp, rc_hash);
385 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
386 KASSERT((rp->rc_flag & (RC_LOCKED|RC_WANTED)) == 0);
387 cleanentry(rp);
388 pool_put(&nfs_reqcache_pool, rp);
389 }
390 numnfsrvcache = 0;
391 simple_unlock(&nfsrv_reqcache_lock);
392 }
393