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
);
250 * caches need to be periodically cleaned.
251 * For this we maintain a list of cache_detail and
252 * a current pointer into that list and into the table
255 * Each time clean_cache is called it finds the next non-empty entry
256 * in the current table and walks the list in that entry
257 * looking for entries that can be removed.
259 * An entry gets removed if:
260 * - The expiry is before current time
261 * - The last_refresh time is before the flush_time for that cache
263 * later we might drop old entries with non-NEVER expiry if that table
264 * is getting 'full' for some definition of 'full'
266 * The question of "how often to scan a table" is an interesting one
267 * and is answered in part by the use of the "nextcheck" field in the
269 * When a scan of a table begins, the nextcheck field is set to a time
270 * that is well into the future.
271 * While scanning, if an expiry time is found that is earlier than the
272 * current nextcheck time, nextcheck is set to that expiry time.
273 * If the flush_time is ever set to a time earlier than the nextcheck
274 * time, the nextcheck time is then set to that flush_time.
276 * A table is then only scanned if the current time is at least
277 * the nextcheck time.
281 static LIST_HEAD(cache_list
);
282 static DEFINE_SPINLOCK(cache_list_lock
);
283 static struct cache_detail
*current_detail
;
284 static int current_index
;
286 static const struct file_operations cache_file_operations
;
287 static const struct file_operations content_file_operations
;
288 static const struct file_operations cache_flush_operations
;
290 static void do_cache_clean(struct work_struct
*work
);
291 static DECLARE_DELAYED_WORK(cache_cleaner
, do_cache_clean
);
293 void cache_register(struct cache_detail
*cd
)
295 cd
->proc_ent
= proc_mkdir(cd
->name
, proc_net_rpc
);
297 struct proc_dir_entry
*p
;
298 cd
->proc_ent
->owner
= cd
->owner
;
299 cd
->channel_ent
= cd
->content_ent
= NULL
;
301 p
= create_proc_entry("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
305 p
->proc_fops
= &cache_flush_operations
;
306 p
->owner
= cd
->owner
;
310 if (cd
->cache_request
|| cd
->cache_parse
) {
311 p
= create_proc_entry("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
315 p
->proc_fops
= &cache_file_operations
;
316 p
->owner
= cd
->owner
;
320 if (cd
->cache_show
) {
321 p
= create_proc_entry("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
325 p
->proc_fops
= &content_file_operations
;
326 p
->owner
= cd
->owner
;
331 rwlock_init(&cd
->hash_lock
);
332 INIT_LIST_HEAD(&cd
->queue
);
333 spin_lock(&cache_list_lock
);
336 atomic_set(&cd
->readers
, 0);
339 list_add(&cd
->others
, &cache_list
);
340 spin_unlock(&cache_list_lock
);
342 /* start the cleaning process */
343 schedule_delayed_work(&cache_cleaner
, 0);
346 int cache_unregister(struct cache_detail
*cd
)
349 spin_lock(&cache_list_lock
);
350 write_lock(&cd
->hash_lock
);
351 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
352 write_unlock(&cd
->hash_lock
);
353 spin_unlock(&cache_list_lock
);
356 if (current_detail
== cd
)
357 current_detail
= NULL
;
358 list_del_init(&cd
->others
);
359 write_unlock(&cd
->hash_lock
);
360 spin_unlock(&cache_list_lock
);
363 remove_proc_entry("flush", cd
->proc_ent
);
365 remove_proc_entry("channel", cd
->proc_ent
);
367 remove_proc_entry("content", cd
->proc_ent
);
370 remove_proc_entry(cd
->name
, proc_net_rpc
);
372 if (list_empty(&cache_list
)) {
373 /* module must be being unloaded so its safe to kill the worker */
374 cancel_delayed_work(&cache_cleaner
);
375 flush_scheduled_work();
380 /* clean cache tries to find something to clean
382 * It returns 1 if it cleaned something,
383 * 0 if it didn't find anything this time
384 * -1 if it fell off the end of the list.
386 static int cache_clean(void)
389 struct list_head
*next
;
391 spin_lock(&cache_list_lock
);
393 /* find a suitable table if we don't already have one */
394 while (current_detail
== NULL
||
395 current_index
>= current_detail
->hash_size
) {
397 next
= current_detail
->others
.next
;
399 next
= cache_list
.next
;
400 if (next
== &cache_list
) {
401 current_detail
= NULL
;
402 spin_unlock(&cache_list_lock
);
405 current_detail
= list_entry(next
, struct cache_detail
, others
);
406 if (current_detail
->nextcheck
> get_seconds())
407 current_index
= current_detail
->hash_size
;
410 current_detail
->nextcheck
= get_seconds()+30*60;
414 /* find a non-empty bucket in the table */
415 while (current_detail
&&
416 current_index
< current_detail
->hash_size
&&
417 current_detail
->hash_table
[current_index
] == NULL
)
420 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
422 if (current_detail
&& current_index
< current_detail
->hash_size
) {
423 struct cache_head
*ch
, **cp
;
424 struct cache_detail
*d
;
426 write_lock(¤t_detail
->hash_lock
);
428 /* Ok, now to clean this strand */
430 cp
= & current_detail
->hash_table
[current_index
];
432 for (; ch
; cp
= & ch
->next
, ch
= *cp
) {
433 if (current_detail
->nextcheck
> ch
->expiry_time
)
434 current_detail
->nextcheck
= ch
->expiry_time
+1;
435 if (ch
->expiry_time
>= get_seconds()
436 && ch
->last_refresh
>= current_detail
->flush_time
439 if (test_and_clear_bit(CACHE_PENDING
, &ch
->flags
))
440 queue_loose(current_detail
, ch
);
442 if (atomic_read(&ch
->ref
.refcount
) == 1)
448 current_detail
->entries
--;
451 write_unlock(¤t_detail
->hash_lock
);
455 spin_unlock(&cache_list_lock
);
459 spin_unlock(&cache_list_lock
);
465 * We want to regularly clean the cache, so we need to schedule some work ...
467 static void do_cache_clean(struct work_struct
*work
)
470 if (cache_clean() == -1)
473 if (list_empty(&cache_list
))
477 schedule_delayed_work(&cache_cleaner
, delay
);
482 * Clean all caches promptly. This just calls cache_clean
483 * repeatedly until we are sure that every cache has had a chance to
486 void cache_flush(void)
488 while (cache_clean() != -1)
490 while (cache_clean() != -1)
494 void cache_purge(struct cache_detail
*detail
)
496 detail
->flush_time
= LONG_MAX
;
497 detail
->nextcheck
= get_seconds();
499 detail
->flush_time
= 1;
505 * Deferral and Revisiting of Requests.
507 * If a cache lookup finds a pending entry, we
508 * need to defer the request and revisit it later.
509 * All deferred requests are stored in a hash table,
510 * indexed by "struct cache_head *".
511 * As it may be wasteful to store a whole request
512 * structure, we allow the request to provide a
513 * deferred form, which must contain a
514 * 'struct cache_deferred_req'
515 * This cache_deferred_req contains a method to allow
516 * it to be revisited when cache info is available
519 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
520 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
522 #define DFR_MAX 300 /* ??? */
524 static DEFINE_SPINLOCK(cache_defer_lock
);
525 static LIST_HEAD(cache_defer_list
);
526 static struct list_head cache_defer_hash
[DFR_HASHSIZE
];
527 static int cache_defer_cnt
;
529 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
531 struct cache_deferred_req
*dreq
;
532 int hash
= DFR_HASH(item
);
534 if (cache_defer_cnt
>= DFR_MAX
) {
535 /* too much in the cache, randomly drop this one,
536 * or continue and drop the oldest below
541 dreq
= req
->defer(req
);
546 dreq
->recv_time
= get_seconds();
548 spin_lock(&cache_defer_lock
);
550 list_add(&dreq
->recent
, &cache_defer_list
);
552 if (cache_defer_hash
[hash
].next
== NULL
)
553 INIT_LIST_HEAD(&cache_defer_hash
[hash
]);
554 list_add(&dreq
->hash
, &cache_defer_hash
[hash
]);
556 /* it is in, now maybe clean up */
558 if (++cache_defer_cnt
> DFR_MAX
) {
559 dreq
= list_entry(cache_defer_list
.prev
,
560 struct cache_deferred_req
, recent
);
561 list_del(&dreq
->recent
);
562 list_del(&dreq
->hash
);
565 spin_unlock(&cache_defer_lock
);
568 /* there was one too many */
569 dreq
->revisit(dreq
, 1);
571 if (!test_bit(CACHE_PENDING
, &item
->flags
)) {
572 /* must have just been validated... */
573 cache_revisit_request(item
);
578 static void cache_revisit_request(struct cache_head
*item
)
580 struct cache_deferred_req
*dreq
;
581 struct list_head pending
;
583 struct list_head
*lp
;
584 int hash
= DFR_HASH(item
);
586 INIT_LIST_HEAD(&pending
);
587 spin_lock(&cache_defer_lock
);
589 lp
= cache_defer_hash
[hash
].next
;
591 while (lp
!= &cache_defer_hash
[hash
]) {
592 dreq
= list_entry(lp
, struct cache_deferred_req
, hash
);
594 if (dreq
->item
== item
) {
595 list_del(&dreq
->hash
);
596 list_move(&dreq
->recent
, &pending
);
601 spin_unlock(&cache_defer_lock
);
603 while (!list_empty(&pending
)) {
604 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
605 list_del_init(&dreq
->recent
);
606 dreq
->revisit(dreq
, 0);
610 void cache_clean_deferred(void *owner
)
612 struct cache_deferred_req
*dreq
, *tmp
;
613 struct list_head pending
;
616 INIT_LIST_HEAD(&pending
);
617 spin_lock(&cache_defer_lock
);
619 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
620 if (dreq
->owner
== owner
) {
621 list_del(&dreq
->hash
);
622 list_move(&dreq
->recent
, &pending
);
626 spin_unlock(&cache_defer_lock
);
628 while (!list_empty(&pending
)) {
629 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
630 list_del_init(&dreq
->recent
);
631 dreq
->revisit(dreq
, 1);
636 * communicate with user-space
638 * We have a magic /proc file - /proc/sunrpc/cache
639 * On read, you get a full request, or block
640 * On write, an update request is processed
641 * Poll works if anything to read, and always allows write
643 * Implemented by linked list of requests. Each open file has
644 * a ->private that also exists in this list. New request are added
645 * to the end and may wakeup and preceding readers.
646 * New readers are added to the head. If, on read, an item is found with
647 * CACHE_UPCALLING clear, we free it from the list.
651 static DEFINE_SPINLOCK(queue_lock
);
652 static DEFINE_MUTEX(queue_io_mutex
);
655 struct list_head list
;
656 int reader
; /* if 0, then request */
658 struct cache_request
{
659 struct cache_queue q
;
660 struct cache_head
*item
;
665 struct cache_reader
{
666 struct cache_queue q
;
667 int offset
; /* if non-0, we have a refcnt on next request */
671 cache_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
673 struct cache_reader
*rp
= filp
->private_data
;
674 struct cache_request
*rq
;
675 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
681 mutex_lock(&queue_io_mutex
); /* protect against multiple concurrent
682 * readers on this file */
684 spin_lock(&queue_lock
);
685 /* need to find next request */
686 while (rp
->q
.list
.next
!= &cd
->queue
&&
687 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
689 struct list_head
*next
= rp
->q
.list
.next
;
690 list_move(&rp
->q
.list
, next
);
692 if (rp
->q
.list
.next
== &cd
->queue
) {
693 spin_unlock(&queue_lock
);
694 mutex_unlock(&queue_io_mutex
);
698 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
699 BUG_ON(rq
->q
.reader
);
702 spin_unlock(&queue_lock
);
704 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
706 spin_lock(&queue_lock
);
707 list_move(&rp
->q
.list
, &rq
->q
.list
);
708 spin_unlock(&queue_lock
);
710 if (rp
->offset
+ count
> rq
->len
)
711 count
= rq
->len
- rp
->offset
;
713 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
716 if (rp
->offset
>= rq
->len
) {
718 spin_lock(&queue_lock
);
719 list_move(&rp
->q
.list
, &rq
->q
.list
);
720 spin_unlock(&queue_lock
);
725 if (rp
->offset
== 0) {
726 /* need to release rq */
727 spin_lock(&queue_lock
);
729 if (rq
->readers
== 0 &&
730 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
731 list_del(&rq
->q
.list
);
732 spin_unlock(&queue_lock
);
733 cache_put(rq
->item
, cd
);
737 spin_unlock(&queue_lock
);
741 mutex_unlock(&queue_io_mutex
);
742 return err
? err
: count
;
745 static char write_buf
[8192]; /* protected by queue_io_mutex */
748 cache_write(struct file
*filp
, const char __user
*buf
, size_t count
,
752 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
756 if (count
>= sizeof(write_buf
))
759 mutex_lock(&queue_io_mutex
);
761 if (copy_from_user(write_buf
, buf
, count
)) {
762 mutex_unlock(&queue_io_mutex
);
765 write_buf
[count
] = '\0';
767 err
= cd
->cache_parse(cd
, write_buf
, count
);
771 mutex_unlock(&queue_io_mutex
);
772 return err
? err
: count
;
775 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
778 cache_poll(struct file
*filp
, poll_table
*wait
)
781 struct cache_reader
*rp
= filp
->private_data
;
782 struct cache_queue
*cq
;
783 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
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
);
806 cache_ioctl(struct inode
*ino
, struct file
*filp
,
807 unsigned int cmd
, unsigned long arg
)
810 struct cache_reader
*rp
= filp
->private_data
;
811 struct cache_queue
*cq
;
812 struct cache_detail
*cd
= PDE(ino
)->data
;
814 if (cmd
!= FIONREAD
|| !rp
)
817 spin_lock(&queue_lock
);
819 /* only find the length remaining in current request,
820 * or the length of the next request
822 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
823 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
825 struct cache_request
*cr
=
826 container_of(cq
, struct cache_request
, q
);
827 len
= cr
->len
- rp
->offset
;
830 spin_unlock(&queue_lock
);
832 return put_user(len
, (int __user
*)arg
);
836 cache_open(struct inode
*inode
, struct file
*filp
)
838 struct cache_reader
*rp
= NULL
;
840 nonseekable_open(inode
, filp
);
841 if (filp
->f_mode
& FMODE_READ
) {
842 struct cache_detail
*cd
= PDE(inode
)->data
;
844 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
849 atomic_inc(&cd
->readers
);
850 spin_lock(&queue_lock
);
851 list_add(&rp
->q
.list
, &cd
->queue
);
852 spin_unlock(&queue_lock
);
854 filp
->private_data
= rp
;
859 cache_release(struct inode
*inode
, struct file
*filp
)
861 struct cache_reader
*rp
= filp
->private_data
;
862 struct cache_detail
*cd
= PDE(inode
)->data
;
865 spin_lock(&queue_lock
);
867 struct cache_queue
*cq
;
868 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
869 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
871 container_of(cq
, struct cache_request
, q
)
877 list_del(&rp
->q
.list
);
878 spin_unlock(&queue_lock
);
880 filp
->private_data
= NULL
;
883 cd
->last_close
= get_seconds();
884 atomic_dec(&cd
->readers
);
891 static const struct file_operations cache_file_operations
= {
892 .owner
= THIS_MODULE
,
895 .write
= cache_write
,
897 .ioctl
= cache_ioctl
, /* for FIONREAD */
899 .release
= cache_release
,
903 static void queue_loose(struct cache_detail
*detail
, struct cache_head
*ch
)
905 struct cache_queue
*cq
;
906 spin_lock(&queue_lock
);
907 list_for_each_entry(cq
, &detail
->queue
, list
)
909 struct cache_request
*cr
= container_of(cq
, struct cache_request
, q
);
912 if (cr
->readers
!= 0)
914 list_del(&cr
->q
.list
);
915 spin_unlock(&queue_lock
);
916 cache_put(cr
->item
, detail
);
921 spin_unlock(&queue_lock
);
925 * Support routines for text-based upcalls.
926 * Fields are separated by spaces.
927 * Fields are either mangled to quote space tab newline slosh with slosh
928 * or a hexified with a leading \x
929 * Record is terminated with newline.
933 void qword_add(char **bpp
, int *lp
, char *str
)
941 while ((c
=*str
++) && len
)
949 *bp
++ = '0' + ((c
& 0300)>>6);
950 *bp
++ = '0' + ((c
& 0070)>>3);
951 *bp
++ = '0' + ((c
& 0007)>>0);
959 if (c
|| len
<1) len
= -1;
968 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
979 while (blen
&& len
>= 2) {
980 unsigned char c
= *buf
++;
981 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
982 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
987 if (blen
|| len
<1) len
= -1;
996 static void warn_no_listener(struct cache_detail
*detail
)
998 if (detail
->last_warn
!= detail
->last_close
) {
999 detail
->last_warn
= detail
->last_close
;
1000 if (detail
->warn_no_listener
)
1001 detail
->warn_no_listener(detail
);
1006 * register an upcall request to user-space.
1007 * Each request is at most one page long.
1009 static int cache_make_upcall(struct cache_detail
*detail
, struct cache_head
*h
)
1013 struct cache_request
*crq
;
1017 if (detail
->cache_request
== NULL
)
1020 if (atomic_read(&detail
->readers
) == 0 &&
1021 detail
->last_close
< get_seconds() - 30) {
1022 warn_no_listener(detail
);
1026 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1030 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1036 bp
= buf
; len
= PAGE_SIZE
;
1038 detail
->cache_request(detail
, h
, &bp
, &len
);
1046 crq
->item
= cache_get(h
);
1048 crq
->len
= PAGE_SIZE
- len
;
1050 spin_lock(&queue_lock
);
1051 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1052 spin_unlock(&queue_lock
);
1053 wake_up(&queue_wait
);
1058 * parse a message from user-space and pass it
1059 * to an appropriate cache
1060 * Messages are, like requests, separated into fields by
1061 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1064 * reply cachename expiry key ... content....
1066 * key and content are both parsed by cache
1069 #define isodigit(c) (isdigit(c) && c <= '7')
1070 int qword_get(char **bpp
, char *dest
, int bufsize
)
1072 /* return bytes copied, or -1 on error */
1076 while (*bp
== ' ') bp
++;
1078 if (bp
[0] == '\\' && bp
[1] == 'x') {
1081 while (isxdigit(bp
[0]) && isxdigit(bp
[1]) && len
< bufsize
) {
1082 int byte
= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1085 byte
|= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1091 /* text with \nnn octal quoting */
1092 while (*bp
!= ' ' && *bp
!= '\n' && *bp
&& len
< bufsize
-1) {
1094 isodigit(bp
[1]) && (bp
[1] <= '3') &&
1097 int byte
= (*++bp
-'0');
1099 byte
= (byte
<< 3) | (*bp
++ - '0');
1100 byte
= (byte
<< 3) | (*bp
++ - '0');
1110 if (*bp
!= ' ' && *bp
!= '\n' && *bp
!= '\0')
1112 while (*bp
== ' ') bp
++;
1120 * support /proc/sunrpc/cache/$CACHENAME/content
1122 * We call ->cache_show passing NULL for the item to
1123 * get a header, then pass each real item in the cache
1127 struct cache_detail
*cd
;
1130 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1133 unsigned hash
, entry
;
1134 struct cache_head
*ch
;
1135 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1138 read_lock(&cd
->hash_lock
);
1140 return SEQ_START_TOKEN
;
1142 entry
= n
& ((1LL<<32) - 1);
1144 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1147 n
&= ~((1LL<<32) - 1);
1151 } while(hash
< cd
->hash_size
&&
1152 cd
->hash_table
[hash
]==NULL
);
1153 if (hash
>= cd
->hash_size
)
1156 return cd
->hash_table
[hash
];
1159 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1161 struct cache_head
*ch
= p
;
1162 int hash
= (*pos
>> 32);
1163 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1165 if (p
== SEQ_START_TOKEN
)
1167 else if (ch
->next
== NULL
) {
1174 *pos
&= ~((1LL<<32) - 1);
1175 while (hash
< cd
->hash_size
&&
1176 cd
->hash_table
[hash
] == NULL
) {
1180 if (hash
>= cd
->hash_size
)
1183 return cd
->hash_table
[hash
];
1186 static void c_stop(struct seq_file
*m
, void *p
)
1188 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1189 read_unlock(&cd
->hash_lock
);
1192 static int c_show(struct seq_file
*m
, void *p
)
1194 struct cache_head
*cp
= p
;
1195 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1197 if (p
== SEQ_START_TOKEN
)
1198 return cd
->cache_show(m
, cd
, NULL
);
1201 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1202 cp
->expiry_time
, atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1204 if (cache_check(cd
, cp
, NULL
))
1205 /* cache_check does a cache_put on failure */
1206 seq_printf(m
, "# ");
1210 return cd
->cache_show(m
, cd
, cp
);
1213 static struct seq_operations cache_content_op
= {
1220 static int content_open(struct inode
*inode
, struct file
*file
)
1224 struct cache_detail
*cd
= PDE(inode
)->data
;
1226 han
= kmalloc(sizeof(*han
), GFP_KERNEL
);
1232 res
= seq_open(file
, &cache_content_op
);
1236 ((struct seq_file
*)file
->private_data
)->private = han
;
1240 static int content_release(struct inode
*inode
, struct file
*file
)
1242 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1243 struct handle
*han
= m
->private;
1246 return seq_release(inode
, file
);
1249 static const struct file_operations content_file_operations
= {
1250 .open
= content_open
,
1252 .llseek
= seq_lseek
,
1253 .release
= content_release
,
1256 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1257 size_t count
, loff_t
*ppos
)
1259 struct cache_detail
*cd
= PDE(file
->f_path
.dentry
->d_inode
)->data
;
1261 unsigned long p
= *ppos
;
1264 sprintf(tbuf
, "%lu\n", cd
->flush_time
);
1269 if (len
> count
) len
= count
;
1270 if (copy_to_user(buf
, (void*)(tbuf
+p
), len
))
1277 static ssize_t
write_flush(struct file
* file
, const char __user
* buf
,
1278 size_t count
, loff_t
*ppos
)
1280 struct cache_detail
*cd
= PDE(file
->f_path
.dentry
->d_inode
)->data
;
1284 if (*ppos
|| count
> sizeof(tbuf
)-1)
1286 if (copy_from_user(tbuf
, buf
, count
))
1289 flushtime
= simple_strtoul(tbuf
, &ep
, 0);
1290 if (*ep
&& *ep
!= '\n')
1293 cd
->flush_time
= flushtime
;
1294 cd
->nextcheck
= get_seconds();
1301 static const struct file_operations cache_flush_operations
= {
1302 .open
= nonseekable_open
,
1304 .write
= write_flush
,