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