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 <linux/smp_lock.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>
37 #include <linux/smp_lock.h>
39 #define RPCDBG_FACILITY RPCDBG_CACHE
41 static int 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
)
46 time_t now
= get_seconds();
50 h
->expiry_time
= now
+ CACHE_NEW_EXPIRY
;
51 h
->last_refresh
= now
;
54 static inline int cache_is_expired(struct cache_detail
*detail
, struct cache_head
*h
)
56 return (h
->expiry_time
< get_seconds()) ||
57 (detail
->flush_time
> h
->last_refresh
);
60 struct cache_head
*sunrpc_cache_lookup(struct cache_detail
*detail
,
61 struct cache_head
*key
, int hash
)
63 struct cache_head
**head
, **hp
;
64 struct cache_head
*new = NULL
, *freeme
= NULL
;
66 head
= &detail
->hash_table
[hash
];
68 read_lock(&detail
->hash_lock
);
70 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
71 struct cache_head
*tmp
= *hp
;
72 if (detail
->match(tmp
, key
)) {
73 if (cache_is_expired(detail
, tmp
))
74 /* This entry is expired, we will discard it. */
77 read_unlock(&detail
->hash_lock
);
81 read_unlock(&detail
->hash_lock
);
82 /* Didn't find anything, insert an empty entry */
84 new = detail
->alloc();
87 /* must fully initialise 'new', else
88 * we might get lose if we need to
92 detail
->init(new, key
);
94 write_lock(&detail
->hash_lock
);
96 /* check if entry appeared while we slept */
97 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
98 struct cache_head
*tmp
= *hp
;
99 if (detail
->match(tmp
, key
)) {
100 if (cache_is_expired(detail
, tmp
)) {
108 write_unlock(&detail
->hash_lock
);
109 cache_put(new, detail
);
117 write_unlock(&detail
->hash_lock
);
120 cache_put(freeme
, detail
);
123 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup
);
126 static void cache_dequeue(struct cache_detail
*detail
, struct cache_head
*ch
);
128 static void cache_fresh_locked(struct cache_head
*head
, time_t expiry
)
130 head
->expiry_time
= expiry
;
131 head
->last_refresh
= get_seconds();
132 set_bit(CACHE_VALID
, &head
->flags
);
135 static void cache_fresh_unlocked(struct cache_head
*head
,
136 struct cache_detail
*detail
)
138 if (test_and_clear_bit(CACHE_PENDING
, &head
->flags
)) {
139 cache_revisit_request(head
);
140 cache_dequeue(detail
, head
);
144 struct cache_head
*sunrpc_cache_update(struct cache_detail
*detail
,
145 struct cache_head
*new, struct cache_head
*old
, int hash
)
147 /* The 'old' entry is to be replaced by 'new'.
148 * If 'old' is not VALID, we update it directly,
149 * otherwise we need to replace it
151 struct cache_head
**head
;
152 struct cache_head
*tmp
;
154 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
155 write_lock(&detail
->hash_lock
);
156 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
157 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
158 set_bit(CACHE_NEGATIVE
, &old
->flags
);
160 detail
->update(old
, new);
161 cache_fresh_locked(old
, new->expiry_time
);
162 write_unlock(&detail
->hash_lock
);
163 cache_fresh_unlocked(old
, detail
);
166 write_unlock(&detail
->hash_lock
);
168 /* We need to insert a new entry */
169 tmp
= detail
->alloc();
171 cache_put(old
, detail
);
175 detail
->init(tmp
, old
);
176 head
= &detail
->hash_table
[hash
];
178 write_lock(&detail
->hash_lock
);
179 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
180 set_bit(CACHE_NEGATIVE
, &tmp
->flags
);
182 detail
->update(tmp
, new);
187 cache_fresh_locked(tmp
, new->expiry_time
);
188 cache_fresh_locked(old
, 0);
189 write_unlock(&detail
->hash_lock
);
190 cache_fresh_unlocked(tmp
, detail
);
191 cache_fresh_unlocked(old
, detail
);
192 cache_put(old
, detail
);
195 EXPORT_SYMBOL_GPL(sunrpc_cache_update
);
197 static int cache_make_upcall(struct cache_detail
*cd
, struct cache_head
*h
)
199 if (!cd
->cache_upcall
)
201 return cd
->cache_upcall(cd
, h
);
204 static inline int cache_is_valid(struct cache_detail
*detail
, struct cache_head
*h
)
206 if (!test_bit(CACHE_VALID
, &h
->flags
))
210 if (test_bit(CACHE_NEGATIVE
, &h
->flags
))
218 * This is the generic cache management routine for all
219 * the authentication caches.
220 * It checks the currency of a cache item and will (later)
221 * initiate an upcall to fill it if needed.
224 * Returns 0 if the cache_head can be used, or cache_puts it and returns
225 * -EAGAIN if upcall is pending and request has been queued
226 * -ETIMEDOUT if upcall failed or request could not be queue or
227 * upcall completed but item is still invalid (implying that
228 * the cache item has been replaced with a newer one).
229 * -ENOENT if cache entry was negative
231 int cache_check(struct cache_detail
*detail
,
232 struct cache_head
*h
, struct cache_req
*rqstp
)
235 long refresh_age
, age
;
237 /* First decide return status as best we can */
238 rv
= cache_is_valid(detail
, h
);
240 /* now see if we want to start an upcall */
241 refresh_age
= (h
->expiry_time
- h
->last_refresh
);
242 age
= get_seconds() - h
->last_refresh
;
247 } else if (rv
== -EAGAIN
|| age
> refresh_age
/2) {
248 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
250 if (!test_and_set_bit(CACHE_PENDING
, &h
->flags
)) {
251 switch (cache_make_upcall(detail
, h
)) {
253 clear_bit(CACHE_PENDING
, &h
->flags
);
254 cache_revisit_request(h
);
256 set_bit(CACHE_NEGATIVE
, &h
->flags
);
257 cache_fresh_locked(h
, get_seconds()+CACHE_NEW_EXPIRY
);
258 cache_fresh_unlocked(h
, detail
);
264 clear_bit(CACHE_PENDING
, &h
->flags
);
265 cache_revisit_request(h
);
272 if (cache_defer_req(rqstp
, h
) < 0) {
273 /* Request is not deferred */
274 rv
= cache_is_valid(detail
, h
);
280 cache_put(h
, detail
);
283 EXPORT_SYMBOL_GPL(cache_check
);
286 * caches need to be periodically cleaned.
287 * For this we maintain a list of cache_detail and
288 * a current pointer into that list and into the table
291 * Each time clean_cache is called it finds the next non-empty entry
292 * in the current table and walks the list in that entry
293 * looking for entries that can be removed.
295 * An entry gets removed if:
296 * - The expiry is before current time
297 * - The last_refresh time is before the flush_time for that cache
299 * later we might drop old entries with non-NEVER expiry if that table
300 * is getting 'full' for some definition of 'full'
302 * The question of "how often to scan a table" is an interesting one
303 * and is answered in part by the use of the "nextcheck" field in the
305 * When a scan of a table begins, the nextcheck field is set to a time
306 * that is well into the future.
307 * While scanning, if an expiry time is found that is earlier than the
308 * current nextcheck time, nextcheck is set to that expiry time.
309 * If the flush_time is ever set to a time earlier than the nextcheck
310 * time, the nextcheck time is then set to that flush_time.
312 * A table is then only scanned if the current time is at least
313 * the nextcheck time.
317 static LIST_HEAD(cache_list
);
318 static DEFINE_SPINLOCK(cache_list_lock
);
319 static struct cache_detail
*current_detail
;
320 static int current_index
;
322 static void do_cache_clean(struct work_struct
*work
);
323 static DECLARE_DELAYED_WORK(cache_cleaner
, do_cache_clean
);
325 static void sunrpc_init_cache_detail(struct cache_detail
*cd
)
327 rwlock_init(&cd
->hash_lock
);
328 INIT_LIST_HEAD(&cd
->queue
);
329 spin_lock(&cache_list_lock
);
332 atomic_set(&cd
->readers
, 0);
335 list_add(&cd
->others
, &cache_list
);
336 spin_unlock(&cache_list_lock
);
338 /* start the cleaning process */
339 schedule_delayed_work(&cache_cleaner
, 0);
342 static void sunrpc_destroy_cache_detail(struct cache_detail
*cd
)
345 spin_lock(&cache_list_lock
);
346 write_lock(&cd
->hash_lock
);
347 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
348 write_unlock(&cd
->hash_lock
);
349 spin_unlock(&cache_list_lock
);
352 if (current_detail
== cd
)
353 current_detail
= NULL
;
354 list_del_init(&cd
->others
);
355 write_unlock(&cd
->hash_lock
);
356 spin_unlock(&cache_list_lock
);
357 if (list_empty(&cache_list
)) {
358 /* module must be being unloaded so its safe to kill the worker */
359 cancel_delayed_work_sync(&cache_cleaner
);
363 printk(KERN_ERR
"nfsd: failed to unregister %s cache\n", cd
->name
);
366 /* clean cache tries to find something to clean
368 * It returns 1 if it cleaned something,
369 * 0 if it didn't find anything this time
370 * -1 if it fell off the end of the list.
372 static int cache_clean(void)
375 struct list_head
*next
;
377 spin_lock(&cache_list_lock
);
379 /* find a suitable table if we don't already have one */
380 while (current_detail
== NULL
||
381 current_index
>= current_detail
->hash_size
) {
383 next
= current_detail
->others
.next
;
385 next
= cache_list
.next
;
386 if (next
== &cache_list
) {
387 current_detail
= NULL
;
388 spin_unlock(&cache_list_lock
);
391 current_detail
= list_entry(next
, struct cache_detail
, others
);
392 if (current_detail
->nextcheck
> get_seconds())
393 current_index
= current_detail
->hash_size
;
396 current_detail
->nextcheck
= get_seconds()+30*60;
400 /* find a non-empty bucket in the table */
401 while (current_detail
&&
402 current_index
< current_detail
->hash_size
&&
403 current_detail
->hash_table
[current_index
] == NULL
)
406 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
408 if (current_detail
&& current_index
< current_detail
->hash_size
) {
409 struct cache_head
*ch
, **cp
;
410 struct cache_detail
*d
;
412 write_lock(¤t_detail
->hash_lock
);
414 /* Ok, now to clean this strand */
416 cp
= & current_detail
->hash_table
[current_index
];
417 for (ch
= *cp
; ch
; cp
= & ch
->next
, ch
= *cp
) {
418 if (current_detail
->nextcheck
> ch
->expiry_time
)
419 current_detail
->nextcheck
= ch
->expiry_time
+1;
420 if (!cache_is_expired(current_detail
, ch
))
425 current_detail
->entries
--;
430 write_unlock(¤t_detail
->hash_lock
);
434 spin_unlock(&cache_list_lock
);
436 if (test_and_clear_bit(CACHE_PENDING
, &ch
->flags
))
437 cache_dequeue(current_detail
, ch
);
438 cache_revisit_request(ch
);
442 spin_unlock(&cache_list_lock
);
448 * We want to regularly clean the cache, so we need to schedule some work ...
450 static void do_cache_clean(struct work_struct
*work
)
453 if (cache_clean() == -1)
454 delay
= round_jiffies_relative(30*HZ
);
456 if (list_empty(&cache_list
))
460 schedule_delayed_work(&cache_cleaner
, delay
);
465 * Clean all caches promptly. This just calls cache_clean
466 * repeatedly until we are sure that every cache has had a chance to
469 void cache_flush(void)
471 while (cache_clean() != -1)
473 while (cache_clean() != -1)
476 EXPORT_SYMBOL_GPL(cache_flush
);
478 void cache_purge(struct cache_detail
*detail
)
480 detail
->flush_time
= LONG_MAX
;
481 detail
->nextcheck
= get_seconds();
483 detail
->flush_time
= 1;
485 EXPORT_SYMBOL_GPL(cache_purge
);
489 * Deferral and Revisiting of Requests.
491 * If a cache lookup finds a pending entry, we
492 * need to defer the request and revisit it later.
493 * All deferred requests are stored in a hash table,
494 * indexed by "struct cache_head *".
495 * As it may be wasteful to store a whole request
496 * structure, we allow the request to provide a
497 * deferred form, which must contain a
498 * 'struct cache_deferred_req'
499 * This cache_deferred_req contains a method to allow
500 * it to be revisited when cache info is available
503 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
504 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
506 #define DFR_MAX 300 /* ??? */
508 static DEFINE_SPINLOCK(cache_defer_lock
);
509 static LIST_HEAD(cache_defer_list
);
510 static struct list_head cache_defer_hash
[DFR_HASHSIZE
];
511 static int cache_defer_cnt
;
513 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
515 struct cache_deferred_req
*dreq
, *discard
;
516 int hash
= DFR_HASH(item
);
518 if (cache_defer_cnt
>= DFR_MAX
) {
519 /* too much in the cache, randomly drop this one,
520 * or continue and drop the oldest below
525 dreq
= req
->defer(req
);
531 spin_lock(&cache_defer_lock
);
533 list_add(&dreq
->recent
, &cache_defer_list
);
535 if (cache_defer_hash
[hash
].next
== NULL
)
536 INIT_LIST_HEAD(&cache_defer_hash
[hash
]);
537 list_add(&dreq
->hash
, &cache_defer_hash
[hash
]);
539 /* it is in, now maybe clean up */
541 if (++cache_defer_cnt
> DFR_MAX
) {
542 discard
= list_entry(cache_defer_list
.prev
,
543 struct cache_deferred_req
, recent
);
544 list_del_init(&discard
->recent
);
545 list_del_init(&discard
->hash
);
548 spin_unlock(&cache_defer_lock
);
551 /* there was one too many */
552 discard
->revisit(discard
, 1);
554 if (!test_bit(CACHE_PENDING
, &item
->flags
)) {
555 /* must have just been validated... */
556 cache_revisit_request(item
);
562 static void cache_revisit_request(struct cache_head
*item
)
564 struct cache_deferred_req
*dreq
;
565 struct list_head pending
;
567 struct list_head
*lp
;
568 int hash
= DFR_HASH(item
);
570 INIT_LIST_HEAD(&pending
);
571 spin_lock(&cache_defer_lock
);
573 lp
= cache_defer_hash
[hash
].next
;
575 while (lp
!= &cache_defer_hash
[hash
]) {
576 dreq
= list_entry(lp
, struct cache_deferred_req
, hash
);
578 if (dreq
->item
== item
) {
579 list_del_init(&dreq
->hash
);
580 list_move(&dreq
->recent
, &pending
);
585 spin_unlock(&cache_defer_lock
);
587 while (!list_empty(&pending
)) {
588 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
589 list_del_init(&dreq
->recent
);
590 dreq
->revisit(dreq
, 0);
594 void cache_clean_deferred(void *owner
)
596 struct cache_deferred_req
*dreq
, *tmp
;
597 struct list_head pending
;
600 INIT_LIST_HEAD(&pending
);
601 spin_lock(&cache_defer_lock
);
603 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
604 if (dreq
->owner
== owner
) {
605 list_del_init(&dreq
->hash
);
606 list_move(&dreq
->recent
, &pending
);
610 spin_unlock(&cache_defer_lock
);
612 while (!list_empty(&pending
)) {
613 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
614 list_del_init(&dreq
->recent
);
615 dreq
->revisit(dreq
, 1);
620 * communicate with user-space
622 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
623 * On read, you get a full request, or block.
624 * On write, an update request is processed.
625 * Poll works if anything to read, and always allows write.
627 * Implemented by linked list of requests. Each open file has
628 * a ->private that also exists in this list. New requests are added
629 * to the end and may wakeup and preceding readers.
630 * New readers are added to the head. If, on read, an item is found with
631 * CACHE_UPCALLING clear, we free it from the list.
635 static DEFINE_SPINLOCK(queue_lock
);
636 static DEFINE_MUTEX(queue_io_mutex
);
639 struct list_head list
;
640 int reader
; /* if 0, then request */
642 struct cache_request
{
643 struct cache_queue q
;
644 struct cache_head
*item
;
649 struct cache_reader
{
650 struct cache_queue q
;
651 int offset
; /* if non-0, we have a refcnt on next request */
654 static ssize_t
cache_read(struct file
*filp
, char __user
*buf
, size_t count
,
655 loff_t
*ppos
, struct cache_detail
*cd
)
657 struct cache_reader
*rp
= filp
->private_data
;
658 struct cache_request
*rq
;
659 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
665 mutex_lock(&inode
->i_mutex
); /* protect against multiple concurrent
666 * readers on this file */
668 spin_lock(&queue_lock
);
669 /* need to find next request */
670 while (rp
->q
.list
.next
!= &cd
->queue
&&
671 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
673 struct list_head
*next
= rp
->q
.list
.next
;
674 list_move(&rp
->q
.list
, next
);
676 if (rp
->q
.list
.next
== &cd
->queue
) {
677 spin_unlock(&queue_lock
);
678 mutex_unlock(&inode
->i_mutex
);
682 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
683 BUG_ON(rq
->q
.reader
);
686 spin_unlock(&queue_lock
);
688 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
690 spin_lock(&queue_lock
);
691 list_move(&rp
->q
.list
, &rq
->q
.list
);
692 spin_unlock(&queue_lock
);
694 if (rp
->offset
+ count
> rq
->len
)
695 count
= rq
->len
- rp
->offset
;
697 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
700 if (rp
->offset
>= rq
->len
) {
702 spin_lock(&queue_lock
);
703 list_move(&rp
->q
.list
, &rq
->q
.list
);
704 spin_unlock(&queue_lock
);
709 if (rp
->offset
== 0) {
710 /* need to release rq */
711 spin_lock(&queue_lock
);
713 if (rq
->readers
== 0 &&
714 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
715 list_del(&rq
->q
.list
);
716 spin_unlock(&queue_lock
);
717 cache_put(rq
->item
, cd
);
721 spin_unlock(&queue_lock
);
725 mutex_unlock(&inode
->i_mutex
);
726 return err
? err
: count
;
729 static ssize_t
cache_do_downcall(char *kaddr
, const char __user
*buf
,
730 size_t count
, struct cache_detail
*cd
)
734 if (copy_from_user(kaddr
, buf
, count
))
737 ret
= cd
->cache_parse(cd
, kaddr
, count
);
743 static ssize_t
cache_slow_downcall(const char __user
*buf
,
744 size_t count
, struct cache_detail
*cd
)
746 static char write_buf
[8192]; /* protected by queue_io_mutex */
747 ssize_t ret
= -EINVAL
;
749 if (count
>= sizeof(write_buf
))
751 mutex_lock(&queue_io_mutex
);
752 ret
= cache_do_downcall(write_buf
, buf
, count
, cd
);
753 mutex_unlock(&queue_io_mutex
);
758 static ssize_t
cache_downcall(struct address_space
*mapping
,
759 const char __user
*buf
,
760 size_t count
, struct cache_detail
*cd
)
764 ssize_t ret
= -ENOMEM
;
766 if (count
>= PAGE_CACHE_SIZE
)
769 page
= find_or_create_page(mapping
, 0, GFP_KERNEL
);
774 ret
= cache_do_downcall(kaddr
, buf
, count
, cd
);
777 page_cache_release(page
);
780 return cache_slow_downcall(buf
, count
, cd
);
783 static ssize_t
cache_write(struct file
*filp
, const char __user
*buf
,
784 size_t count
, loff_t
*ppos
,
785 struct cache_detail
*cd
)
787 struct address_space
*mapping
= filp
->f_mapping
;
788 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
789 ssize_t ret
= -EINVAL
;
791 if (!cd
->cache_parse
)
794 mutex_lock(&inode
->i_mutex
);
795 ret
= cache_downcall(mapping
, buf
, count
, cd
);
796 mutex_unlock(&inode
->i_mutex
);
801 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
803 static unsigned int cache_poll(struct file
*filp
, poll_table
*wait
,
804 struct cache_detail
*cd
)
807 struct cache_reader
*rp
= filp
->private_data
;
808 struct cache_queue
*cq
;
810 poll_wait(filp
, &queue_wait
, wait
);
812 /* alway allow write */
813 mask
= POLL_OUT
| POLLWRNORM
;
818 spin_lock(&queue_lock
);
820 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
821 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
823 mask
|= POLLIN
| POLLRDNORM
;
826 spin_unlock(&queue_lock
);
830 static int cache_ioctl(struct inode
*ino
, struct file
*filp
,
831 unsigned int cmd
, unsigned long arg
,
832 struct cache_detail
*cd
)
835 struct cache_reader
*rp
= filp
->private_data
;
836 struct cache_queue
*cq
;
838 if (cmd
!= FIONREAD
|| !rp
)
841 spin_lock(&queue_lock
);
843 /* only find the length remaining in current request,
844 * or the length of the next request
846 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
847 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
849 struct cache_request
*cr
=
850 container_of(cq
, struct cache_request
, q
);
851 len
= cr
->len
- rp
->offset
;
854 spin_unlock(&queue_lock
);
856 return put_user(len
, (int __user
*)arg
);
859 static int cache_open(struct inode
*inode
, struct file
*filp
,
860 struct cache_detail
*cd
)
862 struct cache_reader
*rp
= NULL
;
864 if (!cd
|| !try_module_get(cd
->owner
))
866 nonseekable_open(inode
, filp
);
867 if (filp
->f_mode
& FMODE_READ
) {
868 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
873 atomic_inc(&cd
->readers
);
874 spin_lock(&queue_lock
);
875 list_add(&rp
->q
.list
, &cd
->queue
);
876 spin_unlock(&queue_lock
);
878 filp
->private_data
= rp
;
882 static int cache_release(struct inode
*inode
, struct file
*filp
,
883 struct cache_detail
*cd
)
885 struct cache_reader
*rp
= filp
->private_data
;
888 spin_lock(&queue_lock
);
890 struct cache_queue
*cq
;
891 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
892 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
894 container_of(cq
, struct cache_request
, q
)
900 list_del(&rp
->q
.list
);
901 spin_unlock(&queue_lock
);
903 filp
->private_data
= NULL
;
906 cd
->last_close
= get_seconds();
907 atomic_dec(&cd
->readers
);
909 module_put(cd
->owner
);
915 static void cache_dequeue(struct cache_detail
*detail
, struct cache_head
*ch
)
917 struct cache_queue
*cq
;
918 spin_lock(&queue_lock
);
919 list_for_each_entry(cq
, &detail
->queue
, list
)
921 struct cache_request
*cr
= container_of(cq
, struct cache_request
, q
);
924 if (cr
->readers
!= 0)
926 list_del(&cr
->q
.list
);
927 spin_unlock(&queue_lock
);
928 cache_put(cr
->item
, detail
);
933 spin_unlock(&queue_lock
);
937 * Support routines for text-based upcalls.
938 * Fields are separated by spaces.
939 * Fields are either mangled to quote space tab newline slosh with slosh
940 * or a hexified with a leading \x
941 * Record is terminated with newline.
945 void qword_add(char **bpp
, int *lp
, char *str
)
953 while ((c
=*str
++) && len
)
961 *bp
++ = '0' + ((c
& 0300)>>6);
962 *bp
++ = '0' + ((c
& 0070)>>3);
963 *bp
++ = '0' + ((c
& 0007)>>0);
971 if (c
|| len
<1) len
= -1;
979 EXPORT_SYMBOL_GPL(qword_add
);
981 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
992 while (blen
&& len
>= 2) {
993 unsigned char c
= *buf
++;
994 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
995 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
1000 if (blen
|| len
<1) len
= -1;
1008 EXPORT_SYMBOL_GPL(qword_addhex
);
1010 static void warn_no_listener(struct cache_detail
*detail
)
1012 if (detail
->last_warn
!= detail
->last_close
) {
1013 detail
->last_warn
= detail
->last_close
;
1014 if (detail
->warn_no_listener
)
1015 detail
->warn_no_listener(detail
, detail
->last_close
!= 0);
1020 * register an upcall request to user-space and queue it up for read() by the
1023 * Each request is at most one page long.
1025 int sunrpc_cache_pipe_upcall(struct cache_detail
*detail
, struct cache_head
*h
,
1026 void (*cache_request
)(struct cache_detail
*,
1027 struct cache_head
*,
1033 struct cache_request
*crq
;
1037 if (atomic_read(&detail
->readers
) == 0 &&
1038 detail
->last_close
< get_seconds() - 30) {
1039 warn_no_listener(detail
);
1043 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1047 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1053 bp
= buf
; len
= PAGE_SIZE
;
1055 cache_request(detail
, h
, &bp
, &len
);
1063 crq
->item
= cache_get(h
);
1065 crq
->len
= PAGE_SIZE
- len
;
1067 spin_lock(&queue_lock
);
1068 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1069 spin_unlock(&queue_lock
);
1070 wake_up(&queue_wait
);
1073 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall
);
1076 * parse a message from user-space and pass it
1077 * to an appropriate cache
1078 * Messages are, like requests, separated into fields by
1079 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1082 * reply cachename expiry key ... content....
1084 * key and content are both parsed by cache
1087 #define isodigit(c) (isdigit(c) && c <= '7')
1088 int qword_get(char **bpp
, char *dest
, int bufsize
)
1090 /* return bytes copied, or -1 on error */
1094 while (*bp
== ' ') bp
++;
1096 if (bp
[0] == '\\' && bp
[1] == 'x') {
1099 while (isxdigit(bp
[0]) && isxdigit(bp
[1]) && len
< bufsize
) {
1100 int byte
= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1103 byte
|= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1109 /* text with \nnn octal quoting */
1110 while (*bp
!= ' ' && *bp
!= '\n' && *bp
&& len
< bufsize
-1) {
1112 isodigit(bp
[1]) && (bp
[1] <= '3') &&
1115 int byte
= (*++bp
-'0');
1117 byte
= (byte
<< 3) | (*bp
++ - '0');
1118 byte
= (byte
<< 3) | (*bp
++ - '0');
1128 if (*bp
!= ' ' && *bp
!= '\n' && *bp
!= '\0')
1130 while (*bp
== ' ') bp
++;
1135 EXPORT_SYMBOL_GPL(qword_get
);
1139 * support /proc/sunrpc/cache/$CACHENAME/content
1141 * We call ->cache_show passing NULL for the item to
1142 * get a header, then pass each real item in the cache
1146 struct cache_detail
*cd
;
1149 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1150 __acquires(cd
->hash_lock
)
1153 unsigned hash
, entry
;
1154 struct cache_head
*ch
;
1155 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1158 read_lock(&cd
->hash_lock
);
1160 return SEQ_START_TOKEN
;
1162 entry
= n
& ((1LL<<32) - 1);
1164 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1167 n
&= ~((1LL<<32) - 1);
1171 } while(hash
< cd
->hash_size
&&
1172 cd
->hash_table
[hash
]==NULL
);
1173 if (hash
>= cd
->hash_size
)
1176 return cd
->hash_table
[hash
];
1179 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1181 struct cache_head
*ch
= p
;
1182 int hash
= (*pos
>> 32);
1183 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1185 if (p
== SEQ_START_TOKEN
)
1187 else if (ch
->next
== NULL
) {
1194 *pos
&= ~((1LL<<32) - 1);
1195 while (hash
< cd
->hash_size
&&
1196 cd
->hash_table
[hash
] == NULL
) {
1200 if (hash
>= cd
->hash_size
)
1203 return cd
->hash_table
[hash
];
1206 static void c_stop(struct seq_file
*m
, void *p
)
1207 __releases(cd
->hash_lock
)
1209 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1210 read_unlock(&cd
->hash_lock
);
1213 static int c_show(struct seq_file
*m
, void *p
)
1215 struct cache_head
*cp
= p
;
1216 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1218 if (p
== SEQ_START_TOKEN
)
1219 return cd
->cache_show(m
, cd
, NULL
);
1222 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1223 cp
->expiry_time
, atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1225 if (cache_check(cd
, cp
, NULL
))
1226 /* cache_check does a cache_put on failure */
1227 seq_printf(m
, "# ");
1231 return cd
->cache_show(m
, cd
, cp
);
1234 static const struct seq_operations cache_content_op
= {
1241 static int content_open(struct inode
*inode
, struct file
*file
,
1242 struct cache_detail
*cd
)
1246 if (!cd
|| !try_module_get(cd
->owner
))
1248 han
= __seq_open_private(file
, &cache_content_op
, sizeof(*han
));
1250 module_put(cd
->owner
);
1258 static int content_release(struct inode
*inode
, struct file
*file
,
1259 struct cache_detail
*cd
)
1261 int ret
= seq_release_private(inode
, file
);
1262 module_put(cd
->owner
);
1266 static int open_flush(struct inode
*inode
, struct file
*file
,
1267 struct cache_detail
*cd
)
1269 if (!cd
|| !try_module_get(cd
->owner
))
1271 return nonseekable_open(inode
, file
);
1274 static int release_flush(struct inode
*inode
, struct file
*file
,
1275 struct cache_detail
*cd
)
1277 module_put(cd
->owner
);
1281 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1282 size_t count
, loff_t
*ppos
,
1283 struct cache_detail
*cd
)
1286 unsigned long p
= *ppos
;
1289 sprintf(tbuf
, "%lu\n", cd
->flush_time
);
1296 if (copy_to_user(buf
, (void*)(tbuf
+p
), len
))
1302 static ssize_t
write_flush(struct file
*file
, const char __user
*buf
,
1303 size_t count
, loff_t
*ppos
,
1304 struct cache_detail
*cd
)
1309 if (*ppos
|| count
> sizeof(tbuf
)-1)
1311 if (copy_from_user(tbuf
, buf
, count
))
1314 flushtime
= simple_strtoul(tbuf
, &ep
, 0);
1315 if (*ep
&& *ep
!= '\n')
1318 cd
->flush_time
= flushtime
;
1319 cd
->nextcheck
= get_seconds();
1326 static ssize_t
cache_read_procfs(struct file
*filp
, char __user
*buf
,
1327 size_t count
, loff_t
*ppos
)
1329 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1331 return cache_read(filp
, buf
, count
, ppos
, cd
);
1334 static ssize_t
cache_write_procfs(struct file
*filp
, const char __user
*buf
,
1335 size_t count
, loff_t
*ppos
)
1337 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1339 return cache_write(filp
, buf
, count
, ppos
, cd
);
1342 static unsigned int cache_poll_procfs(struct file
*filp
, poll_table
*wait
)
1344 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1346 return cache_poll(filp
, wait
, cd
);
1349 static long cache_ioctl_procfs(struct file
*filp
,
1350 unsigned int cmd
, unsigned long arg
)
1353 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1354 struct cache_detail
*cd
= PDE(inode
)->data
;
1357 ret
= cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1363 static int cache_open_procfs(struct inode
*inode
, struct file
*filp
)
1365 struct cache_detail
*cd
= PDE(inode
)->data
;
1367 return cache_open(inode
, filp
, cd
);
1370 static int cache_release_procfs(struct inode
*inode
, struct file
*filp
)
1372 struct cache_detail
*cd
= PDE(inode
)->data
;
1374 return cache_release(inode
, filp
, cd
);
1377 static const struct file_operations cache_file_operations_procfs
= {
1378 .owner
= THIS_MODULE
,
1379 .llseek
= no_llseek
,
1380 .read
= cache_read_procfs
,
1381 .write
= cache_write_procfs
,
1382 .poll
= cache_poll_procfs
,
1383 .unlocked_ioctl
= cache_ioctl_procfs
, /* for FIONREAD */
1384 .open
= cache_open_procfs
,
1385 .release
= cache_release_procfs
,
1388 static int content_open_procfs(struct inode
*inode
, struct file
*filp
)
1390 struct cache_detail
*cd
= PDE(inode
)->data
;
1392 return content_open(inode
, filp
, cd
);
1395 static int content_release_procfs(struct inode
*inode
, struct file
*filp
)
1397 struct cache_detail
*cd
= PDE(inode
)->data
;
1399 return content_release(inode
, filp
, cd
);
1402 static const struct file_operations content_file_operations_procfs
= {
1403 .open
= content_open_procfs
,
1405 .llseek
= seq_lseek
,
1406 .release
= content_release_procfs
,
1409 static int open_flush_procfs(struct inode
*inode
, struct file
*filp
)
1411 struct cache_detail
*cd
= PDE(inode
)->data
;
1413 return open_flush(inode
, filp
, cd
);
1416 static int release_flush_procfs(struct inode
*inode
, struct file
*filp
)
1418 struct cache_detail
*cd
= PDE(inode
)->data
;
1420 return release_flush(inode
, filp
, cd
);
1423 static ssize_t
read_flush_procfs(struct file
*filp
, char __user
*buf
,
1424 size_t count
, loff_t
*ppos
)
1426 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1428 return read_flush(filp
, buf
, count
, ppos
, cd
);
1431 static ssize_t
write_flush_procfs(struct file
*filp
,
1432 const char __user
*buf
,
1433 size_t count
, loff_t
*ppos
)
1435 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1437 return write_flush(filp
, buf
, count
, ppos
, cd
);
1440 static const struct file_operations cache_flush_operations_procfs
= {
1441 .open
= open_flush_procfs
,
1442 .read
= read_flush_procfs
,
1443 .write
= write_flush_procfs
,
1444 .release
= release_flush_procfs
,
1447 static void remove_cache_proc_entries(struct cache_detail
*cd
)
1449 if (cd
->u
.procfs
.proc_ent
== NULL
)
1451 if (cd
->u
.procfs
.flush_ent
)
1452 remove_proc_entry("flush", cd
->u
.procfs
.proc_ent
);
1453 if (cd
->u
.procfs
.channel_ent
)
1454 remove_proc_entry("channel", cd
->u
.procfs
.proc_ent
);
1455 if (cd
->u
.procfs
.content_ent
)
1456 remove_proc_entry("content", cd
->u
.procfs
.proc_ent
);
1457 cd
->u
.procfs
.proc_ent
= NULL
;
1458 remove_proc_entry(cd
->name
, proc_net_rpc
);
1461 #ifdef CONFIG_PROC_FS
1462 static int create_cache_proc_entries(struct cache_detail
*cd
)
1464 struct proc_dir_entry
*p
;
1466 cd
->u
.procfs
.proc_ent
= proc_mkdir(cd
->name
, proc_net_rpc
);
1467 if (cd
->u
.procfs
.proc_ent
== NULL
)
1469 cd
->u
.procfs
.channel_ent
= NULL
;
1470 cd
->u
.procfs
.content_ent
= NULL
;
1472 p
= proc_create_data("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
1473 cd
->u
.procfs
.proc_ent
,
1474 &cache_flush_operations_procfs
, cd
);
1475 cd
->u
.procfs
.flush_ent
= p
;
1479 if (cd
->cache_upcall
|| cd
->cache_parse
) {
1480 p
= proc_create_data("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
1481 cd
->u
.procfs
.proc_ent
,
1482 &cache_file_operations_procfs
, cd
);
1483 cd
->u
.procfs
.channel_ent
= p
;
1487 if (cd
->cache_show
) {
1488 p
= proc_create_data("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
1489 cd
->u
.procfs
.proc_ent
,
1490 &content_file_operations_procfs
, cd
);
1491 cd
->u
.procfs
.content_ent
= p
;
1497 remove_cache_proc_entries(cd
);
1500 #else /* CONFIG_PROC_FS */
1501 static int create_cache_proc_entries(struct cache_detail
*cd
)
1507 int cache_register(struct cache_detail
*cd
)
1511 sunrpc_init_cache_detail(cd
);
1512 ret
= create_cache_proc_entries(cd
);
1514 sunrpc_destroy_cache_detail(cd
);
1517 EXPORT_SYMBOL_GPL(cache_register
);
1519 void cache_unregister(struct cache_detail
*cd
)
1521 remove_cache_proc_entries(cd
);
1522 sunrpc_destroy_cache_detail(cd
);
1524 EXPORT_SYMBOL_GPL(cache_unregister
);
1526 static ssize_t
cache_read_pipefs(struct file
*filp
, char __user
*buf
,
1527 size_t count
, loff_t
*ppos
)
1529 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1531 return cache_read(filp
, buf
, count
, ppos
, cd
);
1534 static ssize_t
cache_write_pipefs(struct file
*filp
, const char __user
*buf
,
1535 size_t count
, loff_t
*ppos
)
1537 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1539 return cache_write(filp
, buf
, count
, ppos
, cd
);
1542 static unsigned int cache_poll_pipefs(struct file
*filp
, poll_table
*wait
)
1544 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1546 return cache_poll(filp
, wait
, cd
);
1549 static long cache_ioctl_pipefs(struct file
*filp
,
1550 unsigned int cmd
, unsigned long arg
)
1552 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1553 struct cache_detail
*cd
= RPC_I(inode
)->private;
1557 ret
= cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1563 static int cache_open_pipefs(struct inode
*inode
, struct file
*filp
)
1565 struct cache_detail
*cd
= RPC_I(inode
)->private;
1567 return cache_open(inode
, filp
, cd
);
1570 static int cache_release_pipefs(struct inode
*inode
, struct file
*filp
)
1572 struct cache_detail
*cd
= RPC_I(inode
)->private;
1574 return cache_release(inode
, filp
, cd
);
1577 const struct file_operations cache_file_operations_pipefs
= {
1578 .owner
= THIS_MODULE
,
1579 .llseek
= no_llseek
,
1580 .read
= cache_read_pipefs
,
1581 .write
= cache_write_pipefs
,
1582 .poll
= cache_poll_pipefs
,
1583 .unlocked_ioctl
= cache_ioctl_pipefs
, /* for FIONREAD */
1584 .open
= cache_open_pipefs
,
1585 .release
= cache_release_pipefs
,
1588 static int content_open_pipefs(struct inode
*inode
, struct file
*filp
)
1590 struct cache_detail
*cd
= RPC_I(inode
)->private;
1592 return content_open(inode
, filp
, cd
);
1595 static int content_release_pipefs(struct inode
*inode
, struct file
*filp
)
1597 struct cache_detail
*cd
= RPC_I(inode
)->private;
1599 return content_release(inode
, filp
, cd
);
1602 const struct file_operations content_file_operations_pipefs
= {
1603 .open
= content_open_pipefs
,
1605 .llseek
= seq_lseek
,
1606 .release
= content_release_pipefs
,
1609 static int open_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1611 struct cache_detail
*cd
= RPC_I(inode
)->private;
1613 return open_flush(inode
, filp
, cd
);
1616 static int release_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1618 struct cache_detail
*cd
= RPC_I(inode
)->private;
1620 return release_flush(inode
, filp
, cd
);
1623 static ssize_t
read_flush_pipefs(struct file
*filp
, char __user
*buf
,
1624 size_t count
, loff_t
*ppos
)
1626 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1628 return read_flush(filp
, buf
, count
, ppos
, cd
);
1631 static ssize_t
write_flush_pipefs(struct file
*filp
,
1632 const char __user
*buf
,
1633 size_t count
, loff_t
*ppos
)
1635 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1637 return write_flush(filp
, buf
, count
, ppos
, cd
);
1640 const struct file_operations cache_flush_operations_pipefs
= {
1641 .open
= open_flush_pipefs
,
1642 .read
= read_flush_pipefs
,
1643 .write
= write_flush_pipefs
,
1644 .release
= release_flush_pipefs
,
1647 int sunrpc_cache_register_pipefs(struct dentry
*parent
,
1648 const char *name
, mode_t umode
,
1649 struct cache_detail
*cd
)
1655 sunrpc_init_cache_detail(cd
);
1657 q
.len
= strlen(name
);
1658 q
.hash
= full_name_hash(q
.name
, q
.len
);
1659 dir
= rpc_create_cache_dir(parent
, &q
, umode
, cd
);
1661 cd
->u
.pipefs
.dir
= dir
;
1663 sunrpc_destroy_cache_detail(cd
);
1668 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs
);
1670 void sunrpc_cache_unregister_pipefs(struct cache_detail
*cd
)
1672 rpc_remove_cache_dir(cd
->u
.pipefs
.dir
);
1673 cd
->u
.pipefs
.dir
= NULL
;
1674 sunrpc_destroy_cache_detail(cd
);
1676 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs
);