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 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();
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();
74 /* must fully initialise 'new', else
75 * we might get lose if we need to
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)) {
88 write_unlock(&detail->hash_lock);
89 cache_put(new, detail);
97 write_unlock(&detail->hash_lock);
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)
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;
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);
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);
147 write_unlock(&detail->hash_lock);
149 /* We need to insert a new entry */
150 tmp = detail->alloc();
152 cache_put(old, detail);
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);
163 detail->update(tmp, new);
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);
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)
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())
201 else if (detail->flush_time > h->last_refresh)
205 if (test_bit(CACHE_NEGATIVE, &h->flags))
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;
217 } else if (rv == -EAGAIN || age > refresh_age/2) {
218 dprintk("Want update, refage=%ld, age=%ld\n", refresh_age, age);
219 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
220 switch (cache_make_upcall(detail, h)) {
222 clear_bit(CACHE_PENDING, &h->flags);
224 set_bit(CACHE_NEGATIVE, &h->flags);
225 cache_fresh_unlocked(h, detail,
226 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY));
232 clear_bit(CACHE_PENDING, &h->flags);
233 cache_revisit_request(h);
240 if (cache_defer_req(rqstp, h) != 0)
244 cache_put(h, detail);
249 * caches need to be periodically cleaned.
250 * For this we maintain a list of cache_detail and
251 * a current pointer into that list and into the table
254 * Each time clean_cache is called it finds the next non-empty entry
255 * in the current table and walks the list in that entry
256 * looking for entries that can be removed.
258 * An entry gets removed if:
259 * - The expiry is before current time
260 * - The last_refresh time is before the flush_time for that cache
262 * later we might drop old entries with non-NEVER expiry if that table
263 * is getting 'full' for some definition of 'full'
265 * The question of "how often to scan a table" is an interesting one
266 * and is answered in part by the use of the "nextcheck" field in the
268 * When a scan of a table begins, the nextcheck field is set to a time
269 * that is well into the future.
270 * While scanning, if an expiry time is found that is earlier than the
271 * current nextcheck time, nextcheck is set to that expiry time.
272 * If the flush_time is ever set to a time earlier than the nextcheck
273 * time, the nextcheck time is then set to that flush_time.
275 * A table is then only scanned if the current time is at least
276 * the nextcheck time.
280 static LIST_HEAD(cache_list);
281 static DEFINE_SPINLOCK(cache_list_lock);
282 static struct cache_detail *current_detail;
283 static int current_index;
285 static const struct file_operations cache_file_operations;
286 static const struct file_operations content_file_operations;
287 static const struct file_operations cache_flush_operations;
289 static void do_cache_clean(struct work_struct *work);
290 static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
292 void cache_register(struct cache_detail *cd)
294 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
296 struct proc_dir_entry *p;
297 cd->proc_ent->owner = cd->owner;
298 cd->channel_ent = cd->content_ent = NULL;
300 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR,
304 p->proc_fops = &cache_flush_operations;
305 p->owner = cd->owner;
309 if (cd->cache_request || cd->cache_parse) {
310 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
314 p->proc_fops = &cache_file_operations;
315 p->owner = cd->owner;
319 if (cd->cache_show) {
320 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
324 p->proc_fops = &content_file_operations;
325 p->owner = cd->owner;
330 rwlock_init(&cd->hash_lock);
331 INIT_LIST_HEAD(&cd->queue);
332 spin_lock(&cache_list_lock);
335 atomic_set(&cd->readers, 0);
338 list_add(&cd->others, &cache_list);
339 spin_unlock(&cache_list_lock);
341 /* start the cleaning process */
342 schedule_delayed_work(&cache_cleaner, 0);
345 int cache_unregister(struct cache_detail *cd)
348 spin_lock(&cache_list_lock);
349 write_lock(&cd->hash_lock);
350 if (cd->entries || atomic_read(&cd->inuse)) {
351 write_unlock(&cd->hash_lock);
352 spin_unlock(&cache_list_lock);
355 if (current_detail == cd)
356 current_detail = NULL;
357 list_del_init(&cd->others);
358 write_unlock(&cd->hash_lock);
359 spin_unlock(&cache_list_lock);
362 remove_proc_entry("flush", cd->proc_ent);
364 remove_proc_entry("channel", cd->proc_ent);
366 remove_proc_entry("content", cd->proc_ent);
369 remove_proc_entry(cd->name, proc_net_rpc);
371 if (list_empty(&cache_list)) {
372 /* module must be being unloaded so its safe to kill the worker */
373 cancel_delayed_work(&cache_cleaner);
374 flush_scheduled_work();
379 /* clean cache tries to find something to clean
381 * It returns 1 if it cleaned something,
382 * 0 if it didn't find anything this time
383 * -1 if it fell off the end of the list.
385 static int cache_clean(void)
388 struct list_head *next;
390 spin_lock(&cache_list_lock);
392 /* find a suitable table if we don't already have one */
393 while (current_detail == NULL ||
394 current_index >= current_detail->hash_size) {
396 next = current_detail->others.next;
398 next = cache_list.next;
399 if (next == &cache_list) {
400 current_detail = NULL;
401 spin_unlock(&cache_list_lock);
404 current_detail = list_entry(next, struct cache_detail, others);
405 if (current_detail->nextcheck > get_seconds())
406 current_index = current_detail->hash_size;
409 current_detail->nextcheck = get_seconds()+30*60;
413 /* find a non-empty bucket in the table */
414 while (current_detail &&
415 current_index < current_detail->hash_size &&
416 current_detail->hash_table[current_index] == NULL)
419 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
421 if (current_detail && current_index < current_detail->hash_size) {
422 struct cache_head *ch, **cp;
423 struct cache_detail *d;
425 write_lock(¤t_detail->hash_lock);
427 /* Ok, now to clean this strand */
429 cp = & current_detail->hash_table[current_index];
431 for (; ch; cp= & ch->next, ch= *cp) {
432 if (current_detail->nextcheck > ch->expiry_time)
433 current_detail->nextcheck = ch->expiry_time+1;
434 if (ch->expiry_time >= get_seconds()
435 && ch->last_refresh >= current_detail->flush_time
438 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
439 queue_loose(current_detail, ch);
441 if (atomic_read(&ch->ref.refcount) == 1)
447 current_detail->entries--;
450 write_unlock(¤t_detail->hash_lock);
454 spin_unlock(&cache_list_lock);
458 spin_unlock(&cache_list_lock);
464 * We want to regularly clean the cache, so we need to schedule some work ...
466 static void do_cache_clean(struct work_struct *work)
469 if (cache_clean() == -1)
472 if (list_empty(&cache_list))
476 schedule_delayed_work(&cache_cleaner, delay);
481 * Clean all caches promptly. This just calls cache_clean
482 * repeatedly until we are sure that every cache has had a chance to
485 void cache_flush(void)
487 while (cache_clean() != -1)
489 while (cache_clean() != -1)
493 void cache_purge(struct cache_detail *detail)
495 detail->flush_time = LONG_MAX;
496 detail->nextcheck = get_seconds();
498 detail->flush_time = 1;
504 * Deferral and Revisiting of Requests.
506 * If a cache lookup finds a pending entry, we
507 * need to defer the request and revisit it later.
508 * All deferred requests are stored in a hash table,
509 * indexed by "struct cache_head *".
510 * As it may be wasteful to store a whole request
511 * structure, we allow the request to provide a
512 * deferred form, which must contain a
513 * 'struct cache_deferred_req'
514 * This cache_deferred_req contains a method to allow
515 * it to be revisited when cache info is available
518 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
519 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
521 #define DFR_MAX 300 /* ??? */
523 static DEFINE_SPINLOCK(cache_defer_lock);
524 static LIST_HEAD(cache_defer_list);
525 static struct list_head cache_defer_hash[DFR_HASHSIZE];
526 static int cache_defer_cnt;
528 static int cache_defer_req(struct cache_req *req, struct cache_head *item)
530 struct cache_deferred_req *dreq;
531 int hash = DFR_HASH(item);
533 if (cache_defer_cnt >= DFR_MAX) {
534 /* too much in the cache, randomly drop this one,
535 * or continue and drop the oldest below
540 dreq = req->defer(req);
545 dreq->recv_time = get_seconds();
547 spin_lock(&cache_defer_lock);
549 list_add(&dreq->recent, &cache_defer_list);
551 if (cache_defer_hash[hash].next == NULL)
552 INIT_LIST_HEAD(&cache_defer_hash[hash]);
553 list_add(&dreq->hash, &cache_defer_hash[hash]);
555 /* it is in, now maybe clean up */
557 if (++cache_defer_cnt > DFR_MAX) {
558 dreq = list_entry(cache_defer_list.prev,
559 struct cache_deferred_req, recent);
560 list_del(&dreq->recent);
561 list_del(&dreq->hash);
564 spin_unlock(&cache_defer_lock);
567 /* there was one too many */
568 dreq->revisit(dreq, 1);
570 if (!test_bit(CACHE_PENDING, &item->flags)) {
571 /* must have just been validated... */
572 cache_revisit_request(item);
577 static void cache_revisit_request(struct cache_head *item)
579 struct cache_deferred_req *dreq;
580 struct list_head pending;
582 struct list_head *lp;
583 int hash = DFR_HASH(item);
585 INIT_LIST_HEAD(&pending);
586 spin_lock(&cache_defer_lock);
588 lp = cache_defer_hash[hash].next;
590 while (lp != &cache_defer_hash[hash]) {
591 dreq = list_entry(lp, struct cache_deferred_req, hash);
593 if (dreq->item == item) {
594 list_del(&dreq->hash);
595 list_move(&dreq->recent, &pending);
600 spin_unlock(&cache_defer_lock);
602 while (!list_empty(&pending)) {
603 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
604 list_del_init(&dreq->recent);
605 dreq->revisit(dreq, 0);
609 void cache_clean_deferred(void *owner)
611 struct cache_deferred_req *dreq, *tmp;
612 struct list_head pending;
615 INIT_LIST_HEAD(&pending);
616 spin_lock(&cache_defer_lock);
618 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
619 if (dreq->owner == owner) {
620 list_del(&dreq->hash);
621 list_move(&dreq->recent, &pending);
625 spin_unlock(&cache_defer_lock);
627 while (!list_empty(&pending)) {
628 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
629 list_del_init(&dreq->recent);
630 dreq->revisit(dreq, 1);
635 * communicate with user-space
637 * We have a magic /proc file - /proc/sunrpc/cache
638 * On read, you get a full request, or block
639 * On write, an update request is processed
640 * Poll works if anything to read, and always allows write
642 * Implemented by linked list of requests. Each open file has
643 * a ->private that also exists in this list. New request are added
644 * to the end and may wakeup and preceding readers.
645 * New readers are added to the head. If, on read, an item is found with
646 * CACHE_UPCALLING clear, we free it from the list.
650 static DEFINE_SPINLOCK(queue_lock);
651 static DEFINE_MUTEX(queue_io_mutex);
654 struct list_head list;
655 int reader; /* if 0, then request */
657 struct cache_request {
658 struct cache_queue q;
659 struct cache_head *item;
664 struct cache_reader {
665 struct cache_queue q;
666 int offset; /* if non-0, we have a refcnt on next request */
670 cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
672 struct cache_reader *rp = filp->private_data;
673 struct cache_request *rq;
674 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
680 mutex_lock(&queue_io_mutex); /* protect against multiple concurrent
681 * readers on this file */
683 spin_lock(&queue_lock);
684 /* need to find next request */
685 while (rp->q.list.next != &cd->queue &&
686 list_entry(rp->q.list.next, struct cache_queue, list)
688 struct list_head *next = rp->q.list.next;
689 list_move(&rp->q.list, next);
691 if (rp->q.list.next == &cd->queue) {
692 spin_unlock(&queue_lock);
693 mutex_unlock(&queue_io_mutex);
697 rq = container_of(rp->q.list.next, struct cache_request, q.list);
698 BUG_ON(rq->q.reader);
701 spin_unlock(&queue_lock);
703 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
705 spin_lock(&queue_lock);
706 list_move(&rp->q.list, &rq->q.list);
707 spin_unlock(&queue_lock);
709 if (rp->offset + count > rq->len)
710 count = rq->len - rp->offset;
712 if (copy_to_user(buf, rq->buf + rp->offset, count))
715 if (rp->offset >= rq->len) {
717 spin_lock(&queue_lock);
718 list_move(&rp->q.list, &rq->q.list);
719 spin_unlock(&queue_lock);
724 if (rp->offset == 0) {
725 /* need to release rq */
726 spin_lock(&queue_lock);
728 if (rq->readers == 0 &&
729 !test_bit(CACHE_PENDING, &rq->item->flags)) {
730 list_del(&rq->q.list);
731 spin_unlock(&queue_lock);
732 cache_put(rq->item, cd);
736 spin_unlock(&queue_lock);
740 mutex_unlock(&queue_io_mutex);
741 return err ? err : count;
744 static char write_buf[8192]; /* protected by queue_io_mutex */
747 cache_write(struct file *filp, const char __user *buf, size_t count,
751 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
755 if (count >= sizeof(write_buf))
758 mutex_lock(&queue_io_mutex);
760 if (copy_from_user(write_buf, buf, count)) {
761 mutex_unlock(&queue_io_mutex);
764 write_buf[count] = '\0';
766 err = cd->cache_parse(cd, write_buf, count);
770 mutex_unlock(&queue_io_mutex);
771 return err ? err : count;
774 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
777 cache_poll(struct file *filp, poll_table *wait)
780 struct cache_reader *rp = filp->private_data;
781 struct cache_queue *cq;
782 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
784 poll_wait(filp, &queue_wait, wait);
786 /* alway allow write */
787 mask = POLL_OUT | POLLWRNORM;
792 spin_lock(&queue_lock);
794 for (cq= &rp->q; &cq->list != &cd->queue;
795 cq = list_entry(cq->list.next, struct cache_queue, list))
797 mask |= POLLIN | POLLRDNORM;
800 spin_unlock(&queue_lock);
805 cache_ioctl(struct inode *ino, struct file *filp,
806 unsigned int cmd, unsigned long arg)
809 struct cache_reader *rp = filp->private_data;
810 struct cache_queue *cq;
811 struct cache_detail *cd = PDE(ino)->data;
813 if (cmd != FIONREAD || !rp)
816 spin_lock(&queue_lock);
818 /* only find the length remaining in current request,
819 * or the length of the next request
821 for (cq= &rp->q; &cq->list != &cd->queue;
822 cq = list_entry(cq->list.next, struct cache_queue, list))
824 struct cache_request *cr =
825 container_of(cq, struct cache_request, q);
826 len = cr->len - rp->offset;
829 spin_unlock(&queue_lock);
831 return put_user(len, (int __user *)arg);
835 cache_open(struct inode *inode, struct file *filp)
837 struct cache_reader *rp = NULL;
839 nonseekable_open(inode, filp);
840 if (filp->f_mode & FMODE_READ) {
841 struct cache_detail *cd = PDE(inode)->data;
843 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
848 atomic_inc(&cd->readers);
849 spin_lock(&queue_lock);
850 list_add(&rp->q.list, &cd->queue);
851 spin_unlock(&queue_lock);
853 filp->private_data = rp;
858 cache_release(struct inode *inode, struct file *filp)
860 struct cache_reader *rp = filp->private_data;
861 struct cache_detail *cd = PDE(inode)->data;
864 spin_lock(&queue_lock);
866 struct cache_queue *cq;
867 for (cq= &rp->q; &cq->list != &cd->queue;
868 cq = list_entry(cq->list.next, struct cache_queue, list))
870 container_of(cq, struct cache_request, q)
876 list_del(&rp->q.list);
877 spin_unlock(&queue_lock);
879 filp->private_data = NULL;
882 cd->last_close = get_seconds();
883 atomic_dec(&cd->readers);
890 static const struct file_operations cache_file_operations = {
891 .owner = THIS_MODULE,
894 .write = cache_write,
896 .ioctl = cache_ioctl, /* for FIONREAD */
898 .release = cache_release,
902 static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
904 struct cache_queue *cq;
905 spin_lock(&queue_lock);
906 list_for_each_entry(cq, &detail->queue, list)
908 struct cache_request *cr = container_of(cq, struct cache_request, q);
911 if (cr->readers != 0)
913 list_del(&cr->q.list);
914 spin_unlock(&queue_lock);
915 cache_put(cr->item, detail);
920 spin_unlock(&queue_lock);
924 * Support routines for text-based upcalls.
925 * Fields are separated by spaces.
926 * Fields are either mangled to quote space tab newline slosh with slosh
927 * or a hexified with a leading \x
928 * Record is terminated with newline.
932 void qword_add(char **bpp, int *lp, char *str)
940 while ((c=*str++) && len)
948 *bp++ = '0' + ((c & 0300)>>6);
949 *bp++ = '0' + ((c & 0070)>>3);
950 *bp++ = '0' + ((c & 0007)>>0);
958 if (c || len <1) len = -1;
967 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
978 while (blen && len >= 2) {
979 unsigned char c = *buf++;
980 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
981 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
986 if (blen || len<1) len = -1;
995 static void warn_no_listener(struct cache_detail *detail)
997 if (detail->last_warn != detail->last_close) {
998 detail->last_warn = detail->last_close;
999 if (detail->warn_no_listener)
1000 detail->warn_no_listener(detail);
1005 * register an upcall request to user-space.
1006 * Each request is at most one page long.
1008 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
1012 struct cache_request *crq;
1016 if (detail->cache_request == NULL)
1019 if (atomic_read(&detail->readers) == 0 &&
1020 detail->last_close < get_seconds() - 30) {
1021 warn_no_listener(detail);
1025 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1029 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1035 bp = buf; len = PAGE_SIZE;
1037 detail->cache_request(detail, h, &bp, &len);
1045 crq->item = cache_get(h);
1047 crq->len = PAGE_SIZE - len;
1049 spin_lock(&queue_lock);
1050 list_add_tail(&crq->q.list, &detail->queue);
1051 spin_unlock(&queue_lock);
1052 wake_up(&queue_wait);
1057 * parse a message from user-space and pass it
1058 * to an appropriate cache
1059 * Messages are, like requests, separated into fields by
1060 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1063 * reply cachename expiry key ... content....
1065 * key and content are both parsed by cache
1068 #define isodigit(c) (isdigit(c) && c <= '7')
1069 int qword_get(char **bpp, char *dest, int bufsize)
1071 /* return bytes copied, or -1 on error */
1075 while (*bp == ' ') bp++;
1077 if (bp[0] == '\\' && bp[1] == 'x') {
1080 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1081 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1084 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1090 /* text with \nnn octal quoting */
1091 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1093 isodigit(bp[1]) && (bp[1] <= '3') &&
1096 int byte = (*++bp -'0');
1098 byte = (byte << 3) | (*bp++ - '0');
1099 byte = (byte << 3) | (*bp++ - '0');
1109 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1111 while (*bp == ' ') bp++;
1119 * support /proc/sunrpc/cache/$CACHENAME/content
1121 * We call ->cache_show passing NULL for the item to
1122 * get a header, then pass each real item in the cache
1126 struct cache_detail *cd;
1129 static void *c_start(struct seq_file *m, loff_t *pos)
1132 unsigned hash, entry;
1133 struct cache_head *ch;
1134 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1137 read_lock(&cd->hash_lock);
1139 return SEQ_START_TOKEN;
1141 entry = n & ((1LL<<32) - 1);
1143 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1146 n &= ~((1LL<<32) - 1);
1150 } while(hash < cd->hash_size &&
1151 cd->hash_table[hash]==NULL);
1152 if (hash >= cd->hash_size)
1155 return cd->hash_table[hash];
1158 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1160 struct cache_head *ch = p;
1161 int hash = (*pos >> 32);
1162 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1164 if (p == SEQ_START_TOKEN)
1166 else if (ch->next == NULL) {
1173 *pos &= ~((1LL<<32) - 1);
1174 while (hash < cd->hash_size &&
1175 cd->hash_table[hash] == NULL) {
1179 if (hash >= cd->hash_size)
1182 return cd->hash_table[hash];
1185 static void c_stop(struct seq_file *m, void *p)
1187 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1188 read_unlock(&cd->hash_lock);
1191 static int c_show(struct seq_file *m, void *p)
1193 struct cache_head *cp = p;
1194 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1196 if (p == SEQ_START_TOKEN)
1197 return cd->cache_show(m, cd, NULL);
1200 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1201 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
1203 if (cache_check(cd, cp, NULL))
1204 /* cache_check does a cache_put on failure */
1205 seq_printf(m, "# ");
1209 return cd->cache_show(m, cd, cp);
1212 static struct seq_operations cache_content_op = {
1219 static int content_open(struct inode *inode, struct file *file)
1223 struct cache_detail *cd = PDE(inode)->data;
1225 han = kmalloc(sizeof(*han), GFP_KERNEL);
1231 res = seq_open(file, &cache_content_op);
1235 ((struct seq_file *)file->private_data)->private = han;
1239 static int content_release(struct inode *inode, struct file *file)
1241 struct seq_file *m = (struct seq_file *)file->private_data;
1242 struct handle *han = m->private;
1245 return seq_release(inode, file);
1248 static const struct file_operations content_file_operations = {
1249 .open = content_open,
1251 .llseek = seq_lseek,
1252 .release = content_release,
1255 static ssize_t read_flush(struct file *file, char __user *buf,
1256 size_t count, loff_t *ppos)
1258 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
1260 unsigned long p = *ppos;
1263 sprintf(tbuf, "%lu\n", cd->flush_time);
1268 if (len > count) len = count;
1269 if (copy_to_user(buf, (void*)(tbuf+p), len))
1276 static ssize_t write_flush(struct file * file, const char __user * buf,
1277 size_t count, loff_t *ppos)
1279 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
1283 if (*ppos || count > sizeof(tbuf)-1)
1285 if (copy_from_user(tbuf, buf, count))
1288 flushtime = simple_strtoul(tbuf, &ep, 0);
1289 if (*ep && *ep != '\n')
1292 cd->flush_time = flushtime;
1293 cd->nextcheck = get_seconds();
1300 static const struct file_operations cache_flush_operations = {
1301 .open = nonseekable_open,
1303 .write = write_flush,