blob: 824e8534e02210bc755b9a4b37b72067984caa4e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080029#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/ioctls.h>
31#include <linux/sunrpc/types.h>
32#include <linux/sunrpc/cache.h>
33#include <linux/sunrpc/stats.h>
34
35#define RPCDBG_FACILITY RPCDBG_CACHE
36
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -080037static int cache_defer_req(struct cache_req *req, struct cache_head *item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static void cache_revisit_request(struct cache_head *item);
39
Adrian Bunk74cae612006-03-27 01:15:10 -080040static void cache_init(struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 time_t now = get_seconds();
43 h->next = NULL;
44 h->flags = 0;
NeilBrownbaab9352006-03-27 01:15:09 -080045 kref_init(&h->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 h->expiry_time = now + CACHE_NEW_EXPIRY;
47 h->last_refresh = now;
48}
49
NeilBrown15a5f6b2006-03-27 01:15:02 -080050struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
51 struct cache_head *key, int hash)
52{
53 struct cache_head **head, **hp;
54 struct cache_head *new = NULL;
55
56 head = &detail->hash_table[hash];
57
58 read_lock(&detail->hash_lock);
59
60 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
61 struct cache_head *tmp = *hp;
62 if (detail->match(tmp, key)) {
63 cache_get(tmp);
64 read_unlock(&detail->hash_lock);
65 return tmp;
66 }
67 }
68 read_unlock(&detail->hash_lock);
69 /* Didn't find anything, insert an empty entry */
70
71 new = detail->alloc();
72 if (!new)
73 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070074 /* must fully initialise 'new', else
75 * we might get lose if we need to
76 * cache_put it soon.
77 */
NeilBrown15a5f6b2006-03-27 01:15:02 -080078 cache_init(new);
Neil Brown2f349312006-08-05 12:14:29 -070079 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080080
81 write_lock(&detail->hash_lock);
82
83 /* check if entry appeared while we slept */
84 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
85 struct cache_head *tmp = *hp;
86 if (detail->match(tmp, key)) {
87 cache_get(tmp);
88 write_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -080089 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -080090 return tmp;
91 }
92 }
NeilBrown15a5f6b2006-03-27 01:15:02 -080093 new->next = *head;
94 *head = new;
95 detail->entries++;
96 cache_get(new);
97 write_unlock(&detail->hash_lock);
98
99 return new;
100}
101EXPORT_SYMBOL(sunrpc_cache_lookup);
102
NeilBrownebd0cb12006-03-27 01:15:08 -0800103
104static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
105
106static int cache_fresh_locked(struct cache_head *head, time_t expiry)
107{
108 head->expiry_time = expiry;
109 head->last_refresh = get_seconds();
110 return !test_and_set_bit(CACHE_VALID, &head->flags);
111}
112
113static void cache_fresh_unlocked(struct cache_head *head,
114 struct cache_detail *detail, int new)
115{
116 if (new)
117 cache_revisit_request(head);
118 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
119 cache_revisit_request(head);
120 queue_loose(detail, head);
121 }
122}
123
NeilBrown15a5f6b2006-03-27 01:15:02 -0800124struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
125 struct cache_head *new, struct cache_head *old, int hash)
126{
127 /* The 'old' entry is to be replaced by 'new'.
128 * If 'old' is not VALID, we update it directly,
129 * otherwise we need to replace it
130 */
131 struct cache_head **head;
132 struct cache_head *tmp;
NeilBrownebd0cb12006-03-27 01:15:08 -0800133 int is_new;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800134
135 if (!test_bit(CACHE_VALID, &old->flags)) {
136 write_lock(&detail->hash_lock);
137 if (!test_bit(CACHE_VALID, &old->flags)) {
138 if (test_bit(CACHE_NEGATIVE, &new->flags))
139 set_bit(CACHE_NEGATIVE, &old->flags);
140 else
141 detail->update(old, new);
NeilBrownebd0cb12006-03-27 01:15:08 -0800142 is_new = cache_fresh_locked(old, new->expiry_time);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800143 write_unlock(&detail->hash_lock);
NeilBrownebd0cb12006-03-27 01:15:08 -0800144 cache_fresh_unlocked(old, detail, is_new);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800145 return old;
146 }
147 write_unlock(&detail->hash_lock);
148 }
149 /* We need to insert a new entry */
150 tmp = detail->alloc();
151 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800152 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800153 return NULL;
154 }
155 cache_init(tmp);
156 detail->init(tmp, old);
157 head = &detail->hash_table[hash];
158
159 write_lock(&detail->hash_lock);
160 if (test_bit(CACHE_NEGATIVE, &new->flags))
161 set_bit(CACHE_NEGATIVE, &tmp->flags);
162 else
163 detail->update(tmp, new);
164 tmp->next = *head;
165 *head = tmp;
NeilBrownf2d39582006-05-22 22:35:25 -0700166 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800167 cache_get(tmp);
NeilBrownebd0cb12006-03-27 01:15:08 -0800168 is_new = cache_fresh_locked(tmp, new->expiry_time);
169 cache_fresh_locked(old, 0);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800170 write_unlock(&detail->hash_lock);
NeilBrownebd0cb12006-03-27 01:15:08 -0800171 cache_fresh_unlocked(tmp, detail, is_new);
172 cache_fresh_unlocked(old, detail, 0);
NeilBrownbaab9352006-03-27 01:15:09 -0800173 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800174 return tmp;
175}
176EXPORT_SYMBOL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h);
179/*
180 * This is the generic cache management routine for all
181 * the authentication caches.
182 * It checks the currency of a cache item and will (later)
183 * initiate an upcall to fill it if needed.
184 *
185 *
186 * Returns 0 if the cache_head can be used, or cache_puts it and returns
187 * -EAGAIN if upcall is pending,
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800188 * -ETIMEDOUT if upcall failed and should be retried,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * -ENOENT if cache entry was negative
190 */
191int cache_check(struct cache_detail *detail,
192 struct cache_head *h, struct cache_req *rqstp)
193{
194 int rv;
195 long refresh_age, age;
196
197 /* First decide return status as best we can */
198 if (!test_bit(CACHE_VALID, &h->flags) ||
199 h->expiry_time < get_seconds())
200 rv = -EAGAIN;
201 else if (detail->flush_time > h->last_refresh)
202 rv = -EAGAIN;
203 else {
204 /* entry is valid */
205 if (test_bit(CACHE_NEGATIVE, &h->flags))
206 rv = -ENOENT;
207 else rv = 0;
208 }
209
210 /* now see if we want to start an upcall */
211 refresh_age = (h->expiry_time - h->last_refresh);
212 age = get_seconds() - h->last_refresh;
213
214 if (rqstp == NULL) {
215 if (rv == -EAGAIN)
216 rv = -ENOENT;
217 } else if (rv == -EAGAIN || age > refresh_age/2) {
218 dprintk("Want update, refage=%ld, age=%ld\n", refresh_age, age);
219 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
220 switch (cache_make_upcall(detail, h)) {
221 case -EINVAL:
222 clear_bit(CACHE_PENDING, &h->flags);
223 if (rv == -EAGAIN) {
224 set_bit(CACHE_NEGATIVE, &h->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800225 cache_fresh_unlocked(h, detail,
226 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 rv = -ENOENT;
228 }
229 break;
230
231 case -EAGAIN:
232 clear_bit(CACHE_PENDING, &h->flags);
233 cache_revisit_request(h);
234 break;
235 }
236 }
237 }
238
239 if (rv == -EAGAIN)
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800240 if (cache_defer_req(rqstp, h) != 0)
241 rv = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
NeilBrown4013ede2006-03-27 01:15:07 -0800243 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800244 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return rv;
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
249 * caches need to be periodically cleaned.
250 * For this we maintain a list of cache_detail and
251 * a current pointer into that list and into the table
252 * for that entry.
253 *
254 * Each time clean_cache is called it finds the next non-empty entry
255 * in the current table and walks the list in that entry
256 * looking for entries that can be removed.
257 *
258 * An entry gets removed if:
259 * - The expiry is before current time
260 * - The last_refresh time is before the flush_time for that cache
261 *
262 * later we might drop old entries with non-NEVER expiry if that table
263 * is getting 'full' for some definition of 'full'
264 *
265 * The question of "how often to scan a table" is an interesting one
266 * and is answered in part by the use of the "nextcheck" field in the
267 * cache_detail.
268 * When a scan of a table begins, the nextcheck field is set to a time
269 * that is well into the future.
270 * While scanning, if an expiry time is found that is earlier than the
271 * current nextcheck time, nextcheck is set to that expiry time.
272 * If the flush_time is ever set to a time earlier than the nextcheck
273 * time, the nextcheck time is then set to that flush_time.
274 *
275 * A table is then only scanned if the current time is at least
276 * the nextcheck time.
277 *
278 */
279
280static LIST_HEAD(cache_list);
281static DEFINE_SPINLOCK(cache_list_lock);
282static struct cache_detail *current_detail;
283static int current_index;
284
285static struct file_operations cache_file_operations;
286static struct file_operations content_file_operations;
287static struct file_operations cache_flush_operations;
288
David Howells65f27f32006-11-22 14:55:48 +0000289static void do_cache_clean(struct work_struct *work);
290static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292void cache_register(struct cache_detail *cd)
293{
294 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
295 if (cd->proc_ent) {
296 struct proc_dir_entry *p;
Bruce Allanf35279d2005-09-06 15:17:08 -0700297 cd->proc_ent->owner = cd->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 cd->channel_ent = cd->content_ent = NULL;
299
300 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR,
301 cd->proc_ent);
302 cd->flush_ent = p;
303 if (p) {
304 p->proc_fops = &cache_flush_operations;
Bruce Allanf35279d2005-09-06 15:17:08 -0700305 p->owner = cd->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 p->data = cd;
307 }
308
309 if (cd->cache_request || cd->cache_parse) {
310 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
311 cd->proc_ent);
312 cd->channel_ent = p;
313 if (p) {
314 p->proc_fops = &cache_file_operations;
Bruce Allanf35279d2005-09-06 15:17:08 -0700315 p->owner = cd->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 p->data = cd;
317 }
318 }
319 if (cd->cache_show) {
320 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
321 cd->proc_ent);
322 cd->content_ent = p;
323 if (p) {
324 p->proc_fops = &content_file_operations;
Bruce Allanf35279d2005-09-06 15:17:08 -0700325 p->owner = cd->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 p->data = cd;
327 }
328 }
329 }
330 rwlock_init(&cd->hash_lock);
331 INIT_LIST_HEAD(&cd->queue);
332 spin_lock(&cache_list_lock);
333 cd->nextcheck = 0;
334 cd->entries = 0;
335 atomic_set(&cd->readers, 0);
336 cd->last_close = 0;
337 cd->last_warn = -1;
338 list_add(&cd->others, &cache_list);
339 spin_unlock(&cache_list_lock);
340
341 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000342 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345int cache_unregister(struct cache_detail *cd)
346{
347 cache_purge(cd);
348 spin_lock(&cache_list_lock);
349 write_lock(&cd->hash_lock);
350 if (cd->entries || atomic_read(&cd->inuse)) {
351 write_unlock(&cd->hash_lock);
352 spin_unlock(&cache_list_lock);
353 return -EBUSY;
354 }
355 if (current_detail == cd)
356 current_detail = NULL;
357 list_del_init(&cd->others);
358 write_unlock(&cd->hash_lock);
359 spin_unlock(&cache_list_lock);
360 if (cd->proc_ent) {
361 if (cd->flush_ent)
362 remove_proc_entry("flush", cd->proc_ent);
363 if (cd->channel_ent)
364 remove_proc_entry("channel", cd->proc_ent);
365 if (cd->content_ent)
366 remove_proc_entry("content", cd->proc_ent);
367
368 cd->proc_ent = NULL;
369 remove_proc_entry(cd->name, proc_net_rpc);
370 }
371 if (list_empty(&cache_list)) {
372 /* module must be being unloaded so its safe to kill the worker */
373 cancel_delayed_work(&cache_cleaner);
374 flush_scheduled_work();
375 }
376 return 0;
377}
378
379/* clean cache tries to find something to clean
380 * and cleans it.
381 * It returns 1 if it cleaned something,
382 * 0 if it didn't find anything this time
383 * -1 if it fell off the end of the list.
384 */
385static int cache_clean(void)
386{
387 int rv = 0;
388 struct list_head *next;
389
390 spin_lock(&cache_list_lock);
391
392 /* find a suitable table if we don't already have one */
393 while (current_detail == NULL ||
394 current_index >= current_detail->hash_size) {
395 if (current_detail)
396 next = current_detail->others.next;
397 else
398 next = cache_list.next;
399 if (next == &cache_list) {
400 current_detail = NULL;
401 spin_unlock(&cache_list_lock);
402 return -1;
403 }
404 current_detail = list_entry(next, struct cache_detail, others);
405 if (current_detail->nextcheck > get_seconds())
406 current_index = current_detail->hash_size;
407 else {
408 current_index = 0;
409 current_detail->nextcheck = get_seconds()+30*60;
410 }
411 }
412
413 /* find a non-empty bucket in the table */
414 while (current_detail &&
415 current_index < current_detail->hash_size &&
416 current_detail->hash_table[current_index] == NULL)
417 current_index++;
418
419 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
420
421 if (current_detail && current_index < current_detail->hash_size) {
422 struct cache_head *ch, **cp;
423 struct cache_detail *d;
424
425 write_lock(&current_detail->hash_lock);
426
427 /* Ok, now to clean this strand */
428
429 cp = & current_detail->hash_table[current_index];
430 ch = *cp;
431 for (; ch; cp= & ch->next, ch= *cp) {
432 if (current_detail->nextcheck > ch->expiry_time)
433 current_detail->nextcheck = ch->expiry_time+1;
434 if (ch->expiry_time >= get_seconds()
435 && ch->last_refresh >= current_detail->flush_time
436 )
437 continue;
438 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
439 queue_loose(current_detail, ch);
440
NeilBrownbaab9352006-03-27 01:15:09 -0800441 if (atomic_read(&ch->ref.refcount) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
443 }
444 if (ch) {
445 *cp = ch->next;
446 ch->next = NULL;
447 current_detail->entries--;
448 rv = 1;
449 }
450 write_unlock(&current_detail->hash_lock);
451 d = current_detail;
452 if (!ch)
453 current_index ++;
454 spin_unlock(&cache_list_lock);
455 if (ch)
NeilBrownbaab9352006-03-27 01:15:09 -0800456 cache_put(ch, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 } else
458 spin_unlock(&cache_list_lock);
459
460 return rv;
461}
462
463/*
464 * We want to regularly clean the cache, so we need to schedule some work ...
465 */
David Howells65f27f32006-11-22 14:55:48 +0000466static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 int delay = 5;
469 if (cache_clean() == -1)
470 delay = 30*HZ;
471
472 if (list_empty(&cache_list))
473 delay = 0;
474
475 if (delay)
476 schedule_delayed_work(&cache_cleaner, delay);
477}
478
479
480/*
481 * Clean all caches promptly. This just calls cache_clean
482 * repeatedly until we are sure that every cache has had a chance to
483 * be fully cleaned
484 */
485void cache_flush(void)
486{
487 while (cache_clean() != -1)
488 cond_resched();
489 while (cache_clean() != -1)
490 cond_resched();
491}
492
493void cache_purge(struct cache_detail *detail)
494{
495 detail->flush_time = LONG_MAX;
496 detail->nextcheck = get_seconds();
497 cache_flush();
498 detail->flush_time = 1;
499}
500
501
502
503/*
504 * Deferral and Revisiting of Requests.
505 *
506 * If a cache lookup finds a pending entry, we
507 * need to defer the request and revisit it later.
508 * All deferred requests are stored in a hash table,
509 * indexed by "struct cache_head *".
510 * As it may be wasteful to store a whole request
511 * structure, we allow the request to provide a
512 * deferred form, which must contain a
513 * 'struct cache_deferred_req'
514 * This cache_deferred_req contains a method to allow
515 * it to be revisited when cache info is available
516 */
517
518#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
519#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
520
521#define DFR_MAX 300 /* ??? */
522
523static DEFINE_SPINLOCK(cache_defer_lock);
524static LIST_HEAD(cache_defer_list);
525static struct list_head cache_defer_hash[DFR_HASHSIZE];
526static int cache_defer_cnt;
527
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800528static int cache_defer_req(struct cache_req *req, struct cache_head *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct cache_deferred_req *dreq;
531 int hash = DFR_HASH(item);
532
533 dreq = req->defer(req);
534 if (dreq == NULL)
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800535 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 dreq->item = item;
538 dreq->recv_time = get_seconds();
539
540 spin_lock(&cache_defer_lock);
541
542 list_add(&dreq->recent, &cache_defer_list);
543
544 if (cache_defer_hash[hash].next == NULL)
545 INIT_LIST_HEAD(&cache_defer_hash[hash]);
546 list_add(&dreq->hash, &cache_defer_hash[hash]);
547
548 /* it is in, now maybe clean up */
549 dreq = NULL;
550 if (++cache_defer_cnt > DFR_MAX) {
551 /* too much in the cache, randomly drop
552 * first or last
553 */
554 if (net_random()&1)
555 dreq = list_entry(cache_defer_list.next,
556 struct cache_deferred_req,
557 recent);
558 else
559 dreq = list_entry(cache_defer_list.prev,
560 struct cache_deferred_req,
561 recent);
562 list_del(&dreq->recent);
563 list_del(&dreq->hash);
564 cache_defer_cnt--;
565 }
566 spin_unlock(&cache_defer_lock);
567
568 if (dreq) {
569 /* there was one too many */
570 dreq->revisit(dreq, 1);
571 }
NeilBrown4013ede2006-03-27 01:15:07 -0800572 if (!test_bit(CACHE_PENDING, &item->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* must have just been validated... */
574 cache_revisit_request(item);
575 }
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579static void cache_revisit_request(struct cache_head *item)
580{
581 struct cache_deferred_req *dreq;
582 struct list_head pending;
583
584 struct list_head *lp;
585 int hash = DFR_HASH(item);
586
587 INIT_LIST_HEAD(&pending);
588 spin_lock(&cache_defer_lock);
589
590 lp = cache_defer_hash[hash].next;
591 if (lp) {
592 while (lp != &cache_defer_hash[hash]) {
593 dreq = list_entry(lp, struct cache_deferred_req, hash);
594 lp = lp->next;
595 if (dreq->item == item) {
596 list_del(&dreq->hash);
597 list_move(&dreq->recent, &pending);
598 cache_defer_cnt--;
599 }
600 }
601 }
602 spin_unlock(&cache_defer_lock);
603
604 while (!list_empty(&pending)) {
605 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
606 list_del_init(&dreq->recent);
607 dreq->revisit(dreq, 0);
608 }
609}
610
611void cache_clean_deferred(void *owner)
612{
613 struct cache_deferred_req *dreq, *tmp;
614 struct list_head pending;
615
616
617 INIT_LIST_HEAD(&pending);
618 spin_lock(&cache_defer_lock);
619
620 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
621 if (dreq->owner == owner) {
622 list_del(&dreq->hash);
623 list_move(&dreq->recent, &pending);
624 cache_defer_cnt--;
625 }
626 }
627 spin_unlock(&cache_defer_lock);
628
629 while (!list_empty(&pending)) {
630 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
631 list_del_init(&dreq->recent);
632 dreq->revisit(dreq, 1);
633 }
634}
635
636/*
637 * communicate with user-space
638 *
639 * We have a magic /proc file - /proc/sunrpc/cache
640 * On read, you get a full request, or block
641 * On write, an update request is processed
642 * Poll works if anything to read, and always allows write
643 *
644 * Implemented by linked list of requests. Each open file has
645 * a ->private that also exists in this list. New request are added
646 * to the end and may wakeup and preceding readers.
647 * New readers are added to the head. If, on read, an item is found with
648 * CACHE_UPCALLING clear, we free it from the list.
649 *
650 */
651
652static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800653static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655struct cache_queue {
656 struct list_head list;
657 int reader; /* if 0, then request */
658};
659struct cache_request {
660 struct cache_queue q;
661 struct cache_head *item;
662 char * buf;
663 int len;
664 int readers;
665};
666struct cache_reader {
667 struct cache_queue q;
668 int offset; /* if non-0, we have a refcnt on next request */
669};
670
671static ssize_t
672cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
673{
674 struct cache_reader *rp = filp->private_data;
675 struct cache_request *rq;
Josef Sipek303b46b2006-12-08 02:37:42 -0800676 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 int err;
678
679 if (count == 0)
680 return 0;
681
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800682 mutex_lock(&queue_io_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 * readers on this file */
684 again:
685 spin_lock(&queue_lock);
686 /* need to find next request */
687 while (rp->q.list.next != &cd->queue &&
688 list_entry(rp->q.list.next, struct cache_queue, list)
689 ->reader) {
690 struct list_head *next = rp->q.list.next;
691 list_move(&rp->q.list, next);
692 }
693 if (rp->q.list.next == &cd->queue) {
694 spin_unlock(&queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800695 mutex_unlock(&queue_io_mutex);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800696 BUG_ON(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698 }
699 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800700 BUG_ON(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (rp->offset == 0)
702 rq->readers++;
703 spin_unlock(&queue_lock);
704
705 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
706 err = -EAGAIN;
707 spin_lock(&queue_lock);
708 list_move(&rp->q.list, &rq->q.list);
709 spin_unlock(&queue_lock);
710 } else {
711 if (rp->offset + count > rq->len)
712 count = rq->len - rp->offset;
713 err = -EFAULT;
714 if (copy_to_user(buf, rq->buf + rp->offset, count))
715 goto out;
716 rp->offset += count;
717 if (rp->offset >= rq->len) {
718 rp->offset = 0;
719 spin_lock(&queue_lock);
720 list_move(&rp->q.list, &rq->q.list);
721 spin_unlock(&queue_lock);
722 }
723 err = 0;
724 }
725 out:
726 if (rp->offset == 0) {
727 /* need to release rq */
728 spin_lock(&queue_lock);
729 rq->readers--;
730 if (rq->readers == 0 &&
731 !test_bit(CACHE_PENDING, &rq->item->flags)) {
732 list_del(&rq->q.list);
733 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800734 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 kfree(rq->buf);
736 kfree(rq);
737 } else
738 spin_unlock(&queue_lock);
739 }
740 if (err == -EAGAIN)
741 goto again;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800742 mutex_unlock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return err ? err : count;
744}
745
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800746static char write_buf[8192]; /* protected by queue_io_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748static ssize_t
749cache_write(struct file *filp, const char __user *buf, size_t count,
750 loff_t *ppos)
751{
752 int err;
Josef Sipek303b46b2006-12-08 02:37:42 -0800753 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (count == 0)
756 return 0;
757 if (count >= sizeof(write_buf))
758 return -EINVAL;
759
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800760 mutex_lock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (copy_from_user(write_buf, buf, count)) {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800763 mutex_unlock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return -EFAULT;
765 }
766 write_buf[count] = '\0';
767 if (cd->cache_parse)
768 err = cd->cache_parse(cd, write_buf, count);
769 else
770 err = -EINVAL;
771
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800772 mutex_unlock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return err ? err : count;
774}
775
776static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
777
778static unsigned int
779cache_poll(struct file *filp, poll_table *wait)
780{
781 unsigned int mask;
782 struct cache_reader *rp = filp->private_data;
783 struct cache_queue *cq;
Josef Sipek303b46b2006-12-08 02:37:42 -0800784 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 poll_wait(filp, &queue_wait, wait);
787
788 /* alway allow write */
789 mask = POLL_OUT | POLLWRNORM;
790
791 if (!rp)
792 return mask;
793
794 spin_lock(&queue_lock);
795
796 for (cq= &rp->q; &cq->list != &cd->queue;
797 cq = list_entry(cq->list.next, struct cache_queue, list))
798 if (!cq->reader) {
799 mask |= POLLIN | POLLRDNORM;
800 break;
801 }
802 spin_unlock(&queue_lock);
803 return mask;
804}
805
806static int
807cache_ioctl(struct inode *ino, struct file *filp,
808 unsigned int cmd, unsigned long arg)
809{
810 int len = 0;
811 struct cache_reader *rp = filp->private_data;
812 struct cache_queue *cq;
813 struct cache_detail *cd = PDE(ino)->data;
814
815 if (cmd != FIONREAD || !rp)
816 return -EINVAL;
817
818 spin_lock(&queue_lock);
819
820 /* only find the length remaining in current request,
821 * or the length of the next request
822 */
823 for (cq= &rp->q; &cq->list != &cd->queue;
824 cq = list_entry(cq->list.next, struct cache_queue, list))
825 if (!cq->reader) {
826 struct cache_request *cr =
827 container_of(cq, struct cache_request, q);
828 len = cr->len - rp->offset;
829 break;
830 }
831 spin_unlock(&queue_lock);
832
833 return put_user(len, (int __user *)arg);
834}
835
836static int
837cache_open(struct inode *inode, struct file *filp)
838{
839 struct cache_reader *rp = NULL;
840
841 nonseekable_open(inode, filp);
842 if (filp->f_mode & FMODE_READ) {
843 struct cache_detail *cd = PDE(inode)->data;
844
845 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
846 if (!rp)
847 return -ENOMEM;
848 rp->offset = 0;
849 rp->q.reader = 1;
850 atomic_inc(&cd->readers);
851 spin_lock(&queue_lock);
852 list_add(&rp->q.list, &cd->queue);
853 spin_unlock(&queue_lock);
854 }
855 filp->private_data = rp;
856 return 0;
857}
858
859static int
860cache_release(struct inode *inode, struct file *filp)
861{
862 struct cache_reader *rp = filp->private_data;
863 struct cache_detail *cd = PDE(inode)->data;
864
865 if (rp) {
866 spin_lock(&queue_lock);
867 if (rp->offset) {
868 struct cache_queue *cq;
869 for (cq= &rp->q; &cq->list != &cd->queue;
870 cq = list_entry(cq->list.next, struct cache_queue, list))
871 if (!cq->reader) {
872 container_of(cq, struct cache_request, q)
873 ->readers--;
874 break;
875 }
876 rp->offset = 0;
877 }
878 list_del(&rp->q.list);
879 spin_unlock(&queue_lock);
880
881 filp->private_data = NULL;
882 kfree(rp);
883
884 cd->last_close = get_seconds();
885 atomic_dec(&cd->readers);
886 }
887 return 0;
888}
889
890
891
892static struct file_operations cache_file_operations = {
893 .owner = THIS_MODULE,
894 .llseek = no_llseek,
895 .read = cache_read,
896 .write = cache_write,
897 .poll = cache_poll,
898 .ioctl = cache_ioctl, /* for FIONREAD */
899 .open = cache_open,
900 .release = cache_release,
901};
902
903
904static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
905{
906 struct cache_queue *cq;
907 spin_lock(&queue_lock);
908 list_for_each_entry(cq, &detail->queue, list)
909 if (!cq->reader) {
910 struct cache_request *cr = container_of(cq, struct cache_request, q);
911 if (cr->item != ch)
912 continue;
913 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -0800914 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 list_del(&cr->q.list);
916 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800917 cache_put(cr->item, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 kfree(cr->buf);
919 kfree(cr);
920 return;
921 }
922 spin_unlock(&queue_lock);
923}
924
925/*
926 * Support routines for text-based upcalls.
927 * Fields are separated by spaces.
928 * Fields are either mangled to quote space tab newline slosh with slosh
929 * or a hexified with a leading \x
930 * Record is terminated with newline.
931 *
932 */
933
934void qword_add(char **bpp, int *lp, char *str)
935{
936 char *bp = *bpp;
937 int len = *lp;
938 char c;
939
940 if (len < 0) return;
941
942 while ((c=*str++) && len)
943 switch(c) {
944 case ' ':
945 case '\t':
946 case '\n':
947 case '\\':
948 if (len >= 4) {
949 *bp++ = '\\';
950 *bp++ = '0' + ((c & 0300)>>6);
951 *bp++ = '0' + ((c & 0070)>>3);
952 *bp++ = '0' + ((c & 0007)>>0);
953 }
954 len -= 4;
955 break;
956 default:
957 *bp++ = c;
958 len--;
959 }
960 if (c || len <1) len = -1;
961 else {
962 *bp++ = ' ';
963 len--;
964 }
965 *bpp = bp;
966 *lp = len;
967}
968
969void qword_addhex(char **bpp, int *lp, char *buf, int blen)
970{
971 char *bp = *bpp;
972 int len = *lp;
973
974 if (len < 0) return;
975
976 if (len > 2) {
977 *bp++ = '\\';
978 *bp++ = 'x';
979 len -= 2;
980 while (blen && len >= 2) {
981 unsigned char c = *buf++;
982 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
983 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
984 len -= 2;
985 blen--;
986 }
987 }
988 if (blen || len<1) len = -1;
989 else {
990 *bp++ = ' ';
991 len--;
992 }
993 *bpp = bp;
994 *lp = len;
995}
996
997static void warn_no_listener(struct cache_detail *detail)
998{
999 if (detail->last_warn != detail->last_close) {
1000 detail->last_warn = detail->last_close;
1001 if (detail->warn_no_listener)
1002 detail->warn_no_listener(detail);
1003 }
1004}
1005
1006/*
1007 * register an upcall request to user-space.
1008 * Each request is at most one page long.
1009 */
1010static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
1011{
1012
1013 char *buf;
1014 struct cache_request *crq;
1015 char *bp;
1016 int len;
1017
1018 if (detail->cache_request == NULL)
1019 return -EINVAL;
1020
1021 if (atomic_read(&detail->readers) == 0 &&
1022 detail->last_close < get_seconds() - 30) {
1023 warn_no_listener(detail);
1024 return -EINVAL;
1025 }
1026
1027 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1028 if (!buf)
1029 return -EAGAIN;
1030
1031 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1032 if (!crq) {
1033 kfree(buf);
1034 return -EAGAIN;
1035 }
1036
1037 bp = buf; len = PAGE_SIZE;
1038
1039 detail->cache_request(detail, h, &bp, &len);
1040
1041 if (len < 0) {
1042 kfree(buf);
1043 kfree(crq);
1044 return -EAGAIN;
1045 }
1046 crq->q.reader = 0;
1047 crq->item = cache_get(h);
1048 crq->buf = buf;
1049 crq->len = PAGE_SIZE - len;
1050 crq->readers = 0;
1051 spin_lock(&queue_lock);
1052 list_add_tail(&crq->q.list, &detail->queue);
1053 spin_unlock(&queue_lock);
1054 wake_up(&queue_wait);
1055 return 0;
1056}
1057
1058/*
1059 * parse a message from user-space and pass it
1060 * to an appropriate cache
1061 * Messages are, like requests, separated into fields by
1062 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1063 *
1064 * Message is
1065 * reply cachename expiry key ... content....
1066 *
1067 * key and content are both parsed by cache
1068 */
1069
1070#define isodigit(c) (isdigit(c) && c <= '7')
1071int qword_get(char **bpp, char *dest, int bufsize)
1072{
1073 /* return bytes copied, or -1 on error */
1074 char *bp = *bpp;
1075 int len = 0;
1076
1077 while (*bp == ' ') bp++;
1078
1079 if (bp[0] == '\\' && bp[1] == 'x') {
1080 /* HEX STRING */
1081 bp += 2;
1082 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1083 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1084 bp++;
1085 byte <<= 4;
1086 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1087 *dest++ = byte;
1088 bp++;
1089 len++;
1090 }
1091 } else {
1092 /* text with \nnn octal quoting */
1093 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1094 if (*bp == '\\' &&
1095 isodigit(bp[1]) && (bp[1] <= '3') &&
1096 isodigit(bp[2]) &&
1097 isodigit(bp[3])) {
1098 int byte = (*++bp -'0');
1099 bp++;
1100 byte = (byte << 3) | (*bp++ - '0');
1101 byte = (byte << 3) | (*bp++ - '0');
1102 *dest++ = byte;
1103 len++;
1104 } else {
1105 *dest++ = *bp++;
1106 len++;
1107 }
1108 }
1109 }
1110
1111 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1112 return -1;
1113 while (*bp == ' ') bp++;
1114 *bpp = bp;
1115 *dest = '\0';
1116 return len;
1117}
1118
1119
1120/*
1121 * support /proc/sunrpc/cache/$CACHENAME/content
1122 * as a seqfile.
1123 * We call ->cache_show passing NULL for the item to
1124 * get a header, then pass each real item in the cache
1125 */
1126
1127struct handle {
1128 struct cache_detail *cd;
1129};
1130
1131static void *c_start(struct seq_file *m, loff_t *pos)
1132{
1133 loff_t n = *pos;
1134 unsigned hash, entry;
1135 struct cache_head *ch;
1136 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1137
1138
1139 read_lock(&cd->hash_lock);
1140 if (!n--)
1141 return SEQ_START_TOKEN;
1142 hash = n >> 32;
1143 entry = n & ((1LL<<32) - 1);
1144
1145 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1146 if (!entry--)
1147 return ch;
1148 n &= ~((1LL<<32) - 1);
1149 do {
1150 hash++;
1151 n += 1LL<<32;
1152 } while(hash < cd->hash_size &&
1153 cd->hash_table[hash]==NULL);
1154 if (hash >= cd->hash_size)
1155 return NULL;
1156 *pos = n+1;
1157 return cd->hash_table[hash];
1158}
1159
1160static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1161{
1162 struct cache_head *ch = p;
1163 int hash = (*pos >> 32);
1164 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1165
1166 if (p == SEQ_START_TOKEN)
1167 hash = 0;
1168 else if (ch->next == NULL) {
1169 hash++;
1170 *pos += 1LL<<32;
1171 } else {
1172 ++*pos;
1173 return ch->next;
1174 }
1175 *pos &= ~((1LL<<32) - 1);
1176 while (hash < cd->hash_size &&
1177 cd->hash_table[hash] == NULL) {
1178 hash++;
1179 *pos += 1LL<<32;
1180 }
1181 if (hash >= cd->hash_size)
1182 return NULL;
1183 ++*pos;
1184 return cd->hash_table[hash];
1185}
1186
1187static void c_stop(struct seq_file *m, void *p)
1188{
1189 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1190 read_unlock(&cd->hash_lock);
1191}
1192
1193static int c_show(struct seq_file *m, void *p)
1194{
1195 struct cache_head *cp = p;
1196 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1197
1198 if (p == SEQ_START_TOKEN)
1199 return cd->cache_show(m, cd, NULL);
1200
1201 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001202 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownbaab9352006-03-27 01:15:09 -08001203 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 cache_get(cp);
1205 if (cache_check(cd, cp, NULL))
1206 /* cache_check does a cache_put on failure */
1207 seq_printf(m, "# ");
1208 else
1209 cache_put(cp, cd);
1210
1211 return cd->cache_show(m, cd, cp);
1212}
1213
1214static struct seq_operations cache_content_op = {
1215 .start = c_start,
1216 .next = c_next,
1217 .stop = c_stop,
1218 .show = c_show,
1219};
1220
1221static int content_open(struct inode *inode, struct file *file)
1222{
1223 int res;
1224 struct handle *han;
1225 struct cache_detail *cd = PDE(inode)->data;
1226
1227 han = kmalloc(sizeof(*han), GFP_KERNEL);
1228 if (han == NULL)
1229 return -ENOMEM;
1230
1231 han->cd = cd;
1232
1233 res = seq_open(file, &cache_content_op);
1234 if (res)
1235 kfree(han);
1236 else
1237 ((struct seq_file *)file->private_data)->private = han;
1238
1239 return res;
1240}
1241static int content_release(struct inode *inode, struct file *file)
1242{
1243 struct seq_file *m = (struct seq_file *)file->private_data;
1244 struct handle *han = m->private;
1245 kfree(han);
1246 m->private = NULL;
1247 return seq_release(inode, file);
1248}
1249
1250static struct file_operations content_file_operations = {
1251 .open = content_open,
1252 .read = seq_read,
1253 .llseek = seq_lseek,
1254 .release = content_release,
1255};
1256
1257static ssize_t read_flush(struct file *file, char __user *buf,
1258 size_t count, loff_t *ppos)
1259{
Josef Sipek303b46b2006-12-08 02:37:42 -08001260 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 char tbuf[20];
1262 unsigned long p = *ppos;
1263 int len;
1264
1265 sprintf(tbuf, "%lu\n", cd->flush_time);
1266 len = strlen(tbuf);
1267 if (p >= len)
1268 return 0;
1269 len -= p;
1270 if (len > count) len = count;
1271 if (copy_to_user(buf, (void*)(tbuf+p), len))
1272 len = -EFAULT;
1273 else
1274 *ppos += len;
1275 return len;
1276}
1277
1278static ssize_t write_flush(struct file * file, const char __user * buf,
1279 size_t count, loff_t *ppos)
1280{
Josef Sipek303b46b2006-12-08 02:37:42 -08001281 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 char tbuf[20];
1283 char *ep;
1284 long flushtime;
1285 if (*ppos || count > sizeof(tbuf)-1)
1286 return -EINVAL;
1287 if (copy_from_user(tbuf, buf, count))
1288 return -EFAULT;
1289 tbuf[count] = 0;
1290 flushtime = simple_strtoul(tbuf, &ep, 0);
1291 if (*ep && *ep != '\n')
1292 return -EINVAL;
1293
1294 cd->flush_time = flushtime;
1295 cd->nextcheck = get_seconds();
1296 cache_flush();
1297
1298 *ppos += count;
1299 return count;
1300}
1301
1302static struct file_operations cache_flush_operations = {
1303 .open = nonseekable_open,
1304 .read = read_flush,
1305 .write = write_flush,
1306};