serial: fix race between flush_to_ldisc and tty_open
[linux-stable.git] / net / sunrpc / cache.c
blobf2cf4edf219bd128802ea18d110fee63eeb950e0
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 <linux/string_helpers.h>
24 #include <linux/uaccess.h>
25 #include <linux/poll.h>
26 #include <linux/seq_file.h>
27 #include <linux/proc_fs.h>
28 #include <linux/net.h>
29 #include <linux/workqueue.h>
30 #include <linux/mutex.h>
31 #include <linux/pagemap.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 "netns.h"
39 #define RPCDBG_FACILITY RPCDBG_CACHE
41 static bool 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, struct cache_detail *detail)
46 time_t now = seconds_since_boot();
47 INIT_HLIST_NODE(&h->cache_list);
48 h->flags = 0;
49 kref_init(&h->ref);
50 h->expiry_time = now + CACHE_NEW_EXPIRY;
51 if (now <= detail->flush_time)
52 /* ensure it isn't already expired */
53 now = detail->flush_time + 1;
54 h->last_refresh = now;
57 static void cache_fresh_locked(struct cache_head *head, time_t expiry,
58 struct cache_detail *detail);
59 static void cache_fresh_unlocked(struct cache_head *head,
60 struct cache_detail *detail);
62 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
63 struct cache_head *key, int hash)
65 struct cache_head *new = NULL, *freeme = NULL, *tmp = NULL;
66 struct hlist_head *head;
68 head = &detail->hash_table[hash];
70 read_lock(&detail->hash_lock);
72 hlist_for_each_entry(tmp, head, cache_list) {
73 if (detail->match(tmp, key)) {
74 if (cache_is_expired(detail, tmp))
75 /* This entry is expired, we will discard it. */
76 break;
77 cache_get(tmp);
78 read_unlock(&detail->hash_lock);
79 return tmp;
82 read_unlock(&detail->hash_lock);
83 /* Didn't find anything, insert an empty entry */
85 new = detail->alloc();
86 if (!new)
87 return NULL;
88 /* must fully initialise 'new', else
89 * we might get lose if we need to
90 * cache_put it soon.
92 cache_init(new, detail);
93 detail->init(new, key);
95 write_lock(&detail->hash_lock);
97 /* check if entry appeared while we slept */
98 hlist_for_each_entry(tmp, head, cache_list) {
99 if (detail->match(tmp, key)) {
100 if (cache_is_expired(detail, tmp)) {
101 hlist_del_init(&tmp->cache_list);
102 detail->entries --;
103 cache_fresh_locked(tmp, 0, detail);
104 freeme = tmp;
105 break;
107 cache_get(tmp);
108 write_unlock(&detail->hash_lock);
109 cache_put(new, detail);
110 return tmp;
114 hlist_add_head(&new->cache_list, head);
115 detail->entries++;
116 cache_get(new);
117 write_unlock(&detail->hash_lock);
119 if (freeme) {
120 cache_fresh_unlocked(freeme, detail);
121 cache_put(freeme, detail);
123 return new;
125 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
128 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
130 static void cache_fresh_locked(struct cache_head *head, time_t expiry,
131 struct cache_detail *detail)
133 time_t now = seconds_since_boot();
134 if (now <= detail->flush_time)
135 /* ensure it isn't immediately treated as expired */
136 now = detail->flush_time + 1;
137 head->expiry_time = expiry;
138 head->last_refresh = now;
139 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
140 set_bit(CACHE_VALID, &head->flags);
143 static void cache_fresh_unlocked(struct cache_head *head,
144 struct cache_detail *detail)
146 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
147 cache_revisit_request(head);
148 cache_dequeue(detail, head);
152 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
153 struct cache_head *new, struct cache_head *old, int hash)
155 /* The 'old' entry is to be replaced by 'new'.
156 * If 'old' is not VALID, we update it directly,
157 * otherwise we need to replace it
159 struct cache_head *tmp;
161 if (!test_bit(CACHE_VALID, &old->flags)) {
162 write_lock(&detail->hash_lock);
163 if (!test_bit(CACHE_VALID, &old->flags)) {
164 if (test_bit(CACHE_NEGATIVE, &new->flags))
165 set_bit(CACHE_NEGATIVE, &old->flags);
166 else
167 detail->update(old, new);
168 cache_fresh_locked(old, new->expiry_time, detail);
169 write_unlock(&detail->hash_lock);
170 cache_fresh_unlocked(old, detail);
171 return old;
173 write_unlock(&detail->hash_lock);
175 /* We need to insert a new entry */
176 tmp = detail->alloc();
177 if (!tmp) {
178 cache_put(old, detail);
179 return NULL;
181 cache_init(tmp, detail);
182 detail->init(tmp, old);
184 write_lock(&detail->hash_lock);
185 if (test_bit(CACHE_NEGATIVE, &new->flags))
186 set_bit(CACHE_NEGATIVE, &tmp->flags);
187 else
188 detail->update(tmp, new);
189 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
190 detail->entries++;
191 cache_get(tmp);
192 cache_fresh_locked(tmp, new->expiry_time, detail);
193 cache_fresh_locked(old, 0, detail);
194 write_unlock(&detail->hash_lock);
195 cache_fresh_unlocked(tmp, detail);
196 cache_fresh_unlocked(old, detail);
197 cache_put(old, detail);
198 return tmp;
200 EXPORT_SYMBOL_GPL(sunrpc_cache_update);
202 static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
204 if (cd->cache_upcall)
205 return cd->cache_upcall(cd, h);
206 return sunrpc_cache_pipe_upcall(cd, h);
209 static inline int cache_is_valid(struct cache_head *h)
211 if (!test_bit(CACHE_VALID, &h->flags))
212 return -EAGAIN;
213 else {
214 /* entry is valid */
215 if (test_bit(CACHE_NEGATIVE, &h->flags))
216 return -ENOENT;
217 else {
219 * In combination with write barrier in
220 * sunrpc_cache_update, ensures that anyone
221 * using the cache entry after this sees the
222 * updated contents:
224 smp_rmb();
225 return 0;
230 static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
232 int rv;
234 write_lock(&detail->hash_lock);
235 rv = cache_is_valid(h);
236 if (rv == -EAGAIN) {
237 set_bit(CACHE_NEGATIVE, &h->flags);
238 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
239 detail);
240 rv = -ENOENT;
242 write_unlock(&detail->hash_lock);
243 cache_fresh_unlocked(h, detail);
244 return rv;
248 * This is the generic cache management routine for all
249 * the authentication caches.
250 * It checks the currency of a cache item and will (later)
251 * initiate an upcall to fill it if needed.
254 * Returns 0 if the cache_head can be used, or cache_puts it and returns
255 * -EAGAIN if upcall is pending and request has been queued
256 * -ETIMEDOUT if upcall failed or request could not be queue or
257 * upcall completed but item is still invalid (implying that
258 * the cache item has been replaced with a newer one).
259 * -ENOENT if cache entry was negative
261 int cache_check(struct cache_detail *detail,
262 struct cache_head *h, struct cache_req *rqstp)
264 int rv;
265 long refresh_age, age;
267 /* First decide return status as best we can */
268 rv = cache_is_valid(h);
270 /* now see if we want to start an upcall */
271 refresh_age = (h->expiry_time - h->last_refresh);
272 age = seconds_since_boot() - h->last_refresh;
274 if (rqstp == NULL) {
275 if (rv == -EAGAIN)
276 rv = -ENOENT;
277 } else if (rv == -EAGAIN ||
278 (h->expiry_time != 0 && age > refresh_age/2)) {
279 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
280 refresh_age, age);
281 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
282 switch (cache_make_upcall(detail, h)) {
283 case -EINVAL:
284 rv = try_to_negate_entry(detail, h);
285 break;
286 case -EAGAIN:
287 cache_fresh_unlocked(h, detail);
288 break;
293 if (rv == -EAGAIN) {
294 if (!cache_defer_req(rqstp, h)) {
296 * Request was not deferred; handle it as best
297 * we can ourselves:
299 rv = cache_is_valid(h);
300 if (rv == -EAGAIN)
301 rv = -ETIMEDOUT;
304 if (rv)
305 cache_put(h, detail);
306 return rv;
308 EXPORT_SYMBOL_GPL(cache_check);
311 * caches need to be periodically cleaned.
312 * For this we maintain a list of cache_detail and
313 * a current pointer into that list and into the table
314 * for that entry.
316 * Each time cache_clean is called it finds the next non-empty entry
317 * in the current table and walks the list in that entry
318 * looking for entries that can be removed.
320 * An entry gets removed if:
321 * - The expiry is before current time
322 * - The last_refresh time is before the flush_time for that cache
324 * later we might drop old entries with non-NEVER expiry if that table
325 * is getting 'full' for some definition of 'full'
327 * The question of "how often to scan a table" is an interesting one
328 * and is answered in part by the use of the "nextcheck" field in the
329 * cache_detail.
330 * When a scan of a table begins, the nextcheck field is set to a time
331 * that is well into the future.
332 * While scanning, if an expiry time is found that is earlier than the
333 * current nextcheck time, nextcheck is set to that expiry time.
334 * If the flush_time is ever set to a time earlier than the nextcheck
335 * time, the nextcheck time is then set to that flush_time.
337 * A table is then only scanned if the current time is at least
338 * the nextcheck time.
342 static LIST_HEAD(cache_list);
343 static DEFINE_SPINLOCK(cache_list_lock);
344 static struct cache_detail *current_detail;
345 static int current_index;
347 static void do_cache_clean(struct work_struct *work);
348 static struct delayed_work cache_cleaner;
350 void sunrpc_init_cache_detail(struct cache_detail *cd)
352 rwlock_init(&cd->hash_lock);
353 INIT_LIST_HEAD(&cd->queue);
354 spin_lock(&cache_list_lock);
355 cd->nextcheck = 0;
356 cd->entries = 0;
357 atomic_set(&cd->readers, 0);
358 cd->last_close = 0;
359 cd->last_warn = -1;
360 list_add(&cd->others, &cache_list);
361 spin_unlock(&cache_list_lock);
363 /* start the cleaning process */
364 queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
366 EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
368 void sunrpc_destroy_cache_detail(struct cache_detail *cd)
370 cache_purge(cd);
371 spin_lock(&cache_list_lock);
372 write_lock(&cd->hash_lock);
373 if (current_detail == cd)
374 current_detail = NULL;
375 list_del_init(&cd->others);
376 write_unlock(&cd->hash_lock);
377 spin_unlock(&cache_list_lock);
378 if (list_empty(&cache_list)) {
379 /* module must be being unloaded so its safe to kill the worker */
380 cancel_delayed_work_sync(&cache_cleaner);
383 EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
385 /* clean cache tries to find something to clean
386 * and cleans it.
387 * It returns 1 if it cleaned something,
388 * 0 if it didn't find anything this time
389 * -1 if it fell off the end of the list.
391 static int cache_clean(void)
393 int rv = 0;
394 struct list_head *next;
396 spin_lock(&cache_list_lock);
398 /* find a suitable table if we don't already have one */
399 while (current_detail == NULL ||
400 current_index >= current_detail->hash_size) {
401 if (current_detail)
402 next = current_detail->others.next;
403 else
404 next = cache_list.next;
405 if (next == &cache_list) {
406 current_detail = NULL;
407 spin_unlock(&cache_list_lock);
408 return -1;
410 current_detail = list_entry(next, struct cache_detail, others);
411 if (current_detail->nextcheck > seconds_since_boot())
412 current_index = current_detail->hash_size;
413 else {
414 current_index = 0;
415 current_detail->nextcheck = seconds_since_boot()+30*60;
419 /* find a non-empty bucket in the table */
420 while (current_detail &&
421 current_index < current_detail->hash_size &&
422 hlist_empty(&current_detail->hash_table[current_index]))
423 current_index++;
425 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
427 if (current_detail && current_index < current_detail->hash_size) {
428 struct cache_head *ch = NULL;
429 struct cache_detail *d;
430 struct hlist_head *head;
431 struct hlist_node *tmp;
433 write_lock(&current_detail->hash_lock);
435 /* Ok, now to clean this strand */
437 head = &current_detail->hash_table[current_index];
438 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
439 if (current_detail->nextcheck > ch->expiry_time)
440 current_detail->nextcheck = ch->expiry_time+1;
441 if (!cache_is_expired(current_detail, ch))
442 continue;
444 hlist_del_init(&ch->cache_list);
445 current_detail->entries--;
446 rv = 1;
447 break;
450 write_unlock(&current_detail->hash_lock);
451 d = current_detail;
452 if (!ch)
453 current_index ++;
454 spin_unlock(&cache_list_lock);
455 if (ch) {
456 set_bit(CACHE_CLEANED, &ch->flags);
457 cache_fresh_unlocked(ch, d);
458 cache_put(ch, d);
460 } else
461 spin_unlock(&cache_list_lock);
463 return rv;
467 * We want to regularly clean the cache, so we need to schedule some work ...
469 static void do_cache_clean(struct work_struct *work)
471 int delay = 5;
472 if (cache_clean() == -1)
473 delay = round_jiffies_relative(30*HZ);
475 if (list_empty(&cache_list))
476 delay = 0;
478 if (delay)
479 queue_delayed_work(system_power_efficient_wq,
480 &cache_cleaner, delay);
485 * Clean all caches promptly. This just calls cache_clean
486 * repeatedly until we are sure that every cache has had a chance to
487 * be fully cleaned
489 void cache_flush(void)
491 while (cache_clean() != -1)
492 cond_resched();
493 while (cache_clean() != -1)
494 cond_resched();
496 EXPORT_SYMBOL_GPL(cache_flush);
498 void cache_purge(struct cache_detail *detail)
500 struct cache_head *ch = NULL;
501 struct hlist_head *head = NULL;
502 struct hlist_node *tmp = NULL;
503 int i = 0;
505 write_lock(&detail->hash_lock);
506 if (!detail->entries) {
507 write_unlock(&detail->hash_lock);
508 return;
511 dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name);
512 for (i = 0; i < detail->hash_size; i++) {
513 head = &detail->hash_table[i];
514 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
515 hlist_del_init(&ch->cache_list);
516 detail->entries--;
518 set_bit(CACHE_CLEANED, &ch->flags);
519 write_unlock(&detail->hash_lock);
520 cache_fresh_unlocked(ch, detail);
521 cache_put(ch, detail);
522 write_lock(&detail->hash_lock);
525 write_unlock(&detail->hash_lock);
527 EXPORT_SYMBOL_GPL(cache_purge);
531 * Deferral and Revisiting of Requests.
533 * If a cache lookup finds a pending entry, we
534 * need to defer the request and revisit it later.
535 * All deferred requests are stored in a hash table,
536 * indexed by "struct cache_head *".
537 * As it may be wasteful to store a whole request
538 * structure, we allow the request to provide a
539 * deferred form, which must contain a
540 * 'struct cache_deferred_req'
541 * This cache_deferred_req contains a method to allow
542 * it to be revisited when cache info is available
545 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
546 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
548 #define DFR_MAX 300 /* ??? */
550 static DEFINE_SPINLOCK(cache_defer_lock);
551 static LIST_HEAD(cache_defer_list);
552 static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
553 static int cache_defer_cnt;
555 static void __unhash_deferred_req(struct cache_deferred_req *dreq)
557 hlist_del_init(&dreq->hash);
558 if (!list_empty(&dreq->recent)) {
559 list_del_init(&dreq->recent);
560 cache_defer_cnt--;
564 static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
566 int hash = DFR_HASH(item);
568 INIT_LIST_HEAD(&dreq->recent);
569 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
572 static void setup_deferral(struct cache_deferred_req *dreq,
573 struct cache_head *item,
574 int count_me)
577 dreq->item = item;
579 spin_lock(&cache_defer_lock);
581 __hash_deferred_req(dreq, item);
583 if (count_me) {
584 cache_defer_cnt++;
585 list_add(&dreq->recent, &cache_defer_list);
588 spin_unlock(&cache_defer_lock);
592 struct thread_deferred_req {
593 struct cache_deferred_req handle;
594 struct completion completion;
597 static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
599 struct thread_deferred_req *dr =
600 container_of(dreq, struct thread_deferred_req, handle);
601 complete(&dr->completion);
604 static void cache_wait_req(struct cache_req *req, struct cache_head *item)
606 struct thread_deferred_req sleeper;
607 struct cache_deferred_req *dreq = &sleeper.handle;
609 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
610 dreq->revisit = cache_restart_thread;
612 setup_deferral(dreq, item, 0);
614 if (!test_bit(CACHE_PENDING, &item->flags) ||
615 wait_for_completion_interruptible_timeout(
616 &sleeper.completion, req->thread_wait) <= 0) {
617 /* The completion wasn't completed, so we need
618 * to clean up
620 spin_lock(&cache_defer_lock);
621 if (!hlist_unhashed(&sleeper.handle.hash)) {
622 __unhash_deferred_req(&sleeper.handle);
623 spin_unlock(&cache_defer_lock);
624 } else {
625 /* cache_revisit_request already removed
626 * this from the hash table, but hasn't
627 * called ->revisit yet. It will very soon
628 * and we need to wait for it.
630 spin_unlock(&cache_defer_lock);
631 wait_for_completion(&sleeper.completion);
636 static void cache_limit_defers(void)
638 /* Make sure we haven't exceed the limit of allowed deferred
639 * requests.
641 struct cache_deferred_req *discard = NULL;
643 if (cache_defer_cnt <= DFR_MAX)
644 return;
646 spin_lock(&cache_defer_lock);
648 /* Consider removing either the first or the last */
649 if (cache_defer_cnt > DFR_MAX) {
650 if (prandom_u32() & 1)
651 discard = list_entry(cache_defer_list.next,
652 struct cache_deferred_req, recent);
653 else
654 discard = list_entry(cache_defer_list.prev,
655 struct cache_deferred_req, recent);
656 __unhash_deferred_req(discard);
658 spin_unlock(&cache_defer_lock);
659 if (discard)
660 discard->revisit(discard, 1);
663 /* Return true if and only if a deferred request is queued. */
664 static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
666 struct cache_deferred_req *dreq;
668 if (req->thread_wait) {
669 cache_wait_req(req, item);
670 if (!test_bit(CACHE_PENDING, &item->flags))
671 return false;
673 dreq = req->defer(req);
674 if (dreq == NULL)
675 return false;
676 setup_deferral(dreq, item, 1);
677 if (!test_bit(CACHE_PENDING, &item->flags))
678 /* Bit could have been cleared before we managed to
679 * set up the deferral, so need to revisit just in case
681 cache_revisit_request(item);
683 cache_limit_defers();
684 return true;
687 static void cache_revisit_request(struct cache_head *item)
689 struct cache_deferred_req *dreq;
690 struct list_head pending;
691 struct hlist_node *tmp;
692 int hash = DFR_HASH(item);
694 INIT_LIST_HEAD(&pending);
695 spin_lock(&cache_defer_lock);
697 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
698 if (dreq->item == item) {
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, 0);
712 void cache_clean_deferred(void *owner)
714 struct cache_deferred_req *dreq, *tmp;
715 struct list_head pending;
718 INIT_LIST_HEAD(&pending);
719 spin_lock(&cache_defer_lock);
721 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
722 if (dreq->owner == owner) {
723 __unhash_deferred_req(dreq);
724 list_add(&dreq->recent, &pending);
727 spin_unlock(&cache_defer_lock);
729 while (!list_empty(&pending)) {
730 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
731 list_del_init(&dreq->recent);
732 dreq->revisit(dreq, 1);
737 * communicate with user-space
739 * We have a magic /proc file - /proc/net/rpc/<cachename>/channel.
740 * On read, you get a full request, or block.
741 * On write, an update request is processed.
742 * Poll works if anything to read, and always allows write.
744 * Implemented by linked list of requests. Each open file has
745 * a ->private that also exists in this list. New requests are added
746 * to the end and may wakeup and preceding readers.
747 * New readers are added to the head. If, on read, an item is found with
748 * CACHE_UPCALLING clear, we free it from the list.
752 static DEFINE_SPINLOCK(queue_lock);
753 static DEFINE_MUTEX(queue_io_mutex);
755 struct cache_queue {
756 struct list_head list;
757 int reader; /* if 0, then request */
759 struct cache_request {
760 struct cache_queue q;
761 struct cache_head *item;
762 char * buf;
763 int len;
764 int readers;
766 struct cache_reader {
767 struct cache_queue q;
768 int offset; /* if non-0, we have a refcnt on next request */
771 static int cache_request(struct cache_detail *detail,
772 struct cache_request *crq)
774 char *bp = crq->buf;
775 int len = PAGE_SIZE;
777 detail->cache_request(detail, crq->item, &bp, &len);
778 if (len < 0)
779 return -EAGAIN;
780 return PAGE_SIZE - len;
783 static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
784 loff_t *ppos, struct cache_detail *cd)
786 struct cache_reader *rp = filp->private_data;
787 struct cache_request *rq;
788 struct inode *inode = file_inode(filp);
789 int err;
791 if (count == 0)
792 return 0;
794 inode_lock(inode); /* protect against multiple concurrent
795 * readers on this file */
796 again:
797 spin_lock(&queue_lock);
798 /* need to find next request */
799 while (rp->q.list.next != &cd->queue &&
800 list_entry(rp->q.list.next, struct cache_queue, list)
801 ->reader) {
802 struct list_head *next = rp->q.list.next;
803 list_move(&rp->q.list, next);
805 if (rp->q.list.next == &cd->queue) {
806 spin_unlock(&queue_lock);
807 inode_unlock(inode);
808 WARN_ON_ONCE(rp->offset);
809 return 0;
811 rq = container_of(rp->q.list.next, struct cache_request, q.list);
812 WARN_ON_ONCE(rq->q.reader);
813 if (rp->offset == 0)
814 rq->readers++;
815 spin_unlock(&queue_lock);
817 if (rq->len == 0) {
818 err = cache_request(cd, rq);
819 if (err < 0)
820 goto out;
821 rq->len = err;
824 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
825 err = -EAGAIN;
826 spin_lock(&queue_lock);
827 list_move(&rp->q.list, &rq->q.list);
828 spin_unlock(&queue_lock);
829 } else {
830 if (rp->offset + count > rq->len)
831 count = rq->len - rp->offset;
832 err = -EFAULT;
833 if (copy_to_user(buf, rq->buf + rp->offset, count))
834 goto out;
835 rp->offset += count;
836 if (rp->offset >= rq->len) {
837 rp->offset = 0;
838 spin_lock(&queue_lock);
839 list_move(&rp->q.list, &rq->q.list);
840 spin_unlock(&queue_lock);
842 err = 0;
844 out:
845 if (rp->offset == 0) {
846 /* need to release rq */
847 spin_lock(&queue_lock);
848 rq->readers--;
849 if (rq->readers == 0 &&
850 !test_bit(CACHE_PENDING, &rq->item->flags)) {
851 list_del(&rq->q.list);
852 spin_unlock(&queue_lock);
853 cache_put(rq->item, cd);
854 kfree(rq->buf);
855 kfree(rq);
856 } else
857 spin_unlock(&queue_lock);
859 if (err == -EAGAIN)
860 goto again;
861 inode_unlock(inode);
862 return err ? err : count;
865 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
866 size_t count, struct cache_detail *cd)
868 ssize_t ret;
870 if (count == 0)
871 return -EINVAL;
872 if (copy_from_user(kaddr, buf, count))
873 return -EFAULT;
874 kaddr[count] = '\0';
875 ret = cd->cache_parse(cd, kaddr, count);
876 if (!ret)
877 ret = count;
878 return ret;
881 static ssize_t cache_slow_downcall(const char __user *buf,
882 size_t count, struct cache_detail *cd)
884 static char write_buf[8192]; /* protected by queue_io_mutex */
885 ssize_t ret = -EINVAL;
887 if (count >= sizeof(write_buf))
888 goto out;
889 mutex_lock(&queue_io_mutex);
890 ret = cache_do_downcall(write_buf, buf, count, cd);
891 mutex_unlock(&queue_io_mutex);
892 out:
893 return ret;
896 static ssize_t cache_downcall(struct address_space *mapping,
897 const char __user *buf,
898 size_t count, struct cache_detail *cd)
900 struct page *page;
901 char *kaddr;
902 ssize_t ret = -ENOMEM;
904 if (count >= PAGE_SIZE)
905 goto out_slow;
907 page = find_or_create_page(mapping, 0, GFP_KERNEL);
908 if (!page)
909 goto out_slow;
911 kaddr = kmap(page);
912 ret = cache_do_downcall(kaddr, buf, count, cd);
913 kunmap(page);
914 unlock_page(page);
915 put_page(page);
916 return ret;
917 out_slow:
918 return cache_slow_downcall(buf, count, cd);
921 static ssize_t cache_write(struct file *filp, const char __user *buf,
922 size_t count, loff_t *ppos,
923 struct cache_detail *cd)
925 struct address_space *mapping = filp->f_mapping;
926 struct inode *inode = file_inode(filp);
927 ssize_t ret = -EINVAL;
929 if (!cd->cache_parse)
930 goto out;
932 inode_lock(inode);
933 ret = cache_downcall(mapping, buf, count, cd);
934 inode_unlock(inode);
935 out:
936 return ret;
939 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
941 static unsigned int cache_poll(struct file *filp, poll_table *wait,
942 struct cache_detail *cd)
944 unsigned int mask;
945 struct cache_reader *rp = filp->private_data;
946 struct cache_queue *cq;
948 poll_wait(filp, &queue_wait, wait);
950 /* alway allow write */
951 mask = POLLOUT | POLLWRNORM;
953 if (!rp)
954 return mask;
956 spin_lock(&queue_lock);
958 for (cq= &rp->q; &cq->list != &cd->queue;
959 cq = list_entry(cq->list.next, struct cache_queue, list))
960 if (!cq->reader) {
961 mask |= POLLIN | POLLRDNORM;
962 break;
964 spin_unlock(&queue_lock);
965 return mask;
968 static int cache_ioctl(struct inode *ino, struct file *filp,
969 unsigned int cmd, unsigned long arg,
970 struct cache_detail *cd)
972 int len = 0;
973 struct cache_reader *rp = filp->private_data;
974 struct cache_queue *cq;
976 if (cmd != FIONREAD || !rp)
977 return -EINVAL;
979 spin_lock(&queue_lock);
981 /* only find the length remaining in current request,
982 * or the length of the next request
984 for (cq= &rp->q; &cq->list != &cd->queue;
985 cq = list_entry(cq->list.next, struct cache_queue, list))
986 if (!cq->reader) {
987 struct cache_request *cr =
988 container_of(cq, struct cache_request, q);
989 len = cr->len - rp->offset;
990 break;
992 spin_unlock(&queue_lock);
994 return put_user(len, (int __user *)arg);
997 static int cache_open(struct inode *inode, struct file *filp,
998 struct cache_detail *cd)
1000 struct cache_reader *rp = NULL;
1002 if (!cd || !try_module_get(cd->owner))
1003 return -EACCES;
1004 nonseekable_open(inode, filp);
1005 if (filp->f_mode & FMODE_READ) {
1006 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
1007 if (!rp) {
1008 module_put(cd->owner);
1009 return -ENOMEM;
1011 rp->offset = 0;
1012 rp->q.reader = 1;
1013 atomic_inc(&cd->readers);
1014 spin_lock(&queue_lock);
1015 list_add(&rp->q.list, &cd->queue);
1016 spin_unlock(&queue_lock);
1018 filp->private_data = rp;
1019 return 0;
1022 static int cache_release(struct inode *inode, struct file *filp,
1023 struct cache_detail *cd)
1025 struct cache_reader *rp = filp->private_data;
1027 if (rp) {
1028 spin_lock(&queue_lock);
1029 if (rp->offset) {
1030 struct cache_queue *cq;
1031 for (cq= &rp->q; &cq->list != &cd->queue;
1032 cq = list_entry(cq->list.next, struct cache_queue, list))
1033 if (!cq->reader) {
1034 container_of(cq, struct cache_request, q)
1035 ->readers--;
1036 break;
1038 rp->offset = 0;
1040 list_del(&rp->q.list);
1041 spin_unlock(&queue_lock);
1043 filp->private_data = NULL;
1044 kfree(rp);
1046 cd->last_close = seconds_since_boot();
1047 atomic_dec(&cd->readers);
1049 module_put(cd->owner);
1050 return 0;
1055 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
1057 struct cache_queue *cq, *tmp;
1058 struct cache_request *cr;
1059 struct list_head dequeued;
1061 INIT_LIST_HEAD(&dequeued);
1062 spin_lock(&queue_lock);
1063 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
1064 if (!cq->reader) {
1065 cr = container_of(cq, struct cache_request, q);
1066 if (cr->item != ch)
1067 continue;
1068 if (test_bit(CACHE_PENDING, &ch->flags))
1069 /* Lost a race and it is pending again */
1070 break;
1071 if (cr->readers != 0)
1072 continue;
1073 list_move(&cr->q.list, &dequeued);
1075 spin_unlock(&queue_lock);
1076 while (!list_empty(&dequeued)) {
1077 cr = list_entry(dequeued.next, struct cache_request, q.list);
1078 list_del(&cr->q.list);
1079 cache_put(cr->item, detail);
1080 kfree(cr->buf);
1081 kfree(cr);
1086 * Support routines for text-based upcalls.
1087 * Fields are separated by spaces.
1088 * Fields are either mangled to quote space tab newline slosh with slosh
1089 * or a hexified with a leading \x
1090 * Record is terminated with newline.
1094 void qword_add(char **bpp, int *lp, char *str)
1096 char *bp = *bpp;
1097 int len = *lp;
1098 int ret;
1100 if (len < 0) return;
1102 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1103 if (ret >= len) {
1104 bp += len;
1105 len = -1;
1106 } else {
1107 bp += ret;
1108 len -= ret;
1109 *bp++ = ' ';
1110 len--;
1112 *bpp = bp;
1113 *lp = len;
1115 EXPORT_SYMBOL_GPL(qword_add);
1117 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1119 char *bp = *bpp;
1120 int len = *lp;
1122 if (len < 0) return;
1124 if (len > 2) {
1125 *bp++ = '\\';
1126 *bp++ = 'x';
1127 len -= 2;
1128 while (blen && len >= 2) {
1129 bp = hex_byte_pack(bp, *buf++);
1130 len -= 2;
1131 blen--;
1134 if (blen || len<1) len = -1;
1135 else {
1136 *bp++ = ' ';
1137 len--;
1139 *bpp = bp;
1140 *lp = len;
1142 EXPORT_SYMBOL_GPL(qword_addhex);
1144 static void warn_no_listener(struct cache_detail *detail)
1146 if (detail->last_warn != detail->last_close) {
1147 detail->last_warn = detail->last_close;
1148 if (detail->warn_no_listener)
1149 detail->warn_no_listener(detail, detail->last_close != 0);
1153 static bool cache_listeners_exist(struct cache_detail *detail)
1155 if (atomic_read(&detail->readers))
1156 return true;
1157 if (detail->last_close == 0)
1158 /* This cache was never opened */
1159 return false;
1160 if (detail->last_close < seconds_since_boot() - 30)
1162 * We allow for the possibility that someone might
1163 * restart a userspace daemon without restarting the
1164 * server; but after 30 seconds, we give up.
1166 return false;
1167 return true;
1171 * register an upcall request to user-space and queue it up for read() by the
1172 * upcall daemon.
1174 * Each request is at most one page long.
1176 int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
1179 char *buf;
1180 struct cache_request *crq;
1181 int ret = 0;
1183 if (!detail->cache_request)
1184 return -EINVAL;
1186 if (!cache_listeners_exist(detail)) {
1187 warn_no_listener(detail);
1188 return -EINVAL;
1190 if (test_bit(CACHE_CLEANED, &h->flags))
1191 /* Too late to make an upcall */
1192 return -EAGAIN;
1194 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1195 if (!buf)
1196 return -EAGAIN;
1198 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1199 if (!crq) {
1200 kfree(buf);
1201 return -EAGAIN;
1204 crq->q.reader = 0;
1205 crq->buf = buf;
1206 crq->len = 0;
1207 crq->readers = 0;
1208 spin_lock(&queue_lock);
1209 if (test_bit(CACHE_PENDING, &h->flags)) {
1210 crq->item = cache_get(h);
1211 list_add_tail(&crq->q.list, &detail->queue);
1212 } else
1213 /* Lost a race, no longer PENDING, so don't enqueue */
1214 ret = -EAGAIN;
1215 spin_unlock(&queue_lock);
1216 wake_up(&queue_wait);
1217 if (ret == -EAGAIN) {
1218 kfree(buf);
1219 kfree(crq);
1221 return ret;
1223 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1226 * parse a message from user-space and pass it
1227 * to an appropriate cache
1228 * Messages are, like requests, separated into fields by
1229 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1231 * Message is
1232 * reply cachename expiry key ... content....
1234 * key and content are both parsed by cache
1237 int qword_get(char **bpp, char *dest, int bufsize)
1239 /* return bytes copied, or -1 on error */
1240 char *bp = *bpp;
1241 int len = 0;
1243 while (*bp == ' ') bp++;
1245 if (bp[0] == '\\' && bp[1] == 'x') {
1246 /* HEX STRING */
1247 bp += 2;
1248 while (len < bufsize - 1) {
1249 int h, l;
1251 h = hex_to_bin(bp[0]);
1252 if (h < 0)
1253 break;
1255 l = hex_to_bin(bp[1]);
1256 if (l < 0)
1257 break;
1259 *dest++ = (h << 4) | l;
1260 bp += 2;
1261 len++;
1263 } else {
1264 /* text with \nnn octal quoting */
1265 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1266 if (*bp == '\\' &&
1267 isodigit(bp[1]) && (bp[1] <= '3') &&
1268 isodigit(bp[2]) &&
1269 isodigit(bp[3])) {
1270 int byte = (*++bp -'0');
1271 bp++;
1272 byte = (byte << 3) | (*bp++ - '0');
1273 byte = (byte << 3) | (*bp++ - '0');
1274 *dest++ = byte;
1275 len++;
1276 } else {
1277 *dest++ = *bp++;
1278 len++;
1283 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1284 return -1;
1285 while (*bp == ' ') bp++;
1286 *bpp = bp;
1287 *dest = '\0';
1288 return len;
1290 EXPORT_SYMBOL_GPL(qword_get);
1294 * support /proc/net/rpc/$CACHENAME/content
1295 * as a seqfile.
1296 * We call ->cache_show passing NULL for the item to
1297 * get a header, then pass each real item in the cache
1300 void *cache_seq_start(struct seq_file *m, loff_t *pos)
1301 __acquires(cd->hash_lock)
1303 loff_t n = *pos;
1304 unsigned int hash, entry;
1305 struct cache_head *ch;
1306 struct cache_detail *cd = m->private;
1308 read_lock(&cd->hash_lock);
1309 if (!n--)
1310 return SEQ_START_TOKEN;
1311 hash = n >> 32;
1312 entry = n & ((1LL<<32) - 1);
1314 hlist_for_each_entry(ch, &cd->hash_table[hash], cache_list)
1315 if (!entry--)
1316 return ch;
1317 n &= ~((1LL<<32) - 1);
1318 do {
1319 hash++;
1320 n += 1LL<<32;
1321 } while(hash < cd->hash_size &&
1322 hlist_empty(&cd->hash_table[hash]));
1323 if (hash >= cd->hash_size)
1324 return NULL;
1325 *pos = n+1;
1326 return hlist_entry_safe(cd->hash_table[hash].first,
1327 struct cache_head, cache_list);
1329 EXPORT_SYMBOL_GPL(cache_seq_start);
1331 void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
1333 struct cache_head *ch = p;
1334 int hash = (*pos >> 32);
1335 struct cache_detail *cd = m->private;
1337 if (p == SEQ_START_TOKEN)
1338 hash = 0;
1339 else if (ch->cache_list.next == NULL) {
1340 hash++;
1341 *pos += 1LL<<32;
1342 } else {
1343 ++*pos;
1344 return hlist_entry_safe(ch->cache_list.next,
1345 struct cache_head, cache_list);
1347 *pos &= ~((1LL<<32) - 1);
1348 while (hash < cd->hash_size &&
1349 hlist_empty(&cd->hash_table[hash])) {
1350 hash++;
1351 *pos += 1LL<<32;
1353 if (hash >= cd->hash_size)
1354 return NULL;
1355 ++*pos;
1356 return hlist_entry_safe(cd->hash_table[hash].first,
1357 struct cache_head, cache_list);
1359 EXPORT_SYMBOL_GPL(cache_seq_next);
1361 void cache_seq_stop(struct seq_file *m, void *p)
1362 __releases(cd->hash_lock)
1364 struct cache_detail *cd = m->private;
1365 read_unlock(&cd->hash_lock);
1367 EXPORT_SYMBOL_GPL(cache_seq_stop);
1369 static int c_show(struct seq_file *m, void *p)
1371 struct cache_head *cp = p;
1372 struct cache_detail *cd = m->private;
1374 if (p == SEQ_START_TOKEN)
1375 return cd->cache_show(m, cd, NULL);
1377 ifdebug(CACHE)
1378 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1379 convert_to_wallclock(cp->expiry_time),
1380 kref_read(&cp->ref), cp->flags);
1381 cache_get(cp);
1382 if (cache_check(cd, cp, NULL))
1383 /* cache_check does a cache_put on failure */
1384 seq_printf(m, "# ");
1385 else {
1386 if (cache_is_expired(cd, cp))
1387 seq_printf(m, "# ");
1388 cache_put(cp, cd);
1391 return cd->cache_show(m, cd, cp);
1394 static const struct seq_operations cache_content_op = {
1395 .start = cache_seq_start,
1396 .next = cache_seq_next,
1397 .stop = cache_seq_stop,
1398 .show = c_show,
1401 static int content_open(struct inode *inode, struct file *file,
1402 struct cache_detail *cd)
1404 struct seq_file *seq;
1405 int err;
1407 if (!cd || !try_module_get(cd->owner))
1408 return -EACCES;
1410 err = seq_open(file, &cache_content_op);
1411 if (err) {
1412 module_put(cd->owner);
1413 return err;
1416 seq = file->private_data;
1417 seq->private = cd;
1418 return 0;
1421 static int content_release(struct inode *inode, struct file *file,
1422 struct cache_detail *cd)
1424 int ret = seq_release(inode, file);
1425 module_put(cd->owner);
1426 return ret;
1429 static int open_flush(struct inode *inode, struct file *file,
1430 struct cache_detail *cd)
1432 if (!cd || !try_module_get(cd->owner))
1433 return -EACCES;
1434 return nonseekable_open(inode, file);
1437 static int release_flush(struct inode *inode, struct file *file,
1438 struct cache_detail *cd)
1440 module_put(cd->owner);
1441 return 0;
1444 static ssize_t read_flush(struct file *file, char __user *buf,
1445 size_t count, loff_t *ppos,
1446 struct cache_detail *cd)
1448 char tbuf[22];
1449 size_t len;
1451 len = snprintf(tbuf, sizeof(tbuf), "%lu\n",
1452 convert_to_wallclock(cd->flush_time));
1453 return simple_read_from_buffer(buf, count, ppos, tbuf, len);
1456 static ssize_t write_flush(struct file *file, const char __user *buf,
1457 size_t count, loff_t *ppos,
1458 struct cache_detail *cd)
1460 char tbuf[20];
1461 char *bp, *ep;
1462 time_t then, now;
1464 if (*ppos || count > sizeof(tbuf)-1)
1465 return -EINVAL;
1466 if (copy_from_user(tbuf, buf, count))
1467 return -EFAULT;
1468 tbuf[count] = 0;
1469 simple_strtoul(tbuf, &ep, 0);
1470 if (*ep && *ep != '\n')
1471 return -EINVAL;
1473 bp = tbuf;
1474 then = get_expiry(&bp);
1475 now = seconds_since_boot();
1476 cd->nextcheck = now;
1477 /* Can only set flush_time to 1 second beyond "now", or
1478 * possibly 1 second beyond flushtime. This is because
1479 * flush_time never goes backwards so it mustn't get too far
1480 * ahead of time.
1482 if (then >= now) {
1483 /* Want to flush everything, so behave like cache_purge() */
1484 if (cd->flush_time >= now)
1485 now = cd->flush_time + 1;
1486 then = now;
1489 cd->flush_time = then;
1490 cache_flush();
1492 *ppos += count;
1493 return count;
1496 static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1497 size_t count, loff_t *ppos)
1499 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1501 return cache_read(filp, buf, count, ppos, cd);
1504 static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1505 size_t count, loff_t *ppos)
1507 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1509 return cache_write(filp, buf, count, ppos, cd);
1512 static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1514 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1516 return cache_poll(filp, wait, cd);
1519 static long cache_ioctl_procfs(struct file *filp,
1520 unsigned int cmd, unsigned long arg)
1522 struct inode *inode = file_inode(filp);
1523 struct cache_detail *cd = PDE_DATA(inode);
1525 return cache_ioctl(inode, filp, cmd, arg, cd);
1528 static int cache_open_procfs(struct inode *inode, struct file *filp)
1530 struct cache_detail *cd = PDE_DATA(inode);
1532 return cache_open(inode, filp, cd);
1535 static int cache_release_procfs(struct inode *inode, struct file *filp)
1537 struct cache_detail *cd = PDE_DATA(inode);
1539 return cache_release(inode, filp, cd);
1542 static const struct file_operations cache_file_operations_procfs = {
1543 .owner = THIS_MODULE,
1544 .llseek = no_llseek,
1545 .read = cache_read_procfs,
1546 .write = cache_write_procfs,
1547 .poll = cache_poll_procfs,
1548 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
1549 .open = cache_open_procfs,
1550 .release = cache_release_procfs,
1553 static int content_open_procfs(struct inode *inode, struct file *filp)
1555 struct cache_detail *cd = PDE_DATA(inode);
1557 return content_open(inode, filp, cd);
1560 static int content_release_procfs(struct inode *inode, struct file *filp)
1562 struct cache_detail *cd = PDE_DATA(inode);
1564 return content_release(inode, filp, cd);
1567 static const struct file_operations content_file_operations_procfs = {
1568 .open = content_open_procfs,
1569 .read = seq_read,
1570 .llseek = seq_lseek,
1571 .release = content_release_procfs,
1574 static int open_flush_procfs(struct inode *inode, struct file *filp)
1576 struct cache_detail *cd = PDE_DATA(inode);
1578 return open_flush(inode, filp, cd);
1581 static int release_flush_procfs(struct inode *inode, struct file *filp)
1583 struct cache_detail *cd = PDE_DATA(inode);
1585 return release_flush(inode, filp, cd);
1588 static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1589 size_t count, loff_t *ppos)
1591 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1593 return read_flush(filp, buf, count, ppos, cd);
1596 static ssize_t write_flush_procfs(struct file *filp,
1597 const char __user *buf,
1598 size_t count, loff_t *ppos)
1600 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1602 return write_flush(filp, buf, count, ppos, cd);
1605 static const struct file_operations cache_flush_operations_procfs = {
1606 .open = open_flush_procfs,
1607 .read = read_flush_procfs,
1608 .write = write_flush_procfs,
1609 .release = release_flush_procfs,
1610 .llseek = no_llseek,
1613 static void remove_cache_proc_entries(struct cache_detail *cd)
1615 if (cd->procfs) {
1616 proc_remove(cd->procfs);
1617 cd->procfs = NULL;
1621 #ifdef CONFIG_PROC_FS
1622 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1624 struct proc_dir_entry *p;
1625 struct sunrpc_net *sn;
1627 sn = net_generic(net, sunrpc_net_id);
1628 cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc);
1629 if (cd->procfs == NULL)
1630 goto out_nomem;
1632 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1633 cd->procfs, &cache_flush_operations_procfs, cd);
1634 if (p == NULL)
1635 goto out_nomem;
1637 if (cd->cache_request || cd->cache_parse) {
1638 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1639 cd->procfs, &cache_file_operations_procfs, cd);
1640 if (p == NULL)
1641 goto out_nomem;
1643 if (cd->cache_show) {
1644 p = proc_create_data("content", S_IFREG|S_IRUSR,
1645 cd->procfs, &content_file_operations_procfs, cd);
1646 if (p == NULL)
1647 goto out_nomem;
1649 return 0;
1650 out_nomem:
1651 remove_cache_proc_entries(cd);
1652 return -ENOMEM;
1654 #else /* CONFIG_PROC_FS */
1655 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1657 return 0;
1659 #endif
1661 void __init cache_initialize(void)
1663 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
1666 int cache_register_net(struct cache_detail *cd, struct net *net)
1668 int ret;
1670 sunrpc_init_cache_detail(cd);
1671 ret = create_cache_proc_entries(cd, net);
1672 if (ret)
1673 sunrpc_destroy_cache_detail(cd);
1674 return ret;
1676 EXPORT_SYMBOL_GPL(cache_register_net);
1678 void cache_unregister_net(struct cache_detail *cd, struct net *net)
1680 remove_cache_proc_entries(cd);
1681 sunrpc_destroy_cache_detail(cd);
1683 EXPORT_SYMBOL_GPL(cache_unregister_net);
1685 struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
1687 struct cache_detail *cd;
1688 int i;
1690 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1691 if (cd == NULL)
1692 return ERR_PTR(-ENOMEM);
1694 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct hlist_head),
1695 GFP_KERNEL);
1696 if (cd->hash_table == NULL) {
1697 kfree(cd);
1698 return ERR_PTR(-ENOMEM);
1701 for (i = 0; i < cd->hash_size; i++)
1702 INIT_HLIST_HEAD(&cd->hash_table[i]);
1703 cd->net = net;
1704 return cd;
1706 EXPORT_SYMBOL_GPL(cache_create_net);
1708 void cache_destroy_net(struct cache_detail *cd, struct net *net)
1710 kfree(cd->hash_table);
1711 kfree(cd);
1713 EXPORT_SYMBOL_GPL(cache_destroy_net);
1715 static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1716 size_t count, loff_t *ppos)
1718 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1720 return cache_read(filp, buf, count, ppos, cd);
1723 static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1724 size_t count, loff_t *ppos)
1726 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1728 return cache_write(filp, buf, count, ppos, cd);
1731 static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1733 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1735 return cache_poll(filp, wait, cd);
1738 static long cache_ioctl_pipefs(struct file *filp,
1739 unsigned int cmd, unsigned long arg)
1741 struct inode *inode = file_inode(filp);
1742 struct cache_detail *cd = RPC_I(inode)->private;
1744 return cache_ioctl(inode, filp, cmd, arg, cd);
1747 static int cache_open_pipefs(struct inode *inode, struct file *filp)
1749 struct cache_detail *cd = RPC_I(inode)->private;
1751 return cache_open(inode, filp, cd);
1754 static int cache_release_pipefs(struct inode *inode, struct file *filp)
1756 struct cache_detail *cd = RPC_I(inode)->private;
1758 return cache_release(inode, filp, cd);
1761 const struct file_operations cache_file_operations_pipefs = {
1762 .owner = THIS_MODULE,
1763 .llseek = no_llseek,
1764 .read = cache_read_pipefs,
1765 .write = cache_write_pipefs,
1766 .poll = cache_poll_pipefs,
1767 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
1768 .open = cache_open_pipefs,
1769 .release = cache_release_pipefs,
1772 static int content_open_pipefs(struct inode *inode, struct file *filp)
1774 struct cache_detail *cd = RPC_I(inode)->private;
1776 return content_open(inode, filp, cd);
1779 static int content_release_pipefs(struct inode *inode, struct file *filp)
1781 struct cache_detail *cd = RPC_I(inode)->private;
1783 return content_release(inode, filp, cd);
1786 const struct file_operations content_file_operations_pipefs = {
1787 .open = content_open_pipefs,
1788 .read = seq_read,
1789 .llseek = seq_lseek,
1790 .release = content_release_pipefs,
1793 static int open_flush_pipefs(struct inode *inode, struct file *filp)
1795 struct cache_detail *cd = RPC_I(inode)->private;
1797 return open_flush(inode, filp, cd);
1800 static int release_flush_pipefs(struct inode *inode, struct file *filp)
1802 struct cache_detail *cd = RPC_I(inode)->private;
1804 return release_flush(inode, filp, cd);
1807 static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1808 size_t count, loff_t *ppos)
1810 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1812 return read_flush(filp, buf, count, ppos, cd);
1815 static ssize_t write_flush_pipefs(struct file *filp,
1816 const char __user *buf,
1817 size_t count, loff_t *ppos)
1819 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1821 return write_flush(filp, buf, count, ppos, cd);
1824 const struct file_operations cache_flush_operations_pipefs = {
1825 .open = open_flush_pipefs,
1826 .read = read_flush_pipefs,
1827 .write = write_flush_pipefs,
1828 .release = release_flush_pipefs,
1829 .llseek = no_llseek,
1832 int sunrpc_cache_register_pipefs(struct dentry *parent,
1833 const char *name, umode_t umode,
1834 struct cache_detail *cd)
1836 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1837 if (IS_ERR(dir))
1838 return PTR_ERR(dir);
1839 cd->pipefs = dir;
1840 return 0;
1842 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1844 void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1846 if (cd->pipefs) {
1847 rpc_remove_cache_dir(cd->pipefs);
1848 cd->pipefs = NULL;
1851 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1853 void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h)
1855 write_lock(&cd->hash_lock);
1856 if (!hlist_unhashed(&h->cache_list)){
1857 hlist_del_init(&h->cache_list);
1858 cd->entries--;
1859 write_unlock(&cd->hash_lock);
1860 cache_put(h, cd);
1861 } else
1862 write_unlock(&cd->hash_lock);
1864 EXPORT_SYMBOL_GPL(sunrpc_cache_unhash);