e1000: Fixed the following issues with ESB2 (requires ESB2 support):
[linux-2.6/mini2440.git] / net / sunrpc / cache.c
blobdcaa0c4453ff4b0a7eb532a5676bfe3d27349e5c
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 <asm/ioctls.h>
30 #include <linux/sunrpc/types.h>
31 #include <linux/sunrpc/cache.h>
32 #include <linux/sunrpc/stats.h>
34 #define RPCDBG_FACILITY RPCDBG_CACHE
36 static void cache_defer_req(struct cache_req *req, struct cache_head *item);
37 static void cache_revisit_request(struct cache_head *item);
39 void cache_init(struct cache_head *h)
41 time_t now = get_seconds();
42 h->next = NULL;
43 h->flags = 0;
44 atomic_set(&h->refcnt, 1);
45 h->expiry_time = now + CACHE_NEW_EXPIRY;
46 h->last_refresh = now;
50 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
52 * This is the generic cache management routine for all
53 * the authentication caches.
54 * It checks the currency of a cache item and will (later)
55 * initiate an upcall to fill it if needed.
58 * Returns 0 if the cache_head can be used, or cache_puts it and returns
59 * -EAGAIN if upcall is pending,
60 * -ENOENT if cache entry was negative
62 int cache_check(struct cache_detail *detail,
63 struct cache_head *h, struct cache_req *rqstp)
65 int rv;
66 long refresh_age, age;
68 /* First decide return status as best we can */
69 if (!test_bit(CACHE_VALID, &h->flags) ||
70 h->expiry_time < get_seconds())
71 rv = -EAGAIN;
72 else if (detail->flush_time > h->last_refresh)
73 rv = -EAGAIN;
74 else {
75 /* entry is valid */
76 if (test_bit(CACHE_NEGATIVE, &h->flags))
77 rv = -ENOENT;
78 else rv = 0;
81 /* now see if we want to start an upcall */
82 refresh_age = (h->expiry_time - h->last_refresh);
83 age = get_seconds() - h->last_refresh;
85 if (rqstp == NULL) {
86 if (rv == -EAGAIN)
87 rv = -ENOENT;
88 } else if (rv == -EAGAIN || age > refresh_age/2) {
89 dprintk("Want update, refage=%ld, age=%ld\n", refresh_age, age);
90 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
91 switch (cache_make_upcall(detail, h)) {
92 case -EINVAL:
93 clear_bit(CACHE_PENDING, &h->flags);
94 if (rv == -EAGAIN) {
95 set_bit(CACHE_NEGATIVE, &h->flags);
96 cache_fresh(detail, h, get_seconds()+CACHE_NEW_EXPIRY);
97 rv = -ENOENT;
99 break;
101 case -EAGAIN:
102 clear_bit(CACHE_PENDING, &h->flags);
103 cache_revisit_request(h);
104 break;
109 if (rv == -EAGAIN)
110 cache_defer_req(rqstp, h);
112 if (rv && h)
113 detail->cache_put(h, detail);
114 return rv;
117 static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
119 void cache_fresh(struct cache_detail *detail,
120 struct cache_head *head, time_t expiry)
123 head->expiry_time = expiry;
124 head->last_refresh = get_seconds();
125 if (!test_and_set_bit(CACHE_VALID, &head->flags))
126 cache_revisit_request(head);
127 if (test_and_clear_bit(CACHE_PENDING, &head->flags))
128 queue_loose(detail, head);
132 * caches need to be periodically cleaned.
133 * For this we maintain a list of cache_detail and
134 * a current pointer into that list and into the table
135 * for that entry.
137 * Each time clean_cache is called it finds the next non-empty entry
138 * in the current table and walks the list in that entry
139 * looking for entries that can be removed.
141 * An entry gets removed if:
142 * - The expiry is before current time
143 * - The last_refresh time is before the flush_time for that cache
145 * later we might drop old entries with non-NEVER expiry if that table
146 * is getting 'full' for some definition of 'full'
148 * The question of "how often to scan a table" is an interesting one
149 * and is answered in part by the use of the "nextcheck" field in the
150 * cache_detail.
151 * When a scan of a table begins, the nextcheck field is set to a time
152 * that is well into the future.
153 * While scanning, if an expiry time is found that is earlier than the
154 * current nextcheck time, nextcheck is set to that expiry time.
155 * If the flush_time is ever set to a time earlier than the nextcheck
156 * time, the nextcheck time is then set to that flush_time.
158 * A table is then only scanned if the current time is at least
159 * the nextcheck time.
163 static LIST_HEAD(cache_list);
164 static DEFINE_SPINLOCK(cache_list_lock);
165 static struct cache_detail *current_detail;
166 static int current_index;
168 static struct file_operations cache_file_operations;
169 static struct file_operations content_file_operations;
170 static struct file_operations cache_flush_operations;
172 static void do_cache_clean(void *data);
173 static DECLARE_WORK(cache_cleaner, do_cache_clean, NULL);
175 void cache_register(struct cache_detail *cd)
177 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
178 if (cd->proc_ent) {
179 struct proc_dir_entry *p;
180 cd->proc_ent->owner = cd->owner;
181 cd->channel_ent = cd->content_ent = NULL;
183 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR,
184 cd->proc_ent);
185 cd->flush_ent = p;
186 if (p) {
187 p->proc_fops = &cache_flush_operations;
188 p->owner = cd->owner;
189 p->data = cd;
192 if (cd->cache_request || cd->cache_parse) {
193 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
194 cd->proc_ent);
195 cd->channel_ent = p;
196 if (p) {
197 p->proc_fops = &cache_file_operations;
198 p->owner = cd->owner;
199 p->data = cd;
202 if (cd->cache_show) {
203 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
204 cd->proc_ent);
205 cd->content_ent = p;
206 if (p) {
207 p->proc_fops = &content_file_operations;
208 p->owner = cd->owner;
209 p->data = cd;
213 rwlock_init(&cd->hash_lock);
214 INIT_LIST_HEAD(&cd->queue);
215 spin_lock(&cache_list_lock);
216 cd->nextcheck = 0;
217 cd->entries = 0;
218 atomic_set(&cd->readers, 0);
219 cd->last_close = 0;
220 cd->last_warn = -1;
221 list_add(&cd->others, &cache_list);
222 spin_unlock(&cache_list_lock);
224 /* start the cleaning process */
225 schedule_work(&cache_cleaner);
228 int cache_unregister(struct cache_detail *cd)
230 cache_purge(cd);
231 spin_lock(&cache_list_lock);
232 write_lock(&cd->hash_lock);
233 if (cd->entries || atomic_read(&cd->inuse)) {
234 write_unlock(&cd->hash_lock);
235 spin_unlock(&cache_list_lock);
236 return -EBUSY;
238 if (current_detail == cd)
239 current_detail = NULL;
240 list_del_init(&cd->others);
241 write_unlock(&cd->hash_lock);
242 spin_unlock(&cache_list_lock);
243 if (cd->proc_ent) {
244 if (cd->flush_ent)
245 remove_proc_entry("flush", cd->proc_ent);
246 if (cd->channel_ent)
247 remove_proc_entry("channel", cd->proc_ent);
248 if (cd->content_ent)
249 remove_proc_entry("content", cd->proc_ent);
251 cd->proc_ent = NULL;
252 remove_proc_entry(cd->name, proc_net_rpc);
254 if (list_empty(&cache_list)) {
255 /* module must be being unloaded so its safe to kill the worker */
256 cancel_delayed_work(&cache_cleaner);
257 flush_scheduled_work();
259 return 0;
262 /* clean cache tries to find something to clean
263 * and cleans it.
264 * It returns 1 if it cleaned something,
265 * 0 if it didn't find anything this time
266 * -1 if it fell off the end of the list.
268 static int cache_clean(void)
270 int rv = 0;
271 struct list_head *next;
273 spin_lock(&cache_list_lock);
275 /* find a suitable table if we don't already have one */
276 while (current_detail == NULL ||
277 current_index >= current_detail->hash_size) {
278 if (current_detail)
279 next = current_detail->others.next;
280 else
281 next = cache_list.next;
282 if (next == &cache_list) {
283 current_detail = NULL;
284 spin_unlock(&cache_list_lock);
285 return -1;
287 current_detail = list_entry(next, struct cache_detail, others);
288 if (current_detail->nextcheck > get_seconds())
289 current_index = current_detail->hash_size;
290 else {
291 current_index = 0;
292 current_detail->nextcheck = get_seconds()+30*60;
296 /* find a non-empty bucket in the table */
297 while (current_detail &&
298 current_index < current_detail->hash_size &&
299 current_detail->hash_table[current_index] == NULL)
300 current_index++;
302 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
304 if (current_detail && current_index < current_detail->hash_size) {
305 struct cache_head *ch, **cp;
306 struct cache_detail *d;
308 write_lock(&current_detail->hash_lock);
310 /* Ok, now to clean this strand */
312 cp = & current_detail->hash_table[current_index];
313 ch = *cp;
314 for (; ch; cp= & ch->next, ch= *cp) {
315 if (current_detail->nextcheck > ch->expiry_time)
316 current_detail->nextcheck = ch->expiry_time+1;
317 if (ch->expiry_time >= get_seconds()
318 && ch->last_refresh >= current_detail->flush_time
320 continue;
321 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
322 queue_loose(current_detail, ch);
324 if (atomic_read(&ch->refcnt) == 1)
325 break;
327 if (ch) {
328 *cp = ch->next;
329 ch->next = NULL;
330 current_detail->entries--;
331 rv = 1;
333 write_unlock(&current_detail->hash_lock);
334 d = current_detail;
335 if (!ch)
336 current_index ++;
337 spin_unlock(&cache_list_lock);
338 if (ch)
339 d->cache_put(ch, d);
340 } else
341 spin_unlock(&cache_list_lock);
343 return rv;
347 * We want to regularly clean the cache, so we need to schedule some work ...
349 static void do_cache_clean(void *data)
351 int delay = 5;
352 if (cache_clean() == -1)
353 delay = 30*HZ;
355 if (list_empty(&cache_list))
356 delay = 0;
358 if (delay)
359 schedule_delayed_work(&cache_cleaner, delay);
364 * Clean all caches promptly. This just calls cache_clean
365 * repeatedly until we are sure that every cache has had a chance to
366 * be fully cleaned
368 void cache_flush(void)
370 while (cache_clean() != -1)
371 cond_resched();
372 while (cache_clean() != -1)
373 cond_resched();
376 void cache_purge(struct cache_detail *detail)
378 detail->flush_time = LONG_MAX;
379 detail->nextcheck = get_seconds();
380 cache_flush();
381 detail->flush_time = 1;
387 * Deferral and Revisiting of Requests.
389 * If a cache lookup finds a pending entry, we
390 * need to defer the request and revisit it later.
391 * All deferred requests are stored in a hash table,
392 * indexed by "struct cache_head *".
393 * As it may be wasteful to store a whole request
394 * structure, we allow the request to provide a
395 * deferred form, which must contain a
396 * 'struct cache_deferred_req'
397 * This cache_deferred_req contains a method to allow
398 * it to be revisited when cache info is available
401 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
402 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
404 #define DFR_MAX 300 /* ??? */
406 static DEFINE_SPINLOCK(cache_defer_lock);
407 static LIST_HEAD(cache_defer_list);
408 static struct list_head cache_defer_hash[DFR_HASHSIZE];
409 static int cache_defer_cnt;
411 static void cache_defer_req(struct cache_req *req, struct cache_head *item)
413 struct cache_deferred_req *dreq;
414 int hash = DFR_HASH(item);
416 dreq = req->defer(req);
417 if (dreq == NULL)
418 return;
420 dreq->item = item;
421 dreq->recv_time = get_seconds();
423 spin_lock(&cache_defer_lock);
425 list_add(&dreq->recent, &cache_defer_list);
427 if (cache_defer_hash[hash].next == NULL)
428 INIT_LIST_HEAD(&cache_defer_hash[hash]);
429 list_add(&dreq->hash, &cache_defer_hash[hash]);
431 /* it is in, now maybe clean up */
432 dreq = NULL;
433 if (++cache_defer_cnt > DFR_MAX) {
434 /* too much in the cache, randomly drop
435 * first or last
437 if (net_random()&1)
438 dreq = list_entry(cache_defer_list.next,
439 struct cache_deferred_req,
440 recent);
441 else
442 dreq = list_entry(cache_defer_list.prev,
443 struct cache_deferred_req,
444 recent);
445 list_del(&dreq->recent);
446 list_del(&dreq->hash);
447 cache_defer_cnt--;
449 spin_unlock(&cache_defer_lock);
451 if (dreq) {
452 /* there was one too many */
453 dreq->revisit(dreq, 1);
455 if (test_bit(CACHE_VALID, &item->flags)) {
456 /* must have just been validated... */
457 cache_revisit_request(item);
461 static void cache_revisit_request(struct cache_head *item)
463 struct cache_deferred_req *dreq;
464 struct list_head pending;
466 struct list_head *lp;
467 int hash = DFR_HASH(item);
469 INIT_LIST_HEAD(&pending);
470 spin_lock(&cache_defer_lock);
472 lp = cache_defer_hash[hash].next;
473 if (lp) {
474 while (lp != &cache_defer_hash[hash]) {
475 dreq = list_entry(lp, struct cache_deferred_req, hash);
476 lp = lp->next;
477 if (dreq->item == item) {
478 list_del(&dreq->hash);
479 list_move(&dreq->recent, &pending);
480 cache_defer_cnt--;
484 spin_unlock(&cache_defer_lock);
486 while (!list_empty(&pending)) {
487 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
488 list_del_init(&dreq->recent);
489 dreq->revisit(dreq, 0);
493 void cache_clean_deferred(void *owner)
495 struct cache_deferred_req *dreq, *tmp;
496 struct list_head pending;
499 INIT_LIST_HEAD(&pending);
500 spin_lock(&cache_defer_lock);
502 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
503 if (dreq->owner == owner) {
504 list_del(&dreq->hash);
505 list_move(&dreq->recent, &pending);
506 cache_defer_cnt--;
509 spin_unlock(&cache_defer_lock);
511 while (!list_empty(&pending)) {
512 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
513 list_del_init(&dreq->recent);
514 dreq->revisit(dreq, 1);
519 * communicate with user-space
521 * We have a magic /proc file - /proc/sunrpc/cache
522 * On read, you get a full request, or block
523 * On write, an update request is processed
524 * Poll works if anything to read, and always allows write
526 * Implemented by linked list of requests. Each open file has
527 * a ->private that also exists in this list. New request are added
528 * to the end and may wakeup and preceding readers.
529 * New readers are added to the head. If, on read, an item is found with
530 * CACHE_UPCALLING clear, we free it from the list.
534 static DEFINE_SPINLOCK(queue_lock);
535 static DECLARE_MUTEX(queue_io_sem);
537 struct cache_queue {
538 struct list_head list;
539 int reader; /* if 0, then request */
541 struct cache_request {
542 struct cache_queue q;
543 struct cache_head *item;
544 char * buf;
545 int len;
546 int readers;
548 struct cache_reader {
549 struct cache_queue q;
550 int offset; /* if non-0, we have a refcnt on next request */
553 static ssize_t
554 cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
556 struct cache_reader *rp = filp->private_data;
557 struct cache_request *rq;
558 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
559 int err;
561 if (count == 0)
562 return 0;
564 down(&queue_io_sem); /* protect against multiple concurrent
565 * readers on this file */
566 again:
567 spin_lock(&queue_lock);
568 /* need to find next request */
569 while (rp->q.list.next != &cd->queue &&
570 list_entry(rp->q.list.next, struct cache_queue, list)
571 ->reader) {
572 struct list_head *next = rp->q.list.next;
573 list_move(&rp->q.list, next);
575 if (rp->q.list.next == &cd->queue) {
576 spin_unlock(&queue_lock);
577 up(&queue_io_sem);
578 BUG_ON(rp->offset);
579 return 0;
581 rq = container_of(rp->q.list.next, struct cache_request, q.list);
582 BUG_ON(rq->q.reader);
583 if (rp->offset == 0)
584 rq->readers++;
585 spin_unlock(&queue_lock);
587 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
588 err = -EAGAIN;
589 spin_lock(&queue_lock);
590 list_move(&rp->q.list, &rq->q.list);
591 spin_unlock(&queue_lock);
592 } else {
593 if (rp->offset + count > rq->len)
594 count = rq->len - rp->offset;
595 err = -EFAULT;
596 if (copy_to_user(buf, rq->buf + rp->offset, count))
597 goto out;
598 rp->offset += count;
599 if (rp->offset >= rq->len) {
600 rp->offset = 0;
601 spin_lock(&queue_lock);
602 list_move(&rp->q.list, &rq->q.list);
603 spin_unlock(&queue_lock);
605 err = 0;
607 out:
608 if (rp->offset == 0) {
609 /* need to release rq */
610 spin_lock(&queue_lock);
611 rq->readers--;
612 if (rq->readers == 0 &&
613 !test_bit(CACHE_PENDING, &rq->item->flags)) {
614 list_del(&rq->q.list);
615 spin_unlock(&queue_lock);
616 cd->cache_put(rq->item, cd);
617 kfree(rq->buf);
618 kfree(rq);
619 } else
620 spin_unlock(&queue_lock);
622 if (err == -EAGAIN)
623 goto again;
624 up(&queue_io_sem);
625 return err ? err : count;
628 static char write_buf[8192]; /* protected by queue_io_sem */
630 static ssize_t
631 cache_write(struct file *filp, const char __user *buf, size_t count,
632 loff_t *ppos)
634 int err;
635 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
637 if (count == 0)
638 return 0;
639 if (count >= sizeof(write_buf))
640 return -EINVAL;
642 down(&queue_io_sem);
644 if (copy_from_user(write_buf, buf, count)) {
645 up(&queue_io_sem);
646 return -EFAULT;
648 write_buf[count] = '\0';
649 if (cd->cache_parse)
650 err = cd->cache_parse(cd, write_buf, count);
651 else
652 err = -EINVAL;
654 up(&queue_io_sem);
655 return err ? err : count;
658 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
660 static unsigned int
661 cache_poll(struct file *filp, poll_table *wait)
663 unsigned int mask;
664 struct cache_reader *rp = filp->private_data;
665 struct cache_queue *cq;
666 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
668 poll_wait(filp, &queue_wait, wait);
670 /* alway allow write */
671 mask = POLL_OUT | POLLWRNORM;
673 if (!rp)
674 return mask;
676 spin_lock(&queue_lock);
678 for (cq= &rp->q; &cq->list != &cd->queue;
679 cq = list_entry(cq->list.next, struct cache_queue, list))
680 if (!cq->reader) {
681 mask |= POLLIN | POLLRDNORM;
682 break;
684 spin_unlock(&queue_lock);
685 return mask;
688 static int
689 cache_ioctl(struct inode *ino, struct file *filp,
690 unsigned int cmd, unsigned long arg)
692 int len = 0;
693 struct cache_reader *rp = filp->private_data;
694 struct cache_queue *cq;
695 struct cache_detail *cd = PDE(ino)->data;
697 if (cmd != FIONREAD || !rp)
698 return -EINVAL;
700 spin_lock(&queue_lock);
702 /* only find the length remaining in current request,
703 * or the length of the next request
705 for (cq= &rp->q; &cq->list != &cd->queue;
706 cq = list_entry(cq->list.next, struct cache_queue, list))
707 if (!cq->reader) {
708 struct cache_request *cr =
709 container_of(cq, struct cache_request, q);
710 len = cr->len - rp->offset;
711 break;
713 spin_unlock(&queue_lock);
715 return put_user(len, (int __user *)arg);
718 static int
719 cache_open(struct inode *inode, struct file *filp)
721 struct cache_reader *rp = NULL;
723 nonseekable_open(inode, filp);
724 if (filp->f_mode & FMODE_READ) {
725 struct cache_detail *cd = PDE(inode)->data;
727 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
728 if (!rp)
729 return -ENOMEM;
730 rp->offset = 0;
731 rp->q.reader = 1;
732 atomic_inc(&cd->readers);
733 spin_lock(&queue_lock);
734 list_add(&rp->q.list, &cd->queue);
735 spin_unlock(&queue_lock);
737 filp->private_data = rp;
738 return 0;
741 static int
742 cache_release(struct inode *inode, struct file *filp)
744 struct cache_reader *rp = filp->private_data;
745 struct cache_detail *cd = PDE(inode)->data;
747 if (rp) {
748 spin_lock(&queue_lock);
749 if (rp->offset) {
750 struct cache_queue *cq;
751 for (cq= &rp->q; &cq->list != &cd->queue;
752 cq = list_entry(cq->list.next, struct cache_queue, list))
753 if (!cq->reader) {
754 container_of(cq, struct cache_request, q)
755 ->readers--;
756 break;
758 rp->offset = 0;
760 list_del(&rp->q.list);
761 spin_unlock(&queue_lock);
763 filp->private_data = NULL;
764 kfree(rp);
766 cd->last_close = get_seconds();
767 atomic_dec(&cd->readers);
769 return 0;
774 static struct file_operations cache_file_operations = {
775 .owner = THIS_MODULE,
776 .llseek = no_llseek,
777 .read = cache_read,
778 .write = cache_write,
779 .poll = cache_poll,
780 .ioctl = cache_ioctl, /* for FIONREAD */
781 .open = cache_open,
782 .release = cache_release,
786 static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
788 struct cache_queue *cq;
789 spin_lock(&queue_lock);
790 list_for_each_entry(cq, &detail->queue, list)
791 if (!cq->reader) {
792 struct cache_request *cr = container_of(cq, struct cache_request, q);
793 if (cr->item != ch)
794 continue;
795 if (cr->readers != 0)
796 break;
797 list_del(&cr->q.list);
798 spin_unlock(&queue_lock);
799 detail->cache_put(cr->item, detail);
800 kfree(cr->buf);
801 kfree(cr);
802 return;
804 spin_unlock(&queue_lock);
808 * Support routines for text-based upcalls.
809 * Fields are separated by spaces.
810 * Fields are either mangled to quote space tab newline slosh with slosh
811 * or a hexified with a leading \x
812 * Record is terminated with newline.
816 void qword_add(char **bpp, int *lp, char *str)
818 char *bp = *bpp;
819 int len = *lp;
820 char c;
822 if (len < 0) return;
824 while ((c=*str++) && len)
825 switch(c) {
826 case ' ':
827 case '\t':
828 case '\n':
829 case '\\':
830 if (len >= 4) {
831 *bp++ = '\\';
832 *bp++ = '0' + ((c & 0300)>>6);
833 *bp++ = '0' + ((c & 0070)>>3);
834 *bp++ = '0' + ((c & 0007)>>0);
836 len -= 4;
837 break;
838 default:
839 *bp++ = c;
840 len--;
842 if (c || len <1) len = -1;
843 else {
844 *bp++ = ' ';
845 len--;
847 *bpp = bp;
848 *lp = len;
851 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
853 char *bp = *bpp;
854 int len = *lp;
856 if (len < 0) return;
858 if (len > 2) {
859 *bp++ = '\\';
860 *bp++ = 'x';
861 len -= 2;
862 while (blen && len >= 2) {
863 unsigned char c = *buf++;
864 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
865 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
866 len -= 2;
867 blen--;
870 if (blen || len<1) len = -1;
871 else {
872 *bp++ = ' ';
873 len--;
875 *bpp = bp;
876 *lp = len;
879 static void warn_no_listener(struct cache_detail *detail)
881 if (detail->last_warn != detail->last_close) {
882 detail->last_warn = detail->last_close;
883 if (detail->warn_no_listener)
884 detail->warn_no_listener(detail);
889 * register an upcall request to user-space.
890 * Each request is at most one page long.
892 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
895 char *buf;
896 struct cache_request *crq;
897 char *bp;
898 int len;
900 if (detail->cache_request == NULL)
901 return -EINVAL;
903 if (atomic_read(&detail->readers) == 0 &&
904 detail->last_close < get_seconds() - 30) {
905 warn_no_listener(detail);
906 return -EINVAL;
909 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
910 if (!buf)
911 return -EAGAIN;
913 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
914 if (!crq) {
915 kfree(buf);
916 return -EAGAIN;
919 bp = buf; len = PAGE_SIZE;
921 detail->cache_request(detail, h, &bp, &len);
923 if (len < 0) {
924 kfree(buf);
925 kfree(crq);
926 return -EAGAIN;
928 crq->q.reader = 0;
929 crq->item = cache_get(h);
930 crq->buf = buf;
931 crq->len = PAGE_SIZE - len;
932 crq->readers = 0;
933 spin_lock(&queue_lock);
934 list_add_tail(&crq->q.list, &detail->queue);
935 spin_unlock(&queue_lock);
936 wake_up(&queue_wait);
937 return 0;
941 * parse a message from user-space and pass it
942 * to an appropriate cache
943 * Messages are, like requests, separated into fields by
944 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
946 * Message is
947 * reply cachename expiry key ... content....
949 * key and content are both parsed by cache
952 #define isodigit(c) (isdigit(c) && c <= '7')
953 int qword_get(char **bpp, char *dest, int bufsize)
955 /* return bytes copied, or -1 on error */
956 char *bp = *bpp;
957 int len = 0;
959 while (*bp == ' ') bp++;
961 if (bp[0] == '\\' && bp[1] == 'x') {
962 /* HEX STRING */
963 bp += 2;
964 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
965 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
966 bp++;
967 byte <<= 4;
968 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
969 *dest++ = byte;
970 bp++;
971 len++;
973 } else {
974 /* text with \nnn octal quoting */
975 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
976 if (*bp == '\\' &&
977 isodigit(bp[1]) && (bp[1] <= '3') &&
978 isodigit(bp[2]) &&
979 isodigit(bp[3])) {
980 int byte = (*++bp -'0');
981 bp++;
982 byte = (byte << 3) | (*bp++ - '0');
983 byte = (byte << 3) | (*bp++ - '0');
984 *dest++ = byte;
985 len++;
986 } else {
987 *dest++ = *bp++;
988 len++;
993 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
994 return -1;
995 while (*bp == ' ') bp++;
996 *bpp = bp;
997 *dest = '\0';
998 return len;
1003 * support /proc/sunrpc/cache/$CACHENAME/content
1004 * as a seqfile.
1005 * We call ->cache_show passing NULL for the item to
1006 * get a header, then pass each real item in the cache
1009 struct handle {
1010 struct cache_detail *cd;
1013 static void *c_start(struct seq_file *m, loff_t *pos)
1015 loff_t n = *pos;
1016 unsigned hash, entry;
1017 struct cache_head *ch;
1018 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1021 read_lock(&cd->hash_lock);
1022 if (!n--)
1023 return SEQ_START_TOKEN;
1024 hash = n >> 32;
1025 entry = n & ((1LL<<32) - 1);
1027 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1028 if (!entry--)
1029 return ch;
1030 n &= ~((1LL<<32) - 1);
1031 do {
1032 hash++;
1033 n += 1LL<<32;
1034 } while(hash < cd->hash_size &&
1035 cd->hash_table[hash]==NULL);
1036 if (hash >= cd->hash_size)
1037 return NULL;
1038 *pos = n+1;
1039 return cd->hash_table[hash];
1042 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1044 struct cache_head *ch = p;
1045 int hash = (*pos >> 32);
1046 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1048 if (p == SEQ_START_TOKEN)
1049 hash = 0;
1050 else if (ch->next == NULL) {
1051 hash++;
1052 *pos += 1LL<<32;
1053 } else {
1054 ++*pos;
1055 return ch->next;
1057 *pos &= ~((1LL<<32) - 1);
1058 while (hash < cd->hash_size &&
1059 cd->hash_table[hash] == NULL) {
1060 hash++;
1061 *pos += 1LL<<32;
1063 if (hash >= cd->hash_size)
1064 return NULL;
1065 ++*pos;
1066 return cd->hash_table[hash];
1069 static void c_stop(struct seq_file *m, void *p)
1071 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1072 read_unlock(&cd->hash_lock);
1075 static int c_show(struct seq_file *m, void *p)
1077 struct cache_head *cp = p;
1078 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1080 if (p == SEQ_START_TOKEN)
1081 return cd->cache_show(m, cd, NULL);
1083 ifdebug(CACHE)
1084 seq_printf(m, "# expiry=%ld refcnt=%d\n",
1085 cp->expiry_time, atomic_read(&cp->refcnt));
1086 cache_get(cp);
1087 if (cache_check(cd, cp, NULL))
1088 /* cache_check does a cache_put on failure */
1089 seq_printf(m, "# ");
1090 else
1091 cache_put(cp, cd);
1093 return cd->cache_show(m, cd, cp);
1096 static struct seq_operations cache_content_op = {
1097 .start = c_start,
1098 .next = c_next,
1099 .stop = c_stop,
1100 .show = c_show,
1103 static int content_open(struct inode *inode, struct file *file)
1105 int res;
1106 struct handle *han;
1107 struct cache_detail *cd = PDE(inode)->data;
1109 han = kmalloc(sizeof(*han), GFP_KERNEL);
1110 if (han == NULL)
1111 return -ENOMEM;
1113 han->cd = cd;
1115 res = seq_open(file, &cache_content_op);
1116 if (res)
1117 kfree(han);
1118 else
1119 ((struct seq_file *)file->private_data)->private = han;
1121 return res;
1123 static int content_release(struct inode *inode, struct file *file)
1125 struct seq_file *m = (struct seq_file *)file->private_data;
1126 struct handle *han = m->private;
1127 kfree(han);
1128 m->private = NULL;
1129 return seq_release(inode, file);
1132 static struct file_operations content_file_operations = {
1133 .open = content_open,
1134 .read = seq_read,
1135 .llseek = seq_lseek,
1136 .release = content_release,
1139 static ssize_t read_flush(struct file *file, char __user *buf,
1140 size_t count, loff_t *ppos)
1142 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1143 char tbuf[20];
1144 unsigned long p = *ppos;
1145 int len;
1147 sprintf(tbuf, "%lu\n", cd->flush_time);
1148 len = strlen(tbuf);
1149 if (p >= len)
1150 return 0;
1151 len -= p;
1152 if (len > count) len = count;
1153 if (copy_to_user(buf, (void*)(tbuf+p), len))
1154 len = -EFAULT;
1155 else
1156 *ppos += len;
1157 return len;
1160 static ssize_t write_flush(struct file * file, const char __user * buf,
1161 size_t count, loff_t *ppos)
1163 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1164 char tbuf[20];
1165 char *ep;
1166 long flushtime;
1167 if (*ppos || count > sizeof(tbuf)-1)
1168 return -EINVAL;
1169 if (copy_from_user(tbuf, buf, count))
1170 return -EFAULT;
1171 tbuf[count] = 0;
1172 flushtime = simple_strtoul(tbuf, &ep, 0);
1173 if (*ep && *ep != '\n')
1174 return -EINVAL;
1176 cd->flush_time = flushtime;
1177 cd->nextcheck = get_seconds();
1178 cache_flush();
1180 *ppos += count;
1181 return count;
1184 static struct file_operations cache_flush_operations = {
1185 .open = nonseekable_open,
1186 .read = read_flush,
1187 .write = write_flush,