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