blob: 6f1528f271eed6ea0b7343c1c81e67702cb31dd5 [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 <neilb@cse.unsw.edu.au>
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);
NeilBrown9d693382019-03-22 13:16:56 +110041static bool cache_listeners_exist(struct cache_detail *detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Neil Brown77862032015-10-16 08:59:08 +110043static void cache_init(struct cache_head *h, struct cache_detail *detail)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
NeilBrownc5b29f82010-08-12 16:55:22 +100045 time_t now = seconds_since_boot();
Kinglong Mee129e5822015-07-27 11:10:15 +080046 INIT_HLIST_NODE(&h->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 h->flags = 0;
NeilBrownbaab9352006-03-27 01:15:09 -080048 kref_init(&h->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 h->expiry_time = now + CACHE_NEW_EXPIRY;
Neil Brown77862032015-10-16 08:59:08 +110050 if (now <= detail->flush_time)
51 /* ensure it isn't already expired */
52 now = detail->flush_time + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 h->last_refresh = now;
54}
55
NeilBrownd58431ea2019-04-05 11:34:40 +110056static inline int cache_is_valid(struct cache_head *h);
Vasily Averin4ecd55e2018-11-28 11:45:57 +030057static void cache_fresh_locked(struct cache_head *head, time_t expiry,
58 struct cache_detail *detail);
59static void cache_fresh_unlocked(struct cache_head *head,
60 struct cache_detail *detail);
61
Trond Myklebustae741362018-10-03 12:01:22 -040062static struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail,
63 struct cache_head *key,
64 int hash)
65{
66 struct hlist_head *head = &detail->hash_table[hash];
67 struct cache_head *tmp;
68
69 rcu_read_lock();
70 hlist_for_each_entry_rcu(tmp, head, cache_list) {
71 if (detail->match(tmp, key)) {
72 if (cache_is_expired(detail, tmp))
73 continue;
74 tmp = cache_get_rcu(tmp);
75 rcu_read_unlock();
76 return tmp;
77 }
78 }
79 rcu_read_unlock();
80 return NULL;
81}
82
Trond Myklebustb92a8fa2018-10-01 10:41:45 -040083static struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail,
84 struct cache_head *key,
85 int hash)
86{
87 struct cache_head *new, *tmp, *freeme = NULL;
88 struct hlist_head *head = &detail->hash_table[hash];
NeilBrown15a5f6b2006-03-27 01:15:02 -080089
90 new = detail->alloc();
91 if (!new)
92 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070093 /* must fully initialise 'new', else
94 * we might get lose if we need to
95 * cache_put it soon.
96 */
Neil Brown77862032015-10-16 08:59:08 +110097 cache_init(new, detail);
Neil Brown2f349312006-08-05 12:14:29 -070098 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080099
Trond Myklebust1863d772018-10-01 10:41:52 -0400100 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800101
102 /* check if entry appeared while we slept */
Trond Myklebustae741362018-10-03 12:01:22 -0400103 hlist_for_each_entry_rcu(tmp, head, cache_list) {
NeilBrown15a5f6b2006-03-27 01:15:02 -0800104 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +1100105 if (cache_is_expired(detail, tmp)) {
Trond Myklebustae741362018-10-03 12:01:22 -0400106 hlist_del_init_rcu(&tmp->cache_list);
NeilBrownd202cce2010-02-03 17:31:31 +1100107 detail->entries --;
NeilBrownd58431ea2019-04-05 11:34:40 +1100108 if (cache_is_valid(tmp) == -EAGAIN)
109 set_bit(CACHE_NEGATIVE, &tmp->flags);
Vasily Averin4ecd55e2018-11-28 11:45:57 +0300110 cache_fresh_locked(tmp, 0, detail);
NeilBrownd202cce2010-02-03 17:31:31 +1100111 freeme = tmp;
112 break;
113 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800114 cache_get(tmp);
Trond Myklebust1863d772018-10-01 10:41:52 -0400115 spin_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800116 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800117 return tmp;
118 }
119 }
Kinglong Mee129e5822015-07-27 11:10:15 +0800120
Trond Myklebustae741362018-10-03 12:01:22 -0400121 hlist_add_head_rcu(&new->cache_list, head);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800122 detail->entries++;
123 cache_get(new);
Trond Myklebust1863d772018-10-01 10:41:52 -0400124 spin_unlock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800125
Vasily Averin4ecd55e2018-11-28 11:45:57 +0300126 if (freeme) {
127 cache_fresh_unlocked(freeme, detail);
NeilBrownd202cce2010-02-03 17:31:31 +1100128 cache_put(freeme, detail);
Vasily Averin4ecd55e2018-11-28 11:45:57 +0300129 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800130 return new;
131}
NeilBrown15a5f6b2006-03-27 01:15:02 -0800132
Trond Myklebustae741362018-10-03 12:01:22 -0400133struct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail,
134 struct cache_head *key, int hash)
135{
136 struct cache_head *ret;
137
138 ret = sunrpc_cache_find_rcu(detail, key, hash);
139 if (ret)
140 return ret;
141 /* Didn't find anything, insert an empty entry */
142 return sunrpc_cache_add_entry(detail, key, hash);
143}
144EXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu);
145
NeilBrownf866a812009-08-04 15:22:38 +1000146static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800147
Neil Brown77862032015-10-16 08:59:08 +1100148static void cache_fresh_locked(struct cache_head *head, time_t expiry,
149 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800150{
Neil Brown77862032015-10-16 08:59:08 +1100151 time_t now = seconds_since_boot();
152 if (now <= detail->flush_time)
153 /* ensure it isn't immediately treated as expired */
154 now = detail->flush_time + 1;
NeilBrownebd0cb12006-03-27 01:15:08 -0800155 head->expiry_time = expiry;
Neil Brown77862032015-10-16 08:59:08 +1100156 head->last_refresh = now;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500157 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
NeilBrown908329f2009-09-09 16:32:54 +1000158 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800159}
160
161static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000162 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800163{
NeilBrownebd0cb12006-03-27 01:15:08 -0800164 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
165 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000166 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800167 }
168}
169
NeilBrown15a5f6b2006-03-27 01:15:02 -0800170struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
171 struct cache_head *new, struct cache_head *old, int hash)
172{
173 /* The 'old' entry is to be replaced by 'new'.
174 * If 'old' is not VALID, we update it directly,
175 * otherwise we need to replace it
176 */
NeilBrown15a5f6b2006-03-27 01:15:02 -0800177 struct cache_head *tmp;
178
179 if (!test_bit(CACHE_VALID, &old->flags)) {
Trond Myklebust1863d772018-10-01 10:41:52 -0400180 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800181 if (!test_bit(CACHE_VALID, &old->flags)) {
182 if (test_bit(CACHE_NEGATIVE, &new->flags))
183 set_bit(CACHE_NEGATIVE, &old->flags);
184 else
185 detail->update(old, new);
Neil Brown77862032015-10-16 08:59:08 +1100186 cache_fresh_locked(old, new->expiry_time, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400187 spin_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000188 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800189 return old;
190 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400191 spin_unlock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800192 }
193 /* We need to insert a new entry */
194 tmp = detail->alloc();
195 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800196 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800197 return NULL;
198 }
Neil Brown77862032015-10-16 08:59:08 +1100199 cache_init(tmp, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800200 detail->init(tmp, old);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800201
Trond Myklebust1863d772018-10-01 10:41:52 -0400202 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800203 if (test_bit(CACHE_NEGATIVE, &new->flags))
204 set_bit(CACHE_NEGATIVE, &tmp->flags);
205 else
206 detail->update(tmp, new);
Kinglong Mee129e5822015-07-27 11:10:15 +0800207 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
NeilBrownf2d39582006-05-22 22:35:25 -0700208 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800209 cache_get(tmp);
Neil Brown77862032015-10-16 08:59:08 +1100210 cache_fresh_locked(tmp, new->expiry_time, detail);
211 cache_fresh_locked(old, 0, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400212 spin_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000213 cache_fresh_unlocked(tmp, detail);
214 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800215 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800216 return tmp;
217}
Trond Myklebust24c37672008-12-23 16:30:12 -0500218EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400220static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
221{
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +0300222 if (cd->cache_upcall)
223 return cd->cache_upcall(cd, h);
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +0300224 return sunrpc_cache_pipe_upcall(cd, h);
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400225}
NeilBrown989a19b2009-08-04 15:22:38 +1000226
chaoting fanb6040f92013-03-28 22:19:45 +0800227static inline int cache_is_valid(struct cache_head *h)
NeilBrown989a19b2009-08-04 15:22:38 +1000228{
NeilBrownd202cce2010-02-03 17:31:31 +1100229 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000230 return -EAGAIN;
231 else {
232 /* entry is valid */
233 if (test_bit(CACHE_NEGATIVE, &h->flags))
234 return -ENOENT;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500235 else {
236 /*
237 * In combination with write barrier in
238 * sunrpc_cache_update, ensures that anyone
239 * using the cache entry after this sees the
240 * updated contents:
241 */
242 smp_rmb();
NeilBrown989a19b2009-08-04 15:22:38 +1000243 return 0;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500244 }
NeilBrown989a19b2009-08-04 15:22:38 +1000245 }
246}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400247
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500248static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
249{
250 int rv;
251
Trond Myklebust1863d772018-10-01 10:41:52 -0400252 spin_lock(&detail->hash_lock);
chaoting fanb6040f92013-03-28 22:19:45 +0800253 rv = cache_is_valid(h);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000254 if (rv == -EAGAIN) {
255 set_bit(CACHE_NEGATIVE, &h->flags);
Neil Brown77862032015-10-16 08:59:08 +1100256 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
257 detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000258 rv = -ENOENT;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500259 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400260 spin_unlock(&detail->hash_lock);
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500261 cache_fresh_unlocked(h, detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000262 return rv;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/*
266 * This is the generic cache management routine for all
267 * the authentication caches.
268 * It checks the currency of a cache item and will (later)
269 * initiate an upcall to fill it if needed.
270 *
271 *
272 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000273 * -EAGAIN if upcall is pending and request has been queued
274 * -ETIMEDOUT if upcall failed or request could not be queue or
275 * upcall completed but item is still invalid (implying that
276 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * -ENOENT if cache entry was negative
278 */
279int cache_check(struct cache_detail *detail,
280 struct cache_head *h, struct cache_req *rqstp)
281{
282 int rv;
283 long refresh_age, age;
284
285 /* First decide return status as best we can */
chaoting fanb6040f92013-03-28 22:19:45 +0800286 rv = cache_is_valid(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /* now see if we want to start an upcall */
289 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000290 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (rqstp == NULL) {
293 if (rv == -EAGAIN)
294 rv = -ENOENT;
NeilBrown0bebc632013-06-13 12:53:42 +1000295 } else if (rv == -EAGAIN ||
296 (h->expiry_time != 0 && age > refresh_age/2)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500297 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
298 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
300 switch (cache_make_upcall(detail, h)) {
301 case -EINVAL:
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500302 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case -EAGAIN:
NeilBrown2a1c7f52013-06-13 12:53:42 +1000305 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 }
NeilBrown9d693382019-03-22 13:16:56 +1100308 } else if (!cache_listeners_exist(detail))
309 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
NeilBrown989a19b2009-08-04 15:22:38 +1000312 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500313 if (!cache_defer_req(rqstp, h)) {
314 /*
315 * Request was not deferred; handle it as best
316 * we can ourselves:
317 */
chaoting fanb6040f92013-03-28 22:19:45 +0800318 rv = cache_is_valid(h);
NeilBrown989a19b2009-08-04 15:22:38 +1000319 if (rv == -EAGAIN)
320 rv = -ETIMEDOUT;
321 }
322 }
NeilBrown4013ede2006-03-27 01:15:07 -0800323 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800324 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return rv;
326}
Trond Myklebust24c37672008-12-23 16:30:12 -0500327EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/*
330 * caches need to be periodically cleaned.
331 * For this we maintain a list of cache_detail and
332 * a current pointer into that list and into the table
333 * for that entry.
334 *
NeilBrown013920e2013-06-13 12:53:42 +1000335 * Each time cache_clean is called it finds the next non-empty entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * in the current table and walks the list in that entry
337 * looking for entries that can be removed.
338 *
339 * An entry gets removed if:
340 * - The expiry is before current time
341 * - The last_refresh time is before the flush_time for that cache
342 *
343 * later we might drop old entries with non-NEVER expiry if that table
344 * is getting 'full' for some definition of 'full'
345 *
346 * The question of "how often to scan a table" is an interesting one
347 * and is answered in part by the use of the "nextcheck" field in the
348 * cache_detail.
349 * When a scan of a table begins, the nextcheck field is set to a time
350 * that is well into the future.
351 * While scanning, if an expiry time is found that is earlier than the
352 * current nextcheck time, nextcheck is set to that expiry time.
353 * If the flush_time is ever set to a time earlier than the nextcheck
354 * time, the nextcheck time is then set to that flush_time.
355 *
356 * A table is then only scanned if the current time is at least
357 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
360
361static LIST_HEAD(cache_list);
362static DEFINE_SPINLOCK(cache_list_lock);
363static struct cache_detail *current_detail;
364static int current_index;
365
David Howells65f27f32006-11-22 14:55:48 +0000366static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300367static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300369void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500370{
Trond Myklebust1863d772018-10-01 10:41:52 -0400371 spin_lock_init(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 INIT_LIST_HEAD(&cd->queue);
373 spin_lock(&cache_list_lock);
374 cd->nextcheck = 0;
375 cd->entries = 0;
376 atomic_set(&cd->readers, 0);
377 cd->last_close = 0;
378 cd->last_warn = -1;
379 list_add(&cd->others, &cache_list);
380 spin_unlock(&cache_list_lock);
381
382 /* start the cleaning process */
Ke Wang77b00bc2016-09-01 15:30:26 +0800383 queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300385EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300387void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 cache_purge(cd);
390 spin_lock(&cache_list_lock);
Trond Myklebust1863d772018-10-01 10:41:52 -0400391 spin_lock(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (current_detail == cd)
393 current_detail = NULL;
394 list_del_init(&cd->others);
Trond Myklebust1863d772018-10-01 10:41:52 -0400395 spin_unlock(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (list_empty(&cache_list)) {
398 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400399 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300402EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404/* clean cache tries to find something to clean
405 * and cleans it.
406 * It returns 1 if it cleaned something,
407 * 0 if it didn't find anything this time
408 * -1 if it fell off the end of the list.
409 */
410static int cache_clean(void)
411{
412 int rv = 0;
413 struct list_head *next;
414
415 spin_lock(&cache_list_lock);
416
417 /* find a suitable table if we don't already have one */
418 while (current_detail == NULL ||
419 current_index >= current_detail->hash_size) {
420 if (current_detail)
421 next = current_detail->others.next;
422 else
423 next = cache_list.next;
424 if (next == &cache_list) {
425 current_detail = NULL;
426 spin_unlock(&cache_list_lock);
427 return -1;
428 }
429 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000430 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 current_index = current_detail->hash_size;
432 else {
433 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000434 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436 }
437
438 /* find a non-empty bucket in the table */
439 while (current_detail &&
440 current_index < current_detail->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +0800441 hlist_empty(&current_detail->hash_table[current_index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 current_index++;
443
444 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (current_detail && current_index < current_detail->hash_size) {
Kinglong Mee129e5822015-07-27 11:10:15 +0800447 struct cache_head *ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct cache_detail *d;
Kinglong Mee129e5822015-07-27 11:10:15 +0800449 struct hlist_head *head;
450 struct hlist_node *tmp;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800451
Trond Myklebust1863d772018-10-01 10:41:52 -0400452 spin_lock(&current_detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800455
Kinglong Mee129e5822015-07-27 11:10:15 +0800456 head = &current_detail->hash_table[current_index];
457 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (current_detail->nextcheck > ch->expiry_time)
459 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100460 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Trond Myklebustae741362018-10-03 12:01:22 -0400463 hlist_del_init_rcu(&ch->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 current_detail->entries--;
465 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
NeilBrown3af49742010-02-03 17:31:31 +1100468
Trond Myklebust1863d772018-10-01 10:41:52 -0400469 spin_unlock(&current_detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 d = current_detail;
471 if (!ch)
472 current_index ++;
473 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000474 if (ch) {
NeilBrown013920e2013-06-13 12:53:42 +1000475 set_bit(CACHE_CLEANED, &ch->flags);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000476 cache_fresh_unlocked(ch, d);
NeilBrownbaab9352006-03-27 01:15:09 -0800477 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 } else
480 spin_unlock(&cache_list_lock);
481
482 return rv;
483}
484
485/*
486 * We want to regularly clean the cache, so we need to schedule some work ...
487 */
David Howells65f27f32006-11-22 14:55:48 +0000488static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 int delay = 5;
491 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700492 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (list_empty(&cache_list))
495 delay = 0;
496
497 if (delay)
Ke Wang77b00bc2016-09-01 15:30:26 +0800498 queue_delayed_work(system_power_efficient_wq,
499 &cache_cleaner, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800503/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800505 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * be fully cleaned
507 */
508void cache_flush(void)
509{
510 while (cache_clean() != -1)
511 cond_resched();
512 while (cache_clean() != -1)
513 cond_resched();
514}
Trond Myklebust24c37672008-12-23 16:30:12 -0500515EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517void cache_purge(struct cache_detail *detail)
518{
Kinglong Mee471a9302017-02-08 09:54:33 +0800519 struct cache_head *ch = NULL;
520 struct hlist_head *head = NULL;
521 struct hlist_node *tmp = NULL;
522 int i = 0;
523
Trond Myklebust1863d772018-10-01 10:41:52 -0400524 spin_lock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800525 if (!detail->entries) {
Trond Myklebust1863d772018-10-01 10:41:52 -0400526 spin_unlock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800527 return;
528 }
529
530 dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name);
531 for (i = 0; i < detail->hash_size; i++) {
532 head = &detail->hash_table[i];
533 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Trond Myklebustae741362018-10-03 12:01:22 -0400534 hlist_del_init_rcu(&ch->cache_list);
Kinglong Mee471a9302017-02-08 09:54:33 +0800535 detail->entries--;
536
537 set_bit(CACHE_CLEANED, &ch->flags);
Trond Myklebust1863d772018-10-01 10:41:52 -0400538 spin_unlock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800539 cache_fresh_unlocked(ch, detail);
540 cache_put(ch, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400541 spin_lock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800542 }
543 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400544 spin_unlock(&detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
Trond Myklebust24c37672008-12-23 16:30:12 -0500546EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548
549/*
550 * Deferral and Revisiting of Requests.
551 *
552 * If a cache lookup finds a pending entry, we
553 * need to defer the request and revisit it later.
554 * All deferred requests are stored in a hash table,
555 * indexed by "struct cache_head *".
556 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800557 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * deferred form, which must contain a
559 * 'struct cache_deferred_req'
560 * This cache_deferred_req contains a method to allow
561 * it to be revisited when cache info is available
562 */
563
564#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
565#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
566
567#define DFR_MAX 300 /* ??? */
568
569static DEFINE_SPINLOCK(cache_defer_lock);
570static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000571static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572static int cache_defer_cnt;
573
J. Bruce Fields6610f722010-08-26 13:19:52 -0400574static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
NeilBrown11174492010-08-12 17:04:08 +1000576 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100577 if (!list_empty(&dreq->recent)) {
578 list_del_init(&dreq->recent);
579 cache_defer_cnt--;
580 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400581}
582
583static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int hash = DFR_HASH(item);
586
NeilBrowne33534d2010-10-07 15:29:46 +1100587 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000588 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400589}
590
NeilBrowne33534d2010-10-07 15:29:46 +1100591static void setup_deferral(struct cache_deferred_req *dreq,
592 struct cache_head *item,
593 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 spin_lock(&cache_defer_lock);
599
J. Bruce Fields6610f722010-08-26 13:19:52 -0400600 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
NeilBrowne33534d2010-10-07 15:29:46 +1100602 if (count_me) {
603 cache_defer_cnt++;
604 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
NeilBrowne33534d2010-10-07 15:29:46 +1100606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 spin_unlock(&cache_defer_lock);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
J. Bruce Fields3211af12010-08-26 16:56:23 -0400611struct thread_deferred_req {
612 struct cache_deferred_req handle;
613 struct completion completion;
614};
615
616static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
617{
618 struct thread_deferred_req *dr =
619 container_of(dreq, struct thread_deferred_req, handle);
620 complete(&dr->completion);
621}
622
NeilBrownd29068c2010-10-07 15:29:46 +1100623static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400624{
625 struct thread_deferred_req sleeper;
626 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400627
628 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
629 dreq->revisit = cache_restart_thread;
630
NeilBrowne33534d2010-10-07 15:29:46 +1100631 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400632
NeilBrownd29068c2010-10-07 15:29:46 +1100633 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000634 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400635 &sleeper.completion, req->thread_wait) <= 0) {
636 /* The completion wasn't completed, so we need
637 * to clean up
638 */
639 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000640 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400641 __unhash_deferred_req(&sleeper.handle);
642 spin_unlock(&cache_defer_lock);
643 } else {
644 /* cache_revisit_request already removed
645 * this from the hash table, but hasn't
646 * called ->revisit yet. It will very soon
647 * and we need to wait for it.
648 */
649 spin_unlock(&cache_defer_lock);
650 wait_for_completion(&sleeper.completion);
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400653}
654
NeilBrowne33534d2010-10-07 15:29:46 +1100655static void cache_limit_defers(void)
656{
657 /* Make sure we haven't exceed the limit of allowed deferred
658 * requests.
659 */
660 struct cache_deferred_req *discard = NULL;
661
662 if (cache_defer_cnt <= DFR_MAX)
663 return;
664
665 spin_lock(&cache_defer_lock);
666
667 /* Consider removing either the first or the last */
668 if (cache_defer_cnt > DFR_MAX) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500669 if (prandom_u32() & 1)
NeilBrowne33534d2010-10-07 15:29:46 +1100670 discard = list_entry(cache_defer_list.next,
671 struct cache_deferred_req, recent);
672 else
673 discard = list_entry(cache_defer_list.prev,
674 struct cache_deferred_req, recent);
675 __unhash_deferred_req(discard);
676 }
677 spin_unlock(&cache_defer_lock);
678 if (discard)
679 discard->revisit(discard, 1);
680}
681
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500682/* Return true if and only if a deferred request is queued. */
683static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400684{
685 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400686
J. Bruce Fields3211af12010-08-26 16:56:23 -0400687 if (req->thread_wait) {
NeilBrownd29068c2010-10-07 15:29:46 +1100688 cache_wait_req(req, item);
689 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500690 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400691 }
692 dreq = req->defer(req);
693 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500694 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100695 setup_deferral(dreq, item, 1);
NeilBrownd29068c2010-10-07 15:29:46 +1100696 if (!test_bit(CACHE_PENDING, &item->flags))
697 /* Bit could have been cleared before we managed to
698 * set up the deferral, so need to revisit just in case
699 */
700 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100701
702 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500703 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706static void cache_revisit_request(struct cache_head *item)
707{
708 struct cache_deferred_req *dreq;
709 struct list_head pending;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800710 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int hash = DFR_HASH(item);
712
713 INIT_LIST_HEAD(&pending);
714 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800715
Sasha Levinb67bfe02013-02-27 17:06:00 -0800716 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000717 if (dreq->item == item) {
718 __unhash_deferred_req(dreq);
719 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
NeilBrown11174492010-08-12 17:04:08 +1000721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 spin_unlock(&cache_defer_lock);
723
724 while (!list_empty(&pending)) {
725 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
726 list_del_init(&dreq->recent);
727 dreq->revisit(dreq, 0);
728 }
729}
730
731void cache_clean_deferred(void *owner)
732{
733 struct cache_deferred_req *dreq, *tmp;
734 struct list_head pending;
735
736
737 INIT_LIST_HEAD(&pending);
738 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
741 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400742 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000743 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 }
746 spin_unlock(&cache_defer_lock);
747
748 while (!list_empty(&pending)) {
749 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
750 list_del_init(&dreq->recent);
751 dreq->revisit(dreq, 1);
752 }
753}
754
755/*
756 * communicate with user-space
757 *
Kinglong Mee6489a8f2017-02-07 21:49:17 +0800758 * We have a magic /proc file - /proc/net/rpc/<cachename>/channel.
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500759 * On read, you get a full request, or block.
760 * On write, an update request is processed.
761 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800763 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500764 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * to the end and may wakeup and preceding readers.
766 * New readers are added to the head. If, on read, an item is found with
767 * CACHE_UPCALLING clear, we free it from the list.
768 *
769 */
770
771static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800772static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774struct cache_queue {
775 struct list_head list;
776 int reader; /* if 0, then request */
777};
778struct cache_request {
779 struct cache_queue q;
780 struct cache_head *item;
781 char * buf;
782 int len;
783 int readers;
784};
785struct cache_reader {
786 struct cache_queue q;
787 int offset; /* if non-0, we have a refcnt on next request */
788};
789
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300790static int cache_request(struct cache_detail *detail,
791 struct cache_request *crq)
792{
793 char *bp = crq->buf;
794 int len = PAGE_SIZE;
795
796 detail->cache_request(detail, crq->item, &bp, &len);
797 if (len < 0)
798 return -EAGAIN;
799 return PAGE_SIZE - len;
800}
801
Trond Myklebust173912a2009-08-09 15:14:29 -0400802static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
803 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct cache_reader *rp = filp->private_data;
806 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500807 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int err;
809
810 if (count == 0)
811 return 0;
812
Al Viro59551022016-01-22 15:40:57 -0500813 inode_lock(inode); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * readers on this file */
815 again:
816 spin_lock(&queue_lock);
817 /* need to find next request */
818 while (rp->q.list.next != &cd->queue &&
819 list_entry(rp->q.list.next, struct cache_queue, list)
820 ->reader) {
821 struct list_head *next = rp->q.list.next;
822 list_move(&rp->q.list, next);
823 }
824 if (rp->q.list.next == &cd->queue) {
825 spin_unlock(&queue_lock);
Al Viro59551022016-01-22 15:40:57 -0500826 inode_unlock(inode);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400827 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 0;
829 }
830 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400831 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (rp->offset == 0)
833 rq->readers++;
834 spin_unlock(&queue_lock);
835
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300836 if (rq->len == 0) {
837 err = cache_request(cd, rq);
838 if (err < 0)
839 goto out;
840 rq->len = err;
841 }
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
844 err = -EAGAIN;
845 spin_lock(&queue_lock);
846 list_move(&rp->q.list, &rq->q.list);
847 spin_unlock(&queue_lock);
848 } else {
849 if (rp->offset + count > rq->len)
850 count = rq->len - rp->offset;
851 err = -EFAULT;
852 if (copy_to_user(buf, rq->buf + rp->offset, count))
853 goto out;
854 rp->offset += count;
855 if (rp->offset >= rq->len) {
856 rp->offset = 0;
857 spin_lock(&queue_lock);
858 list_move(&rp->q.list, &rq->q.list);
859 spin_unlock(&queue_lock);
860 }
861 err = 0;
862 }
863 out:
864 if (rp->offset == 0) {
865 /* need to release rq */
866 spin_lock(&queue_lock);
867 rq->readers--;
868 if (rq->readers == 0 &&
869 !test_bit(CACHE_PENDING, &rq->item->flags)) {
870 list_del(&rq->q.list);
871 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800872 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 kfree(rq->buf);
874 kfree(rq);
875 } else
876 spin_unlock(&queue_lock);
877 }
878 if (err == -EAGAIN)
879 goto again;
Al Viro59551022016-01-22 15:40:57 -0500880 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return err ? err : count;
882}
883
Trond Myklebustda770052009-08-09 15:14:28 -0400884static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
885 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Trond Myklebustda770052009-08-09 15:14:28 -0400887 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300889 if (count == 0)
890 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400891 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400893 kaddr[count] = '\0';
894 ret = cd->cache_parse(cd, kaddr, count);
895 if (!ret)
896 ret = count;
897 return ret;
898}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Trond Myklebustda770052009-08-09 15:14:28 -0400900static ssize_t cache_slow_downcall(const char __user *buf,
901 size_t count, struct cache_detail *cd)
902{
903 static char write_buf[8192]; /* protected by queue_io_mutex */
904 ssize_t ret = -EINVAL;
905
906 if (count >= sizeof(write_buf))
907 goto out;
908 mutex_lock(&queue_io_mutex);
909 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800910 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400911out:
912 return ret;
913}
914
915static ssize_t cache_downcall(struct address_space *mapping,
916 const char __user *buf,
917 size_t count, struct cache_detail *cd)
918{
919 struct page *page;
920 char *kaddr;
921 ssize_t ret = -ENOMEM;
922
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300923 if (count >= PAGE_SIZE)
Trond Myklebustda770052009-08-09 15:14:28 -0400924 goto out_slow;
925
926 page = find_or_create_page(mapping, 0, GFP_KERNEL);
927 if (!page)
928 goto out_slow;
929
930 kaddr = kmap(page);
931 ret = cache_do_downcall(kaddr, buf, count, cd);
932 kunmap(page);
933 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300934 put_page(page);
Trond Myklebustda770052009-08-09 15:14:28 -0400935 return ret;
936out_slow:
937 return cache_slow_downcall(buf, count, cd);
938}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Trond Myklebust173912a2009-08-09 15:14:29 -0400940static ssize_t cache_write(struct file *filp, const char __user *buf,
941 size_t count, loff_t *ppos,
942 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Trond Myklebustda770052009-08-09 15:14:28 -0400944 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500945 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400946 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Trond Myklebustda770052009-08-09 15:14:28 -0400948 if (!cd->cache_parse)
949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Al Viro59551022016-01-22 15:40:57 -0500951 inode_lock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400952 ret = cache_downcall(mapping, buf, count, cd);
Al Viro59551022016-01-22 15:40:57 -0500953 inode_unlock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400954out:
955 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
959
Al Viroade994f2017-07-03 00:01:49 -0400960static __poll_t cache_poll(struct file *filp, poll_table *wait,
Trond Myklebust173912a2009-08-09 15:14:29 -0400961 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Al Viroade994f2017-07-03 00:01:49 -0400963 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 struct cache_reader *rp = filp->private_data;
965 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 poll_wait(filp, &queue_wait, wait);
968
969 /* alway allow write */
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800970 mask = EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 if (!rp)
973 return mask;
974
975 spin_lock(&queue_lock);
976
977 for (cq= &rp->q; &cq->list != &cd->queue;
978 cq = list_entry(cq->list.next, struct cache_queue, list))
979 if (!cq->reader) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800980 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
982 }
983 spin_unlock(&queue_lock);
984 return mask;
985}
986
Trond Myklebust173912a2009-08-09 15:14:29 -0400987static int cache_ioctl(struct inode *ino, struct file *filp,
988 unsigned int cmd, unsigned long arg,
989 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 int len = 0;
992 struct cache_reader *rp = filp->private_data;
993 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (cmd != FIONREAD || !rp)
996 return -EINVAL;
997
998 spin_lock(&queue_lock);
999
1000 /* only find the length remaining in current request,
1001 * or the length of the next request
1002 */
1003 for (cq= &rp->q; &cq->list != &cd->queue;
1004 cq = list_entry(cq->list.next, struct cache_queue, list))
1005 if (!cq->reader) {
1006 struct cache_request *cr =
1007 container_of(cq, struct cache_request, q);
1008 len = cr->len - rp->offset;
1009 break;
1010 }
1011 spin_unlock(&queue_lock);
1012
1013 return put_user(len, (int __user *)arg);
1014}
1015
Trond Myklebust173912a2009-08-09 15:14:29 -04001016static int cache_open(struct inode *inode, struct file *filp,
1017 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct cache_reader *rp = NULL;
1020
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001021 if (!cd || !try_module_get(cd->owner))
1022 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 nonseekable_open(inode, filp);
1024 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +04001026 if (!rp) {
1027 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +04001029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 rp->offset = 0;
1031 rp->q.reader = 1;
1032 atomic_inc(&cd->readers);
1033 spin_lock(&queue_lock);
1034 list_add(&rp->q.list, &cd->queue);
1035 spin_unlock(&queue_lock);
1036 }
1037 filp->private_data = rp;
1038 return 0;
1039}
1040
Trond Myklebust173912a2009-08-09 15:14:29 -04001041static int cache_release(struct inode *inode, struct file *filp,
1042 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 if (rp) {
1047 spin_lock(&queue_lock);
1048 if (rp->offset) {
1049 struct cache_queue *cq;
1050 for (cq= &rp->q; &cq->list != &cd->queue;
1051 cq = list_entry(cq->list.next, struct cache_queue, list))
1052 if (!cq->reader) {
1053 container_of(cq, struct cache_request, q)
1054 ->readers--;
1055 break;
1056 }
1057 rp->offset = 0;
1058 }
1059 list_del(&rp->q.list);
1060 spin_unlock(&queue_lock);
1061
1062 filp->private_data = NULL;
1063 kfree(rp);
1064
NeilBrownc5b29f82010-08-12 16:55:22 +10001065 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 atomic_dec(&cd->readers);
1067 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001068 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return 0;
1070}
1071
1072
1073
NeilBrownf866a812009-08-04 15:22:38 +10001074static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001076 struct cache_queue *cq, *tmp;
1077 struct cache_request *cr;
1078 struct list_head dequeued;
1079
1080 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001082 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001084 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (cr->item != ch)
1086 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001087 if (test_bit(CACHE_PENDING, &ch->flags))
1088 /* Lost a race and it is pending again */
1089 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001091 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001092 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001095 while (!list_empty(&dequeued)) {
1096 cr = list_entry(dequeued.next, struct cache_request, q.list);
1097 list_del(&cr->q.list);
1098 cache_put(cr->item, detail);
1099 kfree(cr->buf);
1100 kfree(cr);
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104/*
1105 * Support routines for text-based upcalls.
1106 * Fields are separated by spaces.
1107 * Fields are either mangled to quote space tab newline slosh with slosh
1108 * or a hexified with a leading \x
1109 * Record is terminated with newline.
1110 *
1111 */
1112
1113void qword_add(char **bpp, int *lp, char *str)
1114{
1115 char *bp = *bpp;
1116 int len = *lp;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001117 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (len < 0) return;
1120
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001121 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1122 if (ret >= len) {
1123 bp += len;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001124 len = -1;
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001125 } else {
1126 bp += ret;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001127 len -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 *bp++ = ' ';
1129 len--;
1130 }
1131 *bpp = bp;
1132 *lp = len;
1133}
Trond Myklebust24c37672008-12-23 16:30:12 -05001134EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1137{
1138 char *bp = *bpp;
1139 int len = *lp;
1140
1141 if (len < 0) return;
1142
1143 if (len > 2) {
1144 *bp++ = '\\';
1145 *bp++ = 'x';
1146 len -= 2;
1147 while (blen && len >= 2) {
Andy Shevchenko056785e2013-12-12 15:49:21 +02001148 bp = hex_byte_pack(bp, *buf++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 len -= 2;
1150 blen--;
1151 }
1152 }
1153 if (blen || len<1) len = -1;
1154 else {
1155 *bp++ = ' ';
1156 len--;
1157 }
1158 *bpp = bp;
1159 *lp = len;
1160}
Trond Myklebust24c37672008-12-23 16:30:12 -05001161EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163static void warn_no_listener(struct cache_detail *detail)
1164{
1165 if (detail->last_warn != detail->last_close) {
1166 detail->last_warn = detail->last_close;
1167 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001168 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170}
1171
J. Bruce Fields06497522010-09-19 22:55:06 -04001172static bool cache_listeners_exist(struct cache_detail *detail)
1173{
1174 if (atomic_read(&detail->readers))
1175 return true;
1176 if (detail->last_close == 0)
1177 /* This cache was never opened */
1178 return false;
1179 if (detail->last_close < seconds_since_boot() - 30)
1180 /*
1181 * We allow for the possibility that someone might
1182 * restart a userspace daemon without restarting the
1183 * server; but after 30 seconds, we give up.
1184 */
1185 return false;
1186 return true;
1187}
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001190 * register an upcall request to user-space and queue it up for read() by the
1191 * upcall daemon.
1192 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * Each request is at most one page long.
1194 */
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +03001195int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197
1198 char *buf;
1199 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001200 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001202 if (!detail->cache_request)
1203 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
J. Bruce Fields06497522010-09-19 22:55:06 -04001205 if (!cache_listeners_exist(detail)) {
1206 warn_no_listener(detail);
1207 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
NeilBrown013920e2013-06-13 12:53:42 +10001209 if (test_bit(CACHE_CLEANED, &h->flags))
1210 /* Too late to make an upcall */
1211 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1214 if (!buf)
1215 return -EAGAIN;
1216
1217 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1218 if (!crq) {
1219 kfree(buf);
1220 return -EAGAIN;
1221 }
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 crq->q.reader = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001225 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 crq->readers = 0;
1227 spin_lock(&queue_lock);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001228 if (test_bit(CACHE_PENDING, &h->flags)) {
1229 crq->item = cache_get(h);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001230 list_add_tail(&crq->q.list, &detail->queue);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001231 } else
NeilBrownf9e1aed2013-06-13 12:53:42 +10001232 /* Lost a race, no longer PENDING, so don't enqueue */
1233 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 spin_unlock(&queue_lock);
1235 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001236 if (ret == -EAGAIN) {
1237 kfree(buf);
1238 kfree(crq);
1239 }
1240 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001242EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244/*
1245 * parse a message from user-space and pass it
1246 * to an appropriate cache
1247 * Messages are, like requests, separated into fields by
1248 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1249 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001250 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 * reply cachename expiry key ... content....
1252 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001253 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256int qword_get(char **bpp, char *dest, int bufsize)
1257{
1258 /* return bytes copied, or -1 on error */
1259 char *bp = *bpp;
1260 int len = 0;
1261
1262 while (*bp == ' ') bp++;
1263
1264 if (bp[0] == '\\' && bp[1] == 'x') {
1265 /* HEX STRING */
1266 bp += 2;
Stefan Hajnoczib7052cd2016-02-18 18:55:54 +00001267 while (len < bufsize - 1) {
Andy Shevchenkoe7f483ea2010-09-21 09:40:25 +03001268 int h, l;
1269
1270 h = hex_to_bin(bp[0]);
1271 if (h < 0)
1272 break;
1273
1274 l = hex_to_bin(bp[1]);
1275 if (l < 0)
1276 break;
1277
1278 *dest++ = (h << 4) | l;
1279 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 len++;
1281 }
1282 } else {
1283 /* text with \nnn octal quoting */
1284 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1285 if (*bp == '\\' &&
1286 isodigit(bp[1]) && (bp[1] <= '3') &&
1287 isodigit(bp[2]) &&
1288 isodigit(bp[3])) {
1289 int byte = (*++bp -'0');
1290 bp++;
1291 byte = (byte << 3) | (*bp++ - '0');
1292 byte = (byte << 3) | (*bp++ - '0');
1293 *dest++ = byte;
1294 len++;
1295 } else {
1296 *dest++ = *bp++;
1297 len++;
1298 }
1299 }
1300 }
1301
1302 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1303 return -1;
1304 while (*bp == ' ') bp++;
1305 *bpp = bp;
1306 *dest = '\0';
1307 return len;
1308}
Trond Myklebust24c37672008-12-23 16:30:12 -05001309EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311
1312/*
Kinglong Mee6489a8f2017-02-07 21:49:17 +08001313 * support /proc/net/rpc/$CACHENAME/content
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * as a seqfile.
1315 * We call ->cache_show passing NULL for the item to
1316 * get a header, then pass each real item in the cache
1317 */
1318
Trond Myklebustae741362018-10-03 12:01:22 -04001319static void *__cache_seq_start(struct seq_file *m, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001322 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 struct cache_head *ch;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001324 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (!n--)
1327 return SEQ_START_TOKEN;
1328 hash = n >> 32;
1329 entry = n & ((1LL<<32) - 1);
1330
Trond Myklebustae741362018-10-03 12:01:22 -04001331 hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (!entry--)
1333 return ch;
1334 n &= ~((1LL<<32) - 1);
1335 do {
1336 hash++;
1337 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001338 } while(hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001339 hlist_empty(&cd->hash_table[hash]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (hash >= cd->hash_size)
1341 return NULL;
1342 *pos = n+1;
Trond Myklebustae741362018-10-03 12:01:22 -04001343 return hlist_entry_safe(rcu_dereference_raw(
1344 hlist_first_rcu(&cd->hash_table[hash])),
Kinglong Mee129e5822015-07-27 11:10:15 +08001345 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
Trond Myklebustae741362018-10-03 12:01:22 -04001347
Trond Myklebustd48cf352018-10-01 10:41:51 -04001348static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct cache_head *ch = p;
1351 int hash = (*pos >> 32);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001352 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 if (p == SEQ_START_TOKEN)
1355 hash = 0;
Kinglong Mee129e5822015-07-27 11:10:15 +08001356 else if (ch->cache_list.next == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 hash++;
1358 *pos += 1LL<<32;
1359 } else {
1360 ++*pos;
Trond Myklebustae741362018-10-03 12:01:22 -04001361 return hlist_entry_safe(rcu_dereference_raw(
1362 hlist_next_rcu(&ch->cache_list)),
Kinglong Mee129e5822015-07-27 11:10:15 +08001363 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 *pos &= ~((1LL<<32) - 1);
1366 while (hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001367 hlist_empty(&cd->hash_table[hash])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 hash++;
1369 *pos += 1LL<<32;
1370 }
1371 if (hash >= cd->hash_size)
1372 return NULL;
1373 ++*pos;
Trond Myklebustae741362018-10-03 12:01:22 -04001374 return hlist_entry_safe(rcu_dereference_raw(
1375 hlist_first_rcu(&cd->hash_table[hash])),
Kinglong Mee129e5822015-07-27 11:10:15 +08001376 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Trond Myklebustae741362018-10-03 12:01:22 -04001379void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos)
1380 __acquires(RCU)
1381{
1382 rcu_read_lock();
1383 return __cache_seq_start(m, pos);
1384}
1385EXPORT_SYMBOL_GPL(cache_seq_start_rcu);
1386
1387void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos)
1388{
1389 return cache_seq_next(file, p, pos);
1390}
1391EXPORT_SYMBOL_GPL(cache_seq_next_rcu);
1392
1393void cache_seq_stop_rcu(struct seq_file *m, void *p)
1394 __releases(RCU)
1395{
1396 rcu_read_unlock();
1397}
1398EXPORT_SYMBOL_GPL(cache_seq_stop_rcu);
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400static int c_show(struct seq_file *m, void *p)
1401{
1402 struct cache_head *cp = p;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001403 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 if (p == SEQ_START_TOKEN)
1406 return cd->cache_show(m, cd, NULL);
1407
1408 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001409 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001410 convert_to_wallclock(cp->expiry_time),
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001411 kref_read(&cp->ref), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 cache_get(cp);
1413 if (cache_check(cd, cp, NULL))
1414 /* cache_check does a cache_put on failure */
1415 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001416 else {
1417 if (cache_is_expired(cd, cp))
1418 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 return cd->cache_show(m, cd, cp);
1423}
1424
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001425static const struct seq_operations cache_content_op = {
Trond Myklebustd48cf352018-10-01 10:41:51 -04001426 .start = cache_seq_start_rcu,
1427 .next = cache_seq_next_rcu,
1428 .stop = cache_seq_stop_rcu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 .show = c_show,
1430};
1431
Trond Myklebust173912a2009-08-09 15:14:29 -04001432static int content_open(struct inode *inode, struct file *file,
1433 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001435 struct seq_file *seq;
1436 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001438 if (!cd || !try_module_get(cd->owner))
1439 return -EACCES;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001440
1441 err = seq_open(file, &cache_content_op);
1442 if (err) {
Li Zefana5990ea2010-03-11 14:08:10 -08001443 module_put(cd->owner);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001444 return err;
Li Zefana5990ea2010-03-11 14:08:10 -08001445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001447 seq = file->private_data;
1448 seq->private = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001452static int content_release(struct inode *inode, struct file *file,
1453 struct cache_detail *cd)
1454{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001455 int ret = seq_release(inode, file);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001456 module_put(cd->owner);
1457 return ret;
1458}
1459
1460static int open_flush(struct inode *inode, struct file *file,
1461 struct cache_detail *cd)
1462{
1463 if (!cd || !try_module_get(cd->owner))
1464 return -EACCES;
1465 return nonseekable_open(inode, file);
1466}
1467
1468static int release_flush(struct inode *inode, struct file *file,
1469 struct cache_detail *cd)
1470{
1471 module_put(cd->owner);
1472 return 0;
1473}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001476 size_t count, loff_t *ppos,
1477 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Sasha Levin212ba902012-07-17 00:01:26 +02001479 char tbuf[22];
Chuck Lever01b29692007-10-26 13:31:20 -04001480 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Kinglong Mee8ccc8692017-02-07 21:50:32 +08001482 len = snprintf(tbuf, sizeof(tbuf), "%lu\n",
1483 convert_to_wallclock(cd->flush_time));
1484 return simple_read_from_buffer(buf, count, ppos, tbuf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
Trond Myklebust173912a2009-08-09 15:14:29 -04001487static ssize_t write_flush(struct file *file, const char __user *buf,
1488 size_t count, loff_t *ppos,
1489 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 char tbuf[20];
NeilBrown3b68e6e2018-02-14 12:15:06 +11001492 char *ep;
1493 time_t now;
NeilBrownc5b29f82010-08-12 16:55:22 +10001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (*ppos || count > sizeof(tbuf)-1)
1496 return -EINVAL;
1497 if (copy_from_user(tbuf, buf, count))
1498 return -EFAULT;
1499 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001500 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (*ep && *ep != '\n')
1502 return -EINVAL;
NeilBrown3b68e6e2018-02-14 12:15:06 +11001503 /* Note that while we check that 'buf' holds a valid number,
1504 * we always ignore the value and just flush everything.
1505 * Making use of the number leads to races.
Neil Brown77862032015-10-16 08:59:08 +11001506 */
Neil Brown77862032015-10-16 08:59:08 +11001507
NeilBrown3b68e6e2018-02-14 12:15:06 +11001508 now = seconds_since_boot();
1509 /* Always flush everything, so behave like cache_purge()
1510 * Do this by advancing flush_time to the current time,
1511 * or by one second if it has already reached the current time.
1512 * Newly added cache entries will always have ->last_refresh greater
1513 * that ->flush_time, so they don't get flushed prematurely.
1514 */
1515
1516 if (cd->flush_time >= now)
1517 now = cd->flush_time + 1;
1518
1519 cd->flush_time = now;
1520 cd->nextcheck = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 cache_flush();
1522
1523 *ppos += count;
1524 return count;
1525}
1526
Trond Myklebust173912a2009-08-09 15:14:29 -04001527static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1528 size_t count, loff_t *ppos)
1529{
Al Virod9dda782013-03-31 18:16:14 -04001530 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001531
1532 return cache_read(filp, buf, count, ppos, cd);
1533}
1534
1535static ssize_t cache_write_procfs(struct file *filp, const 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_write(filp, buf, count, ppos, cd);
1541}
1542
Al Viroade994f2017-07-03 00:01:49 -04001543static __poll_t cache_poll_procfs(struct file *filp, poll_table *wait)
Trond Myklebust173912a2009-08-09 15:14:29 -04001544{
Al Virod9dda782013-03-31 18:16:14 -04001545 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001546
1547 return cache_poll(filp, wait, cd);
1548}
1549
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001550static long cache_ioctl_procfs(struct file *filp,
1551 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001552{
Al Viro496ad9a2013-01-23 17:07:38 -05001553 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001554 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001555
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001556 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001557}
1558
1559static int cache_open_procfs(struct inode *inode, struct file *filp)
1560{
Al Virod9dda782013-03-31 18:16:14 -04001561 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001562
1563 return cache_open(inode, filp, cd);
1564}
1565
1566static int cache_release_procfs(struct inode *inode, struct file *filp)
1567{
Al Virod9dda782013-03-31 18:16:14 -04001568 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001569
1570 return cache_release(inode, filp, cd);
1571}
1572
1573static const struct file_operations cache_file_operations_procfs = {
1574 .owner = THIS_MODULE,
1575 .llseek = no_llseek,
1576 .read = cache_read_procfs,
1577 .write = cache_write_procfs,
1578 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001579 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001580 .open = cache_open_procfs,
1581 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582};
Trond Myklebust173912a2009-08-09 15:14:29 -04001583
1584static int content_open_procfs(struct inode *inode, struct file *filp)
1585{
Al Virod9dda782013-03-31 18:16:14 -04001586 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001587
1588 return content_open(inode, filp, cd);
1589}
1590
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001591static int content_release_procfs(struct inode *inode, struct file *filp)
1592{
Al Virod9dda782013-03-31 18:16:14 -04001593 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001594
1595 return content_release(inode, filp, cd);
1596}
1597
Trond Myklebust173912a2009-08-09 15:14:29 -04001598static const struct file_operations content_file_operations_procfs = {
1599 .open = content_open_procfs,
1600 .read = seq_read,
1601 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001602 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001603};
1604
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001605static int open_flush_procfs(struct inode *inode, struct file *filp)
1606{
Al Virod9dda782013-03-31 18:16:14 -04001607 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001608
1609 return open_flush(inode, filp, cd);
1610}
1611
1612static int release_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 release_flush(inode, filp, cd);
1617}
1618
Trond Myklebust173912a2009-08-09 15:14:29 -04001619static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1620 size_t count, loff_t *ppos)
1621{
Al Virod9dda782013-03-31 18:16:14 -04001622 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001623
1624 return read_flush(filp, buf, count, ppos, cd);
1625}
1626
1627static ssize_t write_flush_procfs(struct file *filp,
1628 const char __user *buf,
1629 size_t count, loff_t *ppos)
1630{
Al Virod9dda782013-03-31 18:16:14 -04001631 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001632
1633 return write_flush(filp, buf, count, ppos, cd);
1634}
1635
1636static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001637 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001638 .read = read_flush_procfs,
1639 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001640 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001641 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001642};
1643
Kinglong Mee863d7d92017-02-07 21:47:16 +08001644static void remove_cache_proc_entries(struct cache_detail *cd)
Trond Myklebust173912a2009-08-09 15:14:29 -04001645{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001646 if (cd->procfs) {
1647 proc_remove(cd->procfs);
1648 cd->procfs = NULL;
1649 }
Trond Myklebust173912a2009-08-09 15:14:29 -04001650}
1651
1652#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001653static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001654{
1655 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001656 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001657
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001658 sn = net_generic(net, sunrpc_net_id);
Kinglong Mee863d7d92017-02-07 21:47:16 +08001659 cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc);
1660 if (cd->procfs == NULL)
Trond Myklebust173912a2009-08-09 15:14:29 -04001661 goto out_nomem;
Trond Myklebust173912a2009-08-09 15:14:29 -04001662
Joe Perchesd6444062018-03-23 15:54:38 -07001663 p = proc_create_data("flush", S_IFREG | 0600,
Kinglong Mee863d7d92017-02-07 21:47:16 +08001664 cd->procfs, &cache_flush_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001665 if (p == NULL)
1666 goto out_nomem;
1667
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001668 if (cd->cache_request || cd->cache_parse) {
Joe Perchesd6444062018-03-23 15:54:38 -07001669 p = proc_create_data("channel", S_IFREG | 0600, cd->procfs,
1670 &cache_file_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001671 if (p == NULL)
1672 goto out_nomem;
1673 }
1674 if (cd->cache_show) {
Joe Perchesd6444062018-03-23 15:54:38 -07001675 p = proc_create_data("content", S_IFREG | 0400, cd->procfs,
1676 &content_file_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001677 if (p == NULL)
1678 goto out_nomem;
1679 }
1680 return 0;
1681out_nomem:
Kinglong Mee863d7d92017-02-07 21:47:16 +08001682 remove_cache_proc_entries(cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001683 return -ENOMEM;
1684}
1685#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001686static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001687{
1688 return 0;
1689}
1690#endif
1691
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001692void __init cache_initialize(void)
1693{
Tejun Heo203b42f2012-08-21 13:18:23 -07001694 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001695}
1696
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001697int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001698{
1699 int ret;
1700
1701 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001702 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001703 if (ret)
1704 sunrpc_destroy_cache_detail(cd);
1705 return ret;
1706}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001707EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001708
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001709void cache_unregister_net(struct cache_detail *cd, struct net *net)
1710{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001711 remove_cache_proc_entries(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001712 sunrpc_destroy_cache_detail(cd);
1713}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001714EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001715
Bhumika Goyald34971a2017-10-17 18:14:23 +02001716struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001717{
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001718 struct cache_detail *cd;
Kinglong Mee129e5822015-07-27 11:10:15 +08001719 int i;
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001720
1721 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1722 if (cd == NULL)
1723 return ERR_PTR(-ENOMEM);
1724
Kees Cook6396bb22018-06-12 14:03:40 -07001725 cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head),
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001726 GFP_KERNEL);
1727 if (cd->hash_table == NULL) {
1728 kfree(cd);
1729 return ERR_PTR(-ENOMEM);
1730 }
Kinglong Mee129e5822015-07-27 11:10:15 +08001731
1732 for (i = 0; i < cd->hash_size; i++)
1733 INIT_HLIST_HEAD(&cd->hash_table[i]);
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001734 cd->net = net;
1735 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001736}
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001737EXPORT_SYMBOL_GPL(cache_create_net);
1738
1739void cache_destroy_net(struct cache_detail *cd, struct net *net)
1740{
1741 kfree(cd->hash_table);
1742 kfree(cd);
1743}
1744EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001745
1746static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1747 size_t count, loff_t *ppos)
1748{
Al Viro496ad9a2013-01-23 17:07:38 -05001749 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001750
1751 return cache_read(filp, buf, count, ppos, cd);
1752}
1753
1754static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1755 size_t count, loff_t *ppos)
1756{
Al Viro496ad9a2013-01-23 17:07:38 -05001757 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001758
1759 return cache_write(filp, buf, count, ppos, cd);
1760}
1761
Al Viroade994f2017-07-03 00:01:49 -04001762static __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait)
Trond Myklebust8854e822009-08-09 15:14:30 -04001763{
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_poll(filp, wait, cd);
1767}
1768
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001769static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001770 unsigned int cmd, unsigned long arg)
1771{
Al Viro496ad9a2013-01-23 17:07:38 -05001772 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001773 struct cache_detail *cd = RPC_I(inode)->private;
1774
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001775 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001776}
1777
1778static int cache_open_pipefs(struct inode *inode, struct file *filp)
1779{
1780 struct cache_detail *cd = RPC_I(inode)->private;
1781
1782 return cache_open(inode, filp, cd);
1783}
1784
1785static int cache_release_pipefs(struct inode *inode, struct file *filp)
1786{
1787 struct cache_detail *cd = RPC_I(inode)->private;
1788
1789 return cache_release(inode, filp, cd);
1790}
1791
1792const struct file_operations cache_file_operations_pipefs = {
1793 .owner = THIS_MODULE,
1794 .llseek = no_llseek,
1795 .read = cache_read_pipefs,
1796 .write = cache_write_pipefs,
1797 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001798 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001799 .open = cache_open_pipefs,
1800 .release = cache_release_pipefs,
1801};
1802
1803static int content_open_pipefs(struct inode *inode, struct file *filp)
1804{
1805 struct cache_detail *cd = RPC_I(inode)->private;
1806
1807 return content_open(inode, filp, cd);
1808}
1809
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001810static int content_release_pipefs(struct inode *inode, struct file *filp)
1811{
1812 struct cache_detail *cd = RPC_I(inode)->private;
1813
1814 return content_release(inode, filp, cd);
1815}
1816
Trond Myklebust8854e822009-08-09 15:14:30 -04001817const struct file_operations content_file_operations_pipefs = {
1818 .open = content_open_pipefs,
1819 .read = seq_read,
1820 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001821 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001822};
1823
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001824static int open_flush_pipefs(struct inode *inode, struct file *filp)
1825{
1826 struct cache_detail *cd = RPC_I(inode)->private;
1827
1828 return open_flush(inode, filp, cd);
1829}
1830
1831static int release_flush_pipefs(struct inode *inode, struct file *filp)
1832{
1833 struct cache_detail *cd = RPC_I(inode)->private;
1834
1835 return release_flush(inode, filp, cd);
1836}
1837
Trond Myklebust8854e822009-08-09 15:14:30 -04001838static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1839 size_t count, loff_t *ppos)
1840{
Al Viro496ad9a2013-01-23 17:07:38 -05001841 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001842
1843 return read_flush(filp, buf, count, ppos, cd);
1844}
1845
1846static ssize_t write_flush_pipefs(struct file *filp,
1847 const char __user *buf,
1848 size_t count, loff_t *ppos)
1849{
Al Viro496ad9a2013-01-23 17:07:38 -05001850 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001851
1852 return write_flush(filp, buf, count, ppos, cd);
1853}
1854
1855const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001856 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001857 .read = read_flush_pipefs,
1858 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001859 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001860 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001861};
1862
1863int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001864 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001865 struct cache_detail *cd)
1866{
Al Viroa95e6912013-07-14 16:43:54 +04001867 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1868 if (IS_ERR(dir))
1869 return PTR_ERR(dir);
Kinglong Mee863d7d92017-02-07 21:47:16 +08001870 cd->pipefs = dir;
Al Viroa95e6912013-07-14 16:43:54 +04001871 return 0;
Trond Myklebust8854e822009-08-09 15:14:30 -04001872}
1873EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1874
1875void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1876{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001877 if (cd->pipefs) {
1878 rpc_remove_cache_dir(cd->pipefs);
1879 cd->pipefs = NULL;
1880 }
Trond Myklebust8854e822009-08-09 15:14:30 -04001881}
1882EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1883
Neil Brown2b477c02016-12-22 12:38:06 -05001884void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h)
1885{
Trond Myklebust1863d772018-10-01 10:41:52 -04001886 spin_lock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001887 if (!hlist_unhashed(&h->cache_list)){
Trond Myklebustae741362018-10-03 12:01:22 -04001888 hlist_del_init_rcu(&h->cache_list);
Neil Brown2b477c02016-12-22 12:38:06 -05001889 cd->entries--;
Trond Myklebust1863d772018-10-01 10:41:52 -04001890 spin_unlock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001891 cache_put(h, cd);
1892 } else
Trond Myklebust1863d772018-10-01 10:41:52 -04001893 spin_unlock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001894}
1895EXPORT_SYMBOL_GPL(sunrpc_cache_unhash);