blob: cd76ef2d26b82a8af8867b141a8d48f4f20a6370 [file] [log] [blame]
Thomas Gleixnerddc64d02019-05-31 01:09:24 -07001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/sunrpc/cache.c
4 *
5 * Generic code for various authentication-related caches
6 * used by sunrpc clients and servers.
7 *
8 * Copyright (C) 2002 Neil Brown <[email protected]>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/types.h>
12#include <linux/fs.h>
13#include <linux/file.h>
14#include <linux/slab.h>
15#include <linux/signal.h>
16#include <linux/sched.h>
17#include <linux/kmod.h>
18#include <linux/list.h>
19#include <linux/module.h>
20#include <linux/ctype.h>
Andy Shevchenko1b2e1222014-11-28 17:50:28 +020021#include <linux/string_helpers.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/poll.h>
24#include <linux/seq_file.h>
25#include <linux/proc_fs.h>
26#include <linux/net.h>
27#include <linux/workqueue.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080028#include <linux/mutex.h>
Trond Myklebustda770052009-08-09 15:14:28 -040029#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/ioctls.h>
31#include <linux/sunrpc/types.h>
32#include <linux/sunrpc/cache.h>
33#include <linux/sunrpc/stats.h>
Trond Myklebust8854e822009-08-09 15:14:30 -040034#include <linux/sunrpc/rpc_pipe_fs.h>
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +040035#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#define RPCDBG_FACILITY RPCDBG_CACHE
38
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -050039static bool cache_defer_req(struct cache_req *req, struct cache_head *item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static void cache_revisit_request(struct cache_head *item);
41
Neil Brown77862032015-10-16 08:59:08 +110042static void cache_init(struct cache_head *h, struct cache_detail *detail)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Arnd Bergmannf5599352017-10-20 16:34:42 +020044 time64_t now = seconds_since_boot();
Kinglong Mee129e5822015-07-27 11:10:15 +080045 INIT_HLIST_NODE(&h->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 h->flags = 0;
NeilBrownbaab9352006-03-27 01:15:09 -080047 kref_init(&h->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 h->expiry_time = now + CACHE_NEW_EXPIRY;
Neil Brown77862032015-10-16 08:59:08 +110049 if (now <= detail->flush_time)
50 /* ensure it isn't already expired */
51 now = detail->flush_time + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 h->last_refresh = now;
53}
54
Vasily Averin4ecd55e2018-11-28 11:45:57 +030055static void cache_fresh_unlocked(struct cache_head *head,
56 struct cache_detail *detail);
57
Trond Myklebustae741362018-10-03 12:01:22 -040058static struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail,
59 struct cache_head *key,
60 int hash)
61{
62 struct hlist_head *head = &detail->hash_table[hash];
63 struct cache_head *tmp;
64
65 rcu_read_lock();
66 hlist_for_each_entry_rcu(tmp, head, cache_list) {
Trond Myklebust277f27e22020-03-01 18:21:43 -050067 if (!detail->match(tmp, key))
68 continue;
69 if (test_bit(CACHE_VALID, &tmp->flags) &&
70 cache_is_expired(detail, tmp))
71 continue;
72 tmp = cache_get_rcu(tmp);
73 rcu_read_unlock();
74 return tmp;
Trond Myklebustae741362018-10-03 12:01:22 -040075 }
76 rcu_read_unlock();
77 return NULL;
78}
79
Trond Myklebust809fe3c52020-01-06 13:40:35 -050080static void sunrpc_begin_cache_remove_entry(struct cache_head *ch,
81 struct cache_detail *cd)
82{
83 /* Must be called under cd->hash_lock */
84 hlist_del_init_rcu(&ch->cache_list);
85 set_bit(CACHE_CLEANED, &ch->flags);
86 cd->entries --;
87}
88
89static void sunrpc_end_cache_remove_entry(struct cache_head *ch,
90 struct cache_detail *cd)
91{
92 cache_fresh_unlocked(ch, cd);
93 cache_put(ch, cd);
94}
95
Trond Myklebustb92a8fa2018-10-01 10:41:45 -040096static struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail,
97 struct cache_head *key,
98 int hash)
99{
100 struct cache_head *new, *tmp, *freeme = NULL;
101 struct hlist_head *head = &detail->hash_table[hash];
NeilBrown15a5f6b2006-03-27 01:15:02 -0800102
103 new = detail->alloc();
104 if (!new)
105 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -0700106 /* must fully initialise 'new', else
107 * we might get lose if we need to
108 * cache_put it soon.
109 */
Neil Brown77862032015-10-16 08:59:08 +1100110 cache_init(new, detail);
Neil Brown2f349312006-08-05 12:14:29 -0700111 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800112
Trond Myklebust1863d772018-10-01 10:41:52 -0400113 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800114
115 /* check if entry appeared while we slept */
Amol Grover51cae672020-02-19 15:05:05 +0530116 hlist_for_each_entry_rcu(tmp, head, cache_list,
117 lockdep_is_held(&detail->hash_lock)) {
Trond Myklebust277f27e22020-03-01 18:21:43 -0500118 if (!detail->match(tmp, key))
119 continue;
120 if (test_bit(CACHE_VALID, &tmp->flags) &&
121 cache_is_expired(detail, tmp)) {
122 sunrpc_begin_cache_remove_entry(tmp, detail);
123 freeme = tmp;
124 break;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800125 }
Trond Myklebust277f27e22020-03-01 18:21:43 -0500126 cache_get(tmp);
127 spin_unlock(&detail->hash_lock);
128 cache_put(new, detail);
129 return tmp;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800130 }
Kinglong Mee129e5822015-07-27 11:10:15 +0800131
Trond Myklebustae741362018-10-03 12:01:22 -0400132 hlist_add_head_rcu(&new->cache_list, head);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800133 detail->entries++;
134 cache_get(new);
Trond Myklebust1863d772018-10-01 10:41:52 -0400135 spin_unlock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800136
Trond Myklebust809fe3c52020-01-06 13:40:35 -0500137 if (freeme)
138 sunrpc_end_cache_remove_entry(freeme, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800139 return new;
140}
NeilBrown15a5f6b2006-03-27 01:15:02 -0800141
Trond Myklebustae741362018-10-03 12:01:22 -0400142struct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail,
143 struct cache_head *key, int hash)
144{
145 struct cache_head *ret;
146
147 ret = sunrpc_cache_find_rcu(detail, key, hash);
148 if (ret)
149 return ret;
150 /* Didn't find anything, insert an empty entry */
151 return sunrpc_cache_add_entry(detail, key, hash);
152}
153EXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu);
154
NeilBrownf866a812009-08-04 15:22:38 +1000155static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800156
Arnd Bergmannf5599352017-10-20 16:34:42 +0200157static void cache_fresh_locked(struct cache_head *head, time64_t expiry,
Neil Brown77862032015-10-16 08:59:08 +1100158 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800159{
Arnd Bergmannf5599352017-10-20 16:34:42 +0200160 time64_t now = seconds_since_boot();
Neil Brown77862032015-10-16 08:59:08 +1100161 if (now <= detail->flush_time)
162 /* ensure it isn't immediately treated as expired */
163 now = detail->flush_time + 1;
NeilBrownebd0cb12006-03-27 01:15:08 -0800164 head->expiry_time = expiry;
Neil Brown77862032015-10-16 08:59:08 +1100165 head->last_refresh = now;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500166 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
NeilBrown908329f2009-09-09 16:32:54 +1000167 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800168}
169
170static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000171 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800172{
NeilBrownebd0cb12006-03-27 01:15:08 -0800173 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
174 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000175 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800176 }
177}
178
NeilBrown15a5f6b2006-03-27 01:15:02 -0800179struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
180 struct cache_head *new, struct cache_head *old, int hash)
181{
182 /* The 'old' entry is to be replaced by 'new'.
183 * If 'old' is not VALID, we update it directly,
184 * otherwise we need to replace it
185 */
NeilBrown15a5f6b2006-03-27 01:15:02 -0800186 struct cache_head *tmp;
187
188 if (!test_bit(CACHE_VALID, &old->flags)) {
Trond Myklebust1863d772018-10-01 10:41:52 -0400189 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800190 if (!test_bit(CACHE_VALID, &old->flags)) {
191 if (test_bit(CACHE_NEGATIVE, &new->flags))
192 set_bit(CACHE_NEGATIVE, &old->flags);
193 else
194 detail->update(old, new);
Neil Brown77862032015-10-16 08:59:08 +1100195 cache_fresh_locked(old, new->expiry_time, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400196 spin_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000197 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800198 return old;
199 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400200 spin_unlock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800201 }
202 /* We need to insert a new entry */
203 tmp = detail->alloc();
204 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800205 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800206 return NULL;
207 }
Neil Brown77862032015-10-16 08:59:08 +1100208 cache_init(tmp, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800209 detail->init(tmp, old);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800210
Trond Myklebust1863d772018-10-01 10:41:52 -0400211 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800212 if (test_bit(CACHE_NEGATIVE, &new->flags))
213 set_bit(CACHE_NEGATIVE, &tmp->flags);
214 else
215 detail->update(tmp, new);
Kinglong Mee129e5822015-07-27 11:10:15 +0800216 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
NeilBrownf2d39582006-05-22 22:35:25 -0700217 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800218 cache_get(tmp);
Neil Brown77862032015-10-16 08:59:08 +1100219 cache_fresh_locked(tmp, new->expiry_time, detail);
220 cache_fresh_locked(old, 0, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400221 spin_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000222 cache_fresh_unlocked(tmp, detail);
223 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800224 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800225 return tmp;
226}
Trond Myklebust24c37672008-12-23 16:30:12 -0500227EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
chaoting fanb6040f972013-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 fanb6040f972013-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;
Arnd Bergmannf5599352017-10-20 16:34:42 +0200285 time64_t refresh_age, age;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /* First decide return status as best we can */
chaoting fanb6040f972013-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)) {
Arnd Bergmannf5599352017-10-20 16:34:42 +0200299 dprintk("RPC: Want update, refage=%lld, age=%lld\n",
Chuck Lever46121cf2007-01-31 12:14:08 -0500300 refresh_age, age);
Trond Myklebust65286b82020-03-01 18:21:42 -0500301 switch (detail->cache_upcall(detail, h)) {
302 case -EINVAL:
NeilBrown9d693382019-03-22 13:16:56 +1100303 rv = try_to_negate_entry(detail, h);
Trond Myklebust65286b82020-03-01 18:21:42 -0500304 break;
305 case -EAGAIN:
306 cache_fresh_unlocked(h, detail);
307 break;
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
NeilBrown989a19b2009-08-04 15:22:38 +1000311 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500312 if (!cache_defer_req(rqstp, h)) {
313 /*
314 * Request was not deferred; handle it as best
315 * we can ourselves:
316 */
chaoting fanb6040f972013-03-28 22:19:45 +0800317 rv = cache_is_valid(h);
NeilBrown989a19b2009-08-04 15:22:38 +1000318 if (rv == -EAGAIN)
319 rv = -ETIMEDOUT;
320 }
321 }
NeilBrown4013ede2006-03-27 01:15:07 -0800322 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800323 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return rv;
325}
Trond Myklebust24c37672008-12-23 16:30:12 -0500326EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/*
329 * caches need to be periodically cleaned.
330 * For this we maintain a list of cache_detail and
331 * a current pointer into that list and into the table
332 * for that entry.
333 *
NeilBrown013920e2013-06-13 12:53:42 +1000334 * Each time cache_clean is called it finds the next non-empty entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * in the current table and walks the list in that entry
336 * looking for entries that can be removed.
337 *
338 * An entry gets removed if:
339 * - The expiry is before current time
340 * - The last_refresh time is before the flush_time for that cache
341 *
342 * later we might drop old entries with non-NEVER expiry if that table
343 * is getting 'full' for some definition of 'full'
344 *
345 * The question of "how often to scan a table" is an interesting one
346 * and is answered in part by the use of the "nextcheck" field in the
347 * cache_detail.
348 * When a scan of a table begins, the nextcheck field is set to a time
349 * that is well into the future.
350 * While scanning, if an expiry time is found that is earlier than the
351 * current nextcheck time, nextcheck is set to that expiry time.
352 * If the flush_time is ever set to a time earlier than the nextcheck
353 * time, the nextcheck time is then set to that flush_time.
354 *
355 * A table is then only scanned if the current time is at least
356 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800357 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 */
359
360static LIST_HEAD(cache_list);
361static DEFINE_SPINLOCK(cache_list_lock);
362static struct cache_detail *current_detail;
363static int current_index;
364
David Howells65f27f32006-11-22 14:55:48 +0000365static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300366static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300368void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500369{
Trond Myklebust1863d772018-10-01 10:41:52 -0400370 spin_lock_init(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 INIT_LIST_HEAD(&cd->queue);
372 spin_lock(&cache_list_lock);
373 cd->nextcheck = 0;
374 cd->entries = 0;
Dave Wysochanski64a38e82019-07-26 18:33:01 -0400375 atomic_set(&cd->writers, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 cd->last_close = 0;
377 cd->last_warn = -1;
378 list_add(&cd->others, &cache_list);
379 spin_unlock(&cache_list_lock);
380
381 /* start the cleaning process */
Ke Wang77b00bc2016-09-01 15:30:26 +0800382 queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300384EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300386void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 cache_purge(cd);
389 spin_lock(&cache_list_lock);
Trond Myklebust1863d772018-10-01 10:41:52 -0400390 spin_lock(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (current_detail == cd)
392 current_detail = NULL;
393 list_del_init(&cd->others);
Trond Myklebust1863d772018-10-01 10:41:52 -0400394 spin_unlock(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (list_empty(&cache_list)) {
397 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400398 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300401EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403/* clean cache tries to find something to clean
404 * and cleans it.
405 * It returns 1 if it cleaned something,
406 * 0 if it didn't find anything this time
407 * -1 if it fell off the end of the list.
408 */
409static int cache_clean(void)
410{
411 int rv = 0;
412 struct list_head *next;
413
414 spin_lock(&cache_list_lock);
415
416 /* find a suitable table if we don't already have one */
417 while (current_detail == NULL ||
418 current_index >= current_detail->hash_size) {
419 if (current_detail)
420 next = current_detail->others.next;
421 else
422 next = cache_list.next;
423 if (next == &cache_list) {
424 current_detail = NULL;
425 spin_unlock(&cache_list_lock);
426 return -1;
427 }
428 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000429 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 current_index = current_detail->hash_size;
431 else {
432 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000433 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 }
436
437 /* find a non-empty bucket in the table */
438 while (current_detail &&
439 current_index < current_detail->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +0800440 hlist_empty(&current_detail->hash_table[current_index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 current_index++;
442
443 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (current_detail && current_index < current_detail->hash_size) {
Kinglong Mee129e5822015-07-27 11:10:15 +0800446 struct cache_head *ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct cache_detail *d;
Kinglong Mee129e5822015-07-27 11:10:15 +0800448 struct hlist_head *head;
449 struct hlist_node *tmp;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800450
Trond Myklebust1863d772018-10-01 10:41:52 -0400451 spin_lock(&current_detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800454
Kinglong Mee129e5822015-07-27 11:10:15 +0800455 head = &current_detail->hash_table[current_index];
456 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (current_detail->nextcheck > ch->expiry_time)
458 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100459 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Trond Myklebust809fe3c52020-01-06 13:40:35 -0500462 sunrpc_begin_cache_remove_entry(ch, current_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100464 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
NeilBrown3af49742010-02-03 17:31:31 +1100466
Trond Myklebust1863d772018-10-01 10:41:52 -0400467 spin_unlock(&current_detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 d = current_detail;
469 if (!ch)
470 current_index ++;
471 spin_unlock(&cache_list_lock);
Trond Myklebust809fe3c52020-01-06 13:40:35 -0500472 if (ch)
473 sunrpc_end_cache_remove_entry(ch, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 } else
475 spin_unlock(&cache_list_lock);
476
477 return rv;
478}
479
480/*
481 * We want to regularly clean the cache, so we need to schedule some work ...
482 */
David Howells65f27f32006-11-22 14:55:48 +0000483static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 int delay = 5;
486 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700487 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (list_empty(&cache_list))
490 delay = 0;
491
492 if (delay)
Ke Wang77b00bc2016-09-01 15:30:26 +0800493 queue_delayed_work(system_power_efficient_wq,
494 &cache_cleaner, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800498/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800500 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * be fully cleaned
502 */
503void cache_flush(void)
504{
505 while (cache_clean() != -1)
506 cond_resched();
507 while (cache_clean() != -1)
508 cond_resched();
509}
Trond Myklebust24c37672008-12-23 16:30:12 -0500510EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512void cache_purge(struct cache_detail *detail)
513{
Kinglong Mee471a9302017-02-08 09:54:33 +0800514 struct cache_head *ch = NULL;
515 struct hlist_head *head = NULL;
516 struct hlist_node *tmp = NULL;
517 int i = 0;
518
Trond Myklebust1863d772018-10-01 10:41:52 -0400519 spin_lock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800520 if (!detail->entries) {
Trond Myklebust1863d772018-10-01 10:41:52 -0400521 spin_unlock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800522 return;
523 }
524
525 dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name);
526 for (i = 0; i < detail->hash_size; i++) {
527 head = &detail->hash_table[i];
528 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Trond Myklebust809fe3c52020-01-06 13:40:35 -0500529 sunrpc_begin_cache_remove_entry(ch, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400530 spin_unlock(&detail->hash_lock);
Trond Myklebust809fe3c52020-01-06 13:40:35 -0500531 sunrpc_end_cache_remove_entry(ch, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400532 spin_lock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800533 }
534 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400535 spin_unlock(&detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
Trond Myklebust24c37672008-12-23 16:30:12 -0500537EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539
540/*
541 * Deferral and Revisiting of Requests.
542 *
543 * If a cache lookup finds a pending entry, we
544 * need to defer the request and revisit it later.
545 * All deferred requests are stored in a hash table,
546 * indexed by "struct cache_head *".
547 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800548 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * deferred form, which must contain a
550 * 'struct cache_deferred_req'
551 * This cache_deferred_req contains a method to allow
552 * it to be revisited when cache info is available
553 */
554
555#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
556#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
557
558#define DFR_MAX 300 /* ??? */
559
560static DEFINE_SPINLOCK(cache_defer_lock);
561static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000562static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563static int cache_defer_cnt;
564
J. Bruce Fields6610f722010-08-26 13:19:52 -0400565static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
NeilBrown11174492010-08-12 17:04:08 +1000567 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100568 if (!list_empty(&dreq->recent)) {
569 list_del_init(&dreq->recent);
570 cache_defer_cnt--;
571 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400572}
573
574static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
575{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int hash = DFR_HASH(item);
577
NeilBrowne33534d2010-10-07 15:29:46 +1100578 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000579 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400580}
581
NeilBrowne33534d2010-10-07 15:29:46 +1100582static void setup_deferral(struct cache_deferred_req *dreq,
583 struct cache_head *item,
584 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 spin_lock(&cache_defer_lock);
590
J. Bruce Fields6610f722010-08-26 13:19:52 -0400591 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
NeilBrowne33534d2010-10-07 15:29:46 +1100593 if (count_me) {
594 cache_defer_cnt++;
595 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
NeilBrowne33534d2010-10-07 15:29:46 +1100597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 spin_unlock(&cache_defer_lock);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
J. Bruce Fields3211af12010-08-26 16:56:23 -0400602struct thread_deferred_req {
603 struct cache_deferred_req handle;
604 struct completion completion;
605};
606
607static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
608{
609 struct thread_deferred_req *dr =
610 container_of(dreq, struct thread_deferred_req, handle);
611 complete(&dr->completion);
612}
613
NeilBrownd29068c2010-10-07 15:29:46 +1100614static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400615{
616 struct thread_deferred_req sleeper;
617 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400618
619 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
620 dreq->revisit = cache_restart_thread;
621
NeilBrowne33534d2010-10-07 15:29:46 +1100622 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400623
NeilBrownd29068c2010-10-07 15:29:46 +1100624 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000625 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400626 &sleeper.completion, req->thread_wait) <= 0) {
627 /* The completion wasn't completed, so we need
628 * to clean up
629 */
630 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000631 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400632 __unhash_deferred_req(&sleeper.handle);
633 spin_unlock(&cache_defer_lock);
634 } else {
635 /* cache_revisit_request already removed
636 * this from the hash table, but hasn't
637 * called ->revisit yet. It will very soon
638 * and we need to wait for it.
639 */
640 spin_unlock(&cache_defer_lock);
641 wait_for_completion(&sleeper.completion);
642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400644}
645
NeilBrowne33534d2010-10-07 15:29:46 +1100646static void cache_limit_defers(void)
647{
648 /* Make sure we haven't exceed the limit of allowed deferred
649 * requests.
650 */
651 struct cache_deferred_req *discard = NULL;
652
653 if (cache_defer_cnt <= DFR_MAX)
654 return;
655
656 spin_lock(&cache_defer_lock);
657
658 /* Consider removing either the first or the last */
659 if (cache_defer_cnt > DFR_MAX) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500660 if (prandom_u32() & 1)
NeilBrowne33534d2010-10-07 15:29:46 +1100661 discard = list_entry(cache_defer_list.next,
662 struct cache_deferred_req, recent);
663 else
664 discard = list_entry(cache_defer_list.prev,
665 struct cache_deferred_req, recent);
666 __unhash_deferred_req(discard);
667 }
668 spin_unlock(&cache_defer_lock);
669 if (discard)
670 discard->revisit(discard, 1);
671}
672
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500673/* Return true if and only if a deferred request is queued. */
674static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400675{
676 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400677
J. Bruce Fields3211af12010-08-26 16:56:23 -0400678 if (req->thread_wait) {
NeilBrownd29068c2010-10-07 15:29:46 +1100679 cache_wait_req(req, item);
680 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500681 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400682 }
683 dreq = req->defer(req);
684 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500685 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100686 setup_deferral(dreq, item, 1);
NeilBrownd29068c2010-10-07 15:29:46 +1100687 if (!test_bit(CACHE_PENDING, &item->flags))
688 /* Bit could have been cleared before we managed to
689 * set up the deferral, so need to revisit just in case
690 */
691 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100692
693 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500694 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697static void cache_revisit_request(struct cache_head *item)
698{
699 struct cache_deferred_req *dreq;
700 struct list_head pending;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800701 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 int hash = DFR_HASH(item);
703
704 INIT_LIST_HEAD(&pending);
705 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800706
Sasha Levinb67bfe02013-02-27 17:06:00 -0800707 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000708 if (dreq->item == item) {
709 __unhash_deferred_req(dreq);
710 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
NeilBrown11174492010-08-12 17:04:08 +1000712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 spin_unlock(&cache_defer_lock);
714
715 while (!list_empty(&pending)) {
716 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
717 list_del_init(&dreq->recent);
718 dreq->revisit(dreq, 0);
719 }
720}
721
722void cache_clean_deferred(void *owner)
723{
724 struct cache_deferred_req *dreq, *tmp;
725 struct list_head pending;
726
727
728 INIT_LIST_HEAD(&pending);
729 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
732 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400733 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000734 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736 }
737 spin_unlock(&cache_defer_lock);
738
739 while (!list_empty(&pending)) {
740 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
741 list_del_init(&dreq->recent);
742 dreq->revisit(dreq, 1);
743 }
744}
745
746/*
747 * communicate with user-space
748 *
Kinglong Mee6489a8f2017-02-07 21:49:17 +0800749 * We have a magic /proc file - /proc/net/rpc/<cachename>/channel.
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500750 * On read, you get a full request, or block.
751 * On write, an update request is processed.
752 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800754 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500755 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * to the end and may wakeup and preceding readers.
757 * New readers are added to the head. If, on read, an item is found with
758 * CACHE_UPCALLING clear, we free it from the list.
759 *
760 */
761
762static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800763static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765struct cache_queue {
766 struct list_head list;
767 int reader; /* if 0, then request */
768};
769struct cache_request {
770 struct cache_queue q;
771 struct cache_head *item;
772 char * buf;
773 int len;
774 int readers;
775};
776struct cache_reader {
777 struct cache_queue q;
778 int offset; /* if non-0, we have a refcnt on next request */
779};
780
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300781static int cache_request(struct cache_detail *detail,
782 struct cache_request *crq)
783{
784 char *bp = crq->buf;
785 int len = PAGE_SIZE;
786
787 detail->cache_request(detail, crq->item, &bp, &len);
788 if (len < 0)
789 return -EAGAIN;
790 return PAGE_SIZE - len;
791}
792
Trond Myklebust173912a2009-08-09 15:14:29 -0400793static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
794 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 struct cache_reader *rp = filp->private_data;
797 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500798 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int err;
800
801 if (count == 0)
802 return 0;
803
Al Viro59551022016-01-22 15:40:57 -0500804 inode_lock(inode); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * readers on this file */
806 again:
807 spin_lock(&queue_lock);
808 /* need to find next request */
809 while (rp->q.list.next != &cd->queue &&
810 list_entry(rp->q.list.next, struct cache_queue, list)
811 ->reader) {
812 struct list_head *next = rp->q.list.next;
813 list_move(&rp->q.list, next);
814 }
815 if (rp->q.list.next == &cd->queue) {
816 spin_unlock(&queue_lock);
Al Viro59551022016-01-22 15:40:57 -0500817 inode_unlock(inode);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400818 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return 0;
820 }
821 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400822 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (rp->offset == 0)
824 rq->readers++;
825 spin_unlock(&queue_lock);
826
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300827 if (rq->len == 0) {
828 err = cache_request(cd, rq);
829 if (err < 0)
830 goto out;
831 rq->len = err;
832 }
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
835 err = -EAGAIN;
836 spin_lock(&queue_lock);
837 list_move(&rp->q.list, &rq->q.list);
838 spin_unlock(&queue_lock);
839 } else {
840 if (rp->offset + count > rq->len)
841 count = rq->len - rp->offset;
842 err = -EFAULT;
843 if (copy_to_user(buf, rq->buf + rp->offset, count))
844 goto out;
845 rp->offset += count;
846 if (rp->offset >= rq->len) {
847 rp->offset = 0;
848 spin_lock(&queue_lock);
849 list_move(&rp->q.list, &rq->q.list);
850 spin_unlock(&queue_lock);
851 }
852 err = 0;
853 }
854 out:
855 if (rp->offset == 0) {
856 /* need to release rq */
857 spin_lock(&queue_lock);
858 rq->readers--;
859 if (rq->readers == 0 &&
860 !test_bit(CACHE_PENDING, &rq->item->flags)) {
861 list_del(&rq->q.list);
862 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800863 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 kfree(rq->buf);
865 kfree(rq);
866 } else
867 spin_unlock(&queue_lock);
868 }
869 if (err == -EAGAIN)
870 goto again;
Al Viro59551022016-01-22 15:40:57 -0500871 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return err ? err : count;
873}
874
Trond Myklebustda770052009-08-09 15:14:28 -0400875static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
876 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Trond Myklebustda770052009-08-09 15:14:28 -0400878 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300880 if (count == 0)
881 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400882 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400884 kaddr[count] = '\0';
885 ret = cd->cache_parse(cd, kaddr, count);
886 if (!ret)
887 ret = count;
888 return ret;
889}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Trond Myklebustda770052009-08-09 15:14:28 -0400891static ssize_t cache_slow_downcall(const char __user *buf,
892 size_t count, struct cache_detail *cd)
893{
894 static char write_buf[8192]; /* protected by queue_io_mutex */
895 ssize_t ret = -EINVAL;
896
897 if (count >= sizeof(write_buf))
898 goto out;
899 mutex_lock(&queue_io_mutex);
900 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800901 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400902out:
903 return ret;
904}
905
906static ssize_t cache_downcall(struct address_space *mapping,
907 const char __user *buf,
908 size_t count, struct cache_detail *cd)
909{
910 struct page *page;
911 char *kaddr;
912 ssize_t ret = -ENOMEM;
913
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300914 if (count >= PAGE_SIZE)
Trond Myklebustda770052009-08-09 15:14:28 -0400915 goto out_slow;
916
917 page = find_or_create_page(mapping, 0, GFP_KERNEL);
918 if (!page)
919 goto out_slow;
920
921 kaddr = kmap(page);
922 ret = cache_do_downcall(kaddr, buf, count, cd);
923 kunmap(page);
924 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300925 put_page(page);
Trond Myklebustda770052009-08-09 15:14:28 -0400926 return ret;
927out_slow:
928 return cache_slow_downcall(buf, count, cd);
929}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Trond Myklebust173912a2009-08-09 15:14:29 -0400931static ssize_t cache_write(struct file *filp, const char __user *buf,
932 size_t count, loff_t *ppos,
933 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Trond Myklebustda770052009-08-09 15:14:28 -0400935 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500936 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400937 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Trond Myklebustda770052009-08-09 15:14:28 -0400939 if (!cd->cache_parse)
940 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Al Viro59551022016-01-22 15:40:57 -0500942 inode_lock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400943 ret = cache_downcall(mapping, buf, count, cd);
Al Viro59551022016-01-22 15:40:57 -0500944 inode_unlock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400945out:
946 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
950
Al Viroade994f2017-07-03 00:01:49 -0400951static __poll_t cache_poll(struct file *filp, poll_table *wait,
Trond Myklebust173912a2009-08-09 15:14:29 -0400952 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Al Viroade994f2017-07-03 00:01:49 -0400954 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 struct cache_reader *rp = filp->private_data;
956 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 poll_wait(filp, &queue_wait, wait);
959
960 /* alway allow write */
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800961 mask = EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (!rp)
964 return mask;
965
966 spin_lock(&queue_lock);
967
968 for (cq= &rp->q; &cq->list != &cd->queue;
969 cq = list_entry(cq->list.next, struct cache_queue, list))
970 if (!cq->reader) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800971 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 break;
973 }
974 spin_unlock(&queue_lock);
975 return mask;
976}
977
Trond Myklebust173912a2009-08-09 15:14:29 -0400978static int cache_ioctl(struct inode *ino, struct file *filp,
979 unsigned int cmd, unsigned long arg,
980 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 int len = 0;
983 struct cache_reader *rp = filp->private_data;
984 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (cmd != FIONREAD || !rp)
987 return -EINVAL;
988
989 spin_lock(&queue_lock);
990
991 /* only find the length remaining in current request,
992 * or the length of the next request
993 */
994 for (cq= &rp->q; &cq->list != &cd->queue;
995 cq = list_entry(cq->list.next, struct cache_queue, list))
996 if (!cq->reader) {
997 struct cache_request *cr =
998 container_of(cq, struct cache_request, q);
999 len = cr->len - rp->offset;
1000 break;
1001 }
1002 spin_unlock(&queue_lock);
1003
1004 return put_user(len, (int __user *)arg);
1005}
1006
Trond Myklebust173912a2009-08-09 15:14:29 -04001007static int cache_open(struct inode *inode, struct file *filp,
1008 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct cache_reader *rp = NULL;
1011
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001012 if (!cd || !try_module_get(cd->owner))
1013 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 nonseekable_open(inode, filp);
1015 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +04001017 if (!rp) {
1018 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +04001020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 rp->offset = 0;
1022 rp->q.reader = 1;
Dave Wysochanski64a38e82019-07-26 18:33:01 -04001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 spin_lock(&queue_lock);
1025 list_add(&rp->q.list, &cd->queue);
1026 spin_unlock(&queue_lock);
1027 }
Dave Wysochanski64a38e82019-07-26 18:33:01 -04001028 if (filp->f_mode & FMODE_WRITE)
1029 atomic_inc(&cd->writers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 filp->private_data = rp;
1031 return 0;
1032}
1033
Trond Myklebust173912a2009-08-09 15:14:29 -04001034static int cache_release(struct inode *inode, struct file *filp,
1035 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (rp) {
1040 spin_lock(&queue_lock);
1041 if (rp->offset) {
1042 struct cache_queue *cq;
1043 for (cq= &rp->q; &cq->list != &cd->queue;
1044 cq = list_entry(cq->list.next, struct cache_queue, list))
1045 if (!cq->reader) {
1046 container_of(cq, struct cache_request, q)
1047 ->readers--;
1048 break;
1049 }
1050 rp->offset = 0;
1051 }
1052 list_del(&rp->q.list);
1053 spin_unlock(&queue_lock);
1054
1055 filp->private_data = NULL;
1056 kfree(rp);
1057
Dave Wysochanski64a38e82019-07-26 18:33:01 -04001058 }
1059 if (filp->f_mode & FMODE_WRITE) {
1060 atomic_dec(&cd->writers);
NeilBrownc5b29f82010-08-12 16:55:22 +10001061 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001063 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return 0;
1065}
1066
1067
1068
NeilBrownf866a812009-08-04 15:22:38 +10001069static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001071 struct cache_queue *cq, *tmp;
1072 struct cache_request *cr;
1073 struct list_head dequeued;
1074
1075 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001077 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001079 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (cr->item != ch)
1081 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001082 if (test_bit(CACHE_PENDING, &ch->flags))
1083 /* Lost a race and it is pending again */
1084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001086 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001087 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001090 while (!list_empty(&dequeued)) {
1091 cr = list_entry(dequeued.next, struct cache_request, q.list);
1092 list_del(&cr->q.list);
1093 cache_put(cr->item, detail);
1094 kfree(cr->buf);
1095 kfree(cr);
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
1099/*
1100 * Support routines for text-based upcalls.
1101 * Fields are separated by spaces.
1102 * Fields are either mangled to quote space tab newline slosh with slosh
1103 * or a hexified with a leading \x
1104 * Record is terminated with newline.
1105 *
1106 */
1107
1108void qword_add(char **bpp, int *lp, char *str)
1109{
1110 char *bp = *bpp;
1111 int len = *lp;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001112 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (len < 0) return;
1115
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001116 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1117 if (ret >= len) {
1118 bp += len;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001119 len = -1;
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001120 } else {
1121 bp += ret;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001122 len -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 *bp++ = ' ';
1124 len--;
1125 }
1126 *bpp = bp;
1127 *lp = len;
1128}
Trond Myklebust24c37672008-12-23 16:30:12 -05001129EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1132{
1133 char *bp = *bpp;
1134 int len = *lp;
1135
1136 if (len < 0) return;
1137
1138 if (len > 2) {
1139 *bp++ = '\\';
1140 *bp++ = 'x';
1141 len -= 2;
1142 while (blen && len >= 2) {
Andy Shevchenko056785e2013-12-12 15:49:21 +02001143 bp = hex_byte_pack(bp, *buf++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 len -= 2;
1145 blen--;
1146 }
1147 }
1148 if (blen || len<1) len = -1;
1149 else {
1150 *bp++ = ' ';
1151 len--;
1152 }
1153 *bpp = bp;
1154 *lp = len;
1155}
Trond Myklebust24c37672008-12-23 16:30:12 -05001156EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158static void warn_no_listener(struct cache_detail *detail)
1159{
1160 if (detail->last_warn != detail->last_close) {
1161 detail->last_warn = detail->last_close;
1162 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001163 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165}
1166
J. Bruce Fields06497522010-09-19 22:55:06 -04001167static bool cache_listeners_exist(struct cache_detail *detail)
1168{
Dave Wysochanski64a38e82019-07-26 18:33:01 -04001169 if (atomic_read(&detail->writers))
J. Bruce Fields06497522010-09-19 22:55:06 -04001170 return true;
1171 if (detail->last_close == 0)
1172 /* This cache was never opened */
1173 return false;
1174 if (detail->last_close < seconds_since_boot() - 30)
1175 /*
1176 * We allow for the possibility that someone might
1177 * restart a userspace daemon without restarting the
1178 * server; but after 30 seconds, we give up.
1179 */
1180 return false;
1181 return true;
1182}
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001185 * register an upcall request to user-space and queue it up for read() by the
1186 * upcall daemon.
1187 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 * Each request is at most one page long.
1189 */
Trond Myklebust65286b82020-03-01 18:21:42 -05001190static int cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 char *buf;
1193 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001194 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
NeilBrown013920e2013-06-13 12:53:42 +10001196 if (test_bit(CACHE_CLEANED, &h->flags))
1197 /* Too late to make an upcall */
1198 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1201 if (!buf)
1202 return -EAGAIN;
1203
1204 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1205 if (!crq) {
1206 kfree(buf);
1207 return -EAGAIN;
1208 }
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 crq->q.reader = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001212 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 crq->readers = 0;
1214 spin_lock(&queue_lock);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001215 if (test_bit(CACHE_PENDING, &h->flags)) {
1216 crq->item = cache_get(h);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001217 list_add_tail(&crq->q.list, &detail->queue);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001218 } else
NeilBrownf9e1aed2013-06-13 12:53:42 +10001219 /* Lost a race, no longer PENDING, so don't enqueue */
1220 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 spin_unlock(&queue_lock);
1222 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001223 if (ret == -EAGAIN) {
1224 kfree(buf);
1225 kfree(crq);
1226 }
1227 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
Trond Myklebust65286b82020-03-01 18:21:42 -05001229
1230int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
1231{
1232 if (test_and_set_bit(CACHE_PENDING, &h->flags))
1233 return 0;
1234 return cache_pipe_upcall(detail, h);
1235}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001236EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Trond Myklebust65286b82020-03-01 18:21:42 -05001238int sunrpc_cache_pipe_upcall_timeout(struct cache_detail *detail,
1239 struct cache_head *h)
1240{
1241 if (!cache_listeners_exist(detail)) {
1242 warn_no_listener(detail);
1243 return -EINVAL;
1244 }
1245 return sunrpc_cache_pipe_upcall(detail, h);
1246}
1247EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall_timeout);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/*
1250 * parse a message from user-space and pass it
1251 * to an appropriate cache
1252 * Messages are, like requests, separated into fields by
1253 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1254 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001255 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 * reply cachename expiry key ... content....
1257 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001258 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261int qword_get(char **bpp, char *dest, int bufsize)
1262{
1263 /* return bytes copied, or -1 on error */
1264 char *bp = *bpp;
1265 int len = 0;
1266
1267 while (*bp == ' ') bp++;
1268
1269 if (bp[0] == '\\' && bp[1] == 'x') {
1270 /* HEX STRING */
1271 bp += 2;
Stefan Hajnoczib7052cd2016-02-18 18:55:54 +00001272 while (len < bufsize - 1) {
Andy Shevchenkoe7f483ea2010-09-21 09:40:25 +03001273 int h, l;
1274
1275 h = hex_to_bin(bp[0]);
1276 if (h < 0)
1277 break;
1278
1279 l = hex_to_bin(bp[1]);
1280 if (l < 0)
1281 break;
1282
1283 *dest++ = (h << 4) | l;
1284 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 len++;
1286 }
1287 } else {
1288 /* text with \nnn octal quoting */
1289 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1290 if (*bp == '\\' &&
1291 isodigit(bp[1]) && (bp[1] <= '3') &&
1292 isodigit(bp[2]) &&
1293 isodigit(bp[3])) {
1294 int byte = (*++bp -'0');
1295 bp++;
1296 byte = (byte << 3) | (*bp++ - '0');
1297 byte = (byte << 3) | (*bp++ - '0');
1298 *dest++ = byte;
1299 len++;
1300 } else {
1301 *dest++ = *bp++;
1302 len++;
1303 }
1304 }
1305 }
1306
1307 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1308 return -1;
1309 while (*bp == ' ') bp++;
1310 *bpp = bp;
1311 *dest = '\0';
1312 return len;
1313}
Trond Myklebust24c37672008-12-23 16:30:12 -05001314EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316
1317/*
Kinglong Mee6489a8f2017-02-07 21:49:17 +08001318 * support /proc/net/rpc/$CACHENAME/content
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 * as a seqfile.
1320 * We call ->cache_show passing NULL for the item to
1321 * get a header, then pass each real item in the cache
1322 */
1323
Trond Myklebustae741362018-10-03 12:01:22 -04001324static void *__cache_seq_start(struct seq_file *m, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001327 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct cache_head *ch;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001329 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (!n--)
1332 return SEQ_START_TOKEN;
1333 hash = n >> 32;
1334 entry = n & ((1LL<<32) - 1);
1335
Trond Myklebustae741362018-10-03 12:01:22 -04001336 hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (!entry--)
1338 return ch;
1339 n &= ~((1LL<<32) - 1);
1340 do {
1341 hash++;
1342 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001343 } while(hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001344 hlist_empty(&cd->hash_table[hash]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (hash >= cd->hash_size)
1346 return NULL;
1347 *pos = n+1;
Trond Myklebustae741362018-10-03 12:01:22 -04001348 return hlist_entry_safe(rcu_dereference_raw(
1349 hlist_first_rcu(&cd->hash_table[hash])),
Kinglong Mee129e5822015-07-27 11:10:15 +08001350 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
Trond Myklebustae741362018-10-03 12:01:22 -04001352
Trond Myklebustd48cf352018-10-01 10:41:51 -04001353static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 struct cache_head *ch = p;
1356 int hash = (*pos >> 32);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001357 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 if (p == SEQ_START_TOKEN)
1360 hash = 0;
Kinglong Mee129e5822015-07-27 11:10:15 +08001361 else if (ch->cache_list.next == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 hash++;
1363 *pos += 1LL<<32;
1364 } else {
1365 ++*pos;
Trond Myklebustae741362018-10-03 12:01:22 -04001366 return hlist_entry_safe(rcu_dereference_raw(
1367 hlist_next_rcu(&ch->cache_list)),
Kinglong Mee129e5822015-07-27 11:10:15 +08001368 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370 *pos &= ~((1LL<<32) - 1);
1371 while (hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001372 hlist_empty(&cd->hash_table[hash])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 hash++;
1374 *pos += 1LL<<32;
1375 }
1376 if (hash >= cd->hash_size)
1377 return NULL;
1378 ++*pos;
Trond Myklebustae741362018-10-03 12:01:22 -04001379 return hlist_entry_safe(rcu_dereference_raw(
1380 hlist_first_rcu(&cd->hash_table[hash])),
Kinglong Mee129e5822015-07-27 11:10:15 +08001381 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
Trond Myklebustae741362018-10-03 12:01:22 -04001384void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos)
1385 __acquires(RCU)
1386{
1387 rcu_read_lock();
1388 return __cache_seq_start(m, pos);
1389}
1390EXPORT_SYMBOL_GPL(cache_seq_start_rcu);
1391
1392void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos)
1393{
1394 return cache_seq_next(file, p, pos);
1395}
1396EXPORT_SYMBOL_GPL(cache_seq_next_rcu);
1397
1398void cache_seq_stop_rcu(struct seq_file *m, void *p)
1399 __releases(RCU)
1400{
1401 rcu_read_unlock();
1402}
1403EXPORT_SYMBOL_GPL(cache_seq_stop_rcu);
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405static int c_show(struct seq_file *m, void *p)
1406{
1407 struct cache_head *cp = p;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001408 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 if (p == SEQ_START_TOKEN)
1411 return cd->cache_show(m, cd, NULL);
1412
1413 ifdebug(CACHE)
Arnd Bergmannf5599352017-10-20 16:34:42 +02001414 seq_printf(m, "# expiry=%lld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001415 convert_to_wallclock(cp->expiry_time),
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001416 kref_read(&cp->ref), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 cache_get(cp);
1418 if (cache_check(cd, cp, NULL))
1419 /* cache_check does a cache_put on failure */
1420 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001421 else {
1422 if (cache_is_expired(cd, cp))
1423 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 return cd->cache_show(m, cd, cp);
1428}
1429
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001430static const struct seq_operations cache_content_op = {
Trond Myklebustd48cf352018-10-01 10:41:51 -04001431 .start = cache_seq_start_rcu,
1432 .next = cache_seq_next_rcu,
1433 .stop = cache_seq_stop_rcu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 .show = c_show,
1435};
1436
Trond Myklebust173912a2009-08-09 15:14:29 -04001437static int content_open(struct inode *inode, struct file *file,
1438 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001440 struct seq_file *seq;
1441 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001443 if (!cd || !try_module_get(cd->owner))
1444 return -EACCES;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001445
1446 err = seq_open(file, &cache_content_op);
1447 if (err) {
Li Zefana5990ea2010-03-11 14:08:10 -08001448 module_put(cd->owner);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001449 return err;
Li Zefana5990ea2010-03-11 14:08:10 -08001450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001452 seq = file->private_data;
1453 seq->private = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001457static int content_release(struct inode *inode, struct file *file,
1458 struct cache_detail *cd)
1459{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001460 int ret = seq_release(inode, file);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001461 module_put(cd->owner);
1462 return ret;
1463}
1464
1465static int open_flush(struct inode *inode, struct file *file,
1466 struct cache_detail *cd)
1467{
1468 if (!cd || !try_module_get(cd->owner))
1469 return -EACCES;
1470 return nonseekable_open(inode, file);
1471}
1472
1473static int release_flush(struct inode *inode, struct file *file,
1474 struct cache_detail *cd)
1475{
1476 module_put(cd->owner);
1477 return 0;
1478}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001481 size_t count, loff_t *ppos,
1482 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Sasha Levin212ba902012-07-17 00:01:26 +02001484 char tbuf[22];
Chuck Lever01b29692007-10-26 13:31:20 -04001485 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Arnd Bergmannf5599352017-10-20 16:34:42 +02001487 len = snprintf(tbuf, sizeof(tbuf), "%llu\n",
Kinglong Mee8ccc8692017-02-07 21:50:32 +08001488 convert_to_wallclock(cd->flush_time));
1489 return simple_read_from_buffer(buf, count, ppos, tbuf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Trond Myklebust173912a2009-08-09 15:14:29 -04001492static ssize_t write_flush(struct file *file, const char __user *buf,
1493 size_t count, loff_t *ppos,
1494 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 char tbuf[20];
NeilBrown3b68e6e2018-02-14 12:15:06 +11001497 char *ep;
Arnd Bergmannf5599352017-10-20 16:34:42 +02001498 time64_t now;
NeilBrownc5b29f82010-08-12 16:55:22 +10001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (*ppos || count > sizeof(tbuf)-1)
1501 return -EINVAL;
1502 if (copy_from_user(tbuf, buf, count))
1503 return -EFAULT;
1504 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001505 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (*ep && *ep != '\n')
1507 return -EINVAL;
NeilBrown3b68e6e2018-02-14 12:15:06 +11001508 /* Note that while we check that 'buf' holds a valid number,
1509 * we always ignore the value and just flush everything.
1510 * Making use of the number leads to races.
Neil Brown77862032015-10-16 08:59:08 +11001511 */
Neil Brown77862032015-10-16 08:59:08 +11001512
NeilBrown3b68e6e2018-02-14 12:15:06 +11001513 now = seconds_since_boot();
1514 /* Always flush everything, so behave like cache_purge()
1515 * Do this by advancing flush_time to the current time,
1516 * or by one second if it has already reached the current time.
1517 * Newly added cache entries will always have ->last_refresh greater
1518 * that ->flush_time, so they don't get flushed prematurely.
1519 */
1520
1521 if (cd->flush_time >= now)
1522 now = cd->flush_time + 1;
1523
1524 cd->flush_time = now;
1525 cd->nextcheck = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 cache_flush();
1527
Jeff Laytonf69d6d82019-08-18 14:18:44 -04001528 if (cd->flush)
1529 cd->flush();
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 *ppos += count;
1532 return count;
1533}
1534
Trond Myklebust173912a2009-08-09 15:14:29 -04001535static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1536 size_t count, loff_t *ppos)
1537{
Al Virod9dda782013-03-31 18:16:14 -04001538 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001539
1540 return cache_read(filp, buf, count, ppos, cd);
1541}
1542
1543static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1544 size_t count, loff_t *ppos)
1545{
Al Virod9dda782013-03-31 18:16:14 -04001546 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001547
1548 return cache_write(filp, buf, count, ppos, cd);
1549}
1550
Al Viroade994f2017-07-03 00:01:49 -04001551static __poll_t cache_poll_procfs(struct file *filp, poll_table *wait)
Trond Myklebust173912a2009-08-09 15:14:29 -04001552{
Al Virod9dda782013-03-31 18:16:14 -04001553 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001554
1555 return cache_poll(filp, wait, cd);
1556}
1557
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001558static long cache_ioctl_procfs(struct file *filp,
1559 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001560{
Al Viro496ad9a2013-01-23 17:07:38 -05001561 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001562 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001563
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001564 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001565}
1566
1567static int cache_open_procfs(struct inode *inode, struct file *filp)
1568{
Al Virod9dda782013-03-31 18:16:14 -04001569 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001570
1571 return cache_open(inode, filp, cd);
1572}
1573
1574static int cache_release_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 cache_release(inode, filp, cd);
1579}
1580
Alexey Dobriyan97a32532020-02-03 17:37:17 -08001581static const struct proc_ops cache_channel_proc_ops = {
1582 .proc_lseek = no_llseek,
1583 .proc_read = cache_read_procfs,
1584 .proc_write = cache_write_procfs,
1585 .proc_poll = cache_poll_procfs,
1586 .proc_ioctl = cache_ioctl_procfs, /* for FIONREAD */
1587 .proc_open = cache_open_procfs,
1588 .proc_release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589};
Trond Myklebust173912a2009-08-09 15:14:29 -04001590
1591static int content_open_procfs(struct inode *inode, struct file *filp)
1592{
Al Virod9dda782013-03-31 18:16:14 -04001593 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001594
1595 return content_open(inode, filp, cd);
1596}
1597
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001598static int content_release_procfs(struct inode *inode, struct file *filp)
1599{
Al Virod9dda782013-03-31 18:16:14 -04001600 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001601
1602 return content_release(inode, filp, cd);
1603}
1604
Alexey Dobriyan97a32532020-02-03 17:37:17 -08001605static const struct proc_ops content_proc_ops = {
1606 .proc_open = content_open_procfs,
1607 .proc_read = seq_read,
1608 .proc_lseek = seq_lseek,
1609 .proc_release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001610};
1611
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001612static int open_flush_procfs(struct inode *inode, struct file *filp)
1613{
Al Virod9dda782013-03-31 18:16:14 -04001614 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001615
1616 return open_flush(inode, filp, cd);
1617}
1618
1619static int release_flush_procfs(struct inode *inode, struct file *filp)
1620{
Al Virod9dda782013-03-31 18:16:14 -04001621 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001622
1623 return release_flush(inode, filp, cd);
1624}
1625
Trond Myklebust173912a2009-08-09 15:14:29 -04001626static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1627 size_t count, loff_t *ppos)
1628{
Al Virod9dda782013-03-31 18:16:14 -04001629 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001630
1631 return read_flush(filp, buf, count, ppos, cd);
1632}
1633
1634static ssize_t write_flush_procfs(struct file *filp,
1635 const char __user *buf,
1636 size_t count, loff_t *ppos)
1637{
Al Virod9dda782013-03-31 18:16:14 -04001638 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001639
1640 return write_flush(filp, buf, count, ppos, cd);
1641}
1642
Alexey Dobriyan97a32532020-02-03 17:37:17 -08001643static const struct proc_ops cache_flush_proc_ops = {
1644 .proc_open = open_flush_procfs,
1645 .proc_read = read_flush_procfs,
1646 .proc_write = write_flush_procfs,
1647 .proc_release = release_flush_procfs,
1648 .proc_lseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001649};
1650
Kinglong Mee863d7d9c2017-02-07 21:47:16 +08001651static void remove_cache_proc_entries(struct cache_detail *cd)
Trond Myklebust173912a2009-08-09 15:14:29 -04001652{
Kinglong Mee863d7d9c2017-02-07 21:47:16 +08001653 if (cd->procfs) {
1654 proc_remove(cd->procfs);
1655 cd->procfs = NULL;
1656 }
Trond Myklebust173912a2009-08-09 15:14:29 -04001657}
1658
1659#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001660static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001661{
1662 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001663 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001664
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001665 sn = net_generic(net, sunrpc_net_id);
Kinglong Mee863d7d9c2017-02-07 21:47:16 +08001666 cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc);
1667 if (cd->procfs == NULL)
Trond Myklebust173912a2009-08-09 15:14:29 -04001668 goto out_nomem;
Trond Myklebust173912a2009-08-09 15:14:29 -04001669
Joe Perchesd6444062018-03-23 15:54:38 -07001670 p = proc_create_data("flush", S_IFREG | 0600,
Alexey Dobriyan97a32532020-02-03 17:37:17 -08001671 cd->procfs, &cache_flush_proc_ops, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001672 if (p == NULL)
1673 goto out_nomem;
1674
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001675 if (cd->cache_request || cd->cache_parse) {
Joe Perchesd6444062018-03-23 15:54:38 -07001676 p = proc_create_data("channel", S_IFREG | 0600, cd->procfs,
Alexey Dobriyan97a32532020-02-03 17:37:17 -08001677 &cache_channel_proc_ops, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001678 if (p == NULL)
1679 goto out_nomem;
1680 }
1681 if (cd->cache_show) {
Joe Perchesd6444062018-03-23 15:54:38 -07001682 p = proc_create_data("content", S_IFREG | 0400, cd->procfs,
Alexey Dobriyan97a32532020-02-03 17:37:17 -08001683 &content_proc_ops, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001684 if (p == NULL)
1685 goto out_nomem;
1686 }
1687 return 0;
1688out_nomem:
Kinglong Mee863d7d9c2017-02-07 21:47:16 +08001689 remove_cache_proc_entries(cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001690 return -ENOMEM;
1691}
1692#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001693static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001694{
1695 return 0;
1696}
1697#endif
1698
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001699void __init cache_initialize(void)
1700{
Tejun Heo203b42f72012-08-21 13:18:23 -07001701 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001702}
1703
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001704int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001705{
1706 int ret;
1707
1708 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001709 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001710 if (ret)
1711 sunrpc_destroy_cache_detail(cd);
1712 return ret;
1713}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001714EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001715
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001716void cache_unregister_net(struct cache_detail *cd, struct net *net)
1717{
Kinglong Mee863d7d9c2017-02-07 21:47:16 +08001718 remove_cache_proc_entries(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001719 sunrpc_destroy_cache_detail(cd);
1720}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001721EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001722
Bhumika Goyald34971a2017-10-17 18:14:23 +02001723struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001724{
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001725 struct cache_detail *cd;
Kinglong Mee129e5822015-07-27 11:10:15 +08001726 int i;
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001727
1728 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1729 if (cd == NULL)
1730 return ERR_PTR(-ENOMEM);
1731
Kees Cook6396bb22018-06-12 14:03:40 -07001732 cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head),
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001733 GFP_KERNEL);
1734 if (cd->hash_table == NULL) {
1735 kfree(cd);
1736 return ERR_PTR(-ENOMEM);
1737 }
Kinglong Mee129e5822015-07-27 11:10:15 +08001738
1739 for (i = 0; i < cd->hash_size; i++)
1740 INIT_HLIST_HEAD(&cd->hash_table[i]);
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001741 cd->net = net;
1742 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001743}
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001744EXPORT_SYMBOL_GPL(cache_create_net);
1745
1746void cache_destroy_net(struct cache_detail *cd, struct net *net)
1747{
1748 kfree(cd->hash_table);
1749 kfree(cd);
1750}
1751EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001752
1753static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1754 size_t count, loff_t *ppos)
1755{
Al Viro496ad9a2013-01-23 17:07:38 -05001756 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001757
1758 return cache_read(filp, buf, count, ppos, cd);
1759}
1760
1761static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1762 size_t count, loff_t *ppos)
1763{
Al Viro496ad9a2013-01-23 17:07:38 -05001764 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001765
1766 return cache_write(filp, buf, count, ppos, cd);
1767}
1768
Al Viroade994f2017-07-03 00:01:49 -04001769static __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait)
Trond Myklebust8854e822009-08-09 15:14:30 -04001770{
Al Viro496ad9a2013-01-23 17:07:38 -05001771 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001772
1773 return cache_poll(filp, wait, cd);
1774}
1775
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001776static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001777 unsigned int cmd, unsigned long arg)
1778{
Al Viro496ad9a2013-01-23 17:07:38 -05001779 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001780 struct cache_detail *cd = RPC_I(inode)->private;
1781
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001782 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001783}
1784
1785static int cache_open_pipefs(struct inode *inode, struct file *filp)
1786{
1787 struct cache_detail *cd = RPC_I(inode)->private;
1788
1789 return cache_open(inode, filp, cd);
1790}
1791
1792static int cache_release_pipefs(struct inode *inode, struct file *filp)
1793{
1794 struct cache_detail *cd = RPC_I(inode)->private;
1795
1796 return cache_release(inode, filp, cd);
1797}
1798
1799const struct file_operations cache_file_operations_pipefs = {
1800 .owner = THIS_MODULE,
1801 .llseek = no_llseek,
1802 .read = cache_read_pipefs,
1803 .write = cache_write_pipefs,
1804 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001805 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001806 .open = cache_open_pipefs,
1807 .release = cache_release_pipefs,
1808};
1809
1810static int content_open_pipefs(struct inode *inode, struct file *filp)
1811{
1812 struct cache_detail *cd = RPC_I(inode)->private;
1813
1814 return content_open(inode, filp, cd);
1815}
1816
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001817static int content_release_pipefs(struct inode *inode, struct file *filp)
1818{
1819 struct cache_detail *cd = RPC_I(inode)->private;
1820
1821 return content_release(inode, filp, cd);
1822}
1823
Trond Myklebust8854e822009-08-09 15:14:30 -04001824const struct file_operations content_file_operations_pipefs = {
1825 .open = content_open_pipefs,
1826 .read = seq_read,
1827 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001828 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001829};
1830
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001831static int open_flush_pipefs(struct inode *inode, struct file *filp)
1832{
1833 struct cache_detail *cd = RPC_I(inode)->private;
1834
1835 return open_flush(inode, filp, cd);
1836}
1837
1838static int release_flush_pipefs(struct inode *inode, struct file *filp)
1839{
1840 struct cache_detail *cd = RPC_I(inode)->private;
1841
1842 return release_flush(inode, filp, cd);
1843}
1844
Trond Myklebust8854e822009-08-09 15:14:30 -04001845static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1846 size_t count, loff_t *ppos)
1847{
Al Viro496ad9a2013-01-23 17:07:38 -05001848 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001849
1850 return read_flush(filp, buf, count, ppos, cd);
1851}
1852
1853static ssize_t write_flush_pipefs(struct file *filp,
1854 const char __user *buf,
1855 size_t count, loff_t *ppos)
1856{
Al Viro496ad9a2013-01-23 17:07:38 -05001857 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001858
1859 return write_flush(filp, buf, count, ppos, cd);
1860}
1861
1862const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001863 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001864 .read = read_flush_pipefs,
1865 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001866 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001867 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001868};
1869
1870int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001871 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001872 struct cache_detail *cd)
1873{
Al Viroa95e6912013-07-14 16:43:54 +04001874 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1875 if (IS_ERR(dir))
1876 return PTR_ERR(dir);
Kinglong Mee863d7d9c2017-02-07 21:47:16 +08001877 cd->pipefs = dir;
Al Viroa95e6912013-07-14 16:43:54 +04001878 return 0;
Trond Myklebust8854e822009-08-09 15:14:30 -04001879}
1880EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1881
1882void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1883{
Kinglong Mee863d7d9c2017-02-07 21:47:16 +08001884 if (cd->pipefs) {
1885 rpc_remove_cache_dir(cd->pipefs);
1886 cd->pipefs = NULL;
1887 }
Trond Myklebust8854e822009-08-09 15:14:30 -04001888}
1889EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1890
Neil Brown2b477c02016-12-22 12:38:06 -05001891void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h)
1892{
Trond Myklebust1863d772018-10-01 10:41:52 -04001893 spin_lock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001894 if (!hlist_unhashed(&h->cache_list)){
Trond Myklebust809fe3c52020-01-06 13:40:35 -05001895 sunrpc_begin_cache_remove_entry(h, cd);
Trond Myklebust1863d772018-10-01 10:41:52 -04001896 spin_unlock(&cd->hash_lock);
Trond Myklebust809fe3c52020-01-06 13:40:35 -05001897 sunrpc_end_cache_remove_entry(h, cd);
Neil Brown2b477c02016-12-22 12:38:06 -05001898 } else
Trond Myklebust1863d772018-10-01 10:41:52 -04001899 spin_unlock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001900}
1901EXPORT_SYMBOL_GPL(sunrpc_cache_unhash);