blob: 29c463396a2ddc7b3e2dbc87cac255209c6e9763 [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 *
309 * Each time clean_cache is called it finds the next non-empty entry
310 * 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) {
NeilBrown2a1c7f52013-06-13 12:53:42 +1000456 cache_fresh_unlocked(ch, d);
NeilBrownbaab9352006-03-27 01:15:09 -0800457 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 } else
460 spin_unlock(&cache_list_lock);
461
462 return rv;
463}
464
465/*
466 * We want to regularly clean the cache, so we need to schedule some work ...
467 */
David Howells65f27f32006-11-22 14:55:48 +0000468static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 int delay = 5;
471 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700472 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 if (list_empty(&cache_list))
475 delay = 0;
476
477 if (delay)
478 schedule_delayed_work(&cache_cleaner, delay);
479}
480
481
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800482/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800484 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * be fully cleaned
486 */
487void cache_flush(void)
488{
489 while (cache_clean() != -1)
490 cond_resched();
491 while (cache_clean() != -1)
492 cond_resched();
493}
Trond Myklebust24c37672008-12-23 16:30:12 -0500494EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496void cache_purge(struct cache_detail *detail)
497{
498 detail->flush_time = LONG_MAX;
NeilBrownc5b29f82010-08-12 16:55:22 +1000499 detail->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 cache_flush();
501 detail->flush_time = 1;
502}
Trond Myklebust24c37672008-12-23 16:30:12 -0500503EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505
506/*
507 * Deferral and Revisiting of Requests.
508 *
509 * If a cache lookup finds a pending entry, we
510 * need to defer the request and revisit it later.
511 * All deferred requests are stored in a hash table,
512 * indexed by "struct cache_head *".
513 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800514 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * deferred form, which must contain a
516 * 'struct cache_deferred_req'
517 * This cache_deferred_req contains a method to allow
518 * it to be revisited when cache info is available
519 */
520
521#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
522#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
523
524#define DFR_MAX 300 /* ??? */
525
526static DEFINE_SPINLOCK(cache_defer_lock);
527static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000528static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529static int cache_defer_cnt;
530
J. Bruce Fields6610f722010-08-26 13:19:52 -0400531static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
NeilBrown11174492010-08-12 17:04:08 +1000533 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100534 if (!list_empty(&dreq->recent)) {
535 list_del_init(&dreq->recent);
536 cache_defer_cnt--;
537 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400538}
539
540static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
541{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 int hash = DFR_HASH(item);
543
NeilBrowne33534d2010-10-07 15:29:46 +1100544 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000545 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400546}
547
NeilBrowne33534d2010-10-07 15:29:46 +1100548static void setup_deferral(struct cache_deferred_req *dreq,
549 struct cache_head *item,
550 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 spin_lock(&cache_defer_lock);
556
J. Bruce Fields6610f722010-08-26 13:19:52 -0400557 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
NeilBrowne33534d2010-10-07 15:29:46 +1100559 if (count_me) {
560 cache_defer_cnt++;
561 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
NeilBrowne33534d2010-10-07 15:29:46 +1100563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 spin_unlock(&cache_defer_lock);
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
J. Bruce Fields3211af12010-08-26 16:56:23 -0400568struct thread_deferred_req {
569 struct cache_deferred_req handle;
570 struct completion completion;
571};
572
573static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
574{
575 struct thread_deferred_req *dr =
576 container_of(dreq, struct thread_deferred_req, handle);
577 complete(&dr->completion);
578}
579
NeilBrownd29068c42010-10-07 15:29:46 +1100580static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400581{
582 struct thread_deferred_req sleeper;
583 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400584
585 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
586 dreq->revisit = cache_restart_thread;
587
NeilBrowne33534d2010-10-07 15:29:46 +1100588 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400589
NeilBrownd29068c42010-10-07 15:29:46 +1100590 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000591 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400592 &sleeper.completion, req->thread_wait) <= 0) {
593 /* The completion wasn't completed, so we need
594 * to clean up
595 */
596 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000597 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400598 __unhash_deferred_req(&sleeper.handle);
599 spin_unlock(&cache_defer_lock);
600 } else {
601 /* cache_revisit_request already removed
602 * this from the hash table, but hasn't
603 * called ->revisit yet. It will very soon
604 * and we need to wait for it.
605 */
606 spin_unlock(&cache_defer_lock);
607 wait_for_completion(&sleeper.completion);
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400610}
611
NeilBrowne33534d2010-10-07 15:29:46 +1100612static void cache_limit_defers(void)
613{
614 /* Make sure we haven't exceed the limit of allowed deferred
615 * requests.
616 */
617 struct cache_deferred_req *discard = NULL;
618
619 if (cache_defer_cnt <= DFR_MAX)
620 return;
621
622 spin_lock(&cache_defer_lock);
623
624 /* Consider removing either the first or the last */
625 if (cache_defer_cnt > DFR_MAX) {
626 if (net_random() & 1)
627 discard = list_entry(cache_defer_list.next,
628 struct cache_deferred_req, recent);
629 else
630 discard = list_entry(cache_defer_list.prev,
631 struct cache_deferred_req, recent);
632 __unhash_deferred_req(discard);
633 }
634 spin_unlock(&cache_defer_lock);
635 if (discard)
636 discard->revisit(discard, 1);
637}
638
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500639/* Return true if and only if a deferred request is queued. */
640static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400641{
642 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400643
J. Bruce Fields3211af12010-08-26 16:56:23 -0400644 if (req->thread_wait) {
NeilBrownd29068c42010-10-07 15:29:46 +1100645 cache_wait_req(req, item);
646 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500647 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400648 }
649 dreq = req->defer(req);
650 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500651 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100652 setup_deferral(dreq, item, 1);
NeilBrownd29068c42010-10-07 15:29:46 +1100653 if (!test_bit(CACHE_PENDING, &item->flags))
654 /* Bit could have been cleared before we managed to
655 * set up the deferral, so need to revisit just in case
656 */
657 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100658
659 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500660 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663static void cache_revisit_request(struct cache_head *item)
664{
665 struct cache_deferred_req *dreq;
666 struct list_head pending;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800667 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int hash = DFR_HASH(item);
669
670 INIT_LIST_HEAD(&pending);
671 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800672
Sasha Levinb67bfe02013-02-27 17:06:00 -0800673 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000674 if (dreq->item == item) {
675 __unhash_deferred_req(dreq);
676 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
NeilBrown11174492010-08-12 17:04:08 +1000678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 spin_unlock(&cache_defer_lock);
680
681 while (!list_empty(&pending)) {
682 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
683 list_del_init(&dreq->recent);
684 dreq->revisit(dreq, 0);
685 }
686}
687
688void cache_clean_deferred(void *owner)
689{
690 struct cache_deferred_req *dreq, *tmp;
691 struct list_head pending;
692
693
694 INIT_LIST_HEAD(&pending);
695 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
698 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400699 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000700 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 }
703 spin_unlock(&cache_defer_lock);
704
705 while (!list_empty(&pending)) {
706 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
707 list_del_init(&dreq->recent);
708 dreq->revisit(dreq, 1);
709 }
710}
711
712/*
713 * communicate with user-space
714 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500715 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
716 * On read, you get a full request, or block.
717 * On write, an update request is processed.
718 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800720 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500721 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 * to the end and may wakeup and preceding readers.
723 * New readers are added to the head. If, on read, an item is found with
724 * CACHE_UPCALLING clear, we free it from the list.
725 *
726 */
727
728static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800729static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731struct cache_queue {
732 struct list_head list;
733 int reader; /* if 0, then request */
734};
735struct cache_request {
736 struct cache_queue q;
737 struct cache_head *item;
738 char * buf;
739 int len;
740 int readers;
741};
742struct cache_reader {
743 struct cache_queue q;
744 int offset; /* if non-0, we have a refcnt on next request */
745};
746
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300747static int cache_request(struct cache_detail *detail,
748 struct cache_request *crq)
749{
750 char *bp = crq->buf;
751 int len = PAGE_SIZE;
752
753 detail->cache_request(detail, crq->item, &bp, &len);
754 if (len < 0)
755 return -EAGAIN;
756 return PAGE_SIZE - len;
757}
758
Trond Myklebust173912a2009-08-09 15:14:29 -0400759static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
760 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct cache_reader *rp = filp->private_data;
763 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500764 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int err;
766
767 if (count == 0)
768 return 0;
769
Trond Myklebustda770052009-08-09 15:14:28 -0400770 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 * readers on this file */
772 again:
773 spin_lock(&queue_lock);
774 /* need to find next request */
775 while (rp->q.list.next != &cd->queue &&
776 list_entry(rp->q.list.next, struct cache_queue, list)
777 ->reader) {
778 struct list_head *next = rp->q.list.next;
779 list_move(&rp->q.list, next);
780 }
781 if (rp->q.list.next == &cd->queue) {
782 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400783 mutex_unlock(&inode->i_mutex);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400784 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786 }
787 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400788 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (rp->offset == 0)
790 rq->readers++;
791 spin_unlock(&queue_lock);
792
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300793 if (rq->len == 0) {
794 err = cache_request(cd, rq);
795 if (err < 0)
796 goto out;
797 rq->len = err;
798 }
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
801 err = -EAGAIN;
802 spin_lock(&queue_lock);
803 list_move(&rp->q.list, &rq->q.list);
804 spin_unlock(&queue_lock);
805 } else {
806 if (rp->offset + count > rq->len)
807 count = rq->len - rp->offset;
808 err = -EFAULT;
809 if (copy_to_user(buf, rq->buf + rp->offset, count))
810 goto out;
811 rp->offset += count;
812 if (rp->offset >= rq->len) {
813 rp->offset = 0;
814 spin_lock(&queue_lock);
815 list_move(&rp->q.list, &rq->q.list);
816 spin_unlock(&queue_lock);
817 }
818 err = 0;
819 }
820 out:
821 if (rp->offset == 0) {
822 /* need to release rq */
823 spin_lock(&queue_lock);
824 rq->readers--;
825 if (rq->readers == 0 &&
826 !test_bit(CACHE_PENDING, &rq->item->flags)) {
827 list_del(&rq->q.list);
828 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800829 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 kfree(rq->buf);
831 kfree(rq);
832 } else
833 spin_unlock(&queue_lock);
834 }
835 if (err == -EAGAIN)
836 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400837 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return err ? err : count;
839}
840
Trond Myklebustda770052009-08-09 15:14:28 -0400841static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
842 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Trond Myklebustda770052009-08-09 15:14:28 -0400844 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300846 if (count == 0)
847 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400848 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400850 kaddr[count] = '\0';
851 ret = cd->cache_parse(cd, kaddr, count);
852 if (!ret)
853 ret = count;
854 return ret;
855}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Trond Myklebustda770052009-08-09 15:14:28 -0400857static ssize_t cache_slow_downcall(const char __user *buf,
858 size_t count, struct cache_detail *cd)
859{
860 static char write_buf[8192]; /* protected by queue_io_mutex */
861 ssize_t ret = -EINVAL;
862
863 if (count >= sizeof(write_buf))
864 goto out;
865 mutex_lock(&queue_io_mutex);
866 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800867 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400868out:
869 return ret;
870}
871
872static ssize_t cache_downcall(struct address_space *mapping,
873 const char __user *buf,
874 size_t count, struct cache_detail *cd)
875{
876 struct page *page;
877 char *kaddr;
878 ssize_t ret = -ENOMEM;
879
880 if (count >= PAGE_CACHE_SIZE)
881 goto out_slow;
882
883 page = find_or_create_page(mapping, 0, GFP_KERNEL);
884 if (!page)
885 goto out_slow;
886
887 kaddr = kmap(page);
888 ret = cache_do_downcall(kaddr, buf, count, cd);
889 kunmap(page);
890 unlock_page(page);
891 page_cache_release(page);
892 return ret;
893out_slow:
894 return cache_slow_downcall(buf, count, cd);
895}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Trond Myklebust173912a2009-08-09 15:14:29 -0400897static ssize_t cache_write(struct file *filp, const char __user *buf,
898 size_t count, loff_t *ppos,
899 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Trond Myklebustda770052009-08-09 15:14:28 -0400901 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500902 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400903 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Trond Myklebustda770052009-08-09 15:14:28 -0400905 if (!cd->cache_parse)
906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Trond Myklebustda770052009-08-09 15:14:28 -0400908 mutex_lock(&inode->i_mutex);
909 ret = cache_downcall(mapping, buf, count, cd);
910 mutex_unlock(&inode->i_mutex);
911out:
912 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
916
Trond Myklebust173912a2009-08-09 15:14:29 -0400917static unsigned int cache_poll(struct file *filp, poll_table *wait,
918 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 unsigned int mask;
921 struct cache_reader *rp = filp->private_data;
922 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 poll_wait(filp, &queue_wait, wait);
925
926 /* alway allow write */
927 mask = POLL_OUT | POLLWRNORM;
928
929 if (!rp)
930 return mask;
931
932 spin_lock(&queue_lock);
933
934 for (cq= &rp->q; &cq->list != &cd->queue;
935 cq = list_entry(cq->list.next, struct cache_queue, list))
936 if (!cq->reader) {
937 mask |= POLLIN | POLLRDNORM;
938 break;
939 }
940 spin_unlock(&queue_lock);
941 return mask;
942}
943
Trond Myklebust173912a2009-08-09 15:14:29 -0400944static int cache_ioctl(struct inode *ino, struct file *filp,
945 unsigned int cmd, unsigned long arg,
946 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 int len = 0;
949 struct cache_reader *rp = filp->private_data;
950 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 if (cmd != FIONREAD || !rp)
953 return -EINVAL;
954
955 spin_lock(&queue_lock);
956
957 /* only find the length remaining in current request,
958 * or the length of the next request
959 */
960 for (cq= &rp->q; &cq->list != &cd->queue;
961 cq = list_entry(cq->list.next, struct cache_queue, list))
962 if (!cq->reader) {
963 struct cache_request *cr =
964 container_of(cq, struct cache_request, q);
965 len = cr->len - rp->offset;
966 break;
967 }
968 spin_unlock(&queue_lock);
969
970 return put_user(len, (int __user *)arg);
971}
972
Trond Myklebust173912a2009-08-09 15:14:29 -0400973static int cache_open(struct inode *inode, struct file *filp,
974 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct cache_reader *rp = NULL;
977
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400978 if (!cd || !try_module_get(cd->owner))
979 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 nonseekable_open(inode, filp);
981 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400983 if (!rp) {
984 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 rp->offset = 0;
988 rp->q.reader = 1;
989 atomic_inc(&cd->readers);
990 spin_lock(&queue_lock);
991 list_add(&rp->q.list, &cd->queue);
992 spin_unlock(&queue_lock);
993 }
994 filp->private_data = rp;
995 return 0;
996}
997
Trond Myklebust173912a2009-08-09 15:14:29 -0400998static int cache_release(struct inode *inode, struct file *filp,
999 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (rp) {
1004 spin_lock(&queue_lock);
1005 if (rp->offset) {
1006 struct cache_queue *cq;
1007 for (cq= &rp->q; &cq->list != &cd->queue;
1008 cq = list_entry(cq->list.next, struct cache_queue, list))
1009 if (!cq->reader) {
1010 container_of(cq, struct cache_request, q)
1011 ->readers--;
1012 break;
1013 }
1014 rp->offset = 0;
1015 }
1016 list_del(&rp->q.list);
1017 spin_unlock(&queue_lock);
1018
1019 filp->private_data = NULL;
1020 kfree(rp);
1021
NeilBrownc5b29f82010-08-12 16:55:22 +10001022 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 atomic_dec(&cd->readers);
1024 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001025 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return 0;
1027}
1028
1029
1030
NeilBrownf866a812009-08-04 15:22:38 +10001031static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001033 struct cache_queue *cq, *tmp;
1034 struct cache_request *cr;
1035 struct list_head dequeued;
1036
1037 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001039 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001041 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (cr->item != ch)
1043 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001044 if (test_bit(CACHE_PENDING, &ch->flags))
1045 /* Lost a race and it is pending again */
1046 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001048 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001049 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001052 while (!list_empty(&dequeued)) {
1053 cr = list_entry(dequeued.next, struct cache_request, q.list);
1054 list_del(&cr->q.list);
1055 cache_put(cr->item, detail);
1056 kfree(cr->buf);
1057 kfree(cr);
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061/*
1062 * Support routines for text-based upcalls.
1063 * Fields are separated by spaces.
1064 * Fields are either mangled to quote space tab newline slosh with slosh
1065 * or a hexified with a leading \x
1066 * Record is terminated with newline.
1067 *
1068 */
1069
1070void qword_add(char **bpp, int *lp, char *str)
1071{
1072 char *bp = *bpp;
1073 int len = *lp;
1074 char c;
1075
1076 if (len < 0) return;
1077
1078 while ((c=*str++) && len)
1079 switch(c) {
1080 case ' ':
1081 case '\t':
1082 case '\n':
1083 case '\\':
1084 if (len >= 4) {
1085 *bp++ = '\\';
1086 *bp++ = '0' + ((c & 0300)>>6);
1087 *bp++ = '0' + ((c & 0070)>>3);
1088 *bp++ = '0' + ((c & 0007)>>0);
1089 }
1090 len -= 4;
1091 break;
1092 default:
1093 *bp++ = c;
1094 len--;
1095 }
1096 if (c || len <1) len = -1;
1097 else {
1098 *bp++ = ' ';
1099 len--;
1100 }
1101 *bpp = bp;
1102 *lp = len;
1103}
Trond Myklebust24c37672008-12-23 16:30:12 -05001104EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1107{
1108 char *bp = *bpp;
1109 int len = *lp;
1110
1111 if (len < 0) return;
1112
1113 if (len > 2) {
1114 *bp++ = '\\';
1115 *bp++ = 'x';
1116 len -= 2;
1117 while (blen && len >= 2) {
1118 unsigned char c = *buf++;
1119 *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
1120 *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
1121 len -= 2;
1122 blen--;
1123 }
1124 }
1125 if (blen || len<1) len = -1;
1126 else {
1127 *bp++ = ' ';
1128 len--;
1129 }
1130 *bpp = bp;
1131 *lp = len;
1132}
Trond Myklebust24c37672008-12-23 16:30:12 -05001133EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135static void warn_no_listener(struct cache_detail *detail)
1136{
1137 if (detail->last_warn != detail->last_close) {
1138 detail->last_warn = detail->last_close;
1139 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001140 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142}
1143
J. Bruce Fields06497522010-09-19 22:55:06 -04001144static bool cache_listeners_exist(struct cache_detail *detail)
1145{
1146 if (atomic_read(&detail->readers))
1147 return true;
1148 if (detail->last_close == 0)
1149 /* This cache was never opened */
1150 return false;
1151 if (detail->last_close < seconds_since_boot() - 30)
1152 /*
1153 * We allow for the possibility that someone might
1154 * restart a userspace daemon without restarting the
1155 * server; but after 30 seconds, we give up.
1156 */
1157 return false;
1158 return true;
1159}
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001162 * register an upcall request to user-space and queue it up for read() by the
1163 * upcall daemon.
1164 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 * Each request is at most one page long.
1166 */
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +03001167int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169
1170 char *buf;
1171 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001172 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001174 if (!detail->cache_request)
1175 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
J. Bruce Fields06497522010-09-19 22:55:06 -04001177 if (!cache_listeners_exist(detail)) {
1178 warn_no_listener(detail);
1179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
1182 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1183 if (!buf)
1184 return -EAGAIN;
1185
1186 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1187 if (!crq) {
1188 kfree(buf);
1189 return -EAGAIN;
1190 }
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 crq->q.reader = 0;
1193 crq->item = cache_get(h);
1194 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001195 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 crq->readers = 0;
1197 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001198 if (test_bit(CACHE_PENDING, &h->flags))
1199 list_add_tail(&crq->q.list, &detail->queue);
1200 else
1201 /* Lost a race, no longer PENDING, so don't enqueue */
1202 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 spin_unlock(&queue_lock);
1204 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001205 if (ret == -EAGAIN) {
1206 kfree(buf);
1207 kfree(crq);
1208 }
1209 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001211EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213/*
1214 * parse a message from user-space and pass it
1215 * to an appropriate cache
1216 * Messages are, like requests, separated into fields by
1217 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1218 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001219 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 * reply cachename expiry key ... content....
1221 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001222 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225int qword_get(char **bpp, char *dest, int bufsize)
1226{
1227 /* return bytes copied, or -1 on error */
1228 char *bp = *bpp;
1229 int len = 0;
1230
1231 while (*bp == ' ') bp++;
1232
1233 if (bp[0] == '\\' && bp[1] == 'x') {
1234 /* HEX STRING */
1235 bp += 2;
Andy Shevchenkoe7f483e2010-09-21 09:40:25 +03001236 while (len < bufsize) {
1237 int h, l;
1238
1239 h = hex_to_bin(bp[0]);
1240 if (h < 0)
1241 break;
1242
1243 l = hex_to_bin(bp[1]);
1244 if (l < 0)
1245 break;
1246
1247 *dest++ = (h << 4) | l;
1248 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 len++;
1250 }
1251 } else {
1252 /* text with \nnn octal quoting */
1253 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1254 if (*bp == '\\' &&
1255 isodigit(bp[1]) && (bp[1] <= '3') &&
1256 isodigit(bp[2]) &&
1257 isodigit(bp[3])) {
1258 int byte = (*++bp -'0');
1259 bp++;
1260 byte = (byte << 3) | (*bp++ - '0');
1261 byte = (byte << 3) | (*bp++ - '0');
1262 *dest++ = byte;
1263 len++;
1264 } else {
1265 *dest++ = *bp++;
1266 len++;
1267 }
1268 }
1269 }
1270
1271 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1272 return -1;
1273 while (*bp == ' ') bp++;
1274 *bpp = bp;
1275 *dest = '\0';
1276 return len;
1277}
Trond Myklebust24c37672008-12-23 16:30:12 -05001278EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280
1281/*
1282 * support /proc/sunrpc/cache/$CACHENAME/content
1283 * as a seqfile.
1284 * We call ->cache_show passing NULL for the item to
1285 * get a header, then pass each real item in the cache
1286 */
1287
1288struct handle {
1289 struct cache_detail *cd;
1290};
1291
1292static void *c_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001293 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001296 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct cache_head *ch;
1298 struct cache_detail *cd = ((struct handle*)m->private)->cd;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 read_lock(&cd->hash_lock);
1302 if (!n--)
1303 return SEQ_START_TOKEN;
1304 hash = n >> 32;
1305 entry = n & ((1LL<<32) - 1);
1306
1307 for (ch=cd->hash_table[hash]; ch; ch=ch->next)
1308 if (!entry--)
1309 return ch;
1310 n &= ~((1LL<<32) - 1);
1311 do {
1312 hash++;
1313 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001314 } while(hash < cd->hash_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 cd->hash_table[hash]==NULL);
1316 if (hash >= cd->hash_size)
1317 return NULL;
1318 *pos = n+1;
1319 return cd->hash_table[hash];
1320}
1321
1322static void *c_next(struct seq_file *m, void *p, loff_t *pos)
1323{
1324 struct cache_head *ch = p;
1325 int hash = (*pos >> 32);
1326 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1327
1328 if (p == SEQ_START_TOKEN)
1329 hash = 0;
1330 else if (ch->next == NULL) {
1331 hash++;
1332 *pos += 1LL<<32;
1333 } else {
1334 ++*pos;
1335 return ch->next;
1336 }
1337 *pos &= ~((1LL<<32) - 1);
1338 while (hash < cd->hash_size &&
1339 cd->hash_table[hash] == NULL) {
1340 hash++;
1341 *pos += 1LL<<32;
1342 }
1343 if (hash >= cd->hash_size)
1344 return NULL;
1345 ++*pos;
1346 return cd->hash_table[hash];
1347}
1348
1349static void c_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001350 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1353 read_unlock(&cd->hash_lock);
1354}
1355
1356static int c_show(struct seq_file *m, void *p)
1357{
1358 struct cache_head *cp = p;
1359 struct cache_detail *cd = ((struct handle*)m->private)->cd;
1360
1361 if (p == SEQ_START_TOKEN)
1362 return cd->cache_show(m, cd, NULL);
1363
1364 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001365 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001366 convert_to_wallclock(cp->expiry_time),
1367 atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 cache_get(cp);
1369 if (cache_check(cd, cp, NULL))
1370 /* cache_check does a cache_put on failure */
1371 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001372 else {
1373 if (cache_is_expired(cd, cp))
1374 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 return cd->cache_show(m, cd, cp);
1379}
1380
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001381static const struct seq_operations cache_content_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 .start = c_start,
1383 .next = c_next,
1384 .stop = c_stop,
1385 .show = c_show,
1386};
1387
Trond Myklebust173912a2009-08-09 15:14:29 -04001388static int content_open(struct inode *inode, struct file *file,
1389 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 struct handle *han;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001393 if (!cd || !try_module_get(cd->owner))
1394 return -EACCES;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001395 han = __seq_open_private(file, &cache_content_op, sizeof(*han));
Li Zefana5990ea2010-03-11 14:08:10 -08001396 if (han == NULL) {
1397 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return -ENOMEM;
Li Zefana5990ea2010-03-11 14:08:10 -08001399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 han->cd = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001405static int content_release(struct inode *inode, struct file *file,
1406 struct cache_detail *cd)
1407{
1408 int ret = seq_release_private(inode, file);
1409 module_put(cd->owner);
1410 return ret;
1411}
1412
1413static int open_flush(struct inode *inode, struct file *file,
1414 struct cache_detail *cd)
1415{
1416 if (!cd || !try_module_get(cd->owner))
1417 return -EACCES;
1418 return nonseekable_open(inode, file);
1419}
1420
1421static int release_flush(struct inode *inode, struct file *file,
1422 struct cache_detail *cd)
1423{
1424 module_put(cd->owner);
1425 return 0;
1426}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001429 size_t count, loff_t *ppos,
1430 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Sasha Levin212ba902012-07-17 00:01:26 +02001432 char tbuf[22];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001434 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Sasha Levin212ba902012-07-17 00:01:26 +02001436 snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 len = strlen(tbuf);
1438 if (p >= len)
1439 return 0;
1440 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001441 if (len > count)
1442 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001444 return -EFAULT;
1445 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return len;
1447}
1448
Trond Myklebust173912a2009-08-09 15:14:29 -04001449static ssize_t write_flush(struct file *file, const char __user *buf,
1450 size_t count, loff_t *ppos,
1451 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 char tbuf[20];
NeilBrownc5b29f82010-08-12 16:55:22 +10001454 char *bp, *ep;
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (*ppos || count > sizeof(tbuf)-1)
1457 return -EINVAL;
1458 if (copy_from_user(tbuf, buf, count))
1459 return -EFAULT;
1460 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001461 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (*ep && *ep != '\n')
1463 return -EINVAL;
1464
NeilBrownc5b29f82010-08-12 16:55:22 +10001465 bp = tbuf;
1466 cd->flush_time = get_expiry(&bp);
1467 cd->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 cache_flush();
1469
1470 *ppos += count;
1471 return count;
1472}
1473
Trond Myklebust173912a2009-08-09 15:14:29 -04001474static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1475 size_t count, loff_t *ppos)
1476{
Al Virod9dda782013-03-31 18:16:14 -04001477 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001478
1479 return cache_read(filp, buf, count, ppos, cd);
1480}
1481
1482static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1483 size_t count, loff_t *ppos)
1484{
Al Virod9dda782013-03-31 18:16:14 -04001485 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001486
1487 return cache_write(filp, buf, count, ppos, cd);
1488}
1489
1490static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1491{
Al Virod9dda782013-03-31 18:16:14 -04001492 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001493
1494 return cache_poll(filp, wait, cd);
1495}
1496
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001497static long cache_ioctl_procfs(struct file *filp,
1498 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001499{
Al Viro496ad9a2013-01-23 17:07:38 -05001500 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001501 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001502
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001503 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001504}
1505
1506static int cache_open_procfs(struct inode *inode, struct file *filp)
1507{
Al Virod9dda782013-03-31 18:16:14 -04001508 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001509
1510 return cache_open(inode, filp, cd);
1511}
1512
1513static int cache_release_procfs(struct inode *inode, struct file *filp)
1514{
Al Virod9dda782013-03-31 18:16:14 -04001515 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001516
1517 return cache_release(inode, filp, cd);
1518}
1519
1520static const struct file_operations cache_file_operations_procfs = {
1521 .owner = THIS_MODULE,
1522 .llseek = no_llseek,
1523 .read = cache_read_procfs,
1524 .write = cache_write_procfs,
1525 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001526 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001527 .open = cache_open_procfs,
1528 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529};
Trond Myklebust173912a2009-08-09 15:14:29 -04001530
1531static int content_open_procfs(struct inode *inode, struct file *filp)
1532{
Al Virod9dda782013-03-31 18:16:14 -04001533 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001534
1535 return content_open(inode, filp, cd);
1536}
1537
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001538static int content_release_procfs(struct inode *inode, struct file *filp)
1539{
Al Virod9dda782013-03-31 18:16:14 -04001540 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001541
1542 return content_release(inode, filp, cd);
1543}
1544
Trond Myklebust173912a2009-08-09 15:14:29 -04001545static const struct file_operations content_file_operations_procfs = {
1546 .open = content_open_procfs,
1547 .read = seq_read,
1548 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001549 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001550};
1551
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001552static int open_flush_procfs(struct inode *inode, struct file *filp)
1553{
Al Virod9dda782013-03-31 18:16:14 -04001554 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001555
1556 return open_flush(inode, filp, cd);
1557}
1558
1559static int release_flush_procfs(struct inode *inode, struct file *filp)
1560{
Al Virod9dda782013-03-31 18:16:14 -04001561 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001562
1563 return release_flush(inode, filp, cd);
1564}
1565
Trond Myklebust173912a2009-08-09 15:14:29 -04001566static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1567 size_t count, loff_t *ppos)
1568{
Al Virod9dda782013-03-31 18:16:14 -04001569 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001570
1571 return read_flush(filp, buf, count, ppos, cd);
1572}
1573
1574static ssize_t write_flush_procfs(struct file *filp,
1575 const char __user *buf,
1576 size_t count, loff_t *ppos)
1577{
Al Virod9dda782013-03-31 18:16:14 -04001578 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001579
1580 return write_flush(filp, buf, count, ppos, cd);
1581}
1582
1583static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001584 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001585 .read = read_flush_procfs,
1586 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001587 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001588 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001589};
1590
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001591static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001592{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001593 struct sunrpc_net *sn;
1594
Trond Myklebust173912a2009-08-09 15:14:29 -04001595 if (cd->u.procfs.proc_ent == NULL)
1596 return;
1597 if (cd->u.procfs.flush_ent)
1598 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1599 if (cd->u.procfs.channel_ent)
1600 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1601 if (cd->u.procfs.content_ent)
1602 remove_proc_entry("content", cd->u.procfs.proc_ent);
1603 cd->u.procfs.proc_ent = NULL;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001604 sn = net_generic(net, sunrpc_net_id);
1605 remove_proc_entry(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001606}
1607
1608#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001609static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001610{
1611 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001612 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001613
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001614 sn = net_generic(net, sunrpc_net_id);
1615 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001616 if (cd->u.procfs.proc_ent == NULL)
1617 goto out_nomem;
1618 cd->u.procfs.channel_ent = NULL;
1619 cd->u.procfs.content_ent = NULL;
1620
1621 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1622 cd->u.procfs.proc_ent,
1623 &cache_flush_operations_procfs, cd);
1624 cd->u.procfs.flush_ent = p;
1625 if (p == NULL)
1626 goto out_nomem;
1627
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001628 if (cd->cache_request || cd->cache_parse) {
Trond Myklebust173912a2009-08-09 15:14:29 -04001629 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1630 cd->u.procfs.proc_ent,
1631 &cache_file_operations_procfs, cd);
1632 cd->u.procfs.channel_ent = p;
1633 if (p == NULL)
1634 goto out_nomem;
1635 }
1636 if (cd->cache_show) {
Yanchuan Nianec168672013-01-04 19:45:35 +08001637 p = proc_create_data("content", S_IFREG|S_IRUSR,
Trond Myklebust173912a2009-08-09 15:14:29 -04001638 cd->u.procfs.proc_ent,
1639 &content_file_operations_procfs, cd);
1640 cd->u.procfs.content_ent = p;
1641 if (p == NULL)
1642 goto out_nomem;
1643 }
1644 return 0;
1645out_nomem:
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001646 remove_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001647 return -ENOMEM;
1648}
1649#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001650static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001651{
1652 return 0;
1653}
1654#endif
1655
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001656void __init cache_initialize(void)
1657{
Tejun Heo203b42f2012-08-21 13:18:23 -07001658 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001659}
1660
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001661int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001662{
1663 int ret;
1664
1665 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001666 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001667 if (ret)
1668 sunrpc_destroy_cache_detail(cd);
1669 return ret;
1670}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001671EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001672
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001673void cache_unregister_net(struct cache_detail *cd, struct net *net)
1674{
1675 remove_cache_proc_entries(cd, net);
1676 sunrpc_destroy_cache_detail(cd);
1677}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001678EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001679
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001680struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001681{
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001682 struct cache_detail *cd;
1683
1684 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1685 if (cd == NULL)
1686 return ERR_PTR(-ENOMEM);
1687
1688 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct cache_head *),
1689 GFP_KERNEL);
1690 if (cd->hash_table == NULL) {
1691 kfree(cd);
1692 return ERR_PTR(-ENOMEM);
1693 }
1694 cd->net = net;
1695 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001696}
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001697EXPORT_SYMBOL_GPL(cache_create_net);
1698
1699void cache_destroy_net(struct cache_detail *cd, struct net *net)
1700{
1701 kfree(cd->hash_table);
1702 kfree(cd);
1703}
1704EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001705
1706static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1707 size_t count, loff_t *ppos)
1708{
Al Viro496ad9a2013-01-23 17:07:38 -05001709 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001710
1711 return cache_read(filp, buf, count, ppos, cd);
1712}
1713
1714static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1715 size_t count, loff_t *ppos)
1716{
Al Viro496ad9a2013-01-23 17:07:38 -05001717 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001718
1719 return cache_write(filp, buf, count, ppos, cd);
1720}
1721
1722static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1723{
Al Viro496ad9a2013-01-23 17:07:38 -05001724 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001725
1726 return cache_poll(filp, wait, cd);
1727}
1728
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001729static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001730 unsigned int cmd, unsigned long arg)
1731{
Al Viro496ad9a2013-01-23 17:07:38 -05001732 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001733 struct cache_detail *cd = RPC_I(inode)->private;
1734
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001735 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001736}
1737
1738static int cache_open_pipefs(struct inode *inode, struct file *filp)
1739{
1740 struct cache_detail *cd = RPC_I(inode)->private;
1741
1742 return cache_open(inode, filp, cd);
1743}
1744
1745static int cache_release_pipefs(struct inode *inode, struct file *filp)
1746{
1747 struct cache_detail *cd = RPC_I(inode)->private;
1748
1749 return cache_release(inode, filp, cd);
1750}
1751
1752const struct file_operations cache_file_operations_pipefs = {
1753 .owner = THIS_MODULE,
1754 .llseek = no_llseek,
1755 .read = cache_read_pipefs,
1756 .write = cache_write_pipefs,
1757 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001758 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001759 .open = cache_open_pipefs,
1760 .release = cache_release_pipefs,
1761};
1762
1763static int content_open_pipefs(struct inode *inode, struct file *filp)
1764{
1765 struct cache_detail *cd = RPC_I(inode)->private;
1766
1767 return content_open(inode, filp, cd);
1768}
1769
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001770static int content_release_pipefs(struct inode *inode, struct file *filp)
1771{
1772 struct cache_detail *cd = RPC_I(inode)->private;
1773
1774 return content_release(inode, filp, cd);
1775}
1776
Trond Myklebust8854e822009-08-09 15:14:30 -04001777const struct file_operations content_file_operations_pipefs = {
1778 .open = content_open_pipefs,
1779 .read = seq_read,
1780 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001781 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001782};
1783
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001784static int open_flush_pipefs(struct inode *inode, struct file *filp)
1785{
1786 struct cache_detail *cd = RPC_I(inode)->private;
1787
1788 return open_flush(inode, filp, cd);
1789}
1790
1791static int release_flush_pipefs(struct inode *inode, struct file *filp)
1792{
1793 struct cache_detail *cd = RPC_I(inode)->private;
1794
1795 return release_flush(inode, filp, cd);
1796}
1797
Trond Myklebust8854e822009-08-09 15:14:30 -04001798static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1799 size_t count, loff_t *ppos)
1800{
Al Viro496ad9a2013-01-23 17:07:38 -05001801 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001802
1803 return read_flush(filp, buf, count, ppos, cd);
1804}
1805
1806static ssize_t write_flush_pipefs(struct file *filp,
1807 const char __user *buf,
1808 size_t count, loff_t *ppos)
1809{
Al Viro496ad9a2013-01-23 17:07:38 -05001810 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001811
1812 return write_flush(filp, buf, count, ppos, cd);
1813}
1814
1815const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001816 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001817 .read = read_flush_pipefs,
1818 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001819 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001820 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001821};
1822
1823int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001824 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001825 struct cache_detail *cd)
1826{
1827 struct qstr q;
1828 struct dentry *dir;
1829 int ret = 0;
1830
Trond Myklebust8854e822009-08-09 15:14:30 -04001831 q.name = name;
1832 q.len = strlen(name);
1833 q.hash = full_name_hash(q.name, q.len);
1834 dir = rpc_create_cache_dir(parent, &q, umode, cd);
1835 if (!IS_ERR(dir))
1836 cd->u.pipefs.dir = dir;
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +03001837 else
Trond Myklebust8854e822009-08-09 15:14:30 -04001838 ret = PTR_ERR(dir);
Trond Myklebust8854e822009-08-09 15:14:30 -04001839 return ret;
1840}
1841EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1842
1843void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1844{
1845 rpc_remove_cache_dir(cd->u.pipefs.dir);
1846 cd->u.pipefs.dir = NULL;
Trond Myklebust8854e822009-08-09 15:14:30 -04001847}
1848EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1849