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