[PATCH] uml: Remove unused variable
[linux-2.6/mini2440.git] / fs / nfsd / nfscache.c
blobfdf7cf3dfadcf0f7775f4aa54cd4acf43e7ffed1
1 /*
2 * linux/fs/nfsd/nfscache.c
4 * Request reply cache. This is currently a global cache, but this may
5 * change in the future and be a per-client cache.
7 * This code is heavily inspired by the 44BSD implementation, although
8 * it does things a bit differently.
10 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
13 #include <linux/kernel.h>
14 #include <linux/time.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/spinlock.h>
18 #include <linux/list.h>
20 #include <linux/sunrpc/svc.h>
21 #include <linux/nfsd/nfsd.h>
22 #include <linux/nfsd/cache.h>
24 /* Size of reply cache. Common values are:
25 * 4.3BSD: 128
26 * 4.4BSD: 256
27 * Solaris2: 1024
28 * DEC Unix: 512-4096
30 #define CACHESIZE 1024
31 #define HASHSIZE 64
32 #define REQHASH(xid) ((((xid) >> 24) ^ (xid)) & (HASHSIZE-1))
34 static struct hlist_head * hash_list;
35 static struct list_head lru_head;
36 static int cache_disabled = 1;
38 static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
40 /*
41 * locking for the reply cache:
42 * A cache entry is "single use" if c_state == RC_INPROG
43 * Otherwise, it when accessing _prev or _next, the lock must be held.
45 static DEFINE_SPINLOCK(cache_lock);
47 void
48 nfsd_cache_init(void)
50 struct svc_cacherep *rp;
51 int i;
53 INIT_LIST_HEAD(&lru_head);
54 i = CACHESIZE;
55 while(i) {
56 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
57 if (!rp) break;
58 list_add(&rp->c_lru, &lru_head);
59 rp->c_state = RC_UNUSED;
60 rp->c_type = RC_NOCACHE;
61 INIT_HLIST_NODE(&rp->c_hash);
62 i--;
65 if (i)
66 printk (KERN_ERR "nfsd: cannot allocate all %d cache entries, only got %d\n",
67 CACHESIZE, CACHESIZE-i);
69 hash_list = kmalloc (HASHSIZE * sizeof(struct hlist_head), GFP_KERNEL);
70 if (!hash_list) {
71 nfsd_cache_shutdown();
72 printk (KERN_ERR "nfsd: cannot allocate %Zd bytes for hash list\n",
73 HASHSIZE * sizeof(struct hlist_head));
74 return;
76 memset(hash_list, 0, HASHSIZE * sizeof(struct hlist_head));
78 cache_disabled = 0;
81 void
82 nfsd_cache_shutdown(void)
84 struct svc_cacherep *rp;
86 while (!list_empty(&lru_head)) {
87 rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
88 if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF)
89 kfree(rp->c_replvec.iov_base);
90 list_del(&rp->c_lru);
91 kfree(rp);
94 cache_disabled = 1;
96 kfree (hash_list);
97 hash_list = NULL;
101 * Move cache entry to end of LRU list
103 static void
104 lru_put_end(struct svc_cacherep *rp)
106 list_move_tail(&rp->c_lru, &lru_head);
110 * Move a cache entry from one hash list to another
112 static void
113 hash_refile(struct svc_cacherep *rp)
115 hlist_del_init(&rp->c_hash);
116 hlist_add_head(&rp->c_hash, hash_list + REQHASH(rp->c_xid));
120 * Try to find an entry matching the current call in the cache. When none
121 * is found, we grab the oldest unlocked entry off the LRU list.
122 * Note that no operation within the loop may sleep.
125 nfsd_cache_lookup(struct svc_rqst *rqstp, int type)
127 struct hlist_node *hn;
128 struct hlist_head *rh;
129 struct svc_cacherep *rp;
130 u32 xid = rqstp->rq_xid,
131 proto = rqstp->rq_prot,
132 vers = rqstp->rq_vers,
133 proc = rqstp->rq_proc;
134 unsigned long age;
135 int rtn;
137 rqstp->rq_cacherep = NULL;
138 if (cache_disabled || type == RC_NOCACHE) {
139 nfsdstats.rcnocache++;
140 return RC_DOIT;
143 spin_lock(&cache_lock);
144 rtn = RC_DOIT;
146 rh = &hash_list[REQHASH(xid)];
147 hlist_for_each_entry(rp, hn, rh, c_hash) {
148 if (rp->c_state != RC_UNUSED &&
149 xid == rp->c_xid && proc == rp->c_proc &&
150 proto == rp->c_prot && vers == rp->c_vers &&
151 time_before(jiffies, rp->c_timestamp + 120*HZ) &&
152 memcmp((char*)&rqstp->rq_addr, (char*)&rp->c_addr, sizeof(rp->c_addr))==0) {
153 nfsdstats.rchits++;
154 goto found_entry;
157 nfsdstats.rcmisses++;
159 /* This loop shouldn't take more than a few iterations normally */
161 int safe = 0;
162 list_for_each_entry(rp, &lru_head, c_lru) {
163 if (rp->c_state != RC_INPROG)
164 break;
165 if (safe++ > CACHESIZE) {
166 printk("nfsd: loop in repcache LRU list\n");
167 cache_disabled = 1;
168 goto out;
173 /* This should not happen */
174 if (rp == NULL) {
175 static int complaints;
177 printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
178 if (++complaints > 5) {
179 printk(KERN_WARNING "nfsd: disabling repcache.\n");
180 cache_disabled = 1;
182 goto out;
185 rqstp->rq_cacherep = rp;
186 rp->c_state = RC_INPROG;
187 rp->c_xid = xid;
188 rp->c_proc = proc;
189 rp->c_addr = rqstp->rq_addr;
190 rp->c_prot = proto;
191 rp->c_vers = vers;
192 rp->c_timestamp = jiffies;
194 hash_refile(rp);
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;
201 rp->c_type = RC_NOCACHE;
202 out:
203 spin_unlock(&cache_lock);
204 return rtn;
206 found_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);
212 rtn = RC_DROPIT;
213 /* Request being processed or excessive rexmits */
214 if (rp->c_state == RC_INPROG || age < RC_DELAY)
215 goto out;
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;
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;
241 goto out;
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.
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).
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.
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.
260 void
261 nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, u32 *statp)
263 struct svc_cacherep *rp;
264 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
265 int len;
267 if (!(rp = rqstp->rq_cacherep) || cache_disabled)
268 return;
270 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
271 len >>= 2;
273 /* Don't cache excessive amounts of data and XDR failures */
274 if (!statp || len > (256 >> 2)) {
275 rp->c_state = RC_UNUSED;
276 return;
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;
294 cachv->iov_len = len << 2;
295 memcpy(cachv->iov_base, statp, len << 2);
296 break;
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;
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....
313 static int
314 nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
316 struct kvec *vec = &rqstp->rq_res.head[0];
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;
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;