xref: /dflybsd-src/sys/vfs/nfs/nfs_srvcache.c (revision 4041d91945fc1e4ba1bb1065fd5f9915983f8605)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)nfs_srvcache.c	8.3 (Berkeley) 3/30/95
37  * $FreeBSD: src/sys/nfs/nfs_srvcache.c,v 1.21 2000/02/13 03:32:06 peter Exp $
38  * $DragonFly: src/sys/vfs/nfs/nfs_srvcache.c,v 1.13 2008/01/05 14:02:41 swildner Exp $
39  */
40 
41 /*
42  * Reference: Chet Juszczak, "Improving the Performance and Correctness
43  *		of an NFS Server", in Proc. Winter 1989 USENIX Conference,
44  *		pages 53-63. San Diego, February 1989.
45  */
46 #include <sys/param.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/systm.h>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>	/* for dup_sockaddr */
53 
54 #include <netinet/in.h>
55 #include "rpcv2.h"
56 #include "nfsproto.h"
57 #include "nfs.h"
58 #include "nfsrvcache.h"
59 
60 #ifndef NFS_NOSERVER
61 static long numnfsrvcache;
62 static long desirednfsrvcache = NFSRVCACHESIZ;
63 
64 #define	NFSRCHASH(xid) \
65 	(&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
66 static LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
67 static TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
68 static u_long nfsrvhash;
69 
70 #define TRUE	1
71 #define	FALSE	0
72 
73 #define	NETFAMILY(rp) \
74 		(((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO)
75 
76 /*
77  * Static array that defines which nfs rpc's are nonidempotent
78  */
79 static int nonidempotent[NFS_NPROCS] = {
80 	FALSE,
81 	FALSE,
82 	TRUE,
83 	FALSE,
84 	FALSE,
85 	FALSE,
86 	FALSE,
87 	TRUE,
88 	TRUE,
89 	TRUE,
90 	TRUE,
91 	TRUE,
92 	TRUE,
93 	TRUE,
94 	TRUE,
95 	TRUE,
96 	FALSE,
97 	FALSE,
98 	FALSE,
99 	FALSE,
100 	FALSE,
101 	FALSE,
102 	FALSE,
103 	FALSE,
104 	FALSE,
105 	FALSE,
106 };
107 
108 /* True iff the rpc reply is an nfs status ONLY! */
109 static int nfsv2_repstat[NFS_NPROCS] = {
110 	FALSE,
111 	FALSE,
112 	FALSE,
113 	FALSE,
114 	FALSE,
115 	FALSE,
116 	FALSE,
117 	FALSE,
118 	FALSE,
119 	FALSE,
120 	TRUE,
121 	TRUE,
122 	TRUE,
123 	TRUE,
124 	FALSE,
125 	TRUE,
126 	FALSE,
127 	FALSE,
128 };
129 
130 /*
131  * Initialize the server request cache list
132  */
133 void
134 nfsrv_initcache(void)
135 {
136 
137 	nfsrvhashtbl = hashinit(desirednfsrvcache, M_NFSD, &nfsrvhash);
138 	TAILQ_INIT(&nfsrvlruhead);
139 }
140 
141 /*
142  * Look for the request in the cache
143  * If found then
144  *    return action and optionally reply
145  * else
146  *    insert it in the cache
147  *
148  * The rules are as follows:
149  * - if in progress, return DROP request
150  * - if completed within DELAY of the current time, return DROP it
151  * - if completed a longer time ago return REPLY if the reply was cached or
152  *   return DOIT
153  * Update/add new request at end of lru list
154  */
155 int
156 nfsrv_getcache(struct nfsrv_descript *nd, struct nfssvc_sock *slp,
157 	       struct mbuf **repp)
158 {
159 	struct nfsrvcache *rp;
160 	struct mbuf *mb;
161 	struct sockaddr_in *saddr;
162 	caddr_t bpos;
163 	int ret;
164 
165 	/*
166 	 * Don't cache recent requests for reliable transport protocols.
167 	 * (Maybe we should for the case of a reconnect, but..)
168 	 */
169 	if (!nd->nd_nam2)
170 		return (RC_DOIT);
171 loop:
172 	for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
173 	    rp = rp->rc_hash.le_next) {
174 	    if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
175 		netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
176 		        NFS_DPF(RC, ("H%03x", rp->rc_xid & 0xfff));
177 			if ((rp->rc_flag & RC_LOCKED) != 0) {
178 				rp->rc_flag |= RC_WANTED;
179 				tsleep((caddr_t)rp, 0, "nfsrc", 0);
180 				goto loop;
181 			}
182 			rp->rc_flag |= RC_LOCKED;
183 			/* If not at end of LRU chain, move it there */
184 			if (TAILQ_NEXT(rp, rc_lru) != NULL) {
185 				TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
186 				TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
187 			}
188 			if (rp->rc_state == RC_UNUSED)
189 				panic("nfsrv cache");
190 			if (rp->rc_state == RC_INPROG) {
191 				nfsstats.srvcache_inproghits++;
192 				ret = RC_DROPIT;
193 			} else if (rp->rc_flag & RC_REPSTATUS) {
194 				nfsstats.srvcache_nonidemdonehits++;
195 				nfs_rephead(0, nd, slp, rp->rc_status,
196 					    repp, &mb, &bpos);
197 				ret = RC_REPLY;
198 			} else if (rp->rc_flag & RC_REPMBUF) {
199 				nfsstats.srvcache_nonidemdonehits++;
200 				*repp = m_copym(rp->rc_reply, 0, M_COPYALL,
201 						MB_WAIT);
202 				ret = RC_REPLY;
203 			} else {
204 				nfsstats.srvcache_idemdonehits++;
205 				rp->rc_state = RC_INPROG;
206 				ret = RC_DOIT;
207 			}
208 			rp->rc_flag &= ~RC_LOCKED;
209 			if (rp->rc_flag & RC_WANTED) {
210 				rp->rc_flag &= ~RC_WANTED;
211 				wakeup((caddr_t)rp);
212 			}
213 			return (ret);
214 		}
215 	}
216 	nfsstats.srvcache_misses++;
217 	NFS_DPF(RC, ("M%03x", nd->nd_retxid & 0xfff));
218 	if (numnfsrvcache < desirednfsrvcache) {
219 		rp = kmalloc((u_long)sizeof *rp, M_NFSD, M_WAITOK | M_ZERO);
220 		numnfsrvcache++;
221 		rp->rc_flag = RC_LOCKED;
222 	} else {
223 		rp = TAILQ_FIRST(&nfsrvlruhead);
224 		while ((rp->rc_flag & RC_LOCKED) != 0) {
225 			rp->rc_flag |= RC_WANTED;
226 			tsleep((caddr_t)rp, 0, "nfsrc", 0);
227 			rp = TAILQ_FIRST(&nfsrvlruhead);
228 		}
229 		rp->rc_flag |= RC_LOCKED;
230 		LIST_REMOVE(rp, rc_hash);
231 		TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
232 		if (rp->rc_flag & RC_REPMBUF) {
233 			m_freem(rp->rc_reply);
234 			rp->rc_reply = NULL;
235 			rp->rc_flag &= ~RC_REPMBUF;
236 		}
237 		if (rp->rc_flag & RC_NAM) {
238 			FREE(rp->rc_nam, M_SONAME);
239 			rp->rc_nam = NULL;
240 			rp->rc_flag &= ~RC_NAM;
241 		}
242 		rp->rc_flag &= (RC_LOCKED | RC_WANTED);
243 	}
244 	TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
245 	rp->rc_state = RC_INPROG;
246 	rp->rc_xid = nd->nd_retxid;
247 	saddr = (struct sockaddr_in *)nd->nd_nam;
248 	switch (saddr->sin_family) {
249 	case AF_INET:
250 		rp->rc_flag |= RC_INETADDR;
251 		rp->rc_inetaddr = saddr->sin_addr.s_addr;
252 		break;
253 	case AF_ISO:
254 	default:
255 		rp->rc_flag |= RC_NAM;
256 		rp->rc_nam = dup_sockaddr(nd->nd_nam);
257 		break;
258 	};
259 	rp->rc_proc = nd->nd_procnum;
260 	LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
261 	rp->rc_flag &= ~RC_LOCKED;
262 	if (rp->rc_flag & RC_WANTED) {
263 		rp->rc_flag &= ~RC_WANTED;
264 		wakeup((caddr_t)rp);
265 	}
266 	return (RC_DOIT);
267 }
268 
269 /*
270  * Update a request cache entry after the rpc has been done
271  */
272 void
273 nfsrv_updatecache(struct nfsrv_descript *nd, int repvalid, struct mbuf *repmbuf)
274 {
275 	struct nfsrvcache *rp;
276 
277 	if (!nd->nd_nam2)
278 		return;
279 loop:
280 	for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
281 	    rp = rp->rc_hash.le_next) {
282 	    if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
283 		netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
284 			NFS_DPF(RC, ("U%03x", rp->rc_xid & 0xfff));
285 			if ((rp->rc_flag & RC_LOCKED) != 0) {
286 				rp->rc_flag |= RC_WANTED;
287 				tsleep((caddr_t)rp, 0, "nfsrc", 0);
288 				goto loop;
289 			}
290 			rp->rc_flag |= RC_LOCKED;
291 			if (rp->rc_state == RC_DONE) {
292 				/*
293 				 * This can occur if the cache is too small.
294 				 * Retransmits of the same request aren't
295 				 * dropped so we may see the operation
296 				 * complete more then once.
297 				 */
298 				if (rp->rc_flag & RC_REPMBUF) {
299 					m_freem(rp->rc_reply);
300 					rp->rc_reply = NULL;
301 					rp->rc_flag &= ~RC_REPMBUF;
302 				}
303 			}
304 			rp->rc_state = RC_DONE;
305 
306 			/*
307 			 * If we have a valid reply update status and save
308 			 * the reply for non-idempotent rpc's.
309 			 */
310 			if (repvalid && nonidempotent[nd->nd_procnum]) {
311 				if ((nd->nd_flag & ND_NFSV3) == 0 &&
312 				  nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
313 					rp->rc_status = nd->nd_repstat;
314 					rp->rc_flag |= RC_REPSTATUS;
315 				} else {
316 					if (rp->rc_flag & RC_REPMBUF) {
317 						m_freem(rp->rc_reply);
318 						rp->rc_reply = NULL;
319 						rp->rc_flag &= ~RC_REPMBUF;
320 					}
321 					rp->rc_reply = m_copym(repmbuf, 0,
322 							M_COPYALL, MB_WAIT);
323 					rp->rc_flag |= RC_REPMBUF;
324 				}
325 			}
326 			rp->rc_flag &= ~RC_LOCKED;
327 			if (rp->rc_flag & RC_WANTED) {
328 				rp->rc_flag &= ~RC_WANTED;
329 				wakeup((caddr_t)rp);
330 			}
331 			return;
332 		}
333 	}
334 	NFS_DPF(RC, ("L%03x", nd->nd_retxid & 0xfff));
335 }
336 
337 /*
338  * Clean out the cache. Called when the last nfsd terminates.
339  */
340 void
341 nfsrv_cleancache(void)
342 {
343 	struct nfsrvcache *rp;
344 
345 	while ((rp = TAILQ_FIRST(&nfsrvlruhead)) != NULL) {
346 		if (rp->rc_flag & RC_LOCKED) {
347 			rp->rc_flag |= RC_WANTED;
348 			tsleep((caddr_t)rp, 0, "nfsrc", 0);
349 			continue;
350 		}
351 		LIST_REMOVE(rp, rc_hash);
352 		TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
353 		if (rp->rc_flag & RC_REPMBUF) {
354 			m_freem(rp->rc_reply);
355 			rp->rc_reply = NULL;
356 			rp->rc_flag &= ~RC_REPMBUF;
357 		}
358 		if (rp->rc_flag & RC_NAM) {
359 			kfree(rp->rc_nam, M_SONAME);
360 			rp->rc_nam = NULL;
361 			rp->rc_flag &= ~RC_NAM;
362 		}
363 		kfree(rp, M_NFSD);
364 	}
365 	numnfsrvcache = 0;
366 }
367 
368 #endif /* NFS_NOSERVER */
369