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 <asm/ioctls.h>
31 #include <linux/sunrpc/types.h>
32 #include <linux/sunrpc/cache.h>
33 #include <linux/sunrpc/stats.h>
35 #define RPCDBG_FACILITY RPCDBG_CACHE
37 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
);
38 static void cache_revisit_request(struct cache_head
*item
);
40 static void cache_init(struct cache_head
*h
)
42 time_t now
= get_seconds();
46 h
->expiry_time
= now
+ CACHE_NEW_EXPIRY
;
47 h
->last_refresh
= now
;
50 struct cache_head
*sunrpc_cache_lookup(struct cache_detail
*detail
,
51 struct cache_head
*key
, int hash
)
53 struct cache_head
**head
, **hp
;
54 struct cache_head
*new = NULL
;
56 head
= &detail
->hash_table
[hash
];
58 read_lock(&detail
->hash_lock
);
60 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
61 struct cache_head
*tmp
= *hp
;
62 if (detail
->match(tmp
, key
)) {
64 read_unlock(&detail
->hash_lock
);
68 read_unlock(&detail
->hash_lock
);
69 /* Didn't find anything, insert an empty entry */
71 new = detail
->alloc();
74 /* must fully initialise 'new', else
75 * we might get lose if we need to
79 detail
->init(new, key
);
81 write_lock(&detail
->hash_lock
);
83 /* check if entry appeared while we slept */
84 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
85 struct cache_head
*tmp
= *hp
;
86 if (detail
->match(tmp
, key
)) {
88 write_unlock(&detail
->hash_lock
);
89 cache_put(new, detail
);
97 write_unlock(&detail
->hash_lock
);
101 EXPORT_SYMBOL(sunrpc_cache_lookup
);
104 static void queue_loose(struct cache_detail
*detail
, struct cache_head
*ch
);
106 static int cache_fresh_locked(struct cache_head
*head
, time_t expiry
)
108 head
->expiry_time
= expiry
;
109 head
->last_refresh
= get_seconds();
110 return !test_and_set_bit(CACHE_VALID
, &head
->flags
);
113 static void cache_fresh_unlocked(struct cache_head
*head
,
114 struct cache_detail
*detail
, int new)
117 cache_revisit_request(head
);
118 if (test_and_clear_bit(CACHE_PENDING
, &head
->flags
)) {
119 cache_revisit_request(head
);
120 queue_loose(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
;
135 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
136 write_lock(&detail
->hash_lock
);
137 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
138 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
139 set_bit(CACHE_NEGATIVE
, &old
->flags
);
141 detail
->update(old
, new);
142 is_new
= cache_fresh_locked(old
, new->expiry_time
);
143 write_unlock(&detail
->hash_lock
);
144 cache_fresh_unlocked(old
, detail
, is_new
);
147 write_unlock(&detail
->hash_lock
);
149 /* We need to insert a new entry */
150 tmp
= detail
->alloc();
152 cache_put(old
, detail
);
156 detail
->init(tmp
, old
);
157 head
= &detail
->hash_table
[hash
];
159 write_lock(&detail
->hash_lock
);
160 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
161 set_bit(CACHE_NEGATIVE
, &tmp
->flags
);
163 detail
->update(tmp
, new);
168 is_new
= cache_fresh_locked(tmp
, new->expiry_time
);
169 cache_fresh_locked(old
, 0);
170 write_unlock(&detail
->hash_lock
);
171 cache_fresh_unlocked(tmp
, detail
, is_new
);
172 cache_fresh_unlocked(old
, detail
, 0);
173 cache_put(old
, detail
);
176 EXPORT_SYMBOL(sunrpc_cache_update
);
178 static int cache_make_upcall(struct cache_detail
*detail
, struct cache_head
*h
);
180 * This is the generic cache management routine for all
181 * the authentication caches.
182 * It checks the currency of a cache item and will (later)
183 * initiate an upcall to fill it if needed.
186 * Returns 0 if the cache_head can be used, or cache_puts it and returns
187 * -EAGAIN if upcall is pending,
188 * -ETIMEDOUT if upcall failed and should be retried,
189 * -ENOENT if cache entry was negative
191 int cache_check(struct cache_detail
*detail
,
192 struct cache_head
*h
, struct cache_req
*rqstp
)
195 long refresh_age
, age
;
197 /* First decide return status as best we can */
198 if (!test_bit(CACHE_VALID
, &h
->flags
) ||
199 h
->expiry_time
< get_seconds())
201 else if (detail
->flush_time
> h
->last_refresh
)
205 if (test_bit(CACHE_NEGATIVE
, &h
->flags
))
210 /* now see if we want to start an upcall */
211 refresh_age
= (h
->expiry_time
- h
->last_refresh
);
212 age
= get_seconds() - h
->last_refresh
;
217 } else if (rv
== -EAGAIN
|| age
> refresh_age
/2) {
218 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
220 if (!test_and_set_bit(CACHE_PENDING
, &h
->flags
)) {
221 switch (cache_make_upcall(detail
, h
)) {
223 clear_bit(CACHE_PENDING
, &h
->flags
);
225 set_bit(CACHE_NEGATIVE
, &h
->flags
);
226 cache_fresh_unlocked(h
, detail
,
227 cache_fresh_locked(h
, get_seconds()+CACHE_NEW_EXPIRY
));
233 clear_bit(CACHE_PENDING
, &h
->flags
);
234 cache_revisit_request(h
);
241 if (cache_defer_req(rqstp
, h
) != 0)
245 cache_put(h
, detail
);
248 EXPORT_SYMBOL(cache_check
);
251 * caches need to be periodically cleaned.
252 * For this we maintain a list of cache_detail and
253 * a current pointer into that list and into the table
256 * Each time clean_cache is called it finds the next non-empty entry
257 * in the current table and walks the list in that entry
258 * looking for entries that can be removed.
260 * An entry gets removed if:
261 * - The expiry is before current time
262 * - The last_refresh time is before the flush_time for that cache
264 * later we might drop old entries with non-NEVER expiry if that table
265 * is getting 'full' for some definition of 'full'
267 * The question of "how often to scan a table" is an interesting one
268 * and is answered in part by the use of the "nextcheck" field in the
270 * When a scan of a table begins, the nextcheck field is set to a time
271 * that is well into the future.
272 * While scanning, if an expiry time is found that is earlier than the
273 * current nextcheck time, nextcheck is set to that expiry time.
274 * If the flush_time is ever set to a time earlier than the nextcheck
275 * time, the nextcheck time is then set to that flush_time.
277 * A table is then only scanned if the current time is at least
278 * the nextcheck time.
282 static LIST_HEAD(cache_list
);
283 static DEFINE_SPINLOCK(cache_list_lock
);
284 static struct cache_detail
*current_detail
;
285 static int current_index
;
287 static const struct file_operations cache_file_operations
;
288 static const struct file_operations content_file_operations
;
289 static const struct file_operations cache_flush_operations
;
291 static void do_cache_clean(struct work_struct
*work
);
292 static DECLARE_DELAYED_WORK(cache_cleaner
, do_cache_clean
);
294 static void remove_cache_proc_entries(struct cache_detail
*cd
)
296 if (cd
->proc_ent
== NULL
)
299 remove_proc_entry("flush", cd
->proc_ent
);
301 remove_proc_entry("channel", cd
->proc_ent
);
303 remove_proc_entry("content", cd
->proc_ent
);
305 remove_proc_entry(cd
->name
, proc_net_rpc
);
308 #ifdef CONFIG_PROC_FS
309 static int create_cache_proc_entries(struct cache_detail
*cd
)
311 struct proc_dir_entry
*p
;
313 cd
->proc_ent
= proc_mkdir(cd
->name
, proc_net_rpc
);
314 if (cd
->proc_ent
== NULL
)
316 cd
->proc_ent
->owner
= cd
->owner
;
317 cd
->channel_ent
= cd
->content_ent
= NULL
;
319 <<<<<<< HEAD
:net
/sunrpc
/cache
.c
320 p
= create_proc_entry("flush", S_IFREG
|S_IRUSR
|S_IWUSR
, cd
->proc_ent
);
322 p
= proc_create("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
323 cd
->proc_ent
, &cache_flush_operations
);
324 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/sunrpc
/cache
.c
328 <<<<<<< HEAD
:net
/sunrpc
/cache
.c
329 p
->proc_fops
= &cache_flush_operations
;
331 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/sunrpc
/cache
.c
332 p
->owner
= cd
->owner
;
335 if (cd
->cache_request
|| cd
->cache_parse
) {
336 <<<<<<< HEAD
:net
/sunrpc
/cache
.c
337 p
= create_proc_entry("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
340 p
= proc_create("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
341 cd
->proc_ent
, &cache_file_operations
);
342 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/sunrpc
/cache
.c
346 <<<<<<< HEAD
:net
/sunrpc
/cache
.c
347 p
->proc_fops
= &cache_file_operations
;
349 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/sunrpc
/cache
.c
350 p
->owner
= cd
->owner
;
353 if (cd
->cache_show
) {
354 <<<<<<< HEAD
:net
/sunrpc
/cache
.c
355 p
= create_proc_entry("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
358 p
= proc_create("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
359 cd
->proc_ent
, &content_file_operations
);
360 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/sunrpc
/cache
.c
364 <<<<<<< HEAD
:net
/sunrpc
/cache
.c
365 p
->proc_fops
= &content_file_operations
;
367 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/sunrpc
/cache
.c
368 p
->owner
= cd
->owner
;
373 remove_cache_proc_entries(cd
);
376 #else /* CONFIG_PROC_FS */
377 static int create_cache_proc_entries(struct cache_detail
*cd
)
383 int cache_register(struct cache_detail
*cd
)
387 ret
= create_cache_proc_entries(cd
);
390 rwlock_init(&cd
->hash_lock
);
391 INIT_LIST_HEAD(&cd
->queue
);
392 spin_lock(&cache_list_lock
);
395 atomic_set(&cd
->readers
, 0);
398 list_add(&cd
->others
, &cache_list
);
399 spin_unlock(&cache_list_lock
);
401 /* start the cleaning process */
402 schedule_delayed_work(&cache_cleaner
, 0);
405 EXPORT_SYMBOL(cache_register
);
407 void cache_unregister(struct cache_detail
*cd
)
410 spin_lock(&cache_list_lock
);
411 write_lock(&cd
->hash_lock
);
412 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
413 write_unlock(&cd
->hash_lock
);
414 spin_unlock(&cache_list_lock
);
417 if (current_detail
== cd
)
418 current_detail
= NULL
;
419 list_del_init(&cd
->others
);
420 write_unlock(&cd
->hash_lock
);
421 spin_unlock(&cache_list_lock
);
422 remove_cache_proc_entries(cd
);
423 if (list_empty(&cache_list
)) {
424 /* module must be being unloaded so its safe to kill the worker */
425 cancel_delayed_work_sync(&cache_cleaner
);
429 printk(KERN_ERR
"nfsd: failed to unregister %s cache\n", cd
->name
);
431 EXPORT_SYMBOL(cache_unregister
);
433 /* clean cache tries to find something to clean
435 * It returns 1 if it cleaned something,
436 * 0 if it didn't find anything this time
437 * -1 if it fell off the end of the list.
439 static int cache_clean(void)
442 struct list_head
*next
;
444 spin_lock(&cache_list_lock
);
446 /* find a suitable table if we don't already have one */
447 while (current_detail
== NULL
||
448 current_index
>= current_detail
->hash_size
) {
450 next
= current_detail
->others
.next
;
452 next
= cache_list
.next
;
453 if (next
== &cache_list
) {
454 current_detail
= NULL
;
455 spin_unlock(&cache_list_lock
);
458 current_detail
= list_entry(next
, struct cache_detail
, others
);
459 if (current_detail
->nextcheck
> get_seconds())
460 current_index
= current_detail
->hash_size
;
463 current_detail
->nextcheck
= get_seconds()+30*60;
467 /* find a non-empty bucket in the table */
468 while (current_detail
&&
469 current_index
< current_detail
->hash_size
&&
470 current_detail
->hash_table
[current_index
] == NULL
)
473 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
475 if (current_detail
&& current_index
< current_detail
->hash_size
) {
476 struct cache_head
*ch
, **cp
;
477 struct cache_detail
*d
;
479 write_lock(¤t_detail
->hash_lock
);
481 /* Ok, now to clean this strand */
483 cp
= & current_detail
->hash_table
[current_index
];
485 for (; ch
; cp
= & ch
->next
, ch
= *cp
) {
486 if (current_detail
->nextcheck
> ch
->expiry_time
)
487 current_detail
->nextcheck
= ch
->expiry_time
+1;
488 if (ch
->expiry_time
>= get_seconds()
489 && ch
->last_refresh
>= current_detail
->flush_time
492 if (test_and_clear_bit(CACHE_PENDING
, &ch
->flags
))
493 queue_loose(current_detail
, ch
);
495 if (atomic_read(&ch
->ref
.refcount
) == 1)
501 current_detail
->entries
--;
504 write_unlock(¤t_detail
->hash_lock
);
508 spin_unlock(&cache_list_lock
);
512 spin_unlock(&cache_list_lock
);
518 * We want to regularly clean the cache, so we need to schedule some work ...
520 static void do_cache_clean(struct work_struct
*work
)
523 if (cache_clean() == -1)
526 if (list_empty(&cache_list
))
530 schedule_delayed_work(&cache_cleaner
, delay
);
535 * Clean all caches promptly. This just calls cache_clean
536 * repeatedly until we are sure that every cache has had a chance to
539 void cache_flush(void)
541 while (cache_clean() != -1)
543 while (cache_clean() != -1)
546 EXPORT_SYMBOL(cache_flush
);
548 void cache_purge(struct cache_detail
*detail
)
550 detail
->flush_time
= LONG_MAX
;
551 detail
->nextcheck
= get_seconds();
553 detail
->flush_time
= 1;
555 EXPORT_SYMBOL(cache_purge
);
559 * Deferral and Revisiting of Requests.
561 * If a cache lookup finds a pending entry, we
562 * need to defer the request and revisit it later.
563 * All deferred requests are stored in a hash table,
564 * indexed by "struct cache_head *".
565 * As it may be wasteful to store a whole request
566 * structure, we allow the request to provide a
567 * deferred form, which must contain a
568 * 'struct cache_deferred_req'
569 * This cache_deferred_req contains a method to allow
570 * it to be revisited when cache info is available
573 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
574 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
576 #define DFR_MAX 300 /* ??? */
578 static DEFINE_SPINLOCK(cache_defer_lock
);
579 static LIST_HEAD(cache_defer_list
);
580 static struct list_head cache_defer_hash
[DFR_HASHSIZE
];
581 static int cache_defer_cnt
;
583 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
585 struct cache_deferred_req
*dreq
;
586 int hash
= DFR_HASH(item
);
588 if (cache_defer_cnt
>= DFR_MAX
) {
589 /* too much in the cache, randomly drop this one,
590 * or continue and drop the oldest below
595 dreq
= req
->defer(req
);
600 dreq
->recv_time
= get_seconds();
602 spin_lock(&cache_defer_lock
);
604 list_add(&dreq
->recent
, &cache_defer_list
);
606 if (cache_defer_hash
[hash
].next
== NULL
)
607 INIT_LIST_HEAD(&cache_defer_hash
[hash
]);
608 list_add(&dreq
->hash
, &cache_defer_hash
[hash
]);
610 /* it is in, now maybe clean up */
612 if (++cache_defer_cnt
> DFR_MAX
) {
613 dreq
= list_entry(cache_defer_list
.prev
,
614 struct cache_deferred_req
, recent
);
615 list_del(&dreq
->recent
);
616 list_del(&dreq
->hash
);
619 spin_unlock(&cache_defer_lock
);
622 /* there was one too many */
623 dreq
->revisit(dreq
, 1);
625 if (!test_bit(CACHE_PENDING
, &item
->flags
)) {
626 /* must have just been validated... */
627 cache_revisit_request(item
);
632 static void cache_revisit_request(struct cache_head
*item
)
634 struct cache_deferred_req
*dreq
;
635 struct list_head pending
;
637 struct list_head
*lp
;
638 int hash
= DFR_HASH(item
);
640 INIT_LIST_HEAD(&pending
);
641 spin_lock(&cache_defer_lock
);
643 lp
= cache_defer_hash
[hash
].next
;
645 while (lp
!= &cache_defer_hash
[hash
]) {
646 dreq
= list_entry(lp
, struct cache_deferred_req
, hash
);
648 if (dreq
->item
== item
) {
649 list_del(&dreq
->hash
);
650 list_move(&dreq
->recent
, &pending
);
655 spin_unlock(&cache_defer_lock
);
657 while (!list_empty(&pending
)) {
658 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
659 list_del_init(&dreq
->recent
);
660 dreq
->revisit(dreq
, 0);
664 void cache_clean_deferred(void *owner
)
666 struct cache_deferred_req
*dreq
, *tmp
;
667 struct list_head pending
;
670 INIT_LIST_HEAD(&pending
);
671 spin_lock(&cache_defer_lock
);
673 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
674 if (dreq
->owner
== owner
) {
675 list_del(&dreq
->hash
);
676 list_move(&dreq
->recent
, &pending
);
680 spin_unlock(&cache_defer_lock
);
682 while (!list_empty(&pending
)) {
683 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
684 list_del_init(&dreq
->recent
);
685 dreq
->revisit(dreq
, 1);
690 * communicate with user-space
692 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
693 * On read, you get a full request, or block.
694 * On write, an update request is processed.
695 * Poll works if anything to read, and always allows write.
697 * Implemented by linked list of requests. Each open file has
698 * a ->private that also exists in this list. New requests are added
699 * to the end and may wakeup and preceding readers.
700 * New readers are added to the head. If, on read, an item is found with
701 * CACHE_UPCALLING clear, we free it from the list.
705 static DEFINE_SPINLOCK(queue_lock
);
706 static DEFINE_MUTEX(queue_io_mutex
);
709 struct list_head list
;
710 int reader
; /* if 0, then request */
712 struct cache_request
{
713 struct cache_queue q
;
714 struct cache_head
*item
;
719 struct cache_reader
{
720 struct cache_queue q
;
721 int offset
; /* if non-0, we have a refcnt on next request */
725 cache_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
727 struct cache_reader
*rp
= filp
->private_data
;
728 struct cache_request
*rq
;
729 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
735 mutex_lock(&queue_io_mutex
); /* protect against multiple concurrent
736 * readers on this file */
738 spin_lock(&queue_lock
);
739 /* need to find next request */
740 while (rp
->q
.list
.next
!= &cd
->queue
&&
741 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
743 struct list_head
*next
= rp
->q
.list
.next
;
744 list_move(&rp
->q
.list
, next
);
746 if (rp
->q
.list
.next
== &cd
->queue
) {
747 spin_unlock(&queue_lock
);
748 mutex_unlock(&queue_io_mutex
);
752 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
753 BUG_ON(rq
->q
.reader
);
756 spin_unlock(&queue_lock
);
758 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
760 spin_lock(&queue_lock
);
761 list_move(&rp
->q
.list
, &rq
->q
.list
);
762 spin_unlock(&queue_lock
);
764 if (rp
->offset
+ count
> rq
->len
)
765 count
= rq
->len
- rp
->offset
;
767 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
770 if (rp
->offset
>= rq
->len
) {
772 spin_lock(&queue_lock
);
773 list_move(&rp
->q
.list
, &rq
->q
.list
);
774 spin_unlock(&queue_lock
);
779 if (rp
->offset
== 0) {
780 /* need to release rq */
781 spin_lock(&queue_lock
);
783 if (rq
->readers
== 0 &&
784 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
785 list_del(&rq
->q
.list
);
786 spin_unlock(&queue_lock
);
787 cache_put(rq
->item
, cd
);
791 spin_unlock(&queue_lock
);
795 mutex_unlock(&queue_io_mutex
);
796 return err
? err
: count
;
799 static char write_buf
[8192]; /* protected by queue_io_mutex */
802 cache_write(struct file
*filp
, const char __user
*buf
, size_t count
,
806 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
810 if (count
>= sizeof(write_buf
))
813 mutex_lock(&queue_io_mutex
);
815 if (copy_from_user(write_buf
, buf
, count
)) {
816 mutex_unlock(&queue_io_mutex
);
819 write_buf
[count
] = '\0';
821 err
= cd
->cache_parse(cd
, write_buf
, count
);
825 mutex_unlock(&queue_io_mutex
);
826 return err
? err
: count
;
829 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
832 cache_poll(struct file
*filp
, poll_table
*wait
)
835 struct cache_reader
*rp
= filp
->private_data
;
836 struct cache_queue
*cq
;
837 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
839 poll_wait(filp
, &queue_wait
, wait
);
841 /* alway allow write */
842 mask
= POLL_OUT
| POLLWRNORM
;
847 spin_lock(&queue_lock
);
849 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
850 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
852 mask
|= POLLIN
| POLLRDNORM
;
855 spin_unlock(&queue_lock
);
860 cache_ioctl(struct inode
*ino
, struct file
*filp
,
861 unsigned int cmd
, unsigned long arg
)
864 struct cache_reader
*rp
= filp
->private_data
;
865 struct cache_queue
*cq
;
866 struct cache_detail
*cd
= PDE(ino
)->data
;
868 if (cmd
!= FIONREAD
|| !rp
)
871 spin_lock(&queue_lock
);
873 /* only find the length remaining in current request,
874 * or the length of the next request
876 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
877 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
879 struct cache_request
*cr
=
880 container_of(cq
, struct cache_request
, q
);
881 len
= cr
->len
- rp
->offset
;
884 spin_unlock(&queue_lock
);
886 return put_user(len
, (int __user
*)arg
);
890 cache_open(struct inode
*inode
, struct file
*filp
)
892 struct cache_reader
*rp
= NULL
;
894 nonseekable_open(inode
, filp
);
895 if (filp
->f_mode
& FMODE_READ
) {
896 struct cache_detail
*cd
= PDE(inode
)->data
;
898 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
903 atomic_inc(&cd
->readers
);
904 spin_lock(&queue_lock
);
905 list_add(&rp
->q
.list
, &cd
->queue
);
906 spin_unlock(&queue_lock
);
908 filp
->private_data
= rp
;
913 cache_release(struct inode
*inode
, struct file
*filp
)
915 struct cache_reader
*rp
= filp
->private_data
;
916 struct cache_detail
*cd
= PDE(inode
)->data
;
919 spin_lock(&queue_lock
);
921 struct cache_queue
*cq
;
922 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
923 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
925 container_of(cq
, struct cache_request
, q
)
931 list_del(&rp
->q
.list
);
932 spin_unlock(&queue_lock
);
934 filp
->private_data
= NULL
;
937 cd
->last_close
= get_seconds();
938 atomic_dec(&cd
->readers
);
945 static const struct file_operations cache_file_operations
= {
946 .owner
= THIS_MODULE
,
949 .write
= cache_write
,
951 .ioctl
= cache_ioctl
, /* for FIONREAD */
953 .release
= cache_release
,
957 static void queue_loose(struct cache_detail
*detail
, struct cache_head
*ch
)
959 struct cache_queue
*cq
;
960 spin_lock(&queue_lock
);
961 list_for_each_entry(cq
, &detail
->queue
, list
)
963 struct cache_request
*cr
= container_of(cq
, struct cache_request
, q
);
966 if (cr
->readers
!= 0)
968 list_del(&cr
->q
.list
);
969 spin_unlock(&queue_lock
);
970 cache_put(cr
->item
, detail
);
975 spin_unlock(&queue_lock
);
979 * Support routines for text-based upcalls.
980 * Fields are separated by spaces.
981 * Fields are either mangled to quote space tab newline slosh with slosh
982 * or a hexified with a leading \x
983 * Record is terminated with newline.
987 void qword_add(char **bpp
, int *lp
, char *str
)
995 while ((c
=*str
++) && len
)
1003 *bp
++ = '0' + ((c
& 0300)>>6);
1004 *bp
++ = '0' + ((c
& 0070)>>3);
1005 *bp
++ = '0' + ((c
& 0007)>>0);
1013 if (c
|| len
<1) len
= -1;
1021 EXPORT_SYMBOL(qword_add
);
1023 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
1028 if (len
< 0) return;
1034 while (blen
&& len
>= 2) {
1035 unsigned char c
= *buf
++;
1036 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
1037 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
1042 if (blen
|| len
<1) len
= -1;
1050 EXPORT_SYMBOL(qword_addhex
);
1052 static void warn_no_listener(struct cache_detail
*detail
)
1054 if (detail
->last_warn
!= detail
->last_close
) {
1055 detail
->last_warn
= detail
->last_close
;
1056 if (detail
->warn_no_listener
)
1057 detail
->warn_no_listener(detail
);
1062 * register an upcall request to user-space.
1063 * Each request is at most one page long.
1065 static int cache_make_upcall(struct cache_detail
*detail
, struct cache_head
*h
)
1069 struct cache_request
*crq
;
1073 if (detail
->cache_request
== NULL
)
1076 if (atomic_read(&detail
->readers
) == 0 &&
1077 detail
->last_close
< get_seconds() - 30) {
1078 warn_no_listener(detail
);
1082 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1086 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1092 bp
= buf
; len
= PAGE_SIZE
;
1094 detail
->cache_request(detail
, h
, &bp
, &len
);
1102 crq
->item
= cache_get(h
);
1104 crq
->len
= PAGE_SIZE
- len
;
1106 spin_lock(&queue_lock
);
1107 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1108 spin_unlock(&queue_lock
);
1109 wake_up(&queue_wait
);
1114 * parse a message from user-space and pass it
1115 * to an appropriate cache
1116 * Messages are, like requests, separated into fields by
1117 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1120 * reply cachename expiry key ... content....
1122 * key and content are both parsed by cache
1125 #define isodigit(c) (isdigit(c) && c <= '7')
1126 int qword_get(char **bpp
, char *dest
, int bufsize
)
1128 /* return bytes copied, or -1 on error */
1132 while (*bp
== ' ') bp
++;
1134 if (bp
[0] == '\\' && bp
[1] == 'x') {
1137 while (isxdigit(bp
[0]) && isxdigit(bp
[1]) && len
< bufsize
) {
1138 int byte
= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1141 byte
|= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1147 /* text with \nnn octal quoting */
1148 while (*bp
!= ' ' && *bp
!= '\n' && *bp
&& len
< bufsize
-1) {
1150 isodigit(bp
[1]) && (bp
[1] <= '3') &&
1153 int byte
= (*++bp
-'0');
1155 byte
= (byte
<< 3) | (*bp
++ - '0');
1156 byte
= (byte
<< 3) | (*bp
++ - '0');
1166 if (*bp
!= ' ' && *bp
!= '\n' && *bp
!= '\0')
1168 while (*bp
== ' ') bp
++;
1173 EXPORT_SYMBOL(qword_get
);
1177 * support /proc/sunrpc/cache/$CACHENAME/content
1179 * We call ->cache_show passing NULL for the item to
1180 * get a header, then pass each real item in the cache
1184 struct cache_detail
*cd
;
1187 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1188 __acquires(cd
->hash_lock
)
1191 unsigned hash
, entry
;
1192 struct cache_head
*ch
;
1193 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1196 read_lock(&cd
->hash_lock
);
1198 return SEQ_START_TOKEN
;
1200 entry
= n
& ((1LL<<32) - 1);
1202 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1205 n
&= ~((1LL<<32) - 1);
1209 } while(hash
< cd
->hash_size
&&
1210 cd
->hash_table
[hash
]==NULL
);
1211 if (hash
>= cd
->hash_size
)
1214 return cd
->hash_table
[hash
];
1217 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1219 struct cache_head
*ch
= p
;
1220 int hash
= (*pos
>> 32);
1221 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1223 if (p
== SEQ_START_TOKEN
)
1225 else if (ch
->next
== NULL
) {
1232 *pos
&= ~((1LL<<32) - 1);
1233 while (hash
< cd
->hash_size
&&
1234 cd
->hash_table
[hash
] == NULL
) {
1238 if (hash
>= cd
->hash_size
)
1241 return cd
->hash_table
[hash
];
1244 static void c_stop(struct seq_file
*m
, void *p
)
1245 __releases(cd
->hash_lock
)
1247 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1248 read_unlock(&cd
->hash_lock
);
1251 static int c_show(struct seq_file
*m
, void *p
)
1253 struct cache_head
*cp
= p
;
1254 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1256 if (p
== SEQ_START_TOKEN
)
1257 return cd
->cache_show(m
, cd
, NULL
);
1260 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1261 cp
->expiry_time
, atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1263 if (cache_check(cd
, cp
, NULL
))
1264 /* cache_check does a cache_put on failure */
1265 seq_printf(m
, "# ");
1269 return cd
->cache_show(m
, cd
, cp
);
1272 static const struct seq_operations cache_content_op
= {
1279 static int content_open(struct inode
*inode
, struct file
*file
)
1282 struct cache_detail
*cd
= PDE(inode
)->data
;
1284 han
= __seq_open_private(file
, &cache_content_op
, sizeof(*han
));
1292 static const struct file_operations content_file_operations
= {
1293 .open
= content_open
,
1295 .llseek
= seq_lseek
,
1296 .release
= seq_release_private
,
1299 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1300 size_t count
, loff_t
*ppos
)
1302 struct cache_detail
*cd
= PDE(file
->f_path
.dentry
->d_inode
)->data
;
1304 unsigned long p
= *ppos
;
1307 sprintf(tbuf
, "%lu\n", cd
->flush_time
);
1314 if (copy_to_user(buf
, (void*)(tbuf
+p
), len
))
1320 static ssize_t
write_flush(struct file
* file
, const char __user
* buf
,
1321 size_t count
, loff_t
*ppos
)
1323 struct cache_detail
*cd
= PDE(file
->f_path
.dentry
->d_inode
)->data
;
1327 if (*ppos
|| count
> sizeof(tbuf
)-1)
1329 if (copy_from_user(tbuf
, buf
, count
))
1332 flushtime
= simple_strtoul(tbuf
, &ep
, 0);
1333 if (*ep
&& *ep
!= '\n')
1336 cd
->flush_time
= flushtime
;
1337 cd
->nextcheck
= get_seconds();
1344 static const struct file_operations cache_flush_operations
= {
1345 .open
= nonseekable_open
,
1347 .write
= write_flush
,