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