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
[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
);
49 struct svc_cacherep
*rp
;
50 struct nfscache_head
*rh
;
53 if (cache_initialized
)
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
;
64 rp
->c_lru_next
= rp
+ 1;
65 rp
->c_lru_prev
= rp
- 1;
68 lru_tail
= nfscache
+ CACHESIZE
- 1;
69 lru_head
->c_lru_prev
= NULL
;
70 lru_tail
->c_lru_next
= NULL
;
72 cache_initialized
= 1;
77 nfsd_cache_shutdown(void)
79 struct svc_cacherep
*rp
;
81 if (!cache_initialized
)
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;
94 * Move cache entry to front of LRU list
97 lru_put_front(struct svc_cacherep
*rp
)
99 struct svc_cacherep
*prev
= rp
->c_lru_prev
,
100 *next
= rp
->c_lru_next
;
103 prev
->c_lru_next
= next
;
107 next
->c_lru_prev
= prev
;
111 rp
->c_lru_next
= lru_head
;
112 rp
->c_lru_prev
= NULL
;
114 lru_head
->c_lru_prev
= rp
;
119 * Move a cache entry from one hash list to another
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
;
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
;
151 rqstp
->rq_cacherep
= NULL
;
152 if (cache_disabled
|| type
== RC_NOCACHE
) {
153 nfsdstats
.rcnocache
++;
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
)) {
166 nfsdstats
.rcmisses
++;
168 /* This loop shouldn't take more than a few iterations normally */
171 for (rp
= lru_tail
; rp
; rp
= rp
->c_lru_prev
) {
172 if (rp
->c_state
!= RC_INPROG
)
174 if (safe
++ > CACHESIZE
) {
175 printk("nfsd: loop in repcache LRU list\n");
182 /* This should not happen */
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");
194 rqstp
->rq_cacherep
= rp
;
195 rp
->c_state
= RC_INPROG
;
198 rp
->c_client
= rqstp
->rq_addr
.sin_addr
;
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
;
211 /* We found a matching entry which is either in progress or done. */
212 age
= jiffies
- rp
->c_timestamp
;
213 rp
->c_timestamp
= jiffies
;
216 /* Request being processed or excessive rexmits */
217 if (rp
->c_state
== RC_INPROG
|| age
< RC_DELAY
)
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
)
225 /* Compose RPC reply header */
226 switch (rp
->c_type
) {
230 svc_putlong(&rqstp
->rq_resbuf
, rp
->c_replstat
);
233 if (!nfsd_cache_append(rqstp
, &rp
->c_replbuf
))
234 return RC_DOIT
; /* should not happen */
237 printk(KERN_WARNING
"nfsd: bad repcache type %d\n", rp
->c_type
);
238 rp
->c_state
= RC_UNUSED
;
246 * Update a cache entry. This is called from nfsd_dispatch when
247 * the procedure has been executed and the complete reply is in
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.
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
;
268 if (!(rp
= rqstp
->rq_cacherep
) || cache_disabled
)
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
;
282 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len
);
283 rp
->c_replstat
= *statp
;
286 cachp
= &rp
->c_replbuf
;
287 cachp
->buf
= (u32
*) kmalloc(len
<< 2, GFP_KERNEL
);
289 rp
->c_state
= RC_UNUSED
;
293 memcpy(cachp
->buf
, statp
, len
<< 2);
298 rp
->c_secure
= rqstp
->rq_secure
;
299 rp
->c_type
= cachetype
;
300 rp
->c_state
= RC_DONE
;
301 rp
->c_timestamp
= jiffies
;
307 * Copy cached reply to current reply buffer. Should always fit.
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",
319 memcpy(resp
->buf
, data
->buf
, data
->len
<< 2);
320 resp
->buf
+= data
->len
;
321 resp
->len
+= data
->len
;