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 <linux/pagemap.h>
31 #include <asm/ioctls.h>
32 #include <linux/sunrpc/types.h>
33 #include <linux/sunrpc/cache.h>
34 #include <linux/sunrpc/stats.h>
36 #define RPCDBG_FACILITY RPCDBG_CACHE
38 static int cache_defer_req(struct cache_req *req, struct cache_head *item);
39 static void cache_revisit_request(struct cache_head *item);
41 static void cache_init(struct cache_head *h)
43 time_t now = get_seconds();
47 h->expiry_time = now + CACHE_NEW_EXPIRY;
48 h->last_refresh = now;
51 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
52 struct cache_head *key, int hash)
54 struct cache_head **head, **hp;
55 struct cache_head *new = NULL;
57 head = &detail->hash_table[hash];
59 read_lock(&detail->hash_lock);
61 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
62 struct cache_head *tmp = *hp;
63 if (detail->match(tmp, key)) {
65 read_unlock(&detail->hash_lock);
69 read_unlock(&detail->hash_lock);
70 /* Didn't find anything, insert an empty entry */
72 new = detail->alloc();
75 /* must fully initialise 'new', else
76 * we might get lose if we need to
80 detail->init(new, key);
82 write_lock(&detail->hash_lock);
84 /* check if entry appeared while we slept */
85 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
86 struct cache_head *tmp = *hp;
87 if (detail->match(tmp, key)) {
89 write_unlock(&detail->hash_lock);
90 cache_put(new, detail);
98 write_unlock(&detail->hash_lock);
102 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
105 static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
107 static int cache_fresh_locked(struct cache_head *head, time_t expiry)
109 head->expiry_time = expiry;
110 head->last_refresh = get_seconds();
111 return !test_and_set_bit(CACHE_VALID, &head->flags);
114 static void cache_fresh_unlocked(struct cache_head *head,
115 struct cache_detail *detail, int new)
118 cache_revisit_request(head);
119 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
120 cache_revisit_request(head);
121 queue_loose(detail, head);
125 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
126 struct cache_head *new, struct cache_head *old, int hash)
128 /* The 'old' entry is to be replaced by 'new'.
129 * If 'old' is not VALID, we update it directly,
130 * otherwise we need to replace it
132 struct cache_head **head;
133 struct cache_head *tmp;
136 if (!test_bit(CACHE_VALID, &old->flags)) {
137 write_lock(&detail->hash_lock);
138 if (!test_bit(CACHE_VALID, &old->flags)) {
139 if (test_bit(CACHE_NEGATIVE, &new->flags))
140 set_bit(CACHE_NEGATIVE, &old->flags);
142 detail->update(old, new);
143 is_new = cache_fresh_locked(old, new->expiry_time);
144 write_unlock(&detail->hash_lock);
145 cache_fresh_unlocked(old, detail, is_new);
148 write_unlock(&detail->hash_lock);
150 /* We need to insert a new entry */
151 tmp = detail->alloc();
153 cache_put(old, detail);
157 detail->init(tmp, old);
158 head = &detail->hash_table[hash];
160 write_lock(&detail->hash_lock);
161 if (test_bit(CACHE_NEGATIVE, &new->flags))
162 set_bit(CACHE_NEGATIVE, &tmp->flags);
164 detail->update(tmp, new);
169 is_new = cache_fresh_locked(tmp, new->expiry_time);
170 cache_fresh_locked(old, 0);
171 write_unlock(&detail->hash_lock);
172 cache_fresh_unlocked(tmp, detail, is_new);
173 cache_fresh_unlocked(old, detail, 0);
174 cache_put(old, detail);
177 EXPORT_SYMBOL_GPL(sunrpc_cache_update);
179 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
181 * This is the generic cache management routine for all
182 * the authentication caches.
183 * It checks the currency of a cache item and will (later)
184 * initiate an upcall to fill it if needed.
187 * Returns 0 if the cache_head can be used, or cache_puts it and returns
188 * -EAGAIN if upcall is pending,
189 * -ETIMEDOUT if upcall failed and should be retried,
190 * -ENOENT if cache entry was negative
192 int cache_check(struct cache_detail *detail,
193 struct cache_head *h, struct cache_req *rqstp)
196 long refresh_age, age;
198 /* First decide return status as best we can */
199 if (!test_bit(CACHE_VALID, &h->flags) ||
200 h->expiry_time < get_seconds())
202 else if (detail->flush_time > h->last_refresh)
206 if (test_bit(CACHE_NEGATIVE, &h->flags))
211 /* now see if we want to start an upcall */
212 refresh_age = (h->expiry_time - h->last_refresh);
213 age = get_seconds() - h->last_refresh;
218 } else if (rv == -EAGAIN || age > refresh_age/2) {
219 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
221 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
222 switch (cache_make_upcall(detail, h)) {
224 clear_bit(CACHE_PENDING, &h->flags);
226 set_bit(CACHE_NEGATIVE, &h->flags);
227 cache_fresh_unlocked(h, detail,
228 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY));
234 clear_bit(CACHE_PENDING, &h->flags);
235 cache_revisit_request(h);
242 if (cache_defer_req(rqstp, h) != 0)
246 cache_put(h, detail);
249 EXPORT_SYMBOL_GPL(cache_check);
252 * caches need to be periodically cleaned.
253 * For this we maintain a list of cache_detail and
254 * a current pointer into that list and into the table
257 * Each time clean_cache is called it finds the next non-empty entry
258 * in the current table and walks the list in that entry
259 * looking for entries that can be removed.
261 * An entry gets removed if:
262 * - The expiry is before current time
263 * - The last_refresh time is before the flush_time for that cache
265 * later we might drop old entries with non-NEVER expiry if that table
266 * is getting 'full' for some definition of 'full'
268 * The question of "how often to scan a table" is an interesting one
269 * and is answered in part by the use of the "nextcheck" field in the
271 * When a scan of a table begins, the nextcheck field is set to a time
272 * that is well into the future.
273 * While scanning, if an expiry time is found that is earlier than the
274 * current nextcheck time, nextcheck is set to that expiry time.
275 * If the flush_time is ever set to a time earlier than the nextcheck
276 * time, the nextcheck time is then set to that flush_time.
278 * A table is then only scanned if the current time is at least
279 * the nextcheck time.
283 static LIST_HEAD(cache_list);
284 static DEFINE_SPINLOCK(cache_list_lock);
285 static struct cache_detail *current_detail;
286 static int current_index;
288 static const struct file_operations cache_file_operations;
289 static const struct file_operations content_file_operations;
290 static const struct file_operations cache_flush_operations;
292 static void do_cache_clean(struct work_struct *work);
293 static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
295 static void remove_cache_proc_entries(struct cache_detail *cd)
297 if (cd->proc_ent == NULL)
300 remove_proc_entry("flush", cd->proc_ent);
302 remove_proc_entry("channel", cd->proc_ent);
304 remove_proc_entry("content", cd->proc_ent);
306 remove_proc_entry(cd->name, proc_net_rpc);
309 #ifdef CONFIG_PROC_FS
310 static int create_cache_proc_entries(struct cache_detail *cd)
312 struct proc_dir_entry *p;
314 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
315 if (cd->proc_ent == NULL)
317 cd->channel_ent = cd->content_ent = NULL;
319 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
320 cd->proc_ent, &cache_flush_operations, cd);
325 if (cd->cache_request || cd->cache_parse) {
326 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
327 cd->proc_ent, &cache_file_operations, cd);
332 if (cd->cache_show) {
333 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
334 cd->proc_ent, &content_file_operations, cd);
341 remove_cache_proc_entries(cd);
344 #else /* CONFIG_PROC_FS */
345 static int create_cache_proc_entries(struct cache_detail *cd)
351 static void sunrpc_init_cache_detail(struct cache_detail *cd)
353 rwlock_init(&cd->hash_lock);
354 INIT_LIST_HEAD(&cd->queue);
355 spin_lock(&cache_list_lock);
358 atomic_set(&cd->readers, 0);
361 list_add(&cd->others, &cache_list);
362 spin_unlock(&cache_list_lock);
364 /* start the cleaning process */
365 schedule_delayed_work(&cache_cleaner, 0);
368 static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
371 spin_lock(&cache_list_lock);
372 write_lock(&cd->hash_lock);
373 if (cd->entries || atomic_read(&cd->inuse)) {
374 write_unlock(&cd->hash_lock);
375 spin_unlock(&cache_list_lock);
378 if (current_detail == cd)
379 current_detail = NULL;
380 list_del_init(&cd->others);
381 write_unlock(&cd->hash_lock);
382 spin_unlock(&cache_list_lock);
383 if (list_empty(&cache_list)) {
384 /* module must be being unloaded so its safe to kill the worker */
385 cancel_delayed_work_sync(&cache_cleaner);
389 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
392 int cache_register(struct cache_detail *cd)
396 sunrpc_init_cache_detail(cd);
397 ret = create_cache_proc_entries(cd);
399 sunrpc_destroy_cache_detail(cd);
402 EXPORT_SYMBOL_GPL(cache_register);
404 void cache_unregister(struct cache_detail *cd)
406 remove_cache_proc_entries(cd);
407 sunrpc_destroy_cache_detail(cd);
409 EXPORT_SYMBOL_GPL(cache_unregister);
411 /* clean cache tries to find something to clean
413 * It returns 1 if it cleaned something,
414 * 0 if it didn't find anything this time
415 * -1 if it fell off the end of the list.
417 static int cache_clean(void)
420 struct list_head *next;
422 spin_lock(&cache_list_lock);
424 /* find a suitable table if we don't already have one */
425 while (current_detail == NULL ||
426 current_index >= current_detail->hash_size) {
428 next = current_detail->others.next;
430 next = cache_list.next;
431 if (next == &cache_list) {
432 current_detail = NULL;
433 spin_unlock(&cache_list_lock);
436 current_detail = list_entry(next, struct cache_detail, others);
437 if (current_detail->nextcheck > get_seconds())
438 current_index = current_detail->hash_size;
441 current_detail->nextcheck = get_seconds()+30*60;
445 /* find a non-empty bucket in the table */
446 while (current_detail &&
447 current_index < current_detail->hash_size &&
448 current_detail->hash_table[current_index] == NULL)
451 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
453 if (current_detail && current_index < current_detail->hash_size) {
454 struct cache_head *ch, **cp;
455 struct cache_detail *d;
457 write_lock(¤t_detail->hash_lock);
459 /* Ok, now to clean this strand */
461 cp = & current_detail->hash_table[current_index];
463 for (; ch; cp= & ch->next, ch= *cp) {
464 if (current_detail->nextcheck > ch->expiry_time)
465 current_detail->nextcheck = ch->expiry_time+1;
466 if (ch->expiry_time >= get_seconds()
467 && ch->last_refresh >= current_detail->flush_time
470 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
471 queue_loose(current_detail, ch);
473 if (atomic_read(&ch->ref.refcount) == 1)
479 current_detail->entries--;
482 write_unlock(¤t_detail->hash_lock);
486 spin_unlock(&cache_list_lock);
490 spin_unlock(&cache_list_lock);
496 * We want to regularly clean the cache, so we need to schedule some work ...
498 static void do_cache_clean(struct work_struct *work)
501 if (cache_clean() == -1)
502 delay = round_jiffies_relative(30*HZ);
504 if (list_empty(&cache_list))
508 schedule_delayed_work(&cache_cleaner, delay);
513 * Clean all caches promptly. This just calls cache_clean
514 * repeatedly until we are sure that every cache has had a chance to
517 void cache_flush(void)
519 while (cache_clean() != -1)
521 while (cache_clean() != -1)
524 EXPORT_SYMBOL_GPL(cache_flush);
526 void cache_purge(struct cache_detail *detail)
528 detail->flush_time = LONG_MAX;
529 detail->nextcheck = get_seconds();
531 detail->flush_time = 1;
533 EXPORT_SYMBOL_GPL(cache_purge);
537 * Deferral and Revisiting of Requests.
539 * If a cache lookup finds a pending entry, we
540 * need to defer the request and revisit it later.
541 * All deferred requests are stored in a hash table,
542 * indexed by "struct cache_head *".
543 * As it may be wasteful to store a whole request
544 * structure, we allow the request to provide a
545 * deferred form, which must contain a
546 * 'struct cache_deferred_req'
547 * This cache_deferred_req contains a method to allow
548 * it to be revisited when cache info is available
551 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
552 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
554 #define DFR_MAX 300 /* ??? */
556 static DEFINE_SPINLOCK(cache_defer_lock);
557 static LIST_HEAD(cache_defer_list);
558 static struct list_head cache_defer_hash[DFR_HASHSIZE];
559 static int cache_defer_cnt;
561 static int cache_defer_req(struct cache_req *req, struct cache_head *item)
563 struct cache_deferred_req *dreq;
564 int hash = DFR_HASH(item);
566 if (cache_defer_cnt >= DFR_MAX) {
567 /* too much in the cache, randomly drop this one,
568 * or continue and drop the oldest below
573 dreq = req->defer(req);
579 spin_lock(&cache_defer_lock);
581 list_add(&dreq->recent, &cache_defer_list);
583 if (cache_defer_hash[hash].next == NULL)
584 INIT_LIST_HEAD(&cache_defer_hash[hash]);
585 list_add(&dreq->hash, &cache_defer_hash[hash]);
587 /* it is in, now maybe clean up */
589 if (++cache_defer_cnt > DFR_MAX) {
590 dreq = list_entry(cache_defer_list.prev,
591 struct cache_deferred_req, recent);
592 list_del(&dreq->recent);
593 list_del(&dreq->hash);
596 spin_unlock(&cache_defer_lock);
599 /* there was one too many */
600 dreq->revisit(dreq, 1);
602 if (!test_bit(CACHE_PENDING, &item->flags)) {
603 /* must have just been validated... */
604 cache_revisit_request(item);
609 static void cache_revisit_request(struct cache_head *item)
611 struct cache_deferred_req *dreq;
612 struct list_head pending;
614 struct list_head *lp;
615 int hash = DFR_HASH(item);
617 INIT_LIST_HEAD(&pending);
618 spin_lock(&cache_defer_lock);
620 lp = cache_defer_hash[hash].next;
622 while (lp != &cache_defer_hash[hash]) {
623 dreq = list_entry(lp, struct cache_deferred_req, hash);
625 if (dreq->item == item) {
626 list_del(&dreq->hash);
627 list_move(&dreq->recent, &pending);
632 spin_unlock(&cache_defer_lock);
634 while (!list_empty(&pending)) {
635 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
636 list_del_init(&dreq->recent);
637 dreq->revisit(dreq, 0);
641 void cache_clean_deferred(void *owner)
643 struct cache_deferred_req *dreq, *tmp;
644 struct list_head pending;
647 INIT_LIST_HEAD(&pending);
648 spin_lock(&cache_defer_lock);
650 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
651 if (dreq->owner == owner) {
652 list_del(&dreq->hash);
653 list_move(&dreq->recent, &pending);
657 spin_unlock(&cache_defer_lock);
659 while (!list_empty(&pending)) {
660 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
661 list_del_init(&dreq->recent);
662 dreq->revisit(dreq, 1);
667 * communicate with user-space
669 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
670 * On read, you get a full request, or block.
671 * On write, an update request is processed.
672 * Poll works if anything to read, and always allows write.
674 * Implemented by linked list of requests. Each open file has
675 * a ->private that also exists in this list. New requests are added
676 * to the end and may wakeup and preceding readers.
677 * New readers are added to the head. If, on read, an item is found with
678 * CACHE_UPCALLING clear, we free it from the list.
682 static DEFINE_SPINLOCK(queue_lock);
683 static DEFINE_MUTEX(queue_io_mutex);
686 struct list_head list;
687 int reader; /* if 0, then request */
689 struct cache_request {
690 struct cache_queue q;
691 struct cache_head *item;
696 struct cache_reader {
697 struct cache_queue q;
698 int offset; /* if non-0, we have a refcnt on next request */
702 cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
704 struct cache_reader *rp = filp->private_data;
705 struct cache_request *rq;
706 struct inode *inode = filp->f_path.dentry->d_inode;
707 struct cache_detail *cd = PDE(inode)->data;
713 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
714 * readers on this file */
716 spin_lock(&queue_lock);
717 /* need to find next request */
718 while (rp->q.list.next != &cd->queue &&
719 list_entry(rp->q.list.next, struct cache_queue, list)
721 struct list_head *next = rp->q.list.next;
722 list_move(&rp->q.list, next);
724 if (rp->q.list.next == &cd->queue) {
725 spin_unlock(&queue_lock);
726 mutex_unlock(&inode->i_mutex);
730 rq = container_of(rp->q.list.next, struct cache_request, q.list);
731 BUG_ON(rq->q.reader);
734 spin_unlock(&queue_lock);
736 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
738 spin_lock(&queue_lock);
739 list_move(&rp->q.list, &rq->q.list);
740 spin_unlock(&queue_lock);
742 if (rp->offset + count > rq->len)
743 count = rq->len - rp->offset;
745 if (copy_to_user(buf, rq->buf + rp->offset, count))
748 if (rp->offset >= rq->len) {
750 spin_lock(&queue_lock);
751 list_move(&rp->q.list, &rq->q.list);
752 spin_unlock(&queue_lock);
757 if (rp->offset == 0) {
758 /* need to release rq */
759 spin_lock(&queue_lock);
761 if (rq->readers == 0 &&
762 !test_bit(CACHE_PENDING, &rq->item->flags)) {
763 list_del(&rq->q.list);
764 spin_unlock(&queue_lock);
765 cache_put(rq->item, cd);
769 spin_unlock(&queue_lock);
773 mutex_unlock(&inode->i_mutex);
774 return err ? err : count;
777 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
778 size_t count, struct cache_detail *cd)
782 if (copy_from_user(kaddr, buf, count))
785 ret = cd->cache_parse(cd, kaddr, count);
791 static ssize_t cache_slow_downcall(const char __user *buf,
792 size_t count, struct cache_detail *cd)
794 static char write_buf[8192]; /* protected by queue_io_mutex */
795 ssize_t ret = -EINVAL;
797 if (count >= sizeof(write_buf))
799 mutex_lock(&queue_io_mutex);
800 ret = cache_do_downcall(write_buf, buf, count, cd);
801 mutex_unlock(&queue_io_mutex);
806 static ssize_t cache_downcall(struct address_space *mapping,
807 const char __user *buf,
808 size_t count, struct cache_detail *cd)
812 ssize_t ret = -ENOMEM;
814 if (count >= PAGE_CACHE_SIZE)
817 page = find_or_create_page(mapping, 0, GFP_KERNEL);
822 ret = cache_do_downcall(kaddr, buf, count, cd);
825 page_cache_release(page);
828 return cache_slow_downcall(buf, count, cd);
832 cache_write(struct file *filp, const char __user *buf, size_t count,
835 struct address_space *mapping = filp->f_mapping;
836 struct inode *inode = filp->f_path.dentry->d_inode;
837 struct cache_detail *cd = PDE(inode)->data;
838 ssize_t ret = -EINVAL;
840 if (!cd->cache_parse)
843 mutex_lock(&inode->i_mutex);
844 ret = cache_downcall(mapping, buf, count, cd);
845 mutex_unlock(&inode->i_mutex);
850 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
853 cache_poll(struct file *filp, poll_table *wait)
856 struct cache_reader *rp = filp->private_data;
857 struct cache_queue *cq;
858 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
860 poll_wait(filp, &queue_wait, wait);
862 /* alway allow write */
863 mask = POLL_OUT | POLLWRNORM;
868 spin_lock(&queue_lock);
870 for (cq= &rp->q; &cq->list != &cd->queue;
871 cq = list_entry(cq->list.next, struct cache_queue, list))
873 mask |= POLLIN | POLLRDNORM;
876 spin_unlock(&queue_lock);
881 cache_ioctl(struct inode *ino, struct file *filp,
882 unsigned int cmd, unsigned long arg)
885 struct cache_reader *rp = filp->private_data;
886 struct cache_queue *cq;
887 struct cache_detail *cd = PDE(ino)->data;
889 if (cmd != FIONREAD || !rp)
892 spin_lock(&queue_lock);
894 /* only find the length remaining in current request,
895 * or the length of the next request
897 for (cq= &rp->q; &cq->list != &cd->queue;
898 cq = list_entry(cq->list.next, struct cache_queue, list))
900 struct cache_request *cr =
901 container_of(cq, struct cache_request, q);
902 len = cr->len - rp->offset;
905 spin_unlock(&queue_lock);
907 return put_user(len, (int __user *)arg);
911 cache_open(struct inode *inode, struct file *filp)
913 struct cache_reader *rp = NULL;
915 nonseekable_open(inode, filp);
916 if (filp->f_mode & FMODE_READ) {
917 struct cache_detail *cd = PDE(inode)->data;
919 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
924 atomic_inc(&cd->readers);
925 spin_lock(&queue_lock);
926 list_add(&rp->q.list, &cd->queue);
927 spin_unlock(&queue_lock);
929 filp->private_data = rp;
934 cache_release(struct inode *inode, struct file *filp)
936 struct cache_reader *rp = filp->private_data;
937 struct cache_detail *cd = PDE(inode)->data;
940 spin_lock(&queue_lock);
942 struct cache_queue *cq;
943 for (cq= &rp->q; &cq->list != &cd->queue;
944 cq = list_entry(cq->list.next, struct cache_queue, list))
946 container_of(cq, struct cache_request, q)
952 list_del(&rp->q.list);
953 spin_unlock(&queue_lock);
955 filp->private_data = NULL;
958 cd->last_close = get_seconds();
959 atomic_dec(&cd->readers);
966 static const struct file_operations cache_file_operations = {
967 .owner = THIS_MODULE,
970 .write = cache_write,
972 .ioctl = cache_ioctl, /* for FIONREAD */
974 .release = cache_release,
978 static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
980 struct cache_queue *cq;
981 spin_lock(&queue_lock);
982 list_for_each_entry(cq, &detail->queue, list)
984 struct cache_request *cr = container_of(cq, struct cache_request, q);
987 if (cr->readers != 0)
989 list_del(&cr->q.list);
990 spin_unlock(&queue_lock);
991 cache_put(cr->item, detail);
996 spin_unlock(&queue_lock);
1000 * Support routines for text-based upcalls.
1001 * Fields are separated by spaces.
1002 * Fields are either mangled to quote space tab newline slosh with slosh
1003 * or a hexified with a leading \x
1004 * Record is terminated with newline.
1008 void qword_add(char **bpp, int *lp, char *str)
1014 if (len < 0) return;
1016 while ((c=*str++) && len)
1024 *bp++ = '0' + ((c & 0300)>>6);
1025 *bp++ = '0' + ((c & 0070)>>3);
1026 *bp++ = '0' + ((c & 0007)>>0);
1034 if (c || len <1) len = -1;
1042 EXPORT_SYMBOL_GPL(qword_add);
1044 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1049 if (len < 0) return;
1055 while (blen && len >= 2) {
1056 unsigned char c = *buf++;
1057 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1058 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1063 if (blen || len<1) len = -1;
1071 EXPORT_SYMBOL_GPL(qword_addhex);
1073 static void warn_no_listener(struct cache_detail *detail)
1075 if (detail->last_warn != detail->last_close) {
1076 detail->last_warn = detail->last_close;
1077 if (detail->warn_no_listener)
1078 detail->warn_no_listener(detail, detail->last_close != 0);
1083 * register an upcall request to user-space.
1084 * Each request is at most one page long.
1086 static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
1090 struct cache_request *crq;
1094 if (detail->cache_request == NULL)
1097 if (atomic_read(&detail->readers) == 0 &&
1098 detail->last_close < get_seconds() - 30) {
1099 warn_no_listener(detail);
1103 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1107 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1113 bp = buf; len = PAGE_SIZE;
1115 detail->cache_request(detail, h, &bp, &len);
1123 crq->item = cache_get(h);
1125 crq->len = PAGE_SIZE - len;
1127 spin_lock(&queue_lock);
1128 list_add_tail(&crq->q.list, &detail->queue);
1129 spin_unlock(&queue_lock);
1130 wake_up(&queue_wait);
1135 * parse a message from user-space and pass it
1136 * to an appropriate cache
1137 * Messages are, like requests, separated into fields by
1138 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1141 * reply cachename expiry key ... content....
1143 * key and content are both parsed by cache
1146 #define isodigit(c) (isdigit(c) && c <= '7')
1147 int qword_get(char **bpp, char *dest, int bufsize)
1149 /* return bytes copied, or -1 on error */
1153 while (*bp == ' ') bp++;
1155 if (bp[0] == '\\' && bp[1] == 'x') {
1158 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1159 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1162 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1168 /* text with \nnn octal quoting */
1169 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1171 isodigit(bp[1]) && (bp[1] <= '3') &&
1174 int byte = (*++bp -'0');
1176 byte = (byte << 3) | (*bp++ - '0');
1177 byte = (byte << 3) | (*bp++ - '0');
1187 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1189 while (*bp == ' ') bp++;
1194 EXPORT_SYMBOL_GPL(qword_get);
1198 * support /proc/sunrpc/cache/$CACHENAME/content
1200 * We call ->cache_show passing NULL for the item to
1201 * get a header, then pass each real item in the cache
1205 struct cache_detail *cd;
1208 static void *c_start(struct seq_file *m, loff_t *pos)
1209 __acquires(cd->hash_lock)
1212 unsigned hash, entry;
1213 struct cache_head *ch;
1214 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1217 read_lock(&cd->hash_lock);
1219 return SEQ_START_TOKEN;
1221 entry = n & ((1LL<<32) - 1);
1223 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1226 n &= ~((1LL<<32) - 1);
1230 } while(hash < cd->hash_size &&
1231 cd->hash_table[hash]==NULL);
1232 if (hash >= cd->hash_size)
1235 return cd->hash_table[hash];
1238 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1240 struct cache_head *ch = p;
1241 int hash = (*pos >> 32);
1242 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1244 if (p == SEQ_START_TOKEN)
1246 else if (ch->next == NULL) {
1253 *pos &= ~((1LL<<32) - 1);
1254 while (hash < cd->hash_size &&
1255 cd->hash_table[hash] == NULL) {
1259 if (hash >= cd->hash_size)
1262 return cd->hash_table[hash];
1265 static void c_stop(struct seq_file *m, void *p)
1266 __releases(cd->hash_lock)
1268 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1269 read_unlock(&cd->hash_lock);
1272 static int c_show(struct seq_file *m, void *p)
1274 struct cache_head *cp = p;
1275 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1277 if (p == SEQ_START_TOKEN)
1278 return cd->cache_show(m, cd, NULL);
1281 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1282 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
1284 if (cache_check(cd, cp, NULL))
1285 /* cache_check does a cache_put on failure */
1286 seq_printf(m, "# ");
1290 return cd->cache_show(m, cd, cp);
1293 static const struct seq_operations cache_content_op = {
1300 static int content_open(struct inode *inode, struct file *file)
1303 struct cache_detail *cd = PDE(inode)->data;
1305 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1313 static const struct file_operations content_file_operations = {
1314 .open = content_open,
1316 .llseek = seq_lseek,
1317 .release = seq_release_private,
1320 static ssize_t read_flush(struct file *file, char __user *buf,
1321 size_t count, loff_t *ppos)
1323 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
1325 unsigned long p = *ppos;
1328 sprintf(tbuf, "%lu\n", cd->flush_time);
1335 if (copy_to_user(buf, (void*)(tbuf+p), len))
1341 static ssize_t write_flush(struct file * file, const char __user * buf,
1342 size_t count, loff_t *ppos)
1344 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
1348 if (*ppos || count > sizeof(tbuf)-1)
1350 if (copy_from_user(tbuf, buf, count))
1353 flushtime = simple_strtoul(tbuf, &ep, 0);
1354 if (*ep && *ep != '\n')
1357 cd->flush_time = flushtime;
1358 cd->nextcheck = get_seconds();
1365 static const struct file_operations cache_flush_operations = {
1366 .open = nonseekable_open,
1368 .write = write_flush,