blob: f96345b1180ee9cf41013008ac2f052a29496818 [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 Torvalds7c0f6ba2016-12-24 11:46:01 -080024#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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
Trond Myklebustae741362018-10-03 12:01:22 -040057static struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail,
58 struct cache_head *key,
59 int hash)
60{
61 struct hlist_head *head = &detail->hash_table[hash];
62 struct cache_head *tmp;
63
64 rcu_read_lock();
65 hlist_for_each_entry_rcu(tmp, head, cache_list) {
66 if (detail->match(tmp, key)) {
67 if (cache_is_expired(detail, tmp))
68 continue;
69 tmp = cache_get_rcu(tmp);
70 rcu_read_unlock();
71 return tmp;
72 }
73 }
74 rcu_read_unlock();
75 return NULL;
76}
77
Trond Myklebustb92a8fa2018-10-01 10:41:45 -040078static struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail,
79 struct cache_head *key,
80 int hash)
81{
82 struct cache_head *new, *tmp, *freeme = NULL;
83 struct hlist_head *head = &detail->hash_table[hash];
NeilBrown15a5f6b2006-03-27 01:15:02 -080084
85 new = detail->alloc();
86 if (!new)
87 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070088 /* must fully initialise 'new', else
89 * we might get lose if we need to
90 * cache_put it soon.
91 */
Neil Brown77862032015-10-16 08:59:08 +110092 cache_init(new, detail);
Neil Brown2f349312006-08-05 12:14:29 -070093 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080094
Trond Myklebust1863d772018-10-01 10:41:52 -040095 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -080096
97 /* check if entry appeared while we slept */
Trond Myklebustae741362018-10-03 12:01:22 -040098 hlist_for_each_entry_rcu(tmp, head, cache_list) {
NeilBrown15a5f6b2006-03-27 01:15:02 -080099 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +1100100 if (cache_is_expired(detail, tmp)) {
Trond Myklebustae741362018-10-03 12:01:22 -0400101 hlist_del_init_rcu(&tmp->cache_list);
NeilBrownd202cce2010-02-03 17:31:31 +1100102 detail->entries --;
103 freeme = tmp;
104 break;
105 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800106 cache_get(tmp);
Trond Myklebust1863d772018-10-01 10:41:52 -0400107 spin_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 }
Kinglong Mee129e5822015-07-27 11:10:15 +0800112
Trond Myklebustae741362018-10-03 12:01:22 -0400113 hlist_add_head_rcu(&new->cache_list, head);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800114 detail->entries++;
115 cache_get(new);
Trond Myklebust1863d772018-10-01 10:41:52 -0400116 spin_unlock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800117
NeilBrownd202cce2010-02-03 17:31:31 +1100118 if (freeme)
119 cache_put(freeme, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800120 return new;
121}
NeilBrown15a5f6b2006-03-27 01:15:02 -0800122
Trond Myklebustae741362018-10-03 12:01:22 -0400123struct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail,
124 struct cache_head *key, int hash)
125{
126 struct cache_head *ret;
127
128 ret = sunrpc_cache_find_rcu(detail, key, hash);
129 if (ret)
130 return ret;
131 /* Didn't find anything, insert an empty entry */
132 return sunrpc_cache_add_entry(detail, key, hash);
133}
134EXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu);
135
NeilBrownf866a812009-08-04 15:22:38 +1000136static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800137
Neil Brown77862032015-10-16 08:59:08 +1100138static void cache_fresh_locked(struct cache_head *head, time_t expiry,
139 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800140{
Neil Brown77862032015-10-16 08:59:08 +1100141 time_t now = seconds_since_boot();
142 if (now <= detail->flush_time)
143 /* ensure it isn't immediately treated as expired */
144 now = detail->flush_time + 1;
NeilBrownebd0cb12006-03-27 01:15:08 -0800145 head->expiry_time = expiry;
Neil Brown77862032015-10-16 08:59:08 +1100146 head->last_refresh = now;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500147 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
NeilBrown908329f2009-09-09 16:32:54 +1000148 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800149}
150
151static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000152 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800153{
NeilBrownebd0cb12006-03-27 01:15:08 -0800154 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
155 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000156 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800157 }
158}
159
NeilBrown15a5f6b2006-03-27 01:15:02 -0800160struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
161 struct cache_head *new, struct cache_head *old, int hash)
162{
163 /* The 'old' entry is to be replaced by 'new'.
164 * If 'old' is not VALID, we update it directly,
165 * otherwise we need to replace it
166 */
NeilBrown15a5f6b2006-03-27 01:15:02 -0800167 struct cache_head *tmp;
168
169 if (!test_bit(CACHE_VALID, &old->flags)) {
Trond Myklebust1863d772018-10-01 10:41:52 -0400170 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800171 if (!test_bit(CACHE_VALID, &old->flags)) {
172 if (test_bit(CACHE_NEGATIVE, &new->flags))
173 set_bit(CACHE_NEGATIVE, &old->flags);
174 else
175 detail->update(old, new);
Neil Brown77862032015-10-16 08:59:08 +1100176 cache_fresh_locked(old, new->expiry_time, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400177 spin_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000178 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800179 return old;
180 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400181 spin_unlock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800182 }
183 /* We need to insert a new entry */
184 tmp = detail->alloc();
185 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800186 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800187 return NULL;
188 }
Neil Brown77862032015-10-16 08:59:08 +1100189 cache_init(tmp, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800190 detail->init(tmp, old);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800191
Trond Myklebust1863d772018-10-01 10:41:52 -0400192 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800193 if (test_bit(CACHE_NEGATIVE, &new->flags))
194 set_bit(CACHE_NEGATIVE, &tmp->flags);
195 else
196 detail->update(tmp, new);
Kinglong Mee129e5822015-07-27 11:10:15 +0800197 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
NeilBrownf2d39582006-05-22 22:35:25 -0700198 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800199 cache_get(tmp);
Neil Brown77862032015-10-16 08:59:08 +1100200 cache_fresh_locked(tmp, new->expiry_time, detail);
201 cache_fresh_locked(old, 0, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400202 spin_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000203 cache_fresh_unlocked(tmp, detail);
204 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800205 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800206 return tmp;
207}
Trond Myklebust24c37672008-12-23 16:30:12 -0500208EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400210static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
211{
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +0300212 if (cd->cache_upcall)
213 return cd->cache_upcall(cd, h);
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +0300214 return sunrpc_cache_pipe_upcall(cd, h);
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400215}
NeilBrown989a19b2009-08-04 15:22:38 +1000216
chaoting fanb6040f92013-03-28 22:19:45 +0800217static inline int cache_is_valid(struct cache_head *h)
NeilBrown989a19b2009-08-04 15:22:38 +1000218{
NeilBrownd202cce2010-02-03 17:31:31 +1100219 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000220 return -EAGAIN;
221 else {
222 /* entry is valid */
223 if (test_bit(CACHE_NEGATIVE, &h->flags))
224 return -ENOENT;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500225 else {
226 /*
227 * In combination with write barrier in
228 * sunrpc_cache_update, ensures that anyone
229 * using the cache entry after this sees the
230 * updated contents:
231 */
232 smp_rmb();
NeilBrown989a19b2009-08-04 15:22:38 +1000233 return 0;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500234 }
NeilBrown989a19b2009-08-04 15:22:38 +1000235 }
236}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400237
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500238static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
239{
240 int rv;
241
Trond Myklebust1863d772018-10-01 10:41:52 -0400242 spin_lock(&detail->hash_lock);
chaoting fanb6040f92013-03-28 22:19:45 +0800243 rv = cache_is_valid(h);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000244 if (rv == -EAGAIN) {
245 set_bit(CACHE_NEGATIVE, &h->flags);
Neil Brown77862032015-10-16 08:59:08 +1100246 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
247 detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000248 rv = -ENOENT;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500249 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400250 spin_unlock(&detail->hash_lock);
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500251 cache_fresh_unlocked(h, detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000252 return rv;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
256 * This is the generic cache management routine for all
257 * the authentication caches.
258 * It checks the currency of a cache item and will (later)
259 * initiate an upcall to fill it if needed.
260 *
261 *
262 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000263 * -EAGAIN if upcall is pending and request has been queued
264 * -ETIMEDOUT if upcall failed or request could not be queue or
265 * upcall completed but item is still invalid (implying that
266 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * -ENOENT if cache entry was negative
268 */
269int cache_check(struct cache_detail *detail,
270 struct cache_head *h, struct cache_req *rqstp)
271{
272 int rv;
273 long refresh_age, age;
274
275 /* First decide return status as best we can */
chaoting fanb6040f92013-03-28 22:19:45 +0800276 rv = cache_is_valid(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /* now see if we want to start an upcall */
279 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000280 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (rqstp == NULL) {
283 if (rv == -EAGAIN)
284 rv = -ENOENT;
NeilBrown0bebc632013-06-13 12:53:42 +1000285 } else if (rv == -EAGAIN ||
286 (h->expiry_time != 0 && age > refresh_age/2)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500287 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
288 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
290 switch (cache_make_upcall(detail, h)) {
291 case -EINVAL:
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500292 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 case -EAGAIN:
NeilBrown2a1c7f52013-06-13 12:53:42 +1000295 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 }
298 }
299 }
300
NeilBrown989a19b2009-08-04 15:22:38 +1000301 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500302 if (!cache_defer_req(rqstp, h)) {
303 /*
304 * Request was not deferred; handle it as best
305 * we can ourselves:
306 */
chaoting fanb6040f92013-03-28 22:19:45 +0800307 rv = cache_is_valid(h);
NeilBrown989a19b2009-08-04 15:22:38 +1000308 if (rv == -EAGAIN)
309 rv = -ETIMEDOUT;
310 }
311 }
NeilBrown4013ede2006-03-27 01:15:07 -0800312 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800313 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return rv;
315}
Trond Myklebust24c37672008-12-23 16:30:12 -0500316EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
319 * caches need to be periodically cleaned.
320 * For this we maintain a list of cache_detail and
321 * a current pointer into that list and into the table
322 * for that entry.
323 *
NeilBrown013920e2013-06-13 12:53:42 +1000324 * Each time cache_clean is called it finds the next non-empty entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * in the current table and walks the list in that entry
326 * looking for entries that can be removed.
327 *
328 * An entry gets removed if:
329 * - The expiry is before current time
330 * - The last_refresh time is before the flush_time for that cache
331 *
332 * later we might drop old entries with non-NEVER expiry if that table
333 * is getting 'full' for some definition of 'full'
334 *
335 * The question of "how often to scan a table" is an interesting one
336 * and is answered in part by the use of the "nextcheck" field in the
337 * cache_detail.
338 * When a scan of a table begins, the nextcheck field is set to a time
339 * that is well into the future.
340 * While scanning, if an expiry time is found that is earlier than the
341 * current nextcheck time, nextcheck is set to that expiry time.
342 * If the flush_time is ever set to a time earlier than the nextcheck
343 * time, the nextcheck time is then set to that flush_time.
344 *
345 * A table is then only scanned if the current time is at least
346 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800347 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
349
350static LIST_HEAD(cache_list);
351static DEFINE_SPINLOCK(cache_list_lock);
352static struct cache_detail *current_detail;
353static int current_index;
354
David Howells65f27f32006-11-22 14:55:48 +0000355static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300356static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300358void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500359{
Trond Myklebust1863d772018-10-01 10:41:52 -0400360 spin_lock_init(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 INIT_LIST_HEAD(&cd->queue);
362 spin_lock(&cache_list_lock);
363 cd->nextcheck = 0;
364 cd->entries = 0;
365 atomic_set(&cd->readers, 0);
366 cd->last_close = 0;
367 cd->last_warn = -1;
368 list_add(&cd->others, &cache_list);
369 spin_unlock(&cache_list_lock);
370
371 /* start the cleaning process */
Ke Wang77b00bc2016-09-01 15:30:26 +0800372 queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300374EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300376void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 cache_purge(cd);
379 spin_lock(&cache_list_lock);
Trond Myklebust1863d772018-10-01 10:41:52 -0400380 spin_lock(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (current_detail == cd)
382 current_detail = NULL;
383 list_del_init(&cd->others);
Trond Myklebust1863d772018-10-01 10:41:52 -0400384 spin_unlock(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (list_empty(&cache_list)) {
387 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400388 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300391EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393/* clean cache tries to find something to clean
394 * and cleans it.
395 * It returns 1 if it cleaned something,
396 * 0 if it didn't find anything this time
397 * -1 if it fell off the end of the list.
398 */
399static int cache_clean(void)
400{
401 int rv = 0;
402 struct list_head *next;
403
404 spin_lock(&cache_list_lock);
405
406 /* find a suitable table if we don't already have one */
407 while (current_detail == NULL ||
408 current_index >= current_detail->hash_size) {
409 if (current_detail)
410 next = current_detail->others.next;
411 else
412 next = cache_list.next;
413 if (next == &cache_list) {
414 current_detail = NULL;
415 spin_unlock(&cache_list_lock);
416 return -1;
417 }
418 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000419 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 current_index = current_detail->hash_size;
421 else {
422 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000423 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 }
426
427 /* find a non-empty bucket in the table */
428 while (current_detail &&
429 current_index < current_detail->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +0800430 hlist_empty(&current_detail->hash_table[current_index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 current_index++;
432
433 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (current_detail && current_index < current_detail->hash_size) {
Kinglong Mee129e5822015-07-27 11:10:15 +0800436 struct cache_head *ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct cache_detail *d;
Kinglong Mee129e5822015-07-27 11:10:15 +0800438 struct hlist_head *head;
439 struct hlist_node *tmp;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800440
Trond Myklebust1863d772018-10-01 10:41:52 -0400441 spin_lock(&current_detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800444
Kinglong Mee129e5822015-07-27 11:10:15 +0800445 head = &current_detail->hash_table[current_index];
446 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (current_detail->nextcheck > ch->expiry_time)
448 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100449 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Trond Myklebustae741362018-10-03 12:01:22 -0400452 hlist_del_init_rcu(&ch->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 current_detail->entries--;
454 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
NeilBrown3af49742010-02-03 17:31:31 +1100457
Trond Myklebust1863d772018-10-01 10:41:52 -0400458 spin_unlock(&current_detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 d = current_detail;
460 if (!ch)
461 current_index ++;
462 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000463 if (ch) {
NeilBrown013920e2013-06-13 12:53:42 +1000464 set_bit(CACHE_CLEANED, &ch->flags);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000465 cache_fresh_unlocked(ch, d);
NeilBrownbaab9352006-03-27 01:15:09 -0800466 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 } else
469 spin_unlock(&cache_list_lock);
470
471 return rv;
472}
473
474/*
475 * We want to regularly clean the cache, so we need to schedule some work ...
476 */
David Howells65f27f32006-11-22 14:55:48 +0000477static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 int delay = 5;
480 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700481 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (list_empty(&cache_list))
484 delay = 0;
485
486 if (delay)
Ke Wang77b00bc2016-09-01 15:30:26 +0800487 queue_delayed_work(system_power_efficient_wq,
488 &cache_cleaner, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800492/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800494 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * be fully cleaned
496 */
497void cache_flush(void)
498{
499 while (cache_clean() != -1)
500 cond_resched();
501 while (cache_clean() != -1)
502 cond_resched();
503}
Trond Myklebust24c37672008-12-23 16:30:12 -0500504EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506void cache_purge(struct cache_detail *detail)
507{
Kinglong Mee471a9302017-02-08 09:54:33 +0800508 struct cache_head *ch = NULL;
509 struct hlist_head *head = NULL;
510 struct hlist_node *tmp = NULL;
511 int i = 0;
512
Trond Myklebust1863d772018-10-01 10:41:52 -0400513 spin_lock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800514 if (!detail->entries) {
Trond Myklebust1863d772018-10-01 10:41:52 -0400515 spin_unlock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800516 return;
517 }
518
519 dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name);
520 for (i = 0; i < detail->hash_size; i++) {
521 head = &detail->hash_table[i];
522 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Trond Myklebustae741362018-10-03 12:01:22 -0400523 hlist_del_init_rcu(&ch->cache_list);
Kinglong Mee471a9302017-02-08 09:54:33 +0800524 detail->entries--;
525
526 set_bit(CACHE_CLEANED, &ch->flags);
Trond Myklebust1863d772018-10-01 10:41:52 -0400527 spin_unlock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800528 cache_fresh_unlocked(ch, detail);
529 cache_put(ch, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400530 spin_lock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800531 }
532 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400533 spin_unlock(&detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
Trond Myklebust24c37672008-12-23 16:30:12 -0500535EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537
538/*
539 * Deferral and Revisiting of Requests.
540 *
541 * If a cache lookup finds a pending entry, we
542 * need to defer the request and revisit it later.
543 * All deferred requests are stored in a hash table,
544 * indexed by "struct cache_head *".
545 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800546 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * deferred form, which must contain a
548 * 'struct cache_deferred_req'
549 * This cache_deferred_req contains a method to allow
550 * it to be revisited when cache info is available
551 */
552
553#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
554#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
555
556#define DFR_MAX 300 /* ??? */
557
558static DEFINE_SPINLOCK(cache_defer_lock);
559static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000560static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static int cache_defer_cnt;
562
J. Bruce Fields6610f722010-08-26 13:19:52 -0400563static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
NeilBrown11174492010-08-12 17:04:08 +1000565 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100566 if (!list_empty(&dreq->recent)) {
567 list_del_init(&dreq->recent);
568 cache_defer_cnt--;
569 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400570}
571
572static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
573{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 int hash = DFR_HASH(item);
575
NeilBrowne33534d2010-10-07 15:29:46 +1100576 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000577 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400578}
579
NeilBrowne33534d2010-10-07 15:29:46 +1100580static void setup_deferral(struct cache_deferred_req *dreq,
581 struct cache_head *item,
582 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 spin_lock(&cache_defer_lock);
588
J. Bruce Fields6610f722010-08-26 13:19:52 -0400589 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
NeilBrowne33534d2010-10-07 15:29:46 +1100591 if (count_me) {
592 cache_defer_cnt++;
593 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
NeilBrowne33534d2010-10-07 15:29:46 +1100595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 spin_unlock(&cache_defer_lock);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
J. Bruce Fields3211af12010-08-26 16:56:23 -0400600struct thread_deferred_req {
601 struct cache_deferred_req handle;
602 struct completion completion;
603};
604
605static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
606{
607 struct thread_deferred_req *dr =
608 container_of(dreq, struct thread_deferred_req, handle);
609 complete(&dr->completion);
610}
611
NeilBrownd29068c2010-10-07 15:29:46 +1100612static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400613{
614 struct thread_deferred_req sleeper;
615 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400616
617 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
618 dreq->revisit = cache_restart_thread;
619
NeilBrowne33534d2010-10-07 15:29:46 +1100620 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400621
NeilBrownd29068c2010-10-07 15:29:46 +1100622 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000623 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400624 &sleeper.completion, req->thread_wait) <= 0) {
625 /* The completion wasn't completed, so we need
626 * to clean up
627 */
628 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000629 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400630 __unhash_deferred_req(&sleeper.handle);
631 spin_unlock(&cache_defer_lock);
632 } else {
633 /* cache_revisit_request already removed
634 * this from the hash table, but hasn't
635 * called ->revisit yet. It will very soon
636 * and we need to wait for it.
637 */
638 spin_unlock(&cache_defer_lock);
639 wait_for_completion(&sleeper.completion);
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400642}
643
NeilBrowne33534d2010-10-07 15:29:46 +1100644static void cache_limit_defers(void)
645{
646 /* Make sure we haven't exceed the limit of allowed deferred
647 * requests.
648 */
649 struct cache_deferred_req *discard = NULL;
650
651 if (cache_defer_cnt <= DFR_MAX)
652 return;
653
654 spin_lock(&cache_defer_lock);
655
656 /* Consider removing either the first or the last */
657 if (cache_defer_cnt > DFR_MAX) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500658 if (prandom_u32() & 1)
NeilBrowne33534d2010-10-07 15:29:46 +1100659 discard = list_entry(cache_defer_list.next,
660 struct cache_deferred_req, recent);
661 else
662 discard = list_entry(cache_defer_list.prev,
663 struct cache_deferred_req, recent);
664 __unhash_deferred_req(discard);
665 }
666 spin_unlock(&cache_defer_lock);
667 if (discard)
668 discard->revisit(discard, 1);
669}
670
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500671/* Return true if and only if a deferred request is queued. */
672static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400673{
674 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400675
J. Bruce Fields3211af12010-08-26 16:56:23 -0400676 if (req->thread_wait) {
NeilBrownd29068c2010-10-07 15:29:46 +1100677 cache_wait_req(req, item);
678 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500679 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400680 }
681 dreq = req->defer(req);
682 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500683 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100684 setup_deferral(dreq, item, 1);
NeilBrownd29068c2010-10-07 15:29:46 +1100685 if (!test_bit(CACHE_PENDING, &item->flags))
686 /* Bit could have been cleared before we managed to
687 * set up the deferral, so need to revisit just in case
688 */
689 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100690
691 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500692 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695static void cache_revisit_request(struct cache_head *item)
696{
697 struct cache_deferred_req *dreq;
698 struct list_head pending;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800699 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int hash = DFR_HASH(item);
701
702 INIT_LIST_HEAD(&pending);
703 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800704
Sasha Levinb67bfe02013-02-27 17:06:00 -0800705 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000706 if (dreq->item == item) {
707 __unhash_deferred_req(dreq);
708 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
NeilBrown11174492010-08-12 17:04:08 +1000710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 spin_unlock(&cache_defer_lock);
712
713 while (!list_empty(&pending)) {
714 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
715 list_del_init(&dreq->recent);
716 dreq->revisit(dreq, 0);
717 }
718}
719
720void cache_clean_deferred(void *owner)
721{
722 struct cache_deferred_req *dreq, *tmp;
723 struct list_head pending;
724
725
726 INIT_LIST_HEAD(&pending);
727 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
730 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400731 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000732 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 }
735 spin_unlock(&cache_defer_lock);
736
737 while (!list_empty(&pending)) {
738 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
739 list_del_init(&dreq->recent);
740 dreq->revisit(dreq, 1);
741 }
742}
743
744/*
745 * communicate with user-space
746 *
Kinglong Mee6489a8f2017-02-07 21:49:17 +0800747 * We have a magic /proc file - /proc/net/rpc/<cachename>/channel.
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500748 * On read, you get a full request, or block.
749 * On write, an update request is processed.
750 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800752 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500753 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * to the end and may wakeup and preceding readers.
755 * New readers are added to the head. If, on read, an item is found with
756 * CACHE_UPCALLING clear, we free it from the list.
757 *
758 */
759
760static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800761static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763struct cache_queue {
764 struct list_head list;
765 int reader; /* if 0, then request */
766};
767struct cache_request {
768 struct cache_queue q;
769 struct cache_head *item;
770 char * buf;
771 int len;
772 int readers;
773};
774struct cache_reader {
775 struct cache_queue q;
776 int offset; /* if non-0, we have a refcnt on next request */
777};
778
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300779static int cache_request(struct cache_detail *detail,
780 struct cache_request *crq)
781{
782 char *bp = crq->buf;
783 int len = PAGE_SIZE;
784
785 detail->cache_request(detail, crq->item, &bp, &len);
786 if (len < 0)
787 return -EAGAIN;
788 return PAGE_SIZE - len;
789}
790
Trond Myklebust173912a2009-08-09 15:14:29 -0400791static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
792 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 struct cache_reader *rp = filp->private_data;
795 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500796 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 int err;
798
799 if (count == 0)
800 return 0;
801
Al Viro59551022016-01-22 15:40:57 -0500802 inode_lock(inode); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * readers on this file */
804 again:
805 spin_lock(&queue_lock);
806 /* need to find next request */
807 while (rp->q.list.next != &cd->queue &&
808 list_entry(rp->q.list.next, struct cache_queue, list)
809 ->reader) {
810 struct list_head *next = rp->q.list.next;
811 list_move(&rp->q.list, next);
812 }
813 if (rp->q.list.next == &cd->queue) {
814 spin_unlock(&queue_lock);
Al Viro59551022016-01-22 15:40:57 -0500815 inode_unlock(inode);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400816 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return 0;
818 }
819 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400820 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (rp->offset == 0)
822 rq->readers++;
823 spin_unlock(&queue_lock);
824
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300825 if (rq->len == 0) {
826 err = cache_request(cd, rq);
827 if (err < 0)
828 goto out;
829 rq->len = err;
830 }
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
833 err = -EAGAIN;
834 spin_lock(&queue_lock);
835 list_move(&rp->q.list, &rq->q.list);
836 spin_unlock(&queue_lock);
837 } else {
838 if (rp->offset + count > rq->len)
839 count = rq->len - rp->offset;
840 err = -EFAULT;
841 if (copy_to_user(buf, rq->buf + rp->offset, count))
842 goto out;
843 rp->offset += count;
844 if (rp->offset >= rq->len) {
845 rp->offset = 0;
846 spin_lock(&queue_lock);
847 list_move(&rp->q.list, &rq->q.list);
848 spin_unlock(&queue_lock);
849 }
850 err = 0;
851 }
852 out:
853 if (rp->offset == 0) {
854 /* need to release rq */
855 spin_lock(&queue_lock);
856 rq->readers--;
857 if (rq->readers == 0 &&
858 !test_bit(CACHE_PENDING, &rq->item->flags)) {
859 list_del(&rq->q.list);
860 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800861 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 kfree(rq->buf);
863 kfree(rq);
864 } else
865 spin_unlock(&queue_lock);
866 }
867 if (err == -EAGAIN)
868 goto again;
Al Viro59551022016-01-22 15:40:57 -0500869 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return err ? err : count;
871}
872
Trond Myklebustda770052009-08-09 15:14:28 -0400873static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
874 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Trond Myklebustda770052009-08-09 15:14:28 -0400876 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300878 if (count == 0)
879 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400880 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400882 kaddr[count] = '\0';
883 ret = cd->cache_parse(cd, kaddr, count);
884 if (!ret)
885 ret = count;
886 return ret;
887}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Trond Myklebustda770052009-08-09 15:14:28 -0400889static ssize_t cache_slow_downcall(const char __user *buf,
890 size_t count, struct cache_detail *cd)
891{
892 static char write_buf[8192]; /* protected by queue_io_mutex */
893 ssize_t ret = -EINVAL;
894
895 if (count >= sizeof(write_buf))
896 goto out;
897 mutex_lock(&queue_io_mutex);
898 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800899 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400900out:
901 return ret;
902}
903
904static ssize_t cache_downcall(struct address_space *mapping,
905 const char __user *buf,
906 size_t count, struct cache_detail *cd)
907{
908 struct page *page;
909 char *kaddr;
910 ssize_t ret = -ENOMEM;
911
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300912 if (count >= PAGE_SIZE)
Trond Myklebustda770052009-08-09 15:14:28 -0400913 goto out_slow;
914
915 page = find_or_create_page(mapping, 0, GFP_KERNEL);
916 if (!page)
917 goto out_slow;
918
919 kaddr = kmap(page);
920 ret = cache_do_downcall(kaddr, buf, count, cd);
921 kunmap(page);
922 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300923 put_page(page);
Trond Myklebustda770052009-08-09 15:14:28 -0400924 return ret;
925out_slow:
926 return cache_slow_downcall(buf, count, cd);
927}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Trond Myklebust173912a2009-08-09 15:14:29 -0400929static ssize_t cache_write(struct file *filp, const char __user *buf,
930 size_t count, loff_t *ppos,
931 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Trond Myklebustda770052009-08-09 15:14:28 -0400933 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500934 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400935 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Trond Myklebustda770052009-08-09 15:14:28 -0400937 if (!cd->cache_parse)
938 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Al Viro59551022016-01-22 15:40:57 -0500940 inode_lock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400941 ret = cache_downcall(mapping, buf, count, cd);
Al Viro59551022016-01-22 15:40:57 -0500942 inode_unlock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400943out:
944 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
948
Al Viroade994f2017-07-03 00:01:49 -0400949static __poll_t cache_poll(struct file *filp, poll_table *wait,
Trond Myklebust173912a2009-08-09 15:14:29 -0400950 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Al Viroade994f2017-07-03 00:01:49 -0400952 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 struct cache_reader *rp = filp->private_data;
954 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 poll_wait(filp, &queue_wait, wait);
957
958 /* alway allow write */
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800959 mask = EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if (!rp)
962 return mask;
963
964 spin_lock(&queue_lock);
965
966 for (cq= &rp->q; &cq->list != &cd->queue;
967 cq = list_entry(cq->list.next, struct cache_queue, list))
968 if (!cq->reader) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800969 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 break;
971 }
972 spin_unlock(&queue_lock);
973 return mask;
974}
975
Trond Myklebust173912a2009-08-09 15:14:29 -0400976static int cache_ioctl(struct inode *ino, struct file *filp,
977 unsigned int cmd, unsigned long arg,
978 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 int len = 0;
981 struct cache_reader *rp = filp->private_data;
982 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 if (cmd != FIONREAD || !rp)
985 return -EINVAL;
986
987 spin_lock(&queue_lock);
988
989 /* only find the length remaining in current request,
990 * or the length of the next request
991 */
992 for (cq= &rp->q; &cq->list != &cd->queue;
993 cq = list_entry(cq->list.next, struct cache_queue, list))
994 if (!cq->reader) {
995 struct cache_request *cr =
996 container_of(cq, struct cache_request, q);
997 len = cr->len - rp->offset;
998 break;
999 }
1000 spin_unlock(&queue_lock);
1001
1002 return put_user(len, (int __user *)arg);
1003}
1004
Trond Myklebust173912a2009-08-09 15:14:29 -04001005static int cache_open(struct inode *inode, struct file *filp,
1006 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 struct cache_reader *rp = NULL;
1009
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001010 if (!cd || !try_module_get(cd->owner))
1011 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 nonseekable_open(inode, filp);
1013 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +04001015 if (!rp) {
1016 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +04001018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 rp->offset = 0;
1020 rp->q.reader = 1;
1021 atomic_inc(&cd->readers);
1022 spin_lock(&queue_lock);
1023 list_add(&rp->q.list, &cd->queue);
1024 spin_unlock(&queue_lock);
1025 }
1026 filp->private_data = rp;
1027 return 0;
1028}
1029
Trond Myklebust173912a2009-08-09 15:14:29 -04001030static int cache_release(struct inode *inode, struct file *filp,
1031 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (rp) {
1036 spin_lock(&queue_lock);
1037 if (rp->offset) {
1038 struct cache_queue *cq;
1039 for (cq= &rp->q; &cq->list != &cd->queue;
1040 cq = list_entry(cq->list.next, struct cache_queue, list))
1041 if (!cq->reader) {
1042 container_of(cq, struct cache_request, q)
1043 ->readers--;
1044 break;
1045 }
1046 rp->offset = 0;
1047 }
1048 list_del(&rp->q.list);
1049 spin_unlock(&queue_lock);
1050
1051 filp->private_data = NULL;
1052 kfree(rp);
1053
NeilBrownc5b29f82010-08-12 16:55:22 +10001054 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 atomic_dec(&cd->readers);
1056 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001057 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 0;
1059}
1060
1061
1062
NeilBrownf866a812009-08-04 15:22:38 +10001063static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001065 struct cache_queue *cq, *tmp;
1066 struct cache_request *cr;
1067 struct list_head dequeued;
1068
1069 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001071 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001073 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (cr->item != ch)
1075 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001076 if (test_bit(CACHE_PENDING, &ch->flags))
1077 /* Lost a race and it is pending again */
1078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001080 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001081 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001084 while (!list_empty(&dequeued)) {
1085 cr = list_entry(dequeued.next, struct cache_request, q.list);
1086 list_del(&cr->q.list);
1087 cache_put(cr->item, detail);
1088 kfree(cr->buf);
1089 kfree(cr);
1090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093/*
1094 * Support routines for text-based upcalls.
1095 * Fields are separated by spaces.
1096 * Fields are either mangled to quote space tab newline slosh with slosh
1097 * or a hexified with a leading \x
1098 * Record is terminated with newline.
1099 *
1100 */
1101
1102void qword_add(char **bpp, int *lp, char *str)
1103{
1104 char *bp = *bpp;
1105 int len = *lp;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001106 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 if (len < 0) return;
1109
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001110 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1111 if (ret >= len) {
1112 bp += len;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001113 len = -1;
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001114 } else {
1115 bp += ret;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001116 len -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 *bp++ = ' ';
1118 len--;
1119 }
1120 *bpp = bp;
1121 *lp = len;
1122}
Trond Myklebust24c37672008-12-23 16:30:12 -05001123EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1126{
1127 char *bp = *bpp;
1128 int len = *lp;
1129
1130 if (len < 0) return;
1131
1132 if (len > 2) {
1133 *bp++ = '\\';
1134 *bp++ = 'x';
1135 len -= 2;
1136 while (blen && len >= 2) {
Andy Shevchenko056785e2013-12-12 15:49:21 +02001137 bp = hex_byte_pack(bp, *buf++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 len -= 2;
1139 blen--;
1140 }
1141 }
1142 if (blen || len<1) len = -1;
1143 else {
1144 *bp++ = ' ';
1145 len--;
1146 }
1147 *bpp = bp;
1148 *lp = len;
1149}
Trond Myklebust24c37672008-12-23 16:30:12 -05001150EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152static void warn_no_listener(struct cache_detail *detail)
1153{
1154 if (detail->last_warn != detail->last_close) {
1155 detail->last_warn = detail->last_close;
1156 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001157 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159}
1160
J. Bruce Fields06497522010-09-19 22:55:06 -04001161static bool cache_listeners_exist(struct cache_detail *detail)
1162{
1163 if (atomic_read(&detail->readers))
1164 return true;
1165 if (detail->last_close == 0)
1166 /* This cache was never opened */
1167 return false;
1168 if (detail->last_close < seconds_since_boot() - 30)
1169 /*
1170 * We allow for the possibility that someone might
1171 * restart a userspace daemon without restarting the
1172 * server; but after 30 seconds, we give up.
1173 */
1174 return false;
1175 return true;
1176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001179 * register an upcall request to user-space and queue it up for read() by the
1180 * upcall daemon.
1181 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 * Each request is at most one page long.
1183 */
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +03001184int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186
1187 char *buf;
1188 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001189 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001191 if (!detail->cache_request)
1192 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
J. Bruce Fields06497522010-09-19 22:55:06 -04001194 if (!cache_listeners_exist(detail)) {
1195 warn_no_listener(detail);
1196 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
NeilBrown013920e2013-06-13 12:53:42 +10001198 if (test_bit(CACHE_CLEANED, &h->flags))
1199 /* Too late to make an upcall */
1200 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1203 if (!buf)
1204 return -EAGAIN;
1205
1206 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1207 if (!crq) {
1208 kfree(buf);
1209 return -EAGAIN;
1210 }
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 crq->q.reader = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001214 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 crq->readers = 0;
1216 spin_lock(&queue_lock);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001217 if (test_bit(CACHE_PENDING, &h->flags)) {
1218 crq->item = cache_get(h);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001219 list_add_tail(&crq->q.list, &detail->queue);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001220 } else
NeilBrownf9e1aed2013-06-13 12:53:42 +10001221 /* Lost a race, no longer PENDING, so don't enqueue */
1222 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 spin_unlock(&queue_lock);
1224 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001225 if (ret == -EAGAIN) {
1226 kfree(buf);
1227 kfree(crq);
1228 }
1229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001231EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233/*
1234 * parse a message from user-space and pass it
1235 * to an appropriate cache
1236 * Messages are, like requests, separated into fields by
1237 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1238 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001239 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 * reply cachename expiry key ... content....
1241 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001242 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 */
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245int qword_get(char **bpp, char *dest, int bufsize)
1246{
1247 /* return bytes copied, or -1 on error */
1248 char *bp = *bpp;
1249 int len = 0;
1250
1251 while (*bp == ' ') bp++;
1252
1253 if (bp[0] == '\\' && bp[1] == 'x') {
1254 /* HEX STRING */
1255 bp += 2;
Stefan Hajnoczib7052cd2016-02-18 18:55:54 +00001256 while (len < bufsize - 1) {
Andy Shevchenkoe7f483ea2010-09-21 09:40:25 +03001257 int h, l;
1258
1259 h = hex_to_bin(bp[0]);
1260 if (h < 0)
1261 break;
1262
1263 l = hex_to_bin(bp[1]);
1264 if (l < 0)
1265 break;
1266
1267 *dest++ = (h << 4) | l;
1268 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 len++;
1270 }
1271 } else {
1272 /* text with \nnn octal quoting */
1273 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1274 if (*bp == '\\' &&
1275 isodigit(bp[1]) && (bp[1] <= '3') &&
1276 isodigit(bp[2]) &&
1277 isodigit(bp[3])) {
1278 int byte = (*++bp -'0');
1279 bp++;
1280 byte = (byte << 3) | (*bp++ - '0');
1281 byte = (byte << 3) | (*bp++ - '0');
1282 *dest++ = byte;
1283 len++;
1284 } else {
1285 *dest++ = *bp++;
1286 len++;
1287 }
1288 }
1289 }
1290
1291 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1292 return -1;
1293 while (*bp == ' ') bp++;
1294 *bpp = bp;
1295 *dest = '\0';
1296 return len;
1297}
Trond Myklebust24c37672008-12-23 16:30:12 -05001298EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300
1301/*
Kinglong Mee6489a8f2017-02-07 21:49:17 +08001302 * support /proc/net/rpc/$CACHENAME/content
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 * as a seqfile.
1304 * We call ->cache_show passing NULL for the item to
1305 * get a header, then pass each real item in the cache
1306 */
1307
Trond Myklebustae741362018-10-03 12:01:22 -04001308static void *__cache_seq_start(struct seq_file *m, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001311 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 struct cache_head *ch;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001313 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (!n--)
1316 return SEQ_START_TOKEN;
1317 hash = n >> 32;
1318 entry = n & ((1LL<<32) - 1);
1319
Trond Myklebustae741362018-10-03 12:01:22 -04001320 hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (!entry--)
1322 return ch;
1323 n &= ~((1LL<<32) - 1);
1324 do {
1325 hash++;
1326 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001327 } while(hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001328 hlist_empty(&cd->hash_table[hash]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (hash >= cd->hash_size)
1330 return NULL;
1331 *pos = n+1;
Trond Myklebustae741362018-10-03 12:01:22 -04001332 return hlist_entry_safe(rcu_dereference_raw(
1333 hlist_first_rcu(&cd->hash_table[hash])),
Kinglong Mee129e5822015-07-27 11:10:15 +08001334 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
Trond Myklebustae741362018-10-03 12:01:22 -04001336
Trond Myklebustd48cf352018-10-01 10:41:51 -04001337static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct cache_head *ch = p;
1340 int hash = (*pos >> 32);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001341 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 if (p == SEQ_START_TOKEN)
1344 hash = 0;
Kinglong Mee129e5822015-07-27 11:10:15 +08001345 else if (ch->cache_list.next == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 hash++;
1347 *pos += 1LL<<32;
1348 } else {
1349 ++*pos;
Trond Myklebustae741362018-10-03 12:01:22 -04001350 return hlist_entry_safe(rcu_dereference_raw(
1351 hlist_next_rcu(&ch->cache_list)),
Kinglong Mee129e5822015-07-27 11:10:15 +08001352 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354 *pos &= ~((1LL<<32) - 1);
1355 while (hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001356 hlist_empty(&cd->hash_table[hash])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 hash++;
1358 *pos += 1LL<<32;
1359 }
1360 if (hash >= cd->hash_size)
1361 return NULL;
1362 ++*pos;
Trond Myklebustae741362018-10-03 12:01:22 -04001363 return hlist_entry_safe(rcu_dereference_raw(
1364 hlist_first_rcu(&cd->hash_table[hash])),
Kinglong Mee129e5822015-07-27 11:10:15 +08001365 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
Kinglong Meec8c081b2015-07-27 11:09:42 +08001367EXPORT_SYMBOL_GPL(cache_seq_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Trond Myklebustae741362018-10-03 12:01:22 -04001369void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos)
1370 __acquires(RCU)
1371{
1372 rcu_read_lock();
1373 return __cache_seq_start(m, pos);
1374}
1375EXPORT_SYMBOL_GPL(cache_seq_start_rcu);
1376
1377void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos)
1378{
1379 return cache_seq_next(file, p, pos);
1380}
1381EXPORT_SYMBOL_GPL(cache_seq_next_rcu);
1382
1383void cache_seq_stop_rcu(struct seq_file *m, void *p)
1384 __releases(RCU)
1385{
1386 rcu_read_unlock();
1387}
1388EXPORT_SYMBOL_GPL(cache_seq_stop_rcu);
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390static int c_show(struct seq_file *m, void *p)
1391{
1392 struct cache_head *cp = p;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001393 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 if (p == SEQ_START_TOKEN)
1396 return cd->cache_show(m, cd, NULL);
1397
1398 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001399 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001400 convert_to_wallclock(cp->expiry_time),
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001401 kref_read(&cp->ref), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 cache_get(cp);
1403 if (cache_check(cd, cp, NULL))
1404 /* cache_check does a cache_put on failure */
1405 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001406 else {
1407 if (cache_is_expired(cd, cp))
1408 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 return cd->cache_show(m, cd, cp);
1413}
1414
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001415static const struct seq_operations cache_content_op = {
Trond Myklebustd48cf352018-10-01 10:41:51 -04001416 .start = cache_seq_start_rcu,
1417 .next = cache_seq_next_rcu,
1418 .stop = cache_seq_stop_rcu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 .show = c_show,
1420};
1421
Trond Myklebust173912a2009-08-09 15:14:29 -04001422static int content_open(struct inode *inode, struct file *file,
1423 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001425 struct seq_file *seq;
1426 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001428 if (!cd || !try_module_get(cd->owner))
1429 return -EACCES;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001430
1431 err = seq_open(file, &cache_content_op);
1432 if (err) {
Li Zefana5990ea2010-03-11 14:08:10 -08001433 module_put(cd->owner);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001434 return err;
Li Zefana5990ea2010-03-11 14:08:10 -08001435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001437 seq = file->private_data;
1438 seq->private = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001442static int content_release(struct inode *inode, struct file *file,
1443 struct cache_detail *cd)
1444{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001445 int ret = seq_release(inode, file);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001446 module_put(cd->owner);
1447 return ret;
1448}
1449
1450static int open_flush(struct inode *inode, struct file *file,
1451 struct cache_detail *cd)
1452{
1453 if (!cd || !try_module_get(cd->owner))
1454 return -EACCES;
1455 return nonseekable_open(inode, file);
1456}
1457
1458static int release_flush(struct inode *inode, struct file *file,
1459 struct cache_detail *cd)
1460{
1461 module_put(cd->owner);
1462 return 0;
1463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001466 size_t count, loff_t *ppos,
1467 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Sasha Levin212ba902012-07-17 00:01:26 +02001469 char tbuf[22];
Chuck Lever01b29692007-10-26 13:31:20 -04001470 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Kinglong Mee8ccc8692017-02-07 21:50:32 +08001472 len = snprintf(tbuf, sizeof(tbuf), "%lu\n",
1473 convert_to_wallclock(cd->flush_time));
1474 return simple_read_from_buffer(buf, count, ppos, tbuf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Trond Myklebust173912a2009-08-09 15:14:29 -04001477static ssize_t write_flush(struct file *file, const char __user *buf,
1478 size_t count, loff_t *ppos,
1479 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 char tbuf[20];
NeilBrown3b68e6e2018-02-14 12:15:06 +11001482 char *ep;
1483 time_t now;
NeilBrownc5b29f82010-08-12 16:55:22 +10001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (*ppos || count > sizeof(tbuf)-1)
1486 return -EINVAL;
1487 if (copy_from_user(tbuf, buf, count))
1488 return -EFAULT;
1489 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001490 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (*ep && *ep != '\n')
1492 return -EINVAL;
NeilBrown3b68e6e2018-02-14 12:15:06 +11001493 /* Note that while we check that 'buf' holds a valid number,
1494 * we always ignore the value and just flush everything.
1495 * Making use of the number leads to races.
Neil Brown77862032015-10-16 08:59:08 +11001496 */
Neil Brown77862032015-10-16 08:59:08 +11001497
NeilBrown3b68e6e2018-02-14 12:15:06 +11001498 now = seconds_since_boot();
1499 /* Always flush everything, so behave like cache_purge()
1500 * Do this by advancing flush_time to the current time,
1501 * or by one second if it has already reached the current time.
1502 * Newly added cache entries will always have ->last_refresh greater
1503 * that ->flush_time, so they don't get flushed prematurely.
1504 */
1505
1506 if (cd->flush_time >= now)
1507 now = cd->flush_time + 1;
1508
1509 cd->flush_time = now;
1510 cd->nextcheck = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 cache_flush();
1512
1513 *ppos += count;
1514 return count;
1515}
1516
Trond Myklebust173912a2009-08-09 15:14:29 -04001517static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1518 size_t count, loff_t *ppos)
1519{
Al Virod9dda782013-03-31 18:16:14 -04001520 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001521
1522 return cache_read(filp, buf, count, ppos, cd);
1523}
1524
1525static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1526 size_t count, loff_t *ppos)
1527{
Al Virod9dda782013-03-31 18:16:14 -04001528 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001529
1530 return cache_write(filp, buf, count, ppos, cd);
1531}
1532
Al Viroade994f2017-07-03 00:01:49 -04001533static __poll_t cache_poll_procfs(struct file *filp, poll_table *wait)
Trond Myklebust173912a2009-08-09 15:14:29 -04001534{
Al Virod9dda782013-03-31 18:16:14 -04001535 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001536
1537 return cache_poll(filp, wait, cd);
1538}
1539
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001540static long cache_ioctl_procfs(struct file *filp,
1541 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001542{
Al Viro496ad9a2013-01-23 17:07:38 -05001543 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001544 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001545
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001546 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001547}
1548
1549static int cache_open_procfs(struct inode *inode, struct file *filp)
1550{
Al Virod9dda782013-03-31 18:16:14 -04001551 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001552
1553 return cache_open(inode, filp, cd);
1554}
1555
1556static int cache_release_procfs(struct inode *inode, struct file *filp)
1557{
Al Virod9dda782013-03-31 18:16:14 -04001558 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001559
1560 return cache_release(inode, filp, cd);
1561}
1562
1563static const struct file_operations cache_file_operations_procfs = {
1564 .owner = THIS_MODULE,
1565 .llseek = no_llseek,
1566 .read = cache_read_procfs,
1567 .write = cache_write_procfs,
1568 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001569 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001570 .open = cache_open_procfs,
1571 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572};
Trond Myklebust173912a2009-08-09 15:14:29 -04001573
1574static int content_open_procfs(struct inode *inode, struct file *filp)
1575{
Al Virod9dda782013-03-31 18:16:14 -04001576 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001577
1578 return content_open(inode, filp, cd);
1579}
1580
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001581static int content_release_procfs(struct inode *inode, struct file *filp)
1582{
Al Virod9dda782013-03-31 18:16:14 -04001583 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001584
1585 return content_release(inode, filp, cd);
1586}
1587
Trond Myklebust173912a2009-08-09 15:14:29 -04001588static const struct file_operations content_file_operations_procfs = {
1589 .open = content_open_procfs,
1590 .read = seq_read,
1591 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001592 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001593};
1594
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001595static int open_flush_procfs(struct inode *inode, struct file *filp)
1596{
Al Virod9dda782013-03-31 18:16:14 -04001597 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001598
1599 return open_flush(inode, filp, cd);
1600}
1601
1602static int release_flush_procfs(struct inode *inode, struct file *filp)
1603{
Al Virod9dda782013-03-31 18:16:14 -04001604 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001605
1606 return release_flush(inode, filp, cd);
1607}
1608
Trond Myklebust173912a2009-08-09 15:14:29 -04001609static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1610 size_t count, loff_t *ppos)
1611{
Al Virod9dda782013-03-31 18:16:14 -04001612 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001613
1614 return read_flush(filp, buf, count, ppos, cd);
1615}
1616
1617static ssize_t write_flush_procfs(struct file *filp,
1618 const char __user *buf,
1619 size_t count, loff_t *ppos)
1620{
Al Virod9dda782013-03-31 18:16:14 -04001621 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001622
1623 return write_flush(filp, buf, count, ppos, cd);
1624}
1625
1626static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001627 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001628 .read = read_flush_procfs,
1629 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001630 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001631 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001632};
1633
Kinglong Mee863d7d92017-02-07 21:47:16 +08001634static void remove_cache_proc_entries(struct cache_detail *cd)
Trond Myklebust173912a2009-08-09 15:14:29 -04001635{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001636 if (cd->procfs) {
1637 proc_remove(cd->procfs);
1638 cd->procfs = NULL;
1639 }
Trond Myklebust173912a2009-08-09 15:14:29 -04001640}
1641
1642#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001643static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001644{
1645 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001646 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001647
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001648 sn = net_generic(net, sunrpc_net_id);
Kinglong Mee863d7d92017-02-07 21:47:16 +08001649 cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc);
1650 if (cd->procfs == NULL)
Trond Myklebust173912a2009-08-09 15:14:29 -04001651 goto out_nomem;
Trond Myklebust173912a2009-08-09 15:14:29 -04001652
Joe Perchesd6444062018-03-23 15:54:38 -07001653 p = proc_create_data("flush", S_IFREG | 0600,
Kinglong Mee863d7d92017-02-07 21:47:16 +08001654 cd->procfs, &cache_flush_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001655 if (p == NULL)
1656 goto out_nomem;
1657
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001658 if (cd->cache_request || cd->cache_parse) {
Joe Perchesd6444062018-03-23 15:54:38 -07001659 p = proc_create_data("channel", S_IFREG | 0600, cd->procfs,
1660 &cache_file_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001661 if (p == NULL)
1662 goto out_nomem;
1663 }
1664 if (cd->cache_show) {
Joe Perchesd6444062018-03-23 15:54:38 -07001665 p = proc_create_data("content", S_IFREG | 0400, cd->procfs,
1666 &content_file_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001667 if (p == NULL)
1668 goto out_nomem;
1669 }
1670 return 0;
1671out_nomem:
Kinglong Mee863d7d92017-02-07 21:47:16 +08001672 remove_cache_proc_entries(cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001673 return -ENOMEM;
1674}
1675#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001676static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001677{
1678 return 0;
1679}
1680#endif
1681
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001682void __init cache_initialize(void)
1683{
Tejun Heo203b42f2012-08-21 13:18:23 -07001684 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001685}
1686
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001687int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001688{
1689 int ret;
1690
1691 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001692 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001693 if (ret)
1694 sunrpc_destroy_cache_detail(cd);
1695 return ret;
1696}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001697EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001698
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001699void cache_unregister_net(struct cache_detail *cd, struct net *net)
1700{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001701 remove_cache_proc_entries(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001702 sunrpc_destroy_cache_detail(cd);
1703}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001704EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001705
Bhumika Goyald34971a2017-10-17 18:14:23 +02001706struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001707{
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001708 struct cache_detail *cd;
Kinglong Mee129e5822015-07-27 11:10:15 +08001709 int i;
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001710
1711 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1712 if (cd == NULL)
1713 return ERR_PTR(-ENOMEM);
1714
Kees Cook6396bb22018-06-12 14:03:40 -07001715 cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head),
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001716 GFP_KERNEL);
1717 if (cd->hash_table == NULL) {
1718 kfree(cd);
1719 return ERR_PTR(-ENOMEM);
1720 }
Kinglong Mee129e5822015-07-27 11:10:15 +08001721
1722 for (i = 0; i < cd->hash_size; i++)
1723 INIT_HLIST_HEAD(&cd->hash_table[i]);
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001724 cd->net = net;
1725 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001726}
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001727EXPORT_SYMBOL_GPL(cache_create_net);
1728
1729void cache_destroy_net(struct cache_detail *cd, struct net *net)
1730{
1731 kfree(cd->hash_table);
1732 kfree(cd);
1733}
1734EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001735
1736static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1737 size_t count, loff_t *ppos)
1738{
Al Viro496ad9a2013-01-23 17:07:38 -05001739 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001740
1741 return cache_read(filp, buf, count, ppos, cd);
1742}
1743
1744static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1745 size_t count, loff_t *ppos)
1746{
Al Viro496ad9a2013-01-23 17:07:38 -05001747 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001748
1749 return cache_write(filp, buf, count, ppos, cd);
1750}
1751
Al Viroade994f2017-07-03 00:01:49 -04001752static __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait)
Trond Myklebust8854e822009-08-09 15:14:30 -04001753{
Al Viro496ad9a2013-01-23 17:07:38 -05001754 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001755
1756 return cache_poll(filp, wait, cd);
1757}
1758
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001759static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001760 unsigned int cmd, unsigned long arg)
1761{
Al Viro496ad9a2013-01-23 17:07:38 -05001762 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001763 struct cache_detail *cd = RPC_I(inode)->private;
1764
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001765 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001766}
1767
1768static int cache_open_pipefs(struct inode *inode, struct file *filp)
1769{
1770 struct cache_detail *cd = RPC_I(inode)->private;
1771
1772 return cache_open(inode, filp, cd);
1773}
1774
1775static int cache_release_pipefs(struct inode *inode, struct file *filp)
1776{
1777 struct cache_detail *cd = RPC_I(inode)->private;
1778
1779 return cache_release(inode, filp, cd);
1780}
1781
1782const struct file_operations cache_file_operations_pipefs = {
1783 .owner = THIS_MODULE,
1784 .llseek = no_llseek,
1785 .read = cache_read_pipefs,
1786 .write = cache_write_pipefs,
1787 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001788 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001789 .open = cache_open_pipefs,
1790 .release = cache_release_pipefs,
1791};
1792
1793static int content_open_pipefs(struct inode *inode, struct file *filp)
1794{
1795 struct cache_detail *cd = RPC_I(inode)->private;
1796
1797 return content_open(inode, filp, cd);
1798}
1799
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001800static int content_release_pipefs(struct inode *inode, struct file *filp)
1801{
1802 struct cache_detail *cd = RPC_I(inode)->private;
1803
1804 return content_release(inode, filp, cd);
1805}
1806
Trond Myklebust8854e822009-08-09 15:14:30 -04001807const struct file_operations content_file_operations_pipefs = {
1808 .open = content_open_pipefs,
1809 .read = seq_read,
1810 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001811 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001812};
1813
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001814static int open_flush_pipefs(struct inode *inode, struct file *filp)
1815{
1816 struct cache_detail *cd = RPC_I(inode)->private;
1817
1818 return open_flush(inode, filp, cd);
1819}
1820
1821static int release_flush_pipefs(struct inode *inode, struct file *filp)
1822{
1823 struct cache_detail *cd = RPC_I(inode)->private;
1824
1825 return release_flush(inode, filp, cd);
1826}
1827
Trond Myklebust8854e822009-08-09 15:14:30 -04001828static ssize_t read_flush_pipefs(struct file *filp, 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 read_flush(filp, buf, count, ppos, cd);
1834}
1835
1836static ssize_t write_flush_pipefs(struct file *filp,
1837 const char __user *buf,
1838 size_t count, loff_t *ppos)
1839{
Al Viro496ad9a2013-01-23 17:07:38 -05001840 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001841
1842 return write_flush(filp, buf, count, ppos, cd);
1843}
1844
1845const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001846 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001847 .read = read_flush_pipefs,
1848 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001849 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001850 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001851};
1852
1853int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001854 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001855 struct cache_detail *cd)
1856{
Al Viroa95e6912013-07-14 16:43:54 +04001857 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1858 if (IS_ERR(dir))
1859 return PTR_ERR(dir);
Kinglong Mee863d7d92017-02-07 21:47:16 +08001860 cd->pipefs = dir;
Al Viroa95e6912013-07-14 16:43:54 +04001861 return 0;
Trond Myklebust8854e822009-08-09 15:14:30 -04001862}
1863EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1864
1865void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1866{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001867 if (cd->pipefs) {
1868 rpc_remove_cache_dir(cd->pipefs);
1869 cd->pipefs = NULL;
1870 }
Trond Myklebust8854e822009-08-09 15:14:30 -04001871}
1872EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1873
Neil Brown2b477c02016-12-22 12:38:06 -05001874void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h)
1875{
Trond Myklebust1863d772018-10-01 10:41:52 -04001876 spin_lock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001877 if (!hlist_unhashed(&h->cache_list)){
Trond Myklebustae741362018-10-03 12:01:22 -04001878 hlist_del_init_rcu(&h->cache_list);
Neil Brown2b477c02016-12-22 12:38:06 -05001879 cd->entries--;
Trond Myklebust1863d772018-10-01 10:41:52 -04001880 spin_unlock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001881 cache_put(h, cd);
1882 } else
Trond Myklebust1863d772018-10-01 10:41:52 -04001883 spin_unlock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001884}
1885EXPORT_SYMBOL_GPL(sunrpc_cache_unhash);