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_GPL(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_GPL(sunrpc_cache_update
);
178 static int cache_make_upcall(struct cache_detail
*detail
, struct cache_head
*h
);
180 * This is the generic cache management routine for all
181 * the authentication caches.
182 * It checks the currency of a cache item and will (later)
183 * initiate an upcall to fill it if needed.
186 * Returns 0 if the cache_head can be used, or cache_puts it and returns
187 * -EAGAIN if upcall is pending,
188 * -ETIMEDOUT if upcall failed and should be retried,
189 * -ENOENT if cache entry was negative
191 int cache_check(struct cache_detail
*detail
,
192 struct cache_head
*h
, struct cache_req
*rqstp
)
195 long refresh_age
, age
;
197 /* First decide return status as best we can */
198 if (!test_bit(CACHE_VALID
, &h
->flags
) ||
199 h
->expiry_time
< get_seconds())
201 else if (detail
->flush_time
> h
->last_refresh
)
205 if (test_bit(CACHE_NEGATIVE
, &h
->flags
))
210 /* now see if we want to start an upcall */
211 refresh_age
= (h
->expiry_time
- h
->last_refresh
);
212 age
= get_seconds() - h
->last_refresh
;
217 } else if (rv
== -EAGAIN
|| age
> refresh_age
/2) {
218 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
220 if (!test_and_set_bit(CACHE_PENDING
, &h
->flags
)) {
221 switch (cache_make_upcall(detail
, h
)) {
223 clear_bit(CACHE_PENDING
, &h
->flags
);
225 set_bit(CACHE_NEGATIVE
, &h
->flags
);
226 cache_fresh_unlocked(h
, detail
,
227 cache_fresh_locked(h
, get_seconds()+CACHE_NEW_EXPIRY
));
233 clear_bit(CACHE_PENDING
, &h
->flags
);
234 cache_revisit_request(h
);
241 if (cache_defer_req(rqstp
, h
) != 0)
245 cache_put(h
, detail
);
248 EXPORT_SYMBOL_GPL(cache_check
);
251 * caches need to be periodically cleaned.
252 * For this we maintain a list of cache_detail and
253 * a current pointer into that list and into the table
256 * Each time clean_cache is called it finds the next non-empty entry
257 * in the current table and walks the list in that entry
258 * looking for entries that can be removed.
260 * An entry gets removed if:
261 * - The expiry is before current time
262 * - The last_refresh time is before the flush_time for that cache
264 * later we might drop old entries with non-NEVER expiry if that table
265 * is getting 'full' for some definition of 'full'
267 * The question of "how often to scan a table" is an interesting one
268 * and is answered in part by the use of the "nextcheck" field in the
270 * When a scan of a table begins, the nextcheck field is set to a time
271 * that is well into the future.
272 * While scanning, if an expiry time is found that is earlier than the
273 * current nextcheck time, nextcheck is set to that expiry time.
274 * If the flush_time is ever set to a time earlier than the nextcheck
275 * time, the nextcheck time is then set to that flush_time.
277 * A table is then only scanned if the current time is at least
278 * the nextcheck time.
282 static LIST_HEAD(cache_list
);
283 static DEFINE_SPINLOCK(cache_list_lock
);
284 static struct cache_detail
*current_detail
;
285 static int current_index
;
287 static const struct file_operations cache_file_operations
;
288 static const struct file_operations content_file_operations
;
289 static const struct file_operations cache_flush_operations
;
291 static void do_cache_clean(struct work_struct
*work
);
292 static DECLARE_DELAYED_WORK(cache_cleaner
, do_cache_clean
);
294 static void remove_cache_proc_entries(struct cache_detail
*cd
)
296 if (cd
->proc_ent
== NULL
)
299 remove_proc_entry("flush", cd
->proc_ent
);
301 remove_proc_entry("channel", cd
->proc_ent
);
303 remove_proc_entry("content", cd
->proc_ent
);
305 remove_proc_entry(cd
->name
, proc_net_rpc
);
308 #ifdef CONFIG_PROC_FS
309 static int create_cache_proc_entries(struct cache_detail
*cd
)
311 struct proc_dir_entry
*p
;
313 cd
->proc_ent
= proc_mkdir(cd
->name
, proc_net_rpc
);
314 if (cd
->proc_ent
== NULL
)
316 cd
->proc_ent
->owner
= cd
->owner
;
317 cd
->channel_ent
= cd
->content_ent
= NULL
;
319 p
= proc_create_data("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
320 cd
->proc_ent
, &cache_flush_operations
, cd
);
324 p
->owner
= cd
->owner
;
326 if (cd
->cache_request
|| cd
->cache_parse
) {
327 p
= proc_create_data("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
328 cd
->proc_ent
, &cache_file_operations
, cd
);
332 p
->owner
= cd
->owner
;
334 if (cd
->cache_show
) {
335 p
= proc_create_data("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
336 cd
->proc_ent
, &content_file_operations
, cd
);
340 p
->owner
= cd
->owner
;
344 remove_cache_proc_entries(cd
);
347 #else /* CONFIG_PROC_FS */
348 static int create_cache_proc_entries(struct cache_detail
*cd
)
354 int cache_register(struct cache_detail
*cd
)
358 ret
= create_cache_proc_entries(cd
);
361 rwlock_init(&cd
->hash_lock
);
362 INIT_LIST_HEAD(&cd
->queue
);
363 spin_lock(&cache_list_lock
);
366 atomic_set(&cd
->readers
, 0);
369 list_add(&cd
->others
, &cache_list
);
370 spin_unlock(&cache_list_lock
);
372 /* start the cleaning process */
373 schedule_delayed_work(&cache_cleaner
, 0);
376 EXPORT_SYMBOL_GPL(cache_register
);
378 void cache_unregister(struct cache_detail
*cd
)
381 spin_lock(&cache_list_lock
);
382 write_lock(&cd
->hash_lock
);
383 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
384 write_unlock(&cd
->hash_lock
);
385 spin_unlock(&cache_list_lock
);
388 if (current_detail
== cd
)
389 current_detail
= NULL
;
390 list_del_init(&cd
->others
);
391 write_unlock(&cd
->hash_lock
);
392 spin_unlock(&cache_list_lock
);
393 remove_cache_proc_entries(cd
);
394 if (list_empty(&cache_list
)) {
395 /* module must be being unloaded so its safe to kill the worker */
396 cancel_delayed_work_sync(&cache_cleaner
);
400 printk(KERN_ERR
"nfsd: failed to unregister %s cache\n", cd
->name
);
402 EXPORT_SYMBOL_GPL(cache_unregister
);
404 /* clean cache tries to find something to clean
406 * It returns 1 if it cleaned something,
407 * 0 if it didn't find anything this time
408 * -1 if it fell off the end of the list.
410 static int cache_clean(void)
413 struct list_head
*next
;
415 spin_lock(&cache_list_lock
);
417 /* find a suitable table if we don't already have one */
418 while (current_detail
== NULL
||
419 current_index
>= current_detail
->hash_size
) {
421 next
= current_detail
->others
.next
;
423 next
= cache_list
.next
;
424 if (next
== &cache_list
) {
425 current_detail
= NULL
;
426 spin_unlock(&cache_list_lock
);
429 current_detail
= list_entry(next
, struct cache_detail
, others
);
430 if (current_detail
->nextcheck
> get_seconds())
431 current_index
= current_detail
->hash_size
;
434 current_detail
->nextcheck
= get_seconds()+30*60;
438 /* find a non-empty bucket in the table */
439 while (current_detail
&&
440 current_index
< current_detail
->hash_size
&&
441 current_detail
->hash_table
[current_index
] == NULL
)
444 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
446 if (current_detail
&& current_index
< current_detail
->hash_size
) {
447 struct cache_head
*ch
, **cp
;
448 struct cache_detail
*d
;
450 write_lock(¤t_detail
->hash_lock
);
452 /* Ok, now to clean this strand */
454 cp
= & current_detail
->hash_table
[current_index
];
456 for (; ch
; cp
= & ch
->next
, ch
= *cp
) {
457 if (current_detail
->nextcheck
> ch
->expiry_time
)
458 current_detail
->nextcheck
= ch
->expiry_time
+1;
459 if (ch
->expiry_time
>= get_seconds()
460 && ch
->last_refresh
>= current_detail
->flush_time
463 if (test_and_clear_bit(CACHE_PENDING
, &ch
->flags
))
464 queue_loose(current_detail
, ch
);
466 if (atomic_read(&ch
->ref
.refcount
) == 1)
472 current_detail
->entries
--;
475 write_unlock(¤t_detail
->hash_lock
);
479 spin_unlock(&cache_list_lock
);
483 spin_unlock(&cache_list_lock
);
489 * We want to regularly clean the cache, so we need to schedule some work ...
491 static void do_cache_clean(struct work_struct
*work
)
494 if (cache_clean() == -1)
497 if (list_empty(&cache_list
))
501 schedule_delayed_work(&cache_cleaner
, delay
);
506 * Clean all caches promptly. This just calls cache_clean
507 * repeatedly until we are sure that every cache has had a chance to
510 void cache_flush(void)
512 while (cache_clean() != -1)
514 while (cache_clean() != -1)
517 EXPORT_SYMBOL_GPL(cache_flush
);
519 void cache_purge(struct cache_detail
*detail
)
521 detail
->flush_time
= LONG_MAX
;
522 detail
->nextcheck
= get_seconds();
524 detail
->flush_time
= 1;
526 EXPORT_SYMBOL_GPL(cache_purge
);
530 * Deferral and Revisiting of Requests.
532 * If a cache lookup finds a pending entry, we
533 * need to defer the request and revisit it later.
534 * All deferred requests are stored in a hash table,
535 * indexed by "struct cache_head *".
536 * As it may be wasteful to store a whole request
537 * structure, we allow the request to provide a
538 * deferred form, which must contain a
539 * 'struct cache_deferred_req'
540 * This cache_deferred_req contains a method to allow
541 * it to be revisited when cache info is available
544 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
545 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
547 #define DFR_MAX 300 /* ??? */
549 static DEFINE_SPINLOCK(cache_defer_lock
);
550 static LIST_HEAD(cache_defer_list
);
551 static struct list_head cache_defer_hash
[DFR_HASHSIZE
];
552 static int cache_defer_cnt
;
554 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
556 struct cache_deferred_req
*dreq
;
557 int hash
= DFR_HASH(item
);
559 if (cache_defer_cnt
>= DFR_MAX
) {
560 /* too much in the cache, randomly drop this one,
561 * or continue and drop the oldest below
566 dreq
= req
->defer(req
);
572 spin_lock(&cache_defer_lock
);
574 list_add(&dreq
->recent
, &cache_defer_list
);
576 if (cache_defer_hash
[hash
].next
== NULL
)
577 INIT_LIST_HEAD(&cache_defer_hash
[hash
]);
578 list_add(&dreq
->hash
, &cache_defer_hash
[hash
]);
580 /* it is in, now maybe clean up */
582 if (++cache_defer_cnt
> DFR_MAX
) {
583 dreq
= list_entry(cache_defer_list
.prev
,
584 struct cache_deferred_req
, recent
);
585 list_del(&dreq
->recent
);
586 list_del(&dreq
->hash
);
589 spin_unlock(&cache_defer_lock
);
592 /* there was one too many */
593 dreq
->revisit(dreq
, 1);
595 if (!test_bit(CACHE_PENDING
, &item
->flags
)) {
596 /* must have just been validated... */
597 cache_revisit_request(item
);
602 static void cache_revisit_request(struct cache_head
*item
)
604 struct cache_deferred_req
*dreq
;
605 struct list_head pending
;
607 struct list_head
*lp
;
608 int hash
= DFR_HASH(item
);
610 INIT_LIST_HEAD(&pending
);
611 spin_lock(&cache_defer_lock
);
613 lp
= cache_defer_hash
[hash
].next
;
615 while (lp
!= &cache_defer_hash
[hash
]) {
616 dreq
= list_entry(lp
, struct cache_deferred_req
, hash
);
618 if (dreq
->item
== item
) {
619 list_del(&dreq
->hash
);
620 list_move(&dreq
->recent
, &pending
);
625 spin_unlock(&cache_defer_lock
);
627 while (!list_empty(&pending
)) {
628 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
629 list_del_init(&dreq
->recent
);
630 dreq
->revisit(dreq
, 0);
634 void cache_clean_deferred(void *owner
)
636 struct cache_deferred_req
*dreq
, *tmp
;
637 struct list_head pending
;
640 INIT_LIST_HEAD(&pending
);
641 spin_lock(&cache_defer_lock
);
643 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
644 if (dreq
->owner
== owner
) {
645 list_del(&dreq
->hash
);
646 list_move(&dreq
->recent
, &pending
);
650 spin_unlock(&cache_defer_lock
);
652 while (!list_empty(&pending
)) {
653 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
654 list_del_init(&dreq
->recent
);
655 dreq
->revisit(dreq
, 1);
660 * communicate with user-space
662 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
663 * On read, you get a full request, or block.
664 * On write, an update request is processed.
665 * Poll works if anything to read, and always allows write.
667 * Implemented by linked list of requests. Each open file has
668 * a ->private that also exists in this list. New requests are added
669 * to the end and may wakeup and preceding readers.
670 * New readers are added to the head. If, on read, an item is found with
671 * CACHE_UPCALLING clear, we free it from the list.
675 static DEFINE_SPINLOCK(queue_lock
);
676 static DEFINE_MUTEX(queue_io_mutex
);
679 struct list_head list
;
680 int reader
; /* if 0, then request */
682 struct cache_request
{
683 struct cache_queue q
;
684 struct cache_head
*item
;
689 struct cache_reader
{
690 struct cache_queue q
;
691 int offset
; /* if non-0, we have a refcnt on next request */
695 cache_read(struct file
*filp
, char __user
*buf
, size_t count
, loff_t
*ppos
)
697 struct cache_reader
*rp
= filp
->private_data
;
698 struct cache_request
*rq
;
699 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
705 mutex_lock(&queue_io_mutex
); /* protect against multiple concurrent
706 * readers on this file */
708 spin_lock(&queue_lock
);
709 /* need to find next request */
710 while (rp
->q
.list
.next
!= &cd
->queue
&&
711 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
713 struct list_head
*next
= rp
->q
.list
.next
;
714 list_move(&rp
->q
.list
, next
);
716 if (rp
->q
.list
.next
== &cd
->queue
) {
717 spin_unlock(&queue_lock
);
718 mutex_unlock(&queue_io_mutex
);
722 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
723 BUG_ON(rq
->q
.reader
);
726 spin_unlock(&queue_lock
);
728 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
730 spin_lock(&queue_lock
);
731 list_move(&rp
->q
.list
, &rq
->q
.list
);
732 spin_unlock(&queue_lock
);
734 if (rp
->offset
+ count
> rq
->len
)
735 count
= rq
->len
- rp
->offset
;
737 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
740 if (rp
->offset
>= rq
->len
) {
742 spin_lock(&queue_lock
);
743 list_move(&rp
->q
.list
, &rq
->q
.list
);
744 spin_unlock(&queue_lock
);
749 if (rp
->offset
== 0) {
750 /* need to release rq */
751 spin_lock(&queue_lock
);
753 if (rq
->readers
== 0 &&
754 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
755 list_del(&rq
->q
.list
);
756 spin_unlock(&queue_lock
);
757 cache_put(rq
->item
, cd
);
761 spin_unlock(&queue_lock
);
765 mutex_unlock(&queue_io_mutex
);
766 return err
? err
: count
;
769 static char write_buf
[8192]; /* protected by queue_io_mutex */
772 cache_write(struct file
*filp
, const char __user
*buf
, size_t count
,
776 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
780 if (count
>= sizeof(write_buf
))
783 mutex_lock(&queue_io_mutex
);
785 if (copy_from_user(write_buf
, buf
, count
)) {
786 mutex_unlock(&queue_io_mutex
);
789 write_buf
[count
] = '\0';
791 err
= cd
->cache_parse(cd
, write_buf
, count
);
795 mutex_unlock(&queue_io_mutex
);
796 return err
? err
: count
;
799 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
802 cache_poll(struct file
*filp
, poll_table
*wait
)
805 struct cache_reader
*rp
= filp
->private_data
;
806 struct cache_queue
*cq
;
807 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
809 poll_wait(filp
, &queue_wait
, wait
);
811 /* alway allow write */
812 mask
= POLL_OUT
| POLLWRNORM
;
817 spin_lock(&queue_lock
);
819 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
820 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
822 mask
|= POLLIN
| POLLRDNORM
;
825 spin_unlock(&queue_lock
);
830 cache_ioctl(struct inode
*ino
, struct file
*filp
,
831 unsigned int cmd
, unsigned long arg
)
834 struct cache_reader
*rp
= filp
->private_data
;
835 struct cache_queue
*cq
;
836 struct cache_detail
*cd
= PDE(ino
)->data
;
838 if (cmd
!= FIONREAD
|| !rp
)
841 spin_lock(&queue_lock
);
843 /* only find the length remaining in current request,
844 * or the length of the next request
846 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
847 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
849 struct cache_request
*cr
=
850 container_of(cq
, struct cache_request
, q
);
851 len
= cr
->len
- rp
->offset
;
854 spin_unlock(&queue_lock
);
856 return put_user(len
, (int __user
*)arg
);
860 cache_open(struct inode
*inode
, struct file
*filp
)
862 struct cache_reader
*rp
= NULL
;
864 nonseekable_open(inode
, filp
);
865 if (filp
->f_mode
& FMODE_READ
) {
866 struct cache_detail
*cd
= PDE(inode
)->data
;
868 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
873 atomic_inc(&cd
->readers
);
874 spin_lock(&queue_lock
);
875 list_add(&rp
->q
.list
, &cd
->queue
);
876 spin_unlock(&queue_lock
);
878 filp
->private_data
= rp
;
883 cache_release(struct inode
*inode
, struct file
*filp
)
885 struct cache_reader
*rp
= filp
->private_data
;
886 struct cache_detail
*cd
= PDE(inode
)->data
;
889 spin_lock(&queue_lock
);
891 struct cache_queue
*cq
;
892 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
893 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
895 container_of(cq
, struct cache_request
, q
)
901 list_del(&rp
->q
.list
);
902 spin_unlock(&queue_lock
);
904 filp
->private_data
= NULL
;
907 cd
->last_close
= get_seconds();
908 atomic_dec(&cd
->readers
);
915 static const struct file_operations cache_file_operations
= {
916 .owner
= THIS_MODULE
,
919 .write
= cache_write
,
921 .ioctl
= cache_ioctl
, /* for FIONREAD */
923 .release
= cache_release
,
927 static void queue_loose(struct cache_detail
*detail
, struct cache_head
*ch
)
929 struct cache_queue
*cq
;
930 spin_lock(&queue_lock
);
931 list_for_each_entry(cq
, &detail
->queue
, list
)
933 struct cache_request
*cr
= container_of(cq
, struct cache_request
, q
);
936 if (cr
->readers
!= 0)
938 list_del(&cr
->q
.list
);
939 spin_unlock(&queue_lock
);
940 cache_put(cr
->item
, detail
);
945 spin_unlock(&queue_lock
);
949 * Support routines for text-based upcalls.
950 * Fields are separated by spaces.
951 * Fields are either mangled to quote space tab newline slosh with slosh
952 * or a hexified with a leading \x
953 * Record is terminated with newline.
957 void qword_add(char **bpp
, int *lp
, char *str
)
965 while ((c
=*str
++) && len
)
973 *bp
++ = '0' + ((c
& 0300)>>6);
974 *bp
++ = '0' + ((c
& 0070)>>3);
975 *bp
++ = '0' + ((c
& 0007)>>0);
983 if (c
|| len
<1) len
= -1;
991 EXPORT_SYMBOL_GPL(qword_add
);
993 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
1004 while (blen
&& len
>= 2) {
1005 unsigned char c
= *buf
++;
1006 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
1007 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
1012 if (blen
|| len
<1) len
= -1;
1020 EXPORT_SYMBOL_GPL(qword_addhex
);
1022 static void warn_no_listener(struct cache_detail
*detail
)
1024 if (detail
->last_warn
!= detail
->last_close
) {
1025 detail
->last_warn
= detail
->last_close
;
1026 if (detail
->warn_no_listener
)
1027 detail
->warn_no_listener(detail
);
1032 * register an upcall request to user-space.
1033 * Each request is at most one page long.
1035 static int cache_make_upcall(struct cache_detail
*detail
, struct cache_head
*h
)
1039 struct cache_request
*crq
;
1043 if (detail
->cache_request
== NULL
)
1046 if (atomic_read(&detail
->readers
) == 0 &&
1047 detail
->last_close
< get_seconds() - 30) {
1048 warn_no_listener(detail
);
1052 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1056 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1062 bp
= buf
; len
= PAGE_SIZE
;
1064 detail
->cache_request(detail
, h
, &bp
, &len
);
1072 crq
->item
= cache_get(h
);
1074 crq
->len
= PAGE_SIZE
- len
;
1076 spin_lock(&queue_lock
);
1077 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1078 spin_unlock(&queue_lock
);
1079 wake_up(&queue_wait
);
1084 * parse a message from user-space and pass it
1085 * to an appropriate cache
1086 * Messages are, like requests, separated into fields by
1087 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1090 * reply cachename expiry key ... content....
1092 * key and content are both parsed by cache
1095 #define isodigit(c) (isdigit(c) && c <= '7')
1096 int qword_get(char **bpp
, char *dest
, int bufsize
)
1098 /* return bytes copied, or -1 on error */
1102 while (*bp
== ' ') bp
++;
1104 if (bp
[0] == '\\' && bp
[1] == 'x') {
1107 while (isxdigit(bp
[0]) && isxdigit(bp
[1]) && len
< bufsize
) {
1108 int byte
= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1111 byte
|= isdigit(*bp
) ? *bp
-'0' : toupper(*bp
)-'A'+10;
1117 /* text with \nnn octal quoting */
1118 while (*bp
!= ' ' && *bp
!= '\n' && *bp
&& len
< bufsize
-1) {
1120 isodigit(bp
[1]) && (bp
[1] <= '3') &&
1123 int byte
= (*++bp
-'0');
1125 byte
= (byte
<< 3) | (*bp
++ - '0');
1126 byte
= (byte
<< 3) | (*bp
++ - '0');
1136 if (*bp
!= ' ' && *bp
!= '\n' && *bp
!= '\0')
1138 while (*bp
== ' ') bp
++;
1143 EXPORT_SYMBOL_GPL(qword_get
);
1147 * support /proc/sunrpc/cache/$CACHENAME/content
1149 * We call ->cache_show passing NULL for the item to
1150 * get a header, then pass each real item in the cache
1154 struct cache_detail
*cd
;
1157 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1158 __acquires(cd
->hash_lock
)
1161 unsigned hash
, entry
;
1162 struct cache_head
*ch
;
1163 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1166 read_lock(&cd
->hash_lock
);
1168 return SEQ_START_TOKEN
;
1170 entry
= n
& ((1LL<<32) - 1);
1172 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1175 n
&= ~((1LL<<32) - 1);
1179 } while(hash
< cd
->hash_size
&&
1180 cd
->hash_table
[hash
]==NULL
);
1181 if (hash
>= cd
->hash_size
)
1184 return cd
->hash_table
[hash
];
1187 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1189 struct cache_head
*ch
= p
;
1190 int hash
= (*pos
>> 32);
1191 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1193 if (p
== SEQ_START_TOKEN
)
1195 else if (ch
->next
== NULL
) {
1202 *pos
&= ~((1LL<<32) - 1);
1203 while (hash
< cd
->hash_size
&&
1204 cd
->hash_table
[hash
] == NULL
) {
1208 if (hash
>= cd
->hash_size
)
1211 return cd
->hash_table
[hash
];
1214 static void c_stop(struct seq_file
*m
, void *p
)
1215 __releases(cd
->hash_lock
)
1217 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1218 read_unlock(&cd
->hash_lock
);
1221 static int c_show(struct seq_file
*m
, void *p
)
1223 struct cache_head
*cp
= p
;
1224 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1226 if (p
== SEQ_START_TOKEN
)
1227 return cd
->cache_show(m
, cd
, NULL
);
1230 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1231 cp
->expiry_time
, atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1233 if (cache_check(cd
, cp
, NULL
))
1234 /* cache_check does a cache_put on failure */
1235 seq_printf(m
, "# ");
1239 return cd
->cache_show(m
, cd
, cp
);
1242 static const struct seq_operations cache_content_op
= {
1249 static int content_open(struct inode
*inode
, struct file
*file
)
1252 struct cache_detail
*cd
= PDE(inode
)->data
;
1254 han
= __seq_open_private(file
, &cache_content_op
, sizeof(*han
));
1262 static const struct file_operations content_file_operations
= {
1263 .open
= content_open
,
1265 .llseek
= seq_lseek
,
1266 .release
= seq_release_private
,
1269 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1270 size_t count
, loff_t
*ppos
)
1272 struct cache_detail
*cd
= PDE(file
->f_path
.dentry
->d_inode
)->data
;
1274 unsigned long p
= *ppos
;
1277 sprintf(tbuf
, "%lu\n", cd
->flush_time
);
1284 if (copy_to_user(buf
, (void*)(tbuf
+p
), len
))
1290 static ssize_t
write_flush(struct file
* file
, const char __user
* buf
,
1291 size_t count
, loff_t
*ppos
)
1293 struct cache_detail
*cd
= PDE(file
->f_path
.dentry
->d_inode
)->data
;
1297 if (*ppos
|| count
> sizeof(tbuf
)-1)
1299 if (copy_from_user(tbuf
, buf
, count
))
1302 flushtime
= simple_strtoul(tbuf
, &ep
, 0);
1303 if (*ep
&& *ep
!= '\n')
1306 cd
->flush_time
= flushtime
;
1307 cd
->nextcheck
= get_seconds();
1314 static const struct file_operations cache_flush_operations
= {
1315 .open
= nonseekable_open
,
1317 .write
= write_flush
,