]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_srvcache.c
10989bef6f54f85671a9f8488821da60394e6aa5
[apple/xnu.git] / bsd / nfs / nfs_srvcache.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 /*
25 * Copyright (c) 1989, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * This code is derived from software contributed to Berkeley by
29 * Rick Macklem at The University of Guelph.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)nfs_srvcache.c 8.3 (Berkeley) 3/30/95
60 * FreeBSD-Id: nfs_srvcache.c,v 1.15 1997/10/12 20:25:46 phk Exp $
61 */
62
63 #ifndef NFS_NOSERVER
64 /*
65 * Reference: Chet Juszczak, "Improving the Performance and Correctness
66 * of an NFS Server", in Proc. Winter 1989 USENIX Conference,
67 * pages 53-63. San Diego, February 1989.
68 */
69 #include <sys/param.h>
70 #include <sys/vnode.h>
71 #include <sys/mount_internal.h>
72 #include <sys/kernel.h>
73 #include <sys/systm.h>
74 #include <sys/proc.h>
75 #include <sys/kpi_mbuf.h>
76 #include <sys/malloc.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h> /* for dup_sockaddr */
79 #include <libkern/OSAtomic.h>
80
81 #include <netinet/in.h>
82 #if ISO
83 #include <netiso/iso.h>
84 #endif
85 #include <nfs/rpcv2.h>
86 #include <nfs/nfsproto.h>
87 #include <nfs/nfs.h>
88 #include <nfs/nfsrvcache.h>
89
90 extern struct nfsstats nfsstats;
91 extern int nfsv2_procid[NFS_NPROCS];
92 long numnfsrvcache;
93 static long desirednfsrvcache = NFSRVCACHESIZ;
94
95 #define NFSRCHASH(xid) \
96 (&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
97 LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
98 TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
99 u_long nfsrvhash;
100
101 lck_grp_t *nfsrv_reqcache_lck_grp;
102 lck_grp_attr_t *nfsrv_reqcache_lck_grp_attr;
103 lck_attr_t *nfsrv_reqcache_lck_attr;
104 lck_mtx_t *nfsrv_reqcache_mutex;
105
106 #define NETFAMILY(rp) \
107 (((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO)
108
109 /*
110 * Static array that defines which nfs rpc's are nonidempotent
111 */
112 static int nonidempotent[NFS_NPROCS] = {
113 FALSE,
114 FALSE,
115 TRUE,
116 FALSE,
117 FALSE,
118 FALSE,
119 FALSE,
120 TRUE,
121 TRUE,
122 TRUE,
123 TRUE,
124 TRUE,
125 TRUE,
126 TRUE,
127 TRUE,
128 TRUE,
129 FALSE,
130 FALSE,
131 FALSE,
132 FALSE,
133 FALSE,
134 FALSE,
135 FALSE,
136 };
137
138 /* True iff the rpc reply is an nfs status ONLY! */
139 static int nfsv2_repstat[NFS_NPROCS] = {
140 FALSE,
141 FALSE,
142 FALSE,
143 FALSE,
144 FALSE,
145 FALSE,
146 FALSE,
147 FALSE,
148 FALSE,
149 FALSE,
150 TRUE,
151 TRUE,
152 TRUE,
153 TRUE,
154 FALSE,
155 TRUE,
156 FALSE,
157 FALSE,
158 };
159
160 /*
161 * Initialize the server request cache list
162 */
163 void
164 nfsrv_initcache()
165 {
166 /* init nfs server request cache mutex */
167 nfsrv_reqcache_lck_grp_attr = lck_grp_attr_alloc_init();
168 lck_grp_attr_setstat(nfsrv_reqcache_lck_grp_attr);
169 nfsrv_reqcache_lck_grp = lck_grp_alloc_init("nfsrv_reqcache", nfsrv_reqcache_lck_grp_attr);
170 nfsrv_reqcache_lck_attr = lck_attr_alloc_init();
171 nfsrv_reqcache_mutex = lck_mtx_alloc_init(nfsrv_reqcache_lck_grp, nfsrv_reqcache_lck_attr);
172
173 nfsrvhashtbl = hashinit(desirednfsrvcache, M_NFSD, &nfsrvhash);
174 TAILQ_INIT(&nfsrvlruhead);
175 }
176
177 /*
178 * Look for the request in the cache
179 * If found then
180 * return action and optionally reply
181 * else
182 * insert it in the cache
183 *
184 * The rules are as follows:
185 * - if in progress, return DROP request
186 * - if completed within DELAY of the current time, return DROP it
187 * - if completed a longer time ago return REPLY if the reply was cached or
188 * return DOIT
189 * Update/add new request at end of lru list
190 */
191 int
192 nfsrv_getcache(nd, slp, repp)
193 struct nfsrv_descript *nd;
194 struct nfssvc_sock *slp;
195 mbuf_t *repp;
196 {
197 struct nfsrvcache *rp;
198 mbuf_t mb;
199 struct sockaddr_in *saddr;
200 caddr_t bpos;
201 int ret, error;
202
203 /*
204 * Don't cache recent requests for reliable transport protocols.
205 * (Maybe we should for the case of a reconnect, but..)
206 */
207 if (!nd->nd_nam2)
208 return (RC_DOIT);
209 lck_mtx_lock(nfsrv_reqcache_mutex);
210 loop:
211 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
212 rp = rp->rc_hash.le_next) {
213 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
214 netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
215 if ((rp->rc_flag & RC_LOCKED) != 0) {
216 rp->rc_flag |= RC_WANTED;
217 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
218 goto loop;
219 }
220 rp->rc_flag |= RC_LOCKED;
221 /* If not at end of LRU chain, move it there */
222 if (rp->rc_lru.tqe_next) {
223 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
224 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
225 }
226 if (rp->rc_state == RC_UNUSED)
227 panic("nfsrv cache");
228 if (rp->rc_state == RC_INPROG) {
229 OSAddAtomic(1, (SInt32*)&nfsstats.srvcache_inproghits);
230 ret = RC_DROPIT;
231 } else if (rp->rc_flag & RC_REPSTATUS) {
232 OSAddAtomic(1, (SInt32*)&nfsstats.srvcache_nonidemdonehits);
233 nfs_rephead(0, nd, slp, rp->rc_status, repp, &mb, &bpos);
234 ret = RC_REPLY;
235 } else if (rp->rc_flag & RC_REPMBUF) {
236 OSAddAtomic(1, (SInt32*)&nfsstats.srvcache_nonidemdonehits);
237 error = mbuf_copym(rp->rc_reply, 0, MBUF_COPYALL, MBUF_WAITOK, repp);
238 if (error) {
239 printf("nfsrv cache: reply copym failed for nonidem request hit\n");
240 ret = RC_DROPIT;
241 } else {
242 ret = RC_REPLY;
243 }
244 } else {
245 OSAddAtomic(1, (SInt32*)&nfsstats.srvcache_idemdonehits);
246 rp->rc_state = RC_INPROG;
247 ret = RC_DOIT;
248 }
249 rp->rc_flag &= ~RC_LOCKED;
250 if (rp->rc_flag & RC_WANTED) {
251 rp->rc_flag &= ~RC_WANTED;
252 wakeup((caddr_t)rp);
253 }
254 lck_mtx_unlock(nfsrv_reqcache_mutex);
255 return (ret);
256 }
257 }
258 OSAddAtomic(1, (SInt32*)&nfsstats.srvcache_misses);
259 if (numnfsrvcache < desirednfsrvcache) {
260 /* try to allocate a new entry */
261 MALLOC(rp, struct nfsrvcache *, sizeof *rp, M_NFSD, M_WAITOK);
262 if (rp) {
263 bzero((char *)rp, sizeof *rp);
264 numnfsrvcache++;
265 rp->rc_flag = RC_LOCKED;
266 }
267 } else {
268 rp = NULL;
269 }
270 if (!rp) {
271 /* try to reuse the least recently used entry */
272 rp = nfsrvlruhead.tqh_first;
273 if (!rp) {
274 /* no entry to reuse? */
275 /* OK, we just won't be able to cache this request */
276 lck_mtx_unlock(nfsrv_reqcache_mutex);
277 return (RC_DOIT);
278 }
279 while ((rp->rc_flag & RC_LOCKED) != 0) {
280 rp->rc_flag |= RC_WANTED;
281 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
282 rp = nfsrvlruhead.tqh_first;
283 }
284 rp->rc_flag |= RC_LOCKED;
285 LIST_REMOVE(rp, rc_hash);
286 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
287 if (rp->rc_flag & RC_REPMBUF)
288 mbuf_freem(rp->rc_reply);
289 if (rp->rc_flag & RC_NAM)
290 mbuf_freem(rp->rc_nam);
291 rp->rc_flag &= (RC_LOCKED | RC_WANTED);
292 }
293 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
294 rp->rc_state = RC_INPROG;
295 rp->rc_xid = nd->nd_retxid;
296 saddr = mbuf_data(nd->nd_nam);
297 switch (saddr->sin_family) {
298 case AF_INET:
299 rp->rc_flag |= RC_INETADDR;
300 rp->rc_inetaddr = saddr->sin_addr.s_addr;
301 break;
302 case AF_ISO:
303 default:
304 error = mbuf_copym(nd->nd_nam, 0, MBUF_COPYALL, MBUF_WAITOK, &rp->rc_nam);
305 if (error)
306 printf("nfsrv cache: nam copym failed\n");
307 else
308 rp->rc_flag |= RC_NAM;
309 break;
310 };
311 rp->rc_proc = nd->nd_procnum;
312 LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
313 rp->rc_flag &= ~RC_LOCKED;
314 if (rp->rc_flag & RC_WANTED) {
315 rp->rc_flag &= ~RC_WANTED;
316 wakeup((caddr_t)rp);
317 }
318 lck_mtx_unlock(nfsrv_reqcache_mutex);
319 return (RC_DOIT);
320 }
321
322 /*
323 * Update a request cache entry after the rpc has been done
324 */
325 void
326 nfsrv_updatecache(nd, repvalid, repmbuf)
327 struct nfsrv_descript *nd;
328 int repvalid;
329 mbuf_t repmbuf;
330 {
331 struct nfsrvcache *rp;
332 int error;
333
334 if (!nd->nd_nam2)
335 return;
336 lck_mtx_lock(nfsrv_reqcache_mutex);
337 loop:
338 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
339 rp = rp->rc_hash.le_next) {
340 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
341 netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
342 if ((rp->rc_flag & RC_LOCKED) != 0) {
343 rp->rc_flag |= RC_WANTED;
344 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
345 goto loop;
346 }
347 rp->rc_flag |= RC_LOCKED;
348 rp->rc_state = RC_DONE;
349 /*
350 * If we have a valid reply update status and save
351 * the reply for non-idempotent rpc's.
352 */
353 if (repvalid && nonidempotent[nd->nd_procnum]) {
354 if ((nd->nd_flag & ND_NFSV3) == 0 &&
355 nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
356 rp->rc_status = nd->nd_repstat;
357 rp->rc_flag |= RC_REPSTATUS;
358 } else {
359 error = mbuf_copym(repmbuf, 0, MBUF_COPYALL, MBUF_WAITOK, &rp->rc_reply);
360 if (!error)
361 rp->rc_flag |= RC_REPMBUF;
362 }
363 }
364 rp->rc_flag &= ~RC_LOCKED;
365 if (rp->rc_flag & RC_WANTED) {
366 rp->rc_flag &= ~RC_WANTED;
367 wakeup((caddr_t)rp);
368 }
369 lck_mtx_unlock(nfsrv_reqcache_mutex);
370 return;
371 }
372 }
373 lck_mtx_unlock(nfsrv_reqcache_mutex);
374 }
375
376 /*
377 * Clean out the cache. Called when the last nfsd terminates.
378 */
379 void
380 nfsrv_cleancache()
381 {
382 struct nfsrvcache *rp, *nextrp;
383
384 lck_mtx_lock(nfsrv_reqcache_mutex);
385 for (rp = nfsrvlruhead.tqh_first; rp != 0; rp = nextrp) {
386 nextrp = rp->rc_lru.tqe_next;
387 LIST_REMOVE(rp, rc_hash);
388 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
389 _FREE(rp, M_NFSD);
390 }
391 numnfsrvcache = 0;
392 lck_mtx_unlock(nfsrv_reqcache_mutex);
393 }
394
395 #endif /* NFS_NOSERVER */