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:
28 #define CACHESIZE 1024
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
;
42 static int cache_disabled
= 1;
44 static int nfsd_cache_append(struct svc_rqst
*rqstp
, struct svc_buf
*data
);
49 struct svc_cacherep
*rp
;
50 struct nfscache_head
*rh
;
54 if (cache_initialized
)
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
);
63 printk (KERN_ERR
"nfsd: cannot allocate %Zd bytes for reply cache\n", i
);
66 memset(nfscache
, 0, i
);
68 i
= HASHSIZE
* sizeof (struct nfscache_head
);
69 hash_list
= kmalloc (i
, GFP_KERNEL
);
71 free_pages ((unsigned long)nfscache
, order
);
73 printk (KERN_ERR
"nfsd: cannot allocate %Zd bytes for hash list\n", i
);
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
;
85 rp
->c_lru_next
= rp
+ 1;
86 rp
->c_lru_prev
= rp
- 1;
89 lru_tail
= nfscache
+ CACHESIZE
- 1;
90 lru_head
->c_lru_prev
= NULL
;
91 lru_tail
->c_lru_next
= NULL
;
93 cache_initialized
= 1;
98 nfsd_cache_shutdown(void)
100 struct svc_cacherep
*rp
;
104 if (!cache_initialized
)
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;
115 i
= CACHESIZE
* sizeof (struct svc_cacherep
);
116 for (order
= 0; (PAGE_SIZE
<< order
) < i
; order
++)
118 free_pages ((unsigned long)nfscache
, order
);
125 * Move cache entry to front of LRU list
128 lru_put_front(struct svc_cacherep
*rp
)
130 struct svc_cacherep
*prev
= rp
->c_lru_prev
,
131 *next
= rp
->c_lru_next
;
134 prev
->c_lru_next
= next
;
138 next
->c_lru_prev
= prev
;
142 rp
->c_lru_next
= lru_head
;
143 rp
->c_lru_prev
= NULL
;
145 lru_head
->c_lru_prev
= rp
;
150 * Move a cache entry from one hash list to another
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
;
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
;
183 rqstp
->rq_cacherep
= NULL
;
184 if (cache_disabled
|| type
== RC_NOCACHE
) {
185 nfsdstats
.rcnocache
++;
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) {
200 nfsdstats
.rcmisses
++;
202 /* This loop shouldn't take more than a few iterations normally */
205 for (rp
= lru_tail
; rp
; rp
= rp
->c_lru_prev
) {
206 if (rp
->c_state
!= RC_INPROG
)
208 if (safe
++ > CACHESIZE
) {
209 printk("nfsd: loop in repcache LRU list\n");
216 /* This should not happen */
218 static int complaints
;
220 printk(KERN_WARNING
"nfsd: all repcache entries locked!\n");
221 if (++complaints
> 5) {
222 printk(KERN_WARNING
"nfsd: disabling repcache.\n");
228 rqstp
->rq_cacherep
= rp
;
229 rp
->c_state
= RC_INPROG
;
232 rp
->c_addr
= rqstp
->rq_addr
;
235 rp
->c_timestamp
= jiffies
;
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
;
249 /* We found a matching entry which is either in progress or done. */
250 age
= jiffies
- rp
->c_timestamp
;
251 rp
->c_timestamp
= jiffies
;
254 /* Request being processed or excessive rexmits */
255 if (rp
->c_state
== RC_INPROG
|| age
< RC_DELAY
)
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
)
263 /* Compose RPC reply header */
264 switch (rp
->c_type
) {
268 svc_putlong(&rqstp
->rq_resbuf
, rp
->c_replstat
);
271 if (!nfsd_cache_append(rqstp
, &rp
->c_replbuf
))
272 return RC_DOIT
; /* should not happen */
275 printk(KERN_WARNING
"nfsd: bad repcache type %d\n", rp
->c_type
);
276 rp
->c_state
= RC_UNUSED
;
284 * Update a cache entry. This is called from nfsd_dispatch when
285 * the procedure has been executed and the complete reply is in
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.
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
;
306 if (!(rp
= rqstp
->rq_cacherep
) || cache_disabled
)
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
;
320 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len
);
321 rp
->c_replstat
= *statp
;
324 cachp
= &rp
->c_replbuf
;
325 cachp
->buf
= (u32
*) kmalloc(len
<< 2, GFP_KERNEL
);
327 rp
->c_state
= RC_UNUSED
;
331 memcpy(cachp
->buf
, statp
, len
<< 2);
336 rp
->c_secure
= rqstp
->rq_secure
;
337 rp
->c_type
= cachetype
;
338 rp
->c_state
= RC_DONE
;
339 rp
->c_timestamp
= jiffies
;
345 * Copy cached reply to current reply buffer. Should always fit.
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",
357 memcpy(resp
->buf
, data
->buf
, data
->len
<< 2);
358 resp
->buf
+= data
->len
;
359 resp
->len
+= data
->len
;