aboutsummaryrefslogtreecommitdiffstats
path: root/fs/nfsd/nfscache.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/nfsd/nfscache.c')
-rw-r--r--fs/nfsd/nfscache.c352
1 files changed, 277 insertions, 75 deletions
diff --git a/fs/nfsd/nfscache.c b/fs/nfsd/nfscache.c
index da3dbd0f8979..62c1ee128aeb 100644
--- a/fs/nfsd/nfscache.c
+++ b/fs/nfsd/nfscache.c
@@ -9,22 +9,22 @@
9 */ 9 */
10 10
11#include <linux/slab.h> 11#include <linux/slab.h>
12#include <linux/sunrpc/addr.h>
13#include <linux/highmem.h>
14#include <net/checksum.h>
12 15
13#include "nfsd.h" 16#include "nfsd.h"
14#include "cache.h" 17#include "cache.h"
15 18
16/* Size of reply cache. Common values are: 19#define NFSDDBG_FACILITY NFSDDBG_REPCACHE
17 * 4.3BSD: 128 20
18 * 4.4BSD: 256
19 * Solaris2: 1024
20 * DEC Unix: 512-4096
21 */
22#define CACHESIZE 1024
23#define HASHSIZE 64 21#define HASHSIZE 64
24 22
25static struct hlist_head * cache_hash; 23static struct hlist_head * cache_hash;
26static struct list_head lru_head; 24static struct list_head lru_head;
27static int cache_disabled = 1; 25static struct kmem_cache *drc_slab;
26static unsigned int num_drc_entries;
27static unsigned int max_drc_entries;
28 28
29/* 29/*
30 * Calculate the hash index from an XID. 30 * Calculate the hash index from an XID.
@@ -37,6 +37,14 @@ static inline u32 request_hash(u32 xid)
37} 37}
38 38
39static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec); 39static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
40static void cache_cleaner_func(struct work_struct *unused);
41static int nfsd_reply_cache_shrink(struct shrinker *shrink,
42 struct shrink_control *sc);
43
44struct shrinker nfsd_reply_cache_shrinker = {
45 .shrink = nfsd_reply_cache_shrink,
46 .seeks = 1,
47};
40 48
41/* 49/*
42 * locking for the reply cache: 50 * locking for the reply cache:
@@ -44,30 +52,86 @@ static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
44 * Otherwise, it when accessing _prev or _next, the lock must be held. 52 * Otherwise, it when accessing _prev or _next, the lock must be held.
45 */ 53 */
46static DEFINE_SPINLOCK(cache_lock); 54static DEFINE_SPINLOCK(cache_lock);
55static DECLARE_DELAYED_WORK(cache_cleaner, cache_cleaner_func);
47 56
48int nfsd_reply_cache_init(void) 57/*
58 * Put a cap on the size of the DRC based on the amount of available
59 * low memory in the machine.
60 *
61 * 64MB: 8192
62 * 128MB: 11585
63 * 256MB: 16384
64 * 512MB: 23170
65 * 1GB: 32768
66 * 2GB: 46340
67 * 4GB: 65536
68 * 8GB: 92681
69 * 16GB: 131072
70 *
71 * ...with a hard cap of 256k entries. In the worst case, each entry will be
72 * ~1k, so the above numbers should give a rough max of the amount of memory
73 * used in k.
74 */
75static unsigned int
76nfsd_cache_size_limit(void)
77{
78 unsigned int limit;
79 unsigned long low_pages = totalram_pages - totalhigh_pages;
80
81 limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
82 return min_t(unsigned int, limit, 256*1024);
83}
84
85static struct svc_cacherep *
86nfsd_reply_cache_alloc(void)
49{ 87{
50 struct svc_cacherep *rp; 88 struct svc_cacherep *rp;
51 int i;
52 89
53 INIT_LIST_HEAD(&lru_head); 90 rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
54 i = CACHESIZE; 91 if (rp) {
55 while (i) {
56 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
57 if (!rp)
58 goto out_nomem;
59 list_add(&rp->c_lru, &lru_head);
60 rp->c_state = RC_UNUSED; 92 rp->c_state = RC_UNUSED;
61 rp->c_type = RC_NOCACHE; 93 rp->c_type = RC_NOCACHE;
94 INIT_LIST_HEAD(&rp->c_lru);
62 INIT_HLIST_NODE(&rp->c_hash); 95 INIT_HLIST_NODE(&rp->c_hash);
63 i--;
64 } 96 }
97 return rp;
98}
65 99
66 cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL); 100static void
101nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
102{
103 if (rp->c_type == RC_REPLBUFF)
104 kfree(rp->c_replvec.iov_base);
105 hlist_del(&rp->c_hash);
106 list_del(&rp->c_lru);
107 --num_drc_entries;
108 kmem_cache_free(drc_slab, rp);
109}
110
111static void
112nfsd_reply_cache_free(struct svc_cacherep *rp)
113{
114 spin_lock(&cache_lock);
115 nfsd_reply_cache_free_locked(rp);
116 spin_unlock(&cache_lock);
117}
118
119int nfsd_reply_cache_init(void)
120{
121 register_shrinker(&nfsd_reply_cache_shrinker);
122 drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
123 0, 0, NULL);
124 if (!drc_slab)
125 goto out_nomem;
126
127 cache_hash = kcalloc(HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
67 if (!cache_hash) 128 if (!cache_hash)
68 goto out_nomem; 129 goto out_nomem;
69 130
70 cache_disabled = 0; 131 INIT_LIST_HEAD(&lru_head);
132 max_drc_entries = nfsd_cache_size_limit();
133 num_drc_entries = 0;
134
71 return 0; 135 return 0;
72out_nomem: 136out_nomem:
73 printk(KERN_ERR "nfsd: failed to allocate reply cache\n"); 137 printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
@@ -79,27 +143,33 @@ void nfsd_reply_cache_shutdown(void)
79{ 143{
80 struct svc_cacherep *rp; 144 struct svc_cacherep *rp;
81 145
146 unregister_shrinker(&nfsd_reply_cache_shrinker);
147 cancel_delayed_work_sync(&cache_cleaner);
148
82 while (!list_empty(&lru_head)) { 149 while (!list_empty(&lru_head)) {
83 rp = list_entry(lru_head.next, struct svc_cacherep, c_lru); 150 rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
84 if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF) 151 nfsd_reply_cache_free_locked(rp);
85 kfree(rp->c_replvec.iov_base);
86 list_del(&rp->c_lru);
87 kfree(rp);
88 } 152 }
89 153
90 cache_disabled = 1;
91
92 kfree (cache_hash); 154 kfree (cache_hash);
93 cache_hash = NULL; 155 cache_hash = NULL;
156
157 if (drc_slab) {
158 kmem_cache_destroy(drc_slab);
159 drc_slab = NULL;
160 }
94} 161}
95 162
96/* 163/*
97 * Move cache entry to end of LRU list 164 * Move cache entry to end of LRU list, and queue the cleaner to run if it's
165 * not already scheduled.
98 */ 166 */
99static void 167static void
100lru_put_end(struct svc_cacherep *rp) 168lru_put_end(struct svc_cacherep *rp)
101{ 169{
170 rp->c_timestamp = jiffies;
102 list_move_tail(&rp->c_lru, &lru_head); 171 list_move_tail(&rp->c_lru, &lru_head);
172 schedule_delayed_work(&cache_cleaner, RC_EXPIRE);
103} 173}
104 174
105/* 175/*
@@ -112,82 +182,214 @@ hash_refile(struct svc_cacherep *rp)
112 hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid)); 182 hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
113} 183}
114 184
185static inline bool
186nfsd_cache_entry_expired(struct svc_cacherep *rp)
187{
188 return rp->c_state != RC_INPROG &&
189 time_after(jiffies, rp->c_timestamp + RC_EXPIRE);
190}
191
192/*
193 * Walk the LRU list and prune off entries that are older than RC_EXPIRE.
194 * Also prune the oldest ones when the total exceeds the max number of entries.
195 */
196static void
197prune_cache_entries(void)
198{
199 struct svc_cacherep *rp, *tmp;
200
201 list_for_each_entry_safe(rp, tmp, &lru_head, c_lru) {
202 if (!nfsd_cache_entry_expired(rp) &&
203 num_drc_entries <= max_drc_entries)
204 break;
205 nfsd_reply_cache_free_locked(rp);
206 }
207
208 /*
209 * Conditionally rearm the job. If we cleaned out the list, then
210 * cancel any pending run (since there won't be any work to do).
211 * Otherwise, we rearm the job or modify the existing one to run in
212 * RC_EXPIRE since we just ran the pruner.
213 */
214 if (list_empty(&lru_head))
215 cancel_delayed_work(&cache_cleaner);
216 else
217 mod_delayed_work(system_wq, &cache_cleaner, RC_EXPIRE);
218}
219
220static void
221cache_cleaner_func(struct work_struct *unused)
222{
223 spin_lock(&cache_lock);
224 prune_cache_entries();
225 spin_unlock(&cache_lock);
226}
227
228static int
229nfsd_reply_cache_shrink(struct shrinker *shrink, struct shrink_control *sc)
230{
231 unsigned int num;
232
233 spin_lock(&cache_lock);
234 if (sc->nr_to_scan)
235 prune_cache_entries();
236 num = num_drc_entries;
237 spin_unlock(&cache_lock);
238
239 return num;
240}
241
242/*
243 * Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes
244 */
245static __wsum
246nfsd_cache_csum(struct svc_rqst *rqstp)
247{
248 int idx;
249 unsigned int base;
250 __wsum csum;
251 struct xdr_buf *buf = &rqstp->rq_arg;
252 const unsigned char *p = buf->head[0].iov_base;
253 size_t csum_len = min_t(size_t, buf->head[0].iov_len + buf->page_len,
254 RC_CSUMLEN);
255 size_t len = min(buf->head[0].iov_len, csum_len);
256
257 /* rq_arg.head first */
258 csum = csum_partial(p, len, 0);
259 csum_len -= len;
260
261 /* Continue into page array */
262 idx = buf->page_base / PAGE_SIZE;
263 base = buf->page_base & ~PAGE_MASK;
264 while (csum_len) {
265 p = page_address(buf->pages[idx]) + base;
266 len = min_t(size_t, PAGE_SIZE - base, csum_len);
267 csum = csum_partial(p, len, csum);
268 csum_len -= len;
269 base = 0;
270 ++idx;
271 }
272 return csum;
273}
274
275/*
276 * Search the request hash for an entry that matches the given rqstp.
277 * Must be called with cache_lock held. Returns the found entry or
278 * NULL on failure.
279 */
280static struct svc_cacherep *
281nfsd_cache_search(struct svc_rqst *rqstp, __wsum csum)
282{
283 struct svc_cacherep *rp;
284 struct hlist_head *rh;
285 __be32 xid = rqstp->rq_xid;
286 u32 proto = rqstp->rq_prot,
287 vers = rqstp->rq_vers,
288 proc = rqstp->rq_proc;
289
290 rh = &cache_hash[request_hash(xid)];
291 hlist_for_each_entry(rp, rh, c_hash) {
292 if (xid == rp->c_xid && proc == rp->c_proc &&
293 proto == rp->c_prot && vers == rp->c_vers &&
294 rqstp->rq_arg.len == rp->c_len && csum == rp->c_csum &&
295 rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) &&
296 rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr))
297 return rp;
298 }
299 return NULL;
300}
301
115/* 302/*
116 * Try to find an entry matching the current call in the cache. When none 303 * Try to find an entry matching the current call in the cache. When none
117 * is found, we grab the oldest unlocked entry off the LRU list. 304 * is found, we try to grab the oldest expired entry off the LRU list. If
118 * Note that no operation within the loop may sleep. 305 * a suitable one isn't there, then drop the cache_lock and allocate a
306 * new one, then search again in case one got inserted while this thread
307 * didn't hold the lock.
119 */ 308 */
120int 309int
121nfsd_cache_lookup(struct svc_rqst *rqstp) 310nfsd_cache_lookup(struct svc_rqst *rqstp)
122{ 311{
123 struct hlist_head *rh; 312 struct svc_cacherep *rp, *found;
124 struct svc_cacherep *rp;
125 __be32 xid = rqstp->rq_xid; 313 __be32 xid = rqstp->rq_xid;
126 u32 proto = rqstp->rq_prot, 314 u32 proto = rqstp->rq_prot,
127 vers = rqstp->rq_vers, 315 vers = rqstp->rq_vers,
128 proc = rqstp->rq_proc; 316 proc = rqstp->rq_proc;
317 __wsum csum;
129 unsigned long age; 318 unsigned long age;
130 int type = rqstp->rq_cachetype; 319 int type = rqstp->rq_cachetype;
131 int rtn; 320 int rtn;
132 321
133 rqstp->rq_cacherep = NULL; 322 rqstp->rq_cacherep = NULL;
134 if (cache_disabled || type == RC_NOCACHE) { 323 if (type == RC_NOCACHE) {
135 nfsdstats.rcnocache++; 324 nfsdstats.rcnocache++;
136 return RC_DOIT; 325 return RC_DOIT;
137 } 326 }
138 327
328 csum = nfsd_cache_csum(rqstp);
329
139 spin_lock(&cache_lock); 330 spin_lock(&cache_lock);
140 rtn = RC_DOIT; 331 rtn = RC_DOIT;
141 332
142 rh = &cache_hash[request_hash(xid)]; 333 rp = nfsd_cache_search(rqstp, csum);
143 hlist_for_each_entry(rp, rh, c_hash) { 334 if (rp)
144 if (rp->c_state != RC_UNUSED && 335 goto found_entry;
145 xid == rp->c_xid && proc == rp->c_proc && 336
146 proto == rp->c_prot && vers == rp->c_vers && 337 /* Try to use the first entry on the LRU */
147 time_before(jiffies, rp->c_timestamp + 120*HZ) && 338 if (!list_empty(&lru_head)) {
148 memcmp((char*)&rqstp->rq_addr, (char*)&rp->c_addr, sizeof(rp->c_addr))==0) { 339 rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
149 nfsdstats.rchits++; 340 if (nfsd_cache_entry_expired(rp) ||
150 goto found_entry; 341 num_drc_entries >= max_drc_entries) {
342 lru_put_end(rp);
343 prune_cache_entries();
344 goto setup_entry;
151 } 345 }
152 } 346 }
153 nfsdstats.rcmisses++;
154 347
155 /* This loop shouldn't take more than a few iterations normally */ 348 /* Drop the lock and allocate a new entry */
156 { 349 spin_unlock(&cache_lock);
157 int safe = 0; 350 rp = nfsd_reply_cache_alloc();
158 list_for_each_entry(rp, &lru_head, c_lru) { 351 if (!rp) {
159 if (rp->c_state != RC_INPROG) 352 dprintk("nfsd: unable to allocate DRC entry!\n");
160 break; 353 return RC_DOIT;
161 if (safe++ > CACHESIZE) {
162 printk("nfsd: loop in repcache LRU list\n");
163 cache_disabled = 1;
164 goto out;
165 }
166 } 354 }
355 spin_lock(&cache_lock);
356 ++num_drc_entries;
357
358 /*
359 * Must search again just in case someone inserted one
360 * after we dropped the lock above.
361 */
362 found = nfsd_cache_search(rqstp, csum);
363 if (found) {
364 nfsd_reply_cache_free_locked(rp);
365 rp = found;
366 goto found_entry;
167 } 367 }
168 368
169 /* All entries on the LRU are in-progress. This should not happen */ 369 /*
170 if (&rp->c_lru == &lru_head) { 370 * We're keeping the one we just allocated. Are we now over the
171 static int complaints; 371 * limit? Prune one off the tip of the LRU in trade for the one we
172 372 * just allocated if so.
173 printk(KERN_WARNING "nfsd: all repcache entries locked!\n"); 373 */
174 if (++complaints > 5) { 374 if (num_drc_entries >= max_drc_entries)
175 printk(KERN_WARNING "nfsd: disabling repcache.\n"); 375 nfsd_reply_cache_free_locked(list_first_entry(&lru_head,
176 cache_disabled = 1; 376 struct svc_cacherep, c_lru));
177 }
178 goto out;
179 }
180 377
378setup_entry:
379 nfsdstats.rcmisses++;
181 rqstp->rq_cacherep = rp; 380 rqstp->rq_cacherep = rp;
182 rp->c_state = RC_INPROG; 381 rp->c_state = RC_INPROG;
183 rp->c_xid = xid; 382 rp->c_xid = xid;
184 rp->c_proc = proc; 383 rp->c_proc = proc;
185 memcpy(&rp->c_addr, svc_addr_in(rqstp), sizeof(rp->c_addr)); 384 rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
385 rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
186 rp->c_prot = proto; 386 rp->c_prot = proto;
187 rp->c_vers = vers; 387 rp->c_vers = vers;
188 rp->c_timestamp = jiffies; 388 rp->c_len = rqstp->rq_arg.len;
389 rp->c_csum = csum;
189 390
190 hash_refile(rp); 391 hash_refile(rp);
392 lru_put_end(rp);
191 393
192 /* release any buffer */ 394 /* release any buffer */
193 if (rp->c_type == RC_REPLBUFF) { 395 if (rp->c_type == RC_REPLBUFF) {
@@ -200,9 +402,9 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
200 return rtn; 402 return rtn;
201 403
202found_entry: 404found_entry:
405 nfsdstats.rchits++;
203 /* We found a matching entry which is either in progress or done. */ 406 /* We found a matching entry which is either in progress or done. */
204 age = jiffies - rp->c_timestamp; 407 age = jiffies - rp->c_timestamp;
205 rp->c_timestamp = jiffies;
206 lru_put_end(rp); 408 lru_put_end(rp);
207 409
208 rtn = RC_DROPIT; 410 rtn = RC_DROPIT;
@@ -231,7 +433,7 @@ found_entry:
231 break; 433 break;
232 default: 434 default:
233 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type); 435 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
234 rp->c_state = RC_UNUSED; 436 nfsd_reply_cache_free_locked(rp);
235 } 437 }
236 438
237 goto out; 439 goto out;
@@ -256,11 +458,11 @@ found_entry:
256void 458void
257nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp) 459nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
258{ 460{
259 struct svc_cacherep *rp; 461 struct svc_cacherep *rp = rqstp->rq_cacherep;
260 struct kvec *resv = &rqstp->rq_res.head[0], *cachv; 462 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
261 int len; 463 int len;
262 464
263 if (!(rp = rqstp->rq_cacherep) || cache_disabled) 465 if (!rp)
264 return; 466 return;
265 467
266 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base); 468 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
@@ -268,7 +470,7 @@ nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
268 470
269 /* Don't cache excessive amounts of data and XDR failures */ 471 /* Don't cache excessive amounts of data and XDR failures */
270 if (!statp || len > (256 >> 2)) { 472 if (!statp || len > (256 >> 2)) {
271 rp->c_state = RC_UNUSED; 473 nfsd_reply_cache_free(rp);
272 return; 474 return;
273 } 475 }
274 476
@@ -282,21 +484,21 @@ nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
282 cachv = &rp->c_replvec; 484 cachv = &rp->c_replvec;
283 cachv->iov_base = kmalloc(len << 2, GFP_KERNEL); 485 cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
284 if (!cachv->iov_base) { 486 if (!cachv->iov_base) {
285 spin_lock(&cache_lock); 487 nfsd_reply_cache_free(rp);
286 rp->c_state = RC_UNUSED;
287 spin_unlock(&cache_lock);
288 return; 488 return;
289 } 489 }
290 cachv->iov_len = len << 2; 490 cachv->iov_len = len << 2;
291 memcpy(cachv->iov_base, statp, len << 2); 491 memcpy(cachv->iov_base, statp, len << 2);
292 break; 492 break;
493 case RC_NOCACHE:
494 nfsd_reply_cache_free(rp);
495 return;
293 } 496 }
294 spin_lock(&cache_lock); 497 spin_lock(&cache_lock);
295 lru_put_end(rp); 498 lru_put_end(rp);
296 rp->c_secure = rqstp->rq_secure; 499 rp->c_secure = rqstp->rq_secure;
297 rp->c_type = cachetype; 500 rp->c_type = cachetype;
298 rp->c_state = RC_DONE; 501 rp->c_state = RC_DONE;
299 rp->c_timestamp = jiffies;
300 spin_unlock(&cache_lock); 502 spin_unlock(&cache_lock);
301 return; 503 return;
302} 504}