blob: c01f44eb7fe43267dc52092606415a2a3201a39c [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 Layton0733c7b2013-03-27 10:15:39 -040014#include <linux/log2.h>
15#include <linux/hash.h>
Jeff Layton01a7dec2013-02-04 11:57:27 -050016#include <net/checksum.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017
Boaz Harrosh9a74af22009-12-03 20:30:56 +020018#include "nfsd.h"
19#include "cache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jeff Layton0338dd12013-02-04 08:18:02 -050021#define NFSDDBG_FACILITY NFSDDBG_REPCACHE
22
Jeff Layton0733c7b2013-03-27 10:15:39 -040023/*
24 * We use this value to determine the number of hash buckets from the max
25 * cache size, the idea being that when the cache is at its maximum number
26 * of entries, then this should be the average number of entries per bucket.
27 */
28#define TARGET_BUCKET_SIZE 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Trond Myklebust7142b982014-08-06 13:44:20 -040030struct nfsd_drc_bucket {
31 struct hlist_head cache_hash;
Trond Myklebustbedd4b62014-08-06 13:44:21 -040032 struct list_head lru_head;
Trond Myklebust7142b982014-08-06 13:44:20 -040033};
34
35static struct nfsd_drc_bucket *drc_hashtbl;
Jeff Layton8a8bc402013-01-28 14:41:10 -050036static struct kmem_cache *drc_slab;
Jeff Layton9dc56142013-03-27 10:15:37 -040037
38/* max number of entries allowed in the cache */
Jeff Layton0338dd12013-02-04 08:18:02 -050039static unsigned int max_drc_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jeff Layton0733c7b2013-03-27 10:15:39 -040041/* number of significant bits in the hash value */
42static unsigned int maskbits;
Trond Myklebustbedd4b62014-08-06 13:44:21 -040043static unsigned int drc_hashsize;
Jeff Layton0733c7b2013-03-27 10:15:39 -040044
Greg Banksfca42172009-04-01 07:28:13 +110045/*
Jeff Layton9dc56142013-03-27 10:15:37 -040046 * Stats and other tracking of on the duplicate reply cache. All of these and
47 * the "rc" fields in nfsdstats are protected by the cache_lock
48 */
49
50/* total number of entries */
51static unsigned int num_drc_entries;
52
53/* cache misses due only to checksum comparison failures */
54static unsigned int payload_misses;
55
Jeff Layton6c6910c2013-03-27 10:15:38 -040056/* amount of memory (in bytes) currently consumed by the DRC */
57static unsigned int drc_mem_usage;
58
Jeff Layton98d821b2013-03-27 10:15:39 -040059/* longest hash chain seen */
60static unsigned int longest_chain;
61
62/* size of cache when we saw the longest hash chain */
63static unsigned int longest_chain_cachesize;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
Jeff Laytonaca8a232013-02-04 08:18:05 -050066static void cache_cleaner_func(struct work_struct *unused);
Dave Chinner1ab6c492013-08-28 10:18:09 +100067static unsigned long nfsd_reply_cache_count(struct shrinker *shrink,
68 struct shrink_control *sc);
69static unsigned long nfsd_reply_cache_scan(struct shrinker *shrink,
70 struct shrink_control *sc);
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -050071
Wei Yongjunc8c797f2013-04-05 21:22:39 +080072static struct shrinker nfsd_reply_cache_shrinker = {
Dave Chinner1ab6c492013-08-28 10:18:09 +100073 .scan_objects = nfsd_reply_cache_scan,
74 .count_objects = nfsd_reply_cache_count,
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -050075 .seeks = 1,
76};
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Greg Banksfca42172009-04-01 07:28:13 +110078/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * locking for the reply cache:
80 * A cache entry is "single use" if c_state == RC_INPROG
81 * Otherwise, it when accessing _prev or _next, the lock must be held.
82 */
83static DEFINE_SPINLOCK(cache_lock);
Jeff Laytonaca8a232013-02-04 08:18:05 -050084static DECLARE_DELAYED_WORK(cache_cleaner, cache_cleaner_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Jeff Layton0338dd12013-02-04 08:18:02 -050086/*
87 * Put a cap on the size of the DRC based on the amount of available
88 * low memory in the machine.
89 *
90 * 64MB: 8192
91 * 128MB: 11585
92 * 256MB: 16384
93 * 512MB: 23170
94 * 1GB: 32768
95 * 2GB: 46340
96 * 4GB: 65536
97 * 8GB: 92681
98 * 16GB: 131072
99 *
100 * ...with a hard cap of 256k entries. In the worst case, each entry will be
101 * ~1k, so the above numbers should give a rough max of the amount of memory
102 * used in k.
103 */
104static unsigned int
105nfsd_cache_size_limit(void)
106{
107 unsigned int limit;
108 unsigned long low_pages = totalram_pages - totalhigh_pages;
109
110 limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
111 return min_t(unsigned int, limit, 256*1024);
112}
113
Jeff Layton0733c7b2013-03-27 10:15:39 -0400114/*
115 * Compute the number of hash buckets we need. Divide the max cachesize by
116 * the "target" max bucket size, and round up to next power of two.
117 */
118static unsigned int
119nfsd_hashsize(unsigned int limit)
120{
121 return roundup_pow_of_two(limit / TARGET_BUCKET_SIZE);
122}
123
Trond Myklebust7142b982014-08-06 13:44:20 -0400124static u32
125nfsd_cache_hash(__be32 xid)
126{
127 return hash_32(be32_to_cpu(xid), maskbits);
128}
129
Jeff Laytonf09841f2013-01-28 14:41:11 -0500130static struct svc_cacherep *
131nfsd_reply_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 struct svc_cacherep *rp;
Jeff Laytonf09841f2013-01-28 14:41:11 -0500134
135 rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
136 if (rp) {
137 rp->c_state = RC_UNUSED;
138 rp->c_type = RC_NOCACHE;
139 INIT_LIST_HEAD(&rp->c_lru);
140 INIT_HLIST_NODE(&rp->c_hash);
141 }
142 return rp;
143}
144
145static void
146nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
147{
Jeff Layton6c6910c2013-03-27 10:15:38 -0400148 if (rp->c_type == RC_REPLBUFF && rp->c_replvec.iov_base) {
149 drc_mem_usage -= rp->c_replvec.iov_len;
Jeff Laytonf09841f2013-01-28 14:41:11 -0500150 kfree(rp->c_replvec.iov_base);
Jeff Layton6c6910c2013-03-27 10:15:38 -0400151 }
Jeff Laytona517b602013-03-18 10:49:07 -0400152 if (!hlist_unhashed(&rp->c_hash))
153 hlist_del(&rp->c_hash);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500154 list_del(&rp->c_lru);
Jeff Layton0ee0bf72013-02-04 08:18:01 -0500155 --num_drc_entries;
Jeff Layton6c6910c2013-03-27 10:15:38 -0400156 drc_mem_usage -= sizeof(*rp);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500157 kmem_cache_free(drc_slab, rp);
158}
159
Jeff Layton2c6b6912013-02-04 08:18:04 -0500160static void
161nfsd_reply_cache_free(struct svc_cacherep *rp)
162{
163 spin_lock(&cache_lock);
164 nfsd_reply_cache_free_locked(rp);
165 spin_unlock(&cache_lock);
166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168int nfsd_reply_cache_init(void)
169{
Jeff Layton0733c7b2013-03-27 10:15:39 -0400170 unsigned int hashsize;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400171 unsigned int i;
Jeff Layton0733c7b2013-03-27 10:15:39 -0400172
Jeff Laytonac534ff2013-03-15 09:16:29 -0400173 max_drc_entries = nfsd_cache_size_limit();
174 num_drc_entries = 0;
Jeff Layton0733c7b2013-03-27 10:15:39 -0400175 hashsize = nfsd_hashsize(max_drc_entries);
176 maskbits = ilog2(hashsize);
Jeff Laytonac534ff2013-03-15 09:16:29 -0400177
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500178 register_shrinker(&nfsd_reply_cache_shrinker);
Jeff Layton8a8bc402013-01-28 14:41:10 -0500179 drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
180 0, 0, NULL);
181 if (!drc_slab)
182 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Trond Myklebust7142b982014-08-06 13:44:20 -0400184 drc_hashtbl = kcalloc(hashsize, sizeof(*drc_hashtbl), GFP_KERNEL);
185 if (!drc_hashtbl)
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500186 goto out_nomem;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400187 for (i = 0; i < hashsize; i++)
188 INIT_LIST_HEAD(&drc_hashtbl[i].lru_head);
189 drc_hashsize = hashsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500191 return 0;
192out_nomem:
193 printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
194 nfsd_reply_cache_shutdown();
195 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500198void nfsd_reply_cache_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct svc_cacherep *rp;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400201 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500203 unregister_shrinker(&nfsd_reply_cache_shrinker);
Jeff Laytonaca8a232013-02-04 08:18:05 -0500204 cancel_delayed_work_sync(&cache_cleaner);
205
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400206 for (i = 0; i < drc_hashsize; i++) {
207 struct list_head *head = &drc_hashtbl[i].lru_head;
208 while (!list_empty(head)) {
209 rp = list_first_entry(head, struct svc_cacherep, c_lru);
210 nfsd_reply_cache_free_locked(rp);
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
Trond Myklebust7142b982014-08-06 13:44:20 -0400214 kfree (drc_hashtbl);
215 drc_hashtbl = NULL;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400216 drc_hashsize = 0;
Jeff Layton8a8bc402013-01-28 14:41:10 -0500217
218 if (drc_slab) {
219 kmem_cache_destroy(drc_slab);
220 drc_slab = NULL;
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/*
Jeff Laytonaca8a232013-02-04 08:18:05 -0500225 * Move cache entry to end of LRU list, and queue the cleaner to run if it's
226 * not already scheduled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
228static void
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400229lru_put_end(struct nfsd_drc_bucket *b, struct svc_cacherep *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Jeff Layton56c25482013-02-04 08:18:00 -0500231 rp->c_timestamp = jiffies;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400232 list_move_tail(&rp->c_lru, &b->lru_head);
Jeff Laytonaca8a232013-02-04 08:18:05 -0500233 schedule_delayed_work(&cache_cleaner, RC_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236/*
237 * Move a cache entry from one hash list to another
238 */
239static void
Trond Myklebust7142b982014-08-06 13:44:20 -0400240hash_refile(struct nfsd_drc_bucket *b, struct svc_cacherep *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 hlist_del_init(&rp->c_hash);
Trond Myklebust7142b982014-08-06 13:44:20 -0400243 hlist_add_head(&rp->c_hash, &b->cache_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Dave Chinner1ab6c492013-08-28 10:18:09 +1000246static long
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400247prune_bucket(struct nfsd_drc_bucket *b)
Jeff Laytonaca8a232013-02-04 08:18:05 -0500248{
249 struct svc_cacherep *rp, *tmp;
Dave Chinner1ab6c492013-08-28 10:18:09 +1000250 long freed = 0;
Jeff Laytonaca8a232013-02-04 08:18:05 -0500251
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400252 list_for_each_entry_safe(rp, tmp, &b->lru_head, c_lru) {
Jeff Layton1b194532014-06-05 09:45:00 -0400253 /*
254 * Don't free entries attached to calls that are still
255 * in-progress, but do keep scanning the list.
256 */
257 if (rp->c_state == RC_INPROG)
258 continue;
259 if (num_drc_entries <= max_drc_entries &&
260 time_before(jiffies, rp->c_timestamp + RC_EXPIRE))
Jeff Laytonaca8a232013-02-04 08:18:05 -0500261 break;
262 nfsd_reply_cache_free_locked(rp);
Dave Chinner1ab6c492013-08-28 10:18:09 +1000263 freed++;
Jeff Laytonaca8a232013-02-04 08:18:05 -0500264 }
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400265 return freed;
266}
267
268/*
269 * Walk the LRU list and prune off entries that are older than RC_EXPIRE.
270 * Also prune the oldest ones when the total exceeds the max number of entries.
271 */
272static long
273prune_cache_entries(void)
274{
275 unsigned int i;
276 long freed = 0;
277 bool cancel = true;
278
279 for (i = 0; i < drc_hashsize; i++) {
280 struct nfsd_drc_bucket *b = &drc_hashtbl[i];
281
282 freed += prune_bucket(b);
283 if (!list_empty(&b->lru_head))
284 cancel = false;
285 }
Jeff Laytonaca8a232013-02-04 08:18:05 -0500286
287 /*
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400288 * Conditionally rearm the job to run in RC_EXPIRE since we just
289 * ran the pruner.
Jeff Laytonaca8a232013-02-04 08:18:05 -0500290 */
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400291 if (!cancel)
Jeff Laytonaca8a232013-02-04 08:18:05 -0500292 mod_delayed_work(system_wq, &cache_cleaner, RC_EXPIRE);
Dave Chinner1ab6c492013-08-28 10:18:09 +1000293 return freed;
Jeff Laytonaca8a232013-02-04 08:18:05 -0500294}
295
296static void
297cache_cleaner_func(struct work_struct *unused)
298{
299 spin_lock(&cache_lock);
300 prune_cache_entries();
301 spin_unlock(&cache_lock);
302}
303
Dave Chinner1ab6c492013-08-28 10:18:09 +1000304static unsigned long
305nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc)
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500306{
Dave Chinner1ab6c492013-08-28 10:18:09 +1000307 unsigned long num;
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500308
309 spin_lock(&cache_lock);
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500310 num = num_drc_entries;
311 spin_unlock(&cache_lock);
312
313 return num;
314}
315
Dave Chinner1ab6c492013-08-28 10:18:09 +1000316static unsigned long
317nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
318{
319 unsigned long freed;
320
321 spin_lock(&cache_lock);
322 freed = prune_cache_entries();
323 spin_unlock(&cache_lock);
324 return freed;
325}
Jeff Laytonaca8a232013-02-04 08:18:05 -0500326/*
Jeff Layton01a7dec2013-02-04 11:57:27 -0500327 * Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes
328 */
329static __wsum
330nfsd_cache_csum(struct svc_rqst *rqstp)
331{
332 int idx;
333 unsigned int base;
334 __wsum csum;
335 struct xdr_buf *buf = &rqstp->rq_arg;
336 const unsigned char *p = buf->head[0].iov_base;
337 size_t csum_len = min_t(size_t, buf->head[0].iov_len + buf->page_len,
338 RC_CSUMLEN);
339 size_t len = min(buf->head[0].iov_len, csum_len);
340
341 /* rq_arg.head first */
342 csum = csum_partial(p, len, 0);
343 csum_len -= len;
344
345 /* Continue into page array */
346 idx = buf->page_base / PAGE_SIZE;
347 base = buf->page_base & ~PAGE_MASK;
348 while (csum_len) {
349 p = page_address(buf->pages[idx]) + base;
Jeff Layton56edc862013-02-15 13:36:34 -0500350 len = min_t(size_t, PAGE_SIZE - base, csum_len);
Jeff Layton01a7dec2013-02-04 11:57:27 -0500351 csum = csum_partial(p, len, csum);
352 csum_len -= len;
353 base = 0;
354 ++idx;
355 }
356 return csum;
357}
358
Jeff Layton9dc56142013-03-27 10:15:37 -0400359static bool
360nfsd_cache_match(struct svc_rqst *rqstp, __wsum csum, struct svc_cacherep *rp)
361{
362 /* Check RPC header info first */
363 if (rqstp->rq_xid != rp->c_xid || rqstp->rq_proc != rp->c_proc ||
364 rqstp->rq_prot != rp->c_prot || rqstp->rq_vers != rp->c_vers ||
365 rqstp->rq_arg.len != rp->c_len ||
366 !rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) ||
367 rpc_get_port(svc_addr(rqstp)) != rpc_get_port((struct sockaddr *)&rp->c_addr))
368 return false;
369
370 /* compare checksum of NFS data */
371 if (csum != rp->c_csum) {
372 ++payload_misses;
373 return false;
374 }
375
376 return true;
377}
378
Jeff Layton01a7dec2013-02-04 11:57:27 -0500379/*
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500380 * Search the request hash for an entry that matches the given rqstp.
381 * Must be called with cache_lock held. Returns the found entry or
382 * NULL on failure.
383 */
384static struct svc_cacherep *
Trond Myklebust7142b982014-08-06 13:44:20 -0400385nfsd_cache_search(struct nfsd_drc_bucket *b, struct svc_rqst *rqstp,
386 __wsum csum)
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500387{
Jeff Layton98d821b2013-03-27 10:15:39 -0400388 struct svc_cacherep *rp, *ret = NULL;
Trond Myklebust7142b982014-08-06 13:44:20 -0400389 struct hlist_head *rh = &b->cache_hash;
Jeff Layton98d821b2013-03-27 10:15:39 -0400390 unsigned int entries = 0;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500391
Linus Torvaldsb6669732013-02-28 18:02:55 -0800392 hlist_for_each_entry(rp, rh, c_hash) {
Jeff Layton98d821b2013-03-27 10:15:39 -0400393 ++entries;
394 if (nfsd_cache_match(rqstp, csum, rp)) {
395 ret = rp;
396 break;
397 }
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500398 }
Jeff Layton98d821b2013-03-27 10:15:39 -0400399
400 /* tally hash chain length stats */
401 if (entries > longest_chain) {
402 longest_chain = entries;
403 longest_chain_cachesize = num_drc_entries;
404 } else if (entries == longest_chain) {
405 /* prefer to keep the smallest cachesize possible here */
406 longest_chain_cachesize = min(longest_chain_cachesize,
407 num_drc_entries);
408 }
409
410 return ret;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500411}
412
413/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * Try to find an entry matching the current call in the cache. When none
Jeff Layton1ac83622013-02-14 16:45:13 -0500415 * is found, we try to grab the oldest expired entry off the LRU list. If
416 * a suitable one isn't there, then drop the cache_lock and allocate a
417 * new one, then search again in case one got inserted while this thread
418 * didn't hold the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
420int
J. Bruce Fields10910062011-01-24 12:11:02 -0500421nfsd_cache_lookup(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Jeff Layton0338dd12013-02-04 08:18:02 -0500423 struct svc_cacherep *rp, *found;
Al Viroc7afef12006-10-19 23:29:02 -0700424 __be32 xid = rqstp->rq_xid;
425 u32 proto = rqstp->rq_prot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 vers = rqstp->rq_vers,
427 proc = rqstp->rq_proc;
Jeff Layton01a7dec2013-02-04 11:57:27 -0500428 __wsum csum;
Trond Myklebust7142b982014-08-06 13:44:20 -0400429 u32 hash = nfsd_cache_hash(xid);
430 struct nfsd_drc_bucket *b = &drc_hashtbl[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 unsigned long age;
J. Bruce Fields10910062011-01-24 12:11:02 -0500432 int type = rqstp->rq_cachetype;
Jeff Layton0b9ea372013-03-27 10:15:37 -0400433 int rtn = RC_DOIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 rqstp->rq_cacherep = NULL;
Jeff Layton13cc8a72013-02-04 08:18:03 -0500436 if (type == RC_NOCACHE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 nfsdstats.rcnocache++;
Jeff Layton0b9ea372013-03-27 10:15:37 -0400438 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
Jeff Layton01a7dec2013-02-04 11:57:27 -0500441 csum = nfsd_cache_csum(rqstp);
442
Jeff Layton0b9ea372013-03-27 10:15:37 -0400443 /*
444 * Since the common case is a cache miss followed by an insert,
Jeff Laytona0ef5e192013-12-05 06:00:51 -0500445 * preallocate an entry.
Jeff Layton0b9ea372013-03-27 10:15:37 -0400446 */
Jeff Layton0338dd12013-02-04 08:18:02 -0500447 rp = nfsd_reply_cache_alloc();
Jeff Layton0338dd12013-02-04 08:18:02 -0500448 spin_lock(&cache_lock);
Jeff Layton6c6910c2013-03-27 10:15:38 -0400449 if (likely(rp)) {
Jeff Layton0b9ea372013-03-27 10:15:37 -0400450 ++num_drc_entries;
Jeff Layton6c6910c2013-03-27 10:15:38 -0400451 drc_mem_usage += sizeof(*rp);
452 }
Jeff Layton0338dd12013-02-04 08:18:02 -0500453
Jeff Laytona0ef5e192013-12-05 06:00:51 -0500454 /* go ahead and prune the cache */
455 prune_cache_entries();
456
Trond Myklebust7142b982014-08-06 13:44:20 -0400457 found = nfsd_cache_search(b, rqstp, csum);
Jeff Layton0338dd12013-02-04 08:18:02 -0500458 if (found) {
Jeff Layton0b9ea372013-03-27 10:15:37 -0400459 if (likely(rp))
460 nfsd_reply_cache_free_locked(rp);
Jeff Layton0338dd12013-02-04 08:18:02 -0500461 rp = found;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500462 goto found_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Jeff Layton0338dd12013-02-04 08:18:02 -0500464
Jeff Layton0b9ea372013-03-27 10:15:37 -0400465 if (!rp) {
466 dprintk("nfsd: unable to allocate DRC entry!\n");
467 goto out;
468 }
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 nfsdstats.rcmisses++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 rqstp->rq_cacherep = rp;
472 rp->c_state = RC_INPROG;
473 rp->c_xid = xid;
474 rp->c_proc = proc;
Jeff Layton7b9e8522013-01-28 14:41:07 -0500475 rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
476 rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 rp->c_prot = proto;
478 rp->c_vers = vers;
Jeff Layton01a7dec2013-02-04 11:57:27 -0500479 rp->c_len = rqstp->rq_arg.len;
480 rp->c_csum = csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Trond Myklebust7142b982014-08-06 13:44:20 -0400482 hash_refile(b, rp);
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400483 lru_put_end(b, rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* release any buffer */
486 if (rp->c_type == RC_REPLBUFF) {
Jeff Layton6c6910c2013-03-27 10:15:38 -0400487 drc_mem_usage -= rp->c_replvec.iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 kfree(rp->c_replvec.iov_base);
489 rp->c_replvec.iov_base = NULL;
490 }
491 rp->c_type = RC_NOCACHE;
492 out:
493 spin_unlock(&cache_lock);
494 return rtn;
495
496found_entry:
Jeff Layton0338dd12013-02-04 08:18:02 -0500497 nfsdstats.rchits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /* We found a matching entry which is either in progress or done. */
499 age = jiffies - rp->c_timestamp;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400500 lru_put_end(b, rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 rtn = RC_DROPIT;
503 /* Request being processed or excessive rexmits */
504 if (rp->c_state == RC_INPROG || age < RC_DELAY)
505 goto out;
506
507 /* From the hall of fame of impractical attacks:
508 * Is this a user who tries to snoop on the cache? */
509 rtn = RC_DOIT;
510 if (!rqstp->rq_secure && rp->c_secure)
511 goto out;
512
513 /* Compose RPC reply header */
514 switch (rp->c_type) {
515 case RC_NOCACHE:
516 break;
517 case RC_REPLSTAT:
518 svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
519 rtn = RC_REPLY;
520 break;
521 case RC_REPLBUFF:
522 if (!nfsd_cache_append(rqstp, &rp->c_replvec))
523 goto out; /* should not happen */
524 rtn = RC_REPLY;
525 break;
526 default:
527 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
Jeff Layton0338dd12013-02-04 08:18:02 -0500528 nfsd_reply_cache_free_locked(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
531 goto out;
532}
533
534/*
535 * Update a cache entry. This is called from nfsd_dispatch when
536 * the procedure has been executed and the complete reply is in
537 * rqstp->rq_res.
538 *
539 * We're copying around data here rather than swapping buffers because
540 * the toplevel loop requires max-sized buffers, which would be a waste
541 * of memory for a cache with a max reply size of 100 bytes (diropokres).
542 *
543 * If we should start to use different types of cache entries tailored
544 * specifically for attrstat and fh's, we may save even more space.
545 *
546 * Also note that a cachetype of RC_NOCACHE can legally be passed when
547 * nfsd failed to encode a reply that otherwise would have been cached.
548 * In this case, nfsd_cache_update is called with statp == NULL.
549 */
550void
Al Viroc7afef12006-10-19 23:29:02 -0700551nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Jeff Layton13cc8a72013-02-04 08:18:03 -0500553 struct svc_cacherep *rp = rqstp->rq_cacherep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400555 u32 hash;
556 struct nfsd_drc_bucket *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int len;
Jeff Layton6c6910c2013-03-27 10:15:38 -0400558 size_t bufsize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Jeff Layton13cc8a72013-02-04 08:18:03 -0500560 if (!rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return;
562
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400563 hash = nfsd_cache_hash(rp->c_xid);
564 b = &drc_hashtbl[hash];
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
567 len >>= 2;
Greg Banksfca42172009-04-01 07:28:13 +1100568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* Don't cache excessive amounts of data and XDR failures */
570 if (!statp || len > (256 >> 2)) {
Jeff Layton2c6b6912013-02-04 08:18:04 -0500571 nfsd_reply_cache_free(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return;
573 }
574
575 switch (cachetype) {
576 case RC_REPLSTAT:
577 if (len != 1)
578 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
579 rp->c_replstat = *statp;
580 break;
581 case RC_REPLBUFF:
582 cachv = &rp->c_replvec;
Jeff Layton6c6910c2013-03-27 10:15:38 -0400583 bufsize = len << 2;
584 cachv->iov_base = kmalloc(bufsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (!cachv->iov_base) {
Jeff Layton2c6b6912013-02-04 08:18:04 -0500586 nfsd_reply_cache_free(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return;
588 }
Jeff Layton6c6910c2013-03-27 10:15:38 -0400589 cachv->iov_len = bufsize;
590 memcpy(cachv->iov_base, statp, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 break;
Jeff Layton2c6b6912013-02-04 08:18:04 -0500592 case RC_NOCACHE:
593 nfsd_reply_cache_free(rp);
594 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 spin_lock(&cache_lock);
Jeff Layton6c6910c2013-03-27 10:15:38 -0400597 drc_mem_usage += bufsize;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400598 lru_put_end(b, rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 rp->c_secure = rqstp->rq_secure;
600 rp->c_type = cachetype;
601 rp->c_state = RC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 spin_unlock(&cache_lock);
603 return;
604}
605
606/*
607 * Copy cached reply to current reply buffer. Should always fit.
608 * FIXME as reply is in a page, we should just attach the page, and
609 * keep a refcount....
610 */
611static int
612nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
613{
614 struct kvec *vec = &rqstp->rq_res.head[0];
615
616 if (vec->iov_len + data->iov_len > PAGE_SIZE) {
617 printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
618 data->iov_len);
619 return 0;
620 }
621 memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
622 vec->iov_len += data->iov_len;
623 return 1;
624}
Jeff Laytona2f999a2013-03-27 10:15:38 -0400625
626/*
627 * Note that fields may be added, removed or reordered in the future. Programs
628 * scraping this file for info should test the labels to ensure they're
629 * getting the correct field.
630 */
631static int nfsd_reply_cache_stats_show(struct seq_file *m, void *v)
632{
633 spin_lock(&cache_lock);
634 seq_printf(m, "max entries: %u\n", max_drc_entries);
635 seq_printf(m, "num entries: %u\n", num_drc_entries);
Jeff Layton0733c7b2013-03-27 10:15:39 -0400636 seq_printf(m, "hash buckets: %u\n", 1 << maskbits);
Jeff Laytona2f999a2013-03-27 10:15:38 -0400637 seq_printf(m, "mem usage: %u\n", drc_mem_usage);
638 seq_printf(m, "cache hits: %u\n", nfsdstats.rchits);
639 seq_printf(m, "cache misses: %u\n", nfsdstats.rcmisses);
640 seq_printf(m, "not cached: %u\n", nfsdstats.rcnocache);
641 seq_printf(m, "payload misses: %u\n", payload_misses);
Jeff Layton98d821b2013-03-27 10:15:39 -0400642 seq_printf(m, "longest chain len: %u\n", longest_chain);
643 seq_printf(m, "cachesize at longest: %u\n", longest_chain_cachesize);
Jeff Laytona2f999a2013-03-27 10:15:38 -0400644 spin_unlock(&cache_lock);
645 return 0;
646}
647
648int nfsd_reply_cache_stats_open(struct inode *inode, struct file *file)
649{
650 return single_open(file, nfsd_reply_cache_stats_show, NULL);
651}