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