blob: fd81ca79a00205a8bfc91b62257bfacf0ee9cc33 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Request reply cache. This is currently a global cache, but this may
3 * change in the future and be a per-client cache.
4 *
5 * This code is heavily inspired by the 44BSD implementation, although
6 * it does things a bit differently.
7 *
8 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 */
10
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Jeff Layton59766872013-02-04 12:50:00 -050012#include <linux/sunrpc/addr.h>
Jeff Layton0338dd12013-02-04 08:18:02 -050013#include <linux/highmem.h>
Jeff Layton01a7dec2013-02-04 11:57:27 -050014#include <net/checksum.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015
Boaz Harrosh9a74af22009-12-03 20:30:56 +020016#include "nfsd.h"
17#include "cache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Jeff Layton0338dd12013-02-04 08:18:02 -050019#define NFSDDBG_FACILITY NFSDDBG_REPCACHE
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define HASHSIZE 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Greg Banksfca42172009-04-01 07:28:13 +110023static struct hlist_head * cache_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static struct list_head lru_head;
Jeff Layton8a8bc402013-01-28 14:41:10 -050025static struct kmem_cache *drc_slab;
Jeff Layton9dc56142013-03-27 10:15:37 -040026
27/* max number of entries allowed in the cache */
Jeff Layton0338dd12013-02-04 08:18:02 -050028static unsigned int max_drc_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Greg Banksfca42172009-04-01 07:28:13 +110030/*
Jeff Layton9dc56142013-03-27 10:15:37 -040031 * Stats and other tracking of on the duplicate reply cache. All of these and
32 * the "rc" fields in nfsdstats are protected by the cache_lock
33 */
34
35/* total number of entries */
36static unsigned int num_drc_entries;
37
38/* cache misses due only to checksum comparison failures */
39static unsigned int payload_misses;
40
Jeff Layton6c6910c2013-03-27 10:15:38 -040041/* amount of memory (in bytes) currently consumed by the DRC */
42static unsigned int drc_mem_usage;
43
Jeff Layton9dc56142013-03-27 10:15:37 -040044/*
Greg Banksfca42172009-04-01 07:28:13 +110045 * Calculate the hash index from an XID.
46 */
47static inline u32 request_hash(u32 xid)
48{
49 u32 h = xid;
50 h ^= (xid >> 24);
51 return h & (HASHSIZE-1);
52}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
Jeff Laytonaca8a232013-02-04 08:18:05 -050055static void cache_cleaner_func(struct work_struct *unused);
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -050056static int nfsd_reply_cache_shrink(struct shrinker *shrink,
57 struct shrink_control *sc);
58
59struct shrinker nfsd_reply_cache_shrinker = {
60 .shrink = nfsd_reply_cache_shrink,
61 .seeks = 1,
62};
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Greg Banksfca42172009-04-01 07:28:13 +110064/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * locking for the reply cache:
66 * A cache entry is "single use" if c_state == RC_INPROG
67 * Otherwise, it when accessing _prev or _next, the lock must be held.
68 */
69static DEFINE_SPINLOCK(cache_lock);
Jeff Laytonaca8a232013-02-04 08:18:05 -050070static DECLARE_DELAYED_WORK(cache_cleaner, cache_cleaner_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jeff Layton0338dd12013-02-04 08:18:02 -050072/*
73 * Put a cap on the size of the DRC based on the amount of available
74 * low memory in the machine.
75 *
76 * 64MB: 8192
77 * 128MB: 11585
78 * 256MB: 16384
79 * 512MB: 23170
80 * 1GB: 32768
81 * 2GB: 46340
82 * 4GB: 65536
83 * 8GB: 92681
84 * 16GB: 131072
85 *
86 * ...with a hard cap of 256k entries. In the worst case, each entry will be
87 * ~1k, so the above numbers should give a rough max of the amount of memory
88 * used in k.
89 */
90static unsigned int
91nfsd_cache_size_limit(void)
92{
93 unsigned int limit;
94 unsigned long low_pages = totalram_pages - totalhigh_pages;
95
96 limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
97 return min_t(unsigned int, limit, 256*1024);
98}
99
Jeff Laytonf09841f2013-01-28 14:41:11 -0500100static struct svc_cacherep *
101nfsd_reply_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct svc_cacherep *rp;
Jeff Laytonf09841f2013-01-28 14:41:11 -0500104
105 rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
106 if (rp) {
107 rp->c_state = RC_UNUSED;
108 rp->c_type = RC_NOCACHE;
109 INIT_LIST_HEAD(&rp->c_lru);
110 INIT_HLIST_NODE(&rp->c_hash);
111 }
112 return rp;
113}
114
115static void
116nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
117{
Jeff Layton6c6910c2013-03-27 10:15:38 -0400118 if (rp->c_type == RC_REPLBUFF && rp->c_replvec.iov_base) {
119 drc_mem_usage -= rp->c_replvec.iov_len;
Jeff Laytonf09841f2013-01-28 14:41:11 -0500120 kfree(rp->c_replvec.iov_base);
Jeff Layton6c6910c2013-03-27 10:15:38 -0400121 }
Jeff Laytona517b602013-03-18 10:49:07 -0400122 if (!hlist_unhashed(&rp->c_hash))
123 hlist_del(&rp->c_hash);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500124 list_del(&rp->c_lru);
Jeff Layton0ee0bf72013-02-04 08:18:01 -0500125 --num_drc_entries;
Jeff Layton6c6910c2013-03-27 10:15:38 -0400126 drc_mem_usage -= sizeof(*rp);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500127 kmem_cache_free(drc_slab, rp);
128}
129
Jeff Layton2c6b6912013-02-04 08:18:04 -0500130static void
131nfsd_reply_cache_free(struct svc_cacherep *rp)
132{
133 spin_lock(&cache_lock);
134 nfsd_reply_cache_free_locked(rp);
135 spin_unlock(&cache_lock);
136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138int nfsd_reply_cache_init(void)
139{
Jeff Laytonac534ff2013-03-15 09:16:29 -0400140 INIT_LIST_HEAD(&lru_head);
141 max_drc_entries = nfsd_cache_size_limit();
142 num_drc_entries = 0;
143
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500144 register_shrinker(&nfsd_reply_cache_shrinker);
Jeff Layton8a8bc402013-01-28 14:41:10 -0500145 drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
146 0, 0, NULL);
147 if (!drc_slab)
148 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Jeff Layton0338dd12013-02-04 08:18:02 -0500150 cache_hash = kcalloc(HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
Greg Banksfca42172009-04-01 07:28:13 +1100151 if (!cache_hash)
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500152 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500154 return 0;
155out_nomem:
156 printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
157 nfsd_reply_cache_shutdown();
158 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500161void nfsd_reply_cache_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct svc_cacherep *rp;
164
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500165 unregister_shrinker(&nfsd_reply_cache_shrinker);
Jeff Laytonaca8a232013-02-04 08:18:05 -0500166 cancel_delayed_work_sync(&cache_cleaner);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 while (!list_empty(&lru_head)) {
169 rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500170 nfsd_reply_cache_free_locked(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
Greg Banksfca42172009-04-01 07:28:13 +1100173 kfree (cache_hash);
174 cache_hash = NULL;
Jeff Layton8a8bc402013-01-28 14:41:10 -0500175
176 if (drc_slab) {
177 kmem_cache_destroy(drc_slab);
178 drc_slab = NULL;
179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
Jeff Laytonaca8a232013-02-04 08:18:05 -0500183 * Move cache entry to end of LRU list, and queue the cleaner to run if it's
184 * not already scheduled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
186static void
187lru_put_end(struct svc_cacherep *rp)
188{
Jeff Layton56c25482013-02-04 08:18:00 -0500189 rp->c_timestamp = jiffies;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700190 list_move_tail(&rp->c_lru, &lru_head);
Jeff Laytonaca8a232013-02-04 08:18:05 -0500191 schedule_delayed_work(&cache_cleaner, RC_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
195 * Move a cache entry from one hash list to another
196 */
197static void
198hash_refile(struct svc_cacherep *rp)
199{
200 hlist_del_init(&rp->c_hash);
Greg Banksfca42172009-04-01 07:28:13 +1100201 hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Jeff Laytond1a07742013-01-28 14:41:13 -0500204static inline bool
205nfsd_cache_entry_expired(struct svc_cacherep *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Jeff Laytond1a07742013-01-28 14:41:13 -0500207 return rp->c_state != RC_INPROG &&
208 time_after(jiffies, rp->c_timestamp + RC_EXPIRE);
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
Jeff Laytonaca8a232013-02-04 08:18:05 -0500212 * Walk the LRU list and prune off entries that are older than RC_EXPIRE.
213 * Also prune the oldest ones when the total exceeds the max number of entries.
214 */
215static void
216prune_cache_entries(void)
217{
218 struct svc_cacherep *rp, *tmp;
219
220 list_for_each_entry_safe(rp, tmp, &lru_head, c_lru) {
221 if (!nfsd_cache_entry_expired(rp) &&
222 num_drc_entries <= max_drc_entries)
223 break;
224 nfsd_reply_cache_free_locked(rp);
225 }
226
227 /*
228 * Conditionally rearm the job. If we cleaned out the list, then
229 * cancel any pending run (since there won't be any work to do).
230 * Otherwise, we rearm the job or modify the existing one to run in
231 * RC_EXPIRE since we just ran the pruner.
232 */
233 if (list_empty(&lru_head))
234 cancel_delayed_work(&cache_cleaner);
235 else
236 mod_delayed_work(system_wq, &cache_cleaner, RC_EXPIRE);
237}
238
239static void
240cache_cleaner_func(struct work_struct *unused)
241{
242 spin_lock(&cache_lock);
243 prune_cache_entries();
244 spin_unlock(&cache_lock);
245}
246
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500247static int
248nfsd_reply_cache_shrink(struct shrinker *shrink, struct shrink_control *sc)
249{
250 unsigned int num;
251
252 spin_lock(&cache_lock);
253 if (sc->nr_to_scan)
254 prune_cache_entries();
255 num = num_drc_entries;
256 spin_unlock(&cache_lock);
257
258 return num;
259}
260
Jeff Laytonaca8a232013-02-04 08:18:05 -0500261/*
Jeff Layton01a7dec2013-02-04 11:57:27 -0500262 * Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes
263 */
264static __wsum
265nfsd_cache_csum(struct svc_rqst *rqstp)
266{
267 int idx;
268 unsigned int base;
269 __wsum csum;
270 struct xdr_buf *buf = &rqstp->rq_arg;
271 const unsigned char *p = buf->head[0].iov_base;
272 size_t csum_len = min_t(size_t, buf->head[0].iov_len + buf->page_len,
273 RC_CSUMLEN);
274 size_t len = min(buf->head[0].iov_len, csum_len);
275
276 /* rq_arg.head first */
277 csum = csum_partial(p, len, 0);
278 csum_len -= len;
279
280 /* Continue into page array */
281 idx = buf->page_base / PAGE_SIZE;
282 base = buf->page_base & ~PAGE_MASK;
283 while (csum_len) {
284 p = page_address(buf->pages[idx]) + base;
Jeff Layton56edc862013-02-15 13:36:34 -0500285 len = min_t(size_t, PAGE_SIZE - base, csum_len);
Jeff Layton01a7dec2013-02-04 11:57:27 -0500286 csum = csum_partial(p, len, csum);
287 csum_len -= len;
288 base = 0;
289 ++idx;
290 }
291 return csum;
292}
293
Jeff Layton9dc56142013-03-27 10:15:37 -0400294static bool
295nfsd_cache_match(struct svc_rqst *rqstp, __wsum csum, struct svc_cacherep *rp)
296{
297 /* Check RPC header info first */
298 if (rqstp->rq_xid != rp->c_xid || rqstp->rq_proc != rp->c_proc ||
299 rqstp->rq_prot != rp->c_prot || rqstp->rq_vers != rp->c_vers ||
300 rqstp->rq_arg.len != rp->c_len ||
301 !rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) ||
302 rpc_get_port(svc_addr(rqstp)) != rpc_get_port((struct sockaddr *)&rp->c_addr))
303 return false;
304
305 /* compare checksum of NFS data */
306 if (csum != rp->c_csum) {
307 ++payload_misses;
308 return false;
309 }
310
311 return true;
312}
313
Jeff Layton01a7dec2013-02-04 11:57:27 -0500314/*
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500315 * Search the request hash for an entry that matches the given rqstp.
316 * Must be called with cache_lock held. Returns the found entry or
317 * NULL on failure.
318 */
319static struct svc_cacherep *
Jeff Layton01a7dec2013-02-04 11:57:27 -0500320nfsd_cache_search(struct svc_rqst *rqstp, __wsum csum)
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500321{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct svc_cacherep *rp;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500323 struct hlist_head *rh;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500324
Jeff Layton9dc56142013-03-27 10:15:37 -0400325 rh = &cache_hash[request_hash(rqstp->rq_xid)];
Linus Torvaldsb6669732013-02-28 18:02:55 -0800326 hlist_for_each_entry(rp, rh, c_hash) {
Jeff Layton9dc56142013-03-27 10:15:37 -0400327 if (nfsd_cache_match(rqstp, csum, rp))
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500328 return rp;
329 }
330 return NULL;
331}
332
333/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * Try to find an entry matching the current call in the cache. When none
Jeff Layton1ac83622013-02-14 16:45:13 -0500335 * is found, we try to grab the oldest expired entry off the LRU list. If
336 * a suitable one isn't there, then drop the cache_lock and allocate a
337 * new one, then search again in case one got inserted while this thread
338 * didn't hold the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
340int
341nfsd_cache_lookup(struct svc_rqst *rqstp)
342{
Jeff Layton0338dd12013-02-04 08:18:02 -0500343 struct svc_cacherep *rp, *found;
Al Viroc7afef12006-10-19 23:29:02 -0700344 __be32 xid = rqstp->rq_xid;
345 u32 proto = rqstp->rq_prot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 vers = rqstp->rq_vers,
347 proc = rqstp->rq_proc;
Jeff Layton01a7dec2013-02-04 11:57:27 -0500348 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unsigned long age;
J. Bruce Fields10910062011-01-24 12:11:02 -0500350 int type = rqstp->rq_cachetype;
Jeff Layton0b9ea372013-03-27 10:15:37 -0400351 int rtn = RC_DOIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 rqstp->rq_cacherep = NULL;
Jeff Layton13cc8a72013-02-04 08:18:03 -0500354 if (type == RC_NOCACHE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 nfsdstats.rcnocache++;
Jeff Layton0b9ea372013-03-27 10:15:37 -0400356 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
Jeff Layton01a7dec2013-02-04 11:57:27 -0500359 csum = nfsd_cache_csum(rqstp);
360
Jeff Layton0b9ea372013-03-27 10:15:37 -0400361 /*
362 * Since the common case is a cache miss followed by an insert,
363 * preallocate an entry. First, try to reuse the first entry on the LRU
364 * if it works, then go ahead and prune the LRU list.
365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 spin_lock(&cache_lock);
Jeff Layton0338dd12013-02-04 08:18:02 -0500367 if (!list_empty(&lru_head)) {
368 rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
369 if (nfsd_cache_entry_expired(rp) ||
Jeff Laytonaca8a232013-02-04 08:18:05 -0500370 num_drc_entries >= max_drc_entries) {
371 lru_put_end(rp);
372 prune_cache_entries();
Jeff Layton0b9ea372013-03-27 10:15:37 -0400373 goto search_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 }
Jeff Layton0338dd12013-02-04 08:18:02 -0500376
Jeff Layton0b9ea372013-03-27 10:15:37 -0400377 /* No expired ones available, allocate a new one. */
Jeff Layton0338dd12013-02-04 08:18:02 -0500378 spin_unlock(&cache_lock);
379 rp = nfsd_reply_cache_alloc();
Jeff Layton0338dd12013-02-04 08:18:02 -0500380 spin_lock(&cache_lock);
Jeff Layton6c6910c2013-03-27 10:15:38 -0400381 if (likely(rp)) {
Jeff Layton0b9ea372013-03-27 10:15:37 -0400382 ++num_drc_entries;
Jeff Layton6c6910c2013-03-27 10:15:38 -0400383 drc_mem_usage += sizeof(*rp);
384 }
Jeff Layton0338dd12013-02-04 08:18:02 -0500385
Jeff Layton0b9ea372013-03-27 10:15:37 -0400386search_cache:
Jeff Layton01a7dec2013-02-04 11:57:27 -0500387 found = nfsd_cache_search(rqstp, csum);
Jeff Layton0338dd12013-02-04 08:18:02 -0500388 if (found) {
Jeff Layton0b9ea372013-03-27 10:15:37 -0400389 if (likely(rp))
390 nfsd_reply_cache_free_locked(rp);
Jeff Layton0338dd12013-02-04 08:18:02 -0500391 rp = found;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500392 goto found_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Jeff Layton0338dd12013-02-04 08:18:02 -0500394
Jeff Layton0b9ea372013-03-27 10:15:37 -0400395 if (!rp) {
396 dprintk("nfsd: unable to allocate DRC entry!\n");
397 goto out;
398 }
399
Jeff Layton0338dd12013-02-04 08:18:02 -0500400 /*
401 * We're keeping the one we just allocated. Are we now over the
402 * limit? Prune one off the tip of the LRU in trade for the one we
403 * just allocated if so.
404 */
405 if (num_drc_entries >= max_drc_entries)
406 nfsd_reply_cache_free_locked(list_first_entry(&lru_head,
407 struct svc_cacherep, c_lru));
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 nfsdstats.rcmisses++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 rqstp->rq_cacherep = rp;
411 rp->c_state = RC_INPROG;
412 rp->c_xid = xid;
413 rp->c_proc = proc;
Jeff Layton7b9e8522013-01-28 14:41:07 -0500414 rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
415 rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 rp->c_prot = proto;
417 rp->c_vers = vers;
Jeff Layton01a7dec2013-02-04 11:57:27 -0500418 rp->c_len = rqstp->rq_arg.len;
419 rp->c_csum = csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 hash_refile(rp);
Jeff Layton56c25482013-02-04 08:18:00 -0500422 lru_put_end(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /* release any buffer */
425 if (rp->c_type == RC_REPLBUFF) {
Jeff Layton6c6910c2013-03-27 10:15:38 -0400426 drc_mem_usage -= rp->c_replvec.iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 kfree(rp->c_replvec.iov_base);
428 rp->c_replvec.iov_base = NULL;
429 }
430 rp->c_type = RC_NOCACHE;
431 out:
432 spin_unlock(&cache_lock);
433 return rtn;
434
435found_entry:
Jeff Layton0338dd12013-02-04 08:18:02 -0500436 nfsdstats.rchits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* We found a matching entry which is either in progress or done. */
438 age = jiffies - rp->c_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 lru_put_end(rp);
440
441 rtn = RC_DROPIT;
442 /* Request being processed or excessive rexmits */
443 if (rp->c_state == RC_INPROG || age < RC_DELAY)
444 goto out;
445
446 /* From the hall of fame of impractical attacks:
447 * Is this a user who tries to snoop on the cache? */
448 rtn = RC_DOIT;
449 if (!rqstp->rq_secure && rp->c_secure)
450 goto out;
451
452 /* Compose RPC reply header */
453 switch (rp->c_type) {
454 case RC_NOCACHE:
455 break;
456 case RC_REPLSTAT:
457 svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
458 rtn = RC_REPLY;
459 break;
460 case RC_REPLBUFF:
461 if (!nfsd_cache_append(rqstp, &rp->c_replvec))
462 goto out; /* should not happen */
463 rtn = RC_REPLY;
464 break;
465 default:
466 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
Jeff Layton0338dd12013-02-04 08:18:02 -0500467 nfsd_reply_cache_free_locked(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 goto out;
471}
472
473/*
474 * Update a cache entry. This is called from nfsd_dispatch when
475 * the procedure has been executed and the complete reply is in
476 * rqstp->rq_res.
477 *
478 * We're copying around data here rather than swapping buffers because
479 * the toplevel loop requires max-sized buffers, which would be a waste
480 * of memory for a cache with a max reply size of 100 bytes (diropokres).
481 *
482 * If we should start to use different types of cache entries tailored
483 * specifically for attrstat and fh's, we may save even more space.
484 *
485 * Also note that a cachetype of RC_NOCACHE can legally be passed when
486 * nfsd failed to encode a reply that otherwise would have been cached.
487 * In this case, nfsd_cache_update is called with statp == NULL.
488 */
489void
Al Viroc7afef12006-10-19 23:29:02 -0700490nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Jeff Layton13cc8a72013-02-04 08:18:03 -0500492 struct svc_cacherep *rp = rqstp->rq_cacherep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
494 int len;
Jeff Layton6c6910c2013-03-27 10:15:38 -0400495 size_t bufsize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Jeff Layton13cc8a72013-02-04 08:18:03 -0500497 if (!rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return;
499
500 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
501 len >>= 2;
Greg Banksfca42172009-04-01 07:28:13 +1100502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* Don't cache excessive amounts of data and XDR failures */
504 if (!statp || len > (256 >> 2)) {
Jeff Layton2c6b6912013-02-04 08:18:04 -0500505 nfsd_reply_cache_free(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return;
507 }
508
509 switch (cachetype) {
510 case RC_REPLSTAT:
511 if (len != 1)
512 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
513 rp->c_replstat = *statp;
514 break;
515 case RC_REPLBUFF:
516 cachv = &rp->c_replvec;
Jeff Layton6c6910c2013-03-27 10:15:38 -0400517 bufsize = len << 2;
518 cachv->iov_base = kmalloc(bufsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (!cachv->iov_base) {
Jeff Layton2c6b6912013-02-04 08:18:04 -0500520 nfsd_reply_cache_free(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return;
522 }
Jeff Layton6c6910c2013-03-27 10:15:38 -0400523 cachv->iov_len = bufsize;
524 memcpy(cachv->iov_base, statp, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 break;
Jeff Layton2c6b6912013-02-04 08:18:04 -0500526 case RC_NOCACHE:
527 nfsd_reply_cache_free(rp);
528 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 spin_lock(&cache_lock);
Jeff Layton6c6910c2013-03-27 10:15:38 -0400531 drc_mem_usage += bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 lru_put_end(rp);
533 rp->c_secure = rqstp->rq_secure;
534 rp->c_type = cachetype;
535 rp->c_state = RC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 spin_unlock(&cache_lock);
537 return;
538}
539
540/*
541 * Copy cached reply to current reply buffer. Should always fit.
542 * FIXME as reply is in a page, we should just attach the page, and
543 * keep a refcount....
544 */
545static int
546nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
547{
548 struct kvec *vec = &rqstp->rq_res.head[0];
549
550 if (vec->iov_len + data->iov_len > PAGE_SIZE) {
551 printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
552 data->iov_len);
553 return 0;
554 }
555 memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
556 vec->iov_len += data->iov_len;
557 return 1;
558}
Jeff Laytona2f999a2013-03-27 10:15:38 -0400559
560/*
561 * Note that fields may be added, removed or reordered in the future. Programs
562 * scraping this file for info should test the labels to ensure they're
563 * getting the correct field.
564 */
565static int nfsd_reply_cache_stats_show(struct seq_file *m, void *v)
566{
567 spin_lock(&cache_lock);
568 seq_printf(m, "max entries: %u\n", max_drc_entries);
569 seq_printf(m, "num entries: %u\n", num_drc_entries);
570 seq_printf(m, "hash buckets: %u\n", HASHSIZE);
571 seq_printf(m, "mem usage: %u\n", drc_mem_usage);
572 seq_printf(m, "cache hits: %u\n", nfsdstats.rchits);
573 seq_printf(m, "cache misses: %u\n", nfsdstats.rcmisses);
574 seq_printf(m, "not cached: %u\n", nfsdstats.rcnocache);
575 seq_printf(m, "payload misses: %u\n", payload_misses);
576 spin_unlock(&cache_lock);
577 return 0;
578}
579
580int nfsd_reply_cache_stats_open(struct inode *inode, struct file *file)
581{
582 return single_open(file, nfsd_reply_cache_stats_show, NULL);
583}