blob: 48f5ef94423471b0053f32f15733cdf03a39d16f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Jeff Layton59766872013-02-04 12:50:00 -050012#include <linux/sunrpc/addr.h>
Jeff Layton0338dd12013-02-04 08:18:02 -050013#include <linux/highmem.h>
Jeff Layton01a7dec2013-02-04 11:57:27 -050014#include <net/checksum.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015
Boaz Harrosh9a74af22009-12-03 20:30:56 +020016#include "nfsd.h"
17#include "cache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Jeff Layton0338dd12013-02-04 08:18:02 -050019#define NFSDDBG_FACILITY NFSDDBG_REPCACHE
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define HASHSIZE 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Greg Banksfca42172009-04-01 07:28:13 +110023static struct hlist_head * cache_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static struct list_head lru_head;
Jeff Layton8a8bc402013-01-28 14:41:10 -050025static struct kmem_cache *drc_slab;
Jeff Layton9dc56142013-03-27 10:15:37 -040026
27/* max number of entries allowed in the cache */
Jeff Layton0338dd12013-02-04 08:18:02 -050028static unsigned int max_drc_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Greg Banksfca42172009-04-01 07:28:13 +110030/*
Jeff Layton9dc56142013-03-27 10:15:37 -040031 * 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 */
36static unsigned int num_drc_entries;
37
38/* cache misses due only to checksum comparison failures */
39static unsigned int payload_misses;
40
41/*
Greg Banksfca42172009-04-01 07:28:13 +110042 * Calculate the hash index from an XID.
43 */
44static inline u32 request_hash(u32 xid)
45{
46 u32 h = xid;
47 h ^= (xid >> 24);
48 return h & (HASHSIZE-1);
49}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
Jeff Laytonaca8a232013-02-04 08:18:05 -050052static void cache_cleaner_func(struct work_struct *unused);
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -050053static int nfsd_reply_cache_shrink(struct shrinker *shrink,
54 struct shrink_control *sc);
55
56struct shrinker nfsd_reply_cache_shrinker = {
57 .shrink = nfsd_reply_cache_shrink,
58 .seeks = 1,
59};
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Greg Banksfca42172009-04-01 07:28:13 +110061/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * locking for the reply cache:
63 * A cache entry is "single use" if c_state == RC_INPROG
64 * Otherwise, it when accessing _prev or _next, the lock must be held.
65 */
66static DEFINE_SPINLOCK(cache_lock);
Jeff Laytonaca8a232013-02-04 08:18:05 -050067static DECLARE_DELAYED_WORK(cache_cleaner, cache_cleaner_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jeff Layton0338dd12013-02-04 08:18:02 -050069/*
70 * Put a cap on the size of the DRC based on the amount of available
71 * low memory in the machine.
72 *
73 * 64MB: 8192
74 * 128MB: 11585
75 * 256MB: 16384
76 * 512MB: 23170
77 * 1GB: 32768
78 * 2GB: 46340
79 * 4GB: 65536
80 * 8GB: 92681
81 * 16GB: 131072
82 *
83 * ...with a hard cap of 256k entries. In the worst case, each entry will be
84 * ~1k, so the above numbers should give a rough max of the amount of memory
85 * used in k.
86 */
87static unsigned int
88nfsd_cache_size_limit(void)
89{
90 unsigned int limit;
91 unsigned long low_pages = totalram_pages - totalhigh_pages;
92
93 limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
94 return min_t(unsigned int, limit, 256*1024);
95}
96
Jeff Laytonf09841f2013-01-28 14:41:11 -050097static struct svc_cacherep *
98nfsd_reply_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct svc_cacherep *rp;
Jeff Laytonf09841f2013-01-28 14:41:11 -0500101
102 rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
103 if (rp) {
104 rp->c_state = RC_UNUSED;
105 rp->c_type = RC_NOCACHE;
106 INIT_LIST_HEAD(&rp->c_lru);
107 INIT_HLIST_NODE(&rp->c_hash);
108 }
109 return rp;
110}
111
112static void
113nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
114{
Jeff Layton25e6b8b2013-01-28 14:41:12 -0500115 if (rp->c_type == RC_REPLBUFF)
Jeff Laytonf09841f2013-01-28 14:41:11 -0500116 kfree(rp->c_replvec.iov_base);
Jeff Laytona517b602013-03-18 10:49:07 -0400117 if (!hlist_unhashed(&rp->c_hash))
118 hlist_del(&rp->c_hash);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500119 list_del(&rp->c_lru);
Jeff Layton0ee0bf72013-02-04 08:18:01 -0500120 --num_drc_entries;
Jeff Laytonf09841f2013-01-28 14:41:11 -0500121 kmem_cache_free(drc_slab, rp);
122}
123
Jeff Layton2c6b6912013-02-04 08:18:04 -0500124static void
125nfsd_reply_cache_free(struct svc_cacherep *rp)
126{
127 spin_lock(&cache_lock);
128 nfsd_reply_cache_free_locked(rp);
129 spin_unlock(&cache_lock);
130}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132int nfsd_reply_cache_init(void)
133{
Jeff Laytonac534ff2013-03-15 09:16:29 -0400134 INIT_LIST_HEAD(&lru_head);
135 max_drc_entries = nfsd_cache_size_limit();
136 num_drc_entries = 0;
137
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500138 register_shrinker(&nfsd_reply_cache_shrinker);
Jeff Layton8a8bc402013-01-28 14:41:10 -0500139 drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
140 0, 0, NULL);
141 if (!drc_slab)
142 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jeff Layton0338dd12013-02-04 08:18:02 -0500144 cache_hash = kcalloc(HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
Greg Banksfca42172009-04-01 07:28:13 +1100145 if (!cache_hash)
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500146 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500148 return 0;
149out_nomem:
150 printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
151 nfsd_reply_cache_shutdown();
152 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500155void nfsd_reply_cache_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct svc_cacherep *rp;
158
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500159 unregister_shrinker(&nfsd_reply_cache_shrinker);
Jeff Laytonaca8a232013-02-04 08:18:05 -0500160 cancel_delayed_work_sync(&cache_cleaner);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 while (!list_empty(&lru_head)) {
163 rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500164 nfsd_reply_cache_free_locked(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
Greg Banksfca42172009-04-01 07:28:13 +1100167 kfree (cache_hash);
168 cache_hash = NULL;
Jeff Layton8a8bc402013-01-28 14:41:10 -0500169
170 if (drc_slab) {
171 kmem_cache_destroy(drc_slab);
172 drc_slab = NULL;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/*
Jeff Laytonaca8a232013-02-04 08:18:05 -0500177 * Move cache entry to end of LRU list, and queue the cleaner to run if it's
178 * not already scheduled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
180static void
181lru_put_end(struct svc_cacherep *rp)
182{
Jeff Layton56c25482013-02-04 08:18:00 -0500183 rp->c_timestamp = jiffies;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700184 list_move_tail(&rp->c_lru, &lru_head);
Jeff Laytonaca8a232013-02-04 08:18:05 -0500185 schedule_delayed_work(&cache_cleaner, RC_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/*
189 * Move a cache entry from one hash list to another
190 */
191static void
192hash_refile(struct svc_cacherep *rp)
193{
194 hlist_del_init(&rp->c_hash);
Greg Banksfca42172009-04-01 07:28:13 +1100195 hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Jeff Laytond1a07742013-01-28 14:41:13 -0500198static inline bool
199nfsd_cache_entry_expired(struct svc_cacherep *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Jeff Laytond1a07742013-01-28 14:41:13 -0500201 return rp->c_state != RC_INPROG &&
202 time_after(jiffies, rp->c_timestamp + RC_EXPIRE);
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
Jeff Laytonaca8a232013-02-04 08:18:05 -0500206 * Walk the LRU list and prune off entries that are older than RC_EXPIRE.
207 * Also prune the oldest ones when the total exceeds the max number of entries.
208 */
209static void
210prune_cache_entries(void)
211{
212 struct svc_cacherep *rp, *tmp;
213
214 list_for_each_entry_safe(rp, tmp, &lru_head, c_lru) {
215 if (!nfsd_cache_entry_expired(rp) &&
216 num_drc_entries <= max_drc_entries)
217 break;
218 nfsd_reply_cache_free_locked(rp);
219 }
220
221 /*
222 * Conditionally rearm the job. If we cleaned out the list, then
223 * cancel any pending run (since there won't be any work to do).
224 * Otherwise, we rearm the job or modify the existing one to run in
225 * RC_EXPIRE since we just ran the pruner.
226 */
227 if (list_empty(&lru_head))
228 cancel_delayed_work(&cache_cleaner);
229 else
230 mod_delayed_work(system_wq, &cache_cleaner, RC_EXPIRE);
231}
232
233static void
234cache_cleaner_func(struct work_struct *unused)
235{
236 spin_lock(&cache_lock);
237 prune_cache_entries();
238 spin_unlock(&cache_lock);
239}
240
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500241static int
242nfsd_reply_cache_shrink(struct shrinker *shrink, struct shrink_control *sc)
243{
244 unsigned int num;
245
246 spin_lock(&cache_lock);
247 if (sc->nr_to_scan)
248 prune_cache_entries();
249 num = num_drc_entries;
250 spin_unlock(&cache_lock);
251
252 return num;
253}
254
Jeff Laytonaca8a232013-02-04 08:18:05 -0500255/*
Jeff Layton01a7dec2013-02-04 11:57:27 -0500256 * Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes
257 */
258static __wsum
259nfsd_cache_csum(struct svc_rqst *rqstp)
260{
261 int idx;
262 unsigned int base;
263 __wsum csum;
264 struct xdr_buf *buf = &rqstp->rq_arg;
265 const unsigned char *p = buf->head[0].iov_base;
266 size_t csum_len = min_t(size_t, buf->head[0].iov_len + buf->page_len,
267 RC_CSUMLEN);
268 size_t len = min(buf->head[0].iov_len, csum_len);
269
270 /* rq_arg.head first */
271 csum = csum_partial(p, len, 0);
272 csum_len -= len;
273
274 /* Continue into page array */
275 idx = buf->page_base / PAGE_SIZE;
276 base = buf->page_base & ~PAGE_MASK;
277 while (csum_len) {
278 p = page_address(buf->pages[idx]) + base;
Jeff Layton56edc862013-02-15 13:36:34 -0500279 len = min_t(size_t, PAGE_SIZE - base, csum_len);
Jeff Layton01a7dec2013-02-04 11:57:27 -0500280 csum = csum_partial(p, len, csum);
281 csum_len -= len;
282 base = 0;
283 ++idx;
284 }
285 return csum;
286}
287
Jeff Layton9dc56142013-03-27 10:15:37 -0400288static bool
289nfsd_cache_match(struct svc_rqst *rqstp, __wsum csum, struct svc_cacherep *rp)
290{
291 /* Check RPC header info first */
292 if (rqstp->rq_xid != rp->c_xid || rqstp->rq_proc != rp->c_proc ||
293 rqstp->rq_prot != rp->c_prot || rqstp->rq_vers != rp->c_vers ||
294 rqstp->rq_arg.len != rp->c_len ||
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 false;
298
299 /* compare checksum of NFS data */
300 if (csum != rp->c_csum) {
301 ++payload_misses;
302 return false;
303 }
304
305 return true;
306}
307
Jeff Layton01a7dec2013-02-04 11:57:27 -0500308/*
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500309 * Search the request hash for an entry that matches the given rqstp.
310 * Must be called with cache_lock held. Returns the found entry or
311 * NULL on failure.
312 */
313static struct svc_cacherep *
Jeff Layton01a7dec2013-02-04 11:57:27 -0500314nfsd_cache_search(struct svc_rqst *rqstp, __wsum csum)
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct svc_cacherep *rp;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500317 struct hlist_head *rh;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500318
Jeff Layton9dc56142013-03-27 10:15:37 -0400319 rh = &cache_hash[request_hash(rqstp->rq_xid)];
Linus Torvaldsb6669732013-02-28 18:02:55 -0800320 hlist_for_each_entry(rp, rh, c_hash) {
Jeff Layton9dc56142013-03-27 10:15:37 -0400321 if (nfsd_cache_match(rqstp, csum, rp))
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500322 return rp;
323 }
324 return NULL;
325}
326
327/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * Try to find an entry matching the current call in the cache. When none
Jeff Layton1ac83622013-02-14 16:45:13 -0500329 * is found, we try to grab the oldest expired entry off the LRU list. If
330 * a suitable one isn't there, then drop the cache_lock and allocate a
331 * new one, then search again in case one got inserted while this thread
332 * didn't hold the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
334int
335nfsd_cache_lookup(struct svc_rqst *rqstp)
336{
Jeff Layton0338dd12013-02-04 08:18:02 -0500337 struct svc_cacherep *rp, *found;
Al Viroc7afef12006-10-19 23:29:02 -0700338 __be32 xid = rqstp->rq_xid;
339 u32 proto = rqstp->rq_prot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 vers = rqstp->rq_vers,
341 proc = rqstp->rq_proc;
Jeff Layton01a7dec2013-02-04 11:57:27 -0500342 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 unsigned long age;
J. Bruce Fields10910062011-01-24 12:11:02 -0500344 int type = rqstp->rq_cachetype;
Jeff Layton0b9ea372013-03-27 10:15:37 -0400345 int rtn = RC_DOIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 rqstp->rq_cacherep = NULL;
Jeff Layton13cc8a72013-02-04 08:18:03 -0500348 if (type == RC_NOCACHE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 nfsdstats.rcnocache++;
Jeff Layton0b9ea372013-03-27 10:15:37 -0400350 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
Jeff Layton01a7dec2013-02-04 11:57:27 -0500353 csum = nfsd_cache_csum(rqstp);
354
Jeff Layton0b9ea372013-03-27 10:15:37 -0400355 /*
356 * Since the common case is a cache miss followed by an insert,
357 * preallocate an entry. First, try to reuse the first entry on the LRU
358 * if it works, then go ahead and prune the LRU list.
359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 spin_lock(&cache_lock);
Jeff Layton0338dd12013-02-04 08:18:02 -0500361 if (!list_empty(&lru_head)) {
362 rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
363 if (nfsd_cache_entry_expired(rp) ||
Jeff Laytonaca8a232013-02-04 08:18:05 -0500364 num_drc_entries >= max_drc_entries) {
365 lru_put_end(rp);
366 prune_cache_entries();
Jeff Layton0b9ea372013-03-27 10:15:37 -0400367 goto search_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 }
Jeff Layton0338dd12013-02-04 08:18:02 -0500370
Jeff Layton0b9ea372013-03-27 10:15:37 -0400371 /* No expired ones available, allocate a new one. */
Jeff Layton0338dd12013-02-04 08:18:02 -0500372 spin_unlock(&cache_lock);
373 rp = nfsd_reply_cache_alloc();
Jeff Layton0338dd12013-02-04 08:18:02 -0500374 spin_lock(&cache_lock);
Jeff Layton0b9ea372013-03-27 10:15:37 -0400375 if (likely(rp))
376 ++num_drc_entries;
Jeff Layton0338dd12013-02-04 08:18:02 -0500377
Jeff Layton0b9ea372013-03-27 10:15:37 -0400378search_cache:
Jeff Layton01a7dec2013-02-04 11:57:27 -0500379 found = nfsd_cache_search(rqstp, csum);
Jeff Layton0338dd12013-02-04 08:18:02 -0500380 if (found) {
Jeff Layton0b9ea372013-03-27 10:15:37 -0400381 if (likely(rp))
382 nfsd_reply_cache_free_locked(rp);
Jeff Layton0338dd12013-02-04 08:18:02 -0500383 rp = found;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500384 goto found_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Jeff Layton0338dd12013-02-04 08:18:02 -0500386
Jeff Layton0b9ea372013-03-27 10:15:37 -0400387 if (!rp) {
388 dprintk("nfsd: unable to allocate DRC entry!\n");
389 goto out;
390 }
391
Jeff Layton0338dd12013-02-04 08:18:02 -0500392 /*
393 * We're keeping the one we just allocated. Are we now over the
394 * limit? Prune one off the tip of the LRU in trade for the one we
395 * just allocated if so.
396 */
397 if (num_drc_entries >= max_drc_entries)
398 nfsd_reply_cache_free_locked(list_first_entry(&lru_head,
399 struct svc_cacherep, c_lru));
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 nfsdstats.rcmisses++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 rqstp->rq_cacherep = rp;
403 rp->c_state = RC_INPROG;
404 rp->c_xid = xid;
405 rp->c_proc = proc;
Jeff Layton7b9e8522013-01-28 14:41:07 -0500406 rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
407 rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 rp->c_prot = proto;
409 rp->c_vers = vers;
Jeff Layton01a7dec2013-02-04 11:57:27 -0500410 rp->c_len = rqstp->rq_arg.len;
411 rp->c_csum = csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 hash_refile(rp);
Jeff Layton56c25482013-02-04 08:18:00 -0500414 lru_put_end(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* release any buffer */
417 if (rp->c_type == RC_REPLBUFF) {
418 kfree(rp->c_replvec.iov_base);
419 rp->c_replvec.iov_base = NULL;
420 }
421 rp->c_type = RC_NOCACHE;
422 out:
423 spin_unlock(&cache_lock);
424 return rtn;
425
426found_entry:
Jeff Layton0338dd12013-02-04 08:18:02 -0500427 nfsdstats.rchits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* We found a matching entry which is either in progress or done. */
429 age = jiffies - rp->c_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 lru_put_end(rp);
431
432 rtn = RC_DROPIT;
433 /* Request being processed or excessive rexmits */
434 if (rp->c_state == RC_INPROG || age < RC_DELAY)
435 goto out;
436
437 /* From the hall of fame of impractical attacks:
438 * Is this a user who tries to snoop on the cache? */
439 rtn = RC_DOIT;
440 if (!rqstp->rq_secure && rp->c_secure)
441 goto out;
442
443 /* Compose RPC reply header */
444 switch (rp->c_type) {
445 case RC_NOCACHE:
446 break;
447 case RC_REPLSTAT:
448 svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
449 rtn = RC_REPLY;
450 break;
451 case RC_REPLBUFF:
452 if (!nfsd_cache_append(rqstp, &rp->c_replvec))
453 goto out; /* should not happen */
454 rtn = RC_REPLY;
455 break;
456 default:
457 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
Jeff Layton0338dd12013-02-04 08:18:02 -0500458 nfsd_reply_cache_free_locked(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 goto out;
462}
463
464/*
465 * Update a cache entry. This is called from nfsd_dispatch when
466 * the procedure has been executed and the complete reply is in
467 * rqstp->rq_res.
468 *
469 * We're copying around data here rather than swapping buffers because
470 * the toplevel loop requires max-sized buffers, which would be a waste
471 * of memory for a cache with a max reply size of 100 bytes (diropokres).
472 *
473 * If we should start to use different types of cache entries tailored
474 * specifically for attrstat and fh's, we may save even more space.
475 *
476 * Also note that a cachetype of RC_NOCACHE can legally be passed when
477 * nfsd failed to encode a reply that otherwise would have been cached.
478 * In this case, nfsd_cache_update is called with statp == NULL.
479 */
480void
Al Viroc7afef12006-10-19 23:29:02 -0700481nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Jeff Layton13cc8a72013-02-04 08:18:03 -0500483 struct svc_cacherep *rp = rqstp->rq_cacherep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
485 int len;
486
Jeff Layton13cc8a72013-02-04 08:18:03 -0500487 if (!rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return;
489
490 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
491 len >>= 2;
Greg Banksfca42172009-04-01 07:28:13 +1100492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* Don't cache excessive amounts of data and XDR failures */
494 if (!statp || len > (256 >> 2)) {
Jeff Layton2c6b6912013-02-04 08:18:04 -0500495 nfsd_reply_cache_free(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return;
497 }
498
499 switch (cachetype) {
500 case RC_REPLSTAT:
501 if (len != 1)
502 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
503 rp->c_replstat = *statp;
504 break;
505 case RC_REPLBUFF:
506 cachv = &rp->c_replvec;
507 cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
508 if (!cachv->iov_base) {
Jeff Layton2c6b6912013-02-04 08:18:04 -0500509 nfsd_reply_cache_free(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return;
511 }
512 cachv->iov_len = len << 2;
513 memcpy(cachv->iov_base, statp, len << 2);
514 break;
Jeff Layton2c6b6912013-02-04 08:18:04 -0500515 case RC_NOCACHE:
516 nfsd_reply_cache_free(rp);
517 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 spin_lock(&cache_lock);
520 lru_put_end(rp);
521 rp->c_secure = rqstp->rq_secure;
522 rp->c_type = cachetype;
523 rp->c_state = RC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 spin_unlock(&cache_lock);
525 return;
526}
527
528/*
529 * Copy cached reply to current reply buffer. Should always fit.
530 * FIXME as reply is in a page, we should just attach the page, and
531 * keep a refcount....
532 */
533static int
534nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
535{
536 struct kvec *vec = &rqstp->rq_res.head[0];
537
538 if (vec->iov_len + data->iov_len > PAGE_SIZE) {
539 printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
540 data->iov_len);
541 return 0;
542 }
543 memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
544 vec->iov_len += data->iov_len;
545 return 1;
546}