blob: 9d80dfa7778eb720f4e613aab48deb02d468305b [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 Layton7b9e8522013-01-28 14:41:07 -050012#include <linux/sunrpc/clnt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013
Boaz Harrosh9a74af22009-12-03 20:30:56 +020014#include "nfsd.h"
15#include "cache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/* Size of reply cache. Common values are:
18 * 4.3BSD: 128
19 * 4.4BSD: 256
20 * Solaris2: 1024
21 * DEC Unix: 512-4096
22 */
23#define CACHESIZE 1024
24#define HASHSIZE 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Greg Banksfca42172009-04-01 07:28:13 +110026static struct hlist_head * cache_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static struct list_head lru_head;
28static int cache_disabled = 1;
Jeff Layton8a8bc402013-01-28 14:41:10 -050029static struct kmem_cache *drc_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Greg Banksfca42172009-04-01 07:28:13 +110031/*
32 * Calculate the hash index from an XID.
33 */
34static inline u32 request_hash(u32 xid)
35{
36 u32 h = xid;
37 h ^= (xid >> 24);
38 return h & (HASHSIZE-1);
39}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
42
Greg Banksfca42172009-04-01 07:28:13 +110043/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * locking for the reply cache:
45 * A cache entry is "single use" if c_state == RC_INPROG
46 * Otherwise, it when accessing _prev or _next, the lock must be held.
47 */
48static DEFINE_SPINLOCK(cache_lock);
49
Jeff Laytonf09841f2013-01-28 14:41:11 -050050static struct svc_cacherep *
51nfsd_reply_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 struct svc_cacherep *rp;
Jeff Laytonf09841f2013-01-28 14:41:11 -050054
55 rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
56 if (rp) {
57 rp->c_state = RC_UNUSED;
58 rp->c_type = RC_NOCACHE;
59 INIT_LIST_HEAD(&rp->c_lru);
60 INIT_HLIST_NODE(&rp->c_hash);
61 }
62 return rp;
63}
64
65static void
66nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
67{
Jeff Layton25e6b8b2013-01-28 14:41:12 -050068 if (rp->c_type == RC_REPLBUFF)
Jeff Laytonf09841f2013-01-28 14:41:11 -050069 kfree(rp->c_replvec.iov_base);
70 list_del(&rp->c_lru);
71 kmem_cache_free(drc_slab, rp);
72}
73
74int nfsd_reply_cache_init(void)
75{
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int i;
Jeff Laytonf09841f2013-01-28 14:41:11 -050077 struct svc_cacherep *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Jeff Layton8a8bc402013-01-28 14:41:10 -050079 drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
80 0, 0, NULL);
81 if (!drc_slab)
82 goto out_nomem;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 INIT_LIST_HEAD(&lru_head);
85 i = CACHESIZE;
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050086 while (i) {
Jeff Laytonf09841f2013-01-28 14:41:11 -050087 rp = nfsd_reply_cache_alloc();
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050088 if (!rp)
89 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 list_add(&rp->c_lru, &lru_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 i--;
92 }
93
Greg Banksfca42172009-04-01 07:28:13 +110094 cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
95 if (!cache_hash)
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050096 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 cache_disabled = 0;
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050099 return 0;
100out_nomem:
101 printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
102 nfsd_reply_cache_shutdown();
103 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500106void nfsd_reply_cache_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct svc_cacherep *rp;
109
110 while (!list_empty(&lru_head)) {
111 rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500112 nfsd_reply_cache_free_locked(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114
115 cache_disabled = 1;
116
Greg Banksfca42172009-04-01 07:28:13 +1100117 kfree (cache_hash);
118 cache_hash = NULL;
Jeff Layton8a8bc402013-01-28 14:41:10 -0500119
120 if (drc_slab) {
121 kmem_cache_destroy(drc_slab);
122 drc_slab = NULL;
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126/*
127 * Move cache entry to end of LRU list
128 */
129static void
130lru_put_end(struct svc_cacherep *rp)
131{
Jeff Layton56c25482013-02-04 08:18:00 -0500132 rp->c_timestamp = jiffies;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700133 list_move_tail(&rp->c_lru, &lru_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136/*
137 * Move a cache entry from one hash list to another
138 */
139static void
140hash_refile(struct svc_cacherep *rp)
141{
142 hlist_del_init(&rp->c_hash);
Greg Banksfca42172009-04-01 07:28:13 +1100143 hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Jeff Laytond1a07742013-01-28 14:41:13 -0500146static inline bool
147nfsd_cache_entry_expired(struct svc_cacherep *rp)
148{
149 return rp->c_state != RC_INPROG &&
150 time_after(jiffies, rp->c_timestamp + RC_EXPIRE);
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500154 * Search the request hash for an entry that matches the given rqstp.
155 * Must be called with cache_lock held. Returns the found entry or
156 * NULL on failure.
157 */
158static struct svc_cacherep *
159nfsd_cache_search(struct svc_rqst *rqstp)
160{
161 struct svc_cacherep *rp;
162 struct hlist_node *hn;
163 struct hlist_head *rh;
164 __be32 xid = rqstp->rq_xid;
165 u32 proto = rqstp->rq_prot,
166 vers = rqstp->rq_vers,
167 proc = rqstp->rq_proc;
168
169 rh = &cache_hash[request_hash(xid)];
170 hlist_for_each_entry(rp, hn, rh, c_hash) {
171 if (rp->c_state != RC_UNUSED &&
172 xid == rp->c_xid && proc == rp->c_proc &&
173 proto == rp->c_prot && vers == rp->c_vers &&
174 !nfsd_cache_entry_expired(rp) &&
175 rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) &&
176 rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr))
177 return rp;
178 }
179 return NULL;
180}
181
182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * Try to find an entry matching the current call in the cache. When none
184 * is found, we grab the oldest unlocked entry off the LRU list.
185 * Note that no operation within the loop may sleep.
186 */
187int
J. Bruce Fields10910062011-01-24 12:11:02 -0500188nfsd_cache_lookup(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct svc_cacherep *rp;
Al Viroc7afef12006-10-19 23:29:02 -0700191 __be32 xid = rqstp->rq_xid;
192 u32 proto = rqstp->rq_prot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 vers = rqstp->rq_vers,
194 proc = rqstp->rq_proc;
195 unsigned long age;
J. Bruce Fields10910062011-01-24 12:11:02 -0500196 int type = rqstp->rq_cachetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int rtn;
198
199 rqstp->rq_cacherep = NULL;
200 if (cache_disabled || type == RC_NOCACHE) {
201 nfsdstats.rcnocache++;
202 return RC_DOIT;
203 }
204
205 spin_lock(&cache_lock);
206 rtn = RC_DOIT;
207
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500208 rp = nfsd_cache_search(rqstp);
209 if (rp) {
210 nfsdstats.rchits++;
211 goto found_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 nfsdstats.rcmisses++;
214
215 /* This loop shouldn't take more than a few iterations normally */
216 {
217 int safe = 0;
218 list_for_each_entry(rp, &lru_head, c_lru) {
219 if (rp->c_state != RC_INPROG)
220 break;
221 if (safe++ > CACHESIZE) {
222 printk("nfsd: loop in repcache LRU list\n");
223 cache_disabled = 1;
224 goto out;
225 }
226 }
227 }
228
Greg Bankscf0a5862009-04-01 07:28:15 +1100229 /* All entries on the LRU are in-progress. This should not happen */
230 if (&rp->c_lru == &lru_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 static int complaints;
232
233 printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
234 if (++complaints > 5) {
235 printk(KERN_WARNING "nfsd: disabling repcache.\n");
236 cache_disabled = 1;
237 }
238 goto out;
239 }
240
241 rqstp->rq_cacherep = rp;
242 rp->c_state = RC_INPROG;
243 rp->c_xid = xid;
244 rp->c_proc = proc;
Jeff Layton7b9e8522013-01-28 14:41:07 -0500245 rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
246 rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 rp->c_prot = proto;
248 rp->c_vers = vers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 hash_refile(rp);
Jeff Layton56c25482013-02-04 08:18:00 -0500251 lru_put_end(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* release any buffer */
254 if (rp->c_type == RC_REPLBUFF) {
255 kfree(rp->c_replvec.iov_base);
256 rp->c_replvec.iov_base = NULL;
257 }
258 rp->c_type = RC_NOCACHE;
259 out:
260 spin_unlock(&cache_lock);
261 return rtn;
262
263found_entry:
264 /* We found a matching entry which is either in progress or done. */
265 age = jiffies - rp->c_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 lru_put_end(rp);
267
268 rtn = RC_DROPIT;
269 /* Request being processed or excessive rexmits */
270 if (rp->c_state == RC_INPROG || age < RC_DELAY)
271 goto out;
272
273 /* From the hall of fame of impractical attacks:
274 * Is this a user who tries to snoop on the cache? */
275 rtn = RC_DOIT;
276 if (!rqstp->rq_secure && rp->c_secure)
277 goto out;
278
279 /* Compose RPC reply header */
280 switch (rp->c_type) {
281 case RC_NOCACHE:
282 break;
283 case RC_REPLSTAT:
284 svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
285 rtn = RC_REPLY;
286 break;
287 case RC_REPLBUFF:
288 if (!nfsd_cache_append(rqstp, &rp->c_replvec))
289 goto out; /* should not happen */
290 rtn = RC_REPLY;
291 break;
292 default:
293 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
294 rp->c_state = RC_UNUSED;
295 }
296
297 goto out;
298}
299
300/*
301 * Update a cache entry. This is called from nfsd_dispatch when
302 * the procedure has been executed and the complete reply is in
303 * rqstp->rq_res.
304 *
305 * We're copying around data here rather than swapping buffers because
306 * the toplevel loop requires max-sized buffers, which would be a waste
307 * of memory for a cache with a max reply size of 100 bytes (diropokres).
308 *
309 * If we should start to use different types of cache entries tailored
310 * specifically for attrstat and fh's, we may save even more space.
311 *
312 * Also note that a cachetype of RC_NOCACHE can legally be passed when
313 * nfsd failed to encode a reply that otherwise would have been cached.
314 * In this case, nfsd_cache_update is called with statp == NULL.
315 */
316void
Al Viroc7afef12006-10-19 23:29:02 -0700317nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct svc_cacherep *rp;
320 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
321 int len;
322
323 if (!(rp = rqstp->rq_cacherep) || cache_disabled)
324 return;
325
326 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
327 len >>= 2;
Greg Banksfca42172009-04-01 07:28:13 +1100328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /* Don't cache excessive amounts of data and XDR failures */
330 if (!statp || len > (256 >> 2)) {
331 rp->c_state = RC_UNUSED;
332 return;
333 }
334
335 switch (cachetype) {
336 case RC_REPLSTAT:
337 if (len != 1)
338 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
339 rp->c_replstat = *statp;
340 break;
341 case RC_REPLBUFF:
342 cachv = &rp->c_replvec;
343 cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
344 if (!cachv->iov_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 rp->c_state = RC_UNUSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return;
347 }
348 cachv->iov_len = len << 2;
349 memcpy(cachv->iov_base, statp, len << 2);
350 break;
351 }
352 spin_lock(&cache_lock);
353 lru_put_end(rp);
354 rp->c_secure = rqstp->rq_secure;
355 rp->c_type = cachetype;
356 rp->c_state = RC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 spin_unlock(&cache_lock);
358 return;
359}
360
361/*
362 * Copy cached reply to current reply buffer. Should always fit.
363 * FIXME as reply is in a page, we should just attach the page, and
364 * keep a refcount....
365 */
366static int
367nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
368{
369 struct kvec *vec = &rqstp->rq_res.head[0];
370
371 if (vec->iov_len + data->iov_len > PAGE_SIZE) {
372 printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
373 data->iov_len);
374 return 0;
375 }
376 memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
377 vec->iov_len += data->iov_len;
378 return 1;
379}