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 <linux/smp_lock.h>
32 #include <asm/ioctls.h>
33 #include <linux/sunrpc/types.h>
34 #include <linux/sunrpc/cache.h>
35 #include <linux/sunrpc/stats.h>
36 #include <linux/sunrpc/rpc_pipe_fs.h>
38 #define RPCDBG_FACILITY RPCDBG_CACHE
40 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
);
41 static void cache_revisit_request(struct cache_head
*item
);
43 static void cache_init(struct cache_head
*h
)
45 time_t now
= seconds_since_boot();
49 h
->expiry_time
= now
+ CACHE_NEW_EXPIRY
;
50 h
->last_refresh
= now
;
53 static inline int cache_is_expired(struct cache_detail
*detail
, struct cache_head
*h
)
55 return (h
->expiry_time
< seconds_since_boot()) ||
56 (detail
->flush_time
> h
->last_refresh
);
59 struct cache_head
*sunrpc_cache_lookup(struct cache_detail
*detail
,
60 struct cache_head
*key
, int hash
)
62 struct cache_head
**head
, **hp
;
63 struct cache_head
*new = NULL
, *freeme
= NULL
;
65 head
= &detail
->hash_table
[hash
];
67 read_lock(&detail
->hash_lock
);
69 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
70 struct cache_head
*tmp
= *hp
;
71 if (detail
->match(tmp
, key
)) {
72 if (cache_is_expired(detail
, tmp
))
73 /* This entry is expired, we will discard it. */
76 read_unlock(&detail
->hash_lock
);
80 read_unlock(&detail
->hash_lock
);
81 /* Didn't find anything, insert an empty entry */
83 new = detail
->alloc();
86 /* must fully initialise 'new', else
87 * we might get lose if we need to
91 detail
->init(new, key
);
93 write_lock(&detail
->hash_lock
);
95 /* check if entry appeared while we slept */
96 for (hp
=head
; *hp
!= NULL
; hp
= &(*hp
)->next
) {
97 struct cache_head
*tmp
= *hp
;
98 if (detail
->match(tmp
, key
)) {
99 if (cache_is_expired(detail
, tmp
)) {
107 write_unlock(&detail
->hash_lock
);
108 cache_put(new, detail
);
116 write_unlock(&detail
->hash_lock
);
119 cache_put(freeme
, detail
);
122 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup
);
125 static void cache_dequeue(struct cache_detail
*detail
, struct cache_head
*ch
);
127 static void cache_fresh_locked(struct cache_head
*head
, time_t expiry
)
129 head
->expiry_time
= expiry
;
130 head
->last_refresh
= seconds_since_boot();
131 set_bit(CACHE_VALID
, &head
->flags
);
134 static void cache_fresh_unlocked(struct cache_head
*head
,
135 struct cache_detail
*detail
)
137 if (test_and_clear_bit(CACHE_PENDING
, &head
->flags
)) {
138 cache_revisit_request(head
);
139 cache_dequeue(detail
, head
);
143 struct cache_head
*sunrpc_cache_update(struct cache_detail
*detail
,
144 struct cache_head
*new, struct cache_head
*old
, int hash
)
146 /* The 'old' entry is to be replaced by 'new'.
147 * If 'old' is not VALID, we update it directly,
148 * otherwise we need to replace it
150 struct cache_head
**head
;
151 struct cache_head
*tmp
;
153 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
154 write_lock(&detail
->hash_lock
);
155 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
156 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
157 set_bit(CACHE_NEGATIVE
, &old
->flags
);
159 detail
->update(old
, new);
160 cache_fresh_locked(old
, new->expiry_time
);
161 write_unlock(&detail
->hash_lock
);
162 cache_fresh_unlocked(old
, detail
);
165 write_unlock(&detail
->hash_lock
);
167 /* We need to insert a new entry */
168 tmp
= detail
->alloc();
170 cache_put(old
, detail
);
174 detail
->init(tmp
, old
);
175 head
= &detail
->hash_table
[hash
];
177 write_lock(&detail
->hash_lock
);
178 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
179 set_bit(CACHE_NEGATIVE
, &tmp
->flags
);
181 detail
->update(tmp
, new);
186 cache_fresh_locked(tmp
, new->expiry_time
);
187 cache_fresh_locked(old
, 0);
188 write_unlock(&detail
->hash_lock
);
189 cache_fresh_unlocked(tmp
, detail
);
190 cache_fresh_unlocked(old
, detail
);
191 cache_put(old
, detail
);
194 EXPORT_SYMBOL_GPL(sunrpc_cache_update
);
196 static int cache_make_upcall(struct cache_detail
*cd
, struct cache_head
*h
)
198 if (!cd
->cache_upcall
)
200 return cd
->cache_upcall(cd
, h
);
203 static inline int cache_is_valid(struct cache_detail
*detail
, struct cache_head
*h
)
205 if (!test_bit(CACHE_VALID
, &h
->flags
))
209 if (test_bit(CACHE_NEGATIVE
, &h
->flags
))
217 * This is the generic cache management routine for all
218 * the authentication caches.
219 * It checks the currency of a cache item and will (later)
220 * initiate an upcall to fill it if needed.
223 * Returns 0 if the cache_head can be used, or cache_puts it and returns
224 * -EAGAIN if upcall is pending and request has been queued
225 * -ETIMEDOUT if upcall failed or request could not be queue or
226 * upcall completed but item is still invalid (implying that
227 * the cache item has been replaced with a newer one).
228 * -ENOENT if cache entry was negative
230 int cache_check(struct cache_detail
*detail
,
231 struct cache_head
*h
, struct cache_req
*rqstp
)
234 long refresh_age
, age
;
236 /* First decide return status as best we can */
237 rv
= cache_is_valid(detail
, h
);
239 /* now see if we want to start an upcall */
240 refresh_age
= (h
->expiry_time
- h
->last_refresh
);
241 age
= seconds_since_boot() - h
->last_refresh
;
246 } else if (rv
== -EAGAIN
|| age
> refresh_age
/2) {
247 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
249 if (!test_and_set_bit(CACHE_PENDING
, &h
->flags
)) {
250 switch (cache_make_upcall(detail
, h
)) {
252 clear_bit(CACHE_PENDING
, &h
->flags
);
253 cache_revisit_request(h
);
255 set_bit(CACHE_NEGATIVE
, &h
->flags
);
256 cache_fresh_locked(h
, seconds_since_boot()+CACHE_NEW_EXPIRY
);
257 cache_fresh_unlocked(h
, detail
);
263 clear_bit(CACHE_PENDING
, &h
->flags
);
264 cache_revisit_request(h
);
271 if (cache_defer_req(rqstp
, h
) < 0) {
272 /* Request is not deferred */
273 rv
= cache_is_valid(detail
, h
);
279 cache_put(h
, detail
);
282 EXPORT_SYMBOL_GPL(cache_check
);
285 * caches need to be periodically cleaned.
286 * For this we maintain a list of cache_detail and
287 * a current pointer into that list and into the table
290 * Each time clean_cache is called it finds the next non-empty entry
291 * in the current table and walks the list in that entry
292 * looking for entries that can be removed.
294 * An entry gets removed if:
295 * - The expiry is before current time
296 * - The last_refresh time is before the flush_time for that cache
298 * later we might drop old entries with non-NEVER expiry if that table
299 * is getting 'full' for some definition of 'full'
301 * The question of "how often to scan a table" is an interesting one
302 * and is answered in part by the use of the "nextcheck" field in the
304 * When a scan of a table begins, the nextcheck field is set to a time
305 * that is well into the future.
306 * While scanning, if an expiry time is found that is earlier than the
307 * current nextcheck time, nextcheck is set to that expiry time.
308 * If the flush_time is ever set to a time earlier than the nextcheck
309 * time, the nextcheck time is then set to that flush_time.
311 * A table is then only scanned if the current time is at least
312 * the nextcheck time.
316 static LIST_HEAD(cache_list
);
317 static DEFINE_SPINLOCK(cache_list_lock
);
318 static struct cache_detail
*current_detail
;
319 static int current_index
;
321 static void do_cache_clean(struct work_struct
*work
);
322 static struct delayed_work cache_cleaner
;
324 static void sunrpc_init_cache_detail(struct cache_detail
*cd
)
326 rwlock_init(&cd
->hash_lock
);
327 INIT_LIST_HEAD(&cd
->queue
);
328 spin_lock(&cache_list_lock
);
331 atomic_set(&cd
->readers
, 0);
334 list_add(&cd
->others
, &cache_list
);
335 spin_unlock(&cache_list_lock
);
337 /* start the cleaning process */
338 schedule_delayed_work(&cache_cleaner
, 0);
341 static void sunrpc_destroy_cache_detail(struct cache_detail
*cd
)
344 spin_lock(&cache_list_lock
);
345 write_lock(&cd
->hash_lock
);
346 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
347 write_unlock(&cd
->hash_lock
);
348 spin_unlock(&cache_list_lock
);
351 if (current_detail
== cd
)
352 current_detail
= NULL
;
353 list_del_init(&cd
->others
);
354 write_unlock(&cd
->hash_lock
);
355 spin_unlock(&cache_list_lock
);
356 if (list_empty(&cache_list
)) {
357 /* module must be being unloaded so its safe to kill the worker */
358 cancel_delayed_work_sync(&cache_cleaner
);
362 printk(KERN_ERR
"nfsd: failed to unregister %s cache\n", cd
->name
);
365 /* clean cache tries to find something to clean
367 * It returns 1 if it cleaned something,
368 * 0 if it didn't find anything this time
369 * -1 if it fell off the end of the list.
371 static int cache_clean(void)
374 struct list_head
*next
;
376 spin_lock(&cache_list_lock
);
378 /* find a suitable table if we don't already have one */
379 while (current_detail
== NULL
||
380 current_index
>= current_detail
->hash_size
) {
382 next
= current_detail
->others
.next
;
384 next
= cache_list
.next
;
385 if (next
== &cache_list
) {
386 current_detail
= NULL
;
387 spin_unlock(&cache_list_lock
);
390 current_detail
= list_entry(next
, struct cache_detail
, others
);
391 if (current_detail
->nextcheck
> seconds_since_boot())
392 current_index
= current_detail
->hash_size
;
395 current_detail
->nextcheck
= seconds_since_boot()+30*60;
399 /* find a non-empty bucket in the table */
400 while (current_detail
&&
401 current_index
< current_detail
->hash_size
&&
402 current_detail
->hash_table
[current_index
] == NULL
)
405 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
407 if (current_detail
&& current_index
< current_detail
->hash_size
) {
408 struct cache_head
*ch
, **cp
;
409 struct cache_detail
*d
;
411 write_lock(¤t_detail
->hash_lock
);
413 /* Ok, now to clean this strand */
415 cp
= & current_detail
->hash_table
[current_index
];
416 for (ch
= *cp
; ch
; cp
= & ch
->next
, ch
= *cp
) {
417 if (current_detail
->nextcheck
> ch
->expiry_time
)
418 current_detail
->nextcheck
= ch
->expiry_time
+1;
419 if (!cache_is_expired(current_detail
, ch
))
424 current_detail
->entries
--;
429 write_unlock(¤t_detail
->hash_lock
);
433 spin_unlock(&cache_list_lock
);
435 if (test_and_clear_bit(CACHE_PENDING
, &ch
->flags
))
436 cache_dequeue(current_detail
, ch
);
437 cache_revisit_request(ch
);
441 spin_unlock(&cache_list_lock
);
447 * We want to regularly clean the cache, so we need to schedule some work ...
449 static void do_cache_clean(struct work_struct
*work
)
452 if (cache_clean() == -1)
453 delay
= round_jiffies_relative(30*HZ
);
455 if (list_empty(&cache_list
))
459 schedule_delayed_work(&cache_cleaner
, delay
);
464 * Clean all caches promptly. This just calls cache_clean
465 * repeatedly until we are sure that every cache has had a chance to
468 void cache_flush(void)
470 while (cache_clean() != -1)
472 while (cache_clean() != -1)
475 EXPORT_SYMBOL_GPL(cache_flush
);
477 void cache_purge(struct cache_detail
*detail
)
479 detail
->flush_time
= LONG_MAX
;
480 detail
->nextcheck
= seconds_since_boot();
482 detail
->flush_time
= 1;
484 EXPORT_SYMBOL_GPL(cache_purge
);
488 * Deferral and Revisiting of Requests.
490 * If a cache lookup finds a pending entry, we
491 * need to defer the request and revisit it later.
492 * All deferred requests are stored in a hash table,
493 * indexed by "struct cache_head *".
494 * As it may be wasteful to store a whole request
495 * structure, we allow the request to provide a
496 * deferred form, which must contain a
497 * 'struct cache_deferred_req'
498 * This cache_deferred_req contains a method to allow
499 * it to be revisited when cache info is available
502 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
503 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
505 #define DFR_MAX 300 /* ??? */
507 static DEFINE_SPINLOCK(cache_defer_lock
);
508 static LIST_HEAD(cache_defer_list
);
509 static struct hlist_head cache_defer_hash
[DFR_HASHSIZE
];
510 static int cache_defer_cnt
;
512 static void __unhash_deferred_req(struct cache_deferred_req
*dreq
)
514 list_del_init(&dreq
->recent
);
515 hlist_del_init(&dreq
->hash
);
519 static void __hash_deferred_req(struct cache_deferred_req
*dreq
, struct cache_head
*item
)
521 int hash
= DFR_HASH(item
);
523 list_add(&dreq
->recent
, &cache_defer_list
);
524 hlist_add_head(&dreq
->hash
, &cache_defer_hash
[hash
]);
527 static int setup_deferral(struct cache_deferred_req
*dreq
, struct cache_head
*item
)
529 struct cache_deferred_req
*discard
;
533 spin_lock(&cache_defer_lock
);
535 __hash_deferred_req(dreq
, item
);
537 /* it is in, now maybe clean up */
539 if (++cache_defer_cnt
> DFR_MAX
) {
540 discard
= list_entry(cache_defer_list
.prev
,
541 struct cache_deferred_req
, recent
);
542 __unhash_deferred_req(discard
);
544 spin_unlock(&cache_defer_lock
);
547 /* there was one too many */
548 discard
->revisit(discard
, 1);
550 if (!test_bit(CACHE_PENDING
, &item
->flags
)) {
551 /* must have just been validated... */
552 cache_revisit_request(item
);
558 struct thread_deferred_req
{
559 struct cache_deferred_req handle
;
560 struct completion completion
;
563 static void cache_restart_thread(struct cache_deferred_req
*dreq
, int too_many
)
565 struct thread_deferred_req
*dr
=
566 container_of(dreq
, struct thread_deferred_req
, handle
);
567 complete(&dr
->completion
);
570 static int cache_wait_req(struct cache_req
*req
, struct cache_head
*item
)
572 struct thread_deferred_req sleeper
;
573 struct cache_deferred_req
*dreq
= &sleeper
.handle
;
576 sleeper
.completion
= COMPLETION_INITIALIZER_ONSTACK(sleeper
.completion
);
577 dreq
->revisit
= cache_restart_thread
;
579 ret
= setup_deferral(dreq
, item
);
583 if (wait_for_completion_interruptible_timeout(
584 &sleeper
.completion
, req
->thread_wait
) <= 0) {
585 /* The completion wasn't completed, so we need
588 spin_lock(&cache_defer_lock
);
589 if (!hlist_unhashed(&sleeper
.handle
.hash
)) {
590 __unhash_deferred_req(&sleeper
.handle
);
591 spin_unlock(&cache_defer_lock
);
593 /* cache_revisit_request already removed
594 * this from the hash table, but hasn't
595 * called ->revisit yet. It will very soon
596 * and we need to wait for it.
598 spin_unlock(&cache_defer_lock
);
599 wait_for_completion(&sleeper
.completion
);
602 if (test_bit(CACHE_PENDING
, &item
->flags
)) {
603 /* item is still pending, try request
608 /* only return success if we actually deferred the
609 * request. In this case we waited until it was
610 * answered so no deferral has happened - rather
611 * an answer already exists.
616 static int cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
618 struct cache_deferred_req
*dreq
;
621 if (cache_defer_cnt
>= DFR_MAX
) {
622 /* too much in the cache, randomly drop this one,
623 * or continue and drop the oldest
628 if (req
->thread_wait
) {
629 ret
= cache_wait_req(req
, item
);
630 if (ret
!= -ETIMEDOUT
)
633 dreq
= req
->defer(req
);
636 return setup_deferral(dreq
, item
);
639 static void cache_revisit_request(struct cache_head
*item
)
641 struct cache_deferred_req
*dreq
;
642 struct list_head pending
;
643 struct hlist_node
*lp
, *tmp
;
644 int hash
= DFR_HASH(item
);
646 INIT_LIST_HEAD(&pending
);
647 spin_lock(&cache_defer_lock
);
649 hlist_for_each_entry_safe(dreq
, lp
, tmp
, &cache_defer_hash
[hash
], hash
)
650 if (dreq
->item
== item
) {
651 __unhash_deferred_req(dreq
);
652 list_add(&dreq
->recent
, &pending
);
655 spin_unlock(&cache_defer_lock
);
657 while (!list_empty(&pending
)) {
658 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
659 list_del_init(&dreq
->recent
);
660 dreq
->revisit(dreq
, 0);
664 void cache_clean_deferred(void *owner
)
666 struct cache_deferred_req
*dreq
, *tmp
;
667 struct list_head pending
;
670 INIT_LIST_HEAD(&pending
);
671 spin_lock(&cache_defer_lock
);
673 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
674 if (dreq
->owner
== owner
)
675 __unhash_deferred_req(dreq
);
677 spin_unlock(&cache_defer_lock
);
679 while (!list_empty(&pending
)) {
680 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
681 list_del_init(&dreq
->recent
);
682 dreq
->revisit(dreq
, 1);
687 * communicate with user-space
689 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
690 * On read, you get a full request, or block.
691 * On write, an update request is processed.
692 * Poll works if anything to read, and always allows write.
694 * Implemented by linked list of requests. Each open file has
695 * a ->private that also exists in this list. New requests are added
696 * to the end and may wakeup and preceding readers.
697 * New readers are added to the head. If, on read, an item is found with
698 * CACHE_UPCALLING clear, we free it from the list.
702 static DEFINE_SPINLOCK(queue_lock
);
703 static DEFINE_MUTEX(queue_io_mutex
);
706 struct list_head list
;
707 int reader
; /* if 0, then request */
709 struct cache_request
{
710 struct cache_queue q
;
711 struct cache_head
*item
;
716 struct cache_reader
{
717 struct cache_queue q
;
718 int offset
; /* if non-0, we have a refcnt on next request */
721 static ssize_t
cache_read(struct file
*filp
, char __user
*buf
, size_t count
,
722 loff_t
*ppos
, struct cache_detail
*cd
)
724 struct cache_reader
*rp
= filp
->private_data
;
725 struct cache_request
*rq
;
726 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
732 mutex_lock(&inode
->i_mutex
); /* protect against multiple concurrent
733 * readers on this file */
735 spin_lock(&queue_lock
);
736 /* need to find next request */
737 while (rp
->q
.list
.next
!= &cd
->queue
&&
738 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
740 struct list_head
*next
= rp
->q
.list
.next
;
741 list_move(&rp
->q
.list
, next
);
743 if (rp
->q
.list
.next
== &cd
->queue
) {
744 spin_unlock(&queue_lock
);
745 mutex_unlock(&inode
->i_mutex
);
749 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
750 BUG_ON(rq
->q
.reader
);
753 spin_unlock(&queue_lock
);
755 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
757 spin_lock(&queue_lock
);
758 list_move(&rp
->q
.list
, &rq
->q
.list
);
759 spin_unlock(&queue_lock
);
761 if (rp
->offset
+ count
> rq
->len
)
762 count
= rq
->len
- rp
->offset
;
764 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
767 if (rp
->offset
>= rq
->len
) {
769 spin_lock(&queue_lock
);
770 list_move(&rp
->q
.list
, &rq
->q
.list
);
771 spin_unlock(&queue_lock
);
776 if (rp
->offset
== 0) {
777 /* need to release rq */
778 spin_lock(&queue_lock
);
780 if (rq
->readers
== 0 &&
781 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
782 list_del(&rq
->q
.list
);
783 spin_unlock(&queue_lock
);
784 cache_put(rq
->item
, cd
);
788 spin_unlock(&queue_lock
);
792 mutex_unlock(&inode
->i_mutex
);
793 return err
? err
: count
;
796 static ssize_t
cache_do_downcall(char *kaddr
, const char __user
*buf
,
797 size_t count
, struct cache_detail
*cd
)
801 if (copy_from_user(kaddr
, buf
, count
))
804 ret
= cd
->cache_parse(cd
, kaddr
, count
);
810 static ssize_t
cache_slow_downcall(const char __user
*buf
,
811 size_t count
, struct cache_detail
*cd
)
813 static char write_buf
[8192]; /* protected by queue_io_mutex */
814 ssize_t ret
= -EINVAL
;
816 if (count
>= sizeof(write_buf
))
818 mutex_lock(&queue_io_mutex
);
819 ret
= cache_do_downcall(write_buf
, buf
, count
, cd
);
820 mutex_unlock(&queue_io_mutex
);
825 static ssize_t
cache_downcall(struct address_space
*mapping
,
826 const char __user
*buf
,
827 size_t count
, struct cache_detail
*cd
)
831 ssize_t ret
= -ENOMEM
;
833 if (count
>= PAGE_CACHE_SIZE
)
836 page
= find_or_create_page(mapping
, 0, GFP_KERNEL
);
841 ret
= cache_do_downcall(kaddr
, buf
, count
, cd
);
844 page_cache_release(page
);
847 return cache_slow_downcall(buf
, count
, cd
);
850 static ssize_t
cache_write(struct file
*filp
, const char __user
*buf
,
851 size_t count
, loff_t
*ppos
,
852 struct cache_detail
*cd
)
854 struct address_space
*mapping
= filp
->f_mapping
;
855 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
856 ssize_t ret
= -EINVAL
;
858 if (!cd
->cache_parse
)
861 mutex_lock(&inode
->i_mutex
);
862 ret
= cache_downcall(mapping
, buf
, count
, cd
);
863 mutex_unlock(&inode
->i_mutex
);
868 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
870 static unsigned int cache_poll(struct file
*filp
, poll_table
*wait
,
871 struct cache_detail
*cd
)
874 struct cache_reader
*rp
= filp
->private_data
;
875 struct cache_queue
*cq
;
877 poll_wait(filp
, &queue_wait
, wait
);
879 /* alway allow write */
880 mask
= POLL_OUT
| POLLWRNORM
;
885 spin_lock(&queue_lock
);
887 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
888 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
890 mask
|= POLLIN
| POLLRDNORM
;
893 spin_unlock(&queue_lock
);
897 static int cache_ioctl(struct inode
*ino
, struct file
*filp
,
898 unsigned int cmd
, unsigned long arg
,
899 struct cache_detail
*cd
)
902 struct cache_reader
*rp
= filp
->private_data
;
903 struct cache_queue
*cq
;
905 if (cmd
!= FIONREAD
|| !rp
)
908 spin_lock(&queue_lock
);
910 /* only find the length remaining in current request,
911 * or the length of the next request
913 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
914 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
916 struct cache_request
*cr
=
917 container_of(cq
, struct cache_request
, q
);
918 len
= cr
->len
- rp
->offset
;
921 spin_unlock(&queue_lock
);
923 return put_user(len
, (int __user
*)arg
);
926 static int cache_open(struct inode
*inode
, struct file
*filp
,
927 struct cache_detail
*cd
)
929 struct cache_reader
*rp
= NULL
;
931 if (!cd
|| !try_module_get(cd
->owner
))
933 nonseekable_open(inode
, filp
);
934 if (filp
->f_mode
& FMODE_READ
) {
935 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
940 atomic_inc(&cd
->readers
);
941 spin_lock(&queue_lock
);
942 list_add(&rp
->q
.list
, &cd
->queue
);
943 spin_unlock(&queue_lock
);
945 filp
->private_data
= rp
;
949 static int cache_release(struct inode
*inode
, struct file
*filp
,
950 struct cache_detail
*cd
)
952 struct cache_reader
*rp
= filp
->private_data
;
955 spin_lock(&queue_lock
);
957 struct cache_queue
*cq
;
958 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
959 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
961 container_of(cq
, struct cache_request
, q
)
967 list_del(&rp
->q
.list
);
968 spin_unlock(&queue_lock
);
970 filp
->private_data
= NULL
;
973 cd
->last_close
= seconds_since_boot();
974 atomic_dec(&cd
->readers
);
976 module_put(cd
->owner
);
982 static void cache_dequeue(struct cache_detail
*detail
, struct cache_head
*ch
)
984 struct cache_queue
*cq
;
985 spin_lock(&queue_lock
);
986 list_for_each_entry(cq
, &detail
->queue
, list
)
988 struct cache_request
*cr
= container_of(cq
, struct cache_request
, q
);
991 if (cr
->readers
!= 0)
993 list_del(&cr
->q
.list
);
994 spin_unlock(&queue_lock
);
995 cache_put(cr
->item
, detail
);
1000 spin_unlock(&queue_lock
);
1004 * Support routines for text-based upcalls.
1005 * Fields are separated by spaces.
1006 * Fields are either mangled to quote space tab newline slosh with slosh
1007 * or a hexified with a leading \x
1008 * Record is terminated with newline.
1012 void qword_add(char **bpp
, int *lp
, char *str
)
1018 if (len
< 0) return;
1020 while ((c
=*str
++) && len
)
1028 *bp
++ = '0' + ((c
& 0300)>>6);
1029 *bp
++ = '0' + ((c
& 0070)>>3);
1030 *bp
++ = '0' + ((c
& 0007)>>0);
1038 if (c
|| len
<1) len
= -1;
1046 EXPORT_SYMBOL_GPL(qword_add
);
1048 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
1053 if (len
< 0) return;
1059 while (blen
&& len
>= 2) {
1060 unsigned char c
= *buf
++;
1061 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
1062 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
1067 if (blen
|| len
<1) len
= -1;
1075 EXPORT_SYMBOL_GPL(qword_addhex
);
1077 static void warn_no_listener(struct cache_detail
*detail
)
1079 if (detail
->last_warn
!= detail
->last_close
) {
1080 detail
->last_warn
= detail
->last_close
;
1081 if (detail
->warn_no_listener
)
1082 detail
->warn_no_listener(detail
, detail
->last_close
!= 0);
1086 static bool cache_listeners_exist(struct cache_detail
*detail
)
1088 if (atomic_read(&detail
->readers
))
1090 if (detail
->last_close
== 0)
1091 /* This cache was never opened */
1093 if (detail
->last_close
< seconds_since_boot() - 30)
1095 * We allow for the possibility that someone might
1096 * restart a userspace daemon without restarting the
1097 * server; but after 30 seconds, we give up.
1104 * register an upcall request to user-space and queue it up for read() by the
1107 * Each request is at most one page long.
1109 int sunrpc_cache_pipe_upcall(struct cache_detail
*detail
, struct cache_head
*h
,
1110 void (*cache_request
)(struct cache_detail
*,
1111 struct cache_head
*,
1117 struct cache_request
*crq
;
1121 if (!cache_listeners_exist(detail
)) {
1122 warn_no_listener(detail
);
1126 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1130 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1136 bp
= buf
; len
= PAGE_SIZE
;
1138 cache_request(detail
, h
, &bp
, &len
);
1146 crq
->item
= cache_get(h
);
1148 crq
->len
= PAGE_SIZE
- len
;
1150 spin_lock(&queue_lock
);
1151 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1152 spin_unlock(&queue_lock
);
1153 wake_up(&queue_wait
);
1156 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall
);
1159 * parse a message from user-space and pass it
1160 * to an appropriate cache
1161 * Messages are, like requests, separated into fields by
1162 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1165 * reply cachename expiry key ... content....
1167 * key and content are both parsed by cache
1170 #define isodigit(c) (isdigit(c) && c <= '7')
1171 int qword_get(char **bpp
, char *dest
, int bufsize
)
1173 /* return bytes copied, or -1 on error */
1177 while (*bp
== ' ') bp
++;
1179 if (bp
[0] == '\\' && bp
[1] == 'x') {
1182 while (len
< bufsize
) {
1185 h
= hex_to_bin(bp
[0]);
1189 l
= hex_to_bin(bp
[1]);
1193 *dest
++ = (h
<< 4) | l
;
1198 /* text with \nnn octal quoting */
1199 while (*bp
!= ' ' && *bp
!= '\n' && *bp
&& len
< bufsize
-1) {
1201 isodigit(bp
[1]) && (bp
[1] <= '3') &&
1204 int byte
= (*++bp
-'0');
1206 byte
= (byte
<< 3) | (*bp
++ - '0');
1207 byte
= (byte
<< 3) | (*bp
++ - '0');
1217 if (*bp
!= ' ' && *bp
!= '\n' && *bp
!= '\0')
1219 while (*bp
== ' ') bp
++;
1224 EXPORT_SYMBOL_GPL(qword_get
);
1228 * support /proc/sunrpc/cache/$CACHENAME/content
1230 * We call ->cache_show passing NULL for the item to
1231 * get a header, then pass each real item in the cache
1235 struct cache_detail
*cd
;
1238 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1239 __acquires(cd
->hash_lock
)
1242 unsigned hash
, entry
;
1243 struct cache_head
*ch
;
1244 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1247 read_lock(&cd
->hash_lock
);
1249 return SEQ_START_TOKEN
;
1251 entry
= n
& ((1LL<<32) - 1);
1253 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1256 n
&= ~((1LL<<32) - 1);
1260 } while(hash
< cd
->hash_size
&&
1261 cd
->hash_table
[hash
]==NULL
);
1262 if (hash
>= cd
->hash_size
)
1265 return cd
->hash_table
[hash
];
1268 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1270 struct cache_head
*ch
= p
;
1271 int hash
= (*pos
>> 32);
1272 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1274 if (p
== SEQ_START_TOKEN
)
1276 else if (ch
->next
== NULL
) {
1283 *pos
&= ~((1LL<<32) - 1);
1284 while (hash
< cd
->hash_size
&&
1285 cd
->hash_table
[hash
] == NULL
) {
1289 if (hash
>= cd
->hash_size
)
1292 return cd
->hash_table
[hash
];
1295 static void c_stop(struct seq_file
*m
, void *p
)
1296 __releases(cd
->hash_lock
)
1298 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1299 read_unlock(&cd
->hash_lock
);
1302 static int c_show(struct seq_file
*m
, void *p
)
1304 struct cache_head
*cp
= p
;
1305 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1307 if (p
== SEQ_START_TOKEN
)
1308 return cd
->cache_show(m
, cd
, NULL
);
1311 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1312 convert_to_wallclock(cp
->expiry_time
),
1313 atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1315 if (cache_check(cd
, cp
, NULL
))
1316 /* cache_check does a cache_put on failure */
1317 seq_printf(m
, "# ");
1321 return cd
->cache_show(m
, cd
, cp
);
1324 static const struct seq_operations cache_content_op
= {
1331 static int content_open(struct inode
*inode
, struct file
*file
,
1332 struct cache_detail
*cd
)
1336 if (!cd
|| !try_module_get(cd
->owner
))
1338 han
= __seq_open_private(file
, &cache_content_op
, sizeof(*han
));
1340 module_put(cd
->owner
);
1348 static int content_release(struct inode
*inode
, struct file
*file
,
1349 struct cache_detail
*cd
)
1351 int ret
= seq_release_private(inode
, file
);
1352 module_put(cd
->owner
);
1356 static int open_flush(struct inode
*inode
, struct file
*file
,
1357 struct cache_detail
*cd
)
1359 if (!cd
|| !try_module_get(cd
->owner
))
1361 return nonseekable_open(inode
, file
);
1364 static int release_flush(struct inode
*inode
, struct file
*file
,
1365 struct cache_detail
*cd
)
1367 module_put(cd
->owner
);
1371 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1372 size_t count
, loff_t
*ppos
,
1373 struct cache_detail
*cd
)
1376 unsigned long p
= *ppos
;
1379 sprintf(tbuf
, "%lu\n", convert_to_wallclock(cd
->flush_time
));
1386 if (copy_to_user(buf
, (void*)(tbuf
+p
), len
))
1392 static ssize_t
write_flush(struct file
*file
, const char __user
*buf
,
1393 size_t count
, loff_t
*ppos
,
1394 struct cache_detail
*cd
)
1399 if (*ppos
|| count
> sizeof(tbuf
)-1)
1401 if (copy_from_user(tbuf
, buf
, count
))
1404 simple_strtoul(tbuf
, &ep
, 0);
1405 if (*ep
&& *ep
!= '\n')
1409 cd
->flush_time
= get_expiry(&bp
);
1410 cd
->nextcheck
= seconds_since_boot();
1417 static ssize_t
cache_read_procfs(struct file
*filp
, char __user
*buf
,
1418 size_t count
, loff_t
*ppos
)
1420 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1422 return cache_read(filp
, buf
, count
, ppos
, cd
);
1425 static ssize_t
cache_write_procfs(struct file
*filp
, const char __user
*buf
,
1426 size_t count
, loff_t
*ppos
)
1428 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1430 return cache_write(filp
, buf
, count
, ppos
, cd
);
1433 static unsigned int cache_poll_procfs(struct file
*filp
, poll_table
*wait
)
1435 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1437 return cache_poll(filp
, wait
, cd
);
1440 static long cache_ioctl_procfs(struct file
*filp
,
1441 unsigned int cmd
, unsigned long arg
)
1444 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1445 struct cache_detail
*cd
= PDE(inode
)->data
;
1448 ret
= cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1454 static int cache_open_procfs(struct inode
*inode
, struct file
*filp
)
1456 struct cache_detail
*cd
= PDE(inode
)->data
;
1458 return cache_open(inode
, filp
, cd
);
1461 static int cache_release_procfs(struct inode
*inode
, struct file
*filp
)
1463 struct cache_detail
*cd
= PDE(inode
)->data
;
1465 return cache_release(inode
, filp
, cd
);
1468 static const struct file_operations cache_file_operations_procfs
= {
1469 .owner
= THIS_MODULE
,
1470 .llseek
= no_llseek
,
1471 .read
= cache_read_procfs
,
1472 .write
= cache_write_procfs
,
1473 .poll
= cache_poll_procfs
,
1474 .unlocked_ioctl
= cache_ioctl_procfs
, /* for FIONREAD */
1475 .open
= cache_open_procfs
,
1476 .release
= cache_release_procfs
,
1479 static int content_open_procfs(struct inode
*inode
, struct file
*filp
)
1481 struct cache_detail
*cd
= PDE(inode
)->data
;
1483 return content_open(inode
, filp
, cd
);
1486 static int content_release_procfs(struct inode
*inode
, struct file
*filp
)
1488 struct cache_detail
*cd
= PDE(inode
)->data
;
1490 return content_release(inode
, filp
, cd
);
1493 static const struct file_operations content_file_operations_procfs
= {
1494 .open
= content_open_procfs
,
1496 .llseek
= seq_lseek
,
1497 .release
= content_release_procfs
,
1500 static int open_flush_procfs(struct inode
*inode
, struct file
*filp
)
1502 struct cache_detail
*cd
= PDE(inode
)->data
;
1504 return open_flush(inode
, filp
, cd
);
1507 static int release_flush_procfs(struct inode
*inode
, struct file
*filp
)
1509 struct cache_detail
*cd
= PDE(inode
)->data
;
1511 return release_flush(inode
, filp
, cd
);
1514 static ssize_t
read_flush_procfs(struct file
*filp
, char __user
*buf
,
1515 size_t count
, loff_t
*ppos
)
1517 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1519 return read_flush(filp
, buf
, count
, ppos
, cd
);
1522 static ssize_t
write_flush_procfs(struct file
*filp
,
1523 const char __user
*buf
,
1524 size_t count
, loff_t
*ppos
)
1526 struct cache_detail
*cd
= PDE(filp
->f_path
.dentry
->d_inode
)->data
;
1528 return write_flush(filp
, buf
, count
, ppos
, cd
);
1531 static const struct file_operations cache_flush_operations_procfs
= {
1532 .open
= open_flush_procfs
,
1533 .read
= read_flush_procfs
,
1534 .write
= write_flush_procfs
,
1535 .release
= release_flush_procfs
,
1538 static void remove_cache_proc_entries(struct cache_detail
*cd
)
1540 if (cd
->u
.procfs
.proc_ent
== NULL
)
1542 if (cd
->u
.procfs
.flush_ent
)
1543 remove_proc_entry("flush", cd
->u
.procfs
.proc_ent
);
1544 if (cd
->u
.procfs
.channel_ent
)
1545 remove_proc_entry("channel", cd
->u
.procfs
.proc_ent
);
1546 if (cd
->u
.procfs
.content_ent
)
1547 remove_proc_entry("content", cd
->u
.procfs
.proc_ent
);
1548 cd
->u
.procfs
.proc_ent
= NULL
;
1549 remove_proc_entry(cd
->name
, proc_net_rpc
);
1552 #ifdef CONFIG_PROC_FS
1553 static int create_cache_proc_entries(struct cache_detail
*cd
)
1555 struct proc_dir_entry
*p
;
1557 cd
->u
.procfs
.proc_ent
= proc_mkdir(cd
->name
, proc_net_rpc
);
1558 if (cd
->u
.procfs
.proc_ent
== NULL
)
1560 cd
->u
.procfs
.channel_ent
= NULL
;
1561 cd
->u
.procfs
.content_ent
= NULL
;
1563 p
= proc_create_data("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
1564 cd
->u
.procfs
.proc_ent
,
1565 &cache_flush_operations_procfs
, cd
);
1566 cd
->u
.procfs
.flush_ent
= p
;
1570 if (cd
->cache_upcall
|| cd
->cache_parse
) {
1571 p
= proc_create_data("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
1572 cd
->u
.procfs
.proc_ent
,
1573 &cache_file_operations_procfs
, cd
);
1574 cd
->u
.procfs
.channel_ent
= p
;
1578 if (cd
->cache_show
) {
1579 p
= proc_create_data("content", S_IFREG
|S_IRUSR
|S_IWUSR
,
1580 cd
->u
.procfs
.proc_ent
,
1581 &content_file_operations_procfs
, cd
);
1582 cd
->u
.procfs
.content_ent
= p
;
1588 remove_cache_proc_entries(cd
);
1591 #else /* CONFIG_PROC_FS */
1592 static int create_cache_proc_entries(struct cache_detail
*cd
)
1598 void __init
cache_initialize(void)
1600 INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner
, do_cache_clean
);
1603 int cache_register(struct cache_detail
*cd
)
1607 sunrpc_init_cache_detail(cd
);
1608 ret
= create_cache_proc_entries(cd
);
1610 sunrpc_destroy_cache_detail(cd
);
1613 EXPORT_SYMBOL_GPL(cache_register
);
1615 void cache_unregister(struct cache_detail
*cd
)
1617 remove_cache_proc_entries(cd
);
1618 sunrpc_destroy_cache_detail(cd
);
1620 EXPORT_SYMBOL_GPL(cache_unregister
);
1622 static ssize_t
cache_read_pipefs(struct file
*filp
, char __user
*buf
,
1623 size_t count
, loff_t
*ppos
)
1625 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1627 return cache_read(filp
, buf
, count
, ppos
, cd
);
1630 static ssize_t
cache_write_pipefs(struct file
*filp
, const char __user
*buf
,
1631 size_t count
, loff_t
*ppos
)
1633 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1635 return cache_write(filp
, buf
, count
, ppos
, cd
);
1638 static unsigned int cache_poll_pipefs(struct file
*filp
, poll_table
*wait
)
1640 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1642 return cache_poll(filp
, wait
, cd
);
1645 static long cache_ioctl_pipefs(struct file
*filp
,
1646 unsigned int cmd
, unsigned long arg
)
1648 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1649 struct cache_detail
*cd
= RPC_I(inode
)->private;
1653 ret
= cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1659 static int cache_open_pipefs(struct inode
*inode
, struct file
*filp
)
1661 struct cache_detail
*cd
= RPC_I(inode
)->private;
1663 return cache_open(inode
, filp
, cd
);
1666 static int cache_release_pipefs(struct inode
*inode
, struct file
*filp
)
1668 struct cache_detail
*cd
= RPC_I(inode
)->private;
1670 return cache_release(inode
, filp
, cd
);
1673 const struct file_operations cache_file_operations_pipefs
= {
1674 .owner
= THIS_MODULE
,
1675 .llseek
= no_llseek
,
1676 .read
= cache_read_pipefs
,
1677 .write
= cache_write_pipefs
,
1678 .poll
= cache_poll_pipefs
,
1679 .unlocked_ioctl
= cache_ioctl_pipefs
, /* for FIONREAD */
1680 .open
= cache_open_pipefs
,
1681 .release
= cache_release_pipefs
,
1684 static int content_open_pipefs(struct inode
*inode
, struct file
*filp
)
1686 struct cache_detail
*cd
= RPC_I(inode
)->private;
1688 return content_open(inode
, filp
, cd
);
1691 static int content_release_pipefs(struct inode
*inode
, struct file
*filp
)
1693 struct cache_detail
*cd
= RPC_I(inode
)->private;
1695 return content_release(inode
, filp
, cd
);
1698 const struct file_operations content_file_operations_pipefs
= {
1699 .open
= content_open_pipefs
,
1701 .llseek
= seq_lseek
,
1702 .release
= content_release_pipefs
,
1705 static int open_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1707 struct cache_detail
*cd
= RPC_I(inode
)->private;
1709 return open_flush(inode
, filp
, cd
);
1712 static int release_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1714 struct cache_detail
*cd
= RPC_I(inode
)->private;
1716 return release_flush(inode
, filp
, cd
);
1719 static ssize_t
read_flush_pipefs(struct file
*filp
, char __user
*buf
,
1720 size_t count
, loff_t
*ppos
)
1722 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1724 return read_flush(filp
, buf
, count
, ppos
, cd
);
1727 static ssize_t
write_flush_pipefs(struct file
*filp
,
1728 const char __user
*buf
,
1729 size_t count
, loff_t
*ppos
)
1731 struct cache_detail
*cd
= RPC_I(filp
->f_path
.dentry
->d_inode
)->private;
1733 return write_flush(filp
, buf
, count
, ppos
, cd
);
1736 const struct file_operations cache_flush_operations_pipefs
= {
1737 .open
= open_flush_pipefs
,
1738 .read
= read_flush_pipefs
,
1739 .write
= write_flush_pipefs
,
1740 .release
= release_flush_pipefs
,
1743 int sunrpc_cache_register_pipefs(struct dentry
*parent
,
1744 const char *name
, mode_t umode
,
1745 struct cache_detail
*cd
)
1751 sunrpc_init_cache_detail(cd
);
1753 q
.len
= strlen(name
);
1754 q
.hash
= full_name_hash(q
.name
, q
.len
);
1755 dir
= rpc_create_cache_dir(parent
, &q
, umode
, cd
);
1757 cd
->u
.pipefs
.dir
= dir
;
1759 sunrpc_destroy_cache_detail(cd
);
1764 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs
);
1766 void sunrpc_cache_unregister_pipefs(struct cache_detail
*cd
)
1768 rpc_remove_cache_dir(cd
->u
.pipefs
.dir
);
1769 cd
->u
.pipefs
.dir
= NULL
;
1770 sunrpc_destroy_cache_detail(cd
);
1772 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs
);