sunrpc/cache: use cache_fresh_unlocked consistently and correctly.
[deliverable/linux.git] / net / sunrpc / cache.c
1 /*
2 * net/sunrpc/cache.c
3 *
4 * Generic code for various authentication-related caches
5 * used by sunrpc clients and servers.
6 *
7 * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
8 *
9 * Released under terms in GPL version 2. See COPYING.
10 *
11 */
12
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 <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>
36 #include "netns.h"
37
38 #define RPCDBG_FACILITY RPCDBG_CACHE
39
40 static bool cache_defer_req(struct cache_req *req, struct cache_head *item);
41 static void cache_revisit_request(struct cache_head *item);
42
43 static void cache_init(struct cache_head *h)
44 {
45 time_t now = seconds_since_boot();
46 h->next = NULL;
47 h->flags = 0;
48 kref_init(&h->ref);
49 h->expiry_time = now + CACHE_NEW_EXPIRY;
50 h->last_refresh = now;
51 }
52
53 static inline int cache_is_expired(struct cache_detail *detail, struct cache_head *h)
54 {
55 return (h->expiry_time < seconds_since_boot()) ||
56 (detail->flush_time > h->last_refresh);
57 }
58
59 struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
60 struct cache_head *key, int hash)
61 {
62 struct cache_head **head, **hp;
63 struct cache_head *new = NULL, *freeme = NULL;
64
65 head = &detail->hash_table[hash];
66
67 read_lock(&detail->hash_lock);
68
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. */
74 break;
75 cache_get(tmp);
76 read_unlock(&detail->hash_lock);
77 return tmp;
78 }
79 }
80 read_unlock(&detail->hash_lock);
81 /* Didn't find anything, insert an empty entry */
82
83 new = detail->alloc();
84 if (!new)
85 return NULL;
86 /* must fully initialise 'new', else
87 * we might get lose if we need to
88 * cache_put it soon.
89 */
90 cache_init(new);
91 detail->init(new, key);
92
93 write_lock(&detail->hash_lock);
94
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)) {
100 *hp = tmp->next;
101 tmp->next = NULL;
102 detail->entries --;
103 freeme = tmp;
104 break;
105 }
106 cache_get(tmp);
107 write_unlock(&detail->hash_lock);
108 cache_put(new, detail);
109 return tmp;
110 }
111 }
112 new->next = *head;
113 *head = new;
114 detail->entries++;
115 cache_get(new);
116 write_unlock(&detail->hash_lock);
117
118 if (freeme)
119 cache_put(freeme, detail);
120 return new;
121 }
122 EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
123
124
125 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
126
127 static void cache_fresh_locked(struct cache_head *head, time_t expiry)
128 {
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);
133 }
134
135 static void cache_fresh_unlocked(struct cache_head *head,
136 struct cache_detail *detail)
137 {
138 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
139 cache_revisit_request(head);
140 cache_dequeue(detail, head);
141 }
142 }
143
144 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
145 struct cache_head *new, struct cache_head *old, int hash)
146 {
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
150 */
151 struct cache_head **head;
152 struct cache_head *tmp;
153
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;
165 }
166 write_unlock(&detail->hash_lock);
167 }
168 /* We need to insert a new entry */
169 tmp = detail->alloc();
170 if (!tmp) {
171 cache_put(old, detail);
172 return NULL;
173 }
174 cache_init(tmp);
175 detail->init(tmp, old);
176 head = &detail->hash_table[hash];
177
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;
194 }
195 EXPORT_SYMBOL_GPL(sunrpc_cache_update);
196
197 static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
198 {
199 if (cd->cache_upcall)
200 return cd->cache_upcall(cd, h);
201 return sunrpc_cache_pipe_upcall(cd, h);
202 }
203
204 static inline int cache_is_valid(struct cache_head *h)
205 {
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 /*
214 * In combination with write barrier in
215 * sunrpc_cache_update, ensures that anyone
216 * using the cache entry after this sees the
217 * updated contents:
218 */
219 smp_rmb();
220 return 0;
221 }
222 }
223 }
224
225 static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
226 {
227 int rv;
228
229 write_lock(&detail->hash_lock);
230 rv = cache_is_valid(h);
231 if (rv == -EAGAIN) {
232 set_bit(CACHE_NEGATIVE, &h->flags);
233 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
234 rv = -ENOENT;
235 }
236 write_unlock(&detail->hash_lock);
237 cache_fresh_unlocked(h, detail);
238 return rv;
239 }
240
241 /*
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.
246 *
247 *
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
254 */
255 int cache_check(struct cache_detail *detail,
256 struct cache_head *h, struct cache_req *rqstp)
257 {
258 int rv;
259 long refresh_age, age;
260
261 /* First decide return status as best we can */
262 rv = cache_is_valid(h);
263
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;
267
268 if (rqstp == NULL) {
269 if (rv == -EAGAIN)
270 rv = -ENOENT;
271 } else if (rv == -EAGAIN || age > refresh_age/2) {
272 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
273 refresh_age, age);
274 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
275 switch (cache_make_upcall(detail, h)) {
276 case -EINVAL:
277 rv = try_to_negate_entry(detail, h);
278 break;
279 case -EAGAIN:
280 cache_fresh_unlocked(h, detail);
281 break;
282 }
283 }
284 }
285
286 if (rv == -EAGAIN) {
287 if (!cache_defer_req(rqstp, h)) {
288 /*
289 * Request was not deferred; handle it as best
290 * we can ourselves:
291 */
292 rv = cache_is_valid(h);
293 if (rv == -EAGAIN)
294 rv = -ETIMEDOUT;
295 }
296 }
297 if (rv)
298 cache_put(h, detail);
299 return rv;
300 }
301 EXPORT_SYMBOL_GPL(cache_check);
302
303 /*
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
307 * for that entry.
308 *
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.
312 *
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
316 *
317 * later we might drop old entries with non-NEVER expiry if that table
318 * is getting 'full' for some definition of 'full'
319 *
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
322 * cache_detail.
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.
329 *
330 * A table is then only scanned if the current time is at least
331 * the nextcheck time.
332 *
333 */
334
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;
339
340 static void do_cache_clean(struct work_struct *work);
341 static struct delayed_work cache_cleaner;
342
343 void sunrpc_init_cache_detail(struct cache_detail *cd)
344 {
345 rwlock_init(&cd->hash_lock);
346 INIT_LIST_HEAD(&cd->queue);
347 spin_lock(&cache_list_lock);
348 cd->nextcheck = 0;
349 cd->entries = 0;
350 atomic_set(&cd->readers, 0);
351 cd->last_close = 0;
352 cd->last_warn = -1;
353 list_add(&cd->others, &cache_list);
354 spin_unlock(&cache_list_lock);
355
356 /* start the cleaning process */
357 schedule_delayed_work(&cache_cleaner, 0);
358 }
359 EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
360
361 void sunrpc_destroy_cache_detail(struct cache_detail *cd)
362 {
363 cache_purge(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);
369 goto out;
370 }
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);
379 }
380 return;
381 out:
382 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
383 }
384 EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
385
386 /* clean cache tries to find something to clean
387 * and cleans it.
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.
391 */
392 static int cache_clean(void)
393 {
394 int rv = 0;
395 struct list_head *next;
396
397 spin_lock(&cache_list_lock);
398
399 /* find a suitable table if we don't already have one */
400 while (current_detail == NULL ||
401 current_index >= current_detail->hash_size) {
402 if (current_detail)
403 next = current_detail->others.next;
404 else
405 next = cache_list.next;
406 if (next == &cache_list) {
407 current_detail = NULL;
408 spin_unlock(&cache_list_lock);
409 return -1;
410 }
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;
414 else {
415 current_index = 0;
416 current_detail->nextcheck = seconds_since_boot()+30*60;
417 }
418 }
419
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)
424 current_index++;
425
426 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
427
428 if (current_detail && current_index < current_detail->hash_size) {
429 struct cache_head *ch, **cp;
430 struct cache_detail *d;
431
432 write_lock(&current_detail->hash_lock);
433
434 /* Ok, now to clean this strand */
435
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))
441 continue;
442
443 *cp = ch->next;
444 ch->next = NULL;
445 current_detail->entries--;
446 rv = 1;
447 break;
448 }
449
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 cache_fresh_unlocked(ch, d);
457 cache_put(ch, d);
458 }
459 } else
460 spin_unlock(&cache_list_lock);
461
462 return rv;
463 }
464
465 /*
466 * We want to regularly clean the cache, so we need to schedule some work ...
467 */
468 static void do_cache_clean(struct work_struct *work)
469 {
470 int delay = 5;
471 if (cache_clean() == -1)
472 delay = round_jiffies_relative(30*HZ);
473
474 if (list_empty(&cache_list))
475 delay = 0;
476
477 if (delay)
478 schedule_delayed_work(&cache_cleaner, delay);
479 }
480
481
482 /*
483 * Clean all caches promptly. This just calls cache_clean
484 * repeatedly until we are sure that every cache has had a chance to
485 * be fully cleaned
486 */
487 void cache_flush(void)
488 {
489 while (cache_clean() != -1)
490 cond_resched();
491 while (cache_clean() != -1)
492 cond_resched();
493 }
494 EXPORT_SYMBOL_GPL(cache_flush);
495
496 void cache_purge(struct cache_detail *detail)
497 {
498 detail->flush_time = LONG_MAX;
499 detail->nextcheck = seconds_since_boot();
500 cache_flush();
501 detail->flush_time = 1;
502 }
503 EXPORT_SYMBOL_GPL(cache_purge);
504
505
506 /*
507 * Deferral and Revisiting of Requests.
508 *
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
519 */
520
521 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
522 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
523
524 #define DFR_MAX 300 /* ??? */
525
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;
530
531 static void __unhash_deferred_req(struct cache_deferred_req *dreq)
532 {
533 hlist_del_init(&dreq->hash);
534 if (!list_empty(&dreq->recent)) {
535 list_del_init(&dreq->recent);
536 cache_defer_cnt--;
537 }
538 }
539
540 static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
541 {
542 int hash = DFR_HASH(item);
543
544 INIT_LIST_HEAD(&dreq->recent);
545 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
546 }
547
548 static void setup_deferral(struct cache_deferred_req *dreq,
549 struct cache_head *item,
550 int count_me)
551 {
552
553 dreq->item = item;
554
555 spin_lock(&cache_defer_lock);
556
557 __hash_deferred_req(dreq, item);
558
559 if (count_me) {
560 cache_defer_cnt++;
561 list_add(&dreq->recent, &cache_defer_list);
562 }
563
564 spin_unlock(&cache_defer_lock);
565
566 }
567
568 struct thread_deferred_req {
569 struct cache_deferred_req handle;
570 struct completion completion;
571 };
572
573 static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
574 {
575 struct thread_deferred_req *dr =
576 container_of(dreq, struct thread_deferred_req, handle);
577 complete(&dr->completion);
578 }
579
580 static void cache_wait_req(struct cache_req *req, struct cache_head *item)
581 {
582 struct thread_deferred_req sleeper;
583 struct cache_deferred_req *dreq = &sleeper.handle;
584
585 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
586 dreq->revisit = cache_restart_thread;
587
588 setup_deferral(dreq, item, 0);
589
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
594 * to clean up
595 */
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);
600 } else {
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.
605 */
606 spin_unlock(&cache_defer_lock);
607 wait_for_completion(&sleeper.completion);
608 }
609 }
610 }
611
612 static void cache_limit_defers(void)
613 {
614 /* Make sure we haven't exceed the limit of allowed deferred
615 * requests.
616 */
617 struct cache_deferred_req *discard = NULL;
618
619 if (cache_defer_cnt <= DFR_MAX)
620 return;
621
622 spin_lock(&cache_defer_lock);
623
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);
629 else
630 discard = list_entry(cache_defer_list.prev,
631 struct cache_deferred_req, recent);
632 __unhash_deferred_req(discard);
633 }
634 spin_unlock(&cache_defer_lock);
635 if (discard)
636 discard->revisit(discard, 1);
637 }
638
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)
641 {
642 struct cache_deferred_req *dreq;
643
644 if (req->thread_wait) {
645 cache_wait_req(req, item);
646 if (!test_bit(CACHE_PENDING, &item->flags))
647 return false;
648 }
649 dreq = req->defer(req);
650 if (dreq == NULL)
651 return false;
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
656 */
657 cache_revisit_request(item);
658
659 cache_limit_defers();
660 return true;
661 }
662
663 static void cache_revisit_request(struct cache_head *item)
664 {
665 struct cache_deferred_req *dreq;
666 struct list_head pending;
667 struct hlist_node *tmp;
668 int hash = DFR_HASH(item);
669
670 INIT_LIST_HEAD(&pending);
671 spin_lock(&cache_defer_lock);
672
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);
677 }
678
679 spin_unlock(&cache_defer_lock);
680
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);
685 }
686 }
687
688 void cache_clean_deferred(void *owner)
689 {
690 struct cache_deferred_req *dreq, *tmp;
691 struct list_head pending;
692
693
694 INIT_LIST_HEAD(&pending);
695 spin_lock(&cache_defer_lock);
696
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);
701 }
702 }
703 spin_unlock(&cache_defer_lock);
704
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);
709 }
710 }
711
712 /*
713 * communicate with user-space
714 *
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.
719 *
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.
725 *
726 */
727
728 static DEFINE_SPINLOCK(queue_lock);
729 static DEFINE_MUTEX(queue_io_mutex);
730
731 struct cache_queue {
732 struct list_head list;
733 int reader; /* if 0, then request */
734 };
735 struct cache_request {
736 struct cache_queue q;
737 struct cache_head *item;
738 char * buf;
739 int len;
740 int readers;
741 };
742 struct cache_reader {
743 struct cache_queue q;
744 int offset; /* if non-0, we have a refcnt on next request */
745 };
746
747 static int cache_request(struct cache_detail *detail,
748 struct cache_request *crq)
749 {
750 char *bp = crq->buf;
751 int len = PAGE_SIZE;
752
753 detail->cache_request(detail, crq->item, &bp, &len);
754 if (len < 0)
755 return -EAGAIN;
756 return PAGE_SIZE - len;
757 }
758
759 static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
760 loff_t *ppos, struct cache_detail *cd)
761 {
762 struct cache_reader *rp = filp->private_data;
763 struct cache_request *rq;
764 struct inode *inode = file_inode(filp);
765 int err;
766
767 if (count == 0)
768 return 0;
769
770 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
771 * readers on this file */
772 again:
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)
777 ->reader) {
778 struct list_head *next = rp->q.list.next;
779 list_move(&rp->q.list, next);
780 }
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);
785 return 0;
786 }
787 rq = container_of(rp->q.list.next, struct cache_request, q.list);
788 WARN_ON_ONCE(rq->q.reader);
789 if (rp->offset == 0)
790 rq->readers++;
791 spin_unlock(&queue_lock);
792
793 if (rq->len == 0) {
794 err = cache_request(cd, rq);
795 if (err < 0)
796 goto out;
797 rq->len = err;
798 }
799
800 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
801 err = -EAGAIN;
802 spin_lock(&queue_lock);
803 list_move(&rp->q.list, &rq->q.list);
804 spin_unlock(&queue_lock);
805 } else {
806 if (rp->offset + count > rq->len)
807 count = rq->len - rp->offset;
808 err = -EFAULT;
809 if (copy_to_user(buf, rq->buf + rp->offset, count))
810 goto out;
811 rp->offset += count;
812 if (rp->offset >= rq->len) {
813 rp->offset = 0;
814 spin_lock(&queue_lock);
815 list_move(&rp->q.list, &rq->q.list);
816 spin_unlock(&queue_lock);
817 }
818 err = 0;
819 }
820 out:
821 if (rp->offset == 0) {
822 /* need to release rq */
823 spin_lock(&queue_lock);
824 rq->readers--;
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);
830 kfree(rq->buf);
831 kfree(rq);
832 } else
833 spin_unlock(&queue_lock);
834 }
835 if (err == -EAGAIN)
836 goto again;
837 mutex_unlock(&inode->i_mutex);
838 return err ? err : count;
839 }
840
841 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
842 size_t count, struct cache_detail *cd)
843 {
844 ssize_t ret;
845
846 if (count == 0)
847 return -EINVAL;
848 if (copy_from_user(kaddr, buf, count))
849 return -EFAULT;
850 kaddr[count] = '\0';
851 ret = cd->cache_parse(cd, kaddr, count);
852 if (!ret)
853 ret = count;
854 return ret;
855 }
856
857 static ssize_t cache_slow_downcall(const char __user *buf,
858 size_t count, struct cache_detail *cd)
859 {
860 static char write_buf[8192]; /* protected by queue_io_mutex */
861 ssize_t ret = -EINVAL;
862
863 if (count >= sizeof(write_buf))
864 goto out;
865 mutex_lock(&queue_io_mutex);
866 ret = cache_do_downcall(write_buf, buf, count, cd);
867 mutex_unlock(&queue_io_mutex);
868 out:
869 return ret;
870 }
871
872 static ssize_t cache_downcall(struct address_space *mapping,
873 const char __user *buf,
874 size_t count, struct cache_detail *cd)
875 {
876 struct page *page;
877 char *kaddr;
878 ssize_t ret = -ENOMEM;
879
880 if (count >= PAGE_CACHE_SIZE)
881 goto out_slow;
882
883 page = find_or_create_page(mapping, 0, GFP_KERNEL);
884 if (!page)
885 goto out_slow;
886
887 kaddr = kmap(page);
888 ret = cache_do_downcall(kaddr, buf, count, cd);
889 kunmap(page);
890 unlock_page(page);
891 page_cache_release(page);
892 return ret;
893 out_slow:
894 return cache_slow_downcall(buf, count, cd);
895 }
896
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)
900 {
901 struct address_space *mapping = filp->f_mapping;
902 struct inode *inode = file_inode(filp);
903 ssize_t ret = -EINVAL;
904
905 if (!cd->cache_parse)
906 goto out;
907
908 mutex_lock(&inode->i_mutex);
909 ret = cache_downcall(mapping, buf, count, cd);
910 mutex_unlock(&inode->i_mutex);
911 out:
912 return ret;
913 }
914
915 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
916
917 static unsigned int cache_poll(struct file *filp, poll_table *wait,
918 struct cache_detail *cd)
919 {
920 unsigned int mask;
921 struct cache_reader *rp = filp->private_data;
922 struct cache_queue *cq;
923
924 poll_wait(filp, &queue_wait, wait);
925
926 /* alway allow write */
927 mask = POLL_OUT | POLLWRNORM;
928
929 if (!rp)
930 return mask;
931
932 spin_lock(&queue_lock);
933
934 for (cq= &rp->q; &cq->list != &cd->queue;
935 cq = list_entry(cq->list.next, struct cache_queue, list))
936 if (!cq->reader) {
937 mask |= POLLIN | POLLRDNORM;
938 break;
939 }
940 spin_unlock(&queue_lock);
941 return mask;
942 }
943
944 static int cache_ioctl(struct inode *ino, struct file *filp,
945 unsigned int cmd, unsigned long arg,
946 struct cache_detail *cd)
947 {
948 int len = 0;
949 struct cache_reader *rp = filp->private_data;
950 struct cache_queue *cq;
951
952 if (cmd != FIONREAD || !rp)
953 return -EINVAL;
954
955 spin_lock(&queue_lock);
956
957 /* only find the length remaining in current request,
958 * or the length of the next request
959 */
960 for (cq= &rp->q; &cq->list != &cd->queue;
961 cq = list_entry(cq->list.next, struct cache_queue, list))
962 if (!cq->reader) {
963 struct cache_request *cr =
964 container_of(cq, struct cache_request, q);
965 len = cr->len - rp->offset;
966 break;
967 }
968 spin_unlock(&queue_lock);
969
970 return put_user(len, (int __user *)arg);
971 }
972
973 static int cache_open(struct inode *inode, struct file *filp,
974 struct cache_detail *cd)
975 {
976 struct cache_reader *rp = NULL;
977
978 if (!cd || !try_module_get(cd->owner))
979 return -EACCES;
980 nonseekable_open(inode, filp);
981 if (filp->f_mode & FMODE_READ) {
982 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
983 if (!rp) {
984 module_put(cd->owner);
985 return -ENOMEM;
986 }
987 rp->offset = 0;
988 rp->q.reader = 1;
989 atomic_inc(&cd->readers);
990 spin_lock(&queue_lock);
991 list_add(&rp->q.list, &cd->queue);
992 spin_unlock(&queue_lock);
993 }
994 filp->private_data = rp;
995 return 0;
996 }
997
998 static int cache_release(struct inode *inode, struct file *filp,
999 struct cache_detail *cd)
1000 {
1001 struct cache_reader *rp = filp->private_data;
1002
1003 if (rp) {
1004 spin_lock(&queue_lock);
1005 if (rp->offset) {
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))
1009 if (!cq->reader) {
1010 container_of(cq, struct cache_request, q)
1011 ->readers--;
1012 break;
1013 }
1014 rp->offset = 0;
1015 }
1016 list_del(&rp->q.list);
1017 spin_unlock(&queue_lock);
1018
1019 filp->private_data = NULL;
1020 kfree(rp);
1021
1022 cd->last_close = seconds_since_boot();
1023 atomic_dec(&cd->readers);
1024 }
1025 module_put(cd->owner);
1026 return 0;
1027 }
1028
1029
1030
1031 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
1032 {
1033 struct cache_queue *cq, *tmp;
1034 struct cache_request *cr;
1035 struct list_head dequeued;
1036
1037 INIT_LIST_HEAD(&dequeued);
1038 spin_lock(&queue_lock);
1039 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
1040 if (!cq->reader) {
1041 cr = container_of(cq, struct cache_request, q);
1042 if (cr->item != ch)
1043 continue;
1044 if (test_bit(CACHE_PENDING, &ch->flags))
1045 /* Lost a race and it is pending again */
1046 break;
1047 if (cr->readers != 0)
1048 continue;
1049 list_move(&cr->q.list, &dequeued);
1050 }
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);
1056 kfree(cr->buf);
1057 kfree(cr);
1058 }
1059 }
1060
1061 /*
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.
1067 *
1068 */
1069
1070 void qword_add(char **bpp, int *lp, char *str)
1071 {
1072 char *bp = *bpp;
1073 int len = *lp;
1074 char c;
1075
1076 if (len < 0) return;
1077
1078 while ((c=*str++) && len)
1079 switch(c) {
1080 case ' ':
1081 case '\t':
1082 case '\n':
1083 case '\\':
1084 if (len >= 4) {
1085 *bp++ = '\\';
1086 *bp++ = '0' + ((c & 0300)>>6);
1087 *bp++ = '0' + ((c & 0070)>>3);
1088 *bp++ = '0' + ((c & 0007)>>0);
1089 }
1090 len -= 4;
1091 break;
1092 default:
1093 *bp++ = c;
1094 len--;
1095 }
1096 if (c || len <1) len = -1;
1097 else {
1098 *bp++ = ' ';
1099 len--;
1100 }
1101 *bpp = bp;
1102 *lp = len;
1103 }
1104 EXPORT_SYMBOL_GPL(qword_add);
1105
1106 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1107 {
1108 char *bp = *bpp;
1109 int len = *lp;
1110
1111 if (len < 0) return;
1112
1113 if (len > 2) {
1114 *bp++ = '\\';
1115 *bp++ = 'x';
1116 len -= 2;
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);
1121 len -= 2;
1122 blen--;
1123 }
1124 }
1125 if (blen || len<1) len = -1;
1126 else {
1127 *bp++ = ' ';
1128 len--;
1129 }
1130 *bpp = bp;
1131 *lp = len;
1132 }
1133 EXPORT_SYMBOL_GPL(qword_addhex);
1134
1135 static void warn_no_listener(struct cache_detail *detail)
1136 {
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);
1141 }
1142 }
1143
1144 static bool cache_listeners_exist(struct cache_detail *detail)
1145 {
1146 if (atomic_read(&detail->readers))
1147 return true;
1148 if (detail->last_close == 0)
1149 /* This cache was never opened */
1150 return false;
1151 if (detail->last_close < seconds_since_boot() - 30)
1152 /*
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.
1156 */
1157 return false;
1158 return true;
1159 }
1160
1161 /*
1162 * register an upcall request to user-space and queue it up for read() by the
1163 * upcall daemon.
1164 *
1165 * Each request is at most one page long.
1166 */
1167 int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
1168 {
1169
1170 char *buf;
1171 struct cache_request *crq;
1172 int ret = 0;
1173
1174 if (!detail->cache_request)
1175 return -EINVAL;
1176
1177 if (!cache_listeners_exist(detail)) {
1178 warn_no_listener(detail);
1179 return -EINVAL;
1180 }
1181
1182 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1183 if (!buf)
1184 return -EAGAIN;
1185
1186 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1187 if (!crq) {
1188 kfree(buf);
1189 return -EAGAIN;
1190 }
1191
1192 crq->q.reader = 0;
1193 crq->item = cache_get(h);
1194 crq->buf = buf;
1195 crq->len = 0;
1196 crq->readers = 0;
1197 spin_lock(&queue_lock);
1198 if (test_bit(CACHE_PENDING, &h->flags))
1199 list_add_tail(&crq->q.list, &detail->queue);
1200 else
1201 /* Lost a race, no longer PENDING, so don't enqueue */
1202 ret = -EAGAIN;
1203 spin_unlock(&queue_lock);
1204 wake_up(&queue_wait);
1205 if (ret == -EAGAIN) {
1206 kfree(buf);
1207 kfree(crq);
1208 }
1209 return ret;
1210 }
1211 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1212
1213 /*
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
1218 *
1219 * Message is
1220 * reply cachename expiry key ... content....
1221 *
1222 * key and content are both parsed by cache
1223 */
1224
1225 int qword_get(char **bpp, char *dest, int bufsize)
1226 {
1227 /* return bytes copied, or -1 on error */
1228 char *bp = *bpp;
1229 int len = 0;
1230
1231 while (*bp == ' ') bp++;
1232
1233 if (bp[0] == '\\' && bp[1] == 'x') {
1234 /* HEX STRING */
1235 bp += 2;
1236 while (len < bufsize) {
1237 int h, l;
1238
1239 h = hex_to_bin(bp[0]);
1240 if (h < 0)
1241 break;
1242
1243 l = hex_to_bin(bp[1]);
1244 if (l < 0)
1245 break;
1246
1247 *dest++ = (h << 4) | l;
1248 bp += 2;
1249 len++;
1250 }
1251 } else {
1252 /* text with \nnn octal quoting */
1253 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1254 if (*bp == '\\' &&
1255 isodigit(bp[1]) && (bp[1] <= '3') &&
1256 isodigit(bp[2]) &&
1257 isodigit(bp[3])) {
1258 int byte = (*++bp -'0');
1259 bp++;
1260 byte = (byte << 3) | (*bp++ - '0');
1261 byte = (byte << 3) | (*bp++ - '0');
1262 *dest++ = byte;
1263 len++;
1264 } else {
1265 *dest++ = *bp++;
1266 len++;
1267 }
1268 }
1269 }
1270
1271 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1272 return -1;
1273 while (*bp == ' ') bp++;
1274 *bpp = bp;
1275 *dest = '\0';
1276 return len;
1277 }
1278 EXPORT_SYMBOL_GPL(qword_get);
1279
1280
1281 /*
1282 * support /proc/sunrpc/cache/$CACHENAME/content
1283 * as a seqfile.
1284 * We call ->cache_show passing NULL for the item to
1285 * get a header, then pass each real item in the cache
1286 */
1287
1288 struct handle {
1289 struct cache_detail *cd;
1290 };
1291
1292 static void *c_start(struct seq_file *m, loff_t *pos)
1293 __acquires(cd->hash_lock)
1294 {
1295 loff_t n = *pos;
1296 unsigned int hash, entry;
1297 struct cache_head *ch;
1298 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1299
1300
1301 read_lock(&cd->hash_lock);
1302 if (!n--)
1303 return SEQ_START_TOKEN;
1304 hash = n >> 32;
1305 entry = n & ((1LL<<32) - 1);
1306
1307 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1308 if (!entry--)
1309 return ch;
1310 n &= ~((1LL<<32) - 1);
1311 do {
1312 hash++;
1313 n += 1LL<<32;
1314 } while(hash < cd->hash_size &&
1315 cd->hash_table[hash]==NULL);
1316 if (hash >= cd->hash_size)
1317 return NULL;
1318 *pos = n+1;
1319 return cd->hash_table[hash];
1320 }
1321
1322 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1323 {
1324 struct cache_head *ch = p;
1325 int hash = (*pos >> 32);
1326 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1327
1328 if (p == SEQ_START_TOKEN)
1329 hash = 0;
1330 else if (ch->next == NULL) {
1331 hash++;
1332 *pos += 1LL<<32;
1333 } else {
1334 ++*pos;
1335 return ch->next;
1336 }
1337 *pos &= ~((1LL<<32) - 1);
1338 while (hash < cd->hash_size &&
1339 cd->hash_table[hash] == NULL) {
1340 hash++;
1341 *pos += 1LL<<32;
1342 }
1343 if (hash >= cd->hash_size)
1344 return NULL;
1345 ++*pos;
1346 return cd->hash_table[hash];
1347 }
1348
1349 static void c_stop(struct seq_file *m, void *p)
1350 __releases(cd->hash_lock)
1351 {
1352 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1353 read_unlock(&cd->hash_lock);
1354 }
1355
1356 static int c_show(struct seq_file *m, void *p)
1357 {
1358 struct cache_head *cp = p;
1359 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1360
1361 if (p == SEQ_START_TOKEN)
1362 return cd->cache_show(m, cd, NULL);
1363
1364 ifdebug(CACHE)
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);
1368 cache_get(cp);
1369 if (cache_check(cd, cp, NULL))
1370 /* cache_check does a cache_put on failure */
1371 seq_printf(m, "# ");
1372 else {
1373 if (cache_is_expired(cd, cp))
1374 seq_printf(m, "# ");
1375 cache_put(cp, cd);
1376 }
1377
1378 return cd->cache_show(m, cd, cp);
1379 }
1380
1381 static const struct seq_operations cache_content_op = {
1382 .start = c_start,
1383 .next = c_next,
1384 .stop = c_stop,
1385 .show = c_show,
1386 };
1387
1388 static int content_open(struct inode *inode, struct file *file,
1389 struct cache_detail *cd)
1390 {
1391 struct handle *han;
1392
1393 if (!cd || !try_module_get(cd->owner))
1394 return -EACCES;
1395 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1396 if (han == NULL) {
1397 module_put(cd->owner);
1398 return -ENOMEM;
1399 }
1400
1401 han->cd = cd;
1402 return 0;
1403 }
1404
1405 static int content_release(struct inode *inode, struct file *file,
1406 struct cache_detail *cd)
1407 {
1408 int ret = seq_release_private(inode, file);
1409 module_put(cd->owner);
1410 return ret;
1411 }
1412
1413 static int open_flush(struct inode *inode, struct file *file,
1414 struct cache_detail *cd)
1415 {
1416 if (!cd || !try_module_get(cd->owner))
1417 return -EACCES;
1418 return nonseekable_open(inode, file);
1419 }
1420
1421 static int release_flush(struct inode *inode, struct file *file,
1422 struct cache_detail *cd)
1423 {
1424 module_put(cd->owner);
1425 return 0;
1426 }
1427
1428 static ssize_t read_flush(struct file *file, char __user *buf,
1429 size_t count, loff_t *ppos,
1430 struct cache_detail *cd)
1431 {
1432 char tbuf[22];
1433 unsigned long p = *ppos;
1434 size_t len;
1435
1436 snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
1437 len = strlen(tbuf);
1438 if (p >= len)
1439 return 0;
1440 len -= p;
1441 if (len > count)
1442 len = count;
1443 if (copy_to_user(buf, (void*)(tbuf+p), len))
1444 return -EFAULT;
1445 *ppos += len;
1446 return len;
1447 }
1448
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)
1452 {
1453 char tbuf[20];
1454 char *bp, *ep;
1455
1456 if (*ppos || count > sizeof(tbuf)-1)
1457 return -EINVAL;
1458 if (copy_from_user(tbuf, buf, count))
1459 return -EFAULT;
1460 tbuf[count] = 0;
1461 simple_strtoul(tbuf, &ep, 0);
1462 if (*ep && *ep != '\n')
1463 return -EINVAL;
1464
1465 bp = tbuf;
1466 cd->flush_time = get_expiry(&bp);
1467 cd->nextcheck = seconds_since_boot();
1468 cache_flush();
1469
1470 *ppos += count;
1471 return count;
1472 }
1473
1474 static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1475 size_t count, loff_t *ppos)
1476 {
1477 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1478
1479 return cache_read(filp, buf, count, ppos, cd);
1480 }
1481
1482 static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1483 size_t count, loff_t *ppos)
1484 {
1485 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1486
1487 return cache_write(filp, buf, count, ppos, cd);
1488 }
1489
1490 static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1491 {
1492 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1493
1494 return cache_poll(filp, wait, cd);
1495 }
1496
1497 static long cache_ioctl_procfs(struct file *filp,
1498 unsigned int cmd, unsigned long arg)
1499 {
1500 struct inode *inode = file_inode(filp);
1501 struct cache_detail *cd = PDE_DATA(inode);
1502
1503 return cache_ioctl(inode, filp, cmd, arg, cd);
1504 }
1505
1506 static int cache_open_procfs(struct inode *inode, struct file *filp)
1507 {
1508 struct cache_detail *cd = PDE_DATA(inode);
1509
1510 return cache_open(inode, filp, cd);
1511 }
1512
1513 static int cache_release_procfs(struct inode *inode, struct file *filp)
1514 {
1515 struct cache_detail *cd = PDE_DATA(inode);
1516
1517 return cache_release(inode, filp, cd);
1518 }
1519
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,
1529 };
1530
1531 static int content_open_procfs(struct inode *inode, struct file *filp)
1532 {
1533 struct cache_detail *cd = PDE_DATA(inode);
1534
1535 return content_open(inode, filp, cd);
1536 }
1537
1538 static int content_release_procfs(struct inode *inode, struct file *filp)
1539 {
1540 struct cache_detail *cd = PDE_DATA(inode);
1541
1542 return content_release(inode, filp, cd);
1543 }
1544
1545 static const struct file_operations content_file_operations_procfs = {
1546 .open = content_open_procfs,
1547 .read = seq_read,
1548 .llseek = seq_lseek,
1549 .release = content_release_procfs,
1550 };
1551
1552 static int open_flush_procfs(struct inode *inode, struct file *filp)
1553 {
1554 struct cache_detail *cd = PDE_DATA(inode);
1555
1556 return open_flush(inode, filp, cd);
1557 }
1558
1559 static int release_flush_procfs(struct inode *inode, struct file *filp)
1560 {
1561 struct cache_detail *cd = PDE_DATA(inode);
1562
1563 return release_flush(inode, filp, cd);
1564 }
1565
1566 static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1567 size_t count, loff_t *ppos)
1568 {
1569 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1570
1571 return read_flush(filp, buf, count, ppos, cd);
1572 }
1573
1574 static ssize_t write_flush_procfs(struct file *filp,
1575 const char __user *buf,
1576 size_t count, loff_t *ppos)
1577 {
1578 struct cache_detail *cd = PDE_DATA(file_inode(filp));
1579
1580 return write_flush(filp, buf, count, ppos, cd);
1581 }
1582
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,
1589 };
1590
1591 static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
1592 {
1593 struct sunrpc_net *sn;
1594
1595 if (cd->u.procfs.proc_ent == NULL)
1596 return;
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);
1606 }
1607
1608 #ifdef CONFIG_PROC_FS
1609 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1610 {
1611 struct proc_dir_entry *p;
1612 struct sunrpc_net *sn;
1613
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)
1617 goto out_nomem;
1618 cd->u.procfs.channel_ent = NULL;
1619 cd->u.procfs.content_ent = NULL;
1620
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;
1625 if (p == NULL)
1626 goto out_nomem;
1627
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;
1633 if (p == NULL)
1634 goto out_nomem;
1635 }
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;
1641 if (p == NULL)
1642 goto out_nomem;
1643 }
1644 return 0;
1645 out_nomem:
1646 remove_cache_proc_entries(cd, net);
1647 return -ENOMEM;
1648 }
1649 #else /* CONFIG_PROC_FS */
1650 static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
1651 {
1652 return 0;
1653 }
1654 #endif
1655
1656 void __init cache_initialize(void)
1657 {
1658 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
1659 }
1660
1661 int cache_register_net(struct cache_detail *cd, struct net *net)
1662 {
1663 int ret;
1664
1665 sunrpc_init_cache_detail(cd);
1666 ret = create_cache_proc_entries(cd, net);
1667 if (ret)
1668 sunrpc_destroy_cache_detail(cd);
1669 return ret;
1670 }
1671 EXPORT_SYMBOL_GPL(cache_register_net);
1672
1673 void cache_unregister_net(struct cache_detail *cd, struct net *net)
1674 {
1675 remove_cache_proc_entries(cd, net);
1676 sunrpc_destroy_cache_detail(cd);
1677 }
1678 EXPORT_SYMBOL_GPL(cache_unregister_net);
1679
1680 struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
1681 {
1682 struct cache_detail *cd;
1683
1684 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1685 if (cd == NULL)
1686 return ERR_PTR(-ENOMEM);
1687
1688 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct cache_head *),
1689 GFP_KERNEL);
1690 if (cd->hash_table == NULL) {
1691 kfree(cd);
1692 return ERR_PTR(-ENOMEM);
1693 }
1694 cd->net = net;
1695 return cd;
1696 }
1697 EXPORT_SYMBOL_GPL(cache_create_net);
1698
1699 void cache_destroy_net(struct cache_detail *cd, struct net *net)
1700 {
1701 kfree(cd->hash_table);
1702 kfree(cd);
1703 }
1704 EXPORT_SYMBOL_GPL(cache_destroy_net);
1705
1706 static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1707 size_t count, loff_t *ppos)
1708 {
1709 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1710
1711 return cache_read(filp, buf, count, ppos, cd);
1712 }
1713
1714 static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1715 size_t count, loff_t *ppos)
1716 {
1717 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1718
1719 return cache_write(filp, buf, count, ppos, cd);
1720 }
1721
1722 static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1723 {
1724 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1725
1726 return cache_poll(filp, wait, cd);
1727 }
1728
1729 static long cache_ioctl_pipefs(struct file *filp,
1730 unsigned int cmd, unsigned long arg)
1731 {
1732 struct inode *inode = file_inode(filp);
1733 struct cache_detail *cd = RPC_I(inode)->private;
1734
1735 return cache_ioctl(inode, filp, cmd, arg, cd);
1736 }
1737
1738 static int cache_open_pipefs(struct inode *inode, struct file *filp)
1739 {
1740 struct cache_detail *cd = RPC_I(inode)->private;
1741
1742 return cache_open(inode, filp, cd);
1743 }
1744
1745 static int cache_release_pipefs(struct inode *inode, struct file *filp)
1746 {
1747 struct cache_detail *cd = RPC_I(inode)->private;
1748
1749 return cache_release(inode, filp, cd);
1750 }
1751
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,
1761 };
1762
1763 static int content_open_pipefs(struct inode *inode, struct file *filp)
1764 {
1765 struct cache_detail *cd = RPC_I(inode)->private;
1766
1767 return content_open(inode, filp, cd);
1768 }
1769
1770 static int content_release_pipefs(struct inode *inode, struct file *filp)
1771 {
1772 struct cache_detail *cd = RPC_I(inode)->private;
1773
1774 return content_release(inode, filp, cd);
1775 }
1776
1777 const struct file_operations content_file_operations_pipefs = {
1778 .open = content_open_pipefs,
1779 .read = seq_read,
1780 .llseek = seq_lseek,
1781 .release = content_release_pipefs,
1782 };
1783
1784 static int open_flush_pipefs(struct inode *inode, struct file *filp)
1785 {
1786 struct cache_detail *cd = RPC_I(inode)->private;
1787
1788 return open_flush(inode, filp, cd);
1789 }
1790
1791 static int release_flush_pipefs(struct inode *inode, struct file *filp)
1792 {
1793 struct cache_detail *cd = RPC_I(inode)->private;
1794
1795 return release_flush(inode, filp, cd);
1796 }
1797
1798 static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1799 size_t count, loff_t *ppos)
1800 {
1801 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1802
1803 return read_flush(filp, buf, count, ppos, cd);
1804 }
1805
1806 static ssize_t write_flush_pipefs(struct file *filp,
1807 const char __user *buf,
1808 size_t count, loff_t *ppos)
1809 {
1810 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
1811
1812 return write_flush(filp, buf, count, ppos, cd);
1813 }
1814
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,
1821 };
1822
1823 int sunrpc_cache_register_pipefs(struct dentry *parent,
1824 const char *name, umode_t umode,
1825 struct cache_detail *cd)
1826 {
1827 struct qstr q;
1828 struct dentry *dir;
1829 int ret = 0;
1830
1831 q.name = name;
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);
1835 if (!IS_ERR(dir))
1836 cd->u.pipefs.dir = dir;
1837 else
1838 ret = PTR_ERR(dir);
1839 return ret;
1840 }
1841 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1842
1843 void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1844 {
1845 rpc_remove_cache_dir(cd->u.pipefs.dir);
1846 cd->u.pipefs.dir = NULL;
1847 }
1848 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1849
This page took 0.093229 seconds and 5 git commands to generate.