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