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