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 <linux/string_helpers.h>
24 #include <asm/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_unlocked(struct cache_head *head,
58 struct cache_detail *detail);
60 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
61 struct cache_head *key, int hash)
63 struct cache_head *new = NULL, *freeme = NULL, *tmp = NULL;
64 struct hlist_head *head;
66 head = &detail->hash_table[hash];
68 read_lock(&detail->hash_lock);
70 hlist_for_each_entry(tmp, head, cache_list) {
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
90 cache_init(new, detail);
91 detail->init(new, key);
93 write_lock(&detail->hash_lock);
95 /* check if entry appeared while we slept */
96 hlist_for_each_entry(tmp, head, cache_list) {
97 if (detail->match(tmp, key)) {
98 if (cache_is_expired(detail, tmp)) {
99 hlist_del_init(&tmp->cache_list);
105 write_unlock(&detail->hash_lock);
106 cache_put(new, detail);
111 hlist_add_head(&new->cache_list, head);
114 write_unlock(&detail->hash_lock);
117 cache_fresh_unlocked(freeme, detail);
118 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,
128 struct cache_detail *detail)
130 time_t now = seconds_since_boot();
131 if (now <= detail->flush_time)
132 /* ensure it isn't immediately treated as expired */
133 now = detail->flush_time + 1;
134 head->expiry_time = expiry;
135 head->last_refresh = now;
136 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
137 set_bit(CACHE_VALID, &head->flags);
140 static void cache_fresh_unlocked(struct cache_head *head,
141 struct cache_detail *detail)
143 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
144 cache_revisit_request(head);
145 cache_dequeue(detail, head);
149 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
150 struct cache_head *new, struct cache_head *old, int hash)
152 /* The 'old' entry is to be replaced by 'new'.
153 * If 'old' is not VALID, we update it directly,
154 * otherwise we need to replace it
156 struct cache_head *tmp;
158 if (!test_bit(CACHE_VALID, &old->flags)) {
159 write_lock(&detail->hash_lock);
160 if (!test_bit(CACHE_VALID, &old->flags)) {
161 if (test_bit(CACHE_NEGATIVE, &new->flags))
162 set_bit(CACHE_NEGATIVE, &old->flags);
164 detail->update(old, new);
165 cache_fresh_locked(old, new->expiry_time, detail);
166 write_unlock(&detail->hash_lock);
167 cache_fresh_unlocked(old, detail);
170 write_unlock(&detail->hash_lock);
172 /* We need to insert a new entry */
173 tmp = detail->alloc();
175 cache_put(old, detail);
178 cache_init(tmp, detail);
179 detail->init(tmp, old);
181 write_lock(&detail->hash_lock);
182 if (test_bit(CACHE_NEGATIVE, &new->flags))
183 set_bit(CACHE_NEGATIVE, &tmp->flags);
185 detail->update(tmp, new);
186 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
189 cache_fresh_locked(tmp, new->expiry_time, detail);
190 cache_fresh_locked(old, 0, detail);
191 write_unlock(&detail->hash_lock);
192 cache_fresh_unlocked(tmp, detail);
193 cache_fresh_unlocked(old, detail);
194 cache_put(old, detail);
197 EXPORT_SYMBOL_GPL(sunrpc_cache_update);
199 static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
201 if (cd->cache_upcall)
202 return cd->cache_upcall(cd, h);
203 return sunrpc_cache_pipe_upcall(cd, h);
206 static inline int cache_is_valid(struct cache_head *h)
208 if (!test_bit(CACHE_VALID, &h->flags))
212 if (test_bit(CACHE_NEGATIVE, &h->flags))
216 * In combination with write barrier in
217 * sunrpc_cache_update, ensures that anyone
218 * using the cache entry after this sees the
227 static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
231 write_lock(&detail->hash_lock);
232 rv = cache_is_valid(h);
234 set_bit(CACHE_NEGATIVE, &h->flags);
235 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
239 write_unlock(&detail->hash_lock);
240 cache_fresh_unlocked(h, detail);
245 * This is the generic cache management routine for all
246 * the authentication caches.
247 * It checks the currency of a cache item and will (later)
248 * initiate an upcall to fill it if needed.
251 * Returns 0 if the cache_head can be used, or cache_puts it and returns
252 * -EAGAIN if upcall is pending and request has been queued
253 * -ETIMEDOUT if upcall failed or request could not be queue or
254 * upcall completed but item is still invalid (implying that
255 * the cache item has been replaced with a newer one).
256 * -ENOENT if cache entry was negative
258 int cache_check(struct cache_detail *detail,
259 struct cache_head *h, struct cache_req *rqstp)
262 long refresh_age, age;
264 /* First decide return status as best we can */
265 rv = cache_is_valid(h);
267 /* now see if we want to start an upcall */
268 refresh_age = (h->expiry_time - h->last_refresh);
269 age = seconds_since_boot() - h->last_refresh;
274 } else if (rv == -EAGAIN ||
275 (h->expiry_time != 0 && age > refresh_age/2)) {
276 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
278 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
279 switch (cache_make_upcall(detail, h)) {
281 rv = try_to_negate_entry(detail, h);
284 cache_fresh_unlocked(h, detail);
291 if (!cache_defer_req(rqstp, h)) {
293 * Request was not deferred; handle it as best
296 rv = cache_is_valid(h);
302 cache_put(h, detail);
305 EXPORT_SYMBOL_GPL(cache_check);
308 * caches need to be periodically cleaned.
309 * For this we maintain a list of cache_detail and
310 * a current pointer into that list and into the table
313 * Each time cache_clean is called it finds the next non-empty entry
314 * in the current table and walks the list in that entry
315 * looking for entries that can be removed.
317 * An entry gets removed if:
318 * - The expiry is before current time
319 * - The last_refresh time is before the flush_time for that cache
321 * later we might drop old entries with non-NEVER expiry if that table
322 * is getting 'full' for some definition of 'full'
324 * The question of "how often to scan a table" is an interesting one
325 * and is answered in part by the use of the "nextcheck" field in the
327 * When a scan of a table begins, the nextcheck field is set to a time
328 * that is well into the future.
329 * While scanning, if an expiry time is found that is earlier than the
330 * current nextcheck time, nextcheck is set to that expiry time.
331 * If the flush_time is ever set to a time earlier than the nextcheck
332 * time, the nextcheck time is then set to that flush_time.
334 * A table is then only scanned if the current time is at least
335 * the nextcheck time.
339 static LIST_HEAD(cache_list);
340 static DEFINE_SPINLOCK(cache_list_lock);
341 static struct cache_detail *current_detail;
342 static int current_index;
344 static void do_cache_clean(struct work_struct *work);
345 static struct delayed_work cache_cleaner;
347 void sunrpc_init_cache_detail(struct cache_detail *cd)
349 rwlock_init(&cd->hash_lock);
350 INIT_LIST_HEAD(&cd->queue);
351 spin_lock(&cache_list_lock);
354 atomic_set(&cd->readers, 0);
357 list_add(&cd->others, &cache_list);
358 spin_unlock(&cache_list_lock);
360 /* start the cleaning process */
361 queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
363 EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
365 void sunrpc_destroy_cache_detail(struct cache_detail *cd)
368 spin_lock(&cache_list_lock);
369 write_lock(&cd->hash_lock);
371 write_unlock(&cd->hash_lock);
372 spin_unlock(&cache_list_lock);
375 if (current_detail == cd)
376 current_detail = NULL;
377 list_del_init(&cd->others);
378 write_unlock(&cd->hash_lock);
379 spin_unlock(&cache_list_lock);
380 if (list_empty(&cache_list)) {
381 /* module must be being unloaded so its safe to kill the worker */
382 cancel_delayed_work_sync(&cache_cleaner);
386 printk(KERN_ERR "RPC: failed to unregister %s cache\n", cd->name);
388 EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
390 /* clean cache tries to find something to clean
392 * It returns 1 if it cleaned something,
393 * 0 if it didn't find anything this time
394 * -1 if it fell off the end of the list.
396 static int cache_clean(void)
399 struct list_head *next;
401 spin_lock(&cache_list_lock);
403 /* find a suitable table if we don't already have one */
404 while (current_detail == NULL ||
405 current_index >= current_detail->hash_size) {
407 next = current_detail->others.next;
409 next = cache_list.next;
410 if (next == &cache_list) {
411 current_detail = NULL;
412 spin_unlock(&cache_list_lock);
415 current_detail = list_entry(next, struct cache_detail, others);
416 if (current_detail->nextcheck > seconds_since_boot())
417 current_index = current_detail->hash_size;
420 current_detail->nextcheck = seconds_since_boot()+30*60;
424 /* find a non-empty bucket in the table */
425 while (current_detail &&
426 current_index < current_detail->hash_size &&
427 hlist_empty(¤t_detail->hash_table[current_index]))
430 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
432 if (current_detail && current_index < current_detail->hash_size) {
433 struct cache_head *ch = NULL;
434 struct cache_detail *d;
435 struct hlist_head *head;
436 struct hlist_node *tmp;
438 write_lock(¤t_detail->hash_lock);
440 /* Ok, now to clean this strand */
442 head = ¤t_detail->hash_table[current_index];
443 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
444 if (current_detail->nextcheck > ch->expiry_time)
445 current_detail->nextcheck = ch->expiry_time+1;
446 if (!cache_is_expired(current_detail, ch))
449 hlist_del_init(&ch->cache_list);
450 current_detail->entries--;
455 write_unlock(¤t_detail->hash_lock);
459 spin_unlock(&cache_list_lock);
461 set_bit(CACHE_CLEANED, &ch->flags);
462 cache_fresh_unlocked(ch, d);
466 spin_unlock(&cache_list_lock);
472 * We want to regularly clean the cache, so we need to schedule some work ...
474 static void do_cache_clean(struct work_struct *work)
477 if (cache_clean() == -1)
478 delay = round_jiffies_relative(30*HZ);
480 if (list_empty(&cache_list))
484 queue_delayed_work(system_power_efficient_wq,
485 &cache_cleaner, delay);
490 * Clean all caches promptly. This just calls cache_clean
491 * repeatedly until we are sure that every cache has had a chance to
494 void cache_flush(void)
496 while (cache_clean() != -1)
498 while (cache_clean() != -1)
501 EXPORT_SYMBOL_GPL(cache_flush);
503 void cache_purge(struct cache_detail *detail)
505 time_t now = seconds_since_boot();
506 if (detail->flush_time >= now)
507 now = detail->flush_time + 1;
508 /* 'now' is the maximum value any 'last_refresh' can have */
509 detail->flush_time = now;
510 detail->nextcheck = seconds_since_boot();
513 EXPORT_SYMBOL_GPL(cache_purge);
517 * Deferral and Revisiting of Requests.
519 * If a cache lookup finds a pending entry, we
520 * need to defer the request and revisit it later.
521 * All deferred requests are stored in a hash table,
522 * indexed by "struct cache_head *".
523 * As it may be wasteful to store a whole request
524 * structure, we allow the request to provide a
525 * deferred form, which must contain a
526 * 'struct cache_deferred_req'
527 * This cache_deferred_req contains a method to allow
528 * it to be revisited when cache info is available
531 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
532 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
534 #define DFR_MAX 300 /* ??? */
536 static DEFINE_SPINLOCK(cache_defer_lock);
537 static LIST_HEAD(cache_defer_list);
538 static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
539 static int cache_defer_cnt;
541 static void __unhash_deferred_req(struct cache_deferred_req *dreq)
543 hlist_del_init(&dreq->hash);
544 if (!list_empty(&dreq->recent)) {
545 list_del_init(&dreq->recent);
550 static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
552 int hash = DFR_HASH(item);
554 INIT_LIST_HEAD(&dreq->recent);
555 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
558 static void setup_deferral(struct cache_deferred_req *dreq,
559 struct cache_head *item,
565 spin_lock(&cache_defer_lock);
567 __hash_deferred_req(dreq, item);
571 list_add(&dreq->recent, &cache_defer_list);
574 spin_unlock(&cache_defer_lock);
578 struct thread_deferred_req {
579 struct cache_deferred_req handle;
580 struct completion completion;
583 static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
585 struct thread_deferred_req *dr =
586 container_of(dreq, struct thread_deferred_req, handle);
587 complete(&dr->completion);
590 static void cache_wait_req(struct cache_req *req, struct cache_head *item)
592 struct thread_deferred_req sleeper;
593 struct cache_deferred_req *dreq = &sleeper.handle;
595 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
596 dreq->revisit = cache_restart_thread;
598 setup_deferral(dreq, item, 0);
600 if (!test_bit(CACHE_PENDING, &item->flags) ||
601 wait_for_completion_interruptible_timeout(
602 &sleeper.completion, req->thread_wait) <= 0) {
603 /* The completion wasn't completed, so we need
606 spin_lock(&cache_defer_lock);
607 if (!hlist_unhashed(&sleeper.handle.hash)) {
608 __unhash_deferred_req(&sleeper.handle);
609 spin_unlock(&cache_defer_lock);
611 /* cache_revisit_request already removed
612 * this from the hash table, but hasn't
613 * called ->revisit yet. It will very soon
614 * and we need to wait for it.
616 spin_unlock(&cache_defer_lock);
617 wait_for_completion(&sleeper.completion);
622 static void cache_limit_defers(void)
624 /* Make sure we haven't exceed the limit of allowed deferred
627 struct cache_deferred_req *discard = NULL;
629 if (cache_defer_cnt <= DFR_MAX)
632 spin_lock(&cache_defer_lock);
634 /* Consider removing either the first or the last */
635 if (cache_defer_cnt > DFR_MAX) {
636 if (prandom_u32() & 1)
637 discard = list_entry(cache_defer_list.next,
638 struct cache_deferred_req, recent);
640 discard = list_entry(cache_defer_list.prev,
641 struct cache_deferred_req, recent);
642 __unhash_deferred_req(discard);
644 spin_unlock(&cache_defer_lock);
646 discard->revisit(discard, 1);
649 /* Return true if and only if a deferred request is queued. */
650 static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
652 struct cache_deferred_req *dreq;
654 if (req->thread_wait) {
655 cache_wait_req(req, item);
656 if (!test_bit(CACHE_PENDING, &item->flags))
659 dreq = req->defer(req);
662 setup_deferral(dreq, item, 1);
663 if (!test_bit(CACHE_PENDING, &item->flags))
664 /* Bit could have been cleared before we managed to
665 * set up the deferral, so need to revisit just in case
667 cache_revisit_request(item);
669 cache_limit_defers();
673 static void cache_revisit_request(struct cache_head *item)
675 struct cache_deferred_req *dreq;
676 struct list_head pending;
677 struct hlist_node *tmp;
678 int hash = DFR_HASH(item);
680 INIT_LIST_HEAD(&pending);
681 spin_lock(&cache_defer_lock);
683 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
684 if (dreq->item == item) {
685 __unhash_deferred_req(dreq);
686 list_add(&dreq->recent, &pending);
689 spin_unlock(&cache_defer_lock);
691 while (!list_empty(&pending)) {
692 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
693 list_del_init(&dreq->recent);
694 dreq->revisit(dreq, 0);
698 void cache_clean_deferred(void *owner)
700 struct cache_deferred_req *dreq, *tmp;
701 struct list_head pending;
704 INIT_LIST_HEAD(&pending);
705 spin_lock(&cache_defer_lock);
707 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
708 if (dreq->owner == owner) {
709 __unhash_deferred_req(dreq);
710 list_add(&dreq->recent, &pending);
713 spin_unlock(&cache_defer_lock);
715 while (!list_empty(&pending)) {
716 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
717 list_del_init(&dreq->recent);
718 dreq->revisit(dreq, 1);
723 * communicate with user-space
725 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
726 * On read, you get a full request, or block.
727 * On write, an update request is processed.
728 * Poll works if anything to read, and always allows write.
730 * Implemented by linked list of requests. Each open file has
731 * a ->private that also exists in this list. New requests are added
732 * to the end and may wakeup and preceding readers.
733 * New readers are added to the head. If, on read, an item is found with
734 * CACHE_UPCALLING clear, we free it from the list.
738 static DEFINE_SPINLOCK(queue_lock);
739 static DEFINE_MUTEX(queue_io_mutex);
742 struct list_head list;
743 int reader; /* if 0, then request */
745 struct cache_request {
746 struct cache_queue q;
747 struct cache_head *item;
752 struct cache_reader {
753 struct cache_queue q;
754 int offset; /* if non-0, we have a refcnt on next request */
757 static int cache_request(struct cache_detail *detail,
758 struct cache_request *crq)
763 detail->cache_request(detail, crq->item, &bp, &len);
766 return PAGE_SIZE - len;
769 static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
770 loff_t *ppos, struct cache_detail *cd)
772 struct cache_reader *rp = filp->private_data;
773 struct cache_request *rq;
774 struct inode *inode = file_inode(filp);
780 inode_lock(inode); /* protect against multiple concurrent
781 * readers on this file */
783 spin_lock(&queue_lock);
784 /* need to find next request */
785 while (rp->q.list.next != &cd->queue &&
786 list_entry(rp->q.list.next, struct cache_queue, list)
788 struct list_head *next = rp->q.list.next;
789 list_move(&rp->q.list, next);
791 if (rp->q.list.next == &cd->queue) {
792 spin_unlock(&queue_lock);
794 WARN_ON_ONCE(rp->offset);
797 rq = container_of(rp->q.list.next, struct cache_request, q.list);
798 WARN_ON_ONCE(rq->q.reader);
801 spin_unlock(&queue_lock);
804 err = cache_request(cd, rq);
810 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
812 spin_lock(&queue_lock);
813 list_move(&rp->q.list, &rq->q.list);
814 spin_unlock(&queue_lock);
816 if (rp->offset + count > rq->len)
817 count = rq->len - rp->offset;
819 if (copy_to_user(buf, rq->buf + rp->offset, count))
822 if (rp->offset >= rq->len) {
824 spin_lock(&queue_lock);
825 list_move(&rp->q.list, &rq->q.list);
826 spin_unlock(&queue_lock);
831 if (rp->offset == 0) {
832 /* need to release rq */
833 spin_lock(&queue_lock);
835 if (rq->readers == 0 &&
836 !test_bit(CACHE_PENDING, &rq->item->flags)) {
837 list_del(&rq->q.list);
838 spin_unlock(&queue_lock);
839 cache_put(rq->item, cd);
843 spin_unlock(&queue_lock);
848 return err ? err : count;
851 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
852 size_t count, struct cache_detail *cd)
858 if (copy_from_user(kaddr, buf, count))
861 ret = cd->cache_parse(cd, kaddr, count);
867 static ssize_t cache_slow_downcall(const char __user *buf,
868 size_t count, struct cache_detail *cd)
870 static char write_buf[8192]; /* protected by queue_io_mutex */
871 ssize_t ret = -EINVAL;
873 if (count >= sizeof(write_buf))
875 mutex_lock(&queue_io_mutex);
876 ret = cache_do_downcall(write_buf, buf, count, cd);
877 mutex_unlock(&queue_io_mutex);
882 static ssize_t cache_downcall(struct address_space *mapping,
883 const char __user *buf,
884 size_t count, struct cache_detail *cd)
888 ssize_t ret = -ENOMEM;
890 if (count >= PAGE_SIZE)
893 page = find_or_create_page(mapping, 0, GFP_KERNEL);
898 ret = cache_do_downcall(kaddr, buf, count, cd);
904 return cache_slow_downcall(buf, count, cd);
907 static ssize_t cache_write(struct file *filp, const char __user *buf,
908 size_t count, loff_t *ppos,
909 struct cache_detail *cd)
911 struct address_space *mapping = filp->f_mapping;
912 struct inode *inode = file_inode(filp);
913 ssize_t ret = -EINVAL;
915 if (!cd->cache_parse)
919 ret = cache_downcall(mapping, buf, count, cd);
925 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
927 static unsigned int cache_poll(struct file *filp, poll_table *wait,
928 struct cache_detail *cd)
931 struct cache_reader *rp = filp->private_data;
932 struct cache_queue *cq;
934 poll_wait(filp, &queue_wait, wait);
936 /* alway allow write */
937 mask = POLLOUT | POLLWRNORM;
942 spin_lock(&queue_lock);
944 for (cq= &rp->q; &cq->list != &cd->queue;
945 cq = list_entry(cq->list.next, struct cache_queue, list))
947 mask |= POLLIN | POLLRDNORM;
950 spin_unlock(&queue_lock);
954 static int cache_ioctl(struct inode *ino, struct file *filp,
955 unsigned int cmd, unsigned long arg,
956 struct cache_detail *cd)
959 struct cache_reader *rp = filp->private_data;
960 struct cache_queue *cq;
962 if (cmd != FIONREAD || !rp)
965 spin_lock(&queue_lock);
967 /* only find the length remaining in current request,
968 * or the length of the next request
970 for (cq= &rp->q; &cq->list != &cd->queue;
971 cq = list_entry(cq->list.next, struct cache_queue, list))
973 struct cache_request *cr =
974 container_of(cq, struct cache_request, q);
975 len = cr->len - rp->offset;
978 spin_unlock(&queue_lock);
980 return put_user(len, (int __user *)arg);
983 static int cache_open(struct inode *inode, struct file *filp,
984 struct cache_detail *cd)
986 struct cache_reader *rp = NULL;
988 if (!cd || !try_module_get(cd->owner))
990 nonseekable_open(inode, filp);
991 if (filp->f_mode & FMODE_READ) {
992 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
994 module_put(cd->owner);
999 atomic_inc(&cd->readers);
1000 spin_lock(&queue_lock);
1001 list_add(&rp->q.list, &cd->queue);
1002 spin_unlock(&queue_lock);
1004 filp->private_data = rp;
1008 static int cache_release(struct inode *inode, struct file *filp,
1009 struct cache_detail *cd)
1011 struct cache_reader *rp = filp->private_data;
1014 spin_lock(&queue_lock);
1016 struct cache_queue *cq;
1017 for (cq= &rp->q; &cq->list != &cd->queue;
1018 cq = list_entry(cq->list.next, struct cache_queue, list))
1020 container_of(cq, struct cache_request, q)
1026 list_del(&rp->q.list);
1027 spin_unlock(&queue_lock);
1029 filp->private_data = NULL;
1032 cd->last_close = seconds_since_boot();
1033 atomic_dec(&cd->readers);
1035 module_put(cd->owner);
1041 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
1043 struct cache_queue *cq, *tmp;
1044 struct cache_request *cr;
1045 struct list_head dequeued;
1047 INIT_LIST_HEAD(&dequeued);
1048 spin_lock(&queue_lock);
1049 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
1051 cr = container_of(cq, struct cache_request, q);
1054 if (test_bit(CACHE_PENDING, &ch->flags))
1055 /* Lost a race and it is pending again */
1057 if (cr->readers != 0)
1059 list_move(&cr->q.list, &dequeued);
1061 spin_unlock(&queue_lock);
1062 while (!list_empty(&dequeued)) {
1063 cr = list_entry(dequeued.next, struct cache_request, q.list);
1064 list_del(&cr->q.list);
1065 cache_put(cr->item, detail);
1072 * Support routines for text-based upcalls.
1073 * Fields are separated by spaces.
1074 * Fields are either mangled to quote space tab newline slosh with slosh
1075 * or a hexified with a leading \x
1076 * Record is terminated with newline.
1080 void qword_add(char **bpp, int *lp, char *str)
1086 if (len < 0) return;
1088 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1101 EXPORT_SYMBOL_GPL(qword_add);
1103 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1108 if (len < 0) return;
1114 while (blen && len >= 2) {
1115 bp = hex_byte_pack(bp, *buf++);
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);
1194 spin_lock(&queue_lock);
1195 if (test_bit(CACHE_PENDING, &h->flags)) {
1196 crq->item = cache_get(h);
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 - 1) {
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
1286 void *cache_seq_start(struct seq_file *m, loff_t *pos)
1287 __acquires(cd->hash_lock)
1290 unsigned int hash, entry;
1291 struct cache_head *ch;
1292 struct cache_detail *cd = m->private;
1294 read_lock(&cd->hash_lock);
1296 return SEQ_START_TOKEN;
1298 entry = n & ((1LL<<32) - 1);
1300 hlist_for_each_entry(ch, &cd->hash_table[hash], cache_list)
1303 n &= ~((1LL<<32) - 1);
1307 } while(hash < cd->hash_size &&
1308 hlist_empty(&cd->hash_table[hash]));
1309 if (hash >= cd->hash_size)
1312 return hlist_entry_safe(cd->hash_table[hash].first,
1313 struct cache_head, cache_list);
1315 EXPORT_SYMBOL_GPL(cache_seq_start);
1317 void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
1319 struct cache_head *ch = p;
1320 int hash = (*pos >> 32);
1321 struct cache_detail *cd = m->private;
1323 if (p == SEQ_START_TOKEN)
1325 else if (ch->cache_list.next == NULL) {
1330 return hlist_entry_safe(ch->cache_list.next,
1331 struct cache_head, cache_list);
1333 *pos &= ~((1LL<<32) - 1);
1334 while (hash < cd->hash_size &&
1335 hlist_empty(&cd->hash_table[hash])) {
1339 if (hash >= cd->hash_size)
1342 return hlist_entry_safe(cd->hash_table[hash].first,
1343 struct cache_head, cache_list);
1345 EXPORT_SYMBOL_GPL(cache_seq_next);
1347 void cache_seq_stop(struct seq_file *m, void *p)
1348 __releases(cd->hash_lock)
1350 struct cache_detail *cd = m->private;
1351 read_unlock(&cd->hash_lock);
1353 EXPORT_SYMBOL_GPL(cache_seq_stop);
1355 static int c_show(struct seq_file *m, void *p)
1357 struct cache_head *cp = p;
1358 struct cache_detail *cd = m->private;
1360 if (p == SEQ_START_TOKEN)
1361 return cd->cache_show(m, cd, NULL);
1364 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1365 convert_to_wallclock(cp->expiry_time),
1366 atomic_read(&cp->ref.refcount), cp->flags);
1368 if (cache_check(cd, cp, NULL))
1369 /* cache_check does a cache_put on failure */
1370 seq_printf(m, "# ");
1372 if (cache_is_expired(cd, cp))
1373 seq_printf(m, "# ");
1377 return cd->cache_show(m, cd, cp);
1380 static const struct seq_operations cache_content_op = {
1381 .start = cache_seq_start,
1382 .next = cache_seq_next,
1383 .stop = cache_seq_stop,
1387 static int content_open(struct inode *inode, struct file *file,
1388 struct cache_detail *cd)
1390 struct seq_file *seq;
1393 if (!cd || !try_module_get(cd->owner))
1396 err = seq_open(file, &cache_content_op);
1398 module_put(cd->owner);
1402 seq = file->private_data;
1407 static int content_release(struct inode *inode, struct file *file,
1408 struct cache_detail *cd)
1410 int ret = seq_release(inode, file);
1411 module_put(cd->owner);
1415 static int open_flush(struct inode *inode, struct file *file,
1416 struct cache_detail *cd)
1418 if (!cd || !try_module_get(cd->owner))
1420 return nonseekable_open(inode, file);
1423 static int release_flush(struct inode *inode, struct file *file,
1424 struct cache_detail *cd)
1426 module_put(cd->owner);
1430 static ssize_t read_flush(struct file *file, char __user *buf,
1431 size_t count, loff_t *ppos,
1432 struct cache_detail *cd)
1435 unsigned long p = *ppos;
1438 snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
1445 if (copy_to_user(buf, (void*)(tbuf+p), len))
1451 static ssize_t write_flush(struct file *file, const char __user *buf,
1452 size_t count, loff_t *ppos,
1453 struct cache_detail *cd)
1459 if (*ppos || count > sizeof(tbuf)-1)
1461 if (copy_from_user(tbuf, buf, count))
1464 simple_strtoul(tbuf, &ep, 0);
1465 if (*ep && *ep != '\n')
1469 then = get_expiry(&bp);
1470 now = seconds_since_boot();
1471 cd->nextcheck = now;
1472 /* Can only set flush_time to 1 second beyond "now", or
1473 * possibly 1 second beyond flushtime. This is because
1474 * flush_time never goes backwards so it mustn't get too far
1478 /* Want to flush everything, so behave like cache_purge() */
1479 if (cd->flush_time >= now)
1480 now = cd->flush_time + 1;
1484 cd->flush_time = then;
1491 static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1492 size_t count, loff_t *ppos)
1494 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1496 return cache_read(filp, buf, count, ppos, cd);
1499 static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1500 size_t count, loff_t *ppos)
1502 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1504 return cache_write(filp, buf, count, ppos, cd);
1507 static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1509 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1511 return cache_poll(filp, wait, cd);
1514 static long cache_ioctl_procfs(struct file *filp,
1515 unsigned int cmd, unsigned long arg)
1517 struct inode *inode = file_inode(filp);
1518 struct cache_detail *cd = PDE_DATA(inode);
1520 return cache_ioctl(inode, filp, cmd, arg, cd);
1523 static int cache_open_procfs(struct inode *inode, struct file *filp)
1525 struct cache_detail *cd = PDE_DATA(inode);
1527 return cache_open(inode, filp, cd);
1530 static int cache_release_procfs(struct inode *inode, struct file *filp)
1532 struct cache_detail *cd = PDE_DATA(inode);
1534 return cache_release(inode, filp, cd);
1537 static const struct file_operations cache_file_operations_procfs = {
1538 .owner = THIS_MODULE,
1539 .llseek = no_llseek,
1540 .read = cache_read_procfs,
1541 .write = cache_write_procfs,
1542 .poll = cache_poll_procfs,
1543 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
1544 .open = cache_open_procfs,
1545 .release = cache_release_procfs,
1548 static int content_open_procfs(struct inode *inode, struct file *filp)
1550 struct cache_detail *cd = PDE_DATA(inode);
1552 return content_open(inode, filp, cd);
1555 static int content_release_procfs(struct inode *inode, struct file *filp)
1557 struct cache_detail *cd = PDE_DATA(inode);
1559 return content_release(inode, filp, cd);
1562 static const struct file_operations content_file_operations_procfs = {
1563 .open = content_open_procfs,
1565 .llseek = seq_lseek,
1566 .release = content_release_procfs,
1569 static int open_flush_procfs(struct inode *inode, struct file *filp)
1571 struct cache_detail *cd = PDE_DATA(inode);
1573 return open_flush(inode, filp, cd);
1576 static int release_flush_procfs(struct inode *inode, struct file *filp)
1578 struct cache_detail *cd = PDE_DATA(inode);
1580 return release_flush(inode, filp, cd);
1583 static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1584 size_t count, loff_t *ppos)
1586 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1588 return read_flush(filp, buf, count, ppos, cd);
1591 static ssize_t write_flush_procfs(struct file *filp,
1592 const char __user *buf,
1593 size_t count, loff_t *ppos)
1595 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1597 return write_flush(filp, buf, count, ppos, cd);
1600 static const struct file_operations cache_flush_operations_procfs = {
1601 .open = open_flush_procfs,
1602 .read = read_flush_procfs,
1603 .write = write_flush_procfs,
1604 .release = release_flush_procfs,
1605 .llseek = no_llseek,
1608 static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
1610 struct sunrpc_net *sn;
1612 if (cd->u.procfs.proc_ent == NULL)
1614 if (cd->u.procfs.flush_ent)
1615 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1616 if (cd->u.procfs.channel_ent)
1617 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1618 if (cd->u.procfs.content_ent)
1619 remove_proc_entry("content", cd->u.procfs.proc_ent);
1620 cd->u.procfs.proc_ent = NULL;
1621 sn = net_generic(net, sunrpc_net_id);
1622 remove_proc_entry(cd->name, sn->proc_net_rpc);
1625 #ifdef CONFIG_PROC_FS
1626 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1628 struct proc_dir_entry *p;
1629 struct sunrpc_net *sn;
1631 sn = net_generic(net, sunrpc_net_id);
1632 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
1633 if (cd->u.procfs.proc_ent == NULL)
1635 cd->u.procfs.channel_ent = NULL;
1636 cd->u.procfs.content_ent = NULL;
1638 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1639 cd->u.procfs.proc_ent,
1640 &cache_flush_operations_procfs, cd);
1641 cd->u.procfs.flush_ent = p;
1645 if (cd->cache_request || cd->cache_parse) {
1646 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1647 cd->u.procfs.proc_ent,
1648 &cache_file_operations_procfs, cd);
1649 cd->u.procfs.channel_ent = p;
1653 if (cd->cache_show) {
1654 p = proc_create_data("content", S_IFREG|S_IRUSR,
1655 cd->u.procfs.proc_ent,
1656 &content_file_operations_procfs, cd);
1657 cd->u.procfs.content_ent = p;
1663 remove_cache_proc_entries(cd, net);
1666 #else /* CONFIG_PROC_FS */
1667 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1673 void __init cache_initialize(void)
1675 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
1678 int cache_register_net(struct cache_detail *cd, struct net *net)
1682 sunrpc_init_cache_detail(cd);
1683 ret = create_cache_proc_entries(cd, net);
1685 sunrpc_destroy_cache_detail(cd);
1688 EXPORT_SYMBOL_GPL(cache_register_net);
1690 void cache_unregister_net(struct cache_detail *cd, struct net *net)
1692 remove_cache_proc_entries(cd, net);
1693 sunrpc_destroy_cache_detail(cd);
1695 EXPORT_SYMBOL_GPL(cache_unregister_net);
1697 struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
1699 struct cache_detail *cd;
1702 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1704 return ERR_PTR(-ENOMEM);
1706 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct hlist_head),
1708 if (cd->hash_table == NULL) {
1710 return ERR_PTR(-ENOMEM);
1713 for (i = 0; i < cd->hash_size; i++)
1714 INIT_HLIST_HEAD(&cd->hash_table[i]);
1718 EXPORT_SYMBOL_GPL(cache_create_net);
1720 void cache_destroy_net(struct cache_detail *cd, struct net *net)
1722 kfree(cd->hash_table);
1725 EXPORT_SYMBOL_GPL(cache_destroy_net);
1727 static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1728 size_t count, loff_t *ppos)
1730 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1732 return cache_read(filp, buf, count, ppos, cd);
1735 static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1736 size_t count, loff_t *ppos)
1738 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1740 return cache_write(filp, buf, count, ppos, cd);
1743 static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1745 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1747 return cache_poll(filp, wait, cd);
1750 static long cache_ioctl_pipefs(struct file *filp,
1751 unsigned int cmd, unsigned long arg)
1753 struct inode *inode = file_inode(filp);
1754 struct cache_detail *cd = RPC_I(inode)->private;
1756 return cache_ioctl(inode, filp, cmd, arg, cd);
1759 static int cache_open_pipefs(struct inode *inode, struct file *filp)
1761 struct cache_detail *cd = RPC_I(inode)->private;
1763 return cache_open(inode, filp, cd);
1766 static int cache_release_pipefs(struct inode *inode, struct file *filp)
1768 struct cache_detail *cd = RPC_I(inode)->private;
1770 return cache_release(inode, filp, cd);
1773 const struct file_operations cache_file_operations_pipefs = {
1774 .owner = THIS_MODULE,
1775 .llseek = no_llseek,
1776 .read = cache_read_pipefs,
1777 .write = cache_write_pipefs,
1778 .poll = cache_poll_pipefs,
1779 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
1780 .open = cache_open_pipefs,
1781 .release = cache_release_pipefs,
1784 static int content_open_pipefs(struct inode *inode, struct file *filp)
1786 struct cache_detail *cd = RPC_I(inode)->private;
1788 return content_open(inode, filp, cd);
1791 static int content_release_pipefs(struct inode *inode, struct file *filp)
1793 struct cache_detail *cd = RPC_I(inode)->private;
1795 return content_release(inode, filp, cd);
1798 const struct file_operations content_file_operations_pipefs = {
1799 .open = content_open_pipefs,
1801 .llseek = seq_lseek,
1802 .release = content_release_pipefs,
1805 static int open_flush_pipefs(struct inode *inode, struct file *filp)
1807 struct cache_detail *cd = RPC_I(inode)->private;
1809 return open_flush(inode, filp, cd);
1812 static int release_flush_pipefs(struct inode *inode, struct file *filp)
1814 struct cache_detail *cd = RPC_I(inode)->private;
1816 return release_flush(inode, filp, cd);
1819 static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1820 size_t count, loff_t *ppos)
1822 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1824 return read_flush(filp, buf, count, ppos, cd);
1827 static ssize_t write_flush_pipefs(struct file *filp,
1828 const char __user *buf,
1829 size_t count, loff_t *ppos)
1831 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1833 return write_flush(filp, buf, count, ppos, cd);
1836 const struct file_operations cache_flush_operations_pipefs = {
1837 .open = open_flush_pipefs,
1838 .read = read_flush_pipefs,
1839 .write = write_flush_pipefs,
1840 .release = release_flush_pipefs,
1841 .llseek = no_llseek,
1844 int sunrpc_cache_register_pipefs(struct dentry *parent,
1845 const char *name, umode_t umode,
1846 struct cache_detail *cd)
1848 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1850 return PTR_ERR(dir);
1851 cd->u.pipefs.dir = dir;
1854 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1856 void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1858 rpc_remove_cache_dir(cd->u.pipefs.dir);
1859 cd->u.pipefs.dir = NULL;
1861 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);