]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net/sunrpc/cache.c | |
3 | * | |
4 | * Generic code for various authentication-related caches | |
5 | * used by sunrpc clients and servers. | |
6 | * | |
7 | * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au> | |
8 | * | |
9 | * Released under terms in GPL version 2. See COPYING. | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <linux/types.h> | |
14 | #include <linux/fs.h> | |
15 | #include <linux/file.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/signal.h> | |
18 | #include <linux/sched.h> | |
19 | #include <linux/kmod.h> | |
20 | #include <linux/list.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/ctype.h> | |
23 | #include <asm/uaccess.h> | |
24 | #include <linux/poll.h> | |
25 | #include <linux/seq_file.h> | |
26 | #include <linux/proc_fs.h> | |
27 | #include <linux/net.h> | |
28 | #include <linux/workqueue.h> | |
4a3e2f71 | 29 | #include <linux/mutex.h> |
da77005f | 30 | #include <linux/pagemap.h> |
1da177e4 LT |
31 | #include <asm/ioctls.h> |
32 | #include <linux/sunrpc/types.h> | |
33 | #include <linux/sunrpc/cache.h> | |
34 | #include <linux/sunrpc/stats.h> | |
35 | ||
36 | #define RPCDBG_FACILITY RPCDBG_CACHE | |
37 | ||
e0bb89ef | 38 | static int cache_defer_req(struct cache_req *req, struct cache_head *item); |
1da177e4 LT |
39 | static void cache_revisit_request(struct cache_head *item); |
40 | ||
74cae61a | 41 | static void cache_init(struct cache_head *h) |
1da177e4 LT |
42 | { |
43 | time_t now = get_seconds(); | |
44 | h->next = NULL; | |
45 | h->flags = 0; | |
baab935f | 46 | kref_init(&h->ref); |
1da177e4 LT |
47 | h->expiry_time = now + CACHE_NEW_EXPIRY; |
48 | h->last_refresh = now; | |
49 | } | |
50 | ||
15a5f6bd N |
51 | struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail, |
52 | struct cache_head *key, int hash) | |
53 | { | |
54 | struct cache_head **head, **hp; | |
55 | struct cache_head *new = NULL; | |
56 | ||
57 | head = &detail->hash_table[hash]; | |
58 | ||
59 | read_lock(&detail->hash_lock); | |
60 | ||
61 | for (hp=head; *hp != NULL ; hp = &(*hp)->next) { | |
62 | struct cache_head *tmp = *hp; | |
63 | if (detail->match(tmp, key)) { | |
64 | cache_get(tmp); | |
65 | read_unlock(&detail->hash_lock); | |
66 | return tmp; | |
67 | } | |
68 | } | |
69 | read_unlock(&detail->hash_lock); | |
70 | /* Didn't find anything, insert an empty entry */ | |
71 | ||
72 | new = detail->alloc(); | |
73 | if (!new) | |
74 | return NULL; | |
2f34931f NB |
75 | /* must fully initialise 'new', else |
76 | * we might get lose if we need to | |
77 | * cache_put it soon. | |
78 | */ | |
15a5f6bd | 79 | cache_init(new); |
2f34931f | 80 | detail->init(new, key); |
15a5f6bd N |
81 | |
82 | write_lock(&detail->hash_lock); | |
83 | ||
84 | /* check if entry appeared while we slept */ | |
85 | for (hp=head; *hp != NULL ; hp = &(*hp)->next) { | |
86 | struct cache_head *tmp = *hp; | |
87 | if (detail->match(tmp, key)) { | |
88 | cache_get(tmp); | |
89 | write_unlock(&detail->hash_lock); | |
baab935f | 90 | cache_put(new, detail); |
15a5f6bd N |
91 | return tmp; |
92 | } | |
93 | } | |
15a5f6bd N |
94 | new->next = *head; |
95 | *head = new; | |
96 | detail->entries++; | |
97 | cache_get(new); | |
98 | write_unlock(&detail->hash_lock); | |
99 | ||
100 | return new; | |
101 | } | |
24c3767e | 102 | EXPORT_SYMBOL_GPL(sunrpc_cache_lookup); |
15a5f6bd | 103 | |
ebd0cb1a N |
104 | |
105 | static void queue_loose(struct cache_detail *detail, struct cache_head *ch); | |
106 | ||
107 | static int cache_fresh_locked(struct cache_head *head, time_t expiry) | |
108 | { | |
109 | head->expiry_time = expiry; | |
110 | head->last_refresh = get_seconds(); | |
111 | return !test_and_set_bit(CACHE_VALID, &head->flags); | |
112 | } | |
113 | ||
114 | static void cache_fresh_unlocked(struct cache_head *head, | |
115 | struct cache_detail *detail, int new) | |
116 | { | |
117 | if (new) | |
118 | cache_revisit_request(head); | |
119 | if (test_and_clear_bit(CACHE_PENDING, &head->flags)) { | |
120 | cache_revisit_request(head); | |
121 | queue_loose(detail, head); | |
122 | } | |
123 | } | |
124 | ||
15a5f6bd N |
125 | struct cache_head *sunrpc_cache_update(struct cache_detail *detail, |
126 | struct cache_head *new, struct cache_head *old, int hash) | |
127 | { | |
128 | /* The 'old' entry is to be replaced by 'new'. | |
129 | * If 'old' is not VALID, we update it directly, | |
130 | * otherwise we need to replace it | |
131 | */ | |
132 | struct cache_head **head; | |
133 | struct cache_head *tmp; | |
ebd0cb1a | 134 | int is_new; |
15a5f6bd N |
135 | |
136 | if (!test_bit(CACHE_VALID, &old->flags)) { | |
137 | write_lock(&detail->hash_lock); | |
138 | if (!test_bit(CACHE_VALID, &old->flags)) { | |
139 | if (test_bit(CACHE_NEGATIVE, &new->flags)) | |
140 | set_bit(CACHE_NEGATIVE, &old->flags); | |
141 | else | |
142 | detail->update(old, new); | |
ebd0cb1a | 143 | is_new = cache_fresh_locked(old, new->expiry_time); |
15a5f6bd | 144 | write_unlock(&detail->hash_lock); |
ebd0cb1a | 145 | cache_fresh_unlocked(old, detail, is_new); |
15a5f6bd N |
146 | return old; |
147 | } | |
148 | write_unlock(&detail->hash_lock); | |
149 | } | |
150 | /* We need to insert a new entry */ | |
151 | tmp = detail->alloc(); | |
152 | if (!tmp) { | |
baab935f | 153 | cache_put(old, detail); |
15a5f6bd N |
154 | return NULL; |
155 | } | |
156 | cache_init(tmp); | |
157 | detail->init(tmp, old); | |
158 | head = &detail->hash_table[hash]; | |
159 | ||
160 | write_lock(&detail->hash_lock); | |
161 | if (test_bit(CACHE_NEGATIVE, &new->flags)) | |
162 | set_bit(CACHE_NEGATIVE, &tmp->flags); | |
163 | else | |
164 | detail->update(tmp, new); | |
165 | tmp->next = *head; | |
166 | *head = tmp; | |
f2d39586 | 167 | detail->entries++; |
15a5f6bd | 168 | cache_get(tmp); |
ebd0cb1a N |
169 | is_new = cache_fresh_locked(tmp, new->expiry_time); |
170 | cache_fresh_locked(old, 0); | |
15a5f6bd | 171 | write_unlock(&detail->hash_lock); |
ebd0cb1a N |
172 | cache_fresh_unlocked(tmp, detail, is_new); |
173 | cache_fresh_unlocked(old, detail, 0); | |
baab935f | 174 | cache_put(old, detail); |
15a5f6bd N |
175 | return tmp; |
176 | } | |
24c3767e | 177 | EXPORT_SYMBOL_GPL(sunrpc_cache_update); |
1da177e4 | 178 | |
bc74b4f5 TM |
179 | static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h) |
180 | { | |
181 | if (!cd->cache_upcall) | |
182 | return -EINVAL; | |
183 | return cd->cache_upcall(cd, h); | |
184 | } | |
185 | ||
1da177e4 LT |
186 | /* |
187 | * This is the generic cache management routine for all | |
188 | * the authentication caches. | |
189 | * It checks the currency of a cache item and will (later) | |
190 | * initiate an upcall to fill it if needed. | |
191 | * | |
192 | * | |
193 | * Returns 0 if the cache_head can be used, or cache_puts it and returns | |
194 | * -EAGAIN if upcall is pending, | |
e0bb89ef | 195 | * -ETIMEDOUT if upcall failed and should be retried, |
1da177e4 LT |
196 | * -ENOENT if cache entry was negative |
197 | */ | |
198 | int cache_check(struct cache_detail *detail, | |
199 | struct cache_head *h, struct cache_req *rqstp) | |
200 | { | |
201 | int rv; | |
202 | long refresh_age, age; | |
203 | ||
204 | /* First decide return status as best we can */ | |
205 | if (!test_bit(CACHE_VALID, &h->flags) || | |
206 | h->expiry_time < get_seconds()) | |
207 | rv = -EAGAIN; | |
208 | else if (detail->flush_time > h->last_refresh) | |
209 | rv = -EAGAIN; | |
210 | else { | |
211 | /* entry is valid */ | |
212 | if (test_bit(CACHE_NEGATIVE, &h->flags)) | |
213 | rv = -ENOENT; | |
214 | else rv = 0; | |
215 | } | |
216 | ||
217 | /* now see if we want to start an upcall */ | |
218 | refresh_age = (h->expiry_time - h->last_refresh); | |
219 | age = get_seconds() - h->last_refresh; | |
220 | ||
221 | if (rqstp == NULL) { | |
222 | if (rv == -EAGAIN) | |
223 | rv = -ENOENT; | |
224 | } else if (rv == -EAGAIN || age > refresh_age/2) { | |
46121cf7 CL |
225 | dprintk("RPC: Want update, refage=%ld, age=%ld\n", |
226 | refresh_age, age); | |
1da177e4 LT |
227 | if (!test_and_set_bit(CACHE_PENDING, &h->flags)) { |
228 | switch (cache_make_upcall(detail, h)) { | |
229 | case -EINVAL: | |
230 | clear_bit(CACHE_PENDING, &h->flags); | |
231 | if (rv == -EAGAIN) { | |
232 | set_bit(CACHE_NEGATIVE, &h->flags); | |
ebd0cb1a N |
233 | cache_fresh_unlocked(h, detail, |
234 | cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY)); | |
1da177e4 LT |
235 | rv = -ENOENT; |
236 | } | |
237 | break; | |
238 | ||
239 | case -EAGAIN: | |
240 | clear_bit(CACHE_PENDING, &h->flags); | |
241 | cache_revisit_request(h); | |
242 | break; | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
247 | if (rv == -EAGAIN) | |
e0bb89ef BF |
248 | if (cache_defer_req(rqstp, h) != 0) |
249 | rv = -ETIMEDOUT; | |
1da177e4 | 250 | |
4013edea | 251 | if (rv) |
baab935f | 252 | cache_put(h, detail); |
1da177e4 LT |
253 | return rv; |
254 | } | |
24c3767e | 255 | EXPORT_SYMBOL_GPL(cache_check); |
1da177e4 | 256 | |
1da177e4 LT |
257 | /* |
258 | * caches need to be periodically cleaned. | |
259 | * For this we maintain a list of cache_detail and | |
260 | * a current pointer into that list and into the table | |
261 | * for that entry. | |
262 | * | |
263 | * Each time clean_cache is called it finds the next non-empty entry | |
264 | * in the current table and walks the list in that entry | |
265 | * looking for entries that can be removed. | |
266 | * | |
267 | * An entry gets removed if: | |
268 | * - The expiry is before current time | |
269 | * - The last_refresh time is before the flush_time for that cache | |
270 | * | |
271 | * later we might drop old entries with non-NEVER expiry if that table | |
272 | * is getting 'full' for some definition of 'full' | |
273 | * | |
274 | * The question of "how often to scan a table" is an interesting one | |
275 | * and is answered in part by the use of the "nextcheck" field in the | |
276 | * cache_detail. | |
277 | * When a scan of a table begins, the nextcheck field is set to a time | |
278 | * that is well into the future. | |
279 | * While scanning, if an expiry time is found that is earlier than the | |
280 | * current nextcheck time, nextcheck is set to that expiry time. | |
281 | * If the flush_time is ever set to a time earlier than the nextcheck | |
282 | * time, the nextcheck time is then set to that flush_time. | |
283 | * | |
284 | * A table is then only scanned if the current time is at least | |
285 | * the nextcheck time. | |
cca5172a | 286 | * |
1da177e4 LT |
287 | */ |
288 | ||
289 | static LIST_HEAD(cache_list); | |
290 | static DEFINE_SPINLOCK(cache_list_lock); | |
291 | static struct cache_detail *current_detail; | |
292 | static int current_index; | |
293 | ||
65f27f38 DH |
294 | static void do_cache_clean(struct work_struct *work); |
295 | static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean); | |
1da177e4 | 296 | |
5b7a1b9f | 297 | static void sunrpc_init_cache_detail(struct cache_detail *cd) |
ffe9386b | 298 | { |
1da177e4 LT |
299 | rwlock_init(&cd->hash_lock); |
300 | INIT_LIST_HEAD(&cd->queue); | |
301 | spin_lock(&cache_list_lock); | |
302 | cd->nextcheck = 0; | |
303 | cd->entries = 0; | |
304 | atomic_set(&cd->readers, 0); | |
305 | cd->last_close = 0; | |
306 | cd->last_warn = -1; | |
307 | list_add(&cd->others, &cache_list); | |
308 | spin_unlock(&cache_list_lock); | |
309 | ||
310 | /* start the cleaning process */ | |
52bad64d | 311 | schedule_delayed_work(&cache_cleaner, 0); |
1da177e4 LT |
312 | } |
313 | ||
5b7a1b9f | 314 | static void sunrpc_destroy_cache_detail(struct cache_detail *cd) |
1da177e4 LT |
315 | { |
316 | cache_purge(cd); | |
317 | spin_lock(&cache_list_lock); | |
318 | write_lock(&cd->hash_lock); | |
319 | if (cd->entries || atomic_read(&cd->inuse)) { | |
320 | write_unlock(&cd->hash_lock); | |
321 | spin_unlock(&cache_list_lock); | |
df95a9d4 | 322 | goto out; |
1da177e4 LT |
323 | } |
324 | if (current_detail == cd) | |
325 | current_detail = NULL; | |
326 | list_del_init(&cd->others); | |
327 | write_unlock(&cd->hash_lock); | |
328 | spin_unlock(&cache_list_lock); | |
1da177e4 LT |
329 | if (list_empty(&cache_list)) { |
330 | /* module must be being unloaded so its safe to kill the worker */ | |
4011cd97 | 331 | cancel_delayed_work_sync(&cache_cleaner); |
1da177e4 | 332 | } |
df95a9d4 BF |
333 | return; |
334 | out: | |
335 | printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name); | |
1da177e4 | 336 | } |
5b7a1b9f | 337 | |
1da177e4 LT |
338 | /* clean cache tries to find something to clean |
339 | * and cleans it. | |
340 | * It returns 1 if it cleaned something, | |
341 | * 0 if it didn't find anything this time | |
342 | * -1 if it fell off the end of the list. | |
343 | */ | |
344 | static int cache_clean(void) | |
345 | { | |
346 | int rv = 0; | |
347 | struct list_head *next; | |
348 | ||
349 | spin_lock(&cache_list_lock); | |
350 | ||
351 | /* find a suitable table if we don't already have one */ | |
352 | while (current_detail == NULL || | |
353 | current_index >= current_detail->hash_size) { | |
354 | if (current_detail) | |
355 | next = current_detail->others.next; | |
356 | else | |
357 | next = cache_list.next; | |
358 | if (next == &cache_list) { | |
359 | current_detail = NULL; | |
360 | spin_unlock(&cache_list_lock); | |
361 | return -1; | |
362 | } | |
363 | current_detail = list_entry(next, struct cache_detail, others); | |
364 | if (current_detail->nextcheck > get_seconds()) | |
365 | current_index = current_detail->hash_size; | |
366 | else { | |
367 | current_index = 0; | |
368 | current_detail->nextcheck = get_seconds()+30*60; | |
369 | } | |
370 | } | |
371 | ||
372 | /* find a non-empty bucket in the table */ | |
373 | while (current_detail && | |
374 | current_index < current_detail->hash_size && | |
375 | current_detail->hash_table[current_index] == NULL) | |
376 | current_index++; | |
377 | ||
378 | /* find a cleanable entry in the bucket and clean it, or set to next bucket */ | |
cca5172a | 379 | |
1da177e4 LT |
380 | if (current_detail && current_index < current_detail->hash_size) { |
381 | struct cache_head *ch, **cp; | |
382 | struct cache_detail *d; | |
cca5172a | 383 | |
1da177e4 LT |
384 | write_lock(¤t_detail->hash_lock); |
385 | ||
386 | /* Ok, now to clean this strand */ | |
cca5172a | 387 | |
1da177e4 LT |
388 | cp = & current_detail->hash_table[current_index]; |
389 | ch = *cp; | |
390 | for (; ch; cp= & ch->next, ch= *cp) { | |
391 | if (current_detail->nextcheck > ch->expiry_time) | |
392 | current_detail->nextcheck = ch->expiry_time+1; | |
393 | if (ch->expiry_time >= get_seconds() | |
394 | && ch->last_refresh >= current_detail->flush_time | |
395 | ) | |
396 | continue; | |
397 | if (test_and_clear_bit(CACHE_PENDING, &ch->flags)) | |
398 | queue_loose(current_detail, ch); | |
399 | ||
baab935f | 400 | if (atomic_read(&ch->ref.refcount) == 1) |
1da177e4 LT |
401 | break; |
402 | } | |
403 | if (ch) { | |
404 | *cp = ch->next; | |
405 | ch->next = NULL; | |
406 | current_detail->entries--; | |
407 | rv = 1; | |
408 | } | |
409 | write_unlock(¤t_detail->hash_lock); | |
410 | d = current_detail; | |
411 | if (!ch) | |
412 | current_index ++; | |
413 | spin_unlock(&cache_list_lock); | |
414 | if (ch) | |
baab935f | 415 | cache_put(ch, d); |
1da177e4 LT |
416 | } else |
417 | spin_unlock(&cache_list_lock); | |
418 | ||
419 | return rv; | |
420 | } | |
421 | ||
422 | /* | |
423 | * We want to regularly clean the cache, so we need to schedule some work ... | |
424 | */ | |
65f27f38 | 425 | static void do_cache_clean(struct work_struct *work) |
1da177e4 LT |
426 | { |
427 | int delay = 5; | |
428 | if (cache_clean() == -1) | |
6aad89c8 | 429 | delay = round_jiffies_relative(30*HZ); |
1da177e4 LT |
430 | |
431 | if (list_empty(&cache_list)) | |
432 | delay = 0; | |
433 | ||
434 | if (delay) | |
435 | schedule_delayed_work(&cache_cleaner, delay); | |
436 | } | |
437 | ||
438 | ||
cca5172a | 439 | /* |
1da177e4 | 440 | * Clean all caches promptly. This just calls cache_clean |
cca5172a | 441 | * repeatedly until we are sure that every cache has had a chance to |
1da177e4 LT |
442 | * be fully cleaned |
443 | */ | |
444 | void cache_flush(void) | |
445 | { | |
446 | while (cache_clean() != -1) | |
447 | cond_resched(); | |
448 | while (cache_clean() != -1) | |
449 | cond_resched(); | |
450 | } | |
24c3767e | 451 | EXPORT_SYMBOL_GPL(cache_flush); |
1da177e4 LT |
452 | |
453 | void cache_purge(struct cache_detail *detail) | |
454 | { | |
455 | detail->flush_time = LONG_MAX; | |
456 | detail->nextcheck = get_seconds(); | |
457 | cache_flush(); | |
458 | detail->flush_time = 1; | |
459 | } | |
24c3767e | 460 | EXPORT_SYMBOL_GPL(cache_purge); |
1da177e4 LT |
461 | |
462 | ||
463 | /* | |
464 | * Deferral and Revisiting of Requests. | |
465 | * | |
466 | * If a cache lookup finds a pending entry, we | |
467 | * need to defer the request and revisit it later. | |
468 | * All deferred requests are stored in a hash table, | |
469 | * indexed by "struct cache_head *". | |
470 | * As it may be wasteful to store a whole request | |
cca5172a | 471 | * structure, we allow the request to provide a |
1da177e4 LT |
472 | * deferred form, which must contain a |
473 | * 'struct cache_deferred_req' | |
474 | * This cache_deferred_req contains a method to allow | |
475 | * it to be revisited when cache info is available | |
476 | */ | |
477 | ||
478 | #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head)) | |
479 | #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE) | |
480 | ||
481 | #define DFR_MAX 300 /* ??? */ | |
482 | ||
483 | static DEFINE_SPINLOCK(cache_defer_lock); | |
484 | static LIST_HEAD(cache_defer_list); | |
485 | static struct list_head cache_defer_hash[DFR_HASHSIZE]; | |
486 | static int cache_defer_cnt; | |
487 | ||
e0bb89ef | 488 | static int cache_defer_req(struct cache_req *req, struct cache_head *item) |
1da177e4 LT |
489 | { |
490 | struct cache_deferred_req *dreq; | |
491 | int hash = DFR_HASH(item); | |
492 | ||
01f3bd1f BF |
493 | if (cache_defer_cnt >= DFR_MAX) { |
494 | /* too much in the cache, randomly drop this one, | |
495 | * or continue and drop the oldest below | |
496 | */ | |
497 | if (net_random()&1) | |
498 | return -ETIMEDOUT; | |
499 | } | |
1da177e4 LT |
500 | dreq = req->defer(req); |
501 | if (dreq == NULL) | |
e0bb89ef | 502 | return -ETIMEDOUT; |
1da177e4 LT |
503 | |
504 | dreq->item = item; | |
1da177e4 LT |
505 | |
506 | spin_lock(&cache_defer_lock); | |
507 | ||
508 | list_add(&dreq->recent, &cache_defer_list); | |
509 | ||
510 | if (cache_defer_hash[hash].next == NULL) | |
511 | INIT_LIST_HEAD(&cache_defer_hash[hash]); | |
512 | list_add(&dreq->hash, &cache_defer_hash[hash]); | |
513 | ||
514 | /* it is in, now maybe clean up */ | |
515 | dreq = NULL; | |
516 | if (++cache_defer_cnt > DFR_MAX) { | |
01f3bd1f BF |
517 | dreq = list_entry(cache_defer_list.prev, |
518 | struct cache_deferred_req, recent); | |
1da177e4 LT |
519 | list_del(&dreq->recent); |
520 | list_del(&dreq->hash); | |
521 | cache_defer_cnt--; | |
522 | } | |
523 | spin_unlock(&cache_defer_lock); | |
524 | ||
525 | if (dreq) { | |
526 | /* there was one too many */ | |
527 | dreq->revisit(dreq, 1); | |
528 | } | |
4013edea | 529 | if (!test_bit(CACHE_PENDING, &item->flags)) { |
1da177e4 LT |
530 | /* must have just been validated... */ |
531 | cache_revisit_request(item); | |
532 | } | |
e0bb89ef | 533 | return 0; |
1da177e4 LT |
534 | } |
535 | ||
536 | static void cache_revisit_request(struct cache_head *item) | |
537 | { | |
538 | struct cache_deferred_req *dreq; | |
539 | struct list_head pending; | |
540 | ||
541 | struct list_head *lp; | |
542 | int hash = DFR_HASH(item); | |
543 | ||
544 | INIT_LIST_HEAD(&pending); | |
545 | spin_lock(&cache_defer_lock); | |
cca5172a | 546 | |
1da177e4 LT |
547 | lp = cache_defer_hash[hash].next; |
548 | if (lp) { | |
549 | while (lp != &cache_defer_hash[hash]) { | |
550 | dreq = list_entry(lp, struct cache_deferred_req, hash); | |
551 | lp = lp->next; | |
552 | if (dreq->item == item) { | |
553 | list_del(&dreq->hash); | |
554 | list_move(&dreq->recent, &pending); | |
555 | cache_defer_cnt--; | |
556 | } | |
557 | } | |
558 | } | |
559 | spin_unlock(&cache_defer_lock); | |
560 | ||
561 | while (!list_empty(&pending)) { | |
562 | dreq = list_entry(pending.next, struct cache_deferred_req, recent); | |
563 | list_del_init(&dreq->recent); | |
564 | dreq->revisit(dreq, 0); | |
565 | } | |
566 | } | |
567 | ||
568 | void cache_clean_deferred(void *owner) | |
569 | { | |
570 | struct cache_deferred_req *dreq, *tmp; | |
571 | struct list_head pending; | |
572 | ||
573 | ||
574 | INIT_LIST_HEAD(&pending); | |
575 | spin_lock(&cache_defer_lock); | |
cca5172a | 576 | |
1da177e4 LT |
577 | list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) { |
578 | if (dreq->owner == owner) { | |
579 | list_del(&dreq->hash); | |
580 | list_move(&dreq->recent, &pending); | |
581 | cache_defer_cnt--; | |
582 | } | |
583 | } | |
584 | spin_unlock(&cache_defer_lock); | |
585 | ||
586 | while (!list_empty(&pending)) { | |
587 | dreq = list_entry(pending.next, struct cache_deferred_req, recent); | |
588 | list_del_init(&dreq->recent); | |
589 | dreq->revisit(dreq, 1); | |
590 | } | |
591 | } | |
592 | ||
593 | /* | |
594 | * communicate with user-space | |
595 | * | |
a490c681 BF |
596 | * We have a magic /proc file - /proc/sunrpc/<cachename>/channel. |
597 | * On read, you get a full request, or block. | |
598 | * On write, an update request is processed. | |
599 | * Poll works if anything to read, and always allows write. | |
1da177e4 | 600 | * |
cca5172a | 601 | * Implemented by linked list of requests. Each open file has |
a490c681 | 602 | * a ->private that also exists in this list. New requests are added |
1da177e4 LT |
603 | * to the end and may wakeup and preceding readers. |
604 | * New readers are added to the head. If, on read, an item is found with | |
605 | * CACHE_UPCALLING clear, we free it from the list. | |
606 | * | |
607 | */ | |
608 | ||
609 | static DEFINE_SPINLOCK(queue_lock); | |
4a3e2f71 | 610 | static DEFINE_MUTEX(queue_io_mutex); |
1da177e4 LT |
611 | |
612 | struct cache_queue { | |
613 | struct list_head list; | |
614 | int reader; /* if 0, then request */ | |
615 | }; | |
616 | struct cache_request { | |
617 | struct cache_queue q; | |
618 | struct cache_head *item; | |
619 | char * buf; | |
620 | int len; | |
621 | int readers; | |
622 | }; | |
623 | struct cache_reader { | |
624 | struct cache_queue q; | |
625 | int offset; /* if non-0, we have a refcnt on next request */ | |
626 | }; | |
627 | ||
173912a6 TM |
628 | static ssize_t cache_read(struct file *filp, char __user *buf, size_t count, |
629 | loff_t *ppos, struct cache_detail *cd) | |
1da177e4 LT |
630 | { |
631 | struct cache_reader *rp = filp->private_data; | |
632 | struct cache_request *rq; | |
da77005f | 633 | struct inode *inode = filp->f_path.dentry->d_inode; |
1da177e4 LT |
634 | int err; |
635 | ||
636 | if (count == 0) | |
637 | return 0; | |
638 | ||
da77005f | 639 | mutex_lock(&inode->i_mutex); /* protect against multiple concurrent |
1da177e4 LT |
640 | * readers on this file */ |
641 | again: | |
642 | spin_lock(&queue_lock); | |
643 | /* need to find next request */ | |
644 | while (rp->q.list.next != &cd->queue && | |
645 | list_entry(rp->q.list.next, struct cache_queue, list) | |
646 | ->reader) { | |
647 | struct list_head *next = rp->q.list.next; | |
648 | list_move(&rp->q.list, next); | |
649 | } | |
650 | if (rp->q.list.next == &cd->queue) { | |
651 | spin_unlock(&queue_lock); | |
da77005f | 652 | mutex_unlock(&inode->i_mutex); |
09a62660 | 653 | BUG_ON(rp->offset); |
1da177e4 LT |
654 | return 0; |
655 | } | |
656 | rq = container_of(rp->q.list.next, struct cache_request, q.list); | |
09a62660 | 657 | BUG_ON(rq->q.reader); |
1da177e4 LT |
658 | if (rp->offset == 0) |
659 | rq->readers++; | |
660 | spin_unlock(&queue_lock); | |
661 | ||
662 | if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) { | |
663 | err = -EAGAIN; | |
664 | spin_lock(&queue_lock); | |
665 | list_move(&rp->q.list, &rq->q.list); | |
666 | spin_unlock(&queue_lock); | |
667 | } else { | |
668 | if (rp->offset + count > rq->len) | |
669 | count = rq->len - rp->offset; | |
670 | err = -EFAULT; | |
671 | if (copy_to_user(buf, rq->buf + rp->offset, count)) | |
672 | goto out; | |
673 | rp->offset += count; | |
674 | if (rp->offset >= rq->len) { | |
675 | rp->offset = 0; | |
676 | spin_lock(&queue_lock); | |
677 | list_move(&rp->q.list, &rq->q.list); | |
678 | spin_unlock(&queue_lock); | |
679 | } | |
680 | err = 0; | |
681 | } | |
682 | out: | |
683 | if (rp->offset == 0) { | |
684 | /* need to release rq */ | |
685 | spin_lock(&queue_lock); | |
686 | rq->readers--; | |
687 | if (rq->readers == 0 && | |
688 | !test_bit(CACHE_PENDING, &rq->item->flags)) { | |
689 | list_del(&rq->q.list); | |
690 | spin_unlock(&queue_lock); | |
baab935f | 691 | cache_put(rq->item, cd); |
1da177e4 LT |
692 | kfree(rq->buf); |
693 | kfree(rq); | |
694 | } else | |
695 | spin_unlock(&queue_lock); | |
696 | } | |
697 | if (err == -EAGAIN) | |
698 | goto again; | |
da77005f | 699 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
700 | return err ? err : count; |
701 | } | |
702 | ||
da77005f TM |
703 | static ssize_t cache_do_downcall(char *kaddr, const char __user *buf, |
704 | size_t count, struct cache_detail *cd) | |
705 | { | |
706 | ssize_t ret; | |
1da177e4 | 707 | |
da77005f TM |
708 | if (copy_from_user(kaddr, buf, count)) |
709 | return -EFAULT; | |
710 | kaddr[count] = '\0'; | |
711 | ret = cd->cache_parse(cd, kaddr, count); | |
712 | if (!ret) | |
713 | ret = count; | |
714 | return ret; | |
715 | } | |
716 | ||
717 | static ssize_t cache_slow_downcall(const char __user *buf, | |
718 | size_t count, struct cache_detail *cd) | |
1da177e4 | 719 | { |
da77005f TM |
720 | static char write_buf[8192]; /* protected by queue_io_mutex */ |
721 | ssize_t ret = -EINVAL; | |
1da177e4 | 722 | |
1da177e4 | 723 | if (count >= sizeof(write_buf)) |
da77005f | 724 | goto out; |
4a3e2f71 | 725 | mutex_lock(&queue_io_mutex); |
da77005f TM |
726 | ret = cache_do_downcall(write_buf, buf, count, cd); |
727 | mutex_unlock(&queue_io_mutex); | |
728 | out: | |
729 | return ret; | |
730 | } | |
1da177e4 | 731 | |
da77005f TM |
732 | static ssize_t cache_downcall(struct address_space *mapping, |
733 | const char __user *buf, | |
734 | size_t count, struct cache_detail *cd) | |
735 | { | |
736 | struct page *page; | |
737 | char *kaddr; | |
738 | ssize_t ret = -ENOMEM; | |
739 | ||
740 | if (count >= PAGE_CACHE_SIZE) | |
741 | goto out_slow; | |
742 | ||
743 | page = find_or_create_page(mapping, 0, GFP_KERNEL); | |
744 | if (!page) | |
745 | goto out_slow; | |
746 | ||
747 | kaddr = kmap(page); | |
748 | ret = cache_do_downcall(kaddr, buf, count, cd); | |
749 | kunmap(page); | |
750 | unlock_page(page); | |
751 | page_cache_release(page); | |
752 | return ret; | |
753 | out_slow: | |
754 | return cache_slow_downcall(buf, count, cd); | |
755 | } | |
1da177e4 | 756 | |
173912a6 TM |
757 | static ssize_t cache_write(struct file *filp, const char __user *buf, |
758 | size_t count, loff_t *ppos, | |
759 | struct cache_detail *cd) | |
da77005f TM |
760 | { |
761 | struct address_space *mapping = filp->f_mapping; | |
762 | struct inode *inode = filp->f_path.dentry->d_inode; | |
da77005f TM |
763 | ssize_t ret = -EINVAL; |
764 | ||
765 | if (!cd->cache_parse) | |
766 | goto out; | |
767 | ||
768 | mutex_lock(&inode->i_mutex); | |
769 | ret = cache_downcall(mapping, buf, count, cd); | |
770 | mutex_unlock(&inode->i_mutex); | |
771 | out: | |
772 | return ret; | |
1da177e4 LT |
773 | } |
774 | ||
775 | static DECLARE_WAIT_QUEUE_HEAD(queue_wait); | |
776 | ||
173912a6 TM |
777 | static unsigned int cache_poll(struct file *filp, poll_table *wait, |
778 | struct cache_detail *cd) | |
1da177e4 LT |
779 | { |
780 | unsigned int mask; | |
781 | struct cache_reader *rp = filp->private_data; | |
782 | struct cache_queue *cq; | |
1da177e4 LT |
783 | |
784 | poll_wait(filp, &queue_wait, wait); | |
785 | ||
786 | /* alway allow write */ | |
787 | mask = POLL_OUT | POLLWRNORM; | |
788 | ||
789 | if (!rp) | |
790 | return mask; | |
791 | ||
792 | spin_lock(&queue_lock); | |
793 | ||
794 | for (cq= &rp->q; &cq->list != &cd->queue; | |
795 | cq = list_entry(cq->list.next, struct cache_queue, list)) | |
796 | if (!cq->reader) { | |
797 | mask |= POLLIN | POLLRDNORM; | |
798 | break; | |
799 | } | |
800 | spin_unlock(&queue_lock); | |
801 | return mask; | |
802 | } | |
803 | ||
173912a6 TM |
804 | static int cache_ioctl(struct inode *ino, struct file *filp, |
805 | unsigned int cmd, unsigned long arg, | |
806 | struct cache_detail *cd) | |
1da177e4 LT |
807 | { |
808 | int len = 0; | |
809 | struct cache_reader *rp = filp->private_data; | |
810 | struct cache_queue *cq; | |
1da177e4 LT |
811 | |
812 | if (cmd != FIONREAD || !rp) | |
813 | return -EINVAL; | |
814 | ||
815 | spin_lock(&queue_lock); | |
816 | ||
817 | /* only find the length remaining in current request, | |
818 | * or the length of the next request | |
819 | */ | |
820 | for (cq= &rp->q; &cq->list != &cd->queue; | |
821 | cq = list_entry(cq->list.next, struct cache_queue, list)) | |
822 | if (!cq->reader) { | |
823 | struct cache_request *cr = | |
824 | container_of(cq, struct cache_request, q); | |
825 | len = cr->len - rp->offset; | |
826 | break; | |
827 | } | |
828 | spin_unlock(&queue_lock); | |
829 | ||
830 | return put_user(len, (int __user *)arg); | |
831 | } | |
832 | ||
173912a6 TM |
833 | static int cache_open(struct inode *inode, struct file *filp, |
834 | struct cache_detail *cd) | |
1da177e4 LT |
835 | { |
836 | struct cache_reader *rp = NULL; | |
837 | ||
838 | nonseekable_open(inode, filp); | |
839 | if (filp->f_mode & FMODE_READ) { | |
1da177e4 LT |
840 | rp = kmalloc(sizeof(*rp), GFP_KERNEL); |
841 | if (!rp) | |
842 | return -ENOMEM; | |
843 | rp->offset = 0; | |
844 | rp->q.reader = 1; | |
845 | atomic_inc(&cd->readers); | |
846 | spin_lock(&queue_lock); | |
847 | list_add(&rp->q.list, &cd->queue); | |
848 | spin_unlock(&queue_lock); | |
849 | } | |
850 | filp->private_data = rp; | |
851 | return 0; | |
852 | } | |
853 | ||
173912a6 TM |
854 | static int cache_release(struct inode *inode, struct file *filp, |
855 | struct cache_detail *cd) | |
1da177e4 LT |
856 | { |
857 | struct cache_reader *rp = filp->private_data; | |
1da177e4 LT |
858 | |
859 | if (rp) { | |
860 | spin_lock(&queue_lock); | |
861 | if (rp->offset) { | |
862 | struct cache_queue *cq; | |
863 | for (cq= &rp->q; &cq->list != &cd->queue; | |
864 | cq = list_entry(cq->list.next, struct cache_queue, list)) | |
865 | if (!cq->reader) { | |
866 | container_of(cq, struct cache_request, q) | |
867 | ->readers--; | |
868 | break; | |
869 | } | |
870 | rp->offset = 0; | |
871 | } | |
872 | list_del(&rp->q.list); | |
873 | spin_unlock(&queue_lock); | |
874 | ||
875 | filp->private_data = NULL; | |
876 | kfree(rp); | |
877 | ||
878 | cd->last_close = get_seconds(); | |
879 | atomic_dec(&cd->readers); | |
880 | } | |
881 | return 0; | |
882 | } | |
883 | ||
884 | ||
885 | ||
1da177e4 LT |
886 | static void queue_loose(struct cache_detail *detail, struct cache_head *ch) |
887 | { | |
888 | struct cache_queue *cq; | |
889 | spin_lock(&queue_lock); | |
890 | list_for_each_entry(cq, &detail->queue, list) | |
891 | if (!cq->reader) { | |
892 | struct cache_request *cr = container_of(cq, struct cache_request, q); | |
893 | if (cr->item != ch) | |
894 | continue; | |
895 | if (cr->readers != 0) | |
4013edea | 896 | continue; |
1da177e4 LT |
897 | list_del(&cr->q.list); |
898 | spin_unlock(&queue_lock); | |
baab935f | 899 | cache_put(cr->item, detail); |
1da177e4 LT |
900 | kfree(cr->buf); |
901 | kfree(cr); | |
902 | return; | |
903 | } | |
904 | spin_unlock(&queue_lock); | |
905 | } | |
906 | ||
907 | /* | |
908 | * Support routines for text-based upcalls. | |
909 | * Fields are separated by spaces. | |
910 | * Fields are either mangled to quote space tab newline slosh with slosh | |
911 | * or a hexified with a leading \x | |
912 | * Record is terminated with newline. | |
913 | * | |
914 | */ | |
915 | ||
916 | void qword_add(char **bpp, int *lp, char *str) | |
917 | { | |
918 | char *bp = *bpp; | |
919 | int len = *lp; | |
920 | char c; | |
921 | ||
922 | if (len < 0) return; | |
923 | ||
924 | while ((c=*str++) && len) | |
925 | switch(c) { | |
926 | case ' ': | |
927 | case '\t': | |
928 | case '\n': | |
929 | case '\\': | |
930 | if (len >= 4) { | |
931 | *bp++ = '\\'; | |
932 | *bp++ = '0' + ((c & 0300)>>6); | |
933 | *bp++ = '0' + ((c & 0070)>>3); | |
934 | *bp++ = '0' + ((c & 0007)>>0); | |
935 | } | |
936 | len -= 4; | |
937 | break; | |
938 | default: | |
939 | *bp++ = c; | |
940 | len--; | |
941 | } | |
942 | if (c || len <1) len = -1; | |
943 | else { | |
944 | *bp++ = ' '; | |
945 | len--; | |
946 | } | |
947 | *bpp = bp; | |
948 | *lp = len; | |
949 | } | |
24c3767e | 950 | EXPORT_SYMBOL_GPL(qword_add); |
1da177e4 LT |
951 | |
952 | void qword_addhex(char **bpp, int *lp, char *buf, int blen) | |
953 | { | |
954 | char *bp = *bpp; | |
955 | int len = *lp; | |
956 | ||
957 | if (len < 0) return; | |
958 | ||
959 | if (len > 2) { | |
960 | *bp++ = '\\'; | |
961 | *bp++ = 'x'; | |
962 | len -= 2; | |
963 | while (blen && len >= 2) { | |
964 | unsigned char c = *buf++; | |
965 | *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1); | |
966 | *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1); | |
967 | len -= 2; | |
968 | blen--; | |
969 | } | |
970 | } | |
971 | if (blen || len<1) len = -1; | |
972 | else { | |
973 | *bp++ = ' '; | |
974 | len--; | |
975 | } | |
976 | *bpp = bp; | |
977 | *lp = len; | |
978 | } | |
24c3767e | 979 | EXPORT_SYMBOL_GPL(qword_addhex); |
1da177e4 LT |
980 | |
981 | static void warn_no_listener(struct cache_detail *detail) | |
982 | { | |
983 | if (detail->last_warn != detail->last_close) { | |
984 | detail->last_warn = detail->last_close; | |
985 | if (detail->warn_no_listener) | |
2da8ca26 | 986 | detail->warn_no_listener(detail, detail->last_close != 0); |
1da177e4 LT |
987 | } |
988 | } | |
989 | ||
990 | /* | |
bc74b4f5 TM |
991 | * register an upcall request to user-space and queue it up for read() by the |
992 | * upcall daemon. | |
993 | * | |
1da177e4 LT |
994 | * Each request is at most one page long. |
995 | */ | |
bc74b4f5 TM |
996 | int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h, |
997 | void (*cache_request)(struct cache_detail *, | |
998 | struct cache_head *, | |
999 | char **, | |
1000 | int *)) | |
1da177e4 LT |
1001 | { |
1002 | ||
1003 | char *buf; | |
1004 | struct cache_request *crq; | |
1005 | char *bp; | |
1006 | int len; | |
1007 | ||
1da177e4 LT |
1008 | if (atomic_read(&detail->readers) == 0 && |
1009 | detail->last_close < get_seconds() - 30) { | |
1010 | warn_no_listener(detail); | |
1011 | return -EINVAL; | |
1012 | } | |
1013 | ||
1014 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
1015 | if (!buf) | |
1016 | return -EAGAIN; | |
1017 | ||
1018 | crq = kmalloc(sizeof (*crq), GFP_KERNEL); | |
1019 | if (!crq) { | |
1020 | kfree(buf); | |
1021 | return -EAGAIN; | |
1022 | } | |
1023 | ||
1024 | bp = buf; len = PAGE_SIZE; | |
1025 | ||
bc74b4f5 | 1026 | cache_request(detail, h, &bp, &len); |
1da177e4 LT |
1027 | |
1028 | if (len < 0) { | |
1029 | kfree(buf); | |
1030 | kfree(crq); | |
1031 | return -EAGAIN; | |
1032 | } | |
1033 | crq->q.reader = 0; | |
1034 | crq->item = cache_get(h); | |
1035 | crq->buf = buf; | |
1036 | crq->len = PAGE_SIZE - len; | |
1037 | crq->readers = 0; | |
1038 | spin_lock(&queue_lock); | |
1039 | list_add_tail(&crq->q.list, &detail->queue); | |
1040 | spin_unlock(&queue_lock); | |
1041 | wake_up(&queue_wait); | |
1042 | return 0; | |
1043 | } | |
bc74b4f5 | 1044 | EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall); |
1da177e4 LT |
1045 | |
1046 | /* | |
1047 | * parse a message from user-space and pass it | |
1048 | * to an appropriate cache | |
1049 | * Messages are, like requests, separated into fields by | |
1050 | * spaces and dequotes as \xHEXSTRING or embedded \nnn octal | |
1051 | * | |
cca5172a | 1052 | * Message is |
1da177e4 LT |
1053 | * reply cachename expiry key ... content.... |
1054 | * | |
cca5172a | 1055 | * key and content are both parsed by cache |
1da177e4 LT |
1056 | */ |
1057 | ||
1058 | #define isodigit(c) (isdigit(c) && c <= '7') | |
1059 | int qword_get(char **bpp, char *dest, int bufsize) | |
1060 | { | |
1061 | /* return bytes copied, or -1 on error */ | |
1062 | char *bp = *bpp; | |
1063 | int len = 0; | |
1064 | ||
1065 | while (*bp == ' ') bp++; | |
1066 | ||
1067 | if (bp[0] == '\\' && bp[1] == 'x') { | |
1068 | /* HEX STRING */ | |
1069 | bp += 2; | |
1070 | while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) { | |
1071 | int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10; | |
1072 | bp++; | |
1073 | byte <<= 4; | |
1074 | byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10; | |
1075 | *dest++ = byte; | |
1076 | bp++; | |
1077 | len++; | |
1078 | } | |
1079 | } else { | |
1080 | /* text with \nnn octal quoting */ | |
1081 | while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) { | |
1082 | if (*bp == '\\' && | |
1083 | isodigit(bp[1]) && (bp[1] <= '3') && | |
1084 | isodigit(bp[2]) && | |
1085 | isodigit(bp[3])) { | |
1086 | int byte = (*++bp -'0'); | |
1087 | bp++; | |
1088 | byte = (byte << 3) | (*bp++ - '0'); | |
1089 | byte = (byte << 3) | (*bp++ - '0'); | |
1090 | *dest++ = byte; | |
1091 | len++; | |
1092 | } else { | |
1093 | *dest++ = *bp++; | |
1094 | len++; | |
1095 | } | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | if (*bp != ' ' && *bp != '\n' && *bp != '\0') | |
1100 | return -1; | |
1101 | while (*bp == ' ') bp++; | |
1102 | *bpp = bp; | |
1103 | *dest = '\0'; | |
1104 | return len; | |
1105 | } | |
24c3767e | 1106 | EXPORT_SYMBOL_GPL(qword_get); |
1da177e4 LT |
1107 | |
1108 | ||
1109 | /* | |
1110 | * support /proc/sunrpc/cache/$CACHENAME/content | |
1111 | * as a seqfile. | |
1112 | * We call ->cache_show passing NULL for the item to | |
1113 | * get a header, then pass each real item in the cache | |
1114 | */ | |
1115 | ||
1116 | struct handle { | |
1117 | struct cache_detail *cd; | |
1118 | }; | |
1119 | ||
1120 | static void *c_start(struct seq_file *m, loff_t *pos) | |
9a429c49 | 1121 | __acquires(cd->hash_lock) |
1da177e4 LT |
1122 | { |
1123 | loff_t n = *pos; | |
1124 | unsigned hash, entry; | |
1125 | struct cache_head *ch; | |
1126 | struct cache_detail *cd = ((struct handle*)m->private)->cd; | |
cca5172a | 1127 | |
1da177e4 LT |
1128 | |
1129 | read_lock(&cd->hash_lock); | |
1130 | if (!n--) | |
1131 | return SEQ_START_TOKEN; | |
1132 | hash = n >> 32; | |
1133 | entry = n & ((1LL<<32) - 1); | |
1134 | ||
1135 | for (ch=cd->hash_table[hash]; ch; ch=ch->next) | |
1136 | if (!entry--) | |
1137 | return ch; | |
1138 | n &= ~((1LL<<32) - 1); | |
1139 | do { | |
1140 | hash++; | |
1141 | n += 1LL<<32; | |
cca5172a | 1142 | } while(hash < cd->hash_size && |
1da177e4 LT |
1143 | cd->hash_table[hash]==NULL); |
1144 | if (hash >= cd->hash_size) | |
1145 | return NULL; | |
1146 | *pos = n+1; | |
1147 | return cd->hash_table[hash]; | |
1148 | } | |
1149 | ||
1150 | static void *c_next(struct seq_file *m, void *p, loff_t *pos) | |
1151 | { | |
1152 | struct cache_head *ch = p; | |
1153 | int hash = (*pos >> 32); | |
1154 | struct cache_detail *cd = ((struct handle*)m->private)->cd; | |
1155 | ||
1156 | if (p == SEQ_START_TOKEN) | |
1157 | hash = 0; | |
1158 | else if (ch->next == NULL) { | |
1159 | hash++; | |
1160 | *pos += 1LL<<32; | |
1161 | } else { | |
1162 | ++*pos; | |
1163 | return ch->next; | |
1164 | } | |
1165 | *pos &= ~((1LL<<32) - 1); | |
1166 | while (hash < cd->hash_size && | |
1167 | cd->hash_table[hash] == NULL) { | |
1168 | hash++; | |
1169 | *pos += 1LL<<32; | |
1170 | } | |
1171 | if (hash >= cd->hash_size) | |
1172 | return NULL; | |
1173 | ++*pos; | |
1174 | return cd->hash_table[hash]; | |
1175 | } | |
1176 | ||
1177 | static void c_stop(struct seq_file *m, void *p) | |
9a429c49 | 1178 | __releases(cd->hash_lock) |
1da177e4 LT |
1179 | { |
1180 | struct cache_detail *cd = ((struct handle*)m->private)->cd; | |
1181 | read_unlock(&cd->hash_lock); | |
1182 | } | |
1183 | ||
1184 | static int c_show(struct seq_file *m, void *p) | |
1185 | { | |
1186 | struct cache_head *cp = p; | |
1187 | struct cache_detail *cd = ((struct handle*)m->private)->cd; | |
1188 | ||
1189 | if (p == SEQ_START_TOKEN) | |
1190 | return cd->cache_show(m, cd, NULL); | |
1191 | ||
1192 | ifdebug(CACHE) | |
4013edea | 1193 | seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n", |
baab935f | 1194 | cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags); |
1da177e4 LT |
1195 | cache_get(cp); |
1196 | if (cache_check(cd, cp, NULL)) | |
1197 | /* cache_check does a cache_put on failure */ | |
1198 | seq_printf(m, "# "); | |
1199 | else | |
1200 | cache_put(cp, cd); | |
1201 | ||
1202 | return cd->cache_show(m, cd, cp); | |
1203 | } | |
1204 | ||
56b3d975 | 1205 | static const struct seq_operations cache_content_op = { |
1da177e4 LT |
1206 | .start = c_start, |
1207 | .next = c_next, | |
1208 | .stop = c_stop, | |
1209 | .show = c_show, | |
1210 | }; | |
1211 | ||
173912a6 TM |
1212 | static int content_open(struct inode *inode, struct file *file, |
1213 | struct cache_detail *cd) | |
1da177e4 | 1214 | { |
1da177e4 | 1215 | struct handle *han; |
1da177e4 | 1216 | |
ec931035 | 1217 | han = __seq_open_private(file, &cache_content_op, sizeof(*han)); |
1da177e4 LT |
1218 | if (han == NULL) |
1219 | return -ENOMEM; | |
1220 | ||
1221 | han->cd = cd; | |
ec931035 | 1222 | return 0; |
1da177e4 | 1223 | } |
1da177e4 | 1224 | |
1da177e4 | 1225 | static ssize_t read_flush(struct file *file, char __user *buf, |
173912a6 TM |
1226 | size_t count, loff_t *ppos, |
1227 | struct cache_detail *cd) | |
1da177e4 | 1228 | { |
1da177e4 LT |
1229 | char tbuf[20]; |
1230 | unsigned long p = *ppos; | |
01b2969a | 1231 | size_t len; |
1da177e4 LT |
1232 | |
1233 | sprintf(tbuf, "%lu\n", cd->flush_time); | |
1234 | len = strlen(tbuf); | |
1235 | if (p >= len) | |
1236 | return 0; | |
1237 | len -= p; | |
01b2969a CL |
1238 | if (len > count) |
1239 | len = count; | |
1da177e4 | 1240 | if (copy_to_user(buf, (void*)(tbuf+p), len)) |
01b2969a CL |
1241 | return -EFAULT; |
1242 | *ppos += len; | |
1da177e4 LT |
1243 | return len; |
1244 | } | |
1245 | ||
173912a6 TM |
1246 | static ssize_t write_flush(struct file *file, const char __user *buf, |
1247 | size_t count, loff_t *ppos, | |
1248 | struct cache_detail *cd) | |
1da177e4 | 1249 | { |
1da177e4 LT |
1250 | char tbuf[20]; |
1251 | char *ep; | |
1252 | long flushtime; | |
1253 | if (*ppos || count > sizeof(tbuf)-1) | |
1254 | return -EINVAL; | |
1255 | if (copy_from_user(tbuf, buf, count)) | |
1256 | return -EFAULT; | |
1257 | tbuf[count] = 0; | |
1258 | flushtime = simple_strtoul(tbuf, &ep, 0); | |
1259 | if (*ep && *ep != '\n') | |
1260 | return -EINVAL; | |
1261 | ||
1262 | cd->flush_time = flushtime; | |
1263 | cd->nextcheck = get_seconds(); | |
1264 | cache_flush(); | |
1265 | ||
1266 | *ppos += count; | |
1267 | return count; | |
1268 | } | |
1269 | ||
173912a6 TM |
1270 | static ssize_t cache_read_procfs(struct file *filp, char __user *buf, |
1271 | size_t count, loff_t *ppos) | |
1272 | { | |
1273 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | |
1274 | ||
1275 | return cache_read(filp, buf, count, ppos, cd); | |
1276 | } | |
1277 | ||
1278 | static ssize_t cache_write_procfs(struct file *filp, const char __user *buf, | |
1279 | size_t count, loff_t *ppos) | |
1280 | { | |
1281 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | |
1282 | ||
1283 | return cache_write(filp, buf, count, ppos, cd); | |
1284 | } | |
1285 | ||
1286 | static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait) | |
1287 | { | |
1288 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | |
1289 | ||
1290 | return cache_poll(filp, wait, cd); | |
1291 | } | |
1292 | ||
1293 | static int cache_ioctl_procfs(struct inode *inode, struct file *filp, | |
1294 | unsigned int cmd, unsigned long arg) | |
1295 | { | |
1296 | struct cache_detail *cd = PDE(inode)->data; | |
1297 | ||
1298 | return cache_ioctl(inode, filp, cmd, arg, cd); | |
1299 | } | |
1300 | ||
1301 | static int cache_open_procfs(struct inode *inode, struct file *filp) | |
1302 | { | |
1303 | struct cache_detail *cd = PDE(inode)->data; | |
1304 | ||
1305 | return cache_open(inode, filp, cd); | |
1306 | } | |
1307 | ||
1308 | static int cache_release_procfs(struct inode *inode, struct file *filp) | |
1309 | { | |
1310 | struct cache_detail *cd = PDE(inode)->data; | |
1311 | ||
1312 | return cache_release(inode, filp, cd); | |
1313 | } | |
1314 | ||
1315 | static const struct file_operations cache_file_operations_procfs = { | |
1316 | .owner = THIS_MODULE, | |
1317 | .llseek = no_llseek, | |
1318 | .read = cache_read_procfs, | |
1319 | .write = cache_write_procfs, | |
1320 | .poll = cache_poll_procfs, | |
1321 | .ioctl = cache_ioctl_procfs, /* for FIONREAD */ | |
1322 | .open = cache_open_procfs, | |
1323 | .release = cache_release_procfs, | |
1324 | }; | |
1325 | ||
1326 | static int content_open_procfs(struct inode *inode, struct file *filp) | |
1327 | { | |
1328 | struct cache_detail *cd = PDE(inode)->data; | |
1329 | ||
1330 | return content_open(inode, filp, cd); | |
1331 | } | |
1332 | ||
1333 | static const struct file_operations content_file_operations_procfs = { | |
1334 | .open = content_open_procfs, | |
1335 | .read = seq_read, | |
1336 | .llseek = seq_lseek, | |
1337 | .release = seq_release_private, | |
1338 | }; | |
1339 | ||
1340 | static ssize_t read_flush_procfs(struct file *filp, char __user *buf, | |
1341 | size_t count, loff_t *ppos) | |
1342 | { | |
1343 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | |
1344 | ||
1345 | return read_flush(filp, buf, count, ppos, cd); | |
1346 | } | |
1347 | ||
1348 | static ssize_t write_flush_procfs(struct file *filp, | |
1349 | const char __user *buf, | |
1350 | size_t count, loff_t *ppos) | |
1351 | { | |
1352 | struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data; | |
1353 | ||
1354 | return write_flush(filp, buf, count, ppos, cd); | |
1355 | } | |
1356 | ||
1357 | static const struct file_operations cache_flush_operations_procfs = { | |
1da177e4 | 1358 | .open = nonseekable_open, |
173912a6 TM |
1359 | .read = read_flush_procfs, |
1360 | .write = write_flush_procfs, | |
1da177e4 | 1361 | }; |
173912a6 TM |
1362 | |
1363 | static void remove_cache_proc_entries(struct cache_detail *cd) | |
1364 | { | |
1365 | if (cd->u.procfs.proc_ent == NULL) | |
1366 | return; | |
1367 | if (cd->u.procfs.flush_ent) | |
1368 | remove_proc_entry("flush", cd->u.procfs.proc_ent); | |
1369 | if (cd->u.procfs.channel_ent) | |
1370 | remove_proc_entry("channel", cd->u.procfs.proc_ent); | |
1371 | if (cd->u.procfs.content_ent) | |
1372 | remove_proc_entry("content", cd->u.procfs.proc_ent); | |
1373 | cd->u.procfs.proc_ent = NULL; | |
1374 | remove_proc_entry(cd->name, proc_net_rpc); | |
1375 | } | |
1376 | ||
1377 | #ifdef CONFIG_PROC_FS | |
1378 | static int create_cache_proc_entries(struct cache_detail *cd) | |
1379 | { | |
1380 | struct proc_dir_entry *p; | |
1381 | ||
1382 | cd->u.procfs.proc_ent = proc_mkdir(cd->name, proc_net_rpc); | |
1383 | if (cd->u.procfs.proc_ent == NULL) | |
1384 | goto out_nomem; | |
1385 | cd->u.procfs.channel_ent = NULL; | |
1386 | cd->u.procfs.content_ent = NULL; | |
1387 | ||
1388 | p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR, | |
1389 | cd->u.procfs.proc_ent, | |
1390 | &cache_flush_operations_procfs, cd); | |
1391 | cd->u.procfs.flush_ent = p; | |
1392 | if (p == NULL) | |
1393 | goto out_nomem; | |
1394 | ||
1395 | if (cd->cache_upcall || cd->cache_parse) { | |
1396 | p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR, | |
1397 | cd->u.procfs.proc_ent, | |
1398 | &cache_file_operations_procfs, cd); | |
1399 | cd->u.procfs.channel_ent = p; | |
1400 | if (p == NULL) | |
1401 | goto out_nomem; | |
1402 | } | |
1403 | if (cd->cache_show) { | |
1404 | p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR, | |
1405 | cd->u.procfs.proc_ent, | |
1406 | &content_file_operations_procfs, cd); | |
1407 | cd->u.procfs.content_ent = p; | |
1408 | if (p == NULL) | |
1409 | goto out_nomem; | |
1410 | } | |
1411 | return 0; | |
1412 | out_nomem: | |
1413 | remove_cache_proc_entries(cd); | |
1414 | return -ENOMEM; | |
1415 | } | |
1416 | #else /* CONFIG_PROC_FS */ | |
1417 | static int create_cache_proc_entries(struct cache_detail *cd) | |
1418 | { | |
1419 | return 0; | |
1420 | } | |
1421 | #endif | |
1422 | ||
1423 | int cache_register(struct cache_detail *cd) | |
1424 | { | |
1425 | int ret; | |
1426 | ||
1427 | sunrpc_init_cache_detail(cd); | |
1428 | ret = create_cache_proc_entries(cd); | |
1429 | if (ret) | |
1430 | sunrpc_destroy_cache_detail(cd); | |
1431 | return ret; | |
1432 | } | |
1433 | EXPORT_SYMBOL_GPL(cache_register); | |
1434 | ||
1435 | void cache_unregister(struct cache_detail *cd) | |
1436 | { | |
1437 | remove_cache_proc_entries(cd); | |
1438 | sunrpc_destroy_cache_detail(cd); | |
1439 | } | |
1440 | EXPORT_SYMBOL_GPL(cache_unregister); |