blob: db7720e453c3b4e67a9a0da5558ffd5b66120d8c [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>
Trond Myklebust8854e822009-08-09 15:14:30 -040035#include <linux/sunrpc/rpc_pipe_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#define RPCDBG_FACILITY RPCDBG_CACHE
38
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -080039static int cache_defer_req(struct cache_req *req, struct cache_head *item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static void cache_revisit_request(struct cache_head *item);
41
Adrian Bunk74cae612006-03-27 01:15:10 -080042static void cache_init(struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 time_t now = get_seconds();
45 h->next = NULL;
46 h->flags = 0;
NeilBrownbaab9352006-03-27 01:15:09 -080047 kref_init(&h->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 h->expiry_time = now + CACHE_NEW_EXPIRY;
49 h->last_refresh = now;
50}
51
NeilBrown15a5f6b2006-03-27 01:15:02 -080052struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
53 struct cache_head *key, int hash)
54{
55 struct cache_head **head, **hp;
56 struct cache_head *new = NULL;
57
58 head = &detail->hash_table[hash];
59
60 read_lock(&detail->hash_lock);
61
62 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
63 struct cache_head *tmp = *hp;
64 if (detail->match(tmp, key)) {
65 cache_get(tmp);
66 read_unlock(&detail->hash_lock);
67 return tmp;
68 }
69 }
70 read_unlock(&detail->hash_lock);
71 /* Didn't find anything, insert an empty entry */
72
73 new = detail->alloc();
74 if (!new)
75 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070076 /* must fully initialise 'new', else
77 * we might get lose if we need to
78 * cache_put it soon.
79 */
NeilBrown15a5f6b2006-03-27 01:15:02 -080080 cache_init(new);
Neil Brown2f349312006-08-05 12:14:29 -070081 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080082
83 write_lock(&detail->hash_lock);
84
85 /* check if entry appeared while we slept */
86 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
87 struct cache_head *tmp = *hp;
88 if (detail->match(tmp, key)) {
89 cache_get(tmp);
90 write_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -080091 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -080092 return tmp;
93 }
94 }
NeilBrown15a5f6b2006-03-27 01:15:02 -080095 new->next = *head;
96 *head = new;
97 detail->entries++;
98 cache_get(new);
99 write_unlock(&detail->hash_lock);
100
101 return new;
102}
Trond Myklebust24c37672008-12-23 16:30:12 -0500103EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800104
NeilBrownebd0cb12006-03-27 01:15:08 -0800105
106static void queue_loose(struct cache_detail *detail, struct cache_head *ch);
107
108static int cache_fresh_locked(struct cache_head *head, time_t expiry)
109{
110 head->expiry_time = expiry;
111 head->last_refresh = get_seconds();
112 return !test_and_set_bit(CACHE_VALID, &head->flags);
113}
114
115static void cache_fresh_unlocked(struct cache_head *head,
116 struct cache_detail *detail, int new)
117{
118 if (new)
119 cache_revisit_request(head);
120 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
121 cache_revisit_request(head);
122 queue_loose(detail, head);
123 }
124}
125
NeilBrown15a5f6b2006-03-27 01:15:02 -0800126struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
127 struct cache_head *new, struct cache_head *old, int hash)
128{
129 /* The 'old' entry is to be replaced by 'new'.
130 * If 'old' is not VALID, we update it directly,
131 * otherwise we need to replace it
132 */
133 struct cache_head **head;
134 struct cache_head *tmp;
NeilBrownebd0cb12006-03-27 01:15:08 -0800135 int is_new;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800136
137 if (!test_bit(CACHE_VALID, &old->flags)) {
138 write_lock(&detail->hash_lock);
139 if (!test_bit(CACHE_VALID, &old->flags)) {
140 if (test_bit(CACHE_NEGATIVE, &new->flags))
141 set_bit(CACHE_NEGATIVE, &old->flags);
142 else
143 detail->update(old, new);
NeilBrownebd0cb12006-03-27 01:15:08 -0800144 is_new = cache_fresh_locked(old, new->expiry_time);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800145 write_unlock(&detail->hash_lock);
NeilBrownebd0cb12006-03-27 01:15:08 -0800146 cache_fresh_unlocked(old, detail, is_new);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800147 return old;
148 }
149 write_unlock(&detail->hash_lock);
150 }
151 /* We need to insert a new entry */
152 tmp = detail->alloc();
153 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800154 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800155 return NULL;
156 }
157 cache_init(tmp);
158 detail->init(tmp, old);
159 head = &detail->hash_table[hash];
160
161 write_lock(&detail->hash_lock);
162 if (test_bit(CACHE_NEGATIVE, &new->flags))
163 set_bit(CACHE_NEGATIVE, &tmp->flags);
164 else
165 detail->update(tmp, new);
166 tmp->next = *head;
167 *head = tmp;
NeilBrownf2d39582006-05-22 22:35:25 -0700168 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800169 cache_get(tmp);
NeilBrownebd0cb12006-03-27 01:15:08 -0800170 is_new = cache_fresh_locked(tmp, new->expiry_time);
171 cache_fresh_locked(old, 0);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800172 write_unlock(&detail->hash_lock);
NeilBrownebd0cb12006-03-27 01:15:08 -0800173 cache_fresh_unlocked(tmp, detail, is_new);
174 cache_fresh_unlocked(old, detail, 0);
NeilBrownbaab9352006-03-27 01:15:09 -0800175 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800176 return tmp;
177}
Trond Myklebust24c37672008-12-23 16:30:12 -0500178EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400180static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
181{
182 if (!cd->cache_upcall)
183 return -EINVAL;
184 return cd->cache_upcall(cd, h);
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
188 * This is the generic cache management routine for all
189 * the authentication caches.
190 * It checks the currency of a cache item and will (later)
191 * initiate an upcall to fill it if needed.
192 *
193 *
194 * Returns 0 if the cache_head can be used, or cache_puts it and returns
195 * -EAGAIN if upcall is pending,
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800196 * -ETIMEDOUT if upcall failed and should be retried,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * -ENOENT if cache entry was negative
198 */
199int cache_check(struct cache_detail *detail,
200 struct cache_head *h, struct cache_req *rqstp)
201{
202 int rv;
203 long refresh_age, age;
204
205 /* First decide return status as best we can */
206 if (!test_bit(CACHE_VALID, &h->flags) ||
207 h->expiry_time < get_seconds())
208 rv = -EAGAIN;
209 else if (detail->flush_time > h->last_refresh)
210 rv = -EAGAIN;
211 else {
212 /* entry is valid */
213 if (test_bit(CACHE_NEGATIVE, &h->flags))
214 rv = -ENOENT;
215 else rv = 0;
216 }
217
218 /* now see if we want to start an upcall */
219 refresh_age = (h->expiry_time - h->last_refresh);
220 age = get_seconds() - h->last_refresh;
221
222 if (rqstp == NULL) {
223 if (rv == -EAGAIN)
224 rv = -ENOENT;
225 } else if (rv == -EAGAIN || age > refresh_age/2) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500226 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
227 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
229 switch (cache_make_upcall(detail, h)) {
230 case -EINVAL:
231 clear_bit(CACHE_PENDING, &h->flags);
232 if (rv == -EAGAIN) {
233 set_bit(CACHE_NEGATIVE, &h->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800234 cache_fresh_unlocked(h, detail,
235 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 rv = -ENOENT;
237 }
238 break;
239
240 case -EAGAIN:
241 clear_bit(CACHE_PENDING, &h->flags);
242 cache_revisit_request(h);
243 break;
244 }
245 }
246 }
247
248 if (rv == -EAGAIN)
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800249 if (cache_defer_req(rqstp, h) != 0)
250 rv = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
NeilBrown4013ede2006-03-27 01:15:07 -0800252 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800253 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return rv;
255}
Trond Myklebust24c37672008-12-23 16:30:12 -0500256EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*
259 * caches need to be periodically cleaned.
260 * For this we maintain a list of cache_detail and
261 * a current pointer into that list and into the table
262 * for that entry.
263 *
264 * Each time clean_cache is called it finds the next non-empty entry
265 * in the current table and walks the list in that entry
266 * looking for entries that can be removed.
267 *
268 * An entry gets removed if:
269 * - The expiry is before current time
270 * - The last_refresh time is before the flush_time for that cache
271 *
272 * later we might drop old entries with non-NEVER expiry if that table
273 * is getting 'full' for some definition of 'full'
274 *
275 * The question of "how often to scan a table" is an interesting one
276 * and is answered in part by the use of the "nextcheck" field in the
277 * cache_detail.
278 * When a scan of a table begins, the nextcheck field is set to a time
279 * that is well into the future.
280 * While scanning, if an expiry time is found that is earlier than the
281 * current nextcheck time, nextcheck is set to that expiry time.
282 * If the flush_time is ever set to a time earlier than the nextcheck
283 * time, the nextcheck time is then set to that flush_time.
284 *
285 * A table is then only scanned if the current time is at least
286 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800287 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289
290static LIST_HEAD(cache_list);
291static DEFINE_SPINLOCK(cache_list_lock);
292static struct cache_detail *current_detail;
293static int current_index;
294
David Howells65f27f32006-11-22 14:55:48 +0000295static void do_cache_clean(struct work_struct *work);
296static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400298static void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsdbf847e2007-11-08 17:20:34 -0500299{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 rwlock_init(&cd->hash_lock);
301 INIT_LIST_HEAD(&cd->queue);
302 spin_lock(&cache_list_lock);
303 cd->nextcheck = 0;
304 cd->entries = 0;
305 atomic_set(&cd->readers, 0);
306 cd->last_close = 0;
307 cd->last_warn = -1;
308 list_add(&cd->others, &cache_list);
309 spin_unlock(&cache_list_lock);
310
311 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000312 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400315static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 cache_purge(cd);
318 spin_lock(&cache_list_lock);
319 write_lock(&cd->hash_lock);
320 if (cd->entries || atomic_read(&cd->inuse)) {
321 write_unlock(&cd->hash_lock);
322 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500323 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325 if (current_detail == cd)
326 current_detail = NULL;
327 list_del_init(&cd->others);
328 write_unlock(&cd->hash_lock);
329 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (list_empty(&cache_list)) {
331 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400332 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500334 return;
335out:
336 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/* clean cache tries to find something to clean
340 * and cleans it.
341 * It returns 1 if it cleaned something,
342 * 0 if it didn't find anything this time
343 * -1 if it fell off the end of the list.
344 */
345static int cache_clean(void)
346{
347 int rv = 0;
348 struct list_head *next;
349
350 spin_lock(&cache_list_lock);
351
352 /* find a suitable table if we don't already have one */
353 while (current_detail == NULL ||
354 current_index >= current_detail->hash_size) {
355 if (current_detail)
356 next = current_detail->others.next;
357 else
358 next = cache_list.next;
359 if (next == &cache_list) {
360 current_detail = NULL;
361 spin_unlock(&cache_list_lock);
362 return -1;
363 }
364 current_detail = list_entry(next, struct cache_detail, others);
365 if (current_detail->nextcheck > get_seconds())
366 current_index = current_detail->hash_size;
367 else {
368 current_index = 0;
369 current_detail->nextcheck = get_seconds()+30*60;
370 }
371 }
372
373 /* find a non-empty bucket in the table */
374 while (current_detail &&
375 current_index < current_detail->hash_size &&
376 current_detail->hash_table[current_index] == NULL)
377 current_index++;
378
379 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (current_detail && current_index < current_detail->hash_size) {
382 struct cache_head *ch, **cp;
383 struct cache_detail *d;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 write_lock(&current_detail->hash_lock);
386
387 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 cp = & current_detail->hash_table[current_index];
390 ch = *cp;
391 for (; ch; cp= & ch->next, ch= *cp) {
392 if (current_detail->nextcheck > ch->expiry_time)
393 current_detail->nextcheck = ch->expiry_time+1;
394 if (ch->expiry_time >= get_seconds()
395 && ch->last_refresh >= current_detail->flush_time
396 )
397 continue;
398 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
399 queue_loose(current_detail, ch);
400
NeilBrownbaab9352006-03-27 01:15:09 -0800401 if (atomic_read(&ch->ref.refcount) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 break;
403 }
404 if (ch) {
405 *cp = ch->next;
406 ch->next = NULL;
407 current_detail->entries--;
408 rv = 1;
409 }
410 write_unlock(&current_detail->hash_lock);
411 d = current_detail;
412 if (!ch)
413 current_index ++;
414 spin_unlock(&cache_list_lock);
415 if (ch)
NeilBrownbaab9352006-03-27 01:15:09 -0800416 cache_put(ch, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 } else
418 spin_unlock(&cache_list_lock);
419
420 return rv;
421}
422
423/*
424 * We want to regularly clean the cache, so we need to schedule some work ...
425 */
David Howells65f27f32006-11-22 14:55:48 +0000426static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 int delay = 5;
429 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700430 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (list_empty(&cache_list))
433 delay = 0;
434
435 if (delay)
436 schedule_delayed_work(&cache_cleaner, delay);
437}
438
439
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800440/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800442 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * be fully cleaned
444 */
445void cache_flush(void)
446{
447 while (cache_clean() != -1)
448 cond_resched();
449 while (cache_clean() != -1)
450 cond_resched();
451}
Trond Myklebust24c37672008-12-23 16:30:12 -0500452EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454void cache_purge(struct cache_detail *detail)
455{
456 detail->flush_time = LONG_MAX;
457 detail->nextcheck = get_seconds();
458 cache_flush();
459 detail->flush_time = 1;
460}
Trond Myklebust24c37672008-12-23 16:30:12 -0500461EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463
464/*
465 * Deferral and Revisiting of Requests.
466 *
467 * If a cache lookup finds a pending entry, we
468 * need to defer the request and revisit it later.
469 * All deferred requests are stored in a hash table,
470 * indexed by "struct cache_head *".
471 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800472 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * deferred form, which must contain a
474 * 'struct cache_deferred_req'
475 * This cache_deferred_req contains a method to allow
476 * it to be revisited when cache info is available
477 */
478
479#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
480#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
481
482#define DFR_MAX 300 /* ??? */
483
484static DEFINE_SPINLOCK(cache_defer_lock);
485static LIST_HEAD(cache_defer_list);
486static struct list_head cache_defer_hash[DFR_HASHSIZE];
487static int cache_defer_cnt;
488
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800489static int cache_defer_req(struct cache_req *req, struct cache_head *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct cache_deferred_req *dreq;
492 int hash = DFR_HASH(item);
493
J.Bruce Fields01f3bd12006-12-13 00:35:26 -0800494 if (cache_defer_cnt >= DFR_MAX) {
495 /* too much in the cache, randomly drop this one,
496 * or continue and drop the oldest below
497 */
498 if (net_random()&1)
499 return -ETIMEDOUT;
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 dreq = req->defer(req);
502 if (dreq == NULL)
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800503 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 spin_lock(&cache_defer_lock);
508
509 list_add(&dreq->recent, &cache_defer_list);
510
511 if (cache_defer_hash[hash].next == NULL)
512 INIT_LIST_HEAD(&cache_defer_hash[hash]);
513 list_add(&dreq->hash, &cache_defer_hash[hash]);
514
515 /* it is in, now maybe clean up */
516 dreq = NULL;
517 if (++cache_defer_cnt > DFR_MAX) {
J.Bruce Fields01f3bd12006-12-13 00:35:26 -0800518 dreq = list_entry(cache_defer_list.prev,
519 struct cache_deferred_req, recent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 list_del(&dreq->recent);
521 list_del(&dreq->hash);
522 cache_defer_cnt--;
523 }
524 spin_unlock(&cache_defer_lock);
525
526 if (dreq) {
527 /* there was one too many */
528 dreq->revisit(dreq, 1);
529 }
NeilBrown4013ede2006-03-27 01:15:07 -0800530 if (!test_bit(CACHE_PENDING, &item->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* must have just been validated... */
532 cache_revisit_request(item);
533 }
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537static void cache_revisit_request(struct cache_head *item)
538{
539 struct cache_deferred_req *dreq;
540 struct list_head pending;
541
542 struct list_head *lp;
543 int hash = DFR_HASH(item);
544
545 INIT_LIST_HEAD(&pending);
546 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 lp = cache_defer_hash[hash].next;
549 if (lp) {
550 while (lp != &cache_defer_hash[hash]) {
551 dreq = list_entry(lp, struct cache_deferred_req, hash);
552 lp = lp->next;
553 if (dreq->item == item) {
554 list_del(&dreq->hash);
555 list_move(&dreq->recent, &pending);
556 cache_defer_cnt--;
557 }
558 }
559 }
560 spin_unlock(&cache_defer_lock);
561
562 while (!list_empty(&pending)) {
563 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
564 list_del_init(&dreq->recent);
565 dreq->revisit(dreq, 0);
566 }
567}
568
569void cache_clean_deferred(void *owner)
570{
571 struct cache_deferred_req *dreq, *tmp;
572 struct list_head pending;
573
574
575 INIT_LIST_HEAD(&pending);
576 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
579 if (dreq->owner == owner) {
580 list_del(&dreq->hash);
581 list_move(&dreq->recent, &pending);
582 cache_defer_cnt--;
583 }
584 }
585 spin_unlock(&cache_defer_lock);
586
587 while (!list_empty(&pending)) {
588 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
589 list_del_init(&dreq->recent);
590 dreq->revisit(dreq, 1);
591 }
592}
593
594/*
595 * communicate with user-space
596 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500597 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
598 * On read, you get a full request, or block.
599 * On write, an update request is processed.
600 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800602 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500603 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * to the end and may wakeup and preceding readers.
605 * New readers are added to the head. If, on read, an item is found with
606 * CACHE_UPCALLING clear, we free it from the list.
607 *
608 */
609
610static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800611static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613struct cache_queue {
614 struct list_head list;
615 int reader; /* if 0, then request */
616};
617struct cache_request {
618 struct cache_queue q;
619 struct cache_head *item;
620 char * buf;
621 int len;
622 int readers;
623};
624struct cache_reader {
625 struct cache_queue q;
626 int offset; /* if non-0, we have a refcnt on next request */
627};
628
Trond Myklebust173912a2009-08-09 15:14:29 -0400629static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
630 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct cache_reader *rp = filp->private_data;
633 struct cache_request *rq;
Trond Myklebustda770052009-08-09 15:14:28 -0400634 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int err;
636
637 if (count == 0)
638 return 0;
639
Trond Myklebustda770052009-08-09 15:14:28 -0400640 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * readers on this file */
642 again:
643 spin_lock(&queue_lock);
644 /* need to find next request */
645 while (rp->q.list.next != &cd->queue &&
646 list_entry(rp->q.list.next, struct cache_queue, list)
647 ->reader) {
648 struct list_head *next = rp->q.list.next;
649 list_move(&rp->q.list, next);
650 }
651 if (rp->q.list.next == &cd->queue) {
652 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400653 mutex_unlock(&inode->i_mutex);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800654 BUG_ON(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 0;
656 }
657 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800658 BUG_ON(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (rp->offset == 0)
660 rq->readers++;
661 spin_unlock(&queue_lock);
662
663 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
664 err = -EAGAIN;
665 spin_lock(&queue_lock);
666 list_move(&rp->q.list, &rq->q.list);
667 spin_unlock(&queue_lock);
668 } else {
669 if (rp->offset + count > rq->len)
670 count = rq->len - rp->offset;
671 err = -EFAULT;
672 if (copy_to_user(buf, rq->buf + rp->offset, count))
673 goto out;
674 rp->offset += count;
675 if (rp->offset >= rq->len) {
676 rp->offset = 0;
677 spin_lock(&queue_lock);
678 list_move(&rp->q.list, &rq->q.list);
679 spin_unlock(&queue_lock);
680 }
681 err = 0;
682 }
683 out:
684 if (rp->offset == 0) {
685 /* need to release rq */
686 spin_lock(&queue_lock);
687 rq->readers--;
688 if (rq->readers == 0 &&
689 !test_bit(CACHE_PENDING, &rq->item->flags)) {
690 list_del(&rq->q.list);
691 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800692 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 kfree(rq->buf);
694 kfree(rq);
695 } else
696 spin_unlock(&queue_lock);
697 }
698 if (err == -EAGAIN)
699 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400700 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return err ? err : count;
702}
703
Trond Myklebustda770052009-08-09 15:14:28 -0400704static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
705 size_t count, struct cache_detail *cd)
706{
707 ssize_t ret;
708
709 if (copy_from_user(kaddr, buf, count))
710 return -EFAULT;
711 kaddr[count] = '\0';
712 ret = cd->cache_parse(cd, kaddr, count);
713 if (!ret)
714 ret = count;
715 return ret;
716}
717
718static ssize_t cache_slow_downcall(const char __user *buf,
719 size_t count, struct cache_detail *cd)
720{
721 static char write_buf[8192]; /* protected by queue_io_mutex */
722 ssize_t ret = -EINVAL;
723
724 if (count >= sizeof(write_buf))
725 goto out;
726 mutex_lock(&queue_io_mutex);
727 ret = cache_do_downcall(write_buf, buf, count, cd);
728 mutex_unlock(&queue_io_mutex);
729out:
730 return ret;
731}
732
733static ssize_t cache_downcall(struct address_space *mapping,
734 const char __user *buf,
735 size_t count, struct cache_detail *cd)
736{
737 struct page *page;
738 char *kaddr;
739 ssize_t ret = -ENOMEM;
740
741 if (count >= PAGE_CACHE_SIZE)
742 goto out_slow;
743
744 page = find_or_create_page(mapping, 0, GFP_KERNEL);
745 if (!page)
746 goto out_slow;
747
748 kaddr = kmap(page);
749 ret = cache_do_downcall(kaddr, buf, count, cd);
750 kunmap(page);
751 unlock_page(page);
752 page_cache_release(page);
753 return ret;
754out_slow:
755 return cache_slow_downcall(buf, count, cd);
756}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Trond Myklebust173912a2009-08-09 15:14:29 -0400758static ssize_t cache_write(struct file *filp, const char __user *buf,
759 size_t count, loff_t *ppos,
760 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Trond Myklebustda770052009-08-09 15:14:28 -0400762 struct address_space *mapping = filp->f_mapping;
763 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebustda770052009-08-09 15:14:28 -0400764 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Trond Myklebustda770052009-08-09 15:14:28 -0400766 if (!cd->cache_parse)
767 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Trond Myklebustda770052009-08-09 15:14:28 -0400769 mutex_lock(&inode->i_mutex);
770 ret = cache_downcall(mapping, buf, count, cd);
771 mutex_unlock(&inode->i_mutex);
772out:
773 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
777
Trond Myklebust173912a2009-08-09 15:14:29 -0400778static unsigned int cache_poll(struct file *filp, poll_table *wait,
779 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 unsigned int mask;
782 struct cache_reader *rp = filp->private_data;
783 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 poll_wait(filp, &queue_wait, wait);
786
787 /* alway allow write */
788 mask = POLL_OUT | POLLWRNORM;
789
790 if (!rp)
791 return mask;
792
793 spin_lock(&queue_lock);
794
795 for (cq= &rp->q; &cq->list != &cd->queue;
796 cq = list_entry(cq->list.next, struct cache_queue, list))
797 if (!cq->reader) {
798 mask |= POLLIN | POLLRDNORM;
799 break;
800 }
801 spin_unlock(&queue_lock);
802 return mask;
803}
804
Trond Myklebust173912a2009-08-09 15:14:29 -0400805static int cache_ioctl(struct inode *ino, struct file *filp,
806 unsigned int cmd, unsigned long arg,
807 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 int len = 0;
810 struct cache_reader *rp = filp->private_data;
811 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (cmd != FIONREAD || !rp)
814 return -EINVAL;
815
816 spin_lock(&queue_lock);
817
818 /* only find the length remaining in current request,
819 * or the length of the next request
820 */
821 for (cq= &rp->q; &cq->list != &cd->queue;
822 cq = list_entry(cq->list.next, struct cache_queue, list))
823 if (!cq->reader) {
824 struct cache_request *cr =
825 container_of(cq, struct cache_request, q);
826 len = cr->len - rp->offset;
827 break;
828 }
829 spin_unlock(&queue_lock);
830
831 return put_user(len, (int __user *)arg);
832}
833
Trond Myklebust173912a2009-08-09 15:14:29 -0400834static int cache_open(struct inode *inode, struct file *filp,
835 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 struct cache_reader *rp = NULL;
838
839 nonseekable_open(inode, filp);
840 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
842 if (!rp)
843 return -ENOMEM;
844 rp->offset = 0;
845 rp->q.reader = 1;
846 atomic_inc(&cd->readers);
847 spin_lock(&queue_lock);
848 list_add(&rp->q.list, &cd->queue);
849 spin_unlock(&queue_lock);
850 }
851 filp->private_data = rp;
852 return 0;
853}
854
Trond Myklebust173912a2009-08-09 15:14:29 -0400855static int cache_release(struct inode *inode, struct file *filp,
856 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 if (rp) {
861 spin_lock(&queue_lock);
862 if (rp->offset) {
863 struct cache_queue *cq;
864 for (cq= &rp->q; &cq->list != &cd->queue;
865 cq = list_entry(cq->list.next, struct cache_queue, list))
866 if (!cq->reader) {
867 container_of(cq, struct cache_request, q)
868 ->readers--;
869 break;
870 }
871 rp->offset = 0;
872 }
873 list_del(&rp->q.list);
874 spin_unlock(&queue_lock);
875
876 filp->private_data = NULL;
877 kfree(rp);
878
879 cd->last_close = get_seconds();
880 atomic_dec(&cd->readers);
881 }
882 return 0;
883}
884
885
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887static void queue_loose(struct cache_detail *detail, struct cache_head *ch)
888{
889 struct cache_queue *cq;
890 spin_lock(&queue_lock);
891 list_for_each_entry(cq, &detail->queue, list)
892 if (!cq->reader) {
893 struct cache_request *cr = container_of(cq, struct cache_request, q);
894 if (cr->item != ch)
895 continue;
896 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -0800897 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 list_del(&cr->q.list);
899 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800900 cache_put(cr->item, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 kfree(cr->buf);
902 kfree(cr);
903 return;
904 }
905 spin_unlock(&queue_lock);
906}
907
908/*
909 * Support routines for text-based upcalls.
910 * Fields are separated by spaces.
911 * Fields are either mangled to quote space tab newline slosh with slosh
912 * or a hexified with a leading \x
913 * Record is terminated with newline.
914 *
915 */
916
917void qword_add(char **bpp, int *lp, char *str)
918{
919 char *bp = *bpp;
920 int len = *lp;
921 char c;
922
923 if (len < 0) return;
924
925 while ((c=*str++) && len)
926 switch(c) {
927 case ' ':
928 case '\t':
929 case '\n':
930 case '\\':
931 if (len >= 4) {
932 *bp++ = '\\';
933 *bp++ = '0' + ((c & 0300)>>6);
934 *bp++ = '0' + ((c & 0070)>>3);
935 *bp++ = '0' + ((c & 0007)>>0);
936 }
937 len -= 4;
938 break;
939 default:
940 *bp++ = c;
941 len--;
942 }
943 if (c || len <1) len = -1;
944 else {
945 *bp++ = ' ';
946 len--;
947 }
948 *bpp = bp;
949 *lp = len;
950}
Trond Myklebust24c37672008-12-23 16:30:12 -0500951EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953void qword_addhex(char **bpp, int *lp, char *buf, int blen)
954{
955 char *bp = *bpp;
956 int len = *lp;
957
958 if (len < 0) return;
959
960 if (len > 2) {
961 *bp++ = '\\';
962 *bp++ = 'x';
963 len -= 2;
964 while (blen && len >= 2) {
965 unsigned char c = *buf++;
966 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
967 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
968 len -= 2;
969 blen--;
970 }
971 }
972 if (blen || len<1) len = -1;
973 else {
974 *bp++ = ' ';
975 len--;
976 }
977 *bpp = bp;
978 *lp = len;
979}
Trond Myklebust24c37672008-12-23 16:30:12 -0500980EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982static void warn_no_listener(struct cache_detail *detail)
983{
984 if (detail->last_warn != detail->last_close) {
985 detail->last_warn = detail->last_close;
986 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -0400987 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989}
990
991/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400992 * register an upcall request to user-space and queue it up for read() by the
993 * upcall daemon.
994 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 * Each request is at most one page long.
996 */
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400997int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
998 void (*cache_request)(struct cache_detail *,
999 struct cache_head *,
1000 char **,
1001 int *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003
1004 char *buf;
1005 struct cache_request *crq;
1006 char *bp;
1007 int len;
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (atomic_read(&detail->readers) == 0 &&
1010 detail->last_close < get_seconds() - 30) {
1011 warn_no_listener(detail);
1012 return -EINVAL;
1013 }
1014
1015 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1016 if (!buf)
1017 return -EAGAIN;
1018
1019 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1020 if (!crq) {
1021 kfree(buf);
1022 return -EAGAIN;
1023 }
1024
1025 bp = buf; len = PAGE_SIZE;
1026
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001027 cache_request(detail, h, &bp, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (len < 0) {
1030 kfree(buf);
1031 kfree(crq);
1032 return -EAGAIN;
1033 }
1034 crq->q.reader = 0;
1035 crq->item = cache_get(h);
1036 crq->buf = buf;
1037 crq->len = PAGE_SIZE - len;
1038 crq->readers = 0;
1039 spin_lock(&queue_lock);
1040 list_add_tail(&crq->q.list, &detail->queue);
1041 spin_unlock(&queue_lock);
1042 wake_up(&queue_wait);
1043 return 0;
1044}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001045EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047/*
1048 * parse a message from user-space and pass it
1049 * to an appropriate cache
1050 * Messages are, like requests, separated into fields by
1051 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1052 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001053 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * reply cachename expiry key ... content....
1055 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001056 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
1058
1059#define isodigit(c) (isdigit(c) && c <= '7')
1060int qword_get(char **bpp, char *dest, int bufsize)
1061{
1062 /* return bytes copied, or -1 on error */
1063 char *bp = *bpp;
1064 int len = 0;
1065
1066 while (*bp == ' ') bp++;
1067
1068 if (bp[0] == '\\' && bp[1] == 'x') {
1069 /* HEX STRING */
1070 bp += 2;
1071 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1072 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1073 bp++;
1074 byte <<= 4;
1075 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1076 *dest++ = byte;
1077 bp++;
1078 len++;
1079 }
1080 } else {
1081 /* text with \nnn octal quoting */
1082 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1083 if (*bp == '\\' &&
1084 isodigit(bp[1]) && (bp[1] <= '3') &&
1085 isodigit(bp[2]) &&
1086 isodigit(bp[3])) {
1087 int byte = (*++bp -'0');
1088 bp++;
1089 byte = (byte << 3) | (*bp++ - '0');
1090 byte = (byte << 3) | (*bp++ - '0');
1091 *dest++ = byte;
1092 len++;
1093 } else {
1094 *dest++ = *bp++;
1095 len++;
1096 }
1097 }
1098 }
1099
1100 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1101 return -1;
1102 while (*bp == ' ') bp++;
1103 *bpp = bp;
1104 *dest = '\0';
1105 return len;
1106}
Trond Myklebust24c37672008-12-23 16:30:12 -05001107EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109
1110/*
1111 * support /proc/sunrpc/cache/$CACHENAME/content
1112 * as a seqfile.
1113 * We call ->cache_show passing NULL for the item to
1114 * get a header, then pass each real item in the cache
1115 */
1116
1117struct handle {
1118 struct cache_detail *cd;
1119};
1120
1121static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001122 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 loff_t n = *pos;
1125 unsigned hash, entry;
1126 struct cache_head *ch;
1127 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 read_lock(&cd->hash_lock);
1131 if (!n--)
1132 return SEQ_START_TOKEN;
1133 hash = n >> 32;
1134 entry = n & ((1LL<<32) - 1);
1135
1136 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1137 if (!entry--)
1138 return ch;
1139 n &= ~((1LL<<32) - 1);
1140 do {
1141 hash++;
1142 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001143 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 cd->hash_table[hash]==NULL);
1145 if (hash >= cd->hash_size)
1146 return NULL;
1147 *pos = n+1;
1148 return cd->hash_table[hash];
1149}
1150
1151static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1152{
1153 struct cache_head *ch = p;
1154 int hash = (*pos >> 32);
1155 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1156
1157 if (p == SEQ_START_TOKEN)
1158 hash = 0;
1159 else if (ch->next == NULL) {
1160 hash++;
1161 *pos += 1LL<<32;
1162 } else {
1163 ++*pos;
1164 return ch->next;
1165 }
1166 *pos &= ~((1LL<<32) - 1);
1167 while (hash < cd->hash_size &&
1168 cd->hash_table[hash] == NULL) {
1169 hash++;
1170 *pos += 1LL<<32;
1171 }
1172 if (hash >= cd->hash_size)
1173 return NULL;
1174 ++*pos;
1175 return cd->hash_table[hash];
1176}
1177
1178static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001179 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1182 read_unlock(&cd->hash_lock);
1183}
1184
1185static int c_show(struct seq_file *m, void *p)
1186{
1187 struct cache_head *cp = p;
1188 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1189
1190 if (p == SEQ_START_TOKEN)
1191 return cd->cache_show(m, cd, NULL);
1192
1193 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001194 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownbaab9352006-03-27 01:15:09 -08001195 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 cache_get(cp);
1197 if (cache_check(cd, cp, NULL))
1198 /* cache_check does a cache_put on failure */
1199 seq_printf(m, "# ");
1200 else
1201 cache_put(cp, cd);
1202
1203 return cd->cache_show(m, cd, cp);
1204}
1205
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001206static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 .start = c_start,
1208 .next = c_next,
1209 .stop = c_stop,
1210 .show = c_show,
1211};
1212
Trond Myklebust173912a2009-08-09 15:14:29 -04001213static int content_open(struct inode *inode, struct file *file,
1214 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 struct handle *han;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Pavel Emelyanovec931032007-10-10 02:31:07 -07001218 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (han == NULL)
1220 return -ENOMEM;
1221
1222 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001227 size_t count, loff_t *ppos,
1228 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 char tbuf[20];
1231 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001232 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 sprintf(tbuf, "%lu\n", cd->flush_time);
1235 len = strlen(tbuf);
1236 if (p >= len)
1237 return 0;
1238 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001239 if (len > count)
1240 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001242 return -EFAULT;
1243 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return len;
1245}
1246
Trond Myklebust173912a2009-08-09 15:14:29 -04001247static ssize_t write_flush(struct file *file, const char __user *buf,
1248 size_t count, loff_t *ppos,
1249 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 char tbuf[20];
1252 char *ep;
1253 long flushtime;
1254 if (*ppos || count > sizeof(tbuf)-1)
1255 return -EINVAL;
1256 if (copy_from_user(tbuf, buf, count))
1257 return -EFAULT;
1258 tbuf[count] = 0;
1259 flushtime = simple_strtoul(tbuf, &ep, 0);
1260 if (*ep && *ep != '\n')
1261 return -EINVAL;
1262
1263 cd->flush_time = flushtime;
1264 cd->nextcheck = get_seconds();
1265 cache_flush();
1266
1267 *ppos += count;
1268 return count;
1269}
1270
Trond Myklebust173912a2009-08-09 15:14:29 -04001271static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1272 size_t count, loff_t *ppos)
1273{
1274 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1275
1276 return cache_read(filp, buf, count, ppos, cd);
1277}
1278
1279static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1280 size_t count, loff_t *ppos)
1281{
1282 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1283
1284 return cache_write(filp, buf, count, ppos, cd);
1285}
1286
1287static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1288{
1289 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1290
1291 return cache_poll(filp, wait, cd);
1292}
1293
1294static int cache_ioctl_procfs(struct inode *inode, struct file *filp,
1295 unsigned int cmd, unsigned long arg)
1296{
1297 struct cache_detail *cd = PDE(inode)->data;
1298
1299 return cache_ioctl(inode, filp, cmd, arg, cd);
1300}
1301
1302static int cache_open_procfs(struct inode *inode, struct file *filp)
1303{
1304 struct cache_detail *cd = PDE(inode)->data;
1305
1306 return cache_open(inode, filp, cd);
1307}
1308
1309static int cache_release_procfs(struct inode *inode, struct file *filp)
1310{
1311 struct cache_detail *cd = PDE(inode)->data;
1312
1313 return cache_release(inode, filp, cd);
1314}
1315
1316static const struct file_operations cache_file_operations_procfs = {
1317 .owner = THIS_MODULE,
1318 .llseek = no_llseek,
1319 .read = cache_read_procfs,
1320 .write = cache_write_procfs,
1321 .poll = cache_poll_procfs,
1322 .ioctl = cache_ioctl_procfs, /* for FIONREAD */
1323 .open = cache_open_procfs,
1324 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325};
Trond Myklebust173912a2009-08-09 15:14:29 -04001326
1327static int content_open_procfs(struct inode *inode, struct file *filp)
1328{
1329 struct cache_detail *cd = PDE(inode)->data;
1330
1331 return content_open(inode, filp, cd);
1332}
1333
1334static const struct file_operations content_file_operations_procfs = {
1335 .open = content_open_procfs,
1336 .read = seq_read,
1337 .llseek = seq_lseek,
1338 .release = seq_release_private,
1339};
1340
1341static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1342 size_t count, loff_t *ppos)
1343{
1344 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1345
1346 return read_flush(filp, buf, count, ppos, cd);
1347}
1348
1349static ssize_t write_flush_procfs(struct file *filp,
1350 const char __user *buf,
1351 size_t count, loff_t *ppos)
1352{
1353 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1354
1355 return write_flush(filp, buf, count, ppos, cd);
1356}
1357
1358static const struct file_operations cache_flush_operations_procfs = {
1359 .open = nonseekable_open,
1360 .read = read_flush_procfs,
1361 .write = write_flush_procfs,
1362};
1363
1364static void remove_cache_proc_entries(struct cache_detail *cd)
1365{
1366 if (cd->u.procfs.proc_ent == NULL)
1367 return;
1368 if (cd->u.procfs.flush_ent)
1369 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1370 if (cd->u.procfs.channel_ent)
1371 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1372 if (cd->u.procfs.content_ent)
1373 remove_proc_entry("content", cd->u.procfs.proc_ent);
1374 cd->u.procfs.proc_ent = NULL;
1375 remove_proc_entry(cd->name, proc_net_rpc);
1376}
1377
1378#ifdef CONFIG_PROC_FS
1379static int create_cache_proc_entries(struct cache_detail *cd)
1380{
1381 struct proc_dir_entry *p;
1382
1383 cd->u.procfs.proc_ent = proc_mkdir(cd->name, proc_net_rpc);
1384 if (cd->u.procfs.proc_ent == NULL)
1385 goto out_nomem;
1386 cd->u.procfs.channel_ent = NULL;
1387 cd->u.procfs.content_ent = NULL;
1388
1389 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1390 cd->u.procfs.proc_ent,
1391 &cache_flush_operations_procfs, cd);
1392 cd->u.procfs.flush_ent = p;
1393 if (p == NULL)
1394 goto out_nomem;
1395
1396 if (cd->cache_upcall || cd->cache_parse) {
1397 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1398 cd->u.procfs.proc_ent,
1399 &cache_file_operations_procfs, cd);
1400 cd->u.procfs.channel_ent = p;
1401 if (p == NULL)
1402 goto out_nomem;
1403 }
1404 if (cd->cache_show) {
1405 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1406 cd->u.procfs.proc_ent,
1407 &content_file_operations_procfs, cd);
1408 cd->u.procfs.content_ent = p;
1409 if (p == NULL)
1410 goto out_nomem;
1411 }
1412 return 0;
1413out_nomem:
1414 remove_cache_proc_entries(cd);
1415 return -ENOMEM;
1416}
1417#else /* CONFIG_PROC_FS */
1418static int create_cache_proc_entries(struct cache_detail *cd)
1419{
1420 return 0;
1421}
1422#endif
1423
1424int cache_register(struct cache_detail *cd)
1425{
1426 int ret;
1427
1428 sunrpc_init_cache_detail(cd);
1429 ret = create_cache_proc_entries(cd);
1430 if (ret)
1431 sunrpc_destroy_cache_detail(cd);
1432 return ret;
1433}
1434EXPORT_SYMBOL_GPL(cache_register);
1435
1436void cache_unregister(struct cache_detail *cd)
1437{
1438 remove_cache_proc_entries(cd);
1439 sunrpc_destroy_cache_detail(cd);
1440}
1441EXPORT_SYMBOL_GPL(cache_unregister);
Trond Myklebust8854e822009-08-09 15:14:30 -04001442
1443static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1444 size_t count, loff_t *ppos)
1445{
1446 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1447
1448 return cache_read(filp, buf, count, ppos, cd);
1449}
1450
1451static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1452 size_t count, loff_t *ppos)
1453{
1454 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1455
1456 return cache_write(filp, buf, count, ppos, cd);
1457}
1458
1459static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1460{
1461 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1462
1463 return cache_poll(filp, wait, cd);
1464}
1465
1466static int cache_ioctl_pipefs(struct inode *inode, struct file *filp,
1467 unsigned int cmd, unsigned long arg)
1468{
1469 struct cache_detail *cd = RPC_I(inode)->private;
1470
1471 return cache_ioctl(inode, filp, cmd, arg, cd);
1472}
1473
1474static int cache_open_pipefs(struct inode *inode, struct file *filp)
1475{
1476 struct cache_detail *cd = RPC_I(inode)->private;
1477
1478 return cache_open(inode, filp, cd);
1479}
1480
1481static int cache_release_pipefs(struct inode *inode, struct file *filp)
1482{
1483 struct cache_detail *cd = RPC_I(inode)->private;
1484
1485 return cache_release(inode, filp, cd);
1486}
1487
1488const struct file_operations cache_file_operations_pipefs = {
1489 .owner = THIS_MODULE,
1490 .llseek = no_llseek,
1491 .read = cache_read_pipefs,
1492 .write = cache_write_pipefs,
1493 .poll = cache_poll_pipefs,
1494 .ioctl = cache_ioctl_pipefs, /* for FIONREAD */
1495 .open = cache_open_pipefs,
1496 .release = cache_release_pipefs,
1497};
1498
1499static int content_open_pipefs(struct inode *inode, struct file *filp)
1500{
1501 struct cache_detail *cd = RPC_I(inode)->private;
1502
1503 return content_open(inode, filp, cd);
1504}
1505
1506const struct file_operations content_file_operations_pipefs = {
1507 .open = content_open_pipefs,
1508 .read = seq_read,
1509 .llseek = seq_lseek,
1510 .release = seq_release_private,
1511};
1512
1513static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1514 size_t count, loff_t *ppos)
1515{
1516 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1517
1518 return read_flush(filp, buf, count, ppos, cd);
1519}
1520
1521static ssize_t write_flush_pipefs(struct file *filp,
1522 const char __user *buf,
1523 size_t count, loff_t *ppos)
1524{
1525 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1526
1527 return write_flush(filp, buf, count, ppos, cd);
1528}
1529
1530const struct file_operations cache_flush_operations_pipefs = {
1531 .open = nonseekable_open,
1532 .read = read_flush_pipefs,
1533 .write = write_flush_pipefs,
1534};
1535
1536int sunrpc_cache_register_pipefs(struct dentry *parent,
1537 const char *name, mode_t umode,
1538 struct cache_detail *cd)
1539{
1540 struct qstr q;
1541 struct dentry *dir;
1542 int ret = 0;
1543
1544 sunrpc_init_cache_detail(cd);
1545 q.name = name;
1546 q.len = strlen(name);
1547 q.hash = full_name_hash(q.name, q.len);
1548 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1549 if (!IS_ERR(dir))
1550 cd->u.pipefs.dir = dir;
1551 else {
1552 sunrpc_destroy_cache_detail(cd);
1553 ret = PTR_ERR(dir);
1554 }
1555 return ret;
1556}
1557EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1558
1559void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1560{
1561 rpc_remove_cache_dir(cd->u.pipefs.dir);
1562 cd->u.pipefs.dir = NULL;
1563 sunrpc_destroy_cache_detail(cd);
1564}
1565EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1566