blob: 24e42919a4800fba1f5c4062e750ad001fe59854 [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
Neil Brown77862032015-10-16 08:59:08 +110044static void cache_init(struct cache_head *h, struct cache_detail *detail)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
NeilBrownc5b29f82010-08-12 16:55:22 +100046 time_t now = seconds_since_boot();
Kinglong Mee129e5822015-07-27 11:10:15 +080047 INIT_HLIST_NODE(&h->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 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;
Neil Brown77862032015-10-16 08:59:08 +110051 if (now <= detail->flush_time)
52 /* ensure it isn't already expired */
53 now = detail->flush_time + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 h->last_refresh = now;
55}
56
Vasily Averin9369b7d2018-11-28 11:45:57 +030057static void cache_fresh_unlocked(struct cache_head *head,
58 struct cache_detail *detail);
59
NeilBrown15a5f6b2006-03-27 01:15:02 -080060struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
61 struct cache_head *key, int hash)
62{
Kinglong Mee129e5822015-07-27 11:10:15 +080063 struct cache_head *new = NULL, *freeme = NULL, *tmp = NULL;
64 struct hlist_head *head;
NeilBrown15a5f6b2006-03-27 01:15:02 -080065
66 head = &detail->hash_table[hash];
67
68 read_lock(&detail->hash_lock);
69
Kinglong Mee129e5822015-07-27 11:10:15 +080070 hlist_for_each_entry(tmp, head, cache_list) {
NeilBrown15a5f6b2006-03-27 01:15:02 -080071 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 */
Neil Brown77862032015-10-16 08:59:08 +110090 cache_init(new, detail);
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 */
Kinglong Mee129e5822015-07-27 11:10:15 +080096 hlist_for_each_entry(tmp, head, cache_list) {
NeilBrown15a5f6b2006-03-27 01:15:02 -080097 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110098 if (cache_is_expired(detail, tmp)) {
Kinglong Mee129e5822015-07-27 11:10:15 +080099 hlist_del_init(&tmp->cache_list);
NeilBrownd202cce2010-02-03 17:31:31 +1100100 detail->entries --;
101 freeme = tmp;
102 break;
103 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800104 cache_get(tmp);
105 write_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800106 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800107 return tmp;
108 }
109 }
Kinglong Mee129e5822015-07-27 11:10:15 +0800110
111 hlist_add_head(&new->cache_list, head);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800112 detail->entries++;
113 cache_get(new);
114 write_unlock(&detail->hash_lock);
115
Vasily Averin9369b7d2018-11-28 11:45:57 +0300116 if (freeme) {
117 cache_fresh_unlocked(freeme, detail);
NeilBrownd202cce2010-02-03 17:31:31 +1100118 cache_put(freeme, detail);
Vasily Averin9369b7d2018-11-28 11:45:57 +0300119 }
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
Neil Brown77862032015-10-16 08:59:08 +1100127static void cache_fresh_locked(struct cache_head *head, time_t expiry,
128 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800129{
Neil Brown77862032015-10-16 08:59:08 +1100130 time_t now = seconds_since_boot();
131 if (now <= detail->flush_time)
132 /* ensure it isn't immediately treated as expired */
133 now = detail->flush_time + 1;
NeilBrownebd0cb12006-03-27 01:15:08 -0800134 head->expiry_time = expiry;
Neil Brown77862032015-10-16 08:59:08 +1100135 head->last_refresh = now;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500136 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
NeilBrown908329f2009-09-09 16:32:54 +1000137 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800138}
139
140static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000141 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800142{
NeilBrownebd0cb12006-03-27 01:15:08 -0800143 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
144 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000145 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800146 }
147}
148
NeilBrown15a5f6b2006-03-27 01:15:02 -0800149struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
150 struct cache_head *new, struct cache_head *old, int hash)
151{
152 /* The 'old' entry is to be replaced by 'new'.
153 * If 'old' is not VALID, we update it directly,
154 * otherwise we need to replace it
155 */
NeilBrown15a5f6b2006-03-27 01:15:02 -0800156 struct cache_head *tmp;
157
158 if (!test_bit(CACHE_VALID, &old->flags)) {
159 write_lock(&detail->hash_lock);
160 if (!test_bit(CACHE_VALID, &old->flags)) {
161 if (test_bit(CACHE_NEGATIVE, &new->flags))
162 set_bit(CACHE_NEGATIVE, &old->flags);
163 else
164 detail->update(old, new);
Neil Brown77862032015-10-16 08:59:08 +1100165 cache_fresh_locked(old, new->expiry_time, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800166 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000167 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800168 return old;
169 }
170 write_unlock(&detail->hash_lock);
171 }
172 /* We need to insert a new entry */
173 tmp = detail->alloc();
174 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800175 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800176 return NULL;
177 }
Neil Brown77862032015-10-16 08:59:08 +1100178 cache_init(tmp, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800179 detail->init(tmp, old);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800180
181 write_lock(&detail->hash_lock);
182 if (test_bit(CACHE_NEGATIVE, &new->flags))
183 set_bit(CACHE_NEGATIVE, &tmp->flags);
184 else
185 detail->update(tmp, new);
Kinglong Mee129e5822015-07-27 11:10:15 +0800186 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
NeilBrownf2d39582006-05-22 22:35:25 -0700187 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800188 cache_get(tmp);
Neil Brown77862032015-10-16 08:59:08 +1100189 cache_fresh_locked(tmp, new->expiry_time, detail);
190 cache_fresh_locked(old, 0, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800191 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000192 cache_fresh_unlocked(tmp, detail);
193 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800194 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800195 return tmp;
196}
Trond Myklebust24c37672008-12-23 16:30:12 -0500197EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400199static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
200{
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +0300201 if (cd->cache_upcall)
202 return cd->cache_upcall(cd, h);
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +0300203 return sunrpc_cache_pipe_upcall(cd, h);
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400204}
NeilBrown989a19b2009-08-04 15:22:38 +1000205
chaoting fanb6040f92013-03-28 22:19:45 +0800206static inline int cache_is_valid(struct cache_head *h)
NeilBrown989a19b2009-08-04 15:22:38 +1000207{
NeilBrownd202cce2010-02-03 17:31:31 +1100208 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000209 return -EAGAIN;
210 else {
211 /* entry is valid */
212 if (test_bit(CACHE_NEGATIVE, &h->flags))
213 return -ENOENT;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500214 else {
215 /*
216 * In combination with write barrier in
217 * sunrpc_cache_update, ensures that anyone
218 * using the cache entry after this sees the
219 * updated contents:
220 */
221 smp_rmb();
NeilBrown989a19b2009-08-04 15:22:38 +1000222 return 0;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500223 }
NeilBrown989a19b2009-08-04 15:22:38 +1000224 }
225}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400226
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500227static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
228{
229 int rv;
230
231 write_lock(&detail->hash_lock);
chaoting fanb6040f92013-03-28 22:19:45 +0800232 rv = cache_is_valid(h);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000233 if (rv == -EAGAIN) {
234 set_bit(CACHE_NEGATIVE, &h->flags);
Neil Brown77862032015-10-16 08:59:08 +1100235 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
236 detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000237 rv = -ENOENT;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500238 }
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500239 write_unlock(&detail->hash_lock);
240 cache_fresh_unlocked(h, detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000241 return rv;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
245 * This is the generic cache management routine for all
246 * the authentication caches.
247 * It checks the currency of a cache item and will (later)
248 * initiate an upcall to fill it if needed.
249 *
250 *
251 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000252 * -EAGAIN if upcall is pending and request has been queued
253 * -ETIMEDOUT if upcall failed or request could not be queue or
254 * upcall completed but item is still invalid (implying that
255 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * -ENOENT if cache entry was negative
257 */
258int cache_check(struct cache_detail *detail,
259 struct cache_head *h, struct cache_req *rqstp)
260{
261 int rv;
262 long refresh_age, age;
263
264 /* First decide return status as best we can */
chaoting fanb6040f92013-03-28 22:19:45 +0800265 rv = cache_is_valid(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 /* now see if we want to start an upcall */
268 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000269 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (rqstp == NULL) {
272 if (rv == -EAGAIN)
273 rv = -ENOENT;
NeilBrown0bebc6332013-06-13 12:53:42 +1000274 } else if (rv == -EAGAIN ||
275 (h->expiry_time != 0 && age > refresh_age/2)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500276 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
277 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
279 switch (cache_make_upcall(detail, h)) {
280 case -EINVAL:
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500281 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 case -EAGAIN:
NeilBrown2a1c7f52013-06-13 12:53:42 +1000284 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286 }
287 }
288 }
289
NeilBrown989a19b2009-08-04 15:22:38 +1000290 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500291 if (!cache_defer_req(rqstp, h)) {
292 /*
293 * Request was not deferred; handle it as best
294 * we can ourselves:
295 */
chaoting fanb6040f92013-03-28 22:19:45 +0800296 rv = cache_is_valid(h);
NeilBrown989a19b2009-08-04 15:22:38 +1000297 if (rv == -EAGAIN)
298 rv = -ETIMEDOUT;
299 }
300 }
NeilBrown4013ede2006-03-27 01:15:07 -0800301 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800302 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return rv;
304}
Trond Myklebust24c37672008-12-23 16:30:12 -0500305EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/*
308 * caches need to be periodically cleaned.
309 * For this we maintain a list of cache_detail and
310 * a current pointer into that list and into the table
311 * for that entry.
312 *
NeilBrown013920e2013-06-13 12:53:42 +1000313 * Each time cache_clean is called it finds the next non-empty entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * in the current table and walks the list in that entry
315 * looking for entries that can be removed.
316 *
317 * An entry gets removed if:
318 * - The expiry is before current time
319 * - The last_refresh time is before the flush_time for that cache
320 *
321 * later we might drop old entries with non-NEVER expiry if that table
322 * is getting 'full' for some definition of 'full'
323 *
324 * The question of "how often to scan a table" is an interesting one
325 * and is answered in part by the use of the "nextcheck" field in the
326 * cache_detail.
327 * When a scan of a table begins, the nextcheck field is set to a time
328 * that is well into the future.
329 * While scanning, if an expiry time is found that is earlier than the
330 * current nextcheck time, nextcheck is set to that expiry time.
331 * If the flush_time is ever set to a time earlier than the nextcheck
332 * time, the nextcheck time is then set to that flush_time.
333 *
334 * A table is then only scanned if the current time is at least
335 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800336 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
338
339static LIST_HEAD(cache_list);
340static DEFINE_SPINLOCK(cache_list_lock);
341static struct cache_detail *current_detail;
342static int current_index;
343
David Howells65f27f32006-11-22 14:55:48 +0000344static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300345static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300347void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500348{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 rwlock_init(&cd->hash_lock);
350 INIT_LIST_HEAD(&cd->queue);
351 spin_lock(&cache_list_lock);
352 cd->nextcheck = 0;
353 cd->entries = 0;
354 atomic_set(&cd->readers, 0);
355 cd->last_close = 0;
356 cd->last_warn = -1;
357 list_add(&cd->others, &cache_list);
358 spin_unlock(&cache_list_lock);
359
360 /* start the cleaning process */
Ke Wang77b00bc2016-09-01 15:30:26 +0800361 queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300363EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300365void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 cache_purge(cd);
368 spin_lock(&cache_list_lock);
369 write_lock(&cd->hash_lock);
NeilBrownd8d29132016-06-02 16:31:03 +1000370 if (cd->entries) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 write_unlock(&cd->hash_lock);
372 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500373 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 if (current_detail == cd)
376 current_detail = NULL;
377 list_del_init(&cd->others);
378 write_unlock(&cd->hash_lock);
379 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (list_empty(&cache_list)) {
381 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400382 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500384 return;
385out:
Kinglong Meeecca0632014-04-15 17:13:56 +0800386 printk(KERN_ERR "RPC: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300388EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390/* clean cache tries to find something to clean
391 * and cleans it.
392 * It returns 1 if it cleaned something,
393 * 0 if it didn't find anything this time
394 * -1 if it fell off the end of the list.
395 */
396static int cache_clean(void)
397{
398 int rv = 0;
399 struct list_head *next;
400
401 spin_lock(&cache_list_lock);
402
403 /* find a suitable table if we don't already have one */
404 while (current_detail == NULL ||
405 current_index >= current_detail->hash_size) {
406 if (current_detail)
407 next = current_detail->others.next;
408 else
409 next = cache_list.next;
410 if (next == &cache_list) {
411 current_detail = NULL;
412 spin_unlock(&cache_list_lock);
413 return -1;
414 }
415 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000416 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 current_index = current_detail->hash_size;
418 else {
419 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000420 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 }
423
424 /* find a non-empty bucket in the table */
425 while (current_detail &&
426 current_index < current_detail->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +0800427 hlist_empty(&current_detail->hash_table[current_index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 current_index++;
429
430 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (current_detail && current_index < current_detail->hash_size) {
Kinglong Mee129e5822015-07-27 11:10:15 +0800433 struct cache_head *ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct cache_detail *d;
Kinglong Mee129e5822015-07-27 11:10:15 +0800435 struct hlist_head *head;
436 struct hlist_node *tmp;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 write_lock(&current_detail->hash_lock);
439
440 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800441
Kinglong Mee129e5822015-07-27 11:10:15 +0800442 head = &current_detail->hash_table[current_index];
443 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (current_detail->nextcheck > ch->expiry_time)
445 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100446 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Kinglong Mee129e5822015-07-27 11:10:15 +0800449 hlist_del_init(&ch->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 current_detail->entries--;
451 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
NeilBrown3af49742010-02-03 17:31:31 +1100454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 write_unlock(&current_detail->hash_lock);
456 d = current_detail;
457 if (!ch)
458 current_index ++;
459 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000460 if (ch) {
NeilBrown013920e2013-06-13 12:53:42 +1000461 set_bit(CACHE_CLEANED, &ch->flags);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000462 cache_fresh_unlocked(ch, d);
NeilBrownbaab9352006-03-27 01:15:09 -0800463 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 } else
466 spin_unlock(&cache_list_lock);
467
468 return rv;
469}
470
471/*
472 * We want to regularly clean the cache, so we need to schedule some work ...
473 */
David Howells65f27f32006-11-22 14:55:48 +0000474static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 int delay = 5;
477 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700478 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 if (list_empty(&cache_list))
481 delay = 0;
482
483 if (delay)
Ke Wang77b00bc2016-09-01 15:30:26 +0800484 queue_delayed_work(system_power_efficient_wq,
485 &cache_cleaner, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800489/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800491 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * be fully cleaned
493 */
494void cache_flush(void)
495{
496 while (cache_clean() != -1)
497 cond_resched();
498 while (cache_clean() != -1)
499 cond_resched();
500}
Trond Myklebust24c37672008-12-23 16:30:12 -0500501EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503void cache_purge(struct cache_detail *detail)
504{
Neil Brown77862032015-10-16 08:59:08 +1100505 time_t now = seconds_since_boot();
506 if (detail->flush_time >= now)
507 now = detail->flush_time + 1;
508 /* 'now' is the maximum value any 'last_refresh' can have */
509 detail->flush_time = now;
NeilBrownc5b29f82010-08-12 16:55:22 +1000510 detail->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 cache_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
Trond Myklebust24c37672008-12-23 16:30:12 -0500513EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515
516/*
517 * Deferral and Revisiting of Requests.
518 *
519 * If a cache lookup finds a pending entry, we
520 * need to defer the request and revisit it later.
521 * All deferred requests are stored in a hash table,
522 * indexed by "struct cache_head *".
523 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800524 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * deferred form, which must contain a
526 * 'struct cache_deferred_req'
527 * This cache_deferred_req contains a method to allow
528 * it to be revisited when cache info is available
529 */
530
531#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
532#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
533
534#define DFR_MAX 300 /* ??? */
535
536static DEFINE_SPINLOCK(cache_defer_lock);
537static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000538static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539static int cache_defer_cnt;
540
J. Bruce Fields6610f722010-08-26 13:19:52 -0400541static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
NeilBrown11174492010-08-12 17:04:08 +1000543 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100544 if (!list_empty(&dreq->recent)) {
545 list_del_init(&dreq->recent);
546 cache_defer_cnt--;
547 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400548}
549
550static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
551{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 int hash = DFR_HASH(item);
553
NeilBrowne33534d2010-10-07 15:29:46 +1100554 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000555 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400556}
557
NeilBrowne33534d2010-10-07 15:29:46 +1100558static void setup_deferral(struct cache_deferred_req *dreq,
559 struct cache_head *item,
560 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 spin_lock(&cache_defer_lock);
566
J. Bruce Fields6610f722010-08-26 13:19:52 -0400567 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
NeilBrowne33534d2010-10-07 15:29:46 +1100569 if (count_me) {
570 cache_defer_cnt++;
571 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
NeilBrowne33534d2010-10-07 15:29:46 +1100573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 spin_unlock(&cache_defer_lock);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
J. Bruce Fields3211af12010-08-26 16:56:23 -0400578struct thread_deferred_req {
579 struct cache_deferred_req handle;
580 struct completion completion;
581};
582
583static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
584{
585 struct thread_deferred_req *dr =
586 container_of(dreq, struct thread_deferred_req, handle);
587 complete(&dr->completion);
588}
589
NeilBrownd29068c42010-10-07 15:29:46 +1100590static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400591{
592 struct thread_deferred_req sleeper;
593 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400594
595 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
596 dreq->revisit = cache_restart_thread;
597
NeilBrowne33534d2010-10-07 15:29:46 +1100598 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400599
NeilBrownd29068c42010-10-07 15:29:46 +1100600 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000601 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400602 &sleeper.completion, req->thread_wait) <= 0) {
603 /* The completion wasn't completed, so we need
604 * to clean up
605 */
606 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000607 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400608 __unhash_deferred_req(&sleeper.handle);
609 spin_unlock(&cache_defer_lock);
610 } else {
611 /* cache_revisit_request already removed
612 * this from the hash table, but hasn't
613 * called ->revisit yet. It will very soon
614 * and we need to wait for it.
615 */
616 spin_unlock(&cache_defer_lock);
617 wait_for_completion(&sleeper.completion);
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400620}
621
NeilBrowne33534d2010-10-07 15:29:46 +1100622static void cache_limit_defers(void)
623{
624 /* Make sure we haven't exceed the limit of allowed deferred
625 * requests.
626 */
627 struct cache_deferred_req *discard = NULL;
628
629 if (cache_defer_cnt <= DFR_MAX)
630 return;
631
632 spin_lock(&cache_defer_lock);
633
634 /* Consider removing either the first or the last */
635 if (cache_defer_cnt > DFR_MAX) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500636 if (prandom_u32() & 1)
NeilBrowne33534d2010-10-07 15:29:46 +1100637 discard = list_entry(cache_defer_list.next,
638 struct cache_deferred_req, recent);
639 else
640 discard = list_entry(cache_defer_list.prev,
641 struct cache_deferred_req, recent);
642 __unhash_deferred_req(discard);
643 }
644 spin_unlock(&cache_defer_lock);
645 if (discard)
646 discard->revisit(discard, 1);
647}
648
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500649/* Return true if and only if a deferred request is queued. */
650static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400651{
652 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400653
J. Bruce Fields3211af12010-08-26 16:56:23 -0400654 if (req->thread_wait) {
NeilBrownd29068c42010-10-07 15:29:46 +1100655 cache_wait_req(req, item);
656 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500657 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400658 }
659 dreq = req->defer(req);
660 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500661 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100662 setup_deferral(dreq, item, 1);
NeilBrownd29068c42010-10-07 15:29:46 +1100663 if (!test_bit(CACHE_PENDING, &item->flags))
664 /* Bit could have been cleared before we managed to
665 * set up the deferral, so need to revisit just in case
666 */
667 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100668
669 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500670 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673static void cache_revisit_request(struct cache_head *item)
674{
675 struct cache_deferred_req *dreq;
676 struct list_head pending;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800677 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int hash = DFR_HASH(item);
679
680 INIT_LIST_HEAD(&pending);
681 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800682
Sasha Levinb67bfe02013-02-27 17:06:00 -0800683 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000684 if (dreq->item == item) {
685 __unhash_deferred_req(dreq);
686 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
NeilBrown11174492010-08-12 17:04:08 +1000688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 spin_unlock(&cache_defer_lock);
690
691 while (!list_empty(&pending)) {
692 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
693 list_del_init(&dreq->recent);
694 dreq->revisit(dreq, 0);
695 }
696}
697
698void cache_clean_deferred(void *owner)
699{
700 struct cache_deferred_req *dreq, *tmp;
701 struct list_head pending;
702
703
704 INIT_LIST_HEAD(&pending);
705 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
708 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400709 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000710 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 }
713 spin_unlock(&cache_defer_lock);
714
715 while (!list_empty(&pending)) {
716 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
717 list_del_init(&dreq->recent);
718 dreq->revisit(dreq, 1);
719 }
720}
721
722/*
723 * communicate with user-space
724 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500725 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
726 * On read, you get a full request, or block.
727 * On write, an update request is processed.
728 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800730 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500731 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * to the end and may wakeup and preceding readers.
733 * New readers are added to the head. If, on read, an item is found with
734 * CACHE_UPCALLING clear, we free it from the list.
735 *
736 */
737
738static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800739static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741struct cache_queue {
742 struct list_head list;
743 int reader; /* if 0, then request */
744};
745struct cache_request {
746 struct cache_queue q;
747 struct cache_head *item;
748 char * buf;
749 int len;
750 int readers;
751};
752struct cache_reader {
753 struct cache_queue q;
754 int offset; /* if non-0, we have a refcnt on next request */
755};
756
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300757static int cache_request(struct cache_detail *detail,
758 struct cache_request *crq)
759{
760 char *bp = crq->buf;
761 int len = PAGE_SIZE;
762
763 detail->cache_request(detail, crq->item, &bp, &len);
764 if (len < 0)
765 return -EAGAIN;
766 return PAGE_SIZE - len;
767}
768
Trond Myklebust173912a2009-08-09 15:14:29 -0400769static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
770 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct cache_reader *rp = filp->private_data;
773 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500774 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int err;
776
777 if (count == 0)
778 return 0;
779
Al Viro59551022016-01-22 15:40:57 -0500780 inode_lock(inode); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * readers on this file */
782 again:
783 spin_lock(&queue_lock);
784 /* need to find next request */
785 while (rp->q.list.next != &cd->queue &&
786 list_entry(rp->q.list.next, struct cache_queue, list)
787 ->reader) {
788 struct list_head *next = rp->q.list.next;
789 list_move(&rp->q.list, next);
790 }
791 if (rp->q.list.next == &cd->queue) {
792 spin_unlock(&queue_lock);
Al Viro59551022016-01-22 15:40:57 -0500793 inode_unlock(inode);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400794 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return 0;
796 }
797 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400798 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (rp->offset == 0)
800 rq->readers++;
801 spin_unlock(&queue_lock);
802
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300803 if (rq->len == 0) {
804 err = cache_request(cd, rq);
805 if (err < 0)
806 goto out;
807 rq->len = err;
808 }
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
811 err = -EAGAIN;
812 spin_lock(&queue_lock);
813 list_move(&rp->q.list, &rq->q.list);
814 spin_unlock(&queue_lock);
815 } else {
816 if (rp->offset + count > rq->len)
817 count = rq->len - rp->offset;
818 err = -EFAULT;
819 if (copy_to_user(buf, rq->buf + rp->offset, count))
820 goto out;
821 rp->offset += count;
822 if (rp->offset >= rq->len) {
823 rp->offset = 0;
824 spin_lock(&queue_lock);
825 list_move(&rp->q.list, &rq->q.list);
826 spin_unlock(&queue_lock);
827 }
828 err = 0;
829 }
830 out:
831 if (rp->offset == 0) {
832 /* need to release rq */
833 spin_lock(&queue_lock);
834 rq->readers--;
835 if (rq->readers == 0 &&
836 !test_bit(CACHE_PENDING, &rq->item->flags)) {
837 list_del(&rq->q.list);
838 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800839 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 kfree(rq->buf);
841 kfree(rq);
842 } else
843 spin_unlock(&queue_lock);
844 }
845 if (err == -EAGAIN)
846 goto again;
Al Viro59551022016-01-22 15:40:57 -0500847 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return err ? err : count;
849}
850
Trond Myklebustda770052009-08-09 15:14:28 -0400851static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
852 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Trond Myklebustda770052009-08-09 15:14:28 -0400854 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300856 if (count == 0)
857 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400858 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400860 kaddr[count] = '\0';
861 ret = cd->cache_parse(cd, kaddr, count);
862 if (!ret)
863 ret = count;
864 return ret;
865}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Trond Myklebustda770052009-08-09 15:14:28 -0400867static ssize_t cache_slow_downcall(const char __user *buf,
868 size_t count, struct cache_detail *cd)
869{
870 static char write_buf[8192]; /* protected by queue_io_mutex */
871 ssize_t ret = -EINVAL;
872
873 if (count >= sizeof(write_buf))
874 goto out;
875 mutex_lock(&queue_io_mutex);
876 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800877 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400878out:
879 return ret;
880}
881
882static ssize_t cache_downcall(struct address_space *mapping,
883 const char __user *buf,
884 size_t count, struct cache_detail *cd)
885{
886 struct page *page;
887 char *kaddr;
888 ssize_t ret = -ENOMEM;
889
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300890 if (count >= PAGE_SIZE)
Trond Myklebustda770052009-08-09 15:14:28 -0400891 goto out_slow;
892
893 page = find_or_create_page(mapping, 0, GFP_KERNEL);
894 if (!page)
895 goto out_slow;
896
897 kaddr = kmap(page);
898 ret = cache_do_downcall(kaddr, buf, count, cd);
899 kunmap(page);
900 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300901 put_page(page);
Trond Myklebustda770052009-08-09 15:14:28 -0400902 return ret;
903out_slow:
904 return cache_slow_downcall(buf, count, cd);
905}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Trond Myklebust173912a2009-08-09 15:14:29 -0400907static ssize_t cache_write(struct file *filp, const char __user *buf,
908 size_t count, loff_t *ppos,
909 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Trond Myklebustda770052009-08-09 15:14:28 -0400911 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500912 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400913 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Trond Myklebustda770052009-08-09 15:14:28 -0400915 if (!cd->cache_parse)
916 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Al Viro59551022016-01-22 15:40:57 -0500918 inode_lock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400919 ret = cache_downcall(mapping, buf, count, cd);
Al Viro59551022016-01-22 15:40:57 -0500920 inode_unlock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400921out:
922 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
925static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
926
Trond Myklebust173912a2009-08-09 15:14:29 -0400927static unsigned int cache_poll(struct file *filp, poll_table *wait,
928 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 unsigned int mask;
931 struct cache_reader *rp = filp->private_data;
932 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 poll_wait(filp, &queue_wait, wait);
935
936 /* alway allow write */
Al Viro1711fd9a2015-03-07 21:08:46 +0000937 mask = POLLOUT | POLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (!rp)
940 return mask;
941
942 spin_lock(&queue_lock);
943
944 for (cq= &rp->q; &cq->list != &cd->queue;
945 cq = list_entry(cq->list.next, struct cache_queue, list))
946 if (!cq->reader) {
947 mask |= POLLIN | POLLRDNORM;
948 break;
949 }
950 spin_unlock(&queue_lock);
951 return mask;
952}
953
Trond Myklebust173912a2009-08-09 15:14:29 -0400954static int cache_ioctl(struct inode *ino, struct file *filp,
955 unsigned int cmd, unsigned long arg,
956 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 int len = 0;
959 struct cache_reader *rp = filp->private_data;
960 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 if (cmd != FIONREAD || !rp)
963 return -EINVAL;
964
965 spin_lock(&queue_lock);
966
967 /* only find the length remaining in current request,
968 * or the length of the next request
969 */
970 for (cq= &rp->q; &cq->list != &cd->queue;
971 cq = list_entry(cq->list.next, struct cache_queue, list))
972 if (!cq->reader) {
973 struct cache_request *cr =
974 container_of(cq, struct cache_request, q);
975 len = cr->len - rp->offset;
976 break;
977 }
978 spin_unlock(&queue_lock);
979
980 return put_user(len, (int __user *)arg);
981}
982
Trond Myklebust173912a2009-08-09 15:14:29 -0400983static int cache_open(struct inode *inode, struct file *filp,
984 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct cache_reader *rp = NULL;
987
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400988 if (!cd || !try_module_get(cd->owner))
989 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 nonseekable_open(inode, filp);
991 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400993 if (!rp) {
994 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 rp->offset = 0;
998 rp->q.reader = 1;
999 atomic_inc(&cd->readers);
1000 spin_lock(&queue_lock);
1001 list_add(&rp->q.list, &cd->queue);
1002 spin_unlock(&queue_lock);
1003 }
1004 filp->private_data = rp;
1005 return 0;
1006}
1007
Trond Myklebust173912a2009-08-09 15:14:29 -04001008static int cache_release(struct inode *inode, struct file *filp,
1009 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (rp) {
1014 spin_lock(&queue_lock);
1015 if (rp->offset) {
1016 struct cache_queue *cq;
1017 for (cq= &rp->q; &cq->list != &cd->queue;
1018 cq = list_entry(cq->list.next, struct cache_queue, list))
1019 if (!cq->reader) {
1020 container_of(cq, struct cache_request, q)
1021 ->readers--;
1022 break;
1023 }
1024 rp->offset = 0;
1025 }
1026 list_del(&rp->q.list);
1027 spin_unlock(&queue_lock);
1028
1029 filp->private_data = NULL;
1030 kfree(rp);
1031
NeilBrownc5b29f82010-08-12 16:55:22 +10001032 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 atomic_dec(&cd->readers);
1034 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001035 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return 0;
1037}
1038
1039
1040
NeilBrownf866a812009-08-04 15:22:38 +10001041static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001043 struct cache_queue *cq, *tmp;
1044 struct cache_request *cr;
1045 struct list_head dequeued;
1046
1047 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001049 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001051 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (cr->item != ch)
1053 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001054 if (test_bit(CACHE_PENDING, &ch->flags))
1055 /* Lost a race and it is pending again */
1056 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001058 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001059 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001062 while (!list_empty(&dequeued)) {
1063 cr = list_entry(dequeued.next, struct cache_request, q.list);
1064 list_del(&cr->q.list);
1065 cache_put(cr->item, detail);
1066 kfree(cr->buf);
1067 kfree(cr);
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071/*
1072 * Support routines for text-based upcalls.
1073 * Fields are separated by spaces.
1074 * Fields are either mangled to quote space tab newline slosh with slosh
1075 * or a hexified with a leading \x
1076 * Record is terminated with newline.
1077 *
1078 */
1079
1080void qword_add(char **bpp, int *lp, char *str)
1081{
1082 char *bp = *bpp;
1083 int len = *lp;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001084 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 if (len < 0) return;
1087
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001088 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1089 if (ret >= len) {
1090 bp += len;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001091 len = -1;
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001092 } else {
1093 bp += ret;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001094 len -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 *bp++ = ' ';
1096 len--;
1097 }
1098 *bpp = bp;
1099 *lp = len;
1100}
Trond Myklebust24c37672008-12-23 16:30:12 -05001101EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1104{
1105 char *bp = *bpp;
1106 int len = *lp;
1107
1108 if (len < 0) return;
1109
1110 if (len > 2) {
1111 *bp++ = '\\';
1112 *bp++ = 'x';
1113 len -= 2;
1114 while (blen && len >= 2) {
Andy Shevchenko056785e2013-12-12 15:49:21 +02001115 bp = hex_byte_pack(bp, *buf++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 len -= 2;
1117 blen--;
1118 }
1119 }
1120 if (blen || len<1) len = -1;
1121 else {
1122 *bp++ = ' ';
1123 len--;
1124 }
1125 *bpp = bp;
1126 *lp = len;
1127}
Trond Myklebust24c37672008-12-23 16:30:12 -05001128EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130static void warn_no_listener(struct cache_detail *detail)
1131{
1132 if (detail->last_warn != detail->last_close) {
1133 detail->last_warn = detail->last_close;
1134 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001135 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137}
1138
J. Bruce Fields06497522010-09-19 22:55:06 -04001139static bool cache_listeners_exist(struct cache_detail *detail)
1140{
1141 if (atomic_read(&detail->readers))
1142 return true;
1143 if (detail->last_close == 0)
1144 /* This cache was never opened */
1145 return false;
1146 if (detail->last_close < seconds_since_boot() - 30)
1147 /*
1148 * We allow for the possibility that someone might
1149 * restart a userspace daemon without restarting the
1150 * server; but after 30 seconds, we give up.
1151 */
1152 return false;
1153 return true;
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001157 * register an upcall request to user-space and queue it up for read() by the
1158 * upcall daemon.
1159 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * Each request is at most one page long.
1161 */
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +03001162int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164
1165 char *buf;
1166 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001167 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001169 if (!detail->cache_request)
1170 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
J. Bruce Fields06497522010-09-19 22:55:06 -04001172 if (!cache_listeners_exist(detail)) {
1173 warn_no_listener(detail);
1174 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
NeilBrown013920e2013-06-13 12:53:42 +10001176 if (test_bit(CACHE_CLEANED, &h->flags))
1177 /* Too late to make an upcall */
1178 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1181 if (!buf)
1182 return -EAGAIN;
1183
1184 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1185 if (!crq) {
1186 kfree(buf);
1187 return -EAGAIN;
1188 }
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 crq->q.reader = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001192 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 crq->readers = 0;
1194 spin_lock(&queue_lock);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001195 if (test_bit(CACHE_PENDING, &h->flags)) {
1196 crq->item = cache_get(h);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001197 list_add_tail(&crq->q.list, &detail->queue);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001198 } else
NeilBrownf9e1aed2013-06-13 12:53:42 +10001199 /* Lost a race, no longer PENDING, so don't enqueue */
1200 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 spin_unlock(&queue_lock);
1202 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001203 if (ret == -EAGAIN) {
1204 kfree(buf);
1205 kfree(crq);
1206 }
1207 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001209EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211/*
1212 * parse a message from user-space and pass it
1213 * to an appropriate cache
1214 * Messages are, like requests, separated into fields by
1215 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1216 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001217 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 * reply cachename expiry key ... content....
1219 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001220 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223int qword_get(char **bpp, char *dest, int bufsize)
1224{
1225 /* return bytes copied, or -1 on error */
1226 char *bp = *bpp;
1227 int len = 0;
1228
1229 while (*bp == ' ') bp++;
1230
1231 if (bp[0] == '\\' && bp[1] == 'x') {
1232 /* HEX STRING */
1233 bp += 2;
Stefan Hajnoczib7052cd2016-02-18 18:55:54 +00001234 while (len < bufsize - 1) {
Andy Shevchenkoe7f483e2010-09-21 09:40:25 +03001235 int h, l;
1236
1237 h = hex_to_bin(bp[0]);
1238 if (h < 0)
1239 break;
1240
1241 l = hex_to_bin(bp[1]);
1242 if (l < 0)
1243 break;
1244
1245 *dest++ = (h << 4) | l;
1246 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 len++;
1248 }
1249 } else {
1250 /* text with \nnn octal quoting */
1251 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1252 if (*bp == '\\' &&
1253 isodigit(bp[1]) && (bp[1] <= '3') &&
1254 isodigit(bp[2]) &&
1255 isodigit(bp[3])) {
1256 int byte = (*++bp -'0');
1257 bp++;
1258 byte = (byte << 3) | (*bp++ - '0');
1259 byte = (byte << 3) | (*bp++ - '0');
1260 *dest++ = byte;
1261 len++;
1262 } else {
1263 *dest++ = *bp++;
1264 len++;
1265 }
1266 }
1267 }
1268
1269 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1270 return -1;
1271 while (*bp == ' ') bp++;
1272 *bpp = bp;
1273 *dest = '\0';
1274 return len;
1275}
Trond Myklebust24c37672008-12-23 16:30:12 -05001276EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278
1279/*
1280 * support /proc/sunrpc/cache/$CACHENAME/content
1281 * as a seqfile.
1282 * We call ->cache_show passing NULL for the item to
1283 * get a header, then pass each real item in the cache
1284 */
1285
Kinglong Meec8c081b2015-07-27 11:09:42 +08001286void *cache_seq_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001287 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001290 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 struct cache_head *ch;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001292 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 read_lock(&cd->hash_lock);
1295 if (!n--)
1296 return SEQ_START_TOKEN;
1297 hash = n >> 32;
1298 entry = n & ((1LL<<32) - 1);
1299
Kinglong Mee129e5822015-07-27 11:10:15 +08001300 hlist_for_each_entry(ch, &cd->hash_table[hash], cache_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (!entry--)
1302 return ch;
1303 n &= ~((1LL<<32) - 1);
1304 do {
1305 hash++;
1306 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001307 } while(hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001308 hlist_empty(&cd->hash_table[hash]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (hash >= cd->hash_size)
1310 return NULL;
1311 *pos = n+1;
Kinglong Mee129e5822015-07-27 11:10:15 +08001312 return hlist_entry_safe(cd->hash_table[hash].first,
1313 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
Kinglong Meec8c081b2015-07-27 11:09:42 +08001315EXPORT_SYMBOL_GPL(cache_seq_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Kinglong Meec8c081b2015-07-27 11:09:42 +08001317void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 struct cache_head *ch = p;
1320 int hash = (*pos >> 32);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001321 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 if (p == SEQ_START_TOKEN)
1324 hash = 0;
Kinglong Mee129e5822015-07-27 11:10:15 +08001325 else if (ch->cache_list.next == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 hash++;
1327 *pos += 1LL<<32;
1328 } else {
1329 ++*pos;
Kinglong Mee129e5822015-07-27 11:10:15 +08001330 return hlist_entry_safe(ch->cache_list.next,
1331 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 *pos &= ~((1LL<<32) - 1);
1334 while (hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001335 hlist_empty(&cd->hash_table[hash])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 hash++;
1337 *pos += 1LL<<32;
1338 }
1339 if (hash >= cd->hash_size)
1340 return NULL;
1341 ++*pos;
Kinglong Mee129e5822015-07-27 11:10:15 +08001342 return hlist_entry_safe(cd->hash_table[hash].first,
1343 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
Kinglong Meec8c081b2015-07-27 11:09:42 +08001345EXPORT_SYMBOL_GPL(cache_seq_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Kinglong Meec8c081b2015-07-27 11:09:42 +08001347void cache_seq_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001348 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001350 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 read_unlock(&cd->hash_lock);
1352}
Kinglong Meec8c081b2015-07-27 11:09:42 +08001353EXPORT_SYMBOL_GPL(cache_seq_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355static int c_show(struct seq_file *m, void *p)
1356{
1357 struct cache_head *cp = p;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001358 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 if (p == SEQ_START_TOKEN)
1361 return cd->cache_show(m, cd, NULL);
1362
1363 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001364 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001365 convert_to_wallclock(cp->expiry_time),
1366 atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 cache_get(cp);
1368 if (cache_check(cd, cp, NULL))
1369 /* cache_check does a cache_put on failure */
1370 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001371 else {
1372 if (cache_is_expired(cd, cp))
1373 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 return cd->cache_show(m, cd, cp);
1378}
1379
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001380static const struct seq_operations cache_content_op = {
Kinglong Meec8c081b2015-07-27 11:09:42 +08001381 .start = cache_seq_start,
1382 .next = cache_seq_next,
1383 .stop = cache_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 .show = c_show,
1385};
1386
Trond Myklebust173912a2009-08-09 15:14:29 -04001387static int content_open(struct inode *inode, struct file *file,
1388 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001390 struct seq_file *seq;
1391 int err;
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;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001395
1396 err = seq_open(file, &cache_content_op);
1397 if (err) {
Li Zefana5990ea2010-03-11 14:08:10 -08001398 module_put(cd->owner);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001399 return err;
Li Zefana5990ea2010-03-11 14:08:10 -08001400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001402 seq = file->private_data;
1403 seq->private = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001407static int content_release(struct inode *inode, struct file *file,
1408 struct cache_detail *cd)
1409{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001410 int ret = seq_release(inode, file);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001411 module_put(cd->owner);
1412 return ret;
1413}
1414
1415static int open_flush(struct inode *inode, struct file *file,
1416 struct cache_detail *cd)
1417{
1418 if (!cd || !try_module_get(cd->owner))
1419 return -EACCES;
1420 return nonseekable_open(inode, file);
1421}
1422
1423static int release_flush(struct inode *inode, struct file *file,
1424 struct cache_detail *cd)
1425{
1426 module_put(cd->owner);
1427 return 0;
1428}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001431 size_t count, loff_t *ppos,
1432 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Sasha Levin212ba902012-07-17 00:01:26 +02001434 char tbuf[22];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001436 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Sasha Levin212ba902012-07-17 00:01:26 +02001438 snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 len = strlen(tbuf);
1440 if (p >= len)
1441 return 0;
1442 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001443 if (len > count)
1444 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001446 return -EFAULT;
1447 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return len;
1449}
1450
Trond Myklebust173912a2009-08-09 15:14:29 -04001451static ssize_t write_flush(struct file *file, const char __user *buf,
1452 size_t count, loff_t *ppos,
1453 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 char tbuf[20];
NeilBrownc5b29f82010-08-12 16:55:22 +10001456 char *bp, *ep;
Neil Brown77862032015-10-16 08:59:08 +11001457 time_t then, now;
NeilBrownc5b29f82010-08-12 16:55:22 +10001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (*ppos || count > sizeof(tbuf)-1)
1460 return -EINVAL;
1461 if (copy_from_user(tbuf, buf, count))
1462 return -EFAULT;
1463 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001464 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (*ep && *ep != '\n')
1466 return -EINVAL;
1467
NeilBrownc5b29f82010-08-12 16:55:22 +10001468 bp = tbuf;
Neil Brown77862032015-10-16 08:59:08 +11001469 then = get_expiry(&bp);
1470 now = seconds_since_boot();
1471 cd->nextcheck = now;
1472 /* Can only set flush_time to 1 second beyond "now", or
1473 * possibly 1 second beyond flushtime. This is because
1474 * flush_time never goes backwards so it mustn't get too far
1475 * ahead of time.
1476 */
1477 if (then >= now) {
1478 /* Want to flush everything, so behave like cache_purge() */
1479 if (cd->flush_time >= now)
1480 now = cd->flush_time + 1;
1481 then = now;
1482 }
1483
1484 cd->flush_time = then;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 cache_flush();
1486
1487 *ppos += count;
1488 return count;
1489}
1490
Trond Myklebust173912a2009-08-09 15:14:29 -04001491static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1492 size_t count, loff_t *ppos)
1493{
Al Virod9dda782013-03-31 18:16:14 -04001494 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001495
1496 return cache_read(filp, buf, count, ppos, cd);
1497}
1498
1499static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1500 size_t count, loff_t *ppos)
1501{
Al Virod9dda782013-03-31 18:16:14 -04001502 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001503
1504 return cache_write(filp, buf, count, ppos, cd);
1505}
1506
1507static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1508{
Al Virod9dda782013-03-31 18:16:14 -04001509 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001510
1511 return cache_poll(filp, wait, cd);
1512}
1513
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001514static long cache_ioctl_procfs(struct file *filp,
1515 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001516{
Al Viro496ad9a2013-01-23 17:07:38 -05001517 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001518 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001519
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001520 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001521}
1522
1523static int cache_open_procfs(struct inode *inode, struct file *filp)
1524{
Al Virod9dda782013-03-31 18:16:14 -04001525 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001526
1527 return cache_open(inode, filp, cd);
1528}
1529
1530static int cache_release_procfs(struct inode *inode, struct file *filp)
1531{
Al Virod9dda782013-03-31 18:16:14 -04001532 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001533
1534 return cache_release(inode, filp, cd);
1535}
1536
1537static const struct file_operations cache_file_operations_procfs = {
1538 .owner = THIS_MODULE,
1539 .llseek = no_llseek,
1540 .read = cache_read_procfs,
1541 .write = cache_write_procfs,
1542 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001543 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001544 .open = cache_open_procfs,
1545 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546};
Trond Myklebust173912a2009-08-09 15:14:29 -04001547
1548static int content_open_procfs(struct inode *inode, struct file *filp)
1549{
Al Virod9dda782013-03-31 18:16:14 -04001550 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001551
1552 return content_open(inode, filp, cd);
1553}
1554
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001555static int content_release_procfs(struct inode *inode, struct file *filp)
1556{
Al Virod9dda782013-03-31 18:16:14 -04001557 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001558
1559 return content_release(inode, filp, cd);
1560}
1561
Trond Myklebust173912a2009-08-09 15:14:29 -04001562static const struct file_operations content_file_operations_procfs = {
1563 .open = content_open_procfs,
1564 .read = seq_read,
1565 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001566 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001567};
1568
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001569static int open_flush_procfs(struct inode *inode, struct file *filp)
1570{
Al Virod9dda782013-03-31 18:16:14 -04001571 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001572
1573 return open_flush(inode, filp, cd);
1574}
1575
1576static int release_flush_procfs(struct inode *inode, struct file *filp)
1577{
Al Virod9dda782013-03-31 18:16:14 -04001578 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001579
1580 return release_flush(inode, filp, cd);
1581}
1582
Trond Myklebust173912a2009-08-09 15:14:29 -04001583static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1584 size_t count, loff_t *ppos)
1585{
Al Virod9dda782013-03-31 18:16:14 -04001586 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001587
1588 return read_flush(filp, buf, count, ppos, cd);
1589}
1590
1591static ssize_t write_flush_procfs(struct file *filp,
1592 const char __user *buf,
1593 size_t count, loff_t *ppos)
1594{
Al Virod9dda782013-03-31 18:16:14 -04001595 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001596
1597 return write_flush(filp, buf, count, ppos, cd);
1598}
1599
1600static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001601 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001602 .read = read_flush_procfs,
1603 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001604 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001605 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001606};
1607
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001608static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001609{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001610 struct sunrpc_net *sn;
1611
Trond Myklebust173912a2009-08-09 15:14:29 -04001612 if (cd->u.procfs.proc_ent == NULL)
1613 return;
1614 if (cd->u.procfs.flush_ent)
1615 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1616 if (cd->u.procfs.channel_ent)
1617 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1618 if (cd->u.procfs.content_ent)
1619 remove_proc_entry("content", cd->u.procfs.proc_ent);
1620 cd->u.procfs.proc_ent = NULL;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001621 sn = net_generic(net, sunrpc_net_id);
1622 remove_proc_entry(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001623}
1624
1625#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001626static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001627{
1628 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001629 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001630
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001631 sn = net_generic(net, sunrpc_net_id);
1632 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001633 if (cd->u.procfs.proc_ent == NULL)
1634 goto out_nomem;
1635 cd->u.procfs.channel_ent = NULL;
1636 cd->u.procfs.content_ent = NULL;
1637
1638 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1639 cd->u.procfs.proc_ent,
1640 &cache_flush_operations_procfs, cd);
1641 cd->u.procfs.flush_ent = p;
1642 if (p == NULL)
1643 goto out_nomem;
1644
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001645 if (cd->cache_request || cd->cache_parse) {
Trond Myklebust173912a2009-08-09 15:14:29 -04001646 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1647 cd->u.procfs.proc_ent,
1648 &cache_file_operations_procfs, cd);
1649 cd->u.procfs.channel_ent = p;
1650 if (p == NULL)
1651 goto out_nomem;
1652 }
1653 if (cd->cache_show) {
Yanchuan Nianec168672013-01-04 19:45:35 +08001654 p = proc_create_data("content", S_IFREG|S_IRUSR,
Trond Myklebust173912a2009-08-09 15:14:29 -04001655 cd->u.procfs.proc_ent,
1656 &content_file_operations_procfs, cd);
1657 cd->u.procfs.content_ent = p;
1658 if (p == NULL)
1659 goto out_nomem;
1660 }
1661 return 0;
1662out_nomem:
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001663 remove_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001664 return -ENOMEM;
1665}
1666#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001667static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001668{
1669 return 0;
1670}
1671#endif
1672
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001673void __init cache_initialize(void)
1674{
Tejun Heo203b42f2012-08-21 13:18:23 -07001675 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001676}
1677
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001678int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001679{
1680 int ret;
1681
1682 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001683 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001684 if (ret)
1685 sunrpc_destroy_cache_detail(cd);
1686 return ret;
1687}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001688EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001689
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001690void cache_unregister_net(struct cache_detail *cd, struct net *net)
1691{
1692 remove_cache_proc_entries(cd, net);
1693 sunrpc_destroy_cache_detail(cd);
1694}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001695EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001696
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001697struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001698{
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001699 struct cache_detail *cd;
Kinglong Mee129e5822015-07-27 11:10:15 +08001700 int i;
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001701
1702 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1703 if (cd == NULL)
1704 return ERR_PTR(-ENOMEM);
1705
Kinglong Mee129e5822015-07-27 11:10:15 +08001706 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct hlist_head),
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001707 GFP_KERNEL);
1708 if (cd->hash_table == NULL) {
1709 kfree(cd);
1710 return ERR_PTR(-ENOMEM);
1711 }
Kinglong Mee129e5822015-07-27 11:10:15 +08001712
1713 for (i = 0; i < cd->hash_size; i++)
1714 INIT_HLIST_HEAD(&cd->hash_table[i]);
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001715 cd->net = net;
1716 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001717}
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001718EXPORT_SYMBOL_GPL(cache_create_net);
1719
1720void cache_destroy_net(struct cache_detail *cd, struct net *net)
1721{
1722 kfree(cd->hash_table);
1723 kfree(cd);
1724}
1725EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001726
1727static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1728 size_t count, loff_t *ppos)
1729{
Al Viro496ad9a2013-01-23 17:07:38 -05001730 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001731
1732 return cache_read(filp, buf, count, ppos, cd);
1733}
1734
1735static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1736 size_t count, loff_t *ppos)
1737{
Al Viro496ad9a2013-01-23 17:07:38 -05001738 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001739
1740 return cache_write(filp, buf, count, ppos, cd);
1741}
1742
1743static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1744{
Al Viro496ad9a2013-01-23 17:07:38 -05001745 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001746
1747 return cache_poll(filp, wait, cd);
1748}
1749
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001750static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001751 unsigned int cmd, unsigned long arg)
1752{
Al Viro496ad9a2013-01-23 17:07:38 -05001753 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001754 struct cache_detail *cd = RPC_I(inode)->private;
1755
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001756 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001757}
1758
1759static int cache_open_pipefs(struct inode *inode, struct file *filp)
1760{
1761 struct cache_detail *cd = RPC_I(inode)->private;
1762
1763 return cache_open(inode, filp, cd);
1764}
1765
1766static int cache_release_pipefs(struct inode *inode, struct file *filp)
1767{
1768 struct cache_detail *cd = RPC_I(inode)->private;
1769
1770 return cache_release(inode, filp, cd);
1771}
1772
1773const struct file_operations cache_file_operations_pipefs = {
1774 .owner = THIS_MODULE,
1775 .llseek = no_llseek,
1776 .read = cache_read_pipefs,
1777 .write = cache_write_pipefs,
1778 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001779 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001780 .open = cache_open_pipefs,
1781 .release = cache_release_pipefs,
1782};
1783
1784static int content_open_pipefs(struct inode *inode, struct file *filp)
1785{
1786 struct cache_detail *cd = RPC_I(inode)->private;
1787
1788 return content_open(inode, filp, cd);
1789}
1790
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001791static int content_release_pipefs(struct inode *inode, struct file *filp)
1792{
1793 struct cache_detail *cd = RPC_I(inode)->private;
1794
1795 return content_release(inode, filp, cd);
1796}
1797
Trond Myklebust8854e822009-08-09 15:14:30 -04001798const struct file_operations content_file_operations_pipefs = {
1799 .open = content_open_pipefs,
1800 .read = seq_read,
1801 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001802 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001803};
1804
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001805static int open_flush_pipefs(struct inode *inode, struct file *filp)
1806{
1807 struct cache_detail *cd = RPC_I(inode)->private;
1808
1809 return open_flush(inode, filp, cd);
1810}
1811
1812static int release_flush_pipefs(struct inode *inode, struct file *filp)
1813{
1814 struct cache_detail *cd = RPC_I(inode)->private;
1815
1816 return release_flush(inode, filp, cd);
1817}
1818
Trond Myklebust8854e822009-08-09 15:14:30 -04001819static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1820 size_t count, loff_t *ppos)
1821{
Al Viro496ad9a2013-01-23 17:07:38 -05001822 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001823
1824 return read_flush(filp, buf, count, ppos, cd);
1825}
1826
1827static ssize_t write_flush_pipefs(struct file *filp,
1828 const char __user *buf,
1829 size_t count, loff_t *ppos)
1830{
Al Viro496ad9a2013-01-23 17:07:38 -05001831 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001832
1833 return write_flush(filp, buf, count, ppos, cd);
1834}
1835
1836const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001837 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001838 .read = read_flush_pipefs,
1839 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001840 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001841 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001842};
1843
1844int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001845 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001846 struct cache_detail *cd)
1847{
Al Viroa95e6912013-07-14 16:43:54 +04001848 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1849 if (IS_ERR(dir))
1850 return PTR_ERR(dir);
1851 cd->u.pipefs.dir = dir;
1852 return 0;
Trond Myklebust8854e822009-08-09 15:14:30 -04001853}
1854EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1855
1856void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1857{
1858 rpc_remove_cache_dir(cd->u.pipefs.dir);
1859 cd->u.pipefs.dir = NULL;
Trond Myklebust8854e822009-08-09 15:14:30 -04001860}
1861EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1862