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 void 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 * -ENOENT if cache entry was negative
190 int cache_check(struct cache_detail
*detail
,
191 struct cache_head
*h
, struct cache_req
*rqstp
)
194 long refresh_age
, age
;
196 /* First decide return status as best we can */
197 if (!test_bit(CACHE_VALID
, &h
->flags
) ||
198 h
->expiry_time
< get_seconds())
200 else if (detail
->flush_time
> h
->last_refresh
)
204 if (test_bit(CACHE_NEGATIVE
, &h
->flags
))
209 /* now see if we want to start an upcall */
210 refresh_age
= (h
->expiry_time
- h
->last_refresh
);
211 age
= get_seconds() - h
->last_refresh
;
216 } else if (rv
== -EAGAIN
|| age
> refresh_age
/2) {
217 dprintk("Want update, refage=%ld, age=%ld\n", refresh_age
, age
);
218 if (!test_and_set_bit(CACHE_PENDING
, &h
->flags
)) {
219 switch (cache_make_upcall(detail
, h
)) {
221 clear_bit(CACHE_PENDING
, &h
->flags
);
223 set_bit(CACHE_NEGATIVE
, &h
->flags
);
224 cache_fresh_unlocked(h
, detail
,
225 cache_fresh_locked(h
, get_seconds()+CACHE_NEW_EXPIRY
));
231 clear_bit(CACHE_PENDING
, &h
->flags
);
232 cache_revisit_request(h
);
239 cache_defer_req(rqstp
, h
);
242 cache_put(h
, detail
);
247 * caches need to be periodically cleaned.
248 * For this we maintain a list of cache_detail and
249 * a current pointer into that list and into the table
252 * Each time clean_cache is called it finds the next non-empty entry
253 * in the current table and walks the list in that entry
254 * looking for entries that can be removed.
256 * An entry gets removed if:
257 * - The expiry is before current time
258 * - The last_refresh time is before the flush_time for that cache
260 * later we might drop old entries with non-NEVER expiry if that table
261 * is getting 'full' for some definition of 'full'
263 * The question of "how often to scan a table" is an interesting one
264 * and is answered in part by the use of the "nextcheck" field in the
266 * When a scan of a table begins, the nextcheck field is set to a time
267 * that is well into the future.
268 * While scanning, if an expiry time is found that is earlier than the
269 * current nextcheck time, nextcheck is set to that expiry time.
270 * If the flush_time is ever set to a time earlier than the nextcheck
271 * time, the nextcheck time is then set to that flush_time.
273 * A table is then only scanned if the current time is at least
274 * the nextcheck time.
278 static LIST_HEAD(cache_list
);
279 static DEFINE_SPINLOCK(cache_list_lock
);
280 static struct cache_detail
*current_detail
;
281 static int current_index
;
283 static struct file_operations cache_file_operations
;
284 static struct file_operations content_file_operations
;
285 static struct file_operations cache_flush_operations
;
287 static void do_cache_clean(void *data
);
288 static DECLARE_WORK(cache_cleaner
, do_cache_clean
, NULL
);
290 void cache_register(struct cache_detail
*cd
)
292 cd
->proc_ent
= proc_mkdir(cd
->name
, proc_net_rpc
);
294 struct proc_dir_entry
*p
;
295 cd
->proc_ent
->owner
= cd
->owner
;
296 cd
->channel_ent
= cd
->content_ent
= NULL
;
298 p
= create_proc_entry("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
302 p
->proc_fops
= &cache_flush_operations
;
303 p
->owner
= cd
->owner
;
307 if (cd
->cache_request
|| cd
->cache_parse
) {
308 p
= create_proc_entry("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
312 p
->proc_fops
= &cache_file_operations
;
313 p
->owner
= cd
->owner
;
317 if (cd
->cache_show
) {
318 p
= create_proc_entry("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
322 p
->proc_fops
= &content_file_operations
;
323 p
->owner
= cd
->owner
;
328 rwlock_init(&cd
->hash_lock
);
329 INIT_LIST_HEAD(&cd
->queue
);
330 spin_lock(&cache_list_lock
);
333 atomic_set(&cd
->readers
, 0);
336 list_add(&cd
->others
, &cache_list
);
337 spin_unlock(&cache_list_lock
);
339 /* start the cleaning process */
340 schedule_work(&cache_cleaner
);
343 int cache_unregister(struct cache_detail
*cd
)
346 spin_lock(&cache_list_lock
);
347 write_lock(&cd
->hash_lock
);
348 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
349 write_unlock(&cd
->hash_lock
);
350 spin_unlock(&cache_list_lock
);
353 if (current_detail
== cd
)
354 current_detail
= NULL
;
355 list_del_init(&cd
->others
);
356 write_unlock(&cd
->hash_lock
);
357 spin_unlock(&cache_list_lock
);
360 remove_proc_entry("flush", cd
->proc_ent
);
362 remove_proc_entry("channel", cd
->proc_ent
);
364 remove_proc_entry("content", cd
->proc_ent
);
367 remove_proc_entry(cd
->name
, proc_net_rpc
);
369 if (list_empty(&cache_list
)) {
370 /* module must be being unloaded so its safe to kill the worker */
371 cancel_delayed_work(&cache_cleaner
);
372 flush_scheduled_work();
377 /* clean cache tries to find something to clean
379 * It returns 1 if it cleaned something,
380 * 0 if it didn't find anything this time
381 * -1 if it fell off the end of the list.
383 static int cache_clean(void)
386 struct list_head
*next
;
388 spin_lock(&cache_list_lock
);
390 /* find a suitable table if we don't already have one */
391 while (current_detail
== NULL
||
392 current_index
>= current_detail
->hash_size
) {
394 next
= current_detail
->others
.next
;
396 next
= cache_list
.next
;
397 if (next
== &cache_list
) {
398 current_detail
= NULL
;
399 spin_unlock(&cache_list_lock
);
402 current_detail
= list_entry(next
, struct cache_detail
, others
);
403 if (current_detail
->nextcheck
> get_seconds())
404 current_index
= current_detail
->hash_size
;
407 current_detail
->nextcheck
= get_seconds()+30*60;
411 /* find a non-empty bucket in the table */
412 while (current_detail
&&
413 current_index
< current_detail
->hash_size
&&
414 current_detail
->hash_table
[current_index
] == NULL
)
417 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
419 if (current_detail
&& current_index
< current_detail
->hash_size
) {
420 struct cache_head
*ch
, **cp
;
421 struct cache_detail
*d
;
423 write_lock(¤t_detail
->hash_lock
);
425 /* Ok, now to clean this strand */
427 cp
= & current_detail
->hash_table
[current_index
];
429 for (; ch
; cp
= & ch
->next
, ch
= *cp
) {
430 if (current_detail
->nextcheck
> ch
->expiry_time
)
431 current_detail
->nextcheck
= ch
->expiry_time
+1;
432 if (ch
->expiry_time
>= get_seconds()
433 && ch
->last_refresh
>= current_detail
->flush_time
436 if (test_and_clear_bit(CACHE_PENDING
, &ch
->flags
))
437 queue_loose(current_detail
, ch
);
439 if (atomic_read(&ch
->ref
.refcount
) == 1)
445 current_detail
->entries
--;
448 write_unlock(¤t_detail
->hash_lock
);
452 spin_unlock(&cache_list_lock
);
456 spin_unlock(&cache_list_lock
);
462 * We want to regularly clean the cache, so we need to schedule some work ...
464 static void do_cache_clean(void *data
)
467 if (cache_clean() == -1)
470 if (list_empty(&cache_list
))
474 schedule_delayed_work(&cache_cleaner
, delay
);
479 * Clean all caches promptly. This just calls cache_clean
480 * repeatedly until we are sure that every cache has had a chance to
483 void cache_flush(void)
485 while (cache_clean() != -1)
487 while (cache_clean() != -1)
491 void cache_purge(struct cache_detail
*detail
)
493 detail
->flush_time
= LONG_MAX
;
494 detail
->nextcheck
= get_seconds();
496 detail
->flush_time
= 1;
502 * Deferral and Revisiting of Requests.
504 * If a cache lookup finds a pending entry, we
505 * need to defer the request and revisit it later.
506 * All deferred requests are stored in a hash table,
507 * indexed by "struct cache_head *".
508 * As it may be wasteful to store a whole request
509 * structure, we allow the request to provide a
510 * deferred form, which must contain a
511 * 'struct cache_deferred_req'
512 * This cache_deferred_req contains a method to allow
513 * it to be revisited when cache info is available
516 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
517 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
519 #define DFR_MAX 300 /* ??? */
521 static DEFINE_SPINLOCK(cache_defer_lock
);
522 static LIST_HEAD(cache_defer_list
);
523 static struct list_head cache_defer_hash
[DFR_HASHSIZE
];
524 static int cache_defer_cnt
;
526 static void cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
528 struct cache_deferred_req
*dreq
;
529 int hash
= DFR_HASH(item
);
531 dreq
= req
->defer(req
);
536 dreq
->recv_time
= get_seconds();
538 spin_lock(&cache_defer_lock
);
540 list_add(&dreq
->recent
, &cache_defer_list
);
542 if (cache_defer_hash
[hash
].next
== NULL
)
543 INIT_LIST_HEAD(&cache_defer_hash
[hash
]);
544 list_add(&dreq
->hash
, &cache_defer_hash
[hash
]);
546 /* it is in, now maybe clean up */
548 if (++cache_defer_cnt
> DFR_MAX
) {
549 /* too much in the cache, randomly drop
553 dreq
= list_entry(cache_defer_list
.next
,
554 struct cache_deferred_req
,
557 dreq
= list_entry(cache_defer_list
.prev
,
558 struct cache_deferred_req
,
560 list_del(&dreq
->recent
);
561 list_del(&dreq
->hash
);
564 spin_unlock(&cache_defer_lock
);
567 /* there was one too many */
568 dreq
->revisit(dreq
, 1);
570 if (!test_bit(CACHE_PENDING
, &item
->flags
)) {
571 /* must have just been validated... */
572 cache_revisit_request(item
);
576 static void cache_revisit_request(struct cache_head
*item
)
578 struct cache_deferred_req
*dreq
;
579 struct list_head pending
;
581 struct list_head
*lp
;
582 int hash
= DFR_HASH(item
);
584 INIT_LIST_HEAD(&pending
);
585 spin_lock(&cache_defer_lock
);
587 lp
= cache_defer_hash
[hash
].next
;
589 while (lp
!= &cache_defer_hash
[hash
]) {
590 dreq
= list_entry(lp
, struct cache_deferred_req
, hash
);
592 if (dreq
->item
== item
) {
593 list_del(&dreq
->hash
);
594 list_move(&dreq
->recent
, &pending
);
599 spin_unlock(&cache_defer_lock
);
601 while (!list_empty(&pending
)) {
602 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
603 list_del_init(&dreq
->recent
);
604 dreq
->revisit(dreq
, 0);
608 void cache_clean_deferred(void *owner
)
610 struct cache_deferred_req
*dreq
, *tmp
;
611 struct list_head pending
;
614 INIT_LIST_HEAD(&pending
);
615 spin_lock(&cache_defer_lock
);
617 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
618 if (dreq
->owner
== owner
) {
619 list_del(&dreq
->hash
);
620 list_move(&dreq
->recent
, &pending
);
624 spin_unlock(&cache_defer_lock
);
626 while (!list_empty(&pending
)) {
627 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
628 list_del_init(&dreq
->recent
);
629 dreq
->revisit(dreq
, 1);
634 * communicate with user-space
636 * We have a magic /proc file - /proc/sunrpc/cache
637 * On read, you get a full request, or block
638 * On write, an update request is processed
639 * Poll works if anything to read, and always allows write
641 * Implemented by linked list of requests. Each open file has
642 * a ->private that also exists in this list. New request are added
643 * to the end and may wakeup and preceding readers.
644 * New readers are added to the head. If, on read, an item is found with
645 * CACHE_UPCALLING clear, we free it from the list.
649 static DEFINE_SPINLOCK(queue_lock
);
650 static DEFINE_MUTEX(queue_io_mutex
);
653 struct list_head list
;
654 int reader
; /* if 0, then request */
656 struct cache_request
{
657 struct cache_queue q
;
658 struct cache_head
*item
;
663 struct cache_reader
{
664 struct cache_queue q
;
665 int offset
; /* if non-0, we have a refcnt on next request */
669 cache_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
671 struct cache_reader
*rp
= filp
->private_data
;
672 struct cache_request
*rq
;
673 struct cache_detail
*cd
= PDE(filp
->f_dentry
->d_inode
)->data
;
679 mutex_lock(&queue_io_mutex
); /* protect against multiple concurrent
680 * readers on this file */
682 spin_lock(&queue_lock
);
683 /* need to find next request */
684 while (rp
->q
.list
.next
!= &cd
->queue
&&
685 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
687 struct list_head
*next
= rp
->q
.list
.next
;
688 list_move(&rp
->q
.list
, next
);
690 if (rp
->q
.list
.next
== &cd
->queue
) {
691 spin_unlock(&queue_lock
);
692 mutex_unlock(&queue_io_mutex
);
696 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
697 BUG_ON(rq
->q
.reader
);
700 spin_unlock(&queue_lock
);
702 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
704 spin_lock(&queue_lock
);
705 list_move(&rp
->q
.list
, &rq
->q
.list
);
706 spin_unlock(&queue_lock
);
708 if (rp
->offset
+ count
> rq
->len
)
709 count
= rq
->len
- rp
->offset
;
711 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
714 if (rp
->offset
>= rq
->len
) {
716 spin_lock(&queue_lock
);
717 list_move(&rp
->q
.list
, &rq
->q
.list
);
718 spin_unlock(&queue_lock
);
723 if (rp
->offset
== 0) {
724 /* need to release rq */
725 spin_lock(&queue_lock
);
727 if (rq
->readers
== 0 &&
728 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
729 list_del(&rq
->q
.list
);
730 spin_unlock(&queue_lock
);
731 cache_put(rq
->item
, cd
);
735 spin_unlock(&queue_lock
);
739 mutex_unlock(&queue_io_mutex
);
740 return err
? err
: count
;
743 static char write_buf
[8192]; /* protected by queue_io_mutex */
746 cache_write(struct file
*filp
, const char __user
*buf
, size_t count
,
750 struct cache_detail
*cd
= PDE(filp
->f_dentry
->d_inode
)->data
;
754 if (count
>= sizeof(write_buf
))
757 mutex_lock(&queue_io_mutex
);
759 if (copy_from_user(write_buf
, buf
, count
)) {
760 mutex_unlock(&queue_io_mutex
);
763 write_buf
[count
] = '\0';
765 err
= cd
->cache_parse(cd
, write_buf
, count
);
769 mutex_unlock(&queue_io_mutex
);
770 return err
? err
: count
;
773 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
776 cache_poll(struct file
*filp
, poll_table
*wait
)
779 struct cache_reader
*rp
= filp
->private_data
;
780 struct cache_queue
*cq
;
781 struct cache_detail
*cd
= PDE(filp
->f_dentry
->d_inode
)->data
;
783 poll_wait(filp
, &queue_wait
, wait
);
785 /* alway allow write */
786 mask
= POLL_OUT
| POLLWRNORM
;
791 spin_lock(&queue_lock
);
793 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
794 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
796 mask
|= POLLIN
| POLLRDNORM
;
799 spin_unlock(&queue_lock
);
804 cache_ioctl(struct inode
*ino
, struct file
*filp
,
805 unsigned int cmd
, unsigned long arg
)
808 struct cache_reader
*rp
= filp
->private_data
;
809 struct cache_queue
*cq
;
810 struct cache_detail
*cd
= PDE(ino
)->data
;
812 if (cmd
!= FIONREAD
|| !rp
)
815 spin_lock(&queue_lock
);
817 /* only find the length remaining in current request,
818 * or the length of the next request
820 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
821 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
823 struct cache_request
*cr
=
824 container_of(cq
, struct cache_request
, q
);
825 len
= cr
->len
- rp
->offset
;
828 spin_unlock(&queue_lock
);
830 return put_user(len
, (int __user
*)arg
);
834 cache_open(struct inode
*inode
, struct file
*filp
)
836 struct cache_reader
*rp
= NULL
;
838 nonseekable_open(inode
, filp
);
839 if (filp
->f_mode
& FMODE_READ
) {
840 struct cache_detail
*cd
= PDE(inode
)->data
;
842 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
847 atomic_inc(&cd
->readers
);
848 spin_lock(&queue_lock
);
849 list_add(&rp
->q
.list
, &cd
->queue
);
850 spin_unlock(&queue_lock
);
852 filp
->private_data
= rp
;
857 cache_release(struct inode
*inode
, struct file
*filp
)
859 struct cache_reader
*rp
= filp
->private_data
;
860 struct cache_detail
*cd
= PDE(inode
)->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
);
889 static struct file_operations cache_file_operations
= {
890 .owner
= THIS_MODULE
,
893 .write
= cache_write
,
895 .ioctl
= cache_ioctl
, /* for FIONREAD */
897 .release
= cache_release
,
901 static void queue_loose(struct cache_detail
*detail
, struct cache_head
*ch
)
903 struct cache_queue
*cq
;
904 spin_lock(&queue_lock
);
905 list_for_each_entry(cq
, &detail
->queue
, list
)
907 struct cache_request
*cr
= container_of(cq
, struct cache_request
, q
);
910 if (cr
->readers
!= 0)
912 list_del(&cr
->q
.list
);
913 spin_unlock(&queue_lock
);
914 cache_put(cr
->item
, detail
);
919 spin_unlock(&queue_lock
);
923 * Support routines for text-based upcalls.
924 * Fields are separated by spaces.
925 * Fields are either mangled to quote space tab newline slosh with slosh
926 * or a hexified with a leading \x
927 * Record is terminated with newline.
931 void qword_add(char **bpp
, int *lp
, char *str
)
939 while ((c
=*str
++) && len
)
947 *bp
++ = '0' + ((c
& 0300)>>6);
948 *bp
++ = '0' + ((c
& 0070)>>3);
949 *bp
++ = '0' + ((c
& 0007)>>0);
957 if (c
|| len
<1) len
= -1;
966 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
977 while (blen
&& len
>= 2) {
978 unsigned char c
= *buf
++;
979 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
980 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
985 if (blen
|| len
<1) len
= -1;
994 static void warn_no_listener(struct cache_detail
*detail
)
996 if (detail
->last_warn
!= detail
->last_close
) {
997 detail
->last_warn
= detail
->last_close
;
998 if (detail
->warn_no_listener
)
999 detail
->warn_no_listener(detail
);
1004 * register an upcall request to user-space.
1005 * Each request is at most one page long.
1007 static int cache_make_upcall(struct cache_detail
*detail
, struct cache_head
*h
)
1011 struct cache_request
*crq
;
1015 if (detail
->cache_request
== NULL
)
1018 if (atomic_read(&detail
->readers
) == 0 &&
1019 detail
->last_close
< get_seconds() - 30) {
1020 warn_no_listener(detail
);
1024 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1028 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1034 bp
= buf
; len
= PAGE_SIZE
;
1036 detail
->cache_request(detail
, h
, &bp
, &len
);
1044 crq
->item
= cache_get(h
);
1046 crq
->len
= PAGE_SIZE
- len
;
1048 spin_lock(&queue_lock
);
1049 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1050 spin_unlock(&queue_lock
);
1051 wake_up(&queue_wait
);
1056 * parse a message from user-space and pass it
1057 * to an appropriate cache
1058 * Messages are, like requests, separated into fields by
1059 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1062 * reply cachename expiry key ... content....
1064 * key and content are both parsed by cache
1067 #define isodigit(c) (isdigit(c) && c <= '7')
1068 int qword_get(char **bpp
, char *dest
, int bufsize
)
1070 /* return bytes copied, or -1 on error */
1074 while (*bp
== ' ') bp
++;
1076 if (bp
[0] == '\\' && bp
[1] == 'x') {
1079 while (isxdigit(bp
[0]) && isxdigit(bp
[1]) && len
< bufsize
) {
1080 int byte
= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1083 byte
|= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1089 /* text with \nnn octal quoting */
1090 while (*bp
!= ' ' && *bp
!= '\n' && *bp
&& len
< bufsize
-1) {
1092 isodigit(bp
[1]) && (bp
[1] <= '3') &&
1095 int byte
= (*++bp
-'0');
1097 byte
= (byte
<< 3) | (*bp
++ - '0');
1098 byte
= (byte
<< 3) | (*bp
++ - '0');
1108 if (*bp
!= ' ' && *bp
!= '\n' && *bp
!= '\0')
1110 while (*bp
== ' ') bp
++;
1118 * support /proc/sunrpc/cache/$CACHENAME/content
1120 * We call ->cache_show passing NULL for the item to
1121 * get a header, then pass each real item in the cache
1125 struct cache_detail
*cd
;
1128 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1131 unsigned hash
, entry
;
1132 struct cache_head
*ch
;
1133 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1136 read_lock(&cd
->hash_lock
);
1138 return SEQ_START_TOKEN
;
1140 entry
= n
& ((1LL<<32) - 1);
1142 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1145 n
&= ~((1LL<<32) - 1);
1149 } while(hash
< cd
->hash_size
&&
1150 cd
->hash_table
[hash
]==NULL
);
1151 if (hash
>= cd
->hash_size
)
1154 return cd
->hash_table
[hash
];
1157 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1159 struct cache_head
*ch
= p
;
1160 int hash
= (*pos
>> 32);
1161 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1163 if (p
== SEQ_START_TOKEN
)
1165 else if (ch
->next
== NULL
) {
1172 *pos
&= ~((1LL<<32) - 1);
1173 while (hash
< cd
->hash_size
&&
1174 cd
->hash_table
[hash
] == NULL
) {
1178 if (hash
>= cd
->hash_size
)
1181 return cd
->hash_table
[hash
];
1184 static void c_stop(struct seq_file
*m
, void *p
)
1186 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1187 read_unlock(&cd
->hash_lock
);
1190 static int c_show(struct seq_file
*m
, void *p
)
1192 struct cache_head
*cp
= p
;
1193 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1195 if (p
== SEQ_START_TOKEN
)
1196 return cd
->cache_show(m
, cd
, NULL
);
1199 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1200 cp
->expiry_time
, atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1202 if (cache_check(cd
, cp
, NULL
))
1203 /* cache_check does a cache_put on failure */
1204 seq_printf(m
, "# ");
1208 return cd
->cache_show(m
, cd
, cp
);
1211 static struct seq_operations cache_content_op
= {
1218 static int content_open(struct inode
*inode
, struct file
*file
)
1222 struct cache_detail
*cd
= PDE(inode
)->data
;
1224 han
= kmalloc(sizeof(*han
), GFP_KERNEL
);
1230 res
= seq_open(file
, &cache_content_op
);
1234 ((struct seq_file
*)file
->private_data
)->private = han
;
1238 static int content_release(struct inode
*inode
, struct file
*file
)
1240 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1241 struct handle
*han
= m
->private;
1244 return seq_release(inode
, file
);
1247 static struct file_operations content_file_operations
= {
1248 .open
= content_open
,
1250 .llseek
= seq_lseek
,
1251 .release
= content_release
,
1254 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1255 size_t count
, loff_t
*ppos
)
1257 struct cache_detail
*cd
= PDE(file
->f_dentry
->d_inode
)->data
;
1259 unsigned long p
= *ppos
;
1262 sprintf(tbuf
, "%lu\n", cd
->flush_time
);
1267 if (len
> count
) len
= count
;
1268 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
)
1278 struct cache_detail
*cd
= PDE(file
->f_dentry
->d_inode
)->data
;
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 struct file_operations cache_flush_operations
= {
1300 .open
= nonseekable_open
,
1302 .write
= write_flush
,