1
2
3
4
5
6
7
8
9
10
11
12
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>
19
20#include <linux/sunrpc/svc.h>
21#include <linux/nfsd/nfsd.h>
22#include <linux/nfsd/cache.h>
23
24
25
26
27
28
29
30#define CACHESIZE 1024
31#define HASHSIZE 64
32
33static struct hlist_head * cache_hash;
34static struct list_head lru_head;
35static int cache_disabled = 1;
36
37
38
39
40static inline u32 request_hash(u32 xid)
41{
42 u32 h = xid;
43 h ^= (xid >> 24);
44 return h & (HASHSIZE-1);
45}
46
47static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec);
48
49
50
51
52
53
54static DEFINE_SPINLOCK(cache_lock);
55
56int nfsd_reply_cache_init(void)
57{
58 struct svc_cacherep *rp;
59 int i;
60
61 INIT_LIST_HEAD(&lru_head);
62 i = CACHESIZE;
63 while (i) {
64 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
65 if (!rp)
66 goto out_nomem;
67 list_add(&rp->c_lru, &lru_head);
68 rp->c_state = RC_UNUSED;
69 rp->c_type = RC_NOCACHE;
70 INIT_HLIST_NODE(&rp->c_hash);
71 i--;
72 }
73
74 cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL);
75 if (!cache_hash)
76 goto out_nomem;
77
78 cache_disabled = 0;
79 return 0;
80out_nomem:
81 printk(KERN_ERR "nfsd: failed to allocate reply cache\n");
82 nfsd_reply_cache_shutdown();
83 return -ENOMEM;
84}
85
86void nfsd_reply_cache_shutdown(void)
87{
88 struct svc_cacherep *rp;
89
90 while (!list_empty(&lru_head)) {
91 rp = list_entry(lru_head.next, struct svc_cacherep, c_lru);
92 if (rp->c_state == RC_DONE && rp->c_type == RC_REPLBUFF)
93 kfree(rp->c_replvec.iov_base);
94 list_del(&rp->c_lru);
95 kfree(rp);
96 }
97
98 cache_disabled = 1;
99
100 kfree (cache_hash);
101 cache_hash = NULL;
102}
103
104
105
106
107static void
108lru_put_end(struct svc_cacherep *rp)
109{
110 list_move_tail(&rp->c_lru, &lru_head);
111}
112
113
114
115
116static void
117hash_refile(struct svc_cacherep *rp)
118{
119 hlist_del_init(&rp->c_hash);
120 hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid));
121}
122
123
124
125
126
127
128int
129nfsd_cache_lookup(struct svc_rqst *rqstp, int type)
130{
131 struct hlist_node *hn;
132 struct hlist_head *rh;
133 struct svc_cacherep *rp;
134 __be32 xid = rqstp->rq_xid;
135 u32 proto = rqstp->rq_prot,
136 vers = rqstp->rq_vers,
137 proc = rqstp->rq_proc;
138 unsigned long age;
139 int rtn;
140
141 rqstp->rq_cacherep = NULL;
142 if (cache_disabled || type == RC_NOCACHE) {
143 nfsdstats.rcnocache++;
144 return RC_DOIT;
145 }
146
147 spin_lock(&cache_lock);
148 rtn = RC_DOIT;
149
150 rh = &cache_hash[request_hash(xid)];
151 hlist_for_each_entry(rp, hn, rh, c_hash) {
152 if (rp->c_state != RC_UNUSED &&
153 xid == rp->c_xid && proc == rp->c_proc &&
154 proto == rp->c_prot && vers == rp->c_vers &&
155 time_before(jiffies, rp->c_timestamp + 120*HZ) &&
156 memcmp((char*)&rqstp->rq_addr, (char*)&rp->c_addr, sizeof(rp->c_addr))==0) {
157 nfsdstats.rchits++;
158 goto found_entry;
159 }
160 }
161 nfsdstats.rcmisses++;
162
163
164 {
165 int safe = 0;
166 list_for_each_entry(rp, &lru_head, c_lru) {
167 if (rp->c_state != RC_INPROG)
168 break;
169 if (safe++ > CACHESIZE) {
170 printk("nfsd: loop in repcache LRU list\n");
171 cache_disabled = 1;
172 goto out;
173 }
174 }
175 }
176
177
178 if (&rp->c_lru == &lru_head) {
179 static int complaints;
180
181 printk(KERN_WARNING "nfsd: all repcache entries locked!\n");
182 if (++complaints > 5) {
183 printk(KERN_WARNING "nfsd: disabling repcache.\n");
184 cache_disabled = 1;
185 }
186 goto out;
187 }
188
189 rqstp->rq_cacherep = rp;
190 rp->c_state = RC_INPROG;
191 rp->c_xid = xid;
192 rp->c_proc = proc;
193 memcpy(&rp->c_addr, svc_addr_in(rqstp), sizeof(rp->c_addr));
194 rp->c_prot = proto;
195 rp->c_vers = vers;
196 rp->c_timestamp = jiffies;
197
198 hash_refile(rp);
199
200
201 if (rp->c_type == RC_REPLBUFF) {
202 kfree(rp->c_replvec.iov_base);
203 rp->c_replvec.iov_base = NULL;
204 }
205 rp->c_type = RC_NOCACHE;
206 out:
207 spin_unlock(&cache_lock);
208 return rtn;
209
210found_entry:
211
212 age = jiffies - rp->c_timestamp;
213 rp->c_timestamp = jiffies;
214 lru_put_end(rp);
215
216 rtn = RC_DROPIT;
217
218 if (rp->c_state == RC_INPROG || age < RC_DELAY)
219 goto out;
220
221
222
223 rtn = RC_DOIT;
224 if (!rqstp->rq_secure && rp->c_secure)
225 goto out;
226
227
228 switch (rp->c_type) {
229 case RC_NOCACHE:
230 break;
231 case RC_REPLSTAT:
232 svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat);
233 rtn = RC_REPLY;
234 break;
235 case RC_REPLBUFF:
236 if (!nfsd_cache_append(rqstp, &rp->c_replvec))
237 goto out;
238 rtn = RC_REPLY;
239 break;
240 default:
241 printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type);
242 rp->c_state = RC_UNUSED;
243 }
244
245 goto out;
246}
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264void
265nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp)
266{
267 struct svc_cacherep *rp;
268 struct kvec *resv = &rqstp->rq_res.head[0], *cachv;
269 int len;
270
271 if (!(rp = rqstp->rq_cacherep) || cache_disabled)
272 return;
273
274 len = resv->iov_len - ((char*)statp - (char*)resv->iov_base);
275 len >>= 2;
276
277
278 if (!statp || len > (256 >> 2)) {
279 rp->c_state = RC_UNUSED;
280 return;
281 }
282
283 switch (cachetype) {
284 case RC_REPLSTAT:
285 if (len != 1)
286 printk("nfsd: RC_REPLSTAT/reply len %d!\n",len);
287 rp->c_replstat = *statp;
288 break;
289 case RC_REPLBUFF:
290 cachv = &rp->c_replvec;
291 cachv->iov_base = kmalloc(len << 2, GFP_KERNEL);
292 if (!cachv->iov_base) {
293 spin_lock(&cache_lock);
294 rp->c_state = RC_UNUSED;
295 spin_unlock(&cache_lock);
296 return;
297 }
298 cachv->iov_len = len << 2;
299 memcpy(cachv->iov_base, statp, len << 2);
300 break;
301 }
302 spin_lock(&cache_lock);
303 lru_put_end(rp);
304 rp->c_secure = rqstp->rq_secure;
305 rp->c_type = cachetype;
306 rp->c_state = RC_DONE;
307 rp->c_timestamp = jiffies;
308 spin_unlock(&cache_lock);
309 return;
310}
311
312
313
314
315
316
317static int
318nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data)
319{
320 struct kvec *vec = &rqstp->rq_res.head[0];
321
322 if (vec->iov_len + data->iov_len > PAGE_SIZE) {
323 printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n",
324 data->iov_len);
325 return 0;
326 }
327 memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len);
328 vec->iov_len += data->iov_len;
329 return 1;
330}
331