Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Jeff Layton | 8f97514 | 2016-10-26 07:26:40 -0400 | [diff] [blame] | 13 | #include <linux/vmalloc.h> |
Jeff Layton | 5976687 | 2013-02-04 12:50:00 -0500 | [diff] [blame] | 14 | #include <linux/sunrpc/addr.h> |
Jeff Layton | 0338dd1 | 2013-02-04 08:18:02 -0500 | [diff] [blame] | 15 | #include <linux/highmem.h> |
Jeff Layton | 0733c7b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 16 | #include <linux/log2.h> |
| 17 | #include <linux/hash.h> |
Jeff Layton | 01a7dec | 2013-02-04 11:57:27 -0500 | [diff] [blame] | 18 | #include <net/checksum.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 20 | #include "nfsd.h" |
| 21 | #include "cache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Jeff Layton | 0338dd1 | 2013-02-04 08:18:02 -0500 | [diff] [blame] | 23 | #define NFSDDBG_FACILITY NFSDDBG_REPCACHE |
| 24 | |
Jeff Layton | 0733c7b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 25 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Trond Myklebust | 7142b98 | 2014-08-06 13:44:20 -0400 | [diff] [blame] | 32 | struct nfsd_drc_bucket { |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 33 | struct rb_root rb_head; |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 34 | struct list_head lru_head; |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 35 | spinlock_t cache_lock; |
Trond Myklebust | 7142b98 | 2014-08-06 13:44:20 -0400 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static struct nfsd_drc_bucket *drc_hashtbl; |
Jeff Layton | 8a8bc40 | 2013-01-28 14:41:10 -0500 | [diff] [blame] | 39 | static struct kmem_cache *drc_slab; |
Jeff Layton | 9dc5614 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 40 | |
| 41 | /* max number of entries allowed in the cache */ |
Jeff Layton | 0338dd1 | 2013-02-04 08:18:02 -0500 | [diff] [blame] | 42 | static unsigned int max_drc_entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Jeff Layton | 0733c7b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 44 | /* number of significant bits in the hash value */ |
| 45 | static unsigned int maskbits; |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 46 | static unsigned int drc_hashsize; |
Jeff Layton | 0733c7b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 47 | |
Greg Banks | fca4217 | 2009-04-01 07:28:13 +1100 | [diff] [blame] | 48 | /* |
Jeff Layton | 9dc5614 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 49 | * Stats and other tracking of on the duplicate reply cache. All of these and |
| 50 | * the "rc" fields in nfsdstats are protected by the cache_lock |
| 51 | */ |
| 52 | |
| 53 | /* total number of entries */ |
Trond Myklebust | 31e60f5 | 2014-08-06 13:44:23 -0400 | [diff] [blame] | 54 | static atomic_t num_drc_entries; |
Jeff Layton | 9dc5614 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 55 | |
| 56 | /* cache misses due only to checksum comparison failures */ |
| 57 | static unsigned int payload_misses; |
| 58 | |
Jeff Layton | 6c6910c | 2013-03-27 10:15:38 -0400 | [diff] [blame] | 59 | /* amount of memory (in bytes) currently consumed by the DRC */ |
| 60 | static unsigned int drc_mem_usage; |
| 61 | |
Jeff Layton | 98d821b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 62 | /* longest hash chain seen */ |
| 63 | static unsigned int longest_chain; |
| 64 | |
| 65 | /* size of cache when we saw the longest hash chain */ |
| 66 | static unsigned int longest_chain_cachesize; |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec); |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 69 | static unsigned long nfsd_reply_cache_count(struct shrinker *shrink, |
| 70 | struct shrink_control *sc); |
| 71 | static unsigned long nfsd_reply_cache_scan(struct shrinker *shrink, |
| 72 | struct shrink_control *sc); |
Jeff Layton | b4e7f2c | 2013-02-04 08:18:06 -0500 | [diff] [blame] | 73 | |
Wei Yongjun | c8c797f | 2013-04-05 21:22:39 +0800 | [diff] [blame] | 74 | static struct shrinker nfsd_reply_cache_shrinker = { |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 75 | .scan_objects = nfsd_reply_cache_scan, |
| 76 | .count_objects = nfsd_reply_cache_count, |
Jeff Layton | b4e7f2c | 2013-02-04 08:18:06 -0500 | [diff] [blame] | 77 | .seeks = 1, |
| 78 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Greg Banks | fca4217 | 2009-04-01 07:28:13 +1100 | [diff] [blame] | 80 | /* |
Jeff Layton | 0338dd1 | 2013-02-04 08:18:02 -0500 | [diff] [blame] | 81 | * Put a cap on the size of the DRC based on the amount of available |
| 82 | * low memory in the machine. |
| 83 | * |
| 84 | * 64MB: 8192 |
| 85 | * 128MB: 11585 |
| 86 | * 256MB: 16384 |
| 87 | * 512MB: 23170 |
| 88 | * 1GB: 32768 |
| 89 | * 2GB: 46340 |
| 90 | * 4GB: 65536 |
| 91 | * 8GB: 92681 |
| 92 | * 16GB: 131072 |
| 93 | * |
| 94 | * ...with a hard cap of 256k entries. In the worst case, each entry will be |
| 95 | * ~1k, so the above numbers should give a rough max of the amount of memory |
| 96 | * used in k. |
| 97 | */ |
| 98 | static unsigned int |
| 99 | nfsd_cache_size_limit(void) |
| 100 | { |
| 101 | unsigned int limit; |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 102 | unsigned long low_pages = totalram_pages() - totalhigh_pages(); |
Jeff Layton | 0338dd1 | 2013-02-04 08:18:02 -0500 | [diff] [blame] | 103 | |
| 104 | limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10); |
| 105 | return min_t(unsigned int, limit, 256*1024); |
| 106 | } |
| 107 | |
Jeff Layton | 0733c7b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 108 | /* |
| 109 | * Compute the number of hash buckets we need. Divide the max cachesize by |
| 110 | * the "target" max bucket size, and round up to next power of two. |
| 111 | */ |
| 112 | static unsigned int |
| 113 | nfsd_hashsize(unsigned int limit) |
| 114 | { |
| 115 | return roundup_pow_of_two(limit / TARGET_BUCKET_SIZE); |
| 116 | } |
| 117 | |
Trond Myklebust | 7142b98 | 2014-08-06 13:44:20 -0400 | [diff] [blame] | 118 | static u32 |
| 119 | nfsd_cache_hash(__be32 xid) |
| 120 | { |
| 121 | return hash_32(be32_to_cpu(xid), maskbits); |
| 122 | } |
| 123 | |
Jeff Layton | f09841f | 2013-01-28 14:41:11 -0500 | [diff] [blame] | 124 | static struct svc_cacherep * |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 125 | nfsd_reply_cache_alloc(struct svc_rqst *rqstp, __wsum csum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
| 127 | struct svc_cacherep *rp; |
Jeff Layton | f09841f | 2013-01-28 14:41:11 -0500 | [diff] [blame] | 128 | |
| 129 | rp = kmem_cache_alloc(drc_slab, GFP_KERNEL); |
| 130 | if (rp) { |
| 131 | rp->c_state = RC_UNUSED; |
| 132 | rp->c_type = RC_NOCACHE; |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 133 | RB_CLEAR_NODE(&rp->c_node); |
Jeff Layton | f09841f | 2013-01-28 14:41:11 -0500 | [diff] [blame] | 134 | INIT_LIST_HEAD(&rp->c_lru); |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 135 | |
Trond Myklebust | ed00c2f | 2018-10-03 13:11:51 -0400 | [diff] [blame] | 136 | memset(&rp->c_key, 0, sizeof(rp->c_key)); |
| 137 | rp->c_key.k_xid = rqstp->rq_xid; |
| 138 | rp->c_key.k_proc = rqstp->rq_proc; |
| 139 | rpc_copy_addr((struct sockaddr *)&rp->c_key.k_addr, svc_addr(rqstp)); |
| 140 | rpc_set_port((struct sockaddr *)&rp->c_key.k_addr, rpc_get_port(svc_addr(rqstp))); |
| 141 | rp->c_key.k_prot = rqstp->rq_prot; |
| 142 | rp->c_key.k_vers = rqstp->rq_vers; |
| 143 | rp->c_key.k_len = rqstp->rq_arg.len; |
| 144 | rp->c_key.k_csum = csum; |
Jeff Layton | f09841f | 2013-01-28 14:41:11 -0500 | [diff] [blame] | 145 | } |
| 146 | return rp; |
| 147 | } |
| 148 | |
| 149 | static void |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 150 | nfsd_reply_cache_free_locked(struct nfsd_drc_bucket *b, struct svc_cacherep *rp) |
Jeff Layton | f09841f | 2013-01-28 14:41:11 -0500 | [diff] [blame] | 151 | { |
Jeff Layton | 6c6910c | 2013-03-27 10:15:38 -0400 | [diff] [blame] | 152 | if (rp->c_type == RC_REPLBUFF && rp->c_replvec.iov_base) { |
| 153 | drc_mem_usage -= rp->c_replvec.iov_len; |
Jeff Layton | f09841f | 2013-01-28 14:41:11 -0500 | [diff] [blame] | 154 | kfree(rp->c_replvec.iov_base); |
Jeff Layton | 6c6910c | 2013-03-27 10:15:38 -0400 | [diff] [blame] | 155 | } |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 156 | if (rp->c_state != RC_UNUSED) { |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 157 | rb_erase(&rp->c_node, &b->rb_head); |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 158 | list_del(&rp->c_lru); |
| 159 | atomic_dec(&num_drc_entries); |
| 160 | drc_mem_usage -= sizeof(*rp); |
| 161 | } |
Jeff Layton | f09841f | 2013-01-28 14:41:11 -0500 | [diff] [blame] | 162 | kmem_cache_free(drc_slab, rp); |
| 163 | } |
| 164 | |
Jeff Layton | 2c6b691 | 2013-02-04 08:18:04 -0500 | [diff] [blame] | 165 | static void |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 166 | nfsd_reply_cache_free(struct nfsd_drc_bucket *b, struct svc_cacherep *rp) |
Jeff Layton | 2c6b691 | 2013-02-04 08:18:04 -0500 | [diff] [blame] | 167 | { |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 168 | spin_lock(&b->cache_lock); |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 169 | nfsd_reply_cache_free_locked(b, rp); |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 170 | spin_unlock(&b->cache_lock); |
Jeff Layton | 2c6b691 | 2013-02-04 08:18:04 -0500 | [diff] [blame] | 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | int nfsd_reply_cache_init(void) |
| 174 | { |
Jeff Layton | 0733c7b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 175 | unsigned int hashsize; |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 176 | unsigned int i; |
Kinglong Mee | a68465c | 2015-03-19 19:48:31 +0800 | [diff] [blame] | 177 | int status = 0; |
Jeff Layton | 0733c7b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 178 | |
Jeff Layton | ac534ff | 2013-03-15 09:16:29 -0400 | [diff] [blame] | 179 | max_drc_entries = nfsd_cache_size_limit(); |
Trond Myklebust | 31e60f5 | 2014-08-06 13:44:23 -0400 | [diff] [blame] | 180 | atomic_set(&num_drc_entries, 0); |
Jeff Layton | 0733c7b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 181 | hashsize = nfsd_hashsize(max_drc_entries); |
| 182 | maskbits = ilog2(hashsize); |
Jeff Layton | ac534ff | 2013-03-15 09:16:29 -0400 | [diff] [blame] | 183 | |
Kinglong Mee | a68465c | 2015-03-19 19:48:31 +0800 | [diff] [blame] | 184 | status = register_shrinker(&nfsd_reply_cache_shrinker); |
| 185 | if (status) |
| 186 | return status; |
| 187 | |
Jeff Layton | 8a8bc40 | 2013-01-28 14:41:10 -0500 | [diff] [blame] | 188 | drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep), |
| 189 | 0, 0, NULL); |
| 190 | if (!drc_slab) |
| 191 | goto out_nomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Trond Myklebust | 7142b98 | 2014-08-06 13:44:20 -0400 | [diff] [blame] | 193 | drc_hashtbl = kcalloc(hashsize, sizeof(*drc_hashtbl), GFP_KERNEL); |
Jeff Layton | 8f97514 | 2016-10-26 07:26:40 -0400 | [diff] [blame] | 194 | if (!drc_hashtbl) { |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 195 | drc_hashtbl = vzalloc(array_size(hashsize, |
| 196 | sizeof(*drc_hashtbl))); |
Jeff Layton | 8f97514 | 2016-10-26 07:26:40 -0400 | [diff] [blame] | 197 | if (!drc_hashtbl) |
| 198 | goto out_nomem; |
| 199 | } |
| 200 | |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 201 | for (i = 0; i < hashsize; i++) { |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 202 | INIT_LIST_HEAD(&drc_hashtbl[i].lru_head); |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 203 | spin_lock_init(&drc_hashtbl[i].cache_lock); |
| 204 | } |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 205 | drc_hashsize = hashsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
J. Bruce Fields | d5c3428 | 2007-11-09 14:10:56 -0500 | [diff] [blame] | 207 | return 0; |
| 208 | out_nomem: |
| 209 | printk(KERN_ERR "nfsd: failed to allocate reply cache\n"); |
| 210 | nfsd_reply_cache_shutdown(); |
| 211 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
J. Bruce Fields | d5c3428 | 2007-11-09 14:10:56 -0500 | [diff] [blame] | 214 | void nfsd_reply_cache_shutdown(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
| 216 | struct svc_cacherep *rp; |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 217 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Jeff Layton | b4e7f2c | 2013-02-04 08:18:06 -0500 | [diff] [blame] | 219 | unregister_shrinker(&nfsd_reply_cache_shrinker); |
Jeff Layton | aca8a23 | 2013-02-04 08:18:05 -0500 | [diff] [blame] | 220 | |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 221 | for (i = 0; i < drc_hashsize; i++) { |
| 222 | struct list_head *head = &drc_hashtbl[i].lru_head; |
| 223 | while (!list_empty(head)) { |
| 224 | rp = list_first_entry(head, struct svc_cacherep, c_lru); |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 225 | nfsd_reply_cache_free_locked(&drc_hashtbl[i], rp); |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Jeff Layton | 8f97514 | 2016-10-26 07:26:40 -0400 | [diff] [blame] | 229 | kvfree(drc_hashtbl); |
Trond Myklebust | 7142b98 | 2014-08-06 13:44:20 -0400 | [diff] [blame] | 230 | drc_hashtbl = NULL; |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 231 | drc_hashsize = 0; |
Jeff Layton | 8a8bc40 | 2013-01-28 14:41:10 -0500 | [diff] [blame] | 232 | |
Julia Lawall | e79017d | 2015-09-13 14:15:15 +0200 | [diff] [blame] | 233 | kmem_cache_destroy(drc_slab); |
| 234 | drc_slab = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* |
Jeff Layton | aca8a23 | 2013-02-04 08:18:05 -0500 | [diff] [blame] | 238 | * Move cache entry to end of LRU list, and queue the cleaner to run if it's |
| 239 | * not already scheduled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | */ |
| 241 | static void |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 242 | lru_put_end(struct nfsd_drc_bucket *b, struct svc_cacherep *rp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
Jeff Layton | 56c2548 | 2013-02-04 08:18:00 -0500 | [diff] [blame] | 244 | rp->c_timestamp = jiffies; |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 245 | list_move_tail(&rp->c_lru, &b->lru_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 248 | static long |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 249 | prune_bucket(struct nfsd_drc_bucket *b) |
Jeff Layton | aca8a23 | 2013-02-04 08:18:05 -0500 | [diff] [blame] | 250 | { |
| 251 | struct svc_cacherep *rp, *tmp; |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 252 | long freed = 0; |
Jeff Layton | aca8a23 | 2013-02-04 08:18:05 -0500 | [diff] [blame] | 253 | |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 254 | list_for_each_entry_safe(rp, tmp, &b->lru_head, c_lru) { |
Jeff Layton | 1b19453 | 2014-06-05 09:45:00 -0400 | [diff] [blame] | 255 | /* |
| 256 | * Don't free entries attached to calls that are still |
| 257 | * in-progress, but do keep scanning the list. |
| 258 | */ |
| 259 | if (rp->c_state == RC_INPROG) |
| 260 | continue; |
Trond Myklebust | 31e60f5 | 2014-08-06 13:44:23 -0400 | [diff] [blame] | 261 | if (atomic_read(&num_drc_entries) <= max_drc_entries && |
Jeff Layton | 1b19453 | 2014-06-05 09:45:00 -0400 | [diff] [blame] | 262 | time_before(jiffies, rp->c_timestamp + RC_EXPIRE)) |
Jeff Layton | aca8a23 | 2013-02-04 08:18:05 -0500 | [diff] [blame] | 263 | break; |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 264 | nfsd_reply_cache_free_locked(b, rp); |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 265 | freed++; |
Jeff Layton | aca8a23 | 2013-02-04 08:18:05 -0500 | [diff] [blame] | 266 | } |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 267 | return freed; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Walk the LRU list and prune off entries that are older than RC_EXPIRE. |
| 272 | * Also prune the oldest ones when the total exceeds the max number of entries. |
| 273 | */ |
| 274 | static long |
| 275 | prune_cache_entries(void) |
| 276 | { |
| 277 | unsigned int i; |
| 278 | long freed = 0; |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 279 | |
| 280 | for (i = 0; i < drc_hashsize; i++) { |
| 281 | struct nfsd_drc_bucket *b = &drc_hashtbl[i]; |
| 282 | |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 283 | if (list_empty(&b->lru_head)) |
| 284 | continue; |
| 285 | spin_lock(&b->cache_lock); |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 286 | freed += prune_bucket(b); |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 287 | spin_unlock(&b->cache_lock); |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 288 | } |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 289 | return freed; |
Jeff Layton | aca8a23 | 2013-02-04 08:18:05 -0500 | [diff] [blame] | 290 | } |
| 291 | |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 292 | static unsigned long |
| 293 | nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc) |
Jeff Layton | b4e7f2c | 2013-02-04 08:18:06 -0500 | [diff] [blame] | 294 | { |
Trond Myklebust | 31e60f5 | 2014-08-06 13:44:23 -0400 | [diff] [blame] | 295 | return atomic_read(&num_drc_entries); |
Jeff Layton | b4e7f2c | 2013-02-04 08:18:06 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 298 | static unsigned long |
| 299 | nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc) |
| 300 | { |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 301 | return prune_cache_entries(); |
Dave Chinner | 1ab6c49 | 2013-08-28 10:18:09 +1000 | [diff] [blame] | 302 | } |
Jeff Layton | aca8a23 | 2013-02-04 08:18:05 -0500 | [diff] [blame] | 303 | /* |
Jeff Layton | 01a7dec | 2013-02-04 11:57:27 -0500 | [diff] [blame] | 304 | * Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes |
| 305 | */ |
| 306 | static __wsum |
| 307 | nfsd_cache_csum(struct svc_rqst *rqstp) |
| 308 | { |
| 309 | int idx; |
| 310 | unsigned int base; |
| 311 | __wsum csum; |
| 312 | struct xdr_buf *buf = &rqstp->rq_arg; |
| 313 | const unsigned char *p = buf->head[0].iov_base; |
| 314 | size_t csum_len = min_t(size_t, buf->head[0].iov_len + buf->page_len, |
| 315 | RC_CSUMLEN); |
| 316 | size_t len = min(buf->head[0].iov_len, csum_len); |
| 317 | |
| 318 | /* rq_arg.head first */ |
| 319 | csum = csum_partial(p, len, 0); |
| 320 | csum_len -= len; |
| 321 | |
| 322 | /* Continue into page array */ |
| 323 | idx = buf->page_base / PAGE_SIZE; |
| 324 | base = buf->page_base & ~PAGE_MASK; |
| 325 | while (csum_len) { |
| 326 | p = page_address(buf->pages[idx]) + base; |
Jeff Layton | 56edc86 | 2013-02-15 13:36:34 -0500 | [diff] [blame] | 327 | len = min_t(size_t, PAGE_SIZE - base, csum_len); |
Jeff Layton | 01a7dec | 2013-02-04 11:57:27 -0500 | [diff] [blame] | 328 | csum = csum_partial(p, len, csum); |
| 329 | csum_len -= len; |
| 330 | base = 0; |
| 331 | ++idx; |
| 332 | } |
| 333 | return csum; |
| 334 | } |
| 335 | |
Trond Myklebust | ed00c2f | 2018-10-03 13:11:51 -0400 | [diff] [blame] | 336 | static int |
| 337 | nfsd_cache_key_cmp(const struct svc_cacherep *key, const struct svc_cacherep *rp) |
Jeff Layton | 9dc5614 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 338 | { |
Trond Myklebust | ed00c2f | 2018-10-03 13:11:51 -0400 | [diff] [blame] | 339 | if (key->c_key.k_xid == rp->c_key.k_xid && |
| 340 | key->c_key.k_csum != rp->c_key.k_csum) |
Jeff Layton | 9dc5614 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 341 | ++payload_misses; |
Jeff Layton | 9dc5614 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 342 | |
Trond Myklebust | ed00c2f | 2018-10-03 13:11:51 -0400 | [diff] [blame] | 343 | return memcmp(&key->c_key, &rp->c_key, sizeof(key->c_key)); |
Jeff Layton | 9dc5614 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Jeff Layton | 01a7dec | 2013-02-04 11:57:27 -0500 | [diff] [blame] | 346 | /* |
Jeff Layton | a4a3ec3 | 2013-01-28 14:41:14 -0500 | [diff] [blame] | 347 | * Search the request hash for an entry that matches the given rqstp. |
| 348 | * Must be called with cache_lock held. Returns the found entry or |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 349 | * inserts an empty key on failure. |
Jeff Layton | a4a3ec3 | 2013-01-28 14:41:14 -0500 | [diff] [blame] | 350 | */ |
| 351 | static struct svc_cacherep * |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 352 | nfsd_cache_insert(struct nfsd_drc_bucket *b, struct svc_cacherep *key) |
Jeff Layton | a4a3ec3 | 2013-01-28 14:41:14 -0500 | [diff] [blame] | 353 | { |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 354 | struct svc_cacherep *rp, *ret = key; |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 355 | struct rb_node **p = &b->rb_head.rb_node, |
| 356 | *parent = NULL; |
Jeff Layton | 98d821b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 357 | unsigned int entries = 0; |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 358 | int cmp; |
Jeff Layton | a4a3ec3 | 2013-01-28 14:41:14 -0500 | [diff] [blame] | 359 | |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 360 | while (*p != NULL) { |
Jeff Layton | 98d821b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 361 | ++entries; |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 362 | parent = *p; |
| 363 | rp = rb_entry(parent, struct svc_cacherep, c_node); |
| 364 | |
| 365 | cmp = nfsd_cache_key_cmp(key, rp); |
| 366 | if (cmp < 0) |
| 367 | p = &parent->rb_left; |
| 368 | else if (cmp > 0) |
| 369 | p = &parent->rb_right; |
| 370 | else { |
Jeff Layton | 98d821b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 371 | ret = rp; |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 372 | goto out; |
Jeff Layton | 98d821b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 373 | } |
Jeff Layton | a4a3ec3 | 2013-01-28 14:41:14 -0500 | [diff] [blame] | 374 | } |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 375 | rb_link_node(&key->c_node, parent, p); |
| 376 | rb_insert_color(&key->c_node, &b->rb_head); |
| 377 | out: |
Jeff Layton | 98d821b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 378 | /* tally hash chain length stats */ |
| 379 | if (entries > longest_chain) { |
| 380 | longest_chain = entries; |
Trond Myklebust | 31e60f5 | 2014-08-06 13:44:23 -0400 | [diff] [blame] | 381 | longest_chain_cachesize = atomic_read(&num_drc_entries); |
Jeff Layton | 98d821b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 382 | } else if (entries == longest_chain) { |
| 383 | /* prefer to keep the smallest cachesize possible here */ |
Trond Myklebust | 31e60f5 | 2014-08-06 13:44:23 -0400 | [diff] [blame] | 384 | longest_chain_cachesize = min_t(unsigned int, |
| 385 | longest_chain_cachesize, |
| 386 | atomic_read(&num_drc_entries)); |
Jeff Layton | 98d821b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 387 | } |
| 388 | |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 389 | lru_put_end(b, ret); |
Jeff Layton | 98d821b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 390 | return ret; |
Jeff Layton | a4a3ec3 | 2013-01-28 14:41:14 -0500 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | * Try to find an entry matching the current call in the cache. When none |
Jeff Layton | 1ac8362 | 2013-02-14 16:45:13 -0500 | [diff] [blame] | 395 | * is found, we try to grab the oldest expired entry off the LRU list. If |
| 396 | * a suitable one isn't there, then drop the cache_lock and allocate a |
| 397 | * new one, then search again in case one got inserted while this thread |
| 398 | * didn't hold the lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | */ |
| 400 | int |
J. Bruce Fields | 1091006 | 2011-01-24 12:11:02 -0500 | [diff] [blame] | 401 | nfsd_cache_lookup(struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
Jeff Layton | 0338dd1 | 2013-02-04 08:18:02 -0500 | [diff] [blame] | 403 | struct svc_cacherep *rp, *found; |
Al Viro | c7afef1 | 2006-10-19 23:29:02 -0700 | [diff] [blame] | 404 | __be32 xid = rqstp->rq_xid; |
Jeff Layton | 01a7dec | 2013-02-04 11:57:27 -0500 | [diff] [blame] | 405 | __wsum csum; |
Trond Myklebust | 7142b98 | 2014-08-06 13:44:20 -0400 | [diff] [blame] | 406 | u32 hash = nfsd_cache_hash(xid); |
| 407 | struct nfsd_drc_bucket *b = &drc_hashtbl[hash]; |
J. Bruce Fields | 1091006 | 2011-01-24 12:11:02 -0500 | [diff] [blame] | 408 | int type = rqstp->rq_cachetype; |
Jeff Layton | 0b9ea37 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 409 | int rtn = RC_DOIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | rqstp->rq_cacherep = NULL; |
Jeff Layton | 13cc8a7 | 2013-02-04 08:18:03 -0500 | [diff] [blame] | 412 | if (type == RC_NOCACHE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | nfsdstats.rcnocache++; |
Jeff Layton | 0b9ea37 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 414 | return rtn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Jeff Layton | 01a7dec | 2013-02-04 11:57:27 -0500 | [diff] [blame] | 417 | csum = nfsd_cache_csum(rqstp); |
| 418 | |
Jeff Layton | 0b9ea37 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 419 | /* |
| 420 | * Since the common case is a cache miss followed by an insert, |
Jeff Layton | a0ef5e19 | 2013-12-05 06:00:51 -0500 | [diff] [blame] | 421 | * preallocate an entry. |
Jeff Layton | 0b9ea37 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 422 | */ |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 423 | rp = nfsd_reply_cache_alloc(rqstp, csum); |
Jeff Layton | 0b9ea37 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 424 | if (!rp) { |
| 425 | dprintk("nfsd: unable to allocate DRC entry!\n"); |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 426 | return rtn; |
| 427 | } |
| 428 | |
| 429 | spin_lock(&b->cache_lock); |
| 430 | found = nfsd_cache_insert(b, rp); |
| 431 | if (found != rp) { |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 432 | nfsd_reply_cache_free_locked(NULL, rp); |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 433 | rp = found; |
| 434 | goto found_entry; |
Jeff Layton | 0b9ea37 | 2013-03-27 10:15:37 -0400 | [diff] [blame] | 435 | } |
| 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | nfsdstats.rcmisses++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | rqstp->rq_cacherep = rp; |
| 439 | rp->c_state = RC_INPROG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 441 | atomic_inc(&num_drc_entries); |
| 442 | drc_mem_usage += sizeof(*rp); |
| 443 | |
| 444 | /* go ahead and prune the cache */ |
| 445 | prune_bucket(b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | out: |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 447 | spin_unlock(&b->cache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | return rtn; |
| 449 | |
| 450 | found_entry: |
| 451 | /* We found a matching entry which is either in progress or done. */ |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 452 | nfsdstats.rchits++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | rtn = RC_DROPIT; |
Trond Myklebust | 76ecec2 | 2018-10-01 10:41:55 -0400 | [diff] [blame] | 454 | |
Trond Myklebust | 7e5d0e0 | 2018-03-28 12:18:01 -0400 | [diff] [blame] | 455 | /* Request being processed */ |
| 456 | if (rp->c_state == RC_INPROG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | goto out; |
| 458 | |
| 459 | /* From the hall of fame of impractical attacks: |
| 460 | * Is this a user who tries to snoop on the cache? */ |
| 461 | rtn = RC_DOIT; |
Jeff Layton | 4d152e2 | 2014-11-19 07:51:14 -0500 | [diff] [blame] | 462 | if (!test_bit(RQ_SECURE, &rqstp->rq_flags) && rp->c_secure) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | goto out; |
| 464 | |
| 465 | /* Compose RPC reply header */ |
| 466 | switch (rp->c_type) { |
| 467 | case RC_NOCACHE: |
| 468 | break; |
| 469 | case RC_REPLSTAT: |
| 470 | svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat); |
| 471 | rtn = RC_REPLY; |
| 472 | break; |
| 473 | case RC_REPLBUFF: |
| 474 | if (!nfsd_cache_append(rqstp, &rp->c_replvec)) |
| 475 | goto out; /* should not happen */ |
| 476 | rtn = RC_REPLY; |
| 477 | break; |
| 478 | default: |
| 479 | printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type); |
Trond Myklebust | 736c662 | 2018-10-01 10:41:57 -0400 | [diff] [blame] | 480 | nfsd_reply_cache_free_locked(b, rp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | goto out; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Update a cache entry. This is called from nfsd_dispatch when |
| 488 | * the procedure has been executed and the complete reply is in |
| 489 | * rqstp->rq_res. |
| 490 | * |
| 491 | * We're copying around data here rather than swapping buffers because |
| 492 | * the toplevel loop requires max-sized buffers, which would be a waste |
| 493 | * of memory for a cache with a max reply size of 100 bytes (diropokres). |
| 494 | * |
| 495 | * If we should start to use different types of cache entries tailored |
| 496 | * specifically for attrstat and fh's, we may save even more space. |
| 497 | * |
| 498 | * Also note that a cachetype of RC_NOCACHE can legally be passed when |
| 499 | * nfsd failed to encode a reply that otherwise would have been cached. |
| 500 | * In this case, nfsd_cache_update is called with statp == NULL. |
| 501 | */ |
| 502 | void |
Al Viro | c7afef1 | 2006-10-19 23:29:02 -0700 | [diff] [blame] | 503 | nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
Jeff Layton | 13cc8a7 | 2013-02-04 08:18:03 -0500 | [diff] [blame] | 505 | struct svc_cacherep *rp = rqstp->rq_cacherep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | struct kvec *resv = &rqstp->rq_res.head[0], *cachv; |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 507 | u32 hash; |
| 508 | struct nfsd_drc_bucket *b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | int len; |
Jeff Layton | 6c6910c | 2013-03-27 10:15:38 -0400 | [diff] [blame] | 510 | size_t bufsize = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
Jeff Layton | 13cc8a7 | 2013-02-04 08:18:03 -0500 | [diff] [blame] | 512 | if (!rp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | return; |
| 514 | |
Trond Myklebust | ed00c2f | 2018-10-03 13:11:51 -0400 | [diff] [blame] | 515 | hash = nfsd_cache_hash(rp->c_key.k_xid); |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 516 | b = &drc_hashtbl[hash]; |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | len = resv->iov_len - ((char*)statp - (char*)resv->iov_base); |
| 519 | len >>= 2; |
Greg Banks | fca4217 | 2009-04-01 07:28:13 +1100 | [diff] [blame] | 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | /* Don't cache excessive amounts of data and XDR failures */ |
| 522 | if (!statp || len > (256 >> 2)) { |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 523 | nfsd_reply_cache_free(b, rp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | return; |
| 525 | } |
| 526 | |
| 527 | switch (cachetype) { |
| 528 | case RC_REPLSTAT: |
| 529 | if (len != 1) |
| 530 | printk("nfsd: RC_REPLSTAT/reply len %d!\n",len); |
| 531 | rp->c_replstat = *statp; |
| 532 | break; |
| 533 | case RC_REPLBUFF: |
| 534 | cachv = &rp->c_replvec; |
Jeff Layton | 6c6910c | 2013-03-27 10:15:38 -0400 | [diff] [blame] | 535 | bufsize = len << 2; |
| 536 | cachv->iov_base = kmalloc(bufsize, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | if (!cachv->iov_base) { |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 538 | nfsd_reply_cache_free(b, rp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | return; |
| 540 | } |
Jeff Layton | 6c6910c | 2013-03-27 10:15:38 -0400 | [diff] [blame] | 541 | cachv->iov_len = bufsize; |
| 542 | memcpy(cachv->iov_base, statp, bufsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | break; |
Jeff Layton | 2c6b691 | 2013-02-04 08:18:04 -0500 | [diff] [blame] | 544 | case RC_NOCACHE: |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 545 | nfsd_reply_cache_free(b, rp); |
Jeff Layton | 2c6b691 | 2013-02-04 08:18:04 -0500 | [diff] [blame] | 546 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 548 | spin_lock(&b->cache_lock); |
Jeff Layton | 6c6910c | 2013-03-27 10:15:38 -0400 | [diff] [blame] | 549 | drc_mem_usage += bufsize; |
Trond Myklebust | bedd4b6 | 2014-08-06 13:44:21 -0400 | [diff] [blame] | 550 | lru_put_end(b, rp); |
Jeff Layton | 4d152e2 | 2014-11-19 07:51:14 -0500 | [diff] [blame] | 551 | rp->c_secure = test_bit(RQ_SECURE, &rqstp->rq_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | rp->c_type = cachetype; |
| 553 | rp->c_state = RC_DONE; |
Trond Myklebust | 89a26b3 | 2014-08-06 13:44:24 -0400 | [diff] [blame] | 554 | spin_unlock(&b->cache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | return; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Copy cached reply to current reply buffer. Should always fit. |
| 560 | * FIXME as reply is in a page, we should just attach the page, and |
| 561 | * keep a refcount.... |
| 562 | */ |
| 563 | static int |
| 564 | nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data) |
| 565 | { |
| 566 | struct kvec *vec = &rqstp->rq_res.head[0]; |
| 567 | |
| 568 | if (vec->iov_len + data->iov_len > PAGE_SIZE) { |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 569 | printk(KERN_WARNING "nfsd: cached reply too large (%zd).\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | data->iov_len); |
| 571 | return 0; |
| 572 | } |
| 573 | memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len); |
| 574 | vec->iov_len += data->iov_len; |
| 575 | return 1; |
| 576 | } |
Jeff Layton | a2f999a | 2013-03-27 10:15:38 -0400 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * Note that fields may be added, removed or reordered in the future. Programs |
| 580 | * scraping this file for info should test the labels to ensure they're |
| 581 | * getting the correct field. |
| 582 | */ |
| 583 | static int nfsd_reply_cache_stats_show(struct seq_file *m, void *v) |
| 584 | { |
Jeff Layton | a2f999a | 2013-03-27 10:15:38 -0400 | [diff] [blame] | 585 | seq_printf(m, "max entries: %u\n", max_drc_entries); |
Trond Myklebust | 31e60f5 | 2014-08-06 13:44:23 -0400 | [diff] [blame] | 586 | seq_printf(m, "num entries: %u\n", |
| 587 | atomic_read(&num_drc_entries)); |
Jeff Layton | 0733c7b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 588 | seq_printf(m, "hash buckets: %u\n", 1 << maskbits); |
Jeff Layton | a2f999a | 2013-03-27 10:15:38 -0400 | [diff] [blame] | 589 | seq_printf(m, "mem usage: %u\n", drc_mem_usage); |
| 590 | seq_printf(m, "cache hits: %u\n", nfsdstats.rchits); |
| 591 | seq_printf(m, "cache misses: %u\n", nfsdstats.rcmisses); |
| 592 | seq_printf(m, "not cached: %u\n", nfsdstats.rcnocache); |
| 593 | seq_printf(m, "payload misses: %u\n", payload_misses); |
Jeff Layton | 98d821b | 2013-03-27 10:15:39 -0400 | [diff] [blame] | 594 | seq_printf(m, "longest chain len: %u\n", longest_chain); |
| 595 | seq_printf(m, "cachesize at longest: %u\n", longest_chain_cachesize); |
Jeff Layton | a2f999a | 2013-03-27 10:15:38 -0400 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | int nfsd_reply_cache_stats_open(struct inode *inode, struct file *file) |
| 600 | { |
| 601 | return single_open(file, nfsd_reply_cache_stats_show, NULL); |
| 602 | } |