SUNRPC: Allow the cache_detail to specify alternative upcall mechanisms
[deliverable/linux.git] / net / sunrpc / cache.c
CommitLineData
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 38static int cache_defer_req(struct cache_req *req, struct cache_head *item);
1da177e4
LT
39static void cache_revisit_request(struct cache_head *item);
40
74cae61a 41static 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
51struct 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 102EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
15a5f6bd 103
ebd0cb1a
N
104
105static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
106
107static 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
114static 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
125struct 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 177EXPORT_SYMBOL_GPL(sunrpc_cache_update);
1da177e4 178
bc74b4f5
TM
179static 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 */
198int 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 255EXPORT_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
289static LIST_HEAD(cache_list);
290static DEFINE_SPINLOCK(cache_list_lock);
291static struct cache_detail *current_detail;
292static int current_index;
293
da7071d7
AV
294static const struct file_operations cache_file_operations;
295static const struct file_operations content_file_operations;
296static const struct file_operations cache_flush_operations;
1da177e4 297
65f27f38
DH
298static void do_cache_clean(struct work_struct *work);
299static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
1da177e4 300
ffe9386b 301static void remove_cache_proc_entries(struct cache_detail *cd)
1da177e4 302{
ffe9386b
BF
303 if (cd->proc_ent == NULL)
304 return;
305 if (cd->flush_ent)
306 remove_proc_entry("flush", cd->proc_ent);
307 if (cd->channel_ent)
308 remove_proc_entry("channel", cd->proc_ent);
309 if (cd->content_ent)
310 remove_proc_entry("content", cd->proc_ent);
311 cd->proc_ent = NULL;
312 remove_proc_entry(cd->name, proc_net_rpc);
313}
cca5172a 314
dbf847ec
BF
315#ifdef CONFIG_PROC_FS
316static int create_cache_proc_entries(struct cache_detail *cd)
ffe9386b
BF
317{
318 struct proc_dir_entry *p;
cca5172a 319
ffe9386b
BF
320 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
321 if (cd->proc_ent == NULL)
dbf847ec 322 goto out_nomem;
ffe9386b
BF
323 cd->channel_ent = cd->content_ent = NULL;
324
e7fe2336
DL
325 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
326 cd->proc_ent, &cache_flush_operations, cd);
ffe9386b
BF
327 cd->flush_ent = p;
328 if (p == NULL)
dbf847ec 329 goto out_nomem;
ffe9386b 330
bc74b4f5 331 if (cd->cache_upcall || cd->cache_parse) {
e7fe2336
DL
332 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
333 cd->proc_ent, &cache_file_operations, cd);
ffe9386b
BF
334 cd->channel_ent = p;
335 if (p == NULL)
dbf847ec 336 goto out_nomem;
1da177e4 337 }
ffe9386b 338 if (cd->cache_show) {
e7fe2336
DL
339 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
340 cd->proc_ent, &content_file_operations, cd);
ffe9386b
BF
341 cd->content_ent = p;
342 if (p == NULL)
dbf847ec 343 goto out_nomem;
ffe9386b 344 }
dbf847ec
BF
345 return 0;
346out_nomem:
347 remove_cache_proc_entries(cd);
348 return -ENOMEM;
ffe9386b 349}
dbf847ec
BF
350#else /* CONFIG_PROC_FS */
351static int create_cache_proc_entries(struct cache_detail *cd)
352{
353 return 0;
354}
355#endif
ffe9386b 356
5b7a1b9f 357static void sunrpc_init_cache_detail(struct cache_detail *cd)
ffe9386b 358{
1da177e4
LT
359 rwlock_init(&cd->hash_lock);
360 INIT_LIST_HEAD(&cd->queue);
361 spin_lock(&cache_list_lock);
362 cd->nextcheck = 0;
363 cd->entries = 0;
364 atomic_set(&cd->readers, 0);
365 cd->last_close = 0;
366 cd->last_warn = -1;
367 list_add(&cd->others, &cache_list);
368 spin_unlock(&cache_list_lock);
369
370 /* start the cleaning process */
52bad64d 371 schedule_delayed_work(&cache_cleaner, 0);
1da177e4
LT
372}
373
5b7a1b9f 374static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
1da177e4
LT
375{
376 cache_purge(cd);
377 spin_lock(&cache_list_lock);
378 write_lock(&cd->hash_lock);
379 if (cd->entries || atomic_read(&cd->inuse)) {
380 write_unlock(&cd->hash_lock);
381 spin_unlock(&cache_list_lock);
df95a9d4 382 goto out;
1da177e4
LT
383 }
384 if (current_detail == cd)
385 current_detail = NULL;
386 list_del_init(&cd->others);
387 write_unlock(&cd->hash_lock);
388 spin_unlock(&cache_list_lock);
1da177e4
LT
389 if (list_empty(&cache_list)) {
390 /* module must be being unloaded so its safe to kill the worker */
4011cd97 391 cancel_delayed_work_sync(&cache_cleaner);
1da177e4 392 }
df95a9d4
BF
393 return;
394out:
395 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
1da177e4 396}
5b7a1b9f
TM
397
398int cache_register(struct cache_detail *cd)
399{
400 int ret;
401
402 sunrpc_init_cache_detail(cd);
403 ret = create_cache_proc_entries(cd);
404 if (ret)
405 sunrpc_destroy_cache_detail(cd);
406 return ret;
407}
408EXPORT_SYMBOL_GPL(cache_register);
409
410void cache_unregister(struct cache_detail *cd)
411{
412 remove_cache_proc_entries(cd);
413 sunrpc_destroy_cache_detail(cd);
414}
24c3767e 415EXPORT_SYMBOL_GPL(cache_unregister);
1da177e4
LT
416
417/* clean cache tries to find something to clean
418 * and cleans it.
419 * It returns 1 if it cleaned something,
420 * 0 if it didn't find anything this time
421 * -1 if it fell off the end of the list.
422 */
423static int cache_clean(void)
424{
425 int rv = 0;
426 struct list_head *next;
427
428 spin_lock(&cache_list_lock);
429
430 /* find a suitable table if we don't already have one */
431 while (current_detail == NULL ||
432 current_index >= current_detail->hash_size) {
433 if (current_detail)
434 next = current_detail->others.next;
435 else
436 next = cache_list.next;
437 if (next == &cache_list) {
438 current_detail = NULL;
439 spin_unlock(&cache_list_lock);
440 return -1;
441 }
442 current_detail = list_entry(next, struct cache_detail, others);
443 if (current_detail->nextcheck > get_seconds())
444 current_index = current_detail->hash_size;
445 else {
446 current_index = 0;
447 current_detail->nextcheck = get_seconds()+30*60;
448 }
449 }
450
451 /* find a non-empty bucket in the table */
452 while (current_detail &&
453 current_index < current_detail->hash_size &&
454 current_detail->hash_table[current_index] == NULL)
455 current_index++;
456
457 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
cca5172a 458
1da177e4
LT
459 if (current_detail && current_index < current_detail->hash_size) {
460 struct cache_head *ch, **cp;
461 struct cache_detail *d;
cca5172a 462
1da177e4
LT
463 write_lock(&current_detail->hash_lock);
464
465 /* Ok, now to clean this strand */
cca5172a 466
1da177e4
LT
467 cp = & current_detail->hash_table[current_index];
468 ch = *cp;
469 for (; ch; cp= & ch->next, ch= *cp) {
470 if (current_detail->nextcheck > ch->expiry_time)
471 current_detail->nextcheck = ch->expiry_time+1;
472 if (ch->expiry_time >= get_seconds()
473 && ch->last_refresh >= current_detail->flush_time
474 )
475 continue;
476 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
477 queue_loose(current_detail, ch);
478
baab935f 479 if (atomic_read(&ch->ref.refcount) == 1)
1da177e4
LT
480 break;
481 }
482 if (ch) {
483 *cp = ch->next;
484 ch->next = NULL;
485 current_detail->entries--;
486 rv = 1;
487 }
488 write_unlock(&current_detail->hash_lock);
489 d = current_detail;
490 if (!ch)
491 current_index ++;
492 spin_unlock(&cache_list_lock);
493 if (ch)
baab935f 494 cache_put(ch, d);
1da177e4
LT
495 } else
496 spin_unlock(&cache_list_lock);
497
498 return rv;
499}
500
501/*
502 * We want to regularly clean the cache, so we need to schedule some work ...
503 */
65f27f38 504static void do_cache_clean(struct work_struct *work)
1da177e4
LT
505{
506 int delay = 5;
507 if (cache_clean() == -1)
6aad89c8 508 delay = round_jiffies_relative(30*HZ);
1da177e4
LT
509
510 if (list_empty(&cache_list))
511 delay = 0;
512
513 if (delay)
514 schedule_delayed_work(&cache_cleaner, delay);
515}
516
517
cca5172a 518/*
1da177e4 519 * Clean all caches promptly. This just calls cache_clean
cca5172a 520 * repeatedly until we are sure that every cache has had a chance to
1da177e4
LT
521 * be fully cleaned
522 */
523void cache_flush(void)
524{
525 while (cache_clean() != -1)
526 cond_resched();
527 while (cache_clean() != -1)
528 cond_resched();
529}
24c3767e 530EXPORT_SYMBOL_GPL(cache_flush);
1da177e4
LT
531
532void cache_purge(struct cache_detail *detail)
533{
534 detail->flush_time = LONG_MAX;
535 detail->nextcheck = get_seconds();
536 cache_flush();
537 detail->flush_time = 1;
538}
24c3767e 539EXPORT_SYMBOL_GPL(cache_purge);
1da177e4
LT
540
541
542/*
543 * Deferral and Revisiting of Requests.
544 *
545 * If a cache lookup finds a pending entry, we
546 * need to defer the request and revisit it later.
547 * All deferred requests are stored in a hash table,
548 * indexed by "struct cache_head *".
549 * As it may be wasteful to store a whole request
cca5172a 550 * structure, we allow the request to provide a
1da177e4
LT
551 * deferred form, which must contain a
552 * 'struct cache_deferred_req'
553 * This cache_deferred_req contains a method to allow
554 * it to be revisited when cache info is available
555 */
556
557#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
558#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
559
560#define DFR_MAX 300 /* ??? */
561
562static DEFINE_SPINLOCK(cache_defer_lock);
563static LIST_HEAD(cache_defer_list);
564static struct list_head cache_defer_hash[DFR_HASHSIZE];
565static int cache_defer_cnt;
566
e0bb89ef 567static int cache_defer_req(struct cache_req *req, struct cache_head *item)
1da177e4
LT
568{
569 struct cache_deferred_req *dreq;
570 int hash = DFR_HASH(item);
571
01f3bd1f
BF
572 if (cache_defer_cnt >= DFR_MAX) {
573 /* too much in the cache, randomly drop this one,
574 * or continue and drop the oldest below
575 */
576 if (net_random()&1)
577 return -ETIMEDOUT;
578 }
1da177e4
LT
579 dreq = req->defer(req);
580 if (dreq == NULL)
e0bb89ef 581 return -ETIMEDOUT;
1da177e4
LT
582
583 dreq->item = item;
1da177e4
LT
584
585 spin_lock(&cache_defer_lock);
586
587 list_add(&dreq->recent, &cache_defer_list);
588
589 if (cache_defer_hash[hash].next == NULL)
590 INIT_LIST_HEAD(&cache_defer_hash[hash]);
591 list_add(&dreq->hash, &cache_defer_hash[hash]);
592
593 /* it is in, now maybe clean up */
594 dreq = NULL;
595 if (++cache_defer_cnt > DFR_MAX) {
01f3bd1f
BF
596 dreq = list_entry(cache_defer_list.prev,
597 struct cache_deferred_req, recent);
1da177e4
LT
598 list_del(&dreq->recent);
599 list_del(&dreq->hash);
600 cache_defer_cnt--;
601 }
602 spin_unlock(&cache_defer_lock);
603
604 if (dreq) {
605 /* there was one too many */
606 dreq->revisit(dreq, 1);
607 }
4013edea 608 if (!test_bit(CACHE_PENDING, &item->flags)) {
1da177e4
LT
609 /* must have just been validated... */
610 cache_revisit_request(item);
611 }
e0bb89ef 612 return 0;
1da177e4
LT
613}
614
615static void cache_revisit_request(struct cache_head *item)
616{
617 struct cache_deferred_req *dreq;
618 struct list_head pending;
619
620 struct list_head *lp;
621 int hash = DFR_HASH(item);
622
623 INIT_LIST_HEAD(&pending);
624 spin_lock(&cache_defer_lock);
cca5172a 625
1da177e4
LT
626 lp = cache_defer_hash[hash].next;
627 if (lp) {
628 while (lp != &cache_defer_hash[hash]) {
629 dreq = list_entry(lp, struct cache_deferred_req, hash);
630 lp = lp->next;
631 if (dreq->item == item) {
632 list_del(&dreq->hash);
633 list_move(&dreq->recent, &pending);
634 cache_defer_cnt--;
635 }
636 }
637 }
638 spin_unlock(&cache_defer_lock);
639
640 while (!list_empty(&pending)) {
641 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
642 list_del_init(&dreq->recent);
643 dreq->revisit(dreq, 0);
644 }
645}
646
647void cache_clean_deferred(void *owner)
648{
649 struct cache_deferred_req *dreq, *tmp;
650 struct list_head pending;
651
652
653 INIT_LIST_HEAD(&pending);
654 spin_lock(&cache_defer_lock);
cca5172a 655
1da177e4
LT
656 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
657 if (dreq->owner == owner) {
658 list_del(&dreq->hash);
659 list_move(&dreq->recent, &pending);
660 cache_defer_cnt--;
661 }
662 }
663 spin_unlock(&cache_defer_lock);
664
665 while (!list_empty(&pending)) {
666 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
667 list_del_init(&dreq->recent);
668 dreq->revisit(dreq, 1);
669 }
670}
671
672/*
673 * communicate with user-space
674 *
a490c681
BF
675 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
676 * On read, you get a full request, or block.
677 * On write, an update request is processed.
678 * Poll works if anything to read, and always allows write.
1da177e4 679 *
cca5172a 680 * Implemented by linked list of requests. Each open file has
a490c681 681 * a ->private that also exists in this list. New requests are added
1da177e4
LT
682 * to the end and may wakeup and preceding readers.
683 * New readers are added to the head. If, on read, an item is found with
684 * CACHE_UPCALLING clear, we free it from the list.
685 *
686 */
687
688static DEFINE_SPINLOCK(queue_lock);
4a3e2f71 689static DEFINE_MUTEX(queue_io_mutex);
1da177e4
LT
690
691struct cache_queue {
692 struct list_head list;
693 int reader; /* if 0, then request */
694};
695struct cache_request {
696 struct cache_queue q;
697 struct cache_head *item;
698 char * buf;
699 int len;
700 int readers;
701};
702struct cache_reader {
703 struct cache_queue q;
704 int offset; /* if non-0, we have a refcnt on next request */
705};
706
707static ssize_t
708cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
709{
710 struct cache_reader *rp = filp->private_data;
711 struct cache_request *rq;
da77005f
TM
712 struct inode *inode = filp->f_path.dentry->d_inode;
713 struct cache_detail *cd = PDE(inode)->data;
1da177e4
LT
714 int err;
715
716 if (count == 0)
717 return 0;
718
da77005f 719 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
1da177e4
LT
720 * readers on this file */
721 again:
722 spin_lock(&queue_lock);
723 /* need to find next request */
724 while (rp->q.list.next != &cd->queue &&
725 list_entry(rp->q.list.next, struct cache_queue, list)
726 ->reader) {
727 struct list_head *next = rp->q.list.next;
728 list_move(&rp->q.list, next);
729 }
730 if (rp->q.list.next == &cd->queue) {
731 spin_unlock(&queue_lock);
da77005f 732 mutex_unlock(&inode->i_mutex);
09a62660 733 BUG_ON(rp->offset);
1da177e4
LT
734 return 0;
735 }
736 rq = container_of(rp->q.list.next, struct cache_request, q.list);
09a62660 737 BUG_ON(rq->q.reader);
1da177e4
LT
738 if (rp->offset == 0)
739 rq->readers++;
740 spin_unlock(&queue_lock);
741
742 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
743 err = -EAGAIN;
744 spin_lock(&queue_lock);
745 list_move(&rp->q.list, &rq->q.list);
746 spin_unlock(&queue_lock);
747 } else {
748 if (rp->offset + count > rq->len)
749 count = rq->len - rp->offset;
750 err = -EFAULT;
751 if (copy_to_user(buf, rq->buf + rp->offset, count))
752 goto out;
753 rp->offset += count;
754 if (rp->offset >= rq->len) {
755 rp->offset = 0;
756 spin_lock(&queue_lock);
757 list_move(&rp->q.list, &rq->q.list);
758 spin_unlock(&queue_lock);
759 }
760 err = 0;
761 }
762 out:
763 if (rp->offset == 0) {
764 /* need to release rq */
765 spin_lock(&queue_lock);
766 rq->readers--;
767 if (rq->readers == 0 &&
768 !test_bit(CACHE_PENDING, &rq->item->flags)) {
769 list_del(&rq->q.list);
770 spin_unlock(&queue_lock);
baab935f 771 cache_put(rq->item, cd);
1da177e4
LT
772 kfree(rq->buf);
773 kfree(rq);
774 } else
775 spin_unlock(&queue_lock);
776 }
777 if (err == -EAGAIN)
778 goto again;
da77005f 779 mutex_unlock(&inode->i_mutex);
1da177e4
LT
780 return err ? err : count;
781}
782
da77005f
TM
783static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
784 size_t count, struct cache_detail *cd)
785{
786 ssize_t ret;
1da177e4 787
da77005f
TM
788 if (copy_from_user(kaddr, buf, count))
789 return -EFAULT;
790 kaddr[count] = '\0';
791 ret = cd->cache_parse(cd, kaddr, count);
792 if (!ret)
793 ret = count;
794 return ret;
795}
796
797static ssize_t cache_slow_downcall(const char __user *buf,
798 size_t count, struct cache_detail *cd)
1da177e4 799{
da77005f
TM
800 static char write_buf[8192]; /* protected by queue_io_mutex */
801 ssize_t ret = -EINVAL;
1da177e4 802
1da177e4 803 if (count >= sizeof(write_buf))
da77005f 804 goto out;
4a3e2f71 805 mutex_lock(&queue_io_mutex);
da77005f
TM
806 ret = cache_do_downcall(write_buf, buf, count, cd);
807 mutex_unlock(&queue_io_mutex);
808out:
809 return ret;
810}
1da177e4 811
da77005f
TM
812static ssize_t cache_downcall(struct address_space *mapping,
813 const char __user *buf,
814 size_t count, struct cache_detail *cd)
815{
816 struct page *page;
817 char *kaddr;
818 ssize_t ret = -ENOMEM;
819
820 if (count >= PAGE_CACHE_SIZE)
821 goto out_slow;
822
823 page = find_or_create_page(mapping, 0, GFP_KERNEL);
824 if (!page)
825 goto out_slow;
826
827 kaddr = kmap(page);
828 ret = cache_do_downcall(kaddr, buf, count, cd);
829 kunmap(page);
830 unlock_page(page);
831 page_cache_release(page);
832 return ret;
833out_slow:
834 return cache_slow_downcall(buf, count, cd);
835}
1da177e4 836
da77005f
TM
837static ssize_t
838cache_write(struct file *filp, const char __user *buf, size_t count,
839 loff_t *ppos)
840{
841 struct address_space *mapping = filp->f_mapping;
842 struct inode *inode = filp->f_path.dentry->d_inode;
843 struct cache_detail *cd = PDE(inode)->data;
844 ssize_t ret = -EINVAL;
845
846 if (!cd->cache_parse)
847 goto out;
848
849 mutex_lock(&inode->i_mutex);
850 ret = cache_downcall(mapping, buf, count, cd);
851 mutex_unlock(&inode->i_mutex);
852out:
853 return ret;
1da177e4
LT
854}
855
856static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
857
858static unsigned int
859cache_poll(struct file *filp, poll_table *wait)
860{
861 unsigned int mask;
862 struct cache_reader *rp = filp->private_data;
863 struct cache_queue *cq;
303b46bb 864 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1da177e4
LT
865
866 poll_wait(filp, &queue_wait, wait);
867
868 /* alway allow write */
869 mask = POLL_OUT | POLLWRNORM;
870
871 if (!rp)
872 return mask;
873
874 spin_lock(&queue_lock);
875
876 for (cq= &rp->q; &cq->list != &cd->queue;
877 cq = list_entry(cq->list.next, struct cache_queue, list))
878 if (!cq->reader) {
879 mask |= POLLIN | POLLRDNORM;
880 break;
881 }
882 spin_unlock(&queue_lock);
883 return mask;
884}
885
886static int
887cache_ioctl(struct inode *ino, struct file *filp,
888 unsigned int cmd, unsigned long arg)
889{
890 int len = 0;
891 struct cache_reader *rp = filp->private_data;
892 struct cache_queue *cq;
893 struct cache_detail *cd = PDE(ino)->data;
894
895 if (cmd != FIONREAD || !rp)
896 return -EINVAL;
897
898 spin_lock(&queue_lock);
899
900 /* only find the length remaining in current request,
901 * or the length of the next request
902 */
903 for (cq= &rp->q; &cq->list != &cd->queue;
904 cq = list_entry(cq->list.next, struct cache_queue, list))
905 if (!cq->reader) {
906 struct cache_request *cr =
907 container_of(cq, struct cache_request, q);
908 len = cr->len - rp->offset;
909 break;
910 }
911 spin_unlock(&queue_lock);
912
913 return put_user(len, (int __user *)arg);
914}
915
916static int
917cache_open(struct inode *inode, struct file *filp)
918{
919 struct cache_reader *rp = NULL;
920
921 nonseekable_open(inode, filp);
922 if (filp->f_mode & FMODE_READ) {
923 struct cache_detail *cd = PDE(inode)->data;
924
925 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
926 if (!rp)
927 return -ENOMEM;
928 rp->offset = 0;
929 rp->q.reader = 1;
930 atomic_inc(&cd->readers);
931 spin_lock(&queue_lock);
932 list_add(&rp->q.list, &cd->queue);
933 spin_unlock(&queue_lock);
934 }
935 filp->private_data = rp;
936 return 0;
937}
938
939static int
940cache_release(struct inode *inode, struct file *filp)
941{
942 struct cache_reader *rp = filp->private_data;
943 struct cache_detail *cd = PDE(inode)->data;
944
945 if (rp) {
946 spin_lock(&queue_lock);
947 if (rp->offset) {
948 struct cache_queue *cq;
949 for (cq= &rp->q; &cq->list != &cd->queue;
950 cq = list_entry(cq->list.next, struct cache_queue, list))
951 if (!cq->reader) {
952 container_of(cq, struct cache_request, q)
953 ->readers--;
954 break;
955 }
956 rp->offset = 0;
957 }
958 list_del(&rp->q.list);
959 spin_unlock(&queue_lock);
960
961 filp->private_data = NULL;
962 kfree(rp);
963
964 cd->last_close = get_seconds();
965 atomic_dec(&cd->readers);
966 }
967 return 0;
968}
969
970
971
da7071d7 972static const struct file_operations cache_file_operations = {
1da177e4
LT
973 .owner = THIS_MODULE,
974 .llseek = no_llseek,
975 .read = cache_read,
976 .write = cache_write,
977 .poll = cache_poll,
978 .ioctl = cache_ioctl, /* for FIONREAD */
979 .open = cache_open,
980 .release = cache_release,
981};
982
983
984static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
985{
986 struct cache_queue *cq;
987 spin_lock(&queue_lock);
988 list_for_each_entry(cq, &detail->queue, list)
989 if (!cq->reader) {
990 struct cache_request *cr = container_of(cq, struct cache_request, q);
991 if (cr->item != ch)
992 continue;
993 if (cr->readers != 0)
4013edea 994 continue;
1da177e4
LT
995 list_del(&cr->q.list);
996 spin_unlock(&queue_lock);
baab935f 997 cache_put(cr->item, detail);
1da177e4
LT
998 kfree(cr->buf);
999 kfree(cr);
1000 return;
1001 }
1002 spin_unlock(&queue_lock);
1003}
1004
1005/*
1006 * Support routines for text-based upcalls.
1007 * Fields are separated by spaces.
1008 * Fields are either mangled to quote space tab newline slosh with slosh
1009 * or a hexified with a leading \x
1010 * Record is terminated with newline.
1011 *
1012 */
1013
1014void qword_add(char **bpp, int *lp, char *str)
1015{
1016 char *bp = *bpp;
1017 int len = *lp;
1018 char c;
1019
1020 if (len < 0) return;
1021
1022 while ((c=*str++) && len)
1023 switch(c) {
1024 case ' ':
1025 case '\t':
1026 case '\n':
1027 case '\\':
1028 if (len >= 4) {
1029 *bp++ = '\\';
1030 *bp++ = '0' + ((c & 0300)>>6);
1031 *bp++ = '0' + ((c & 0070)>>3);
1032 *bp++ = '0' + ((c & 0007)>>0);
1033 }
1034 len -= 4;
1035 break;
1036 default:
1037 *bp++ = c;
1038 len--;
1039 }
1040 if (c || len <1) len = -1;
1041 else {
1042 *bp++ = ' ';
1043 len--;
1044 }
1045 *bpp = bp;
1046 *lp = len;
1047}
24c3767e 1048EXPORT_SYMBOL_GPL(qword_add);
1da177e4
LT
1049
1050void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1051{
1052 char *bp = *bpp;
1053 int len = *lp;
1054
1055 if (len < 0) return;
1056
1057 if (len > 2) {
1058 *bp++ = '\\';
1059 *bp++ = 'x';
1060 len -= 2;
1061 while (blen && len >= 2) {
1062 unsigned char c = *buf++;
1063 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1064 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1065 len -= 2;
1066 blen--;
1067 }
1068 }
1069 if (blen || len<1) len = -1;
1070 else {
1071 *bp++ = ' ';
1072 len--;
1073 }
1074 *bpp = bp;
1075 *lp = len;
1076}
24c3767e 1077EXPORT_SYMBOL_GPL(qword_addhex);
1da177e4
LT
1078
1079static void warn_no_listener(struct cache_detail *detail)
1080{
1081 if (detail->last_warn != detail->last_close) {
1082 detail->last_warn = detail->last_close;
1083 if (detail->warn_no_listener)
2da8ca26 1084 detail->warn_no_listener(detail, detail->last_close != 0);
1da177e4
LT
1085 }
1086}
1087
1088/*
bc74b4f5
TM
1089 * register an upcall request to user-space and queue it up for read() by the
1090 * upcall daemon.
1091 *
1da177e4
LT
1092 * Each request is at most one page long.
1093 */
bc74b4f5
TM
1094int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1095 void (*cache_request)(struct cache_detail *,
1096 struct cache_head *,
1097 char **,
1098 int *))
1da177e4
LT
1099{
1100
1101 char *buf;
1102 struct cache_request *crq;
1103 char *bp;
1104 int len;
1105
1da177e4
LT
1106 if (atomic_read(&detail->readers) == 0 &&
1107 detail->last_close < get_seconds() - 30) {
1108 warn_no_listener(detail);
1109 return -EINVAL;
1110 }
1111
1112 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1113 if (!buf)
1114 return -EAGAIN;
1115
1116 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1117 if (!crq) {
1118 kfree(buf);
1119 return -EAGAIN;
1120 }
1121
1122 bp = buf; len = PAGE_SIZE;
1123
bc74b4f5 1124 cache_request(detail, h, &bp, &len);
1da177e4
LT
1125
1126 if (len < 0) {
1127 kfree(buf);
1128 kfree(crq);
1129 return -EAGAIN;
1130 }
1131 crq->q.reader = 0;
1132 crq->item = cache_get(h);
1133 crq->buf = buf;
1134 crq->len = PAGE_SIZE - len;
1135 crq->readers = 0;
1136 spin_lock(&queue_lock);
1137 list_add_tail(&crq->q.list, &detail->queue);
1138 spin_unlock(&queue_lock);
1139 wake_up(&queue_wait);
1140 return 0;
1141}
bc74b4f5 1142EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1da177e4
LT
1143
1144/*
1145 * parse a message from user-space and pass it
1146 * to an appropriate cache
1147 * Messages are, like requests, separated into fields by
1148 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1149 *
cca5172a 1150 * Message is
1da177e4
LT
1151 * reply cachename expiry key ... content....
1152 *
cca5172a 1153 * key and content are both parsed by cache
1da177e4
LT
1154 */
1155
1156#define isodigit(c) (isdigit(c) && c <= '7')
1157int qword_get(char **bpp, char *dest, int bufsize)
1158{
1159 /* return bytes copied, or -1 on error */
1160 char *bp = *bpp;
1161 int len = 0;
1162
1163 while (*bp == ' ') bp++;
1164
1165 if (bp[0] == '\\' && bp[1] == 'x') {
1166 /* HEX STRING */
1167 bp += 2;
1168 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1169 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1170 bp++;
1171 byte <<= 4;
1172 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1173 *dest++ = byte;
1174 bp++;
1175 len++;
1176 }
1177 } else {
1178 /* text with \nnn octal quoting */
1179 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1180 if (*bp == '\\' &&
1181 isodigit(bp[1]) && (bp[1] <= '3') &&
1182 isodigit(bp[2]) &&
1183 isodigit(bp[3])) {
1184 int byte = (*++bp -'0');
1185 bp++;
1186 byte = (byte << 3) | (*bp++ - '0');
1187 byte = (byte << 3) | (*bp++ - '0');
1188 *dest++ = byte;
1189 len++;
1190 } else {
1191 *dest++ = *bp++;
1192 len++;
1193 }
1194 }
1195 }
1196
1197 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1198 return -1;
1199 while (*bp == ' ') bp++;
1200 *bpp = bp;
1201 *dest = '\0';
1202 return len;
1203}
24c3767e 1204EXPORT_SYMBOL_GPL(qword_get);
1da177e4
LT
1205
1206
1207/*
1208 * support /proc/sunrpc/cache/$CACHENAME/content
1209 * as a seqfile.
1210 * We call ->cache_show passing NULL for the item to
1211 * get a header, then pass each real item in the cache
1212 */
1213
1214struct handle {
1215 struct cache_detail *cd;
1216};
1217
1218static void *c_start(struct seq_file *m, loff_t *pos)
9a429c49 1219 __acquires(cd->hash_lock)
1da177e4
LT
1220{
1221 loff_t n = *pos;
1222 unsigned hash, entry;
1223 struct cache_head *ch;
1224 struct cache_detail *cd = ((struct handle*)m->private)->cd;
cca5172a 1225
1da177e4
LT
1226
1227 read_lock(&cd->hash_lock);
1228 if (!n--)
1229 return SEQ_START_TOKEN;
1230 hash = n >> 32;
1231 entry = n & ((1LL<<32) - 1);
1232
1233 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1234 if (!entry--)
1235 return ch;
1236 n &= ~((1LL<<32) - 1);
1237 do {
1238 hash++;
1239 n += 1LL<<32;
cca5172a 1240 } while(hash < cd->hash_size &&
1da177e4
LT
1241 cd->hash_table[hash]==NULL);
1242 if (hash >= cd->hash_size)
1243 return NULL;
1244 *pos = n+1;
1245 return cd->hash_table[hash];
1246}
1247
1248static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1249{
1250 struct cache_head *ch = p;
1251 int hash = (*pos >> 32);
1252 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1253
1254 if (p == SEQ_START_TOKEN)
1255 hash = 0;
1256 else if (ch->next == NULL) {
1257 hash++;
1258 *pos += 1LL<<32;
1259 } else {
1260 ++*pos;
1261 return ch->next;
1262 }
1263 *pos &= ~((1LL<<32) - 1);
1264 while (hash < cd->hash_size &&
1265 cd->hash_table[hash] == NULL) {
1266 hash++;
1267 *pos += 1LL<<32;
1268 }
1269 if (hash >= cd->hash_size)
1270 return NULL;
1271 ++*pos;
1272 return cd->hash_table[hash];
1273}
1274
1275static void c_stop(struct seq_file *m, void *p)
9a429c49 1276 __releases(cd->hash_lock)
1da177e4
LT
1277{
1278 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1279 read_unlock(&cd->hash_lock);
1280}
1281
1282static int c_show(struct seq_file *m, void *p)
1283{
1284 struct cache_head *cp = p;
1285 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1286
1287 if (p == SEQ_START_TOKEN)
1288 return cd->cache_show(m, cd, NULL);
1289
1290 ifdebug(CACHE)
4013edea 1291 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
baab935f 1292 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
1da177e4
LT
1293 cache_get(cp);
1294 if (cache_check(cd, cp, NULL))
1295 /* cache_check does a cache_put on failure */
1296 seq_printf(m, "# ");
1297 else
1298 cache_put(cp, cd);
1299
1300 return cd->cache_show(m, cd, cp);
1301}
1302
56b3d975 1303static const struct seq_operations cache_content_op = {
1da177e4
LT
1304 .start = c_start,
1305 .next = c_next,
1306 .stop = c_stop,
1307 .show = c_show,
1308};
1309
1310static int content_open(struct inode *inode, struct file *file)
1311{
1da177e4
LT
1312 struct handle *han;
1313 struct cache_detail *cd = PDE(inode)->data;
1314
ec931035 1315 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1da177e4
LT
1316 if (han == NULL)
1317 return -ENOMEM;
1318
1319 han->cd = cd;
ec931035 1320 return 0;
1da177e4 1321}
1da177e4 1322
da7071d7 1323static const struct file_operations content_file_operations = {
1da177e4
LT
1324 .open = content_open,
1325 .read = seq_read,
1326 .llseek = seq_lseek,
14690fc6 1327 .release = seq_release_private,
1da177e4
LT
1328};
1329
1330static ssize_t read_flush(struct file *file, char __user *buf,
1331 size_t count, loff_t *ppos)
1332{
303b46bb 1333 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
1da177e4
LT
1334 char tbuf[20];
1335 unsigned long p = *ppos;
01b2969a 1336 size_t len;
1da177e4
LT
1337
1338 sprintf(tbuf, "%lu\n", cd->flush_time);
1339 len = strlen(tbuf);
1340 if (p >= len)
1341 return 0;
1342 len -= p;
01b2969a
CL
1343 if (len > count)
1344 len = count;
1da177e4 1345 if (copy_to_user(buf, (void*)(tbuf+p), len))
01b2969a
CL
1346 return -EFAULT;
1347 *ppos += len;
1da177e4
LT
1348 return len;
1349}
1350
1351static ssize_t write_flush(struct file * file, const char __user * buf,
1352 size_t count, loff_t *ppos)
1353{
303b46bb 1354 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
1da177e4
LT
1355 char tbuf[20];
1356 char *ep;
1357 long flushtime;
1358 if (*ppos || count > sizeof(tbuf)-1)
1359 return -EINVAL;
1360 if (copy_from_user(tbuf, buf, count))
1361 return -EFAULT;
1362 tbuf[count] = 0;
1363 flushtime = simple_strtoul(tbuf, &ep, 0);
1364 if (*ep && *ep != '\n')
1365 return -EINVAL;
1366
1367 cd->flush_time = flushtime;
1368 cd->nextcheck = get_seconds();
1369 cache_flush();
1370
1371 *ppos += count;
1372 return count;
1373}
1374
da7071d7 1375static const struct file_operations cache_flush_operations = {
1da177e4
LT
1376 .open = nonseekable_open,
1377 .read = read_flush,
1378 .write = write_flush,
1379};
This page took 0.716851 seconds and 5 git commands to generate.