]>
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 | 30 | struct nfsd_drc_bucket { |
bedd4b61 | 31 | struct list_head lru_head; |
7142b98d TM |
32 | }; |
33 | ||
34 | static struct nfsd_drc_bucket *drc_hashtbl; | |
8a8bc40d | 35 | static struct kmem_cache *drc_slab; |
9dc56143 JL |
36 | |
37 | /* max number of entries allowed in the cache */ | |
0338dd15 | 38 | static unsigned int max_drc_entries; |
1da177e4 | 39 | |
0733c7ba JL |
40 | /* number of significant bits in the hash value */ |
41 | static unsigned int maskbits; | |
bedd4b61 | 42 | static unsigned int drc_hashsize; |
0733c7ba | 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 */ | |
31e60f52 | 50 | static atomic_t num_drc_entries; |
9dc56143 JL |
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 | } |
f09841fd JL |
140 | return rp; |
141 | } | |
1da177e4 | 142 | |
f09841fd JL |
143 | static void |
144 | nfsd_reply_cache_free_locked(struct svc_cacherep *rp) | |
145 | { | |
6c6910cd JL |
146 | if (rp->c_type == RC_REPLBUFF && rp->c_replvec.iov_base) { |
147 | drc_mem_usage -= rp->c_replvec.iov_len; | |
f09841fd | 148 | kfree(rp->c_replvec.iov_base); |
6c6910cd | 149 | } |
f09841fd | 150 | list_del(&rp->c_lru); |
31e60f52 | 151 | atomic_dec(&num_drc_entries); |
6c6910cd | 152 | drc_mem_usage -= sizeof(*rp); |
f09841fd JL |
153 | kmem_cache_free(drc_slab, rp); |
154 | } | |
155 | ||
2c6b691c JL |
156 | static void |
157 | nfsd_reply_cache_free(struct svc_cacherep *rp) | |
158 | { | |
159 | spin_lock(&cache_lock); | |
160 | nfsd_reply_cache_free_locked(rp); | |
161 | spin_unlock(&cache_lock); | |
162 | } | |
163 | ||
f09841fd JL |
164 | int nfsd_reply_cache_init(void) |
165 | { | |
0733c7ba | 166 | unsigned int hashsize; |
bedd4b61 | 167 | unsigned int i; |
0733c7ba | 168 | |
ac534ff2 | 169 | max_drc_entries = nfsd_cache_size_limit(); |
31e60f52 | 170 | atomic_set(&num_drc_entries, 0); |
0733c7ba JL |
171 | hashsize = nfsd_hashsize(max_drc_entries); |
172 | maskbits = ilog2(hashsize); | |
ac534ff2 | 173 | |
b4e7f2c9 | 174 | register_shrinker(&nfsd_reply_cache_shrinker); |
8a8bc40d JL |
175 | drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep), |
176 | 0, 0, NULL); | |
177 | if (!drc_slab) | |
178 | goto out_nomem; | |
179 | ||
7142b98d TM |
180 | drc_hashtbl = kcalloc(hashsize, sizeof(*drc_hashtbl), GFP_KERNEL); |
181 | if (!drc_hashtbl) | |
d5c3428b | 182 | goto out_nomem; |
bedd4b61 TM |
183 | for (i = 0; i < hashsize; i++) |
184 | INIT_LIST_HEAD(&drc_hashtbl[i].lru_head); | |
185 | drc_hashsize = hashsize; | |
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; | |
bedd4b61 | 197 | unsigned int i; |
1da177e4 | 198 | |
b4e7f2c9 | 199 | unregister_shrinker(&nfsd_reply_cache_shrinker); |
aca8a23d JL |
200 | cancel_delayed_work_sync(&cache_cleaner); |
201 | ||
bedd4b61 TM |
202 | for (i = 0; i < drc_hashsize; i++) { |
203 | struct list_head *head = &drc_hashtbl[i].lru_head; | |
204 | while (!list_empty(head)) { | |
205 | rp = list_first_entry(head, struct svc_cacherep, c_lru); | |
206 | nfsd_reply_cache_free_locked(rp); | |
207 | } | |
1da177e4 LT |
208 | } |
209 | ||
7142b98d TM |
210 | kfree (drc_hashtbl); |
211 | drc_hashtbl = NULL; | |
bedd4b61 | 212 | drc_hashsize = 0; |
8a8bc40d JL |
213 | |
214 | if (drc_slab) { | |
215 | kmem_cache_destroy(drc_slab); | |
216 | drc_slab = NULL; | |
217 | } | |
1da177e4 LT |
218 | } |
219 | ||
220 | /* | |
aca8a23d JL |
221 | * Move cache entry to end of LRU list, and queue the cleaner to run if it's |
222 | * not already scheduled. | |
1da177e4 LT |
223 | */ |
224 | static void | |
bedd4b61 | 225 | lru_put_end(struct nfsd_drc_bucket *b, struct svc_cacherep *rp) |
1da177e4 | 226 | { |
56c2548b | 227 | rp->c_timestamp = jiffies; |
bedd4b61 | 228 | list_move_tail(&rp->c_lru, &b->lru_head); |
aca8a23d | 229 | schedule_delayed_work(&cache_cleaner, RC_EXPIRE); |
1da177e4 LT |
230 | } |
231 | ||
1ab6c499 | 232 | static long |
bedd4b61 | 233 | prune_bucket(struct nfsd_drc_bucket *b) |
aca8a23d JL |
234 | { |
235 | struct svc_cacherep *rp, *tmp; | |
1ab6c499 | 236 | long freed = 0; |
aca8a23d | 237 | |
bedd4b61 | 238 | list_for_each_entry_safe(rp, tmp, &b->lru_head, c_lru) { |
1b19453d JL |
239 | /* |
240 | * Don't free entries attached to calls that are still | |
241 | * in-progress, but do keep scanning the list. | |
242 | */ | |
243 | if (rp->c_state == RC_INPROG) | |
244 | continue; | |
31e60f52 | 245 | if (atomic_read(&num_drc_entries) <= max_drc_entries && |
1b19453d | 246 | time_before(jiffies, rp->c_timestamp + RC_EXPIRE)) |
aca8a23d JL |
247 | break; |
248 | nfsd_reply_cache_free_locked(rp); | |
1ab6c499 | 249 | freed++; |
aca8a23d | 250 | } |
bedd4b61 TM |
251 | return freed; |
252 | } | |
253 | ||
254 | /* | |
255 | * Walk the LRU list and prune off entries that are older than RC_EXPIRE. | |
256 | * Also prune the oldest ones when the total exceeds the max number of entries. | |
257 | */ | |
258 | static long | |
259 | prune_cache_entries(void) | |
260 | { | |
261 | unsigned int i; | |
262 | long freed = 0; | |
263 | bool cancel = true; | |
264 | ||
265 | for (i = 0; i < drc_hashsize; i++) { | |
266 | struct nfsd_drc_bucket *b = &drc_hashtbl[i]; | |
267 | ||
268 | freed += prune_bucket(b); | |
269 | if (!list_empty(&b->lru_head)) | |
270 | cancel = false; | |
271 | } | |
aca8a23d JL |
272 | |
273 | /* | |
bedd4b61 TM |
274 | * Conditionally rearm the job to run in RC_EXPIRE since we just |
275 | * ran the pruner. | |
aca8a23d | 276 | */ |
bedd4b61 | 277 | if (!cancel) |
aca8a23d | 278 | mod_delayed_work(system_wq, &cache_cleaner, RC_EXPIRE); |
1ab6c499 | 279 | return freed; |
aca8a23d JL |
280 | } |
281 | ||
282 | static void | |
283 | cache_cleaner_func(struct work_struct *unused) | |
284 | { | |
285 | spin_lock(&cache_lock); | |
286 | prune_cache_entries(); | |
287 | spin_unlock(&cache_lock); | |
288 | } | |
289 | ||
1ab6c499 DC |
290 | static unsigned long |
291 | nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc) | |
b4e7f2c9 | 292 | { |
31e60f52 | 293 | return atomic_read(&num_drc_entries); |
b4e7f2c9 JL |
294 | } |
295 | ||
1ab6c499 DC |
296 | static unsigned long |
297 | nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc) | |
298 | { | |
299 | unsigned long freed; | |
300 | ||
301 | spin_lock(&cache_lock); | |
302 | freed = prune_cache_entries(); | |
303 | spin_unlock(&cache_lock); | |
304 | return freed; | |
305 | } | |
01a7decf JL |
306 | /* |
307 | * Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes | |
308 | */ | |
309 | static __wsum | |
310 | nfsd_cache_csum(struct svc_rqst *rqstp) | |
311 | { | |
312 | int idx; | |
313 | unsigned int base; | |
314 | __wsum csum; | |
315 | struct xdr_buf *buf = &rqstp->rq_arg; | |
316 | const unsigned char *p = buf->head[0].iov_base; | |
317 | size_t csum_len = min_t(size_t, buf->head[0].iov_len + buf->page_len, | |
318 | RC_CSUMLEN); | |
319 | size_t len = min(buf->head[0].iov_len, csum_len); | |
320 | ||
321 | /* rq_arg.head first */ | |
322 | csum = csum_partial(p, len, 0); | |
323 | csum_len -= len; | |
324 | ||
325 | /* Continue into page array */ | |
326 | idx = buf->page_base / PAGE_SIZE; | |
327 | base = buf->page_base & ~PAGE_MASK; | |
328 | while (csum_len) { | |
329 | p = page_address(buf->pages[idx]) + base; | |
56edc86b | 330 | len = min_t(size_t, PAGE_SIZE - base, csum_len); |
01a7decf JL |
331 | csum = csum_partial(p, len, csum); |
332 | csum_len -= len; | |
333 | base = 0; | |
334 | ++idx; | |
335 | } | |
336 | return csum; | |
337 | } | |
338 | ||
9dc56143 JL |
339 | static bool |
340 | nfsd_cache_match(struct svc_rqst *rqstp, __wsum csum, struct svc_cacherep *rp) | |
341 | { | |
342 | /* Check RPC header info first */ | |
343 | if (rqstp->rq_xid != rp->c_xid || rqstp->rq_proc != rp->c_proc || | |
344 | rqstp->rq_prot != rp->c_prot || rqstp->rq_vers != rp->c_vers || | |
345 | rqstp->rq_arg.len != rp->c_len || | |
346 | !rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) || | |
347 | rpc_get_port(svc_addr(rqstp)) != rpc_get_port((struct sockaddr *)&rp->c_addr)) | |
348 | return false; | |
349 | ||
350 | /* compare checksum of NFS data */ | |
351 | if (csum != rp->c_csum) { | |
352 | ++payload_misses; | |
353 | return false; | |
354 | } | |
355 | ||
356 | return true; | |
357 | } | |
358 | ||
a4a3ec32 JL |
359 | /* |
360 | * Search the request hash for an entry that matches the given rqstp. | |
361 | * Must be called with cache_lock held. Returns the found entry or | |
362 | * NULL on failure. | |
363 | */ | |
364 | static struct svc_cacherep * | |
7142b98d TM |
365 | nfsd_cache_search(struct nfsd_drc_bucket *b, struct svc_rqst *rqstp, |
366 | __wsum csum) | |
a4a3ec32 | 367 | { |
98d821bd | 368 | struct svc_cacherep *rp, *ret = NULL; |
11acf6ef | 369 | struct list_head *rh = &b->lru_head; |
98d821bd | 370 | unsigned int entries = 0; |
a4a3ec32 | 371 | |
11acf6ef | 372 | list_for_each_entry(rp, rh, c_lru) { |
98d821bd JL |
373 | ++entries; |
374 | if (nfsd_cache_match(rqstp, csum, rp)) { | |
375 | ret = rp; | |
376 | break; | |
377 | } | |
378 | } | |
379 | ||
380 | /* tally hash chain length stats */ | |
381 | if (entries > longest_chain) { | |
382 | longest_chain = entries; | |
31e60f52 | 383 | longest_chain_cachesize = atomic_read(&num_drc_entries); |
98d821bd JL |
384 | } else if (entries == longest_chain) { |
385 | /* prefer to keep the smallest cachesize possible here */ | |
31e60f52 TM |
386 | longest_chain_cachesize = min_t(unsigned int, |
387 | longest_chain_cachesize, | |
388 | atomic_read(&num_drc_entries)); | |
a4a3ec32 | 389 | } |
98d821bd JL |
390 | |
391 | return ret; | |
a4a3ec32 JL |
392 | } |
393 | ||
1da177e4 LT |
394 | /* |
395 | * Try to find an entry matching the current call in the cache. When none | |
1ac83629 JL |
396 | * is found, we try to grab the oldest expired entry off the LRU list. If |
397 | * a suitable one isn't there, then drop the cache_lock and allocate a | |
398 | * new one, then search again in case one got inserted while this thread | |
399 | * didn't hold the lock. | |
1da177e4 LT |
400 | */ |
401 | int | |
1091006c | 402 | nfsd_cache_lookup(struct svc_rqst *rqstp) |
1da177e4 | 403 | { |
0338dd15 | 404 | struct svc_cacherep *rp, *found; |
c7afef1f AV |
405 | __be32 xid = rqstp->rq_xid; |
406 | u32 proto = rqstp->rq_prot, | |
1da177e4 LT |
407 | vers = rqstp->rq_vers, |
408 | proc = rqstp->rq_proc; | |
01a7decf | 409 | __wsum csum; |
7142b98d TM |
410 | u32 hash = nfsd_cache_hash(xid); |
411 | struct nfsd_drc_bucket *b = &drc_hashtbl[hash]; | |
1da177e4 | 412 | unsigned long age; |
1091006c | 413 | int type = rqstp->rq_cachetype; |
0b9ea37f | 414 | int rtn = RC_DOIT; |
1da177e4 LT |
415 | |
416 | rqstp->rq_cacherep = NULL; | |
13cc8a78 | 417 | if (type == RC_NOCACHE) { |
1da177e4 | 418 | nfsdstats.rcnocache++; |
0b9ea37f | 419 | return rtn; |
1da177e4 LT |
420 | } |
421 | ||
01a7decf JL |
422 | csum = nfsd_cache_csum(rqstp); |
423 | ||
0b9ea37f JL |
424 | /* |
425 | * Since the common case is a cache miss followed by an insert, | |
a0ef5e19 | 426 | * preallocate an entry. |
0b9ea37f | 427 | */ |
0338dd15 | 428 | rp = nfsd_reply_cache_alloc(); |
0338dd15 | 429 | spin_lock(&cache_lock); |
6c6910cd | 430 | if (likely(rp)) { |
31e60f52 | 431 | atomic_inc(&num_drc_entries); |
6c6910cd JL |
432 | drc_mem_usage += sizeof(*rp); |
433 | } | |
0338dd15 | 434 | |
a0ef5e19 JL |
435 | /* go ahead and prune the cache */ |
436 | prune_cache_entries(); | |
437 | ||
7142b98d | 438 | found = nfsd_cache_search(b, rqstp, csum); |
0338dd15 | 439 | if (found) { |
0b9ea37f JL |
440 | if (likely(rp)) |
441 | nfsd_reply_cache_free_locked(rp); | |
0338dd15 JL |
442 | rp = found; |
443 | goto found_entry; | |
1da177e4 LT |
444 | } |
445 | ||
0b9ea37f JL |
446 | if (!rp) { |
447 | dprintk("nfsd: unable to allocate DRC entry!\n"); | |
448 | goto out; | |
449 | } | |
450 | ||
0338dd15 | 451 | nfsdstats.rcmisses++; |
1da177e4 LT |
452 | rqstp->rq_cacherep = rp; |
453 | rp->c_state = RC_INPROG; | |
454 | rp->c_xid = xid; | |
455 | rp->c_proc = proc; | |
7b9e8522 JL |
456 | rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp)); |
457 | rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp))); | |
1da177e4 LT |
458 | rp->c_prot = proto; |
459 | rp->c_vers = vers; | |
01a7decf JL |
460 | rp->c_len = rqstp->rq_arg.len; |
461 | rp->c_csum = csum; | |
1da177e4 | 462 | |
bedd4b61 | 463 | lru_put_end(b, rp); |
1da177e4 LT |
464 | |
465 | /* release any buffer */ | |
466 | if (rp->c_type == RC_REPLBUFF) { | |
6c6910cd | 467 | drc_mem_usage -= rp->c_replvec.iov_len; |
1da177e4 LT |
468 | kfree(rp->c_replvec.iov_base); |
469 | rp->c_replvec.iov_base = NULL; | |
470 | } | |
471 | rp->c_type = RC_NOCACHE; | |
472 | out: | |
473 | spin_unlock(&cache_lock); | |
474 | return rtn; | |
475 | ||
476 | found_entry: | |
0338dd15 | 477 | nfsdstats.rchits++; |
1da177e4 LT |
478 | /* We found a matching entry which is either in progress or done. */ |
479 | age = jiffies - rp->c_timestamp; | |
bedd4b61 | 480 | lru_put_end(b, rp); |
1da177e4 LT |
481 | |
482 | rtn = RC_DROPIT; | |
483 | /* Request being processed or excessive rexmits */ | |
484 | if (rp->c_state == RC_INPROG || age < RC_DELAY) | |
485 | goto out; | |
486 | ||
487 | /* From the hall of fame of impractical attacks: | |
488 | * Is this a user who tries to snoop on the cache? */ | |
489 | rtn = RC_DOIT; | |
490 | if (!rqstp->rq_secure && rp->c_secure) | |
491 | goto out; | |
492 | ||
493 | /* Compose RPC reply header */ | |
494 | switch (rp->c_type) { | |
495 | case RC_NOCACHE: | |
496 | break; | |
497 | case RC_REPLSTAT: | |
498 | svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat); | |
499 | rtn = RC_REPLY; | |
500 | break; | |
501 | case RC_REPLBUFF: | |
502 | if (!nfsd_cache_append(rqstp, &rp->c_replvec)) | |
503 | goto out; /* should not happen */ | |
504 | rtn = RC_REPLY; | |
505 | break; | |
506 | default: | |
507 | printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type); | |
0338dd15 | 508 | nfsd_reply_cache_free_locked(rp); |
1da177e4 LT |
509 | } |
510 | ||
511 | goto out; | |
512 | } | |
513 | ||
514 | /* | |
515 | * Update a cache entry. This is called from nfsd_dispatch when | |
516 | * the procedure has been executed and the complete reply is in | |
517 | * rqstp->rq_res. | |
518 | * | |
519 | * We're copying around data here rather than swapping buffers because | |
520 | * the toplevel loop requires max-sized buffers, which would be a waste | |
521 | * of memory for a cache with a max reply size of 100 bytes (diropokres). | |
522 | * | |
523 | * If we should start to use different types of cache entries tailored | |
524 | * specifically for attrstat and fh's, we may save even more space. | |
525 | * | |
526 | * Also note that a cachetype of RC_NOCACHE can legally be passed when | |
527 | * nfsd failed to encode a reply that otherwise would have been cached. | |
528 | * In this case, nfsd_cache_update is called with statp == NULL. | |
529 | */ | |
530 | void | |
c7afef1f | 531 | nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp) |
1da177e4 | 532 | { |
13cc8a78 | 533 | struct svc_cacherep *rp = rqstp->rq_cacherep; |
1da177e4 | 534 | struct kvec *resv = &rqstp->rq_res.head[0], *cachv; |
bedd4b61 TM |
535 | u32 hash; |
536 | struct nfsd_drc_bucket *b; | |
1da177e4 | 537 | int len; |
6c6910cd | 538 | size_t bufsize = 0; |
1da177e4 | 539 | |
13cc8a78 | 540 | if (!rp) |
1da177e4 LT |
541 | return; |
542 | ||
bedd4b61 TM |
543 | hash = nfsd_cache_hash(rp->c_xid); |
544 | b = &drc_hashtbl[hash]; | |
545 | ||
1da177e4 LT |
546 | len = resv->iov_len - ((char*)statp - (char*)resv->iov_base); |
547 | len >>= 2; | |
fca4217c | 548 | |
1da177e4 LT |
549 | /* Don't cache excessive amounts of data and XDR failures */ |
550 | if (!statp || len > (256 >> 2)) { | |
2c6b691c | 551 | nfsd_reply_cache_free(rp); |
1da177e4 LT |
552 | return; |
553 | } | |
554 | ||
555 | switch (cachetype) { | |
556 | case RC_REPLSTAT: | |
557 | if (len != 1) | |
558 | printk("nfsd: RC_REPLSTAT/reply len %d!\n",len); | |
559 | rp->c_replstat = *statp; | |
560 | break; | |
561 | case RC_REPLBUFF: | |
562 | cachv = &rp->c_replvec; | |
6c6910cd JL |
563 | bufsize = len << 2; |
564 | cachv->iov_base = kmalloc(bufsize, GFP_KERNEL); | |
1da177e4 | 565 | if (!cachv->iov_base) { |
2c6b691c | 566 | nfsd_reply_cache_free(rp); |
1da177e4 LT |
567 | return; |
568 | } | |
6c6910cd JL |
569 | cachv->iov_len = bufsize; |
570 | memcpy(cachv->iov_base, statp, bufsize); | |
1da177e4 | 571 | break; |
2c6b691c JL |
572 | case RC_NOCACHE: |
573 | nfsd_reply_cache_free(rp); | |
574 | return; | |
1da177e4 LT |
575 | } |
576 | spin_lock(&cache_lock); | |
6c6910cd | 577 | drc_mem_usage += bufsize; |
bedd4b61 | 578 | lru_put_end(b, rp); |
1da177e4 LT |
579 | rp->c_secure = rqstp->rq_secure; |
580 | rp->c_type = cachetype; | |
581 | rp->c_state = RC_DONE; | |
1da177e4 LT |
582 | spin_unlock(&cache_lock); |
583 | return; | |
584 | } | |
585 | ||
586 | /* | |
587 | * Copy cached reply to current reply buffer. Should always fit. | |
588 | * FIXME as reply is in a page, we should just attach the page, and | |
589 | * keep a refcount.... | |
590 | */ | |
591 | static int | |
592 | nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data) | |
593 | { | |
594 | struct kvec *vec = &rqstp->rq_res.head[0]; | |
595 | ||
596 | if (vec->iov_len + data->iov_len > PAGE_SIZE) { | |
597 | printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n", | |
598 | data->iov_len); | |
599 | return 0; | |
600 | } | |
601 | memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len); | |
602 | vec->iov_len += data->iov_len; | |
603 | return 1; | |
604 | } | |
a2f999a3 JL |
605 | |
606 | /* | |
607 | * Note that fields may be added, removed or reordered in the future. Programs | |
608 | * scraping this file for info should test the labels to ensure they're | |
609 | * getting the correct field. | |
610 | */ | |
611 | static int nfsd_reply_cache_stats_show(struct seq_file *m, void *v) | |
612 | { | |
613 | spin_lock(&cache_lock); | |
614 | seq_printf(m, "max entries: %u\n", max_drc_entries); | |
31e60f52 TM |
615 | seq_printf(m, "num entries: %u\n", |
616 | atomic_read(&num_drc_entries)); | |
0733c7ba | 617 | seq_printf(m, "hash buckets: %u\n", 1 << maskbits); |
a2f999a3 JL |
618 | seq_printf(m, "mem usage: %u\n", drc_mem_usage); |
619 | seq_printf(m, "cache hits: %u\n", nfsdstats.rchits); | |
620 | seq_printf(m, "cache misses: %u\n", nfsdstats.rcmisses); | |
621 | seq_printf(m, "not cached: %u\n", nfsdstats.rcnocache); | |
622 | seq_printf(m, "payload misses: %u\n", payload_misses); | |
98d821bd JL |
623 | seq_printf(m, "longest chain len: %u\n", longest_chain); |
624 | seq_printf(m, "cachesize at longest: %u\n", longest_chain_cachesize); | |
a2f999a3 JL |
625 | spin_unlock(&cache_lock); |
626 | return 0; | |
627 | } | |
628 | ||
629 | int nfsd_reply_cache_stats_open(struct inode *inode, struct file *file) | |
630 | { | |
631 | return single_open(file, nfsd_reply_cache_stats_show, NULL); | |
632 | } |