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