blob: d75530ff2a6d90aaca6f9a4f39cada3e56b89fc7 [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}
Trond Myklebustd2f7e792007-07-14 15:39:58 -0400248EXPORT_SYMBOL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/*
251 * caches need to be periodically cleaned.
252 * For this we maintain a list of cache_detail and
253 * a current pointer into that list and into the table
254 * for that entry.
255 *
256 * Each time clean_cache is called it finds the next non-empty entry
257 * in the current table and walks the list in that entry
258 * looking for entries that can be removed.
259 *
260 * An entry gets removed if:
261 * - The expiry is before current time
262 * - The last_refresh time is before the flush_time for that cache
263 *
264 * later we might drop old entries with non-NEVER expiry if that table
265 * is getting 'full' for some definition of 'full'
266 *
267 * The question of "how often to scan a table" is an interesting one
268 * and is answered in part by the use of the "nextcheck" field in the
269 * cache_detail.
270 * When a scan of a table begins, the nextcheck field is set to a time
271 * that is well into the future.
272 * While scanning, if an expiry time is found that is earlier than the
273 * current nextcheck time, nextcheck is set to that expiry time.
274 * If the flush_time is ever set to a time earlier than the nextcheck
275 * time, the nextcheck time is then set to that flush_time.
276 *
277 * A table is then only scanned if the current time is at least
278 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800279 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
281
282static LIST_HEAD(cache_list);
283static DEFINE_SPINLOCK(cache_list_lock);
284static struct cache_detail *current_detail;
285static int current_index;
286
Arjan van de Venda7071d2007-02-12 00:55:36 -0800287static const struct file_operations cache_file_operations;
288static const struct file_operations content_file_operations;
289static const struct file_operations cache_flush_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
David Howells65f27f32006-11-22 14:55:48 +0000291static void do_cache_clean(struct work_struct *work);
292static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500294static void remove_cache_proc_entries(struct cache_detail *cd)
295{
296 if (cd->proc_ent == NULL)
297 return;
298 if (cd->flush_ent)
299 remove_proc_entry("flush", cd->proc_ent);
300 if (cd->channel_ent)
301 remove_proc_entry("channel", cd->proc_ent);
302 if (cd->content_ent)
303 remove_proc_entry("content", cd->proc_ent);
304 cd->proc_ent = NULL;
305 remove_proc_entry(cd->name, proc_net_rpc);
306}
307
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500308#ifdef CONFIG_PROC_FS
309static int create_cache_proc_entries(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500310{
311 struct proc_dir_entry *p;
312
313 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
314 if (cd->proc_ent == NULL)
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500315 goto out_nomem;
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500316 cd->proc_ent->owner = cd->owner;
317 cd->channel_ent = cd->content_ent = NULL;
318
Wang Chen2ce8f042008-02-28 14:00:59 -0800319 p = proc_create("flush", S_IFREG|S_IRUSR|S_IWUSR,
320 cd->proc_ent, &cache_flush_operations);
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500321 cd->flush_ent = p;
322 if (p == NULL)
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500323 goto out_nomem;
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500324 p->owner = cd->owner;
325 p->data = cd;
326
327 if (cd->cache_request || cd->cache_parse) {
Wang Chen2ce8f042008-02-28 14:00:59 -0800328 p = proc_create("channel", S_IFREG|S_IRUSR|S_IWUSR,
329 cd->proc_ent, &cache_file_operations);
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500330 cd->channel_ent = p;
331 if (p == NULL)
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500332 goto out_nomem;
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500333 p->owner = cd->owner;
334 p->data = cd;
335 }
336 if (cd->cache_show) {
Wang Chen2ce8f042008-02-28 14:00:59 -0800337 p = proc_create("content", S_IFREG|S_IRUSR|S_IWUSR,
338 cd->proc_ent, &content_file_operations);
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500339 cd->content_ent = p;
340 if (p == NULL)
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500341 goto out_nomem;
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500342 p->owner = cd->owner;
343 p->data = cd;
344 }
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500345 return 0;
346out_nomem:
347 remove_cache_proc_entries(cd);
348 return -ENOMEM;
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500349}
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500350#else /* CONFIG_PROC_FS */
351static int create_cache_proc_entries(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500353 return 0;
354}
355#endif
356
357int cache_register(struct cache_detail *cd)
358{
359 int ret;
360
361 ret = create_cache_proc_entries(cd);
362 if (ret)
363 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 rwlock_init(&cd->hash_lock);
365 INIT_LIST_HEAD(&cd->queue);
366 spin_lock(&cache_list_lock);
367 cd->nextcheck = 0;
368 cd->entries = 0;
369 atomic_set(&cd->readers, 0);
370 cd->last_close = 0;
371 cd->last_warn = -1;
372 list_add(&cd->others, &cache_list);
373 spin_unlock(&cache_list_lock);
374
375 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000376 schedule_delayed_work(&cache_cleaner, 0);
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
Trond Myklebustd2f7e792007-07-14 15:39:58 -0400379EXPORT_SYMBOL(cache_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500381void cache_unregister(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 cache_purge(cd);
384 spin_lock(&cache_list_lock);
385 write_lock(&cd->hash_lock);
386 if (cd->entries || atomic_read(&cd->inuse)) {
387 write_unlock(&cd->hash_lock);
388 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500389 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 if (current_detail == cd)
392 current_detail = NULL;
393 list_del_init(&cd->others);
394 write_unlock(&cd->hash_lock);
395 spin_unlock(&cache_list_lock);
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500396 remove_cache_proc_entries(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (list_empty(&cache_list)) {
398 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400399 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500401 return;
402out:
403 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
Trond Myklebustd2f7e792007-07-14 15:39:58 -0400405EXPORT_SYMBOL(cache_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407/* clean cache tries to find something to clean
408 * and cleans it.
409 * It returns 1 if it cleaned something,
410 * 0 if it didn't find anything this time
411 * -1 if it fell off the end of the list.
412 */
413static int cache_clean(void)
414{
415 int rv = 0;
416 struct list_head *next;
417
418 spin_lock(&cache_list_lock);
419
420 /* find a suitable table if we don't already have one */
421 while (current_detail == NULL ||
422 current_index >= current_detail->hash_size) {
423 if (current_detail)
424 next = current_detail->others.next;
425 else
426 next = cache_list.next;
427 if (next == &cache_list) {
428 current_detail = NULL;
429 spin_unlock(&cache_list_lock);
430 return -1;
431 }
432 current_detail = list_entry(next, struct cache_detail, others);
433 if (current_detail->nextcheck > get_seconds())
434 current_index = current_detail->hash_size;
435 else {
436 current_index = 0;
437 current_detail->nextcheck = get_seconds()+30*60;
438 }
439 }
440
441 /* find a non-empty bucket in the table */
442 while (current_detail &&
443 current_index < current_detail->hash_size &&
444 current_detail->hash_table[current_index] == NULL)
445 current_index++;
446
447 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (current_detail && current_index < current_detail->hash_size) {
450 struct cache_head *ch, **cp;
451 struct cache_detail *d;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 write_lock(&current_detail->hash_lock);
454
455 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 cp = & current_detail->hash_table[current_index];
458 ch = *cp;
459 for (; ch; cp= & ch->next, ch= *cp) {
460 if (current_detail->nextcheck > ch->expiry_time)
461 current_detail->nextcheck = ch->expiry_time+1;
462 if (ch->expiry_time >= get_seconds()
463 && ch->last_refresh >= current_detail->flush_time
464 )
465 continue;
466 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
467 queue_loose(current_detail, ch);
468
NeilBrownbaab9352006-03-27 01:15:09 -0800469 if (atomic_read(&ch->ref.refcount) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471 }
472 if (ch) {
473 *cp = ch->next;
474 ch->next = NULL;
475 current_detail->entries--;
476 rv = 1;
477 }
478 write_unlock(&current_detail->hash_lock);
479 d = current_detail;
480 if (!ch)
481 current_index ++;
482 spin_unlock(&cache_list_lock);
483 if (ch)
NeilBrownbaab9352006-03-27 01:15:09 -0800484 cache_put(ch, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } else
486 spin_unlock(&cache_list_lock);
487
488 return rv;
489}
490
491/*
492 * We want to regularly clean the cache, so we need to schedule some work ...
493 */
David Howells65f27f32006-11-22 14:55:48 +0000494static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 int delay = 5;
497 if (cache_clean() == -1)
498 delay = 30*HZ;
499
500 if (list_empty(&cache_list))
501 delay = 0;
502
503 if (delay)
504 schedule_delayed_work(&cache_cleaner, delay);
505}
506
507
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800508/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800510 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * be fully cleaned
512 */
513void cache_flush(void)
514{
515 while (cache_clean() != -1)
516 cond_resched();
517 while (cache_clean() != -1)
518 cond_resched();
519}
Trond Myklebustd2f7e792007-07-14 15:39:58 -0400520EXPORT_SYMBOL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522void cache_purge(struct cache_detail *detail)
523{
524 detail->flush_time = LONG_MAX;
525 detail->nextcheck = get_seconds();
526 cache_flush();
527 detail->flush_time = 1;
528}
Trond Myklebustd2f7e792007-07-14 15:39:58 -0400529EXPORT_SYMBOL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531
532/*
533 * Deferral and Revisiting of Requests.
534 *
535 * If a cache lookup finds a pending entry, we
536 * need to defer the request and revisit it later.
537 * All deferred requests are stored in a hash table,
538 * indexed by "struct cache_head *".
539 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800540 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 * deferred form, which must contain a
542 * 'struct cache_deferred_req'
543 * This cache_deferred_req contains a method to allow
544 * it to be revisited when cache info is available
545 */
546
547#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
548#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
549
550#define DFR_MAX 300 /* ??? */
551
552static DEFINE_SPINLOCK(cache_defer_lock);
553static LIST_HEAD(cache_defer_list);
554static struct list_head cache_defer_hash[DFR_HASHSIZE];
555static int cache_defer_cnt;
556
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800557static int cache_defer_req(struct cache_req *req, struct cache_head *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct cache_deferred_req *dreq;
560 int hash = DFR_HASH(item);
561
J.Bruce Fields01f3bd12006-12-13 00:35:26 -0800562 if (cache_defer_cnt >= DFR_MAX) {
563 /* too much in the cache, randomly drop this one,
564 * or continue and drop the oldest below
565 */
566 if (net_random()&1)
567 return -ETIMEDOUT;
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 dreq = req->defer(req);
570 if (dreq == NULL)
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800571 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 spin_lock(&cache_defer_lock);
576
577 list_add(&dreq->recent, &cache_defer_list);
578
579 if (cache_defer_hash[hash].next == NULL)
580 INIT_LIST_HEAD(&cache_defer_hash[hash]);
581 list_add(&dreq->hash, &cache_defer_hash[hash]);
582
583 /* it is in, now maybe clean up */
584 dreq = NULL;
585 if (++cache_defer_cnt > DFR_MAX) {
J.Bruce Fields01f3bd12006-12-13 00:35:26 -0800586 dreq = list_entry(cache_defer_list.prev,
587 struct cache_deferred_req, recent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 list_del(&dreq->recent);
589 list_del(&dreq->hash);
590 cache_defer_cnt--;
591 }
592 spin_unlock(&cache_defer_lock);
593
594 if (dreq) {
595 /* there was one too many */
596 dreq->revisit(dreq, 1);
597 }
NeilBrown4013ede2006-03-27 01:15:07 -0800598 if (!test_bit(CACHE_PENDING, &item->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* must have just been validated... */
600 cache_revisit_request(item);
601 }
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605static void cache_revisit_request(struct cache_head *item)
606{
607 struct cache_deferred_req *dreq;
608 struct list_head pending;
609
610 struct list_head *lp;
611 int hash = DFR_HASH(item);
612
613 INIT_LIST_HEAD(&pending);
614 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 lp = cache_defer_hash[hash].next;
617 if (lp) {
618 while (lp != &cache_defer_hash[hash]) {
619 dreq = list_entry(lp, struct cache_deferred_req, hash);
620 lp = lp->next;
621 if (dreq->item == item) {
622 list_del(&dreq->hash);
623 list_move(&dreq->recent, &pending);
624 cache_defer_cnt--;
625 }
626 }
627 }
628 spin_unlock(&cache_defer_lock);
629
630 while (!list_empty(&pending)) {
631 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
632 list_del_init(&dreq->recent);
633 dreq->revisit(dreq, 0);
634 }
635}
636
637void cache_clean_deferred(void *owner)
638{
639 struct cache_deferred_req *dreq, *tmp;
640 struct list_head pending;
641
642
643 INIT_LIST_HEAD(&pending);
644 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
647 if (dreq->owner == owner) {
648 list_del(&dreq->hash);
649 list_move(&dreq->recent, &pending);
650 cache_defer_cnt--;
651 }
652 }
653 spin_unlock(&cache_defer_lock);
654
655 while (!list_empty(&pending)) {
656 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
657 list_del_init(&dreq->recent);
658 dreq->revisit(dreq, 1);
659 }
660}
661
662/*
663 * communicate with user-space
664 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500665 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
666 * On read, you get a full request, or block.
667 * On write, an update request is processed.
668 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800670 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500671 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * to the end and may wakeup and preceding readers.
673 * New readers are added to the head. If, on read, an item is found with
674 * CACHE_UPCALLING clear, we free it from the list.
675 *
676 */
677
678static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800679static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681struct cache_queue {
682 struct list_head list;
683 int reader; /* if 0, then request */
684};
685struct cache_request {
686 struct cache_queue q;
687 struct cache_head *item;
688 char * buf;
689 int len;
690 int readers;
691};
692struct cache_reader {
693 struct cache_queue q;
694 int offset; /* if non-0, we have a refcnt on next request */
695};
696
697static ssize_t
698cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
699{
700 struct cache_reader *rp = filp->private_data;
701 struct cache_request *rq;
Josef Sipek303b46b2006-12-08 02:37:42 -0800702 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int err;
704
705 if (count == 0)
706 return 0;
707
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800708 mutex_lock(&queue_io_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * readers on this file */
710 again:
711 spin_lock(&queue_lock);
712 /* need to find next request */
713 while (rp->q.list.next != &cd->queue &&
714 list_entry(rp->q.list.next, struct cache_queue, list)
715 ->reader) {
716 struct list_head *next = rp->q.list.next;
717 list_move(&rp->q.list, next);
718 }
719 if (rp->q.list.next == &cd->queue) {
720 spin_unlock(&queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800721 mutex_unlock(&queue_io_mutex);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800722 BUG_ON(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return 0;
724 }
725 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800726 BUG_ON(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (rp->offset == 0)
728 rq->readers++;
729 spin_unlock(&queue_lock);
730
731 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
732 err = -EAGAIN;
733 spin_lock(&queue_lock);
734 list_move(&rp->q.list, &rq->q.list);
735 spin_unlock(&queue_lock);
736 } else {
737 if (rp->offset + count > rq->len)
738 count = rq->len - rp->offset;
739 err = -EFAULT;
740 if (copy_to_user(buf, rq->buf + rp->offset, count))
741 goto out;
742 rp->offset += count;
743 if (rp->offset >= rq->len) {
744 rp->offset = 0;
745 spin_lock(&queue_lock);
746 list_move(&rp->q.list, &rq->q.list);
747 spin_unlock(&queue_lock);
748 }
749 err = 0;
750 }
751 out:
752 if (rp->offset == 0) {
753 /* need to release rq */
754 spin_lock(&queue_lock);
755 rq->readers--;
756 if (rq->readers == 0 &&
757 !test_bit(CACHE_PENDING, &rq->item->flags)) {
758 list_del(&rq->q.list);
759 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800760 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 kfree(rq->buf);
762 kfree(rq);
763 } else
764 spin_unlock(&queue_lock);
765 }
766 if (err == -EAGAIN)
767 goto again;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800768 mutex_unlock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return err ? err : count;
770}
771
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800772static char write_buf[8192]; /* protected by queue_io_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774static ssize_t
775cache_write(struct file *filp, const char __user *buf, size_t count,
776 loff_t *ppos)
777{
778 int err;
Josef Sipek303b46b2006-12-08 02:37:42 -0800779 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (count == 0)
782 return 0;
783 if (count >= sizeof(write_buf))
784 return -EINVAL;
785
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800786 mutex_lock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (copy_from_user(write_buf, buf, count)) {
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800789 mutex_unlock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return -EFAULT;
791 }
792 write_buf[count] = '\0';
793 if (cd->cache_parse)
794 err = cd->cache_parse(cd, write_buf, count);
795 else
796 err = -EINVAL;
797
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800798 mutex_unlock(&queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return err ? err : count;
800}
801
802static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
803
804static unsigned int
805cache_poll(struct file *filp, poll_table *wait)
806{
807 unsigned int mask;
808 struct cache_reader *rp = filp->private_data;
809 struct cache_queue *cq;
Josef Sipek303b46b2006-12-08 02:37:42 -0800810 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 poll_wait(filp, &queue_wait, wait);
813
814 /* alway allow write */
815 mask = POLL_OUT | POLLWRNORM;
816
817 if (!rp)
818 return mask;
819
820 spin_lock(&queue_lock);
821
822 for (cq= &rp->q; &cq->list != &cd->queue;
823 cq = list_entry(cq->list.next, struct cache_queue, list))
824 if (!cq->reader) {
825 mask |= POLLIN | POLLRDNORM;
826 break;
827 }
828 spin_unlock(&queue_lock);
829 return mask;
830}
831
832static int
833cache_ioctl(struct inode *ino, struct file *filp,
834 unsigned int cmd, unsigned long arg)
835{
836 int len = 0;
837 struct cache_reader *rp = filp->private_data;
838 struct cache_queue *cq;
839 struct cache_detail *cd = PDE(ino)->data;
840
841 if (cmd != FIONREAD || !rp)
842 return -EINVAL;
843
844 spin_lock(&queue_lock);
845
846 /* only find the length remaining in current request,
847 * or the length of the next request
848 */
849 for (cq= &rp->q; &cq->list != &cd->queue;
850 cq = list_entry(cq->list.next, struct cache_queue, list))
851 if (!cq->reader) {
852 struct cache_request *cr =
853 container_of(cq, struct cache_request, q);
854 len = cr->len - rp->offset;
855 break;
856 }
857 spin_unlock(&queue_lock);
858
859 return put_user(len, (int __user *)arg);
860}
861
862static int
863cache_open(struct inode *inode, struct file *filp)
864{
865 struct cache_reader *rp = NULL;
866
867 nonseekable_open(inode, filp);
868 if (filp->f_mode & FMODE_READ) {
869 struct cache_detail *cd = PDE(inode)->data;
870
871 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
872 if (!rp)
873 return -ENOMEM;
874 rp->offset = 0;
875 rp->q.reader = 1;
876 atomic_inc(&cd->readers);
877 spin_lock(&queue_lock);
878 list_add(&rp->q.list, &cd->queue);
879 spin_unlock(&queue_lock);
880 }
881 filp->private_data = rp;
882 return 0;
883}
884
885static int
886cache_release(struct inode *inode, struct file *filp)
887{
888 struct cache_reader *rp = filp->private_data;
889 struct cache_detail *cd = PDE(inode)->data;
890
891 if (rp) {
892 spin_lock(&queue_lock);
893 if (rp->offset) {
894 struct cache_queue *cq;
895 for (cq= &rp->q; &cq->list != &cd->queue;
896 cq = list_entry(cq->list.next, struct cache_queue, list))
897 if (!cq->reader) {
898 container_of(cq, struct cache_request, q)
899 ->readers--;
900 break;
901 }
902 rp->offset = 0;
903 }
904 list_del(&rp->q.list);
905 spin_unlock(&queue_lock);
906
907 filp->private_data = NULL;
908 kfree(rp);
909
910 cd->last_close = get_seconds();
911 atomic_dec(&cd->readers);
912 }
913 return 0;
914}
915
916
917
Arjan van de Venda7071d2007-02-12 00:55:36 -0800918static const struct file_operations cache_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 .owner = THIS_MODULE,
920 .llseek = no_llseek,
921 .read = cache_read,
922 .write = cache_write,
923 .poll = cache_poll,
924 .ioctl = cache_ioctl, /* for FIONREAD */
925 .open = cache_open,
926 .release = cache_release,
927};
928
929
930static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
931{
932 struct cache_queue *cq;
933 spin_lock(&queue_lock);
934 list_for_each_entry(cq, &detail->queue, list)
935 if (!cq->reader) {
936 struct cache_request *cr = container_of(cq, struct cache_request, q);
937 if (cr->item != ch)
938 continue;
939 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -0800940 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 list_del(&cr->q.list);
942 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800943 cache_put(cr->item, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 kfree(cr->buf);
945 kfree(cr);
946 return;
947 }
948 spin_unlock(&queue_lock);
949}
950
951/*
952 * Support routines for text-based upcalls.
953 * Fields are separated by spaces.
954 * Fields are either mangled to quote space tab newline slosh with slosh
955 * or a hexified with a leading \x
956 * Record is terminated with newline.
957 *
958 */
959
960void qword_add(char **bpp, int *lp, char *str)
961{
962 char *bp = *bpp;
963 int len = *lp;
964 char c;
965
966 if (len < 0) return;
967
968 while ((c=*str++) && len)
969 switch(c) {
970 case ' ':
971 case '\t':
972 case '\n':
973 case '\\':
974 if (len >= 4) {
975 *bp++ = '\\';
976 *bp++ = '0' + ((c & 0300)>>6);
977 *bp++ = '0' + ((c & 0070)>>3);
978 *bp++ = '0' + ((c & 0007)>>0);
979 }
980 len -= 4;
981 break;
982 default:
983 *bp++ = c;
984 len--;
985 }
986 if (c || len <1) len = -1;
987 else {
988 *bp++ = ' ';
989 len--;
990 }
991 *bpp = bp;
992 *lp = len;
993}
Trond Myklebustd2f7e792007-07-14 15:39:58 -0400994EXPORT_SYMBOL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996void qword_addhex(char **bpp, int *lp, char *buf, int blen)
997{
998 char *bp = *bpp;
999 int len = *lp;
1000
1001 if (len < 0) return;
1002
1003 if (len > 2) {
1004 *bp++ = '\\';
1005 *bp++ = 'x';
1006 len -= 2;
1007 while (blen && len >= 2) {
1008 unsigned char c = *buf++;
1009 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1010 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1011 len -= 2;
1012 blen--;
1013 }
1014 }
1015 if (blen || len<1) len = -1;
1016 else {
1017 *bp++ = ' ';
1018 len--;
1019 }
1020 *bpp = bp;
1021 *lp = len;
1022}
Trond Myklebustd2f7e792007-07-14 15:39:58 -04001023EXPORT_SYMBOL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025static void warn_no_listener(struct cache_detail *detail)
1026{
1027 if (detail->last_warn != detail->last_close) {
1028 detail->last_warn = detail->last_close;
1029 if (detail->warn_no_listener)
1030 detail->warn_no_listener(detail);
1031 }
1032}
1033
1034/*
1035 * register an upcall request to user-space.
1036 * Each request is at most one page long.
1037 */
1038static int cache_make_upcall(struct cache_detail *detail, struct cache_head *h)
1039{
1040
1041 char *buf;
1042 struct cache_request *crq;
1043 char *bp;
1044 int len;
1045
1046 if (detail->cache_request == NULL)
1047 return -EINVAL;
1048
1049 if (atomic_read(&detail->readers) == 0 &&
1050 detail->last_close < get_seconds() - 30) {
1051 warn_no_listener(detail);
1052 return -EINVAL;
1053 }
1054
1055 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1056 if (!buf)
1057 return -EAGAIN;
1058
1059 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1060 if (!crq) {
1061 kfree(buf);
1062 return -EAGAIN;
1063 }
1064
1065 bp = buf; len = PAGE_SIZE;
1066
1067 detail->cache_request(detail, h, &bp, &len);
1068
1069 if (len < 0) {
1070 kfree(buf);
1071 kfree(crq);
1072 return -EAGAIN;
1073 }
1074 crq->q.reader = 0;
1075 crq->item = cache_get(h);
1076 crq->buf = buf;
1077 crq->len = PAGE_SIZE - len;
1078 crq->readers = 0;
1079 spin_lock(&queue_lock);
1080 list_add_tail(&crq->q.list, &detail->queue);
1081 spin_unlock(&queue_lock);
1082 wake_up(&queue_wait);
1083 return 0;
1084}
1085
1086/*
1087 * parse a message from user-space and pass it
1088 * to an appropriate cache
1089 * Messages are, like requests, separated into fields by
1090 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1091 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001092 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 * reply cachename expiry key ... content....
1094 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001095 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 */
1097
1098#define isodigit(c) (isdigit(c) && c <= '7')
1099int qword_get(char **bpp, char *dest, int bufsize)
1100{
1101 /* return bytes copied, or -1 on error */
1102 char *bp = *bpp;
1103 int len = 0;
1104
1105 while (*bp == ' ') bp++;
1106
1107 if (bp[0] == '\\' && bp[1] == 'x') {
1108 /* HEX STRING */
1109 bp += 2;
1110 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1111 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1112 bp++;
1113 byte <<= 4;
1114 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1115 *dest++ = byte;
1116 bp++;
1117 len++;
1118 }
1119 } else {
1120 /* text with \nnn octal quoting */
1121 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1122 if (*bp == '\\' &&
1123 isodigit(bp[1]) && (bp[1] <= '3') &&
1124 isodigit(bp[2]) &&
1125 isodigit(bp[3])) {
1126 int byte = (*++bp -'0');
1127 bp++;
1128 byte = (byte << 3) | (*bp++ - '0');
1129 byte = (byte << 3) | (*bp++ - '0');
1130 *dest++ = byte;
1131 len++;
1132 } else {
1133 *dest++ = *bp++;
1134 len++;
1135 }
1136 }
1137 }
1138
1139 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1140 return -1;
1141 while (*bp == ' ') bp++;
1142 *bpp = bp;
1143 *dest = '\0';
1144 return len;
1145}
Trond Myklebustd2f7e792007-07-14 15:39:58 -04001146EXPORT_SYMBOL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148
1149/*
1150 * support /proc/sunrpc/cache/$CACHENAME/content
1151 * as a seqfile.
1152 * We call ->cache_show passing NULL for the item to
1153 * get a header, then pass each real item in the cache
1154 */
1155
1156struct handle {
1157 struct cache_detail *cd;
1158};
1159
1160static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001161 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 loff_t n = *pos;
1164 unsigned hash, entry;
1165 struct cache_head *ch;
1166 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 read_lock(&cd->hash_lock);
1170 if (!n--)
1171 return SEQ_START_TOKEN;
1172 hash = n >> 32;
1173 entry = n & ((1LL<<32) - 1);
1174
1175 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1176 if (!entry--)
1177 return ch;
1178 n &= ~((1LL<<32) - 1);
1179 do {
1180 hash++;
1181 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001182 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 cd->hash_table[hash]==NULL);
1184 if (hash >= cd->hash_size)
1185 return NULL;
1186 *pos = n+1;
1187 return cd->hash_table[hash];
1188}
1189
1190static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1191{
1192 struct cache_head *ch = p;
1193 int hash = (*pos >> 32);
1194 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1195
1196 if (p == SEQ_START_TOKEN)
1197 hash = 0;
1198 else if (ch->next == NULL) {
1199 hash++;
1200 *pos += 1LL<<32;
1201 } else {
1202 ++*pos;
1203 return ch->next;
1204 }
1205 *pos &= ~((1LL<<32) - 1);
1206 while (hash < cd->hash_size &&
1207 cd->hash_table[hash] == NULL) {
1208 hash++;
1209 *pos += 1LL<<32;
1210 }
1211 if (hash >= cd->hash_size)
1212 return NULL;
1213 ++*pos;
1214 return cd->hash_table[hash];
1215}
1216
1217static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001218 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1221 read_unlock(&cd->hash_lock);
1222}
1223
1224static int c_show(struct seq_file *m, void *p)
1225{
1226 struct cache_head *cp = p;
1227 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1228
1229 if (p == SEQ_START_TOKEN)
1230 return cd->cache_show(m, cd, NULL);
1231
1232 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001233 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownbaab9352006-03-27 01:15:09 -08001234 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 cache_get(cp);
1236 if (cache_check(cd, cp, NULL))
1237 /* cache_check does a cache_put on failure */
1238 seq_printf(m, "# ");
1239 else
1240 cache_put(cp, cd);
1241
1242 return cd->cache_show(m, cd, cp);
1243}
1244
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001245static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 .start = c_start,
1247 .next = c_next,
1248 .stop = c_stop,
1249 .show = c_show,
1250};
1251
1252static int content_open(struct inode *inode, struct file *file)
1253{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 struct handle *han;
1255 struct cache_detail *cd = PDE(inode)->data;
1256
Pavel Emelyanovec931032007-10-10 02:31:07 -07001257 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (han == NULL)
1259 return -ENOMEM;
1260
1261 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Arjan van de Venda7071d2007-02-12 00:55:36 -08001265static const struct file_operations content_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 .open = content_open,
1267 .read = seq_read,
1268 .llseek = seq_lseek,
Martin Peschke14690fc2007-04-26 01:03:43 -07001269 .release = seq_release_private,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270};
1271
1272static ssize_t read_flush(struct file *file, char __user *buf,
1273 size_t count, loff_t *ppos)
1274{
Josef Sipek303b46b2006-12-08 02:37:42 -08001275 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 char tbuf[20];
1277 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001278 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 sprintf(tbuf, "%lu\n", cd->flush_time);
1281 len = strlen(tbuf);
1282 if (p >= len)
1283 return 0;
1284 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001285 if (len > count)
1286 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001288 return -EFAULT;
1289 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return len;
1291}
1292
1293static ssize_t write_flush(struct file * file, const char __user * buf,
1294 size_t count, loff_t *ppos)
1295{
Josef Sipek303b46b2006-12-08 02:37:42 -08001296 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 char tbuf[20];
1298 char *ep;
1299 long flushtime;
1300 if (*ppos || count > sizeof(tbuf)-1)
1301 return -EINVAL;
1302 if (copy_from_user(tbuf, buf, count))
1303 return -EFAULT;
1304 tbuf[count] = 0;
1305 flushtime = simple_strtoul(tbuf, &ep, 0);
1306 if (*ep && *ep != '\n')
1307 return -EINVAL;
1308
1309 cd->flush_time = flushtime;
1310 cd->nextcheck = get_seconds();
1311 cache_flush();
1312
1313 *ppos += count;
1314 return count;
1315}
1316
Arjan van de Venda7071d2007-02-12 00:55:36 -08001317static const struct file_operations cache_flush_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 .open = nonseekable_open,
1319 .read = read_flush,
1320 .write = write_flush,
1321};