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 <linux/smp_lock.h>
32 #include <asm/ioctls.h>
33 #include <linux/sunrpc/types.h>
34 #include <linux/sunrpc/cache.h>
35 #include <linux/sunrpc/stats.h>
36 #include <linux/sunrpc/rpc_pipe_fs.h>
39 #define RPCDBG_FACILITY RPCDBG_CACHE
41 static int cache_defer_req(struct cache_req *req, struct cache_head *item);
42 static void cache_revisit_request(struct cache_head *item);
44 static void cache_init(struct cache_head *h)
46 time_t now = seconds_since_boot();
50 h->expiry_time = now + CACHE_NEW_EXPIRY;
51 h->last_refresh = now;
54 static inline int cache_is_expired(struct cache_detail *detail, struct cache_head *h)
56 return (h->expiry_time < seconds_since_boot()) ||
57 (detail->flush_time > h->last_refresh);
60 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
61 struct cache_head *key, int hash)
63 struct cache_head **head, **hp;
64 struct cache_head *new = NULL, *freeme = NULL;
66 head = &detail->hash_table[hash];
68 read_lock(&detail->hash_lock);
70 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
71 struct cache_head *tmp = *hp;
72 if (detail->match(tmp, key)) {
73 if (cache_is_expired(detail, tmp))
74 /* This entry is expired, we will discard it. */
77 read_unlock(&detail->hash_lock);
81 read_unlock(&detail->hash_lock);
82 /* Didn't find anything, insert an empty entry */
84 new = detail->alloc();
87 /* must fully initialise 'new', else
88 * we might get lose if we need to
92 detail->init(new, key);
94 write_lock(&detail->hash_lock);
96 /* check if entry appeared while we slept */
97 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
98 struct cache_head *tmp = *hp;
99 if (detail->match(tmp, key)) {
100 if (cache_is_expired(detail, tmp)) {
108 write_unlock(&detail->hash_lock);
109 cache_put(new, detail);
117 write_unlock(&detail->hash_lock);
120 cache_put(freeme, detail);
123 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
126 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
128 static void cache_fresh_locked(struct cache_head *head, time_t expiry)
130 head->expiry_time = expiry;
131 head->last_refresh = seconds_since_boot();
132 set_bit(CACHE_VALID, &head->flags);
135 static void cache_fresh_unlocked(struct cache_head *head,
136 struct cache_detail *detail)
138 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
139 cache_revisit_request(head);
140 cache_dequeue(detail, head);
144 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
145 struct cache_head *new, struct cache_head *old, int hash)
147 /* The 'old' entry is to be replaced by 'new'.
148 * If 'old' is not VALID, we update it directly,
149 * otherwise we need to replace it
151 struct cache_head **head;
152 struct cache_head *tmp;
154 if (!test_bit(CACHE_VALID, &old->flags)) {
155 write_lock(&detail->hash_lock);
156 if (!test_bit(CACHE_VALID, &old->flags)) {
157 if (test_bit(CACHE_NEGATIVE, &new->flags))
158 set_bit(CACHE_NEGATIVE, &old->flags);
160 detail->update(old, new);
161 cache_fresh_locked(old, new->expiry_time);
162 write_unlock(&detail->hash_lock);
163 cache_fresh_unlocked(old, detail);
166 write_unlock(&detail->hash_lock);
168 /* We need to insert a new entry */
169 tmp = detail->alloc();
171 cache_put(old, detail);
175 detail->init(tmp, old);
176 head = &detail->hash_table[hash];
178 write_lock(&detail->hash_lock);
179 if (test_bit(CACHE_NEGATIVE, &new->flags))
180 set_bit(CACHE_NEGATIVE, &tmp->flags);
182 detail->update(tmp, new);
187 cache_fresh_locked(tmp, new->expiry_time);
188 cache_fresh_locked(old, 0);
189 write_unlock(&detail->hash_lock);
190 cache_fresh_unlocked(tmp, detail);
191 cache_fresh_unlocked(old, detail);
192 cache_put(old, detail);
195 EXPORT_SYMBOL_GPL(sunrpc_cache_update);
197 static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
199 if (!cd->cache_upcall)
201 return cd->cache_upcall(cd, h);
204 static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h)
206 if (!test_bit(CACHE_VALID, &h->flags))
210 if (test_bit(CACHE_NEGATIVE, &h->flags))
218 * This is the generic cache management routine for all
219 * the authentication caches.
220 * It checks the currency of a cache item and will (later)
221 * initiate an upcall to fill it if needed.
224 * Returns 0 if the cache_head can be used, or cache_puts it and returns
225 * -EAGAIN if upcall is pending and request has been queued
226 * -ETIMEDOUT if upcall failed or request could not be queue or
227 * upcall completed but item is still invalid (implying that
228 * the cache item has been replaced with a newer one).
229 * -ENOENT if cache entry was negative
231 int cache_check(struct cache_detail *detail,
232 struct cache_head *h, struct cache_req *rqstp)
235 long refresh_age, age;
237 /* First decide return status as best we can */
238 rv = cache_is_valid(detail, h);
240 /* now see if we want to start an upcall */
241 refresh_age = (h->expiry_time - h->last_refresh);
242 age = seconds_since_boot() - h->last_refresh;
247 } else if (rv == -EAGAIN || age > refresh_age/2) {
248 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
250 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
251 switch (cache_make_upcall(detail, h)) {
253 clear_bit(CACHE_PENDING, &h->flags);
254 cache_revisit_request(h);
256 set_bit(CACHE_NEGATIVE, &h->flags);
257 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
258 cache_fresh_unlocked(h, detail);
264 clear_bit(CACHE_PENDING, &h->flags);
265 cache_revisit_request(h);
272 if (cache_defer_req(rqstp, h) < 0) {
273 /* Request is not deferred */
274 rv = cache_is_valid(detail, h);
280 cache_put(h, detail);
283 EXPORT_SYMBOL_GPL(cache_check);
286 * caches need to be periodically cleaned.
287 * For this we maintain a list of cache_detail and
288 * a current pointer into that list and into the table
291 * Each time clean_cache is called it finds the next non-empty entry
292 * in the current table and walks the list in that entry
293 * looking for entries that can be removed.
295 * An entry gets removed if:
296 * - The expiry is before current time
297 * - The last_refresh time is before the flush_time for that cache
299 * later we might drop old entries with non-NEVER expiry if that table
300 * is getting 'full' for some definition of 'full'
302 * The question of "how often to scan a table" is an interesting one
303 * and is answered in part by the use of the "nextcheck" field in the
305 * When a scan of a table begins, the nextcheck field is set to a time
306 * that is well into the future.
307 * While scanning, if an expiry time is found that is earlier than the
308 * current nextcheck time, nextcheck is set to that expiry time.
309 * If the flush_time is ever set to a time earlier than the nextcheck
310 * time, the nextcheck time is then set to that flush_time.
312 * A table is then only scanned if the current time is at least
313 * the nextcheck time.
317 static LIST_HEAD(cache_list);
318 static DEFINE_SPINLOCK(cache_list_lock);
319 static struct cache_detail *current_detail;
320 static int current_index;
322 static void do_cache_clean(struct work_struct *work);
323 static struct delayed_work cache_cleaner;
325 static void sunrpc_init_cache_detail(struct cache_detail *cd)
327 rwlock_init(&cd->hash_lock);
328 INIT_LIST_HEAD(&cd->queue);
329 spin_lock(&cache_list_lock);
332 atomic_set(&cd->readers, 0);
335 list_add(&cd->others, &cache_list);
336 spin_unlock(&cache_list_lock);
338 /* start the cleaning process */
339 schedule_delayed_work(&cache_cleaner, 0);
342 static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
345 spin_lock(&cache_list_lock);
346 write_lock(&cd->hash_lock);
347 if (cd->entries || atomic_read(&cd->inuse)) {
348 write_unlock(&cd->hash_lock);
349 spin_unlock(&cache_list_lock);
352 if (current_detail == cd)
353 current_detail = NULL;
354 list_del_init(&cd->others);
355 write_unlock(&cd->hash_lock);
356 spin_unlock(&cache_list_lock);
357 if (list_empty(&cache_list)) {
358 /* module must be being unloaded so its safe to kill the worker */
359 cancel_delayed_work_sync(&cache_cleaner);
363 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
366 /* clean cache tries to find something to clean
368 * It returns 1 if it cleaned something,
369 * 0 if it didn't find anything this time
370 * -1 if it fell off the end of the list.
372 static int cache_clean(void)
375 struct list_head *next;
377 spin_lock(&cache_list_lock);
379 /* find a suitable table if we don't already have one */
380 while (current_detail == NULL ||
381 current_index >= current_detail->hash_size) {
383 next = current_detail->others.next;
385 next = cache_list.next;
386 if (next == &cache_list) {
387 current_detail = NULL;
388 spin_unlock(&cache_list_lock);
391 current_detail = list_entry(next, struct cache_detail, others);
392 if (current_detail->nextcheck > seconds_since_boot())
393 current_index = current_detail->hash_size;
396 current_detail->nextcheck = seconds_since_boot()+30*60;
400 /* find a non-empty bucket in the table */
401 while (current_detail &&
402 current_index < current_detail->hash_size &&
403 current_detail->hash_table[current_index] == NULL)
406 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
408 if (current_detail && current_index < current_detail->hash_size) {
409 struct cache_head *ch, **cp;
410 struct cache_detail *d;
412 write_lock(¤t_detail->hash_lock);
414 /* Ok, now to clean this strand */
416 cp = & current_detail->hash_table[current_index];
417 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
418 if (current_detail->nextcheck > ch->expiry_time)
419 current_detail->nextcheck = ch->expiry_time+1;
420 if (!cache_is_expired(current_detail, ch))
425 current_detail->entries--;
430 write_unlock(¤t_detail->hash_lock);
434 spin_unlock(&cache_list_lock);
436 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
437 cache_dequeue(current_detail, ch);
438 cache_revisit_request(ch);
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(struct work_struct *work)
453 if (cache_clean() == -1)
454 delay = round_jiffies_relative(30*HZ);
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)
476 EXPORT_SYMBOL_GPL(cache_flush);
478 void cache_purge(struct cache_detail *detail)
480 detail->flush_time = LONG_MAX;
481 detail->nextcheck = seconds_since_boot();
483 detail->flush_time = 1;
485 EXPORT_SYMBOL_GPL(cache_purge);
489 * Deferral and Revisiting of Requests.
491 * If a cache lookup finds a pending entry, we
492 * need to defer the request and revisit it later.
493 * All deferred requests are stored in a hash table,
494 * indexed by "struct cache_head *".
495 * As it may be wasteful to store a whole request
496 * structure, we allow the request to provide a
497 * deferred form, which must contain a
498 * 'struct cache_deferred_req'
499 * This cache_deferred_req contains a method to allow
500 * it to be revisited when cache info is available
503 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
504 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
506 #define DFR_MAX 300 /* ??? */
508 static DEFINE_SPINLOCK(cache_defer_lock);
509 static LIST_HEAD(cache_defer_list);
510 static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
511 static int cache_defer_cnt;
513 static void __unhash_deferred_req(struct cache_deferred_req *dreq)
515 list_del_init(&dreq->recent);
516 hlist_del_init(&dreq->hash);
520 static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
522 int hash = DFR_HASH(item);
524 list_add(&dreq->recent, &cache_defer_list);
525 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
528 static int setup_deferral(struct cache_deferred_req *dreq, struct cache_head *item)
530 struct cache_deferred_req *discard;
534 spin_lock(&cache_defer_lock);
536 __hash_deferred_req(dreq, item);
538 /* it is in, now maybe clean up */
540 if (++cache_defer_cnt > DFR_MAX) {
541 discard = list_entry(cache_defer_list.prev,
542 struct cache_deferred_req, recent);
543 __unhash_deferred_req(discard);
545 spin_unlock(&cache_defer_lock);
548 /* there was one too many */
549 discard->revisit(discard, 1);
551 if (!test_bit(CACHE_PENDING, &item->flags)) {
552 /* must have just been validated... */
553 cache_revisit_request(item);
559 struct thread_deferred_req {
560 struct cache_deferred_req handle;
561 struct completion completion;
564 static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
566 struct thread_deferred_req *dr =
567 container_of(dreq, struct thread_deferred_req, handle);
568 complete(&dr->completion);
571 static int cache_wait_req(struct cache_req *req, struct cache_head *item)
573 struct thread_deferred_req sleeper;
574 struct cache_deferred_req *dreq = &sleeper.handle;
577 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
578 dreq->revisit = cache_restart_thread;
580 ret = setup_deferral(dreq, item);
584 if (wait_for_completion_interruptible_timeout(
585 &sleeper.completion, req->thread_wait) <= 0) {
586 /* The completion wasn't completed, so we need
589 spin_lock(&cache_defer_lock);
590 if (!hlist_unhashed(&sleeper.handle.hash)) {
591 __unhash_deferred_req(&sleeper.handle);
592 spin_unlock(&cache_defer_lock);
594 /* cache_revisit_request already removed
595 * this from the hash table, but hasn't
596 * called ->revisit yet. It will very soon
597 * and we need to wait for it.
599 spin_unlock(&cache_defer_lock);
600 wait_for_completion(&sleeper.completion);
603 if (test_bit(CACHE_PENDING, &item->flags)) {
604 /* item is still pending, try request
609 /* only return success if we actually deferred the
610 * request. In this case we waited until it was
611 * answered so no deferral has happened - rather
612 * an answer already exists.
617 static int cache_defer_req(struct cache_req *req, struct cache_head *item)
619 struct cache_deferred_req *dreq;
622 if (cache_defer_cnt >= DFR_MAX) {
623 /* too much in the cache, randomly drop this one,
624 * or continue and drop the oldest
629 if (req->thread_wait) {
630 ret = cache_wait_req(req, item);
631 if (ret != -ETIMEDOUT)
634 dreq = req->defer(req);
637 return setup_deferral(dreq, item);
640 static void cache_revisit_request(struct cache_head *item)
642 struct cache_deferred_req *dreq;
643 struct list_head pending;
644 struct hlist_node *lp, *tmp;
645 int hash = DFR_HASH(item);
647 INIT_LIST_HEAD(&pending);
648 spin_lock(&cache_defer_lock);
650 hlist_for_each_entry_safe(dreq, lp, tmp, &cache_defer_hash[hash], hash)
651 if (dreq->item == item) {
652 __unhash_deferred_req(dreq);
653 list_add(&dreq->recent, &pending);
656 spin_unlock(&cache_defer_lock);
658 while (!list_empty(&pending)) {
659 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
660 list_del_init(&dreq->recent);
661 dreq->revisit(dreq, 0);
665 void cache_clean_deferred(void *owner)
667 struct cache_deferred_req *dreq, *tmp;
668 struct list_head pending;
671 INIT_LIST_HEAD(&pending);
672 spin_lock(&cache_defer_lock);
674 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
675 if (dreq->owner == owner) {
676 __unhash_deferred_req(dreq);
677 list_add(&dreq->recent, &pending);
680 spin_unlock(&cache_defer_lock);
682 while (!list_empty(&pending)) {
683 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
684 list_del_init(&dreq->recent);
685 dreq->revisit(dreq, 1);
690 * communicate with user-space
692 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
693 * On read, you get a full request, or block.
694 * On write, an update request is processed.
695 * Poll works if anything to read, and always allows write.
697 * Implemented by linked list of requests. Each open file has
698 * a ->private that also exists in this list. New requests are added
699 * to the end and may wakeup and preceding readers.
700 * New readers are added to the head. If, on read, an item is found with
701 * CACHE_UPCALLING clear, we free it from the list.
705 static DEFINE_SPINLOCK(queue_lock);
706 static DEFINE_MUTEX(queue_io_mutex);
709 struct list_head list;
710 int reader; /* if 0, then request */
712 struct cache_request {
713 struct cache_queue q;
714 struct cache_head *item;
719 struct cache_reader {
720 struct cache_queue q;
721 int offset; /* if non-0, we have a refcnt on next request */
724 static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
725 loff_t *ppos, struct cache_detail *cd)
727 struct cache_reader *rp = filp->private_data;
728 struct cache_request *rq;
729 struct inode *inode = filp->f_path.dentry->d_inode;
735 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
736 * readers on this file */
738 spin_lock(&queue_lock);
739 /* need to find next request */
740 while (rp->q.list.next != &cd->queue &&
741 list_entry(rp->q.list.next, struct cache_queue, list)
743 struct list_head *next = rp->q.list.next;
744 list_move(&rp->q.list, next);
746 if (rp->q.list.next == &cd->queue) {
747 spin_unlock(&queue_lock);
748 mutex_unlock(&inode->i_mutex);
752 rq = container_of(rp->q.list.next, struct cache_request, q.list);
753 BUG_ON(rq->q.reader);
756 spin_unlock(&queue_lock);
758 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
760 spin_lock(&queue_lock);
761 list_move(&rp->q.list, &rq->q.list);
762 spin_unlock(&queue_lock);
764 if (rp->offset + count > rq->len)
765 count = rq->len - rp->offset;
767 if (copy_to_user(buf, rq->buf + rp->offset, count))
770 if (rp->offset >= rq->len) {
772 spin_lock(&queue_lock);
773 list_move(&rp->q.list, &rq->q.list);
774 spin_unlock(&queue_lock);
779 if (rp->offset == 0) {
780 /* need to release rq */
781 spin_lock(&queue_lock);
783 if (rq->readers == 0 &&
784 !test_bit(CACHE_PENDING, &rq->item->flags)) {
785 list_del(&rq->q.list);
786 spin_unlock(&queue_lock);
787 cache_put(rq->item, cd);
791 spin_unlock(&queue_lock);
795 mutex_unlock(&inode->i_mutex);
796 return err ? err : count;
799 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
800 size_t count, struct cache_detail *cd)
804 if (copy_from_user(kaddr, buf, count))
807 ret = cd->cache_parse(cd, kaddr, count);
813 static ssize_t cache_slow_downcall(const char __user *buf,
814 size_t count, struct cache_detail *cd)
816 static char write_buf[8192]; /* protected by queue_io_mutex */
817 ssize_t ret = -EINVAL;
819 if (count >= sizeof(write_buf))
821 mutex_lock(&queue_io_mutex);
822 ret = cache_do_downcall(write_buf, buf, count, cd);
823 mutex_unlock(&queue_io_mutex);
828 static ssize_t cache_downcall(struct address_space *mapping,
829 const char __user *buf,
830 size_t count, struct cache_detail *cd)
834 ssize_t ret = -ENOMEM;
836 if (count >= PAGE_CACHE_SIZE)
839 page = find_or_create_page(mapping, 0, GFP_KERNEL);
844 ret = cache_do_downcall(kaddr, buf, count, cd);
847 page_cache_release(page);
850 return cache_slow_downcall(buf, count, cd);
853 static ssize_t cache_write(struct file *filp, const char __user *buf,
854 size_t count, loff_t *ppos,
855 struct cache_detail *cd)
857 struct address_space *mapping = filp->f_mapping;
858 struct inode *inode = filp->f_path.dentry->d_inode;
859 ssize_t ret = -EINVAL;
861 if (!cd->cache_parse)
864 mutex_lock(&inode->i_mutex);
865 ret = cache_downcall(mapping, buf, count, cd);
866 mutex_unlock(&inode->i_mutex);
871 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
873 static unsigned int cache_poll(struct file *filp, poll_table *wait,
874 struct cache_detail *cd)
877 struct cache_reader *rp = filp->private_data;
878 struct cache_queue *cq;
880 poll_wait(filp, &queue_wait, wait);
882 /* alway allow write */
883 mask = POLL_OUT | POLLWRNORM;
888 spin_lock(&queue_lock);
890 for (cq= &rp->q; &cq->list != &cd->queue;
891 cq = list_entry(cq->list.next, struct cache_queue, list))
893 mask |= POLLIN | POLLRDNORM;
896 spin_unlock(&queue_lock);
900 static int cache_ioctl(struct inode *ino, struct file *filp,
901 unsigned int cmd, unsigned long arg,
902 struct cache_detail *cd)
905 struct cache_reader *rp = filp->private_data;
906 struct cache_queue *cq;
908 if (cmd != FIONREAD || !rp)
911 spin_lock(&queue_lock);
913 /* only find the length remaining in current request,
914 * or the length of the next request
916 for (cq= &rp->q; &cq->list != &cd->queue;
917 cq = list_entry(cq->list.next, struct cache_queue, list))
919 struct cache_request *cr =
920 container_of(cq, struct cache_request, q);
921 len = cr->len - rp->offset;
924 spin_unlock(&queue_lock);
926 return put_user(len, (int __user *)arg);
929 static int cache_open(struct inode *inode, struct file *filp,
930 struct cache_detail *cd)
932 struct cache_reader *rp = NULL;
934 if (!cd || !try_module_get(cd->owner))
936 nonseekable_open(inode, filp);
937 if (filp->f_mode & FMODE_READ) {
938 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
943 atomic_inc(&cd->readers);
944 spin_lock(&queue_lock);
945 list_add(&rp->q.list, &cd->queue);
946 spin_unlock(&queue_lock);
948 filp->private_data = rp;
952 static int cache_release(struct inode *inode, struct file *filp,
953 struct cache_detail *cd)
955 struct cache_reader *rp = filp->private_data;
958 spin_lock(&queue_lock);
960 struct cache_queue *cq;
961 for (cq= &rp->q; &cq->list != &cd->queue;
962 cq = list_entry(cq->list.next, struct cache_queue, list))
964 container_of(cq, struct cache_request, q)
970 list_del(&rp->q.list);
971 spin_unlock(&queue_lock);
973 filp->private_data = NULL;
976 cd->last_close = seconds_since_boot();
977 atomic_dec(&cd->readers);
979 module_put(cd->owner);
985 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
987 struct cache_queue *cq;
988 spin_lock(&queue_lock);
989 list_for_each_entry(cq, &detail->queue, list)
991 struct cache_request *cr = container_of(cq, struct cache_request, q);
994 if (cr->readers != 0)
996 list_del(&cr->q.list);
997 spin_unlock(&queue_lock);
998 cache_put(cr->item, detail);
1003 spin_unlock(&queue_lock);
1007 * Support routines for text-based upcalls.
1008 * Fields are separated by spaces.
1009 * Fields are either mangled to quote space tab newline slosh with slosh
1010 * or a hexified with a leading \x
1011 * Record is terminated with newline.
1015 void qword_add(char **bpp, int *lp, char *str)
1021 if (len < 0) return;
1023 while ((c=*str++) && len)
1031 *bp++ = '0' + ((c & 0300)>>6);
1032 *bp++ = '0' + ((c & 0070)>>3);
1033 *bp++ = '0' + ((c & 0007)>>0);
1041 if (c || len <1) len = -1;
1049 EXPORT_SYMBOL_GPL(qword_add);
1051 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1056 if (len < 0) return;
1062 while (blen && len >= 2) {
1063 unsigned char c = *buf++;
1064 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1065 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1070 if (blen || len<1) len = -1;
1078 EXPORT_SYMBOL_GPL(qword_addhex);
1080 static void warn_no_listener(struct cache_detail *detail)
1082 if (detail->last_warn != detail->last_close) {
1083 detail->last_warn = detail->last_close;
1084 if (detail->warn_no_listener)
1085 detail->warn_no_listener(detail, detail->last_close != 0);
1089 static bool cache_listeners_exist(struct cache_detail *detail)
1091 if (atomic_read(&detail->readers))
1093 if (detail->last_close == 0)
1094 /* This cache was never opened */
1096 if (detail->last_close < seconds_since_boot() - 30)
1098 * We allow for the possibility that someone might
1099 * restart a userspace daemon without restarting the
1100 * server; but after 30 seconds, we give up.
1107 * register an upcall request to user-space and queue it up for read() by the
1110 * Each request is at most one page long.
1112 int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1113 void (*cache_request)(struct cache_detail *,
1114 struct cache_head *,
1120 struct cache_request *crq;
1124 if (!cache_listeners_exist(detail)) {
1125 warn_no_listener(detail);
1129 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1133 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1139 bp = buf; len = PAGE_SIZE;
1141 cache_request(detail, h, &bp, &len);
1149 crq->item = cache_get(h);
1151 crq->len = PAGE_SIZE - len;
1153 spin_lock(&queue_lock);
1154 list_add_tail(&crq->q.list, &detail->queue);
1155 spin_unlock(&queue_lock);
1156 wake_up(&queue_wait);
1159 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1162 * parse a message from user-space and pass it
1163 * to an appropriate cache
1164 * Messages are, like requests, separated into fields by
1165 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1168 * reply cachename expiry key ... content....
1170 * key and content are both parsed by cache
1173 #define isodigit(c) (isdigit(c) && c <= '7')
1174 int qword_get(char **bpp, char *dest, int bufsize)
1176 /* return bytes copied, or -1 on error */
1180 while (*bp == ' ') bp++;
1182 if (bp[0] == '\\' && bp[1] == 'x') {
1185 while (len < bufsize) {
1188 h = hex_to_bin(bp[0]);
1192 l = hex_to_bin(bp[1]);
1196 *dest++ = (h << 4) | l;
1201 /* text with \nnn octal quoting */
1202 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1204 isodigit(bp[1]) && (bp[1] <= '3') &&
1207 int byte = (*++bp -'0');
1209 byte = (byte << 3) | (*bp++ - '0');
1210 byte = (byte << 3) | (*bp++ - '0');
1220 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1222 while (*bp == ' ') bp++;
1227 EXPORT_SYMBOL_GPL(qword_get);
1231 * support /proc/sunrpc/cache/$CACHENAME/content
1233 * We call ->cache_show passing NULL for the item to
1234 * get a header, then pass each real item in the cache
1238 struct cache_detail *cd;
1241 static void *c_start(struct seq_file *m, loff_t *pos)
1242 __acquires(cd->hash_lock)
1245 unsigned hash, entry;
1246 struct cache_head *ch;
1247 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1250 read_lock(&cd->hash_lock);
1252 return SEQ_START_TOKEN;
1254 entry = n & ((1LL<<32) - 1);
1256 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1259 n &= ~((1LL<<32) - 1);
1263 } while(hash < cd->hash_size &&
1264 cd->hash_table[hash]==NULL);
1265 if (hash >= cd->hash_size)
1268 return cd->hash_table[hash];
1271 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1273 struct cache_head *ch = p;
1274 int hash = (*pos >> 32);
1275 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1277 if (p == SEQ_START_TOKEN)
1279 else if (ch->next == NULL) {
1286 *pos &= ~((1LL<<32) - 1);
1287 while (hash < cd->hash_size &&
1288 cd->hash_table[hash] == NULL) {
1292 if (hash >= cd->hash_size)
1295 return cd->hash_table[hash];
1298 static void c_stop(struct seq_file *m, void *p)
1299 __releases(cd->hash_lock)
1301 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1302 read_unlock(&cd->hash_lock);
1305 static int c_show(struct seq_file *m, void *p)
1307 struct cache_head *cp = p;
1308 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1310 if (p == SEQ_START_TOKEN)
1311 return cd->cache_show(m, cd, NULL);
1314 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1315 convert_to_wallclock(cp->expiry_time),
1316 atomic_read(&cp->ref.refcount), cp->flags);
1318 if (cache_check(cd, cp, NULL))
1319 /* cache_check does a cache_put on failure */
1320 seq_printf(m, "# ");
1324 return cd->cache_show(m, cd, cp);
1327 static const struct seq_operations cache_content_op = {
1334 static int content_open(struct inode *inode, struct file *file,
1335 struct cache_detail *cd)
1339 if (!cd || !try_module_get(cd->owner))
1341 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1343 module_put(cd->owner);
1351 static int content_release(struct inode *inode, struct file *file,
1352 struct cache_detail *cd)
1354 int ret = seq_release_private(inode, file);
1355 module_put(cd->owner);
1359 static int open_flush(struct inode *inode, struct file *file,
1360 struct cache_detail *cd)
1362 if (!cd || !try_module_get(cd->owner))
1364 return nonseekable_open(inode, file);
1367 static int release_flush(struct inode *inode, struct file *file,
1368 struct cache_detail *cd)
1370 module_put(cd->owner);
1374 static ssize_t read_flush(struct file *file, char __user *buf,
1375 size_t count, loff_t *ppos,
1376 struct cache_detail *cd)
1379 unsigned long p = *ppos;
1382 sprintf(tbuf, "%lu\n", convert_to_wallclock(cd->flush_time));
1389 if (copy_to_user(buf, (void*)(tbuf+p), len))
1395 static ssize_t write_flush(struct file *file, const char __user *buf,
1396 size_t count, loff_t *ppos,
1397 struct cache_detail *cd)
1402 if (*ppos || count > sizeof(tbuf)-1)
1404 if (copy_from_user(tbuf, buf, count))
1407 simple_strtoul(tbuf, &ep, 0);
1408 if (*ep && *ep != '\n')
1412 cd->flush_time = get_expiry(&bp);
1413 cd->nextcheck = seconds_since_boot();
1420 static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1421 size_t count, loff_t *ppos)
1423 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1425 return cache_read(filp, buf, count, ppos, cd);
1428 static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1429 size_t count, loff_t *ppos)
1431 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1433 return cache_write(filp, buf, count, ppos, cd);
1436 static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1438 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1440 return cache_poll(filp, wait, cd);
1443 static long cache_ioctl_procfs(struct file *filp,
1444 unsigned int cmd, unsigned long arg)
1447 struct inode *inode = filp->f_path.dentry->d_inode;
1448 struct cache_detail *cd = PDE(inode)->data;
1451 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1457 static int cache_open_procfs(struct inode *inode, struct file *filp)
1459 struct cache_detail *cd = PDE(inode)->data;
1461 return cache_open(inode, filp, cd);
1464 static int cache_release_procfs(struct inode *inode, struct file *filp)
1466 struct cache_detail *cd = PDE(inode)->data;
1468 return cache_release(inode, filp, cd);
1471 static const struct file_operations cache_file_operations_procfs = {
1472 .owner = THIS_MODULE,
1473 .llseek = no_llseek,
1474 .read = cache_read_procfs,
1475 .write = cache_write_procfs,
1476 .poll = cache_poll_procfs,
1477 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
1478 .open = cache_open_procfs,
1479 .release = cache_release_procfs,
1482 static int content_open_procfs(struct inode *inode, struct file *filp)
1484 struct cache_detail *cd = PDE(inode)->data;
1486 return content_open(inode, filp, cd);
1489 static int content_release_procfs(struct inode *inode, struct file *filp)
1491 struct cache_detail *cd = PDE(inode)->data;
1493 return content_release(inode, filp, cd);
1496 static const struct file_operations content_file_operations_procfs = {
1497 .open = content_open_procfs,
1499 .llseek = seq_lseek,
1500 .release = content_release_procfs,
1503 static int open_flush_procfs(struct inode *inode, struct file *filp)
1505 struct cache_detail *cd = PDE(inode)->data;
1507 return open_flush(inode, filp, cd);
1510 static int release_flush_procfs(struct inode *inode, struct file *filp)
1512 struct cache_detail *cd = PDE(inode)->data;
1514 return release_flush(inode, filp, cd);
1517 static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1518 size_t count, loff_t *ppos)
1520 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1522 return read_flush(filp, buf, count, ppos, cd);
1525 static ssize_t write_flush_procfs(struct file *filp,
1526 const char __user *buf,
1527 size_t count, loff_t *ppos)
1529 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1531 return write_flush(filp, buf, count, ppos, cd);
1534 static const struct file_operations cache_flush_operations_procfs = {
1535 .open = open_flush_procfs,
1536 .read = read_flush_procfs,
1537 .write = write_flush_procfs,
1538 .release = release_flush_procfs,
1541 static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
1543 struct sunrpc_net *sn;
1545 if (cd->u.procfs.proc_ent == NULL)
1547 if (cd->u.procfs.flush_ent)
1548 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1549 if (cd->u.procfs.channel_ent)
1550 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1551 if (cd->u.procfs.content_ent)
1552 remove_proc_entry("content", cd->u.procfs.proc_ent);
1553 cd->u.procfs.proc_ent = NULL;
1554 sn = net_generic(net, sunrpc_net_id);
1555 remove_proc_entry(cd->name, sn->proc_net_rpc);
1558 #ifdef CONFIG_PROC_FS
1559 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1561 struct proc_dir_entry *p;
1562 struct sunrpc_net *sn;
1564 sn = net_generic(net, sunrpc_net_id);
1565 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
1566 if (cd->u.procfs.proc_ent == NULL)
1568 cd->u.procfs.channel_ent = NULL;
1569 cd->u.procfs.content_ent = NULL;
1571 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1572 cd->u.procfs.proc_ent,
1573 &cache_flush_operations_procfs, cd);
1574 cd->u.procfs.flush_ent = p;
1578 if (cd->cache_upcall || cd->cache_parse) {
1579 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1580 cd->u.procfs.proc_ent,
1581 &cache_file_operations_procfs, cd);
1582 cd->u.procfs.channel_ent = p;
1586 if (cd->cache_show) {
1587 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1588 cd->u.procfs.proc_ent,
1589 &content_file_operations_procfs, cd);
1590 cd->u.procfs.content_ent = p;
1596 remove_cache_proc_entries(cd, net);
1599 #else /* CONFIG_PROC_FS */
1600 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1606 void __init cache_initialize(void)
1608 INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner, do_cache_clean);
1611 int cache_register_net(struct cache_detail *cd, struct net *net)
1615 sunrpc_init_cache_detail(cd);
1616 ret = create_cache_proc_entries(cd, net);
1618 sunrpc_destroy_cache_detail(cd);
1622 int cache_register(struct cache_detail *cd)
1624 return cache_register_net(cd, &init_net);
1626 EXPORT_SYMBOL_GPL(cache_register);
1628 void cache_unregister_net(struct cache_detail *cd, struct net *net)
1630 remove_cache_proc_entries(cd, net);
1631 sunrpc_destroy_cache_detail(cd);
1634 void cache_unregister(struct cache_detail *cd)
1636 cache_unregister_net(cd, &init_net);
1638 EXPORT_SYMBOL_GPL(cache_unregister);
1640 static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1641 size_t count, loff_t *ppos)
1643 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1645 return cache_read(filp, buf, count, ppos, cd);
1648 static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1649 size_t count, loff_t *ppos)
1651 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1653 return cache_write(filp, buf, count, ppos, cd);
1656 static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1658 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1660 return cache_poll(filp, wait, cd);
1663 static long cache_ioctl_pipefs(struct file *filp,
1664 unsigned int cmd, unsigned long arg)
1666 struct inode *inode = filp->f_dentry->d_inode;
1667 struct cache_detail *cd = RPC_I(inode)->private;
1671 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1677 static int cache_open_pipefs(struct inode *inode, struct file *filp)
1679 struct cache_detail *cd = RPC_I(inode)->private;
1681 return cache_open(inode, filp, cd);
1684 static int cache_release_pipefs(struct inode *inode, struct file *filp)
1686 struct cache_detail *cd = RPC_I(inode)->private;
1688 return cache_release(inode, filp, cd);
1691 const struct file_operations cache_file_operations_pipefs = {
1692 .owner = THIS_MODULE,
1693 .llseek = no_llseek,
1694 .read = cache_read_pipefs,
1695 .write = cache_write_pipefs,
1696 .poll = cache_poll_pipefs,
1697 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
1698 .open = cache_open_pipefs,
1699 .release = cache_release_pipefs,
1702 static int content_open_pipefs(struct inode *inode, struct file *filp)
1704 struct cache_detail *cd = RPC_I(inode)->private;
1706 return content_open(inode, filp, cd);
1709 static int content_release_pipefs(struct inode *inode, struct file *filp)
1711 struct cache_detail *cd = RPC_I(inode)->private;
1713 return content_release(inode, filp, cd);
1716 const struct file_operations content_file_operations_pipefs = {
1717 .open = content_open_pipefs,
1719 .llseek = seq_lseek,
1720 .release = content_release_pipefs,
1723 static int open_flush_pipefs(struct inode *inode, struct file *filp)
1725 struct cache_detail *cd = RPC_I(inode)->private;
1727 return open_flush(inode, filp, cd);
1730 static int release_flush_pipefs(struct inode *inode, struct file *filp)
1732 struct cache_detail *cd = RPC_I(inode)->private;
1734 return release_flush(inode, filp, cd);
1737 static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1738 size_t count, loff_t *ppos)
1740 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1742 return read_flush(filp, buf, count, ppos, cd);
1745 static ssize_t write_flush_pipefs(struct file *filp,
1746 const char __user *buf,
1747 size_t count, loff_t *ppos)
1749 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1751 return write_flush(filp, buf, count, ppos, cd);
1754 const struct file_operations cache_flush_operations_pipefs = {
1755 .open = open_flush_pipefs,
1756 .read = read_flush_pipefs,
1757 .write = write_flush_pipefs,
1758 .release = release_flush_pipefs,
1761 int sunrpc_cache_register_pipefs(struct dentry *parent,
1762 const char *name, mode_t umode,
1763 struct cache_detail *cd)
1769 sunrpc_init_cache_detail(cd);
1771 q.len = strlen(name);
1772 q.hash = full_name_hash(q.name, q.len);
1773 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1775 cd->u.pipefs.dir = dir;
1777 sunrpc_destroy_cache_detail(cd);
1782 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1784 void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1786 rpc_remove_cache_dir(cd->u.pipefs.dir);
1787 cd->u.pipefs.dir = NULL;
1788 sunrpc_destroy_cache_detail(cd);
1790 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);