Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfsd/nfscache.c | |
3 | * | |
4 | * Request reply cache. This is currently a global cache, but this may | |
5 | * change in the future and be a per-client cache. | |
6 | * | |
7 | * This code is heavily inspired by the 44BSD implementation, although | |
8 | * it does things a bit differently. | |
9 | * | |
10 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> | |
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 | /* Size of reply cache. Common values are: | |
25 | * 4.3BSD: 128 | |
26 | * 4.4BSD: 256 | |
27 | * Solaris2: 1024 | |
28 | * DEC Unix: 512-4096 | |
29 | */ | |
30 | #define CACHESIZE 1024 | |
31 | #define HASHSIZE 64 | |
1da177e4 | 32 | |
fca4217c | 33 | static struct hlist_head * cache_hash; |
1da177e4 LT |
34 | static struct list_head lru_head; |
35 | static int cache_disabled = 1; | |
36 | ||
fca4217c GB |
37 | /* |
38 | * Calculate the hash index from an XID. | |
39 | */ | |
40 | static inline u32 request_hash(u32 xid) | |
41 | { | |
42 | u32 h = xid; | |
43 | h ^= (xid >> 24); | |
44 | return h & (HASHSIZE-1); | |
45 | } | |
46 | ||
1da177e4 LT |
47 | static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec); |
48 | ||
fca4217c | 49 | /* |
1da177e4 LT |
50 | * locking for the reply cache: |
51 | * A cache entry is "single use" if c_state == RC_INPROG | |
52 | * Otherwise, it when accessing _prev or _next, the lock must be held. | |
53 | */ | |
54 | static DEFINE_SPINLOCK(cache_lock); | |
55 | ||
d5c3428b | 56 | int nfsd_reply_cache_init(void) |
1da177e4 LT |
57 | { |
58 | struct svc_cacherep *rp; | |
59 | int i; | |
60 | ||
61 | INIT_LIST_HEAD(&lru_head); | |
62 | i = CACHESIZE; | |
d5c3428b | 63 | while (i) { |
1da177e4 | 64 | rp = kmalloc(sizeof(*rp), GFP_KERNEL); |
d5c3428b BF |
65 | if (!rp) |
66 | goto out_nomem; | |
1da177e4 LT |
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 | ||
fca4217c GB |
74 | cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL); |
75 | if (!cache_hash) | |
d5c3428b | 76 | goto out_nomem; |
1da177e4 LT |
77 | |
78 | cache_disabled = 0; | |
d5c3428b BF |
79 | return 0; |
80 | out_nomem: | |
81 | printk(KERN_ERR "nfsd: failed to allocate reply cache\n"); | |
82 | nfsd_reply_cache_shutdown(); | |
83 | return -ENOMEM; | |
1da177e4 LT |
84 | } |
85 | ||
d5c3428b | 86 | void nfsd_reply_cache_shutdown(void) |
1da177e4 LT |
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 | ||
fca4217c GB |
100 | kfree (cache_hash); |
101 | cache_hash = NULL; | |
1da177e4 LT |
102 | } |
103 | ||
104 | /* | |
105 | * Move cache entry to end of LRU list | |
106 | */ | |
107 | static void | |
108 | lru_put_end(struct svc_cacherep *rp) | |
109 | { | |
f116629d | 110 | list_move_tail(&rp->c_lru, &lru_head); |
1da177e4 LT |
111 | } |
112 | ||
113 | /* | |
114 | * Move a cache entry from one hash list to another | |
115 | */ | |
116 | static void | |
117 | hash_refile(struct svc_cacherep *rp) | |
118 | { | |
119 | hlist_del_init(&rp->c_hash); | |
fca4217c | 120 | hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid)); |
1da177e4 LT |
121 | } |
122 | ||
123 | /* | |
124 | * Try to find an entry matching the current call in the cache. When none | |
125 | * is found, we grab the oldest unlocked entry off the LRU list. | |
126 | * Note that no operation within the loop may sleep. | |
127 | */ | |
128 | int | |
129 | nfsd_cache_lookup(struct svc_rqst *rqstp, int type) | |
130 | { | |
131 | struct hlist_node *hn; | |
132 | struct hlist_head *rh; | |
133 | struct svc_cacherep *rp; | |
c7afef1f AV |
134 | __be32 xid = rqstp->rq_xid; |
135 | u32 proto = rqstp->rq_prot, | |
1da177e4 LT |
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 | ||
fca4217c | 150 | rh = &cache_hash[request_hash(xid)]; |
1da177e4 LT |
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 | /* This loop shouldn't take more than a few iterations normally */ | |
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 | ||
cf0a586c GB |
177 | /* All entries on the LRU are in-progress. This should not happen */ |
178 | if (&rp->c_lru == &lru_head) { | |
1da177e4 LT |
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; | |
27459f09 | 193 | memcpy(&rp->c_addr, svc_addr_in(rqstp), sizeof(rp->c_addr)); |
1da177e4 LT |
194 | rp->c_prot = proto; |
195 | rp->c_vers = vers; | |
196 | rp->c_timestamp = jiffies; | |
197 | ||
198 | hash_refile(rp); | |
199 | ||
200 | /* release any buffer */ | |
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 | ||
210 | found_entry: | |
211 | /* We found a matching entry which is either in progress or done. */ | |
212 | age = jiffies - rp->c_timestamp; | |
213 | rp->c_timestamp = jiffies; | |
214 | lru_put_end(rp); | |
215 | ||
216 | rtn = RC_DROPIT; | |
217 | /* Request being processed or excessive rexmits */ | |
218 | if (rp->c_state == RC_INPROG || age < RC_DELAY) | |
219 | goto out; | |
220 | ||
221 | /* From the hall of fame of impractical attacks: | |
222 | * Is this a user who tries to snoop on the cache? */ | |
223 | rtn = RC_DOIT; | |
224 | if (!rqstp->rq_secure && rp->c_secure) | |
225 | goto out; | |
226 | ||
227 | /* Compose RPC reply header */ | |
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; /* should not happen */ | |
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 | * Update a cache entry. This is called from nfsd_dispatch when | |
250 | * the procedure has been executed and the complete reply is in | |
251 | * rqstp->rq_res. | |
252 | * | |
253 | * We're copying around data here rather than swapping buffers because | |
254 | * the toplevel loop requires max-sized buffers, which would be a waste | |
255 | * of memory for a cache with a max reply size of 100 bytes (diropokres). | |
256 | * | |
257 | * If we should start to use different types of cache entries tailored | |
258 | * specifically for attrstat and fh's, we may save even more space. | |
259 | * | |
260 | * Also note that a cachetype of RC_NOCACHE can legally be passed when | |
261 | * nfsd failed to encode a reply that otherwise would have been cached. | |
262 | * In this case, nfsd_cache_update is called with statp == NULL. | |
263 | */ | |
264 | void | |
c7afef1f | 265 | nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp) |
1da177e4 LT |
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; | |
fca4217c | 276 | |
1da177e4 LT |
277 | /* Don't cache excessive amounts of data and XDR failures */ |
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 | * Copy cached reply to current reply buffer. Should always fit. | |
314 | * FIXME as reply is in a page, we should just attach the page, and | |
315 | * keep a refcount.... | |
316 | */ | |
317 | static int | |
318 | nfsd_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 | } |