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 schedule_delayed_work(&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);
370 if (cd->entries || atomic_read(&cd->inuse)) {
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 schedule_delayed_work(&cache_cleaner, delay);
489 * Clean all caches promptly. This just calls cache_clean
490 * repeatedly until we are sure that every cache has had a chance to
493 void cache_flush(void)
495 while (cache_clean() != -1)
497 while (cache_clean() != -1)
500 EXPORT_SYMBOL_GPL(cache_flush);
502 void cache_purge(struct cache_detail *detail)
504 time_t now = seconds_since_boot();
505 if (detail->flush_time >= now)
506 now = detail->flush_time + 1;
507 /* 'now' is the maximum value any 'last_refresh' can have */
508 detail->flush_time = now;
509 detail->nextcheck = seconds_since_boot();
512 EXPORT_SYMBOL_GPL(cache_purge);
516 * Deferral and Revisiting of Requests.
518 * If a cache lookup finds a pending entry, we
519 * need to defer the request and revisit it later.
520 * All deferred requests are stored in a hash table,
521 * indexed by "struct cache_head *".
522 * As it may be wasteful to store a whole request
523 * structure, we allow the request to provide a
524 * deferred form, which must contain a
525 * 'struct cache_deferred_req'
526 * This cache_deferred_req contains a method to allow
527 * it to be revisited when cache info is available
530 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
531 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
533 #define DFR_MAX 300 /* ??? */
535 static DEFINE_SPINLOCK(cache_defer_lock);
536 static LIST_HEAD(cache_defer_list);
537 static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
538 static int cache_defer_cnt;
540 static void __unhash_deferred_req(struct cache_deferred_req *dreq)
542 hlist_del_init(&dreq->hash);
543 if (!list_empty(&dreq->recent)) {
544 list_del_init(&dreq->recent);
549 static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
551 int hash = DFR_HASH(item);
553 INIT_LIST_HEAD(&dreq->recent);
554 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
557 static void setup_deferral(struct cache_deferred_req *dreq,
558 struct cache_head *item,
564 spin_lock(&cache_defer_lock);
566 __hash_deferred_req(dreq, item);
570 list_add(&dreq->recent, &cache_defer_list);
573 spin_unlock(&cache_defer_lock);
577 struct thread_deferred_req {
578 struct cache_deferred_req handle;
579 struct completion completion;
582 static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
584 struct thread_deferred_req *dr =
585 container_of(dreq, struct thread_deferred_req, handle);
586 complete(&dr->completion);
589 static void cache_wait_req(struct cache_req *req, struct cache_head *item)
591 struct thread_deferred_req sleeper;
592 struct cache_deferred_req *dreq = &sleeper.handle;
594 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
595 dreq->revisit = cache_restart_thread;
597 setup_deferral(dreq, item, 0);
599 if (!test_bit(CACHE_PENDING, &item->flags) ||
600 wait_for_completion_interruptible_timeout(
601 &sleeper.completion, req->thread_wait) <= 0) {
602 /* The completion wasn't completed, so we need
605 spin_lock(&cache_defer_lock);
606 if (!hlist_unhashed(&sleeper.handle.hash)) {
607 __unhash_deferred_req(&sleeper.handle);
608 spin_unlock(&cache_defer_lock);
610 /* cache_revisit_request already removed
611 * this from the hash table, but hasn't
612 * called ->revisit yet. It will very soon
613 * and we need to wait for it.
615 spin_unlock(&cache_defer_lock);
616 wait_for_completion(&sleeper.completion);
621 static void cache_limit_defers(void)
623 /* Make sure we haven't exceed the limit of allowed deferred
626 struct cache_deferred_req *discard = NULL;
628 if (cache_defer_cnt <= DFR_MAX)
631 spin_lock(&cache_defer_lock);
633 /* Consider removing either the first or the last */
634 if (cache_defer_cnt > DFR_MAX) {
635 if (prandom_u32() & 1)
636 discard = list_entry(cache_defer_list.next,
637 struct cache_deferred_req, recent);
639 discard = list_entry(cache_defer_list.prev,
640 struct cache_deferred_req, recent);
641 __unhash_deferred_req(discard);
643 spin_unlock(&cache_defer_lock);
645 discard->revisit(discard, 1);
648 /* Return true if and only if a deferred request is queued. */
649 static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
651 struct cache_deferred_req *dreq;
653 if (req->thread_wait) {
654 cache_wait_req(req, item);
655 if (!test_bit(CACHE_PENDING, &item->flags))
658 dreq = req->defer(req);
661 setup_deferral(dreq, item, 1);
662 if (!test_bit(CACHE_PENDING, &item->flags))
663 /* Bit could have been cleared before we managed to
664 * set up the deferral, so need to revisit just in case
666 cache_revisit_request(item);
668 cache_limit_defers();
672 static void cache_revisit_request(struct cache_head *item)
674 struct cache_deferred_req *dreq;
675 struct list_head pending;
676 struct hlist_node *tmp;
677 int hash = DFR_HASH(item);
679 INIT_LIST_HEAD(&pending);
680 spin_lock(&cache_defer_lock);
682 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
683 if (dreq->item == item) {
684 __unhash_deferred_req(dreq);
685 list_add(&dreq->recent, &pending);
688 spin_unlock(&cache_defer_lock);
690 while (!list_empty(&pending)) {
691 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
692 list_del_init(&dreq->recent);
693 dreq->revisit(dreq, 0);
697 void cache_clean_deferred(void *owner)
699 struct cache_deferred_req *dreq, *tmp;
700 struct list_head pending;
703 INIT_LIST_HEAD(&pending);
704 spin_lock(&cache_defer_lock);
706 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
707 if (dreq->owner == owner) {
708 __unhash_deferred_req(dreq);
709 list_add(&dreq->recent, &pending);
712 spin_unlock(&cache_defer_lock);
714 while (!list_empty(&pending)) {
715 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
716 list_del_init(&dreq->recent);
717 dreq->revisit(dreq, 1);
722 * communicate with user-space
724 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
725 * On read, you get a full request, or block.
726 * On write, an update request is processed.
727 * Poll works if anything to read, and always allows write.
729 * Implemented by linked list of requests. Each open file has
730 * a ->private that also exists in this list. New requests are added
731 * to the end and may wakeup and preceding readers.
732 * New readers are added to the head. If, on read, an item is found with
733 * CACHE_UPCALLING clear, we free it from the list.
737 static DEFINE_SPINLOCK(queue_lock);
738 static DEFINE_MUTEX(queue_io_mutex);
741 struct list_head list;
742 int reader; /* if 0, then request */
744 struct cache_request {
745 struct cache_queue q;
746 struct cache_head *item;
751 struct cache_reader {
752 struct cache_queue q;
753 int offset; /* if non-0, we have a refcnt on next request */
756 static int cache_request(struct cache_detail *detail,
757 struct cache_request *crq)
762 detail->cache_request(detail, crq->item, &bp, &len);
765 return PAGE_SIZE - len;
768 static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
769 loff_t *ppos, struct cache_detail *cd)
771 struct cache_reader *rp = filp->private_data;
772 struct cache_request *rq;
773 struct inode *inode = file_inode(filp);
779 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
780 * readers on this file */
782 spin_lock(&queue_lock);
783 /* need to find next request */
784 while (rp->q.list.next != &cd->queue &&
785 list_entry(rp->q.list.next, struct cache_queue, list)
787 struct list_head *next = rp->q.list.next;
788 list_move(&rp->q.list, next);
790 if (rp->q.list.next == &cd->queue) {
791 spin_unlock(&queue_lock);
792 mutex_unlock(&inode->i_mutex);
793 WARN_ON_ONCE(rp->offset);
796 rq = container_of(rp->q.list.next, struct cache_request, q.list);
797 WARN_ON_ONCE(rq->q.reader);
800 spin_unlock(&queue_lock);
803 err = cache_request(cd, rq);
809 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
811 spin_lock(&queue_lock);
812 list_move(&rp->q.list, &rq->q.list);
813 spin_unlock(&queue_lock);
815 if (rp->offset + count > rq->len)
816 count = rq->len - rp->offset;
818 if (copy_to_user(buf, rq->buf + rp->offset, count))
821 if (rp->offset >= rq->len) {
823 spin_lock(&queue_lock);
824 list_move(&rp->q.list, &rq->q.list);
825 spin_unlock(&queue_lock);
830 if (rp->offset == 0) {
831 /* need to release rq */
832 spin_lock(&queue_lock);
834 if (rq->readers == 0 &&
835 !test_bit(CACHE_PENDING, &rq->item->flags)) {
836 list_del(&rq->q.list);
837 spin_unlock(&queue_lock);
838 cache_put(rq->item, cd);
842 spin_unlock(&queue_lock);
846 mutex_unlock(&inode->i_mutex);
847 return err ? err : count;
850 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
851 size_t count, struct cache_detail *cd)
857 if (copy_from_user(kaddr, buf, count))
860 ret = cd->cache_parse(cd, kaddr, count);
866 static ssize_t cache_slow_downcall(const char __user *buf,
867 size_t count, struct cache_detail *cd)
869 static char write_buf[8192]; /* protected by queue_io_mutex */
870 ssize_t ret = -EINVAL;
872 if (count >= sizeof(write_buf))
874 mutex_lock(&queue_io_mutex);
875 ret = cache_do_downcall(write_buf, buf, count, cd);
876 mutex_unlock(&queue_io_mutex);
881 static ssize_t cache_downcall(struct address_space *mapping,
882 const char __user *buf,
883 size_t count, struct cache_detail *cd)
887 ssize_t ret = -ENOMEM;
889 if (count >= PAGE_CACHE_SIZE)
892 page = find_or_create_page(mapping, 0, GFP_KERNEL);
897 ret = cache_do_downcall(kaddr, buf, count, cd);
900 page_cache_release(page);
903 return cache_slow_downcall(buf, count, cd);
906 static ssize_t cache_write(struct file *filp, const char __user *buf,
907 size_t count, loff_t *ppos,
908 struct cache_detail *cd)
910 struct address_space *mapping = filp->f_mapping;
911 struct inode *inode = file_inode(filp);
912 ssize_t ret = -EINVAL;
914 if (!cd->cache_parse)
917 mutex_lock(&inode->i_mutex);
918 ret = cache_downcall(mapping, buf, count, cd);
919 mutex_unlock(&inode->i_mutex);
924 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
926 static unsigned int cache_poll(struct file *filp, poll_table *wait,
927 struct cache_detail *cd)
930 struct cache_reader *rp = filp->private_data;
931 struct cache_queue *cq;
933 poll_wait(filp, &queue_wait, wait);
935 /* alway allow write */
936 mask = POLLOUT | POLLWRNORM;
941 spin_lock(&queue_lock);
943 for (cq= &rp->q; &cq->list != &cd->queue;
944 cq = list_entry(cq->list.next, struct cache_queue, list))
946 mask |= POLLIN | POLLRDNORM;
949 spin_unlock(&queue_lock);
953 static int cache_ioctl(struct inode *ino, struct file *filp,
954 unsigned int cmd, unsigned long arg,
955 struct cache_detail *cd)
958 struct cache_reader *rp = filp->private_data;
959 struct cache_queue *cq;
961 if (cmd != FIONREAD || !rp)
964 spin_lock(&queue_lock);
966 /* only find the length remaining in current request,
967 * or the length of the next request
969 for (cq= &rp->q; &cq->list != &cd->queue;
970 cq = list_entry(cq->list.next, struct cache_queue, list))
972 struct cache_request *cr =
973 container_of(cq, struct cache_request, q);
974 len = cr->len - rp->offset;
977 spin_unlock(&queue_lock);
979 return put_user(len, (int __user *)arg);
982 static int cache_open(struct inode *inode, struct file *filp,
983 struct cache_detail *cd)
985 struct cache_reader *rp = NULL;
987 if (!cd || !try_module_get(cd->owner))
989 nonseekable_open(inode, filp);
990 if (filp->f_mode & FMODE_READ) {
991 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
993 module_put(cd->owner);
998 atomic_inc(&cd->readers);
999 spin_lock(&queue_lock);
1000 list_add(&rp->q.list, &cd->queue);
1001 spin_unlock(&queue_lock);
1003 filp->private_data = rp;
1007 static int cache_release(struct inode *inode, struct file *filp,
1008 struct cache_detail *cd)
1010 struct cache_reader *rp = filp->private_data;
1013 spin_lock(&queue_lock);
1015 struct cache_queue *cq;
1016 for (cq= &rp->q; &cq->list != &cd->queue;
1017 cq = list_entry(cq->list.next, struct cache_queue, list))
1019 container_of(cq, struct cache_request, q)
1025 list_del(&rp->q.list);
1026 spin_unlock(&queue_lock);
1028 filp->private_data = NULL;
1031 cd->last_close = seconds_since_boot();
1032 atomic_dec(&cd->readers);
1034 module_put(cd->owner);
1040 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
1042 struct cache_queue *cq, *tmp;
1043 struct cache_request *cr;
1044 struct list_head dequeued;
1046 INIT_LIST_HEAD(&dequeued);
1047 spin_lock(&queue_lock);
1048 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
1050 cr = container_of(cq, struct cache_request, q);
1053 if (test_bit(CACHE_PENDING, &ch->flags))
1054 /* Lost a race and it is pending again */
1056 if (cr->readers != 0)
1058 list_move(&cr->q.list, &dequeued);
1060 spin_unlock(&queue_lock);
1061 while (!list_empty(&dequeued)) {
1062 cr = list_entry(dequeued.next, struct cache_request, q.list);
1063 list_del(&cr->q.list);
1064 cache_put(cr->item, detail);
1071 * Support routines for text-based upcalls.
1072 * Fields are separated by spaces.
1073 * Fields are either mangled to quote space tab newline slosh with slosh
1074 * or a hexified with a leading \x
1075 * Record is terminated with newline.
1079 void qword_add(char **bpp, int *lp, char *str)
1085 if (len < 0) return;
1087 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1100 EXPORT_SYMBOL_GPL(qword_add);
1102 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1107 if (len < 0) return;
1113 while (blen && len >= 2) {
1114 bp = hex_byte_pack(bp, *buf++);
1119 if (blen || len<1) len = -1;
1127 EXPORT_SYMBOL_GPL(qword_addhex);
1129 static void warn_no_listener(struct cache_detail *detail)
1131 if (detail->last_warn != detail->last_close) {
1132 detail->last_warn = detail->last_close;
1133 if (detail->warn_no_listener)
1134 detail->warn_no_listener(detail, detail->last_close != 0);
1138 static bool cache_listeners_exist(struct cache_detail *detail)
1140 if (atomic_read(&detail->readers))
1142 if (detail->last_close == 0)
1143 /* This cache was never opened */
1145 if (detail->last_close < seconds_since_boot() - 30)
1147 * We allow for the possibility that someone might
1148 * restart a userspace daemon without restarting the
1149 * server; but after 30 seconds, we give up.
1156 * register an upcall request to user-space and queue it up for read() by the
1159 * Each request is at most one page long.
1161 int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
1165 struct cache_request *crq;
1168 if (!detail->cache_request)
1171 if (!cache_listeners_exist(detail)) {
1172 warn_no_listener(detail);
1175 if (test_bit(CACHE_CLEANED, &h->flags))
1176 /* Too late to make an upcall */
1179 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1183 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1193 spin_lock(&queue_lock);
1194 if (test_bit(CACHE_PENDING, &h->flags)) {
1195 crq->item = cache_get(h);
1196 list_add_tail(&crq->q.list, &detail->queue);
1198 /* Lost a race, no longer PENDING, so don't enqueue */
1200 spin_unlock(&queue_lock);
1201 wake_up(&queue_wait);
1202 if (ret == -EAGAIN) {
1208 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1211 * parse a message from user-space and pass it
1212 * to an appropriate cache
1213 * Messages are, like requests, separated into fields by
1214 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1217 * reply cachename expiry key ... content....
1219 * key and content are both parsed by cache
1222 int qword_get(char **bpp, char *dest, int bufsize)
1224 /* return bytes copied, or -1 on error */
1228 while (*bp == ' ') bp++;
1230 if (bp[0] == '\\' && bp[1] == 'x') {
1233 while (len < bufsize - 1) {
1236 h = hex_to_bin(bp[0]);
1240 l = hex_to_bin(bp[1]);
1244 *dest++ = (h << 4) | l;
1249 /* text with \nnn octal quoting */
1250 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1252 isodigit(bp[1]) && (bp[1] <= '3') &&
1255 int byte = (*++bp -'0');
1257 byte = (byte << 3) | (*bp++ - '0');
1258 byte = (byte << 3) | (*bp++ - '0');
1268 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1270 while (*bp == ' ') bp++;
1275 EXPORT_SYMBOL_GPL(qword_get);
1279 * support /proc/sunrpc/cache/$CACHENAME/content
1281 * We call ->cache_show passing NULL for the item to
1282 * get a header, then pass each real item in the cache
1285 void *cache_seq_start(struct seq_file *m, loff_t *pos)
1286 __acquires(cd->hash_lock)
1289 unsigned int hash, entry;
1290 struct cache_head *ch;
1291 struct cache_detail *cd = m->private;
1293 read_lock(&cd->hash_lock);
1295 return SEQ_START_TOKEN;
1297 entry = n & ((1LL<<32) - 1);
1299 hlist_for_each_entry(ch, &cd->hash_table[hash], cache_list)
1302 n &= ~((1LL<<32) - 1);
1306 } while(hash < cd->hash_size &&
1307 hlist_empty(&cd->hash_table[hash]));
1308 if (hash >= cd->hash_size)
1311 return hlist_entry_safe(cd->hash_table[hash].first,
1312 struct cache_head, cache_list);
1314 EXPORT_SYMBOL_GPL(cache_seq_start);
1316 void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
1318 struct cache_head *ch = p;
1319 int hash = (*pos >> 32);
1320 struct cache_detail *cd = m->private;
1322 if (p == SEQ_START_TOKEN)
1324 else if (ch->cache_list.next == NULL) {
1329 return hlist_entry_safe(ch->cache_list.next,
1330 struct cache_head, cache_list);
1332 *pos &= ~((1LL<<32) - 1);
1333 while (hash < cd->hash_size &&
1334 hlist_empty(&cd->hash_table[hash])) {
1338 if (hash >= cd->hash_size)
1341 return hlist_entry_safe(cd->hash_table[hash].first,
1342 struct cache_head, cache_list);
1344 EXPORT_SYMBOL_GPL(cache_seq_next);
1346 void cache_seq_stop(struct seq_file *m, void *p)
1347 __releases(cd->hash_lock)
1349 struct cache_detail *cd = m->private;
1350 read_unlock(&cd->hash_lock);
1352 EXPORT_SYMBOL_GPL(cache_seq_stop);
1354 static int c_show(struct seq_file *m, void *p)
1356 struct cache_head *cp = p;
1357 struct cache_detail *cd = m->private;
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 = {
1380 .start = cache_seq_start,
1381 .next = cache_seq_next,
1382 .stop = cache_seq_stop,
1386 static int content_open(struct inode *inode, struct file *file,
1387 struct cache_detail *cd)
1389 struct seq_file *seq;
1392 if (!cd || !try_module_get(cd->owner))
1395 err = seq_open(file, &cache_content_op);
1397 module_put(cd->owner);
1401 seq = file->private_data;
1406 static int content_release(struct inode *inode, struct file *file,
1407 struct cache_detail *cd)
1409 int ret = seq_release(inode, file);
1410 module_put(cd->owner);
1414 static int open_flush(struct inode *inode, struct file *file,
1415 struct cache_detail *cd)
1417 if (!cd || !try_module_get(cd->owner))
1419 return nonseekable_open(inode, file);
1422 static int release_flush(struct inode *inode, struct file *file,
1423 struct cache_detail *cd)
1425 module_put(cd->owner);
1429 static ssize_t read_flush(struct file *file, char __user *buf,
1430 size_t count, loff_t *ppos,
1431 struct cache_detail *cd)
1434 unsigned long p = *ppos;
1437 snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
1444 if (copy_to_user(buf, (void*)(tbuf+p), len))
1450 static ssize_t write_flush(struct file *file, const char __user *buf,
1451 size_t count, loff_t *ppos,
1452 struct cache_detail *cd)
1458 if (*ppos || count > sizeof(tbuf)-1)
1460 if (copy_from_user(tbuf, buf, count))
1463 simple_strtoul(tbuf, &ep, 0);
1464 if (*ep && *ep != '\n')
1468 then = get_expiry(&bp);
1469 now = seconds_since_boot();
1470 cd->nextcheck = now;
1471 /* Can only set flush_time to 1 second beyond "now", or
1472 * possibly 1 second beyond flushtime. This is because
1473 * flush_time never goes backwards so it mustn't get too far
1477 /* Want to flush everything, so behave like cache_purge() */
1478 if (cd->flush_time >= now)
1479 now = cd->flush_time + 1;
1483 cd->flush_time = then;
1490 static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1491 size_t count, loff_t *ppos)
1493 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1495 return cache_read(filp, buf, count, ppos, cd);
1498 static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1499 size_t count, loff_t *ppos)
1501 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1503 return cache_write(filp, buf, count, ppos, cd);
1506 static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1508 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1510 return cache_poll(filp, wait, cd);
1513 static long cache_ioctl_procfs(struct file *filp,
1514 unsigned int cmd, unsigned long arg)
1516 struct inode *inode = file_inode(filp);
1517 struct cache_detail *cd = PDE_DATA(inode);
1519 return cache_ioctl(inode, filp, cmd, arg, cd);
1522 static int cache_open_procfs(struct inode *inode, struct file *filp)
1524 struct cache_detail *cd = PDE_DATA(inode);
1526 return cache_open(inode, filp, cd);
1529 static int cache_release_procfs(struct inode *inode, struct file *filp)
1531 struct cache_detail *cd = PDE_DATA(inode);
1533 return cache_release(inode, filp, cd);
1536 static const struct file_operations cache_file_operations_procfs = {
1537 .owner = THIS_MODULE,
1538 .llseek = no_llseek,
1539 .read = cache_read_procfs,
1540 .write = cache_write_procfs,
1541 .poll = cache_poll_procfs,
1542 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
1543 .open = cache_open_procfs,
1544 .release = cache_release_procfs,
1547 static int content_open_procfs(struct inode *inode, struct file *filp)
1549 struct cache_detail *cd = PDE_DATA(inode);
1551 return content_open(inode, filp, cd);
1554 static int content_release_procfs(struct inode *inode, struct file *filp)
1556 struct cache_detail *cd = PDE_DATA(inode);
1558 return content_release(inode, filp, cd);
1561 static const struct file_operations content_file_operations_procfs = {
1562 .open = content_open_procfs,
1564 .llseek = seq_lseek,
1565 .release = content_release_procfs,
1568 static int open_flush_procfs(struct inode *inode, struct file *filp)
1570 struct cache_detail *cd = PDE_DATA(inode);
1572 return open_flush(inode, filp, cd);
1575 static int release_flush_procfs(struct inode *inode, struct file *filp)
1577 struct cache_detail *cd = PDE_DATA(inode);
1579 return release_flush(inode, filp, cd);
1582 static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1583 size_t count, loff_t *ppos)
1585 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1587 return read_flush(filp, buf, count, ppos, cd);
1590 static ssize_t write_flush_procfs(struct file *filp,
1591 const char __user *buf,
1592 size_t count, loff_t *ppos)
1594 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1596 return write_flush(filp, buf, count, ppos, cd);
1599 static const struct file_operations cache_flush_operations_procfs = {
1600 .open = open_flush_procfs,
1601 .read = read_flush_procfs,
1602 .write = write_flush_procfs,
1603 .release = release_flush_procfs,
1604 .llseek = no_llseek,
1607 static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
1609 struct sunrpc_net *sn;
1611 if (cd->u.procfs.proc_ent == NULL)
1613 if (cd->u.procfs.flush_ent)
1614 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1615 if (cd->u.procfs.channel_ent)
1616 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1617 if (cd->u.procfs.content_ent)
1618 remove_proc_entry("content", cd->u.procfs.proc_ent);
1619 cd->u.procfs.proc_ent = NULL;
1620 sn = net_generic(net, sunrpc_net_id);
1621 remove_proc_entry(cd->name, sn->proc_net_rpc);
1624 #ifdef CONFIG_PROC_FS
1625 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1627 struct proc_dir_entry *p;
1628 struct sunrpc_net *sn;
1630 sn = net_generic(net, sunrpc_net_id);
1631 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
1632 if (cd->u.procfs.proc_ent == NULL)
1634 cd->u.procfs.channel_ent = NULL;
1635 cd->u.procfs.content_ent = NULL;
1637 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1638 cd->u.procfs.proc_ent,
1639 &cache_flush_operations_procfs, cd);
1640 cd->u.procfs.flush_ent = p;
1644 if (cd->cache_request || cd->cache_parse) {
1645 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1646 cd->u.procfs.proc_ent,
1647 &cache_file_operations_procfs, cd);
1648 cd->u.procfs.channel_ent = p;
1652 if (cd->cache_show) {
1653 p = proc_create_data("content", S_IFREG|S_IRUSR,
1654 cd->u.procfs.proc_ent,
1655 &content_file_operations_procfs, cd);
1656 cd->u.procfs.content_ent = p;
1662 remove_cache_proc_entries(cd, net);
1665 #else /* CONFIG_PROC_FS */
1666 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1672 void __init cache_initialize(void)
1674 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
1677 int cache_register_net(struct cache_detail *cd, struct net *net)
1681 sunrpc_init_cache_detail(cd);
1682 ret = create_cache_proc_entries(cd, net);
1684 sunrpc_destroy_cache_detail(cd);
1687 EXPORT_SYMBOL_GPL(cache_register_net);
1689 void cache_unregister_net(struct cache_detail *cd, struct net *net)
1691 remove_cache_proc_entries(cd, net);
1692 sunrpc_destroy_cache_detail(cd);
1694 EXPORT_SYMBOL_GPL(cache_unregister_net);
1696 struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
1698 struct cache_detail *cd;
1701 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1703 return ERR_PTR(-ENOMEM);
1705 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct hlist_head),
1707 if (cd->hash_table == NULL) {
1709 return ERR_PTR(-ENOMEM);
1712 for (i = 0; i < cd->hash_size; i++)
1713 INIT_HLIST_HEAD(&cd->hash_table[i]);
1717 EXPORT_SYMBOL_GPL(cache_create_net);
1719 void cache_destroy_net(struct cache_detail *cd, struct net *net)
1721 kfree(cd->hash_table);
1724 EXPORT_SYMBOL_GPL(cache_destroy_net);
1726 static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1727 size_t count, loff_t *ppos)
1729 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1731 return cache_read(filp, buf, count, ppos, cd);
1734 static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1735 size_t count, loff_t *ppos)
1737 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1739 return cache_write(filp, buf, count, ppos, cd);
1742 static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1744 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1746 return cache_poll(filp, wait, cd);
1749 static long cache_ioctl_pipefs(struct file *filp,
1750 unsigned int cmd, unsigned long arg)
1752 struct inode *inode = file_inode(filp);
1753 struct cache_detail *cd = RPC_I(inode)->private;
1755 return cache_ioctl(inode, filp, cmd, arg, cd);
1758 static int cache_open_pipefs(struct inode *inode, struct file *filp)
1760 struct cache_detail *cd = RPC_I(inode)->private;
1762 return cache_open(inode, filp, cd);
1765 static int cache_release_pipefs(struct inode *inode, struct file *filp)
1767 struct cache_detail *cd = RPC_I(inode)->private;
1769 return cache_release(inode, filp, cd);
1772 const struct file_operations cache_file_operations_pipefs = {
1773 .owner = THIS_MODULE,
1774 .llseek = no_llseek,
1775 .read = cache_read_pipefs,
1776 .write = cache_write_pipefs,
1777 .poll = cache_poll_pipefs,
1778 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
1779 .open = cache_open_pipefs,
1780 .release = cache_release_pipefs,
1783 static int content_open_pipefs(struct inode *inode, struct file *filp)
1785 struct cache_detail *cd = RPC_I(inode)->private;
1787 return content_open(inode, filp, cd);
1790 static int content_release_pipefs(struct inode *inode, struct file *filp)
1792 struct cache_detail *cd = RPC_I(inode)->private;
1794 return content_release(inode, filp, cd);
1797 const struct file_operations content_file_operations_pipefs = {
1798 .open = content_open_pipefs,
1800 .llseek = seq_lseek,
1801 .release = content_release_pipefs,
1804 static int open_flush_pipefs(struct inode *inode, struct file *filp)
1806 struct cache_detail *cd = RPC_I(inode)->private;
1808 return open_flush(inode, filp, cd);
1811 static int release_flush_pipefs(struct inode *inode, struct file *filp)
1813 struct cache_detail *cd = RPC_I(inode)->private;
1815 return release_flush(inode, filp, cd);
1818 static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1819 size_t count, loff_t *ppos)
1821 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1823 return read_flush(filp, buf, count, ppos, cd);
1826 static ssize_t write_flush_pipefs(struct file *filp,
1827 const char __user *buf,
1828 size_t count, loff_t *ppos)
1830 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1832 return write_flush(filp, buf, count, ppos, cd);
1835 const struct file_operations cache_flush_operations_pipefs = {
1836 .open = open_flush_pipefs,
1837 .read = read_flush_pipefs,
1838 .write = write_flush_pipefs,
1839 .release = release_flush_pipefs,
1840 .llseek = no_llseek,
1843 int sunrpc_cache_register_pipefs(struct dentry *parent,
1844 const char *name, umode_t umode,
1845 struct cache_detail *cd)
1847 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1849 return PTR_ERR(dir);
1850 cd->u.pipefs.dir = dir;
1853 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1855 void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1857 rpc_remove_cache_dir(cd->u.pipefs.dir);
1858 cd->u.pipefs.dir = NULL;
1860 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);