blob: 5478a015ab003f0f6b62ba48ae7ffcac7770fa51 [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
NeilBrown15a5f6b2006-03-27 01:15:02 -080053struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
54 struct cache_head *key, int hash)
55{
56 struct cache_head **head, **hp;
NeilBrownd202cce2010-02-03 17:31:31 +110057 struct cache_head *new = NULL, *freeme = NULL;
NeilBrown15a5f6b2006-03-27 01:15:02 -080058
59 head = &detail->hash_table[hash];
60
61 read_lock(&detail->hash_lock);
62
63 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
64 struct cache_head *tmp = *hp;
65 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110066 if (cache_is_expired(detail, tmp))
67 /* This entry is expired, we will discard it. */
68 break;
NeilBrown15a5f6b2006-03-27 01:15:02 -080069 cache_get(tmp);
70 read_unlock(&detail->hash_lock);
71 return tmp;
72 }
73 }
74 read_unlock(&detail->hash_lock);
75 /* Didn't find anything, insert an empty entry */
76
77 new = detail->alloc();
78 if (!new)
79 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070080 /* must fully initialise 'new', else
81 * we might get lose if we need to
82 * cache_put it soon.
83 */
NeilBrown15a5f6b2006-03-27 01:15:02 -080084 cache_init(new);
Neil Brown2f349312006-08-05 12:14:29 -070085 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080086
87 write_lock(&detail->hash_lock);
88
89 /* check if entry appeared while we slept */
90 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
91 struct cache_head *tmp = *hp;
92 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110093 if (cache_is_expired(detail, tmp)) {
94 *hp = tmp->next;
95 tmp->next = NULL;
96 detail->entries --;
97 freeme = tmp;
98 break;
99 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800100 cache_get(tmp);
101 write_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800102 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800103 return tmp;
104 }
105 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800106 new->next = *head;
107 *head = new;
108 detail->entries++;
109 cache_get(new);
110 write_unlock(&detail->hash_lock);
111
NeilBrownd202cce2010-02-03 17:31:31 +1100112 if (freeme)
113 cache_put(freeme, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800114 return new;
115}
Trond Myklebust24c37672008-12-23 16:30:12 -0500116EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800117
NeilBrownebd0cb12006-03-27 01:15:08 -0800118
NeilBrownf866a812009-08-04 15:22:38 +1000119static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800120
NeilBrown908329f2009-09-09 16:32:54 +1000121static void cache_fresh_locked(struct cache_head *head, time_t expiry)
NeilBrownebd0cb12006-03-27 01:15:08 -0800122{
123 head->expiry_time = expiry;
NeilBrownc5b29f82010-08-12 16:55:22 +1000124 head->last_refresh = seconds_since_boot();
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500125 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
NeilBrown908329f2009-09-09 16:32:54 +1000126 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800127}
128
129static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000130 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800131{
NeilBrownebd0cb12006-03-27 01:15:08 -0800132 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
133 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000134 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800135 }
136}
137
NeilBrown15a5f6b2006-03-27 01:15:02 -0800138struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
139 struct cache_head *new, struct cache_head *old, int hash)
140{
141 /* The 'old' entry is to be replaced by 'new'.
142 * If 'old' is not VALID, we update it directly,
143 * otherwise we need to replace it
144 */
145 struct cache_head **head;
146 struct cache_head *tmp;
147
148 if (!test_bit(CACHE_VALID, &old->flags)) {
149 write_lock(&detail->hash_lock);
150 if (!test_bit(CACHE_VALID, &old->flags)) {
151 if (test_bit(CACHE_NEGATIVE, &new->flags))
152 set_bit(CACHE_NEGATIVE, &old->flags);
153 else
154 detail->update(old, new);
NeilBrown908329f2009-09-09 16:32:54 +1000155 cache_fresh_locked(old, new->expiry_time);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800156 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000157 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800158 return old;
159 }
160 write_unlock(&detail->hash_lock);
161 }
162 /* We need to insert a new entry */
163 tmp = detail->alloc();
164 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800165 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800166 return NULL;
167 }
168 cache_init(tmp);
169 detail->init(tmp, old);
170 head = &detail->hash_table[hash];
171
172 write_lock(&detail->hash_lock);
173 if (test_bit(CACHE_NEGATIVE, &new->flags))
174 set_bit(CACHE_NEGATIVE, &tmp->flags);
175 else
176 detail->update(tmp, new);
177 tmp->next = *head;
178 *head = tmp;
NeilBrownf2d39582006-05-22 22:35:25 -0700179 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800180 cache_get(tmp);
NeilBrown908329f2009-09-09 16:32:54 +1000181 cache_fresh_locked(tmp, new->expiry_time);
NeilBrownebd0cb12006-03-27 01:15:08 -0800182 cache_fresh_locked(old, 0);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800183 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000184 cache_fresh_unlocked(tmp, detail);
185 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800186 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800187 return tmp;
188}
Trond Myklebust24c37672008-12-23 16:30:12 -0500189EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400191static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
192{
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +0300193 if (cd->cache_upcall)
194 return cd->cache_upcall(cd, h);
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +0300195 return sunrpc_cache_pipe_upcall(cd, h);
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400196}
NeilBrown989a19b2009-08-04 15:22:38 +1000197
chaoting fanb6040f92013-03-28 22:19:45 +0800198static inline int cache_is_valid(struct cache_head *h)
NeilBrown989a19b2009-08-04 15:22:38 +1000199{
NeilBrownd202cce2010-02-03 17:31:31 +1100200 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000201 return -EAGAIN;
202 else {
203 /* entry is valid */
204 if (test_bit(CACHE_NEGATIVE, &h->flags))
205 return -ENOENT;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500206 else {
207 /*
208 * In combination with write barrier in
209 * sunrpc_cache_update, ensures that anyone
210 * using the cache entry after this sees the
211 * updated contents:
212 */
213 smp_rmb();
NeilBrown989a19b2009-08-04 15:22:38 +1000214 return 0;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500215 }
NeilBrown989a19b2009-08-04 15:22:38 +1000216 }
217}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400218
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500219static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
220{
221 int rv;
222
223 write_lock(&detail->hash_lock);
chaoting fanb6040f92013-03-28 22:19:45 +0800224 rv = cache_is_valid(h);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000225 if (rv == -EAGAIN) {
226 set_bit(CACHE_NEGATIVE, &h->flags);
227 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
228 rv = -ENOENT;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500229 }
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500230 write_unlock(&detail->hash_lock);
231 cache_fresh_unlocked(h, detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000232 return rv;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*
236 * This is the generic cache management routine for all
237 * the authentication caches.
238 * It checks the currency of a cache item and will (later)
239 * initiate an upcall to fill it if needed.
240 *
241 *
242 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000243 * -EAGAIN if upcall is pending and request has been queued
244 * -ETIMEDOUT if upcall failed or request could not be queue or
245 * upcall completed but item is still invalid (implying that
246 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * -ENOENT if cache entry was negative
248 */
249int cache_check(struct cache_detail *detail,
250 struct cache_head *h, struct cache_req *rqstp)
251{
252 int rv;
253 long refresh_age, age;
254
255 /* First decide return status as best we can */
chaoting fanb6040f92013-03-28 22:19:45 +0800256 rv = cache_is_valid(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* now see if we want to start an upcall */
259 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000260 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (rqstp == NULL) {
263 if (rv == -EAGAIN)
264 rv = -ENOENT;
265 } else if (rv == -EAGAIN || age > refresh_age/2) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500266 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
267 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
269 switch (cache_make_upcall(detail, h)) {
270 case -EINVAL:
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:
NeilBrown2a1c7f52013-06-13 12:53:42 +1000274 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276 }
277 }
278 }
279
NeilBrown989a19b2009-08-04 15:22:38 +1000280 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500281 if (!cache_defer_req(rqstp, h)) {
282 /*
283 * Request was not deferred; handle it as best
284 * we can ourselves:
285 */
chaoting fanb6040f92013-03-28 22:19:45 +0800286 rv = cache_is_valid(h);
NeilBrown989a19b2009-08-04 15:22:38 +1000287 if (rv == -EAGAIN)
288 rv = -ETIMEDOUT;
289 }
290 }
NeilBrown4013ede2006-03-27 01:15:07 -0800291 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800292 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return rv;
294}
Trond Myklebust24c37672008-12-23 16:30:12 -0500295EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
298 * caches need to be periodically cleaned.
299 * For this we maintain a list of cache_detail and
300 * a current pointer into that list and into the table
301 * for that entry.
302 *
NeilBrown013920e2013-06-13 12:53:42 +1000303 * Each time cache_clean is called it finds the next non-empty entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * in the current table and walks the list in that entry
305 * looking for entries that can be removed.
306 *
307 * An entry gets removed if:
308 * - The expiry is before current time
309 * - The last_refresh time is before the flush_time for that cache
310 *
311 * later we might drop old entries with non-NEVER expiry if that table
312 * is getting 'full' for some definition of 'full'
313 *
314 * The question of "how often to scan a table" is an interesting one
315 * and is answered in part by the use of the "nextcheck" field in the
316 * cache_detail.
317 * When a scan of a table begins, the nextcheck field is set to a time
318 * that is well into the future.
319 * While scanning, if an expiry time is found that is earlier than the
320 * current nextcheck time, nextcheck is set to that expiry time.
321 * If the flush_time is ever set to a time earlier than the nextcheck
322 * time, the nextcheck time is then set to that flush_time.
323 *
324 * A table is then only scanned if the current time is at least
325 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800326 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
328
329static LIST_HEAD(cache_list);
330static DEFINE_SPINLOCK(cache_list_lock);
331static struct cache_detail *current_detail;
332static int current_index;
333
David Howells65f27f32006-11-22 14:55:48 +0000334static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300335static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300337void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500338{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 rwlock_init(&cd->hash_lock);
340 INIT_LIST_HEAD(&cd->queue);
341 spin_lock(&cache_list_lock);
342 cd->nextcheck = 0;
343 cd->entries = 0;
344 atomic_set(&cd->readers, 0);
345 cd->last_close = 0;
346 cd->last_warn = -1;
347 list_add(&cd->others, &cache_list);
348 spin_unlock(&cache_list_lock);
349
350 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000351 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300353EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300355void 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}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300378EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380/* clean cache tries to find something to clean
381 * and cleans it.
382 * It returns 1 if it cleaned something,
383 * 0 if it didn't find anything this time
384 * -1 if it fell off the end of the list.
385 */
386static int cache_clean(void)
387{
388 int rv = 0;
389 struct list_head *next;
390
391 spin_lock(&cache_list_lock);
392
393 /* find a suitable table if we don't already have one */
394 while (current_detail == NULL ||
395 current_index >= current_detail->hash_size) {
396 if (current_detail)
397 next = current_detail->others.next;
398 else
399 next = cache_list.next;
400 if (next == &cache_list) {
401 current_detail = NULL;
402 spin_unlock(&cache_list_lock);
403 return -1;
404 }
405 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000406 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 current_index = current_detail->hash_size;
408 else {
409 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000410 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 }
413
414 /* find a non-empty bucket in the table */
415 while (current_detail &&
416 current_index < current_detail->hash_size &&
417 current_detail->hash_table[current_index] == NULL)
418 current_index++;
419
420 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (current_detail && current_index < current_detail->hash_size) {
423 struct cache_head *ch, **cp;
424 struct cache_detail *d;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 write_lock(&current_detail->hash_lock);
427
428 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 cp = & current_detail->hash_table[current_index];
NeilBrown3af49742010-02-03 17:31:31 +1100431 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (current_detail->nextcheck > ch->expiry_time)
433 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100434 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *cp = ch->next;
438 ch->next = NULL;
439 current_detail->entries--;
440 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
NeilBrown3af49742010-02-03 17:31:31 +1100443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 write_unlock(&current_detail->hash_lock);
445 d = current_detail;
446 if (!ch)
447 current_index ++;
448 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000449 if (ch) {
NeilBrown013920e2013-06-13 12:53:42 +1000450 set_bit(CACHE_CLEANED, &ch->flags);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000451 cache_fresh_unlocked(ch, d);
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;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800662 struct hlist_node *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
Sasha Levinb67bfe02013-02-27 17:06:00 -0800668 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000669 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
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300742static int cache_request(struct cache_detail *detail,
743 struct cache_request *crq)
744{
745 char *bp = crq->buf;
746 int len = PAGE_SIZE;
747
748 detail->cache_request(detail, crq->item, &bp, &len);
749 if (len < 0)
750 return -EAGAIN;
751 return PAGE_SIZE - len;
752}
753
Trond Myklebust173912a2009-08-09 15:14:29 -0400754static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
755 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct cache_reader *rp = filp->private_data;
758 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500759 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int err;
761
762 if (count == 0)
763 return 0;
764
Trond Myklebustda770052009-08-09 15:14:28 -0400765 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 * readers on this file */
767 again:
768 spin_lock(&queue_lock);
769 /* need to find next request */
770 while (rp->q.list.next != &cd->queue &&
771 list_entry(rp->q.list.next, struct cache_queue, list)
772 ->reader) {
773 struct list_head *next = rp->q.list.next;
774 list_move(&rp->q.list, next);
775 }
776 if (rp->q.list.next == &cd->queue) {
777 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400778 mutex_unlock(&inode->i_mutex);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400779 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return 0;
781 }
782 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400783 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (rp->offset == 0)
785 rq->readers++;
786 spin_unlock(&queue_lock);
787
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300788 if (rq->len == 0) {
789 err = cache_request(cd, rq);
790 if (err < 0)
791 goto out;
792 rq->len = err;
793 }
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
796 err = -EAGAIN;
797 spin_lock(&queue_lock);
798 list_move(&rp->q.list, &rq->q.list);
799 spin_unlock(&queue_lock);
800 } else {
801 if (rp->offset + count > rq->len)
802 count = rq->len - rp->offset;
803 err = -EFAULT;
804 if (copy_to_user(buf, rq->buf + rp->offset, count))
805 goto out;
806 rp->offset += count;
807 if (rp->offset >= rq->len) {
808 rp->offset = 0;
809 spin_lock(&queue_lock);
810 list_move(&rp->q.list, &rq->q.list);
811 spin_unlock(&queue_lock);
812 }
813 err = 0;
814 }
815 out:
816 if (rp->offset == 0) {
817 /* need to release rq */
818 spin_lock(&queue_lock);
819 rq->readers--;
820 if (rq->readers == 0 &&
821 !test_bit(CACHE_PENDING, &rq->item->flags)) {
822 list_del(&rq->q.list);
823 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800824 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 kfree(rq->buf);
826 kfree(rq);
827 } else
828 spin_unlock(&queue_lock);
829 }
830 if (err == -EAGAIN)
831 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400832 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return err ? err : count;
834}
835
Trond Myklebustda770052009-08-09 15:14:28 -0400836static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
837 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Trond Myklebustda770052009-08-09 15:14:28 -0400839 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300841 if (count == 0)
842 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400843 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400845 kaddr[count] = '\0';
846 ret = cd->cache_parse(cd, kaddr, count);
847 if (!ret)
848 ret = count;
849 return ret;
850}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Trond Myklebustda770052009-08-09 15:14:28 -0400852static ssize_t cache_slow_downcall(const char __user *buf,
853 size_t count, struct cache_detail *cd)
854{
855 static char write_buf[8192]; /* protected by queue_io_mutex */
856 ssize_t ret = -EINVAL;
857
858 if (count >= sizeof(write_buf))
859 goto out;
860 mutex_lock(&queue_io_mutex);
861 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800862 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400863out:
864 return ret;
865}
866
867static ssize_t cache_downcall(struct address_space *mapping,
868 const char __user *buf,
869 size_t count, struct cache_detail *cd)
870{
871 struct page *page;
872 char *kaddr;
873 ssize_t ret = -ENOMEM;
874
875 if (count >= PAGE_CACHE_SIZE)
876 goto out_slow;
877
878 page = find_or_create_page(mapping, 0, GFP_KERNEL);
879 if (!page)
880 goto out_slow;
881
882 kaddr = kmap(page);
883 ret = cache_do_downcall(kaddr, buf, count, cd);
884 kunmap(page);
885 unlock_page(page);
886 page_cache_release(page);
887 return ret;
888out_slow:
889 return cache_slow_downcall(buf, count, cd);
890}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Trond Myklebust173912a2009-08-09 15:14:29 -0400892static ssize_t cache_write(struct file *filp, const char __user *buf,
893 size_t count, loff_t *ppos,
894 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Trond Myklebustda770052009-08-09 15:14:28 -0400896 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500897 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400898 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Trond Myklebustda770052009-08-09 15:14:28 -0400900 if (!cd->cache_parse)
901 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Trond Myklebustda770052009-08-09 15:14:28 -0400903 mutex_lock(&inode->i_mutex);
904 ret = cache_downcall(mapping, buf, count, cd);
905 mutex_unlock(&inode->i_mutex);
906out:
907 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
911
Trond Myklebust173912a2009-08-09 15:14:29 -0400912static unsigned int cache_poll(struct file *filp, poll_table *wait,
913 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 unsigned int mask;
916 struct cache_reader *rp = filp->private_data;
917 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 poll_wait(filp, &queue_wait, wait);
920
921 /* alway allow write */
922 mask = POLL_OUT | POLLWRNORM;
923
924 if (!rp)
925 return mask;
926
927 spin_lock(&queue_lock);
928
929 for (cq= &rp->q; &cq->list != &cd->queue;
930 cq = list_entry(cq->list.next, struct cache_queue, list))
931 if (!cq->reader) {
932 mask |= POLLIN | POLLRDNORM;
933 break;
934 }
935 spin_unlock(&queue_lock);
936 return mask;
937}
938
Trond Myklebust173912a2009-08-09 15:14:29 -0400939static int cache_ioctl(struct inode *ino, struct file *filp,
940 unsigned int cmd, unsigned long arg,
941 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 int len = 0;
944 struct cache_reader *rp = filp->private_data;
945 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 if (cmd != FIONREAD || !rp)
948 return -EINVAL;
949
950 spin_lock(&queue_lock);
951
952 /* only find the length remaining in current request,
953 * or the length of the next request
954 */
955 for (cq= &rp->q; &cq->list != &cd->queue;
956 cq = list_entry(cq->list.next, struct cache_queue, list))
957 if (!cq->reader) {
958 struct cache_request *cr =
959 container_of(cq, struct cache_request, q);
960 len = cr->len - rp->offset;
961 break;
962 }
963 spin_unlock(&queue_lock);
964
965 return put_user(len, (int __user *)arg);
966}
967
Trond Myklebust173912a2009-08-09 15:14:29 -0400968static int cache_open(struct inode *inode, struct file *filp,
969 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 struct cache_reader *rp = NULL;
972
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400973 if (!cd || !try_module_get(cd->owner))
974 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 nonseekable_open(inode, filp);
976 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400978 if (!rp) {
979 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 rp->offset = 0;
983 rp->q.reader = 1;
984 atomic_inc(&cd->readers);
985 spin_lock(&queue_lock);
986 list_add(&rp->q.list, &cd->queue);
987 spin_unlock(&queue_lock);
988 }
989 filp->private_data = rp;
990 return 0;
991}
992
Trond Myklebust173912a2009-08-09 15:14:29 -0400993static int cache_release(struct inode *inode, struct file *filp,
994 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 if (rp) {
999 spin_lock(&queue_lock);
1000 if (rp->offset) {
1001 struct cache_queue *cq;
1002 for (cq= &rp->q; &cq->list != &cd->queue;
1003 cq = list_entry(cq->list.next, struct cache_queue, list))
1004 if (!cq->reader) {
1005 container_of(cq, struct cache_request, q)
1006 ->readers--;
1007 break;
1008 }
1009 rp->offset = 0;
1010 }
1011 list_del(&rp->q.list);
1012 spin_unlock(&queue_lock);
1013
1014 filp->private_data = NULL;
1015 kfree(rp);
1016
NeilBrownc5b29f82010-08-12 16:55:22 +10001017 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 atomic_dec(&cd->readers);
1019 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001020 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return 0;
1022}
1023
1024
1025
NeilBrownf866a812009-08-04 15:22:38 +10001026static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001028 struct cache_queue *cq, *tmp;
1029 struct cache_request *cr;
1030 struct list_head dequeued;
1031
1032 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001034 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001036 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (cr->item != ch)
1038 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001039 if (test_bit(CACHE_PENDING, &ch->flags))
1040 /* Lost a race and it is pending again */
1041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001043 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001044 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001047 while (!list_empty(&dequeued)) {
1048 cr = list_entry(dequeued.next, struct cache_request, q.list);
1049 list_del(&cr->q.list);
1050 cache_put(cr->item, detail);
1051 kfree(cr->buf);
1052 kfree(cr);
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056/*
1057 * Support routines for text-based upcalls.
1058 * Fields are separated by spaces.
1059 * Fields are either mangled to quote space tab newline slosh with slosh
1060 * or a hexified with a leading \x
1061 * Record is terminated with newline.
1062 *
1063 */
1064
1065void qword_add(char **bpp, int *lp, char *str)
1066{
1067 char *bp = *bpp;
1068 int len = *lp;
1069 char c;
1070
1071 if (len < 0) return;
1072
1073 while ((c=*str++) && len)
1074 switch(c) {
1075 case ' ':
1076 case '\t':
1077 case '\n':
1078 case '\\':
1079 if (len >= 4) {
1080 *bp++ = '\\';
1081 *bp++ = '0' + ((c & 0300)>>6);
1082 *bp++ = '0' + ((c & 0070)>>3);
1083 *bp++ = '0' + ((c & 0007)>>0);
1084 }
1085 len -= 4;
1086 break;
1087 default:
1088 *bp++ = c;
1089 len--;
1090 }
1091 if (c || len <1) len = -1;
1092 else {
1093 *bp++ = ' ';
1094 len--;
1095 }
1096 *bpp = bp;
1097 *lp = len;
1098}
Trond Myklebust24c37672008-12-23 16:30:12 -05001099EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1102{
1103 char *bp = *bpp;
1104 int len = *lp;
1105
1106 if (len < 0) return;
1107
1108 if (len > 2) {
1109 *bp++ = '\\';
1110 *bp++ = 'x';
1111 len -= 2;
1112 while (blen && len >= 2) {
1113 unsigned char c = *buf++;
1114 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1115 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1116 len -= 2;
1117 blen--;
1118 }
1119 }
1120 if (blen || len<1) len = -1;
1121 else {
1122 *bp++ = ' ';
1123 len--;
1124 }
1125 *bpp = bp;
1126 *lp = len;
1127}
Trond Myklebust24c37672008-12-23 16:30:12 -05001128EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130static void warn_no_listener(struct cache_detail *detail)
1131{
1132 if (detail->last_warn != detail->last_close) {
1133 detail->last_warn = detail->last_close;
1134 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001135 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137}
1138
J. Bruce Fields06497522010-09-19 22:55:06 -04001139static bool cache_listeners_exist(struct cache_detail *detail)
1140{
1141 if (atomic_read(&detail->readers))
1142 return true;
1143 if (detail->last_close == 0)
1144 /* This cache was never opened */
1145 return false;
1146 if (detail->last_close < seconds_since_boot() - 30)
1147 /*
1148 * We allow for the possibility that someone might
1149 * restart a userspace daemon without restarting the
1150 * server; but after 30 seconds, we give up.
1151 */
1152 return false;
1153 return true;
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001157 * register an upcall request to user-space and queue it up for read() by the
1158 * upcall daemon.
1159 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * Each request is at most one page long.
1161 */
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +03001162int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164
1165 char *buf;
1166 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001167 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001169 if (!detail->cache_request)
1170 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
J. Bruce Fields06497522010-09-19 22:55:06 -04001172 if (!cache_listeners_exist(detail)) {
1173 warn_no_listener(detail);
1174 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
NeilBrown013920e2013-06-13 12:53:42 +10001176 if (test_bit(CACHE_CLEANED, &h->flags))
1177 /* Too late to make an upcall */
1178 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1181 if (!buf)
1182 return -EAGAIN;
1183
1184 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1185 if (!crq) {
1186 kfree(buf);
1187 return -EAGAIN;
1188 }
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 crq->q.reader = 0;
1191 crq->item = cache_get(h);
1192 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001193 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 crq->readers = 0;
1195 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001196 if (test_bit(CACHE_PENDING, &h->flags))
1197 list_add_tail(&crq->q.list, &detail->queue);
1198 else
1199 /* Lost a race, no longer PENDING, so don't enqueue */
1200 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 spin_unlock(&queue_lock);
1202 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001203 if (ret == -EAGAIN) {
1204 kfree(buf);
1205 kfree(crq);
1206 }
1207 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001209EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211/*
1212 * parse a message from user-space and pass it
1213 * to an appropriate cache
1214 * Messages are, like requests, separated into fields by
1215 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1216 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001217 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 * reply cachename expiry key ... content....
1219 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001220 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223int qword_get(char **bpp, char *dest, int bufsize)
1224{
1225 /* return bytes copied, or -1 on error */
1226 char *bp = *bpp;
1227 int len = 0;
1228
1229 while (*bp == ' ') bp++;
1230
1231 if (bp[0] == '\\' && bp[1] == 'x') {
1232 /* HEX STRING */
1233 bp += 2;
Andy Shevchenkoe7f483ea2010-09-21 09:40:25 +03001234 while (len < bufsize) {
1235 int h, l;
1236
1237 h = hex_to_bin(bp[0]);
1238 if (h < 0)
1239 break;
1240
1241 l = hex_to_bin(bp[1]);
1242 if (l < 0)
1243 break;
1244
1245 *dest++ = (h << 4) | l;
1246 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 len++;
1248 }
1249 } else {
1250 /* text with \nnn octal quoting */
1251 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1252 if (*bp == '\\' &&
1253 isodigit(bp[1]) && (bp[1] <= '3') &&
1254 isodigit(bp[2]) &&
1255 isodigit(bp[3])) {
1256 int byte = (*++bp -'0');
1257 bp++;
1258 byte = (byte << 3) | (*bp++ - '0');
1259 byte = (byte << 3) | (*bp++ - '0');
1260 *dest++ = byte;
1261 len++;
1262 } else {
1263 *dest++ = *bp++;
1264 len++;
1265 }
1266 }
1267 }
1268
1269 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1270 return -1;
1271 while (*bp == ' ') bp++;
1272 *bpp = bp;
1273 *dest = '\0';
1274 return len;
1275}
Trond Myklebust24c37672008-12-23 16:30:12 -05001276EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278
1279/*
1280 * support /proc/sunrpc/cache/$CACHENAME/content
1281 * as a seqfile.
1282 * We call ->cache_show passing NULL for the item to
1283 * get a header, then pass each real item in the cache
1284 */
1285
1286struct handle {
1287 struct cache_detail *cd;
1288};
1289
1290static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001291 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001294 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct cache_head *ch;
1296 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 read_lock(&cd->hash_lock);
1300 if (!n--)
1301 return SEQ_START_TOKEN;
1302 hash = n >> 32;
1303 entry = n & ((1LL<<32) - 1);
1304
1305 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1306 if (!entry--)
1307 return ch;
1308 n &= ~((1LL<<32) - 1);
1309 do {
1310 hash++;
1311 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001312 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 cd->hash_table[hash]==NULL);
1314 if (hash >= cd->hash_size)
1315 return NULL;
1316 *pos = n+1;
1317 return cd->hash_table[hash];
1318}
1319
1320static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1321{
1322 struct cache_head *ch = p;
1323 int hash = (*pos >> 32);
1324 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1325
1326 if (p == SEQ_START_TOKEN)
1327 hash = 0;
1328 else if (ch->next == NULL) {
1329 hash++;
1330 *pos += 1LL<<32;
1331 } else {
1332 ++*pos;
1333 return ch->next;
1334 }
1335 *pos &= ~((1LL<<32) - 1);
1336 while (hash < cd->hash_size &&
1337 cd->hash_table[hash] == NULL) {
1338 hash++;
1339 *pos += 1LL<<32;
1340 }
1341 if (hash >= cd->hash_size)
1342 return NULL;
1343 ++*pos;
1344 return cd->hash_table[hash];
1345}
1346
1347static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001348 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1351 read_unlock(&cd->hash_lock);
1352}
1353
1354static int c_show(struct seq_file *m, void *p)
1355{
1356 struct cache_head *cp = p;
1357 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1358
1359 if (p == SEQ_START_TOKEN)
1360 return cd->cache_show(m, cd, NULL);
1361
1362 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001363 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001364 convert_to_wallclock(cp->expiry_time),
1365 atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 cache_get(cp);
1367 if (cache_check(cd, cp, NULL))
1368 /* cache_check does a cache_put on failure */
1369 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001370 else {
1371 if (cache_is_expired(cd, cp))
1372 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 return cd->cache_show(m, cd, cp);
1377}
1378
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001379static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 .start = c_start,
1381 .next = c_next,
1382 .stop = c_stop,
1383 .show = c_show,
1384};
1385
Trond Myklebust173912a2009-08-09 15:14:29 -04001386static int content_open(struct inode *inode, struct file *file,
1387 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 struct handle *han;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001391 if (!cd || !try_module_get(cd->owner))
1392 return -EACCES;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001393 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Li Zefana5990ea2010-03-11 14:08:10 -08001394 if (han == NULL) {
1395 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return -ENOMEM;
Li Zefana5990ea2010-03-11 14:08:10 -08001397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001403static int content_release(struct inode *inode, struct file *file,
1404 struct cache_detail *cd)
1405{
1406 int ret = seq_release_private(inode, file);
1407 module_put(cd->owner);
1408 return ret;
1409}
1410
1411static int open_flush(struct inode *inode, struct file *file,
1412 struct cache_detail *cd)
1413{
1414 if (!cd || !try_module_get(cd->owner))
1415 return -EACCES;
1416 return nonseekable_open(inode, file);
1417}
1418
1419static int release_flush(struct inode *inode, struct file *file,
1420 struct cache_detail *cd)
1421{
1422 module_put(cd->owner);
1423 return 0;
1424}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001427 size_t count, loff_t *ppos,
1428 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Sasha Levin212ba902012-07-17 00:01:26 +02001430 char tbuf[22];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001432 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Sasha Levin212ba902012-07-17 00:01:26 +02001434 snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 len = strlen(tbuf);
1436 if (p >= len)
1437 return 0;
1438 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001439 if (len > count)
1440 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001442 return -EFAULT;
1443 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return len;
1445}
1446
Trond Myklebust173912a2009-08-09 15:14:29 -04001447static ssize_t write_flush(struct file *file, const char __user *buf,
1448 size_t count, loff_t *ppos,
1449 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 char tbuf[20];
NeilBrownc5b29f82010-08-12 16:55:22 +10001452 char *bp, *ep;
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (*ppos || count > sizeof(tbuf)-1)
1455 return -EINVAL;
1456 if (copy_from_user(tbuf, buf, count))
1457 return -EFAULT;
1458 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001459 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (*ep && *ep != '\n')
1461 return -EINVAL;
1462
NeilBrownc5b29f82010-08-12 16:55:22 +10001463 bp = tbuf;
1464 cd->flush_time = get_expiry(&bp);
1465 cd->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 cache_flush();
1467
1468 *ppos += count;
1469 return count;
1470}
1471
Trond Myklebust173912a2009-08-09 15:14:29 -04001472static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1473 size_t count, loff_t *ppos)
1474{
Al Virod9dda782013-03-31 18:16:14 -04001475 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001476
1477 return cache_read(filp, buf, count, ppos, cd);
1478}
1479
1480static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1481 size_t count, loff_t *ppos)
1482{
Al Virod9dda782013-03-31 18:16:14 -04001483 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001484
1485 return cache_write(filp, buf, count, ppos, cd);
1486}
1487
1488static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1489{
Al Virod9dda782013-03-31 18:16:14 -04001490 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001491
1492 return cache_poll(filp, wait, cd);
1493}
1494
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001495static long cache_ioctl_procfs(struct file *filp,
1496 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001497{
Al Viro496ad9a2013-01-23 17:07:38 -05001498 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001499 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001500
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001501 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001502}
1503
1504static int cache_open_procfs(struct inode *inode, struct file *filp)
1505{
Al Virod9dda782013-03-31 18:16:14 -04001506 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001507
1508 return cache_open(inode, filp, cd);
1509}
1510
1511static int cache_release_procfs(struct inode *inode, struct file *filp)
1512{
Al Virod9dda782013-03-31 18:16:14 -04001513 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001514
1515 return cache_release(inode, filp, cd);
1516}
1517
1518static const struct file_operations cache_file_operations_procfs = {
1519 .owner = THIS_MODULE,
1520 .llseek = no_llseek,
1521 .read = cache_read_procfs,
1522 .write = cache_write_procfs,
1523 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001524 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001525 .open = cache_open_procfs,
1526 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527};
Trond Myklebust173912a2009-08-09 15:14:29 -04001528
1529static int content_open_procfs(struct inode *inode, struct file *filp)
1530{
Al Virod9dda782013-03-31 18:16:14 -04001531 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001532
1533 return content_open(inode, filp, cd);
1534}
1535
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001536static int content_release_procfs(struct inode *inode, struct file *filp)
1537{
Al Virod9dda782013-03-31 18:16:14 -04001538 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001539
1540 return content_release(inode, filp, cd);
1541}
1542
Trond Myklebust173912a2009-08-09 15:14:29 -04001543static const struct file_operations content_file_operations_procfs = {
1544 .open = content_open_procfs,
1545 .read = seq_read,
1546 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001547 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001548};
1549
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001550static int open_flush_procfs(struct inode *inode, struct file *filp)
1551{
Al Virod9dda782013-03-31 18:16:14 -04001552 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001553
1554 return open_flush(inode, filp, cd);
1555}
1556
1557static int release_flush_procfs(struct inode *inode, struct file *filp)
1558{
Al Virod9dda782013-03-31 18:16:14 -04001559 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001560
1561 return release_flush(inode, filp, cd);
1562}
1563
Trond Myklebust173912a2009-08-09 15:14:29 -04001564static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1565 size_t count, loff_t *ppos)
1566{
Al Virod9dda782013-03-31 18:16:14 -04001567 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001568
1569 return read_flush(filp, buf, count, ppos, cd);
1570}
1571
1572static ssize_t write_flush_procfs(struct file *filp,
1573 const char __user *buf,
1574 size_t count, loff_t *ppos)
1575{
Al Virod9dda782013-03-31 18:16:14 -04001576 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001577
1578 return write_flush(filp, buf, count, ppos, cd);
1579}
1580
1581static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001582 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001583 .read = read_flush_procfs,
1584 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001585 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001586 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001587};
1588
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001589static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001590{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001591 struct sunrpc_net *sn;
1592
Trond Myklebust173912a2009-08-09 15:14:29 -04001593 if (cd->u.procfs.proc_ent == NULL)
1594 return;
1595 if (cd->u.procfs.flush_ent)
1596 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1597 if (cd->u.procfs.channel_ent)
1598 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1599 if (cd->u.procfs.content_ent)
1600 remove_proc_entry("content", cd->u.procfs.proc_ent);
1601 cd->u.procfs.proc_ent = NULL;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001602 sn = net_generic(net, sunrpc_net_id);
1603 remove_proc_entry(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001604}
1605
1606#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001607static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001608{
1609 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001610 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001611
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001612 sn = net_generic(net, sunrpc_net_id);
1613 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001614 if (cd->u.procfs.proc_ent == NULL)
1615 goto out_nomem;
1616 cd->u.procfs.channel_ent = NULL;
1617 cd->u.procfs.content_ent = NULL;
1618
1619 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1620 cd->u.procfs.proc_ent,
1621 &cache_flush_operations_procfs, cd);
1622 cd->u.procfs.flush_ent = p;
1623 if (p == NULL)
1624 goto out_nomem;
1625
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001626 if (cd->cache_request || cd->cache_parse) {
Trond Myklebust173912a2009-08-09 15:14:29 -04001627 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1628 cd->u.procfs.proc_ent,
1629 &cache_file_operations_procfs, cd);
1630 cd->u.procfs.channel_ent = p;
1631 if (p == NULL)
1632 goto out_nomem;
1633 }
1634 if (cd->cache_show) {
Yanchuan Nianec168672013-01-04 19:45:35 +08001635 p = proc_create_data("content", S_IFREG|S_IRUSR,
Trond Myklebust173912a2009-08-09 15:14:29 -04001636 cd->u.procfs.proc_ent,
1637 &content_file_operations_procfs, cd);
1638 cd->u.procfs.content_ent = p;
1639 if (p == NULL)
1640 goto out_nomem;
1641 }
1642 return 0;
1643out_nomem:
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001644 remove_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001645 return -ENOMEM;
1646}
1647#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001648static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001649{
1650 return 0;
1651}
1652#endif
1653
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001654void __init cache_initialize(void)
1655{
Tejun Heo203b42f2012-08-21 13:18:23 -07001656 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001657}
1658
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001659int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001660{
1661 int ret;
1662
1663 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001664 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001665 if (ret)
1666 sunrpc_destroy_cache_detail(cd);
1667 return ret;
1668}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001669EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001670
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001671void cache_unregister_net(struct cache_detail *cd, struct net *net)
1672{
1673 remove_cache_proc_entries(cd, net);
1674 sunrpc_destroy_cache_detail(cd);
1675}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001676EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001677
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001678struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001679{
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001680 struct cache_detail *cd;
1681
1682 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1683 if (cd == NULL)
1684 return ERR_PTR(-ENOMEM);
1685
1686 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct cache_head *),
1687 GFP_KERNEL);
1688 if (cd->hash_table == NULL) {
1689 kfree(cd);
1690 return ERR_PTR(-ENOMEM);
1691 }
1692 cd->net = net;
1693 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001694}
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001695EXPORT_SYMBOL_GPL(cache_create_net);
1696
1697void cache_destroy_net(struct cache_detail *cd, struct net *net)
1698{
1699 kfree(cd->hash_table);
1700 kfree(cd);
1701}
1702EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001703
1704static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1705 size_t count, loff_t *ppos)
1706{
Al Viro496ad9a2013-01-23 17:07:38 -05001707 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001708
1709 return cache_read(filp, buf, count, ppos, cd);
1710}
1711
1712static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1713 size_t count, loff_t *ppos)
1714{
Al Viro496ad9a2013-01-23 17:07:38 -05001715 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001716
1717 return cache_write(filp, buf, count, ppos, cd);
1718}
1719
1720static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1721{
Al Viro496ad9a2013-01-23 17:07:38 -05001722 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001723
1724 return cache_poll(filp, wait, cd);
1725}
1726
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001727static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001728 unsigned int cmd, unsigned long arg)
1729{
Al Viro496ad9a2013-01-23 17:07:38 -05001730 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001731 struct cache_detail *cd = RPC_I(inode)->private;
1732
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001733 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001734}
1735
1736static int cache_open_pipefs(struct inode *inode, struct file *filp)
1737{
1738 struct cache_detail *cd = RPC_I(inode)->private;
1739
1740 return cache_open(inode, filp, cd);
1741}
1742
1743static int cache_release_pipefs(struct inode *inode, struct file *filp)
1744{
1745 struct cache_detail *cd = RPC_I(inode)->private;
1746
1747 return cache_release(inode, filp, cd);
1748}
1749
1750const struct file_operations cache_file_operations_pipefs = {
1751 .owner = THIS_MODULE,
1752 .llseek = no_llseek,
1753 .read = cache_read_pipefs,
1754 .write = cache_write_pipefs,
1755 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001756 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001757 .open = cache_open_pipefs,
1758 .release = cache_release_pipefs,
1759};
1760
1761static int content_open_pipefs(struct inode *inode, struct file *filp)
1762{
1763 struct cache_detail *cd = RPC_I(inode)->private;
1764
1765 return content_open(inode, filp, cd);
1766}
1767
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001768static int content_release_pipefs(struct inode *inode, struct file *filp)
1769{
1770 struct cache_detail *cd = RPC_I(inode)->private;
1771
1772 return content_release(inode, filp, cd);
1773}
1774
Trond Myklebust8854e822009-08-09 15:14:30 -04001775const struct file_operations content_file_operations_pipefs = {
1776 .open = content_open_pipefs,
1777 .read = seq_read,
1778 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001779 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001780};
1781
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001782static int open_flush_pipefs(struct inode *inode, struct file *filp)
1783{
1784 struct cache_detail *cd = RPC_I(inode)->private;
1785
1786 return open_flush(inode, filp, cd);
1787}
1788
1789static int release_flush_pipefs(struct inode *inode, struct file *filp)
1790{
1791 struct cache_detail *cd = RPC_I(inode)->private;
1792
1793 return release_flush(inode, filp, cd);
1794}
1795
Trond Myklebust8854e822009-08-09 15:14:30 -04001796static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1797 size_t count, loff_t *ppos)
1798{
Al Viro496ad9a2013-01-23 17:07:38 -05001799 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001800
1801 return read_flush(filp, buf, count, ppos, cd);
1802}
1803
1804static ssize_t write_flush_pipefs(struct file *filp,
1805 const char __user *buf,
1806 size_t count, loff_t *ppos)
1807{
Al Viro496ad9a2013-01-23 17:07:38 -05001808 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001809
1810 return write_flush(filp, buf, count, ppos, cd);
1811}
1812
1813const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001814 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001815 .read = read_flush_pipefs,
1816 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001817 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001818 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001819};
1820
1821int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001822 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001823 struct cache_detail *cd)
1824{
1825 struct qstr q;
1826 struct dentry *dir;
1827 int ret = 0;
1828
Trond Myklebust8854e822009-08-09 15:14:30 -04001829 q.name = name;
1830 q.len = strlen(name);
1831 q.hash = full_name_hash(q.name, q.len);
1832 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1833 if (!IS_ERR(dir))
1834 cd->u.pipefs.dir = dir;
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +03001835 else
Trond Myklebust8854e822009-08-09 15:14:30 -04001836 ret = PTR_ERR(dir);
Trond Myklebust8854e822009-08-09 15:14:30 -04001837 return ret;
1838}
1839EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1840
1841void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1842{
1843 rpc_remove_cache_dir(cd->u.pipefs.dir);
1844 cd->u.pipefs.dir = NULL;
Trond Myklebust8854e822009-08-09 15:14:30 -04001845}
1846EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1847