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