]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/sunrpc/cache.c
sunrpc/cache: allow threads to block while waiting for cache update.
[mirror_ubuntu-bionic-kernel.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 <linux/smp_lock.h>
32 #include <asm/ioctls.h>
33 #include <linux/sunrpc/types.h>
34 #include <linux/sunrpc/cache.h>
35 #include <linux/sunrpc/stats.h>
36 #include <linux/sunrpc/rpc_pipe_fs.h>
37
38 #define RPCDBG_FACILITY RPCDBG_CACHE
39
40 static int 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 set_bit(CACHE_VALID, &head->flags);
132 }
133
134 static void cache_fresh_unlocked(struct cache_head *head,
135 struct cache_detail *detail)
136 {
137 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
138 cache_revisit_request(head);
139 cache_dequeue(detail, head);
140 }
141 }
142
143 struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
144 struct cache_head *new, struct cache_head *old, int hash)
145 {
146 /* The 'old' entry is to be replaced by 'new'.
147 * If 'old' is not VALID, we update it directly,
148 * otherwise we need to replace it
149 */
150 struct cache_head **head;
151 struct cache_head *tmp;
152
153 if (!test_bit(CACHE_VALID, &old->flags)) {
154 write_lock(&detail->hash_lock);
155 if (!test_bit(CACHE_VALID, &old->flags)) {
156 if (test_bit(CACHE_NEGATIVE, &new->flags))
157 set_bit(CACHE_NEGATIVE, &old->flags);
158 else
159 detail->update(old, new);
160 cache_fresh_locked(old, new->expiry_time);
161 write_unlock(&detail->hash_lock);
162 cache_fresh_unlocked(old, detail);
163 return old;
164 }
165 write_unlock(&detail->hash_lock);
166 }
167 /* We need to insert a new entry */
168 tmp = detail->alloc();
169 if (!tmp) {
170 cache_put(old, detail);
171 return NULL;
172 }
173 cache_init(tmp);
174 detail->init(tmp, old);
175 head = &detail->hash_table[hash];
176
177 write_lock(&detail->hash_lock);
178 if (test_bit(CACHE_NEGATIVE, &new->flags))
179 set_bit(CACHE_NEGATIVE, &tmp->flags);
180 else
181 detail->update(tmp, new);
182 tmp->next = *head;
183 *head = tmp;
184 detail->entries++;
185 cache_get(tmp);
186 cache_fresh_locked(tmp, new->expiry_time);
187 cache_fresh_locked(old, 0);
188 write_unlock(&detail->hash_lock);
189 cache_fresh_unlocked(tmp, detail);
190 cache_fresh_unlocked(old, detail);
191 cache_put(old, detail);
192 return tmp;
193 }
194 EXPORT_SYMBOL_GPL(sunrpc_cache_update);
195
196 static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
197 {
198 if (!cd->cache_upcall)
199 return -EINVAL;
200 return cd->cache_upcall(cd, h);
201 }
202
203 static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h)
204 {
205 if (!test_bit(CACHE_VALID, &h->flags))
206 return -EAGAIN;
207 else {
208 /* entry is valid */
209 if (test_bit(CACHE_NEGATIVE, &h->flags))
210 return -ENOENT;
211 else
212 return 0;
213 }
214 }
215
216 /*
217 * This is the generic cache management routine for all
218 * the authentication caches.
219 * It checks the currency of a cache item and will (later)
220 * initiate an upcall to fill it if needed.
221 *
222 *
223 * Returns 0 if the cache_head can be used, or cache_puts it and returns
224 * -EAGAIN if upcall is pending and request has been queued
225 * -ETIMEDOUT if upcall failed or request could not be queue or
226 * upcall completed but item is still invalid (implying that
227 * the cache item has been replaced with a newer one).
228 * -ENOENT if cache entry was negative
229 */
230 int cache_check(struct cache_detail *detail,
231 struct cache_head *h, struct cache_req *rqstp)
232 {
233 int rv;
234 long refresh_age, age;
235
236 /* First decide return status as best we can */
237 rv = cache_is_valid(detail, h);
238
239 /* now see if we want to start an upcall */
240 refresh_age = (h->expiry_time - h->last_refresh);
241 age = seconds_since_boot() - h->last_refresh;
242
243 if (rqstp == NULL) {
244 if (rv == -EAGAIN)
245 rv = -ENOENT;
246 } else if (rv == -EAGAIN || age > refresh_age/2) {
247 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
248 refresh_age, age);
249 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
250 switch (cache_make_upcall(detail, h)) {
251 case -EINVAL:
252 clear_bit(CACHE_PENDING, &h->flags);
253 cache_revisit_request(h);
254 if (rv == -EAGAIN) {
255 set_bit(CACHE_NEGATIVE, &h->flags);
256 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
257 cache_fresh_unlocked(h, detail);
258 rv = -ENOENT;
259 }
260 break;
261
262 case -EAGAIN:
263 clear_bit(CACHE_PENDING, &h->flags);
264 cache_revisit_request(h);
265 break;
266 }
267 }
268 }
269
270 if (rv == -EAGAIN) {
271 if (cache_defer_req(rqstp, h) < 0) {
272 /* Request is not deferred */
273 rv = cache_is_valid(detail, h);
274 if (rv == -EAGAIN)
275 rv = -ETIMEDOUT;
276 }
277 }
278 if (rv)
279 cache_put(h, detail);
280 return rv;
281 }
282 EXPORT_SYMBOL_GPL(cache_check);
283
284 /*
285 * caches need to be periodically cleaned.
286 * For this we maintain a list of cache_detail and
287 * a current pointer into that list and into the table
288 * for that entry.
289 *
290 * Each time clean_cache is called it finds the next non-empty entry
291 * in the current table and walks the list in that entry
292 * looking for entries that can be removed.
293 *
294 * An entry gets removed if:
295 * - The expiry is before current time
296 * - The last_refresh time is before the flush_time for that cache
297 *
298 * later we might drop old entries with non-NEVER expiry if that table
299 * is getting 'full' for some definition of 'full'
300 *
301 * The question of "how often to scan a table" is an interesting one
302 * and is answered in part by the use of the "nextcheck" field in the
303 * cache_detail.
304 * When a scan of a table begins, the nextcheck field is set to a time
305 * that is well into the future.
306 * While scanning, if an expiry time is found that is earlier than the
307 * current nextcheck time, nextcheck is set to that expiry time.
308 * If the flush_time is ever set to a time earlier than the nextcheck
309 * time, the nextcheck time is then set to that flush_time.
310 *
311 * A table is then only scanned if the current time is at least
312 * the nextcheck time.
313 *
314 */
315
316 static LIST_HEAD(cache_list);
317 static DEFINE_SPINLOCK(cache_list_lock);
318 static struct cache_detail *current_detail;
319 static int current_index;
320
321 static void do_cache_clean(struct work_struct *work);
322 static struct delayed_work cache_cleaner;
323
324 static void sunrpc_init_cache_detail(struct cache_detail *cd)
325 {
326 rwlock_init(&cd->hash_lock);
327 INIT_LIST_HEAD(&cd->queue);
328 spin_lock(&cache_list_lock);
329 cd->nextcheck = 0;
330 cd->entries = 0;
331 atomic_set(&cd->readers, 0);
332 cd->last_close = 0;
333 cd->last_warn = -1;
334 list_add(&cd->others, &cache_list);
335 spin_unlock(&cache_list_lock);
336
337 /* start the cleaning process */
338 schedule_delayed_work(&cache_cleaner, 0);
339 }
340
341 static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
342 {
343 cache_purge(cd);
344 spin_lock(&cache_list_lock);
345 write_lock(&cd->hash_lock);
346 if (cd->entries || atomic_read(&cd->inuse)) {
347 write_unlock(&cd->hash_lock);
348 spin_unlock(&cache_list_lock);
349 goto out;
350 }
351 if (current_detail == cd)
352 current_detail = NULL;
353 list_del_init(&cd->others);
354 write_unlock(&cd->hash_lock);
355 spin_unlock(&cache_list_lock);
356 if (list_empty(&cache_list)) {
357 /* module must be being unloaded so its safe to kill the worker */
358 cancel_delayed_work_sync(&cache_cleaner);
359 }
360 return;
361 out:
362 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
363 }
364
365 /* clean cache tries to find something to clean
366 * and cleans it.
367 * It returns 1 if it cleaned something,
368 * 0 if it didn't find anything this time
369 * -1 if it fell off the end of the list.
370 */
371 static int cache_clean(void)
372 {
373 int rv = 0;
374 struct list_head *next;
375
376 spin_lock(&cache_list_lock);
377
378 /* find a suitable table if we don't already have one */
379 while (current_detail == NULL ||
380 current_index >= current_detail->hash_size) {
381 if (current_detail)
382 next = current_detail->others.next;
383 else
384 next = cache_list.next;
385 if (next == &cache_list) {
386 current_detail = NULL;
387 spin_unlock(&cache_list_lock);
388 return -1;
389 }
390 current_detail = list_entry(next, struct cache_detail, others);
391 if (current_detail->nextcheck > seconds_since_boot())
392 current_index = current_detail->hash_size;
393 else {
394 current_index = 0;
395 current_detail->nextcheck = seconds_since_boot()+30*60;
396 }
397 }
398
399 /* find a non-empty bucket in the table */
400 while (current_detail &&
401 current_index < current_detail->hash_size &&
402 current_detail->hash_table[current_index] == NULL)
403 current_index++;
404
405 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
406
407 if (current_detail && current_index < current_detail->hash_size) {
408 struct cache_head *ch, **cp;
409 struct cache_detail *d;
410
411 write_lock(&current_detail->hash_lock);
412
413 /* Ok, now to clean this strand */
414
415 cp = & current_detail->hash_table[current_index];
416 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
417 if (current_detail->nextcheck > ch->expiry_time)
418 current_detail->nextcheck = ch->expiry_time+1;
419 if (!cache_is_expired(current_detail, ch))
420 continue;
421
422 *cp = ch->next;
423 ch->next = NULL;
424 current_detail->entries--;
425 rv = 1;
426 break;
427 }
428
429 write_unlock(&current_detail->hash_lock);
430 d = current_detail;
431 if (!ch)
432 current_index ++;
433 spin_unlock(&cache_list_lock);
434 if (ch) {
435 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
436 cache_dequeue(current_detail, ch);
437 cache_revisit_request(ch);
438 cache_put(ch, d);
439 }
440 } else
441 spin_unlock(&cache_list_lock);
442
443 return rv;
444 }
445
446 /*
447 * We want to regularly clean the cache, so we need to schedule some work ...
448 */
449 static void do_cache_clean(struct work_struct *work)
450 {
451 int delay = 5;
452 if (cache_clean() == -1)
453 delay = round_jiffies_relative(30*HZ);
454
455 if (list_empty(&cache_list))
456 delay = 0;
457
458 if (delay)
459 schedule_delayed_work(&cache_cleaner, delay);
460 }
461
462
463 /*
464 * Clean all caches promptly. This just calls cache_clean
465 * repeatedly until we are sure that every cache has had a chance to
466 * be fully cleaned
467 */
468 void cache_flush(void)
469 {
470 while (cache_clean() != -1)
471 cond_resched();
472 while (cache_clean() != -1)
473 cond_resched();
474 }
475 EXPORT_SYMBOL_GPL(cache_flush);
476
477 void cache_purge(struct cache_detail *detail)
478 {
479 detail->flush_time = LONG_MAX;
480 detail->nextcheck = seconds_since_boot();
481 cache_flush();
482 detail->flush_time = 1;
483 }
484 EXPORT_SYMBOL_GPL(cache_purge);
485
486
487 /*
488 * Deferral and Revisiting of Requests.
489 *
490 * If a cache lookup finds a pending entry, we
491 * need to defer the request and revisit it later.
492 * All deferred requests are stored in a hash table,
493 * indexed by "struct cache_head *".
494 * As it may be wasteful to store a whole request
495 * structure, we allow the request to provide a
496 * deferred form, which must contain a
497 * 'struct cache_deferred_req'
498 * This cache_deferred_req contains a method to allow
499 * it to be revisited when cache info is available
500 */
501
502 #define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
503 #define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
504
505 #define DFR_MAX 300 /* ??? */
506
507 static DEFINE_SPINLOCK(cache_defer_lock);
508 static LIST_HEAD(cache_defer_list);
509 static struct list_head cache_defer_hash[DFR_HASHSIZE];
510 static int cache_defer_cnt;
511
512 struct thread_deferred_req {
513 struct cache_deferred_req handle;
514 struct completion completion;
515 };
516 static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
517 {
518 struct thread_deferred_req *dr =
519 container_of(dreq, struct thread_deferred_req, handle);
520 complete(&dr->completion);
521 }
522
523 static int cache_defer_req(struct cache_req *req, struct cache_head *item)
524 {
525 struct cache_deferred_req *dreq, *discard;
526 int hash = DFR_HASH(item);
527 struct thread_deferred_req sleeper;
528
529 if (cache_defer_cnt >= DFR_MAX) {
530 /* too much in the cache, randomly drop this one,
531 * or continue and drop the oldest below
532 */
533 if (net_random()&1)
534 return -ENOMEM;
535 }
536 if (req->thread_wait) {
537 dreq = &sleeper.handle;
538 sleeper.completion =
539 COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
540 dreq->revisit = cache_restart_thread;
541 } else
542 dreq = req->defer(req);
543
544 retry:
545 if (dreq == NULL)
546 return -ENOMEM;
547
548 dreq->item = item;
549
550 spin_lock(&cache_defer_lock);
551
552 list_add(&dreq->recent, &cache_defer_list);
553
554 if (cache_defer_hash[hash].next == NULL)
555 INIT_LIST_HEAD(&cache_defer_hash[hash]);
556 list_add(&dreq->hash, &cache_defer_hash[hash]);
557
558 /* it is in, now maybe clean up */
559 discard = NULL;
560 if (++cache_defer_cnt > DFR_MAX) {
561 discard = list_entry(cache_defer_list.prev,
562 struct cache_deferred_req, recent);
563 list_del_init(&discard->recent);
564 list_del_init(&discard->hash);
565 cache_defer_cnt--;
566 }
567 spin_unlock(&cache_defer_lock);
568
569 if (discard)
570 /* there was one too many */
571 discard->revisit(discard, 1);
572
573 if (!test_bit(CACHE_PENDING, &item->flags)) {
574 /* must have just been validated... */
575 cache_revisit_request(item);
576 return -EAGAIN;
577 }
578
579 if (dreq == &sleeper.handle) {
580 if (wait_for_completion_interruptible_timeout(
581 &sleeper.completion, req->thread_wait) <= 0) {
582 /* The completion wasn't completed, so we need
583 * to clean up
584 */
585 spin_lock(&cache_defer_lock);
586 if (!list_empty(&sleeper.handle.hash)) {
587 list_del_init(&sleeper.handle.recent);
588 list_del_init(&sleeper.handle.hash);
589 cache_defer_cnt--;
590 spin_unlock(&cache_defer_lock);
591 } else {
592 /* cache_revisit_request already removed
593 * this from the hash table, but hasn't
594 * called ->revisit yet. It will very soon
595 * and we need to wait for it.
596 */
597 spin_unlock(&cache_defer_lock);
598 wait_for_completion(&sleeper.completion);
599 }
600 }
601 if (test_bit(CACHE_PENDING, &item->flags)) {
602 /* item is still pending, try request
603 * deferral
604 */
605 dreq = req->defer(req);
606 goto retry;
607 }
608 /* only return success if we actually deferred the
609 * request. In this case we waited until it was
610 * answered so no deferral has happened - rather
611 * an answer already exists.
612 */
613 return -EEXIST;
614 }
615 return 0;
616 }
617
618 static void cache_revisit_request(struct cache_head *item)
619 {
620 struct cache_deferred_req *dreq;
621 struct list_head pending;
622
623 struct list_head *lp;
624 int hash = DFR_HASH(item);
625
626 INIT_LIST_HEAD(&pending);
627 spin_lock(&cache_defer_lock);
628
629 lp = cache_defer_hash[hash].next;
630 if (lp) {
631 while (lp != &cache_defer_hash[hash]) {
632 dreq = list_entry(lp, struct cache_deferred_req, hash);
633 lp = lp->next;
634 if (dreq->item == item) {
635 list_del_init(&dreq->hash);
636 list_move(&dreq->recent, &pending);
637 cache_defer_cnt--;
638 }
639 }
640 }
641 spin_unlock(&cache_defer_lock);
642
643 while (!list_empty(&pending)) {
644 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
645 list_del_init(&dreq->recent);
646 dreq->revisit(dreq, 0);
647 }
648 }
649
650 void cache_clean_deferred(void *owner)
651 {
652 struct cache_deferred_req *dreq, *tmp;
653 struct list_head pending;
654
655
656 INIT_LIST_HEAD(&pending);
657 spin_lock(&cache_defer_lock);
658
659 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
660 if (dreq->owner == owner) {
661 list_del_init(&dreq->hash);
662 list_move(&dreq->recent, &pending);
663 cache_defer_cnt--;
664 }
665 }
666 spin_unlock(&cache_defer_lock);
667
668 while (!list_empty(&pending)) {
669 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
670 list_del_init(&dreq->recent);
671 dreq->revisit(dreq, 1);
672 }
673 }
674
675 /*
676 * communicate with user-space
677 *
678 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
679 * On read, you get a full request, or block.
680 * On write, an update request is processed.
681 * Poll works if anything to read, and always allows write.
682 *
683 * Implemented by linked list of requests. Each open file has
684 * a ->private that also exists in this list. New requests are added
685 * to the end and may wakeup and preceding readers.
686 * New readers are added to the head. If, on read, an item is found with
687 * CACHE_UPCALLING clear, we free it from the list.
688 *
689 */
690
691 static DEFINE_SPINLOCK(queue_lock);
692 static DEFINE_MUTEX(queue_io_mutex);
693
694 struct cache_queue {
695 struct list_head list;
696 int reader; /* if 0, then request */
697 };
698 struct cache_request {
699 struct cache_queue q;
700 struct cache_head *item;
701 char * buf;
702 int len;
703 int readers;
704 };
705 struct cache_reader {
706 struct cache_queue q;
707 int offset; /* if non-0, we have a refcnt on next request */
708 };
709
710 static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
711 loff_t *ppos, struct cache_detail *cd)
712 {
713 struct cache_reader *rp = filp->private_data;
714 struct cache_request *rq;
715 struct inode *inode = filp->f_path.dentry->d_inode;
716 int err;
717
718 if (count == 0)
719 return 0;
720
721 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
722 * readers on this file */
723 again:
724 spin_lock(&queue_lock);
725 /* need to find next request */
726 while (rp->q.list.next != &cd->queue &&
727 list_entry(rp->q.list.next, struct cache_queue, list)
728 ->reader) {
729 struct list_head *next = rp->q.list.next;
730 list_move(&rp->q.list, next);
731 }
732 if (rp->q.list.next == &cd->queue) {
733 spin_unlock(&queue_lock);
734 mutex_unlock(&inode->i_mutex);
735 BUG_ON(rp->offset);
736 return 0;
737 }
738 rq = container_of(rp->q.list.next, struct cache_request, q.list);
739 BUG_ON(rq->q.reader);
740 if (rp->offset == 0)
741 rq->readers++;
742 spin_unlock(&queue_lock);
743
744 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
745 err = -EAGAIN;
746 spin_lock(&queue_lock);
747 list_move(&rp->q.list, &rq->q.list);
748 spin_unlock(&queue_lock);
749 } else {
750 if (rp->offset + count > rq->len)
751 count = rq->len - rp->offset;
752 err = -EFAULT;
753 if (copy_to_user(buf, rq->buf + rp->offset, count))
754 goto out;
755 rp->offset += count;
756 if (rp->offset >= rq->len) {
757 rp->offset = 0;
758 spin_lock(&queue_lock);
759 list_move(&rp->q.list, &rq->q.list);
760 spin_unlock(&queue_lock);
761 }
762 err = 0;
763 }
764 out:
765 if (rp->offset == 0) {
766 /* need to release rq */
767 spin_lock(&queue_lock);
768 rq->readers--;
769 if (rq->readers == 0 &&
770 !test_bit(CACHE_PENDING, &rq->item->flags)) {
771 list_del(&rq->q.list);
772 spin_unlock(&queue_lock);
773 cache_put(rq->item, cd);
774 kfree(rq->buf);
775 kfree(rq);
776 } else
777 spin_unlock(&queue_lock);
778 }
779 if (err == -EAGAIN)
780 goto again;
781 mutex_unlock(&inode->i_mutex);
782 return err ? err : count;
783 }
784
785 static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
786 size_t count, struct cache_detail *cd)
787 {
788 ssize_t ret;
789
790 if (copy_from_user(kaddr, buf, count))
791 return -EFAULT;
792 kaddr[count] = '\0';
793 ret = cd->cache_parse(cd, kaddr, count);
794 if (!ret)
795 ret = count;
796 return ret;
797 }
798
799 static ssize_t cache_slow_downcall(const char __user *buf,
800 size_t count, struct cache_detail *cd)
801 {
802 static char write_buf[8192]; /* protected by queue_io_mutex */
803 ssize_t ret = -EINVAL;
804
805 if (count >= sizeof(write_buf))
806 goto out;
807 mutex_lock(&queue_io_mutex);
808 ret = cache_do_downcall(write_buf, buf, count, cd);
809 mutex_unlock(&queue_io_mutex);
810 out:
811 return ret;
812 }
813
814 static ssize_t cache_downcall(struct address_space *mapping,
815 const char __user *buf,
816 size_t count, struct cache_detail *cd)
817 {
818 struct page *page;
819 char *kaddr;
820 ssize_t ret = -ENOMEM;
821
822 if (count >= PAGE_CACHE_SIZE)
823 goto out_slow;
824
825 page = find_or_create_page(mapping, 0, GFP_KERNEL);
826 if (!page)
827 goto out_slow;
828
829 kaddr = kmap(page);
830 ret = cache_do_downcall(kaddr, buf, count, cd);
831 kunmap(page);
832 unlock_page(page);
833 page_cache_release(page);
834 return ret;
835 out_slow:
836 return cache_slow_downcall(buf, count, cd);
837 }
838
839 static ssize_t cache_write(struct file *filp, const char __user *buf,
840 size_t count, loff_t *ppos,
841 struct cache_detail *cd)
842 {
843 struct address_space *mapping = filp->f_mapping;
844 struct inode *inode = filp->f_path.dentry->d_inode;
845 ssize_t ret = -EINVAL;
846
847 if (!cd->cache_parse)
848 goto out;
849
850 mutex_lock(&inode->i_mutex);
851 ret = cache_downcall(mapping, buf, count, cd);
852 mutex_unlock(&inode->i_mutex);
853 out:
854 return ret;
855 }
856
857 static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
858
859 static unsigned int cache_poll(struct file *filp, poll_table *wait,
860 struct cache_detail *cd)
861 {
862 unsigned int mask;
863 struct cache_reader *rp = filp->private_data;
864 struct cache_queue *cq;
865
866 poll_wait(filp, &queue_wait, wait);
867
868 /* alway allow write */
869 mask = POLL_OUT | POLLWRNORM;
870
871 if (!rp)
872 return mask;
873
874 spin_lock(&queue_lock);
875
876 for (cq= &rp->q; &cq->list != &cd->queue;
877 cq = list_entry(cq->list.next, struct cache_queue, list))
878 if (!cq->reader) {
879 mask |= POLLIN | POLLRDNORM;
880 break;
881 }
882 spin_unlock(&queue_lock);
883 return mask;
884 }
885
886 static int cache_ioctl(struct inode *ino, struct file *filp,
887 unsigned int cmd, unsigned long arg,
888 struct cache_detail *cd)
889 {
890 int len = 0;
891 struct cache_reader *rp = filp->private_data;
892 struct cache_queue *cq;
893
894 if (cmd != FIONREAD || !rp)
895 return -EINVAL;
896
897 spin_lock(&queue_lock);
898
899 /* only find the length remaining in current request,
900 * or the length of the next request
901 */
902 for (cq= &rp->q; &cq->list != &cd->queue;
903 cq = list_entry(cq->list.next, struct cache_queue, list))
904 if (!cq->reader) {
905 struct cache_request *cr =
906 container_of(cq, struct cache_request, q);
907 len = cr->len - rp->offset;
908 break;
909 }
910 spin_unlock(&queue_lock);
911
912 return put_user(len, (int __user *)arg);
913 }
914
915 static int cache_open(struct inode *inode, struct file *filp,
916 struct cache_detail *cd)
917 {
918 struct cache_reader *rp = NULL;
919
920 if (!cd || !try_module_get(cd->owner))
921 return -EACCES;
922 nonseekable_open(inode, filp);
923 if (filp->f_mode & FMODE_READ) {
924 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
925 if (!rp)
926 return -ENOMEM;
927 rp->offset = 0;
928 rp->q.reader = 1;
929 atomic_inc(&cd->readers);
930 spin_lock(&queue_lock);
931 list_add(&rp->q.list, &cd->queue);
932 spin_unlock(&queue_lock);
933 }
934 filp->private_data = rp;
935 return 0;
936 }
937
938 static int cache_release(struct inode *inode, struct file *filp,
939 struct cache_detail *cd)
940 {
941 struct cache_reader *rp = filp->private_data;
942
943 if (rp) {
944 spin_lock(&queue_lock);
945 if (rp->offset) {
946 struct cache_queue *cq;
947 for (cq= &rp->q; &cq->list != &cd->queue;
948 cq = list_entry(cq->list.next, struct cache_queue, list))
949 if (!cq->reader) {
950 container_of(cq, struct cache_request, q)
951 ->readers--;
952 break;
953 }
954 rp->offset = 0;
955 }
956 list_del(&rp->q.list);
957 spin_unlock(&queue_lock);
958
959 filp->private_data = NULL;
960 kfree(rp);
961
962 cd->last_close = seconds_since_boot();
963 atomic_dec(&cd->readers);
964 }
965 module_put(cd->owner);
966 return 0;
967 }
968
969
970
971 static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
972 {
973 struct cache_queue *cq;
974 spin_lock(&queue_lock);
975 list_for_each_entry(cq, &detail->queue, list)
976 if (!cq->reader) {
977 struct cache_request *cr = container_of(cq, struct cache_request, q);
978 if (cr->item != ch)
979 continue;
980 if (cr->readers != 0)
981 continue;
982 list_del(&cr->q.list);
983 spin_unlock(&queue_lock);
984 cache_put(cr->item, detail);
985 kfree(cr->buf);
986 kfree(cr);
987 return;
988 }
989 spin_unlock(&queue_lock);
990 }
991
992 /*
993 * Support routines for text-based upcalls.
994 * Fields are separated by spaces.
995 * Fields are either mangled to quote space tab newline slosh with slosh
996 * or a hexified with a leading \x
997 * Record is terminated with newline.
998 *
999 */
1000
1001 void qword_add(char **bpp, int *lp, char *str)
1002 {
1003 char *bp = *bpp;
1004 int len = *lp;
1005 char c;
1006
1007 if (len < 0) return;
1008
1009 while ((c=*str++) && len)
1010 switch(c) {
1011 case ' ':
1012 case '\t':
1013 case '\n':
1014 case '\\':
1015 if (len >= 4) {
1016 *bp++ = '\\';
1017 *bp++ = '0' + ((c & 0300)>>6);
1018 *bp++ = '0' + ((c & 0070)>>3);
1019 *bp++ = '0' + ((c & 0007)>>0);
1020 }
1021 len -= 4;
1022 break;
1023 default:
1024 *bp++ = c;
1025 len--;
1026 }
1027 if (c || len <1) len = -1;
1028 else {
1029 *bp++ = ' ';
1030 len--;
1031 }
1032 *bpp = bp;
1033 *lp = len;
1034 }
1035 EXPORT_SYMBOL_GPL(qword_add);
1036
1037 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1038 {
1039 char *bp = *bpp;
1040 int len = *lp;
1041
1042 if (len < 0) return;
1043
1044 if (len > 2) {
1045 *bp++ = '\\';
1046 *bp++ = 'x';
1047 len -= 2;
1048 while (blen && len >= 2) {
1049 unsigned char c = *buf++;
1050 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1051 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1052 len -= 2;
1053 blen--;
1054 }
1055 }
1056 if (blen || len<1) len = -1;
1057 else {
1058 *bp++ = ' ';
1059 len--;
1060 }
1061 *bpp = bp;
1062 *lp = len;
1063 }
1064 EXPORT_SYMBOL_GPL(qword_addhex);
1065
1066 static void warn_no_listener(struct cache_detail *detail)
1067 {
1068 if (detail->last_warn != detail->last_close) {
1069 detail->last_warn = detail->last_close;
1070 if (detail->warn_no_listener)
1071 detail->warn_no_listener(detail, detail->last_close != 0);
1072 }
1073 }
1074
1075 /*
1076 * register an upcall request to user-space and queue it up for read() by the
1077 * upcall daemon.
1078 *
1079 * Each request is at most one page long.
1080 */
1081 int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1082 void (*cache_request)(struct cache_detail *,
1083 struct cache_head *,
1084 char **,
1085 int *))
1086 {
1087
1088 char *buf;
1089 struct cache_request *crq;
1090 char *bp;
1091 int len;
1092
1093 if (atomic_read(&detail->readers) == 0 &&
1094 detail->last_close < seconds_since_boot() - 30) {
1095 warn_no_listener(detail);
1096 return -EINVAL;
1097 }
1098
1099 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1100 if (!buf)
1101 return -EAGAIN;
1102
1103 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1104 if (!crq) {
1105 kfree(buf);
1106 return -EAGAIN;
1107 }
1108
1109 bp = buf; len = PAGE_SIZE;
1110
1111 cache_request(detail, h, &bp, &len);
1112
1113 if (len < 0) {
1114 kfree(buf);
1115 kfree(crq);
1116 return -EAGAIN;
1117 }
1118 crq->q.reader = 0;
1119 crq->item = cache_get(h);
1120 crq->buf = buf;
1121 crq->len = PAGE_SIZE - len;
1122 crq->readers = 0;
1123 spin_lock(&queue_lock);
1124 list_add_tail(&crq->q.list, &detail->queue);
1125 spin_unlock(&queue_lock);
1126 wake_up(&queue_wait);
1127 return 0;
1128 }
1129 EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
1130
1131 /*
1132 * parse a message from user-space and pass it
1133 * to an appropriate cache
1134 * Messages are, like requests, separated into fields by
1135 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1136 *
1137 * Message is
1138 * reply cachename expiry key ... content....
1139 *
1140 * key and content are both parsed by cache
1141 */
1142
1143 #define isodigit(c) (isdigit(c) && c <= '7')
1144 int qword_get(char **bpp, char *dest, int bufsize)
1145 {
1146 /* return bytes copied, or -1 on error */
1147 char *bp = *bpp;
1148 int len = 0;
1149
1150 while (*bp == ' ') bp++;
1151
1152 if (bp[0] == '\\' && bp[1] == 'x') {
1153 /* HEX STRING */
1154 bp += 2;
1155 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1156 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1157 bp++;
1158 byte <<= 4;
1159 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1160 *dest++ = byte;
1161 bp++;
1162 len++;
1163 }
1164 } else {
1165 /* text with \nnn octal quoting */
1166 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1167 if (*bp == '\\' &&
1168 isodigit(bp[1]) && (bp[1] <= '3') &&
1169 isodigit(bp[2]) &&
1170 isodigit(bp[3])) {
1171 int byte = (*++bp -'0');
1172 bp++;
1173 byte = (byte << 3) | (*bp++ - '0');
1174 byte = (byte << 3) | (*bp++ - '0');
1175 *dest++ = byte;
1176 len++;
1177 } else {
1178 *dest++ = *bp++;
1179 len++;
1180 }
1181 }
1182 }
1183
1184 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1185 return -1;
1186 while (*bp == ' ') bp++;
1187 *bpp = bp;
1188 *dest = '\0';
1189 return len;
1190 }
1191 EXPORT_SYMBOL_GPL(qword_get);
1192
1193
1194 /*
1195 * support /proc/sunrpc/cache/$CACHENAME/content
1196 * as a seqfile.
1197 * We call ->cache_show passing NULL for the item to
1198 * get a header, then pass each real item in the cache
1199 */
1200
1201 struct handle {
1202 struct cache_detail *cd;
1203 };
1204
1205 static void *c_start(struct seq_file *m, loff_t *pos)
1206 __acquires(cd->hash_lock)
1207 {
1208 loff_t n = *pos;
1209 unsigned hash, entry;
1210 struct cache_head *ch;
1211 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1212
1213
1214 read_lock(&cd->hash_lock);
1215 if (!n--)
1216 return SEQ_START_TOKEN;
1217 hash = n >> 32;
1218 entry = n & ((1LL<<32) - 1);
1219
1220 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1221 if (!entry--)
1222 return ch;
1223 n &= ~((1LL<<32) - 1);
1224 do {
1225 hash++;
1226 n += 1LL<<32;
1227 } while(hash < cd->hash_size &&
1228 cd->hash_table[hash]==NULL);
1229 if (hash >= cd->hash_size)
1230 return NULL;
1231 *pos = n+1;
1232 return cd->hash_table[hash];
1233 }
1234
1235 static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1236 {
1237 struct cache_head *ch = p;
1238 int hash = (*pos >> 32);
1239 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1240
1241 if (p == SEQ_START_TOKEN)
1242 hash = 0;
1243 else if (ch->next == NULL) {
1244 hash++;
1245 *pos += 1LL<<32;
1246 } else {
1247 ++*pos;
1248 return ch->next;
1249 }
1250 *pos &= ~((1LL<<32) - 1);
1251 while (hash < cd->hash_size &&
1252 cd->hash_table[hash] == NULL) {
1253 hash++;
1254 *pos += 1LL<<32;
1255 }
1256 if (hash >= cd->hash_size)
1257 return NULL;
1258 ++*pos;
1259 return cd->hash_table[hash];
1260 }
1261
1262 static void c_stop(struct seq_file *m, void *p)
1263 __releases(cd->hash_lock)
1264 {
1265 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1266 read_unlock(&cd->hash_lock);
1267 }
1268
1269 static int c_show(struct seq_file *m, void *p)
1270 {
1271 struct cache_head *cp = p;
1272 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1273
1274 if (p == SEQ_START_TOKEN)
1275 return cd->cache_show(m, cd, NULL);
1276
1277 ifdebug(CACHE)
1278 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
1279 convert_to_wallclock(cp->expiry_time),
1280 atomic_read(&cp->ref.refcount), cp->flags);
1281 cache_get(cp);
1282 if (cache_check(cd, cp, NULL))
1283 /* cache_check does a cache_put on failure */
1284 seq_printf(m, "# ");
1285 else
1286 cache_put(cp, cd);
1287
1288 return cd->cache_show(m, cd, cp);
1289 }
1290
1291 static const struct seq_operations cache_content_op = {
1292 .start = c_start,
1293 .next = c_next,
1294 .stop = c_stop,
1295 .show = c_show,
1296 };
1297
1298 static int content_open(struct inode *inode, struct file *file,
1299 struct cache_detail *cd)
1300 {
1301 struct handle *han;
1302
1303 if (!cd || !try_module_get(cd->owner))
1304 return -EACCES;
1305 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
1306 if (han == NULL) {
1307 module_put(cd->owner);
1308 return -ENOMEM;
1309 }
1310
1311 han->cd = cd;
1312 return 0;
1313 }
1314
1315 static int content_release(struct inode *inode, struct file *file,
1316 struct cache_detail *cd)
1317 {
1318 int ret = seq_release_private(inode, file);
1319 module_put(cd->owner);
1320 return ret;
1321 }
1322
1323 static int open_flush(struct inode *inode, struct file *file,
1324 struct cache_detail *cd)
1325 {
1326 if (!cd || !try_module_get(cd->owner))
1327 return -EACCES;
1328 return nonseekable_open(inode, file);
1329 }
1330
1331 static int release_flush(struct inode *inode, struct file *file,
1332 struct cache_detail *cd)
1333 {
1334 module_put(cd->owner);
1335 return 0;
1336 }
1337
1338 static ssize_t read_flush(struct file *file, char __user *buf,
1339 size_t count, loff_t *ppos,
1340 struct cache_detail *cd)
1341 {
1342 char tbuf[20];
1343 unsigned long p = *ppos;
1344 size_t len;
1345
1346 sprintf(tbuf, "%lu\n", convert_to_wallclock(cd->flush_time));
1347 len = strlen(tbuf);
1348 if (p >= len)
1349 return 0;
1350 len -= p;
1351 if (len > count)
1352 len = count;
1353 if (copy_to_user(buf, (void*)(tbuf+p), len))
1354 return -EFAULT;
1355 *ppos += len;
1356 return len;
1357 }
1358
1359 static ssize_t write_flush(struct file *file, const char __user *buf,
1360 size_t count, loff_t *ppos,
1361 struct cache_detail *cd)
1362 {
1363 char tbuf[20];
1364 char *bp, *ep;
1365
1366 if (*ppos || count > sizeof(tbuf)-1)
1367 return -EINVAL;
1368 if (copy_from_user(tbuf, buf, count))
1369 return -EFAULT;
1370 tbuf[count] = 0;
1371 simple_strtoul(tbuf, &ep, 0);
1372 if (*ep && *ep != '\n')
1373 return -EINVAL;
1374
1375 bp = tbuf;
1376 cd->flush_time = get_expiry(&bp);
1377 cd->nextcheck = seconds_since_boot();
1378 cache_flush();
1379
1380 *ppos += count;
1381 return count;
1382 }
1383
1384 static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1385 size_t count, loff_t *ppos)
1386 {
1387 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1388
1389 return cache_read(filp, buf, count, ppos, cd);
1390 }
1391
1392 static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1393 size_t count, loff_t *ppos)
1394 {
1395 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1396
1397 return cache_write(filp, buf, count, ppos, cd);
1398 }
1399
1400 static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1401 {
1402 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1403
1404 return cache_poll(filp, wait, cd);
1405 }
1406
1407 static long cache_ioctl_procfs(struct file *filp,
1408 unsigned int cmd, unsigned long arg)
1409 {
1410 long ret;
1411 struct inode *inode = filp->f_path.dentry->d_inode;
1412 struct cache_detail *cd = PDE(inode)->data;
1413
1414 lock_kernel();
1415 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1416 unlock_kernel();
1417
1418 return ret;
1419 }
1420
1421 static int cache_open_procfs(struct inode *inode, struct file *filp)
1422 {
1423 struct cache_detail *cd = PDE(inode)->data;
1424
1425 return cache_open(inode, filp, cd);
1426 }
1427
1428 static int cache_release_procfs(struct inode *inode, struct file *filp)
1429 {
1430 struct cache_detail *cd = PDE(inode)->data;
1431
1432 return cache_release(inode, filp, cd);
1433 }
1434
1435 static const struct file_operations cache_file_operations_procfs = {
1436 .owner = THIS_MODULE,
1437 .llseek = no_llseek,
1438 .read = cache_read_procfs,
1439 .write = cache_write_procfs,
1440 .poll = cache_poll_procfs,
1441 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
1442 .open = cache_open_procfs,
1443 .release = cache_release_procfs,
1444 };
1445
1446 static int content_open_procfs(struct inode *inode, struct file *filp)
1447 {
1448 struct cache_detail *cd = PDE(inode)->data;
1449
1450 return content_open(inode, filp, cd);
1451 }
1452
1453 static int content_release_procfs(struct inode *inode, struct file *filp)
1454 {
1455 struct cache_detail *cd = PDE(inode)->data;
1456
1457 return content_release(inode, filp, cd);
1458 }
1459
1460 static const struct file_operations content_file_operations_procfs = {
1461 .open = content_open_procfs,
1462 .read = seq_read,
1463 .llseek = seq_lseek,
1464 .release = content_release_procfs,
1465 };
1466
1467 static int open_flush_procfs(struct inode *inode, struct file *filp)
1468 {
1469 struct cache_detail *cd = PDE(inode)->data;
1470
1471 return open_flush(inode, filp, cd);
1472 }
1473
1474 static int release_flush_procfs(struct inode *inode, struct file *filp)
1475 {
1476 struct cache_detail *cd = PDE(inode)->data;
1477
1478 return release_flush(inode, filp, cd);
1479 }
1480
1481 static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1482 size_t count, loff_t *ppos)
1483 {
1484 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1485
1486 return read_flush(filp, buf, count, ppos, cd);
1487 }
1488
1489 static ssize_t write_flush_procfs(struct file *filp,
1490 const char __user *buf,
1491 size_t count, loff_t *ppos)
1492 {
1493 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1494
1495 return write_flush(filp, buf, count, ppos, cd);
1496 }
1497
1498 static const struct file_operations cache_flush_operations_procfs = {
1499 .open = open_flush_procfs,
1500 .read = read_flush_procfs,
1501 .write = write_flush_procfs,
1502 .release = release_flush_procfs,
1503 };
1504
1505 static void remove_cache_proc_entries(struct cache_detail *cd)
1506 {
1507 if (cd->u.procfs.proc_ent == NULL)
1508 return;
1509 if (cd->u.procfs.flush_ent)
1510 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1511 if (cd->u.procfs.channel_ent)
1512 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1513 if (cd->u.procfs.content_ent)
1514 remove_proc_entry("content", cd->u.procfs.proc_ent);
1515 cd->u.procfs.proc_ent = NULL;
1516 remove_proc_entry(cd->name, proc_net_rpc);
1517 }
1518
1519 #ifdef CONFIG_PROC_FS
1520 static int create_cache_proc_entries(struct cache_detail *cd)
1521 {
1522 struct proc_dir_entry *p;
1523
1524 cd->u.procfs.proc_ent = proc_mkdir(cd->name, proc_net_rpc);
1525 if (cd->u.procfs.proc_ent == NULL)
1526 goto out_nomem;
1527 cd->u.procfs.channel_ent = NULL;
1528 cd->u.procfs.content_ent = NULL;
1529
1530 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1531 cd->u.procfs.proc_ent,
1532 &cache_flush_operations_procfs, cd);
1533 cd->u.procfs.flush_ent = p;
1534 if (p == NULL)
1535 goto out_nomem;
1536
1537 if (cd->cache_upcall || cd->cache_parse) {
1538 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1539 cd->u.procfs.proc_ent,
1540 &cache_file_operations_procfs, cd);
1541 cd->u.procfs.channel_ent = p;
1542 if (p == NULL)
1543 goto out_nomem;
1544 }
1545 if (cd->cache_show) {
1546 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1547 cd->u.procfs.proc_ent,
1548 &content_file_operations_procfs, cd);
1549 cd->u.procfs.content_ent = p;
1550 if (p == NULL)
1551 goto out_nomem;
1552 }
1553 return 0;
1554 out_nomem:
1555 remove_cache_proc_entries(cd);
1556 return -ENOMEM;
1557 }
1558 #else /* CONFIG_PROC_FS */
1559 static int create_cache_proc_entries(struct cache_detail *cd)
1560 {
1561 return 0;
1562 }
1563 #endif
1564
1565 void __init cache_initialize(void)
1566 {
1567 INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner, do_cache_clean);
1568 }
1569
1570 int cache_register(struct cache_detail *cd)
1571 {
1572 int ret;
1573
1574 sunrpc_init_cache_detail(cd);
1575 ret = create_cache_proc_entries(cd);
1576 if (ret)
1577 sunrpc_destroy_cache_detail(cd);
1578 return ret;
1579 }
1580 EXPORT_SYMBOL_GPL(cache_register);
1581
1582 void cache_unregister(struct cache_detail *cd)
1583 {
1584 remove_cache_proc_entries(cd);
1585 sunrpc_destroy_cache_detail(cd);
1586 }
1587 EXPORT_SYMBOL_GPL(cache_unregister);
1588
1589 static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1590 size_t count, loff_t *ppos)
1591 {
1592 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1593
1594 return cache_read(filp, buf, count, ppos, cd);
1595 }
1596
1597 static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1598 size_t count, loff_t *ppos)
1599 {
1600 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1601
1602 return cache_write(filp, buf, count, ppos, cd);
1603 }
1604
1605 static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1606 {
1607 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1608
1609 return cache_poll(filp, wait, cd);
1610 }
1611
1612 static long cache_ioctl_pipefs(struct file *filp,
1613 unsigned int cmd, unsigned long arg)
1614 {
1615 struct inode *inode = filp->f_dentry->d_inode;
1616 struct cache_detail *cd = RPC_I(inode)->private;
1617 long ret;
1618
1619 lock_kernel();
1620 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1621 unlock_kernel();
1622
1623 return ret;
1624 }
1625
1626 static int cache_open_pipefs(struct inode *inode, struct file *filp)
1627 {
1628 struct cache_detail *cd = RPC_I(inode)->private;
1629
1630 return cache_open(inode, filp, cd);
1631 }
1632
1633 static int cache_release_pipefs(struct inode *inode, struct file *filp)
1634 {
1635 struct cache_detail *cd = RPC_I(inode)->private;
1636
1637 return cache_release(inode, filp, cd);
1638 }
1639
1640 const struct file_operations cache_file_operations_pipefs = {
1641 .owner = THIS_MODULE,
1642 .llseek = no_llseek,
1643 .read = cache_read_pipefs,
1644 .write = cache_write_pipefs,
1645 .poll = cache_poll_pipefs,
1646 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
1647 .open = cache_open_pipefs,
1648 .release = cache_release_pipefs,
1649 };
1650
1651 static int content_open_pipefs(struct inode *inode, struct file *filp)
1652 {
1653 struct cache_detail *cd = RPC_I(inode)->private;
1654
1655 return content_open(inode, filp, cd);
1656 }
1657
1658 static int content_release_pipefs(struct inode *inode, struct file *filp)
1659 {
1660 struct cache_detail *cd = RPC_I(inode)->private;
1661
1662 return content_release(inode, filp, cd);
1663 }
1664
1665 const struct file_operations content_file_operations_pipefs = {
1666 .open = content_open_pipefs,
1667 .read = seq_read,
1668 .llseek = seq_lseek,
1669 .release = content_release_pipefs,
1670 };
1671
1672 static int open_flush_pipefs(struct inode *inode, struct file *filp)
1673 {
1674 struct cache_detail *cd = RPC_I(inode)->private;
1675
1676 return open_flush(inode, filp, cd);
1677 }
1678
1679 static int release_flush_pipefs(struct inode *inode, struct file *filp)
1680 {
1681 struct cache_detail *cd = RPC_I(inode)->private;
1682
1683 return release_flush(inode, filp, cd);
1684 }
1685
1686 static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1687 size_t count, loff_t *ppos)
1688 {
1689 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1690
1691 return read_flush(filp, buf, count, ppos, cd);
1692 }
1693
1694 static ssize_t write_flush_pipefs(struct file *filp,
1695 const char __user *buf,
1696 size_t count, loff_t *ppos)
1697 {
1698 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1699
1700 return write_flush(filp, buf, count, ppos, cd);
1701 }
1702
1703 const struct file_operations cache_flush_operations_pipefs = {
1704 .open = open_flush_pipefs,
1705 .read = read_flush_pipefs,
1706 .write = write_flush_pipefs,
1707 .release = release_flush_pipefs,
1708 };
1709
1710 int sunrpc_cache_register_pipefs(struct dentry *parent,
1711 const char *name, mode_t umode,
1712 struct cache_detail *cd)
1713 {
1714 struct qstr q;
1715 struct dentry *dir;
1716 int ret = 0;
1717
1718 sunrpc_init_cache_detail(cd);
1719 q.name = name;
1720 q.len = strlen(name);
1721 q.hash = full_name_hash(q.name, q.len);
1722 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1723 if (!IS_ERR(dir))
1724 cd->u.pipefs.dir = dir;
1725 else {
1726 sunrpc_destroy_cache_detail(cd);
1727 ret = PTR_ERR(dir);
1728 }
1729 return ret;
1730 }
1731 EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1732
1733 void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1734 {
1735 rpc_remove_cache_dir(cd->u.pipefs.dir);
1736 cd->u.pipefs.dir = NULL;
1737 sunrpc_destroy_cache_detail(cd);
1738 }
1739 EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1740