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 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
54 struct cache_head *key, int hash)
56 struct cache_head **head, **hp;
57 struct cache_head *new = NULL, *freeme = NULL;
59 head = &detail->hash_table[hash];
61 read_lock(&detail->hash_lock);
63 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
64 struct cache_head *tmp = *hp;
65 if (detail->match(tmp, key)) {
66 if (cache_is_expired(detail, tmp))
67 /* This entry is expired, we will discard it. */
70 read_unlock(&detail->hash_lock);
74 read_unlock(&detail->hash_lock);
75 /* Didn't find anything, insert an empty entry */
77 new = detail->alloc();
80 /* must fully initialise 'new', else
81 * we might get lose if we need to
85 detail->init(new, key);
87 write_lock(&detail->hash_lock);
89 /* check if entry appeared while we slept */
90 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
91 struct cache_head *tmp = *hp;
92 if (detail->match(tmp, key)) {
93 if (cache_is_expired(detail, tmp)) {
101 write_unlock(&detail->hash_lock);
102 cache_put(new, detail);
110 write_unlock(&detail->hash_lock);
113 cache_put(freeme, detail);
116 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
119 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
121 static void cache_fresh_locked(struct cache_head *head, time_t expiry)
123 head->expiry_time = expiry;
124 head->last_refresh = seconds_since_boot();
125 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
126 set_bit(CACHE_VALID, &head->flags);
129 static void cache_fresh_unlocked(struct cache_head *head,
130 struct cache_detail *detail)
132 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
133 cache_revisit_request(head);
134 cache_dequeue(detail, head);
138 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
139 struct cache_head *new, struct cache_head *old, int hash)
141 /* The 'old' entry is to be replaced by 'new'.
142 * If 'old' is not VALID, we update it directly,
143 * otherwise we need to replace it
145 struct cache_head **head;
146 struct cache_head *tmp;
148 if (!test_bit(CACHE_VALID, &old->flags)) {
149 write_lock(&detail->hash_lock);
150 if (!test_bit(CACHE_VALID, &old->flags)) {
151 if (test_bit(CACHE_NEGATIVE, &new->flags))
152 set_bit(CACHE_NEGATIVE, &old->flags);
154 detail->update(old, new);
155 cache_fresh_locked(old, new->expiry_time);
156 write_unlock(&detail->hash_lock);
157 cache_fresh_unlocked(old, detail);
160 write_unlock(&detail->hash_lock);
162 /* We need to insert a new entry */
163 tmp = detail->alloc();
165 cache_put(old, detail);
169 detail->init(tmp, old);
170 head = &detail->hash_table[hash];
172 write_lock(&detail->hash_lock);
173 if (test_bit(CACHE_NEGATIVE, &new->flags))
174 set_bit(CACHE_NEGATIVE, &tmp->flags);
176 detail->update(tmp, new);
181 cache_fresh_locked(tmp, new->expiry_time);
182 cache_fresh_locked(old, 0);
183 write_unlock(&detail->hash_lock);
184 cache_fresh_unlocked(tmp, detail);
185 cache_fresh_unlocked(old, detail);
186 cache_put(old, detail);
189 EXPORT_SYMBOL_GPL(sunrpc_cache_update);
191 static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
193 if (cd->cache_upcall)
194 return cd->cache_upcall(cd, h);
195 return sunrpc_cache_pipe_upcall(cd, h);
198 static inline int cache_is_valid(struct cache_head *h)
200 if (!test_bit(CACHE_VALID, &h->flags))
204 if (test_bit(CACHE_NEGATIVE, &h->flags))
208 * In combination with write barrier in
209 * sunrpc_cache_update, ensures that anyone
210 * using the cache entry after this sees the
219 static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
223 write_lock(&detail->hash_lock);
224 rv = cache_is_valid(h);
226 set_bit(CACHE_NEGATIVE, &h->flags);
227 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
230 write_unlock(&detail->hash_lock);
231 cache_fresh_unlocked(h, detail);
236 * This is the generic cache management routine for all
237 * the authentication caches.
238 * It checks the currency of a cache item and will (later)
239 * initiate an upcall to fill it if needed.
242 * Returns 0 if the cache_head can be used, or cache_puts it and returns
243 * -EAGAIN if upcall is pending and request has been queued
244 * -ETIMEDOUT if upcall failed or request could not be queue or
245 * upcall completed but item is still invalid (implying that
246 * the cache item has been replaced with a newer one).
247 * -ENOENT if cache entry was negative
249 int cache_check(struct cache_detail *detail,
250 struct cache_head *h, struct cache_req *rqstp)
253 long refresh_age, age;
255 /* First decide return status as best we can */
256 rv = cache_is_valid(h);
258 /* now see if we want to start an upcall */
259 refresh_age = (h->expiry_time - h->last_refresh);
260 age = seconds_since_boot() - h->last_refresh;
265 } else if (rv == -EAGAIN || age > refresh_age/2) {
266 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
268 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
269 switch (cache_make_upcall(detail, h)) {
271 rv = try_to_negate_entry(detail, h);
274 cache_fresh_unlocked(h, detail);
281 if (!cache_defer_req(rqstp, h)) {
283 * Request was not deferred; handle it as best
286 rv = cache_is_valid(h);
292 cache_put(h, detail);
295 EXPORT_SYMBOL_GPL(cache_check);
298 * caches need to be periodically cleaned.
299 * For this we maintain a list of cache_detail and
300 * a current pointer into that list and into the table
303 * Each time cache_clean is called it finds the next non-empty entry
304 * in the current table and walks the list in that entry
305 * looking for entries that can be removed.
307 * An entry gets removed if:
308 * - The expiry is before current time
309 * - The last_refresh time is before the flush_time for that cache
311 * later we might drop old entries with non-NEVER expiry if that table
312 * is getting 'full' for some definition of 'full'
314 * The question of "how often to scan a table" is an interesting one
315 * and is answered in part by the use of the "nextcheck" field in the
317 * When a scan of a table begins, the nextcheck field is set to a time
318 * that is well into the future.
319 * While scanning, if an expiry time is found that is earlier than the
320 * current nextcheck time, nextcheck is set to that expiry time.
321 * If the flush_time is ever set to a time earlier than the nextcheck
322 * time, the nextcheck time is then set to that flush_time.
324 * A table is then only scanned if the current time is at least
325 * the nextcheck time.
329 static LIST_HEAD(cache_list);
330 static DEFINE_SPINLOCK(cache_list_lock);
331 static struct cache_detail *current_detail;
332 static int current_index;
334 static void do_cache_clean(struct work_struct *work);
335 static struct delayed_work cache_cleaner;
337 void sunrpc_init_cache_detail(struct cache_detail *cd)
339 rwlock_init(&cd->hash_lock);
340 INIT_LIST_HEAD(&cd->queue);
341 spin_lock(&cache_list_lock);
344 atomic_set(&cd->readers, 0);
347 list_add(&cd->others, &cache_list);
348 spin_unlock(&cache_list_lock);
350 /* start the cleaning process */
351 schedule_delayed_work(&cache_cleaner, 0);
353 EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
355 void sunrpc_destroy_cache_detail(struct cache_detail *cd)
358 spin_lock(&cache_list_lock);
359 write_lock(&cd->hash_lock);
360 if (cd->entries || atomic_read(&cd->inuse)) {
361 write_unlock(&cd->hash_lock);
362 spin_unlock(&cache_list_lock);
365 if (current_detail == cd)
366 current_detail = NULL;
367 list_del_init(&cd->others);
368 write_unlock(&cd->hash_lock);
369 spin_unlock(&cache_list_lock);
370 if (list_empty(&cache_list)) {
371 /* module must be being unloaded so its safe to kill the worker */
372 cancel_delayed_work_sync(&cache_cleaner);
376 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
378 EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
380 /* clean cache tries to find something to clean
382 * It returns 1 if it cleaned something,
383 * 0 if it didn't find anything this time
384 * -1 if it fell off the end of the list.
386 static int cache_clean(void)
389 struct list_head *next;
391 spin_lock(&cache_list_lock);
393 /* find a suitable table if we don't already have one */
394 while (current_detail == NULL ||
395 current_index >= current_detail->hash_size) {
397 next = current_detail->others.next;
399 next = cache_list.next;
400 if (next == &cache_list) {
401 current_detail = NULL;
402 spin_unlock(&cache_list_lock);
405 current_detail = list_entry(next, struct cache_detail, others);
406 if (current_detail->nextcheck > seconds_since_boot())
407 current_index = current_detail->hash_size;
410 current_detail->nextcheck = seconds_since_boot()+30*60;
414 /* find a non-empty bucket in the table */
415 while (current_detail &&
416 current_index < current_detail->hash_size &&
417 current_detail->hash_table[current_index] == NULL)
420 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
422 if (current_detail && current_index < current_detail->hash_size) {
423 struct cache_head *ch, **cp;
424 struct cache_detail *d;
426 write_lock(¤t_detail->hash_lock);
428 /* Ok, now to clean this strand */
430 cp = & current_detail->hash_table[current_index];
431 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
432 if (current_detail->nextcheck > ch->expiry_time)
433 current_detail->nextcheck = ch->expiry_time+1;
434 if (!cache_is_expired(current_detail, ch))
439 current_detail->entries--;
444 write_unlock(¤t_detail->hash_lock);
448 spin_unlock(&cache_list_lock);
450 set_bit(CACHE_CLEANED, &ch->flags);
451 cache_fresh_unlocked(ch, d);
455 spin_unlock(&cache_list_lock);
461 * We want to regularly clean the cache, so we need to schedule some work ...
463 static void do_cache_clean(struct work_struct *work)
466 if (cache_clean() == -1)
467 delay = round_jiffies_relative(30*HZ);
469 if (list_empty(&cache_list))
473 schedule_delayed_work(&cache_cleaner, delay);
478 * Clean all caches promptly. This just calls cache_clean
479 * repeatedly until we are sure that every cache has had a chance to
482 void cache_flush(void)
484 while (cache_clean() != -1)
486 while (cache_clean() != -1)
489 EXPORT_SYMBOL_GPL(cache_flush);
491 void cache_purge(struct cache_detail *detail)
493 detail->flush_time = LONG_MAX;
494 detail->nextcheck = seconds_since_boot();
496 detail->flush_time = 1;
498 EXPORT_SYMBOL_GPL(cache_purge);
502 * Deferral and Revisiting of Requests.
504 * If a cache lookup finds a pending entry, we
505 * need to defer the request and revisit it later.
506 * All deferred requests are stored in a hash table,
507 * indexed by "struct cache_head *".
508 * As it may be wasteful to store a whole request
509 * structure, we allow the request to provide a
510 * deferred form, which must contain a
511 * 'struct cache_deferred_req'
512 * This cache_deferred_req contains a method to allow
513 * it to be revisited when cache info is available
516 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
517 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
519 #define DFR_MAX 300 /* ??? */
521 static DEFINE_SPINLOCK(cache_defer_lock);
522 static LIST_HEAD(cache_defer_list);
523 static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
524 static int cache_defer_cnt;
526 static void __unhash_deferred_req(struct cache_deferred_req *dreq)
528 hlist_del_init(&dreq->hash);
529 if (!list_empty(&dreq->recent)) {
530 list_del_init(&dreq->recent);
535 static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
537 int hash = DFR_HASH(item);
539 INIT_LIST_HEAD(&dreq->recent);
540 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
543 static void setup_deferral(struct cache_deferred_req *dreq,
544 struct cache_head *item,
550 spin_lock(&cache_defer_lock);
552 __hash_deferred_req(dreq, item);
556 list_add(&dreq->recent, &cache_defer_list);
559 spin_unlock(&cache_defer_lock);
563 struct thread_deferred_req {
564 struct cache_deferred_req handle;
565 struct completion completion;
568 static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
570 struct thread_deferred_req *dr =
571 container_of(dreq, struct thread_deferred_req, handle);
572 complete(&dr->completion);
575 static void cache_wait_req(struct cache_req *req, struct cache_head *item)
577 struct thread_deferred_req sleeper;
578 struct cache_deferred_req *dreq = &sleeper.handle;
580 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
581 dreq->revisit = cache_restart_thread;
583 setup_deferral(dreq, item, 0);
585 if (!test_bit(CACHE_PENDING, &item->flags) ||
586 wait_for_completion_interruptible_timeout(
587 &sleeper.completion, req->thread_wait) <= 0) {
588 /* The completion wasn't completed, so we need
591 spin_lock(&cache_defer_lock);
592 if (!hlist_unhashed(&sleeper.handle.hash)) {
593 __unhash_deferred_req(&sleeper.handle);
594 spin_unlock(&cache_defer_lock);
596 /* cache_revisit_request already removed
597 * this from the hash table, but hasn't
598 * called ->revisit yet. It will very soon
599 * and we need to wait for it.
601 spin_unlock(&cache_defer_lock);
602 wait_for_completion(&sleeper.completion);
607 static void cache_limit_defers(void)
609 /* Make sure we haven't exceed the limit of allowed deferred
612 struct cache_deferred_req *discard = NULL;
614 if (cache_defer_cnt <= DFR_MAX)
617 spin_lock(&cache_defer_lock);
619 /* Consider removing either the first or the last */
620 if (cache_defer_cnt > DFR_MAX) {
621 if (net_random() & 1)
622 discard = list_entry(cache_defer_list.next,
623 struct cache_deferred_req, recent);
625 discard = list_entry(cache_defer_list.prev,
626 struct cache_deferred_req, recent);
627 __unhash_deferred_req(discard);
629 spin_unlock(&cache_defer_lock);
631 discard->revisit(discard, 1);
634 /* Return true if and only if a deferred request is queued. */
635 static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
637 struct cache_deferred_req *dreq;
639 if (req->thread_wait) {
640 cache_wait_req(req, item);
641 if (!test_bit(CACHE_PENDING, &item->flags))
644 dreq = req->defer(req);
647 setup_deferral(dreq, item, 1);
648 if (!test_bit(CACHE_PENDING, &item->flags))
649 /* Bit could have been cleared before we managed to
650 * set up the deferral, so need to revisit just in case
652 cache_revisit_request(item);
654 cache_limit_defers();
658 static void cache_revisit_request(struct cache_head *item)
660 struct cache_deferred_req *dreq;
661 struct list_head pending;
662 struct hlist_node *tmp;
663 int hash = DFR_HASH(item);
665 INIT_LIST_HEAD(&pending);
666 spin_lock(&cache_defer_lock);
668 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
669 if (dreq->item == item) {
670 __unhash_deferred_req(dreq);
671 list_add(&dreq->recent, &pending);
674 spin_unlock(&cache_defer_lock);
676 while (!list_empty(&pending)) {
677 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
678 list_del_init(&dreq->recent);
679 dreq->revisit(dreq, 0);
683 void cache_clean_deferred(void *owner)
685 struct cache_deferred_req *dreq, *tmp;
686 struct list_head pending;
689 INIT_LIST_HEAD(&pending);
690 spin_lock(&cache_defer_lock);
692 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
693 if (dreq->owner == owner) {
694 __unhash_deferred_req(dreq);
695 list_add(&dreq->recent, &pending);
698 spin_unlock(&cache_defer_lock);
700 while (!list_empty(&pending)) {
701 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
702 list_del_init(&dreq->recent);
703 dreq->revisit(dreq, 1);
708 * communicate with user-space
710 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
711 * On read, you get a full request, or block.
712 * On write, an update request is processed.
713 * Poll works if anything to read, and always allows write.
715 * Implemented by linked list of requests. Each open file has
716 * a ->private that also exists in this list. New requests are added
717 * to the end and may wakeup and preceding readers.
718 * New readers are added to the head. If, on read, an item is found with
719 * CACHE_UPCALLING clear, we free it from the list.
723 static DEFINE_SPINLOCK(queue_lock);
724 static DEFINE_MUTEX(queue_io_mutex);
727 struct list_head list;
728 int reader; /* if 0, then request */
730 struct cache_request {
731 struct cache_queue q;
732 struct cache_head *item;
737 struct cache_reader {
738 struct cache_queue q;
739 int offset; /* if non-0, we have a refcnt on next request */
742 static int cache_request(struct cache_detail *detail,
743 struct cache_request *crq)
748 detail->cache_request(detail, crq->item, &bp, &len);
751 return PAGE_SIZE - len;
754 static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
755 loff_t *ppos, struct cache_detail *cd)
757 struct cache_reader *rp = filp->private_data;
758 struct cache_request *rq;
759 struct inode *inode = file_inode(filp);
765 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
766 * readers on this file */
768 spin_lock(&queue_lock);
769 /* need to find next request */
770 while (rp->q.list.next != &cd->queue &&
771 list_entry(rp->q.list.next, struct cache_queue, list)
773 struct list_head *next = rp->q.list.next;
774 list_move(&rp->q.list, next);
776 if (rp->q.list.next == &cd->queue) {
777 spin_unlock(&queue_lock);
778 mutex_unlock(&inode->i_mutex);
779 WARN_ON_ONCE(rp->offset);
782 rq = container_of(rp->q.list.next, struct cache_request, q.list);
783 WARN_ON_ONCE(rq->q.reader);
786 spin_unlock(&queue_lock);
789 err = cache_request(cd, rq);
795 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
797 spin_lock(&queue_lock);
798 list_move(&rp->q.list, &rq->q.list);
799 spin_unlock(&queue_lock);
801 if (rp->offset + count > rq->len)
802 count = rq->len - rp->offset;
804 if (copy_to_user(buf, rq->buf + rp->offset, count))
807 if (rp->offset >= rq->len) {
809 spin_lock(&queue_lock);
810 list_move(&rp->q.list, &rq->q.list);
811 spin_unlock(&queue_lock);
816 if (rp->offset == 0) {
817 /* need to release rq */
818 spin_lock(&queue_lock);
820 if (rq->readers == 0 &&
821 !test_bit(CACHE_PENDING, &rq->item->flags)) {
822 list_del(&rq->q.list);
823 spin_unlock(&queue_lock);
824 cache_put(rq->item, cd);
828 spin_unlock(&queue_lock);
832 mutex_unlock(&inode->i_mutex);
833 return err ? err : count;
836 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
837 size_t count, struct cache_detail *cd)
843 if (copy_from_user(kaddr, buf, count))
846 ret = cd->cache_parse(cd, kaddr, count);
852 static ssize_t cache_slow_downcall(const char __user *buf,
853 size_t count, struct cache_detail *cd)
855 static char write_buf[8192]; /* protected by queue_io_mutex */
856 ssize_t ret = -EINVAL;
858 if (count >= sizeof(write_buf))
860 mutex_lock(&queue_io_mutex);
861 ret = cache_do_downcall(write_buf, buf, count, cd);
862 mutex_unlock(&queue_io_mutex);
867 static ssize_t cache_downcall(struct address_space *mapping,
868 const char __user *buf,
869 size_t count, struct cache_detail *cd)
873 ssize_t ret = -ENOMEM;
875 if (count >= PAGE_CACHE_SIZE)
878 page = find_or_create_page(mapping, 0, GFP_KERNEL);
883 ret = cache_do_downcall(kaddr, buf, count, cd);
886 page_cache_release(page);
889 return cache_slow_downcall(buf, count, cd);
892 static ssize_t cache_write(struct file *filp, const char __user *buf,
893 size_t count, loff_t *ppos,
894 struct cache_detail *cd)
896 struct address_space *mapping = filp->f_mapping;
897 struct inode *inode = file_inode(filp);
898 ssize_t ret = -EINVAL;
900 if (!cd->cache_parse)
903 mutex_lock(&inode->i_mutex);
904 ret = cache_downcall(mapping, buf, count, cd);
905 mutex_unlock(&inode->i_mutex);
910 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
912 static unsigned int cache_poll(struct file *filp, poll_table *wait,
913 struct cache_detail *cd)
916 struct cache_reader *rp = filp->private_data;
917 struct cache_queue *cq;
919 poll_wait(filp, &queue_wait, wait);
921 /* alway allow write */
922 mask = POLL_OUT | POLLWRNORM;
927 spin_lock(&queue_lock);
929 for (cq= &rp->q; &cq->list != &cd->queue;
930 cq = list_entry(cq->list.next, struct cache_queue, list))
932 mask |= POLLIN | POLLRDNORM;
935 spin_unlock(&queue_lock);
939 static int cache_ioctl(struct inode *ino, struct file *filp,
940 unsigned int cmd, unsigned long arg,
941 struct cache_detail *cd)
944 struct cache_reader *rp = filp->private_data;
945 struct cache_queue *cq;
947 if (cmd != FIONREAD || !rp)
950 spin_lock(&queue_lock);
952 /* only find the length remaining in current request,
953 * or the length of the next request
955 for (cq= &rp->q; &cq->list != &cd->queue;
956 cq = list_entry(cq->list.next, struct cache_queue, list))
958 struct cache_request *cr =
959 container_of(cq, struct cache_request, q);
960 len = cr->len - rp->offset;
963 spin_unlock(&queue_lock);
965 return put_user(len, (int __user *)arg);
968 static int cache_open(struct inode *inode, struct file *filp,
969 struct cache_detail *cd)
971 struct cache_reader *rp = NULL;
973 if (!cd || !try_module_get(cd->owner))
975 nonseekable_open(inode, filp);
976 if (filp->f_mode & FMODE_READ) {
977 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
979 module_put(cd->owner);
984 atomic_inc(&cd->readers);
985 spin_lock(&queue_lock);
986 list_add(&rp->q.list, &cd->queue);
987 spin_unlock(&queue_lock);
989 filp->private_data = rp;
993 static int cache_release(struct inode *inode, struct file *filp,
994 struct cache_detail *cd)
996 struct cache_reader *rp = filp->private_data;
999 spin_lock(&queue_lock);
1001 struct cache_queue *cq;
1002 for (cq= &rp->q; &cq->list != &cd->queue;
1003 cq = list_entry(cq->list.next, struct cache_queue, list))
1005 container_of(cq, struct cache_request, q)
1011 list_del(&rp->q.list);
1012 spin_unlock(&queue_lock);
1014 filp->private_data = NULL;
1017 cd->last_close = seconds_since_boot();
1018 atomic_dec(&cd->readers);
1020 module_put(cd->owner);
1026 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
1028 struct cache_queue *cq, *tmp;
1029 struct cache_request *cr;
1030 struct list_head dequeued;
1032 INIT_LIST_HEAD(&dequeued);
1033 spin_lock(&queue_lock);
1034 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
1036 cr = container_of(cq, struct cache_request, q);
1039 if (test_bit(CACHE_PENDING, &ch->flags))
1040 /* Lost a race and it is pending again */
1042 if (cr->readers != 0)
1044 list_move(&cr->q.list, &dequeued);
1046 spin_unlock(&queue_lock);
1047 while (!list_empty(&dequeued)) {
1048 cr = list_entry(dequeued.next, struct cache_request, q.list);
1049 list_del(&cr->q.list);
1050 cache_put(cr->item, detail);
1057 * Support routines for text-based upcalls.
1058 * Fields are separated by spaces.
1059 * Fields are either mangled to quote space tab newline slosh with slosh
1060 * or a hexified with a leading \x
1061 * Record is terminated with newline.
1065 void qword_add(char **bpp, int *lp, char *str)
1071 if (len < 0) return;
1073 while ((c=*str++) && len)
1081 *bp++ = '0' + ((c & 0300)>>6);
1082 *bp++ = '0' + ((c & 0070)>>3);
1083 *bp++ = '0' + ((c & 0007)>>0);
1091 if (c || len <1) len = -1;
1099 EXPORT_SYMBOL_GPL(qword_add);
1101 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1106 if (len < 0) return;
1112 while (blen && len >= 2) {
1113 unsigned char c = *buf++;
1114 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1115 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1120 if (blen || len<1) len = -1;
1128 EXPORT_SYMBOL_GPL(qword_addhex);
1130 static void warn_no_listener(struct cache_detail *detail)
1132 if (detail->last_warn != detail->last_close) {
1133 detail->last_warn = detail->last_close;
1134 if (detail->warn_no_listener)
1135 detail->warn_no_listener(detail, detail->last_close != 0);
1139 static bool cache_listeners_exist(struct cache_detail *detail)
1141 if (atomic_read(&detail->readers))
1143 if (detail->last_close == 0)
1144 /* This cache was never opened */
1146 if (detail->last_close < seconds_since_boot() - 30)
1148 * We allow for the possibility that someone might
1149 * restart a userspace daemon without restarting the
1150 * server; but after 30 seconds, we give up.
1157 * register an upcall request to user-space and queue it up for read() by the
1160 * Each request is at most one page long.
1162 int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
1166 struct cache_request *crq;
1169 if (!detail->cache_request)
1172 if (!cache_listeners_exist(detail)) {
1173 warn_no_listener(detail);
1176 if (test_bit(CACHE_CLEANED, &h->flags))
1177 /* Too late to make an upcall */
1180 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1184 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1191 crq->item = cache_get(h);
1195 spin_lock(&queue_lock);
1196 if (test_bit(CACHE_PENDING, &h->flags))
1197 list_add_tail(&crq->q.list, &detail->queue);
1199 /* Lost a race, no longer PENDING, so don't enqueue */
1201 spin_unlock(&queue_lock);
1202 wake_up(&queue_wait);
1203 if (ret == -EAGAIN) {
1209 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1212 * parse a message from user-space and pass it
1213 * to an appropriate cache
1214 * Messages are, like requests, separated into fields by
1215 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1218 * reply cachename expiry key ... content....
1220 * key and content are both parsed by cache
1223 int qword_get(char **bpp, char *dest, int bufsize)
1225 /* return bytes copied, or -1 on error */
1229 while (*bp == ' ') bp++;
1231 if (bp[0] == '\\' && bp[1] == 'x') {
1234 while (len < bufsize) {
1237 h = hex_to_bin(bp[0]);
1241 l = hex_to_bin(bp[1]);
1245 *dest++ = (h << 4) | l;
1250 /* text with \nnn octal quoting */
1251 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1253 isodigit(bp[1]) && (bp[1] <= '3') &&
1256 int byte = (*++bp -'0');
1258 byte = (byte << 3) | (*bp++ - '0');
1259 byte = (byte << 3) | (*bp++ - '0');
1269 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1271 while (*bp == ' ') bp++;
1276 EXPORT_SYMBOL_GPL(qword_get);
1280 * support /proc/sunrpc/cache/$CACHENAME/content
1282 * We call ->cache_show passing NULL for the item to
1283 * get a header, then pass each real item in the cache
1287 struct cache_detail *cd;
1290 static void *c_start(struct seq_file *m, loff_t *pos)
1291 __acquires(cd->hash_lock)
1294 unsigned int hash, entry;
1295 struct cache_head *ch;
1296 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1299 read_lock(&cd->hash_lock);
1301 return SEQ_START_TOKEN;
1303 entry = n & ((1LL<<32) - 1);
1305 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1308 n &= ~((1LL<<32) - 1);
1312 } while(hash < cd->hash_size &&
1313 cd->hash_table[hash]==NULL);
1314 if (hash >= cd->hash_size)
1317 return cd->hash_table[hash];
1320 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1322 struct cache_head *ch = p;
1323 int hash = (*pos >> 32);
1324 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1326 if (p == SEQ_START_TOKEN)
1328 else if (ch->next == NULL) {
1335 *pos &= ~((1LL<<32) - 1);
1336 while (hash < cd->hash_size &&
1337 cd->hash_table[hash] == NULL) {
1341 if (hash >= cd->hash_size)
1344 return cd->hash_table[hash];
1347 static void c_stop(struct seq_file *m, void *p)
1348 __releases(cd->hash_lock)
1350 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1351 read_unlock(&cd->hash_lock);
1354 static int c_show(struct seq_file *m, void *p)
1356 struct cache_head *cp = p;
1357 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1359 if (p == SEQ_START_TOKEN)
1360 return cd->cache_show(m, cd, NULL);
1363 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1364 convert_to_wallclock(cp->expiry_time),
1365 atomic_read(&cp->ref.refcount), cp->flags);
1367 if (cache_check(cd, cp, NULL))
1368 /* cache_check does a cache_put on failure */
1369 seq_printf(m, "# ");
1371 if (cache_is_expired(cd, cp))
1372 seq_printf(m, "# ");
1376 return cd->cache_show(m, cd, cp);
1379 static const struct seq_operations cache_content_op = {
1386 static int content_open(struct inode *inode, struct file *file,
1387 struct cache_detail *cd)
1391 if (!cd || !try_module_get(cd->owner))
1393 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1395 module_put(cd->owner);
1403 static int content_release(struct inode *inode, struct file *file,
1404 struct cache_detail *cd)
1406 int ret = seq_release_private(inode, file);
1407 module_put(cd->owner);
1411 static int open_flush(struct inode *inode, struct file *file,
1412 struct cache_detail *cd)
1414 if (!cd || !try_module_get(cd->owner))
1416 return nonseekable_open(inode, file);
1419 static int release_flush(struct inode *inode, struct file *file,
1420 struct cache_detail *cd)
1422 module_put(cd->owner);
1426 static ssize_t read_flush(struct file *file, char __user *buf,
1427 size_t count, loff_t *ppos,
1428 struct cache_detail *cd)
1431 unsigned long p = *ppos;
1434 snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
1441 if (copy_to_user(buf, (void*)(tbuf+p), len))
1447 static ssize_t write_flush(struct file *file, const char __user *buf,
1448 size_t count, loff_t *ppos,
1449 struct cache_detail *cd)
1454 if (*ppos || count > sizeof(tbuf)-1)
1456 if (copy_from_user(tbuf, buf, count))
1459 simple_strtoul(tbuf, &ep, 0);
1460 if (*ep && *ep != '\n')
1464 cd->flush_time = get_expiry(&bp);
1465 cd->nextcheck = seconds_since_boot();
1472 static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1473 size_t count, loff_t *ppos)
1475 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1477 return cache_read(filp, buf, count, ppos, cd);
1480 static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1481 size_t count, loff_t *ppos)
1483 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1485 return cache_write(filp, buf, count, ppos, cd);
1488 static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1490 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1492 return cache_poll(filp, wait, cd);
1495 static long cache_ioctl_procfs(struct file *filp,
1496 unsigned int cmd, unsigned long arg)
1498 struct inode *inode = file_inode(filp);
1499 struct cache_detail *cd = PDE_DATA(inode);
1501 return cache_ioctl(inode, filp, cmd, arg, cd);
1504 static int cache_open_procfs(struct inode *inode, struct file *filp)
1506 struct cache_detail *cd = PDE_DATA(inode);
1508 return cache_open(inode, filp, cd);
1511 static int cache_release_procfs(struct inode *inode, struct file *filp)
1513 struct cache_detail *cd = PDE_DATA(inode);
1515 return cache_release(inode, filp, cd);
1518 static const struct file_operations cache_file_operations_procfs = {
1519 .owner = THIS_MODULE,
1520 .llseek = no_llseek,
1521 .read = cache_read_procfs,
1522 .write = cache_write_procfs,
1523 .poll = cache_poll_procfs,
1524 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
1525 .open = cache_open_procfs,
1526 .release = cache_release_procfs,
1529 static int content_open_procfs(struct inode *inode, struct file *filp)
1531 struct cache_detail *cd = PDE_DATA(inode);
1533 return content_open(inode, filp, cd);
1536 static int content_release_procfs(struct inode *inode, struct file *filp)
1538 struct cache_detail *cd = PDE_DATA(inode);
1540 return content_release(inode, filp, cd);
1543 static const struct file_operations content_file_operations_procfs = {
1544 .open = content_open_procfs,
1546 .llseek = seq_lseek,
1547 .release = content_release_procfs,
1550 static int open_flush_procfs(struct inode *inode, struct file *filp)
1552 struct cache_detail *cd = PDE_DATA(inode);
1554 return open_flush(inode, filp, cd);
1557 static int release_flush_procfs(struct inode *inode, struct file *filp)
1559 struct cache_detail *cd = PDE_DATA(inode);
1561 return release_flush(inode, filp, cd);
1564 static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1565 size_t count, loff_t *ppos)
1567 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1569 return read_flush(filp, buf, count, ppos, cd);
1572 static ssize_t write_flush_procfs(struct file *filp,
1573 const char __user *buf,
1574 size_t count, loff_t *ppos)
1576 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1578 return write_flush(filp, buf, count, ppos, cd);
1581 static const struct file_operations cache_flush_operations_procfs = {
1582 .open = open_flush_procfs,
1583 .read = read_flush_procfs,
1584 .write = write_flush_procfs,
1585 .release = release_flush_procfs,
1586 .llseek = no_llseek,
1589 static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
1591 struct sunrpc_net *sn;
1593 if (cd->u.procfs.proc_ent == NULL)
1595 if (cd->u.procfs.flush_ent)
1596 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1597 if (cd->u.procfs.channel_ent)
1598 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1599 if (cd->u.procfs.content_ent)
1600 remove_proc_entry("content", cd->u.procfs.proc_ent);
1601 cd->u.procfs.proc_ent = NULL;
1602 sn = net_generic(net, sunrpc_net_id);
1603 remove_proc_entry(cd->name, sn->proc_net_rpc);
1606 #ifdef CONFIG_PROC_FS
1607 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1609 struct proc_dir_entry *p;
1610 struct sunrpc_net *sn;
1612 sn = net_generic(net, sunrpc_net_id);
1613 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
1614 if (cd->u.procfs.proc_ent == NULL)
1616 cd->u.procfs.channel_ent = NULL;
1617 cd->u.procfs.content_ent = NULL;
1619 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1620 cd->u.procfs.proc_ent,
1621 &cache_flush_operations_procfs, cd);
1622 cd->u.procfs.flush_ent = p;
1626 if (cd->cache_request || cd->cache_parse) {
1627 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1628 cd->u.procfs.proc_ent,
1629 &cache_file_operations_procfs, cd);
1630 cd->u.procfs.channel_ent = p;
1634 if (cd->cache_show) {
1635 p = proc_create_data("content", S_IFREG|S_IRUSR,
1636 cd->u.procfs.proc_ent,
1637 &content_file_operations_procfs, cd);
1638 cd->u.procfs.content_ent = p;
1644 remove_cache_proc_entries(cd, net);
1647 #else /* CONFIG_PROC_FS */
1648 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1654 void __init cache_initialize(void)
1656 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
1659 int cache_register_net(struct cache_detail *cd, struct net *net)
1663 sunrpc_init_cache_detail(cd);
1664 ret = create_cache_proc_entries(cd, net);
1666 sunrpc_destroy_cache_detail(cd);
1669 EXPORT_SYMBOL_GPL(cache_register_net);
1671 void cache_unregister_net(struct cache_detail *cd, struct net *net)
1673 remove_cache_proc_entries(cd, net);
1674 sunrpc_destroy_cache_detail(cd);
1676 EXPORT_SYMBOL_GPL(cache_unregister_net);
1678 struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
1680 struct cache_detail *cd;
1682 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1684 return ERR_PTR(-ENOMEM);
1686 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct cache_head *),
1688 if (cd->hash_table == NULL) {
1690 return ERR_PTR(-ENOMEM);
1695 EXPORT_SYMBOL_GPL(cache_create_net);
1697 void cache_destroy_net(struct cache_detail *cd, struct net *net)
1699 kfree(cd->hash_table);
1702 EXPORT_SYMBOL_GPL(cache_destroy_net);
1704 static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1705 size_t count, loff_t *ppos)
1707 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1709 return cache_read(filp, buf, count, ppos, cd);
1712 static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1713 size_t count, loff_t *ppos)
1715 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1717 return cache_write(filp, buf, count, ppos, cd);
1720 static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1722 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1724 return cache_poll(filp, wait, cd);
1727 static long cache_ioctl_pipefs(struct file *filp,
1728 unsigned int cmd, unsigned long arg)
1730 struct inode *inode = file_inode(filp);
1731 struct cache_detail *cd = RPC_I(inode)->private;
1733 return cache_ioctl(inode, filp, cmd, arg, cd);
1736 static int cache_open_pipefs(struct inode *inode, struct file *filp)
1738 struct cache_detail *cd = RPC_I(inode)->private;
1740 return cache_open(inode, filp, cd);
1743 static int cache_release_pipefs(struct inode *inode, struct file *filp)
1745 struct cache_detail *cd = RPC_I(inode)->private;
1747 return cache_release(inode, filp, cd);
1750 const struct file_operations cache_file_operations_pipefs = {
1751 .owner = THIS_MODULE,
1752 .llseek = no_llseek,
1753 .read = cache_read_pipefs,
1754 .write = cache_write_pipefs,
1755 .poll = cache_poll_pipefs,
1756 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
1757 .open = cache_open_pipefs,
1758 .release = cache_release_pipefs,
1761 static int content_open_pipefs(struct inode *inode, struct file *filp)
1763 struct cache_detail *cd = RPC_I(inode)->private;
1765 return content_open(inode, filp, cd);
1768 static int content_release_pipefs(struct inode *inode, struct file *filp)
1770 struct cache_detail *cd = RPC_I(inode)->private;
1772 return content_release(inode, filp, cd);
1775 const struct file_operations content_file_operations_pipefs = {
1776 .open = content_open_pipefs,
1778 .llseek = seq_lseek,
1779 .release = content_release_pipefs,
1782 static int open_flush_pipefs(struct inode *inode, struct file *filp)
1784 struct cache_detail *cd = RPC_I(inode)->private;
1786 return open_flush(inode, filp, cd);
1789 static int release_flush_pipefs(struct inode *inode, struct file *filp)
1791 struct cache_detail *cd = RPC_I(inode)->private;
1793 return release_flush(inode, filp, cd);
1796 static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1797 size_t count, loff_t *ppos)
1799 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1801 return read_flush(filp, buf, count, ppos, cd);
1804 static ssize_t write_flush_pipefs(struct file *filp,
1805 const char __user *buf,
1806 size_t count, loff_t *ppos)
1808 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1810 return write_flush(filp, buf, count, ppos, cd);
1813 const struct file_operations cache_flush_operations_pipefs = {
1814 .open = open_flush_pipefs,
1815 .read = read_flush_pipefs,
1816 .write = write_flush_pipefs,
1817 .release = release_flush_pipefs,
1818 .llseek = no_llseek,
1821 int sunrpc_cache_register_pipefs(struct dentry *parent,
1822 const char *name, umode_t umode,
1823 struct cache_detail *cd)
1830 q.len = strlen(name);
1831 q.hash = full_name_hash(q.name, q.len);
1832 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1834 cd->u.pipefs.dir = dir;
1839 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1841 void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1843 rpc_remove_cache_dir(cd->u.pipefs.dir);
1844 cd->u.pipefs.dir = NULL;
1846 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);