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