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>
35 #include <linux/sunrpc/rpc_pipe_fs.h>
38 #define RPCDBG_FACILITY RPCDBG_CACHE
40 static bool 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 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
132 set_bit(CACHE_VALID
, &head
->flags
);
135 static void cache_fresh_unlocked(struct cache_head
*head
,
136 struct cache_detail
*detail
)
138 if (test_and_clear_bit(CACHE_PENDING
, &head
->flags
)) {
139 cache_revisit_request(head
);
140 cache_dequeue(detail
, head
);
144 struct cache_head
*sunrpc_cache_update(struct cache_detail
*detail
,
145 struct cache_head
*new, struct cache_head
*old
, int hash
)
147 /* The 'old' entry is to be replaced by 'new'.
148 * If 'old' is not VALID, we update it directly,
149 * otherwise we need to replace it
151 struct cache_head
**head
;
152 struct cache_head
*tmp
;
154 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
155 write_lock(&detail
->hash_lock
);
156 if (!test_bit(CACHE_VALID
, &old
->flags
)) {
157 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
158 set_bit(CACHE_NEGATIVE
, &old
->flags
);
160 detail
->update(old
, new);
161 cache_fresh_locked(old
, new->expiry_time
);
162 write_unlock(&detail
->hash_lock
);
163 cache_fresh_unlocked(old
, detail
);
166 write_unlock(&detail
->hash_lock
);
168 /* We need to insert a new entry */
169 tmp
= detail
->alloc();
171 cache_put(old
, detail
);
175 detail
->init(tmp
, old
);
176 head
= &detail
->hash_table
[hash
];
178 write_lock(&detail
->hash_lock
);
179 if (test_bit(CACHE_NEGATIVE
, &new->flags
))
180 set_bit(CACHE_NEGATIVE
, &tmp
->flags
);
182 detail
->update(tmp
, new);
187 cache_fresh_locked(tmp
, new->expiry_time
);
188 cache_fresh_locked(old
, 0);
189 write_unlock(&detail
->hash_lock
);
190 cache_fresh_unlocked(tmp
, detail
);
191 cache_fresh_unlocked(old
, detail
);
192 cache_put(old
, detail
);
195 EXPORT_SYMBOL_GPL(sunrpc_cache_update
);
197 static int cache_make_upcall(struct cache_detail
*cd
, struct cache_head
*h
)
199 if (cd
->cache_upcall
)
200 return cd
->cache_upcall(cd
, h
);
201 return sunrpc_cache_pipe_upcall(cd
, h
);
204 static inline int cache_is_valid(struct cache_head
*h
)
206 if (!test_bit(CACHE_VALID
, &h
->flags
))
210 if (test_bit(CACHE_NEGATIVE
, &h
->flags
))
214 * In combination with write barrier in
215 * sunrpc_cache_update, ensures that anyone
216 * using the cache entry after this sees the
225 static int try_to_negate_entry(struct cache_detail
*detail
, struct cache_head
*h
)
229 write_lock(&detail
->hash_lock
);
230 rv
= cache_is_valid(h
);
232 set_bit(CACHE_NEGATIVE
, &h
->flags
);
233 cache_fresh_locked(h
, seconds_since_boot()+CACHE_NEW_EXPIRY
);
236 write_unlock(&detail
->hash_lock
);
237 cache_fresh_unlocked(h
, detail
);
242 * This is the generic cache management routine for all
243 * the authentication caches.
244 * It checks the currency of a cache item and will (later)
245 * initiate an upcall to fill it if needed.
248 * Returns 0 if the cache_head can be used, or cache_puts it and returns
249 * -EAGAIN if upcall is pending and request has been queued
250 * -ETIMEDOUT if upcall failed or request could not be queue or
251 * upcall completed but item is still invalid (implying that
252 * the cache item has been replaced with a newer one).
253 * -ENOENT if cache entry was negative
255 int cache_check(struct cache_detail
*detail
,
256 struct cache_head
*h
, struct cache_req
*rqstp
)
259 long refresh_age
, age
;
261 /* First decide return status as best we can */
262 rv
= cache_is_valid(h
);
264 /* now see if we want to start an upcall */
265 refresh_age
= (h
->expiry_time
- h
->last_refresh
);
266 age
= seconds_since_boot() - h
->last_refresh
;
271 } else if (rv
== -EAGAIN
|| age
> refresh_age
/2) {
272 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
274 if (!test_and_set_bit(CACHE_PENDING
, &h
->flags
)) {
275 switch (cache_make_upcall(detail
, h
)) {
277 rv
= try_to_negate_entry(detail
, h
);
280 cache_fresh_unlocked(h
, detail
);
287 if (!cache_defer_req(rqstp
, h
)) {
289 * Request was not deferred; handle it as best
292 rv
= cache_is_valid(h
);
298 cache_put(h
, detail
);
301 EXPORT_SYMBOL_GPL(cache_check
);
304 * caches need to be periodically cleaned.
305 * For this we maintain a list of cache_detail and
306 * a current pointer into that list and into the table
309 * Each time clean_cache is called it finds the next non-empty entry
310 * in the current table and walks the list in that entry
311 * looking for entries that can be removed.
313 * An entry gets removed if:
314 * - The expiry is before current time
315 * - The last_refresh time is before the flush_time for that cache
317 * later we might drop old entries with non-NEVER expiry if that table
318 * is getting 'full' for some definition of 'full'
320 * The question of "how often to scan a table" is an interesting one
321 * and is answered in part by the use of the "nextcheck" field in the
323 * When a scan of a table begins, the nextcheck field is set to a time
324 * that is well into the future.
325 * While scanning, if an expiry time is found that is earlier than the
326 * current nextcheck time, nextcheck is set to that expiry time.
327 * If the flush_time is ever set to a time earlier than the nextcheck
328 * time, the nextcheck time is then set to that flush_time.
330 * A table is then only scanned if the current time is at least
331 * the nextcheck time.
335 static LIST_HEAD(cache_list
);
336 static DEFINE_SPINLOCK(cache_list_lock
);
337 static struct cache_detail
*current_detail
;
338 static int current_index
;
340 static void do_cache_clean(struct work_struct
*work
);
341 static struct delayed_work cache_cleaner
;
343 void sunrpc_init_cache_detail(struct cache_detail
*cd
)
345 rwlock_init(&cd
->hash_lock
);
346 INIT_LIST_HEAD(&cd
->queue
);
347 spin_lock(&cache_list_lock
);
350 atomic_set(&cd
->readers
, 0);
353 list_add(&cd
->others
, &cache_list
);
354 spin_unlock(&cache_list_lock
);
356 /* start the cleaning process */
357 schedule_delayed_work(&cache_cleaner
, 0);
359 EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail
);
361 void sunrpc_destroy_cache_detail(struct cache_detail
*cd
)
364 spin_lock(&cache_list_lock
);
365 write_lock(&cd
->hash_lock
);
366 if (cd
->entries
|| atomic_read(&cd
->inuse
)) {
367 write_unlock(&cd
->hash_lock
);
368 spin_unlock(&cache_list_lock
);
371 if (current_detail
== cd
)
372 current_detail
= NULL
;
373 list_del_init(&cd
->others
);
374 write_unlock(&cd
->hash_lock
);
375 spin_unlock(&cache_list_lock
);
376 if (list_empty(&cache_list
)) {
377 /* module must be being unloaded so its safe to kill the worker */
378 cancel_delayed_work_sync(&cache_cleaner
);
382 printk(KERN_ERR
"nfsd: failed to unregister %s cache\n", cd
->name
);
384 EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail
);
386 /* clean cache tries to find something to clean
388 * It returns 1 if it cleaned something,
389 * 0 if it didn't find anything this time
390 * -1 if it fell off the end of the list.
392 static int cache_clean(void)
395 struct list_head
*next
;
397 spin_lock(&cache_list_lock
);
399 /* find a suitable table if we don't already have one */
400 while (current_detail
== NULL
||
401 current_index
>= current_detail
->hash_size
) {
403 next
= current_detail
->others
.next
;
405 next
= cache_list
.next
;
406 if (next
== &cache_list
) {
407 current_detail
= NULL
;
408 spin_unlock(&cache_list_lock
);
411 current_detail
= list_entry(next
, struct cache_detail
, others
);
412 if (current_detail
->nextcheck
> seconds_since_boot())
413 current_index
= current_detail
->hash_size
;
416 current_detail
->nextcheck
= seconds_since_boot()+30*60;
420 /* find a non-empty bucket in the table */
421 while (current_detail
&&
422 current_index
< current_detail
->hash_size
&&
423 current_detail
->hash_table
[current_index
] == NULL
)
426 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
428 if (current_detail
&& current_index
< current_detail
->hash_size
) {
429 struct cache_head
*ch
, **cp
;
430 struct cache_detail
*d
;
432 write_lock(¤t_detail
->hash_lock
);
434 /* Ok, now to clean this strand */
436 cp
= & current_detail
->hash_table
[current_index
];
437 for (ch
= *cp
; ch
; cp
= & ch
->next
, ch
= *cp
) {
438 if (current_detail
->nextcheck
> ch
->expiry_time
)
439 current_detail
->nextcheck
= ch
->expiry_time
+1;
440 if (!cache_is_expired(current_detail
, ch
))
445 current_detail
->entries
--;
450 write_unlock(¤t_detail
->hash_lock
);
454 spin_unlock(&cache_list_lock
);
456 cache_fresh_unlocked(ch
, d
);
460 spin_unlock(&cache_list_lock
);
466 * We want to regularly clean the cache, so we need to schedule some work ...
468 static void do_cache_clean(struct work_struct
*work
)
471 if (cache_clean() == -1)
472 delay
= round_jiffies_relative(30*HZ
);
474 if (list_empty(&cache_list
))
478 schedule_delayed_work(&cache_cleaner
, delay
);
483 * Clean all caches promptly. This just calls cache_clean
484 * repeatedly until we are sure that every cache has had a chance to
487 void cache_flush(void)
489 while (cache_clean() != -1)
491 while (cache_clean() != -1)
494 EXPORT_SYMBOL_GPL(cache_flush
);
496 void cache_purge(struct cache_detail
*detail
)
498 detail
->flush_time
= LONG_MAX
;
499 detail
->nextcheck
= seconds_since_boot();
501 detail
->flush_time
= 1;
503 EXPORT_SYMBOL_GPL(cache_purge
);
507 * Deferral and Revisiting of Requests.
509 * If a cache lookup finds a pending entry, we
510 * need to defer the request and revisit it later.
511 * All deferred requests are stored in a hash table,
512 * indexed by "struct cache_head *".
513 * As it may be wasteful to store a whole request
514 * structure, we allow the request to provide a
515 * deferred form, which must contain a
516 * 'struct cache_deferred_req'
517 * This cache_deferred_req contains a method to allow
518 * it to be revisited when cache info is available
521 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
522 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
524 #define DFR_MAX 300 /* ??? */
526 static DEFINE_SPINLOCK(cache_defer_lock
);
527 static LIST_HEAD(cache_defer_list
);
528 static struct hlist_head cache_defer_hash
[DFR_HASHSIZE
];
529 static int cache_defer_cnt
;
531 static void __unhash_deferred_req(struct cache_deferred_req
*dreq
)
533 hlist_del_init(&dreq
->hash
);
534 if (!list_empty(&dreq
->recent
)) {
535 list_del_init(&dreq
->recent
);
540 static void __hash_deferred_req(struct cache_deferred_req
*dreq
, struct cache_head
*item
)
542 int hash
= DFR_HASH(item
);
544 INIT_LIST_HEAD(&dreq
->recent
);
545 hlist_add_head(&dreq
->hash
, &cache_defer_hash
[hash
]);
548 static void setup_deferral(struct cache_deferred_req
*dreq
,
549 struct cache_head
*item
,
555 spin_lock(&cache_defer_lock
);
557 __hash_deferred_req(dreq
, item
);
561 list_add(&dreq
->recent
, &cache_defer_list
);
564 spin_unlock(&cache_defer_lock
);
568 struct thread_deferred_req
{
569 struct cache_deferred_req handle
;
570 struct completion completion
;
573 static void cache_restart_thread(struct cache_deferred_req
*dreq
, int too_many
)
575 struct thread_deferred_req
*dr
=
576 container_of(dreq
, struct thread_deferred_req
, handle
);
577 complete(&dr
->completion
);
580 static void cache_wait_req(struct cache_req
*req
, struct cache_head
*item
)
582 struct thread_deferred_req sleeper
;
583 struct cache_deferred_req
*dreq
= &sleeper
.handle
;
585 sleeper
.completion
= COMPLETION_INITIALIZER_ONSTACK(sleeper
.completion
);
586 dreq
->revisit
= cache_restart_thread
;
588 setup_deferral(dreq
, item
, 0);
590 if (!test_bit(CACHE_PENDING
, &item
->flags
) ||
591 wait_for_completion_interruptible_timeout(
592 &sleeper
.completion
, req
->thread_wait
) <= 0) {
593 /* The completion wasn't completed, so we need
596 spin_lock(&cache_defer_lock
);
597 if (!hlist_unhashed(&sleeper
.handle
.hash
)) {
598 __unhash_deferred_req(&sleeper
.handle
);
599 spin_unlock(&cache_defer_lock
);
601 /* cache_revisit_request already removed
602 * this from the hash table, but hasn't
603 * called ->revisit yet. It will very soon
604 * and we need to wait for it.
606 spin_unlock(&cache_defer_lock
);
607 wait_for_completion(&sleeper
.completion
);
612 static void cache_limit_defers(void)
614 /* Make sure we haven't exceed the limit of allowed deferred
617 struct cache_deferred_req
*discard
= NULL
;
619 if (cache_defer_cnt
<= DFR_MAX
)
622 spin_lock(&cache_defer_lock
);
624 /* Consider removing either the first or the last */
625 if (cache_defer_cnt
> DFR_MAX
) {
626 if (net_random() & 1)
627 discard
= list_entry(cache_defer_list
.next
,
628 struct cache_deferred_req
, recent
);
630 discard
= list_entry(cache_defer_list
.prev
,
631 struct cache_deferred_req
, recent
);
632 __unhash_deferred_req(discard
);
634 spin_unlock(&cache_defer_lock
);
636 discard
->revisit(discard
, 1);
639 /* Return true if and only if a deferred request is queued. */
640 static bool cache_defer_req(struct cache_req
*req
, struct cache_head
*item
)
642 struct cache_deferred_req
*dreq
;
644 if (req
->thread_wait
) {
645 cache_wait_req(req
, item
);
646 if (!test_bit(CACHE_PENDING
, &item
->flags
))
649 dreq
= req
->defer(req
);
652 setup_deferral(dreq
, item
, 1);
653 if (!test_bit(CACHE_PENDING
, &item
->flags
))
654 /* Bit could have been cleared before we managed to
655 * set up the deferral, so need to revisit just in case
657 cache_revisit_request(item
);
659 cache_limit_defers();
663 static void cache_revisit_request(struct cache_head
*item
)
665 struct cache_deferred_req
*dreq
;
666 struct list_head pending
;
667 struct hlist_node
*tmp
;
668 int hash
= DFR_HASH(item
);
670 INIT_LIST_HEAD(&pending
);
671 spin_lock(&cache_defer_lock
);
673 hlist_for_each_entry_safe(dreq
, tmp
, &cache_defer_hash
[hash
], hash
)
674 if (dreq
->item
== item
) {
675 __unhash_deferred_req(dreq
);
676 list_add(&dreq
->recent
, &pending
);
679 spin_unlock(&cache_defer_lock
);
681 while (!list_empty(&pending
)) {
682 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
683 list_del_init(&dreq
->recent
);
684 dreq
->revisit(dreq
, 0);
688 void cache_clean_deferred(void *owner
)
690 struct cache_deferred_req
*dreq
, *tmp
;
691 struct list_head pending
;
694 INIT_LIST_HEAD(&pending
);
695 spin_lock(&cache_defer_lock
);
697 list_for_each_entry_safe(dreq
, tmp
, &cache_defer_list
, recent
) {
698 if (dreq
->owner
== owner
) {
699 __unhash_deferred_req(dreq
);
700 list_add(&dreq
->recent
, &pending
);
703 spin_unlock(&cache_defer_lock
);
705 while (!list_empty(&pending
)) {
706 dreq
= list_entry(pending
.next
, struct cache_deferred_req
, recent
);
707 list_del_init(&dreq
->recent
);
708 dreq
->revisit(dreq
, 1);
713 * communicate with user-space
715 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
716 * On read, you get a full request, or block.
717 * On write, an update request is processed.
718 * Poll works if anything to read, and always allows write.
720 * Implemented by linked list of requests. Each open file has
721 * a ->private that also exists in this list. New requests are added
722 * to the end and may wakeup and preceding readers.
723 * New readers are added to the head. If, on read, an item is found with
724 * CACHE_UPCALLING clear, we free it from the list.
728 static DEFINE_SPINLOCK(queue_lock
);
729 static DEFINE_MUTEX(queue_io_mutex
);
732 struct list_head list
;
733 int reader
; /* if 0, then request */
735 struct cache_request
{
736 struct cache_queue q
;
737 struct cache_head
*item
;
742 struct cache_reader
{
743 struct cache_queue q
;
744 int offset
; /* if non-0, we have a refcnt on next request */
747 static int cache_request(struct cache_detail
*detail
,
748 struct cache_request
*crq
)
753 detail
->cache_request(detail
, crq
->item
, &bp
, &len
);
756 return PAGE_SIZE
- len
;
759 static ssize_t
cache_read(struct file
*filp
, char __user
*buf
, size_t count
,
760 loff_t
*ppos
, struct cache_detail
*cd
)
762 struct cache_reader
*rp
= filp
->private_data
;
763 struct cache_request
*rq
;
764 struct inode
*inode
= file_inode(filp
);
770 mutex_lock(&inode
->i_mutex
); /* protect against multiple concurrent
771 * readers on this file */
773 spin_lock(&queue_lock
);
774 /* need to find next request */
775 while (rp
->q
.list
.next
!= &cd
->queue
&&
776 list_entry(rp
->q
.list
.next
, struct cache_queue
, list
)
778 struct list_head
*next
= rp
->q
.list
.next
;
779 list_move(&rp
->q
.list
, next
);
781 if (rp
->q
.list
.next
== &cd
->queue
) {
782 spin_unlock(&queue_lock
);
783 mutex_unlock(&inode
->i_mutex
);
784 WARN_ON_ONCE(rp
->offset
);
787 rq
= container_of(rp
->q
.list
.next
, struct cache_request
, q
.list
);
788 WARN_ON_ONCE(rq
->q
.reader
);
791 spin_unlock(&queue_lock
);
794 err
= cache_request(cd
, rq
);
800 if (rp
->offset
== 0 && !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
802 spin_lock(&queue_lock
);
803 list_move(&rp
->q
.list
, &rq
->q
.list
);
804 spin_unlock(&queue_lock
);
806 if (rp
->offset
+ count
> rq
->len
)
807 count
= rq
->len
- rp
->offset
;
809 if (copy_to_user(buf
, rq
->buf
+ rp
->offset
, count
))
812 if (rp
->offset
>= rq
->len
) {
814 spin_lock(&queue_lock
);
815 list_move(&rp
->q
.list
, &rq
->q
.list
);
816 spin_unlock(&queue_lock
);
821 if (rp
->offset
== 0) {
822 /* need to release rq */
823 spin_lock(&queue_lock
);
825 if (rq
->readers
== 0 &&
826 !test_bit(CACHE_PENDING
, &rq
->item
->flags
)) {
827 list_del(&rq
->q
.list
);
828 spin_unlock(&queue_lock
);
829 cache_put(rq
->item
, cd
);
833 spin_unlock(&queue_lock
);
837 mutex_unlock(&inode
->i_mutex
);
838 return err
? err
: count
;
841 static ssize_t
cache_do_downcall(char *kaddr
, const char __user
*buf
,
842 size_t count
, struct cache_detail
*cd
)
848 if (copy_from_user(kaddr
, buf
, count
))
851 ret
= cd
->cache_parse(cd
, kaddr
, count
);
857 static ssize_t
cache_slow_downcall(const char __user
*buf
,
858 size_t count
, struct cache_detail
*cd
)
860 static char write_buf
[8192]; /* protected by queue_io_mutex */
861 ssize_t ret
= -EINVAL
;
863 if (count
>= sizeof(write_buf
))
865 mutex_lock(&queue_io_mutex
);
866 ret
= cache_do_downcall(write_buf
, buf
, count
, cd
);
867 mutex_unlock(&queue_io_mutex
);
872 static ssize_t
cache_downcall(struct address_space
*mapping
,
873 const char __user
*buf
,
874 size_t count
, struct cache_detail
*cd
)
878 ssize_t ret
= -ENOMEM
;
880 if (count
>= PAGE_CACHE_SIZE
)
883 page
= find_or_create_page(mapping
, 0, GFP_KERNEL
);
888 ret
= cache_do_downcall(kaddr
, buf
, count
, cd
);
891 page_cache_release(page
);
894 return cache_slow_downcall(buf
, count
, cd
);
897 static ssize_t
cache_write(struct file
*filp
, const char __user
*buf
,
898 size_t count
, loff_t
*ppos
,
899 struct cache_detail
*cd
)
901 struct address_space
*mapping
= filp
->f_mapping
;
902 struct inode
*inode
= file_inode(filp
);
903 ssize_t ret
= -EINVAL
;
905 if (!cd
->cache_parse
)
908 mutex_lock(&inode
->i_mutex
);
909 ret
= cache_downcall(mapping
, buf
, count
, cd
);
910 mutex_unlock(&inode
->i_mutex
);
915 static DECLARE_WAIT_QUEUE_HEAD(queue_wait
);
917 static unsigned int cache_poll(struct file
*filp
, poll_table
*wait
,
918 struct cache_detail
*cd
)
921 struct cache_reader
*rp
= filp
->private_data
;
922 struct cache_queue
*cq
;
924 poll_wait(filp
, &queue_wait
, wait
);
926 /* alway allow write */
927 mask
= POLL_OUT
| POLLWRNORM
;
932 spin_lock(&queue_lock
);
934 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
935 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
937 mask
|= POLLIN
| POLLRDNORM
;
940 spin_unlock(&queue_lock
);
944 static int cache_ioctl(struct inode
*ino
, struct file
*filp
,
945 unsigned int cmd
, unsigned long arg
,
946 struct cache_detail
*cd
)
949 struct cache_reader
*rp
= filp
->private_data
;
950 struct cache_queue
*cq
;
952 if (cmd
!= FIONREAD
|| !rp
)
955 spin_lock(&queue_lock
);
957 /* only find the length remaining in current request,
958 * or the length of the next request
960 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
961 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
963 struct cache_request
*cr
=
964 container_of(cq
, struct cache_request
, q
);
965 len
= cr
->len
- rp
->offset
;
968 spin_unlock(&queue_lock
);
970 return put_user(len
, (int __user
*)arg
);
973 static int cache_open(struct inode
*inode
, struct file
*filp
,
974 struct cache_detail
*cd
)
976 struct cache_reader
*rp
= NULL
;
978 if (!cd
|| !try_module_get(cd
->owner
))
980 nonseekable_open(inode
, filp
);
981 if (filp
->f_mode
& FMODE_READ
) {
982 rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
984 module_put(cd
->owner
);
989 atomic_inc(&cd
->readers
);
990 spin_lock(&queue_lock
);
991 list_add(&rp
->q
.list
, &cd
->queue
);
992 spin_unlock(&queue_lock
);
994 filp
->private_data
= rp
;
998 static int cache_release(struct inode
*inode
, struct file
*filp
,
999 struct cache_detail
*cd
)
1001 struct cache_reader
*rp
= filp
->private_data
;
1004 spin_lock(&queue_lock
);
1006 struct cache_queue
*cq
;
1007 for (cq
= &rp
->q
; &cq
->list
!= &cd
->queue
;
1008 cq
= list_entry(cq
->list
.next
, struct cache_queue
, list
))
1010 container_of(cq
, struct cache_request
, q
)
1016 list_del(&rp
->q
.list
);
1017 spin_unlock(&queue_lock
);
1019 filp
->private_data
= NULL
;
1022 cd
->last_close
= seconds_since_boot();
1023 atomic_dec(&cd
->readers
);
1025 module_put(cd
->owner
);
1031 static void cache_dequeue(struct cache_detail
*detail
, struct cache_head
*ch
)
1033 struct cache_queue
*cq
, *tmp
;
1034 struct cache_request
*cr
;
1035 struct list_head dequeued
;
1037 INIT_LIST_HEAD(&dequeued
);
1038 spin_lock(&queue_lock
);
1039 list_for_each_entry_safe(cq
, tmp
, &detail
->queue
, list
)
1041 cr
= container_of(cq
, struct cache_request
, q
);
1044 if (test_bit(CACHE_PENDING
, &ch
->flags
))
1045 /* Lost a race and it is pending again */
1047 if (cr
->readers
!= 0)
1049 list_move(&cr
->q
.list
, &dequeued
);
1051 spin_unlock(&queue_lock
);
1052 while (!list_empty(&dequeued
)) {
1053 cr
= list_entry(dequeued
.next
, struct cache_request
, q
.list
);
1054 list_del(&cr
->q
.list
);
1055 cache_put(cr
->item
, detail
);
1062 * Support routines for text-based upcalls.
1063 * Fields are separated by spaces.
1064 * Fields are either mangled to quote space tab newline slosh with slosh
1065 * or a hexified with a leading \x
1066 * Record is terminated with newline.
1070 void qword_add(char **bpp
, int *lp
, char *str
)
1076 if (len
< 0) return;
1078 while ((c
=*str
++) && len
)
1086 *bp
++ = '0' + ((c
& 0300)>>6);
1087 *bp
++ = '0' + ((c
& 0070)>>3);
1088 *bp
++ = '0' + ((c
& 0007)>>0);
1096 if (c
|| len
<1) len
= -1;
1104 EXPORT_SYMBOL_GPL(qword_add
);
1106 void qword_addhex(char **bpp
, int *lp
, char *buf
, int blen
)
1111 if (len
< 0) return;
1117 while (blen
&& len
>= 2) {
1118 unsigned char c
= *buf
++;
1119 *bp
++ = '0' + ((c
&0xf0)>>4) + (c
>=0xa0)*('a'-'9'-1);
1120 *bp
++ = '0' + (c
&0x0f) + ((c
&0x0f)>=0x0a)*('a'-'9'-1);
1125 if (blen
|| len
<1) len
= -1;
1133 EXPORT_SYMBOL_GPL(qword_addhex
);
1135 static void warn_no_listener(struct cache_detail
*detail
)
1137 if (detail
->last_warn
!= detail
->last_close
) {
1138 detail
->last_warn
= detail
->last_close
;
1139 if (detail
->warn_no_listener
)
1140 detail
->warn_no_listener(detail
, detail
->last_close
!= 0);
1144 static bool cache_listeners_exist(struct cache_detail
*detail
)
1146 if (atomic_read(&detail
->readers
))
1148 if (detail
->last_close
== 0)
1149 /* This cache was never opened */
1151 if (detail
->last_close
< seconds_since_boot() - 30)
1153 * We allow for the possibility that someone might
1154 * restart a userspace daemon without restarting the
1155 * server; but after 30 seconds, we give up.
1162 * register an upcall request to user-space and queue it up for read() by the
1165 * Each request is at most one page long.
1167 int sunrpc_cache_pipe_upcall(struct cache_detail
*detail
, struct cache_head
*h
)
1171 struct cache_request
*crq
;
1174 if (!detail
->cache_request
)
1177 if (!cache_listeners_exist(detail
)) {
1178 warn_no_listener(detail
);
1182 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1186 crq
= kmalloc(sizeof (*crq
), GFP_KERNEL
);
1193 crq
->item
= cache_get(h
);
1197 spin_lock(&queue_lock
);
1198 if (test_bit(CACHE_PENDING
, &h
->flags
))
1199 list_add_tail(&crq
->q
.list
, &detail
->queue
);
1201 /* Lost a race, no longer PENDING, so don't enqueue */
1203 spin_unlock(&queue_lock
);
1204 wake_up(&queue_wait
);
1205 if (ret
== -EAGAIN
) {
1211 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall
);
1214 * parse a message from user-space and pass it
1215 * to an appropriate cache
1216 * Messages are, like requests, separated into fields by
1217 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1220 * reply cachename expiry key ... content....
1222 * key and content are both parsed by cache
1225 int qword_get(char **bpp
, char *dest
, int bufsize
)
1227 /* return bytes copied, or -1 on error */
1231 while (*bp
== ' ') bp
++;
1233 if (bp
[0] == '\\' && bp
[1] == 'x') {
1236 while (len
< bufsize
) {
1239 h
= hex_to_bin(bp
[0]);
1243 l
= hex_to_bin(bp
[1]);
1247 *dest
++ = (h
<< 4) | l
;
1252 /* text with \nnn octal quoting */
1253 while (*bp
!= ' ' && *bp
!= '\n' && *bp
&& len
< bufsize
-1) {
1255 isodigit(bp
[1]) && (bp
[1] <= '3') &&
1258 int byte
= (*++bp
-'0');
1260 byte
= (byte
<< 3) | (*bp
++ - '0');
1261 byte
= (byte
<< 3) | (*bp
++ - '0');
1271 if (*bp
!= ' ' && *bp
!= '\n' && *bp
!= '\0')
1273 while (*bp
== ' ') bp
++;
1278 EXPORT_SYMBOL_GPL(qword_get
);
1282 * support /proc/sunrpc/cache/$CACHENAME/content
1284 * We call ->cache_show passing NULL for the item to
1285 * get a header, then pass each real item in the cache
1289 struct cache_detail
*cd
;
1292 static void *c_start(struct seq_file
*m
, loff_t
*pos
)
1293 __acquires(cd
->hash_lock
)
1296 unsigned int hash
, entry
;
1297 struct cache_head
*ch
;
1298 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1301 read_lock(&cd
->hash_lock
);
1303 return SEQ_START_TOKEN
;
1305 entry
= n
& ((1LL<<32) - 1);
1307 for (ch
=cd
->hash_table
[hash
]; ch
; ch
=ch
->next
)
1310 n
&= ~((1LL<<32) - 1);
1314 } while(hash
< cd
->hash_size
&&
1315 cd
->hash_table
[hash
]==NULL
);
1316 if (hash
>= cd
->hash_size
)
1319 return cd
->hash_table
[hash
];
1322 static void *c_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
1324 struct cache_head
*ch
= p
;
1325 int hash
= (*pos
>> 32);
1326 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1328 if (p
== SEQ_START_TOKEN
)
1330 else if (ch
->next
== NULL
) {
1337 *pos
&= ~((1LL<<32) - 1);
1338 while (hash
< cd
->hash_size
&&
1339 cd
->hash_table
[hash
] == NULL
) {
1343 if (hash
>= cd
->hash_size
)
1346 return cd
->hash_table
[hash
];
1349 static void c_stop(struct seq_file
*m
, void *p
)
1350 __releases(cd
->hash_lock
)
1352 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1353 read_unlock(&cd
->hash_lock
);
1356 static int c_show(struct seq_file
*m
, void *p
)
1358 struct cache_head
*cp
= p
;
1359 struct cache_detail
*cd
= ((struct handle
*)m
->private)->cd
;
1361 if (p
== SEQ_START_TOKEN
)
1362 return cd
->cache_show(m
, cd
, NULL
);
1365 seq_printf(m
, "# expiry=%ld refcnt=%d flags=%lx\n",
1366 convert_to_wallclock(cp
->expiry_time
),
1367 atomic_read(&cp
->ref
.refcount
), cp
->flags
);
1369 if (cache_check(cd
, cp
, NULL
))
1370 /* cache_check does a cache_put on failure */
1371 seq_printf(m
, "# ");
1373 if (cache_is_expired(cd
, cp
))
1374 seq_printf(m
, "# ");
1378 return cd
->cache_show(m
, cd
, cp
);
1381 static const struct seq_operations cache_content_op
= {
1388 static int content_open(struct inode
*inode
, struct file
*file
,
1389 struct cache_detail
*cd
)
1393 if (!cd
|| !try_module_get(cd
->owner
))
1395 han
= __seq_open_private(file
, &cache_content_op
, sizeof(*han
));
1397 module_put(cd
->owner
);
1405 static int content_release(struct inode
*inode
, struct file
*file
,
1406 struct cache_detail
*cd
)
1408 int ret
= seq_release_private(inode
, file
);
1409 module_put(cd
->owner
);
1413 static int open_flush(struct inode
*inode
, struct file
*file
,
1414 struct cache_detail
*cd
)
1416 if (!cd
|| !try_module_get(cd
->owner
))
1418 return nonseekable_open(inode
, file
);
1421 static int release_flush(struct inode
*inode
, struct file
*file
,
1422 struct cache_detail
*cd
)
1424 module_put(cd
->owner
);
1428 static ssize_t
read_flush(struct file
*file
, char __user
*buf
,
1429 size_t count
, loff_t
*ppos
,
1430 struct cache_detail
*cd
)
1433 unsigned long p
= *ppos
;
1436 snprintf(tbuf
, sizeof(tbuf
), "%lu\n", convert_to_wallclock(cd
->flush_time
));
1443 if (copy_to_user(buf
, (void*)(tbuf
+p
), len
))
1449 static ssize_t
write_flush(struct file
*file
, const char __user
*buf
,
1450 size_t count
, loff_t
*ppos
,
1451 struct cache_detail
*cd
)
1456 if (*ppos
|| count
> sizeof(tbuf
)-1)
1458 if (copy_from_user(tbuf
, buf
, count
))
1461 simple_strtoul(tbuf
, &ep
, 0);
1462 if (*ep
&& *ep
!= '\n')
1466 cd
->flush_time
= get_expiry(&bp
);
1467 cd
->nextcheck
= seconds_since_boot();
1474 static ssize_t
cache_read_procfs(struct file
*filp
, char __user
*buf
,
1475 size_t count
, loff_t
*ppos
)
1477 struct cache_detail
*cd
= PDE_DATA(file_inode(filp
));
1479 return cache_read(filp
, buf
, count
, ppos
, cd
);
1482 static ssize_t
cache_write_procfs(struct file
*filp
, const char __user
*buf
,
1483 size_t count
, loff_t
*ppos
)
1485 struct cache_detail
*cd
= PDE_DATA(file_inode(filp
));
1487 return cache_write(filp
, buf
, count
, ppos
, cd
);
1490 static unsigned int cache_poll_procfs(struct file
*filp
, poll_table
*wait
)
1492 struct cache_detail
*cd
= PDE_DATA(file_inode(filp
));
1494 return cache_poll(filp
, wait
, cd
);
1497 static long cache_ioctl_procfs(struct file
*filp
,
1498 unsigned int cmd
, unsigned long arg
)
1500 struct inode
*inode
= file_inode(filp
);
1501 struct cache_detail
*cd
= PDE_DATA(inode
);
1503 return cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1506 static int cache_open_procfs(struct inode
*inode
, struct file
*filp
)
1508 struct cache_detail
*cd
= PDE_DATA(inode
);
1510 return cache_open(inode
, filp
, cd
);
1513 static int cache_release_procfs(struct inode
*inode
, struct file
*filp
)
1515 struct cache_detail
*cd
= PDE_DATA(inode
);
1517 return cache_release(inode
, filp
, cd
);
1520 static const struct file_operations cache_file_operations_procfs
= {
1521 .owner
= THIS_MODULE
,
1522 .llseek
= no_llseek
,
1523 .read
= cache_read_procfs
,
1524 .write
= cache_write_procfs
,
1525 .poll
= cache_poll_procfs
,
1526 .unlocked_ioctl
= cache_ioctl_procfs
, /* for FIONREAD */
1527 .open
= cache_open_procfs
,
1528 .release
= cache_release_procfs
,
1531 static int content_open_procfs(struct inode
*inode
, struct file
*filp
)
1533 struct cache_detail
*cd
= PDE_DATA(inode
);
1535 return content_open(inode
, filp
, cd
);
1538 static int content_release_procfs(struct inode
*inode
, struct file
*filp
)
1540 struct cache_detail
*cd
= PDE_DATA(inode
);
1542 return content_release(inode
, filp
, cd
);
1545 static const struct file_operations content_file_operations_procfs
= {
1546 .open
= content_open_procfs
,
1548 .llseek
= seq_lseek
,
1549 .release
= content_release_procfs
,
1552 static int open_flush_procfs(struct inode
*inode
, struct file
*filp
)
1554 struct cache_detail
*cd
= PDE_DATA(inode
);
1556 return open_flush(inode
, filp
, cd
);
1559 static int release_flush_procfs(struct inode
*inode
, struct file
*filp
)
1561 struct cache_detail
*cd
= PDE_DATA(inode
);
1563 return release_flush(inode
, filp
, cd
);
1566 static ssize_t
read_flush_procfs(struct file
*filp
, char __user
*buf
,
1567 size_t count
, loff_t
*ppos
)
1569 struct cache_detail
*cd
= PDE_DATA(file_inode(filp
));
1571 return read_flush(filp
, buf
, count
, ppos
, cd
);
1574 static ssize_t
write_flush_procfs(struct file
*filp
,
1575 const char __user
*buf
,
1576 size_t count
, loff_t
*ppos
)
1578 struct cache_detail
*cd
= PDE_DATA(file_inode(filp
));
1580 return write_flush(filp
, buf
, count
, ppos
, cd
);
1583 static const struct file_operations cache_flush_operations_procfs
= {
1584 .open
= open_flush_procfs
,
1585 .read
= read_flush_procfs
,
1586 .write
= write_flush_procfs
,
1587 .release
= release_flush_procfs
,
1588 .llseek
= no_llseek
,
1591 static void remove_cache_proc_entries(struct cache_detail
*cd
, struct net
*net
)
1593 struct sunrpc_net
*sn
;
1595 if (cd
->u
.procfs
.proc_ent
== NULL
)
1597 if (cd
->u
.procfs
.flush_ent
)
1598 remove_proc_entry("flush", cd
->u
.procfs
.proc_ent
);
1599 if (cd
->u
.procfs
.channel_ent
)
1600 remove_proc_entry("channel", cd
->u
.procfs
.proc_ent
);
1601 if (cd
->u
.procfs
.content_ent
)
1602 remove_proc_entry("content", cd
->u
.procfs
.proc_ent
);
1603 cd
->u
.procfs
.proc_ent
= NULL
;
1604 sn
= net_generic(net
, sunrpc_net_id
);
1605 remove_proc_entry(cd
->name
, sn
->proc_net_rpc
);
1608 #ifdef CONFIG_PROC_FS
1609 static int create_cache_proc_entries(struct cache_detail
*cd
, struct net
*net
)
1611 struct proc_dir_entry
*p
;
1612 struct sunrpc_net
*sn
;
1614 sn
= net_generic(net
, sunrpc_net_id
);
1615 cd
->u
.procfs
.proc_ent
= proc_mkdir(cd
->name
, sn
->proc_net_rpc
);
1616 if (cd
->u
.procfs
.proc_ent
== NULL
)
1618 cd
->u
.procfs
.channel_ent
= NULL
;
1619 cd
->u
.procfs
.content_ent
= NULL
;
1621 p
= proc_create_data("flush", S_IFREG
|S_IRUSR
|S_IWUSR
,
1622 cd
->u
.procfs
.proc_ent
,
1623 &cache_flush_operations_procfs
, cd
);
1624 cd
->u
.procfs
.flush_ent
= p
;
1628 if (cd
->cache_request
|| cd
->cache_parse
) {
1629 p
= proc_create_data("channel", S_IFREG
|S_IRUSR
|S_IWUSR
,
1630 cd
->u
.procfs
.proc_ent
,
1631 &cache_file_operations_procfs
, cd
);
1632 cd
->u
.procfs
.channel_ent
= p
;
1636 if (cd
->cache_show
) {
1637 p
= proc_create_data("content", S_IFREG
|S_IRUSR
,
1638 cd
->u
.procfs
.proc_ent
,
1639 &content_file_operations_procfs
, cd
);
1640 cd
->u
.procfs
.content_ent
= p
;
1646 remove_cache_proc_entries(cd
, net
);
1649 #else /* CONFIG_PROC_FS */
1650 static int create_cache_proc_entries(struct cache_detail
*cd
, struct net
*net
)
1656 void __init
cache_initialize(void)
1658 INIT_DEFERRABLE_WORK(&cache_cleaner
, do_cache_clean
);
1661 int cache_register_net(struct cache_detail
*cd
, struct net
*net
)
1665 sunrpc_init_cache_detail(cd
);
1666 ret
= create_cache_proc_entries(cd
, net
);
1668 sunrpc_destroy_cache_detail(cd
);
1671 EXPORT_SYMBOL_GPL(cache_register_net
);
1673 void cache_unregister_net(struct cache_detail
*cd
, struct net
*net
)
1675 remove_cache_proc_entries(cd
, net
);
1676 sunrpc_destroy_cache_detail(cd
);
1678 EXPORT_SYMBOL_GPL(cache_unregister_net
);
1680 struct cache_detail
*cache_create_net(struct cache_detail
*tmpl
, struct net
*net
)
1682 struct cache_detail
*cd
;
1684 cd
= kmemdup(tmpl
, sizeof(struct cache_detail
), GFP_KERNEL
);
1686 return ERR_PTR(-ENOMEM
);
1688 cd
->hash_table
= kzalloc(cd
->hash_size
* sizeof(struct cache_head
*),
1690 if (cd
->hash_table
== NULL
) {
1692 return ERR_PTR(-ENOMEM
);
1697 EXPORT_SYMBOL_GPL(cache_create_net
);
1699 void cache_destroy_net(struct cache_detail
*cd
, struct net
*net
)
1701 kfree(cd
->hash_table
);
1704 EXPORT_SYMBOL_GPL(cache_destroy_net
);
1706 static ssize_t
cache_read_pipefs(struct file
*filp
, char __user
*buf
,
1707 size_t count
, loff_t
*ppos
)
1709 struct cache_detail
*cd
= RPC_I(file_inode(filp
))->private;
1711 return cache_read(filp
, buf
, count
, ppos
, cd
);
1714 static ssize_t
cache_write_pipefs(struct file
*filp
, const char __user
*buf
,
1715 size_t count
, loff_t
*ppos
)
1717 struct cache_detail
*cd
= RPC_I(file_inode(filp
))->private;
1719 return cache_write(filp
, buf
, count
, ppos
, cd
);
1722 static unsigned int cache_poll_pipefs(struct file
*filp
, poll_table
*wait
)
1724 struct cache_detail
*cd
= RPC_I(file_inode(filp
))->private;
1726 return cache_poll(filp
, wait
, cd
);
1729 static long cache_ioctl_pipefs(struct file
*filp
,
1730 unsigned int cmd
, unsigned long arg
)
1732 struct inode
*inode
= file_inode(filp
);
1733 struct cache_detail
*cd
= RPC_I(inode
)->private;
1735 return cache_ioctl(inode
, filp
, cmd
, arg
, cd
);
1738 static int cache_open_pipefs(struct inode
*inode
, struct file
*filp
)
1740 struct cache_detail
*cd
= RPC_I(inode
)->private;
1742 return cache_open(inode
, filp
, cd
);
1745 static int cache_release_pipefs(struct inode
*inode
, struct file
*filp
)
1747 struct cache_detail
*cd
= RPC_I(inode
)->private;
1749 return cache_release(inode
, filp
, cd
);
1752 const struct file_operations cache_file_operations_pipefs
= {
1753 .owner
= THIS_MODULE
,
1754 .llseek
= no_llseek
,
1755 .read
= cache_read_pipefs
,
1756 .write
= cache_write_pipefs
,
1757 .poll
= cache_poll_pipefs
,
1758 .unlocked_ioctl
= cache_ioctl_pipefs
, /* for FIONREAD */
1759 .open
= cache_open_pipefs
,
1760 .release
= cache_release_pipefs
,
1763 static int content_open_pipefs(struct inode
*inode
, struct file
*filp
)
1765 struct cache_detail
*cd
= RPC_I(inode
)->private;
1767 return content_open(inode
, filp
, cd
);
1770 static int content_release_pipefs(struct inode
*inode
, struct file
*filp
)
1772 struct cache_detail
*cd
= RPC_I(inode
)->private;
1774 return content_release(inode
, filp
, cd
);
1777 const struct file_operations content_file_operations_pipefs
= {
1778 .open
= content_open_pipefs
,
1780 .llseek
= seq_lseek
,
1781 .release
= content_release_pipefs
,
1784 static int open_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1786 struct cache_detail
*cd
= RPC_I(inode
)->private;
1788 return open_flush(inode
, filp
, cd
);
1791 static int release_flush_pipefs(struct inode
*inode
, struct file
*filp
)
1793 struct cache_detail
*cd
= RPC_I(inode
)->private;
1795 return release_flush(inode
, filp
, cd
);
1798 static ssize_t
read_flush_pipefs(struct file
*filp
, char __user
*buf
,
1799 size_t count
, loff_t
*ppos
)
1801 struct cache_detail
*cd
= RPC_I(file_inode(filp
))->private;
1803 return read_flush(filp
, buf
, count
, ppos
, cd
);
1806 static ssize_t
write_flush_pipefs(struct file
*filp
,
1807 const char __user
*buf
,
1808 size_t count
, loff_t
*ppos
)
1810 struct cache_detail
*cd
= RPC_I(file_inode(filp
))->private;
1812 return write_flush(filp
, buf
, count
, ppos
, cd
);
1815 const struct file_operations cache_flush_operations_pipefs
= {
1816 .open
= open_flush_pipefs
,
1817 .read
= read_flush_pipefs
,
1818 .write
= write_flush_pipefs
,
1819 .release
= release_flush_pipefs
,
1820 .llseek
= no_llseek
,
1823 int sunrpc_cache_register_pipefs(struct dentry
*parent
,
1824 const char *name
, umode_t umode
,
1825 struct cache_detail
*cd
)
1832 q
.len
= strlen(name
);
1833 q
.hash
= full_name_hash(q
.name
, q
.len
);
1834 dir
= rpc_create_cache_dir(parent
, &q
, umode
, cd
);
1836 cd
->u
.pipefs
.dir
= dir
;
1841 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs
);
1843 void sunrpc_cache_unregister_pipefs(struct cache_detail
*cd
)
1845 rpc_remove_cache_dir(cd
->u
.pipefs
.dir
);
1846 cd
->u
.pipefs
.dir
= NULL
;
1848 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs
);