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