blob: d223289848537809c0cbde147ade893f7bdd38c2 [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);
NeilBrown9d693382019-03-22 13:16:56 +110043static bool cache_listeners_exist(struct cache_detail *detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Neil Brown77862032015-10-16 08:59:08 +110045static void cache_init(struct cache_head *h, struct cache_detail *detail)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
NeilBrownc5b29f82010-08-12 16:55:22 +100047 time_t now = seconds_since_boot();
Kinglong Mee129e5822015-07-27 11:10:15 +080048 INIT_HLIST_NODE(&h->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 h->flags = 0;
NeilBrownbaab9352006-03-27 01:15:09 -080050 kref_init(&h->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 h->expiry_time = now + CACHE_NEW_EXPIRY;
Neil Brown77862032015-10-16 08:59:08 +110052 if (now <= detail->flush_time)
53 /* ensure it isn't already expired */
54 now = detail->flush_time + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 h->last_refresh = now;
56}
57
NeilBrownd58431ea2019-04-05 11:34:40 +110058static inline int cache_is_valid(struct cache_head *h);
Vasily Averin4ecd55e2018-11-28 11:45:57 +030059static void cache_fresh_locked(struct cache_head *head, time_t expiry,
60 struct cache_detail *detail);
61static void cache_fresh_unlocked(struct cache_head *head,
62 struct cache_detail *detail);
63
Trond Myklebustae741362018-10-03 12:01:22 -040064static struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail,
65 struct cache_head *key,
66 int hash)
67{
68 struct hlist_head *head = &detail->hash_table[hash];
69 struct cache_head *tmp;
70
71 rcu_read_lock();
72 hlist_for_each_entry_rcu(tmp, head, cache_list) {
73 if (detail->match(tmp, key)) {
74 if (cache_is_expired(detail, tmp))
75 continue;
76 tmp = cache_get_rcu(tmp);
77 rcu_read_unlock();
78 return tmp;
79 }
80 }
81 rcu_read_unlock();
82 return NULL;
83}
84
Trond Myklebustb92a8fa2018-10-01 10:41:45 -040085static struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail,
86 struct cache_head *key,
87 int hash)
88{
89 struct cache_head *new, *tmp, *freeme = NULL;
90 struct hlist_head *head = &detail->hash_table[hash];
NeilBrown15a5f6b2006-03-27 01:15:02 -080091
92 new = detail->alloc();
93 if (!new)
94 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070095 /* must fully initialise 'new', else
96 * we might get lose if we need to
97 * cache_put it soon.
98 */
Neil Brown77862032015-10-16 08:59:08 +110099 cache_init(new, detail);
Neil Brown2f349312006-08-05 12:14:29 -0700100 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800101
Trond Myklebust1863d772018-10-01 10:41:52 -0400102 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800103
104 /* check if entry appeared while we slept */
Trond Myklebustae741362018-10-03 12:01:22 -0400105 hlist_for_each_entry_rcu(tmp, head, cache_list) {
NeilBrown15a5f6b2006-03-27 01:15:02 -0800106 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +1100107 if (cache_is_expired(detail, tmp)) {
Trond Myklebustae741362018-10-03 12:01:22 -0400108 hlist_del_init_rcu(&tmp->cache_list);
NeilBrownd202cce2010-02-03 17:31:31 +1100109 detail->entries --;
NeilBrownd58431ea2019-04-05 11:34:40 +1100110 if (cache_is_valid(tmp) == -EAGAIN)
111 set_bit(CACHE_NEGATIVE, &tmp->flags);
Vasily Averin4ecd55e2018-11-28 11:45:57 +0300112 cache_fresh_locked(tmp, 0, detail);
NeilBrownd202cce2010-02-03 17:31:31 +1100113 freeme = tmp;
114 break;
115 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800116 cache_get(tmp);
Trond Myklebust1863d772018-10-01 10:41:52 -0400117 spin_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800118 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800119 return tmp;
120 }
121 }
Kinglong Mee129e5822015-07-27 11:10:15 +0800122
Trond Myklebustae741362018-10-03 12:01:22 -0400123 hlist_add_head_rcu(&new->cache_list, head);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800124 detail->entries++;
125 cache_get(new);
Trond Myklebust1863d772018-10-01 10:41:52 -0400126 spin_unlock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800127
Vasily Averin4ecd55e2018-11-28 11:45:57 +0300128 if (freeme) {
129 cache_fresh_unlocked(freeme, detail);
NeilBrownd202cce2010-02-03 17:31:31 +1100130 cache_put(freeme, detail);
Vasily Averin4ecd55e2018-11-28 11:45:57 +0300131 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800132 return new;
133}
NeilBrown15a5f6b2006-03-27 01:15:02 -0800134
Trond Myklebustae741362018-10-03 12:01:22 -0400135struct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail,
136 struct cache_head *key, int hash)
137{
138 struct cache_head *ret;
139
140 ret = sunrpc_cache_find_rcu(detail, key, hash);
141 if (ret)
142 return ret;
143 /* Didn't find anything, insert an empty entry */
144 return sunrpc_cache_add_entry(detail, key, hash);
145}
146EXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu);
147
NeilBrownf866a812009-08-04 15:22:38 +1000148static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800149
Neil Brown77862032015-10-16 08:59:08 +1100150static void cache_fresh_locked(struct cache_head *head, time_t expiry,
151 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800152{
Neil Brown77862032015-10-16 08:59:08 +1100153 time_t now = seconds_since_boot();
154 if (now <= detail->flush_time)
155 /* ensure it isn't immediately treated as expired */
156 now = detail->flush_time + 1;
NeilBrownebd0cb12006-03-27 01:15:08 -0800157 head->expiry_time = expiry;
Neil Brown77862032015-10-16 08:59:08 +1100158 head->last_refresh = now;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500159 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
NeilBrown908329f2009-09-09 16:32:54 +1000160 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800161}
162
163static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000164 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800165{
NeilBrownebd0cb12006-03-27 01:15:08 -0800166 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
167 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000168 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800169 }
170}
171
NeilBrown15a5f6b2006-03-27 01:15:02 -0800172struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
173 struct cache_head *new, struct cache_head *old, int hash)
174{
175 /* The 'old' entry is to be replaced by 'new'.
176 * If 'old' is not VALID, we update it directly,
177 * otherwise we need to replace it
178 */
NeilBrown15a5f6b2006-03-27 01:15:02 -0800179 struct cache_head *tmp;
180
181 if (!test_bit(CACHE_VALID, &old->flags)) {
Trond Myklebust1863d772018-10-01 10:41:52 -0400182 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800183 if (!test_bit(CACHE_VALID, &old->flags)) {
184 if (test_bit(CACHE_NEGATIVE, &new->flags))
185 set_bit(CACHE_NEGATIVE, &old->flags);
186 else
187 detail->update(old, new);
Neil Brown77862032015-10-16 08:59:08 +1100188 cache_fresh_locked(old, new->expiry_time, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400189 spin_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000190 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800191 return old;
192 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400193 spin_unlock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800194 }
195 /* We need to insert a new entry */
196 tmp = detail->alloc();
197 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800198 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800199 return NULL;
200 }
Neil Brown77862032015-10-16 08:59:08 +1100201 cache_init(tmp, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800202 detail->init(tmp, old);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800203
Trond Myklebust1863d772018-10-01 10:41:52 -0400204 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800205 if (test_bit(CACHE_NEGATIVE, &new->flags))
206 set_bit(CACHE_NEGATIVE, &tmp->flags);
207 else
208 detail->update(tmp, new);
Kinglong Mee129e5822015-07-27 11:10:15 +0800209 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
NeilBrownf2d39582006-05-22 22:35:25 -0700210 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800211 cache_get(tmp);
Neil Brown77862032015-10-16 08:59:08 +1100212 cache_fresh_locked(tmp, new->expiry_time, detail);
213 cache_fresh_locked(old, 0, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400214 spin_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000215 cache_fresh_unlocked(tmp, detail);
216 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800217 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800218 return tmp;
219}
Trond Myklebust24c37672008-12-23 16:30:12 -0500220EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400222static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
223{
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +0300224 if (cd->cache_upcall)
225 return cd->cache_upcall(cd, h);
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +0300226 return sunrpc_cache_pipe_upcall(cd, h);
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400227}
NeilBrown989a19b2009-08-04 15:22:38 +1000228
chaoting fanb6040f92013-03-28 22:19:45 +0800229static inline int cache_is_valid(struct cache_head *h)
NeilBrown989a19b2009-08-04 15:22:38 +1000230{
NeilBrownd202cce2010-02-03 17:31:31 +1100231 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000232 return -EAGAIN;
233 else {
234 /* entry is valid */
235 if (test_bit(CACHE_NEGATIVE, &h->flags))
236 return -ENOENT;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500237 else {
238 /*
239 * In combination with write barrier in
240 * sunrpc_cache_update, ensures that anyone
241 * using the cache entry after this sees the
242 * updated contents:
243 */
244 smp_rmb();
NeilBrown989a19b2009-08-04 15:22:38 +1000245 return 0;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500246 }
NeilBrown989a19b2009-08-04 15:22:38 +1000247 }
248}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400249
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500250static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
251{
252 int rv;
253
Trond Myklebust1863d772018-10-01 10:41:52 -0400254 spin_lock(&detail->hash_lock);
chaoting fanb6040f92013-03-28 22:19:45 +0800255 rv = cache_is_valid(h);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000256 if (rv == -EAGAIN) {
257 set_bit(CACHE_NEGATIVE, &h->flags);
Neil Brown77862032015-10-16 08:59:08 +1100258 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
259 detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000260 rv = -ENOENT;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500261 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400262 spin_unlock(&detail->hash_lock);
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500263 cache_fresh_unlocked(h, detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000264 return rv;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/*
268 * This is the generic cache management routine for all
269 * the authentication caches.
270 * It checks the currency of a cache item and will (later)
271 * initiate an upcall to fill it if needed.
272 *
273 *
274 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000275 * -EAGAIN if upcall is pending and request has been queued
276 * -ETIMEDOUT if upcall failed or request could not be queue or
277 * upcall completed but item is still invalid (implying that
278 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * -ENOENT if cache entry was negative
280 */
281int cache_check(struct cache_detail *detail,
282 struct cache_head *h, struct cache_req *rqstp)
283{
284 int rv;
285 long refresh_age, age;
286
287 /* First decide return status as best we can */
chaoting fanb6040f92013-03-28 22:19:45 +0800288 rv = cache_is_valid(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* now see if we want to start an upcall */
291 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000292 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 if (rqstp == NULL) {
295 if (rv == -EAGAIN)
296 rv = -ENOENT;
NeilBrown0bebc632013-06-13 12:53:42 +1000297 } else if (rv == -EAGAIN ||
298 (h->expiry_time != 0 && age > refresh_age/2)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500299 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
300 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
302 switch (cache_make_upcall(detail, h)) {
303 case -EINVAL:
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500304 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 case -EAGAIN:
NeilBrown2a1c7f52013-06-13 12:53:42 +1000307 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309 }
NeilBrown9d693382019-03-22 13:16:56 +1100310 } else if (!cache_listeners_exist(detail))
311 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
NeilBrown989a19b2009-08-04 15:22:38 +1000314 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500315 if (!cache_defer_req(rqstp, h)) {
316 /*
317 * Request was not deferred; handle it as best
318 * we can ourselves:
319 */
chaoting fanb6040f92013-03-28 22:19:45 +0800320 rv = cache_is_valid(h);
NeilBrown989a19b2009-08-04 15:22:38 +1000321 if (rv == -EAGAIN)
322 rv = -ETIMEDOUT;
323 }
324 }
NeilBrown4013ede2006-03-27 01:15:07 -0800325 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800326 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return rv;
328}
Trond Myklebust24c37672008-12-23 16:30:12 -0500329EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/*
332 * caches need to be periodically cleaned.
333 * For this we maintain a list of cache_detail and
334 * a current pointer into that list and into the table
335 * for that entry.
336 *
NeilBrown013920e2013-06-13 12:53:42 +1000337 * Each time cache_clean is called it finds the next non-empty entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * in the current table and walks the list in that entry
339 * looking for entries that can be removed.
340 *
341 * An entry gets removed if:
342 * - The expiry is before current time
343 * - The last_refresh time is before the flush_time for that cache
344 *
345 * later we might drop old entries with non-NEVER expiry if that table
346 * is getting 'full' for some definition of 'full'
347 *
348 * The question of "how often to scan a table" is an interesting one
349 * and is answered in part by the use of the "nextcheck" field in the
350 * cache_detail.
351 * When a scan of a table begins, the nextcheck field is set to a time
352 * that is well into the future.
353 * While scanning, if an expiry time is found that is earlier than the
354 * current nextcheck time, nextcheck is set to that expiry time.
355 * If the flush_time is ever set to a time earlier than the nextcheck
356 * time, the nextcheck time is then set to that flush_time.
357 *
358 * A table is then only scanned if the current time is at least
359 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800360 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
362
363static LIST_HEAD(cache_list);
364static DEFINE_SPINLOCK(cache_list_lock);
365static struct cache_detail *current_detail;
366static int current_index;
367
David Howells65f27f32006-11-22 14:55:48 +0000368static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300369static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300371void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500372{
Trond Myklebust1863d772018-10-01 10:41:52 -0400373 spin_lock_init(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 INIT_LIST_HEAD(&cd->queue);
375 spin_lock(&cache_list_lock);
376 cd->nextcheck = 0;
377 cd->entries = 0;
378 atomic_set(&cd->readers, 0);
379 cd->last_close = 0;
380 cd->last_warn = -1;
381 list_add(&cd->others, &cache_list);
382 spin_unlock(&cache_list_lock);
383
384 /* start the cleaning process */
Ke Wang77b00bc2016-09-01 15:30:26 +0800385 queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300387EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300389void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 cache_purge(cd);
392 spin_lock(&cache_list_lock);
Trond Myklebust1863d772018-10-01 10:41:52 -0400393 spin_lock(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (current_detail == cd)
395 current_detail = NULL;
396 list_del_init(&cd->others);
Trond Myklebust1863d772018-10-01 10:41:52 -0400397 spin_unlock(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (list_empty(&cache_list)) {
400 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400401 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300404EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406/* clean cache tries to find something to clean
407 * and cleans it.
408 * It returns 1 if it cleaned something,
409 * 0 if it didn't find anything this time
410 * -1 if it fell off the end of the list.
411 */
412static int cache_clean(void)
413{
414 int rv = 0;
415 struct list_head *next;
416
417 spin_lock(&cache_list_lock);
418
419 /* find a suitable table if we don't already have one */
420 while (current_detail == NULL ||
421 current_index >= current_detail->hash_size) {
422 if (current_detail)
423 next = current_detail->others.next;
424 else
425 next = cache_list.next;
426 if (next == &cache_list) {
427 current_detail = NULL;
428 spin_unlock(&cache_list_lock);
429 return -1;
430 }
431 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000432 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 current_index = current_detail->hash_size;
434 else {
435 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000436 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438 }
439
440 /* find a non-empty bucket in the table */
441 while (current_detail &&
442 current_index < current_detail->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +0800443 hlist_empty(&current_detail->hash_table[current_index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 current_index++;
445
446 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (current_detail && current_index < current_detail->hash_size) {
Kinglong Mee129e5822015-07-27 11:10:15 +0800449 struct cache_head *ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 struct cache_detail *d;
Kinglong Mee129e5822015-07-27 11:10:15 +0800451 struct hlist_head *head;
452 struct hlist_node *tmp;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800453
Trond Myklebust1863d772018-10-01 10:41:52 -0400454 spin_lock(&current_detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800457
Kinglong Mee129e5822015-07-27 11:10:15 +0800458 head = &current_detail->hash_table[current_index];
459 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (current_detail->nextcheck > ch->expiry_time)
461 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100462 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Trond Myklebustae741362018-10-03 12:01:22 -0400465 hlist_del_init_rcu(&ch->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 current_detail->entries--;
467 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
NeilBrown3af49742010-02-03 17:31:31 +1100470
Trond Myklebust1863d772018-10-01 10:41:52 -0400471 spin_unlock(&current_detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 d = current_detail;
473 if (!ch)
474 current_index ++;
475 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000476 if (ch) {
NeilBrown013920e2013-06-13 12:53:42 +1000477 set_bit(CACHE_CLEANED, &ch->flags);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000478 cache_fresh_unlocked(ch, d);
NeilBrownbaab9352006-03-27 01:15:09 -0800479 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 } else
482 spin_unlock(&cache_list_lock);
483
484 return rv;
485}
486
487/*
488 * We want to regularly clean the cache, so we need to schedule some work ...
489 */
David Howells65f27f32006-11-22 14:55:48 +0000490static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 int delay = 5;
493 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700494 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 if (list_empty(&cache_list))
497 delay = 0;
498
499 if (delay)
Ke Wang77b00bc2016-09-01 15:30:26 +0800500 queue_delayed_work(system_power_efficient_wq,
501 &cache_cleaner, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800505/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800507 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * be fully cleaned
509 */
510void cache_flush(void)
511{
512 while (cache_clean() != -1)
513 cond_resched();
514 while (cache_clean() != -1)
515 cond_resched();
516}
Trond Myklebust24c37672008-12-23 16:30:12 -0500517EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519void cache_purge(struct cache_detail *detail)
520{
Kinglong Mee471a9302017-02-08 09:54:33 +0800521 struct cache_head *ch = NULL;
522 struct hlist_head *head = NULL;
523 struct hlist_node *tmp = NULL;
524 int i = 0;
525
Trond Myklebust1863d772018-10-01 10:41:52 -0400526 spin_lock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800527 if (!detail->entries) {
Trond Myklebust1863d772018-10-01 10:41:52 -0400528 spin_unlock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800529 return;
530 }
531
532 dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name);
533 for (i = 0; i < detail->hash_size; i++) {
534 head = &detail->hash_table[i];
535 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Trond Myklebustae741362018-10-03 12:01:22 -0400536 hlist_del_init_rcu(&ch->cache_list);
Kinglong Mee471a9302017-02-08 09:54:33 +0800537 detail->entries--;
538
539 set_bit(CACHE_CLEANED, &ch->flags);
Trond Myklebust1863d772018-10-01 10:41:52 -0400540 spin_unlock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800541 cache_fresh_unlocked(ch, detail);
542 cache_put(ch, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400543 spin_lock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800544 }
545 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400546 spin_unlock(&detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
Trond Myklebust24c37672008-12-23 16:30:12 -0500548EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550
551/*
552 * Deferral and Revisiting of Requests.
553 *
554 * If a cache lookup finds a pending entry, we
555 * need to defer the request and revisit it later.
556 * All deferred requests are stored in a hash table,
557 * indexed by "struct cache_head *".
558 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800559 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * deferred form, which must contain a
561 * 'struct cache_deferred_req'
562 * This cache_deferred_req contains a method to allow
563 * it to be revisited when cache info is available
564 */
565
566#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
567#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
568
569#define DFR_MAX 300 /* ??? */
570
571static DEFINE_SPINLOCK(cache_defer_lock);
572static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000573static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574static int cache_defer_cnt;
575
J. Bruce Fields6610f722010-08-26 13:19:52 -0400576static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
NeilBrown11174492010-08-12 17:04:08 +1000578 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100579 if (!list_empty(&dreq->recent)) {
580 list_del_init(&dreq->recent);
581 cache_defer_cnt--;
582 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400583}
584
585static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
586{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int hash = DFR_HASH(item);
588
NeilBrowne33534d2010-10-07 15:29:46 +1100589 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000590 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400591}
592
NeilBrowne33534d2010-10-07 15:29:46 +1100593static void setup_deferral(struct cache_deferred_req *dreq,
594 struct cache_head *item,
595 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 spin_lock(&cache_defer_lock);
601
J. Bruce Fields6610f722010-08-26 13:19:52 -0400602 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
NeilBrowne33534d2010-10-07 15:29:46 +1100604 if (count_me) {
605 cache_defer_cnt++;
606 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
NeilBrowne33534d2010-10-07 15:29:46 +1100608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 spin_unlock(&cache_defer_lock);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
J. Bruce Fields3211af12010-08-26 16:56:23 -0400613struct thread_deferred_req {
614 struct cache_deferred_req handle;
615 struct completion completion;
616};
617
618static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
619{
620 struct thread_deferred_req *dr =
621 container_of(dreq, struct thread_deferred_req, handle);
622 complete(&dr->completion);
623}
624
NeilBrownd29068c2010-10-07 15:29:46 +1100625static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400626{
627 struct thread_deferred_req sleeper;
628 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400629
630 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
631 dreq->revisit = cache_restart_thread;
632
NeilBrowne33534d2010-10-07 15:29:46 +1100633 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400634
NeilBrownd29068c2010-10-07 15:29:46 +1100635 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000636 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400637 &sleeper.completion, req->thread_wait) <= 0) {
638 /* The completion wasn't completed, so we need
639 * to clean up
640 */
641 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000642 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400643 __unhash_deferred_req(&sleeper.handle);
644 spin_unlock(&cache_defer_lock);
645 } else {
646 /* cache_revisit_request already removed
647 * this from the hash table, but hasn't
648 * called ->revisit yet. It will very soon
649 * and we need to wait for it.
650 */
651 spin_unlock(&cache_defer_lock);
652 wait_for_completion(&sleeper.completion);
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400655}
656
NeilBrowne33534d2010-10-07 15:29:46 +1100657static void cache_limit_defers(void)
658{
659 /* Make sure we haven't exceed the limit of allowed deferred
660 * requests.
661 */
662 struct cache_deferred_req *discard = NULL;
663
664 if (cache_defer_cnt <= DFR_MAX)
665 return;
666
667 spin_lock(&cache_defer_lock);
668
669 /* Consider removing either the first or the last */
670 if (cache_defer_cnt > DFR_MAX) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500671 if (prandom_u32() & 1)
NeilBrowne33534d2010-10-07 15:29:46 +1100672 discard = list_entry(cache_defer_list.next,
673 struct cache_deferred_req, recent);
674 else
675 discard = list_entry(cache_defer_list.prev,
676 struct cache_deferred_req, recent);
677 __unhash_deferred_req(discard);
678 }
679 spin_unlock(&cache_defer_lock);
680 if (discard)
681 discard->revisit(discard, 1);
682}
683
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500684/* Return true if and only if a deferred request is queued. */
685static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400686{
687 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400688
J. Bruce Fields3211af12010-08-26 16:56:23 -0400689 if (req->thread_wait) {
NeilBrownd29068c2010-10-07 15:29:46 +1100690 cache_wait_req(req, item);
691 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500692 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400693 }
694 dreq = req->defer(req);
695 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500696 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100697 setup_deferral(dreq, item, 1);
NeilBrownd29068c2010-10-07 15:29:46 +1100698 if (!test_bit(CACHE_PENDING, &item->flags))
699 /* Bit could have been cleared before we managed to
700 * set up the deferral, so need to revisit just in case
701 */
702 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100703
704 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500705 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708static void cache_revisit_request(struct cache_head *item)
709{
710 struct cache_deferred_req *dreq;
711 struct list_head pending;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800712 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int hash = DFR_HASH(item);
714
715 INIT_LIST_HEAD(&pending);
716 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800717
Sasha Levinb67bfe02013-02-27 17:06:00 -0800718 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000719 if (dreq->item == item) {
720 __unhash_deferred_req(dreq);
721 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
NeilBrown11174492010-08-12 17:04:08 +1000723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 spin_unlock(&cache_defer_lock);
725
726 while (!list_empty(&pending)) {
727 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
728 list_del_init(&dreq->recent);
729 dreq->revisit(dreq, 0);
730 }
731}
732
733void cache_clean_deferred(void *owner)
734{
735 struct cache_deferred_req *dreq, *tmp;
736 struct list_head pending;
737
738
739 INIT_LIST_HEAD(&pending);
740 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
743 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400744 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000745 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747 }
748 spin_unlock(&cache_defer_lock);
749
750 while (!list_empty(&pending)) {
751 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
752 list_del_init(&dreq->recent);
753 dreq->revisit(dreq, 1);
754 }
755}
756
757/*
758 * communicate with user-space
759 *
Kinglong Mee6489a8f2017-02-07 21:49:17 +0800760 * We have a magic /proc file - /proc/net/rpc/<cachename>/channel.
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500761 * On read, you get a full request, or block.
762 * On write, an update request is processed.
763 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800765 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500766 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 * to the end and may wakeup and preceding readers.
768 * New readers are added to the head. If, on read, an item is found with
769 * CACHE_UPCALLING clear, we free it from the list.
770 *
771 */
772
773static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800774static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776struct cache_queue {
777 struct list_head list;
778 int reader; /* if 0, then request */
779};
780struct cache_request {
781 struct cache_queue q;
782 struct cache_head *item;
783 char * buf;
784 int len;
785 int readers;
786};
787struct cache_reader {
788 struct cache_queue q;
789 int offset; /* if non-0, we have a refcnt on next request */
790};
791
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300792static int cache_request(struct cache_detail *detail,
793 struct cache_request *crq)
794{
795 char *bp = crq->buf;
796 int len = PAGE_SIZE;
797
798 detail->cache_request(detail, crq->item, &bp, &len);
799 if (len < 0)
800 return -EAGAIN;
801 return PAGE_SIZE - len;
802}
803
Trond Myklebust173912a2009-08-09 15:14:29 -0400804static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
805 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct cache_reader *rp = filp->private_data;
808 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500809 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 int err;
811
812 if (count == 0)
813 return 0;
814
Al Viro59551022016-01-22 15:40:57 -0500815 inode_lock(inode); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * readers on this file */
817 again:
818 spin_lock(&queue_lock);
819 /* need to find next request */
820 while (rp->q.list.next != &cd->queue &&
821 list_entry(rp->q.list.next, struct cache_queue, list)
822 ->reader) {
823 struct list_head *next = rp->q.list.next;
824 list_move(&rp->q.list, next);
825 }
826 if (rp->q.list.next == &cd->queue) {
827 spin_unlock(&queue_lock);
Al Viro59551022016-01-22 15:40:57 -0500828 inode_unlock(inode);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400829 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return 0;
831 }
832 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400833 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (rp->offset == 0)
835 rq->readers++;
836 spin_unlock(&queue_lock);
837
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300838 if (rq->len == 0) {
839 err = cache_request(cd, rq);
840 if (err < 0)
841 goto out;
842 rq->len = err;
843 }
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
846 err = -EAGAIN;
847 spin_lock(&queue_lock);
848 list_move(&rp->q.list, &rq->q.list);
849 spin_unlock(&queue_lock);
850 } else {
851 if (rp->offset + count > rq->len)
852 count = rq->len - rp->offset;
853 err = -EFAULT;
854 if (copy_to_user(buf, rq->buf + rp->offset, count))
855 goto out;
856 rp->offset += count;
857 if (rp->offset >= rq->len) {
858 rp->offset = 0;
859 spin_lock(&queue_lock);
860 list_move(&rp->q.list, &rq->q.list);
861 spin_unlock(&queue_lock);
862 }
863 err = 0;
864 }
865 out:
866 if (rp->offset == 0) {
867 /* need to release rq */
868 spin_lock(&queue_lock);
869 rq->readers--;
870 if (rq->readers == 0 &&
871 !test_bit(CACHE_PENDING, &rq->item->flags)) {
872 list_del(&rq->q.list);
873 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800874 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 kfree(rq->buf);
876 kfree(rq);
877 } else
878 spin_unlock(&queue_lock);
879 }
880 if (err == -EAGAIN)
881 goto again;
Al Viro59551022016-01-22 15:40:57 -0500882 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return err ? err : count;
884}
885
Trond Myklebustda770052009-08-09 15:14:28 -0400886static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
887 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Trond Myklebustda770052009-08-09 15:14:28 -0400889 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300891 if (count == 0)
892 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400893 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400895 kaddr[count] = '\0';
896 ret = cd->cache_parse(cd, kaddr, count);
897 if (!ret)
898 ret = count;
899 return ret;
900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Trond Myklebustda770052009-08-09 15:14:28 -0400902static ssize_t cache_slow_downcall(const char __user *buf,
903 size_t count, struct cache_detail *cd)
904{
905 static char write_buf[8192]; /* protected by queue_io_mutex */
906 ssize_t ret = -EINVAL;
907
908 if (count >= sizeof(write_buf))
909 goto out;
910 mutex_lock(&queue_io_mutex);
911 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800912 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400913out:
914 return ret;
915}
916
917static ssize_t cache_downcall(struct address_space *mapping,
918 const char __user *buf,
919 size_t count, struct cache_detail *cd)
920{
921 struct page *page;
922 char *kaddr;
923 ssize_t ret = -ENOMEM;
924
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300925 if (count >= PAGE_SIZE)
Trond Myklebustda770052009-08-09 15:14:28 -0400926 goto out_slow;
927
928 page = find_or_create_page(mapping, 0, GFP_KERNEL);
929 if (!page)
930 goto out_slow;
931
932 kaddr = kmap(page);
933 ret = cache_do_downcall(kaddr, buf, count, cd);
934 kunmap(page);
935 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300936 put_page(page);
Trond Myklebustda770052009-08-09 15:14:28 -0400937 return ret;
938out_slow:
939 return cache_slow_downcall(buf, count, cd);
940}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Trond Myklebust173912a2009-08-09 15:14:29 -0400942static ssize_t cache_write(struct file *filp, const char __user *buf,
943 size_t count, loff_t *ppos,
944 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Trond Myklebustda770052009-08-09 15:14:28 -0400946 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500947 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400948 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Trond Myklebustda770052009-08-09 15:14:28 -0400950 if (!cd->cache_parse)
951 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Al Viro59551022016-01-22 15:40:57 -0500953 inode_lock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400954 ret = cache_downcall(mapping, buf, count, cd);
Al Viro59551022016-01-22 15:40:57 -0500955 inode_unlock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400956out:
957 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
961
Al Viroade994f2017-07-03 00:01:49 -0400962static __poll_t cache_poll(struct file *filp, poll_table *wait,
Trond Myklebust173912a2009-08-09 15:14:29 -0400963 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Al Viroade994f2017-07-03 00:01:49 -0400965 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 struct cache_reader *rp = filp->private_data;
967 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 poll_wait(filp, &queue_wait, wait);
970
971 /* alway allow write */
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800972 mask = EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 if (!rp)
975 return mask;
976
977 spin_lock(&queue_lock);
978
979 for (cq= &rp->q; &cq->list != &cd->queue;
980 cq = list_entry(cq->list.next, struct cache_queue, list))
981 if (!cq->reader) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800982 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
984 }
985 spin_unlock(&queue_lock);
986 return mask;
987}
988
Trond Myklebust173912a2009-08-09 15:14:29 -0400989static int cache_ioctl(struct inode *ino, struct file *filp,
990 unsigned int cmd, unsigned long arg,
991 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 int len = 0;
994 struct cache_reader *rp = filp->private_data;
995 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 if (cmd != FIONREAD || !rp)
998 return -EINVAL;
999
1000 spin_lock(&queue_lock);
1001
1002 /* only find the length remaining in current request,
1003 * or the length of the next request
1004 */
1005 for (cq= &rp->q; &cq->list != &cd->queue;
1006 cq = list_entry(cq->list.next, struct cache_queue, list))
1007 if (!cq->reader) {
1008 struct cache_request *cr =
1009 container_of(cq, struct cache_request, q);
1010 len = cr->len - rp->offset;
1011 break;
1012 }
1013 spin_unlock(&queue_lock);
1014
1015 return put_user(len, (int __user *)arg);
1016}
1017
Trond Myklebust173912a2009-08-09 15:14:29 -04001018static int cache_open(struct inode *inode, struct file *filp,
1019 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 struct cache_reader *rp = NULL;
1022
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001023 if (!cd || !try_module_get(cd->owner))
1024 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 nonseekable_open(inode, filp);
1026 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +04001028 if (!rp) {
1029 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +04001031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 rp->offset = 0;
1033 rp->q.reader = 1;
1034 atomic_inc(&cd->readers);
1035 spin_lock(&queue_lock);
1036 list_add(&rp->q.list, &cd->queue);
1037 spin_unlock(&queue_lock);
1038 }
1039 filp->private_data = rp;
1040 return 0;
1041}
1042
Trond Myklebust173912a2009-08-09 15:14:29 -04001043static int cache_release(struct inode *inode, struct file *filp,
1044 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 if (rp) {
1049 spin_lock(&queue_lock);
1050 if (rp->offset) {
1051 struct cache_queue *cq;
1052 for (cq= &rp->q; &cq->list != &cd->queue;
1053 cq = list_entry(cq->list.next, struct cache_queue, list))
1054 if (!cq->reader) {
1055 container_of(cq, struct cache_request, q)
1056 ->readers--;
1057 break;
1058 }
1059 rp->offset = 0;
1060 }
1061 list_del(&rp->q.list);
1062 spin_unlock(&queue_lock);
1063
1064 filp->private_data = NULL;
1065 kfree(rp);
1066
NeilBrownc5b29f82010-08-12 16:55:22 +10001067 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 atomic_dec(&cd->readers);
1069 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001070 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return 0;
1072}
1073
1074
1075
NeilBrownf866a812009-08-04 15:22:38 +10001076static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001078 struct cache_queue *cq, *tmp;
1079 struct cache_request *cr;
1080 struct list_head dequeued;
1081
1082 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001084 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001086 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (cr->item != ch)
1088 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001089 if (test_bit(CACHE_PENDING, &ch->flags))
1090 /* Lost a race and it is pending again */
1091 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001093 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001094 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001097 while (!list_empty(&dequeued)) {
1098 cr = list_entry(dequeued.next, struct cache_request, q.list);
1099 list_del(&cr->q.list);
1100 cache_put(cr->item, detail);
1101 kfree(cr->buf);
1102 kfree(cr);
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106/*
1107 * Support routines for text-based upcalls.
1108 * Fields are separated by spaces.
1109 * Fields are either mangled to quote space tab newline slosh with slosh
1110 * or a hexified with a leading \x
1111 * Record is terminated with newline.
1112 *
1113 */
1114
1115void qword_add(char **bpp, int *lp, char *str)
1116{
1117 char *bp = *bpp;
1118 int len = *lp;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001119 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (len < 0) return;
1122
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001123 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1124 if (ret >= len) {
1125 bp += len;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001126 len = -1;
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001127 } else {
1128 bp += ret;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001129 len -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 *bp++ = ' ';
1131 len--;
1132 }
1133 *bpp = bp;
1134 *lp = len;
1135}
Trond Myklebust24c37672008-12-23 16:30:12 -05001136EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1139{
1140 char *bp = *bpp;
1141 int len = *lp;
1142
1143 if (len < 0) return;
1144
1145 if (len > 2) {
1146 *bp++ = '\\';
1147 *bp++ = 'x';
1148 len -= 2;
1149 while (blen && len >= 2) {
Andy Shevchenko056785e2013-12-12 15:49:21 +02001150 bp = hex_byte_pack(bp, *buf++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 len -= 2;
1152 blen--;
1153 }
1154 }
1155 if (blen || len<1) len = -1;
1156 else {
1157 *bp++ = ' ';
1158 len--;
1159 }
1160 *bpp = bp;
1161 *lp = len;
1162}
Trond Myklebust24c37672008-12-23 16:30:12 -05001163EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165static void warn_no_listener(struct cache_detail *detail)
1166{
1167 if (detail->last_warn != detail->last_close) {
1168 detail->last_warn = detail->last_close;
1169 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001170 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172}
1173
J. Bruce Fields06497522010-09-19 22:55:06 -04001174static bool cache_listeners_exist(struct cache_detail *detail)
1175{
1176 if (atomic_read(&detail->readers))
1177 return true;
1178 if (detail->last_close == 0)
1179 /* This cache was never opened */
1180 return false;
1181 if (detail->last_close < seconds_since_boot() - 30)
1182 /*
1183 * We allow for the possibility that someone might
1184 * restart a userspace daemon without restarting the
1185 * server; but after 30 seconds, we give up.
1186 */
1187 return false;
1188 return true;
1189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001192 * register an upcall request to user-space and queue it up for read() by the
1193 * upcall daemon.
1194 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 * Each request is at most one page long.
1196 */
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +03001197int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199
1200 char *buf;
1201 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001202 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001204 if (!detail->cache_request)
1205 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
J. Bruce Fields06497522010-09-19 22:55:06 -04001207 if (!cache_listeners_exist(detail)) {
1208 warn_no_listener(detail);
1209 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
NeilBrown013920e2013-06-13 12:53:42 +10001211 if (test_bit(CACHE_CLEANED, &h->flags))
1212 /* Too late to make an upcall */
1213 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1216 if (!buf)
1217 return -EAGAIN;
1218
1219 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1220 if (!crq) {
1221 kfree(buf);
1222 return -EAGAIN;
1223 }
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 crq->q.reader = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001227 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 crq->readers = 0;
1229 spin_lock(&queue_lock);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001230 if (test_bit(CACHE_PENDING, &h->flags)) {
1231 crq->item = cache_get(h);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001232 list_add_tail(&crq->q.list, &detail->queue);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001233 } else
NeilBrownf9e1aed2013-06-13 12:53:42 +10001234 /* Lost a race, no longer PENDING, so don't enqueue */
1235 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 spin_unlock(&queue_lock);
1237 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001238 if (ret == -EAGAIN) {
1239 kfree(buf);
1240 kfree(crq);
1241 }
1242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001244EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246/*
1247 * parse a message from user-space and pass it
1248 * to an appropriate cache
1249 * Messages are, like requests, separated into fields by
1250 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1251 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001252 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 * reply cachename expiry key ... content....
1254 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001255 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 */
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258int qword_get(char **bpp, char *dest, int bufsize)
1259{
1260 /* return bytes copied, or -1 on error */
1261 char *bp = *bpp;
1262 int len = 0;
1263
1264 while (*bp == ' ') bp++;
1265
1266 if (bp[0] == '\\' && bp[1] == 'x') {
1267 /* HEX STRING */
1268 bp += 2;
Stefan Hajnoczib7052cd2016-02-18 18:55:54 +00001269 while (len < bufsize - 1) {
Andy Shevchenkoe7f483ea2010-09-21 09:40:25 +03001270 int h, l;
1271
1272 h = hex_to_bin(bp[0]);
1273 if (h < 0)
1274 break;
1275
1276 l = hex_to_bin(bp[1]);
1277 if (l < 0)
1278 break;
1279
1280 *dest++ = (h << 4) | l;
1281 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 len++;
1283 }
1284 } else {
1285 /* text with \nnn octal quoting */
1286 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1287 if (*bp == '\\' &&
1288 isodigit(bp[1]) && (bp[1] <= '3') &&
1289 isodigit(bp[2]) &&
1290 isodigit(bp[3])) {
1291 int byte = (*++bp -'0');
1292 bp++;
1293 byte = (byte << 3) | (*bp++ - '0');
1294 byte = (byte << 3) | (*bp++ - '0');
1295 *dest++ = byte;
1296 len++;
1297 } else {
1298 *dest++ = *bp++;
1299 len++;
1300 }
1301 }
1302 }
1303
1304 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1305 return -1;
1306 while (*bp == ' ') bp++;
1307 *bpp = bp;
1308 *dest = '\0';
1309 return len;
1310}
Trond Myklebust24c37672008-12-23 16:30:12 -05001311EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313
1314/*
Kinglong Mee6489a8f2017-02-07 21:49:17 +08001315 * support /proc/net/rpc/$CACHENAME/content
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * as a seqfile.
1317 * We call ->cache_show passing NULL for the item to
1318 * get a header, then pass each real item in the cache
1319 */
1320
Trond Myklebustae741362018-10-03 12:01:22 -04001321static void *__cache_seq_start(struct seq_file *m, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001324 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 struct cache_head *ch;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001326 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (!n--)
1329 return SEQ_START_TOKEN;
1330 hash = n >> 32;
1331 entry = n & ((1LL<<32) - 1);
1332
Trond Myklebustae741362018-10-03 12:01:22 -04001333 hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (!entry--)
1335 return ch;
1336 n &= ~((1LL<<32) - 1);
1337 do {
1338 hash++;
1339 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001340 } while(hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001341 hlist_empty(&cd->hash_table[hash]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (hash >= cd->hash_size)
1343 return NULL;
1344 *pos = n+1;
Trond Myklebustae741362018-10-03 12:01:22 -04001345 return hlist_entry_safe(rcu_dereference_raw(
1346 hlist_first_rcu(&cd->hash_table[hash])),
Kinglong Mee129e5822015-07-27 11:10:15 +08001347 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
Trond Myklebustae741362018-10-03 12:01:22 -04001349
Trond Myklebustd48cf352018-10-01 10:41:51 -04001350static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 struct cache_head *ch = p;
1353 int hash = (*pos >> 32);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001354 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 if (p == SEQ_START_TOKEN)
1357 hash = 0;
Kinglong Mee129e5822015-07-27 11:10:15 +08001358 else if (ch->cache_list.next == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 hash++;
1360 *pos += 1LL<<32;
1361 } else {
1362 ++*pos;
Trond Myklebustae741362018-10-03 12:01:22 -04001363 return hlist_entry_safe(rcu_dereference_raw(
1364 hlist_next_rcu(&ch->cache_list)),
Kinglong Mee129e5822015-07-27 11:10:15 +08001365 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 *pos &= ~((1LL<<32) - 1);
1368 while (hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001369 hlist_empty(&cd->hash_table[hash])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 hash++;
1371 *pos += 1LL<<32;
1372 }
1373 if (hash >= cd->hash_size)
1374 return NULL;
1375 ++*pos;
Trond Myklebustae741362018-10-03 12:01:22 -04001376 return hlist_entry_safe(rcu_dereference_raw(
1377 hlist_first_rcu(&cd->hash_table[hash])),
Kinglong Mee129e5822015-07-27 11:10:15 +08001378 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
Kinglong Meec8c081b2015-07-27 11:09:42 +08001380EXPORT_SYMBOL_GPL(cache_seq_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Trond Myklebustae741362018-10-03 12:01:22 -04001382void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos)
1383 __acquires(RCU)
1384{
1385 rcu_read_lock();
1386 return __cache_seq_start(m, pos);
1387}
1388EXPORT_SYMBOL_GPL(cache_seq_start_rcu);
1389
1390void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos)
1391{
1392 return cache_seq_next(file, p, pos);
1393}
1394EXPORT_SYMBOL_GPL(cache_seq_next_rcu);
1395
1396void cache_seq_stop_rcu(struct seq_file *m, void *p)
1397 __releases(RCU)
1398{
1399 rcu_read_unlock();
1400}
1401EXPORT_SYMBOL_GPL(cache_seq_stop_rcu);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403static int c_show(struct seq_file *m, void *p)
1404{
1405 struct cache_head *cp = p;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001406 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 if (p == SEQ_START_TOKEN)
1409 return cd->cache_show(m, cd, NULL);
1410
1411 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001412 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001413 convert_to_wallclock(cp->expiry_time),
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001414 kref_read(&cp->ref), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 cache_get(cp);
1416 if (cache_check(cd, cp, NULL))
1417 /* cache_check does a cache_put on failure */
1418 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001419 else {
1420 if (cache_is_expired(cd, cp))
1421 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 return cd->cache_show(m, cd, cp);
1426}
1427
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001428static const struct seq_operations cache_content_op = {
Trond Myklebustd48cf352018-10-01 10:41:51 -04001429 .start = cache_seq_start_rcu,
1430 .next = cache_seq_next_rcu,
1431 .stop = cache_seq_stop_rcu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 .show = c_show,
1433};
1434
Trond Myklebust173912a2009-08-09 15:14:29 -04001435static int content_open(struct inode *inode, struct file *file,
1436 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001438 struct seq_file *seq;
1439 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001441 if (!cd || !try_module_get(cd->owner))
1442 return -EACCES;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001443
1444 err = seq_open(file, &cache_content_op);
1445 if (err) {
Li Zefana5990ea2010-03-11 14:08:10 -08001446 module_put(cd->owner);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001447 return err;
Li Zefana5990ea2010-03-11 14:08:10 -08001448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001450 seq = file->private_data;
1451 seq->private = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001455static int content_release(struct inode *inode, struct file *file,
1456 struct cache_detail *cd)
1457{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001458 int ret = seq_release(inode, file);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001459 module_put(cd->owner);
1460 return ret;
1461}
1462
1463static int open_flush(struct inode *inode, struct file *file,
1464 struct cache_detail *cd)
1465{
1466 if (!cd || !try_module_get(cd->owner))
1467 return -EACCES;
1468 return nonseekable_open(inode, file);
1469}
1470
1471static int release_flush(struct inode *inode, struct file *file,
1472 struct cache_detail *cd)
1473{
1474 module_put(cd->owner);
1475 return 0;
1476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001479 size_t count, loff_t *ppos,
1480 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Sasha Levin212ba902012-07-17 00:01:26 +02001482 char tbuf[22];
Chuck Lever01b29692007-10-26 13:31:20 -04001483 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Kinglong Mee8ccc8692017-02-07 21:50:32 +08001485 len = snprintf(tbuf, sizeof(tbuf), "%lu\n",
1486 convert_to_wallclock(cd->flush_time));
1487 return simple_read_from_buffer(buf, count, ppos, tbuf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Trond Myklebust173912a2009-08-09 15:14:29 -04001490static ssize_t write_flush(struct file *file, const char __user *buf,
1491 size_t count, loff_t *ppos,
1492 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 char tbuf[20];
NeilBrown3b68e6e2018-02-14 12:15:06 +11001495 char *ep;
1496 time_t now;
NeilBrownc5b29f82010-08-12 16:55:22 +10001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (*ppos || count > sizeof(tbuf)-1)
1499 return -EINVAL;
1500 if (copy_from_user(tbuf, buf, count))
1501 return -EFAULT;
1502 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001503 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (*ep && *ep != '\n')
1505 return -EINVAL;
NeilBrown3b68e6e2018-02-14 12:15:06 +11001506 /* Note that while we check that 'buf' holds a valid number,
1507 * we always ignore the value and just flush everything.
1508 * Making use of the number leads to races.
Neil Brown77862032015-10-16 08:59:08 +11001509 */
Neil Brown77862032015-10-16 08:59:08 +11001510
NeilBrown3b68e6e2018-02-14 12:15:06 +11001511 now = seconds_since_boot();
1512 /* Always flush everything, so behave like cache_purge()
1513 * Do this by advancing flush_time to the current time,
1514 * or by one second if it has already reached the current time.
1515 * Newly added cache entries will always have ->last_refresh greater
1516 * that ->flush_time, so they don't get flushed prematurely.
1517 */
1518
1519 if (cd->flush_time >= now)
1520 now = cd->flush_time + 1;
1521
1522 cd->flush_time = now;
1523 cd->nextcheck = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 cache_flush();
1525
1526 *ppos += count;
1527 return count;
1528}
1529
Trond Myklebust173912a2009-08-09 15:14:29 -04001530static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1531 size_t count, loff_t *ppos)
1532{
Al Virod9dda782013-03-31 18:16:14 -04001533 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001534
1535 return cache_read(filp, buf, count, ppos, cd);
1536}
1537
1538static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1539 size_t count, loff_t *ppos)
1540{
Al Virod9dda782013-03-31 18:16:14 -04001541 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001542
1543 return cache_write(filp, buf, count, ppos, cd);
1544}
1545
Al Viroade994f2017-07-03 00:01:49 -04001546static __poll_t cache_poll_procfs(struct file *filp, poll_table *wait)
Trond Myklebust173912a2009-08-09 15:14:29 -04001547{
Al Virod9dda782013-03-31 18:16:14 -04001548 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001549
1550 return cache_poll(filp, wait, cd);
1551}
1552
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001553static long cache_ioctl_procfs(struct file *filp,
1554 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001555{
Al Viro496ad9a2013-01-23 17:07:38 -05001556 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001557 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001558
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001559 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001560}
1561
1562static int cache_open_procfs(struct inode *inode, struct file *filp)
1563{
Al Virod9dda782013-03-31 18:16:14 -04001564 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001565
1566 return cache_open(inode, filp, cd);
1567}
1568
1569static int cache_release_procfs(struct inode *inode, struct file *filp)
1570{
Al Virod9dda782013-03-31 18:16:14 -04001571 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001572
1573 return cache_release(inode, filp, cd);
1574}
1575
1576static const struct file_operations cache_file_operations_procfs = {
1577 .owner = THIS_MODULE,
1578 .llseek = no_llseek,
1579 .read = cache_read_procfs,
1580 .write = cache_write_procfs,
1581 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001582 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001583 .open = cache_open_procfs,
1584 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585};
Trond Myklebust173912a2009-08-09 15:14:29 -04001586
1587static int content_open_procfs(struct inode *inode, struct file *filp)
1588{
Al Virod9dda782013-03-31 18:16:14 -04001589 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001590
1591 return content_open(inode, filp, cd);
1592}
1593
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001594static int content_release_procfs(struct inode *inode, struct file *filp)
1595{
Al Virod9dda782013-03-31 18:16:14 -04001596 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001597
1598 return content_release(inode, filp, cd);
1599}
1600
Trond Myklebust173912a2009-08-09 15:14:29 -04001601static const struct file_operations content_file_operations_procfs = {
1602 .open = content_open_procfs,
1603 .read = seq_read,
1604 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001605 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001606};
1607
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001608static int open_flush_procfs(struct inode *inode, struct file *filp)
1609{
Al Virod9dda782013-03-31 18:16:14 -04001610 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001611
1612 return open_flush(inode, filp, cd);
1613}
1614
1615static int release_flush_procfs(struct inode *inode, struct file *filp)
1616{
Al Virod9dda782013-03-31 18:16:14 -04001617 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001618
1619 return release_flush(inode, filp, cd);
1620}
1621
Trond Myklebust173912a2009-08-09 15:14:29 -04001622static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1623 size_t count, loff_t *ppos)
1624{
Al Virod9dda782013-03-31 18:16:14 -04001625 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001626
1627 return read_flush(filp, buf, count, ppos, cd);
1628}
1629
1630static ssize_t write_flush_procfs(struct file *filp,
1631 const char __user *buf,
1632 size_t count, loff_t *ppos)
1633{
Al Virod9dda782013-03-31 18:16:14 -04001634 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001635
1636 return write_flush(filp, buf, count, ppos, cd);
1637}
1638
1639static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001640 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001641 .read = read_flush_procfs,
1642 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001643 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001644 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001645};
1646
Kinglong Mee863d7d92017-02-07 21:47:16 +08001647static void remove_cache_proc_entries(struct cache_detail *cd)
Trond Myklebust173912a2009-08-09 15:14:29 -04001648{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001649 if (cd->procfs) {
1650 proc_remove(cd->procfs);
1651 cd->procfs = NULL;
1652 }
Trond Myklebust173912a2009-08-09 15:14:29 -04001653}
1654
1655#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001656static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001657{
1658 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001659 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001660
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001661 sn = net_generic(net, sunrpc_net_id);
Kinglong Mee863d7d92017-02-07 21:47:16 +08001662 cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc);
1663 if (cd->procfs == NULL)
Trond Myklebust173912a2009-08-09 15:14:29 -04001664 goto out_nomem;
Trond Myklebust173912a2009-08-09 15:14:29 -04001665
Joe Perchesd6444062018-03-23 15:54:38 -07001666 p = proc_create_data("flush", S_IFREG | 0600,
Kinglong Mee863d7d92017-02-07 21:47:16 +08001667 cd->procfs, &cache_flush_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001668 if (p == NULL)
1669 goto out_nomem;
1670
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001671 if (cd->cache_request || cd->cache_parse) {
Joe Perchesd6444062018-03-23 15:54:38 -07001672 p = proc_create_data("channel", S_IFREG | 0600, cd->procfs,
1673 &cache_file_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001674 if (p == NULL)
1675 goto out_nomem;
1676 }
1677 if (cd->cache_show) {
Joe Perchesd6444062018-03-23 15:54:38 -07001678 p = proc_create_data("content", S_IFREG | 0400, cd->procfs,
1679 &content_file_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001680 if (p == NULL)
1681 goto out_nomem;
1682 }
1683 return 0;
1684out_nomem:
Kinglong Mee863d7d92017-02-07 21:47:16 +08001685 remove_cache_proc_entries(cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001686 return -ENOMEM;
1687}
1688#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001689static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001690{
1691 return 0;
1692}
1693#endif
1694
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001695void __init cache_initialize(void)
1696{
Tejun Heo203b42f2012-08-21 13:18:23 -07001697 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001698}
1699
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001700int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001701{
1702 int ret;
1703
1704 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001705 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001706 if (ret)
1707 sunrpc_destroy_cache_detail(cd);
1708 return ret;
1709}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001710EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001711
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001712void cache_unregister_net(struct cache_detail *cd, struct net *net)
1713{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001714 remove_cache_proc_entries(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001715 sunrpc_destroy_cache_detail(cd);
1716}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001717EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001718
Bhumika Goyald34971a2017-10-17 18:14:23 +02001719struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001720{
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001721 struct cache_detail *cd;
Kinglong Mee129e5822015-07-27 11:10:15 +08001722 int i;
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001723
1724 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1725 if (cd == NULL)
1726 return ERR_PTR(-ENOMEM);
1727
Kees Cook6396bb22018-06-12 14:03:40 -07001728 cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head),
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001729 GFP_KERNEL);
1730 if (cd->hash_table == NULL) {
1731 kfree(cd);
1732 return ERR_PTR(-ENOMEM);
1733 }
Kinglong Mee129e5822015-07-27 11:10:15 +08001734
1735 for (i = 0; i < cd->hash_size; i++)
1736 INIT_HLIST_HEAD(&cd->hash_table[i]);
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001737 cd->net = net;
1738 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001739}
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001740EXPORT_SYMBOL_GPL(cache_create_net);
1741
1742void cache_destroy_net(struct cache_detail *cd, struct net *net)
1743{
1744 kfree(cd->hash_table);
1745 kfree(cd);
1746}
1747EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001748
1749static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1750 size_t count, loff_t *ppos)
1751{
Al Viro496ad9a2013-01-23 17:07:38 -05001752 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001753
1754 return cache_read(filp, buf, count, ppos, cd);
1755}
1756
1757static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1758 size_t count, loff_t *ppos)
1759{
Al Viro496ad9a2013-01-23 17:07:38 -05001760 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001761
1762 return cache_write(filp, buf, count, ppos, cd);
1763}
1764
Al Viroade994f2017-07-03 00:01:49 -04001765static __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait)
Trond Myklebust8854e822009-08-09 15:14:30 -04001766{
Al Viro496ad9a2013-01-23 17:07:38 -05001767 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001768
1769 return cache_poll(filp, wait, cd);
1770}
1771
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001772static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001773 unsigned int cmd, unsigned long arg)
1774{
Al Viro496ad9a2013-01-23 17:07:38 -05001775 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001776 struct cache_detail *cd = RPC_I(inode)->private;
1777
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001778 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001779}
1780
1781static int cache_open_pipefs(struct inode *inode, struct file *filp)
1782{
1783 struct cache_detail *cd = RPC_I(inode)->private;
1784
1785 return cache_open(inode, filp, cd);
1786}
1787
1788static int cache_release_pipefs(struct inode *inode, struct file *filp)
1789{
1790 struct cache_detail *cd = RPC_I(inode)->private;
1791
1792 return cache_release(inode, filp, cd);
1793}
1794
1795const struct file_operations cache_file_operations_pipefs = {
1796 .owner = THIS_MODULE,
1797 .llseek = no_llseek,
1798 .read = cache_read_pipefs,
1799 .write = cache_write_pipefs,
1800 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001801 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001802 .open = cache_open_pipefs,
1803 .release = cache_release_pipefs,
1804};
1805
1806static int content_open_pipefs(struct inode *inode, struct file *filp)
1807{
1808 struct cache_detail *cd = RPC_I(inode)->private;
1809
1810 return content_open(inode, filp, cd);
1811}
1812
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001813static int content_release_pipefs(struct inode *inode, struct file *filp)
1814{
1815 struct cache_detail *cd = RPC_I(inode)->private;
1816
1817 return content_release(inode, filp, cd);
1818}
1819
Trond Myklebust8854e822009-08-09 15:14:30 -04001820const struct file_operations content_file_operations_pipefs = {
1821 .open = content_open_pipefs,
1822 .read = seq_read,
1823 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001824 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001825};
1826
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001827static int open_flush_pipefs(struct inode *inode, struct file *filp)
1828{
1829 struct cache_detail *cd = RPC_I(inode)->private;
1830
1831 return open_flush(inode, filp, cd);
1832}
1833
1834static int release_flush_pipefs(struct inode *inode, struct file *filp)
1835{
1836 struct cache_detail *cd = RPC_I(inode)->private;
1837
1838 return release_flush(inode, filp, cd);
1839}
1840
Trond Myklebust8854e822009-08-09 15:14:30 -04001841static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1842 size_t count, loff_t *ppos)
1843{
Al Viro496ad9a2013-01-23 17:07:38 -05001844 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001845
1846 return read_flush(filp, buf, count, ppos, cd);
1847}
1848
1849static ssize_t write_flush_pipefs(struct file *filp,
1850 const char __user *buf,
1851 size_t count, loff_t *ppos)
1852{
Al Viro496ad9a2013-01-23 17:07:38 -05001853 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001854
1855 return write_flush(filp, buf, count, ppos, cd);
1856}
1857
1858const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001859 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001860 .read = read_flush_pipefs,
1861 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001862 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001863 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001864};
1865
1866int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001867 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001868 struct cache_detail *cd)
1869{
Al Viroa95e6912013-07-14 16:43:54 +04001870 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1871 if (IS_ERR(dir))
1872 return PTR_ERR(dir);
Kinglong Mee863d7d92017-02-07 21:47:16 +08001873 cd->pipefs = dir;
Al Viroa95e6912013-07-14 16:43:54 +04001874 return 0;
Trond Myklebust8854e822009-08-09 15:14:30 -04001875}
1876EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1877
1878void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1879{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001880 if (cd->pipefs) {
1881 rpc_remove_cache_dir(cd->pipefs);
1882 cd->pipefs = NULL;
1883 }
Trond Myklebust8854e822009-08-09 15:14:30 -04001884}
1885EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1886
Neil Brown2b477c02016-12-22 12:38:06 -05001887void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h)
1888{
Trond Myklebust1863d772018-10-01 10:41:52 -04001889 spin_lock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001890 if (!hlist_unhashed(&h->cache_list)){
Trond Myklebustae741362018-10-03 12:01:22 -04001891 hlist_del_init_rcu(&h->cache_list);
Neil Brown2b477c02016-12-22 12:38:06 -05001892 cd->entries--;
Trond Myklebust1863d772018-10-01 10:41:52 -04001893 spin_unlock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001894 cache_put(h, cd);
1895 } else
Trond Myklebust1863d772018-10-01 10:41:52 -04001896 spin_unlock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001897}
1898EXPORT_SYMBOL_GPL(sunrpc_cache_unhash);