1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Request reply cache. This is currently a global cache, but this may
41da177e4SLinus Torvalds * change in the future and be a per-client cache.
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * This code is heavily inspired by the 44BSD implementation, although
71da177e4SLinus Torvalds * it does things a bit differently.
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
101da177e4SLinus Torvalds */
111da177e4SLinus Torvalds
123ba75830SJ. Bruce Fields #include <linux/sunrpc/svc_xprt.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
148f97514bSJeff Layton #include <linux/vmalloc.h>
155976687aSJeff Layton #include <linux/sunrpc/addr.h>
160338dd15SJeff Layton #include <linux/highmem.h>
170733c7baSJeff Layton #include <linux/log2.h>
180733c7baSJeff Layton #include <linux/hash.h>
1901a7decfSJeff Layton #include <net/checksum.h>
205a0e3ad6STejun Heo
219a74af21SBoaz Harrosh #include "nfsd.h"
229a74af21SBoaz Harrosh #include "cache.h"
230b175b18SChuck Lever #include "trace.h"
240338dd15SJeff Layton
250733c7baSJeff Layton /*
260733c7baSJeff Layton * We use this value to determine the number of hash buckets from the max
270733c7baSJeff Layton * cache size, the idea being that when the cache is at its maximum number
280733c7baSJeff Layton * of entries, then this should be the average number of entries per bucket.
290733c7baSJeff Layton */
300733c7baSJeff Layton #define TARGET_BUCKET_SIZE 64
311da177e4SLinus Torvalds
327142b98dSTrond Myklebust struct nfsd_drc_bucket {
33736c6625STrond Myklebust struct rb_root rb_head;
34bedd4b61STrond Myklebust struct list_head lru_head;
3589a26b3dSTrond Myklebust spinlock_t cache_lock;
367142b98dSTrond Myklebust };
377142b98dSTrond Myklebust
38027690c7SJ. Bruce Fields static struct kmem_cache *drc_slab;
39027690c7SJ. Bruce Fields
401da177e4SLinus Torvalds static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
411ab6c499SDave Chinner static unsigned long nfsd_reply_cache_count(struct shrinker *shrink,
421ab6c499SDave Chinner struct shrink_control *sc);
431ab6c499SDave Chinner static unsigned long nfsd_reply_cache_scan(struct shrinker *shrink,
44b4e7f2c9SJeff Layton struct shrink_control *sc);
45b4e7f2c9SJeff Layton
461da177e4SLinus Torvalds /*
470338dd15SJeff Layton * Put a cap on the size of the DRC based on the amount of available
480338dd15SJeff Layton * low memory in the machine.
490338dd15SJeff Layton *
500338dd15SJeff Layton * 64MB: 8192
510338dd15SJeff Layton * 128MB: 11585
520338dd15SJeff Layton * 256MB: 16384
530338dd15SJeff Layton * 512MB: 23170
540338dd15SJeff Layton * 1GB: 32768
550338dd15SJeff Layton * 2GB: 46340
560338dd15SJeff Layton * 4GB: 65536
570338dd15SJeff Layton * 8GB: 92681
580338dd15SJeff Layton * 16GB: 131072
590338dd15SJeff Layton *
600338dd15SJeff Layton * ...with a hard cap of 256k entries. In the worst case, each entry will be
610338dd15SJeff Layton * ~1k, so the above numbers should give a rough max of the amount of memory
620338dd15SJeff Layton * used in k.
633ba75830SJ. Bruce Fields *
643ba75830SJ. Bruce Fields * XXX: these limits are per-container, so memory used will increase
653ba75830SJ. Bruce Fields * linearly with number of containers. Maybe that's OK.
660338dd15SJeff Layton */
670338dd15SJeff Layton static unsigned int
nfsd_cache_size_limit(void)680338dd15SJeff Layton nfsd_cache_size_limit(void)
690338dd15SJeff Layton {
700338dd15SJeff Layton unsigned int limit;
71ca79b0c2SArun KS unsigned long low_pages = totalram_pages() - totalhigh_pages();
720338dd15SJeff Layton
730338dd15SJeff Layton limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
740338dd15SJeff Layton return min_t(unsigned int, limit, 256*1024);
750338dd15SJeff Layton }
760338dd15SJeff Layton
770733c7baSJeff Layton /*
780733c7baSJeff Layton * Compute the number of hash buckets we need. Divide the max cachesize by
790733c7baSJeff Layton * the "target" max bucket size, and round up to next power of two.
800733c7baSJeff Layton */
810733c7baSJeff Layton static unsigned int
nfsd_hashsize(unsigned int limit)820733c7baSJeff Layton nfsd_hashsize(unsigned int limit)
830733c7baSJeff Layton {
840733c7baSJeff Layton return roundup_pow_of_two(limit / TARGET_BUCKET_SIZE);
850733c7baSJeff Layton }
860733c7baSJeff Layton
87e7421ce7SChuck Lever static struct nfsd_cacherep *
nfsd_cacherep_alloc(struct svc_rqst * rqstp,__wsum csum,struct nfsd_net * nn)88ff0d1693SChuck Lever nfsd_cacherep_alloc(struct svc_rqst *rqstp, __wsum csum,
893ba75830SJ. Bruce Fields struct nfsd_net *nn)
901da177e4SLinus Torvalds {
91e7421ce7SChuck Lever struct nfsd_cacherep *rp;
92f09841fdSJeff Layton
93027690c7SJ. Bruce Fields rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
94f09841fdSJeff Layton if (rp) {
95f09841fdSJeff Layton rp->c_state = RC_UNUSED;
96f09841fdSJeff Layton rp->c_type = RC_NOCACHE;
97736c6625STrond Myklebust RB_CLEAR_NODE(&rp->c_node);
98f09841fdSJeff Layton INIT_LIST_HEAD(&rp->c_lru);
9976ecec21STrond Myklebust
100ed00c2f6STrond Myklebust memset(&rp->c_key, 0, sizeof(rp->c_key));
101ed00c2f6STrond Myklebust rp->c_key.k_xid = rqstp->rq_xid;
102ed00c2f6STrond Myklebust rp->c_key.k_proc = rqstp->rq_proc;
103ed00c2f6STrond Myklebust rpc_copy_addr((struct sockaddr *)&rp->c_key.k_addr, svc_addr(rqstp));
104ed00c2f6STrond Myklebust rpc_set_port((struct sockaddr *)&rp->c_key.k_addr, rpc_get_port(svc_addr(rqstp)));
105ed00c2f6STrond Myklebust rp->c_key.k_prot = rqstp->rq_prot;
106ed00c2f6STrond Myklebust rp->c_key.k_vers = rqstp->rq_vers;
107ed00c2f6STrond Myklebust rp->c_key.k_len = rqstp->rq_arg.len;
108ed00c2f6STrond Myklebust rp->c_key.k_csum = csum;
109f09841fdSJeff Layton }
110f09841fdSJeff Layton return rp;
111f09841fdSJeff Layton }
112f09841fdSJeff Layton
nfsd_cacherep_free(struct nfsd_cacherep * rp)113e7421ce7SChuck Lever static void nfsd_cacherep_free(struct nfsd_cacherep *rp)
114f09841fdSJeff Layton {
11535308e7fSChuck Lever if (rp->c_type == RC_REPLBUFF)
116f09841fdSJeff Layton kfree(rp->c_replvec.iov_base);
11735308e7fSChuck Lever kmem_cache_free(drc_slab, rp);
1186c6910cdSJeff Layton }
11935308e7fSChuck Lever
120a9507f6aSChuck Lever static unsigned long
nfsd_cacherep_dispose(struct list_head * dispose)121a9507f6aSChuck Lever nfsd_cacherep_dispose(struct list_head *dispose)
122a9507f6aSChuck Lever {
123e7421ce7SChuck Lever struct nfsd_cacherep *rp;
124a9507f6aSChuck Lever unsigned long freed = 0;
125a9507f6aSChuck Lever
126a9507f6aSChuck Lever while (!list_empty(dispose)) {
127e7421ce7SChuck Lever rp = list_first_entry(dispose, struct nfsd_cacherep, c_lru);
128a9507f6aSChuck Lever list_del(&rp->c_lru);
129a9507f6aSChuck Lever nfsd_cacherep_free(rp);
130a9507f6aSChuck Lever freed++;
131a9507f6aSChuck Lever }
132a9507f6aSChuck Lever return freed;
133a9507f6aSChuck Lever }
134a9507f6aSChuck Lever
13535308e7fSChuck Lever static void
nfsd_cacherep_unlink_locked(struct nfsd_net * nn,struct nfsd_drc_bucket * b,struct nfsd_cacherep * rp)13635308e7fSChuck Lever nfsd_cacherep_unlink_locked(struct nfsd_net *nn, struct nfsd_drc_bucket *b,
137e7421ce7SChuck Lever struct nfsd_cacherep *rp)
13835308e7fSChuck Lever {
13935308e7fSChuck Lever if (rp->c_type == RC_REPLBUFF && rp->c_replvec.iov_base)
14035308e7fSChuck Lever nfsd_stats_drc_mem_usage_sub(nn, rp->c_replvec.iov_len);
14176ecec21STrond Myklebust if (rp->c_state != RC_UNUSED) {
142736c6625STrond Myklebust rb_erase(&rp->c_node, &b->rb_head);
143f09841fdSJeff Layton list_del(&rp->c_lru);
1443ba75830SJ. Bruce Fields atomic_dec(&nn->num_drc_entries);
145e567b98cSAmir Goldstein nfsd_stats_drc_mem_usage_sub(nn, sizeof(*rp));
14676ecec21STrond Myklebust }
14735308e7fSChuck Lever }
14835308e7fSChuck Lever
14935308e7fSChuck Lever static void
nfsd_reply_cache_free_locked(struct nfsd_drc_bucket * b,struct nfsd_cacherep * rp,struct nfsd_net * nn)150e7421ce7SChuck Lever nfsd_reply_cache_free_locked(struct nfsd_drc_bucket *b, struct nfsd_cacherep *rp,
15135308e7fSChuck Lever struct nfsd_net *nn)
15235308e7fSChuck Lever {
15335308e7fSChuck Lever nfsd_cacherep_unlink_locked(nn, b, rp);
15435308e7fSChuck Lever nfsd_cacherep_free(rp);
155f09841fdSJeff Layton }
156f09841fdSJeff Layton
1572c6b691cSJeff Layton static void
nfsd_reply_cache_free(struct nfsd_drc_bucket * b,struct nfsd_cacherep * rp,struct nfsd_net * nn)158e7421ce7SChuck Lever nfsd_reply_cache_free(struct nfsd_drc_bucket *b, struct nfsd_cacherep *rp,
1593ba75830SJ. Bruce Fields struct nfsd_net *nn)
1602c6b691cSJeff Layton {
16189a26b3dSTrond Myklebust spin_lock(&b->cache_lock);
16235308e7fSChuck Lever nfsd_cacherep_unlink_locked(nn, b, rp);
16389a26b3dSTrond Myklebust spin_unlock(&b->cache_lock);
16435308e7fSChuck Lever nfsd_cacherep_free(rp);
1652c6b691cSJeff Layton }
1661da177e4SLinus Torvalds
nfsd_drc_slab_create(void)167027690c7SJ. Bruce Fields int nfsd_drc_slab_create(void)
168027690c7SJ. Bruce Fields {
169027690c7SJ. Bruce Fields drc_slab = kmem_cache_create("nfsd_drc",
170e7421ce7SChuck Lever sizeof(struct nfsd_cacherep), 0, 0, NULL);
171027690c7SJ. Bruce Fields return drc_slab ? 0: -ENOMEM;
172027690c7SJ. Bruce Fields }
173027690c7SJ. Bruce Fields
nfsd_drc_slab_free(void)174027690c7SJ. Bruce Fields void nfsd_drc_slab_free(void)
175027690c7SJ. Bruce Fields {
176027690c7SJ. Bruce Fields kmem_cache_destroy(drc_slab);
177027690c7SJ. Bruce Fields }
178027690c7SJ. Bruce Fields
nfsd_reply_cache_init(struct nfsd_net * nn)1793ba75830SJ. Bruce Fields int nfsd_reply_cache_init(struct nfsd_net *nn)
1801da177e4SLinus Torvalds {
1810733c7baSJeff Layton unsigned int hashsize;
182bedd4b61STrond Myklebust unsigned int i;
183a68465c9SKinglong Mee int status = 0;
1840733c7baSJeff Layton
1853ba75830SJ. Bruce Fields nn->max_drc_entries = nfsd_cache_size_limit();
1863ba75830SJ. Bruce Fields atomic_set(&nn->num_drc_entries, 0);
1873ba75830SJ. Bruce Fields hashsize = nfsd_hashsize(nn->max_drc_entries);
1883ba75830SJ. Bruce Fields nn->maskbits = ilog2(hashsize);
189ac534ff2SJeff Layton
1903ba75830SJ. Bruce Fields nn->nfsd_reply_cache_shrinker.scan_objects = nfsd_reply_cache_scan;
1913ba75830SJ. Bruce Fields nn->nfsd_reply_cache_shrinker.count_objects = nfsd_reply_cache_count;
1923ba75830SJ. Bruce Fields nn->nfsd_reply_cache_shrinker.seeks = 1;
193e33c267aSRoman Gushchin status = register_shrinker(&nn->nfsd_reply_cache_shrinker,
194e33c267aSRoman Gushchin "nfsd-reply:%s", nn->nfsd_name);
195a68465c9SKinglong Mee if (status)
196ed9ab734SJeff Layton return status;
197a68465c9SKinglong Mee
1988c38b705SRik van Riel nn->drc_hashtbl = kvzalloc(array_size(hashsize,
1998c38b705SRik van Riel sizeof(*nn->drc_hashtbl)), GFP_KERNEL);
2003ba75830SJ. Bruce Fields if (!nn->drc_hashtbl)
201027690c7SJ. Bruce Fields goto out_shrinker;
2028f97514bSJeff Layton
20389a26b3dSTrond Myklebust for (i = 0; i < hashsize; i++) {
2043ba75830SJ. Bruce Fields INIT_LIST_HEAD(&nn->drc_hashtbl[i].lru_head);
2053ba75830SJ. Bruce Fields spin_lock_init(&nn->drc_hashtbl[i].cache_lock);
20689a26b3dSTrond Myklebust }
2073ba75830SJ. Bruce Fields nn->drc_hashsize = hashsize;
2081da177e4SLinus Torvalds
209d5c3428bSJ. Bruce Fields return 0;
210689d7ba4SJ. Bruce Fields out_shrinker:
211689d7ba4SJ. Bruce Fields unregister_shrinker(&nn->nfsd_reply_cache_shrinker);
212d5c3428bSJ. Bruce Fields printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
213d5c3428bSJ. Bruce Fields return -ENOMEM;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds
nfsd_reply_cache_shutdown(struct nfsd_net * nn)2163ba75830SJ. Bruce Fields void nfsd_reply_cache_shutdown(struct nfsd_net *nn)
2171da177e4SLinus Torvalds {
218e7421ce7SChuck Lever struct nfsd_cacherep *rp;
219bedd4b61STrond Myklebust unsigned int i;
2201da177e4SLinus Torvalds
2213ba75830SJ. Bruce Fields unregister_shrinker(&nn->nfsd_reply_cache_shrinker);
222aca8a23dSJeff Layton
2233ba75830SJ. Bruce Fields for (i = 0; i < nn->drc_hashsize; i++) {
2243ba75830SJ. Bruce Fields struct list_head *head = &nn->drc_hashtbl[i].lru_head;
225bedd4b61STrond Myklebust while (!list_empty(head)) {
226e7421ce7SChuck Lever rp = list_first_entry(head, struct nfsd_cacherep, c_lru);
2273ba75830SJ. Bruce Fields nfsd_reply_cache_free_locked(&nn->drc_hashtbl[i],
2283ba75830SJ. Bruce Fields rp, nn);
2291da177e4SLinus Torvalds }
230bedd4b61STrond Myklebust }
2311da177e4SLinus Torvalds
2323ba75830SJ. Bruce Fields kvfree(nn->drc_hashtbl);
2333ba75830SJ. Bruce Fields nn->drc_hashtbl = NULL;
2343ba75830SJ. Bruce Fields nn->drc_hashsize = 0;
2358a8bc40dSJeff Layton
2368a8bc40dSJeff Layton }
2371da177e4SLinus Torvalds
2381da177e4SLinus Torvalds /*
239aca8a23dSJeff Layton * Move cache entry to end of LRU list, and queue the cleaner to run if it's
240aca8a23dSJeff Layton * not already scheduled.
2411da177e4SLinus Torvalds */
2421da177e4SLinus Torvalds static void
lru_put_end(struct nfsd_drc_bucket * b,struct nfsd_cacherep * rp)243e7421ce7SChuck Lever lru_put_end(struct nfsd_drc_bucket *b, struct nfsd_cacherep *rp)
2441da177e4SLinus Torvalds {
24556c2548bSJeff Layton rp->c_timestamp = jiffies;
246bedd4b61STrond Myklebust list_move_tail(&rp->c_lru, &b->lru_head);
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds
249378a6109SChuck Lever static noinline struct nfsd_drc_bucket *
nfsd_cache_bucket_find(__be32 xid,struct nfsd_net * nn)250378a6109SChuck Lever nfsd_cache_bucket_find(__be32 xid, struct nfsd_net *nn)
251378a6109SChuck Lever {
252378a6109SChuck Lever unsigned int hash = hash_32((__force u32)xid, nn->maskbits);
253378a6109SChuck Lever
254378a6109SChuck Lever return &nn->drc_hashtbl[hash];
255378a6109SChuck Lever }
256378a6109SChuck Lever
257a9507f6aSChuck Lever /*
258a9507f6aSChuck Lever * Remove and return no more than @max expired entries in bucket @b.
259a9507f6aSChuck Lever * If @max is zero, do not limit the number of removed entries.
260a9507f6aSChuck Lever */
261a9507f6aSChuck Lever static void
nfsd_prune_bucket_locked(struct nfsd_net * nn,struct nfsd_drc_bucket * b,unsigned int max,struct list_head * dispose)262a9507f6aSChuck Lever nfsd_prune_bucket_locked(struct nfsd_net *nn, struct nfsd_drc_bucket *b,
263a9507f6aSChuck Lever unsigned int max, struct list_head *dispose)
264a9507f6aSChuck Lever {
265a9507f6aSChuck Lever unsigned long expiry = jiffies - RC_EXPIRE;
266e7421ce7SChuck Lever struct nfsd_cacherep *rp, *tmp;
267a9507f6aSChuck Lever unsigned int freed = 0;
268a9507f6aSChuck Lever
269a9507f6aSChuck Lever lockdep_assert_held(&b->cache_lock);
270a9507f6aSChuck Lever
271a9507f6aSChuck Lever /* The bucket LRU is ordered oldest-first. */
272a9507f6aSChuck Lever list_for_each_entry_safe(rp, tmp, &b->lru_head, c_lru) {
273a9507f6aSChuck Lever /*
274a9507f6aSChuck Lever * Don't free entries attached to calls that are still
275a9507f6aSChuck Lever * in-progress, but do keep scanning the list.
276a9507f6aSChuck Lever */
277a9507f6aSChuck Lever if (rp->c_state == RC_INPROG)
278a9507f6aSChuck Lever continue;
279a9507f6aSChuck Lever
280a9507f6aSChuck Lever if (atomic_read(&nn->num_drc_entries) <= nn->max_drc_entries &&
281a9507f6aSChuck Lever time_before(expiry, rp->c_timestamp))
282a9507f6aSChuck Lever break;
283a9507f6aSChuck Lever
284a9507f6aSChuck Lever nfsd_cacherep_unlink_locked(nn, b, rp);
285a9507f6aSChuck Lever list_add(&rp->c_lru, dispose);
286a9507f6aSChuck Lever
287a9507f6aSChuck Lever if (max && ++freed > max)
288a9507f6aSChuck Lever break;
289a9507f6aSChuck Lever }
290a9507f6aSChuck Lever }
291a9507f6aSChuck Lever
292c135e126SChuck Lever /**
293c135e126SChuck Lever * nfsd_reply_cache_count - count_objects method for the DRC shrinker
294c135e126SChuck Lever * @shrink: our registered shrinker context
295c135e126SChuck Lever * @sc: garbage collection parameters
296c135e126SChuck Lever *
297c135e126SChuck Lever * Returns the total number of entries in the duplicate reply cache. To
298c135e126SChuck Lever * keep things simple and quick, this is not the number of expired entries
299c135e126SChuck Lever * in the cache (ie, the number that would be removed by a call to
300c135e126SChuck Lever * nfsd_reply_cache_scan).
3011b19453dSJeff Layton */
3021ab6c499SDave Chinner static unsigned long
nfsd_reply_cache_count(struct shrinker * shrink,struct shrink_control * sc)3031ab6c499SDave Chinner nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc)
304b4e7f2c9SJeff Layton {
3053ba75830SJ. Bruce Fields struct nfsd_net *nn = container_of(shrink,
3063ba75830SJ. Bruce Fields struct nfsd_net, nfsd_reply_cache_shrinker);
3073ba75830SJ. Bruce Fields
3083ba75830SJ. Bruce Fields return atomic_read(&nn->num_drc_entries);
309b4e7f2c9SJeff Layton }
310b4e7f2c9SJeff Layton
311c135e126SChuck Lever /**
312c135e126SChuck Lever * nfsd_reply_cache_scan - scan_objects method for the DRC shrinker
313c135e126SChuck Lever * @shrink: our registered shrinker context
314c135e126SChuck Lever * @sc: garbage collection parameters
315c135e126SChuck Lever *
316c135e126SChuck Lever * Free expired entries on each bucket's LRU list until we've released
317c135e126SChuck Lever * nr_to_scan freed objects. Nothing will be released if the cache
318c135e126SChuck Lever * has not exceeded it's max_drc_entries limit.
319c135e126SChuck Lever *
320c135e126SChuck Lever * Returns the number of entries released by this call.
321c135e126SChuck Lever */
3221ab6c499SDave Chinner static unsigned long
nfsd_reply_cache_scan(struct shrinker * shrink,struct shrink_control * sc)3231ab6c499SDave Chinner nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
3241ab6c499SDave Chinner {
3253ba75830SJ. Bruce Fields struct nfsd_net *nn = container_of(shrink,
3263ba75830SJ. Bruce Fields struct nfsd_net, nfsd_reply_cache_shrinker);
327c135e126SChuck Lever unsigned long freed = 0;
328c135e126SChuck Lever LIST_HEAD(dispose);
329c135e126SChuck Lever unsigned int i;
3303ba75830SJ. Bruce Fields
331c135e126SChuck Lever for (i = 0; i < nn->drc_hashsize; i++) {
332c135e126SChuck Lever struct nfsd_drc_bucket *b = &nn->drc_hashtbl[i];
333c135e126SChuck Lever
334c135e126SChuck Lever if (list_empty(&b->lru_head))
335c135e126SChuck Lever continue;
336c135e126SChuck Lever
337c135e126SChuck Lever spin_lock(&b->cache_lock);
338c135e126SChuck Lever nfsd_prune_bucket_locked(nn, b, 0, &dispose);
339c135e126SChuck Lever spin_unlock(&b->cache_lock);
340c135e126SChuck Lever
341c135e126SChuck Lever freed += nfsd_cacherep_dispose(&dispose);
342c135e126SChuck Lever if (freed > sc->nr_to_scan)
343c135e126SChuck Lever break;
3441ab6c499SDave Chinner }
345c135e126SChuck Lever
346c135e126SChuck Lever trace_nfsd_drc_gc(nn, freed);
347c135e126SChuck Lever return freed;
348c135e126SChuck Lever }
349c135e126SChuck Lever
350b79e3569SChuck Lever /**
351b79e3569SChuck Lever * nfsd_cache_csum - Checksum incoming NFS Call arguments
352b79e3569SChuck Lever * @buf: buffer containing a whole RPC Call message
353b79e3569SChuck Lever * @start: starting byte of the NFS Call header
354b79e3569SChuck Lever * @remaining: size of the NFS Call header, in bytes
355b79e3569SChuck Lever *
356b79e3569SChuck Lever * Compute a weak checksum of the leading bytes of an NFS procedure
357b79e3569SChuck Lever * call header to help verify that a retransmitted Call matches an
358b79e3569SChuck Lever * entry in the duplicate reply cache.
359b79e3569SChuck Lever *
360b79e3569SChuck Lever * To avoid assumptions about how the RPC message is laid out in
361b79e3569SChuck Lever * @buf and what else it might contain (eg, a GSS MIC suffix), the
362b79e3569SChuck Lever * caller passes us the exact location and length of the NFS Call
363b79e3569SChuck Lever * header.
364b79e3569SChuck Lever *
365b79e3569SChuck Lever * Returns a 32-bit checksum value, as defined in RFC 793.
36601a7decfSJeff Layton */
nfsd_cache_csum(struct xdr_buf * buf,unsigned int start,unsigned int remaining)367b79e3569SChuck Lever static __wsum nfsd_cache_csum(struct xdr_buf *buf, unsigned int start,
368b79e3569SChuck Lever unsigned int remaining)
36901a7decfSJeff Layton {
370b79e3569SChuck Lever unsigned int base, len;
371b79e3569SChuck Lever struct xdr_buf subbuf;
372b79e3569SChuck Lever __wsum csum = 0;
373b79e3569SChuck Lever void *p;
37401a7decfSJeff Layton int idx;
375b79e3569SChuck Lever
376b79e3569SChuck Lever if (remaining > RC_CSUMLEN)
377b79e3569SChuck Lever remaining = RC_CSUMLEN;
378b79e3569SChuck Lever if (xdr_buf_subsegment(buf, &subbuf, start, remaining))
379b79e3569SChuck Lever return csum;
38001a7decfSJeff Layton
38101a7decfSJeff Layton /* rq_arg.head first */
382b79e3569SChuck Lever if (subbuf.head[0].iov_len) {
383b79e3569SChuck Lever len = min_t(unsigned int, subbuf.head[0].iov_len, remaining);
384b79e3569SChuck Lever csum = csum_partial(subbuf.head[0].iov_base, len, csum);
385b79e3569SChuck Lever remaining -= len;
386b79e3569SChuck Lever }
38701a7decfSJeff Layton
38801a7decfSJeff Layton /* Continue into page array */
389b79e3569SChuck Lever idx = subbuf.page_base / PAGE_SIZE;
390b79e3569SChuck Lever base = subbuf.page_base & ~PAGE_MASK;
391b79e3569SChuck Lever while (remaining) {
392b79e3569SChuck Lever p = page_address(subbuf.pages[idx]) + base;
393b79e3569SChuck Lever len = min_t(unsigned int, PAGE_SIZE - base, remaining);
39401a7decfSJeff Layton csum = csum_partial(p, len, csum);
395b79e3569SChuck Lever remaining -= len;
39601a7decfSJeff Layton base = 0;
39701a7decfSJeff Layton ++idx;
39801a7decfSJeff Layton }
39901a7decfSJeff Layton return csum;
40001a7decfSJeff Layton }
40101a7decfSJeff Layton
402ed00c2f6STrond Myklebust static int
nfsd_cache_key_cmp(const struct nfsd_cacherep * key,const struct nfsd_cacherep * rp,struct nfsd_net * nn)403e7421ce7SChuck Lever nfsd_cache_key_cmp(const struct nfsd_cacherep *key,
404e7421ce7SChuck Lever const struct nfsd_cacherep *rp, struct nfsd_net *nn)
4059dc56143SJeff Layton {
406ed00c2f6STrond Myklebust if (key->c_key.k_xid == rp->c_key.k_xid &&
4070b175b18SChuck Lever key->c_key.k_csum != rp->c_key.k_csum) {
408e567b98cSAmir Goldstein nfsd_stats_payload_misses_inc(nn);
4090b175b18SChuck Lever trace_nfsd_drc_mismatch(nn, key, rp);
4100b175b18SChuck Lever }
4119dc56143SJeff Layton
412ed00c2f6STrond Myklebust return memcmp(&key->c_key, &rp->c_key, sizeof(key->c_key));
4139dc56143SJeff Layton }
4149dc56143SJeff Layton
41501a7decfSJeff Layton /*
416a4a3ec32SJeff Layton * Search the request hash for an entry that matches the given rqstp.
417a4a3ec32SJeff Layton * Must be called with cache_lock held. Returns the found entry or
41876ecec21STrond Myklebust * inserts an empty key on failure.
419a4a3ec32SJeff Layton */
420e7421ce7SChuck Lever static struct nfsd_cacherep *
nfsd_cache_insert(struct nfsd_drc_bucket * b,struct nfsd_cacherep * key,struct nfsd_net * nn)421e7421ce7SChuck Lever nfsd_cache_insert(struct nfsd_drc_bucket *b, struct nfsd_cacherep *key,
4223ba75830SJ. Bruce Fields struct nfsd_net *nn)
423a4a3ec32SJeff Layton {
424e7421ce7SChuck Lever struct nfsd_cacherep *rp, *ret = key;
425736c6625STrond Myklebust struct rb_node **p = &b->rb_head.rb_node,
426736c6625STrond Myklebust *parent = NULL;
42798d821bdSJeff Layton unsigned int entries = 0;
428736c6625STrond Myklebust int cmp;
429a4a3ec32SJeff Layton
430736c6625STrond Myklebust while (*p != NULL) {
43198d821bdSJeff Layton ++entries;
432736c6625STrond Myklebust parent = *p;
433e7421ce7SChuck Lever rp = rb_entry(parent, struct nfsd_cacherep, c_node);
43498d821bdSJeff Layton
4353ba75830SJ. Bruce Fields cmp = nfsd_cache_key_cmp(key, rp, nn);
436736c6625STrond Myklebust if (cmp < 0)
437736c6625STrond Myklebust p = &parent->rb_left;
438736c6625STrond Myklebust else if (cmp > 0)
439736c6625STrond Myklebust p = &parent->rb_right;
440736c6625STrond Myklebust else {
441736c6625STrond Myklebust ret = rp;
442736c6625STrond Myklebust goto out;
443736c6625STrond Myklebust }
444736c6625STrond Myklebust }
445736c6625STrond Myklebust rb_link_node(&key->c_node, parent, p);
446736c6625STrond Myklebust rb_insert_color(&key->c_node, &b->rb_head);
447736c6625STrond Myklebust out:
44898d821bdSJeff Layton /* tally hash chain length stats */
4493ba75830SJ. Bruce Fields if (entries > nn->longest_chain) {
4503ba75830SJ. Bruce Fields nn->longest_chain = entries;
4513ba75830SJ. Bruce Fields nn->longest_chain_cachesize = atomic_read(&nn->num_drc_entries);
4523ba75830SJ. Bruce Fields } else if (entries == nn->longest_chain) {
45398d821bdSJeff Layton /* prefer to keep the smallest cachesize possible here */
4543ba75830SJ. Bruce Fields nn->longest_chain_cachesize = min_t(unsigned int,
4553ba75830SJ. Bruce Fields nn->longest_chain_cachesize,
4563ba75830SJ. Bruce Fields atomic_read(&nn->num_drc_entries));
45798d821bdSJeff Layton }
45898d821bdSJeff Layton
45976ecec21STrond Myklebust lru_put_end(b, ret);
46098d821bdSJeff Layton return ret;
461a4a3ec32SJeff Layton }
462a4a3ec32SJeff Layton
4630b175b18SChuck Lever /**
4640b175b18SChuck Lever * nfsd_cache_lookup - Find an entry in the duplicate reply cache
4650b175b18SChuck Lever * @rqstp: Incoming Call to find
466b79e3569SChuck Lever * @start: starting byte in @rqstp->rq_arg of the NFS Call header
467b79e3569SChuck Lever * @len: size of the NFS Call header, in bytes
468cb18eca4SChuck Lever * @cacherep: OUT: DRC entry for this request
4690b175b18SChuck Lever *
4701da177e4SLinus Torvalds * Try to find an entry matching the current call in the cache. When none
4711ac83629SJeff Layton * is found, we try to grab the oldest expired entry off the LRU list. If
4721ac83629SJeff Layton * a suitable one isn't there, then drop the cache_lock and allocate a
4731ac83629SJeff Layton * new one, then search again in case one got inserted while this thread
4741ac83629SJeff Layton * didn't hold the lock.
4750b175b18SChuck Lever *
4760b175b18SChuck Lever * Return values:
4770b175b18SChuck Lever * %RC_DOIT: Process the request normally
4780b175b18SChuck Lever * %RC_REPLY: Reply from cache
4790b175b18SChuck Lever * %RC_DROPIT: Do not process the request further
4801da177e4SLinus Torvalds */
nfsd_cache_lookup(struct svc_rqst * rqstp,unsigned int start,unsigned int len,struct nfsd_cacherep ** cacherep)481b79e3569SChuck Lever int nfsd_cache_lookup(struct svc_rqst *rqstp, unsigned int start,
482b79e3569SChuck Lever unsigned int len, struct nfsd_cacherep **cacherep)
4831da177e4SLinus Torvalds {
484*b670a598SJosef Bacik struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
485e7421ce7SChuck Lever struct nfsd_cacherep *rp, *found;
48601a7decfSJeff Layton __wsum csum;
4870f29ce32SChuck Lever struct nfsd_drc_bucket *b;
4881091006cSJ. Bruce Fields int type = rqstp->rq_cachetype;
489a9507f6aSChuck Lever unsigned long freed;
490a9507f6aSChuck Lever LIST_HEAD(dispose);
4910b9ea37fSJeff Layton int rtn = RC_DOIT;
4921da177e4SLinus Torvalds
49313cc8a78SJeff Layton if (type == RC_NOCACHE) {
494*b670a598SJosef Bacik nfsd_stats_rc_nocache_inc(nn);
4950b175b18SChuck Lever goto out;
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds
498b79e3569SChuck Lever csum = nfsd_cache_csum(&rqstp->rq_arg, start, len);
49901a7decfSJeff Layton
5000b9ea37fSJeff Layton /*
5010b9ea37fSJeff Layton * Since the common case is a cache miss followed by an insert,
502a0ef5e19SJeff Layton * preallocate an entry.
5030b9ea37fSJeff Layton */
504ff0d1693SChuck Lever rp = nfsd_cacherep_alloc(rqstp, csum, nn);
5050b175b18SChuck Lever if (!rp)
5060b175b18SChuck Lever goto out;
5070338dd15SJeff Layton
5080f29ce32SChuck Lever b = nfsd_cache_bucket_find(rqstp->rq_xid, nn);
50976ecec21STrond Myklebust spin_lock(&b->cache_lock);
5103ba75830SJ. Bruce Fields found = nfsd_cache_insert(b, rp, nn);
511add1511cSChuck Lever if (found != rp)
5121da177e4SLinus Torvalds goto found_entry;
513cb18eca4SChuck Lever *cacherep = rp;
5141da177e4SLinus Torvalds rp->c_state = RC_INPROG;
515a9507f6aSChuck Lever nfsd_prune_bucket_locked(nn, b, 3, &dispose);
516a9507f6aSChuck Lever spin_unlock(&b->cache_lock);
5171da177e4SLinus Torvalds
518a9507f6aSChuck Lever freed = nfsd_cacherep_dispose(&dispose);
519a9507f6aSChuck Lever trace_nfsd_drc_gc(nn, freed);
520a9507f6aSChuck Lever
521*b670a598SJosef Bacik nfsd_stats_rc_misses_inc(nn);
5223ba75830SJ. Bruce Fields atomic_inc(&nn->num_drc_entries);
523e567b98cSAmir Goldstein nfsd_stats_drc_mem_usage_add(nn, sizeof(*rp));
524a9507f6aSChuck Lever goto out;
5251da177e4SLinus Torvalds
5261da177e4SLinus Torvalds found_entry:
5271da177e4SLinus Torvalds /* We found a matching entry which is either in progress or done. */
528add1511cSChuck Lever nfsd_reply_cache_free_locked(NULL, rp, nn);
529*b670a598SJosef Bacik nfsd_stats_rc_hits_inc(nn);
5301da177e4SLinus Torvalds rtn = RC_DROPIT;
531add1511cSChuck Lever rp = found;
53276ecec21STrond Myklebust
5337e5d0e0dSTrond Myklebust /* Request being processed */
5347e5d0e0dSTrond Myklebust if (rp->c_state == RC_INPROG)
5350b175b18SChuck Lever goto out_trace;
5361da177e4SLinus Torvalds
5371da177e4SLinus Torvalds /* From the hall of fame of impractical attacks:
5381da177e4SLinus Torvalds * Is this a user who tries to snoop on the cache? */
5391da177e4SLinus Torvalds rtn = RC_DOIT;
5404d152e2cSJeff Layton if (!test_bit(RQ_SECURE, &rqstp->rq_flags) && rp->c_secure)
5410b175b18SChuck Lever goto out_trace;
5421da177e4SLinus Torvalds
5431da177e4SLinus Torvalds /* Compose RPC reply header */
5441da177e4SLinus Torvalds switch (rp->c_type) {
5451da177e4SLinus Torvalds case RC_NOCACHE:
5461da177e4SLinus Torvalds break;
5471da177e4SLinus Torvalds case RC_REPLSTAT:
5488dd41d70SChuck Lever xdr_stream_encode_be32(&rqstp->rq_res_stream, rp->c_replstat);
5491da177e4SLinus Torvalds rtn = RC_REPLY;
5501da177e4SLinus Torvalds break;
5511da177e4SLinus Torvalds case RC_REPLBUFF:
5521da177e4SLinus Torvalds if (!nfsd_cache_append(rqstp, &rp->c_replvec))
5530b175b18SChuck Lever goto out_unlock; /* should not happen */
5541da177e4SLinus Torvalds rtn = RC_REPLY;
5551da177e4SLinus Torvalds break;
5561da177e4SLinus Torvalds default:
557c25bf185SJ. Bruce Fields WARN_ONCE(1, "nfsd: bad repcache type %d\n", rp->c_type);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds
5600b175b18SChuck Lever out_trace:
5610b175b18SChuck Lever trace_nfsd_drc_found(nn, rqstp, rtn);
562a9507f6aSChuck Lever out_unlock:
563a9507f6aSChuck Lever spin_unlock(&b->cache_lock);
564a9507f6aSChuck Lever out:
565a9507f6aSChuck Lever return rtn;
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds
5680b175b18SChuck Lever /**
5690b175b18SChuck Lever * nfsd_cache_update - Update an entry in the duplicate reply cache.
5700b175b18SChuck Lever * @rqstp: svc_rqst with a finished Reply
571cb18eca4SChuck Lever * @rp: IN: DRC entry for this request
5720b175b18SChuck Lever * @cachetype: which cache to update
573cee4db19SChuck Lever * @statp: pointer to Reply's NFS status code, or NULL
5740b175b18SChuck Lever *
5750b175b18SChuck Lever * This is called from nfsd_dispatch when the procedure has been
5760b175b18SChuck Lever * executed and the complete reply is in rqstp->rq_res.
5771da177e4SLinus Torvalds *
5781da177e4SLinus Torvalds * We're copying around data here rather than swapping buffers because
5791da177e4SLinus Torvalds * the toplevel loop requires max-sized buffers, which would be a waste
5801da177e4SLinus Torvalds * of memory for a cache with a max reply size of 100 bytes (diropokres).
5811da177e4SLinus Torvalds *
5821da177e4SLinus Torvalds * If we should start to use different types of cache entries tailored
5831da177e4SLinus Torvalds * specifically for attrstat and fh's, we may save even more space.
5841da177e4SLinus Torvalds *
5851da177e4SLinus Torvalds * Also note that a cachetype of RC_NOCACHE can legally be passed when
5861da177e4SLinus Torvalds * nfsd failed to encode a reply that otherwise would have been cached.
5871da177e4SLinus Torvalds * In this case, nfsd_cache_update is called with statp == NULL.
5881da177e4SLinus Torvalds */
nfsd_cache_update(struct svc_rqst * rqstp,struct nfsd_cacherep * rp,int cachetype,__be32 * statp)589e7421ce7SChuck Lever void nfsd_cache_update(struct svc_rqst *rqstp, struct nfsd_cacherep *rp,
590cb18eca4SChuck Lever int cachetype, __be32 *statp)
5911da177e4SLinus Torvalds {
5923ba75830SJ. Bruce Fields struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5931da177e4SLinus Torvalds struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
594bedd4b61STrond Myklebust struct nfsd_drc_bucket *b;
5951da177e4SLinus Torvalds int len;
5966c6910cdSJeff Layton size_t bufsize = 0;
5971da177e4SLinus Torvalds
59813cc8a78SJeff Layton if (!rp)
5991da177e4SLinus Torvalds return;
6001da177e4SLinus Torvalds
601378a6109SChuck Lever b = nfsd_cache_bucket_find(rp->c_key.k_xid, nn);
602bedd4b61STrond Myklebust
6031da177e4SLinus Torvalds len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
6041da177e4SLinus Torvalds len >>= 2;
6051da177e4SLinus Torvalds
6061da177e4SLinus Torvalds /* Don't cache excessive amounts of data and XDR failures */
6071da177e4SLinus Torvalds if (!statp || len > (256 >> 2)) {
6083ba75830SJ. Bruce Fields nfsd_reply_cache_free(b, rp, nn);
6091da177e4SLinus Torvalds return;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds
6121da177e4SLinus Torvalds switch (cachetype) {
6131da177e4SLinus Torvalds case RC_REPLSTAT:
6141da177e4SLinus Torvalds if (len != 1)
6151da177e4SLinus Torvalds printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
6161da177e4SLinus Torvalds rp->c_replstat = *statp;
6171da177e4SLinus Torvalds break;
6181da177e4SLinus Torvalds case RC_REPLBUFF:
6191da177e4SLinus Torvalds cachv = &rp->c_replvec;
6206c6910cdSJeff Layton bufsize = len << 2;
6216c6910cdSJeff Layton cachv->iov_base = kmalloc(bufsize, GFP_KERNEL);
6221da177e4SLinus Torvalds if (!cachv->iov_base) {
6233ba75830SJ. Bruce Fields nfsd_reply_cache_free(b, rp, nn);
6241da177e4SLinus Torvalds return;
6251da177e4SLinus Torvalds }
6266c6910cdSJeff Layton cachv->iov_len = bufsize;
6276c6910cdSJeff Layton memcpy(cachv->iov_base, statp, bufsize);
6281da177e4SLinus Torvalds break;
6292c6b691cSJeff Layton case RC_NOCACHE:
6303ba75830SJ. Bruce Fields nfsd_reply_cache_free(b, rp, nn);
6312c6b691cSJeff Layton return;
6321da177e4SLinus Torvalds }
63389a26b3dSTrond Myklebust spin_lock(&b->cache_lock);
634e567b98cSAmir Goldstein nfsd_stats_drc_mem_usage_add(nn, bufsize);
635bedd4b61STrond Myklebust lru_put_end(b, rp);
6364d152e2cSJeff Layton rp->c_secure = test_bit(RQ_SECURE, &rqstp->rq_flags);
6371da177e4SLinus Torvalds rp->c_type = cachetype;
6381da177e4SLinus Torvalds rp->c_state = RC_DONE;
63989a26b3dSTrond Myklebust spin_unlock(&b->cache_lock);
6401da177e4SLinus Torvalds return;
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds
6431da177e4SLinus Torvalds static int
nfsd_cache_append(struct svc_rqst * rqstp,struct kvec * data)6441da177e4SLinus Torvalds nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
6451da177e4SLinus Torvalds {
64668eb0889SChuck Lever __be32 *p;
6471da177e4SLinus Torvalds
64868eb0889SChuck Lever p = xdr_reserve_space(&rqstp->rq_res_stream, data->iov_len);
64968eb0889SChuck Lever if (unlikely(!p))
65068eb0889SChuck Lever return false;
65168eb0889SChuck Lever memcpy(p, data->iov_base, data->iov_len);
65268eb0889SChuck Lever xdr_commit_encode(&rqstp->rq_res_stream);
65368eb0889SChuck Lever return true;
6541da177e4SLinus Torvalds }
655a2f999a3SJeff Layton
656a2f999a3SJeff Layton /*
657a2f999a3SJeff Layton * Note that fields may be added, removed or reordered in the future. Programs
658a2f999a3SJeff Layton * scraping this file for info should test the labels to ensure they're
659a2f999a3SJeff Layton * getting the correct field.
660a2f999a3SJeff Layton */
nfsd_reply_cache_stats_show(struct seq_file * m,void * v)66164776611SChenXiaoSong int nfsd_reply_cache_stats_show(struct seq_file *m, void *v)
662a2f999a3SJeff Layton {
66364776611SChenXiaoSong struct nfsd_net *nn = net_generic(file_inode(m->file)->i_sb->s_fs_info,
66464776611SChenXiaoSong nfsd_net_id);
6653ba75830SJ. Bruce Fields
6663ba75830SJ. Bruce Fields seq_printf(m, "max entries: %u\n", nn->max_drc_entries);
66731e60f52STrond Myklebust seq_printf(m, "num entries: %u\n",
6683ba75830SJ. Bruce Fields atomic_read(&nn->num_drc_entries));
6693ba75830SJ. Bruce Fields seq_printf(m, "hash buckets: %u\n", 1 << nn->maskbits);
670e567b98cSAmir Goldstein seq_printf(m, "mem usage: %lld\n",
6715b3a1ecfSJosef Bacik percpu_counter_sum_positive(&nn->counter[NFSD_STATS_DRC_MEM_USAGE]));
672e567b98cSAmir Goldstein seq_printf(m, "cache hits: %lld\n",
673*b670a598SJosef Bacik percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_HITS]));
674e567b98cSAmir Goldstein seq_printf(m, "cache misses: %lld\n",
675*b670a598SJosef Bacik percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_MISSES]));
676e567b98cSAmir Goldstein seq_printf(m, "not cached: %lld\n",
677*b670a598SJosef Bacik percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_NOCACHE]));
678e567b98cSAmir Goldstein seq_printf(m, "payload misses: %lld\n",
6795b3a1ecfSJosef Bacik percpu_counter_sum_positive(&nn->counter[NFSD_STATS_PAYLOAD_MISSES]));
6803ba75830SJ. Bruce Fields seq_printf(m, "longest chain len: %u\n", nn->longest_chain);
6813ba75830SJ. Bruce Fields seq_printf(m, "cachesize at longest: %u\n", nn->longest_chain_cachesize);
682a2f999a3SJeff Layton return 0;
683a2f999a3SJeff Layton }
684