blob: e433e7580e27b221eff94d8b95ceaea703440618 [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
NeilBrownd29068c2010-10-07 15:29:46 +110040static void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
217 * This is the generic cache management routine for all
218 * the authentication caches.
219 * It checks the currency of a cache item and will (later)
220 * initiate an upcall to fill it if needed.
221 *
222 *
223 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000224 * -EAGAIN if upcall is pending and request has been queued
225 * -ETIMEDOUT if upcall failed or request could not be queue or
226 * upcall completed but item is still invalid (implying that
227 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * -ENOENT if cache entry was negative
229 */
230int cache_check(struct cache_detail *detail,
231 struct cache_head *h, struct cache_req *rqstp)
232{
233 int rv;
234 long refresh_age, age;
235
236 /* First decide return status as best we can */
NeilBrown989a19b2009-08-04 15:22:38 +1000237 rv = cache_is_valid(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /* now see if we want to start an upcall */
240 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000241 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 if (rqstp == NULL) {
244 if (rv == -EAGAIN)
245 rv = -ENOENT;
246 } else if (rv == -EAGAIN || age > refresh_age/2) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500247 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
248 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
250 switch (cache_make_upcall(detail, h)) {
251 case -EINVAL:
252 clear_bit(CACHE_PENDING, &h->flags);
NeilBrown5c4d2632009-08-04 15:22:38 +1000253 cache_revisit_request(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (rv == -EAGAIN) {
255 set_bit(CACHE_NEGATIVE, &h->flags);
NeilBrownc5b29f82010-08-12 16:55:22 +1000256 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
NeilBrown908329f2009-09-09 16:32:54 +1000257 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 rv = -ENOENT;
259 }
260 break;
261
262 case -EAGAIN:
263 clear_bit(CACHE_PENDING, &h->flags);
264 cache_revisit_request(h);
265 break;
266 }
267 }
268 }
269
NeilBrown989a19b2009-08-04 15:22:38 +1000270 if (rv == -EAGAIN) {
NeilBrownd29068c2010-10-07 15:29:46 +1100271 cache_defer_req(rqstp, h);
272 if (!test_bit(CACHE_PENDING, &h->flags)) {
NeilBrown989a19b2009-08-04 15:22:38 +1000273 /* Request is not deferred */
274 rv = cache_is_valid(detail, h);
275 if (rv == -EAGAIN)
276 rv = -ETIMEDOUT;
277 }
278 }
NeilBrown4013ede2006-03-27 01:15:07 -0800279 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800280 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return rv;
282}
Trond Myklebust24c37672008-12-23 16:30:12 -0500283EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/*
286 * caches need to be periodically cleaned.
287 * For this we maintain a list of cache_detail and
288 * a current pointer into that list and into the table
289 * for that entry.
290 *
291 * Each time clean_cache is called it finds the next non-empty entry
292 * in the current table and walks the list in that entry
293 * looking for entries that can be removed.
294 *
295 * An entry gets removed if:
296 * - The expiry is before current time
297 * - The last_refresh time is before the flush_time for that cache
298 *
299 * later we might drop old entries with non-NEVER expiry if that table
300 * is getting 'full' for some definition of 'full'
301 *
302 * The question of "how often to scan a table" is an interesting one
303 * and is answered in part by the use of the "nextcheck" field in the
304 * cache_detail.
305 * When a scan of a table begins, the nextcheck field is set to a time
306 * that is well into the future.
307 * While scanning, if an expiry time is found that is earlier than the
308 * current nextcheck time, nextcheck is set to that expiry time.
309 * If the flush_time is ever set to a time earlier than the nextcheck
310 * time, the nextcheck time is then set to that flush_time.
311 *
312 * A table is then only scanned if the current time is at least
313 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800314 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
316
317static LIST_HEAD(cache_list);
318static DEFINE_SPINLOCK(cache_list_lock);
319static struct cache_detail *current_detail;
320static int current_index;
321
David Howells65f27f32006-11-22 14:55:48 +0000322static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300323static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400325static void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500326{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 rwlock_init(&cd->hash_lock);
328 INIT_LIST_HEAD(&cd->queue);
329 spin_lock(&cache_list_lock);
330 cd->nextcheck = 0;
331 cd->entries = 0;
332 atomic_set(&cd->readers, 0);
333 cd->last_close = 0;
334 cd->last_warn = -1;
335 list_add(&cd->others, &cache_list);
336 spin_unlock(&cache_list_lock);
337
338 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000339 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Trond Myklebust5b7a1b92009-08-09 15:14:27 -0400342static void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 cache_purge(cd);
345 spin_lock(&cache_list_lock);
346 write_lock(&cd->hash_lock);
347 if (cd->entries || atomic_read(&cd->inuse)) {
348 write_unlock(&cd->hash_lock);
349 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500350 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352 if (current_detail == cd)
353 current_detail = NULL;
354 list_del_init(&cd->others);
355 write_unlock(&cd->hash_lock);
356 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (list_empty(&cache_list)) {
358 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400359 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500361 return;
362out:
363 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366/* clean cache tries to find something to clean
367 * and cleans it.
368 * It returns 1 if it cleaned something,
369 * 0 if it didn't find anything this time
370 * -1 if it fell off the end of the list.
371 */
372static int cache_clean(void)
373{
374 int rv = 0;
375 struct list_head *next;
376
377 spin_lock(&cache_list_lock);
378
379 /* find a suitable table if we don't already have one */
380 while (current_detail == NULL ||
381 current_index >= current_detail->hash_size) {
382 if (current_detail)
383 next = current_detail->others.next;
384 else
385 next = cache_list.next;
386 if (next == &cache_list) {
387 current_detail = NULL;
388 spin_unlock(&cache_list_lock);
389 return -1;
390 }
391 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000392 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 current_index = current_detail->hash_size;
394 else {
395 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000396 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 }
399
400 /* find a non-empty bucket in the table */
401 while (current_detail &&
402 current_index < current_detail->hash_size &&
403 current_detail->hash_table[current_index] == NULL)
404 current_index++;
405
406 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (current_detail && current_index < current_detail->hash_size) {
409 struct cache_head *ch, **cp;
410 struct cache_detail *d;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 write_lock(&current_detail->hash_lock);
413
414 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 cp = & current_detail->hash_table[current_index];
NeilBrown3af49742010-02-03 17:31:31 +1100417 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (current_detail->nextcheck > ch->expiry_time)
419 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100420 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 *cp = ch->next;
424 ch->next = NULL;
425 current_detail->entries--;
426 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
NeilBrown3af49742010-02-03 17:31:31 +1100429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 write_unlock(&current_detail->hash_lock);
431 d = current_detail;
432 if (!ch)
433 current_index ++;
434 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000435 if (ch) {
NeilBrown3af49742010-02-03 17:31:31 +1100436 if (test_and_clear_bit(CACHE_PENDING, &ch->flags))
437 cache_dequeue(current_detail, ch);
NeilBrown5c4d2632009-08-04 15:22:38 +1000438 cache_revisit_request(ch);
NeilBrownbaab9352006-03-27 01:15:09 -0800439 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 } else
442 spin_unlock(&cache_list_lock);
443
444 return rv;
445}
446
447/*
448 * We want to regularly clean the cache, so we need to schedule some work ...
449 */
David Howells65f27f32006-11-22 14:55:48 +0000450static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 int delay = 5;
453 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700454 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (list_empty(&cache_list))
457 delay = 0;
458
459 if (delay)
460 schedule_delayed_work(&cache_cleaner, delay);
461}
462
463
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800464/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800466 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * be fully cleaned
468 */
469void cache_flush(void)
470{
471 while (cache_clean() != -1)
472 cond_resched();
473 while (cache_clean() != -1)
474 cond_resched();
475}
Trond Myklebust24c37672008-12-23 16:30:12 -0500476EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478void cache_purge(struct cache_detail *detail)
479{
480 detail->flush_time = LONG_MAX;
NeilBrownc5b29f82010-08-12 16:55:22 +1000481 detail->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 cache_flush();
483 detail->flush_time = 1;
484}
Trond Myklebust24c37672008-12-23 16:30:12 -0500485EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487
488/*
489 * Deferral and Revisiting of Requests.
490 *
491 * If a cache lookup finds a pending entry, we
492 * need to defer the request and revisit it later.
493 * All deferred requests are stored in a hash table,
494 * indexed by "struct cache_head *".
495 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800496 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * deferred form, which must contain a
498 * 'struct cache_deferred_req'
499 * This cache_deferred_req contains a method to allow
500 * it to be revisited when cache info is available
501 */
502
503#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
504#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
505
506#define DFR_MAX 300 /* ??? */
507
508static DEFINE_SPINLOCK(cache_defer_lock);
509static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000510static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static int cache_defer_cnt;
512
J. Bruce Fields6610f722010-08-26 13:19:52 -0400513static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
NeilBrown11174492010-08-12 17:04:08 +1000515 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100516 if (!list_empty(&dreq->recent)) {
517 list_del_init(&dreq->recent);
518 cache_defer_cnt--;
519 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400520}
521
522static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
523{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int hash = DFR_HASH(item);
525
NeilBrowne33534d2010-10-07 15:29:46 +1100526 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000527 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400528}
529
NeilBrowne33534d2010-10-07 15:29:46 +1100530static void setup_deferral(struct cache_deferred_req *dreq,
531 struct cache_head *item,
532 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 spin_lock(&cache_defer_lock);
538
J. Bruce Fields6610f722010-08-26 13:19:52 -0400539 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
NeilBrowne33534d2010-10-07 15:29:46 +1100541 if (count_me) {
542 cache_defer_cnt++;
543 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
NeilBrowne33534d2010-10-07 15:29:46 +1100545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 spin_unlock(&cache_defer_lock);
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
J. Bruce Fields3211af12010-08-26 16:56:23 -0400550struct thread_deferred_req {
551 struct cache_deferred_req handle;
552 struct completion completion;
553};
554
555static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
556{
557 struct thread_deferred_req *dr =
558 container_of(dreq, struct thread_deferred_req, handle);
559 complete(&dr->completion);
560}
561
NeilBrownd29068c2010-10-07 15:29:46 +1100562static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400563{
564 struct thread_deferred_req sleeper;
565 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400566
567 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
568 dreq->revisit = cache_restart_thread;
569
NeilBrowne33534d2010-10-07 15:29:46 +1100570 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400571
NeilBrownd29068c2010-10-07 15:29:46 +1100572 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000573 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400574 &sleeper.completion, req->thread_wait) <= 0) {
575 /* The completion wasn't completed, so we need
576 * to clean up
577 */
578 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000579 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400580 __unhash_deferred_req(&sleeper.handle);
581 spin_unlock(&cache_defer_lock);
582 } else {
583 /* cache_revisit_request already removed
584 * this from the hash table, but hasn't
585 * called ->revisit yet. It will very soon
586 * and we need to wait for it.
587 */
588 spin_unlock(&cache_defer_lock);
589 wait_for_completion(&sleeper.completion);
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400592}
593
NeilBrowne33534d2010-10-07 15:29:46 +1100594static void cache_limit_defers(void)
595{
596 /* Make sure we haven't exceed the limit of allowed deferred
597 * requests.
598 */
599 struct cache_deferred_req *discard = NULL;
600
601 if (cache_defer_cnt <= DFR_MAX)
602 return;
603
604 spin_lock(&cache_defer_lock);
605
606 /* Consider removing either the first or the last */
607 if (cache_defer_cnt > DFR_MAX) {
608 if (net_random() & 1)
609 discard = list_entry(cache_defer_list.next,
610 struct cache_deferred_req, recent);
611 else
612 discard = list_entry(cache_defer_list.prev,
613 struct cache_deferred_req, recent);
614 __unhash_deferred_req(discard);
615 }
616 spin_unlock(&cache_defer_lock);
617 if (discard)
618 discard->revisit(discard, 1);
619}
620
NeilBrownd29068c2010-10-07 15:29:46 +1100621static void cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400622{
623 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400624
J. Bruce Fields3211af12010-08-26 16:56:23 -0400625 if (req->thread_wait) {
NeilBrownd29068c2010-10-07 15:29:46 +1100626 cache_wait_req(req, item);
627 if (!test_bit(CACHE_PENDING, &item->flags))
628 return;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400629 }
630 dreq = req->defer(req);
631 if (dreq == NULL)
NeilBrownd29068c2010-10-07 15:29:46 +1100632 return;
NeilBrowne33534d2010-10-07 15:29:46 +1100633 setup_deferral(dreq, item, 1);
NeilBrownd29068c2010-10-07 15:29:46 +1100634 if (!test_bit(CACHE_PENDING, &item->flags))
635 /* Bit could have been cleared before we managed to
636 * set up the deferral, so need to revisit just in case
637 */
638 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100639
640 cache_limit_defers();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643static void cache_revisit_request(struct cache_head *item)
644{
645 struct cache_deferred_req *dreq;
646 struct list_head pending;
NeilBrown11174492010-08-12 17:04:08 +1000647 struct hlist_node *lp, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 int hash = DFR_HASH(item);
649
650 INIT_LIST_HEAD(&pending);
651 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800652
NeilBrown11174492010-08-12 17:04:08 +1000653 hlist_for_each_entry_safe(dreq, lp, tmp, &cache_defer_hash[hash], hash)
654 if (dreq->item == item) {
655 __unhash_deferred_req(dreq);
656 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
NeilBrown11174492010-08-12 17:04:08 +1000658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 spin_unlock(&cache_defer_lock);
660
661 while (!list_empty(&pending)) {
662 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
663 list_del_init(&dreq->recent);
664 dreq->revisit(dreq, 0);
665 }
666}
667
668void cache_clean_deferred(void *owner)
669{
670 struct cache_deferred_req *dreq, *tmp;
671 struct list_head pending;
672
673
674 INIT_LIST_HEAD(&pending);
675 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
678 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400679 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000680 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 }
683 spin_unlock(&cache_defer_lock);
684
685 while (!list_empty(&pending)) {
686 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
687 list_del_init(&dreq->recent);
688 dreq->revisit(dreq, 1);
689 }
690}
691
692/*
693 * communicate with user-space
694 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500695 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
696 * On read, you get a full request, or block.
697 * On write, an update request is processed.
698 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800700 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500701 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 * to the end and may wakeup and preceding readers.
703 * New readers are added to the head. If, on read, an item is found with
704 * CACHE_UPCALLING clear, we free it from the list.
705 *
706 */
707
708static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800709static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711struct cache_queue {
712 struct list_head list;
713 int reader; /* if 0, then request */
714};
715struct cache_request {
716 struct cache_queue q;
717 struct cache_head *item;
718 char * buf;
719 int len;
720 int readers;
721};
722struct cache_reader {
723 struct cache_queue q;
724 int offset; /* if non-0, we have a refcnt on next request */
725};
726
Trond Myklebust173912a2009-08-09 15:14:29 -0400727static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
728 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 struct cache_reader *rp = filp->private_data;
731 struct cache_request *rq;
Trond Myklebustda770052009-08-09 15:14:28 -0400732 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int err;
734
735 if (count == 0)
736 return 0;
737
Trond Myklebustda770052009-08-09 15:14:28 -0400738 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * readers on this file */
740 again:
741 spin_lock(&queue_lock);
742 /* need to find next request */
743 while (rp->q.list.next != &cd->queue &&
744 list_entry(rp->q.list.next, struct cache_queue, list)
745 ->reader) {
746 struct list_head *next = rp->q.list.next;
747 list_move(&rp->q.list, next);
748 }
749 if (rp->q.list.next == &cd->queue) {
750 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400751 mutex_unlock(&inode->i_mutex);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800752 BUG_ON(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return 0;
754 }
755 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800756 BUG_ON(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (rp->offset == 0)
758 rq->readers++;
759 spin_unlock(&queue_lock);
760
761 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
762 err = -EAGAIN;
763 spin_lock(&queue_lock);
764 list_move(&rp->q.list, &rq->q.list);
765 spin_unlock(&queue_lock);
766 } else {
767 if (rp->offset + count > rq->len)
768 count = rq->len - rp->offset;
769 err = -EFAULT;
770 if (copy_to_user(buf, rq->buf + rp->offset, count))
771 goto out;
772 rp->offset += count;
773 if (rp->offset >= rq->len) {
774 rp->offset = 0;
775 spin_lock(&queue_lock);
776 list_move(&rp->q.list, &rq->q.list);
777 spin_unlock(&queue_lock);
778 }
779 err = 0;
780 }
781 out:
782 if (rp->offset == 0) {
783 /* need to release rq */
784 spin_lock(&queue_lock);
785 rq->readers--;
786 if (rq->readers == 0 &&
787 !test_bit(CACHE_PENDING, &rq->item->flags)) {
788 list_del(&rq->q.list);
789 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800790 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 kfree(rq->buf);
792 kfree(rq);
793 } else
794 spin_unlock(&queue_lock);
795 }
796 if (err == -EAGAIN)
797 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400798 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return err ? err : count;
800}
801
Trond Myklebustda770052009-08-09 15:14:28 -0400802static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
803 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Trond Myklebustda770052009-08-09 15:14:28 -0400805 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Trond Myklebustda770052009-08-09 15:14:28 -0400807 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400809 kaddr[count] = '\0';
810 ret = cd->cache_parse(cd, kaddr, count);
811 if (!ret)
812 ret = count;
813 return ret;
814}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Trond Myklebustda770052009-08-09 15:14:28 -0400816static ssize_t cache_slow_downcall(const char __user *buf,
817 size_t count, struct cache_detail *cd)
818{
819 static char write_buf[8192]; /* protected by queue_io_mutex */
820 ssize_t ret = -EINVAL;
821
822 if (count >= sizeof(write_buf))
823 goto out;
824 mutex_lock(&queue_io_mutex);
825 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800826 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400827out:
828 return ret;
829}
830
831static ssize_t cache_downcall(struct address_space *mapping,
832 const char __user *buf,
833 size_t count, struct cache_detail *cd)
834{
835 struct page *page;
836 char *kaddr;
837 ssize_t ret = -ENOMEM;
838
839 if (count >= PAGE_CACHE_SIZE)
840 goto out_slow;
841
842 page = find_or_create_page(mapping, 0, GFP_KERNEL);
843 if (!page)
844 goto out_slow;
845
846 kaddr = kmap(page);
847 ret = cache_do_downcall(kaddr, buf, count, cd);
848 kunmap(page);
849 unlock_page(page);
850 page_cache_release(page);
851 return ret;
852out_slow:
853 return cache_slow_downcall(buf, count, cd);
854}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Trond Myklebust173912a2009-08-09 15:14:29 -0400856static ssize_t cache_write(struct file *filp, const char __user *buf,
857 size_t count, loff_t *ppos,
858 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Trond Myklebustda770052009-08-09 15:14:28 -0400860 struct address_space *mapping = filp->f_mapping;
861 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebustda770052009-08-09 15:14:28 -0400862 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Trond Myklebustda770052009-08-09 15:14:28 -0400864 if (!cd->cache_parse)
865 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Trond Myklebustda770052009-08-09 15:14:28 -0400867 mutex_lock(&inode->i_mutex);
868 ret = cache_downcall(mapping, buf, count, cd);
869 mutex_unlock(&inode->i_mutex);
870out:
871 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
875
Trond Myklebust173912a2009-08-09 15:14:29 -0400876static unsigned int cache_poll(struct file *filp, poll_table *wait,
877 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 unsigned int mask;
880 struct cache_reader *rp = filp->private_data;
881 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 poll_wait(filp, &queue_wait, wait);
884
885 /* alway allow write */
886 mask = POLL_OUT | POLLWRNORM;
887
888 if (!rp)
889 return mask;
890
891 spin_lock(&queue_lock);
892
893 for (cq= &rp->q; &cq->list != &cd->queue;
894 cq = list_entry(cq->list.next, struct cache_queue, list))
895 if (!cq->reader) {
896 mask |= POLLIN | POLLRDNORM;
897 break;
898 }
899 spin_unlock(&queue_lock);
900 return mask;
901}
902
Trond Myklebust173912a2009-08-09 15:14:29 -0400903static int cache_ioctl(struct inode *ino, struct file *filp,
904 unsigned int cmd, unsigned long arg,
905 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 int len = 0;
908 struct cache_reader *rp = filp->private_data;
909 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (cmd != FIONREAD || !rp)
912 return -EINVAL;
913
914 spin_lock(&queue_lock);
915
916 /* only find the length remaining in current request,
917 * or the length of the next request
918 */
919 for (cq= &rp->q; &cq->list != &cd->queue;
920 cq = list_entry(cq->list.next, struct cache_queue, list))
921 if (!cq->reader) {
922 struct cache_request *cr =
923 container_of(cq, struct cache_request, q);
924 len = cr->len - rp->offset;
925 break;
926 }
927 spin_unlock(&queue_lock);
928
929 return put_user(len, (int __user *)arg);
930}
931
Trond Myklebust173912a2009-08-09 15:14:29 -0400932static int cache_open(struct inode *inode, struct file *filp,
933 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 struct cache_reader *rp = NULL;
936
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400937 if (!cd || !try_module_get(cd->owner))
938 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 nonseekable_open(inode, filp);
940 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
942 if (!rp)
943 return -ENOMEM;
944 rp->offset = 0;
945 rp->q.reader = 1;
946 atomic_inc(&cd->readers);
947 spin_lock(&queue_lock);
948 list_add(&rp->q.list, &cd->queue);
949 spin_unlock(&queue_lock);
950 }
951 filp->private_data = rp;
952 return 0;
953}
954
Trond Myklebust173912a2009-08-09 15:14:29 -0400955static int cache_release(struct inode *inode, struct file *filp,
956 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (rp) {
961 spin_lock(&queue_lock);
962 if (rp->offset) {
963 struct cache_queue *cq;
964 for (cq= &rp->q; &cq->list != &cd->queue;
965 cq = list_entry(cq->list.next, struct cache_queue, list))
966 if (!cq->reader) {
967 container_of(cq, struct cache_request, q)
968 ->readers--;
969 break;
970 }
971 rp->offset = 0;
972 }
973 list_del(&rp->q.list);
974 spin_unlock(&queue_lock);
975
976 filp->private_data = NULL;
977 kfree(rp);
978
NeilBrownc5b29f82010-08-12 16:55:22 +1000979 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 atomic_dec(&cd->readers);
981 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400982 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return 0;
984}
985
986
987
NeilBrownf866a812009-08-04 15:22:38 +1000988static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 struct cache_queue *cq;
991 spin_lock(&queue_lock);
992 list_for_each_entry(cq, &detail->queue, list)
993 if (!cq->reader) {
994 struct cache_request *cr = container_of(cq, struct cache_request, q);
995 if (cr->item != ch)
996 continue;
997 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -0800998 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 list_del(&cr->q.list);
1000 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -08001001 cache_put(cr->item, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 kfree(cr->buf);
1003 kfree(cr);
1004 return;
1005 }
1006 spin_unlock(&queue_lock);
1007}
1008
1009/*
1010 * Support routines for text-based upcalls.
1011 * Fields are separated by spaces.
1012 * Fields are either mangled to quote space tab newline slosh with slosh
1013 * or a hexified with a leading \x
1014 * Record is terminated with newline.
1015 *
1016 */
1017
1018void qword_add(char **bpp, int *lp, char *str)
1019{
1020 char *bp = *bpp;
1021 int len = *lp;
1022 char c;
1023
1024 if (len < 0) return;
1025
1026 while ((c=*str++) && len)
1027 switch(c) {
1028 case ' ':
1029 case '\t':
1030 case '\n':
1031 case '\\':
1032 if (len >= 4) {
1033 *bp++ = '\\';
1034 *bp++ = '0' + ((c & 0300)>>6);
1035 *bp++ = '0' + ((c & 0070)>>3);
1036 *bp++ = '0' + ((c & 0007)>>0);
1037 }
1038 len -= 4;
1039 break;
1040 default:
1041 *bp++ = c;
1042 len--;
1043 }
1044 if (c || len <1) len = -1;
1045 else {
1046 *bp++ = ' ';
1047 len--;
1048 }
1049 *bpp = bp;
1050 *lp = len;
1051}
Trond Myklebust24c37672008-12-23 16:30:12 -05001052EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1055{
1056 char *bp = *bpp;
1057 int len = *lp;
1058
1059 if (len < 0) return;
1060
1061 if (len > 2) {
1062 *bp++ = '\\';
1063 *bp++ = 'x';
1064 len -= 2;
1065 while (blen && len >= 2) {
1066 unsigned char c = *buf++;
1067 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1068 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1069 len -= 2;
1070 blen--;
1071 }
1072 }
1073 if (blen || len<1) len = -1;
1074 else {
1075 *bp++ = ' ';
1076 len--;
1077 }
1078 *bpp = bp;
1079 *lp = len;
1080}
Trond Myklebust24c37672008-12-23 16:30:12 -05001081EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083static void warn_no_listener(struct cache_detail *detail)
1084{
1085 if (detail->last_warn != detail->last_close) {
1086 detail->last_warn = detail->last_close;
1087 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001088 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090}
1091
J. Bruce Fields06497522010-09-19 22:55:06 -04001092static bool cache_listeners_exist(struct cache_detail *detail)
1093{
1094 if (atomic_read(&detail->readers))
1095 return true;
1096 if (detail->last_close == 0)
1097 /* This cache was never opened */
1098 return false;
1099 if (detail->last_close < seconds_since_boot() - 30)
1100 /*
1101 * We allow for the possibility that someone might
1102 * restart a userspace daemon without restarting the
1103 * server; but after 30 seconds, we give up.
1104 */
1105 return false;
1106 return true;
1107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001110 * register an upcall request to user-space and queue it up for read() by the
1111 * upcall daemon.
1112 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 * Each request is at most one page long.
1114 */
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001115int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,
1116 void (*cache_request)(struct cache_detail *,
1117 struct cache_head *,
1118 char **,
1119 int *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121
1122 char *buf;
1123 struct cache_request *crq;
1124 char *bp;
1125 int len;
1126
J. Bruce Fields06497522010-09-19 22:55:06 -04001127 if (!cache_listeners_exist(detail)) {
1128 warn_no_listener(detail);
1129 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
1132 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1133 if (!buf)
1134 return -EAGAIN;
1135
1136 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1137 if (!crq) {
1138 kfree(buf);
1139 return -EAGAIN;
1140 }
1141
1142 bp = buf; len = PAGE_SIZE;
1143
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001144 cache_request(detail, h, &bp, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 if (len < 0) {
1147 kfree(buf);
1148 kfree(crq);
1149 return -EAGAIN;
1150 }
1151 crq->q.reader = 0;
1152 crq->item = cache_get(h);
1153 crq->buf = buf;
1154 crq->len = PAGE_SIZE - len;
1155 crq->readers = 0;
1156 spin_lock(&queue_lock);
1157 list_add_tail(&crq->q.list, &detail->queue);
1158 spin_unlock(&queue_lock);
1159 wake_up(&queue_wait);
1160 return 0;
1161}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001162EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164/*
1165 * parse a message from user-space and pass it
1166 * to an appropriate cache
1167 * Messages are, like requests, separated into fields by
1168 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1169 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001170 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 * reply cachename expiry key ... content....
1172 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001173 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
1175
1176#define isodigit(c) (isdigit(c) && c <= '7')
1177int qword_get(char **bpp, char *dest, int bufsize)
1178{
1179 /* return bytes copied, or -1 on error */
1180 char *bp = *bpp;
1181 int len = 0;
1182
1183 while (*bp == ' ') bp++;
1184
1185 if (bp[0] == '\\' && bp[1] == 'x') {
1186 /* HEX STRING */
1187 bp += 2;
Andy Shevchenkoe7f483e2010-09-21 09:40:25 +03001188 while (len < bufsize) {
1189 int h, l;
1190
1191 h = hex_to_bin(bp[0]);
1192 if (h < 0)
1193 break;
1194
1195 l = hex_to_bin(bp[1]);
1196 if (l < 0)
1197 break;
1198
1199 *dest++ = (h << 4) | l;
1200 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 len++;
1202 }
1203 } else {
1204 /* text with \nnn octal quoting */
1205 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1206 if (*bp == '\\' &&
1207 isodigit(bp[1]) && (bp[1] <= '3') &&
1208 isodigit(bp[2]) &&
1209 isodigit(bp[3])) {
1210 int byte = (*++bp -'0');
1211 bp++;
1212 byte = (byte << 3) | (*bp++ - '0');
1213 byte = (byte << 3) | (*bp++ - '0');
1214 *dest++ = byte;
1215 len++;
1216 } else {
1217 *dest++ = *bp++;
1218 len++;
1219 }
1220 }
1221 }
1222
1223 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1224 return -1;
1225 while (*bp == ' ') bp++;
1226 *bpp = bp;
1227 *dest = '\0';
1228 return len;
1229}
Trond Myklebust24c37672008-12-23 16:30:12 -05001230EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232
1233/*
1234 * support /proc/sunrpc/cache/$CACHENAME/content
1235 * as a seqfile.
1236 * We call ->cache_show passing NULL for the item to
1237 * get a header, then pass each real item in the cache
1238 */
1239
1240struct handle {
1241 struct cache_detail *cd;
1242};
1243
1244static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001245 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 loff_t n = *pos;
1248 unsigned hash, entry;
1249 struct cache_head *ch;
1250 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 read_lock(&cd->hash_lock);
1254 if (!n--)
1255 return SEQ_START_TOKEN;
1256 hash = n >> 32;
1257 entry = n & ((1LL<<32) - 1);
1258
1259 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1260 if (!entry--)
1261 return ch;
1262 n &= ~((1LL<<32) - 1);
1263 do {
1264 hash++;
1265 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001266 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 cd->hash_table[hash]==NULL);
1268 if (hash >= cd->hash_size)
1269 return NULL;
1270 *pos = n+1;
1271 return cd->hash_table[hash];
1272}
1273
1274static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1275{
1276 struct cache_head *ch = p;
1277 int hash = (*pos >> 32);
1278 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1279
1280 if (p == SEQ_START_TOKEN)
1281 hash = 0;
1282 else if (ch->next == NULL) {
1283 hash++;
1284 *pos += 1LL<<32;
1285 } else {
1286 ++*pos;
1287 return ch->next;
1288 }
1289 *pos &= ~((1LL<<32) - 1);
1290 while (hash < cd->hash_size &&
1291 cd->hash_table[hash] == NULL) {
1292 hash++;
1293 *pos += 1LL<<32;
1294 }
1295 if (hash >= cd->hash_size)
1296 return NULL;
1297 ++*pos;
1298 return cd->hash_table[hash];
1299}
1300
1301static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001302 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1305 read_unlock(&cd->hash_lock);
1306}
1307
1308static int c_show(struct seq_file *m, void *p)
1309{
1310 struct cache_head *cp = p;
1311 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1312
1313 if (p == SEQ_START_TOKEN)
1314 return cd->cache_show(m, cd, NULL);
1315
1316 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001317 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001318 convert_to_wallclock(cp->expiry_time),
1319 atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 cache_get(cp);
1321 if (cache_check(cd, cp, NULL))
1322 /* cache_check does a cache_put on failure */
1323 seq_printf(m, "# ");
1324 else
1325 cache_put(cp, cd);
1326
1327 return cd->cache_show(m, cd, cp);
1328}
1329
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001330static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 .start = c_start,
1332 .next = c_next,
1333 .stop = c_stop,
1334 .show = c_show,
1335};
1336
Trond Myklebust173912a2009-08-09 15:14:29 -04001337static int content_open(struct inode *inode, struct file *file,
1338 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 struct handle *han;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001342 if (!cd || !try_module_get(cd->owner))
1343 return -EACCES;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001344 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Li Zefana5990ea2010-03-11 14:08:10 -08001345 if (han == NULL) {
1346 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -ENOMEM;
Li Zefana5990ea2010-03-11 14:08:10 -08001348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001354static int content_release(struct inode *inode, struct file *file,
1355 struct cache_detail *cd)
1356{
1357 int ret = seq_release_private(inode, file);
1358 module_put(cd->owner);
1359 return ret;
1360}
1361
1362static int open_flush(struct inode *inode, struct file *file,
1363 struct cache_detail *cd)
1364{
1365 if (!cd || !try_module_get(cd->owner))
1366 return -EACCES;
1367 return nonseekable_open(inode, file);
1368}
1369
1370static int release_flush(struct inode *inode, struct file *file,
1371 struct cache_detail *cd)
1372{
1373 module_put(cd->owner);
1374 return 0;
1375}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001378 size_t count, loff_t *ppos,
1379 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 char tbuf[20];
1382 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001383 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
NeilBrownc5b29f82010-08-12 16:55:22 +10001385 sprintf(tbuf, "%lu\n", convert_to_wallclock(cd->flush_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 len = strlen(tbuf);
1387 if (p >= len)
1388 return 0;
1389 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001390 if (len > count)
1391 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001393 return -EFAULT;
1394 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return len;
1396}
1397
Trond Myklebust173912a2009-08-09 15:14:29 -04001398static ssize_t write_flush(struct file *file, const char __user *buf,
1399 size_t count, loff_t *ppos,
1400 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 char tbuf[20];
NeilBrownc5b29f82010-08-12 16:55:22 +10001403 char *bp, *ep;
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (*ppos || count > sizeof(tbuf)-1)
1406 return -EINVAL;
1407 if (copy_from_user(tbuf, buf, count))
1408 return -EFAULT;
1409 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001410 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (*ep && *ep != '\n')
1412 return -EINVAL;
1413
NeilBrownc5b29f82010-08-12 16:55:22 +10001414 bp = tbuf;
1415 cd->flush_time = get_expiry(&bp);
1416 cd->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 cache_flush();
1418
1419 *ppos += count;
1420 return count;
1421}
1422
Trond Myklebust173912a2009-08-09 15:14:29 -04001423static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1424 size_t count, loff_t *ppos)
1425{
1426 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1427
1428 return cache_read(filp, buf, count, ppos, cd);
1429}
1430
1431static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1432 size_t count, loff_t *ppos)
1433{
1434 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1435
1436 return cache_write(filp, buf, count, ppos, cd);
1437}
1438
1439static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1440{
1441 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1442
1443 return cache_poll(filp, wait, cd);
1444}
1445
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001446static long cache_ioctl_procfs(struct file *filp,
1447 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001448{
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001449 struct inode *inode = filp->f_path.dentry->d_inode;
Trond Myklebust173912a2009-08-09 15:14:29 -04001450 struct cache_detail *cd = PDE(inode)->data;
1451
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001452 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001453}
1454
1455static int cache_open_procfs(struct inode *inode, struct file *filp)
1456{
1457 struct cache_detail *cd = PDE(inode)->data;
1458
1459 return cache_open(inode, filp, cd);
1460}
1461
1462static int cache_release_procfs(struct inode *inode, struct file *filp)
1463{
1464 struct cache_detail *cd = PDE(inode)->data;
1465
1466 return cache_release(inode, filp, cd);
1467}
1468
1469static const struct file_operations cache_file_operations_procfs = {
1470 .owner = THIS_MODULE,
1471 .llseek = no_llseek,
1472 .read = cache_read_procfs,
1473 .write = cache_write_procfs,
1474 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001475 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001476 .open = cache_open_procfs,
1477 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478};
Trond Myklebust173912a2009-08-09 15:14:29 -04001479
1480static int content_open_procfs(struct inode *inode, struct file *filp)
1481{
1482 struct cache_detail *cd = PDE(inode)->data;
1483
1484 return content_open(inode, filp, cd);
1485}
1486
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001487static int content_release_procfs(struct inode *inode, struct file *filp)
1488{
1489 struct cache_detail *cd = PDE(inode)->data;
1490
1491 return content_release(inode, filp, cd);
1492}
1493
Trond Myklebust173912a2009-08-09 15:14:29 -04001494static const struct file_operations content_file_operations_procfs = {
1495 .open = content_open_procfs,
1496 .read = seq_read,
1497 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001498 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001499};
1500
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001501static int open_flush_procfs(struct inode *inode, struct file *filp)
1502{
1503 struct cache_detail *cd = PDE(inode)->data;
1504
1505 return open_flush(inode, filp, cd);
1506}
1507
1508static int release_flush_procfs(struct inode *inode, struct file *filp)
1509{
1510 struct cache_detail *cd = PDE(inode)->data;
1511
1512 return release_flush(inode, filp, cd);
1513}
1514
Trond Myklebust173912a2009-08-09 15:14:29 -04001515static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1516 size_t count, loff_t *ppos)
1517{
1518 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1519
1520 return read_flush(filp, buf, count, ppos, cd);
1521}
1522
1523static ssize_t write_flush_procfs(struct file *filp,
1524 const char __user *buf,
1525 size_t count, loff_t *ppos)
1526{
1527 struct cache_detail *cd = PDE(filp->f_path.dentry->d_inode)->data;
1528
1529 return write_flush(filp, buf, count, ppos, cd);
1530}
1531
1532static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001533 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001534 .read = read_flush_procfs,
1535 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001536 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001537 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001538};
1539
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001540static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001541{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001542 struct sunrpc_net *sn;
1543
Trond Myklebust173912a2009-08-09 15:14:29 -04001544 if (cd->u.procfs.proc_ent == NULL)
1545 return;
1546 if (cd->u.procfs.flush_ent)
1547 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1548 if (cd->u.procfs.channel_ent)
1549 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1550 if (cd->u.procfs.content_ent)
1551 remove_proc_entry("content", cd->u.procfs.proc_ent);
1552 cd->u.procfs.proc_ent = NULL;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001553 sn = net_generic(net, sunrpc_net_id);
1554 remove_proc_entry(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001555}
1556
1557#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001558static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001559{
1560 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001561 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001562
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001563 sn = net_generic(net, sunrpc_net_id);
1564 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001565 if (cd->u.procfs.proc_ent == NULL)
1566 goto out_nomem;
1567 cd->u.procfs.channel_ent = NULL;
1568 cd->u.procfs.content_ent = NULL;
1569
1570 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1571 cd->u.procfs.proc_ent,
1572 &cache_flush_operations_procfs, cd);
1573 cd->u.procfs.flush_ent = p;
1574 if (p == NULL)
1575 goto out_nomem;
1576
1577 if (cd->cache_upcall || cd->cache_parse) {
1578 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1579 cd->u.procfs.proc_ent,
1580 &cache_file_operations_procfs, cd);
1581 cd->u.procfs.channel_ent = p;
1582 if (p == NULL)
1583 goto out_nomem;
1584 }
1585 if (cd->cache_show) {
1586 p = proc_create_data("content", S_IFREG|S_IRUSR|S_IWUSR,
1587 cd->u.procfs.proc_ent,
1588 &content_file_operations_procfs, cd);
1589 cd->u.procfs.content_ent = p;
1590 if (p == NULL)
1591 goto out_nomem;
1592 }
1593 return 0;
1594out_nomem:
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001595 remove_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001596 return -ENOMEM;
1597}
1598#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001599static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001600{
1601 return 0;
1602}
1603#endif
1604
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001605void __init cache_initialize(void)
1606{
1607 INIT_DELAYED_WORK_DEFERRABLE(&cache_cleaner, do_cache_clean);
1608}
1609
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001610int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001611{
1612 int ret;
1613
1614 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001615 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001616 if (ret)
1617 sunrpc_destroy_cache_detail(cd);
1618 return ret;
1619}
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001620
1621int cache_register(struct cache_detail *cd)
1622{
1623 return cache_register_net(cd, &init_net);
1624}
Trond Myklebust173912a2009-08-09 15:14:29 -04001625EXPORT_SYMBOL_GPL(cache_register);
1626
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001627void cache_unregister_net(struct cache_detail *cd, struct net *net)
1628{
1629 remove_cache_proc_entries(cd, net);
1630 sunrpc_destroy_cache_detail(cd);
1631}
1632
Trond Myklebust173912a2009-08-09 15:14:29 -04001633void cache_unregister(struct cache_detail *cd)
1634{
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001635 cache_unregister_net(cd, &init_net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001636}
1637EXPORT_SYMBOL_GPL(cache_unregister);
Trond Myklebust8854e822009-08-09 15:14:30 -04001638
1639static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1640 size_t count, loff_t *ppos)
1641{
1642 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1643
1644 return cache_read(filp, buf, count, ppos, cd);
1645}
1646
1647static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1648 size_t count, loff_t *ppos)
1649{
1650 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1651
1652 return cache_write(filp, buf, count, ppos, cd);
1653}
1654
1655static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1656{
1657 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1658
1659 return cache_poll(filp, wait, cd);
1660}
1661
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001662static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001663 unsigned int cmd, unsigned long arg)
1664{
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001665 struct inode *inode = filp->f_dentry->d_inode;
Trond Myklebust8854e822009-08-09 15:14:30 -04001666 struct cache_detail *cd = RPC_I(inode)->private;
1667
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001668 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001669}
1670
1671static int cache_open_pipefs(struct inode *inode, struct file *filp)
1672{
1673 struct cache_detail *cd = RPC_I(inode)->private;
1674
1675 return cache_open(inode, filp, cd);
1676}
1677
1678static int cache_release_pipefs(struct inode *inode, struct file *filp)
1679{
1680 struct cache_detail *cd = RPC_I(inode)->private;
1681
1682 return cache_release(inode, filp, cd);
1683}
1684
1685const struct file_operations cache_file_operations_pipefs = {
1686 .owner = THIS_MODULE,
1687 .llseek = no_llseek,
1688 .read = cache_read_pipefs,
1689 .write = cache_write_pipefs,
1690 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001691 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001692 .open = cache_open_pipefs,
1693 .release = cache_release_pipefs,
1694};
1695
1696static int content_open_pipefs(struct inode *inode, struct file *filp)
1697{
1698 struct cache_detail *cd = RPC_I(inode)->private;
1699
1700 return content_open(inode, filp, cd);
1701}
1702
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001703static int content_release_pipefs(struct inode *inode, struct file *filp)
1704{
1705 struct cache_detail *cd = RPC_I(inode)->private;
1706
1707 return content_release(inode, filp, cd);
1708}
1709
Trond Myklebust8854e822009-08-09 15:14:30 -04001710const struct file_operations content_file_operations_pipefs = {
1711 .open = content_open_pipefs,
1712 .read = seq_read,
1713 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001714 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001715};
1716
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001717static int open_flush_pipefs(struct inode *inode, struct file *filp)
1718{
1719 struct cache_detail *cd = RPC_I(inode)->private;
1720
1721 return open_flush(inode, filp, cd);
1722}
1723
1724static int release_flush_pipefs(struct inode *inode, struct file *filp)
1725{
1726 struct cache_detail *cd = RPC_I(inode)->private;
1727
1728 return release_flush(inode, filp, cd);
1729}
1730
Trond Myklebust8854e822009-08-09 15:14:30 -04001731static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1732 size_t count, loff_t *ppos)
1733{
1734 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1735
1736 return read_flush(filp, buf, count, ppos, cd);
1737}
1738
1739static ssize_t write_flush_pipefs(struct file *filp,
1740 const char __user *buf,
1741 size_t count, loff_t *ppos)
1742{
1743 struct cache_detail *cd = RPC_I(filp->f_path.dentry->d_inode)->private;
1744
1745 return write_flush(filp, buf, count, ppos, cd);
1746}
1747
1748const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001749 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001750 .read = read_flush_pipefs,
1751 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001752 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001753 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001754};
1755
1756int sunrpc_cache_register_pipefs(struct dentry *parent,
1757 const char *name, mode_t umode,
1758 struct cache_detail *cd)
1759{
1760 struct qstr q;
1761 struct dentry *dir;
1762 int ret = 0;
1763
1764 sunrpc_init_cache_detail(cd);
1765 q.name = name;
1766 q.len = strlen(name);
1767 q.hash = full_name_hash(q.name, q.len);
1768 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1769 if (!IS_ERR(dir))
1770 cd->u.pipefs.dir = dir;
1771 else {
1772 sunrpc_destroy_cache_detail(cd);
1773 ret = PTR_ERR(dir);
1774 }
1775 return ret;
1776}
1777EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1778
1779void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1780{
1781 rpc_remove_cache_dir(cd->u.pipefs.dir);
1782 cd->u.pipefs.dir = NULL;
1783 sunrpc_destroy_cache_detail(cd);
1784}
1785EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1786