MIPS: Make init_vdso a subsys_initcall.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sunrpc / cache.c
blob58de76c8540c65dac38999ea4c9a5c2af5303f92
1 /*
2 * net/sunrpc/cache.c
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>
14 #include <linux/fs.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>
37 #include <linux/smp_lock.h>
39 #define RPCDBG_FACILITY RPCDBG_CACHE
41 static int cache_defer_req(struct cache_req *req, struct cache_head *item);
42 static void cache_revisit_request(struct cache_head *item);
44 static void cache_init(struct cache_head *h)
46 time_t now = get_seconds();
47 h->next = NULL;
48 h->flags = 0;
49 kref_init(&h->ref);
50 h->expiry_time = now + CACHE_NEW_EXPIRY;
51 h->last_refresh = now;
54 static inline int cache_is_expired(struct cache_detail *detail, struct cache_head *h)
56 return (h->expiry_time < get_seconds()) ||
57 (detail->flush_time > h->last_refresh);
60 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
61 struct cache_head *key, int hash)
63 struct cache_head **head, **hp;
64 struct cache_head *new = NULL, *freeme = NULL;
66 head = &detail->hash_table[hash];
68 read_lock(&detail->hash_lock);
70 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
71 struct cache_head *tmp = *hp;
72 if (detail->match(tmp, key)) {
73 if (cache_is_expired(detail, tmp))
74 /* This entry is expired, we will discard it. */
75 break;
76 cache_get(tmp);
77 read_unlock(&detail->hash_lock);
78 return tmp;
81 read_unlock(&detail->hash_lock);
82 /* Didn't find anything, insert an empty entry */
84 new = detail->alloc();
85 if (!new)
86 return NULL;
87 /* must fully initialise 'new', else
88 * we might get lose if we need to
89 * cache_put it soon.
91 cache_init(new);
92 detail->init(new, key);
94 write_lock(&detail->hash_lock);
96 /* check if entry appeared while we slept */
97 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
98 struct cache_head *tmp = *hp;
99 if (detail->match(tmp, key)) {
100 if (cache_is_expired(detail, tmp)) {
101 *hp = tmp->next;
102 tmp->next = NULL;
103 detail->entries --;
104 freeme = tmp;
105 break;
107 cache_get(tmp);
108 write_unlock(&detail->hash_lock);
109 cache_put(new, detail);
110 return tmp;
113 new->next = *head;
114 *head = new;
115 detail->entries++;
116 cache_get(new);
117 write_unlock(&detail->hash_lock);
119 if (freeme)
120 cache_put(freeme, detail);
121 return new;
123 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
126 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
128 static void cache_fresh_locked(struct cache_head *head, time_t expiry)
130 head->expiry_time = expiry;
131 head->last_refresh = get_seconds();
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);
159 else
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);
164 return old;
166 write_unlock(&detail->hash_lock);
168 /* We need to insert a new entry */
169 tmp = detail->alloc();
170 if (!tmp) {
171 cache_put(old, detail);
172 return NULL;
174 cache_init(tmp);
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);
181 else
182 detail->update(tmp, new);
183 tmp->next = *head;
184 *head = tmp;
185 detail->entries++;
186 cache_get(tmp);
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);
193 return tmp;
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 -EINVAL;
201 return cd->cache_upcall(cd, h);
204 static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h)
206 if (!test_bit(CACHE_VALID, &h->flags))
207 return -EAGAIN;
208 else {
209 /* entry is valid */
210 if (test_bit(CACHE_NEGATIVE, &h->flags))
211 return -ENOENT;
212 else
213 return 0;
218 * This is the generic cache management routine for all
219 * the authentication caches.
220 * It checks the currency of a cache item and will (later)
221 * initiate an upcall to fill it if needed.
224 * Returns 0 if the cache_head can be used, or cache_puts it and returns
225 * -EAGAIN if upcall is pending and request has been queued
226 * -ETIMEDOUT if upcall failed or request could not be queue or
227 * upcall completed but item is still invalid (implying that
228 * the cache item has been replaced with a newer one).
229 * -ENOENT if cache entry was negative
231 int cache_check(struct cache_detail *detail,
232 struct cache_head *h, struct cache_req *rqstp)
234 int rv;
235 long refresh_age, age;
237 /* First decide return status as best we can */
238 rv = cache_is_valid(detail, h);
240 /* now see if we want to start an upcall */
241 refresh_age = (h->expiry_time - h->last_refresh);
242 age = get_seconds() - h->last_refresh;
244 if (rqstp == NULL) {
245 if (rv == -EAGAIN)
246 rv = -ENOENT;
247 } else if (rv == -EAGAIN || age > refresh_age/2) {
248 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
249 refresh_age, age);
250 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
251 switch (cache_make_upcall(detail, h)) {
252 case -EINVAL:
253 clear_bit(CACHE_PENDING, &h->flags);
254 cache_revisit_request(h);
255 if (rv == -EAGAIN) {
256 set_bit(CACHE_NEGATIVE, &h->flags);
257 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY);
258 cache_fresh_unlocked(h, detail);
259 rv = -ENOENT;
261 break;
263 case -EAGAIN:
264 clear_bit(CACHE_PENDING, &h->flags);
265 cache_revisit_request(h);
266 break;
271 if (rv == -EAGAIN) {
272 if (cache_defer_req(rqstp, h) < 0) {
273 /* Request is not deferred */
274 rv = cache_is_valid(detail, h);
275 if (rv == -EAGAIN)
276 rv = -ETIMEDOUT;
279 if (rv)
280 cache_put(h, detail);
281 return rv;
283 EXPORT_SYMBOL_GPL(cache_check);
286 * caches need to be periodically cleaned.
287 * For this we maintain a list of cache_detail and
288 * a current pointer into that list and into the table
289 * for that entry.
291 * Each time clean_cache is called it finds the next non-empty entry
292 * in the current table and walks the list in that entry
293 * looking for entries that can be removed.
295 * An entry gets removed if:
296 * - The expiry is before current time
297 * - The last_refresh time is before the flush_time for that cache
299 * later we might drop old entries with non-NEVER expiry if that table
300 * is getting 'full' for some definition of 'full'
302 * The question of "how often to scan a table" is an interesting one
303 * and is answered in part by the use of the "nextcheck" field in the
304 * cache_detail.
305 * When a scan of a table begins, the nextcheck field is set to a time
306 * that is well into the future.
307 * While scanning, if an expiry time is found that is earlier than the
308 * current nextcheck time, nextcheck is set to that expiry time.
309 * If the flush_time is ever set to a time earlier than the nextcheck
310 * time, the nextcheck time is then set to that flush_time.
312 * A table is then only scanned if the current time is at least
313 * the nextcheck time.
317 static LIST_HEAD(cache_list);
318 static DEFINE_SPINLOCK(cache_list_lock);
319 static struct cache_detail *current_detail;
320 static int current_index;
322 static void do_cache_clean(struct work_struct *work);
323 static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
325 static void sunrpc_init_cache_detail(struct cache_detail *cd)
327 rwlock_init(&cd->hash_lock);
328 INIT_LIST_HEAD(&cd->queue);
329 spin_lock(&cache_list_lock);
330 cd->nextcheck = 0;
331 cd->entries = 0;
332 atomic_set(&cd->readers, 0);
333 cd->last_close = 0;
334 cd->last_warn = -1;
335 list_add(&cd->others, &cache_list);
336 spin_unlock(&cache_list_lock);
338 /* start the cleaning process */
339 schedule_delayed_work(&cache_cleaner, 0);
342 static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
344 cache_purge(cd);
345 spin_lock(&cache_list_lock);
346 write_lock(&cd->hash_lock);
347 if (cd->entries || atomic_read(&cd->inuse)) {
348 write_unlock(&cd->hash_lock);
349 spin_unlock(&cache_list_lock);
350 goto out;
352 if (current_detail == cd)
353 current_detail = NULL;
354 list_del_init(&cd->others);
355 write_unlock(&cd->hash_lock);
356 spin_unlock(&cache_list_lock);
357 if (list_empty(&cache_list)) {
358 /* module must be being unloaded so its safe to kill the worker */
359 cancel_delayed_work_sync(&cache_cleaner);
361 return;
362 out:
363 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
366 /* clean cache tries to find something to clean
367 * and cleans it.
368 * It returns 1 if it cleaned something,
369 * 0 if it didn't find anything this time
370 * -1 if it fell off the end of the list.
372 static int cache_clean(void)
374 int rv = 0;
375 struct list_head *next;
377 spin_lock(&cache_list_lock);
379 /* find a suitable table if we don't already have one */
380 while (current_detail == NULL ||
381 current_index >= current_detail->hash_size) {
382 if (current_detail)
383 next = current_detail->others.next;
384 else
385 next = cache_list.next;
386 if (next == &cache_list) {
387 current_detail = NULL;
388 spin_unlock(&cache_list_lock);
389 return -1;
391 current_detail = list_entry(next, struct cache_detail, others);
392 if (current_detail->nextcheck > get_seconds())
393 current_index = current_detail->hash_size;
394 else {
395 current_index = 0;
396 current_detail->nextcheck = get_seconds()+30*60;
400 /* find a non-empty bucket in the table */
401 while (current_detail &&
402 current_index < current_detail->hash_size &&
403 current_detail->hash_table[current_index] == NULL)
404 current_index++;
406 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
408 if (current_detail && current_index < current_detail->hash_size) {
409 struct cache_head *ch, **cp;
410 struct cache_detail *d;
412 write_lock(&current_detail->hash_lock);
414 /* Ok, now to clean this strand */
416 cp = & current_detail->hash_table[current_index];
417 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
418 if (current_detail->nextcheck > ch->expiry_time)
419 current_detail->nextcheck = ch->expiry_time+1;
420 if (!cache_is_expired(current_detail, ch))
421 continue;
423 *cp = ch->next;
424 ch->next = NULL;
425 current_detail->entries--;
426 rv = 1;
427 break;
430 write_unlock(&current_detail->hash_lock);
431 d = current_detail;
432 if (!ch)
433 current_index ++;
434 spin_unlock(&cache_list_lock);
435 if (ch) {
436 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
437 cache_dequeue(current_detail, ch);
438 cache_revisit_request(ch);
439 cache_put(ch, d);
441 } else
442 spin_unlock(&cache_list_lock);
444 return rv;
448 * We want to regularly clean the cache, so we need to schedule some work ...
450 static void do_cache_clean(struct work_struct *work)
452 int delay = 5;
453 if (cache_clean() == -1)
454 delay = round_jiffies_relative(30*HZ);
456 if (list_empty(&cache_list))
457 delay = 0;
459 if (delay)
460 schedule_delayed_work(&cache_cleaner, delay);
465 * Clean all caches promptly. This just calls cache_clean
466 * repeatedly until we are sure that every cache has had a chance to
467 * be fully cleaned
469 void cache_flush(void)
471 while (cache_clean() != -1)
472 cond_resched();
473 while (cache_clean() != -1)
474 cond_resched();
476 EXPORT_SYMBOL_GPL(cache_flush);
478 void cache_purge(struct cache_detail *detail)
480 detail->flush_time = LONG_MAX;
481 detail->nextcheck = get_seconds();
482 cache_flush();
483 detail->flush_time = 1;
485 EXPORT_SYMBOL_GPL(cache_purge);
489 * Deferral and Revisiting of Requests.
491 * If a cache lookup finds a pending entry, we
492 * need to defer the request and revisit it later.
493 * All deferred requests are stored in a hash table,
494 * indexed by "struct cache_head *".
495 * As it may be wasteful to store a whole request
496 * structure, we allow the request to provide a
497 * deferred form, which must contain a
498 * 'struct cache_deferred_req'
499 * This cache_deferred_req contains a method to allow
500 * it to be revisited when cache info is available
503 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
504 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
506 #define DFR_MAX 300 /* ??? */
508 static DEFINE_SPINLOCK(cache_defer_lock);
509 static LIST_HEAD(cache_defer_list);
510 static struct list_head cache_defer_hash[DFR_HASHSIZE];
511 static int cache_defer_cnt;
513 static int cache_defer_req(struct cache_req *req, struct cache_head *item)
515 struct cache_deferred_req *dreq, *discard;
516 int hash = DFR_HASH(item);
518 if (cache_defer_cnt >= DFR_MAX) {
519 /* too much in the cache, randomly drop this one,
520 * or continue and drop the oldest below
522 if (net_random()&1)
523 return -ENOMEM;
525 dreq = req->defer(req);
526 if (dreq == NULL)
527 return -ENOMEM;
529 dreq->item = item;
531 spin_lock(&cache_defer_lock);
533 list_add(&dreq->recent, &cache_defer_list);
535 if (cache_defer_hash[hash].next == NULL)
536 INIT_LIST_HEAD(&cache_defer_hash[hash]);
537 list_add(&dreq->hash, &cache_defer_hash[hash]);
539 /* it is in, now maybe clean up */
540 discard = NULL;
541 if (++cache_defer_cnt > DFR_MAX) {
542 discard = list_entry(cache_defer_list.prev,
543 struct cache_deferred_req, recent);
544 list_del_init(&discard->recent);
545 list_del_init(&discard->hash);
546 cache_defer_cnt--;
548 spin_unlock(&cache_defer_lock);
550 if (discard)
551 /* there was one too many */
552 discard->revisit(discard, 1);
554 if (!test_bit(CACHE_PENDING, &item->flags)) {
555 /* must have just been validated... */
556 cache_revisit_request(item);
557 return -EAGAIN;
559 return 0;
562 static void cache_revisit_request(struct cache_head *item)
564 struct cache_deferred_req *dreq;
565 struct list_head pending;
567 struct list_head *lp;
568 int hash = DFR_HASH(item);
570 INIT_LIST_HEAD(&pending);
571 spin_lock(&cache_defer_lock);
573 lp = cache_defer_hash[hash].next;
574 if (lp) {
575 while (lp != &cache_defer_hash[hash]) {
576 dreq = list_entry(lp, struct cache_deferred_req, hash);
577 lp = lp->next;
578 if (dreq->item == item) {
579 list_del_init(&dreq->hash);
580 list_move(&dreq->recent, &pending);
581 cache_defer_cnt--;
585 spin_unlock(&cache_defer_lock);
587 while (!list_empty(&pending)) {
588 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
589 list_del_init(&dreq->recent);
590 dreq->revisit(dreq, 0);
594 void cache_clean_deferred(void *owner)
596 struct cache_deferred_req *dreq, *tmp;
597 struct list_head pending;
600 INIT_LIST_HEAD(&pending);
601 spin_lock(&cache_defer_lock);
603 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
604 if (dreq->owner == owner) {
605 list_del_init(&dreq->hash);
606 list_move(&dreq->recent, &pending);
607 cache_defer_cnt--;
610 spin_unlock(&cache_defer_lock);
612 while (!list_empty(&pending)) {
613 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
614 list_del_init(&dreq->recent);
615 dreq->revisit(dreq, 1);
620 * communicate with user-space
622 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
623 * On read, you get a full request, or block.
624 * On write, an update request is processed.
625 * Poll works if anything to read, and always allows write.
627 * Implemented by linked list of requests. Each open file has
628 * a ->private that also exists in this list. New requests are added
629 * to the end and may wakeup and preceding readers.
630 * New readers are added to the head. If, on read, an item is found with
631 * CACHE_UPCALLING clear, we free it from the list.
635 static DEFINE_SPINLOCK(queue_lock);
636 static DEFINE_MUTEX(queue_io_mutex);
638 struct cache_queue {
639 struct list_head list;
640 int reader; /* if 0, then request */
642 struct cache_request {
643 struct cache_queue q;
644 struct cache_head *item;
645 char * buf;
646 int len;
647 int readers;
649 struct cache_reader {
650 struct cache_queue q;
651 int offset; /* if non-0, we have a refcnt on next request */
654 static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
655 loff_t *ppos, struct cache_detail *cd)
657 struct cache_reader *rp = filp->private_data;
658 struct cache_request *rq;
659 struct inode *inode = filp->f_path.dentry->d_inode;
660 int err;
662 if (count == 0)
663 return 0;
665 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
666 * readers on this file */
667 again:
668 spin_lock(&queue_lock);
669 /* need to find next request */
670 while (rp->q.list.next != &cd->queue &&
671 list_entry(rp->q.list.next, struct cache_queue, list)
672 ->reader) {
673 struct list_head *next = rp->q.list.next;
674 list_move(&rp->q.list, next);
676 if (rp->q.list.next == &cd->queue) {
677 spin_unlock(&queue_lock);
678 mutex_unlock(&inode->i_mutex);
679 BUG_ON(rp->offset);
680 return 0;
682 rq = container_of(rp->q.list.next, struct cache_request, q.list);
683 BUG_ON(rq->q.reader);
684 if (rp->offset == 0)
685 rq->readers++;
686 spin_unlock(&queue_lock);
688 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
689 err = -EAGAIN;
690 spin_lock(&queue_lock);
691 list_move(&rp->q.list, &rq->q.list);
692 spin_unlock(&queue_lock);
693 } else {
694 if (rp->offset + count > rq->len)
695 count = rq->len - rp->offset;
696 err = -EFAULT;
697 if (copy_to_user(buf, rq->buf + rp->offset, count))
698 goto out;
699 rp->offset += count;
700 if (rp->offset >= rq->len) {
701 rp->offset = 0;
702 spin_lock(&queue_lock);
703 list_move(&rp->q.list, &rq->q.list);
704 spin_unlock(&queue_lock);
706 err = 0;
708 out:
709 if (rp->offset == 0) {
710 /* need to release rq */
711 spin_lock(&queue_lock);
712 rq->readers--;
713 if (rq->readers == 0 &&
714 !test_bit(CACHE_PENDING, &rq->item->flags)) {
715 list_del(&rq->q.list);
716 spin_unlock(&queue_lock);
717 cache_put(rq->item, cd);
718 kfree(rq->buf);
719 kfree(rq);
720 } else
721 spin_unlock(&queue_lock);
723 if (err == -EAGAIN)
724 goto again;
725 mutex_unlock(&inode->i_mutex);
726 return err ? err : count;
729 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
730 size_t count, struct cache_detail *cd)
732 ssize_t ret;
734 if (copy_from_user(kaddr, buf, count))
735 return -EFAULT;
736 kaddr[count] = '\0';
737 ret = cd->cache_parse(cd, kaddr, count);
738 if (!ret)
739 ret = count;
740 return ret;
743 static ssize_t cache_slow_downcall(const char __user *buf,
744 size_t count, struct cache_detail *cd)
746 static char write_buf[8192]; /* protected by queue_io_mutex */
747 ssize_t ret = -EINVAL;
749 if (count >= sizeof(write_buf))
750 goto out;
751 mutex_lock(&queue_io_mutex);
752 ret = cache_do_downcall(write_buf, buf, count, cd);
753 mutex_unlock(&queue_io_mutex);
754 out:
755 return ret;
758 static ssize_t cache_downcall(struct address_space *mapping,
759 const char __user *buf,
760 size_t count, struct cache_detail *cd)
762 struct page *page;
763 char *kaddr;
764 ssize_t ret = -ENOMEM;
766 if (count >= PAGE_CACHE_SIZE)
767 goto out_slow;
769 page = find_or_create_page(mapping, 0, GFP_KERNEL);
770 if (!page)
771 goto out_slow;
773 kaddr = kmap(page);
774 ret = cache_do_downcall(kaddr, buf, count, cd);
775 kunmap(page);
776 unlock_page(page);
777 page_cache_release(page);
778 return ret;
779 out_slow:
780 return cache_slow_downcall(buf, count, cd);
783 static ssize_t cache_write(struct file *filp, const char __user *buf,
784 size_t count, loff_t *ppos,
785 struct cache_detail *cd)
787 struct address_space *mapping = filp->f_mapping;
788 struct inode *inode = filp->f_path.dentry->d_inode;
789 ssize_t ret = -EINVAL;
791 if (!cd->cache_parse)
792 goto out;
794 mutex_lock(&inode->i_mutex);
795 ret = cache_downcall(mapping, buf, count, cd);
796 mutex_unlock(&inode->i_mutex);
797 out:
798 return ret;
801 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
803 static unsigned int cache_poll(struct file *filp, poll_table *wait,
804 struct cache_detail *cd)
806 unsigned int mask;
807 struct cache_reader *rp = filp->private_data;
808 struct cache_queue *cq;
810 poll_wait(filp, &queue_wait, wait);
812 /* alway allow write */
813 mask = POLL_OUT | POLLWRNORM;
815 if (!rp)
816 return mask;
818 spin_lock(&queue_lock);
820 for (cq= &rp->q; &cq->list != &cd->queue;
821 cq = list_entry(cq->list.next, struct cache_queue, list))
822 if (!cq->reader) {
823 mask |= POLLIN | POLLRDNORM;
824 break;
826 spin_unlock(&queue_lock);
827 return mask;
830 static int cache_ioctl(struct inode *ino, struct file *filp,
831 unsigned int cmd, unsigned long arg,
832 struct cache_detail *cd)
834 int len = 0;
835 struct cache_reader *rp = filp->private_data;
836 struct cache_queue *cq;
838 if (cmd != FIONREAD || !rp)
839 return -EINVAL;
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))
848 if (!cq->reader) {
849 struct cache_request *cr =
850 container_of(cq, struct cache_request, q);
851 len = cr->len - rp->offset;
852 break;
854 spin_unlock(&queue_lock);
856 return put_user(len, (int __user *)arg);
859 static int cache_open(struct inode *inode, struct file *filp,
860 struct cache_detail *cd)
862 struct cache_reader *rp = NULL;
864 if (!cd || !try_module_get(cd->owner))
865 return -EACCES;
866 nonseekable_open(inode, filp);
867 if (filp->f_mode & FMODE_READ) {
868 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
869 if (!rp)
870 return -ENOMEM;
871 rp->offset = 0;
872 rp->q.reader = 1;
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;
879 return 0;
882 static int cache_release(struct inode *inode, struct file *filp,
883 struct cache_detail *cd)
885 struct cache_reader *rp = filp->private_data;
887 if (rp) {
888 spin_lock(&queue_lock);
889 if (rp->offset) {
890 struct cache_queue *cq;
891 for (cq= &rp->q; &cq->list != &cd->queue;
892 cq = list_entry(cq->list.next, struct cache_queue, list))
893 if (!cq->reader) {
894 container_of(cq, struct cache_request, q)
895 ->readers--;
896 break;
898 rp->offset = 0;
900 list_del(&rp->q.list);
901 spin_unlock(&queue_lock);
903 filp->private_data = NULL;
904 kfree(rp);
906 cd->last_close = get_seconds();
907 atomic_dec(&cd->readers);
909 module_put(cd->owner);
910 return 0;
915 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
917 struct cache_queue *cq;
918 spin_lock(&queue_lock);
919 list_for_each_entry(cq, &detail->queue, list)
920 if (!cq->reader) {
921 struct cache_request *cr = container_of(cq, struct cache_request, q);
922 if (cr->item != ch)
923 continue;
924 if (cr->readers != 0)
925 continue;
926 list_del(&cr->q.list);
927 spin_unlock(&queue_lock);
928 cache_put(cr->item, detail);
929 kfree(cr->buf);
930 kfree(cr);
931 return;
933 spin_unlock(&queue_lock);
937 * Support routines for text-based upcalls.
938 * Fields are separated by spaces.
939 * Fields are either mangled to quote space tab newline slosh with slosh
940 * or a hexified with a leading \x
941 * Record is terminated with newline.
945 void qword_add(char **bpp, int *lp, char *str)
947 char *bp = *bpp;
948 int len = *lp;
949 char c;
951 if (len < 0) return;
953 while ((c=*str++) && len)
954 switch(c) {
955 case ' ':
956 case '\t':
957 case '\n':
958 case '\\':
959 if (len >= 4) {
960 *bp++ = '\\';
961 *bp++ = '0' + ((c & 0300)>>6);
962 *bp++ = '0' + ((c & 0070)>>3);
963 *bp++ = '0' + ((c & 0007)>>0);
965 len -= 4;
966 break;
967 default:
968 *bp++ = c;
969 len--;
971 if (c || len <1) len = -1;
972 else {
973 *bp++ = ' ';
974 len--;
976 *bpp = bp;
977 *lp = len;
979 EXPORT_SYMBOL_GPL(qword_add);
981 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
983 char *bp = *bpp;
984 int len = *lp;
986 if (len < 0) return;
988 if (len > 2) {
989 *bp++ = '\\';
990 *bp++ = 'x';
991 len -= 2;
992 while (blen && len >= 2) {
993 unsigned char c = *buf++;
994 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
995 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
996 len -= 2;
997 blen--;
1000 if (blen || len<1) len = -1;
1001 else {
1002 *bp++ = ' ';
1003 len--;
1005 *bpp = bp;
1006 *lp = len;
1008 EXPORT_SYMBOL_GPL(qword_addhex);
1010 static void warn_no_listener(struct cache_detail *detail)
1012 if (detail->last_warn != detail->last_close) {
1013 detail->last_warn = detail->last_close;
1014 if (detail->warn_no_listener)
1015 detail->warn_no_listener(detail, detail->last_close != 0);
1020 * register an upcall request to user-space and queue it up for read() by the
1021 * upcall daemon.
1023 * Each request is at most one page long.
1025 int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1026 void (*cache_request)(struct cache_detail *,
1027 struct cache_head *,
1028 char **,
1029 int *))
1032 char *buf;
1033 struct cache_request *crq;
1034 char *bp;
1035 int len;
1037 if (atomic_read(&detail->readers) == 0 &&
1038 detail->last_close < get_seconds() - 30) {
1039 warn_no_listener(detail);
1040 return -EINVAL;
1043 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1044 if (!buf)
1045 return -EAGAIN;
1047 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1048 if (!crq) {
1049 kfree(buf);
1050 return -EAGAIN;
1053 bp = buf; len = PAGE_SIZE;
1055 cache_request(detail, h, &bp, &len);
1057 if (len < 0) {
1058 kfree(buf);
1059 kfree(crq);
1060 return -EAGAIN;
1062 crq->q.reader = 0;
1063 crq->item = cache_get(h);
1064 crq->buf = buf;
1065 crq->len = PAGE_SIZE - len;
1066 crq->readers = 0;
1067 spin_lock(&queue_lock);
1068 list_add_tail(&crq->q.list, &detail->queue);
1069 spin_unlock(&queue_lock);
1070 wake_up(&queue_wait);
1071 return 0;
1073 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1076 * parse a message from user-space and pass it
1077 * to an appropriate cache
1078 * Messages are, like requests, separated into fields by
1079 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1081 * Message is
1082 * reply cachename expiry key ... content....
1084 * key and content are both parsed by cache
1087 #define isodigit(c) (isdigit(c) && c <= '7')
1088 int qword_get(char **bpp, char *dest, int bufsize)
1090 /* return bytes copied, or -1 on error */
1091 char *bp = *bpp;
1092 int len = 0;
1094 while (*bp == ' ') bp++;
1096 if (bp[0] == '\\' && bp[1] == 'x') {
1097 /* HEX STRING */
1098 bp += 2;
1099 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1100 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1101 bp++;
1102 byte <<= 4;
1103 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1104 *dest++ = byte;
1105 bp++;
1106 len++;
1108 } else {
1109 /* text with \nnn octal quoting */
1110 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1111 if (*bp == '\\' &&
1112 isodigit(bp[1]) && (bp[1] <= '3') &&
1113 isodigit(bp[2]) &&
1114 isodigit(bp[3])) {
1115 int byte = (*++bp -'0');
1116 bp++;
1117 byte = (byte << 3) | (*bp++ - '0');
1118 byte = (byte << 3) | (*bp++ - '0');
1119 *dest++ = byte;
1120 len++;
1121 } else {
1122 *dest++ = *bp++;
1123 len++;
1128 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1129 return -1;
1130 while (*bp == ' ') bp++;
1131 *bpp = bp;
1132 *dest = '\0';
1133 return len;
1135 EXPORT_SYMBOL_GPL(qword_get);
1139 * support /proc/sunrpc/cache/$CACHENAME/content
1140 * as a seqfile.
1141 * We call ->cache_show passing NULL for the item to
1142 * get a header, then pass each real item in the cache
1145 struct handle {
1146 struct cache_detail *cd;
1149 static void *c_start(struct seq_file *m, loff_t *pos)
1150 __acquires(cd->hash_lock)
1152 loff_t n = *pos;
1153 unsigned hash, entry;
1154 struct cache_head *ch;
1155 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1158 read_lock(&cd->hash_lock);
1159 if (!n--)
1160 return SEQ_START_TOKEN;
1161 hash = n >> 32;
1162 entry = n & ((1LL<<32) - 1);
1164 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1165 if (!entry--)
1166 return ch;
1167 n &= ~((1LL<<32) - 1);
1168 do {
1169 hash++;
1170 n += 1LL<<32;
1171 } while(hash < cd->hash_size &&
1172 cd->hash_table[hash]==NULL);
1173 if (hash >= cd->hash_size)
1174 return NULL;
1175 *pos = n+1;
1176 return cd->hash_table[hash];
1179 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1181 struct cache_head *ch = p;
1182 int hash = (*pos >> 32);
1183 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1185 if (p == SEQ_START_TOKEN)
1186 hash = 0;
1187 else if (ch->next == NULL) {
1188 hash++;
1189 *pos += 1LL<<32;
1190 } else {
1191 ++*pos;
1192 return ch->next;
1194 *pos &= ~((1LL<<32) - 1);
1195 while (hash < cd->hash_size &&
1196 cd->hash_table[hash] == NULL) {
1197 hash++;
1198 *pos += 1LL<<32;
1200 if (hash >= cd->hash_size)
1201 return NULL;
1202 ++*pos;
1203 return cd->hash_table[hash];
1206 static void c_stop(struct seq_file *m, void *p)
1207 __releases(cd->hash_lock)
1209 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1210 read_unlock(&cd->hash_lock);
1213 static int c_show(struct seq_file *m, void *p)
1215 struct cache_head *cp = p;
1216 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1218 if (p == SEQ_START_TOKEN)
1219 return cd->cache_show(m, cd, NULL);
1221 ifdebug(CACHE)
1222 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1223 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
1224 cache_get(cp);
1225 if (cache_check(cd, cp, NULL))
1226 /* cache_check does a cache_put on failure */
1227 seq_printf(m, "# ");
1228 else
1229 cache_put(cp, cd);
1231 return cd->cache_show(m, cd, cp);
1234 static const struct seq_operations cache_content_op = {
1235 .start = c_start,
1236 .next = c_next,
1237 .stop = c_stop,
1238 .show = c_show,
1241 static int content_open(struct inode *inode, struct file *file,
1242 struct cache_detail *cd)
1244 struct handle *han;
1246 if (!cd || !try_module_get(cd->owner))
1247 return -EACCES;
1248 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1249 if (han == NULL) {
1250 module_put(cd->owner);
1251 return -ENOMEM;
1254 han->cd = cd;
1255 return 0;
1258 static int content_release(struct inode *inode, struct file *file,
1259 struct cache_detail *cd)
1261 int ret = seq_release_private(inode, file);
1262 module_put(cd->owner);
1263 return ret;
1266 static int open_flush(struct inode *inode, struct file *file,
1267 struct cache_detail *cd)
1269 if (!cd || !try_module_get(cd->owner))
1270 return -EACCES;
1271 return nonseekable_open(inode, file);
1274 static int release_flush(struct inode *inode, struct file *file,
1275 struct cache_detail *cd)
1277 module_put(cd->owner);
1278 return 0;
1281 static ssize_t read_flush(struct file *file, char __user *buf,
1282 size_t count, loff_t *ppos,
1283 struct cache_detail *cd)
1285 char tbuf[20];
1286 unsigned long p = *ppos;
1287 size_t len;
1289 sprintf(tbuf, "%lu\n", cd->flush_time);
1290 len = strlen(tbuf);
1291 if (p >= len)
1292 return 0;
1293 len -= p;
1294 if (len > count)
1295 len = count;
1296 if (copy_to_user(buf, (void*)(tbuf+p), len))
1297 return -EFAULT;
1298 *ppos += len;
1299 return len;
1302 static ssize_t write_flush(struct file *file, const char __user *buf,
1303 size_t count, loff_t *ppos,
1304 struct cache_detail *cd)
1306 char tbuf[20];
1307 char *ep;
1308 long flushtime;
1309 if (*ppos || count > sizeof(tbuf)-1)
1310 return -EINVAL;
1311 if (copy_from_user(tbuf, buf, count))
1312 return -EFAULT;
1313 tbuf[count] = 0;
1314 flushtime = simple_strtoul(tbuf, &ep, 0);
1315 if (*ep && *ep != '\n')
1316 return -EINVAL;
1318 cd->flush_time = flushtime;
1319 cd->nextcheck = get_seconds();
1320 cache_flush();
1322 *ppos += count;
1323 return count;
1326 static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1327 size_t count, loff_t *ppos)
1329 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1331 return cache_read(filp, buf, count, ppos, cd);
1334 static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1335 size_t count, loff_t *ppos)
1337 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1339 return cache_write(filp, buf, count, ppos, cd);
1342 static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1344 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1346 return cache_poll(filp, wait, cd);
1349 static long cache_ioctl_procfs(struct file *filp,
1350 unsigned int cmd, unsigned long arg)
1352 long ret;
1353 struct inode *inode = filp->f_path.dentry->d_inode;
1354 struct cache_detail *cd = PDE(inode)->data;
1356 lock_kernel();
1357 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1358 unlock_kernel();
1360 return ret;
1363 static int cache_open_procfs(struct inode *inode, struct file *filp)
1365 struct cache_detail *cd = PDE(inode)->data;
1367 return cache_open(inode, filp, cd);
1370 static int cache_release_procfs(struct inode *inode, struct file *filp)
1372 struct cache_detail *cd = PDE(inode)->data;
1374 return cache_release(inode, filp, cd);
1377 static const struct file_operations cache_file_operations_procfs = {
1378 .owner = THIS_MODULE,
1379 .llseek = no_llseek,
1380 .read = cache_read_procfs,
1381 .write = cache_write_procfs,
1382 .poll = cache_poll_procfs,
1383 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
1384 .open = cache_open_procfs,
1385 .release = cache_release_procfs,
1388 static int content_open_procfs(struct inode *inode, struct file *filp)
1390 struct cache_detail *cd = PDE(inode)->data;
1392 return content_open(inode, filp, cd);
1395 static int content_release_procfs(struct inode *inode, struct file *filp)
1397 struct cache_detail *cd = PDE(inode)->data;
1399 return content_release(inode, filp, cd);
1402 static const struct file_operations content_file_operations_procfs = {
1403 .open = content_open_procfs,
1404 .read = seq_read,
1405 .llseek = seq_lseek,
1406 .release = content_release_procfs,
1409 static int open_flush_procfs(struct inode *inode, struct file *filp)
1411 struct cache_detail *cd = PDE(inode)->data;
1413 return open_flush(inode, filp, cd);
1416 static int release_flush_procfs(struct inode *inode, struct file *filp)
1418 struct cache_detail *cd = PDE(inode)->data;
1420 return release_flush(inode, filp, cd);
1423 static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1424 size_t count, loff_t *ppos)
1426 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1428 return read_flush(filp, buf, count, ppos, cd);
1431 static ssize_t write_flush_procfs(struct file *filp,
1432 const char __user *buf,
1433 size_t count, loff_t *ppos)
1435 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1437 return write_flush(filp, buf, count, ppos, cd);
1440 static const struct file_operations cache_flush_operations_procfs = {
1441 .open = open_flush_procfs,
1442 .read = read_flush_procfs,
1443 .write = write_flush_procfs,
1444 .release = release_flush_procfs,
1447 static void remove_cache_proc_entries(struct cache_detail *cd)
1449 if (cd->u.procfs.proc_ent == NULL)
1450 return;
1451 if (cd->u.procfs.flush_ent)
1452 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1453 if (cd->u.procfs.channel_ent)
1454 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1455 if (cd->u.procfs.content_ent)
1456 remove_proc_entry("content", cd->u.procfs.proc_ent);
1457 cd->u.procfs.proc_ent = NULL;
1458 remove_proc_entry(cd->name, proc_net_rpc);
1461 #ifdef CONFIG_PROC_FS
1462 static int create_cache_proc_entries(struct cache_detail *cd)
1464 struct proc_dir_entry *p;
1466 cd->u.procfs.proc_ent = proc_mkdir(cd->name, proc_net_rpc);
1467 if (cd->u.procfs.proc_ent == NULL)
1468 goto out_nomem;
1469 cd->u.procfs.channel_ent = NULL;
1470 cd->u.procfs.content_ent = NULL;
1472 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1473 cd->u.procfs.proc_ent,
1474 &cache_flush_operations_procfs, cd);
1475 cd->u.procfs.flush_ent = p;
1476 if (p == NULL)
1477 goto out_nomem;
1479 if (cd->cache_upcall || cd->cache_parse) {
1480 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1481 cd->u.procfs.proc_ent,
1482 &cache_file_operations_procfs, cd);
1483 cd->u.procfs.channel_ent = p;
1484 if (p == NULL)
1485 goto out_nomem;
1487 if (cd->cache_show) {
1488 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1489 cd->u.procfs.proc_ent,
1490 &content_file_operations_procfs, cd);
1491 cd->u.procfs.content_ent = p;
1492 if (p == NULL)
1493 goto out_nomem;
1495 return 0;
1496 out_nomem:
1497 remove_cache_proc_entries(cd);
1498 return -ENOMEM;
1500 #else /* CONFIG_PROC_FS */
1501 static int create_cache_proc_entries(struct cache_detail *cd)
1503 return 0;
1505 #endif
1507 int cache_register(struct cache_detail *cd)
1509 int ret;
1511 sunrpc_init_cache_detail(cd);
1512 ret = create_cache_proc_entries(cd);
1513 if (ret)
1514 sunrpc_destroy_cache_detail(cd);
1515 return ret;
1517 EXPORT_SYMBOL_GPL(cache_register);
1519 void cache_unregister(struct cache_detail *cd)
1521 remove_cache_proc_entries(cd);
1522 sunrpc_destroy_cache_detail(cd);
1524 EXPORT_SYMBOL_GPL(cache_unregister);
1526 static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1527 size_t count, loff_t *ppos)
1529 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1531 return cache_read(filp, buf, count, ppos, cd);
1534 static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1535 size_t count, loff_t *ppos)
1537 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1539 return cache_write(filp, buf, count, ppos, cd);
1542 static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1544 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1546 return cache_poll(filp, wait, cd);
1549 static long cache_ioctl_pipefs(struct file *filp,
1550 unsigned int cmd, unsigned long arg)
1552 struct inode *inode = filp->f_dentry->d_inode;
1553 struct cache_detail *cd = RPC_I(inode)->private;
1554 long ret;
1556 lock_kernel();
1557 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1558 unlock_kernel();
1560 return ret;
1563 static int cache_open_pipefs(struct inode *inode, struct file *filp)
1565 struct cache_detail *cd = RPC_I(inode)->private;
1567 return cache_open(inode, filp, cd);
1570 static int cache_release_pipefs(struct inode *inode, struct file *filp)
1572 struct cache_detail *cd = RPC_I(inode)->private;
1574 return cache_release(inode, filp, cd);
1577 const struct file_operations cache_file_operations_pipefs = {
1578 .owner = THIS_MODULE,
1579 .llseek = no_llseek,
1580 .read = cache_read_pipefs,
1581 .write = cache_write_pipefs,
1582 .poll = cache_poll_pipefs,
1583 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
1584 .open = cache_open_pipefs,
1585 .release = cache_release_pipefs,
1588 static int content_open_pipefs(struct inode *inode, struct file *filp)
1590 struct cache_detail *cd = RPC_I(inode)->private;
1592 return content_open(inode, filp, cd);
1595 static int content_release_pipefs(struct inode *inode, struct file *filp)
1597 struct cache_detail *cd = RPC_I(inode)->private;
1599 return content_release(inode, filp, cd);
1602 const struct file_operations content_file_operations_pipefs = {
1603 .open = content_open_pipefs,
1604 .read = seq_read,
1605 .llseek = seq_lseek,
1606 .release = content_release_pipefs,
1609 static int open_flush_pipefs(struct inode *inode, struct file *filp)
1611 struct cache_detail *cd = RPC_I(inode)->private;
1613 return open_flush(inode, filp, cd);
1616 static int release_flush_pipefs(struct inode *inode, struct file *filp)
1618 struct cache_detail *cd = RPC_I(inode)->private;
1620 return release_flush(inode, filp, cd);
1623 static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1624 size_t count, loff_t *ppos)
1626 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1628 return read_flush(filp, buf, count, ppos, cd);
1631 static ssize_t write_flush_pipefs(struct file *filp,
1632 const char __user *buf,
1633 size_t count, loff_t *ppos)
1635 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1637 return write_flush(filp, buf, count, ppos, cd);
1640 const struct file_operations cache_flush_operations_pipefs = {
1641 .open = open_flush_pipefs,
1642 .read = read_flush_pipefs,
1643 .write = write_flush_pipefs,
1644 .release = release_flush_pipefs,
1647 int sunrpc_cache_register_pipefs(struct dentry *parent,
1648 const char *name, mode_t umode,
1649 struct cache_detail *cd)
1651 struct qstr q;
1652 struct dentry *dir;
1653 int ret = 0;
1655 sunrpc_init_cache_detail(cd);
1656 q.name = name;
1657 q.len = strlen(name);
1658 q.hash = full_name_hash(q.name, q.len);
1659 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1660 if (!IS_ERR(dir))
1661 cd->u.pipefs.dir = dir;
1662 else {
1663 sunrpc_destroy_cache_detail(cd);
1664 ret = PTR_ERR(dir);
1666 return ret;
1668 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1670 void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1672 rpc_remove_cache_dir(cd->u.pipefs.dir);
1673 cd->u.pipefs.dir = NULL;
1674 sunrpc_destroy_cache_detail(cd);
1676 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);