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