Import 2.4.0-test3pre1
[davej-history.git] / fs / nfsd / nfscache.c
blobf5795583bd8cea0977b3b7fbee2f742196490537
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;
38 static struct svc_cacherep * lru_head;
39 static struct svc_cacherep * lru_tail;
40 static struct svc_cacherep * nfscache;
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 size_t i;
52 unsigned long order;
54 if (cache_initialized)
55 return;
57 i = CACHESIZE * sizeof (struct svc_cacherep);
58 for (order = 0; (PAGE_SIZE << order) < i; order++)
60 nfscache = (struct svc_cacherep *)
61 __get_free_pages(GFP_KERNEL, order);
62 if (!nfscache) {
63 printk (KERN_ERR "nfsd: cannot allocate %d bytes for reply cache\n", i);
64 return;
66 memset(nfscache, 0, i);
68 i = HASHSIZE * sizeof (struct nfscache_head);
69 hash_list = kmalloc (i, GFP_KERNEL);
70 if (!hash_list) {
71 free_pages ((unsigned long)nfscache, order);
72 nfscache = NULL;
73 printk (KERN_ERR "nfsd: cannot allocate %d bytes for hash list\n", i);
74 return;
77 for (i = 0, rh = hash_list; i < HASHSIZE; i++, rh++)
78 rh->next = rh->prev = (struct svc_cacherep *) rh;
80 for (i = 0, rp = nfscache; i < CACHESIZE; i++, rp++) {
81 rp->c_state = RC_UNUSED;
82 rp->c_type = RC_NOCACHE;
83 rp->c_hash_next =
84 rp->c_hash_prev = rp;
85 rp->c_lru_next = rp + 1;
86 rp->c_lru_prev = rp - 1;
88 lru_head = nfscache;
89 lru_tail = nfscache + CACHESIZE - 1;
90 lru_head->c_lru_prev = NULL;
91 lru_tail->c_lru_next = NULL;
93 cache_initialized = 1;
94 cache_disabled = 0;
97 void
98 nfsd_cache_shutdown(void)
100 struct svc_cacherep *rp;
101 size_t i;
102 unsigned long order;
104 if (!cache_initialized)
105 return;
107 for (rp = lru_head; rp; rp = rp->c_lru_next) {
108 if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF)
109 kfree(rp->c_replbuf.buf);
112 cache_initialized = 0;
113 cache_disabled = 1;
115 i = CACHESIZE * sizeof (struct svc_cacherep);
116 for (order = 0; (PAGE_SIZE << order) < i; order++)
118 free_pages ((unsigned long)nfscache, order);
119 nfscache = NULL;
120 kfree (hash_list);
121 hash_list = NULL;
125 * Move cache entry to front of LRU list
127 static void
128 lru_put_front(struct svc_cacherep *rp)
130 struct svc_cacherep *prev = rp->c_lru_prev,
131 *next = rp->c_lru_next;
133 if (prev)
134 prev->c_lru_next = next;
135 else
136 lru_head = next;
137 if (next)
138 next->c_lru_prev = prev;
139 else
140 lru_tail = prev;
142 rp->c_lru_next = lru_head;
143 rp->c_lru_prev = NULL;
144 if (lru_head)
145 lru_head->c_lru_prev = rp;
146 lru_head = rp;
150 * Move a cache entry from one hash list to another
152 static void
153 hash_refile(struct svc_cacherep *rp)
155 struct svc_cacherep *prev = rp->c_hash_prev,
156 *next = rp->c_hash_next;
157 struct nfscache_head *head = hash_list + REQHASH(rp->c_xid);
159 prev->c_hash_next = next;
160 next->c_hash_prev = prev;
162 rp->c_hash_next = head->next;
163 rp->c_hash_prev = (struct svc_cacherep *) head;
164 head->next->c_hash_prev = rp;
165 head->next = rp;
169 * Try to find an entry matching the current call in the cache. When none
170 * is found, we grab the oldest unlocked entry off the LRU list.
171 * Note that no operation within the loop may sleep.
174 nfsd_cache_lookup(struct svc_rqst *rqstp, int type)
176 struct svc_cacherep *rh, *rp;
177 u32 xid = rqstp->rq_xid,
178 proto = rqstp->rq_prot,
179 vers = rqstp->rq_vers,
180 proc = rqstp->rq_proc;
181 unsigned long age;
183 rqstp->rq_cacherep = NULL;
184 if (cache_disabled || type == RC_NOCACHE) {
185 nfsdstats.rcnocache++;
186 return RC_DOIT;
189 rp = rh = (struct svc_cacherep *) &hash_list[REQHASH(xid)];
190 while ((rp = rp->c_hash_next) != rh) {
191 if (rp->c_state != RC_UNUSED &&
192 xid == rp->c_xid && proc == rp->c_proc &&
193 proto == rp->c_prot && vers == rp->c_vers &&
194 time_before(jiffies, rp->c_timestamp + 120*HZ) &&
195 memcmp((char*)&rqstp->rq_addr, (char*)&rp->c_addr, rqstp->rq_addrlen)==0) {
196 nfsdstats.rchits++;
197 goto found_entry;
200 nfsdstats.rcmisses++;
202 /* This loop shouldn't take more than a few iterations normally */
204 int safe = 0;
205 for (rp = lru_tail; rp; rp = rp->c_lru_prev) {
206 if (rp->c_state != RC_INPROG)
207 break;
208 if (safe++ > CACHESIZE) {
209 printk("nfsd: loop in repcache LRU list\n");
210 cache_disabled = 1;
211 return RC_DOIT;
216 /* This should not happen */
217 if (rp == NULL) {
218 static int complaints = 0;
220 printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
221 if (++complaints > 5) {
222 printk(KERN_WARNING "nfsd: disabling repcache.\n");
223 cache_disabled = 1;
225 return RC_DOIT;
228 rqstp->rq_cacherep = rp;
229 rp->c_state = RC_INPROG;
230 rp->c_xid = xid;
231 rp->c_proc = proc;
232 rp->c_addr = rqstp->rq_addr;
233 rp->c_prot = proto;
234 rp->c_vers = vers;
235 rp->c_timestamp = jiffies;
237 hash_refile(rp);
239 /* release any buffer */
240 if (rp->c_type == RC_REPLBUFF) {
241 kfree(rp->c_replbuf.buf);
242 rp->c_replbuf.buf = NULL;
244 rp->c_type = RC_NOCACHE;
246 return RC_DOIT;
248 found_entry:
249 /* We found a matching entry which is either in progress or done. */
250 age = jiffies - rp->c_timestamp;
251 rp->c_timestamp = jiffies;
252 lru_put_front(rp);
254 /* Request being processed or excessive rexmits */
255 if (rp->c_state == RC_INPROG || age < RC_DELAY)
256 return RC_DROPIT;
258 /* From the hall of fame of impractical attacks:
259 * Is this a user who tries to snoop on the cache? */
260 if (!rqstp->rq_secure && rp->c_secure)
261 return RC_DOIT;
263 /* Compose RPC reply header */
264 switch (rp->c_type) {
265 case RC_NOCACHE:
266 return RC_DOIT;
267 case RC_REPLSTAT:
268 svc_putlong(&rqstp->rq_resbuf, rp->c_replstat);
269 break;
270 case RC_REPLBUFF:
271 if (!nfsd_cache_append(rqstp, &rp->c_replbuf))
272 return RC_DOIT; /* should not happen */
273 break;
274 default:
275 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
276 rp->c_state = RC_UNUSED;
277 return RC_DOIT;
280 return RC_REPLY;
284 * Update a cache entry. This is called from nfsd_dispatch when
285 * the procedure has been executed and the complete reply is in
286 * rqstp->rq_res.
288 * We're copying around data here rather than swapping buffers because
289 * the toplevel loop requires max-sized buffers, which would be a waste
290 * of memory for a cache with a max reply size of 100 bytes (diropokres).
292 * If we should start to use different types of cache entries tailored
293 * specifically for attrstat and fh's, we may save even more space.
295 * Also note that a cachetype of RC_NOCACHE can legally be passed when
296 * nfsd failed to encode a reply that otherwise would have been cached.
297 * In this case, nfsd_cache_update is called with statp == NULL.
299 void
300 nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, u32 *statp)
302 struct svc_cacherep *rp;
303 struct svc_buf *resp = &rqstp->rq_resbuf, *cachp;
304 int len;
306 if (!(rp = rqstp->rq_cacherep) || cache_disabled)
307 return;
309 len = resp->len - (statp - resp->base);
311 /* Don't cache excessive amounts of data and XDR failures */
312 if (!statp || len > (256 >> 2)) {
313 rp->c_state = RC_UNUSED;
314 return;
317 switch (cachetype) {
318 case RC_REPLSTAT:
319 if (len != 1)
320 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
321 rp->c_replstat = *statp;
322 break;
323 case RC_REPLBUFF:
324 cachp = &rp->c_replbuf;
325 cachp->buf = (u32 *) kmalloc(len << 2, GFP_KERNEL);
326 if (!cachp->buf) {
327 rp->c_state = RC_UNUSED;
328 return;
330 cachp->len = len;
331 memcpy(cachp->buf, statp, len << 2);
332 break;
335 lru_put_front(rp);
336 rp->c_secure = rqstp->rq_secure;
337 rp->c_type = cachetype;
338 rp->c_state = RC_DONE;
339 rp->c_timestamp = jiffies;
341 return;
345 * Copy cached reply to current reply buffer. Should always fit.
347 static int
348 nfsd_cache_append(struct svc_rqst *rqstp, struct svc_buf *data)
350 struct svc_buf *resp = &rqstp->rq_resbuf;
352 if (resp->len + data->len > resp->buflen) {
353 printk(KERN_WARNING "nfsd: cached reply too large (%d).\n",
354 data->len);
355 return 0;
357 memcpy(resp->buf, data->buf, data->len << 2);
358 resp->buf += data->len;
359 resp->len += data->len;
360 return 1;