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/time.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/spinlock.h>
18 #include <linux/list.h>
20 #include <linux/sunrpc/svc.h>
21 #include <linux/nfsd/nfsd.h>
22 #include <linux/nfsd/cache.h>
24 /* Size of reply cache. Common values are:
30 #define CACHESIZE 1024
32 #define REQHASH(xid) (((((__force __u32)xid) >> 24) ^ ((__force __u32)xid)) & (HASHSIZE-1))
34 static struct hlist_head
* hash_list
;
35 static struct list_head lru_head
;
36 static int cache_disabled
= 1;
38 static int nfsd_cache_append(struct svc_rqst
*rqstp
, struct kvec
*vec
);
41 * locking for the reply cache:
42 * A cache entry is "single use" if c_state == RC_INPROG
43 * Otherwise, it when accessing _prev or _next, the lock must be held.
45 static DEFINE_SPINLOCK(cache_lock
);
48 nfsd_reply_cache_init(void)
50 struct svc_cacherep
*rp
;
53 INIT_LIST_HEAD(&lru_head
);
56 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
59 list_add(&rp
->c_lru
, &lru_head
);
60 rp
->c_state
= RC_UNUSED
;
61 rp
->c_type
= RC_NOCACHE
;
62 INIT_HLIST_NODE(&rp
->c_hash
);
66 hash_list
= kcalloc (HASHSIZE
, sizeof(struct hlist_head
), GFP_KERNEL
);
73 printk(KERN_ERR
"nfsd: failed to allocate reply cache\n");
74 nfsd_reply_cache_shutdown();
79 nfsd_reply_cache_shutdown(void)
81 struct svc_cacherep
*rp
;
83 while (!list_empty(&lru_head
)) {
84 rp
= list_entry(lru_head
.next
, struct svc_cacherep
, c_lru
);
85 if (rp
->c_state
== RC_DONE
&& rp
->c_type
== RC_REPLBUFF
)
86 kfree(rp
->c_replvec
.iov_base
);
98 * Move cache entry to end of LRU list
101 lru_put_end(struct svc_cacherep
*rp
)
103 list_move_tail(&rp
->c_lru
, &lru_head
);
107 * Move a cache entry from one hash list to another
110 hash_refile(struct svc_cacherep
*rp
)
112 hlist_del_init(&rp
->c_hash
);
113 hlist_add_head(&rp
->c_hash
, hash_list
+ REQHASH(rp
->c_xid
));
117 * Try to find an entry matching the current call in the cache. When none
118 * is found, we grab the oldest unlocked entry off the LRU list.
119 * Note that no operation within the loop may sleep.
122 nfsd_cache_lookup(struct svc_rqst
*rqstp
, int type
)
124 struct hlist_node
*hn
;
125 struct hlist_head
*rh
;
126 struct svc_cacherep
*rp
;
127 __be32 xid
= rqstp
->rq_xid
;
128 u32 proto
= rqstp
->rq_prot
,
129 vers
= rqstp
->rq_vers
,
130 proc
= rqstp
->rq_proc
;
134 rqstp
->rq_cacherep
= NULL
;
135 if (cache_disabled
|| type
== RC_NOCACHE
) {
136 nfsdstats
.rcnocache
++;
140 spin_lock(&cache_lock
);
143 rh
= &hash_list
[REQHASH(xid
)];
144 hlist_for_each_entry(rp
, hn
, rh
, c_hash
) {
145 if (rp
->c_state
!= RC_UNUSED
&&
146 xid
== rp
->c_xid
&& proc
== rp
->c_proc
&&
147 proto
== rp
->c_prot
&& vers
== rp
->c_vers
&&
148 time_before(jiffies
, rp
->c_timestamp
+ 120*HZ
) &&
149 memcmp((char*)&rqstp
->rq_addr
, (char*)&rp
->c_addr
, sizeof(rp
->c_addr
))==0) {
154 nfsdstats
.rcmisses
++;
156 /* This loop shouldn't take more than a few iterations normally */
159 list_for_each_entry(rp
, &lru_head
, c_lru
) {
160 if (rp
->c_state
!= RC_INPROG
)
162 if (safe
++ > CACHESIZE
) {
163 printk("nfsd: loop in repcache LRU list\n");
170 /* This should not happen */
172 static int complaints
;
174 printk(KERN_WARNING
"nfsd: all repcache entries locked!\n");
175 if (++complaints
> 5) {
176 printk(KERN_WARNING
"nfsd: disabling repcache.\n");
182 rqstp
->rq_cacherep
= rp
;
183 rp
->c_state
= RC_INPROG
;
186 memcpy(&rp
->c_addr
, svc_addr_in(rqstp
), sizeof(rp
->c_addr
));
189 rp
->c_timestamp
= jiffies
;
193 /* release any buffer */
194 if (rp
->c_type
== RC_REPLBUFF
) {
195 kfree(rp
->c_replvec
.iov_base
);
196 rp
->c_replvec
.iov_base
= NULL
;
198 rp
->c_type
= RC_NOCACHE
;
200 spin_unlock(&cache_lock
);
204 /* We found a matching entry which is either in progress or done. */
205 age
= jiffies
- rp
->c_timestamp
;
206 rp
->c_timestamp
= jiffies
;
210 /* Request being processed or excessive rexmits */
211 if (rp
->c_state
== RC_INPROG
|| age
< RC_DELAY
)
214 /* From the hall of fame of impractical attacks:
215 * Is this a user who tries to snoop on the cache? */
217 if (!rqstp
->rq_secure
&& rp
->c_secure
)
220 /* Compose RPC reply header */
221 switch (rp
->c_type
) {
225 svc_putu32(&rqstp
->rq_res
.head
[0], rp
->c_replstat
);
229 if (!nfsd_cache_append(rqstp
, &rp
->c_replvec
))
230 goto out
; /* should not happen */
234 printk(KERN_WARNING
"nfsd: bad repcache type %d\n", rp
->c_type
);
235 rp
->c_state
= RC_UNUSED
;
242 * Update a cache entry. This is called from nfsd_dispatch when
243 * the procedure has been executed and the complete reply is in
246 * We're copying around data here rather than swapping buffers because
247 * the toplevel loop requires max-sized buffers, which would be a waste
248 * of memory for a cache with a max reply size of 100 bytes (diropokres).
250 * If we should start to use different types of cache entries tailored
251 * specifically for attrstat and fh's, we may save even more space.
253 * Also note that a cachetype of RC_NOCACHE can legally be passed when
254 * nfsd failed to encode a reply that otherwise would have been cached.
255 * In this case, nfsd_cache_update is called with statp == NULL.
258 nfsd_cache_update(struct svc_rqst
*rqstp
, int cachetype
, __be32
*statp
)
260 struct svc_cacherep
*rp
;
261 struct kvec
*resv
= &rqstp
->rq_res
.head
[0], *cachv
;
264 if (!(rp
= rqstp
->rq_cacherep
) || cache_disabled
)
267 len
= resv
->iov_len
- ((char*)statp
- (char*)resv
->iov_base
);
270 /* Don't cache excessive amounts of data and XDR failures */
271 if (!statp
|| len
> (256 >> 2)) {
272 rp
->c_state
= RC_UNUSED
;
279 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len
);
280 rp
->c_replstat
= *statp
;
283 cachv
= &rp
->c_replvec
;
284 cachv
->iov_base
= kmalloc(len
<< 2, GFP_KERNEL
);
285 if (!cachv
->iov_base
) {
286 spin_lock(&cache_lock
);
287 rp
->c_state
= RC_UNUSED
;
288 spin_unlock(&cache_lock
);
291 cachv
->iov_len
= len
<< 2;
292 memcpy(cachv
->iov_base
, statp
, len
<< 2);
295 spin_lock(&cache_lock
);
297 rp
->c_secure
= rqstp
->rq_secure
;
298 rp
->c_type
= cachetype
;
299 rp
->c_state
= RC_DONE
;
300 rp
->c_timestamp
= jiffies
;
301 spin_unlock(&cache_lock
);
306 * Copy cached reply to current reply buffer. Should always fit.
307 * FIXME as reply is in a page, we should just attach the page, and
308 * keep a refcount....
311 nfsd_cache_append(struct svc_rqst
*rqstp
, struct kvec
*data
)
313 struct kvec
*vec
= &rqstp
->rq_res
.head
[0];
315 if (vec
->iov_len
+ data
->iov_len
> PAGE_SIZE
) {
316 printk(KERN_WARNING
"nfsd: cached reply too large (%Zd).\n",
320 memcpy((char*)vec
->iov_base
+ vec
->iov_len
, data
->iov_base
, data
->iov_len
);
321 vec
->iov_len
+= data
->iov_len
;