Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / sunrpc / cache.c
blob0f10fee9a185e3ee899dfe4b17e4760d55d25a2f
1 /*
2 * net/sunrpc/cache.c
4 * Generic code for various authentication-related caches
5 * used by sunrpc clients and servers.
7 * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
9 * Released under terms in GPL version 2. See COPYING.
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>
29 #include <linux/mutex.h>
30 #include <asm/ioctls.h>
31 #include <linux/sunrpc/types.h>
32 #include <linux/sunrpc/cache.h>
33 #include <linux/sunrpc/stats.h>
35 #define RPCDBG_FACILITY RPCDBG_CACHE
37 static int cache_defer_req(struct cache_req *req, struct cache_head *item);
38 static void cache_revisit_request(struct cache_head *item);
40 static void cache_init(struct cache_head *h)
42 time_t now = get_seconds();
43 h->next = NULL;
44 h->flags = 0;
45 kref_init(&h->ref);
46 h->expiry_time = now + CACHE_NEW_EXPIRY;
47 h->last_refresh = now;
50 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
51 struct cache_head *key, int hash)
53 struct cache_head **head, **hp;
54 struct cache_head *new = NULL;
56 head = &detail->hash_table[hash];
58 read_lock(&detail->hash_lock);
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;
68 read_unlock(&detail->hash_lock);
69 /* Didn't find anything, insert an empty entry */
71 new = detail->alloc();
72 if (!new)
73 return NULL;
74 /* must fully initialise 'new', else
75 * we might get lose if we need to
76 * cache_put it soon.
78 cache_init(new);
79 detail->init(new, key);
81 write_lock(&detail->hash_lock);
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);
89 cache_put(new, detail);
90 return tmp;
93 new->next = *head;
94 *head = new;
95 detail->entries++;
96 cache_get(new);
97 write_unlock(&detail->hash_lock);
99 return new;
101 EXPORT_SYMBOL(sunrpc_cache_lookup);
104 static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
106 static int cache_fresh_locked(struct cache_head *head, time_t expiry)
108 head->expiry_time = expiry;
109 head->last_refresh = get_seconds();
110 return !test_and_set_bit(CACHE_VALID, &head->flags);
113 static void cache_fresh_unlocked(struct cache_head *head,
114 struct cache_detail *detail, int new)
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);
124 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
125 struct cache_head *new, struct cache_head *old, int hash)
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
131 struct cache_head **head;
132 struct cache_head *tmp;
133 int is_new;
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);
142 is_new = cache_fresh_locked(old, new->expiry_time);
143 write_unlock(&detail->hash_lock);
144 cache_fresh_unlocked(old, detail, is_new);
145 return old;
147 write_unlock(&detail->hash_lock);
149 /* We need to insert a new entry */
150 tmp = detail->alloc();
151 if (!tmp) {
152 cache_put(old, detail);
153 return NULL;
155 cache_init(tmp);
156 detail->init(tmp, old);
157 head = &detail->hash_table[hash];
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;
166 detail->entries++;
167 cache_get(tmp);
168 is_new = cache_fresh_locked(tmp, new->expiry_time);
169 cache_fresh_locked(old, 0);
170 write_unlock(&detail->hash_lock);
171 cache_fresh_unlocked(tmp, detail, is_new);
172 cache_fresh_unlocked(old, detail, 0);
173 cache_put(old, detail);
174 return tmp;
176 EXPORT_SYMBOL(sunrpc_cache_update);
178 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
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.
186 * Returns 0 if the cache_head can be used, or cache_puts it and returns
187 * -EAGAIN if upcall is pending,
188 * -ETIMEDOUT if upcall failed and should be retried,
189 * -ENOENT if cache entry was negative
191 int cache_check(struct cache_detail *detail,
192 struct cache_head *h, struct cache_req *rqstp)
194 int rv;
195 long refresh_age, age;
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;
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;
214 if (rqstp == NULL) {
215 if (rv == -EAGAIN)
216 rv = -ENOENT;
217 } else if (rv == -EAGAIN || age > refresh_age/2) {
218 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
219 refresh_age, age);
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);
226 cache_fresh_unlocked(h, detail,
227 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY));
228 rv = -ENOENT;
230 break;
232 case -EAGAIN:
233 clear_bit(CACHE_PENDING, &h->flags);
234 cache_revisit_request(h);
235 break;
240 if (rv == -EAGAIN)
241 if (cache_defer_req(rqstp, h) != 0)
242 rv = -ETIMEDOUT;
244 if (rv)
245 cache_put(h, detail);
246 return rv;
248 EXPORT_SYMBOL(cache_check);
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.
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.
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
264 * later we might drop old entries with non-NEVER expiry if that table
265 * is getting 'full' for some definition of 'full'
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.
277 * A table is then only scanned if the current time is at least
278 * the nextcheck time.
282 static LIST_HEAD(cache_list);
283 static DEFINE_SPINLOCK(cache_list_lock);
284 static struct cache_detail *current_detail;
285 static int current_index;
287 static const struct file_operations cache_file_operations;
288 static const struct file_operations content_file_operations;
289 static const struct file_operations cache_flush_operations;
291 static void do_cache_clean(struct work_struct *work);
292 static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
294 static void remove_cache_proc_entries(struct cache_detail *cd)
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);
308 #ifdef CONFIG_PROC_FS
309 static int create_cache_proc_entries(struct cache_detail *cd)
311 struct proc_dir_entry *p;
313 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
314 if (cd->proc_ent == NULL)
315 goto out_nomem;
316 cd->proc_ent->owner = cd->owner;
317 cd->channel_ent = cd->content_ent = NULL;
319 <<<<<<< HEAD:net/sunrpc/cache.c
320 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR, cd->proc_ent);
321 =======
322 p = proc_create("flush", S_IFREG|S_IRUSR|S_IWUSR,
323 cd->proc_ent, &cache_flush_operations);
324 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sunrpc/cache.c
325 cd->flush_ent = p;
326 if (p == NULL)
327 goto out_nomem;
328 <<<<<<< HEAD:net/sunrpc/cache.c
329 p->proc_fops = &cache_flush_operations;
330 =======
331 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sunrpc/cache.c
332 p->owner = cd->owner;
333 p->data = cd;
335 if (cd->cache_request || cd->cache_parse) {
336 <<<<<<< HEAD:net/sunrpc/cache.c
337 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
338 cd->proc_ent);
339 =======
340 p = proc_create("channel", S_IFREG|S_IRUSR|S_IWUSR,
341 cd->proc_ent, &cache_file_operations);
342 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sunrpc/cache.c
343 cd->channel_ent = p;
344 if (p == NULL)
345 goto out_nomem;
346 <<<<<<< HEAD:net/sunrpc/cache.c
347 p->proc_fops = &cache_file_operations;
348 =======
349 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sunrpc/cache.c
350 p->owner = cd->owner;
351 p->data = cd;
353 if (cd->cache_show) {
354 <<<<<<< HEAD:net/sunrpc/cache.c
355 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
356 cd->proc_ent);
357 =======
358 p = proc_create("content", S_IFREG|S_IRUSR|S_IWUSR,
359 cd->proc_ent, &content_file_operations);
360 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sunrpc/cache.c
361 cd->content_ent = p;
362 if (p == NULL)
363 goto out_nomem;
364 <<<<<<< HEAD:net/sunrpc/cache.c
365 p->proc_fops = &content_file_operations;
366 =======
367 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/sunrpc/cache.c
368 p->owner = cd->owner;
369 p->data = cd;
371 return 0;
372 out_nomem:
373 remove_cache_proc_entries(cd);
374 return -ENOMEM;
376 #else /* CONFIG_PROC_FS */
377 static int create_cache_proc_entries(struct cache_detail *cd)
379 return 0;
381 #endif
383 int cache_register(struct cache_detail *cd)
385 int ret;
387 ret = create_cache_proc_entries(cd);
388 if (ret)
389 return ret;
390 rwlock_init(&cd->hash_lock);
391 INIT_LIST_HEAD(&cd->queue);
392 spin_lock(&cache_list_lock);
393 cd->nextcheck = 0;
394 cd->entries = 0;
395 atomic_set(&cd->readers, 0);
396 cd->last_close = 0;
397 cd->last_warn = -1;
398 list_add(&cd->others, &cache_list);
399 spin_unlock(&cache_list_lock);
401 /* start the cleaning process */
402 schedule_delayed_work(&cache_cleaner, 0);
403 return 0;
405 EXPORT_SYMBOL(cache_register);
407 void cache_unregister(struct cache_detail *cd)
409 cache_purge(cd);
410 spin_lock(&cache_list_lock);
411 write_lock(&cd->hash_lock);
412 if (cd->entries || atomic_read(&cd->inuse)) {
413 write_unlock(&cd->hash_lock);
414 spin_unlock(&cache_list_lock);
415 goto out;
417 if (current_detail == cd)
418 current_detail = NULL;
419 list_del_init(&cd->others);
420 write_unlock(&cd->hash_lock);
421 spin_unlock(&cache_list_lock);
422 remove_cache_proc_entries(cd);
423 if (list_empty(&cache_list)) {
424 /* module must be being unloaded so its safe to kill the worker */
425 cancel_delayed_work_sync(&cache_cleaner);
427 return;
428 out:
429 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
431 EXPORT_SYMBOL(cache_unregister);
433 /* clean cache tries to find something to clean
434 * and cleans it.
435 * It returns 1 if it cleaned something,
436 * 0 if it didn't find anything this time
437 * -1 if it fell off the end of the list.
439 static int cache_clean(void)
441 int rv = 0;
442 struct list_head *next;
444 spin_lock(&cache_list_lock);
446 /* find a suitable table if we don't already have one */
447 while (current_detail == NULL ||
448 current_index >= current_detail->hash_size) {
449 if (current_detail)
450 next = current_detail->others.next;
451 else
452 next = cache_list.next;
453 if (next == &cache_list) {
454 current_detail = NULL;
455 spin_unlock(&cache_list_lock);
456 return -1;
458 current_detail = list_entry(next, struct cache_detail, others);
459 if (current_detail->nextcheck > get_seconds())
460 current_index = current_detail->hash_size;
461 else {
462 current_index = 0;
463 current_detail->nextcheck = get_seconds()+30*60;
467 /* find a non-empty bucket in the table */
468 while (current_detail &&
469 current_index < current_detail->hash_size &&
470 current_detail->hash_table[current_index] == NULL)
471 current_index++;
473 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
475 if (current_detail && current_index < current_detail->hash_size) {
476 struct cache_head *ch, **cp;
477 struct cache_detail *d;
479 write_lock(&current_detail->hash_lock);
481 /* Ok, now to clean this strand */
483 cp = & current_detail->hash_table[current_index];
484 ch = *cp;
485 for (; ch; cp= & ch->next, ch= *cp) {
486 if (current_detail->nextcheck > ch->expiry_time)
487 current_detail->nextcheck = ch->expiry_time+1;
488 if (ch->expiry_time >= get_seconds()
489 && ch->last_refresh >= current_detail->flush_time
491 continue;
492 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
493 queue_loose(current_detail, ch);
495 if (atomic_read(&ch->ref.refcount) == 1)
496 break;
498 if (ch) {
499 *cp = ch->next;
500 ch->next = NULL;
501 current_detail->entries--;
502 rv = 1;
504 write_unlock(&current_detail->hash_lock);
505 d = current_detail;
506 if (!ch)
507 current_index ++;
508 spin_unlock(&cache_list_lock);
509 if (ch)
510 cache_put(ch, d);
511 } else
512 spin_unlock(&cache_list_lock);
514 return rv;
518 * We want to regularly clean the cache, so we need to schedule some work ...
520 static void do_cache_clean(struct work_struct *work)
522 int delay = 5;
523 if (cache_clean() == -1)
524 delay = 30*HZ;
526 if (list_empty(&cache_list))
527 delay = 0;
529 if (delay)
530 schedule_delayed_work(&cache_cleaner, delay);
535 * Clean all caches promptly. This just calls cache_clean
536 * repeatedly until we are sure that every cache has had a chance to
537 * be fully cleaned
539 void cache_flush(void)
541 while (cache_clean() != -1)
542 cond_resched();
543 while (cache_clean() != -1)
544 cond_resched();
546 EXPORT_SYMBOL(cache_flush);
548 void cache_purge(struct cache_detail *detail)
550 detail->flush_time = LONG_MAX;
551 detail->nextcheck = get_seconds();
552 cache_flush();
553 detail->flush_time = 1;
555 EXPORT_SYMBOL(cache_purge);
559 * Deferral and Revisiting of Requests.
561 * If a cache lookup finds a pending entry, we
562 * need to defer the request and revisit it later.
563 * All deferred requests are stored in a hash table,
564 * indexed by "struct cache_head *".
565 * As it may be wasteful to store a whole request
566 * structure, we allow the request to provide a
567 * deferred form, which must contain a
568 * 'struct cache_deferred_req'
569 * This cache_deferred_req contains a method to allow
570 * it to be revisited when cache info is available
573 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
574 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
576 #define DFR_MAX 300 /* ??? */
578 static DEFINE_SPINLOCK(cache_defer_lock);
579 static LIST_HEAD(cache_defer_list);
580 static struct list_head cache_defer_hash[DFR_HASHSIZE];
581 static int cache_defer_cnt;
583 static int cache_defer_req(struct cache_req *req, struct cache_head *item)
585 struct cache_deferred_req *dreq;
586 int hash = DFR_HASH(item);
588 if (cache_defer_cnt >= DFR_MAX) {
589 /* too much in the cache, randomly drop this one,
590 * or continue and drop the oldest below
592 if (net_random()&1)
593 return -ETIMEDOUT;
595 dreq = req->defer(req);
596 if (dreq == NULL)
597 return -ETIMEDOUT;
599 dreq->item = item;
600 dreq->recv_time = get_seconds();
602 spin_lock(&cache_defer_lock);
604 list_add(&dreq->recent, &cache_defer_list);
606 if (cache_defer_hash[hash].next == NULL)
607 INIT_LIST_HEAD(&cache_defer_hash[hash]);
608 list_add(&dreq->hash, &cache_defer_hash[hash]);
610 /* it is in, now maybe clean up */
611 dreq = NULL;
612 if (++cache_defer_cnt > DFR_MAX) {
613 dreq = list_entry(cache_defer_list.prev,
614 struct cache_deferred_req, recent);
615 list_del(&dreq->recent);
616 list_del(&dreq->hash);
617 cache_defer_cnt--;
619 spin_unlock(&cache_defer_lock);
621 if (dreq) {
622 /* there was one too many */
623 dreq->revisit(dreq, 1);
625 if (!test_bit(CACHE_PENDING, &item->flags)) {
626 /* must have just been validated... */
627 cache_revisit_request(item);
629 return 0;
632 static void cache_revisit_request(struct cache_head *item)
634 struct cache_deferred_req *dreq;
635 struct list_head pending;
637 struct list_head *lp;
638 int hash = DFR_HASH(item);
640 INIT_LIST_HEAD(&pending);
641 spin_lock(&cache_defer_lock);
643 lp = cache_defer_hash[hash].next;
644 if (lp) {
645 while (lp != &cache_defer_hash[hash]) {
646 dreq = list_entry(lp, struct cache_deferred_req, hash);
647 lp = lp->next;
648 if (dreq->item == item) {
649 list_del(&dreq->hash);
650 list_move(&dreq->recent, &pending);
651 cache_defer_cnt--;
655 spin_unlock(&cache_defer_lock);
657 while (!list_empty(&pending)) {
658 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
659 list_del_init(&dreq->recent);
660 dreq->revisit(dreq, 0);
664 void cache_clean_deferred(void *owner)
666 struct cache_deferred_req *dreq, *tmp;
667 struct list_head pending;
670 INIT_LIST_HEAD(&pending);
671 spin_lock(&cache_defer_lock);
673 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
674 if (dreq->owner == owner) {
675 list_del(&dreq->hash);
676 list_move(&dreq->recent, &pending);
677 cache_defer_cnt--;
680 spin_unlock(&cache_defer_lock);
682 while (!list_empty(&pending)) {
683 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
684 list_del_init(&dreq->recent);
685 dreq->revisit(dreq, 1);
690 * communicate with user-space
692 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
693 * On read, you get a full request, or block.
694 * On write, an update request is processed.
695 * Poll works if anything to read, and always allows write.
697 * Implemented by linked list of requests. Each open file has
698 * a ->private that also exists in this list. New requests are added
699 * to the end and may wakeup and preceding readers.
700 * New readers are added to the head. If, on read, an item is found with
701 * CACHE_UPCALLING clear, we free it from the list.
705 static DEFINE_SPINLOCK(queue_lock);
706 static DEFINE_MUTEX(queue_io_mutex);
708 struct cache_queue {
709 struct list_head list;
710 int reader; /* if 0, then request */
712 struct cache_request {
713 struct cache_queue q;
714 struct cache_head *item;
715 char * buf;
716 int len;
717 int readers;
719 struct cache_reader {
720 struct cache_queue q;
721 int offset; /* if non-0, we have a refcnt on next request */
724 static ssize_t
725 cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
727 struct cache_reader *rp = filp->private_data;
728 struct cache_request *rq;
729 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
730 int err;
732 if (count == 0)
733 return 0;
735 mutex_lock(&queue_io_mutex); /* protect against multiple concurrent
736 * readers on this file */
737 again:
738 spin_lock(&queue_lock);
739 /* need to find next request */
740 while (rp->q.list.next != &cd->queue &&
741 list_entry(rp->q.list.next, struct cache_queue, list)
742 ->reader) {
743 struct list_head *next = rp->q.list.next;
744 list_move(&rp->q.list, next);
746 if (rp->q.list.next == &cd->queue) {
747 spin_unlock(&queue_lock);
748 mutex_unlock(&queue_io_mutex);
749 BUG_ON(rp->offset);
750 return 0;
752 rq = container_of(rp->q.list.next, struct cache_request, q.list);
753 BUG_ON(rq->q.reader);
754 if (rp->offset == 0)
755 rq->readers++;
756 spin_unlock(&queue_lock);
758 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
759 err = -EAGAIN;
760 spin_lock(&queue_lock);
761 list_move(&rp->q.list, &rq->q.list);
762 spin_unlock(&queue_lock);
763 } else {
764 if (rp->offset + count > rq->len)
765 count = rq->len - rp->offset;
766 err = -EFAULT;
767 if (copy_to_user(buf, rq->buf + rp->offset, count))
768 goto out;
769 rp->offset += count;
770 if (rp->offset >= rq->len) {
771 rp->offset = 0;
772 spin_lock(&queue_lock);
773 list_move(&rp->q.list, &rq->q.list);
774 spin_unlock(&queue_lock);
776 err = 0;
778 out:
779 if (rp->offset == 0) {
780 /* need to release rq */
781 spin_lock(&queue_lock);
782 rq->readers--;
783 if (rq->readers == 0 &&
784 !test_bit(CACHE_PENDING, &rq->item->flags)) {
785 list_del(&rq->q.list);
786 spin_unlock(&queue_lock);
787 cache_put(rq->item, cd);
788 kfree(rq->buf);
789 kfree(rq);
790 } else
791 spin_unlock(&queue_lock);
793 if (err == -EAGAIN)
794 goto again;
795 mutex_unlock(&queue_io_mutex);
796 return err ? err : count;
799 static char write_buf[8192]; /* protected by queue_io_mutex */
801 static ssize_t
802 cache_write(struct file *filp, const char __user *buf, size_t count,
803 loff_t *ppos)
805 int err;
806 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
808 if (count == 0)
809 return 0;
810 if (count >= sizeof(write_buf))
811 return -EINVAL;
813 mutex_lock(&queue_io_mutex);
815 if (copy_from_user(write_buf, buf, count)) {
816 mutex_unlock(&queue_io_mutex);
817 return -EFAULT;
819 write_buf[count] = '\0';
820 if (cd->cache_parse)
821 err = cd->cache_parse(cd, write_buf, count);
822 else
823 err = -EINVAL;
825 mutex_unlock(&queue_io_mutex);
826 return err ? err : count;
829 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
831 static unsigned int
832 cache_poll(struct file *filp, poll_table *wait)
834 unsigned int mask;
835 struct cache_reader *rp = filp->private_data;
836 struct cache_queue *cq;
837 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
839 poll_wait(filp, &queue_wait, wait);
841 /* alway allow write */
842 mask = POLL_OUT | POLLWRNORM;
844 if (!rp)
845 return mask;
847 spin_lock(&queue_lock);
849 for (cq= &rp->q; &cq->list != &cd->queue;
850 cq = list_entry(cq->list.next, struct cache_queue, list))
851 if (!cq->reader) {
852 mask |= POLLIN | POLLRDNORM;
853 break;
855 spin_unlock(&queue_lock);
856 return mask;
859 static int
860 cache_ioctl(struct inode *ino, struct file *filp,
861 unsigned int cmd, unsigned long arg)
863 int len = 0;
864 struct cache_reader *rp = filp->private_data;
865 struct cache_queue *cq;
866 struct cache_detail *cd = PDE(ino)->data;
868 if (cmd != FIONREAD || !rp)
869 return -EINVAL;
871 spin_lock(&queue_lock);
873 /* only find the length remaining in current request,
874 * or the length of the next request
876 for (cq= &rp->q; &cq->list != &cd->queue;
877 cq = list_entry(cq->list.next, struct cache_queue, list))
878 if (!cq->reader) {
879 struct cache_request *cr =
880 container_of(cq, struct cache_request, q);
881 len = cr->len - rp->offset;
882 break;
884 spin_unlock(&queue_lock);
886 return put_user(len, (int __user *)arg);
889 static int
890 cache_open(struct inode *inode, struct file *filp)
892 struct cache_reader *rp = NULL;
894 nonseekable_open(inode, filp);
895 if (filp->f_mode & FMODE_READ) {
896 struct cache_detail *cd = PDE(inode)->data;
898 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
899 if (!rp)
900 return -ENOMEM;
901 rp->offset = 0;
902 rp->q.reader = 1;
903 atomic_inc(&cd->readers);
904 spin_lock(&queue_lock);
905 list_add(&rp->q.list, &cd->queue);
906 spin_unlock(&queue_lock);
908 filp->private_data = rp;
909 return 0;
912 static int
913 cache_release(struct inode *inode, struct file *filp)
915 struct cache_reader *rp = filp->private_data;
916 struct cache_detail *cd = PDE(inode)->data;
918 if (rp) {
919 spin_lock(&queue_lock);
920 if (rp->offset) {
921 struct cache_queue *cq;
922 for (cq= &rp->q; &cq->list != &cd->queue;
923 cq = list_entry(cq->list.next, struct cache_queue, list))
924 if (!cq->reader) {
925 container_of(cq, struct cache_request, q)
926 ->readers--;
927 break;
929 rp->offset = 0;
931 list_del(&rp->q.list);
932 spin_unlock(&queue_lock);
934 filp->private_data = NULL;
935 kfree(rp);
937 cd->last_close = get_seconds();
938 atomic_dec(&cd->readers);
940 return 0;
945 static const struct file_operations cache_file_operations = {
946 .owner = THIS_MODULE,
947 .llseek = no_llseek,
948 .read = cache_read,
949 .write = cache_write,
950 .poll = cache_poll,
951 .ioctl = cache_ioctl, /* for FIONREAD */
952 .open = cache_open,
953 .release = cache_release,
957 static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
959 struct cache_queue *cq;
960 spin_lock(&queue_lock);
961 list_for_each_entry(cq, &detail->queue, list)
962 if (!cq->reader) {
963 struct cache_request *cr = container_of(cq, struct cache_request, q);
964 if (cr->item != ch)
965 continue;
966 if (cr->readers != 0)
967 continue;
968 list_del(&cr->q.list);
969 spin_unlock(&queue_lock);
970 cache_put(cr->item, detail);
971 kfree(cr->buf);
972 kfree(cr);
973 return;
975 spin_unlock(&queue_lock);
979 * Support routines for text-based upcalls.
980 * Fields are separated by spaces.
981 * Fields are either mangled to quote space tab newline slosh with slosh
982 * or a hexified with a leading \x
983 * Record is terminated with newline.
987 void qword_add(char **bpp, int *lp, char *str)
989 char *bp = *bpp;
990 int len = *lp;
991 char c;
993 if (len < 0) return;
995 while ((c=*str++) && len)
996 switch(c) {
997 case ' ':
998 case '\t':
999 case '\n':
1000 case '\\':
1001 if (len >= 4) {
1002 *bp++ = '\\';
1003 *bp++ = '0' + ((c & 0300)>>6);
1004 *bp++ = '0' + ((c & 0070)>>3);
1005 *bp++ = '0' + ((c & 0007)>>0);
1007 len -= 4;
1008 break;
1009 default:
1010 *bp++ = c;
1011 len--;
1013 if (c || len <1) len = -1;
1014 else {
1015 *bp++ = ' ';
1016 len--;
1018 *bpp = bp;
1019 *lp = len;
1021 EXPORT_SYMBOL(qword_add);
1023 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1025 char *bp = *bpp;
1026 int len = *lp;
1028 if (len < 0) return;
1030 if (len > 2) {
1031 *bp++ = '\\';
1032 *bp++ = 'x';
1033 len -= 2;
1034 while (blen && len >= 2) {
1035 unsigned char c = *buf++;
1036 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1037 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1038 len -= 2;
1039 blen--;
1042 if (blen || len<1) len = -1;
1043 else {
1044 *bp++ = ' ';
1045 len--;
1047 *bpp = bp;
1048 *lp = len;
1050 EXPORT_SYMBOL(qword_addhex);
1052 static void warn_no_listener(struct cache_detail *detail)
1054 if (detail->last_warn != detail->last_close) {
1055 detail->last_warn = detail->last_close;
1056 if (detail->warn_no_listener)
1057 detail->warn_no_listener(detail);
1062 * register an upcall request to user-space.
1063 * Each request is at most one page long.
1065 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
1068 char *buf;
1069 struct cache_request *crq;
1070 char *bp;
1071 int len;
1073 if (detail->cache_request == NULL)
1074 return -EINVAL;
1076 if (atomic_read(&detail->readers) == 0 &&
1077 detail->last_close < get_seconds() - 30) {
1078 warn_no_listener(detail);
1079 return -EINVAL;
1082 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1083 if (!buf)
1084 return -EAGAIN;
1086 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1087 if (!crq) {
1088 kfree(buf);
1089 return -EAGAIN;
1092 bp = buf; len = PAGE_SIZE;
1094 detail->cache_request(detail, h, &bp, &len);
1096 if (len < 0) {
1097 kfree(buf);
1098 kfree(crq);
1099 return -EAGAIN;
1101 crq->q.reader = 0;
1102 crq->item = cache_get(h);
1103 crq->buf = buf;
1104 crq->len = PAGE_SIZE - len;
1105 crq->readers = 0;
1106 spin_lock(&queue_lock);
1107 list_add_tail(&crq->q.list, &detail->queue);
1108 spin_unlock(&queue_lock);
1109 wake_up(&queue_wait);
1110 return 0;
1114 * parse a message from user-space and pass it
1115 * to an appropriate cache
1116 * Messages are, like requests, separated into fields by
1117 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1119 * Message is
1120 * reply cachename expiry key ... content....
1122 * key and content are both parsed by cache
1125 #define isodigit(c) (isdigit(c) && c <= '7')
1126 int qword_get(char **bpp, char *dest, int bufsize)
1128 /* return bytes copied, or -1 on error */
1129 char *bp = *bpp;
1130 int len = 0;
1132 while (*bp == ' ') bp++;
1134 if (bp[0] == '\\' && bp[1] == 'x') {
1135 /* HEX STRING */
1136 bp += 2;
1137 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1138 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1139 bp++;
1140 byte <<= 4;
1141 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1142 *dest++ = byte;
1143 bp++;
1144 len++;
1146 } else {
1147 /* text with \nnn octal quoting */
1148 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1149 if (*bp == '\\' &&
1150 isodigit(bp[1]) && (bp[1] <= '3') &&
1151 isodigit(bp[2]) &&
1152 isodigit(bp[3])) {
1153 int byte = (*++bp -'0');
1154 bp++;
1155 byte = (byte << 3) | (*bp++ - '0');
1156 byte = (byte << 3) | (*bp++ - '0');
1157 *dest++ = byte;
1158 len++;
1159 } else {
1160 *dest++ = *bp++;
1161 len++;
1166 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1167 return -1;
1168 while (*bp == ' ') bp++;
1169 *bpp = bp;
1170 *dest = '\0';
1171 return len;
1173 EXPORT_SYMBOL(qword_get);
1177 * support /proc/sunrpc/cache/$CACHENAME/content
1178 * as a seqfile.
1179 * We call ->cache_show passing NULL for the item to
1180 * get a header, then pass each real item in the cache
1183 struct handle {
1184 struct cache_detail *cd;
1187 static void *c_start(struct seq_file *m, loff_t *pos)
1188 __acquires(cd->hash_lock)
1190 loff_t n = *pos;
1191 unsigned hash, entry;
1192 struct cache_head *ch;
1193 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1196 read_lock(&cd->hash_lock);
1197 if (!n--)
1198 return SEQ_START_TOKEN;
1199 hash = n >> 32;
1200 entry = n & ((1LL<<32) - 1);
1202 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1203 if (!entry--)
1204 return ch;
1205 n &= ~((1LL<<32) - 1);
1206 do {
1207 hash++;
1208 n += 1LL<<32;
1209 } while(hash < cd->hash_size &&
1210 cd->hash_table[hash]==NULL);
1211 if (hash >= cd->hash_size)
1212 return NULL;
1213 *pos = n+1;
1214 return cd->hash_table[hash];
1217 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1219 struct cache_head *ch = p;
1220 int hash = (*pos >> 32);
1221 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1223 if (p == SEQ_START_TOKEN)
1224 hash = 0;
1225 else if (ch->next == NULL) {
1226 hash++;
1227 *pos += 1LL<<32;
1228 } else {
1229 ++*pos;
1230 return ch->next;
1232 *pos &= ~((1LL<<32) - 1);
1233 while (hash < cd->hash_size &&
1234 cd->hash_table[hash] == NULL) {
1235 hash++;
1236 *pos += 1LL<<32;
1238 if (hash >= cd->hash_size)
1239 return NULL;
1240 ++*pos;
1241 return cd->hash_table[hash];
1244 static void c_stop(struct seq_file *m, void *p)
1245 __releases(cd->hash_lock)
1247 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1248 read_unlock(&cd->hash_lock);
1251 static int c_show(struct seq_file *m, void *p)
1253 struct cache_head *cp = p;
1254 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1256 if (p == SEQ_START_TOKEN)
1257 return cd->cache_show(m, cd, NULL);
1259 ifdebug(CACHE)
1260 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1261 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
1262 cache_get(cp);
1263 if (cache_check(cd, cp, NULL))
1264 /* cache_check does a cache_put on failure */
1265 seq_printf(m, "# ");
1266 else
1267 cache_put(cp, cd);
1269 return cd->cache_show(m, cd, cp);
1272 static const struct seq_operations cache_content_op = {
1273 .start = c_start,
1274 .next = c_next,
1275 .stop = c_stop,
1276 .show = c_show,
1279 static int content_open(struct inode *inode, struct file *file)
1281 struct handle *han;
1282 struct cache_detail *cd = PDE(inode)->data;
1284 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1285 if (han == NULL)
1286 return -ENOMEM;
1288 han->cd = cd;
1289 return 0;
1292 static const struct file_operations content_file_operations = {
1293 .open = content_open,
1294 .read = seq_read,
1295 .llseek = seq_lseek,
1296 .release = seq_release_private,
1299 static ssize_t read_flush(struct file *file, char __user *buf,
1300 size_t count, loff_t *ppos)
1302 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
1303 char tbuf[20];
1304 unsigned long p = *ppos;
1305 size_t len;
1307 sprintf(tbuf, "%lu\n", cd->flush_time);
1308 len = strlen(tbuf);
1309 if (p >= len)
1310 return 0;
1311 len -= p;
1312 if (len > count)
1313 len = count;
1314 if (copy_to_user(buf, (void*)(tbuf+p), len))
1315 return -EFAULT;
1316 *ppos += len;
1317 return len;
1320 static ssize_t write_flush(struct file * file, const char __user * buf,
1321 size_t count, loff_t *ppos)
1323 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
1324 char tbuf[20];
1325 char *ep;
1326 long flushtime;
1327 if (*ppos || count > sizeof(tbuf)-1)
1328 return -EINVAL;
1329 if (copy_from_user(tbuf, buf, count))
1330 return -EFAULT;
1331 tbuf[count] = 0;
1332 flushtime = simple_strtoul(tbuf, &ep, 0);
1333 if (*ep && *ep != '\n')
1334 return -EINVAL;
1336 cd->flush_time = flushtime;
1337 cd->nextcheck = get_seconds();
1338 cache_flush();
1340 *ppos += count;
1341 return count;
1344 static const struct file_operations cache_flush_operations = {
1345 .open = nonseekable_open,
1346 .read = read_flush,
1347 .write = write_flush,