Linux 2.2.0
[davej-history.git] / fs / nfsd / nfscache.c
blob370411d7c2a95e2ac6a08568e2f803ce499cd8ef
1 /*
2 * linux/fs/nfsd/nfscache.c
4 * Request reply cache. This is currently a global cache, but this may
5 * change in the future and be a per-client cache.
7 * This code is heavily inspired by the 44BSD implementation, although
8 * it does things a bit differently.
10 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/malloc.h>
16 #include <linux/string.h>
18 #include <linux/sunrpc/svc.h>
19 #include <linux/nfsd/nfsd.h>
20 #include <linux/nfsd/cache.h>
22 /* Size of reply cache. Common values are:
23 * 4.3BSD: 128
24 * 4.4BSD: 256
25 * Solaris2: 1024
26 * DEC Unix: 512-4096
28 #define CACHESIZE 1024
29 #define HASHSIZE 64
30 #define REQHASH(xid) ((((xid) >> 24) ^ (xid)) & (HASHSIZE-1))
32 struct nfscache_head {
33 struct svc_cacherep * next;
34 struct svc_cacherep * prev;
37 static struct nfscache_head hash_list[HASHSIZE];
38 static struct svc_cacherep * lru_head;
39 static struct svc_cacherep * lru_tail;
40 static struct svc_cacherep nfscache[CACHESIZE];
41 static int cache_initialized = 0;
42 static int cache_disabled = 1;
44 static int nfsd_cache_append(struct svc_rqst *rqstp, struct svc_buf *data);
46 void
47 nfsd_cache_init(void)
49 struct svc_cacherep *rp;
50 struct nfscache_head *rh;
51 int i;
53 if (cache_initialized)
54 return;
56 for (i = 0, rh = hash_list; i < HASHSIZE; i++, rh++)
57 rh->next = rh->prev = (struct svc_cacherep *) rh;
59 for (i = 0, rp = nfscache; i < CACHESIZE; i++, rp++) {
60 rp->c_state = RC_UNUSED;
61 rp->c_type = RC_NOCACHE;
62 rp->c_hash_next =
63 rp->c_hash_prev = rp;
64 rp->c_lru_next = rp + 1;
65 rp->c_lru_prev = rp - 1;
67 lru_head = nfscache;
68 lru_tail = nfscache + CACHESIZE - 1;
69 lru_head->c_lru_prev = NULL;
70 lru_tail->c_lru_next = NULL;
72 cache_initialized = 1;
73 cache_disabled = 0;
76 void
77 nfsd_cache_shutdown(void)
79 struct svc_cacherep *rp;
81 if (!cache_initialized)
82 return;
84 for (rp = lru_head; rp; rp = rp->c_lru_next) {
85 if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF)
86 kfree(rp->c_replbuf.buf);
89 cache_initialized = 0;
90 cache_disabled = 1;
94 * Move cache entry to front of LRU list
96 static void
97 lru_put_front(struct svc_cacherep *rp)
99 struct svc_cacherep *prev = rp->c_lru_prev,
100 *next = rp->c_lru_next;
102 if (prev)
103 prev->c_lru_next = next;
104 else
105 lru_head = next;
106 if (next)
107 next->c_lru_prev = prev;
108 else
109 lru_tail = prev;
111 rp->c_lru_next = lru_head;
112 rp->c_lru_prev = NULL;
113 if (lru_head)
114 lru_head->c_lru_prev = rp;
115 lru_head = rp;
119 * Move a cache entry from one hash list to another
121 static void
122 hash_refile(struct svc_cacherep *rp)
124 struct svc_cacherep *prev = rp->c_hash_prev,
125 *next = rp->c_hash_next;
126 struct nfscache_head *head = hash_list + REQHASH(rp->c_xid);
128 prev->c_hash_next = next;
129 next->c_hash_prev = prev;
131 rp->c_hash_next = head->next;
132 rp->c_hash_prev = (struct svc_cacherep *) head;
133 head->next->c_hash_prev = rp;
134 head->next = rp;
138 * Try to find an entry matching the current call in the cache. When none
139 * is found, we grab the oldest unlocked entry off the LRU list.
140 * Note that no operation within the loop may sleep.
143 nfsd_cache_lookup(struct svc_rqst *rqstp, int type)
145 struct svc_cacherep *rh, *rp;
146 struct svc_client *clp = rqstp->rq_client;
147 u32 xid = rqstp->rq_xid,
148 proc = rqstp->rq_proc;
149 unsigned long age;
151 rqstp->rq_cacherep = NULL;
152 if (cache_disabled || type == RC_NOCACHE) {
153 nfsdstats.rcnocache++;
154 return RC_DOIT;
157 rp = rh = (struct svc_cacherep *) &hash_list[REQHASH(xid)];
158 while ((rp = rp->c_hash_next) != rh) {
159 if (rp->c_state != RC_UNUSED &&
160 xid == rp->c_xid && proc == rp->c_proc &&
161 exp_checkaddr(clp, rp->c_client)) {
162 nfsdstats.rchits++;
163 goto found_entry;
166 nfsdstats.rcmisses++;
168 /* This loop shouldn't take more than a few iterations normally */
170 int safe = 0;
171 for (rp = lru_tail; rp; rp = rp->c_lru_prev) {
172 if (rp->c_state != RC_INPROG)
173 break;
174 if (safe++ > CACHESIZE) {
175 printk("nfsd: loop in repcache LRU list\n");
176 cache_disabled = 1;
177 return RC_DOIT;
182 /* This should not happen */
183 if (rp == NULL) {
184 static int complaints = 0;
186 printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
187 if (++complaints > 5) {
188 printk(KERN_WARNING "nfsd: disabling repcache.\n");
189 cache_disabled = 1;
191 return RC_DOIT;
194 rqstp->rq_cacherep = rp;
195 rp->c_state = RC_INPROG;
196 rp->c_xid = xid;
197 rp->c_proc = proc;
198 rp->c_client = rqstp->rq_addr.sin_addr;
199 hash_refile(rp);
201 /* release any buffer */
202 if (rp->c_type == RC_REPLBUFF) {
203 kfree(rp->c_replbuf.buf);
204 rp->c_replbuf.buf = NULL;
206 rp->c_type = RC_NOCACHE;
208 return RC_DOIT;
210 found_entry:
211 /* We found a matching entry which is either in progress or done. */
212 age = jiffies - rp->c_timestamp;
213 rp->c_timestamp = jiffies;
214 lru_put_front(rp);
216 /* Request being processed or excessive rexmits */
217 if (rp->c_state == RC_INPROG || age < RC_DELAY)
218 return RC_DROPIT;
220 /* From the hall of fame of impractical attacks:
221 * Is this a user who tries to snoop on the cache? */
222 if (!rqstp->rq_secure && rp->c_secure)
223 return RC_DOIT;
225 /* Compose RPC reply header */
226 switch (rp->c_type) {
227 case RC_NOCACHE:
228 return RC_DOIT;
229 case RC_REPLSTAT:
230 svc_putlong(&rqstp->rq_resbuf, rp->c_replstat);
231 break;
232 case RC_REPLBUFF:
233 if (!nfsd_cache_append(rqstp, &rp->c_replbuf))
234 return RC_DOIT; /* should not happen */
235 break;
236 default:
237 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
238 rp->c_state = RC_UNUSED;
239 return RC_DOIT;
242 return RC_REPLY;
246 * Update a cache entry. This is called from nfsd_dispatch when
247 * the procedure has been executed and the complete reply is in
248 * rqstp->rq_res.
250 * We're copying around data here rather than swapping buffers because
251 * the toplevel loop requires max-sized buffers, which would be a waste
252 * of memory for a cache with a max reply size of 100 bytes (diropokres).
254 * If we should start to use different types of cache entries tailored
255 * specifically for attrstat and fh's, we may save even more space.
257 * Also note that a cachetype of RC_NOCACHE can legally be passed when
258 * nfsd failed to encode a reply that otherwise would have been cached.
259 * In this case, nfsd_cache_update is called with statp == NULL.
261 void
262 nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, u32 *statp)
264 struct svc_cacherep *rp;
265 struct svc_buf *resp = &rqstp->rq_resbuf, *cachp;
266 int len;
268 if (!(rp = rqstp->rq_cacherep) || cache_disabled)
269 return;
271 len = resp->len - (statp - resp->base);
273 /* Don't cache excessive amounts of data and XDR failures */
274 if (!statp || len > (256 >> 2)) {
275 rp->c_state = RC_UNUSED;
276 return;
279 switch (cachetype) {
280 case RC_REPLSTAT:
281 if (len != 1)
282 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
283 rp->c_replstat = *statp;
284 break;
285 case RC_REPLBUFF:
286 cachp = &rp->c_replbuf;
287 cachp->buf = (u32 *) kmalloc(len << 2, GFP_KERNEL);
288 if (!cachp->buf) {
289 rp->c_state = RC_UNUSED;
290 return;
292 cachp->len = len;
293 memcpy(cachp->buf, statp, len << 2);
294 break;
297 lru_put_front(rp);
298 rp->c_secure = rqstp->rq_secure;
299 rp->c_type = cachetype;
300 rp->c_state = RC_DONE;
301 rp->c_timestamp = jiffies;
303 return;
307 * Copy cached reply to current reply buffer. Should always fit.
309 static int
310 nfsd_cache_append(struct svc_rqst *rqstp, struct svc_buf *data)
312 struct svc_buf *resp = &rqstp->rq_resbuf;
314 if (resp->len + data->len > resp->buflen) {
315 printk(KERN_WARNING "nfsd: cached reply too large (%d).\n",
316 data->len);
317 return 0;
319 memcpy(resp->buf, data->buf, data->len << 2);
320 resp->buf += data->len;
321 resp->len += data->len;
322 return 1;