blob: 5199bb1a017e47b1b7503caf8075191dac1ade57 [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>
Andy Shevchenko1b2e1222014-11-28 17:50:28 +020023#include <linux/string_helpers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/uaccess.h>
25#include <linux/poll.h>
26#include <linux/seq_file.h>
27#include <linux/proc_fs.h>
28#include <linux/net.h>
29#include <linux/workqueue.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080030#include <linux/mutex.h>
Trond Myklebustda770052009-08-09 15:14:28 -040031#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/ioctls.h>
33#include <linux/sunrpc/types.h>
34#include <linux/sunrpc/cache.h>
35#include <linux/sunrpc/stats.h>
Trond Myklebust8854e822009-08-09 15:14:30 -040036#include <linux/sunrpc/rpc_pipe_fs.h>
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +040037#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define RPCDBG_FACILITY RPCDBG_CACHE
40
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -050041static bool cache_defer_req(struct cache_req *req, struct cache_head *item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static void cache_revisit_request(struct cache_head *item);
43
Adrian Bunk74cae612006-03-27 01:15:10 -080044static void cache_init(struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
NeilBrownc5b29f82010-08-12 16:55:22 +100046 time_t now = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 h->next = NULL;
48 h->flags = 0;
NeilBrownbaab9352006-03-27 01:15:09 -080049 kref_init(&h->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 h->expiry_time = now + CACHE_NEW_EXPIRY;
51 h->last_refresh = now;
52}
53
NeilBrown15a5f6b2006-03-27 01:15:02 -080054struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
55 struct cache_head *key, int hash)
56{
57 struct cache_head **head, **hp;
NeilBrownd202cce2010-02-03 17:31:31 +110058 struct cache_head *new = NULL, *freeme = NULL;
NeilBrown15a5f6b2006-03-27 01:15:02 -080059
60 head = &detail->hash_table[hash];
61
62 read_lock(&detail->hash_lock);
63
64 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
65 struct cache_head *tmp = *hp;
66 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110067 if (cache_is_expired(detail, tmp))
68 /* This entry is expired, we will discard it. */
69 break;
NeilBrown15a5f6b2006-03-27 01:15:02 -080070 cache_get(tmp);
71 read_unlock(&detail->hash_lock);
72 return tmp;
73 }
74 }
75 read_unlock(&detail->hash_lock);
76 /* Didn't find anything, insert an empty entry */
77
78 new = detail->alloc();
79 if (!new)
80 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070081 /* must fully initialise 'new', else
82 * we might get lose if we need to
83 * cache_put it soon.
84 */
NeilBrown15a5f6b2006-03-27 01:15:02 -080085 cache_init(new);
Neil Brown2f349312006-08-05 12:14:29 -070086 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080087
88 write_lock(&detail->hash_lock);
89
90 /* check if entry appeared while we slept */
91 for (hp=head; *hp != NULL ; hp = &(*hp)->next) {
92 struct cache_head *tmp = *hp;
93 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110094 if (cache_is_expired(detail, tmp)) {
95 *hp = tmp->next;
96 tmp->next = NULL;
97 detail->entries --;
98 freeme = tmp;
99 break;
100 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800101 cache_get(tmp);
102 write_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800103 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800104 return tmp;
105 }
106 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800107 new->next = *head;
108 *head = new;
109 detail->entries++;
110 cache_get(new);
111 write_unlock(&detail->hash_lock);
112
NeilBrownd202cce2010-02-03 17:31:31 +1100113 if (freeme)
114 cache_put(freeme, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800115 return new;
116}
Trond Myklebust24c37672008-12-23 16:30:12 -0500117EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800118
NeilBrownebd0cb12006-03-27 01:15:08 -0800119
NeilBrownf866a812009-08-04 15:22:38 +1000120static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800121
NeilBrown908329f2009-09-09 16:32:54 +1000122static void cache_fresh_locked(struct cache_head *head, time_t expiry)
NeilBrownebd0cb12006-03-27 01:15:08 -0800123{
124 head->expiry_time = expiry;
NeilBrownc5b29f82010-08-12 16:55:22 +1000125 head->last_refresh = seconds_since_boot();
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500126 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
NeilBrown908329f2009-09-09 16:32:54 +1000127 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800128}
129
130static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000131 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800132{
NeilBrownebd0cb12006-03-27 01:15:08 -0800133 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
134 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000135 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800136 }
137}
138
NeilBrown15a5f6b2006-03-27 01:15:02 -0800139struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
140 struct cache_head *new, struct cache_head *old, int hash)
141{
142 /* The 'old' entry is to be replaced by 'new'.
143 * If 'old' is not VALID, we update it directly,
144 * otherwise we need to replace it
145 */
146 struct cache_head **head;
147 struct cache_head *tmp;
148
149 if (!test_bit(CACHE_VALID, &old->flags)) {
150 write_lock(&detail->hash_lock);
151 if (!test_bit(CACHE_VALID, &old->flags)) {
152 if (test_bit(CACHE_NEGATIVE, &new->flags))
153 set_bit(CACHE_NEGATIVE, &old->flags);
154 else
155 detail->update(old, new);
NeilBrown908329f2009-09-09 16:32:54 +1000156 cache_fresh_locked(old, new->expiry_time);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800157 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000158 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800159 return old;
160 }
161 write_unlock(&detail->hash_lock);
162 }
163 /* We need to insert a new entry */
164 tmp = detail->alloc();
165 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800166 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800167 return NULL;
168 }
169 cache_init(tmp);
170 detail->init(tmp, old);
171 head = &detail->hash_table[hash];
172
173 write_lock(&detail->hash_lock);
174 if (test_bit(CACHE_NEGATIVE, &new->flags))
175 set_bit(CACHE_NEGATIVE, &tmp->flags);
176 else
177 detail->update(tmp, new);
178 tmp->next = *head;
179 *head = tmp;
NeilBrownf2d39582006-05-22 22:35:25 -0700180 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800181 cache_get(tmp);
NeilBrown908329f2009-09-09 16:32:54 +1000182 cache_fresh_locked(tmp, new->expiry_time);
NeilBrownebd0cb12006-03-27 01:15:08 -0800183 cache_fresh_locked(old, 0);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800184 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000185 cache_fresh_unlocked(tmp, detail);
186 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800187 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800188 return tmp;
189}
Trond Myklebust24c37672008-12-23 16:30:12 -0500190EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400192static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
193{
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +0300194 if (cd->cache_upcall)
195 return cd->cache_upcall(cd, h);
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +0300196 return sunrpc_cache_pipe_upcall(cd, h);
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400197}
NeilBrown989a19b2009-08-04 15:22:38 +1000198
chaoting fanb6040f92013-03-28 22:19:45 +0800199static inline int cache_is_valid(struct cache_head *h)
NeilBrown989a19b2009-08-04 15:22:38 +1000200{
NeilBrownd202cce2010-02-03 17:31:31 +1100201 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000202 return -EAGAIN;
203 else {
204 /* entry is valid */
205 if (test_bit(CACHE_NEGATIVE, &h->flags))
206 return -ENOENT;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500207 else {
208 /*
209 * In combination with write barrier in
210 * sunrpc_cache_update, ensures that anyone
211 * using the cache entry after this sees the
212 * updated contents:
213 */
214 smp_rmb();
NeilBrown989a19b2009-08-04 15:22:38 +1000215 return 0;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500216 }
NeilBrown989a19b2009-08-04 15:22:38 +1000217 }
218}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400219
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500220static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
221{
222 int rv;
223
224 write_lock(&detail->hash_lock);
chaoting fanb6040f92013-03-28 22:19:45 +0800225 rv = cache_is_valid(h);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000226 if (rv == -EAGAIN) {
227 set_bit(CACHE_NEGATIVE, &h->flags);
228 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
229 rv = -ENOENT;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500230 }
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500231 write_unlock(&detail->hash_lock);
232 cache_fresh_unlocked(h, detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000233 return rv;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/*
237 * This is the generic cache management routine for all
238 * the authentication caches.
239 * It checks the currency of a cache item and will (later)
240 * initiate an upcall to fill it if needed.
241 *
242 *
243 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000244 * -EAGAIN if upcall is pending and request has been queued
245 * -ETIMEDOUT if upcall failed or request could not be queue or
246 * upcall completed but item is still invalid (implying that
247 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * -ENOENT if cache entry was negative
249 */
250int cache_check(struct cache_detail *detail,
251 struct cache_head *h, struct cache_req *rqstp)
252{
253 int rv;
254 long refresh_age, age;
255
256 /* First decide return status as best we can */
chaoting fanb6040f92013-03-28 22:19:45 +0800257 rv = cache_is_valid(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* now see if we want to start an upcall */
260 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000261 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 if (rqstp == NULL) {
264 if (rv == -EAGAIN)
265 rv = -ENOENT;
NeilBrown0bebc632013-06-13 12:53:42 +1000266 } else if (rv == -EAGAIN ||
267 (h->expiry_time != 0 && age > refresh_age/2)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500268 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
269 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
271 switch (cache_make_upcall(detail, h)) {
272 case -EINVAL:
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500273 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 case -EAGAIN:
NeilBrown2a1c7f52013-06-13 12:53:42 +1000276 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
278 }
279 }
280 }
281
NeilBrown989a19b2009-08-04 15:22:38 +1000282 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500283 if (!cache_defer_req(rqstp, h)) {
284 /*
285 * Request was not deferred; handle it as best
286 * we can ourselves:
287 */
chaoting fanb6040f92013-03-28 22:19:45 +0800288 rv = cache_is_valid(h);
NeilBrown989a19b2009-08-04 15:22:38 +1000289 if (rv == -EAGAIN)
290 rv = -ETIMEDOUT;
291 }
292 }
NeilBrown4013ede2006-03-27 01:15:07 -0800293 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800294 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return rv;
296}
Trond Myklebust24c37672008-12-23 16:30:12 -0500297EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
300 * caches need to be periodically cleaned.
301 * For this we maintain a list of cache_detail and
302 * a current pointer into that list and into the table
303 * for that entry.
304 *
NeilBrown013920e2013-06-13 12:53:42 +1000305 * Each time cache_clean is called it finds the next non-empty entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * in the current table and walks the list in that entry
307 * looking for entries that can be removed.
308 *
309 * An entry gets removed if:
310 * - The expiry is before current time
311 * - The last_refresh time is before the flush_time for that cache
312 *
313 * later we might drop old entries with non-NEVER expiry if that table
314 * is getting 'full' for some definition of 'full'
315 *
316 * The question of "how often to scan a table" is an interesting one
317 * and is answered in part by the use of the "nextcheck" field in the
318 * cache_detail.
319 * When a scan of a table begins, the nextcheck field is set to a time
320 * that is well into the future.
321 * While scanning, if an expiry time is found that is earlier than the
322 * current nextcheck time, nextcheck is set to that expiry time.
323 * If the flush_time is ever set to a time earlier than the nextcheck
324 * time, the nextcheck time is then set to that flush_time.
325 *
326 * A table is then only scanned if the current time is at least
327 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800328 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
330
331static LIST_HEAD(cache_list);
332static DEFINE_SPINLOCK(cache_list_lock);
333static struct cache_detail *current_detail;
334static int current_index;
335
David Howells65f27f32006-11-22 14:55:48 +0000336static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300337static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300339void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 rwlock_init(&cd->hash_lock);
342 INIT_LIST_HEAD(&cd->queue);
343 spin_lock(&cache_list_lock);
344 cd->nextcheck = 0;
345 cd->entries = 0;
346 atomic_set(&cd->readers, 0);
347 cd->last_close = 0;
348 cd->last_warn = -1;
349 list_add(&cd->others, &cache_list);
350 spin_unlock(&cache_list_lock);
351
352 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000353 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300355EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300357void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 cache_purge(cd);
360 spin_lock(&cache_list_lock);
361 write_lock(&cd->hash_lock);
362 if (cd->entries || atomic_read(&cd->inuse)) {
363 write_unlock(&cd->hash_lock);
364 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367 if (current_detail == cd)
368 current_detail = NULL;
369 list_del_init(&cd->others);
370 write_unlock(&cd->hash_lock);
371 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (list_empty(&cache_list)) {
373 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400374 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500376 return;
377out:
Kinglong Meeecca0632014-04-15 17:13:56 +0800378 printk(KERN_ERR "RPC: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300380EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382/* clean cache tries to find something to clean
383 * and cleans it.
384 * It returns 1 if it cleaned something,
385 * 0 if it didn't find anything this time
386 * -1 if it fell off the end of the list.
387 */
388static int cache_clean(void)
389{
390 int rv = 0;
391 struct list_head *next;
392
393 spin_lock(&cache_list_lock);
394
395 /* find a suitable table if we don't already have one */
396 while (current_detail == NULL ||
397 current_index >= current_detail->hash_size) {
398 if (current_detail)
399 next = current_detail->others.next;
400 else
401 next = cache_list.next;
402 if (next == &cache_list) {
403 current_detail = NULL;
404 spin_unlock(&cache_list_lock);
405 return -1;
406 }
407 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000408 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 current_index = current_detail->hash_size;
410 else {
411 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000412 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415
416 /* find a non-empty bucket in the table */
417 while (current_detail &&
418 current_index < current_detail->hash_size &&
419 current_detail->hash_table[current_index] == NULL)
420 current_index++;
421
422 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (current_detail && current_index < current_detail->hash_size) {
425 struct cache_head *ch, **cp;
426 struct cache_detail *d;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 write_lock(&current_detail->hash_lock);
429
430 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 cp = & current_detail->hash_table[current_index];
NeilBrown3af49742010-02-03 17:31:31 +1100433 for (ch = *cp ; ch ; cp = & ch->next, ch = *cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (current_detail->nextcheck > ch->expiry_time)
435 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100436 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 *cp = ch->next;
440 ch->next = NULL;
441 current_detail->entries--;
442 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100443 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
NeilBrown3af49742010-02-03 17:31:31 +1100445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 write_unlock(&current_detail->hash_lock);
447 d = current_detail;
448 if (!ch)
449 current_index ++;
450 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000451 if (ch) {
NeilBrown013920e2013-06-13 12:53:42 +1000452 set_bit(CACHE_CLEANED, &ch->flags);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000453 cache_fresh_unlocked(ch, d);
NeilBrownbaab9352006-03-27 01:15:09 -0800454 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 } else
457 spin_unlock(&cache_list_lock);
458
459 return rv;
460}
461
462/*
463 * We want to regularly clean the cache, so we need to schedule some work ...
464 */
David Howells65f27f32006-11-22 14:55:48 +0000465static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 int delay = 5;
468 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700469 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 if (list_empty(&cache_list))
472 delay = 0;
473
474 if (delay)
475 schedule_delayed_work(&cache_cleaner, delay);
476}
477
478
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800479/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800481 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * be fully cleaned
483 */
484void cache_flush(void)
485{
486 while (cache_clean() != -1)
487 cond_resched();
488 while (cache_clean() != -1)
489 cond_resched();
490}
Trond Myklebust24c37672008-12-23 16:30:12 -0500491EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493void cache_purge(struct cache_detail *detail)
494{
495 detail->flush_time = LONG_MAX;
NeilBrownc5b29f82010-08-12 16:55:22 +1000496 detail->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 cache_flush();
498 detail->flush_time = 1;
499}
Trond Myklebust24c37672008-12-23 16:30:12 -0500500EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502
503/*
504 * Deferral and Revisiting of Requests.
505 *
506 * If a cache lookup finds a pending entry, we
507 * need to defer the request and revisit it later.
508 * All deferred requests are stored in a hash table,
509 * indexed by "struct cache_head *".
510 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800511 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * deferred form, which must contain a
513 * 'struct cache_deferred_req'
514 * This cache_deferred_req contains a method to allow
515 * it to be revisited when cache info is available
516 */
517
518#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
519#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
520
521#define DFR_MAX 300 /* ??? */
522
523static DEFINE_SPINLOCK(cache_defer_lock);
524static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000525static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526static int cache_defer_cnt;
527
J. Bruce Fields6610f722010-08-26 13:19:52 -0400528static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
NeilBrown11174492010-08-12 17:04:08 +1000530 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100531 if (!list_empty(&dreq->recent)) {
532 list_del_init(&dreq->recent);
533 cache_defer_cnt--;
534 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400535}
536
537static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
538{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int hash = DFR_HASH(item);
540
NeilBrowne33534d2010-10-07 15:29:46 +1100541 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000542 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400543}
544
NeilBrowne33534d2010-10-07 15:29:46 +1100545static void setup_deferral(struct cache_deferred_req *dreq,
546 struct cache_head *item,
547 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 spin_lock(&cache_defer_lock);
553
J. Bruce Fields6610f722010-08-26 13:19:52 -0400554 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
NeilBrowne33534d2010-10-07 15:29:46 +1100556 if (count_me) {
557 cache_defer_cnt++;
558 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
NeilBrowne33534d2010-10-07 15:29:46 +1100560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_unlock(&cache_defer_lock);
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
J. Bruce Fields3211af12010-08-26 16:56:23 -0400565struct thread_deferred_req {
566 struct cache_deferred_req handle;
567 struct completion completion;
568};
569
570static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
571{
572 struct thread_deferred_req *dr =
573 container_of(dreq, struct thread_deferred_req, handle);
574 complete(&dr->completion);
575}
576
NeilBrownd29068c2010-10-07 15:29:46 +1100577static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400578{
579 struct thread_deferred_req sleeper;
580 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400581
582 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
583 dreq->revisit = cache_restart_thread;
584
NeilBrowne33534d2010-10-07 15:29:46 +1100585 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400586
NeilBrownd29068c2010-10-07 15:29:46 +1100587 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000588 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400589 &sleeper.completion, req->thread_wait) <= 0) {
590 /* The completion wasn't completed, so we need
591 * to clean up
592 */
593 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000594 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400595 __unhash_deferred_req(&sleeper.handle);
596 spin_unlock(&cache_defer_lock);
597 } else {
598 /* cache_revisit_request already removed
599 * this from the hash table, but hasn't
600 * called ->revisit yet. It will very soon
601 * and we need to wait for it.
602 */
603 spin_unlock(&cache_defer_lock);
604 wait_for_completion(&sleeper.completion);
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400607}
608
NeilBrowne33534d2010-10-07 15:29:46 +1100609static void cache_limit_defers(void)
610{
611 /* Make sure we haven't exceed the limit of allowed deferred
612 * requests.
613 */
614 struct cache_deferred_req *discard = NULL;
615
616 if (cache_defer_cnt <= DFR_MAX)
617 return;
618
619 spin_lock(&cache_defer_lock);
620
621 /* Consider removing either the first or the last */
622 if (cache_defer_cnt > DFR_MAX) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500623 if (prandom_u32() & 1)
NeilBrowne33534d2010-10-07 15:29:46 +1100624 discard = list_entry(cache_defer_list.next,
625 struct cache_deferred_req, recent);
626 else
627 discard = list_entry(cache_defer_list.prev,
628 struct cache_deferred_req, recent);
629 __unhash_deferred_req(discard);
630 }
631 spin_unlock(&cache_defer_lock);
632 if (discard)
633 discard->revisit(discard, 1);
634}
635
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500636/* Return true if and only if a deferred request is queued. */
637static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400638{
639 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400640
J. Bruce Fields3211af12010-08-26 16:56:23 -0400641 if (req->thread_wait) {
NeilBrownd29068c2010-10-07 15:29:46 +1100642 cache_wait_req(req, item);
643 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500644 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400645 }
646 dreq = req->defer(req);
647 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500648 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100649 setup_deferral(dreq, item, 1);
NeilBrownd29068c2010-10-07 15:29:46 +1100650 if (!test_bit(CACHE_PENDING, &item->flags))
651 /* Bit could have been cleared before we managed to
652 * set up the deferral, so need to revisit just in case
653 */
654 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100655
656 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500657 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660static void cache_revisit_request(struct cache_head *item)
661{
662 struct cache_deferred_req *dreq;
663 struct list_head pending;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800664 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int hash = DFR_HASH(item);
666
667 INIT_LIST_HEAD(&pending);
668 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800669
Sasha Levinb67bfe02013-02-27 17:06:00 -0800670 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000671 if (dreq->item == item) {
672 __unhash_deferred_req(dreq);
673 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
NeilBrown11174492010-08-12 17:04:08 +1000675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 spin_unlock(&cache_defer_lock);
677
678 while (!list_empty(&pending)) {
679 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
680 list_del_init(&dreq->recent);
681 dreq->revisit(dreq, 0);
682 }
683}
684
685void cache_clean_deferred(void *owner)
686{
687 struct cache_deferred_req *dreq, *tmp;
688 struct list_head pending;
689
690
691 INIT_LIST_HEAD(&pending);
692 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
695 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400696 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000697 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 }
700 spin_unlock(&cache_defer_lock);
701
702 while (!list_empty(&pending)) {
703 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
704 list_del_init(&dreq->recent);
705 dreq->revisit(dreq, 1);
706 }
707}
708
709/*
710 * communicate with user-space
711 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500712 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
713 * On read, you get a full request, or block.
714 * On write, an update request is processed.
715 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800717 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500718 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * to the end and may wakeup and preceding readers.
720 * New readers are added to the head. If, on read, an item is found with
721 * CACHE_UPCALLING clear, we free it from the list.
722 *
723 */
724
725static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800726static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728struct cache_queue {
729 struct list_head list;
730 int reader; /* if 0, then request */
731};
732struct cache_request {
733 struct cache_queue q;
734 struct cache_head *item;
735 char * buf;
736 int len;
737 int readers;
738};
739struct cache_reader {
740 struct cache_queue q;
741 int offset; /* if non-0, we have a refcnt on next request */
742};
743
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300744static int cache_request(struct cache_detail *detail,
745 struct cache_request *crq)
746{
747 char *bp = crq->buf;
748 int len = PAGE_SIZE;
749
750 detail->cache_request(detail, crq->item, &bp, &len);
751 if (len < 0)
752 return -EAGAIN;
753 return PAGE_SIZE - len;
754}
755
Trond Myklebust173912a2009-08-09 15:14:29 -0400756static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
757 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct cache_reader *rp = filp->private_data;
760 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500761 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 int err;
763
764 if (count == 0)
765 return 0;
766
Trond Myklebustda770052009-08-09 15:14:28 -0400767 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * readers on this file */
769 again:
770 spin_lock(&queue_lock);
771 /* need to find next request */
772 while (rp->q.list.next != &cd->queue &&
773 list_entry(rp->q.list.next, struct cache_queue, list)
774 ->reader) {
775 struct list_head *next = rp->q.list.next;
776 list_move(&rp->q.list, next);
777 }
778 if (rp->q.list.next == &cd->queue) {
779 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400780 mutex_unlock(&inode->i_mutex);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400781 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
783 }
784 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400785 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (rp->offset == 0)
787 rq->readers++;
788 spin_unlock(&queue_lock);
789
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300790 if (rq->len == 0) {
791 err = cache_request(cd, rq);
792 if (err < 0)
793 goto out;
794 rq->len = err;
795 }
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
798 err = -EAGAIN;
799 spin_lock(&queue_lock);
800 list_move(&rp->q.list, &rq->q.list);
801 spin_unlock(&queue_lock);
802 } else {
803 if (rp->offset + count > rq->len)
804 count = rq->len - rp->offset;
805 err = -EFAULT;
806 if (copy_to_user(buf, rq->buf + rp->offset, count))
807 goto out;
808 rp->offset += count;
809 if (rp->offset >= rq->len) {
810 rp->offset = 0;
811 spin_lock(&queue_lock);
812 list_move(&rp->q.list, &rq->q.list);
813 spin_unlock(&queue_lock);
814 }
815 err = 0;
816 }
817 out:
818 if (rp->offset == 0) {
819 /* need to release rq */
820 spin_lock(&queue_lock);
821 rq->readers--;
822 if (rq->readers == 0 &&
823 !test_bit(CACHE_PENDING, &rq->item->flags)) {
824 list_del(&rq->q.list);
825 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800826 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 kfree(rq->buf);
828 kfree(rq);
829 } else
830 spin_unlock(&queue_lock);
831 }
832 if (err == -EAGAIN)
833 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400834 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return err ? err : count;
836}
837
Trond Myklebustda770052009-08-09 15:14:28 -0400838static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
839 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Trond Myklebustda770052009-08-09 15:14:28 -0400841 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300843 if (count == 0)
844 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400845 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400847 kaddr[count] = '\0';
848 ret = cd->cache_parse(cd, kaddr, count);
849 if (!ret)
850 ret = count;
851 return ret;
852}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Trond Myklebustda770052009-08-09 15:14:28 -0400854static ssize_t cache_slow_downcall(const char __user *buf,
855 size_t count, struct cache_detail *cd)
856{
857 static char write_buf[8192]; /* protected by queue_io_mutex */
858 ssize_t ret = -EINVAL;
859
860 if (count >= sizeof(write_buf))
861 goto out;
862 mutex_lock(&queue_io_mutex);
863 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800864 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400865out:
866 return ret;
867}
868
869static ssize_t cache_downcall(struct address_space *mapping,
870 const char __user *buf,
871 size_t count, struct cache_detail *cd)
872{
873 struct page *page;
874 char *kaddr;
875 ssize_t ret = -ENOMEM;
876
877 if (count >= PAGE_CACHE_SIZE)
878 goto out_slow;
879
880 page = find_or_create_page(mapping, 0, GFP_KERNEL);
881 if (!page)
882 goto out_slow;
883
884 kaddr = kmap(page);
885 ret = cache_do_downcall(kaddr, buf, count, cd);
886 kunmap(page);
887 unlock_page(page);
888 page_cache_release(page);
889 return ret;
890out_slow:
891 return cache_slow_downcall(buf, count, cd);
892}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Trond Myklebust173912a2009-08-09 15:14:29 -0400894static ssize_t cache_write(struct file *filp, const char __user *buf,
895 size_t count, loff_t *ppos,
896 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Trond Myklebustda770052009-08-09 15:14:28 -0400898 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500899 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400900 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Trond Myklebustda770052009-08-09 15:14:28 -0400902 if (!cd->cache_parse)
903 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Trond Myklebustda770052009-08-09 15:14:28 -0400905 mutex_lock(&inode->i_mutex);
906 ret = cache_downcall(mapping, buf, count, cd);
907 mutex_unlock(&inode->i_mutex);
908out:
909 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
913
Trond Myklebust173912a2009-08-09 15:14:29 -0400914static unsigned int cache_poll(struct file *filp, poll_table *wait,
915 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 unsigned int mask;
918 struct cache_reader *rp = filp->private_data;
919 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 poll_wait(filp, &queue_wait, wait);
922
923 /* alway allow write */
Al Viro1711fd9a2015-03-07 21:08:46 +0000924 mask = POLLOUT | POLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (!rp)
927 return mask;
928
929 spin_lock(&queue_lock);
930
931 for (cq= &rp->q; &cq->list != &cd->queue;
932 cq = list_entry(cq->list.next, struct cache_queue, list))
933 if (!cq->reader) {
934 mask |= POLLIN | POLLRDNORM;
935 break;
936 }
937 spin_unlock(&queue_lock);
938 return mask;
939}
940
Trond Myklebust173912a2009-08-09 15:14:29 -0400941static int cache_ioctl(struct inode *ino, struct file *filp,
942 unsigned int cmd, unsigned long arg,
943 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 int len = 0;
946 struct cache_reader *rp = filp->private_data;
947 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (cmd != FIONREAD || !rp)
950 return -EINVAL;
951
952 spin_lock(&queue_lock);
953
954 /* only find the length remaining in current request,
955 * or the length of the next request
956 */
957 for (cq= &rp->q; &cq->list != &cd->queue;
958 cq = list_entry(cq->list.next, struct cache_queue, list))
959 if (!cq->reader) {
960 struct cache_request *cr =
961 container_of(cq, struct cache_request, q);
962 len = cr->len - rp->offset;
963 break;
964 }
965 spin_unlock(&queue_lock);
966
967 return put_user(len, (int __user *)arg);
968}
969
Trond Myklebust173912a2009-08-09 15:14:29 -0400970static int cache_open(struct inode *inode, struct file *filp,
971 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct cache_reader *rp = NULL;
974
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400975 if (!cd || !try_module_get(cd->owner))
976 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 nonseekable_open(inode, filp);
978 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400980 if (!rp) {
981 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 rp->offset = 0;
985 rp->q.reader = 1;
986 atomic_inc(&cd->readers);
987 spin_lock(&queue_lock);
988 list_add(&rp->q.list, &cd->queue);
989 spin_unlock(&queue_lock);
990 }
991 filp->private_data = rp;
992 return 0;
993}
994
Trond Myklebust173912a2009-08-09 15:14:29 -0400995static int cache_release(struct inode *inode, struct file *filp,
996 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 if (rp) {
1001 spin_lock(&queue_lock);
1002 if (rp->offset) {
1003 struct cache_queue *cq;
1004 for (cq= &rp->q; &cq->list != &cd->queue;
1005 cq = list_entry(cq->list.next, struct cache_queue, list))
1006 if (!cq->reader) {
1007 container_of(cq, struct cache_request, q)
1008 ->readers--;
1009 break;
1010 }
1011 rp->offset = 0;
1012 }
1013 list_del(&rp->q.list);
1014 spin_unlock(&queue_lock);
1015
1016 filp->private_data = NULL;
1017 kfree(rp);
1018
NeilBrownc5b29f82010-08-12 16:55:22 +10001019 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 atomic_dec(&cd->readers);
1021 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001022 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return 0;
1024}
1025
1026
1027
NeilBrownf866a812009-08-04 15:22:38 +10001028static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001030 struct cache_queue *cq, *tmp;
1031 struct cache_request *cr;
1032 struct list_head dequeued;
1033
1034 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001036 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001038 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (cr->item != ch)
1040 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001041 if (test_bit(CACHE_PENDING, &ch->flags))
1042 /* Lost a race and it is pending again */
1043 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001045 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001046 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001049 while (!list_empty(&dequeued)) {
1050 cr = list_entry(dequeued.next, struct cache_request, q.list);
1051 list_del(&cr->q.list);
1052 cache_put(cr->item, detail);
1053 kfree(cr->buf);
1054 kfree(cr);
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058/*
1059 * Support routines for text-based upcalls.
1060 * Fields are separated by spaces.
1061 * Fields are either mangled to quote space tab newline slosh with slosh
1062 * or a hexified with a leading \x
1063 * Record is terminated with newline.
1064 *
1065 */
1066
1067void qword_add(char **bpp, int *lp, char *str)
1068{
1069 char *bp = *bpp;
1070 int len = *lp;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001071 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 if (len < 0) return;
1074
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001075 ret = string_escape_str(str, &bp, len, ESCAPE_OCTAL, "\\ \n\t");
1076 if (ret < 0 || ret == len)
1077 len = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 else {
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001079 len -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 *bp++ = ' ';
1081 len--;
1082 }
1083 *bpp = bp;
1084 *lp = len;
1085}
Trond Myklebust24c37672008-12-23 16:30:12 -05001086EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1089{
1090 char *bp = *bpp;
1091 int len = *lp;
1092
1093 if (len < 0) return;
1094
1095 if (len > 2) {
1096 *bp++ = '\\';
1097 *bp++ = 'x';
1098 len -= 2;
1099 while (blen && len >= 2) {
Andy Shevchenko056785e2013-12-12 15:49:21 +02001100 bp = hex_byte_pack(bp, *buf++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 len -= 2;
1102 blen--;
1103 }
1104 }
1105 if (blen || len<1) len = -1;
1106 else {
1107 *bp++ = ' ';
1108 len--;
1109 }
1110 *bpp = bp;
1111 *lp = len;
1112}
Trond Myklebust24c37672008-12-23 16:30:12 -05001113EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115static void warn_no_listener(struct cache_detail *detail)
1116{
1117 if (detail->last_warn != detail->last_close) {
1118 detail->last_warn = detail->last_close;
1119 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001120 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122}
1123
J. Bruce Fields06497522010-09-19 22:55:06 -04001124static bool cache_listeners_exist(struct cache_detail *detail)
1125{
1126 if (atomic_read(&detail->readers))
1127 return true;
1128 if (detail->last_close == 0)
1129 /* This cache was never opened */
1130 return false;
1131 if (detail->last_close < seconds_since_boot() - 30)
1132 /*
1133 * We allow for the possibility that someone might
1134 * restart a userspace daemon without restarting the
1135 * server; but after 30 seconds, we give up.
1136 */
1137 return false;
1138 return true;
1139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001142 * register an upcall request to user-space and queue it up for read() by the
1143 * upcall daemon.
1144 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * Each request is at most one page long.
1146 */
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +03001147int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149
1150 char *buf;
1151 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001152 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001154 if (!detail->cache_request)
1155 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
J. Bruce Fields06497522010-09-19 22:55:06 -04001157 if (!cache_listeners_exist(detail)) {
1158 warn_no_listener(detail);
1159 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
NeilBrown013920e2013-06-13 12:53:42 +10001161 if (test_bit(CACHE_CLEANED, &h->flags))
1162 /* Too late to make an upcall */
1163 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1166 if (!buf)
1167 return -EAGAIN;
1168
1169 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1170 if (!crq) {
1171 kfree(buf);
1172 return -EAGAIN;
1173 }
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 crq->q.reader = 0;
1176 crq->item = cache_get(h);
1177 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001178 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 crq->readers = 0;
1180 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001181 if (test_bit(CACHE_PENDING, &h->flags))
1182 list_add_tail(&crq->q.list, &detail->queue);
1183 else
1184 /* Lost a race, no longer PENDING, so don't enqueue */
1185 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 spin_unlock(&queue_lock);
1187 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001188 if (ret == -EAGAIN) {
1189 kfree(buf);
1190 kfree(crq);
1191 }
1192 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001194EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196/*
1197 * parse a message from user-space and pass it
1198 * to an appropriate cache
1199 * Messages are, like requests, separated into fields by
1200 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1201 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001202 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 * reply cachename expiry key ... content....
1204 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001205 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 */
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208int qword_get(char **bpp, char *dest, int bufsize)
1209{
1210 /* return bytes copied, or -1 on error */
1211 char *bp = *bpp;
1212 int len = 0;
1213
1214 while (*bp == ' ') bp++;
1215
1216 if (bp[0] == '\\' && bp[1] == 'x') {
1217 /* HEX STRING */
1218 bp += 2;
Andy Shevchenkoe7f483e2010-09-21 09:40:25 +03001219 while (len < bufsize) {
1220 int h, l;
1221
1222 h = hex_to_bin(bp[0]);
1223 if (h < 0)
1224 break;
1225
1226 l = hex_to_bin(bp[1]);
1227 if (l < 0)
1228 break;
1229
1230 *dest++ = (h << 4) | l;
1231 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 len++;
1233 }
1234 } else {
1235 /* text with \nnn octal quoting */
1236 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1237 if (*bp == '\\' &&
1238 isodigit(bp[1]) && (bp[1] <= '3') &&
1239 isodigit(bp[2]) &&
1240 isodigit(bp[3])) {
1241 int byte = (*++bp -'0');
1242 bp++;
1243 byte = (byte << 3) | (*bp++ - '0');
1244 byte = (byte << 3) | (*bp++ - '0');
1245 *dest++ = byte;
1246 len++;
1247 } else {
1248 *dest++ = *bp++;
1249 len++;
1250 }
1251 }
1252 }
1253
1254 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1255 return -1;
1256 while (*bp == ' ') bp++;
1257 *bpp = bp;
1258 *dest = '\0';
1259 return len;
1260}
Trond Myklebust24c37672008-12-23 16:30:12 -05001261EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263
1264/*
1265 * support /proc/sunrpc/cache/$CACHENAME/content
1266 * as a seqfile.
1267 * We call ->cache_show passing NULL for the item to
1268 * get a header, then pass each real item in the cache
1269 */
1270
1271struct handle {
1272 struct cache_detail *cd;
1273};
1274
1275static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001276 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001279 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 struct cache_head *ch;
1281 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 read_lock(&cd->hash_lock);
1285 if (!n--)
1286 return SEQ_START_TOKEN;
1287 hash = n >> 32;
1288 entry = n & ((1LL<<32) - 1);
1289
1290 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1291 if (!entry--)
1292 return ch;
1293 n &= ~((1LL<<32) - 1);
1294 do {
1295 hash++;
1296 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001297 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 cd->hash_table[hash]==NULL);
1299 if (hash >= cd->hash_size)
1300 return NULL;
1301 *pos = n+1;
1302 return cd->hash_table[hash];
1303}
1304
1305static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1306{
1307 struct cache_head *ch = p;
1308 int hash = (*pos >> 32);
1309 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1310
1311 if (p == SEQ_START_TOKEN)
1312 hash = 0;
1313 else if (ch->next == NULL) {
1314 hash++;
1315 *pos += 1LL<<32;
1316 } else {
1317 ++*pos;
1318 return ch->next;
1319 }
1320 *pos &= ~((1LL<<32) - 1);
1321 while (hash < cd->hash_size &&
1322 cd->hash_table[hash] == NULL) {
1323 hash++;
1324 *pos += 1LL<<32;
1325 }
1326 if (hash >= cd->hash_size)
1327 return NULL;
1328 ++*pos;
1329 return cd->hash_table[hash];
1330}
1331
1332static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001333 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1336 read_unlock(&cd->hash_lock);
1337}
1338
1339static int c_show(struct seq_file *m, void *p)
1340{
1341 struct cache_head *cp = p;
1342 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1343
1344 if (p == SEQ_START_TOKEN)
1345 return cd->cache_show(m, cd, NULL);
1346
1347 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001348 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001349 convert_to_wallclock(cp->expiry_time),
1350 atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 cache_get(cp);
1352 if (cache_check(cd, cp, NULL))
1353 /* cache_check does a cache_put on failure */
1354 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001355 else {
1356 if (cache_is_expired(cd, cp))
1357 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 return cd->cache_show(m, cd, cp);
1362}
1363
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001364static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 .start = c_start,
1366 .next = c_next,
1367 .stop = c_stop,
1368 .show = c_show,
1369};
1370
Trond Myklebust173912a2009-08-09 15:14:29 -04001371static int content_open(struct inode *inode, struct file *file,
1372 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 struct handle *han;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001376 if (!cd || !try_module_get(cd->owner))
1377 return -EACCES;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001378 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Li Zefana5990ea2010-03-11 14:08:10 -08001379 if (han == NULL) {
1380 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return -ENOMEM;
Li Zefana5990ea2010-03-11 14:08:10 -08001382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001388static int content_release(struct inode *inode, struct file *file,
1389 struct cache_detail *cd)
1390{
1391 int ret = seq_release_private(inode, file);
1392 module_put(cd->owner);
1393 return ret;
1394}
1395
1396static int open_flush(struct inode *inode, struct file *file,
1397 struct cache_detail *cd)
1398{
1399 if (!cd || !try_module_get(cd->owner))
1400 return -EACCES;
1401 return nonseekable_open(inode, file);
1402}
1403
1404static int release_flush(struct inode *inode, struct file *file,
1405 struct cache_detail *cd)
1406{
1407 module_put(cd->owner);
1408 return 0;
1409}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001412 size_t count, loff_t *ppos,
1413 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Sasha Levin212ba902012-07-17 00:01:26 +02001415 char tbuf[22];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001417 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Sasha Levin212ba902012-07-17 00:01:26 +02001419 snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 len = strlen(tbuf);
1421 if (p >= len)
1422 return 0;
1423 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001424 if (len > count)
1425 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001427 return -EFAULT;
1428 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return len;
1430}
1431
Trond Myklebust173912a2009-08-09 15:14:29 -04001432static ssize_t write_flush(struct file *file, const char __user *buf,
1433 size_t count, loff_t *ppos,
1434 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 char tbuf[20];
NeilBrownc5b29f82010-08-12 16:55:22 +10001437 char *bp, *ep;
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (*ppos || count > sizeof(tbuf)-1)
1440 return -EINVAL;
1441 if (copy_from_user(tbuf, buf, count))
1442 return -EFAULT;
1443 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001444 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (*ep && *ep != '\n')
1446 return -EINVAL;
1447
NeilBrownc5b29f82010-08-12 16:55:22 +10001448 bp = tbuf;
1449 cd->flush_time = get_expiry(&bp);
1450 cd->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 cache_flush();
1452
1453 *ppos += count;
1454 return count;
1455}
1456
Trond Myklebust173912a2009-08-09 15:14:29 -04001457static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1458 size_t count, loff_t *ppos)
1459{
Al Virod9dda782013-03-31 18:16:14 -04001460 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001461
1462 return cache_read(filp, buf, count, ppos, cd);
1463}
1464
1465static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1466 size_t count, loff_t *ppos)
1467{
Al Virod9dda782013-03-31 18:16:14 -04001468 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001469
1470 return cache_write(filp, buf, count, ppos, cd);
1471}
1472
1473static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
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_poll(filp, wait, cd);
1478}
1479
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001480static long cache_ioctl_procfs(struct file *filp,
1481 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001482{
Al Viro496ad9a2013-01-23 17:07:38 -05001483 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001484 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001485
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001486 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001487}
1488
1489static int cache_open_procfs(struct inode *inode, struct file *filp)
1490{
Al Virod9dda782013-03-31 18:16:14 -04001491 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001492
1493 return cache_open(inode, filp, cd);
1494}
1495
1496static int cache_release_procfs(struct inode *inode, struct file *filp)
1497{
Al Virod9dda782013-03-31 18:16:14 -04001498 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001499
1500 return cache_release(inode, filp, cd);
1501}
1502
1503static const struct file_operations cache_file_operations_procfs = {
1504 .owner = THIS_MODULE,
1505 .llseek = no_llseek,
1506 .read = cache_read_procfs,
1507 .write = cache_write_procfs,
1508 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001509 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001510 .open = cache_open_procfs,
1511 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512};
Trond Myklebust173912a2009-08-09 15:14:29 -04001513
1514static int content_open_procfs(struct inode *inode, struct file *filp)
1515{
Al Virod9dda782013-03-31 18:16:14 -04001516 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001517
1518 return content_open(inode, filp, cd);
1519}
1520
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001521static int content_release_procfs(struct inode *inode, struct file *filp)
1522{
Al Virod9dda782013-03-31 18:16:14 -04001523 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001524
1525 return content_release(inode, filp, cd);
1526}
1527
Trond Myklebust173912a2009-08-09 15:14:29 -04001528static const struct file_operations content_file_operations_procfs = {
1529 .open = content_open_procfs,
1530 .read = seq_read,
1531 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001532 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001533};
1534
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001535static int open_flush_procfs(struct inode *inode, struct file *filp)
1536{
Al Virod9dda782013-03-31 18:16:14 -04001537 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001538
1539 return open_flush(inode, filp, cd);
1540}
1541
1542static int release_flush_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 release_flush(inode, filp, cd);
1547}
1548
Trond Myklebust173912a2009-08-09 15:14:29 -04001549static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1550 size_t count, loff_t *ppos)
1551{
Al Virod9dda782013-03-31 18:16:14 -04001552 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001553
1554 return read_flush(filp, buf, count, ppos, cd);
1555}
1556
1557static ssize_t write_flush_procfs(struct file *filp,
1558 const char __user *buf,
1559 size_t count, loff_t *ppos)
1560{
Al Virod9dda782013-03-31 18:16:14 -04001561 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001562
1563 return write_flush(filp, buf, count, ppos, cd);
1564}
1565
1566static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001567 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001568 .read = read_flush_procfs,
1569 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001570 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001571 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001572};
1573
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001574static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001575{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001576 struct sunrpc_net *sn;
1577
Trond Myklebust173912a2009-08-09 15:14:29 -04001578 if (cd->u.procfs.proc_ent == NULL)
1579 return;
1580 if (cd->u.procfs.flush_ent)
1581 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1582 if (cd->u.procfs.channel_ent)
1583 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1584 if (cd->u.procfs.content_ent)
1585 remove_proc_entry("content", cd->u.procfs.proc_ent);
1586 cd->u.procfs.proc_ent = NULL;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001587 sn = net_generic(net, sunrpc_net_id);
1588 remove_proc_entry(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001589}
1590
1591#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001592static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001593{
1594 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001595 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001596
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001597 sn = net_generic(net, sunrpc_net_id);
1598 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001599 if (cd->u.procfs.proc_ent == NULL)
1600 goto out_nomem;
1601 cd->u.procfs.channel_ent = NULL;
1602 cd->u.procfs.content_ent = NULL;
1603
1604 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1605 cd->u.procfs.proc_ent,
1606 &cache_flush_operations_procfs, cd);
1607 cd->u.procfs.flush_ent = p;
1608 if (p == NULL)
1609 goto out_nomem;
1610
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001611 if (cd->cache_request || cd->cache_parse) {
Trond Myklebust173912a2009-08-09 15:14:29 -04001612 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1613 cd->u.procfs.proc_ent,
1614 &cache_file_operations_procfs, cd);
1615 cd->u.procfs.channel_ent = p;
1616 if (p == NULL)
1617 goto out_nomem;
1618 }
1619 if (cd->cache_show) {
Yanchuan Nianec168672013-01-04 19:45:35 +08001620 p = proc_create_data("content", S_IFREG|S_IRUSR,
Trond Myklebust173912a2009-08-09 15:14:29 -04001621 cd->u.procfs.proc_ent,
1622 &content_file_operations_procfs, cd);
1623 cd->u.procfs.content_ent = p;
1624 if (p == NULL)
1625 goto out_nomem;
1626 }
1627 return 0;
1628out_nomem:
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001629 remove_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001630 return -ENOMEM;
1631}
1632#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001633static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001634{
1635 return 0;
1636}
1637#endif
1638
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001639void __init cache_initialize(void)
1640{
Tejun Heo203b42f2012-08-21 13:18:23 -07001641 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001642}
1643
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001644int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001645{
1646 int ret;
1647
1648 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001649 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001650 if (ret)
1651 sunrpc_destroy_cache_detail(cd);
1652 return ret;
1653}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001654EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001655
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001656void cache_unregister_net(struct cache_detail *cd, struct net *net)
1657{
1658 remove_cache_proc_entries(cd, net);
1659 sunrpc_destroy_cache_detail(cd);
1660}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001661EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001662
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001663struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001664{
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001665 struct cache_detail *cd;
1666
1667 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1668 if (cd == NULL)
1669 return ERR_PTR(-ENOMEM);
1670
1671 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct cache_head *),
1672 GFP_KERNEL);
1673 if (cd->hash_table == NULL) {
1674 kfree(cd);
1675 return ERR_PTR(-ENOMEM);
1676 }
1677 cd->net = net;
1678 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001679}
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001680EXPORT_SYMBOL_GPL(cache_create_net);
1681
1682void cache_destroy_net(struct cache_detail *cd, struct net *net)
1683{
1684 kfree(cd->hash_table);
1685 kfree(cd);
1686}
1687EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001688
1689static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1690 size_t count, loff_t *ppos)
1691{
Al Viro496ad9a2013-01-23 17:07:38 -05001692 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001693
1694 return cache_read(filp, buf, count, ppos, cd);
1695}
1696
1697static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1698 size_t count, loff_t *ppos)
1699{
Al Viro496ad9a2013-01-23 17:07:38 -05001700 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001701
1702 return cache_write(filp, buf, count, ppos, cd);
1703}
1704
1705static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
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_poll(filp, wait, cd);
1710}
1711
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001712static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001713 unsigned int cmd, unsigned long arg)
1714{
Al Viro496ad9a2013-01-23 17:07:38 -05001715 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001716 struct cache_detail *cd = RPC_I(inode)->private;
1717
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001718 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001719}
1720
1721static int cache_open_pipefs(struct inode *inode, struct file *filp)
1722{
1723 struct cache_detail *cd = RPC_I(inode)->private;
1724
1725 return cache_open(inode, filp, cd);
1726}
1727
1728static int cache_release_pipefs(struct inode *inode, struct file *filp)
1729{
1730 struct cache_detail *cd = RPC_I(inode)->private;
1731
1732 return cache_release(inode, filp, cd);
1733}
1734
1735const struct file_operations cache_file_operations_pipefs = {
1736 .owner = THIS_MODULE,
1737 .llseek = no_llseek,
1738 .read = cache_read_pipefs,
1739 .write = cache_write_pipefs,
1740 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001741 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001742 .open = cache_open_pipefs,
1743 .release = cache_release_pipefs,
1744};
1745
1746static int content_open_pipefs(struct inode *inode, struct file *filp)
1747{
1748 struct cache_detail *cd = RPC_I(inode)->private;
1749
1750 return content_open(inode, filp, cd);
1751}
1752
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001753static int content_release_pipefs(struct inode *inode, struct file *filp)
1754{
1755 struct cache_detail *cd = RPC_I(inode)->private;
1756
1757 return content_release(inode, filp, cd);
1758}
1759
Trond Myklebust8854e822009-08-09 15:14:30 -04001760const struct file_operations content_file_operations_pipefs = {
1761 .open = content_open_pipefs,
1762 .read = seq_read,
1763 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001764 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001765};
1766
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001767static int open_flush_pipefs(struct inode *inode, struct file *filp)
1768{
1769 struct cache_detail *cd = RPC_I(inode)->private;
1770
1771 return open_flush(inode, filp, cd);
1772}
1773
1774static int release_flush_pipefs(struct inode *inode, struct file *filp)
1775{
1776 struct cache_detail *cd = RPC_I(inode)->private;
1777
1778 return release_flush(inode, filp, cd);
1779}
1780
Trond Myklebust8854e822009-08-09 15:14:30 -04001781static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1782 size_t count, loff_t *ppos)
1783{
Al Viro496ad9a2013-01-23 17:07:38 -05001784 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001785
1786 return read_flush(filp, buf, count, ppos, cd);
1787}
1788
1789static ssize_t write_flush_pipefs(struct file *filp,
1790 const char __user *buf,
1791 size_t count, loff_t *ppos)
1792{
Al Viro496ad9a2013-01-23 17:07:38 -05001793 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001794
1795 return write_flush(filp, buf, count, ppos, cd);
1796}
1797
1798const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001799 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001800 .read = read_flush_pipefs,
1801 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001802 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001803 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001804};
1805
1806int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001807 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001808 struct cache_detail *cd)
1809{
Al Viroa95e6912013-07-14 16:43:54 +04001810 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1811 if (IS_ERR(dir))
1812 return PTR_ERR(dir);
1813 cd->u.pipefs.dir = dir;
1814 return 0;
Trond Myklebust8854e822009-08-09 15:14:30 -04001815}
1816EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1817
1818void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1819{
1820 rpc_remove_cache_dir(cd->u.pipefs.dir);
1821 cd->u.pipefs.dir = NULL;
Trond Myklebust8854e822009-08-09 15:14:30 -04001822}
1823EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1824