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