4 * Generic code for various authentication-related caches
5 * used by sunrpc clients and servers.
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 <linux/string_helpers.h>
24 #include <linux/uaccess.h>
25 #include <linux/poll.h>
26 #include <linux/seq_file.h>
27 #include <linux/proc_fs.h>
28 #include <linux/net.h>
29 #include <linux/workqueue.h>
30 #include <linux/mutex.h>
31 #include <linux/pagemap.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 bool 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, struct cache_detail *detail)
46 time_t now = seconds_since_boot();
47 INIT_HLIST_NODE(&h->cache_list);
50 h->expiry_time = now + CACHE_NEW_EXPIRY;
51 if (now <= detail->flush_time)
52 /* ensure it isn't already expired */
53 now = detail->flush_time + 1;
54 h->last_refresh = now;
57 static void cache_fresh_locked(struct cache_head *head, time_t expiry,
58 struct cache_detail *detail);
59 static void cache_fresh_unlocked(struct cache_head *head,
60 struct cache_detail *detail);
62 static struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail,
63 struct cache_head *key,
66 struct hlist_head *head = &detail->hash_table[hash];
67 struct cache_head *tmp;
70 hlist_for_each_entry_rcu(tmp, head, cache_list) {
71 if (detail->match(tmp, key)) {
72 if (cache_is_expired(detail, tmp))
74 tmp = cache_get_rcu(tmp);
83 static struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail,
84 struct cache_head *key,
87 struct cache_head *new, *tmp, *freeme = NULL;
88 struct hlist_head *head = &detail->hash_table[hash];
90 new = detail->alloc();
93 /* must fully initialise 'new', else
94 * we might get lose if we need to
97 cache_init(new, detail);
98 detail->init(new, key);
100 spin_lock(&detail->hash_lock);
102 /* check if entry appeared while we slept */
103 hlist_for_each_entry_rcu(tmp, head, cache_list) {
104 if (detail->match(tmp, key)) {
105 if (cache_is_expired(detail, tmp)) {
106 hlist_del_init_rcu(&tmp->cache_list);
108 cache_fresh_locked(tmp, 0, detail);
113 spin_unlock(&detail->hash_lock);
114 cache_put(new, detail);
119 hlist_add_head_rcu(&new->cache_list, head);
122 spin_unlock(&detail->hash_lock);
125 cache_fresh_unlocked(freeme, detail);
126 cache_put(freeme, detail);
131 struct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail,
132 struct cache_head *key, int hash)
134 struct cache_head *ret;
136 ret = sunrpc_cache_find_rcu(detail, key, hash);
139 /* Didn't find anything, insert an empty entry */
140 return sunrpc_cache_add_entry(detail, key, hash);
142 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu);
144 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
146 static void cache_fresh_locked(struct cache_head *head, time_t expiry,
147 struct cache_detail *detail)
149 time_t now = seconds_since_boot();
150 if (now <= detail->flush_time)
151 /* ensure it isn't immediately treated as expired */
152 now = detail->flush_time + 1;
153 head->expiry_time = expiry;
154 head->last_refresh = now;
155 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
156 set_bit(CACHE_VALID, &head->flags);
159 static void cache_fresh_unlocked(struct cache_head *head,
160 struct cache_detail *detail)
162 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
163 cache_revisit_request(head);
164 cache_dequeue(detail, head);
168 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
169 struct cache_head *new, struct cache_head *old, int hash)
171 /* The 'old' entry is to be replaced by 'new'.
172 * If 'old' is not VALID, we update it directly,
173 * otherwise we need to replace it
175 struct cache_head *tmp;
177 if (!test_bit(CACHE_VALID, &old->flags)) {
178 spin_lock(&detail->hash_lock);
179 if (!test_bit(CACHE_VALID, &old->flags)) {
180 if (test_bit(CACHE_NEGATIVE, &new->flags))
181 set_bit(CACHE_NEGATIVE, &old->flags);
183 detail->update(old, new);
184 cache_fresh_locked(old, new->expiry_time, detail);
185 spin_unlock(&detail->hash_lock);
186 cache_fresh_unlocked(old, detail);
189 spin_unlock(&detail->hash_lock);
191 /* We need to insert a new entry */
192 tmp = detail->alloc();
194 cache_put(old, detail);
197 cache_init(tmp, detail);
198 detail->init(tmp, old);
200 spin_lock(&detail->hash_lock);
201 if (test_bit(CACHE_NEGATIVE, &new->flags))
202 set_bit(CACHE_NEGATIVE, &tmp->flags);
204 detail->update(tmp, new);
205 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
208 cache_fresh_locked(tmp, new->expiry_time, detail);
209 cache_fresh_locked(old, 0, detail);
210 spin_unlock(&detail->hash_lock);
211 cache_fresh_unlocked(tmp, detail);
212 cache_fresh_unlocked(old, detail);
213 cache_put(old, detail);
216 EXPORT_SYMBOL_GPL(sunrpc_cache_update);
218 static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
220 if (cd->cache_upcall)
221 return cd->cache_upcall(cd, h);
222 return sunrpc_cache_pipe_upcall(cd, h);
225 static inline int cache_is_valid(struct cache_head *h)
227 if (!test_bit(CACHE_VALID, &h->flags))
231 if (test_bit(CACHE_NEGATIVE, &h->flags))
235 * In combination with write barrier in
236 * sunrpc_cache_update, ensures that anyone
237 * using the cache entry after this sees the
246 static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
250 spin_lock(&detail->hash_lock);
251 rv = cache_is_valid(h);
253 set_bit(CACHE_NEGATIVE, &h->flags);
254 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
258 spin_unlock(&detail->hash_lock);
259 cache_fresh_unlocked(h, detail);
264 * This is the generic cache management routine for all
265 * the authentication caches.
266 * It checks the currency of a cache item and will (later)
267 * initiate an upcall to fill it if needed.
270 * Returns 0 if the cache_head can be used, or cache_puts it and returns
271 * -EAGAIN if upcall is pending and request has been queued
272 * -ETIMEDOUT if upcall failed or request could not be queue or
273 * upcall completed but item is still invalid (implying that
274 * the cache item has been replaced with a newer one).
275 * -ENOENT if cache entry was negative
277 int cache_check(struct cache_detail *detail,
278 struct cache_head *h, struct cache_req *rqstp)
281 long refresh_age, age;
283 /* First decide return status as best we can */
284 rv = cache_is_valid(h);
286 /* now see if we want to start an upcall */
287 refresh_age = (h->expiry_time - h->last_refresh);
288 age = seconds_since_boot() - h->last_refresh;
293 } else if (rv == -EAGAIN ||
294 (h->expiry_time != 0 && age > refresh_age/2)) {
295 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
297 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
298 switch (cache_make_upcall(detail, h)) {
300 rv = try_to_negate_entry(detail, h);
303 cache_fresh_unlocked(h, detail);
310 if (!cache_defer_req(rqstp, h)) {
312 * Request was not deferred; handle it as best
315 rv = cache_is_valid(h);
321 cache_put(h, detail);
324 EXPORT_SYMBOL_GPL(cache_check);
327 * caches need to be periodically cleaned.
328 * For this we maintain a list of cache_detail and
329 * a current pointer into that list and into the table
332 * Each time cache_clean is called it finds the next non-empty entry
333 * in the current table and walks the list in that entry
334 * looking for entries that can be removed.
336 * An entry gets removed if:
337 * - The expiry is before current time
338 * - The last_refresh time is before the flush_time for that cache
340 * later we might drop old entries with non-NEVER expiry if that table
341 * is getting 'full' for some definition of 'full'
343 * The question of "how often to scan a table" is an interesting one
344 * and is answered in part by the use of the "nextcheck" field in the
346 * When a scan of a table begins, the nextcheck field is set to a time
347 * that is well into the future.
348 * While scanning, if an expiry time is found that is earlier than the
349 * current nextcheck time, nextcheck is set to that expiry time.
350 * If the flush_time is ever set to a time earlier than the nextcheck
351 * time, the nextcheck time is then set to that flush_time.
353 * A table is then only scanned if the current time is at least
354 * the nextcheck time.
358 static LIST_HEAD(cache_list);
359 static DEFINE_SPINLOCK(cache_list_lock);
360 static struct cache_detail *current_detail;
361 static int current_index;
363 static void do_cache_clean(struct work_struct *work);
364 static struct delayed_work cache_cleaner;
366 void sunrpc_init_cache_detail(struct cache_detail *cd)
368 spin_lock_init(&cd->hash_lock);
369 INIT_LIST_HEAD(&cd->queue);
370 spin_lock(&cache_list_lock);
373 atomic_set(&cd->readers, 0);
376 list_add(&cd->others, &cache_list);
377 spin_unlock(&cache_list_lock);
379 /* start the cleaning process */
380 queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
382 EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
384 void sunrpc_destroy_cache_detail(struct cache_detail *cd)
387 spin_lock(&cache_list_lock);
388 spin_lock(&cd->hash_lock);
389 if (current_detail == cd)
390 current_detail = NULL;
391 list_del_init(&cd->others);
392 spin_unlock(&cd->hash_lock);
393 spin_unlock(&cache_list_lock);
394 if (list_empty(&cache_list)) {
395 /* module must be being unloaded so its safe to kill the worker */
396 cancel_delayed_work_sync(&cache_cleaner);
399 EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
401 /* clean cache tries to find something to clean
403 * It returns 1 if it cleaned something,
404 * 0 if it didn't find anything this time
405 * -1 if it fell off the end of the list.
407 static int cache_clean(void)
410 struct list_head *next;
412 spin_lock(&cache_list_lock);
414 /* find a suitable table if we don't already have one */
415 while (current_detail == NULL ||
416 current_index >= current_detail->hash_size) {
418 next = current_detail->others.next;
420 next = cache_list.next;
421 if (next == &cache_list) {
422 current_detail = NULL;
423 spin_unlock(&cache_list_lock);
426 current_detail = list_entry(next, struct cache_detail, others);
427 if (current_detail->nextcheck > seconds_since_boot())
428 current_index = current_detail->hash_size;
431 current_detail->nextcheck = seconds_since_boot()+30*60;
435 /* find a non-empty bucket in the table */
436 while (current_detail &&
437 current_index < current_detail->hash_size &&
438 hlist_empty(¤t_detail->hash_table[current_index]))
441 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
443 if (current_detail && current_index < current_detail->hash_size) {
444 struct cache_head *ch = NULL;
445 struct cache_detail *d;
446 struct hlist_head *head;
447 struct hlist_node *tmp;
449 spin_lock(¤t_detail->hash_lock);
451 /* Ok, now to clean this strand */
453 head = ¤t_detail->hash_table[current_index];
454 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
455 if (current_detail->nextcheck > ch->expiry_time)
456 current_detail->nextcheck = ch->expiry_time+1;
457 if (!cache_is_expired(current_detail, ch))
460 hlist_del_init_rcu(&ch->cache_list);
461 current_detail->entries--;
466 spin_unlock(¤t_detail->hash_lock);
470 spin_unlock(&cache_list_lock);
472 set_bit(CACHE_CLEANED, &ch->flags);
473 cache_fresh_unlocked(ch, d);
477 spin_unlock(&cache_list_lock);
483 * We want to regularly clean the cache, so we need to schedule some work ...
485 static void do_cache_clean(struct work_struct *work)
488 if (cache_clean() == -1)
489 delay = round_jiffies_relative(30*HZ);
491 if (list_empty(&cache_list))
495 queue_delayed_work(system_power_efficient_wq,
496 &cache_cleaner, delay);
501 * Clean all caches promptly. This just calls cache_clean
502 * repeatedly until we are sure that every cache has had a chance to
505 void cache_flush(void)
507 while (cache_clean() != -1)
509 while (cache_clean() != -1)
512 EXPORT_SYMBOL_GPL(cache_flush);
514 void cache_purge(struct cache_detail *detail)
516 struct cache_head *ch = NULL;
517 struct hlist_head *head = NULL;
518 struct hlist_node *tmp = NULL;
521 spin_lock(&detail->hash_lock);
522 if (!detail->entries) {
523 spin_unlock(&detail->hash_lock);
527 dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name);
528 for (i = 0; i < detail->hash_size; i++) {
529 head = &detail->hash_table[i];
530 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
531 hlist_del_init_rcu(&ch->cache_list);
534 set_bit(CACHE_CLEANED, &ch->flags);
535 spin_unlock(&detail->hash_lock);
536 cache_fresh_unlocked(ch, detail);
537 cache_put(ch, detail);
538 spin_lock(&detail->hash_lock);
541 spin_unlock(&detail->hash_lock);
543 EXPORT_SYMBOL_GPL(cache_purge);
547 * Deferral and Revisiting of Requests.
549 * If a cache lookup finds a pending entry, we
550 * need to defer the request and revisit it later.
551 * All deferred requests are stored in a hash table,
552 * indexed by "struct cache_head *".
553 * As it may be wasteful to store a whole request
554 * structure, we allow the request to provide a
555 * deferred form, which must contain a
556 * 'struct cache_deferred_req'
557 * This cache_deferred_req contains a method to allow
558 * it to be revisited when cache info is available
561 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
562 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
564 #define DFR_MAX 300 /* ??? */
566 static DEFINE_SPINLOCK(cache_defer_lock);
567 static LIST_HEAD(cache_defer_list);
568 static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
569 static int cache_defer_cnt;
571 static void __unhash_deferred_req(struct cache_deferred_req *dreq)
573 hlist_del_init(&dreq->hash);
574 if (!list_empty(&dreq->recent)) {
575 list_del_init(&dreq->recent);
580 static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
582 int hash = DFR_HASH(item);
584 INIT_LIST_HEAD(&dreq->recent);
585 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
588 static void setup_deferral(struct cache_deferred_req *dreq,
589 struct cache_head *item,
595 spin_lock(&cache_defer_lock);
597 __hash_deferred_req(dreq, item);
601 list_add(&dreq->recent, &cache_defer_list);
604 spin_unlock(&cache_defer_lock);
608 struct thread_deferred_req {
609 struct cache_deferred_req handle;
610 struct completion completion;
613 static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
615 struct thread_deferred_req *dr =
616 container_of(dreq, struct thread_deferred_req, handle);
617 complete(&dr->completion);
620 static void cache_wait_req(struct cache_req *req, struct cache_head *item)
622 struct thread_deferred_req sleeper;
623 struct cache_deferred_req *dreq = &sleeper.handle;
625 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
626 dreq->revisit = cache_restart_thread;
628 setup_deferral(dreq, item, 0);
630 if (!test_bit(CACHE_PENDING, &item->flags) ||
631 wait_for_completion_interruptible_timeout(
632 &sleeper.completion, req->thread_wait) <= 0) {
633 /* The completion wasn't completed, so we need
636 spin_lock(&cache_defer_lock);
637 if (!hlist_unhashed(&sleeper.handle.hash)) {
638 __unhash_deferred_req(&sleeper.handle);
639 spin_unlock(&cache_defer_lock);
641 /* cache_revisit_request already removed
642 * this from the hash table, but hasn't
643 * called ->revisit yet. It will very soon
644 * and we need to wait for it.
646 spin_unlock(&cache_defer_lock);
647 wait_for_completion(&sleeper.completion);
652 static void cache_limit_defers(void)
654 /* Make sure we haven't exceed the limit of allowed deferred
657 struct cache_deferred_req *discard = NULL;
659 if (cache_defer_cnt <= DFR_MAX)
662 spin_lock(&cache_defer_lock);
664 /* Consider removing either the first or the last */
665 if (cache_defer_cnt > DFR_MAX) {
666 if (prandom_u32() & 1)
667 discard = list_entry(cache_defer_list.next,
668 struct cache_deferred_req, recent);
670 discard = list_entry(cache_defer_list.prev,
671 struct cache_deferred_req, recent);
672 __unhash_deferred_req(discard);
674 spin_unlock(&cache_defer_lock);
676 discard->revisit(discard, 1);
679 /* Return true if and only if a deferred request is queued. */
680 static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
682 struct cache_deferred_req *dreq;
684 if (req->thread_wait) {
685 cache_wait_req(req, item);
686 if (!test_bit(CACHE_PENDING, &item->flags))
689 dreq = req->defer(req);
692 setup_deferral(dreq, item, 1);
693 if (!test_bit(CACHE_PENDING, &item->flags))
694 /* Bit could have been cleared before we managed to
695 * set up the deferral, so need to revisit just in case
697 cache_revisit_request(item);
699 cache_limit_defers();
703 static void cache_revisit_request(struct cache_head *item)
705 struct cache_deferred_req *dreq;
706 struct list_head pending;
707 struct hlist_node *tmp;
708 int hash = DFR_HASH(item);
710 INIT_LIST_HEAD(&pending);
711 spin_lock(&cache_defer_lock);
713 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
714 if (dreq->item == item) {
715 __unhash_deferred_req(dreq);
716 list_add(&dreq->recent, &pending);
719 spin_unlock(&cache_defer_lock);
721 while (!list_empty(&pending)) {
722 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
723 list_del_init(&dreq->recent);
724 dreq->revisit(dreq, 0);
728 void cache_clean_deferred(void *owner)
730 struct cache_deferred_req *dreq, *tmp;
731 struct list_head pending;
734 INIT_LIST_HEAD(&pending);
735 spin_lock(&cache_defer_lock);
737 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
738 if (dreq->owner == owner) {
739 __unhash_deferred_req(dreq);
740 list_add(&dreq->recent, &pending);
743 spin_unlock(&cache_defer_lock);
745 while (!list_empty(&pending)) {
746 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
747 list_del_init(&dreq->recent);
748 dreq->revisit(dreq, 1);
753 * communicate with user-space
755 * We have a magic /proc file - /proc/net/rpc/<cachename>/channel.
756 * On read, you get a full request, or block.
757 * On write, an update request is processed.
758 * Poll works if anything to read, and always allows write.
760 * Implemented by linked list of requests. Each open file has
761 * a ->private that also exists in this list. New requests are added
762 * to the end and may wakeup and preceding readers.
763 * New readers are added to the head. If, on read, an item is found with
764 * CACHE_UPCALLING clear, we free it from the list.
768 static DEFINE_SPINLOCK(queue_lock);
769 static DEFINE_MUTEX(queue_io_mutex);
772 struct list_head list;
773 int reader; /* if 0, then request */
775 struct cache_request {
776 struct cache_queue q;
777 struct cache_head *item;
782 struct cache_reader {
783 struct cache_queue q;
784 int offset; /* if non-0, we have a refcnt on next request */
787 static int cache_request(struct cache_detail *detail,
788 struct cache_request *crq)
793 detail->cache_request(detail, crq->item, &bp, &len);
796 return PAGE_SIZE - len;
799 static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
800 loff_t *ppos, struct cache_detail *cd)
802 struct cache_reader *rp = filp->private_data;
803 struct cache_request *rq;
804 struct inode *inode = file_inode(filp);
810 inode_lock(inode); /* protect against multiple concurrent
811 * readers on this file */
813 spin_lock(&queue_lock);
814 /* need to find next request */
815 while (rp->q.list.next != &cd->queue &&
816 list_entry(rp->q.list.next, struct cache_queue, list)
818 struct list_head *next = rp->q.list.next;
819 list_move(&rp->q.list, next);
821 if (rp->q.list.next == &cd->queue) {
822 spin_unlock(&queue_lock);
824 WARN_ON_ONCE(rp->offset);
827 rq = container_of(rp->q.list.next, struct cache_request, q.list);
828 WARN_ON_ONCE(rq->q.reader);
831 spin_unlock(&queue_lock);
834 err = cache_request(cd, rq);
840 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
842 spin_lock(&queue_lock);
843 list_move(&rp->q.list, &rq->q.list);
844 spin_unlock(&queue_lock);
846 if (rp->offset + count > rq->len)
847 count = rq->len - rp->offset;
849 if (copy_to_user(buf, rq->buf + rp->offset, count))
852 if (rp->offset >= rq->len) {
854 spin_lock(&queue_lock);
855 list_move(&rp->q.list, &rq->q.list);
856 spin_unlock(&queue_lock);
861 if (rp->offset == 0) {
862 /* need to release rq */
863 spin_lock(&queue_lock);
865 if (rq->readers == 0 &&
866 !test_bit(CACHE_PENDING, &rq->item->flags)) {
867 list_del(&rq->q.list);
868 spin_unlock(&queue_lock);
869 cache_put(rq->item, cd);
873 spin_unlock(&queue_lock);
878 return err ? err : count;
881 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
882 size_t count, struct cache_detail *cd)
888 if (copy_from_user(kaddr, buf, count))
891 ret = cd->cache_parse(cd, kaddr, count);
897 static ssize_t cache_slow_downcall(const char __user *buf,
898 size_t count, struct cache_detail *cd)
900 static char write_buf[8192]; /* protected by queue_io_mutex */
901 ssize_t ret = -EINVAL;
903 if (count >= sizeof(write_buf))
905 mutex_lock(&queue_io_mutex);
906 ret = cache_do_downcall(write_buf, buf, count, cd);
907 mutex_unlock(&queue_io_mutex);
912 static ssize_t cache_downcall(struct address_space *mapping,
913 const char __user *buf,
914 size_t count, struct cache_detail *cd)
918 ssize_t ret = -ENOMEM;
920 if (count >= PAGE_SIZE)
923 page = find_or_create_page(mapping, 0, GFP_KERNEL);
928 ret = cache_do_downcall(kaddr, buf, count, cd);
934 return cache_slow_downcall(buf, count, cd);
937 static ssize_t cache_write(struct file *filp, const char __user *buf,
938 size_t count, loff_t *ppos,
939 struct cache_detail *cd)
941 struct address_space *mapping = filp->f_mapping;
942 struct inode *inode = file_inode(filp);
943 ssize_t ret = -EINVAL;
945 if (!cd->cache_parse)
949 ret = cache_downcall(mapping, buf, count, cd);
955 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
957 static __poll_t cache_poll(struct file *filp, poll_table *wait,
958 struct cache_detail *cd)
961 struct cache_reader *rp = filp->private_data;
962 struct cache_queue *cq;
964 poll_wait(filp, &queue_wait, wait);
966 /* alway allow write */
967 mask = EPOLLOUT | EPOLLWRNORM;
972 spin_lock(&queue_lock);
974 for (cq= &rp->q; &cq->list != &cd->queue;
975 cq = list_entry(cq->list.next, struct cache_queue, list))
977 mask |= EPOLLIN | EPOLLRDNORM;
980 spin_unlock(&queue_lock);
984 static int cache_ioctl(struct inode *ino, struct file *filp,
985 unsigned int cmd, unsigned long arg,
986 struct cache_detail *cd)
989 struct cache_reader *rp = filp->private_data;
990 struct cache_queue *cq;
992 if (cmd != FIONREAD || !rp)
995 spin_lock(&queue_lock);
997 /* only find the length remaining in current request,
998 * or the length of the next request
1000 for (cq= &rp->q; &cq->list != &cd->queue;
1001 cq = list_entry(cq->list.next, struct cache_queue, list))
1003 struct cache_request *cr =
1004 container_of(cq, struct cache_request, q);
1005 len = cr->len - rp->offset;
1008 spin_unlock(&queue_lock);
1010 return put_user(len, (int __user *)arg);
1013 static int cache_open(struct inode *inode, struct file *filp,
1014 struct cache_detail *cd)
1016 struct cache_reader *rp = NULL;
1018 if (!cd || !try_module_get(cd->owner))
1020 nonseekable_open(inode, filp);
1021 if (filp->f_mode & FMODE_READ) {
1022 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
1024 module_put(cd->owner);
1029 atomic_inc(&cd->readers);
1030 spin_lock(&queue_lock);
1031 list_add(&rp->q.list, &cd->queue);
1032 spin_unlock(&queue_lock);
1034 filp->private_data = rp;
1038 static int cache_release(struct inode *inode, struct file *filp,
1039 struct cache_detail *cd)
1041 struct cache_reader *rp = filp->private_data;
1044 spin_lock(&queue_lock);
1046 struct cache_queue *cq;
1047 for (cq= &rp->q; &cq->list != &cd->queue;
1048 cq = list_entry(cq->list.next, struct cache_queue, list))
1050 container_of(cq, struct cache_request, q)
1056 list_del(&rp->q.list);
1057 spin_unlock(&queue_lock);
1059 filp->private_data = NULL;
1062 cd->last_close = seconds_since_boot();
1063 atomic_dec(&cd->readers);
1065 module_put(cd->owner);
1071 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
1073 struct cache_queue *cq, *tmp;
1074 struct cache_request *cr;
1075 struct list_head dequeued;
1077 INIT_LIST_HEAD(&dequeued);
1078 spin_lock(&queue_lock);
1079 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
1081 cr = container_of(cq, struct cache_request, q);
1084 if (test_bit(CACHE_PENDING, &ch->flags))
1085 /* Lost a race and it is pending again */
1087 if (cr->readers != 0)
1089 list_move(&cr->q.list, &dequeued);
1091 spin_unlock(&queue_lock);
1092 while (!list_empty(&dequeued)) {
1093 cr = list_entry(dequeued.next, struct cache_request, q.list);
1094 list_del(&cr->q.list);
1095 cache_put(cr->item, detail);
1102 * Support routines for text-based upcalls.
1103 * Fields are separated by spaces.
1104 * Fields are either mangled to quote space tab newline slosh with slosh
1105 * or a hexified with a leading \x
1106 * Record is terminated with newline.
1110 void qword_add(char **bpp, int *lp, char *str)
1116 if (len < 0) return;
1118 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1131 EXPORT_SYMBOL_GPL(qword_add);
1133 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1138 if (len < 0) return;
1144 while (blen && len >= 2) {
1145 bp = hex_byte_pack(bp, *buf++);
1150 if (blen || len<1) len = -1;
1158 EXPORT_SYMBOL_GPL(qword_addhex);
1160 static void warn_no_listener(struct cache_detail *detail)
1162 if (detail->last_warn != detail->last_close) {
1163 detail->last_warn = detail->last_close;
1164 if (detail->warn_no_listener)
1165 detail->warn_no_listener(detail, detail->last_close != 0);
1169 static bool cache_listeners_exist(struct cache_detail *detail)
1171 if (atomic_read(&detail->readers))
1173 if (detail->last_close == 0)
1174 /* This cache was never opened */
1176 if (detail->last_close < seconds_since_boot() - 30)
1178 * We allow for the possibility that someone might
1179 * restart a userspace daemon without restarting the
1180 * server; but after 30 seconds, we give up.
1187 * register an upcall request to user-space and queue it up for read() by the
1190 * Each request is at most one page long.
1192 int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
1196 struct cache_request *crq;
1199 if (!detail->cache_request)
1202 if (!cache_listeners_exist(detail)) {
1203 warn_no_listener(detail);
1206 if (test_bit(CACHE_CLEANED, &h->flags))
1207 /* Too late to make an upcall */
1210 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1214 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1224 spin_lock(&queue_lock);
1225 if (test_bit(CACHE_PENDING, &h->flags)) {
1226 crq->item = cache_get(h);
1227 list_add_tail(&crq->q.list, &detail->queue);
1229 /* Lost a race, no longer PENDING, so don't enqueue */
1231 spin_unlock(&queue_lock);
1232 wake_up(&queue_wait);
1233 if (ret == -EAGAIN) {
1239 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1242 * parse a message from user-space and pass it
1243 * to an appropriate cache
1244 * Messages are, like requests, separated into fields by
1245 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1248 * reply cachename expiry key ... content....
1250 * key and content are both parsed by cache
1253 int qword_get(char **bpp, char *dest, int bufsize)
1255 /* return bytes copied, or -1 on error */
1259 while (*bp == ' ') bp++;
1261 if (bp[0] == '\\' && bp[1] == 'x') {
1264 while (len < bufsize - 1) {
1267 h = hex_to_bin(bp[0]);
1271 l = hex_to_bin(bp[1]);
1275 *dest++ = (h << 4) | l;
1280 /* text with \nnn octal quoting */
1281 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1283 isodigit(bp[1]) && (bp[1] <= '3') &&
1286 int byte = (*++bp -'0');
1288 byte = (byte << 3) | (*bp++ - '0');
1289 byte = (byte << 3) | (*bp++ - '0');
1299 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1301 while (*bp == ' ') bp++;
1306 EXPORT_SYMBOL_GPL(qword_get);
1310 * support /proc/net/rpc/$CACHENAME/content
1312 * We call ->cache_show passing NULL for the item to
1313 * get a header, then pass each real item in the cache
1316 static void *__cache_seq_start(struct seq_file *m, loff_t *pos)
1319 unsigned int hash, entry;
1320 struct cache_head *ch;
1321 struct cache_detail *cd = m->private;
1324 return SEQ_START_TOKEN;
1326 entry = n & ((1LL<<32) - 1);
1328 hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list)
1331 n &= ~((1LL<<32) - 1);
1335 } while(hash < cd->hash_size &&
1336 hlist_empty(&cd->hash_table[hash]));
1337 if (hash >= cd->hash_size)
1340 return hlist_entry_safe(rcu_dereference_raw(
1341 hlist_first_rcu(&cd->hash_table[hash])),
1342 struct cache_head, cache_list);
1345 static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
1347 struct cache_head *ch = p;
1348 int hash = (*pos >> 32);
1349 struct cache_detail *cd = m->private;
1351 if (p == SEQ_START_TOKEN)
1353 else if (ch->cache_list.next == NULL) {
1358 return hlist_entry_safe(rcu_dereference_raw(
1359 hlist_next_rcu(&ch->cache_list)),
1360 struct cache_head, cache_list);
1362 *pos &= ~((1LL<<32) - 1);
1363 while (hash < cd->hash_size &&
1364 hlist_empty(&cd->hash_table[hash])) {
1368 if (hash >= cd->hash_size)
1371 return hlist_entry_safe(rcu_dereference_raw(
1372 hlist_first_rcu(&cd->hash_table[hash])),
1373 struct cache_head, cache_list);
1375 EXPORT_SYMBOL_GPL(cache_seq_next);
1377 void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos)
1381 return __cache_seq_start(m, pos);
1383 EXPORT_SYMBOL_GPL(cache_seq_start_rcu);
1385 void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos)
1387 return cache_seq_next(file, p, pos);
1389 EXPORT_SYMBOL_GPL(cache_seq_next_rcu);
1391 void cache_seq_stop_rcu(struct seq_file *m, void *p)
1396 EXPORT_SYMBOL_GPL(cache_seq_stop_rcu);
1398 static int c_show(struct seq_file *m, void *p)
1400 struct cache_head *cp = p;
1401 struct cache_detail *cd = m->private;
1403 if (p == SEQ_START_TOKEN)
1404 return cd->cache_show(m, cd, NULL);
1407 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1408 convert_to_wallclock(cp->expiry_time),
1409 kref_read(&cp->ref), cp->flags);
1411 if (cache_check(cd, cp, NULL))
1412 /* cache_check does a cache_put on failure */
1413 seq_printf(m, "# ");
1415 if (cache_is_expired(cd, cp))
1416 seq_printf(m, "# ");
1420 return cd->cache_show(m, cd, cp);
1423 static const struct seq_operations cache_content_op = {
1424 .start = cache_seq_start_rcu,
1425 .next = cache_seq_next_rcu,
1426 .stop = cache_seq_stop_rcu,
1430 static int content_open(struct inode *inode, struct file *file,
1431 struct cache_detail *cd)
1433 struct seq_file *seq;
1436 if (!cd || !try_module_get(cd->owner))
1439 err = seq_open(file, &cache_content_op);
1441 module_put(cd->owner);
1445 seq = file->private_data;
1450 static int content_release(struct inode *inode, struct file *file,
1451 struct cache_detail *cd)
1453 int ret = seq_release(inode, file);
1454 module_put(cd->owner);
1458 static int open_flush(struct inode *inode, struct file *file,
1459 struct cache_detail *cd)
1461 if (!cd || !try_module_get(cd->owner))
1463 return nonseekable_open(inode, file);
1466 static int release_flush(struct inode *inode, struct file *file,
1467 struct cache_detail *cd)
1469 module_put(cd->owner);
1473 static ssize_t read_flush(struct file *file, char __user *buf,
1474 size_t count, loff_t *ppos,
1475 struct cache_detail *cd)
1480 len = snprintf(tbuf, sizeof(tbuf), "%lu\n",
1481 convert_to_wallclock(cd->flush_time));
1482 return simple_read_from_buffer(buf, count, ppos, tbuf, len);
1485 static ssize_t write_flush(struct file *file, const char __user *buf,
1486 size_t count, loff_t *ppos,
1487 struct cache_detail *cd)
1493 if (*ppos || count > sizeof(tbuf)-1)
1495 if (copy_from_user(tbuf, buf, count))
1498 simple_strtoul(tbuf, &ep, 0);
1499 if (*ep && *ep != '\n')
1501 /* Note that while we check that 'buf' holds a valid number,
1502 * we always ignore the value and just flush everything.
1503 * Making use of the number leads to races.
1506 now = seconds_since_boot();
1507 /* Always flush everything, so behave like cache_purge()
1508 * Do this by advancing flush_time to the current time,
1509 * or by one second if it has already reached the current time.
1510 * Newly added cache entries will always have ->last_refresh greater
1511 * that ->flush_time, so they don't get flushed prematurely.
1514 if (cd->flush_time >= now)
1515 now = cd->flush_time + 1;
1517 cd->flush_time = now;
1518 cd->nextcheck = now;
1525 static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1526 size_t count, loff_t *ppos)
1528 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1530 return cache_read(filp, buf, count, ppos, cd);
1533 static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1534 size_t count, loff_t *ppos)
1536 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1538 return cache_write(filp, buf, count, ppos, cd);
1541 static __poll_t cache_poll_procfs(struct file *filp, poll_table *wait)
1543 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1545 return cache_poll(filp, wait, cd);
1548 static long cache_ioctl_procfs(struct file *filp,
1549 unsigned int cmd, unsigned long arg)
1551 struct inode *inode = file_inode(filp);
1552 struct cache_detail *cd = PDE_DATA(inode);
1554 return cache_ioctl(inode, filp, cmd, arg, cd);
1557 static int cache_open_procfs(struct inode *inode, struct file *filp)
1559 struct cache_detail *cd = PDE_DATA(inode);
1561 return cache_open(inode, filp, cd);
1564 static int cache_release_procfs(struct inode *inode, struct file *filp)
1566 struct cache_detail *cd = PDE_DATA(inode);
1568 return cache_release(inode, filp, cd);
1571 static const struct file_operations cache_file_operations_procfs = {
1572 .owner = THIS_MODULE,
1573 .llseek = no_llseek,
1574 .read = cache_read_procfs,
1575 .write = cache_write_procfs,
1576 .poll = cache_poll_procfs,
1577 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
1578 .open = cache_open_procfs,
1579 .release = cache_release_procfs,
1582 static int content_open_procfs(struct inode *inode, struct file *filp)
1584 struct cache_detail *cd = PDE_DATA(inode);
1586 return content_open(inode, filp, cd);
1589 static int content_release_procfs(struct inode *inode, struct file *filp)
1591 struct cache_detail *cd = PDE_DATA(inode);
1593 return content_release(inode, filp, cd);
1596 static const struct file_operations content_file_operations_procfs = {
1597 .open = content_open_procfs,
1599 .llseek = seq_lseek,
1600 .release = content_release_procfs,
1603 static int open_flush_procfs(struct inode *inode, struct file *filp)
1605 struct cache_detail *cd = PDE_DATA(inode);
1607 return open_flush(inode, filp, cd);
1610 static int release_flush_procfs(struct inode *inode, struct file *filp)
1612 struct cache_detail *cd = PDE_DATA(inode);
1614 return release_flush(inode, filp, cd);
1617 static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1618 size_t count, loff_t *ppos)
1620 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1622 return read_flush(filp, buf, count, ppos, cd);
1625 static ssize_t write_flush_procfs(struct file *filp,
1626 const char __user *buf,
1627 size_t count, loff_t *ppos)
1629 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1631 return write_flush(filp, buf, count, ppos, cd);
1634 static const struct file_operations cache_flush_operations_procfs = {
1635 .open = open_flush_procfs,
1636 .read = read_flush_procfs,
1637 .write = write_flush_procfs,
1638 .release = release_flush_procfs,
1639 .llseek = no_llseek,
1642 static void remove_cache_proc_entries(struct cache_detail *cd)
1645 proc_remove(cd->procfs);
1650 #ifdef CONFIG_PROC_FS
1651 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1653 struct proc_dir_entry *p;
1654 struct sunrpc_net *sn;
1656 sn = net_generic(net, sunrpc_net_id);
1657 cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc);
1658 if (cd->procfs == NULL)
1661 p = proc_create_data("flush", S_IFREG | 0600,
1662 cd->procfs, &cache_flush_operations_procfs, cd);
1666 if (cd->cache_request || cd->cache_parse) {
1667 p = proc_create_data("channel", S_IFREG | 0600, cd->procfs,
1668 &cache_file_operations_procfs, cd);
1672 if (cd->cache_show) {
1673 p = proc_create_data("content", S_IFREG | 0400, cd->procfs,
1674 &content_file_operations_procfs, cd);
1680 remove_cache_proc_entries(cd);
1683 #else /* CONFIG_PROC_FS */
1684 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1690 void __init cache_initialize(void)
1692 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
1695 int cache_register_net(struct cache_detail *cd, struct net *net)
1699 sunrpc_init_cache_detail(cd);
1700 ret = create_cache_proc_entries(cd, net);
1702 sunrpc_destroy_cache_detail(cd);
1705 EXPORT_SYMBOL_GPL(cache_register_net);
1707 void cache_unregister_net(struct cache_detail *cd, struct net *net)
1709 remove_cache_proc_entries(cd);
1710 sunrpc_destroy_cache_detail(cd);
1712 EXPORT_SYMBOL_GPL(cache_unregister_net);
1714 struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net)
1716 struct cache_detail *cd;
1719 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1721 return ERR_PTR(-ENOMEM);
1723 cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head),
1725 if (cd->hash_table == NULL) {
1727 return ERR_PTR(-ENOMEM);
1730 for (i = 0; i < cd->hash_size; i++)
1731 INIT_HLIST_HEAD(&cd->hash_table[i]);
1735 EXPORT_SYMBOL_GPL(cache_create_net);
1737 void cache_destroy_net(struct cache_detail *cd, struct net *net)
1739 kfree(cd->hash_table);
1742 EXPORT_SYMBOL_GPL(cache_destroy_net);
1744 static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1745 size_t count, loff_t *ppos)
1747 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1749 return cache_read(filp, buf, count, ppos, cd);
1752 static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1753 size_t count, loff_t *ppos)
1755 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1757 return cache_write(filp, buf, count, ppos, cd);
1760 static __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait)
1762 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1764 return cache_poll(filp, wait, cd);
1767 static long cache_ioctl_pipefs(struct file *filp,
1768 unsigned int cmd, unsigned long arg)
1770 struct inode *inode = file_inode(filp);
1771 struct cache_detail *cd = RPC_I(inode)->private;
1773 return cache_ioctl(inode, filp, cmd, arg, cd);
1776 static int cache_open_pipefs(struct inode *inode, struct file *filp)
1778 struct cache_detail *cd = RPC_I(inode)->private;
1780 return cache_open(inode, filp, cd);
1783 static int cache_release_pipefs(struct inode *inode, struct file *filp)
1785 struct cache_detail *cd = RPC_I(inode)->private;
1787 return cache_release(inode, filp, cd);
1790 const struct file_operations cache_file_operations_pipefs = {
1791 .owner = THIS_MODULE,
1792 .llseek = no_llseek,
1793 .read = cache_read_pipefs,
1794 .write = cache_write_pipefs,
1795 .poll = cache_poll_pipefs,
1796 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
1797 .open = cache_open_pipefs,
1798 .release = cache_release_pipefs,
1801 static int content_open_pipefs(struct inode *inode, struct file *filp)
1803 struct cache_detail *cd = RPC_I(inode)->private;
1805 return content_open(inode, filp, cd);
1808 static int content_release_pipefs(struct inode *inode, struct file *filp)
1810 struct cache_detail *cd = RPC_I(inode)->private;
1812 return content_release(inode, filp, cd);
1815 const struct file_operations content_file_operations_pipefs = {
1816 .open = content_open_pipefs,
1818 .llseek = seq_lseek,
1819 .release = content_release_pipefs,
1822 static int open_flush_pipefs(struct inode *inode, struct file *filp)
1824 struct cache_detail *cd = RPC_I(inode)->private;
1826 return open_flush(inode, filp, cd);
1829 static int release_flush_pipefs(struct inode *inode, struct file *filp)
1831 struct cache_detail *cd = RPC_I(inode)->private;
1833 return release_flush(inode, filp, cd);
1836 static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1837 size_t count, loff_t *ppos)
1839 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1841 return read_flush(filp, buf, count, ppos, cd);
1844 static ssize_t write_flush_pipefs(struct file *filp,
1845 const char __user *buf,
1846 size_t count, loff_t *ppos)
1848 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1850 return write_flush(filp, buf, count, ppos, cd);
1853 const struct file_operations cache_flush_operations_pipefs = {
1854 .open = open_flush_pipefs,
1855 .read = read_flush_pipefs,
1856 .write = write_flush_pipefs,
1857 .release = release_flush_pipefs,
1858 .llseek = no_llseek,
1861 int sunrpc_cache_register_pipefs(struct dentry *parent,
1862 const char *name, umode_t umode,
1863 struct cache_detail *cd)
1865 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1867 return PTR_ERR(dir);
1871 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1873 void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1876 rpc_remove_cache_dir(cd->pipefs);
1880 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1882 void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h)
1884 spin_lock(&cd->hash_lock);
1885 if (!hlist_unhashed(&h->cache_list)){
1886 hlist_del_init_rcu(&h->cache_list);
1888 spin_unlock(&cd->hash_lock);
1891 spin_unlock(&cd->hash_lock);
1893 EXPORT_SYMBOL_GPL(sunrpc_cache_unhash);