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();
76 write_lock(&detail
->hash_lock
);
78 /* check if entry appeared while we slept */
79 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
80 struct cache_head
*tmp
= *hp
;
81 if (detail
->match(tmp
, key
)) {
83 write_unlock(&detail
->hash_lock
);
84 cache_put(new, detail
);
88 detail
->init(new, key
);
93 write_unlock(&detail
->hash_lock
);
97 EXPORT_SYMBOL(sunrpc_cache_lookup
);
100 static void queue_loose(struct cache_detail
*detail
, struct cache_head
*ch
);
102 static int cache_fresh_locked(struct cache_head
*head
, time_t expiry
)
104 head
->expiry_time
= expiry
;
105 head
->last_refresh
= get_seconds();
106 return !test_and_set_bit(CACHE_VALID
, &head
->flags
);
109 static void cache_fresh_unlocked(struct cache_head
*head
,
110 struct cache_detail
*detail
, int new)
113 cache_revisit_request(head
);
114 if (test_and_clear_bit(CACHE_PENDING
, &head
->flags
)) {
115 cache_revisit_request(head
);
116 queue_loose(detail
, head
);
120 struct cache_head
*sunrpc_cache_update(struct cache_detail
*detail
,
121 struct cache_head
*new, struct cache_head
*old
, int hash
)
123 /* The 'old' entry is to be replaced by 'new'.
124 * If 'old' is not VALID, we update it directly,
125 * otherwise we need to replace it
127 struct cache_head
**head
;
128 struct cache_head
*tmp
;
131 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
132 write_lock(&detail
->hash_lock
);
133 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
134 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
135 set_bit(CACHE_NEGATIVE
, &old
->flags
);
137 detail
->update(old
, new);
138 is_new
= cache_fresh_locked(old
, new->expiry_time
);
139 write_unlock(&detail
->hash_lock
);
140 cache_fresh_unlocked(old
, detail
, is_new
);
143 write_unlock(&detail
->hash_lock
);
145 /* We need to insert a new entry */
146 tmp
= detail
->alloc();
148 cache_put(old
, detail
);
152 detail
->init(tmp
, old
);
153 head
= &detail
->hash_table
[hash
];
155 write_lock(&detail
->hash_lock
);
156 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
157 set_bit(CACHE_NEGATIVE
, &tmp
->flags
);
159 detail
->update(tmp
, new);
163 is_new
= cache_fresh_locked(tmp
, new->expiry_time
);
164 cache_fresh_locked(old
, 0);
165 write_unlock(&detail
->hash_lock
);
166 cache_fresh_unlocked(tmp
, detail
, is_new
);
167 cache_fresh_unlocked(old
, detail
, 0);
168 cache_put(old
, detail
);
171 EXPORT_SYMBOL(sunrpc_cache_update
);
173 static int cache_make_upcall(struct cache_detail
*detail
, struct cache_head
*h
);
175 * This is the generic cache management routine for all
176 * the authentication caches.
177 * It checks the currency of a cache item and will (later)
178 * initiate an upcall to fill it if needed.
181 * Returns 0 if the cache_head can be used, or cache_puts it and returns
182 * -EAGAIN if upcall is pending,
183 * -ENOENT if cache entry was negative
185 int cache_check(struct cache_detail
*detail
,
186 struct cache_head
*h
, struct cache_req
*rqstp
)
189 long refresh_age
, age
;
191 /* First decide return status as best we can */
192 if (!test_bit(CACHE_VALID
, &h
->flags
) ||
193 h
->expiry_time
< get_seconds())
195 else if (detail
->flush_time
> h
->last_refresh
)
199 if (test_bit(CACHE_NEGATIVE
, &h
->flags
))
204 /* now see if we want to start an upcall */
205 refresh_age
= (h
->expiry_time
- h
->last_refresh
);
206 age
= get_seconds() - h
->last_refresh
;
211 } else if (rv
== -EAGAIN
|| age
> refresh_age
/2) {
212 dprintk("Want update, refage=%ld, age=%ld\n", refresh_age
, age
);
213 if (!test_and_set_bit(CACHE_PENDING
, &h
->flags
)) {
214 switch (cache_make_upcall(detail
, h
)) {
216 clear_bit(CACHE_PENDING
, &h
->flags
);
218 set_bit(CACHE_NEGATIVE
, &h
->flags
);
219 cache_fresh_unlocked(h
, detail
,
220 cache_fresh_locked(h
, get_seconds()+CACHE_NEW_EXPIRY
));
226 clear_bit(CACHE_PENDING
, &h
->flags
);
227 cache_revisit_request(h
);
234 cache_defer_req(rqstp
, h
);
237 cache_put(h
, detail
);
242 * caches need to be periodically cleaned.
243 * For this we maintain a list of cache_detail and
244 * a current pointer into that list and into the table
247 * Each time clean_cache is called it finds the next non-empty entry
248 * in the current table and walks the list in that entry
249 * looking for entries that can be removed.
251 * An entry gets removed if:
252 * - The expiry is before current time
253 * - The last_refresh time is before the flush_time for that cache
255 * later we might drop old entries with non-NEVER expiry if that table
256 * is getting 'full' for some definition of 'full'
258 * The question of "how often to scan a table" is an interesting one
259 * and is answered in part by the use of the "nextcheck" field in the
261 * When a scan of a table begins, the nextcheck field is set to a time
262 * that is well into the future.
263 * While scanning, if an expiry time is found that is earlier than the
264 * current nextcheck time, nextcheck is set to that expiry time.
265 * If the flush_time is ever set to a time earlier than the nextcheck
266 * time, the nextcheck time is then set to that flush_time.
268 * A table is then only scanned if the current time is at least
269 * the nextcheck time.
273 static LIST_HEAD(cache_list
);
274 static DEFINE_SPINLOCK(cache_list_lock
);
275 static struct cache_detail
*current_detail
;
276 static int current_index
;
278 static struct file_operations cache_file_operations
;
279 static struct file_operations content_file_operations
;
280 static struct file_operations cache_flush_operations
;
282 static void do_cache_clean(void *data
);
283 static DECLARE_WORK(cache_cleaner
, do_cache_clean
, NULL
);
285 void cache_register(struct cache_detail
*cd
)
287 cd
->proc_ent
= proc_mkdir(cd
->name
, proc_net_rpc
);
289 struct proc_dir_entry
*p
;
290 cd
->proc_ent
->owner
= cd
->owner
;
291 cd
->channel_ent
= cd
->content_ent
= NULL
;
293 p
= create_proc_entry("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
297 p
->proc_fops
= &cache_flush_operations
;
298 p
->owner
= cd
->owner
;
302 if (cd
->cache_request
|| cd
->cache_parse
) {
303 p
= create_proc_entry("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
307 p
->proc_fops
= &cache_file_operations
;
308 p
->owner
= cd
->owner
;
312 if (cd
->cache_show
) {
313 p
= create_proc_entry("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
317 p
->proc_fops
= &content_file_operations
;
318 p
->owner
= cd
->owner
;
323 rwlock_init(&cd
->hash_lock
);
324 INIT_LIST_HEAD(&cd
->queue
);
325 spin_lock(&cache_list_lock
);
328 atomic_set(&cd
->readers
, 0);
331 list_add(&cd
->others
, &cache_list
);
332 spin_unlock(&cache_list_lock
);
334 /* start the cleaning process */
335 schedule_work(&cache_cleaner
);
338 int cache_unregister(struct cache_detail
*cd
)
341 spin_lock(&cache_list_lock
);
342 write_lock(&cd
->hash_lock
);
343 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
344 write_unlock(&cd
->hash_lock
);
345 spin_unlock(&cache_list_lock
);
348 if (current_detail
== cd
)
349 current_detail
= NULL
;
350 list_del_init(&cd
->others
);
351 write_unlock(&cd
->hash_lock
);
352 spin_unlock(&cache_list_lock
);
355 remove_proc_entry("flush", cd
->proc_ent
);
357 remove_proc_entry("channel", cd
->proc_ent
);
359 remove_proc_entry("content", cd
->proc_ent
);
362 remove_proc_entry(cd
->name
, proc_net_rpc
);
364 if (list_empty(&cache_list
)) {
365 /* module must be being unloaded so its safe to kill the worker */
366 cancel_delayed_work(&cache_cleaner
);
367 flush_scheduled_work();
372 /* clean cache tries to find something to clean
374 * It returns 1 if it cleaned something,
375 * 0 if it didn't find anything this time
376 * -1 if it fell off the end of the list.
378 static int cache_clean(void)
381 struct list_head
*next
;
383 spin_lock(&cache_list_lock
);
385 /* find a suitable table if we don't already have one */
386 while (current_detail
== NULL
||
387 current_index
>= current_detail
->hash_size
) {
389 next
= current_detail
->others
.next
;
391 next
= cache_list
.next
;
392 if (next
== &cache_list
) {
393 current_detail
= NULL
;
394 spin_unlock(&cache_list_lock
);
397 current_detail
= list_entry(next
, struct cache_detail
, others
);
398 if (current_detail
->nextcheck
> get_seconds())
399 current_index
= current_detail
->hash_size
;
402 current_detail
->nextcheck
= get_seconds()+30*60;
406 /* find a non-empty bucket in the table */
407 while (current_detail
&&
408 current_index
< current_detail
->hash_size
&&
409 current_detail
->hash_table
[current_index
] == NULL
)
412 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
414 if (current_detail
&& current_index
< current_detail
->hash_size
) {
415 struct cache_head
*ch
, **cp
;
416 struct cache_detail
*d
;
418 write_lock(¤t_detail
->hash_lock
);
420 /* Ok, now to clean this strand */
422 cp
= & current_detail
->hash_table
[current_index
];
424 for (; ch
; cp
= & ch
->next
, ch
= *cp
) {
425 if (current_detail
->nextcheck
> ch
->expiry_time
)
426 current_detail
->nextcheck
= ch
->expiry_time
+1;
427 if (ch
->expiry_time
>= get_seconds()
428 && ch
->last_refresh
>= current_detail
->flush_time
431 if (test_and_clear_bit(CACHE_PENDING
, &ch
->flags
))
432 queue_loose(current_detail
, ch
);
434 if (atomic_read(&ch
->ref
.refcount
) == 1)
440 current_detail
->entries
--;
443 write_unlock(¤t_detail
->hash_lock
);
447 spin_unlock(&cache_list_lock
);
451 spin_unlock(&cache_list_lock
);
457 * We want to regularly clean the cache, so we need to schedule some work ...
459 static void do_cache_clean(void *data
)
462 if (cache_clean() == -1)
465 if (list_empty(&cache_list
))
469 schedule_delayed_work(&cache_cleaner
, delay
);
474 * Clean all caches promptly. This just calls cache_clean
475 * repeatedly until we are sure that every cache has had a chance to
478 void cache_flush(void)
480 while (cache_clean() != -1)
482 while (cache_clean() != -1)
486 void cache_purge(struct cache_detail
*detail
)
488 detail
->flush_time
= LONG_MAX
;
489 detail
->nextcheck
= get_seconds();
491 detail
->flush_time
= 1;
497 * Deferral and Revisiting of Requests.
499 * If a cache lookup finds a pending entry, we
500 * need to defer the request and revisit it later.
501 * All deferred requests are stored in a hash table,
502 * indexed by "struct cache_head *".
503 * As it may be wasteful to store a whole request
504 * structure, we allow the request to provide a
505 * deferred form, which must contain a
506 * 'struct cache_deferred_req'
507 * This cache_deferred_req contains a method to allow
508 * it to be revisited when cache info is available
511 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
512 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
514 #define DFR_MAX 300 /* ??? */
516 static DEFINE_SPINLOCK(cache_defer_lock
);
517 static LIST_HEAD(cache_defer_list
);
518 static struct list_head cache_defer_hash
[DFR_HASHSIZE
];
519 static int cache_defer_cnt
;
521 static void cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
523 struct cache_deferred_req
*dreq
;
524 int hash
= DFR_HASH(item
);
526 dreq
= req
->defer(req
);
531 dreq
->recv_time
= get_seconds();
533 spin_lock(&cache_defer_lock
);
535 list_add(&dreq
->recent
, &cache_defer_list
);
537 if (cache_defer_hash
[hash
].next
== NULL
)
538 INIT_LIST_HEAD(&cache_defer_hash
[hash
]);
539 list_add(&dreq
->hash
, &cache_defer_hash
[hash
]);
541 /* it is in, now maybe clean up */
543 if (++cache_defer_cnt
> DFR_MAX
) {
544 /* too much in the cache, randomly drop
548 dreq
= list_entry(cache_defer_list
.next
,
549 struct cache_deferred_req
,
552 dreq
= list_entry(cache_defer_list
.prev
,
553 struct cache_deferred_req
,
555 list_del(&dreq
->recent
);
556 list_del(&dreq
->hash
);
559 spin_unlock(&cache_defer_lock
);
562 /* there was one too many */
563 dreq
->revisit(dreq
, 1);
565 if (!test_bit(CACHE_PENDING
, &item
->flags
)) {
566 /* must have just been validated... */
567 cache_revisit_request(item
);
571 static void cache_revisit_request(struct cache_head
*item
)
573 struct cache_deferred_req
*dreq
;
574 struct list_head pending
;
576 struct list_head
*lp
;
577 int hash
= DFR_HASH(item
);
579 INIT_LIST_HEAD(&pending
);
580 spin_lock(&cache_defer_lock
);
582 lp
= cache_defer_hash
[hash
].next
;
584 while (lp
!= &cache_defer_hash
[hash
]) {
585 dreq
= list_entry(lp
, struct cache_deferred_req
, hash
);
587 if (dreq
->item
== item
) {
588 list_del(&dreq
->hash
);
589 list_move(&dreq
->recent
, &pending
);
594 spin_unlock(&cache_defer_lock
);
596 while (!list_empty(&pending
)) {
597 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
598 list_del_init(&dreq
->recent
);
599 dreq
->revisit(dreq
, 0);
603 void cache_clean_deferred(void *owner
)
605 struct cache_deferred_req
*dreq
, *tmp
;
606 struct list_head pending
;
609 INIT_LIST_HEAD(&pending
);
610 spin_lock(&cache_defer_lock
);
612 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
613 if (dreq
->owner
== owner
) {
614 list_del(&dreq
->hash
);
615 list_move(&dreq
->recent
, &pending
);
619 spin_unlock(&cache_defer_lock
);
621 while (!list_empty(&pending
)) {
622 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
623 list_del_init(&dreq
->recent
);
624 dreq
->revisit(dreq
, 1);
629 * communicate with user-space
631 * We have a magic /proc file - /proc/sunrpc/cache
632 * On read, you get a full request, or block
633 * On write, an update request is processed
634 * Poll works if anything to read, and always allows write
636 * Implemented by linked list of requests. Each open file has
637 * a ->private that also exists in this list. New request are added
638 * to the end and may wakeup and preceding readers.
639 * New readers are added to the head. If, on read, an item is found with
640 * CACHE_UPCALLING clear, we free it from the list.
644 static DEFINE_SPINLOCK(queue_lock
);
645 static DEFINE_MUTEX(queue_io_mutex
);
648 struct list_head list
;
649 int reader
; /* if 0, then request */
651 struct cache_request
{
652 struct cache_queue q
;
653 struct cache_head
*item
;
658 struct cache_reader
{
659 struct cache_queue q
;
660 int offset
; /* if non-0, we have a refcnt on next request */
664 cache_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
666 struct cache_reader
*rp
= filp
->private_data
;
667 struct cache_request
*rq
;
668 struct cache_detail
*cd
= PDE(filp
->f_dentry
->d_inode
)->data
;
674 mutex_lock(&queue_io_mutex
); /* protect against multiple concurrent
675 * readers on this file */
677 spin_lock(&queue_lock
);
678 /* need to find next request */
679 while (rp
->q
.list
.next
!= &cd
->queue
&&
680 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
682 struct list_head
*next
= rp
->q
.list
.next
;
683 list_move(&rp
->q
.list
, next
);
685 if (rp
->q
.list
.next
== &cd
->queue
) {
686 spin_unlock(&queue_lock
);
687 mutex_unlock(&queue_io_mutex
);
691 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
692 BUG_ON(rq
->q
.reader
);
695 spin_unlock(&queue_lock
);
697 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
699 spin_lock(&queue_lock
);
700 list_move(&rp
->q
.list
, &rq
->q
.list
);
701 spin_unlock(&queue_lock
);
703 if (rp
->offset
+ count
> rq
->len
)
704 count
= rq
->len
- rp
->offset
;
706 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
709 if (rp
->offset
>= rq
->len
) {
711 spin_lock(&queue_lock
);
712 list_move(&rp
->q
.list
, &rq
->q
.list
);
713 spin_unlock(&queue_lock
);
718 if (rp
->offset
== 0) {
719 /* need to release rq */
720 spin_lock(&queue_lock
);
722 if (rq
->readers
== 0 &&
723 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
724 list_del(&rq
->q
.list
);
725 spin_unlock(&queue_lock
);
726 cache_put(rq
->item
, cd
);
730 spin_unlock(&queue_lock
);
734 mutex_unlock(&queue_io_mutex
);
735 return err
? err
: count
;
738 static char write_buf
[8192]; /* protected by queue_io_mutex */
741 cache_write(struct file
*filp
, const char __user
*buf
, size_t count
,
745 struct cache_detail
*cd
= PDE(filp
->f_dentry
->d_inode
)->data
;
749 if (count
>= sizeof(write_buf
))
752 mutex_lock(&queue_io_mutex
);
754 if (copy_from_user(write_buf
, buf
, count
)) {
755 mutex_unlock(&queue_io_mutex
);
758 write_buf
[count
] = '\0';
760 err
= cd
->cache_parse(cd
, write_buf
, count
);
764 mutex_unlock(&queue_io_mutex
);
765 return err
? err
: count
;
768 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
771 cache_poll(struct file
*filp
, poll_table
*wait
)
774 struct cache_reader
*rp
= filp
->private_data
;
775 struct cache_queue
*cq
;
776 struct cache_detail
*cd
= PDE(filp
->f_dentry
->d_inode
)->data
;
778 poll_wait(filp
, &queue_wait
, wait
);
780 /* alway allow write */
781 mask
= POLL_OUT
| POLLWRNORM
;
786 spin_lock(&queue_lock
);
788 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
789 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
791 mask
|= POLLIN
| POLLRDNORM
;
794 spin_unlock(&queue_lock
);
799 cache_ioctl(struct inode
*ino
, struct file
*filp
,
800 unsigned int cmd
, unsigned long arg
)
803 struct cache_reader
*rp
= filp
->private_data
;
804 struct cache_queue
*cq
;
805 struct cache_detail
*cd
= PDE(ino
)->data
;
807 if (cmd
!= FIONREAD
|| !rp
)
810 spin_lock(&queue_lock
);
812 /* only find the length remaining in current request,
813 * or the length of the next request
815 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
816 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
818 struct cache_request
*cr
=
819 container_of(cq
, struct cache_request
, q
);
820 len
= cr
->len
- rp
->offset
;
823 spin_unlock(&queue_lock
);
825 return put_user(len
, (int __user
*)arg
);
829 cache_open(struct inode
*inode
, struct file
*filp
)
831 struct cache_reader
*rp
= NULL
;
833 nonseekable_open(inode
, filp
);
834 if (filp
->f_mode
& FMODE_READ
) {
835 struct cache_detail
*cd
= PDE(inode
)->data
;
837 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
842 atomic_inc(&cd
->readers
);
843 spin_lock(&queue_lock
);
844 list_add(&rp
->q
.list
, &cd
->queue
);
845 spin_unlock(&queue_lock
);
847 filp
->private_data
= rp
;
852 cache_release(struct inode
*inode
, struct file
*filp
)
854 struct cache_reader
*rp
= filp
->private_data
;
855 struct cache_detail
*cd
= PDE(inode
)->data
;
858 spin_lock(&queue_lock
);
860 struct cache_queue
*cq
;
861 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
862 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
864 container_of(cq
, struct cache_request
, q
)
870 list_del(&rp
->q
.list
);
871 spin_unlock(&queue_lock
);
873 filp
->private_data
= NULL
;
876 cd
->last_close
= get_seconds();
877 atomic_dec(&cd
->readers
);
884 static struct file_operations cache_file_operations
= {
885 .owner
= THIS_MODULE
,
888 .write
= cache_write
,
890 .ioctl
= cache_ioctl
, /* for FIONREAD */
892 .release
= cache_release
,
896 static void queue_loose(struct cache_detail
*detail
, struct cache_head
*ch
)
898 struct cache_queue
*cq
;
899 spin_lock(&queue_lock
);
900 list_for_each_entry(cq
, &detail
->queue
, list
)
902 struct cache_request
*cr
= container_of(cq
, struct cache_request
, q
);
905 if (cr
->readers
!= 0)
907 list_del(&cr
->q
.list
);
908 spin_unlock(&queue_lock
);
909 cache_put(cr
->item
, detail
);
914 spin_unlock(&queue_lock
);
918 * Support routines for text-based upcalls.
919 * Fields are separated by spaces.
920 * Fields are either mangled to quote space tab newline slosh with slosh
921 * or a hexified with a leading \x
922 * Record is terminated with newline.
926 void qword_add(char **bpp
, int *lp
, char *str
)
934 while ((c
=*str
++) && len
)
942 *bp
++ = '0' + ((c
& 0300)>>6);
943 *bp
++ = '0' + ((c
& 0070)>>3);
944 *bp
++ = '0' + ((c
& 0007)>>0);
952 if (c
|| len
<1) len
= -1;
961 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
972 while (blen
&& len
>= 2) {
973 unsigned char c
= *buf
++;
974 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
975 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
980 if (blen
|| len
<1) len
= -1;
989 static void warn_no_listener(struct cache_detail
*detail
)
991 if (detail
->last_warn
!= detail
->last_close
) {
992 detail
->last_warn
= detail
->last_close
;
993 if (detail
->warn_no_listener
)
994 detail
->warn_no_listener(detail
);
999 * register an upcall request to user-space.
1000 * Each request is at most one page long.
1002 static int cache_make_upcall(struct cache_detail
*detail
, struct cache_head
*h
)
1006 struct cache_request
*crq
;
1010 if (detail
->cache_request
== NULL
)
1013 if (atomic_read(&detail
->readers
) == 0 &&
1014 detail
->last_close
< get_seconds() - 30) {
1015 warn_no_listener(detail
);
1019 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1023 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1029 bp
= buf
; len
= PAGE_SIZE
;
1031 detail
->cache_request(detail
, h
, &bp
, &len
);
1039 crq
->item
= cache_get(h
);
1041 crq
->len
= PAGE_SIZE
- len
;
1043 spin_lock(&queue_lock
);
1044 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1045 spin_unlock(&queue_lock
);
1046 wake_up(&queue_wait
);
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
++;
1113 * support /proc/sunrpc/cache/$CACHENAME/content
1115 * We call ->cache_show passing NULL for the item to
1116 * get a header, then pass each real item in the cache
1120 struct cache_detail
*cd
;
1123 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1126 unsigned hash
, entry
;
1127 struct cache_head
*ch
;
1128 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1131 read_lock(&cd
->hash_lock
);
1133 return SEQ_START_TOKEN
;
1135 entry
= n
& ((1LL<<32) - 1);
1137 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1140 n
&= ~((1LL<<32) - 1);
1144 } while(hash
< cd
->hash_size
&&
1145 cd
->hash_table
[hash
]==NULL
);
1146 if (hash
>= cd
->hash_size
)
1149 return cd
->hash_table
[hash
];
1152 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1154 struct cache_head
*ch
= p
;
1155 int hash
= (*pos
>> 32);
1156 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1158 if (p
== SEQ_START_TOKEN
)
1160 else if (ch
->next
== NULL
) {
1167 *pos
&= ~((1LL<<32) - 1);
1168 while (hash
< cd
->hash_size
&&
1169 cd
->hash_table
[hash
] == NULL
) {
1173 if (hash
>= cd
->hash_size
)
1176 return cd
->hash_table
[hash
];
1179 static void c_stop(struct seq_file
*m
, void *p
)
1181 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1182 read_unlock(&cd
->hash_lock
);
1185 static int c_show(struct seq_file
*m
, void *p
)
1187 struct cache_head
*cp
= p
;
1188 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1190 if (p
== SEQ_START_TOKEN
)
1191 return cd
->cache_show(m
, cd
, NULL
);
1194 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1195 cp
->expiry_time
, atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1197 if (cache_check(cd
, cp
, NULL
))
1198 /* cache_check does a cache_put on failure */
1199 seq_printf(m
, "# ");
1203 return cd
->cache_show(m
, cd
, cp
);
1206 static struct seq_operations cache_content_op
= {
1213 static int content_open(struct inode
*inode
, struct file
*file
)
1217 struct cache_detail
*cd
= PDE(inode
)->data
;
1219 han
= kmalloc(sizeof(*han
), GFP_KERNEL
);
1225 res
= seq_open(file
, &cache_content_op
);
1229 ((struct seq_file
*)file
->private_data
)->private = han
;
1233 static int content_release(struct inode
*inode
, struct file
*file
)
1235 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1236 struct handle
*han
= m
->private;
1239 return seq_release(inode
, file
);
1242 static struct file_operations content_file_operations
= {
1243 .open
= content_open
,
1245 .llseek
= seq_lseek
,
1246 .release
= content_release
,
1249 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1250 size_t count
, loff_t
*ppos
)
1252 struct cache_detail
*cd
= PDE(file
->f_dentry
->d_inode
)->data
;
1254 unsigned long p
= *ppos
;
1257 sprintf(tbuf
, "%lu\n", cd
->flush_time
);
1262 if (len
> count
) len
= count
;
1263 if (copy_to_user(buf
, (void*)(tbuf
+p
), len
))
1270 static ssize_t
write_flush(struct file
* file
, const char __user
* buf
,
1271 size_t count
, loff_t
*ppos
)
1273 struct cache_detail
*cd
= PDE(file
->f_dentry
->d_inode
)->data
;
1277 if (*ppos
|| count
> sizeof(tbuf
)-1)
1279 if (copy_from_user(tbuf
, buf
, count
))
1282 flushtime
= simple_strtoul(tbuf
, &ep
, 0);
1283 if (*ep
&& *ep
!= '\n')
1286 cd
->flush_time
= flushtime
;
1287 cd
->nextcheck
= get_seconds();
1294 static struct file_operations cache_flush_operations
= {
1295 .open
= nonseekable_open
,
1297 .write
= write_flush
,