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>
38 #define RPCDBG_FACILITY RPCDBG_CACHE
40 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
);
41 static void cache_revisit_request(struct cache_head
*item
);
43 static void cache_init(struct cache_head
*h
)
45 time_t now
= get_seconds();
49 h
->expiry_time
= now
+ CACHE_NEW_EXPIRY
;
50 h
->last_refresh
= now
;
53 static inline int cache_is_expired(struct cache_detail
*detail
, struct cache_head
*h
)
55 return (h
->expiry_time
< get_seconds()) ||
56 (detail
->flush_time
> h
->last_refresh
);
59 struct cache_head
*sunrpc_cache_lookup(struct cache_detail
*detail
,
60 struct cache_head
*key
, int hash
)
62 struct cache_head
**head
, **hp
;
63 struct cache_head
*new = NULL
, *freeme
= NULL
;
65 head
= &detail
->hash_table
[hash
];
67 read_lock(&detail
->hash_lock
);
69 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
70 struct cache_head
*tmp
= *hp
;
71 if (detail
->match(tmp
, key
)) {
72 if (cache_is_expired(detail
, tmp
))
73 /* This entry is expired, we will discard it. */
76 read_unlock(&detail
->hash_lock
);
80 read_unlock(&detail
->hash_lock
);
81 /* Didn't find anything, insert an empty entry */
83 new = detail
->alloc();
86 /* must fully initialise 'new', else
87 * we might get lose if we need to
91 detail
->init(new, key
);
93 write_lock(&detail
->hash_lock
);
95 /* check if entry appeared while we slept */
96 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
97 struct cache_head
*tmp
= *hp
;
98 if (detail
->match(tmp
, key
)) {
99 if (cache_is_expired(detail
, tmp
)) {
107 write_unlock(&detail
->hash_lock
);
108 cache_put(new, detail
);
116 write_unlock(&detail
->hash_lock
);
119 cache_put(freeme
, detail
);
122 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup
);
125 static void cache_dequeue(struct cache_detail
*detail
, struct cache_head
*ch
);
127 static void cache_fresh_locked(struct cache_head
*head
, time_t expiry
)
129 head
->expiry_time
= expiry
;
130 head
->last_refresh
= get_seconds();
131 set_bit(CACHE_VALID
, &head
->flags
);
134 static void cache_fresh_unlocked(struct cache_head
*head
,
135 struct cache_detail
*detail
)
137 if (test_and_clear_bit(CACHE_PENDING
, &head
->flags
)) {
138 cache_revisit_request(head
);
139 cache_dequeue(detail
, head
);
143 struct cache_head
*sunrpc_cache_update(struct cache_detail
*detail
,
144 struct cache_head
*new, struct cache_head
*old
, int hash
)
146 /* The 'old' entry is to be replaced by 'new'.
147 * If 'old' is not VALID, we update it directly,
148 * otherwise we need to replace it
150 struct cache_head
**head
;
151 struct cache_head
*tmp
;
153 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
154 write_lock(&detail
->hash_lock
);
155 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
156 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
157 set_bit(CACHE_NEGATIVE
, &old
->flags
);
159 detail
->update(old
, new);
160 cache_fresh_locked(old
, new->expiry_time
);
161 write_unlock(&detail
->hash_lock
);
162 cache_fresh_unlocked(old
, detail
);
165 write_unlock(&detail
->hash_lock
);
167 /* We need to insert a new entry */
168 tmp
= detail
->alloc();
170 cache_put(old
, detail
);
174 detail
->init(tmp
, old
);
175 head
= &detail
->hash_table
[hash
];
177 write_lock(&detail
->hash_lock
);
178 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
179 set_bit(CACHE_NEGATIVE
, &tmp
->flags
);
181 detail
->update(tmp
, new);
186 cache_fresh_locked(tmp
, new->expiry_time
);
187 cache_fresh_locked(old
, 0);
188 write_unlock(&detail
->hash_lock
);
189 cache_fresh_unlocked(tmp
, detail
);
190 cache_fresh_unlocked(old
, detail
);
191 cache_put(old
, detail
);
194 EXPORT_SYMBOL_GPL(sunrpc_cache_update
);
196 static int cache_make_upcall(struct cache_detail
*cd
, struct cache_head
*h
)
198 if (!cd
->cache_upcall
)
200 return cd
->cache_upcall(cd
, h
);
203 static inline int cache_is_valid(struct cache_detail
*detail
, struct cache_head
*h
)
205 if (!test_bit(CACHE_VALID
, &h
->flags
))
209 if (test_bit(CACHE_NEGATIVE
, &h
->flags
))
217 * This is the generic cache management routine for all
218 * the authentication caches.
219 * It checks the currency of a cache item and will (later)
220 * initiate an upcall to fill it if needed.
223 * Returns 0 if the cache_head can be used, or cache_puts it and returns
224 * -EAGAIN if upcall is pending and request has been queued
225 * -ETIMEDOUT if upcall failed or request could not be queue or
226 * upcall completed but item is still invalid (implying that
227 * the cache item has been replaced with a newer one).
228 * -ENOENT if cache entry was negative
230 int cache_check(struct cache_detail
*detail
,
231 struct cache_head
*h
, struct cache_req
*rqstp
)
234 long refresh_age
, age
;
236 /* First decide return status as best we can */
237 rv
= cache_is_valid(detail
, h
);
239 /* now see if we want to start an upcall */
240 refresh_age
= (h
->expiry_time
- h
->last_refresh
);
241 age
= get_seconds() - h
->last_refresh
;
246 } else if (rv
== -EAGAIN
|| age
> refresh_age
/2) {
247 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
249 if (!test_and_set_bit(CACHE_PENDING
, &h
->flags
)) {
250 switch (cache_make_upcall(detail
, h
)) {
252 clear_bit(CACHE_PENDING
, &h
->flags
);
253 cache_revisit_request(h
);
255 set_bit(CACHE_NEGATIVE
, &h
->flags
);
256 cache_fresh_locked(h
, get_seconds()+CACHE_NEW_EXPIRY
);
257 cache_fresh_unlocked(h
, detail
);
263 clear_bit(CACHE_PENDING
, &h
->flags
);
264 cache_revisit_request(h
);
271 if (cache_defer_req(rqstp
, h
) < 0) {
272 /* Request is not deferred */
273 rv
= cache_is_valid(detail
, h
);
279 cache_put(h
, detail
);
282 EXPORT_SYMBOL_GPL(cache_check
);
285 * caches need to be periodically cleaned.
286 * For this we maintain a list of cache_detail and
287 * a current pointer into that list and into the table
290 * Each time clean_cache is called it finds the next non-empty entry
291 * in the current table and walks the list in that entry
292 * looking for entries that can be removed.
294 * An entry gets removed if:
295 * - The expiry is before current time
296 * - The last_refresh time is before the flush_time for that cache
298 * later we might drop old entries with non-NEVER expiry if that table
299 * is getting 'full' for some definition of 'full'
301 * The question of "how often to scan a table" is an interesting one
302 * and is answered in part by the use of the "nextcheck" field in the
304 * When a scan of a table begins, the nextcheck field is set to a time
305 * that is well into the future.
306 * While scanning, if an expiry time is found that is earlier than the
307 * current nextcheck time, nextcheck is set to that expiry time.
308 * If the flush_time is ever set to a time earlier than the nextcheck
309 * time, the nextcheck time is then set to that flush_time.
311 * A table is then only scanned if the current time is at least
312 * the nextcheck time.
316 static LIST_HEAD(cache_list
);
317 static DEFINE_SPINLOCK(cache_list_lock
);
318 static struct cache_detail
*current_detail
;
319 static int current_index
;
321 static void do_cache_clean(struct work_struct
*work
);
322 static struct delayed_work cache_cleaner
;
324 static void sunrpc_init_cache_detail(struct cache_detail
*cd
)
326 rwlock_init(&cd
->hash_lock
);
327 INIT_LIST_HEAD(&cd
->queue
);
328 spin_lock(&cache_list_lock
);
331 atomic_set(&cd
->readers
, 0);
334 list_add(&cd
->others
, &cache_list
);
335 spin_unlock(&cache_list_lock
);
337 /* start the cleaning process */
338 schedule_delayed_work(&cache_cleaner
, 0);
341 static void sunrpc_destroy_cache_detail(struct cache_detail
*cd
)
344 spin_lock(&cache_list_lock
);
345 write_lock(&cd
->hash_lock
);
346 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
347 write_unlock(&cd
->hash_lock
);
348 spin_unlock(&cache_list_lock
);
351 if (current_detail
== cd
)
352 current_detail
= NULL
;
353 list_del_init(&cd
->others
);
354 write_unlock(&cd
->hash_lock
);
355 spin_unlock(&cache_list_lock
);
356 if (list_empty(&cache_list
)) {
357 /* module must be being unloaded so its safe to kill the worker */
358 cancel_delayed_work_sync(&cache_cleaner
);
362 printk(KERN_ERR
"nfsd: failed to unregister %s cache\n", cd
->name
);
365 /* clean cache tries to find something to clean
367 * It returns 1 if it cleaned something,
368 * 0 if it didn't find anything this time
369 * -1 if it fell off the end of the list.
371 static int cache_clean(void)
374 struct list_head
*next
;
376 spin_lock(&cache_list_lock
);
378 /* find a suitable table if we don't already have one */
379 while (current_detail
== NULL
||
380 current_index
>= current_detail
->hash_size
) {
382 next
= current_detail
->others
.next
;
384 next
= cache_list
.next
;
385 if (next
== &cache_list
) {
386 current_detail
= NULL
;
387 spin_unlock(&cache_list_lock
);
390 current_detail
= list_entry(next
, struct cache_detail
, others
);
391 if (current_detail
->nextcheck
> get_seconds())
392 current_index
= current_detail
->hash_size
;
395 current_detail
->nextcheck
= get_seconds()+30*60;
399 /* find a non-empty bucket in the table */
400 while (current_detail
&&
401 current_index
< current_detail
->hash_size
&&
402 current_detail
->hash_table
[current_index
] == NULL
)
405 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
407 if (current_detail
&& current_index
< current_detail
->hash_size
) {
408 struct cache_head
*ch
, **cp
;
409 struct cache_detail
*d
;
411 write_lock(¤t_detail
->hash_lock
);
413 /* Ok, now to clean this strand */
415 cp
= & current_detail
->hash_table
[current_index
];
416 for (ch
= *cp
; ch
; cp
= & ch
->next
, ch
= *cp
) {
417 if (current_detail
->nextcheck
> ch
->expiry_time
)
418 current_detail
->nextcheck
= ch
->expiry_time
+1;
419 if (!cache_is_expired(current_detail
, ch
))
424 current_detail
->entries
--;
429 write_unlock(¤t_detail
->hash_lock
);
433 spin_unlock(&cache_list_lock
);
435 if (test_and_clear_bit(CACHE_PENDING
, &ch
->flags
))
436 cache_dequeue(current_detail
, ch
);
437 cache_revisit_request(ch
);
441 spin_unlock(&cache_list_lock
);
447 * We want to regularly clean the cache, so we need to schedule some work ...
449 static void do_cache_clean(struct work_struct
*work
)
452 if (cache_clean() == -1)
453 delay
= round_jiffies_relative(30*HZ
);
455 if (list_empty(&cache_list
))
459 schedule_delayed_work(&cache_cleaner
, delay
);
464 * Clean all caches promptly. This just calls cache_clean
465 * repeatedly until we are sure that every cache has had a chance to
468 void cache_flush(void)
470 while (cache_clean() != -1)
472 while (cache_clean() != -1)
475 EXPORT_SYMBOL_GPL(cache_flush
);
477 void cache_purge(struct cache_detail
*detail
)
479 detail
->flush_time
= LONG_MAX
;
480 detail
->nextcheck
= get_seconds();
482 detail
->flush_time
= 1;
484 EXPORT_SYMBOL_GPL(cache_purge
);
488 * Deferral and Revisiting of Requests.
490 * If a cache lookup finds a pending entry, we
491 * need to defer the request and revisit it later.
492 * All deferred requests are stored in a hash table,
493 * indexed by "struct cache_head *".
494 * As it may be wasteful to store a whole request
495 * structure, we allow the request to provide a
496 * deferred form, which must contain a
497 * 'struct cache_deferred_req'
498 * This cache_deferred_req contains a method to allow
499 * it to be revisited when cache info is available
502 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
503 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
505 #define DFR_MAX 300 /* ??? */
507 static DEFINE_SPINLOCK(cache_defer_lock
);
508 static LIST_HEAD(cache_defer_list
);
509 static struct list_head cache_defer_hash
[DFR_HASHSIZE
];
510 static int cache_defer_cnt
;
512 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
514 struct cache_deferred_req
*dreq
, *discard
;
515 int hash
= DFR_HASH(item
);
517 if (cache_defer_cnt
>= DFR_MAX
) {
518 /* too much in the cache, randomly drop this one,
519 * or continue and drop the oldest below
524 dreq
= req
->defer(req
);
530 spin_lock(&cache_defer_lock
);
532 list_add(&dreq
->recent
, &cache_defer_list
);
534 if (cache_defer_hash
[hash
].next
== NULL
)
535 INIT_LIST_HEAD(&cache_defer_hash
[hash
]);
536 list_add(&dreq
->hash
, &cache_defer_hash
[hash
]);
538 /* it is in, now maybe clean up */
540 if (++cache_defer_cnt
> DFR_MAX
) {
541 discard
= list_entry(cache_defer_list
.prev
,
542 struct cache_deferred_req
, recent
);
543 list_del_init(&discard
->recent
);
544 list_del_init(&discard
->hash
);
547 spin_unlock(&cache_defer_lock
);
550 /* there was one too many */
551 discard
->revisit(discard
, 1);
553 if (!test_bit(CACHE_PENDING
, &item
->flags
)) {
554 /* must have just been validated... */
555 cache_revisit_request(item
);
561 static void cache_revisit_request(struct cache_head
*item
)
563 struct cache_deferred_req
*dreq
;
564 struct list_head pending
;
566 struct list_head
*lp
;
567 int hash
= DFR_HASH(item
);
569 INIT_LIST_HEAD(&pending
);
570 spin_lock(&cache_defer_lock
);
572 lp
= cache_defer_hash
[hash
].next
;
574 while (lp
!= &cache_defer_hash
[hash
]) {
575 dreq
= list_entry(lp
, struct cache_deferred_req
, hash
);
577 if (dreq
->item
== item
) {
578 list_del_init(&dreq
->hash
);
579 list_move(&dreq
->recent
, &pending
);
584 spin_unlock(&cache_defer_lock
);
586 while (!list_empty(&pending
)) {
587 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
588 list_del_init(&dreq
->recent
);
589 dreq
->revisit(dreq
, 0);
593 void cache_clean_deferred(void *owner
)
595 struct cache_deferred_req
*dreq
, *tmp
;
596 struct list_head pending
;
599 INIT_LIST_HEAD(&pending
);
600 spin_lock(&cache_defer_lock
);
602 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
603 if (dreq
->owner
== owner
) {
604 list_del_init(&dreq
->hash
);
605 list_move(&dreq
->recent
, &pending
);
609 spin_unlock(&cache_defer_lock
);
611 while (!list_empty(&pending
)) {
612 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
613 list_del_init(&dreq
->recent
);
614 dreq
->revisit(dreq
, 1);
619 * communicate with user-space
621 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
622 * On read, you get a full request, or block.
623 * On write, an update request is processed.
624 * Poll works if anything to read, and always allows write.
626 * Implemented by linked list of requests. Each open file has
627 * a ->private that also exists in this list. New requests are added
628 * to the end and may wakeup and preceding readers.
629 * New readers are added to the head. If, on read, an item is found with
630 * CACHE_UPCALLING clear, we free it from the list.
634 static DEFINE_SPINLOCK(queue_lock
);
635 static DEFINE_MUTEX(queue_io_mutex
);
638 struct list_head list
;
639 int reader
; /* if 0, then request */
641 struct cache_request
{
642 struct cache_queue q
;
643 struct cache_head
*item
;
648 struct cache_reader
{
649 struct cache_queue q
;
650 int offset
; /* if non-0, we have a refcnt on next request */
653 static ssize_t
cache_read(struct file
*filp
, char __user
*buf
, size_t count
,
654 loff_t
*ppos
, struct cache_detail
*cd
)
656 struct cache_reader
*rp
= filp
->private_data
;
657 struct cache_request
*rq
;
658 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
664 mutex_lock(&inode
->i_mutex
); /* protect against multiple concurrent
665 * readers on this file */
667 spin_lock(&queue_lock
);
668 /* need to find next request */
669 while (rp
->q
.list
.next
!= &cd
->queue
&&
670 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
672 struct list_head
*next
= rp
->q
.list
.next
;
673 list_move(&rp
->q
.list
, next
);
675 if (rp
->q
.list
.next
== &cd
->queue
) {
676 spin_unlock(&queue_lock
);
677 mutex_unlock(&inode
->i_mutex
);
681 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
682 BUG_ON(rq
->q
.reader
);
685 spin_unlock(&queue_lock
);
687 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
689 spin_lock(&queue_lock
);
690 list_move(&rp
->q
.list
, &rq
->q
.list
);
691 spin_unlock(&queue_lock
);
693 if (rp
->offset
+ count
> rq
->len
)
694 count
= rq
->len
- rp
->offset
;
696 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
699 if (rp
->offset
>= rq
->len
) {
701 spin_lock(&queue_lock
);
702 list_move(&rp
->q
.list
, &rq
->q
.list
);
703 spin_unlock(&queue_lock
);
708 if (rp
->offset
== 0) {
709 /* need to release rq */
710 spin_lock(&queue_lock
);
712 if (rq
->readers
== 0 &&
713 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
714 list_del(&rq
->q
.list
);
715 spin_unlock(&queue_lock
);
716 cache_put(rq
->item
, cd
);
720 spin_unlock(&queue_lock
);
724 mutex_unlock(&inode
->i_mutex
);
725 return err
? err
: count
;
728 static ssize_t
cache_do_downcall(char *kaddr
, const char __user
*buf
,
729 size_t count
, struct cache_detail
*cd
)
733 if (copy_from_user(kaddr
, buf
, count
))
736 ret
= cd
->cache_parse(cd
, kaddr
, count
);
742 static ssize_t
cache_slow_downcall(const char __user
*buf
,
743 size_t count
, struct cache_detail
*cd
)
745 static char write_buf
[8192]; /* protected by queue_io_mutex */
746 ssize_t ret
= -EINVAL
;
748 if (count
>= sizeof(write_buf
))
750 mutex_lock(&queue_io_mutex
);
751 ret
= cache_do_downcall(write_buf
, buf
, count
, cd
);
752 mutex_unlock(&queue_io_mutex
);
757 static ssize_t
cache_downcall(struct address_space
*mapping
,
758 const char __user
*buf
,
759 size_t count
, struct cache_detail
*cd
)
763 ssize_t ret
= -ENOMEM
;
765 if (count
>= PAGE_CACHE_SIZE
)
768 page
= find_or_create_page(mapping
, 0, GFP_KERNEL
);
773 ret
= cache_do_downcall(kaddr
, buf
, count
, cd
);
776 page_cache_release(page
);
779 return cache_slow_downcall(buf
, count
, cd
);
782 static ssize_t
cache_write(struct file
*filp
, const char __user
*buf
,
783 size_t count
, loff_t
*ppos
,
784 struct cache_detail
*cd
)
786 struct address_space
*mapping
= filp
->f_mapping
;
787 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
788 ssize_t ret
= -EINVAL
;
790 if (!cd
->cache_parse
)
793 mutex_lock(&inode
->i_mutex
);
794 ret
= cache_downcall(mapping
, buf
, count
, cd
);
795 mutex_unlock(&inode
->i_mutex
);
800 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
802 static unsigned int cache_poll(struct file
*filp
, poll_table
*wait
,
803 struct cache_detail
*cd
)
806 struct cache_reader
*rp
= filp
->private_data
;
807 struct cache_queue
*cq
;
809 poll_wait(filp
, &queue_wait
, wait
);
811 /* alway allow write */
812 mask
= POLL_OUT
| POLLWRNORM
;
817 spin_lock(&queue_lock
);
819 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
820 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
822 mask
|= POLLIN
| POLLRDNORM
;
825 spin_unlock(&queue_lock
);
829 static int cache_ioctl(struct inode
*ino
, struct file
*filp
,
830 unsigned int cmd
, unsigned long arg
,
831 struct cache_detail
*cd
)
834 struct cache_reader
*rp
= filp
->private_data
;
835 struct cache_queue
*cq
;
837 if (cmd
!= FIONREAD
|| !rp
)
840 spin_lock(&queue_lock
);
842 /* only find the length remaining in current request,
843 * or the length of the next request
845 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
846 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
848 struct cache_request
*cr
=
849 container_of(cq
, struct cache_request
, q
);
850 len
= cr
->len
- rp
->offset
;
853 spin_unlock(&queue_lock
);
855 return put_user(len
, (int __user
*)arg
);
858 static int cache_open(struct inode
*inode
, struct file
*filp
,
859 struct cache_detail
*cd
)
861 struct cache_reader
*rp
= NULL
;
863 if (!cd
|| !try_module_get(cd
->owner
))
865 nonseekable_open(inode
, filp
);
866 if (filp
->f_mode
& FMODE_READ
) {
867 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
872 atomic_inc(&cd
->readers
);
873 spin_lock(&queue_lock
);
874 list_add(&rp
->q
.list
, &cd
->queue
);
875 spin_unlock(&queue_lock
);
877 filp
->private_data
= rp
;
881 static int cache_release(struct inode
*inode
, struct file
*filp
,
882 struct cache_detail
*cd
)
884 struct cache_reader
*rp
= filp
->private_data
;
887 spin_lock(&queue_lock
);
889 struct cache_queue
*cq
;
890 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
891 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
893 container_of(cq
, struct cache_request
, q
)
899 list_del(&rp
->q
.list
);
900 spin_unlock(&queue_lock
);
902 filp
->private_data
= NULL
;
905 cd
->last_close
= get_seconds();
906 atomic_dec(&cd
->readers
);
908 module_put(cd
->owner
);
914 static void cache_dequeue(struct cache_detail
*detail
, struct cache_head
*ch
)
916 struct cache_queue
*cq
;
917 spin_lock(&queue_lock
);
918 list_for_each_entry(cq
, &detail
->queue
, list
)
920 struct cache_request
*cr
= container_of(cq
, struct cache_request
, q
);
923 if (cr
->readers
!= 0)
925 list_del(&cr
->q
.list
);
926 spin_unlock(&queue_lock
);
927 cache_put(cr
->item
, detail
);
932 spin_unlock(&queue_lock
);
936 * Support routines for text-based upcalls.
937 * Fields are separated by spaces.
938 * Fields are either mangled to quote space tab newline slosh with slosh
939 * or a hexified with a leading \x
940 * Record is terminated with newline.
944 void qword_add(char **bpp
, int *lp
, char *str
)
952 while ((c
=*str
++) && len
)
960 *bp
++ = '0' + ((c
& 0300)>>6);
961 *bp
++ = '0' + ((c
& 0070)>>3);
962 *bp
++ = '0' + ((c
& 0007)>>0);
970 if (c
|| len
<1) len
= -1;
978 EXPORT_SYMBOL_GPL(qword_add
);
980 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
991 while (blen
&& len
>= 2) {
992 unsigned char c
= *buf
++;
993 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
994 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
999 if (blen
|| len
<1) len
= -1;
1007 EXPORT_SYMBOL_GPL(qword_addhex
);
1009 static void warn_no_listener(struct cache_detail
*detail
)
1011 if (detail
->last_warn
!= detail
->last_close
) {
1012 detail
->last_warn
= detail
->last_close
;
1013 if (detail
->warn_no_listener
)
1014 detail
->warn_no_listener(detail
, detail
->last_close
!= 0);
1019 * register an upcall request to user-space and queue it up for read() by the
1022 * Each request is at most one page long.
1024 int sunrpc_cache_pipe_upcall(struct cache_detail
*detail
, struct cache_head
*h
,
1025 void (*cache_request
)(struct cache_detail
*,
1026 struct cache_head
*,
1032 struct cache_request
*crq
;
1036 if (atomic_read(&detail
->readers
) == 0 &&
1037 detail
->last_close
< get_seconds() - 30) {
1038 warn_no_listener(detail
);
1042 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1046 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1052 bp
= buf
; len
= PAGE_SIZE
;
1054 cache_request(detail
, h
, &bp
, &len
);
1062 crq
->item
= cache_get(h
);
1064 crq
->len
= PAGE_SIZE
- len
;
1066 spin_lock(&queue_lock
);
1067 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1068 spin_unlock(&queue_lock
);
1069 wake_up(&queue_wait
);
1072 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall
);
1075 * parse a message from user-space and pass it
1076 * to an appropriate cache
1077 * Messages are, like requests, separated into fields by
1078 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1081 * reply cachename expiry key ... content....
1083 * key and content are both parsed by cache
1086 #define isodigit(c) (isdigit(c) && c <= '7')
1087 int qword_get(char **bpp
, char *dest
, int bufsize
)
1089 /* return bytes copied, or -1 on error */
1093 while (*bp
== ' ') bp
++;
1095 if (bp
[0] == '\\' && bp
[1] == 'x') {
1098 while (isxdigit(bp
[0]) && isxdigit(bp
[1]) && len
< bufsize
) {
1099 int byte
= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1102 byte
|= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1108 /* text with \nnn octal quoting */
1109 while (*bp
!= ' ' && *bp
!= '\n' && *bp
&& len
< bufsize
-1) {
1111 isodigit(bp
[1]) && (bp
[1] <= '3') &&
1114 int byte
= (*++bp
-'0');
1116 byte
= (byte
<< 3) | (*bp
++ - '0');
1117 byte
= (byte
<< 3) | (*bp
++ - '0');
1127 if (*bp
!= ' ' && *bp
!= '\n' && *bp
!= '\0')
1129 while (*bp
== ' ') bp
++;
1134 EXPORT_SYMBOL_GPL(qword_get
);
1138 * support /proc/sunrpc/cache/$CACHENAME/content
1140 * We call ->cache_show passing NULL for the item to
1141 * get a header, then pass each real item in the cache
1145 struct cache_detail
*cd
;
1148 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1149 __acquires(cd
->hash_lock
)
1152 unsigned hash
, entry
;
1153 struct cache_head
*ch
;
1154 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1157 read_lock(&cd
->hash_lock
);
1159 return SEQ_START_TOKEN
;
1161 entry
= n
& ((1LL<<32) - 1);
1163 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1166 n
&= ~((1LL<<32) - 1);
1170 } while(hash
< cd
->hash_size
&&
1171 cd
->hash_table
[hash
]==NULL
);
1172 if (hash
>= cd
->hash_size
)
1175 return cd
->hash_table
[hash
];
1178 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1180 struct cache_head
*ch
= p
;
1181 int hash
= (*pos
>> 32);
1182 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1184 if (p
== SEQ_START_TOKEN
)
1186 else if (ch
->next
== NULL
) {
1193 *pos
&= ~((1LL<<32) - 1);
1194 while (hash
< cd
->hash_size
&&
1195 cd
->hash_table
[hash
] == NULL
) {
1199 if (hash
>= cd
->hash_size
)
1202 return cd
->hash_table
[hash
];
1205 static void c_stop(struct seq_file
*m
, void *p
)
1206 __releases(cd
->hash_lock
)
1208 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1209 read_unlock(&cd
->hash_lock
);
1212 static int c_show(struct seq_file
*m
, void *p
)
1214 struct cache_head
*cp
= p
;
1215 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1217 if (p
== SEQ_START_TOKEN
)
1218 return cd
->cache_show(m
, cd
, NULL
);
1221 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1222 cp
->expiry_time
, atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1224 if (cache_check(cd
, cp
, NULL
))
1225 /* cache_check does a cache_put on failure */
1226 seq_printf(m
, "# ");
1230 return cd
->cache_show(m
, cd
, cp
);
1233 static const struct seq_operations cache_content_op
= {
1240 static int content_open(struct inode
*inode
, struct file
*file
,
1241 struct cache_detail
*cd
)
1245 if (!cd
|| !try_module_get(cd
->owner
))
1247 han
= __seq_open_private(file
, &cache_content_op
, sizeof(*han
));
1249 module_put(cd
->owner
);
1257 static int content_release(struct inode
*inode
, struct file
*file
,
1258 struct cache_detail
*cd
)
1260 int ret
= seq_release_private(inode
, file
);
1261 module_put(cd
->owner
);
1265 static int open_flush(struct inode
*inode
, struct file
*file
,
1266 struct cache_detail
*cd
)
1268 if (!cd
|| !try_module_get(cd
->owner
))
1270 return nonseekable_open(inode
, file
);
1273 static int release_flush(struct inode
*inode
, struct file
*file
,
1274 struct cache_detail
*cd
)
1276 module_put(cd
->owner
);
1280 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1281 size_t count
, loff_t
*ppos
,
1282 struct cache_detail
*cd
)
1285 unsigned long p
= *ppos
;
1288 sprintf(tbuf
, "%lu\n", cd
->flush_time
);
1295 if (copy_to_user(buf
, (void*)(tbuf
+p
), len
))
1301 static ssize_t
write_flush(struct file
*file
, const char __user
*buf
,
1302 size_t count
, loff_t
*ppos
,
1303 struct cache_detail
*cd
)
1308 if (*ppos
|| count
> sizeof(tbuf
)-1)
1310 if (copy_from_user(tbuf
, buf
, count
))
1313 flushtime
= simple_strtoul(tbuf
, &ep
, 0);
1314 if (*ep
&& *ep
!= '\n')
1317 cd
->flush_time
= flushtime
;
1318 cd
->nextcheck
= get_seconds();
1325 static ssize_t
cache_read_procfs(struct file
*filp
, char __user
*buf
,
1326 size_t count
, loff_t
*ppos
)
1328 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1330 return cache_read(filp
, buf
, count
, ppos
, cd
);
1333 static ssize_t
cache_write_procfs(struct file
*filp
, const char __user
*buf
,
1334 size_t count
, loff_t
*ppos
)
1336 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1338 return cache_write(filp
, buf
, count
, ppos
, cd
);
1341 static unsigned int cache_poll_procfs(struct file
*filp
, poll_table
*wait
)
1343 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1345 return cache_poll(filp
, wait
, cd
);
1348 static long cache_ioctl_procfs(struct file
*filp
,
1349 unsigned int cmd
, unsigned long arg
)
1352 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1353 struct cache_detail
*cd
= PDE(inode
)->data
;
1356 ret
= cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1362 static int cache_open_procfs(struct inode
*inode
, struct file
*filp
)
1364 struct cache_detail
*cd
= PDE(inode
)->data
;
1366 return cache_open(inode
, filp
, cd
);
1369 static int cache_release_procfs(struct inode
*inode
, struct file
*filp
)
1371 struct cache_detail
*cd
= PDE(inode
)->data
;
1373 return cache_release(inode
, filp
, cd
);
1376 static const struct file_operations cache_file_operations_procfs
= {
1377 .owner
= THIS_MODULE
,
1378 .llseek
= no_llseek
,
1379 .read
= cache_read_procfs
,
1380 .write
= cache_write_procfs
,
1381 .poll
= cache_poll_procfs
,
1382 .unlocked_ioctl
= cache_ioctl_procfs
, /* for FIONREAD */
1383 .open
= cache_open_procfs
,
1384 .release
= cache_release_procfs
,
1387 static int content_open_procfs(struct inode
*inode
, struct file
*filp
)
1389 struct cache_detail
*cd
= PDE(inode
)->data
;
1391 return content_open(inode
, filp
, cd
);
1394 static int content_release_procfs(struct inode
*inode
, struct file
*filp
)
1396 struct cache_detail
*cd
= PDE(inode
)->data
;
1398 return content_release(inode
, filp
, cd
);
1401 static const struct file_operations content_file_operations_procfs
= {
1402 .open
= content_open_procfs
,
1404 .llseek
= seq_lseek
,
1405 .release
= content_release_procfs
,
1408 static int open_flush_procfs(struct inode
*inode
, struct file
*filp
)
1410 struct cache_detail
*cd
= PDE(inode
)->data
;
1412 return open_flush(inode
, filp
, cd
);
1415 static int release_flush_procfs(struct inode
*inode
, struct file
*filp
)
1417 struct cache_detail
*cd
= PDE(inode
)->data
;
1419 return release_flush(inode
, filp
, cd
);
1422 static ssize_t
read_flush_procfs(struct file
*filp
, char __user
*buf
,
1423 size_t count
, loff_t
*ppos
)
1425 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1427 return read_flush(filp
, buf
, count
, ppos
, cd
);
1430 static ssize_t
write_flush_procfs(struct file
*filp
,
1431 const char __user
*buf
,
1432 size_t count
, loff_t
*ppos
)
1434 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1436 return write_flush(filp
, buf
, count
, ppos
, cd
);
1439 static const struct file_operations cache_flush_operations_procfs
= {
1440 .open
= open_flush_procfs
,
1441 .read
= read_flush_procfs
,
1442 .write
= write_flush_procfs
,
1443 .release
= release_flush_procfs
,
1446 static void remove_cache_proc_entries(struct cache_detail
*cd
)
1448 if (cd
->u
.procfs
.proc_ent
== NULL
)
1450 if (cd
->u
.procfs
.flush_ent
)
1451 remove_proc_entry("flush", cd
->u
.procfs
.proc_ent
);
1452 if (cd
->u
.procfs
.channel_ent
)
1453 remove_proc_entry("channel", cd
->u
.procfs
.proc_ent
);
1454 if (cd
->u
.procfs
.content_ent
)
1455 remove_proc_entry("content", cd
->u
.procfs
.proc_ent
);
1456 cd
->u
.procfs
.proc_ent
= NULL
;
1457 remove_proc_entry(cd
->name
, proc_net_rpc
);
1460 #ifdef CONFIG_PROC_FS
1461 static int create_cache_proc_entries(struct cache_detail
*cd
)
1463 struct proc_dir_entry
*p
;
1465 cd
->u
.procfs
.proc_ent
= proc_mkdir(cd
->name
, proc_net_rpc
);
1466 if (cd
->u
.procfs
.proc_ent
== NULL
)
1468 cd
->u
.procfs
.channel_ent
= NULL
;
1469 cd
->u
.procfs
.content_ent
= NULL
;
1471 p
= proc_create_data("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
1472 cd
->u
.procfs
.proc_ent
,
1473 &cache_flush_operations_procfs
, cd
);
1474 cd
->u
.procfs
.flush_ent
= p
;
1478 if (cd
->cache_upcall
|| cd
->cache_parse
) {
1479 p
= proc_create_data("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
1480 cd
->u
.procfs
.proc_ent
,
1481 &cache_file_operations_procfs
, cd
);
1482 cd
->u
.procfs
.channel_ent
= p
;
1486 if (cd
->cache_show
) {
1487 p
= proc_create_data("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
1488 cd
->u
.procfs
.proc_ent
,
1489 &content_file_operations_procfs
, cd
);
1490 cd
->u
.procfs
.content_ent
= p
;
1496 remove_cache_proc_entries(cd
);
1499 #else /* CONFIG_PROC_FS */
1500 static int create_cache_proc_entries(struct cache_detail
*cd
)
1506 void __init
cache_initialize(void)
1508 INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner
, do_cache_clean
);
1511 int cache_register(struct cache_detail
*cd
)
1515 sunrpc_init_cache_detail(cd
);
1516 ret
= create_cache_proc_entries(cd
);
1518 sunrpc_destroy_cache_detail(cd
);
1521 EXPORT_SYMBOL_GPL(cache_register
);
1523 void cache_unregister(struct cache_detail
*cd
)
1525 remove_cache_proc_entries(cd
);
1526 sunrpc_destroy_cache_detail(cd
);
1528 EXPORT_SYMBOL_GPL(cache_unregister
);
1530 static ssize_t
cache_read_pipefs(struct file
*filp
, char __user
*buf
,
1531 size_t count
, loff_t
*ppos
)
1533 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1535 return cache_read(filp
, buf
, count
, ppos
, cd
);
1538 static ssize_t
cache_write_pipefs(struct file
*filp
, const char __user
*buf
,
1539 size_t count
, loff_t
*ppos
)
1541 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1543 return cache_write(filp
, buf
, count
, ppos
, cd
);
1546 static unsigned int cache_poll_pipefs(struct file
*filp
, poll_table
*wait
)
1548 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1550 return cache_poll(filp
, wait
, cd
);
1553 static long cache_ioctl_pipefs(struct file
*filp
,
1554 unsigned int cmd
, unsigned long arg
)
1556 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1557 struct cache_detail
*cd
= RPC_I(inode
)->private;
1561 ret
= cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1567 static int cache_open_pipefs(struct inode
*inode
, struct file
*filp
)
1569 struct cache_detail
*cd
= RPC_I(inode
)->private;
1571 return cache_open(inode
, filp
, cd
);
1574 static int cache_release_pipefs(struct inode
*inode
, struct file
*filp
)
1576 struct cache_detail
*cd
= RPC_I(inode
)->private;
1578 return cache_release(inode
, filp
, cd
);
1581 const struct file_operations cache_file_operations_pipefs
= {
1582 .owner
= THIS_MODULE
,
1583 .llseek
= no_llseek
,
1584 .read
= cache_read_pipefs
,
1585 .write
= cache_write_pipefs
,
1586 .poll
= cache_poll_pipefs
,
1587 .unlocked_ioctl
= cache_ioctl_pipefs
, /* for FIONREAD */
1588 .open
= cache_open_pipefs
,
1589 .release
= cache_release_pipefs
,
1592 static int content_open_pipefs(struct inode
*inode
, struct file
*filp
)
1594 struct cache_detail
*cd
= RPC_I(inode
)->private;
1596 return content_open(inode
, filp
, cd
);
1599 static int content_release_pipefs(struct inode
*inode
, struct file
*filp
)
1601 struct cache_detail
*cd
= RPC_I(inode
)->private;
1603 return content_release(inode
, filp
, cd
);
1606 const struct file_operations content_file_operations_pipefs
= {
1607 .open
= content_open_pipefs
,
1609 .llseek
= seq_lseek
,
1610 .release
= content_release_pipefs
,
1613 static int open_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1615 struct cache_detail
*cd
= RPC_I(inode
)->private;
1617 return open_flush(inode
, filp
, cd
);
1620 static int release_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1622 struct cache_detail
*cd
= RPC_I(inode
)->private;
1624 return release_flush(inode
, filp
, cd
);
1627 static ssize_t
read_flush_pipefs(struct file
*filp
, char __user
*buf
,
1628 size_t count
, loff_t
*ppos
)
1630 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1632 return read_flush(filp
, buf
, count
, ppos
, cd
);
1635 static ssize_t
write_flush_pipefs(struct file
*filp
,
1636 const char __user
*buf
,
1637 size_t count
, loff_t
*ppos
)
1639 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1641 return write_flush(filp
, buf
, count
, ppos
, cd
);
1644 const struct file_operations cache_flush_operations_pipefs
= {
1645 .open
= open_flush_pipefs
,
1646 .read
= read_flush_pipefs
,
1647 .write
= write_flush_pipefs
,
1648 .release
= release_flush_pipefs
,
1651 int sunrpc_cache_register_pipefs(struct dentry
*parent
,
1652 const char *name
, mode_t umode
,
1653 struct cache_detail
*cd
)
1659 sunrpc_init_cache_detail(cd
);
1661 q
.len
= strlen(name
);
1662 q
.hash
= full_name_hash(q
.name
, q
.len
);
1663 dir
= rpc_create_cache_dir(parent
, &q
, umode
, cd
);
1665 cd
->u
.pipefs
.dir
= dir
;
1667 sunrpc_destroy_cache_detail(cd
);
1672 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs
);
1674 void sunrpc_cache_unregister_pipefs(struct cache_detail
*cd
)
1676 rpc_remove_cache_dir(cd
->u
.pipefs
.dir
);
1677 cd
->u
.pipefs
.dir
= NULL
;
1678 sunrpc_destroy_cache_detail(cd
);
1680 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs
);