nfsd: create a dedicated slabcache for DRC entries
[deliverable/linux.git] / fs / nfsd / nfscache.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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
5a0e3ad6 11#include <linux/slab.h>
7b9e8522 12#include <linux/sunrpc/clnt.h>
5a0e3ad6 13
9a74af21
BH
14#include "nfsd.h"
15#include "cache.h"
1da177e4
LT
16
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
1da177e4 25
fca4217c 26static struct hlist_head * cache_hash;
1da177e4
LT
27static struct list_head lru_head;
28static int cache_disabled = 1;
8a8bc40d 29static struct kmem_cache *drc_slab;
1da177e4 30
fca4217c
GB
31/*
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
1da177e4
LT
41static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
42
fca4217c 43/*
1da177e4
LT
44 * 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
d5c3428b 50int nfsd_reply_cache_init(void)
1da177e4
LT
51{
52 struct svc_cacherep *rp;
53 int i;
54
8a8bc40d
JL
55 drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep),
56 0, 0, NULL);
57 if (!drc_slab)
58 goto out_nomem;
59
1da177e4
LT
60 INIT_LIST_HEAD(&lru_head);
61 i = CACHESIZE;
d5c3428b 62 while (i) {
8a8bc40d 63 rp = kmem_cache_alloc(drc_slab, GFP_KERNEL);
d5c3428b
BF
64 if (!rp)
65 goto out_nomem;
1da177e4
LT
66 list_add(&rp->c_lru, &lru_head);
67 rp->c_state = RC_UNUSED;
68 rp->c_type = RC_NOCACHE;
69 INIT_HLIST_NODE(&rp->c_hash);
70 i--;
71 }
72
fca4217c
GB
73 cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
74 if (!cache_hash)
d5c3428b 75 goto out_nomem;
1da177e4
LT
76
77 cache_disabled = 0;
d5c3428b
BF
78 return 0;
79out_nomem:
80 printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
81 nfsd_reply_cache_shutdown();
82 return -ENOMEM;
1da177e4
LT
83}
84
d5c3428b 85void nfsd_reply_cache_shutdown(void)
1da177e4
LT
86{
87 struct svc_cacherep *rp;
88
89 while (!list_empty(&lru_head)) {
90 rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
91 if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF)
92 kfree(rp->c_replvec.iov_base);
93 list_del(&rp->c_lru);
8a8bc40d 94 kmem_cache_free(drc_slab, rp);
1da177e4
LT
95 }
96
97 cache_disabled = 1;
98
fca4217c
GB
99 kfree (cache_hash);
100 cache_hash = NULL;
8a8bc40d
JL
101
102 if (drc_slab) {
103 kmem_cache_destroy(drc_slab);
104 drc_slab = NULL;
105 }
1da177e4
LT
106}
107
108/*
109 * Move cache entry to end of LRU list
110 */
111static void
112lru_put_end(struct svc_cacherep *rp)
113{
f116629d 114 list_move_tail(&rp->c_lru, &lru_head);
1da177e4
LT
115}
116
117/*
118 * Move a cache entry from one hash list to another
119 */
120static void
121hash_refile(struct svc_cacherep *rp)
122{
123 hlist_del_init(&rp->c_hash);
fca4217c 124 hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
1da177e4
LT
125}
126
127/*
128 * Try to find an entry matching the current call in the cache. When none
129 * is found, we grab the oldest unlocked entry off the LRU list.
130 * Note that no operation within the loop may sleep.
131 */
132int
1091006c 133nfsd_cache_lookup(struct svc_rqst *rqstp)
1da177e4
LT
134{
135 struct hlist_node *hn;
136 struct hlist_head *rh;
137 struct svc_cacherep *rp;
c7afef1f
AV
138 __be32 xid = rqstp->rq_xid;
139 u32 proto = rqstp->rq_prot,
1da177e4
LT
140 vers = rqstp->rq_vers,
141 proc = rqstp->rq_proc;
142 unsigned long age;
1091006c 143 int type = rqstp->rq_cachetype;
1da177e4
LT
144 int rtn;
145
146 rqstp->rq_cacherep = NULL;
147 if (cache_disabled || type == RC_NOCACHE) {
148 nfsdstats.rcnocache++;
149 return RC_DOIT;
150 }
151
152 spin_lock(&cache_lock);
153 rtn = RC_DOIT;
154
fca4217c 155 rh = &cache_hash[request_hash(xid)];
1da177e4
LT
156 hlist_for_each_entry(rp, hn, rh, c_hash) {
157 if (rp->c_state != RC_UNUSED &&
158 xid == rp->c_xid && proc == rp->c_proc &&
159 proto == rp->c_prot && vers == rp->c_vers &&
160 time_before(jiffies, rp->c_timestamp + 120*HZ) &&
7b9e8522
JL
161 rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) &&
162 rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr)) {
1da177e4
LT
163 nfsdstats.rchits++;
164 goto found_entry;
165 }
166 }
167 nfsdstats.rcmisses++;
168
169 /* This loop shouldn't take more than a few iterations normally */
170 {
171 int safe = 0;
172 list_for_each_entry(rp, &lru_head, c_lru) {
173 if (rp->c_state != RC_INPROG)
174 break;
175 if (safe++ > CACHESIZE) {
176 printk("nfsd: loop in repcache LRU list\n");
177 cache_disabled = 1;
178 goto out;
179 }
180 }
181 }
182
cf0a586c
GB
183 /* All entries on the LRU are in-progress. This should not happen */
184 if (&rp->c_lru == &lru_head) {
1da177e4
LT
185 static int complaints;
186
187 printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
188 if (++complaints > 5) {
189 printk(KERN_WARNING "nfsd: disabling repcache.\n");
190 cache_disabled = 1;
191 }
192 goto out;
193 }
194
195 rqstp->rq_cacherep = rp;
196 rp->c_state = RC_INPROG;
197 rp->c_xid = xid;
198 rp->c_proc = proc;
7b9e8522
JL
199 rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp));
200 rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp)));
1da177e4
LT
201 rp->c_prot = proto;
202 rp->c_vers = vers;
203 rp->c_timestamp = jiffies;
204
205 hash_refile(rp);
206
207 /* release any buffer */
208 if (rp->c_type == RC_REPLBUFF) {
209 kfree(rp->c_replvec.iov_base);
210 rp->c_replvec.iov_base = NULL;
211 }
212 rp->c_type = RC_NOCACHE;
213 out:
214 spin_unlock(&cache_lock);
215 return rtn;
216
217found_entry:
218 /* We found a matching entry which is either in progress or done. */
219 age = jiffies - rp->c_timestamp;
220 rp->c_timestamp = jiffies;
221 lru_put_end(rp);
222
223 rtn = RC_DROPIT;
224 /* Request being processed or excessive rexmits */
225 if (rp->c_state == RC_INPROG || age < RC_DELAY)
226 goto out;
227
228 /* From the hall of fame of impractical attacks:
229 * Is this a user who tries to snoop on the cache? */
230 rtn = RC_DOIT;
231 if (!rqstp->rq_secure && rp->c_secure)
232 goto out;
233
234 /* Compose RPC reply header */
235 switch (rp->c_type) {
236 case RC_NOCACHE:
237 break;
238 case RC_REPLSTAT:
239 svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
240 rtn = RC_REPLY;
241 break;
242 case RC_REPLBUFF:
243 if (!nfsd_cache_append(rqstp, &rp->c_replvec))
244 goto out; /* should not happen */
245 rtn = RC_REPLY;
246 break;
247 default:
248 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
249 rp->c_state = RC_UNUSED;
250 }
251
252 goto out;
253}
254
255/*
256 * Update a cache entry. This is called from nfsd_dispatch when
257 * the procedure has been executed and the complete reply is in
258 * rqstp->rq_res.
259 *
260 * We're copying around data here rather than swapping buffers because
261 * the toplevel loop requires max-sized buffers, which would be a waste
262 * of memory for a cache with a max reply size of 100 bytes (diropokres).
263 *
264 * If we should start to use different types of cache entries tailored
265 * specifically for attrstat and fh's, we may save even more space.
266 *
267 * Also note that a cachetype of RC_NOCACHE can legally be passed when
268 * nfsd failed to encode a reply that otherwise would have been cached.
269 * In this case, nfsd_cache_update is called with statp == NULL.
270 */
271void
c7afef1f 272nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
1da177e4
LT
273{
274 struct svc_cacherep *rp;
275 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
276 int len;
277
278 if (!(rp = rqstp->rq_cacherep) || cache_disabled)
279 return;
280
281 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
282 len >>= 2;
fca4217c 283
1da177e4
LT
284 /* Don't cache excessive amounts of data and XDR failures */
285 if (!statp || len > (256 >> 2)) {
286 rp->c_state = RC_UNUSED;
287 return;
288 }
289
290 switch (cachetype) {
291 case RC_REPLSTAT:
292 if (len != 1)
293 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
294 rp->c_replstat = *statp;
295 break;
296 case RC_REPLBUFF:
297 cachv = &rp->c_replvec;
298 cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
299 if (!cachv->iov_base) {
1da177e4 300 rp->c_state = RC_UNUSED;
1da177e4
LT
301 return;
302 }
303 cachv->iov_len = len << 2;
304 memcpy(cachv->iov_base, statp, len << 2);
305 break;
306 }
307 spin_lock(&cache_lock);
308 lru_put_end(rp);
309 rp->c_secure = rqstp->rq_secure;
310 rp->c_type = cachetype;
311 rp->c_state = RC_DONE;
312 rp->c_timestamp = jiffies;
313 spin_unlock(&cache_lock);
314 return;
315}
316
317/*
318 * Copy cached reply to current reply buffer. Should always fit.
319 * FIXME as reply is in a page, we should just attach the page, and
320 * keep a refcount....
321 */
322static int
323nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
324{
325 struct kvec *vec = &rqstp->rq_res.head[0];
326
327 if (vec->iov_len + data->iov_len > PAGE_SIZE) {
328 printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
329 data->iov_len);
330 return 0;
331 }
332 memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
333 vec->iov_len += data->iov_len;
334 return 1;
335}
This page took 0.646772 seconds and 5 git commands to generate.