blob: b12144c5edd0130fd36e4ec48f54b96fcfa75835 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sunrpc/cache.c
3 *
4 * Generic code for various authentication-related caches
5 * used by sunrpc clients and servers.
6 *
7 * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
8 *
9 * Released under terms in GPL version 2. See COPYING.
10 *
11 */
12
13#include <linux/types.h>
14#include <linux/fs.h>
15#include <linux/file.h>
16#include <linux/slab.h>
17#include <linux/signal.h>
18#include <linux/sched.h>
19#include <linux/kmod.h>
20#include <linux/list.h>
21#include <linux/module.h>
22#include <linux/ctype.h>
23#include <asm/uaccess.h>
24#include <linux/poll.h>
25#include <linux/seq_file.h>
26#include <linux/proc_fs.h>
27#include <linux/net.h>
28#include <linux/workqueue.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080029#include <linux/mutex.h>
Trond Myklebustda770052009-08-09 15:14:28 -040030#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/ioctls.h>
32#include <linux/sunrpc/types.h>
33#include <linux/sunrpc/cache.h>
34#include <linux/sunrpc/stats.h>
Trond Myklebust8854e822009-08-09 15:14:30 -040035#include <linux/sunrpc/rpc_pipe_fs.h>
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +040036#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define RPCDBG_FACILITY RPCDBG_CACHE
39
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -050040static bool cache_defer_req(struct cache_req *req, struct cache_head *item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static void cache_revisit_request(struct cache_head *item);
42
Adrian Bunk74cae612006-03-27 01:15:10 -080043static void cache_init(struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
NeilBrownc5b29f82010-08-12 16:55:22 +100045 time_t now = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 h->next = NULL;
47 h->flags = 0;
NeilBrownbaab9352006-03-27 01:15:09 -080048 kref_init(&h->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 h->expiry_time = now + CACHE_NEW_EXPIRY;
50 h->last_refresh = now;
51}
52
NeilBrown2f50d8b2010-02-03 17:31:31 +110053static inline int cache_is_expired(struct cache_detail *detail, struct cache_head *h)
54{
NeilBrownc5b29f82010-08-12 16:55:22 +100055 return (h->expiry_time < seconds_since_boot()) ||
NeilBrown2f50d8b2010-02-03 17:31:31 +110056 (detail->flush_time > h->last_refresh);
57}
58
NeilBrown15a5f6b2006-03-27 01:15:02 -080059struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
60 struct cache_head *key, int hash)
61{
62 struct cache_head **head, **hp;
NeilBrownd202cce2010-02-03 17:31:31 +110063 struct cache_head *new = NULL, *freeme = NULL;
NeilBrown15a5f6b2006-03-27 01:15:02 -080064
65 head = &detail->hash_table[hash];
66
67 read_lock(&detail->hash_lock);
68
69 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
70 struct cache_head *tmp = *hp;
71 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110072 if (cache_is_expired(detail, tmp))
73 /* This entry is expired, we will discard it. */
74 break;
NeilBrown15a5f6b2006-03-27 01:15:02 -080075 cache_get(tmp);
76 read_unlock(&detail->hash_lock);
77 return tmp;
78 }
79 }
80 read_unlock(&detail->hash_lock);
81 /* Didn't find anything, insert an empty entry */
82
83 new = detail->alloc();
84 if (!new)
85 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070086 /* must fully initialise 'new', else
87 * we might get lose if we need to
88 * cache_put it soon.
89 */
NeilBrown15a5f6b2006-03-27 01:15:02 -080090 cache_init(new);
Neil Brown2f349312006-08-05 12:14:29 -070091 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080092
93 write_lock(&detail->hash_lock);
94
95 /* check if entry appeared while we slept */
96 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
97 struct cache_head *tmp = *hp;
98 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110099 if (cache_is_expired(detail, tmp)) {
100 *hp = tmp->next;
101 tmp->next = NULL;
102 detail->entries --;
103 freeme = tmp;
104 break;
105 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800106 cache_get(tmp);
107 write_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800108 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800109 return tmp;
110 }
111 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800112 new->next = *head;
113 *head = new;
114 detail->entries++;
115 cache_get(new);
116 write_unlock(&detail->hash_lock);
117
NeilBrownd202cce2010-02-03 17:31:31 +1100118 if (freeme)
119 cache_put(freeme, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800120 return new;
121}
Trond Myklebust24c37672008-12-23 16:30:12 -0500122EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800123
NeilBrownebd0cb12006-03-27 01:15:08 -0800124
NeilBrownf866a812009-08-04 15:22:38 +1000125static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800126
NeilBrown908329f2009-09-09 16:32:54 +1000127static void cache_fresh_locked(struct cache_head *head, time_t expiry)
NeilBrownebd0cb12006-03-27 01:15:08 -0800128{
129 head->expiry_time = expiry;
NeilBrownc5b29f82010-08-12 16:55:22 +1000130 head->last_refresh = seconds_since_boot();
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500131 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
NeilBrown908329f2009-09-09 16:32:54 +1000132 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800133}
134
135static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000136 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800137{
NeilBrownebd0cb12006-03-27 01:15:08 -0800138 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
139 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000140 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800141 }
142}
143
NeilBrown15a5f6b2006-03-27 01:15:02 -0800144struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
145 struct cache_head *new, struct cache_head *old, int hash)
146{
147 /* The 'old' entry is to be replaced by 'new'.
148 * If 'old' is not VALID, we update it directly,
149 * otherwise we need to replace it
150 */
151 struct cache_head **head;
152 struct cache_head *tmp;
153
154 if (!test_bit(CACHE_VALID, &old->flags)) {
155 write_lock(&detail->hash_lock);
156 if (!test_bit(CACHE_VALID, &old->flags)) {
157 if (test_bit(CACHE_NEGATIVE, &new->flags))
158 set_bit(CACHE_NEGATIVE, &old->flags);
159 else
160 detail->update(old, new);
NeilBrown908329f2009-09-09 16:32:54 +1000161 cache_fresh_locked(old, new->expiry_time);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800162 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000163 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800164 return old;
165 }
166 write_unlock(&detail->hash_lock);
167 }
168 /* We need to insert a new entry */
169 tmp = detail->alloc();
170 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800171 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800172 return NULL;
173 }
174 cache_init(tmp);
175 detail->init(tmp, old);
176 head = &detail->hash_table[hash];
177
178 write_lock(&detail->hash_lock);
179 if (test_bit(CACHE_NEGATIVE, &new->flags))
180 set_bit(CACHE_NEGATIVE, &tmp->flags);
181 else
182 detail->update(tmp, new);
183 tmp->next = *head;
184 *head = tmp;
NeilBrownf2d39582006-05-22 22:35:25 -0700185 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800186 cache_get(tmp);
NeilBrown908329f2009-09-09 16:32:54 +1000187 cache_fresh_locked(tmp, new->expiry_time);
NeilBrownebd0cb12006-03-27 01:15:08 -0800188 cache_fresh_locked(old, 0);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800189 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000190 cache_fresh_unlocked(tmp, detail);
191 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800192 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800193 return tmp;
194}
Trond Myklebust24c37672008-12-23 16:30:12 -0500195EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400197static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
198{
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +0300199 if (cd->cache_upcall)
200 return cd->cache_upcall(cd, h);
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +0300201 return sunrpc_cache_pipe_upcall(cd, h);
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400202}
NeilBrown989a19b2009-08-04 15:22:38 +1000203
chaoting fanb6040f92013-03-28 22:19:45 +0800204static inline int cache_is_valid(struct cache_head *h)
NeilBrown989a19b2009-08-04 15:22:38 +1000205{
NeilBrownd202cce2010-02-03 17:31:31 +1100206 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000207 return -EAGAIN;
208 else {
209 /* entry is valid */
210 if (test_bit(CACHE_NEGATIVE, &h->flags))
211 return -ENOENT;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500212 else {
213 /*
214 * In combination with write barrier in
215 * sunrpc_cache_update, ensures that anyone
216 * using the cache entry after this sees the
217 * updated contents:
218 */
219 smp_rmb();
NeilBrown989a19b2009-08-04 15:22:38 +1000220 return 0;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500221 }
NeilBrown989a19b2009-08-04 15:22:38 +1000222 }
223}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400224
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500225static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
226{
227 int rv;
228
229 write_lock(&detail->hash_lock);
chaoting fanb6040f92013-03-28 22:19:45 +0800230 rv = cache_is_valid(h);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000231 if (rv == -EAGAIN) {
232 set_bit(CACHE_NEGATIVE, &h->flags);
233 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
234 rv = -ENOENT;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500235 }
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500236 write_unlock(&detail->hash_lock);
237 cache_fresh_unlocked(h, detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000238 return rv;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/*
242 * This is the generic cache management routine for all
243 * the authentication caches.
244 * It checks the currency of a cache item and will (later)
245 * initiate an upcall to fill it if needed.
246 *
247 *
248 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000249 * -EAGAIN if upcall is pending and request has been queued
250 * -ETIMEDOUT if upcall failed or request could not be queue or
251 * upcall completed but item is still invalid (implying that
252 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * -ENOENT if cache entry was negative
254 */
255int cache_check(struct cache_detail *detail,
256 struct cache_head *h, struct cache_req *rqstp)
257{
258 int rv;
259 long refresh_age, age;
260
261 /* First decide return status as best we can */
chaoting fanb6040f92013-03-28 22:19:45 +0800262 rv = cache_is_valid(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* now see if we want to start an upcall */
265 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000266 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (rqstp == NULL) {
269 if (rv == -EAGAIN)
270 rv = -ENOENT;
271 } else if (rv == -EAGAIN || age > refresh_age/2) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500272 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
273 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
275 switch (cache_make_upcall(detail, h)) {
276 case -EINVAL:
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500277 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 case -EAGAIN:
NeilBrown2a1c7f52013-06-13 12:53:42 +1000280 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
282 }
283 }
284 }
285
NeilBrown989a19b2009-08-04 15:22:38 +1000286 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500287 if (!cache_defer_req(rqstp, h)) {
288 /*
289 * Request was not deferred; handle it as best
290 * we can ourselves:
291 */
chaoting fanb6040f92013-03-28 22:19:45 +0800292 rv = cache_is_valid(h);
NeilBrown989a19b2009-08-04 15:22:38 +1000293 if (rv == -EAGAIN)
294 rv = -ETIMEDOUT;
295 }
296 }
NeilBrown4013ede2006-03-27 01:15:07 -0800297 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800298 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return rv;
300}
Trond Myklebust24c37672008-12-23 16:30:12 -0500301EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
304 * caches need to be periodically cleaned.
305 * For this we maintain a list of cache_detail and
306 * a current pointer into that list and into the table
307 * for that entry.
308 *
NeilBrown013920e2013-06-13 12:53:42 +1000309 * Each time cache_clean is called it finds the next non-empty entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * in the current table and walks the list in that entry
311 * looking for entries that can be removed.
312 *
313 * An entry gets removed if:
314 * - The expiry is before current time
315 * - The last_refresh time is before the flush_time for that cache
316 *
317 * later we might drop old entries with non-NEVER expiry if that table
318 * is getting 'full' for some definition of 'full'
319 *
320 * The question of "how often to scan a table" is an interesting one
321 * and is answered in part by the use of the "nextcheck" field in the
322 * cache_detail.
323 * When a scan of a table begins, the nextcheck field is set to a time
324 * that is well into the future.
325 * While scanning, if an expiry time is found that is earlier than the
326 * current nextcheck time, nextcheck is set to that expiry time.
327 * If the flush_time is ever set to a time earlier than the nextcheck
328 * time, the nextcheck time is then set to that flush_time.
329 *
330 * A table is then only scanned if the current time is at least
331 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800332 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
334
335static LIST_HEAD(cache_list);
336static DEFINE_SPINLOCK(cache_list_lock);
337static struct cache_detail *current_detail;
338static int current_index;
339
David Howells65f27f32006-11-22 14:55:48 +0000340static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300341static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300343void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500344{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 rwlock_init(&cd->hash_lock);
346 INIT_LIST_HEAD(&cd->queue);
347 spin_lock(&cache_list_lock);
348 cd->nextcheck = 0;
349 cd->entries = 0;
350 atomic_set(&cd->readers, 0);
351 cd->last_close = 0;
352 cd->last_warn = -1;
353 list_add(&cd->others, &cache_list);
354 spin_unlock(&cache_list_lock);
355
356 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000357 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300359EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300361void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 cache_purge(cd);
364 spin_lock(&cache_list_lock);
365 write_lock(&cd->hash_lock);
366 if (cd->entries || atomic_read(&cd->inuse)) {
367 write_unlock(&cd->hash_lock);
368 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500369 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 if (current_detail == cd)
372 current_detail = NULL;
373 list_del_init(&cd->others);
374 write_unlock(&cd->hash_lock);
375 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (list_empty(&cache_list)) {
377 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400378 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500380 return;
381out:
382 printk(KERN_ERR "nfsd: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300384EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386/* clean cache tries to find something to clean
387 * and cleans it.
388 * It returns 1 if it cleaned something,
389 * 0 if it didn't find anything this time
390 * -1 if it fell off the end of the list.
391 */
392static int cache_clean(void)
393{
394 int rv = 0;
395 struct list_head *next;
396
397 spin_lock(&cache_list_lock);
398
399 /* find a suitable table if we don't already have one */
400 while (current_detail == NULL ||
401 current_index >= current_detail->hash_size) {
402 if (current_detail)
403 next = current_detail->others.next;
404 else
405 next = cache_list.next;
406 if (next == &cache_list) {
407 current_detail = NULL;
408 spin_unlock(&cache_list_lock);
409 return -1;
410 }
411 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000412 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 current_index = current_detail->hash_size;
414 else {
415 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000416 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418 }
419
420 /* find a non-empty bucket in the table */
421 while (current_detail &&
422 current_index < current_detail->hash_size &&
423 current_detail->hash_table[current_index] == NULL)
424 current_index++;
425
426 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (current_detail && current_index < current_detail->hash_size) {
429 struct cache_head *ch, **cp;
430 struct cache_detail *d;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 write_lock(&current_detail->hash_lock);
433
434 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 cp = & current_detail->hash_table[current_index];
NeilBrown3af49742010-02-03 17:31:31 +1100437 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (current_detail->nextcheck > ch->expiry_time)
439 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100440 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 *cp = ch->next;
444 ch->next = NULL;
445 current_detail->entries--;
446 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
NeilBrown3af49742010-02-03 17:31:31 +1100449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 write_unlock(&current_detail->hash_lock);
451 d = current_detail;
452 if (!ch)
453 current_index ++;
454 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000455 if (ch) {
NeilBrown013920e2013-06-13 12:53:42 +1000456 set_bit(CACHE_CLEANED, &ch->flags);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000457 cache_fresh_unlocked(ch, d);
NeilBrownbaab9352006-03-27 01:15:09 -0800458 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 } else
461 spin_unlock(&cache_list_lock);
462
463 return rv;
464}
465
466/*
467 * We want to regularly clean the cache, so we need to schedule some work ...
468 */
David Howells65f27f32006-11-22 14:55:48 +0000469static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 int delay = 5;
472 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700473 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 if (list_empty(&cache_list))
476 delay = 0;
477
478 if (delay)
479 schedule_delayed_work(&cache_cleaner, delay);
480}
481
482
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800483/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800485 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * be fully cleaned
487 */
488void cache_flush(void)
489{
490 while (cache_clean() != -1)
491 cond_resched();
492 while (cache_clean() != -1)
493 cond_resched();
494}
Trond Myklebust24c37672008-12-23 16:30:12 -0500495EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497void cache_purge(struct cache_detail *detail)
498{
499 detail->flush_time = LONG_MAX;
NeilBrownc5b29f82010-08-12 16:55:22 +1000500 detail->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 cache_flush();
502 detail->flush_time = 1;
503}
Trond Myklebust24c37672008-12-23 16:30:12 -0500504EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506
507/*
508 * Deferral and Revisiting of Requests.
509 *
510 * If a cache lookup finds a pending entry, we
511 * need to defer the request and revisit it later.
512 * All deferred requests are stored in a hash table,
513 * indexed by "struct cache_head *".
514 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800515 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * deferred form, which must contain a
517 * 'struct cache_deferred_req'
518 * This cache_deferred_req contains a method to allow
519 * it to be revisited when cache info is available
520 */
521
522#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
523#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
524
525#define DFR_MAX 300 /* ??? */
526
527static DEFINE_SPINLOCK(cache_defer_lock);
528static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000529static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530static int cache_defer_cnt;
531
J. Bruce Fields6610f722010-08-26 13:19:52 -0400532static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
NeilBrown11174492010-08-12 17:04:08 +1000534 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100535 if (!list_empty(&dreq->recent)) {
536 list_del_init(&dreq->recent);
537 cache_defer_cnt--;
538 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400539}
540
541static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
542{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int hash = DFR_HASH(item);
544
NeilBrowne33534d2010-10-07 15:29:46 +1100545 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000546 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400547}
548
NeilBrowne33534d2010-10-07 15:29:46 +1100549static void setup_deferral(struct cache_deferred_req *dreq,
550 struct cache_head *item,
551 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 spin_lock(&cache_defer_lock);
557
J. Bruce Fields6610f722010-08-26 13:19:52 -0400558 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
NeilBrowne33534d2010-10-07 15:29:46 +1100560 if (count_me) {
561 cache_defer_cnt++;
562 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
NeilBrowne33534d2010-10-07 15:29:46 +1100564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 spin_unlock(&cache_defer_lock);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
J. Bruce Fields3211af12010-08-26 16:56:23 -0400569struct thread_deferred_req {
570 struct cache_deferred_req handle;
571 struct completion completion;
572};
573
574static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
575{
576 struct thread_deferred_req *dr =
577 container_of(dreq, struct thread_deferred_req, handle);
578 complete(&dr->completion);
579}
580
NeilBrownd29068c42010-10-07 15:29:46 +1100581static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400582{
583 struct thread_deferred_req sleeper;
584 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400585
586 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
587 dreq->revisit = cache_restart_thread;
588
NeilBrowne33534d2010-10-07 15:29:46 +1100589 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400590
NeilBrownd29068c42010-10-07 15:29:46 +1100591 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000592 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400593 &sleeper.completion, req->thread_wait) <= 0) {
594 /* The completion wasn't completed, so we need
595 * to clean up
596 */
597 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000598 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400599 __unhash_deferred_req(&sleeper.handle);
600 spin_unlock(&cache_defer_lock);
601 } else {
602 /* cache_revisit_request already removed
603 * this from the hash table, but hasn't
604 * called ->revisit yet. It will very soon
605 * and we need to wait for it.
606 */
607 spin_unlock(&cache_defer_lock);
608 wait_for_completion(&sleeper.completion);
609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400611}
612
NeilBrowne33534d2010-10-07 15:29:46 +1100613static void cache_limit_defers(void)
614{
615 /* Make sure we haven't exceed the limit of allowed deferred
616 * requests.
617 */
618 struct cache_deferred_req *discard = NULL;
619
620 if (cache_defer_cnt <= DFR_MAX)
621 return;
622
623 spin_lock(&cache_defer_lock);
624
625 /* Consider removing either the first or the last */
626 if (cache_defer_cnt > DFR_MAX) {
627 if (net_random() & 1)
628 discard = list_entry(cache_defer_list.next,
629 struct cache_deferred_req, recent);
630 else
631 discard = list_entry(cache_defer_list.prev,
632 struct cache_deferred_req, recent);
633 __unhash_deferred_req(discard);
634 }
635 spin_unlock(&cache_defer_lock);
636 if (discard)
637 discard->revisit(discard, 1);
638}
639
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500640/* Return true if and only if a deferred request is queued. */
641static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400642{
643 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400644
J. Bruce Fields3211af12010-08-26 16:56:23 -0400645 if (req->thread_wait) {
NeilBrownd29068c42010-10-07 15:29:46 +1100646 cache_wait_req(req, item);
647 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500648 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400649 }
650 dreq = req->defer(req);
651 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500652 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100653 setup_deferral(dreq, item, 1);
NeilBrownd29068c42010-10-07 15:29:46 +1100654 if (!test_bit(CACHE_PENDING, &item->flags))
655 /* Bit could have been cleared before we managed to
656 * set up the deferral, so need to revisit just in case
657 */
658 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100659
660 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500661 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static void cache_revisit_request(struct cache_head *item)
665{
666 struct cache_deferred_req *dreq;
667 struct list_head pending;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800668 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int hash = DFR_HASH(item);
670
671 INIT_LIST_HEAD(&pending);
672 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800673
Sasha Levinb67bfe02013-02-27 17:06:00 -0800674 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000675 if (dreq->item == item) {
676 __unhash_deferred_req(dreq);
677 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
NeilBrown11174492010-08-12 17:04:08 +1000679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 spin_unlock(&cache_defer_lock);
681
682 while (!list_empty(&pending)) {
683 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
684 list_del_init(&dreq->recent);
685 dreq->revisit(dreq, 0);
686 }
687}
688
689void cache_clean_deferred(void *owner)
690{
691 struct cache_deferred_req *dreq, *tmp;
692 struct list_head pending;
693
694
695 INIT_LIST_HEAD(&pending);
696 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
699 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400700 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000701 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 }
704 spin_unlock(&cache_defer_lock);
705
706 while (!list_empty(&pending)) {
707 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
708 list_del_init(&dreq->recent);
709 dreq->revisit(dreq, 1);
710 }
711}
712
713/*
714 * communicate with user-space
715 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500716 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
717 * On read, you get a full request, or block.
718 * On write, an update request is processed.
719 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800721 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500722 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * to the end and may wakeup and preceding readers.
724 * New readers are added to the head. If, on read, an item is found with
725 * CACHE_UPCALLING clear, we free it from the list.
726 *
727 */
728
729static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800730static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732struct cache_queue {
733 struct list_head list;
734 int reader; /* if 0, then request */
735};
736struct cache_request {
737 struct cache_queue q;
738 struct cache_head *item;
739 char * buf;
740 int len;
741 int readers;
742};
743struct cache_reader {
744 struct cache_queue q;
745 int offset; /* if non-0, we have a refcnt on next request */
746};
747
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300748static int cache_request(struct cache_detail *detail,
749 struct cache_request *crq)
750{
751 char *bp = crq->buf;
752 int len = PAGE_SIZE;
753
754 detail->cache_request(detail, crq->item, &bp, &len);
755 if (len < 0)
756 return -EAGAIN;
757 return PAGE_SIZE - len;
758}
759
Trond Myklebust173912a2009-08-09 15:14:29 -0400760static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
761 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct cache_reader *rp = filp->private_data;
764 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500765 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int err;
767
768 if (count == 0)
769 return 0;
770
Trond Myklebustda770052009-08-09 15:14:28 -0400771 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 * readers on this file */
773 again:
774 spin_lock(&queue_lock);
775 /* need to find next request */
776 while (rp->q.list.next != &cd->queue &&
777 list_entry(rp->q.list.next, struct cache_queue, list)
778 ->reader) {
779 struct list_head *next = rp->q.list.next;
780 list_move(&rp->q.list, next);
781 }
782 if (rp->q.list.next == &cd->queue) {
783 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400784 mutex_unlock(&inode->i_mutex);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400785 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return 0;
787 }
788 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400789 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (rp->offset == 0)
791 rq->readers++;
792 spin_unlock(&queue_lock);
793
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300794 if (rq->len == 0) {
795 err = cache_request(cd, rq);
796 if (err < 0)
797 goto out;
798 rq->len = err;
799 }
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
802 err = -EAGAIN;
803 spin_lock(&queue_lock);
804 list_move(&rp->q.list, &rq->q.list);
805 spin_unlock(&queue_lock);
806 } else {
807 if (rp->offset + count > rq->len)
808 count = rq->len - rp->offset;
809 err = -EFAULT;
810 if (copy_to_user(buf, rq->buf + rp->offset, count))
811 goto out;
812 rp->offset += count;
813 if (rp->offset >= rq->len) {
814 rp->offset = 0;
815 spin_lock(&queue_lock);
816 list_move(&rp->q.list, &rq->q.list);
817 spin_unlock(&queue_lock);
818 }
819 err = 0;
820 }
821 out:
822 if (rp->offset == 0) {
823 /* need to release rq */
824 spin_lock(&queue_lock);
825 rq->readers--;
826 if (rq->readers == 0 &&
827 !test_bit(CACHE_PENDING, &rq->item->flags)) {
828 list_del(&rq->q.list);
829 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800830 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 kfree(rq->buf);
832 kfree(rq);
833 } else
834 spin_unlock(&queue_lock);
835 }
836 if (err == -EAGAIN)
837 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400838 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return err ? err : count;
840}
841
Trond Myklebustda770052009-08-09 15:14:28 -0400842static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
843 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Trond Myklebustda770052009-08-09 15:14:28 -0400845 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300847 if (count == 0)
848 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400849 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400851 kaddr[count] = '\0';
852 ret = cd->cache_parse(cd, kaddr, count);
853 if (!ret)
854 ret = count;
855 return ret;
856}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Trond Myklebustda770052009-08-09 15:14:28 -0400858static ssize_t cache_slow_downcall(const char __user *buf,
859 size_t count, struct cache_detail *cd)
860{
861 static char write_buf[8192]; /* protected by queue_io_mutex */
862 ssize_t ret = -EINVAL;
863
864 if (count >= sizeof(write_buf))
865 goto out;
866 mutex_lock(&queue_io_mutex);
867 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800868 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400869out:
870 return ret;
871}
872
873static ssize_t cache_downcall(struct address_space *mapping,
874 const char __user *buf,
875 size_t count, struct cache_detail *cd)
876{
877 struct page *page;
878 char *kaddr;
879 ssize_t ret = -ENOMEM;
880
881 if (count >= PAGE_CACHE_SIZE)
882 goto out_slow;
883
884 page = find_or_create_page(mapping, 0, GFP_KERNEL);
885 if (!page)
886 goto out_slow;
887
888 kaddr = kmap(page);
889 ret = cache_do_downcall(kaddr, buf, count, cd);
890 kunmap(page);
891 unlock_page(page);
892 page_cache_release(page);
893 return ret;
894out_slow:
895 return cache_slow_downcall(buf, count, cd);
896}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Trond Myklebust173912a2009-08-09 15:14:29 -0400898static ssize_t cache_write(struct file *filp, const char __user *buf,
899 size_t count, loff_t *ppos,
900 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Trond Myklebustda770052009-08-09 15:14:28 -0400902 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500903 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400904 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Trond Myklebustda770052009-08-09 15:14:28 -0400906 if (!cd->cache_parse)
907 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Trond Myklebustda770052009-08-09 15:14:28 -0400909 mutex_lock(&inode->i_mutex);
910 ret = cache_downcall(mapping, buf, count, cd);
911 mutex_unlock(&inode->i_mutex);
912out:
913 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
917
Trond Myklebust173912a2009-08-09 15:14:29 -0400918static unsigned int cache_poll(struct file *filp, poll_table *wait,
919 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 unsigned int mask;
922 struct cache_reader *rp = filp->private_data;
923 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 poll_wait(filp, &queue_wait, wait);
926
927 /* alway allow write */
928 mask = POLL_OUT | POLLWRNORM;
929
930 if (!rp)
931 return mask;
932
933 spin_lock(&queue_lock);
934
935 for (cq= &rp->q; &cq->list != &cd->queue;
936 cq = list_entry(cq->list.next, struct cache_queue, list))
937 if (!cq->reader) {
938 mask |= POLLIN | POLLRDNORM;
939 break;
940 }
941 spin_unlock(&queue_lock);
942 return mask;
943}
944
Trond Myklebust173912a2009-08-09 15:14:29 -0400945static int cache_ioctl(struct inode *ino, struct file *filp,
946 unsigned int cmd, unsigned long arg,
947 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 int len = 0;
950 struct cache_reader *rp = filp->private_data;
951 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 if (cmd != FIONREAD || !rp)
954 return -EINVAL;
955
956 spin_lock(&queue_lock);
957
958 /* only find the length remaining in current request,
959 * or the length of the next request
960 */
961 for (cq= &rp->q; &cq->list != &cd->queue;
962 cq = list_entry(cq->list.next, struct cache_queue, list))
963 if (!cq->reader) {
964 struct cache_request *cr =
965 container_of(cq, struct cache_request, q);
966 len = cr->len - rp->offset;
967 break;
968 }
969 spin_unlock(&queue_lock);
970
971 return put_user(len, (int __user *)arg);
972}
973
Trond Myklebust173912a2009-08-09 15:14:29 -0400974static int cache_open(struct inode *inode, struct file *filp,
975 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 struct cache_reader *rp = NULL;
978
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400979 if (!cd || !try_module_get(cd->owner))
980 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 nonseekable_open(inode, filp);
982 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400984 if (!rp) {
985 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 rp->offset = 0;
989 rp->q.reader = 1;
990 atomic_inc(&cd->readers);
991 spin_lock(&queue_lock);
992 list_add(&rp->q.list, &cd->queue);
993 spin_unlock(&queue_lock);
994 }
995 filp->private_data = rp;
996 return 0;
997}
998
Trond Myklebust173912a2009-08-09 15:14:29 -0400999static int cache_release(struct inode *inode, struct file *filp,
1000 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 if (rp) {
1005 spin_lock(&queue_lock);
1006 if (rp->offset) {
1007 struct cache_queue *cq;
1008 for (cq= &rp->q; &cq->list != &cd->queue;
1009 cq = list_entry(cq->list.next, struct cache_queue, list))
1010 if (!cq->reader) {
1011 container_of(cq, struct cache_request, q)
1012 ->readers--;
1013 break;
1014 }
1015 rp->offset = 0;
1016 }
1017 list_del(&rp->q.list);
1018 spin_unlock(&queue_lock);
1019
1020 filp->private_data = NULL;
1021 kfree(rp);
1022
NeilBrownc5b29f82010-08-12 16:55:22 +10001023 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 atomic_dec(&cd->readers);
1025 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001026 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return 0;
1028}
1029
1030
1031
NeilBrownf866a812009-08-04 15:22:38 +10001032static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001034 struct cache_queue *cq, *tmp;
1035 struct cache_request *cr;
1036 struct list_head dequeued;
1037
1038 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001040 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001042 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (cr->item != ch)
1044 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001045 if (test_bit(CACHE_PENDING, &ch->flags))
1046 /* Lost a race and it is pending again */
1047 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001049 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001050 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001053 while (!list_empty(&dequeued)) {
1054 cr = list_entry(dequeued.next, struct cache_request, q.list);
1055 list_del(&cr->q.list);
1056 cache_put(cr->item, detail);
1057 kfree(cr->buf);
1058 kfree(cr);
1059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062/*
1063 * Support routines for text-based upcalls.
1064 * Fields are separated by spaces.
1065 * Fields are either mangled to quote space tab newline slosh with slosh
1066 * or a hexified with a leading \x
1067 * Record is terminated with newline.
1068 *
1069 */
1070
1071void qword_add(char **bpp, int *lp, char *str)
1072{
1073 char *bp = *bpp;
1074 int len = *lp;
1075 char c;
1076
1077 if (len < 0) return;
1078
1079 while ((c=*str++) && len)
1080 switch(c) {
1081 case ' ':
1082 case '\t':
1083 case '\n':
1084 case '\\':
1085 if (len >= 4) {
1086 *bp++ = '\\';
1087 *bp++ = '0' + ((c & 0300)>>6);
1088 *bp++ = '0' + ((c & 0070)>>3);
1089 *bp++ = '0' + ((c & 0007)>>0);
1090 }
1091 len -= 4;
1092 break;
1093 default:
1094 *bp++ = c;
1095 len--;
1096 }
1097 if (c || len <1) len = -1;
1098 else {
1099 *bp++ = ' ';
1100 len--;
1101 }
1102 *bpp = bp;
1103 *lp = len;
1104}
Trond Myklebust24c37672008-12-23 16:30:12 -05001105EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1108{
1109 char *bp = *bpp;
1110 int len = *lp;
1111
1112 if (len < 0) return;
1113
1114 if (len > 2) {
1115 *bp++ = '\\';
1116 *bp++ = 'x';
1117 len -= 2;
1118 while (blen && len >= 2) {
1119 unsigned char c = *buf++;
1120 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1121 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1122 len -= 2;
1123 blen--;
1124 }
1125 }
1126 if (blen || len<1) len = -1;
1127 else {
1128 *bp++ = ' ';
1129 len--;
1130 }
1131 *bpp = bp;
1132 *lp = len;
1133}
Trond Myklebust24c37672008-12-23 16:30:12 -05001134EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136static void warn_no_listener(struct cache_detail *detail)
1137{
1138 if (detail->last_warn != detail->last_close) {
1139 detail->last_warn = detail->last_close;
1140 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001141 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143}
1144
J. Bruce Fields06497522010-09-19 22:55:06 -04001145static bool cache_listeners_exist(struct cache_detail *detail)
1146{
1147 if (atomic_read(&detail->readers))
1148 return true;
1149 if (detail->last_close == 0)
1150 /* This cache was never opened */
1151 return false;
1152 if (detail->last_close < seconds_since_boot() - 30)
1153 /*
1154 * We allow for the possibility that someone might
1155 * restart a userspace daemon without restarting the
1156 * server; but after 30 seconds, we give up.
1157 */
1158 return false;
1159 return true;
1160}
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001163 * register an upcall request to user-space and queue it up for read() by the
1164 * upcall daemon.
1165 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * Each request is at most one page long.
1167 */
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +03001168int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170
1171 char *buf;
1172 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001173 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001175 if (!detail->cache_request)
1176 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
J. Bruce Fields06497522010-09-19 22:55:06 -04001178 if (!cache_listeners_exist(detail)) {
1179 warn_no_listener(detail);
1180 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
NeilBrown013920e2013-06-13 12:53:42 +10001182 if (test_bit(CACHE_CLEANED, &h->flags))
1183 /* Too late to make an upcall */
1184 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1187 if (!buf)
1188 return -EAGAIN;
1189
1190 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1191 if (!crq) {
1192 kfree(buf);
1193 return -EAGAIN;
1194 }
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 crq->q.reader = 0;
1197 crq->item = cache_get(h);
1198 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001199 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 crq->readers = 0;
1201 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001202 if (test_bit(CACHE_PENDING, &h->flags))
1203 list_add_tail(&crq->q.list, &detail->queue);
1204 else
1205 /* Lost a race, no longer PENDING, so don't enqueue */
1206 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 spin_unlock(&queue_lock);
1208 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001209 if (ret == -EAGAIN) {
1210 kfree(buf);
1211 kfree(crq);
1212 }
1213 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001215EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217/*
1218 * parse a message from user-space and pass it
1219 * to an appropriate cache
1220 * Messages are, like requests, separated into fields by
1221 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1222 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001223 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 * reply cachename expiry key ... content....
1225 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001226 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 */
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229int qword_get(char **bpp, char *dest, int bufsize)
1230{
1231 /* return bytes copied, or -1 on error */
1232 char *bp = *bpp;
1233 int len = 0;
1234
1235 while (*bp == ' ') bp++;
1236
1237 if (bp[0] == '\\' && bp[1] == 'x') {
1238 /* HEX STRING */
1239 bp += 2;
Andy Shevchenkoe7f483e2010-09-21 09:40:25 +03001240 while (len < bufsize) {
1241 int h, l;
1242
1243 h = hex_to_bin(bp[0]);
1244 if (h < 0)
1245 break;
1246
1247 l = hex_to_bin(bp[1]);
1248 if (l < 0)
1249 break;
1250
1251 *dest++ = (h << 4) | l;
1252 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 len++;
1254 }
1255 } else {
1256 /* text with \nnn octal quoting */
1257 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1258 if (*bp == '\\' &&
1259 isodigit(bp[1]) && (bp[1] <= '3') &&
1260 isodigit(bp[2]) &&
1261 isodigit(bp[3])) {
1262 int byte = (*++bp -'0');
1263 bp++;
1264 byte = (byte << 3) | (*bp++ - '0');
1265 byte = (byte << 3) | (*bp++ - '0');
1266 *dest++ = byte;
1267 len++;
1268 } else {
1269 *dest++ = *bp++;
1270 len++;
1271 }
1272 }
1273 }
1274
1275 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1276 return -1;
1277 while (*bp == ' ') bp++;
1278 *bpp = bp;
1279 *dest = '\0';
1280 return len;
1281}
Trond Myklebust24c37672008-12-23 16:30:12 -05001282EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284
1285/*
1286 * support /proc/sunrpc/cache/$CACHENAME/content
1287 * as a seqfile.
1288 * We call ->cache_show passing NULL for the item to
1289 * get a header, then pass each real item in the cache
1290 */
1291
1292struct handle {
1293 struct cache_detail *cd;
1294};
1295
1296static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001297 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001300 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct cache_head *ch;
1302 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 read_lock(&cd->hash_lock);
1306 if (!n--)
1307 return SEQ_START_TOKEN;
1308 hash = n >> 32;
1309 entry = n & ((1LL<<32) - 1);
1310
1311 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1312 if (!entry--)
1313 return ch;
1314 n &= ~((1LL<<32) - 1);
1315 do {
1316 hash++;
1317 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001318 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 cd->hash_table[hash]==NULL);
1320 if (hash >= cd->hash_size)
1321 return NULL;
1322 *pos = n+1;
1323 return cd->hash_table[hash];
1324}
1325
1326static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1327{
1328 struct cache_head *ch = p;
1329 int hash = (*pos >> 32);
1330 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1331
1332 if (p == SEQ_START_TOKEN)
1333 hash = 0;
1334 else if (ch->next == NULL) {
1335 hash++;
1336 *pos += 1LL<<32;
1337 } else {
1338 ++*pos;
1339 return ch->next;
1340 }
1341 *pos &= ~((1LL<<32) - 1);
1342 while (hash < cd->hash_size &&
1343 cd->hash_table[hash] == NULL) {
1344 hash++;
1345 *pos += 1LL<<32;
1346 }
1347 if (hash >= cd->hash_size)
1348 return NULL;
1349 ++*pos;
1350 return cd->hash_table[hash];
1351}
1352
1353static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001354 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1357 read_unlock(&cd->hash_lock);
1358}
1359
1360static int c_show(struct seq_file *m, void *p)
1361{
1362 struct cache_head *cp = p;
1363 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1364
1365 if (p == SEQ_START_TOKEN)
1366 return cd->cache_show(m, cd, NULL);
1367
1368 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001369 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001370 convert_to_wallclock(cp->expiry_time),
1371 atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 cache_get(cp);
1373 if (cache_check(cd, cp, NULL))
1374 /* cache_check does a cache_put on failure */
1375 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001376 else {
1377 if (cache_is_expired(cd, cp))
1378 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 return cd->cache_show(m, cd, cp);
1383}
1384
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001385static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 .start = c_start,
1387 .next = c_next,
1388 .stop = c_stop,
1389 .show = c_show,
1390};
1391
Trond Myklebust173912a2009-08-09 15:14:29 -04001392static int content_open(struct inode *inode, struct file *file,
1393 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 struct handle *han;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001397 if (!cd || !try_module_get(cd->owner))
1398 return -EACCES;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001399 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Li Zefana5990ea2010-03-11 14:08:10 -08001400 if (han == NULL) {
1401 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return -ENOMEM;
Li Zefana5990ea2010-03-11 14:08:10 -08001403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001409static int content_release(struct inode *inode, struct file *file,
1410 struct cache_detail *cd)
1411{
1412 int ret = seq_release_private(inode, file);
1413 module_put(cd->owner);
1414 return ret;
1415}
1416
1417static int open_flush(struct inode *inode, struct file *file,
1418 struct cache_detail *cd)
1419{
1420 if (!cd || !try_module_get(cd->owner))
1421 return -EACCES;
1422 return nonseekable_open(inode, file);
1423}
1424
1425static int release_flush(struct inode *inode, struct file *file,
1426 struct cache_detail *cd)
1427{
1428 module_put(cd->owner);
1429 return 0;
1430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001433 size_t count, loff_t *ppos,
1434 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Sasha Levin212ba902012-07-17 00:01:26 +02001436 char tbuf[22];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001438 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Sasha Levin212ba902012-07-17 00:01:26 +02001440 snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 len = strlen(tbuf);
1442 if (p >= len)
1443 return 0;
1444 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001445 if (len > count)
1446 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001448 return -EFAULT;
1449 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return len;
1451}
1452
Trond Myklebust173912a2009-08-09 15:14:29 -04001453static ssize_t write_flush(struct file *file, const char __user *buf,
1454 size_t count, loff_t *ppos,
1455 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 char tbuf[20];
NeilBrownc5b29f82010-08-12 16:55:22 +10001458 char *bp, *ep;
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (*ppos || count > sizeof(tbuf)-1)
1461 return -EINVAL;
1462 if (copy_from_user(tbuf, buf, count))
1463 return -EFAULT;
1464 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001465 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (*ep && *ep != '\n')
1467 return -EINVAL;
1468
NeilBrownc5b29f82010-08-12 16:55:22 +10001469 bp = tbuf;
1470 cd->flush_time = get_expiry(&bp);
1471 cd->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 cache_flush();
1473
1474 *ppos += count;
1475 return count;
1476}
1477
Trond Myklebust173912a2009-08-09 15:14:29 -04001478static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1479 size_t count, loff_t *ppos)
1480{
Al Virod9dda782013-03-31 18:16:14 -04001481 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001482
1483 return cache_read(filp, buf, count, ppos, cd);
1484}
1485
1486static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1487 size_t count, loff_t *ppos)
1488{
Al Virod9dda782013-03-31 18:16:14 -04001489 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001490
1491 return cache_write(filp, buf, count, ppos, cd);
1492}
1493
1494static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1495{
Al Virod9dda782013-03-31 18:16:14 -04001496 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001497
1498 return cache_poll(filp, wait, cd);
1499}
1500
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001501static long cache_ioctl_procfs(struct file *filp,
1502 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001503{
Al Viro496ad9a2013-01-23 17:07:38 -05001504 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001505 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001506
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001507 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001508}
1509
1510static int cache_open_procfs(struct inode *inode, struct file *filp)
1511{
Al Virod9dda782013-03-31 18:16:14 -04001512 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001513
1514 return cache_open(inode, filp, cd);
1515}
1516
1517static int cache_release_procfs(struct inode *inode, struct file *filp)
1518{
Al Virod9dda782013-03-31 18:16:14 -04001519 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001520
1521 return cache_release(inode, filp, cd);
1522}
1523
1524static const struct file_operations cache_file_operations_procfs = {
1525 .owner = THIS_MODULE,
1526 .llseek = no_llseek,
1527 .read = cache_read_procfs,
1528 .write = cache_write_procfs,
1529 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001530 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001531 .open = cache_open_procfs,
1532 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533};
Trond Myklebust173912a2009-08-09 15:14:29 -04001534
1535static int content_open_procfs(struct inode *inode, struct file *filp)
1536{
Al Virod9dda782013-03-31 18:16:14 -04001537 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001538
1539 return content_open(inode, filp, cd);
1540}
1541
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001542static int content_release_procfs(struct inode *inode, struct file *filp)
1543{
Al Virod9dda782013-03-31 18:16:14 -04001544 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001545
1546 return content_release(inode, filp, cd);
1547}
1548
Trond Myklebust173912a2009-08-09 15:14:29 -04001549static const struct file_operations content_file_operations_procfs = {
1550 .open = content_open_procfs,
1551 .read = seq_read,
1552 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001553 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001554};
1555
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001556static int open_flush_procfs(struct inode *inode, struct file *filp)
1557{
Al Virod9dda782013-03-31 18:16:14 -04001558 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001559
1560 return open_flush(inode, filp, cd);
1561}
1562
1563static int release_flush_procfs(struct inode *inode, struct file *filp)
1564{
Al Virod9dda782013-03-31 18:16:14 -04001565 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001566
1567 return release_flush(inode, filp, cd);
1568}
1569
Trond Myklebust173912a2009-08-09 15:14:29 -04001570static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1571 size_t count, loff_t *ppos)
1572{
Al Virod9dda782013-03-31 18:16:14 -04001573 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001574
1575 return read_flush(filp, buf, count, ppos, cd);
1576}
1577
1578static ssize_t write_flush_procfs(struct file *filp,
1579 const char __user *buf,
1580 size_t count, loff_t *ppos)
1581{
Al Virod9dda782013-03-31 18:16:14 -04001582 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001583
1584 return write_flush(filp, buf, count, ppos, cd);
1585}
1586
1587static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001588 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001589 .read = read_flush_procfs,
1590 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001591 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001592 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001593};
1594
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001595static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001596{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001597 struct sunrpc_net *sn;
1598
Trond Myklebust173912a2009-08-09 15:14:29 -04001599 if (cd->u.procfs.proc_ent == NULL)
1600 return;
1601 if (cd->u.procfs.flush_ent)
1602 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1603 if (cd->u.procfs.channel_ent)
1604 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1605 if (cd->u.procfs.content_ent)
1606 remove_proc_entry("content", cd->u.procfs.proc_ent);
1607 cd->u.procfs.proc_ent = NULL;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001608 sn = net_generic(net, sunrpc_net_id);
1609 remove_proc_entry(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001610}
1611
1612#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001613static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001614{
1615 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001616 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001617
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001618 sn = net_generic(net, sunrpc_net_id);
1619 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001620 if (cd->u.procfs.proc_ent == NULL)
1621 goto out_nomem;
1622 cd->u.procfs.channel_ent = NULL;
1623 cd->u.procfs.content_ent = NULL;
1624
1625 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1626 cd->u.procfs.proc_ent,
1627 &cache_flush_operations_procfs, cd);
1628 cd->u.procfs.flush_ent = p;
1629 if (p == NULL)
1630 goto out_nomem;
1631
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001632 if (cd->cache_request || cd->cache_parse) {
Trond Myklebust173912a2009-08-09 15:14:29 -04001633 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1634 cd->u.procfs.proc_ent,
1635 &cache_file_operations_procfs, cd);
1636 cd->u.procfs.channel_ent = p;
1637 if (p == NULL)
1638 goto out_nomem;
1639 }
1640 if (cd->cache_show) {
Yanchuan Nianec168672013-01-04 19:45:35 +08001641 p = proc_create_data("content", S_IFREG|S_IRUSR,
Trond Myklebust173912a2009-08-09 15:14:29 -04001642 cd->u.procfs.proc_ent,
1643 &content_file_operations_procfs, cd);
1644 cd->u.procfs.content_ent = p;
1645 if (p == NULL)
1646 goto out_nomem;
1647 }
1648 return 0;
1649out_nomem:
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001650 remove_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001651 return -ENOMEM;
1652}
1653#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001654static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001655{
1656 return 0;
1657}
1658#endif
1659
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001660void __init cache_initialize(void)
1661{
Tejun Heo203b42f2012-08-21 13:18:23 -07001662 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001663}
1664
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001665int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001666{
1667 int ret;
1668
1669 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001670 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001671 if (ret)
1672 sunrpc_destroy_cache_detail(cd);
1673 return ret;
1674}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001675EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001676
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001677void cache_unregister_net(struct cache_detail *cd, struct net *net)
1678{
1679 remove_cache_proc_entries(cd, net);
1680 sunrpc_destroy_cache_detail(cd);
1681}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001682EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001683
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001684struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001685{
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001686 struct cache_detail *cd;
1687
1688 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1689 if (cd == NULL)
1690 return ERR_PTR(-ENOMEM);
1691
1692 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct cache_head *),
1693 GFP_KERNEL);
1694 if (cd->hash_table == NULL) {
1695 kfree(cd);
1696 return ERR_PTR(-ENOMEM);
1697 }
1698 cd->net = net;
1699 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001700}
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001701EXPORT_SYMBOL_GPL(cache_create_net);
1702
1703void cache_destroy_net(struct cache_detail *cd, struct net *net)
1704{
1705 kfree(cd->hash_table);
1706 kfree(cd);
1707}
1708EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001709
1710static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1711 size_t count, loff_t *ppos)
1712{
Al Viro496ad9a2013-01-23 17:07:38 -05001713 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001714
1715 return cache_read(filp, buf, count, ppos, cd);
1716}
1717
1718static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1719 size_t count, loff_t *ppos)
1720{
Al Viro496ad9a2013-01-23 17:07:38 -05001721 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001722
1723 return cache_write(filp, buf, count, ppos, cd);
1724}
1725
1726static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1727{
Al Viro496ad9a2013-01-23 17:07:38 -05001728 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001729
1730 return cache_poll(filp, wait, cd);
1731}
1732
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001733static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001734 unsigned int cmd, unsigned long arg)
1735{
Al Viro496ad9a2013-01-23 17:07:38 -05001736 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001737 struct cache_detail *cd = RPC_I(inode)->private;
1738
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001739 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001740}
1741
1742static int cache_open_pipefs(struct inode *inode, struct file *filp)
1743{
1744 struct cache_detail *cd = RPC_I(inode)->private;
1745
1746 return cache_open(inode, filp, cd);
1747}
1748
1749static int cache_release_pipefs(struct inode *inode, struct file *filp)
1750{
1751 struct cache_detail *cd = RPC_I(inode)->private;
1752
1753 return cache_release(inode, filp, cd);
1754}
1755
1756const struct file_operations cache_file_operations_pipefs = {
1757 .owner = THIS_MODULE,
1758 .llseek = no_llseek,
1759 .read = cache_read_pipefs,
1760 .write = cache_write_pipefs,
1761 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001762 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001763 .open = cache_open_pipefs,
1764 .release = cache_release_pipefs,
1765};
1766
1767static int content_open_pipefs(struct inode *inode, struct file *filp)
1768{
1769 struct cache_detail *cd = RPC_I(inode)->private;
1770
1771 return content_open(inode, filp, cd);
1772}
1773
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001774static int content_release_pipefs(struct inode *inode, struct file *filp)
1775{
1776 struct cache_detail *cd = RPC_I(inode)->private;
1777
1778 return content_release(inode, filp, cd);
1779}
1780
Trond Myklebust8854e822009-08-09 15:14:30 -04001781const struct file_operations content_file_operations_pipefs = {
1782 .open = content_open_pipefs,
1783 .read = seq_read,
1784 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001785 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001786};
1787
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001788static int open_flush_pipefs(struct inode *inode, struct file *filp)
1789{
1790 struct cache_detail *cd = RPC_I(inode)->private;
1791
1792 return open_flush(inode, filp, cd);
1793}
1794
1795static int release_flush_pipefs(struct inode *inode, struct file *filp)
1796{
1797 struct cache_detail *cd = RPC_I(inode)->private;
1798
1799 return release_flush(inode, filp, cd);
1800}
1801
Trond Myklebust8854e822009-08-09 15:14:30 -04001802static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1803 size_t count, loff_t *ppos)
1804{
Al Viro496ad9a2013-01-23 17:07:38 -05001805 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001806
1807 return read_flush(filp, buf, count, ppos, cd);
1808}
1809
1810static ssize_t write_flush_pipefs(struct file *filp,
1811 const char __user *buf,
1812 size_t count, loff_t *ppos)
1813{
Al Viro496ad9a2013-01-23 17:07:38 -05001814 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001815
1816 return write_flush(filp, buf, count, ppos, cd);
1817}
1818
1819const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001820 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001821 .read = read_flush_pipefs,
1822 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001823 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001824 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001825};
1826
1827int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001828 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001829 struct cache_detail *cd)
1830{
1831 struct qstr q;
1832 struct dentry *dir;
1833 int ret = 0;
1834
Trond Myklebust8854e822009-08-09 15:14:30 -04001835 q.name = name;
1836 q.len = strlen(name);
1837 q.hash = full_name_hash(q.name, q.len);
1838 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1839 if (!IS_ERR(dir))
1840 cd->u.pipefs.dir = dir;
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +03001841 else
Trond Myklebust8854e822009-08-09 15:14:30 -04001842 ret = PTR_ERR(dir);
Trond Myklebust8854e822009-08-09 15:14:30 -04001843 return ret;
1844}
1845EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1846
1847void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1848{
1849 rpc_remove_cache_dir(cd->u.pipefs.dir);
1850 cd->u.pipefs.dir = NULL;
Trond Myklebust8854e822009-08-09 15:14:30 -04001851}
1852EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1853