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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_srvcache.c 8.3 (Berkeley) 3/30/95
33 * $FreeBSD: src/sys/nfs/nfs_srvcache.c,v 1.21 2000/02/13 03:32:06 peter Exp $
37 * Reference: Chet Juszczak, "Improving the Performance and Correctness
38 * of an NFS Server", in Proc. Winter 1989 USENIX Conference,
39 * pages 53-63. San Diego, February 1989.
41 #include <sys/param.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/systm.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h> /* for dup_sockaddr */
49 #include <netinet/in.h>
53 #include "nfsrvcache.h"
56 static long numnfsrvcache;
57 static long desirednfsrvcache;
59 #define NFSRCHASH(xid) \
60 (&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
61 static LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
62 static TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
63 static u_long nfsrvhash;
68 struct lwkt_token srvcache_token = LWKT_TOKEN_INITIALIZER(srvcache_token);
71 * Static array that defines which nfs rpc's are nonidempotent
73 static int nonidempotent[NFS_NPROCS] = {
102 /* True iff the rpc reply is an nfs status ONLY! */
103 static int nfsv2_repstat[NFS_NPROCS] = {
125 * Size the NFS server's duplicate request cache at 1/2 the nmbclusters,
126 * within a (64, 2048) range. This is to prevent all mbuf clusters being
127 * tied up in the NFS dupreq cache for small values of nmbclusters.
130 nfsrvcache_size_change(void)
132 desirednfsrvcache = nmbclusters / 2;
133 desirednfsrvcache = MIN(desirednfsrvcache, NFSRVCACHE_MAX_SIZE);
134 desirednfsrvcache = MAX(desirednfsrvcache, NFSRVCACHE_MIN_SIZE);
138 * Initialize the server request cache list
141 nfsrv_initcache(void)
143 nfsrvcache_size_change();
144 nfsrvhashtbl = hashinit(desirednfsrvcache, M_NFSD, &nfsrvhash);
145 TAILQ_INIT(&nfsrvlruhead);
149 * Destroy the server request cache list
152 nfsrv_destroycache(void)
154 KKASSERT(TAILQ_EMPTY(&nfsrvlruhead));
155 hashdestroy(nfsrvhashtbl, M_NFSD, nfsrvhash);
159 * Look for the request in the cache
161 * return action and optionally reply
163 * insert it in the cache
165 * The rules are as follows:
166 * - if in progress, return DROP request
167 * - if completed within DELAY of the current time, return DROP it
168 * - if completed a longer time ago return REPLY if the reply was cached or
170 * Update/add new request at end of lru list
173 nfsrv_getcache(struct nfsrv_descript *nd, struct nfssvc_sock *slp,
176 struct nfsrvcache *rp;
178 struct sockaddr_in *saddr;
183 * Don't cache recent requests for reliable transport protocols.
184 * (Maybe we should for the case of a reconnect, but..)
189 lwkt_gettoken(&srvcache_token);
191 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != NULL;
192 rp = rp->rc_hash.le_next) {
193 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
194 netaddr_match(AF_INET, &rp->rc_haddr, nd->nd_nam)) {
195 NFS_DPF(RC, ("H%03x", rp->rc_xid & 0xfff));
196 if ((rp->rc_flag & RC_LOCKED) != 0) {
197 rp->rc_flag |= RC_WANTED;
198 tsleep((caddr_t)rp, 0, "nfsrc", 0);
201 rp->rc_flag |= RC_LOCKED;
202 /* If not at end of LRU chain, move it there */
203 if (TAILQ_NEXT(rp, rc_lru) != NULL) {
204 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
205 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
207 if (rp->rc_state == RC_UNUSED)
208 panic("nfsrv cache");
209 if (rp->rc_state == RC_INPROG) {
210 nfsstats.srvcache_inproghits++;
212 } else if (rp->rc_flag & RC_REPSTATUS) {
213 nfsstats.srvcache_nonidemdonehits++;
214 nfs_rephead(0, nd, slp, rp->rc_status,
217 } else if (rp->rc_flag & RC_REPMBUF) {
218 nfsstats.srvcache_nonidemdonehits++;
219 *repp = m_copym(rp->rc_reply, 0, M_COPYALL,
223 nfsstats.srvcache_idemdonehits++;
224 rp->rc_state = RC_INPROG;
227 rp->rc_flag &= ~RC_LOCKED;
228 if (rp->rc_flag & RC_WANTED) {
229 rp->rc_flag &= ~RC_WANTED;
232 lwkt_reltoken(&srvcache_token);
237 nfsstats.srvcache_misses++;
238 NFS_DPF(RC, ("M%03x", nd->nd_retxid & 0xfff));
239 if (numnfsrvcache < desirednfsrvcache) {
240 rp = kmalloc((u_long)sizeof *rp, M_NFSD, M_WAITOK | M_ZERO);
242 rp->rc_flag = RC_LOCKED;
244 rp = TAILQ_FIRST(&nfsrvlruhead);
245 while ((rp->rc_flag & RC_LOCKED) != 0) {
246 rp->rc_flag |= RC_WANTED;
247 tsleep((caddr_t)rp, 0, "nfsrc", 0);
248 rp = TAILQ_FIRST(&nfsrvlruhead);
250 rp->rc_flag |= RC_LOCKED;
251 LIST_REMOVE(rp, rc_hash);
252 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
253 if (rp->rc_flag & RC_REPMBUF) {
254 m_freem(rp->rc_reply);
256 rp->rc_flag &= ~RC_REPMBUF;
258 if (rp->rc_flag & RC_NAM) {
259 kfree(rp->rc_nam, M_SONAME);
261 rp->rc_flag &= ~RC_NAM;
264 TAILQ_INSERT_TAIL(&nfsrvlruhead, rp, rc_lru);
266 rp->rc_state = RC_INPROG;
267 rp->rc_xid = nd->nd_retxid;
268 saddr = (struct sockaddr_in *)nd->nd_nam;
269 switch (saddr->sin_family) {
271 rp->rc_flag |= RC_INETADDR;
272 rp->rc_inetaddr = saddr->sin_addr.s_addr;
275 rp->rc_flag |= RC_NAM;
276 rp->rc_nam = dup_sockaddr(nd->nd_nam);
279 rp->rc_proc = nd->nd_procnum;
280 LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
281 rp->rc_flag &= ~RC_LOCKED;
282 if (rp->rc_flag & RC_WANTED) {
283 rp->rc_flag &= ~RC_WANTED;
286 lwkt_reltoken(&srvcache_token);
292 * Update a request cache entry after the rpc has been done
295 nfsrv_updatecache(struct nfsrv_descript *nd, int repvalid, struct mbuf *repmbuf)
297 struct nfsrvcache *rp;
302 lwkt_gettoken(&srvcache_token);
304 for (rp = NFSRCHASH(nd->nd_retxid)->lh_first; rp != NULL;
305 rp = rp->rc_hash.le_next) {
306 if (nd->nd_retxid == rp->rc_xid && nd->nd_procnum == rp->rc_proc &&
307 netaddr_match(AF_INET, &rp->rc_haddr, nd->nd_nam)) {
308 NFS_DPF(RC, ("U%03x", rp->rc_xid & 0xfff));
309 if ((rp->rc_flag & RC_LOCKED) != 0) {
310 rp->rc_flag |= RC_WANTED;
311 tsleep((caddr_t)rp, 0, "nfsrc", 0);
314 rp->rc_flag |= RC_LOCKED;
315 if (rp->rc_state == RC_DONE) {
317 * This can occur if the cache is too small.
318 * Retransmits of the same request aren't
319 * dropped so we may see the operation
320 * complete more then once.
322 if (rp->rc_flag & RC_REPMBUF) {
323 m_freem(rp->rc_reply);
325 rp->rc_flag &= ~RC_REPMBUF;
328 rp->rc_state = RC_DONE;
331 * If we have a valid reply update status and save
332 * the reply for non-idempotent rpc's.
334 if (repvalid && nonidempotent[nd->nd_procnum]) {
335 if ((nd->nd_flag & ND_NFSV3) == 0 &&
336 nfsv2_repstat[nfsv2_procid[nd->nd_procnum]]) {
337 rp->rc_status = nd->nd_repstat;
338 rp->rc_flag |= RC_REPSTATUS;
340 if (rp->rc_flag & RC_REPMBUF) {
341 m_freem(rp->rc_reply);
343 rp->rc_flag &= ~RC_REPMBUF;
345 rp->rc_reply = m_copym(repmbuf, 0,
346 M_COPYALL, M_WAITOK);
347 rp->rc_flag |= RC_REPMBUF;
350 rp->rc_flag &= ~RC_LOCKED;
351 if (rp->rc_flag & RC_WANTED) {
352 rp->rc_flag &= ~RC_WANTED;
358 lwkt_reltoken(&srvcache_token);
359 NFS_DPF(RC, ("L%03x", nd->nd_retxid & 0xfff));
363 * Clean out the cache. Called when the last nfsd terminates.
366 nfsrv_cleancache(void)
368 struct nfsrvcache *rp;
370 lwkt_gettoken(&srvcache_token);
371 while ((rp = TAILQ_FIRST(&nfsrvlruhead)) != NULL) {
372 if (rp->rc_flag & RC_LOCKED) {
373 rp->rc_flag |= RC_WANTED;
374 tsleep((caddr_t)rp, 0, "nfsrc", 0);
377 LIST_REMOVE(rp, rc_hash);
378 TAILQ_REMOVE(&nfsrvlruhead, rp, rc_lru);
379 if (rp->rc_flag & RC_REPMBUF) {
380 m_freem(rp->rc_reply);
382 rp->rc_flag &= ~RC_REPMBUF;
384 if (rp->rc_flag & RC_NAM) {
385 kfree(rp->rc_nam, M_SONAME);
387 rp->rc_flag &= ~RC_NAM;
392 lwkt_reltoken(&srvcache_token);
395 #endif /* NFS_NOSERVER */