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>
35 #include <linux/sunrpc/rpc_pipe_fs.h>
38 #define RPCDBG_FACILITY RPCDBG_CACHE
40 static bool cache_defer_req(struct cache_req *req, struct cache_head *item);
41 static void cache_revisit_request(struct cache_head *item);
43 static void cache_init(struct cache_head *h)
45 time_t now = seconds_since_boot();
49 h->expiry_time = now + CACHE_NEW_EXPIRY;
50 h->last_refresh = now;
53 static inline int cache_is_expired(struct cache_detail *detail, struct cache_head *h)
55 return (h->expiry_time < seconds_since_boot()) ||
56 (detail->flush_time > h->last_refresh);
59 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
60 struct cache_head *key, int hash)
62 struct cache_head **head, **hp;
63 struct cache_head *new = NULL, *freeme = NULL;
65 head = &detail->hash_table[hash];
67 read_lock(&detail->hash_lock);
69 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
70 struct cache_head *tmp = *hp;
71 if (detail->match(tmp, key)) {
72 if (cache_is_expired(detail, tmp))
73 /* This entry is expired, we will discard it. */
76 read_unlock(&detail->hash_lock);
80 read_unlock(&detail->hash_lock);
81 /* Didn't find anything, insert an empty entry */
83 new = detail->alloc();
86 /* must fully initialise 'new', else
87 * we might get lose if we need to
91 detail->init(new, key);
93 write_lock(&detail->hash_lock);
95 /* check if entry appeared while we slept */
96 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
97 struct cache_head *tmp = *hp;
98 if (detail->match(tmp, key)) {
99 if (cache_is_expired(detail, tmp)) {
107 write_unlock(&detail->hash_lock);
108 cache_put(new, detail);
116 write_unlock(&detail->hash_lock);
119 cache_put(freeme, detail);
122 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
125 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
127 static void cache_fresh_locked(struct cache_head *head, time_t expiry)
129 head->expiry_time = expiry;
130 head->last_refresh = seconds_since_boot();
131 set_bit(CACHE_VALID, &head->flags);
134 static void cache_fresh_unlocked(struct cache_head *head,
135 struct cache_detail *detail)
137 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
138 cache_revisit_request(head);
139 cache_dequeue(detail, head);
143 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
144 struct cache_head *new, struct cache_head *old, int hash)
146 /* The 'old' entry is to be replaced by 'new'.
147 * If 'old' is not VALID, we update it directly,
148 * otherwise we need to replace it
150 struct cache_head **head;
151 struct cache_head *tmp;
153 if (!test_bit(CACHE_VALID, &old->flags)) {
154 write_lock(&detail->hash_lock);
155 if (!test_bit(CACHE_VALID, &old->flags)) {
156 if (test_bit(CACHE_NEGATIVE, &new->flags))
157 set_bit(CACHE_NEGATIVE, &old->flags);
159 detail->update(old, new);
160 cache_fresh_locked(old, new->expiry_time);
161 write_unlock(&detail->hash_lock);
162 cache_fresh_unlocked(old, detail);
165 write_unlock(&detail->hash_lock);
167 /* We need to insert a new entry */
168 tmp = detail->alloc();
170 cache_put(old, detail);
174 detail->init(tmp, old);
175 head = &detail->hash_table[hash];
177 write_lock(&detail->hash_lock);
178 if (test_bit(CACHE_NEGATIVE, &new->flags))
179 set_bit(CACHE_NEGATIVE, &tmp->flags);
181 detail->update(tmp, new);
186 cache_fresh_locked(tmp, new->expiry_time);
187 cache_fresh_locked(old, 0);
188 write_unlock(&detail->hash_lock);
189 cache_fresh_unlocked(tmp, detail);
190 cache_fresh_unlocked(old, detail);
191 cache_put(old, detail);
194 EXPORT_SYMBOL_GPL(sunrpc_cache_update);
196 static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
198 if (!cd->cache_upcall)
200 return cd->cache_upcall(cd, h);
203 static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h)
205 if (!test_bit(CACHE_VALID, &h->flags))
209 if (test_bit(CACHE_NEGATIVE, &h->flags))
217 * This is the generic cache management routine for all
218 * the authentication caches.
219 * It checks the currency of a cache item and will (later)
220 * initiate an upcall to fill it if needed.
223 * Returns 0 if the cache_head can be used, or cache_puts it and returns
224 * -EAGAIN if upcall is pending and request has been queued
225 * -ETIMEDOUT if upcall failed or request could not be queue or
226 * upcall completed but item is still invalid (implying that
227 * the cache item has been replaced with a newer one).
228 * -ENOENT if cache entry was negative
230 int cache_check(struct cache_detail *detail,
231 struct cache_head *h, struct cache_req *rqstp)
234 long refresh_age, age;
236 /* First decide return status as best we can */
237 rv = cache_is_valid(detail, h);
239 /* now see if we want to start an upcall */
240 refresh_age = (h->expiry_time - h->last_refresh);
241 age = seconds_since_boot() - h->last_refresh;
246 } else if (rv == -EAGAIN || age > refresh_age/2) {
247 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
249 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
250 switch (cache_make_upcall(detail, h)) {
252 clear_bit(CACHE_PENDING, &h->flags);
253 cache_revisit_request(h);
255 set_bit(CACHE_NEGATIVE, &h->flags);
256 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
257 cache_fresh_unlocked(h, detail);
263 clear_bit(CACHE_PENDING, &h->flags);
264 cache_revisit_request(h);
271 if (!cache_defer_req(rqstp, h)) {
273 * Request was not deferred; handle it as best
276 rv = cache_is_valid(detail, h);
282 cache_put(h, detail);
285 EXPORT_SYMBOL_GPL(cache_check);
288 * caches need to be periodically cleaned.
289 * For this we maintain a list of cache_detail and
290 * a current pointer into that list and into the table
293 * Each time clean_cache is called it finds the next non-empty entry
294 * in the current table and walks the list in that entry
295 * looking for entries that can be removed.
297 * An entry gets removed if:
298 * - The expiry is before current time
299 * - The last_refresh time is before the flush_time for that cache
301 * later we might drop old entries with non-NEVER expiry if that table
302 * is getting 'full' for some definition of 'full'
304 * The question of "how often to scan a table" is an interesting one
305 * and is answered in part by the use of the "nextcheck" field in the
307 * When a scan of a table begins, the nextcheck field is set to a time
308 * that is well into the future.
309 * While scanning, if an expiry time is found that is earlier than the
310 * current nextcheck time, nextcheck is set to that expiry time.
311 * If the flush_time is ever set to a time earlier than the nextcheck
312 * time, the nextcheck time is then set to that flush_time.
314 * A table is then only scanned if the current time is at least
315 * the nextcheck time.
319 static LIST_HEAD(cache_list);
320 static DEFINE_SPINLOCK(cache_list_lock);
321 static struct cache_detail *current_detail;
322 static int current_index;
324 static void do_cache_clean(struct work_struct *work);
325 static struct delayed_work cache_cleaner;
327 static void sunrpc_init_cache_detail(struct cache_detail *cd)
329 rwlock_init(&cd->hash_lock);
330 INIT_LIST_HEAD(&cd->queue);
331 spin_lock(&cache_list_lock);
334 atomic_set(&cd->readers, 0);
337 list_add(&cd->others, &cache_list);
338 spin_unlock(&cache_list_lock);
340 /* start the cleaning process */
341 schedule_delayed_work(&cache_cleaner, 0);
344 static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
347 spin_lock(&cache_list_lock);
348 write_lock(&cd->hash_lock);
349 if (cd->entries || atomic_read(&cd->inuse)) {
350 write_unlock(&cd->hash_lock);
351 spin_unlock(&cache_list_lock);
354 if (current_detail == cd)
355 current_detail = NULL;
356 list_del_init(&cd->others);
357 write_unlock(&cd->hash_lock);
358 spin_unlock(&cache_list_lock);
359 if (list_empty(&cache_list)) {
360 /* module must be being unloaded so its safe to kill the worker */
361 cancel_delayed_work_sync(&cache_cleaner);
365 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
368 /* clean cache tries to find something to clean
370 * It returns 1 if it cleaned something,
371 * 0 if it didn't find anything this time
372 * -1 if it fell off the end of the list.
374 static int cache_clean(void)
377 struct list_head *next;
379 spin_lock(&cache_list_lock);
381 /* find a suitable table if we don't already have one */
382 while (current_detail == NULL ||
383 current_index >= current_detail->hash_size) {
385 next = current_detail->others.next;
387 next = cache_list.next;
388 if (next == &cache_list) {
389 current_detail = NULL;
390 spin_unlock(&cache_list_lock);
393 current_detail = list_entry(next, struct cache_detail, others);
394 if (current_detail->nextcheck > seconds_since_boot())
395 current_index = current_detail->hash_size;
398 current_detail->nextcheck = seconds_since_boot()+30*60;
402 /* find a non-empty bucket in the table */
403 while (current_detail &&
404 current_index < current_detail->hash_size &&
405 current_detail->hash_table[current_index] == NULL)
408 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
410 if (current_detail && current_index < current_detail->hash_size) {
411 struct cache_head *ch, **cp;
412 struct cache_detail *d;
414 write_lock(¤t_detail->hash_lock);
416 /* Ok, now to clean this strand */
418 cp = & current_detail->hash_table[current_index];
419 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
420 if (current_detail->nextcheck > ch->expiry_time)
421 current_detail->nextcheck = ch->expiry_time+1;
422 if (!cache_is_expired(current_detail, ch))
427 current_detail->entries--;
432 write_unlock(¤t_detail->hash_lock);
436 spin_unlock(&cache_list_lock);
438 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
439 cache_dequeue(current_detail, ch);
440 cache_revisit_request(ch);
444 spin_unlock(&cache_list_lock);
450 * We want to regularly clean the cache, so we need to schedule some work ...
452 static void do_cache_clean(struct work_struct *work)
455 if (cache_clean() == -1)
456 delay = round_jiffies_relative(30*HZ);
458 if (list_empty(&cache_list))
462 schedule_delayed_work(&cache_cleaner, delay);
467 * Clean all caches promptly. This just calls cache_clean
468 * repeatedly until we are sure that every cache has had a chance to
471 void cache_flush(void)
473 while (cache_clean() != -1)
475 while (cache_clean() != -1)
478 EXPORT_SYMBOL_GPL(cache_flush);
480 void cache_purge(struct cache_detail *detail)
482 detail->flush_time = LONG_MAX;
483 detail->nextcheck = seconds_since_boot();
485 detail->flush_time = 1;
487 EXPORT_SYMBOL_GPL(cache_purge);
491 * Deferral and Revisiting of Requests.
493 * If a cache lookup finds a pending entry, we
494 * need to defer the request and revisit it later.
495 * All deferred requests are stored in a hash table,
496 * indexed by "struct cache_head *".
497 * As it may be wasteful to store a whole request
498 * structure, we allow the request to provide a
499 * deferred form, which must contain a
500 * 'struct cache_deferred_req'
501 * This cache_deferred_req contains a method to allow
502 * it to be revisited when cache info is available
505 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
506 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
508 #define DFR_MAX 300 /* ??? */
510 static DEFINE_SPINLOCK(cache_defer_lock);
511 static LIST_HEAD(cache_defer_list);
512 static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
513 static int cache_defer_cnt;
515 static void __unhash_deferred_req(struct cache_deferred_req *dreq)
517 hlist_del_init(&dreq->hash);
518 if (!list_empty(&dreq->recent)) {
519 list_del_init(&dreq->recent);
524 static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
526 int hash = DFR_HASH(item);
528 INIT_LIST_HEAD(&dreq->recent);
529 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
532 static void setup_deferral(struct cache_deferred_req *dreq,
533 struct cache_head *item,
539 spin_lock(&cache_defer_lock);
541 __hash_deferred_req(dreq, item);
545 list_add(&dreq->recent, &cache_defer_list);
548 spin_unlock(&cache_defer_lock);
552 struct thread_deferred_req {
553 struct cache_deferred_req handle;
554 struct completion completion;
557 static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
559 struct thread_deferred_req *dr =
560 container_of(dreq, struct thread_deferred_req, handle);
561 complete(&dr->completion);
564 static void cache_wait_req(struct cache_req *req, struct cache_head *item)
566 struct thread_deferred_req sleeper;
567 struct cache_deferred_req *dreq = &sleeper.handle;
569 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
570 dreq->revisit = cache_restart_thread;
572 setup_deferral(dreq, item, 0);
574 if (!test_bit(CACHE_PENDING, &item->flags) ||
575 wait_for_completion_interruptible_timeout(
576 &sleeper.completion, req->thread_wait) <= 0) {
577 /* The completion wasn't completed, so we need
580 spin_lock(&cache_defer_lock);
581 if (!hlist_unhashed(&sleeper.handle.hash)) {
582 __unhash_deferred_req(&sleeper.handle);
583 spin_unlock(&cache_defer_lock);
585 /* cache_revisit_request already removed
586 * this from the hash table, but hasn't
587 * called ->revisit yet. It will very soon
588 * and we need to wait for it.
590 spin_unlock(&cache_defer_lock);
591 wait_for_completion(&sleeper.completion);
596 static void cache_limit_defers(void)
598 /* Make sure we haven't exceed the limit of allowed deferred
601 struct cache_deferred_req *discard = NULL;
603 if (cache_defer_cnt <= DFR_MAX)
606 spin_lock(&cache_defer_lock);
608 /* Consider removing either the first or the last */
609 if (cache_defer_cnt > DFR_MAX) {
610 if (net_random() & 1)
611 discard = list_entry(cache_defer_list.next,
612 struct cache_deferred_req, recent);
614 discard = list_entry(cache_defer_list.prev,
615 struct cache_deferred_req, recent);
616 __unhash_deferred_req(discard);
618 spin_unlock(&cache_defer_lock);
620 discard->revisit(discard, 1);
623 /* Return true if and only if a deferred request is queued. */
624 static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
626 struct cache_deferred_req *dreq;
628 if (req->thread_wait) {
629 cache_wait_req(req, item);
630 if (!test_bit(CACHE_PENDING, &item->flags))
633 dreq = req->defer(req);
636 setup_deferral(dreq, item, 1);
637 if (!test_bit(CACHE_PENDING, &item->flags))
638 /* Bit could have been cleared before we managed to
639 * set up the deferral, so need to revisit just in case
641 cache_revisit_request(item);
643 cache_limit_defers();
647 static void cache_revisit_request(struct cache_head *item)
649 struct cache_deferred_req *dreq;
650 struct list_head pending;
651 struct hlist_node *lp, *tmp;
652 int hash = DFR_HASH(item);
654 INIT_LIST_HEAD(&pending);
655 spin_lock(&cache_defer_lock);
657 hlist_for_each_entry_safe(dreq, lp, tmp, &cache_defer_hash[hash], hash)
658 if (dreq->item == item) {
659 __unhash_deferred_req(dreq);
660 list_add(&dreq->recent, &pending);
663 spin_unlock(&cache_defer_lock);
665 while (!list_empty(&pending)) {
666 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
667 list_del_init(&dreq->recent);
668 dreq->revisit(dreq, 0);
672 void cache_clean_deferred(void *owner)
674 struct cache_deferred_req *dreq, *tmp;
675 struct list_head pending;
678 INIT_LIST_HEAD(&pending);
679 spin_lock(&cache_defer_lock);
681 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
682 if (dreq->owner == owner) {
683 __unhash_deferred_req(dreq);
684 list_add(&dreq->recent, &pending);
687 spin_unlock(&cache_defer_lock);
689 while (!list_empty(&pending)) {
690 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
691 list_del_init(&dreq->recent);
692 dreq->revisit(dreq, 1);
697 * communicate with user-space
699 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
700 * On read, you get a full request, or block.
701 * On write, an update request is processed.
702 * Poll works if anything to read, and always allows write.
704 * Implemented by linked list of requests. Each open file has
705 * a ->private that also exists in this list. New requests are added
706 * to the end and may wakeup and preceding readers.
707 * New readers are added to the head. If, on read, an item is found with
708 * CACHE_UPCALLING clear, we free it from the list.
712 static DEFINE_SPINLOCK(queue_lock);
713 static DEFINE_MUTEX(queue_io_mutex);
716 struct list_head list;
717 int reader; /* if 0, then request */
719 struct cache_request {
720 struct cache_queue q;
721 struct cache_head *item;
726 struct cache_reader {
727 struct cache_queue q;
728 int offset; /* if non-0, we have a refcnt on next request */
731 static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
732 loff_t *ppos, struct cache_detail *cd)
734 struct cache_reader *rp = filp->private_data;
735 struct cache_request *rq;
736 struct inode *inode = filp->f_path.dentry->d_inode;
742 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
743 * readers on this file */
745 spin_lock(&queue_lock);
746 /* need to find next request */
747 while (rp->q.list.next != &cd->queue &&
748 list_entry(rp->q.list.next, struct cache_queue, list)
750 struct list_head *next = rp->q.list.next;
751 list_move(&rp->q.list, next);
753 if (rp->q.list.next == &cd->queue) {
754 spin_unlock(&queue_lock);
755 mutex_unlock(&inode->i_mutex);
759 rq = container_of(rp->q.list.next, struct cache_request, q.list);
760 BUG_ON(rq->q.reader);
763 spin_unlock(&queue_lock);
765 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
767 spin_lock(&queue_lock);
768 list_move(&rp->q.list, &rq->q.list);
769 spin_unlock(&queue_lock);
771 if (rp->offset + count > rq->len)
772 count = rq->len - rp->offset;
774 if (copy_to_user(buf, rq->buf + rp->offset, count))
777 if (rp->offset >= rq->len) {
779 spin_lock(&queue_lock);
780 list_move(&rp->q.list, &rq->q.list);
781 spin_unlock(&queue_lock);
786 if (rp->offset == 0) {
787 /* need to release rq */
788 spin_lock(&queue_lock);
790 if (rq->readers == 0 &&
791 !test_bit(CACHE_PENDING, &rq->item->flags)) {
792 list_del(&rq->q.list);
793 spin_unlock(&queue_lock);
794 cache_put(rq->item, cd);
798 spin_unlock(&queue_lock);
802 mutex_unlock(&inode->i_mutex);
803 return err ? err : count;
806 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
807 size_t count, struct cache_detail *cd)
811 if (copy_from_user(kaddr, buf, count))
814 ret = cd->cache_parse(cd, kaddr, count);
820 static ssize_t cache_slow_downcall(const char __user *buf,
821 size_t count, struct cache_detail *cd)
823 static char write_buf[8192]; /* protected by queue_io_mutex */
824 ssize_t ret = -EINVAL;
826 if (count >= sizeof(write_buf))
828 mutex_lock(&queue_io_mutex);
829 ret = cache_do_downcall(write_buf, buf, count, cd);
830 mutex_unlock(&queue_io_mutex);
835 static ssize_t cache_downcall(struct address_space *mapping,
836 const char __user *buf,
837 size_t count, struct cache_detail *cd)
841 ssize_t ret = -ENOMEM;
843 if (count >= PAGE_CACHE_SIZE)
846 page = find_or_create_page(mapping, 0, GFP_KERNEL);
851 ret = cache_do_downcall(kaddr, buf, count, cd);
854 page_cache_release(page);
857 return cache_slow_downcall(buf, count, cd);
860 static ssize_t cache_write(struct file *filp, const char __user *buf,
861 size_t count, loff_t *ppos,
862 struct cache_detail *cd)
864 struct address_space *mapping = filp->f_mapping;
865 struct inode *inode = filp->f_path.dentry->d_inode;
866 ssize_t ret = -EINVAL;
868 if (!cd->cache_parse)
871 mutex_lock(&inode->i_mutex);
872 ret = cache_downcall(mapping, buf, count, cd);
873 mutex_unlock(&inode->i_mutex);
878 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
880 static unsigned int cache_poll(struct file *filp, poll_table *wait,
881 struct cache_detail *cd)
884 struct cache_reader *rp = filp->private_data;
885 struct cache_queue *cq;
887 poll_wait(filp, &queue_wait, wait);
889 /* alway allow write */
890 mask = POLL_OUT | POLLWRNORM;
895 spin_lock(&queue_lock);
897 for (cq= &rp->q; &cq->list != &cd->queue;
898 cq = list_entry(cq->list.next, struct cache_queue, list))
900 mask |= POLLIN | POLLRDNORM;
903 spin_unlock(&queue_lock);
907 static int cache_ioctl(struct inode *ino, struct file *filp,
908 unsigned int cmd, unsigned long arg,
909 struct cache_detail *cd)
912 struct cache_reader *rp = filp->private_data;
913 struct cache_queue *cq;
915 if (cmd != FIONREAD || !rp)
918 spin_lock(&queue_lock);
920 /* only find the length remaining in current request,
921 * or the length of the next request
923 for (cq= &rp->q; &cq->list != &cd->queue;
924 cq = list_entry(cq->list.next, struct cache_queue, list))
926 struct cache_request *cr =
927 container_of(cq, struct cache_request, q);
928 len = cr->len - rp->offset;
931 spin_unlock(&queue_lock);
933 return put_user(len, (int __user *)arg);
936 static int cache_open(struct inode *inode, struct file *filp,
937 struct cache_detail *cd)
939 struct cache_reader *rp = NULL;
941 if (!cd || !try_module_get(cd->owner))
943 nonseekable_open(inode, filp);
944 if (filp->f_mode & FMODE_READ) {
945 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
950 atomic_inc(&cd->readers);
951 spin_lock(&queue_lock);
952 list_add(&rp->q.list, &cd->queue);
953 spin_unlock(&queue_lock);
955 filp->private_data = rp;
959 static int cache_release(struct inode *inode, struct file *filp,
960 struct cache_detail *cd)
962 struct cache_reader *rp = filp->private_data;
965 spin_lock(&queue_lock);
967 struct cache_queue *cq;
968 for (cq= &rp->q; &cq->list != &cd->queue;
969 cq = list_entry(cq->list.next, struct cache_queue, list))
971 container_of(cq, struct cache_request, q)
977 list_del(&rp->q.list);
978 spin_unlock(&queue_lock);
980 filp->private_data = NULL;
983 cd->last_close = seconds_since_boot();
984 atomic_dec(&cd->readers);
986 module_put(cd->owner);
992 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
994 struct cache_queue *cq;
995 spin_lock(&queue_lock);
996 list_for_each_entry(cq, &detail->queue, list)
998 struct cache_request *cr = container_of(cq, struct cache_request, q);
1001 if (cr->readers != 0)
1003 list_del(&cr->q.list);
1004 spin_unlock(&queue_lock);
1005 cache_put(cr->item, detail);
1010 spin_unlock(&queue_lock);
1014 * Support routines for text-based upcalls.
1015 * Fields are separated by spaces.
1016 * Fields are either mangled to quote space tab newline slosh with slosh
1017 * or a hexified with a leading \x
1018 * Record is terminated with newline.
1022 void qword_add(char **bpp, int *lp, char *str)
1028 if (len < 0) return;
1030 while ((c=*str++) && len)
1038 *bp++ = '0' + ((c & 0300)>>6);
1039 *bp++ = '0' + ((c & 0070)>>3);
1040 *bp++ = '0' + ((c & 0007)>>0);
1048 if (c || len <1) len = -1;
1056 EXPORT_SYMBOL_GPL(qword_add);
1058 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1063 if (len < 0) return;
1069 while (blen && len >= 2) {
1070 unsigned char c = *buf++;
1071 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1072 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1077 if (blen || len<1) len = -1;
1085 EXPORT_SYMBOL_GPL(qword_addhex);
1087 static void warn_no_listener(struct cache_detail *detail)
1089 if (detail->last_warn != detail->last_close) {
1090 detail->last_warn = detail->last_close;
1091 if (detail->warn_no_listener)
1092 detail->warn_no_listener(detail, detail->last_close != 0);
1096 static bool cache_listeners_exist(struct cache_detail *detail)
1098 if (atomic_read(&detail->readers))
1100 if (detail->last_close == 0)
1101 /* This cache was never opened */
1103 if (detail->last_close < seconds_since_boot() - 30)
1105 * We allow for the possibility that someone might
1106 * restart a userspace daemon without restarting the
1107 * server; but after 30 seconds, we give up.
1114 * register an upcall request to user-space and queue it up for read() by the
1117 * Each request is at most one page long.
1119 int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1120 void (*cache_request)(struct cache_detail *,
1121 struct cache_head *,
1127 struct cache_request *crq;
1131 if (!cache_listeners_exist(detail)) {
1132 warn_no_listener(detail);
1136 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1140 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1146 bp = buf; len = PAGE_SIZE;
1148 cache_request(detail, h, &bp, &len);
1156 crq->item = cache_get(h);
1158 crq->len = PAGE_SIZE - len;
1160 spin_lock(&queue_lock);
1161 list_add_tail(&crq->q.list, &detail->queue);
1162 spin_unlock(&queue_lock);
1163 wake_up(&queue_wait);
1166 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1169 * parse a message from user-space and pass it
1170 * to an appropriate cache
1171 * Messages are, like requests, separated into fields by
1172 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1175 * reply cachename expiry key ... content....
1177 * key and content are both parsed by cache
1180 #define isodigit(c) (isdigit(c) && c <= '7')
1181 int qword_get(char **bpp, char *dest, int bufsize)
1183 /* return bytes copied, or -1 on error */
1187 while (*bp == ' ') bp++;
1189 if (bp[0] == '\\' && bp[1] == 'x') {
1192 while (len < bufsize) {
1195 h = hex_to_bin(bp[0]);
1199 l = hex_to_bin(bp[1]);
1203 *dest++ = (h << 4) | l;
1208 /* text with \nnn octal quoting */
1209 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1211 isodigit(bp[1]) && (bp[1] <= '3') &&
1214 int byte = (*++bp -'0');
1216 byte = (byte << 3) | (*bp++ - '0');
1217 byte = (byte << 3) | (*bp++ - '0');
1227 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1229 while (*bp == ' ') bp++;
1234 EXPORT_SYMBOL_GPL(qword_get);
1238 * support /proc/sunrpc/cache/$CACHENAME/content
1240 * We call ->cache_show passing NULL for the item to
1241 * get a header, then pass each real item in the cache
1245 struct cache_detail *cd;
1248 static void *c_start(struct seq_file *m, loff_t *pos)
1249 __acquires(cd->hash_lock)
1252 unsigned hash, entry;
1253 struct cache_head *ch;
1254 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1257 read_lock(&cd->hash_lock);
1259 return SEQ_START_TOKEN;
1261 entry = n & ((1LL<<32) - 1);
1263 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1266 n &= ~((1LL<<32) - 1);
1270 } while(hash < cd->hash_size &&
1271 cd->hash_table[hash]==NULL);
1272 if (hash >= cd->hash_size)
1275 return cd->hash_table[hash];
1278 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1280 struct cache_head *ch = p;
1281 int hash = (*pos >> 32);
1282 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1284 if (p == SEQ_START_TOKEN)
1286 else if (ch->next == NULL) {
1293 *pos &= ~((1LL<<32) - 1);
1294 while (hash < cd->hash_size &&
1295 cd->hash_table[hash] == NULL) {
1299 if (hash >= cd->hash_size)
1302 return cd->hash_table[hash];
1305 static void c_stop(struct seq_file *m, void *p)
1306 __releases(cd->hash_lock)
1308 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1309 read_unlock(&cd->hash_lock);
1312 static int c_show(struct seq_file *m, void *p)
1314 struct cache_head *cp = p;
1315 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1317 if (p == SEQ_START_TOKEN)
1318 return cd->cache_show(m, cd, NULL);
1321 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1322 convert_to_wallclock(cp->expiry_time),
1323 atomic_read(&cp->ref.refcount), cp->flags);
1325 if (cache_check(cd, cp, NULL))
1326 /* cache_check does a cache_put on failure */
1327 seq_printf(m, "# ");
1331 return cd->cache_show(m, cd, cp);
1334 static const struct seq_operations cache_content_op = {
1341 static int content_open(struct inode *inode, struct file *file,
1342 struct cache_detail *cd)
1346 if (!cd || !try_module_get(cd->owner))
1348 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1350 module_put(cd->owner);
1358 static int content_release(struct inode *inode, struct file *file,
1359 struct cache_detail *cd)
1361 int ret = seq_release_private(inode, file);
1362 module_put(cd->owner);
1366 static int open_flush(struct inode *inode, struct file *file,
1367 struct cache_detail *cd)
1369 if (!cd || !try_module_get(cd->owner))
1371 return nonseekable_open(inode, file);
1374 static int release_flush(struct inode *inode, struct file *file,
1375 struct cache_detail *cd)
1377 module_put(cd->owner);
1381 static ssize_t read_flush(struct file *file, char __user *buf,
1382 size_t count, loff_t *ppos,
1383 struct cache_detail *cd)
1386 unsigned long p = *ppos;
1389 sprintf(tbuf, "%lu\n", convert_to_wallclock(cd->flush_time));
1396 if (copy_to_user(buf, (void*)(tbuf+p), len))
1402 static ssize_t write_flush(struct file *file, const char __user *buf,
1403 size_t count, loff_t *ppos,
1404 struct cache_detail *cd)
1409 if (*ppos || count > sizeof(tbuf)-1)
1411 if (copy_from_user(tbuf, buf, count))
1414 simple_strtoul(tbuf, &ep, 0);
1415 if (*ep && *ep != '\n')
1419 cd->flush_time = get_expiry(&bp);
1420 cd->nextcheck = seconds_since_boot();
1427 static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1428 size_t count, loff_t *ppos)
1430 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1432 return cache_read(filp, buf, count, ppos, cd);
1435 static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1436 size_t count, loff_t *ppos)
1438 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1440 return cache_write(filp, buf, count, ppos, cd);
1443 static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1445 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1447 return cache_poll(filp, wait, cd);
1450 static long cache_ioctl_procfs(struct file *filp,
1451 unsigned int cmd, unsigned long arg)
1453 struct inode *inode = filp->f_path.dentry->d_inode;
1454 struct cache_detail *cd = PDE(inode)->data;
1456 return cache_ioctl(inode, filp, cmd, arg, cd);
1459 static int cache_open_procfs(struct inode *inode, struct file *filp)
1461 struct cache_detail *cd = PDE(inode)->data;
1463 return cache_open(inode, filp, cd);
1466 static int cache_release_procfs(struct inode *inode, struct file *filp)
1468 struct cache_detail *cd = PDE(inode)->data;
1470 return cache_release(inode, filp, cd);
1473 static const struct file_operations cache_file_operations_procfs = {
1474 .owner = THIS_MODULE,
1475 .llseek = no_llseek,
1476 .read = cache_read_procfs,
1477 .write = cache_write_procfs,
1478 .poll = cache_poll_procfs,
1479 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
1480 .open = cache_open_procfs,
1481 .release = cache_release_procfs,
1484 static int content_open_procfs(struct inode *inode, struct file *filp)
1486 struct cache_detail *cd = PDE(inode)->data;
1488 return content_open(inode, filp, cd);
1491 static int content_release_procfs(struct inode *inode, struct file *filp)
1493 struct cache_detail *cd = PDE(inode)->data;
1495 return content_release(inode, filp, cd);
1498 static const struct file_operations content_file_operations_procfs = {
1499 .open = content_open_procfs,
1501 .llseek = seq_lseek,
1502 .release = content_release_procfs,
1505 static int open_flush_procfs(struct inode *inode, struct file *filp)
1507 struct cache_detail *cd = PDE(inode)->data;
1509 return open_flush(inode, filp, cd);
1512 static int release_flush_procfs(struct inode *inode, struct file *filp)
1514 struct cache_detail *cd = PDE(inode)->data;
1516 return release_flush(inode, filp, cd);
1519 static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1520 size_t count, loff_t *ppos)
1522 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1524 return read_flush(filp, buf, count, ppos, cd);
1527 static ssize_t write_flush_procfs(struct file *filp,
1528 const char __user *buf,
1529 size_t count, loff_t *ppos)
1531 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1533 return write_flush(filp, buf, count, ppos, cd);
1536 static const struct file_operations cache_flush_operations_procfs = {
1537 .open = open_flush_procfs,
1538 .read = read_flush_procfs,
1539 .write = write_flush_procfs,
1540 .release = release_flush_procfs,
1541 .llseek = no_llseek,
1544 static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
1546 struct sunrpc_net *sn;
1548 if (cd->u.procfs.proc_ent == NULL)
1550 if (cd->u.procfs.flush_ent)
1551 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1552 if (cd->u.procfs.channel_ent)
1553 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1554 if (cd->u.procfs.content_ent)
1555 remove_proc_entry("content", cd->u.procfs.proc_ent);
1556 cd->u.procfs.proc_ent = NULL;
1557 sn = net_generic(net, sunrpc_net_id);
1558 remove_proc_entry(cd->name, sn->proc_net_rpc);
1561 #ifdef CONFIG_PROC_FS
1562 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1564 struct proc_dir_entry *p;
1565 struct sunrpc_net *sn;
1567 sn = net_generic(net, sunrpc_net_id);
1568 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
1569 if (cd->u.procfs.proc_ent == NULL)
1571 cd->u.procfs.channel_ent = NULL;
1572 cd->u.procfs.content_ent = NULL;
1574 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1575 cd->u.procfs.proc_ent,
1576 &cache_flush_operations_procfs, cd);
1577 cd->u.procfs.flush_ent = p;
1581 if (cd->cache_upcall || cd->cache_parse) {
1582 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1583 cd->u.procfs.proc_ent,
1584 &cache_file_operations_procfs, cd);
1585 cd->u.procfs.channel_ent = p;
1589 if (cd->cache_show) {
1590 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1591 cd->u.procfs.proc_ent,
1592 &content_file_operations_procfs, cd);
1593 cd->u.procfs.content_ent = p;
1599 remove_cache_proc_entries(cd, net);
1602 #else /* CONFIG_PROC_FS */
1603 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1609 void __init cache_initialize(void)
1611 INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner, do_cache_clean);
1614 int cache_register_net(struct cache_detail *cd, struct net *net)
1618 sunrpc_init_cache_detail(cd);
1619 ret = create_cache_proc_entries(cd, net);
1621 sunrpc_destroy_cache_detail(cd);
1625 int cache_register(struct cache_detail *cd)
1627 return cache_register_net(cd, &init_net);
1629 EXPORT_SYMBOL_GPL(cache_register);
1631 void cache_unregister_net(struct cache_detail *cd, struct net *net)
1633 remove_cache_proc_entries(cd, net);
1634 sunrpc_destroy_cache_detail(cd);
1637 void cache_unregister(struct cache_detail *cd)
1639 cache_unregister_net(cd, &init_net);
1641 EXPORT_SYMBOL_GPL(cache_unregister);
1643 static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1644 size_t count, loff_t *ppos)
1646 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1648 return cache_read(filp, buf, count, ppos, cd);
1651 static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1652 size_t count, loff_t *ppos)
1654 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1656 return cache_write(filp, buf, count, ppos, cd);
1659 static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1661 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1663 return cache_poll(filp, wait, cd);
1666 static long cache_ioctl_pipefs(struct file *filp,
1667 unsigned int cmd, unsigned long arg)
1669 struct inode *inode = filp->f_dentry->d_inode;
1670 struct cache_detail *cd = RPC_I(inode)->private;
1672 return cache_ioctl(inode, filp, cmd, arg, cd);
1675 static int cache_open_pipefs(struct inode *inode, struct file *filp)
1677 struct cache_detail *cd = RPC_I(inode)->private;
1679 return cache_open(inode, filp, cd);
1682 static int cache_release_pipefs(struct inode *inode, struct file *filp)
1684 struct cache_detail *cd = RPC_I(inode)->private;
1686 return cache_release(inode, filp, cd);
1689 const struct file_operations cache_file_operations_pipefs = {
1690 .owner = THIS_MODULE,
1691 .llseek = no_llseek,
1692 .read = cache_read_pipefs,
1693 .write = cache_write_pipefs,
1694 .poll = cache_poll_pipefs,
1695 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
1696 .open = cache_open_pipefs,
1697 .release = cache_release_pipefs,
1700 static int content_open_pipefs(struct inode *inode, struct file *filp)
1702 struct cache_detail *cd = RPC_I(inode)->private;
1704 return content_open(inode, filp, cd);
1707 static int content_release_pipefs(struct inode *inode, struct file *filp)
1709 struct cache_detail *cd = RPC_I(inode)->private;
1711 return content_release(inode, filp, cd);
1714 const struct file_operations content_file_operations_pipefs = {
1715 .open = content_open_pipefs,
1717 .llseek = seq_lseek,
1718 .release = content_release_pipefs,
1721 static int open_flush_pipefs(struct inode *inode, struct file *filp)
1723 struct cache_detail *cd = RPC_I(inode)->private;
1725 return open_flush(inode, filp, cd);
1728 static int release_flush_pipefs(struct inode *inode, struct file *filp)
1730 struct cache_detail *cd = RPC_I(inode)->private;
1732 return release_flush(inode, filp, cd);
1735 static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1736 size_t count, loff_t *ppos)
1738 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1740 return read_flush(filp, buf, count, ppos, cd);
1743 static ssize_t write_flush_pipefs(struct file *filp,
1744 const char __user *buf,
1745 size_t count, loff_t *ppos)
1747 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1749 return write_flush(filp, buf, count, ppos, cd);
1752 const struct file_operations cache_flush_operations_pipefs = {
1753 .open = open_flush_pipefs,
1754 .read = read_flush_pipefs,
1755 .write = write_flush_pipefs,
1756 .release = release_flush_pipefs,
1757 .llseek = no_llseek,
1760 int sunrpc_cache_register_pipefs(struct dentry *parent,
1761 const char *name, mode_t umode,
1762 struct cache_detail *cd)
1768 sunrpc_init_cache_detail(cd);
1770 q.len = strlen(name);
1771 q.hash = full_name_hash(q.name, q.len);
1772 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1774 cd->u.pipefs.dir = dir;
1776 sunrpc_destroy_cache_detail(cd);
1781 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1783 void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1785 rpc_remove_cache_dir(cd->u.pipefs.dir);
1786 cd->u.pipefs.dir = NULL;
1787 sunrpc_destroy_cache_detail(cd);
1789 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);