nfs_srvcache.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Rick Macklem at The University of Guelph.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd *
36 1.1 cgd * @(#)nfs_srvcache.c 7.11 (Berkeley) 4/16/91
37 1.1 cgd */
38 1.1 cgd
39 1.1 cgd /*
40 1.1 cgd * Reference: Chet Juszczak, "Improving the Performance and Correctness
41 1.1 cgd * of an NFS Server", in Proc. Winter 1989 USENIX Conference,
42 1.1 cgd * pages 53-63. San Diego, February 1989.
43 1.1 cgd */
44 1.1 cgd
45 1.1 cgd #include "param.h"
46 1.1 cgd #include "namei.h"
47 1.1 cgd #include "vnode.h"
48 1.1 cgd #include "mount.h"
49 1.1 cgd #include "kernel.h"
50 1.1 cgd #include "systm.h"
51 1.1 cgd #include "mbuf.h"
52 1.1 cgd #include "socket.h"
53 1.1 cgd #include "socketvar.h"
54 1.1 cgd
55 1.1 cgd #include "../netinet/in.h"
56 1.1 cgd
57 1.1 cgd #include "nfsm_subs.h"
58 1.1 cgd #include "nfsv2.h"
59 1.1 cgd #include "nfsrvcache.h"
60 1.1 cgd #include "nfs.h"
61 1.1 cgd
62 1.1 cgd #if ((NFSRCHSZ&(NFSRCHSZ-1)) == 0)
63 1.1 cgd #define NFSRCHASH(xid) (((xid)+((xid)>>16))&(NFSRCHSZ-1))
64 1.1 cgd #else
65 1.1 cgd #define NFSRCHASH(xid) (((unsigned)((xid)+((xid)>>16)))%NFSRCHSZ)
66 1.1 cgd #endif
67 1.1 cgd
68 1.1 cgd union rhead {
69 1.1 cgd union rhead *rh_head[2];
70 1.1 cgd struct nfsrvcache *rh_chain[2];
71 1.1 cgd } rhead[NFSRCHSZ];
72 1.1 cgd
73 1.1 cgd static struct nfsrvcache nfsrvcachehead;
74 1.1 cgd static struct nfsrvcache nfsrvcache[NFSRVCACHESIZ];
75 1.1 cgd
76 1.1 cgd #define TRUE 1
77 1.1 cgd #define FALSE 0
78 1.1 cgd
79 1.1 cgd /*
80 1.1 cgd * Static array that defines which nfs rpc's are nonidempotent
81 1.1 cgd */
82 1.1 cgd int nonidempotent[NFS_NPROCS] = {
83 1.1 cgd FALSE,
84 1.1 cgd FALSE,
85 1.1 cgd TRUE,
86 1.1 cgd FALSE,
87 1.1 cgd FALSE,
88 1.1 cgd FALSE,
89 1.1 cgd FALSE,
90 1.1 cgd FALSE,
91 1.1 cgd TRUE,
92 1.1 cgd TRUE,
93 1.1 cgd TRUE,
94 1.1 cgd TRUE,
95 1.1 cgd TRUE,
96 1.1 cgd TRUE,
97 1.1 cgd TRUE,
98 1.1 cgd TRUE,
99 1.1 cgd FALSE,
100 1.1 cgd FALSE,
101 1.1 cgd };
102 1.1 cgd
103 1.1 cgd /* True iff the rpc reply is an nfs status ONLY! */
104 1.1 cgd static int repliesstatus[NFS_NPROCS] = {
105 1.1 cgd FALSE,
106 1.1 cgd FALSE,
107 1.1 cgd FALSE,
108 1.1 cgd FALSE,
109 1.1 cgd FALSE,
110 1.1 cgd FALSE,
111 1.1 cgd FALSE,
112 1.1 cgd FALSE,
113 1.1 cgd FALSE,
114 1.1 cgd FALSE,
115 1.1 cgd TRUE,
116 1.1 cgd TRUE,
117 1.1 cgd TRUE,
118 1.1 cgd TRUE,
119 1.1 cgd FALSE,
120 1.1 cgd TRUE,
121 1.1 cgd FALSE,
122 1.1 cgd FALSE,
123 1.1 cgd };
124 1.1 cgd
125 1.1 cgd /*
126 1.1 cgd * Initialize the server request cache list
127 1.1 cgd */
128 1.1 cgd nfsrv_initcache()
129 1.1 cgd {
130 1.1 cgd register int i;
131 1.1 cgd register struct nfsrvcache *rp = nfsrvcache;
132 1.1 cgd register struct nfsrvcache *hp = &nfsrvcachehead;
133 1.1 cgd register union rhead *rh = rhead;
134 1.1 cgd
135 1.1 cgd for (i = NFSRCHSZ; --i >= 0; rh++) {
136 1.1 cgd rh->rh_head[0] = rh;
137 1.1 cgd rh->rh_head[1] = rh;
138 1.1 cgd }
139 1.1 cgd hp->rc_next = hp->rc_prev = hp;
140 1.1 cgd for (i = NFSRVCACHESIZ; i-- > 0; ) {
141 1.1 cgd rp->rc_state = RC_UNUSED;
142 1.1 cgd rp->rc_flag = 0;
143 1.1 cgd rp->rc_forw = rp;
144 1.1 cgd rp->rc_back = rp;
145 1.1 cgd rp->rc_next = hp->rc_next;
146 1.1 cgd hp->rc_next->rc_prev = rp;
147 1.1 cgd rp->rc_prev = hp;
148 1.1 cgd hp->rc_next = rp;
149 1.1 cgd rp++;
150 1.1 cgd }
151 1.1 cgd }
152 1.1 cgd
153 1.1 cgd /*
154 1.1 cgd * Look for the request in the cache
155 1.1 cgd * If found then
156 1.1 cgd * return action and optionally reply
157 1.1 cgd * else
158 1.1 cgd * insert it in the cache
159 1.1 cgd *
160 1.1 cgd * The rules are as follows:
161 1.1 cgd * - if in progress, return DROP request
162 1.1 cgd * - if completed within DELAY of the current time, return DROP it
163 1.1 cgd * - if completed a longer time ago return REPLY if the reply was cached or
164 1.1 cgd * return DOIT
165 1.1 cgd * Update/add new request at end of lru list
166 1.1 cgd */
167 1.1 cgd nfsrv_getcache(nam, xid, proc, repp)
168 1.1 cgd struct mbuf *nam;
169 1.1 cgd u_long xid;
170 1.1 cgd int proc;
171 1.1 cgd struct mbuf **repp;
172 1.1 cgd {
173 1.1 cgd register struct nfsrvcache *rp;
174 1.1 cgd register union rhead *rh;
175 1.1 cgd struct mbuf *mb;
176 1.1 cgd caddr_t bpos;
177 1.1 cgd int ret;
178 1.1 cgd
179 1.1 cgd rh = &rhead[NFSRCHASH(xid)];
180 1.1 cgd loop:
181 1.1 cgd for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
182 1.1 cgd if (xid == rp->rc_xid && proc == rp->rc_proc &&
183 1.1 cgd nfs_netaddr_match(nam, &rp->rc_nam)) {
184 1.1 cgd if ((rp->rc_flag & RC_LOCKED) != 0) {
185 1.1 cgd rp->rc_flag |= RC_WANTED;
186 1.1 cgd (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
187 1.1 cgd goto loop;
188 1.1 cgd }
189 1.1 cgd rp->rc_flag |= RC_LOCKED;
190 1.1 cgd put_at_head(rp);
191 1.1 cgd if (rp->rc_state == RC_UNUSED)
192 1.1 cgd panic("nfsrv cache");
193 1.1 cgd if (rp->rc_state == RC_INPROG ||
194 1.1 cgd (time.tv_sec - rp->rc_timestamp) < RC_DELAY) {
195 1.1 cgd nfsstats.srvcache_inproghits++;
196 1.1 cgd ret = RC_DROPIT;
197 1.1 cgd } else if (rp->rc_flag & RC_REPSTATUS) {
198 1.1 cgd nfsstats.srvcache_idemdonehits++;
199 1.1 cgd nfs_rephead(0, xid, rp->rc_status, repp, &mb,
200 1.1 cgd &bpos);
201 1.1 cgd rp->rc_timestamp = time.tv_sec;
202 1.1 cgd ret = RC_REPLY;
203 1.1 cgd } else if (rp->rc_flag & RC_REPMBUF) {
204 1.1 cgd nfsstats.srvcache_idemdonehits++;
205 1.1 cgd *repp = m_copym(rp->rc_reply, 0, M_COPYALL,
206 1.1 cgd M_WAIT);
207 1.1 cgd rp->rc_timestamp = time.tv_sec;
208 1.1 cgd ret = RC_REPLY;
209 1.1 cgd } else {
210 1.1 cgd nfsstats.srvcache_nonidemdonehits++;
211 1.1 cgd rp->rc_state = RC_INPROG;
212 1.1 cgd ret = RC_DOIT;
213 1.1 cgd }
214 1.1 cgd rp->rc_flag &= ~RC_LOCKED;
215 1.1 cgd if (rp->rc_flag & RC_WANTED) {
216 1.1 cgd rp->rc_flag &= ~RC_WANTED;
217 1.1 cgd wakeup((caddr_t)rp);
218 1.1 cgd }
219 1.1 cgd return (ret);
220 1.1 cgd }
221 1.1 cgd }
222 1.1 cgd nfsstats.srvcache_misses++;
223 1.1 cgd rp = nfsrvcachehead.rc_prev;
224 1.1 cgd while ((rp->rc_flag & RC_LOCKED) != 0) {
225 1.1 cgd rp->rc_flag |= RC_WANTED;
226 1.1 cgd (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
227 1.1 cgd }
228 1.1 cgd remque(rp);
229 1.1 cgd put_at_head(rp);
230 1.1 cgd if (rp->rc_flag & RC_REPMBUF)
231 1.1 cgd mb = rp->rc_reply;
232 1.1 cgd else
233 1.1 cgd mb = (struct mbuf *)0;
234 1.1 cgd rp->rc_flag = 0;
235 1.1 cgd rp->rc_state = RC_INPROG;
236 1.1 cgd rp->rc_xid = xid;
237 1.1 cgd bcopy((caddr_t)nam, (caddr_t)&rp->rc_nam, sizeof (struct mbuf));
238 1.1 cgd rp->rc_proc = proc;
239 1.1 cgd insque(rp, rh);
240 1.1 cgd if (mb)
241 1.1 cgd m_freem(mb);
242 1.1 cgd return (RC_DOIT);
243 1.1 cgd }
244 1.1 cgd
245 1.1 cgd /*
246 1.1 cgd * Update a request cache entry after the rpc has been done
247 1.1 cgd */
248 1.1 cgd nfsrv_updatecache(nam, xid, proc, repvalid, repstat, repmbuf)
249 1.1 cgd struct mbuf *nam;
250 1.1 cgd u_long xid;
251 1.1 cgd int proc;
252 1.1 cgd int repvalid;
253 1.1 cgd int repstat;
254 1.1 cgd struct mbuf *repmbuf;
255 1.1 cgd {
256 1.1 cgd register struct nfsrvcache *rp;
257 1.1 cgd register union rhead *rh;
258 1.1 cgd
259 1.1 cgd rh = &rhead[NFSRCHASH(xid)];
260 1.1 cgd loop:
261 1.1 cgd for (rp = rh->rh_chain[0]; rp != (struct nfsrvcache *)rh; rp = rp->rc_forw) {
262 1.1 cgd if (xid == rp->rc_xid && proc == rp->rc_proc &&
263 1.1 cgd nfs_netaddr_match(nam, &rp->rc_nam)) {
264 1.1 cgd if ((rp->rc_flag & RC_LOCKED) != 0) {
265 1.1 cgd rp->rc_flag |= RC_WANTED;
266 1.1 cgd (void) tsleep((caddr_t)rp, PZERO-1, "nfsrc", 0);
267 1.1 cgd goto loop;
268 1.1 cgd }
269 1.1 cgd rp->rc_flag |= RC_LOCKED;
270 1.1 cgd rp->rc_state = RC_DONE;
271 1.1 cgd /*
272 1.1 cgd * If we have a valid reply update status and save
273 1.1 cgd * the reply for non-idempotent rpc's.
274 1.1 cgd * Otherwise invalidate entry by setting the timestamp
275 1.1 cgd * to nil.
276 1.1 cgd */
277 1.1 cgd if (repvalid) {
278 1.1 cgd rp->rc_timestamp = time.tv_sec;
279 1.1 cgd if (nonidempotent[proc]) {
280 1.1 cgd if (repliesstatus[proc]) {
281 1.1 cgd rp->rc_status = repstat;
282 1.1 cgd rp->rc_flag |= RC_REPSTATUS;
283 1.1 cgd } else {
284 1.1 cgd rp->rc_reply = m_copym(repmbuf,
285 1.1 cgd 0, M_COPYALL, M_WAIT);
286 1.1 cgd rp->rc_flag |= RC_REPMBUF;
287 1.1 cgd }
288 1.1 cgd }
289 1.1 cgd } else {
290 1.1 cgd rp->rc_timestamp = 0;
291 1.1 cgd }
292 1.1 cgd rp->rc_flag &= ~RC_LOCKED;
293 1.1 cgd if (rp->rc_flag & RC_WANTED) {
294 1.1 cgd rp->rc_flag &= ~RC_WANTED;
295 1.1 cgd wakeup((caddr_t)rp);
296 1.1 cgd }
297 1.1 cgd return;
298 1.1 cgd }
299 1.1 cgd }
300 1.1 cgd }
301