blob: c61391e8e09dd2d5e7108c9e3dab134c779ffa83 [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 Layton0ee0bf72013-02-04 08:18:01 -050026static unsigned int num_drc_entries;
Jeff Layton0338dd12013-02-04 08:18:02 -050027static unsigned int max_drc_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Greg Banksfca42172009-04-01 07:28:13 +110029/*
30 * Calculate the hash index from an XID.
31 */
32static inline u32 request_hash(u32 xid)
33{
34 u32 h = xid;
35 h ^= (xid >> 24);
36 return h & (HASHSIZE-1);
37}
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
Jeff Laytonaca8a232013-02-04 08:18:05 -050040static void cache_cleaner_func(struct work_struct *unused);
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -050041static int nfsd_reply_cache_shrink(struct shrinker *shrink,
42 struct shrink_control *sc);
43
44struct shrinker nfsd_reply_cache_shrinker = {
45 .shrink = nfsd_reply_cache_shrink,
46 .seeks = 1,
47};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Greg Banksfca42172009-04-01 07:28:13 +110049/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * locking for the reply cache:
51 * A cache entry is "single use" if c_state == RC_INPROG
52 * Otherwise, it when accessing _prev or _next, the lock must be held.
53 */
54static DEFINE_SPINLOCK(cache_lock);
Jeff Laytonaca8a232013-02-04 08:18:05 -050055static DECLARE_DELAYED_WORK(cache_cleaner, cache_cleaner_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jeff Layton0338dd12013-02-04 08:18:02 -050057/*
58 * Put a cap on the size of the DRC based on the amount of available
59 * low memory in the machine.
60 *
61 * 64MB: 8192
62 * 128MB: 11585
63 * 256MB: 16384
64 * 512MB: 23170
65 * 1GB: 32768
66 * 2GB: 46340
67 * 4GB: 65536
68 * 8GB: 92681
69 * 16GB: 131072
70 *
71 * ...with a hard cap of 256k entries. In the worst case, each entry will be
72 * ~1k, so the above numbers should give a rough max of the amount of memory
73 * used in k.
74 */
75static unsigned int
76nfsd_cache_size_limit(void)
77{
78 unsigned int limit;
79 unsigned long low_pages = totalram_pages - totalhigh_pages;
80
81 limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
82 return min_t(unsigned int, limit, 256*1024);
83}
84
Jeff Laytonf09841f2013-01-28 14:41:11 -050085static struct svc_cacherep *
86nfsd_reply_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 struct svc_cacherep *rp;
Jeff Laytonf09841f2013-01-28 14:41:11 -050089
90 rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
91 if (rp) {
92 rp->c_state = RC_UNUSED;
93 rp->c_type = RC_NOCACHE;
94 INIT_LIST_HEAD(&rp->c_lru);
95 INIT_HLIST_NODE(&rp->c_hash);
96 }
97 return rp;
98}
99
100static void
101nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
102{
Jeff Layton25e6b8b2013-01-28 14:41:12 -0500103 if (rp->c_type == RC_REPLBUFF)
Jeff Laytonf09841f2013-01-28 14:41:11 -0500104 kfree(rp->c_replvec.iov_base);
Jeff Laytona517b602013-03-18 10:49:07 -0400105 if (!hlist_unhashed(&rp->c_hash))
106 hlist_del(&rp->c_hash);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500107 list_del(&rp->c_lru);
Jeff Layton0ee0bf72013-02-04 08:18:01 -0500108 --num_drc_entries;
Jeff Laytonf09841f2013-01-28 14:41:11 -0500109 kmem_cache_free(drc_slab, rp);
110}
111
Jeff Layton2c6b6912013-02-04 08:18:04 -0500112static void
113nfsd_reply_cache_free(struct svc_cacherep *rp)
114{
115 spin_lock(&cache_lock);
116 nfsd_reply_cache_free_locked(rp);
117 spin_unlock(&cache_lock);
118}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120int nfsd_reply_cache_init(void)
121{
Jeff Laytonac534ff2013-03-15 09:16:29 -0400122 INIT_LIST_HEAD(&lru_head);
123 max_drc_entries = nfsd_cache_size_limit();
124 num_drc_entries = 0;
125
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500126 register_shrinker(&nfsd_reply_cache_shrinker);
Jeff Layton8a8bc402013-01-28 14:41:10 -0500127 drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
128 0, 0, NULL);
129 if (!drc_slab)
130 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jeff Layton0338dd12013-02-04 08:18:02 -0500132 cache_hash = kcalloc(HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
Greg Banksfca42172009-04-01 07:28:13 +1100133 if (!cache_hash)
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500134 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500136 return 0;
137out_nomem:
138 printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
139 nfsd_reply_cache_shutdown();
140 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500143void nfsd_reply_cache_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct svc_cacherep *rp;
146
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500147 unregister_shrinker(&nfsd_reply_cache_shrinker);
Jeff Laytonaca8a232013-02-04 08:18:05 -0500148 cancel_delayed_work_sync(&cache_cleaner);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 while (!list_empty(&lru_head)) {
151 rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500152 nfsd_reply_cache_free_locked(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
Greg Banksfca42172009-04-01 07:28:13 +1100155 kfree (cache_hash);
156 cache_hash = NULL;
Jeff Layton8a8bc402013-01-28 14:41:10 -0500157
158 if (drc_slab) {
159 kmem_cache_destroy(drc_slab);
160 drc_slab = NULL;
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164/*
Jeff Laytonaca8a232013-02-04 08:18:05 -0500165 * Move cache entry to end of LRU list, and queue the cleaner to run if it's
166 * not already scheduled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
168static void
169lru_put_end(struct svc_cacherep *rp)
170{
Jeff Layton56c25482013-02-04 08:18:00 -0500171 rp->c_timestamp = jiffies;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700172 list_move_tail(&rp->c_lru, &lru_head);
Jeff Laytonaca8a232013-02-04 08:18:05 -0500173 schedule_delayed_work(&cache_cleaner, RC_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/*
177 * Move a cache entry from one hash list to another
178 */
179static void
180hash_refile(struct svc_cacherep *rp)
181{
182 hlist_del_init(&rp->c_hash);
Greg Banksfca42172009-04-01 07:28:13 +1100183 hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Jeff Laytond1a07742013-01-28 14:41:13 -0500186static inline bool
187nfsd_cache_entry_expired(struct svc_cacherep *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Jeff Laytond1a07742013-01-28 14:41:13 -0500189 return rp->c_state != RC_INPROG &&
190 time_after(jiffies, rp->c_timestamp + RC_EXPIRE);
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*
Jeff Laytonaca8a232013-02-04 08:18:05 -0500194 * Walk the LRU list and prune off entries that are older than RC_EXPIRE.
195 * Also prune the oldest ones when the total exceeds the max number of entries.
196 */
197static void
198prune_cache_entries(void)
199{
200 struct svc_cacherep *rp, *tmp;
201
202 list_for_each_entry_safe(rp, tmp, &lru_head, c_lru) {
203 if (!nfsd_cache_entry_expired(rp) &&
204 num_drc_entries <= max_drc_entries)
205 break;
206 nfsd_reply_cache_free_locked(rp);
207 }
208
209 /*
210 * Conditionally rearm the job. If we cleaned out the list, then
211 * cancel any pending run (since there won't be any work to do).
212 * Otherwise, we rearm the job or modify the existing one to run in
213 * RC_EXPIRE since we just ran the pruner.
214 */
215 if (list_empty(&lru_head))
216 cancel_delayed_work(&cache_cleaner);
217 else
218 mod_delayed_work(system_wq, &cache_cleaner, RC_EXPIRE);
219}
220
221static void
222cache_cleaner_func(struct work_struct *unused)
223{
224 spin_lock(&cache_lock);
225 prune_cache_entries();
226 spin_unlock(&cache_lock);
227}
228
Jeff Laytonb4e7f2c2013-02-04 08:18:06 -0500229static int
230nfsd_reply_cache_shrink(struct shrinker *shrink, struct shrink_control *sc)
231{
232 unsigned int num;
233
234 spin_lock(&cache_lock);
235 if (sc->nr_to_scan)
236 prune_cache_entries();
237 num = num_drc_entries;
238 spin_unlock(&cache_lock);
239
240 return num;
241}
242
Jeff Laytonaca8a232013-02-04 08:18:05 -0500243/*
Jeff Layton01a7dec2013-02-04 11:57:27 -0500244 * Walk an xdr_buf and get a CRC for at most the first RC_CSUMLEN bytes
245 */
246static __wsum
247nfsd_cache_csum(struct svc_rqst *rqstp)
248{
249 int idx;
250 unsigned int base;
251 __wsum csum;
252 struct xdr_buf *buf = &rqstp->rq_arg;
253 const unsigned char *p = buf->head[0].iov_base;
254 size_t csum_len = min_t(size_t, buf->head[0].iov_len + buf->page_len,
255 RC_CSUMLEN);
256 size_t len = min(buf->head[0].iov_len, csum_len);
257
258 /* rq_arg.head first */
259 csum = csum_partial(p, len, 0);
260 csum_len -= len;
261
262 /* Continue into page array */
263 idx = buf->page_base / PAGE_SIZE;
264 base = buf->page_base & ~PAGE_MASK;
265 while (csum_len) {
266 p = page_address(buf->pages[idx]) + base;
Jeff Layton56edc862013-02-15 13:36:34 -0500267 len = min_t(size_t, PAGE_SIZE - base, csum_len);
Jeff Layton01a7dec2013-02-04 11:57:27 -0500268 csum = csum_partial(p, len, csum);
269 csum_len -= len;
270 base = 0;
271 ++idx;
272 }
273 return csum;
274}
275
276/*
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500277 * Search the request hash for an entry that matches the given rqstp.
278 * Must be called with cache_lock held. Returns the found entry or
279 * NULL on failure.
280 */
281static struct svc_cacherep *
Jeff Layton01a7dec2013-02-04 11:57:27 -0500282nfsd_cache_search(struct svc_rqst *rqstp, __wsum csum)
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500283{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct svc_cacherep *rp;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500285 struct hlist_head *rh;
Al Viroc7afef12006-10-19 23:29:02 -0700286 __be32 xid = rqstp->rq_xid;
287 u32 proto = rqstp->rq_prot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 vers = rqstp->rq_vers,
289 proc = rqstp->rq_proc;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500290
291 rh = &cache_hash[request_hash(xid)];
Linus Torvaldsb6669732013-02-28 18:02:55 -0800292 hlist_for_each_entry(rp, rh, c_hash) {
Jeff Layton2c6b6912013-02-04 08:18:04 -0500293 if (xid == rp->c_xid && proc == rp->c_proc &&
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500294 proto == rp->c_prot && vers == rp->c_vers &&
Jeff Layton01a7dec2013-02-04 11:57:27 -0500295 rqstp->rq_arg.len == rp->c_len && csum == rp->c_csum &&
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500296 rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) &&
297 rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr))
298 return rp;
299 }
300 return NULL;
301}
302
303/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * Try to find an entry matching the current call in the cache. When none
Jeff Layton1ac83622013-02-14 16:45:13 -0500305 * is found, we try to grab the oldest expired entry off the LRU list. If
306 * a suitable one isn't there, then drop the cache_lock and allocate a
307 * new one, then search again in case one got inserted while this thread
308 * didn't hold the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
310int
311nfsd_cache_lookup(struct svc_rqst *rqstp)
312{
Jeff Layton0338dd12013-02-04 08:18:02 -0500313 struct svc_cacherep *rp, *found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 __be32 xid = rqstp->rq_xid;
315 u32 proto = rqstp->rq_prot,
316 vers = rqstp->rq_vers,
317 proc = rqstp->rq_proc;
Jeff Layton01a7dec2013-02-04 11:57:27 -0500318 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 unsigned long age;
J. Bruce Fields10910062011-01-24 12:11:02 -0500320 int type = rqstp->rq_cachetype;
Jeff Layton0b9ea372013-03-27 10:15:37 -0400321 int rtn = RC_DOIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 rqstp->rq_cacherep = NULL;
Jeff Layton13cc8a72013-02-04 08:18:03 -0500324 if (type == RC_NOCACHE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 nfsdstats.rcnocache++;
Jeff Layton0b9ea372013-03-27 10:15:37 -0400326 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
Jeff Layton01a7dec2013-02-04 11:57:27 -0500329 csum = nfsd_cache_csum(rqstp);
330
Jeff Layton0b9ea372013-03-27 10:15:37 -0400331 /*
332 * Since the common case is a cache miss followed by an insert,
333 * preallocate an entry. First, try to reuse the first entry on the LRU
334 * if it works, then go ahead and prune the LRU list.
335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 spin_lock(&cache_lock);
Jeff Layton0338dd12013-02-04 08:18:02 -0500337 if (!list_empty(&lru_head)) {
338 rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
339 if (nfsd_cache_entry_expired(rp) ||
Jeff Laytonaca8a232013-02-04 08:18:05 -0500340 num_drc_entries >= max_drc_entries) {
341 lru_put_end(rp);
342 prune_cache_entries();
Jeff Layton0b9ea372013-03-27 10:15:37 -0400343 goto search_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 }
Jeff Layton0338dd12013-02-04 08:18:02 -0500346
Jeff Layton0b9ea372013-03-27 10:15:37 -0400347 /* No expired ones available, allocate a new one. */
Jeff Layton0338dd12013-02-04 08:18:02 -0500348 spin_unlock(&cache_lock);
349 rp = nfsd_reply_cache_alloc();
Jeff Layton0338dd12013-02-04 08:18:02 -0500350 spin_lock(&cache_lock);
Jeff Layton0b9ea372013-03-27 10:15:37 -0400351 if (likely(rp))
352 ++num_drc_entries;
Jeff Layton0338dd12013-02-04 08:18:02 -0500353
Jeff Layton0b9ea372013-03-27 10:15:37 -0400354search_cache:
Jeff Layton01a7dec2013-02-04 11:57:27 -0500355 found = nfsd_cache_search(rqstp, csum);
Jeff Layton0338dd12013-02-04 08:18:02 -0500356 if (found) {
Jeff Layton0b9ea372013-03-27 10:15:37 -0400357 if (likely(rp))
358 nfsd_reply_cache_free_locked(rp);
Jeff Layton0338dd12013-02-04 08:18:02 -0500359 rp = found;
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500360 goto found_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Jeff Layton0338dd12013-02-04 08:18:02 -0500362
Jeff Layton0b9ea372013-03-27 10:15:37 -0400363 if (!rp) {
364 dprintk("nfsd: unable to allocate DRC entry!\n");
365 goto out;
366 }
367
Jeff Layton0338dd12013-02-04 08:18:02 -0500368 /*
369 * We're keeping the one we just allocated. Are we now over the
370 * limit? Prune one off the tip of the LRU in trade for the one we
371 * just allocated if so.
372 */
373 if (num_drc_entries >= max_drc_entries)
374 nfsd_reply_cache_free_locked(list_first_entry(&lru_head,
375 struct svc_cacherep, c_lru));
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 nfsdstats.rcmisses++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 rqstp->rq_cacherep = rp;
379 rp->c_state = RC_INPROG;
380 rp->c_xid = xid;
381 rp->c_proc = proc;
Jeff Layton7b9e8522013-01-28 14:41:07 -0500382 rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
383 rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 rp->c_prot = proto;
385 rp->c_vers = vers;
Jeff Layton01a7dec2013-02-04 11:57:27 -0500386 rp->c_len = rqstp->rq_arg.len;
387 rp->c_csum = csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 hash_refile(rp);
Jeff Layton56c25482013-02-04 08:18:00 -0500390 lru_put_end(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /* release any buffer */
393 if (rp->c_type == RC_REPLBUFF) {
394 kfree(rp->c_replvec.iov_base);
395 rp->c_replvec.iov_base = NULL;
396 }
397 rp->c_type = RC_NOCACHE;
398 out:
399 spin_unlock(&cache_lock);
400 return rtn;
401
402found_entry:
Jeff Layton0338dd12013-02-04 08:18:02 -0500403 nfsdstats.rchits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* We found a matching entry which is either in progress or done. */
405 age = jiffies - rp->c_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 lru_put_end(rp);
407
408 rtn = RC_DROPIT;
409 /* Request being processed or excessive rexmits */
410 if (rp->c_state == RC_INPROG || age < RC_DELAY)
411 goto out;
412
413 /* From the hall of fame of impractical attacks:
414 * Is this a user who tries to snoop on the cache? */
415 rtn = RC_DOIT;
416 if (!rqstp->rq_secure && rp->c_secure)
417 goto out;
418
419 /* Compose RPC reply header */
420 switch (rp->c_type) {
421 case RC_NOCACHE:
422 break;
423 case RC_REPLSTAT:
424 svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
425 rtn = RC_REPLY;
426 break;
427 case RC_REPLBUFF:
428 if (!nfsd_cache_append(rqstp, &rp->c_replvec))
429 goto out; /* should not happen */
430 rtn = RC_REPLY;
431 break;
432 default:
433 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
Jeff Layton0338dd12013-02-04 08:18:02 -0500434 nfsd_reply_cache_free_locked(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
437 goto out;
438}
439
440/*
441 * Update a cache entry. This is called from nfsd_dispatch when
442 * the procedure has been executed and the complete reply is in
443 * rqstp->rq_res.
444 *
445 * We're copying around data here rather than swapping buffers because
446 * the toplevel loop requires max-sized buffers, which would be a waste
447 * of memory for a cache with a max reply size of 100 bytes (diropokres).
448 *
449 * If we should start to use different types of cache entries tailored
450 * specifically for attrstat and fh's, we may save even more space.
451 *
452 * Also note that a cachetype of RC_NOCACHE can legally be passed when
453 * nfsd failed to encode a reply that otherwise would have been cached.
454 * In this case, nfsd_cache_update is called with statp == NULL.
455 */
456void
Al Viroc7afef12006-10-19 23:29:02 -0700457nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Jeff Layton13cc8a72013-02-04 08:18:03 -0500459 struct svc_cacherep *rp = rqstp->rq_cacherep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
461 int len;
462
Jeff Layton13cc8a72013-02-04 08:18:03 -0500463 if (!rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return;
465
466 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
467 len >>= 2;
Greg Banksfca42172009-04-01 07:28:13 +1100468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Don't cache excessive amounts of data and XDR failures */
470 if (!statp || len > (256 >> 2)) {
Jeff Layton2c6b6912013-02-04 08:18:04 -0500471 nfsd_reply_cache_free(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return;
473 }
474
475 switch (cachetype) {
476 case RC_REPLSTAT:
477 if (len != 1)
478 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
479 rp->c_replstat = *statp;
480 break;
481 case RC_REPLBUFF:
482 cachv = &rp->c_replvec;
483 cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
484 if (!cachv->iov_base) {
Jeff Layton2c6b6912013-02-04 08:18:04 -0500485 nfsd_reply_cache_free(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return;
487 }
488 cachv->iov_len = len << 2;
489 memcpy(cachv->iov_base, statp, len << 2);
490 break;
Jeff Layton2c6b6912013-02-04 08:18:04 -0500491 case RC_NOCACHE:
492 nfsd_reply_cache_free(rp);
493 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 spin_lock(&cache_lock);
496 lru_put_end(rp);
497 rp->c_secure = rqstp->rq_secure;
498 rp->c_type = cachetype;
499 rp->c_state = RC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 spin_unlock(&cache_lock);
501 return;
502}
503
504/*
505 * Copy cached reply to current reply buffer. Should always fit.
506 * FIXME as reply is in a page, we should just attach the page, and
507 * keep a refcount....
508 */
509static int
510nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
511{
512 struct kvec *vec = &rqstp->rq_res.head[0];
513
514 if (vec->iov_len + data->iov_len > PAGE_SIZE) {
515 printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
516 data->iov_len);
517 return 0;
518 }
519 memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
520 vec->iov_len += data->iov_len;
521 return 1;
522}