blob: f41a7cc4cf628799b89da3eaf4009374460bc62f [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) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500218 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
219 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
221 switch (cache_make_upcall(detail, h)) {
222 case -EINVAL:
223 clear_bit(CACHE_PENDING, &h->flags);
224 if (rv == -EAGAIN) {
225 set_bit(CACHE_NEGATIVE, &h->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800226 cache_fresh_unlocked(h, detail,
227 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 rv = -ENOENT;
229 }
230 break;
231
232 case -EAGAIN:
233 clear_bit(CACHE_PENDING, &h->flags);
234 cache_revisit_request(h);
235 break;
236 }
237 }
238 }
239
240 if (rv == -EAGAIN)
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800241 if (cache_defer_req(rqstp, h) != 0)
242 rv = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
NeilBrown4013ede2006-03-27 01:15:07 -0800244 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800245 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return rv;
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*
250 * caches need to be periodically cleaned.
251 * For this we maintain a list of cache_detail and
252 * a current pointer into that list and into the table
253 * for that entry.
254 *
255 * Each time clean_cache is called it finds the next non-empty entry
256 * in the current table and walks the list in that entry
257 * looking for entries that can be removed.
258 *
259 * An entry gets removed if:
260 * - The expiry is before current time
261 * - The last_refresh time is before the flush_time for that cache
262 *
263 * later we might drop old entries with non-NEVER expiry if that table
264 * is getting 'full' for some definition of 'full'
265 *
266 * The question of "how often to scan a table" is an interesting one
267 * and is answered in part by the use of the "nextcheck" field in the
268 * cache_detail.
269 * When a scan of a table begins, the nextcheck field is set to a time
270 * that is well into the future.
271 * While scanning, if an expiry time is found that is earlier than the
272 * current nextcheck time, nextcheck is set to that expiry time.
273 * If the flush_time is ever set to a time earlier than the nextcheck
274 * time, the nextcheck time is then set to that flush_time.
275 *
276 * A table is then only scanned if the current time is at least
277 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800278 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280
281static LIST_HEAD(cache_list);
282static DEFINE_SPINLOCK(cache_list_lock);
283static struct cache_detail *current_detail;
284static int current_index;
285
Arjan van de Venda7071d2007-02-12 00:55:36 -0800286static const struct file_operations cache_file_operations;
287static const struct file_operations content_file_operations;
288static const struct file_operations cache_flush_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
David Howells65f27f32006-11-22 14:55:48 +0000290static void do_cache_clean(struct work_struct *work);
291static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500293static void remove_cache_proc_entries(struct cache_detail *cd)
294{
295 if (cd->proc_ent == NULL)
296 return;
297 if (cd->flush_ent)
298 remove_proc_entry("flush", cd->proc_ent);
299 if (cd->channel_ent)
300 remove_proc_entry("channel", cd->proc_ent);
301 if (cd->content_ent)
302 remove_proc_entry("content", cd->proc_ent);
303 cd->proc_ent = NULL;
304 remove_proc_entry(cd->name, proc_net_rpc);
305}
306
307static void create_cache_proc_entries(struct cache_detail *cd)
308{
309 struct proc_dir_entry *p;
310
311 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
312 if (cd->proc_ent == NULL)
313 return;
314 cd->proc_ent->owner = cd->owner;
315 cd->channel_ent = cd->content_ent = NULL;
316
317 p = create_proc_entry("flush", S_IFREG|S_IRUSR|S_IWUSR, cd->proc_ent);
318 cd->flush_ent = p;
319 if (p == NULL)
320 return;
321 p->proc_fops = &cache_flush_operations;
322 p->owner = cd->owner;
323 p->data = cd;
324
325 if (cd->cache_request || cd->cache_parse) {
326 p = create_proc_entry("channel", S_IFREG|S_IRUSR|S_IWUSR,
327 cd->proc_ent);
328 cd->channel_ent = p;
329 if (p == NULL)
330 return;
331 p->proc_fops = &cache_file_operations;
332 p->owner = cd->owner;
333 p->data = cd;
334 }
335 if (cd->cache_show) {
336 p = create_proc_entry("content", S_IFREG|S_IRUSR|S_IWUSR,
337 cd->proc_ent);
338 cd->content_ent = p;
339 if (p == NULL)
340 return;
341 p->proc_fops = &content_file_operations;
342 p->owner = cd->owner;
343 p->data = cd;
344 }
345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347void cache_register(struct cache_detail *cd)
348{
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500349 create_cache_proc_entries(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 rwlock_init(&cd->hash_lock);
351 INIT_LIST_HEAD(&cd->queue);
352 spin_lock(&cache_list_lock);
353 cd->nextcheck = 0;
354 cd->entries = 0;
355 atomic_set(&cd->readers, 0);
356 cd->last_close = 0;
357 cd->last_warn = -1;
358 list_add(&cd->others, &cache_list);
359 spin_unlock(&cache_list_lock);
360
361 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000362 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500365void cache_unregister(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 cache_purge(cd);
368 spin_lock(&cache_list_lock);
369 write_lock(&cd->hash_lock);
370 if (cd->entries || atomic_read(&cd->inuse)) {
371 write_unlock(&cd->hash_lock);
372 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500373 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 if (current_detail == cd)
376 current_detail = NULL;
377 list_del_init(&cd->others);
378 write_unlock(&cd->hash_lock);
379 spin_unlock(&cache_list_lock);
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500380 remove_cache_proc_entries(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (list_empty(&cache_list)) {
382 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400383 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500385 return;
386out:
387 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390/* clean cache tries to find something to clean
391 * and cleans it.
392 * It returns 1 if it cleaned something,
393 * 0 if it didn't find anything this time
394 * -1 if it fell off the end of the list.
395 */
396static int cache_clean(void)
397{
398 int rv = 0;
399 struct list_head *next;
400
401 spin_lock(&cache_list_lock);
402
403 /* find a suitable table if we don't already have one */
404 while (current_detail == NULL ||
405 current_index >= current_detail->hash_size) {
406 if (current_detail)
407 next = current_detail->others.next;
408 else
409 next = cache_list.next;
410 if (next == &cache_list) {
411 current_detail = NULL;
412 spin_unlock(&cache_list_lock);
413 return -1;
414 }
415 current_detail = list_entry(next, struct cache_detail, others);
416 if (current_detail->nextcheck > get_seconds())
417 current_index = current_detail->hash_size;
418 else {
419 current_index = 0;
420 current_detail->nextcheck = get_seconds()+30*60;
421 }
422 }
423
424 /* find a non-empty bucket in the table */
425 while (current_detail &&
426 current_index < current_detail->hash_size &&
427 current_detail->hash_table[current_index] == NULL)
428 current_index++;
429
430 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (current_detail && current_index < current_detail->hash_size) {
433 struct cache_head *ch, **cp;
434 struct cache_detail *d;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 write_lock(&current_detail->hash_lock);
437
438 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 cp = & current_detail->hash_table[current_index];
441 ch = *cp;
442 for (; ch; cp= & ch->next, ch= *cp) {
443 if (current_detail->nextcheck > ch->expiry_time)
444 current_detail->nextcheck = ch->expiry_time+1;
445 if (ch->expiry_time >= get_seconds()
446 && ch->last_refresh >= current_detail->flush_time
447 )
448 continue;
449 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
450 queue_loose(current_detail, ch);
451
NeilBrownbaab9352006-03-27 01:15:09 -0800452 if (atomic_read(&ch->ref.refcount) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
454 }
455 if (ch) {
456 *cp = ch->next;
457 ch->next = NULL;
458 current_detail->entries--;
459 rv = 1;
460 }
461 write_unlock(&current_detail->hash_lock);
462 d = current_detail;
463 if (!ch)
464 current_index ++;
465 spin_unlock(&cache_list_lock);
466 if (ch)
NeilBrownbaab9352006-03-27 01:15:09 -0800467 cache_put(ch, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 } else
469 spin_unlock(&cache_list_lock);
470
471 return rv;
472}
473
474/*
475 * We want to regularly clean the cache, so we need to schedule some work ...
476 */
David Howells65f27f32006-11-22 14:55:48 +0000477static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 int delay = 5;
480 if (cache_clean() == -1)
481 delay = 30*HZ;
482
483 if (list_empty(&cache_list))
484 delay = 0;
485
486 if (delay)
487 schedule_delayed_work(&cache_cleaner, delay);
488}
489
490
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800491/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800493 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * be fully cleaned
495 */
496void cache_flush(void)
497{
498 while (cache_clean() != -1)
499 cond_resched();
500 while (cache_clean() != -1)
501 cond_resched();
502}
503
504void cache_purge(struct cache_detail *detail)
505{
506 detail->flush_time = LONG_MAX;
507 detail->nextcheck = get_seconds();
508 cache_flush();
509 detail->flush_time = 1;
510}
511
512
513
514/*
515 * Deferral and Revisiting of Requests.
516 *
517 * If a cache lookup finds a pending entry, we
518 * need to defer the request and revisit it later.
519 * All deferred requests are stored in a hash table,
520 * indexed by "struct cache_head *".
521 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800522 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * deferred form, which must contain a
524 * 'struct cache_deferred_req'
525 * This cache_deferred_req contains a method to allow
526 * it to be revisited when cache info is available
527 */
528
529#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
530#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
531
532#define DFR_MAX 300 /* ??? */
533
534static DEFINE_SPINLOCK(cache_defer_lock);
535static LIST_HEAD(cache_defer_list);
536static struct list_head cache_defer_hash[DFR_HASHSIZE];
537static int cache_defer_cnt;
538
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800539static int cache_defer_req(struct cache_req *req, struct cache_head *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct cache_deferred_req *dreq;
542 int hash = DFR_HASH(item);
543
J.Bruce Fields01f3bd12006-12-13 00:35:26 -0800544 if (cache_defer_cnt >= DFR_MAX) {
545 /* too much in the cache, randomly drop this one,
546 * or continue and drop the oldest below
547 */
548 if (net_random()&1)
549 return -ETIMEDOUT;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 dreq = req->defer(req);
552 if (dreq == NULL)
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800553 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 dreq->item = item;
556 dreq->recv_time = get_seconds();
557
558 spin_lock(&cache_defer_lock);
559
560 list_add(&dreq->recent, &cache_defer_list);
561
562 if (cache_defer_hash[hash].next == NULL)
563 INIT_LIST_HEAD(&cache_defer_hash[hash]);
564 list_add(&dreq->hash, &cache_defer_hash[hash]);
565
566 /* it is in, now maybe clean up */
567 dreq = NULL;
568 if (++cache_defer_cnt > DFR_MAX) {
J.Bruce Fields01f3bd12006-12-13 00:35:26 -0800569 dreq = list_entry(cache_defer_list.prev,
570 struct cache_deferred_req, recent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 list_del(&dreq->recent);
572 list_del(&dreq->hash);
573 cache_defer_cnt--;
574 }
575 spin_unlock(&cache_defer_lock);
576
577 if (dreq) {
578 /* there was one too many */
579 dreq->revisit(dreq, 1);
580 }
NeilBrown4013ede2006-03-27 01:15:07 -0800581 if (!test_bit(CACHE_PENDING, &item->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* must have just been validated... */
583 cache_revisit_request(item);
584 }
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588static void cache_revisit_request(struct cache_head *item)
589{
590 struct cache_deferred_req *dreq;
591 struct list_head pending;
592
593 struct list_head *lp;
594 int hash = DFR_HASH(item);
595
596 INIT_LIST_HEAD(&pending);
597 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 lp = cache_defer_hash[hash].next;
600 if (lp) {
601 while (lp != &cache_defer_hash[hash]) {
602 dreq = list_entry(lp, struct cache_deferred_req, hash);
603 lp = lp->next;
604 if (dreq->item == item) {
605 list_del(&dreq->hash);
606 list_move(&dreq->recent, &pending);
607 cache_defer_cnt--;
608 }
609 }
610 }
611 spin_unlock(&cache_defer_lock);
612
613 while (!list_empty(&pending)) {
614 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
615 list_del_init(&dreq->recent);
616 dreq->revisit(dreq, 0);
617 }
618}
619
620void cache_clean_deferred(void *owner)
621{
622 struct cache_deferred_req *dreq, *tmp;
623 struct list_head pending;
624
625
626 INIT_LIST_HEAD(&pending);
627 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
630 if (dreq->owner == owner) {
631 list_del(&dreq->hash);
632 list_move(&dreq->recent, &pending);
633 cache_defer_cnt--;
634 }
635 }
636 spin_unlock(&cache_defer_lock);
637
638 while (!list_empty(&pending)) {
639 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
640 list_del_init(&dreq->recent);
641 dreq->revisit(dreq, 1);
642 }
643}
644
645/*
646 * communicate with user-space
647 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500648 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
649 * On read, you get a full request, or block.
650 * On write, an update request is processed.
651 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800653 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500654 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * to the end and may wakeup and preceding readers.
656 * New readers are added to the head. If, on read, an item is found with
657 * CACHE_UPCALLING clear, we free it from the list.
658 *
659 */
660
661static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800662static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664struct cache_queue {
665 struct list_head list;
666 int reader; /* if 0, then request */
667};
668struct cache_request {
669 struct cache_queue q;
670 struct cache_head *item;
671 char * buf;
672 int len;
673 int readers;
674};
675struct cache_reader {
676 struct cache_queue q;
677 int offset; /* if non-0, we have a refcnt on next request */
678};
679
680static ssize_t
681cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
682{
683 struct cache_reader *rp = filp->private_data;
684 struct cache_request *rq;
Josef Sipek303b46b2006-12-08 02:37:42 -0800685 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 int err;
687
688 if (count == 0)
689 return 0;
690
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800691 mutex_lock(&queue_io_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * readers on this file */
693 again:
694 spin_lock(&queue_lock);
695 /* need to find next request */
696 while (rp->q.list.next != &cd->queue &&
697 list_entry(rp->q.list.next, struct cache_queue, list)
698 ->reader) {
699 struct list_head *next = rp->q.list.next;
700 list_move(&rp->q.list, next);
701 }
702 if (rp->q.list.next == &cd->queue) {
703 spin_unlock(&queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800704 mutex_unlock(&queue_io_mutex);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800705 BUG_ON(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
707 }
708 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800709 BUG_ON(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (rp->offset == 0)
711 rq->readers++;
712 spin_unlock(&queue_lock);
713
714 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
715 err = -EAGAIN;
716 spin_lock(&queue_lock);
717 list_move(&rp->q.list, &rq->q.list);
718 spin_unlock(&queue_lock);
719 } else {
720 if (rp->offset + count > rq->len)
721 count = rq->len - rp->offset;
722 err = -EFAULT;
723 if (copy_to_user(buf, rq->buf + rp->offset, count))
724 goto out;
725 rp->offset += count;
726 if (rp->offset >= rq->len) {
727 rp->offset = 0;
728 spin_lock(&queue_lock);
729 list_move(&rp->q.list, &rq->q.list);
730 spin_unlock(&queue_lock);
731 }
732 err = 0;
733 }
734 out:
735 if (rp->offset == 0) {
736 /* need to release rq */
737 spin_lock(&queue_lock);
738 rq->readers--;
739 if (rq->readers == 0 &&
740 !test_bit(CACHE_PENDING, &rq->item->flags)) {
741 list_del(&rq->q.list);
742 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800743 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 kfree(rq->buf);
745 kfree(rq);
746 } else
747 spin_unlock(&queue_lock);
748 }
749 if (err == -EAGAIN)
750 goto again;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800751 mutex_unlock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return err ? err : count;
753}
754
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800755static char write_buf[8192]; /* protected by queue_io_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757static ssize_t
758cache_write(struct file *filp, const char __user *buf, size_t count,
759 loff_t *ppos)
760{
761 int err;
Josef Sipek303b46b2006-12-08 02:37:42 -0800762 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (count == 0)
765 return 0;
766 if (count >= sizeof(write_buf))
767 return -EINVAL;
768
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800769 mutex_lock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (copy_from_user(write_buf, buf, count)) {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800772 mutex_unlock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -EFAULT;
774 }
775 write_buf[count] = '\0';
776 if (cd->cache_parse)
777 err = cd->cache_parse(cd, write_buf, count);
778 else
779 err = -EINVAL;
780
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800781 mutex_unlock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return err ? err : count;
783}
784
785static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
786
787static unsigned int
788cache_poll(struct file *filp, poll_table *wait)
789{
790 unsigned int mask;
791 struct cache_reader *rp = filp->private_data;
792 struct cache_queue *cq;
Josef Sipek303b46b2006-12-08 02:37:42 -0800793 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 poll_wait(filp, &queue_wait, wait);
796
797 /* alway allow write */
798 mask = POLL_OUT | POLLWRNORM;
799
800 if (!rp)
801 return mask;
802
803 spin_lock(&queue_lock);
804
805 for (cq= &rp->q; &cq->list != &cd->queue;
806 cq = list_entry(cq->list.next, struct cache_queue, list))
807 if (!cq->reader) {
808 mask |= POLLIN | POLLRDNORM;
809 break;
810 }
811 spin_unlock(&queue_lock);
812 return mask;
813}
814
815static int
816cache_ioctl(struct inode *ino, struct file *filp,
817 unsigned int cmd, unsigned long arg)
818{
819 int len = 0;
820 struct cache_reader *rp = filp->private_data;
821 struct cache_queue *cq;
822 struct cache_detail *cd = PDE(ino)->data;
823
824 if (cmd != FIONREAD || !rp)
825 return -EINVAL;
826
827 spin_lock(&queue_lock);
828
829 /* only find the length remaining in current request,
830 * or the length of the next request
831 */
832 for (cq= &rp->q; &cq->list != &cd->queue;
833 cq = list_entry(cq->list.next, struct cache_queue, list))
834 if (!cq->reader) {
835 struct cache_request *cr =
836 container_of(cq, struct cache_request, q);
837 len = cr->len - rp->offset;
838 break;
839 }
840 spin_unlock(&queue_lock);
841
842 return put_user(len, (int __user *)arg);
843}
844
845static int
846cache_open(struct inode *inode, struct file *filp)
847{
848 struct cache_reader *rp = NULL;
849
850 nonseekable_open(inode, filp);
851 if (filp->f_mode & FMODE_READ) {
852 struct cache_detail *cd = PDE(inode)->data;
853
854 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
855 if (!rp)
856 return -ENOMEM;
857 rp->offset = 0;
858 rp->q.reader = 1;
859 atomic_inc(&cd->readers);
860 spin_lock(&queue_lock);
861 list_add(&rp->q.list, &cd->queue);
862 spin_unlock(&queue_lock);
863 }
864 filp->private_data = rp;
865 return 0;
866}
867
868static int
869cache_release(struct inode *inode, struct file *filp)
870{
871 struct cache_reader *rp = filp->private_data;
872 struct cache_detail *cd = PDE(inode)->data;
873
874 if (rp) {
875 spin_lock(&queue_lock);
876 if (rp->offset) {
877 struct cache_queue *cq;
878 for (cq= &rp->q; &cq->list != &cd->queue;
879 cq = list_entry(cq->list.next, struct cache_queue, list))
880 if (!cq->reader) {
881 container_of(cq, struct cache_request, q)
882 ->readers--;
883 break;
884 }
885 rp->offset = 0;
886 }
887 list_del(&rp->q.list);
888 spin_unlock(&queue_lock);
889
890 filp->private_data = NULL;
891 kfree(rp);
892
893 cd->last_close = get_seconds();
894 atomic_dec(&cd->readers);
895 }
896 return 0;
897}
898
899
900
Arjan van de Venda7071d2007-02-12 00:55:36 -0800901static const struct file_operations cache_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 .owner = THIS_MODULE,
903 .llseek = no_llseek,
904 .read = cache_read,
905 .write = cache_write,
906 .poll = cache_poll,
907 .ioctl = cache_ioctl, /* for FIONREAD */
908 .open = cache_open,
909 .release = cache_release,
910};
911
912
913static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
914{
915 struct cache_queue *cq;
916 spin_lock(&queue_lock);
917 list_for_each_entry(cq, &detail->queue, list)
918 if (!cq->reader) {
919 struct cache_request *cr = container_of(cq, struct cache_request, q);
920 if (cr->item != ch)
921 continue;
922 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -0800923 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 list_del(&cr->q.list);
925 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800926 cache_put(cr->item, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 kfree(cr->buf);
928 kfree(cr);
929 return;
930 }
931 spin_unlock(&queue_lock);
932}
933
934/*
935 * Support routines for text-based upcalls.
936 * Fields are separated by spaces.
937 * Fields are either mangled to quote space tab newline slosh with slosh
938 * or a hexified with a leading \x
939 * Record is terminated with newline.
940 *
941 */
942
943void qword_add(char **bpp, int *lp, char *str)
944{
945 char *bp = *bpp;
946 int len = *lp;
947 char c;
948
949 if (len < 0) return;
950
951 while ((c=*str++) && len)
952 switch(c) {
953 case ' ':
954 case '\t':
955 case '\n':
956 case '\\':
957 if (len >= 4) {
958 *bp++ = '\\';
959 *bp++ = '0' + ((c & 0300)>>6);
960 *bp++ = '0' + ((c & 0070)>>3);
961 *bp++ = '0' + ((c & 0007)>>0);
962 }
963 len -= 4;
964 break;
965 default:
966 *bp++ = c;
967 len--;
968 }
969 if (c || len <1) len = -1;
970 else {
971 *bp++ = ' ';
972 len--;
973 }
974 *bpp = bp;
975 *lp = len;
976}
977
978void qword_addhex(char **bpp, int *lp, char *buf, int blen)
979{
980 char *bp = *bpp;
981 int len = *lp;
982
983 if (len < 0) return;
984
985 if (len > 2) {
986 *bp++ = '\\';
987 *bp++ = 'x';
988 len -= 2;
989 while (blen && len >= 2) {
990 unsigned char c = *buf++;
991 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
992 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
993 len -= 2;
994 blen--;
995 }
996 }
997 if (blen || len<1) len = -1;
998 else {
999 *bp++ = ' ';
1000 len--;
1001 }
1002 *bpp = bp;
1003 *lp = len;
1004}
1005
1006static void warn_no_listener(struct cache_detail *detail)
1007{
1008 if (detail->last_warn != detail->last_close) {
1009 detail->last_warn = detail->last_close;
1010 if (detail->warn_no_listener)
1011 detail->warn_no_listener(detail);
1012 }
1013}
1014
1015/*
1016 * register an upcall request to user-space.
1017 * Each request is at most one page long.
1018 */
1019static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
1020{
1021
1022 char *buf;
1023 struct cache_request *crq;
1024 char *bp;
1025 int len;
1026
1027 if (detail->cache_request == NULL)
1028 return -EINVAL;
1029
1030 if (atomic_read(&detail->readers) == 0 &&
1031 detail->last_close < get_seconds() - 30) {
1032 warn_no_listener(detail);
1033 return -EINVAL;
1034 }
1035
1036 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1037 if (!buf)
1038 return -EAGAIN;
1039
1040 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1041 if (!crq) {
1042 kfree(buf);
1043 return -EAGAIN;
1044 }
1045
1046 bp = buf; len = PAGE_SIZE;
1047
1048 detail->cache_request(detail, h, &bp, &len);
1049
1050 if (len < 0) {
1051 kfree(buf);
1052 kfree(crq);
1053 return -EAGAIN;
1054 }
1055 crq->q.reader = 0;
1056 crq->item = cache_get(h);
1057 crq->buf = buf;
1058 crq->len = PAGE_SIZE - len;
1059 crq->readers = 0;
1060 spin_lock(&queue_lock);
1061 list_add_tail(&crq->q.list, &detail->queue);
1062 spin_unlock(&queue_lock);
1063 wake_up(&queue_wait);
1064 return 0;
1065}
1066
1067/*
1068 * parse a message from user-space and pass it
1069 * to an appropriate cache
1070 * Messages are, like requests, separated into fields by
1071 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1072 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001073 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * reply cachename expiry key ... content....
1075 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001076 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
1078
1079#define isodigit(c) (isdigit(c) && c <= '7')
1080int qword_get(char **bpp, char *dest, int bufsize)
1081{
1082 /* return bytes copied, or -1 on error */
1083 char *bp = *bpp;
1084 int len = 0;
1085
1086 while (*bp == ' ') bp++;
1087
1088 if (bp[0] == '\\' && bp[1] == 'x') {
1089 /* HEX STRING */
1090 bp += 2;
1091 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1092 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1093 bp++;
1094 byte <<= 4;
1095 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1096 *dest++ = byte;
1097 bp++;
1098 len++;
1099 }
1100 } else {
1101 /* text with \nnn octal quoting */
1102 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1103 if (*bp == '\\' &&
1104 isodigit(bp[1]) && (bp[1] <= '3') &&
1105 isodigit(bp[2]) &&
1106 isodigit(bp[3])) {
1107 int byte = (*++bp -'0');
1108 bp++;
1109 byte = (byte << 3) | (*bp++ - '0');
1110 byte = (byte << 3) | (*bp++ - '0');
1111 *dest++ = byte;
1112 len++;
1113 } else {
1114 *dest++ = *bp++;
1115 len++;
1116 }
1117 }
1118 }
1119
1120 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1121 return -1;
1122 while (*bp == ' ') bp++;
1123 *bpp = bp;
1124 *dest = '\0';
1125 return len;
1126}
1127
1128
1129/*
1130 * support /proc/sunrpc/cache/$CACHENAME/content
1131 * as a seqfile.
1132 * We call ->cache_show passing NULL for the item to
1133 * get a header, then pass each real item in the cache
1134 */
1135
1136struct handle {
1137 struct cache_detail *cd;
1138};
1139
1140static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001141 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 loff_t n = *pos;
1144 unsigned hash, entry;
1145 struct cache_head *ch;
1146 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 read_lock(&cd->hash_lock);
1150 if (!n--)
1151 return SEQ_START_TOKEN;
1152 hash = n >> 32;
1153 entry = n & ((1LL<<32) - 1);
1154
1155 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1156 if (!entry--)
1157 return ch;
1158 n &= ~((1LL<<32) - 1);
1159 do {
1160 hash++;
1161 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001162 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 cd->hash_table[hash]==NULL);
1164 if (hash >= cd->hash_size)
1165 return NULL;
1166 *pos = n+1;
1167 return cd->hash_table[hash];
1168}
1169
1170static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1171{
1172 struct cache_head *ch = p;
1173 int hash = (*pos >> 32);
1174 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1175
1176 if (p == SEQ_START_TOKEN)
1177 hash = 0;
1178 else if (ch->next == NULL) {
1179 hash++;
1180 *pos += 1LL<<32;
1181 } else {
1182 ++*pos;
1183 return ch->next;
1184 }
1185 *pos &= ~((1LL<<32) - 1);
1186 while (hash < cd->hash_size &&
1187 cd->hash_table[hash] == NULL) {
1188 hash++;
1189 *pos += 1LL<<32;
1190 }
1191 if (hash >= cd->hash_size)
1192 return NULL;
1193 ++*pos;
1194 return cd->hash_table[hash];
1195}
1196
1197static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001198 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1201 read_unlock(&cd->hash_lock);
1202}
1203
1204static int c_show(struct seq_file *m, void *p)
1205{
1206 struct cache_head *cp = p;
1207 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1208
1209 if (p == SEQ_START_TOKEN)
1210 return cd->cache_show(m, cd, NULL);
1211
1212 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001213 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownbaab9352006-03-27 01:15:09 -08001214 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 cache_get(cp);
1216 if (cache_check(cd, cp, NULL))
1217 /* cache_check does a cache_put on failure */
1218 seq_printf(m, "# ");
1219 else
1220 cache_put(cp, cd);
1221
1222 return cd->cache_show(m, cd, cp);
1223}
1224
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001225static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 .start = c_start,
1227 .next = c_next,
1228 .stop = c_stop,
1229 .show = c_show,
1230};
1231
1232static int content_open(struct inode *inode, struct file *file)
1233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct handle *han;
1235 struct cache_detail *cd = PDE(inode)->data;
1236
Pavel Emelyanovec931032007-10-10 02:31:07 -07001237 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (han == NULL)
1239 return -ENOMEM;
1240
1241 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Arjan van de Venda7071d2007-02-12 00:55:36 -08001245static const struct file_operations content_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 .open = content_open,
1247 .read = seq_read,
1248 .llseek = seq_lseek,
Martin Peschke14690fc2007-04-26 01:03:43 -07001249 .release = seq_release_private,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250};
1251
1252static ssize_t read_flush(struct file *file, char __user *buf,
1253 size_t count, loff_t *ppos)
1254{
Josef Sipek303b46b2006-12-08 02:37:42 -08001255 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 char tbuf[20];
1257 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001258 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 sprintf(tbuf, "%lu\n", cd->flush_time);
1261 len = strlen(tbuf);
1262 if (p >= len)
1263 return 0;
1264 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001265 if (len > count)
1266 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001268 return -EFAULT;
1269 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return len;
1271}
1272
1273static ssize_t write_flush(struct file * file, const char __user * buf,
1274 size_t count, loff_t *ppos)
1275{
Josef Sipek303b46b2006-12-08 02:37:42 -08001276 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 char tbuf[20];
1278 char *ep;
1279 long flushtime;
1280 if (*ppos || count > sizeof(tbuf)-1)
1281 return -EINVAL;
1282 if (copy_from_user(tbuf, buf, count))
1283 return -EFAULT;
1284 tbuf[count] = 0;
1285 flushtime = simple_strtoul(tbuf, &ep, 0);
1286 if (*ep && *ep != '\n')
1287 return -EINVAL;
1288
1289 cd->flush_time = flushtime;
1290 cd->nextcheck = get_seconds();
1291 cache_flush();
1292
1293 *ppos += count;
1294 return count;
1295}
1296
Arjan van de Venda7071d2007-02-12 00:55:36 -08001297static const struct file_operations cache_flush_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 .open = nonseekable_open,
1299 .read = read_flush,
1300 .write = write_flush,
1301};