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 queue_loose(struct cache_detail
*detail
, struct cache_head
*ch
);
108 static int cache_fresh_locked(struct cache_head
*head
, time_t expiry
)
110 head
->expiry_time
= expiry
;
111 head
->last_refresh
= get_seconds();
112 return !test_and_set_bit(CACHE_VALID
, &head
->flags
);
115 static void cache_fresh_unlocked(struct cache_head
*head
,
116 struct cache_detail
*detail
, int new)
119 cache_revisit_request(head
);
120 if (test_and_clear_bit(CACHE_PENDING
, &head
->flags
)) {
121 cache_revisit_request(head
);
122 queue_loose(detail
, head
);
126 struct cache_head
*sunrpc_cache_update(struct cache_detail
*detail
,
127 struct cache_head
*new, struct cache_head
*old
, int hash
)
129 /* The 'old' entry is to be replaced by 'new'.
130 * If 'old' is not VALID, we update it directly,
131 * otherwise we need to replace it
133 struct cache_head
**head
;
134 struct cache_head
*tmp
;
137 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
138 write_lock(&detail
->hash_lock
);
139 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
140 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
141 set_bit(CACHE_NEGATIVE
, &old
->flags
);
143 detail
->update(old
, new);
144 is_new
= cache_fresh_locked(old
, new->expiry_time
);
145 write_unlock(&detail
->hash_lock
);
146 cache_fresh_unlocked(old
, detail
, is_new
);
149 write_unlock(&detail
->hash_lock
);
151 /* We need to insert a new entry */
152 tmp
= detail
->alloc();
154 cache_put(old
, detail
);
158 detail
->init(tmp
, old
);
159 head
= &detail
->hash_table
[hash
];
161 write_lock(&detail
->hash_lock
);
162 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
163 set_bit(CACHE_NEGATIVE
, &tmp
->flags
);
165 detail
->update(tmp
, new);
170 is_new
= cache_fresh_locked(tmp
, new->expiry_time
);
171 cache_fresh_locked(old
, 0);
172 write_unlock(&detail
->hash_lock
);
173 cache_fresh_unlocked(tmp
, detail
, is_new
);
174 cache_fresh_unlocked(old
, detail
, 0);
175 cache_put(old
, detail
);
178 EXPORT_SYMBOL_GPL(sunrpc_cache_update
);
180 static int cache_make_upcall(struct cache_detail
*cd
, struct cache_head
*h
)
182 if (!cd
->cache_upcall
)
184 return cd
->cache_upcall(cd
, h
);
188 * This is the generic cache management routine for all
189 * the authentication caches.
190 * It checks the currency of a cache item and will (later)
191 * initiate an upcall to fill it if needed.
194 * Returns 0 if the cache_head can be used, or cache_puts it and returns
195 * -EAGAIN if upcall is pending,
196 * -ETIMEDOUT if upcall failed and should be retried,
197 * -ENOENT if cache entry was negative
199 int cache_check(struct cache_detail
*detail
,
200 struct cache_head
*h
, struct cache_req
*rqstp
)
203 long refresh_age
, age
;
205 /* First decide return status as best we can */
206 if (!test_bit(CACHE_VALID
, &h
->flags
) ||
207 h
->expiry_time
< get_seconds())
209 else if (detail
->flush_time
> h
->last_refresh
)
213 if (test_bit(CACHE_NEGATIVE
, &h
->flags
))
218 /* now see if we want to start an upcall */
219 refresh_age
= (h
->expiry_time
- h
->last_refresh
);
220 age
= get_seconds() - h
->last_refresh
;
225 } else if (rv
== -EAGAIN
|| age
> refresh_age
/2) {
226 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
228 if (!test_and_set_bit(CACHE_PENDING
, &h
->flags
)) {
229 switch (cache_make_upcall(detail
, h
)) {
231 clear_bit(CACHE_PENDING
, &h
->flags
);
233 set_bit(CACHE_NEGATIVE
, &h
->flags
);
234 cache_fresh_unlocked(h
, detail
,
235 cache_fresh_locked(h
, get_seconds()+CACHE_NEW_EXPIRY
));
241 clear_bit(CACHE_PENDING
, &h
->flags
);
242 cache_revisit_request(h
);
249 if (cache_defer_req(rqstp
, h
) != 0)
253 cache_put(h
, detail
);
256 EXPORT_SYMBOL_GPL(cache_check
);
259 * caches need to be periodically cleaned.
260 * For this we maintain a list of cache_detail and
261 * a current pointer into that list and into the table
264 * Each time clean_cache is called it finds the next non-empty entry
265 * in the current table and walks the list in that entry
266 * looking for entries that can be removed.
268 * An entry gets removed if:
269 * - The expiry is before current time
270 * - The last_refresh time is before the flush_time for that cache
272 * later we might drop old entries with non-NEVER expiry if that table
273 * is getting 'full' for some definition of 'full'
275 * The question of "how often to scan a table" is an interesting one
276 * and is answered in part by the use of the "nextcheck" field in the
278 * When a scan of a table begins, the nextcheck field is set to a time
279 * that is well into the future.
280 * While scanning, if an expiry time is found that is earlier than the
281 * current nextcheck time, nextcheck is set to that expiry time.
282 * If the flush_time is ever set to a time earlier than the nextcheck
283 * time, the nextcheck time is then set to that flush_time.
285 * A table is then only scanned if the current time is at least
286 * the nextcheck time.
290 static LIST_HEAD(cache_list
);
291 static DEFINE_SPINLOCK(cache_list_lock
);
292 static struct cache_detail
*current_detail
;
293 static int current_index
;
295 static void do_cache_clean(struct work_struct
*work
);
296 static DECLARE_DELAYED_WORK(cache_cleaner
, do_cache_clean
);
298 static void sunrpc_init_cache_detail(struct cache_detail
*cd
)
300 rwlock_init(&cd
->hash_lock
);
301 INIT_LIST_HEAD(&cd
->queue
);
302 spin_lock(&cache_list_lock
);
305 atomic_set(&cd
->readers
, 0);
308 list_add(&cd
->others
, &cache_list
);
309 spin_unlock(&cache_list_lock
);
311 /* start the cleaning process */
312 schedule_delayed_work(&cache_cleaner
, 0);
315 static void sunrpc_destroy_cache_detail(struct cache_detail
*cd
)
318 spin_lock(&cache_list_lock
);
319 write_lock(&cd
->hash_lock
);
320 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
321 write_unlock(&cd
->hash_lock
);
322 spin_unlock(&cache_list_lock
);
325 if (current_detail
== cd
)
326 current_detail
= NULL
;
327 list_del_init(&cd
->others
);
328 write_unlock(&cd
->hash_lock
);
329 spin_unlock(&cache_list_lock
);
330 if (list_empty(&cache_list
)) {
331 /* module must be being unloaded so its safe to kill the worker */
332 cancel_delayed_work_sync(&cache_cleaner
);
336 printk(KERN_ERR
"nfsd: failed to unregister %s cache\n", cd
->name
);
339 /* clean cache tries to find something to clean
341 * It returns 1 if it cleaned something,
342 * 0 if it didn't find anything this time
343 * -1 if it fell off the end of the list.
345 static int cache_clean(void)
348 struct list_head
*next
;
350 spin_lock(&cache_list_lock
);
352 /* find a suitable table if we don't already have one */
353 while (current_detail
== NULL
||
354 current_index
>= current_detail
->hash_size
) {
356 next
= current_detail
->others
.next
;
358 next
= cache_list
.next
;
359 if (next
== &cache_list
) {
360 current_detail
= NULL
;
361 spin_unlock(&cache_list_lock
);
364 current_detail
= list_entry(next
, struct cache_detail
, others
);
365 if (current_detail
->nextcheck
> get_seconds())
366 current_index
= current_detail
->hash_size
;
369 current_detail
->nextcheck
= get_seconds()+30*60;
373 /* find a non-empty bucket in the table */
374 while (current_detail
&&
375 current_index
< current_detail
->hash_size
&&
376 current_detail
->hash_table
[current_index
] == NULL
)
379 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
381 if (current_detail
&& current_index
< current_detail
->hash_size
) {
382 struct cache_head
*ch
, **cp
;
383 struct cache_detail
*d
;
385 write_lock(¤t_detail
->hash_lock
);
387 /* Ok, now to clean this strand */
389 cp
= & current_detail
->hash_table
[current_index
];
391 for (; ch
; cp
= & ch
->next
, ch
= *cp
) {
392 if (current_detail
->nextcheck
> ch
->expiry_time
)
393 current_detail
->nextcheck
= ch
->expiry_time
+1;
394 if (ch
->expiry_time
>= get_seconds()
395 && ch
->last_refresh
>= current_detail
->flush_time
398 if (test_and_clear_bit(CACHE_PENDING
, &ch
->flags
))
399 queue_loose(current_detail
, ch
);
401 if (atomic_read(&ch
->ref
.refcount
) == 1)
407 current_detail
->entries
--;
410 write_unlock(¤t_detail
->hash_lock
);
414 spin_unlock(&cache_list_lock
);
418 spin_unlock(&cache_list_lock
);
424 * We want to regularly clean the cache, so we need to schedule some work ...
426 static void do_cache_clean(struct work_struct
*work
)
429 if (cache_clean() == -1)
430 delay
= round_jiffies_relative(30*HZ
);
432 if (list_empty(&cache_list
))
436 schedule_delayed_work(&cache_cleaner
, delay
);
441 * Clean all caches promptly. This just calls cache_clean
442 * repeatedly until we are sure that every cache has had a chance to
445 void cache_flush(void)
447 while (cache_clean() != -1)
449 while (cache_clean() != -1)
452 EXPORT_SYMBOL_GPL(cache_flush
);
454 void cache_purge(struct cache_detail
*detail
)
456 detail
->flush_time
= LONG_MAX
;
457 detail
->nextcheck
= get_seconds();
459 detail
->flush_time
= 1;
461 EXPORT_SYMBOL_GPL(cache_purge
);
465 * Deferral and Revisiting of Requests.
467 * If a cache lookup finds a pending entry, we
468 * need to defer the request and revisit it later.
469 * All deferred requests are stored in a hash table,
470 * indexed by "struct cache_head *".
471 * As it may be wasteful to store a whole request
472 * structure, we allow the request to provide a
473 * deferred form, which must contain a
474 * 'struct cache_deferred_req'
475 * This cache_deferred_req contains a method to allow
476 * it to be revisited when cache info is available
479 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
480 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
482 #define DFR_MAX 300 /* ??? */
484 static DEFINE_SPINLOCK(cache_defer_lock
);
485 static LIST_HEAD(cache_defer_list
);
486 static struct list_head cache_defer_hash
[DFR_HASHSIZE
];
487 static int cache_defer_cnt
;
489 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
491 struct cache_deferred_req
*dreq
;
492 int hash
= DFR_HASH(item
);
494 if (cache_defer_cnt
>= DFR_MAX
) {
495 /* too much in the cache, randomly drop this one,
496 * or continue and drop the oldest below
501 dreq
= req
->defer(req
);
507 spin_lock(&cache_defer_lock
);
509 list_add(&dreq
->recent
, &cache_defer_list
);
511 if (cache_defer_hash
[hash
].next
== NULL
)
512 INIT_LIST_HEAD(&cache_defer_hash
[hash
]);
513 list_add(&dreq
->hash
, &cache_defer_hash
[hash
]);
515 /* it is in, now maybe clean up */
517 if (++cache_defer_cnt
> DFR_MAX
) {
518 dreq
= list_entry(cache_defer_list
.prev
,
519 struct cache_deferred_req
, recent
);
520 list_del(&dreq
->recent
);
521 list_del(&dreq
->hash
);
524 spin_unlock(&cache_defer_lock
);
527 /* there was one too many */
528 dreq
->revisit(dreq
, 1);
530 if (!test_bit(CACHE_PENDING
, &item
->flags
)) {
531 /* must have just been validated... */
532 cache_revisit_request(item
);
537 static void cache_revisit_request(struct cache_head
*item
)
539 struct cache_deferred_req
*dreq
;
540 struct list_head pending
;
542 struct list_head
*lp
;
543 int hash
= DFR_HASH(item
);
545 INIT_LIST_HEAD(&pending
);
546 spin_lock(&cache_defer_lock
);
548 lp
= cache_defer_hash
[hash
].next
;
550 while (lp
!= &cache_defer_hash
[hash
]) {
551 dreq
= list_entry(lp
, struct cache_deferred_req
, hash
);
553 if (dreq
->item
== item
) {
554 list_del(&dreq
->hash
);
555 list_move(&dreq
->recent
, &pending
);
560 spin_unlock(&cache_defer_lock
);
562 while (!list_empty(&pending
)) {
563 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
564 list_del_init(&dreq
->recent
);
565 dreq
->revisit(dreq
, 0);
569 void cache_clean_deferred(void *owner
)
571 struct cache_deferred_req
*dreq
, *tmp
;
572 struct list_head pending
;
575 INIT_LIST_HEAD(&pending
);
576 spin_lock(&cache_defer_lock
);
578 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
579 if (dreq
->owner
== owner
) {
580 list_del(&dreq
->hash
);
581 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
, 1);
595 * communicate with user-space
597 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
598 * On read, you get a full request, or block.
599 * On write, an update request is processed.
600 * Poll works if anything to read, and always allows write.
602 * Implemented by linked list of requests. Each open file has
603 * a ->private that also exists in this list. New requests are added
604 * to the end and may wakeup and preceding readers.
605 * New readers are added to the head. If, on read, an item is found with
606 * CACHE_UPCALLING clear, we free it from the list.
610 static DEFINE_SPINLOCK(queue_lock
);
611 static DEFINE_MUTEX(queue_io_mutex
);
614 struct list_head list
;
615 int reader
; /* if 0, then request */
617 struct cache_request
{
618 struct cache_queue q
;
619 struct cache_head
*item
;
624 struct cache_reader
{
625 struct cache_queue q
;
626 int offset
; /* if non-0, we have a refcnt on next request */
629 static ssize_t
cache_read(struct file
*filp
, char __user
*buf
, size_t count
,
630 loff_t
*ppos
, struct cache_detail
*cd
)
632 struct cache_reader
*rp
= filp
->private_data
;
633 struct cache_request
*rq
;
634 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
640 mutex_lock(&inode
->i_mutex
); /* protect against multiple concurrent
641 * readers on this file */
643 spin_lock(&queue_lock
);
644 /* need to find next request */
645 while (rp
->q
.list
.next
!= &cd
->queue
&&
646 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
648 struct list_head
*next
= rp
->q
.list
.next
;
649 list_move(&rp
->q
.list
, next
);
651 if (rp
->q
.list
.next
== &cd
->queue
) {
652 spin_unlock(&queue_lock
);
653 mutex_unlock(&inode
->i_mutex
);
657 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
658 BUG_ON(rq
->q
.reader
);
661 spin_unlock(&queue_lock
);
663 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
665 spin_lock(&queue_lock
);
666 list_move(&rp
->q
.list
, &rq
->q
.list
);
667 spin_unlock(&queue_lock
);
669 if (rp
->offset
+ count
> rq
->len
)
670 count
= rq
->len
- rp
->offset
;
672 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
675 if (rp
->offset
>= rq
->len
) {
677 spin_lock(&queue_lock
);
678 list_move(&rp
->q
.list
, &rq
->q
.list
);
679 spin_unlock(&queue_lock
);
684 if (rp
->offset
== 0) {
685 /* need to release rq */
686 spin_lock(&queue_lock
);
688 if (rq
->readers
== 0 &&
689 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
690 list_del(&rq
->q
.list
);
691 spin_unlock(&queue_lock
);
692 cache_put(rq
->item
, cd
);
696 spin_unlock(&queue_lock
);
700 mutex_unlock(&inode
->i_mutex
);
701 return err
? err
: count
;
704 static ssize_t
cache_do_downcall(char *kaddr
, const char __user
*buf
,
705 size_t count
, struct cache_detail
*cd
)
709 if (copy_from_user(kaddr
, buf
, count
))
712 ret
= cd
->cache_parse(cd
, kaddr
, count
);
718 static ssize_t
cache_slow_downcall(const char __user
*buf
,
719 size_t count
, struct cache_detail
*cd
)
721 static char write_buf
[8192]; /* protected by queue_io_mutex */
722 ssize_t ret
= -EINVAL
;
724 if (count
>= sizeof(write_buf
))
726 mutex_lock(&queue_io_mutex
);
727 ret
= cache_do_downcall(write_buf
, buf
, count
, cd
);
728 mutex_unlock(&queue_io_mutex
);
733 static ssize_t
cache_downcall(struct address_space
*mapping
,
734 const char __user
*buf
,
735 size_t count
, struct cache_detail
*cd
)
739 ssize_t ret
= -ENOMEM
;
741 if (count
>= PAGE_CACHE_SIZE
)
744 page
= find_or_create_page(mapping
, 0, GFP_KERNEL
);
749 ret
= cache_do_downcall(kaddr
, buf
, count
, cd
);
752 page_cache_release(page
);
755 return cache_slow_downcall(buf
, count
, cd
);
758 static ssize_t
cache_write(struct file
*filp
, const char __user
*buf
,
759 size_t count
, loff_t
*ppos
,
760 struct cache_detail
*cd
)
762 struct address_space
*mapping
= filp
->f_mapping
;
763 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
764 ssize_t ret
= -EINVAL
;
766 if (!cd
->cache_parse
)
769 mutex_lock(&inode
->i_mutex
);
770 ret
= cache_downcall(mapping
, buf
, count
, cd
);
771 mutex_unlock(&inode
->i_mutex
);
776 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
778 static unsigned int cache_poll(struct file
*filp
, poll_table
*wait
,
779 struct cache_detail
*cd
)
782 struct cache_reader
*rp
= filp
->private_data
;
783 struct cache_queue
*cq
;
785 poll_wait(filp
, &queue_wait
, wait
);
787 /* alway allow write */
788 mask
= POLL_OUT
| POLLWRNORM
;
793 spin_lock(&queue_lock
);
795 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
796 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
798 mask
|= POLLIN
| POLLRDNORM
;
801 spin_unlock(&queue_lock
);
805 static int cache_ioctl(struct inode
*ino
, struct file
*filp
,
806 unsigned int cmd
, unsigned long arg
,
807 struct cache_detail
*cd
)
810 struct cache_reader
*rp
= filp
->private_data
;
811 struct cache_queue
*cq
;
813 if (cmd
!= FIONREAD
|| !rp
)
816 spin_lock(&queue_lock
);
818 /* only find the length remaining in current request,
819 * or the length of the next request
821 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
822 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
824 struct cache_request
*cr
=
825 container_of(cq
, struct cache_request
, q
);
826 len
= cr
->len
- rp
->offset
;
829 spin_unlock(&queue_lock
);
831 return put_user(len
, (int __user
*)arg
);
834 static int cache_open(struct inode
*inode
, struct file
*filp
,
835 struct cache_detail
*cd
)
837 struct cache_reader
*rp
= NULL
;
839 if (!cd
|| !try_module_get(cd
->owner
))
841 nonseekable_open(inode
, filp
);
842 if (filp
->f_mode
& FMODE_READ
) {
843 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
848 atomic_inc(&cd
->readers
);
849 spin_lock(&queue_lock
);
850 list_add(&rp
->q
.list
, &cd
->queue
);
851 spin_unlock(&queue_lock
);
853 filp
->private_data
= rp
;
857 static int cache_release(struct inode
*inode
, struct file
*filp
,
858 struct cache_detail
*cd
)
860 struct cache_reader
*rp
= filp
->private_data
;
863 spin_lock(&queue_lock
);
865 struct cache_queue
*cq
;
866 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
867 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
869 container_of(cq
, struct cache_request
, q
)
875 list_del(&rp
->q
.list
);
876 spin_unlock(&queue_lock
);
878 filp
->private_data
= NULL
;
881 cd
->last_close
= get_seconds();
882 atomic_dec(&cd
->readers
);
884 module_put(cd
->owner
);
890 static void queue_loose(struct cache_detail
*detail
, struct cache_head
*ch
)
892 struct cache_queue
*cq
;
893 spin_lock(&queue_lock
);
894 list_for_each_entry(cq
, &detail
->queue
, list
)
896 struct cache_request
*cr
= container_of(cq
, struct cache_request
, q
);
899 if (cr
->readers
!= 0)
901 list_del(&cr
->q
.list
);
902 spin_unlock(&queue_lock
);
903 cache_put(cr
->item
, detail
);
908 spin_unlock(&queue_lock
);
912 * Support routines for text-based upcalls.
913 * Fields are separated by spaces.
914 * Fields are either mangled to quote space tab newline slosh with slosh
915 * or a hexified with a leading \x
916 * Record is terminated with newline.
920 void qword_add(char **bpp
, int *lp
, char *str
)
928 while ((c
=*str
++) && len
)
936 *bp
++ = '0' + ((c
& 0300)>>6);
937 *bp
++ = '0' + ((c
& 0070)>>3);
938 *bp
++ = '0' + ((c
& 0007)>>0);
946 if (c
|| len
<1) len
= -1;
954 EXPORT_SYMBOL_GPL(qword_add
);
956 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
967 while (blen
&& len
>= 2) {
968 unsigned char c
= *buf
++;
969 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
970 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
975 if (blen
|| len
<1) len
= -1;
983 EXPORT_SYMBOL_GPL(qword_addhex
);
985 static void warn_no_listener(struct cache_detail
*detail
)
987 if (detail
->last_warn
!= detail
->last_close
) {
988 detail
->last_warn
= detail
->last_close
;
989 if (detail
->warn_no_listener
)
990 detail
->warn_no_listener(detail
, detail
->last_close
!= 0);
995 * register an upcall request to user-space and queue it up for read() by the
998 * Each request is at most one page long.
1000 int sunrpc_cache_pipe_upcall(struct cache_detail
*detail
, struct cache_head
*h
,
1001 void (*cache_request
)(struct cache_detail
*,
1002 struct cache_head
*,
1008 struct cache_request
*crq
;
1012 if (atomic_read(&detail
->readers
) == 0 &&
1013 detail
->last_close
< get_seconds() - 30) {
1014 warn_no_listener(detail
);
1018 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1022 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1028 bp
= buf
; len
= PAGE_SIZE
;
1030 cache_request(detail
, h
, &bp
, &len
);
1038 crq
->item
= cache_get(h
);
1040 crq
->len
= PAGE_SIZE
- len
;
1042 spin_lock(&queue_lock
);
1043 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1044 spin_unlock(&queue_lock
);
1045 wake_up(&queue_wait
);
1048 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall
);
1051 * parse a message from user-space and pass it
1052 * to an appropriate cache
1053 * Messages are, like requests, separated into fields by
1054 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1057 * reply cachename expiry key ... content....
1059 * key and content are both parsed by cache
1062 #define isodigit(c) (isdigit(c) && c <= '7')
1063 int qword_get(char **bpp
, char *dest
, int bufsize
)
1065 /* return bytes copied, or -1 on error */
1069 while (*bp
== ' ') bp
++;
1071 if (bp
[0] == '\\' && bp
[1] == 'x') {
1074 while (isxdigit(bp
[0]) && isxdigit(bp
[1]) && len
< bufsize
) {
1075 int byte
= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1078 byte
|= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1084 /* text with \nnn octal quoting */
1085 while (*bp
!= ' ' && *bp
!= '\n' && *bp
&& len
< bufsize
-1) {
1087 isodigit(bp
[1]) && (bp
[1] <= '3') &&
1090 int byte
= (*++bp
-'0');
1092 byte
= (byte
<< 3) | (*bp
++ - '0');
1093 byte
= (byte
<< 3) | (*bp
++ - '0');
1103 if (*bp
!= ' ' && *bp
!= '\n' && *bp
!= '\0')
1105 while (*bp
== ' ') bp
++;
1110 EXPORT_SYMBOL_GPL(qword_get
);
1114 * support /proc/sunrpc/cache/$CACHENAME/content
1116 * We call ->cache_show passing NULL for the item to
1117 * get a header, then pass each real item in the cache
1121 struct cache_detail
*cd
;
1124 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1125 __acquires(cd
->hash_lock
)
1128 unsigned hash
, entry
;
1129 struct cache_head
*ch
;
1130 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1133 read_lock(&cd
->hash_lock
);
1135 return SEQ_START_TOKEN
;
1137 entry
= n
& ((1LL<<32) - 1);
1139 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1142 n
&= ~((1LL<<32) - 1);
1146 } while(hash
< cd
->hash_size
&&
1147 cd
->hash_table
[hash
]==NULL
);
1148 if (hash
>= cd
->hash_size
)
1151 return cd
->hash_table
[hash
];
1154 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1156 struct cache_head
*ch
= p
;
1157 int hash
= (*pos
>> 32);
1158 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1160 if (p
== SEQ_START_TOKEN
)
1162 else if (ch
->next
== NULL
) {
1169 *pos
&= ~((1LL<<32) - 1);
1170 while (hash
< cd
->hash_size
&&
1171 cd
->hash_table
[hash
] == NULL
) {
1175 if (hash
>= cd
->hash_size
)
1178 return cd
->hash_table
[hash
];
1181 static void c_stop(struct seq_file
*m
, void *p
)
1182 __releases(cd
->hash_lock
)
1184 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1185 read_unlock(&cd
->hash_lock
);
1188 static int c_show(struct seq_file
*m
, void *p
)
1190 struct cache_head
*cp
= p
;
1191 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1193 if (p
== SEQ_START_TOKEN
)
1194 return cd
->cache_show(m
, cd
, NULL
);
1197 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1198 cp
->expiry_time
, atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1200 if (cache_check(cd
, cp
, NULL
))
1201 /* cache_check does a cache_put on failure */
1202 seq_printf(m
, "# ");
1206 return cd
->cache_show(m
, cd
, cp
);
1209 static const struct seq_operations cache_content_op
= {
1216 static int content_open(struct inode
*inode
, struct file
*file
,
1217 struct cache_detail
*cd
)
1221 if (!cd
|| !try_module_get(cd
->owner
))
1223 han
= __seq_open_private(file
, &cache_content_op
, sizeof(*han
));
1231 static int content_release(struct inode
*inode
, struct file
*file
,
1232 struct cache_detail
*cd
)
1234 int ret
= seq_release_private(inode
, file
);
1235 module_put(cd
->owner
);
1239 static int open_flush(struct inode
*inode
, struct file
*file
,
1240 struct cache_detail
*cd
)
1242 if (!cd
|| !try_module_get(cd
->owner
))
1244 return nonseekable_open(inode
, file
);
1247 static int release_flush(struct inode
*inode
, struct file
*file
,
1248 struct cache_detail
*cd
)
1250 module_put(cd
->owner
);
1254 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1255 size_t count
, loff_t
*ppos
,
1256 struct cache_detail
*cd
)
1259 unsigned long p
= *ppos
;
1262 sprintf(tbuf
, "%lu\n", cd
->flush_time
);
1269 if (copy_to_user(buf
, (void*)(tbuf
+p
), len
))
1275 static ssize_t
write_flush(struct file
*file
, const char __user
*buf
,
1276 size_t count
, loff_t
*ppos
,
1277 struct cache_detail
*cd
)
1282 if (*ppos
|| count
> sizeof(tbuf
)-1)
1284 if (copy_from_user(tbuf
, buf
, count
))
1287 flushtime
= simple_strtoul(tbuf
, &ep
, 0);
1288 if (*ep
&& *ep
!= '\n')
1291 cd
->flush_time
= flushtime
;
1292 cd
->nextcheck
= get_seconds();
1299 static ssize_t
cache_read_procfs(struct file
*filp
, char __user
*buf
,
1300 size_t count
, loff_t
*ppos
)
1302 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1304 return cache_read(filp
, buf
, count
, ppos
, cd
);
1307 static ssize_t
cache_write_procfs(struct file
*filp
, const char __user
*buf
,
1308 size_t count
, loff_t
*ppos
)
1310 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1312 return cache_write(filp
, buf
, count
, ppos
, cd
);
1315 static unsigned int cache_poll_procfs(struct file
*filp
, poll_table
*wait
)
1317 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1319 return cache_poll(filp
, wait
, cd
);
1322 static int cache_ioctl_procfs(struct inode
*inode
, struct file
*filp
,
1323 unsigned int cmd
, unsigned long arg
)
1325 struct cache_detail
*cd
= PDE(inode
)->data
;
1327 return cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1330 static int cache_open_procfs(struct inode
*inode
, struct file
*filp
)
1332 struct cache_detail
*cd
= PDE(inode
)->data
;
1334 return cache_open(inode
, filp
, cd
);
1337 static int cache_release_procfs(struct inode
*inode
, struct file
*filp
)
1339 struct cache_detail
*cd
= PDE(inode
)->data
;
1341 return cache_release(inode
, filp
, cd
);
1344 static const struct file_operations cache_file_operations_procfs
= {
1345 .owner
= THIS_MODULE
,
1346 .llseek
= no_llseek
,
1347 .read
= cache_read_procfs
,
1348 .write
= cache_write_procfs
,
1349 .poll
= cache_poll_procfs
,
1350 .ioctl
= cache_ioctl_procfs
, /* for FIONREAD */
1351 .open
= cache_open_procfs
,
1352 .release
= cache_release_procfs
,
1355 static int content_open_procfs(struct inode
*inode
, struct file
*filp
)
1357 struct cache_detail
*cd
= PDE(inode
)->data
;
1359 return content_open(inode
, filp
, cd
);
1362 static int content_release_procfs(struct inode
*inode
, struct file
*filp
)
1364 struct cache_detail
*cd
= PDE(inode
)->data
;
1366 return content_release(inode
, filp
, cd
);
1369 static const struct file_operations content_file_operations_procfs
= {
1370 .open
= content_open_procfs
,
1372 .llseek
= seq_lseek
,
1373 .release
= content_release_procfs
,
1376 static int open_flush_procfs(struct inode
*inode
, struct file
*filp
)
1378 struct cache_detail
*cd
= PDE(inode
)->data
;
1380 return open_flush(inode
, filp
, cd
);
1383 static int release_flush_procfs(struct inode
*inode
, struct file
*filp
)
1385 struct cache_detail
*cd
= PDE(inode
)->data
;
1387 return release_flush(inode
, filp
, cd
);
1390 static ssize_t
read_flush_procfs(struct file
*filp
, char __user
*buf
,
1391 size_t count
, loff_t
*ppos
)
1393 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1395 return read_flush(filp
, buf
, count
, ppos
, cd
);
1398 static ssize_t
write_flush_procfs(struct file
*filp
,
1399 const char __user
*buf
,
1400 size_t count
, loff_t
*ppos
)
1402 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1404 return write_flush(filp
, buf
, count
, ppos
, cd
);
1407 static const struct file_operations cache_flush_operations_procfs
= {
1408 .open
= open_flush_procfs
,
1409 .read
= read_flush_procfs
,
1410 .write
= write_flush_procfs
,
1411 .release
= release_flush_procfs
,
1414 static void remove_cache_proc_entries(struct cache_detail
*cd
)
1416 if (cd
->u
.procfs
.proc_ent
== NULL
)
1418 if (cd
->u
.procfs
.flush_ent
)
1419 remove_proc_entry("flush", cd
->u
.procfs
.proc_ent
);
1420 if (cd
->u
.procfs
.channel_ent
)
1421 remove_proc_entry("channel", cd
->u
.procfs
.proc_ent
);
1422 if (cd
->u
.procfs
.content_ent
)
1423 remove_proc_entry("content", cd
->u
.procfs
.proc_ent
);
1424 cd
->u
.procfs
.proc_ent
= NULL
;
1425 remove_proc_entry(cd
->name
, proc_net_rpc
);
1428 #ifdef CONFIG_PROC_FS
1429 static int create_cache_proc_entries(struct cache_detail
*cd
)
1431 struct proc_dir_entry
*p
;
1433 cd
->u
.procfs
.proc_ent
= proc_mkdir(cd
->name
, proc_net_rpc
);
1434 if (cd
->u
.procfs
.proc_ent
== NULL
)
1436 cd
->u
.procfs
.channel_ent
= NULL
;
1437 cd
->u
.procfs
.content_ent
= NULL
;
1439 p
= proc_create_data("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
1440 cd
->u
.procfs
.proc_ent
,
1441 &cache_flush_operations_procfs
, cd
);
1442 cd
->u
.procfs
.flush_ent
= p
;
1446 if (cd
->cache_upcall
|| cd
->cache_parse
) {
1447 p
= proc_create_data("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
1448 cd
->u
.procfs
.proc_ent
,
1449 &cache_file_operations_procfs
, cd
);
1450 cd
->u
.procfs
.channel_ent
= p
;
1454 if (cd
->cache_show
) {
1455 p
= proc_create_data("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
1456 cd
->u
.procfs
.proc_ent
,
1457 &content_file_operations_procfs
, cd
);
1458 cd
->u
.procfs
.content_ent
= p
;
1464 remove_cache_proc_entries(cd
);
1467 #else /* CONFIG_PROC_FS */
1468 static int create_cache_proc_entries(struct cache_detail
*cd
)
1474 int cache_register(struct cache_detail
*cd
)
1478 sunrpc_init_cache_detail(cd
);
1479 ret
= create_cache_proc_entries(cd
);
1481 sunrpc_destroy_cache_detail(cd
);
1484 EXPORT_SYMBOL_GPL(cache_register
);
1486 void cache_unregister(struct cache_detail
*cd
)
1488 remove_cache_proc_entries(cd
);
1489 sunrpc_destroy_cache_detail(cd
);
1491 EXPORT_SYMBOL_GPL(cache_unregister
);
1493 static ssize_t
cache_read_pipefs(struct file
*filp
, char __user
*buf
,
1494 size_t count
, loff_t
*ppos
)
1496 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1498 return cache_read(filp
, buf
, count
, ppos
, cd
);
1501 static ssize_t
cache_write_pipefs(struct file
*filp
, const char __user
*buf
,
1502 size_t count
, loff_t
*ppos
)
1504 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1506 return cache_write(filp
, buf
, count
, ppos
, cd
);
1509 static unsigned int cache_poll_pipefs(struct file
*filp
, poll_table
*wait
)
1511 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1513 return cache_poll(filp
, wait
, cd
);
1516 static int cache_ioctl_pipefs(struct inode
*inode
, struct file
*filp
,
1517 unsigned int cmd
, unsigned long arg
)
1519 struct cache_detail
*cd
= RPC_I(inode
)->private;
1521 return cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1524 static int cache_open_pipefs(struct inode
*inode
, struct file
*filp
)
1526 struct cache_detail
*cd
= RPC_I(inode
)->private;
1528 return cache_open(inode
, filp
, cd
);
1531 static int cache_release_pipefs(struct inode
*inode
, struct file
*filp
)
1533 struct cache_detail
*cd
= RPC_I(inode
)->private;
1535 return cache_release(inode
, filp
, cd
);
1538 const struct file_operations cache_file_operations_pipefs
= {
1539 .owner
= THIS_MODULE
,
1540 .llseek
= no_llseek
,
1541 .read
= cache_read_pipefs
,
1542 .write
= cache_write_pipefs
,
1543 .poll
= cache_poll_pipefs
,
1544 .ioctl
= cache_ioctl_pipefs
, /* for FIONREAD */
1545 .open
= cache_open_pipefs
,
1546 .release
= cache_release_pipefs
,
1549 static int content_open_pipefs(struct inode
*inode
, struct file
*filp
)
1551 struct cache_detail
*cd
= RPC_I(inode
)->private;
1553 return content_open(inode
, filp
, cd
);
1556 static int content_release_pipefs(struct inode
*inode
, struct file
*filp
)
1558 struct cache_detail
*cd
= RPC_I(inode
)->private;
1560 return content_release(inode
, filp
, cd
);
1563 const struct file_operations content_file_operations_pipefs
= {
1564 .open
= content_open_pipefs
,
1566 .llseek
= seq_lseek
,
1567 .release
= content_release_pipefs
,
1570 static int open_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1572 struct cache_detail
*cd
= RPC_I(inode
)->private;
1574 return open_flush(inode
, filp
, cd
);
1577 static int release_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1579 struct cache_detail
*cd
= RPC_I(inode
)->private;
1581 return release_flush(inode
, filp
, cd
);
1584 static ssize_t
read_flush_pipefs(struct file
*filp
, char __user
*buf
,
1585 size_t count
, loff_t
*ppos
)
1587 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1589 return read_flush(filp
, buf
, count
, ppos
, cd
);
1592 static ssize_t
write_flush_pipefs(struct file
*filp
,
1593 const char __user
*buf
,
1594 size_t count
, loff_t
*ppos
)
1596 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1598 return write_flush(filp
, buf
, count
, ppos
, cd
);
1601 const struct file_operations cache_flush_operations_pipefs
= {
1602 .open
= open_flush_pipefs
,
1603 .read
= read_flush_pipefs
,
1604 .write
= write_flush_pipefs
,
1605 .release
= release_flush_pipefs
,
1608 int sunrpc_cache_register_pipefs(struct dentry
*parent
,
1609 const char *name
, mode_t umode
,
1610 struct cache_detail
*cd
)
1616 sunrpc_init_cache_detail(cd
);
1618 q
.len
= strlen(name
);
1619 q
.hash
= full_name_hash(q
.name
, q
.len
);
1620 dir
= rpc_create_cache_dir(parent
, &q
, umode
, cd
);
1622 cd
->u
.pipefs
.dir
= dir
;
1624 sunrpc_destroy_cache_detail(cd
);
1629 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs
);
1631 void sunrpc_cache_unregister_pipefs(struct cache_detail
*cd
)
1633 rpc_remove_cache_dir(cd
->u
.pipefs
.dir
);
1634 cd
->u
.pipefs
.dir
= NULL
;
1635 sunrpc_destroy_cache_detail(cd
);
1637 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs
);