xref: /netbsd-src/sys/nfs/nfs_srvcache.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /*	$NetBSD: nfs_srvcache.c,v 1.43 2008/11/19 18:36:09 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.43 2008/11/19 18:36:09 ad Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/vnode.h>
48 #include <sys/condvar.h>
49 #include <sys/mount.h>
50 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/lock.h>
53 #include <sys/proc.h>
54 #include <sys/pool.h>
55 #include <sys/mbuf.h>
56 #include <sys/mutex.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 
60 #include <netinet/in.h>
61 #include <nfs/nfsm_subs.h>
62 #include <nfs/rpcv2.h>
63 #include <nfs/nfsproto.h>
64 #include <nfs/nfs.h>
65 #include <nfs/nfsrvcache.h>
66 #include <nfs/nfs_var.h>
67 
68 extern struct nfsstats nfsstats;
69 extern const int nfsv2_procid[NFS_NPROCS];
70 long numnfsrvcache, desirednfsrvcache = NFSRVCACHESIZ;
71 struct pool nfs_reqcache_pool;
72 
73 #define	NFSRCHASH(xid) \
74 	(&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
75 LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
76 TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
77 kmutex_t nfsrv_reqcache_lock;
78 u_long nfsrvhash;
79 
80 #if defined(MBUFTRACE)
81 static struct mowner nfsd_cache_mowner = MOWNER_INIT("nfsd", "cache");
82 #endif /* defined(MBUFTRACE) */
83 
84 #define	NETFAMILY(rp) \
85 		(((rp)->rc_flags & RC_INETADDR) ? AF_INET : -1)
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_flags & RC_REPMBUF) != 0) {
146 		m_freem(rp->rc_reply);
147 	}
148 	if ((rp->rc_flags & RC_NAM) != 0) {
149 		m_free(rp->rc_nam);
150 	}
151 	rp->rc_flags &= ~(RC_REPSTATUS|RC_REPMBUF);
152 }
153 
154 /*
155  * Initialize the server request cache list
156  */
157 void
158 nfsrv_initcache()
159 {
160 
161 	mutex_init(&nfsrv_reqcache_lock, MUTEX_DEFAULT, IPL_NONE);
162 	nfsrvhashtbl = hashinit(desirednfsrvcache, HASH_LIST, true,
163 	    &nfsrvhash);
164 	TAILQ_INIT(&nfsrvlruhead);
165 	pool_init(&nfs_reqcache_pool, sizeof(struct nfsrvcache), 0, 0, 0,
166 	    "nfsreqcachepl", &pool_allocator_nointr, IPL_NONE);
167 	MOWNER_ATTACH(&nfsd_cache_mowner);
168 }
169 
170 void
171 nfsrv_finicache()
172 {
173 
174 	nfsrv_cleancache();
175 	KASSERT(TAILQ_EMPTY(&nfsrvlruhead));
176 	pool_destroy(&nfs_reqcache_pool);
177 	hashdone(nfsrvhashtbl, HASH_LIST, nfsrvhash);
178 	MOWNER_DETACH(&nfsd_cache_mowner);
179 	mutex_destroy(&nfsrv_reqcache_lock);
180 }
181 
182 /*
183  * Lookup a cache and lock it
184  */
185 static struct nfsrvcache *
186 nfsrv_lookupcache(nd)
187 	struct nfsrv_descript *nd;
188 {
189 	struct nfsrvcache *rp;
190 
191 	KASSERT(mutex_owned(&nfsrv_reqcache_lock));
192 
193 loop:
194 	LIST_FOREACH(rp, NFSRCHASH(nd->nd_retxid), rc_hash) {
195 		if (nd->nd_retxid == rp->rc_xid &&
196 		    nd->nd_procnum == rp->rc_proc &&
197 		    netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
198 			if ((rp->rc_gflags & RC_G_LOCKED) != 0) {
199 				cv_wait(&rp->rc_cv, &nfsrv_reqcache_lock);
200 				goto loop;
201 			}
202 			rp->rc_gflags |= RC_G_LOCKED;
203 			break;
204 		}
205 	}
206 
207 	return rp;
208 }
209 
210 /*
211  * Unlock a cache
212  */
213 static void
214 nfsrv_unlockcache(rp)
215 	struct nfsrvcache *rp;
216 {
217 
218 	KASSERT(mutex_owned(&nfsrv_reqcache_lock));
219 
220 	KASSERT((rp->rc_gflags & RC_G_LOCKED) != 0);
221 	rp->rc_gflags &= ~RC_G_LOCKED;
222 	cv_broadcast(&rp->rc_cv);
223 }
224 
225 /*
226  * Look for the request in the cache
227  * If found then
228  *    return action and optionally reply
229  * else
230  *    insert it in the cache
231  *
232  * The rules are as follows:
233  * - if in progress, return DROP request
234  * - if completed within DELAY of the current time, return DROP it
235  * - if completed a longer time ago return REPLY if the reply was cached or
236  *   return DOIT
237  * Update/add new request at end of lru list
238  */
239 int
240 nfsrv_getcache(nd, slp, repp)
241 	struct nfsrv_descript *nd;
242 	struct nfssvc_sock *slp;
243 	struct mbuf **repp;
244 {
245 	struct nfsrvcache *rp, *rpdup;
246 	struct mbuf *mb;
247 	struct sockaddr_in *saddr;
248 	char *bpos;
249 	int ret;
250 
251 	mutex_enter(&nfsrv_reqcache_lock);
252 	rp = nfsrv_lookupcache(nd);
253 	if (rp) {
254 		mutex_exit(&nfsrv_reqcache_lock);
255 found:
256 		/* If not at end of LRU chain, move it there */
257 		if (TAILQ_NEXT(rp, rc_lru)) { /* racy but ok */
258 			mutex_enter(&nfsrv_reqcache_lock);
259 			TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
260 			TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
261 			mutex_exit(&nfsrv_reqcache_lock);
262 		}
263 		if (rp->rc_state == RC_UNUSED)
264 			panic("nfsrv cache");
265 		if (rp->rc_state == RC_INPROG) {
266 			nfsstats.srvcache_inproghits++;
267 			ret = RC_DROPIT;
268 		} else if (rp->rc_flags & RC_REPSTATUS) {
269 			nfsstats.srvcache_nonidemdonehits++;
270 			nfs_rephead(0, nd, slp, rp->rc_status,
271 			   0, (u_quad_t *)0, repp, &mb, &bpos);
272 			ret = RC_REPLY;
273 		} else if (rp->rc_flags & RC_REPMBUF) {
274 			nfsstats.srvcache_nonidemdonehits++;
275 			*repp = m_copym(rp->rc_reply, 0, M_COPYALL,
276 					M_WAIT);
277 			ret = RC_REPLY;
278 		} else {
279 			nfsstats.srvcache_idemdonehits++;
280 			rp->rc_state = RC_INPROG;
281 			ret = RC_DOIT;
282 		}
283 		mutex_enter(&nfsrv_reqcache_lock);
284 		nfsrv_unlockcache(rp);
285 		mutex_exit(&nfsrv_reqcache_lock);
286 		return ret;
287 	}
288 	nfsstats.srvcache_misses++;
289 	if (numnfsrvcache < desirednfsrvcache) {
290 		numnfsrvcache++;
291 		mutex_exit(&nfsrv_reqcache_lock);
292 		rp = pool_get(&nfs_reqcache_pool, PR_WAITOK);
293 		memset(rp, 0, sizeof *rp);
294 		cv_init(&rp->rc_cv, "nfsdrc");
295 		rp->rc_gflags = RC_G_LOCKED;
296 	} else {
297 		rp = TAILQ_FIRST(&nfsrvlruhead);
298 		while ((rp->rc_gflags & RC_G_LOCKED) != 0) {
299 			cv_wait(&rp->rc_cv, &nfsrv_reqcache_lock);
300 			rp = TAILQ_FIRST(&nfsrvlruhead);
301 		}
302 		rp->rc_gflags |= RC_G_LOCKED;
303 		LIST_REMOVE(rp, rc_hash);
304 		TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
305 		mutex_exit(&nfsrv_reqcache_lock);
306 		cleanentry(rp);
307 		rp->rc_flags = 0;
308 	}
309 	rp->rc_state = RC_INPROG;
310 	rp->rc_xid = nd->nd_retxid;
311 	saddr = mtod(nd->nd_nam, struct sockaddr_in *);
312 	switch (saddr->sin_family) {
313 	case AF_INET:
314 		rp->rc_flags |= RC_INETADDR;
315 		rp->rc_inetaddr = saddr->sin_addr.s_addr;
316 		break;
317 	default:
318 		rp->rc_flags |= RC_NAM;
319 		rp->rc_nam = m_copym(nd->nd_nam, 0, M_COPYALL, M_WAIT);
320 		m_claimm(rp->rc_nam, &nfsd_cache_mowner);
321 		break;
322 	};
323 	rp->rc_proc = nd->nd_procnum;
324 	mutex_enter(&nfsrv_reqcache_lock);
325 	rpdup = nfsrv_lookupcache(nd);
326 	if (rpdup != NULL) {
327 		/*
328 		 * other thread made duplicate cache entry.
329 		 */
330 		KASSERT(numnfsrvcache > 0);
331 		numnfsrvcache--;
332 		mutex_exit(&nfsrv_reqcache_lock);
333 		cleanentry(rp);
334 		cv_destroy(&rp->rc_cv);
335 		pool_put(&nfs_reqcache_pool, rp);
336 		rp = rpdup;
337 		goto found;
338 	}
339 	TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
340 	LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
341 	nfsrv_unlockcache(rp);
342 	mutex_exit(&nfsrv_reqcache_lock);
343 	return RC_DOIT;
344 }
345 
346 /*
347  * Update a request cache entry after the rpc has been done
348  */
349 void
350 nfsrv_updatecache(nd, repvalid, repmbuf)
351 	struct nfsrv_descript *nd;
352 	int repvalid;
353 	struct mbuf *repmbuf;
354 {
355 	struct nfsrvcache *rp;
356 
357 	mutex_enter(&nfsrv_reqcache_lock);
358 	rp = nfsrv_lookupcache(nd);
359 	mutex_exit(&nfsrv_reqcache_lock);
360 	if (rp) {
361 		cleanentry(rp);
362 		rp->rc_state = RC_DONE;
363 		/*
364 		 * If we have a valid reply update status and save
365 		 * the reply for non-idempotent rpc's.
366 		 */
367 		if (repvalid && nonidempotent[nd->nd_procnum]) {
368 			if ((nd->nd_flag & ND_NFSV3) == 0 &&
369 			  nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
370 				rp->rc_status = nd->nd_repstat;
371 				rp->rc_flags |= RC_REPSTATUS;
372 			} else {
373 				rp->rc_reply = m_copym(repmbuf,
374 					0, M_COPYALL, M_WAIT);
375 				m_claimm(rp->rc_reply, &nfsd_cache_mowner);
376 				rp->rc_flags |= RC_REPMBUF;
377 			}
378 		}
379 		mutex_enter(&nfsrv_reqcache_lock);
380 		nfsrv_unlockcache(rp);
381 		mutex_exit(&nfsrv_reqcache_lock);
382 	}
383 }
384 
385 /*
386  * Clean out the cache. Called when the last nfsd terminates.
387  */
388 void
389 nfsrv_cleancache()
390 {
391 	struct nfsrvcache *rp;
392 
393 	mutex_enter(&nfsrv_reqcache_lock);
394 	while ((rp = TAILQ_FIRST(&nfsrvlruhead)) != NULL) {
395 		KASSERT((rp->rc_gflags & RC_G_LOCKED) == 0);
396 		LIST_REMOVE(rp, rc_hash);
397 		TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
398 		KASSERT(numnfsrvcache > 0);
399 		numnfsrvcache--;
400 		mutex_exit(&nfsrv_reqcache_lock);
401 		cleanentry(rp);
402 		cv_destroy(&rp->rc_cv);
403 		pool_put(&nfs_reqcache_pool, rp);
404 		mutex_enter(&nfsrv_reqcache_lock);
405 	}
406 	KASSERT(numnfsrvcache == 0);
407 	mutex_exit(&nfsrv_reqcache_lock);
408 }
409