blob: 4a2340a5440115dd4b758d4490793306b2bb4641 [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
Adrian Bunk74cae612006-03-27 01:15:10 -080044static void cache_init(struct cache_head *h)
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;
51 h->last_refresh = now;
52}
53
NeilBrown15a5f6b2006-03-27 01:15:02 -080054struct cache_head *sunrpc_cache_lookup(struct cache_detail *detail,
55 struct cache_head *key, int hash)
56{
Kinglong Mee129e5822015-07-27 11:10:15 +080057 struct cache_head *new = NULL, *freeme = NULL, *tmp = NULL;
58 struct hlist_head *head;
NeilBrown15a5f6b2006-03-27 01:15:02 -080059
60 head = &detail->hash_table[hash];
61
62 read_lock(&detail->hash_lock);
63
Kinglong Mee129e5822015-07-27 11:10:15 +080064 hlist_for_each_entry(tmp, head, cache_list) {
NeilBrown15a5f6b2006-03-27 01:15:02 -080065 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110066 if (cache_is_expired(detail, tmp))
67 /* This entry is expired, we will discard it. */
68 break;
NeilBrown15a5f6b2006-03-27 01:15:02 -080069 cache_get(tmp);
70 read_unlock(&detail->hash_lock);
71 return tmp;
72 }
73 }
74 read_unlock(&detail->hash_lock);
75 /* Didn't find anything, insert an empty entry */
76
77 new = detail->alloc();
78 if (!new)
79 return NULL;
Neil Brown2f349312006-08-05 12:14:29 -070080 /* must fully initialise 'new', else
81 * we might get lose if we need to
82 * cache_put it soon.
83 */
NeilBrown15a5f6b2006-03-27 01:15:02 -080084 cache_init(new);
Neil Brown2f349312006-08-05 12:14:29 -070085 detail->init(new, key);
NeilBrown15a5f6b2006-03-27 01:15:02 -080086
87 write_lock(&detail->hash_lock);
88
89 /* check if entry appeared while we slept */
Kinglong Mee129e5822015-07-27 11:10:15 +080090 hlist_for_each_entry(tmp, head, cache_list) {
NeilBrown15a5f6b2006-03-27 01:15:02 -080091 if (detail->match(tmp, key)) {
NeilBrownd202cce2010-02-03 17:31:31 +110092 if (cache_is_expired(detail, tmp)) {
Kinglong Mee129e5822015-07-27 11:10:15 +080093 hlist_del_init(&tmp->cache_list);
NeilBrownd202cce2010-02-03 17:31:31 +110094 detail->entries --;
95 freeme = tmp;
96 break;
97 }
NeilBrown15a5f6b2006-03-27 01:15:02 -080098 cache_get(tmp);
99 write_unlock(&detail->hash_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800100 cache_put(new, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800101 return tmp;
102 }
103 }
Kinglong Mee129e5822015-07-27 11:10:15 +0800104
105 hlist_add_head(&new->cache_list, head);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800106 detail->entries++;
107 cache_get(new);
108 write_unlock(&detail->hash_lock);
109
NeilBrownd202cce2010-02-03 17:31:31 +1100110 if (freeme)
111 cache_put(freeme, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800112 return new;
113}
Trond Myklebust24c37672008-12-23 16:30:12 -0500114EXPORT_SYMBOL_GPL(sunrpc_cache_lookup);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800115
NeilBrownebd0cb12006-03-27 01:15:08 -0800116
NeilBrownf866a812009-08-04 15:22:38 +1000117static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch);
NeilBrownebd0cb12006-03-27 01:15:08 -0800118
NeilBrown908329f2009-09-09 16:32:54 +1000119static void cache_fresh_locked(struct cache_head *head, time_t expiry)
NeilBrownebd0cb12006-03-27 01:15:08 -0800120{
121 head->expiry_time = expiry;
NeilBrownc5b29f82010-08-12 16:55:22 +1000122 head->last_refresh = seconds_since_boot();
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500123 smp_wmb(); /* paired with smp_rmb() in cache_is_valid() */
NeilBrown908329f2009-09-09 16:32:54 +1000124 set_bit(CACHE_VALID, &head->flags);
NeilBrownebd0cb12006-03-27 01:15:08 -0800125}
126
127static void cache_fresh_unlocked(struct cache_head *head,
NeilBrown908329f2009-09-09 16:32:54 +1000128 struct cache_detail *detail)
NeilBrownebd0cb12006-03-27 01:15:08 -0800129{
NeilBrownebd0cb12006-03-27 01:15:08 -0800130 if (test_and_clear_bit(CACHE_PENDING, &head->flags)) {
131 cache_revisit_request(head);
NeilBrownf866a812009-08-04 15:22:38 +1000132 cache_dequeue(detail, head);
NeilBrownebd0cb12006-03-27 01:15:08 -0800133 }
134}
135
NeilBrown15a5f6b2006-03-27 01:15:02 -0800136struct cache_head *sunrpc_cache_update(struct cache_detail *detail,
137 struct cache_head *new, struct cache_head *old, int hash)
138{
139 /* The 'old' entry is to be replaced by 'new'.
140 * If 'old' is not VALID, we update it directly,
141 * otherwise we need to replace it
142 */
NeilBrown15a5f6b2006-03-27 01:15:02 -0800143 struct cache_head *tmp;
144
145 if (!test_bit(CACHE_VALID, &old->flags)) {
146 write_lock(&detail->hash_lock);
147 if (!test_bit(CACHE_VALID, &old->flags)) {
148 if (test_bit(CACHE_NEGATIVE, &new->flags))
149 set_bit(CACHE_NEGATIVE, &old->flags);
150 else
151 detail->update(old, new);
NeilBrown908329f2009-09-09 16:32:54 +1000152 cache_fresh_locked(old, new->expiry_time);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800153 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000154 cache_fresh_unlocked(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800155 return old;
156 }
157 write_unlock(&detail->hash_lock);
158 }
159 /* We need to insert a new entry */
160 tmp = detail->alloc();
161 if (!tmp) {
NeilBrownbaab9352006-03-27 01:15:09 -0800162 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800163 return NULL;
164 }
165 cache_init(tmp);
166 detail->init(tmp, old);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800167
168 write_lock(&detail->hash_lock);
169 if (test_bit(CACHE_NEGATIVE, &new->flags))
170 set_bit(CACHE_NEGATIVE, &tmp->flags);
171 else
172 detail->update(tmp, new);
Kinglong Mee129e5822015-07-27 11:10:15 +0800173 hlist_add_head(&tmp->cache_list, &detail->hash_table[hash]);
NeilBrownf2d39582006-05-22 22:35:25 -0700174 detail->entries++;
NeilBrown15a5f6b2006-03-27 01:15:02 -0800175 cache_get(tmp);
NeilBrown908329f2009-09-09 16:32:54 +1000176 cache_fresh_locked(tmp, new->expiry_time);
NeilBrownebd0cb12006-03-27 01:15:08 -0800177 cache_fresh_locked(old, 0);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800178 write_unlock(&detail->hash_lock);
NeilBrown908329f2009-09-09 16:32:54 +1000179 cache_fresh_unlocked(tmp, detail);
180 cache_fresh_unlocked(old, detail);
NeilBrownbaab9352006-03-27 01:15:09 -0800181 cache_put(old, detail);
NeilBrown15a5f6b2006-03-27 01:15:02 -0800182 return tmp;
183}
Trond Myklebust24c37672008-12-23 16:30:12 -0500184EXPORT_SYMBOL_GPL(sunrpc_cache_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400186static int cache_make_upcall(struct cache_detail *cd, struct cache_head *h)
187{
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +0300188 if (cd->cache_upcall)
189 return cd->cache_upcall(cd, h);
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +0300190 return sunrpc_cache_pipe_upcall(cd, h);
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400191}
NeilBrown989a19b2009-08-04 15:22:38 +1000192
chaoting fanb6040f92013-03-28 22:19:45 +0800193static inline int cache_is_valid(struct cache_head *h)
NeilBrown989a19b2009-08-04 15:22:38 +1000194{
NeilBrownd202cce2010-02-03 17:31:31 +1100195 if (!test_bit(CACHE_VALID, &h->flags))
NeilBrown989a19b2009-08-04 15:22:38 +1000196 return -EAGAIN;
197 else {
198 /* entry is valid */
199 if (test_bit(CACHE_NEGATIVE, &h->flags))
200 return -ENOENT;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500201 else {
202 /*
203 * In combination with write barrier in
204 * sunrpc_cache_update, ensures that anyone
205 * using the cache entry after this sees the
206 * updated contents:
207 */
208 smp_rmb();
NeilBrown989a19b2009-08-04 15:22:38 +1000209 return 0;
J. Bruce Fieldsfdef7aa2011-01-04 14:12:47 -0500210 }
NeilBrown989a19b2009-08-04 15:22:38 +1000211 }
212}
J. Bruce Fieldse9dc1222009-08-21 11:27:29 -0400213
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500214static int try_to_negate_entry(struct cache_detail *detail, struct cache_head *h)
215{
216 int rv;
217
218 write_lock(&detail->hash_lock);
chaoting fanb6040f92013-03-28 22:19:45 +0800219 rv = cache_is_valid(h);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000220 if (rv == -EAGAIN) {
221 set_bit(CACHE_NEGATIVE, &h->flags);
222 cache_fresh_locked(h, seconds_since_boot()+CACHE_NEW_EXPIRY);
223 rv = -ENOENT;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500224 }
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500225 write_unlock(&detail->hash_lock);
226 cache_fresh_unlocked(h, detail);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000227 return rv;
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/*
231 * This is the generic cache management routine for all
232 * the authentication caches.
233 * It checks the currency of a cache item and will (later)
234 * initiate an upcall to fill it if needed.
235 *
236 *
237 * Returns 0 if the cache_head can be used, or cache_puts it and returns
NeilBrown989a19b2009-08-04 15:22:38 +1000238 * -EAGAIN if upcall is pending and request has been queued
239 * -ETIMEDOUT if upcall failed or request could not be queue or
240 * upcall completed but item is still invalid (implying that
241 * the cache item has been replaced with a newer one).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * -ENOENT if cache entry was negative
243 */
244int cache_check(struct cache_detail *detail,
245 struct cache_head *h, struct cache_req *rqstp)
246{
247 int rv;
248 long refresh_age, age;
249
250 /* First decide return status as best we can */
chaoting fanb6040f92013-03-28 22:19:45 +0800251 rv = cache_is_valid(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* now see if we want to start an upcall */
254 refresh_age = (h->expiry_time - h->last_refresh);
NeilBrownc5b29f82010-08-12 16:55:22 +1000255 age = seconds_since_boot() - h->last_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 if (rqstp == NULL) {
258 if (rv == -EAGAIN)
259 rv = -ENOENT;
NeilBrown0bebc632013-06-13 12:53:42 +1000260 } else if (rv == -EAGAIN ||
261 (h->expiry_time != 0 && age > refresh_age/2)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500262 dprintk("RPC: Want update, refage=%ld, age=%ld\n",
263 refresh_age, age);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!test_and_set_bit(CACHE_PENDING, &h->flags)) {
265 switch (cache_make_upcall(detail, h)) {
266 case -EINVAL:
J. Bruce Fields6bab93f2011-01-03 15:10:27 -0500267 rv = try_to_negate_entry(detail, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 case -EAGAIN:
NeilBrown2a1c7f52013-06-13 12:53:42 +1000270 cache_fresh_unlocked(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 break;
272 }
273 }
274 }
275
NeilBrown989a19b2009-08-04 15:22:38 +1000276 if (rv == -EAGAIN) {
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500277 if (!cache_defer_req(rqstp, h)) {
278 /*
279 * Request was not deferred; handle it as best
280 * we can ourselves:
281 */
chaoting fanb6040f92013-03-28 22:19:45 +0800282 rv = cache_is_valid(h);
NeilBrown989a19b2009-08-04 15:22:38 +1000283 if (rv == -EAGAIN)
284 rv = -ETIMEDOUT;
285 }
286 }
NeilBrown4013ede2006-03-27 01:15:07 -0800287 if (rv)
NeilBrownbaab9352006-03-27 01:15:09 -0800288 cache_put(h, detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return rv;
290}
Trond Myklebust24c37672008-12-23 16:30:12 -0500291EXPORT_SYMBOL_GPL(cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/*
294 * caches need to be periodically cleaned.
295 * For this we maintain a list of cache_detail and
296 * a current pointer into that list and into the table
297 * for that entry.
298 *
NeilBrown013920e2013-06-13 12:53:42 +1000299 * Each time cache_clean is called it finds the next non-empty entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * in the current table and walks the list in that entry
301 * looking for entries that can be removed.
302 *
303 * An entry gets removed if:
304 * - The expiry is before current time
305 * - The last_refresh time is before the flush_time for that cache
306 *
307 * later we might drop old entries with non-NEVER expiry if that table
308 * is getting 'full' for some definition of 'full'
309 *
310 * The question of "how often to scan a table" is an interesting one
311 * and is answered in part by the use of the "nextcheck" field in the
312 * cache_detail.
313 * When a scan of a table begins, the nextcheck field is set to a time
314 * that is well into the future.
315 * While scanning, if an expiry time is found that is earlier than the
316 * current nextcheck time, nextcheck is set to that expiry time.
317 * If the flush_time is ever set to a time earlier than the nextcheck
318 * time, the nextcheck time is then set to that flush_time.
319 *
320 * A table is then only scanned if the current time is at least
321 * the nextcheck time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800322 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
324
325static LIST_HEAD(cache_list);
326static DEFINE_SPINLOCK(cache_list_lock);
327static struct cache_detail *current_detail;
328static int current_index;
329
David Howells65f27f32006-11-22 14:55:48 +0000330static void do_cache_clean(struct work_struct *work);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +0300331static struct delayed_work cache_cleaner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300333void sunrpc_init_cache_detail(struct cache_detail *cd)
J. Bruce Fieldsffe93862007-11-12 17:04:29 -0500334{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 rwlock_init(&cd->hash_lock);
336 INIT_LIST_HEAD(&cd->queue);
337 spin_lock(&cache_list_lock);
338 cd->nextcheck = 0;
339 cd->entries = 0;
340 atomic_set(&cd->readers, 0);
341 cd->last_close = 0;
342 cd->last_warn = -1;
343 list_add(&cd->others, &cache_list);
344 spin_unlock(&cache_list_lock);
345
346 /* start the cleaning process */
David Howells52bad642006-11-22 14:54:01 +0000347 schedule_delayed_work(&cache_cleaner, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300349EXPORT_SYMBOL_GPL(sunrpc_init_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300351void sunrpc_destroy_cache_detail(struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 cache_purge(cd);
354 spin_lock(&cache_list_lock);
355 write_lock(&cd->hash_lock);
356 if (cd->entries || atomic_read(&cd->inuse)) {
357 write_unlock(&cd->hash_lock);
358 spin_unlock(&cache_list_lock);
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 if (current_detail == cd)
362 current_detail = NULL;
363 list_del_init(&cd->others);
364 write_unlock(&cd->hash_lock);
365 spin_unlock(&cache_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (list_empty(&cache_list)) {
367 /* module must be being unloaded so its safe to kill the worker */
Trond Myklebust4011cd92007-08-07 15:33:01 -0400368 cancel_delayed_work_sync(&cache_cleaner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
J. Bruce Fieldsdf95a9d2007-11-08 16:09:59 -0500370 return;
371out:
Kinglong Meeecca0632014-04-15 17:13:56 +0800372 printk(KERN_ERR "RPC: failed to unregister %s cache\n", cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
Stanislav Kinsbursky820f9442011-11-25 17:12:40 +0300374EXPORT_SYMBOL_GPL(sunrpc_destroy_cache_detail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376/* clean cache tries to find something to clean
377 * and cleans it.
378 * It returns 1 if it cleaned something,
379 * 0 if it didn't find anything this time
380 * -1 if it fell off the end of the list.
381 */
382static int cache_clean(void)
383{
384 int rv = 0;
385 struct list_head *next;
386
387 spin_lock(&cache_list_lock);
388
389 /* find a suitable table if we don't already have one */
390 while (current_detail == NULL ||
391 current_index >= current_detail->hash_size) {
392 if (current_detail)
393 next = current_detail->others.next;
394 else
395 next = cache_list.next;
396 if (next == &cache_list) {
397 current_detail = NULL;
398 spin_unlock(&cache_list_lock);
399 return -1;
400 }
401 current_detail = list_entry(next, struct cache_detail, others);
NeilBrownc5b29f82010-08-12 16:55:22 +1000402 if (current_detail->nextcheck > seconds_since_boot())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 current_index = current_detail->hash_size;
404 else {
405 current_index = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +1000406 current_detail->nextcheck = seconds_since_boot()+30*60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 }
409
410 /* find a non-empty bucket in the table */
411 while (current_detail &&
412 current_index < current_detail->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +0800413 hlist_empty(&current_detail->hash_table[current_index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 current_index++;
415
416 /* find a cleanable entry in the bucket and clean it, or set to next bucket */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (current_detail && current_index < current_detail->hash_size) {
Kinglong Mee129e5822015-07-27 11:10:15 +0800419 struct cache_head *ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct cache_detail *d;
Kinglong Mee129e5822015-07-27 11:10:15 +0800421 struct hlist_head *head;
422 struct hlist_node *tmp;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 write_lock(&current_detail->hash_lock);
425
426 /* Ok, now to clean this strand */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800427
Kinglong Mee129e5822015-07-27 11:10:15 +0800428 head = &current_detail->hash_table[current_index];
429 hlist_for_each_entry_safe(ch, tmp, head, cache_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (current_detail->nextcheck > ch->expiry_time)
431 current_detail->nextcheck = ch->expiry_time+1;
NeilBrown2f50d8b2010-02-03 17:31:31 +1100432 if (!cache_is_expired(current_detail, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Kinglong Mee129e5822015-07-27 11:10:15 +0800435 hlist_del_init(&ch->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 current_detail->entries--;
437 rv = 1;
NeilBrown3af49742010-02-03 17:31:31 +1100438 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
NeilBrown3af49742010-02-03 17:31:31 +1100440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 write_unlock(&current_detail->hash_lock);
442 d = current_detail;
443 if (!ch)
444 current_index ++;
445 spin_unlock(&cache_list_lock);
NeilBrown5c4d2632009-08-04 15:22:38 +1000446 if (ch) {
NeilBrown013920e2013-06-13 12:53:42 +1000447 set_bit(CACHE_CLEANED, &ch->flags);
NeilBrown2a1c7f52013-06-13 12:53:42 +1000448 cache_fresh_unlocked(ch, d);
NeilBrownbaab9352006-03-27 01:15:09 -0800449 cache_put(ch, d);
NeilBrown5c4d2632009-08-04 15:22:38 +1000450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 } else
452 spin_unlock(&cache_list_lock);
453
454 return rv;
455}
456
457/*
458 * We want to regularly clean the cache, so we need to schedule some work ...
459 */
David Howells65f27f32006-11-22 14:55:48 +0000460static void do_cache_clean(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 int delay = 5;
463 if (cache_clean() == -1)
Anton Blanchard6aad89c2009-06-10 12:52:21 -0700464 delay = round_jiffies_relative(30*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (list_empty(&cache_list))
467 delay = 0;
468
469 if (delay)
470 schedule_delayed_work(&cache_cleaner, delay);
471}
472
473
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800474/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * Clean all caches promptly. This just calls cache_clean
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800476 * repeatedly until we are sure that every cache has had a chance to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * be fully cleaned
478 */
479void cache_flush(void)
480{
481 while (cache_clean() != -1)
482 cond_resched();
483 while (cache_clean() != -1)
484 cond_resched();
485}
Trond Myklebust24c37672008-12-23 16:30:12 -0500486EXPORT_SYMBOL_GPL(cache_flush);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488void cache_purge(struct cache_detail *detail)
489{
490 detail->flush_time = LONG_MAX;
NeilBrownc5b29f82010-08-12 16:55:22 +1000491 detail->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 cache_flush();
493 detail->flush_time = 1;
494}
Trond Myklebust24c37672008-12-23 16:30:12 -0500495EXPORT_SYMBOL_GPL(cache_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497
498/*
499 * Deferral and Revisiting of Requests.
500 *
501 * If a cache lookup finds a pending entry, we
502 * need to defer the request and revisit it later.
503 * All deferred requests are stored in a hash table,
504 * indexed by "struct cache_head *".
505 * As it may be wasteful to store a whole request
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800506 * structure, we allow the request to provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * deferred form, which must contain a
508 * 'struct cache_deferred_req'
509 * This cache_deferred_req contains a method to allow
510 * it to be revisited when cache info is available
511 */
512
513#define DFR_HASHSIZE (PAGE_SIZE/sizeof(struct list_head))
514#define DFR_HASH(item) ((((long)item)>>4 ^ (((long)item)>>13)) % DFR_HASHSIZE)
515
516#define DFR_MAX 300 /* ??? */
517
518static DEFINE_SPINLOCK(cache_defer_lock);
519static LIST_HEAD(cache_defer_list);
NeilBrown11174492010-08-12 17:04:08 +1000520static struct hlist_head cache_defer_hash[DFR_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static int cache_defer_cnt;
522
J. Bruce Fields6610f722010-08-26 13:19:52 -0400523static void __unhash_deferred_req(struct cache_deferred_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
NeilBrown11174492010-08-12 17:04:08 +1000525 hlist_del_init(&dreq->hash);
NeilBrowne33534d2010-10-07 15:29:46 +1100526 if (!list_empty(&dreq->recent)) {
527 list_del_init(&dreq->recent);
528 cache_defer_cnt--;
529 }
J. Bruce Fields6610f722010-08-26 13:19:52 -0400530}
531
532static void __hash_deferred_req(struct cache_deferred_req *dreq, struct cache_head *item)
533{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int hash = DFR_HASH(item);
535
NeilBrowne33534d2010-10-07 15:29:46 +1100536 INIT_LIST_HEAD(&dreq->recent);
NeilBrown11174492010-08-12 17:04:08 +1000537 hlist_add_head(&dreq->hash, &cache_defer_hash[hash]);
J. Bruce Fields6610f722010-08-26 13:19:52 -0400538}
539
NeilBrowne33534d2010-10-07 15:29:46 +1100540static void setup_deferral(struct cache_deferred_req *dreq,
541 struct cache_head *item,
542 int count_me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 dreq->item = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 spin_lock(&cache_defer_lock);
548
J. Bruce Fields6610f722010-08-26 13:19:52 -0400549 __hash_deferred_req(dreq, item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
NeilBrowne33534d2010-10-07 15:29:46 +1100551 if (count_me) {
552 cache_defer_cnt++;
553 list_add(&dreq->recent, &cache_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
NeilBrowne33534d2010-10-07 15:29:46 +1100555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 spin_unlock(&cache_defer_lock);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
J. Bruce Fields3211af12010-08-26 16:56:23 -0400560struct thread_deferred_req {
561 struct cache_deferred_req handle;
562 struct completion completion;
563};
564
565static void cache_restart_thread(struct cache_deferred_req *dreq, int too_many)
566{
567 struct thread_deferred_req *dr =
568 container_of(dreq, struct thread_deferred_req, handle);
569 complete(&dr->completion);
570}
571
NeilBrownd29068c2010-10-07 15:29:46 +1100572static void cache_wait_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400573{
574 struct thread_deferred_req sleeper;
575 struct cache_deferred_req *dreq = &sleeper.handle;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400576
577 sleeper.completion = COMPLETION_INITIALIZER_ONSTACK(sleeper.completion);
578 dreq->revisit = cache_restart_thread;
579
NeilBrowne33534d2010-10-07 15:29:46 +1100580 setup_deferral(dreq, item, 0);
J. Bruce Fields3211af12010-08-26 16:56:23 -0400581
NeilBrownd29068c2010-10-07 15:29:46 +1100582 if (!test_bit(CACHE_PENDING, &item->flags) ||
NeilBrown277f68d2010-09-22 12:55:06 +1000583 wait_for_completion_interruptible_timeout(
J. Bruce Fields3211af12010-08-26 16:56:23 -0400584 &sleeper.completion, req->thread_wait) <= 0) {
585 /* The completion wasn't completed, so we need
586 * to clean up
587 */
588 spin_lock(&cache_defer_lock);
NeilBrown11174492010-08-12 17:04:08 +1000589 if (!hlist_unhashed(&sleeper.handle.hash)) {
J. Bruce Fields3211af12010-08-26 16:56:23 -0400590 __unhash_deferred_req(&sleeper.handle);
591 spin_unlock(&cache_defer_lock);
592 } else {
593 /* cache_revisit_request already removed
594 * this from the hash table, but hasn't
595 * called ->revisit yet. It will very soon
596 * and we need to wait for it.
597 */
598 spin_unlock(&cache_defer_lock);
599 wait_for_completion(&sleeper.completion);
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
J. Bruce Fields3211af12010-08-26 16:56:23 -0400602}
603
NeilBrowne33534d2010-10-07 15:29:46 +1100604static void cache_limit_defers(void)
605{
606 /* Make sure we haven't exceed the limit of allowed deferred
607 * requests.
608 */
609 struct cache_deferred_req *discard = NULL;
610
611 if (cache_defer_cnt <= DFR_MAX)
612 return;
613
614 spin_lock(&cache_defer_lock);
615
616 /* Consider removing either the first or the last */
617 if (cache_defer_cnt > DFR_MAX) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500618 if (prandom_u32() & 1)
NeilBrowne33534d2010-10-07 15:29:46 +1100619 discard = list_entry(cache_defer_list.next,
620 struct cache_deferred_req, recent);
621 else
622 discard = list_entry(cache_defer_list.prev,
623 struct cache_deferred_req, recent);
624 __unhash_deferred_req(discard);
625 }
626 spin_unlock(&cache_defer_lock);
627 if (discard)
628 discard->revisit(discard, 1);
629}
630
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500631/* Return true if and only if a deferred request is queued. */
632static bool cache_defer_req(struct cache_req *req, struct cache_head *item)
J. Bruce Fields3211af12010-08-26 16:56:23 -0400633{
634 struct cache_deferred_req *dreq;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400635
J. Bruce Fields3211af12010-08-26 16:56:23 -0400636 if (req->thread_wait) {
NeilBrownd29068c2010-10-07 15:29:46 +1100637 cache_wait_req(req, item);
638 if (!test_bit(CACHE_PENDING, &item->flags))
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500639 return false;
J. Bruce Fields3211af12010-08-26 16:56:23 -0400640 }
641 dreq = req->defer(req);
642 if (dreq == NULL)
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500643 return false;
NeilBrowne33534d2010-10-07 15:29:46 +1100644 setup_deferral(dreq, item, 1);
NeilBrownd29068c2010-10-07 15:29:46 +1100645 if (!test_bit(CACHE_PENDING, &item->flags))
646 /* Bit could have been cleared before we managed to
647 * set up the deferral, so need to revisit just in case
648 */
649 cache_revisit_request(item);
NeilBrowne33534d2010-10-07 15:29:46 +1100650
651 cache_limit_defers();
J. Bruce Fieldsd76d1812011-01-02 21:28:34 -0500652 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655static void cache_revisit_request(struct cache_head *item)
656{
657 struct cache_deferred_req *dreq;
658 struct list_head pending;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800659 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int hash = DFR_HASH(item);
661
662 INIT_LIST_HEAD(&pending);
663 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800664
Sasha Levinb67bfe02013-02-27 17:06:00 -0800665 hlist_for_each_entry_safe(dreq, tmp, &cache_defer_hash[hash], hash)
NeilBrown11174492010-08-12 17:04:08 +1000666 if (dreq->item == item) {
667 __unhash_deferred_req(dreq);
668 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
NeilBrown11174492010-08-12 17:04:08 +1000670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 spin_unlock(&cache_defer_lock);
672
673 while (!list_empty(&pending)) {
674 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
675 list_del_init(&dreq->recent);
676 dreq->revisit(dreq, 0);
677 }
678}
679
680void cache_clean_deferred(void *owner)
681{
682 struct cache_deferred_req *dreq, *tmp;
683 struct list_head pending;
684
685
686 INIT_LIST_HEAD(&pending);
687 spin_lock(&cache_defer_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 list_for_each_entry_safe(dreq, tmp, &cache_defer_list, recent) {
690 if (dreq->owner == owner) {
J. Bruce Fields6610f722010-08-26 13:19:52 -0400691 __unhash_deferred_req(dreq);
NeilBrowne95dffa2010-09-22 12:55:06 +1000692 list_add(&dreq->recent, &pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 }
695 spin_unlock(&cache_defer_lock);
696
697 while (!list_empty(&pending)) {
698 dreq = list_entry(pending.next, struct cache_deferred_req, recent);
699 list_del_init(&dreq->recent);
700 dreq->revisit(dreq, 1);
701 }
702}
703
704/*
705 * communicate with user-space
706 *
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500707 * We have a magic /proc file - /proc/sunrpc/<cachename>/channel.
708 * On read, you get a full request, or block.
709 * On write, an update request is processed.
710 * Poll works if anything to read, and always allows write.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800712 * Implemented by linked list of requests. Each open file has
J. Bruce Fieldsa490c682007-11-06 14:15:19 -0500713 * a ->private that also exists in this list. New requests are added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 * to the end and may wakeup and preceding readers.
715 * New readers are added to the head. If, on read, an item is found with
716 * CACHE_UPCALLING clear, we free it from the list.
717 *
718 */
719
720static DEFINE_SPINLOCK(queue_lock);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800721static DEFINE_MUTEX(queue_io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723struct cache_queue {
724 struct list_head list;
725 int reader; /* if 0, then request */
726};
727struct cache_request {
728 struct cache_queue q;
729 struct cache_head *item;
730 char * buf;
731 int len;
732 int readers;
733};
734struct cache_reader {
735 struct cache_queue q;
736 int offset; /* if non-0, we have a refcnt on next request */
737};
738
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300739static int cache_request(struct cache_detail *detail,
740 struct cache_request *crq)
741{
742 char *bp = crq->buf;
743 int len = PAGE_SIZE;
744
745 detail->cache_request(detail, crq->item, &bp, &len);
746 if (len < 0)
747 return -EAGAIN;
748 return PAGE_SIZE - len;
749}
750
Trond Myklebust173912a2009-08-09 15:14:29 -0400751static ssize_t cache_read(struct file *filp, char __user *buf, size_t count,
752 loff_t *ppos, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct cache_reader *rp = filp->private_data;
755 struct cache_request *rq;
Al Viro496ad9a2013-01-23 17:07:38 -0500756 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int err;
758
759 if (count == 0)
760 return 0;
761
Trond Myklebustda770052009-08-09 15:14:28 -0400762 mutex_lock(&inode->i_mutex); /* protect against multiple concurrent
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * readers on this file */
764 again:
765 spin_lock(&queue_lock);
766 /* need to find next request */
767 while (rp->q.list.next != &cd->queue &&
768 list_entry(rp->q.list.next, struct cache_queue, list)
769 ->reader) {
770 struct list_head *next = rp->q.list.next;
771 list_move(&rp->q.list, next);
772 }
773 if (rp->q.list.next == &cd->queue) {
774 spin_unlock(&queue_lock);
Trond Myklebustda770052009-08-09 15:14:28 -0400775 mutex_unlock(&inode->i_mutex);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400776 WARN_ON_ONCE(rp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 0;
778 }
779 rq = container_of(rp->q.list.next, struct cache_request, q.list);
Weston Andros Adamson0db74d92012-10-23 10:43:36 -0400780 WARN_ON_ONCE(rq->q.reader);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (rp->offset == 0)
782 rq->readers++;
783 spin_unlock(&queue_lock);
784
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +0300785 if (rq->len == 0) {
786 err = cache_request(cd, rq);
787 if (err < 0)
788 goto out;
789 rq->len = err;
790 }
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (rp->offset == 0 && !test_bit(CACHE_PENDING, &rq->item->flags)) {
793 err = -EAGAIN;
794 spin_lock(&queue_lock);
795 list_move(&rp->q.list, &rq->q.list);
796 spin_unlock(&queue_lock);
797 } else {
798 if (rp->offset + count > rq->len)
799 count = rq->len - rp->offset;
800 err = -EFAULT;
801 if (copy_to_user(buf, rq->buf + rp->offset, count))
802 goto out;
803 rp->offset += count;
804 if (rp->offset >= rq->len) {
805 rp->offset = 0;
806 spin_lock(&queue_lock);
807 list_move(&rp->q.list, &rq->q.list);
808 spin_unlock(&queue_lock);
809 }
810 err = 0;
811 }
812 out:
813 if (rp->offset == 0) {
814 /* need to release rq */
815 spin_lock(&queue_lock);
816 rq->readers--;
817 if (rq->readers == 0 &&
818 !test_bit(CACHE_PENDING, &rq->item->flags)) {
819 list_del(&rq->q.list);
820 spin_unlock(&queue_lock);
NeilBrownbaab9352006-03-27 01:15:09 -0800821 cache_put(rq->item, cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 kfree(rq->buf);
823 kfree(rq);
824 } else
825 spin_unlock(&queue_lock);
826 }
827 if (err == -EAGAIN)
828 goto again;
Trond Myklebustda770052009-08-09 15:14:28 -0400829 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return err ? err : count;
831}
832
Trond Myklebustda770052009-08-09 15:14:28 -0400833static ssize_t cache_do_downcall(char *kaddr, const char __user *buf,
834 size_t count, struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Trond Myklebustda770052009-08-09 15:14:28 -0400836 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Dan Carpenter6d8d1742012-01-18 12:56:02 +0300838 if (count == 0)
839 return -EINVAL;
Trond Myklebustda770052009-08-09 15:14:28 -0400840 if (copy_from_user(kaddr, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return -EFAULT;
Trond Myklebustda770052009-08-09 15:14:28 -0400842 kaddr[count] = '\0';
843 ret = cd->cache_parse(cd, kaddr, count);
844 if (!ret)
845 ret = count;
846 return ret;
847}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Trond Myklebustda770052009-08-09 15:14:28 -0400849static ssize_t cache_slow_downcall(const char __user *buf,
850 size_t count, struct cache_detail *cd)
851{
852 static char write_buf[8192]; /* protected by queue_io_mutex */
853 ssize_t ret = -EINVAL;
854
855 if (count >= sizeof(write_buf))
856 goto out;
857 mutex_lock(&queue_io_mutex);
858 ret = cache_do_downcall(write_buf, buf, count, cd);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800859 mutex_unlock(&queue_io_mutex);
Trond Myklebustda770052009-08-09 15:14:28 -0400860out:
861 return ret;
862}
863
864static ssize_t cache_downcall(struct address_space *mapping,
865 const char __user *buf,
866 size_t count, struct cache_detail *cd)
867{
868 struct page *page;
869 char *kaddr;
870 ssize_t ret = -ENOMEM;
871
872 if (count >= PAGE_CACHE_SIZE)
873 goto out_slow;
874
875 page = find_or_create_page(mapping, 0, GFP_KERNEL);
876 if (!page)
877 goto out_slow;
878
879 kaddr = kmap(page);
880 ret = cache_do_downcall(kaddr, buf, count, cd);
881 kunmap(page);
882 unlock_page(page);
883 page_cache_release(page);
884 return ret;
885out_slow:
886 return cache_slow_downcall(buf, count, cd);
887}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Trond Myklebust173912a2009-08-09 15:14:29 -0400889static ssize_t cache_write(struct file *filp, const char __user *buf,
890 size_t count, loff_t *ppos,
891 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Trond Myklebustda770052009-08-09 15:14:28 -0400893 struct address_space *mapping = filp->f_mapping;
Al Viro496ad9a2013-01-23 17:07:38 -0500894 struct inode *inode = file_inode(filp);
Trond Myklebustda770052009-08-09 15:14:28 -0400895 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Trond Myklebustda770052009-08-09 15:14:28 -0400897 if (!cd->cache_parse)
898 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Trond Myklebustda770052009-08-09 15:14:28 -0400900 mutex_lock(&inode->i_mutex);
901 ret = cache_downcall(mapping, buf, count, cd);
902 mutex_unlock(&inode->i_mutex);
903out:
904 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
907static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
908
Trond Myklebust173912a2009-08-09 15:14:29 -0400909static unsigned int cache_poll(struct file *filp, poll_table *wait,
910 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 unsigned int mask;
913 struct cache_reader *rp = filp->private_data;
914 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 poll_wait(filp, &queue_wait, wait);
917
918 /* alway allow write */
Al Viro1711fd9a2015-03-07 21:08:46 +0000919 mask = POLLOUT | POLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (!rp)
922 return mask;
923
924 spin_lock(&queue_lock);
925
926 for (cq= &rp->q; &cq->list != &cd->queue;
927 cq = list_entry(cq->list.next, struct cache_queue, list))
928 if (!cq->reader) {
929 mask |= POLLIN | POLLRDNORM;
930 break;
931 }
932 spin_unlock(&queue_lock);
933 return mask;
934}
935
Trond Myklebust173912a2009-08-09 15:14:29 -0400936static int cache_ioctl(struct inode *ino, struct file *filp,
937 unsigned int cmd, unsigned long arg,
938 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 int len = 0;
941 struct cache_reader *rp = filp->private_data;
942 struct cache_queue *cq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 if (cmd != FIONREAD || !rp)
945 return -EINVAL;
946
947 spin_lock(&queue_lock);
948
949 /* only find the length remaining in current request,
950 * or the length of the next request
951 */
952 for (cq= &rp->q; &cq->list != &cd->queue;
953 cq = list_entry(cq->list.next, struct cache_queue, list))
954 if (!cq->reader) {
955 struct cache_request *cr =
956 container_of(cq, struct cache_request, q);
957 len = cr->len - rp->offset;
958 break;
959 }
960 spin_unlock(&queue_lock);
961
962 return put_user(len, (int __user *)arg);
963}
964
Trond Myklebust173912a2009-08-09 15:14:29 -0400965static int cache_open(struct inode *inode, struct file *filp,
966 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 struct cache_reader *rp = NULL;
969
Trond Myklebustf7e86ab2009-08-19 18:13:00 -0400970 if (!cd || !try_module_get(cd->owner))
971 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 nonseekable_open(inode, filp);
973 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 rp = kmalloc(sizeof(*rp), GFP_KERNEL);
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400975 if (!rp) {
976 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -ENOMEM;
Alexey Khoroshilova7823c72013-03-23 00:36:44 +0400978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 rp->offset = 0;
980 rp->q.reader = 1;
981 atomic_inc(&cd->readers);
982 spin_lock(&queue_lock);
983 list_add(&rp->q.list, &cd->queue);
984 spin_unlock(&queue_lock);
985 }
986 filp->private_data = rp;
987 return 0;
988}
989
Trond Myklebust173912a2009-08-09 15:14:29 -0400990static int cache_release(struct inode *inode, struct file *filp,
991 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
993 struct cache_reader *rp = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (rp) {
996 spin_lock(&queue_lock);
997 if (rp->offset) {
998 struct cache_queue *cq;
999 for (cq= &rp->q; &cq->list != &cd->queue;
1000 cq = list_entry(cq->list.next, struct cache_queue, list))
1001 if (!cq->reader) {
1002 container_of(cq, struct cache_request, q)
1003 ->readers--;
1004 break;
1005 }
1006 rp->offset = 0;
1007 }
1008 list_del(&rp->q.list);
1009 spin_unlock(&queue_lock);
1010
1011 filp->private_data = NULL;
1012 kfree(rp);
1013
NeilBrownc5b29f82010-08-12 16:55:22 +10001014 cd->last_close = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 atomic_dec(&cd->readers);
1016 }
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001017 module_put(cd->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return 0;
1019}
1020
1021
1022
NeilBrownf866a812009-08-04 15:22:38 +10001023static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
NeilBrownf9e1aed2013-06-13 12:53:42 +10001025 struct cache_queue *cq, *tmp;
1026 struct cache_request *cr;
1027 struct list_head dequeued;
1028
1029 INIT_LIST_HEAD(&dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001031 list_for_each_entry_safe(cq, tmp, &detail->queue, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!cq->reader) {
NeilBrownf9e1aed2013-06-13 12:53:42 +10001033 cr = container_of(cq, struct cache_request, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (cr->item != ch)
1035 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001036 if (test_bit(CACHE_PENDING, &ch->flags))
1037 /* Lost a race and it is pending again */
1038 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (cr->readers != 0)
NeilBrown4013ede2006-03-27 01:15:07 -08001040 continue;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001041 list_move(&cr->q.list, &dequeued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043 spin_unlock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001044 while (!list_empty(&dequeued)) {
1045 cr = list_entry(dequeued.next, struct cache_request, q.list);
1046 list_del(&cr->q.list);
1047 cache_put(cr->item, detail);
1048 kfree(cr->buf);
1049 kfree(cr);
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053/*
1054 * Support routines for text-based upcalls.
1055 * Fields are separated by spaces.
1056 * Fields are either mangled to quote space tab newline slosh with slosh
1057 * or a hexified with a leading \x
1058 * Record is terminated with newline.
1059 *
1060 */
1061
1062void qword_add(char **bpp, int *lp, char *str)
1063{
1064 char *bp = *bpp;
1065 int len = *lp;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001066 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (len < 0) return;
1069
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001070 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1071 if (ret >= len) {
1072 bp += len;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001073 len = -1;
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001074 } else {
1075 bp += ret;
Andy Shevchenko1b2e1222014-11-28 17:50:28 +02001076 len -= ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 *bp++ = ' ';
1078 len--;
1079 }
1080 *bpp = bp;
1081 *lp = len;
1082}
Trond Myklebust24c37672008-12-23 16:30:12 -05001083EXPORT_SYMBOL_GPL(qword_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085void qword_addhex(char **bpp, int *lp, char *buf, int blen)
1086{
1087 char *bp = *bpp;
1088 int len = *lp;
1089
1090 if (len < 0) return;
1091
1092 if (len > 2) {
1093 *bp++ = '\\';
1094 *bp++ = 'x';
1095 len -= 2;
1096 while (blen && len >= 2) {
Andy Shevchenko056785e2013-12-12 15:49:21 +02001097 bp = hex_byte_pack(bp, *buf++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 len -= 2;
1099 blen--;
1100 }
1101 }
1102 if (blen || len<1) len = -1;
1103 else {
1104 *bp++ = ' ';
1105 len--;
1106 }
1107 *bpp = bp;
1108 *lp = len;
1109}
Trond Myklebust24c37672008-12-23 16:30:12 -05001110EXPORT_SYMBOL_GPL(qword_addhex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112static void warn_no_listener(struct cache_detail *detail)
1113{
1114 if (detail->last_warn != detail->last_close) {
1115 detail->last_warn = detail->last_close;
1116 if (detail->warn_no_listener)
Trond Myklebust2da8ca22009-08-09 15:14:26 -04001117 detail->warn_no_listener(detail, detail->last_close != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119}
1120
J. Bruce Fields06497522010-09-19 22:55:06 -04001121static bool cache_listeners_exist(struct cache_detail *detail)
1122{
1123 if (atomic_read(&detail->readers))
1124 return true;
1125 if (detail->last_close == 0)
1126 /* This cache was never opened */
1127 return false;
1128 if (detail->last_close < seconds_since_boot() - 30)
1129 /*
1130 * We allow for the possibility that someone might
1131 * restart a userspace daemon without restarting the
1132 * server; but after 30 seconds, we give up.
1133 */
1134 return false;
1135 return true;
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138/*
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001139 * register an upcall request to user-space and queue it up for read() by the
1140 * upcall daemon.
1141 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 * Each request is at most one page long.
1143 */
Stanislav Kinsbursky21cd1252013-02-04 14:02:55 +03001144int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146
1147 char *buf;
1148 struct cache_request *crq;
NeilBrownf9e1aed2013-06-13 12:53:42 +10001149 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001151 if (!detail->cache_request)
1152 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
J. Bruce Fields06497522010-09-19 22:55:06 -04001154 if (!cache_listeners_exist(detail)) {
1155 warn_no_listener(detail);
1156 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
NeilBrown013920e2013-06-13 12:53:42 +10001158 if (test_bit(CACHE_CLEANED, &h->flags))
1159 /* Too late to make an upcall */
1160 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1163 if (!buf)
1164 return -EAGAIN;
1165
1166 crq = kmalloc(sizeof (*crq), GFP_KERNEL);
1167 if (!crq) {
1168 kfree(buf);
1169 return -EAGAIN;
1170 }
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 crq->q.reader = 0;
1173 crq->item = cache_get(h);
1174 crq->buf = buf;
Stanislav Kinsburskyd94af6d2013-02-04 14:03:03 +03001175 crq->len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 crq->readers = 0;
1177 spin_lock(&queue_lock);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001178 if (test_bit(CACHE_PENDING, &h->flags))
1179 list_add_tail(&crq->q.list, &detail->queue);
1180 else
1181 /* Lost a race, no longer PENDING, so don't enqueue */
1182 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 spin_unlock(&queue_lock);
1184 wake_up(&queue_wait);
NeilBrownf9e1aed2013-06-13 12:53:42 +10001185 if (ret == -EAGAIN) {
1186 kfree(buf);
1187 kfree(crq);
1188 }
1189 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
Trond Myklebustbc74b4f2009-08-09 15:14:29 -04001191EXPORT_SYMBOL_GPL(sunrpc_cache_pipe_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193/*
1194 * parse a message from user-space and pass it
1195 * to an appropriate cache
1196 * Messages are, like requests, separated into fields by
1197 * spaces and dequotes as \xHEXSTRING or embedded \nnn octal
1198 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001199 * Message is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 * reply cachename expiry key ... content....
1201 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001202 * key and content are both parsed by cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 */
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205int qword_get(char **bpp, char *dest, int bufsize)
1206{
1207 /* return bytes copied, or -1 on error */
1208 char *bp = *bpp;
1209 int len = 0;
1210
1211 while (*bp == ' ') bp++;
1212
1213 if (bp[0] == '\\' && bp[1] == 'x') {
1214 /* HEX STRING */
1215 bp += 2;
Andy Shevchenkoe7f483e2010-09-21 09:40:25 +03001216 while (len < bufsize) {
1217 int h, l;
1218
1219 h = hex_to_bin(bp[0]);
1220 if (h < 0)
1221 break;
1222
1223 l = hex_to_bin(bp[1]);
1224 if (l < 0)
1225 break;
1226
1227 *dest++ = (h << 4) | l;
1228 bp += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 len++;
1230 }
1231 } else {
1232 /* text with \nnn octal quoting */
1233 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
1234 if (*bp == '\\' &&
1235 isodigit(bp[1]) && (bp[1] <= '3') &&
1236 isodigit(bp[2]) &&
1237 isodigit(bp[3])) {
1238 int byte = (*++bp -'0');
1239 bp++;
1240 byte = (byte << 3) | (*bp++ - '0');
1241 byte = (byte << 3) | (*bp++ - '0');
1242 *dest++ = byte;
1243 len++;
1244 } else {
1245 *dest++ = *bp++;
1246 len++;
1247 }
1248 }
1249 }
1250
1251 if (*bp != ' ' && *bp != '\n' && *bp != '\0')
1252 return -1;
1253 while (*bp == ' ') bp++;
1254 *bpp = bp;
1255 *dest = '\0';
1256 return len;
1257}
Trond Myklebust24c37672008-12-23 16:30:12 -05001258EXPORT_SYMBOL_GPL(qword_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260
1261/*
1262 * support /proc/sunrpc/cache/$CACHENAME/content
1263 * as a seqfile.
1264 * We call ->cache_show passing NULL for the item to
1265 * get a header, then pass each real item in the cache
1266 */
1267
Kinglong Meec8c081b2015-07-27 11:09:42 +08001268void *cache_seq_start(struct seq_file *m, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001269 __acquires(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 loff_t n = *pos;
Eric Dumazet95c96172012-04-15 05:58:06 +00001272 unsigned int hash, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct cache_head *ch;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001274 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 read_lock(&cd->hash_lock);
1277 if (!n--)
1278 return SEQ_START_TOKEN;
1279 hash = n >> 32;
1280 entry = n & ((1LL<<32) - 1);
1281
Kinglong Mee129e5822015-07-27 11:10:15 +08001282 hlist_for_each_entry(ch, &cd->hash_table[hash], cache_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (!entry--)
1284 return ch;
1285 n &= ~((1LL<<32) - 1);
1286 do {
1287 hash++;
1288 n += 1LL<<32;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001289 } while(hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001290 hlist_empty(&cd->hash_table[hash]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (hash >= cd->hash_size)
1292 return NULL;
1293 *pos = n+1;
Kinglong Mee129e5822015-07-27 11:10:15 +08001294 return hlist_entry_safe(cd->hash_table[hash].first,
1295 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
Kinglong Meec8c081b2015-07-27 11:09:42 +08001297EXPORT_SYMBOL_GPL(cache_seq_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Kinglong Meec8c081b2015-07-27 11:09:42 +08001299void *cache_seq_next(struct seq_file *m, void *p, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct cache_head *ch = p;
1302 int hash = (*pos >> 32);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001303 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 if (p == SEQ_START_TOKEN)
1306 hash = 0;
Kinglong Mee129e5822015-07-27 11:10:15 +08001307 else if (ch->cache_list.next == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 hash++;
1309 *pos += 1LL<<32;
1310 } else {
1311 ++*pos;
Kinglong Mee129e5822015-07-27 11:10:15 +08001312 return hlist_entry_safe(ch->cache_list.next,
1313 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315 *pos &= ~((1LL<<32) - 1);
1316 while (hash < cd->hash_size &&
Kinglong Mee129e5822015-07-27 11:10:15 +08001317 hlist_empty(&cd->hash_table[hash])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 hash++;
1319 *pos += 1LL<<32;
1320 }
1321 if (hash >= cd->hash_size)
1322 return NULL;
1323 ++*pos;
Kinglong Mee129e5822015-07-27 11:10:15 +08001324 return hlist_entry_safe(cd->hash_table[hash].first,
1325 struct cache_head, cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
Kinglong Meec8c081b2015-07-27 11:09:42 +08001327EXPORT_SYMBOL_GPL(cache_seq_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Kinglong Meec8c081b2015-07-27 11:09:42 +08001329void cache_seq_stop(struct seq_file *m, void *p)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001330 __releases(cd->hash_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001332 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 read_unlock(&cd->hash_lock);
1334}
Kinglong Meec8c081b2015-07-27 11:09:42 +08001335EXPORT_SYMBOL_GPL(cache_seq_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337static int c_show(struct seq_file *m, void *p)
1338{
1339 struct cache_head *cp = p;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001340 struct cache_detail *cd = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 if (p == SEQ_START_TOKEN)
1343 return cd->cache_show(m, cd, NULL);
1344
1345 ifdebug(CACHE)
NeilBrown4013ede2006-03-27 01:15:07 -08001346 seq_printf(m, "# expiry=%ld refcnt=%d flags=%lx\n",
NeilBrownc5b29f82010-08-12 16:55:22 +10001347 convert_to_wallclock(cp->expiry_time),
1348 atomic_read(&cp->ref.refcount), cp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 cache_get(cp);
1350 if (cache_check(cd, cp, NULL))
1351 /* cache_check does a cache_put on failure */
1352 seq_printf(m, "# ");
NeilBrown200724a2012-07-12 10:37:34 +10001353 else {
1354 if (cache_is_expired(cd, cp))
1355 seq_printf(m, "# ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 cache_put(cp, cd);
NeilBrown200724a2012-07-12 10:37:34 +10001357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 return cd->cache_show(m, cd, cp);
1360}
1361
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001362static const struct seq_operations cache_content_op = {
Kinglong Meec8c081b2015-07-27 11:09:42 +08001363 .start = cache_seq_start,
1364 .next = cache_seq_next,
1365 .stop = cache_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 .show = c_show,
1367};
1368
Trond Myklebust173912a2009-08-09 15:14:29 -04001369static int content_open(struct inode *inode, struct file *file,
1370 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001372 struct seq_file *seq;
1373 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001375 if (!cd || !try_module_get(cd->owner))
1376 return -EACCES;
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001377
1378 err = seq_open(file, &cache_content_op);
1379 if (err) {
Li Zefana5990ea2010-03-11 14:08:10 -08001380 module_put(cd->owner);
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001381 return err;
Li Zefana5990ea2010-03-11 14:08:10 -08001382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001384 seq = file->private_data;
1385 seq->private = cd;
Pavel Emelyanovec931032007-10-10 02:31:07 -07001386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001389static int content_release(struct inode *inode, struct file *file,
1390 struct cache_detail *cd)
1391{
Kinglong Mee9936f2a2015-07-27 11:09:10 +08001392 int ret = seq_release(inode, file);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001393 module_put(cd->owner);
1394 return ret;
1395}
1396
1397static int open_flush(struct inode *inode, struct file *file,
1398 struct cache_detail *cd)
1399{
1400 if (!cd || !try_module_get(cd->owner))
1401 return -EACCES;
1402 return nonseekable_open(inode, file);
1403}
1404
1405static int release_flush(struct inode *inode, struct file *file,
1406 struct cache_detail *cd)
1407{
1408 module_put(cd->owner);
1409 return 0;
1410}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412static ssize_t read_flush(struct file *file, char __user *buf,
Trond Myklebust173912a2009-08-09 15:14:29 -04001413 size_t count, loff_t *ppos,
1414 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Sasha Levin212ba902012-07-17 00:01:26 +02001416 char tbuf[22];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 unsigned long p = *ppos;
Chuck Lever01b29692007-10-26 13:31:20 -04001418 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Sasha Levin212ba902012-07-17 00:01:26 +02001420 snprintf(tbuf, sizeof(tbuf), "%lu\n", convert_to_wallclock(cd->flush_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 len = strlen(tbuf);
1422 if (p >= len)
1423 return 0;
1424 len -= p;
Chuck Lever01b29692007-10-26 13:31:20 -04001425 if (len > count)
1426 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (copy_to_user(buf, (void*)(tbuf+p), len))
Chuck Lever01b29692007-10-26 13:31:20 -04001428 return -EFAULT;
1429 *ppos += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return len;
1431}
1432
Trond Myklebust173912a2009-08-09 15:14:29 -04001433static ssize_t write_flush(struct file *file, const char __user *buf,
1434 size_t count, loff_t *ppos,
1435 struct cache_detail *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 char tbuf[20];
NeilBrownc5b29f82010-08-12 16:55:22 +10001438 char *bp, *ep;
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (*ppos || count > sizeof(tbuf)-1)
1441 return -EINVAL;
1442 if (copy_from_user(tbuf, buf, count))
1443 return -EFAULT;
1444 tbuf[count] = 0;
NeilBrownc5b29f82010-08-12 16:55:22 +10001445 simple_strtoul(tbuf, &ep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (*ep && *ep != '\n')
1447 return -EINVAL;
1448
NeilBrownc5b29f82010-08-12 16:55:22 +10001449 bp = tbuf;
1450 cd->flush_time = get_expiry(&bp);
1451 cd->nextcheck = seconds_since_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 cache_flush();
1453
1454 *ppos += count;
1455 return count;
1456}
1457
Trond Myklebust173912a2009-08-09 15:14:29 -04001458static ssize_t cache_read_procfs(struct file *filp, char __user *buf,
1459 size_t count, loff_t *ppos)
1460{
Al Virod9dda782013-03-31 18:16:14 -04001461 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001462
1463 return cache_read(filp, buf, count, ppos, cd);
1464}
1465
1466static ssize_t cache_write_procfs(struct file *filp, const char __user *buf,
1467 size_t count, loff_t *ppos)
1468{
Al Virod9dda782013-03-31 18:16:14 -04001469 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001470
1471 return cache_write(filp, buf, count, ppos, cd);
1472}
1473
1474static unsigned int cache_poll_procfs(struct file *filp, poll_table *wait)
1475{
Al Virod9dda782013-03-31 18:16:14 -04001476 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001477
1478 return cache_poll(filp, wait, cd);
1479}
1480
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001481static long cache_ioctl_procfs(struct file *filp,
1482 unsigned int cmd, unsigned long arg)
Trond Myklebust173912a2009-08-09 15:14:29 -04001483{
Al Viro496ad9a2013-01-23 17:07:38 -05001484 struct inode *inode = file_inode(filp);
Al Virod9dda782013-03-31 18:16:14 -04001485 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001486
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001487 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust173912a2009-08-09 15:14:29 -04001488}
1489
1490static int cache_open_procfs(struct inode *inode, struct file *filp)
1491{
Al Virod9dda782013-03-31 18:16:14 -04001492 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001493
1494 return cache_open(inode, filp, cd);
1495}
1496
1497static int cache_release_procfs(struct inode *inode, struct file *filp)
1498{
Al Virod9dda782013-03-31 18:16:14 -04001499 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001500
1501 return cache_release(inode, filp, cd);
1502}
1503
1504static const struct file_operations cache_file_operations_procfs = {
1505 .owner = THIS_MODULE,
1506 .llseek = no_llseek,
1507 .read = cache_read_procfs,
1508 .write = cache_write_procfs,
1509 .poll = cache_poll_procfs,
Frederic Weisbeckerd79b6f42010-03-30 07:27:50 +02001510 .unlocked_ioctl = cache_ioctl_procfs, /* for FIONREAD */
Trond Myklebust173912a2009-08-09 15:14:29 -04001511 .open = cache_open_procfs,
1512 .release = cache_release_procfs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513};
Trond Myklebust173912a2009-08-09 15:14:29 -04001514
1515static int content_open_procfs(struct inode *inode, struct file *filp)
1516{
Al Virod9dda782013-03-31 18:16:14 -04001517 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebust173912a2009-08-09 15:14:29 -04001518
1519 return content_open(inode, filp, cd);
1520}
1521
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001522static int content_release_procfs(struct inode *inode, struct file *filp)
1523{
Al Virod9dda782013-03-31 18:16:14 -04001524 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001525
1526 return content_release(inode, filp, cd);
1527}
1528
Trond Myklebust173912a2009-08-09 15:14:29 -04001529static const struct file_operations content_file_operations_procfs = {
1530 .open = content_open_procfs,
1531 .read = seq_read,
1532 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001533 .release = content_release_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001534};
1535
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001536static int open_flush_procfs(struct inode *inode, struct file *filp)
1537{
Al Virod9dda782013-03-31 18:16:14 -04001538 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001539
1540 return open_flush(inode, filp, cd);
1541}
1542
1543static int release_flush_procfs(struct inode *inode, struct file *filp)
1544{
Al Virod9dda782013-03-31 18:16:14 -04001545 struct cache_detail *cd = PDE_DATA(inode);
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001546
1547 return release_flush(inode, filp, cd);
1548}
1549
Trond Myklebust173912a2009-08-09 15:14:29 -04001550static ssize_t read_flush_procfs(struct file *filp, char __user *buf,
1551 size_t count, loff_t *ppos)
1552{
Al Virod9dda782013-03-31 18:16:14 -04001553 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001554
1555 return read_flush(filp, buf, count, ppos, cd);
1556}
1557
1558static ssize_t write_flush_procfs(struct file *filp,
1559 const char __user *buf,
1560 size_t count, loff_t *ppos)
1561{
Al Virod9dda782013-03-31 18:16:14 -04001562 struct cache_detail *cd = PDE_DATA(file_inode(filp));
Trond Myklebust173912a2009-08-09 15:14:29 -04001563
1564 return write_flush(filp, buf, count, ppos, cd);
1565}
1566
1567static const struct file_operations cache_flush_operations_procfs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001568 .open = open_flush_procfs,
Trond Myklebust173912a2009-08-09 15:14:29 -04001569 .read = read_flush_procfs,
1570 .write = write_flush_procfs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001571 .release = release_flush_procfs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001572 .llseek = no_llseek,
Trond Myklebust173912a2009-08-09 15:14:29 -04001573};
1574
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001575static void remove_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001576{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001577 struct sunrpc_net *sn;
1578
Trond Myklebust173912a2009-08-09 15:14:29 -04001579 if (cd->u.procfs.proc_ent == NULL)
1580 return;
1581 if (cd->u.procfs.flush_ent)
1582 remove_proc_entry("flush", cd->u.procfs.proc_ent);
1583 if (cd->u.procfs.channel_ent)
1584 remove_proc_entry("channel", cd->u.procfs.proc_ent);
1585 if (cd->u.procfs.content_ent)
1586 remove_proc_entry("content", cd->u.procfs.proc_ent);
1587 cd->u.procfs.proc_ent = NULL;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001588 sn = net_generic(net, sunrpc_net_id);
1589 remove_proc_entry(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001590}
1591
1592#ifdef CONFIG_PROC_FS
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001593static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001594{
1595 struct proc_dir_entry *p;
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001596 struct sunrpc_net *sn;
Trond Myklebust173912a2009-08-09 15:14:29 -04001597
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +04001598 sn = net_generic(net, sunrpc_net_id);
1599 cd->u.procfs.proc_ent = proc_mkdir(cd->name, sn->proc_net_rpc);
Trond Myklebust173912a2009-08-09 15:14:29 -04001600 if (cd->u.procfs.proc_ent == NULL)
1601 goto out_nomem;
1602 cd->u.procfs.channel_ent = NULL;
1603 cd->u.procfs.content_ent = NULL;
1604
1605 p = proc_create_data("flush", S_IFREG|S_IRUSR|S_IWUSR,
1606 cd->u.procfs.proc_ent,
1607 &cache_flush_operations_procfs, cd);
1608 cd->u.procfs.flush_ent = p;
1609 if (p == NULL)
1610 goto out_nomem;
1611
Stanislav Kinsbursky2d438332013-02-04 14:02:50 +03001612 if (cd->cache_request || cd->cache_parse) {
Trond Myklebust173912a2009-08-09 15:14:29 -04001613 p = proc_create_data("channel", S_IFREG|S_IRUSR|S_IWUSR,
1614 cd->u.procfs.proc_ent,
1615 &cache_file_operations_procfs, cd);
1616 cd->u.procfs.channel_ent = p;
1617 if (p == NULL)
1618 goto out_nomem;
1619 }
1620 if (cd->cache_show) {
Yanchuan Nianec168672013-01-04 19:45:35 +08001621 p = proc_create_data("content", S_IFREG|S_IRUSR,
Trond Myklebust173912a2009-08-09 15:14:29 -04001622 cd->u.procfs.proc_ent,
1623 &content_file_operations_procfs, cd);
1624 cd->u.procfs.content_ent = p;
1625 if (p == NULL)
1626 goto out_nomem;
1627 }
1628 return 0;
1629out_nomem:
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001630 remove_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001631 return -ENOMEM;
1632}
1633#else /* CONFIG_PROC_FS */
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001634static int create_cache_proc_entries(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001635{
1636 return 0;
1637}
1638#endif
1639
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001640void __init cache_initialize(void)
1641{
Tejun Heo203b42f2012-08-21 13:18:23 -07001642 INIT_DEFERRABLE_WORK(&cache_cleaner, do_cache_clean);
Artem Bityutskiy8eab9452010-07-01 18:05:56 +03001643}
1644
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001645int cache_register_net(struct cache_detail *cd, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001646{
1647 int ret;
1648
1649 sunrpc_init_cache_detail(cd);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001650 ret = create_cache_proc_entries(cd, net);
Trond Myklebust173912a2009-08-09 15:14:29 -04001651 if (ret)
1652 sunrpc_destroy_cache_detail(cd);
1653 return ret;
1654}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001655EXPORT_SYMBOL_GPL(cache_register_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001656
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001657void cache_unregister_net(struct cache_detail *cd, struct net *net)
1658{
1659 remove_cache_proc_entries(cd, net);
1660 sunrpc_destroy_cache_detail(cd);
1661}
Stanislav Kinsburskyf5c8593b2011-12-07 12:57:56 +03001662EXPORT_SYMBOL_GPL(cache_unregister_net);
Pavel Emelyanov593ce162010-09-27 14:00:15 +04001663
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001664struct cache_detail *cache_create_net(struct cache_detail *tmpl, struct net *net)
Trond Myklebust173912a2009-08-09 15:14:29 -04001665{
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001666 struct cache_detail *cd;
Kinglong Mee129e5822015-07-27 11:10:15 +08001667 int i;
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001668
1669 cd = kmemdup(tmpl, sizeof(struct cache_detail), GFP_KERNEL);
1670 if (cd == NULL)
1671 return ERR_PTR(-ENOMEM);
1672
Kinglong Mee129e5822015-07-27 11:10:15 +08001673 cd->hash_table = kzalloc(cd->hash_size * sizeof(struct hlist_head),
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001674 GFP_KERNEL);
1675 if (cd->hash_table == NULL) {
1676 kfree(cd);
1677 return ERR_PTR(-ENOMEM);
1678 }
Kinglong Mee129e5822015-07-27 11:10:15 +08001679
1680 for (i = 0; i < cd->hash_size; i++)
1681 INIT_HLIST_HEAD(&cd->hash_table[i]);
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001682 cd->net = net;
1683 return cd;
Trond Myklebust173912a2009-08-09 15:14:29 -04001684}
Stanislav Kinsbursky0a402d52012-01-19 21:42:21 +04001685EXPORT_SYMBOL_GPL(cache_create_net);
1686
1687void cache_destroy_net(struct cache_detail *cd, struct net *net)
1688{
1689 kfree(cd->hash_table);
1690 kfree(cd);
1691}
1692EXPORT_SYMBOL_GPL(cache_destroy_net);
Trond Myklebust8854e822009-08-09 15:14:30 -04001693
1694static ssize_t cache_read_pipefs(struct file *filp, char __user *buf,
1695 size_t count, loff_t *ppos)
1696{
Al Viro496ad9a2013-01-23 17:07:38 -05001697 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001698
1699 return cache_read(filp, buf, count, ppos, cd);
1700}
1701
1702static ssize_t cache_write_pipefs(struct file *filp, const char __user *buf,
1703 size_t count, loff_t *ppos)
1704{
Al Viro496ad9a2013-01-23 17:07:38 -05001705 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001706
1707 return cache_write(filp, buf, count, ppos, cd);
1708}
1709
1710static unsigned int cache_poll_pipefs(struct file *filp, poll_table *wait)
1711{
Al Viro496ad9a2013-01-23 17:07:38 -05001712 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001713
1714 return cache_poll(filp, wait, cd);
1715}
1716
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001717static long cache_ioctl_pipefs(struct file *filp,
Trond Myklebust8854e822009-08-09 15:14:30 -04001718 unsigned int cmd, unsigned long arg)
1719{
Al Viro496ad9a2013-01-23 17:07:38 -05001720 struct inode *inode = file_inode(filp);
Trond Myklebust8854e822009-08-09 15:14:30 -04001721 struct cache_detail *cd = RPC_I(inode)->private;
1722
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +02001723 return cache_ioctl(inode, filp, cmd, arg, cd);
Trond Myklebust8854e822009-08-09 15:14:30 -04001724}
1725
1726static int cache_open_pipefs(struct inode *inode, struct file *filp)
1727{
1728 struct cache_detail *cd = RPC_I(inode)->private;
1729
1730 return cache_open(inode, filp, cd);
1731}
1732
1733static int cache_release_pipefs(struct inode *inode, struct file *filp)
1734{
1735 struct cache_detail *cd = RPC_I(inode)->private;
1736
1737 return cache_release(inode, filp, cd);
1738}
1739
1740const struct file_operations cache_file_operations_pipefs = {
1741 .owner = THIS_MODULE,
1742 .llseek = no_llseek,
1743 .read = cache_read_pipefs,
1744 .write = cache_write_pipefs,
1745 .poll = cache_poll_pipefs,
Frederic Weisbecker9918ff22010-05-19 15:08:17 +02001746 .unlocked_ioctl = cache_ioctl_pipefs, /* for FIONREAD */
Trond Myklebust8854e822009-08-09 15:14:30 -04001747 .open = cache_open_pipefs,
1748 .release = cache_release_pipefs,
1749};
1750
1751static int content_open_pipefs(struct inode *inode, struct file *filp)
1752{
1753 struct cache_detail *cd = RPC_I(inode)->private;
1754
1755 return content_open(inode, filp, cd);
1756}
1757
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001758static int content_release_pipefs(struct inode *inode, struct file *filp)
1759{
1760 struct cache_detail *cd = RPC_I(inode)->private;
1761
1762 return content_release(inode, filp, cd);
1763}
1764
Trond Myklebust8854e822009-08-09 15:14:30 -04001765const struct file_operations content_file_operations_pipefs = {
1766 .open = content_open_pipefs,
1767 .read = seq_read,
1768 .llseek = seq_lseek,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001769 .release = content_release_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001770};
1771
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001772static int open_flush_pipefs(struct inode *inode, struct file *filp)
1773{
1774 struct cache_detail *cd = RPC_I(inode)->private;
1775
1776 return open_flush(inode, filp, cd);
1777}
1778
1779static int release_flush_pipefs(struct inode *inode, struct file *filp)
1780{
1781 struct cache_detail *cd = RPC_I(inode)->private;
1782
1783 return release_flush(inode, filp, cd);
1784}
1785
Trond Myklebust8854e822009-08-09 15:14:30 -04001786static ssize_t read_flush_pipefs(struct file *filp, char __user *buf,
1787 size_t count, loff_t *ppos)
1788{
Al Viro496ad9a2013-01-23 17:07:38 -05001789 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001790
1791 return read_flush(filp, buf, count, ppos, cd);
1792}
1793
1794static ssize_t write_flush_pipefs(struct file *filp,
1795 const char __user *buf,
1796 size_t count, loff_t *ppos)
1797{
Al Viro496ad9a2013-01-23 17:07:38 -05001798 struct cache_detail *cd = RPC_I(file_inode(filp))->private;
Trond Myklebust8854e822009-08-09 15:14:30 -04001799
1800 return write_flush(filp, buf, count, ppos, cd);
1801}
1802
1803const struct file_operations cache_flush_operations_pipefs = {
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001804 .open = open_flush_pipefs,
Trond Myklebust8854e822009-08-09 15:14:30 -04001805 .read = read_flush_pipefs,
1806 .write = write_flush_pipefs,
Trond Myklebustf7e86ab2009-08-19 18:13:00 -04001807 .release = release_flush_pipefs,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001808 .llseek = no_llseek,
Trond Myklebust8854e822009-08-09 15:14:30 -04001809};
1810
1811int sunrpc_cache_register_pipefs(struct dentry *parent,
Al Viro64f14262011-07-25 00:35:13 -04001812 const char *name, umode_t umode,
Trond Myklebust8854e822009-08-09 15:14:30 -04001813 struct cache_detail *cd)
1814{
Al Viroa95e6912013-07-14 16:43:54 +04001815 struct dentry *dir = rpc_create_cache_dir(parent, name, umode, cd);
1816 if (IS_ERR(dir))
1817 return PTR_ERR(dir);
1818 cd->u.pipefs.dir = dir;
1819 return 0;
Trond Myklebust8854e822009-08-09 15:14:30 -04001820}
1821EXPORT_SYMBOL_GPL(sunrpc_cache_register_pipefs);
1822
1823void sunrpc_cache_unregister_pipefs(struct cache_detail *cd)
1824{
1825 rpc_remove_cache_dir(cd->u.pipefs.dir);
1826 cd->u.pipefs.dir = NULL;
Trond Myklebust8854e822009-08-09 15:14:30 -04001827}
1828EXPORT_SYMBOL_GPL(sunrpc_cache_unregister_pipefs);
1829