]>
Commit | Line | Data |
---|---|---|
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 | 26 | static struct hlist_head * cache_hash; |
1da177e4 LT |
27 | static struct list_head lru_head; |
28 | static int cache_disabled = 1; | |
29 | ||
fca4217c GB |
30 | /* |
31 | * Calculate the hash index from an XID. | |
32 | */ | |
33 | static inline u32 request_hash(u32 xid) | |
34 | { | |
35 | u32 h = xid; | |
36 | h ^= (xid >> 24); | |
37 | return h & (HASHSIZE-1); | |
38 | } | |
39 | ||
1da177e4 LT |
40 | static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec); |
41 | ||
fca4217c | 42 | /* |
1da177e4 LT |
43 | * 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 | */ | |
47 | static DEFINE_SPINLOCK(cache_lock); | |
48 | ||
d5c3428b | 49 | int nfsd_reply_cache_init(void) |
1da177e4 LT |
50 | { |
51 | struct svc_cacherep *rp; | |
52 | int i; | |
53 | ||
54 | INIT_LIST_HEAD(&lru_head); | |
55 | i = CACHESIZE; | |
d5c3428b | 56 | while (i) { |
1da177e4 | 57 | rp = kmalloc(sizeof(*rp), GFP_KERNEL); |
d5c3428b BF |
58 | if (!rp) |
59 | goto out_nomem; | |
1da177e4 LT |
60 | 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 | ||
fca4217c GB |
67 | cache_hash = kcalloc (HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL); |
68 | if (!cache_hash) | |
d5c3428b | 69 | goto out_nomem; |
1da177e4 LT |
70 | |
71 | cache_disabled = 0; | |
d5c3428b BF |
72 | return 0; |
73 | out_nomem: | |
74 | printk(KERN_ERR "nfsd: failed to allocate reply cache\n"); | |
75 | nfsd_reply_cache_shutdown(); | |
76 | return -ENOMEM; | |
1da177e4 LT |
77 | } |
78 | ||
d5c3428b | 79 | void nfsd_reply_cache_shutdown(void) |
1da177e4 LT |
80 | { |
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 | ||
fca4217c GB |
93 | kfree (cache_hash); |
94 | cache_hash = NULL; | |
1da177e4 LT |
95 | } |
96 | ||
97 | /* | |
98 | * Move cache entry to end of LRU list | |
99 | */ | |
100 | static void | |
101 | lru_put_end(struct svc_cacherep *rp) | |
102 | { | |
f116629d | 103 | list_move_tail(&rp->c_lru, &lru_head); |
1da177e4 LT |
104 | } |
105 | ||
106 | /* | |
107 | * Move a cache entry from one hash list to another | |
108 | */ | |
109 | static void | |
110 | hash_refile(struct svc_cacherep *rp) | |
111 | { | |
112 | hlist_del_init(&rp->c_hash); | |
fca4217c | 113 | hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid)); |
1da177e4 LT |
114 | } |
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 | */ | |
121 | int | |
1091006c | 122 | nfsd_cache_lookup(struct svc_rqst *rqstp) |
1da177e4 LT |
123 | { |
124 | struct hlist_node *hn; | |
125 | struct hlist_head *rh; | |
126 | struct svc_cacherep *rp; | |
c7afef1f AV |
127 | __be32 xid = rqstp->rq_xid; |
128 | u32 proto = rqstp->rq_prot, | |
1da177e4 LT |
129 | vers = rqstp->rq_vers, |
130 | proc = rqstp->rq_proc; | |
131 | unsigned long age; | |
1091006c | 132 | int type = rqstp->rq_cachetype; |
1da177e4 LT |
133 | 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 | ||
fca4217c | 144 | rh = &cache_hash[request_hash(xid)]; |
1da177e4 LT |
145 | 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) && | |
7b9e8522 JL |
150 | 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)) { | |
1da177e4 LT |
152 | 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 | ||
cf0a586c GB |
172 | /* All entries on the LRU are in-progress. This should not happen */ |
173 | if (&rp->c_lru == &lru_head) { | |
1da177e4 LT |
174 | 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; | |
7b9e8522 JL |
188 | 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))); | |
1da177e4 LT |
190 | 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 | ||
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); | |
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 | */ | |
260 | void | |
c7afef1f | 261 | nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp) |
1da177e4 LT |
262 | { |
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; | |
fca4217c | 272 | |
1da177e4 LT |
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; | |
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) { | |
1da177e4 | 289 | rp->c_state = RC_UNUSED; |
1da177e4 LT |
290 | return; |
291 | } | |
292 | cachv->iov_len = len << 2; | |
293 | memcpy(cachv->iov_base, statp, len << 2); | |
294 | break; | |
295 | } | |
296 | spin_lock(&cache_lock); | |
297 | lru_put_end(rp); | |
298 | rp->c_secure = rqstp->rq_secure; | |
299 | rp->c_type = cachetype; | |
300 | rp->c_state = RC_DONE; | |
301 | rp->c_timestamp = jiffies; | |
302 | spin_unlock(&cache_lock); | |
303 | return; | |
304 | } | |
305 | ||
306 | /* | |
307 | * Copy cached reply to current reply buffer. Should always fit. | |
308 | * FIXME as reply is in a page, we should just attach the page, and | |
309 | * keep a refcount.... | |
310 | */ | |
311 | static int | |
312 | nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data) | |
313 | { | |
314 | struct kvec *vec = &rqstp->rq_res.head[0]; | |
315 | ||
316 | if (vec->iov_len + data->iov_len > PAGE_SIZE) { | |
317 | printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n", | |
318 | data->iov_len); | |
319 | return 0; | |
320 | } | |
321 | memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len); | |
322 | vec->iov_len += data->iov_len; | |
323 | return 1; | |
324 | } |