blob: e438352bed7bd4b159abe524e2fcf081dea56124 [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>
Trond Myklebustda770052009-08-09 15:14:28 -040030#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/ioctls.h>
32#include <linux/sunrpc/types.h>
33#include <linux/sunrpc/cache.h>
34#include <linux/sunrpc/stats.h>
35
36#define RPCDBG_FACILITY RPCDBG_CACHE
37
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -080038static int cache_defer_req(struct cache_req *req, struct cache_head *item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static void cache_revisit_request(struct cache_head *item);
40
Adrian Bunk74cae612006-03-27 01:15:10 -080041static void cache_init(struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 time_t now = get_seconds();
44 h->next = NULL;
45 h->flags = 0;
NeilBrownbaab9352006-03-27 01:15:09 -080046 kref_init(&h->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 h->expiry_time = now + CACHE_NEW_EXPIRY;
48 h->last_refresh = now;
49}
50
NeilBrown15a5f6b2006-03-27 01:15:02 -080051struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
52 struct cache_head *key, int hash)
53{
54 struct cache_head **head, **hp;
55 struct cache_head *new = NULL;
56
57 head = &detail->hash_table[hash];
58
59 read_lock(&detail->hash_lock);
60
61 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
62 struct cache_head *tmp = *hp;
63 if (detail->match(tmp, key)) {
64 cache_get(tmp);
65 read_unlock(&detail->hash_lock);
66 return tmp;
67 }
68 }
69 read_unlock(&detail->hash_lock);
70 /* Didn't find anything, insert an empty entry */
71
72 new = detail->alloc();
73 if (!new)
74 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070075 /* must fully initialise 'new', else
76 * we might get lose if we need to
77 * cache_put it soon.
78 */
NeilBrown15a5f6b2006-03-27 01:15:02 -080079 cache_init(new);
Neil Brown2f349312006-08-05 12:14:29 -070080 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080081
82 write_lock(&detail->hash_lock);
83
84 /* check if entry appeared while we slept */
85 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
86 struct cache_head *tmp = *hp;
87 if (detail->match(tmp, key)) {
88 cache_get(tmp);
89 write_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -080090 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -080091 return tmp;
92 }
93 }
NeilBrown15a5f6b2006-03-27 01:15:02 -080094 new->next = *head;
95 *head = new;
96 detail->entries++;
97 cache_get(new);
98 write_unlock(&detail->hash_lock);
99
100 return new;
101}
Trond Myklebust24c37672008-12-23 16:30:12 -0500102EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800103
NeilBrownebd0cb12006-03-27 01:15:08 -0800104
105static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
106
107static int cache_fresh_locked(struct cache_head *head, time_t expiry)
108{
109 head->expiry_time = expiry;
110 head->last_refresh = get_seconds();
111 return !test_and_set_bit(CACHE_VALID, &head->flags);
112}
113
114static void cache_fresh_unlocked(struct cache_head *head,
115 struct cache_detail *detail, int new)
116{
117 if (new)
118 cache_revisit_request(head);
119 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
120 cache_revisit_request(head);
121 queue_loose(detail, head);
122 }
123}
124
NeilBrown15a5f6b2006-03-27 01:15:02 -0800125struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
126 struct cache_head *new, struct cache_head *old, int hash)
127{
128 /* The 'old' entry is to be replaced by 'new'.
129 * If 'old' is not VALID, we update it directly,
130 * otherwise we need to replace it
131 */
132 struct cache_head **head;
133 struct cache_head *tmp;
NeilBrownebd0cb12006-03-27 01:15:08 -0800134 int is_new;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800135
136 if (!test_bit(CACHE_VALID, &old->flags)) {
137 write_lock(&detail->hash_lock);
138 if (!test_bit(CACHE_VALID, &old->flags)) {
139 if (test_bit(CACHE_NEGATIVE, &new->flags))
140 set_bit(CACHE_NEGATIVE, &old->flags);
141 else
142 detail->update(old, new);
NeilBrownebd0cb12006-03-27 01:15:08 -0800143 is_new = cache_fresh_locked(old, new->expiry_time);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800144 write_unlock(&detail->hash_lock);
NeilBrownebd0cb12006-03-27 01:15:08 -0800145 cache_fresh_unlocked(old, detail, is_new);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800146 return old;
147 }
148 write_unlock(&detail->hash_lock);
149 }
150 /* We need to insert a new entry */
151 tmp = detail->alloc();
152 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800153 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800154 return NULL;
155 }
156 cache_init(tmp);
157 detail->init(tmp, old);
158 head = &detail->hash_table[hash];
159
160 write_lock(&detail->hash_lock);
161 if (test_bit(CACHE_NEGATIVE, &new->flags))
162 set_bit(CACHE_NEGATIVE, &tmp->flags);
163 else
164 detail->update(tmp, new);
165 tmp->next = *head;
166 *head = tmp;
NeilBrownf2d39582006-05-22 22:35:25 -0700167 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800168 cache_get(tmp);
NeilBrownebd0cb12006-03-27 01:15:08 -0800169 is_new = cache_fresh_locked(tmp, new->expiry_time);
170 cache_fresh_locked(old, 0);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800171 write_unlock(&detail->hash_lock);
NeilBrownebd0cb12006-03-27 01:15:08 -0800172 cache_fresh_unlocked(tmp, detail, is_new);
173 cache_fresh_unlocked(old, detail, 0);
NeilBrownbaab9352006-03-27 01:15:09 -0800174 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800175 return tmp;
176}
Trond Myklebust24c37672008-12-23 16:30:12 -0500177EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400179static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
180{
181 if (!cd->cache_upcall)
182 return -EINVAL;
183 return cd->cache_upcall(cd, h);
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
187 * This is the generic cache management routine for all
188 * the authentication caches.
189 * It checks the currency of a cache item and will (later)
190 * initiate an upcall to fill it if needed.
191 *
192 *
193 * Returns 0 if the cache_head can be used, or cache_puts it and returns
194 * -EAGAIN if upcall is pending,
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800195 * -ETIMEDOUT if upcall failed and should be retried,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * -ENOENT if cache entry was negative
197 */
198int cache_check(struct cache_detail *detail,
199 struct cache_head *h, struct cache_req *rqstp)
200{
201 int rv;
202 long refresh_age, age;
203
204 /* First decide return status as best we can */
205 if (!test_bit(CACHE_VALID, &h->flags) ||
206 h->expiry_time < get_seconds())
207 rv = -EAGAIN;
208 else if (detail->flush_time > h->last_refresh)
209 rv = -EAGAIN;
210 else {
211 /* entry is valid */
212 if (test_bit(CACHE_NEGATIVE, &h->flags))
213 rv = -ENOENT;
214 else rv = 0;
215 }
216
217 /* now see if we want to start an upcall */
218 refresh_age = (h->expiry_time - h->last_refresh);
219 age = get_seconds() - h->last_refresh;
220
221 if (rqstp == NULL) {
222 if (rv == -EAGAIN)
223 rv = -ENOENT;
224 } else if (rv == -EAGAIN || age > refresh_age/2) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500225 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
226 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
228 switch (cache_make_upcall(detail, h)) {
229 case -EINVAL:
230 clear_bit(CACHE_PENDING, &h->flags);
231 if (rv == -EAGAIN) {
232 set_bit(CACHE_NEGATIVE, &h->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800233 cache_fresh_unlocked(h, detail,
234 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 rv = -ENOENT;
236 }
237 break;
238
239 case -EAGAIN:
240 clear_bit(CACHE_PENDING, &h->flags);
241 cache_revisit_request(h);
242 break;
243 }
244 }
245 }
246
247 if (rv == -EAGAIN)
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800248 if (cache_defer_req(rqstp, h) != 0)
249 rv = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
NeilBrown4013ede2006-03-27 01:15:07 -0800251 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800252 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return rv;
254}
Trond Myklebust24c37672008-12-23 16:30:12 -0500255EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/*
258 * caches need to be periodically cleaned.
259 * For this we maintain a list of cache_detail and
260 * a current pointer into that list and into the table
261 * for that entry.
262 *
263 * Each time clean_cache is called it finds the next non-empty entry
264 * in the current table and walks the list in that entry
265 * looking for entries that can be removed.
266 *
267 * An entry gets removed if:
268 * - The expiry is before current time
269 * - The last_refresh time is before the flush_time for that cache
270 *
271 * later we might drop old entries with non-NEVER expiry if that table
272 * is getting 'full' for some definition of 'full'
273 *
274 * The question of "how often to scan a table" is an interesting one
275 * and is answered in part by the use of the "nextcheck" field in the
276 * cache_detail.
277 * When a scan of a table begins, the nextcheck field is set to a time
278 * that is well into the future.
279 * While scanning, if an expiry time is found that is earlier than the
280 * current nextcheck time, nextcheck is set to that expiry time.
281 * If the flush_time is ever set to a time earlier than the nextcheck
282 * time, the nextcheck time is then set to that flush_time.
283 *
284 * A table is then only scanned if the current time is at least
285 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800286 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
288
289static LIST_HEAD(cache_list);
290static DEFINE_SPINLOCK(cache_list_lock);
291static struct cache_detail *current_detail;
292static int current_index;
293
Arjan van de Venda7071d2007-02-12 00:55:36 -0800294static const struct file_operations cache_file_operations;
295static const struct file_operations content_file_operations;
296static const struct file_operations cache_flush_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
David Howells65f27f32006-11-22 14:55:48 +0000298static void do_cache_clean(struct work_struct *work);
299static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500301static void remove_cache_proc_entries(struct cache_detail *cd)
302{
303 if (cd->proc_ent == NULL)
304 return;
305 if (cd->flush_ent)
306 remove_proc_entry("flush", cd->proc_ent);
307 if (cd->channel_ent)
308 remove_proc_entry("channel", cd->proc_ent);
309 if (cd->content_ent)
310 remove_proc_entry("content", cd->proc_ent);
311 cd->proc_ent = NULL;
312 remove_proc_entry(cd->name, proc_net_rpc);
313}
314
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500315#ifdef CONFIG_PROC_FS
316static int create_cache_proc_entries(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500317{
318 struct proc_dir_entry *p;
319
320 cd->proc_ent = proc_mkdir(cd->name, proc_net_rpc);
321 if (cd->proc_ent == NULL)
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500322 goto out_nomem;
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500323 cd->channel_ent = cd->content_ent = NULL;
324
Denis V. Luneve7fe2332008-05-02 02:44:36 -0700325 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
326 cd->proc_ent, &cache_flush_operations, cd);
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500327 cd->flush_ent = p;
328 if (p == NULL)
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500329 goto out_nomem;
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500330
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400331 if (cd->cache_upcall || cd->cache_parse) {
Denis V. Luneve7fe2332008-05-02 02:44:36 -0700332 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
333 cd->proc_ent, &cache_file_operations, cd);
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500334 cd->channel_ent = p;
335 if (p == NULL)
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500336 goto out_nomem;
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500337 }
338 if (cd->cache_show) {
Denis V. Luneve7fe2332008-05-02 02:44:36 -0700339 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
340 cd->proc_ent, &content_file_operations, cd);
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500341 cd->content_ent = p;
342 if (p == NULL)
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500343 goto out_nomem;
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500344 }
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
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400357static void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500358{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 rwlock_init(&cd->hash_lock);
360 INIT_LIST_HEAD(&cd->queue);
361 spin_lock(&cache_list_lock);
362 cd->nextcheck = 0;
363 cd->entries = 0;
364 atomic_set(&cd->readers, 0);
365 cd->last_close = 0;
366 cd->last_warn = -1;
367 list_add(&cd->others, &cache_list);
368 spin_unlock(&cache_list_lock);
369
370 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000371 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400374static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 cache_purge(cd);
377 spin_lock(&cache_list_lock);
378 write_lock(&cd->hash_lock);
379 if (cd->entries || atomic_read(&cd->inuse)) {
380 write_unlock(&cd->hash_lock);
381 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 if (current_detail == cd)
385 current_detail = NULL;
386 list_del_init(&cd->others);
387 write_unlock(&cd->hash_lock);
388 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (list_empty(&cache_list)) {
390 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400391 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500393 return;
394out:
395 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400397
398int cache_register(struct cache_detail *cd)
399{
400 int ret;
401
402 sunrpc_init_cache_detail(cd);
403 ret = create_cache_proc_entries(cd);
404 if (ret)
405 sunrpc_destroy_cache_detail(cd);
406 return ret;
407}
408EXPORT_SYMBOL_GPL(cache_register);
409
410void cache_unregister(struct cache_detail *cd)
411{
412 remove_cache_proc_entries(cd);
413 sunrpc_destroy_cache_detail(cd);
414}
Trond Myklebust24c37672008-12-23 16:30:12 -0500415EXPORT_SYMBOL_GPL(cache_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417/* clean cache tries to find something to clean
418 * and cleans it.
419 * It returns 1 if it cleaned something,
420 * 0 if it didn't find anything this time
421 * -1 if it fell off the end of the list.
422 */
423static int cache_clean(void)
424{
425 int rv = 0;
426 struct list_head *next;
427
428 spin_lock(&cache_list_lock);
429
430 /* find a suitable table if we don't already have one */
431 while (current_detail == NULL ||
432 current_index >= current_detail->hash_size) {
433 if (current_detail)
434 next = current_detail->others.next;
435 else
436 next = cache_list.next;
437 if (next == &cache_list) {
438 current_detail = NULL;
439 spin_unlock(&cache_list_lock);
440 return -1;
441 }
442 current_detail = list_entry(next, struct cache_detail, others);
443 if (current_detail->nextcheck > get_seconds())
444 current_index = current_detail->hash_size;
445 else {
446 current_index = 0;
447 current_detail->nextcheck = get_seconds()+30*60;
448 }
449 }
450
451 /* find a non-empty bucket in the table */
452 while (current_detail &&
453 current_index < current_detail->hash_size &&
454 current_detail->hash_table[current_index] == NULL)
455 current_index++;
456
457 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (current_detail && current_index < current_detail->hash_size) {
460 struct cache_head *ch, **cp;
461 struct cache_detail *d;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 write_lock(&current_detail->hash_lock);
464
465 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 cp = & current_detail->hash_table[current_index];
468 ch = *cp;
469 for (; ch; cp= & ch->next, ch= *cp) {
470 if (current_detail->nextcheck > ch->expiry_time)
471 current_detail->nextcheck = ch->expiry_time+1;
472 if (ch->expiry_time >= get_seconds()
473 && ch->last_refresh >= current_detail->flush_time
474 )
475 continue;
476 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
477 queue_loose(current_detail, ch);
478
NeilBrownbaab9352006-03-27 01:15:09 -0800479 if (atomic_read(&ch->ref.refcount) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481 }
482 if (ch) {
483 *cp = ch->next;
484 ch->next = NULL;
485 current_detail->entries--;
486 rv = 1;
487 }
488 write_unlock(&current_detail->hash_lock);
489 d = current_detail;
490 if (!ch)
491 current_index ++;
492 spin_unlock(&cache_list_lock);
493 if (ch)
NeilBrownbaab9352006-03-27 01:15:09 -0800494 cache_put(ch, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 } else
496 spin_unlock(&cache_list_lock);
497
498 return rv;
499}
500
501/*
502 * We want to regularly clean the cache, so we need to schedule some work ...
503 */
David Howells65f27f32006-11-22 14:55:48 +0000504static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 int delay = 5;
507 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700508 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (list_empty(&cache_list))
511 delay = 0;
512
513 if (delay)
514 schedule_delayed_work(&cache_cleaner, delay);
515}
516
517
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800518/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800520 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * be fully cleaned
522 */
523void cache_flush(void)
524{
525 while (cache_clean() != -1)
526 cond_resched();
527 while (cache_clean() != -1)
528 cond_resched();
529}
Trond Myklebust24c37672008-12-23 16:30:12 -0500530EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532void cache_purge(struct cache_detail *detail)
533{
534 detail->flush_time = LONG_MAX;
535 detail->nextcheck = get_seconds();
536 cache_flush();
537 detail->flush_time = 1;
538}
Trond Myklebust24c37672008-12-23 16:30:12 -0500539EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541
542/*
543 * Deferral and Revisiting of Requests.
544 *
545 * If a cache lookup finds a pending entry, we
546 * need to defer the request and revisit it later.
547 * All deferred requests are stored in a hash table,
548 * indexed by "struct cache_head *".
549 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800550 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * deferred form, which must contain a
552 * 'struct cache_deferred_req'
553 * This cache_deferred_req contains a method to allow
554 * it to be revisited when cache info is available
555 */
556
557#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
558#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
559
560#define DFR_MAX 300 /* ??? */
561
562static DEFINE_SPINLOCK(cache_defer_lock);
563static LIST_HEAD(cache_defer_list);
564static struct list_head cache_defer_hash[DFR_HASHSIZE];
565static int cache_defer_cnt;
566
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800567static int cache_defer_req(struct cache_req *req, struct cache_head *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct cache_deferred_req *dreq;
570 int hash = DFR_HASH(item);
571
J.Bruce Fields01f3bd12006-12-13 00:35:26 -0800572 if (cache_defer_cnt >= DFR_MAX) {
573 /* too much in the cache, randomly drop this one,
574 * or continue and drop the oldest below
575 */
576 if (net_random()&1)
577 return -ETIMEDOUT;
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 dreq = req->defer(req);
580 if (dreq == NULL)
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800581 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 spin_lock(&cache_defer_lock);
586
587 list_add(&dreq->recent, &cache_defer_list);
588
589 if (cache_defer_hash[hash].next == NULL)
590 INIT_LIST_HEAD(&cache_defer_hash[hash]);
591 list_add(&dreq->hash, &cache_defer_hash[hash]);
592
593 /* it is in, now maybe clean up */
594 dreq = NULL;
595 if (++cache_defer_cnt > DFR_MAX) {
J.Bruce Fields01f3bd12006-12-13 00:35:26 -0800596 dreq = list_entry(cache_defer_list.prev,
597 struct cache_deferred_req, recent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 list_del(&dreq->recent);
599 list_del(&dreq->hash);
600 cache_defer_cnt--;
601 }
602 spin_unlock(&cache_defer_lock);
603
604 if (dreq) {
605 /* there was one too many */
606 dreq->revisit(dreq, 1);
607 }
NeilBrown4013ede2006-03-27 01:15:07 -0800608 if (!test_bit(CACHE_PENDING, &item->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* must have just been validated... */
610 cache_revisit_request(item);
611 }
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615static void cache_revisit_request(struct cache_head *item)
616{
617 struct cache_deferred_req *dreq;
618 struct list_head pending;
619
620 struct list_head *lp;
621 int hash = DFR_HASH(item);
622
623 INIT_LIST_HEAD(&pending);
624 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 lp = cache_defer_hash[hash].next;
627 if (lp) {
628 while (lp != &cache_defer_hash[hash]) {
629 dreq = list_entry(lp, struct cache_deferred_req, hash);
630 lp = lp->next;
631 if (dreq->item == item) {
632 list_del(&dreq->hash);
633 list_move(&dreq->recent, &pending);
634 cache_defer_cnt--;
635 }
636 }
637 }
638 spin_unlock(&cache_defer_lock);
639
640 while (!list_empty(&pending)) {
641 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
642 list_del_init(&dreq->recent);
643 dreq->revisit(dreq, 0);
644 }
645}
646
647void cache_clean_deferred(void *owner)
648{
649 struct cache_deferred_req *dreq, *tmp;
650 struct list_head pending;
651
652
653 INIT_LIST_HEAD(&pending);
654 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
657 if (dreq->owner == owner) {
658 list_del(&dreq->hash);
659 list_move(&dreq->recent, &pending);
660 cache_defer_cnt--;
661 }
662 }
663 spin_unlock(&cache_defer_lock);
664
665 while (!list_empty(&pending)) {
666 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
667 list_del_init(&dreq->recent);
668 dreq->revisit(dreq, 1);
669 }
670}
671
672/*
673 * communicate with user-space
674 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500675 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
676 * On read, you get a full request, or block.
677 * On write, an update request is processed.
678 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800680 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500681 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * to the end and may wakeup and preceding readers.
683 * New readers are added to the head. If, on read, an item is found with
684 * CACHE_UPCALLING clear, we free it from the list.
685 *
686 */
687
688static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800689static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691struct cache_queue {
692 struct list_head list;
693 int reader; /* if 0, then request */
694};
695struct cache_request {
696 struct cache_queue q;
697 struct cache_head *item;
698 char * buf;
699 int len;
700 int readers;
701};
702struct cache_reader {
703 struct cache_queue q;
704 int offset; /* if non-0, we have a refcnt on next request */
705};
706
707static ssize_t
708cache_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
709{
710 struct cache_reader *rp = filp->private_data;
711 struct cache_request *rq;
Trond Myklebustda770052009-08-09 15:14:28 -0400712 struct inode *inode = filp->f_path.dentry->d_inode;
713 struct cache_detail *cd = PDE(inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int err;
715
716 if (count == 0)
717 return 0;
718
Trond Myklebustda770052009-08-09 15:14:28 -0400719 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * readers on this file */
721 again:
722 spin_lock(&queue_lock);
723 /* need to find next request */
724 while (rp->q.list.next != &cd->queue &&
725 list_entry(rp->q.list.next, struct cache_queue, list)
726 ->reader) {
727 struct list_head *next = rp->q.list.next;
728 list_move(&rp->q.list, next);
729 }
730 if (rp->q.list.next == &cd->queue) {
731 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400732 mutex_unlock(&inode->i_mutex);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800733 BUG_ON(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return 0;
735 }
736 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800737 BUG_ON(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (rp->offset == 0)
739 rq->readers++;
740 spin_unlock(&queue_lock);
741
742 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
743 err = -EAGAIN;
744 spin_lock(&queue_lock);
745 list_move(&rp->q.list, &rq->q.list);
746 spin_unlock(&queue_lock);
747 } else {
748 if (rp->offset + count > rq->len)
749 count = rq->len - rp->offset;
750 err = -EFAULT;
751 if (copy_to_user(buf, rq->buf + rp->offset, count))
752 goto out;
753 rp->offset += count;
754 if (rp->offset >= rq->len) {
755 rp->offset = 0;
756 spin_lock(&queue_lock);
757 list_move(&rp->q.list, &rq->q.list);
758 spin_unlock(&queue_lock);
759 }
760 err = 0;
761 }
762 out:
763 if (rp->offset == 0) {
764 /* need to release rq */
765 spin_lock(&queue_lock);
766 rq->readers--;
767 if (rq->readers == 0 &&
768 !test_bit(CACHE_PENDING, &rq->item->flags)) {
769 list_del(&rq->q.list);
770 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800771 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 kfree(rq->buf);
773 kfree(rq);
774 } else
775 spin_unlock(&queue_lock);
776 }
777 if (err == -EAGAIN)
778 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400779 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return err ? err : count;
781}
782
Trond Myklebustda770052009-08-09 15:14:28 -0400783static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
784 size_t count, struct cache_detail *cd)
785{
786 ssize_t ret;
787
788 if (copy_from_user(kaddr, buf, count))
789 return -EFAULT;
790 kaddr[count] = '\0';
791 ret = cd->cache_parse(cd, kaddr, count);
792 if (!ret)
793 ret = count;
794 return ret;
795}
796
797static ssize_t cache_slow_downcall(const char __user *buf,
798 size_t count, struct cache_detail *cd)
799{
800 static char write_buf[8192]; /* protected by queue_io_mutex */
801 ssize_t ret = -EINVAL;
802
803 if (count >= sizeof(write_buf))
804 goto out;
805 mutex_lock(&queue_io_mutex);
806 ret = cache_do_downcall(write_buf, buf, count, cd);
807 mutex_unlock(&queue_io_mutex);
808out:
809 return ret;
810}
811
812static ssize_t cache_downcall(struct address_space *mapping,
813 const char __user *buf,
814 size_t count, struct cache_detail *cd)
815{
816 struct page *page;
817 char *kaddr;
818 ssize_t ret = -ENOMEM;
819
820 if (count >= PAGE_CACHE_SIZE)
821 goto out_slow;
822
823 page = find_or_create_page(mapping, 0, GFP_KERNEL);
824 if (!page)
825 goto out_slow;
826
827 kaddr = kmap(page);
828 ret = cache_do_downcall(kaddr, buf, count, cd);
829 kunmap(page);
830 unlock_page(page);
831 page_cache_release(page);
832 return ret;
833out_slow:
834 return cache_slow_downcall(buf, count, cd);
835}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837static ssize_t
838cache_write(struct file *filp, const char __user *buf, size_t count,
839 loff_t *ppos)
840{
Trond Myklebustda770052009-08-09 15:14:28 -0400841 struct address_space *mapping = filp->f_mapping;
842 struct inode *inode = filp->f_path.dentry->d_inode;
843 struct cache_detail *cd = PDE(inode)->data;
844 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Trond Myklebustda770052009-08-09 15:14:28 -0400846 if (!cd->cache_parse)
847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Trond Myklebustda770052009-08-09 15:14:28 -0400849 mutex_lock(&inode->i_mutex);
850 ret = cache_downcall(mapping, buf, count, cd);
851 mutex_unlock(&inode->i_mutex);
852out:
853 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
857
858static unsigned int
859cache_poll(struct file *filp, poll_table *wait)
860{
861 unsigned int mask;
862 struct cache_reader *rp = filp->private_data;
863 struct cache_queue *cq;
Josef Sipek303b46b2006-12-08 02:37:42 -0800864 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 poll_wait(filp, &queue_wait, wait);
867
868 /* alway allow write */
869 mask = POLL_OUT | POLLWRNORM;
870
871 if (!rp)
872 return mask;
873
874 spin_lock(&queue_lock);
875
876 for (cq= &rp->q; &cq->list != &cd->queue;
877 cq = list_entry(cq->list.next, struct cache_queue, list))
878 if (!cq->reader) {
879 mask |= POLLIN | POLLRDNORM;
880 break;
881 }
882 spin_unlock(&queue_lock);
883 return mask;
884}
885
886static int
887cache_ioctl(struct inode *ino, struct file *filp,
888 unsigned int cmd, unsigned long arg)
889{
890 int len = 0;
891 struct cache_reader *rp = filp->private_data;
892 struct cache_queue *cq;
893 struct cache_detail *cd = PDE(ino)->data;
894
895 if (cmd != FIONREAD || !rp)
896 return -EINVAL;
897
898 spin_lock(&queue_lock);
899
900 /* only find the length remaining in current request,
901 * or the length of the next request
902 */
903 for (cq= &rp->q; &cq->list != &cd->queue;
904 cq = list_entry(cq->list.next, struct cache_queue, list))
905 if (!cq->reader) {
906 struct cache_request *cr =
907 container_of(cq, struct cache_request, q);
908 len = cr->len - rp->offset;
909 break;
910 }
911 spin_unlock(&queue_lock);
912
913 return put_user(len, (int __user *)arg);
914}
915
916static int
917cache_open(struct inode *inode, struct file *filp)
918{
919 struct cache_reader *rp = NULL;
920
921 nonseekable_open(inode, filp);
922 if (filp->f_mode & FMODE_READ) {
923 struct cache_detail *cd = PDE(inode)->data;
924
925 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
926 if (!rp)
927 return -ENOMEM;
928 rp->offset = 0;
929 rp->q.reader = 1;
930 atomic_inc(&cd->readers);
931 spin_lock(&queue_lock);
932 list_add(&rp->q.list, &cd->queue);
933 spin_unlock(&queue_lock);
934 }
935 filp->private_data = rp;
936 return 0;
937}
938
939static int
940cache_release(struct inode *inode, struct file *filp)
941{
942 struct cache_reader *rp = filp->private_data;
943 struct cache_detail *cd = PDE(inode)->data;
944
945 if (rp) {
946 spin_lock(&queue_lock);
947 if (rp->offset) {
948 struct cache_queue *cq;
949 for (cq= &rp->q; &cq->list != &cd->queue;
950 cq = list_entry(cq->list.next, struct cache_queue, list))
951 if (!cq->reader) {
952 container_of(cq, struct cache_request, q)
953 ->readers--;
954 break;
955 }
956 rp->offset = 0;
957 }
958 list_del(&rp->q.list);
959 spin_unlock(&queue_lock);
960
961 filp->private_data = NULL;
962 kfree(rp);
963
964 cd->last_close = get_seconds();
965 atomic_dec(&cd->readers);
966 }
967 return 0;
968}
969
970
971
Arjan van de Venda7071d2007-02-12 00:55:36 -0800972static const struct file_operations cache_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 .owner = THIS_MODULE,
974 .llseek = no_llseek,
975 .read = cache_read,
976 .write = cache_write,
977 .poll = cache_poll,
978 .ioctl = cache_ioctl, /* for FIONREAD */
979 .open = cache_open,
980 .release = cache_release,
981};
982
983
984static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
985{
986 struct cache_queue *cq;
987 spin_lock(&queue_lock);
988 list_for_each_entry(cq, &detail->queue, list)
989 if (!cq->reader) {
990 struct cache_request *cr = container_of(cq, struct cache_request, q);
991 if (cr->item != ch)
992 continue;
993 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -0800994 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 list_del(&cr->q.list);
996 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800997 cache_put(cr->item, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 kfree(cr->buf);
999 kfree(cr);
1000 return;
1001 }
1002 spin_unlock(&queue_lock);
1003}
1004
1005/*
1006 * Support routines for text-based upcalls.
1007 * Fields are separated by spaces.
1008 * Fields are either mangled to quote space tab newline slosh with slosh
1009 * or a hexified with a leading \x
1010 * Record is terminated with newline.
1011 *
1012 */
1013
1014void qword_add(char **bpp, int *lp, char *str)
1015{
1016 char *bp = *bpp;
1017 int len = *lp;
1018 char c;
1019
1020 if (len < 0) return;
1021
1022 while ((c=*str++) && len)
1023 switch(c) {
1024 case ' ':
1025 case '\t':
1026 case '\n':
1027 case '\\':
1028 if (len >= 4) {
1029 *bp++ = '\\';
1030 *bp++ = '0' + ((c & 0300)>>6);
1031 *bp++ = '0' + ((c & 0070)>>3);
1032 *bp++ = '0' + ((c & 0007)>>0);
1033 }
1034 len -= 4;
1035 break;
1036 default:
1037 *bp++ = c;
1038 len--;
1039 }
1040 if (c || len <1) len = -1;
1041 else {
1042 *bp++ = ' ';
1043 len--;
1044 }
1045 *bpp = bp;
1046 *lp = len;
1047}
Trond Myklebust24c37672008-12-23 16:30:12 -05001048EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1051{
1052 char *bp = *bpp;
1053 int len = *lp;
1054
1055 if (len < 0) return;
1056
1057 if (len > 2) {
1058 *bp++ = '\\';
1059 *bp++ = 'x';
1060 len -= 2;
1061 while (blen && len >= 2) {
1062 unsigned char c = *buf++;
1063 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1064 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1065 len -= 2;
1066 blen--;
1067 }
1068 }
1069 if (blen || len<1) len = -1;
1070 else {
1071 *bp++ = ' ';
1072 len--;
1073 }
1074 *bpp = bp;
1075 *lp = len;
1076}
Trond Myklebust24c37672008-12-23 16:30:12 -05001077EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079static void warn_no_listener(struct cache_detail *detail)
1080{
1081 if (detail->last_warn != detail->last_close) {
1082 detail->last_warn = detail->last_close;
1083 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001084 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086}
1087
1088/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001089 * register an upcall request to user-space and queue it up for read() by the
1090 * upcall daemon.
1091 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 * Each request is at most one page long.
1093 */
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001094int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1095 void (*cache_request)(struct cache_detail *,
1096 struct cache_head *,
1097 char **,
1098 int *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100
1101 char *buf;
1102 struct cache_request *crq;
1103 char *bp;
1104 int len;
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (atomic_read(&detail->readers) == 0 &&
1107 detail->last_close < get_seconds() - 30) {
1108 warn_no_listener(detail);
1109 return -EINVAL;
1110 }
1111
1112 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1113 if (!buf)
1114 return -EAGAIN;
1115
1116 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1117 if (!crq) {
1118 kfree(buf);
1119 return -EAGAIN;
1120 }
1121
1122 bp = buf; len = PAGE_SIZE;
1123
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001124 cache_request(detail, h, &bp, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 if (len < 0) {
1127 kfree(buf);
1128 kfree(crq);
1129 return -EAGAIN;
1130 }
1131 crq->q.reader = 0;
1132 crq->item = cache_get(h);
1133 crq->buf = buf;
1134 crq->len = PAGE_SIZE - len;
1135 crq->readers = 0;
1136 spin_lock(&queue_lock);
1137 list_add_tail(&crq->q.list, &detail->queue);
1138 spin_unlock(&queue_lock);
1139 wake_up(&queue_wait);
1140 return 0;
1141}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001142EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144/*
1145 * parse a message from user-space and pass it
1146 * to an appropriate cache
1147 * Messages are, like requests, separated into fields by
1148 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1149 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001150 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 * reply cachename expiry key ... content....
1152 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001153 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 */
1155
1156#define isodigit(c) (isdigit(c) && c <= '7')
1157int qword_get(char **bpp, char *dest, int bufsize)
1158{
1159 /* return bytes copied, or -1 on error */
1160 char *bp = *bpp;
1161 int len = 0;
1162
1163 while (*bp == ' ') bp++;
1164
1165 if (bp[0] == '\\' && bp[1] == 'x') {
1166 /* HEX STRING */
1167 bp += 2;
1168 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1169 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1170 bp++;
1171 byte <<= 4;
1172 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1173 *dest++ = byte;
1174 bp++;
1175 len++;
1176 }
1177 } else {
1178 /* text with \nnn octal quoting */
1179 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1180 if (*bp == '\\' &&
1181 isodigit(bp[1]) && (bp[1] <= '3') &&
1182 isodigit(bp[2]) &&
1183 isodigit(bp[3])) {
1184 int byte = (*++bp -'0');
1185 bp++;
1186 byte = (byte << 3) | (*bp++ - '0');
1187 byte = (byte << 3) | (*bp++ - '0');
1188 *dest++ = byte;
1189 len++;
1190 } else {
1191 *dest++ = *bp++;
1192 len++;
1193 }
1194 }
1195 }
1196
1197 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1198 return -1;
1199 while (*bp == ' ') bp++;
1200 *bpp = bp;
1201 *dest = '\0';
1202 return len;
1203}
Trond Myklebust24c37672008-12-23 16:30:12 -05001204EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206
1207/*
1208 * support /proc/sunrpc/cache/$CACHENAME/content
1209 * as a seqfile.
1210 * We call ->cache_show passing NULL for the item to
1211 * get a header, then pass each real item in the cache
1212 */
1213
1214struct handle {
1215 struct cache_detail *cd;
1216};
1217
1218static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001219 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 loff_t n = *pos;
1222 unsigned hash, entry;
1223 struct cache_head *ch;
1224 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 read_lock(&cd->hash_lock);
1228 if (!n--)
1229 return SEQ_START_TOKEN;
1230 hash = n >> 32;
1231 entry = n & ((1LL<<32) - 1);
1232
1233 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1234 if (!entry--)
1235 return ch;
1236 n &= ~((1LL<<32) - 1);
1237 do {
1238 hash++;
1239 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001240 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 cd->hash_table[hash]==NULL);
1242 if (hash >= cd->hash_size)
1243 return NULL;
1244 *pos = n+1;
1245 return cd->hash_table[hash];
1246}
1247
1248static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1249{
1250 struct cache_head *ch = p;
1251 int hash = (*pos >> 32);
1252 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1253
1254 if (p == SEQ_START_TOKEN)
1255 hash = 0;
1256 else if (ch->next == NULL) {
1257 hash++;
1258 *pos += 1LL<<32;
1259 } else {
1260 ++*pos;
1261 return ch->next;
1262 }
1263 *pos &= ~((1LL<<32) - 1);
1264 while (hash < cd->hash_size &&
1265 cd->hash_table[hash] == NULL) {
1266 hash++;
1267 *pos += 1LL<<32;
1268 }
1269 if (hash >= cd->hash_size)
1270 return NULL;
1271 ++*pos;
1272 return cd->hash_table[hash];
1273}
1274
1275static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001276 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1279 read_unlock(&cd->hash_lock);
1280}
1281
1282static int c_show(struct seq_file *m, void *p)
1283{
1284 struct cache_head *cp = p;
1285 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1286
1287 if (p == SEQ_START_TOKEN)
1288 return cd->cache_show(m, cd, NULL);
1289
1290 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001291 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownbaab9352006-03-27 01:15:09 -08001292 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 cache_get(cp);
1294 if (cache_check(cd, cp, NULL))
1295 /* cache_check does a cache_put on failure */
1296 seq_printf(m, "# ");
1297 else
1298 cache_put(cp, cd);
1299
1300 return cd->cache_show(m, cd, cp);
1301}
1302
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001303static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 .start = c_start,
1305 .next = c_next,
1306 .stop = c_stop,
1307 .show = c_show,
1308};
1309
1310static int content_open(struct inode *inode, struct file *file)
1311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 struct handle *han;
1313 struct cache_detail *cd = PDE(inode)->data;
1314
Pavel Emelyanovec931032007-10-10 02:31:07 -07001315 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (han == NULL)
1317 return -ENOMEM;
1318
1319 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Arjan van de Venda7071d2007-02-12 00:55:36 -08001323static const struct file_operations content_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 .open = content_open,
1325 .read = seq_read,
1326 .llseek = seq_lseek,
Martin Peschke14690fc2007-04-26 01:03:43 -07001327 .release = seq_release_private,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328};
1329
1330static ssize_t read_flush(struct file *file, char __user *buf,
1331 size_t count, loff_t *ppos)
1332{
Josef Sipek303b46b2006-12-08 02:37:42 -08001333 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 char tbuf[20];
1335 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001336 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 sprintf(tbuf, "%lu\n", cd->flush_time);
1339 len = strlen(tbuf);
1340 if (p >= len)
1341 return 0;
1342 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001343 if (len > count)
1344 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001346 return -EFAULT;
1347 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return len;
1349}
1350
1351static ssize_t write_flush(struct file * file, const char __user * buf,
1352 size_t count, loff_t *ppos)
1353{
Josef Sipek303b46b2006-12-08 02:37:42 -08001354 struct cache_detail *cd = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 char tbuf[20];
1356 char *ep;
1357 long flushtime;
1358 if (*ppos || count > sizeof(tbuf)-1)
1359 return -EINVAL;
1360 if (copy_from_user(tbuf, buf, count))
1361 return -EFAULT;
1362 tbuf[count] = 0;
1363 flushtime = simple_strtoul(tbuf, &ep, 0);
1364 if (*ep && *ep != '\n')
1365 return -EINVAL;
1366
1367 cd->flush_time = flushtime;
1368 cd->nextcheck = get_seconds();
1369 cache_flush();
1370
1371 *ppos += count;
1372 return count;
1373}
1374
Arjan van de Venda7071d2007-02-12 00:55:36 -08001375static const struct file_operations cache_flush_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 .open = nonseekable_open,
1377 .read = read_flush,
1378 .write = write_flush,
1379};