blob: 334f2ad6070491d2302e93fd7fc87b6603c325f7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Request reply cache. This is currently a global cache, but this may
4 * change in the future and be a per-client cache.
5 *
6 * This code is heavily inspired by the 44BSD implementation, although
7 * it does things a bit differently.
8 *
9 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
10 */
11
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Jeff Layton8f975142016-10-26 07:26:40 -040013#include <linux/vmalloc.h>
Jeff Layton59766872013-02-04 12:50:00 -050014#include <linux/sunrpc/addr.h>
Jeff Layton0338dd12013-02-04 08:18:02 -050015#include <linux/highmem.h>
Jeff Layton0733c7b2013-03-27 10:15:39 -040016#include <linux/log2.h>
17#include <linux/hash.h>
Jeff Layton01a7dec2013-02-04 11:57:27 -050018#include <net/checksum.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019
Boaz Harrosh9a74af22009-12-03 20:30:56 +020020#include "nfsd.h"
21#include "cache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Jeff Layton0338dd12013-02-04 08:18:02 -050023#define NFSDDBG_FACILITY NFSDDBG_REPCACHE
24
Jeff Layton0733c7b2013-03-27 10:15:39 -040025/*
26 * We use this value to determine the number of hash buckets from the max
27 * cache size, the idea being that when the cache is at its maximum number
28 * of entries, then this should be the average number of entries per bucket.
29 */
30#define TARGET_BUCKET_SIZE 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Trond Myklebust7142b982014-08-06 13:44:20 -040032struct nfsd_drc_bucket {
Trond Myklebustbedd4b62014-08-06 13:44:21 -040033 struct list_head lru_head;
Trond Myklebust89a26b32014-08-06 13:44:24 -040034 spinlock_t cache_lock;
Trond Myklebust7142b982014-08-06 13:44:20 -040035};
36
37static struct nfsd_drc_bucket *drc_hashtbl;
Jeff Layton8a8bc402013-01-28 14:41:10 -050038static struct kmem_cache *drc_slab;
Jeff Layton9dc56142013-03-27 10:15:37 -040039
40/* max number of entries allowed in the cache */
Jeff Layton0338dd12013-02-04 08:18:02 -050041static unsigned int max_drc_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jeff Layton0733c7b2013-03-27 10:15:39 -040043/* number of significant bits in the hash value */
44static unsigned int maskbits;
Trond Myklebustbedd4b62014-08-06 13:44:21 -040045static unsigned int drc_hashsize;
Jeff Layton0733c7b2013-03-27 10:15:39 -040046
Greg Banksfca42172009-04-01 07:28:13 +110047/*
Jeff Layton9dc56142013-03-27 10:15:37 -040048 * Stats and other tracking of on the duplicate reply cache. All of these and
49 * the "rc" fields in nfsdstats are protected by the cache_lock
50 */
51
52/* total number of entries */
Trond Myklebust31e60f52014-08-06 13:44:23 -040053static atomic_t num_drc_entries;
Jeff Layton9dc56142013-03-27 10:15:37 -040054
55/* cache misses due only to checksum comparison failures */
56static unsigned int payload_misses;
57
Jeff Layton6c6910c2013-03-27 10:15:38 -040058/* amount of memory (in bytes) currently consumed by the DRC */
59static unsigned int drc_mem_usage;
60
Jeff Layton98d821b2013-03-27 10:15:39 -040061/* longest hash chain seen */
62static unsigned int longest_chain;
63
64/* size of cache when we saw the longest hash chain */
65static unsigned int longest_chain_cachesize;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
Dave Chinner1ab6c492013-08-28 10:18:09 +100068static unsigned long nfsd_reply_cache_count(struct shrinker *shrink,
69 struct shrink_control *sc);
70static unsigned long nfsd_reply_cache_scan(struct shrinker *shrink,
71 struct shrink_control *sc);
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -050072
Wei Yongjunc8c797f2013-04-05 21:22:39 +080073static struct shrinker nfsd_reply_cache_shrinker = {
Dave Chinner1ab6c492013-08-28 10:18:09 +100074 .scan_objects = nfsd_reply_cache_scan,
75 .count_objects = nfsd_reply_cache_count,
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -050076 .seeks = 1,
77};
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Greg Banksfca42172009-04-01 07:28:13 +110079/*
Jeff Layton0338dd12013-02-04 08:18:02 -050080 * Put a cap on the size of the DRC based on the amount of available
81 * low memory in the machine.
82 *
83 * 64MB: 8192
84 * 128MB: 11585
85 * 256MB: 16384
86 * 512MB: 23170
87 * 1GB: 32768
88 * 2GB: 46340
89 * 4GB: 65536
90 * 8GB: 92681
91 * 16GB: 131072
92 *
93 * ...with a hard cap of 256k entries. In the worst case, each entry will be
94 * ~1k, so the above numbers should give a rough max of the amount of memory
95 * used in k.
96 */
97static unsigned int
98nfsd_cache_size_limit(void)
99{
100 unsigned int limit;
101 unsigned long low_pages = totalram_pages - totalhigh_pages;
102
103 limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
104 return min_t(unsigned int, limit, 256*1024);
105}
106
Jeff Layton0733c7b2013-03-27 10:15:39 -0400107/*
108 * Compute the number of hash buckets we need. Divide the max cachesize by
109 * the "target" max bucket size, and round up to next power of two.
110 */
111static unsigned int
112nfsd_hashsize(unsigned int limit)
113{
114 return roundup_pow_of_two(limit / TARGET_BUCKET_SIZE);
115}
116
Trond Myklebust7142b982014-08-06 13:44:20 -0400117static u32
118nfsd_cache_hash(__be32 xid)
119{
120 return hash_32(be32_to_cpu(xid), maskbits);
121}
122
Jeff Laytonf09841f2013-01-28 14:41:11 -0500123static struct svc_cacherep *
124nfsd_reply_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct svc_cacherep *rp;
Jeff Laytonf09841f2013-01-28 14:41:11 -0500127
128 rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
129 if (rp) {
130 rp->c_state = RC_UNUSED;
131 rp->c_type = RC_NOCACHE;
132 INIT_LIST_HEAD(&rp->c_lru);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500133 }
134 return rp;
135}
136
137static void
138nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
139{
Jeff Layton6c6910c2013-03-27 10:15:38 -0400140 if (rp->c_type == RC_REPLBUFF && rp->c_replvec.iov_base) {
141 drc_mem_usage -= rp->c_replvec.iov_len;
Jeff Laytonf09841f2013-01-28 14:41:11 -0500142 kfree(rp->c_replvec.iov_base);
Jeff Layton6c6910c2013-03-27 10:15:38 -0400143 }
Jeff Laytonf09841f2013-01-28 14:41:11 -0500144 list_del(&rp->c_lru);
Trond Myklebust31e60f52014-08-06 13:44:23 -0400145 atomic_dec(&num_drc_entries);
Jeff Layton6c6910c2013-03-27 10:15:38 -0400146 drc_mem_usage -= sizeof(*rp);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500147 kmem_cache_free(drc_slab, rp);
148}
149
Jeff Layton2c6b6912013-02-04 08:18:04 -0500150static void
Trond Myklebust89a26b32014-08-06 13:44:24 -0400151nfsd_reply_cache_free(struct nfsd_drc_bucket *b, struct svc_cacherep *rp)
Jeff Layton2c6b6912013-02-04 08:18:04 -0500152{
Trond Myklebust89a26b32014-08-06 13:44:24 -0400153 spin_lock(&b->cache_lock);
Jeff Layton2c6b6912013-02-04 08:18:04 -0500154 nfsd_reply_cache_free_locked(rp);
Trond Myklebust89a26b32014-08-06 13:44:24 -0400155 spin_unlock(&b->cache_lock);
Jeff Layton2c6b6912013-02-04 08:18:04 -0500156}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158int nfsd_reply_cache_init(void)
159{
Jeff Layton0733c7b2013-03-27 10:15:39 -0400160 unsigned int hashsize;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400161 unsigned int i;
Kinglong Meea68465c2015-03-19 19:48:31 +0800162 int status = 0;
Jeff Layton0733c7b2013-03-27 10:15:39 -0400163
Jeff Laytonac534ff2013-03-15 09:16:29 -0400164 max_drc_entries = nfsd_cache_size_limit();
Trond Myklebust31e60f52014-08-06 13:44:23 -0400165 atomic_set(&num_drc_entries, 0);
Jeff Layton0733c7b2013-03-27 10:15:39 -0400166 hashsize = nfsd_hashsize(max_drc_entries);
167 maskbits = ilog2(hashsize);
Jeff Laytonac534ff2013-03-15 09:16:29 -0400168
Kinglong Meea68465c2015-03-19 19:48:31 +0800169 status = register_shrinker(&nfsd_reply_cache_shrinker);
170 if (status)
171 return status;
172
Jeff Layton8a8bc402013-01-28 14:41:10 -0500173 drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
174 0, 0, NULL);
175 if (!drc_slab)
176 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Trond Myklebust7142b982014-08-06 13:44:20 -0400178 drc_hashtbl = kcalloc(hashsize, sizeof(*drc_hashtbl), GFP_KERNEL);
Jeff Layton8f975142016-10-26 07:26:40 -0400179 if (!drc_hashtbl) {
180 drc_hashtbl = vzalloc(hashsize * sizeof(*drc_hashtbl));
181 if (!drc_hashtbl)
182 goto out_nomem;
183 }
184
Trond Myklebust89a26b32014-08-06 13:44:24 -0400185 for (i = 0; i < hashsize; i++) {
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400186 INIT_LIST_HEAD(&drc_hashtbl[i].lru_head);
Trond Myklebust89a26b32014-08-06 13:44:24 -0400187 spin_lock_init(&drc_hashtbl[i].cache_lock);
188 }
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400189 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
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400205 for (i = 0; i < drc_hashsize; i++) {
206 struct list_head *head = &drc_hashtbl[i].lru_head;
207 while (!list_empty(head)) {
208 rp = list_first_entry(head, struct svc_cacherep, c_lru);
209 nfsd_reply_cache_free_locked(rp);
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
Jeff Layton8f975142016-10-26 07:26:40 -0400213 kvfree(drc_hashtbl);
Trond Myklebust7142b982014-08-06 13:44:20 -0400214 drc_hashtbl = NULL;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400215 drc_hashsize = 0;
Jeff Layton8a8bc402013-01-28 14:41:10 -0500216
Julia Lawalle79017d2015-09-13 14:15:15 +0200217 kmem_cache_destroy(drc_slab);
218 drc_slab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/*
Jeff Laytonaca8a232013-02-04 08:18:05 -0500222 * Move cache entry to end of LRU list, and queue the cleaner to run if it's
223 * not already scheduled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
225static void
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400226lru_put_end(struct nfsd_drc_bucket *b, struct svc_cacherep *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Jeff Layton56c25482013-02-04 08:18:00 -0500228 rp->c_timestamp = jiffies;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400229 list_move_tail(&rp->c_lru, &b->lru_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Dave Chinner1ab6c492013-08-28 10:18:09 +1000232static long
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400233prune_bucket(struct nfsd_drc_bucket *b)
Jeff Laytonaca8a232013-02-04 08:18:05 -0500234{
235 struct svc_cacherep *rp, *tmp;
Dave Chinner1ab6c492013-08-28 10:18:09 +1000236 long freed = 0;
Jeff Laytonaca8a232013-02-04 08:18:05 -0500237
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400238 list_for_each_entry_safe(rp, tmp, &b->lru_head, c_lru) {
Jeff Layton1b194532014-06-05 09:45:00 -0400239 /*
240 * Don't free entries attached to calls that are still
241 * in-progress, but do keep scanning the list.
242 */
243 if (rp->c_state == RC_INPROG)
244 continue;
Trond Myklebust31e60f52014-08-06 13:44:23 -0400245 if (atomic_read(&num_drc_entries) <= max_drc_entries &&
Jeff Layton1b194532014-06-05 09:45:00 -0400246 time_before(jiffies, rp->c_timestamp + RC_EXPIRE))
Jeff Laytonaca8a232013-02-04 08:18:05 -0500247 break;
248 nfsd_reply_cache_free_locked(rp);
Dave Chinner1ab6c492013-08-28 10:18:09 +1000249 freed++;
Jeff Laytonaca8a232013-02-04 08:18:05 -0500250 }
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400251 return freed;
252}
253
254/*
255 * Walk the LRU list and prune off entries that are older than RC_EXPIRE.
256 * Also prune the oldest ones when the total exceeds the max number of entries.
257 */
258static long
259prune_cache_entries(void)
260{
261 unsigned int i;
262 long freed = 0;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400263
264 for (i = 0; i < drc_hashsize; i++) {
265 struct nfsd_drc_bucket *b = &drc_hashtbl[i];
266
Trond Myklebust89a26b32014-08-06 13:44:24 -0400267 if (list_empty(&b->lru_head))
268 continue;
269 spin_lock(&b->cache_lock);
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400270 freed += prune_bucket(b);
Trond Myklebust89a26b32014-08-06 13:44:24 -0400271 spin_unlock(&b->cache_lock);
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400272 }
Dave Chinner1ab6c492013-08-28 10:18:09 +1000273 return freed;
Jeff Laytonaca8a232013-02-04 08:18:05 -0500274}
275
Dave Chinner1ab6c492013-08-28 10:18:09 +1000276static unsigned long
277nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc)
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500278{
Trond Myklebust31e60f52014-08-06 13:44:23 -0400279 return atomic_read(&num_drc_entries);
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500280}
281
Dave Chinner1ab6c492013-08-28 10:18:09 +1000282static unsigned long
283nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
284{
Trond Myklebust89a26b32014-08-06 13:44:24 -0400285 return prune_cache_entries();
Dave Chinner1ab6c492013-08-28 10:18:09 +1000286}
Jeff Laytonaca8a232013-02-04 08:18:05 -0500287/*
Jeff Layton01a7dec2013-02-04 11:57:27 -0500288 * Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes
289 */
290static __wsum
291nfsd_cache_csum(struct svc_rqst *rqstp)
292{
293 int idx;
294 unsigned int base;
295 __wsum csum;
296 struct xdr_buf *buf = &rqstp->rq_arg;
297 const unsigned char *p = buf->head[0].iov_base;
298 size_t csum_len = min_t(size_t, buf->head[0].iov_len + buf->page_len,
299 RC_CSUMLEN);
300 size_t len = min(buf->head[0].iov_len, csum_len);
301
302 /* rq_arg.head first */
303 csum = csum_partial(p, len, 0);
304 csum_len -= len;
305
306 /* Continue into page array */
307 idx = buf->page_base / PAGE_SIZE;
308 base = buf->page_base & ~PAGE_MASK;
309 while (csum_len) {
310 p = page_address(buf->pages[idx]) + base;
Jeff Layton56edc862013-02-15 13:36:34 -0500311 len = min_t(size_t, PAGE_SIZE - base, csum_len);
Jeff Layton01a7dec2013-02-04 11:57:27 -0500312 csum = csum_partial(p, len, csum);
313 csum_len -= len;
314 base = 0;
315 ++idx;
316 }
317 return csum;
318}
319
Jeff Layton9dc56142013-03-27 10:15:37 -0400320static bool
321nfsd_cache_match(struct svc_rqst *rqstp, __wsum csum, struct svc_cacherep *rp)
322{
Trond Myklebustef9b16d2014-08-06 13:44:25 -0400323 /* Check RPC XID first */
324 if (rqstp->rq_xid != rp->c_xid)
Jeff Layton9dc56142013-03-27 10:15:37 -0400325 return false;
Jeff Layton9dc56142013-03-27 10:15:37 -0400326 /* compare checksum of NFS data */
327 if (csum != rp->c_csum) {
328 ++payload_misses;
329 return false;
330 }
331
Trond Myklebustef9b16d2014-08-06 13:44:25 -0400332 /* Other discriminators */
333 if (rqstp->rq_proc != rp->c_proc ||
334 rqstp->rq_prot != rp->c_prot ||
335 rqstp->rq_vers != rp->c_vers ||
336 rqstp->rq_arg.len != rp->c_len ||
337 !rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) ||
338 rpc_get_port(svc_addr(rqstp)) != rpc_get_port((struct sockaddr *)&rp->c_addr))
339 return false;
340
Jeff Layton9dc56142013-03-27 10:15:37 -0400341 return true;
342}
343
Jeff Layton01a7dec2013-02-04 11:57:27 -0500344/*
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500345 * Search the request hash for an entry that matches the given rqstp.
346 * Must be called with cache_lock held. Returns the found entry or
347 * NULL on failure.
348 */
349static struct svc_cacherep *
Trond Myklebust7142b982014-08-06 13:44:20 -0400350nfsd_cache_search(struct nfsd_drc_bucket *b, struct svc_rqst *rqstp,
351 __wsum csum)
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500352{
Jeff Layton98d821b2013-03-27 10:15:39 -0400353 struct svc_cacherep *rp, *ret = NULL;
Trond Myklebust11acf6e2014-08-06 13:44:22 -0400354 struct list_head *rh = &b->lru_head;
Jeff Layton98d821b2013-03-27 10:15:39 -0400355 unsigned int entries = 0;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500356
Trond Myklebust11acf6e2014-08-06 13:44:22 -0400357 list_for_each_entry(rp, rh, c_lru) {
Jeff Layton98d821b2013-03-27 10:15:39 -0400358 ++entries;
359 if (nfsd_cache_match(rqstp, csum, rp)) {
360 ret = rp;
361 break;
362 }
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500363 }
Jeff Layton98d821b2013-03-27 10:15:39 -0400364
365 /* tally hash chain length stats */
366 if (entries > longest_chain) {
367 longest_chain = entries;
Trond Myklebust31e60f52014-08-06 13:44:23 -0400368 longest_chain_cachesize = atomic_read(&num_drc_entries);
Jeff Layton98d821b2013-03-27 10:15:39 -0400369 } else if (entries == longest_chain) {
370 /* prefer to keep the smallest cachesize possible here */
Trond Myklebust31e60f52014-08-06 13:44:23 -0400371 longest_chain_cachesize = min_t(unsigned int,
372 longest_chain_cachesize,
373 atomic_read(&num_drc_entries));
Jeff Layton98d821b2013-03-27 10:15:39 -0400374 }
375
376 return ret;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500377}
378
379/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * Try to find an entry matching the current call in the cache. When none
Jeff Layton1ac83622013-02-14 16:45:13 -0500381 * is found, we try to grab the oldest expired entry off the LRU list. If
382 * a suitable one isn't there, then drop the cache_lock and allocate a
383 * new one, then search again in case one got inserted while this thread
384 * didn't hold the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
386int
J. Bruce Fields10910062011-01-24 12:11:02 -0500387nfsd_cache_lookup(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Jeff Layton0338dd12013-02-04 08:18:02 -0500389 struct svc_cacherep *rp, *found;
Al Viroc7afef12006-10-19 23:29:02 -0700390 __be32 xid = rqstp->rq_xid;
391 u32 proto = rqstp->rq_prot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 vers = rqstp->rq_vers,
393 proc = rqstp->rq_proc;
Jeff Layton01a7dec2013-02-04 11:57:27 -0500394 __wsum csum;
Trond Myklebust7142b982014-08-06 13:44:20 -0400395 u32 hash = nfsd_cache_hash(xid);
396 struct nfsd_drc_bucket *b = &drc_hashtbl[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 unsigned long age;
J. Bruce Fields10910062011-01-24 12:11:02 -0500398 int type = rqstp->rq_cachetype;
Jeff Layton0b9ea372013-03-27 10:15:37 -0400399 int rtn = RC_DOIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 rqstp->rq_cacherep = NULL;
Jeff Layton13cc8a72013-02-04 08:18:03 -0500402 if (type == RC_NOCACHE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 nfsdstats.rcnocache++;
Jeff Layton0b9ea372013-03-27 10:15:37 -0400404 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
Jeff Layton01a7dec2013-02-04 11:57:27 -0500407 csum = nfsd_cache_csum(rqstp);
408
Jeff Layton0b9ea372013-03-27 10:15:37 -0400409 /*
410 * Since the common case is a cache miss followed by an insert,
Jeff Laytona0ef5e192013-12-05 06:00:51 -0500411 * preallocate an entry.
Jeff Layton0b9ea372013-03-27 10:15:37 -0400412 */
Jeff Layton0338dd12013-02-04 08:18:02 -0500413 rp = nfsd_reply_cache_alloc();
Trond Myklebust89a26b32014-08-06 13:44:24 -0400414 spin_lock(&b->cache_lock);
Jeff Layton6c6910c2013-03-27 10:15:38 -0400415 if (likely(rp)) {
Trond Myklebust31e60f52014-08-06 13:44:23 -0400416 atomic_inc(&num_drc_entries);
Jeff Layton6c6910c2013-03-27 10:15:38 -0400417 drc_mem_usage += sizeof(*rp);
418 }
Jeff Layton0338dd12013-02-04 08:18:02 -0500419
Jeff Laytona0ef5e192013-12-05 06:00:51 -0500420 /* go ahead and prune the cache */
Trond Myklebust89a26b32014-08-06 13:44:24 -0400421 prune_bucket(b);
Jeff Laytona0ef5e192013-12-05 06:00:51 -0500422
Trond Myklebust7142b982014-08-06 13:44:20 -0400423 found = nfsd_cache_search(b, rqstp, csum);
Jeff Layton0338dd12013-02-04 08:18:02 -0500424 if (found) {
Jeff Layton0b9ea372013-03-27 10:15:37 -0400425 if (likely(rp))
426 nfsd_reply_cache_free_locked(rp);
Jeff Layton0338dd12013-02-04 08:18:02 -0500427 rp = found;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500428 goto found_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Jeff Layton0338dd12013-02-04 08:18:02 -0500430
Jeff Layton0b9ea372013-03-27 10:15:37 -0400431 if (!rp) {
432 dprintk("nfsd: unable to allocate DRC entry!\n");
433 goto out;
434 }
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 nfsdstats.rcmisses++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 rqstp->rq_cacherep = rp;
438 rp->c_state = RC_INPROG;
439 rp->c_xid = xid;
440 rp->c_proc = proc;
Jeff Layton7b9e8522013-01-28 14:41:07 -0500441 rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
442 rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 rp->c_prot = proto;
444 rp->c_vers = vers;
Jeff Layton01a7dec2013-02-04 11:57:27 -0500445 rp->c_len = rqstp->rq_arg.len;
446 rp->c_csum = csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400448 lru_put_end(b, rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /* release any buffer */
451 if (rp->c_type == RC_REPLBUFF) {
Jeff Layton6c6910c2013-03-27 10:15:38 -0400452 drc_mem_usage -= rp->c_replvec.iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 kfree(rp->c_replvec.iov_base);
454 rp->c_replvec.iov_base = NULL;
455 }
456 rp->c_type = RC_NOCACHE;
457 out:
Trond Myklebust89a26b32014-08-06 13:44:24 -0400458 spin_unlock(&b->cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return rtn;
460
461found_entry:
Jeff Layton0338dd12013-02-04 08:18:02 -0500462 nfsdstats.rchits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /* We found a matching entry which is either in progress or done. */
464 age = jiffies - rp->c_timestamp;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400465 lru_put_end(b, rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 rtn = RC_DROPIT;
468 /* Request being processed or excessive rexmits */
469 if (rp->c_state == RC_INPROG || age < RC_DELAY)
470 goto out;
471
472 /* From the hall of fame of impractical attacks:
473 * Is this a user who tries to snoop on the cache? */
474 rtn = RC_DOIT;
Jeff Layton4d152e22014-11-19 07:51:14 -0500475 if (!test_bit(RQ_SECURE, &rqstp->rq_flags) && rp->c_secure)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 goto out;
477
478 /* Compose RPC reply header */
479 switch (rp->c_type) {
480 case RC_NOCACHE:
481 break;
482 case RC_REPLSTAT:
483 svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
484 rtn = RC_REPLY;
485 break;
486 case RC_REPLBUFF:
487 if (!nfsd_cache_append(rqstp, &rp->c_replvec))
488 goto out; /* should not happen */
489 rtn = RC_REPLY;
490 break;
491 default:
492 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
Jeff Layton0338dd12013-02-04 08:18:02 -0500493 nfsd_reply_cache_free_locked(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495
496 goto out;
497}
498
499/*
500 * Update a cache entry. This is called from nfsd_dispatch when
501 * the procedure has been executed and the complete reply is in
502 * rqstp->rq_res.
503 *
504 * We're copying around data here rather than swapping buffers because
505 * the toplevel loop requires max-sized buffers, which would be a waste
506 * of memory for a cache with a max reply size of 100 bytes (diropokres).
507 *
508 * If we should start to use different types of cache entries tailored
509 * specifically for attrstat and fh's, we may save even more space.
510 *
511 * Also note that a cachetype of RC_NOCACHE can legally be passed when
512 * nfsd failed to encode a reply that otherwise would have been cached.
513 * In this case, nfsd_cache_update is called with statp == NULL.
514 */
515void
Al Viroc7afef12006-10-19 23:29:02 -0700516nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Jeff Layton13cc8a72013-02-04 08:18:03 -0500518 struct svc_cacherep *rp = rqstp->rq_cacherep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400520 u32 hash;
521 struct nfsd_drc_bucket *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int len;
Jeff Layton6c6910c2013-03-27 10:15:38 -0400523 size_t bufsize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Jeff Layton13cc8a72013-02-04 08:18:03 -0500525 if (!rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return;
527
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400528 hash = nfsd_cache_hash(rp->c_xid);
529 b = &drc_hashtbl[hash];
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
532 len >>= 2;
Greg Banksfca42172009-04-01 07:28:13 +1100533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* Don't cache excessive amounts of data and XDR failures */
535 if (!statp || len > (256 >> 2)) {
Trond Myklebust89a26b32014-08-06 13:44:24 -0400536 nfsd_reply_cache_free(b, rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return;
538 }
539
540 switch (cachetype) {
541 case RC_REPLSTAT:
542 if (len != 1)
543 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
544 rp->c_replstat = *statp;
545 break;
546 case RC_REPLBUFF:
547 cachv = &rp->c_replvec;
Jeff Layton6c6910c2013-03-27 10:15:38 -0400548 bufsize = len << 2;
549 cachv->iov_base = kmalloc(bufsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (!cachv->iov_base) {
Trond Myklebust89a26b32014-08-06 13:44:24 -0400551 nfsd_reply_cache_free(b, rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return;
553 }
Jeff Layton6c6910c2013-03-27 10:15:38 -0400554 cachv->iov_len = bufsize;
555 memcpy(cachv->iov_base, statp, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 break;
Jeff Layton2c6b6912013-02-04 08:18:04 -0500557 case RC_NOCACHE:
Trond Myklebust89a26b32014-08-06 13:44:24 -0400558 nfsd_reply_cache_free(b, rp);
Jeff Layton2c6b6912013-02-04 08:18:04 -0500559 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Trond Myklebust89a26b32014-08-06 13:44:24 -0400561 spin_lock(&b->cache_lock);
Jeff Layton6c6910c2013-03-27 10:15:38 -0400562 drc_mem_usage += bufsize;
Trond Myklebustbedd4b62014-08-06 13:44:21 -0400563 lru_put_end(b, rp);
Jeff Layton4d152e22014-11-19 07:51:14 -0500564 rp->c_secure = test_bit(RQ_SECURE, &rqstp->rq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rp->c_type = cachetype;
566 rp->c_state = RC_DONE;
Trond Myklebust89a26b32014-08-06 13:44:24 -0400567 spin_unlock(&b->cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return;
569}
570
571/*
572 * Copy cached reply to current reply buffer. Should always fit.
573 * FIXME as reply is in a page, we should just attach the page, and
574 * keep a refcount....
575 */
576static int
577nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
578{
579 struct kvec *vec = &rqstp->rq_res.head[0];
580
581 if (vec->iov_len + data->iov_len > PAGE_SIZE) {
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800582 printk(KERN_WARNING "nfsd: cached reply too large (%zd).\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 data->iov_len);
584 return 0;
585 }
586 memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
587 vec->iov_len += data->iov_len;
588 return 1;
589}
Jeff Laytona2f999a2013-03-27 10:15:38 -0400590
591/*
592 * Note that fields may be added, removed or reordered in the future. Programs
593 * scraping this file for info should test the labels to ensure they're
594 * getting the correct field.
595 */
596static int nfsd_reply_cache_stats_show(struct seq_file *m, void *v)
597{
Jeff Laytona2f999a2013-03-27 10:15:38 -0400598 seq_printf(m, "max entries: %u\n", max_drc_entries);
Trond Myklebust31e60f52014-08-06 13:44:23 -0400599 seq_printf(m, "num entries: %u\n",
600 atomic_read(&num_drc_entries));
Jeff Layton0733c7b2013-03-27 10:15:39 -0400601 seq_printf(m, "hash buckets: %u\n", 1 << maskbits);
Jeff Laytona2f999a2013-03-27 10:15:38 -0400602 seq_printf(m, "mem usage: %u\n", drc_mem_usage);
603 seq_printf(m, "cache hits: %u\n", nfsdstats.rchits);
604 seq_printf(m, "cache misses: %u\n", nfsdstats.rcmisses);
605 seq_printf(m, "not cached: %u\n", nfsdstats.rcnocache);
606 seq_printf(m, "payload misses: %u\n", payload_misses);
Jeff Layton98d821b2013-03-27 10:15:39 -0400607 seq_printf(m, "longest chain len: %u\n", longest_chain);
608 seq_printf(m, "cachesize at longest: %u\n", longest_chain_cachesize);
Jeff Laytona2f999a2013-03-27 10:15:38 -0400609 return 0;
610}
611
612int nfsd_reply_cache_stats_open(struct inode *inode, struct file *file)
613{
614 return single_open(file, nfsd_reply_cache_stats_show, NULL);
615}