4 * Generic code for various authentication-related caches
5 * used by sunrpc clients and servers.
7 * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
9 * Released under terms in GPL version 2. See COPYING.
13 #include <linux/types.h>
15 #include <linux/file.h>
16 #include <linux/slab.h>
17 #include <linux/signal.h>
18 #include <linux/sched.h>
19 #include <linux/kmod.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/ctype.h>
23 #include <asm/uaccess.h>
24 #include <linux/poll.h>
25 #include <linux/seq_file.h>
26 #include <linux/proc_fs.h>
27 #include <linux/net.h>
28 #include <linux/workqueue.h>
29 #include <linux/mutex.h>
30 #include <linux/pagemap.h>
31 #include <asm/ioctls.h>
32 #include <linux/sunrpc/types.h>
33 #include <linux/sunrpc/cache.h>
34 #include <linux/sunrpc/stats.h>
36 #define RPCDBG_FACILITY RPCDBG_CACHE
38 static int cache_defer_req(struct cache_req *req, struct cache_head *item);
39 static void cache_revisit_request(struct cache_head *item);
41 static void cache_init(struct cache_head *h)
43 time_t now = get_seconds();
47 h->expiry_time = now + CACHE_NEW_EXPIRY;
48 h->last_refresh = now;
51 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
52 struct cache_head *key, int hash)
54 struct cache_head **head, **hp;
55 struct cache_head *new = NULL;
57 head = &detail->hash_table[hash];
59 read_lock(&detail->hash_lock);
61 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
62 struct cache_head *tmp = *hp;
63 if (detail->match(tmp, key)) {
65 read_unlock(&detail->hash_lock);
69 read_unlock(&detail->hash_lock);
70 /* Didn't find anything, insert an empty entry */
72 new = detail->alloc();
75 /* must fully initialise 'new', else
76 * we might get lose if we need to
80 detail->init(new, key);
82 write_lock(&detail->hash_lock);
84 /* check if entry appeared while we slept */
85 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
86 struct cache_head *tmp = *hp;
87 if (detail->match(tmp, key)) {
89 write_unlock(&detail->hash_lock);
90 cache_put(new, detail);
98 write_unlock(&detail->hash_lock);
102 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
105 static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
107 static int cache_fresh_locked(struct cache_head *head, time_t expiry)
109 head->expiry_time = expiry;
110 head->last_refresh = get_seconds();
111 return !test_and_set_bit(CACHE_VALID, &head->flags);
114 static void cache_fresh_unlocked(struct cache_head *head,
115 struct cache_detail *detail, int new)
118 cache_revisit_request(head);
119 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
120 cache_revisit_request(head);
121 queue_loose(detail, head);
125 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
126 struct cache_head *new, struct cache_head *old, int hash)
128 /* The 'old' entry is to be replaced by 'new'.
129 * If 'old' is not VALID, we update it directly,
130 * otherwise we need to replace it
132 struct cache_head **head;
133 struct cache_head *tmp;
136 if (!test_bit(CACHE_VALID, &old->flags)) {
137 write_lock(&detail->hash_lock);
138 if (!test_bit(CACHE_VALID, &old->flags)) {
139 if (test_bit(CACHE_NEGATIVE, &new->flags))
140 set_bit(CACHE_NEGATIVE, &old->flags);
142 detail->update(old, new);
143 is_new = cache_fresh_locked(old, new->expiry_time);
144 write_unlock(&detail->hash_lock);
145 cache_fresh_unlocked(old, detail, is_new);
148 write_unlock(&detail->hash_lock);
150 /* We need to insert a new entry */
151 tmp = detail->alloc();
153 cache_put(old, detail);
157 detail->init(tmp, old);
158 head = &detail->hash_table[hash];
160 write_lock(&detail->hash_lock);
161 if (test_bit(CACHE_NEGATIVE, &new->flags))
162 set_bit(CACHE_NEGATIVE, &tmp->flags);
164 detail->update(tmp, new);
169 is_new = cache_fresh_locked(tmp, new->expiry_time);
170 cache_fresh_locked(old, 0);
171 write_unlock(&detail->hash_lock);
172 cache_fresh_unlocked(tmp, detail, is_new);
173 cache_fresh_unlocked(old, detail, 0);
174 cache_put(old, detail);
177 EXPORT_SYMBOL_GPL(sunrpc_cache_update);
179 static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
181 if (!cd->cache_upcall)
183 return cd->cache_upcall(cd, h);
187 * This is the generic cache management routine for all
188 * the authentication caches.
189 * It checks the currency of a cache item and will (later)
190 * initiate an upcall to fill it if needed.
193 * Returns 0 if the cache_head can be used, or cache_puts it and returns
194 * -EAGAIN if upcall is pending,
195 * -ETIMEDOUT if upcall failed and should be retried,
196 * -ENOENT if cache entry was negative
198 int cache_check(struct cache_detail *detail,
199 struct cache_head *h, struct cache_req *rqstp)
202 long refresh_age, age;
204 /* First decide return status as best we can */
205 if (!test_bit(CACHE_VALID, &h->flags) ||
206 h->expiry_time < get_seconds())
208 else if (detail->flush_time > h->last_refresh)
212 if (test_bit(CACHE_NEGATIVE, &h->flags))
217 /* now see if we want to start an upcall */
218 refresh_age = (h->expiry_time - h->last_refresh);
219 age = get_seconds() - h->last_refresh;
224 } else if (rv == -EAGAIN || age > refresh_age/2) {
225 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
227 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
228 switch (cache_make_upcall(detail, h)) {
230 clear_bit(CACHE_PENDING, &h->flags);
232 set_bit(CACHE_NEGATIVE, &h->flags);
233 cache_fresh_unlocked(h, detail,
234 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY));
240 clear_bit(CACHE_PENDING, &h->flags);
241 cache_revisit_request(h);
248 if (cache_defer_req(rqstp, h) != 0)
252 cache_put(h, detail);
255 EXPORT_SYMBOL_GPL(cache_check);
258 * caches need to be periodically cleaned.
259 * For this we maintain a list of cache_detail and
260 * a current pointer into that list and into the table
263 * Each time clean_cache is called it finds the next non-empty entry
264 * in the current table and walks the list in that entry
265 * looking for entries that can be removed.
267 * An entry gets removed if:
268 * - The expiry is before current time
269 * - The last_refresh time is before the flush_time for that cache
271 * later we might drop old entries with non-NEVER expiry if that table
272 * is getting 'full' for some definition of 'full'
274 * The question of "how often to scan a table" is an interesting one
275 * and is answered in part by the use of the "nextcheck" field in the
277 * When a scan of a table begins, the nextcheck field is set to a time
278 * that is well into the future.
279 * While scanning, if an expiry time is found that is earlier than the
280 * current nextcheck time, nextcheck is set to that expiry time.
281 * If the flush_time is ever set to a time earlier than the nextcheck
282 * time, the nextcheck time is then set to that flush_time.
284 * A table is then only scanned if the current time is at least
285 * the nextcheck time.
289 static LIST_HEAD(cache_list);
290 static DEFINE_SPINLOCK(cache_list_lock);
291 static struct cache_detail *current_detail;
292 static int current_index;
294 static const struct file_operations cache_file_operations;
295 static const struct file_operations content_file_operations;
296 static const struct file_operations cache_flush_operations;
298 static void do_cache_clean(struct work_struct *work);
299 static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
301 static void remove_cache_proc_entries(struct cache_detail *cd)
303 if (cd->proc_ent == NULL)
306 remove_proc_entry("flush", cd->proc_ent);
308 remove_proc_entry("channel", cd->proc_ent);
310 remove_proc_entry("content", cd->proc_ent);
312 remove_proc_entry(cd->name, proc_net_rpc);
315 #ifdef CONFIG_PROC_FS
316 static int create_cache_proc_entries(struct cache_detail *cd)
318 struct proc_dir_entry *p;
320 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
321 if (cd->proc_ent == NULL)
323 cd->channel_ent = cd->content_ent = NULL;
325 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
326 cd->proc_ent, &cache_flush_operations, cd);
331 if (cd->cache_upcall || cd->cache_parse) {
332 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
333 cd->proc_ent, &cache_file_operations, cd);
338 if (cd->cache_show) {
339 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
340 cd->proc_ent, &content_file_operations, cd);
347 remove_cache_proc_entries(cd);
350 #else /* CONFIG_PROC_FS */
351 static int create_cache_proc_entries(struct cache_detail *cd)
357 static void sunrpc_init_cache_detail(struct cache_detail *cd)
359 rwlock_init(&cd->hash_lock);
360 INIT_LIST_HEAD(&cd->queue);
361 spin_lock(&cache_list_lock);
364 atomic_set(&cd->readers, 0);
367 list_add(&cd->others, &cache_list);
368 spin_unlock(&cache_list_lock);
370 /* start the cleaning process */
371 schedule_delayed_work(&cache_cleaner, 0);
374 static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
377 spin_lock(&cache_list_lock);
378 write_lock(&cd->hash_lock);
379 if (cd->entries || atomic_read(&cd->inuse)) {
380 write_unlock(&cd->hash_lock);
381 spin_unlock(&cache_list_lock);
384 if (current_detail == cd)
385 current_detail = NULL;
386 list_del_init(&cd->others);
387 write_unlock(&cd->hash_lock);
388 spin_unlock(&cache_list_lock);
389 if (list_empty(&cache_list)) {
390 /* module must be being unloaded so its safe to kill the worker */
391 cancel_delayed_work_sync(&cache_cleaner);
395 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
398 int cache_register(struct cache_detail *cd)
402 sunrpc_init_cache_detail(cd);
403 ret = create_cache_proc_entries(cd);
405 sunrpc_destroy_cache_detail(cd);
408 EXPORT_SYMBOL_GPL(cache_register);
410 void cache_unregister(struct cache_detail *cd)
412 remove_cache_proc_entries(cd);
413 sunrpc_destroy_cache_detail(cd);
415 EXPORT_SYMBOL_GPL(cache_unregister);
417 /* clean cache tries to find something to clean
419 * It returns 1 if it cleaned something,
420 * 0 if it didn't find anything this time
421 * -1 if it fell off the end of the list.
423 static int cache_clean(void)
426 struct list_head *next;
428 spin_lock(&cache_list_lock);
430 /* find a suitable table if we don't already have one */
431 while (current_detail == NULL ||
432 current_index >= current_detail->hash_size) {
434 next = current_detail->others.next;
436 next = cache_list.next;
437 if (next == &cache_list) {
438 current_detail = NULL;
439 spin_unlock(&cache_list_lock);
442 current_detail = list_entry(next, struct cache_detail, others);
443 if (current_detail->nextcheck > get_seconds())
444 current_index = current_detail->hash_size;
447 current_detail->nextcheck = get_seconds()+30*60;
451 /* find a non-empty bucket in the table */
452 while (current_detail &&
453 current_index < current_detail->hash_size &&
454 current_detail->hash_table[current_index] == NULL)
457 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
459 if (current_detail && current_index < current_detail->hash_size) {
460 struct cache_head *ch, **cp;
461 struct cache_detail *d;
463 write_lock(¤t_detail->hash_lock);
465 /* Ok, now to clean this strand */
467 cp = & current_detail->hash_table[current_index];
469 for (; ch; cp= & ch->next, ch= *cp) {
470 if (current_detail->nextcheck > ch->expiry_time)
471 current_detail->nextcheck = ch->expiry_time+1;
472 if (ch->expiry_time >= get_seconds()
473 && ch->last_refresh >= current_detail->flush_time
476 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
477 queue_loose(current_detail, ch);
479 if (atomic_read(&ch->ref.refcount) == 1)
485 current_detail->entries--;
488 write_unlock(¤t_detail->hash_lock);
492 spin_unlock(&cache_list_lock);
496 spin_unlock(&cache_list_lock);
502 * We want to regularly clean the cache, so we need to schedule some work ...
504 static void do_cache_clean(struct work_struct *work)
507 if (cache_clean() == -1)
508 delay = round_jiffies_relative(30*HZ);
510 if (list_empty(&cache_list))
514 schedule_delayed_work(&cache_cleaner, delay);
519 * Clean all caches promptly. This just calls cache_clean
520 * repeatedly until we are sure that every cache has had a chance to
523 void cache_flush(void)
525 while (cache_clean() != -1)
527 while (cache_clean() != -1)
530 EXPORT_SYMBOL_GPL(cache_flush);
532 void cache_purge(struct cache_detail *detail)
534 detail->flush_time = LONG_MAX;
535 detail->nextcheck = get_seconds();
537 detail->flush_time = 1;
539 EXPORT_SYMBOL_GPL(cache_purge);
543 * Deferral and Revisiting of Requests.
545 * If a cache lookup finds a pending entry, we
546 * need to defer the request and revisit it later.
547 * All deferred requests are stored in a hash table,
548 * indexed by "struct cache_head *".
549 * As it may be wasteful to store a whole request
550 * structure, we allow the request to provide a
551 * deferred form, which must contain a
552 * 'struct cache_deferred_req'
553 * This cache_deferred_req contains a method to allow
554 * it to be revisited when cache info is available
557 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
558 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
560 #define DFR_MAX 300 /* ??? */
562 static DEFINE_SPINLOCK(cache_defer_lock);
563 static LIST_HEAD(cache_defer_list);
564 static struct list_head cache_defer_hash[DFR_HASHSIZE];
565 static int cache_defer_cnt;
567 static int cache_defer_req(struct cache_req *req, struct cache_head *item)
569 struct cache_deferred_req *dreq;
570 int hash = DFR_HASH(item);
572 if (cache_defer_cnt >= DFR_MAX) {
573 /* too much in the cache, randomly drop this one,
574 * or continue and drop the oldest below
579 dreq = req->defer(req);
585 spin_lock(&cache_defer_lock);
587 list_add(&dreq->recent, &cache_defer_list);
589 if (cache_defer_hash[hash].next == NULL)
590 INIT_LIST_HEAD(&cache_defer_hash[hash]);
591 list_add(&dreq->hash, &cache_defer_hash[hash]);
593 /* it is in, now maybe clean up */
595 if (++cache_defer_cnt > DFR_MAX) {
596 dreq = list_entry(cache_defer_list.prev,
597 struct cache_deferred_req, recent);
598 list_del(&dreq->recent);
599 list_del(&dreq->hash);
602 spin_unlock(&cache_defer_lock);
605 /* there was one too many */
606 dreq->revisit(dreq, 1);
608 if (!test_bit(CACHE_PENDING, &item->flags)) {
609 /* must have just been validated... */
610 cache_revisit_request(item);
615 static void cache_revisit_request(struct cache_head *item)
617 struct cache_deferred_req *dreq;
618 struct list_head pending;
620 struct list_head *lp;
621 int hash = DFR_HASH(item);
623 INIT_LIST_HEAD(&pending);
624 spin_lock(&cache_defer_lock);
626 lp = cache_defer_hash[hash].next;
628 while (lp != &cache_defer_hash[hash]) {
629 dreq = list_entry(lp, struct cache_deferred_req, hash);
631 if (dreq->item == item) {
632 list_del(&dreq->hash);
633 list_move(&dreq->recent, &pending);
638 spin_unlock(&cache_defer_lock);
640 while (!list_empty(&pending)) {
641 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
642 list_del_init(&dreq->recent);
643 dreq->revisit(dreq, 0);
647 void cache_clean_deferred(void *owner)
649 struct cache_deferred_req *dreq, *tmp;
650 struct list_head pending;
653 INIT_LIST_HEAD(&pending);
654 spin_lock(&cache_defer_lock);
656 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
657 if (dreq->owner == owner) {
658 list_del(&dreq->hash);
659 list_move(&dreq->recent, &pending);
663 spin_unlock(&cache_defer_lock);
665 while (!list_empty(&pending)) {
666 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
667 list_del_init(&dreq->recent);
668 dreq->revisit(dreq, 1);
673 * communicate with user-space
675 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
676 * On read, you get a full request, or block.
677 * On write, an update request is processed.
678 * Poll works if anything to read, and always allows write.
680 * Implemented by linked list of requests. Each open file has
681 * a ->private that also exists in this list. New requests are added
682 * to the end and may wakeup and preceding readers.
683 * New readers are added to the head. If, on read, an item is found with
684 * CACHE_UPCALLING clear, we free it from the list.
688 static DEFINE_SPINLOCK(queue_lock);
689 static DEFINE_MUTEX(queue_io_mutex);
692 struct list_head list;
693 int reader; /* if 0, then request */
695 struct cache_request {
696 struct cache_queue q;
697 struct cache_head *item;
702 struct cache_reader {
703 struct cache_queue q;
704 int offset; /* if non-0, we have a refcnt on next request */
708 cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
710 struct cache_reader *rp = filp->private_data;
711 struct cache_request *rq;
712 struct inode *inode = filp->f_path.dentry->d_inode;
713 struct cache_detail *cd = PDE(inode)->data;
719 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
720 * readers on this file */
722 spin_lock(&queue_lock);
723 /* need to find next request */
724 while (rp->q.list.next != &cd->queue &&
725 list_entry(rp->q.list.next, struct cache_queue, list)
727 struct list_head *next = rp->q.list.next;
728 list_move(&rp->q.list, next);
730 if (rp->q.list.next == &cd->queue) {
731 spin_unlock(&queue_lock);
732 mutex_unlock(&inode->i_mutex);
736 rq = container_of(rp->q.list.next, struct cache_request, q.list);
737 BUG_ON(rq->q.reader);
740 spin_unlock(&queue_lock);
742 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
744 spin_lock(&queue_lock);
745 list_move(&rp->q.list, &rq->q.list);
746 spin_unlock(&queue_lock);
748 if (rp->offset + count > rq->len)
749 count = rq->len - rp->offset;
751 if (copy_to_user(buf, rq->buf + rp->offset, count))
754 if (rp->offset >= rq->len) {
756 spin_lock(&queue_lock);
757 list_move(&rp->q.list, &rq->q.list);
758 spin_unlock(&queue_lock);
763 if (rp->offset == 0) {
764 /* need to release rq */
765 spin_lock(&queue_lock);
767 if (rq->readers == 0 &&
768 !test_bit(CACHE_PENDING, &rq->item->flags)) {
769 list_del(&rq->q.list);
770 spin_unlock(&queue_lock);
771 cache_put(rq->item, cd);
775 spin_unlock(&queue_lock);
779 mutex_unlock(&inode->i_mutex);
780 return err ? err : count;
783 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
784 size_t count, struct cache_detail *cd)
788 if (copy_from_user(kaddr, buf, count))
791 ret = cd->cache_parse(cd, kaddr, count);
797 static ssize_t cache_slow_downcall(const char __user *buf,
798 size_t count, struct cache_detail *cd)
800 static char write_buf[8192]; /* protected by queue_io_mutex */
801 ssize_t ret = -EINVAL;
803 if (count >= sizeof(write_buf))
805 mutex_lock(&queue_io_mutex);
806 ret = cache_do_downcall(write_buf, buf, count, cd);
807 mutex_unlock(&queue_io_mutex);
812 static ssize_t cache_downcall(struct address_space *mapping,
813 const char __user *buf,
814 size_t count, struct cache_detail *cd)
818 ssize_t ret = -ENOMEM;
820 if (count >= PAGE_CACHE_SIZE)
823 page = find_or_create_page(mapping, 0, GFP_KERNEL);
828 ret = cache_do_downcall(kaddr, buf, count, cd);
831 page_cache_release(page);
834 return cache_slow_downcall(buf, count, cd);
838 cache_write(struct file *filp, const char __user *buf, size_t count,
841 struct address_space *mapping = filp->f_mapping;
842 struct inode *inode = filp->f_path.dentry->d_inode;
843 struct cache_detail *cd = PDE(inode)->data;
844 ssize_t ret = -EINVAL;
846 if (!cd->cache_parse)
849 mutex_lock(&inode->i_mutex);
850 ret = cache_downcall(mapping, buf, count, cd);
851 mutex_unlock(&inode->i_mutex);
856 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
859 cache_poll(struct file *filp, poll_table *wait)
862 struct cache_reader *rp = filp->private_data;
863 struct cache_queue *cq;
864 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
866 poll_wait(filp, &queue_wait, wait);
868 /* alway allow write */
869 mask = POLL_OUT | POLLWRNORM;
874 spin_lock(&queue_lock);
876 for (cq= &rp->q; &cq->list != &cd->queue;
877 cq = list_entry(cq->list.next, struct cache_queue, list))
879 mask |= POLLIN | POLLRDNORM;
882 spin_unlock(&queue_lock);
887 cache_ioctl(struct inode *ino, struct file *filp,
888 unsigned int cmd, unsigned long arg)
891 struct cache_reader *rp = filp->private_data;
892 struct cache_queue *cq;
893 struct cache_detail *cd = PDE(ino)->data;
895 if (cmd != FIONREAD || !rp)
898 spin_lock(&queue_lock);
900 /* only find the length remaining in current request,
901 * or the length of the next request
903 for (cq= &rp->q; &cq->list != &cd->queue;
904 cq = list_entry(cq->list.next, struct cache_queue, list))
906 struct cache_request *cr =
907 container_of(cq, struct cache_request, q);
908 len = cr->len - rp->offset;
911 spin_unlock(&queue_lock);
913 return put_user(len, (int __user *)arg);
917 cache_open(struct inode *inode, struct file *filp)
919 struct cache_reader *rp = NULL;
921 nonseekable_open(inode, filp);
922 if (filp->f_mode & FMODE_READ) {
923 struct cache_detail *cd = PDE(inode)->data;
925 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
930 atomic_inc(&cd->readers);
931 spin_lock(&queue_lock);
932 list_add(&rp->q.list, &cd->queue);
933 spin_unlock(&queue_lock);
935 filp->private_data = rp;
940 cache_release(struct inode *inode, struct file *filp)
942 struct cache_reader *rp = filp->private_data;
943 struct cache_detail *cd = PDE(inode)->data;
946 spin_lock(&queue_lock);
948 struct cache_queue *cq;
949 for (cq= &rp->q; &cq->list != &cd->queue;
950 cq = list_entry(cq->list.next, struct cache_queue, list))
952 container_of(cq, struct cache_request, q)
958 list_del(&rp->q.list);
959 spin_unlock(&queue_lock);
961 filp->private_data = NULL;
964 cd->last_close = get_seconds();
965 atomic_dec(&cd->readers);
972 static const struct file_operations cache_file_operations = {
973 .owner = THIS_MODULE,
976 .write = cache_write,
978 .ioctl = cache_ioctl, /* for FIONREAD */
980 .release = cache_release,
984 static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
986 struct cache_queue *cq;
987 spin_lock(&queue_lock);
988 list_for_each_entry(cq, &detail->queue, list)
990 struct cache_request *cr = container_of(cq, struct cache_request, q);
993 if (cr->readers != 0)
995 list_del(&cr->q.list);
996 spin_unlock(&queue_lock);
997 cache_put(cr->item, detail);
1002 spin_unlock(&queue_lock);
1006 * Support routines for text-based upcalls.
1007 * Fields are separated by spaces.
1008 * Fields are either mangled to quote space tab newline slosh with slosh
1009 * or a hexified with a leading \x
1010 * Record is terminated with newline.
1014 void qword_add(char **bpp, int *lp, char *str)
1020 if (len < 0) return;
1022 while ((c=*str++) && len)
1030 *bp++ = '0' + ((c & 0300)>>6);
1031 *bp++ = '0' + ((c & 0070)>>3);
1032 *bp++ = '0' + ((c & 0007)>>0);
1040 if (c || len <1) len = -1;
1048 EXPORT_SYMBOL_GPL(qword_add);
1050 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1055 if (len < 0) return;
1061 while (blen && len >= 2) {
1062 unsigned char c = *buf++;
1063 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1064 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1069 if (blen || len<1) len = -1;
1077 EXPORT_SYMBOL_GPL(qword_addhex);
1079 static void warn_no_listener(struct cache_detail *detail)
1081 if (detail->last_warn != detail->last_close) {
1082 detail->last_warn = detail->last_close;
1083 if (detail->warn_no_listener)
1084 detail->warn_no_listener(detail, detail->last_close != 0);
1089 * register an upcall request to user-space and queue it up for read() by the
1092 * Each request is at most one page long.
1094 int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1095 void (*cache_request)(struct cache_detail *,
1096 struct cache_head *,
1102 struct cache_request *crq;
1106 if (atomic_read(&detail->readers) == 0 &&
1107 detail->last_close < get_seconds() - 30) {
1108 warn_no_listener(detail);
1112 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1116 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1122 bp = buf; len = PAGE_SIZE;
1124 cache_request(detail, h, &bp, &len);
1132 crq->item = cache_get(h);
1134 crq->len = PAGE_SIZE - len;
1136 spin_lock(&queue_lock);
1137 list_add_tail(&crq->q.list, &detail->queue);
1138 spin_unlock(&queue_lock);
1139 wake_up(&queue_wait);
1142 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1145 * parse a message from user-space and pass it
1146 * to an appropriate cache
1147 * Messages are, like requests, separated into fields by
1148 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1151 * reply cachename expiry key ... content....
1153 * key and content are both parsed by cache
1156 #define isodigit(c) (isdigit(c) && c <= '7')
1157 int qword_get(char **bpp, char *dest, int bufsize)
1159 /* return bytes copied, or -1 on error */
1163 while (*bp == ' ') bp++;
1165 if (bp[0] == '\\' && bp[1] == 'x') {
1168 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1169 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1172 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1178 /* text with \nnn octal quoting */
1179 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1181 isodigit(bp[1]) && (bp[1] <= '3') &&
1184 int byte = (*++bp -'0');
1186 byte = (byte << 3) | (*bp++ - '0');
1187 byte = (byte << 3) | (*bp++ - '0');
1197 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1199 while (*bp == ' ') bp++;
1204 EXPORT_SYMBOL_GPL(qword_get);
1208 * support /proc/sunrpc/cache/$CACHENAME/content
1210 * We call ->cache_show passing NULL for the item to
1211 * get a header, then pass each real item in the cache
1215 struct cache_detail *cd;
1218 static void *c_start(struct seq_file *m, loff_t *pos)
1219 __acquires(cd->hash_lock)
1222 unsigned hash, entry;
1223 struct cache_head *ch;
1224 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1227 read_lock(&cd->hash_lock);
1229 return SEQ_START_TOKEN;
1231 entry = n & ((1LL<<32) - 1);
1233 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1236 n &= ~((1LL<<32) - 1);
1240 } while(hash < cd->hash_size &&
1241 cd->hash_table[hash]==NULL);
1242 if (hash >= cd->hash_size)
1245 return cd->hash_table[hash];
1248 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1250 struct cache_head *ch = p;
1251 int hash = (*pos >> 32);
1252 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1254 if (p == SEQ_START_TOKEN)
1256 else if (ch->next == NULL) {
1263 *pos &= ~((1LL<<32) - 1);
1264 while (hash < cd->hash_size &&
1265 cd->hash_table[hash] == NULL) {
1269 if (hash >= cd->hash_size)
1272 return cd->hash_table[hash];
1275 static void c_stop(struct seq_file *m, void *p)
1276 __releases(cd->hash_lock)
1278 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1279 read_unlock(&cd->hash_lock);
1282 static int c_show(struct seq_file *m, void *p)
1284 struct cache_head *cp = p;
1285 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1287 if (p == SEQ_START_TOKEN)
1288 return cd->cache_show(m, cd, NULL);
1291 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1292 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
1294 if (cache_check(cd, cp, NULL))
1295 /* cache_check does a cache_put on failure */
1296 seq_printf(m, "# ");
1300 return cd->cache_show(m, cd, cp);
1303 static const struct seq_operations cache_content_op = {
1310 static int content_open(struct inode *inode, struct file *file)
1313 struct cache_detail *cd = PDE(inode)->data;
1315 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1323 static const struct file_operations content_file_operations = {
1324 .open = content_open,
1326 .llseek = seq_lseek,
1327 .release = seq_release_private,
1330 static ssize_t read_flush(struct file *file, char __user *buf,
1331 size_t count, loff_t *ppos)
1333 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
1335 unsigned long p = *ppos;
1338 sprintf(tbuf, "%lu\n", cd->flush_time);
1345 if (copy_to_user(buf, (void*)(tbuf+p), len))
1351 static ssize_t write_flush(struct file * file, const char __user * buf,
1352 size_t count, loff_t *ppos)
1354 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
1358 if (*ppos || count > sizeof(tbuf)-1)
1360 if (copy_from_user(tbuf, buf, count))
1363 flushtime = simple_strtoul(tbuf, &ep, 0);
1364 if (*ep && *ep != '\n')
1367 cd->flush_time = flushtime;
1368 cd->nextcheck = get_seconds();
1375 static const struct file_operations cache_flush_operations = {
1376 .open = nonseekable_open,
1378 .write = write_flush,