blob: 1e72cc9559319a5cf43244f1dae0b5fc99587d29 [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>
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +040037#include "netns.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{
NeilBrownc5b29f82010-08-12 16:55:22 +100046 time_t now = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 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{
NeilBrownc5b29f82010-08-12 16:55:22 +100056 return (h->expiry_time < seconds_since_boot()) ||
NeilBrown2f50d8b2010-02-03 17:31:31 +110057 (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;
NeilBrownc5b29f82010-08-12 16:55:22 +1000131 head->last_refresh = seconds_since_boot();
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);
NeilBrownc5b29f82010-08-12 16:55:22 +1000242 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
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);
NeilBrownc5b29f82010-08-12 16:55:22 +1000257 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
NeilBrown908329f2009-09-09 16:32:54 +1000258 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);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300323static struct delayed_work cache_cleaner;
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);
NeilBrownc5b29f82010-08-12 16:55:22 +1000392 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 current_index = current_detail->hash_size;
394 else {
395 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000396 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
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;
NeilBrownc5b29f82010-08-12 16:55:22 +1000481 detail->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 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);
NeilBrown11174492010-08-12 17:04:08 +1000510static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static int cache_defer_cnt;
512
J. Bruce Fields6610f722010-08-26 13:19:52 -0400513static void __unhash_deferred_req(struct cache_deferred_req *dreq)
514{
515 list_del_init(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000516 hlist_del_init(&dreq->hash);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400517 cache_defer_cnt--;
518}
519
520static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
521{
522 int hash = DFR_HASH(item);
523
524 list_add(&dreq->recent, &cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000525 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400526}
527
J. Bruce Fields3211af12010-08-26 16:56:23 -0400528static int setup_deferral(struct cache_deferred_req *dreq, struct cache_head *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
J. Bruce Fields3211af12010-08-26 16:56:23 -0400530 struct cache_deferred_req *discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 spin_lock(&cache_defer_lock);
535
J. Bruce Fields6610f722010-08-26 13:19:52 -0400536 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /* it is in, now maybe clean up */
NeilBrowncd68c3742009-09-09 16:32:54 +1000539 discard = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (++cache_defer_cnt > DFR_MAX) {
NeilBrowncd68c3742009-09-09 16:32:54 +1000541 discard = list_entry(cache_defer_list.prev,
542 struct cache_deferred_req, recent);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400543 __unhash_deferred_req(discard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 spin_unlock(&cache_defer_lock);
546
NeilBrowncd68c3742009-09-09 16:32:54 +1000547 if (discard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* there was one too many */
NeilBrowncd68c3742009-09-09 16:32:54 +1000549 discard->revisit(discard, 1);
550
NeilBrown4013ede2006-03-27 01:15:07 -0800551 if (!test_bit(CACHE_PENDING, &item->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* must have just been validated... */
553 cache_revisit_request(item);
NeilBrown9e4c6372009-09-09 16:32:54 +1000554 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
NeilBrown9e4c6372009-09-09 16:32:54 +1000556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
J. Bruce Fields3211af12010-08-26 16:56:23 -0400559struct thread_deferred_req {
560 struct cache_deferred_req handle;
561 struct completion completion;
562};
563
564static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
565{
566 struct thread_deferred_req *dr =
567 container_of(dreq, struct thread_deferred_req, handle);
568 complete(&dr->completion);
569}
570
571static int cache_wait_req(struct cache_req *req, struct cache_head *item)
572{
573 struct thread_deferred_req sleeper;
574 struct cache_deferred_req *dreq = &sleeper.handle;
575 int ret;
576
577 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
578 dreq->revisit = cache_restart_thread;
579
580 ret = setup_deferral(dreq, item);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400581
NeilBrown277f68d2010-09-22 12:55:06 +1000582 if (ret ||
583 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400584 &sleeper.completion, req->thread_wait) <= 0) {
585 /* The completion wasn't completed, so we need
586 * to clean up
587 */
588 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000589 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400590 __unhash_deferred_req(&sleeper.handle);
591 spin_unlock(&cache_defer_lock);
592 } else {
593 /* cache_revisit_request already removed
594 * this from the hash table, but hasn't
595 * called ->revisit yet. It will very soon
596 * and we need to wait for it.
597 */
598 spin_unlock(&cache_defer_lock);
599 wait_for_completion(&sleeper.completion);
600 }
601 }
602 if (test_bit(CACHE_PENDING, &item->flags)) {
603 /* item is still pending, try request
604 * deferral
605 */
606 return -ETIMEDOUT;
607 }
608 /* only return success if we actually deferred the
609 * request. In this case we waited until it was
610 * answered so no deferral has happened - rather
611 * an answer already exists.
612 */
613 return -EEXIST;
614}
615
616static int cache_defer_req(struct cache_req *req, struct cache_head *item)
617{
618 struct cache_deferred_req *dreq;
619 int ret;
620
621 if (cache_defer_cnt >= DFR_MAX) {
622 /* too much in the cache, randomly drop this one,
623 * or continue and drop the oldest
624 */
625 if (net_random()&1)
626 return -ENOMEM;
627 }
628 if (req->thread_wait) {
629 ret = cache_wait_req(req, item);
630 if (ret != -ETIMEDOUT)
631 return ret;
632 }
633 dreq = req->defer(req);
634 if (dreq == NULL)
635 return -ENOMEM;
636 return setup_deferral(dreq, item);
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639static void cache_revisit_request(struct cache_head *item)
640{
641 struct cache_deferred_req *dreq;
642 struct list_head pending;
NeilBrown11174492010-08-12 17:04:08 +1000643 struct hlist_node *lp, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 int hash = DFR_HASH(item);
645
646 INIT_LIST_HEAD(&pending);
647 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800648
NeilBrown11174492010-08-12 17:04:08 +1000649 hlist_for_each_entry_safe(dreq, lp, tmp, &cache_defer_hash[hash], hash)
650 if (dreq->item == item) {
651 __unhash_deferred_req(dreq);
652 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
NeilBrown11174492010-08-12 17:04:08 +1000654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 spin_unlock(&cache_defer_lock);
656
657 while (!list_empty(&pending)) {
658 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
659 list_del_init(&dreq->recent);
660 dreq->revisit(dreq, 0);
661 }
662}
663
664void cache_clean_deferred(void *owner)
665{
666 struct cache_deferred_req *dreq, *tmp;
667 struct list_head pending;
668
669
670 INIT_LIST_HEAD(&pending);
671 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
NeilBrowne95dffa2010-09-22 12:55:06 +1000674 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400675 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000676 list_add(&dreq->recent, &pending);
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679 spin_unlock(&cache_defer_lock);
680
681 while (!list_empty(&pending)) {
682 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
683 list_del_init(&dreq->recent);
684 dreq->revisit(dreq, 1);
685 }
686}
687
688/*
689 * communicate with user-space
690 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500691 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
692 * On read, you get a full request, or block.
693 * On write, an update request is processed.
694 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800696 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500697 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * to the end and may wakeup and preceding readers.
699 * New readers are added to the head. If, on read, an item is found with
700 * CACHE_UPCALLING clear, we free it from the list.
701 *
702 */
703
704static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800705static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707struct cache_queue {
708 struct list_head list;
709 int reader; /* if 0, then request */
710};
711struct cache_request {
712 struct cache_queue q;
713 struct cache_head *item;
714 char * buf;
715 int len;
716 int readers;
717};
718struct cache_reader {
719 struct cache_queue q;
720 int offset; /* if non-0, we have a refcnt on next request */
721};
722
Trond Myklebust173912a2009-08-09 15:14:29 -0400723static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
724 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct cache_reader *rp = filp->private_data;
727 struct cache_request *rq;
Trond Myklebustda770052009-08-09 15:14:28 -0400728 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 int err;
730
731 if (count == 0)
732 return 0;
733
Trond Myklebustda770052009-08-09 15:14:28 -0400734 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * readers on this file */
736 again:
737 spin_lock(&queue_lock);
738 /* need to find next request */
739 while (rp->q.list.next != &cd->queue &&
740 list_entry(rp->q.list.next, struct cache_queue, list)
741 ->reader) {
742 struct list_head *next = rp->q.list.next;
743 list_move(&rp->q.list, next);
744 }
745 if (rp->q.list.next == &cd->queue) {
746 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400747 mutex_unlock(&inode->i_mutex);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800748 BUG_ON(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return 0;
750 }
751 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800752 BUG_ON(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (rp->offset == 0)
754 rq->readers++;
755 spin_unlock(&queue_lock);
756
757 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
758 err = -EAGAIN;
759 spin_lock(&queue_lock);
760 list_move(&rp->q.list, &rq->q.list);
761 spin_unlock(&queue_lock);
762 } else {
763 if (rp->offset + count > rq->len)
764 count = rq->len - rp->offset;
765 err = -EFAULT;
766 if (copy_to_user(buf, rq->buf + rp->offset, count))
767 goto out;
768 rp->offset += count;
769 if (rp->offset >= rq->len) {
770 rp->offset = 0;
771 spin_lock(&queue_lock);
772 list_move(&rp->q.list, &rq->q.list);
773 spin_unlock(&queue_lock);
774 }
775 err = 0;
776 }
777 out:
778 if (rp->offset == 0) {
779 /* need to release rq */
780 spin_lock(&queue_lock);
781 rq->readers--;
782 if (rq->readers == 0 &&
783 !test_bit(CACHE_PENDING, &rq->item->flags)) {
784 list_del(&rq->q.list);
785 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800786 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 kfree(rq->buf);
788 kfree(rq);
789 } else
790 spin_unlock(&queue_lock);
791 }
792 if (err == -EAGAIN)
793 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400794 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return err ? err : count;
796}
797
Trond Myklebustda770052009-08-09 15:14:28 -0400798static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
799 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Trond Myklebustda770052009-08-09 15:14:28 -0400801 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Trond Myklebustda770052009-08-09 15:14:28 -0400803 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400805 kaddr[count] = '\0';
806 ret = cd->cache_parse(cd, kaddr, count);
807 if (!ret)
808 ret = count;
809 return ret;
810}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Trond Myklebustda770052009-08-09 15:14:28 -0400812static ssize_t cache_slow_downcall(const char __user *buf,
813 size_t count, struct cache_detail *cd)
814{
815 static char write_buf[8192]; /* protected by queue_io_mutex */
816 ssize_t ret = -EINVAL;
817
818 if (count >= sizeof(write_buf))
819 goto out;
820 mutex_lock(&queue_io_mutex);
821 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800822 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400823out:
824 return ret;
825}
826
827static ssize_t cache_downcall(struct address_space *mapping,
828 const char __user *buf,
829 size_t count, struct cache_detail *cd)
830{
831 struct page *page;
832 char *kaddr;
833 ssize_t ret = -ENOMEM;
834
835 if (count >= PAGE_CACHE_SIZE)
836 goto out_slow;
837
838 page = find_or_create_page(mapping, 0, GFP_KERNEL);
839 if (!page)
840 goto out_slow;
841
842 kaddr = kmap(page);
843 ret = cache_do_downcall(kaddr, buf, count, cd);
844 kunmap(page);
845 unlock_page(page);
846 page_cache_release(page);
847 return ret;
848out_slow:
849 return cache_slow_downcall(buf, count, cd);
850}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Trond Myklebust173912a2009-08-09 15:14:29 -0400852static ssize_t cache_write(struct file *filp, const char __user *buf,
853 size_t count, loff_t *ppos,
854 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Trond Myklebustda770052009-08-09 15:14:28 -0400856 struct address_space *mapping = filp->f_mapping;
857 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebustda770052009-08-09 15:14:28 -0400858 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Trond Myklebustda770052009-08-09 15:14:28 -0400860 if (!cd->cache_parse)
861 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Trond Myklebustda770052009-08-09 15:14:28 -0400863 mutex_lock(&inode->i_mutex);
864 ret = cache_downcall(mapping, buf, count, cd);
865 mutex_unlock(&inode->i_mutex);
866out:
867 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
871
Trond Myklebust173912a2009-08-09 15:14:29 -0400872static unsigned int cache_poll(struct file *filp, poll_table *wait,
873 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 unsigned int mask;
876 struct cache_reader *rp = filp->private_data;
877 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 poll_wait(filp, &queue_wait, wait);
880
881 /* alway allow write */
882 mask = POLL_OUT | POLLWRNORM;
883
884 if (!rp)
885 return mask;
886
887 spin_lock(&queue_lock);
888
889 for (cq= &rp->q; &cq->list != &cd->queue;
890 cq = list_entry(cq->list.next, struct cache_queue, list))
891 if (!cq->reader) {
892 mask |= POLLIN | POLLRDNORM;
893 break;
894 }
895 spin_unlock(&queue_lock);
896 return mask;
897}
898
Trond Myklebust173912a2009-08-09 15:14:29 -0400899static int cache_ioctl(struct inode *ino, struct file *filp,
900 unsigned int cmd, unsigned long arg,
901 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 int len = 0;
904 struct cache_reader *rp = filp->private_data;
905 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 if (cmd != FIONREAD || !rp)
908 return -EINVAL;
909
910 spin_lock(&queue_lock);
911
912 /* only find the length remaining in current request,
913 * or the length of the next request
914 */
915 for (cq= &rp->q; &cq->list != &cd->queue;
916 cq = list_entry(cq->list.next, struct cache_queue, list))
917 if (!cq->reader) {
918 struct cache_request *cr =
919 container_of(cq, struct cache_request, q);
920 len = cr->len - rp->offset;
921 break;
922 }
923 spin_unlock(&queue_lock);
924
925 return put_user(len, (int __user *)arg);
926}
927
Trond Myklebust173912a2009-08-09 15:14:29 -0400928static int cache_open(struct inode *inode, struct file *filp,
929 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 struct cache_reader *rp = NULL;
932
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400933 if (!cd || !try_module_get(cd->owner))
934 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 nonseekable_open(inode, filp);
936 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
938 if (!rp)
939 return -ENOMEM;
940 rp->offset = 0;
941 rp->q.reader = 1;
942 atomic_inc(&cd->readers);
943 spin_lock(&queue_lock);
944 list_add(&rp->q.list, &cd->queue);
945 spin_unlock(&queue_lock);
946 }
947 filp->private_data = rp;
948 return 0;
949}
950
Trond Myklebust173912a2009-08-09 15:14:29 -0400951static int cache_release(struct inode *inode, struct file *filp,
952 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (rp) {
957 spin_lock(&queue_lock);
958 if (rp->offset) {
959 struct cache_queue *cq;
960 for (cq= &rp->q; &cq->list != &cd->queue;
961 cq = list_entry(cq->list.next, struct cache_queue, list))
962 if (!cq->reader) {
963 container_of(cq, struct cache_request, q)
964 ->readers--;
965 break;
966 }
967 rp->offset = 0;
968 }
969 list_del(&rp->q.list);
970 spin_unlock(&queue_lock);
971
972 filp->private_data = NULL;
973 kfree(rp);
974
NeilBrownc5b29f82010-08-12 16:55:22 +1000975 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 atomic_dec(&cd->readers);
977 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400978 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980}
981
982
983
NeilBrownf866a812009-08-04 15:22:38 +1000984static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct cache_queue *cq;
987 spin_lock(&queue_lock);
988 list_for_each_entry(cq, &detail->queue, list)
989 if (!cq->reader) {
990 struct cache_request *cr = container_of(cq, struct cache_request, q);
991 if (cr->item != ch)
992 continue;
993 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -0800994 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 list_del(&cr->q.list);
996 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800997 cache_put(cr->item, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 kfree(cr->buf);
999 kfree(cr);
1000 return;
1001 }
1002 spin_unlock(&queue_lock);
1003}
1004
1005/*
1006 * Support routines for text-based upcalls.
1007 * Fields are separated by spaces.
1008 * Fields are either mangled to quote space tab newline slosh with slosh
1009 * or a hexified with a leading \x
1010 * Record is terminated with newline.
1011 *
1012 */
1013
1014void qword_add(char **bpp, int *lp, char *str)
1015{
1016 char *bp = *bpp;
1017 int len = *lp;
1018 char c;
1019
1020 if (len < 0) return;
1021
1022 while ((c=*str++) && len)
1023 switch(c) {
1024 case ' ':
1025 case '\t':
1026 case '\n':
1027 case '\\':
1028 if (len >= 4) {
1029 *bp++ = '\\';
1030 *bp++ = '0' + ((c & 0300)>>6);
1031 *bp++ = '0' + ((c & 0070)>>3);
1032 *bp++ = '0' + ((c & 0007)>>0);
1033 }
1034 len -= 4;
1035 break;
1036 default:
1037 *bp++ = c;
1038 len--;
1039 }
1040 if (c || len <1) len = -1;
1041 else {
1042 *bp++ = ' ';
1043 len--;
1044 }
1045 *bpp = bp;
1046 *lp = len;
1047}
Trond Myklebust24c37672008-12-23 16:30:12 -05001048EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1051{
1052 char *bp = *bpp;
1053 int len = *lp;
1054
1055 if (len < 0) return;
1056
1057 if (len > 2) {
1058 *bp++ = '\\';
1059 *bp++ = 'x';
1060 len -= 2;
1061 while (blen && len >= 2) {
1062 unsigned char c = *buf++;
1063 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1064 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1065 len -= 2;
1066 blen--;
1067 }
1068 }
1069 if (blen || len<1) len = -1;
1070 else {
1071 *bp++ = ' ';
1072 len--;
1073 }
1074 *bpp = bp;
1075 *lp = len;
1076}
Trond Myklebust24c37672008-12-23 16:30:12 -05001077EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079static void warn_no_listener(struct cache_detail *detail)
1080{
1081 if (detail->last_warn != detail->last_close) {
1082 detail->last_warn = detail->last_close;
1083 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001084 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086}
1087
J. Bruce Fields06497522010-09-19 22:55:06 -04001088static bool cache_listeners_exist(struct cache_detail *detail)
1089{
1090 if (atomic_read(&detail->readers))
1091 return true;
1092 if (detail->last_close == 0)
1093 /* This cache was never opened */
1094 return false;
1095 if (detail->last_close < seconds_since_boot() - 30)
1096 /*
1097 * We allow for the possibility that someone might
1098 * restart a userspace daemon without restarting the
1099 * server; but after 30 seconds, we give up.
1100 */
1101 return false;
1102 return true;
1103}
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001106 * register an upcall request to user-space and queue it up for read() by the
1107 * upcall daemon.
1108 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * Each request is at most one page long.
1110 */
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001111int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1112 void (*cache_request)(struct cache_detail *,
1113 struct cache_head *,
1114 char **,
1115 int *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117
1118 char *buf;
1119 struct cache_request *crq;
1120 char *bp;
1121 int len;
1122
J. Bruce Fields06497522010-09-19 22:55:06 -04001123 if (!cache_listeners_exist(detail)) {
1124 warn_no_listener(detail);
1125 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1129 if (!buf)
1130 return -EAGAIN;
1131
1132 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1133 if (!crq) {
1134 kfree(buf);
1135 return -EAGAIN;
1136 }
1137
1138 bp = buf; len = PAGE_SIZE;
1139
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001140 cache_request(detail, h, &bp, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 if (len < 0) {
1143 kfree(buf);
1144 kfree(crq);
1145 return -EAGAIN;
1146 }
1147 crq->q.reader = 0;
1148 crq->item = cache_get(h);
1149 crq->buf = buf;
1150 crq->len = PAGE_SIZE - len;
1151 crq->readers = 0;
1152 spin_lock(&queue_lock);
1153 list_add_tail(&crq->q.list, &detail->queue);
1154 spin_unlock(&queue_lock);
1155 wake_up(&queue_wait);
1156 return 0;
1157}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001158EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160/*
1161 * parse a message from user-space and pass it
1162 * to an appropriate cache
1163 * Messages are, like requests, separated into fields by
1164 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1165 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001166 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 * reply cachename expiry key ... content....
1168 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001169 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 */
1171
1172#define isodigit(c) (isdigit(c) && c <= '7')
1173int qword_get(char **bpp, char *dest, int bufsize)
1174{
1175 /* return bytes copied, or -1 on error */
1176 char *bp = *bpp;
1177 int len = 0;
1178
1179 while (*bp == ' ') bp++;
1180
1181 if (bp[0] == '\\' && bp[1] == 'x') {
1182 /* HEX STRING */
1183 bp += 2;
Andy Shevchenkoe7f483e2010-09-21 09:40:25 +03001184 while (len < bufsize) {
1185 int h, l;
1186
1187 h = hex_to_bin(bp[0]);
1188 if (h < 0)
1189 break;
1190
1191 l = hex_to_bin(bp[1]);
1192 if (l < 0)
1193 break;
1194
1195 *dest++ = (h << 4) | l;
1196 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 len++;
1198 }
1199 } else {
1200 /* text with \nnn octal quoting */
1201 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1202 if (*bp == '\\' &&
1203 isodigit(bp[1]) && (bp[1] <= '3') &&
1204 isodigit(bp[2]) &&
1205 isodigit(bp[3])) {
1206 int byte = (*++bp -'0');
1207 bp++;
1208 byte = (byte << 3) | (*bp++ - '0');
1209 byte = (byte << 3) | (*bp++ - '0');
1210 *dest++ = byte;
1211 len++;
1212 } else {
1213 *dest++ = *bp++;
1214 len++;
1215 }
1216 }
1217 }
1218
1219 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1220 return -1;
1221 while (*bp == ' ') bp++;
1222 *bpp = bp;
1223 *dest = '\0';
1224 return len;
1225}
Trond Myklebust24c37672008-12-23 16:30:12 -05001226EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228
1229/*
1230 * support /proc/sunrpc/cache/$CACHENAME/content
1231 * as a seqfile.
1232 * We call ->cache_show passing NULL for the item to
1233 * get a header, then pass each real item in the cache
1234 */
1235
1236struct handle {
1237 struct cache_detail *cd;
1238};
1239
1240static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001241 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 loff_t n = *pos;
1244 unsigned hash, entry;
1245 struct cache_head *ch;
1246 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 read_lock(&cd->hash_lock);
1250 if (!n--)
1251 return SEQ_START_TOKEN;
1252 hash = n >> 32;
1253 entry = n & ((1LL<<32) - 1);
1254
1255 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1256 if (!entry--)
1257 return ch;
1258 n &= ~((1LL<<32) - 1);
1259 do {
1260 hash++;
1261 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001262 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 cd->hash_table[hash]==NULL);
1264 if (hash >= cd->hash_size)
1265 return NULL;
1266 *pos = n+1;
1267 return cd->hash_table[hash];
1268}
1269
1270static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1271{
1272 struct cache_head *ch = p;
1273 int hash = (*pos >> 32);
1274 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1275
1276 if (p == SEQ_START_TOKEN)
1277 hash = 0;
1278 else if (ch->next == NULL) {
1279 hash++;
1280 *pos += 1LL<<32;
1281 } else {
1282 ++*pos;
1283 return ch->next;
1284 }
1285 *pos &= ~((1LL<<32) - 1);
1286 while (hash < cd->hash_size &&
1287 cd->hash_table[hash] == NULL) {
1288 hash++;
1289 *pos += 1LL<<32;
1290 }
1291 if (hash >= cd->hash_size)
1292 return NULL;
1293 ++*pos;
1294 return cd->hash_table[hash];
1295}
1296
1297static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001298 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1301 read_unlock(&cd->hash_lock);
1302}
1303
1304static int c_show(struct seq_file *m, void *p)
1305{
1306 struct cache_head *cp = p;
1307 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1308
1309 if (p == SEQ_START_TOKEN)
1310 return cd->cache_show(m, cd, NULL);
1311
1312 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001313 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001314 convert_to_wallclock(cp->expiry_time),
1315 atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 cache_get(cp);
1317 if (cache_check(cd, cp, NULL))
1318 /* cache_check does a cache_put on failure */
1319 seq_printf(m, "# ");
1320 else
1321 cache_put(cp, cd);
1322
1323 return cd->cache_show(m, cd, cp);
1324}
1325
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001326static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 .start = c_start,
1328 .next = c_next,
1329 .stop = c_stop,
1330 .show = c_show,
1331};
1332
Trond Myklebust173912a2009-08-09 15:14:29 -04001333static int content_open(struct inode *inode, struct file *file,
1334 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 struct handle *han;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001338 if (!cd || !try_module_get(cd->owner))
1339 return -EACCES;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001340 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Li Zefana5990ea2010-03-11 14:08:10 -08001341 if (han == NULL) {
1342 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return -ENOMEM;
Li Zefana5990ea2010-03-11 14:08:10 -08001344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001350static int content_release(struct inode *inode, struct file *file,
1351 struct cache_detail *cd)
1352{
1353 int ret = seq_release_private(inode, file);
1354 module_put(cd->owner);
1355 return ret;
1356}
1357
1358static int open_flush(struct inode *inode, struct file *file,
1359 struct cache_detail *cd)
1360{
1361 if (!cd || !try_module_get(cd->owner))
1362 return -EACCES;
1363 return nonseekable_open(inode, file);
1364}
1365
1366static int release_flush(struct inode *inode, struct file *file,
1367 struct cache_detail *cd)
1368{
1369 module_put(cd->owner);
1370 return 0;
1371}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001374 size_t count, loff_t *ppos,
1375 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 char tbuf[20];
1378 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001379 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
NeilBrownc5b29f82010-08-12 16:55:22 +10001381 sprintf(tbuf, "%lu\n", convert_to_wallclock(cd->flush_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 len = strlen(tbuf);
1383 if (p >= len)
1384 return 0;
1385 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001386 if (len > count)
1387 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001389 return -EFAULT;
1390 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return len;
1392}
1393
Trond Myklebust173912a2009-08-09 15:14:29 -04001394static ssize_t write_flush(struct file *file, const char __user *buf,
1395 size_t count, loff_t *ppos,
1396 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 char tbuf[20];
NeilBrownc5b29f82010-08-12 16:55:22 +10001399 char *bp, *ep;
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (*ppos || count > sizeof(tbuf)-1)
1402 return -EINVAL;
1403 if (copy_from_user(tbuf, buf, count))
1404 return -EFAULT;
1405 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001406 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (*ep && *ep != '\n')
1408 return -EINVAL;
1409
NeilBrownc5b29f82010-08-12 16:55:22 +10001410 bp = tbuf;
1411 cd->flush_time = get_expiry(&bp);
1412 cd->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 cache_flush();
1414
1415 *ppos += count;
1416 return count;
1417}
1418
Trond Myklebust173912a2009-08-09 15:14:29 -04001419static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1420 size_t count, loff_t *ppos)
1421{
1422 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1423
1424 return cache_read(filp, buf, count, ppos, cd);
1425}
1426
1427static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1428 size_t count, loff_t *ppos)
1429{
1430 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1431
1432 return cache_write(filp, buf, count, ppos, cd);
1433}
1434
1435static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1436{
1437 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1438
1439 return cache_poll(filp, wait, cd);
1440}
1441
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001442static long cache_ioctl_procfs(struct file *filp,
1443 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001444{
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001445 long ret;
1446 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebust173912a2009-08-09 15:14:29 -04001447 struct cache_detail *cd = PDE(inode)->data;
1448
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001449 lock_kernel();
1450 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1451 unlock_kernel();
1452
1453 return ret;
Trond Myklebust173912a2009-08-09 15:14:29 -04001454}
1455
1456static int cache_open_procfs(struct inode *inode, struct file *filp)
1457{
1458 struct cache_detail *cd = PDE(inode)->data;
1459
1460 return cache_open(inode, filp, cd);
1461}
1462
1463static int cache_release_procfs(struct inode *inode, struct file *filp)
1464{
1465 struct cache_detail *cd = PDE(inode)->data;
1466
1467 return cache_release(inode, filp, cd);
1468}
1469
1470static const struct file_operations cache_file_operations_procfs = {
1471 .owner = THIS_MODULE,
1472 .llseek = no_llseek,
1473 .read = cache_read_procfs,
1474 .write = cache_write_procfs,
1475 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001476 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001477 .open = cache_open_procfs,
1478 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479};
Trond Myklebust173912a2009-08-09 15:14:29 -04001480
1481static int content_open_procfs(struct inode *inode, struct file *filp)
1482{
1483 struct cache_detail *cd = PDE(inode)->data;
1484
1485 return content_open(inode, filp, cd);
1486}
1487
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001488static int content_release_procfs(struct inode *inode, struct file *filp)
1489{
1490 struct cache_detail *cd = PDE(inode)->data;
1491
1492 return content_release(inode, filp, cd);
1493}
1494
Trond Myklebust173912a2009-08-09 15:14:29 -04001495static const struct file_operations content_file_operations_procfs = {
1496 .open = content_open_procfs,
1497 .read = seq_read,
1498 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001499 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001500};
1501
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001502static int open_flush_procfs(struct inode *inode, struct file *filp)
1503{
1504 struct cache_detail *cd = PDE(inode)->data;
1505
1506 return open_flush(inode, filp, cd);
1507}
1508
1509static int release_flush_procfs(struct inode *inode, struct file *filp)
1510{
1511 struct cache_detail *cd = PDE(inode)->data;
1512
1513 return release_flush(inode, filp, cd);
1514}
1515
Trond Myklebust173912a2009-08-09 15:14:29 -04001516static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1517 size_t count, loff_t *ppos)
1518{
1519 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1520
1521 return read_flush(filp, buf, count, ppos, cd);
1522}
1523
1524static ssize_t write_flush_procfs(struct file *filp,
1525 const char __user *buf,
1526 size_t count, loff_t *ppos)
1527{
1528 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1529
1530 return write_flush(filp, buf, count, ppos, cd);
1531}
1532
1533static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001534 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001535 .read = read_flush_procfs,
1536 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001537 .release = release_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001538};
1539
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001540static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001541{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001542 struct sunrpc_net *sn;
1543
Trond Myklebust173912a2009-08-09 15:14:29 -04001544 if (cd->u.procfs.proc_ent == NULL)
1545 return;
1546 if (cd->u.procfs.flush_ent)
1547 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1548 if (cd->u.procfs.channel_ent)
1549 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1550 if (cd->u.procfs.content_ent)
1551 remove_proc_entry("content", cd->u.procfs.proc_ent);
1552 cd->u.procfs.proc_ent = NULL;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001553 sn = net_generic(net, sunrpc_net_id);
1554 remove_proc_entry(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001555}
1556
1557#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001558static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001559{
1560 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001561 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001562
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001563 sn = net_generic(net, sunrpc_net_id);
1564 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001565 if (cd->u.procfs.proc_ent == NULL)
1566 goto out_nomem;
1567 cd->u.procfs.channel_ent = NULL;
1568 cd->u.procfs.content_ent = NULL;
1569
1570 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1571 cd->u.procfs.proc_ent,
1572 &cache_flush_operations_procfs, cd);
1573 cd->u.procfs.flush_ent = p;
1574 if (p == NULL)
1575 goto out_nomem;
1576
1577 if (cd->cache_upcall || cd->cache_parse) {
1578 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1579 cd->u.procfs.proc_ent,
1580 &cache_file_operations_procfs, cd);
1581 cd->u.procfs.channel_ent = p;
1582 if (p == NULL)
1583 goto out_nomem;
1584 }
1585 if (cd->cache_show) {
1586 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1587 cd->u.procfs.proc_ent,
1588 &content_file_operations_procfs, cd);
1589 cd->u.procfs.content_ent = p;
1590 if (p == NULL)
1591 goto out_nomem;
1592 }
1593 return 0;
1594out_nomem:
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001595 remove_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001596 return -ENOMEM;
1597}
1598#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001599static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001600{
1601 return 0;
1602}
1603#endif
1604
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001605void __init cache_initialize(void)
1606{
1607 INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner, do_cache_clean);
1608}
1609
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001610int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001611{
1612 int ret;
1613
1614 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001615 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001616 if (ret)
1617 sunrpc_destroy_cache_detail(cd);
1618 return ret;
1619}
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001620
1621int cache_register(struct cache_detail *cd)
1622{
1623 return cache_register_net(cd, &init_net);
1624}
Trond Myklebust173912a2009-08-09 15:14:29 -04001625EXPORT_SYMBOL_GPL(cache_register);
1626
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001627void cache_unregister_net(struct cache_detail *cd, struct net *net)
1628{
1629 remove_cache_proc_entries(cd, net);
1630 sunrpc_destroy_cache_detail(cd);
1631}
1632
Trond Myklebust173912a2009-08-09 15:14:29 -04001633void cache_unregister(struct cache_detail *cd)
1634{
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001635 cache_unregister_net(cd, &init_net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001636}
1637EXPORT_SYMBOL_GPL(cache_unregister);
Trond Myklebust8854e822009-08-09 15:14:30 -04001638
1639static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1640 size_t count, loff_t *ppos)
1641{
1642 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1643
1644 return cache_read(filp, buf, count, ppos, cd);
1645}
1646
1647static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1648 size_t count, loff_t *ppos)
1649{
1650 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1651
1652 return cache_write(filp, buf, count, ppos, cd);
1653}
1654
1655static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1656{
1657 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1658
1659 return cache_poll(filp, wait, cd);
1660}
1661
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001662static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001663 unsigned int cmd, unsigned long arg)
1664{
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001665 struct inode *inode = filp->f_dentry->d_inode;
Trond Myklebust8854e822009-08-09 15:14:30 -04001666 struct cache_detail *cd = RPC_I(inode)->private;
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001667 long ret;
Trond Myklebust8854e822009-08-09 15:14:30 -04001668
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001669 lock_kernel();
1670 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1671 unlock_kernel();
1672
1673 return ret;
Trond Myklebust8854e822009-08-09 15:14:30 -04001674}
1675
1676static int cache_open_pipefs(struct inode *inode, struct file *filp)
1677{
1678 struct cache_detail *cd = RPC_I(inode)->private;
1679
1680 return cache_open(inode, filp, cd);
1681}
1682
1683static int cache_release_pipefs(struct inode *inode, struct file *filp)
1684{
1685 struct cache_detail *cd = RPC_I(inode)->private;
1686
1687 return cache_release(inode, filp, cd);
1688}
1689
1690const struct file_operations cache_file_operations_pipefs = {
1691 .owner = THIS_MODULE,
1692 .llseek = no_llseek,
1693 .read = cache_read_pipefs,
1694 .write = cache_write_pipefs,
1695 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001696 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001697 .open = cache_open_pipefs,
1698 .release = cache_release_pipefs,
1699};
1700
1701static int content_open_pipefs(struct inode *inode, struct file *filp)
1702{
1703 struct cache_detail *cd = RPC_I(inode)->private;
1704
1705 return content_open(inode, filp, cd);
1706}
1707
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001708static int content_release_pipefs(struct inode *inode, struct file *filp)
1709{
1710 struct cache_detail *cd = RPC_I(inode)->private;
1711
1712 return content_release(inode, filp, cd);
1713}
1714
Trond Myklebust8854e822009-08-09 15:14:30 -04001715const struct file_operations content_file_operations_pipefs = {
1716 .open = content_open_pipefs,
1717 .read = seq_read,
1718 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001719 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001720};
1721
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001722static int open_flush_pipefs(struct inode *inode, struct file *filp)
1723{
1724 struct cache_detail *cd = RPC_I(inode)->private;
1725
1726 return open_flush(inode, filp, cd);
1727}
1728
1729static int release_flush_pipefs(struct inode *inode, struct file *filp)
1730{
1731 struct cache_detail *cd = RPC_I(inode)->private;
1732
1733 return release_flush(inode, filp, cd);
1734}
1735
Trond Myklebust8854e822009-08-09 15:14:30 -04001736static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1737 size_t count, loff_t *ppos)
1738{
1739 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1740
1741 return read_flush(filp, buf, count, ppos, cd);
1742}
1743
1744static ssize_t write_flush_pipefs(struct file *filp,
1745 const char __user *buf,
1746 size_t count, loff_t *ppos)
1747{
1748 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1749
1750 return write_flush(filp, buf, count, ppos, cd);
1751}
1752
1753const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001754 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001755 .read = read_flush_pipefs,
1756 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001757 .release = release_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001758};
1759
1760int sunrpc_cache_register_pipefs(struct dentry *parent,
1761 const char *name, mode_t umode,
1762 struct cache_detail *cd)
1763{
1764 struct qstr q;
1765 struct dentry *dir;
1766 int ret = 0;
1767
1768 sunrpc_init_cache_detail(cd);
1769 q.name = name;
1770 q.len = strlen(name);
1771 q.hash = full_name_hash(q.name, q.len);
1772 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1773 if (!IS_ERR(dir))
1774 cd->u.pipefs.dir = dir;
1775 else {
1776 sunrpc_destroy_cache_detail(cd);
1777 ret = PTR_ERR(dir);
1778 }
1779 return ret;
1780}
1781EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1782
1783void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1784{
1785 rpc_remove_cache_dir(cd->u.pipefs.dir);
1786 cd->u.pipefs.dir = NULL;
1787 sunrpc_destroy_cache_detail(cd);
1788}
1789EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1790