blob: a6c57334fa137749753ad5d3397c011bdae37ec3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sunrpc/cache.c
3 *
4 * Generic code for various authentication-related caches
5 * used by sunrpc clients and servers.
6 *
7 * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
8 *
9 * Released under terms in GPL version 2. See COPYING.
10 *
11 */
12
13#include <linux/types.h>
14#include <linux/fs.h>
15#include <linux/file.h>
16#include <linux/slab.h>
17#include <linux/signal.h>
18#include <linux/sched.h>
19#include <linux/kmod.h>
20#include <linux/list.h>
21#include <linux/module.h>
22#include <linux/ctype.h>
23#include <asm/uaccess.h>
24#include <linux/poll.h>
25#include <linux/seq_file.h>
26#include <linux/proc_fs.h>
27#include <linux/net.h>
28#include <linux/workqueue.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080029#include <linux/mutex.h>
Trond Myklebustda770052009-08-09 15:14:28 -040030#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/ioctls.h>
32#include <linux/sunrpc/types.h>
33#include <linux/sunrpc/cache.h>
34#include <linux/sunrpc/stats.h>
Trond Myklebust8854e822009-08-09 15:14:30 -040035#include <linux/sunrpc/rpc_pipe_fs.h>
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +040036#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define RPCDBG_FACILITY RPCDBG_CACHE
39
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -050040static bool cache_defer_req(struct cache_req *req, struct cache_head *item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static void cache_revisit_request(struct cache_head *item);
42
Adrian Bunk74cae612006-03-27 01:15:10 -080043static void cache_init(struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
NeilBrownc5b29f82010-08-12 16:55:22 +100045 time_t now = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 h->next = NULL;
47 h->flags = 0;
NeilBrownbaab9352006-03-27 01:15:09 -080048 kref_init(&h->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 h->expiry_time = now + CACHE_NEW_EXPIRY;
50 h->last_refresh = now;
51}
52
NeilBrown2f50d8b2010-02-03 17:31:31 +110053static inline int cache_is_expired(struct cache_detail *detail, struct cache_head *h)
54{
NeilBrownc5b29f82010-08-12 16:55:22 +100055 return (h->expiry_time < seconds_since_boot()) ||
NeilBrown2f50d8b2010-02-03 17:31:31 +110056 (detail->flush_time > h->last_refresh);
57}
58
NeilBrown15a5f6b2006-03-27 01:15:02 -080059struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
60 struct cache_head *key, int hash)
61{
62 struct cache_head **head, **hp;
NeilBrownd202cce2010-02-03 17:31:31 +110063 struct cache_head *new = NULL, *freeme = NULL;
NeilBrown15a5f6b2006-03-27 01:15:02 -080064
65 head = &detail->hash_table[hash];
66
67 read_lock(&detail->hash_lock);
68
69 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
70 struct cache_head *tmp = *hp;
71 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110072 if (cache_is_expired(detail, tmp))
73 /* This entry is expired, we will discard it. */
74 break;
NeilBrown15a5f6b2006-03-27 01:15:02 -080075 cache_get(tmp);
76 read_unlock(&detail->hash_lock);
77 return tmp;
78 }
79 }
80 read_unlock(&detail->hash_lock);
81 /* Didn't find anything, insert an empty entry */
82
83 new = detail->alloc();
84 if (!new)
85 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070086 /* must fully initialise 'new', else
87 * we might get lose if we need to
88 * cache_put it soon.
89 */
NeilBrown15a5f6b2006-03-27 01:15:02 -080090 cache_init(new);
Neil Brown2f349312006-08-05 12:14:29 -070091 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080092
93 write_lock(&detail->hash_lock);
94
95 /* check if entry appeared while we slept */
96 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
97 struct cache_head *tmp = *hp;
98 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110099 if (cache_is_expired(detail, tmp)) {
100 *hp = tmp->next;
101 tmp->next = NULL;
102 detail->entries --;
103 freeme = tmp;
104 break;
105 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800106 cache_get(tmp);
107 write_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800108 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800109 return tmp;
110 }
111 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800112 new->next = *head;
113 *head = new;
114 detail->entries++;
115 cache_get(new);
116 write_unlock(&detail->hash_lock);
117
NeilBrownd202cce2010-02-03 17:31:31 +1100118 if (freeme)
119 cache_put(freeme, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800120 return new;
121}
Trond Myklebust24c37672008-12-23 16:30:12 -0500122EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800123
NeilBrownebd0cb12006-03-27 01:15:08 -0800124
NeilBrownf866a812009-08-04 15:22:38 +1000125static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800126
NeilBrown908329f2009-09-09 16:32:54 +1000127static void cache_fresh_locked(struct cache_head *head, time_t expiry)
NeilBrownebd0cb12006-03-27 01:15:08 -0800128{
129 head->expiry_time = expiry;
NeilBrownc5b29f82010-08-12 16:55:22 +1000130 head->last_refresh = seconds_since_boot();
NeilBrown908329f2009-09-09 16:32:54 +1000131 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800132}
133
134static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000135 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800136{
NeilBrownebd0cb12006-03-27 01:15:08 -0800137 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
138 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000139 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800140 }
141}
142
NeilBrown15a5f6b2006-03-27 01:15:02 -0800143struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
144 struct cache_head *new, struct cache_head *old, int hash)
145{
146 /* The 'old' entry is to be replaced by 'new'.
147 * If 'old' is not VALID, we update it directly,
148 * otherwise we need to replace it
149 */
150 struct cache_head **head;
151 struct cache_head *tmp;
152
153 if (!test_bit(CACHE_VALID, &old->flags)) {
154 write_lock(&detail->hash_lock);
155 if (!test_bit(CACHE_VALID, &old->flags)) {
156 if (test_bit(CACHE_NEGATIVE, &new->flags))
157 set_bit(CACHE_NEGATIVE, &old->flags);
158 else
159 detail->update(old, new);
NeilBrown908329f2009-09-09 16:32:54 +1000160 cache_fresh_locked(old, new->expiry_time);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800161 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000162 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800163 return old;
164 }
165 write_unlock(&detail->hash_lock);
166 }
167 /* We need to insert a new entry */
168 tmp = detail->alloc();
169 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800170 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800171 return NULL;
172 }
173 cache_init(tmp);
174 detail->init(tmp, old);
175 head = &detail->hash_table[hash];
176
177 write_lock(&detail->hash_lock);
178 if (test_bit(CACHE_NEGATIVE, &new->flags))
179 set_bit(CACHE_NEGATIVE, &tmp->flags);
180 else
181 detail->update(tmp, new);
182 tmp->next = *head;
183 *head = tmp;
NeilBrownf2d39582006-05-22 22:35:25 -0700184 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800185 cache_get(tmp);
NeilBrown908329f2009-09-09 16:32:54 +1000186 cache_fresh_locked(tmp, new->expiry_time);
NeilBrownebd0cb12006-03-27 01:15:08 -0800187 cache_fresh_locked(old, 0);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800188 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000189 cache_fresh_unlocked(tmp, detail);
190 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800191 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800192 return tmp;
193}
Trond Myklebust24c37672008-12-23 16:30:12 -0500194EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400196static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
197{
198 if (!cd->cache_upcall)
199 return -EINVAL;
200 return cd->cache_upcall(cd, h);
201}
NeilBrown989a19b2009-08-04 15:22:38 +1000202
203static inline int cache_is_valid(struct cache_detail *detail, struct cache_head *h)
204{
NeilBrownd202cce2010-02-03 17:31:31 +1100205 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000206 return -EAGAIN;
207 else {
208 /* entry is valid */
209 if (test_bit(CACHE_NEGATIVE, &h->flags))
210 return -ENOENT;
211 else
212 return 0;
213 }
214}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400215
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500216static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
217{
218 int rv;
219
220 write_lock(&detail->hash_lock);
221 rv = cache_is_valid(detail, h);
222 if (rv != -EAGAIN) {
223 write_unlock(&detail->hash_lock);
224 return rv;
225 }
226 set_bit(CACHE_NEGATIVE, &h->flags);
227 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
228 write_unlock(&detail->hash_lock);
229 cache_fresh_unlocked(h, detail);
230 return -ENOENT;
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*
234 * This is the generic cache management routine for all
235 * the authentication caches.
236 * It checks the currency of a cache item and will (later)
237 * initiate an upcall to fill it if needed.
238 *
239 *
240 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000241 * -EAGAIN if upcall is pending and request has been queued
242 * -ETIMEDOUT if upcall failed or request could not be queue or
243 * upcall completed but item is still invalid (implying that
244 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * -ENOENT if cache entry was negative
246 */
247int cache_check(struct cache_detail *detail,
248 struct cache_head *h, struct cache_req *rqstp)
249{
250 int rv;
251 long refresh_age, age;
252
253 /* First decide return status as best we can */
NeilBrown989a19b2009-08-04 15:22:38 +1000254 rv = cache_is_valid(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 /* now see if we want to start an upcall */
257 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000258 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (rqstp == NULL) {
261 if (rv == -EAGAIN)
262 rv = -ENOENT;
263 } else if (rv == -EAGAIN || age > refresh_age/2) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500264 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
265 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
267 switch (cache_make_upcall(detail, h)) {
268 case -EINVAL:
269 clear_bit(CACHE_PENDING, &h->flags);
NeilBrown5c4d2632009-08-04 15:22:38 +1000270 cache_revisit_request(h);
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500271 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 case -EAGAIN:
274 clear_bit(CACHE_PENDING, &h->flags);
275 cache_revisit_request(h);
276 break;
277 }
278 }
279 }
280
NeilBrown989a19b2009-08-04 15:22:38 +1000281 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500282 if (!cache_defer_req(rqstp, h)) {
283 /*
284 * Request was not deferred; handle it as best
285 * we can ourselves:
286 */
NeilBrown989a19b2009-08-04 15:22:38 +1000287 rv = cache_is_valid(detail, h);
288 if (rv == -EAGAIN)
289 rv = -ETIMEDOUT;
290 }
291 }
NeilBrown4013ede2006-03-27 01:15:07 -0800292 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800293 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return rv;
295}
Trond Myklebust24c37672008-12-23 16:30:12 -0500296EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/*
299 * caches need to be periodically cleaned.
300 * For this we maintain a list of cache_detail and
301 * a current pointer into that list and into the table
302 * for that entry.
303 *
304 * Each time clean_cache is called it finds the next non-empty entry
305 * in the current table and walks the list in that entry
306 * looking for entries that can be removed.
307 *
308 * An entry gets removed if:
309 * - The expiry is before current time
310 * - The last_refresh time is before the flush_time for that cache
311 *
312 * later we might drop old entries with non-NEVER expiry if that table
313 * is getting 'full' for some definition of 'full'
314 *
315 * The question of "how often to scan a table" is an interesting one
316 * and is answered in part by the use of the "nextcheck" field in the
317 * cache_detail.
318 * When a scan of a table begins, the nextcheck field is set to a time
319 * that is well into the future.
320 * While scanning, if an expiry time is found that is earlier than the
321 * current nextcheck time, nextcheck is set to that expiry time.
322 * If the flush_time is ever set to a time earlier than the nextcheck
323 * time, the nextcheck time is then set to that flush_time.
324 *
325 * A table is then only scanned if the current time is at least
326 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800327 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
329
330static LIST_HEAD(cache_list);
331static DEFINE_SPINLOCK(cache_list_lock);
332static struct cache_detail *current_detail;
333static int current_index;
334
David Howells65f27f32006-11-22 14:55:48 +0000335static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300336static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400338static void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500339{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 rwlock_init(&cd->hash_lock);
341 INIT_LIST_HEAD(&cd->queue);
342 spin_lock(&cache_list_lock);
343 cd->nextcheck = 0;
344 cd->entries = 0;
345 atomic_set(&cd->readers, 0);
346 cd->last_close = 0;
347 cd->last_warn = -1;
348 list_add(&cd->others, &cache_list);
349 spin_unlock(&cache_list_lock);
350
351 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000352 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400355static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 cache_purge(cd);
358 spin_lock(&cache_list_lock);
359 write_lock(&cd->hash_lock);
360 if (cd->entries || atomic_read(&cd->inuse)) {
361 write_unlock(&cd->hash_lock);
362 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500363 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 if (current_detail == cd)
366 current_detail = NULL;
367 list_del_init(&cd->others);
368 write_unlock(&cd->hash_lock);
369 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (list_empty(&cache_list)) {
371 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400372 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500374 return;
375out:
376 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379/* clean cache tries to find something to clean
380 * and cleans it.
381 * It returns 1 if it cleaned something,
382 * 0 if it didn't find anything this time
383 * -1 if it fell off the end of the list.
384 */
385static int cache_clean(void)
386{
387 int rv = 0;
388 struct list_head *next;
389
390 spin_lock(&cache_list_lock);
391
392 /* find a suitable table if we don't already have one */
393 while (current_detail == NULL ||
394 current_index >= current_detail->hash_size) {
395 if (current_detail)
396 next = current_detail->others.next;
397 else
398 next = cache_list.next;
399 if (next == &cache_list) {
400 current_detail = NULL;
401 spin_unlock(&cache_list_lock);
402 return -1;
403 }
404 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000405 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 current_index = current_detail->hash_size;
407 else {
408 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000409 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 }
412
413 /* find a non-empty bucket in the table */
414 while (current_detail &&
415 current_index < current_detail->hash_size &&
416 current_detail->hash_table[current_index] == NULL)
417 current_index++;
418
419 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (current_detail && current_index < current_detail->hash_size) {
422 struct cache_head *ch, **cp;
423 struct cache_detail *d;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 write_lock(&current_detail->hash_lock);
426
427 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 cp = & current_detail->hash_table[current_index];
NeilBrown3af49742010-02-03 17:31:31 +1100430 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (current_detail->nextcheck > ch->expiry_time)
432 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100433 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *cp = ch->next;
437 ch->next = NULL;
438 current_detail->entries--;
439 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100440 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
NeilBrown3af49742010-02-03 17:31:31 +1100442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 write_unlock(&current_detail->hash_lock);
444 d = current_detail;
445 if (!ch)
446 current_index ++;
447 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000448 if (ch) {
NeilBrown3af49742010-02-03 17:31:31 +1100449 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
450 cache_dequeue(current_detail, ch);
NeilBrown5c4d2632009-08-04 15:22:38 +1000451 cache_revisit_request(ch);
NeilBrownbaab9352006-03-27 01:15:09 -0800452 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 } else
455 spin_unlock(&cache_list_lock);
456
457 return rv;
458}
459
460/*
461 * We want to regularly clean the cache, so we need to schedule some work ...
462 */
David Howells65f27f32006-11-22 14:55:48 +0000463static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 int delay = 5;
466 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700467 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 if (list_empty(&cache_list))
470 delay = 0;
471
472 if (delay)
473 schedule_delayed_work(&cache_cleaner, delay);
474}
475
476
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800477/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800479 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * be fully cleaned
481 */
482void cache_flush(void)
483{
484 while (cache_clean() != -1)
485 cond_resched();
486 while (cache_clean() != -1)
487 cond_resched();
488}
Trond Myklebust24c37672008-12-23 16:30:12 -0500489EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491void cache_purge(struct cache_detail *detail)
492{
493 detail->flush_time = LONG_MAX;
NeilBrownc5b29f82010-08-12 16:55:22 +1000494 detail->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 cache_flush();
496 detail->flush_time = 1;
497}
Trond Myklebust24c37672008-12-23 16:30:12 -0500498EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500
501/*
502 * Deferral and Revisiting of Requests.
503 *
504 * If a cache lookup finds a pending entry, we
505 * need to defer the request and revisit it later.
506 * All deferred requests are stored in a hash table,
507 * indexed by "struct cache_head *".
508 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800509 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * deferred form, which must contain a
511 * 'struct cache_deferred_req'
512 * This cache_deferred_req contains a method to allow
513 * it to be revisited when cache info is available
514 */
515
516#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
517#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
518
519#define DFR_MAX 300 /* ??? */
520
521static DEFINE_SPINLOCK(cache_defer_lock);
522static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000523static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524static int cache_defer_cnt;
525
J. Bruce Fields6610f722010-08-26 13:19:52 -0400526static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
NeilBrown11174492010-08-12 17:04:08 +1000528 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100529 if (!list_empty(&dreq->recent)) {
530 list_del_init(&dreq->recent);
531 cache_defer_cnt--;
532 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400533}
534
535static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
536{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int hash = DFR_HASH(item);
538
NeilBrowne33534d2010-10-07 15:29:46 +1100539 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000540 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400541}
542
NeilBrowne33534d2010-10-07 15:29:46 +1100543static void setup_deferral(struct cache_deferred_req *dreq,
544 struct cache_head *item,
545 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 spin_lock(&cache_defer_lock);
551
J. Bruce Fields6610f722010-08-26 13:19:52 -0400552 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
NeilBrowne33534d2010-10-07 15:29:46 +1100554 if (count_me) {
555 cache_defer_cnt++;
556 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
NeilBrowne33534d2010-10-07 15:29:46 +1100558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 spin_unlock(&cache_defer_lock);
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
J. Bruce Fields3211af12010-08-26 16:56:23 -0400563struct thread_deferred_req {
564 struct cache_deferred_req handle;
565 struct completion completion;
566};
567
568static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
569{
570 struct thread_deferred_req *dr =
571 container_of(dreq, struct thread_deferred_req, handle);
572 complete(&dr->completion);
573}
574
NeilBrownd29068c2010-10-07 15:29:46 +1100575static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400576{
577 struct thread_deferred_req sleeper;
578 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400579
580 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
581 dreq->revisit = cache_restart_thread;
582
NeilBrowne33534d2010-10-07 15:29:46 +1100583 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400584
NeilBrownd29068c2010-10-07 15:29:46 +1100585 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000586 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400587 &sleeper.completion, req->thread_wait) <= 0) {
588 /* The completion wasn't completed, so we need
589 * to clean up
590 */
591 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000592 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400593 __unhash_deferred_req(&sleeper.handle);
594 spin_unlock(&cache_defer_lock);
595 } else {
596 /* cache_revisit_request already removed
597 * this from the hash table, but hasn't
598 * called ->revisit yet. It will very soon
599 * and we need to wait for it.
600 */
601 spin_unlock(&cache_defer_lock);
602 wait_for_completion(&sleeper.completion);
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400605}
606
NeilBrowne33534d2010-10-07 15:29:46 +1100607static void cache_limit_defers(void)
608{
609 /* Make sure we haven't exceed the limit of allowed deferred
610 * requests.
611 */
612 struct cache_deferred_req *discard = NULL;
613
614 if (cache_defer_cnt <= DFR_MAX)
615 return;
616
617 spin_lock(&cache_defer_lock);
618
619 /* Consider removing either the first or the last */
620 if (cache_defer_cnt > DFR_MAX) {
621 if (net_random() & 1)
622 discard = list_entry(cache_defer_list.next,
623 struct cache_deferred_req, recent);
624 else
625 discard = list_entry(cache_defer_list.prev,
626 struct cache_deferred_req, recent);
627 __unhash_deferred_req(discard);
628 }
629 spin_unlock(&cache_defer_lock);
630 if (discard)
631 discard->revisit(discard, 1);
632}
633
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500634/* Return true if and only if a deferred request is queued. */
635static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400636{
637 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400638
J. Bruce Fields3211af12010-08-26 16:56:23 -0400639 if (req->thread_wait) {
NeilBrownd29068c2010-10-07 15:29:46 +1100640 cache_wait_req(req, item);
641 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500642 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400643 }
644 dreq = req->defer(req);
645 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500646 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100647 setup_deferral(dreq, item, 1);
NeilBrownd29068c2010-10-07 15:29:46 +1100648 if (!test_bit(CACHE_PENDING, &item->flags))
649 /* Bit could have been cleared before we managed to
650 * set up the deferral, so need to revisit just in case
651 */
652 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100653
654 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500655 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658static void cache_revisit_request(struct cache_head *item)
659{
660 struct cache_deferred_req *dreq;
661 struct list_head pending;
NeilBrown11174492010-08-12 17:04:08 +1000662 struct hlist_node *lp, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int hash = DFR_HASH(item);
664
665 INIT_LIST_HEAD(&pending);
666 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800667
NeilBrown11174492010-08-12 17:04:08 +1000668 hlist_for_each_entry_safe(dreq, lp, tmp, &cache_defer_hash[hash], hash)
669 if (dreq->item == item) {
670 __unhash_deferred_req(dreq);
671 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
NeilBrown11174492010-08-12 17:04:08 +1000673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 spin_unlock(&cache_defer_lock);
675
676 while (!list_empty(&pending)) {
677 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
678 list_del_init(&dreq->recent);
679 dreq->revisit(dreq, 0);
680 }
681}
682
683void cache_clean_deferred(void *owner)
684{
685 struct cache_deferred_req *dreq, *tmp;
686 struct list_head pending;
687
688
689 INIT_LIST_HEAD(&pending);
690 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
693 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400694 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000695 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697 }
698 spin_unlock(&cache_defer_lock);
699
700 while (!list_empty(&pending)) {
701 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
702 list_del_init(&dreq->recent);
703 dreq->revisit(dreq, 1);
704 }
705}
706
707/*
708 * communicate with user-space
709 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500710 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
711 * On read, you get a full request, or block.
712 * On write, an update request is processed.
713 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800715 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500716 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * to the end and may wakeup and preceding readers.
718 * New readers are added to the head. If, on read, an item is found with
719 * CACHE_UPCALLING clear, we free it from the list.
720 *
721 */
722
723static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800724static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726struct cache_queue {
727 struct list_head list;
728 int reader; /* if 0, then request */
729};
730struct cache_request {
731 struct cache_queue q;
732 struct cache_head *item;
733 char * buf;
734 int len;
735 int readers;
736};
737struct cache_reader {
738 struct cache_queue q;
739 int offset; /* if non-0, we have a refcnt on next request */
740};
741
Trond Myklebust173912a2009-08-09 15:14:29 -0400742static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
743 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct cache_reader *rp = filp->private_data;
746 struct cache_request *rq;
Trond Myklebustda770052009-08-09 15:14:28 -0400747 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int err;
749
750 if (count == 0)
751 return 0;
752
Trond Myklebustda770052009-08-09 15:14:28 -0400753 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * readers on this file */
755 again:
756 spin_lock(&queue_lock);
757 /* need to find next request */
758 while (rp->q.list.next != &cd->queue &&
759 list_entry(rp->q.list.next, struct cache_queue, list)
760 ->reader) {
761 struct list_head *next = rp->q.list.next;
762 list_move(&rp->q.list, next);
763 }
764 if (rp->q.list.next == &cd->queue) {
765 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400766 mutex_unlock(&inode->i_mutex);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800767 BUG_ON(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
769 }
770 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800771 BUG_ON(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (rp->offset == 0)
773 rq->readers++;
774 spin_unlock(&queue_lock);
775
776 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
777 err = -EAGAIN;
778 spin_lock(&queue_lock);
779 list_move(&rp->q.list, &rq->q.list);
780 spin_unlock(&queue_lock);
781 } else {
782 if (rp->offset + count > rq->len)
783 count = rq->len - rp->offset;
784 err = -EFAULT;
785 if (copy_to_user(buf, rq->buf + rp->offset, count))
786 goto out;
787 rp->offset += count;
788 if (rp->offset >= rq->len) {
789 rp->offset = 0;
790 spin_lock(&queue_lock);
791 list_move(&rp->q.list, &rq->q.list);
792 spin_unlock(&queue_lock);
793 }
794 err = 0;
795 }
796 out:
797 if (rp->offset == 0) {
798 /* need to release rq */
799 spin_lock(&queue_lock);
800 rq->readers--;
801 if (rq->readers == 0 &&
802 !test_bit(CACHE_PENDING, &rq->item->flags)) {
803 list_del(&rq->q.list);
804 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800805 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 kfree(rq->buf);
807 kfree(rq);
808 } else
809 spin_unlock(&queue_lock);
810 }
811 if (err == -EAGAIN)
812 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400813 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return err ? err : count;
815}
816
Trond Myklebustda770052009-08-09 15:14:28 -0400817static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
818 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Trond Myklebustda770052009-08-09 15:14:28 -0400820 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Trond Myklebustda770052009-08-09 15:14:28 -0400822 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400824 kaddr[count] = '\0';
825 ret = cd->cache_parse(cd, kaddr, count);
826 if (!ret)
827 ret = count;
828 return ret;
829}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Trond Myklebustda770052009-08-09 15:14:28 -0400831static ssize_t cache_slow_downcall(const char __user *buf,
832 size_t count, struct cache_detail *cd)
833{
834 static char write_buf[8192]; /* protected by queue_io_mutex */
835 ssize_t ret = -EINVAL;
836
837 if (count >= sizeof(write_buf))
838 goto out;
839 mutex_lock(&queue_io_mutex);
840 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800841 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400842out:
843 return ret;
844}
845
846static ssize_t cache_downcall(struct address_space *mapping,
847 const char __user *buf,
848 size_t count, struct cache_detail *cd)
849{
850 struct page *page;
851 char *kaddr;
852 ssize_t ret = -ENOMEM;
853
854 if (count >= PAGE_CACHE_SIZE)
855 goto out_slow;
856
857 page = find_or_create_page(mapping, 0, GFP_KERNEL);
858 if (!page)
859 goto out_slow;
860
861 kaddr = kmap(page);
862 ret = cache_do_downcall(kaddr, buf, count, cd);
863 kunmap(page);
864 unlock_page(page);
865 page_cache_release(page);
866 return ret;
867out_slow:
868 return cache_slow_downcall(buf, count, cd);
869}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Trond Myklebust173912a2009-08-09 15:14:29 -0400871static ssize_t cache_write(struct file *filp, const char __user *buf,
872 size_t count, loff_t *ppos,
873 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Trond Myklebustda770052009-08-09 15:14:28 -0400875 struct address_space *mapping = filp->f_mapping;
876 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebustda770052009-08-09 15:14:28 -0400877 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Trond Myklebustda770052009-08-09 15:14:28 -0400879 if (!cd->cache_parse)
880 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Trond Myklebustda770052009-08-09 15:14:28 -0400882 mutex_lock(&inode->i_mutex);
883 ret = cache_downcall(mapping, buf, count, cd);
884 mutex_unlock(&inode->i_mutex);
885out:
886 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
890
Trond Myklebust173912a2009-08-09 15:14:29 -0400891static unsigned int cache_poll(struct file *filp, poll_table *wait,
892 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 unsigned int mask;
895 struct cache_reader *rp = filp->private_data;
896 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 poll_wait(filp, &queue_wait, wait);
899
900 /* alway allow write */
901 mask = POLL_OUT | POLLWRNORM;
902
903 if (!rp)
904 return mask;
905
906 spin_lock(&queue_lock);
907
908 for (cq= &rp->q; &cq->list != &cd->queue;
909 cq = list_entry(cq->list.next, struct cache_queue, list))
910 if (!cq->reader) {
911 mask |= POLLIN | POLLRDNORM;
912 break;
913 }
914 spin_unlock(&queue_lock);
915 return mask;
916}
917
Trond Myklebust173912a2009-08-09 15:14:29 -0400918static int cache_ioctl(struct inode *ino, struct file *filp,
919 unsigned int cmd, unsigned long arg,
920 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 int len = 0;
923 struct cache_reader *rp = filp->private_data;
924 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (cmd != FIONREAD || !rp)
927 return -EINVAL;
928
929 spin_lock(&queue_lock);
930
931 /* only find the length remaining in current request,
932 * or the length of the next request
933 */
934 for (cq= &rp->q; &cq->list != &cd->queue;
935 cq = list_entry(cq->list.next, struct cache_queue, list))
936 if (!cq->reader) {
937 struct cache_request *cr =
938 container_of(cq, struct cache_request, q);
939 len = cr->len - rp->offset;
940 break;
941 }
942 spin_unlock(&queue_lock);
943
944 return put_user(len, (int __user *)arg);
945}
946
Trond Myklebust173912a2009-08-09 15:14:29 -0400947static int cache_open(struct inode *inode, struct file *filp,
948 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 struct cache_reader *rp = NULL;
951
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400952 if (!cd || !try_module_get(cd->owner))
953 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 nonseekable_open(inode, filp);
955 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
957 if (!rp)
958 return -ENOMEM;
959 rp->offset = 0;
960 rp->q.reader = 1;
961 atomic_inc(&cd->readers);
962 spin_lock(&queue_lock);
963 list_add(&rp->q.list, &cd->queue);
964 spin_unlock(&queue_lock);
965 }
966 filp->private_data = rp;
967 return 0;
968}
969
Trond Myklebust173912a2009-08-09 15:14:29 -0400970static int cache_release(struct inode *inode, struct file *filp,
971 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (rp) {
976 spin_lock(&queue_lock);
977 if (rp->offset) {
978 struct cache_queue *cq;
979 for (cq= &rp->q; &cq->list != &cd->queue;
980 cq = list_entry(cq->list.next, struct cache_queue, list))
981 if (!cq->reader) {
982 container_of(cq, struct cache_request, q)
983 ->readers--;
984 break;
985 }
986 rp->offset = 0;
987 }
988 list_del(&rp->q.list);
989 spin_unlock(&queue_lock);
990
991 filp->private_data = NULL;
992 kfree(rp);
993
NeilBrownc5b29f82010-08-12 16:55:22 +1000994 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 atomic_dec(&cd->readers);
996 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400997 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return 0;
999}
1000
1001
1002
NeilBrownf866a812009-08-04 15:22:38 +10001003static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct cache_queue *cq;
1006 spin_lock(&queue_lock);
1007 list_for_each_entry(cq, &detail->queue, list)
1008 if (!cq->reader) {
1009 struct cache_request *cr = container_of(cq, struct cache_request, q);
1010 if (cr->item != ch)
1011 continue;
1012 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001013 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 list_del(&cr->q.list);
1015 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -08001016 cache_put(cr->item, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 kfree(cr->buf);
1018 kfree(cr);
1019 return;
1020 }
1021 spin_unlock(&queue_lock);
1022}
1023
1024/*
1025 * Support routines for text-based upcalls.
1026 * Fields are separated by spaces.
1027 * Fields are either mangled to quote space tab newline slosh with slosh
1028 * or a hexified with a leading \x
1029 * Record is terminated with newline.
1030 *
1031 */
1032
1033void qword_add(char **bpp, int *lp, char *str)
1034{
1035 char *bp = *bpp;
1036 int len = *lp;
1037 char c;
1038
1039 if (len < 0) return;
1040
1041 while ((c=*str++) && len)
1042 switch(c) {
1043 case ' ':
1044 case '\t':
1045 case '\n':
1046 case '\\':
1047 if (len >= 4) {
1048 *bp++ = '\\';
1049 *bp++ = '0' + ((c & 0300)>>6);
1050 *bp++ = '0' + ((c & 0070)>>3);
1051 *bp++ = '0' + ((c & 0007)>>0);
1052 }
1053 len -= 4;
1054 break;
1055 default:
1056 *bp++ = c;
1057 len--;
1058 }
1059 if (c || len <1) len = -1;
1060 else {
1061 *bp++ = ' ';
1062 len--;
1063 }
1064 *bpp = bp;
1065 *lp = len;
1066}
Trond Myklebust24c37672008-12-23 16:30:12 -05001067EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1070{
1071 char *bp = *bpp;
1072 int len = *lp;
1073
1074 if (len < 0) return;
1075
1076 if (len > 2) {
1077 *bp++ = '\\';
1078 *bp++ = 'x';
1079 len -= 2;
1080 while (blen && len >= 2) {
1081 unsigned char c = *buf++;
1082 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1083 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1084 len -= 2;
1085 blen--;
1086 }
1087 }
1088 if (blen || len<1) len = -1;
1089 else {
1090 *bp++ = ' ';
1091 len--;
1092 }
1093 *bpp = bp;
1094 *lp = len;
1095}
Trond Myklebust24c37672008-12-23 16:30:12 -05001096EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098static void warn_no_listener(struct cache_detail *detail)
1099{
1100 if (detail->last_warn != detail->last_close) {
1101 detail->last_warn = detail->last_close;
1102 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001103 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105}
1106
J. Bruce Fields06497522010-09-19 22:55:06 -04001107static bool cache_listeners_exist(struct cache_detail *detail)
1108{
1109 if (atomic_read(&detail->readers))
1110 return true;
1111 if (detail->last_close == 0)
1112 /* This cache was never opened */
1113 return false;
1114 if (detail->last_close < seconds_since_boot() - 30)
1115 /*
1116 * We allow for the possibility that someone might
1117 * restart a userspace daemon without restarting the
1118 * server; but after 30 seconds, we give up.
1119 */
1120 return false;
1121 return true;
1122}
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001125 * register an upcall request to user-space and queue it up for read() by the
1126 * upcall daemon.
1127 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 * Each request is at most one page long.
1129 */
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001130int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1131 void (*cache_request)(struct cache_detail *,
1132 struct cache_head *,
1133 char **,
1134 int *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136
1137 char *buf;
1138 struct cache_request *crq;
1139 char *bp;
1140 int len;
1141
J. Bruce Fields06497522010-09-19 22:55:06 -04001142 if (!cache_listeners_exist(detail)) {
1143 warn_no_listener(detail);
1144 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
1147 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1148 if (!buf)
1149 return -EAGAIN;
1150
1151 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1152 if (!crq) {
1153 kfree(buf);
1154 return -EAGAIN;
1155 }
1156
1157 bp = buf; len = PAGE_SIZE;
1158
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001159 cache_request(detail, h, &bp, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 if (len < 0) {
1162 kfree(buf);
1163 kfree(crq);
1164 return -EAGAIN;
1165 }
1166 crq->q.reader = 0;
1167 crq->item = cache_get(h);
1168 crq->buf = buf;
1169 crq->len = PAGE_SIZE - len;
1170 crq->readers = 0;
1171 spin_lock(&queue_lock);
1172 list_add_tail(&crq->q.list, &detail->queue);
1173 spin_unlock(&queue_lock);
1174 wake_up(&queue_wait);
1175 return 0;
1176}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001177EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179/*
1180 * parse a message from user-space and pass it
1181 * to an appropriate cache
1182 * Messages are, like requests, separated into fields by
1183 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1184 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001185 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 * reply cachename expiry key ... content....
1187 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001188 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
1190
1191#define isodigit(c) (isdigit(c) && c <= '7')
1192int qword_get(char **bpp, char *dest, int bufsize)
1193{
1194 /* return bytes copied, or -1 on error */
1195 char *bp = *bpp;
1196 int len = 0;
1197
1198 while (*bp == ' ') bp++;
1199
1200 if (bp[0] == '\\' && bp[1] == 'x') {
1201 /* HEX STRING */
1202 bp += 2;
Andy Shevchenkoe7f483ea2010-09-21 09:40:25 +03001203 while (len < bufsize) {
1204 int h, l;
1205
1206 h = hex_to_bin(bp[0]);
1207 if (h < 0)
1208 break;
1209
1210 l = hex_to_bin(bp[1]);
1211 if (l < 0)
1212 break;
1213
1214 *dest++ = (h << 4) | l;
1215 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 len++;
1217 }
1218 } else {
1219 /* text with \nnn octal quoting */
1220 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1221 if (*bp == '\\' &&
1222 isodigit(bp[1]) && (bp[1] <= '3') &&
1223 isodigit(bp[2]) &&
1224 isodigit(bp[3])) {
1225 int byte = (*++bp -'0');
1226 bp++;
1227 byte = (byte << 3) | (*bp++ - '0');
1228 byte = (byte << 3) | (*bp++ - '0');
1229 *dest++ = byte;
1230 len++;
1231 } else {
1232 *dest++ = *bp++;
1233 len++;
1234 }
1235 }
1236 }
1237
1238 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1239 return -1;
1240 while (*bp == ' ') bp++;
1241 *bpp = bp;
1242 *dest = '\0';
1243 return len;
1244}
Trond Myklebust24c37672008-12-23 16:30:12 -05001245EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247
1248/*
1249 * support /proc/sunrpc/cache/$CACHENAME/content
1250 * as a seqfile.
1251 * We call ->cache_show passing NULL for the item to
1252 * get a header, then pass each real item in the cache
1253 */
1254
1255struct handle {
1256 struct cache_detail *cd;
1257};
1258
1259static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001260 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 loff_t n = *pos;
1263 unsigned hash, entry;
1264 struct cache_head *ch;
1265 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 read_lock(&cd->hash_lock);
1269 if (!n--)
1270 return SEQ_START_TOKEN;
1271 hash = n >> 32;
1272 entry = n & ((1LL<<32) - 1);
1273
1274 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1275 if (!entry--)
1276 return ch;
1277 n &= ~((1LL<<32) - 1);
1278 do {
1279 hash++;
1280 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001281 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 cd->hash_table[hash]==NULL);
1283 if (hash >= cd->hash_size)
1284 return NULL;
1285 *pos = n+1;
1286 return cd->hash_table[hash];
1287}
1288
1289static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1290{
1291 struct cache_head *ch = p;
1292 int hash = (*pos >> 32);
1293 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1294
1295 if (p == SEQ_START_TOKEN)
1296 hash = 0;
1297 else if (ch->next == NULL) {
1298 hash++;
1299 *pos += 1LL<<32;
1300 } else {
1301 ++*pos;
1302 return ch->next;
1303 }
1304 *pos &= ~((1LL<<32) - 1);
1305 while (hash < cd->hash_size &&
1306 cd->hash_table[hash] == NULL) {
1307 hash++;
1308 *pos += 1LL<<32;
1309 }
1310 if (hash >= cd->hash_size)
1311 return NULL;
1312 ++*pos;
1313 return cd->hash_table[hash];
1314}
1315
1316static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001317 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1320 read_unlock(&cd->hash_lock);
1321}
1322
1323static int c_show(struct seq_file *m, void *p)
1324{
1325 struct cache_head *cp = p;
1326 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1327
1328 if (p == SEQ_START_TOKEN)
1329 return cd->cache_show(m, cd, NULL);
1330
1331 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001332 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001333 convert_to_wallclock(cp->expiry_time),
1334 atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 cache_get(cp);
1336 if (cache_check(cd, cp, NULL))
1337 /* cache_check does a cache_put on failure */
1338 seq_printf(m, "# ");
1339 else
1340 cache_put(cp, cd);
1341
1342 return cd->cache_show(m, cd, cp);
1343}
1344
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001345static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 .start = c_start,
1347 .next = c_next,
1348 .stop = c_stop,
1349 .show = c_show,
1350};
1351
Trond Myklebust173912a2009-08-09 15:14:29 -04001352static int content_open(struct inode *inode, struct file *file,
1353 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 struct handle *han;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001357 if (!cd || !try_module_get(cd->owner))
1358 return -EACCES;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001359 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Li Zefana5990ea2010-03-11 14:08:10 -08001360 if (han == NULL) {
1361 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return -ENOMEM;
Li Zefana5990ea2010-03-11 14:08:10 -08001363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001369static int content_release(struct inode *inode, struct file *file,
1370 struct cache_detail *cd)
1371{
1372 int ret = seq_release_private(inode, file);
1373 module_put(cd->owner);
1374 return ret;
1375}
1376
1377static int open_flush(struct inode *inode, struct file *file,
1378 struct cache_detail *cd)
1379{
1380 if (!cd || !try_module_get(cd->owner))
1381 return -EACCES;
1382 return nonseekable_open(inode, file);
1383}
1384
1385static int release_flush(struct inode *inode, struct file *file,
1386 struct cache_detail *cd)
1387{
1388 module_put(cd->owner);
1389 return 0;
1390}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001393 size_t count, loff_t *ppos,
1394 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 char tbuf[20];
1397 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001398 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
NeilBrownc5b29f82010-08-12 16:55:22 +10001400 sprintf(tbuf, "%lu\n", convert_to_wallclock(cd->flush_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 len = strlen(tbuf);
1402 if (p >= len)
1403 return 0;
1404 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001405 if (len > count)
1406 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001408 return -EFAULT;
1409 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return len;
1411}
1412
Trond Myklebust173912a2009-08-09 15:14:29 -04001413static ssize_t write_flush(struct file *file, const char __user *buf,
1414 size_t count, loff_t *ppos,
1415 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 char tbuf[20];
NeilBrownc5b29f82010-08-12 16:55:22 +10001418 char *bp, *ep;
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (*ppos || count > sizeof(tbuf)-1)
1421 return -EINVAL;
1422 if (copy_from_user(tbuf, buf, count))
1423 return -EFAULT;
1424 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001425 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (*ep && *ep != '\n')
1427 return -EINVAL;
1428
NeilBrownc5b29f82010-08-12 16:55:22 +10001429 bp = tbuf;
1430 cd->flush_time = get_expiry(&bp);
1431 cd->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 cache_flush();
1433
1434 *ppos += count;
1435 return count;
1436}
1437
Trond Myklebust173912a2009-08-09 15:14:29 -04001438static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1439 size_t count, loff_t *ppos)
1440{
1441 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1442
1443 return cache_read(filp, buf, count, ppos, cd);
1444}
1445
1446static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1447 size_t count, loff_t *ppos)
1448{
1449 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1450
1451 return cache_write(filp, buf, count, ppos, cd);
1452}
1453
1454static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1455{
1456 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1457
1458 return cache_poll(filp, wait, cd);
1459}
1460
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001461static long cache_ioctl_procfs(struct file *filp,
1462 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001463{
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001464 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebust173912a2009-08-09 15:14:29 -04001465 struct cache_detail *cd = PDE(inode)->data;
1466
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001467 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001468}
1469
1470static int cache_open_procfs(struct inode *inode, struct file *filp)
1471{
1472 struct cache_detail *cd = PDE(inode)->data;
1473
1474 return cache_open(inode, filp, cd);
1475}
1476
1477static int cache_release_procfs(struct inode *inode, struct file *filp)
1478{
1479 struct cache_detail *cd = PDE(inode)->data;
1480
1481 return cache_release(inode, filp, cd);
1482}
1483
1484static const struct file_operations cache_file_operations_procfs = {
1485 .owner = THIS_MODULE,
1486 .llseek = no_llseek,
1487 .read = cache_read_procfs,
1488 .write = cache_write_procfs,
1489 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001490 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001491 .open = cache_open_procfs,
1492 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493};
Trond Myklebust173912a2009-08-09 15:14:29 -04001494
1495static int content_open_procfs(struct inode *inode, struct file *filp)
1496{
1497 struct cache_detail *cd = PDE(inode)->data;
1498
1499 return content_open(inode, filp, cd);
1500}
1501
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001502static int content_release_procfs(struct inode *inode, struct file *filp)
1503{
1504 struct cache_detail *cd = PDE(inode)->data;
1505
1506 return content_release(inode, filp, cd);
1507}
1508
Trond Myklebust173912a2009-08-09 15:14:29 -04001509static const struct file_operations content_file_operations_procfs = {
1510 .open = content_open_procfs,
1511 .read = seq_read,
1512 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001513 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001514};
1515
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001516static int open_flush_procfs(struct inode *inode, struct file *filp)
1517{
1518 struct cache_detail *cd = PDE(inode)->data;
1519
1520 return open_flush(inode, filp, cd);
1521}
1522
1523static int release_flush_procfs(struct inode *inode, struct file *filp)
1524{
1525 struct cache_detail *cd = PDE(inode)->data;
1526
1527 return release_flush(inode, filp, cd);
1528}
1529
Trond Myklebust173912a2009-08-09 15:14:29 -04001530static ssize_t read_flush_procfs(struct file *filp, 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 read_flush(filp, buf, count, ppos, cd);
1536}
1537
1538static ssize_t write_flush_procfs(struct file *filp,
1539 const char __user *buf,
1540 size_t count, loff_t *ppos)
1541{
1542 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1543
1544 return write_flush(filp, buf, count, ppos, cd);
1545}
1546
1547static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001548 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001549 .read = read_flush_procfs,
1550 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001551 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001552 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001553};
1554
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001555static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001556{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001557 struct sunrpc_net *sn;
1558
Trond Myklebust173912a2009-08-09 15:14:29 -04001559 if (cd->u.procfs.proc_ent == NULL)
1560 return;
1561 if (cd->u.procfs.flush_ent)
1562 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1563 if (cd->u.procfs.channel_ent)
1564 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1565 if (cd->u.procfs.content_ent)
1566 remove_proc_entry("content", cd->u.procfs.proc_ent);
1567 cd->u.procfs.proc_ent = NULL;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001568 sn = net_generic(net, sunrpc_net_id);
1569 remove_proc_entry(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001570}
1571
1572#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001573static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001574{
1575 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001576 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001577
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001578 sn = net_generic(net, sunrpc_net_id);
1579 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001580 if (cd->u.procfs.proc_ent == NULL)
1581 goto out_nomem;
1582 cd->u.procfs.channel_ent = NULL;
1583 cd->u.procfs.content_ent = NULL;
1584
1585 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1586 cd->u.procfs.proc_ent,
1587 &cache_flush_operations_procfs, cd);
1588 cd->u.procfs.flush_ent = p;
1589 if (p == NULL)
1590 goto out_nomem;
1591
1592 if (cd->cache_upcall || cd->cache_parse) {
1593 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1594 cd->u.procfs.proc_ent,
1595 &cache_file_operations_procfs, cd);
1596 cd->u.procfs.channel_ent = p;
1597 if (p == NULL)
1598 goto out_nomem;
1599 }
1600 if (cd->cache_show) {
1601 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1602 cd->u.procfs.proc_ent,
1603 &content_file_operations_procfs, cd);
1604 cd->u.procfs.content_ent = p;
1605 if (p == NULL)
1606 goto out_nomem;
1607 }
1608 return 0;
1609out_nomem:
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001610 remove_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001611 return -ENOMEM;
1612}
1613#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001614static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001615{
1616 return 0;
1617}
1618#endif
1619
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001620void __init cache_initialize(void)
1621{
1622 INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner, do_cache_clean);
1623}
1624
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001625int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001626{
1627 int ret;
1628
1629 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001630 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001631 if (ret)
1632 sunrpc_destroy_cache_detail(cd);
1633 return ret;
1634}
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001635
1636int cache_register(struct cache_detail *cd)
1637{
1638 return cache_register_net(cd, &init_net);
1639}
Trond Myklebust173912a2009-08-09 15:14:29 -04001640EXPORT_SYMBOL_GPL(cache_register);
1641
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001642void cache_unregister_net(struct cache_detail *cd, struct net *net)
1643{
1644 remove_cache_proc_entries(cd, net);
1645 sunrpc_destroy_cache_detail(cd);
1646}
1647
Trond Myklebust173912a2009-08-09 15:14:29 -04001648void cache_unregister(struct cache_detail *cd)
1649{
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001650 cache_unregister_net(cd, &init_net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001651}
1652EXPORT_SYMBOL_GPL(cache_unregister);
Trond Myklebust8854e822009-08-09 15:14:30 -04001653
1654static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1655 size_t count, loff_t *ppos)
1656{
1657 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1658
1659 return cache_read(filp, buf, count, ppos, cd);
1660}
1661
1662static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1663 size_t count, loff_t *ppos)
1664{
1665 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1666
1667 return cache_write(filp, buf, count, ppos, cd);
1668}
1669
1670static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1671{
1672 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1673
1674 return cache_poll(filp, wait, cd);
1675}
1676
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001677static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001678 unsigned int cmd, unsigned long arg)
1679{
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001680 struct inode *inode = filp->f_dentry->d_inode;
Trond Myklebust8854e822009-08-09 15:14:30 -04001681 struct cache_detail *cd = RPC_I(inode)->private;
1682
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001683 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001684}
1685
1686static int cache_open_pipefs(struct inode *inode, struct file *filp)
1687{
1688 struct cache_detail *cd = RPC_I(inode)->private;
1689
1690 return cache_open(inode, filp, cd);
1691}
1692
1693static int cache_release_pipefs(struct inode *inode, struct file *filp)
1694{
1695 struct cache_detail *cd = RPC_I(inode)->private;
1696
1697 return cache_release(inode, filp, cd);
1698}
1699
1700const struct file_operations cache_file_operations_pipefs = {
1701 .owner = THIS_MODULE,
1702 .llseek = no_llseek,
1703 .read = cache_read_pipefs,
1704 .write = cache_write_pipefs,
1705 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001706 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001707 .open = cache_open_pipefs,
1708 .release = cache_release_pipefs,
1709};
1710
1711static int content_open_pipefs(struct inode *inode, struct file *filp)
1712{
1713 struct cache_detail *cd = RPC_I(inode)->private;
1714
1715 return content_open(inode, filp, cd);
1716}
1717
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001718static int content_release_pipefs(struct inode *inode, struct file *filp)
1719{
1720 struct cache_detail *cd = RPC_I(inode)->private;
1721
1722 return content_release(inode, filp, cd);
1723}
1724
Trond Myklebust8854e822009-08-09 15:14:30 -04001725const struct file_operations content_file_operations_pipefs = {
1726 .open = content_open_pipefs,
1727 .read = seq_read,
1728 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001729 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001730};
1731
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001732static int open_flush_pipefs(struct inode *inode, struct file *filp)
1733{
1734 struct cache_detail *cd = RPC_I(inode)->private;
1735
1736 return open_flush(inode, filp, cd);
1737}
1738
1739static int release_flush_pipefs(struct inode *inode, struct file *filp)
1740{
1741 struct cache_detail *cd = RPC_I(inode)->private;
1742
1743 return release_flush(inode, filp, cd);
1744}
1745
Trond Myklebust8854e822009-08-09 15:14:30 -04001746static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1747 size_t count, loff_t *ppos)
1748{
1749 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1750
1751 return read_flush(filp, buf, count, ppos, cd);
1752}
1753
1754static ssize_t write_flush_pipefs(struct file *filp,
1755 const char __user *buf,
1756 size_t count, loff_t *ppos)
1757{
1758 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1759
1760 return write_flush(filp, buf, count, ppos, cd);
1761}
1762
1763const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001764 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001765 .read = read_flush_pipefs,
1766 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001767 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001768 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001769};
1770
1771int sunrpc_cache_register_pipefs(struct dentry *parent,
1772 const char *name, mode_t umode,
1773 struct cache_detail *cd)
1774{
1775 struct qstr q;
1776 struct dentry *dir;
1777 int ret = 0;
1778
1779 sunrpc_init_cache_detail(cd);
1780 q.name = name;
1781 q.len = strlen(name);
1782 q.hash = full_name_hash(q.name, q.len);
1783 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1784 if (!IS_ERR(dir))
1785 cd->u.pipefs.dir = dir;
1786 else {
1787 sunrpc_destroy_cache_detail(cd);
1788 ret = PTR_ERR(dir);
1789 }
1790 return ret;
1791}
1792EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1793
1794void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1795{
1796 rpc_remove_cache_dir(cd->u.pipefs.dir);
1797 cd->u.pipefs.dir = NULL;
1798 sunrpc_destroy_cache_detail(cd);
1799}
1800EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1801