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 $
38 * $DragonFly: src/sys/vfs/nfs/nfs_srvcache.c,v 1.2 2003/06/17 04:28:54 dillon Exp $
42 * Reference: Chet Juszczak, "Improving the Performance and Correctness
43 * of an NFS Server", in Proc. Winter 1989 USENIX Conference,
44 * pages 53-63. San Diego, February 1989.
46 #include <sys/param.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/systm.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h> /* for dup_sockaddr */
54 #include <netinet/in.h>
55 #include <nfs/rpcv2.h>
56 #include <nfs/nfsproto.h>
58 #include <nfs/nfsrvcache.h>
61 extern struct nfsstats nfsstats;
62 extern int nfsv2_procid[NFS_NPROCS];
63 static long numnfsrvcache;
64 static long desirednfsrvcache = NFSRVCACHESIZ;
66 #define NFSRCHASH(xid) \
67 (&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
68 static LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
69 static TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
70 static u_long nfsrvhash;
75 #define NETFAMILY(rp) \
76 (((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_ISO)
79 * Static array that defines which nfs rpc's are nonidempotent
81 static int nonidempotent[NFS_NPROCS] = {
110 /* True iff the rpc reply is an nfs status ONLY! */
111 static int nfsv2_repstat[NFS_NPROCS] = {
133 * Initialize the server request cache list
139 nfsrvhashtbl = hashinit(desirednfsrvcache, M_NFSD, &nfsrvhash);
140 TAILQ_INIT(&nfsrvlruhead);
144 * Look for the request in the cache
146 * return action and optionally reply
148 * insert it in the cache
150 * The rules are as follows:
151 * - if in progress, return DROP request
152 * - if completed within DELAY of the current time, return DROP it
153 * - if completed a longer time ago return REPLY if the reply was cached or
155 * Update/add new request at end of lru list
158 nfsrv_getcache(nd, slp, repp)
159 register struct nfsrv_descript *nd;
160 struct nfssvc_sock *slp;
163 register struct nfsrvcache *rp;
165 struct sockaddr_in *saddr;
170 * Don't cache recent requests for reliable transport protocols.
171 * (Maybe we should for the case of a reconnect, but..)
176 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
177 rp = rp->rc_hash.le_next) {
178 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
179 netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
180 NFS_DPF(RC, ("H%03x", rp->rc_xid & 0xfff));
181 if ((rp->rc_flag & RC_LOCKED) != 0) {
182 rp->rc_flag |= RC_WANTED;
183 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
186 rp->rc_flag |= RC_LOCKED;
187 /* If not at end of LRU chain, move it there */
188 if (rp->rc_lru.tqe_next) {
189 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
190 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
192 if (rp->rc_state == RC_UNUSED)
193 panic("nfsrv cache");
194 if (rp->rc_state == RC_INPROG) {
195 nfsstats.srvcache_inproghits++;
197 } else if (rp->rc_flag & RC_REPSTATUS) {
198 nfsstats.srvcache_nonidemdonehits++;
199 nfs_rephead(0, nd, slp, rp->rc_status,
200 0, (u_quad_t *)0, repp, &mb, &bpos);
202 } else if (rp->rc_flag & RC_REPMBUF) {
203 nfsstats.srvcache_nonidemdonehits++;
204 *repp = m_copym(rp->rc_reply, 0, M_COPYALL,
208 nfsstats.srvcache_idemdonehits++;
209 rp->rc_state = RC_INPROG;
212 rp->rc_flag &= ~RC_LOCKED;
213 if (rp->rc_flag & RC_WANTED) {
214 rp->rc_flag &= ~RC_WANTED;
220 nfsstats.srvcache_misses++;
221 NFS_DPF(RC, ("M%03x", nd->nd_retxid & 0xfff));
222 if (numnfsrvcache < desirednfsrvcache) {
223 rp = (struct nfsrvcache *)malloc((u_long)sizeof *rp,
225 bzero((char *)rp, sizeof *rp);
227 rp->rc_flag = RC_LOCKED;
229 rp = nfsrvlruhead.tqh_first;
230 while ((rp->rc_flag & RC_LOCKED) != 0) {
231 rp->rc_flag |= RC_WANTED;
232 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
233 rp = nfsrvlruhead.tqh_first;
235 rp->rc_flag |= RC_LOCKED;
236 LIST_REMOVE(rp, rc_hash);
237 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
238 if (rp->rc_flag & RC_REPMBUF)
239 m_freem(rp->rc_reply);
240 if (rp->rc_flag & RC_NAM)
241 FREE(rp->rc_nam, M_SONAME);
242 rp->rc_flag &= (RC_LOCKED | RC_WANTED);
244 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
245 rp->rc_state = RC_INPROG;
246 rp->rc_xid = nd->nd_retxid;
247 saddr = (struct sockaddr_in *)nd->nd_nam;
248 switch (saddr->sin_family) {
250 rp->rc_flag |= RC_INETADDR;
251 rp->rc_inetaddr = saddr->sin_addr.s_addr;
255 rp->rc_flag |= RC_NAM;
256 rp->rc_nam = dup_sockaddr(nd->nd_nam, 1);
259 rp->rc_proc = nd->nd_procnum;
260 LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
261 rp->rc_flag &= ~RC_LOCKED;
262 if (rp->rc_flag & RC_WANTED) {
263 rp->rc_flag &= ~RC_WANTED;
270 * Update a request cache entry after the rpc has been done
273 nfsrv_updatecache(nd, repvalid, repmbuf)
274 register struct nfsrv_descript *nd;
276 struct mbuf *repmbuf;
278 register struct nfsrvcache *rp;
283 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != 0;
284 rp = rp->rc_hash.le_next) {
285 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
286 netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
287 NFS_DPF(RC, ("U%03x", rp->rc_xid & 0xfff));
288 if ((rp->rc_flag & RC_LOCKED) != 0) {
289 rp->rc_flag |= RC_WANTED;
290 (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
293 rp->rc_flag |= RC_LOCKED;
294 if (rp->rc_state == RC_DONE) {
296 * This can occur if the cache is too small.
297 * Retransmits of the same request aren't
298 * dropped so we may see the operation
299 * complete more then once.
301 if (rp->rc_flag & RC_REPMBUF) {
302 m_freem(rp->rc_reply);
303 rp->rc_flag &= ~RC_REPMBUF;
306 rp->rc_state = RC_DONE;
308 * If we have a valid reply update status and save
309 * the reply for non-idempotent rpc's.
311 if (repvalid && nonidempotent[nd->nd_procnum]) {
312 if ((nd->nd_flag & ND_NFSV3) == 0 &&
313 nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
314 rp->rc_status = nd->nd_repstat;
315 rp->rc_flag |= RC_REPSTATUS;
317 rp->rc_reply = m_copym(repmbuf,
318 0, M_COPYALL, M_WAIT);
319 rp->rc_flag |= RC_REPMBUF;
322 rp->rc_flag &= ~RC_LOCKED;
323 if (rp->rc_flag & RC_WANTED) {
324 rp->rc_flag &= ~RC_WANTED;
330 NFS_DPF(RC, ("L%03x", nd->nd_retxid & 0xfff));
334 * Clean out the cache. Called when the last nfsd terminates.
339 register struct nfsrvcache *rp, *nextrp;
341 for (rp = nfsrvlruhead.tqh_first; rp != 0; rp = nextrp) {
342 nextrp = rp->rc_lru.tqe_next;
343 LIST_REMOVE(rp, rc_hash);
344 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
345 if (rp->rc_flag & RC_REPMBUF)
346 m_freem(rp->rc_reply);
347 if (rp->rc_flag & RC_NAM)
348 free(rp->rc_nam, M_SONAME);
354 #endif /* NFS_NOSERVER */