2 * Request reply cache. This is currently a global cache, but this may
3 * change in the future and be a per-client cache.
5 * This code is heavily inspired by the 44BSD implementation, although
6 * it does things a bit differently.
8 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
14 /* Size of reply cache. Common values are:
20 #define CACHESIZE 1024
23 static struct hlist_head
* cache_hash
;
24 static struct list_head lru_head
;
25 static int cache_disabled
= 1;
28 * Calculate the hash index from an XID.
30 static inline u32
request_hash(u32 xid
)
34 return h
& (HASHSIZE
-1);
37 static int nfsd_cache_append(struct svc_rqst
*rqstp
, struct kvec
*vec
);
40 * locking for the reply cache:
41 * A cache entry is "single use" if c_state == RC_INPROG
42 * Otherwise, it when accessing _prev or _next, the lock must be held.
44 static DEFINE_SPINLOCK(cache_lock
);
46 int nfsd_reply_cache_init(void)
48 struct svc_cacherep
*rp
;
51 INIT_LIST_HEAD(&lru_head
);
54 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
57 list_add(&rp
->c_lru
, &lru_head
);
58 rp
->c_state
= RC_UNUSED
;
59 rp
->c_type
= RC_NOCACHE
;
60 INIT_HLIST_NODE(&rp
->c_hash
);
64 cache_hash
= kcalloc (HASHSIZE
, sizeof(struct hlist_head
), GFP_KERNEL
);
71 printk(KERN_ERR
"nfsd: failed to allocate reply cache\n");
72 nfsd_reply_cache_shutdown();
76 void nfsd_reply_cache_shutdown(void)
78 struct svc_cacherep
*rp
;
80 while (!list_empty(&lru_head
)) {
81 rp
= list_entry(lru_head
.next
, struct svc_cacherep
, c_lru
);
82 if (rp
->c_state
== RC_DONE
&& rp
->c_type
== RC_REPLBUFF
)
83 kfree(rp
->c_replvec
.iov_base
);
95 * Move cache entry to end of LRU list
98 lru_put_end(struct svc_cacherep
*rp
)
100 list_move_tail(&rp
->c_lru
, &lru_head
);
104 * Move a cache entry from one hash list to another
107 hash_refile(struct svc_cacherep
*rp
)
109 hlist_del_init(&rp
->c_hash
);
110 hlist_add_head(&rp
->c_hash
, cache_hash
+ request_hash(rp
->c_xid
));
114 * Try to find an entry matching the current call in the cache. When none
115 * is found, we grab the oldest unlocked entry off the LRU list.
116 * Note that no operation within the loop may sleep.
119 nfsd_cache_lookup(struct svc_rqst
*rqstp
, int type
)
121 struct hlist_node
*hn
;
122 struct hlist_head
*rh
;
123 struct svc_cacherep
*rp
;
124 __be32 xid
= rqstp
->rq_xid
;
125 u32 proto
= rqstp
->rq_prot
,
126 vers
= rqstp
->rq_vers
,
127 proc
= rqstp
->rq_proc
;
131 rqstp
->rq_cacherep
= NULL
;
132 if (cache_disabled
|| type
== RC_NOCACHE
) {
133 nfsdstats
.rcnocache
++;
137 spin_lock(&cache_lock
);
140 rh
= &cache_hash
[request_hash(xid
)];
141 hlist_for_each_entry(rp
, hn
, rh
, c_hash
) {
142 if (rp
->c_state
!= RC_UNUSED
&&
143 xid
== rp
->c_xid
&& proc
== rp
->c_proc
&&
144 proto
== rp
->c_prot
&& vers
== rp
->c_vers
&&
145 time_before(jiffies
, rp
->c_timestamp
+ 120*HZ
) &&
146 memcmp((char*)&rqstp
->rq_addr
, (char*)&rp
->c_addr
, sizeof(rp
->c_addr
))==0) {
151 nfsdstats
.rcmisses
++;
153 /* This loop shouldn't take more than a few iterations normally */
156 list_for_each_entry(rp
, &lru_head
, c_lru
) {
157 if (rp
->c_state
!= RC_INPROG
)
159 if (safe
++ > CACHESIZE
) {
160 printk("nfsd: loop in repcache LRU list\n");
167 /* All entries on the LRU are in-progress. This should not happen */
168 if (&rp
->c_lru
== &lru_head
) {
169 static int complaints
;
171 printk(KERN_WARNING
"nfsd: all repcache entries locked!\n");
172 if (++complaints
> 5) {
173 printk(KERN_WARNING
"nfsd: disabling repcache.\n");
179 rqstp
->rq_cacherep
= rp
;
180 rp
->c_state
= RC_INPROG
;
183 memcpy(&rp
->c_addr
, svc_addr_in(rqstp
), sizeof(rp
->c_addr
));
186 rp
->c_timestamp
= jiffies
;
190 /* release any buffer */
191 if (rp
->c_type
== RC_REPLBUFF
) {
192 kfree(rp
->c_replvec
.iov_base
);
193 rp
->c_replvec
.iov_base
= NULL
;
195 rp
->c_type
= RC_NOCACHE
;
197 spin_unlock(&cache_lock
);
201 /* We found a matching entry which is either in progress or done. */
202 age
= jiffies
- rp
->c_timestamp
;
203 rp
->c_timestamp
= jiffies
;
207 /* Request being processed or excessive rexmits */
208 if (rp
->c_state
== RC_INPROG
|| age
< RC_DELAY
)
211 /* From the hall of fame of impractical attacks:
212 * Is this a user who tries to snoop on the cache? */
214 if (!rqstp
->rq_secure
&& rp
->c_secure
)
217 /* Compose RPC reply header */
218 switch (rp
->c_type
) {
222 svc_putu32(&rqstp
->rq_res
.head
[0], rp
->c_replstat
);
226 if (!nfsd_cache_append(rqstp
, &rp
->c_replvec
))
227 goto out
; /* should not happen */
231 printk(KERN_WARNING
"nfsd: bad repcache type %d\n", rp
->c_type
);
232 rp
->c_state
= RC_UNUSED
;
239 * Update a cache entry. This is called from nfsd_dispatch when
240 * the procedure has been executed and the complete reply is in
243 * We're copying around data here rather than swapping buffers because
244 * the toplevel loop requires max-sized buffers, which would be a waste
245 * of memory for a cache with a max reply size of 100 bytes (diropokres).
247 * If we should start to use different types of cache entries tailored
248 * specifically for attrstat and fh's, we may save even more space.
250 * Also note that a cachetype of RC_NOCACHE can legally be passed when
251 * nfsd failed to encode a reply that otherwise would have been cached.
252 * In this case, nfsd_cache_update is called with statp == NULL.
255 nfsd_cache_update(struct svc_rqst
*rqstp
, int cachetype
, __be32
*statp
)
257 struct svc_cacherep
*rp
;
258 struct kvec
*resv
= &rqstp
->rq_res
.head
[0], *cachv
;
261 if (!(rp
= rqstp
->rq_cacherep
) || cache_disabled
)
264 len
= resv
->iov_len
- ((char*)statp
- (char*)resv
->iov_base
);
267 /* Don't cache excessive amounts of data and XDR failures */
268 if (!statp
|| len
> (256 >> 2)) {
269 rp
->c_state
= RC_UNUSED
;
276 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len
);
277 rp
->c_replstat
= *statp
;
280 cachv
= &rp
->c_replvec
;
281 cachv
->iov_base
= kmalloc(len
<< 2, GFP_KERNEL
);
282 if (!cachv
->iov_base
) {
283 spin_lock(&cache_lock
);
284 rp
->c_state
= RC_UNUSED
;
285 spin_unlock(&cache_lock
);
288 cachv
->iov_len
= len
<< 2;
289 memcpy(cachv
->iov_base
, statp
, len
<< 2);
292 spin_lock(&cache_lock
);
294 rp
->c_secure
= rqstp
->rq_secure
;
295 rp
->c_type
= cachetype
;
296 rp
->c_state
= RC_DONE
;
297 rp
->c_timestamp
= jiffies
;
298 spin_unlock(&cache_lock
);
303 * Copy cached reply to current reply buffer. Should always fit.
304 * FIXME as reply is in a page, we should just attach the page, and
305 * keep a refcount....
308 nfsd_cache_append(struct svc_rqst
*rqstp
, struct kvec
*data
)
310 struct kvec
*vec
= &rqstp
->rq_res
.head
[0];
312 if (vec
->iov_len
+ data
->iov_len
> PAGE_SIZE
) {
313 printk(KERN_WARNING
"nfsd: cached reply too large (%Zd).\n",
317 memcpy((char*)vec
->iov_base
+ vec
->iov_len
, data
->iov_base
, data
->iov_len
);
318 vec
->iov_len
+= data
->iov_len
;