xref: /linux/fs/nfsd/nfscache.c (revision d141ec2825b4d3ec52f27c43bdd864090159273a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Request reply cache. This is currently a global cache, but this may
4  * change in the future and be a per-client cache.
5  *
6  * This code is heavily inspired by the 44BSD implementation, although
7  * it does things a bit differently.
8  *
9  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
10  */
11 
12 #include <linux/sunrpc/svc_xprt.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/sunrpc/addr.h>
16 #include <linux/highmem.h>
17 #include <linux/log2.h>
18 #include <linux/hash.h>
19 #include <net/checksum.h>
20 
21 #include "nfsd.h"
22 #include "netns.h"
23 #include "stats.h"
24 #include "cache.h"
25 #include "trace.h"
26 
27 /*
28  * We use this value to determine the number of hash buckets from the max
29  * cache size, the idea being that when the cache is at its maximum number
30  * of entries, then this should be the average number of entries per bucket.
31  */
32 #define TARGET_BUCKET_SIZE	8
33 
34 struct nfsd_drc_bucket {
35 	struct rb_root rb_head;
36 	struct list_head lru_head;
37 	spinlock_t cache_lock;
38 };
39 
40 static struct kmem_cache	*drc_slab;
41 
42 static int	nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
43 static unsigned long nfsd_reply_cache_count(struct shrinker *shrink,
44 					    struct shrink_control *sc);
45 static unsigned long nfsd_reply_cache_scan(struct shrinker *shrink,
46 					   struct shrink_control *sc);
47 
48 /*
49  * Put a cap on the size of the DRC based on the amount of available
50  * low memory in the machine.
51  *
52  *  64MB:    8192
53  * 128MB:   11585
54  * 256MB:   16384
55  * 512MB:   23170
56  *   1GB:   32768
57  *   2GB:   46340
58  *   4GB:   65536
59  *   8GB:   92681
60  *  16GB:  131072
61  *
62  * ...with a hard cap of 256k entries. In the worst case, each entry will be
63  * ~1k, so the above numbers should give a rough max of the amount of memory
64  * used in k.
65  *
66  * XXX: these limits are per-container, so memory used will increase
67  * linearly with number of containers.  Maybe that's OK.
68  */
69 static unsigned int
nfsd_cache_size_limit(void)70 nfsd_cache_size_limit(void)
71 {
72 	unsigned int limit;
73 	unsigned long low_pages = totalram_pages() - totalhigh_pages();
74 
75 	limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
76 	return min_t(unsigned int, limit, 256*1024);
77 }
78 
79 /*
80  * Compute the number of hash buckets we need. Divide the max cachesize by
81  * the "target" max bucket size, and round up to next power of two.
82  */
83 static unsigned int
nfsd_hashsize(unsigned int limit)84 nfsd_hashsize(unsigned int limit)
85 {
86 	return roundup_pow_of_two(limit / TARGET_BUCKET_SIZE);
87 }
88 
89 static struct nfsd_cacherep *
nfsd_cacherep_alloc(struct svc_rqst * rqstp,__wsum csum,struct nfsd_net * nn)90 nfsd_cacherep_alloc(struct svc_rqst *rqstp, __wsum csum,
91 		    struct nfsd_net *nn)
92 {
93 	struct nfsd_cacherep *rp;
94 
95 	rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
96 	if (rp) {
97 		rp->c_state = RC_UNUSED;
98 		rp->c_type = RC_NOCACHE;
99 		RB_CLEAR_NODE(&rp->c_node);
100 		INIT_LIST_HEAD(&rp->c_lru);
101 
102 		memset(&rp->c_key, 0, sizeof(rp->c_key));
103 		rp->c_key.k_xid = rqstp->rq_xid;
104 		rp->c_key.k_proc = rqstp->rq_proc;
105 		rpc_copy_addr((struct sockaddr *)&rp->c_key.k_addr, svc_addr(rqstp));
106 		rpc_set_port((struct sockaddr *)&rp->c_key.k_addr, rpc_get_port(svc_addr(rqstp)));
107 		rp->c_key.k_prot = rqstp->rq_prot;
108 		rp->c_key.k_vers = rqstp->rq_vers;
109 		rp->c_key.k_len = rqstp->rq_arg.len;
110 		rp->c_key.k_csum = csum;
111 	}
112 	return rp;
113 }
114 
nfsd_cacherep_free(struct nfsd_cacherep * rp)115 static void nfsd_cacherep_free(struct nfsd_cacherep *rp)
116 {
117 	if (rp->c_type == RC_REPLBUFF)
118 		kfree(rp->c_replvec.iov_base);
119 	kmem_cache_free(drc_slab, rp);
120 }
121 
122 static unsigned long
nfsd_cacherep_dispose(struct list_head * dispose)123 nfsd_cacherep_dispose(struct list_head *dispose)
124 {
125 	struct nfsd_cacherep *rp;
126 	unsigned long freed = 0;
127 
128 	while (!list_empty(dispose)) {
129 		rp = list_first_entry(dispose, struct nfsd_cacherep, c_lru);
130 		list_del(&rp->c_lru);
131 		nfsd_cacherep_free(rp);
132 		freed++;
133 	}
134 	return freed;
135 }
136 
137 static void
nfsd_cacherep_unlink_locked(struct nfsd_net * nn,struct nfsd_drc_bucket * b,struct nfsd_cacherep * rp)138 nfsd_cacherep_unlink_locked(struct nfsd_net *nn, struct nfsd_drc_bucket *b,
139 			    struct nfsd_cacherep *rp)
140 {
141 	if (rp->c_type == RC_REPLBUFF && rp->c_replvec.iov_base)
142 		nfsd_stats_drc_mem_usage_sub(nn, rp->c_replvec.iov_len);
143 	if (rp->c_state != RC_UNUSED) {
144 		rb_erase(&rp->c_node, &b->rb_head);
145 		list_del(&rp->c_lru);
146 		atomic_dec(&nn->num_drc_entries);
147 		nfsd_stats_drc_mem_usage_sub(nn, sizeof(*rp));
148 	}
149 }
150 
151 static void
nfsd_reply_cache_free_locked(struct nfsd_drc_bucket * b,struct nfsd_cacherep * rp,struct nfsd_net * nn)152 nfsd_reply_cache_free_locked(struct nfsd_drc_bucket *b, struct nfsd_cacherep *rp,
153 				struct nfsd_net *nn)
154 {
155 	nfsd_cacherep_unlink_locked(nn, b, rp);
156 	nfsd_cacherep_free(rp);
157 }
158 
159 static void
nfsd_reply_cache_free(struct nfsd_drc_bucket * b,struct nfsd_cacherep * rp,struct nfsd_net * nn)160 nfsd_reply_cache_free(struct nfsd_drc_bucket *b, struct nfsd_cacherep *rp,
161 			struct nfsd_net *nn)
162 {
163 	spin_lock(&b->cache_lock);
164 	nfsd_cacherep_unlink_locked(nn, b, rp);
165 	spin_unlock(&b->cache_lock);
166 	nfsd_cacherep_free(rp);
167 }
168 
nfsd_drc_slab_create(void)169 int nfsd_drc_slab_create(void)
170 {
171 	drc_slab = KMEM_CACHE(nfsd_cacherep, 0);
172 	return drc_slab ? 0: -ENOMEM;
173 }
174 
nfsd_drc_slab_free(void)175 void nfsd_drc_slab_free(void)
176 {
177 	kmem_cache_destroy(drc_slab);
178 }
179 
nfsd_reply_cache_init(struct nfsd_net * nn)180 int nfsd_reply_cache_init(struct nfsd_net *nn)
181 {
182 	unsigned int hashsize;
183 	unsigned int i;
184 
185 	nn->max_drc_entries = nfsd_cache_size_limit();
186 	atomic_set(&nn->num_drc_entries, 0);
187 	hashsize = nfsd_hashsize(nn->max_drc_entries);
188 	nn->maskbits = ilog2(hashsize);
189 
190 	nn->drc_hashtbl = kvzalloc(array_size(hashsize,
191 				sizeof(*nn->drc_hashtbl)), GFP_KERNEL);
192 	if (!nn->drc_hashtbl)
193 		return -ENOMEM;
194 
195 	nn->nfsd_reply_cache_shrinker = shrinker_alloc(0, "nfsd-reply:%s",
196 						       nn->nfsd_name);
197 	if (!nn->nfsd_reply_cache_shrinker)
198 		goto out_shrinker;
199 
200 	nn->nfsd_reply_cache_shrinker->scan_objects = nfsd_reply_cache_scan;
201 	nn->nfsd_reply_cache_shrinker->count_objects = nfsd_reply_cache_count;
202 	nn->nfsd_reply_cache_shrinker->seeks = 1;
203 	nn->nfsd_reply_cache_shrinker->private_data = nn;
204 
205 	for (i = 0; i < hashsize; i++) {
206 		INIT_LIST_HEAD(&nn->drc_hashtbl[i].lru_head);
207 		spin_lock_init(&nn->drc_hashtbl[i].cache_lock);
208 	}
209 	nn->drc_hashsize = hashsize;
210 
211 	shrinker_register(nn->nfsd_reply_cache_shrinker);
212 
213 	return 0;
214 out_shrinker:
215 	kvfree(nn->drc_hashtbl);
216 	printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
217 	return -ENOMEM;
218 }
219 
nfsd_reply_cache_shutdown(struct nfsd_net * nn)220 void nfsd_reply_cache_shutdown(struct nfsd_net *nn)
221 {
222 	struct nfsd_cacherep *rp;
223 	unsigned int i;
224 
225 	shrinker_free(nn->nfsd_reply_cache_shrinker);
226 
227 	for (i = 0; i < nn->drc_hashsize; i++) {
228 		struct list_head *head = &nn->drc_hashtbl[i].lru_head;
229 		while (!list_empty(head)) {
230 			rp = list_first_entry(head, struct nfsd_cacherep, c_lru);
231 			nfsd_reply_cache_free_locked(&nn->drc_hashtbl[i],
232 									rp, nn);
233 		}
234 	}
235 
236 	kvfree(nn->drc_hashtbl);
237 	nn->drc_hashtbl = NULL;
238 	nn->drc_hashsize = 0;
239 
240 }
241 
242 static void
lru_put_end(struct nfsd_drc_bucket * b,struct nfsd_cacherep * rp)243 lru_put_end(struct nfsd_drc_bucket *b, struct nfsd_cacherep *rp)
244 {
245 	rp->c_timestamp = jiffies;
246 	list_move_tail(&rp->c_lru, &b->lru_head);
247 }
248 
249 static noinline struct nfsd_drc_bucket *
nfsd_cache_bucket_find(__be32 xid,struct nfsd_net * nn)250 nfsd_cache_bucket_find(__be32 xid, struct nfsd_net *nn)
251 {
252 	unsigned int hash = hash_32((__force u32)xid, nn->maskbits);
253 
254 	return &nn->drc_hashtbl[hash];
255 }
256 
257 /*
258  * Remove and return no more than @max expired entries in bucket @b.
259  * If @max is zero, do not limit the number of removed entries.
260  */
261 static void
nfsd_prune_bucket_locked(struct nfsd_net * nn,struct nfsd_drc_bucket * b,unsigned int max,struct list_head * dispose)262 nfsd_prune_bucket_locked(struct nfsd_net *nn, struct nfsd_drc_bucket *b,
263 			 unsigned int max, struct list_head *dispose)
264 {
265 	unsigned long expiry = jiffies - RC_EXPIRE;
266 	struct nfsd_cacherep *rp, *tmp;
267 	unsigned int freed = 0;
268 
269 	lockdep_assert_held(&b->cache_lock);
270 
271 	/* The bucket LRU is ordered oldest-first. */
272 	list_for_each_entry_safe(rp, tmp, &b->lru_head, c_lru) {
273 		if (atomic_read(&nn->num_drc_entries) <= nn->max_drc_entries &&
274 		    time_before(expiry, rp->c_timestamp))
275 			break;
276 
277 		nfsd_cacherep_unlink_locked(nn, b, rp);
278 		list_add(&rp->c_lru, dispose);
279 
280 		if (max && ++freed >= max)
281 			break;
282 	}
283 }
284 
285 /**
286  * nfsd_reply_cache_count - count_objects method for the DRC shrinker
287  * @shrink: our registered shrinker context
288  * @sc: garbage collection parameters
289  *
290  * Returns the total number of entries in the duplicate reply cache. To
291  * keep things simple and quick, this is not the number of expired entries
292  * in the cache (ie, the number that would be removed by a call to
293  * nfsd_reply_cache_scan).
294  */
295 static unsigned long
nfsd_reply_cache_count(struct shrinker * shrink,struct shrink_control * sc)296 nfsd_reply_cache_count(struct shrinker *shrink, struct shrink_control *sc)
297 {
298 	struct nfsd_net *nn = shrink->private_data;
299 
300 	return atomic_read(&nn->num_drc_entries);
301 }
302 
303 /**
304  * nfsd_reply_cache_scan - scan_objects method for the DRC shrinker
305  * @shrink: our registered shrinker context
306  * @sc: garbage collection parameters
307  *
308  * Free expired entries on each bucket's LRU list until we've released
309  * nr_to_scan freed objects. Nothing will be released if the cache
310  * has not exceeded it's max_drc_entries limit.
311  *
312  * Returns the number of entries released by this call.
313  */
314 static unsigned long
nfsd_reply_cache_scan(struct shrinker * shrink,struct shrink_control * sc)315 nfsd_reply_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
316 {
317 	struct nfsd_net *nn = shrink->private_data;
318 	unsigned long freed = 0;
319 	LIST_HEAD(dispose);
320 	unsigned int i;
321 
322 	for (i = 0; i < nn->drc_hashsize; i++) {
323 		struct nfsd_drc_bucket *b = &nn->drc_hashtbl[i];
324 
325 		if (list_empty(&b->lru_head))
326 			continue;
327 
328 		spin_lock(&b->cache_lock);
329 		nfsd_prune_bucket_locked(nn, b, 0, &dispose);
330 		spin_unlock(&b->cache_lock);
331 
332 		freed += nfsd_cacherep_dispose(&dispose);
333 		if (freed > sc->nr_to_scan)
334 			break;
335 	}
336 	return freed;
337 }
338 
339 /**
340  * nfsd_cache_csum - Checksum incoming NFS Call arguments
341  * @buf: buffer containing a whole RPC Call message
342  * @start: starting byte of the NFS Call header
343  * @remaining: size of the NFS Call header, in bytes
344  *
345  * Compute a weak checksum of the leading bytes of an NFS procedure
346  * call header to help verify that a retransmitted Call matches an
347  * entry in the duplicate reply cache.
348  *
349  * To avoid assumptions about how the RPC message is laid out in
350  * @buf and what else it might contain (eg, a GSS MIC suffix), the
351  * caller passes us the exact location and length of the NFS Call
352  * header.
353  *
354  * Returns a 32-bit checksum value, as defined in RFC 793.
355  */
nfsd_cache_csum(struct xdr_buf * buf,unsigned int start,unsigned int remaining)356 static __wsum nfsd_cache_csum(struct xdr_buf *buf, unsigned int start,
357 			      unsigned int remaining)
358 {
359 	unsigned int base, len;
360 	struct xdr_buf subbuf;
361 	__wsum csum = 0;
362 	void *p;
363 	int idx;
364 
365 	if (remaining > RC_CSUMLEN)
366 		remaining = RC_CSUMLEN;
367 	if (xdr_buf_subsegment(buf, &subbuf, start, remaining))
368 		return csum;
369 
370 	/* rq_arg.head first */
371 	if (subbuf.head[0].iov_len) {
372 		len = min_t(unsigned int, subbuf.head[0].iov_len, remaining);
373 		csum = csum_partial(subbuf.head[0].iov_base, len, csum);
374 		remaining -= len;
375 	}
376 
377 	/* Continue into page array */
378 	idx = subbuf.page_base / PAGE_SIZE;
379 	base = subbuf.page_base & ~PAGE_MASK;
380 	while (remaining) {
381 		p = page_address(subbuf.pages[idx]) + base;
382 		len = min_t(unsigned int, PAGE_SIZE - base, remaining);
383 		csum = csum_partial(p, len, csum);
384 		remaining -= len;
385 		base = 0;
386 		++idx;
387 	}
388 	return csum;
389 }
390 
391 static int
nfsd_cache_key_cmp(const struct nfsd_cacherep * key,const struct nfsd_cacherep * rp,struct nfsd_net * nn)392 nfsd_cache_key_cmp(const struct nfsd_cacherep *key,
393 		   const struct nfsd_cacherep *rp, struct nfsd_net *nn)
394 {
395 	if (key->c_key.k_xid == rp->c_key.k_xid &&
396 	    key->c_key.k_csum != rp->c_key.k_csum) {
397 		nfsd_stats_payload_misses_inc(nn);
398 		trace_nfsd_drc_mismatch(nn, key, rp);
399 	}
400 
401 	return memcmp(&key->c_key, &rp->c_key, sizeof(key->c_key));
402 }
403 
404 /*
405  * Search the request hash for an entry that matches the given rqstp.
406  * Must be called with cache_lock held. Returns the found entry or
407  * inserts an empty key on failure.
408  */
409 static struct nfsd_cacherep *
nfsd_cache_insert(struct nfsd_drc_bucket * b,struct nfsd_cacherep * key,struct nfsd_net * nn)410 nfsd_cache_insert(struct nfsd_drc_bucket *b, struct nfsd_cacherep *key,
411 			struct nfsd_net *nn)
412 {
413 	struct nfsd_cacherep	*rp, *ret = key;
414 	struct rb_node		**p = &b->rb_head.rb_node,
415 				*parent = NULL;
416 	unsigned int		entries = 0;
417 	int cmp;
418 
419 	while (*p != NULL) {
420 		++entries;
421 		parent = *p;
422 		rp = rb_entry(parent, struct nfsd_cacherep, c_node);
423 
424 		cmp = nfsd_cache_key_cmp(key, rp, nn);
425 		if (cmp < 0)
426 			p = &parent->rb_left;
427 		else if (cmp > 0)
428 			p = &parent->rb_right;
429 		else {
430 			ret = rp;
431 			goto out;
432 		}
433 	}
434 	rb_link_node(&key->c_node, parent, p);
435 	rb_insert_color(&key->c_node, &b->rb_head);
436 out:
437 	/* tally hash chain length stats */
438 	if (entries > nn->longest_chain) {
439 		nn->longest_chain = entries;
440 		nn->longest_chain_cachesize = atomic_read(&nn->num_drc_entries);
441 	} else if (entries == nn->longest_chain) {
442 		/* prefer to keep the smallest cachesize possible here */
443 		nn->longest_chain_cachesize = min_t(unsigned int,
444 				nn->longest_chain_cachesize,
445 				atomic_read(&nn->num_drc_entries));
446 	}
447 	return ret;
448 }
449 
450 /**
451  * nfsd_cache_lookup - Find an entry in the duplicate reply cache
452  * @rqstp: Incoming Call to find
453  * @start: starting byte in @rqstp->rq_arg of the NFS Call header
454  * @len: size of the NFS Call header, in bytes
455  * @cacherep: OUT: DRC entry for this request
456  *
457  * Try to find an entry matching the current call in the cache. When none
458  * is found, we try to grab the oldest expired entry off the LRU list. If
459  * a suitable one isn't there, then drop the cache_lock and allocate a
460  * new one, then search again in case one got inserted while this thread
461  * didn't hold the lock.
462  *
463  * Return values:
464  *   %RC_DOIT: Process the request normally
465  *   %RC_REPLY: Reply from cache
466  *   %RC_DROPIT: Do not process the request further
467  */
nfsd_cache_lookup(struct svc_rqst * rqstp,unsigned int start,unsigned int len,struct nfsd_cacherep ** cacherep)468 int nfsd_cache_lookup(struct svc_rqst *rqstp, unsigned int start,
469 		      unsigned int len, struct nfsd_cacherep **cacherep)
470 {
471 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
472 	struct nfsd_thread_local_info *ntli = rqstp->rq_private;
473 	struct nfsd_cacherep	*rp, *found;
474 	__wsum			csum;
475 	struct nfsd_drc_bucket	*b;
476 	int type = ntli->ntli_cachetype;
477 	LIST_HEAD(dispose);
478 	int rtn = RC_DOIT;
479 
480 	if (type == RC_NOCACHE) {
481 		nfsd_stats_rc_nocache_inc(nn);
482 		goto out;
483 	}
484 
485 	csum = nfsd_cache_csum(&rqstp->rq_arg, start, len);
486 
487 	/*
488 	 * Since the common case is a cache miss followed by an insert,
489 	 * preallocate an entry.
490 	 */
491 	rp = nfsd_cacherep_alloc(rqstp, csum, nn);
492 	if (!rp)
493 		goto out;
494 
495 	b = nfsd_cache_bucket_find(rqstp->rq_xid, nn);
496 	spin_lock(&b->cache_lock);
497 	found = nfsd_cache_insert(b, rp, nn);
498 	if (found != rp)
499 		goto found_entry;
500 	*cacherep = rp;
501 	rp->c_state = RC_INPROG;
502 	nfsd_prune_bucket_locked(nn, b, 3, &dispose);
503 	spin_unlock(&b->cache_lock);
504 
505 	nfsd_cacherep_dispose(&dispose);
506 
507 	nfsd_stats_rc_misses_inc(nn);
508 	atomic_inc(&nn->num_drc_entries);
509 	nfsd_stats_drc_mem_usage_add(nn, sizeof(*rp));
510 	goto out;
511 
512 found_entry:
513 	/* We found a matching entry which is either in progress or done. */
514 	nfsd_reply_cache_free_locked(NULL, rp, nn);
515 	nfsd_stats_rc_hits_inc(nn);
516 	rtn = RC_DROPIT;
517 	rp = found;
518 
519 	/* Request being processed */
520 	if (rp->c_state == RC_INPROG)
521 		goto out_trace;
522 
523 	/* From the hall of fame of impractical attacks:
524 	 * Is this a user who tries to snoop on the cache? */
525 	rtn = RC_DOIT;
526 	if (!test_bit(RQ_SECURE, &rqstp->rq_flags) && rp->c_secure)
527 		goto out_trace;
528 
529 	/* Compose RPC reply header */
530 	switch (rp->c_type) {
531 	case RC_NOCACHE:
532 		break;
533 	case RC_REPLSTAT:
534 		xdr_stream_encode_be32(&rqstp->rq_res_stream, rp->c_replstat);
535 		rtn = RC_REPLY;
536 		break;
537 	case RC_REPLBUFF:
538 		if (!nfsd_cache_append(rqstp, &rp->c_replvec))
539 			goto out_unlock; /* should not happen */
540 		rtn = RC_REPLY;
541 		break;
542 	default:
543 		WARN_ONCE(1, "nfsd: bad repcache type %d\n", rp->c_type);
544 	}
545 
546 out_trace:
547 	trace_nfsd_drc_found(nn, rqstp, rtn);
548 out_unlock:
549 	spin_unlock(&b->cache_lock);
550 out:
551 	return rtn;
552 }
553 
554 /**
555  * nfsd_cache_update - Update an entry in the duplicate reply cache.
556  * @rqstp: svc_rqst with a finished Reply
557  * @rp: IN: DRC entry for this request
558  * @cachetype: which cache to update
559  * @statp: pointer to Reply's NFS status code, or NULL
560  *
561  * This is called from nfsd_dispatch when the procedure has been
562  * executed and the complete reply is in rqstp->rq_res.
563  *
564  * We're copying around data here rather than swapping buffers because
565  * the toplevel loop requires max-sized buffers, which would be a waste
566  * of memory for a cache with a max reply size of 100 bytes (diropokres).
567  *
568  * If we should start to use different types of cache entries tailored
569  * specifically for attrstat and fh's, we may save even more space.
570  *
571  * Also note that a cachetype of RC_NOCACHE can legally be passed when
572  * nfsd failed to encode a reply that otherwise would have been cached.
573  * In this case, nfsd_cache_update is called with statp == NULL.
574  */
nfsd_cache_update(struct svc_rqst * rqstp,struct nfsd_cacherep * rp,int cachetype,__be32 * statp)575 void nfsd_cache_update(struct svc_rqst *rqstp, struct nfsd_cacherep *rp,
576 		       int cachetype, __be32 *statp)
577 {
578 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
579 	struct kvec	*resv = &rqstp->rq_res.head[0], *cachv;
580 	struct nfsd_drc_bucket *b;
581 	int		len;
582 	size_t		bufsize = 0;
583 
584 	if (!rp)
585 		return;
586 
587 	b = nfsd_cache_bucket_find(rp->c_key.k_xid, nn);
588 
589 	len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
590 	len >>= 2;
591 
592 	/* Don't cache excessive amounts of data and XDR failures */
593 	if (!statp || len > (256 >> 2)) {
594 		nfsd_reply_cache_free(b, rp, nn);
595 		return;
596 	}
597 
598 	switch (cachetype) {
599 	case RC_REPLSTAT:
600 		if (len != 1)
601 			printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
602 		rp->c_replstat = *statp;
603 		break;
604 	case RC_REPLBUFF:
605 		cachv = &rp->c_replvec;
606 		bufsize = len << 2;
607 		cachv->iov_base = kmalloc(bufsize, GFP_KERNEL);
608 		if (!cachv->iov_base) {
609 			nfsd_reply_cache_free(b, rp, nn);
610 			return;
611 		}
612 		cachv->iov_len = bufsize;
613 		memcpy(cachv->iov_base, statp, bufsize);
614 		break;
615 	case RC_NOCACHE:
616 		nfsd_reply_cache_free(b, rp, nn);
617 		return;
618 	}
619 	spin_lock(&b->cache_lock);
620 	nfsd_stats_drc_mem_usage_add(nn, bufsize);
621 	lru_put_end(b, rp);
622 	rp->c_secure = test_bit(RQ_SECURE, &rqstp->rq_flags);
623 	rp->c_type = cachetype;
624 	rp->c_state = RC_DONE;
625 	spin_unlock(&b->cache_lock);
626 	return;
627 }
628 
629 static int
nfsd_cache_append(struct svc_rqst * rqstp,struct kvec * data)630 nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
631 {
632 	__be32 *p;
633 
634 	p = xdr_reserve_space(&rqstp->rq_res_stream, data->iov_len);
635 	if (unlikely(!p))
636 		return false;
637 	memcpy(p, data->iov_base, data->iov_len);
638 	xdr_commit_encode(&rqstp->rq_res_stream);
639 	return true;
640 }
641 
642 /*
643  * Note that fields may be added, removed or reordered in the future. Programs
644  * scraping this file for info should test the labels to ensure they're
645  * getting the correct field.
646  */
nfsd_reply_cache_stats_show(struct seq_file * m,void * v)647 int nfsd_reply_cache_stats_show(struct seq_file *m, void *v)
648 {
649 	struct nfsd_net *nn = net_generic(file_inode(m->file)->i_sb->s_fs_info,
650 					  nfsd_net_id);
651 
652 	seq_printf(m, "max entries:           %u\n", nn->max_drc_entries);
653 	seq_printf(m, "num entries:           %u\n",
654 		   atomic_read(&nn->num_drc_entries));
655 	seq_printf(m, "hash buckets:          %u\n", 1 << nn->maskbits);
656 	seq_printf(m, "mem usage:             %lld\n",
657 		   percpu_counter_sum_positive(&nn->counter[NFSD_STATS_DRC_MEM_USAGE]));
658 	seq_printf(m, "cache hits:            %lld\n",
659 		   percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_HITS]));
660 	seq_printf(m, "cache misses:          %lld\n",
661 		   percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_MISSES]));
662 	seq_printf(m, "not cached:            %lld\n",
663 		   percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_NOCACHE]));
664 	seq_printf(m, "payload misses:        %lld\n",
665 		   percpu_counter_sum_positive(&nn->counter[NFSD_STATS_PAYLOAD_MISSES]));
666 	seq_printf(m, "longest chain len:     %u\n", nn->longest_chain);
667 	seq_printf(m, "cachesize at longest:  %u\n", nn->longest_chain_cachesize);
668 	return 0;
669 }
670