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 queue_loose(detail, head);
231 * caches need to be periodically cleaned.
232 * For this we maintain a list of cache_detail and
233 * a current pointer into that list and into the table
236 * Each time clean_cache is called it finds the next non-empty entry
237 * in the current table and walks the list in that entry
238 * looking for entries that can be removed.
240 * An entry gets removed if:
241 * - The expiry is before current time
242 * - The last_refresh time is before the flush_time for that cache
244 * later we might drop old entries with non-NEVER expiry if that table
245 * is getting 'full' for some definition of 'full'
247 * The question of "how often to scan a table" is an interesting one
248 * and is answered in part by the use of the "nextcheck" field in the
250 * When a scan of a table begins, the nextcheck field is set to a time
251 * that is well into the future.
252 * While scanning, if an expiry time is found that is earlier than the
253 * current nextcheck time, nextcheck is set to that expiry time.
254 * If the flush_time is ever set to a time earlier than the nextcheck
255 * time, the nextcheck time is then set to that flush_time.
257 * A table is then only scanned if the current time is at least
258 * the nextcheck time.
262 static LIST_HEAD(cache_list);
263 static DEFINE_SPINLOCK(cache_list_lock);
264 static struct cache_detail *current_detail;
265 static int current_index;
267 static struct file_operations cache_file_operations;
268 static struct file_operations content_file_operations;
269 static struct file_operations cache_flush_operations;
271 static void do_cache_clean(void *data);
272 static DECLARE_WORK(cache_cleaner, do_cache_clean, NULL);
274 void cache_register(struct cache_detail *cd)
276 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
278 struct proc_dir_entry *p;
279 cd->proc_ent->owner = cd->owner;
280 cd->channel_ent = cd->content_ent = NULL;
282 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR,
286 p->proc_fops = &cache_flush_operations;
287 p->owner = cd->owner;
291 if (cd->cache_request || cd->cache_parse) {
292 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
296 p->proc_fops = &cache_file_operations;
297 p->owner = cd->owner;
301 if (cd->cache_show) {
302 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
306 p->proc_fops = &content_file_operations;
307 p->owner = cd->owner;
312 rwlock_init(&cd->hash_lock);
313 INIT_LIST_HEAD(&cd->queue);
314 spin_lock(&cache_list_lock);
317 atomic_set(&cd->readers, 0);
320 list_add(&cd->others, &cache_list);
321 spin_unlock(&cache_list_lock);
323 /* start the cleaning process */
324 schedule_work(&cache_cleaner);
327 int cache_unregister(struct cache_detail *cd)
330 spin_lock(&cache_list_lock);
331 write_lock(&cd->hash_lock);
332 if (cd->entries || atomic_read(&cd->inuse)) {
333 write_unlock(&cd->hash_lock);
334 spin_unlock(&cache_list_lock);
337 if (current_detail == cd)
338 current_detail = NULL;
339 list_del_init(&cd->others);
340 write_unlock(&cd->hash_lock);
341 spin_unlock(&cache_list_lock);
344 remove_proc_entry("flush", cd->proc_ent);
346 remove_proc_entry("channel", cd->proc_ent);
348 remove_proc_entry("content", cd->proc_ent);
351 remove_proc_entry(cd->name, proc_net_rpc);
353 if (list_empty(&cache_list)) {
354 /* module must be being unloaded so its safe to kill the worker */
355 cancel_delayed_work(&cache_cleaner);
356 flush_scheduled_work();
361 /* clean cache tries to find something to clean
363 * It returns 1 if it cleaned something,
364 * 0 if it didn't find anything this time
365 * -1 if it fell off the end of the list.
367 static int cache_clean(void)
370 struct list_head *next;
372 spin_lock(&cache_list_lock);
374 /* find a suitable table if we don't already have one */
375 while (current_detail == NULL ||
376 current_index >= current_detail->hash_size) {
378 next = current_detail->others.next;
380 next = cache_list.next;
381 if (next == &cache_list) {
382 current_detail = NULL;
383 spin_unlock(&cache_list_lock);
386 current_detail = list_entry(next, struct cache_detail, others);
387 if (current_detail->nextcheck > get_seconds())
388 current_index = current_detail->hash_size;
391 current_detail->nextcheck = get_seconds()+30*60;
395 /* find a non-empty bucket in the table */
396 while (current_detail &&
397 current_index < current_detail->hash_size &&
398 current_detail->hash_table[current_index] == NULL)
401 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
403 if (current_detail && current_index < current_detail->hash_size) {
404 struct cache_head *ch, **cp;
405 struct cache_detail *d;
407 write_lock(¤t_detail->hash_lock);
409 /* Ok, now to clean this strand */
411 cp = & current_detail->hash_table[current_index];
413 for (; ch; cp= & ch->next, ch= *cp) {
414 if (current_detail->nextcheck > ch->expiry_time)
415 current_detail->nextcheck = ch->expiry_time+1;
416 if (ch->expiry_time >= get_seconds()
417 && ch->last_refresh >= current_detail->flush_time
420 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
421 queue_loose(current_detail, ch);
423 if (atomic_read(&ch->refcnt) == 1)
429 current_detail->entries--;
432 write_unlock(¤t_detail->hash_lock);
436 spin_unlock(&cache_list_lock);
440 spin_unlock(&cache_list_lock);
446 * We want to regularly clean the cache, so we need to schedule some work ...
448 static void do_cache_clean(void *data)
451 if (cache_clean() == -1)
454 if (list_empty(&cache_list))
458 schedule_delayed_work(&cache_cleaner, delay);
463 * Clean all caches promptly. This just calls cache_clean
464 * repeatedly until we are sure that every cache has had a chance to
467 void cache_flush(void)
469 while (cache_clean() != -1)
471 while (cache_clean() != -1)
475 void cache_purge(struct cache_detail *detail)
477 detail->flush_time = LONG_MAX;
478 detail->nextcheck = get_seconds();
480 detail->flush_time = 1;
486 * Deferral and Revisiting of Requests.
488 * If a cache lookup finds a pending entry, we
489 * need to defer the request and revisit it later.
490 * All deferred requests are stored in a hash table,
491 * indexed by "struct cache_head *".
492 * As it may be wasteful to store a whole request
493 * structure, we allow the request to provide a
494 * deferred form, which must contain a
495 * 'struct cache_deferred_req'
496 * This cache_deferred_req contains a method to allow
497 * it to be revisited when cache info is available
500 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
501 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
503 #define DFR_MAX 300 /* ??? */
505 static DEFINE_SPINLOCK(cache_defer_lock);
506 static LIST_HEAD(cache_defer_list);
507 static struct list_head cache_defer_hash[DFR_HASHSIZE];
508 static int cache_defer_cnt;
510 static void cache_defer_req(struct cache_req *req, struct cache_head *item)
512 struct cache_deferred_req *dreq;
513 int hash = DFR_HASH(item);
515 dreq = req->defer(req);
520 dreq->recv_time = get_seconds();
522 spin_lock(&cache_defer_lock);
524 list_add(&dreq->recent, &cache_defer_list);
526 if (cache_defer_hash[hash].next == NULL)
527 INIT_LIST_HEAD(&cache_defer_hash[hash]);
528 list_add(&dreq->hash, &cache_defer_hash[hash]);
530 /* it is in, now maybe clean up */
532 if (++cache_defer_cnt > DFR_MAX) {
533 /* too much in the cache, randomly drop
537 dreq = list_entry(cache_defer_list.next,
538 struct cache_deferred_req,
541 dreq = list_entry(cache_defer_list.prev,
542 struct cache_deferred_req,
544 list_del(&dreq->recent);
545 list_del(&dreq->hash);
548 spin_unlock(&cache_defer_lock);
551 /* there was one too many */
552 dreq->revisit(dreq, 1);
554 if (test_bit(CACHE_VALID, &item->flags)) {
555 /* must have just been validated... */
556 cache_revisit_request(item);
560 static void cache_revisit_request(struct cache_head *item)
562 struct cache_deferred_req *dreq;
563 struct list_head pending;
565 struct list_head *lp;
566 int hash = DFR_HASH(item);
568 INIT_LIST_HEAD(&pending);
569 spin_lock(&cache_defer_lock);
571 lp = cache_defer_hash[hash].next;
573 while (lp != &cache_defer_hash[hash]) {
574 dreq = list_entry(lp, struct cache_deferred_req, hash);
576 if (dreq->item == item) {
577 list_del(&dreq->hash);
578 list_move(&dreq->recent, &pending);
583 spin_unlock(&cache_defer_lock);
585 while (!list_empty(&pending)) {
586 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
587 list_del_init(&dreq->recent);
588 dreq->revisit(dreq, 0);
592 void cache_clean_deferred(void *owner)
594 struct cache_deferred_req *dreq, *tmp;
595 struct list_head pending;
598 INIT_LIST_HEAD(&pending);
599 spin_lock(&cache_defer_lock);
601 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
602 if (dreq->owner == owner) {
603 list_del(&dreq->hash);
604 list_move(&dreq->recent, &pending);
608 spin_unlock(&cache_defer_lock);
610 while (!list_empty(&pending)) {
611 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
612 list_del_init(&dreq->recent);
613 dreq->revisit(dreq, 1);
618 * communicate with user-space
620 * We have a magic /proc file - /proc/sunrpc/cache
621 * On read, you get a full request, or block
622 * On write, an update request is processed
623 * Poll works if anything to read, and always allows write
625 * Implemented by linked list of requests. Each open file has
626 * a ->private that also exists in this list. New request are added
627 * to the end and may wakeup and preceding readers.
628 * New readers are added to the head. If, on read, an item is found with
629 * CACHE_UPCALLING clear, we free it from the list.
633 static DEFINE_SPINLOCK(queue_lock);
634 static DEFINE_MUTEX(queue_io_mutex);
637 struct list_head list;
638 int reader; /* if 0, then request */
640 struct cache_request {
641 struct cache_queue q;
642 struct cache_head *item;
647 struct cache_reader {
648 struct cache_queue q;
649 int offset; /* if non-0, we have a refcnt on next request */
653 cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
655 struct cache_reader *rp = filp->private_data;
656 struct cache_request *rq;
657 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
663 mutex_lock(&queue_io_mutex); /* protect against multiple concurrent
664 * readers on this file */
666 spin_lock(&queue_lock);
667 /* need to find next request */
668 while (rp->q.list.next != &cd->queue &&
669 list_entry(rp->q.list.next, struct cache_queue, list)
671 struct list_head *next = rp->q.list.next;
672 list_move(&rp->q.list, next);
674 if (rp->q.list.next == &cd->queue) {
675 spin_unlock(&queue_lock);
676 mutex_unlock(&queue_io_mutex);
680 rq = container_of(rp->q.list.next, struct cache_request, q.list);
681 BUG_ON(rq->q.reader);
684 spin_unlock(&queue_lock);
686 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
688 spin_lock(&queue_lock);
689 list_move(&rp->q.list, &rq->q.list);
690 spin_unlock(&queue_lock);
692 if (rp->offset + count > rq->len)
693 count = rq->len - rp->offset;
695 if (copy_to_user(buf, rq->buf + rp->offset, count))
698 if (rp->offset >= rq->len) {
700 spin_lock(&queue_lock);
701 list_move(&rp->q.list, &rq->q.list);
702 spin_unlock(&queue_lock);
707 if (rp->offset == 0) {
708 /* need to release rq */
709 spin_lock(&queue_lock);
711 if (rq->readers == 0 &&
712 !test_bit(CACHE_PENDING, &rq->item->flags)) {
713 list_del(&rq->q.list);
714 spin_unlock(&queue_lock);
715 cd->cache_put(rq->item, cd);
719 spin_unlock(&queue_lock);
723 mutex_unlock(&queue_io_mutex);
724 return err ? err : count;
727 static char write_buf[8192]; /* protected by queue_io_mutex */
730 cache_write(struct file *filp, const char __user *buf, size_t count,
734 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
738 if (count >= sizeof(write_buf))
741 mutex_lock(&queue_io_mutex);
743 if (copy_from_user(write_buf, buf, count)) {
744 mutex_unlock(&queue_io_mutex);
747 write_buf[count] = '\0';
749 err = cd->cache_parse(cd, write_buf, count);
753 mutex_unlock(&queue_io_mutex);
754 return err ? err : count;
757 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
760 cache_poll(struct file *filp, poll_table *wait)
763 struct cache_reader *rp = filp->private_data;
764 struct cache_queue *cq;
765 struct cache_detail *cd = PDE(filp->f_dentry->d_inode)->data;
767 poll_wait(filp, &queue_wait, wait);
769 /* alway allow write */
770 mask = POLL_OUT | POLLWRNORM;
775 spin_lock(&queue_lock);
777 for (cq= &rp->q; &cq->list != &cd->queue;
778 cq = list_entry(cq->list.next, struct cache_queue, list))
780 mask |= POLLIN | POLLRDNORM;
783 spin_unlock(&queue_lock);
788 cache_ioctl(struct inode *ino, struct file *filp,
789 unsigned int cmd, unsigned long arg)
792 struct cache_reader *rp = filp->private_data;
793 struct cache_queue *cq;
794 struct cache_detail *cd = PDE(ino)->data;
796 if (cmd != FIONREAD || !rp)
799 spin_lock(&queue_lock);
801 /* only find the length remaining in current request,
802 * or the length of the next request
804 for (cq= &rp->q; &cq->list != &cd->queue;
805 cq = list_entry(cq->list.next, struct cache_queue, list))
807 struct cache_request *cr =
808 container_of(cq, struct cache_request, q);
809 len = cr->len - rp->offset;
812 spin_unlock(&queue_lock);
814 return put_user(len, (int __user *)arg);
818 cache_open(struct inode *inode, struct file *filp)
820 struct cache_reader *rp = NULL;
822 nonseekable_open(inode, filp);
823 if (filp->f_mode & FMODE_READ) {
824 struct cache_detail *cd = PDE(inode)->data;
826 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
831 atomic_inc(&cd->readers);
832 spin_lock(&queue_lock);
833 list_add(&rp->q.list, &cd->queue);
834 spin_unlock(&queue_lock);
836 filp->private_data = rp;
841 cache_release(struct inode *inode, struct file *filp)
843 struct cache_reader *rp = filp->private_data;
844 struct cache_detail *cd = PDE(inode)->data;
847 spin_lock(&queue_lock);
849 struct cache_queue *cq;
850 for (cq= &rp->q; &cq->list != &cd->queue;
851 cq = list_entry(cq->list.next, struct cache_queue, list))
853 container_of(cq, struct cache_request, q)
859 list_del(&rp->q.list);
860 spin_unlock(&queue_lock);
862 filp->private_data = NULL;
865 cd->last_close = get_seconds();
866 atomic_dec(&cd->readers);
873 static struct file_operations cache_file_operations = {
874 .owner = THIS_MODULE,
877 .write = cache_write,
879 .ioctl = cache_ioctl, /* for FIONREAD */
881 .release = cache_release,
885 static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
887 struct cache_queue *cq;
888 spin_lock(&queue_lock);
889 list_for_each_entry(cq, &detail->queue, list)
891 struct cache_request *cr = container_of(cq, struct cache_request, q);
894 if (cr->readers != 0)
896 list_del(&cr->q.list);
897 spin_unlock(&queue_lock);
898 detail->cache_put(cr->item, detail);
903 spin_unlock(&queue_lock);
907 * Support routines for text-based upcalls.
908 * Fields are separated by spaces.
909 * Fields are either mangled to quote space tab newline slosh with slosh
910 * or a hexified with a leading \x
911 * Record is terminated with newline.
915 void qword_add(char **bpp, int *lp, char *str)
923 while ((c=*str++) && len)
931 *bp++ = '0' + ((c & 0300)>>6);
932 *bp++ = '0' + ((c & 0070)>>3);
933 *bp++ = '0' + ((c & 0007)>>0);
941 if (c || len <1) len = -1;
950 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
961 while (blen && len >= 2) {
962 unsigned char c = *buf++;
963 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
964 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
969 if (blen || len<1) len = -1;
978 static void warn_no_listener(struct cache_detail *detail)
980 if (detail->last_warn != detail->last_close) {
981 detail->last_warn = detail->last_close;
982 if (detail->warn_no_listener)
983 detail->warn_no_listener(detail);
988 * register an upcall request to user-space.
989 * Each request is at most one page long.
991 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
995 struct cache_request *crq;
999 if (detail->cache_request == NULL)
1002 if (atomic_read(&detail->readers) == 0 &&
1003 detail->last_close < get_seconds() - 30) {
1004 warn_no_listener(detail);
1008 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1012 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1018 bp = buf; len = PAGE_SIZE;
1020 detail->cache_request(detail, h, &bp, &len);
1028 crq->item = cache_get(h);
1030 crq->len = PAGE_SIZE - len;
1032 spin_lock(&queue_lock);
1033 list_add_tail(&crq->q.list, &detail->queue);
1034 spin_unlock(&queue_lock);
1035 wake_up(&queue_wait);
1040 * parse a message from user-space and pass it
1041 * to an appropriate cache
1042 * Messages are, like requests, separated into fields by
1043 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1046 * reply cachename expiry key ... content....
1048 * key and content are both parsed by cache
1051 #define isodigit(c) (isdigit(c) && c <= '7')
1052 int qword_get(char **bpp, char *dest, int bufsize)
1054 /* return bytes copied, or -1 on error */
1058 while (*bp == ' ') bp++;
1060 if (bp[0] == '\\' && bp[1] == 'x') {
1063 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1064 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1067 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1073 /* text with \nnn octal quoting */
1074 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1076 isodigit(bp[1]) && (bp[1] <= '3') &&
1079 int byte = (*++bp -'0');
1081 byte = (byte << 3) | (*bp++ - '0');
1082 byte = (byte << 3) | (*bp++ - '0');
1092 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1094 while (*bp == ' ') bp++;
1102 * support /proc/sunrpc/cache/$CACHENAME/content
1104 * We call ->cache_show passing NULL for the item to
1105 * get a header, then pass each real item in the cache
1109 struct cache_detail *cd;
1112 static void *c_start(struct seq_file *m, loff_t *pos)
1115 unsigned hash, entry;
1116 struct cache_head *ch;
1117 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1120 read_lock(&cd->hash_lock);
1122 return SEQ_START_TOKEN;
1124 entry = n & ((1LL<<32) - 1);
1126 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1129 n &= ~((1LL<<32) - 1);
1133 } while(hash < cd->hash_size &&
1134 cd->hash_table[hash]==NULL);
1135 if (hash >= cd->hash_size)
1138 return cd->hash_table[hash];
1141 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1143 struct cache_head *ch = p;
1144 int hash = (*pos >> 32);
1145 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1147 if (p == SEQ_START_TOKEN)
1149 else if (ch->next == NULL) {
1156 *pos &= ~((1LL<<32) - 1);
1157 while (hash < cd->hash_size &&
1158 cd->hash_table[hash] == NULL) {
1162 if (hash >= cd->hash_size)
1165 return cd->hash_table[hash];
1168 static void c_stop(struct seq_file *m, void *p)
1170 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1171 read_unlock(&cd->hash_lock);
1174 static int c_show(struct seq_file *m, void *p)
1176 struct cache_head *cp = p;
1177 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1179 if (p == SEQ_START_TOKEN)
1180 return cd->cache_show(m, cd, NULL);
1183 seq_printf(m, "# expiry=%ld refcnt=%d\n",
1184 cp->expiry_time, atomic_read(&cp->refcnt));
1186 if (cache_check(cd, cp, NULL))
1187 /* cache_check does a cache_put on failure */
1188 seq_printf(m, "# ");
1192 return cd->cache_show(m, cd, cp);
1195 static struct seq_operations cache_content_op = {
1202 static int content_open(struct inode *inode, struct file *file)
1206 struct cache_detail *cd = PDE(inode)->data;
1208 han = kmalloc(sizeof(*han), GFP_KERNEL);
1214 res = seq_open(file, &cache_content_op);
1218 ((struct seq_file *)file->private_data)->private = han;
1222 static int content_release(struct inode *inode, struct file *file)
1224 struct seq_file *m = (struct seq_file *)file->private_data;
1225 struct handle *han = m->private;
1228 return seq_release(inode, file);
1231 static struct file_operations content_file_operations = {
1232 .open = content_open,
1234 .llseek = seq_lseek,
1235 .release = content_release,
1238 static ssize_t read_flush(struct file *file, char __user *buf,
1239 size_t count, loff_t *ppos)
1241 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1243 unsigned long p = *ppos;
1246 sprintf(tbuf, "%lu\n", cd->flush_time);
1251 if (len > count) len = count;
1252 if (copy_to_user(buf, (void*)(tbuf+p), len))
1259 static ssize_t write_flush(struct file * file, const char __user * buf,
1260 size_t count, loff_t *ppos)
1262 struct cache_detail *cd = PDE(file->f_dentry->d_inode)->data;
1266 if (*ppos || count > sizeof(tbuf)-1)
1268 if (copy_from_user(tbuf, buf, count))
1271 flushtime = simple_strtoul(tbuf, &ep, 0);
1272 if (*ep && *ep != '\n')
1275 cd->flush_time = flushtime;
1276 cd->nextcheck = get_seconds();
1283 static struct file_operations cache_flush_operations = {
1284 .open = nonseekable_open,
1286 .write = write_flush,