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