]>
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> |
0338dd15 | 13 | #include <linux/highmem.h> |
5a0e3ad6 | 14 | |
9a74af21 BH |
15 | #include "nfsd.h" |
16 | #include "cache.h" | |
1da177e4 | 17 | |
0338dd15 JL |
18 | #define NFSDDBG_FACILITY NFSDDBG_REPCACHE |
19 | ||
1da177e4 | 20 | #define HASHSIZE 64 |
1da177e4 | 21 | |
fca4217c | 22 | static struct hlist_head * cache_hash; |
1da177e4 | 23 | static struct list_head lru_head; |
8a8bc40d | 24 | static struct kmem_cache *drc_slab; |
0ee0bf7e | 25 | static unsigned int num_drc_entries; |
0338dd15 | 26 | static unsigned int max_drc_entries; |
1da177e4 | 27 | |
fca4217c GB |
28 | /* |
29 | * Calculate the hash index from an XID. | |
30 | */ | |
31 | static inline u32 request_hash(u32 xid) | |
32 | { | |
33 | u32 h = xid; | |
34 | h ^= (xid >> 24); | |
35 | return h & (HASHSIZE-1); | |
36 | } | |
37 | ||
1da177e4 LT |
38 | static int nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *vec); |
39 | ||
fca4217c | 40 | /* |
1da177e4 LT |
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. | |
44 | */ | |
45 | static DEFINE_SPINLOCK(cache_lock); | |
46 | ||
0338dd15 JL |
47 | /* |
48 | * Put a cap on the size of the DRC based on the amount of available | |
49 | * low memory in the machine. | |
50 | * | |
51 | * 64MB: 8192 | |
52 | * 128MB: 11585 | |
53 | * 256MB: 16384 | |
54 | * 512MB: 23170 | |
55 | * 1GB: 32768 | |
56 | * 2GB: 46340 | |
57 | * 4GB: 65536 | |
58 | * 8GB: 92681 | |
59 | * 16GB: 131072 | |
60 | * | |
61 | * ...with a hard cap of 256k entries. In the worst case, each entry will be | |
62 | * ~1k, so the above numbers should give a rough max of the amount of memory | |
63 | * used in k. | |
64 | */ | |
65 | static unsigned int | |
66 | nfsd_cache_size_limit(void) | |
67 | { | |
68 | unsigned int limit; | |
69 | unsigned long low_pages = totalram_pages - totalhigh_pages; | |
70 | ||
71 | limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10); | |
72 | return min_t(unsigned int, limit, 256*1024); | |
73 | } | |
74 | ||
f09841fd JL |
75 | static struct svc_cacherep * |
76 | nfsd_reply_cache_alloc(void) | |
1da177e4 LT |
77 | { |
78 | struct svc_cacherep *rp; | |
f09841fd JL |
79 | |
80 | rp = kmem_cache_alloc(drc_slab, GFP_KERNEL); | |
81 | if (rp) { | |
82 | rp->c_state = RC_UNUSED; | |
83 | rp->c_type = RC_NOCACHE; | |
84 | INIT_LIST_HEAD(&rp->c_lru); | |
85 | INIT_HLIST_NODE(&rp->c_hash); | |
86 | } | |
87 | return rp; | |
88 | } | |
89 | ||
90 | static void | |
91 | nfsd_reply_cache_free_locked(struct svc_cacherep *rp) | |
92 | { | |
25e6b8b0 | 93 | if (rp->c_type == RC_REPLBUFF) |
f09841fd | 94 | kfree(rp->c_replvec.iov_base); |
0338dd15 | 95 | hlist_del(&rp->c_hash); |
f09841fd | 96 | list_del(&rp->c_lru); |
0ee0bf7e | 97 | --num_drc_entries; |
f09841fd JL |
98 | kmem_cache_free(drc_slab, rp); |
99 | } | |
100 | ||
2c6b691c JL |
101 | static void |
102 | nfsd_reply_cache_free(struct svc_cacherep *rp) | |
103 | { | |
104 | spin_lock(&cache_lock); | |
105 | nfsd_reply_cache_free_locked(rp); | |
106 | spin_unlock(&cache_lock); | |
107 | } | |
108 | ||
f09841fd JL |
109 | int nfsd_reply_cache_init(void) |
110 | { | |
8a8bc40d JL |
111 | drc_slab = kmem_cache_create("nfsd_drc", sizeof(struct svc_cacherep), |
112 | 0, 0, NULL); | |
113 | if (!drc_slab) | |
114 | goto out_nomem; | |
115 | ||
0338dd15 | 116 | cache_hash = kcalloc(HASHSIZE, sizeof(struct hlist_head), GFP_KERNEL); |
fca4217c | 117 | if (!cache_hash) |
d5c3428b | 118 | goto out_nomem; |
1da177e4 | 119 | |
0338dd15 JL |
120 | INIT_LIST_HEAD(&lru_head); |
121 | max_drc_entries = nfsd_cache_size_limit(); | |
122 | num_drc_entries = 0; | |
d5c3428b BF |
123 | return 0; |
124 | out_nomem: | |
125 | printk(KERN_ERR "nfsd: failed to allocate reply cache\n"); | |
126 | nfsd_reply_cache_shutdown(); | |
127 | return -ENOMEM; | |
1da177e4 LT |
128 | } |
129 | ||
d5c3428b | 130 | void nfsd_reply_cache_shutdown(void) |
1da177e4 LT |
131 | { |
132 | struct svc_cacherep *rp; | |
133 | ||
134 | while (!list_empty(&lru_head)) { | |
135 | rp = list_entry(lru_head.next, struct svc_cacherep, c_lru); | |
f09841fd | 136 | nfsd_reply_cache_free_locked(rp); |
1da177e4 LT |
137 | } |
138 | ||
fca4217c GB |
139 | kfree (cache_hash); |
140 | cache_hash = NULL; | |
8a8bc40d JL |
141 | |
142 | if (drc_slab) { | |
143 | kmem_cache_destroy(drc_slab); | |
144 | drc_slab = NULL; | |
145 | } | |
1da177e4 LT |
146 | } |
147 | ||
148 | /* | |
149 | * Move cache entry to end of LRU list | |
150 | */ | |
151 | static void | |
152 | lru_put_end(struct svc_cacherep *rp) | |
153 | { | |
56c2548b | 154 | rp->c_timestamp = jiffies; |
f116629d | 155 | list_move_tail(&rp->c_lru, &lru_head); |
1da177e4 LT |
156 | } |
157 | ||
158 | /* | |
159 | * Move a cache entry from one hash list to another | |
160 | */ | |
161 | static void | |
162 | hash_refile(struct svc_cacherep *rp) | |
163 | { | |
164 | hlist_del_init(&rp->c_hash); | |
fca4217c | 165 | hlist_add_head(&rp->c_hash, cache_hash + request_hash(rp->c_xid)); |
1da177e4 LT |
166 | } |
167 | ||
d1a0774d JL |
168 | static inline bool |
169 | nfsd_cache_entry_expired(struct svc_cacherep *rp) | |
170 | { | |
171 | return rp->c_state != RC_INPROG && | |
172 | time_after(jiffies, rp->c_timestamp + RC_EXPIRE); | |
173 | } | |
174 | ||
a4a3ec32 JL |
175 | /* |
176 | * Search the request hash for an entry that matches the given rqstp. | |
177 | * Must be called with cache_lock held. Returns the found entry or | |
178 | * NULL on failure. | |
179 | */ | |
180 | static struct svc_cacherep * | |
181 | nfsd_cache_search(struct svc_rqst *rqstp) | |
182 | { | |
183 | struct svc_cacherep *rp; | |
184 | struct hlist_node *hn; | |
185 | struct hlist_head *rh; | |
186 | __be32 xid = rqstp->rq_xid; | |
187 | u32 proto = rqstp->rq_prot, | |
188 | vers = rqstp->rq_vers, | |
189 | proc = rqstp->rq_proc; | |
190 | ||
191 | rh = &cache_hash[request_hash(xid)]; | |
192 | hlist_for_each_entry(rp, hn, rh, c_hash) { | |
2c6b691c | 193 | if (xid == rp->c_xid && proc == rp->c_proc && |
a4a3ec32 JL |
194 | proto == rp->c_prot && vers == rp->c_vers && |
195 | !nfsd_cache_entry_expired(rp) && | |
196 | rpc_cmp_addr(svc_addr(rqstp), (struct sockaddr *)&rp->c_addr) && | |
197 | rpc_get_port(svc_addr(rqstp)) == rpc_get_port((struct sockaddr *)&rp->c_addr)) | |
198 | return rp; | |
199 | } | |
200 | return NULL; | |
201 | } | |
202 | ||
1da177e4 LT |
203 | /* |
204 | * Try to find an entry matching the current call in the cache. When none | |
205 | * is found, we grab the oldest unlocked entry off the LRU list. | |
206 | * Note that no operation within the loop may sleep. | |
207 | */ | |
208 | int | |
1091006c | 209 | nfsd_cache_lookup(struct svc_rqst *rqstp) |
1da177e4 | 210 | { |
0338dd15 | 211 | struct svc_cacherep *rp, *found; |
c7afef1f AV |
212 | __be32 xid = rqstp->rq_xid; |
213 | u32 proto = rqstp->rq_prot, | |
1da177e4 LT |
214 | vers = rqstp->rq_vers, |
215 | proc = rqstp->rq_proc; | |
216 | unsigned long age; | |
1091006c | 217 | int type = rqstp->rq_cachetype; |
1da177e4 LT |
218 | int rtn; |
219 | ||
220 | rqstp->rq_cacherep = NULL; | |
13cc8a78 | 221 | if (type == RC_NOCACHE) { |
1da177e4 LT |
222 | nfsdstats.rcnocache++; |
223 | return RC_DOIT; | |
224 | } | |
225 | ||
226 | spin_lock(&cache_lock); | |
227 | rtn = RC_DOIT; | |
228 | ||
a4a3ec32 | 229 | rp = nfsd_cache_search(rqstp); |
0338dd15 | 230 | if (rp) |
a4a3ec32 | 231 | goto found_entry; |
0338dd15 JL |
232 | |
233 | /* Try to use the first entry on the LRU */ | |
234 | if (!list_empty(&lru_head)) { | |
235 | rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru); | |
236 | if (nfsd_cache_entry_expired(rp) || | |
237 | num_drc_entries >= max_drc_entries) | |
238 | goto setup_entry; | |
1da177e4 | 239 | } |
1da177e4 | 240 | |
0338dd15 JL |
241 | spin_unlock(&cache_lock); |
242 | rp = nfsd_reply_cache_alloc(); | |
243 | if (!rp) { | |
244 | dprintk("nfsd: unable to allocate DRC entry!\n"); | |
245 | return RC_DOIT; | |
1da177e4 | 246 | } |
0338dd15 JL |
247 | spin_lock(&cache_lock); |
248 | ++num_drc_entries; | |
249 | ||
250 | /* | |
251 | * Must search again just in case someone inserted one | |
252 | * after we dropped the lock above. | |
253 | */ | |
254 | found = nfsd_cache_search(rqstp); | |
255 | if (found) { | |
256 | nfsd_reply_cache_free_locked(rp); | |
257 | rp = found; | |
258 | goto found_entry; | |
1da177e4 LT |
259 | } |
260 | ||
0338dd15 JL |
261 | /* |
262 | * We're keeping the one we just allocated. Are we now over the | |
263 | * limit? Prune one off the tip of the LRU in trade for the one we | |
264 | * just allocated if so. | |
265 | */ | |
266 | if (num_drc_entries >= max_drc_entries) | |
267 | nfsd_reply_cache_free_locked(list_first_entry(&lru_head, | |
268 | struct svc_cacherep, c_lru)); | |
1da177e4 | 269 | |
0338dd15 JL |
270 | setup_entry: |
271 | nfsdstats.rcmisses++; | |
1da177e4 LT |
272 | rqstp->rq_cacherep = rp; |
273 | rp->c_state = RC_INPROG; | |
274 | rp->c_xid = xid; | |
275 | rp->c_proc = proc; | |
7b9e8522 JL |
276 | rpc_copy_addr((struct sockaddr *)&rp->c_addr, svc_addr(rqstp)); |
277 | rpc_set_port((struct sockaddr *)&rp->c_addr, rpc_get_port(svc_addr(rqstp))); | |
1da177e4 LT |
278 | rp->c_prot = proto; |
279 | rp->c_vers = vers; | |
1da177e4 LT |
280 | |
281 | hash_refile(rp); | |
56c2548b | 282 | lru_put_end(rp); |
1da177e4 LT |
283 | |
284 | /* release any buffer */ | |
285 | if (rp->c_type == RC_REPLBUFF) { | |
286 | kfree(rp->c_replvec.iov_base); | |
287 | rp->c_replvec.iov_base = NULL; | |
288 | } | |
289 | rp->c_type = RC_NOCACHE; | |
290 | out: | |
291 | spin_unlock(&cache_lock); | |
292 | return rtn; | |
293 | ||
294 | found_entry: | |
0338dd15 | 295 | nfsdstats.rchits++; |
1da177e4 LT |
296 | /* We found a matching entry which is either in progress or done. */ |
297 | age = jiffies - rp->c_timestamp; | |
1da177e4 LT |
298 | lru_put_end(rp); |
299 | ||
300 | rtn = RC_DROPIT; | |
301 | /* Request being processed or excessive rexmits */ | |
302 | if (rp->c_state == RC_INPROG || age < RC_DELAY) | |
303 | goto out; | |
304 | ||
305 | /* From the hall of fame of impractical attacks: | |
306 | * Is this a user who tries to snoop on the cache? */ | |
307 | rtn = RC_DOIT; | |
308 | if (!rqstp->rq_secure && rp->c_secure) | |
309 | goto out; | |
310 | ||
311 | /* Compose RPC reply header */ | |
312 | switch (rp->c_type) { | |
313 | case RC_NOCACHE: | |
314 | break; | |
315 | case RC_REPLSTAT: | |
316 | svc_putu32(&rqstp->rq_res.head[0], rp->c_replstat); | |
317 | rtn = RC_REPLY; | |
318 | break; | |
319 | case RC_REPLBUFF: | |
320 | if (!nfsd_cache_append(rqstp, &rp->c_replvec)) | |
321 | goto out; /* should not happen */ | |
322 | rtn = RC_REPLY; | |
323 | break; | |
324 | default: | |
325 | printk(KERN_WARNING "nfsd: bad repcache type %d\n", rp->c_type); | |
0338dd15 | 326 | nfsd_reply_cache_free_locked(rp); |
1da177e4 LT |
327 | } |
328 | ||
329 | goto out; | |
330 | } | |
331 | ||
332 | /* | |
333 | * Update a cache entry. This is called from nfsd_dispatch when | |
334 | * the procedure has been executed and the complete reply is in | |
335 | * rqstp->rq_res. | |
336 | * | |
337 | * We're copying around data here rather than swapping buffers because | |
338 | * the toplevel loop requires max-sized buffers, which would be a waste | |
339 | * of memory for a cache with a max reply size of 100 bytes (diropokres). | |
340 | * | |
341 | * If we should start to use different types of cache entries tailored | |
342 | * specifically for attrstat and fh's, we may save even more space. | |
343 | * | |
344 | * Also note that a cachetype of RC_NOCACHE can legally be passed when | |
345 | * nfsd failed to encode a reply that otherwise would have been cached. | |
346 | * In this case, nfsd_cache_update is called with statp == NULL. | |
347 | */ | |
348 | void | |
c7afef1f | 349 | nfsd_cache_update(struct svc_rqst *rqstp, int cachetype, __be32 *statp) |
1da177e4 | 350 | { |
13cc8a78 | 351 | struct svc_cacherep *rp = rqstp->rq_cacherep; |
1da177e4 LT |
352 | struct kvec *resv = &rqstp->rq_res.head[0], *cachv; |
353 | int len; | |
354 | ||
13cc8a78 | 355 | if (!rp) |
1da177e4 LT |
356 | return; |
357 | ||
358 | len = resv->iov_len - ((char*)statp - (char*)resv->iov_base); | |
359 | len >>= 2; | |
fca4217c | 360 | |
1da177e4 LT |
361 | /* Don't cache excessive amounts of data and XDR failures */ |
362 | if (!statp || len > (256 >> 2)) { | |
2c6b691c | 363 | nfsd_reply_cache_free(rp); |
1da177e4 LT |
364 | return; |
365 | } | |
366 | ||
367 | switch (cachetype) { | |
368 | case RC_REPLSTAT: | |
369 | if (len != 1) | |
370 | printk("nfsd: RC_REPLSTAT/reply len %d!\n",len); | |
371 | rp->c_replstat = *statp; | |
372 | break; | |
373 | case RC_REPLBUFF: | |
374 | cachv = &rp->c_replvec; | |
375 | cachv->iov_base = kmalloc(len << 2, GFP_KERNEL); | |
376 | if (!cachv->iov_base) { | |
2c6b691c | 377 | nfsd_reply_cache_free(rp); |
1da177e4 LT |
378 | return; |
379 | } | |
380 | cachv->iov_len = len << 2; | |
381 | memcpy(cachv->iov_base, statp, len << 2); | |
382 | break; | |
2c6b691c JL |
383 | case RC_NOCACHE: |
384 | nfsd_reply_cache_free(rp); | |
385 | return; | |
1da177e4 LT |
386 | } |
387 | spin_lock(&cache_lock); | |
388 | lru_put_end(rp); | |
389 | rp->c_secure = rqstp->rq_secure; | |
390 | rp->c_type = cachetype; | |
391 | rp->c_state = RC_DONE; | |
1da177e4 LT |
392 | spin_unlock(&cache_lock); |
393 | return; | |
394 | } | |
395 | ||
396 | /* | |
397 | * Copy cached reply to current reply buffer. Should always fit. | |
398 | * FIXME as reply is in a page, we should just attach the page, and | |
399 | * keep a refcount.... | |
400 | */ | |
401 | static int | |
402 | nfsd_cache_append(struct svc_rqst *rqstp, struct kvec *data) | |
403 | { | |
404 | struct kvec *vec = &rqstp->rq_res.head[0]; | |
405 | ||
406 | if (vec->iov_len + data->iov_len > PAGE_SIZE) { | |
407 | printk(KERN_WARNING "nfsd: cached reply too large (%Zd).\n", | |
408 | data->iov_len); | |
409 | return 0; | |
410 | } | |
411 | memcpy((char*)vec->iov_base + vec->iov_len, data->iov_base, data->iov_len); | |
412 | vec->iov_len += data->iov_len; | |
413 | return 1; | |
414 | } |