4 * Generic code for various authentication-related caches
5 * used by sunrpc clients and servers.
7 * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
9 * Released under terms in GPL version 2. See COPYING.
13 #include <linux/types.h>
15 #include <linux/file.h>
16 #include <linux/slab.h>
17 #include <linux/signal.h>
18 #include <linux/sched.h>
19 #include <linux/kmod.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/ctype.h>
23 #include <asm/uaccess.h>
24 #include <linux/poll.h>
25 #include <linux/seq_file.h>
26 #include <linux/proc_fs.h>
27 #include <linux/net.h>
28 #include <linux/workqueue.h>
29 #include <linux/mutex.h>
30 #include <linux/pagemap.h>
31 #include <asm/ioctls.h>
32 #include <linux/sunrpc/types.h>
33 #include <linux/sunrpc/cache.h>
34 #include <linux/sunrpc/stats.h>
36 #define RPCDBG_FACILITY RPCDBG_CACHE
38 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
);
39 static void cache_revisit_request(struct cache_head
*item
);
41 static void cache_init(struct cache_head
*h
)
43 time_t now
= get_seconds();
47 h
->expiry_time
= now
+ CACHE_NEW_EXPIRY
;
48 h
->last_refresh
= now
;
51 struct cache_head
*sunrpc_cache_lookup(struct cache_detail
*detail
,
52 struct cache_head
*key
, int hash
)
54 struct cache_head
**head
, **hp
;
55 struct cache_head
*new = NULL
;
57 head
= &detail
->hash_table
[hash
];
59 read_lock(&detail
->hash_lock
);
61 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
62 struct cache_head
*tmp
= *hp
;
63 if (detail
->match(tmp
, key
)) {
65 read_unlock(&detail
->hash_lock
);
69 read_unlock(&detail
->hash_lock
);
70 /* Didn't find anything, insert an empty entry */
72 new = detail
->alloc();
75 /* must fully initialise 'new', else
76 * we might get lose if we need to
80 detail
->init(new, key
);
82 write_lock(&detail
->hash_lock
);
84 /* check if entry appeared while we slept */
85 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
86 struct cache_head
*tmp
= *hp
;
87 if (detail
->match(tmp
, key
)) {
89 write_unlock(&detail
->hash_lock
);
90 cache_put(new, detail
);
98 write_unlock(&detail
->hash_lock
);
102 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup
);
105 static void queue_loose(struct cache_detail
*detail
, struct cache_head
*ch
);
107 static int cache_fresh_locked(struct cache_head
*head
, time_t expiry
)
109 head
->expiry_time
= expiry
;
110 head
->last_refresh
= get_seconds();
111 return !test_and_set_bit(CACHE_VALID
, &head
->flags
);
114 static void cache_fresh_unlocked(struct cache_head
*head
,
115 struct cache_detail
*detail
, int new)
118 cache_revisit_request(head
);
119 if (test_and_clear_bit(CACHE_PENDING
, &head
->flags
)) {
120 cache_revisit_request(head
);
121 queue_loose(detail
, head
);
125 struct cache_head
*sunrpc_cache_update(struct cache_detail
*detail
,
126 struct cache_head
*new, struct cache_head
*old
, int hash
)
128 /* The 'old' entry is to be replaced by 'new'.
129 * If 'old' is not VALID, we update it directly,
130 * otherwise we need to replace it
132 struct cache_head
**head
;
133 struct cache_head
*tmp
;
136 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
137 write_lock(&detail
->hash_lock
);
138 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
139 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
140 set_bit(CACHE_NEGATIVE
, &old
->flags
);
142 detail
->update(old
, new);
143 is_new
= cache_fresh_locked(old
, new->expiry_time
);
144 write_unlock(&detail
->hash_lock
);
145 cache_fresh_unlocked(old
, detail
, is_new
);
148 write_unlock(&detail
->hash_lock
);
150 /* We need to insert a new entry */
151 tmp
= detail
->alloc();
153 cache_put(old
, detail
);
157 detail
->init(tmp
, old
);
158 head
= &detail
->hash_table
[hash
];
160 write_lock(&detail
->hash_lock
);
161 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
162 set_bit(CACHE_NEGATIVE
, &tmp
->flags
);
164 detail
->update(tmp
, new);
169 is_new
= cache_fresh_locked(tmp
, new->expiry_time
);
170 cache_fresh_locked(old
, 0);
171 write_unlock(&detail
->hash_lock
);
172 cache_fresh_unlocked(tmp
, detail
, is_new
);
173 cache_fresh_unlocked(old
, detail
, 0);
174 cache_put(old
, detail
);
177 EXPORT_SYMBOL_GPL(sunrpc_cache_update
);
179 static int cache_make_upcall(struct cache_detail
*cd
, struct cache_head
*h
)
181 if (!cd
->cache_upcall
)
183 return cd
->cache_upcall(cd
, h
);
187 * This is the generic cache management routine for all
188 * the authentication caches.
189 * It checks the currency of a cache item and will (later)
190 * initiate an upcall to fill it if needed.
193 * Returns 0 if the cache_head can be used, or cache_puts it and returns
194 * -EAGAIN if upcall is pending,
195 * -ETIMEDOUT if upcall failed and should be retried,
196 * -ENOENT if cache entry was negative
198 int cache_check(struct cache_detail
*detail
,
199 struct cache_head
*h
, struct cache_req
*rqstp
)
202 long refresh_age
, age
;
204 /* First decide return status as best we can */
205 if (!test_bit(CACHE_VALID
, &h
->flags
) ||
206 h
->expiry_time
< get_seconds())
208 else if (detail
->flush_time
> h
->last_refresh
)
212 if (test_bit(CACHE_NEGATIVE
, &h
->flags
))
217 /* now see if we want to start an upcall */
218 refresh_age
= (h
->expiry_time
- h
->last_refresh
);
219 age
= get_seconds() - h
->last_refresh
;
224 } else if (rv
== -EAGAIN
|| age
> refresh_age
/2) {
225 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
227 if (!test_and_set_bit(CACHE_PENDING
, &h
->flags
)) {
228 switch (cache_make_upcall(detail
, h
)) {
230 clear_bit(CACHE_PENDING
, &h
->flags
);
232 set_bit(CACHE_NEGATIVE
, &h
->flags
);
233 cache_fresh_unlocked(h
, detail
,
234 cache_fresh_locked(h
, get_seconds()+CACHE_NEW_EXPIRY
));
240 clear_bit(CACHE_PENDING
, &h
->flags
);
241 cache_revisit_request(h
);
248 if (cache_defer_req(rqstp
, h
) != 0)
252 cache_put(h
, detail
);
255 EXPORT_SYMBOL_GPL(cache_check
);
258 * caches need to be periodically cleaned.
259 * For this we maintain a list of cache_detail and
260 * a current pointer into that list and into the table
263 * Each time clean_cache is called it finds the next non-empty entry
264 * in the current table and walks the list in that entry
265 * looking for entries that can be removed.
267 * An entry gets removed if:
268 * - The expiry is before current time
269 * - The last_refresh time is before the flush_time for that cache
271 * later we might drop old entries with non-NEVER expiry if that table
272 * is getting 'full' for some definition of 'full'
274 * The question of "how often to scan a table" is an interesting one
275 * and is answered in part by the use of the "nextcheck" field in the
277 * When a scan of a table begins, the nextcheck field is set to a time
278 * that is well into the future.
279 * While scanning, if an expiry time is found that is earlier than the
280 * current nextcheck time, nextcheck is set to that expiry time.
281 * If the flush_time is ever set to a time earlier than the nextcheck
282 * time, the nextcheck time is then set to that flush_time.
284 * A table is then only scanned if the current time is at least
285 * the nextcheck time.
289 static LIST_HEAD(cache_list
);
290 static DEFINE_SPINLOCK(cache_list_lock
);
291 static struct cache_detail
*current_detail
;
292 static int current_index
;
294 static void do_cache_clean(struct work_struct
*work
);
295 static DECLARE_DELAYED_WORK(cache_cleaner
, do_cache_clean
);
297 static void sunrpc_init_cache_detail(struct cache_detail
*cd
)
299 rwlock_init(&cd
->hash_lock
);
300 INIT_LIST_HEAD(&cd
->queue
);
301 spin_lock(&cache_list_lock
);
304 atomic_set(&cd
->readers
, 0);
307 list_add(&cd
->others
, &cache_list
);
308 spin_unlock(&cache_list_lock
);
310 /* start the cleaning process */
311 schedule_delayed_work(&cache_cleaner
, 0);
314 static void sunrpc_destroy_cache_detail(struct cache_detail
*cd
)
317 spin_lock(&cache_list_lock
);
318 write_lock(&cd
->hash_lock
);
319 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
320 write_unlock(&cd
->hash_lock
);
321 spin_unlock(&cache_list_lock
);
324 if (current_detail
== cd
)
325 current_detail
= NULL
;
326 list_del_init(&cd
->others
);
327 write_unlock(&cd
->hash_lock
);
328 spin_unlock(&cache_list_lock
);
329 if (list_empty(&cache_list
)) {
330 /* module must be being unloaded so its safe to kill the worker */
331 cancel_delayed_work_sync(&cache_cleaner
);
335 printk(KERN_ERR
"nfsd: failed to unregister %s cache\n", cd
->name
);
338 /* clean cache tries to find something to clean
340 * It returns 1 if it cleaned something,
341 * 0 if it didn't find anything this time
342 * -1 if it fell off the end of the list.
344 static int cache_clean(void)
347 struct list_head
*next
;
349 spin_lock(&cache_list_lock
);
351 /* find a suitable table if we don't already have one */
352 while (current_detail
== NULL
||
353 current_index
>= current_detail
->hash_size
) {
355 next
= current_detail
->others
.next
;
357 next
= cache_list
.next
;
358 if (next
== &cache_list
) {
359 current_detail
= NULL
;
360 spin_unlock(&cache_list_lock
);
363 current_detail
= list_entry(next
, struct cache_detail
, others
);
364 if (current_detail
->nextcheck
> get_seconds())
365 current_index
= current_detail
->hash_size
;
368 current_detail
->nextcheck
= get_seconds()+30*60;
372 /* find a non-empty bucket in the table */
373 while (current_detail
&&
374 current_index
< current_detail
->hash_size
&&
375 current_detail
->hash_table
[current_index
] == NULL
)
378 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
380 if (current_detail
&& current_index
< current_detail
->hash_size
) {
381 struct cache_head
*ch
, **cp
;
382 struct cache_detail
*d
;
384 write_lock(¤t_detail
->hash_lock
);
386 /* Ok, now to clean this strand */
388 cp
= & current_detail
->hash_table
[current_index
];
390 for (; ch
; cp
= & ch
->next
, ch
= *cp
) {
391 if (current_detail
->nextcheck
> ch
->expiry_time
)
392 current_detail
->nextcheck
= ch
->expiry_time
+1;
393 if (ch
->expiry_time
>= get_seconds()
394 && ch
->last_refresh
>= current_detail
->flush_time
397 if (test_and_clear_bit(CACHE_PENDING
, &ch
->flags
))
398 queue_loose(current_detail
, ch
);
400 if (atomic_read(&ch
->ref
.refcount
) == 1)
406 current_detail
->entries
--;
409 write_unlock(¤t_detail
->hash_lock
);
413 spin_unlock(&cache_list_lock
);
417 spin_unlock(&cache_list_lock
);
423 * We want to regularly clean the cache, so we need to schedule some work ...
425 static void do_cache_clean(struct work_struct
*work
)
428 if (cache_clean() == -1)
429 delay
= round_jiffies_relative(30*HZ
);
431 if (list_empty(&cache_list
))
435 schedule_delayed_work(&cache_cleaner
, delay
);
440 * Clean all caches promptly. This just calls cache_clean
441 * repeatedly until we are sure that every cache has had a chance to
444 void cache_flush(void)
446 while (cache_clean() != -1)
448 while (cache_clean() != -1)
451 EXPORT_SYMBOL_GPL(cache_flush
);
453 void cache_purge(struct cache_detail
*detail
)
455 detail
->flush_time
= LONG_MAX
;
456 detail
->nextcheck
= get_seconds();
458 detail
->flush_time
= 1;
460 EXPORT_SYMBOL_GPL(cache_purge
);
464 * Deferral and Revisiting of Requests.
466 * If a cache lookup finds a pending entry, we
467 * need to defer the request and revisit it later.
468 * All deferred requests are stored in a hash table,
469 * indexed by "struct cache_head *".
470 * As it may be wasteful to store a whole request
471 * structure, we allow the request to provide a
472 * deferred form, which must contain a
473 * 'struct cache_deferred_req'
474 * This cache_deferred_req contains a method to allow
475 * it to be revisited when cache info is available
478 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
479 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
481 #define DFR_MAX 300 /* ??? */
483 static DEFINE_SPINLOCK(cache_defer_lock
);
484 static LIST_HEAD(cache_defer_list
);
485 static struct list_head cache_defer_hash
[DFR_HASHSIZE
];
486 static int cache_defer_cnt
;
488 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
490 struct cache_deferred_req
*dreq
;
491 int hash
= DFR_HASH(item
);
493 if (cache_defer_cnt
>= DFR_MAX
) {
494 /* too much in the cache, randomly drop this one,
495 * or continue and drop the oldest below
500 dreq
= req
->defer(req
);
506 spin_lock(&cache_defer_lock
);
508 list_add(&dreq
->recent
, &cache_defer_list
);
510 if (cache_defer_hash
[hash
].next
== NULL
)
511 INIT_LIST_HEAD(&cache_defer_hash
[hash
]);
512 list_add(&dreq
->hash
, &cache_defer_hash
[hash
]);
514 /* it is in, now maybe clean up */
516 if (++cache_defer_cnt
> DFR_MAX
) {
517 dreq
= list_entry(cache_defer_list
.prev
,
518 struct cache_deferred_req
, recent
);
519 list_del(&dreq
->recent
);
520 list_del(&dreq
->hash
);
523 spin_unlock(&cache_defer_lock
);
526 /* there was one too many */
527 dreq
->revisit(dreq
, 1);
529 if (!test_bit(CACHE_PENDING
, &item
->flags
)) {
530 /* must have just been validated... */
531 cache_revisit_request(item
);
536 static void cache_revisit_request(struct cache_head
*item
)
538 struct cache_deferred_req
*dreq
;
539 struct list_head pending
;
541 struct list_head
*lp
;
542 int hash
= DFR_HASH(item
);
544 INIT_LIST_HEAD(&pending
);
545 spin_lock(&cache_defer_lock
);
547 lp
= cache_defer_hash
[hash
].next
;
549 while (lp
!= &cache_defer_hash
[hash
]) {
550 dreq
= list_entry(lp
, struct cache_deferred_req
, hash
);
552 if (dreq
->item
== item
) {
553 list_del(&dreq
->hash
);
554 list_move(&dreq
->recent
, &pending
);
559 spin_unlock(&cache_defer_lock
);
561 while (!list_empty(&pending
)) {
562 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
563 list_del_init(&dreq
->recent
);
564 dreq
->revisit(dreq
, 0);
568 void cache_clean_deferred(void *owner
)
570 struct cache_deferred_req
*dreq
, *tmp
;
571 struct list_head pending
;
574 INIT_LIST_HEAD(&pending
);
575 spin_lock(&cache_defer_lock
);
577 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
578 if (dreq
->owner
== owner
) {
579 list_del(&dreq
->hash
);
580 list_move(&dreq
->recent
, &pending
);
584 spin_unlock(&cache_defer_lock
);
586 while (!list_empty(&pending
)) {
587 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
588 list_del_init(&dreq
->recent
);
589 dreq
->revisit(dreq
, 1);
594 * communicate with user-space
596 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
597 * On read, you get a full request, or block.
598 * On write, an update request is processed.
599 * Poll works if anything to read, and always allows write.
601 * Implemented by linked list of requests. Each open file has
602 * a ->private that also exists in this list. New requests are added
603 * to the end and may wakeup and preceding readers.
604 * New readers are added to the head. If, on read, an item is found with
605 * CACHE_UPCALLING clear, we free it from the list.
609 static DEFINE_SPINLOCK(queue_lock
);
610 static DEFINE_MUTEX(queue_io_mutex
);
613 struct list_head list
;
614 int reader
; /* if 0, then request */
616 struct cache_request
{
617 struct cache_queue q
;
618 struct cache_head
*item
;
623 struct cache_reader
{
624 struct cache_queue q
;
625 int offset
; /* if non-0, we have a refcnt on next request */
628 static ssize_t
cache_read(struct file
*filp
, char __user
*buf
, size_t count
,
629 loff_t
*ppos
, struct cache_detail
*cd
)
631 struct cache_reader
*rp
= filp
->private_data
;
632 struct cache_request
*rq
;
633 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
639 mutex_lock(&inode
->i_mutex
); /* protect against multiple concurrent
640 * readers on this file */
642 spin_lock(&queue_lock
);
643 /* need to find next request */
644 while (rp
->q
.list
.next
!= &cd
->queue
&&
645 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
647 struct list_head
*next
= rp
->q
.list
.next
;
648 list_move(&rp
->q
.list
, next
);
650 if (rp
->q
.list
.next
== &cd
->queue
) {
651 spin_unlock(&queue_lock
);
652 mutex_unlock(&inode
->i_mutex
);
656 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
657 BUG_ON(rq
->q
.reader
);
660 spin_unlock(&queue_lock
);
662 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
664 spin_lock(&queue_lock
);
665 list_move(&rp
->q
.list
, &rq
->q
.list
);
666 spin_unlock(&queue_lock
);
668 if (rp
->offset
+ count
> rq
->len
)
669 count
= rq
->len
- rp
->offset
;
671 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
674 if (rp
->offset
>= rq
->len
) {
676 spin_lock(&queue_lock
);
677 list_move(&rp
->q
.list
, &rq
->q
.list
);
678 spin_unlock(&queue_lock
);
683 if (rp
->offset
== 0) {
684 /* need to release rq */
685 spin_lock(&queue_lock
);
687 if (rq
->readers
== 0 &&
688 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
689 list_del(&rq
->q
.list
);
690 spin_unlock(&queue_lock
);
691 cache_put(rq
->item
, cd
);
695 spin_unlock(&queue_lock
);
699 mutex_unlock(&inode
->i_mutex
);
700 return err
? err
: count
;
703 static ssize_t
cache_do_downcall(char *kaddr
, const char __user
*buf
,
704 size_t count
, struct cache_detail
*cd
)
708 if (copy_from_user(kaddr
, buf
, count
))
711 ret
= cd
->cache_parse(cd
, kaddr
, count
);
717 static ssize_t
cache_slow_downcall(const char __user
*buf
,
718 size_t count
, struct cache_detail
*cd
)
720 static char write_buf
[8192]; /* protected by queue_io_mutex */
721 ssize_t ret
= -EINVAL
;
723 if (count
>= sizeof(write_buf
))
725 mutex_lock(&queue_io_mutex
);
726 ret
= cache_do_downcall(write_buf
, buf
, count
, cd
);
727 mutex_unlock(&queue_io_mutex
);
732 static ssize_t
cache_downcall(struct address_space
*mapping
,
733 const char __user
*buf
,
734 size_t count
, struct cache_detail
*cd
)
738 ssize_t ret
= -ENOMEM
;
740 if (count
>= PAGE_CACHE_SIZE
)
743 page
= find_or_create_page(mapping
, 0, GFP_KERNEL
);
748 ret
= cache_do_downcall(kaddr
, buf
, count
, cd
);
751 page_cache_release(page
);
754 return cache_slow_downcall(buf
, count
, cd
);
757 static ssize_t
cache_write(struct file
*filp
, const char __user
*buf
,
758 size_t count
, loff_t
*ppos
,
759 struct cache_detail
*cd
)
761 struct address_space
*mapping
= filp
->f_mapping
;
762 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
763 ssize_t ret
= -EINVAL
;
765 if (!cd
->cache_parse
)
768 mutex_lock(&inode
->i_mutex
);
769 ret
= cache_downcall(mapping
, buf
, count
, cd
);
770 mutex_unlock(&inode
->i_mutex
);
775 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
777 static unsigned int cache_poll(struct file
*filp
, poll_table
*wait
,
778 struct cache_detail
*cd
)
781 struct cache_reader
*rp
= filp
->private_data
;
782 struct cache_queue
*cq
;
784 poll_wait(filp
, &queue_wait
, wait
);
786 /* alway allow write */
787 mask
= POLL_OUT
| POLLWRNORM
;
792 spin_lock(&queue_lock
);
794 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
795 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
797 mask
|= POLLIN
| POLLRDNORM
;
800 spin_unlock(&queue_lock
);
804 static int cache_ioctl(struct inode
*ino
, struct file
*filp
,
805 unsigned int cmd
, unsigned long arg
,
806 struct cache_detail
*cd
)
809 struct cache_reader
*rp
= filp
->private_data
;
810 struct cache_queue
*cq
;
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
);
833 static int cache_open(struct inode
*inode
, struct file
*filp
,
834 struct cache_detail
*cd
)
836 struct cache_reader
*rp
= NULL
;
838 nonseekable_open(inode
, filp
);
839 if (filp
->f_mode
& FMODE_READ
) {
840 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
845 atomic_inc(&cd
->readers
);
846 spin_lock(&queue_lock
);
847 list_add(&rp
->q
.list
, &cd
->queue
);
848 spin_unlock(&queue_lock
);
850 filp
->private_data
= rp
;
854 static int cache_release(struct inode
*inode
, struct file
*filp
,
855 struct cache_detail
*cd
)
857 struct cache_reader
*rp
= filp
->private_data
;
860 spin_lock(&queue_lock
);
862 struct cache_queue
*cq
;
863 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
864 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
866 container_of(cq
, struct cache_request
, q
)
872 list_del(&rp
->q
.list
);
873 spin_unlock(&queue_lock
);
875 filp
->private_data
= NULL
;
878 cd
->last_close
= get_seconds();
879 atomic_dec(&cd
->readers
);
886 static void queue_loose(struct cache_detail
*detail
, struct cache_head
*ch
)
888 struct cache_queue
*cq
;
889 spin_lock(&queue_lock
);
890 list_for_each_entry(cq
, &detail
->queue
, list
)
892 struct cache_request
*cr
= container_of(cq
, struct cache_request
, q
);
895 if (cr
->readers
!= 0)
897 list_del(&cr
->q
.list
);
898 spin_unlock(&queue_lock
);
899 cache_put(cr
->item
, detail
);
904 spin_unlock(&queue_lock
);
908 * Support routines for text-based upcalls.
909 * Fields are separated by spaces.
910 * Fields are either mangled to quote space tab newline slosh with slosh
911 * or a hexified with a leading \x
912 * Record is terminated with newline.
916 void qword_add(char **bpp
, int *lp
, char *str
)
924 while ((c
=*str
++) && len
)
932 *bp
++ = '0' + ((c
& 0300)>>6);
933 *bp
++ = '0' + ((c
& 0070)>>3);
934 *bp
++ = '0' + ((c
& 0007)>>0);
942 if (c
|| len
<1) len
= -1;
950 EXPORT_SYMBOL_GPL(qword_add
);
952 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
963 while (blen
&& len
>= 2) {
964 unsigned char c
= *buf
++;
965 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
966 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
971 if (blen
|| len
<1) len
= -1;
979 EXPORT_SYMBOL_GPL(qword_addhex
);
981 static void warn_no_listener(struct cache_detail
*detail
)
983 if (detail
->last_warn
!= detail
->last_close
) {
984 detail
->last_warn
= detail
->last_close
;
985 if (detail
->warn_no_listener
)
986 detail
->warn_no_listener(detail
, detail
->last_close
!= 0);
991 * register an upcall request to user-space and queue it up for read() by the
994 * Each request is at most one page long.
996 int sunrpc_cache_pipe_upcall(struct cache_detail
*detail
, struct cache_head
*h
,
997 void (*cache_request
)(struct cache_detail
*,
1004 struct cache_request
*crq
;
1008 if (atomic_read(&detail
->readers
) == 0 &&
1009 detail
->last_close
< get_seconds() - 30) {
1010 warn_no_listener(detail
);
1014 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1018 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1024 bp
= buf
; len
= PAGE_SIZE
;
1026 cache_request(detail
, h
, &bp
, &len
);
1034 crq
->item
= cache_get(h
);
1036 crq
->len
= PAGE_SIZE
- len
;
1038 spin_lock(&queue_lock
);
1039 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1040 spin_unlock(&queue_lock
);
1041 wake_up(&queue_wait
);
1044 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall
);
1047 * parse a message from user-space and pass it
1048 * to an appropriate cache
1049 * Messages are, like requests, separated into fields by
1050 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1053 * reply cachename expiry key ... content....
1055 * key and content are both parsed by cache
1058 #define isodigit(c) (isdigit(c) && c <= '7')
1059 int qword_get(char **bpp
, char *dest
, int bufsize
)
1061 /* return bytes copied, or -1 on error */
1065 while (*bp
== ' ') bp
++;
1067 if (bp
[0] == '\\' && bp
[1] == 'x') {
1070 while (isxdigit(bp
[0]) && isxdigit(bp
[1]) && len
< bufsize
) {
1071 int byte
= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1074 byte
|= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1080 /* text with \nnn octal quoting */
1081 while (*bp
!= ' ' && *bp
!= '\n' && *bp
&& len
< bufsize
-1) {
1083 isodigit(bp
[1]) && (bp
[1] <= '3') &&
1086 int byte
= (*++bp
-'0');
1088 byte
= (byte
<< 3) | (*bp
++ - '0');
1089 byte
= (byte
<< 3) | (*bp
++ - '0');
1099 if (*bp
!= ' ' && *bp
!= '\n' && *bp
!= '\0')
1101 while (*bp
== ' ') bp
++;
1106 EXPORT_SYMBOL_GPL(qword_get
);
1110 * support /proc/sunrpc/cache/$CACHENAME/content
1112 * We call ->cache_show passing NULL for the item to
1113 * get a header, then pass each real item in the cache
1117 struct cache_detail
*cd
;
1120 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1121 __acquires(cd
->hash_lock
)
1124 unsigned hash
, entry
;
1125 struct cache_head
*ch
;
1126 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1129 read_lock(&cd
->hash_lock
);
1131 return SEQ_START_TOKEN
;
1133 entry
= n
& ((1LL<<32) - 1);
1135 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1138 n
&= ~((1LL<<32) - 1);
1142 } while(hash
< cd
->hash_size
&&
1143 cd
->hash_table
[hash
]==NULL
);
1144 if (hash
>= cd
->hash_size
)
1147 return cd
->hash_table
[hash
];
1150 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1152 struct cache_head
*ch
= p
;
1153 int hash
= (*pos
>> 32);
1154 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1156 if (p
== SEQ_START_TOKEN
)
1158 else if (ch
->next
== NULL
) {
1165 *pos
&= ~((1LL<<32) - 1);
1166 while (hash
< cd
->hash_size
&&
1167 cd
->hash_table
[hash
] == NULL
) {
1171 if (hash
>= cd
->hash_size
)
1174 return cd
->hash_table
[hash
];
1177 static void c_stop(struct seq_file
*m
, void *p
)
1178 __releases(cd
->hash_lock
)
1180 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1181 read_unlock(&cd
->hash_lock
);
1184 static int c_show(struct seq_file
*m
, void *p
)
1186 struct cache_head
*cp
= p
;
1187 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1189 if (p
== SEQ_START_TOKEN
)
1190 return cd
->cache_show(m
, cd
, NULL
);
1193 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1194 cp
->expiry_time
, atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1196 if (cache_check(cd
, cp
, NULL
))
1197 /* cache_check does a cache_put on failure */
1198 seq_printf(m
, "# ");
1202 return cd
->cache_show(m
, cd
, cp
);
1205 static const struct seq_operations cache_content_op
= {
1212 static int content_open(struct inode
*inode
, struct file
*file
,
1213 struct cache_detail
*cd
)
1217 han
= __seq_open_private(file
, &cache_content_op
, sizeof(*han
));
1225 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1226 size_t count
, loff_t
*ppos
,
1227 struct cache_detail
*cd
)
1230 unsigned long p
= *ppos
;
1233 sprintf(tbuf
, "%lu\n", cd
->flush_time
);
1240 if (copy_to_user(buf
, (void*)(tbuf
+p
), len
))
1246 static ssize_t
write_flush(struct file
*file
, const char __user
*buf
,
1247 size_t count
, loff_t
*ppos
,
1248 struct cache_detail
*cd
)
1253 if (*ppos
|| count
> sizeof(tbuf
)-1)
1255 if (copy_from_user(tbuf
, buf
, count
))
1258 flushtime
= simple_strtoul(tbuf
, &ep
, 0);
1259 if (*ep
&& *ep
!= '\n')
1262 cd
->flush_time
= flushtime
;
1263 cd
->nextcheck
= get_seconds();
1270 static ssize_t
cache_read_procfs(struct file
*filp
, char __user
*buf
,
1271 size_t count
, loff_t
*ppos
)
1273 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1275 return cache_read(filp
, buf
, count
, ppos
, cd
);
1278 static ssize_t
cache_write_procfs(struct file
*filp
, const char __user
*buf
,
1279 size_t count
, loff_t
*ppos
)
1281 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1283 return cache_write(filp
, buf
, count
, ppos
, cd
);
1286 static unsigned int cache_poll_procfs(struct file
*filp
, poll_table
*wait
)
1288 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1290 return cache_poll(filp
, wait
, cd
);
1293 static int cache_ioctl_procfs(struct inode
*inode
, struct file
*filp
,
1294 unsigned int cmd
, unsigned long arg
)
1296 struct cache_detail
*cd
= PDE(inode
)->data
;
1298 return cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1301 static int cache_open_procfs(struct inode
*inode
, struct file
*filp
)
1303 struct cache_detail
*cd
= PDE(inode
)->data
;
1305 return cache_open(inode
, filp
, cd
);
1308 static int cache_release_procfs(struct inode
*inode
, struct file
*filp
)
1310 struct cache_detail
*cd
= PDE(inode
)->data
;
1312 return cache_release(inode
, filp
, cd
);
1315 static const struct file_operations cache_file_operations_procfs
= {
1316 .owner
= THIS_MODULE
,
1317 .llseek
= no_llseek
,
1318 .read
= cache_read_procfs
,
1319 .write
= cache_write_procfs
,
1320 .poll
= cache_poll_procfs
,
1321 .ioctl
= cache_ioctl_procfs
, /* for FIONREAD */
1322 .open
= cache_open_procfs
,
1323 .release
= cache_release_procfs
,
1326 static int content_open_procfs(struct inode
*inode
, struct file
*filp
)
1328 struct cache_detail
*cd
= PDE(inode
)->data
;
1330 return content_open(inode
, filp
, cd
);
1333 static const struct file_operations content_file_operations_procfs
= {
1334 .open
= content_open_procfs
,
1336 .llseek
= seq_lseek
,
1337 .release
= seq_release_private
,
1340 static ssize_t
read_flush_procfs(struct file
*filp
, char __user
*buf
,
1341 size_t count
, loff_t
*ppos
)
1343 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1345 return read_flush(filp
, buf
, count
, ppos
, cd
);
1348 static ssize_t
write_flush_procfs(struct file
*filp
,
1349 const char __user
*buf
,
1350 size_t count
, loff_t
*ppos
)
1352 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1354 return write_flush(filp
, buf
, count
, ppos
, cd
);
1357 static const struct file_operations cache_flush_operations_procfs
= {
1358 .open
= nonseekable_open
,
1359 .read
= read_flush_procfs
,
1360 .write
= write_flush_procfs
,
1363 static void remove_cache_proc_entries(struct cache_detail
*cd
)
1365 if (cd
->u
.procfs
.proc_ent
== NULL
)
1367 if (cd
->u
.procfs
.flush_ent
)
1368 remove_proc_entry("flush", cd
->u
.procfs
.proc_ent
);
1369 if (cd
->u
.procfs
.channel_ent
)
1370 remove_proc_entry("channel", cd
->u
.procfs
.proc_ent
);
1371 if (cd
->u
.procfs
.content_ent
)
1372 remove_proc_entry("content", cd
->u
.procfs
.proc_ent
);
1373 cd
->u
.procfs
.proc_ent
= NULL
;
1374 remove_proc_entry(cd
->name
, proc_net_rpc
);
1377 #ifdef CONFIG_PROC_FS
1378 static int create_cache_proc_entries(struct cache_detail
*cd
)
1380 struct proc_dir_entry
*p
;
1382 cd
->u
.procfs
.proc_ent
= proc_mkdir(cd
->name
, proc_net_rpc
);
1383 if (cd
->u
.procfs
.proc_ent
== NULL
)
1385 cd
->u
.procfs
.channel_ent
= NULL
;
1386 cd
->u
.procfs
.content_ent
= NULL
;
1388 p
= proc_create_data("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
1389 cd
->u
.procfs
.proc_ent
,
1390 &cache_flush_operations_procfs
, cd
);
1391 cd
->u
.procfs
.flush_ent
= p
;
1395 if (cd
->cache_upcall
|| cd
->cache_parse
) {
1396 p
= proc_create_data("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
1397 cd
->u
.procfs
.proc_ent
,
1398 &cache_file_operations_procfs
, cd
);
1399 cd
->u
.procfs
.channel_ent
= p
;
1403 if (cd
->cache_show
) {
1404 p
= proc_create_data("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
1405 cd
->u
.procfs
.proc_ent
,
1406 &content_file_operations_procfs
, cd
);
1407 cd
->u
.procfs
.content_ent
= p
;
1413 remove_cache_proc_entries(cd
);
1416 #else /* CONFIG_PROC_FS */
1417 static int create_cache_proc_entries(struct cache_detail
*cd
)
1423 int cache_register(struct cache_detail
*cd
)
1427 sunrpc_init_cache_detail(cd
);
1428 ret
= create_cache_proc_entries(cd
);
1430 sunrpc_destroy_cache_detail(cd
);
1433 EXPORT_SYMBOL_GPL(cache_register
);
1435 void cache_unregister(struct cache_detail
*cd
)
1437 remove_cache_proc_entries(cd
);
1438 sunrpc_destroy_cache_detail(cd
);
1440 EXPORT_SYMBOL_GPL(cache_unregister
);