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