blob: d996bf872a7c9f6d5465756324ecaa2b8856e9b8 [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{
Arnd Bergmannf5599352017-10-20 16:34:42 +020045 time64_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
Vasily Averin4ecd55e2018-11-28 11:45:57 +030056static void cache_fresh_unlocked(struct cache_head *head,
57 struct cache_detail *detail);
58
Trond Myklebustae741362018-10-03 12:01:22 -040059static struct cache_head *sunrpc_cache_find_rcu(struct cache_detail *detail,
60 struct cache_head *key,
61 int hash)
62{
63 struct hlist_head *head = &detail->hash_table[hash];
64 struct cache_head *tmp;
65
66 rcu_read_lock();
67 hlist_for_each_entry_rcu(tmp, head, cache_list) {
68 if (detail->match(tmp, key)) {
69 if (cache_is_expired(detail, tmp))
70 continue;
71 tmp = cache_get_rcu(tmp);
72 rcu_read_unlock();
73 return tmp;
74 }
75 }
76 rcu_read_unlock();
77 return NULL;
78}
79
Trond Myklebustb92a8fa2018-10-01 10:41:45 -040080static struct cache_head *sunrpc_cache_add_entry(struct cache_detail *detail,
81 struct cache_head *key,
82 int hash)
83{
84 struct cache_head *new, *tmp, *freeme = NULL;
85 struct hlist_head *head = &detail->hash_table[hash];
NeilBrown15a5f6b2006-03-27 01:15:02 -080086
87 new = detail->alloc();
88 if (!new)
89 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070090 /* must fully initialise 'new', else
91 * we might get lose if we need to
92 * cache_put it soon.
93 */
Neil Brown77862032015-10-16 08:59:08 +110094 cache_init(new, detail);
Neil Brown2f349312006-08-05 12:14:29 -070095 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080096
Trond Myklebust1863d772018-10-01 10:41:52 -040097 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -080098
99 /* check if entry appeared while we slept */
Trond Myklebustae741362018-10-03 12:01:22 -0400100 hlist_for_each_entry_rcu(tmp, head, cache_list) {
NeilBrown15a5f6b2006-03-27 01:15:02 -0800101 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +1100102 if (cache_is_expired(detail, tmp)) {
Trond Myklebustae741362018-10-03 12:01:22 -0400103 hlist_del_init_rcu(&tmp->cache_list);
NeilBrownd202cce2010-02-03 17:31:31 +1100104 detail->entries --;
105 freeme = tmp;
106 break;
107 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800108 cache_get(tmp);
Trond Myklebust1863d772018-10-01 10:41:52 -0400109 spin_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800110 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800111 return tmp;
112 }
113 }
Kinglong Mee129e5822015-07-27 11:10:15 +0800114
Trond Myklebustae741362018-10-03 12:01:22 -0400115 hlist_add_head_rcu(&new->cache_list, head);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800116 detail->entries++;
117 cache_get(new);
Trond Myklebust1863d772018-10-01 10:41:52 -0400118 spin_unlock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800119
Vasily Averin4ecd55e2018-11-28 11:45:57 +0300120 if (freeme) {
121 cache_fresh_unlocked(freeme, detail);
NeilBrownd202cce2010-02-03 17:31:31 +1100122 cache_put(freeme, detail);
Vasily Averin4ecd55e2018-11-28 11:45:57 +0300123 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800124 return new;
125}
NeilBrown15a5f6b2006-03-27 01:15:02 -0800126
Trond Myklebustae741362018-10-03 12:01:22 -0400127struct cache_head *sunrpc_cache_lookup_rcu(struct cache_detail *detail,
128 struct cache_head *key, int hash)
129{
130 struct cache_head *ret;
131
132 ret = sunrpc_cache_find_rcu(detail, key, hash);
133 if (ret)
134 return ret;
135 /* Didn't find anything, insert an empty entry */
136 return sunrpc_cache_add_entry(detail, key, hash);
137}
138EXPORT_SYMBOL_GPL(sunrpc_cache_lookup_rcu);
139
NeilBrownf866a812009-08-04 15:22:38 +1000140static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800141
Arnd Bergmannf5599352017-10-20 16:34:42 +0200142static void cache_fresh_locked(struct cache_head *head, time64_t expiry,
Neil Brown77862032015-10-16 08:59:08 +1100143 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800144{
Arnd Bergmannf5599352017-10-20 16:34:42 +0200145 time64_t now = seconds_since_boot();
Neil Brown77862032015-10-16 08:59:08 +1100146 if (now <= detail->flush_time)
147 /* ensure it isn't immediately treated as expired */
148 now = detail->flush_time + 1;
NeilBrownebd0cb12006-03-27 01:15:08 -0800149 head->expiry_time = expiry;
Neil Brown77862032015-10-16 08:59:08 +1100150 head->last_refresh = now;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500151 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
NeilBrown908329f2009-09-09 16:32:54 +1000152 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800153}
154
155static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000156 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800157{
NeilBrownebd0cb12006-03-27 01:15:08 -0800158 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
159 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000160 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800161 }
162}
163
NeilBrown15a5f6b2006-03-27 01:15:02 -0800164struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
165 struct cache_head *new, struct cache_head *old, int hash)
166{
167 /* The 'old' entry is to be replaced by 'new'.
168 * If 'old' is not VALID, we update it directly,
169 * otherwise we need to replace it
170 */
NeilBrown15a5f6b2006-03-27 01:15:02 -0800171 struct cache_head *tmp;
172
173 if (!test_bit(CACHE_VALID, &old->flags)) {
Trond Myklebust1863d772018-10-01 10:41:52 -0400174 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800175 if (!test_bit(CACHE_VALID, &old->flags)) {
176 if (test_bit(CACHE_NEGATIVE, &new->flags))
177 set_bit(CACHE_NEGATIVE, &old->flags);
178 else
179 detail->update(old, new);
Neil Brown77862032015-10-16 08:59:08 +1100180 cache_fresh_locked(old, new->expiry_time, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400181 spin_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000182 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800183 return old;
184 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400185 spin_unlock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800186 }
187 /* We need to insert a new entry */
188 tmp = detail->alloc();
189 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800190 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800191 return NULL;
192 }
Neil Brown77862032015-10-16 08:59:08 +1100193 cache_init(tmp, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800194 detail->init(tmp, old);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800195
Trond Myklebust1863d772018-10-01 10:41:52 -0400196 spin_lock(&detail->hash_lock);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800197 if (test_bit(CACHE_NEGATIVE, &new->flags))
198 set_bit(CACHE_NEGATIVE, &tmp->flags);
199 else
200 detail->update(tmp, new);
Kinglong Mee129e5822015-07-27 11:10:15 +0800201 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
NeilBrownf2d39582006-05-22 22:35:25 -0700202 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800203 cache_get(tmp);
Neil Brown77862032015-10-16 08:59:08 +1100204 cache_fresh_locked(tmp, new->expiry_time, detail);
205 cache_fresh_locked(old, 0, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400206 spin_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000207 cache_fresh_unlocked(tmp, detail);
208 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800209 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800210 return tmp;
211}
Trond Myklebust24c37672008-12-23 16:30:12 -0500212EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400214static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
215{
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +0300216 if (cd->cache_upcall)
217 return cd->cache_upcall(cd, h);
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +0300218 return sunrpc_cache_pipe_upcall(cd, h);
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400219}
NeilBrown989a19b2009-08-04 15:22:38 +1000220
chaoting fanb6040f972013-03-28 22:19:45 +0800221static inline int cache_is_valid(struct cache_head *h)
NeilBrown989a19b2009-08-04 15:22:38 +1000222{
NeilBrownd202cce2010-02-03 17:31:31 +1100223 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000224 return -EAGAIN;
225 else {
226 /* entry is valid */
227 if (test_bit(CACHE_NEGATIVE, &h->flags))
228 return -ENOENT;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500229 else {
230 /*
231 * In combination with write barrier in
232 * sunrpc_cache_update, ensures that anyone
233 * using the cache entry after this sees the
234 * updated contents:
235 */
236 smp_rmb();
NeilBrown989a19b2009-08-04 15:22:38 +1000237 return 0;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500238 }
NeilBrown989a19b2009-08-04 15:22:38 +1000239 }
240}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400241
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500242static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
243{
244 int rv;
245
Trond Myklebust1863d772018-10-01 10:41:52 -0400246 spin_lock(&detail->hash_lock);
chaoting fanb6040f972013-03-28 22:19:45 +0800247 rv = cache_is_valid(h);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000248 if (rv == -EAGAIN) {
249 set_bit(CACHE_NEGATIVE, &h->flags);
Neil Brown77862032015-10-16 08:59:08 +1100250 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
251 detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000252 rv = -ENOENT;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500253 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400254 spin_unlock(&detail->hash_lock);
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500255 cache_fresh_unlocked(h, detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000256 return rv;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/*
260 * This is the generic cache management routine for all
261 * the authentication caches.
262 * It checks the currency of a cache item and will (later)
263 * initiate an upcall to fill it if needed.
264 *
265 *
266 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000267 * -EAGAIN if upcall is pending and request has been queued
268 * -ETIMEDOUT if upcall failed or request could not be queue or
269 * upcall completed but item is still invalid (implying that
270 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * -ENOENT if cache entry was negative
272 */
273int cache_check(struct cache_detail *detail,
274 struct cache_head *h, struct cache_req *rqstp)
275{
276 int rv;
Arnd Bergmannf5599352017-10-20 16:34:42 +0200277 time64_t refresh_age, age;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* First decide return status as best we can */
chaoting fanb6040f972013-03-28 22:19:45 +0800280 rv = cache_is_valid(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /* now see if we want to start an upcall */
283 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000284 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (rqstp == NULL) {
287 if (rv == -EAGAIN)
288 rv = -ENOENT;
NeilBrown0bebc632013-06-13 12:53:42 +1000289 } else if (rv == -EAGAIN ||
290 (h->expiry_time != 0 && age > refresh_age/2)) {
Arnd Bergmannf5599352017-10-20 16:34:42 +0200291 dprintk("RPC: Want update, refage=%lld, age=%lld\n",
Chuck Lever46121cf2007-01-31 12:14:08 -0500292 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
294 switch (cache_make_upcall(detail, h)) {
295 case -EINVAL:
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500296 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 case -EAGAIN:
NeilBrown2a1c7f52013-06-13 12:53:42 +1000299 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
301 }
NeilBrown9d693382019-03-22 13:16:56 +1100302 } else if (!cache_listeners_exist(detail))
303 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
NeilBrown989a19b2009-08-04 15:22:38 +1000306 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500307 if (!cache_defer_req(rqstp, h)) {
308 /*
309 * Request was not deferred; handle it as best
310 * we can ourselves:
311 */
chaoting fanb6040f972013-03-28 22:19:45 +0800312 rv = cache_is_valid(h);
NeilBrown989a19b2009-08-04 15:22:38 +1000313 if (rv == -EAGAIN)
314 rv = -ETIMEDOUT;
315 }
316 }
NeilBrown4013ede2006-03-27 01:15:07 -0800317 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800318 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return rv;
320}
Trond Myklebust24c37672008-12-23 16:30:12 -0500321EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/*
324 * caches need to be periodically cleaned.
325 * For this we maintain a list of cache_detail and
326 * a current pointer into that list and into the table
327 * for that entry.
328 *
NeilBrown013920e2013-06-13 12:53:42 +1000329 * Each time cache_clean is called it finds the next non-empty entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * in the current table and walks the list in that entry
331 * looking for entries that can be removed.
332 *
333 * An entry gets removed if:
334 * - The expiry is before current time
335 * - The last_refresh time is before the flush_time for that cache
336 *
337 * later we might drop old entries with non-NEVER expiry if that table
338 * is getting 'full' for some definition of 'full'
339 *
340 * The question of "how often to scan a table" is an interesting one
341 * and is answered in part by the use of the "nextcheck" field in the
342 * cache_detail.
343 * When a scan of a table begins, the nextcheck field is set to a time
344 * that is well into the future.
345 * While scanning, if an expiry time is found that is earlier than the
346 * current nextcheck time, nextcheck is set to that expiry time.
347 * If the flush_time is ever set to a time earlier than the nextcheck
348 * time, the nextcheck time is then set to that flush_time.
349 *
350 * A table is then only scanned if the current time is at least
351 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800352 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
354
355static LIST_HEAD(cache_list);
356static DEFINE_SPINLOCK(cache_list_lock);
357static struct cache_detail *current_detail;
358static int current_index;
359
David Howells65f27f32006-11-22 14:55:48 +0000360static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300361static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300363void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500364{
Trond Myklebust1863d772018-10-01 10:41:52 -0400365 spin_lock_init(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 INIT_LIST_HEAD(&cd->queue);
367 spin_lock(&cache_list_lock);
368 cd->nextcheck = 0;
369 cd->entries = 0;
Dave Wysochanski64a38e82019-07-26 18:33:01 -0400370 atomic_set(&cd->writers, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 cd->last_close = 0;
372 cd->last_warn = -1;
373 list_add(&cd->others, &cache_list);
374 spin_unlock(&cache_list_lock);
375
376 /* start the cleaning process */
Ke Wang77b00bc2016-09-01 15:30:26 +0800377 queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300379EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300381void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 cache_purge(cd);
384 spin_lock(&cache_list_lock);
Trond Myklebust1863d772018-10-01 10:41:52 -0400385 spin_lock(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (current_detail == cd)
387 current_detail = NULL;
388 list_del_init(&cd->others);
Trond Myklebust1863d772018-10-01 10:41:52 -0400389 spin_unlock(&cd->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (list_empty(&cache_list)) {
392 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400393 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300396EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398/* clean cache tries to find something to clean
399 * and cleans it.
400 * It returns 1 if it cleaned something,
401 * 0 if it didn't find anything this time
402 * -1 if it fell off the end of the list.
403 */
404static int cache_clean(void)
405{
406 int rv = 0;
407 struct list_head *next;
408
409 spin_lock(&cache_list_lock);
410
411 /* find a suitable table if we don't already have one */
412 while (current_detail == NULL ||
413 current_index >= current_detail->hash_size) {
414 if (current_detail)
415 next = current_detail->others.next;
416 else
417 next = cache_list.next;
418 if (next == &cache_list) {
419 current_detail = NULL;
420 spin_unlock(&cache_list_lock);
421 return -1;
422 }
423 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000424 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 current_index = current_detail->hash_size;
426 else {
427 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000428 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 }
431
432 /* find a non-empty bucket in the table */
433 while (current_detail &&
434 current_index < current_detail->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +0800435 hlist_empty(&current_detail->hash_table[current_index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 current_index++;
437
438 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (current_detail && current_index < current_detail->hash_size) {
Kinglong Mee129e5822015-07-27 11:10:15 +0800441 struct cache_head *ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct cache_detail *d;
Kinglong Mee129e5822015-07-27 11:10:15 +0800443 struct hlist_head *head;
444 struct hlist_node *tmp;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800445
Trond Myklebust1863d772018-10-01 10:41:52 -0400446 spin_lock(&current_detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800449
Kinglong Mee129e5822015-07-27 11:10:15 +0800450 head = &current_detail->hash_table[current_index];
451 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (current_detail->nextcheck > ch->expiry_time)
453 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100454 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Trond Myklebustae741362018-10-03 12:01:22 -0400457 hlist_del_init_rcu(&ch->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 current_detail->entries--;
459 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100460 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
NeilBrown3af49742010-02-03 17:31:31 +1100462
Trond Myklebust1863d772018-10-01 10:41:52 -0400463 spin_unlock(&current_detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 d = current_detail;
465 if (!ch)
466 current_index ++;
467 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000468 if (ch) {
NeilBrown013920e2013-06-13 12:53:42 +1000469 set_bit(CACHE_CLEANED, &ch->flags);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000470 cache_fresh_unlocked(ch, d);
NeilBrownbaab9352006-03-27 01:15:09 -0800471 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else
474 spin_unlock(&cache_list_lock);
475
476 return rv;
477}
478
479/*
480 * We want to regularly clean the cache, so we need to schedule some work ...
481 */
David Howells65f27f32006-11-22 14:55:48 +0000482static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 int delay = 5;
485 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700486 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (list_empty(&cache_list))
489 delay = 0;
490
491 if (delay)
Ke Wang77b00bc2016-09-01 15:30:26 +0800492 queue_delayed_work(system_power_efficient_wq,
493 &cache_cleaner, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800497/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800499 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * be fully cleaned
501 */
502void cache_flush(void)
503{
504 while (cache_clean() != -1)
505 cond_resched();
506 while (cache_clean() != -1)
507 cond_resched();
508}
Trond Myklebust24c37672008-12-23 16:30:12 -0500509EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511void cache_purge(struct cache_detail *detail)
512{
Kinglong Mee471a9302017-02-08 09:54:33 +0800513 struct cache_head *ch = NULL;
514 struct hlist_head *head = NULL;
515 struct hlist_node *tmp = NULL;
516 int i = 0;
517
Trond Myklebust1863d772018-10-01 10:41:52 -0400518 spin_lock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800519 if (!detail->entries) {
Trond Myklebust1863d772018-10-01 10:41:52 -0400520 spin_unlock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800521 return;
522 }
523
524 dprintk("RPC: %d entries in %s cache\n", detail->entries, detail->name);
525 for (i = 0; i < detail->hash_size; i++) {
526 head = &detail->hash_table[i];
527 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Trond Myklebustae741362018-10-03 12:01:22 -0400528 hlist_del_init_rcu(&ch->cache_list);
Kinglong Mee471a9302017-02-08 09:54:33 +0800529 detail->entries--;
530
531 set_bit(CACHE_CLEANED, &ch->flags);
Trond Myklebust1863d772018-10-01 10:41:52 -0400532 spin_unlock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800533 cache_fresh_unlocked(ch, detail);
534 cache_put(ch, detail);
Trond Myklebust1863d772018-10-01 10:41:52 -0400535 spin_lock(&detail->hash_lock);
Kinglong Mee471a9302017-02-08 09:54:33 +0800536 }
537 }
Trond Myklebust1863d772018-10-01 10:41:52 -0400538 spin_unlock(&detail->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
Trond Myklebust24c37672008-12-23 16:30:12 -0500540EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542
543/*
544 * Deferral and Revisiting of Requests.
545 *
546 * If a cache lookup finds a pending entry, we
547 * need to defer the request and revisit it later.
548 * All deferred requests are stored in a hash table,
549 * indexed by "struct cache_head *".
550 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800551 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * deferred form, which must contain a
553 * 'struct cache_deferred_req'
554 * This cache_deferred_req contains a method to allow
555 * it to be revisited when cache info is available
556 */
557
558#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
559#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
560
561#define DFR_MAX 300 /* ??? */
562
563static DEFINE_SPINLOCK(cache_defer_lock);
564static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000565static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566static int cache_defer_cnt;
567
J. Bruce Fields6610f722010-08-26 13:19:52 -0400568static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
NeilBrown11174492010-08-12 17:04:08 +1000570 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100571 if (!list_empty(&dreq->recent)) {
572 list_del_init(&dreq->recent);
573 cache_defer_cnt--;
574 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400575}
576
577static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
578{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int hash = DFR_HASH(item);
580
NeilBrowne33534d2010-10-07 15:29:46 +1100581 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000582 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400583}
584
NeilBrowne33534d2010-10-07 15:29:46 +1100585static void setup_deferral(struct cache_deferred_req *dreq,
586 struct cache_head *item,
587 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 spin_lock(&cache_defer_lock);
593
J. Bruce Fields6610f722010-08-26 13:19:52 -0400594 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
NeilBrowne33534d2010-10-07 15:29:46 +1100596 if (count_me) {
597 cache_defer_cnt++;
598 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
NeilBrowne33534d2010-10-07 15:29:46 +1100600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 spin_unlock(&cache_defer_lock);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
J. Bruce Fields3211af12010-08-26 16:56:23 -0400605struct thread_deferred_req {
606 struct cache_deferred_req handle;
607 struct completion completion;
608};
609
610static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
611{
612 struct thread_deferred_req *dr =
613 container_of(dreq, struct thread_deferred_req, handle);
614 complete(&dr->completion);
615}
616
NeilBrownd29068c2010-10-07 15:29:46 +1100617static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400618{
619 struct thread_deferred_req sleeper;
620 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400621
622 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
623 dreq->revisit = cache_restart_thread;
624
NeilBrowne33534d2010-10-07 15:29:46 +1100625 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400626
NeilBrownd29068c2010-10-07 15:29:46 +1100627 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000628 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400629 &sleeper.completion, req->thread_wait) <= 0) {
630 /* The completion wasn't completed, so we need
631 * to clean up
632 */
633 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000634 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400635 __unhash_deferred_req(&sleeper.handle);
636 spin_unlock(&cache_defer_lock);
637 } else {
638 /* cache_revisit_request already removed
639 * this from the hash table, but hasn't
640 * called ->revisit yet. It will very soon
641 * and we need to wait for it.
642 */
643 spin_unlock(&cache_defer_lock);
644 wait_for_completion(&sleeper.completion);
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400647}
648
NeilBrowne33534d2010-10-07 15:29:46 +1100649static void cache_limit_defers(void)
650{
651 /* Make sure we haven't exceed the limit of allowed deferred
652 * requests.
653 */
654 struct cache_deferred_req *discard = NULL;
655
656 if (cache_defer_cnt <= DFR_MAX)
657 return;
658
659 spin_lock(&cache_defer_lock);
660
661 /* Consider removing either the first or the last */
662 if (cache_defer_cnt > DFR_MAX) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500663 if (prandom_u32() & 1)
NeilBrowne33534d2010-10-07 15:29:46 +1100664 discard = list_entry(cache_defer_list.next,
665 struct cache_deferred_req, recent);
666 else
667 discard = list_entry(cache_defer_list.prev,
668 struct cache_deferred_req, recent);
669 __unhash_deferred_req(discard);
670 }
671 spin_unlock(&cache_defer_lock);
672 if (discard)
673 discard->revisit(discard, 1);
674}
675
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500676/* Return true if and only if a deferred request is queued. */
677static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400678{
679 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400680
J. Bruce Fields3211af12010-08-26 16:56:23 -0400681 if (req->thread_wait) {
NeilBrownd29068c2010-10-07 15:29:46 +1100682 cache_wait_req(req, item);
683 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500684 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400685 }
686 dreq = req->defer(req);
687 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500688 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100689 setup_deferral(dreq, item, 1);
NeilBrownd29068c2010-10-07 15:29:46 +1100690 if (!test_bit(CACHE_PENDING, &item->flags))
691 /* Bit could have been cleared before we managed to
692 * set up the deferral, so need to revisit just in case
693 */
694 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100695
696 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500697 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700static void cache_revisit_request(struct cache_head *item)
701{
702 struct cache_deferred_req *dreq;
703 struct list_head pending;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800704 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int hash = DFR_HASH(item);
706
707 INIT_LIST_HEAD(&pending);
708 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800709
Sasha Levinb67bfe02013-02-27 17:06:00 -0800710 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000711 if (dreq->item == item) {
712 __unhash_deferred_req(dreq);
713 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
NeilBrown11174492010-08-12 17:04:08 +1000715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 spin_unlock(&cache_defer_lock);
717
718 while (!list_empty(&pending)) {
719 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
720 list_del_init(&dreq->recent);
721 dreq->revisit(dreq, 0);
722 }
723}
724
725void cache_clean_deferred(void *owner)
726{
727 struct cache_deferred_req *dreq, *tmp;
728 struct list_head pending;
729
730
731 INIT_LIST_HEAD(&pending);
732 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
735 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400736 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000737 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 }
740 spin_unlock(&cache_defer_lock);
741
742 while (!list_empty(&pending)) {
743 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
744 list_del_init(&dreq->recent);
745 dreq->revisit(dreq, 1);
746 }
747}
748
749/*
750 * communicate with user-space
751 *
Kinglong Mee6489a8f2017-02-07 21:49:17 +0800752 * We have a magic /proc file - /proc/net/rpc/<cachename>/channel.
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500753 * On read, you get a full request, or block.
754 * On write, an update request is processed.
755 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800757 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500758 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * to the end and may wakeup and preceding readers.
760 * New readers are added to the head. If, on read, an item is found with
761 * CACHE_UPCALLING clear, we free it from the list.
762 *
763 */
764
765static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800766static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768struct cache_queue {
769 struct list_head list;
770 int reader; /* if 0, then request */
771};
772struct cache_request {
773 struct cache_queue q;
774 struct cache_head *item;
775 char * buf;
776 int len;
777 int readers;
778};
779struct cache_reader {
780 struct cache_queue q;
781 int offset; /* if non-0, we have a refcnt on next request */
782};
783
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300784static int cache_request(struct cache_detail *detail,
785 struct cache_request *crq)
786{
787 char *bp = crq->buf;
788 int len = PAGE_SIZE;
789
790 detail->cache_request(detail, crq->item, &bp, &len);
791 if (len < 0)
792 return -EAGAIN;
793 return PAGE_SIZE - len;
794}
795
Trond Myklebust173912a2009-08-09 15:14:29 -0400796static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
797 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 struct cache_reader *rp = filp->private_data;
800 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500801 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int err;
803
804 if (count == 0)
805 return 0;
806
Al Viro59551022016-01-22 15:40:57 -0500807 inode_lock(inode); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * readers on this file */
809 again:
810 spin_lock(&queue_lock);
811 /* need to find next request */
812 while (rp->q.list.next != &cd->queue &&
813 list_entry(rp->q.list.next, struct cache_queue, list)
814 ->reader) {
815 struct list_head *next = rp->q.list.next;
816 list_move(&rp->q.list, next);
817 }
818 if (rp->q.list.next == &cd->queue) {
819 spin_unlock(&queue_lock);
Al Viro59551022016-01-22 15:40:57 -0500820 inode_unlock(inode);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400821 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823 }
824 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400825 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (rp->offset == 0)
827 rq->readers++;
828 spin_unlock(&queue_lock);
829
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300830 if (rq->len == 0) {
831 err = cache_request(cd, rq);
832 if (err < 0)
833 goto out;
834 rq->len = err;
835 }
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
838 err = -EAGAIN;
839 spin_lock(&queue_lock);
840 list_move(&rp->q.list, &rq->q.list);
841 spin_unlock(&queue_lock);
842 } else {
843 if (rp->offset + count > rq->len)
844 count = rq->len - rp->offset;
845 err = -EFAULT;
846 if (copy_to_user(buf, rq->buf + rp->offset, count))
847 goto out;
848 rp->offset += count;
849 if (rp->offset >= rq->len) {
850 rp->offset = 0;
851 spin_lock(&queue_lock);
852 list_move(&rp->q.list, &rq->q.list);
853 spin_unlock(&queue_lock);
854 }
855 err = 0;
856 }
857 out:
858 if (rp->offset == 0) {
859 /* need to release rq */
860 spin_lock(&queue_lock);
861 rq->readers--;
862 if (rq->readers == 0 &&
863 !test_bit(CACHE_PENDING, &rq->item->flags)) {
864 list_del(&rq->q.list);
865 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800866 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 kfree(rq->buf);
868 kfree(rq);
869 } else
870 spin_unlock(&queue_lock);
871 }
872 if (err == -EAGAIN)
873 goto again;
Al Viro59551022016-01-22 15:40:57 -0500874 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return err ? err : count;
876}
877
Trond Myklebustda770052009-08-09 15:14:28 -0400878static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
879 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Trond Myklebustda770052009-08-09 15:14:28 -0400881 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300883 if (count == 0)
884 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400885 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400887 kaddr[count] = '\0';
888 ret = cd->cache_parse(cd, kaddr, count);
889 if (!ret)
890 ret = count;
891 return ret;
892}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Trond Myklebustda770052009-08-09 15:14:28 -0400894static ssize_t cache_slow_downcall(const char __user *buf,
895 size_t count, struct cache_detail *cd)
896{
897 static char write_buf[8192]; /* protected by queue_io_mutex */
898 ssize_t ret = -EINVAL;
899
900 if (count >= sizeof(write_buf))
901 goto out;
902 mutex_lock(&queue_io_mutex);
903 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800904 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400905out:
906 return ret;
907}
908
909static ssize_t cache_downcall(struct address_space *mapping,
910 const char __user *buf,
911 size_t count, struct cache_detail *cd)
912{
913 struct page *page;
914 char *kaddr;
915 ssize_t ret = -ENOMEM;
916
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300917 if (count >= PAGE_SIZE)
Trond Myklebustda770052009-08-09 15:14:28 -0400918 goto out_slow;
919
920 page = find_or_create_page(mapping, 0, GFP_KERNEL);
921 if (!page)
922 goto out_slow;
923
924 kaddr = kmap(page);
925 ret = cache_do_downcall(kaddr, buf, count, cd);
926 kunmap(page);
927 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300928 put_page(page);
Trond Myklebustda770052009-08-09 15:14:28 -0400929 return ret;
930out_slow:
931 return cache_slow_downcall(buf, count, cd);
932}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Trond Myklebust173912a2009-08-09 15:14:29 -0400934static ssize_t cache_write(struct file *filp, const char __user *buf,
935 size_t count, loff_t *ppos,
936 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Trond Myklebustda770052009-08-09 15:14:28 -0400938 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500939 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400940 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Trond Myklebustda770052009-08-09 15:14:28 -0400942 if (!cd->cache_parse)
943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Al Viro59551022016-01-22 15:40:57 -0500945 inode_lock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400946 ret = cache_downcall(mapping, buf, count, cd);
Al Viro59551022016-01-22 15:40:57 -0500947 inode_unlock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400948out:
949 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
953
Al Viroade994f2017-07-03 00:01:49 -0400954static __poll_t cache_poll(struct file *filp, poll_table *wait,
Trond Myklebust173912a2009-08-09 15:14:29 -0400955 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Al Viroade994f2017-07-03 00:01:49 -0400957 __poll_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct cache_reader *rp = filp->private_data;
959 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 poll_wait(filp, &queue_wait, wait);
962
963 /* alway allow write */
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800964 mask = EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (!rp)
967 return mask;
968
969 spin_lock(&queue_lock);
970
971 for (cq= &rp->q; &cq->list != &cd->queue;
972 cq = list_entry(cq->list.next, struct cache_queue, list))
973 if (!cq->reader) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800974 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 break;
976 }
977 spin_unlock(&queue_lock);
978 return mask;
979}
980
Trond Myklebust173912a2009-08-09 15:14:29 -0400981static int cache_ioctl(struct inode *ino, struct file *filp,
982 unsigned int cmd, unsigned long arg,
983 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 int len = 0;
986 struct cache_reader *rp = filp->private_data;
987 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 if (cmd != FIONREAD || !rp)
990 return -EINVAL;
991
992 spin_lock(&queue_lock);
993
994 /* only find the length remaining in current request,
995 * or the length of the next request
996 */
997 for (cq= &rp->q; &cq->list != &cd->queue;
998 cq = list_entry(cq->list.next, struct cache_queue, list))
999 if (!cq->reader) {
1000 struct cache_request *cr =
1001 container_of(cq, struct cache_request, q);
1002 len = cr->len - rp->offset;
1003 break;
1004 }
1005 spin_unlock(&queue_lock);
1006
1007 return put_user(len, (int __user *)arg);
1008}
1009
Trond Myklebust173912a2009-08-09 15:14:29 -04001010static int cache_open(struct inode *inode, struct file *filp,
1011 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 struct cache_reader *rp = NULL;
1014
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001015 if (!cd || !try_module_get(cd->owner))
1016 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 nonseekable_open(inode, filp);
1018 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +04001020 if (!rp) {
1021 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +04001023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 rp->offset = 0;
1025 rp->q.reader = 1;
Dave Wysochanski64a38e82019-07-26 18:33:01 -04001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 spin_lock(&queue_lock);
1028 list_add(&rp->q.list, &cd->queue);
1029 spin_unlock(&queue_lock);
1030 }
Dave Wysochanski64a38e82019-07-26 18:33:01 -04001031 if (filp->f_mode & FMODE_WRITE)
1032 atomic_inc(&cd->writers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 filp->private_data = rp;
1034 return 0;
1035}
1036
Trond Myklebust173912a2009-08-09 15:14:29 -04001037static int cache_release(struct inode *inode, struct file *filp,
1038 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (rp) {
1043 spin_lock(&queue_lock);
1044 if (rp->offset) {
1045 struct cache_queue *cq;
1046 for (cq= &rp->q; &cq->list != &cd->queue;
1047 cq = list_entry(cq->list.next, struct cache_queue, list))
1048 if (!cq->reader) {
1049 container_of(cq, struct cache_request, q)
1050 ->readers--;
1051 break;
1052 }
1053 rp->offset = 0;
1054 }
1055 list_del(&rp->q.list);
1056 spin_unlock(&queue_lock);
1057
1058 filp->private_data = NULL;
1059 kfree(rp);
1060
Dave Wysochanski64a38e82019-07-26 18:33:01 -04001061 }
1062 if (filp->f_mode & FMODE_WRITE) {
1063 atomic_dec(&cd->writers);
NeilBrownc5b29f82010-08-12 16:55:22 +10001064 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001066 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return 0;
1068}
1069
1070
1071
NeilBrownf866a812009-08-04 15:22:38 +10001072static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001074 struct cache_queue *cq, *tmp;
1075 struct cache_request *cr;
1076 struct list_head dequeued;
1077
1078 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001080 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001082 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (cr->item != ch)
1084 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001085 if (test_bit(CACHE_PENDING, &ch->flags))
1086 /* Lost a race and it is pending again */
1087 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001089 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001090 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001093 while (!list_empty(&dequeued)) {
1094 cr = list_entry(dequeued.next, struct cache_request, q.list);
1095 list_del(&cr->q.list);
1096 cache_put(cr->item, detail);
1097 kfree(cr->buf);
1098 kfree(cr);
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102/*
1103 * Support routines for text-based upcalls.
1104 * Fields are separated by spaces.
1105 * Fields are either mangled to quote space tab newline slosh with slosh
1106 * or a hexified with a leading \x
1107 * Record is terminated with newline.
1108 *
1109 */
1110
1111void qword_add(char **bpp, int *lp, char *str)
1112{
1113 char *bp = *bpp;
1114 int len = *lp;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001115 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (len < 0) return;
1118
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001119 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1120 if (ret >= len) {
1121 bp += len;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001122 len = -1;
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001123 } else {
1124 bp += ret;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001125 len -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 *bp++ = ' ';
1127 len--;
1128 }
1129 *bpp = bp;
1130 *lp = len;
1131}
Trond Myklebust24c37672008-12-23 16:30:12 -05001132EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1135{
1136 char *bp = *bpp;
1137 int len = *lp;
1138
1139 if (len < 0) return;
1140
1141 if (len > 2) {
1142 *bp++ = '\\';
1143 *bp++ = 'x';
1144 len -= 2;
1145 while (blen && len >= 2) {
Andy Shevchenko056785e2013-12-12 15:49:21 +02001146 bp = hex_byte_pack(bp, *buf++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 len -= 2;
1148 blen--;
1149 }
1150 }
1151 if (blen || len<1) len = -1;
1152 else {
1153 *bp++ = ' ';
1154 len--;
1155 }
1156 *bpp = bp;
1157 *lp = len;
1158}
Trond Myklebust24c37672008-12-23 16:30:12 -05001159EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161static void warn_no_listener(struct cache_detail *detail)
1162{
1163 if (detail->last_warn != detail->last_close) {
1164 detail->last_warn = detail->last_close;
1165 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001166 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168}
1169
J. Bruce Fields06497522010-09-19 22:55:06 -04001170static bool cache_listeners_exist(struct cache_detail *detail)
1171{
Dave Wysochanski64a38e82019-07-26 18:33:01 -04001172 if (atomic_read(&detail->writers))
J. Bruce Fields06497522010-09-19 22:55:06 -04001173 return true;
1174 if (detail->last_close == 0)
1175 /* This cache was never opened */
1176 return false;
1177 if (detail->last_close < seconds_since_boot() - 30)
1178 /*
1179 * We allow for the possibility that someone might
1180 * restart a userspace daemon without restarting the
1181 * server; but after 30 seconds, we give up.
1182 */
1183 return false;
1184 return true;
1185}
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001188 * register an upcall request to user-space and queue it up for read() by the
1189 * upcall daemon.
1190 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 * Each request is at most one page long.
1192 */
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +03001193int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195
1196 char *buf;
1197 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001198 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001200 if (!detail->cache_request)
1201 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
J. Bruce Fields06497522010-09-19 22:55:06 -04001203 if (!cache_listeners_exist(detail)) {
1204 warn_no_listener(detail);
1205 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
NeilBrown013920e2013-06-13 12:53:42 +10001207 if (test_bit(CACHE_CLEANED, &h->flags))
1208 /* Too late to make an upcall */
1209 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1212 if (!buf)
1213 return -EAGAIN;
1214
1215 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1216 if (!crq) {
1217 kfree(buf);
1218 return -EAGAIN;
1219 }
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 crq->q.reader = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001223 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 crq->readers = 0;
1225 spin_lock(&queue_lock);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001226 if (test_bit(CACHE_PENDING, &h->flags)) {
1227 crq->item = cache_get(h);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001228 list_add_tail(&crq->q.list, &detail->queue);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001229 } else
NeilBrownf9e1aed2013-06-13 12:53:42 +10001230 /* Lost a race, no longer PENDING, so don't enqueue */
1231 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 spin_unlock(&queue_lock);
1233 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001234 if (ret == -EAGAIN) {
1235 kfree(buf);
1236 kfree(crq);
1237 }
1238 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001240EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242/*
1243 * parse a message from user-space and pass it
1244 * to an appropriate cache
1245 * Messages are, like requests, separated into fields by
1246 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1247 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001248 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 * reply cachename expiry key ... content....
1250 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001251 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254int qword_get(char **bpp, char *dest, int bufsize)
1255{
1256 /* return bytes copied, or -1 on error */
1257 char *bp = *bpp;
1258 int len = 0;
1259
1260 while (*bp == ' ') bp++;
1261
1262 if (bp[0] == '\\' && bp[1] == 'x') {
1263 /* HEX STRING */
1264 bp += 2;
Stefan Hajnoczib7052cd2016-02-18 18:55:54 +00001265 while (len < bufsize - 1) {
Andy Shevchenkoe7f483ea2010-09-21 09:40:25 +03001266 int h, l;
1267
1268 h = hex_to_bin(bp[0]);
1269 if (h < 0)
1270 break;
1271
1272 l = hex_to_bin(bp[1]);
1273 if (l < 0)
1274 break;
1275
1276 *dest++ = (h << 4) | l;
1277 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 len++;
1279 }
1280 } else {
1281 /* text with \nnn octal quoting */
1282 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1283 if (*bp == '\\' &&
1284 isodigit(bp[1]) && (bp[1] <= '3') &&
1285 isodigit(bp[2]) &&
1286 isodigit(bp[3])) {
1287 int byte = (*++bp -'0');
1288 bp++;
1289 byte = (byte << 3) | (*bp++ - '0');
1290 byte = (byte << 3) | (*bp++ - '0');
1291 *dest++ = byte;
1292 len++;
1293 } else {
1294 *dest++ = *bp++;
1295 len++;
1296 }
1297 }
1298 }
1299
1300 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1301 return -1;
1302 while (*bp == ' ') bp++;
1303 *bpp = bp;
1304 *dest = '\0';
1305 return len;
1306}
Trond Myklebust24c37672008-12-23 16:30:12 -05001307EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309
1310/*
Kinglong Mee6489a8f2017-02-07 21:49:17 +08001311 * support /proc/net/rpc/$CACHENAME/content
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 * as a seqfile.
1313 * We call ->cache_show passing NULL for the item to
1314 * get a header, then pass each real item in the cache
1315 */
1316
Trond Myklebustae741362018-10-03 12:01:22 -04001317static void *__cache_seq_start(struct seq_file *m, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001320 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct cache_head *ch;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001322 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (!n--)
1325 return SEQ_START_TOKEN;
1326 hash = n >> 32;
1327 entry = n & ((1LL<<32) - 1);
1328
Trond Myklebustae741362018-10-03 12:01:22 -04001329 hlist_for_each_entry_rcu(ch, &cd->hash_table[hash], cache_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (!entry--)
1331 return ch;
1332 n &= ~((1LL<<32) - 1);
1333 do {
1334 hash++;
1335 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001336 } while(hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001337 hlist_empty(&cd->hash_table[hash]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (hash >= cd->hash_size)
1339 return NULL;
1340 *pos = n+1;
Trond Myklebustae741362018-10-03 12:01:22 -04001341 return hlist_entry_safe(rcu_dereference_raw(
1342 hlist_first_rcu(&cd->hash_table[hash])),
Kinglong Mee129e5822015-07-27 11:10:15 +08001343 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
Trond Myklebustae741362018-10-03 12:01:22 -04001345
Trond Myklebustd48cf352018-10-01 10:41:51 -04001346static void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct cache_head *ch = p;
1349 int hash = (*pos >> 32);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001350 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 if (p == SEQ_START_TOKEN)
1353 hash = 0;
Kinglong Mee129e5822015-07-27 11:10:15 +08001354 else if (ch->cache_list.next == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 hash++;
1356 *pos += 1LL<<32;
1357 } else {
1358 ++*pos;
Trond Myklebustae741362018-10-03 12:01:22 -04001359 return hlist_entry_safe(rcu_dereference_raw(
1360 hlist_next_rcu(&ch->cache_list)),
Kinglong Mee129e5822015-07-27 11:10:15 +08001361 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363 *pos &= ~((1LL<<32) - 1);
1364 while (hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001365 hlist_empty(&cd->hash_table[hash])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 hash++;
1367 *pos += 1LL<<32;
1368 }
1369 if (hash >= cd->hash_size)
1370 return NULL;
1371 ++*pos;
Trond Myklebustae741362018-10-03 12:01:22 -04001372 return hlist_entry_safe(rcu_dereference_raw(
1373 hlist_first_rcu(&cd->hash_table[hash])),
Kinglong Mee129e5822015-07-27 11:10:15 +08001374 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Trond Myklebustae741362018-10-03 12:01:22 -04001377void *cache_seq_start_rcu(struct seq_file *m, loff_t *pos)
1378 __acquires(RCU)
1379{
1380 rcu_read_lock();
1381 return __cache_seq_start(m, pos);
1382}
1383EXPORT_SYMBOL_GPL(cache_seq_start_rcu);
1384
1385void *cache_seq_next_rcu(struct seq_file *file, void *p, loff_t *pos)
1386{
1387 return cache_seq_next(file, p, pos);
1388}
1389EXPORT_SYMBOL_GPL(cache_seq_next_rcu);
1390
1391void cache_seq_stop_rcu(struct seq_file *m, void *p)
1392 __releases(RCU)
1393{
1394 rcu_read_unlock();
1395}
1396EXPORT_SYMBOL_GPL(cache_seq_stop_rcu);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398static int c_show(struct seq_file *m, void *p)
1399{
1400 struct cache_head *cp = p;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001401 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 if (p == SEQ_START_TOKEN)
1404 return cd->cache_show(m, cd, NULL);
1405
1406 ifdebug(CACHE)
Arnd Bergmannf5599352017-10-20 16:34:42 +02001407 seq_printf(m, "# expiry=%lld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001408 convert_to_wallclock(cp->expiry_time),
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001409 kref_read(&cp->ref), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 cache_get(cp);
1411 if (cache_check(cd, cp, NULL))
1412 /* cache_check does a cache_put on failure */
1413 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001414 else {
1415 if (cache_is_expired(cd, cp))
1416 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 return cd->cache_show(m, cd, cp);
1421}
1422
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001423static const struct seq_operations cache_content_op = {
Trond Myklebustd48cf352018-10-01 10:41:51 -04001424 .start = cache_seq_start_rcu,
1425 .next = cache_seq_next_rcu,
1426 .stop = cache_seq_stop_rcu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 .show = c_show,
1428};
1429
Trond Myklebust173912a2009-08-09 15:14:29 -04001430static int content_open(struct inode *inode, struct file *file,
1431 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001433 struct seq_file *seq;
1434 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001436 if (!cd || !try_module_get(cd->owner))
1437 return -EACCES;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001438
1439 err = seq_open(file, &cache_content_op);
1440 if (err) {
Li Zefana5990ea2010-03-11 14:08:10 -08001441 module_put(cd->owner);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001442 return err;
Li Zefana5990ea2010-03-11 14:08:10 -08001443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001445 seq = file->private_data;
1446 seq->private = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001450static int content_release(struct inode *inode, struct file *file,
1451 struct cache_detail *cd)
1452{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001453 int ret = seq_release(inode, file);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001454 module_put(cd->owner);
1455 return ret;
1456}
1457
1458static int open_flush(struct inode *inode, struct file *file,
1459 struct cache_detail *cd)
1460{
1461 if (!cd || !try_module_get(cd->owner))
1462 return -EACCES;
1463 return nonseekable_open(inode, file);
1464}
1465
1466static int release_flush(struct inode *inode, struct file *file,
1467 struct cache_detail *cd)
1468{
1469 module_put(cd->owner);
1470 return 0;
1471}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001474 size_t count, loff_t *ppos,
1475 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Sasha Levin212ba902012-07-17 00:01:26 +02001477 char tbuf[22];
Chuck Lever01b29692007-10-26 13:31:20 -04001478 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Arnd Bergmannf5599352017-10-20 16:34:42 +02001480 len = snprintf(tbuf, sizeof(tbuf), "%llu\n",
Kinglong Mee8ccc8692017-02-07 21:50:32 +08001481 convert_to_wallclock(cd->flush_time));
1482 return simple_read_from_buffer(buf, count, ppos, tbuf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Trond Myklebust173912a2009-08-09 15:14:29 -04001485static ssize_t write_flush(struct file *file, const char __user *buf,
1486 size_t count, loff_t *ppos,
1487 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 char tbuf[20];
NeilBrown3b68e6e2018-02-14 12:15:06 +11001490 char *ep;
Arnd Bergmannf5599352017-10-20 16:34:42 +02001491 time64_t now;
NeilBrownc5b29f82010-08-12 16:55:22 +10001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (*ppos || count > sizeof(tbuf)-1)
1494 return -EINVAL;
1495 if (copy_from_user(tbuf, buf, count))
1496 return -EFAULT;
1497 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001498 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (*ep && *ep != '\n')
1500 return -EINVAL;
NeilBrown3b68e6e2018-02-14 12:15:06 +11001501 /* Note that while we check that 'buf' holds a valid number,
1502 * we always ignore the value and just flush everything.
1503 * Making use of the number leads to races.
Neil Brown77862032015-10-16 08:59:08 +11001504 */
Neil Brown77862032015-10-16 08:59:08 +11001505
NeilBrown3b68e6e2018-02-14 12:15:06 +11001506 now = seconds_since_boot();
1507 /* Always flush everything, so behave like cache_purge()
1508 * Do this by advancing flush_time to the current time,
1509 * or by one second if it has already reached the current time.
1510 * Newly added cache entries will always have ->last_refresh greater
1511 * that ->flush_time, so they don't get flushed prematurely.
1512 */
1513
1514 if (cd->flush_time >= now)
1515 now = cd->flush_time + 1;
1516
1517 cd->flush_time = now;
1518 cd->nextcheck = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 cache_flush();
1520
Jeff Laytonf69d6d82019-08-18 14:18:44 -04001521 if (cd->flush)
1522 cd->flush();
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 *ppos += count;
1525 return count;
1526}
1527
Trond Myklebust173912a2009-08-09 15:14:29 -04001528static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1529 size_t count, loff_t *ppos)
1530{
Al Virod9dda782013-03-31 18:16:14 -04001531 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001532
1533 return cache_read(filp, buf, count, ppos, cd);
1534}
1535
1536static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1537 size_t count, loff_t *ppos)
1538{
Al Virod9dda782013-03-31 18:16:14 -04001539 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001540
1541 return cache_write(filp, buf, count, ppos, cd);
1542}
1543
Al Viroade994f2017-07-03 00:01:49 -04001544static __poll_t cache_poll_procfs(struct file *filp, poll_table *wait)
Trond Myklebust173912a2009-08-09 15:14:29 -04001545{
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_poll(filp, wait, cd);
1549}
1550
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001551static long cache_ioctl_procfs(struct file *filp,
1552 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001553{
Al Viro496ad9a2013-01-23 17:07:38 -05001554 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001555 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001556
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001557 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001558}
1559
1560static int cache_open_procfs(struct inode *inode, struct file *filp)
1561{
Al Virod9dda782013-03-31 18:16:14 -04001562 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001563
1564 return cache_open(inode, filp, cd);
1565}
1566
1567static int cache_release_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_release(inode, filp, cd);
1572}
1573
1574static const struct file_operations cache_file_operations_procfs = {
1575 .owner = THIS_MODULE,
1576 .llseek = no_llseek,
1577 .read = cache_read_procfs,
1578 .write = cache_write_procfs,
1579 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001580 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001581 .open = cache_open_procfs,
1582 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583};
Trond Myklebust173912a2009-08-09 15:14:29 -04001584
1585static int content_open_procfs(struct inode *inode, struct file *filp)
1586{
Al Virod9dda782013-03-31 18:16:14 -04001587 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001588
1589 return content_open(inode, filp, cd);
1590}
1591
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001592static int content_release_procfs(struct inode *inode, struct file *filp)
1593{
Al Virod9dda782013-03-31 18:16:14 -04001594 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001595
1596 return content_release(inode, filp, cd);
1597}
1598
Trond Myklebust173912a2009-08-09 15:14:29 -04001599static const struct file_operations content_file_operations_procfs = {
1600 .open = content_open_procfs,
1601 .read = seq_read,
1602 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001603 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001604};
1605
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001606static int open_flush_procfs(struct inode *inode, struct file *filp)
1607{
Al Virod9dda782013-03-31 18:16:14 -04001608 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001609
1610 return open_flush(inode, filp, cd);
1611}
1612
1613static int release_flush_procfs(struct inode *inode, struct file *filp)
1614{
Al Virod9dda782013-03-31 18:16:14 -04001615 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001616
1617 return release_flush(inode, filp, cd);
1618}
1619
Trond Myklebust173912a2009-08-09 15:14:29 -04001620static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1621 size_t count, loff_t *ppos)
1622{
Al Virod9dda782013-03-31 18:16:14 -04001623 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001624
1625 return read_flush(filp, buf, count, ppos, cd);
1626}
1627
1628static ssize_t write_flush_procfs(struct file *filp,
1629 const char __user *buf,
1630 size_t count, loff_t *ppos)
1631{
Al Virod9dda782013-03-31 18:16:14 -04001632 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001633
1634 return write_flush(filp, buf, count, ppos, cd);
1635}
1636
1637static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001638 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001639 .read = read_flush_procfs,
1640 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001641 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001642 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001643};
1644
Kinglong Mee863d7d92017-02-07 21:47:16 +08001645static void remove_cache_proc_entries(struct cache_detail *cd)
Trond Myklebust173912a2009-08-09 15:14:29 -04001646{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001647 if (cd->procfs) {
1648 proc_remove(cd->procfs);
1649 cd->procfs = NULL;
1650 }
Trond Myklebust173912a2009-08-09 15:14:29 -04001651}
1652
1653#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001654static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001655{
1656 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001657 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001658
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001659 sn = net_generic(net, sunrpc_net_id);
Kinglong Mee863d7d92017-02-07 21:47:16 +08001660 cd->procfs = proc_mkdir(cd->name, sn->proc_net_rpc);
1661 if (cd->procfs == NULL)
Trond Myklebust173912a2009-08-09 15:14:29 -04001662 goto out_nomem;
Trond Myklebust173912a2009-08-09 15:14:29 -04001663
Joe Perchesd6444062018-03-23 15:54:38 -07001664 p = proc_create_data("flush", S_IFREG | 0600,
Kinglong Mee863d7d92017-02-07 21:47:16 +08001665 cd->procfs, &cache_flush_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001666 if (p == NULL)
1667 goto out_nomem;
1668
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001669 if (cd->cache_request || cd->cache_parse) {
Joe Perchesd6444062018-03-23 15:54:38 -07001670 p = proc_create_data("channel", S_IFREG | 0600, cd->procfs,
1671 &cache_file_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001672 if (p == NULL)
1673 goto out_nomem;
1674 }
1675 if (cd->cache_show) {
Joe Perchesd6444062018-03-23 15:54:38 -07001676 p = proc_create_data("content", S_IFREG | 0400, cd->procfs,
1677 &content_file_operations_procfs, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001678 if (p == NULL)
1679 goto out_nomem;
1680 }
1681 return 0;
1682out_nomem:
Kinglong Mee863d7d92017-02-07 21:47:16 +08001683 remove_cache_proc_entries(cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001684 return -ENOMEM;
1685}
1686#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001687static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001688{
1689 return 0;
1690}
1691#endif
1692
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001693void __init cache_initialize(void)
1694{
Tejun Heo203b42f2012-08-21 13:18:23 -07001695 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001696}
1697
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001698int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001699{
1700 int ret;
1701
1702 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001703 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001704 if (ret)
1705 sunrpc_destroy_cache_detail(cd);
1706 return ret;
1707}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001708EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001709
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001710void cache_unregister_net(struct cache_detail *cd, struct net *net)
1711{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001712 remove_cache_proc_entries(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001713 sunrpc_destroy_cache_detail(cd);
1714}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001715EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001716
Bhumika Goyald34971a2017-10-17 18:14:23 +02001717struct cache_detail *cache_create_net(const struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001718{
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001719 struct cache_detail *cd;
Kinglong Mee129e5822015-07-27 11:10:15 +08001720 int i;
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001721
1722 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1723 if (cd == NULL)
1724 return ERR_PTR(-ENOMEM);
1725
Kees Cook6396bb22018-06-12 14:03:40 -07001726 cd->hash_table = kcalloc(cd->hash_size, sizeof(struct hlist_head),
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001727 GFP_KERNEL);
1728 if (cd->hash_table == NULL) {
1729 kfree(cd);
1730 return ERR_PTR(-ENOMEM);
1731 }
Kinglong Mee129e5822015-07-27 11:10:15 +08001732
1733 for (i = 0; i < cd->hash_size; i++)
1734 INIT_HLIST_HEAD(&cd->hash_table[i]);
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001735 cd->net = net;
1736 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001737}
Stanislav Kinsbursky0a402d5a2012-01-19 21:42:21 +04001738EXPORT_SYMBOL_GPL(cache_create_net);
1739
1740void cache_destroy_net(struct cache_detail *cd, struct net *net)
1741{
1742 kfree(cd->hash_table);
1743 kfree(cd);
1744}
1745EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001746
1747static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1748 size_t count, loff_t *ppos)
1749{
Al Viro496ad9a2013-01-23 17:07:38 -05001750 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001751
1752 return cache_read(filp, buf, count, ppos, cd);
1753}
1754
1755static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1756 size_t count, loff_t *ppos)
1757{
Al Viro496ad9a2013-01-23 17:07:38 -05001758 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001759
1760 return cache_write(filp, buf, count, ppos, cd);
1761}
1762
Al Viroade994f2017-07-03 00:01:49 -04001763static __poll_t cache_poll_pipefs(struct file *filp, poll_table *wait)
Trond Myklebust8854e822009-08-09 15:14:30 -04001764{
Al Viro496ad9a2013-01-23 17:07:38 -05001765 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001766
1767 return cache_poll(filp, wait, cd);
1768}
1769
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001770static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001771 unsigned int cmd, unsigned long arg)
1772{
Al Viro496ad9a2013-01-23 17:07:38 -05001773 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001774 struct cache_detail *cd = RPC_I(inode)->private;
1775
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001776 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001777}
1778
1779static int cache_open_pipefs(struct inode *inode, struct file *filp)
1780{
1781 struct cache_detail *cd = RPC_I(inode)->private;
1782
1783 return cache_open(inode, filp, cd);
1784}
1785
1786static int cache_release_pipefs(struct inode *inode, struct file *filp)
1787{
1788 struct cache_detail *cd = RPC_I(inode)->private;
1789
1790 return cache_release(inode, filp, cd);
1791}
1792
1793const struct file_operations cache_file_operations_pipefs = {
1794 .owner = THIS_MODULE,
1795 .llseek = no_llseek,
1796 .read = cache_read_pipefs,
1797 .write = cache_write_pipefs,
1798 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001799 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001800 .open = cache_open_pipefs,
1801 .release = cache_release_pipefs,
1802};
1803
1804static int content_open_pipefs(struct inode *inode, struct file *filp)
1805{
1806 struct cache_detail *cd = RPC_I(inode)->private;
1807
1808 return content_open(inode, filp, cd);
1809}
1810
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001811static int content_release_pipefs(struct inode *inode, struct file *filp)
1812{
1813 struct cache_detail *cd = RPC_I(inode)->private;
1814
1815 return content_release(inode, filp, cd);
1816}
1817
Trond Myklebust8854e822009-08-09 15:14:30 -04001818const struct file_operations content_file_operations_pipefs = {
1819 .open = content_open_pipefs,
1820 .read = seq_read,
1821 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001822 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001823};
1824
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001825static int open_flush_pipefs(struct inode *inode, struct file *filp)
1826{
1827 struct cache_detail *cd = RPC_I(inode)->private;
1828
1829 return open_flush(inode, filp, cd);
1830}
1831
1832static int release_flush_pipefs(struct inode *inode, struct file *filp)
1833{
1834 struct cache_detail *cd = RPC_I(inode)->private;
1835
1836 return release_flush(inode, filp, cd);
1837}
1838
Trond Myklebust8854e822009-08-09 15:14:30 -04001839static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1840 size_t count, loff_t *ppos)
1841{
Al Viro496ad9a2013-01-23 17:07:38 -05001842 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001843
1844 return read_flush(filp, buf, count, ppos, cd);
1845}
1846
1847static ssize_t write_flush_pipefs(struct file *filp,
1848 const char __user *buf,
1849 size_t count, loff_t *ppos)
1850{
Al Viro496ad9a2013-01-23 17:07:38 -05001851 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001852
1853 return write_flush(filp, buf, count, ppos, cd);
1854}
1855
1856const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001857 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001858 .read = read_flush_pipefs,
1859 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001860 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001861 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001862};
1863
1864int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001865 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001866 struct cache_detail *cd)
1867{
Al Viroa95e6912013-07-14 16:43:54 +04001868 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1869 if (IS_ERR(dir))
1870 return PTR_ERR(dir);
Kinglong Mee863d7d92017-02-07 21:47:16 +08001871 cd->pipefs = dir;
Al Viroa95e6912013-07-14 16:43:54 +04001872 return 0;
Trond Myklebust8854e822009-08-09 15:14:30 -04001873}
1874EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1875
1876void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1877{
Kinglong Mee863d7d92017-02-07 21:47:16 +08001878 if (cd->pipefs) {
1879 rpc_remove_cache_dir(cd->pipefs);
1880 cd->pipefs = NULL;
1881 }
Trond Myklebust8854e822009-08-09 15:14:30 -04001882}
1883EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1884
Neil Brown2b477c02016-12-22 12:38:06 -05001885void sunrpc_cache_unhash(struct cache_detail *cd, struct cache_head *h)
1886{
Trond Myklebust1863d772018-10-01 10:41:52 -04001887 spin_lock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001888 if (!hlist_unhashed(&h->cache_list)){
Trond Myklebustae741362018-10-03 12:01:22 -04001889 hlist_del_init_rcu(&h->cache_list);
Neil Brown2b477c02016-12-22 12:38:06 -05001890 cd->entries--;
Trond Myklebust1863d772018-10-01 10:41:52 -04001891 spin_unlock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001892 cache_put(h, cd);
1893 } else
Trond Myklebust1863d772018-10-01 10:41:52 -04001894 spin_unlock(&cd->hash_lock);
Neil Brown2b477c02016-12-22 12:38:06 -05001895}
1896EXPORT_SYMBOL_GPL(sunrpc_cache_unhash);