blob: cab50ece6f3d9e18cb7dd9f243688ae74259422e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sunrpc/cache.c
3 *
4 * Generic code for various authentication-related caches
5 * used by sunrpc clients and servers.
6 *
7 * Copyright (C) 2002 Neil Brown <neilb@cse.unsw.edu.au>
8 *
9 * Released under terms in GPL version 2. See COPYING.
10 *
11 */
12
13#include <linux/types.h>
14#include <linux/fs.h>
15#include <linux/file.h>
16#include <linux/slab.h>
17#include <linux/signal.h>
18#include <linux/sched.h>
19#include <linux/kmod.h>
20#include <linux/list.h>
21#include <linux/module.h>
22#include <linux/ctype.h>
Andy Shevchenko1b2e1222014-11-28 17:50:28 +020023#include <linux/string_helpers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/uaccess.h>
25#include <linux/poll.h>
26#include <linux/seq_file.h>
27#include <linux/proc_fs.h>
28#include <linux/net.h>
29#include <linux/workqueue.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080030#include <linux/mutex.h>
Trond Myklebustda770052009-08-09 15:14:28 -040031#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/ioctls.h>
33#include <linux/sunrpc/types.h>
34#include <linux/sunrpc/cache.h>
35#include <linux/sunrpc/stats.h>
Trond Myklebust8854e822009-08-09 15:14:30 -040036#include <linux/sunrpc/rpc_pipe_fs.h>
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +040037#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define RPCDBG_FACILITY RPCDBG_CACHE
40
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -050041static bool cache_defer_req(struct cache_req *req, struct cache_head *item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static void cache_revisit_request(struct cache_head *item);
43
Neil Brown77862032015-10-16 08:59:08 +110044static void cache_init(struct cache_head *h, struct cache_detail *detail)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
NeilBrownc5b29f82010-08-12 16:55:22 +100046 time_t now = seconds_since_boot();
Kinglong Mee129e5822015-07-27 11:10:15 +080047 INIT_HLIST_NODE(&h->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 h->flags = 0;
NeilBrownbaab9352006-03-27 01:15:09 -080049 kref_init(&h->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 h->expiry_time = now + CACHE_NEW_EXPIRY;
Neil Brown77862032015-10-16 08:59:08 +110051 if (now <= detail->flush_time)
52 /* ensure it isn't already expired */
53 now = detail->flush_time + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 h->last_refresh = now;
55}
56
Vasily Averin9369b7d2018-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
NeilBrown15a5f6b2006-03-27 01:15:02 -080062struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
63 struct cache_head *key, int hash)
64{
Kinglong Mee129e5822015-07-27 11:10:15 +080065 struct cache_head *new = NULL, *freeme = NULL, *tmp = NULL;
66 struct hlist_head *head;
NeilBrown15a5f6b2006-03-27 01:15:02 -080067
68 head = &detail->hash_table[hash];
69
70 read_lock(&detail->hash_lock);
71
Kinglong Mee129e5822015-07-27 11:10:15 +080072 hlist_for_each_entry(tmp, head, cache_list) {
NeilBrown15a5f6b2006-03-27 01:15:02 -080073 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110074 if (cache_is_expired(detail, tmp))
75 /* This entry is expired, we will discard it. */
76 break;
NeilBrown15a5f6b2006-03-27 01:15:02 -080077 cache_get(tmp);
78 read_unlock(&detail->hash_lock);
79 return tmp;
80 }
81 }
82 read_unlock(&detail->hash_lock);
83 /* Didn't find anything, insert an empty entry */
84
85 new = detail->alloc();
86 if (!new)
87 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070088 /* must fully initialise 'new', else
89 * we might get lose if we need to
90 * cache_put it soon.
91 */
Neil Brown77862032015-10-16 08:59:08 +110092 cache_init(new, detail);
Neil Brown2f349312006-08-05 12:14:29 -070093 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080094
95 write_lock(&detail->hash_lock);
96
97 /* check if entry appeared while we slept */
Kinglong Mee129e5822015-07-27 11:10:15 +080098 hlist_for_each_entry(tmp, head, cache_list) {
NeilBrown15a5f6b2006-03-27 01:15:02 -080099 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +1100100 if (cache_is_expired(detail, tmp)) {
Kinglong Mee129e5822015-07-27 11:10:15 +0800101 hlist_del_init(&tmp->cache_list);
NeilBrownd202cce2010-02-03 17:31:31 +1100102 detail->entries --;
Vasily Averin9369b7d2018-11-28 11:45:57 +0300103 cache_fresh_locked(tmp, 0, detail);
NeilBrownd202cce2010-02-03 17:31:31 +1100104 freeme = tmp;
105 break;
106 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800107 cache_get(tmp);
108 write_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800109 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800110 return tmp;
111 }
112 }
Kinglong Mee129e5822015-07-27 11:10:15 +0800113
114 hlist_add_head(&new->cache_list, head);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800115 detail->entries++;
116 cache_get(new);
117 write_unlock(&detail->hash_lock);
118
Vasily Averin9369b7d2018-11-28 11:45:57 +0300119 if (freeme) {
120 cache_fresh_unlocked(freeme, detail);
NeilBrownd202cce2010-02-03 17:31:31 +1100121 cache_put(freeme, detail);
Vasily Averin9369b7d2018-11-28 11:45:57 +0300122 }
NeilBrown15a5f6b2006-03-27 01:15:02 -0800123 return new;
124}
Trond Myklebust24c37672008-12-23 16:30:12 -0500125EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800126
NeilBrownebd0cb12006-03-27 01:15:08 -0800127
NeilBrownf866a812009-08-04 15:22:38 +1000128static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800129
Neil Brown77862032015-10-16 08:59:08 +1100130static void cache_fresh_locked(struct cache_head *head, time_t expiry,
131 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800132{
Neil Brown77862032015-10-16 08:59:08 +1100133 time_t now = seconds_since_boot();
134 if (now <= detail->flush_time)
135 /* ensure it isn't immediately treated as expired */
136 now = detail->flush_time + 1;
NeilBrownebd0cb12006-03-27 01:15:08 -0800137 head->expiry_time = expiry;
Neil Brown77862032015-10-16 08:59:08 +1100138 head->last_refresh = now;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500139 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
NeilBrown908329f2009-09-09 16:32:54 +1000140 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800141}
142
143static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000144 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800145{
NeilBrownebd0cb12006-03-27 01:15:08 -0800146 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
147 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000148 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800149 }
150}
151
NeilBrown15a5f6b2006-03-27 01:15:02 -0800152struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
153 struct cache_head *new, struct cache_head *old, int hash)
154{
155 /* The 'old' entry is to be replaced by 'new'.
156 * If 'old' is not VALID, we update it directly,
157 * otherwise we need to replace it
158 */
NeilBrown15a5f6b2006-03-27 01:15:02 -0800159 struct cache_head *tmp;
160
161 if (!test_bit(CACHE_VALID, &old->flags)) {
162 write_lock(&detail->hash_lock);
163 if (!test_bit(CACHE_VALID, &old->flags)) {
164 if (test_bit(CACHE_NEGATIVE, &new->flags))
165 set_bit(CACHE_NEGATIVE, &old->flags);
166 else
167 detail->update(old, new);
Neil Brown77862032015-10-16 08:59:08 +1100168 cache_fresh_locked(old, new->expiry_time, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800169 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000170 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800171 return old;
172 }
173 write_unlock(&detail->hash_lock);
174 }
175 /* We need to insert a new entry */
176 tmp = detail->alloc();
177 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800178 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800179 return NULL;
180 }
Neil Brown77862032015-10-16 08:59:08 +1100181 cache_init(tmp, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800182 detail->init(tmp, old);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800183
184 write_lock(&detail->hash_lock);
185 if (test_bit(CACHE_NEGATIVE, &new->flags))
186 set_bit(CACHE_NEGATIVE, &tmp->flags);
187 else
188 detail->update(tmp, new);
Kinglong Mee129e5822015-07-27 11:10:15 +0800189 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
NeilBrownf2d39582006-05-22 22:35:25 -0700190 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800191 cache_get(tmp);
Neil Brown77862032015-10-16 08:59:08 +1100192 cache_fresh_locked(tmp, new->expiry_time, detail);
193 cache_fresh_locked(old, 0, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800194 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000195 cache_fresh_unlocked(tmp, detail);
196 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800197 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800198 return tmp;
199}
Trond Myklebust24c37672008-12-23 16:30:12 -0500200EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400202static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
203{
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +0300204 if (cd->cache_upcall)
205 return cd->cache_upcall(cd, h);
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +0300206 return sunrpc_cache_pipe_upcall(cd, h);
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400207}
NeilBrown989a19b2009-08-04 15:22:38 +1000208
chaoting fanb6040f92013-03-28 22:19:45 +0800209static inline int cache_is_valid(struct cache_head *h)
NeilBrown989a19b2009-08-04 15:22:38 +1000210{
NeilBrownd202cce2010-02-03 17:31:31 +1100211 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000212 return -EAGAIN;
213 else {
214 /* entry is valid */
215 if (test_bit(CACHE_NEGATIVE, &h->flags))
216 return -ENOENT;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500217 else {
218 /*
219 * In combination with write barrier in
220 * sunrpc_cache_update, ensures that anyone
221 * using the cache entry after this sees the
222 * updated contents:
223 */
224 smp_rmb();
NeilBrown989a19b2009-08-04 15:22:38 +1000225 return 0;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500226 }
NeilBrown989a19b2009-08-04 15:22:38 +1000227 }
228}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400229
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500230static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
231{
232 int rv;
233
234 write_lock(&detail->hash_lock);
chaoting fanb6040f92013-03-28 22:19:45 +0800235 rv = cache_is_valid(h);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000236 if (rv == -EAGAIN) {
237 set_bit(CACHE_NEGATIVE, &h->flags);
Neil Brown77862032015-10-16 08:59:08 +1100238 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY,
239 detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000240 rv = -ENOENT;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500241 }
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500242 write_unlock(&detail->hash_lock);
243 cache_fresh_unlocked(h, detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000244 return rv;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
248 * This is the generic cache management routine for all
249 * the authentication caches.
250 * It checks the currency of a cache item and will (later)
251 * initiate an upcall to fill it if needed.
252 *
253 *
254 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000255 * -EAGAIN if upcall is pending and request has been queued
256 * -ETIMEDOUT if upcall failed or request could not be queue or
257 * upcall completed but item is still invalid (implying that
258 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * -ENOENT if cache entry was negative
260 */
261int cache_check(struct cache_detail *detail,
262 struct cache_head *h, struct cache_req *rqstp)
263{
264 int rv;
265 long refresh_age, age;
266
267 /* First decide return status as best we can */
chaoting fanb6040f92013-03-28 22:19:45 +0800268 rv = cache_is_valid(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /* now see if we want to start an upcall */
271 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000272 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (rqstp == NULL) {
275 if (rv == -EAGAIN)
276 rv = -ENOENT;
NeilBrown0bebc6332013-06-13 12:53:42 +1000277 } else if (rv == -EAGAIN ||
278 (h->expiry_time != 0 && age > refresh_age/2)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500279 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
280 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
282 switch (cache_make_upcall(detail, h)) {
283 case -EINVAL:
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500284 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 case -EAGAIN:
NeilBrown2a1c7f52013-06-13 12:53:42 +1000287 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 break;
289 }
290 }
291 }
292
NeilBrown989a19b2009-08-04 15:22:38 +1000293 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500294 if (!cache_defer_req(rqstp, h)) {
295 /*
296 * Request was not deferred; handle it as best
297 * we can ourselves:
298 */
chaoting fanb6040f92013-03-28 22:19:45 +0800299 rv = cache_is_valid(h);
NeilBrown989a19b2009-08-04 15:22:38 +1000300 if (rv == -EAGAIN)
301 rv = -ETIMEDOUT;
302 }
303 }
NeilBrown4013ede2006-03-27 01:15:07 -0800304 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800305 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return rv;
307}
Trond Myklebust24c37672008-12-23 16:30:12 -0500308EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/*
311 * caches need to be periodically cleaned.
312 * For this we maintain a list of cache_detail and
313 * a current pointer into that list and into the table
314 * for that entry.
315 *
NeilBrown013920e2013-06-13 12:53:42 +1000316 * Each time cache_clean is called it finds the next non-empty entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * in the current table and walks the list in that entry
318 * looking for entries that can be removed.
319 *
320 * An entry gets removed if:
321 * - The expiry is before current time
322 * - The last_refresh time is before the flush_time for that cache
323 *
324 * later we might drop old entries with non-NEVER expiry if that table
325 * is getting 'full' for some definition of 'full'
326 *
327 * The question of "how often to scan a table" is an interesting one
328 * and is answered in part by the use of the "nextcheck" field in the
329 * cache_detail.
330 * When a scan of a table begins, the nextcheck field is set to a time
331 * that is well into the future.
332 * While scanning, if an expiry time is found that is earlier than the
333 * current nextcheck time, nextcheck is set to that expiry time.
334 * If the flush_time is ever set to a time earlier than the nextcheck
335 * time, the nextcheck time is then set to that flush_time.
336 *
337 * A table is then only scanned if the current time is at least
338 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800339 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
341
342static LIST_HEAD(cache_list);
343static DEFINE_SPINLOCK(cache_list_lock);
344static struct cache_detail *current_detail;
345static int current_index;
346
David Howells65f27f32006-11-22 14:55:48 +0000347static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300348static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300350void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500351{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 rwlock_init(&cd->hash_lock);
353 INIT_LIST_HEAD(&cd->queue);
354 spin_lock(&cache_list_lock);
355 cd->nextcheck = 0;
356 cd->entries = 0;
357 atomic_set(&cd->readers, 0);
358 cd->last_close = 0;
359 cd->last_warn = -1;
360 list_add(&cd->others, &cache_list);
361 spin_unlock(&cache_list_lock);
362
363 /* start the cleaning process */
Ke Wang77b00bc2016-09-01 15:30:26 +0800364 queue_delayed_work(system_power_efficient_wq, &cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300366EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300368void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 cache_purge(cd);
371 spin_lock(&cache_list_lock);
372 write_lock(&cd->hash_lock);
NeilBrownd8d29132016-06-02 16:31:03 +1000373 if (cd->entries) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 write_unlock(&cd->hash_lock);
375 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 if (current_detail == cd)
379 current_detail = NULL;
380 list_del_init(&cd->others);
381 write_unlock(&cd->hash_lock);
382 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (list_empty(&cache_list)) {
384 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400385 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500387 return;
388out:
Kinglong Meeecca0632014-04-15 17:13:56 +0800389 printk(KERN_ERR "RPC: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300391EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393/* clean cache tries to find something to clean
394 * and cleans it.
395 * It returns 1 if it cleaned something,
396 * 0 if it didn't find anything this time
397 * -1 if it fell off the end of the list.
398 */
399static int cache_clean(void)
400{
401 int rv = 0;
402 struct list_head *next;
403
404 spin_lock(&cache_list_lock);
405
406 /* find a suitable table if we don't already have one */
407 while (current_detail == NULL ||
408 current_index >= current_detail->hash_size) {
409 if (current_detail)
410 next = current_detail->others.next;
411 else
412 next = cache_list.next;
413 if (next == &cache_list) {
414 current_detail = NULL;
415 spin_unlock(&cache_list_lock);
416 return -1;
417 }
418 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000419 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 current_index = current_detail->hash_size;
421 else {
422 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000423 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 }
426
427 /* find a non-empty bucket in the table */
428 while (current_detail &&
429 current_index < current_detail->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +0800430 hlist_empty(&current_detail->hash_table[current_index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 current_index++;
432
433 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (current_detail && current_index < current_detail->hash_size) {
Kinglong Mee129e5822015-07-27 11:10:15 +0800436 struct cache_head *ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct cache_detail *d;
Kinglong Mee129e5822015-07-27 11:10:15 +0800438 struct hlist_head *head;
439 struct hlist_node *tmp;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 write_lock(&current_detail->hash_lock);
442
443 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800444
Kinglong Mee129e5822015-07-27 11:10:15 +0800445 head = &current_detail->hash_table[current_index];
446 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (current_detail->nextcheck > ch->expiry_time)
448 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100449 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Kinglong Mee129e5822015-07-27 11:10:15 +0800452 hlist_del_init(&ch->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 current_detail->entries--;
454 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
NeilBrown3af49742010-02-03 17:31:31 +1100457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 write_unlock(&current_detail->hash_lock);
459 d = current_detail;
460 if (!ch)
461 current_index ++;
462 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000463 if (ch) {
NeilBrown013920e2013-06-13 12:53:42 +1000464 set_bit(CACHE_CLEANED, &ch->flags);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000465 cache_fresh_unlocked(ch, d);
NeilBrownbaab9352006-03-27 01:15:09 -0800466 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 } else
469 spin_unlock(&cache_list_lock);
470
471 return rv;
472}
473
474/*
475 * We want to regularly clean the cache, so we need to schedule some work ...
476 */
David Howells65f27f32006-11-22 14:55:48 +0000477static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 int delay = 5;
480 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700481 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (list_empty(&cache_list))
484 delay = 0;
485
486 if (delay)
Ke Wang77b00bc2016-09-01 15:30:26 +0800487 queue_delayed_work(system_power_efficient_wq,
488 &cache_cleaner, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800492/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800494 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * be fully cleaned
496 */
497void cache_flush(void)
498{
499 while (cache_clean() != -1)
500 cond_resched();
501 while (cache_clean() != -1)
502 cond_resched();
503}
Trond Myklebust24c37672008-12-23 16:30:12 -0500504EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506void cache_purge(struct cache_detail *detail)
507{
Neil Brown77862032015-10-16 08:59:08 +1100508 time_t now = seconds_since_boot();
509 if (detail->flush_time >= now)
510 now = detail->flush_time + 1;
511 /* 'now' is the maximum value any 'last_refresh' can have */
512 detail->flush_time = now;
NeilBrownc5b29f82010-08-12 16:55:22 +1000513 detail->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 cache_flush();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
Trond Myklebust24c37672008-12-23 16:30:12 -0500516EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518
519/*
520 * Deferral and Revisiting of Requests.
521 *
522 * If a cache lookup finds a pending entry, we
523 * need to defer the request and revisit it later.
524 * All deferred requests are stored in a hash table,
525 * indexed by "struct cache_head *".
526 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800527 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * deferred form, which must contain a
529 * 'struct cache_deferred_req'
530 * This cache_deferred_req contains a method to allow
531 * it to be revisited when cache info is available
532 */
533
534#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
535#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
536
537#define DFR_MAX 300 /* ??? */
538
539static DEFINE_SPINLOCK(cache_defer_lock);
540static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000541static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static int cache_defer_cnt;
543
J. Bruce Fields6610f722010-08-26 13:19:52 -0400544static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
NeilBrown11174492010-08-12 17:04:08 +1000546 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100547 if (!list_empty(&dreq->recent)) {
548 list_del_init(&dreq->recent);
549 cache_defer_cnt--;
550 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400551}
552
553static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
554{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int hash = DFR_HASH(item);
556
NeilBrowne33534d2010-10-07 15:29:46 +1100557 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000558 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400559}
560
NeilBrowne33534d2010-10-07 15:29:46 +1100561static void setup_deferral(struct cache_deferred_req *dreq,
562 struct cache_head *item,
563 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 spin_lock(&cache_defer_lock);
569
J. Bruce Fields6610f722010-08-26 13:19:52 -0400570 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
NeilBrowne33534d2010-10-07 15:29:46 +1100572 if (count_me) {
573 cache_defer_cnt++;
574 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
NeilBrowne33534d2010-10-07 15:29:46 +1100576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 spin_unlock(&cache_defer_lock);
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
J. Bruce Fields3211af12010-08-26 16:56:23 -0400581struct thread_deferred_req {
582 struct cache_deferred_req handle;
583 struct completion completion;
584};
585
586static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
587{
588 struct thread_deferred_req *dr =
589 container_of(dreq, struct thread_deferred_req, handle);
590 complete(&dr->completion);
591}
592
NeilBrownd29068c42010-10-07 15:29:46 +1100593static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400594{
595 struct thread_deferred_req sleeper;
596 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400597
598 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
599 dreq->revisit = cache_restart_thread;
600
NeilBrowne33534d2010-10-07 15:29:46 +1100601 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400602
NeilBrownd29068c42010-10-07 15:29:46 +1100603 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000604 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400605 &sleeper.completion, req->thread_wait) <= 0) {
606 /* The completion wasn't completed, so we need
607 * to clean up
608 */
609 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000610 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400611 __unhash_deferred_req(&sleeper.handle);
612 spin_unlock(&cache_defer_lock);
613 } else {
614 /* cache_revisit_request already removed
615 * this from the hash table, but hasn't
616 * called ->revisit yet. It will very soon
617 * and we need to wait for it.
618 */
619 spin_unlock(&cache_defer_lock);
620 wait_for_completion(&sleeper.completion);
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400623}
624
NeilBrowne33534d2010-10-07 15:29:46 +1100625static void cache_limit_defers(void)
626{
627 /* Make sure we haven't exceed the limit of allowed deferred
628 * requests.
629 */
630 struct cache_deferred_req *discard = NULL;
631
632 if (cache_defer_cnt <= DFR_MAX)
633 return;
634
635 spin_lock(&cache_defer_lock);
636
637 /* Consider removing either the first or the last */
638 if (cache_defer_cnt > DFR_MAX) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500639 if (prandom_u32() & 1)
NeilBrowne33534d2010-10-07 15:29:46 +1100640 discard = list_entry(cache_defer_list.next,
641 struct cache_deferred_req, recent);
642 else
643 discard = list_entry(cache_defer_list.prev,
644 struct cache_deferred_req, recent);
645 __unhash_deferred_req(discard);
646 }
647 spin_unlock(&cache_defer_lock);
648 if (discard)
649 discard->revisit(discard, 1);
650}
651
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500652/* Return true if and only if a deferred request is queued. */
653static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400654{
655 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400656
J. Bruce Fields3211af12010-08-26 16:56:23 -0400657 if (req->thread_wait) {
NeilBrownd29068c42010-10-07 15:29:46 +1100658 cache_wait_req(req, item);
659 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500660 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400661 }
662 dreq = req->defer(req);
663 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500664 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100665 setup_deferral(dreq, item, 1);
NeilBrownd29068c42010-10-07 15:29:46 +1100666 if (!test_bit(CACHE_PENDING, &item->flags))
667 /* Bit could have been cleared before we managed to
668 * set up the deferral, so need to revisit just in case
669 */
670 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100671
672 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500673 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676static void cache_revisit_request(struct cache_head *item)
677{
678 struct cache_deferred_req *dreq;
679 struct list_head pending;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800680 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int hash = DFR_HASH(item);
682
683 INIT_LIST_HEAD(&pending);
684 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800685
Sasha Levinb67bfe02013-02-27 17:06:00 -0800686 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000687 if (dreq->item == item) {
688 __unhash_deferred_req(dreq);
689 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
NeilBrown11174492010-08-12 17:04:08 +1000691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 spin_unlock(&cache_defer_lock);
693
694 while (!list_empty(&pending)) {
695 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
696 list_del_init(&dreq->recent);
697 dreq->revisit(dreq, 0);
698 }
699}
700
701void cache_clean_deferred(void *owner)
702{
703 struct cache_deferred_req *dreq, *tmp;
704 struct list_head pending;
705
706
707 INIT_LIST_HEAD(&pending);
708 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
711 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400712 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000713 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 }
716 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, 1);
722 }
723}
724
725/*
726 * communicate with user-space
727 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500728 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
729 * On read, you get a full request, or block.
730 * On write, an update request is processed.
731 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800733 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500734 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * to the end and may wakeup and preceding readers.
736 * New readers are added to the head. If, on read, an item is found with
737 * CACHE_UPCALLING clear, we free it from the list.
738 *
739 */
740
741static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800742static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744struct cache_queue {
745 struct list_head list;
746 int reader; /* if 0, then request */
747};
748struct cache_request {
749 struct cache_queue q;
750 struct cache_head *item;
751 char * buf;
752 int len;
753 int readers;
754};
755struct cache_reader {
756 struct cache_queue q;
757 int offset; /* if non-0, we have a refcnt on next request */
758};
759
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300760static int cache_request(struct cache_detail *detail,
761 struct cache_request *crq)
762{
763 char *bp = crq->buf;
764 int len = PAGE_SIZE;
765
766 detail->cache_request(detail, crq->item, &bp, &len);
767 if (len < 0)
768 return -EAGAIN;
769 return PAGE_SIZE - len;
770}
771
Trond Myklebust173912a2009-08-09 15:14:29 -0400772static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
773 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 struct cache_reader *rp = filp->private_data;
776 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500777 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int err;
779
780 if (count == 0)
781 return 0;
782
Al Viro59551022016-01-22 15:40:57 -0500783 inode_lock(inode); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * readers on this file */
785 again:
786 spin_lock(&queue_lock);
787 /* need to find next request */
788 while (rp->q.list.next != &cd->queue &&
789 list_entry(rp->q.list.next, struct cache_queue, list)
790 ->reader) {
791 struct list_head *next = rp->q.list.next;
792 list_move(&rp->q.list, next);
793 }
794 if (rp->q.list.next == &cd->queue) {
795 spin_unlock(&queue_lock);
Al Viro59551022016-01-22 15:40:57 -0500796 inode_unlock(inode);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400797 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return 0;
799 }
800 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400801 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (rp->offset == 0)
803 rq->readers++;
804 spin_unlock(&queue_lock);
805
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300806 if (rq->len == 0) {
807 err = cache_request(cd, rq);
808 if (err < 0)
809 goto out;
810 rq->len = err;
811 }
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
814 err = -EAGAIN;
815 spin_lock(&queue_lock);
816 list_move(&rp->q.list, &rq->q.list);
817 spin_unlock(&queue_lock);
818 } else {
819 if (rp->offset + count > rq->len)
820 count = rq->len - rp->offset;
821 err = -EFAULT;
822 if (copy_to_user(buf, rq->buf + rp->offset, count))
823 goto out;
824 rp->offset += count;
825 if (rp->offset >= rq->len) {
826 rp->offset = 0;
827 spin_lock(&queue_lock);
828 list_move(&rp->q.list, &rq->q.list);
829 spin_unlock(&queue_lock);
830 }
831 err = 0;
832 }
833 out:
834 if (rp->offset == 0) {
835 /* need to release rq */
836 spin_lock(&queue_lock);
837 rq->readers--;
838 if (rq->readers == 0 &&
839 !test_bit(CACHE_PENDING, &rq->item->flags)) {
840 list_del(&rq->q.list);
841 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800842 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 kfree(rq->buf);
844 kfree(rq);
845 } else
846 spin_unlock(&queue_lock);
847 }
848 if (err == -EAGAIN)
849 goto again;
Al Viro59551022016-01-22 15:40:57 -0500850 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return err ? err : count;
852}
853
Trond Myklebustda770052009-08-09 15:14:28 -0400854static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
855 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Trond Myklebustda770052009-08-09 15:14:28 -0400857 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300859 if (count == 0)
860 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400861 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400863 kaddr[count] = '\0';
864 ret = cd->cache_parse(cd, kaddr, count);
865 if (!ret)
866 ret = count;
867 return ret;
868}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Trond Myklebustda770052009-08-09 15:14:28 -0400870static ssize_t cache_slow_downcall(const char __user *buf,
871 size_t count, struct cache_detail *cd)
872{
873 static char write_buf[8192]; /* protected by queue_io_mutex */
874 ssize_t ret = -EINVAL;
875
876 if (count >= sizeof(write_buf))
877 goto out;
878 mutex_lock(&queue_io_mutex);
879 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800880 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400881out:
882 return ret;
883}
884
885static ssize_t cache_downcall(struct address_space *mapping,
886 const char __user *buf,
887 size_t count, struct cache_detail *cd)
888{
889 struct page *page;
890 char *kaddr;
891 ssize_t ret = -ENOMEM;
892
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300893 if (count >= PAGE_SIZE)
Trond Myklebustda770052009-08-09 15:14:28 -0400894 goto out_slow;
895
896 page = find_or_create_page(mapping, 0, GFP_KERNEL);
897 if (!page)
898 goto out_slow;
899
900 kaddr = kmap(page);
901 ret = cache_do_downcall(kaddr, buf, count, cd);
902 kunmap(page);
903 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300904 put_page(page);
Trond Myklebustda770052009-08-09 15:14:28 -0400905 return ret;
906out_slow:
907 return cache_slow_downcall(buf, count, cd);
908}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Trond Myklebust173912a2009-08-09 15:14:29 -0400910static ssize_t cache_write(struct file *filp, const char __user *buf,
911 size_t count, loff_t *ppos,
912 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Trond Myklebustda770052009-08-09 15:14:28 -0400914 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500915 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400916 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Trond Myklebustda770052009-08-09 15:14:28 -0400918 if (!cd->cache_parse)
919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Al Viro59551022016-01-22 15:40:57 -0500921 inode_lock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400922 ret = cache_downcall(mapping, buf, count, cd);
Al Viro59551022016-01-22 15:40:57 -0500923 inode_unlock(inode);
Trond Myklebustda770052009-08-09 15:14:28 -0400924out:
925 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
928static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
929
Trond Myklebust173912a2009-08-09 15:14:29 -0400930static unsigned int cache_poll(struct file *filp, poll_table *wait,
931 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 unsigned int mask;
934 struct cache_reader *rp = filp->private_data;
935 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 poll_wait(filp, &queue_wait, wait);
938
939 /* alway allow write */
Al Viro1711fd9a2015-03-07 21:08:46 +0000940 mask = POLLOUT | POLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 if (!rp)
943 return mask;
944
945 spin_lock(&queue_lock);
946
947 for (cq= &rp->q; &cq->list != &cd->queue;
948 cq = list_entry(cq->list.next, struct cache_queue, list))
949 if (!cq->reader) {
950 mask |= POLLIN | POLLRDNORM;
951 break;
952 }
953 spin_unlock(&queue_lock);
954 return mask;
955}
956
Trond Myklebust173912a2009-08-09 15:14:29 -0400957static int cache_ioctl(struct inode *ino, struct file *filp,
958 unsigned int cmd, unsigned long arg,
959 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 int len = 0;
962 struct cache_reader *rp = filp->private_data;
963 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 if (cmd != FIONREAD || !rp)
966 return -EINVAL;
967
968 spin_lock(&queue_lock);
969
970 /* only find the length remaining in current request,
971 * or the length of the next request
972 */
973 for (cq= &rp->q; &cq->list != &cd->queue;
974 cq = list_entry(cq->list.next, struct cache_queue, list))
975 if (!cq->reader) {
976 struct cache_request *cr =
977 container_of(cq, struct cache_request, q);
978 len = cr->len - rp->offset;
979 break;
980 }
981 spin_unlock(&queue_lock);
982
983 return put_user(len, (int __user *)arg);
984}
985
Trond Myklebust173912a2009-08-09 15:14:29 -0400986static int cache_open(struct inode *inode, struct file *filp,
987 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 struct cache_reader *rp = NULL;
990
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400991 if (!cd || !try_module_get(cd->owner))
992 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 nonseekable_open(inode, filp);
994 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400996 if (!rp) {
997 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 rp->offset = 0;
1001 rp->q.reader = 1;
1002 atomic_inc(&cd->readers);
1003 spin_lock(&queue_lock);
1004 list_add(&rp->q.list, &cd->queue);
1005 spin_unlock(&queue_lock);
1006 }
1007 filp->private_data = rp;
1008 return 0;
1009}
1010
Trond Myklebust173912a2009-08-09 15:14:29 -04001011static int cache_release(struct inode *inode, struct file *filp,
1012 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (rp) {
1017 spin_lock(&queue_lock);
1018 if (rp->offset) {
1019 struct cache_queue *cq;
1020 for (cq= &rp->q; &cq->list != &cd->queue;
1021 cq = list_entry(cq->list.next, struct cache_queue, list))
1022 if (!cq->reader) {
1023 container_of(cq, struct cache_request, q)
1024 ->readers--;
1025 break;
1026 }
1027 rp->offset = 0;
1028 }
1029 list_del(&rp->q.list);
1030 spin_unlock(&queue_lock);
1031
1032 filp->private_data = NULL;
1033 kfree(rp);
1034
NeilBrownc5b29f82010-08-12 16:55:22 +10001035 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 atomic_dec(&cd->readers);
1037 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001038 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return 0;
1040}
1041
1042
1043
NeilBrownf866a812009-08-04 15:22:38 +10001044static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001046 struct cache_queue *cq, *tmp;
1047 struct cache_request *cr;
1048 struct list_head dequeued;
1049
1050 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001052 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001054 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (cr->item != ch)
1056 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001057 if (test_bit(CACHE_PENDING, &ch->flags))
1058 /* Lost a race and it is pending again */
1059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001061 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001062 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001065 while (!list_empty(&dequeued)) {
1066 cr = list_entry(dequeued.next, struct cache_request, q.list);
1067 list_del(&cr->q.list);
1068 cache_put(cr->item, detail);
1069 kfree(cr->buf);
1070 kfree(cr);
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074/*
1075 * Support routines for text-based upcalls.
1076 * Fields are separated by spaces.
1077 * Fields are either mangled to quote space tab newline slosh with slosh
1078 * or a hexified with a leading \x
1079 * Record is terminated with newline.
1080 *
1081 */
1082
1083void qword_add(char **bpp, int *lp, char *str)
1084{
1085 char *bp = *bpp;
1086 int len = *lp;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001087 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 if (len < 0) return;
1090
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001091 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1092 if (ret >= len) {
1093 bp += len;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001094 len = -1;
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001095 } else {
1096 bp += ret;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001097 len -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 *bp++ = ' ';
1099 len--;
1100 }
1101 *bpp = bp;
1102 *lp = len;
1103}
Trond Myklebust24c37672008-12-23 16:30:12 -05001104EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1107{
1108 char *bp = *bpp;
1109 int len = *lp;
1110
1111 if (len < 0) return;
1112
1113 if (len > 2) {
1114 *bp++ = '\\';
1115 *bp++ = 'x';
1116 len -= 2;
1117 while (blen && len >= 2) {
Andy Shevchenko056785e2013-12-12 15:49:21 +02001118 bp = hex_byte_pack(bp, *buf++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 len -= 2;
1120 blen--;
1121 }
1122 }
1123 if (blen || len<1) len = -1;
1124 else {
1125 *bp++ = ' ';
1126 len--;
1127 }
1128 *bpp = bp;
1129 *lp = len;
1130}
Trond Myklebust24c37672008-12-23 16:30:12 -05001131EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133static void warn_no_listener(struct cache_detail *detail)
1134{
1135 if (detail->last_warn != detail->last_close) {
1136 detail->last_warn = detail->last_close;
1137 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001138 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140}
1141
J. Bruce Fields06497522010-09-19 22:55:06 -04001142static bool cache_listeners_exist(struct cache_detail *detail)
1143{
1144 if (atomic_read(&detail->readers))
1145 return true;
1146 if (detail->last_close == 0)
1147 /* This cache was never opened */
1148 return false;
1149 if (detail->last_close < seconds_since_boot() - 30)
1150 /*
1151 * We allow for the possibility that someone might
1152 * restart a userspace daemon without restarting the
1153 * server; but after 30 seconds, we give up.
1154 */
1155 return false;
1156 return true;
1157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001160 * register an upcall request to user-space and queue it up for read() by the
1161 * upcall daemon.
1162 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * Each request is at most one page long.
1164 */
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +03001165int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167
1168 char *buf;
1169 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001170 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001172 if (!detail->cache_request)
1173 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
J. Bruce Fields06497522010-09-19 22:55:06 -04001175 if (!cache_listeners_exist(detail)) {
1176 warn_no_listener(detail);
1177 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
NeilBrown013920e2013-06-13 12:53:42 +10001179 if (test_bit(CACHE_CLEANED, &h->flags))
1180 /* Too late to make an upcall */
1181 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1184 if (!buf)
1185 return -EAGAIN;
1186
1187 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1188 if (!crq) {
1189 kfree(buf);
1190 return -EAGAIN;
1191 }
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 crq->q.reader = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001195 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 crq->readers = 0;
1197 spin_lock(&queue_lock);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001198 if (test_bit(CACHE_PENDING, &h->flags)) {
1199 crq->item = cache_get(h);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001200 list_add_tail(&crq->q.list, &detail->queue);
NeilBrowna6ab1e82016-03-04 17:20:13 +11001201 } else
NeilBrownf9e1aed2013-06-13 12:53:42 +10001202 /* Lost a race, no longer PENDING, so don't enqueue */
1203 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 spin_unlock(&queue_lock);
1205 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001206 if (ret == -EAGAIN) {
1207 kfree(buf);
1208 kfree(crq);
1209 }
1210 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001212EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214/*
1215 * parse a message from user-space and pass it
1216 * to an appropriate cache
1217 * Messages are, like requests, separated into fields by
1218 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1219 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001220 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 * reply cachename expiry key ... content....
1222 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001223 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 */
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226int qword_get(char **bpp, char *dest, int bufsize)
1227{
1228 /* return bytes copied, or -1 on error */
1229 char *bp = *bpp;
1230 int len = 0;
1231
1232 while (*bp == ' ') bp++;
1233
1234 if (bp[0] == '\\' && bp[1] == 'x') {
1235 /* HEX STRING */
1236 bp += 2;
Stefan Hajnoczib7052cd2016-02-18 18:55:54 +00001237 while (len < bufsize - 1) {
Andy Shevchenkoe7f483e2010-09-21 09:40:25 +03001238 int h, l;
1239
1240 h = hex_to_bin(bp[0]);
1241 if (h < 0)
1242 break;
1243
1244 l = hex_to_bin(bp[1]);
1245 if (l < 0)
1246 break;
1247
1248 *dest++ = (h << 4) | l;
1249 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 len++;
1251 }
1252 } else {
1253 /* text with \nnn octal quoting */
1254 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1255 if (*bp == '\\' &&
1256 isodigit(bp[1]) && (bp[1] <= '3') &&
1257 isodigit(bp[2]) &&
1258 isodigit(bp[3])) {
1259 int byte = (*++bp -'0');
1260 bp++;
1261 byte = (byte << 3) | (*bp++ - '0');
1262 byte = (byte << 3) | (*bp++ - '0');
1263 *dest++ = byte;
1264 len++;
1265 } else {
1266 *dest++ = *bp++;
1267 len++;
1268 }
1269 }
1270 }
1271
1272 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1273 return -1;
1274 while (*bp == ' ') bp++;
1275 *bpp = bp;
1276 *dest = '\0';
1277 return len;
1278}
Trond Myklebust24c37672008-12-23 16:30:12 -05001279EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281
1282/*
1283 * support /proc/sunrpc/cache/$CACHENAME/content
1284 * as a seqfile.
1285 * We call ->cache_show passing NULL for the item to
1286 * get a header, then pass each real item in the cache
1287 */
1288
Kinglong Meec8c081b2015-07-27 11:09:42 +08001289void *cache_seq_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001290 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001293 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 struct cache_head *ch;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001295 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 read_lock(&cd->hash_lock);
1298 if (!n--)
1299 return SEQ_START_TOKEN;
1300 hash = n >> 32;
1301 entry = n & ((1LL<<32) - 1);
1302
Kinglong Mee129e5822015-07-27 11:10:15 +08001303 hlist_for_each_entry(ch, &cd->hash_table[hash], cache_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (!entry--)
1305 return ch;
1306 n &= ~((1LL<<32) - 1);
1307 do {
1308 hash++;
1309 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001310 } while(hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001311 hlist_empty(&cd->hash_table[hash]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (hash >= cd->hash_size)
1313 return NULL;
1314 *pos = n+1;
Kinglong Mee129e5822015-07-27 11:10:15 +08001315 return hlist_entry_safe(cd->hash_table[hash].first,
1316 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
Kinglong Meec8c081b2015-07-27 11:09:42 +08001318EXPORT_SYMBOL_GPL(cache_seq_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Kinglong Meec8c081b2015-07-27 11:09:42 +08001320void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 struct cache_head *ch = p;
1323 int hash = (*pos >> 32);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001324 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (p == SEQ_START_TOKEN)
1327 hash = 0;
Kinglong Mee129e5822015-07-27 11:10:15 +08001328 else if (ch->cache_list.next == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 hash++;
1330 *pos += 1LL<<32;
1331 } else {
1332 ++*pos;
Kinglong Mee129e5822015-07-27 11:10:15 +08001333 return hlist_entry_safe(ch->cache_list.next,
1334 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336 *pos &= ~((1LL<<32) - 1);
1337 while (hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001338 hlist_empty(&cd->hash_table[hash])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 hash++;
1340 *pos += 1LL<<32;
1341 }
1342 if (hash >= cd->hash_size)
1343 return NULL;
1344 ++*pos;
Kinglong Mee129e5822015-07-27 11:10:15 +08001345 return hlist_entry_safe(cd->hash_table[hash].first,
1346 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
Kinglong Meec8c081b2015-07-27 11:09:42 +08001348EXPORT_SYMBOL_GPL(cache_seq_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Kinglong Meec8c081b2015-07-27 11:09:42 +08001350void cache_seq_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001351 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001353 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 read_unlock(&cd->hash_lock);
1355}
Kinglong Meec8c081b2015-07-27 11:09:42 +08001356EXPORT_SYMBOL_GPL(cache_seq_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358static int c_show(struct seq_file *m, void *p)
1359{
1360 struct cache_head *cp = p;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001361 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 if (p == SEQ_START_TOKEN)
1364 return cd->cache_show(m, cd, NULL);
1365
1366 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001367 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001368 convert_to_wallclock(cp->expiry_time),
1369 atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 cache_get(cp);
1371 if (cache_check(cd, cp, NULL))
1372 /* cache_check does a cache_put on failure */
1373 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001374 else {
1375 if (cache_is_expired(cd, cp))
1376 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 return cd->cache_show(m, cd, cp);
1381}
1382
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001383static const struct seq_operations cache_content_op = {
Kinglong Meec8c081b2015-07-27 11:09:42 +08001384 .start = cache_seq_start,
1385 .next = cache_seq_next,
1386 .stop = cache_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 .show = c_show,
1388};
1389
Trond Myklebust173912a2009-08-09 15:14:29 -04001390static int content_open(struct inode *inode, struct file *file,
1391 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001393 struct seq_file *seq;
1394 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001396 if (!cd || !try_module_get(cd->owner))
1397 return -EACCES;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001398
1399 err = seq_open(file, &cache_content_op);
1400 if (err) {
Li Zefana5990ea2010-03-11 14:08:10 -08001401 module_put(cd->owner);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001402 return err;
Li Zefana5990ea2010-03-11 14:08:10 -08001403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001405 seq = file->private_data;
1406 seq->private = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001410static int content_release(struct inode *inode, struct file *file,
1411 struct cache_detail *cd)
1412{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001413 int ret = seq_release(inode, file);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001414 module_put(cd->owner);
1415 return ret;
1416}
1417
1418static int open_flush(struct inode *inode, struct file *file,
1419 struct cache_detail *cd)
1420{
1421 if (!cd || !try_module_get(cd->owner))
1422 return -EACCES;
1423 return nonseekable_open(inode, file);
1424}
1425
1426static int release_flush(struct inode *inode, struct file *file,
1427 struct cache_detail *cd)
1428{
1429 module_put(cd->owner);
1430 return 0;
1431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001434 size_t count, loff_t *ppos,
1435 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Sasha Levin212ba902012-07-17 00:01:26 +02001437 char tbuf[22];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001439 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Sasha Levin212ba902012-07-17 00:01:26 +02001441 snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 len = strlen(tbuf);
1443 if (p >= len)
1444 return 0;
1445 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001446 if (len > count)
1447 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001449 return -EFAULT;
1450 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return len;
1452}
1453
Trond Myklebust173912a2009-08-09 15:14:29 -04001454static ssize_t write_flush(struct file *file, const char __user *buf,
1455 size_t count, loff_t *ppos,
1456 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 char tbuf[20];
NeilBrownc5b29f82010-08-12 16:55:22 +10001459 char *bp, *ep;
Neil Brown77862032015-10-16 08:59:08 +11001460 time_t then, now;
NeilBrownc5b29f82010-08-12 16:55:22 +10001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (*ppos || count > sizeof(tbuf)-1)
1463 return -EINVAL;
1464 if (copy_from_user(tbuf, buf, count))
1465 return -EFAULT;
1466 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001467 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (*ep && *ep != '\n')
1469 return -EINVAL;
1470
NeilBrownc5b29f82010-08-12 16:55:22 +10001471 bp = tbuf;
Neil Brown77862032015-10-16 08:59:08 +11001472 then = get_expiry(&bp);
1473 now = seconds_since_boot();
1474 cd->nextcheck = now;
1475 /* Can only set flush_time to 1 second beyond "now", or
1476 * possibly 1 second beyond flushtime. This is because
1477 * flush_time never goes backwards so it mustn't get too far
1478 * ahead of time.
1479 */
1480 if (then >= now) {
1481 /* Want to flush everything, so behave like cache_purge() */
1482 if (cd->flush_time >= now)
1483 now = cd->flush_time + 1;
1484 then = now;
1485 }
1486
1487 cd->flush_time = then;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 cache_flush();
1489
1490 *ppos += count;
1491 return count;
1492}
1493
Trond Myklebust173912a2009-08-09 15:14:29 -04001494static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1495 size_t count, loff_t *ppos)
1496{
Al Virod9dda782013-03-31 18:16:14 -04001497 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001498
1499 return cache_read(filp, buf, count, ppos, cd);
1500}
1501
1502static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1503 size_t count, loff_t *ppos)
1504{
Al Virod9dda782013-03-31 18:16:14 -04001505 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001506
1507 return cache_write(filp, buf, count, ppos, cd);
1508}
1509
1510static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1511{
Al Virod9dda782013-03-31 18:16:14 -04001512 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001513
1514 return cache_poll(filp, wait, cd);
1515}
1516
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001517static long cache_ioctl_procfs(struct file *filp,
1518 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001519{
Al Viro496ad9a2013-01-23 17:07:38 -05001520 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001521 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001522
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001523 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001524}
1525
1526static int cache_open_procfs(struct inode *inode, struct file *filp)
1527{
Al Virod9dda782013-03-31 18:16:14 -04001528 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001529
1530 return cache_open(inode, filp, cd);
1531}
1532
1533static int cache_release_procfs(struct inode *inode, struct file *filp)
1534{
Al Virod9dda782013-03-31 18:16:14 -04001535 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001536
1537 return cache_release(inode, filp, cd);
1538}
1539
1540static const struct file_operations cache_file_operations_procfs = {
1541 .owner = THIS_MODULE,
1542 .llseek = no_llseek,
1543 .read = cache_read_procfs,
1544 .write = cache_write_procfs,
1545 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001546 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001547 .open = cache_open_procfs,
1548 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549};
Trond Myklebust173912a2009-08-09 15:14:29 -04001550
1551static int content_open_procfs(struct inode *inode, struct file *filp)
1552{
Al Virod9dda782013-03-31 18:16:14 -04001553 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001554
1555 return content_open(inode, filp, cd);
1556}
1557
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001558static int content_release_procfs(struct inode *inode, struct file *filp)
1559{
Al Virod9dda782013-03-31 18:16:14 -04001560 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001561
1562 return content_release(inode, filp, cd);
1563}
1564
Trond Myklebust173912a2009-08-09 15:14:29 -04001565static const struct file_operations content_file_operations_procfs = {
1566 .open = content_open_procfs,
1567 .read = seq_read,
1568 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001569 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001570};
1571
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001572static int open_flush_procfs(struct inode *inode, struct file *filp)
1573{
Al Virod9dda782013-03-31 18:16:14 -04001574 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001575
1576 return open_flush(inode, filp, cd);
1577}
1578
1579static int release_flush_procfs(struct inode *inode, struct file *filp)
1580{
Al Virod9dda782013-03-31 18:16:14 -04001581 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001582
1583 return release_flush(inode, filp, cd);
1584}
1585
Trond Myklebust173912a2009-08-09 15:14:29 -04001586static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1587 size_t count, loff_t *ppos)
1588{
Al Virod9dda782013-03-31 18:16:14 -04001589 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001590
1591 return read_flush(filp, buf, count, ppos, cd);
1592}
1593
1594static ssize_t write_flush_procfs(struct file *filp,
1595 const char __user *buf,
1596 size_t count, loff_t *ppos)
1597{
Al Virod9dda782013-03-31 18:16:14 -04001598 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001599
1600 return write_flush(filp, buf, count, ppos, cd);
1601}
1602
1603static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001604 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001605 .read = read_flush_procfs,
1606 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001607 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001608 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001609};
1610
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001611static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001612{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001613 struct sunrpc_net *sn;
1614
Trond Myklebust173912a2009-08-09 15:14:29 -04001615 if (cd->u.procfs.proc_ent == NULL)
1616 return;
1617 if (cd->u.procfs.flush_ent)
1618 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1619 if (cd->u.procfs.channel_ent)
1620 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1621 if (cd->u.procfs.content_ent)
1622 remove_proc_entry("content", cd->u.procfs.proc_ent);
1623 cd->u.procfs.proc_ent = NULL;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001624 sn = net_generic(net, sunrpc_net_id);
1625 remove_proc_entry(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001626}
1627
1628#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001629static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001630{
1631 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001632 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001633
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001634 sn = net_generic(net, sunrpc_net_id);
1635 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001636 if (cd->u.procfs.proc_ent == NULL)
1637 goto out_nomem;
1638 cd->u.procfs.channel_ent = NULL;
1639 cd->u.procfs.content_ent = NULL;
1640
1641 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1642 cd->u.procfs.proc_ent,
1643 &cache_flush_operations_procfs, cd);
1644 cd->u.procfs.flush_ent = p;
1645 if (p == NULL)
1646 goto out_nomem;
1647
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001648 if (cd->cache_request || cd->cache_parse) {
Trond Myklebust173912a2009-08-09 15:14:29 -04001649 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1650 cd->u.procfs.proc_ent,
1651 &cache_file_operations_procfs, cd);
1652 cd->u.procfs.channel_ent = p;
1653 if (p == NULL)
1654 goto out_nomem;
1655 }
1656 if (cd->cache_show) {
Yanchuan Nianec168672013-01-04 19:45:35 +08001657 p = proc_create_data("content", S_IFREG|S_IRUSR,
Trond Myklebust173912a2009-08-09 15:14:29 -04001658 cd->u.procfs.proc_ent,
1659 &content_file_operations_procfs, cd);
1660 cd->u.procfs.content_ent = p;
1661 if (p == NULL)
1662 goto out_nomem;
1663 }
1664 return 0;
1665out_nomem:
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001666 remove_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001667 return -ENOMEM;
1668}
1669#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001670static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001671{
1672 return 0;
1673}
1674#endif
1675
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001676void __init cache_initialize(void)
1677{
Tejun Heo203b42f2012-08-21 13:18:23 -07001678 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001679}
1680
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001681int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001682{
1683 int ret;
1684
1685 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001686 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001687 if (ret)
1688 sunrpc_destroy_cache_detail(cd);
1689 return ret;
1690}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001691EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001692
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001693void cache_unregister_net(struct cache_detail *cd, struct net *net)
1694{
1695 remove_cache_proc_entries(cd, net);
1696 sunrpc_destroy_cache_detail(cd);
1697}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001698EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001699
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001700struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001701{
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001702 struct cache_detail *cd;
Kinglong Mee129e5822015-07-27 11:10:15 +08001703 int i;
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001704
1705 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1706 if (cd == NULL)
1707 return ERR_PTR(-ENOMEM);
1708
Kinglong Mee129e5822015-07-27 11:10:15 +08001709 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct hlist_head),
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001710 GFP_KERNEL);
1711 if (cd->hash_table == NULL) {
1712 kfree(cd);
1713 return ERR_PTR(-ENOMEM);
1714 }
Kinglong Mee129e5822015-07-27 11:10:15 +08001715
1716 for (i = 0; i < cd->hash_size; i++)
1717 INIT_HLIST_HEAD(&cd->hash_table[i]);
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001718 cd->net = net;
1719 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001720}
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001721EXPORT_SYMBOL_GPL(cache_create_net);
1722
1723void cache_destroy_net(struct cache_detail *cd, struct net *net)
1724{
1725 kfree(cd->hash_table);
1726 kfree(cd);
1727}
1728EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001729
1730static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1731 size_t count, loff_t *ppos)
1732{
Al Viro496ad9a2013-01-23 17:07:38 -05001733 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001734
1735 return cache_read(filp, buf, count, ppos, cd);
1736}
1737
1738static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1739 size_t count, loff_t *ppos)
1740{
Al Viro496ad9a2013-01-23 17:07:38 -05001741 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001742
1743 return cache_write(filp, buf, count, ppos, cd);
1744}
1745
1746static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1747{
Al Viro496ad9a2013-01-23 17:07:38 -05001748 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001749
1750 return cache_poll(filp, wait, cd);
1751}
1752
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001753static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001754 unsigned int cmd, unsigned long arg)
1755{
Al Viro496ad9a2013-01-23 17:07:38 -05001756 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001757 struct cache_detail *cd = RPC_I(inode)->private;
1758
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001759 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001760}
1761
1762static int cache_open_pipefs(struct inode *inode, struct file *filp)
1763{
1764 struct cache_detail *cd = RPC_I(inode)->private;
1765
1766 return cache_open(inode, filp, cd);
1767}
1768
1769static int cache_release_pipefs(struct inode *inode, struct file *filp)
1770{
1771 struct cache_detail *cd = RPC_I(inode)->private;
1772
1773 return cache_release(inode, filp, cd);
1774}
1775
1776const struct file_operations cache_file_operations_pipefs = {
1777 .owner = THIS_MODULE,
1778 .llseek = no_llseek,
1779 .read = cache_read_pipefs,
1780 .write = cache_write_pipefs,
1781 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001782 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001783 .open = cache_open_pipefs,
1784 .release = cache_release_pipefs,
1785};
1786
1787static int content_open_pipefs(struct inode *inode, struct file *filp)
1788{
1789 struct cache_detail *cd = RPC_I(inode)->private;
1790
1791 return content_open(inode, filp, cd);
1792}
1793
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001794static int content_release_pipefs(struct inode *inode, struct file *filp)
1795{
1796 struct cache_detail *cd = RPC_I(inode)->private;
1797
1798 return content_release(inode, filp, cd);
1799}
1800
Trond Myklebust8854e822009-08-09 15:14:30 -04001801const struct file_operations content_file_operations_pipefs = {
1802 .open = content_open_pipefs,
1803 .read = seq_read,
1804 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001805 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001806};
1807
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001808static int open_flush_pipefs(struct inode *inode, struct file *filp)
1809{
1810 struct cache_detail *cd = RPC_I(inode)->private;
1811
1812 return open_flush(inode, filp, cd);
1813}
1814
1815static int release_flush_pipefs(struct inode *inode, struct file *filp)
1816{
1817 struct cache_detail *cd = RPC_I(inode)->private;
1818
1819 return release_flush(inode, filp, cd);
1820}
1821
Trond Myklebust8854e822009-08-09 15:14:30 -04001822static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1823 size_t count, loff_t *ppos)
1824{
Al Viro496ad9a2013-01-23 17:07:38 -05001825 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001826
1827 return read_flush(filp, buf, count, ppos, cd);
1828}
1829
1830static ssize_t write_flush_pipefs(struct file *filp,
1831 const char __user *buf,
1832 size_t count, loff_t *ppos)
1833{
Al Viro496ad9a2013-01-23 17:07:38 -05001834 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001835
1836 return write_flush(filp, buf, count, ppos, cd);
1837}
1838
1839const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001840 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001841 .read = read_flush_pipefs,
1842 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001843 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001844 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001845};
1846
1847int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001848 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001849 struct cache_detail *cd)
1850{
Al Viroa95e6912013-07-14 16:43:54 +04001851 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1852 if (IS_ERR(dir))
1853 return PTR_ERR(dir);
1854 cd->u.pipefs.dir = dir;
1855 return 0;
Trond Myklebust8854e822009-08-09 15:14:30 -04001856}
1857EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1858
1859void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1860{
1861 rpc_remove_cache_dir(cd->u.pipefs.dir);
1862 cd->u.pipefs.dir = NULL;
Trond Myklebust8854e822009-08-09 15:14:30 -04001863}
1864EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1865