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 void 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 cache_defer_req(rqstp, h);
273 if (!test_bit(CACHE_PENDING, &h->flags)) {
274 /* Request is not deferred */
275 rv = cache_is_valid(detail, h);
281 cache_put(h, detail);
284 EXPORT_SYMBOL_GPL(cache_check);
287 * caches need to be periodically cleaned.
288 * For this we maintain a list of cache_detail and
289 * a current pointer into that list and into the table
292 * Each time clean_cache is called it finds the next non-empty entry
293 * in the current table and walks the list in that entry
294 * looking for entries that can be removed.
296 * An entry gets removed if:
297 * - The expiry is before current time
298 * - The last_refresh time is before the flush_time for that cache
300 * later we might drop old entries with non-NEVER expiry if that table
301 * is getting 'full' for some definition of 'full'
303 * The question of "how often to scan a table" is an interesting one
304 * and is answered in part by the use of the "nextcheck" field in the
306 * When a scan of a table begins, the nextcheck field is set to a time
307 * that is well into the future.
308 * While scanning, if an expiry time is found that is earlier than the
309 * current nextcheck time, nextcheck is set to that expiry time.
310 * If the flush_time is ever set to a time earlier than the nextcheck
311 * time, the nextcheck time is then set to that flush_time.
313 * A table is then only scanned if the current time is at least
314 * the nextcheck time.
318 static LIST_HEAD(cache_list);
319 static DEFINE_SPINLOCK(cache_list_lock);
320 static struct cache_detail *current_detail;
321 static int current_index;
323 static void do_cache_clean(struct work_struct *work);
324 static struct delayed_work cache_cleaner;
326 static void sunrpc_init_cache_detail(struct cache_detail *cd)
328 rwlock_init(&cd->hash_lock);
329 INIT_LIST_HEAD(&cd->queue);
330 spin_lock(&cache_list_lock);
333 atomic_set(&cd->readers, 0);
336 list_add(&cd->others, &cache_list);
337 spin_unlock(&cache_list_lock);
339 /* start the cleaning process */
340 schedule_delayed_work(&cache_cleaner, 0);
343 static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
346 spin_lock(&cache_list_lock);
347 write_lock(&cd->hash_lock);
348 if (cd->entries || atomic_read(&cd->inuse)) {
349 write_unlock(&cd->hash_lock);
350 spin_unlock(&cache_list_lock);
353 if (current_detail == cd)
354 current_detail = NULL;
355 list_del_init(&cd->others);
356 write_unlock(&cd->hash_lock);
357 spin_unlock(&cache_list_lock);
358 if (list_empty(&cache_list)) {
359 /* module must be being unloaded so its safe to kill the worker */
360 cancel_delayed_work_sync(&cache_cleaner);
364 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
367 /* clean cache tries to find something to clean
369 * It returns 1 if it cleaned something,
370 * 0 if it didn't find anything this time
371 * -1 if it fell off the end of the list.
373 static int cache_clean(void)
376 struct list_head *next;
378 spin_lock(&cache_list_lock);
380 /* find a suitable table if we don't already have one */
381 while (current_detail == NULL ||
382 current_index >= current_detail->hash_size) {
384 next = current_detail->others.next;
386 next = cache_list.next;
387 if (next == &cache_list) {
388 current_detail = NULL;
389 spin_unlock(&cache_list_lock);
392 current_detail = list_entry(next, struct cache_detail, others);
393 if (current_detail->nextcheck > seconds_since_boot())
394 current_index = current_detail->hash_size;
397 current_detail->nextcheck = seconds_since_boot()+30*60;
401 /* find a non-empty bucket in the table */
402 while (current_detail &&
403 current_index < current_detail->hash_size &&
404 current_detail->hash_table[current_index] == NULL)
407 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
409 if (current_detail && current_index < current_detail->hash_size) {
410 struct cache_head *ch, **cp;
411 struct cache_detail *d;
413 write_lock(¤t_detail->hash_lock);
415 /* Ok, now to clean this strand */
417 cp = & current_detail->hash_table[current_index];
418 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
419 if (current_detail->nextcheck > ch->expiry_time)
420 current_detail->nextcheck = ch->expiry_time+1;
421 if (!cache_is_expired(current_detail, ch))
426 current_detail->entries--;
431 write_unlock(¤t_detail->hash_lock);
435 spin_unlock(&cache_list_lock);
437 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
438 cache_dequeue(current_detail, ch);
439 cache_revisit_request(ch);
443 spin_unlock(&cache_list_lock);
449 * We want to regularly clean the cache, so we need to schedule some work ...
451 static void do_cache_clean(struct work_struct *work)
454 if (cache_clean() == -1)
455 delay = round_jiffies_relative(30*HZ);
457 if (list_empty(&cache_list))
461 schedule_delayed_work(&cache_cleaner, delay);
466 * Clean all caches promptly. This just calls cache_clean
467 * repeatedly until we are sure that every cache has had a chance to
470 void cache_flush(void)
472 while (cache_clean() != -1)
474 while (cache_clean() != -1)
477 EXPORT_SYMBOL_GPL(cache_flush);
479 void cache_purge(struct cache_detail *detail)
481 detail->flush_time = LONG_MAX;
482 detail->nextcheck = seconds_since_boot();
484 detail->flush_time = 1;
486 EXPORT_SYMBOL_GPL(cache_purge);
490 * Deferral and Revisiting of Requests.
492 * If a cache lookup finds a pending entry, we
493 * need to defer the request and revisit it later.
494 * All deferred requests are stored in a hash table,
495 * indexed by "struct cache_head *".
496 * As it may be wasteful to store a whole request
497 * structure, we allow the request to provide a
498 * deferred form, which must contain a
499 * 'struct cache_deferred_req'
500 * This cache_deferred_req contains a method to allow
501 * it to be revisited when cache info is available
504 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
505 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
507 #define DFR_MAX 300 /* ??? */
509 static DEFINE_SPINLOCK(cache_defer_lock);
510 static LIST_HEAD(cache_defer_list);
511 static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
512 static int cache_defer_cnt;
514 static void __unhash_deferred_req(struct cache_deferred_req *dreq)
516 hlist_del_init(&dreq->hash);
517 if (!list_empty(&dreq->recent)) {
518 list_del_init(&dreq->recent);
523 static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
525 int hash = DFR_HASH(item);
527 INIT_LIST_HEAD(&dreq->recent);
528 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
531 static void setup_deferral(struct cache_deferred_req *dreq,
532 struct cache_head *item,
538 spin_lock(&cache_defer_lock);
540 __hash_deferred_req(dreq, item);
544 list_add(&dreq->recent, &cache_defer_list);
547 spin_unlock(&cache_defer_lock);
551 struct thread_deferred_req {
552 struct cache_deferred_req handle;
553 struct completion completion;
556 static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
558 struct thread_deferred_req *dr =
559 container_of(dreq, struct thread_deferred_req, handle);
560 complete(&dr->completion);
563 static void cache_wait_req(struct cache_req *req, struct cache_head *item)
565 struct thread_deferred_req sleeper;
566 struct cache_deferred_req *dreq = &sleeper.handle;
568 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
569 dreq->revisit = cache_restart_thread;
571 setup_deferral(dreq, item, 0);
573 if (!test_bit(CACHE_PENDING, &item->flags) ||
574 wait_for_completion_interruptible_timeout(
575 &sleeper.completion, req->thread_wait) <= 0) {
576 /* The completion wasn't completed, so we need
579 spin_lock(&cache_defer_lock);
580 if (!hlist_unhashed(&sleeper.handle.hash)) {
581 __unhash_deferred_req(&sleeper.handle);
582 spin_unlock(&cache_defer_lock);
584 /* cache_revisit_request already removed
585 * this from the hash table, but hasn't
586 * called ->revisit yet. It will very soon
587 * and we need to wait for it.
589 spin_unlock(&cache_defer_lock);
590 wait_for_completion(&sleeper.completion);
595 static void cache_limit_defers(void)
597 /* Make sure we haven't exceed the limit of allowed deferred
600 struct cache_deferred_req *discard = NULL;
602 if (cache_defer_cnt <= DFR_MAX)
605 spin_lock(&cache_defer_lock);
607 /* Consider removing either the first or the last */
608 if (cache_defer_cnt > DFR_MAX) {
609 if (net_random() & 1)
610 discard = list_entry(cache_defer_list.next,
611 struct cache_deferred_req, recent);
613 discard = list_entry(cache_defer_list.prev,
614 struct cache_deferred_req, recent);
615 __unhash_deferred_req(discard);
617 spin_unlock(&cache_defer_lock);
619 discard->revisit(discard, 1);
622 static void cache_defer_req(struct cache_req *req, struct cache_head *item)
624 struct cache_deferred_req *dreq;
626 if (req->thread_wait) {
627 cache_wait_req(req, item);
628 if (!test_bit(CACHE_PENDING, &item->flags))
631 dreq = req->defer(req);
634 setup_deferral(dreq, item, 1);
635 if (!test_bit(CACHE_PENDING, &item->flags))
636 /* Bit could have been cleared before we managed to
637 * set up the deferral, so need to revisit just in case
639 cache_revisit_request(item);
641 cache_limit_defers();
644 static void cache_revisit_request(struct cache_head *item)
646 struct cache_deferred_req *dreq;
647 struct list_head pending;
648 struct hlist_node *lp, *tmp;
649 int hash = DFR_HASH(item);
651 INIT_LIST_HEAD(&pending);
652 spin_lock(&cache_defer_lock);
654 hlist_for_each_entry_safe(dreq, lp, tmp, &cache_defer_hash[hash], hash)
655 if (dreq->item == item) {
656 __unhash_deferred_req(dreq);
657 list_add(&dreq->recent, &pending);
660 spin_unlock(&cache_defer_lock);
662 while (!list_empty(&pending)) {
663 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
664 list_del_init(&dreq->recent);
665 dreq->revisit(dreq, 0);
669 void cache_clean_deferred(void *owner)
671 struct cache_deferred_req *dreq, *tmp;
672 struct list_head pending;
675 INIT_LIST_HEAD(&pending);
676 spin_lock(&cache_defer_lock);
678 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
679 if (dreq->owner == owner) {
680 __unhash_deferred_req(dreq);
681 list_add(&dreq->recent, &pending);
684 spin_unlock(&cache_defer_lock);
686 while (!list_empty(&pending)) {
687 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
688 list_del_init(&dreq->recent);
689 dreq->revisit(dreq, 1);
694 * communicate with user-space
696 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
697 * On read, you get a full request, or block.
698 * On write, an update request is processed.
699 * Poll works if anything to read, and always allows write.
701 * Implemented by linked list of requests. Each open file has
702 * a ->private that also exists in this list. New requests are added
703 * to the end and may wakeup and preceding readers.
704 * New readers are added to the head. If, on read, an item is found with
705 * CACHE_UPCALLING clear, we free it from the list.
709 static DEFINE_SPINLOCK(queue_lock);
710 static DEFINE_MUTEX(queue_io_mutex);
713 struct list_head list;
714 int reader; /* if 0, then request */
716 struct cache_request {
717 struct cache_queue q;
718 struct cache_head *item;
723 struct cache_reader {
724 struct cache_queue q;
725 int offset; /* if non-0, we have a refcnt on next request */
728 static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
729 loff_t *ppos, struct cache_detail *cd)
731 struct cache_reader *rp = filp->private_data;
732 struct cache_request *rq;
733 struct inode *inode = filp->f_path.dentry->d_inode;
739 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
740 * readers on this file */
742 spin_lock(&queue_lock);
743 /* need to find next request */
744 while (rp->q.list.next != &cd->queue &&
745 list_entry(rp->q.list.next, struct cache_queue, list)
747 struct list_head *next = rp->q.list.next;
748 list_move(&rp->q.list, next);
750 if (rp->q.list.next == &cd->queue) {
751 spin_unlock(&queue_lock);
752 mutex_unlock(&inode->i_mutex);
756 rq = container_of(rp->q.list.next, struct cache_request, q.list);
757 BUG_ON(rq->q.reader);
760 spin_unlock(&queue_lock);
762 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
764 spin_lock(&queue_lock);
765 list_move(&rp->q.list, &rq->q.list);
766 spin_unlock(&queue_lock);
768 if (rp->offset + count > rq->len)
769 count = rq->len - rp->offset;
771 if (copy_to_user(buf, rq->buf + rp->offset, count))
774 if (rp->offset >= rq->len) {
776 spin_lock(&queue_lock);
777 list_move(&rp->q.list, &rq->q.list);
778 spin_unlock(&queue_lock);
783 if (rp->offset == 0) {
784 /* need to release rq */
785 spin_lock(&queue_lock);
787 if (rq->readers == 0 &&
788 !test_bit(CACHE_PENDING, &rq->item->flags)) {
789 list_del(&rq->q.list);
790 spin_unlock(&queue_lock);
791 cache_put(rq->item, cd);
795 spin_unlock(&queue_lock);
799 mutex_unlock(&inode->i_mutex);
800 return err ? err : count;
803 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
804 size_t count, struct cache_detail *cd)
808 if (copy_from_user(kaddr, buf, count))
811 ret = cd->cache_parse(cd, kaddr, count);
817 static ssize_t cache_slow_downcall(const char __user *buf,
818 size_t count, struct cache_detail *cd)
820 static char write_buf[8192]; /* protected by queue_io_mutex */
821 ssize_t ret = -EINVAL;
823 if (count >= sizeof(write_buf))
825 mutex_lock(&queue_io_mutex);
826 ret = cache_do_downcall(write_buf, buf, count, cd);
827 mutex_unlock(&queue_io_mutex);
832 static ssize_t cache_downcall(struct address_space *mapping,
833 const char __user *buf,
834 size_t count, struct cache_detail *cd)
838 ssize_t ret = -ENOMEM;
840 if (count >= PAGE_CACHE_SIZE)
843 page = find_or_create_page(mapping, 0, GFP_KERNEL);
848 ret = cache_do_downcall(kaddr, buf, count, cd);
851 page_cache_release(page);
854 return cache_slow_downcall(buf, count, cd);
857 static ssize_t cache_write(struct file *filp, const char __user *buf,
858 size_t count, loff_t *ppos,
859 struct cache_detail *cd)
861 struct address_space *mapping = filp->f_mapping;
862 struct inode *inode = filp->f_path.dentry->d_inode;
863 ssize_t ret = -EINVAL;
865 if (!cd->cache_parse)
868 mutex_lock(&inode->i_mutex);
869 ret = cache_downcall(mapping, buf, count, cd);
870 mutex_unlock(&inode->i_mutex);
875 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
877 static unsigned int cache_poll(struct file *filp, poll_table *wait,
878 struct cache_detail *cd)
881 struct cache_reader *rp = filp->private_data;
882 struct cache_queue *cq;
884 poll_wait(filp, &queue_wait, wait);
886 /* alway allow write */
887 mask = POLL_OUT | POLLWRNORM;
892 spin_lock(&queue_lock);
894 for (cq= &rp->q; &cq->list != &cd->queue;
895 cq = list_entry(cq->list.next, struct cache_queue, list))
897 mask |= POLLIN | POLLRDNORM;
900 spin_unlock(&queue_lock);
904 static int cache_ioctl(struct inode *ino, struct file *filp,
905 unsigned int cmd, unsigned long arg,
906 struct cache_detail *cd)
909 struct cache_reader *rp = filp->private_data;
910 struct cache_queue *cq;
912 if (cmd != FIONREAD || !rp)
915 spin_lock(&queue_lock);
917 /* only find the length remaining in current request,
918 * or the length of the next request
920 for (cq= &rp->q; &cq->list != &cd->queue;
921 cq = list_entry(cq->list.next, struct cache_queue, list))
923 struct cache_request *cr =
924 container_of(cq, struct cache_request, q);
925 len = cr->len - rp->offset;
928 spin_unlock(&queue_lock);
930 return put_user(len, (int __user *)arg);
933 static int cache_open(struct inode *inode, struct file *filp,
934 struct cache_detail *cd)
936 struct cache_reader *rp = NULL;
938 if (!cd || !try_module_get(cd->owner))
940 nonseekable_open(inode, filp);
941 if (filp->f_mode & FMODE_READ) {
942 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
947 atomic_inc(&cd->readers);
948 spin_lock(&queue_lock);
949 list_add(&rp->q.list, &cd->queue);
950 spin_unlock(&queue_lock);
952 filp->private_data = rp;
956 static int cache_release(struct inode *inode, struct file *filp,
957 struct cache_detail *cd)
959 struct cache_reader *rp = filp->private_data;
962 spin_lock(&queue_lock);
964 struct cache_queue *cq;
965 for (cq= &rp->q; &cq->list != &cd->queue;
966 cq = list_entry(cq->list.next, struct cache_queue, list))
968 container_of(cq, struct cache_request, q)
974 list_del(&rp->q.list);
975 spin_unlock(&queue_lock);
977 filp->private_data = NULL;
980 cd->last_close = seconds_since_boot();
981 atomic_dec(&cd->readers);
983 module_put(cd->owner);
989 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
991 struct cache_queue *cq;
992 spin_lock(&queue_lock);
993 list_for_each_entry(cq, &detail->queue, list)
995 struct cache_request *cr = container_of(cq, struct cache_request, q);
998 if (cr->readers != 0)
1000 list_del(&cr->q.list);
1001 spin_unlock(&queue_lock);
1002 cache_put(cr->item, detail);
1007 spin_unlock(&queue_lock);
1011 * Support routines for text-based upcalls.
1012 * Fields are separated by spaces.
1013 * Fields are either mangled to quote space tab newline slosh with slosh
1014 * or a hexified with a leading \x
1015 * Record is terminated with newline.
1019 void qword_add(char **bpp, int *lp, char *str)
1025 if (len < 0) return;
1027 while ((c=*str++) && len)
1035 *bp++ = '0' + ((c & 0300)>>6);
1036 *bp++ = '0' + ((c & 0070)>>3);
1037 *bp++ = '0' + ((c & 0007)>>0);
1045 if (c || len <1) len = -1;
1053 EXPORT_SYMBOL_GPL(qword_add);
1055 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1060 if (len < 0) return;
1066 while (blen && len >= 2) {
1067 unsigned char c = *buf++;
1068 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1069 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1074 if (blen || len<1) len = -1;
1082 EXPORT_SYMBOL_GPL(qword_addhex);
1084 static void warn_no_listener(struct cache_detail *detail)
1086 if (detail->last_warn != detail->last_close) {
1087 detail->last_warn = detail->last_close;
1088 if (detail->warn_no_listener)
1089 detail->warn_no_listener(detail, detail->last_close != 0);
1093 static bool cache_listeners_exist(struct cache_detail *detail)
1095 if (atomic_read(&detail->readers))
1097 if (detail->last_close == 0)
1098 /* This cache was never opened */
1100 if (detail->last_close < seconds_since_boot() - 30)
1102 * We allow for the possibility that someone might
1103 * restart a userspace daemon without restarting the
1104 * server; but after 30 seconds, we give up.
1111 * register an upcall request to user-space and queue it up for read() by the
1114 * Each request is at most one page long.
1116 int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1117 void (*cache_request)(struct cache_detail *,
1118 struct cache_head *,
1124 struct cache_request *crq;
1128 if (!cache_listeners_exist(detail)) {
1129 warn_no_listener(detail);
1133 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1137 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1143 bp = buf; len = PAGE_SIZE;
1145 cache_request(detail, h, &bp, &len);
1153 crq->item = cache_get(h);
1155 crq->len = PAGE_SIZE - len;
1157 spin_lock(&queue_lock);
1158 list_add_tail(&crq->q.list, &detail->queue);
1159 spin_unlock(&queue_lock);
1160 wake_up(&queue_wait);
1163 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1166 * parse a message from user-space and pass it
1167 * to an appropriate cache
1168 * Messages are, like requests, separated into fields by
1169 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1172 * reply cachename expiry key ... content....
1174 * key and content are both parsed by cache
1177 #define isodigit(c) (isdigit(c) && c <= '7')
1178 int qword_get(char **bpp, char *dest, int bufsize)
1180 /* return bytes copied, or -1 on error */
1184 while (*bp == ' ') bp++;
1186 if (bp[0] == '\\' && bp[1] == 'x') {
1189 while (len < bufsize) {
1192 h = hex_to_bin(bp[0]);
1196 l = hex_to_bin(bp[1]);
1200 *dest++ = (h << 4) | l;
1205 /* text with \nnn octal quoting */
1206 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1208 isodigit(bp[1]) && (bp[1] <= '3') &&
1211 int byte = (*++bp -'0');
1213 byte = (byte << 3) | (*bp++ - '0');
1214 byte = (byte << 3) | (*bp++ - '0');
1224 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1226 while (*bp == ' ') bp++;
1231 EXPORT_SYMBOL_GPL(qword_get);
1235 * support /proc/sunrpc/cache/$CACHENAME/content
1237 * We call ->cache_show passing NULL for the item to
1238 * get a header, then pass each real item in the cache
1242 struct cache_detail *cd;
1245 static void *c_start(struct seq_file *m, loff_t *pos)
1246 __acquires(cd->hash_lock)
1249 unsigned hash, entry;
1250 struct cache_head *ch;
1251 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1254 read_lock(&cd->hash_lock);
1256 return SEQ_START_TOKEN;
1258 entry = n & ((1LL<<32) - 1);
1260 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1263 n &= ~((1LL<<32) - 1);
1267 } while(hash < cd->hash_size &&
1268 cd->hash_table[hash]==NULL);
1269 if (hash >= cd->hash_size)
1272 return cd->hash_table[hash];
1275 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1277 struct cache_head *ch = p;
1278 int hash = (*pos >> 32);
1279 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1281 if (p == SEQ_START_TOKEN)
1283 else if (ch->next == NULL) {
1290 *pos &= ~((1LL<<32) - 1);
1291 while (hash < cd->hash_size &&
1292 cd->hash_table[hash] == NULL) {
1296 if (hash >= cd->hash_size)
1299 return cd->hash_table[hash];
1302 static void c_stop(struct seq_file *m, void *p)
1303 __releases(cd->hash_lock)
1305 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1306 read_unlock(&cd->hash_lock);
1309 static int c_show(struct seq_file *m, void *p)
1311 struct cache_head *cp = p;
1312 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1314 if (p == SEQ_START_TOKEN)
1315 return cd->cache_show(m, cd, NULL);
1318 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1319 convert_to_wallclock(cp->expiry_time),
1320 atomic_read(&cp->ref.refcount), cp->flags);
1322 if (cache_check(cd, cp, NULL))
1323 /* cache_check does a cache_put on failure */
1324 seq_printf(m, "# ");
1328 return cd->cache_show(m, cd, cp);
1331 static const struct seq_operations cache_content_op = {
1338 static int content_open(struct inode *inode, struct file *file,
1339 struct cache_detail *cd)
1343 if (!cd || !try_module_get(cd->owner))
1345 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1347 module_put(cd->owner);
1355 static int content_release(struct inode *inode, struct file *file,
1356 struct cache_detail *cd)
1358 int ret = seq_release_private(inode, file);
1359 module_put(cd->owner);
1363 static int open_flush(struct inode *inode, struct file *file,
1364 struct cache_detail *cd)
1366 if (!cd || !try_module_get(cd->owner))
1368 return nonseekable_open(inode, file);
1371 static int release_flush(struct inode *inode, struct file *file,
1372 struct cache_detail *cd)
1374 module_put(cd->owner);
1378 static ssize_t read_flush(struct file *file, char __user *buf,
1379 size_t count, loff_t *ppos,
1380 struct cache_detail *cd)
1383 unsigned long p = *ppos;
1386 sprintf(tbuf, "%lu\n", convert_to_wallclock(cd->flush_time));
1393 if (copy_to_user(buf, (void*)(tbuf+p), len))
1399 static ssize_t write_flush(struct file *file, const char __user *buf,
1400 size_t count, loff_t *ppos,
1401 struct cache_detail *cd)
1406 if (*ppos || count > sizeof(tbuf)-1)
1408 if (copy_from_user(tbuf, buf, count))
1411 simple_strtoul(tbuf, &ep, 0);
1412 if (*ep && *ep != '\n')
1416 cd->flush_time = get_expiry(&bp);
1417 cd->nextcheck = seconds_since_boot();
1424 static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1425 size_t count, loff_t *ppos)
1427 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1429 return cache_read(filp, buf, count, ppos, cd);
1432 static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1433 size_t count, loff_t *ppos)
1435 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1437 return cache_write(filp, buf, count, ppos, cd);
1440 static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1442 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1444 return cache_poll(filp, wait, cd);
1447 static long cache_ioctl_procfs(struct file *filp,
1448 unsigned int cmd, unsigned long arg)
1451 struct inode *inode = filp->f_path.dentry->d_inode;
1452 struct cache_detail *cd = PDE(inode)->data;
1455 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1461 static int cache_open_procfs(struct inode *inode, struct file *filp)
1463 struct cache_detail *cd = PDE(inode)->data;
1465 return cache_open(inode, filp, cd);
1468 static int cache_release_procfs(struct inode *inode, struct file *filp)
1470 struct cache_detail *cd = PDE(inode)->data;
1472 return cache_release(inode, filp, cd);
1475 static const struct file_operations cache_file_operations_procfs = {
1476 .owner = THIS_MODULE,
1477 .llseek = no_llseek,
1478 .read = cache_read_procfs,
1479 .write = cache_write_procfs,
1480 .poll = cache_poll_procfs,
1481 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
1482 .open = cache_open_procfs,
1483 .release = cache_release_procfs,
1486 static int content_open_procfs(struct inode *inode, struct file *filp)
1488 struct cache_detail *cd = PDE(inode)->data;
1490 return content_open(inode, filp, cd);
1493 static int content_release_procfs(struct inode *inode, struct file *filp)
1495 struct cache_detail *cd = PDE(inode)->data;
1497 return content_release(inode, filp, cd);
1500 static const struct file_operations content_file_operations_procfs = {
1501 .open = content_open_procfs,
1503 .llseek = seq_lseek,
1504 .release = content_release_procfs,
1507 static int open_flush_procfs(struct inode *inode, struct file *filp)
1509 struct cache_detail *cd = PDE(inode)->data;
1511 return open_flush(inode, filp, cd);
1514 static int release_flush_procfs(struct inode *inode, struct file *filp)
1516 struct cache_detail *cd = PDE(inode)->data;
1518 return release_flush(inode, filp, cd);
1521 static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1522 size_t count, loff_t *ppos)
1524 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1526 return read_flush(filp, buf, count, ppos, cd);
1529 static ssize_t write_flush_procfs(struct file *filp,
1530 const char __user *buf,
1531 size_t count, loff_t *ppos)
1533 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1535 return write_flush(filp, buf, count, ppos, cd);
1538 static const struct file_operations cache_flush_operations_procfs = {
1539 .open = open_flush_procfs,
1540 .read = read_flush_procfs,
1541 .write = write_flush_procfs,
1542 .release = release_flush_procfs,
1545 static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
1547 struct sunrpc_net *sn;
1549 if (cd->u.procfs.proc_ent == NULL)
1551 if (cd->u.procfs.flush_ent)
1552 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1553 if (cd->u.procfs.channel_ent)
1554 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1555 if (cd->u.procfs.content_ent)
1556 remove_proc_entry("content", cd->u.procfs.proc_ent);
1557 cd->u.procfs.proc_ent = NULL;
1558 sn = net_generic(net, sunrpc_net_id);
1559 remove_proc_entry(cd->name, sn->proc_net_rpc);
1562 #ifdef CONFIG_PROC_FS
1563 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1565 struct proc_dir_entry *p;
1566 struct sunrpc_net *sn;
1568 sn = net_generic(net, sunrpc_net_id);
1569 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
1570 if (cd->u.procfs.proc_ent == NULL)
1572 cd->u.procfs.channel_ent = NULL;
1573 cd->u.procfs.content_ent = NULL;
1575 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1576 cd->u.procfs.proc_ent,
1577 &cache_flush_operations_procfs, cd);
1578 cd->u.procfs.flush_ent = p;
1582 if (cd->cache_upcall || cd->cache_parse) {
1583 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1584 cd->u.procfs.proc_ent,
1585 &cache_file_operations_procfs, cd);
1586 cd->u.procfs.channel_ent = p;
1590 if (cd->cache_show) {
1591 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1592 cd->u.procfs.proc_ent,
1593 &content_file_operations_procfs, cd);
1594 cd->u.procfs.content_ent = p;
1600 remove_cache_proc_entries(cd, net);
1603 #else /* CONFIG_PROC_FS */
1604 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1610 void __init cache_initialize(void)
1612 INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner, do_cache_clean);
1615 int cache_register_net(struct cache_detail *cd, struct net *net)
1619 sunrpc_init_cache_detail(cd);
1620 ret = create_cache_proc_entries(cd, net);
1622 sunrpc_destroy_cache_detail(cd);
1626 int cache_register(struct cache_detail *cd)
1628 return cache_register_net(cd, &init_net);
1630 EXPORT_SYMBOL_GPL(cache_register);
1632 void cache_unregister_net(struct cache_detail *cd, struct net *net)
1634 remove_cache_proc_entries(cd, net);
1635 sunrpc_destroy_cache_detail(cd);
1638 void cache_unregister(struct cache_detail *cd)
1640 cache_unregister_net(cd, &init_net);
1642 EXPORT_SYMBOL_GPL(cache_unregister);
1644 static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1645 size_t count, loff_t *ppos)
1647 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1649 return cache_read(filp, buf, count, ppos, cd);
1652 static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1653 size_t count, loff_t *ppos)
1655 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1657 return cache_write(filp, buf, count, ppos, cd);
1660 static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1662 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1664 return cache_poll(filp, wait, cd);
1667 static long cache_ioctl_pipefs(struct file *filp,
1668 unsigned int cmd, unsigned long arg)
1670 struct inode *inode = filp->f_dentry->d_inode;
1671 struct cache_detail *cd = RPC_I(inode)->private;
1675 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1681 static int cache_open_pipefs(struct inode *inode, struct file *filp)
1683 struct cache_detail *cd = RPC_I(inode)->private;
1685 return cache_open(inode, filp, cd);
1688 static int cache_release_pipefs(struct inode *inode, struct file *filp)
1690 struct cache_detail *cd = RPC_I(inode)->private;
1692 return cache_release(inode, filp, cd);
1695 const struct file_operations cache_file_operations_pipefs = {
1696 .owner = THIS_MODULE,
1697 .llseek = no_llseek,
1698 .read = cache_read_pipefs,
1699 .write = cache_write_pipefs,
1700 .poll = cache_poll_pipefs,
1701 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
1702 .open = cache_open_pipefs,
1703 .release = cache_release_pipefs,
1706 static int content_open_pipefs(struct inode *inode, struct file *filp)
1708 struct cache_detail *cd = RPC_I(inode)->private;
1710 return content_open(inode, filp, cd);
1713 static int content_release_pipefs(struct inode *inode, struct file *filp)
1715 struct cache_detail *cd = RPC_I(inode)->private;
1717 return content_release(inode, filp, cd);
1720 const struct file_operations content_file_operations_pipefs = {
1721 .open = content_open_pipefs,
1723 .llseek = seq_lseek,
1724 .release = content_release_pipefs,
1727 static int open_flush_pipefs(struct inode *inode, struct file *filp)
1729 struct cache_detail *cd = RPC_I(inode)->private;
1731 return open_flush(inode, filp, cd);
1734 static int release_flush_pipefs(struct inode *inode, struct file *filp)
1736 struct cache_detail *cd = RPC_I(inode)->private;
1738 return release_flush(inode, filp, cd);
1741 static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1742 size_t count, loff_t *ppos)
1744 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1746 return read_flush(filp, buf, count, ppos, cd);
1749 static ssize_t write_flush_pipefs(struct file *filp,
1750 const char __user *buf,
1751 size_t count, loff_t *ppos)
1753 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1755 return write_flush(filp, buf, count, ppos, cd);
1758 const struct file_operations cache_flush_operations_pipefs = {
1759 .open = open_flush_pipefs,
1760 .read = read_flush_pipefs,
1761 .write = write_flush_pipefs,
1762 .release = release_flush_pipefs,
1765 int sunrpc_cache_register_pipefs(struct dentry *parent,
1766 const char *name, mode_t umode,
1767 struct cache_detail *cd)
1773 sunrpc_init_cache_detail(cd);
1775 q.len = strlen(name);
1776 q.hash = full_name_hash(q.name, q.len);
1777 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1779 cd->u.pipefs.dir = dir;
1781 sunrpc_destroy_cache_detail(cd);
1786 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1788 void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1790 rpc_remove_cache_dir(cd->u.pipefs.dir);
1791 cd->u.pipefs.dir = NULL;
1792 sunrpc_destroy_cache_detail(cd);
1794 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);