]>
git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_srvcache.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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.
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
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 $
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.
68 #include <sys/param.h>
69 #include <sys/vnode.h>
70 #include <sys/mount.h>
71 #include <sys/kernel.h>
72 #include <sys/systm.h>
75 #include <sys/malloc.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h> /* for dup_sockaddr */
79 #include <netinet/in.h>
81 #include <netiso/iso.h>
83 #include <nfs/rpcv2.h>
84 #include <nfs/nfsproto.h>
86 #include <nfs/nfsrvcache.h>
88 extern struct nfsstats nfsstats
;
89 extern int nfsv2_procid
[NFS_NPROCS
];
91 static long desirednfsrvcache
= NFSRVCACHESIZ
;
93 #define NFSRCHASH(xid) \
94 (&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
95 LIST_HEAD(nfsrvhash
, nfsrvcache
) *nfsrvhashtbl
;
96 TAILQ_HEAD(nfsrvlru
, nfsrvcache
) nfsrvlruhead
;
102 #define NETFAMILY(rp) \
103 (((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO)
106 * Static array that defines which nfs rpc's are nonidempotent
108 static int nonidempotent
[NFS_NPROCS
] = {
137 /* True iff the rpc reply is an nfs status ONLY! */
138 static int nfsv2_repstat
[NFS_NPROCS
] = {
160 * Initialize the server request cache list
166 nfsrvhashtbl
= hashinit(desirednfsrvcache
, M_NFSD
, &nfsrvhash
);
167 TAILQ_INIT(&nfsrvlruhead
);
171 * Look for the request in the cache
173 * return action and optionally reply
175 * insert it in the cache
177 * The rules are as follows:
178 * - if in progress, return DROP request
179 * - if completed within DELAY of the current time, return DROP it
180 * - if completed a longer time ago return REPLY if the reply was cached or
182 * Update/add new request at end of lru list
185 nfsrv_getcache(nd
, slp
, repp
)
186 register struct nfsrv_descript
*nd
;
187 struct nfssvc_sock
*slp
;
190 register struct nfsrvcache
*rp
;
192 struct sockaddr_in
*saddr
;
197 * Don't cache recent requests for reliable transport protocols.
198 * (Maybe we should for the case of a reconnect, but..)
203 for (rp
= NFSRCHASH(nd
->nd_retxid
)->lh_first
; rp
!= 0;
204 rp
= rp
->rc_hash
.le_next
) {
205 if (nd
->nd_retxid
== rp
->rc_xid
&& nd
->nd_procnum
== rp
->rc_proc
&&
206 netaddr_match(NETFAMILY(rp
), &rp
->rc_haddr
, nd
->nd_nam
)) {
207 NFS_DPF(RC
, ("H%03x", rp
->rc_xid
& 0xfff));
208 if ((rp
->rc_flag
& RC_LOCKED
) != 0) {
209 rp
->rc_flag
|= RC_WANTED
;
210 (void) tsleep((caddr_t
)rp
, PZERO
-1, "nfsrc", 0);
213 rp
->rc_flag
|= RC_LOCKED
;
214 /* If not at end of LRU chain, move it there */
215 if (rp
->rc_lru
.tqe_next
) {
216 TAILQ_REMOVE(&nfsrvlruhead
, rp
, rc_lru
);
217 TAILQ_INSERT_TAIL(&nfsrvlruhead
, rp
, rc_lru
);
219 if (rp
->rc_state
== RC_UNUSED
)
220 panic("nfsrv cache");
221 if (rp
->rc_state
== RC_INPROG
) {
222 nfsstats
.srvcache_inproghits
++;
224 } else if (rp
->rc_flag
& RC_REPSTATUS
) {
225 nfsstats
.srvcache_nonidemdonehits
++;
226 nfs_rephead(0, nd
, slp
, rp
->rc_status
,
227 0, (u_quad_t
*)0, repp
, &mb
, &bpos
);
229 } else if (rp
->rc_flag
& RC_REPMBUF
) {
230 nfsstats
.srvcache_nonidemdonehits
++;
231 *repp
= m_copym(rp
->rc_reply
, 0, M_COPYALL
,
235 nfsstats
.srvcache_idemdonehits
++;
236 rp
->rc_state
= RC_INPROG
;
239 rp
->rc_flag
&= ~RC_LOCKED
;
240 if (rp
->rc_flag
& RC_WANTED
) {
241 rp
->rc_flag
&= ~RC_WANTED
;
247 nfsstats
.srvcache_misses
++;
248 NFS_DPF(RC
, ("M%03x", nd
->nd_retxid
& 0xfff));
249 if (numnfsrvcache
< desirednfsrvcache
) {
250 MALLOC(rp
, struct nfsrvcache
*, sizeof *rp
, M_NFSD
, M_WAITOK
);
251 bzero((char *)rp
, sizeof *rp
);
253 rp
->rc_flag
= RC_LOCKED
;
255 rp
= nfsrvlruhead
.tqh_first
;
256 while ((rp
->rc_flag
& RC_LOCKED
) != 0) {
257 rp
->rc_flag
|= RC_WANTED
;
258 (void) tsleep((caddr_t
)rp
, PZERO
-1, "nfsrc", 0);
259 rp
= nfsrvlruhead
.tqh_first
;
261 rp
->rc_flag
|= RC_LOCKED
;
262 LIST_REMOVE(rp
, rc_hash
);
263 TAILQ_REMOVE(&nfsrvlruhead
, rp
, rc_lru
);
264 if (rp
->rc_flag
& RC_REPMBUF
)
265 m_freem(rp
->rc_reply
);
266 if (rp
->rc_flag
& RC_NAM
)
267 MFREE(rp
->rc_nam
, mb
);
268 rp
->rc_flag
&= (RC_LOCKED
| RC_WANTED
);
270 TAILQ_INSERT_TAIL(&nfsrvlruhead
, rp
, rc_lru
);
271 rp
->rc_state
= RC_INPROG
;
272 rp
->rc_xid
= nd
->nd_retxid
;
273 saddr
= mtod(nd
->nd_nam
, struct sockaddr_in
*);
274 switch (saddr
->sin_family
) {
276 rp
->rc_flag
|= RC_INETADDR
;
277 rp
->rc_inetaddr
= saddr
->sin_addr
.s_addr
;
281 rp
->rc_flag
|= RC_NAM
;
282 rp
->rc_nam
= m_copym(nd
->nd_nam
, 0, M_COPYALL
, M_WAIT
);
285 rp
->rc_proc
= nd
->nd_procnum
;
286 LIST_INSERT_HEAD(NFSRCHASH(nd
->nd_retxid
), rp
, rc_hash
);
287 rp
->rc_flag
&= ~RC_LOCKED
;
288 if (rp
->rc_flag
& RC_WANTED
) {
289 rp
->rc_flag
&= ~RC_WANTED
;
296 * Update a request cache entry after the rpc has been done
299 nfsrv_updatecache(nd
, repvalid
, repmbuf
)
300 register struct nfsrv_descript
*nd
;
302 struct mbuf
*repmbuf
;
304 register struct nfsrvcache
*rp
;
309 for (rp
= NFSRCHASH(nd
->nd_retxid
)->lh_first
; rp
!= 0;
310 rp
= rp
->rc_hash
.le_next
) {
311 if (nd
->nd_retxid
== rp
->rc_xid
&& nd
->nd_procnum
== rp
->rc_proc
&&
312 netaddr_match(NETFAMILY(rp
), &rp
->rc_haddr
, nd
->nd_nam
)) {
313 NFS_DPF(RC
, ("U%03x", rp
->rc_xid
& 0xfff));
314 if ((rp
->rc_flag
& RC_LOCKED
) != 0) {
315 rp
->rc_flag
|= RC_WANTED
;
316 (void) tsleep((caddr_t
)rp
, PZERO
-1, "nfsrc", 0);
319 rp
->rc_flag
|= RC_LOCKED
;
320 rp
->rc_state
= RC_DONE
;
322 * If we have a valid reply update status and save
323 * the reply for non-idempotent rpc's.
325 if (repvalid
&& nonidempotent
[nd
->nd_procnum
]) {
326 if ((nd
->nd_flag
& ND_NFSV3
) == 0 &&
327 nfsv2_repstat
[nfsv2_procid
[nd
->nd_procnum
]]) {
328 rp
->rc_status
= nd
->nd_repstat
;
329 rp
->rc_flag
|= RC_REPSTATUS
;
331 rp
->rc_reply
= m_copym(repmbuf
,
332 0, M_COPYALL
, M_WAIT
);
333 rp
->rc_flag
|= RC_REPMBUF
;
336 rp
->rc_flag
&= ~RC_LOCKED
;
337 if (rp
->rc_flag
& RC_WANTED
) {
338 rp
->rc_flag
&= ~RC_WANTED
;
344 NFS_DPF(RC
, ("L%03x", nd
->nd_retxid
& 0xfff));
348 * Clean out the cache. Called when the last nfsd terminates.
353 register struct nfsrvcache
*rp
, *nextrp
;
355 for (rp
= nfsrvlruhead
.tqh_first
; rp
!= 0; rp
= nextrp
) {
356 nextrp
= rp
->rc_lru
.tqe_next
;
357 LIST_REMOVE(rp
, rc_hash
);
358 TAILQ_REMOVE(&nfsrvlruhead
, rp
, rc_lru
);
364 #endif /* NFS_NOSERVER */