blob: 5dd9ec2a177f97fdb4a0949613d30433030b18b7 [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;
29
Greg Banksfca42172009-04-01 07:28:13 +110030/*
31 * Calculate the hash index from an XID.
32 */
33static inline u32 request_hash(u32 xid)
34{
35 u32 h = xid;
36 h ^= (xid >> 24);
37 return h & (HASHSIZE-1);
38}
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
41
Greg Banksfca42172009-04-01 07:28:13 +110042/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * locking for the reply cache:
44 * A cache entry is "single use" if c_state == RC_INPROG
45 * Otherwise, it when accessing _prev or _next, the lock must be held.
46 */
47static DEFINE_SPINLOCK(cache_lock);
48
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050049int nfsd_reply_cache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 struct svc_cacherep *rp;
52 int i;
53
54 INIT_LIST_HEAD(&lru_head);
55 i = CACHESIZE;
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050056 while (i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050058 if (!rp)
59 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 list_add(&rp->c_lru, &lru_head);
61 rp->c_state = RC_UNUSED;
62 rp->c_type = RC_NOCACHE;
63 INIT_HLIST_NODE(&rp->c_hash);
64 i--;
65 }
66
Greg Banksfca42172009-04-01 07:28:13 +110067 cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
68 if (!cache_hash)
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050069 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 cache_disabled = 0;
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050072 return 0;
73out_nomem:
74 printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
75 nfsd_reply_cache_shutdown();
76 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
J. Bruce Fieldsd5c34282007-11-09 14:10:56 -050079void nfsd_reply_cache_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 struct svc_cacherep *rp;
82
83 while (!list_empty(&lru_head)) {
84 rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
85 if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF)
86 kfree(rp->c_replvec.iov_base);
87 list_del(&rp->c_lru);
88 kfree(rp);
89 }
90
91 cache_disabled = 1;
92
Greg Banksfca42172009-04-01 07:28:13 +110093 kfree (cache_hash);
94 cache_hash = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97/*
98 * Move cache entry to end of LRU list
99 */
100static void
101lru_put_end(struct svc_cacherep *rp)
102{
Akinobu Mitaf1166292006-06-26 00:24:46 -0700103 list_move_tail(&rp->c_lru, &lru_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106/*
107 * Move a cache entry from one hash list to another
108 */
109static void
110hash_refile(struct svc_cacherep *rp)
111{
112 hlist_del_init(&rp->c_hash);
Greg Banksfca42172009-04-01 07:28:13 +1100113 hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * Try to find an entry matching the current call in the cache. When none
118 * is found, we grab the oldest unlocked entry off the LRU list.
119 * Note that no operation within the loop may sleep.
120 */
121int
J. Bruce Fields10910062011-01-24 12:11:02 -0500122nfsd_cache_lookup(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct hlist_node *hn;
125 struct hlist_head *rh;
126 struct svc_cacherep *rp;
Al Viroc7afef12006-10-19 23:29:02 -0700127 __be32 xid = rqstp->rq_xid;
128 u32 proto = rqstp->rq_prot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 vers = rqstp->rq_vers,
130 proc = rqstp->rq_proc;
131 unsigned long age;
J. Bruce Fields10910062011-01-24 12:11:02 -0500132 int type = rqstp->rq_cachetype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int rtn;
134
135 rqstp->rq_cacherep = NULL;
136 if (cache_disabled || type == RC_NOCACHE) {
137 nfsdstats.rcnocache++;
138 return RC_DOIT;
139 }
140
141 spin_lock(&cache_lock);
142 rtn = RC_DOIT;
143
Greg Banksfca42172009-04-01 07:28:13 +1100144 rh = &cache_hash[request_hash(xid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 hlist_for_each_entry(rp, hn, rh, c_hash) {
146 if (rp->c_state != RC_UNUSED &&
147 xid == rp->c_xid && proc == rp->c_proc &&
148 proto == rp->c_prot && vers == rp->c_vers &&
149 time_before(jiffies, rp->c_timestamp + 120*HZ) &&
Jeff Layton7b9e8522013-01-28 14:41:07 -0500150 rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) &&
151 rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 nfsdstats.rchits++;
153 goto found_entry;
154 }
155 }
156 nfsdstats.rcmisses++;
157
158 /* This loop shouldn't take more than a few iterations normally */
159 {
160 int safe = 0;
161 list_for_each_entry(rp, &lru_head, c_lru) {
162 if (rp->c_state != RC_INPROG)
163 break;
164 if (safe++ > CACHESIZE) {
165 printk("nfsd: loop in repcache LRU list\n");
166 cache_disabled = 1;
167 goto out;
168 }
169 }
170 }
171
Greg Bankscf0a5862009-04-01 07:28:15 +1100172 /* All entries on the LRU are in-progress. This should not happen */
173 if (&rp->c_lru == &lru_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 static int complaints;
175
176 printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
177 if (++complaints > 5) {
178 printk(KERN_WARNING "nfsd: disabling repcache.\n");
179 cache_disabled = 1;
180 }
181 goto out;
182 }
183
184 rqstp->rq_cacherep = rp;
185 rp->c_state = RC_INPROG;
186 rp->c_xid = xid;
187 rp->c_proc = proc;
Jeff Layton7b9e8522013-01-28 14:41:07 -0500188 rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
189 rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 rp->c_prot = proto;
191 rp->c_vers = vers;
192 rp->c_timestamp = jiffies;
193
194 hash_refile(rp);
195
196 /* release any buffer */
197 if (rp->c_type == RC_REPLBUFF) {
198 kfree(rp->c_replvec.iov_base);
199 rp->c_replvec.iov_base = NULL;
200 }
201 rp->c_type = RC_NOCACHE;
202 out:
203 spin_unlock(&cache_lock);
204 return rtn;
205
206found_entry:
207 /* We found a matching entry which is either in progress or done. */
208 age = jiffies - rp->c_timestamp;
209 rp->c_timestamp = jiffies;
210 lru_put_end(rp);
211
212 rtn = RC_DROPIT;
213 /* Request being processed or excessive rexmits */
214 if (rp->c_state == RC_INPROG || age < RC_DELAY)
215 goto out;
216
217 /* From the hall of fame of impractical attacks:
218 * Is this a user who tries to snoop on the cache? */
219 rtn = RC_DOIT;
220 if (!rqstp->rq_secure && rp->c_secure)
221 goto out;
222
223 /* Compose RPC reply header */
224 switch (rp->c_type) {
225 case RC_NOCACHE:
226 break;
227 case RC_REPLSTAT:
228 svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
229 rtn = RC_REPLY;
230 break;
231 case RC_REPLBUFF:
232 if (!nfsd_cache_append(rqstp, &rp->c_replvec))
233 goto out; /* should not happen */
234 rtn = RC_REPLY;
235 break;
236 default:
237 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
238 rp->c_state = RC_UNUSED;
239 }
240
241 goto out;
242}
243
244/*
245 * Update a cache entry. This is called from nfsd_dispatch when
246 * the procedure has been executed and the complete reply is in
247 * rqstp->rq_res.
248 *
249 * We're copying around data here rather than swapping buffers because
250 * the toplevel loop requires max-sized buffers, which would be a waste
251 * of memory for a cache with a max reply size of 100 bytes (diropokres).
252 *
253 * If we should start to use different types of cache entries tailored
254 * specifically for attrstat and fh's, we may save even more space.
255 *
256 * Also note that a cachetype of RC_NOCACHE can legally be passed when
257 * nfsd failed to encode a reply that otherwise would have been cached.
258 * In this case, nfsd_cache_update is called with statp == NULL.
259 */
260void
Al Viroc7afef12006-10-19 23:29:02 -0700261nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct svc_cacherep *rp;
264 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
265 int len;
266
267 if (!(rp = rqstp->rq_cacherep) || cache_disabled)
268 return;
269
270 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
271 len >>= 2;
Greg Banksfca42172009-04-01 07:28:13 +1100272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Don't cache excessive amounts of data and XDR failures */
274 if (!statp || len > (256 >> 2)) {
275 rp->c_state = RC_UNUSED;
276 return;
277 }
278
279 switch (cachetype) {
280 case RC_REPLSTAT:
281 if (len != 1)
282 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
283 rp->c_replstat = *statp;
284 break;
285 case RC_REPLBUFF:
286 cachv = &rp->c_replvec;
287 cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
288 if (!cachv->iov_base) {
289 spin_lock(&cache_lock);
290 rp->c_state = RC_UNUSED;
291 spin_unlock(&cache_lock);
292 return;
293 }
294 cachv->iov_len = len << 2;
295 memcpy(cachv->iov_base, statp, len << 2);
296 break;
297 }
298 spin_lock(&cache_lock);
299 lru_put_end(rp);
300 rp->c_secure = rqstp->rq_secure;
301 rp->c_type = cachetype;
302 rp->c_state = RC_DONE;
303 rp->c_timestamp = jiffies;
304 spin_unlock(&cache_lock);
305 return;
306}
307
308/*
309 * Copy cached reply to current reply buffer. Should always fit.
310 * FIXME as reply is in a page, we should just attach the page, and
311 * keep a refcount....
312 */
313static int
314nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
315{
316 struct kvec *vec = &rqstp->rq_res.head[0];
317
318 if (vec->iov_len + data->iov_len > PAGE_SIZE) {
319 printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
320 data->iov_len);
321 return 0;
322 }
323 memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
324 vec->iov_len += data->iov_len;
325 return 1;
326}