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>
11 #include <linux/slab.h>
12 #include <linux/sunrpc/addr.h>
13 #include <linux/highmem.h>
18 #define NFSDDBG_FACILITY NFSDDBG_REPCACHE
22 static struct hlist_head
* cache_hash
;
23 static struct list_head lru_head
;
24 static struct kmem_cache
*drc_slab
;
25 static unsigned int num_drc_entries
;
26 static unsigned int max_drc_entries
;
29 * Calculate the hash index from an XID.
31 static inline u32
request_hash(u32 xid
)
35 return h
& (HASHSIZE
-1);
38 static int nfsd_cache_append(struct svc_rqst
*rqstp
, struct kvec
*vec
);
39 static void cache_cleaner_func(struct work_struct
*unused
);
40 static int nfsd_reply_cache_shrink(struct shrinker
*shrink
,
41 struct shrink_control
*sc
);
43 struct shrinker nfsd_reply_cache_shrinker
= {
44 .shrink
= nfsd_reply_cache_shrink
,
49 * locking for the reply cache:
50 * A cache entry is "single use" if c_state == RC_INPROG
51 * Otherwise, it when accessing _prev or _next, the lock must be held.
53 static DEFINE_SPINLOCK(cache_lock
);
54 static DECLARE_DELAYED_WORK(cache_cleaner
, cache_cleaner_func
);
57 * Put a cap on the size of the DRC based on the amount of available
58 * low memory in the machine.
70 * ...with a hard cap of 256k entries. In the worst case, each entry will be
71 * ~1k, so the above numbers should give a rough max of the amount of memory
75 nfsd_cache_size_limit(void)
78 unsigned long low_pages
= totalram_pages
- totalhigh_pages
;
80 limit
= (16 * int_sqrt(low_pages
)) << (PAGE_SHIFT
-10);
81 return min_t(unsigned int, limit
, 256*1024);
84 static struct svc_cacherep
*
85 nfsd_reply_cache_alloc(void)
87 struct svc_cacherep
*rp
;
89 rp
= kmem_cache_alloc(drc_slab
, GFP_KERNEL
);
91 rp
->c_state
= RC_UNUSED
;
92 rp
->c_type
= RC_NOCACHE
;
93 INIT_LIST_HEAD(&rp
->c_lru
);
94 INIT_HLIST_NODE(&rp
->c_hash
);
100 nfsd_reply_cache_free_locked(struct svc_cacherep
*rp
)
102 if (rp
->c_type
== RC_REPLBUFF
)
103 kfree(rp
->c_replvec
.iov_base
);
104 hlist_del(&rp
->c_hash
);
105 list_del(&rp
->c_lru
);
107 kmem_cache_free(drc_slab
, rp
);
111 nfsd_reply_cache_free(struct svc_cacherep
*rp
)
113 spin_lock(&cache_lock
);
114 nfsd_reply_cache_free_locked(rp
);
115 spin_unlock(&cache_lock
);
118 int nfsd_reply_cache_init(void)
120 register_shrinker(&nfsd_reply_cache_shrinker
);
121 drc_slab
= kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep
),
126 cache_hash
= kcalloc(HASHSIZE
, sizeof(struct hlist_head
), GFP_KERNEL
);
130 INIT_LIST_HEAD(&lru_head
);
131 max_drc_entries
= nfsd_cache_size_limit();
135 printk(KERN_ERR
"nfsd: failed to allocate reply cache\n");
136 nfsd_reply_cache_shutdown();
140 void nfsd_reply_cache_shutdown(void)
142 struct svc_cacherep
*rp
;
144 unregister_shrinker(&nfsd_reply_cache_shrinker
);
145 cancel_delayed_work_sync(&cache_cleaner
);
147 while (!list_empty(&lru_head
)) {
148 rp
= list_entry(lru_head
.next
, struct svc_cacherep
, c_lru
);
149 nfsd_reply_cache_free_locked(rp
);
156 kmem_cache_destroy(drc_slab
);
162 * Move cache entry to end of LRU list, and queue the cleaner to run if it's
163 * not already scheduled.
166 lru_put_end(struct svc_cacherep
*rp
)
168 rp
->c_timestamp
= jiffies
;
169 list_move_tail(&rp
->c_lru
, &lru_head
);
170 schedule_delayed_work(&cache_cleaner
, RC_EXPIRE
);
174 * Move a cache entry from one hash list to another
177 hash_refile(struct svc_cacherep
*rp
)
179 hlist_del_init(&rp
->c_hash
);
180 hlist_add_head(&rp
->c_hash
, cache_hash
+ request_hash(rp
->c_xid
));
184 nfsd_cache_entry_expired(struct svc_cacherep
*rp
)
186 return rp
->c_state
!= RC_INPROG
&&
187 time_after(jiffies
, rp
->c_timestamp
+ RC_EXPIRE
);
191 * Walk the LRU list and prune off entries that are older than RC_EXPIRE.
192 * Also prune the oldest ones when the total exceeds the max number of entries.
195 prune_cache_entries(void)
197 struct svc_cacherep
*rp
, *tmp
;
199 list_for_each_entry_safe(rp
, tmp
, &lru_head
, c_lru
) {
200 if (!nfsd_cache_entry_expired(rp
) &&
201 num_drc_entries
<= max_drc_entries
)
203 nfsd_reply_cache_free_locked(rp
);
207 * Conditionally rearm the job. If we cleaned out the list, then
208 * cancel any pending run (since there won't be any work to do).
209 * Otherwise, we rearm the job or modify the existing one to run in
210 * RC_EXPIRE since we just ran the pruner.
212 if (list_empty(&lru_head
))
213 cancel_delayed_work(&cache_cleaner
);
215 mod_delayed_work(system_wq
, &cache_cleaner
, RC_EXPIRE
);
219 cache_cleaner_func(struct work_struct
*unused
)
221 spin_lock(&cache_lock
);
222 prune_cache_entries();
223 spin_unlock(&cache_lock
);
227 nfsd_reply_cache_shrink(struct shrinker
*shrink
, struct shrink_control
*sc
)
231 spin_lock(&cache_lock
);
233 prune_cache_entries();
234 num
= num_drc_entries
;
235 spin_unlock(&cache_lock
);
241 * Search the request hash for an entry that matches the given rqstp.
242 * Must be called with cache_lock held. Returns the found entry or
245 static struct svc_cacherep
*
246 nfsd_cache_search(struct svc_rqst
*rqstp
)
248 struct svc_cacherep
*rp
;
249 struct hlist_node
*hn
;
250 struct hlist_head
*rh
;
251 __be32 xid
= rqstp
->rq_xid
;
252 u32 proto
= rqstp
->rq_prot
,
253 vers
= rqstp
->rq_vers
,
254 proc
= rqstp
->rq_proc
;
256 rh
= &cache_hash
[request_hash(xid
)];
257 hlist_for_each_entry(rp
, hn
, rh
, c_hash
) {
258 if (xid
== rp
->c_xid
&& proc
== rp
->c_proc
&&
259 proto
== rp
->c_prot
&& vers
== rp
->c_vers
&&
260 rpc_cmp_addr(svc_addr(rqstp
), (struct sockaddr
*)&rp
->c_addr
) &&
261 rpc_get_port(svc_addr(rqstp
)) == rpc_get_port((struct sockaddr
*)&rp
->c_addr
))
268 * Try to find an entry matching the current call in the cache. When none
269 * is found, we grab the oldest unlocked entry off the LRU list.
270 * Note that no operation within the loop may sleep.
273 nfsd_cache_lookup(struct svc_rqst
*rqstp
)
275 struct svc_cacherep
*rp
, *found
;
276 __be32 xid
= rqstp
->rq_xid
;
277 u32 proto
= rqstp
->rq_prot
,
278 vers
= rqstp
->rq_vers
,
279 proc
= rqstp
->rq_proc
;
281 int type
= rqstp
->rq_cachetype
;
284 rqstp
->rq_cacherep
= NULL
;
285 if (type
== RC_NOCACHE
) {
286 nfsdstats
.rcnocache
++;
290 spin_lock(&cache_lock
);
293 rp
= nfsd_cache_search(rqstp
);
297 /* Try to use the first entry on the LRU */
298 if (!list_empty(&lru_head
)) {
299 rp
= list_first_entry(&lru_head
, struct svc_cacherep
, c_lru
);
300 if (nfsd_cache_entry_expired(rp
) ||
301 num_drc_entries
>= max_drc_entries
) {
303 prune_cache_entries();
308 spin_unlock(&cache_lock
);
309 rp
= nfsd_reply_cache_alloc();
311 dprintk("nfsd: unable to allocate DRC entry!\n");
314 spin_lock(&cache_lock
);
318 * Must search again just in case someone inserted one
319 * after we dropped the lock above.
321 found
= nfsd_cache_search(rqstp
);
323 nfsd_reply_cache_free_locked(rp
);
329 * We're keeping the one we just allocated. Are we now over the
330 * limit? Prune one off the tip of the LRU in trade for the one we
331 * just allocated if so.
333 if (num_drc_entries
>= max_drc_entries
)
334 nfsd_reply_cache_free_locked(list_first_entry(&lru_head
,
335 struct svc_cacherep
, c_lru
));
338 nfsdstats
.rcmisses
++;
339 rqstp
->rq_cacherep
= rp
;
340 rp
->c_state
= RC_INPROG
;
343 rpc_copy_addr((struct sockaddr
*)&rp
->c_addr
, svc_addr(rqstp
));
344 rpc_set_port((struct sockaddr
*)&rp
->c_addr
, rpc_get_port(svc_addr(rqstp
)));
351 /* release any buffer */
352 if (rp
->c_type
== RC_REPLBUFF
) {
353 kfree(rp
->c_replvec
.iov_base
);
354 rp
->c_replvec
.iov_base
= NULL
;
356 rp
->c_type
= RC_NOCACHE
;
358 spin_unlock(&cache_lock
);
363 /* We found a matching entry which is either in progress or done. */
364 age
= jiffies
- rp
->c_timestamp
;
368 /* Request being processed or excessive rexmits */
369 if (rp
->c_state
== RC_INPROG
|| age
< RC_DELAY
)
372 /* From the hall of fame of impractical attacks:
373 * Is this a user who tries to snoop on the cache? */
375 if (!rqstp
->rq_secure
&& rp
->c_secure
)
378 /* Compose RPC reply header */
379 switch (rp
->c_type
) {
383 svc_putu32(&rqstp
->rq_res
.head
[0], rp
->c_replstat
);
387 if (!nfsd_cache_append(rqstp
, &rp
->c_replvec
))
388 goto out
; /* should not happen */
392 printk(KERN_WARNING
"nfsd: bad repcache type %d\n", rp
->c_type
);
393 nfsd_reply_cache_free_locked(rp
);
400 * Update a cache entry. This is called from nfsd_dispatch when
401 * the procedure has been executed and the complete reply is in
404 * We're copying around data here rather than swapping buffers because
405 * the toplevel loop requires max-sized buffers, which would be a waste
406 * of memory for a cache with a max reply size of 100 bytes (diropokres).
408 * If we should start to use different types of cache entries tailored
409 * specifically for attrstat and fh's, we may save even more space.
411 * Also note that a cachetype of RC_NOCACHE can legally be passed when
412 * nfsd failed to encode a reply that otherwise would have been cached.
413 * In this case, nfsd_cache_update is called with statp == NULL.
416 nfsd_cache_update(struct svc_rqst
*rqstp
, int cachetype
, __be32
*statp
)
418 struct svc_cacherep
*rp
= rqstp
->rq_cacherep
;
419 struct kvec
*resv
= &rqstp
->rq_res
.head
[0], *cachv
;
425 len
= resv
->iov_len
- ((char*)statp
- (char*)resv
->iov_base
);
428 /* Don't cache excessive amounts of data and XDR failures */
429 if (!statp
|| len
> (256 >> 2)) {
430 nfsd_reply_cache_free(rp
);
437 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len
);
438 rp
->c_replstat
= *statp
;
441 cachv
= &rp
->c_replvec
;
442 cachv
->iov_base
= kmalloc(len
<< 2, GFP_KERNEL
);
443 if (!cachv
->iov_base
) {
444 nfsd_reply_cache_free(rp
);
447 cachv
->iov_len
= len
<< 2;
448 memcpy(cachv
->iov_base
, statp
, len
<< 2);
451 nfsd_reply_cache_free(rp
);
454 spin_lock(&cache_lock
);
456 rp
->c_secure
= rqstp
->rq_secure
;
457 rp
->c_type
= cachetype
;
458 rp
->c_state
= RC_DONE
;
459 spin_unlock(&cache_lock
);
464 * Copy cached reply to current reply buffer. Should always fit.
465 * FIXME as reply is in a page, we should just attach the page, and
466 * keep a refcount....
469 nfsd_cache_append(struct svc_rqst
*rqstp
, struct kvec
*data
)
471 struct kvec
*vec
= &rqstp
->rq_res
.head
[0];
473 if (vec
->iov_len
+ data
->iov_len
> PAGE_SIZE
) {
474 printk(KERN_WARNING
"nfsd: cached reply too large (%Zd).\n",
478 memcpy((char*)vec
->iov_base
+ vec
->iov_len
, data
->iov_base
, data
->iov_len
);
479 vec
->iov_len
+= data
->iov_len
;