Line data Source code
1 : /* $OpenBSD: nfs_srvcache.c,v 1.28 2015/08/28 00:03:54 deraadt 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/mbuf.h>
48 : #include <sys/malloc.h>
49 : #include <sys/socket.h>
50 : #include <sys/queue.h>
51 :
52 : #include <crypto/siphash.h>
53 :
54 : #include <netinet/in.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 : void nfsrv_cleanentry(struct nfsrvcache *);
66 :
67 : LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
68 : SIPHASH_KEY nfsrvhashkey;
69 : TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
70 : u_long nfsrvhash;
71 : #define NFSRCHASH(xid) \
72 : (&nfsrvhashtbl[SipHash24(&nfsrvhashkey, &(xid), sizeof(xid)) & nfsrvhash])
73 :
74 : #define NETFAMILY(rp) \
75 : (((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_UNSPEC)
76 :
77 : /* Array that defines which nfs rpc's are nonidempotent */
78 : int nonidempotent[NFS_NPROCS] = {
79 : 0, 0, 1, 0, 0, 0, 0, 1,
80 : 1, 1, 1, 1, 1, 1, 1, 1,
81 : 0, 0, 0, 0, 0, 0, 0
82 : };
83 :
84 : /* True iff the rpc reply is an nfs status ONLY! */
85 : int nfsv2_repstat[NFS_NPROCS] = {
86 : 0, 0, 0, 0, 0, 0, 0, 0,
87 : 0, 0, 1, 1, 1, 1, 0, 1,
88 : 0, 0
89 : };
90 :
91 : void
92 0 : nfsrv_cleanentry(struct nfsrvcache *rp)
93 : {
94 0 : if ((rp->rc_flag & RC_REPMBUF) != 0)
95 0 : m_freem(rp->rc_reply);
96 :
97 0 : if ((rp->rc_flag & RC_NAM) != 0)
98 0 : m_free(rp->rc_nam);
99 :
100 0 : rp->rc_flag &= ~(RC_REPSTATUS|RC_REPMBUF);
101 0 : }
102 :
103 : /* Initialize the server request cache list */
104 : void
105 0 : nfsrv_initcache(void)
106 : {
107 :
108 0 : nfsrvhashtbl = hashinit(desirednfsrvcache, M_NFSD, M_WAITOK, &nfsrvhash);
109 0 : arc4random_buf(&nfsrvhashkey, sizeof(nfsrvhashkey));
110 0 : TAILQ_INIT(&nfsrvlruhead);
111 0 : }
112 :
113 : /*
114 : * Look for the request in the cache
115 : * If found then
116 : * return action and optionally reply
117 : * else
118 : * insert it in the cache
119 : *
120 : * The rules are as follows:
121 : * - if in progress, return DROP request
122 : * - if completed within DELAY of the current time, return DROP it
123 : * - if completed a longer time ago return REPLY if the reply was cached or
124 : * return DOIT
125 : * Update/add new request at end of lru list
126 : */
127 : int
128 0 : nfsrv_getcache(struct nfsrv_descript *nd, struct nfssvc_sock *slp,
129 : struct mbuf **repp)
130 : {
131 : struct nfsrvhash *hash;
132 : struct nfsrvcache *rp;
133 0 : struct mbuf *mb;
134 : struct sockaddr_in *saddr;
135 : int ret;
136 :
137 : /*
138 : * Don't cache recent requests for reliable transport protocols.
139 : * (Maybe we should for the case of a reconnect, but..)
140 : */
141 0 : if (!nd->nd_nam2)
142 0 : return (RC_DOIT);
143 :
144 0 : rp = nfsrv_lookupcache(nd);
145 0 : if (rp) {
146 : /* If not at end of LRU chain, move it there */
147 0 : if (TAILQ_NEXT(rp, rc_lru)) {
148 0 : TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
149 0 : TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
150 0 : }
151 0 : if (rp->rc_state == RC_UNUSED)
152 0 : panic("nfsrv cache");
153 0 : if (rp->rc_state == RC_INPROG) {
154 0 : nfsstats.srvcache_inproghits++;
155 : ret = RC_DROPIT;
156 0 : } else if (rp->rc_flag & RC_REPSTATUS) {
157 0 : nfsstats.srvcache_nonidemdonehits++;
158 0 : nfs_rephead(0, nd, slp, rp->rc_status, repp, &mb);
159 : ret = RC_REPLY;
160 0 : } else if (rp->rc_flag & RC_REPMBUF) {
161 0 : nfsstats.srvcache_nonidemdonehits++;
162 0 : *repp = m_copym(rp->rc_reply, 0, M_COPYALL, M_WAIT);
163 : ret = RC_REPLY;
164 0 : } else {
165 0 : nfsstats.srvcache_idemdonehits++;
166 0 : rp->rc_state = RC_INPROG;
167 : ret = RC_DOIT;
168 : }
169 0 : rp->rc_flag &= ~RC_LOCKED;
170 0 : if (rp->rc_flag & RC_WANTED) {
171 0 : rp->rc_flag &= ~RC_WANTED;
172 0 : wakeup(rp);
173 0 : }
174 0 : return (ret);
175 : }
176 :
177 0 : nfsstats.srvcache_misses++;
178 0 : if (numnfsrvcache < desirednfsrvcache) {
179 0 : rp = malloc(sizeof(*rp), M_NFSD, M_WAITOK|M_ZERO);
180 0 : numnfsrvcache++;
181 0 : rp->rc_flag = RC_LOCKED;
182 0 : } else {
183 0 : rp = TAILQ_FIRST(&nfsrvlruhead);
184 0 : while ((rp->rc_flag & RC_LOCKED) != 0) {
185 0 : rp->rc_flag |= RC_WANTED;
186 0 : tsleep(rp, PZERO-1, "nfsrc", 0);
187 0 : rp = TAILQ_FIRST(&nfsrvlruhead);
188 : }
189 0 : rp->rc_flag |= RC_LOCKED;
190 0 : LIST_REMOVE(rp, rc_hash);
191 0 : TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
192 0 : nfsrv_cleanentry(rp);
193 0 : rp->rc_flag &= (RC_LOCKED | RC_WANTED);
194 : }
195 0 : TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
196 0 : rp->rc_state = RC_INPROG;
197 0 : rp->rc_xid = nd->nd_retxid;
198 0 : saddr = mtod(nd->nd_nam, struct sockaddr_in *);
199 0 : switch (saddr->sin_family) {
200 : case AF_INET:
201 0 : rp->rc_flag |= RC_INETADDR;
202 0 : rp->rc_inetaddr = saddr->sin_addr.s_addr;
203 0 : break;
204 : default:
205 0 : rp->rc_flag |= RC_NAM;
206 0 : rp->rc_nam = m_copym(nd->nd_nam, 0, M_COPYALL, M_WAIT);
207 0 : break;
208 : };
209 0 : rp->rc_proc = nd->nd_procnum;
210 0 : hash = NFSRCHASH(nd->nd_retxid);
211 0 : LIST_INSERT_HEAD(hash, rp, rc_hash);
212 0 : rp->rc_flag &= ~RC_LOCKED;
213 0 : if (rp->rc_flag & RC_WANTED) {
214 0 : rp->rc_flag &= ~RC_WANTED;
215 0 : wakeup(rp);
216 0 : }
217 0 : return (RC_DOIT);
218 0 : }
219 :
220 : /* Update a request cache entry after the rpc has been done */
221 : void
222 0 : nfsrv_updatecache(struct nfsrv_descript *nd, int repvalid,
223 : struct mbuf *repmbuf)
224 : {
225 : struct nfsrvcache *rp;
226 :
227 0 : if (!nd->nd_nam2)
228 0 : return;
229 :
230 0 : rp = nfsrv_lookupcache(nd);
231 0 : if (rp) {
232 0 : nfsrv_cleanentry(rp);
233 0 : rp->rc_state = RC_DONE;
234 : /*
235 : * If we have a valid reply update status and save
236 : * the reply for non-idempotent rpc's.
237 : */
238 0 : if (repvalid && nonidempotent[nd->nd_procnum]) {
239 0 : if ((nd->nd_flag & ND_NFSV3) == 0 &&
240 0 : nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
241 0 : rp->rc_status = nd->nd_repstat;
242 0 : rp->rc_flag |= RC_REPSTATUS;
243 0 : } else {
244 0 : rp->rc_reply = m_copym(repmbuf, 0, M_COPYALL,
245 : M_WAIT);
246 0 : rp->rc_flag |= RC_REPMBUF;
247 : }
248 : }
249 0 : rp->rc_flag &= ~RC_LOCKED;
250 0 : if (rp->rc_flag & RC_WANTED) {
251 0 : rp->rc_flag &= ~RC_WANTED;
252 0 : wakeup(rp);
253 0 : }
254 0 : return;
255 : }
256 0 : }
257 :
258 : /* Clean out the cache. Called when the last nfsd terminates. */
259 : void
260 0 : nfsrv_cleancache(void)
261 : {
262 : struct nfsrvcache *rp, *nextrp;
263 :
264 0 : for (rp = TAILQ_FIRST(&nfsrvlruhead); rp != NULL; rp = nextrp) {
265 0 : nextrp = TAILQ_NEXT(rp, rc_lru);
266 0 : LIST_REMOVE(rp, rc_hash);
267 0 : TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
268 0 : nfsrv_cleanentry(rp);
269 0 : free(rp, M_NFSD, sizeof(*rp));
270 : }
271 0 : numnfsrvcache = 0;
272 0 : }
273 :
274 : struct nfsrvcache *
275 0 : nfsrv_lookupcache(struct nfsrv_descript *nd)
276 : {
277 : struct nfsrvhash *hash;
278 : struct nfsrvcache *rp;
279 :
280 0 : hash = NFSRCHASH(nd->nd_retxid);
281 : loop:
282 0 : LIST_FOREACH(rp, hash, rc_hash) {
283 0 : if (nd->nd_retxid == rp->rc_xid &&
284 0 : nd->nd_procnum == rp->rc_proc &&
285 0 : netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
286 0 : if ((rp->rc_flag & RC_LOCKED)) {
287 0 : rp->rc_flag |= RC_WANTED;
288 0 : tsleep(rp, PZERO - 1, "nfsrc", 0);
289 0 : goto loop;
290 : }
291 0 : rp->rc_flag |= RC_LOCKED;
292 0 : return (rp);
293 : }
294 : }
295 :
296 0 : return (NULL);
297 0 : }
|