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>
35 #include <linux/sunrpc/rpc_pipe_fs.h>
37 #define RPCDBG_FACILITY RPCDBG_CACHE
39 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
);
40 static void cache_revisit_request(struct cache_head
*item
);
42 static void cache_init(struct cache_head
*h
)
44 time_t now
= get_seconds();
48 h
->expiry_time
= now
+ CACHE_NEW_EXPIRY
;
49 h
->last_refresh
= now
;
52 struct cache_head
*sunrpc_cache_lookup(struct cache_detail
*detail
,
53 struct cache_head
*key
, int hash
)
55 struct cache_head
**head
, **hp
;
56 struct cache_head
*new = NULL
;
58 head
= &detail
->hash_table
[hash
];
60 read_lock(&detail
->hash_lock
);
62 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
63 struct cache_head
*tmp
= *hp
;
64 if (detail
->match(tmp
, key
)) {
66 read_unlock(&detail
->hash_lock
);
70 read_unlock(&detail
->hash_lock
);
71 /* Didn't find anything, insert an empty entry */
73 new = detail
->alloc();
76 /* must fully initialise 'new', else
77 * we might get lose if we need to
81 detail
->init(new, key
);
83 write_lock(&detail
->hash_lock
);
85 /* check if entry appeared while we slept */
86 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
87 struct cache_head
*tmp
= *hp
;
88 if (detail
->match(tmp
, key
)) {
90 write_unlock(&detail
->hash_lock
);
91 cache_put(new, detail
);
99 write_unlock(&detail
->hash_lock
);
103 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup
);
106 static void cache_dequeue(struct cache_detail
*detail
, struct cache_head
*ch
);
108 static void cache_fresh_locked(struct cache_head
*head
, time_t expiry
)
110 head
->expiry_time
= expiry
;
111 head
->last_refresh
= get_seconds();
112 set_bit(CACHE_VALID
, &head
->flags
);
115 static void cache_fresh_unlocked(struct cache_head
*head
,
116 struct cache_detail
*detail
)
118 if (test_and_clear_bit(CACHE_PENDING
, &head
->flags
)) {
119 cache_revisit_request(head
);
120 cache_dequeue(detail
, head
);
124 struct cache_head
*sunrpc_cache_update(struct cache_detail
*detail
,
125 struct cache_head
*new, struct cache_head
*old
, int hash
)
127 /* The 'old' entry is to be replaced by 'new'.
128 * If 'old' is not VALID, we update it directly,
129 * otherwise we need to replace it
131 struct cache_head
**head
;
132 struct cache_head
*tmp
;
134 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
135 write_lock(&detail
->hash_lock
);
136 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
137 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
138 set_bit(CACHE_NEGATIVE
, &old
->flags
);
140 detail
->update(old
, new);
141 cache_fresh_locked(old
, new->expiry_time
);
142 write_unlock(&detail
->hash_lock
);
143 cache_fresh_unlocked(old
, detail
);
146 write_unlock(&detail
->hash_lock
);
148 /* We need to insert a new entry */
149 tmp
= detail
->alloc();
151 cache_put(old
, detail
);
155 detail
->init(tmp
, old
);
156 head
= &detail
->hash_table
[hash
];
158 write_lock(&detail
->hash_lock
);
159 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
160 set_bit(CACHE_NEGATIVE
, &tmp
->flags
);
162 detail
->update(tmp
, new);
167 cache_fresh_locked(tmp
, new->expiry_time
);
168 cache_fresh_locked(old
, 0);
169 write_unlock(&detail
->hash_lock
);
170 cache_fresh_unlocked(tmp
, detail
);
171 cache_fresh_unlocked(old
, detail
);
172 cache_put(old
, detail
);
175 EXPORT_SYMBOL_GPL(sunrpc_cache_update
);
177 static int cache_make_upcall(struct cache_detail
*cd
, struct cache_head
*h
)
179 if (!cd
->cache_upcall
)
181 return cd
->cache_upcall(cd
, h
);
184 static inline int cache_is_valid(struct cache_detail
*detail
, struct cache_head
*h
)
186 if (!test_bit(CACHE_VALID
, &h
->flags
) ||
187 h
->expiry_time
< get_seconds())
189 else if (detail
->flush_time
> h
->last_refresh
)
193 if (test_bit(CACHE_NEGATIVE
, &h
->flags
))
201 * This is the generic cache management routine for all
202 * the authentication caches.
203 * It checks the currency of a cache item and will (later)
204 * initiate an upcall to fill it if needed.
207 * Returns 0 if the cache_head can be used, or cache_puts it and returns
208 * -EAGAIN if upcall is pending and request has been queued
209 * -ETIMEDOUT if upcall failed or request could not be queue or
210 * upcall completed but item is still invalid (implying that
211 * the cache item has been replaced with a newer one).
212 * -ENOENT if cache entry was negative
214 int cache_check(struct cache_detail
*detail
,
215 struct cache_head
*h
, struct cache_req
*rqstp
)
218 long refresh_age
, age
;
220 /* First decide return status as best we can */
221 rv
= cache_is_valid(detail
, h
);
223 /* now see if we want to start an upcall */
224 refresh_age
= (h
->expiry_time
- h
->last_refresh
);
225 age
= get_seconds() - h
->last_refresh
;
230 } else if (rv
== -EAGAIN
|| age
> refresh_age
/2) {
231 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
233 if (!test_and_set_bit(CACHE_PENDING
, &h
->flags
)) {
234 switch (cache_make_upcall(detail
, h
)) {
236 clear_bit(CACHE_PENDING
, &h
->flags
);
237 cache_revisit_request(h
);
239 set_bit(CACHE_NEGATIVE
, &h
->flags
);
240 cache_fresh_locked(h
, get_seconds()+CACHE_NEW_EXPIRY
);
241 cache_fresh_unlocked(h
, detail
);
247 clear_bit(CACHE_PENDING
, &h
->flags
);
248 cache_revisit_request(h
);
255 if (cache_defer_req(rqstp
, h
) < 0) {
256 /* Request is not deferred */
257 rv
= cache_is_valid(detail
, h
);
263 cache_put(h
, detail
);
266 EXPORT_SYMBOL_GPL(cache_check
);
269 * caches need to be periodically cleaned.
270 * For this we maintain a list of cache_detail and
271 * a current pointer into that list and into the table
274 * Each time clean_cache is called it finds the next non-empty entry
275 * in the current table and walks the list in that entry
276 * looking for entries that can be removed.
278 * An entry gets removed if:
279 * - The expiry is before current time
280 * - The last_refresh time is before the flush_time for that cache
282 * later we might drop old entries with non-NEVER expiry if that table
283 * is getting 'full' for some definition of 'full'
285 * The question of "how often to scan a table" is an interesting one
286 * and is answered in part by the use of the "nextcheck" field in the
288 * When a scan of a table begins, the nextcheck field is set to a time
289 * that is well into the future.
290 * While scanning, if an expiry time is found that is earlier than the
291 * current nextcheck time, nextcheck is set to that expiry time.
292 * If the flush_time is ever set to a time earlier than the nextcheck
293 * time, the nextcheck time is then set to that flush_time.
295 * A table is then only scanned if the current time is at least
296 * the nextcheck time.
300 static LIST_HEAD(cache_list
);
301 static DEFINE_SPINLOCK(cache_list_lock
);
302 static struct cache_detail
*current_detail
;
303 static int current_index
;
305 static void do_cache_clean(struct work_struct
*work
);
306 static DECLARE_DELAYED_WORK(cache_cleaner
, do_cache_clean
);
308 static void sunrpc_init_cache_detail(struct cache_detail
*cd
)
310 rwlock_init(&cd
->hash_lock
);
311 INIT_LIST_HEAD(&cd
->queue
);
312 spin_lock(&cache_list_lock
);
315 atomic_set(&cd
->readers
, 0);
318 list_add(&cd
->others
, &cache_list
);
319 spin_unlock(&cache_list_lock
);
321 /* start the cleaning process */
322 schedule_delayed_work(&cache_cleaner
, 0);
325 static void sunrpc_destroy_cache_detail(struct cache_detail
*cd
)
328 spin_lock(&cache_list_lock
);
329 write_lock(&cd
->hash_lock
);
330 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
331 write_unlock(&cd
->hash_lock
);
332 spin_unlock(&cache_list_lock
);
335 if (current_detail
== cd
)
336 current_detail
= NULL
;
337 list_del_init(&cd
->others
);
338 write_unlock(&cd
->hash_lock
);
339 spin_unlock(&cache_list_lock
);
340 if (list_empty(&cache_list
)) {
341 /* module must be being unloaded so its safe to kill the worker */
342 cancel_delayed_work_sync(&cache_cleaner
);
346 printk(KERN_ERR
"nfsd: failed to unregister %s cache\n", cd
->name
);
349 /* clean cache tries to find something to clean
351 * It returns 1 if it cleaned something,
352 * 0 if it didn't find anything this time
353 * -1 if it fell off the end of the list.
355 static int cache_clean(void)
358 struct list_head
*next
;
360 spin_lock(&cache_list_lock
);
362 /* find a suitable table if we don't already have one */
363 while (current_detail
== NULL
||
364 current_index
>= current_detail
->hash_size
) {
366 next
= current_detail
->others
.next
;
368 next
= cache_list
.next
;
369 if (next
== &cache_list
) {
370 current_detail
= NULL
;
371 spin_unlock(&cache_list_lock
);
374 current_detail
= list_entry(next
, struct cache_detail
, others
);
375 if (current_detail
->nextcheck
> get_seconds())
376 current_index
= current_detail
->hash_size
;
379 current_detail
->nextcheck
= get_seconds()+30*60;
383 /* find a non-empty bucket in the table */
384 while (current_detail
&&
385 current_index
< current_detail
->hash_size
&&
386 current_detail
->hash_table
[current_index
] == NULL
)
389 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
391 if (current_detail
&& current_index
< current_detail
->hash_size
) {
392 struct cache_head
*ch
, **cp
;
393 struct cache_detail
*d
;
395 write_lock(¤t_detail
->hash_lock
);
397 /* Ok, now to clean this strand */
399 cp
= & current_detail
->hash_table
[current_index
];
401 for (; ch
; cp
= & ch
->next
, ch
= *cp
) {
402 if (current_detail
->nextcheck
> ch
->expiry_time
)
403 current_detail
->nextcheck
= ch
->expiry_time
+1;
404 if (ch
->expiry_time
>= get_seconds()
405 && ch
->last_refresh
>= current_detail
->flush_time
408 if (test_and_clear_bit(CACHE_PENDING
, &ch
->flags
))
409 cache_dequeue(current_detail
, ch
);
411 if (atomic_read(&ch
->ref
.refcount
) == 1)
417 current_detail
->entries
--;
420 write_unlock(¤t_detail
->hash_lock
);
424 spin_unlock(&cache_list_lock
);
426 cache_revisit_request(ch
);
430 spin_unlock(&cache_list_lock
);
436 * We want to regularly clean the cache, so we need to schedule some work ...
438 static void do_cache_clean(struct work_struct
*work
)
441 if (cache_clean() == -1)
442 delay
= round_jiffies_relative(30*HZ
);
444 if (list_empty(&cache_list
))
448 schedule_delayed_work(&cache_cleaner
, delay
);
453 * Clean all caches promptly. This just calls cache_clean
454 * repeatedly until we are sure that every cache has had a chance to
457 void cache_flush(void)
459 while (cache_clean() != -1)
461 while (cache_clean() != -1)
464 EXPORT_SYMBOL_GPL(cache_flush
);
466 void cache_purge(struct cache_detail
*detail
)
468 detail
->flush_time
= LONG_MAX
;
469 detail
->nextcheck
= get_seconds();
471 detail
->flush_time
= 1;
473 EXPORT_SYMBOL_GPL(cache_purge
);
477 * Deferral and Revisiting of Requests.
479 * If a cache lookup finds a pending entry, we
480 * need to defer the request and revisit it later.
481 * All deferred requests are stored in a hash table,
482 * indexed by "struct cache_head *".
483 * As it may be wasteful to store a whole request
484 * structure, we allow the request to provide a
485 * deferred form, which must contain a
486 * 'struct cache_deferred_req'
487 * This cache_deferred_req contains a method to allow
488 * it to be revisited when cache info is available
491 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
492 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
494 #define DFR_MAX 300 /* ??? */
496 static DEFINE_SPINLOCK(cache_defer_lock
);
497 static LIST_HEAD(cache_defer_list
);
498 static struct list_head cache_defer_hash
[DFR_HASHSIZE
];
499 static int cache_defer_cnt
;
501 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
503 struct cache_deferred_req
*dreq
, *discard
;
504 int hash
= DFR_HASH(item
);
506 if (cache_defer_cnt
>= DFR_MAX
) {
507 /* too much in the cache, randomly drop this one,
508 * or continue and drop the oldest below
513 dreq
= req
->defer(req
);
519 spin_lock(&cache_defer_lock
);
521 list_add(&dreq
->recent
, &cache_defer_list
);
523 if (cache_defer_hash
[hash
].next
== NULL
)
524 INIT_LIST_HEAD(&cache_defer_hash
[hash
]);
525 list_add(&dreq
->hash
, &cache_defer_hash
[hash
]);
527 /* it is in, now maybe clean up */
529 if (++cache_defer_cnt
> DFR_MAX
) {
530 discard
= list_entry(cache_defer_list
.prev
,
531 struct cache_deferred_req
, recent
);
532 list_del_init(&discard
->recent
);
533 list_del_init(&discard
->hash
);
536 spin_unlock(&cache_defer_lock
);
539 /* there was one too many */
540 discard
->revisit(discard
, 1);
542 if (!test_bit(CACHE_PENDING
, &item
->flags
)) {
543 /* must have just been validated... */
544 cache_revisit_request(item
);
550 static void cache_revisit_request(struct cache_head
*item
)
552 struct cache_deferred_req
*dreq
;
553 struct list_head pending
;
555 struct list_head
*lp
;
556 int hash
= DFR_HASH(item
);
558 INIT_LIST_HEAD(&pending
);
559 spin_lock(&cache_defer_lock
);
561 lp
= cache_defer_hash
[hash
].next
;
563 while (lp
!= &cache_defer_hash
[hash
]) {
564 dreq
= list_entry(lp
, struct cache_deferred_req
, hash
);
566 if (dreq
->item
== item
) {
567 list_del_init(&dreq
->hash
);
568 list_move(&dreq
->recent
, &pending
);
573 spin_unlock(&cache_defer_lock
);
575 while (!list_empty(&pending
)) {
576 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
577 list_del_init(&dreq
->recent
);
578 dreq
->revisit(dreq
, 0);
582 void cache_clean_deferred(void *owner
)
584 struct cache_deferred_req
*dreq
, *tmp
;
585 struct list_head pending
;
588 INIT_LIST_HEAD(&pending
);
589 spin_lock(&cache_defer_lock
);
591 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
592 if (dreq
->owner
== owner
) {
593 list_del_init(&dreq
->hash
);
594 list_move(&dreq
->recent
, &pending
);
598 spin_unlock(&cache_defer_lock
);
600 while (!list_empty(&pending
)) {
601 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
602 list_del_init(&dreq
->recent
);
603 dreq
->revisit(dreq
, 1);
608 * communicate with user-space
610 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
611 * On read, you get a full request, or block.
612 * On write, an update request is processed.
613 * Poll works if anything to read, and always allows write.
615 * Implemented by linked list of requests. Each open file has
616 * a ->private that also exists in this list. New requests are added
617 * to the end and may wakeup and preceding readers.
618 * New readers are added to the head. If, on read, an item is found with
619 * CACHE_UPCALLING clear, we free it from the list.
623 static DEFINE_SPINLOCK(queue_lock
);
624 static DEFINE_MUTEX(queue_io_mutex
);
627 struct list_head list
;
628 int reader
; /* if 0, then request */
630 struct cache_request
{
631 struct cache_queue q
;
632 struct cache_head
*item
;
637 struct cache_reader
{
638 struct cache_queue q
;
639 int offset
; /* if non-0, we have a refcnt on next request */
642 static ssize_t
cache_read(struct file
*filp
, char __user
*buf
, size_t count
,
643 loff_t
*ppos
, struct cache_detail
*cd
)
645 struct cache_reader
*rp
= filp
->private_data
;
646 struct cache_request
*rq
;
647 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
653 mutex_lock(&inode
->i_mutex
); /* protect against multiple concurrent
654 * readers on this file */
656 spin_lock(&queue_lock
);
657 /* need to find next request */
658 while (rp
->q
.list
.next
!= &cd
->queue
&&
659 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
661 struct list_head
*next
= rp
->q
.list
.next
;
662 list_move(&rp
->q
.list
, next
);
664 if (rp
->q
.list
.next
== &cd
->queue
) {
665 spin_unlock(&queue_lock
);
666 mutex_unlock(&inode
->i_mutex
);
670 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
671 BUG_ON(rq
->q
.reader
);
674 spin_unlock(&queue_lock
);
676 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
678 spin_lock(&queue_lock
);
679 list_move(&rp
->q
.list
, &rq
->q
.list
);
680 spin_unlock(&queue_lock
);
682 if (rp
->offset
+ count
> rq
->len
)
683 count
= rq
->len
- rp
->offset
;
685 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
688 if (rp
->offset
>= rq
->len
) {
690 spin_lock(&queue_lock
);
691 list_move(&rp
->q
.list
, &rq
->q
.list
);
692 spin_unlock(&queue_lock
);
697 if (rp
->offset
== 0) {
698 /* need to release rq */
699 spin_lock(&queue_lock
);
701 if (rq
->readers
== 0 &&
702 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
703 list_del(&rq
->q
.list
);
704 spin_unlock(&queue_lock
);
705 cache_put(rq
->item
, cd
);
709 spin_unlock(&queue_lock
);
713 mutex_unlock(&inode
->i_mutex
);
714 return err
? err
: count
;
717 static ssize_t
cache_do_downcall(char *kaddr
, const char __user
*buf
,
718 size_t count
, struct cache_detail
*cd
)
722 if (copy_from_user(kaddr
, buf
, count
))
725 ret
= cd
->cache_parse(cd
, kaddr
, count
);
731 static ssize_t
cache_slow_downcall(const char __user
*buf
,
732 size_t count
, struct cache_detail
*cd
)
734 static char write_buf
[8192]; /* protected by queue_io_mutex */
735 ssize_t ret
= -EINVAL
;
737 if (count
>= sizeof(write_buf
))
739 mutex_lock(&queue_io_mutex
);
740 ret
= cache_do_downcall(write_buf
, buf
, count
, cd
);
741 mutex_unlock(&queue_io_mutex
);
746 static ssize_t
cache_downcall(struct address_space
*mapping
,
747 const char __user
*buf
,
748 size_t count
, struct cache_detail
*cd
)
752 ssize_t ret
= -ENOMEM
;
754 if (count
>= PAGE_CACHE_SIZE
)
757 page
= find_or_create_page(mapping
, 0, GFP_KERNEL
);
762 ret
= cache_do_downcall(kaddr
, buf
, count
, cd
);
765 page_cache_release(page
);
768 return cache_slow_downcall(buf
, count
, cd
);
771 static ssize_t
cache_write(struct file
*filp
, const char __user
*buf
,
772 size_t count
, loff_t
*ppos
,
773 struct cache_detail
*cd
)
775 struct address_space
*mapping
= filp
->f_mapping
;
776 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
777 ssize_t ret
= -EINVAL
;
779 if (!cd
->cache_parse
)
782 mutex_lock(&inode
->i_mutex
);
783 ret
= cache_downcall(mapping
, buf
, count
, cd
);
784 mutex_unlock(&inode
->i_mutex
);
789 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
791 static unsigned int cache_poll(struct file
*filp
, poll_table
*wait
,
792 struct cache_detail
*cd
)
795 struct cache_reader
*rp
= filp
->private_data
;
796 struct cache_queue
*cq
;
798 poll_wait(filp
, &queue_wait
, wait
);
800 /* alway allow write */
801 mask
= POLL_OUT
| POLLWRNORM
;
806 spin_lock(&queue_lock
);
808 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
809 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
811 mask
|= POLLIN
| POLLRDNORM
;
814 spin_unlock(&queue_lock
);
818 static int cache_ioctl(struct inode
*ino
, struct file
*filp
,
819 unsigned int cmd
, unsigned long arg
,
820 struct cache_detail
*cd
)
823 struct cache_reader
*rp
= filp
->private_data
;
824 struct cache_queue
*cq
;
826 if (cmd
!= FIONREAD
|| !rp
)
829 spin_lock(&queue_lock
);
831 /* only find the length remaining in current request,
832 * or the length of the next request
834 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
835 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
837 struct cache_request
*cr
=
838 container_of(cq
, struct cache_request
, q
);
839 len
= cr
->len
- rp
->offset
;
842 spin_unlock(&queue_lock
);
844 return put_user(len
, (int __user
*)arg
);
847 static int cache_open(struct inode
*inode
, struct file
*filp
,
848 struct cache_detail
*cd
)
850 struct cache_reader
*rp
= NULL
;
852 if (!cd
|| !try_module_get(cd
->owner
))
854 nonseekable_open(inode
, filp
);
855 if (filp
->f_mode
& FMODE_READ
) {
856 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
861 atomic_inc(&cd
->readers
);
862 spin_lock(&queue_lock
);
863 list_add(&rp
->q
.list
, &cd
->queue
);
864 spin_unlock(&queue_lock
);
866 filp
->private_data
= rp
;
870 static int cache_release(struct inode
*inode
, struct file
*filp
,
871 struct cache_detail
*cd
)
873 struct cache_reader
*rp
= filp
->private_data
;
876 spin_lock(&queue_lock
);
878 struct cache_queue
*cq
;
879 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
880 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
882 container_of(cq
, struct cache_request
, q
)
888 list_del(&rp
->q
.list
);
889 spin_unlock(&queue_lock
);
891 filp
->private_data
= NULL
;
894 cd
->last_close
= get_seconds();
895 atomic_dec(&cd
->readers
);
897 module_put(cd
->owner
);
903 static void cache_dequeue(struct cache_detail
*detail
, struct cache_head
*ch
)
905 struct cache_queue
*cq
;
906 spin_lock(&queue_lock
);
907 list_for_each_entry(cq
, &detail
->queue
, list
)
909 struct cache_request
*cr
= container_of(cq
, struct cache_request
, q
);
912 if (cr
->readers
!= 0)
914 list_del(&cr
->q
.list
);
915 spin_unlock(&queue_lock
);
916 cache_put(cr
->item
, detail
);
921 spin_unlock(&queue_lock
);
925 * Support routines for text-based upcalls.
926 * Fields are separated by spaces.
927 * Fields are either mangled to quote space tab newline slosh with slosh
928 * or a hexified with a leading \x
929 * Record is terminated with newline.
933 void qword_add(char **bpp
, int *lp
, char *str
)
941 while ((c
=*str
++) && len
)
949 *bp
++ = '0' + ((c
& 0300)>>6);
950 *bp
++ = '0' + ((c
& 0070)>>3);
951 *bp
++ = '0' + ((c
& 0007)>>0);
959 if (c
|| len
<1) len
= -1;
967 EXPORT_SYMBOL_GPL(qword_add
);
969 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
980 while (blen
&& len
>= 2) {
981 unsigned char c
= *buf
++;
982 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
983 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
988 if (blen
|| len
<1) len
= -1;
996 EXPORT_SYMBOL_GPL(qword_addhex
);
998 static void warn_no_listener(struct cache_detail
*detail
)
1000 if (detail
->last_warn
!= detail
->last_close
) {
1001 detail
->last_warn
= detail
->last_close
;
1002 if (detail
->warn_no_listener
)
1003 detail
->warn_no_listener(detail
, detail
->last_close
!= 0);
1008 * register an upcall request to user-space and queue it up for read() by the
1011 * Each request is at most one page long.
1013 int sunrpc_cache_pipe_upcall(struct cache_detail
*detail
, struct cache_head
*h
,
1014 void (*cache_request
)(struct cache_detail
*,
1015 struct cache_head
*,
1021 struct cache_request
*crq
;
1025 if (atomic_read(&detail
->readers
) == 0 &&
1026 detail
->last_close
< get_seconds() - 30) {
1027 warn_no_listener(detail
);
1031 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1035 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1041 bp
= buf
; len
= PAGE_SIZE
;
1043 cache_request(detail
, h
, &bp
, &len
);
1051 crq
->item
= cache_get(h
);
1053 crq
->len
= PAGE_SIZE
- len
;
1055 spin_lock(&queue_lock
);
1056 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1057 spin_unlock(&queue_lock
);
1058 wake_up(&queue_wait
);
1061 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall
);
1064 * parse a message from user-space and pass it
1065 * to an appropriate cache
1066 * Messages are, like requests, separated into fields by
1067 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1070 * reply cachename expiry key ... content....
1072 * key and content are both parsed by cache
1075 #define isodigit(c) (isdigit(c) && c <= '7')
1076 int qword_get(char **bpp
, char *dest
, int bufsize
)
1078 /* return bytes copied, or -1 on error */
1082 while (*bp
== ' ') bp
++;
1084 if (bp
[0] == '\\' && bp
[1] == 'x') {
1087 while (isxdigit(bp
[0]) && isxdigit(bp
[1]) && len
< bufsize
) {
1088 int byte
= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1091 byte
|= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1097 /* text with \nnn octal quoting */
1098 while (*bp
!= ' ' && *bp
!= '\n' && *bp
&& len
< bufsize
-1) {
1100 isodigit(bp
[1]) && (bp
[1] <= '3') &&
1103 int byte
= (*++bp
-'0');
1105 byte
= (byte
<< 3) | (*bp
++ - '0');
1106 byte
= (byte
<< 3) | (*bp
++ - '0');
1116 if (*bp
!= ' ' && *bp
!= '\n' && *bp
!= '\0')
1118 while (*bp
== ' ') bp
++;
1123 EXPORT_SYMBOL_GPL(qword_get
);
1127 * support /proc/sunrpc/cache/$CACHENAME/content
1129 * We call ->cache_show passing NULL for the item to
1130 * get a header, then pass each real item in the cache
1134 struct cache_detail
*cd
;
1137 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1138 __acquires(cd
->hash_lock
)
1141 unsigned hash
, entry
;
1142 struct cache_head
*ch
;
1143 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1146 read_lock(&cd
->hash_lock
);
1148 return SEQ_START_TOKEN
;
1150 entry
= n
& ((1LL<<32) - 1);
1152 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1155 n
&= ~((1LL<<32) - 1);
1159 } while(hash
< cd
->hash_size
&&
1160 cd
->hash_table
[hash
]==NULL
);
1161 if (hash
>= cd
->hash_size
)
1164 return cd
->hash_table
[hash
];
1167 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1169 struct cache_head
*ch
= p
;
1170 int hash
= (*pos
>> 32);
1171 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1173 if (p
== SEQ_START_TOKEN
)
1175 else if (ch
->next
== NULL
) {
1182 *pos
&= ~((1LL<<32) - 1);
1183 while (hash
< cd
->hash_size
&&
1184 cd
->hash_table
[hash
] == NULL
) {
1188 if (hash
>= cd
->hash_size
)
1191 return cd
->hash_table
[hash
];
1194 static void c_stop(struct seq_file
*m
, void *p
)
1195 __releases(cd
->hash_lock
)
1197 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1198 read_unlock(&cd
->hash_lock
);
1201 static int c_show(struct seq_file
*m
, void *p
)
1203 struct cache_head
*cp
= p
;
1204 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1206 if (p
== SEQ_START_TOKEN
)
1207 return cd
->cache_show(m
, cd
, NULL
);
1210 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1211 cp
->expiry_time
, atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1213 if (cache_check(cd
, cp
, NULL
))
1214 /* cache_check does a cache_put on failure */
1215 seq_printf(m
, "# ");
1219 return cd
->cache_show(m
, cd
, cp
);
1222 static const struct seq_operations cache_content_op
= {
1229 static int content_open(struct inode
*inode
, struct file
*file
,
1230 struct cache_detail
*cd
)
1234 if (!cd
|| !try_module_get(cd
->owner
))
1236 han
= __seq_open_private(file
, &cache_content_op
, sizeof(*han
));
1238 module_put(cd
->owner
);
1246 static int content_release(struct inode
*inode
, struct file
*file
,
1247 struct cache_detail
*cd
)
1249 int ret
= seq_release_private(inode
, file
);
1250 module_put(cd
->owner
);
1254 static int open_flush(struct inode
*inode
, struct file
*file
,
1255 struct cache_detail
*cd
)
1257 if (!cd
|| !try_module_get(cd
->owner
))
1259 return nonseekable_open(inode
, file
);
1262 static int release_flush(struct inode
*inode
, struct file
*file
,
1263 struct cache_detail
*cd
)
1265 module_put(cd
->owner
);
1269 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1270 size_t count
, loff_t
*ppos
,
1271 struct cache_detail
*cd
)
1274 unsigned long p
= *ppos
;
1277 sprintf(tbuf
, "%lu\n", cd
->flush_time
);
1284 if (copy_to_user(buf
, (void*)(tbuf
+p
), len
))
1290 static ssize_t
write_flush(struct file
*file
, const char __user
*buf
,
1291 size_t count
, loff_t
*ppos
,
1292 struct cache_detail
*cd
)
1297 if (*ppos
|| count
> sizeof(tbuf
)-1)
1299 if (copy_from_user(tbuf
, buf
, count
))
1302 flushtime
= simple_strtoul(tbuf
, &ep
, 0);
1303 if (*ep
&& *ep
!= '\n')
1306 cd
->flush_time
= flushtime
;
1307 cd
->nextcheck
= get_seconds();
1314 static ssize_t
cache_read_procfs(struct file
*filp
, char __user
*buf
,
1315 size_t count
, loff_t
*ppos
)
1317 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1319 return cache_read(filp
, buf
, count
, ppos
, cd
);
1322 static ssize_t
cache_write_procfs(struct file
*filp
, const char __user
*buf
,
1323 size_t count
, loff_t
*ppos
)
1325 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1327 return cache_write(filp
, buf
, count
, ppos
, cd
);
1330 static unsigned int cache_poll_procfs(struct file
*filp
, poll_table
*wait
)
1332 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1334 return cache_poll(filp
, wait
, cd
);
1337 static int cache_ioctl_procfs(struct inode
*inode
, struct file
*filp
,
1338 unsigned int cmd
, unsigned long arg
)
1340 struct cache_detail
*cd
= PDE(inode
)->data
;
1342 return cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1345 static int cache_open_procfs(struct inode
*inode
, struct file
*filp
)
1347 struct cache_detail
*cd
= PDE(inode
)->data
;
1349 return cache_open(inode
, filp
, cd
);
1352 static int cache_release_procfs(struct inode
*inode
, struct file
*filp
)
1354 struct cache_detail
*cd
= PDE(inode
)->data
;
1356 return cache_release(inode
, filp
, cd
);
1359 static const struct file_operations cache_file_operations_procfs
= {
1360 .owner
= THIS_MODULE
,
1361 .llseek
= no_llseek
,
1362 .read
= cache_read_procfs
,
1363 .write
= cache_write_procfs
,
1364 .poll
= cache_poll_procfs
,
1365 .ioctl
= cache_ioctl_procfs
, /* for FIONREAD */
1366 .open
= cache_open_procfs
,
1367 .release
= cache_release_procfs
,
1370 static int content_open_procfs(struct inode
*inode
, struct file
*filp
)
1372 struct cache_detail
*cd
= PDE(inode
)->data
;
1374 return content_open(inode
, filp
, cd
);
1377 static int content_release_procfs(struct inode
*inode
, struct file
*filp
)
1379 struct cache_detail
*cd
= PDE(inode
)->data
;
1381 return content_release(inode
, filp
, cd
);
1384 static const struct file_operations content_file_operations_procfs
= {
1385 .open
= content_open_procfs
,
1387 .llseek
= seq_lseek
,
1388 .release
= content_release_procfs
,
1391 static int open_flush_procfs(struct inode
*inode
, struct file
*filp
)
1393 struct cache_detail
*cd
= PDE(inode
)->data
;
1395 return open_flush(inode
, filp
, cd
);
1398 static int release_flush_procfs(struct inode
*inode
, struct file
*filp
)
1400 struct cache_detail
*cd
= PDE(inode
)->data
;
1402 return release_flush(inode
, filp
, cd
);
1405 static ssize_t
read_flush_procfs(struct file
*filp
, char __user
*buf
,
1406 size_t count
, loff_t
*ppos
)
1408 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1410 return read_flush(filp
, buf
, count
, ppos
, cd
);
1413 static ssize_t
write_flush_procfs(struct file
*filp
,
1414 const char __user
*buf
,
1415 size_t count
, loff_t
*ppos
)
1417 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1419 return write_flush(filp
, buf
, count
, ppos
, cd
);
1422 static const struct file_operations cache_flush_operations_procfs
= {
1423 .open
= open_flush_procfs
,
1424 .read
= read_flush_procfs
,
1425 .write
= write_flush_procfs
,
1426 .release
= release_flush_procfs
,
1429 static void remove_cache_proc_entries(struct cache_detail
*cd
)
1431 if (cd
->u
.procfs
.proc_ent
== NULL
)
1433 if (cd
->u
.procfs
.flush_ent
)
1434 remove_proc_entry("flush", cd
->u
.procfs
.proc_ent
);
1435 if (cd
->u
.procfs
.channel_ent
)
1436 remove_proc_entry("channel", cd
->u
.procfs
.proc_ent
);
1437 if (cd
->u
.procfs
.content_ent
)
1438 remove_proc_entry("content", cd
->u
.procfs
.proc_ent
);
1439 cd
->u
.procfs
.proc_ent
= NULL
;
1440 remove_proc_entry(cd
->name
, proc_net_rpc
);
1443 #ifdef CONFIG_PROC_FS
1444 static int create_cache_proc_entries(struct cache_detail
*cd
)
1446 struct proc_dir_entry
*p
;
1448 cd
->u
.procfs
.proc_ent
= proc_mkdir(cd
->name
, proc_net_rpc
);
1449 if (cd
->u
.procfs
.proc_ent
== NULL
)
1451 cd
->u
.procfs
.channel_ent
= NULL
;
1452 cd
->u
.procfs
.content_ent
= NULL
;
1454 p
= proc_create_data("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
1455 cd
->u
.procfs
.proc_ent
,
1456 &cache_flush_operations_procfs
, cd
);
1457 cd
->u
.procfs
.flush_ent
= p
;
1461 if (cd
->cache_upcall
|| cd
->cache_parse
) {
1462 p
= proc_create_data("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
1463 cd
->u
.procfs
.proc_ent
,
1464 &cache_file_operations_procfs
, cd
);
1465 cd
->u
.procfs
.channel_ent
= p
;
1469 if (cd
->cache_show
) {
1470 p
= proc_create_data("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
1471 cd
->u
.procfs
.proc_ent
,
1472 &content_file_operations_procfs
, cd
);
1473 cd
->u
.procfs
.content_ent
= p
;
1479 remove_cache_proc_entries(cd
);
1482 #else /* CONFIG_PROC_FS */
1483 static int create_cache_proc_entries(struct cache_detail
*cd
)
1489 int cache_register(struct cache_detail
*cd
)
1493 sunrpc_init_cache_detail(cd
);
1494 ret
= create_cache_proc_entries(cd
);
1496 sunrpc_destroy_cache_detail(cd
);
1499 EXPORT_SYMBOL_GPL(cache_register
);
1501 void cache_unregister(struct cache_detail
*cd
)
1503 remove_cache_proc_entries(cd
);
1504 sunrpc_destroy_cache_detail(cd
);
1506 EXPORT_SYMBOL_GPL(cache_unregister
);
1508 static ssize_t
cache_read_pipefs(struct file
*filp
, char __user
*buf
,
1509 size_t count
, loff_t
*ppos
)
1511 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1513 return cache_read(filp
, buf
, count
, ppos
, cd
);
1516 static ssize_t
cache_write_pipefs(struct file
*filp
, const char __user
*buf
,
1517 size_t count
, loff_t
*ppos
)
1519 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1521 return cache_write(filp
, buf
, count
, ppos
, cd
);
1524 static unsigned int cache_poll_pipefs(struct file
*filp
, poll_table
*wait
)
1526 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1528 return cache_poll(filp
, wait
, cd
);
1531 static int cache_ioctl_pipefs(struct inode
*inode
, struct file
*filp
,
1532 unsigned int cmd
, unsigned long arg
)
1534 struct cache_detail
*cd
= RPC_I(inode
)->private;
1536 return cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1539 static int cache_open_pipefs(struct inode
*inode
, struct file
*filp
)
1541 struct cache_detail
*cd
= RPC_I(inode
)->private;
1543 return cache_open(inode
, filp
, cd
);
1546 static int cache_release_pipefs(struct inode
*inode
, struct file
*filp
)
1548 struct cache_detail
*cd
= RPC_I(inode
)->private;
1550 return cache_release(inode
, filp
, cd
);
1553 const struct file_operations cache_file_operations_pipefs
= {
1554 .owner
= THIS_MODULE
,
1555 .llseek
= no_llseek
,
1556 .read
= cache_read_pipefs
,
1557 .write
= cache_write_pipefs
,
1558 .poll
= cache_poll_pipefs
,
1559 .ioctl
= cache_ioctl_pipefs
, /* for FIONREAD */
1560 .open
= cache_open_pipefs
,
1561 .release
= cache_release_pipefs
,
1564 static int content_open_pipefs(struct inode
*inode
, struct file
*filp
)
1566 struct cache_detail
*cd
= RPC_I(inode
)->private;
1568 return content_open(inode
, filp
, cd
);
1571 static int content_release_pipefs(struct inode
*inode
, struct file
*filp
)
1573 struct cache_detail
*cd
= RPC_I(inode
)->private;
1575 return content_release(inode
, filp
, cd
);
1578 const struct file_operations content_file_operations_pipefs
= {
1579 .open
= content_open_pipefs
,
1581 .llseek
= seq_lseek
,
1582 .release
= content_release_pipefs
,
1585 static int open_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1587 struct cache_detail
*cd
= RPC_I(inode
)->private;
1589 return open_flush(inode
, filp
, cd
);
1592 static int release_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1594 struct cache_detail
*cd
= RPC_I(inode
)->private;
1596 return release_flush(inode
, filp
, cd
);
1599 static ssize_t
read_flush_pipefs(struct file
*filp
, char __user
*buf
,
1600 size_t count
, loff_t
*ppos
)
1602 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1604 return read_flush(filp
, buf
, count
, ppos
, cd
);
1607 static ssize_t
write_flush_pipefs(struct file
*filp
,
1608 const char __user
*buf
,
1609 size_t count
, loff_t
*ppos
)
1611 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1613 return write_flush(filp
, buf
, count
, ppos
, cd
);
1616 const struct file_operations cache_flush_operations_pipefs
= {
1617 .open
= open_flush_pipefs
,
1618 .read
= read_flush_pipefs
,
1619 .write
= write_flush_pipefs
,
1620 .release
= release_flush_pipefs
,
1623 int sunrpc_cache_register_pipefs(struct dentry
*parent
,
1624 const char *name
, mode_t umode
,
1625 struct cache_detail
*cd
)
1631 sunrpc_init_cache_detail(cd
);
1633 q
.len
= strlen(name
);
1634 q
.hash
= full_name_hash(q
.name
, q
.len
);
1635 dir
= rpc_create_cache_dir(parent
, &q
, umode
, cd
);
1637 cd
->u
.pipefs
.dir
= dir
;
1639 sunrpc_destroy_cache_detail(cd
);
1644 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs
);
1646 void sunrpc_cache_unregister_pipefs(struct cache_detail
*cd
)
1648 rpc_remove_cache_dir(cd
->u
.pipefs
.dir
);
1649 cd
->u
.pipefs
.dir
= NULL
;
1650 sunrpc_destroy_cache_detail(cd
);
1652 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs
);