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>
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 void cache_defer_req(struct cache_req *req, struct cache_head *item);
38 static void cache_revisit_request(struct cache_head *item);
40 void cache_init(struct cache_head *h)
42 time_t now = get_seconds();
45 atomic_set(&h->refcnt, 1);
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)) {
64 read_unlock(&detail->hash_lock);
68 read_unlock(&detail->hash_lock);
69 /* Didn't find anything, insert an empty entry */
71 new = detail->alloc();
76 write_lock(&detail->hash_lock);
78 /* check if entry appeared while we slept */
79 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
80 struct cache_head *tmp = *hp;
81 if (detail->match(tmp, key)) {
83 write_unlock(&detail->hash_lock);
84 detail->cache_put(new, detail);
88 detail->init(new, key);
93 write_unlock(&detail->hash_lock);
97 EXPORT_SYMBOL(sunrpc_cache_lookup);
99 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
100 struct cache_head *new, struct cache_head *old, int hash)
102 /* The 'old' entry is to be replaced by 'new'.
103 * If 'old' is not VALID, we update it directly,
104 * otherwise we need to replace it
106 struct cache_head **head;
107 struct cache_head *tmp;
109 if (!test_bit(CACHE_VALID, &old->flags)) {
110 write_lock(&detail->hash_lock);
111 if (!test_bit(CACHE_VALID, &old->flags)) {
112 if (test_bit(CACHE_NEGATIVE, &new->flags))
113 set_bit(CACHE_NEGATIVE, &old->flags);
115 detail->update(old, new);
116 /* FIXME cache_fresh should come first */
117 write_unlock(&detail->hash_lock);
118 cache_fresh(detail, old, new->expiry_time);
121 write_unlock(&detail->hash_lock);
123 /* We need to insert a new entry */
124 tmp = detail->alloc();
126 detail->cache_put(old, detail);
130 detail->init(tmp, old);
131 head = &detail->hash_table[hash];
133 write_lock(&detail->hash_lock);
134 if (test_bit(CACHE_NEGATIVE, &new->flags))
135 set_bit(CACHE_NEGATIVE, &tmp->flags);
137 detail->update(tmp, new);
141 write_unlock(&detail->hash_lock);
142 cache_fresh(detail, tmp, new->expiry_time);
143 cache_fresh(detail, old, 0);
144 detail->cache_put(old, detail);
147 EXPORT_SYMBOL(sunrpc_cache_update);
149 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
151 * This is the generic cache management routine for all
152 * the authentication caches.
153 * It checks the currency of a cache item and will (later)
154 * initiate an upcall to fill it if needed.
157 * Returns 0 if the cache_head can be used, or cache_puts it and returns
158 * -EAGAIN if upcall is pending,
159 * -ENOENT if cache entry was negative
161 int cache_check(struct cache_detail *detail,
162 struct cache_head *h, struct cache_req *rqstp)
165 long refresh_age, age;
167 /* First decide return status as best we can */
168 if (!test_bit(CACHE_VALID, &h->flags) ||
169 h->expiry_time < get_seconds())
171 else if (detail->flush_time > h->last_refresh)
175 if (test_bit(CACHE_NEGATIVE, &h->flags))
180 /* now see if we want to start an upcall */
181 refresh_age = (h->expiry_time - h->last_refresh);
182 age = get_seconds() - h->last_refresh;
187 } else if (rv == -EAGAIN || age > refresh_age/2) {
188 dprintk("Want update, refage=%ld, age=%ld\n", refresh_age, age);
189 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
190 switch (cache_make_upcall(detail, h)) {
192 clear_bit(CACHE_PENDING, &h->flags);
194 set_bit(CACHE_NEGATIVE, &h->flags);
195 cache_fresh(detail, h, get_seconds()+CACHE_NEW_EXPIRY);
201 clear_bit(CACHE_PENDING, &h->flags);
202 cache_revisit_request(h);
209 cache_defer_req(rqstp, h);
212 detail->cache_put(h, detail);
216 static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
218 void cache_fresh(struct cache_detail *detail,
219 struct cache_head *head, time_t expiry)
222 head->expiry_time = expiry;
223 head->last_refresh = get_seconds();
224 if (!test_and_set_bit(CACHE_VALID, &head->flags))
225 cache_revisit_request(head);
226 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
227 cache_revisit_request(head);
228 queue_loose(detail, head);
233 * caches need to be periodically cleaned.
234 * For this we maintain a list of cache_detail and
235 * a current pointer into that list and into the table
238 * Each time clean_cache is called it finds the next non-empty entry
239 * in the current table and walks the list in that entry
240 * looking for entries that can be removed.
242 * An entry gets removed if:
243 * - The expiry is before current time
244 * - The last_refresh time is before the flush_time for that cache
246 * later we might drop old entries with non-NEVER expiry if that table
247 * is getting 'full' for some definition of 'full'
249 * The question of "how often to scan a table" is an interesting one
250 * and is answered in part by the use of the "nextcheck" field in the
252 * When a scan of a table begins, the nextcheck field is set to a time
253 * that is well into the future.
254 * While scanning, if an expiry time is found that is earlier than the
255 * current nextcheck time, nextcheck is set to that expiry time.
256 * If the flush_time is ever set to a time earlier than the nextcheck
257 * time, the nextcheck time is then set to that flush_time.
259 * A table is then only scanned if the current time is at least
260 * the nextcheck time.
264 static LIST_HEAD(cache_list);
265 static DEFINE_SPINLOCK(cache_list_lock);
266 static struct cache_detail *current_detail;
267 static int current_index;
269 static struct file_operations cache_file_operations;
270 static struct file_operations content_file_operations;
271 static struct file_operations cache_flush_operations;
273 static void do_cache_clean(void *data);
274 static DECLARE_WORK(cache_cleaner, do_cache_clean, NULL);
276 void cache_register(struct cache_detail *cd)
278 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
280 struct proc_dir_entry *p;
281 cd->proc_ent->owner = cd->owner;
282 cd->channel_ent = cd->content_ent = NULL;
284 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR,
288 p->proc_fops = &cache_flush_operations;
289 p->owner = cd->owner;
293 if (cd->cache_request || cd->cache_parse) {
294 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
298 p->proc_fops = &cache_file_operations;
299 p->owner = cd->owner;
303 if (cd->cache_show) {
304 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
308 p->proc_fops = &content_file_operations;
309 p->owner = cd->owner;
314 rwlock_init(&cd->hash_lock);
315 INIT_LIST_HEAD(&cd->queue);
316 spin_lock(&cache_list_lock);
319 atomic_set(&cd->readers, 0);
322 list_add(&cd->others, &cache_list);
323 spin_unlock(&cache_list_lock);
325 /* start the cleaning process */
326 schedule_work(&cache_cleaner);
329 int cache_unregister(struct cache_detail *cd)
332 spin_lock(&cache_list_lock);
333 write_lock(&cd->hash_lock);
334 if (cd->entries || atomic_read(&cd->inuse)) {
335 write_unlock(&cd->hash_lock);
336 spin_unlock(&cache_list_lock);
339 if (current_detail == cd)
340 current_detail = NULL;
341 list_del_init(&cd->others);
342 write_unlock(&cd->hash_lock);
343 spin_unlock(&cache_list_lock);
346 remove_proc_entry("flush", cd->proc_ent);
348 remove_proc_entry("channel", cd->proc_ent);
350 remove_proc_entry("content", cd->proc_ent);
353 remove_proc_entry(cd->name, proc_net_rpc);
355 if (list_empty(&cache_list)) {
356 /* module must be being unloaded so its safe to kill the worker */
357 cancel_delayed_work(&cache_cleaner);
358 flush_scheduled_work();
363 /* clean cache tries to find something to clean
365 * It returns 1 if it cleaned something,
366 * 0 if it didn't find anything this time
367 * -1 if it fell off the end of the list.
369 static int cache_clean(void)
372 struct list_head *next;
374 spin_lock(&cache_list_lock);
376 /* find a suitable table if we don't already have one */
377 while (current_detail == NULL ||
378 current_index >= current_detail->hash_size) {
380 next = current_detail->others.next;
382 next = cache_list.next;
383 if (next == &cache_list) {
384 current_detail = NULL;
385 spin_unlock(&cache_list_lock);
388 current_detail = list_entry(next, struct cache_detail, others);
389 if (current_detail->nextcheck > get_seconds())
390 current_index = current_detail->hash_size;
393 current_detail->nextcheck = get_seconds()+30*60;
397 /* find a non-empty bucket in the table */
398 while (current_detail &&
399 current_index < current_detail->hash_size &&
400 current_detail->hash_table[current_index] == NULL)
403 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
405 if (current_detail && current_index < current_detail->hash_size) {
406 struct cache_head *ch, **cp;
407 struct cache_detail *d;
409 write_lock(¤t_detail->hash_lock);
411 /* Ok, now to clean this strand */
413 cp = & current_detail->hash_table[current_index];
415 for (; ch; cp= & ch->next, ch= *cp) {
416 if (current_detail->nextcheck > ch->expiry_time)
417 current_detail->nextcheck = ch->expiry_time+1;
418 if (ch->expiry_time >= get_seconds()
419 && ch->last_refresh >= current_detail->flush_time
422 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
423 queue_loose(current_detail, ch);
425 if (atomic_read(&ch->refcnt) == 1)
431 current_detail->entries--;
434 write_unlock(¤t_detail->hash_lock);
438 spin_unlock(&cache_list_lock);
442 spin_unlock(&cache_list_lock);
448 * We want to regularly clean the cache, so we need to schedule some work ...
450 static void do_cache_clean(void *data)
453 if (cache_clean() == -1)
456 if (list_empty(&cache_list))
460 schedule_delayed_work(&cache_cleaner, delay);
465 * Clean all caches promptly. This just calls cache_clean
466 * repeatedly until we are sure that every cache has had a chance to
469 void cache_flush(void)
471 while (cache_clean() != -1)
473 while (cache_clean() != -1)
477 void cache_purge(struct cache_detail *detail)
479 detail->flush_time = LONG_MAX;
480 detail->nextcheck = get_seconds();
482 detail->flush_time = 1;
488 * Deferral and Revisiting of Requests.
490 * If a cache lookup finds a pending entry, we
491 * need to defer the request and revisit it later.
492 * All deferred requests are stored in a hash table,
493 * indexed by "struct cache_head *".
494 * As it may be wasteful to store a whole request
495 * structure, we allow the request to provide a
496 * deferred form, which must contain a
497 * 'struct cache_deferred_req'
498 * This cache_deferred_req contains a method to allow
499 * it to be revisited when cache info is available
502 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
503 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
505 #define DFR_MAX 300 /* ??? */
507 static DEFINE_SPINLOCK(cache_defer_lock);
508 static LIST_HEAD(cache_defer_list);
509 static struct list_head cache_defer_hash[DFR_HASHSIZE];
510 static int cache_defer_cnt;
512 static void cache_defer_req(struct cache_req *req, struct cache_head *item)
514 struct cache_deferred_req *dreq;
515 int hash = DFR_HASH(item);
517 dreq = req->defer(req);
522 dreq->recv_time = get_seconds();
524 spin_lock(&cache_defer_lock);
526 list_add(&dreq->recent, &cache_defer_list);
528 if (cache_defer_hash[hash].next == NULL)
529 INIT_LIST_HEAD(&cache_defer_hash[hash]);
530 list_add(&dreq->hash, &cache_defer_hash[hash]);
532 /* it is in, now maybe clean up */
534 if (++cache_defer_cnt > DFR_MAX) {
535 /* too much in the cache, randomly drop
539 dreq = list_entry(cache_defer_list.next,
540 struct cache_deferred_req,
543 dreq = list_entry(cache_defer_list.prev,
544 struct cache_deferred_req,
546 list_del(&dreq->recent);
547 list_del(&dreq->hash);
550 spin_unlock(&cache_defer_lock);
553 /* there was one too many */
554 dreq->revisit(dreq, 1);
556 if (!test_bit(CACHE_PENDING, &item->flags)) {
557 /* must have just been validated... */
558 cache_revisit_request(item);
562 static void cache_revisit_request(struct cache_head *item)
564 struct cache_deferred_req *dreq;
565 struct list_head pending;
567 struct list_head *lp;
568 int hash = DFR_HASH(item);
570 INIT_LIST_HEAD(&pending);
571 spin_lock(&cache_defer_lock);
573 lp = cache_defer_hash[hash].next;
575 while (lp != &cache_defer_hash[hash]) {
576 dreq = list_entry(lp, struct cache_deferred_req, hash);
578 if (dreq->item == item) {
579 list_del(&dreq->hash);
580 list_move(&dreq->recent, &pending);
585 spin_unlock(&cache_defer_lock);
587 while (!list_empty(&pending)) {
588 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
589 list_del_init(&dreq->recent);
590 dreq->revisit(dreq, 0);
594 void cache_clean_deferred(void *owner)
596 struct cache_deferred_req *dreq, *tmp;
597 struct list_head pending;
600 INIT_LIST_HEAD(&pending);
601 spin_lock(&cache_defer_lock);
603 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
604 if (dreq->owner == owner) {
605 list_del(&dreq->hash);
606 list_move(&dreq->recent, &pending);
610 spin_unlock(&cache_defer_lock);
612 while (!list_empty(&pending)) {
613 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
614 list_del_init(&dreq->recent);
615 dreq->revisit(dreq, 1);
620 * communicate with user-space
622 * We have a magic /proc file - /proc/sunrpc/cache
623 * On read, you get a full request, or block
624 * On write, an update request is processed
625 * Poll works if anything to read, and always allows write
627 * Implemented by linked list of requests. Each open file has
628 * a ->private that also exists in this list. New request are added
629 * to the end and may wakeup and preceding readers.
630 * New readers are added to the head. If, on read, an item is found with
631 * CACHE_UPCALLING clear, we free it from the list.
635 static DEFINE_SPINLOCK(queue_lock);
636 static DEFINE_MUTEX(queue_io_mutex);
639 struct list_head list;
640 int reader; /* if 0, then request */
642 struct cache_request {
643 struct cache_queue q;
644 struct cache_head *item;
649 struct cache_reader {
650 struct cache_queue q;
651 int offset; /* if non-0, we have a refcnt on next request */
655 cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
657 struct cache_reader *rp = filp->private_data;
658 struct cache_request *rq;
659 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
665 mutex_lock(&queue_io_mutex); /* protect against multiple concurrent
666 * readers on this file */
668 spin_lock(&queue_lock);
669 /* need to find next request */
670 while (rp->q.list.next != &cd->queue &&
671 list_entry(rp->q.list.next, struct cache_queue, list)
673 struct list_head *next = rp->q.list.next;
674 list_move(&rp->q.list, next);
676 if (rp->q.list.next == &cd->queue) {
677 spin_unlock(&queue_lock);
678 mutex_unlock(&queue_io_mutex);
682 rq = container_of(rp->q.list.next, struct cache_request, q.list);
683 BUG_ON(rq->q.reader);
686 spin_unlock(&queue_lock);
688 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
690 spin_lock(&queue_lock);
691 list_move(&rp->q.list, &rq->q.list);
692 spin_unlock(&queue_lock);
694 if (rp->offset + count > rq->len)
695 count = rq->len - rp->offset;
697 if (copy_to_user(buf, rq->buf + rp->offset, count))
700 if (rp->offset >= rq->len) {
702 spin_lock(&queue_lock);
703 list_move(&rp->q.list, &rq->q.list);
704 spin_unlock(&queue_lock);
709 if (rp->offset == 0) {
710 /* need to release rq */
711 spin_lock(&queue_lock);
713 if (rq->readers == 0 &&
714 !test_bit(CACHE_PENDING, &rq->item->flags)) {
715 list_del(&rq->q.list);
716 spin_unlock(&queue_lock);
717 cd->cache_put(rq->item, cd);
721 spin_unlock(&queue_lock);
725 mutex_unlock(&queue_io_mutex);
726 return err ? err : count;
729 static char write_buf[8192]; /* protected by queue_io_mutex */
732 cache_write(struct file *filp, const char __user *buf, size_t count,
736 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
740 if (count >= sizeof(write_buf))
743 mutex_lock(&queue_io_mutex);
745 if (copy_from_user(write_buf, buf, count)) {
746 mutex_unlock(&queue_io_mutex);
749 write_buf[count] = '\0';
751 err = cd->cache_parse(cd, write_buf, count);
755 mutex_unlock(&queue_io_mutex);
756 return err ? err : count;
759 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
762 cache_poll(struct file *filp, poll_table *wait)
765 struct cache_reader *rp = filp->private_data;
766 struct cache_queue *cq;
767 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
769 poll_wait(filp, &queue_wait, wait);
771 /* alway allow write */
772 mask = POLL_OUT | POLLWRNORM;
777 spin_lock(&queue_lock);
779 for (cq= &rp->q; &cq->list != &cd->queue;
780 cq = list_entry(cq->list.next, struct cache_queue, list))
782 mask |= POLLIN | POLLRDNORM;
785 spin_unlock(&queue_lock);
790 cache_ioctl(struct inode *ino, struct file *filp,
791 unsigned int cmd, unsigned long arg)
794 struct cache_reader *rp = filp->private_data;
795 struct cache_queue *cq;
796 struct cache_detail *cd = PDE(ino)->data;
798 if (cmd != FIONREAD || !rp)
801 spin_lock(&queue_lock);
803 /* only find the length remaining in current request,
804 * or the length of the next request
806 for (cq= &rp->q; &cq->list != &cd->queue;
807 cq = list_entry(cq->list.next, struct cache_queue, list))
809 struct cache_request *cr =
810 container_of(cq, struct cache_request, q);
811 len = cr->len - rp->offset;
814 spin_unlock(&queue_lock);
816 return put_user(len, (int __user *)arg);
820 cache_open(struct inode *inode, struct file *filp)
822 struct cache_reader *rp = NULL;
824 nonseekable_open(inode, filp);
825 if (filp->f_mode & FMODE_READ) {
826 struct cache_detail *cd = PDE(inode)->data;
828 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
833 atomic_inc(&cd->readers);
834 spin_lock(&queue_lock);
835 list_add(&rp->q.list, &cd->queue);
836 spin_unlock(&queue_lock);
838 filp->private_data = rp;
843 cache_release(struct inode *inode, struct file *filp)
845 struct cache_reader *rp = filp->private_data;
846 struct cache_detail *cd = PDE(inode)->data;
849 spin_lock(&queue_lock);
851 struct cache_queue *cq;
852 for (cq= &rp->q; &cq->list != &cd->queue;
853 cq = list_entry(cq->list.next, struct cache_queue, list))
855 container_of(cq, struct cache_request, q)
861 list_del(&rp->q.list);
862 spin_unlock(&queue_lock);
864 filp->private_data = NULL;
867 cd->last_close = get_seconds();
868 atomic_dec(&cd->readers);
875 static struct file_operations cache_file_operations = {
876 .owner = THIS_MODULE,
879 .write = cache_write,
881 .ioctl = cache_ioctl, /* for FIONREAD */
883 .release = cache_release,
887 static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
889 struct cache_queue *cq;
890 spin_lock(&queue_lock);
891 list_for_each_entry(cq, &detail->queue, list)
893 struct cache_request *cr = container_of(cq, struct cache_request, q);
896 if (cr->readers != 0)
898 list_del(&cr->q.list);
899 spin_unlock(&queue_lock);
900 detail->cache_put(cr->item, detail);
905 spin_unlock(&queue_lock);
909 * Support routines for text-based upcalls.
910 * Fields are separated by spaces.
911 * Fields are either mangled to quote space tab newline slosh with slosh
912 * or a hexified with a leading \x
913 * Record is terminated with newline.
917 void qword_add(char **bpp, int *lp, char *str)
925 while ((c=*str++) && len)
933 *bp++ = '0' + ((c & 0300)>>6);
934 *bp++ = '0' + ((c & 0070)>>3);
935 *bp++ = '0' + ((c & 0007)>>0);
943 if (c || len <1) len = -1;
952 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
963 while (blen && len >= 2) {
964 unsigned char c = *buf++;
965 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
966 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
971 if (blen || len<1) len = -1;
980 static void warn_no_listener(struct cache_detail *detail)
982 if (detail->last_warn != detail->last_close) {
983 detail->last_warn = detail->last_close;
984 if (detail->warn_no_listener)
985 detail->warn_no_listener(detail);
990 * register an upcall request to user-space.
991 * Each request is at most one page long.
993 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
997 struct cache_request *crq;
1001 if (detail->cache_request == NULL)
1004 if (atomic_read(&detail->readers) == 0 &&
1005 detail->last_close < get_seconds() - 30) {
1006 warn_no_listener(detail);
1010 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1014 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1020 bp = buf; len = PAGE_SIZE;
1022 detail->cache_request(detail, h, &bp, &len);
1030 crq->item = cache_get(h);
1032 crq->len = PAGE_SIZE - len;
1034 spin_lock(&queue_lock);
1035 list_add_tail(&crq->q.list, &detail->queue);
1036 spin_unlock(&queue_lock);
1037 wake_up(&queue_wait);
1042 * parse a message from user-space and pass it
1043 * to an appropriate cache
1044 * Messages are, like requests, separated into fields by
1045 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1048 * reply cachename expiry key ... content....
1050 * key and content are both parsed by cache
1053 #define isodigit(c) (isdigit(c) && c <= '7')
1054 int qword_get(char **bpp, char *dest, int bufsize)
1056 /* return bytes copied, or -1 on error */
1060 while (*bp == ' ') bp++;
1062 if (bp[0] == '\\' && bp[1] == 'x') {
1065 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1066 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1069 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1075 /* text with \nnn octal quoting */
1076 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1078 isodigit(bp[1]) && (bp[1] <= '3') &&
1081 int byte = (*++bp -'0');
1083 byte = (byte << 3) | (*bp++ - '0');
1084 byte = (byte << 3) | (*bp++ - '0');
1094 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1096 while (*bp == ' ') bp++;
1104 * support /proc/sunrpc/cache/$CACHENAME/content
1106 * We call ->cache_show passing NULL for the item to
1107 * get a header, then pass each real item in the cache
1111 struct cache_detail *cd;
1114 static void *c_start(struct seq_file *m, loff_t *pos)
1117 unsigned hash, entry;
1118 struct cache_head *ch;
1119 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1122 read_lock(&cd->hash_lock);
1124 return SEQ_START_TOKEN;
1126 entry = n & ((1LL<<32) - 1);
1128 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1131 n &= ~((1LL<<32) - 1);
1135 } while(hash < cd->hash_size &&
1136 cd->hash_table[hash]==NULL);
1137 if (hash >= cd->hash_size)
1140 return cd->hash_table[hash];
1143 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1145 struct cache_head *ch = p;
1146 int hash = (*pos >> 32);
1147 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1149 if (p == SEQ_START_TOKEN)
1151 else if (ch->next == NULL) {
1158 *pos &= ~((1LL<<32) - 1);
1159 while (hash < cd->hash_size &&
1160 cd->hash_table[hash] == NULL) {
1164 if (hash >= cd->hash_size)
1167 return cd->hash_table[hash];
1170 static void c_stop(struct seq_file *m, void *p)
1172 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1173 read_unlock(&cd->hash_lock);
1176 static int c_show(struct seq_file *m, void *p)
1178 struct cache_head *cp = p;
1179 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1181 if (p == SEQ_START_TOKEN)
1182 return cd->cache_show(m, cd, NULL);
1185 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1186 cp->expiry_time, atomic_read(&cp->refcnt), cp->flags);
1188 if (cache_check(cd, cp, NULL))
1189 /* cache_check does a cache_put on failure */
1190 seq_printf(m, "# ");
1194 return cd->cache_show(m, cd, cp);
1197 static struct seq_operations cache_content_op = {
1204 static int content_open(struct inode *inode, struct file *file)
1208 struct cache_detail *cd = PDE(inode)->data;
1210 han = kmalloc(sizeof(*han), GFP_KERNEL);
1216 res = seq_open(file, &cache_content_op);
1220 ((struct seq_file *)file->private_data)->private = han;
1224 static int content_release(struct inode *inode, struct file *file)
1226 struct seq_file *m = (struct seq_file *)file->private_data;
1227 struct handle *han = m->private;
1230 return seq_release(inode, file);
1233 static struct file_operations content_file_operations = {
1234 .open = content_open,
1236 .llseek = seq_lseek,
1237 .release = content_release,
1240 static ssize_t read_flush(struct file *file, char __user *buf,
1241 size_t count, loff_t *ppos)
1243 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1245 unsigned long p = *ppos;
1248 sprintf(tbuf, "%lu\n", cd->flush_time);
1253 if (len > count) len = count;
1254 if (copy_to_user(buf, (void*)(tbuf+p), len))
1261 static ssize_t write_flush(struct file * file, const char __user * buf,
1262 size_t count, loff_t *ppos)
1264 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1268 if (*ppos || count > sizeof(tbuf)-1)
1270 if (copy_from_user(tbuf, buf, count))
1273 flushtime = simple_strtoul(tbuf, &ep, 0);
1274 if (*ep && *ep != '\n')
1277 cd->flush_time = flushtime;
1278 cd->nextcheck = get_seconds();
1285 static struct file_operations cache_flush_operations = {
1286 .open = nonseekable_open,
1288 .write = write_flush,