2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_srvcache.c 8.3 (Berkeley) 3/30/95
37 * $FreeBSD: src/sys/nfs/nfs_srvcache.c,v 1.21 2000/02/13 03:32:06 peter Exp $
41 * Reference: Chet Juszczak, "Improving the Performance and Correctness
42 * of an NFS Server", in Proc. Winter 1989 USENIX Conference,
43 * pages 53-63. San Diego, February 1989.
45 #include <sys/param.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/systm.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h> /* for dup_sockaddr */
53 #include <netinet/in.h>
54 #include <nfs/rpcv2.h>
55 #include <nfs/nfsproto.h>
57 #include <nfs/nfsrvcache.h>
60 extern struct nfsstats nfsstats;
61 extern int nfsv2_procid[NFS_NPROCS];
62 static long numnfsrvcache;
63 static long desirednfsrvcache = NFSRVCACHESIZ;
65 #define NFSRCHASH(xid) \
66 (&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
67 static LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
68 static TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
69 static u_long nfsrvhash;
74 #define NETFAMILY(rp) \
75 (((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO)
78 * Static array that defines which nfs rpc's are nonidempotent
80 static int nonidempotent[NFS_NPROCS] = {
109 /* True iff the rpc reply is an nfs status ONLY! */
110 static int nfsv2_repstat[NFS_NPROCS] = {
132 * Initialize the server request cache list
138 nfsrvhashtbl = hashinit(desirednfsrvcache, M_NFSD, &nfsrvhash);
139 TAILQ_INIT(&nfsrvlruhead);
143 * Look for the request in the cache
145 * return action and optionally reply
147 * insert it in the cache
149 * The rules are as follows:
150 * - if in progress, return DROP request
151 * - if completed within DELAY of the current time, return DROP it
152 * - if completed a longer time ago return REPLY if the reply was cached or
154 * Update/add new request at end of lru list
157 nfsrv_getcache(nd, slp, repp)
158 register struct nfsrv_descript *nd;
159 struct nfssvc_sock *slp;
162 register struct nfsrvcache *rp;
164 struct sockaddr_in *saddr;
169 * Don't cache recent requests for reliable transport protocols.
170 * (Maybe we should for the case of a reconnect, but..)
175 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
176 rp = rp->rc_hash.le_next) {
177 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
178 netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
179 NFS_DPF(RC, ("H%03x", rp->rc_xid & 0xfff));
180 if ((rp->rc_flag & RC_LOCKED) != 0) {
181 rp->rc_flag |= RC_WANTED;
182 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
185 rp->rc_flag |= RC_LOCKED;
186 /* If not at end of LRU chain, move it there */
187 if (rp->rc_lru.tqe_next) {
188 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
189 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
191 if (rp->rc_state == RC_UNUSED)
192 panic("nfsrv cache");
193 if (rp->rc_state == RC_INPROG) {
194 nfsstats.srvcache_inproghits++;
196 } else if (rp->rc_flag & RC_REPSTATUS) {
197 nfsstats.srvcache_nonidemdonehits++;
198 nfs_rephead(0, nd, slp, rp->rc_status,
199 0, (u_quad_t *)0, repp, &mb, &bpos);
201 } else if (rp->rc_flag & RC_REPMBUF) {
202 nfsstats.srvcache_nonidemdonehits++;
203 *repp = m_copym(rp->rc_reply, 0, M_COPYALL,
207 nfsstats.srvcache_idemdonehits++;
208 rp->rc_state = RC_INPROG;
211 rp->rc_flag &= ~RC_LOCKED;
212 if (rp->rc_flag & RC_WANTED) {
213 rp->rc_flag &= ~RC_WANTED;
219 nfsstats.srvcache_misses++;
220 NFS_DPF(RC, ("M%03x", nd->nd_retxid & 0xfff));
221 if (numnfsrvcache < desirednfsrvcache) {
222 rp = (struct nfsrvcache *)malloc((u_long)sizeof *rp,
224 bzero((char *)rp, sizeof *rp);
226 rp->rc_flag = RC_LOCKED;
228 rp = nfsrvlruhead.tqh_first;
229 while ((rp->rc_flag & RC_LOCKED) != 0) {
230 rp->rc_flag |= RC_WANTED;
231 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
232 rp = nfsrvlruhead.tqh_first;
234 rp->rc_flag |= RC_LOCKED;
235 LIST_REMOVE(rp, rc_hash);
236 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
237 if (rp->rc_flag & RC_REPMBUF)
238 m_freem(rp->rc_reply);
239 if (rp->rc_flag & RC_NAM)
240 FREE(rp->rc_nam, M_SONAME);
241 rp->rc_flag &= (RC_LOCKED | RC_WANTED);
243 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
244 rp->rc_state = RC_INPROG;
245 rp->rc_xid = nd->nd_retxid;
246 saddr = (struct sockaddr_in *)nd->nd_nam;
247 switch (saddr->sin_family) {
249 rp->rc_flag |= RC_INETADDR;
250 rp->rc_inetaddr = saddr->sin_addr.s_addr;
254 rp->rc_flag |= RC_NAM;
255 rp->rc_nam = dup_sockaddr(nd->nd_nam, 1);
258 rp->rc_proc = nd->nd_procnum;
259 LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
260 rp->rc_flag &= ~RC_LOCKED;
261 if (rp->rc_flag & RC_WANTED) {
262 rp->rc_flag &= ~RC_WANTED;
269 * Update a request cache entry after the rpc has been done
272 nfsrv_updatecache(nd, repvalid, repmbuf)
273 register struct nfsrv_descript *nd;
275 struct mbuf *repmbuf;
277 register struct nfsrvcache *rp;
282 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
283 rp = rp->rc_hash.le_next) {
284 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
285 netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
286 NFS_DPF(RC, ("U%03x", rp->rc_xid & 0xfff));
287 if ((rp->rc_flag & RC_LOCKED) != 0) {
288 rp->rc_flag |= RC_WANTED;
289 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
292 rp->rc_flag |= RC_LOCKED;
293 if (rp->rc_state == RC_DONE) {
295 * This can occur if the cache is too small.
296 * Retransmits of the same request aren't
297 * dropped so we may see the operation
298 * complete more then once.
300 if (rp->rc_flag & RC_REPMBUF) {
301 m_freem(rp->rc_reply);
302 rp->rc_flag &= ~RC_REPMBUF;
305 rp->rc_state = RC_DONE;
307 * If we have a valid reply update status and save
308 * the reply for non-idempotent rpc's.
310 if (repvalid && nonidempotent[nd->nd_procnum]) {
311 if ((nd->nd_flag & ND_NFSV3) == 0 &&
312 nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
313 rp->rc_status = nd->nd_repstat;
314 rp->rc_flag |= RC_REPSTATUS;
316 rp->rc_reply = m_copym(repmbuf,
317 0, M_COPYALL, M_WAIT);
318 rp->rc_flag |= RC_REPMBUF;
321 rp->rc_flag &= ~RC_LOCKED;
322 if (rp->rc_flag & RC_WANTED) {
323 rp->rc_flag &= ~RC_WANTED;
329 NFS_DPF(RC, ("L%03x", nd->nd_retxid & 0xfff));
333 * Clean out the cache. Called when the last nfsd terminates.
338 register struct nfsrvcache *rp, *nextrp;
340 for (rp = nfsrvlruhead.tqh_first; rp != 0; rp = nextrp) {
341 nextrp = rp->rc_lru.tqe_next;
342 LIST_REMOVE(rp, rc_hash);
343 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
344 if (rp->rc_flag & RC_REPMBUF)
345 m_freem(rp->rc_reply);
346 if (rp->rc_flag & RC_NAM)
347 free(rp->rc_nam, M_SONAME);
353 #endif /* NFS_NOSERVER */