blob: ba61d0fa4b8036552a89c3fe12592e08df8c9145 [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
NeilBrownd29068c2010-10-07 15:29:46 +110041static void 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) {
NeilBrownd29068c2010-10-07 15:29:46 +1100272 cache_defer_req(rqstp, h);
273 if (!test_bit(CACHE_PENDING, &h->flags)) {
NeilBrown989a19b2009-08-04 15:22:38 +1000274 /* Request is not deferred */
275 rv = cache_is_valid(detail, h);
276 if (rv == -EAGAIN)
277 rv = -ETIMEDOUT;
278 }
279 }
NeilBrown4013ede2006-03-27 01:15:07 -0800280 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800281 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return rv;
283}
Trond Myklebust24c37672008-12-23 16:30:12 -0500284EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*
287 * caches need to be periodically cleaned.
288 * For this we maintain a list of cache_detail and
289 * a current pointer into that list and into the table
290 * for that entry.
291 *
292 * Each time clean_cache is called it finds the next non-empty entry
293 * in the current table and walks the list in that entry
294 * looking for entries that can be removed.
295 *
296 * An entry gets removed if:
297 * - The expiry is before current time
298 * - The last_refresh time is before the flush_time for that cache
299 *
300 * later we might drop old entries with non-NEVER expiry if that table
301 * is getting 'full' for some definition of 'full'
302 *
303 * The question of "how often to scan a table" is an interesting one
304 * and is answered in part by the use of the "nextcheck" field in the
305 * cache_detail.
306 * When a scan of a table begins, the nextcheck field is set to a time
307 * that is well into the future.
308 * While scanning, if an expiry time is found that is earlier than the
309 * current nextcheck time, nextcheck is set to that expiry time.
310 * If the flush_time is ever set to a time earlier than the nextcheck
311 * time, the nextcheck time is then set to that flush_time.
312 *
313 * A table is then only scanned if the current time is at least
314 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800315 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
317
318static LIST_HEAD(cache_list);
319static DEFINE_SPINLOCK(cache_list_lock);
320static struct cache_detail *current_detail;
321static int current_index;
322
David Howells65f27f32006-11-22 14:55:48 +0000323static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300324static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400326static void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500327{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 rwlock_init(&cd->hash_lock);
329 INIT_LIST_HEAD(&cd->queue);
330 spin_lock(&cache_list_lock);
331 cd->nextcheck = 0;
332 cd->entries = 0;
333 atomic_set(&cd->readers, 0);
334 cd->last_close = 0;
335 cd->last_warn = -1;
336 list_add(&cd->others, &cache_list);
337 spin_unlock(&cache_list_lock);
338
339 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000340 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400343static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 cache_purge(cd);
346 spin_lock(&cache_list_lock);
347 write_lock(&cd->hash_lock);
348 if (cd->entries || atomic_read(&cd->inuse)) {
349 write_unlock(&cd->hash_lock);
350 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500351 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 if (current_detail == cd)
354 current_detail = NULL;
355 list_del_init(&cd->others);
356 write_unlock(&cd->hash_lock);
357 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (list_empty(&cache_list)) {
359 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400360 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500362 return;
363out:
364 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367/* clean cache tries to find something to clean
368 * and cleans it.
369 * It returns 1 if it cleaned something,
370 * 0 if it didn't find anything this time
371 * -1 if it fell off the end of the list.
372 */
373static int cache_clean(void)
374{
375 int rv = 0;
376 struct list_head *next;
377
378 spin_lock(&cache_list_lock);
379
380 /* find a suitable table if we don't already have one */
381 while (current_detail == NULL ||
382 current_index >= current_detail->hash_size) {
383 if (current_detail)
384 next = current_detail->others.next;
385 else
386 next = cache_list.next;
387 if (next == &cache_list) {
388 current_detail = NULL;
389 spin_unlock(&cache_list_lock);
390 return -1;
391 }
392 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000393 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 current_index = current_detail->hash_size;
395 else {
396 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000397 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 }
400
401 /* find a non-empty bucket in the table */
402 while (current_detail &&
403 current_index < current_detail->hash_size &&
404 current_detail->hash_table[current_index] == NULL)
405 current_index++;
406
407 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (current_detail && current_index < current_detail->hash_size) {
410 struct cache_head *ch, **cp;
411 struct cache_detail *d;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 write_lock(&current_detail->hash_lock);
414
415 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 cp = & current_detail->hash_table[current_index];
NeilBrown3af49742010-02-03 17:31:31 +1100418 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (current_detail->nextcheck > ch->expiry_time)
420 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100421 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 *cp = ch->next;
425 ch->next = NULL;
426 current_detail->entries--;
427 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100428 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
NeilBrown3af49742010-02-03 17:31:31 +1100430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 write_unlock(&current_detail->hash_lock);
432 d = current_detail;
433 if (!ch)
434 current_index ++;
435 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000436 if (ch) {
NeilBrown3af49742010-02-03 17:31:31 +1100437 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
438 cache_dequeue(current_detail, ch);
NeilBrown5c4d2632009-08-04 15:22:38 +1000439 cache_revisit_request(ch);
NeilBrownbaab9352006-03-27 01:15:09 -0800440 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 } else
443 spin_unlock(&cache_list_lock);
444
445 return rv;
446}
447
448/*
449 * We want to regularly clean the cache, so we need to schedule some work ...
450 */
David Howells65f27f32006-11-22 14:55:48 +0000451static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 int delay = 5;
454 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700455 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 if (list_empty(&cache_list))
458 delay = 0;
459
460 if (delay)
461 schedule_delayed_work(&cache_cleaner, delay);
462}
463
464
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800465/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800467 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * be fully cleaned
469 */
470void cache_flush(void)
471{
472 while (cache_clean() != -1)
473 cond_resched();
474 while (cache_clean() != -1)
475 cond_resched();
476}
Trond Myklebust24c37672008-12-23 16:30:12 -0500477EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479void cache_purge(struct cache_detail *detail)
480{
481 detail->flush_time = LONG_MAX;
NeilBrownc5b29f82010-08-12 16:55:22 +1000482 detail->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 cache_flush();
484 detail->flush_time = 1;
485}
Trond Myklebust24c37672008-12-23 16:30:12 -0500486EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488
489/*
490 * Deferral and Revisiting of Requests.
491 *
492 * If a cache lookup finds a pending entry, we
493 * need to defer the request and revisit it later.
494 * All deferred requests are stored in a hash table,
495 * indexed by "struct cache_head *".
496 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800497 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * deferred form, which must contain a
499 * 'struct cache_deferred_req'
500 * This cache_deferred_req contains a method to allow
501 * it to be revisited when cache info is available
502 */
503
504#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
505#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
506
507#define DFR_MAX 300 /* ??? */
508
509static DEFINE_SPINLOCK(cache_defer_lock);
510static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000511static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static int cache_defer_cnt;
513
J. Bruce Fields6610f722010-08-26 13:19:52 -0400514static void __unhash_deferred_req(struct cache_deferred_req *dreq)
515{
NeilBrown11174492010-08-12 17:04:08 +1000516 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100517 if (!list_empty(&dreq->recent)) {
518 list_del_init(&dreq->recent);
519 cache_defer_cnt--;
520 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400521}
522
523static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
524{
525 int hash = DFR_HASH(item);
526
NeilBrowne33534d2010-10-07 15:29:46 +1100527 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000528 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400529}
530
NeilBrowne33534d2010-10-07 15:29:46 +1100531static void setup_deferral(struct cache_deferred_req *dreq,
532 struct cache_head *item,
533 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 spin_lock(&cache_defer_lock);
539
J. Bruce Fields6610f722010-08-26 13:19:52 -0400540 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
NeilBrowne33534d2010-10-07 15:29:46 +1100542 if (count_me) {
543 cache_defer_cnt++;
544 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
NeilBrowne33534d2010-10-07 15:29:46 +1100546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 spin_unlock(&cache_defer_lock);
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
J. Bruce Fields3211af12010-08-26 16:56:23 -0400551struct thread_deferred_req {
552 struct cache_deferred_req handle;
553 struct completion completion;
554};
555
556static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
557{
558 struct thread_deferred_req *dr =
559 container_of(dreq, struct thread_deferred_req, handle);
560 complete(&dr->completion);
561}
562
NeilBrownd29068c2010-10-07 15:29:46 +1100563static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400564{
565 struct thread_deferred_req sleeper;
566 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400567
568 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
569 dreq->revisit = cache_restart_thread;
570
NeilBrowne33534d2010-10-07 15:29:46 +1100571 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400572
NeilBrownd29068c2010-10-07 15:29:46 +1100573 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000574 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400575 &sleeper.completion, req->thread_wait) <= 0) {
576 /* The completion wasn't completed, so we need
577 * to clean up
578 */
579 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000580 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400581 __unhash_deferred_req(&sleeper.handle);
582 spin_unlock(&cache_defer_lock);
583 } else {
584 /* cache_revisit_request already removed
585 * this from the hash table, but hasn't
586 * called ->revisit yet. It will very soon
587 * and we need to wait for it.
588 */
589 spin_unlock(&cache_defer_lock);
590 wait_for_completion(&sleeper.completion);
591 }
592 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400593}
594
NeilBrowne33534d2010-10-07 15:29:46 +1100595static void cache_limit_defers(void)
596{
597 /* Make sure we haven't exceed the limit of allowed deferred
598 * requests.
599 */
600 struct cache_deferred_req *discard = NULL;
601
602 if (cache_defer_cnt <= DFR_MAX)
603 return;
604
605 spin_lock(&cache_defer_lock);
606
607 /* Consider removing either the first or the last */
608 if (cache_defer_cnt > DFR_MAX) {
609 if (net_random() & 1)
610 discard = list_entry(cache_defer_list.next,
611 struct cache_deferred_req, recent);
612 else
613 discard = list_entry(cache_defer_list.prev,
614 struct cache_deferred_req, recent);
615 __unhash_deferred_req(discard);
616 }
617 spin_unlock(&cache_defer_lock);
618 if (discard)
619 discard->revisit(discard, 1);
620}
621
NeilBrownd29068c2010-10-07 15:29:46 +1100622static void cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400623{
624 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400625
J. Bruce Fields3211af12010-08-26 16:56:23 -0400626 if (req->thread_wait) {
NeilBrownd29068c2010-10-07 15:29:46 +1100627 cache_wait_req(req, item);
628 if (!test_bit(CACHE_PENDING, &item->flags))
629 return;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400630 }
631 dreq = req->defer(req);
632 if (dreq == NULL)
NeilBrownd29068c2010-10-07 15:29:46 +1100633 return;
NeilBrowne33534d2010-10-07 15:29:46 +1100634 setup_deferral(dreq, item, 1);
NeilBrownd29068c2010-10-07 15:29:46 +1100635 if (!test_bit(CACHE_PENDING, &item->flags))
636 /* Bit could have been cleared before we managed to
637 * set up the deferral, so need to revisit just in case
638 */
639 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100640
641 cache_limit_defers();
J. Bruce Fields3211af12010-08-26 16:56:23 -0400642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644static void cache_revisit_request(struct cache_head *item)
645{
646 struct cache_deferred_req *dreq;
647 struct list_head pending;
NeilBrown11174492010-08-12 17:04:08 +1000648 struct hlist_node *lp, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int hash = DFR_HASH(item);
650
651 INIT_LIST_HEAD(&pending);
652 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800653
NeilBrown11174492010-08-12 17:04:08 +1000654 hlist_for_each_entry_safe(dreq, lp, tmp, &cache_defer_hash[hash], hash)
655 if (dreq->item == item) {
656 __unhash_deferred_req(dreq);
657 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
NeilBrown11174492010-08-12 17:04:08 +1000659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 spin_unlock(&cache_defer_lock);
661
662 while (!list_empty(&pending)) {
663 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
664 list_del_init(&dreq->recent);
665 dreq->revisit(dreq, 0);
666 }
667}
668
669void cache_clean_deferred(void *owner)
670{
671 struct cache_deferred_req *dreq, *tmp;
672 struct list_head pending;
673
674
675 INIT_LIST_HEAD(&pending);
676 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
NeilBrowne95dffa2010-09-22 12:55:06 +1000679 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400680 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000681 list_add(&dreq->recent, &pending);
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 spin_unlock(&cache_defer_lock);
685
686 while (!list_empty(&pending)) {
687 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
688 list_del_init(&dreq->recent);
689 dreq->revisit(dreq, 1);
690 }
691}
692
693/*
694 * communicate with user-space
695 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500696 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
697 * On read, you get a full request, or block.
698 * On write, an update request is processed.
699 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800701 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500702 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * to the end and may wakeup and preceding readers.
704 * New readers are added to the head. If, on read, an item is found with
705 * CACHE_UPCALLING clear, we free it from the list.
706 *
707 */
708
709static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800710static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712struct cache_queue {
713 struct list_head list;
714 int reader; /* if 0, then request */
715};
716struct cache_request {
717 struct cache_queue q;
718 struct cache_head *item;
719 char * buf;
720 int len;
721 int readers;
722};
723struct cache_reader {
724 struct cache_queue q;
725 int offset; /* if non-0, we have a refcnt on next request */
726};
727
Trond Myklebust173912a2009-08-09 15:14:29 -0400728static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
729 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 struct cache_reader *rp = filp->private_data;
732 struct cache_request *rq;
Trond Myklebustda770052009-08-09 15:14:28 -0400733 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int err;
735
736 if (count == 0)
737 return 0;
738
Trond Myklebustda770052009-08-09 15:14:28 -0400739 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * readers on this file */
741 again:
742 spin_lock(&queue_lock);
743 /* need to find next request */
744 while (rp->q.list.next != &cd->queue &&
745 list_entry(rp->q.list.next, struct cache_queue, list)
746 ->reader) {
747 struct list_head *next = rp->q.list.next;
748 list_move(&rp->q.list, next);
749 }
750 if (rp->q.list.next == &cd->queue) {
751 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400752 mutex_unlock(&inode->i_mutex);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800753 BUG_ON(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755 }
756 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800757 BUG_ON(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (rp->offset == 0)
759 rq->readers++;
760 spin_unlock(&queue_lock);
761
762 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
763 err = -EAGAIN;
764 spin_lock(&queue_lock);
765 list_move(&rp->q.list, &rq->q.list);
766 spin_unlock(&queue_lock);
767 } else {
768 if (rp->offset + count > rq->len)
769 count = rq->len - rp->offset;
770 err = -EFAULT;
771 if (copy_to_user(buf, rq->buf + rp->offset, count))
772 goto out;
773 rp->offset += count;
774 if (rp->offset >= rq->len) {
775 rp->offset = 0;
776 spin_lock(&queue_lock);
777 list_move(&rp->q.list, &rq->q.list);
778 spin_unlock(&queue_lock);
779 }
780 err = 0;
781 }
782 out:
783 if (rp->offset == 0) {
784 /* need to release rq */
785 spin_lock(&queue_lock);
786 rq->readers--;
787 if (rq->readers == 0 &&
788 !test_bit(CACHE_PENDING, &rq->item->flags)) {
789 list_del(&rq->q.list);
790 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800791 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 kfree(rq->buf);
793 kfree(rq);
794 } else
795 spin_unlock(&queue_lock);
796 }
797 if (err == -EAGAIN)
798 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400799 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return err ? err : count;
801}
802
Trond Myklebustda770052009-08-09 15:14:28 -0400803static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
804 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Trond Myklebustda770052009-08-09 15:14:28 -0400806 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Trond Myklebustda770052009-08-09 15:14:28 -0400808 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400810 kaddr[count] = '\0';
811 ret = cd->cache_parse(cd, kaddr, count);
812 if (!ret)
813 ret = count;
814 return ret;
815}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Trond Myklebustda770052009-08-09 15:14:28 -0400817static ssize_t cache_slow_downcall(const char __user *buf,
818 size_t count, struct cache_detail *cd)
819{
820 static char write_buf[8192]; /* protected by queue_io_mutex */
821 ssize_t ret = -EINVAL;
822
823 if (count >= sizeof(write_buf))
824 goto out;
825 mutex_lock(&queue_io_mutex);
826 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800827 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400828out:
829 return ret;
830}
831
832static ssize_t cache_downcall(struct address_space *mapping,
833 const char __user *buf,
834 size_t count, struct cache_detail *cd)
835{
836 struct page *page;
837 char *kaddr;
838 ssize_t ret = -ENOMEM;
839
840 if (count >= PAGE_CACHE_SIZE)
841 goto out_slow;
842
843 page = find_or_create_page(mapping, 0, GFP_KERNEL);
844 if (!page)
845 goto out_slow;
846
847 kaddr = kmap(page);
848 ret = cache_do_downcall(kaddr, buf, count, cd);
849 kunmap(page);
850 unlock_page(page);
851 page_cache_release(page);
852 return ret;
853out_slow:
854 return cache_slow_downcall(buf, count, cd);
855}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Trond Myklebust173912a2009-08-09 15:14:29 -0400857static ssize_t cache_write(struct file *filp, const char __user *buf,
858 size_t count, loff_t *ppos,
859 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Trond Myklebustda770052009-08-09 15:14:28 -0400861 struct address_space *mapping = filp->f_mapping;
862 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebustda770052009-08-09 15:14:28 -0400863 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Trond Myklebustda770052009-08-09 15:14:28 -0400865 if (!cd->cache_parse)
866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Trond Myklebustda770052009-08-09 15:14:28 -0400868 mutex_lock(&inode->i_mutex);
869 ret = cache_downcall(mapping, buf, count, cd);
870 mutex_unlock(&inode->i_mutex);
871out:
872 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
875static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
876
Trond Myklebust173912a2009-08-09 15:14:29 -0400877static unsigned int cache_poll(struct file *filp, poll_table *wait,
878 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 unsigned int mask;
881 struct cache_reader *rp = filp->private_data;
882 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 poll_wait(filp, &queue_wait, wait);
885
886 /* alway allow write */
887 mask = POLL_OUT | POLLWRNORM;
888
889 if (!rp)
890 return mask;
891
892 spin_lock(&queue_lock);
893
894 for (cq= &rp->q; &cq->list != &cd->queue;
895 cq = list_entry(cq->list.next, struct cache_queue, list))
896 if (!cq->reader) {
897 mask |= POLLIN | POLLRDNORM;
898 break;
899 }
900 spin_unlock(&queue_lock);
901 return mask;
902}
903
Trond Myklebust173912a2009-08-09 15:14:29 -0400904static int cache_ioctl(struct inode *ino, struct file *filp,
905 unsigned int cmd, unsigned long arg,
906 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 int len = 0;
909 struct cache_reader *rp = filp->private_data;
910 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (cmd != FIONREAD || !rp)
913 return -EINVAL;
914
915 spin_lock(&queue_lock);
916
917 /* only find the length remaining in current request,
918 * or the length of the next request
919 */
920 for (cq= &rp->q; &cq->list != &cd->queue;
921 cq = list_entry(cq->list.next, struct cache_queue, list))
922 if (!cq->reader) {
923 struct cache_request *cr =
924 container_of(cq, struct cache_request, q);
925 len = cr->len - rp->offset;
926 break;
927 }
928 spin_unlock(&queue_lock);
929
930 return put_user(len, (int __user *)arg);
931}
932
Trond Myklebust173912a2009-08-09 15:14:29 -0400933static int cache_open(struct inode *inode, struct file *filp,
934 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct cache_reader *rp = NULL;
937
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400938 if (!cd || !try_module_get(cd->owner))
939 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 nonseekable_open(inode, filp);
941 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
943 if (!rp)
944 return -ENOMEM;
945 rp->offset = 0;
946 rp->q.reader = 1;
947 atomic_inc(&cd->readers);
948 spin_lock(&queue_lock);
949 list_add(&rp->q.list, &cd->queue);
950 spin_unlock(&queue_lock);
951 }
952 filp->private_data = rp;
953 return 0;
954}
955
Trond Myklebust173912a2009-08-09 15:14:29 -0400956static int cache_release(struct inode *inode, struct file *filp,
957 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if (rp) {
962 spin_lock(&queue_lock);
963 if (rp->offset) {
964 struct cache_queue *cq;
965 for (cq= &rp->q; &cq->list != &cd->queue;
966 cq = list_entry(cq->list.next, struct cache_queue, list))
967 if (!cq->reader) {
968 container_of(cq, struct cache_request, q)
969 ->readers--;
970 break;
971 }
972 rp->offset = 0;
973 }
974 list_del(&rp->q.list);
975 spin_unlock(&queue_lock);
976
977 filp->private_data = NULL;
978 kfree(rp);
979
NeilBrownc5b29f82010-08-12 16:55:22 +1000980 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 atomic_dec(&cd->readers);
982 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400983 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return 0;
985}
986
987
988
NeilBrownf866a812009-08-04 15:22:38 +1000989static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 struct cache_queue *cq;
992 spin_lock(&queue_lock);
993 list_for_each_entry(cq, &detail->queue, list)
994 if (!cq->reader) {
995 struct cache_request *cr = container_of(cq, struct cache_request, q);
996 if (cr->item != ch)
997 continue;
998 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -0800999 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 list_del(&cr->q.list);
1001 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -08001002 cache_put(cr->item, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 kfree(cr->buf);
1004 kfree(cr);
1005 return;
1006 }
1007 spin_unlock(&queue_lock);
1008}
1009
1010/*
1011 * Support routines for text-based upcalls.
1012 * Fields are separated by spaces.
1013 * Fields are either mangled to quote space tab newline slosh with slosh
1014 * or a hexified with a leading \x
1015 * Record is terminated with newline.
1016 *
1017 */
1018
1019void qword_add(char **bpp, int *lp, char *str)
1020{
1021 char *bp = *bpp;
1022 int len = *lp;
1023 char c;
1024
1025 if (len < 0) return;
1026
1027 while ((c=*str++) && len)
1028 switch(c) {
1029 case ' ':
1030 case '\t':
1031 case '\n':
1032 case '\\':
1033 if (len >= 4) {
1034 *bp++ = '\\';
1035 *bp++ = '0' + ((c & 0300)>>6);
1036 *bp++ = '0' + ((c & 0070)>>3);
1037 *bp++ = '0' + ((c & 0007)>>0);
1038 }
1039 len -= 4;
1040 break;
1041 default:
1042 *bp++ = c;
1043 len--;
1044 }
1045 if (c || len <1) len = -1;
1046 else {
1047 *bp++ = ' ';
1048 len--;
1049 }
1050 *bpp = bp;
1051 *lp = len;
1052}
Trond Myklebust24c37672008-12-23 16:30:12 -05001053EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1056{
1057 char *bp = *bpp;
1058 int len = *lp;
1059
1060 if (len < 0) return;
1061
1062 if (len > 2) {
1063 *bp++ = '\\';
1064 *bp++ = 'x';
1065 len -= 2;
1066 while (blen && len >= 2) {
1067 unsigned char c = *buf++;
1068 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1069 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1070 len -= 2;
1071 blen--;
1072 }
1073 }
1074 if (blen || len<1) len = -1;
1075 else {
1076 *bp++ = ' ';
1077 len--;
1078 }
1079 *bpp = bp;
1080 *lp = len;
1081}
Trond Myklebust24c37672008-12-23 16:30:12 -05001082EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084static void warn_no_listener(struct cache_detail *detail)
1085{
1086 if (detail->last_warn != detail->last_close) {
1087 detail->last_warn = detail->last_close;
1088 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001089 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091}
1092
J. Bruce Fields06497522010-09-19 22:55:06 -04001093static bool cache_listeners_exist(struct cache_detail *detail)
1094{
1095 if (atomic_read(&detail->readers))
1096 return true;
1097 if (detail->last_close == 0)
1098 /* This cache was never opened */
1099 return false;
1100 if (detail->last_close < seconds_since_boot() - 30)
1101 /*
1102 * We allow for the possibility that someone might
1103 * restart a userspace daemon without restarting the
1104 * server; but after 30 seconds, we give up.
1105 */
1106 return false;
1107 return true;
1108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001111 * register an upcall request to user-space and queue it up for read() by the
1112 * upcall daemon.
1113 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 * Each request is at most one page long.
1115 */
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001116int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1117 void (*cache_request)(struct cache_detail *,
1118 struct cache_head *,
1119 char **,
1120 int *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122
1123 char *buf;
1124 struct cache_request *crq;
1125 char *bp;
1126 int len;
1127
J. Bruce Fields06497522010-09-19 22:55:06 -04001128 if (!cache_listeners_exist(detail)) {
1129 warn_no_listener(detail);
1130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132
1133 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1134 if (!buf)
1135 return -EAGAIN;
1136
1137 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1138 if (!crq) {
1139 kfree(buf);
1140 return -EAGAIN;
1141 }
1142
1143 bp = buf; len = PAGE_SIZE;
1144
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001145 cache_request(detail, h, &bp, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 if (len < 0) {
1148 kfree(buf);
1149 kfree(crq);
1150 return -EAGAIN;
1151 }
1152 crq->q.reader = 0;
1153 crq->item = cache_get(h);
1154 crq->buf = buf;
1155 crq->len = PAGE_SIZE - len;
1156 crq->readers = 0;
1157 spin_lock(&queue_lock);
1158 list_add_tail(&crq->q.list, &detail->queue);
1159 spin_unlock(&queue_lock);
1160 wake_up(&queue_wait);
1161 return 0;
1162}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001163EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165/*
1166 * parse a message from user-space and pass it
1167 * to an appropriate cache
1168 * Messages are, like requests, separated into fields by
1169 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1170 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001171 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * reply cachename expiry key ... content....
1173 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001174 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 */
1176
1177#define isodigit(c) (isdigit(c) && c <= '7')
1178int qword_get(char **bpp, char *dest, int bufsize)
1179{
1180 /* return bytes copied, or -1 on error */
1181 char *bp = *bpp;
1182 int len = 0;
1183
1184 while (*bp == ' ') bp++;
1185
1186 if (bp[0] == '\\' && bp[1] == 'x') {
1187 /* HEX STRING */
1188 bp += 2;
Andy Shevchenkoe7f483ea2010-09-21 09:40:25 +03001189 while (len < bufsize) {
1190 int h, l;
1191
1192 h = hex_to_bin(bp[0]);
1193 if (h < 0)
1194 break;
1195
1196 l = hex_to_bin(bp[1]);
1197 if (l < 0)
1198 break;
1199
1200 *dest++ = (h << 4) | l;
1201 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 len++;
1203 }
1204 } else {
1205 /* text with \nnn octal quoting */
1206 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1207 if (*bp == '\\' &&
1208 isodigit(bp[1]) && (bp[1] <= '3') &&
1209 isodigit(bp[2]) &&
1210 isodigit(bp[3])) {
1211 int byte = (*++bp -'0');
1212 bp++;
1213 byte = (byte << 3) | (*bp++ - '0');
1214 byte = (byte << 3) | (*bp++ - '0');
1215 *dest++ = byte;
1216 len++;
1217 } else {
1218 *dest++ = *bp++;
1219 len++;
1220 }
1221 }
1222 }
1223
1224 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1225 return -1;
1226 while (*bp == ' ') bp++;
1227 *bpp = bp;
1228 *dest = '\0';
1229 return len;
1230}
Trond Myklebust24c37672008-12-23 16:30:12 -05001231EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233
1234/*
1235 * support /proc/sunrpc/cache/$CACHENAME/content
1236 * as a seqfile.
1237 * We call ->cache_show passing NULL for the item to
1238 * get a header, then pass each real item in the cache
1239 */
1240
1241struct handle {
1242 struct cache_detail *cd;
1243};
1244
1245static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001246 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 loff_t n = *pos;
1249 unsigned hash, entry;
1250 struct cache_head *ch;
1251 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 read_lock(&cd->hash_lock);
1255 if (!n--)
1256 return SEQ_START_TOKEN;
1257 hash = n >> 32;
1258 entry = n & ((1LL<<32) - 1);
1259
1260 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1261 if (!entry--)
1262 return ch;
1263 n &= ~((1LL<<32) - 1);
1264 do {
1265 hash++;
1266 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001267 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 cd->hash_table[hash]==NULL);
1269 if (hash >= cd->hash_size)
1270 return NULL;
1271 *pos = n+1;
1272 return cd->hash_table[hash];
1273}
1274
1275static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1276{
1277 struct cache_head *ch = p;
1278 int hash = (*pos >> 32);
1279 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1280
1281 if (p == SEQ_START_TOKEN)
1282 hash = 0;
1283 else if (ch->next == NULL) {
1284 hash++;
1285 *pos += 1LL<<32;
1286 } else {
1287 ++*pos;
1288 return ch->next;
1289 }
1290 *pos &= ~((1LL<<32) - 1);
1291 while (hash < cd->hash_size &&
1292 cd->hash_table[hash] == NULL) {
1293 hash++;
1294 *pos += 1LL<<32;
1295 }
1296 if (hash >= cd->hash_size)
1297 return NULL;
1298 ++*pos;
1299 return cd->hash_table[hash];
1300}
1301
1302static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001303 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1306 read_unlock(&cd->hash_lock);
1307}
1308
1309static int c_show(struct seq_file *m, void *p)
1310{
1311 struct cache_head *cp = p;
1312 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1313
1314 if (p == SEQ_START_TOKEN)
1315 return cd->cache_show(m, cd, NULL);
1316
1317 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001318 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001319 convert_to_wallclock(cp->expiry_time),
1320 atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 cache_get(cp);
1322 if (cache_check(cd, cp, NULL))
1323 /* cache_check does a cache_put on failure */
1324 seq_printf(m, "# ");
1325 else
1326 cache_put(cp, cd);
1327
1328 return cd->cache_show(m, cd, cp);
1329}
1330
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001331static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 .start = c_start,
1333 .next = c_next,
1334 .stop = c_stop,
1335 .show = c_show,
1336};
1337
Trond Myklebust173912a2009-08-09 15:14:29 -04001338static int content_open(struct inode *inode, struct file *file,
1339 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 struct handle *han;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001343 if (!cd || !try_module_get(cd->owner))
1344 return -EACCES;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001345 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Li Zefana5990ea2010-03-11 14:08:10 -08001346 if (han == NULL) {
1347 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -ENOMEM;
Li Zefana5990ea2010-03-11 14:08:10 -08001349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001355static int content_release(struct inode *inode, struct file *file,
1356 struct cache_detail *cd)
1357{
1358 int ret = seq_release_private(inode, file);
1359 module_put(cd->owner);
1360 return ret;
1361}
1362
1363static int open_flush(struct inode *inode, struct file *file,
1364 struct cache_detail *cd)
1365{
1366 if (!cd || !try_module_get(cd->owner))
1367 return -EACCES;
1368 return nonseekable_open(inode, file);
1369}
1370
1371static int release_flush(struct inode *inode, struct file *file,
1372 struct cache_detail *cd)
1373{
1374 module_put(cd->owner);
1375 return 0;
1376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001379 size_t count, loff_t *ppos,
1380 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 char tbuf[20];
1383 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001384 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
NeilBrownc5b29f82010-08-12 16:55:22 +10001386 sprintf(tbuf, "%lu\n", convert_to_wallclock(cd->flush_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 len = strlen(tbuf);
1388 if (p >= len)
1389 return 0;
1390 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001391 if (len > count)
1392 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001394 return -EFAULT;
1395 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return len;
1397}
1398
Trond Myklebust173912a2009-08-09 15:14:29 -04001399static ssize_t write_flush(struct file *file, const char __user *buf,
1400 size_t count, loff_t *ppos,
1401 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 char tbuf[20];
NeilBrownc5b29f82010-08-12 16:55:22 +10001404 char *bp, *ep;
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (*ppos || count > sizeof(tbuf)-1)
1407 return -EINVAL;
1408 if (copy_from_user(tbuf, buf, count))
1409 return -EFAULT;
1410 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001411 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (*ep && *ep != '\n')
1413 return -EINVAL;
1414
NeilBrownc5b29f82010-08-12 16:55:22 +10001415 bp = tbuf;
1416 cd->flush_time = get_expiry(&bp);
1417 cd->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 cache_flush();
1419
1420 *ppos += count;
1421 return count;
1422}
1423
Trond Myklebust173912a2009-08-09 15:14:29 -04001424static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1425 size_t count, loff_t *ppos)
1426{
1427 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1428
1429 return cache_read(filp, buf, count, ppos, cd);
1430}
1431
1432static ssize_t cache_write_procfs(struct file *filp, 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 cache_write(filp, buf, count, ppos, cd);
1438}
1439
1440static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1441{
1442 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1443
1444 return cache_poll(filp, wait, cd);
1445}
1446
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001447static long cache_ioctl_procfs(struct file *filp,
1448 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001449{
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001450 long ret;
1451 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebust173912a2009-08-09 15:14:29 -04001452 struct cache_detail *cd = PDE(inode)->data;
1453
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001454 lock_kernel();
1455 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1456 unlock_kernel();
1457
1458 return ret;
Trond Myklebust173912a2009-08-09 15:14:29 -04001459}
1460
1461static int cache_open_procfs(struct inode *inode, struct file *filp)
1462{
1463 struct cache_detail *cd = PDE(inode)->data;
1464
1465 return cache_open(inode, filp, cd);
1466}
1467
1468static int cache_release_procfs(struct inode *inode, struct file *filp)
1469{
1470 struct cache_detail *cd = PDE(inode)->data;
1471
1472 return cache_release(inode, filp, cd);
1473}
1474
1475static const struct file_operations cache_file_operations_procfs = {
1476 .owner = THIS_MODULE,
1477 .llseek = no_llseek,
1478 .read = cache_read_procfs,
1479 .write = cache_write_procfs,
1480 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001481 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001482 .open = cache_open_procfs,
1483 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484};
Trond Myklebust173912a2009-08-09 15:14:29 -04001485
1486static int content_open_procfs(struct inode *inode, struct file *filp)
1487{
1488 struct cache_detail *cd = PDE(inode)->data;
1489
1490 return content_open(inode, filp, cd);
1491}
1492
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001493static int content_release_procfs(struct inode *inode, struct file *filp)
1494{
1495 struct cache_detail *cd = PDE(inode)->data;
1496
1497 return content_release(inode, filp, cd);
1498}
1499
Trond Myklebust173912a2009-08-09 15:14:29 -04001500static const struct file_operations content_file_operations_procfs = {
1501 .open = content_open_procfs,
1502 .read = seq_read,
1503 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001504 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001505};
1506
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001507static int open_flush_procfs(struct inode *inode, struct file *filp)
1508{
1509 struct cache_detail *cd = PDE(inode)->data;
1510
1511 return open_flush(inode, filp, cd);
1512}
1513
1514static int release_flush_procfs(struct inode *inode, struct file *filp)
1515{
1516 struct cache_detail *cd = PDE(inode)->data;
1517
1518 return release_flush(inode, filp, cd);
1519}
1520
Trond Myklebust173912a2009-08-09 15:14:29 -04001521static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1522 size_t count, loff_t *ppos)
1523{
1524 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1525
1526 return read_flush(filp, buf, count, ppos, cd);
1527}
1528
1529static ssize_t write_flush_procfs(struct file *filp,
1530 const char __user *buf,
1531 size_t count, loff_t *ppos)
1532{
1533 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1534
1535 return write_flush(filp, buf, count, ppos, cd);
1536}
1537
1538static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001539 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001540 .read = read_flush_procfs,
1541 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001542 .release = release_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001543};
1544
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001545static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001546{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001547 struct sunrpc_net *sn;
1548
Trond Myklebust173912a2009-08-09 15:14:29 -04001549 if (cd->u.procfs.proc_ent == NULL)
1550 return;
1551 if (cd->u.procfs.flush_ent)
1552 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1553 if (cd->u.procfs.channel_ent)
1554 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1555 if (cd->u.procfs.content_ent)
1556 remove_proc_entry("content", cd->u.procfs.proc_ent);
1557 cd->u.procfs.proc_ent = NULL;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001558 sn = net_generic(net, sunrpc_net_id);
1559 remove_proc_entry(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001560}
1561
1562#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001563static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001564{
1565 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001566 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001567
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001568 sn = net_generic(net, sunrpc_net_id);
1569 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001570 if (cd->u.procfs.proc_ent == NULL)
1571 goto out_nomem;
1572 cd->u.procfs.channel_ent = NULL;
1573 cd->u.procfs.content_ent = NULL;
1574
1575 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1576 cd->u.procfs.proc_ent,
1577 &cache_flush_operations_procfs, cd);
1578 cd->u.procfs.flush_ent = p;
1579 if (p == NULL)
1580 goto out_nomem;
1581
1582 if (cd->cache_upcall || cd->cache_parse) {
1583 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1584 cd->u.procfs.proc_ent,
1585 &cache_file_operations_procfs, cd);
1586 cd->u.procfs.channel_ent = p;
1587 if (p == NULL)
1588 goto out_nomem;
1589 }
1590 if (cd->cache_show) {
1591 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1592 cd->u.procfs.proc_ent,
1593 &content_file_operations_procfs, cd);
1594 cd->u.procfs.content_ent = p;
1595 if (p == NULL)
1596 goto out_nomem;
1597 }
1598 return 0;
1599out_nomem:
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001600 remove_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001601 return -ENOMEM;
1602}
1603#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001604static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001605{
1606 return 0;
1607}
1608#endif
1609
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001610void __init cache_initialize(void)
1611{
1612 INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner, do_cache_clean);
1613}
1614
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001615int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001616{
1617 int ret;
1618
1619 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001620 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001621 if (ret)
1622 sunrpc_destroy_cache_detail(cd);
1623 return ret;
1624}
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001625
1626int cache_register(struct cache_detail *cd)
1627{
1628 return cache_register_net(cd, &init_net);
1629}
Trond Myklebust173912a2009-08-09 15:14:29 -04001630EXPORT_SYMBOL_GPL(cache_register);
1631
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001632void cache_unregister_net(struct cache_detail *cd, struct net *net)
1633{
1634 remove_cache_proc_entries(cd, net);
1635 sunrpc_destroy_cache_detail(cd);
1636}
1637
Trond Myklebust173912a2009-08-09 15:14:29 -04001638void cache_unregister(struct cache_detail *cd)
1639{
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001640 cache_unregister_net(cd, &init_net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001641}
1642EXPORT_SYMBOL_GPL(cache_unregister);
Trond Myklebust8854e822009-08-09 15:14:30 -04001643
1644static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1645 size_t count, loff_t *ppos)
1646{
1647 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1648
1649 return cache_read(filp, buf, count, ppos, cd);
1650}
1651
1652static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1653 size_t count, loff_t *ppos)
1654{
1655 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1656
1657 return cache_write(filp, buf, count, ppos, cd);
1658}
1659
1660static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1661{
1662 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1663
1664 return cache_poll(filp, wait, cd);
1665}
1666
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001667static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001668 unsigned int cmd, unsigned long arg)
1669{
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001670 struct inode *inode = filp->f_dentry->d_inode;
Trond Myklebust8854e822009-08-09 15:14:30 -04001671 struct cache_detail *cd = RPC_I(inode)->private;
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001672 long ret;
Trond Myklebust8854e822009-08-09 15:14:30 -04001673
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001674 lock_kernel();
1675 ret = cache_ioctl(inode, filp, cmd, arg, cd);
1676 unlock_kernel();
1677
1678 return ret;
Trond Myklebust8854e822009-08-09 15:14:30 -04001679}
1680
1681static int cache_open_pipefs(struct inode *inode, struct file *filp)
1682{
1683 struct cache_detail *cd = RPC_I(inode)->private;
1684
1685 return cache_open(inode, filp, cd);
1686}
1687
1688static int cache_release_pipefs(struct inode *inode, struct file *filp)
1689{
1690 struct cache_detail *cd = RPC_I(inode)->private;
1691
1692 return cache_release(inode, filp, cd);
1693}
1694
1695const struct file_operations cache_file_operations_pipefs = {
1696 .owner = THIS_MODULE,
1697 .llseek = no_llseek,
1698 .read = cache_read_pipefs,
1699 .write = cache_write_pipefs,
1700 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001701 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001702 .open = cache_open_pipefs,
1703 .release = cache_release_pipefs,
1704};
1705
1706static int content_open_pipefs(struct inode *inode, struct file *filp)
1707{
1708 struct cache_detail *cd = RPC_I(inode)->private;
1709
1710 return content_open(inode, filp, cd);
1711}
1712
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001713static int content_release_pipefs(struct inode *inode, struct file *filp)
1714{
1715 struct cache_detail *cd = RPC_I(inode)->private;
1716
1717 return content_release(inode, filp, cd);
1718}
1719
Trond Myklebust8854e822009-08-09 15:14:30 -04001720const struct file_operations content_file_operations_pipefs = {
1721 .open = content_open_pipefs,
1722 .read = seq_read,
1723 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001724 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001725};
1726
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001727static int open_flush_pipefs(struct inode *inode, struct file *filp)
1728{
1729 struct cache_detail *cd = RPC_I(inode)->private;
1730
1731 return open_flush(inode, filp, cd);
1732}
1733
1734static int release_flush_pipefs(struct inode *inode, struct file *filp)
1735{
1736 struct cache_detail *cd = RPC_I(inode)->private;
1737
1738 return release_flush(inode, filp, cd);
1739}
1740
Trond Myklebust8854e822009-08-09 15:14:30 -04001741static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1742 size_t count, loff_t *ppos)
1743{
1744 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1745
1746 return read_flush(filp, buf, count, ppos, cd);
1747}
1748
1749static ssize_t write_flush_pipefs(struct file *filp,
1750 const char __user *buf,
1751 size_t count, loff_t *ppos)
1752{
1753 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1754
1755 return write_flush(filp, buf, count, ppos, cd);
1756}
1757
1758const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001759 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001760 .read = read_flush_pipefs,
1761 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001762 .release = release_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001763};
1764
1765int sunrpc_cache_register_pipefs(struct dentry *parent,
1766 const char *name, mode_t umode,
1767 struct cache_detail *cd)
1768{
1769 struct qstr q;
1770 struct dentry *dir;
1771 int ret = 0;
1772
1773 sunrpc_init_cache_detail(cd);
1774 q.name = name;
1775 q.len = strlen(name);
1776 q.hash = full_name_hash(q.name, q.len);
1777 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1778 if (!IS_ERR(dir))
1779 cd->u.pipefs.dir = dir;
1780 else {
1781 sunrpc_destroy_cache_detail(cd);
1782 ret = PTR_ERR(dir);
1783 }
1784 return ret;
1785}
1786EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1787
1788void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1789{
1790 rpc_remove_cache_dir(cd->u.pipefs.dir);
1791 cd->u.pipefs.dir = NULL;
1792 sunrpc_destroy_cache_detail(cd);
1793}
1794EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1795