blob: 58de76c8540c65dac38999ea4c9a5c2af5303f92 [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>
Frederic Weisbecker99df95a2010-04-13 22:46:36 +020031#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/ioctls.h>
33#include <linux/sunrpc/types.h>
34#include <linux/sunrpc/cache.h>
35#include <linux/sunrpc/stats.h>
Trond Myklebust8854e822009-08-09 15:14:30 -040036#include <linux/sunrpc/rpc_pipe_fs.h>
Frederic Weisbecker9918ff22010-05-19 15:08:17 +020037#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define RPCDBG_FACILITY RPCDBG_CACHE
40
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -080041static int cache_defer_req(struct cache_req *req, struct cache_head *item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static void cache_revisit_request(struct cache_head *item);
43
Adrian Bunk74cae612006-03-27 01:15:10 -080044static void cache_init(struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 time_t now = get_seconds();
47 h->next = NULL;
48 h->flags = 0;
NeilBrownbaab9352006-03-27 01:15:09 -080049 kref_init(&h->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 h->expiry_time = now + CACHE_NEW_EXPIRY;
51 h->last_refresh = now;
52}
53
NeilBrown2f50d8b2010-02-03 17:31:31 +110054static inline int cache_is_expired(struct cache_detail *detail, struct cache_head *h)
55{
56 return (h->expiry_time < get_seconds()) ||
57 (detail->flush_time > h->last_refresh);
58}
59
NeilBrown15a5f6b2006-03-27 01:15:02 -080060struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
61 struct cache_head *key, int hash)
62{
63 struct cache_head **head, **hp;
NeilBrownd202cce2010-02-03 17:31:31 +110064 struct cache_head *new = NULL, *freeme = NULL;
NeilBrown15a5f6b2006-03-27 01:15:02 -080065
66 head = &detail->hash_table[hash];
67
68 read_lock(&detail->hash_lock);
69
70 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
71 struct cache_head *tmp = *hp;
72 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110073 if (cache_is_expired(detail, tmp))
74 /* This entry is expired, we will discard it. */
75 break;
NeilBrown15a5f6b2006-03-27 01:15:02 -080076 cache_get(tmp);
77 read_unlock(&detail->hash_lock);
78 return tmp;
79 }
80 }
81 read_unlock(&detail->hash_lock);
82 /* Didn't find anything, insert an empty entry */
83
84 new = detail->alloc();
85 if (!new)
86 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070087 /* must fully initialise 'new', else
88 * we might get lose if we need to
89 * cache_put it soon.
90 */
NeilBrown15a5f6b2006-03-27 01:15:02 -080091 cache_init(new);
Neil Brown2f349312006-08-05 12:14:29 -070092 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080093
94 write_lock(&detail->hash_lock);
95
96 /* check if entry appeared while we slept */
97 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
98 struct cache_head *tmp = *hp;
99 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +1100100 if (cache_is_expired(detail, tmp)) {
101 *hp = tmp->next;
102 tmp->next = NULL;
103 detail->entries --;
104 freeme = tmp;
105 break;
106 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800107 cache_get(tmp);
108 write_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800109 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800110 return tmp;
111 }
112 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800113 new->next = *head;
114 *head = new;
115 detail->entries++;
116 cache_get(new);
117 write_unlock(&detail->hash_lock);
118
NeilBrownd202cce2010-02-03 17:31:31 +1100119 if (freeme)
120 cache_put(freeme, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800121 return new;
122}
Trond Myklebust24c37672008-12-23 16:30:12 -0500123EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800124
NeilBrownebd0cb12006-03-27 01:15:08 -0800125
NeilBrownf866a812009-08-04 15:22:38 +1000126static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800127
NeilBrown908329f2009-09-09 16:32:54 +1000128static void cache_fresh_locked(struct cache_head *head, time_t expiry)
NeilBrownebd0cb12006-03-27 01:15:08 -0800129{
130 head->expiry_time = expiry;
131 head->last_refresh = get_seconds();
NeilBrown908329f2009-09-09 16:32:54 +1000132 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800133}
134
135static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000136 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800137{
NeilBrownebd0cb12006-03-27 01:15:08 -0800138 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
139 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000140 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800141 }
142}
143
NeilBrown15a5f6b2006-03-27 01:15:02 -0800144struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
145 struct cache_head *new, struct cache_head *old, int hash)
146{
147 /* The 'old' entry is to be replaced by 'new'.
148 * If 'old' is not VALID, we update it directly,
149 * otherwise we need to replace it
150 */
151 struct cache_head **head;
152 struct cache_head *tmp;
153
154 if (!test_bit(CACHE_VALID, &old->flags)) {
155 write_lock(&detail->hash_lock);
156 if (!test_bit(CACHE_VALID, &old->flags)) {
157 if (test_bit(CACHE_NEGATIVE, &new->flags))
158 set_bit(CACHE_NEGATIVE, &old->flags);
159 else
160 detail->update(old, new);
NeilBrown908329f2009-09-09 16:32:54 +1000161 cache_fresh_locked(old, new->expiry_time);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800162 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000163 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800164 return old;
165 }
166 write_unlock(&detail->hash_lock);
167 }
168 /* We need to insert a new entry */
169 tmp = detail->alloc();
170 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800171 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800172 return NULL;
173 }
174 cache_init(tmp);
175 detail->init(tmp, old);
176 head = &detail->hash_table[hash];
177
178 write_lock(&detail->hash_lock);
179 if (test_bit(CACHE_NEGATIVE, &new->flags))
180 set_bit(CACHE_NEGATIVE, &tmp->flags);
181 else
182 detail->update(tmp, new);
183 tmp->next = *head;
184 *head = tmp;
NeilBrownf2d39582006-05-22 22:35:25 -0700185 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800186 cache_get(tmp);
NeilBrown908329f2009-09-09 16:32:54 +1000187 cache_fresh_locked(tmp, new->expiry_time);
NeilBrownebd0cb12006-03-27 01:15:08 -0800188 cache_fresh_locked(old, 0);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800189 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000190 cache_fresh_unlocked(tmp, detail);
191 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800192 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800193 return tmp;
194}
Trond Myklebust24c37672008-12-23 16:30:12 -0500195EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400197static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
198{
199 if (!cd->cache_upcall)
200 return -EINVAL;
201 return cd->cache_upcall(cd, h);
202}
NeilBrown989a19b2009-08-04 15:22:38 +1000203
204static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h)
205{
NeilBrownd202cce2010-02-03 17:31:31 +1100206 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000207 return -EAGAIN;
208 else {
209 /* entry is valid */
210 if (test_bit(CACHE_NEGATIVE, &h->flags))
211 return -ENOENT;
212 else
213 return 0;
214 }
215}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
218 * This is the generic cache management routine for all
219 * the authentication caches.
220 * It checks the currency of a cache item and will (later)
221 * initiate an upcall to fill it if needed.
222 *
223 *
224 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000225 * -EAGAIN if upcall is pending and request has been queued
226 * -ETIMEDOUT if upcall failed or request could not be queue or
227 * upcall completed but item is still invalid (implying that
228 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * -ENOENT if cache entry was negative
230 */
231int cache_check(struct cache_detail *detail,
232 struct cache_head *h, struct cache_req *rqstp)
233{
234 int rv;
235 long refresh_age, age;
236
237 /* First decide return status as best we can */
NeilBrown989a19b2009-08-04 15:22:38 +1000238 rv = cache_is_valid(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* now see if we want to start an upcall */
241 refresh_age = (h->expiry_time - h->last_refresh);
242 age = get_seconds() - h->last_refresh;
243
244 if (rqstp == NULL) {
245 if (rv == -EAGAIN)
246 rv = -ENOENT;
247 } else if (rv == -EAGAIN || age > refresh_age/2) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500248 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
249 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
251 switch (cache_make_upcall(detail, h)) {
252 case -EINVAL:
253 clear_bit(CACHE_PENDING, &h->flags);
NeilBrown5c4d2632009-08-04 15:22:38 +1000254 cache_revisit_request(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (rv == -EAGAIN) {
256 set_bit(CACHE_NEGATIVE, &h->flags);
NeilBrown908329f2009-09-09 16:32:54 +1000257 cache_fresh_locked(h, get_seconds()+CACHE_NEW_EXPIRY);
258 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 rv = -ENOENT;
260 }
261 break;
262
263 case -EAGAIN:
264 clear_bit(CACHE_PENDING, &h->flags);
265 cache_revisit_request(h);
266 break;
267 }
268 }
269 }
270
NeilBrown989a19b2009-08-04 15:22:38 +1000271 if (rv == -EAGAIN) {
NeilBrown9e4c6372009-09-09 16:32:54 +1000272 if (cache_defer_req(rqstp, h) < 0) {
NeilBrown989a19b2009-08-04 15:22:38 +1000273 /* Request is not deferred */
274 rv = cache_is_valid(detail, h);
275 if (rv == -EAGAIN)
276 rv = -ETIMEDOUT;
277 }
278 }
NeilBrown4013ede2006-03-27 01:15:07 -0800279 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800280 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return rv;
282}
Trond Myklebust24c37672008-12-23 16:30:12 -0500283EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/*
286 * caches need to be periodically cleaned.
287 * For this we maintain a list of cache_detail and
288 * a current pointer into that list and into the table
289 * for that entry.
290 *
291 * Each time clean_cache is called it finds the next non-empty entry
292 * in the current table and walks the list in that entry
293 * looking for entries that can be removed.
294 *
295 * An entry gets removed if:
296 * - The expiry is before current time
297 * - The last_refresh time is before the flush_time for that cache
298 *
299 * later we might drop old entries with non-NEVER expiry if that table
300 * is getting 'full' for some definition of 'full'
301 *
302 * The question of "how often to scan a table" is an interesting one
303 * and is answered in part by the use of the "nextcheck" field in the
304 * cache_detail.
305 * When a scan of a table begins, the nextcheck field is set to a time
306 * that is well into the future.
307 * While scanning, if an expiry time is found that is earlier than the
308 * current nextcheck time, nextcheck is set to that expiry time.
309 * If the flush_time is ever set to a time earlier than the nextcheck
310 * time, the nextcheck time is then set to that flush_time.
311 *
312 * A table is then only scanned if the current time is at least
313 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800314 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
316
317static LIST_HEAD(cache_list);
318static DEFINE_SPINLOCK(cache_list_lock);
319static struct cache_detail *current_detail;
320static int current_index;
321
David Howells65f27f32006-11-22 14:55:48 +0000322static void do_cache_clean(struct work_struct *work);
323static DECLARE_DELAYED_WORK(cache_cleaner, do_cache_clean);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400325static void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500326{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 rwlock_init(&cd->hash_lock);
328 INIT_LIST_HEAD(&cd->queue);
329 spin_lock(&cache_list_lock);
330 cd->nextcheck = 0;
331 cd->entries = 0;
332 atomic_set(&cd->readers, 0);
333 cd->last_close = 0;
334 cd->last_warn = -1;
335 list_add(&cd->others, &cache_list);
336 spin_unlock(&cache_list_lock);
337
338 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000339 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400342static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 cache_purge(cd);
345 spin_lock(&cache_list_lock);
346 write_lock(&cd->hash_lock);
347 if (cd->entries || atomic_read(&cd->inuse)) {
348 write_unlock(&cd->hash_lock);
349 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500350 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352 if (current_detail == cd)
353 current_detail = NULL;
354 list_del_init(&cd->others);
355 write_unlock(&cd->hash_lock);
356 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (list_empty(&cache_list)) {
358 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400359 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500361 return;
362out:
363 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366/* clean cache tries to find something to clean
367 * and cleans it.
368 * It returns 1 if it cleaned something,
369 * 0 if it didn't find anything this time
370 * -1 if it fell off the end of the list.
371 */
372static int cache_clean(void)
373{
374 int rv = 0;
375 struct list_head *next;
376
377 spin_lock(&cache_list_lock);
378
379 /* find a suitable table if we don't already have one */
380 while (current_detail == NULL ||
381 current_index >= current_detail->hash_size) {
382 if (current_detail)
383 next = current_detail->others.next;
384 else
385 next = cache_list.next;
386 if (next == &cache_list) {
387 current_detail = NULL;
388 spin_unlock(&cache_list_lock);
389 return -1;
390 }
391 current_detail = list_entry(next, struct cache_detail, others);
392 if (current_detail->nextcheck > get_seconds())
393 current_index = current_detail->hash_size;
394 else {
395 current_index = 0;
396 current_detail->nextcheck = get_seconds()+30*60;
397 }
398 }
399
400 /* find a non-empty bucket in the table */
401 while (current_detail &&
402 current_index < current_detail->hash_size &&
403 current_detail->hash_table[current_index] == NULL)
404 current_index++;
405
406 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (current_detail && current_index < current_detail->hash_size) {
409 struct cache_head *ch, **cp;
410 struct cache_detail *d;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 write_lock(&current_detail->hash_lock);
413
414 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 cp = & current_detail->hash_table[current_index];
NeilBrown3af49742010-02-03 17:31:31 +1100417 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (current_detail->nextcheck > ch->expiry_time)
419 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100420 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 *cp = ch->next;
424 ch->next = NULL;
425 current_detail->entries--;
426 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
NeilBrown3af49742010-02-03 17:31:31 +1100429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 write_unlock(&current_detail->hash_lock);
431 d = current_detail;
432 if (!ch)
433 current_index ++;
434 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000435 if (ch) {
NeilBrown3af49742010-02-03 17:31:31 +1100436 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
437 cache_dequeue(current_detail, ch);
NeilBrown5c4d2632009-08-04 15:22:38 +1000438 cache_revisit_request(ch);
NeilBrownbaab9352006-03-27 01:15:09 -0800439 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 } else
442 spin_unlock(&cache_list_lock);
443
444 return rv;
445}
446
447/*
448 * We want to regularly clean the cache, so we need to schedule some work ...
449 */
David Howells65f27f32006-11-22 14:55:48 +0000450static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 int delay = 5;
453 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700454 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (list_empty(&cache_list))
457 delay = 0;
458
459 if (delay)
460 schedule_delayed_work(&cache_cleaner, delay);
461}
462
463
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800464/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800466 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * be fully cleaned
468 */
469void cache_flush(void)
470{
471 while (cache_clean() != -1)
472 cond_resched();
473 while (cache_clean() != -1)
474 cond_resched();
475}
Trond Myklebust24c37672008-12-23 16:30:12 -0500476EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478void cache_purge(struct cache_detail *detail)
479{
480 detail->flush_time = LONG_MAX;
481 detail->nextcheck = get_seconds();
482 cache_flush();
483 detail->flush_time = 1;
484}
Trond Myklebust24c37672008-12-23 16:30:12 -0500485EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487
488/*
489 * Deferral and Revisiting of Requests.
490 *
491 * If a cache lookup finds a pending entry, we
492 * need to defer the request and revisit it later.
493 * All deferred requests are stored in a hash table,
494 * indexed by "struct cache_head *".
495 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800496 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * deferred form, which must contain a
498 * 'struct cache_deferred_req'
499 * This cache_deferred_req contains a method to allow
500 * it to be revisited when cache info is available
501 */
502
503#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
504#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
505
506#define DFR_MAX 300 /* ??? */
507
508static DEFINE_SPINLOCK(cache_defer_lock);
509static LIST_HEAD(cache_defer_list);
510static struct list_head cache_defer_hash[DFR_HASHSIZE];
511static int cache_defer_cnt;
512
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800513static int cache_defer_req(struct cache_req *req, struct cache_head *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
NeilBrowncd68c3742009-09-09 16:32:54 +1000515 struct cache_deferred_req *dreq, *discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 int hash = DFR_HASH(item);
517
J.Bruce Fields01f3bd12006-12-13 00:35:26 -0800518 if (cache_defer_cnt >= DFR_MAX) {
519 /* too much in the cache, randomly drop this one,
520 * or continue and drop the oldest below
521 */
522 if (net_random()&1)
NeilBrown9e4c6372009-09-09 16:32:54 +1000523 return -ENOMEM;
J.Bruce Fields01f3bd12006-12-13 00:35:26 -0800524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 dreq = req->defer(req);
526 if (dreq == NULL)
NeilBrown9e4c6372009-09-09 16:32:54 +1000527 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 spin_lock(&cache_defer_lock);
532
533 list_add(&dreq->recent, &cache_defer_list);
534
535 if (cache_defer_hash[hash].next == NULL)
536 INIT_LIST_HEAD(&cache_defer_hash[hash]);
537 list_add(&dreq->hash, &cache_defer_hash[hash]);
538
539 /* it is in, now maybe clean up */
NeilBrowncd68c3742009-09-09 16:32:54 +1000540 discard = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (++cache_defer_cnt > DFR_MAX) {
NeilBrowncd68c3742009-09-09 16:32:54 +1000542 discard = list_entry(cache_defer_list.prev,
543 struct cache_deferred_req, recent);
544 list_del_init(&discard->recent);
545 list_del_init(&discard->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 cache_defer_cnt--;
547 }
548 spin_unlock(&cache_defer_lock);
549
NeilBrowncd68c3742009-09-09 16:32:54 +1000550 if (discard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* there was one too many */
NeilBrowncd68c3742009-09-09 16:32:54 +1000552 discard->revisit(discard, 1);
553
NeilBrown4013ede2006-03-27 01:15:07 -0800554 if (!test_bit(CACHE_PENDING, &item->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* must have just been validated... */
556 cache_revisit_request(item);
NeilBrown9e4c6372009-09-09 16:32:54 +1000557 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
NeilBrown9e4c6372009-09-09 16:32:54 +1000559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562static void cache_revisit_request(struct cache_head *item)
563{
564 struct cache_deferred_req *dreq;
565 struct list_head pending;
566
567 struct list_head *lp;
568 int hash = DFR_HASH(item);
569
570 INIT_LIST_HEAD(&pending);
571 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 lp = cache_defer_hash[hash].next;
574 if (lp) {
575 while (lp != &cache_defer_hash[hash]) {
576 dreq = list_entry(lp, struct cache_deferred_req, hash);
577 lp = lp->next;
578 if (dreq->item == item) {
NeilBrown67e73282009-09-09 16:32:54 +1000579 list_del_init(&dreq->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 list_move(&dreq->recent, &pending);
581 cache_defer_cnt--;
582 }
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, 0);
591 }
592}
593
594void cache_clean_deferred(void *owner)
595{
596 struct cache_deferred_req *dreq, *tmp;
597 struct list_head pending;
598
599
600 INIT_LIST_HEAD(&pending);
601 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
604 if (dreq->owner == owner) {
NeilBrown67e73282009-09-09 16:32:54 +1000605 list_del_init(&dreq->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 list_move(&dreq->recent, &pending);
607 cache_defer_cnt--;
608 }
609 }
610 spin_unlock(&cache_defer_lock);
611
612 while (!list_empty(&pending)) {
613 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
614 list_del_init(&dreq->recent);
615 dreq->revisit(dreq, 1);
616 }
617}
618
619/*
620 * communicate with user-space
621 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500622 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
623 * On read, you get a full request, or block.
624 * On write, an update request is processed.
625 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800627 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500628 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * to the end and may wakeup and preceding readers.
630 * New readers are added to the head. If, on read, an item is found with
631 * CACHE_UPCALLING clear, we free it from the list.
632 *
633 */
634
635static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800636static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638struct cache_queue {
639 struct list_head list;
640 int reader; /* if 0, then request */
641};
642struct cache_request {
643 struct cache_queue q;
644 struct cache_head *item;
645 char * buf;
646 int len;
647 int readers;
648};
649struct cache_reader {
650 struct cache_queue q;
651 int offset; /* if non-0, we have a refcnt on next request */
652};
653
Trond Myklebust173912a2009-08-09 15:14:29 -0400654static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
655 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct cache_reader *rp = filp->private_data;
658 struct cache_request *rq;
Trond Myklebustda770052009-08-09 15:14:28 -0400659 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int err;
661
662 if (count == 0)
663 return 0;
664
Trond Myklebustda770052009-08-09 15:14:28 -0400665 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * readers on this file */
667 again:
668 spin_lock(&queue_lock);
669 /* need to find next request */
670 while (rp->q.list.next != &cd->queue &&
671 list_entry(rp->q.list.next, struct cache_queue, list)
672 ->reader) {
673 struct list_head *next = rp->q.list.next;
674 list_move(&rp->q.list, next);
675 }
676 if (rp->q.list.next == &cd->queue) {
677 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400678 mutex_unlock(&inode->i_mutex);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800679 BUG_ON(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681 }
682 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800683 BUG_ON(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (rp->offset == 0)
685 rq->readers++;
686 spin_unlock(&queue_lock);
687
688 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
689 err = -EAGAIN;
690 spin_lock(&queue_lock);
691 list_move(&rp->q.list, &rq->q.list);
692 spin_unlock(&queue_lock);
693 } else {
694 if (rp->offset + count > rq->len)
695 count = rq->len - rp->offset;
696 err = -EFAULT;
697 if (copy_to_user(buf, rq->buf + rp->offset, count))
698 goto out;
699 rp->offset += count;
700 if (rp->offset >= rq->len) {
701 rp->offset = 0;
702 spin_lock(&queue_lock);
703 list_move(&rp->q.list, &rq->q.list);
704 spin_unlock(&queue_lock);
705 }
706 err = 0;
707 }
708 out:
709 if (rp->offset == 0) {
710 /* need to release rq */
711 spin_lock(&queue_lock);
712 rq->readers--;
713 if (rq->readers == 0 &&
714 !test_bit(CACHE_PENDING, &rq->item->flags)) {
715 list_del(&rq->q.list);
716 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800717 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 kfree(rq->buf);
719 kfree(rq);
720 } else
721 spin_unlock(&queue_lock);
722 }
723 if (err == -EAGAIN)
724 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400725 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return err ? err : count;
727}
728
Trond Myklebustda770052009-08-09 15:14:28 -0400729static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
730 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Trond Myklebustda770052009-08-09 15:14:28 -0400732 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Trond Myklebustda770052009-08-09 15:14:28 -0400734 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400736 kaddr[count] = '\0';
737 ret = cd->cache_parse(cd, kaddr, count);
738 if (!ret)
739 ret = count;
740 return ret;
741}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Trond Myklebustda770052009-08-09 15:14:28 -0400743static ssize_t cache_slow_downcall(const char __user *buf,
744 size_t count, struct cache_detail *cd)
745{
746 static char write_buf[8192]; /* protected by queue_io_mutex */
747 ssize_t ret = -EINVAL;
748
749 if (count >= sizeof(write_buf))
750 goto out;
751 mutex_lock(&queue_io_mutex);
752 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800753 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400754out:
755 return ret;
756}
757
758static ssize_t cache_downcall(struct address_space *mapping,
759 const char __user *buf,
760 size_t count, struct cache_detail *cd)
761{
762 struct page *page;
763 char *kaddr;
764 ssize_t ret = -ENOMEM;
765
766 if (count >= PAGE_CACHE_SIZE)
767 goto out_slow;
768
769 page = find_or_create_page(mapping, 0, GFP_KERNEL);
770 if (!page)
771 goto out_slow;
772
773 kaddr = kmap(page);
774 ret = cache_do_downcall(kaddr, buf, count, cd);
775 kunmap(page);
776 unlock_page(page);
777 page_cache_release(page);
778 return ret;
779out_slow:
780 return cache_slow_downcall(buf, count, cd);
781}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Trond Myklebust173912a2009-08-09 15:14:29 -0400783static ssize_t cache_write(struct file *filp, const char __user *buf,
784 size_t count, loff_t *ppos,
785 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Trond Myklebustda770052009-08-09 15:14:28 -0400787 struct address_space *mapping = filp->f_mapping;
788 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebustda770052009-08-09 15:14:28 -0400789 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Trond Myklebustda770052009-08-09 15:14:28 -0400791 if (!cd->cache_parse)
792 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Trond Myklebustda770052009-08-09 15:14:28 -0400794 mutex_lock(&inode->i_mutex);
795 ret = cache_downcall(mapping, buf, count, cd);
796 mutex_unlock(&inode->i_mutex);
797out:
798 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
801static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
802
Trond Myklebust173912a2009-08-09 15:14:29 -0400803static unsigned int cache_poll(struct file *filp, poll_table *wait,
804 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 unsigned int mask;
807 struct cache_reader *rp = filp->private_data;
808 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 poll_wait(filp, &queue_wait, wait);
811
812 /* alway allow write */
813 mask = POLL_OUT | POLLWRNORM;
814
815 if (!rp)
816 return mask;
817
818 spin_lock(&queue_lock);
819
820 for (cq= &rp->q; &cq->list != &cd->queue;
821 cq = list_entry(cq->list.next, struct cache_queue, list))
822 if (!cq->reader) {
823 mask |= POLLIN | POLLRDNORM;
824 break;
825 }
826 spin_unlock(&queue_lock);
827 return mask;
828}
829
Trond Myklebust173912a2009-08-09 15:14:29 -0400830static int cache_ioctl(struct inode *ino, struct file *filp,
831 unsigned int cmd, unsigned long arg,
832 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 int len = 0;
835 struct cache_reader *rp = filp->private_data;
836 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (cmd != FIONREAD || !rp)
839 return -EINVAL;
840
841 spin_lock(&queue_lock);
842
843 /* only find the length remaining in current request,
844 * or the length of the next request
845 */
846 for (cq= &rp->q; &cq->list != &cd->queue;
847 cq = list_entry(cq->list.next, struct cache_queue, list))
848 if (!cq->reader) {
849 struct cache_request *cr =
850 container_of(cq, struct cache_request, q);
851 len = cr->len - rp->offset;
852 break;
853 }
854 spin_unlock(&queue_lock);
855
856 return put_user(len, (int __user *)arg);
857}
858
Trond Myklebust173912a2009-08-09 15:14:29 -0400859static int cache_open(struct inode *inode, struct file *filp,
860 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 struct cache_reader *rp = NULL;
863
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400864 if (!cd || !try_module_get(cd->owner))
865 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 nonseekable_open(inode, filp);
867 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
869 if (!rp)
870 return -ENOMEM;
871 rp->offset = 0;
872 rp->q.reader = 1;
873 atomic_inc(&cd->readers);
874 spin_lock(&queue_lock);
875 list_add(&rp->q.list, &cd->queue);
876 spin_unlock(&queue_lock);
877 }
878 filp->private_data = rp;
879 return 0;
880}
881
Trond Myklebust173912a2009-08-09 15:14:29 -0400882static int cache_release(struct inode *inode, struct file *filp,
883 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 if (rp) {
888 spin_lock(&queue_lock);
889 if (rp->offset) {
890 struct cache_queue *cq;
891 for (cq= &rp->q; &cq->list != &cd->queue;
892 cq = list_entry(cq->list.next, struct cache_queue, list))
893 if (!cq->reader) {
894 container_of(cq, struct cache_request, q)
895 ->readers--;
896 break;
897 }
898 rp->offset = 0;
899 }
900 list_del(&rp->q.list);
901 spin_unlock(&queue_lock);
902
903 filp->private_data = NULL;
904 kfree(rp);
905
906 cd->last_close = get_seconds();
907 atomic_dec(&cd->readers);
908 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400909 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return 0;
911}
912
913
914
NeilBrownf866a812009-08-04 15:22:38 +1000915static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct cache_queue *cq;
918 spin_lock(&queue_lock);
919 list_for_each_entry(cq, &detail->queue, list)
920 if (!cq->reader) {
921 struct cache_request *cr = container_of(cq, struct cache_request, q);
922 if (cr->item != ch)
923 continue;
924 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -0800925 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 list_del(&cr->q.list);
927 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800928 cache_put(cr->item, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 kfree(cr->buf);
930 kfree(cr);
931 return;
932 }
933 spin_unlock(&queue_lock);
934}
935
936/*
937 * Support routines for text-based upcalls.
938 * Fields are separated by spaces.
939 * Fields are either mangled to quote space tab newline slosh with slosh
940 * or a hexified with a leading \x
941 * Record is terminated with newline.
942 *
943 */
944
945void qword_add(char **bpp, int *lp, char *str)
946{
947 char *bp = *bpp;
948 int len = *lp;
949 char c;
950
951 if (len < 0) return;
952
953 while ((c=*str++) && len)
954 switch(c) {
955 case ' ':
956 case '\t':
957 case '\n':
958 case '\\':
959 if (len >= 4) {
960 *bp++ = '\\';
961 *bp++ = '0' + ((c & 0300)>>6);
962 *bp++ = '0' + ((c & 0070)>>3);
963 *bp++ = '0' + ((c & 0007)>>0);
964 }
965 len -= 4;
966 break;
967 default:
968 *bp++ = c;
969 len--;
970 }
971 if (c || len <1) len = -1;
972 else {
973 *bp++ = ' ';
974 len--;
975 }
976 *bpp = bp;
977 *lp = len;
978}
Trond Myklebust24c37672008-12-23 16:30:12 -0500979EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981void qword_addhex(char **bpp, int *lp, char *buf, int blen)
982{
983 char *bp = *bpp;
984 int len = *lp;
985
986 if (len < 0) return;
987
988 if (len > 2) {
989 *bp++ = '\\';
990 *bp++ = 'x';
991 len -= 2;
992 while (blen && len >= 2) {
993 unsigned char c = *buf++;
994 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
995 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
996 len -= 2;
997 blen--;
998 }
999 }
1000 if (blen || len<1) len = -1;
1001 else {
1002 *bp++ = ' ';
1003 len--;
1004 }
1005 *bpp = bp;
1006 *lp = len;
1007}
Trond Myklebust24c37672008-12-23 16:30:12 -05001008EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010static void warn_no_listener(struct cache_detail *detail)
1011{
1012 if (detail->last_warn != detail->last_close) {
1013 detail->last_warn = detail->last_close;
1014 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001015 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017}
1018
1019/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001020 * register an upcall request to user-space and queue it up for read() by the
1021 * upcall daemon.
1022 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 * Each request is at most one page long.
1024 */
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001025int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1026 void (*cache_request)(struct cache_detail *,
1027 struct cache_head *,
1028 char **,
1029 int *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031
1032 char *buf;
1033 struct cache_request *crq;
1034 char *bp;
1035 int len;
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (atomic_read(&detail->readers) == 0 &&
1038 detail->last_close < get_seconds() - 30) {
1039 warn_no_listener(detail);
1040 return -EINVAL;
1041 }
1042
1043 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1044 if (!buf)
1045 return -EAGAIN;
1046
1047 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1048 if (!crq) {
1049 kfree(buf);
1050 return -EAGAIN;
1051 }
1052
1053 bp = buf; len = PAGE_SIZE;
1054
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001055 cache_request(detail, h, &bp, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (len < 0) {
1058 kfree(buf);
1059 kfree(crq);
1060 return -EAGAIN;
1061 }
1062 crq->q.reader = 0;
1063 crq->item = cache_get(h);
1064 crq->buf = buf;
1065 crq->len = PAGE_SIZE - len;
1066 crq->readers = 0;
1067 spin_lock(&queue_lock);
1068 list_add_tail(&crq->q.list, &detail->queue);
1069 spin_unlock(&queue_lock);
1070 wake_up(&queue_wait);
1071 return 0;
1072}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001073EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075/*
1076 * parse a message from user-space and pass it
1077 * to an appropriate cache
1078 * Messages are, like requests, separated into fields by
1079 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1080 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001081 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 * reply cachename expiry key ... content....
1083 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001084 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
1086
1087#define isodigit(c) (isdigit(c) && c <= '7')
1088int qword_get(char **bpp, char *dest, int bufsize)
1089{
1090 /* return bytes copied, or -1 on error */
1091 char *bp = *bpp;
1092 int len = 0;
1093
1094 while (*bp == ' ') bp++;
1095
1096 if (bp[0] == '\\' && bp[1] == 'x') {
1097 /* HEX STRING */
1098 bp += 2;
1099 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
1100 int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1101 bp++;
1102 byte <<= 4;
1103 byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
1104 *dest++ = byte;
1105 bp++;
1106 len++;
1107 }
1108 } else {
1109 /* text with \nnn octal quoting */
1110 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1111 if (*bp == '\\' &&
1112 isodigit(bp[1]) && (bp[1] <= '3') &&
1113 isodigit(bp[2]) &&
1114 isodigit(bp[3])) {
1115 int byte = (*++bp -'0');
1116 bp++;
1117 byte = (byte << 3) | (*bp++ - '0');
1118 byte = (byte << 3) | (*bp++ - '0');
1119 *dest++ = byte;
1120 len++;
1121 } else {
1122 *dest++ = *bp++;
1123 len++;
1124 }
1125 }
1126 }
1127
1128 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1129 return -1;
1130 while (*bp == ' ') bp++;
1131 *bpp = bp;
1132 *dest = '\0';
1133 return len;
1134}
Trond Myklebust24c37672008-12-23 16:30:12 -05001135EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137
1138/*
1139 * support /proc/sunrpc/cache/$CACHENAME/content
1140 * as a seqfile.
1141 * We call ->cache_show passing NULL for the item to
1142 * get a header, then pass each real item in the cache
1143 */
1144
1145struct handle {
1146 struct cache_detail *cd;
1147};
1148
1149static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001150 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 loff_t n = *pos;
1153 unsigned hash, entry;
1154 struct cache_head *ch;
1155 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 read_lock(&cd->hash_lock);
1159 if (!n--)
1160 return SEQ_START_TOKEN;
1161 hash = n >> 32;
1162 entry = n & ((1LL<<32) - 1);
1163
1164 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1165 if (!entry--)
1166 return ch;
1167 n &= ~((1LL<<32) - 1);
1168 do {
1169 hash++;
1170 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001171 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 cd->hash_table[hash]==NULL);
1173 if (hash >= cd->hash_size)
1174 return NULL;
1175 *pos = n+1;
1176 return cd->hash_table[hash];
1177}
1178
1179static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1180{
1181 struct cache_head *ch = p;
1182 int hash = (*pos >> 32);
1183 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1184
1185 if (p == SEQ_START_TOKEN)
1186 hash = 0;
1187 else if (ch->next == NULL) {
1188 hash++;
1189 *pos += 1LL<<32;
1190 } else {
1191 ++*pos;
1192 return ch->next;
1193 }
1194 *pos &= ~((1LL<<32) - 1);
1195 while (hash < cd->hash_size &&
1196 cd->hash_table[hash] == NULL) {
1197 hash++;
1198 *pos += 1LL<<32;
1199 }
1200 if (hash >= cd->hash_size)
1201 return NULL;
1202 ++*pos;
1203 return cd->hash_table[hash];
1204}
1205
1206static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001207 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1210 read_unlock(&cd->hash_lock);
1211}
1212
1213static int c_show(struct seq_file *m, void *p)
1214{
1215 struct cache_head *cp = p;
1216 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1217
1218 if (p == SEQ_START_TOKEN)
1219 return cd->cache_show(m, cd, NULL);
1220
1221 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001222 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownbaab9352006-03-27 01:15:09 -08001223 cp->expiry_time, atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 cache_get(cp);
1225 if (cache_check(cd, cp, NULL))
1226 /* cache_check does a cache_put on failure */
1227 seq_printf(m, "# ");
1228 else
1229 cache_put(cp, cd);
1230
1231 return cd->cache_show(m, cd, cp);
1232}
1233
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001234static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 .start = c_start,
1236 .next = c_next,
1237 .stop = c_stop,
1238 .show = c_show,
1239};
1240
Trond Myklebust173912a2009-08-09 15:14:29 -04001241static int content_open(struct inode *inode, struct file *file,
1242 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 struct handle *han;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001246 if (!cd || !try_module_get(cd->owner))
1247 return -EACCES;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001248 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Li Zefana5990ea2010-03-11 14:08:10 -08001249 if (han == NULL) {
1250 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return -ENOMEM;
Li Zefana5990ea2010-03-11 14:08:10 -08001252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001258static int content_release(struct inode *inode, struct file *file,
1259 struct cache_detail *cd)
1260{
1261 int ret = seq_release_private(inode, file);
1262 module_put(cd->owner);
1263 return ret;
1264}
1265
1266static int open_flush(struct inode *inode, struct file *file,
1267 struct cache_detail *cd)
1268{
1269 if (!cd || !try_module_get(cd->owner))
1270 return -EACCES;
1271 return nonseekable_open(inode, file);
1272}
1273
1274static int release_flush(struct inode *inode, struct file *file,
1275 struct cache_detail *cd)
1276{
1277 module_put(cd->owner);
1278 return 0;
1279}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001282 size_t count, loff_t *ppos,
1283 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 char tbuf[20];
1286 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001287 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 sprintf(tbuf, "%lu\n", cd->flush_time);
1290 len = strlen(tbuf);
1291 if (p >= len)
1292 return 0;
1293 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001294 if (len > count)
1295 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001297 return -EFAULT;
1298 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return len;
1300}
1301
Trond Myklebust173912a2009-08-09 15:14:29 -04001302static ssize_t write_flush(struct file *file, const char __user *buf,
1303 size_t count, loff_t *ppos,
1304 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 char tbuf[20];
1307 char *ep;
1308 long flushtime;
1309 if (*ppos || count > sizeof(tbuf)-1)
1310 return -EINVAL;
1311 if (copy_from_user(tbuf, buf, count))
1312 return -EFAULT;
1313 tbuf[count] = 0;
1314 flushtime = simple_strtoul(tbuf, &ep, 0);
1315 if (*ep && *ep != '\n')
1316 return -EINVAL;
1317
1318 cd->flush_time = flushtime;
1319 cd->nextcheck = get_seconds();
1320 cache_flush();
1321
1322 *ppos += count;
1323 return count;
1324}
1325
Trond Myklebust173912a2009-08-09 15:14:29 -04001326static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1327 size_t count, loff_t *ppos)
1328{
1329 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1330
1331 return cache_read(filp, buf, count, ppos, cd);
1332}
1333
1334static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1335 size_t count, loff_t *ppos)
1336{
1337 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1338
1339 return cache_write(filp, buf, count, ppos, cd);
1340}
1341
1342static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1343{
1344 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1345
1346 return cache_poll(filp, wait, cd);
1347}
1348
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001349static long cache_ioctl_procfs(struct file *filp,
1350 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001351{
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001352 long ret;
1353 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebust173912a2009-08-09 15:14:29 -04001354 struct cache_detail *cd = PDE(inode)->data;
1355
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001356 lock_kernel();
1357 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1358 unlock_kernel();
1359
1360 return ret;
Trond Myklebust173912a2009-08-09 15:14:29 -04001361}
1362
1363static int cache_open_procfs(struct inode *inode, struct file *filp)
1364{
1365 struct cache_detail *cd = PDE(inode)->data;
1366
1367 return cache_open(inode, filp, cd);
1368}
1369
1370static int cache_release_procfs(struct inode *inode, struct file *filp)
1371{
1372 struct cache_detail *cd = PDE(inode)->data;
1373
1374 return cache_release(inode, filp, cd);
1375}
1376
1377static const struct file_operations cache_file_operations_procfs = {
1378 .owner = THIS_MODULE,
1379 .llseek = no_llseek,
1380 .read = cache_read_procfs,
1381 .write = cache_write_procfs,
1382 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001383 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001384 .open = cache_open_procfs,
1385 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386};
Trond Myklebust173912a2009-08-09 15:14:29 -04001387
1388static int content_open_procfs(struct inode *inode, struct file *filp)
1389{
1390 struct cache_detail *cd = PDE(inode)->data;
1391
1392 return content_open(inode, filp, cd);
1393}
1394
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001395static int content_release_procfs(struct inode *inode, struct file *filp)
1396{
1397 struct cache_detail *cd = PDE(inode)->data;
1398
1399 return content_release(inode, filp, cd);
1400}
1401
Trond Myklebust173912a2009-08-09 15:14:29 -04001402static const struct file_operations content_file_operations_procfs = {
1403 .open = content_open_procfs,
1404 .read = seq_read,
1405 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001406 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001407};
1408
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001409static int open_flush_procfs(struct inode *inode, struct file *filp)
1410{
1411 struct cache_detail *cd = PDE(inode)->data;
1412
1413 return open_flush(inode, filp, cd);
1414}
1415
1416static int release_flush_procfs(struct inode *inode, struct file *filp)
1417{
1418 struct cache_detail *cd = PDE(inode)->data;
1419
1420 return release_flush(inode, filp, cd);
1421}
1422
Trond Myklebust173912a2009-08-09 15:14:29 -04001423static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1424 size_t count, loff_t *ppos)
1425{
1426 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1427
1428 return read_flush(filp, buf, count, ppos, cd);
1429}
1430
1431static ssize_t write_flush_procfs(struct file *filp,
1432 const char __user *buf,
1433 size_t count, loff_t *ppos)
1434{
1435 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1436
1437 return write_flush(filp, buf, count, ppos, cd);
1438}
1439
1440static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001441 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001442 .read = read_flush_procfs,
1443 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001444 .release = release_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001445};
1446
1447static void remove_cache_proc_entries(struct cache_detail *cd)
1448{
1449 if (cd->u.procfs.proc_ent == NULL)
1450 return;
1451 if (cd->u.procfs.flush_ent)
1452 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1453 if (cd->u.procfs.channel_ent)
1454 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1455 if (cd->u.procfs.content_ent)
1456 remove_proc_entry("content", cd->u.procfs.proc_ent);
1457 cd->u.procfs.proc_ent = NULL;
1458 remove_proc_entry(cd->name, proc_net_rpc);
1459}
1460
1461#ifdef CONFIG_PROC_FS
1462static int create_cache_proc_entries(struct cache_detail *cd)
1463{
1464 struct proc_dir_entry *p;
1465
1466 cd->u.procfs.proc_ent = proc_mkdir(cd->name, proc_net_rpc);
1467 if (cd->u.procfs.proc_ent == NULL)
1468 goto out_nomem;
1469 cd->u.procfs.channel_ent = NULL;
1470 cd->u.procfs.content_ent = NULL;
1471
1472 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1473 cd->u.procfs.proc_ent,
1474 &cache_flush_operations_procfs, cd);
1475 cd->u.procfs.flush_ent = p;
1476 if (p == NULL)
1477 goto out_nomem;
1478
1479 if (cd->cache_upcall || cd->cache_parse) {
1480 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1481 cd->u.procfs.proc_ent,
1482 &cache_file_operations_procfs, cd);
1483 cd->u.procfs.channel_ent = p;
1484 if (p == NULL)
1485 goto out_nomem;
1486 }
1487 if (cd->cache_show) {
1488 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1489 cd->u.procfs.proc_ent,
1490 &content_file_operations_procfs, cd);
1491 cd->u.procfs.content_ent = p;
1492 if (p == NULL)
1493 goto out_nomem;
1494 }
1495 return 0;
1496out_nomem:
1497 remove_cache_proc_entries(cd);
1498 return -ENOMEM;
1499}
1500#else /* CONFIG_PROC_FS */
1501static int create_cache_proc_entries(struct cache_detail *cd)
1502{
1503 return 0;
1504}
1505#endif
1506
1507int cache_register(struct cache_detail *cd)
1508{
1509 int ret;
1510
1511 sunrpc_init_cache_detail(cd);
1512 ret = create_cache_proc_entries(cd);
1513 if (ret)
1514 sunrpc_destroy_cache_detail(cd);
1515 return ret;
1516}
1517EXPORT_SYMBOL_GPL(cache_register);
1518
1519void cache_unregister(struct cache_detail *cd)
1520{
1521 remove_cache_proc_entries(cd);
1522 sunrpc_destroy_cache_detail(cd);
1523}
1524EXPORT_SYMBOL_GPL(cache_unregister);
Trond Myklebust8854e822009-08-09 15:14:30 -04001525
1526static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1527 size_t count, loff_t *ppos)
1528{
1529 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1530
1531 return cache_read(filp, buf, count, ppos, cd);
1532}
1533
1534static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1535 size_t count, loff_t *ppos)
1536{
1537 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1538
1539 return cache_write(filp, buf, count, ppos, cd);
1540}
1541
1542static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1543{
1544 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1545
1546 return cache_poll(filp, wait, cd);
1547}
1548
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001549static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001550 unsigned int cmd, unsigned long arg)
1551{
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001552 struct inode *inode = filp->f_dentry->d_inode;
Trond Myklebust8854e822009-08-09 15:14:30 -04001553 struct cache_detail *cd = RPC_I(inode)->private;
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001554 long ret;
Trond Myklebust8854e822009-08-09 15:14:30 -04001555
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001556 lock_kernel();
1557 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1558 unlock_kernel();
1559
1560 return ret;
Trond Myklebust8854e822009-08-09 15:14:30 -04001561}
1562
1563static int cache_open_pipefs(struct inode *inode, struct file *filp)
1564{
1565 struct cache_detail *cd = RPC_I(inode)->private;
1566
1567 return cache_open(inode, filp, cd);
1568}
1569
1570static int cache_release_pipefs(struct inode *inode, struct file *filp)
1571{
1572 struct cache_detail *cd = RPC_I(inode)->private;
1573
1574 return cache_release(inode, filp, cd);
1575}
1576
1577const struct file_operations cache_file_operations_pipefs = {
1578 .owner = THIS_MODULE,
1579 .llseek = no_llseek,
1580 .read = cache_read_pipefs,
1581 .write = cache_write_pipefs,
1582 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001583 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001584 .open = cache_open_pipefs,
1585 .release = cache_release_pipefs,
1586};
1587
1588static int content_open_pipefs(struct inode *inode, struct file *filp)
1589{
1590 struct cache_detail *cd = RPC_I(inode)->private;
1591
1592 return content_open(inode, filp, cd);
1593}
1594
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001595static int content_release_pipefs(struct inode *inode, struct file *filp)
1596{
1597 struct cache_detail *cd = RPC_I(inode)->private;
1598
1599 return content_release(inode, filp, cd);
1600}
1601
Trond Myklebust8854e822009-08-09 15:14:30 -04001602const struct file_operations content_file_operations_pipefs = {
1603 .open = content_open_pipefs,
1604 .read = seq_read,
1605 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001606 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001607};
1608
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001609static int open_flush_pipefs(struct inode *inode, struct file *filp)
1610{
1611 struct cache_detail *cd = RPC_I(inode)->private;
1612
1613 return open_flush(inode, filp, cd);
1614}
1615
1616static int release_flush_pipefs(struct inode *inode, struct file *filp)
1617{
1618 struct cache_detail *cd = RPC_I(inode)->private;
1619
1620 return release_flush(inode, filp, cd);
1621}
1622
Trond Myklebust8854e822009-08-09 15:14:30 -04001623static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1624 size_t count, loff_t *ppos)
1625{
1626 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1627
1628 return read_flush(filp, buf, count, ppos, cd);
1629}
1630
1631static ssize_t write_flush_pipefs(struct file *filp,
1632 const char __user *buf,
1633 size_t count, loff_t *ppos)
1634{
1635 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1636
1637 return write_flush(filp, buf, count, ppos, cd);
1638}
1639
1640const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001641 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001642 .read = read_flush_pipefs,
1643 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001644 .release = release_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001645};
1646
1647int sunrpc_cache_register_pipefs(struct dentry *parent,
1648 const char *name, mode_t umode,
1649 struct cache_detail *cd)
1650{
1651 struct qstr q;
1652 struct dentry *dir;
1653 int ret = 0;
1654
1655 sunrpc_init_cache_detail(cd);
1656 q.name = name;
1657 q.len = strlen(name);
1658 q.hash = full_name_hash(q.name, q.len);
1659 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1660 if (!IS_ERR(dir))
1661 cd->u.pipefs.dir = dir;
1662 else {
1663 sunrpc_destroy_cache_detail(cd);
1664 ret = PTR_ERR(dir);
1665 }
1666 return ret;
1667}
1668EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1669
1670void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1671{
1672 rpc_remove_cache_dir(cd->u.pipefs.dir);
1673 cd->u.pipefs.dir = NULL;
1674 sunrpc_destroy_cache_detail(cd);
1675}
1676EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1677