blob: c0c58471eb45b07fedc7f35a52a29a4a78f195d4 [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;
Jeff Layton0ee0bf72013-02-04 08:18:01 -050030static unsigned int num_drc_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Greg Banksfca42172009-04-01 07:28:13 +110032/*
33 * Calculate the hash index from an XID.
34 */
35static inline u32 request_hash(u32 xid)
36{
37 u32 h = xid;
38 h ^= (xid >> 24);
39 return h & (HASHSIZE-1);
40}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
43
Greg Banksfca42172009-04-01 07:28:13 +110044/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * locking for the reply cache:
46 * A cache entry is "single use" if c_state == RC_INPROG
47 * Otherwise, it when accessing _prev or _next, the lock must be held.
48 */
49static DEFINE_SPINLOCK(cache_lock);
50
Jeff Laytonf09841f2013-01-28 14:41:11 -050051static struct svc_cacherep *
52nfsd_reply_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 struct svc_cacherep *rp;
Jeff Laytonf09841f2013-01-28 14:41:11 -050055
56 rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
57 if (rp) {
58 rp->c_state = RC_UNUSED;
59 rp->c_type = RC_NOCACHE;
60 INIT_LIST_HEAD(&rp->c_lru);
61 INIT_HLIST_NODE(&rp->c_hash);
62 }
63 return rp;
64}
65
66static void
67nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
68{
Jeff Layton25e6b8b2013-01-28 14:41:12 -050069 if (rp->c_type == RC_REPLBUFF)
Jeff Laytonf09841f2013-01-28 14:41:11 -050070 kfree(rp->c_replvec.iov_base);
71 list_del(&rp->c_lru);
Jeff Layton0ee0bf72013-02-04 08:18:01 -050072 --num_drc_entries;
Jeff Laytonf09841f2013-01-28 14:41:11 -050073 kmem_cache_free(drc_slab, rp);
74}
75
76int nfsd_reply_cache_init(void)
77{
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int i;
Jeff Laytonf09841f2013-01-28 14:41:11 -050079 struct svc_cacherep *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Jeff Layton8a8bc402013-01-28 14:41:10 -050081 drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
82 0, 0, NULL);
83 if (!drc_slab)
84 goto out_nomem;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 INIT_LIST_HEAD(&lru_head);
87 i = CACHESIZE;
Jeff Layton0ee0bf72013-02-04 08:18:01 -050088 num_drc_entries = 0;
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050089 while (i) {
Jeff Laytonf09841f2013-01-28 14:41:11 -050090 rp = nfsd_reply_cache_alloc();
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050091 if (!rp)
92 goto out_nomem;
Jeff Layton0ee0bf72013-02-04 08:18:01 -050093 ++num_drc_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 list_add(&rp->c_lru, &lru_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 i--;
96 }
97
Greg Banksfca42172009-04-01 07:28:13 +110098 cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
99 if (!cache_hash)
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500100 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 cache_disabled = 0;
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500103 return 0;
104out_nomem:
105 printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
106 nfsd_reply_cache_shutdown();
107 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -0500110void nfsd_reply_cache_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct svc_cacherep *rp;
113
114 while (!list_empty(&lru_head)) {
115 rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
Jeff Laytonf09841f2013-01-28 14:41:11 -0500116 nfsd_reply_cache_free_locked(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118
119 cache_disabled = 1;
120
Greg Banksfca42172009-04-01 07:28:13 +1100121 kfree (cache_hash);
122 cache_hash = NULL;
Jeff Layton8a8bc402013-01-28 14:41:10 -0500123
124 if (drc_slab) {
125 kmem_cache_destroy(drc_slab);
126 drc_slab = NULL;
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/*
131 * Move cache entry to end of LRU list
132 */
133static void
134lru_put_end(struct svc_cacherep *rp)
135{
Jeff Layton56c25482013-02-04 08:18:00 -0500136 rp->c_timestamp = jiffies;
Akinobu Mitaf1166292006-06-26 00:24:46 -0700137 list_move_tail(&rp->c_lru, &lru_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140/*
141 * Move a cache entry from one hash list to another
142 */
143static void
144hash_refile(struct svc_cacherep *rp)
145{
146 hlist_del_init(&rp->c_hash);
Greg Banksfca42172009-04-01 07:28:13 +1100147 hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Jeff Laytond1a07742013-01-28 14:41:13 -0500150static inline bool
151nfsd_cache_entry_expired(struct svc_cacherep *rp)
152{
153 return rp->c_state != RC_INPROG &&
154 time_after(jiffies, rp->c_timestamp + RC_EXPIRE);
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500158 * Search the request hash for an entry that matches the given rqstp.
159 * Must be called with cache_lock held. Returns the found entry or
160 * NULL on failure.
161 */
162static struct svc_cacherep *
163nfsd_cache_search(struct svc_rqst *rqstp)
164{
165 struct svc_cacherep *rp;
166 struct hlist_node *hn;
167 struct hlist_head *rh;
168 __be32 xid = rqstp->rq_xid;
169 u32 proto = rqstp->rq_prot,
170 vers = rqstp->rq_vers,
171 proc = rqstp->rq_proc;
172
173 rh = &cache_hash[request_hash(xid)];
174 hlist_for_each_entry(rp, hn, rh, c_hash) {
175 if (rp->c_state != RC_UNUSED &&
176 xid == rp->c_xid && proc == rp->c_proc &&
177 proto == rp->c_prot && vers == rp->c_vers &&
178 !nfsd_cache_entry_expired(rp) &&
179 rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) &&
180 rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr))
181 return rp;
182 }
183 return NULL;
184}
185
186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * Try to find an entry matching the current call in the cache. When none
188 * is found, we grab the oldest unlocked entry off the LRU list.
189 * Note that no operation within the loop may sleep.
190 */
191int
J. Bruce Fields10910062011-01-24 12:11:02 -0500192nfsd_cache_lookup(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct svc_cacherep *rp;
Al Viroc7afef12006-10-19 23:29:02 -0700195 __be32 xid = rqstp->rq_xid;
196 u32 proto = rqstp->rq_prot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 vers = rqstp->rq_vers,
198 proc = rqstp->rq_proc;
199 unsigned long age;
J. Bruce Fields10910062011-01-24 12:11:02 -0500200 int type = rqstp->rq_cachetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int rtn;
202
203 rqstp->rq_cacherep = NULL;
204 if (cache_disabled || type == RC_NOCACHE) {
205 nfsdstats.rcnocache++;
206 return RC_DOIT;
207 }
208
209 spin_lock(&cache_lock);
210 rtn = RC_DOIT;
211
Jeff Laytona4a3ec32013-01-28 14:41:14 -0500212 rp = nfsd_cache_search(rqstp);
213 if (rp) {
214 nfsdstats.rchits++;
215 goto found_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217 nfsdstats.rcmisses++;
218
219 /* This loop shouldn't take more than a few iterations normally */
220 {
221 int safe = 0;
222 list_for_each_entry(rp, &lru_head, c_lru) {
223 if (rp->c_state != RC_INPROG)
224 break;
225 if (safe++ > CACHESIZE) {
226 printk("nfsd: loop in repcache LRU list\n");
227 cache_disabled = 1;
228 goto out;
229 }
230 }
231 }
232
Greg Bankscf0a5862009-04-01 07:28:15 +1100233 /* All entries on the LRU are in-progress. This should not happen */
234 if (&rp->c_lru == &lru_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 static int complaints;
236
237 printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
238 if (++complaints > 5) {
239 printk(KERN_WARNING "nfsd: disabling repcache.\n");
240 cache_disabled = 1;
241 }
242 goto out;
243 }
244
245 rqstp->rq_cacherep = rp;
246 rp->c_state = RC_INPROG;
247 rp->c_xid = xid;
248 rp->c_proc = proc;
Jeff Layton7b9e8522013-01-28 14:41:07 -0500249 rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
250 rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 rp->c_prot = proto;
252 rp->c_vers = vers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 hash_refile(rp);
Jeff Layton56c25482013-02-04 08:18:00 -0500255 lru_put_end(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* release any buffer */
258 if (rp->c_type == RC_REPLBUFF) {
259 kfree(rp->c_replvec.iov_base);
260 rp->c_replvec.iov_base = NULL;
261 }
262 rp->c_type = RC_NOCACHE;
263 out:
264 spin_unlock(&cache_lock);
265 return rtn;
266
267found_entry:
268 /* We found a matching entry which is either in progress or done. */
269 age = jiffies - rp->c_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 lru_put_end(rp);
271
272 rtn = RC_DROPIT;
273 /* Request being processed or excessive rexmits */
274 if (rp->c_state == RC_INPROG || age < RC_DELAY)
275 goto out;
276
277 /* From the hall of fame of impractical attacks:
278 * Is this a user who tries to snoop on the cache? */
279 rtn = RC_DOIT;
280 if (!rqstp->rq_secure && rp->c_secure)
281 goto out;
282
283 /* Compose RPC reply header */
284 switch (rp->c_type) {
285 case RC_NOCACHE:
286 break;
287 case RC_REPLSTAT:
288 svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
289 rtn = RC_REPLY;
290 break;
291 case RC_REPLBUFF:
292 if (!nfsd_cache_append(rqstp, &rp->c_replvec))
293 goto out; /* should not happen */
294 rtn = RC_REPLY;
295 break;
296 default:
297 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
298 rp->c_state = RC_UNUSED;
299 }
300
301 goto out;
302}
303
304/*
305 * Update a cache entry. This is called from nfsd_dispatch when
306 * the procedure has been executed and the complete reply is in
307 * rqstp->rq_res.
308 *
309 * We're copying around data here rather than swapping buffers because
310 * the toplevel loop requires max-sized buffers, which would be a waste
311 * of memory for a cache with a max reply size of 100 bytes (diropokres).
312 *
313 * If we should start to use different types of cache entries tailored
314 * specifically for attrstat and fh's, we may save even more space.
315 *
316 * Also note that a cachetype of RC_NOCACHE can legally be passed when
317 * nfsd failed to encode a reply that otherwise would have been cached.
318 * In this case, nfsd_cache_update is called with statp == NULL.
319 */
320void
Al Viroc7afef12006-10-19 23:29:02 -0700321nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct svc_cacherep *rp;
324 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
325 int len;
326
327 if (!(rp = rqstp->rq_cacherep) || cache_disabled)
328 return;
329
330 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
331 len >>= 2;
Greg Banksfca42172009-04-01 07:28:13 +1100332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Don't cache excessive amounts of data and XDR failures */
334 if (!statp || len > (256 >> 2)) {
335 rp->c_state = RC_UNUSED;
336 return;
337 }
338
339 switch (cachetype) {
340 case RC_REPLSTAT:
341 if (len != 1)
342 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
343 rp->c_replstat = *statp;
344 break;
345 case RC_REPLBUFF:
346 cachv = &rp->c_replvec;
347 cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
348 if (!cachv->iov_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 rp->c_state = RC_UNUSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return;
351 }
352 cachv->iov_len = len << 2;
353 memcpy(cachv->iov_base, statp, len << 2);
354 break;
355 }
356 spin_lock(&cache_lock);
357 lru_put_end(rp);
358 rp->c_secure = rqstp->rq_secure;
359 rp->c_type = cachetype;
360 rp->c_state = RC_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 spin_unlock(&cache_lock);
362 return;
363}
364
365/*
366 * Copy cached reply to current reply buffer. Should always fit.
367 * FIXME as reply is in a page, we should just attach the page, and
368 * keep a refcount....
369 */
370static int
371nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
372{
373 struct kvec *vec = &rqstp->rq_res.head[0];
374
375 if (vec->iov_len + data->iov_len > PAGE_SIZE) {
376 printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
377 data->iov_len);
378 return 0;
379 }
380 memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
381 vec->iov_len += data->iov_len;
382 return 1;
383}